blob: 13e6d6e2696460e25f7a1e07ef1db4ab82aeeef2 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalfunc.c: Builtin functions
12 */
13#define USING_FLOAT_STUFF
14
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
23#ifdef VMS
24# include <float.h>
25#endif
26
Bram Moolenaard0573012017-10-28 21:11:06 +020027#ifdef MACOS_X
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020028# include <time.h> /* for time_t */
29#endif
30
31static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020032static char *e_stringreq = N_("E928: String required");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020033
34#ifdef FEAT_FLOAT
35static void f_abs(typval_T *argvars, typval_T *rettv);
36static void f_acos(typval_T *argvars, typval_T *rettv);
37#endif
38static void f_add(typval_T *argvars, typval_T *rettv);
39static void f_and(typval_T *argvars, typval_T *rettv);
40static void f_append(typval_T *argvars, typval_T *rettv);
Bram Moolenaarca851592018-06-06 21:04:07 +020041static void f_appendbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020042static void f_argc(typval_T *argvars, typval_T *rettv);
43static void f_argidx(typval_T *argvars, typval_T *rettv);
44static void f_arglistid(typval_T *argvars, typval_T *rettv);
45static void f_argv(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +010046static void f_assert_beeps(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020047static void f_assert_equal(typval_T *argvars, typval_T *rettv);
Bram Moolenaard96ff162018-02-18 22:13:29 +010048static void f_assert_equalfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020049static void f_assert_exception(typval_T *argvars, typval_T *rettv);
50static void f_assert_fails(typval_T *argvars, typval_T *rettv);
51static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaar61c04492016-07-23 15:35:35 +020052static void f_assert_inrange(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020053static void f_assert_match(typval_T *argvars, typval_T *rettv);
54static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
55static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar42205552017-03-18 19:42:22 +010056static void f_assert_report(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020057static void f_assert_true(typval_T *argvars, typval_T *rettv);
58#ifdef FEAT_FLOAT
59static void f_asin(typval_T *argvars, typval_T *rettv);
60static void f_atan(typval_T *argvars, typval_T *rettv);
61static void f_atan2(typval_T *argvars, typval_T *rettv);
62#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010063#ifdef FEAT_BEVAL
64static void f_balloon_show(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010065# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010066static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010067# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010068#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020069static void f_browse(typval_T *argvars, typval_T *rettv);
70static void f_browsedir(typval_T *argvars, typval_T *rettv);
71static void f_bufexists(typval_T *argvars, typval_T *rettv);
72static void f_buflisted(typval_T *argvars, typval_T *rettv);
73static void f_bufloaded(typval_T *argvars, typval_T *rettv);
74static void f_bufname(typval_T *argvars, typval_T *rettv);
75static void f_bufnr(typval_T *argvars, typval_T *rettv);
76static void f_bufwinid(typval_T *argvars, typval_T *rettv);
77static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
78static void f_byte2line(typval_T *argvars, typval_T *rettv);
79static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
80static void f_byteidx(typval_T *argvars, typval_T *rettv);
81static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
82static void f_call(typval_T *argvars, typval_T *rettv);
83#ifdef FEAT_FLOAT
84static void f_ceil(typval_T *argvars, typval_T *rettv);
85#endif
86#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +010087static void f_ch_canread(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020088static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0874a832016-09-01 15:11:51 +020089static void f_ch_close_in(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020090static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
91static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
92static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
93static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
94static void f_ch_info(typval_T *argvars, typval_T *rettv);
95static void f_ch_log(typval_T *argvars, typval_T *rettv);
96static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
97static void f_ch_open(typval_T *argvars, typval_T *rettv);
98static void f_ch_read(typval_T *argvars, typval_T *rettv);
99static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
100static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
101static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
102static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
103static void f_ch_status(typval_T *argvars, typval_T *rettv);
104#endif
105static void f_changenr(typval_T *argvars, typval_T *rettv);
106static void f_char2nr(typval_T *argvars, typval_T *rettv);
107static void f_cindent(typval_T *argvars, typval_T *rettv);
108static void f_clearmatches(typval_T *argvars, typval_T *rettv);
109static void f_col(typval_T *argvars, typval_T *rettv);
110#if defined(FEAT_INS_EXPAND)
111static void f_complete(typval_T *argvars, typval_T *rettv);
112static void f_complete_add(typval_T *argvars, typval_T *rettv);
113static void f_complete_check(typval_T *argvars, typval_T *rettv);
114#endif
115static void f_confirm(typval_T *argvars, typval_T *rettv);
116static void f_copy(typval_T *argvars, typval_T *rettv);
117#ifdef FEAT_FLOAT
118static void f_cos(typval_T *argvars, typval_T *rettv);
119static void f_cosh(typval_T *argvars, typval_T *rettv);
120#endif
121static void f_count(typval_T *argvars, typval_T *rettv);
122static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
123static void f_cursor(typval_T *argsvars, typval_T *rettv);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200124#ifdef WIN3264
125static void f_debugbreak(typval_T *argvars, typval_T *rettv);
126#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200127static void f_deepcopy(typval_T *argvars, typval_T *rettv);
128static void f_delete(typval_T *argvars, typval_T *rettv);
Bram Moolenaard79a2622018-06-07 18:17:46 +0200129static void f_deletebufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200130static void f_did_filetype(typval_T *argvars, typval_T *rettv);
131static void f_diff_filler(typval_T *argvars, typval_T *rettv);
132static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
133static void f_empty(typval_T *argvars, typval_T *rettv);
134static void f_escape(typval_T *argvars, typval_T *rettv);
135static void f_eval(typval_T *argvars, typval_T *rettv);
136static void f_eventhandler(typval_T *argvars, typval_T *rettv);
137static void f_executable(typval_T *argvars, typval_T *rettv);
138static void f_execute(typval_T *argvars, typval_T *rettv);
139static void f_exepath(typval_T *argvars, typval_T *rettv);
140static void f_exists(typval_T *argvars, typval_T *rettv);
141#ifdef FEAT_FLOAT
142static void f_exp(typval_T *argvars, typval_T *rettv);
143#endif
144static void f_expand(typval_T *argvars, typval_T *rettv);
145static void f_extend(typval_T *argvars, typval_T *rettv);
146static void f_feedkeys(typval_T *argvars, typval_T *rettv);
147static void f_filereadable(typval_T *argvars, typval_T *rettv);
148static void f_filewritable(typval_T *argvars, typval_T *rettv);
149static void f_filter(typval_T *argvars, typval_T *rettv);
150static void f_finddir(typval_T *argvars, typval_T *rettv);
151static void f_findfile(typval_T *argvars, typval_T *rettv);
152#ifdef FEAT_FLOAT
153static void f_float2nr(typval_T *argvars, typval_T *rettv);
154static void f_floor(typval_T *argvars, typval_T *rettv);
155static void f_fmod(typval_T *argvars, typval_T *rettv);
156#endif
157static void f_fnameescape(typval_T *argvars, typval_T *rettv);
158static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
159static void f_foldclosed(typval_T *argvars, typval_T *rettv);
160static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
161static void f_foldlevel(typval_T *argvars, typval_T *rettv);
162static void f_foldtext(typval_T *argvars, typval_T *rettv);
163static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
164static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200165static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200166static void f_function(typval_T *argvars, typval_T *rettv);
167static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
168static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200169static void f_getbufinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200170static void f_getbufline(typval_T *argvars, typval_T *rettv);
171static void f_getbufvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100172static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200173static void f_getchar(typval_T *argvars, typval_T *rettv);
174static void f_getcharmod(typval_T *argvars, typval_T *rettv);
175static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
176static void f_getcmdline(typval_T *argvars, typval_T *rettv);
177#if defined(FEAT_CMDL_COMPL)
178static void f_getcompletion(typval_T *argvars, typval_T *rettv);
179#endif
180static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
181static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
182static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
183static void f_getcwd(typval_T *argvars, typval_T *rettv);
184static void f_getfontname(typval_T *argvars, typval_T *rettv);
185static void f_getfperm(typval_T *argvars, typval_T *rettv);
186static void f_getfsize(typval_T *argvars, typval_T *rettv);
187static void f_getftime(typval_T *argvars, typval_T *rettv);
188static void f_getftype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +0100189static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200190static void f_getline(typval_T *argvars, typval_T *rettv);
Bram Moolenaard823fa92016-08-12 16:29:27 +0200191static void f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200192static void f_getmatches(typval_T *argvars, typval_T *rettv);
193static void f_getpid(typval_T *argvars, typval_T *rettv);
194static void f_getcurpos(typval_T *argvars, typval_T *rettv);
195static void f_getpos(typval_T *argvars, typval_T *rettv);
196static void f_getqflist(typval_T *argvars, typval_T *rettv);
197static void f_getreg(typval_T *argvars, typval_T *rettv);
198static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200199static void f_gettabinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200200static void f_gettabvar(typval_T *argvars, typval_T *rettv);
201static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100202static void f_gettagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200203static void f_getwininfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100204static void f_getwinpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200205static void f_getwinposx(typval_T *argvars, typval_T *rettv);
206static void f_getwinposy(typval_T *argvars, typval_T *rettv);
207static void f_getwinvar(typval_T *argvars, typval_T *rettv);
208static void f_glob(typval_T *argvars, typval_T *rettv);
209static void f_globpath(typval_T *argvars, typval_T *rettv);
210static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
211static void f_has(typval_T *argvars, typval_T *rettv);
212static void f_has_key(typval_T *argvars, typval_T *rettv);
213static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
214static void f_hasmapto(typval_T *argvars, typval_T *rettv);
215static void f_histadd(typval_T *argvars, typval_T *rettv);
216static void f_histdel(typval_T *argvars, typval_T *rettv);
217static void f_histget(typval_T *argvars, typval_T *rettv);
218static void f_histnr(typval_T *argvars, typval_T *rettv);
219static void f_hlID(typval_T *argvars, typval_T *rettv);
220static void f_hlexists(typval_T *argvars, typval_T *rettv);
221static void f_hostname(typval_T *argvars, typval_T *rettv);
222static void f_iconv(typval_T *argvars, typval_T *rettv);
223static void f_indent(typval_T *argvars, typval_T *rettv);
224static void f_index(typval_T *argvars, typval_T *rettv);
225static void f_input(typval_T *argvars, typval_T *rettv);
226static void f_inputdialog(typval_T *argvars, typval_T *rettv);
227static void f_inputlist(typval_T *argvars, typval_T *rettv);
228static void f_inputrestore(typval_T *argvars, typval_T *rettv);
229static void f_inputsave(typval_T *argvars, typval_T *rettv);
230static void f_inputsecret(typval_T *argvars, typval_T *rettv);
231static void f_insert(typval_T *argvars, typval_T *rettv);
232static void f_invert(typval_T *argvars, typval_T *rettv);
233static void f_isdirectory(typval_T *argvars, typval_T *rettv);
234static void f_islocked(typval_T *argvars, typval_T *rettv);
235#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
236static void f_isnan(typval_T *argvars, typval_T *rettv);
237#endif
238static void f_items(typval_T *argvars, typval_T *rettv);
239#ifdef FEAT_JOB_CHANNEL
240static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
241static void f_job_info(typval_T *argvars, typval_T *rettv);
242static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
243static void f_job_start(typval_T *argvars, typval_T *rettv);
244static void f_job_stop(typval_T *argvars, typval_T *rettv);
245static void f_job_status(typval_T *argvars, typval_T *rettv);
246#endif
247static void f_join(typval_T *argvars, typval_T *rettv);
248static void f_js_decode(typval_T *argvars, typval_T *rettv);
249static void f_js_encode(typval_T *argvars, typval_T *rettv);
250static void f_json_decode(typval_T *argvars, typval_T *rettv);
251static void f_json_encode(typval_T *argvars, typval_T *rettv);
252static void f_keys(typval_T *argvars, typval_T *rettv);
253static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
254static void f_len(typval_T *argvars, typval_T *rettv);
255static void f_libcall(typval_T *argvars, typval_T *rettv);
256static void f_libcallnr(typval_T *argvars, typval_T *rettv);
257static void f_line(typval_T *argvars, typval_T *rettv);
258static void f_line2byte(typval_T *argvars, typval_T *rettv);
259static void f_lispindent(typval_T *argvars, typval_T *rettv);
260static void f_localtime(typval_T *argvars, typval_T *rettv);
261#ifdef FEAT_FLOAT
262static void f_log(typval_T *argvars, typval_T *rettv);
263static void f_log10(typval_T *argvars, typval_T *rettv);
264#endif
265#ifdef FEAT_LUA
266static void f_luaeval(typval_T *argvars, typval_T *rettv);
267#endif
268static void f_map(typval_T *argvars, typval_T *rettv);
269static void f_maparg(typval_T *argvars, typval_T *rettv);
270static void f_mapcheck(typval_T *argvars, typval_T *rettv);
271static void f_match(typval_T *argvars, typval_T *rettv);
272static void f_matchadd(typval_T *argvars, typval_T *rettv);
273static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
274static void f_matcharg(typval_T *argvars, typval_T *rettv);
275static void f_matchdelete(typval_T *argvars, typval_T *rettv);
276static void f_matchend(typval_T *argvars, typval_T *rettv);
277static void f_matchlist(typval_T *argvars, typval_T *rettv);
278static void f_matchstr(typval_T *argvars, typval_T *rettv);
279static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
280static void f_max(typval_T *argvars, typval_T *rettv);
281static void f_min(typval_T *argvars, typval_T *rettv);
282#ifdef vim_mkdir
283static void f_mkdir(typval_T *argvars, typval_T *rettv);
284#endif
285static void f_mode(typval_T *argvars, typval_T *rettv);
286#ifdef FEAT_MZSCHEME
287static void f_mzeval(typval_T *argvars, typval_T *rettv);
288#endif
289static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
290static void f_nr2char(typval_T *argvars, typval_T *rettv);
291static void f_or(typval_T *argvars, typval_T *rettv);
292static void f_pathshorten(typval_T *argvars, typval_T *rettv);
293#ifdef FEAT_PERL
294static void f_perleval(typval_T *argvars, typval_T *rettv);
295#endif
296#ifdef FEAT_FLOAT
297static void f_pow(typval_T *argvars, typval_T *rettv);
298#endif
299static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
300static void f_printf(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200301#ifdef FEAT_JOB_CHANNEL
302static void f_prompt_setcallback(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200303static void f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200304static void f_prompt_setprompt(typval_T *argvars, typval_T *rettv);
305#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200306static void f_pumvisible(typval_T *argvars, typval_T *rettv);
307#ifdef FEAT_PYTHON3
308static void f_py3eval(typval_T *argvars, typval_T *rettv);
309#endif
310#ifdef FEAT_PYTHON
311static void f_pyeval(typval_T *argvars, typval_T *rettv);
312#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100313#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
314static void f_pyxeval(typval_T *argvars, typval_T *rettv);
315#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200316static void f_range(typval_T *argvars, typval_T *rettv);
317static void f_readfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200318static void f_reg_executing(typval_T *argvars, typval_T *rettv);
319static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200320static void f_reltime(typval_T *argvars, typval_T *rettv);
321#ifdef FEAT_FLOAT
322static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
323#endif
324static void f_reltimestr(typval_T *argvars, typval_T *rettv);
325static void f_remote_expr(typval_T *argvars, typval_T *rettv);
326static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
327static void f_remote_peek(typval_T *argvars, typval_T *rettv);
328static void f_remote_read(typval_T *argvars, typval_T *rettv);
329static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100330static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200331static void f_remove(typval_T *argvars, typval_T *rettv);
332static void f_rename(typval_T *argvars, typval_T *rettv);
333static void f_repeat(typval_T *argvars, typval_T *rettv);
334static void f_resolve(typval_T *argvars, typval_T *rettv);
335static void f_reverse(typval_T *argvars, typval_T *rettv);
336#ifdef FEAT_FLOAT
337static void f_round(typval_T *argvars, typval_T *rettv);
338#endif
339static void f_screenattr(typval_T *argvars, typval_T *rettv);
340static void f_screenchar(typval_T *argvars, typval_T *rettv);
341static void f_screencol(typval_T *argvars, typval_T *rettv);
342static void f_screenrow(typval_T *argvars, typval_T *rettv);
343static void f_search(typval_T *argvars, typval_T *rettv);
344static void f_searchdecl(typval_T *argvars, typval_T *rettv);
345static void f_searchpair(typval_T *argvars, typval_T *rettv);
346static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
347static void f_searchpos(typval_T *argvars, typval_T *rettv);
348static void f_server2client(typval_T *argvars, typval_T *rettv);
349static void f_serverlist(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200350static void f_setbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200351static void f_setbufvar(typval_T *argvars, typval_T *rettv);
352static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
353static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
354static void f_setfperm(typval_T *argvars, typval_T *rettv);
355static void f_setline(typval_T *argvars, typval_T *rettv);
356static void f_setloclist(typval_T *argvars, typval_T *rettv);
357static void f_setmatches(typval_T *argvars, typval_T *rettv);
358static void f_setpos(typval_T *argvars, typval_T *rettv);
359static void f_setqflist(typval_T *argvars, typval_T *rettv);
360static void f_setreg(typval_T *argvars, typval_T *rettv);
361static void f_settabvar(typval_T *argvars, typval_T *rettv);
362static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100363static void f_settagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200364static void f_setwinvar(typval_T *argvars, typval_T *rettv);
365#ifdef FEAT_CRYPT
366static void f_sha256(typval_T *argvars, typval_T *rettv);
367#endif /* FEAT_CRYPT */
368static void f_shellescape(typval_T *argvars, typval_T *rettv);
369static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
Bram Moolenaar162b7142018-12-21 15:17:36 +0100370#ifdef FEAT_SIGNS
371static void f_sign_define(typval_T *argvars, typval_T *rettv);
372static void f_sign_getdefined(typval_T *argvars, typval_T *rettv);
373static void f_sign_getplaced(typval_T *argvars, typval_T *rettv);
374static void f_sign_place(typval_T *argvars, typval_T *rettv);
375static void f_sign_undefine(typval_T *argvars, typval_T *rettv);
376static void f_sign_unplace(typval_T *argvars, typval_T *rettv);
377#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200378static void f_simplify(typval_T *argvars, typval_T *rettv);
379#ifdef FEAT_FLOAT
380static void f_sin(typval_T *argvars, typval_T *rettv);
381static void f_sinh(typval_T *argvars, typval_T *rettv);
382#endif
383static void f_sort(typval_T *argvars, typval_T *rettv);
384static void f_soundfold(typval_T *argvars, typval_T *rettv);
385static void f_spellbadword(typval_T *argvars, typval_T *rettv);
386static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
387static void f_split(typval_T *argvars, typval_T *rettv);
388#ifdef FEAT_FLOAT
389static void f_sqrt(typval_T *argvars, typval_T *rettv);
390static void f_str2float(typval_T *argvars, typval_T *rettv);
391#endif
392static void f_str2nr(typval_T *argvars, typval_T *rettv);
393static void f_strchars(typval_T *argvars, typval_T *rettv);
394#ifdef HAVE_STRFTIME
395static void f_strftime(typval_T *argvars, typval_T *rettv);
396#endif
397static void f_strgetchar(typval_T *argvars, typval_T *rettv);
398static void f_stridx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200399static void f_strlen(typval_T *argvars, typval_T *rettv);
400static void f_strcharpart(typval_T *argvars, typval_T *rettv);
401static void f_strpart(typval_T *argvars, typval_T *rettv);
402static void f_strridx(typval_T *argvars, typval_T *rettv);
403static void f_strtrans(typval_T *argvars, typval_T *rettv);
404static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
405static void f_strwidth(typval_T *argvars, typval_T *rettv);
406static void f_submatch(typval_T *argvars, typval_T *rettv);
407static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200408static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200409static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200410static void f_synID(typval_T *argvars, typval_T *rettv);
411static void f_synIDattr(typval_T *argvars, typval_T *rettv);
412static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
413static void f_synstack(typval_T *argvars, typval_T *rettv);
414static void f_synconcealed(typval_T *argvars, typval_T *rettv);
415static void f_system(typval_T *argvars, typval_T *rettv);
416static void f_systemlist(typval_T *argvars, typval_T *rettv);
417static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
418static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
419static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
420static void f_taglist(typval_T *argvars, typval_T *rettv);
421static void f_tagfiles(typval_T *argvars, typval_T *rettv);
422static void f_tempname(typval_T *argvars, typval_T *rettv);
423static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
424static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200425static void f_test_feedinput(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200426static void f_test_option_not_set(typval_T *argvars, typval_T *rettv);
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100427static void f_test_override(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200428static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100429static void f_test_ignore_error(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200430#ifdef FEAT_JOB_CHANNEL
431static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
432#endif
433static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
434#ifdef FEAT_JOB_CHANNEL
435static void f_test_null_job(typval_T *argvars, typval_T *rettv);
436#endif
437static void f_test_null_list(typval_T *argvars, typval_T *rettv);
438static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
439static void f_test_null_string(typval_T *argvars, typval_T *rettv);
Bram Moolenaarab186732018-09-14 21:27:06 +0200440#ifdef FEAT_GUI
441static void f_test_scrollbar(typval_T *argvars, typval_T *rettv);
442#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200443static void f_test_settime(typval_T *argvars, typval_T *rettv);
444#ifdef FEAT_FLOAT
445static void f_tan(typval_T *argvars, typval_T *rettv);
446static void f_tanh(typval_T *argvars, typval_T *rettv);
447#endif
448#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200449static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200450static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200451static void f_timer_start(typval_T *argvars, typval_T *rettv);
452static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200453static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200454#endif
455static void f_tolower(typval_T *argvars, typval_T *rettv);
456static void f_toupper(typval_T *argvars, typval_T *rettv);
457static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100458static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200459#ifdef FEAT_FLOAT
460static void f_trunc(typval_T *argvars, typval_T *rettv);
461#endif
462static void f_type(typval_T *argvars, typval_T *rettv);
463static void f_undofile(typval_T *argvars, typval_T *rettv);
464static void f_undotree(typval_T *argvars, typval_T *rettv);
465static void f_uniq(typval_T *argvars, typval_T *rettv);
466static void f_values(typval_T *argvars, typval_T *rettv);
467static void f_virtcol(typval_T *argvars, typval_T *rettv);
468static void f_visualmode(typval_T *argvars, typval_T *rettv);
469static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
470static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
471static void f_win_getid(typval_T *argvars, typval_T *rettv);
472static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
473static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
474static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100475static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200476static void f_winbufnr(typval_T *argvars, typval_T *rettv);
477static void f_wincol(typval_T *argvars, typval_T *rettv);
478static void f_winheight(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200479static void f_winlayout(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200480static void f_winline(typval_T *argvars, typval_T *rettv);
481static void f_winnr(typval_T *argvars, typval_T *rettv);
482static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
483static void f_winrestview(typval_T *argvars, typval_T *rettv);
484static void f_winsaveview(typval_T *argvars, typval_T *rettv);
485static void f_winwidth(typval_T *argvars, typval_T *rettv);
486static void f_writefile(typval_T *argvars, typval_T *rettv);
487static void f_wordcount(typval_T *argvars, typval_T *rettv);
488static void f_xor(typval_T *argvars, typval_T *rettv);
489
490/*
491 * Array with names and number of arguments of all internal functions
492 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
493 */
494static struct fst
495{
496 char *f_name; /* function name */
497 char f_min_argc; /* minimal number of arguments */
498 char f_max_argc; /* maximal number of arguments */
499 void (*f_func)(typval_T *args, typval_T *rvar);
500 /* implementation of function */
501} functions[] =
502{
503#ifdef FEAT_FLOAT
504 {"abs", 1, 1, f_abs},
505 {"acos", 1, 1, f_acos}, /* WJMc */
506#endif
507 {"add", 2, 2, f_add},
508 {"and", 2, 2, f_and},
509 {"append", 2, 2, f_append},
Bram Moolenaarca851592018-06-06 21:04:07 +0200510 {"appendbufline", 3, 3, f_appendbufline},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200511 {"argc", 0, 1, f_argc},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200512 {"argidx", 0, 0, f_argidx},
513 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200514 {"argv", 0, 2, f_argv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200515#ifdef FEAT_FLOAT
516 {"asin", 1, 1, f_asin}, /* WJMc */
517#endif
Bram Moolenaarb48e96f2018-02-13 12:26:14 +0100518 {"assert_beeps", 1, 2, f_assert_beeps},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200519 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaard96ff162018-02-18 22:13:29 +0100520 {"assert_equalfile", 2, 2, f_assert_equalfile},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200521 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaar1307d1c2018-10-07 20:16:49 +0200522 {"assert_fails", 1, 3, f_assert_fails},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200523 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100524 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200525 {"assert_match", 2, 3, f_assert_match},
526 {"assert_notequal", 2, 3, f_assert_notequal},
527 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100528 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200529 {"assert_true", 1, 2, f_assert_true},
530#ifdef FEAT_FLOAT
531 {"atan", 1, 1, f_atan},
532 {"atan2", 2, 2, f_atan2},
533#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100534#ifdef FEAT_BEVAL
535 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100536# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100537 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100538# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100539#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200540 {"browse", 4, 4, f_browse},
541 {"browsedir", 2, 2, f_browsedir},
542 {"bufexists", 1, 1, f_bufexists},
543 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
544 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
545 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
546 {"buflisted", 1, 1, f_buflisted},
547 {"bufloaded", 1, 1, f_bufloaded},
548 {"bufname", 1, 1, f_bufname},
549 {"bufnr", 1, 2, f_bufnr},
550 {"bufwinid", 1, 1, f_bufwinid},
551 {"bufwinnr", 1, 1, f_bufwinnr},
552 {"byte2line", 1, 1, f_byte2line},
553 {"byteidx", 2, 2, f_byteidx},
554 {"byteidxcomp", 2, 2, f_byteidxcomp},
555 {"call", 2, 3, f_call},
556#ifdef FEAT_FLOAT
557 {"ceil", 1, 1, f_ceil},
558#endif
559#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100560 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200561 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200562 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200563 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
564 {"ch_evalraw", 2, 3, f_ch_evalraw},
565 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
566 {"ch_getjob", 1, 1, f_ch_getjob},
567 {"ch_info", 1, 1, f_ch_info},
568 {"ch_log", 1, 2, f_ch_log},
569 {"ch_logfile", 1, 2, f_ch_logfile},
570 {"ch_open", 1, 2, f_ch_open},
571 {"ch_read", 1, 2, f_ch_read},
572 {"ch_readraw", 1, 2, f_ch_readraw},
573 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
574 {"ch_sendraw", 2, 3, f_ch_sendraw},
575 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200576 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200577#endif
578 {"changenr", 0, 0, f_changenr},
579 {"char2nr", 1, 2, f_char2nr},
580 {"cindent", 1, 1, f_cindent},
581 {"clearmatches", 0, 0, f_clearmatches},
582 {"col", 1, 1, f_col},
583#if defined(FEAT_INS_EXPAND)
584 {"complete", 2, 2, f_complete},
585 {"complete_add", 1, 1, f_complete_add},
586 {"complete_check", 0, 0, f_complete_check},
587#endif
588 {"confirm", 1, 4, f_confirm},
589 {"copy", 1, 1, f_copy},
590#ifdef FEAT_FLOAT
591 {"cos", 1, 1, f_cos},
592 {"cosh", 1, 1, f_cosh},
593#endif
594 {"count", 2, 4, f_count},
595 {"cscope_connection",0,3, f_cscope_connection},
596 {"cursor", 1, 3, f_cursor},
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200597#ifdef WIN3264
598 {"debugbreak", 1, 1, f_debugbreak},
599#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200600 {"deepcopy", 1, 2, f_deepcopy},
601 {"delete", 1, 2, f_delete},
Bram Moolenaard79a2622018-06-07 18:17:46 +0200602 {"deletebufline", 2, 3, f_deletebufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200603 {"did_filetype", 0, 0, f_did_filetype},
604 {"diff_filler", 1, 1, f_diff_filler},
605 {"diff_hlID", 2, 2, f_diff_hlID},
606 {"empty", 1, 1, f_empty},
607 {"escape", 2, 2, f_escape},
608 {"eval", 1, 1, f_eval},
609 {"eventhandler", 0, 0, f_eventhandler},
610 {"executable", 1, 1, f_executable},
611 {"execute", 1, 2, f_execute},
612 {"exepath", 1, 1, f_exepath},
613 {"exists", 1, 1, f_exists},
614#ifdef FEAT_FLOAT
615 {"exp", 1, 1, f_exp},
616#endif
617 {"expand", 1, 3, f_expand},
618 {"extend", 2, 3, f_extend},
619 {"feedkeys", 1, 2, f_feedkeys},
620 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
621 {"filereadable", 1, 1, f_filereadable},
622 {"filewritable", 1, 1, f_filewritable},
623 {"filter", 2, 2, f_filter},
624 {"finddir", 1, 3, f_finddir},
625 {"findfile", 1, 3, f_findfile},
626#ifdef FEAT_FLOAT
627 {"float2nr", 1, 1, f_float2nr},
628 {"floor", 1, 1, f_floor},
629 {"fmod", 2, 2, f_fmod},
630#endif
631 {"fnameescape", 1, 1, f_fnameescape},
632 {"fnamemodify", 2, 2, f_fnamemodify},
633 {"foldclosed", 1, 1, f_foldclosed},
634 {"foldclosedend", 1, 1, f_foldclosedend},
635 {"foldlevel", 1, 1, f_foldlevel},
636 {"foldtext", 0, 0, f_foldtext},
637 {"foldtextresult", 1, 1, f_foldtextresult},
638 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200639 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200640 {"function", 1, 3, f_function},
641 {"garbagecollect", 0, 1, f_garbagecollect},
642 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200643 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200644 {"getbufline", 2, 3, f_getbufline},
645 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100646 {"getchangelist", 1, 1, f_getchangelist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200647 {"getchar", 0, 1, f_getchar},
648 {"getcharmod", 0, 0, f_getcharmod},
649 {"getcharsearch", 0, 0, f_getcharsearch},
650 {"getcmdline", 0, 0, f_getcmdline},
651 {"getcmdpos", 0, 0, f_getcmdpos},
652 {"getcmdtype", 0, 0, f_getcmdtype},
653 {"getcmdwintype", 0, 0, f_getcmdwintype},
654#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200655 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200656#endif
657 {"getcurpos", 0, 0, f_getcurpos},
658 {"getcwd", 0, 2, f_getcwd},
659 {"getfontname", 0, 1, f_getfontname},
660 {"getfperm", 1, 1, f_getfperm},
661 {"getfsize", 1, 1, f_getfsize},
662 {"getftime", 1, 1, f_getftime},
663 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100664 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200665 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200666 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200667 {"getmatches", 0, 0, f_getmatches},
668 {"getpid", 0, 0, f_getpid},
669 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200670 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200671 {"getreg", 0, 3, f_getreg},
672 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200673 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200674 {"gettabvar", 2, 3, f_gettabvar},
675 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100676 {"gettagstack", 0, 1, f_gettagstack},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200677 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100678 {"getwinpos", 0, 1, f_getwinpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200679 {"getwinposx", 0, 0, f_getwinposx},
680 {"getwinposy", 0, 0, f_getwinposy},
681 {"getwinvar", 2, 3, f_getwinvar},
682 {"glob", 1, 4, f_glob},
683 {"glob2regpat", 1, 1, f_glob2regpat},
684 {"globpath", 2, 5, f_globpath},
685 {"has", 1, 1, f_has},
686 {"has_key", 2, 2, f_has_key},
687 {"haslocaldir", 0, 2, f_haslocaldir},
688 {"hasmapto", 1, 3, f_hasmapto},
689 {"highlightID", 1, 1, f_hlID}, /* obsolete */
690 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
691 {"histadd", 2, 2, f_histadd},
692 {"histdel", 1, 2, f_histdel},
693 {"histget", 1, 2, f_histget},
694 {"histnr", 1, 1, f_histnr},
695 {"hlID", 1, 1, f_hlID},
696 {"hlexists", 1, 1, f_hlexists},
697 {"hostname", 0, 0, f_hostname},
698 {"iconv", 3, 3, f_iconv},
699 {"indent", 1, 1, f_indent},
700 {"index", 2, 4, f_index},
701 {"input", 1, 3, f_input},
702 {"inputdialog", 1, 3, f_inputdialog},
703 {"inputlist", 1, 1, f_inputlist},
704 {"inputrestore", 0, 0, f_inputrestore},
705 {"inputsave", 0, 0, f_inputsave},
706 {"inputsecret", 1, 2, f_inputsecret},
707 {"insert", 2, 3, f_insert},
708 {"invert", 1, 1, f_invert},
709 {"isdirectory", 1, 1, f_isdirectory},
710 {"islocked", 1, 1, f_islocked},
711#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
712 {"isnan", 1, 1, f_isnan},
713#endif
714 {"items", 1, 1, f_items},
715#ifdef FEAT_JOB_CHANNEL
716 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200717 {"job_info", 0, 1, f_job_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200718 {"job_setoptions", 2, 2, f_job_setoptions},
719 {"job_start", 1, 2, f_job_start},
720 {"job_status", 1, 1, f_job_status},
721 {"job_stop", 1, 2, f_job_stop},
722#endif
723 {"join", 1, 2, f_join},
724 {"js_decode", 1, 1, f_js_decode},
725 {"js_encode", 1, 1, f_js_encode},
726 {"json_decode", 1, 1, f_json_decode},
727 {"json_encode", 1, 1, f_json_encode},
728 {"keys", 1, 1, f_keys},
729 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
730 {"len", 1, 1, f_len},
731 {"libcall", 3, 3, f_libcall},
732 {"libcallnr", 3, 3, f_libcallnr},
733 {"line", 1, 1, f_line},
734 {"line2byte", 1, 1, f_line2byte},
735 {"lispindent", 1, 1, f_lispindent},
736 {"localtime", 0, 0, f_localtime},
737#ifdef FEAT_FLOAT
738 {"log", 1, 1, f_log},
739 {"log10", 1, 1, f_log10},
740#endif
741#ifdef FEAT_LUA
742 {"luaeval", 1, 2, f_luaeval},
743#endif
744 {"map", 2, 2, f_map},
745 {"maparg", 1, 4, f_maparg},
746 {"mapcheck", 1, 3, f_mapcheck},
747 {"match", 2, 4, f_match},
748 {"matchadd", 2, 5, f_matchadd},
749 {"matchaddpos", 2, 5, f_matchaddpos},
750 {"matcharg", 1, 1, f_matcharg},
751 {"matchdelete", 1, 1, f_matchdelete},
752 {"matchend", 2, 4, f_matchend},
753 {"matchlist", 2, 4, f_matchlist},
754 {"matchstr", 2, 4, f_matchstr},
755 {"matchstrpos", 2, 4, f_matchstrpos},
756 {"max", 1, 1, f_max},
757 {"min", 1, 1, f_min},
758#ifdef vim_mkdir
759 {"mkdir", 1, 3, f_mkdir},
760#endif
761 {"mode", 0, 1, f_mode},
762#ifdef FEAT_MZSCHEME
763 {"mzeval", 1, 1, f_mzeval},
764#endif
765 {"nextnonblank", 1, 1, f_nextnonblank},
766 {"nr2char", 1, 2, f_nr2char},
767 {"or", 2, 2, f_or},
768 {"pathshorten", 1, 1, f_pathshorten},
769#ifdef FEAT_PERL
770 {"perleval", 1, 1, f_perleval},
771#endif
772#ifdef FEAT_FLOAT
773 {"pow", 2, 2, f_pow},
774#endif
775 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100776 {"printf", 1, 19, f_printf},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200777#ifdef FEAT_JOB_CHANNEL
778 {"prompt_setcallback", 2, 2, f_prompt_setcallback},
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200779 {"prompt_setinterrupt", 2, 2, f_prompt_setinterrupt},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200780 {"prompt_setprompt", 2, 2, f_prompt_setprompt},
781#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100782#ifdef FEAT_TEXT_PROP
783 {"prop_add", 3, 3, f_prop_add},
784 {"prop_clear", 1, 3, f_prop_clear},
785 {"prop_list", 1, 2, f_prop_list},
786 {"prop_remove", 2, 3, f_prop_remove},
787 {"prop_type_add", 2, 2, f_prop_type_add},
788 {"prop_type_change", 2, 2, f_prop_type_change},
789 {"prop_type_delete", 1, 2, f_prop_type_delete},
790 {"prop_type_get", 1, 2, f_prop_type_get},
791 {"prop_type_list", 0, 1, f_prop_type_list},
792#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200793 {"pumvisible", 0, 0, f_pumvisible},
794#ifdef FEAT_PYTHON3
795 {"py3eval", 1, 1, f_py3eval},
796#endif
797#ifdef FEAT_PYTHON
798 {"pyeval", 1, 1, f_pyeval},
799#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100800#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
801 {"pyxeval", 1, 1, f_pyxeval},
802#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200803 {"range", 1, 3, f_range},
804 {"readfile", 1, 3, f_readfile},
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200805 {"reg_executing", 0, 0, f_reg_executing},
806 {"reg_recording", 0, 0, f_reg_recording},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200807 {"reltime", 0, 2, f_reltime},
808#ifdef FEAT_FLOAT
809 {"reltimefloat", 1, 1, f_reltimefloat},
810#endif
811 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100812 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200813 {"remote_foreground", 1, 1, f_remote_foreground},
814 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100815 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200816 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100817 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200818 {"remove", 2, 3, f_remove},
819 {"rename", 2, 2, f_rename},
820 {"repeat", 2, 2, f_repeat},
821 {"resolve", 1, 1, f_resolve},
822 {"reverse", 1, 1, f_reverse},
823#ifdef FEAT_FLOAT
824 {"round", 1, 1, f_round},
825#endif
826 {"screenattr", 2, 2, f_screenattr},
827 {"screenchar", 2, 2, f_screenchar},
828 {"screencol", 0, 0, f_screencol},
829 {"screenrow", 0, 0, f_screenrow},
830 {"search", 1, 4, f_search},
831 {"searchdecl", 1, 3, f_searchdecl},
832 {"searchpair", 3, 7, f_searchpair},
833 {"searchpairpos", 3, 7, f_searchpairpos},
834 {"searchpos", 1, 4, f_searchpos},
835 {"server2client", 2, 2, f_server2client},
836 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200837 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200838 {"setbufvar", 3, 3, f_setbufvar},
839 {"setcharsearch", 1, 1, f_setcharsearch},
840 {"setcmdpos", 1, 1, f_setcmdpos},
841 {"setfperm", 2, 2, f_setfperm},
842 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200843 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200844 {"setmatches", 1, 1, f_setmatches},
845 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200846 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200847 {"setreg", 2, 3, f_setreg},
848 {"settabvar", 3, 3, f_settabvar},
849 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100850 {"settagstack", 2, 3, f_settagstack},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200851 {"setwinvar", 3, 3, f_setwinvar},
852#ifdef FEAT_CRYPT
853 {"sha256", 1, 1, f_sha256},
854#endif
855 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaarf9514162018-11-22 03:08:29 +0100856 {"shiftwidth", 0, 1, f_shiftwidth},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100857#ifdef FEAT_SIGNS
858 {"sign_define", 1, 2, f_sign_define},
859 {"sign_getdefined", 0, 1, f_sign_getdefined},
860 {"sign_getplaced", 0, 2, f_sign_getplaced},
861 {"sign_place", 4, 5, f_sign_place},
862 {"sign_undefine", 0, 1, f_sign_undefine},
863 {"sign_unplace", 1, 2, f_sign_unplace},
864#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200865 {"simplify", 1, 1, f_simplify},
866#ifdef FEAT_FLOAT
867 {"sin", 1, 1, f_sin},
868 {"sinh", 1, 1, f_sinh},
869#endif
870 {"sort", 1, 3, f_sort},
871 {"soundfold", 1, 1, f_soundfold},
872 {"spellbadword", 0, 1, f_spellbadword},
873 {"spellsuggest", 1, 3, f_spellsuggest},
874 {"split", 1, 3, f_split},
875#ifdef FEAT_FLOAT
876 {"sqrt", 1, 1, f_sqrt},
877 {"str2float", 1, 1, f_str2float},
878#endif
879 {"str2nr", 1, 2, f_str2nr},
880 {"strcharpart", 2, 3, f_strcharpart},
881 {"strchars", 1, 2, f_strchars},
882 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
883#ifdef HAVE_STRFTIME
884 {"strftime", 1, 2, f_strftime},
885#endif
886 {"strgetchar", 2, 2, f_strgetchar},
887 {"stridx", 2, 3, f_stridx},
888 {"string", 1, 1, f_string},
889 {"strlen", 1, 1, f_strlen},
890 {"strpart", 2, 3, f_strpart},
891 {"strridx", 2, 3, f_strridx},
892 {"strtrans", 1, 1, f_strtrans},
893 {"strwidth", 1, 1, f_strwidth},
894 {"submatch", 1, 2, f_submatch},
895 {"substitute", 4, 4, f_substitute},
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200896 {"swapinfo", 1, 1, f_swapinfo},
Bram Moolenaar110bd602018-09-16 18:46:59 +0200897 {"swapname", 1, 1, f_swapname},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200898 {"synID", 3, 3, f_synID},
899 {"synIDattr", 2, 3, f_synIDattr},
900 {"synIDtrans", 1, 1, f_synIDtrans},
901 {"synconcealed", 2, 2, f_synconcealed},
902 {"synstack", 2, 2, f_synstack},
903 {"system", 1, 2, f_system},
904 {"systemlist", 1, 2, f_systemlist},
905 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
906 {"tabpagenr", 0, 1, f_tabpagenr},
907 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
908 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100909 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200910#ifdef FEAT_FLOAT
911 {"tan", 1, 1, f_tan},
912 {"tanh", 1, 1, f_tanh},
913#endif
914 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200915#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100916 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
917 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100918 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200919 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200920# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
921 {"term_getansicolors", 1, 1, f_term_getansicolors},
922# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200923 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200924 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200925 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200926 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200927 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200928 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200929 {"term_getstatus", 1, 1, f_term_getstatus},
930 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200931 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200932 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200933 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200934 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200935# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
936 {"term_setansicolors", 2, 2, f_term_setansicolors},
937# endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100938 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100939 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaara42d3632018-04-14 17:05:38 +0200940 {"term_setsize", 3, 3, f_term_setsize},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200941 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200942 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200943#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200944 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
945 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200946 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200947 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100948 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200949#ifdef FEAT_JOB_CHANNEL
950 {"test_null_channel", 0, 0, f_test_null_channel},
951#endif
952 {"test_null_dict", 0, 0, f_test_null_dict},
953#ifdef FEAT_JOB_CHANNEL
954 {"test_null_job", 0, 0, f_test_null_job},
955#endif
956 {"test_null_list", 0, 0, f_test_null_list},
957 {"test_null_partial", 0, 0, f_test_null_partial},
958 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200959 {"test_option_not_set", 1, 1, f_test_option_not_set},
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100960 {"test_override", 2, 2, f_test_override},
Bram Moolenaarab186732018-09-14 21:27:06 +0200961#ifdef FEAT_GUI
962 {"test_scrollbar", 3, 3, f_test_scrollbar},
963#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200964 {"test_settime", 1, 1, f_test_settime},
965#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200966 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200967 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200968 {"timer_start", 2, 3, f_timer_start},
969 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200970 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200971#endif
972 {"tolower", 1, 1, f_tolower},
973 {"toupper", 1, 1, f_toupper},
974 {"tr", 3, 3, f_tr},
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100975 {"trim", 1, 2, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200976#ifdef FEAT_FLOAT
977 {"trunc", 1, 1, f_trunc},
978#endif
979 {"type", 1, 1, f_type},
980 {"undofile", 1, 1, f_undofile},
981 {"undotree", 0, 0, f_undotree},
982 {"uniq", 1, 3, f_uniq},
983 {"values", 1, 1, f_values},
984 {"virtcol", 1, 1, f_virtcol},
985 {"visualmode", 0, 1, f_visualmode},
986 {"wildmenumode", 0, 0, f_wildmenumode},
987 {"win_findbuf", 1, 1, f_win_findbuf},
988 {"win_getid", 0, 2, f_win_getid},
989 {"win_gotoid", 1, 1, f_win_gotoid},
990 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
991 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100992 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200993 {"winbufnr", 1, 1, f_winbufnr},
994 {"wincol", 0, 0, f_wincol},
995 {"winheight", 1, 1, f_winheight},
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200996 {"winlayout", 0, 1, f_winlayout},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200997 {"winline", 0, 0, f_winline},
998 {"winnr", 0, 1, f_winnr},
999 {"winrestcmd", 0, 0, f_winrestcmd},
1000 {"winrestview", 1, 1, f_winrestview},
1001 {"winsaveview", 0, 0, f_winsaveview},
1002 {"winwidth", 1, 1, f_winwidth},
1003 {"wordcount", 0, 0, f_wordcount},
1004 {"writefile", 2, 3, f_writefile},
1005 {"xor", 2, 2, f_xor},
1006};
1007
1008#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
1009
1010/*
1011 * Function given to ExpandGeneric() to obtain the list of internal
1012 * or user defined function names.
1013 */
1014 char_u *
1015get_function_name(expand_T *xp, int idx)
1016{
1017 static int intidx = -1;
1018 char_u *name;
1019
1020 if (idx == 0)
1021 intidx = -1;
1022 if (intidx < 0)
1023 {
1024 name = get_user_func_name(xp, idx);
1025 if (name != NULL)
1026 return name;
1027 }
1028 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
1029 {
1030 STRCPY(IObuff, functions[intidx].f_name);
1031 STRCAT(IObuff, "(");
1032 if (functions[intidx].f_max_argc == 0)
1033 STRCAT(IObuff, ")");
1034 return IObuff;
1035 }
1036
1037 return NULL;
1038}
1039
1040/*
1041 * Function given to ExpandGeneric() to obtain the list of internal or
1042 * user defined variable or function names.
1043 */
1044 char_u *
1045get_expr_name(expand_T *xp, int idx)
1046{
1047 static int intidx = -1;
1048 char_u *name;
1049
1050 if (idx == 0)
1051 intidx = -1;
1052 if (intidx < 0)
1053 {
1054 name = get_function_name(xp, idx);
1055 if (name != NULL)
1056 return name;
1057 }
1058 return get_user_var_name(xp, ++intidx);
1059}
1060
1061#endif /* FEAT_CMDL_COMPL */
1062
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001063/*
1064 * Find internal function in table above.
1065 * Return index, or -1 if not found
1066 */
1067 int
1068find_internal_func(
1069 char_u *name) /* name of the function */
1070{
1071 int first = 0;
1072 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
1073 int cmp;
1074 int x;
1075
1076 /*
1077 * Find the function name in the table. Binary search.
1078 */
1079 while (first <= last)
1080 {
1081 x = first + ((unsigned)(last - first) >> 1);
1082 cmp = STRCMP(name, functions[x].f_name);
1083 if (cmp < 0)
1084 last = x - 1;
1085 else if (cmp > 0)
1086 first = x + 1;
1087 else
1088 return x;
1089 }
1090 return -1;
1091}
1092
1093 int
1094call_internal_func(
1095 char_u *name,
1096 int argcount,
1097 typval_T *argvars,
1098 typval_T *rettv)
1099{
1100 int i;
1101
1102 i = find_internal_func(name);
1103 if (i < 0)
1104 return ERROR_UNKNOWN;
1105 if (argcount < functions[i].f_min_argc)
1106 return ERROR_TOOFEW;
1107 if (argcount > functions[i].f_max_argc)
1108 return ERROR_TOOMANY;
1109 argvars[argcount].v_type = VAR_UNKNOWN;
1110 functions[i].f_func(argvars, rettv);
1111 return ERROR_NONE;
1112}
1113
1114/*
1115 * Return TRUE for a non-zero Number and a non-empty String.
1116 */
1117 static int
1118non_zero_arg(typval_T *argvars)
1119{
1120 return ((argvars[0].v_type == VAR_NUMBER
1121 && argvars[0].vval.v_number != 0)
1122 || (argvars[0].v_type == VAR_SPECIAL
1123 && argvars[0].vval.v_number == VVAL_TRUE)
1124 || (argvars[0].v_type == VAR_STRING
1125 && argvars[0].vval.v_string != NULL
1126 && *argvars[0].vval.v_string != NUL));
1127}
1128
1129/*
1130 * Get the lnum from the first argument.
1131 * Also accepts ".", "$", etc., but that only works for the current buffer.
1132 * Returns -1 on error.
1133 */
1134 static linenr_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001135tv_get_lnum(typval_T *argvars)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001136{
1137 typval_T rettv;
1138 linenr_T lnum;
1139
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001140 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001141 if (lnum == 0) /* no valid number, try using line() */
1142 {
1143 rettv.v_type = VAR_NUMBER;
1144 f_line(argvars, &rettv);
1145 lnum = (linenr_T)rettv.vval.v_number;
1146 clear_tv(&rettv);
1147 }
1148 return lnum;
1149}
1150
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001151/*
1152 * Get the lnum from the first argument.
1153 * Also accepts "$", then "buf" is used.
1154 * Returns 0 on error.
1155 */
1156 static linenr_T
1157tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
1158{
1159 if (argvars[0].v_type == VAR_STRING
1160 && argvars[0].vval.v_string != NULL
1161 && argvars[0].vval.v_string[0] == '$'
1162 && buf != NULL)
1163 return buf->b_ml.ml_line_count;
1164 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1165}
1166
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001167#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001168/*
1169 * Get the float value of "argvars[0]" into "f".
1170 * Returns FAIL when the argument is not a Number or Float.
1171 */
1172 static int
1173get_float_arg(typval_T *argvars, float_T *f)
1174{
1175 if (argvars[0].v_type == VAR_FLOAT)
1176 {
1177 *f = argvars[0].vval.v_float;
1178 return OK;
1179 }
1180 if (argvars[0].v_type == VAR_NUMBER)
1181 {
1182 *f = (float_T)argvars[0].vval.v_number;
1183 return OK;
1184 }
1185 EMSG(_("E808: Number or Float required"));
1186 return FAIL;
1187}
1188
1189/*
1190 * "abs(expr)" function
1191 */
1192 static void
1193f_abs(typval_T *argvars, typval_T *rettv)
1194{
1195 if (argvars[0].v_type == VAR_FLOAT)
1196 {
1197 rettv->v_type = VAR_FLOAT;
1198 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1199 }
1200 else
1201 {
1202 varnumber_T n;
1203 int error = FALSE;
1204
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001205 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001206 if (error)
1207 rettv->vval.v_number = -1;
1208 else if (n > 0)
1209 rettv->vval.v_number = n;
1210 else
1211 rettv->vval.v_number = -n;
1212 }
1213}
1214
1215/*
1216 * "acos()" function
1217 */
1218 static void
1219f_acos(typval_T *argvars, typval_T *rettv)
1220{
1221 float_T f = 0.0;
1222
1223 rettv->v_type = VAR_FLOAT;
1224 if (get_float_arg(argvars, &f) == OK)
1225 rettv->vval.v_float = acos(f);
1226 else
1227 rettv->vval.v_float = 0.0;
1228}
1229#endif
1230
1231/*
1232 * "add(list, item)" function
1233 */
1234 static void
1235f_add(typval_T *argvars, typval_T *rettv)
1236{
1237 list_T *l;
1238
1239 rettv->vval.v_number = 1; /* Default: Failed */
1240 if (argvars[0].v_type == VAR_LIST)
1241 {
1242 if ((l = argvars[0].vval.v_list) != NULL
1243 && !tv_check_lock(l->lv_lock,
1244 (char_u *)N_("add() argument"), TRUE)
1245 && list_append_tv(l, &argvars[1]) == OK)
1246 copy_tv(&argvars[0], rettv);
1247 }
1248 else
1249 EMSG(_(e_listreq));
1250}
1251
1252/*
1253 * "and(expr, expr)" function
1254 */
1255 static void
1256f_and(typval_T *argvars, typval_T *rettv)
1257{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001258 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1259 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001260}
1261
1262/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02001263 * If there is a window for "curbuf", make it the current window.
1264 */
1265 static void
1266find_win_for_curbuf(void)
1267{
1268 wininfo_T *wip;
1269
1270 for (wip = curbuf->b_wininfo; wip != NULL; wip = wip->wi_next)
1271 {
1272 if (wip->wi_win != NULL)
1273 {
1274 curwin = wip->wi_win;
1275 break;
1276 }
1277 }
1278}
1279
1280/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001281 * Set line or list of lines in buffer "buf".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001282 */
1283 static void
Bram Moolenaarca851592018-06-06 21:04:07 +02001284set_buffer_lines(
1285 buf_T *buf,
1286 linenr_T lnum_arg,
1287 int append,
1288 typval_T *lines,
1289 typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001290{
Bram Moolenaarca851592018-06-06 21:04:07 +02001291 linenr_T lnum = lnum_arg + (append ? 1 : 0);
1292 char_u *line = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001293 list_T *l = NULL;
1294 listitem_T *li = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001295 long added = 0;
Bram Moolenaarca851592018-06-06 21:04:07 +02001296 linenr_T append_lnum;
1297 buf_T *curbuf_save = NULL;
1298 win_T *curwin_save = NULL;
1299 int is_curbuf = buf == curbuf;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001300
Bram Moolenaarca851592018-06-06 21:04:07 +02001301 /* When using the current buffer ml_mfp will be set if needed. Useful when
1302 * setline() is used on startup. For other buffers the buffer must be
1303 * loaded. */
1304 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001305 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001306 rettv->vval.v_number = 1; /* FAIL */
1307 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001308 }
1309
Bram Moolenaarca851592018-06-06 21:04:07 +02001310 if (!is_curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001311 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001312 curbuf_save = curbuf;
1313 curwin_save = curwin;
1314 curbuf = buf;
Bram Moolenaard79a2622018-06-07 18:17:46 +02001315 find_win_for_curbuf();
Bram Moolenaarca851592018-06-06 21:04:07 +02001316 }
1317
1318 if (append)
1319 // appendbufline() uses the line number below which we insert
1320 append_lnum = lnum - 1;
1321 else
1322 // setbufline() uses the line number above which we insert, we only
1323 // append if it's below the last line
1324 append_lnum = curbuf->b_ml.ml_line_count;
1325
1326 if (lines->v_type == VAR_LIST)
1327 {
1328 l = lines->vval.v_list;
1329 li = l->lv_first;
1330 }
1331 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001332 line = tv_get_string_chk(lines);
Bram Moolenaarca851592018-06-06 21:04:07 +02001333
1334 /* default result is zero == OK */
1335 for (;;)
1336 {
1337 if (l != NULL)
1338 {
1339 /* list argument, get next string */
1340 if (li == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001341 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001342 line = tv_get_string_chk(&li->li_tv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001343 li = li->li_next;
1344 }
1345
Bram Moolenaarca851592018-06-06 21:04:07 +02001346 rettv->vval.v_number = 1; /* FAIL */
1347 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
1348 break;
1349
1350 /* When coming here from Insert mode, sync undo, so that this can be
1351 * undone separately from what was previously inserted. */
1352 if (u_sync_once == 2)
1353 {
1354 u_sync_once = 1; /* notify that u_sync() was called */
1355 u_sync(TRUE);
1356 }
1357
1358 if (!append && lnum <= curbuf->b_ml.ml_line_count)
1359 {
1360 /* existing line, replace it */
1361 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
1362 {
1363 changed_bytes(lnum, 0);
1364 if (is_curbuf && lnum == curwin->w_cursor.lnum)
1365 check_cursor_col();
1366 rettv->vval.v_number = 0; /* OK */
1367 }
1368 }
1369 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
1370 {
1371 /* append the line */
1372 ++added;
1373 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
1374 rettv->vval.v_number = 0; /* OK */
1375 }
1376
1377 if (l == NULL) /* only one string argument */
1378 break;
1379 ++lnum;
1380 }
1381
1382 if (added > 0)
1383 {
1384 win_T *wp;
1385 tabpage_T *tp;
1386
1387 appended_lines_mark(append_lnum, added);
1388 FOR_ALL_TAB_WINDOWS(tp, wp)
1389 if (wp->w_buffer == buf && wp->w_cursor.lnum > append_lnum)
1390 wp->w_cursor.lnum += added;
1391 check_cursor_col();
1392
Bram Moolenaarf2732452018-06-03 14:47:35 +02001393#ifdef FEAT_JOB_CHANNEL
1394 if (bt_prompt(curbuf) && (State & INSERT))
1395 // show the line with the prompt
1396 update_topline();
1397#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001398 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001399
1400 if (!is_curbuf)
1401 {
1402 curbuf = curbuf_save;
1403 curwin = curwin_save;
1404 }
1405}
1406
1407/*
1408 * "append(lnum, string/list)" function
1409 */
1410 static void
1411f_append(typval_T *argvars, typval_T *rettv)
1412{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001413 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaarca851592018-06-06 21:04:07 +02001414
1415 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1416}
1417
1418/*
1419 * "appendbufline(buf, lnum, string/list)" function
1420 */
1421 static void
1422f_appendbufline(typval_T *argvars, typval_T *rettv)
1423{
1424 linenr_T lnum;
1425 buf_T *buf;
1426
1427 buf = get_buf_tv(&argvars[0], FALSE);
1428 if (buf == NULL)
1429 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001430 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001431 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001432 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +02001433 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1434 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001435}
1436
1437/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001438 * "argc([window id])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001439 */
1440 static void
Bram Moolenaare6e39892018-10-25 12:32:11 +02001441f_argc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001442{
Bram Moolenaare6e39892018-10-25 12:32:11 +02001443 win_T *wp;
1444
1445 if (argvars[0].v_type == VAR_UNKNOWN)
1446 // use the current window
1447 rettv->vval.v_number = ARGCOUNT;
1448 else if (argvars[0].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001449 && tv_get_number(&argvars[0]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001450 // use the global argument list
1451 rettv->vval.v_number = GARGCOUNT;
1452 else
1453 {
1454 // use the argument list of the specified window
1455 wp = find_win_by_nr_or_id(&argvars[0]);
1456 if (wp != NULL)
1457 rettv->vval.v_number = WARGCOUNT(wp);
1458 else
1459 rettv->vval.v_number = -1;
1460 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001461}
1462
1463/*
1464 * "argidx()" function
1465 */
1466 static void
1467f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1468{
1469 rettv->vval.v_number = curwin->w_arg_idx;
1470}
1471
1472/*
1473 * "arglistid()" function
1474 */
1475 static void
1476f_arglistid(typval_T *argvars, typval_T *rettv)
1477{
1478 win_T *wp;
1479
1480 rettv->vval.v_number = -1;
1481 wp = find_tabwin(&argvars[0], &argvars[1]);
1482 if (wp != NULL)
1483 rettv->vval.v_number = wp->w_alist->id;
1484}
1485
1486/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001487 * Get the argument list for a given window
1488 */
1489 static void
1490get_arglist_as_rettv(aentry_T *arglist, int argcount, typval_T *rettv)
1491{
1492 int idx;
1493
1494 if (rettv_list_alloc(rettv) == OK && arglist != NULL)
1495 for (idx = 0; idx < argcount; ++idx)
1496 list_append_string(rettv->vval.v_list,
1497 alist_name(&arglist[idx]), -1);
1498}
1499
1500/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001501 * "argv(nr)" function
1502 */
1503 static void
1504f_argv(typval_T *argvars, typval_T *rettv)
1505{
1506 int idx;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001507 aentry_T *arglist = NULL;
1508 int argcount = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001509
1510 if (argvars[0].v_type != VAR_UNKNOWN)
1511 {
Bram Moolenaare6e39892018-10-25 12:32:11 +02001512 if (argvars[1].v_type == VAR_UNKNOWN)
1513 {
1514 arglist = ARGLIST;
1515 argcount = ARGCOUNT;
1516 }
1517 else if (argvars[1].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001518 && tv_get_number(&argvars[1]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001519 {
1520 arglist = GARGLIST;
1521 argcount = GARGCOUNT;
1522 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001523 else
Bram Moolenaare6e39892018-10-25 12:32:11 +02001524 {
1525 win_T *wp = find_win_by_nr_or_id(&argvars[1]);
1526
1527 if (wp != NULL)
1528 {
1529 /* Use the argument list of the specified window */
1530 arglist = WARGLIST(wp);
1531 argcount = WARGCOUNT(wp);
1532 }
1533 }
1534
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001535 rettv->v_type = VAR_STRING;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001536 rettv->vval.v_string = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001537 idx = tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02001538 if (arglist != NULL && idx >= 0 && idx < argcount)
1539 rettv->vval.v_string = vim_strsave(alist_name(&arglist[idx]));
1540 else if (idx == -1)
1541 get_arglist_as_rettv(arglist, argcount, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001542 }
Bram Moolenaare6e39892018-10-25 12:32:11 +02001543 else
1544 get_arglist_as_rettv(ARGLIST, ARGCOUNT, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001545}
1546
1547/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001548 * "assert_beeps(cmd [, error])" function
1549 */
1550 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001551f_assert_beeps(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001552{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001553 rettv->vval.v_number = assert_beeps(argvars);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001554}
1555
1556/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001557 * "assert_equal(expected, actual[, msg])" function
1558 */
1559 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001560f_assert_equal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001561{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001562 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_EQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001563}
1564
1565/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01001566 * "assert_equalfile(fname-one, fname-two)" function
1567 */
1568 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001569f_assert_equalfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaard96ff162018-02-18 22:13:29 +01001570{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001571 rettv->vval.v_number = assert_equalfile(argvars);
Bram Moolenaard96ff162018-02-18 22:13:29 +01001572}
1573
1574/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001575 * "assert_notequal(expected, actual[, msg])" function
1576 */
1577 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001578f_assert_notequal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001579{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001580 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001581}
1582
1583/*
1584 * "assert_exception(string[, msg])" function
1585 */
1586 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001587f_assert_exception(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001588{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001589 rettv->vval.v_number = assert_exception(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001590}
1591
1592/*
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02001593 * "assert_fails(cmd [, error[, msg]])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001594 */
1595 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001596f_assert_fails(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001597{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001598 rettv->vval.v_number = assert_fails(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001599}
1600
1601/*
1602 * "assert_false(actual[, msg])" function
1603 */
1604 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001605f_assert_false(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001606{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001607 rettv->vval.v_number = assert_bool(argvars, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001608}
1609
1610/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001611 * "assert_inrange(lower, upper[, msg])" function
1612 */
1613 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001614f_assert_inrange(typval_T *argvars, typval_T *rettv)
Bram Moolenaar61c04492016-07-23 15:35:35 +02001615{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001616 rettv->vval.v_number = assert_inrange(argvars);
Bram Moolenaar61c04492016-07-23 15:35:35 +02001617}
1618
1619/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001620 * "assert_match(pattern, actual[, msg])" function
1621 */
1622 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001623f_assert_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001624{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001625 rettv->vval.v_number = assert_match_common(argvars, ASSERT_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001626}
1627
1628/*
1629 * "assert_notmatch(pattern, actual[, msg])" function
1630 */
1631 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001632f_assert_notmatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001633{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001634 rettv->vval.v_number = assert_match_common(argvars, ASSERT_NOTMATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001635}
1636
1637/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001638 * "assert_report(msg)" function
1639 */
1640 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001641f_assert_report(typval_T *argvars, typval_T *rettv)
Bram Moolenaar42205552017-03-18 19:42:22 +01001642{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001643 rettv->vval.v_number = assert_report(argvars);
Bram Moolenaar42205552017-03-18 19:42:22 +01001644}
1645
1646/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001647 * "assert_true(actual[, msg])" function
1648 */
1649 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001650f_assert_true(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001651{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001652 rettv->vval.v_number = assert_bool(argvars, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001653}
1654
1655#ifdef FEAT_FLOAT
1656/*
1657 * "asin()" function
1658 */
1659 static void
1660f_asin(typval_T *argvars, typval_T *rettv)
1661{
1662 float_T f = 0.0;
1663
1664 rettv->v_type = VAR_FLOAT;
1665 if (get_float_arg(argvars, &f) == OK)
1666 rettv->vval.v_float = asin(f);
1667 else
1668 rettv->vval.v_float = 0.0;
1669}
1670
1671/*
1672 * "atan()" function
1673 */
1674 static void
1675f_atan(typval_T *argvars, typval_T *rettv)
1676{
1677 float_T f = 0.0;
1678
1679 rettv->v_type = VAR_FLOAT;
1680 if (get_float_arg(argvars, &f) == OK)
1681 rettv->vval.v_float = atan(f);
1682 else
1683 rettv->vval.v_float = 0.0;
1684}
1685
1686/*
1687 * "atan2()" function
1688 */
1689 static void
1690f_atan2(typval_T *argvars, typval_T *rettv)
1691{
1692 float_T fx = 0.0, fy = 0.0;
1693
1694 rettv->v_type = VAR_FLOAT;
1695 if (get_float_arg(argvars, &fx) == OK
1696 && get_float_arg(&argvars[1], &fy) == OK)
1697 rettv->vval.v_float = atan2(fx, fy);
1698 else
1699 rettv->vval.v_float = 0.0;
1700}
1701#endif
1702
1703/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001704 * "balloon_show()" function
1705 */
1706#ifdef FEAT_BEVAL
1707 static void
1708f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1709{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001710 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001711 {
1712 if (argvars[0].v_type == VAR_LIST
1713# ifdef FEAT_GUI
1714 && !gui.in_use
1715# endif
1716 )
1717 post_balloon(balloonEval, NULL, argvars[0].vval.v_list);
1718 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001719 post_balloon(balloonEval, tv_get_string_chk(&argvars[0]), NULL);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001720 }
1721}
1722
Bram Moolenaar669a8282017-11-19 20:13:05 +01001723# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001724 static void
1725f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1726{
1727 if (rettv_list_alloc(rettv) == OK)
1728 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001729 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001730
1731 if (msg != NULL)
1732 {
1733 pumitem_T *array;
1734 int size = split_message(msg, &array);
1735 int i;
1736
1737 /* Skip the first and last item, they are always empty. */
1738 for (i = 1; i < size - 1; ++i)
1739 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001740 while (size > 0)
1741 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001742 vim_free(array);
1743 }
1744 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001745}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001746# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001747#endif
1748
1749/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001750 * "browse(save, title, initdir, default)" function
1751 */
1752 static void
1753f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1754{
1755#ifdef FEAT_BROWSE
1756 int save;
1757 char_u *title;
1758 char_u *initdir;
1759 char_u *defname;
1760 char_u buf[NUMBUFLEN];
1761 char_u buf2[NUMBUFLEN];
1762 int error = FALSE;
1763
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001764 save = (int)tv_get_number_chk(&argvars[0], &error);
1765 title = tv_get_string_chk(&argvars[1]);
1766 initdir = tv_get_string_buf_chk(&argvars[2], buf);
1767 defname = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001768
1769 if (error || title == NULL || initdir == NULL || defname == NULL)
1770 rettv->vval.v_string = NULL;
1771 else
1772 rettv->vval.v_string =
1773 do_browse(save ? BROWSE_SAVE : 0,
1774 title, defname, NULL, initdir, NULL, curbuf);
1775#else
1776 rettv->vval.v_string = NULL;
1777#endif
1778 rettv->v_type = VAR_STRING;
1779}
1780
1781/*
1782 * "browsedir(title, initdir)" function
1783 */
1784 static void
1785f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1786{
1787#ifdef FEAT_BROWSE
1788 char_u *title;
1789 char_u *initdir;
1790 char_u buf[NUMBUFLEN];
1791
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001792 title = tv_get_string_chk(&argvars[0]);
1793 initdir = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001794
1795 if (title == NULL || initdir == NULL)
1796 rettv->vval.v_string = NULL;
1797 else
1798 rettv->vval.v_string = do_browse(BROWSE_DIR,
1799 title, NULL, NULL, initdir, NULL, curbuf);
1800#else
1801 rettv->vval.v_string = NULL;
1802#endif
1803 rettv->v_type = VAR_STRING;
1804}
1805
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001806/*
1807 * Find a buffer by number or exact name.
1808 */
1809 static buf_T *
1810find_buffer(typval_T *avar)
1811{
1812 buf_T *buf = NULL;
1813
1814 if (avar->v_type == VAR_NUMBER)
1815 buf = buflist_findnr((int)avar->vval.v_number);
1816 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1817 {
1818 buf = buflist_findname_exp(avar->vval.v_string);
1819 if (buf == NULL)
1820 {
1821 /* No full path name match, try a match with a URL or a "nofile"
1822 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001823 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001824 if (buf->b_fname != NULL
1825 && (path_with_url(buf->b_fname)
1826#ifdef FEAT_QUICKFIX
1827 || bt_nofile(buf)
1828#endif
1829 )
1830 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1831 break;
1832 }
1833 }
1834 return buf;
1835}
1836
1837/*
1838 * "bufexists(expr)" function
1839 */
1840 static void
1841f_bufexists(typval_T *argvars, typval_T *rettv)
1842{
1843 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1844}
1845
1846/*
1847 * "buflisted(expr)" function
1848 */
1849 static void
1850f_buflisted(typval_T *argvars, typval_T *rettv)
1851{
1852 buf_T *buf;
1853
1854 buf = find_buffer(&argvars[0]);
1855 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1856}
1857
1858/*
1859 * "bufloaded(expr)" function
1860 */
1861 static void
1862f_bufloaded(typval_T *argvars, typval_T *rettv)
1863{
1864 buf_T *buf;
1865
1866 buf = find_buffer(&argvars[0]);
1867 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1868}
1869
1870 buf_T *
1871buflist_find_by_name(char_u *name, int curtab_only)
1872{
1873 int save_magic;
1874 char_u *save_cpo;
1875 buf_T *buf;
1876
1877 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1878 save_magic = p_magic;
1879 p_magic = TRUE;
1880 save_cpo = p_cpo;
1881 p_cpo = (char_u *)"";
1882
1883 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1884 TRUE, FALSE, curtab_only));
1885
1886 p_magic = save_magic;
1887 p_cpo = save_cpo;
1888 return buf;
1889}
1890
1891/*
1892 * Get buffer by number or pattern.
1893 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001894 buf_T *
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001895get_buf_tv(typval_T *tv, int curtab_only)
1896{
1897 char_u *name = tv->vval.v_string;
1898 buf_T *buf;
1899
1900 if (tv->v_type == VAR_NUMBER)
1901 return buflist_findnr((int)tv->vval.v_number);
1902 if (tv->v_type != VAR_STRING)
1903 return NULL;
1904 if (name == NULL || *name == NUL)
1905 return curbuf;
1906 if (name[0] == '$' && name[1] == NUL)
1907 return lastbuf;
1908
1909 buf = buflist_find_by_name(name, curtab_only);
1910
1911 /* If not found, try expanding the name, like done for bufexists(). */
1912 if (buf == NULL)
1913 buf = find_buffer(tv);
1914
1915 return buf;
1916}
1917
1918/*
1919 * "bufname(expr)" function
1920 */
1921 static void
1922f_bufname(typval_T *argvars, typval_T *rettv)
1923{
1924 buf_T *buf;
1925
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001926 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001927 ++emsg_off;
1928 buf = get_buf_tv(&argvars[0], FALSE);
1929 rettv->v_type = VAR_STRING;
1930 if (buf != NULL && buf->b_fname != NULL)
1931 rettv->vval.v_string = vim_strsave(buf->b_fname);
1932 else
1933 rettv->vval.v_string = NULL;
1934 --emsg_off;
1935}
1936
1937/*
1938 * "bufnr(expr)" function
1939 */
1940 static void
1941f_bufnr(typval_T *argvars, typval_T *rettv)
1942{
1943 buf_T *buf;
1944 int error = FALSE;
1945 char_u *name;
1946
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001947 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001948 ++emsg_off;
1949 buf = get_buf_tv(&argvars[0], FALSE);
1950 --emsg_off;
1951
1952 /* If the buffer isn't found and the second argument is not zero create a
1953 * new buffer. */
1954 if (buf == NULL
1955 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001956 && tv_get_number_chk(&argvars[1], &error) != 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001957 && !error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001958 && (name = tv_get_string_chk(&argvars[0])) != NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001959 && !error)
1960 buf = buflist_new(name, NULL, (linenr_T)1, 0);
1961
1962 if (buf != NULL)
1963 rettv->vval.v_number = buf->b_fnum;
1964 else
1965 rettv->vval.v_number = -1;
1966}
1967
1968 static void
1969buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
1970{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001971 win_T *wp;
1972 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001973 buf_T *buf;
1974
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001975 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001976 ++emsg_off;
1977 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02001978 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001979 {
1980 ++winnr;
1981 if (wp->w_buffer == buf)
1982 break;
1983 }
1984 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001985 --emsg_off;
1986}
1987
1988/*
1989 * "bufwinid(nr)" function
1990 */
1991 static void
1992f_bufwinid(typval_T *argvars, typval_T *rettv)
1993{
1994 buf_win_common(argvars, rettv, FALSE);
1995}
1996
1997/*
1998 * "bufwinnr(nr)" function
1999 */
2000 static void
2001f_bufwinnr(typval_T *argvars, typval_T *rettv)
2002{
2003 buf_win_common(argvars, rettv, TRUE);
2004}
2005
2006/*
2007 * "byte2line(byte)" function
2008 */
2009 static void
2010f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2011{
2012#ifndef FEAT_BYTEOFF
2013 rettv->vval.v_number = -1;
2014#else
2015 long boff = 0;
2016
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002017 boff = tv_get_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002018 if (boff < 0)
2019 rettv->vval.v_number = -1;
2020 else
2021 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2022 (linenr_T)0, &boff);
2023#endif
2024}
2025
2026 static void
2027byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2028{
2029#ifdef FEAT_MBYTE
2030 char_u *t;
2031#endif
2032 char_u *str;
2033 varnumber_T idx;
2034
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002035 str = tv_get_string_chk(&argvars[0]);
2036 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002037 rettv->vval.v_number = -1;
2038 if (str == NULL || idx < 0)
2039 return;
2040
2041#ifdef FEAT_MBYTE
2042 t = str;
2043 for ( ; idx > 0; idx--)
2044 {
2045 if (*t == NUL) /* EOL reached */
2046 return;
2047 if (enc_utf8 && comp)
2048 t += utf_ptr2len(t);
2049 else
2050 t += (*mb_ptr2len)(t);
2051 }
2052 rettv->vval.v_number = (varnumber_T)(t - str);
2053#else
2054 if ((size_t)idx <= STRLEN(str))
2055 rettv->vval.v_number = idx;
2056#endif
2057}
2058
2059/*
2060 * "byteidx()" function
2061 */
2062 static void
2063f_byteidx(typval_T *argvars, typval_T *rettv)
2064{
2065 byteidx(argvars, rettv, FALSE);
2066}
2067
2068/*
2069 * "byteidxcomp()" function
2070 */
2071 static void
2072f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2073{
2074 byteidx(argvars, rettv, TRUE);
2075}
2076
2077/*
2078 * "call(func, arglist [, dict])" function
2079 */
2080 static void
2081f_call(typval_T *argvars, typval_T *rettv)
2082{
2083 char_u *func;
2084 partial_T *partial = NULL;
2085 dict_T *selfdict = NULL;
2086
2087 if (argvars[1].v_type != VAR_LIST)
2088 {
2089 EMSG(_(e_listreq));
2090 return;
2091 }
2092 if (argvars[1].vval.v_list == NULL)
2093 return;
2094
2095 if (argvars[0].v_type == VAR_FUNC)
2096 func = argvars[0].vval.v_string;
2097 else if (argvars[0].v_type == VAR_PARTIAL)
2098 {
2099 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002100 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002101 }
2102 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002103 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002104 if (*func == NUL)
2105 return; /* type error or empty name */
2106
2107 if (argvars[2].v_type != VAR_UNKNOWN)
2108 {
2109 if (argvars[2].v_type != VAR_DICT)
2110 {
2111 EMSG(_(e_dictreq));
2112 return;
2113 }
2114 selfdict = argvars[2].vval.v_dict;
2115 }
2116
2117 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2118}
2119
2120#ifdef FEAT_FLOAT
2121/*
2122 * "ceil({float})" function
2123 */
2124 static void
2125f_ceil(typval_T *argvars, typval_T *rettv)
2126{
2127 float_T f = 0.0;
2128
2129 rettv->v_type = VAR_FLOAT;
2130 if (get_float_arg(argvars, &f) == OK)
2131 rettv->vval.v_float = ceil(f);
2132 else
2133 rettv->vval.v_float = 0.0;
2134}
2135#endif
2136
2137#ifdef FEAT_JOB_CHANNEL
2138/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002139 * "ch_canread()" function
2140 */
2141 static void
2142f_ch_canread(typval_T *argvars, typval_T *rettv)
2143{
Bram Moolenaar958dc692016-12-01 15:34:12 +01002144 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002145
2146 rettv->vval.v_number = 0;
2147 if (channel != NULL)
2148 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
2149 || channel_has_readahead(channel, PART_OUT)
2150 || channel_has_readahead(channel, PART_ERR);
2151}
2152
2153/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002154 * "ch_close()" function
2155 */
2156 static void
2157f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
2158{
2159 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2160
2161 if (channel != NULL)
2162 {
2163 channel_close(channel, FALSE);
2164 channel_clear(channel);
2165 }
2166}
2167
2168/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002169 * "ch_close()" function
2170 */
2171 static void
2172f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
2173{
2174 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2175
2176 if (channel != NULL)
2177 channel_close_in(channel);
2178}
2179
2180/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002181 * "ch_getbufnr()" function
2182 */
2183 static void
2184f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
2185{
2186 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2187
2188 rettv->vval.v_number = -1;
2189 if (channel != NULL)
2190 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002191 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002192 int part;
2193
2194 if (STRCMP(what, "err") == 0)
2195 part = PART_ERR;
2196 else if (STRCMP(what, "out") == 0)
2197 part = PART_OUT;
2198 else if (STRCMP(what, "in") == 0)
2199 part = PART_IN;
2200 else
2201 part = PART_SOCK;
2202 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
2203 rettv->vval.v_number =
2204 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
2205 }
2206}
2207
2208/*
2209 * "ch_getjob()" function
2210 */
2211 static void
2212f_ch_getjob(typval_T *argvars, typval_T *rettv)
2213{
2214 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2215
2216 if (channel != NULL)
2217 {
2218 rettv->v_type = VAR_JOB;
2219 rettv->vval.v_job = channel->ch_job;
2220 if (channel->ch_job != NULL)
2221 ++channel->ch_job->jv_refcount;
2222 }
2223}
2224
2225/*
2226 * "ch_info()" function
2227 */
2228 static void
2229f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
2230{
2231 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2232
2233 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
2234 channel_info(channel, rettv->vval.v_dict);
2235}
2236
2237/*
2238 * "ch_log()" function
2239 */
2240 static void
2241f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
2242{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002243 char_u *msg = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002244 channel_T *channel = NULL;
2245
2246 if (argvars[1].v_type != VAR_UNKNOWN)
2247 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
2248
Bram Moolenaard5359b22018-04-05 22:44:39 +02002249 ch_log(channel, "%s", msg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002250}
2251
2252/*
2253 * "ch_logfile()" function
2254 */
2255 static void
2256f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
2257{
2258 char_u *fname;
2259 char_u *opt = (char_u *)"";
2260 char_u buf[NUMBUFLEN];
2261
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02002262 /* Don't open a file in restricted mode. */
2263 if (check_restricted() || check_secure())
2264 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002265 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002266 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002267 opt = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002268 ch_logfile(fname, opt);
2269}
2270
2271/*
2272 * "ch_open()" function
2273 */
2274 static void
2275f_ch_open(typval_T *argvars, typval_T *rettv)
2276{
2277 rettv->v_type = VAR_CHANNEL;
2278 if (check_restricted() || check_secure())
2279 return;
2280 rettv->vval.v_channel = channel_open_func(argvars);
2281}
2282
2283/*
2284 * "ch_read()" function
2285 */
2286 static void
2287f_ch_read(typval_T *argvars, typval_T *rettv)
2288{
2289 common_channel_read(argvars, rettv, FALSE);
2290}
2291
2292/*
2293 * "ch_readraw()" function
2294 */
2295 static void
2296f_ch_readraw(typval_T *argvars, typval_T *rettv)
2297{
2298 common_channel_read(argvars, rettv, TRUE);
2299}
2300
2301/*
2302 * "ch_evalexpr()" function
2303 */
2304 static void
2305f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2306{
2307 ch_expr_common(argvars, rettv, TRUE);
2308}
2309
2310/*
2311 * "ch_sendexpr()" function
2312 */
2313 static void
2314f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2315{
2316 ch_expr_common(argvars, rettv, FALSE);
2317}
2318
2319/*
2320 * "ch_evalraw()" function
2321 */
2322 static void
2323f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2324{
2325 ch_raw_common(argvars, rettv, TRUE);
2326}
2327
2328/*
2329 * "ch_sendraw()" function
2330 */
2331 static void
2332f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2333{
2334 ch_raw_common(argvars, rettv, FALSE);
2335}
2336
2337/*
2338 * "ch_setoptions()" function
2339 */
2340 static void
2341f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2342{
2343 channel_T *channel;
2344 jobopt_T opt;
2345
2346 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2347 if (channel == NULL)
2348 return;
2349 clear_job_options(&opt);
2350 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002351 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002352 channel_set_options(channel, &opt);
2353 free_job_options(&opt);
2354}
2355
2356/*
2357 * "ch_status()" function
2358 */
2359 static void
2360f_ch_status(typval_T *argvars, typval_T *rettv)
2361{
2362 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002363 jobopt_T opt;
2364 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002365
2366 /* return an empty string by default */
2367 rettv->v_type = VAR_STRING;
2368 rettv->vval.v_string = NULL;
2369
2370 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002371
2372 if (argvars[1].v_type != VAR_UNKNOWN)
2373 {
2374 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002375 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002376 && (opt.jo_set & JO_PART))
2377 part = opt.jo_part;
2378 }
2379
2380 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002381}
2382#endif
2383
2384/*
2385 * "changenr()" function
2386 */
2387 static void
2388f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2389{
2390 rettv->vval.v_number = curbuf->b_u_seq_cur;
2391}
2392
2393/*
2394 * "char2nr(string)" function
2395 */
2396 static void
2397f_char2nr(typval_T *argvars, typval_T *rettv)
2398{
2399#ifdef FEAT_MBYTE
2400 if (has_mbyte)
2401 {
2402 int utf8 = 0;
2403
2404 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002405 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002406
2407 if (utf8)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002408 rettv->vval.v_number = (*utf_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002409 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002410 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002411 }
2412 else
2413#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002414 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002415}
2416
2417/*
2418 * "cindent(lnum)" function
2419 */
2420 static void
2421f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2422{
2423#ifdef FEAT_CINDENT
2424 pos_T pos;
2425 linenr_T lnum;
2426
2427 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002428 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002429 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2430 {
2431 curwin->w_cursor.lnum = lnum;
2432 rettv->vval.v_number = get_c_indent();
2433 curwin->w_cursor = pos;
2434 }
2435 else
2436#endif
2437 rettv->vval.v_number = -1;
2438}
2439
2440/*
2441 * "clearmatches()" function
2442 */
2443 static void
2444f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2445{
2446#ifdef FEAT_SEARCH_EXTRA
2447 clear_matches(curwin);
2448#endif
2449}
2450
2451/*
2452 * "col(string)" function
2453 */
2454 static void
2455f_col(typval_T *argvars, typval_T *rettv)
2456{
2457 colnr_T col = 0;
2458 pos_T *fp;
2459 int fnum = curbuf->b_fnum;
2460
2461 fp = var2fpos(&argvars[0], FALSE, &fnum);
2462 if (fp != NULL && fnum == curbuf->b_fnum)
2463 {
2464 if (fp->col == MAXCOL)
2465 {
2466 /* '> can be MAXCOL, get the length of the line then */
2467 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2468 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2469 else
2470 col = MAXCOL;
2471 }
2472 else
2473 {
2474 col = fp->col + 1;
2475#ifdef FEAT_VIRTUALEDIT
2476 /* col(".") when the cursor is on the NUL at the end of the line
2477 * because of "coladd" can be seen as an extra column. */
2478 if (virtual_active() && fp == &curwin->w_cursor)
2479 {
2480 char_u *p = ml_get_cursor();
2481
2482 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2483 curwin->w_virtcol - curwin->w_cursor.coladd))
2484 {
2485# ifdef FEAT_MBYTE
2486 int l;
2487
2488 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2489 col += l;
2490# else
2491 if (*p != NUL && p[1] == NUL)
2492 ++col;
2493# endif
2494 }
2495 }
2496#endif
2497 }
2498 }
2499 rettv->vval.v_number = col;
2500}
2501
2502#if defined(FEAT_INS_EXPAND)
2503/*
2504 * "complete()" function
2505 */
2506 static void
2507f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2508{
2509 int startcol;
2510
2511 if ((State & INSERT) == 0)
2512 {
2513 EMSG(_("E785: complete() can only be used in Insert mode"));
2514 return;
2515 }
2516
2517 /* Check for undo allowed here, because if something was already inserted
2518 * the line was already saved for undo and this check isn't done. */
2519 if (!undo_allowed())
2520 return;
2521
2522 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2523 {
2524 EMSG(_(e_invarg));
2525 return;
2526 }
2527
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002528 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002529 if (startcol <= 0)
2530 return;
2531
2532 set_completion(startcol - 1, argvars[1].vval.v_list);
2533}
2534
2535/*
2536 * "complete_add()" function
2537 */
2538 static void
2539f_complete_add(typval_T *argvars, typval_T *rettv)
2540{
2541 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2542}
2543
2544/*
2545 * "complete_check()" function
2546 */
2547 static void
2548f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2549{
2550 int saved = RedrawingDisabled;
2551
2552 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002553 ins_compl_check_keys(0, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002554 rettv->vval.v_number = compl_interrupted;
2555 RedrawingDisabled = saved;
2556}
2557#endif
2558
2559/*
2560 * "confirm(message, buttons[, default [, type]])" function
2561 */
2562 static void
2563f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2564{
2565#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2566 char_u *message;
2567 char_u *buttons = NULL;
2568 char_u buf[NUMBUFLEN];
2569 char_u buf2[NUMBUFLEN];
2570 int def = 1;
2571 int type = VIM_GENERIC;
2572 char_u *typestr;
2573 int error = FALSE;
2574
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002575 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002576 if (message == NULL)
2577 error = TRUE;
2578 if (argvars[1].v_type != VAR_UNKNOWN)
2579 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002580 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002581 if (buttons == NULL)
2582 error = TRUE;
2583 if (argvars[2].v_type != VAR_UNKNOWN)
2584 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002585 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002586 if (argvars[3].v_type != VAR_UNKNOWN)
2587 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002588 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002589 if (typestr == NULL)
2590 error = TRUE;
2591 else
2592 {
2593 switch (TOUPPER_ASC(*typestr))
2594 {
2595 case 'E': type = VIM_ERROR; break;
2596 case 'Q': type = VIM_QUESTION; break;
2597 case 'I': type = VIM_INFO; break;
2598 case 'W': type = VIM_WARNING; break;
2599 case 'G': type = VIM_GENERIC; break;
2600 }
2601 }
2602 }
2603 }
2604 }
2605
2606 if (buttons == NULL || *buttons == NUL)
2607 buttons = (char_u *)_("&Ok");
2608
2609 if (!error)
2610 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2611 def, NULL, FALSE);
2612#endif
2613}
2614
2615/*
2616 * "copy()" function
2617 */
2618 static void
2619f_copy(typval_T *argvars, typval_T *rettv)
2620{
2621 item_copy(&argvars[0], rettv, FALSE, 0);
2622}
2623
2624#ifdef FEAT_FLOAT
2625/*
2626 * "cos()" function
2627 */
2628 static void
2629f_cos(typval_T *argvars, typval_T *rettv)
2630{
2631 float_T f = 0.0;
2632
2633 rettv->v_type = VAR_FLOAT;
2634 if (get_float_arg(argvars, &f) == OK)
2635 rettv->vval.v_float = cos(f);
2636 else
2637 rettv->vval.v_float = 0.0;
2638}
2639
2640/*
2641 * "cosh()" function
2642 */
2643 static void
2644f_cosh(typval_T *argvars, typval_T *rettv)
2645{
2646 float_T f = 0.0;
2647
2648 rettv->v_type = VAR_FLOAT;
2649 if (get_float_arg(argvars, &f) == OK)
2650 rettv->vval.v_float = cosh(f);
2651 else
2652 rettv->vval.v_float = 0.0;
2653}
2654#endif
2655
2656/*
2657 * "count()" function
2658 */
2659 static void
2660f_count(typval_T *argvars, typval_T *rettv)
2661{
2662 long n = 0;
2663 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002664 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002665
Bram Moolenaar9966b212017-07-28 16:46:57 +02002666 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002667 ic = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002668
2669 if (argvars[0].v_type == VAR_STRING)
2670 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002671 char_u *expr = tv_get_string_chk(&argvars[1]);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002672 char_u *p = argvars[0].vval.v_string;
2673 char_u *next;
2674
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002675 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002676 {
2677 if (ic)
2678 {
2679 size_t len = STRLEN(expr);
2680
2681 while (*p != NUL)
2682 {
2683 if (MB_STRNICMP(p, expr, len) == 0)
2684 {
2685 ++n;
2686 p += len;
2687 }
2688 else
2689 MB_PTR_ADV(p);
2690 }
2691 }
2692 else
2693 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2694 != NULL)
2695 {
2696 ++n;
2697 p = next + STRLEN(expr);
2698 }
2699 }
2700
2701 }
2702 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002703 {
2704 listitem_T *li;
2705 list_T *l;
2706 long idx;
2707
2708 if ((l = argvars[0].vval.v_list) != NULL)
2709 {
2710 li = l->lv_first;
2711 if (argvars[2].v_type != VAR_UNKNOWN)
2712 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002713 if (argvars[3].v_type != VAR_UNKNOWN)
2714 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002715 idx = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002716 if (!error)
2717 {
2718 li = list_find(l, idx);
2719 if (li == NULL)
2720 EMSGN(_(e_listidx), idx);
2721 }
2722 }
2723 if (error)
2724 li = NULL;
2725 }
2726
2727 for ( ; li != NULL; li = li->li_next)
2728 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2729 ++n;
2730 }
2731 }
2732 else if (argvars[0].v_type == VAR_DICT)
2733 {
2734 int todo;
2735 dict_T *d;
2736 hashitem_T *hi;
2737
2738 if ((d = argvars[0].vval.v_dict) != NULL)
2739 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002740 if (argvars[2].v_type != VAR_UNKNOWN)
2741 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002742 if (argvars[3].v_type != VAR_UNKNOWN)
2743 EMSG(_(e_invarg));
2744 }
2745
2746 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2747 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2748 {
2749 if (!HASHITEM_EMPTY(hi))
2750 {
2751 --todo;
2752 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2753 ++n;
2754 }
2755 }
2756 }
2757 }
2758 else
2759 EMSG2(_(e_listdictarg), "count()");
2760 rettv->vval.v_number = n;
2761}
2762
2763/*
2764 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2765 *
2766 * Checks the existence of a cscope connection.
2767 */
2768 static void
2769f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2770{
2771#ifdef FEAT_CSCOPE
2772 int num = 0;
2773 char_u *dbpath = NULL;
2774 char_u *prepend = NULL;
2775 char_u buf[NUMBUFLEN];
2776
2777 if (argvars[0].v_type != VAR_UNKNOWN
2778 && argvars[1].v_type != VAR_UNKNOWN)
2779 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002780 num = (int)tv_get_number(&argvars[0]);
2781 dbpath = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002782 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002783 prepend = tv_get_string_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002784 }
2785
2786 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2787#endif
2788}
2789
2790/*
2791 * "cursor(lnum, col)" function, or
2792 * "cursor(list)"
2793 *
2794 * Moves the cursor to the specified line and column.
2795 * Returns 0 when the position could be set, -1 otherwise.
2796 */
2797 static void
2798f_cursor(typval_T *argvars, typval_T *rettv)
2799{
2800 long line, col;
2801#ifdef FEAT_VIRTUALEDIT
2802 long coladd = 0;
2803#endif
2804 int set_curswant = TRUE;
2805
2806 rettv->vval.v_number = -1;
2807 if (argvars[1].v_type == VAR_UNKNOWN)
2808 {
2809 pos_T pos;
2810 colnr_T curswant = -1;
2811
2812 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2813 {
2814 EMSG(_(e_invarg));
2815 return;
2816 }
2817 line = pos.lnum;
2818 col = pos.col;
2819#ifdef FEAT_VIRTUALEDIT
2820 coladd = pos.coladd;
2821#endif
2822 if (curswant >= 0)
2823 {
2824 curwin->w_curswant = curswant - 1;
2825 set_curswant = FALSE;
2826 }
2827 }
2828 else
2829 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002830 line = tv_get_lnum(argvars);
2831 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002832#ifdef FEAT_VIRTUALEDIT
2833 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002834 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002835#endif
2836 }
2837 if (line < 0 || col < 0
2838#ifdef FEAT_VIRTUALEDIT
2839 || coladd < 0
2840#endif
2841 )
2842 return; /* type error; errmsg already given */
2843 if (line > 0)
2844 curwin->w_cursor.lnum = line;
2845 if (col > 0)
2846 curwin->w_cursor.col = col - 1;
2847#ifdef FEAT_VIRTUALEDIT
2848 curwin->w_cursor.coladd = coladd;
2849#endif
2850
2851 /* Make sure the cursor is in a valid position. */
2852 check_cursor();
2853#ifdef FEAT_MBYTE
2854 /* Correct cursor for multi-byte character. */
2855 if (has_mbyte)
2856 mb_adjust_cursor();
2857#endif
2858
2859 curwin->w_set_curswant = set_curswant;
2860 rettv->vval.v_number = 0;
2861}
2862
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002863#ifdef WIN3264
2864/*
2865 * "debugbreak()" function
2866 */
2867 static void
2868f_debugbreak(typval_T *argvars, typval_T *rettv)
2869{
2870 int pid;
2871
2872 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002873 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002874 if (pid == 0)
2875 EMSG(_(e_invarg));
2876 else
2877 {
2878 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2879
2880 if (hProcess != NULL)
2881 {
2882 DebugBreakProcess(hProcess);
2883 CloseHandle(hProcess);
2884 rettv->vval.v_number = OK;
2885 }
2886 }
2887}
2888#endif
2889
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002890/*
2891 * "deepcopy()" function
2892 */
2893 static void
2894f_deepcopy(typval_T *argvars, typval_T *rettv)
2895{
2896 int noref = 0;
2897 int copyID;
2898
2899 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002900 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002901 if (noref < 0 || noref > 1)
2902 EMSG(_(e_invarg));
2903 else
2904 {
2905 copyID = get_copyID();
2906 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2907 }
2908}
2909
2910/*
2911 * "delete()" function
2912 */
2913 static void
2914f_delete(typval_T *argvars, typval_T *rettv)
2915{
2916 char_u nbuf[NUMBUFLEN];
2917 char_u *name;
2918 char_u *flags;
2919
2920 rettv->vval.v_number = -1;
2921 if (check_restricted() || check_secure())
2922 return;
2923
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002924 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002925 if (name == NULL || *name == NUL)
2926 {
2927 EMSG(_(e_invarg));
2928 return;
2929 }
2930
2931 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002932 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002933 else
2934 flags = (char_u *)"";
2935
2936 if (*flags == NUL)
2937 /* delete a file */
2938 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2939 else if (STRCMP(flags, "d") == 0)
2940 /* delete an empty directory */
2941 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2942 else if (STRCMP(flags, "rf") == 0)
2943 /* delete a directory recursively */
2944 rettv->vval.v_number = delete_recursive(name);
2945 else
2946 EMSG2(_(e_invexpr2), flags);
2947}
2948
2949/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02002950 * "deletebufline()" function
2951 */
2952 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02002953f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02002954{
2955 buf_T *buf;
2956 linenr_T first, last;
2957 linenr_T lnum;
2958 long count;
2959 int is_curbuf;
2960 buf_T *curbuf_save = NULL;
2961 win_T *curwin_save = NULL;
2962 tabpage_T *tp;
2963 win_T *wp;
2964
2965 buf = get_buf_tv(&argvars[0], FALSE);
2966 if (buf == NULL)
2967 {
2968 rettv->vval.v_number = 1; /* FAIL */
2969 return;
2970 }
2971 is_curbuf = buf == curbuf;
2972
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002973 first = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002974 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002975 last = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002976 else
2977 last = first;
2978
2979 if (buf->b_ml.ml_mfp == NULL || first < 1
2980 || first > buf->b_ml.ml_line_count || last < first)
2981 {
2982 rettv->vval.v_number = 1; /* FAIL */
2983 return;
2984 }
2985
2986 if (!is_curbuf)
2987 {
2988 curbuf_save = curbuf;
2989 curwin_save = curwin;
2990 curbuf = buf;
2991 find_win_for_curbuf();
2992 }
2993 if (last > curbuf->b_ml.ml_line_count)
2994 last = curbuf->b_ml.ml_line_count;
2995 count = last - first + 1;
2996
2997 // When coming here from Insert mode, sync undo, so that this can be
2998 // undone separately from what was previously inserted.
2999 if (u_sync_once == 2)
3000 {
3001 u_sync_once = 1; // notify that u_sync() was called
3002 u_sync(TRUE);
3003 }
3004
3005 if (u_save(first - 1, last + 1) == FAIL)
3006 {
3007 rettv->vval.v_number = 1; /* FAIL */
3008 return;
3009 }
3010
3011 for (lnum = first; lnum <= last; ++lnum)
3012 ml_delete(first, TRUE);
3013
3014 FOR_ALL_TAB_WINDOWS(tp, wp)
3015 if (wp->w_buffer == buf)
3016 {
3017 if (wp->w_cursor.lnum > last)
3018 wp->w_cursor.lnum -= count;
3019 else if (wp->w_cursor.lnum> first)
3020 wp->w_cursor.lnum = first;
3021 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
3022 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
3023 }
3024 check_cursor_col();
3025 deleted_lines_mark(first, count);
3026
3027 if (!is_curbuf)
3028 {
3029 curbuf = curbuf_save;
3030 curwin = curwin_save;
3031 }
3032}
3033
3034/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003035 * "did_filetype()" function
3036 */
3037 static void
3038f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3039{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003040 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003041}
3042
3043/*
3044 * "diff_filler()" function
3045 */
3046 static void
3047f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3048{
3049#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003050 rettv->vval.v_number = diff_check_fill(curwin, tv_get_lnum(argvars));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003051#endif
3052}
3053
3054/*
3055 * "diff_hlID()" function
3056 */
3057 static void
3058f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3059{
3060#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003061 linenr_T lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003062 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01003063 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003064 static int fnum = 0;
3065 static int change_start = 0;
3066 static int change_end = 0;
3067 static hlf_T hlID = (hlf_T)0;
3068 int filler_lines;
3069 int col;
3070
3071 if (lnum < 0) /* ignore type error in {lnum} arg */
3072 lnum = 0;
3073 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003074 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003075 || fnum != curbuf->b_fnum)
3076 {
3077 /* New line, buffer, change: need to get the values. */
3078 filler_lines = diff_check(curwin, lnum);
3079 if (filler_lines < 0)
3080 {
3081 if (filler_lines == -1)
3082 {
3083 change_start = MAXCOL;
3084 change_end = -1;
3085 if (diff_find_change(curwin, lnum, &change_start, &change_end))
3086 hlID = HLF_ADD; /* added line */
3087 else
3088 hlID = HLF_CHD; /* changed line */
3089 }
3090 else
3091 hlID = HLF_ADD; /* added line */
3092 }
3093 else
3094 hlID = (hlf_T)0;
3095 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003096 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003097 fnum = curbuf->b_fnum;
3098 }
3099
3100 if (hlID == HLF_CHD || hlID == HLF_TXD)
3101 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003102 col = tv_get_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003103 if (col >= change_start && col <= change_end)
3104 hlID = HLF_TXD; /* changed text */
3105 else
3106 hlID = HLF_CHD; /* changed line */
3107 }
3108 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
3109#endif
3110}
3111
3112/*
3113 * "empty({expr})" function
3114 */
3115 static void
3116f_empty(typval_T *argvars, typval_T *rettv)
3117{
3118 int n = FALSE;
3119
3120 switch (argvars[0].v_type)
3121 {
3122 case VAR_STRING:
3123 case VAR_FUNC:
3124 n = argvars[0].vval.v_string == NULL
3125 || *argvars[0].vval.v_string == NUL;
3126 break;
3127 case VAR_PARTIAL:
3128 n = FALSE;
3129 break;
3130 case VAR_NUMBER:
3131 n = argvars[0].vval.v_number == 0;
3132 break;
3133 case VAR_FLOAT:
3134#ifdef FEAT_FLOAT
3135 n = argvars[0].vval.v_float == 0.0;
3136 break;
3137#endif
3138 case VAR_LIST:
3139 n = argvars[0].vval.v_list == NULL
3140 || argvars[0].vval.v_list->lv_first == NULL;
3141 break;
3142 case VAR_DICT:
3143 n = argvars[0].vval.v_dict == NULL
3144 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
3145 break;
3146 case VAR_SPECIAL:
3147 n = argvars[0].vval.v_number != VVAL_TRUE;
3148 break;
3149
3150 case VAR_JOB:
3151#ifdef FEAT_JOB_CHANNEL
3152 n = argvars[0].vval.v_job == NULL
3153 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
3154 break;
3155#endif
3156 case VAR_CHANNEL:
3157#ifdef FEAT_JOB_CHANNEL
3158 n = argvars[0].vval.v_channel == NULL
3159 || !channel_is_open(argvars[0].vval.v_channel);
3160 break;
3161#endif
3162 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01003163 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003164 n = TRUE;
3165 break;
3166 }
3167
3168 rettv->vval.v_number = n;
3169}
3170
3171/*
3172 * "escape({string}, {chars})" function
3173 */
3174 static void
3175f_escape(typval_T *argvars, typval_T *rettv)
3176{
3177 char_u buf[NUMBUFLEN];
3178
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003179 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
3180 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003181 rettv->v_type = VAR_STRING;
3182}
3183
3184/*
3185 * "eval()" function
3186 */
3187 static void
3188f_eval(typval_T *argvars, typval_T *rettv)
3189{
3190 char_u *s, *p;
3191
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003192 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003193 if (s != NULL)
3194 s = skipwhite(s);
3195
3196 p = s;
3197 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3198 {
3199 if (p != NULL && !aborting())
3200 EMSG2(_(e_invexpr2), p);
3201 need_clr_eos = FALSE;
3202 rettv->v_type = VAR_NUMBER;
3203 rettv->vval.v_number = 0;
3204 }
3205 else if (*s != NUL)
3206 EMSG(_(e_trailing));
3207}
3208
3209/*
3210 * "eventhandler()" function
3211 */
3212 static void
3213f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3214{
3215 rettv->vval.v_number = vgetc_busy;
3216}
3217
3218/*
3219 * "executable()" function
3220 */
3221 static void
3222f_executable(typval_T *argvars, typval_T *rettv)
3223{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003224 char_u *name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003225
3226 /* Check in $PATH and also check directly if there is a directory name. */
3227 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
3228 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
3229}
3230
3231static garray_T redir_execute_ga;
3232
3233/*
3234 * Append "value[value_len]" to the execute() output.
3235 */
3236 void
3237execute_redir_str(char_u *value, int value_len)
3238{
3239 int len;
3240
3241 if (value_len == -1)
3242 len = (int)STRLEN(value); /* Append the entire string */
3243 else
3244 len = value_len; /* Append only "value_len" characters */
3245 if (ga_grow(&redir_execute_ga, len) == OK)
3246 {
3247 mch_memmove((char *)redir_execute_ga.ga_data
3248 + redir_execute_ga.ga_len, value, len);
3249 redir_execute_ga.ga_len += len;
3250 }
3251}
3252
3253/*
3254 * Get next line from a list.
3255 * Called by do_cmdline() to get the next line.
3256 * Returns allocated string, or NULL for end of function.
3257 */
3258
3259 static char_u *
3260get_list_line(
3261 int c UNUSED,
3262 void *cookie,
3263 int indent UNUSED)
3264{
3265 listitem_T **p = (listitem_T **)cookie;
3266 listitem_T *item = *p;
3267 char_u buf[NUMBUFLEN];
3268 char_u *s;
3269
3270 if (item == NULL)
3271 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003272 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003273 *p = item->li_next;
3274 return s == NULL ? NULL : vim_strsave(s);
3275}
3276
3277/*
3278 * "execute()" function
3279 */
3280 static void
3281f_execute(typval_T *argvars, typval_T *rettv)
3282{
3283 char_u *cmd = NULL;
3284 list_T *list = NULL;
3285 int save_msg_silent = msg_silent;
3286 int save_emsg_silent = emsg_silent;
3287 int save_emsg_noredir = emsg_noredir;
3288 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003289 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003290 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003291 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003292 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003293
3294 rettv->vval.v_string = NULL;
3295 rettv->v_type = VAR_STRING;
3296
3297 if (argvars[0].v_type == VAR_LIST)
3298 {
3299 list = argvars[0].vval.v_list;
3300 if (list == NULL || list->lv_first == NULL)
3301 /* empty list, no commands, empty output */
3302 return;
3303 ++list->lv_refcount;
3304 }
3305 else
3306 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003307 cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003308 if (cmd == NULL)
3309 return;
3310 }
3311
3312 if (argvars[1].v_type != VAR_UNKNOWN)
3313 {
3314 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003315 char_u *s = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003316
3317 if (s == NULL)
3318 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003319 if (*s == NUL)
3320 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003321 if (STRNCMP(s, "silent", 6) == 0)
3322 ++msg_silent;
3323 if (STRCMP(s, "silent!") == 0)
3324 {
3325 emsg_silent = TRUE;
3326 emsg_noredir = TRUE;
3327 }
3328 }
3329 else
3330 ++msg_silent;
3331
3332 if (redir_execute)
3333 save_ga = redir_execute_ga;
3334 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3335 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003336 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003337 if (!echo_output)
3338 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003339
3340 if (cmd != NULL)
3341 do_cmdline_cmd(cmd);
3342 else
3343 {
3344 listitem_T *item = list->lv_first;
3345
3346 do_cmdline(NULL, get_list_line, (void *)&item,
3347 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3348 --list->lv_refcount;
3349 }
3350
Bram Moolenaard297f352017-01-29 20:31:21 +01003351 /* Need to append a NUL to the result. */
3352 if (ga_grow(&redir_execute_ga, 1) == OK)
3353 {
3354 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3355 rettv->vval.v_string = redir_execute_ga.ga_data;
3356 }
3357 else
3358 {
3359 ga_clear(&redir_execute_ga);
3360 rettv->vval.v_string = NULL;
3361 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003362 msg_silent = save_msg_silent;
3363 emsg_silent = save_emsg_silent;
3364 emsg_noredir = save_emsg_noredir;
3365
3366 redir_execute = save_redir_execute;
3367 if (redir_execute)
3368 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003369 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003370
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003371 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003372 if (echo_output)
3373 // When not working silently: put it in column zero. A following
3374 // "echon" will overwrite the message, unavoidably.
3375 msg_col = 0;
3376 else
3377 // When working silently: Put it back where it was, since nothing
3378 // should have been written.
3379 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003380}
3381
3382/*
3383 * "exepath()" function
3384 */
3385 static void
3386f_exepath(typval_T *argvars, typval_T *rettv)
3387{
3388 char_u *p = NULL;
3389
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003390 (void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003391 rettv->v_type = VAR_STRING;
3392 rettv->vval.v_string = p;
3393}
3394
3395/*
3396 * "exists()" function
3397 */
3398 static void
3399f_exists(typval_T *argvars, typval_T *rettv)
3400{
3401 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003402 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003403
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003404 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003405 if (*p == '$') /* environment variable */
3406 {
3407 /* first try "normal" environment variables (fast) */
3408 if (mch_getenv(p + 1) != NULL)
3409 n = TRUE;
3410 else
3411 {
3412 /* try expanding things like $VIM and ${HOME} */
3413 p = expand_env_save(p);
3414 if (p != NULL && *p != '$')
3415 n = TRUE;
3416 vim_free(p);
3417 }
3418 }
3419 else if (*p == '&' || *p == '+') /* option */
3420 {
3421 n = (get_option_tv(&p, NULL, TRUE) == OK);
3422 if (*skipwhite(p) != NUL)
3423 n = FALSE; /* trailing garbage */
3424 }
3425 else if (*p == '*') /* internal or user defined function */
3426 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003427 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003428 }
3429 else if (*p == ':')
3430 {
3431 n = cmd_exists(p + 1);
3432 }
3433 else if (*p == '#')
3434 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003435 if (p[1] == '#')
3436 n = autocmd_supported(p + 2);
3437 else
3438 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003439 }
3440 else /* internal variable */
3441 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003442 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003443 }
3444
3445 rettv->vval.v_number = n;
3446}
3447
3448#ifdef FEAT_FLOAT
3449/*
3450 * "exp()" function
3451 */
3452 static void
3453f_exp(typval_T *argvars, typval_T *rettv)
3454{
3455 float_T f = 0.0;
3456
3457 rettv->v_type = VAR_FLOAT;
3458 if (get_float_arg(argvars, &f) == OK)
3459 rettv->vval.v_float = exp(f);
3460 else
3461 rettv->vval.v_float = 0.0;
3462}
3463#endif
3464
3465/*
3466 * "expand()" function
3467 */
3468 static void
3469f_expand(typval_T *argvars, typval_T *rettv)
3470{
3471 char_u *s;
3472 int len;
3473 char_u *errormsg;
3474 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3475 expand_T xpc;
3476 int error = FALSE;
3477 char_u *result;
3478
3479 rettv->v_type = VAR_STRING;
3480 if (argvars[1].v_type != VAR_UNKNOWN
3481 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003482 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003483 && !error)
3484 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003485 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003486 }
3487
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003488 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003489 if (*s == '%' || *s == '#' || *s == '<')
3490 {
3491 ++emsg_off;
3492 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3493 --emsg_off;
3494 if (rettv->v_type == VAR_LIST)
3495 {
3496 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3497 list_append_string(rettv->vval.v_list, result, -1);
3498 else
3499 vim_free(result);
3500 }
3501 else
3502 rettv->vval.v_string = result;
3503 }
3504 else
3505 {
3506 /* When the optional second argument is non-zero, don't remove matches
3507 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3508 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003509 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003510 options |= WILD_KEEP_ALL;
3511 if (!error)
3512 {
3513 ExpandInit(&xpc);
3514 xpc.xp_context = EXPAND_FILES;
3515 if (p_wic)
3516 options += WILD_ICASE;
3517 if (rettv->v_type == VAR_STRING)
3518 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3519 options, WILD_ALL);
3520 else if (rettv_list_alloc(rettv) != FAIL)
3521 {
3522 int i;
3523
3524 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3525 for (i = 0; i < xpc.xp_numfiles; i++)
3526 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3527 ExpandCleanup(&xpc);
3528 }
3529 }
3530 else
3531 rettv->vval.v_string = NULL;
3532 }
3533}
3534
3535/*
3536 * "extend(list, list [, idx])" function
3537 * "extend(dict, dict [, action])" function
3538 */
3539 static void
3540f_extend(typval_T *argvars, typval_T *rettv)
3541{
3542 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3543
3544 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3545 {
3546 list_T *l1, *l2;
3547 listitem_T *item;
3548 long before;
3549 int error = FALSE;
3550
3551 l1 = argvars[0].vval.v_list;
3552 l2 = argvars[1].vval.v_list;
3553 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
3554 && l2 != NULL)
3555 {
3556 if (argvars[2].v_type != VAR_UNKNOWN)
3557 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003558 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003559 if (error)
3560 return; /* type error; errmsg already given */
3561
3562 if (before == l1->lv_len)
3563 item = NULL;
3564 else
3565 {
3566 item = list_find(l1, before);
3567 if (item == NULL)
3568 {
3569 EMSGN(_(e_listidx), before);
3570 return;
3571 }
3572 }
3573 }
3574 else
3575 item = NULL;
3576 list_extend(l1, l2, item);
3577
3578 copy_tv(&argvars[0], rettv);
3579 }
3580 }
3581 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3582 {
3583 dict_T *d1, *d2;
3584 char_u *action;
3585 int i;
3586
3587 d1 = argvars[0].vval.v_dict;
3588 d2 = argvars[1].vval.v_dict;
3589 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
3590 && d2 != NULL)
3591 {
3592 /* Check the third argument. */
3593 if (argvars[2].v_type != VAR_UNKNOWN)
3594 {
3595 static char *(av[]) = {"keep", "force", "error"};
3596
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003597 action = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003598 if (action == NULL)
3599 return; /* type error; errmsg already given */
3600 for (i = 0; i < 3; ++i)
3601 if (STRCMP(action, av[i]) == 0)
3602 break;
3603 if (i == 3)
3604 {
3605 EMSG2(_(e_invarg2), action);
3606 return;
3607 }
3608 }
3609 else
3610 action = (char_u *)"force";
3611
3612 dict_extend(d1, d2, action);
3613
3614 copy_tv(&argvars[0], rettv);
3615 }
3616 }
3617 else
3618 EMSG2(_(e_listdictarg), "extend()");
3619}
3620
3621/*
3622 * "feedkeys()" function
3623 */
3624 static void
3625f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3626{
3627 int remap = TRUE;
3628 int insert = FALSE;
3629 char_u *keys, *flags;
3630 char_u nbuf[NUMBUFLEN];
3631 int typed = FALSE;
3632 int execute = FALSE;
3633 int dangerous = FALSE;
3634 char_u *keys_esc;
3635
3636 /* This is not allowed in the sandbox. If the commands would still be
3637 * executed in the sandbox it would be OK, but it probably happens later,
3638 * when "sandbox" is no longer set. */
3639 if (check_secure())
3640 return;
3641
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003642 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003643
3644 if (argvars[1].v_type != VAR_UNKNOWN)
3645 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003646 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003647 for ( ; *flags != NUL; ++flags)
3648 {
3649 switch (*flags)
3650 {
3651 case 'n': remap = FALSE; break;
3652 case 'm': remap = TRUE; break;
3653 case 't': typed = TRUE; break;
3654 case 'i': insert = TRUE; break;
3655 case 'x': execute = TRUE; break;
3656 case '!': dangerous = TRUE; break;
3657 }
3658 }
3659 }
3660
3661 if (*keys != NUL || execute)
3662 {
3663 /* Need to escape K_SPECIAL and CSI before putting the string in the
3664 * typeahead buffer. */
3665 keys_esc = vim_strsave_escape_csi(keys);
3666 if (keys_esc != NULL)
3667 {
3668 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
3669 insert ? 0 : typebuf.tb_len, !typed, FALSE);
3670 vim_free(keys_esc);
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003671 if (vgetc_busy
3672#ifdef FEAT_TIMERS
3673 || timer_busy
3674#endif
3675 )
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003676 typebuf_was_filled = TRUE;
3677 if (execute)
3678 {
3679 int save_msg_scroll = msg_scroll;
3680
3681 /* Avoid a 1 second delay when the keys start Insert mode. */
3682 msg_scroll = FALSE;
3683
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003684 if (!dangerous)
3685 ++ex_normal_busy;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02003686 exec_normal(TRUE, FALSE, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003687 if (!dangerous)
3688 --ex_normal_busy;
3689
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003690 msg_scroll |= save_msg_scroll;
3691 }
3692 }
3693 }
3694}
3695
3696/*
3697 * "filereadable()" function
3698 */
3699 static void
3700f_filereadable(typval_T *argvars, typval_T *rettv)
3701{
3702 int fd;
3703 char_u *p;
3704 int n;
3705
3706#ifndef O_NONBLOCK
3707# define O_NONBLOCK 0
3708#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003709 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003710 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3711 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3712 {
3713 n = TRUE;
3714 close(fd);
3715 }
3716 else
3717 n = FALSE;
3718
3719 rettv->vval.v_number = n;
3720}
3721
3722/*
3723 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3724 * rights to write into.
3725 */
3726 static void
3727f_filewritable(typval_T *argvars, typval_T *rettv)
3728{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003729 rettv->vval.v_number = filewritable(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003730}
3731
3732 static void
3733findfilendir(
3734 typval_T *argvars UNUSED,
3735 typval_T *rettv,
3736 int find_what UNUSED)
3737{
3738#ifdef FEAT_SEARCHPATH
3739 char_u *fname;
3740 char_u *fresult = NULL;
3741 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3742 char_u *p;
3743 char_u pathbuf[NUMBUFLEN];
3744 int count = 1;
3745 int first = TRUE;
3746 int error = FALSE;
3747#endif
3748
3749 rettv->vval.v_string = NULL;
3750 rettv->v_type = VAR_STRING;
3751
3752#ifdef FEAT_SEARCHPATH
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003753 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003754
3755 if (argvars[1].v_type != VAR_UNKNOWN)
3756 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003757 p = tv_get_string_buf_chk(&argvars[1], pathbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003758 if (p == NULL)
3759 error = TRUE;
3760 else
3761 {
3762 if (*p != NUL)
3763 path = p;
3764
3765 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003766 count = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003767 }
3768 }
3769
3770 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3771 error = TRUE;
3772
3773 if (*fname != NUL && !error)
3774 {
3775 do
3776 {
3777 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3778 vim_free(fresult);
3779 fresult = find_file_in_path_option(first ? fname : NULL,
3780 first ? (int)STRLEN(fname) : 0,
3781 0, first, path,
3782 find_what,
3783 curbuf->b_ffname,
3784 find_what == FINDFILE_DIR
3785 ? (char_u *)"" : curbuf->b_p_sua);
3786 first = FALSE;
3787
3788 if (fresult != NULL && rettv->v_type == VAR_LIST)
3789 list_append_string(rettv->vval.v_list, fresult, -1);
3790
3791 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3792 }
3793
3794 if (rettv->v_type == VAR_STRING)
3795 rettv->vval.v_string = fresult;
3796#endif
3797}
3798
3799/*
3800 * "filter()" function
3801 */
3802 static void
3803f_filter(typval_T *argvars, typval_T *rettv)
3804{
3805 filter_map(argvars, rettv, FALSE);
3806}
3807
3808/*
3809 * "finddir({fname}[, {path}[, {count}]])" function
3810 */
3811 static void
3812f_finddir(typval_T *argvars, typval_T *rettv)
3813{
3814 findfilendir(argvars, rettv, FINDFILE_DIR);
3815}
3816
3817/*
3818 * "findfile({fname}[, {path}[, {count}]])" function
3819 */
3820 static void
3821f_findfile(typval_T *argvars, typval_T *rettv)
3822{
3823 findfilendir(argvars, rettv, FINDFILE_FILE);
3824}
3825
3826#ifdef FEAT_FLOAT
3827/*
3828 * "float2nr({float})" function
3829 */
3830 static void
3831f_float2nr(typval_T *argvars, typval_T *rettv)
3832{
3833 float_T f = 0.0;
3834
3835 if (get_float_arg(argvars, &f) == OK)
3836 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003837 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003838 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003839 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003840 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003841 else
3842 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003843 }
3844}
3845
3846/*
3847 * "floor({float})" function
3848 */
3849 static void
3850f_floor(typval_T *argvars, typval_T *rettv)
3851{
3852 float_T f = 0.0;
3853
3854 rettv->v_type = VAR_FLOAT;
3855 if (get_float_arg(argvars, &f) == OK)
3856 rettv->vval.v_float = floor(f);
3857 else
3858 rettv->vval.v_float = 0.0;
3859}
3860
3861/*
3862 * "fmod()" function
3863 */
3864 static void
3865f_fmod(typval_T *argvars, typval_T *rettv)
3866{
3867 float_T fx = 0.0, fy = 0.0;
3868
3869 rettv->v_type = VAR_FLOAT;
3870 if (get_float_arg(argvars, &fx) == OK
3871 && get_float_arg(&argvars[1], &fy) == OK)
3872 rettv->vval.v_float = fmod(fx, fy);
3873 else
3874 rettv->vval.v_float = 0.0;
3875}
3876#endif
3877
3878/*
3879 * "fnameescape({string})" function
3880 */
3881 static void
3882f_fnameescape(typval_T *argvars, typval_T *rettv)
3883{
3884 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003885 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003886 rettv->v_type = VAR_STRING;
3887}
3888
3889/*
3890 * "fnamemodify({fname}, {mods})" function
3891 */
3892 static void
3893f_fnamemodify(typval_T *argvars, typval_T *rettv)
3894{
3895 char_u *fname;
3896 char_u *mods;
3897 int usedlen = 0;
3898 int len;
3899 char_u *fbuf = NULL;
3900 char_u buf[NUMBUFLEN];
3901
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003902 fname = tv_get_string_chk(&argvars[0]);
3903 mods = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003904 if (fname == NULL || mods == NULL)
3905 fname = NULL;
3906 else
3907 {
3908 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02003909 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003910 }
3911
3912 rettv->v_type = VAR_STRING;
3913 if (fname == NULL)
3914 rettv->vval.v_string = NULL;
3915 else
3916 rettv->vval.v_string = vim_strnsave(fname, len);
3917 vim_free(fbuf);
3918}
3919
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003920/*
3921 * "foldclosed()" function
3922 */
3923 static void
3924foldclosed_both(
3925 typval_T *argvars UNUSED,
3926 typval_T *rettv,
3927 int end UNUSED)
3928{
3929#ifdef FEAT_FOLDING
3930 linenr_T lnum;
3931 linenr_T first, last;
3932
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003933 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003934 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3935 {
3936 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3937 {
3938 if (end)
3939 rettv->vval.v_number = (varnumber_T)last;
3940 else
3941 rettv->vval.v_number = (varnumber_T)first;
3942 return;
3943 }
3944 }
3945#endif
3946 rettv->vval.v_number = -1;
3947}
3948
3949/*
3950 * "foldclosed()" function
3951 */
3952 static void
3953f_foldclosed(typval_T *argvars, typval_T *rettv)
3954{
3955 foldclosed_both(argvars, rettv, FALSE);
3956}
3957
3958/*
3959 * "foldclosedend()" function
3960 */
3961 static void
3962f_foldclosedend(typval_T *argvars, typval_T *rettv)
3963{
3964 foldclosed_both(argvars, rettv, TRUE);
3965}
3966
3967/*
3968 * "foldlevel()" function
3969 */
3970 static void
3971f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3972{
3973#ifdef FEAT_FOLDING
3974 linenr_T lnum;
3975
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003976 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003977 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3978 rettv->vval.v_number = foldLevel(lnum);
3979#endif
3980}
3981
3982/*
3983 * "foldtext()" function
3984 */
3985 static void
3986f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
3987{
3988#ifdef FEAT_FOLDING
3989 linenr_T foldstart;
3990 linenr_T foldend;
3991 char_u *dashes;
3992 linenr_T lnum;
3993 char_u *s;
3994 char_u *r;
3995 int len;
3996 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003997 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003998#endif
3999
4000 rettv->v_type = VAR_STRING;
4001 rettv->vval.v_string = NULL;
4002#ifdef FEAT_FOLDING
4003 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
4004 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
4005 dashes = get_vim_var_str(VV_FOLDDASHES);
4006 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
4007 && dashes != NULL)
4008 {
4009 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02004010 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004011 if (!linewhite(lnum))
4012 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004013
4014 /* Find interesting text in this line. */
4015 s = skipwhite(ml_get(lnum));
4016 /* skip C comment-start */
4017 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
4018 {
4019 s = skipwhite(s + 2);
4020 if (*skipwhite(s) == NUL
4021 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
4022 {
4023 s = skipwhite(ml_get(lnum + 1));
4024 if (*s == '*')
4025 s = skipwhite(s + 1);
4026 }
4027 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02004028 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01004029 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004030 r = alloc((unsigned)(STRLEN(txt)
4031 + STRLEN(dashes) /* for %s */
4032 + 20 /* for %3ld */
4033 + STRLEN(s))); /* concatenated */
4034 if (r != NULL)
4035 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02004036 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004037 len = (int)STRLEN(r);
4038 STRCAT(r, s);
4039 /* remove 'foldmarker' and 'commentstring' */
4040 foldtext_cleanup(r + len);
4041 rettv->vval.v_string = r;
4042 }
4043 }
4044#endif
4045}
4046
4047/*
4048 * "foldtextresult(lnum)" function
4049 */
4050 static void
4051f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
4052{
4053#ifdef FEAT_FOLDING
4054 linenr_T lnum;
4055 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004056 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004057 foldinfo_T foldinfo;
4058 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004059 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004060#endif
4061
4062 rettv->v_type = VAR_STRING;
4063 rettv->vval.v_string = NULL;
4064#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004065 if (entered)
4066 return; /* reject recursive use */
4067 entered = TRUE;
4068
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004069 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004070 /* treat illegal types and illegal string values for {lnum} the same */
4071 if (lnum < 0)
4072 lnum = 0;
4073 fold_count = foldedCount(curwin, lnum, &foldinfo);
4074 if (fold_count > 0)
4075 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01004076 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
4077 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004078 if (text == buf)
4079 text = vim_strsave(text);
4080 rettv->vval.v_string = text;
4081 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004082
4083 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004084#endif
4085}
4086
4087/*
4088 * "foreground()" function
4089 */
4090 static void
4091f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4092{
4093#ifdef FEAT_GUI
4094 if (gui.in_use)
4095 gui_mch_set_foreground();
4096#else
4097# ifdef WIN32
4098 win32_set_foreground();
4099# endif
4100#endif
4101}
4102
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004103 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004104common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004105{
4106 char_u *s;
4107 char_u *name;
4108 int use_string = FALSE;
4109 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004110 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004111
4112 if (argvars[0].v_type == VAR_FUNC)
4113 {
4114 /* function(MyFunc, [arg], dict) */
4115 s = argvars[0].vval.v_string;
4116 }
4117 else if (argvars[0].v_type == VAR_PARTIAL
4118 && argvars[0].vval.v_partial != NULL)
4119 {
4120 /* function(dict.MyFunc, [arg]) */
4121 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004122 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004123 }
4124 else
4125 {
4126 /* function('MyFunc', [arg], dict) */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004127 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004128 use_string = TRUE;
4129 }
4130
Bram Moolenaar843b8842016-08-21 14:36:15 +02004131 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004132 {
4133 name = s;
4134 trans_name = trans_function_name(&name, FALSE,
4135 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4136 if (*name != NUL)
4137 s = NULL;
4138 }
4139
Bram Moolenaar843b8842016-08-21 14:36:15 +02004140 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4141 || (is_funcref && trans_name == NULL))
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004142 EMSG2(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004143 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004144 else if (trans_name != NULL && (is_funcref
4145 ? find_func(trans_name) == NULL
4146 : !translated_function_exists(trans_name)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004147 EMSG2(_("E700: Unknown function: %s"), s);
4148 else
4149 {
4150 int dict_idx = 0;
4151 int arg_idx = 0;
4152 list_T *list = NULL;
4153
4154 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4155 {
4156 char sid_buf[25];
4157 int off = *s == 's' ? 2 : 5;
4158
4159 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4160 * also be called from another script. Using trans_function_name()
4161 * would also work, but some plugins depend on the name being
4162 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004163 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004164 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
4165 if (name != NULL)
4166 {
4167 STRCPY(name, sid_buf);
4168 STRCAT(name, s + off);
4169 }
4170 }
4171 else
4172 name = vim_strsave(s);
4173
4174 if (argvars[1].v_type != VAR_UNKNOWN)
4175 {
4176 if (argvars[2].v_type != VAR_UNKNOWN)
4177 {
4178 /* function(name, [args], dict) */
4179 arg_idx = 1;
4180 dict_idx = 2;
4181 }
4182 else if (argvars[1].v_type == VAR_DICT)
4183 /* function(name, dict) */
4184 dict_idx = 1;
4185 else
4186 /* function(name, [args]) */
4187 arg_idx = 1;
4188 if (dict_idx > 0)
4189 {
4190 if (argvars[dict_idx].v_type != VAR_DICT)
4191 {
4192 EMSG(_("E922: expected a dict"));
4193 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004194 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004195 }
4196 if (argvars[dict_idx].vval.v_dict == NULL)
4197 dict_idx = 0;
4198 }
4199 if (arg_idx > 0)
4200 {
4201 if (argvars[arg_idx].v_type != VAR_LIST)
4202 {
4203 EMSG(_("E923: Second argument of function() must be a list or a dict"));
4204 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004205 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004206 }
4207 list = argvars[arg_idx].vval.v_list;
4208 if (list == NULL || list->lv_len == 0)
4209 arg_idx = 0;
4210 }
4211 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004212 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004213 {
4214 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
4215
4216 /* result is a VAR_PARTIAL */
4217 if (pt == NULL)
4218 vim_free(name);
4219 else
4220 {
4221 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4222 {
4223 listitem_T *li;
4224 int i = 0;
4225 int arg_len = 0;
4226 int lv_len = 0;
4227
4228 if (arg_pt != NULL)
4229 arg_len = arg_pt->pt_argc;
4230 if (list != NULL)
4231 lv_len = list->lv_len;
4232 pt->pt_argc = arg_len + lv_len;
4233 pt->pt_argv = (typval_T *)alloc(
4234 sizeof(typval_T) * pt->pt_argc);
4235 if (pt->pt_argv == NULL)
4236 {
4237 vim_free(pt);
4238 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004239 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004240 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004241 for (i = 0; i < arg_len; i++)
4242 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4243 if (lv_len > 0)
4244 for (li = list->lv_first; li != NULL;
4245 li = li->li_next)
4246 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004247 }
4248
4249 /* For "function(dict.func, [], dict)" and "func" is a partial
4250 * use "dict". That is backwards compatible. */
4251 if (dict_idx > 0)
4252 {
4253 /* The dict is bound explicitly, pt_auto is FALSE. */
4254 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4255 ++pt->pt_dict->dv_refcount;
4256 }
4257 else if (arg_pt != NULL)
4258 {
4259 /* If the dict was bound automatically the result is also
4260 * bound automatically. */
4261 pt->pt_dict = arg_pt->pt_dict;
4262 pt->pt_auto = arg_pt->pt_auto;
4263 if (pt->pt_dict != NULL)
4264 ++pt->pt_dict->dv_refcount;
4265 }
4266
4267 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004268 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4269 {
4270 pt->pt_func = arg_pt->pt_func;
4271 func_ptr_ref(pt->pt_func);
4272 vim_free(name);
4273 }
4274 else if (is_funcref)
4275 {
4276 pt->pt_func = find_func(trans_name);
4277 func_ptr_ref(pt->pt_func);
4278 vim_free(name);
4279 }
4280 else
4281 {
4282 pt->pt_name = name;
4283 func_ref(name);
4284 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004285 }
4286 rettv->v_type = VAR_PARTIAL;
4287 rettv->vval.v_partial = pt;
4288 }
4289 else
4290 {
4291 /* result is a VAR_FUNC */
4292 rettv->v_type = VAR_FUNC;
4293 rettv->vval.v_string = name;
4294 func_ref(name);
4295 }
4296 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004297theend:
4298 vim_free(trans_name);
4299}
4300
4301/*
4302 * "funcref()" function
4303 */
4304 static void
4305f_funcref(typval_T *argvars, typval_T *rettv)
4306{
4307 common_function(argvars, rettv, TRUE);
4308}
4309
4310/*
4311 * "function()" function
4312 */
4313 static void
4314f_function(typval_T *argvars, typval_T *rettv)
4315{
4316 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004317}
4318
4319/*
4320 * "garbagecollect()" function
4321 */
4322 static void
4323f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4324{
4325 /* This is postponed until we are back at the toplevel, because we may be
4326 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4327 want_garbage_collect = TRUE;
4328
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004329 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004330 garbage_collect_at_exit = TRUE;
4331}
4332
4333/*
4334 * "get()" function
4335 */
4336 static void
4337f_get(typval_T *argvars, typval_T *rettv)
4338{
4339 listitem_T *li;
4340 list_T *l;
4341 dictitem_T *di;
4342 dict_T *d;
4343 typval_T *tv = NULL;
4344
4345 if (argvars[0].v_type == VAR_LIST)
4346 {
4347 if ((l = argvars[0].vval.v_list) != NULL)
4348 {
4349 int error = FALSE;
4350
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004351 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004352 if (!error && li != NULL)
4353 tv = &li->li_tv;
4354 }
4355 }
4356 else if (argvars[0].v_type == VAR_DICT)
4357 {
4358 if ((d = argvars[0].vval.v_dict) != NULL)
4359 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004360 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004361 if (di != NULL)
4362 tv = &di->di_tv;
4363 }
4364 }
4365 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4366 {
4367 partial_T *pt;
4368 partial_T fref_pt;
4369
4370 if (argvars[0].v_type == VAR_PARTIAL)
4371 pt = argvars[0].vval.v_partial;
4372 else
4373 {
4374 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4375 fref_pt.pt_name = argvars[0].vval.v_string;
4376 pt = &fref_pt;
4377 }
4378
4379 if (pt != NULL)
4380 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004381 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004382 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004383
4384 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4385 {
4386 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004387 n = partial_name(pt);
4388 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004389 rettv->vval.v_string = NULL;
4390 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004391 {
4392 rettv->vval.v_string = vim_strsave(n);
4393 if (rettv->v_type == VAR_FUNC)
4394 func_ref(rettv->vval.v_string);
4395 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004396 }
4397 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004398 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004399 else if (STRCMP(what, "args") == 0)
4400 {
4401 rettv->v_type = VAR_LIST;
4402 if (rettv_list_alloc(rettv) == OK)
4403 {
4404 int i;
4405
4406 for (i = 0; i < pt->pt_argc; ++i)
4407 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4408 }
4409 }
4410 else
4411 EMSG2(_(e_invarg2), what);
4412 return;
4413 }
4414 }
4415 else
4416 EMSG2(_(e_listdictarg), "get()");
4417
4418 if (tv == NULL)
4419 {
4420 if (argvars[2].v_type != VAR_UNKNOWN)
4421 copy_tv(&argvars[2], rettv);
4422 }
4423 else
4424 copy_tv(tv, rettv);
4425}
4426
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004427#ifdef FEAT_SIGNS
4428/*
4429 * Returns information about signs placed in a buffer as list of dicts.
4430 */
4431 static void
4432get_buffer_signs(buf_T *buf, list_T *l)
4433{
4434 signlist_T *sign;
Bram Moolenaar162b7142018-12-21 15:17:36 +01004435 dict_T *d;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004436
Bram Moolenaar6436cd82018-12-27 00:28:33 +01004437 FOR_ALL_SIGNS_IN_BUF(buf, sign)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004438 {
Bram Moolenaar162b7142018-12-21 15:17:36 +01004439 if ((d = sign_get_info(sign)) != NULL)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004440 list_append_dict(l, d);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004441 }
4442}
4443#endif
4444
4445/*
4446 * Returns buffer options, variables and other attributes in a dictionary.
4447 */
4448 static dict_T *
4449get_buffer_info(buf_T *buf)
4450{
4451 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004452 tabpage_T *tp;
4453 win_T *wp;
4454 list_T *windows;
4455
4456 dict = dict_alloc();
4457 if (dict == NULL)
4458 return NULL;
4459
Bram Moolenaare0be1672018-07-08 16:50:37 +02004460 dict_add_number(dict, "bufnr", buf->b_fnum);
4461 dict_add_string(dict, "name", buf->b_ffname);
4462 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4463 : buflist_findlnum(buf));
4464 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4465 dict_add_number(dict, "listed", buf->b_p_bl);
4466 dict_add_number(dict, "changed", bufIsChanged(buf));
4467 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4468 dict_add_number(dict, "hidden",
4469 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004470
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004471 /* Get a reference to buffer variables */
4472 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004473
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004474 /* List of windows displaying this buffer */
4475 windows = list_alloc();
4476 if (windows != NULL)
4477 {
4478 FOR_ALL_TAB_WINDOWS(tp, wp)
4479 if (wp->w_buffer == buf)
4480 list_append_number(windows, (varnumber_T)wp->w_id);
4481 dict_add_list(dict, "windows", windows);
4482 }
4483
4484#ifdef FEAT_SIGNS
4485 if (buf->b_signlist != NULL)
4486 {
4487 /* List of signs placed in this buffer */
4488 list_T *signs = list_alloc();
4489 if (signs != NULL)
4490 {
4491 get_buffer_signs(buf, signs);
4492 dict_add_list(dict, "signs", signs);
4493 }
4494 }
4495#endif
4496
4497 return dict;
4498}
4499
4500/*
4501 * "getbufinfo()" function
4502 */
4503 static void
4504f_getbufinfo(typval_T *argvars, typval_T *rettv)
4505{
4506 buf_T *buf = NULL;
4507 buf_T *argbuf = NULL;
4508 dict_T *d;
4509 int filtered = FALSE;
4510 int sel_buflisted = FALSE;
4511 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004512 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004513
4514 if (rettv_list_alloc(rettv) != OK)
4515 return;
4516
4517 /* List of all the buffers or selected buffers */
4518 if (argvars[0].v_type == VAR_DICT)
4519 {
4520 dict_T *sel_d = argvars[0].vval.v_dict;
4521
4522 if (sel_d != NULL)
4523 {
4524 dictitem_T *di;
4525
4526 filtered = TRUE;
4527
4528 di = dict_find(sel_d, (char_u *)"buflisted", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004529 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004530 sel_buflisted = TRUE;
4531
4532 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004533 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004534 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004535
4536 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004537 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004538 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004539 }
4540 }
4541 else if (argvars[0].v_type != VAR_UNKNOWN)
4542 {
4543 /* Information about one buffer. Argument specifies the buffer */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004544 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004545 ++emsg_off;
4546 argbuf = get_buf_tv(&argvars[0], FALSE);
4547 --emsg_off;
4548 if (argbuf == NULL)
4549 return;
4550 }
4551
4552 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004553 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004554 {
4555 if (argbuf != NULL && argbuf != buf)
4556 continue;
4557 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004558 || (sel_buflisted && !buf->b_p_bl)
4559 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004560 continue;
4561
4562 d = get_buffer_info(buf);
4563 if (d != NULL)
4564 list_append_dict(rettv->vval.v_list, d);
4565 if (argbuf != NULL)
4566 return;
4567 }
4568}
4569
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004570/*
4571 * Get line or list of lines from buffer "buf" into "rettv".
4572 * Return a range (from start to end) of lines in rettv from the specified
4573 * buffer.
4574 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4575 */
4576 static void
4577get_buffer_lines(
4578 buf_T *buf,
4579 linenr_T start,
4580 linenr_T end,
4581 int retlist,
4582 typval_T *rettv)
4583{
4584 char_u *p;
4585
4586 rettv->v_type = VAR_STRING;
4587 rettv->vval.v_string = NULL;
4588 if (retlist && rettv_list_alloc(rettv) == FAIL)
4589 return;
4590
4591 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4592 return;
4593
4594 if (!retlist)
4595 {
4596 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4597 p = ml_get_buf(buf, start, FALSE);
4598 else
4599 p = (char_u *)"";
4600 rettv->vval.v_string = vim_strsave(p);
4601 }
4602 else
4603 {
4604 if (end < start)
4605 return;
4606
4607 if (start < 1)
4608 start = 1;
4609 if (end > buf->b_ml.ml_line_count)
4610 end = buf->b_ml.ml_line_count;
4611 while (start <= end)
4612 if (list_append_string(rettv->vval.v_list,
4613 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4614 break;
4615 }
4616}
4617
4618/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004619 * "getbufline()" function
4620 */
4621 static void
4622f_getbufline(typval_T *argvars, typval_T *rettv)
4623{
4624 linenr_T lnum;
4625 linenr_T end;
4626 buf_T *buf;
4627
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004628 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004629 ++emsg_off;
4630 buf = get_buf_tv(&argvars[0], FALSE);
4631 --emsg_off;
4632
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004633 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004634 if (argvars[2].v_type == VAR_UNKNOWN)
4635 end = lnum;
4636 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004637 end = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004638
4639 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4640}
4641
4642/*
4643 * "getbufvar()" function
4644 */
4645 static void
4646f_getbufvar(typval_T *argvars, typval_T *rettv)
4647{
4648 buf_T *buf;
4649 buf_T *save_curbuf;
4650 char_u *varname;
4651 dictitem_T *v;
4652 int done = FALSE;
4653
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004654 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
4655 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004656 ++emsg_off;
4657 buf = get_buf_tv(&argvars[0], FALSE);
4658
4659 rettv->v_type = VAR_STRING;
4660 rettv->vval.v_string = NULL;
4661
4662 if (buf != NULL && varname != NULL)
4663 {
4664 /* set curbuf to be our buf, temporarily */
4665 save_curbuf = curbuf;
4666 curbuf = buf;
4667
Bram Moolenaar30567352016-08-27 21:25:44 +02004668 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004669 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004670 if (varname[1] == NUL)
4671 {
4672 /* get all buffer-local options in a dict */
4673 dict_T *opts = get_winbuf_options(TRUE);
4674
4675 if (opts != NULL)
4676 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004677 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004678 done = TRUE;
4679 }
4680 }
4681 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4682 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004683 done = TRUE;
4684 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004685 else
4686 {
4687 /* Look up the variable. */
4688 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4689 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4690 'b', varname, FALSE);
4691 if (v != NULL)
4692 {
4693 copy_tv(&v->di_tv, rettv);
4694 done = TRUE;
4695 }
4696 }
4697
4698 /* restore previous notion of curbuf */
4699 curbuf = save_curbuf;
4700 }
4701
4702 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4703 /* use the default value */
4704 copy_tv(&argvars[2], rettv);
4705
4706 --emsg_off;
4707}
4708
4709/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004710 * "getchangelist()" function
4711 */
4712 static void
4713f_getchangelist(typval_T *argvars, typval_T *rettv)
4714{
4715#ifdef FEAT_JUMPLIST
4716 buf_T *buf;
4717 int i;
4718 list_T *l;
4719 dict_T *d;
4720#endif
4721
4722 if (rettv_list_alloc(rettv) != OK)
4723 return;
4724
4725#ifdef FEAT_JUMPLIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004726 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004727 ++emsg_off;
4728 buf = get_buf_tv(&argvars[0], FALSE);
4729 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004730 if (buf == NULL)
4731 return;
4732
4733 l = list_alloc();
4734 if (l == NULL)
4735 return;
4736
4737 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4738 return;
4739 /*
4740 * The current window change list index tracks only the position in the
4741 * current buffer change list. For other buffers, use the change list
4742 * length as the current index.
4743 */
4744 list_append_number(rettv->vval.v_list,
4745 (varnumber_T)((buf == curwin->w_buffer)
4746 ? curwin->w_changelistidx : buf->b_changelistlen));
4747
4748 for (i = 0; i < buf->b_changelistlen; ++i)
4749 {
4750 if (buf->b_changelist[i].lnum == 0)
4751 continue;
4752 if ((d = dict_alloc()) == NULL)
4753 return;
4754 if (list_append_dict(l, d) == FAIL)
4755 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004756 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4757 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004758# ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +02004759 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004760# endif
4761 }
4762#endif
4763}
4764/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004765 * "getchar()" function
4766 */
4767 static void
4768f_getchar(typval_T *argvars, typval_T *rettv)
4769{
4770 varnumber_T n;
4771 int error = FALSE;
4772
Bram Moolenaar84d93902018-09-11 20:10:20 +02004773#ifdef MESSAGE_QUEUE
4774 // vpeekc() used to check for messages, but that caused problems, invoking
4775 // a callback where it was not expected. Some plugins use getchar(1) in a
4776 // loop to await a message, therefore make sure we check for messages here.
4777 parse_queued_messages();
4778#endif
4779
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004780 /* Position the cursor. Needed after a message that ends in a space. */
4781 windgoto(msg_row, msg_col);
4782
4783 ++no_mapping;
4784 ++allow_keys;
4785 for (;;)
4786 {
4787 if (argvars[0].v_type == VAR_UNKNOWN)
4788 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004789 n = plain_vgetc();
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004790 else if (tv_get_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004791 /* getchar(1): only check if char avail */
4792 n = vpeekc_any();
4793 else if (error || vpeekc_any() == NUL)
4794 /* illegal argument or getchar(0) and no char avail: return zero */
4795 n = 0;
4796 else
4797 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004798 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004799
4800 if (n == K_IGNORE)
4801 continue;
4802 break;
4803 }
4804 --no_mapping;
4805 --allow_keys;
4806
4807 set_vim_var_nr(VV_MOUSE_WIN, 0);
4808 set_vim_var_nr(VV_MOUSE_WINID, 0);
4809 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4810 set_vim_var_nr(VV_MOUSE_COL, 0);
4811
4812 rettv->vval.v_number = n;
4813 if (IS_SPECIAL(n) || mod_mask != 0)
4814 {
4815 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4816 int i = 0;
4817
4818 /* Turn a special key into three bytes, plus modifier. */
4819 if (mod_mask != 0)
4820 {
4821 temp[i++] = K_SPECIAL;
4822 temp[i++] = KS_MODIFIER;
4823 temp[i++] = mod_mask;
4824 }
4825 if (IS_SPECIAL(n))
4826 {
4827 temp[i++] = K_SPECIAL;
4828 temp[i++] = K_SECOND(n);
4829 temp[i++] = K_THIRD(n);
4830 }
4831#ifdef FEAT_MBYTE
4832 else if (has_mbyte)
4833 i += (*mb_char2bytes)(n, temp + i);
4834#endif
4835 else
4836 temp[i++] = n;
4837 temp[i++] = NUL;
4838 rettv->v_type = VAR_STRING;
4839 rettv->vval.v_string = vim_strsave(temp);
4840
4841#ifdef FEAT_MOUSE
4842 if (is_mouse_key(n))
4843 {
4844 int row = mouse_row;
4845 int col = mouse_col;
4846 win_T *win;
4847 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004848 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004849 int winnr = 1;
4850
4851 if (row >= 0 && col >= 0)
4852 {
4853 /* Find the window at the mouse coordinates and compute the
4854 * text position. */
4855 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004856 if (win == NULL)
4857 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004858 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004859 for (wp = firstwin; wp != win; wp = wp->w_next)
4860 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004861 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4862 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4863 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4864 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4865 }
4866 }
4867#endif
4868 }
4869}
4870
4871/*
4872 * "getcharmod()" function
4873 */
4874 static void
4875f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4876{
4877 rettv->vval.v_number = mod_mask;
4878}
4879
4880/*
4881 * "getcharsearch()" function
4882 */
4883 static void
4884f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4885{
4886 if (rettv_dict_alloc(rettv) != FAIL)
4887 {
4888 dict_T *dict = rettv->vval.v_dict;
4889
Bram Moolenaare0be1672018-07-08 16:50:37 +02004890 dict_add_string(dict, "char", last_csearch());
4891 dict_add_number(dict, "forward", last_csearch_forward());
4892 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004893 }
4894}
4895
4896/*
4897 * "getcmdline()" function
4898 */
4899 static void
4900f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4901{
4902 rettv->v_type = VAR_STRING;
4903 rettv->vval.v_string = get_cmdline_str();
4904}
4905
4906/*
4907 * "getcmdpos()" function
4908 */
4909 static void
4910f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4911{
4912 rettv->vval.v_number = get_cmdline_pos() + 1;
4913}
4914
4915/*
4916 * "getcmdtype()" function
4917 */
4918 static void
4919f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4920{
4921 rettv->v_type = VAR_STRING;
4922 rettv->vval.v_string = alloc(2);
4923 if (rettv->vval.v_string != NULL)
4924 {
4925 rettv->vval.v_string[0] = get_cmdline_type();
4926 rettv->vval.v_string[1] = NUL;
4927 }
4928}
4929
4930/*
4931 * "getcmdwintype()" function
4932 */
4933 static void
4934f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4935{
4936 rettv->v_type = VAR_STRING;
4937 rettv->vval.v_string = NULL;
4938#ifdef FEAT_CMDWIN
4939 rettv->vval.v_string = alloc(2);
4940 if (rettv->vval.v_string != NULL)
4941 {
4942 rettv->vval.v_string[0] = cmdwin_type;
4943 rettv->vval.v_string[1] = NUL;
4944 }
4945#endif
4946}
4947
4948#if defined(FEAT_CMDL_COMPL)
4949/*
4950 * "getcompletion()" function
4951 */
4952 static void
4953f_getcompletion(typval_T *argvars, typval_T *rettv)
4954{
4955 char_u *pat;
4956 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004957 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004958 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
4959 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004960
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004961 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004962 filtered = tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004963
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004964 if (p_wic)
4965 options |= WILD_ICASE;
4966
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004967 /* For filtered results, 'wildignore' is used */
4968 if (!filtered)
4969 options |= WILD_KEEP_ALL;
4970
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004971 ExpandInit(&xpc);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004972 xpc.xp_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004973 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004974 xpc.xp_context = cmdcomplete_str_to_type(tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004975 if (xpc.xp_context == EXPAND_NOTHING)
4976 {
4977 if (argvars[1].v_type == VAR_STRING)
4978 EMSG2(_(e_invarg2), argvars[1].vval.v_string);
4979 else
4980 EMSG(_(e_invarg));
4981 return;
4982 }
4983
4984# if defined(FEAT_MENU)
4985 if (xpc.xp_context == EXPAND_MENUS)
4986 {
4987 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4988 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4989 }
4990# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02004991#ifdef FEAT_CSCOPE
4992 if (xpc.xp_context == EXPAND_CSCOPE)
4993 {
4994 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4995 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4996 }
4997#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02004998#ifdef FEAT_SIGNS
4999 if (xpc.xp_context == EXPAND_SIGN)
5000 {
5001 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
5002 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5003 }
5004#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005005
5006 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
5007 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
5008 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005009 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005010
5011 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
5012
5013 for (i = 0; i < xpc.xp_numfiles; i++)
5014 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5015 }
5016 vim_free(pat);
5017 ExpandCleanup(&xpc);
5018}
5019#endif
5020
5021/*
5022 * "getcwd()" function
5023 */
5024 static void
5025f_getcwd(typval_T *argvars, typval_T *rettv)
5026{
5027 win_T *wp = NULL;
5028 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01005029 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005030
5031 rettv->v_type = VAR_STRING;
5032 rettv->vval.v_string = NULL;
5033
Bram Moolenaar54591292018-02-09 20:53:59 +01005034 if (argvars[0].v_type == VAR_NUMBER && argvars[0].vval.v_number == -1)
5035 global = TRUE;
5036 else
5037 wp = find_tabwin(&argvars[0], &argvars[1]);
5038
5039 if (wp != NULL && wp->w_localdir != NULL)
5040 rettv->vval.v_string = vim_strsave(wp->w_localdir);
5041 else if (wp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005042 {
Bram Moolenaar54591292018-02-09 20:53:59 +01005043 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005044 rettv->vval.v_string = vim_strsave(globaldir);
5045 else
5046 {
5047 cwd = alloc(MAXPATHL);
5048 if (cwd != NULL)
5049 {
5050 if (mch_dirname(cwd, MAXPATHL) != FAIL)
5051 rettv->vval.v_string = vim_strsave(cwd);
5052 vim_free(cwd);
5053 }
5054 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005055 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02005056#ifdef BACKSLASH_IN_FILENAME
5057 if (rettv->vval.v_string != NULL)
5058 slash_adjust(rettv->vval.v_string);
5059#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005060}
5061
5062/*
5063 * "getfontname()" function
5064 */
5065 static void
5066f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
5067{
5068 rettv->v_type = VAR_STRING;
5069 rettv->vval.v_string = NULL;
5070#ifdef FEAT_GUI
5071 if (gui.in_use)
5072 {
5073 GuiFont font;
5074 char_u *name = NULL;
5075
5076 if (argvars[0].v_type == VAR_UNKNOWN)
5077 {
5078 /* Get the "Normal" font. Either the name saved by
5079 * hl_set_font_name() or from the font ID. */
5080 font = gui.norm_font;
5081 name = hl_get_font_name();
5082 }
5083 else
5084 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005085 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005086 if (STRCMP(name, "*") == 0) /* don't use font dialog */
5087 return;
5088 font = gui_mch_get_font(name, FALSE);
5089 if (font == NOFONT)
5090 return; /* Invalid font name, return empty string. */
5091 }
5092 rettv->vval.v_string = gui_mch_get_fontname(font, name);
5093 if (argvars[0].v_type != VAR_UNKNOWN)
5094 gui_mch_free_font(font);
5095 }
5096#endif
5097}
5098
5099/*
5100 * "getfperm({fname})" function
5101 */
5102 static void
5103f_getfperm(typval_T *argvars, typval_T *rettv)
5104{
5105 char_u *fname;
5106 stat_T st;
5107 char_u *perm = NULL;
5108 char_u flags[] = "rwx";
5109 int i;
5110
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005111 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005112
5113 rettv->v_type = VAR_STRING;
5114 if (mch_stat((char *)fname, &st) >= 0)
5115 {
5116 perm = vim_strsave((char_u *)"---------");
5117 if (perm != NULL)
5118 {
5119 for (i = 0; i < 9; i++)
5120 {
5121 if (st.st_mode & (1 << (8 - i)))
5122 perm[i] = flags[i % 3];
5123 }
5124 }
5125 }
5126 rettv->vval.v_string = perm;
5127}
5128
5129/*
5130 * "getfsize({fname})" function
5131 */
5132 static void
5133f_getfsize(typval_T *argvars, typval_T *rettv)
5134{
5135 char_u *fname;
5136 stat_T st;
5137
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005138 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005139
5140 rettv->v_type = VAR_NUMBER;
5141
5142 if (mch_stat((char *)fname, &st) >= 0)
5143 {
5144 if (mch_isdir(fname))
5145 rettv->vval.v_number = 0;
5146 else
5147 {
5148 rettv->vval.v_number = (varnumber_T)st.st_size;
5149
5150 /* non-perfect check for overflow */
5151 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5152 rettv->vval.v_number = -2;
5153 }
5154 }
5155 else
5156 rettv->vval.v_number = -1;
5157}
5158
5159/*
5160 * "getftime({fname})" function
5161 */
5162 static void
5163f_getftime(typval_T *argvars, typval_T *rettv)
5164{
5165 char_u *fname;
5166 stat_T st;
5167
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005168 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005169
5170 if (mch_stat((char *)fname, &st) >= 0)
5171 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5172 else
5173 rettv->vval.v_number = -1;
5174}
5175
5176/*
5177 * "getftype({fname})" function
5178 */
5179 static void
5180f_getftype(typval_T *argvars, typval_T *rettv)
5181{
5182 char_u *fname;
5183 stat_T st;
5184 char_u *type = NULL;
5185 char *t;
5186
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005187 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005188
5189 rettv->v_type = VAR_STRING;
5190 if (mch_lstat((char *)fname, &st) >= 0)
5191 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005192 if (S_ISREG(st.st_mode))
5193 t = "file";
5194 else if (S_ISDIR(st.st_mode))
5195 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005196 else if (S_ISLNK(st.st_mode))
5197 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005198 else if (S_ISBLK(st.st_mode))
5199 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005200 else if (S_ISCHR(st.st_mode))
5201 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005202 else if (S_ISFIFO(st.st_mode))
5203 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005204 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005205 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005206 else
5207 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005208 type = vim_strsave((char_u *)t);
5209 }
5210 rettv->vval.v_string = type;
5211}
5212
5213/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005214 * "getjumplist()" function
5215 */
5216 static void
5217f_getjumplist(typval_T *argvars, typval_T *rettv)
5218{
5219#ifdef FEAT_JUMPLIST
5220 win_T *wp;
5221 int i;
5222 list_T *l;
5223 dict_T *d;
5224#endif
5225
5226 if (rettv_list_alloc(rettv) != OK)
5227 return;
5228
5229#ifdef FEAT_JUMPLIST
5230 wp = find_tabwin(&argvars[0], &argvars[1]);
5231 if (wp == NULL)
5232 return;
5233
5234 l = list_alloc();
5235 if (l == NULL)
5236 return;
5237
5238 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5239 return;
5240 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5241
Bram Moolenaar48679742018-02-13 13:33:29 +01005242 cleanup_jumplist(wp, TRUE);
5243
Bram Moolenaar4f505882018-02-10 21:06:32 +01005244 for (i = 0; i < wp->w_jumplistlen; ++i)
5245 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005246 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5247 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005248 if ((d = dict_alloc()) == NULL)
5249 return;
5250 if (list_append_dict(l, d) == FAIL)
5251 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005252 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5253 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005254# ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +02005255 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005256# endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005257 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005258 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005259 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005260 }
5261#endif
5262}
5263
5264/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005265 * "getline(lnum, [end])" function
5266 */
5267 static void
5268f_getline(typval_T *argvars, typval_T *rettv)
5269{
5270 linenr_T lnum;
5271 linenr_T end;
5272 int retlist;
5273
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005274 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005275 if (argvars[1].v_type == VAR_UNKNOWN)
5276 {
5277 end = 0;
5278 retlist = FALSE;
5279 }
5280 else
5281 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005282 end = tv_get_lnum(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005283 retlist = TRUE;
5284 }
5285
5286 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5287}
5288
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005289#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005290 static void
5291get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5292{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005293 if (what_arg->v_type == VAR_UNKNOWN)
5294 {
5295 if (rettv_list_alloc(rettv) == OK)
5296 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005297 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005298 }
5299 else
5300 {
5301 if (rettv_dict_alloc(rettv) == OK)
5302 if (is_qf || (wp != NULL))
5303 {
5304 if (what_arg->v_type == VAR_DICT)
5305 {
5306 dict_T *d = what_arg->vval.v_dict;
5307
5308 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005309 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005310 }
5311 else
5312 EMSG(_(e_dictreq));
5313 }
5314 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005315}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005316#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005317
5318/*
5319 * "getloclist()" function
5320 */
5321 static void
5322f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5323{
5324#ifdef FEAT_QUICKFIX
5325 win_T *wp;
5326
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005327 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005328 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5329#endif
5330}
5331
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005332/*
5333 * "getmatches()" function
5334 */
5335 static void
5336f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5337{
5338#ifdef FEAT_SEARCH_EXTRA
5339 dict_T *dict;
5340 matchitem_T *cur = curwin->w_match_head;
5341 int i;
5342
5343 if (rettv_list_alloc(rettv) == OK)
5344 {
5345 while (cur != NULL)
5346 {
5347 dict = dict_alloc();
5348 if (dict == NULL)
5349 return;
5350 if (cur->match.regprog == NULL)
5351 {
5352 /* match added with matchaddpos() */
5353 for (i = 0; i < MAXPOSMATCH; ++i)
5354 {
5355 llpos_T *llpos;
5356 char buf[6];
5357 list_T *l;
5358
5359 llpos = &cur->pos.pos[i];
5360 if (llpos->lnum == 0)
5361 break;
5362 l = list_alloc();
5363 if (l == NULL)
5364 break;
5365 list_append_number(l, (varnumber_T)llpos->lnum);
5366 if (llpos->col > 0)
5367 {
5368 list_append_number(l, (varnumber_T)llpos->col);
5369 list_append_number(l, (varnumber_T)llpos->len);
5370 }
5371 sprintf(buf, "pos%d", i + 1);
5372 dict_add_list(dict, buf, l);
5373 }
5374 }
5375 else
5376 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02005377 dict_add_string(dict, "pattern", cur->pattern);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005378 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02005379 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
5380 dict_add_number(dict, "priority", (long)cur->priority);
5381 dict_add_number(dict, "id", (long)cur->id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005382# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
5383 if (cur->conceal_char)
5384 {
5385 char_u buf[MB_MAXBYTES + 1];
5386
5387 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005388 dict_add_string(dict, "conceal", (char_u *)&buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005389 }
5390# endif
5391 list_append_dict(rettv->vval.v_list, dict);
5392 cur = cur->next;
5393 }
5394 }
5395#endif
5396}
5397
5398/*
5399 * "getpid()" function
5400 */
5401 static void
5402f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5403{
5404 rettv->vval.v_number = mch_get_pid();
5405}
5406
5407 static void
5408getpos_both(
5409 typval_T *argvars,
5410 typval_T *rettv,
5411 int getcurpos)
5412{
5413 pos_T *fp;
5414 list_T *l;
5415 int fnum = -1;
5416
5417 if (rettv_list_alloc(rettv) == OK)
5418 {
5419 l = rettv->vval.v_list;
5420 if (getcurpos)
5421 fp = &curwin->w_cursor;
5422 else
5423 fp = var2fpos(&argvars[0], TRUE, &fnum);
5424 if (fnum != -1)
5425 list_append_number(l, (varnumber_T)fnum);
5426 else
5427 list_append_number(l, (varnumber_T)0);
5428 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5429 : (varnumber_T)0);
5430 list_append_number(l, (fp != NULL)
5431 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5432 : (varnumber_T)0);
5433 list_append_number(l,
5434#ifdef FEAT_VIRTUALEDIT
5435 (fp != NULL) ? (varnumber_T)fp->coladd :
5436#endif
5437 (varnumber_T)0);
5438 if (getcurpos)
5439 {
5440 update_curswant();
5441 list_append_number(l, curwin->w_curswant == MAXCOL ?
5442 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
5443 }
5444 }
5445 else
5446 rettv->vval.v_number = FALSE;
5447}
5448
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005449/*
5450 * "getcurpos()" function
5451 */
5452 static void
5453f_getcurpos(typval_T *argvars, typval_T *rettv)
5454{
5455 getpos_both(argvars, rettv, TRUE);
5456}
5457
5458/*
5459 * "getpos(string)" function
5460 */
5461 static void
5462f_getpos(typval_T *argvars, typval_T *rettv)
5463{
5464 getpos_both(argvars, rettv, FALSE);
5465}
5466
5467/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005468 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005469 */
5470 static void
5471f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5472{
5473#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005474 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005475#endif
5476}
5477
5478/*
5479 * "getreg()" function
5480 */
5481 static void
5482f_getreg(typval_T *argvars, typval_T *rettv)
5483{
5484 char_u *strregname;
5485 int regname;
5486 int arg2 = FALSE;
5487 int return_list = FALSE;
5488 int error = FALSE;
5489
5490 if (argvars[0].v_type != VAR_UNKNOWN)
5491 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005492 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005493 error = strregname == NULL;
5494 if (argvars[1].v_type != VAR_UNKNOWN)
5495 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005496 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005497 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005498 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005499 }
5500 }
5501 else
5502 strregname = get_vim_var_str(VV_REG);
5503
5504 if (error)
5505 return;
5506
5507 regname = (strregname == NULL ? '"' : *strregname);
5508 if (regname == 0)
5509 regname = '"';
5510
5511 if (return_list)
5512 {
5513 rettv->v_type = VAR_LIST;
5514 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5515 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5516 if (rettv->vval.v_list == NULL)
5517 (void)rettv_list_alloc(rettv);
5518 else
5519 ++rettv->vval.v_list->lv_refcount;
5520 }
5521 else
5522 {
5523 rettv->v_type = VAR_STRING;
5524 rettv->vval.v_string = get_reg_contents(regname,
5525 arg2 ? GREG_EXPR_SRC : 0);
5526 }
5527}
5528
5529/*
5530 * "getregtype()" function
5531 */
5532 static void
5533f_getregtype(typval_T *argvars, typval_T *rettv)
5534{
5535 char_u *strregname;
5536 int regname;
5537 char_u buf[NUMBUFLEN + 2];
5538 long reglen = 0;
5539
5540 if (argvars[0].v_type != VAR_UNKNOWN)
5541 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005542 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005543 if (strregname == NULL) /* type error; errmsg already given */
5544 {
5545 rettv->v_type = VAR_STRING;
5546 rettv->vval.v_string = NULL;
5547 return;
5548 }
5549 }
5550 else
5551 /* Default to v:register */
5552 strregname = get_vim_var_str(VV_REG);
5553
5554 regname = (strregname == NULL ? '"' : *strregname);
5555 if (regname == 0)
5556 regname = '"';
5557
5558 buf[0] = NUL;
5559 buf[1] = NUL;
5560 switch (get_reg_type(regname, &reglen))
5561 {
5562 case MLINE: buf[0] = 'V'; break;
5563 case MCHAR: buf[0] = 'v'; break;
5564 case MBLOCK:
5565 buf[0] = Ctrl_V;
5566 sprintf((char *)buf + 1, "%ld", reglen + 1);
5567 break;
5568 }
5569 rettv->v_type = VAR_STRING;
5570 rettv->vval.v_string = vim_strsave(buf);
5571}
5572
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005573/*
5574 * Returns information (variables, options, etc.) about a tab page
5575 * as a dictionary.
5576 */
5577 static dict_T *
5578get_tabpage_info(tabpage_T *tp, int tp_idx)
5579{
5580 win_T *wp;
5581 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005582 list_T *l;
5583
5584 dict = dict_alloc();
5585 if (dict == NULL)
5586 return NULL;
5587
Bram Moolenaare0be1672018-07-08 16:50:37 +02005588 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005589
5590 l = list_alloc();
5591 if (l != NULL)
5592 {
5593 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5594 wp; wp = wp->w_next)
5595 list_append_number(l, (varnumber_T)wp->w_id);
5596 dict_add_list(dict, "windows", l);
5597 }
5598
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005599 /* Make a reference to tabpage variables */
5600 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005601
5602 return dict;
5603}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005604
5605/*
5606 * "gettabinfo()" function
5607 */
5608 static void
5609f_gettabinfo(typval_T *argvars, typval_T *rettv)
5610{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005611 tabpage_T *tp, *tparg = NULL;
5612 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005613 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005614
5615 if (rettv_list_alloc(rettv) != OK)
5616 return;
5617
5618 if (argvars[0].v_type != VAR_UNKNOWN)
5619 {
5620 /* Information about one tab page */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005621 tparg = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005622 if (tparg == NULL)
5623 return;
5624 }
5625
5626 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005627 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005628 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005629 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005630 if (tparg != NULL && tp != tparg)
5631 continue;
5632 d = get_tabpage_info(tp, tpnr);
5633 if (d != NULL)
5634 list_append_dict(rettv->vval.v_list, d);
5635 if (tparg != NULL)
5636 return;
5637 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005638}
5639
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005640/*
5641 * "gettabvar()" function
5642 */
5643 static void
5644f_gettabvar(typval_T *argvars, typval_T *rettv)
5645{
5646 win_T *oldcurwin;
5647 tabpage_T *tp, *oldtabpage;
5648 dictitem_T *v;
5649 char_u *varname;
5650 int done = FALSE;
5651
5652 rettv->v_type = VAR_STRING;
5653 rettv->vval.v_string = NULL;
5654
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005655 varname = tv_get_string_chk(&argvars[1]);
5656 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005657 if (tp != NULL && varname != NULL)
5658 {
5659 /* Set tp to be our tabpage, temporarily. Also set the window to the
5660 * first window in the tabpage, otherwise the window is not valid. */
5661 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005662 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5663 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005664 {
5665 /* look up the variable */
5666 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5667 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5668 if (v != NULL)
5669 {
5670 copy_tv(&v->di_tv, rettv);
5671 done = TRUE;
5672 }
5673 }
5674
5675 /* restore previous notion of curwin */
5676 restore_win(oldcurwin, oldtabpage, TRUE);
5677 }
5678
5679 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5680 copy_tv(&argvars[2], rettv);
5681}
5682
5683/*
5684 * "gettabwinvar()" function
5685 */
5686 static void
5687f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5688{
5689 getwinvar(argvars, rettv, 1);
5690}
5691
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005692/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01005693 * "gettagstack()" function
5694 */
5695 static void
5696f_gettagstack(typval_T *argvars, typval_T *rettv)
5697{
5698 win_T *wp = curwin; // default is current window
5699
5700 if (rettv_dict_alloc(rettv) != OK)
5701 return;
5702
5703 if (argvars[0].v_type != VAR_UNKNOWN)
5704 {
5705 wp = find_win_by_nr_or_id(&argvars[0]);
5706 if (wp == NULL)
5707 return;
5708 }
5709
5710 get_tagstack(wp, rettv->vval.v_dict);
5711}
5712
5713/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005714 * Returns information about a window as a dictionary.
5715 */
5716 static dict_T *
5717get_win_info(win_T *wp, short tpnr, short winnr)
5718{
5719 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005720
5721 dict = dict_alloc();
5722 if (dict == NULL)
5723 return NULL;
5724
Bram Moolenaare0be1672018-07-08 16:50:37 +02005725 dict_add_number(dict, "tabnr", tpnr);
5726 dict_add_number(dict, "winnr", winnr);
5727 dict_add_number(dict, "winid", wp->w_id);
5728 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005729 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005730#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02005731 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005732#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005733 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005734 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005735 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005736
Bram Moolenaar69905d12017-08-13 18:14:47 +02005737#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02005738 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02005739#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005740#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02005741 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
5742 dict_add_number(dict, "loclist",
5743 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02005744#endif
5745
Bram Moolenaar30567352016-08-27 21:25:44 +02005746 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005747 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005748
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005749 return dict;
5750}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005751
5752/*
5753 * "getwininfo()" function
5754 */
5755 static void
5756f_getwininfo(typval_T *argvars, typval_T *rettv)
5757{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005758 tabpage_T *tp;
5759 win_T *wp = NULL, *wparg = NULL;
5760 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005761 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005762
5763 if (rettv_list_alloc(rettv) != OK)
5764 return;
5765
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005766 if (argvars[0].v_type != VAR_UNKNOWN)
5767 {
5768 wparg = win_id2wp(argvars);
5769 if (wparg == NULL)
5770 return;
5771 }
5772
5773 /* Collect information about either all the windows across all the tab
5774 * pages or one particular window.
5775 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005776 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005777 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005778 tabnr++;
5779 winnr = 0;
5780 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005781 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005782 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005783 if (wparg != NULL && wp != wparg)
5784 continue;
5785 d = get_win_info(wp, tabnr, winnr);
5786 if (d != NULL)
5787 list_append_dict(rettv->vval.v_list, d);
5788 if (wparg != NULL)
5789 /* found information about a specific window */
5790 return;
5791 }
5792 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005793}
5794
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005795/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005796 * "win_findbuf()" function
5797 */
5798 static void
5799f_win_findbuf(typval_T *argvars, typval_T *rettv)
5800{
5801 if (rettv_list_alloc(rettv) != FAIL)
5802 win_findbuf(argvars, rettv->vval.v_list);
5803}
5804
5805/*
5806 * "win_getid()" function
5807 */
5808 static void
5809f_win_getid(typval_T *argvars, typval_T *rettv)
5810{
5811 rettv->vval.v_number = win_getid(argvars);
5812}
5813
5814/*
5815 * "win_gotoid()" function
5816 */
5817 static void
5818f_win_gotoid(typval_T *argvars, typval_T *rettv)
5819{
5820 rettv->vval.v_number = win_gotoid(argvars);
5821}
5822
5823/*
5824 * "win_id2tabwin()" function
5825 */
5826 static void
5827f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5828{
5829 if (rettv_list_alloc(rettv) != FAIL)
5830 win_id2tabwin(argvars, rettv->vval.v_list);
5831}
5832
5833/*
5834 * "win_id2win()" function
5835 */
5836 static void
5837f_win_id2win(typval_T *argvars, typval_T *rettv)
5838{
5839 rettv->vval.v_number = win_id2win(argvars);
5840}
5841
5842/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005843 * "win_screenpos()" function
5844 */
5845 static void
5846f_win_screenpos(typval_T *argvars, typval_T *rettv)
5847{
5848 win_T *wp;
5849
5850 if (rettv_list_alloc(rettv) == FAIL)
5851 return;
5852
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005853 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005854 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5855 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5856}
5857
5858/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005859 * "getwinpos({timeout})" function
5860 */
5861 static void
5862f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
5863{
5864 int x = -1;
5865 int y = -1;
5866
5867 if (rettv_list_alloc(rettv) == FAIL)
5868 return;
5869#ifdef FEAT_GUI
5870 if (gui.in_use)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01005871 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005872# if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5873 else
5874# endif
5875#endif
5876#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5877 {
5878 varnumber_T timeout = 100;
5879
5880 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005881 timeout = tv_get_number(&argvars[0]);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005882 term_get_winpos(&x, &y, timeout);
5883 }
5884#endif
5885 list_append_number(rettv->vval.v_list, (varnumber_T)x);
5886 list_append_number(rettv->vval.v_list, (varnumber_T)y);
5887}
5888
5889
5890/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005891 * "getwinposx()" function
5892 */
5893 static void
5894f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5895{
5896 rettv->vval.v_number = -1;
5897#ifdef FEAT_GUI
5898 if (gui.in_use)
5899 {
5900 int x, y;
5901
5902 if (gui_mch_get_winpos(&x, &y) == OK)
5903 rettv->vval.v_number = x;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005904 return;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005905 }
5906#endif
5907#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5908 {
5909 int x, y;
5910
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005911 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005912 rettv->vval.v_number = x;
5913 }
5914#endif
5915}
5916
5917/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005918 * "getwinposy()" function
5919 */
5920 static void
5921f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5922{
5923 rettv->vval.v_number = -1;
5924#ifdef FEAT_GUI
5925 if (gui.in_use)
5926 {
5927 int x, y;
5928
5929 if (gui_mch_get_winpos(&x, &y) == OK)
5930 rettv->vval.v_number = y;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005931 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005932 }
5933#endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005934#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5935 {
5936 int x, y;
5937
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005938 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005939 rettv->vval.v_number = y;
5940 }
5941#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005942}
5943
5944/*
5945 * "getwinvar()" function
5946 */
5947 static void
5948f_getwinvar(typval_T *argvars, typval_T *rettv)
5949{
5950 getwinvar(argvars, rettv, 0);
5951}
5952
5953/*
5954 * "glob()" function
5955 */
5956 static void
5957f_glob(typval_T *argvars, typval_T *rettv)
5958{
5959 int options = WILD_SILENT|WILD_USE_NL;
5960 expand_T xpc;
5961 int error = FALSE;
5962
5963 /* When the optional second argument is non-zero, don't remove matches
5964 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5965 rettv->v_type = VAR_STRING;
5966 if (argvars[1].v_type != VAR_UNKNOWN)
5967 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005968 if (tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005969 options |= WILD_KEEP_ALL;
5970 if (argvars[2].v_type != VAR_UNKNOWN)
5971 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005972 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005973 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005974 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005975 }
5976 if (argvars[3].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005977 && tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005978 options |= WILD_ALLLINKS;
5979 }
5980 }
5981 if (!error)
5982 {
5983 ExpandInit(&xpc);
5984 xpc.xp_context = EXPAND_FILES;
5985 if (p_wic)
5986 options += WILD_ICASE;
5987 if (rettv->v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005988 rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005989 NULL, options, WILD_ALL);
5990 else if (rettv_list_alloc(rettv) != FAIL)
5991 {
5992 int i;
5993
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005994 ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005995 NULL, options, WILD_ALL_KEEP);
5996 for (i = 0; i < xpc.xp_numfiles; i++)
5997 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5998
5999 ExpandCleanup(&xpc);
6000 }
6001 }
6002 else
6003 rettv->vval.v_string = NULL;
6004}
6005
6006/*
6007 * "globpath()" function
6008 */
6009 static void
6010f_globpath(typval_T *argvars, typval_T *rettv)
6011{
6012 int flags = 0;
6013 char_u buf1[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006014 char_u *file = tv_get_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006015 int error = FALSE;
6016 garray_T ga;
6017 int i;
6018
6019 /* When the optional second argument is non-zero, don't remove matches
6020 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6021 rettv->v_type = VAR_STRING;
6022 if (argvars[2].v_type != VAR_UNKNOWN)
6023 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006024 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006025 flags |= WILD_KEEP_ALL;
6026 if (argvars[3].v_type != VAR_UNKNOWN)
6027 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006028 if (tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006029 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006030 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006031 }
6032 if (argvars[4].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006033 && tv_get_number_chk(&argvars[4], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006034 flags |= WILD_ALLLINKS;
6035 }
6036 }
6037 if (file != NULL && !error)
6038 {
6039 ga_init2(&ga, (int)sizeof(char_u *), 10);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006040 globpath(tv_get_string(&argvars[0]), file, &ga, flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006041 if (rettv->v_type == VAR_STRING)
6042 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
6043 else if (rettv_list_alloc(rettv) != FAIL)
6044 for (i = 0; i < ga.ga_len; ++i)
6045 list_append_string(rettv->vval.v_list,
6046 ((char_u **)(ga.ga_data))[i], -1);
6047 ga_clear_strings(&ga);
6048 }
6049 else
6050 rettv->vval.v_string = NULL;
6051}
6052
6053/*
6054 * "glob2regpat()" function
6055 */
6056 static void
6057f_glob2regpat(typval_T *argvars, typval_T *rettv)
6058{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006059 char_u *pat = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006060
6061 rettv->v_type = VAR_STRING;
6062 rettv->vval.v_string = (pat == NULL)
6063 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
6064}
6065
6066/* for VIM_VERSION_ defines */
6067#include "version.h"
6068
6069/*
6070 * "has()" function
6071 */
6072 static void
6073f_has(typval_T *argvars, typval_T *rettv)
6074{
6075 int i;
6076 char_u *name;
6077 int n = FALSE;
6078 static char *(has_list[]) =
6079 {
6080#ifdef AMIGA
6081 "amiga",
6082# ifdef FEAT_ARP
6083 "arp",
6084# endif
6085#endif
6086#ifdef __BEOS__
6087 "beos",
6088#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02006089#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01006090 "mac", /* Mac OS X (and, once, Mac OS Classic) */
6091 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02006092# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01006093 "macunix", /* Mac OS X, with the darwin feature */
6094 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02006095# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006096#endif
6097#ifdef __QNX__
6098 "qnx",
6099#endif
6100#ifdef UNIX
6101 "unix",
6102#endif
6103#ifdef VMS
6104 "vms",
6105#endif
6106#ifdef WIN32
6107 "win32",
6108#endif
6109#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
6110 "win32unix",
6111#endif
6112#if defined(WIN64) || defined(_WIN64)
6113 "win64",
6114#endif
6115#ifdef EBCDIC
6116 "ebcdic",
6117#endif
6118#ifndef CASE_INSENSITIVE_FILENAME
6119 "fname_case",
6120#endif
6121#ifdef HAVE_ACL
6122 "acl",
6123#endif
6124#ifdef FEAT_ARABIC
6125 "arabic",
6126#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006127 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006128#ifdef FEAT_AUTOCHDIR
6129 "autochdir",
6130#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006131#ifdef FEAT_AUTOSERVERNAME
6132 "autoservername",
6133#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006134#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006135 "balloon_eval",
6136# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
6137 "balloon_multiline",
6138# endif
6139#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006140#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006141 "balloon_eval_term",
6142#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006143#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6144 "builtin_terms",
6145# ifdef ALL_BUILTIN_TCAPS
6146 "all_builtin_terms",
6147# endif
6148#endif
6149#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
6150 || defined(FEAT_GUI_W32) \
6151 || defined(FEAT_GUI_MOTIF))
6152 "browsefilter",
6153#endif
6154#ifdef FEAT_BYTEOFF
6155 "byte_offset",
6156#endif
6157#ifdef FEAT_JOB_CHANNEL
6158 "channel",
6159#endif
6160#ifdef FEAT_CINDENT
6161 "cindent",
6162#endif
6163#ifdef FEAT_CLIENTSERVER
6164 "clientserver",
6165#endif
6166#ifdef FEAT_CLIPBOARD
6167 "clipboard",
6168#endif
6169#ifdef FEAT_CMDL_COMPL
6170 "cmdline_compl",
6171#endif
6172#ifdef FEAT_CMDHIST
6173 "cmdline_hist",
6174#endif
6175#ifdef FEAT_COMMENTS
6176 "comments",
6177#endif
6178#ifdef FEAT_CONCEAL
6179 "conceal",
6180#endif
6181#ifdef FEAT_CRYPT
6182 "cryptv",
6183 "crypt-blowfish",
6184 "crypt-blowfish2",
6185#endif
6186#ifdef FEAT_CSCOPE
6187 "cscope",
6188#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006189 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006190#ifdef CURSOR_SHAPE
6191 "cursorshape",
6192#endif
6193#ifdef DEBUG
6194 "debug",
6195#endif
6196#ifdef FEAT_CON_DIALOG
6197 "dialog_con",
6198#endif
6199#ifdef FEAT_GUI_DIALOG
6200 "dialog_gui",
6201#endif
6202#ifdef FEAT_DIFF
6203 "diff",
6204#endif
6205#ifdef FEAT_DIGRAPHS
6206 "digraphs",
6207#endif
6208#ifdef FEAT_DIRECTX
6209 "directx",
6210#endif
6211#ifdef FEAT_DND
6212 "dnd",
6213#endif
6214#ifdef FEAT_EMACS_TAGS
6215 "emacs_tags",
6216#endif
6217 "eval", /* always present, of course! */
6218 "ex_extra", /* graduated feature */
6219#ifdef FEAT_SEARCH_EXTRA
6220 "extra_search",
6221#endif
6222#ifdef FEAT_FKMAP
6223 "farsi",
6224#endif
6225#ifdef FEAT_SEARCHPATH
6226 "file_in_path",
6227#endif
6228#ifdef FEAT_FILTERPIPE
6229 "filterpipe",
6230#endif
6231#ifdef FEAT_FIND_ID
6232 "find_in_path",
6233#endif
6234#ifdef FEAT_FLOAT
6235 "float",
6236#endif
6237#ifdef FEAT_FOLDING
6238 "folding",
6239#endif
6240#ifdef FEAT_FOOTER
6241 "footer",
6242#endif
6243#if !defined(USE_SYSTEM) && defined(UNIX)
6244 "fork",
6245#endif
6246#ifdef FEAT_GETTEXT
6247 "gettext",
6248#endif
6249#ifdef FEAT_GUI
6250 "gui",
6251#endif
6252#ifdef FEAT_GUI_ATHENA
6253# ifdef FEAT_GUI_NEXTAW
6254 "gui_neXtaw",
6255# else
6256 "gui_athena",
6257# endif
6258#endif
6259#ifdef FEAT_GUI_GTK
6260 "gui_gtk",
6261# ifdef USE_GTK3
6262 "gui_gtk3",
6263# else
6264 "gui_gtk2",
6265# endif
6266#endif
6267#ifdef FEAT_GUI_GNOME
6268 "gui_gnome",
6269#endif
6270#ifdef FEAT_GUI_MAC
6271 "gui_mac",
6272#endif
6273#ifdef FEAT_GUI_MOTIF
6274 "gui_motif",
6275#endif
6276#ifdef FEAT_GUI_PHOTON
6277 "gui_photon",
6278#endif
6279#ifdef FEAT_GUI_W32
6280 "gui_win32",
6281#endif
6282#ifdef FEAT_HANGULIN
6283 "hangul_input",
6284#endif
6285#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6286 "iconv",
6287#endif
6288#ifdef FEAT_INS_EXPAND
6289 "insert_expand",
6290#endif
6291#ifdef FEAT_JOB_CHANNEL
6292 "job",
6293#endif
6294#ifdef FEAT_JUMPLIST
6295 "jumplist",
6296#endif
6297#ifdef FEAT_KEYMAP
6298 "keymap",
6299#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006300 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006301#ifdef FEAT_LANGMAP
6302 "langmap",
6303#endif
6304#ifdef FEAT_LIBCALL
6305 "libcall",
6306#endif
6307#ifdef FEAT_LINEBREAK
6308 "linebreak",
6309#endif
6310#ifdef FEAT_LISP
6311 "lispindent",
6312#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006313 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006314#ifdef FEAT_LOCALMAP
6315 "localmap",
6316#endif
6317#ifdef FEAT_LUA
6318# ifndef DYNAMIC_LUA
6319 "lua",
6320# endif
6321#endif
6322#ifdef FEAT_MENU
6323 "menu",
6324#endif
6325#ifdef FEAT_SESSION
6326 "mksession",
6327#endif
6328#ifdef FEAT_MODIFY_FNAME
6329 "modify_fname",
6330#endif
6331#ifdef FEAT_MOUSE
6332 "mouse",
6333#endif
6334#ifdef FEAT_MOUSESHAPE
6335 "mouseshape",
6336#endif
6337#if defined(UNIX) || defined(VMS)
6338# ifdef FEAT_MOUSE_DEC
6339 "mouse_dec",
6340# endif
6341# ifdef FEAT_MOUSE_GPM
6342 "mouse_gpm",
6343# endif
6344# ifdef FEAT_MOUSE_JSB
6345 "mouse_jsbterm",
6346# endif
6347# ifdef FEAT_MOUSE_NET
6348 "mouse_netterm",
6349# endif
6350# ifdef FEAT_MOUSE_PTERM
6351 "mouse_pterm",
6352# endif
6353# ifdef FEAT_MOUSE_SGR
6354 "mouse_sgr",
6355# endif
6356# ifdef FEAT_SYSMOUSE
6357 "mouse_sysmouse",
6358# endif
6359# ifdef FEAT_MOUSE_URXVT
6360 "mouse_urxvt",
6361# endif
6362# ifdef FEAT_MOUSE_XTERM
6363 "mouse_xterm",
6364# endif
6365#endif
6366#ifdef FEAT_MBYTE
6367 "multi_byte",
6368#endif
6369#ifdef FEAT_MBYTE_IME
6370 "multi_byte_ime",
6371#endif
6372#ifdef FEAT_MULTI_LANG
6373 "multi_lang",
6374#endif
6375#ifdef FEAT_MZSCHEME
6376#ifndef DYNAMIC_MZSCHEME
6377 "mzscheme",
6378#endif
6379#endif
6380#ifdef FEAT_NUM64
6381 "num64",
6382#endif
6383#ifdef FEAT_OLE
6384 "ole",
6385#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006386#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006387 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006388#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006389#ifdef FEAT_PATH_EXTRA
6390 "path_extra",
6391#endif
6392#ifdef FEAT_PERL
6393#ifndef DYNAMIC_PERL
6394 "perl",
6395#endif
6396#endif
6397#ifdef FEAT_PERSISTENT_UNDO
6398 "persistent_undo",
6399#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006400#if defined(FEAT_PYTHON)
6401 "python_compiled",
6402# if defined(DYNAMIC_PYTHON)
6403 "python_dynamic",
6404# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006405 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006406 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006407# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006408#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006409#if defined(FEAT_PYTHON3)
6410 "python3_compiled",
6411# if defined(DYNAMIC_PYTHON3)
6412 "python3_dynamic",
6413# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006414 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006415 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006416# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006417#endif
6418#ifdef FEAT_POSTSCRIPT
6419 "postscript",
6420#endif
6421#ifdef FEAT_PRINTER
6422 "printer",
6423#endif
6424#ifdef FEAT_PROFILE
6425 "profile",
6426#endif
6427#ifdef FEAT_RELTIME
6428 "reltime",
6429#endif
6430#ifdef FEAT_QUICKFIX
6431 "quickfix",
6432#endif
6433#ifdef FEAT_RIGHTLEFT
6434 "rightleft",
6435#endif
6436#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6437 "ruby",
6438#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006439 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006440#ifdef FEAT_CMDL_INFO
6441 "showcmd",
6442 "cmdline_info",
6443#endif
6444#ifdef FEAT_SIGNS
6445 "signs",
6446#endif
6447#ifdef FEAT_SMARTINDENT
6448 "smartindent",
6449#endif
6450#ifdef STARTUPTIME
6451 "startuptime",
6452#endif
6453#ifdef FEAT_STL_OPT
6454 "statusline",
6455#endif
6456#ifdef FEAT_SUN_WORKSHOP
6457 "sun_workshop",
6458#endif
6459#ifdef FEAT_NETBEANS_INTG
6460 "netbeans_intg",
6461#endif
6462#ifdef FEAT_SPELL
6463 "spell",
6464#endif
6465#ifdef FEAT_SYN_HL
6466 "syntax",
6467#endif
6468#if defined(USE_SYSTEM) || !defined(UNIX)
6469 "system",
6470#endif
6471#ifdef FEAT_TAG_BINS
6472 "tag_binary",
6473#endif
6474#ifdef FEAT_TAG_OLDSTATIC
6475 "tag_old_static",
6476#endif
6477#ifdef FEAT_TAG_ANYWHITE
6478 "tag_any_white",
6479#endif
6480#ifdef FEAT_TCL
6481# ifndef DYNAMIC_TCL
6482 "tcl",
6483# endif
6484#endif
6485#ifdef FEAT_TERMGUICOLORS
6486 "termguicolors",
6487#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006488#if defined(FEAT_TERMINAL) && !defined(WIN3264)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006489 "terminal",
6490#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006491#ifdef TERMINFO
6492 "terminfo",
6493#endif
6494#ifdef FEAT_TERMRESPONSE
6495 "termresponse",
6496#endif
6497#ifdef FEAT_TEXTOBJ
6498 "textobjects",
6499#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006500#ifdef FEAT_TEXT_PROP
6501 "textprop",
6502#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006503#ifdef HAVE_TGETENT
6504 "tgetent",
6505#endif
6506#ifdef FEAT_TIMERS
6507 "timers",
6508#endif
6509#ifdef FEAT_TITLE
6510 "title",
6511#endif
6512#ifdef FEAT_TOOLBAR
6513 "toolbar",
6514#endif
6515#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6516 "unnamedplus",
6517#endif
6518#ifdef FEAT_USR_CMDS
6519 "user-commands", /* was accidentally included in 5.4 */
6520 "user_commands",
6521#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006522#ifdef FEAT_VARTABS
6523 "vartabs",
6524#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006525#ifdef FEAT_VIMINFO
6526 "viminfo",
6527#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006528 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006529#ifdef FEAT_VIRTUALEDIT
6530 "virtualedit",
6531#endif
6532 "visual",
6533#ifdef FEAT_VISUALEXTRA
6534 "visualextra",
6535#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006536 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006537#ifdef FEAT_VTP
6538 "vtp",
6539#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006540#ifdef FEAT_WILDIGN
6541 "wildignore",
6542#endif
6543#ifdef FEAT_WILDMENU
6544 "wildmenu",
6545#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006546 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006547#ifdef FEAT_WAK
6548 "winaltkeys",
6549#endif
6550#ifdef FEAT_WRITEBACKUP
6551 "writebackup",
6552#endif
6553#ifdef FEAT_XIM
6554 "xim",
6555#endif
6556#ifdef FEAT_XFONTSET
6557 "xfontset",
6558#endif
6559#ifdef FEAT_XPM_W32
6560 "xpm",
6561 "xpm_w32", /* for backward compatibility */
6562#else
6563# if defined(HAVE_XPM)
6564 "xpm",
6565# endif
6566#endif
6567#ifdef USE_XSMP
6568 "xsmp",
6569#endif
6570#ifdef USE_XSMP_INTERACT
6571 "xsmp_interact",
6572#endif
6573#ifdef FEAT_XCLIPBOARD
6574 "xterm_clipboard",
6575#endif
6576#ifdef FEAT_XTERM_SAVE
6577 "xterm_save",
6578#endif
6579#if defined(UNIX) && defined(FEAT_X11)
6580 "X11",
6581#endif
6582 NULL
6583 };
6584
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006585 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006586 for (i = 0; has_list[i] != NULL; ++i)
6587 if (STRICMP(name, has_list[i]) == 0)
6588 {
6589 n = TRUE;
6590 break;
6591 }
6592
6593 if (n == FALSE)
6594 {
6595 if (STRNICMP(name, "patch", 5) == 0)
6596 {
6597 if (name[5] == '-'
6598 && STRLEN(name) >= 11
6599 && vim_isdigit(name[6])
6600 && vim_isdigit(name[8])
6601 && vim_isdigit(name[10]))
6602 {
6603 int major = atoi((char *)name + 6);
6604 int minor = atoi((char *)name + 8);
6605
6606 /* Expect "patch-9.9.01234". */
6607 n = (major < VIM_VERSION_MAJOR
6608 || (major == VIM_VERSION_MAJOR
6609 && (minor < VIM_VERSION_MINOR
6610 || (minor == VIM_VERSION_MINOR
6611 && has_patch(atoi((char *)name + 10))))));
6612 }
6613 else
6614 n = has_patch(atoi((char *)name + 5));
6615 }
6616 else if (STRICMP(name, "vim_starting") == 0)
6617 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006618 else if (STRICMP(name, "ttyin") == 0)
6619 n = mch_input_isatty();
6620 else if (STRICMP(name, "ttyout") == 0)
6621 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006622#ifdef FEAT_MBYTE
6623 else if (STRICMP(name, "multi_byte_encoding") == 0)
6624 n = has_mbyte;
6625#endif
6626#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
6627 else if (STRICMP(name, "balloon_multiline") == 0)
6628 n = multiline_balloon_available();
6629#endif
6630#ifdef DYNAMIC_TCL
6631 else if (STRICMP(name, "tcl") == 0)
6632 n = tcl_enabled(FALSE);
6633#endif
6634#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6635 else if (STRICMP(name, "iconv") == 0)
6636 n = iconv_enabled(FALSE);
6637#endif
6638#ifdef DYNAMIC_LUA
6639 else if (STRICMP(name, "lua") == 0)
6640 n = lua_enabled(FALSE);
6641#endif
6642#ifdef DYNAMIC_MZSCHEME
6643 else if (STRICMP(name, "mzscheme") == 0)
6644 n = mzscheme_enabled(FALSE);
6645#endif
6646#ifdef DYNAMIC_RUBY
6647 else if (STRICMP(name, "ruby") == 0)
6648 n = ruby_enabled(FALSE);
6649#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006650#ifdef DYNAMIC_PYTHON
6651 else if (STRICMP(name, "python") == 0)
6652 n = python_enabled(FALSE);
6653#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006654#ifdef DYNAMIC_PYTHON3
6655 else if (STRICMP(name, "python3") == 0)
6656 n = python3_enabled(FALSE);
6657#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006658#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6659 else if (STRICMP(name, "pythonx") == 0)
6660 {
6661# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6662 if (p_pyx == 0)
6663 n = python3_enabled(FALSE) || python_enabled(FALSE);
6664 else if (p_pyx == 3)
6665 n = python3_enabled(FALSE);
6666 else if (p_pyx == 2)
6667 n = python_enabled(FALSE);
6668# elif defined(DYNAMIC_PYTHON)
6669 n = python_enabled(FALSE);
6670# elif defined(DYNAMIC_PYTHON3)
6671 n = python3_enabled(FALSE);
6672# endif
6673 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006674#endif
6675#ifdef DYNAMIC_PERL
6676 else if (STRICMP(name, "perl") == 0)
6677 n = perl_enabled(FALSE);
6678#endif
6679#ifdef FEAT_GUI
6680 else if (STRICMP(name, "gui_running") == 0)
6681 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006682# ifdef FEAT_BROWSE
6683 else if (STRICMP(name, "browse") == 0)
6684 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6685# endif
6686#endif
6687#ifdef FEAT_SYN_HL
6688 else if (STRICMP(name, "syntax_items") == 0)
6689 n = syntax_present(curwin);
6690#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006691#ifdef FEAT_VTP
6692 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02006693 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006694#endif
6695#ifdef FEAT_NETBEANS_INTG
6696 else if (STRICMP(name, "netbeans_enabled") == 0)
6697 n = netbeans_active();
6698#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006699#if defined(FEAT_TERMINAL) && defined(WIN3264)
6700 else if (STRICMP(name, "terminal") == 0)
6701 n = terminal_enabled();
6702#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006703 }
6704
6705 rettv->vval.v_number = n;
6706}
6707
6708/*
6709 * "has_key()" function
6710 */
6711 static void
6712f_has_key(typval_T *argvars, typval_T *rettv)
6713{
6714 if (argvars[0].v_type != VAR_DICT)
6715 {
6716 EMSG(_(e_dictreq));
6717 return;
6718 }
6719 if (argvars[0].vval.v_dict == NULL)
6720 return;
6721
6722 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006723 tv_get_string(&argvars[1]), -1) != NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006724}
6725
6726/*
6727 * "haslocaldir()" function
6728 */
6729 static void
6730f_haslocaldir(typval_T *argvars, typval_T *rettv)
6731{
6732 win_T *wp = NULL;
6733
6734 wp = find_tabwin(&argvars[0], &argvars[1]);
6735 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6736}
6737
6738/*
6739 * "hasmapto()" function
6740 */
6741 static void
6742f_hasmapto(typval_T *argvars, typval_T *rettv)
6743{
6744 char_u *name;
6745 char_u *mode;
6746 char_u buf[NUMBUFLEN];
6747 int abbr = FALSE;
6748
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006749 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006750 if (argvars[1].v_type == VAR_UNKNOWN)
6751 mode = (char_u *)"nvo";
6752 else
6753 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006754 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006755 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006756 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006757 }
6758
6759 if (map_to_exists(name, mode, abbr))
6760 rettv->vval.v_number = TRUE;
6761 else
6762 rettv->vval.v_number = FALSE;
6763}
6764
6765/*
6766 * "histadd()" function
6767 */
6768 static void
6769f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6770{
6771#ifdef FEAT_CMDHIST
6772 int histype;
6773 char_u *str;
6774 char_u buf[NUMBUFLEN];
6775#endif
6776
6777 rettv->vval.v_number = FALSE;
6778 if (check_restricted() || check_secure())
6779 return;
6780#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006781 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006782 histype = str != NULL ? get_histtype(str) : -1;
6783 if (histype >= 0)
6784 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006785 str = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006786 if (*str != NUL)
6787 {
6788 init_history();
6789 add_to_history(histype, str, FALSE, NUL);
6790 rettv->vval.v_number = TRUE;
6791 return;
6792 }
6793 }
6794#endif
6795}
6796
6797/*
6798 * "histdel()" function
6799 */
6800 static void
6801f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6802{
6803#ifdef FEAT_CMDHIST
6804 int n;
6805 char_u buf[NUMBUFLEN];
6806 char_u *str;
6807
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006808 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006809 if (str == NULL)
6810 n = 0;
6811 else if (argvars[1].v_type == VAR_UNKNOWN)
6812 /* only one argument: clear entire history */
6813 n = clr_history(get_histtype(str));
6814 else if (argvars[1].v_type == VAR_NUMBER)
6815 /* index given: remove that entry */
6816 n = del_history_idx(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006817 (int)tv_get_number(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006818 else
6819 /* string given: remove all matching entries */
6820 n = del_history_entry(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006821 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006822 rettv->vval.v_number = n;
6823#endif
6824}
6825
6826/*
6827 * "histget()" function
6828 */
6829 static void
6830f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6831{
6832#ifdef FEAT_CMDHIST
6833 int type;
6834 int idx;
6835 char_u *str;
6836
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006837 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006838 if (str == NULL)
6839 rettv->vval.v_string = NULL;
6840 else
6841 {
6842 type = get_histtype(str);
6843 if (argvars[1].v_type == VAR_UNKNOWN)
6844 idx = get_history_idx(type);
6845 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006846 idx = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006847 /* -1 on type error */
6848 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6849 }
6850#else
6851 rettv->vval.v_string = NULL;
6852#endif
6853 rettv->v_type = VAR_STRING;
6854}
6855
6856/*
6857 * "histnr()" function
6858 */
6859 static void
6860f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6861{
6862 int i;
6863
6864#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006865 char_u *history = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006866
6867 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6868 if (i >= HIST_CMD && i < HIST_COUNT)
6869 i = get_history_idx(i);
6870 else
6871#endif
6872 i = -1;
6873 rettv->vval.v_number = i;
6874}
6875
6876/*
6877 * "highlightID(name)" function
6878 */
6879 static void
6880f_hlID(typval_T *argvars, typval_T *rettv)
6881{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006882 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006883}
6884
6885/*
6886 * "highlight_exists()" function
6887 */
6888 static void
6889f_hlexists(typval_T *argvars, typval_T *rettv)
6890{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006891 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006892}
6893
6894/*
6895 * "hostname()" function
6896 */
6897 static void
6898f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6899{
6900 char_u hostname[256];
6901
6902 mch_get_host_name(hostname, 256);
6903 rettv->v_type = VAR_STRING;
6904 rettv->vval.v_string = vim_strsave(hostname);
6905}
6906
6907/*
6908 * iconv() function
6909 */
6910 static void
6911f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6912{
6913#ifdef FEAT_MBYTE
6914 char_u buf1[NUMBUFLEN];
6915 char_u buf2[NUMBUFLEN];
6916 char_u *from, *to, *str;
6917 vimconv_T vimconv;
6918#endif
6919
6920 rettv->v_type = VAR_STRING;
6921 rettv->vval.v_string = NULL;
6922
6923#ifdef FEAT_MBYTE
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006924 str = tv_get_string(&argvars[0]);
6925 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
6926 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006927 vimconv.vc_type = CONV_NONE;
6928 convert_setup(&vimconv, from, to);
6929
6930 /* If the encodings are equal, no conversion needed. */
6931 if (vimconv.vc_type == CONV_NONE)
6932 rettv->vval.v_string = vim_strsave(str);
6933 else
6934 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6935
6936 convert_setup(&vimconv, NULL, NULL);
6937 vim_free(from);
6938 vim_free(to);
6939#endif
6940}
6941
6942/*
6943 * "indent()" function
6944 */
6945 static void
6946f_indent(typval_T *argvars, typval_T *rettv)
6947{
6948 linenr_T lnum;
6949
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006950 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006951 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6952 rettv->vval.v_number = get_indent_lnum(lnum);
6953 else
6954 rettv->vval.v_number = -1;
6955}
6956
6957/*
6958 * "index()" function
6959 */
6960 static void
6961f_index(typval_T *argvars, typval_T *rettv)
6962{
6963 list_T *l;
6964 listitem_T *item;
6965 long idx = 0;
6966 int ic = FALSE;
6967
6968 rettv->vval.v_number = -1;
6969 if (argvars[0].v_type != VAR_LIST)
6970 {
6971 EMSG(_(e_listreq));
6972 return;
6973 }
6974 l = argvars[0].vval.v_list;
6975 if (l != NULL)
6976 {
6977 item = l->lv_first;
6978 if (argvars[2].v_type != VAR_UNKNOWN)
6979 {
6980 int error = FALSE;
6981
6982 /* Start at specified item. Use the cached index that list_find()
6983 * sets, so that a negative number also works. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006984 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006985 idx = l->lv_idx;
6986 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006987 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006988 if (error)
6989 item = NULL;
6990 }
6991
6992 for ( ; item != NULL; item = item->li_next, ++idx)
6993 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6994 {
6995 rettv->vval.v_number = idx;
6996 break;
6997 }
6998 }
6999}
7000
7001static int inputsecret_flag = 0;
7002
7003/*
7004 * "input()" function
7005 * Also handles inputsecret() when inputsecret is set.
7006 */
7007 static void
7008f_input(typval_T *argvars, typval_T *rettv)
7009{
7010 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
7011}
7012
7013/*
7014 * "inputdialog()" function
7015 */
7016 static void
7017f_inputdialog(typval_T *argvars, typval_T *rettv)
7018{
7019#if defined(FEAT_GUI_TEXTDIALOG)
7020 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
7021 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
7022 {
7023 char_u *message;
7024 char_u buf[NUMBUFLEN];
7025 char_u *defstr = (char_u *)"";
7026
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007027 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007028 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007029 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007030 vim_strncpy(IObuff, defstr, IOSIZE - 1);
7031 else
7032 IObuff[0] = NUL;
7033 if (message != NULL && defstr != NULL
7034 && do_dialog(VIM_QUESTION, NULL, message,
7035 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
7036 rettv->vval.v_string = vim_strsave(IObuff);
7037 else
7038 {
7039 if (message != NULL && defstr != NULL
7040 && argvars[1].v_type != VAR_UNKNOWN
7041 && argvars[2].v_type != VAR_UNKNOWN)
7042 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007043 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007044 else
7045 rettv->vval.v_string = NULL;
7046 }
7047 rettv->v_type = VAR_STRING;
7048 }
7049 else
7050#endif
7051 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
7052}
7053
7054/*
7055 * "inputlist()" function
7056 */
7057 static void
7058f_inputlist(typval_T *argvars, typval_T *rettv)
7059{
7060 listitem_T *li;
7061 int selected;
7062 int mouse_used;
7063
7064#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007065 /* While starting up, there is no place to enter text. When running tests
7066 * with --not-a-term we assume feedkeys() will be used. */
7067 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007068 return;
7069#endif
7070 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
7071 {
7072 EMSG2(_(e_listarg), "inputlist()");
7073 return;
7074 }
7075
7076 msg_start();
7077 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
7078 lines_left = Rows; /* avoid more prompt */
7079 msg_scroll = TRUE;
7080 msg_clr_eos();
7081
7082 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
7083 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007084 msg_puts(tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007085 msg_putchar('\n');
7086 }
7087
7088 /* Ask for choice. */
7089 selected = prompt_for_number(&mouse_used);
7090 if (mouse_used)
7091 selected -= lines_left;
7092
7093 rettv->vval.v_number = selected;
7094}
7095
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007096static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7097
7098/*
7099 * "inputrestore()" function
7100 */
7101 static void
7102f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7103{
7104 if (ga_userinput.ga_len > 0)
7105 {
7106 --ga_userinput.ga_len;
7107 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7108 + ga_userinput.ga_len);
7109 /* default return is zero == OK */
7110 }
7111 else if (p_verbose > 1)
7112 {
7113 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
7114 rettv->vval.v_number = 1; /* Failed */
7115 }
7116}
7117
7118/*
7119 * "inputsave()" function
7120 */
7121 static void
7122f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7123{
7124 /* Add an entry to the stack of typeahead storage. */
7125 if (ga_grow(&ga_userinput, 1) == OK)
7126 {
7127 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7128 + ga_userinput.ga_len);
7129 ++ga_userinput.ga_len;
7130 /* default return is zero == OK */
7131 }
7132 else
7133 rettv->vval.v_number = 1; /* Failed */
7134}
7135
7136/*
7137 * "inputsecret()" function
7138 */
7139 static void
7140f_inputsecret(typval_T *argvars, typval_T *rettv)
7141{
7142 ++cmdline_star;
7143 ++inputsecret_flag;
7144 f_input(argvars, rettv);
7145 --cmdline_star;
7146 --inputsecret_flag;
7147}
7148
7149/*
7150 * "insert()" function
7151 */
7152 static void
7153f_insert(typval_T *argvars, typval_T *rettv)
7154{
7155 long before = 0;
7156 listitem_T *item;
7157 list_T *l;
7158 int error = FALSE;
7159
7160 if (argvars[0].v_type != VAR_LIST)
7161 EMSG2(_(e_listarg), "insert()");
7162 else if ((l = argvars[0].vval.v_list) != NULL
7163 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
7164 {
7165 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007166 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007167 if (error)
7168 return; /* type error; errmsg already given */
7169
7170 if (before == l->lv_len)
7171 item = NULL;
7172 else
7173 {
7174 item = list_find(l, before);
7175 if (item == NULL)
7176 {
7177 EMSGN(_(e_listidx), before);
7178 l = NULL;
7179 }
7180 }
7181 if (l != NULL)
7182 {
7183 list_insert_tv(l, &argvars[1], item);
7184 copy_tv(&argvars[0], rettv);
7185 }
7186 }
7187}
7188
7189/*
7190 * "invert(expr)" function
7191 */
7192 static void
7193f_invert(typval_T *argvars, typval_T *rettv)
7194{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007195 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007196}
7197
7198/*
7199 * "isdirectory()" function
7200 */
7201 static void
7202f_isdirectory(typval_T *argvars, typval_T *rettv)
7203{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007204 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007205}
7206
7207/*
7208 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7209 * or it refers to a List or Dictionary that is locked.
7210 */
7211 static int
7212tv_islocked(typval_T *tv)
7213{
7214 return (tv->v_lock & VAR_LOCKED)
7215 || (tv->v_type == VAR_LIST
7216 && tv->vval.v_list != NULL
7217 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7218 || (tv->v_type == VAR_DICT
7219 && tv->vval.v_dict != NULL
7220 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7221}
7222
7223/*
7224 * "islocked()" function
7225 */
7226 static void
7227f_islocked(typval_T *argvars, typval_T *rettv)
7228{
7229 lval_T lv;
7230 char_u *end;
7231 dictitem_T *di;
7232
7233 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007234 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007235 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007236 if (end != NULL && lv.ll_name != NULL)
7237 {
7238 if (*end != NUL)
7239 EMSG(_(e_trailing));
7240 else
7241 {
7242 if (lv.ll_tv == NULL)
7243 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007244 di = find_var(lv.ll_name, NULL, TRUE);
7245 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007246 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007247 /* Consider a variable locked when:
7248 * 1. the variable itself is locked
7249 * 2. the value of the variable is locked.
7250 * 3. the List or Dict value is locked.
7251 */
7252 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7253 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007254 }
7255 }
7256 else if (lv.ll_range)
7257 EMSG(_("E786: Range not allowed"));
7258 else if (lv.ll_newkey != NULL)
7259 EMSG2(_(e_dictkey), lv.ll_newkey);
7260 else if (lv.ll_list != NULL)
7261 /* List item. */
7262 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7263 else
7264 /* Dictionary item. */
7265 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7266 }
7267 }
7268
7269 clear_lval(&lv);
7270}
7271
7272#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7273/*
7274 * "isnan()" function
7275 */
7276 static void
7277f_isnan(typval_T *argvars, typval_T *rettv)
7278{
7279 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7280 && isnan(argvars[0].vval.v_float);
7281}
7282#endif
7283
7284/*
7285 * "items(dict)" function
7286 */
7287 static void
7288f_items(typval_T *argvars, typval_T *rettv)
7289{
7290 dict_list(argvars, rettv, 2);
7291}
7292
7293#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
7294/*
7295 * Get the job from the argument.
7296 * Returns NULL if the job is invalid.
7297 */
7298 static job_T *
7299get_job_arg(typval_T *tv)
7300{
7301 job_T *job;
7302
7303 if (tv->v_type != VAR_JOB)
7304 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007305 EMSG2(_(e_invarg2), tv_get_string(tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007306 return NULL;
7307 }
7308 job = tv->vval.v_job;
7309
7310 if (job == NULL)
7311 EMSG(_("E916: not a valid job"));
7312 return job;
7313}
7314
7315/*
7316 * "job_getchannel()" function
7317 */
7318 static void
7319f_job_getchannel(typval_T *argvars, typval_T *rettv)
7320{
7321 job_T *job = get_job_arg(&argvars[0]);
7322
7323 if (job != NULL)
7324 {
7325 rettv->v_type = VAR_CHANNEL;
7326 rettv->vval.v_channel = job->jv_channel;
7327 if (job->jv_channel != NULL)
7328 ++job->jv_channel->ch_refcount;
7329 }
7330}
7331
7332/*
7333 * "job_info()" function
7334 */
7335 static void
7336f_job_info(typval_T *argvars, typval_T *rettv)
7337{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007338 if (argvars[0].v_type != VAR_UNKNOWN)
7339 {
7340 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007341
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007342 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7343 job_info(job, rettv->vval.v_dict);
7344 }
7345 else if (rettv_list_alloc(rettv) == OK)
7346 job_info_all(rettv->vval.v_list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007347}
7348
7349/*
7350 * "job_setoptions()" function
7351 */
7352 static void
7353f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7354{
7355 job_T *job = get_job_arg(&argvars[0]);
7356 jobopt_T opt;
7357
7358 if (job == NULL)
7359 return;
7360 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007361 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007362 job_set_options(job, &opt);
7363 free_job_options(&opt);
7364}
7365
7366/*
7367 * "job_start()" function
7368 */
7369 static void
7370f_job_start(typval_T *argvars, typval_T *rettv)
7371{
7372 rettv->v_type = VAR_JOB;
7373 if (check_restricted() || check_secure())
7374 return;
Bram Moolenaar493359e2018-06-12 20:25:52 +02007375 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007376}
7377
7378/*
7379 * "job_status()" function
7380 */
7381 static void
7382f_job_status(typval_T *argvars, typval_T *rettv)
7383{
7384 job_T *job = get_job_arg(&argvars[0]);
7385
7386 if (job != NULL)
7387 {
7388 rettv->v_type = VAR_STRING;
7389 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7390 }
7391}
7392
7393/*
7394 * "job_stop()" function
7395 */
7396 static void
7397f_job_stop(typval_T *argvars, typval_T *rettv)
7398{
7399 job_T *job = get_job_arg(&argvars[0]);
7400
7401 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007402 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007403}
7404#endif
7405
7406/*
7407 * "join()" function
7408 */
7409 static void
7410f_join(typval_T *argvars, typval_T *rettv)
7411{
7412 garray_T ga;
7413 char_u *sep;
7414
7415 if (argvars[0].v_type != VAR_LIST)
7416 {
7417 EMSG(_(e_listreq));
7418 return;
7419 }
7420 if (argvars[0].vval.v_list == NULL)
7421 return;
7422 if (argvars[1].v_type == VAR_UNKNOWN)
7423 sep = (char_u *)" ";
7424 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007425 sep = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007426
7427 rettv->v_type = VAR_STRING;
7428
7429 if (sep != NULL)
7430 {
7431 ga_init2(&ga, (int)sizeof(char), 80);
7432 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7433 ga_append(&ga, NUL);
7434 rettv->vval.v_string = (char_u *)ga.ga_data;
7435 }
7436 else
7437 rettv->vval.v_string = NULL;
7438}
7439
7440/*
7441 * "js_decode()" function
7442 */
7443 static void
7444f_js_decode(typval_T *argvars, typval_T *rettv)
7445{
7446 js_read_T reader;
7447
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007448 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007449 reader.js_fill = NULL;
7450 reader.js_used = 0;
7451 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
7452 EMSG(_(e_invarg));
7453}
7454
7455/*
7456 * "js_encode()" function
7457 */
7458 static void
7459f_js_encode(typval_T *argvars, typval_T *rettv)
7460{
7461 rettv->v_type = VAR_STRING;
7462 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7463}
7464
7465/*
7466 * "json_decode()" function
7467 */
7468 static void
7469f_json_decode(typval_T *argvars, typval_T *rettv)
7470{
7471 js_read_T reader;
7472
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007473 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007474 reader.js_fill = NULL;
7475 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007476 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007477}
7478
7479/*
7480 * "json_encode()" function
7481 */
7482 static void
7483f_json_encode(typval_T *argvars, typval_T *rettv)
7484{
7485 rettv->v_type = VAR_STRING;
7486 rettv->vval.v_string = json_encode(&argvars[0], 0);
7487}
7488
7489/*
7490 * "keys()" function
7491 */
7492 static void
7493f_keys(typval_T *argvars, typval_T *rettv)
7494{
7495 dict_list(argvars, rettv, 0);
7496}
7497
7498/*
7499 * "last_buffer_nr()" function.
7500 */
7501 static void
7502f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7503{
7504 int n = 0;
7505 buf_T *buf;
7506
Bram Moolenaar29323592016-07-24 22:04:11 +02007507 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007508 if (n < buf->b_fnum)
7509 n = buf->b_fnum;
7510
7511 rettv->vval.v_number = n;
7512}
7513
7514/*
7515 * "len()" function
7516 */
7517 static void
7518f_len(typval_T *argvars, typval_T *rettv)
7519{
7520 switch (argvars[0].v_type)
7521 {
7522 case VAR_STRING:
7523 case VAR_NUMBER:
7524 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007525 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007526 break;
7527 case VAR_LIST:
7528 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7529 break;
7530 case VAR_DICT:
7531 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7532 break;
7533 case VAR_UNKNOWN:
7534 case VAR_SPECIAL:
7535 case VAR_FLOAT:
7536 case VAR_FUNC:
7537 case VAR_PARTIAL:
7538 case VAR_JOB:
7539 case VAR_CHANNEL:
7540 EMSG(_("E701: Invalid type for len()"));
7541 break;
7542 }
7543}
7544
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007545 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007546libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007547{
7548#ifdef FEAT_LIBCALL
7549 char_u *string_in;
7550 char_u **string_result;
7551 int nr_result;
7552#endif
7553
7554 rettv->v_type = type;
7555 if (type != VAR_NUMBER)
7556 rettv->vval.v_string = NULL;
7557
7558 if (check_restricted() || check_secure())
7559 return;
7560
7561#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007562 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007563 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7564 {
7565 string_in = NULL;
7566 if (argvars[2].v_type == VAR_STRING)
7567 string_in = argvars[2].vval.v_string;
7568 if (type == VAR_NUMBER)
7569 string_result = NULL;
7570 else
7571 string_result = &rettv->vval.v_string;
7572 if (mch_libcall(argvars[0].vval.v_string,
7573 argvars[1].vval.v_string,
7574 string_in,
7575 argvars[2].vval.v_number,
7576 string_result,
7577 &nr_result) == OK
7578 && type == VAR_NUMBER)
7579 rettv->vval.v_number = nr_result;
7580 }
7581#endif
7582}
7583
7584/*
7585 * "libcall()" function
7586 */
7587 static void
7588f_libcall(typval_T *argvars, typval_T *rettv)
7589{
7590 libcall_common(argvars, rettv, VAR_STRING);
7591}
7592
7593/*
7594 * "libcallnr()" function
7595 */
7596 static void
7597f_libcallnr(typval_T *argvars, typval_T *rettv)
7598{
7599 libcall_common(argvars, rettv, VAR_NUMBER);
7600}
7601
7602/*
7603 * "line(string)" function
7604 */
7605 static void
7606f_line(typval_T *argvars, typval_T *rettv)
7607{
7608 linenr_T lnum = 0;
7609 pos_T *fp;
7610 int fnum;
7611
7612 fp = var2fpos(&argvars[0], TRUE, &fnum);
7613 if (fp != NULL)
7614 lnum = fp->lnum;
7615 rettv->vval.v_number = lnum;
7616}
7617
7618/*
7619 * "line2byte(lnum)" function
7620 */
7621 static void
7622f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7623{
7624#ifndef FEAT_BYTEOFF
7625 rettv->vval.v_number = -1;
7626#else
7627 linenr_T lnum;
7628
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007629 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007630 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7631 rettv->vval.v_number = -1;
7632 else
7633 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7634 if (rettv->vval.v_number >= 0)
7635 ++rettv->vval.v_number;
7636#endif
7637}
7638
7639/*
7640 * "lispindent(lnum)" function
7641 */
7642 static void
7643f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7644{
7645#ifdef FEAT_LISP
7646 pos_T pos;
7647 linenr_T lnum;
7648
7649 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007650 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007651 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7652 {
7653 curwin->w_cursor.lnum = lnum;
7654 rettv->vval.v_number = get_lisp_indent();
7655 curwin->w_cursor = pos;
7656 }
7657 else
7658#endif
7659 rettv->vval.v_number = -1;
7660}
7661
7662/*
7663 * "localtime()" function
7664 */
7665 static void
7666f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7667{
7668 rettv->vval.v_number = (varnumber_T)time(NULL);
7669}
7670
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007671 static void
7672get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7673{
7674 char_u *keys;
7675 char_u *which;
7676 char_u buf[NUMBUFLEN];
7677 char_u *keys_buf = NULL;
7678 char_u *rhs;
7679 int mode;
7680 int abbr = FALSE;
7681 int get_dict = FALSE;
7682 mapblock_T *mp;
7683 int buffer_local;
7684
7685 /* return empty string for failure */
7686 rettv->v_type = VAR_STRING;
7687 rettv->vval.v_string = NULL;
7688
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007689 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007690 if (*keys == NUL)
7691 return;
7692
7693 if (argvars[1].v_type != VAR_UNKNOWN)
7694 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007695 which = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007696 if (argvars[2].v_type != VAR_UNKNOWN)
7697 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007698 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007699 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007700 get_dict = (int)tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007701 }
7702 }
7703 else
7704 which = (char_u *)"";
7705 if (which == NULL)
7706 return;
7707
7708 mode = get_map_mode(&which, 0);
7709
7710 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7711 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7712 vim_free(keys_buf);
7713
7714 if (!get_dict)
7715 {
7716 /* Return a string. */
7717 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02007718 {
7719 if (*rhs == NUL)
7720 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
7721 else
7722 rettv->vval.v_string = str2special_save(rhs, FALSE);
7723 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007724
7725 }
7726 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7727 {
7728 /* Return a dictionary. */
7729 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7730 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7731 dict_T *dict = rettv->vval.v_dict;
7732
Bram Moolenaare0be1672018-07-08 16:50:37 +02007733 dict_add_string(dict, "lhs", lhs);
7734 dict_add_string(dict, "rhs", mp->m_orig_str);
7735 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
7736 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
7737 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007738 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
7739 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02007740 dict_add_number(dict, "buffer", (long)buffer_local);
7741 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
7742 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007743
7744 vim_free(lhs);
7745 vim_free(mapmode);
7746 }
7747}
7748
7749#ifdef FEAT_FLOAT
7750/*
7751 * "log()" function
7752 */
7753 static void
7754f_log(typval_T *argvars, typval_T *rettv)
7755{
7756 float_T f = 0.0;
7757
7758 rettv->v_type = VAR_FLOAT;
7759 if (get_float_arg(argvars, &f) == OK)
7760 rettv->vval.v_float = log(f);
7761 else
7762 rettv->vval.v_float = 0.0;
7763}
7764
7765/*
7766 * "log10()" function
7767 */
7768 static void
7769f_log10(typval_T *argvars, typval_T *rettv)
7770{
7771 float_T f = 0.0;
7772
7773 rettv->v_type = VAR_FLOAT;
7774 if (get_float_arg(argvars, &f) == OK)
7775 rettv->vval.v_float = log10(f);
7776 else
7777 rettv->vval.v_float = 0.0;
7778}
7779#endif
7780
7781#ifdef FEAT_LUA
7782/*
7783 * "luaeval()" function
7784 */
7785 static void
7786f_luaeval(typval_T *argvars, typval_T *rettv)
7787{
7788 char_u *str;
7789 char_u buf[NUMBUFLEN];
7790
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007791 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007792 do_luaeval(str, argvars + 1, rettv);
7793}
7794#endif
7795
7796/*
7797 * "map()" function
7798 */
7799 static void
7800f_map(typval_T *argvars, typval_T *rettv)
7801{
7802 filter_map(argvars, rettv, TRUE);
7803}
7804
7805/*
7806 * "maparg()" function
7807 */
7808 static void
7809f_maparg(typval_T *argvars, typval_T *rettv)
7810{
7811 get_maparg(argvars, rettv, TRUE);
7812}
7813
7814/*
7815 * "mapcheck()" function
7816 */
7817 static void
7818f_mapcheck(typval_T *argvars, typval_T *rettv)
7819{
7820 get_maparg(argvars, rettv, FALSE);
7821}
7822
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007823typedef enum
7824{
7825 MATCH_END, /* matchend() */
7826 MATCH_MATCH, /* match() */
7827 MATCH_STR, /* matchstr() */
7828 MATCH_LIST, /* matchlist() */
7829 MATCH_POS /* matchstrpos() */
7830} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007831
7832 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007833find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007834{
7835 char_u *str = NULL;
7836 long len = 0;
7837 char_u *expr = NULL;
7838 char_u *pat;
7839 regmatch_T regmatch;
7840 char_u patbuf[NUMBUFLEN];
7841 char_u strbuf[NUMBUFLEN];
7842 char_u *save_cpo;
7843 long start = 0;
7844 long nth = 1;
7845 colnr_T startcol = 0;
7846 int match = 0;
7847 list_T *l = NULL;
7848 listitem_T *li = NULL;
7849 long idx = 0;
7850 char_u *tofree = NULL;
7851
7852 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7853 save_cpo = p_cpo;
7854 p_cpo = (char_u *)"";
7855
7856 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007857 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007858 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007859 /* type MATCH_LIST: return empty list when there are no matches.
7860 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007861 if (rettv_list_alloc(rettv) == FAIL)
7862 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007863 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007864 && (list_append_string(rettv->vval.v_list,
7865 (char_u *)"", 0) == FAIL
7866 || list_append_number(rettv->vval.v_list,
7867 (varnumber_T)-1) == FAIL
7868 || list_append_number(rettv->vval.v_list,
7869 (varnumber_T)-1) == FAIL
7870 || list_append_number(rettv->vval.v_list,
7871 (varnumber_T)-1) == FAIL))
7872 {
7873 list_free(rettv->vval.v_list);
7874 rettv->vval.v_list = NULL;
7875 goto theend;
7876 }
7877 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007878 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007879 {
7880 rettv->v_type = VAR_STRING;
7881 rettv->vval.v_string = NULL;
7882 }
7883
7884 if (argvars[0].v_type == VAR_LIST)
7885 {
7886 if ((l = argvars[0].vval.v_list) == NULL)
7887 goto theend;
7888 li = l->lv_first;
7889 }
7890 else
7891 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007892 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007893 len = (long)STRLEN(str);
7894 }
7895
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007896 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007897 if (pat == NULL)
7898 goto theend;
7899
7900 if (argvars[2].v_type != VAR_UNKNOWN)
7901 {
7902 int error = FALSE;
7903
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007904 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007905 if (error)
7906 goto theend;
7907 if (l != NULL)
7908 {
7909 li = list_find(l, start);
7910 if (li == NULL)
7911 goto theend;
7912 idx = l->lv_idx; /* use the cached index */
7913 }
7914 else
7915 {
7916 if (start < 0)
7917 start = 0;
7918 if (start > len)
7919 goto theend;
7920 /* When "count" argument is there ignore matches before "start",
7921 * otherwise skip part of the string. Differs when pattern is "^"
7922 * or "\<". */
7923 if (argvars[3].v_type != VAR_UNKNOWN)
7924 startcol = start;
7925 else
7926 {
7927 str += start;
7928 len -= start;
7929 }
7930 }
7931
7932 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007933 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007934 if (error)
7935 goto theend;
7936 }
7937
7938 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7939 if (regmatch.regprog != NULL)
7940 {
7941 regmatch.rm_ic = p_ic;
7942
7943 for (;;)
7944 {
7945 if (l != NULL)
7946 {
7947 if (li == NULL)
7948 {
7949 match = FALSE;
7950 break;
7951 }
7952 vim_free(tofree);
7953 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
7954 if (str == NULL)
7955 break;
7956 }
7957
7958 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
7959
7960 if (match && --nth <= 0)
7961 break;
7962 if (l == NULL && !match)
7963 break;
7964
7965 /* Advance to just after the match. */
7966 if (l != NULL)
7967 {
7968 li = li->li_next;
7969 ++idx;
7970 }
7971 else
7972 {
7973#ifdef FEAT_MBYTE
7974 startcol = (colnr_T)(regmatch.startp[0]
7975 + (*mb_ptr2len)(regmatch.startp[0]) - str);
7976#else
7977 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
7978#endif
7979 if (startcol > (colnr_T)len
7980 || str + startcol <= regmatch.startp[0])
7981 {
7982 match = FALSE;
7983 break;
7984 }
7985 }
7986 }
7987
7988 if (match)
7989 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007990 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007991 {
7992 listitem_T *li1 = rettv->vval.v_list->lv_first;
7993 listitem_T *li2 = li1->li_next;
7994 listitem_T *li3 = li2->li_next;
7995 listitem_T *li4 = li3->li_next;
7996
7997 vim_free(li1->li_tv.vval.v_string);
7998 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
7999 (int)(regmatch.endp[0] - regmatch.startp[0]));
8000 li3->li_tv.vval.v_number =
8001 (varnumber_T)(regmatch.startp[0] - expr);
8002 li4->li_tv.vval.v_number =
8003 (varnumber_T)(regmatch.endp[0] - expr);
8004 if (l != NULL)
8005 li2->li_tv.vval.v_number = (varnumber_T)idx;
8006 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008007 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008008 {
8009 int i;
8010
8011 /* return list with matched string and submatches */
8012 for (i = 0; i < NSUBEXP; ++i)
8013 {
8014 if (regmatch.endp[i] == NULL)
8015 {
8016 if (list_append_string(rettv->vval.v_list,
8017 (char_u *)"", 0) == FAIL)
8018 break;
8019 }
8020 else if (list_append_string(rettv->vval.v_list,
8021 regmatch.startp[i],
8022 (int)(regmatch.endp[i] - regmatch.startp[i]))
8023 == FAIL)
8024 break;
8025 }
8026 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008027 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008028 {
8029 /* return matched string */
8030 if (l != NULL)
8031 copy_tv(&li->li_tv, rettv);
8032 else
8033 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
8034 (int)(regmatch.endp[0] - regmatch.startp[0]));
8035 }
8036 else if (l != NULL)
8037 rettv->vval.v_number = idx;
8038 else
8039 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008040 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008041 rettv->vval.v_number =
8042 (varnumber_T)(regmatch.startp[0] - str);
8043 else
8044 rettv->vval.v_number =
8045 (varnumber_T)(regmatch.endp[0] - str);
8046 rettv->vval.v_number += (varnumber_T)(str - expr);
8047 }
8048 }
8049 vim_regfree(regmatch.regprog);
8050 }
8051
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008052theend:
8053 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008054 /* matchstrpos() without a list: drop the second item. */
8055 listitem_remove(rettv->vval.v_list,
8056 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008057 vim_free(tofree);
8058 p_cpo = save_cpo;
8059}
8060
8061/*
8062 * "match()" function
8063 */
8064 static void
8065f_match(typval_T *argvars, typval_T *rettv)
8066{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008067 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008068}
8069
Bram Moolenaar95e51472018-07-28 16:55:56 +02008070#ifdef FEAT_SEARCH_EXTRA
8071 static int
8072matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
8073{
8074 dictitem_T *di;
8075
8076 if (tv->v_type != VAR_DICT)
8077 {
8078 EMSG(_(e_dictreq));
8079 return FAIL;
8080 }
8081
8082 if (dict_find(tv->vval.v_dict, (char_u *)"conceal", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01008083 *conceal_char = dict_get_string(tv->vval.v_dict,
Bram Moolenaar95e51472018-07-28 16:55:56 +02008084 (char_u *)"conceal", FALSE);
8085
8086 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
8087 {
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02008088 *win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008089 if (*win == NULL)
8090 {
8091 EMSG(_("E957: Invalid window number"));
8092 return FAIL;
8093 }
8094 }
8095
8096 return OK;
8097}
8098#endif
8099
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008100/*
8101 * "matchadd()" function
8102 */
8103 static void
8104f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8105{
8106#ifdef FEAT_SEARCH_EXTRA
8107 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008108 char_u *grp = tv_get_string_buf_chk(&argvars[0], buf); /* group */
8109 char_u *pat = tv_get_string_buf_chk(&argvars[1], buf); /* pattern */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008110 int prio = 10; /* default priority */
8111 int id = -1;
8112 int error = FALSE;
8113 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008114 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008115
8116 rettv->vval.v_number = -1;
8117
8118 if (grp == NULL || pat == NULL)
8119 return;
8120 if (argvars[2].v_type != VAR_UNKNOWN)
8121 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008122 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008123 if (argvars[3].v_type != VAR_UNKNOWN)
8124 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008125 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008126 if (argvars[4].v_type != VAR_UNKNOWN
8127 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8128 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008129 }
8130 }
8131 if (error == TRUE)
8132 return;
8133 if (id >= 1 && id <= 3)
8134 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02008135 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008136 return;
8137 }
8138
Bram Moolenaar95e51472018-07-28 16:55:56 +02008139 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008140 conceal_char);
8141#endif
8142}
8143
8144/*
8145 * "matchaddpos()" function
8146 */
8147 static void
8148f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8149{
8150#ifdef FEAT_SEARCH_EXTRA
8151 char_u buf[NUMBUFLEN];
8152 char_u *group;
8153 int prio = 10;
8154 int id = -1;
8155 int error = FALSE;
8156 list_T *l;
8157 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008158 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008159
8160 rettv->vval.v_number = -1;
8161
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008162 group = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008163 if (group == NULL)
8164 return;
8165
8166 if (argvars[1].v_type != VAR_LIST)
8167 {
8168 EMSG2(_(e_listarg), "matchaddpos()");
8169 return;
8170 }
8171 l = argvars[1].vval.v_list;
8172 if (l == NULL)
8173 return;
8174
8175 if (argvars[2].v_type != VAR_UNKNOWN)
8176 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008177 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008178 if (argvars[3].v_type != VAR_UNKNOWN)
8179 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008180 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008181
8182 if (argvars[4].v_type != VAR_UNKNOWN
8183 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8184 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008185 }
8186 }
8187 if (error == TRUE)
8188 return;
8189
8190 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8191 if (id == 1 || id == 2)
8192 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02008193 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008194 return;
8195 }
8196
Bram Moolenaar95e51472018-07-28 16:55:56 +02008197 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008198 conceal_char);
8199#endif
8200}
8201
8202/*
8203 * "matcharg()" function
8204 */
8205 static void
8206f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8207{
8208 if (rettv_list_alloc(rettv) == OK)
8209 {
8210#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008211 int id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008212 matchitem_T *m;
8213
8214 if (id >= 1 && id <= 3)
8215 {
8216 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8217 {
8218 list_append_string(rettv->vval.v_list,
8219 syn_id2name(m->hlg_id), -1);
8220 list_append_string(rettv->vval.v_list, m->pattern, -1);
8221 }
8222 else
8223 {
8224 list_append_string(rettv->vval.v_list, NULL, -1);
8225 list_append_string(rettv->vval.v_list, NULL, -1);
8226 }
8227 }
8228#endif
8229 }
8230}
8231
8232/*
8233 * "matchdelete()" function
8234 */
8235 static void
8236f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8237{
8238#ifdef FEAT_SEARCH_EXTRA
8239 rettv->vval.v_number = match_delete(curwin,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008240 (int)tv_get_number(&argvars[0]), TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008241#endif
8242}
8243
8244/*
8245 * "matchend()" function
8246 */
8247 static void
8248f_matchend(typval_T *argvars, typval_T *rettv)
8249{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008250 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008251}
8252
8253/*
8254 * "matchlist()" function
8255 */
8256 static void
8257f_matchlist(typval_T *argvars, typval_T *rettv)
8258{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008259 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008260}
8261
8262/*
8263 * "matchstr()" function
8264 */
8265 static void
8266f_matchstr(typval_T *argvars, typval_T *rettv)
8267{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008268 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008269}
8270
8271/*
8272 * "matchstrpos()" function
8273 */
8274 static void
8275f_matchstrpos(typval_T *argvars, typval_T *rettv)
8276{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008277 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008278}
8279
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008280 static void
8281max_min(typval_T *argvars, typval_T *rettv, int domax)
8282{
8283 varnumber_T n = 0;
8284 varnumber_T i;
8285 int error = FALSE;
8286
8287 if (argvars[0].v_type == VAR_LIST)
8288 {
8289 list_T *l;
8290 listitem_T *li;
8291
8292 l = argvars[0].vval.v_list;
8293 if (l != NULL)
8294 {
8295 li = l->lv_first;
8296 if (li != NULL)
8297 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008298 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008299 for (;;)
8300 {
8301 li = li->li_next;
8302 if (li == NULL)
8303 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008304 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008305 if (domax ? i > n : i < n)
8306 n = i;
8307 }
8308 }
8309 }
8310 }
8311 else if (argvars[0].v_type == VAR_DICT)
8312 {
8313 dict_T *d;
8314 int first = TRUE;
8315 hashitem_T *hi;
8316 int todo;
8317
8318 d = argvars[0].vval.v_dict;
8319 if (d != NULL)
8320 {
8321 todo = (int)d->dv_hashtab.ht_used;
8322 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8323 {
8324 if (!HASHITEM_EMPTY(hi))
8325 {
8326 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008327 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008328 if (first)
8329 {
8330 n = i;
8331 first = FALSE;
8332 }
8333 else if (domax ? i > n : i < n)
8334 n = i;
8335 }
8336 }
8337 }
8338 }
8339 else
Bram Moolenaar26b84332016-09-04 21:42:36 +02008340 EMSG2(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008341 rettv->vval.v_number = error ? 0 : n;
8342}
8343
8344/*
8345 * "max()" function
8346 */
8347 static void
8348f_max(typval_T *argvars, typval_T *rettv)
8349{
8350 max_min(argvars, rettv, TRUE);
8351}
8352
8353/*
8354 * "min()" function
8355 */
8356 static void
8357f_min(typval_T *argvars, typval_T *rettv)
8358{
8359 max_min(argvars, rettv, FALSE);
8360}
8361
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008362/*
8363 * Create the directory in which "dir" is located, and higher levels when
8364 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008365 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008366 */
8367 static int
8368mkdir_recurse(char_u *dir, int prot)
8369{
8370 char_u *p;
8371 char_u *updir;
8372 int r = FAIL;
8373
8374 /* Get end of directory name in "dir".
8375 * We're done when it's "/" or "c:/". */
8376 p = gettail_sep(dir);
8377 if (p <= get_past_head(dir))
8378 return OK;
8379
8380 /* If the directory exists we're done. Otherwise: create it.*/
8381 updir = vim_strnsave(dir, (int)(p - dir));
8382 if (updir == NULL)
8383 return FAIL;
8384 if (mch_isdir(updir))
8385 r = OK;
8386 else if (mkdir_recurse(updir, prot) == OK)
8387 r = vim_mkdir_emsg(updir, prot);
8388 vim_free(updir);
8389 return r;
8390}
8391
8392#ifdef vim_mkdir
8393/*
8394 * "mkdir()" function
8395 */
8396 static void
8397f_mkdir(typval_T *argvars, typval_T *rettv)
8398{
8399 char_u *dir;
8400 char_u buf[NUMBUFLEN];
8401 int prot = 0755;
8402
8403 rettv->vval.v_number = FAIL;
8404 if (check_restricted() || check_secure())
8405 return;
8406
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008407 dir = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008408 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008409 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008410
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008411 if (*gettail(dir) == NUL)
8412 /* remove trailing slashes */
8413 *gettail_sep(dir) = NUL;
8414
8415 if (argvars[1].v_type != VAR_UNKNOWN)
8416 {
8417 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008418 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008419 prot = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008420 if (prot == -1)
8421 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008422 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008423 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008424 {
8425 if (mch_isdir(dir))
8426 {
8427 /* With the "p" flag it's OK if the dir already exists. */
8428 rettv->vval.v_number = OK;
8429 return;
8430 }
8431 mkdir_recurse(dir, prot);
8432 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008433 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008434 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008435}
8436#endif
8437
8438/*
8439 * "mode()" function
8440 */
8441 static void
8442f_mode(typval_T *argvars, typval_T *rettv)
8443{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008444 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008445
Bram Moolenaar612cc382018-07-29 15:34:26 +02008446 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008447
8448 if (time_for_testing == 93784)
8449 {
8450 /* Testing the two-character code. */
8451 buf[0] = 'x';
8452 buf[1] = '!';
8453 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008454#ifdef FEAT_TERMINAL
8455 else if (term_use_loop())
8456 buf[0] = 't';
8457#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008458 else if (VIsual_active)
8459 {
8460 if (VIsual_select)
8461 buf[0] = VIsual_mode + 's' - 'v';
8462 else
8463 buf[0] = VIsual_mode;
8464 }
8465 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8466 || State == CONFIRM)
8467 {
8468 buf[0] = 'r';
8469 if (State == ASKMORE)
8470 buf[1] = 'm';
8471 else if (State == CONFIRM)
8472 buf[1] = '?';
8473 }
8474 else if (State == EXTERNCMD)
8475 buf[0] = '!';
8476 else if (State & INSERT)
8477 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008478 if (State & VREPLACE_FLAG)
8479 {
8480 buf[0] = 'R';
8481 buf[1] = 'v';
8482 }
8483 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008484 {
8485 if (State & REPLACE_FLAG)
8486 buf[0] = 'R';
8487 else
8488 buf[0] = 'i';
8489#ifdef FEAT_INS_EXPAND
8490 if (ins_compl_active())
8491 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008492 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008493 buf[1] = 'x';
8494#endif
8495 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008496 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008497 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008498 {
8499 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008500 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008501 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008502 else if (exmode_active == EXMODE_NORMAL)
8503 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008504 }
8505 else
8506 {
8507 buf[0] = 'n';
8508 if (finish_op)
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008509 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008510 buf[1] = 'o';
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008511 // to be able to detect force-linewise/blockwise/characterwise operations
8512 buf[2] = motion_force;
8513 }
Bram Moolenaar612cc382018-07-29 15:34:26 +02008514 else if (restart_edit == 'I' || restart_edit == 'R'
8515 || restart_edit == 'V')
8516 {
8517 buf[1] = 'i';
8518 buf[2] = restart_edit;
8519 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008520 }
8521
8522 /* Clear out the minor mode when the argument is not a non-zero number or
8523 * non-empty string. */
8524 if (!non_zero_arg(&argvars[0]))
8525 buf[1] = NUL;
8526
8527 rettv->vval.v_string = vim_strsave(buf);
8528 rettv->v_type = VAR_STRING;
8529}
8530
8531#if defined(FEAT_MZSCHEME) || defined(PROTO)
8532/*
8533 * "mzeval()" function
8534 */
8535 static void
8536f_mzeval(typval_T *argvars, typval_T *rettv)
8537{
8538 char_u *str;
8539 char_u buf[NUMBUFLEN];
8540
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008541 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008542 do_mzeval(str, rettv);
8543}
8544
8545 void
8546mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8547{
8548 typval_T argvars[3];
8549
8550 argvars[0].v_type = VAR_STRING;
8551 argvars[0].vval.v_string = name;
8552 copy_tv(args, &argvars[1]);
8553 argvars[2].v_type = VAR_UNKNOWN;
8554 f_call(argvars, rettv);
8555 clear_tv(&argvars[1]);
8556}
8557#endif
8558
8559/*
8560 * "nextnonblank()" function
8561 */
8562 static void
8563f_nextnonblank(typval_T *argvars, typval_T *rettv)
8564{
8565 linenr_T lnum;
8566
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008567 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008568 {
8569 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8570 {
8571 lnum = 0;
8572 break;
8573 }
8574 if (*skipwhite(ml_get(lnum)) != NUL)
8575 break;
8576 }
8577 rettv->vval.v_number = lnum;
8578}
8579
8580/*
8581 * "nr2char()" function
8582 */
8583 static void
8584f_nr2char(typval_T *argvars, typval_T *rettv)
8585{
8586 char_u buf[NUMBUFLEN];
8587
8588#ifdef FEAT_MBYTE
8589 if (has_mbyte)
8590 {
8591 int utf8 = 0;
8592
8593 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008594 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008595 if (utf8)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008596 buf[(*utf_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008597 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008598 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008599 }
8600 else
8601#endif
8602 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008603 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008604 buf[1] = NUL;
8605 }
8606 rettv->v_type = VAR_STRING;
8607 rettv->vval.v_string = vim_strsave(buf);
8608}
8609
8610/*
8611 * "or(expr, expr)" function
8612 */
8613 static void
8614f_or(typval_T *argvars, typval_T *rettv)
8615{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008616 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
8617 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008618}
8619
8620/*
8621 * "pathshorten()" function
8622 */
8623 static void
8624f_pathshorten(typval_T *argvars, typval_T *rettv)
8625{
8626 char_u *p;
8627
8628 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008629 p = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008630 if (p == NULL)
8631 rettv->vval.v_string = NULL;
8632 else
8633 {
8634 p = vim_strsave(p);
8635 rettv->vval.v_string = p;
8636 if (p != NULL)
8637 shorten_dir(p);
8638 }
8639}
8640
8641#ifdef FEAT_PERL
8642/*
8643 * "perleval()" function
8644 */
8645 static void
8646f_perleval(typval_T *argvars, typval_T *rettv)
8647{
8648 char_u *str;
8649 char_u buf[NUMBUFLEN];
8650
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008651 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008652 do_perleval(str, rettv);
8653}
8654#endif
8655
8656#ifdef FEAT_FLOAT
8657/*
8658 * "pow()" function
8659 */
8660 static void
8661f_pow(typval_T *argvars, typval_T *rettv)
8662{
8663 float_T fx = 0.0, fy = 0.0;
8664
8665 rettv->v_type = VAR_FLOAT;
8666 if (get_float_arg(argvars, &fx) == OK
8667 && get_float_arg(&argvars[1], &fy) == OK)
8668 rettv->vval.v_float = pow(fx, fy);
8669 else
8670 rettv->vval.v_float = 0.0;
8671}
8672#endif
8673
8674/*
8675 * "prevnonblank()" function
8676 */
8677 static void
8678f_prevnonblank(typval_T *argvars, typval_T *rettv)
8679{
8680 linenr_T lnum;
8681
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008682 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008683 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8684 lnum = 0;
8685 else
8686 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8687 --lnum;
8688 rettv->vval.v_number = lnum;
8689}
8690
8691/* This dummy va_list is here because:
8692 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8693 * - locally in the function results in a "used before set" warning
8694 * - using va_start() to initialize it gives "function with fixed args" error */
8695static va_list ap;
8696
8697/*
8698 * "printf()" function
8699 */
8700 static void
8701f_printf(typval_T *argvars, typval_T *rettv)
8702{
8703 char_u buf[NUMBUFLEN];
8704 int len;
8705 char_u *s;
8706 int saved_did_emsg = did_emsg;
8707 char *fmt;
8708
8709 rettv->v_type = VAR_STRING;
8710 rettv->vval.v_string = NULL;
8711
8712 /* Get the required length, allocate the buffer and do it for real. */
8713 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008714 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008715 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008716 if (!did_emsg)
8717 {
8718 s = alloc(len + 1);
8719 if (s != NULL)
8720 {
8721 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008722 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8723 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008724 }
8725 }
8726 did_emsg |= saved_did_emsg;
8727}
8728
Bram Moolenaarf2732452018-06-03 14:47:35 +02008729#ifdef FEAT_JOB_CHANNEL
8730/*
8731 * "prompt_setcallback({buffer}, {callback})" function
8732 */
8733 static void
8734f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
8735{
8736 buf_T *buf;
8737 char_u *callback;
8738 partial_T *partial;
8739
8740 if (check_secure())
8741 return;
8742 buf = get_buf_tv(&argvars[0], FALSE);
8743 if (buf == NULL)
8744 return;
8745
8746 callback = get_callback(&argvars[1], &partial);
8747 if (callback == NULL)
8748 return;
8749
8750 free_callback(buf->b_prompt_callback, buf->b_prompt_partial);
8751 if (partial == NULL)
8752 buf->b_prompt_callback = vim_strsave(callback);
8753 else
8754 /* pointer into the partial */
8755 buf->b_prompt_callback = callback;
8756 buf->b_prompt_partial = partial;
8757}
8758
8759/*
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02008760 * "prompt_setinterrupt({buffer}, {callback})" function
8761 */
8762 static void
8763f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
8764{
8765 buf_T *buf;
8766 char_u *callback;
8767 partial_T *partial;
8768
8769 if (check_secure())
8770 return;
8771 buf = get_buf_tv(&argvars[0], FALSE);
8772 if (buf == NULL)
8773 return;
8774
8775 callback = get_callback(&argvars[1], &partial);
8776 if (callback == NULL)
8777 return;
8778
8779 free_callback(buf->b_prompt_interrupt, buf->b_prompt_int_partial);
8780 if (partial == NULL)
8781 buf->b_prompt_interrupt = vim_strsave(callback);
8782 else
8783 /* pointer into the partial */
8784 buf->b_prompt_interrupt = callback;
8785 buf->b_prompt_int_partial = partial;
8786}
8787
8788/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02008789 * "prompt_setprompt({buffer}, {text})" function
8790 */
8791 static void
8792f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
8793{
8794 buf_T *buf;
8795 char_u *text;
8796
8797 if (check_secure())
8798 return;
8799 buf = get_buf_tv(&argvars[0], FALSE);
8800 if (buf == NULL)
8801 return;
8802
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008803 text = tv_get_string(&argvars[1]);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008804 vim_free(buf->b_prompt_text);
8805 buf->b_prompt_text = vim_strsave(text);
8806}
8807#endif
8808
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008809/*
8810 * "pumvisible()" function
8811 */
8812 static void
8813f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8814{
8815#ifdef FEAT_INS_EXPAND
8816 if (pum_visible())
8817 rettv->vval.v_number = 1;
8818#endif
8819}
8820
8821#ifdef FEAT_PYTHON3
8822/*
8823 * "py3eval()" function
8824 */
8825 static void
8826f_py3eval(typval_T *argvars, typval_T *rettv)
8827{
8828 char_u *str;
8829 char_u buf[NUMBUFLEN];
8830
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008831 if (p_pyx == 0)
8832 p_pyx = 3;
8833
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008834 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008835 do_py3eval(str, rettv);
8836}
8837#endif
8838
8839#ifdef FEAT_PYTHON
8840/*
8841 * "pyeval()" function
8842 */
8843 static void
8844f_pyeval(typval_T *argvars, typval_T *rettv)
8845{
8846 char_u *str;
8847 char_u buf[NUMBUFLEN];
8848
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008849 if (p_pyx == 0)
8850 p_pyx = 2;
8851
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008852 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008853 do_pyeval(str, rettv);
8854}
8855#endif
8856
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008857#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8858/*
8859 * "pyxeval()" function
8860 */
8861 static void
8862f_pyxeval(typval_T *argvars, typval_T *rettv)
8863{
8864# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8865 init_pyxversion();
8866 if (p_pyx == 2)
8867 f_pyeval(argvars, rettv);
8868 else
8869 f_py3eval(argvars, rettv);
8870# elif defined(FEAT_PYTHON)
8871 f_pyeval(argvars, rettv);
8872# elif defined(FEAT_PYTHON3)
8873 f_py3eval(argvars, rettv);
8874# endif
8875}
8876#endif
8877
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008878/*
8879 * "range()" function
8880 */
8881 static void
8882f_range(typval_T *argvars, typval_T *rettv)
8883{
8884 varnumber_T start;
8885 varnumber_T end;
8886 varnumber_T stride = 1;
8887 varnumber_T i;
8888 int error = FALSE;
8889
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008890 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008891 if (argvars[1].v_type == VAR_UNKNOWN)
8892 {
8893 end = start - 1;
8894 start = 0;
8895 }
8896 else
8897 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008898 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008899 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008900 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008901 }
8902
8903 if (error)
8904 return; /* type error; errmsg already given */
8905 if (stride == 0)
8906 EMSG(_("E726: Stride is zero"));
8907 else if (stride > 0 ? end + 1 < start : end - 1 > start)
8908 EMSG(_("E727: Start past end"));
8909 else
8910 {
8911 if (rettv_list_alloc(rettv) == OK)
8912 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8913 if (list_append_number(rettv->vval.v_list,
8914 (varnumber_T)i) == FAIL)
8915 break;
8916 }
8917}
8918
8919/*
8920 * "readfile()" function
8921 */
8922 static void
8923f_readfile(typval_T *argvars, typval_T *rettv)
8924{
8925 int binary = FALSE;
8926 int failed = FALSE;
8927 char_u *fname;
8928 FILE *fd;
8929 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
8930 int io_size = sizeof(buf);
8931 int readlen; /* size of last fread() */
8932 char_u *prev = NULL; /* previously read bytes, if any */
8933 long prevlen = 0; /* length of data in prev */
8934 long prevsize = 0; /* size of prev buffer */
8935 long maxline = MAXLNUM;
8936 long cnt = 0;
8937 char_u *p; /* position in buf */
8938 char_u *start; /* start of current line */
8939
8940 if (argvars[1].v_type != VAR_UNKNOWN)
8941 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008942 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008943 binary = TRUE;
8944 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008945 maxline = (long)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008946 }
8947
8948 if (rettv_list_alloc(rettv) == FAIL)
8949 return;
8950
8951 /* Always open the file in binary mode, library functions have a mind of
8952 * their own about CR-LF conversion. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008953 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008954 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
8955 {
8956 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
8957 return;
8958 }
8959
8960 while (cnt < maxline || maxline < 0)
8961 {
8962 readlen = (int)fread(buf, 1, io_size, fd);
8963
8964 /* This for loop processes what was read, but is also entered at end
8965 * of file so that either:
8966 * - an incomplete line gets written
8967 * - a "binary" file gets an empty line at the end if it ends in a
8968 * newline. */
8969 for (p = buf, start = buf;
8970 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
8971 ++p)
8972 {
8973 if (*p == '\n' || readlen <= 0)
8974 {
8975 listitem_T *li;
8976 char_u *s = NULL;
8977 long_u len = p - start;
8978
8979 /* Finished a line. Remove CRs before NL. */
8980 if (readlen > 0 && !binary)
8981 {
8982 while (len > 0 && start[len - 1] == '\r')
8983 --len;
8984 /* removal may cross back to the "prev" string */
8985 if (len == 0)
8986 while (prevlen > 0 && prev[prevlen - 1] == '\r')
8987 --prevlen;
8988 }
8989 if (prevlen == 0)
8990 s = vim_strnsave(start, (int)len);
8991 else
8992 {
8993 /* Change "prev" buffer to be the right size. This way
8994 * the bytes are only copied once, and very long lines are
8995 * allocated only once. */
8996 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
8997 {
8998 mch_memmove(s + prevlen, start, len);
8999 s[prevlen + len] = NUL;
9000 prev = NULL; /* the list will own the string */
9001 prevlen = prevsize = 0;
9002 }
9003 }
9004 if (s == NULL)
9005 {
9006 do_outofmem_msg((long_u) prevlen + len + 1);
9007 failed = TRUE;
9008 break;
9009 }
9010
9011 if ((li = listitem_alloc()) == NULL)
9012 {
9013 vim_free(s);
9014 failed = TRUE;
9015 break;
9016 }
9017 li->li_tv.v_type = VAR_STRING;
9018 li->li_tv.v_lock = 0;
9019 li->li_tv.vval.v_string = s;
9020 list_append(rettv->vval.v_list, li);
9021
9022 start = p + 1; /* step over newline */
9023 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
9024 break;
9025 }
9026 else if (*p == NUL)
9027 *p = '\n';
9028#ifdef FEAT_MBYTE
9029 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
9030 * when finding the BF and check the previous two bytes. */
9031 else if (*p == 0xbf && enc_utf8 && !binary)
9032 {
9033 /* Find the two bytes before the 0xbf. If p is at buf, or buf
9034 * + 1, these may be in the "prev" string. */
9035 char_u back1 = p >= buf + 1 ? p[-1]
9036 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
9037 char_u back2 = p >= buf + 2 ? p[-2]
9038 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
9039 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
9040
9041 if (back2 == 0xef && back1 == 0xbb)
9042 {
9043 char_u *dest = p - 2;
9044
9045 /* Usually a BOM is at the beginning of a file, and so at
9046 * the beginning of a line; then we can just step over it.
9047 */
9048 if (start == dest)
9049 start = p + 1;
9050 else
9051 {
9052 /* have to shuffle buf to close gap */
9053 int adjust_prevlen = 0;
9054
9055 if (dest < buf)
9056 {
9057 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
9058 dest = buf;
9059 }
9060 if (readlen > p - buf + 1)
9061 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
9062 readlen -= 3 - adjust_prevlen;
9063 prevlen -= adjust_prevlen;
9064 p = dest - 1;
9065 }
9066 }
9067 }
9068#endif
9069 } /* for */
9070
9071 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
9072 break;
9073 if (start < p)
9074 {
9075 /* There's part of a line in buf, store it in "prev". */
9076 if (p - start + prevlen >= prevsize)
9077 {
9078 /* need bigger "prev" buffer */
9079 char_u *newprev;
9080
9081 /* A common use case is ordinary text files and "prev" gets a
9082 * fragment of a line, so the first allocation is made
9083 * small, to avoid repeatedly 'allocing' large and
9084 * 'reallocing' small. */
9085 if (prevsize == 0)
9086 prevsize = (long)(p - start);
9087 else
9088 {
9089 long grow50pc = (prevsize * 3) / 2;
9090 long growmin = (long)((p - start) * 2 + prevlen);
9091 prevsize = grow50pc > growmin ? grow50pc : growmin;
9092 }
9093 newprev = prev == NULL ? alloc(prevsize)
9094 : vim_realloc(prev, prevsize);
9095 if (newprev == NULL)
9096 {
9097 do_outofmem_msg((long_u)prevsize);
9098 failed = TRUE;
9099 break;
9100 }
9101 prev = newprev;
9102 }
9103 /* Add the line part to end of "prev". */
9104 mch_memmove(prev + prevlen, start, p - start);
9105 prevlen += (long)(p - start);
9106 }
9107 } /* while */
9108
9109 /*
9110 * For a negative line count use only the lines at the end of the file,
9111 * free the rest.
9112 */
9113 if (!failed && maxline < 0)
9114 while (cnt > -maxline)
9115 {
9116 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
9117 --cnt;
9118 }
9119
9120 if (failed)
9121 {
9122 list_free(rettv->vval.v_list);
9123 /* readfile doc says an empty list is returned on error */
9124 rettv->vval.v_list = list_alloc();
9125 }
9126
9127 vim_free(prev);
9128 fclose(fd);
9129}
9130
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009131 static void
9132return_register(int regname, typval_T *rettv)
9133{
9134 char_u buf[2] = {0, 0};
9135
9136 buf[0] = (char_u)regname;
9137 rettv->v_type = VAR_STRING;
9138 rettv->vval.v_string = vim_strsave(buf);
9139}
9140
9141/*
9142 * "reg_executing()" function
9143 */
9144 static void
9145f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9146{
9147 return_register(reg_executing, rettv);
9148}
9149
9150/*
9151 * "reg_recording()" function
9152 */
9153 static void
9154f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9155{
9156 return_register(reg_recording, rettv);
9157}
9158
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009159#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009160/*
9161 * Convert a List to proftime_T.
9162 * Return FAIL when there is something wrong.
9163 */
9164 static int
9165list2proftime(typval_T *arg, proftime_T *tm)
9166{
9167 long n1, n2;
9168 int error = FALSE;
9169
9170 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9171 || arg->vval.v_list->lv_len != 2)
9172 return FAIL;
9173 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9174 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
9175# ifdef WIN3264
9176 tm->HighPart = n1;
9177 tm->LowPart = n2;
9178# else
9179 tm->tv_sec = n1;
9180 tm->tv_usec = n2;
9181# endif
9182 return error ? FAIL : OK;
9183}
9184#endif /* FEAT_RELTIME */
9185
9186/*
9187 * "reltime()" function
9188 */
9189 static void
9190f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9191{
9192#ifdef FEAT_RELTIME
9193 proftime_T res;
9194 proftime_T start;
9195
9196 if (argvars[0].v_type == VAR_UNKNOWN)
9197 {
9198 /* No arguments: get current time. */
9199 profile_start(&res);
9200 }
9201 else if (argvars[1].v_type == VAR_UNKNOWN)
9202 {
9203 if (list2proftime(&argvars[0], &res) == FAIL)
9204 return;
9205 profile_end(&res);
9206 }
9207 else
9208 {
9209 /* Two arguments: compute the difference. */
9210 if (list2proftime(&argvars[0], &start) == FAIL
9211 || list2proftime(&argvars[1], &res) == FAIL)
9212 return;
9213 profile_sub(&res, &start);
9214 }
9215
9216 if (rettv_list_alloc(rettv) == OK)
9217 {
9218 long n1, n2;
9219
9220# ifdef WIN3264
9221 n1 = res.HighPart;
9222 n2 = res.LowPart;
9223# else
9224 n1 = res.tv_sec;
9225 n2 = res.tv_usec;
9226# endif
9227 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9228 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9229 }
9230#endif
9231}
9232
9233#ifdef FEAT_FLOAT
9234/*
9235 * "reltimefloat()" function
9236 */
9237 static void
9238f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9239{
9240# ifdef FEAT_RELTIME
9241 proftime_T tm;
9242# endif
9243
9244 rettv->v_type = VAR_FLOAT;
9245 rettv->vval.v_float = 0;
9246# ifdef FEAT_RELTIME
9247 if (list2proftime(&argvars[0], &tm) == OK)
9248 rettv->vval.v_float = profile_float(&tm);
9249# endif
9250}
9251#endif
9252
9253/*
9254 * "reltimestr()" function
9255 */
9256 static void
9257f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9258{
9259#ifdef FEAT_RELTIME
9260 proftime_T tm;
9261#endif
9262
9263 rettv->v_type = VAR_STRING;
9264 rettv->vval.v_string = NULL;
9265#ifdef FEAT_RELTIME
9266 if (list2proftime(&argvars[0], &tm) == OK)
9267 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9268#endif
9269}
9270
9271#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009272 static void
9273make_connection(void)
9274{
9275 if (X_DISPLAY == NULL
9276# ifdef FEAT_GUI
9277 && !gui.in_use
9278# endif
9279 )
9280 {
9281 x_force_connect = TRUE;
9282 setup_term_clip();
9283 x_force_connect = FALSE;
9284 }
9285}
9286
9287 static int
9288check_connection(void)
9289{
9290 make_connection();
9291 if (X_DISPLAY == NULL)
9292 {
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009293 EMSG(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009294 return FAIL;
9295 }
9296 return OK;
9297}
9298#endif
9299
9300#ifdef FEAT_CLIENTSERVER
9301 static void
9302remote_common(typval_T *argvars, typval_T *rettv, int expr)
9303{
9304 char_u *server_name;
9305 char_u *keys;
9306 char_u *r = NULL;
9307 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009308 int timeout = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009309# ifdef WIN32
9310 HWND w;
9311# else
9312 Window w;
9313# endif
9314
9315 if (check_restricted() || check_secure())
9316 return;
9317
9318# ifdef FEAT_X11
9319 if (check_connection() == FAIL)
9320 return;
9321# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009322 if (argvars[2].v_type != VAR_UNKNOWN
9323 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009324 timeout = tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009325
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009326 server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009327 if (server_name == NULL)
9328 return; /* type error; errmsg already given */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009329 keys = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009330# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009331 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009332# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009333 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9334 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009335# endif
9336 {
9337 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009338 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009339 EMSG(r); /* sending worked but evaluation failed */
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009340 vim_free(r);
9341 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009342 else
9343 EMSG2(_("E241: Unable to send to %s"), server_name);
9344 return;
9345 }
9346
9347 rettv->vval.v_string = r;
9348
9349 if (argvars[2].v_type != VAR_UNKNOWN)
9350 {
9351 dictitem_T v;
9352 char_u str[30];
9353 char_u *idvar;
9354
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009355 idvar = tv_get_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009356 if (idvar != NULL && *idvar != NUL)
9357 {
9358 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9359 v.di_tv.v_type = VAR_STRING;
9360 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009361 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009362 vim_free(v.di_tv.vval.v_string);
9363 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009364 }
9365}
9366#endif
9367
9368/*
9369 * "remote_expr()" function
9370 */
9371 static void
9372f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9373{
9374 rettv->v_type = VAR_STRING;
9375 rettv->vval.v_string = NULL;
9376#ifdef FEAT_CLIENTSERVER
9377 remote_common(argvars, rettv, TRUE);
9378#endif
9379}
9380
9381/*
9382 * "remote_foreground()" function
9383 */
9384 static void
9385f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9386{
9387#ifdef FEAT_CLIENTSERVER
9388# ifdef WIN32
9389 /* On Win32 it's done in this application. */
9390 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009391 char_u *server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009392
9393 if (server_name != NULL)
9394 serverForeground(server_name);
9395 }
9396# else
9397 /* Send a foreground() expression to the server. */
9398 argvars[1].v_type = VAR_STRING;
9399 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9400 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009401 rettv->v_type = VAR_STRING;
9402 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009403 remote_common(argvars, rettv, TRUE);
9404 vim_free(argvars[1].vval.v_string);
9405# endif
9406#endif
9407}
9408
9409 static void
9410f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
9411{
9412#ifdef FEAT_CLIENTSERVER
9413 dictitem_T v;
9414 char_u *s = NULL;
9415# ifdef WIN32
9416 long_u n = 0;
9417# endif
9418 char_u *serverid;
9419
9420 if (check_restricted() || check_secure())
9421 {
9422 rettv->vval.v_number = -1;
9423 return;
9424 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009425 serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009426 if (serverid == NULL)
9427 {
9428 rettv->vval.v_number = -1;
9429 return; /* type error; errmsg already given */
9430 }
9431# ifdef WIN32
9432 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
9433 if (n == 0)
9434 rettv->vval.v_number = -1;
9435 else
9436 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009437 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009438 rettv->vval.v_number = (s != NULL);
9439 }
9440# else
9441 if (check_connection() == FAIL)
9442 return;
9443
9444 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
9445 serverStrToWin(serverid), &s);
9446# endif
9447
9448 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
9449 {
9450 char_u *retvar;
9451
9452 v.di_tv.v_type = VAR_STRING;
9453 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009454 retvar = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009455 if (retvar != NULL)
9456 set_var(retvar, &v.di_tv, FALSE);
9457 vim_free(v.di_tv.vval.v_string);
9458 }
9459#else
9460 rettv->vval.v_number = -1;
9461#endif
9462}
9463
9464 static void
9465f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
9466{
9467 char_u *r = NULL;
9468
9469#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009470 char_u *serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009471
9472 if (serverid != NULL && !check_restricted() && !check_secure())
9473 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009474 int timeout = 0;
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009475# ifdef WIN32
9476 /* The server's HWND is encoded in the 'id' parameter */
9477 long_u n = 0;
9478# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009479
9480 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009481 timeout = tv_get_number(&argvars[1]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009482
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009483# ifdef WIN32
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009484 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
9485 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009486 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009487 if (r == NULL)
9488# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009489 if (check_connection() == FAIL
9490 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
9491 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009492# endif
9493 EMSG(_("E277: Unable to read a server reply"));
9494 }
9495#endif
9496 rettv->v_type = VAR_STRING;
9497 rettv->vval.v_string = r;
9498}
9499
9500/*
9501 * "remote_send()" function
9502 */
9503 static void
9504f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9505{
9506 rettv->v_type = VAR_STRING;
9507 rettv->vval.v_string = NULL;
9508#ifdef FEAT_CLIENTSERVER
9509 remote_common(argvars, rettv, FALSE);
9510#endif
9511}
9512
9513/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009514 * "remote_startserver()" function
9515 */
9516 static void
9517f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9518{
9519#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009520 char_u *server = tv_get_string_chk(&argvars[0]);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009521
9522 if (server == NULL)
9523 return; /* type error; errmsg already given */
9524 if (serverName != NULL)
9525 EMSG(_("E941: already started a server"));
9526 else
9527 {
9528# ifdef FEAT_X11
9529 if (check_connection() == OK)
9530 serverRegisterName(X_DISPLAY, server);
9531# else
9532 serverSetName(server);
9533# endif
9534 }
9535#else
9536 EMSG(_("E942: +clientserver feature not available"));
9537#endif
9538}
9539
9540/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009541 * "remove()" function
9542 */
9543 static void
9544f_remove(typval_T *argvars, typval_T *rettv)
9545{
9546 list_T *l;
9547 listitem_T *item, *item2;
9548 listitem_T *li;
9549 long idx;
9550 long end;
9551 char_u *key;
9552 dict_T *d;
9553 dictitem_T *di;
9554 char_u *arg_errmsg = (char_u *)N_("remove() argument");
9555
9556 if (argvars[0].v_type == VAR_DICT)
9557 {
9558 if (argvars[2].v_type != VAR_UNKNOWN)
9559 EMSG2(_(e_toomanyarg), "remove()");
9560 else if ((d = argvars[0].vval.v_dict) != NULL
9561 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
9562 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009563 key = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009564 if (key != NULL)
9565 {
9566 di = dict_find(d, key, -1);
9567 if (di == NULL)
9568 EMSG2(_(e_dictkey), key);
9569 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
9570 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
9571 {
9572 *rettv = di->di_tv;
9573 init_tv(&di->di_tv);
9574 dictitem_remove(d, di);
9575 }
9576 }
9577 }
9578 }
9579 else if (argvars[0].v_type != VAR_LIST)
9580 EMSG2(_(e_listdictarg), "remove()");
9581 else if ((l = argvars[0].vval.v_list) != NULL
9582 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
9583 {
9584 int error = FALSE;
9585
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009586 idx = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009587 if (error)
9588 ; /* type error: do nothing, errmsg already given */
9589 else if ((item = list_find(l, idx)) == NULL)
9590 EMSGN(_(e_listidx), idx);
9591 else
9592 {
9593 if (argvars[2].v_type == VAR_UNKNOWN)
9594 {
9595 /* Remove one item, return its value. */
9596 vimlist_remove(l, item, item);
9597 *rettv = item->li_tv;
9598 vim_free(item);
9599 }
9600 else
9601 {
9602 /* Remove range of items, return list with values. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009603 end = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009604 if (error)
9605 ; /* type error: do nothing */
9606 else if ((item2 = list_find(l, end)) == NULL)
9607 EMSGN(_(e_listidx), end);
9608 else
9609 {
9610 int cnt = 0;
9611
9612 for (li = item; li != NULL; li = li->li_next)
9613 {
9614 ++cnt;
9615 if (li == item2)
9616 break;
9617 }
9618 if (li == NULL) /* didn't find "item2" after "item" */
9619 EMSG(_(e_invrange));
9620 else
9621 {
9622 vimlist_remove(l, item, item2);
9623 if (rettv_list_alloc(rettv) == OK)
9624 {
9625 l = rettv->vval.v_list;
9626 l->lv_first = item;
9627 l->lv_last = item2;
9628 item->li_prev = NULL;
9629 item2->li_next = NULL;
9630 l->lv_len = cnt;
9631 }
9632 }
9633 }
9634 }
9635 }
9636 }
9637}
9638
9639/*
9640 * "rename({from}, {to})" function
9641 */
9642 static void
9643f_rename(typval_T *argvars, typval_T *rettv)
9644{
9645 char_u buf[NUMBUFLEN];
9646
9647 if (check_restricted() || check_secure())
9648 rettv->vval.v_number = -1;
9649 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009650 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
9651 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009652}
9653
9654/*
9655 * "repeat()" function
9656 */
9657 static void
9658f_repeat(typval_T *argvars, typval_T *rettv)
9659{
9660 char_u *p;
9661 int n;
9662 int slen;
9663 int len;
9664 char_u *r;
9665 int i;
9666
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009667 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009668 if (argvars[0].v_type == VAR_LIST)
9669 {
9670 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9671 while (n-- > 0)
9672 if (list_extend(rettv->vval.v_list,
9673 argvars[0].vval.v_list, NULL) == FAIL)
9674 break;
9675 }
9676 else
9677 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009678 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009679 rettv->v_type = VAR_STRING;
9680 rettv->vval.v_string = NULL;
9681
9682 slen = (int)STRLEN(p);
9683 len = slen * n;
9684 if (len <= 0)
9685 return;
9686
9687 r = alloc(len + 1);
9688 if (r != NULL)
9689 {
9690 for (i = 0; i < n; i++)
9691 mch_memmove(r + i * slen, p, (size_t)slen);
9692 r[len] = NUL;
9693 }
9694
9695 rettv->vval.v_string = r;
9696 }
9697}
9698
9699/*
9700 * "resolve()" function
9701 */
9702 static void
9703f_resolve(typval_T *argvars, typval_T *rettv)
9704{
9705 char_u *p;
9706#ifdef HAVE_READLINK
9707 char_u *buf = NULL;
9708#endif
9709
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009710 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009711#ifdef FEAT_SHORTCUT
9712 {
9713 char_u *v = NULL;
9714
9715 v = mch_resolve_shortcut(p);
9716 if (v != NULL)
9717 rettv->vval.v_string = v;
9718 else
9719 rettv->vval.v_string = vim_strsave(p);
9720 }
9721#else
9722# ifdef HAVE_READLINK
9723 {
9724 char_u *cpy;
9725 int len;
9726 char_u *remain = NULL;
9727 char_u *q;
9728 int is_relative_to_current = FALSE;
9729 int has_trailing_pathsep = FALSE;
9730 int limit = 100;
9731
9732 p = vim_strsave(p);
9733
9734 if (p[0] == '.' && (vim_ispathsep(p[1])
9735 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9736 is_relative_to_current = TRUE;
9737
9738 len = STRLEN(p);
9739 if (len > 0 && after_pathsep(p, p + len))
9740 {
9741 has_trailing_pathsep = TRUE;
9742 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9743 }
9744
9745 q = getnextcomp(p);
9746 if (*q != NUL)
9747 {
9748 /* Separate the first path component in "p", and keep the
9749 * remainder (beginning with the path separator). */
9750 remain = vim_strsave(q - 1);
9751 q[-1] = NUL;
9752 }
9753
9754 buf = alloc(MAXPATHL + 1);
9755 if (buf == NULL)
9756 goto fail;
9757
9758 for (;;)
9759 {
9760 for (;;)
9761 {
9762 len = readlink((char *)p, (char *)buf, MAXPATHL);
9763 if (len <= 0)
9764 break;
9765 buf[len] = NUL;
9766
9767 if (limit-- == 0)
9768 {
9769 vim_free(p);
9770 vim_free(remain);
9771 EMSG(_("E655: Too many symbolic links (cycle?)"));
9772 rettv->vval.v_string = NULL;
9773 goto fail;
9774 }
9775
9776 /* Ensure that the result will have a trailing path separator
9777 * if the argument has one. */
9778 if (remain == NULL && has_trailing_pathsep)
9779 add_pathsep(buf);
9780
9781 /* Separate the first path component in the link value and
9782 * concatenate the remainders. */
9783 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9784 if (*q != NUL)
9785 {
9786 if (remain == NULL)
9787 remain = vim_strsave(q - 1);
9788 else
9789 {
9790 cpy = concat_str(q - 1, remain);
9791 if (cpy != NULL)
9792 {
9793 vim_free(remain);
9794 remain = cpy;
9795 }
9796 }
9797 q[-1] = NUL;
9798 }
9799
9800 q = gettail(p);
9801 if (q > p && *q == NUL)
9802 {
9803 /* Ignore trailing path separator. */
9804 q[-1] = NUL;
9805 q = gettail(p);
9806 }
9807 if (q > p && !mch_isFullName(buf))
9808 {
9809 /* symlink is relative to directory of argument */
9810 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
9811 if (cpy != NULL)
9812 {
9813 STRCPY(cpy, p);
9814 STRCPY(gettail(cpy), buf);
9815 vim_free(p);
9816 p = cpy;
9817 }
9818 }
9819 else
9820 {
9821 vim_free(p);
9822 p = vim_strsave(buf);
9823 }
9824 }
9825
9826 if (remain == NULL)
9827 break;
9828
9829 /* Append the first path component of "remain" to "p". */
9830 q = getnextcomp(remain + 1);
9831 len = q - remain - (*q != NUL);
9832 cpy = vim_strnsave(p, STRLEN(p) + len);
9833 if (cpy != NULL)
9834 {
9835 STRNCAT(cpy, remain, len);
9836 vim_free(p);
9837 p = cpy;
9838 }
9839 /* Shorten "remain". */
9840 if (*q != NUL)
9841 STRMOVE(remain, q - 1);
9842 else
Bram Moolenaard23a8232018-02-10 18:45:26 +01009843 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009844 }
9845
9846 /* If the result is a relative path name, make it explicitly relative to
9847 * the current directory if and only if the argument had this form. */
9848 if (!vim_ispathsep(*p))
9849 {
9850 if (is_relative_to_current
9851 && *p != NUL
9852 && !(p[0] == '.'
9853 && (p[1] == NUL
9854 || vim_ispathsep(p[1])
9855 || (p[1] == '.'
9856 && (p[2] == NUL
9857 || vim_ispathsep(p[2]))))))
9858 {
9859 /* Prepend "./". */
9860 cpy = concat_str((char_u *)"./", p);
9861 if (cpy != NULL)
9862 {
9863 vim_free(p);
9864 p = cpy;
9865 }
9866 }
9867 else if (!is_relative_to_current)
9868 {
9869 /* Strip leading "./". */
9870 q = p;
9871 while (q[0] == '.' && vim_ispathsep(q[1]))
9872 q += 2;
9873 if (q > p)
9874 STRMOVE(p, p + 2);
9875 }
9876 }
9877
9878 /* Ensure that the result will have no trailing path separator
9879 * if the argument had none. But keep "/" or "//". */
9880 if (!has_trailing_pathsep)
9881 {
9882 q = p + STRLEN(p);
9883 if (after_pathsep(p, q))
9884 *gettail_sep(p) = NUL;
9885 }
9886
9887 rettv->vval.v_string = p;
9888 }
9889# else
9890 rettv->vval.v_string = vim_strsave(p);
9891# endif
9892#endif
9893
9894 simplify_filename(rettv->vval.v_string);
9895
9896#ifdef HAVE_READLINK
9897fail:
9898 vim_free(buf);
9899#endif
9900 rettv->v_type = VAR_STRING;
9901}
9902
9903/*
9904 * "reverse({list})" function
9905 */
9906 static void
9907f_reverse(typval_T *argvars, typval_T *rettv)
9908{
9909 list_T *l;
9910 listitem_T *li, *ni;
9911
9912 if (argvars[0].v_type != VAR_LIST)
9913 EMSG2(_(e_listarg), "reverse()");
9914 else if ((l = argvars[0].vval.v_list) != NULL
9915 && !tv_check_lock(l->lv_lock,
9916 (char_u *)N_("reverse() argument"), TRUE))
9917 {
9918 li = l->lv_last;
9919 l->lv_first = l->lv_last = NULL;
9920 l->lv_len = 0;
9921 while (li != NULL)
9922 {
9923 ni = li->li_prev;
9924 list_append(l, li);
9925 li = ni;
9926 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009927 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009928 l->lv_idx = l->lv_len - l->lv_idx - 1;
9929 }
9930}
9931
9932#define SP_NOMOVE 0x01 /* don't move cursor */
9933#define SP_REPEAT 0x02 /* repeat to find outer pair */
9934#define SP_RETCOUNT 0x04 /* return matchcount */
9935#define SP_SETPCMARK 0x08 /* set previous context mark */
9936#define SP_START 0x10 /* accept match at start position */
9937#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
9938#define SP_END 0x40 /* leave cursor at end of match */
9939#define SP_COLUMN 0x80 /* start at cursor column */
9940
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009941/*
9942 * Get flags for a search function.
9943 * Possibly sets "p_ws".
9944 * Returns BACKWARD, FORWARD or zero (for an error).
9945 */
9946 static int
9947get_search_arg(typval_T *varp, int *flagsp)
9948{
9949 int dir = FORWARD;
9950 char_u *flags;
9951 char_u nbuf[NUMBUFLEN];
9952 int mask;
9953
9954 if (varp->v_type != VAR_UNKNOWN)
9955 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009956 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009957 if (flags == NULL)
9958 return 0; /* type error; errmsg already given */
9959 while (*flags != NUL)
9960 {
9961 switch (*flags)
9962 {
9963 case 'b': dir = BACKWARD; break;
9964 case 'w': p_ws = TRUE; break;
9965 case 'W': p_ws = FALSE; break;
9966 default: mask = 0;
9967 if (flagsp != NULL)
9968 switch (*flags)
9969 {
9970 case 'c': mask = SP_START; break;
9971 case 'e': mask = SP_END; break;
9972 case 'm': mask = SP_RETCOUNT; break;
9973 case 'n': mask = SP_NOMOVE; break;
9974 case 'p': mask = SP_SUBPAT; break;
9975 case 'r': mask = SP_REPEAT; break;
9976 case 's': mask = SP_SETPCMARK; break;
9977 case 'z': mask = SP_COLUMN; break;
9978 }
9979 if (mask == 0)
9980 {
9981 EMSG2(_(e_invarg2), flags);
9982 dir = 0;
9983 }
9984 else
9985 *flagsp |= mask;
9986 }
9987 if (dir == 0)
9988 break;
9989 ++flags;
9990 }
9991 }
9992 return dir;
9993}
9994
9995/*
9996 * Shared by search() and searchpos() functions.
9997 */
9998 static int
9999search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
10000{
10001 int flags;
10002 char_u *pat;
10003 pos_T pos;
10004 pos_T save_cursor;
10005 int save_p_ws = p_ws;
10006 int dir;
10007 int retval = 0; /* default: FAIL */
10008 long lnum_stop = 0;
10009 proftime_T tm;
10010#ifdef FEAT_RELTIME
10011 long time_limit = 0;
10012#endif
10013 int options = SEARCH_KEEP;
10014 int subpatnum;
10015
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010016 pat = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010017 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
10018 if (dir == 0)
10019 goto theend;
10020 flags = *flagsp;
10021 if (flags & SP_START)
10022 options |= SEARCH_START;
10023 if (flags & SP_END)
10024 options |= SEARCH_END;
10025 if (flags & SP_COLUMN)
10026 options |= SEARCH_COL;
10027
10028 /* Optional arguments: line number to stop searching and timeout. */
10029 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
10030 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010031 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010032 if (lnum_stop < 0)
10033 goto theend;
10034#ifdef FEAT_RELTIME
10035 if (argvars[3].v_type != VAR_UNKNOWN)
10036 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010037 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010038 if (time_limit < 0)
10039 goto theend;
10040 }
10041#endif
10042 }
10043
10044#ifdef FEAT_RELTIME
10045 /* Set the time limit, if there is one. */
10046 profile_setlimit(time_limit, &tm);
10047#endif
10048
10049 /*
10050 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
10051 * Check to make sure only those flags are set.
10052 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
10053 * flags cannot be set. Check for that condition also.
10054 */
10055 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
10056 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10057 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010058 EMSG2(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010059 goto theend;
10060 }
10061
10062 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010063 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010064 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010065 if (subpatnum != FAIL)
10066 {
10067 if (flags & SP_SUBPAT)
10068 retval = subpatnum;
10069 else
10070 retval = pos.lnum;
10071 if (flags & SP_SETPCMARK)
10072 setpcmark();
10073 curwin->w_cursor = pos;
10074 if (match_pos != NULL)
10075 {
10076 /* Store the match cursor position */
10077 match_pos->lnum = pos.lnum;
10078 match_pos->col = pos.col + 1;
10079 }
10080 /* "/$" will put the cursor after the end of the line, may need to
10081 * correct that here */
10082 check_cursor();
10083 }
10084
10085 /* If 'n' flag is used: restore cursor position. */
10086 if (flags & SP_NOMOVE)
10087 curwin->w_cursor = save_cursor;
10088 else
10089 curwin->w_set_curswant = TRUE;
10090theend:
10091 p_ws = save_p_ws;
10092
10093 return retval;
10094}
10095
10096#ifdef FEAT_FLOAT
10097
10098/*
10099 * round() is not in C90, use ceil() or floor() instead.
10100 */
10101 float_T
10102vim_round(float_T f)
10103{
10104 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
10105}
10106
10107/*
10108 * "round({float})" function
10109 */
10110 static void
10111f_round(typval_T *argvars, typval_T *rettv)
10112{
10113 float_T f = 0.0;
10114
10115 rettv->v_type = VAR_FLOAT;
10116 if (get_float_arg(argvars, &f) == OK)
10117 rettv->vval.v_float = vim_round(f);
10118 else
10119 rettv->vval.v_float = 0.0;
10120}
10121#endif
10122
10123/*
10124 * "screenattr()" function
10125 */
10126 static void
10127f_screenattr(typval_T *argvars, typval_T *rettv)
10128{
10129 int row;
10130 int col;
10131 int c;
10132
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010133 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10134 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010135 if (row < 0 || row >= screen_Rows
10136 || col < 0 || col >= screen_Columns)
10137 c = -1;
10138 else
10139 c = ScreenAttrs[LineOffset[row] + col];
10140 rettv->vval.v_number = c;
10141}
10142
10143/*
10144 * "screenchar()" function
10145 */
10146 static void
10147f_screenchar(typval_T *argvars, typval_T *rettv)
10148{
10149 int row;
10150 int col;
10151 int off;
10152 int c;
10153
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010154 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10155 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010156 if (row < 0 || row >= screen_Rows
10157 || col < 0 || col >= screen_Columns)
10158 c = -1;
10159 else
10160 {
10161 off = LineOffset[row] + col;
10162#ifdef FEAT_MBYTE
10163 if (enc_utf8 && ScreenLinesUC[off] != 0)
10164 c = ScreenLinesUC[off];
10165 else
10166#endif
10167 c = ScreenLines[off];
10168 }
10169 rettv->vval.v_number = c;
10170}
10171
10172/*
10173 * "screencol()" function
10174 *
10175 * First column is 1 to be consistent with virtcol().
10176 */
10177 static void
10178f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10179{
10180 rettv->vval.v_number = screen_screencol() + 1;
10181}
10182
10183/*
10184 * "screenrow()" function
10185 */
10186 static void
10187f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10188{
10189 rettv->vval.v_number = screen_screenrow() + 1;
10190}
10191
10192/*
10193 * "search()" function
10194 */
10195 static void
10196f_search(typval_T *argvars, typval_T *rettv)
10197{
10198 int flags = 0;
10199
10200 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10201}
10202
10203/*
10204 * "searchdecl()" function
10205 */
10206 static void
10207f_searchdecl(typval_T *argvars, typval_T *rettv)
10208{
10209 int locally = 1;
10210 int thisblock = 0;
10211 int error = FALSE;
10212 char_u *name;
10213
10214 rettv->vval.v_number = 1; /* default: FAIL */
10215
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010216 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010217 if (argvars[1].v_type != VAR_UNKNOWN)
10218 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010219 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010220 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010221 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010222 }
10223 if (!error && name != NULL)
10224 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10225 locally, thisblock, SEARCH_KEEP) == FAIL;
10226}
10227
10228/*
10229 * Used by searchpair() and searchpairpos()
10230 */
10231 static int
10232searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10233{
10234 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010235 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010236 int save_p_ws = p_ws;
10237 int dir;
10238 int flags = 0;
10239 char_u nbuf1[NUMBUFLEN];
10240 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010241 int retval = 0; /* default: FAIL */
10242 long lnum_stop = 0;
10243 long time_limit = 0;
10244
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010245 /* Get the three pattern arguments: start, middle, end. Will result in an
10246 * error if not a valid argument. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010247 spat = tv_get_string_chk(&argvars[0]);
10248 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
10249 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010250 if (spat == NULL || mpat == NULL || epat == NULL)
10251 goto theend; /* type error */
10252
10253 /* Handle the optional fourth argument: flags */
10254 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
10255 if (dir == 0)
10256 goto theend;
10257
10258 /* Don't accept SP_END or SP_SUBPAT.
10259 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
10260 */
10261 if ((flags & (SP_END | SP_SUBPAT)) != 0
10262 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10263 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010264 EMSG2(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010265 goto theend;
10266 }
10267
10268 /* Using 'r' implies 'W', otherwise it doesn't work. */
10269 if (flags & SP_REPEAT)
10270 p_ws = FALSE;
10271
10272 /* Optional fifth argument: skip expression */
10273 if (argvars[3].v_type == VAR_UNKNOWN
10274 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010010275 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010276 else
10277 {
Bram Moolenaar48570482017-10-30 21:48:41 +010010278 skip = &argvars[4];
10279 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
10280 && skip->v_type != VAR_STRING)
10281 {
10282 /* Type error */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010283 EMSG2(_(e_invarg2), tv_get_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010010284 goto theend;
10285 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010286 if (argvars[5].v_type != VAR_UNKNOWN)
10287 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010288 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010289 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010290 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010291 EMSG2(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010292 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010293 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010294#ifdef FEAT_RELTIME
10295 if (argvars[6].v_type != VAR_UNKNOWN)
10296 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010297 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010298 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010299 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010300 EMSG2(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010301 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010302 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010303 }
10304#endif
10305 }
10306 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010307
10308 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
10309 match_pos, lnum_stop, time_limit);
10310
10311theend:
10312 p_ws = save_p_ws;
10313
10314 return retval;
10315}
10316
10317/*
10318 * "searchpair()" function
10319 */
10320 static void
10321f_searchpair(typval_T *argvars, typval_T *rettv)
10322{
10323 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
10324}
10325
10326/*
10327 * "searchpairpos()" function
10328 */
10329 static void
10330f_searchpairpos(typval_T *argvars, typval_T *rettv)
10331{
10332 pos_T match_pos;
10333 int lnum = 0;
10334 int col = 0;
10335
10336 if (rettv_list_alloc(rettv) == FAIL)
10337 return;
10338
10339 if (searchpair_cmn(argvars, &match_pos) > 0)
10340 {
10341 lnum = match_pos.lnum;
10342 col = match_pos.col;
10343 }
10344
10345 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10346 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10347}
10348
10349/*
10350 * Search for a start/middle/end thing.
10351 * Used by searchpair(), see its documentation for the details.
10352 * Returns 0 or -1 for no match,
10353 */
10354 long
10355do_searchpair(
10356 char_u *spat, /* start pattern */
10357 char_u *mpat, /* middle pattern */
10358 char_u *epat, /* end pattern */
10359 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010010360 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010361 int flags, /* SP_SETPCMARK and other SP_ values */
10362 pos_T *match_pos,
10363 linenr_T lnum_stop, /* stop at this line if not zero */
10364 long time_limit UNUSED) /* stop after this many msec */
10365{
10366 char_u *save_cpo;
10367 char_u *pat, *pat2 = NULL, *pat3 = NULL;
10368 long retval = 0;
10369 pos_T pos;
10370 pos_T firstpos;
10371 pos_T foundpos;
10372 pos_T save_cursor;
10373 pos_T save_pos;
10374 int n;
10375 int r;
10376 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010010377 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010378 int err;
10379 int options = SEARCH_KEEP;
10380 proftime_T tm;
10381
10382 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10383 save_cpo = p_cpo;
10384 p_cpo = empty_option;
10385
10386#ifdef FEAT_RELTIME
10387 /* Set the time limit, if there is one. */
10388 profile_setlimit(time_limit, &tm);
10389#endif
10390
10391 /* Make two search patterns: start/end (pat2, for in nested pairs) and
10392 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010393 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
10394 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010395 if (pat2 == NULL || pat3 == NULL)
10396 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010397 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010398 if (*mpat == NUL)
10399 STRCPY(pat3, pat2);
10400 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010401 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010402 spat, epat, mpat);
10403 if (flags & SP_START)
10404 options |= SEARCH_START;
10405
Bram Moolenaar48570482017-10-30 21:48:41 +010010406 if (skip != NULL)
10407 {
10408 /* Empty string means to not use the skip expression. */
10409 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
10410 use_skip = skip->vval.v_string != NULL
10411 && *skip->vval.v_string != NUL;
10412 }
10413
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010414 save_cursor = curwin->w_cursor;
10415 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010416 CLEAR_POS(&firstpos);
10417 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010418 pat = pat3;
10419 for (;;)
10420 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010421 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010422 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010423 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010424 /* didn't find it or found the first match again: FAIL */
10425 break;
10426
10427 if (firstpos.lnum == 0)
10428 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010429 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010430 {
10431 /* Found the same position again. Can happen with a pattern that
10432 * has "\zs" at the end and searching backwards. Advance one
10433 * character and try again. */
10434 if (dir == BACKWARD)
10435 decl(&pos);
10436 else
10437 incl(&pos);
10438 }
10439 foundpos = pos;
10440
10441 /* clear the start flag to avoid getting stuck here */
10442 options &= ~SEARCH_START;
10443
10444 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010010445 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010446 {
10447 save_pos = curwin->w_cursor;
10448 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010010449 err = FALSE;
10450 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010451 curwin->w_cursor = save_pos;
10452 if (err)
10453 {
10454 /* Evaluating {skip} caused an error, break here. */
10455 curwin->w_cursor = save_cursor;
10456 retval = -1;
10457 break;
10458 }
10459 if (r)
10460 continue;
10461 }
10462
10463 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
10464 {
10465 /* Found end when searching backwards or start when searching
10466 * forward: nested pair. */
10467 ++nest;
10468 pat = pat2; /* nested, don't search for middle */
10469 }
10470 else
10471 {
10472 /* Found end when searching forward or start when searching
10473 * backward: end of (nested) pair; or found middle in outer pair. */
10474 if (--nest == 1)
10475 pat = pat3; /* outer level, search for middle */
10476 }
10477
10478 if (nest == 0)
10479 {
10480 /* Found the match: return matchcount or line number. */
10481 if (flags & SP_RETCOUNT)
10482 ++retval;
10483 else
10484 retval = pos.lnum;
10485 if (flags & SP_SETPCMARK)
10486 setpcmark();
10487 curwin->w_cursor = pos;
10488 if (!(flags & SP_REPEAT))
10489 break;
10490 nest = 1; /* search for next unmatched */
10491 }
10492 }
10493
10494 if (match_pos != NULL)
10495 {
10496 /* Store the match cursor position */
10497 match_pos->lnum = curwin->w_cursor.lnum;
10498 match_pos->col = curwin->w_cursor.col + 1;
10499 }
10500
10501 /* If 'n' flag is used or search failed: restore cursor position. */
10502 if ((flags & SP_NOMOVE) || retval == 0)
10503 curwin->w_cursor = save_cursor;
10504
10505theend:
10506 vim_free(pat2);
10507 vim_free(pat3);
10508 if (p_cpo == empty_option)
10509 p_cpo = save_cpo;
10510 else
10511 /* Darn, evaluating the {skip} expression changed the value. */
10512 free_string_option(save_cpo);
10513
10514 return retval;
10515}
10516
10517/*
10518 * "searchpos()" function
10519 */
10520 static void
10521f_searchpos(typval_T *argvars, typval_T *rettv)
10522{
10523 pos_T match_pos;
10524 int lnum = 0;
10525 int col = 0;
10526 int n;
10527 int flags = 0;
10528
10529 if (rettv_list_alloc(rettv) == FAIL)
10530 return;
10531
10532 n = search_cmn(argvars, &match_pos, &flags);
10533 if (n > 0)
10534 {
10535 lnum = match_pos.lnum;
10536 col = match_pos.col;
10537 }
10538
10539 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10540 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10541 if (flags & SP_SUBPAT)
10542 list_append_number(rettv->vval.v_list, (varnumber_T)n);
10543}
10544
10545 static void
10546f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
10547{
10548#ifdef FEAT_CLIENTSERVER
10549 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010550 char_u *server = tv_get_string_chk(&argvars[0]);
10551 char_u *reply = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010552
10553 rettv->vval.v_number = -1;
10554 if (server == NULL || reply == NULL)
10555 return;
10556 if (check_restricted() || check_secure())
10557 return;
10558# ifdef FEAT_X11
10559 if (check_connection() == FAIL)
10560 return;
10561# endif
10562
10563 if (serverSendReply(server, reply) < 0)
10564 {
10565 EMSG(_("E258: Unable to send to client"));
10566 return;
10567 }
10568 rettv->vval.v_number = 0;
10569#else
10570 rettv->vval.v_number = -1;
10571#endif
10572}
10573
10574 static void
10575f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
10576{
10577 char_u *r = NULL;
10578
10579#ifdef FEAT_CLIENTSERVER
10580# ifdef WIN32
10581 r = serverGetVimNames();
10582# else
10583 make_connection();
10584 if (X_DISPLAY != NULL)
10585 r = serverGetVimNames(X_DISPLAY);
10586# endif
10587#endif
10588 rettv->v_type = VAR_STRING;
10589 rettv->vval.v_string = r;
10590}
10591
10592/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010593 * "setbufline()" function
10594 */
10595 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020010596f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010597{
10598 linenr_T lnum;
10599 buf_T *buf;
10600
10601 buf = get_buf_tv(&argvars[0], FALSE);
10602 if (buf == NULL)
10603 rettv->vval.v_number = 1; /* FAIL */
10604 else
10605 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010606 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020010607 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010608 }
10609}
10610
10611/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010612 * "setbufvar()" function
10613 */
10614 static void
10615f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10616{
10617 buf_T *buf;
10618 char_u *varname, *bufvarname;
10619 typval_T *varp;
10620 char_u nbuf[NUMBUFLEN];
10621
10622 if (check_restricted() || check_secure())
10623 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010624 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
10625 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010626 buf = get_buf_tv(&argvars[0], FALSE);
10627 varp = &argvars[2];
10628
10629 if (buf != NULL && varname != NULL && varp != NULL)
10630 {
10631 if (*varname == '&')
10632 {
10633 long numval;
10634 char_u *strval;
10635 int error = FALSE;
10636 aco_save_T aco;
10637
10638 /* set curbuf to be our buf, temporarily */
10639 aucmd_prepbuf(&aco, buf);
10640
10641 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010642 numval = (long)tv_get_number_chk(varp, &error);
10643 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010644 if (!error && strval != NULL)
10645 set_option_value(varname, numval, strval, OPT_LOCAL);
10646
10647 /* reset notion of buffer */
10648 aucmd_restbuf(&aco);
10649 }
10650 else
10651 {
10652 buf_T *save_curbuf = curbuf;
10653
10654 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
10655 if (bufvarname != NULL)
10656 {
10657 curbuf = buf;
10658 STRCPY(bufvarname, "b:");
10659 STRCPY(bufvarname + 2, varname);
10660 set_var(bufvarname, varp, TRUE);
10661 vim_free(bufvarname);
10662 curbuf = save_curbuf;
10663 }
10664 }
10665 }
10666}
10667
10668 static void
10669f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10670{
10671 dict_T *d;
10672 dictitem_T *di;
10673 char_u *csearch;
10674
10675 if (argvars[0].v_type != VAR_DICT)
10676 {
10677 EMSG(_(e_dictreq));
10678 return;
10679 }
10680
10681 if ((d = argvars[0].vval.v_dict) != NULL)
10682 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010010683 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010684 if (csearch != NULL)
10685 {
10686#ifdef FEAT_MBYTE
10687 if (enc_utf8)
10688 {
10689 int pcc[MAX_MCO];
10690 int c = utfc_ptr2char(csearch, pcc);
10691
10692 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
10693 }
10694 else
10695#endif
10696 set_last_csearch(PTR2CHAR(csearch),
10697 csearch, MB_PTR2LEN(csearch));
10698 }
10699
10700 di = dict_find(d, (char_u *)"forward", -1);
10701 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010702 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010703 ? FORWARD : BACKWARD);
10704
10705 di = dict_find(d, (char_u *)"until", -1);
10706 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010707 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010708 }
10709}
10710
10711/*
10712 * "setcmdpos()" function
10713 */
10714 static void
10715f_setcmdpos(typval_T *argvars, typval_T *rettv)
10716{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010717 int pos = (int)tv_get_number(&argvars[0]) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010718
10719 if (pos >= 0)
10720 rettv->vval.v_number = set_cmdline_pos(pos);
10721}
10722
10723/*
10724 * "setfperm({fname}, {mode})" function
10725 */
10726 static void
10727f_setfperm(typval_T *argvars, typval_T *rettv)
10728{
10729 char_u *fname;
10730 char_u modebuf[NUMBUFLEN];
10731 char_u *mode_str;
10732 int i;
10733 int mask;
10734 int mode = 0;
10735
10736 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010737 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010738 if (fname == NULL)
10739 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010740 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010741 if (mode_str == NULL)
10742 return;
10743 if (STRLEN(mode_str) != 9)
10744 {
10745 EMSG2(_(e_invarg2), mode_str);
10746 return;
10747 }
10748
10749 mask = 1;
10750 for (i = 8; i >= 0; --i)
10751 {
10752 if (mode_str[i] != '-')
10753 mode |= mask;
10754 mask = mask << 1;
10755 }
10756 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
10757}
10758
10759/*
10760 * "setline()" function
10761 */
10762 static void
10763f_setline(typval_T *argvars, typval_T *rettv)
10764{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010765 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010766
Bram Moolenaarca851592018-06-06 21:04:07 +020010767 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010768}
10769
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010770/*
10771 * Used by "setqflist()" and "setloclist()" functions
10772 */
10773 static void
10774set_qf_ll_list(
10775 win_T *wp UNUSED,
10776 typval_T *list_arg UNUSED,
10777 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020010778 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010779 typval_T *rettv)
10780{
10781#ifdef FEAT_QUICKFIX
10782 static char *e_invact = N_("E927: Invalid action: '%s'");
10783 char_u *act;
10784 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010785 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010786#endif
10787
10788 rettv->vval.v_number = -1;
10789
10790#ifdef FEAT_QUICKFIX
10791 if (list_arg->v_type != VAR_LIST)
10792 EMSG(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010793 else if (recursive != 0)
10794 EMSG(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010795 else
10796 {
10797 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010798 dict_T *d = NULL;
10799 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010800
10801 if (action_arg->v_type == VAR_STRING)
10802 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010803 act = tv_get_string_chk(action_arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010804 if (act == NULL)
10805 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020010806 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
10807 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010808 action = *act;
10809 else
10810 EMSG2(_(e_invact), act);
10811 }
10812 else if (action_arg->v_type == VAR_UNKNOWN)
10813 action = ' ';
10814 else
10815 EMSG(_(e_stringreq));
10816
Bram Moolenaard823fa92016-08-12 16:29:27 +020010817 if (action_arg->v_type != VAR_UNKNOWN
10818 && what_arg->v_type != VAR_UNKNOWN)
10819 {
10820 if (what_arg->v_type == VAR_DICT)
10821 d = what_arg->vval.v_dict;
10822 else
10823 {
10824 EMSG(_(e_dictreq));
10825 valid_dict = FALSE;
10826 }
10827 }
10828
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010829 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010830 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010831 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
10832 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010833 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010834 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010835 }
10836#endif
10837}
10838
10839/*
10840 * "setloclist()" function
10841 */
10842 static void
10843f_setloclist(typval_T *argvars, typval_T *rettv)
10844{
10845 win_T *win;
10846
10847 rettv->vval.v_number = -1;
10848
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020010849 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010850 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020010851 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010852}
10853
10854/*
10855 * "setmatches()" function
10856 */
10857 static void
10858f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10859{
10860#ifdef FEAT_SEARCH_EXTRA
10861 list_T *l;
10862 listitem_T *li;
10863 dict_T *d;
10864 list_T *s = NULL;
10865
10866 rettv->vval.v_number = -1;
10867 if (argvars[0].v_type != VAR_LIST)
10868 {
10869 EMSG(_(e_listreq));
10870 return;
10871 }
10872 if ((l = argvars[0].vval.v_list) != NULL)
10873 {
10874
10875 /* To some extent make sure that we are dealing with a list from
10876 * "getmatches()". */
10877 li = l->lv_first;
10878 while (li != NULL)
10879 {
10880 if (li->li_tv.v_type != VAR_DICT
10881 || (d = li->li_tv.vval.v_dict) == NULL)
10882 {
10883 EMSG(_(e_invarg));
10884 return;
10885 }
10886 if (!(dict_find(d, (char_u *)"group", -1) != NULL
10887 && (dict_find(d, (char_u *)"pattern", -1) != NULL
10888 || dict_find(d, (char_u *)"pos1", -1) != NULL)
10889 && dict_find(d, (char_u *)"priority", -1) != NULL
10890 && dict_find(d, (char_u *)"id", -1) != NULL))
10891 {
10892 EMSG(_(e_invarg));
10893 return;
10894 }
10895 li = li->li_next;
10896 }
10897
10898 clear_matches(curwin);
10899 li = l->lv_first;
10900 while (li != NULL)
10901 {
10902 int i = 0;
10903 char_u buf[5];
10904 dictitem_T *di;
10905 char_u *group;
10906 int priority;
10907 int id;
10908 char_u *conceal;
10909
10910 d = li->li_tv.vval.v_dict;
10911 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
10912 {
10913 if (s == NULL)
10914 {
10915 s = list_alloc();
10916 if (s == NULL)
10917 return;
10918 }
10919
10920 /* match from matchaddpos() */
10921 for (i = 1; i < 9; i++)
10922 {
10923 sprintf((char *)buf, (char *)"pos%d", i);
10924 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
10925 {
10926 if (di->di_tv.v_type != VAR_LIST)
10927 return;
10928
10929 list_append_tv(s, &di->di_tv);
10930 s->lv_refcount++;
10931 }
10932 else
10933 break;
10934 }
10935 }
10936
Bram Moolenaar8f667172018-12-14 15:38:31 +010010937 group = dict_get_string(d, (char_u *)"group", TRUE);
10938 priority = (int)dict_get_number(d, (char_u *)"priority");
10939 id = (int)dict_get_number(d, (char_u *)"id");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010940 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar8f667172018-12-14 15:38:31 +010010941 ? dict_get_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010942 : NULL;
10943 if (i == 0)
10944 {
10945 match_add(curwin, group,
Bram Moolenaar8f667172018-12-14 15:38:31 +010010946 dict_get_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010947 priority, id, NULL, conceal);
10948 }
10949 else
10950 {
10951 match_add(curwin, group, NULL, priority, id, s, conceal);
10952 list_unref(s);
10953 s = NULL;
10954 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010955 vim_free(group);
10956 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010957
10958 li = li->li_next;
10959 }
10960 rettv->vval.v_number = 0;
10961 }
10962#endif
10963}
10964
10965/*
10966 * "setpos()" function
10967 */
10968 static void
10969f_setpos(typval_T *argvars, typval_T *rettv)
10970{
10971 pos_T pos;
10972 int fnum;
10973 char_u *name;
10974 colnr_T curswant = -1;
10975
10976 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010977 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010978 if (name != NULL)
10979 {
10980 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
10981 {
10982 if (--pos.col < 0)
10983 pos.col = 0;
10984 if (name[0] == '.' && name[1] == NUL)
10985 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010986 /* set cursor; "fnum" is ignored */
10987 curwin->w_cursor = pos;
10988 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010989 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010990 curwin->w_curswant = curswant - 1;
10991 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010992 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010993 check_cursor();
10994 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010995 }
10996 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
10997 {
10998 /* set mark */
10999 if (setmark_pos(name[1], &pos, fnum) == OK)
11000 rettv->vval.v_number = 0;
11001 }
11002 else
11003 EMSG(_(e_invarg));
11004 }
11005 }
11006}
11007
11008/*
11009 * "setqflist()" function
11010 */
11011 static void
11012f_setqflist(typval_T *argvars, typval_T *rettv)
11013{
Bram Moolenaard823fa92016-08-12 16:29:27 +020011014 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011015}
11016
11017/*
11018 * "setreg()" function
11019 */
11020 static void
11021f_setreg(typval_T *argvars, typval_T *rettv)
11022{
11023 int regname;
11024 char_u *strregname;
11025 char_u *stropt;
11026 char_u *strval;
11027 int append;
11028 char_u yank_type;
11029 long block_len;
11030
11031 block_len = -1;
11032 yank_type = MAUTO;
11033 append = FALSE;
11034
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011035 strregname = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011036 rettv->vval.v_number = 1; /* FAIL is default */
11037
11038 if (strregname == NULL)
11039 return; /* type error; errmsg already given */
11040 regname = *strregname;
11041 if (regname == 0 || regname == '@')
11042 regname = '"';
11043
11044 if (argvars[2].v_type != VAR_UNKNOWN)
11045 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011046 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011047 if (stropt == NULL)
11048 return; /* type error */
11049 for (; *stropt != NUL; ++stropt)
11050 switch (*stropt)
11051 {
11052 case 'a': case 'A': /* append */
11053 append = TRUE;
11054 break;
11055 case 'v': case 'c': /* character-wise selection */
11056 yank_type = MCHAR;
11057 break;
11058 case 'V': case 'l': /* line-wise selection */
11059 yank_type = MLINE;
11060 break;
11061 case 'b': case Ctrl_V: /* block-wise selection */
11062 yank_type = MBLOCK;
11063 if (VIM_ISDIGIT(stropt[1]))
11064 {
11065 ++stropt;
11066 block_len = getdigits(&stropt) - 1;
11067 --stropt;
11068 }
11069 break;
11070 }
11071 }
11072
11073 if (argvars[1].v_type == VAR_LIST)
11074 {
11075 char_u **lstval;
11076 char_u **allocval;
11077 char_u buf[NUMBUFLEN];
11078 char_u **curval;
11079 char_u **curallocval;
11080 list_T *ll = argvars[1].vval.v_list;
11081 listitem_T *li;
11082 int len;
11083
11084 /* If the list is NULL handle like an empty list. */
11085 len = ll == NULL ? 0 : ll->lv_len;
11086
11087 /* First half: use for pointers to result lines; second half: use for
11088 * pointers to allocated copies. */
11089 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
11090 if (lstval == NULL)
11091 return;
11092 curval = lstval;
11093 allocval = lstval + len + 2;
11094 curallocval = allocval;
11095
11096 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
11097 li = li->li_next)
11098 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011099 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011100 if (strval == NULL)
11101 goto free_lstval;
11102 if (strval == buf)
11103 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011104 /* Need to make a copy, next tv_get_string_buf_chk() will
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011105 * overwrite the string. */
11106 strval = vim_strsave(buf);
11107 if (strval == NULL)
11108 goto free_lstval;
11109 *curallocval++ = strval;
11110 }
11111 *curval++ = strval;
11112 }
11113 *curval++ = NULL;
11114
11115 write_reg_contents_lst(regname, lstval, -1,
11116 append, yank_type, block_len);
11117free_lstval:
11118 while (curallocval > allocval)
11119 vim_free(*--curallocval);
11120 vim_free(lstval);
11121 }
11122 else
11123 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011124 strval = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011125 if (strval == NULL)
11126 return;
11127 write_reg_contents_ex(regname, strval, -1,
11128 append, yank_type, block_len);
11129 }
11130 rettv->vval.v_number = 0;
11131}
11132
11133/*
11134 * "settabvar()" function
11135 */
11136 static void
11137f_settabvar(typval_T *argvars, typval_T *rettv)
11138{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011139 tabpage_T *save_curtab;
11140 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011141 char_u *varname, *tabvarname;
11142 typval_T *varp;
11143
11144 rettv->vval.v_number = 0;
11145
11146 if (check_restricted() || check_secure())
11147 return;
11148
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011149 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
11150 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011151 varp = &argvars[2];
11152
Bram Moolenaar4033c552017-09-16 20:54:51 +020011153 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011154 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011155 save_curtab = curtab;
11156 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011157
11158 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
11159 if (tabvarname != NULL)
11160 {
11161 STRCPY(tabvarname, "t:");
11162 STRCPY(tabvarname + 2, varname);
11163 set_var(tabvarname, varp, TRUE);
11164 vim_free(tabvarname);
11165 }
11166
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011167 /* Restore current tabpage */
11168 if (valid_tabpage(save_curtab))
11169 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011170 }
11171}
11172
11173/*
11174 * "settabwinvar()" function
11175 */
11176 static void
11177f_settabwinvar(typval_T *argvars, typval_T *rettv)
11178{
11179 setwinvar(argvars, rettv, 1);
11180}
11181
11182/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011183 * "settagstack()" function
11184 */
11185 static void
11186f_settagstack(typval_T *argvars, typval_T *rettv)
11187{
11188 static char *e_invact2 = N_("E962: Invalid action: '%s'");
11189 win_T *wp;
11190 dict_T *d;
11191 int action = 'r';
11192
11193 rettv->vval.v_number = -1;
11194
11195 // first argument: window number or id
11196 wp = find_win_by_nr_or_id(&argvars[0]);
11197 if (wp == NULL)
11198 return;
11199
11200 // second argument: dict with items to set in the tag stack
11201 if (argvars[1].v_type != VAR_DICT)
11202 {
11203 EMSG(_(e_dictreq));
11204 return;
11205 }
11206 d = argvars[1].vval.v_dict;
11207 if (d == NULL)
11208 return;
11209
11210 // third argument: action - 'a' for append and 'r' for replace.
11211 // default is to replace the stack.
11212 if (argvars[2].v_type == VAR_UNKNOWN)
11213 action = 'r';
11214 else if (argvars[2].v_type == VAR_STRING)
11215 {
11216 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011217 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011218 if (actstr == NULL)
11219 return;
11220 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
11221 action = *actstr;
11222 else
11223 {
11224 EMSG2(_(e_invact2), actstr);
11225 return;
11226 }
11227 }
11228 else
11229 {
11230 EMSG(_(e_stringreq));
11231 return;
11232 }
11233
11234 if (set_tagstack(wp, d, action) == OK)
11235 rettv->vval.v_number = 0;
11236}
11237
11238/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011239 * "setwinvar()" function
11240 */
11241 static void
11242f_setwinvar(typval_T *argvars, typval_T *rettv)
11243{
11244 setwinvar(argvars, rettv, 0);
11245}
11246
11247#ifdef FEAT_CRYPT
11248/*
11249 * "sha256({string})" function
11250 */
11251 static void
11252f_sha256(typval_T *argvars, typval_T *rettv)
11253{
11254 char_u *p;
11255
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011256 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011257 rettv->vval.v_string = vim_strsave(
11258 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
11259 rettv->v_type = VAR_STRING;
11260}
11261#endif /* FEAT_CRYPT */
11262
11263/*
11264 * "shellescape({string})" function
11265 */
11266 static void
11267f_shellescape(typval_T *argvars, typval_T *rettv)
11268{
Bram Moolenaar20615522017-06-05 18:46:26 +020011269 int do_special = non_zero_arg(&argvars[1]);
11270
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011271 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011272 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011273 rettv->v_type = VAR_STRING;
11274}
11275
11276/*
11277 * shiftwidth() function
11278 */
11279 static void
11280f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
11281{
Bram Moolenaarf9514162018-11-22 03:08:29 +010011282 rettv->vval.v_number = 0;
11283
11284 if (argvars[0].v_type != VAR_UNKNOWN)
11285 {
11286 long col;
11287
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011288 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +010011289 if (col < 0)
11290 return; // type error; errmsg already given
11291#ifdef FEAT_VARTABS
11292 rettv->vval.v_number = get_sw_value_col(curbuf, col);
11293 return;
11294#endif
11295 }
11296
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011297 rettv->vval.v_number = get_sw_value(curbuf);
11298}
11299
Bram Moolenaar162b7142018-12-21 15:17:36 +010011300#ifdef FEAT_SIGNS
11301/*
11302 * "sign_define()" function
11303 */
11304 static void
11305f_sign_define(typval_T *argvars, typval_T *rettv)
11306{
11307 char_u *name;
11308 dict_T *dict;
11309 char_u *icon = NULL;
11310 char_u *linehl = NULL;
11311 char_u *text = NULL;
11312 char_u *texthl = NULL;
11313
11314 rettv->vval.v_number = -1;
11315
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011316 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011317 if (name == NULL)
11318 return;
11319
11320 if (argvars[1].v_type != VAR_UNKNOWN)
11321 {
11322 if (argvars[1].v_type != VAR_DICT)
11323 {
11324 EMSG(_(e_dictreq));
11325 return;
11326 }
11327
11328 // sign attributes
11329 dict = argvars[1].vval.v_dict;
11330 if (dict_find(dict, (char_u *)"icon", -1) != NULL)
11331 icon = dict_get_string(dict, (char_u *)"icon", TRUE);
11332 if (dict_find(dict, (char_u *)"linehl", -1) != NULL)
11333 linehl = dict_get_string(dict, (char_u *)"linehl", TRUE);
11334 if (dict_find(dict, (char_u *)"text", -1) != NULL)
11335 text = dict_get_string(dict, (char_u *)"text", TRUE);
11336 if (dict_find(dict, (char_u *)"texthl", -1) != NULL)
11337 texthl = dict_get_string(dict, (char_u *)"texthl", TRUE);
11338 }
11339
11340 if (sign_define_by_name(name, icon, linehl, text, texthl) == OK)
11341 rettv->vval.v_number = 0;
11342
11343 vim_free(icon);
11344 vim_free(linehl);
11345 vim_free(text);
11346 vim_free(texthl);
11347}
11348
11349/*
11350 * "sign_getdefined()" function
11351 */
11352 static void
11353f_sign_getdefined(typval_T *argvars, typval_T *rettv)
11354{
11355 char_u *name = NULL;
11356
11357 if (rettv_list_alloc_id(rettv, aid_sign_getdefined) != OK)
11358 return;
11359
11360 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011361 name = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011362
11363 sign_getlist(name, rettv->vval.v_list);
11364}
11365
11366/*
11367 * "sign_getplaced()" function
11368 */
11369 static void
11370f_sign_getplaced(typval_T *argvars, typval_T *rettv)
11371{
11372 buf_T *buf = NULL;
11373 dict_T *dict;
11374 dictitem_T *di;
11375 linenr_T lnum = 0;
11376 int sign_id = 0;
11377 char_u *group = NULL;
11378 int notanum = FALSE;
11379
11380 if (rettv_list_alloc_id(rettv, aid_sign_getplaced) != OK)
11381 return;
11382
11383 if (argvars[0].v_type != VAR_UNKNOWN)
11384 {
11385 // get signs placed in this buffer
11386 buf = find_buffer(&argvars[0]);
11387 if (buf == NULL)
11388 {
11389 EMSG2(_("E158: Invalid buffer name: %s"),
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011390 tv_get_string(&argvars[0]));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011391 return;
11392 }
11393
11394 if (argvars[1].v_type != VAR_UNKNOWN)
11395 {
11396 if (argvars[1].v_type != VAR_DICT ||
11397 ((dict = argvars[1].vval.v_dict) == NULL))
11398 {
11399 EMSG(_(e_dictreq));
11400 return;
11401 }
11402 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
11403 {
11404 // get signs placed at this line
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011405 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011406 if (notanum)
11407 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011408 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011409 }
11410 if ((di = dict_find(dict, (char_u *)"id", -1)) != NULL)
11411 {
11412 // get sign placed with this identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011413 sign_id = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011414 if (notanum)
11415 return;
11416 }
11417 if ((di = dict_find(dict, (char_u *)"group", -1)) != NULL)
11418 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011419 group = tv_get_string_chk(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011420 if (group == NULL)
11421 return;
Bram Moolenaar6436cd82018-12-27 00:28:33 +010011422 if (*group == '\0') // empty string means global group
11423 group = NULL;
Bram Moolenaar162b7142018-12-21 15:17:36 +010011424 }
11425 }
11426 }
11427
11428 sign_get_placed(buf, lnum, sign_id, group, rettv->vval.v_list);
11429}
11430
11431/*
11432 * "sign_place()" function
11433 */
11434 static void
11435f_sign_place(typval_T *argvars, typval_T *rettv)
11436{
11437 int sign_id;
11438 char_u *group = NULL;
11439 char_u *sign_name;
11440 buf_T *buf;
11441 dict_T *dict;
11442 dictitem_T *di;
11443 linenr_T lnum = 0;
11444 int prio = SIGN_DEF_PRIO;
11445 int notanum = FALSE;
11446
11447 rettv->vval.v_number = -1;
11448
11449 // Sign identifer
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011450 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011451 if (notanum)
11452 return;
11453 if (sign_id < 0)
11454 {
11455 EMSG(_(e_invarg));
11456 return;
11457 }
11458
11459 // Sign group
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011460 group = tv_get_string_chk(&argvars[1]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011461 if (group == NULL)
11462 return;
11463 if (group[0] == '\0')
11464 group = NULL; // global sign group
11465 else
11466 {
11467 group = vim_strsave(group);
11468 if (group == NULL)
11469 return;
11470 }
11471
11472 // Sign name
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011473 sign_name = tv_get_string_chk(&argvars[2]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011474 if (sign_name == NULL)
11475 goto cleanup;
11476
11477 // Buffer to place the sign
11478 buf = find_buffer(&argvars[3]);
11479 if (buf == NULL)
11480 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011481 EMSG2(_("E158: Invalid buffer name: %s"), tv_get_string(&argvars[2]));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011482 goto cleanup;
11483 }
11484
11485 if (argvars[4].v_type != VAR_UNKNOWN)
11486 {
11487 if (argvars[4].v_type != VAR_DICT ||
11488 ((dict = argvars[4].vval.v_dict) == NULL))
11489 {
11490 EMSG(_(e_dictreq));
11491 goto cleanup;
11492 }
11493
11494 // Line number where the sign is to be placed
11495 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
11496 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011497 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011498 if (notanum)
11499 goto cleanup;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011500 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011501 }
11502 if ((di = dict_find(dict, (char_u *)"priority", -1)) != NULL)
11503 {
11504 // Sign priority
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011505 prio = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011506 if (notanum)
11507 goto cleanup;
11508 }
11509 }
11510
11511 if (sign_place(&sign_id, group, sign_name, buf, lnum, prio) == OK)
11512 rettv->vval.v_number = sign_id;
11513
11514cleanup:
11515 vim_free(group);
11516}
11517
11518/*
11519 * "sign_undefine()" function
11520 */
11521 static void
11522f_sign_undefine(typval_T *argvars, typval_T *rettv)
11523{
11524 char_u *name;
11525
11526 rettv->vval.v_number = -1;
11527
11528 if (argvars[0].v_type == VAR_UNKNOWN)
11529 {
11530 // Free all the signs
11531 free_signs();
11532 rettv->vval.v_number = 0;
11533 }
11534 else
11535 {
11536 // Free only the specified sign
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011537 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011538 if (name == NULL)
11539 return;
11540
11541 if (sign_undefine_by_name(name) == OK)
11542 rettv->vval.v_number = 0;
11543 }
11544}
11545
11546/*
11547 * "sign_unplace()" function
11548 */
11549 static void
11550f_sign_unplace(typval_T *argvars, typval_T *rettv)
11551{
11552 dict_T *dict;
11553 dictitem_T *di;
11554 int sign_id = 0;
11555 buf_T *buf = NULL;
11556 char_u *group = NULL;
11557
11558 rettv->vval.v_number = -1;
11559
11560 if (argvars[0].v_type != VAR_STRING)
11561 {
11562 EMSG(_(e_invarg));
11563 return;
11564 }
11565
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011566 group = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011567 if (group[0] == '\0')
11568 group = NULL; // global sign group
11569 else
11570 {
11571 group = vim_strsave(group);
11572 if (group == NULL)
11573 return;
11574 }
11575
11576 if (argvars[1].v_type != VAR_UNKNOWN)
11577 {
11578 if (argvars[1].v_type != VAR_DICT)
11579 {
11580 EMSG(_(e_dictreq));
11581 return;
11582 }
11583 dict = argvars[1].vval.v_dict;
11584
11585 if ((di = dict_find(dict, (char_u *)"buffer", -1)) != NULL)
11586 {
11587 buf = find_buffer(&di->di_tv);
11588 if (buf == NULL)
11589 {
11590 EMSG2(_("E158: Invalid buffer name: %s"),
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011591 tv_get_string(&di->di_tv));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011592 return;
11593 }
11594 }
11595 if (dict_find(dict, (char_u *)"id", -1) != NULL)
11596 sign_id = dict_get_number(dict, (char_u *)"id");
11597 }
11598
11599 if (buf == NULL)
11600 {
11601 // Delete the sign in all the buffers
11602 FOR_ALL_BUFFERS(buf)
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010011603 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010011604 rettv->vval.v_number = 0;
11605 }
11606 else
11607 {
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010011608 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010011609 rettv->vval.v_number = 0;
11610 }
11611 vim_free(group);
11612}
11613#endif
11614
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011615/*
11616 * "simplify()" function
11617 */
11618 static void
11619f_simplify(typval_T *argvars, typval_T *rettv)
11620{
11621 char_u *p;
11622
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011623 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011624 rettv->vval.v_string = vim_strsave(p);
11625 simplify_filename(rettv->vval.v_string); /* simplify in place */
11626 rettv->v_type = VAR_STRING;
11627}
11628
11629#ifdef FEAT_FLOAT
11630/*
11631 * "sin()" function
11632 */
11633 static void
11634f_sin(typval_T *argvars, typval_T *rettv)
11635{
11636 float_T f = 0.0;
11637
11638 rettv->v_type = VAR_FLOAT;
11639 if (get_float_arg(argvars, &f) == OK)
11640 rettv->vval.v_float = sin(f);
11641 else
11642 rettv->vval.v_float = 0.0;
11643}
11644
11645/*
11646 * "sinh()" function
11647 */
11648 static void
11649f_sinh(typval_T *argvars, typval_T *rettv)
11650{
11651 float_T f = 0.0;
11652
11653 rettv->v_type = VAR_FLOAT;
11654 if (get_float_arg(argvars, &f) == OK)
11655 rettv->vval.v_float = sinh(f);
11656 else
11657 rettv->vval.v_float = 0.0;
11658}
11659#endif
11660
11661static int
11662#ifdef __BORLANDC__
11663 _RTLENTRYF
11664#endif
11665 item_compare(const void *s1, const void *s2);
11666static int
11667#ifdef __BORLANDC__
11668 _RTLENTRYF
11669#endif
11670 item_compare2(const void *s1, const void *s2);
11671
11672/* struct used in the array that's given to qsort() */
11673typedef struct
11674{
11675 listitem_T *item;
11676 int idx;
11677} sortItem_T;
11678
11679/* struct storing information about current sort */
11680typedef struct
11681{
11682 int item_compare_ic;
11683 int item_compare_numeric;
11684 int item_compare_numbers;
11685#ifdef FEAT_FLOAT
11686 int item_compare_float;
11687#endif
11688 char_u *item_compare_func;
11689 partial_T *item_compare_partial;
11690 dict_T *item_compare_selfdict;
11691 int item_compare_func_err;
11692 int item_compare_keep_zero;
11693} sortinfo_T;
11694static sortinfo_T *sortinfo = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011695#define ITEM_COMPARE_FAIL 999
11696
11697/*
11698 * Compare functions for f_sort() and f_uniq() below.
11699 */
11700 static int
11701#ifdef __BORLANDC__
11702_RTLENTRYF
11703#endif
11704item_compare(const void *s1, const void *s2)
11705{
11706 sortItem_T *si1, *si2;
11707 typval_T *tv1, *tv2;
11708 char_u *p1, *p2;
11709 char_u *tofree1 = NULL, *tofree2 = NULL;
11710 int res;
11711 char_u numbuf1[NUMBUFLEN];
11712 char_u numbuf2[NUMBUFLEN];
11713
11714 si1 = (sortItem_T *)s1;
11715 si2 = (sortItem_T *)s2;
11716 tv1 = &si1->item->li_tv;
11717 tv2 = &si2->item->li_tv;
11718
11719 if (sortinfo->item_compare_numbers)
11720 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011721 varnumber_T v1 = tv_get_number(tv1);
11722 varnumber_T v2 = tv_get_number(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011723
11724 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11725 }
11726
11727#ifdef FEAT_FLOAT
11728 if (sortinfo->item_compare_float)
11729 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011730 float_T v1 = tv_get_float(tv1);
11731 float_T v2 = tv_get_float(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011732
11733 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11734 }
11735#endif
11736
11737 /* tv2string() puts quotes around a string and allocates memory. Don't do
11738 * that for string variables. Use a single quote when comparing with a
11739 * non-string to do what the docs promise. */
11740 if (tv1->v_type == VAR_STRING)
11741 {
11742 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11743 p1 = (char_u *)"'";
11744 else
11745 p1 = tv1->vval.v_string;
11746 }
11747 else
11748 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
11749 if (tv2->v_type == VAR_STRING)
11750 {
11751 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11752 p2 = (char_u *)"'";
11753 else
11754 p2 = tv2->vval.v_string;
11755 }
11756 else
11757 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
11758 if (p1 == NULL)
11759 p1 = (char_u *)"";
11760 if (p2 == NULL)
11761 p2 = (char_u *)"";
11762 if (!sortinfo->item_compare_numeric)
11763 {
11764 if (sortinfo->item_compare_ic)
11765 res = STRICMP(p1, p2);
11766 else
11767 res = STRCMP(p1, p2);
11768 }
11769 else
11770 {
11771 double n1, n2;
11772 n1 = strtod((char *)p1, (char **)&p1);
11773 n2 = strtod((char *)p2, (char **)&p2);
11774 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
11775 }
11776
11777 /* When the result would be zero, compare the item indexes. Makes the
11778 * sort stable. */
11779 if (res == 0 && !sortinfo->item_compare_keep_zero)
11780 res = si1->idx > si2->idx ? 1 : -1;
11781
11782 vim_free(tofree1);
11783 vim_free(tofree2);
11784 return res;
11785}
11786
11787 static int
11788#ifdef __BORLANDC__
11789_RTLENTRYF
11790#endif
11791item_compare2(const void *s1, const void *s2)
11792{
11793 sortItem_T *si1, *si2;
11794 int res;
11795 typval_T rettv;
11796 typval_T argv[3];
11797 int dummy;
11798 char_u *func_name;
11799 partial_T *partial = sortinfo->item_compare_partial;
11800
11801 /* shortcut after failure in previous call; compare all items equal */
11802 if (sortinfo->item_compare_func_err)
11803 return 0;
11804
11805 si1 = (sortItem_T *)s1;
11806 si2 = (sortItem_T *)s2;
11807
11808 if (partial == NULL)
11809 func_name = sortinfo->item_compare_func;
11810 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020011811 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011812
11813 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
11814 * in the copy without changing the original list items. */
11815 copy_tv(&si1->item->li_tv, &argv[0]);
11816 copy_tv(&si2->item->li_tv, &argv[1]);
11817
11818 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
11819 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020011820 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011821 partial, sortinfo->item_compare_selfdict);
11822 clear_tv(&argv[0]);
11823 clear_tv(&argv[1]);
11824
11825 if (res == FAIL)
11826 res = ITEM_COMPARE_FAIL;
11827 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011828 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011829 if (sortinfo->item_compare_func_err)
11830 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
11831 clear_tv(&rettv);
11832
11833 /* When the result would be zero, compare the pointers themselves. Makes
11834 * the sort stable. */
11835 if (res == 0 && !sortinfo->item_compare_keep_zero)
11836 res = si1->idx > si2->idx ? 1 : -1;
11837
11838 return res;
11839}
11840
11841/*
11842 * "sort({list})" function
11843 */
11844 static void
11845do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
11846{
11847 list_T *l;
11848 listitem_T *li;
11849 sortItem_T *ptrs;
11850 sortinfo_T *old_sortinfo;
11851 sortinfo_T info;
11852 long len;
11853 long i;
11854
11855 /* Pointer to current info struct used in compare function. Save and
11856 * restore the current one for nested calls. */
11857 old_sortinfo = sortinfo;
11858 sortinfo = &info;
11859
11860 if (argvars[0].v_type != VAR_LIST)
11861 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
11862 else
11863 {
11864 l = argvars[0].vval.v_list;
11865 if (l == NULL || tv_check_lock(l->lv_lock,
11866 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
11867 TRUE))
11868 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011869 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011870
11871 len = list_len(l);
11872 if (len <= 1)
11873 goto theend; /* short list sorts pretty quickly */
11874
11875 info.item_compare_ic = FALSE;
11876 info.item_compare_numeric = FALSE;
11877 info.item_compare_numbers = FALSE;
11878#ifdef FEAT_FLOAT
11879 info.item_compare_float = FALSE;
11880#endif
11881 info.item_compare_func = NULL;
11882 info.item_compare_partial = NULL;
11883 info.item_compare_selfdict = NULL;
11884 if (argvars[1].v_type != VAR_UNKNOWN)
11885 {
11886 /* optional second argument: {func} */
11887 if (argvars[1].v_type == VAR_FUNC)
11888 info.item_compare_func = argvars[1].vval.v_string;
11889 else if (argvars[1].v_type == VAR_PARTIAL)
11890 info.item_compare_partial = argvars[1].vval.v_partial;
11891 else
11892 {
11893 int error = FALSE;
11894
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011895 i = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011896 if (error)
11897 goto theend; /* type error; errmsg already given */
11898 if (i == 1)
11899 info.item_compare_ic = TRUE;
11900 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011901 info.item_compare_func = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011902 else if (i != 0)
11903 {
11904 EMSG(_(e_invarg));
11905 goto theend;
11906 }
11907 if (info.item_compare_func != NULL)
11908 {
11909 if (*info.item_compare_func == NUL)
11910 {
11911 /* empty string means default sort */
11912 info.item_compare_func = NULL;
11913 }
11914 else if (STRCMP(info.item_compare_func, "n") == 0)
11915 {
11916 info.item_compare_func = NULL;
11917 info.item_compare_numeric = TRUE;
11918 }
11919 else if (STRCMP(info.item_compare_func, "N") == 0)
11920 {
11921 info.item_compare_func = NULL;
11922 info.item_compare_numbers = TRUE;
11923 }
11924#ifdef FEAT_FLOAT
11925 else if (STRCMP(info.item_compare_func, "f") == 0)
11926 {
11927 info.item_compare_func = NULL;
11928 info.item_compare_float = TRUE;
11929 }
11930#endif
11931 else if (STRCMP(info.item_compare_func, "i") == 0)
11932 {
11933 info.item_compare_func = NULL;
11934 info.item_compare_ic = TRUE;
11935 }
11936 }
11937 }
11938
11939 if (argvars[2].v_type != VAR_UNKNOWN)
11940 {
11941 /* optional third argument: {dict} */
11942 if (argvars[2].v_type != VAR_DICT)
11943 {
11944 EMSG(_(e_dictreq));
11945 goto theend;
11946 }
11947 info.item_compare_selfdict = argvars[2].vval.v_dict;
11948 }
11949 }
11950
11951 /* Make an array with each entry pointing to an item in the List. */
11952 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
11953 if (ptrs == NULL)
11954 goto theend;
11955
11956 i = 0;
11957 if (sort)
11958 {
11959 /* sort(): ptrs will be the list to sort */
11960 for (li = l->lv_first; li != NULL; li = li->li_next)
11961 {
11962 ptrs[i].item = li;
11963 ptrs[i].idx = i;
11964 ++i;
11965 }
11966
11967 info.item_compare_func_err = FALSE;
11968 info.item_compare_keep_zero = FALSE;
11969 /* test the compare function */
11970 if ((info.item_compare_func != NULL
11971 || info.item_compare_partial != NULL)
11972 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
11973 == ITEM_COMPARE_FAIL)
11974 EMSG(_("E702: Sort compare function failed"));
11975 else
11976 {
11977 /* Sort the array with item pointers. */
11978 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
11979 info.item_compare_func == NULL
11980 && info.item_compare_partial == NULL
11981 ? item_compare : item_compare2);
11982
11983 if (!info.item_compare_func_err)
11984 {
11985 /* Clear the List and append the items in sorted order. */
11986 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
11987 l->lv_len = 0;
11988 for (i = 0; i < len; ++i)
11989 list_append(l, ptrs[i].item);
11990 }
11991 }
11992 }
11993 else
11994 {
11995 int (*item_compare_func_ptr)(const void *, const void *);
11996
11997 /* f_uniq(): ptrs will be a stack of items to remove */
11998 info.item_compare_func_err = FALSE;
11999 info.item_compare_keep_zero = TRUE;
12000 item_compare_func_ptr = info.item_compare_func != NULL
12001 || info.item_compare_partial != NULL
12002 ? item_compare2 : item_compare;
12003
12004 for (li = l->lv_first; li != NULL && li->li_next != NULL;
12005 li = li->li_next)
12006 {
12007 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
12008 == 0)
12009 ptrs[i++].item = li;
12010 if (info.item_compare_func_err)
12011 {
12012 EMSG(_("E882: Uniq compare function failed"));
12013 break;
12014 }
12015 }
12016
12017 if (!info.item_compare_func_err)
12018 {
12019 while (--i >= 0)
12020 {
12021 li = ptrs[i].item->li_next;
12022 ptrs[i].item->li_next = li->li_next;
12023 if (li->li_next != NULL)
12024 li->li_next->li_prev = ptrs[i].item;
12025 else
12026 l->lv_last = ptrs[i].item;
12027 list_fix_watch(l, li);
12028 listitem_free(li);
12029 l->lv_len--;
12030 }
12031 }
12032 }
12033
12034 vim_free(ptrs);
12035 }
12036theend:
12037 sortinfo = old_sortinfo;
12038}
12039
12040/*
12041 * "sort({list})" function
12042 */
12043 static void
12044f_sort(typval_T *argvars, typval_T *rettv)
12045{
12046 do_sort_uniq(argvars, rettv, TRUE);
12047}
12048
12049/*
12050 * "uniq({list})" function
12051 */
12052 static void
12053f_uniq(typval_T *argvars, typval_T *rettv)
12054{
12055 do_sort_uniq(argvars, rettv, FALSE);
12056}
12057
12058/*
12059 * "soundfold({word})" function
12060 */
12061 static void
12062f_soundfold(typval_T *argvars, typval_T *rettv)
12063{
12064 char_u *s;
12065
12066 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012067 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012068#ifdef FEAT_SPELL
12069 rettv->vval.v_string = eval_soundfold(s);
12070#else
12071 rettv->vval.v_string = vim_strsave(s);
12072#endif
12073}
12074
12075/*
12076 * "spellbadword()" function
12077 */
12078 static void
12079f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
12080{
12081 char_u *word = (char_u *)"";
12082 hlf_T attr = HLF_COUNT;
12083 int len = 0;
12084
12085 if (rettv_list_alloc(rettv) == FAIL)
12086 return;
12087
12088#ifdef FEAT_SPELL
12089 if (argvars[0].v_type == VAR_UNKNOWN)
12090 {
12091 /* Find the start and length of the badly spelled word. */
12092 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
12093 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012094 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012095 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012096 curwin->w_set_curswant = TRUE;
12097 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012098 }
12099 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
12100 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012101 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012102 int capcol = -1;
12103
12104 if (str != NULL)
12105 {
12106 /* Check the argument for spelling. */
12107 while (*str != NUL)
12108 {
12109 len = spell_check(curwin, str, &attr, &capcol, FALSE);
12110 if (attr != HLF_COUNT)
12111 {
12112 word = str;
12113 break;
12114 }
12115 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020012116 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012117 }
12118 }
12119 }
12120#endif
12121
12122 list_append_string(rettv->vval.v_list, word, len);
12123 list_append_string(rettv->vval.v_list, (char_u *)(
12124 attr == HLF_SPB ? "bad" :
12125 attr == HLF_SPR ? "rare" :
12126 attr == HLF_SPL ? "local" :
12127 attr == HLF_SPC ? "caps" :
12128 ""), -1);
12129}
12130
12131/*
12132 * "spellsuggest()" function
12133 */
12134 static void
12135f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
12136{
12137#ifdef FEAT_SPELL
12138 char_u *str;
12139 int typeerr = FALSE;
12140 int maxcount;
12141 garray_T ga;
12142 int i;
12143 listitem_T *li;
12144 int need_capital = FALSE;
12145#endif
12146
12147 if (rettv_list_alloc(rettv) == FAIL)
12148 return;
12149
12150#ifdef FEAT_SPELL
12151 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
12152 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012153 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012154 if (argvars[1].v_type != VAR_UNKNOWN)
12155 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012156 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012157 if (maxcount <= 0)
12158 return;
12159 if (argvars[2].v_type != VAR_UNKNOWN)
12160 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012161 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012162 if (typeerr)
12163 return;
12164 }
12165 }
12166 else
12167 maxcount = 25;
12168
12169 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
12170
12171 for (i = 0; i < ga.ga_len; ++i)
12172 {
12173 str = ((char_u **)ga.ga_data)[i];
12174
12175 li = listitem_alloc();
12176 if (li == NULL)
12177 vim_free(str);
12178 else
12179 {
12180 li->li_tv.v_type = VAR_STRING;
12181 li->li_tv.v_lock = 0;
12182 li->li_tv.vval.v_string = str;
12183 list_append(rettv->vval.v_list, li);
12184 }
12185 }
12186 ga_clear(&ga);
12187 }
12188#endif
12189}
12190
12191 static void
12192f_split(typval_T *argvars, typval_T *rettv)
12193{
12194 char_u *str;
12195 char_u *end;
12196 char_u *pat = NULL;
12197 regmatch_T regmatch;
12198 char_u patbuf[NUMBUFLEN];
12199 char_u *save_cpo;
12200 int match;
12201 colnr_T col = 0;
12202 int keepempty = FALSE;
12203 int typeerr = FALSE;
12204
12205 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12206 save_cpo = p_cpo;
12207 p_cpo = (char_u *)"";
12208
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012209 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012210 if (argvars[1].v_type != VAR_UNKNOWN)
12211 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012212 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012213 if (pat == NULL)
12214 typeerr = TRUE;
12215 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012216 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012217 }
12218 if (pat == NULL || *pat == NUL)
12219 pat = (char_u *)"[\\x01- ]\\+";
12220
12221 if (rettv_list_alloc(rettv) == FAIL)
12222 return;
12223 if (typeerr)
12224 return;
12225
12226 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12227 if (regmatch.regprog != NULL)
12228 {
12229 regmatch.rm_ic = FALSE;
12230 while (*str != NUL || keepempty)
12231 {
12232 if (*str == NUL)
12233 match = FALSE; /* empty item at the end */
12234 else
12235 match = vim_regexec_nl(&regmatch, str, col);
12236 if (match)
12237 end = regmatch.startp[0];
12238 else
12239 end = str + STRLEN(str);
12240 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
12241 && *str != NUL && match && end < regmatch.endp[0]))
12242 {
12243 if (list_append_string(rettv->vval.v_list, str,
12244 (int)(end - str)) == FAIL)
12245 break;
12246 }
12247 if (!match)
12248 break;
12249 /* Advance to just after the match. */
12250 if (regmatch.endp[0] > str)
12251 col = 0;
12252 else
12253 {
12254 /* Don't get stuck at the same match. */
12255#ifdef FEAT_MBYTE
12256 col = (*mb_ptr2len)(regmatch.endp[0]);
12257#else
12258 col = 1;
12259#endif
12260 }
12261 str = regmatch.endp[0];
12262 }
12263
12264 vim_regfree(regmatch.regprog);
12265 }
12266
12267 p_cpo = save_cpo;
12268}
12269
12270#ifdef FEAT_FLOAT
12271/*
12272 * "sqrt()" function
12273 */
12274 static void
12275f_sqrt(typval_T *argvars, typval_T *rettv)
12276{
12277 float_T f = 0.0;
12278
12279 rettv->v_type = VAR_FLOAT;
12280 if (get_float_arg(argvars, &f) == OK)
12281 rettv->vval.v_float = sqrt(f);
12282 else
12283 rettv->vval.v_float = 0.0;
12284}
12285
12286/*
12287 * "str2float()" function
12288 */
12289 static void
12290f_str2float(typval_T *argvars, typval_T *rettv)
12291{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012292 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010012293 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012294
Bram Moolenaar08243d22017-01-10 16:12:29 +010012295 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012296 p = skipwhite(p + 1);
12297 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010012298 if (isneg)
12299 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012300 rettv->v_type = VAR_FLOAT;
12301}
12302#endif
12303
12304/*
12305 * "str2nr()" function
12306 */
12307 static void
12308f_str2nr(typval_T *argvars, typval_T *rettv)
12309{
12310 int base = 10;
12311 char_u *p;
12312 varnumber_T n;
12313 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010012314 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012315
12316 if (argvars[1].v_type != VAR_UNKNOWN)
12317 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012318 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012319 if (base != 2 && base != 8 && base != 10 && base != 16)
12320 {
12321 EMSG(_(e_invarg));
12322 return;
12323 }
12324 }
12325
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012326 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010012327 isneg = (*p == '-');
12328 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012329 p = skipwhite(p + 1);
12330 switch (base)
12331 {
12332 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
12333 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
12334 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
12335 default: what = 0;
12336 }
12337 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010012338 if (isneg)
12339 rettv->vval.v_number = -n;
12340 else
12341 rettv->vval.v_number = n;
12342
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012343}
12344
12345#ifdef HAVE_STRFTIME
12346/*
12347 * "strftime({format}[, {time}])" function
12348 */
12349 static void
12350f_strftime(typval_T *argvars, typval_T *rettv)
12351{
12352 char_u result_buf[256];
12353 struct tm *curtime;
12354 time_t seconds;
12355 char_u *p;
12356
12357 rettv->v_type = VAR_STRING;
12358
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012359 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012360 if (argvars[1].v_type == VAR_UNKNOWN)
12361 seconds = time(NULL);
12362 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012363 seconds = (time_t)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012364 curtime = localtime(&seconds);
12365 /* MSVC returns NULL for an invalid value of seconds. */
12366 if (curtime == NULL)
12367 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
12368 else
12369 {
12370# ifdef FEAT_MBYTE
12371 vimconv_T conv;
12372 char_u *enc;
12373
12374 conv.vc_type = CONV_NONE;
12375 enc = enc_locale();
12376 convert_setup(&conv, p_enc, enc);
12377 if (conv.vc_type != CONV_NONE)
12378 p = string_convert(&conv, p, NULL);
12379# endif
12380 if (p != NULL)
12381 (void)strftime((char *)result_buf, sizeof(result_buf),
12382 (char *)p, curtime);
12383 else
12384 result_buf[0] = NUL;
12385
12386# ifdef FEAT_MBYTE
12387 if (conv.vc_type != CONV_NONE)
12388 vim_free(p);
12389 convert_setup(&conv, enc, p_enc);
12390 if (conv.vc_type != CONV_NONE)
12391 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
12392 else
12393# endif
12394 rettv->vval.v_string = vim_strsave(result_buf);
12395
12396# ifdef FEAT_MBYTE
12397 /* Release conversion descriptors */
12398 convert_setup(&conv, NULL, NULL);
12399 vim_free(enc);
12400# endif
12401 }
12402}
12403#endif
12404
12405/*
12406 * "strgetchar()" function
12407 */
12408 static void
12409f_strgetchar(typval_T *argvars, typval_T *rettv)
12410{
12411 char_u *str;
12412 int len;
12413 int error = FALSE;
12414 int charidx;
12415
12416 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012417 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012418 if (str == NULL)
12419 return;
12420 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012421 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012422 if (error)
12423 return;
12424#ifdef FEAT_MBYTE
12425 {
12426 int byteidx = 0;
12427
12428 while (charidx >= 0 && byteidx < len)
12429 {
12430 if (charidx == 0)
12431 {
12432 rettv->vval.v_number = mb_ptr2char(str + byteidx);
12433 break;
12434 }
12435 --charidx;
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012436 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012437 }
12438 }
12439#else
12440 if (charidx < len)
12441 rettv->vval.v_number = str[charidx];
12442#endif
12443}
12444
12445/*
12446 * "stridx()" function
12447 */
12448 static void
12449f_stridx(typval_T *argvars, typval_T *rettv)
12450{
12451 char_u buf[NUMBUFLEN];
12452 char_u *needle;
12453 char_u *haystack;
12454 char_u *save_haystack;
12455 char_u *pos;
12456 int start_idx;
12457
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012458 needle = tv_get_string_chk(&argvars[1]);
12459 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012460 rettv->vval.v_number = -1;
12461 if (needle == NULL || haystack == NULL)
12462 return; /* type error; errmsg already given */
12463
12464 if (argvars[2].v_type != VAR_UNKNOWN)
12465 {
12466 int error = FALSE;
12467
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012468 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012469 if (error || start_idx >= (int)STRLEN(haystack))
12470 return;
12471 if (start_idx >= 0)
12472 haystack += start_idx;
12473 }
12474
12475 pos = (char_u *)strstr((char *)haystack, (char *)needle);
12476 if (pos != NULL)
12477 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
12478}
12479
12480/*
12481 * "string()" function
12482 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010012483 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012484f_string(typval_T *argvars, typval_T *rettv)
12485{
12486 char_u *tofree;
12487 char_u numbuf[NUMBUFLEN];
12488
12489 rettv->v_type = VAR_STRING;
12490 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
12491 get_copyID());
12492 /* Make a copy if we have a value but it's not in allocated memory. */
12493 if (rettv->vval.v_string != NULL && tofree == NULL)
12494 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
12495}
12496
12497/*
12498 * "strlen()" function
12499 */
12500 static void
12501f_strlen(typval_T *argvars, typval_T *rettv)
12502{
12503 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012504 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012505}
12506
12507/*
12508 * "strchars()" function
12509 */
12510 static void
12511f_strchars(typval_T *argvars, typval_T *rettv)
12512{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012513 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012514 int skipcc = 0;
12515#ifdef FEAT_MBYTE
12516 varnumber_T len = 0;
12517 int (*func_mb_ptr2char_adv)(char_u **pp);
12518#endif
12519
12520 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012521 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012522 if (skipcc < 0 || skipcc > 1)
12523 EMSG(_(e_invarg));
12524 else
12525 {
12526#ifdef FEAT_MBYTE
12527 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
12528 while (*s != NUL)
12529 {
12530 func_mb_ptr2char_adv(&s);
12531 ++len;
12532 }
12533 rettv->vval.v_number = len;
12534#else
12535 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
12536#endif
12537 }
12538}
12539
12540/*
12541 * "strdisplaywidth()" function
12542 */
12543 static void
12544f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
12545{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012546 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012547 int col = 0;
12548
12549 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012550 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012551
12552 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
12553}
12554
12555/*
12556 * "strwidth()" function
12557 */
12558 static void
12559f_strwidth(typval_T *argvars, typval_T *rettv)
12560{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012561 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012562
12563 rettv->vval.v_number = (varnumber_T)(
12564#ifdef FEAT_MBYTE
12565 mb_string2cells(s, -1)
12566#else
12567 STRLEN(s)
12568#endif
12569 );
12570}
12571
12572/*
12573 * "strcharpart()" function
12574 */
12575 static void
12576f_strcharpart(typval_T *argvars, typval_T *rettv)
12577{
12578#ifdef FEAT_MBYTE
12579 char_u *p;
12580 int nchar;
12581 int nbyte = 0;
12582 int charlen;
12583 int len = 0;
12584 int slen;
12585 int error = FALSE;
12586
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012587 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012588 slen = (int)STRLEN(p);
12589
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012590 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012591 if (!error)
12592 {
12593 if (nchar > 0)
12594 while (nchar > 0 && nbyte < slen)
12595 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012596 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012597 --nchar;
12598 }
12599 else
12600 nbyte = nchar;
12601 if (argvars[2].v_type != VAR_UNKNOWN)
12602 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012603 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012604 while (charlen > 0 && nbyte + len < slen)
12605 {
12606 int off = nbyte + len;
12607
12608 if (off < 0)
12609 len += 1;
12610 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012611 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012612 --charlen;
12613 }
12614 }
12615 else
12616 len = slen - nbyte; /* default: all bytes that are available. */
12617 }
12618
12619 /*
12620 * Only return the overlap between the specified part and the actual
12621 * string.
12622 */
12623 if (nbyte < 0)
12624 {
12625 len += nbyte;
12626 nbyte = 0;
12627 }
12628 else if (nbyte > slen)
12629 nbyte = slen;
12630 if (len < 0)
12631 len = 0;
12632 else if (nbyte + len > slen)
12633 len = slen - nbyte;
12634
12635 rettv->v_type = VAR_STRING;
12636 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
12637#else
12638 f_strpart(argvars, rettv);
12639#endif
12640}
12641
12642/*
12643 * "strpart()" function
12644 */
12645 static void
12646f_strpart(typval_T *argvars, typval_T *rettv)
12647{
12648 char_u *p;
12649 int n;
12650 int len;
12651 int slen;
12652 int error = FALSE;
12653
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012654 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012655 slen = (int)STRLEN(p);
12656
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012657 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012658 if (error)
12659 len = 0;
12660 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012661 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012662 else
12663 len = slen - n; /* default len: all bytes that are available. */
12664
12665 /*
12666 * Only return the overlap between the specified part and the actual
12667 * string.
12668 */
12669 if (n < 0)
12670 {
12671 len += n;
12672 n = 0;
12673 }
12674 else if (n > slen)
12675 n = slen;
12676 if (len < 0)
12677 len = 0;
12678 else if (n + len > slen)
12679 len = slen - n;
12680
12681 rettv->v_type = VAR_STRING;
12682 rettv->vval.v_string = vim_strnsave(p + n, len);
12683}
12684
12685/*
12686 * "strridx()" function
12687 */
12688 static void
12689f_strridx(typval_T *argvars, typval_T *rettv)
12690{
12691 char_u buf[NUMBUFLEN];
12692 char_u *needle;
12693 char_u *haystack;
12694 char_u *rest;
12695 char_u *lastmatch = NULL;
12696 int haystack_len, end_idx;
12697
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012698 needle = tv_get_string_chk(&argvars[1]);
12699 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012700
12701 rettv->vval.v_number = -1;
12702 if (needle == NULL || haystack == NULL)
12703 return; /* type error; errmsg already given */
12704
12705 haystack_len = (int)STRLEN(haystack);
12706 if (argvars[2].v_type != VAR_UNKNOWN)
12707 {
12708 /* Third argument: upper limit for index */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012709 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012710 if (end_idx < 0)
12711 return; /* can never find a match */
12712 }
12713 else
12714 end_idx = haystack_len;
12715
12716 if (*needle == NUL)
12717 {
12718 /* Empty string matches past the end. */
12719 lastmatch = haystack + end_idx;
12720 }
12721 else
12722 {
12723 for (rest = haystack; *rest != '\0'; ++rest)
12724 {
12725 rest = (char_u *)strstr((char *)rest, (char *)needle);
12726 if (rest == NULL || rest > haystack + end_idx)
12727 break;
12728 lastmatch = rest;
12729 }
12730 }
12731
12732 if (lastmatch == NULL)
12733 rettv->vval.v_number = -1;
12734 else
12735 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
12736}
12737
12738/*
12739 * "strtrans()" function
12740 */
12741 static void
12742f_strtrans(typval_T *argvars, typval_T *rettv)
12743{
12744 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012745 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012746}
12747
12748/*
12749 * "submatch()" function
12750 */
12751 static void
12752f_submatch(typval_T *argvars, typval_T *rettv)
12753{
12754 int error = FALSE;
12755 int no;
12756 int retList = 0;
12757
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012758 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012759 if (error)
12760 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012761 if (no < 0 || no >= NSUBEXP)
12762 {
Bram Moolenaar79518e22017-02-17 16:31:35 +010012763 EMSGN(_("E935: invalid submatch number: %d"), no);
12764 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012765 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012766 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012767 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012768 if (error)
12769 return;
12770
12771 if (retList == 0)
12772 {
12773 rettv->v_type = VAR_STRING;
12774 rettv->vval.v_string = reg_submatch(no);
12775 }
12776 else
12777 {
12778 rettv->v_type = VAR_LIST;
12779 rettv->vval.v_list = reg_submatch_list(no);
12780 }
12781}
12782
12783/*
12784 * "substitute()" function
12785 */
12786 static void
12787f_substitute(typval_T *argvars, typval_T *rettv)
12788{
12789 char_u patbuf[NUMBUFLEN];
12790 char_u subbuf[NUMBUFLEN];
12791 char_u flagsbuf[NUMBUFLEN];
12792
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012793 char_u *str = tv_get_string_chk(&argvars[0]);
12794 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012795 char_u *sub = NULL;
12796 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012797 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012798
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012799 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
12800 expr = &argvars[2];
12801 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012802 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012803
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012804 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012805 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
12806 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012807 rettv->vval.v_string = NULL;
12808 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012809 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012810}
12811
12812/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020012813 * "swapinfo(swap_filename)" function
12814 */
12815 static void
12816f_swapinfo(typval_T *argvars, typval_T *rettv)
12817{
12818 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012819 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +020012820}
12821
12822/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020012823 * "swapname(expr)" function
12824 */
12825 static void
12826f_swapname(typval_T *argvars, typval_T *rettv)
12827{
12828 buf_T *buf;
12829
12830 rettv->v_type = VAR_STRING;
12831 buf = get_buf_tv(&argvars[0], FALSE);
12832 if (buf == NULL || buf->b_ml.ml_mfp == NULL
12833 || buf->b_ml.ml_mfp->mf_fname == NULL)
12834 rettv->vval.v_string = NULL;
12835 else
12836 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
12837}
12838
12839/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012840 * "synID(lnum, col, trans)" function
12841 */
12842 static void
12843f_synID(typval_T *argvars UNUSED, typval_T *rettv)
12844{
12845 int id = 0;
12846#ifdef FEAT_SYN_HL
12847 linenr_T lnum;
12848 colnr_T col;
12849 int trans;
12850 int transerr = FALSE;
12851
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012852 lnum = tv_get_lnum(argvars); /* -1 on type error */
12853 col = (linenr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
12854 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012855
12856 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12857 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
12858 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
12859#endif
12860
12861 rettv->vval.v_number = id;
12862}
12863
12864/*
12865 * "synIDattr(id, what [, mode])" function
12866 */
12867 static void
12868f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
12869{
12870 char_u *p = NULL;
12871#ifdef FEAT_SYN_HL
12872 int id;
12873 char_u *what;
12874 char_u *mode;
12875 char_u modebuf[NUMBUFLEN];
12876 int modec;
12877
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012878 id = (int)tv_get_number(&argvars[0]);
12879 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012880 if (argvars[2].v_type != VAR_UNKNOWN)
12881 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012882 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012883 modec = TOLOWER_ASC(mode[0]);
12884 if (modec != 't' && modec != 'c' && modec != 'g')
12885 modec = 0; /* replace invalid with current */
12886 }
12887 else
12888 {
12889#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
12890 if (USE_24BIT)
12891 modec = 'g';
12892 else
12893#endif
12894 if (t_colors > 1)
12895 modec = 'c';
12896 else
12897 modec = 't';
12898 }
12899
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012900 switch (TOLOWER_ASC(what[0]))
12901 {
12902 case 'b':
12903 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
12904 p = highlight_color(id, what, modec);
12905 else /* bold */
12906 p = highlight_has_attr(id, HL_BOLD, modec);
12907 break;
12908
12909 case 'f': /* fg[#] or font */
12910 p = highlight_color(id, what, modec);
12911 break;
12912
12913 case 'i':
12914 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
12915 p = highlight_has_attr(id, HL_INVERSE, modec);
12916 else /* italic */
12917 p = highlight_has_attr(id, HL_ITALIC, modec);
12918 break;
12919
12920 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020012921 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012922 break;
12923
12924 case 'r': /* reverse */
12925 p = highlight_has_attr(id, HL_INVERSE, modec);
12926 break;
12927
12928 case 's':
12929 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
12930 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020012931 /* strikeout */
12932 else if (TOLOWER_ASC(what[1]) == 't' &&
12933 TOLOWER_ASC(what[2]) == 'r')
12934 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012935 else /* standout */
12936 p = highlight_has_attr(id, HL_STANDOUT, modec);
12937 break;
12938
12939 case 'u':
12940 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
12941 /* underline */
12942 p = highlight_has_attr(id, HL_UNDERLINE, modec);
12943 else
12944 /* undercurl */
12945 p = highlight_has_attr(id, HL_UNDERCURL, modec);
12946 break;
12947 }
12948
12949 if (p != NULL)
12950 p = vim_strsave(p);
12951#endif
12952 rettv->v_type = VAR_STRING;
12953 rettv->vval.v_string = p;
12954}
12955
12956/*
12957 * "synIDtrans(id)" function
12958 */
12959 static void
12960f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
12961{
12962 int id;
12963
12964#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012965 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012966
12967 if (id > 0)
12968 id = syn_get_final_id(id);
12969 else
12970#endif
12971 id = 0;
12972
12973 rettv->vval.v_number = id;
12974}
12975
12976/*
12977 * "synconcealed(lnum, col)" function
12978 */
12979 static void
12980f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
12981{
12982#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12983 linenr_T lnum;
12984 colnr_T col;
12985 int syntax_flags = 0;
12986 int cchar;
12987 int matchid = 0;
12988 char_u str[NUMBUFLEN];
12989#endif
12990
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012991 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012992
12993#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012994 lnum = tv_get_lnum(argvars); /* -1 on type error */
12995 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012996
12997 vim_memset(str, NUL, sizeof(str));
12998
12999 if (rettv_list_alloc(rettv) != FAIL)
13000 {
13001 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13002 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13003 && curwin->w_p_cole > 0)
13004 {
13005 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
13006 syntax_flags = get_syntax_info(&matchid);
13007
13008 /* get the conceal character */
13009 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
13010 {
13011 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020013012 if (cchar == NUL && curwin->w_p_cole == 1)
13013 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013014 if (cchar != NUL)
13015 {
13016# ifdef FEAT_MBYTE
13017 if (has_mbyte)
13018 (*mb_char2bytes)(cchar, str);
13019 else
13020# endif
13021 str[0] = cchar;
13022 }
13023 }
13024 }
13025
13026 list_append_number(rettv->vval.v_list,
13027 (syntax_flags & HL_CONCEAL) != 0);
13028 /* -1 to auto-determine strlen */
13029 list_append_string(rettv->vval.v_list, str, -1);
13030 list_append_number(rettv->vval.v_list, matchid);
13031 }
13032#endif
13033}
13034
13035/*
13036 * "synstack(lnum, col)" function
13037 */
13038 static void
13039f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
13040{
13041#ifdef FEAT_SYN_HL
13042 linenr_T lnum;
13043 colnr_T col;
13044 int i;
13045 int id;
13046#endif
13047
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013048 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013049
13050#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013051 lnum = tv_get_lnum(argvars); /* -1 on type error */
13052 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013053
13054 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13055 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13056 && rettv_list_alloc(rettv) != FAIL)
13057 {
13058 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
13059 for (i = 0; ; ++i)
13060 {
13061 id = syn_get_stack_item(i);
13062 if (id < 0)
13063 break;
13064 if (list_append_number(rettv->vval.v_list, id) == FAIL)
13065 break;
13066 }
13067 }
13068#endif
13069}
13070
13071 static void
13072get_cmd_output_as_rettv(
13073 typval_T *argvars,
13074 typval_T *rettv,
13075 int retlist)
13076{
13077 char_u *res = NULL;
13078 char_u *p;
13079 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013080 int err = FALSE;
13081 FILE *fd;
13082 list_T *list = NULL;
13083 int flags = SHELL_SILENT;
13084
13085 rettv->v_type = VAR_STRING;
13086 rettv->vval.v_string = NULL;
13087 if (check_restricted() || check_secure())
13088 goto errret;
13089
13090 if (argvars[1].v_type != VAR_UNKNOWN)
13091 {
13092 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010013093 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013094 * command.
13095 */
13096 if ((infile = vim_tempname('i', TRUE)) == NULL)
13097 {
13098 EMSG(_(e_notmp));
13099 goto errret;
13100 }
13101
13102 fd = mch_fopen((char *)infile, WRITEBIN);
13103 if (fd == NULL)
13104 {
13105 EMSG2(_(e_notopen), infile);
13106 goto errret;
13107 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010013108 if (argvars[1].v_type == VAR_NUMBER)
13109 {
13110 linenr_T lnum;
13111 buf_T *buf;
13112
13113 buf = buflist_findnr(argvars[1].vval.v_number);
13114 if (buf == NULL)
13115 {
13116 EMSGN(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010013117 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010013118 goto errret;
13119 }
13120
13121 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
13122 {
13123 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
13124 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
13125 {
13126 err = TRUE;
13127 break;
13128 }
13129 if (putc(NL, fd) == EOF)
13130 {
13131 err = TRUE;
13132 break;
13133 }
13134 }
13135 }
13136 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013137 {
13138 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
13139 err = TRUE;
13140 }
13141 else
13142 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010013143 size_t len;
13144 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013145
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013146 p = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013147 if (p == NULL)
13148 {
13149 fclose(fd);
13150 goto errret; /* type error; errmsg already given */
13151 }
13152 len = STRLEN(p);
13153 if (len > 0 && fwrite(p, len, 1, fd) != 1)
13154 err = TRUE;
13155 }
13156 if (fclose(fd) != 0)
13157 err = TRUE;
13158 if (err)
13159 {
13160 EMSG(_("E677: Error writing temp file"));
13161 goto errret;
13162 }
13163 }
13164
13165 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
13166 * echoes typeahead, that messes up the display. */
13167 if (!msg_silent)
13168 flags += SHELL_COOKED;
13169
13170 if (retlist)
13171 {
13172 int len;
13173 listitem_T *li;
13174 char_u *s = NULL;
13175 char_u *start;
13176 char_u *end;
13177 int i;
13178
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013179 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013180 if (res == NULL)
13181 goto errret;
13182
13183 list = list_alloc();
13184 if (list == NULL)
13185 goto errret;
13186
13187 for (i = 0; i < len; ++i)
13188 {
13189 start = res + i;
13190 while (i < len && res[i] != NL)
13191 ++i;
13192 end = res + i;
13193
13194 s = alloc((unsigned)(end - start + 1));
13195 if (s == NULL)
13196 goto errret;
13197
13198 for (p = s; start < end; ++p, ++start)
13199 *p = *start == NUL ? NL : *start;
13200 *p = NUL;
13201
13202 li = listitem_alloc();
13203 if (li == NULL)
13204 {
13205 vim_free(s);
13206 goto errret;
13207 }
13208 li->li_tv.v_type = VAR_STRING;
13209 li->li_tv.v_lock = 0;
13210 li->li_tv.vval.v_string = s;
13211 list_append(list, li);
13212 }
13213
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013214 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013215 list = NULL;
13216 }
13217 else
13218 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013219 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013220#ifdef USE_CR
13221 /* translate <CR> into <NL> */
13222 if (res != NULL)
13223 {
13224 char_u *s;
13225
13226 for (s = res; *s; ++s)
13227 {
13228 if (*s == CAR)
13229 *s = NL;
13230 }
13231 }
13232#else
13233# ifdef USE_CRNL
13234 /* translate <CR><NL> into <NL> */
13235 if (res != NULL)
13236 {
13237 char_u *s, *d;
13238
13239 d = res;
13240 for (s = res; *s; ++s)
13241 {
13242 if (s[0] == CAR && s[1] == NL)
13243 ++s;
13244 *d++ = *s;
13245 }
13246 *d = NUL;
13247 }
13248# endif
13249#endif
13250 rettv->vval.v_string = res;
13251 res = NULL;
13252 }
13253
13254errret:
13255 if (infile != NULL)
13256 {
13257 mch_remove(infile);
13258 vim_free(infile);
13259 }
13260 if (res != NULL)
13261 vim_free(res);
13262 if (list != NULL)
13263 list_free(list);
13264}
13265
13266/*
13267 * "system()" function
13268 */
13269 static void
13270f_system(typval_T *argvars, typval_T *rettv)
13271{
13272 get_cmd_output_as_rettv(argvars, rettv, FALSE);
13273}
13274
13275/*
13276 * "systemlist()" function
13277 */
13278 static void
13279f_systemlist(typval_T *argvars, typval_T *rettv)
13280{
13281 get_cmd_output_as_rettv(argvars, rettv, TRUE);
13282}
13283
13284/*
13285 * "tabpagebuflist()" function
13286 */
13287 static void
13288f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13289{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013290 tabpage_T *tp;
13291 win_T *wp = NULL;
13292
13293 if (argvars[0].v_type == VAR_UNKNOWN)
13294 wp = firstwin;
13295 else
13296 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013297 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013298 if (tp != NULL)
13299 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
13300 }
13301 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
13302 {
13303 for (; wp != NULL; wp = wp->w_next)
13304 if (list_append_number(rettv->vval.v_list,
13305 wp->w_buffer->b_fnum) == FAIL)
13306 break;
13307 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013308}
13309
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013310/*
13311 * "tabpagenr()" function
13312 */
13313 static void
13314f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
13315{
13316 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013317 char_u *arg;
13318
13319 if (argvars[0].v_type != VAR_UNKNOWN)
13320 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013321 arg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013322 nr = 0;
13323 if (arg != NULL)
13324 {
13325 if (STRCMP(arg, "$") == 0)
13326 nr = tabpage_index(NULL) - 1;
13327 else
13328 EMSG2(_(e_invexpr2), arg);
13329 }
13330 }
13331 else
13332 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013333 rettv->vval.v_number = nr;
13334}
13335
13336
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013337/*
13338 * Common code for tabpagewinnr() and winnr().
13339 */
13340 static int
13341get_winnr(tabpage_T *tp, typval_T *argvar)
13342{
13343 win_T *twin;
13344 int nr = 1;
13345 win_T *wp;
13346 char_u *arg;
13347
13348 twin = (tp == curtab) ? curwin : tp->tp_curwin;
13349 if (argvar->v_type != VAR_UNKNOWN)
13350 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013351 arg = tv_get_string_chk(argvar);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013352 if (arg == NULL)
13353 nr = 0; /* type error; errmsg already given */
13354 else if (STRCMP(arg, "$") == 0)
13355 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
13356 else if (STRCMP(arg, "#") == 0)
13357 {
13358 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
13359 if (twin == NULL)
13360 nr = 0;
13361 }
13362 else
13363 {
13364 EMSG2(_(e_invexpr2), arg);
13365 nr = 0;
13366 }
13367 }
13368
13369 if (nr > 0)
13370 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
13371 wp != twin; wp = wp->w_next)
13372 {
13373 if (wp == NULL)
13374 {
13375 /* didn't find it in this tabpage */
13376 nr = 0;
13377 break;
13378 }
13379 ++nr;
13380 }
13381 return nr;
13382}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013383
13384/*
13385 * "tabpagewinnr()" function
13386 */
13387 static void
13388f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
13389{
13390 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013391 tabpage_T *tp;
13392
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013393 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013394 if (tp == NULL)
13395 nr = 0;
13396 else
13397 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013398 rettv->vval.v_number = nr;
13399}
13400
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013401/*
13402 * "tagfiles()" function
13403 */
13404 static void
13405f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
13406{
13407 char_u *fname;
13408 tagname_T tn;
13409 int first;
13410
13411 if (rettv_list_alloc(rettv) == FAIL)
13412 return;
13413 fname = alloc(MAXPATHL);
13414 if (fname == NULL)
13415 return;
13416
13417 for (first = TRUE; ; first = FALSE)
13418 if (get_tagfname(&tn, first, fname) == FAIL
13419 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
13420 break;
13421 tagname_free(&tn);
13422 vim_free(fname);
13423}
13424
13425/*
13426 * "taglist()" function
13427 */
13428 static void
13429f_taglist(typval_T *argvars, typval_T *rettv)
13430{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013431 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013432 char_u *tag_pattern;
13433
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013434 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013435
13436 rettv->vval.v_number = FALSE;
13437 if (*tag_pattern == NUL)
13438 return;
13439
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013440 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013441 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013442 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013443 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013444}
13445
13446/*
13447 * "tempname()" function
13448 */
13449 static void
13450f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
13451{
13452 static int x = 'A';
13453
13454 rettv->v_type = VAR_STRING;
13455 rettv->vval.v_string = vim_tempname(x, FALSE);
13456
13457 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
13458 * names. Skip 'I' and 'O', they are used for shell redirection. */
13459 do
13460 {
13461 if (x == 'Z')
13462 x = '0';
13463 else if (x == '9')
13464 x = 'A';
13465 else
13466 {
13467#ifdef EBCDIC
13468 if (x == 'I')
13469 x = 'J';
13470 else if (x == 'R')
13471 x = 'S';
13472 else
13473#endif
13474 ++x;
13475 }
13476 } while (x == 'I' || x == 'O');
13477}
13478
13479#ifdef FEAT_FLOAT
13480/*
13481 * "tan()" function
13482 */
13483 static void
13484f_tan(typval_T *argvars, typval_T *rettv)
13485{
13486 float_T f = 0.0;
13487
13488 rettv->v_type = VAR_FLOAT;
13489 if (get_float_arg(argvars, &f) == OK)
13490 rettv->vval.v_float = tan(f);
13491 else
13492 rettv->vval.v_float = 0.0;
13493}
13494
13495/*
13496 * "tanh()" function
13497 */
13498 static void
13499f_tanh(typval_T *argvars, typval_T *rettv)
13500{
13501 float_T f = 0.0;
13502
13503 rettv->v_type = VAR_FLOAT;
13504 if (get_float_arg(argvars, &f) == OK)
13505 rettv->vval.v_float = tanh(f);
13506 else
13507 rettv->vval.v_float = 0.0;
13508}
13509#endif
13510
13511/*
13512 * "test_alloc_fail(id, countdown, repeat)" function
13513 */
13514 static void
13515f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
13516{
13517 if (argvars[0].v_type != VAR_NUMBER
13518 || argvars[0].vval.v_number <= 0
13519 || argvars[1].v_type != VAR_NUMBER
13520 || argvars[1].vval.v_number < 0
13521 || argvars[2].v_type != VAR_NUMBER)
13522 EMSG(_(e_invarg));
13523 else
13524 {
13525 alloc_fail_id = argvars[0].vval.v_number;
13526 if (alloc_fail_id >= aid_last)
13527 EMSG(_(e_invarg));
13528 alloc_fail_countdown = argvars[1].vval.v_number;
13529 alloc_fail_repeat = argvars[2].vval.v_number;
13530 did_outofmem_msg = FALSE;
13531 }
13532}
13533
13534/*
13535 * "test_autochdir()"
13536 */
13537 static void
13538f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13539{
13540#if defined(FEAT_AUTOCHDIR)
13541 test_autochdir = TRUE;
13542#endif
13543}
13544
13545/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020013546 * "test_feedinput()"
13547 */
13548 static void
13549f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
13550{
13551#ifdef USE_INPUT_BUF
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013552 char_u *val = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5e80de32017-09-03 15:48:12 +020013553
13554 if (val != NULL)
13555 {
13556 trash_input_buf();
13557 add_to_input_buf_csi(val, (int)STRLEN(val));
13558 }
13559#endif
13560}
13561
13562/*
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013563 * "test_option_not_set({name})" function
13564 */
13565 static void
13566f_test_option_not_set(typval_T *argvars, typval_T *rettv UNUSED)
13567{
13568 char_u *name = (char_u *)"";
13569
13570 if (argvars[0].v_type != VAR_STRING)
13571 EMSG(_(e_invarg));
13572 else
13573 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013574 name = tv_get_string(&argvars[0]);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013575 if (reset_option_was_set(name) == FAIL)
13576 EMSG2(_(e_invarg2), name);
13577 }
13578}
13579
13580/*
13581 * "test_override({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013582 */
13583 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013584f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013585{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013586 char_u *name = (char_u *)"";
13587 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013588 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013589
13590 if (argvars[0].v_type != VAR_STRING
13591 || (argvars[1].v_type) != VAR_NUMBER)
13592 EMSG(_(e_invarg));
13593 else
13594 {
Bram Moolenaare38197d2018-12-24 23:35:13 +010013595 name = tv_get_string(&argvars[0]);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013596 val = (int)tv_get_number(&argvars[1]);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013597
13598 if (STRCMP(name, (char_u *)"redraw") == 0)
13599 disable_redraw_for_testing = val;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020013600 else if (STRCMP(name, (char_u *)"redraw_flag") == 0)
13601 ignore_redraw_flag_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013602 else if (STRCMP(name, (char_u *)"char_avail") == 0)
13603 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013604 else if (STRCMP(name, (char_u *)"starting") == 0)
13605 {
13606 if (val)
13607 {
13608 if (save_starting < 0)
13609 save_starting = starting;
13610 starting = 0;
13611 }
13612 else
13613 {
13614 starting = save_starting;
13615 save_starting = -1;
13616 }
13617 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013618 else if (STRCMP(name, (char_u *)"nfa_fail") == 0)
13619 nfa_fail_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013620 else if (STRCMP(name, (char_u *)"ALL") == 0)
13621 {
13622 disable_char_avail_for_testing = FALSE;
13623 disable_redraw_for_testing = FALSE;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020013624 ignore_redraw_flag_for_testing = FALSE;
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013625 nfa_fail_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013626 if (save_starting >= 0)
13627 {
13628 starting = save_starting;
13629 save_starting = -1;
13630 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013631 }
13632 else
13633 EMSG2(_(e_invarg2), name);
13634 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013635}
13636
13637/*
13638 * "test_garbagecollect_now()" function
13639 */
13640 static void
13641f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13642{
13643 /* This is dangerous, any Lists and Dicts used internally may be freed
13644 * while still in use. */
13645 garbage_collect(TRUE);
13646}
13647
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013648/*
13649 * "test_ignore_error()" function
13650 */
13651 static void
13652f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
13653{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013654 ignore_error_for_testing(tv_get_string(&argvars[0]));
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013655}
13656
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013657#ifdef FEAT_JOB_CHANNEL
13658 static void
13659f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
13660{
13661 rettv->v_type = VAR_CHANNEL;
13662 rettv->vval.v_channel = NULL;
13663}
13664#endif
13665
13666 static void
13667f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
13668{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013669 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013670}
13671
13672#ifdef FEAT_JOB_CHANNEL
13673 static void
13674f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
13675{
13676 rettv->v_type = VAR_JOB;
13677 rettv->vval.v_job = NULL;
13678}
13679#endif
13680
13681 static void
13682f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
13683{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013684 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013685}
13686
13687 static void
13688f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
13689{
13690 rettv->v_type = VAR_PARTIAL;
13691 rettv->vval.v_partial = NULL;
13692}
13693
13694 static void
13695f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
13696{
13697 rettv->v_type = VAR_STRING;
13698 rettv->vval.v_string = NULL;
13699}
13700
Bram Moolenaarab186732018-09-14 21:27:06 +020013701#ifdef FEAT_GUI
13702 static void
13703f_test_scrollbar(typval_T *argvars, typval_T *rettv UNUSED)
13704{
13705 char_u *which;
13706 long value;
13707 int dragging;
13708 scrollbar_T *sb = NULL;
13709
13710 if (argvars[0].v_type != VAR_STRING
13711 || (argvars[1].v_type) != VAR_NUMBER
13712 || (argvars[2].v_type) != VAR_NUMBER)
13713 {
13714 EMSG(_(e_invarg));
13715 return;
13716 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013717 which = tv_get_string(&argvars[0]);
13718 value = tv_get_number(&argvars[1]);
13719 dragging = tv_get_number(&argvars[2]);
Bram Moolenaarab186732018-09-14 21:27:06 +020013720
13721 if (STRCMP(which, "left") == 0)
13722 sb = &curwin->w_scrollbars[SBAR_LEFT];
13723 else if (STRCMP(which, "right") == 0)
13724 sb = &curwin->w_scrollbars[SBAR_RIGHT];
13725 else if (STRCMP(which, "hor") == 0)
13726 sb = &gui.bottom_sbar;
13727 if (sb == NULL)
13728 {
13729 EMSG2(_(e_invarg2), which);
13730 return;
13731 }
13732 gui_drag_scrollbar(sb, value, dragging);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020013733# ifndef USE_ON_FLY_SCROLL
13734 // need to loop through normal_cmd() to handle the scroll events
13735 exec_normal(FALSE, TRUE, FALSE);
13736# endif
Bram Moolenaarab186732018-09-14 21:27:06 +020013737}
13738#endif
13739
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013740 static void
13741f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
13742{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013743 time_for_testing = (time_t)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013744}
13745
13746#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
13747/*
13748 * Get a callback from "arg". It can be a Funcref or a function name.
13749 * When "arg" is zero return an empty string.
13750 * Return NULL for an invalid argument.
13751 */
13752 char_u *
13753get_callback(typval_T *arg, partial_T **pp)
13754{
13755 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
13756 {
13757 *pp = arg->vval.v_partial;
13758 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020013759 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013760 }
13761 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020013762 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013763 {
13764 func_ref(arg->vval.v_string);
13765 return arg->vval.v_string;
13766 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013767 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
13768 return (char_u *)"";
13769 EMSG(_("E921: Invalid callback argument"));
13770 return NULL;
13771}
13772
13773/*
Bram Moolenaard5359b22018-04-05 22:44:39 +020013774 * Unref/free "callback" and "partial" returned by get_callback().
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013775 */
13776 void
13777free_callback(char_u *callback, partial_T *partial)
13778{
13779 if (partial != NULL)
13780 partial_unref(partial);
13781 else if (callback != NULL)
13782 {
13783 func_unref(callback);
13784 vim_free(callback);
13785 }
13786}
13787#endif
13788
13789#ifdef FEAT_TIMERS
13790/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020013791 * "timer_info([timer])" function
13792 */
13793 static void
13794f_timer_info(typval_T *argvars, typval_T *rettv)
13795{
13796 timer_T *timer = NULL;
13797
13798 if (rettv_list_alloc(rettv) != OK)
13799 return;
13800 if (argvars[0].v_type != VAR_UNKNOWN)
13801 {
13802 if (argvars[0].v_type != VAR_NUMBER)
13803 EMSG(_(e_number_exp));
13804 else
13805 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013806 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020013807 if (timer != NULL)
13808 add_timer_info(rettv, timer);
13809 }
13810 }
13811 else
13812 add_timer_info_all(rettv);
13813}
13814
13815/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013816 * "timer_pause(timer, paused)" function
13817 */
13818 static void
13819f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
13820{
13821 timer_T *timer = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013822 int paused = (int)tv_get_number(&argvars[1]);
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013823
13824 if (argvars[0].v_type != VAR_NUMBER)
13825 EMSG(_(e_number_exp));
13826 else
13827 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013828 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013829 if (timer != NULL)
13830 timer->tr_paused = paused;
13831 }
13832}
13833
13834/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013835 * "timer_start(time, callback [, options])" function
13836 */
13837 static void
13838f_timer_start(typval_T *argvars, typval_T *rettv)
13839{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013840 long msec = (long)tv_get_number(&argvars[0]);
Bram Moolenaar75537a92016-09-05 22:45:28 +020013841 timer_T *timer;
13842 int repeat = 0;
13843 char_u *callback;
13844 dict_T *dict;
13845 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013846
Bram Moolenaar75537a92016-09-05 22:45:28 +020013847 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013848 if (check_secure())
13849 return;
13850 if (argvars[2].v_type != VAR_UNKNOWN)
13851 {
13852 if (argvars[2].v_type != VAR_DICT
13853 || (dict = argvars[2].vval.v_dict) == NULL)
13854 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013855 EMSG2(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013856 return;
13857 }
13858 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010013859 repeat = dict_get_number(dict, (char_u *)"repeat");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013860 }
13861
Bram Moolenaar75537a92016-09-05 22:45:28 +020013862 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013863 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020013864 return;
13865
13866 timer = create_timer(msec, repeat);
13867 if (timer == NULL)
13868 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013869 else
13870 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020013871 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020013872 timer->tr_callback = vim_strsave(callback);
13873 else
13874 /* pointer into the partial */
13875 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020013876 timer->tr_partial = partial;
13877 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013878 }
13879}
13880
13881/*
13882 * "timer_stop(timer)" function
13883 */
13884 static void
13885f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
13886{
13887 timer_T *timer;
13888
13889 if (argvars[0].v_type != VAR_NUMBER)
13890 {
13891 EMSG(_(e_number_exp));
13892 return;
13893 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013894 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013895 if (timer != NULL)
13896 stop_timer(timer);
13897}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013898
13899/*
13900 * "timer_stopall()" function
13901 */
13902 static void
13903f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13904{
13905 stop_all_timers();
13906}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013907#endif
13908
13909/*
13910 * "tolower(string)" function
13911 */
13912 static void
13913f_tolower(typval_T *argvars, typval_T *rettv)
13914{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013915 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013916 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013917}
13918
13919/*
13920 * "toupper(string)" function
13921 */
13922 static void
13923f_toupper(typval_T *argvars, typval_T *rettv)
13924{
13925 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013926 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013927}
13928
13929/*
13930 * "tr(string, fromstr, tostr)" function
13931 */
13932 static void
13933f_tr(typval_T *argvars, typval_T *rettv)
13934{
13935 char_u *in_str;
13936 char_u *fromstr;
13937 char_u *tostr;
13938 char_u *p;
13939#ifdef FEAT_MBYTE
13940 int inlen;
13941 int fromlen;
13942 int tolen;
13943 int idx;
13944 char_u *cpstr;
13945 int cplen;
13946 int first = TRUE;
13947#endif
13948 char_u buf[NUMBUFLEN];
13949 char_u buf2[NUMBUFLEN];
13950 garray_T ga;
13951
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013952 in_str = tv_get_string(&argvars[0]);
13953 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
13954 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013955
13956 /* Default return value: empty string. */
13957 rettv->v_type = VAR_STRING;
13958 rettv->vval.v_string = NULL;
13959 if (fromstr == NULL || tostr == NULL)
13960 return; /* type error; errmsg already given */
13961 ga_init2(&ga, (int)sizeof(char), 80);
13962
13963#ifdef FEAT_MBYTE
13964 if (!has_mbyte)
13965#endif
13966 /* not multi-byte: fromstr and tostr must be the same length */
13967 if (STRLEN(fromstr) != STRLEN(tostr))
13968 {
13969#ifdef FEAT_MBYTE
13970error:
13971#endif
13972 EMSG2(_(e_invarg2), fromstr);
13973 ga_clear(&ga);
13974 return;
13975 }
13976
13977 /* fromstr and tostr have to contain the same number of chars */
13978 while (*in_str != NUL)
13979 {
13980#ifdef FEAT_MBYTE
13981 if (has_mbyte)
13982 {
13983 inlen = (*mb_ptr2len)(in_str);
13984 cpstr = in_str;
13985 cplen = inlen;
13986 idx = 0;
13987 for (p = fromstr; *p != NUL; p += fromlen)
13988 {
13989 fromlen = (*mb_ptr2len)(p);
13990 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
13991 {
13992 for (p = tostr; *p != NUL; p += tolen)
13993 {
13994 tolen = (*mb_ptr2len)(p);
13995 if (idx-- == 0)
13996 {
13997 cplen = tolen;
13998 cpstr = p;
13999 break;
14000 }
14001 }
14002 if (*p == NUL) /* tostr is shorter than fromstr */
14003 goto error;
14004 break;
14005 }
14006 ++idx;
14007 }
14008
14009 if (first && cpstr == in_str)
14010 {
14011 /* Check that fromstr and tostr have the same number of
14012 * (multi-byte) characters. Done only once when a character
14013 * of in_str doesn't appear in fromstr. */
14014 first = FALSE;
14015 for (p = tostr; *p != NUL; p += tolen)
14016 {
14017 tolen = (*mb_ptr2len)(p);
14018 --idx;
14019 }
14020 if (idx != 0)
14021 goto error;
14022 }
14023
14024 (void)ga_grow(&ga, cplen);
14025 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
14026 ga.ga_len += cplen;
14027
14028 in_str += inlen;
14029 }
14030 else
14031#endif
14032 {
14033 /* When not using multi-byte chars we can do it faster. */
14034 p = vim_strchr(fromstr, *in_str);
14035 if (p != NULL)
14036 ga_append(&ga, tostr[p - fromstr]);
14037 else
14038 ga_append(&ga, *in_str);
14039 ++in_str;
14040 }
14041 }
14042
14043 /* add a terminating NUL */
14044 (void)ga_grow(&ga, 1);
14045 ga_append(&ga, NUL);
14046
14047 rettv->vval.v_string = ga.ga_data;
14048}
14049
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014050/*
14051 * "trim({expr})" function
14052 */
14053 static void
14054f_trim(typval_T *argvars, typval_T *rettv)
14055{
14056 char_u buf1[NUMBUFLEN];
14057 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014058 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014059 char_u *mask = NULL;
14060 char_u *tail;
14061 char_u *prev;
14062 char_u *p;
14063 int c1;
14064
14065 rettv->v_type = VAR_STRING;
14066 if (head == NULL)
14067 {
14068 rettv->vval.v_string = NULL;
14069 return;
14070 }
14071
14072 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014073 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014074
14075 while (*head != NUL)
14076 {
14077 c1 = PTR2CHAR(head);
14078 if (mask == NULL)
14079 {
14080 if (c1 > ' ' && c1 != 0xa0)
14081 break;
14082 }
14083 else
14084 {
14085 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14086 if (c1 == PTR2CHAR(p))
14087 break;
14088 if (*p == NUL)
14089 break;
14090 }
14091 MB_PTR_ADV(head);
14092 }
14093
14094 for (tail = head + STRLEN(head); tail > head; tail = prev)
14095 {
14096 prev = tail;
14097 MB_PTR_BACK(head, prev);
14098 c1 = PTR2CHAR(prev);
14099 if (mask == NULL)
14100 {
14101 if (c1 > ' ' && c1 != 0xa0)
14102 break;
14103 }
14104 else
14105 {
14106 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14107 if (c1 == PTR2CHAR(p))
14108 break;
14109 if (*p == NUL)
14110 break;
14111 }
14112 }
14113 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
14114}
14115
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014116#ifdef FEAT_FLOAT
14117/*
14118 * "trunc({float})" function
14119 */
14120 static void
14121f_trunc(typval_T *argvars, typval_T *rettv)
14122{
14123 float_T f = 0.0;
14124
14125 rettv->v_type = VAR_FLOAT;
14126 if (get_float_arg(argvars, &f) == OK)
14127 /* trunc() is not in C90, use floor() or ceil() instead. */
14128 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
14129 else
14130 rettv->vval.v_float = 0.0;
14131}
14132#endif
14133
14134/*
14135 * "type(expr)" function
14136 */
14137 static void
14138f_type(typval_T *argvars, typval_T *rettv)
14139{
14140 int n = -1;
14141
14142 switch (argvars[0].v_type)
14143 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020014144 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
14145 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014146 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020014147 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
14148 case VAR_LIST: n = VAR_TYPE_LIST; break;
14149 case VAR_DICT: n = VAR_TYPE_DICT; break;
14150 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014151 case VAR_SPECIAL:
14152 if (argvars[0].vval.v_number == VVAL_FALSE
14153 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020014154 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014155 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020014156 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014157 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020014158 case VAR_JOB: n = VAR_TYPE_JOB; break;
14159 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014160 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010014161 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014162 n = -1;
14163 break;
14164 }
14165 rettv->vval.v_number = n;
14166}
14167
14168/*
14169 * "undofile(name)" function
14170 */
14171 static void
14172f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
14173{
14174 rettv->v_type = VAR_STRING;
14175#ifdef FEAT_PERSISTENT_UNDO
14176 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014177 char_u *fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014178
14179 if (*fname == NUL)
14180 {
14181 /* If there is no file name there will be no undo file. */
14182 rettv->vval.v_string = NULL;
14183 }
14184 else
14185 {
14186 char_u *ffname = FullName_save(fname, FALSE);
14187
14188 if (ffname != NULL)
14189 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
14190 vim_free(ffname);
14191 }
14192 }
14193#else
14194 rettv->vval.v_string = NULL;
14195#endif
14196}
14197
14198/*
14199 * "undotree()" function
14200 */
14201 static void
14202f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
14203{
14204 if (rettv_dict_alloc(rettv) == OK)
14205 {
14206 dict_T *dict = rettv->vval.v_dict;
14207 list_T *list;
14208
Bram Moolenaare0be1672018-07-08 16:50:37 +020014209 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
14210 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
14211 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
14212 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
14213 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
14214 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014215
14216 list = list_alloc();
14217 if (list != NULL)
14218 {
14219 u_eval_tree(curbuf->b_u_oldhead, list);
14220 dict_add_list(dict, "entries", list);
14221 }
14222 }
14223}
14224
14225/*
14226 * "values(dict)" function
14227 */
14228 static void
14229f_values(typval_T *argvars, typval_T *rettv)
14230{
14231 dict_list(argvars, rettv, 1);
14232}
14233
14234/*
14235 * "virtcol(string)" function
14236 */
14237 static void
14238f_virtcol(typval_T *argvars, typval_T *rettv)
14239{
14240 colnr_T vcol = 0;
14241 pos_T *fp;
14242 int fnum = curbuf->b_fnum;
14243
14244 fp = var2fpos(&argvars[0], FALSE, &fnum);
14245 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
14246 && fnum == curbuf->b_fnum)
14247 {
14248 getvvcol(curwin, fp, NULL, NULL, &vcol);
14249 ++vcol;
14250 }
14251
14252 rettv->vval.v_number = vcol;
14253}
14254
14255/*
14256 * "visualmode()" function
14257 */
14258 static void
14259f_visualmode(typval_T *argvars, typval_T *rettv)
14260{
14261 char_u str[2];
14262
14263 rettv->v_type = VAR_STRING;
14264 str[0] = curbuf->b_visual_mode_eval;
14265 str[1] = NUL;
14266 rettv->vval.v_string = vim_strsave(str);
14267
14268 /* A non-zero number or non-empty string argument: reset mode. */
14269 if (non_zero_arg(&argvars[0]))
14270 curbuf->b_visual_mode_eval = NUL;
14271}
14272
14273/*
14274 * "wildmenumode()" function
14275 */
14276 static void
14277f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14278{
14279#ifdef FEAT_WILDMENU
14280 if (wild_menu_showing)
14281 rettv->vval.v_number = 1;
14282#endif
14283}
14284
14285/*
14286 * "winbufnr(nr)" function
14287 */
14288 static void
14289f_winbufnr(typval_T *argvars, typval_T *rettv)
14290{
14291 win_T *wp;
14292
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014293 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014294 if (wp == NULL)
14295 rettv->vval.v_number = -1;
14296 else
14297 rettv->vval.v_number = wp->w_buffer->b_fnum;
14298}
14299
14300/*
14301 * "wincol()" function
14302 */
14303 static void
14304f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
14305{
14306 validate_cursor();
14307 rettv->vval.v_number = curwin->w_wcol + 1;
14308}
14309
14310/*
14311 * "winheight(nr)" function
14312 */
14313 static void
14314f_winheight(typval_T *argvars, typval_T *rettv)
14315{
14316 win_T *wp;
14317
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014318 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014319 if (wp == NULL)
14320 rettv->vval.v_number = -1;
14321 else
14322 rettv->vval.v_number = wp->w_height;
14323}
14324
14325/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020014326 * "winlayout()" function
14327 */
14328 static void
14329f_winlayout(typval_T *argvars, typval_T *rettv)
14330{
14331 tabpage_T *tp;
14332
14333 if (rettv_list_alloc(rettv) != OK)
14334 return;
14335
14336 if (argvars[0].v_type == VAR_UNKNOWN)
14337 tp = curtab;
14338 else
14339 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014340 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020014341 if (tp == NULL)
14342 return;
14343 }
14344
14345 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
14346}
14347
14348/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014349 * "winline()" function
14350 */
14351 static void
14352f_winline(typval_T *argvars UNUSED, typval_T *rettv)
14353{
14354 validate_cursor();
14355 rettv->vval.v_number = curwin->w_wrow + 1;
14356}
14357
14358/*
14359 * "winnr()" function
14360 */
14361 static void
14362f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
14363{
14364 int nr = 1;
14365
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014366 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014367 rettv->vval.v_number = nr;
14368}
14369
14370/*
14371 * "winrestcmd()" function
14372 */
14373 static void
14374f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
14375{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014376 win_T *wp;
14377 int winnr = 1;
14378 garray_T ga;
14379 char_u buf[50];
14380
14381 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020014382 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014383 {
14384 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
14385 ga_concat(&ga, buf);
14386 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
14387 ga_concat(&ga, buf);
14388 ++winnr;
14389 }
14390 ga_append(&ga, NUL);
14391
14392 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014393 rettv->v_type = VAR_STRING;
14394}
14395
14396/*
14397 * "winrestview()" function
14398 */
14399 static void
14400f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
14401{
14402 dict_T *dict;
14403
14404 if (argvars[0].v_type != VAR_DICT
14405 || (dict = argvars[0].vval.v_dict) == NULL)
14406 EMSG(_(e_invarg));
14407 else
14408 {
14409 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014410 curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014411 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014412 curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014413#ifdef FEAT_VIRTUALEDIT
14414 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014415 curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014416#endif
14417 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
14418 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010014419 curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014420 curwin->w_set_curswant = FALSE;
14421 }
14422
14423 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014424 set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014425#ifdef FEAT_DIFF
14426 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014427 curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014428#endif
14429 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014430 curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014431 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014432 curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014433
14434 check_cursor();
14435 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020014436 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014437 changed_window_setting();
14438
14439 if (curwin->w_topline <= 0)
14440 curwin->w_topline = 1;
14441 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
14442 curwin->w_topline = curbuf->b_ml.ml_line_count;
14443#ifdef FEAT_DIFF
14444 check_topfill(curwin, TRUE);
14445#endif
14446 }
14447}
14448
14449/*
14450 * "winsaveview()" function
14451 */
14452 static void
14453f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
14454{
14455 dict_T *dict;
14456
14457 if (rettv_dict_alloc(rettv) == FAIL)
14458 return;
14459 dict = rettv->vval.v_dict;
14460
Bram Moolenaare0be1672018-07-08 16:50:37 +020014461 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
14462 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014463#ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +020014464 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014465#endif
14466 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020014467 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014468
Bram Moolenaare0be1672018-07-08 16:50:37 +020014469 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014470#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020014471 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014472#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020014473 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
14474 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014475}
14476
14477/*
14478 * "winwidth(nr)" function
14479 */
14480 static void
14481f_winwidth(typval_T *argvars, typval_T *rettv)
14482{
14483 win_T *wp;
14484
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014485 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014486 if (wp == NULL)
14487 rettv->vval.v_number = -1;
14488 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014489 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014490}
14491
14492/*
14493 * "wordcount()" function
14494 */
14495 static void
14496f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
14497{
14498 if (rettv_dict_alloc(rettv) == FAIL)
14499 return;
14500 cursor_pos_info(rettv->vval.v_dict);
14501}
14502
14503/*
14504 * "writefile()" function
14505 */
14506 static void
14507f_writefile(typval_T *argvars, typval_T *rettv)
14508{
14509 int binary = FALSE;
14510 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014511#ifdef HAVE_FSYNC
14512 int do_fsync = p_fs;
14513#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014514 char_u *fname;
14515 FILE *fd;
14516 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014517 listitem_T *li;
14518 list_T *list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014519
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014520 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014521 if (check_restricted() || check_secure())
14522 return;
14523
14524 if (argvars[0].v_type != VAR_LIST)
14525 {
14526 EMSG2(_(e_listarg), "writefile()");
14527 return;
14528 }
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014529 list = argvars[0].vval.v_list;
14530 if (list == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014531 return;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014532 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014533 if (tv_get_string_chk(&li->li_tv) == NULL)
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014534 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014535
14536 if (argvars[2].v_type != VAR_UNKNOWN)
14537 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014538 char_u *arg2 = tv_get_string_chk(&argvars[2]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014539
14540 if (arg2 == NULL)
14541 return;
14542 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014543 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014544 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014545 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014546#ifdef HAVE_FSYNC
14547 if (vim_strchr(arg2, 's') != NULL)
14548 do_fsync = TRUE;
14549 else if (vim_strchr(arg2, 'S') != NULL)
14550 do_fsync = FALSE;
14551#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014552 }
14553
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014554 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014555 if (fname == NULL)
14556 return;
14557
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014558 /* Always open the file in binary mode, library functions have a mind of
14559 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014560 if (*fname == NUL || (fd = mch_fopen((char *)fname,
14561 append ? APPENDBIN : WRITEBIN)) == NULL)
14562 {
14563 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
14564 ret = -1;
14565 }
14566 else
14567 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014568 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014569 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014570#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010014571 else if (do_fsync)
14572 /* Ignore the error, the user wouldn't know what to do about it.
14573 * May happen for a device. */
Bram Moolenaar42335f52018-09-13 15:33:43 +020014574 vim_ignored = fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014575#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014576 fclose(fd);
14577 }
14578
14579 rettv->vval.v_number = ret;
14580}
14581
14582/*
14583 * "xor(expr, expr)" function
14584 */
14585 static void
14586f_xor(typval_T *argvars, typval_T *rettv)
14587{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014588 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
14589 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014590}
14591
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014592#endif /* FEAT_EVAL */