blob: 83c4b9447fb09bd22427305a9e082c1132147e65 [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 Moolenaarbf821bc2019-01-23 21:15:02 +010032static char *e_listblobarg = N_("E899: Argument of %s must be a List or Blob");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020033static char *e_stringreq = N_("E928: String required");
Bram Moolenaaraff74912019-03-30 18:11:49 +010034static char *e_invalwindow = N_("E957: Invalid window number");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020035
36#ifdef FEAT_FLOAT
37static void f_abs(typval_T *argvars, typval_T *rettv);
38static void f_acos(typval_T *argvars, typval_T *rettv);
39#endif
40static void f_add(typval_T *argvars, typval_T *rettv);
41static void f_and(typval_T *argvars, typval_T *rettv);
42static void f_append(typval_T *argvars, typval_T *rettv);
Bram Moolenaarca851592018-06-06 21:04:07 +020043static void f_appendbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020044static void f_argc(typval_T *argvars, typval_T *rettv);
45static void f_argidx(typval_T *argvars, typval_T *rettv);
46static void f_arglistid(typval_T *argvars, typval_T *rettv);
47static void f_argv(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +010048static void f_assert_beeps(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020049static void f_assert_equal(typval_T *argvars, typval_T *rettv);
Bram Moolenaard96ff162018-02-18 22:13:29 +010050static void f_assert_equalfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020051static void f_assert_exception(typval_T *argvars, typval_T *rettv);
52static void f_assert_fails(typval_T *argvars, typval_T *rettv);
53static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaar61c04492016-07-23 15:35:35 +020054static void f_assert_inrange(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020055static void f_assert_match(typval_T *argvars, typval_T *rettv);
56static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
57static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar42205552017-03-18 19:42:22 +010058static void f_assert_report(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020059static void f_assert_true(typval_T *argvars, typval_T *rettv);
60#ifdef FEAT_FLOAT
61static void f_asin(typval_T *argvars, typval_T *rettv);
62static void f_atan(typval_T *argvars, typval_T *rettv);
63static void f_atan2(typval_T *argvars, typval_T *rettv);
64#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010065#ifdef FEAT_BEVAL
66static void f_balloon_show(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010067# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010068static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010069# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010070#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020071static void f_browse(typval_T *argvars, typval_T *rettv);
72static void f_browsedir(typval_T *argvars, typval_T *rettv);
73static void f_bufexists(typval_T *argvars, typval_T *rettv);
74static void f_buflisted(typval_T *argvars, typval_T *rettv);
75static void f_bufloaded(typval_T *argvars, typval_T *rettv);
76static void f_bufname(typval_T *argvars, typval_T *rettv);
77static void f_bufnr(typval_T *argvars, typval_T *rettv);
78static void f_bufwinid(typval_T *argvars, typval_T *rettv);
79static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
80static void f_byte2line(typval_T *argvars, typval_T *rettv);
81static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
82static void f_byteidx(typval_T *argvars, typval_T *rettv);
83static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
84static void f_call(typval_T *argvars, typval_T *rettv);
85#ifdef FEAT_FLOAT
86static void f_ceil(typval_T *argvars, typval_T *rettv);
87#endif
88#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +010089static void f_ch_canread(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020090static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0874a832016-09-01 15:11:51 +020091static void f_ch_close_in(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020092static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
93static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
94static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
95static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
96static void f_ch_info(typval_T *argvars, typval_T *rettv);
97static void f_ch_log(typval_T *argvars, typval_T *rettv);
98static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
99static void f_ch_open(typval_T *argvars, typval_T *rettv);
100static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100101static void f_ch_readblob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200102static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
103static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
104static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
105static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
106static void f_ch_status(typval_T *argvars, typval_T *rettv);
107#endif
108static void f_changenr(typval_T *argvars, typval_T *rettv);
109static void f_char2nr(typval_T *argvars, typval_T *rettv);
110static void f_cindent(typval_T *argvars, typval_T *rettv);
111static void f_clearmatches(typval_T *argvars, typval_T *rettv);
112static void f_col(typval_T *argvars, typval_T *rettv);
113#if defined(FEAT_INS_EXPAND)
114static void f_complete(typval_T *argvars, typval_T *rettv);
115static void f_complete_add(typval_T *argvars, typval_T *rettv);
116static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfd133322019-03-29 12:20:27 +0100117static void f_complete_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200118#endif
119static void f_confirm(typval_T *argvars, typval_T *rettv);
120static void f_copy(typval_T *argvars, typval_T *rettv);
121#ifdef FEAT_FLOAT
122static void f_cos(typval_T *argvars, typval_T *rettv);
123static void f_cosh(typval_T *argvars, typval_T *rettv);
124#endif
125static void f_count(typval_T *argvars, typval_T *rettv);
126static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
127static void f_cursor(typval_T *argsvars, typval_T *rettv);
Bram Moolenaar4f974752019-02-17 17:44:42 +0100128#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200129static void f_debugbreak(typval_T *argvars, typval_T *rettv);
130#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200131static void f_deepcopy(typval_T *argvars, typval_T *rettv);
132static void f_delete(typval_T *argvars, typval_T *rettv);
Bram Moolenaard79a2622018-06-07 18:17:46 +0200133static void f_deletebufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200134static void f_did_filetype(typval_T *argvars, typval_T *rettv);
135static void f_diff_filler(typval_T *argvars, typval_T *rettv);
136static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
137static void f_empty(typval_T *argvars, typval_T *rettv);
138static void f_escape(typval_T *argvars, typval_T *rettv);
139static void f_eval(typval_T *argvars, typval_T *rettv);
140static void f_eventhandler(typval_T *argvars, typval_T *rettv);
141static void f_executable(typval_T *argvars, typval_T *rettv);
142static void f_execute(typval_T *argvars, typval_T *rettv);
143static void f_exepath(typval_T *argvars, typval_T *rettv);
144static void f_exists(typval_T *argvars, typval_T *rettv);
145#ifdef FEAT_FLOAT
146static void f_exp(typval_T *argvars, typval_T *rettv);
147#endif
148static void f_expand(typval_T *argvars, typval_T *rettv);
149static void f_extend(typval_T *argvars, typval_T *rettv);
150static void f_feedkeys(typval_T *argvars, typval_T *rettv);
151static void f_filereadable(typval_T *argvars, typval_T *rettv);
152static void f_filewritable(typval_T *argvars, typval_T *rettv);
153static void f_filter(typval_T *argvars, typval_T *rettv);
154static void f_finddir(typval_T *argvars, typval_T *rettv);
155static void f_findfile(typval_T *argvars, typval_T *rettv);
156#ifdef FEAT_FLOAT
157static void f_float2nr(typval_T *argvars, typval_T *rettv);
158static void f_floor(typval_T *argvars, typval_T *rettv);
159static void f_fmod(typval_T *argvars, typval_T *rettv);
160#endif
161static void f_fnameescape(typval_T *argvars, typval_T *rettv);
162static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
163static void f_foldclosed(typval_T *argvars, typval_T *rettv);
164static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
165static void f_foldlevel(typval_T *argvars, typval_T *rettv);
166static void f_foldtext(typval_T *argvars, typval_T *rettv);
167static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
168static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200169static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200170static void f_function(typval_T *argvars, typval_T *rettv);
171static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
172static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200173static void f_getbufinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200174static void f_getbufline(typval_T *argvars, typval_T *rettv);
175static void f_getbufvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100176static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200177static void f_getchar(typval_T *argvars, typval_T *rettv);
178static void f_getcharmod(typval_T *argvars, typval_T *rettv);
179static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
180static void f_getcmdline(typval_T *argvars, typval_T *rettv);
181#if defined(FEAT_CMDL_COMPL)
182static void f_getcompletion(typval_T *argvars, typval_T *rettv);
183#endif
184static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
185static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
186static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
187static void f_getcwd(typval_T *argvars, typval_T *rettv);
188static void f_getfontname(typval_T *argvars, typval_T *rettv);
189static void f_getfperm(typval_T *argvars, typval_T *rettv);
190static void f_getfsize(typval_T *argvars, typval_T *rettv);
191static void f_getftime(typval_T *argvars, typval_T *rettv);
192static void f_getftype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +0100193static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200194static void f_getline(typval_T *argvars, typval_T *rettv);
Bram Moolenaard823fa92016-08-12 16:29:27 +0200195static void f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200196static void f_getmatches(typval_T *argvars, typval_T *rettv);
197static void f_getpid(typval_T *argvars, typval_T *rettv);
198static void f_getcurpos(typval_T *argvars, typval_T *rettv);
199static void f_getpos(typval_T *argvars, typval_T *rettv);
200static void f_getqflist(typval_T *argvars, typval_T *rettv);
201static void f_getreg(typval_T *argvars, typval_T *rettv);
202static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200203static void f_gettabinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200204static void f_gettabvar(typval_T *argvars, typval_T *rettv);
205static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100206static void f_gettagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200207static void f_getwininfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100208static void f_getwinpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200209static void f_getwinposx(typval_T *argvars, typval_T *rettv);
210static void f_getwinposy(typval_T *argvars, typval_T *rettv);
211static void f_getwinvar(typval_T *argvars, typval_T *rettv);
212static void f_glob(typval_T *argvars, typval_T *rettv);
213static void f_globpath(typval_T *argvars, typval_T *rettv);
214static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
215static void f_has(typval_T *argvars, typval_T *rettv);
216static void f_has_key(typval_T *argvars, typval_T *rettv);
217static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
218static void f_hasmapto(typval_T *argvars, typval_T *rettv);
219static void f_histadd(typval_T *argvars, typval_T *rettv);
220static void f_histdel(typval_T *argvars, typval_T *rettv);
221static void f_histget(typval_T *argvars, typval_T *rettv);
222static void f_histnr(typval_T *argvars, typval_T *rettv);
223static void f_hlID(typval_T *argvars, typval_T *rettv);
224static void f_hlexists(typval_T *argvars, typval_T *rettv);
225static void f_hostname(typval_T *argvars, typval_T *rettv);
226static void f_iconv(typval_T *argvars, typval_T *rettv);
227static void f_indent(typval_T *argvars, typval_T *rettv);
228static void f_index(typval_T *argvars, typval_T *rettv);
229static void f_input(typval_T *argvars, typval_T *rettv);
230static void f_inputdialog(typval_T *argvars, typval_T *rettv);
231static void f_inputlist(typval_T *argvars, typval_T *rettv);
232static void f_inputrestore(typval_T *argvars, typval_T *rettv);
233static void f_inputsave(typval_T *argvars, typval_T *rettv);
234static void f_inputsecret(typval_T *argvars, typval_T *rettv);
235static void f_insert(typval_T *argvars, typval_T *rettv);
236static void f_invert(typval_T *argvars, typval_T *rettv);
237static void f_isdirectory(typval_T *argvars, typval_T *rettv);
238static void f_islocked(typval_T *argvars, typval_T *rettv);
239#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200240static void f_isinf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200241static void f_isnan(typval_T *argvars, typval_T *rettv);
242#endif
243static void f_items(typval_T *argvars, typval_T *rettv);
244#ifdef FEAT_JOB_CHANNEL
245static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
246static void f_job_info(typval_T *argvars, typval_T *rettv);
247static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
248static void f_job_start(typval_T *argvars, typval_T *rettv);
249static void f_job_stop(typval_T *argvars, typval_T *rettv);
250static void f_job_status(typval_T *argvars, typval_T *rettv);
251#endif
252static void f_join(typval_T *argvars, typval_T *rettv);
253static void f_js_decode(typval_T *argvars, typval_T *rettv);
254static void f_js_encode(typval_T *argvars, typval_T *rettv);
255static void f_json_decode(typval_T *argvars, typval_T *rettv);
256static void f_json_encode(typval_T *argvars, typval_T *rettv);
257static void f_keys(typval_T *argvars, typval_T *rettv);
258static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
259static void f_len(typval_T *argvars, typval_T *rettv);
260static void f_libcall(typval_T *argvars, typval_T *rettv);
261static void f_libcallnr(typval_T *argvars, typval_T *rettv);
262static void f_line(typval_T *argvars, typval_T *rettv);
263static void f_line2byte(typval_T *argvars, typval_T *rettv);
264static void f_lispindent(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9d401282019-04-06 13:18:12 +0200265static void f_list2str(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200266static void f_localtime(typval_T *argvars, typval_T *rettv);
267#ifdef FEAT_FLOAT
268static void f_log(typval_T *argvars, typval_T *rettv);
269static void f_log10(typval_T *argvars, typval_T *rettv);
270#endif
271#ifdef FEAT_LUA
272static void f_luaeval(typval_T *argvars, typval_T *rettv);
273#endif
274static void f_map(typval_T *argvars, typval_T *rettv);
275static void f_maparg(typval_T *argvars, typval_T *rettv);
276static void f_mapcheck(typval_T *argvars, typval_T *rettv);
277static void f_match(typval_T *argvars, typval_T *rettv);
278static void f_matchadd(typval_T *argvars, typval_T *rettv);
279static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
280static void f_matcharg(typval_T *argvars, typval_T *rettv);
281static void f_matchdelete(typval_T *argvars, typval_T *rettv);
282static void f_matchend(typval_T *argvars, typval_T *rettv);
283static void f_matchlist(typval_T *argvars, typval_T *rettv);
284static void f_matchstr(typval_T *argvars, typval_T *rettv);
285static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
286static void f_max(typval_T *argvars, typval_T *rettv);
287static void f_min(typval_T *argvars, typval_T *rettv);
288#ifdef vim_mkdir
289static void f_mkdir(typval_T *argvars, typval_T *rettv);
290#endif
291static void f_mode(typval_T *argvars, typval_T *rettv);
292#ifdef FEAT_MZSCHEME
293static void f_mzeval(typval_T *argvars, typval_T *rettv);
294#endif
295static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
296static void f_nr2char(typval_T *argvars, typval_T *rettv);
297static void f_or(typval_T *argvars, typval_T *rettv);
298static void f_pathshorten(typval_T *argvars, typval_T *rettv);
299#ifdef FEAT_PERL
300static void f_perleval(typval_T *argvars, typval_T *rettv);
301#endif
302#ifdef FEAT_FLOAT
303static void f_pow(typval_T *argvars, typval_T *rettv);
304#endif
305static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
306static void f_printf(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200307#ifdef FEAT_JOB_CHANNEL
308static void f_prompt_setcallback(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200309static void f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200310static void f_prompt_setprompt(typval_T *argvars, typval_T *rettv);
311#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200312static void f_pumvisible(typval_T *argvars, typval_T *rettv);
313#ifdef FEAT_PYTHON3
314static void f_py3eval(typval_T *argvars, typval_T *rettv);
315#endif
316#ifdef FEAT_PYTHON
317static void f_pyeval(typval_T *argvars, typval_T *rettv);
318#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100319#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
320static void f_pyxeval(typval_T *argvars, typval_T *rettv);
321#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200322static void f_range(typval_T *argvars, typval_T *rettv);
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200323static void f_readdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200324static void f_readfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200325static void f_reg_executing(typval_T *argvars, typval_T *rettv);
326static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200327static void f_reltime(typval_T *argvars, typval_T *rettv);
328#ifdef FEAT_FLOAT
329static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
330#endif
331static void f_reltimestr(typval_T *argvars, typval_T *rettv);
332static void f_remote_expr(typval_T *argvars, typval_T *rettv);
333static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
334static void f_remote_peek(typval_T *argvars, typval_T *rettv);
335static void f_remote_read(typval_T *argvars, typval_T *rettv);
336static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100337static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200338static void f_remove(typval_T *argvars, typval_T *rettv);
339static void f_rename(typval_T *argvars, typval_T *rettv);
340static void f_repeat(typval_T *argvars, typval_T *rettv);
341static void f_resolve(typval_T *argvars, typval_T *rettv);
342static void f_reverse(typval_T *argvars, typval_T *rettv);
343#ifdef FEAT_FLOAT
344static void f_round(typval_T *argvars, typval_T *rettv);
345#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100346#ifdef FEAT_RUBY
347static void f_rubyeval(typval_T *argvars, typval_T *rettv);
348#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200349static void f_screenattr(typval_T *argvars, typval_T *rettv);
350static void f_screenchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100351static void f_screenchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200352static void f_screencol(typval_T *argvars, typval_T *rettv);
353static void f_screenrow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100354static void f_screenstring(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200355static void f_search(typval_T *argvars, typval_T *rettv);
356static void f_searchdecl(typval_T *argvars, typval_T *rettv);
357static void f_searchpair(typval_T *argvars, typval_T *rettv);
358static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
359static void f_searchpos(typval_T *argvars, typval_T *rettv);
360static void f_server2client(typval_T *argvars, typval_T *rettv);
361static void f_serverlist(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200362static void f_setbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200363static void f_setbufvar(typval_T *argvars, typval_T *rettv);
364static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
365static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
366static void f_setfperm(typval_T *argvars, typval_T *rettv);
367static void f_setline(typval_T *argvars, typval_T *rettv);
368static void f_setloclist(typval_T *argvars, typval_T *rettv);
369static void f_setmatches(typval_T *argvars, typval_T *rettv);
370static void f_setpos(typval_T *argvars, typval_T *rettv);
371static void f_setqflist(typval_T *argvars, typval_T *rettv);
372static void f_setreg(typval_T *argvars, typval_T *rettv);
373static void f_settabvar(typval_T *argvars, typval_T *rettv);
374static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100375static void f_settagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200376static void f_setwinvar(typval_T *argvars, typval_T *rettv);
377#ifdef FEAT_CRYPT
378static void f_sha256(typval_T *argvars, typval_T *rettv);
379#endif /* FEAT_CRYPT */
380static void f_shellescape(typval_T *argvars, typval_T *rettv);
381static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
Bram Moolenaar162b7142018-12-21 15:17:36 +0100382#ifdef FEAT_SIGNS
383static void f_sign_define(typval_T *argvars, typval_T *rettv);
384static void f_sign_getdefined(typval_T *argvars, typval_T *rettv);
385static void f_sign_getplaced(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100386static void f_sign_jump(typval_T *argvars, typval_T *rettv);
Bram Moolenaar162b7142018-12-21 15:17:36 +0100387static void f_sign_place(typval_T *argvars, typval_T *rettv);
388static void f_sign_undefine(typval_T *argvars, typval_T *rettv);
389static void f_sign_unplace(typval_T *argvars, typval_T *rettv);
390#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200391static void f_simplify(typval_T *argvars, typval_T *rettv);
392#ifdef FEAT_FLOAT
393static void f_sin(typval_T *argvars, typval_T *rettv);
394static void f_sinh(typval_T *argvars, typval_T *rettv);
395#endif
396static void f_sort(typval_T *argvars, typval_T *rettv);
397static void f_soundfold(typval_T *argvars, typval_T *rettv);
398static void f_spellbadword(typval_T *argvars, typval_T *rettv);
399static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
400static void f_split(typval_T *argvars, typval_T *rettv);
401#ifdef FEAT_FLOAT
402static void f_sqrt(typval_T *argvars, typval_T *rettv);
403static void f_str2float(typval_T *argvars, typval_T *rettv);
404#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200405static void f_str2list(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200406static void f_str2nr(typval_T *argvars, typval_T *rettv);
407static void f_strchars(typval_T *argvars, typval_T *rettv);
408#ifdef HAVE_STRFTIME
409static void f_strftime(typval_T *argvars, typval_T *rettv);
410#endif
411static void f_strgetchar(typval_T *argvars, typval_T *rettv);
412static void f_stridx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200413static void f_strlen(typval_T *argvars, typval_T *rettv);
414static void f_strcharpart(typval_T *argvars, typval_T *rettv);
415static void f_strpart(typval_T *argvars, typval_T *rettv);
416static void f_strridx(typval_T *argvars, typval_T *rettv);
417static void f_strtrans(typval_T *argvars, typval_T *rettv);
418static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
419static void f_strwidth(typval_T *argvars, typval_T *rettv);
420static void f_submatch(typval_T *argvars, typval_T *rettv);
421static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200422static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200423static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200424static void f_synID(typval_T *argvars, typval_T *rettv);
425static void f_synIDattr(typval_T *argvars, typval_T *rettv);
426static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
427static void f_synstack(typval_T *argvars, typval_T *rettv);
428static void f_synconcealed(typval_T *argvars, typval_T *rettv);
429static void f_system(typval_T *argvars, typval_T *rettv);
430static void f_systemlist(typval_T *argvars, typval_T *rettv);
431static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
432static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
433static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
434static void f_taglist(typval_T *argvars, typval_T *rettv);
435static void f_tagfiles(typval_T *argvars, typval_T *rettv);
436static void f_tempname(typval_T *argvars, typval_T *rettv);
437static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
438static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200439static void f_test_feedinput(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200440static void f_test_option_not_set(typval_T *argvars, typval_T *rettv);
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100441static void f_test_override(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc3e92c12019-03-23 14:23:07 +0100442static void f_test_refcount(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200443static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100444static void f_test_ignore_error(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100445static void f_test_null_blob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200446#ifdef FEAT_JOB_CHANNEL
447static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
448#endif
449static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
450#ifdef FEAT_JOB_CHANNEL
451static void f_test_null_job(typval_T *argvars, typval_T *rettv);
452#endif
453static void f_test_null_list(typval_T *argvars, typval_T *rettv);
454static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
455static void f_test_null_string(typval_T *argvars, typval_T *rettv);
Bram Moolenaarab186732018-09-14 21:27:06 +0200456#ifdef FEAT_GUI
457static void f_test_scrollbar(typval_T *argvars, typval_T *rettv);
458#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200459static void f_test_settime(typval_T *argvars, typval_T *rettv);
460#ifdef FEAT_FLOAT
461static void f_tan(typval_T *argvars, typval_T *rettv);
462static void f_tanh(typval_T *argvars, typval_T *rettv);
463#endif
464#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200465static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200466static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200467static void f_timer_start(typval_T *argvars, typval_T *rettv);
468static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200469static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200470#endif
471static void f_tolower(typval_T *argvars, typval_T *rettv);
472static void f_toupper(typval_T *argvars, typval_T *rettv);
473static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100474static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200475#ifdef FEAT_FLOAT
476static void f_trunc(typval_T *argvars, typval_T *rettv);
477#endif
478static void f_type(typval_T *argvars, typval_T *rettv);
479static void f_undofile(typval_T *argvars, typval_T *rettv);
480static void f_undotree(typval_T *argvars, typval_T *rettv);
481static void f_uniq(typval_T *argvars, typval_T *rettv);
482static void f_values(typval_T *argvars, typval_T *rettv);
483static void f_virtcol(typval_T *argvars, typval_T *rettv);
484static void f_visualmode(typval_T *argvars, typval_T *rettv);
485static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
486static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
487static void f_win_getid(typval_T *argvars, typval_T *rettv);
488static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
489static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
490static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100491static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200492static void f_winbufnr(typval_T *argvars, typval_T *rettv);
493static void f_wincol(typval_T *argvars, typval_T *rettv);
494static void f_winheight(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200495static void f_winlayout(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200496static void f_winline(typval_T *argvars, typval_T *rettv);
497static void f_winnr(typval_T *argvars, typval_T *rettv);
498static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
499static void f_winrestview(typval_T *argvars, typval_T *rettv);
500static void f_winsaveview(typval_T *argvars, typval_T *rettv);
501static void f_winwidth(typval_T *argvars, typval_T *rettv);
502static void f_writefile(typval_T *argvars, typval_T *rettv);
503static void f_wordcount(typval_T *argvars, typval_T *rettv);
504static void f_xor(typval_T *argvars, typval_T *rettv);
505
506/*
507 * Array with names and number of arguments of all internal functions
508 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
509 */
510static struct fst
511{
512 char *f_name; /* function name */
513 char f_min_argc; /* minimal number of arguments */
514 char f_max_argc; /* maximal number of arguments */
515 void (*f_func)(typval_T *args, typval_T *rvar);
516 /* implementation of function */
517} functions[] =
518{
519#ifdef FEAT_FLOAT
520 {"abs", 1, 1, f_abs},
521 {"acos", 1, 1, f_acos}, /* WJMc */
522#endif
523 {"add", 2, 2, f_add},
524 {"and", 2, 2, f_and},
525 {"append", 2, 2, f_append},
Bram Moolenaarca851592018-06-06 21:04:07 +0200526 {"appendbufline", 3, 3, f_appendbufline},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200527 {"argc", 0, 1, f_argc},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200528 {"argidx", 0, 0, f_argidx},
529 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200530 {"argv", 0, 2, f_argv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200531#ifdef FEAT_FLOAT
532 {"asin", 1, 1, f_asin}, /* WJMc */
533#endif
Bram Moolenaarb48e96f2018-02-13 12:26:14 +0100534 {"assert_beeps", 1, 2, f_assert_beeps},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200535 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaard96ff162018-02-18 22:13:29 +0100536 {"assert_equalfile", 2, 2, f_assert_equalfile},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200537 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaar1307d1c2018-10-07 20:16:49 +0200538 {"assert_fails", 1, 3, f_assert_fails},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200539 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100540 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200541 {"assert_match", 2, 3, f_assert_match},
542 {"assert_notequal", 2, 3, f_assert_notequal},
543 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100544 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200545 {"assert_true", 1, 2, f_assert_true},
546#ifdef FEAT_FLOAT
547 {"atan", 1, 1, f_atan},
548 {"atan2", 2, 2, f_atan2},
549#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100550#ifdef FEAT_BEVAL
551 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100552# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100553 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100554# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100555#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200556 {"browse", 4, 4, f_browse},
557 {"browsedir", 2, 2, f_browsedir},
558 {"bufexists", 1, 1, f_bufexists},
559 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
560 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
561 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
562 {"buflisted", 1, 1, f_buflisted},
563 {"bufloaded", 1, 1, f_bufloaded},
564 {"bufname", 1, 1, f_bufname},
565 {"bufnr", 1, 2, f_bufnr},
566 {"bufwinid", 1, 1, f_bufwinid},
567 {"bufwinnr", 1, 1, f_bufwinnr},
568 {"byte2line", 1, 1, f_byte2line},
569 {"byteidx", 2, 2, f_byteidx},
570 {"byteidxcomp", 2, 2, f_byteidxcomp},
571 {"call", 2, 3, f_call},
572#ifdef FEAT_FLOAT
573 {"ceil", 1, 1, f_ceil},
574#endif
575#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100576 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200577 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200578 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200579 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
580 {"ch_evalraw", 2, 3, f_ch_evalraw},
581 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
582 {"ch_getjob", 1, 1, f_ch_getjob},
583 {"ch_info", 1, 1, f_ch_info},
584 {"ch_log", 1, 2, f_ch_log},
585 {"ch_logfile", 1, 2, f_ch_logfile},
586 {"ch_open", 1, 2, f_ch_open},
587 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100588 {"ch_readblob", 1, 2, f_ch_readblob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200589 {"ch_readraw", 1, 2, f_ch_readraw},
590 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
591 {"ch_sendraw", 2, 3, f_ch_sendraw},
592 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200593 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200594#endif
595 {"changenr", 0, 0, f_changenr},
596 {"char2nr", 1, 2, f_char2nr},
597 {"cindent", 1, 1, f_cindent},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100598 {"clearmatches", 0, 1, f_clearmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200599 {"col", 1, 1, f_col},
600#if defined(FEAT_INS_EXPAND)
601 {"complete", 2, 2, f_complete},
602 {"complete_add", 1, 1, f_complete_add},
603 {"complete_check", 0, 0, f_complete_check},
Bram Moolenaarfd133322019-03-29 12:20:27 +0100604 {"complete_info", 0, 1, f_complete_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200605#endif
606 {"confirm", 1, 4, f_confirm},
607 {"copy", 1, 1, f_copy},
608#ifdef FEAT_FLOAT
609 {"cos", 1, 1, f_cos},
610 {"cosh", 1, 1, f_cosh},
611#endif
612 {"count", 2, 4, f_count},
613 {"cscope_connection",0,3, f_cscope_connection},
614 {"cursor", 1, 3, f_cursor},
Bram Moolenaar4f974752019-02-17 17:44:42 +0100615#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200616 {"debugbreak", 1, 1, f_debugbreak},
617#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200618 {"deepcopy", 1, 2, f_deepcopy},
619 {"delete", 1, 2, f_delete},
Bram Moolenaard79a2622018-06-07 18:17:46 +0200620 {"deletebufline", 2, 3, f_deletebufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200621 {"did_filetype", 0, 0, f_did_filetype},
622 {"diff_filler", 1, 1, f_diff_filler},
623 {"diff_hlID", 2, 2, f_diff_hlID},
624 {"empty", 1, 1, f_empty},
625 {"escape", 2, 2, f_escape},
626 {"eval", 1, 1, f_eval},
627 {"eventhandler", 0, 0, f_eventhandler},
628 {"executable", 1, 1, f_executable},
629 {"execute", 1, 2, f_execute},
630 {"exepath", 1, 1, f_exepath},
631 {"exists", 1, 1, f_exists},
632#ifdef FEAT_FLOAT
633 {"exp", 1, 1, f_exp},
634#endif
635 {"expand", 1, 3, f_expand},
636 {"extend", 2, 3, f_extend},
637 {"feedkeys", 1, 2, f_feedkeys},
638 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
639 {"filereadable", 1, 1, f_filereadable},
640 {"filewritable", 1, 1, f_filewritable},
641 {"filter", 2, 2, f_filter},
642 {"finddir", 1, 3, f_finddir},
643 {"findfile", 1, 3, f_findfile},
644#ifdef FEAT_FLOAT
645 {"float2nr", 1, 1, f_float2nr},
646 {"floor", 1, 1, f_floor},
647 {"fmod", 2, 2, f_fmod},
648#endif
649 {"fnameescape", 1, 1, f_fnameescape},
650 {"fnamemodify", 2, 2, f_fnamemodify},
651 {"foldclosed", 1, 1, f_foldclosed},
652 {"foldclosedend", 1, 1, f_foldclosedend},
653 {"foldlevel", 1, 1, f_foldlevel},
654 {"foldtext", 0, 0, f_foldtext},
655 {"foldtextresult", 1, 1, f_foldtextresult},
656 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200657 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200658 {"function", 1, 3, f_function},
659 {"garbagecollect", 0, 1, f_garbagecollect},
660 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200661 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200662 {"getbufline", 2, 3, f_getbufline},
663 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100664 {"getchangelist", 1, 1, f_getchangelist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200665 {"getchar", 0, 1, f_getchar},
666 {"getcharmod", 0, 0, f_getcharmod},
667 {"getcharsearch", 0, 0, f_getcharsearch},
668 {"getcmdline", 0, 0, f_getcmdline},
669 {"getcmdpos", 0, 0, f_getcmdpos},
670 {"getcmdtype", 0, 0, f_getcmdtype},
671 {"getcmdwintype", 0, 0, f_getcmdwintype},
672#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200673 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200674#endif
675 {"getcurpos", 0, 0, f_getcurpos},
676 {"getcwd", 0, 2, f_getcwd},
677 {"getfontname", 0, 1, f_getfontname},
678 {"getfperm", 1, 1, f_getfperm},
679 {"getfsize", 1, 1, f_getfsize},
680 {"getftime", 1, 1, f_getftime},
681 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100682 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200683 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200684 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100685 {"getmatches", 0, 1, f_getmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200686 {"getpid", 0, 0, f_getpid},
687 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200688 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200689 {"getreg", 0, 3, f_getreg},
690 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200691 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200692 {"gettabvar", 2, 3, f_gettabvar},
693 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100694 {"gettagstack", 0, 1, f_gettagstack},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200695 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100696 {"getwinpos", 0, 1, f_getwinpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200697 {"getwinposx", 0, 0, f_getwinposx},
698 {"getwinposy", 0, 0, f_getwinposy},
699 {"getwinvar", 2, 3, f_getwinvar},
700 {"glob", 1, 4, f_glob},
701 {"glob2regpat", 1, 1, f_glob2regpat},
702 {"globpath", 2, 5, f_globpath},
703 {"has", 1, 1, f_has},
704 {"has_key", 2, 2, f_has_key},
705 {"haslocaldir", 0, 2, f_haslocaldir},
706 {"hasmapto", 1, 3, f_hasmapto},
707 {"highlightID", 1, 1, f_hlID}, /* obsolete */
708 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
709 {"histadd", 2, 2, f_histadd},
710 {"histdel", 1, 2, f_histdel},
711 {"histget", 1, 2, f_histget},
712 {"histnr", 1, 1, f_histnr},
713 {"hlID", 1, 1, f_hlID},
714 {"hlexists", 1, 1, f_hlexists},
715 {"hostname", 0, 0, f_hostname},
716 {"iconv", 3, 3, f_iconv},
717 {"indent", 1, 1, f_indent},
718 {"index", 2, 4, f_index},
719 {"input", 1, 3, f_input},
720 {"inputdialog", 1, 3, f_inputdialog},
721 {"inputlist", 1, 1, f_inputlist},
722 {"inputrestore", 0, 0, f_inputrestore},
723 {"inputsave", 0, 0, f_inputsave},
724 {"inputsecret", 1, 2, f_inputsecret},
725 {"insert", 2, 3, f_insert},
726 {"invert", 1, 1, f_invert},
727 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200728#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
729 {"isinf", 1, 1, f_isinf},
730#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200731 {"islocked", 1, 1, f_islocked},
732#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
733 {"isnan", 1, 1, f_isnan},
734#endif
735 {"items", 1, 1, f_items},
736#ifdef FEAT_JOB_CHANNEL
737 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200738 {"job_info", 0, 1, f_job_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200739 {"job_setoptions", 2, 2, f_job_setoptions},
740 {"job_start", 1, 2, f_job_start},
741 {"job_status", 1, 1, f_job_status},
742 {"job_stop", 1, 2, f_job_stop},
743#endif
744 {"join", 1, 2, f_join},
745 {"js_decode", 1, 1, f_js_decode},
746 {"js_encode", 1, 1, f_js_encode},
747 {"json_decode", 1, 1, f_json_decode},
748 {"json_encode", 1, 1, f_json_encode},
749 {"keys", 1, 1, f_keys},
750 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
751 {"len", 1, 1, f_len},
752 {"libcall", 3, 3, f_libcall},
753 {"libcallnr", 3, 3, f_libcallnr},
754 {"line", 1, 1, f_line},
755 {"line2byte", 1, 1, f_line2byte},
756 {"lispindent", 1, 1, f_lispindent},
Bram Moolenaar9d401282019-04-06 13:18:12 +0200757 {"list2str", 1, 2, f_list2str},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200758 {"localtime", 0, 0, f_localtime},
759#ifdef FEAT_FLOAT
760 {"log", 1, 1, f_log},
761 {"log10", 1, 1, f_log10},
762#endif
763#ifdef FEAT_LUA
764 {"luaeval", 1, 2, f_luaeval},
765#endif
766 {"map", 2, 2, f_map},
767 {"maparg", 1, 4, f_maparg},
768 {"mapcheck", 1, 3, f_mapcheck},
769 {"match", 2, 4, f_match},
770 {"matchadd", 2, 5, f_matchadd},
771 {"matchaddpos", 2, 5, f_matchaddpos},
772 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100773 {"matchdelete", 1, 2, f_matchdelete},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200774 {"matchend", 2, 4, f_matchend},
775 {"matchlist", 2, 4, f_matchlist},
776 {"matchstr", 2, 4, f_matchstr},
777 {"matchstrpos", 2, 4, f_matchstrpos},
778 {"max", 1, 1, f_max},
779 {"min", 1, 1, f_min},
780#ifdef vim_mkdir
781 {"mkdir", 1, 3, f_mkdir},
782#endif
783 {"mode", 0, 1, f_mode},
784#ifdef FEAT_MZSCHEME
785 {"mzeval", 1, 1, f_mzeval},
786#endif
787 {"nextnonblank", 1, 1, f_nextnonblank},
788 {"nr2char", 1, 2, f_nr2char},
789 {"or", 2, 2, f_or},
790 {"pathshorten", 1, 1, f_pathshorten},
791#ifdef FEAT_PERL
792 {"perleval", 1, 1, f_perleval},
793#endif
794#ifdef FEAT_FLOAT
795 {"pow", 2, 2, f_pow},
796#endif
797 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100798 {"printf", 1, 19, f_printf},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200799#ifdef FEAT_JOB_CHANNEL
800 {"prompt_setcallback", 2, 2, f_prompt_setcallback},
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200801 {"prompt_setinterrupt", 2, 2, f_prompt_setinterrupt},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200802 {"prompt_setprompt", 2, 2, f_prompt_setprompt},
803#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100804#ifdef FEAT_TEXT_PROP
805 {"prop_add", 3, 3, f_prop_add},
806 {"prop_clear", 1, 3, f_prop_clear},
807 {"prop_list", 1, 2, f_prop_list},
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100808 {"prop_remove", 1, 3, f_prop_remove},
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100809 {"prop_type_add", 2, 2, f_prop_type_add},
810 {"prop_type_change", 2, 2, f_prop_type_change},
811 {"prop_type_delete", 1, 2, f_prop_type_delete},
812 {"prop_type_get", 1, 2, f_prop_type_get},
813 {"prop_type_list", 0, 1, f_prop_type_list},
814#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200815 {"pumvisible", 0, 0, f_pumvisible},
816#ifdef FEAT_PYTHON3
817 {"py3eval", 1, 1, f_py3eval},
818#endif
819#ifdef FEAT_PYTHON
820 {"pyeval", 1, 1, f_pyeval},
821#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100822#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
823 {"pyxeval", 1, 1, f_pyxeval},
824#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200825 {"range", 1, 3, f_range},
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200826 {"readdir", 1, 2, f_readdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200827 {"readfile", 1, 3, f_readfile},
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200828 {"reg_executing", 0, 0, f_reg_executing},
829 {"reg_recording", 0, 0, f_reg_recording},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200830 {"reltime", 0, 2, f_reltime},
831#ifdef FEAT_FLOAT
832 {"reltimefloat", 1, 1, f_reltimefloat},
833#endif
834 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100835 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200836 {"remote_foreground", 1, 1, f_remote_foreground},
837 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100838 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200839 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100840 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200841 {"remove", 2, 3, f_remove},
842 {"rename", 2, 2, f_rename},
843 {"repeat", 2, 2, f_repeat},
844 {"resolve", 1, 1, f_resolve},
845 {"reverse", 1, 1, f_reverse},
846#ifdef FEAT_FLOAT
847 {"round", 1, 1, f_round},
848#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100849#ifdef FEAT_RUBY
850 {"rubyeval", 1, 1, f_rubyeval},
851#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200852 {"screenattr", 2, 2, f_screenattr},
853 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100854 {"screenchars", 2, 2, f_screenchars},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200855 {"screencol", 0, 0, f_screencol},
856 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100857 {"screenstring", 2, 2, f_screenstring},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200858 {"search", 1, 4, f_search},
859 {"searchdecl", 1, 3, f_searchdecl},
860 {"searchpair", 3, 7, f_searchpair},
861 {"searchpairpos", 3, 7, f_searchpairpos},
862 {"searchpos", 1, 4, f_searchpos},
863 {"server2client", 2, 2, f_server2client},
864 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200865 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200866 {"setbufvar", 3, 3, f_setbufvar},
867 {"setcharsearch", 1, 1, f_setcharsearch},
868 {"setcmdpos", 1, 1, f_setcmdpos},
869 {"setfperm", 2, 2, f_setfperm},
870 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200871 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100872 {"setmatches", 1, 2, f_setmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200873 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200874 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200875 {"setreg", 2, 3, f_setreg},
876 {"settabvar", 3, 3, f_settabvar},
877 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100878 {"settagstack", 2, 3, f_settagstack},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200879 {"setwinvar", 3, 3, f_setwinvar},
880#ifdef FEAT_CRYPT
881 {"sha256", 1, 1, f_sha256},
882#endif
883 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaarf9514162018-11-22 03:08:29 +0100884 {"shiftwidth", 0, 1, f_shiftwidth},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100885#ifdef FEAT_SIGNS
886 {"sign_define", 1, 2, f_sign_define},
887 {"sign_getdefined", 0, 1, f_sign_getdefined},
888 {"sign_getplaced", 0, 2, f_sign_getplaced},
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100889 {"sign_jump", 3, 3, f_sign_jump},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100890 {"sign_place", 4, 5, f_sign_place},
891 {"sign_undefine", 0, 1, f_sign_undefine},
892 {"sign_unplace", 1, 2, f_sign_unplace},
893#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200894 {"simplify", 1, 1, f_simplify},
895#ifdef FEAT_FLOAT
896 {"sin", 1, 1, f_sin},
897 {"sinh", 1, 1, f_sinh},
898#endif
899 {"sort", 1, 3, f_sort},
900 {"soundfold", 1, 1, f_soundfold},
901 {"spellbadword", 0, 1, f_spellbadword},
902 {"spellsuggest", 1, 3, f_spellsuggest},
903 {"split", 1, 3, f_split},
904#ifdef FEAT_FLOAT
905 {"sqrt", 1, 1, f_sqrt},
906 {"str2float", 1, 1, f_str2float},
907#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200908 {"str2list", 1, 2, f_str2list},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200909 {"str2nr", 1, 2, f_str2nr},
910 {"strcharpart", 2, 3, f_strcharpart},
911 {"strchars", 1, 2, f_strchars},
912 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
913#ifdef HAVE_STRFTIME
914 {"strftime", 1, 2, f_strftime},
915#endif
916 {"strgetchar", 2, 2, f_strgetchar},
917 {"stridx", 2, 3, f_stridx},
918 {"string", 1, 1, f_string},
919 {"strlen", 1, 1, f_strlen},
920 {"strpart", 2, 3, f_strpart},
921 {"strridx", 2, 3, f_strridx},
922 {"strtrans", 1, 1, f_strtrans},
923 {"strwidth", 1, 1, f_strwidth},
924 {"submatch", 1, 2, f_submatch},
925 {"substitute", 4, 4, f_substitute},
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200926 {"swapinfo", 1, 1, f_swapinfo},
Bram Moolenaar110bd602018-09-16 18:46:59 +0200927 {"swapname", 1, 1, f_swapname},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200928 {"synID", 3, 3, f_synID},
929 {"synIDattr", 2, 3, f_synIDattr},
930 {"synIDtrans", 1, 1, f_synIDtrans},
931 {"synconcealed", 2, 2, f_synconcealed},
932 {"synstack", 2, 2, f_synstack},
933 {"system", 1, 2, f_system},
934 {"systemlist", 1, 2, f_systemlist},
935 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
936 {"tabpagenr", 0, 1, f_tabpagenr},
937 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
938 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100939 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200940#ifdef FEAT_FLOAT
941 {"tan", 1, 1, f_tan},
942 {"tanh", 1, 1, f_tanh},
943#endif
944 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200945#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100946 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
947 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100948 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200949 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200950# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
951 {"term_getansicolors", 1, 1, f_term_getansicolors},
952# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200953 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200954 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200955 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200956 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200957 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200958 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200959 {"term_getstatus", 1, 1, f_term_getstatus},
960 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200961 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200962 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200963 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200964 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200965# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
966 {"term_setansicolors", 2, 2, f_term_setansicolors},
967# endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100968 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100969 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaara42d3632018-04-14 17:05:38 +0200970 {"term_setsize", 3, 3, f_term_setsize},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200971 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200972 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200973#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200974 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
975 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200976 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200977 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100978 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +0100979 {"test_null_blob", 0, 0, f_test_null_blob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200980#ifdef FEAT_JOB_CHANNEL
981 {"test_null_channel", 0, 0, f_test_null_channel},
982#endif
983 {"test_null_dict", 0, 0, f_test_null_dict},
984#ifdef FEAT_JOB_CHANNEL
985 {"test_null_job", 0, 0, f_test_null_job},
986#endif
987 {"test_null_list", 0, 0, f_test_null_list},
988 {"test_null_partial", 0, 0, f_test_null_partial},
989 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200990 {"test_option_not_set", 1, 1, f_test_option_not_set},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +0100991 {"test_override", 2, 2, f_test_override},
992 {"test_refcount", 1, 1, f_test_refcount},
Bram Moolenaarab186732018-09-14 21:27:06 +0200993#ifdef FEAT_GUI
994 {"test_scrollbar", 3, 3, f_test_scrollbar},
995#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200996 {"test_settime", 1, 1, f_test_settime},
997#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200998 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200999 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001000 {"timer_start", 2, 3, f_timer_start},
1001 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +02001002 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001003#endif
1004 {"tolower", 1, 1, f_tolower},
1005 {"toupper", 1, 1, f_toupper},
1006 {"tr", 3, 3, f_tr},
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01001007 {"trim", 1, 2, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001008#ifdef FEAT_FLOAT
1009 {"trunc", 1, 1, f_trunc},
1010#endif
1011 {"type", 1, 1, f_type},
1012 {"undofile", 1, 1, f_undofile},
1013 {"undotree", 0, 0, f_undotree},
1014 {"uniq", 1, 3, f_uniq},
1015 {"values", 1, 1, f_values},
1016 {"virtcol", 1, 1, f_virtcol},
1017 {"visualmode", 0, 1, f_visualmode},
1018 {"wildmenumode", 0, 0, f_wildmenumode},
1019 {"win_findbuf", 1, 1, f_win_findbuf},
1020 {"win_getid", 0, 2, f_win_getid},
1021 {"win_gotoid", 1, 1, f_win_gotoid},
1022 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
1023 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +01001024 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001025 {"winbufnr", 1, 1, f_winbufnr},
1026 {"wincol", 0, 0, f_wincol},
1027 {"winheight", 1, 1, f_winheight},
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +02001028 {"winlayout", 0, 1, f_winlayout},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001029 {"winline", 0, 0, f_winline},
1030 {"winnr", 0, 1, f_winnr},
1031 {"winrestcmd", 0, 0, f_winrestcmd},
1032 {"winrestview", 1, 1, f_winrestview},
1033 {"winsaveview", 0, 0, f_winsaveview},
1034 {"winwidth", 1, 1, f_winwidth},
1035 {"wordcount", 0, 0, f_wordcount},
1036 {"writefile", 2, 3, f_writefile},
1037 {"xor", 2, 2, f_xor},
1038};
1039
1040#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
1041
1042/*
1043 * Function given to ExpandGeneric() to obtain the list of internal
1044 * or user defined function names.
1045 */
1046 char_u *
1047get_function_name(expand_T *xp, int idx)
1048{
1049 static int intidx = -1;
1050 char_u *name;
1051
1052 if (idx == 0)
1053 intidx = -1;
1054 if (intidx < 0)
1055 {
1056 name = get_user_func_name(xp, idx);
1057 if (name != NULL)
1058 return name;
1059 }
1060 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
1061 {
1062 STRCPY(IObuff, functions[intidx].f_name);
1063 STRCAT(IObuff, "(");
1064 if (functions[intidx].f_max_argc == 0)
1065 STRCAT(IObuff, ")");
1066 return IObuff;
1067 }
1068
1069 return NULL;
1070}
1071
1072/*
1073 * Function given to ExpandGeneric() to obtain the list of internal or
1074 * user defined variable or function names.
1075 */
1076 char_u *
1077get_expr_name(expand_T *xp, int idx)
1078{
1079 static int intidx = -1;
1080 char_u *name;
1081
1082 if (idx == 0)
1083 intidx = -1;
1084 if (intidx < 0)
1085 {
1086 name = get_function_name(xp, idx);
1087 if (name != NULL)
1088 return name;
1089 }
1090 return get_user_var_name(xp, ++intidx);
1091}
1092
1093#endif /* FEAT_CMDL_COMPL */
1094
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001095/*
1096 * Find internal function in table above.
1097 * Return index, or -1 if not found
1098 */
1099 int
1100find_internal_func(
1101 char_u *name) /* name of the function */
1102{
1103 int first = 0;
1104 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
1105 int cmp;
1106 int x;
1107
1108 /*
1109 * Find the function name in the table. Binary search.
1110 */
1111 while (first <= last)
1112 {
1113 x = first + ((unsigned)(last - first) >> 1);
1114 cmp = STRCMP(name, functions[x].f_name);
1115 if (cmp < 0)
1116 last = x - 1;
1117 else if (cmp > 0)
1118 first = x + 1;
1119 else
1120 return x;
1121 }
1122 return -1;
1123}
1124
1125 int
1126call_internal_func(
1127 char_u *name,
1128 int argcount,
1129 typval_T *argvars,
1130 typval_T *rettv)
1131{
1132 int i;
1133
1134 i = find_internal_func(name);
1135 if (i < 0)
1136 return ERROR_UNKNOWN;
1137 if (argcount < functions[i].f_min_argc)
1138 return ERROR_TOOFEW;
1139 if (argcount > functions[i].f_max_argc)
1140 return ERROR_TOOMANY;
1141 argvars[argcount].v_type = VAR_UNKNOWN;
1142 functions[i].f_func(argvars, rettv);
1143 return ERROR_NONE;
1144}
1145
1146/*
1147 * Return TRUE for a non-zero Number and a non-empty String.
1148 */
1149 static int
1150non_zero_arg(typval_T *argvars)
1151{
1152 return ((argvars[0].v_type == VAR_NUMBER
1153 && argvars[0].vval.v_number != 0)
1154 || (argvars[0].v_type == VAR_SPECIAL
1155 && argvars[0].vval.v_number == VVAL_TRUE)
1156 || (argvars[0].v_type == VAR_STRING
1157 && argvars[0].vval.v_string != NULL
1158 && *argvars[0].vval.v_string != NUL));
1159}
1160
1161/*
1162 * Get the lnum from the first argument.
1163 * Also accepts ".", "$", etc., but that only works for the current buffer.
1164 * Returns -1 on error.
1165 */
1166 static linenr_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001167tv_get_lnum(typval_T *argvars)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001168{
1169 typval_T rettv;
1170 linenr_T lnum;
1171
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001172 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001173 if (lnum == 0) /* no valid number, try using line() */
1174 {
1175 rettv.v_type = VAR_NUMBER;
1176 f_line(argvars, &rettv);
1177 lnum = (linenr_T)rettv.vval.v_number;
1178 clear_tv(&rettv);
1179 }
1180 return lnum;
1181}
1182
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001183/*
1184 * Get the lnum from the first argument.
1185 * Also accepts "$", then "buf" is used.
1186 * Returns 0 on error.
1187 */
1188 static linenr_T
1189tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
1190{
1191 if (argvars[0].v_type == VAR_STRING
1192 && argvars[0].vval.v_string != NULL
1193 && argvars[0].vval.v_string[0] == '$'
1194 && buf != NULL)
1195 return buf->b_ml.ml_line_count;
1196 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1197}
1198
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001199#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001200/*
1201 * Get the float value of "argvars[0]" into "f".
1202 * Returns FAIL when the argument is not a Number or Float.
1203 */
1204 static int
1205get_float_arg(typval_T *argvars, float_T *f)
1206{
1207 if (argvars[0].v_type == VAR_FLOAT)
1208 {
1209 *f = argvars[0].vval.v_float;
1210 return OK;
1211 }
1212 if (argvars[0].v_type == VAR_NUMBER)
1213 {
1214 *f = (float_T)argvars[0].vval.v_number;
1215 return OK;
1216 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001217 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001218 return FAIL;
1219}
1220
1221/*
1222 * "abs(expr)" function
1223 */
1224 static void
1225f_abs(typval_T *argvars, typval_T *rettv)
1226{
1227 if (argvars[0].v_type == VAR_FLOAT)
1228 {
1229 rettv->v_type = VAR_FLOAT;
1230 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1231 }
1232 else
1233 {
1234 varnumber_T n;
1235 int error = FALSE;
1236
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001237 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001238 if (error)
1239 rettv->vval.v_number = -1;
1240 else if (n > 0)
1241 rettv->vval.v_number = n;
1242 else
1243 rettv->vval.v_number = -n;
1244 }
1245}
1246
1247/*
1248 * "acos()" function
1249 */
1250 static void
1251f_acos(typval_T *argvars, typval_T *rettv)
1252{
1253 float_T f = 0.0;
1254
1255 rettv->v_type = VAR_FLOAT;
1256 if (get_float_arg(argvars, &f) == OK)
1257 rettv->vval.v_float = acos(f);
1258 else
1259 rettv->vval.v_float = 0.0;
1260}
1261#endif
1262
1263/*
1264 * "add(list, item)" function
1265 */
1266 static void
1267f_add(typval_T *argvars, typval_T *rettv)
1268{
1269 list_T *l;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001270 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001271
1272 rettv->vval.v_number = 1; /* Default: Failed */
1273 if (argvars[0].v_type == VAR_LIST)
1274 {
1275 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001276 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001277 (char_u *)N_("add() argument"), TRUE)
1278 && list_append_tv(l, &argvars[1]) == OK)
1279 copy_tv(&argvars[0], rettv);
1280 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001281 else if (argvars[0].v_type == VAR_BLOB)
1282 {
1283 if ((b = argvars[0].vval.v_blob) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001284 && !var_check_lock(b->bv_lock,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001285 (char_u *)N_("add() argument"), TRUE))
1286 {
Bram Moolenaar05500ec2019-01-13 19:10:33 +01001287 int error = FALSE;
1288 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
1289
1290 if (!error)
1291 {
1292 ga_append(&b->bv_ga, (int)n);
1293 copy_tv(&argvars[0], rettv);
1294 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001295 }
1296 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001297 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01001298 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001299}
1300
1301/*
1302 * "and(expr, expr)" function
1303 */
1304 static void
1305f_and(typval_T *argvars, typval_T *rettv)
1306{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001307 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1308 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001309}
1310
1311/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02001312 * If there is a window for "curbuf", make it the current window.
1313 */
1314 static void
1315find_win_for_curbuf(void)
1316{
1317 wininfo_T *wip;
1318
1319 for (wip = curbuf->b_wininfo; wip != NULL; wip = wip->wi_next)
1320 {
1321 if (wip->wi_win != NULL)
1322 {
1323 curwin = wip->wi_win;
1324 break;
1325 }
1326 }
1327}
1328
1329/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001330 * Set line or list of lines in buffer "buf".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001331 */
1332 static void
Bram Moolenaarca851592018-06-06 21:04:07 +02001333set_buffer_lines(
1334 buf_T *buf,
1335 linenr_T lnum_arg,
1336 int append,
1337 typval_T *lines,
1338 typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001339{
Bram Moolenaarca851592018-06-06 21:04:07 +02001340 linenr_T lnum = lnum_arg + (append ? 1 : 0);
1341 char_u *line = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001342 list_T *l = NULL;
1343 listitem_T *li = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001344 long added = 0;
Bram Moolenaarca851592018-06-06 21:04:07 +02001345 linenr_T append_lnum;
1346 buf_T *curbuf_save = NULL;
1347 win_T *curwin_save = NULL;
1348 int is_curbuf = buf == curbuf;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001349
Bram Moolenaarca851592018-06-06 21:04:07 +02001350 /* When using the current buffer ml_mfp will be set if needed. Useful when
1351 * setline() is used on startup. For other buffers the buffer must be
1352 * loaded. */
1353 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001354 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001355 rettv->vval.v_number = 1; /* FAIL */
1356 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001357 }
1358
Bram Moolenaarca851592018-06-06 21:04:07 +02001359 if (!is_curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001360 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001361 curbuf_save = curbuf;
1362 curwin_save = curwin;
1363 curbuf = buf;
Bram Moolenaard79a2622018-06-07 18:17:46 +02001364 find_win_for_curbuf();
Bram Moolenaarca851592018-06-06 21:04:07 +02001365 }
1366
1367 if (append)
1368 // appendbufline() uses the line number below which we insert
1369 append_lnum = lnum - 1;
1370 else
1371 // setbufline() uses the line number above which we insert, we only
1372 // append if it's below the last line
1373 append_lnum = curbuf->b_ml.ml_line_count;
1374
1375 if (lines->v_type == VAR_LIST)
1376 {
1377 l = lines->vval.v_list;
1378 li = l->lv_first;
1379 }
1380 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001381 line = tv_get_string_chk(lines);
Bram Moolenaarca851592018-06-06 21:04:07 +02001382
1383 /* default result is zero == OK */
1384 for (;;)
1385 {
1386 if (l != NULL)
1387 {
1388 /* list argument, get next string */
1389 if (li == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001390 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001391 line = tv_get_string_chk(&li->li_tv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001392 li = li->li_next;
1393 }
1394
Bram Moolenaarca851592018-06-06 21:04:07 +02001395 rettv->vval.v_number = 1; /* FAIL */
1396 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
1397 break;
1398
1399 /* When coming here from Insert mode, sync undo, so that this can be
1400 * undone separately from what was previously inserted. */
1401 if (u_sync_once == 2)
1402 {
1403 u_sync_once = 1; /* notify that u_sync() was called */
1404 u_sync(TRUE);
1405 }
1406
1407 if (!append && lnum <= curbuf->b_ml.ml_line_count)
1408 {
Bram Moolenaar21b50382019-01-04 18:07:24 +01001409 // Existing line, replace it.
1410 // Removes any existing text properties.
1411 if (u_savesub(lnum) == OK && ml_replace_len(
1412 lnum, line, (colnr_T)STRLEN(line) + 1, TRUE, TRUE) == OK)
Bram Moolenaarca851592018-06-06 21:04:07 +02001413 {
1414 changed_bytes(lnum, 0);
1415 if (is_curbuf && lnum == curwin->w_cursor.lnum)
1416 check_cursor_col();
1417 rettv->vval.v_number = 0; /* OK */
1418 }
1419 }
1420 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
1421 {
1422 /* append the line */
1423 ++added;
1424 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
1425 rettv->vval.v_number = 0; /* OK */
1426 }
1427
1428 if (l == NULL) /* only one string argument */
1429 break;
1430 ++lnum;
1431 }
1432
1433 if (added > 0)
1434 {
1435 win_T *wp;
1436 tabpage_T *tp;
1437
1438 appended_lines_mark(append_lnum, added);
1439 FOR_ALL_TAB_WINDOWS(tp, wp)
1440 if (wp->w_buffer == buf && wp->w_cursor.lnum > append_lnum)
1441 wp->w_cursor.lnum += added;
1442 check_cursor_col();
1443
Bram Moolenaarf2732452018-06-03 14:47:35 +02001444#ifdef FEAT_JOB_CHANNEL
1445 if (bt_prompt(curbuf) && (State & INSERT))
1446 // show the line with the prompt
1447 update_topline();
1448#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001449 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001450
1451 if (!is_curbuf)
1452 {
1453 curbuf = curbuf_save;
1454 curwin = curwin_save;
1455 }
1456}
1457
1458/*
1459 * "append(lnum, string/list)" function
1460 */
1461 static void
1462f_append(typval_T *argvars, typval_T *rettv)
1463{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001464 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaarca851592018-06-06 21:04:07 +02001465
1466 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1467}
1468
1469/*
1470 * "appendbufline(buf, lnum, string/list)" function
1471 */
1472 static void
1473f_appendbufline(typval_T *argvars, typval_T *rettv)
1474{
1475 linenr_T lnum;
1476 buf_T *buf;
1477
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001478 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarca851592018-06-06 21:04:07 +02001479 if (buf == NULL)
1480 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001481 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001482 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001483 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +02001484 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1485 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001486}
1487
1488/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001489 * "argc([window id])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001490 */
1491 static void
Bram Moolenaare6e39892018-10-25 12:32:11 +02001492f_argc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001493{
Bram Moolenaare6e39892018-10-25 12:32:11 +02001494 win_T *wp;
1495
1496 if (argvars[0].v_type == VAR_UNKNOWN)
1497 // use the current window
1498 rettv->vval.v_number = ARGCOUNT;
1499 else if (argvars[0].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001500 && tv_get_number(&argvars[0]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001501 // use the global argument list
1502 rettv->vval.v_number = GARGCOUNT;
1503 else
1504 {
1505 // use the argument list of the specified window
1506 wp = find_win_by_nr_or_id(&argvars[0]);
1507 if (wp != NULL)
1508 rettv->vval.v_number = WARGCOUNT(wp);
1509 else
1510 rettv->vval.v_number = -1;
1511 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001512}
1513
1514/*
1515 * "argidx()" function
1516 */
1517 static void
1518f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1519{
1520 rettv->vval.v_number = curwin->w_arg_idx;
1521}
1522
1523/*
1524 * "arglistid()" function
1525 */
1526 static void
1527f_arglistid(typval_T *argvars, typval_T *rettv)
1528{
1529 win_T *wp;
1530
1531 rettv->vval.v_number = -1;
1532 wp = find_tabwin(&argvars[0], &argvars[1]);
1533 if (wp != NULL)
1534 rettv->vval.v_number = wp->w_alist->id;
1535}
1536
1537/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001538 * Get the argument list for a given window
1539 */
1540 static void
1541get_arglist_as_rettv(aentry_T *arglist, int argcount, typval_T *rettv)
1542{
1543 int idx;
1544
1545 if (rettv_list_alloc(rettv) == OK && arglist != NULL)
1546 for (idx = 0; idx < argcount; ++idx)
1547 list_append_string(rettv->vval.v_list,
1548 alist_name(&arglist[idx]), -1);
1549}
1550
1551/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001552 * "argv(nr)" function
1553 */
1554 static void
1555f_argv(typval_T *argvars, typval_T *rettv)
1556{
1557 int idx;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001558 aentry_T *arglist = NULL;
1559 int argcount = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001560
1561 if (argvars[0].v_type != VAR_UNKNOWN)
1562 {
Bram Moolenaare6e39892018-10-25 12:32:11 +02001563 if (argvars[1].v_type == VAR_UNKNOWN)
1564 {
1565 arglist = ARGLIST;
1566 argcount = ARGCOUNT;
1567 }
1568 else if (argvars[1].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001569 && tv_get_number(&argvars[1]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001570 {
1571 arglist = GARGLIST;
1572 argcount = GARGCOUNT;
1573 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001574 else
Bram Moolenaare6e39892018-10-25 12:32:11 +02001575 {
1576 win_T *wp = find_win_by_nr_or_id(&argvars[1]);
1577
1578 if (wp != NULL)
1579 {
1580 /* Use the argument list of the specified window */
1581 arglist = WARGLIST(wp);
1582 argcount = WARGCOUNT(wp);
1583 }
1584 }
1585
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001586 rettv->v_type = VAR_STRING;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001587 rettv->vval.v_string = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001588 idx = tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02001589 if (arglist != NULL && idx >= 0 && idx < argcount)
1590 rettv->vval.v_string = vim_strsave(alist_name(&arglist[idx]));
1591 else if (idx == -1)
1592 get_arglist_as_rettv(arglist, argcount, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001593 }
Bram Moolenaare6e39892018-10-25 12:32:11 +02001594 else
1595 get_arglist_as_rettv(ARGLIST, ARGCOUNT, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001596}
1597
1598/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001599 * "assert_beeps(cmd [, error])" function
1600 */
1601 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001602f_assert_beeps(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001603{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001604 rettv->vval.v_number = assert_beeps(argvars);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001605}
1606
1607/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001608 * "assert_equal(expected, actual[, msg])" function
1609 */
1610 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001611f_assert_equal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001612{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001613 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_EQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001614}
1615
1616/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01001617 * "assert_equalfile(fname-one, fname-two)" function
1618 */
1619 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001620f_assert_equalfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaard96ff162018-02-18 22:13:29 +01001621{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001622 rettv->vval.v_number = assert_equalfile(argvars);
Bram Moolenaard96ff162018-02-18 22:13:29 +01001623}
1624
1625/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001626 * "assert_notequal(expected, actual[, msg])" function
1627 */
1628 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001629f_assert_notequal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001630{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001631 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001632}
1633
1634/*
1635 * "assert_exception(string[, msg])" function
1636 */
1637 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001638f_assert_exception(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001639{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001640 rettv->vval.v_number = assert_exception(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001641}
1642
1643/*
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02001644 * "assert_fails(cmd [, error[, msg]])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001645 */
1646 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001647f_assert_fails(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001648{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001649 rettv->vval.v_number = assert_fails(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001650}
1651
1652/*
1653 * "assert_false(actual[, msg])" function
1654 */
1655 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001656f_assert_false(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001657{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001658 rettv->vval.v_number = assert_bool(argvars, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001659}
1660
1661/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001662 * "assert_inrange(lower, upper[, msg])" function
1663 */
1664 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001665f_assert_inrange(typval_T *argvars, typval_T *rettv)
Bram Moolenaar61c04492016-07-23 15:35:35 +02001666{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001667 rettv->vval.v_number = assert_inrange(argvars);
Bram Moolenaar61c04492016-07-23 15:35:35 +02001668}
1669
1670/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001671 * "assert_match(pattern, actual[, msg])" function
1672 */
1673 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001674f_assert_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001675{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001676 rettv->vval.v_number = assert_match_common(argvars, ASSERT_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001677}
1678
1679/*
1680 * "assert_notmatch(pattern, actual[, msg])" function
1681 */
1682 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001683f_assert_notmatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001684{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001685 rettv->vval.v_number = assert_match_common(argvars, ASSERT_NOTMATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001686}
1687
1688/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001689 * "assert_report(msg)" function
1690 */
1691 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001692f_assert_report(typval_T *argvars, typval_T *rettv)
Bram Moolenaar42205552017-03-18 19:42:22 +01001693{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001694 rettv->vval.v_number = assert_report(argvars);
Bram Moolenaar42205552017-03-18 19:42:22 +01001695}
1696
1697/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001698 * "assert_true(actual[, msg])" function
1699 */
1700 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001701f_assert_true(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001702{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001703 rettv->vval.v_number = assert_bool(argvars, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001704}
1705
1706#ifdef FEAT_FLOAT
1707/*
1708 * "asin()" function
1709 */
1710 static void
1711f_asin(typval_T *argvars, typval_T *rettv)
1712{
1713 float_T f = 0.0;
1714
1715 rettv->v_type = VAR_FLOAT;
1716 if (get_float_arg(argvars, &f) == OK)
1717 rettv->vval.v_float = asin(f);
1718 else
1719 rettv->vval.v_float = 0.0;
1720}
1721
1722/*
1723 * "atan()" function
1724 */
1725 static void
1726f_atan(typval_T *argvars, typval_T *rettv)
1727{
1728 float_T f = 0.0;
1729
1730 rettv->v_type = VAR_FLOAT;
1731 if (get_float_arg(argvars, &f) == OK)
1732 rettv->vval.v_float = atan(f);
1733 else
1734 rettv->vval.v_float = 0.0;
1735}
1736
1737/*
1738 * "atan2()" function
1739 */
1740 static void
1741f_atan2(typval_T *argvars, typval_T *rettv)
1742{
1743 float_T fx = 0.0, fy = 0.0;
1744
1745 rettv->v_type = VAR_FLOAT;
1746 if (get_float_arg(argvars, &fx) == OK
1747 && get_float_arg(&argvars[1], &fy) == OK)
1748 rettv->vval.v_float = atan2(fx, fy);
1749 else
1750 rettv->vval.v_float = 0.0;
1751}
1752#endif
1753
1754/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001755 * "balloon_show()" function
1756 */
1757#ifdef FEAT_BEVAL
1758 static void
1759f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1760{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001761 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001762 {
1763 if (argvars[0].v_type == VAR_LIST
1764# ifdef FEAT_GUI
1765 && !gui.in_use
1766# endif
1767 )
1768 post_balloon(balloonEval, NULL, argvars[0].vval.v_list);
1769 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001770 post_balloon(balloonEval, tv_get_string_chk(&argvars[0]), NULL);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001771 }
1772}
1773
Bram Moolenaar669a8282017-11-19 20:13:05 +01001774# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001775 static void
1776f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1777{
1778 if (rettv_list_alloc(rettv) == OK)
1779 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001780 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001781
1782 if (msg != NULL)
1783 {
1784 pumitem_T *array;
1785 int size = split_message(msg, &array);
1786 int i;
1787
1788 /* Skip the first and last item, they are always empty. */
1789 for (i = 1; i < size - 1; ++i)
1790 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001791 while (size > 0)
1792 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001793 vim_free(array);
1794 }
1795 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001796}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001797# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001798#endif
1799
1800/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001801 * "browse(save, title, initdir, default)" function
1802 */
1803 static void
1804f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1805{
1806#ifdef FEAT_BROWSE
1807 int save;
1808 char_u *title;
1809 char_u *initdir;
1810 char_u *defname;
1811 char_u buf[NUMBUFLEN];
1812 char_u buf2[NUMBUFLEN];
1813 int error = FALSE;
1814
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001815 save = (int)tv_get_number_chk(&argvars[0], &error);
1816 title = tv_get_string_chk(&argvars[1]);
1817 initdir = tv_get_string_buf_chk(&argvars[2], buf);
1818 defname = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001819
1820 if (error || title == NULL || initdir == NULL || defname == NULL)
1821 rettv->vval.v_string = NULL;
1822 else
1823 rettv->vval.v_string =
1824 do_browse(save ? BROWSE_SAVE : 0,
1825 title, defname, NULL, initdir, NULL, curbuf);
1826#else
1827 rettv->vval.v_string = NULL;
1828#endif
1829 rettv->v_type = VAR_STRING;
1830}
1831
1832/*
1833 * "browsedir(title, initdir)" function
1834 */
1835 static void
1836f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1837{
1838#ifdef FEAT_BROWSE
1839 char_u *title;
1840 char_u *initdir;
1841 char_u buf[NUMBUFLEN];
1842
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001843 title = tv_get_string_chk(&argvars[0]);
1844 initdir = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001845
1846 if (title == NULL || initdir == NULL)
1847 rettv->vval.v_string = NULL;
1848 else
1849 rettv->vval.v_string = do_browse(BROWSE_DIR,
1850 title, NULL, NULL, initdir, NULL, curbuf);
1851#else
1852 rettv->vval.v_string = NULL;
1853#endif
1854 rettv->v_type = VAR_STRING;
1855}
1856
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001857/*
1858 * Find a buffer by number or exact name.
1859 */
1860 static buf_T *
1861find_buffer(typval_T *avar)
1862{
1863 buf_T *buf = NULL;
1864
1865 if (avar->v_type == VAR_NUMBER)
1866 buf = buflist_findnr((int)avar->vval.v_number);
1867 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1868 {
1869 buf = buflist_findname_exp(avar->vval.v_string);
1870 if (buf == NULL)
1871 {
1872 /* No full path name match, try a match with a URL or a "nofile"
1873 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001874 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001875 if (buf->b_fname != NULL
1876 && (path_with_url(buf->b_fname)
1877#ifdef FEAT_QUICKFIX
1878 || bt_nofile(buf)
1879#endif
1880 )
1881 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1882 break;
1883 }
1884 }
1885 return buf;
1886}
1887
1888/*
1889 * "bufexists(expr)" function
1890 */
1891 static void
1892f_bufexists(typval_T *argvars, typval_T *rettv)
1893{
1894 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1895}
1896
1897/*
1898 * "buflisted(expr)" function
1899 */
1900 static void
1901f_buflisted(typval_T *argvars, typval_T *rettv)
1902{
1903 buf_T *buf;
1904
1905 buf = find_buffer(&argvars[0]);
1906 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1907}
1908
1909/*
1910 * "bufloaded(expr)" function
1911 */
1912 static void
1913f_bufloaded(typval_T *argvars, typval_T *rettv)
1914{
1915 buf_T *buf;
1916
1917 buf = find_buffer(&argvars[0]);
1918 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1919}
1920
1921 buf_T *
1922buflist_find_by_name(char_u *name, int curtab_only)
1923{
1924 int save_magic;
1925 char_u *save_cpo;
1926 buf_T *buf;
1927
1928 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1929 save_magic = p_magic;
1930 p_magic = TRUE;
1931 save_cpo = p_cpo;
1932 p_cpo = (char_u *)"";
1933
1934 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1935 TRUE, FALSE, curtab_only));
1936
1937 p_magic = save_magic;
1938 p_cpo = save_cpo;
1939 return buf;
1940}
1941
1942/*
1943 * Get buffer by number or pattern.
1944 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001945 buf_T *
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001946tv_get_buf(typval_T *tv, int curtab_only)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001947{
1948 char_u *name = tv->vval.v_string;
1949 buf_T *buf;
1950
1951 if (tv->v_type == VAR_NUMBER)
1952 return buflist_findnr((int)tv->vval.v_number);
1953 if (tv->v_type != VAR_STRING)
1954 return NULL;
1955 if (name == NULL || *name == NUL)
1956 return curbuf;
1957 if (name[0] == '$' && name[1] == NUL)
1958 return lastbuf;
1959
1960 buf = buflist_find_by_name(name, curtab_only);
1961
1962 /* If not found, try expanding the name, like done for bufexists(). */
1963 if (buf == NULL)
1964 buf = find_buffer(tv);
1965
1966 return buf;
1967}
1968
Bram Moolenaarec9d3002019-01-12 13:50:31 +01001969#ifdef FEAT_SIGNS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001970/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001971 * Get the buffer from "arg" and give an error and return NULL if it is not
1972 * valid.
1973 */
1974 static buf_T *
1975get_buf_arg(typval_T *arg)
1976{
1977 buf_T *buf;
1978
1979 ++emsg_off;
1980 buf = tv_get_buf(arg, FALSE);
1981 --emsg_off;
1982 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001983 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001984 return buf;
1985}
Bram Moolenaarec9d3002019-01-12 13:50:31 +01001986#endif
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001987
1988/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001989 * "bufname(expr)" function
1990 */
1991 static void
1992f_bufname(typval_T *argvars, typval_T *rettv)
1993{
1994 buf_T *buf;
1995
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001996 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001997 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001998 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001999 rettv->v_type = VAR_STRING;
2000 if (buf != NULL && buf->b_fname != NULL)
2001 rettv->vval.v_string = vim_strsave(buf->b_fname);
2002 else
2003 rettv->vval.v_string = NULL;
2004 --emsg_off;
2005}
2006
2007/*
2008 * "bufnr(expr)" function
2009 */
2010 static void
2011f_bufnr(typval_T *argvars, typval_T *rettv)
2012{
2013 buf_T *buf;
2014 int error = FALSE;
2015 char_u *name;
2016
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002017 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002018 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002019 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002020 --emsg_off;
2021
2022 /* If the buffer isn't found and the second argument is not zero create a
2023 * new buffer. */
2024 if (buf == NULL
2025 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002026 && tv_get_number_chk(&argvars[1], &error) != 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002027 && !error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002028 && (name = tv_get_string_chk(&argvars[0])) != NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002029 && !error)
2030 buf = buflist_new(name, NULL, (linenr_T)1, 0);
2031
2032 if (buf != NULL)
2033 rettv->vval.v_number = buf->b_fnum;
2034 else
2035 rettv->vval.v_number = -1;
2036}
2037
2038 static void
2039buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
2040{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002041 win_T *wp;
2042 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002043 buf_T *buf;
2044
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002045 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002046 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002047 buf = tv_get_buf(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02002048 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002049 {
2050 ++winnr;
2051 if (wp->w_buffer == buf)
2052 break;
2053 }
2054 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002055 --emsg_off;
2056}
2057
2058/*
2059 * "bufwinid(nr)" function
2060 */
2061 static void
2062f_bufwinid(typval_T *argvars, typval_T *rettv)
2063{
2064 buf_win_common(argvars, rettv, FALSE);
2065}
2066
2067/*
2068 * "bufwinnr(nr)" function
2069 */
2070 static void
2071f_bufwinnr(typval_T *argvars, typval_T *rettv)
2072{
2073 buf_win_common(argvars, rettv, TRUE);
2074}
2075
2076/*
2077 * "byte2line(byte)" function
2078 */
2079 static void
2080f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2081{
2082#ifndef FEAT_BYTEOFF
2083 rettv->vval.v_number = -1;
2084#else
2085 long boff = 0;
2086
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002087 boff = tv_get_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002088 if (boff < 0)
2089 rettv->vval.v_number = -1;
2090 else
2091 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2092 (linenr_T)0, &boff);
2093#endif
2094}
2095
2096 static void
2097byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2098{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002099 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002100 char_u *str;
2101 varnumber_T idx;
2102
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002103 str = tv_get_string_chk(&argvars[0]);
2104 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002105 rettv->vval.v_number = -1;
2106 if (str == NULL || idx < 0)
2107 return;
2108
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002109 t = str;
2110 for ( ; idx > 0; idx--)
2111 {
2112 if (*t == NUL) /* EOL reached */
2113 return;
2114 if (enc_utf8 && comp)
2115 t += utf_ptr2len(t);
2116 else
2117 t += (*mb_ptr2len)(t);
2118 }
2119 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002120}
2121
2122/*
2123 * "byteidx()" function
2124 */
2125 static void
2126f_byteidx(typval_T *argvars, typval_T *rettv)
2127{
2128 byteidx(argvars, rettv, FALSE);
2129}
2130
2131/*
2132 * "byteidxcomp()" function
2133 */
2134 static void
2135f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2136{
2137 byteidx(argvars, rettv, TRUE);
2138}
2139
2140/*
2141 * "call(func, arglist [, dict])" function
2142 */
2143 static void
2144f_call(typval_T *argvars, typval_T *rettv)
2145{
2146 char_u *func;
2147 partial_T *partial = NULL;
2148 dict_T *selfdict = NULL;
2149
2150 if (argvars[1].v_type != VAR_LIST)
2151 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002152 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002153 return;
2154 }
2155 if (argvars[1].vval.v_list == NULL)
2156 return;
2157
2158 if (argvars[0].v_type == VAR_FUNC)
2159 func = argvars[0].vval.v_string;
2160 else if (argvars[0].v_type == VAR_PARTIAL)
2161 {
2162 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002163 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002164 }
2165 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002166 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002167 if (*func == NUL)
2168 return; /* type error or empty name */
2169
2170 if (argvars[2].v_type != VAR_UNKNOWN)
2171 {
2172 if (argvars[2].v_type != VAR_DICT)
2173 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002174 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002175 return;
2176 }
2177 selfdict = argvars[2].vval.v_dict;
2178 }
2179
2180 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2181}
2182
2183#ifdef FEAT_FLOAT
2184/*
2185 * "ceil({float})" function
2186 */
2187 static void
2188f_ceil(typval_T *argvars, typval_T *rettv)
2189{
2190 float_T f = 0.0;
2191
2192 rettv->v_type = VAR_FLOAT;
2193 if (get_float_arg(argvars, &f) == OK)
2194 rettv->vval.v_float = ceil(f);
2195 else
2196 rettv->vval.v_float = 0.0;
2197}
2198#endif
2199
2200#ifdef FEAT_JOB_CHANNEL
2201/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002202 * "ch_canread()" function
2203 */
2204 static void
2205f_ch_canread(typval_T *argvars, typval_T *rettv)
2206{
Bram Moolenaar958dc692016-12-01 15:34:12 +01002207 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002208
2209 rettv->vval.v_number = 0;
2210 if (channel != NULL)
2211 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
2212 || channel_has_readahead(channel, PART_OUT)
2213 || channel_has_readahead(channel, PART_ERR);
2214}
2215
2216/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002217 * "ch_close()" function
2218 */
2219 static void
2220f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
2221{
2222 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2223
2224 if (channel != NULL)
2225 {
2226 channel_close(channel, FALSE);
2227 channel_clear(channel);
2228 }
2229}
2230
2231/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002232 * "ch_close()" function
2233 */
2234 static void
2235f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
2236{
2237 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2238
2239 if (channel != NULL)
2240 channel_close_in(channel);
2241}
2242
2243/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002244 * "ch_getbufnr()" function
2245 */
2246 static void
2247f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
2248{
2249 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2250
2251 rettv->vval.v_number = -1;
2252 if (channel != NULL)
2253 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002254 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002255 int part;
2256
2257 if (STRCMP(what, "err") == 0)
2258 part = PART_ERR;
2259 else if (STRCMP(what, "out") == 0)
2260 part = PART_OUT;
2261 else if (STRCMP(what, "in") == 0)
2262 part = PART_IN;
2263 else
2264 part = PART_SOCK;
2265 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
2266 rettv->vval.v_number =
2267 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
2268 }
2269}
2270
2271/*
2272 * "ch_getjob()" function
2273 */
2274 static void
2275f_ch_getjob(typval_T *argvars, typval_T *rettv)
2276{
2277 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2278
2279 if (channel != NULL)
2280 {
2281 rettv->v_type = VAR_JOB;
2282 rettv->vval.v_job = channel->ch_job;
2283 if (channel->ch_job != NULL)
2284 ++channel->ch_job->jv_refcount;
2285 }
2286}
2287
2288/*
2289 * "ch_info()" function
2290 */
2291 static void
2292f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
2293{
2294 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2295
2296 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
2297 channel_info(channel, rettv->vval.v_dict);
2298}
2299
2300/*
2301 * "ch_log()" function
2302 */
2303 static void
2304f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
2305{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002306 char_u *msg = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002307 channel_T *channel = NULL;
2308
2309 if (argvars[1].v_type != VAR_UNKNOWN)
2310 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
2311
Bram Moolenaard5359b22018-04-05 22:44:39 +02002312 ch_log(channel, "%s", msg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002313}
2314
2315/*
2316 * "ch_logfile()" function
2317 */
2318 static void
2319f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
2320{
2321 char_u *fname;
2322 char_u *opt = (char_u *)"";
2323 char_u buf[NUMBUFLEN];
2324
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02002325 /* Don't open a file in restricted mode. */
2326 if (check_restricted() || check_secure())
2327 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002328 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002329 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002330 opt = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002331 ch_logfile(fname, opt);
2332}
2333
2334/*
2335 * "ch_open()" function
2336 */
2337 static void
2338f_ch_open(typval_T *argvars, typval_T *rettv)
2339{
2340 rettv->v_type = VAR_CHANNEL;
2341 if (check_restricted() || check_secure())
2342 return;
2343 rettv->vval.v_channel = channel_open_func(argvars);
2344}
2345
2346/*
2347 * "ch_read()" function
2348 */
2349 static void
2350f_ch_read(typval_T *argvars, typval_T *rettv)
2351{
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002352 common_channel_read(argvars, rettv, FALSE, FALSE);
2353}
2354
2355/*
2356 * "ch_readblob()" function
2357 */
2358 static void
2359f_ch_readblob(typval_T *argvars, typval_T *rettv)
2360{
2361 common_channel_read(argvars, rettv, TRUE, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002362}
2363
2364/*
2365 * "ch_readraw()" function
2366 */
2367 static void
2368f_ch_readraw(typval_T *argvars, typval_T *rettv)
2369{
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002370 common_channel_read(argvars, rettv, TRUE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002371}
2372
2373/*
2374 * "ch_evalexpr()" function
2375 */
2376 static void
2377f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2378{
2379 ch_expr_common(argvars, rettv, TRUE);
2380}
2381
2382/*
2383 * "ch_sendexpr()" function
2384 */
2385 static void
2386f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2387{
2388 ch_expr_common(argvars, rettv, FALSE);
2389}
2390
2391/*
2392 * "ch_evalraw()" function
2393 */
2394 static void
2395f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2396{
2397 ch_raw_common(argvars, rettv, TRUE);
2398}
2399
2400/*
2401 * "ch_sendraw()" function
2402 */
2403 static void
2404f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2405{
2406 ch_raw_common(argvars, rettv, FALSE);
2407}
2408
2409/*
2410 * "ch_setoptions()" function
2411 */
2412 static void
2413f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2414{
2415 channel_T *channel;
2416 jobopt_T opt;
2417
2418 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2419 if (channel == NULL)
2420 return;
2421 clear_job_options(&opt);
2422 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002423 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002424 channel_set_options(channel, &opt);
2425 free_job_options(&opt);
2426}
2427
2428/*
2429 * "ch_status()" function
2430 */
2431 static void
2432f_ch_status(typval_T *argvars, typval_T *rettv)
2433{
2434 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002435 jobopt_T opt;
2436 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002437
2438 /* return an empty string by default */
2439 rettv->v_type = VAR_STRING;
2440 rettv->vval.v_string = NULL;
2441
2442 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002443
2444 if (argvars[1].v_type != VAR_UNKNOWN)
2445 {
2446 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002447 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002448 && (opt.jo_set & JO_PART))
2449 part = opt.jo_part;
2450 }
2451
2452 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002453}
2454#endif
2455
2456/*
2457 * "changenr()" function
2458 */
2459 static void
2460f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2461{
2462 rettv->vval.v_number = curbuf->b_u_seq_cur;
2463}
2464
2465/*
2466 * "char2nr(string)" function
2467 */
2468 static void
2469f_char2nr(typval_T *argvars, typval_T *rettv)
2470{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002471 if (has_mbyte)
2472 {
2473 int utf8 = 0;
2474
2475 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002476 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002477
2478 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002479 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002480 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002481 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002482 }
2483 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002484 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002485}
2486
2487/*
2488 * "cindent(lnum)" function
2489 */
2490 static void
2491f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2492{
2493#ifdef FEAT_CINDENT
2494 pos_T pos;
2495 linenr_T lnum;
2496
2497 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002498 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002499 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2500 {
2501 curwin->w_cursor.lnum = lnum;
2502 rettv->vval.v_number = get_c_indent();
2503 curwin->w_cursor = pos;
2504 }
2505 else
2506#endif
2507 rettv->vval.v_number = -1;
2508}
2509
Bram Moolenaaraff74912019-03-30 18:11:49 +01002510 static win_T *
2511get_optional_window(typval_T *argvars, int idx)
2512{
2513 win_T *win = curwin;
2514
2515 if (argvars[idx].v_type != VAR_UNKNOWN)
2516 {
2517 win = find_win_by_nr_or_id(&argvars[idx]);
2518 if (win == NULL)
2519 {
2520 emsg(_(e_invalwindow));
2521 return NULL;
2522 }
2523 }
2524 return win;
2525}
2526
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002527/*
2528 * "clearmatches()" function
2529 */
2530 static void
2531f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2532{
2533#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01002534 win_T *win = get_optional_window(argvars, 0);
2535
2536 if (win != NULL)
2537 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002538#endif
2539}
2540
2541/*
2542 * "col(string)" function
2543 */
2544 static void
2545f_col(typval_T *argvars, typval_T *rettv)
2546{
2547 colnr_T col = 0;
2548 pos_T *fp;
2549 int fnum = curbuf->b_fnum;
2550
2551 fp = var2fpos(&argvars[0], FALSE, &fnum);
2552 if (fp != NULL && fnum == curbuf->b_fnum)
2553 {
2554 if (fp->col == MAXCOL)
2555 {
2556 /* '> can be MAXCOL, get the length of the line then */
2557 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2558 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2559 else
2560 col = MAXCOL;
2561 }
2562 else
2563 {
2564 col = fp->col + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002565 /* col(".") when the cursor is on the NUL at the end of the line
2566 * because of "coladd" can be seen as an extra column. */
2567 if (virtual_active() && fp == &curwin->w_cursor)
2568 {
2569 char_u *p = ml_get_cursor();
2570
2571 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2572 curwin->w_virtcol - curwin->w_cursor.coladd))
2573 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002574 int l;
2575
2576 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2577 col += l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002578 }
2579 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002580 }
2581 }
2582 rettv->vval.v_number = col;
2583}
2584
2585#if defined(FEAT_INS_EXPAND)
2586/*
2587 * "complete()" function
2588 */
2589 static void
2590f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2591{
2592 int startcol;
2593
2594 if ((State & INSERT) == 0)
2595 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002596 emsg(_("E785: complete() can only be used in Insert mode"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002597 return;
2598 }
2599
2600 /* Check for undo allowed here, because if something was already inserted
2601 * the line was already saved for undo and this check isn't done. */
2602 if (!undo_allowed())
2603 return;
2604
2605 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2606 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002607 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002608 return;
2609 }
2610
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002611 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002612 if (startcol <= 0)
2613 return;
2614
2615 set_completion(startcol - 1, argvars[1].vval.v_list);
2616}
2617
2618/*
2619 * "complete_add()" function
2620 */
2621 static void
2622f_complete_add(typval_T *argvars, typval_T *rettv)
2623{
2624 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2625}
2626
2627/*
2628 * "complete_check()" function
2629 */
2630 static void
2631f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2632{
2633 int saved = RedrawingDisabled;
2634
2635 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002636 ins_compl_check_keys(0, TRUE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002637 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002638 RedrawingDisabled = saved;
2639}
Bram Moolenaarfd133322019-03-29 12:20:27 +01002640
2641/*
2642 * "complete_info()" function
2643 */
2644 static void
2645f_complete_info(typval_T *argvars, typval_T *rettv)
2646{
2647 list_T *what_list = NULL;
2648
2649 if (rettv_dict_alloc(rettv) != OK)
2650 return;
2651
2652 if (argvars[0].v_type != VAR_UNKNOWN)
2653 {
2654 if (argvars[0].v_type != VAR_LIST)
2655 {
2656 emsg(_(e_listreq));
2657 return;
2658 }
2659 what_list = argvars[0].vval.v_list;
2660 }
2661 get_complete_info(what_list, rettv->vval.v_dict);
2662}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002663#endif
2664
2665/*
2666 * "confirm(message, buttons[, default [, type]])" function
2667 */
2668 static void
2669f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2670{
2671#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2672 char_u *message;
2673 char_u *buttons = NULL;
2674 char_u buf[NUMBUFLEN];
2675 char_u buf2[NUMBUFLEN];
2676 int def = 1;
2677 int type = VIM_GENERIC;
2678 char_u *typestr;
2679 int error = FALSE;
2680
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002681 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002682 if (message == NULL)
2683 error = TRUE;
2684 if (argvars[1].v_type != VAR_UNKNOWN)
2685 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002686 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002687 if (buttons == NULL)
2688 error = TRUE;
2689 if (argvars[2].v_type != VAR_UNKNOWN)
2690 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002691 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002692 if (argvars[3].v_type != VAR_UNKNOWN)
2693 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002694 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002695 if (typestr == NULL)
2696 error = TRUE;
2697 else
2698 {
2699 switch (TOUPPER_ASC(*typestr))
2700 {
2701 case 'E': type = VIM_ERROR; break;
2702 case 'Q': type = VIM_QUESTION; break;
2703 case 'I': type = VIM_INFO; break;
2704 case 'W': type = VIM_WARNING; break;
2705 case 'G': type = VIM_GENERIC; break;
2706 }
2707 }
2708 }
2709 }
2710 }
2711
2712 if (buttons == NULL || *buttons == NUL)
2713 buttons = (char_u *)_("&Ok");
2714
2715 if (!error)
2716 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2717 def, NULL, FALSE);
2718#endif
2719}
2720
2721/*
2722 * "copy()" function
2723 */
2724 static void
2725f_copy(typval_T *argvars, typval_T *rettv)
2726{
2727 item_copy(&argvars[0], rettv, FALSE, 0);
2728}
2729
2730#ifdef FEAT_FLOAT
2731/*
2732 * "cos()" function
2733 */
2734 static void
2735f_cos(typval_T *argvars, typval_T *rettv)
2736{
2737 float_T f = 0.0;
2738
2739 rettv->v_type = VAR_FLOAT;
2740 if (get_float_arg(argvars, &f) == OK)
2741 rettv->vval.v_float = cos(f);
2742 else
2743 rettv->vval.v_float = 0.0;
2744}
2745
2746/*
2747 * "cosh()" function
2748 */
2749 static void
2750f_cosh(typval_T *argvars, typval_T *rettv)
2751{
2752 float_T f = 0.0;
2753
2754 rettv->v_type = VAR_FLOAT;
2755 if (get_float_arg(argvars, &f) == OK)
2756 rettv->vval.v_float = cosh(f);
2757 else
2758 rettv->vval.v_float = 0.0;
2759}
2760#endif
2761
2762/*
2763 * "count()" function
2764 */
2765 static void
2766f_count(typval_T *argvars, typval_T *rettv)
2767{
2768 long n = 0;
2769 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002770 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002771
Bram Moolenaar9966b212017-07-28 16:46:57 +02002772 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002773 ic = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002774
2775 if (argvars[0].v_type == VAR_STRING)
2776 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002777 char_u *expr = tv_get_string_chk(&argvars[1]);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002778 char_u *p = argvars[0].vval.v_string;
2779 char_u *next;
2780
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002781 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002782 {
2783 if (ic)
2784 {
2785 size_t len = STRLEN(expr);
2786
2787 while (*p != NUL)
2788 {
2789 if (MB_STRNICMP(p, expr, len) == 0)
2790 {
2791 ++n;
2792 p += len;
2793 }
2794 else
2795 MB_PTR_ADV(p);
2796 }
2797 }
2798 else
2799 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2800 != NULL)
2801 {
2802 ++n;
2803 p = next + STRLEN(expr);
2804 }
2805 }
2806
2807 }
2808 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002809 {
2810 listitem_T *li;
2811 list_T *l;
2812 long idx;
2813
2814 if ((l = argvars[0].vval.v_list) != NULL)
2815 {
2816 li = l->lv_first;
2817 if (argvars[2].v_type != VAR_UNKNOWN)
2818 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002819 if (argvars[3].v_type != VAR_UNKNOWN)
2820 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002821 idx = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002822 if (!error)
2823 {
2824 li = list_find(l, idx);
2825 if (li == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002826 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002827 }
2828 }
2829 if (error)
2830 li = NULL;
2831 }
2832
2833 for ( ; li != NULL; li = li->li_next)
2834 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2835 ++n;
2836 }
2837 }
2838 else if (argvars[0].v_type == VAR_DICT)
2839 {
2840 int todo;
2841 dict_T *d;
2842 hashitem_T *hi;
2843
2844 if ((d = argvars[0].vval.v_dict) != NULL)
2845 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002846 if (argvars[2].v_type != VAR_UNKNOWN)
2847 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002848 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002849 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002850 }
2851
2852 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2853 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2854 {
2855 if (!HASHITEM_EMPTY(hi))
2856 {
2857 --todo;
2858 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2859 ++n;
2860 }
2861 }
2862 }
2863 }
2864 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002865 semsg(_(e_listdictarg), "count()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002866 rettv->vval.v_number = n;
2867}
2868
2869/*
2870 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2871 *
2872 * Checks the existence of a cscope connection.
2873 */
2874 static void
2875f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2876{
2877#ifdef FEAT_CSCOPE
2878 int num = 0;
2879 char_u *dbpath = NULL;
2880 char_u *prepend = NULL;
2881 char_u buf[NUMBUFLEN];
2882
2883 if (argvars[0].v_type != VAR_UNKNOWN
2884 && argvars[1].v_type != VAR_UNKNOWN)
2885 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002886 num = (int)tv_get_number(&argvars[0]);
2887 dbpath = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002888 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002889 prepend = tv_get_string_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002890 }
2891
2892 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2893#endif
2894}
2895
2896/*
2897 * "cursor(lnum, col)" function, or
2898 * "cursor(list)"
2899 *
2900 * Moves the cursor to the specified line and column.
2901 * Returns 0 when the position could be set, -1 otherwise.
2902 */
2903 static void
2904f_cursor(typval_T *argvars, typval_T *rettv)
2905{
2906 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002907 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002908 int set_curswant = TRUE;
2909
2910 rettv->vval.v_number = -1;
2911 if (argvars[1].v_type == VAR_UNKNOWN)
2912 {
2913 pos_T pos;
2914 colnr_T curswant = -1;
2915
2916 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2917 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002918 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002919 return;
2920 }
2921 line = pos.lnum;
2922 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002923 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002924 if (curswant >= 0)
2925 {
2926 curwin->w_curswant = curswant - 1;
2927 set_curswant = FALSE;
2928 }
2929 }
2930 else
2931 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002932 line = tv_get_lnum(argvars);
2933 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002934 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002935 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002936 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002937 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002938 return; /* type error; errmsg already given */
2939 if (line > 0)
2940 curwin->w_cursor.lnum = line;
2941 if (col > 0)
2942 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002943 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002944
2945 /* Make sure the cursor is in a valid position. */
2946 check_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002947 /* Correct cursor for multi-byte character. */
2948 if (has_mbyte)
2949 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002950
2951 curwin->w_set_curswant = set_curswant;
2952 rettv->vval.v_number = 0;
2953}
2954
Bram Moolenaar4f974752019-02-17 17:44:42 +01002955#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002956/*
2957 * "debugbreak()" function
2958 */
2959 static void
2960f_debugbreak(typval_T *argvars, typval_T *rettv)
2961{
2962 int pid;
2963
2964 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002965 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002966 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002967 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002968 else
2969 {
2970 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2971
2972 if (hProcess != NULL)
2973 {
2974 DebugBreakProcess(hProcess);
2975 CloseHandle(hProcess);
2976 rettv->vval.v_number = OK;
2977 }
2978 }
2979}
2980#endif
2981
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002982/*
2983 * "deepcopy()" function
2984 */
2985 static void
2986f_deepcopy(typval_T *argvars, typval_T *rettv)
2987{
2988 int noref = 0;
2989 int copyID;
2990
2991 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002992 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002993 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002994 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002995 else
2996 {
2997 copyID = get_copyID();
2998 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2999 }
3000}
3001
3002/*
3003 * "delete()" function
3004 */
3005 static void
3006f_delete(typval_T *argvars, typval_T *rettv)
3007{
3008 char_u nbuf[NUMBUFLEN];
3009 char_u *name;
3010 char_u *flags;
3011
3012 rettv->vval.v_number = -1;
3013 if (check_restricted() || check_secure())
3014 return;
3015
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003016 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003017 if (name == NULL || *name == NUL)
3018 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003019 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003020 return;
3021 }
3022
3023 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003024 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003025 else
3026 flags = (char_u *)"";
3027
3028 if (*flags == NUL)
3029 /* delete a file */
3030 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
3031 else if (STRCMP(flags, "d") == 0)
3032 /* delete an empty directory */
3033 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
3034 else if (STRCMP(flags, "rf") == 0)
3035 /* delete a directory recursively */
3036 rettv->vval.v_number = delete_recursive(name);
3037 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003038 semsg(_(e_invexpr2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003039}
3040
3041/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02003042 * "deletebufline()" function
3043 */
3044 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02003045f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02003046{
3047 buf_T *buf;
3048 linenr_T first, last;
3049 linenr_T lnum;
3050 long count;
3051 int is_curbuf;
3052 buf_T *curbuf_save = NULL;
3053 win_T *curwin_save = NULL;
3054 tabpage_T *tp;
3055 win_T *wp;
3056
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01003057 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003058 if (buf == NULL)
3059 {
3060 rettv->vval.v_number = 1; /* FAIL */
3061 return;
3062 }
3063 is_curbuf = buf == curbuf;
3064
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003065 first = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003066 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003067 last = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003068 else
3069 last = first;
3070
3071 if (buf->b_ml.ml_mfp == NULL || first < 1
3072 || first > buf->b_ml.ml_line_count || last < first)
3073 {
3074 rettv->vval.v_number = 1; /* FAIL */
3075 return;
3076 }
3077
3078 if (!is_curbuf)
3079 {
3080 curbuf_save = curbuf;
3081 curwin_save = curwin;
3082 curbuf = buf;
3083 find_win_for_curbuf();
3084 }
3085 if (last > curbuf->b_ml.ml_line_count)
3086 last = curbuf->b_ml.ml_line_count;
3087 count = last - first + 1;
3088
3089 // When coming here from Insert mode, sync undo, so that this can be
3090 // undone separately from what was previously inserted.
3091 if (u_sync_once == 2)
3092 {
3093 u_sync_once = 1; // notify that u_sync() was called
3094 u_sync(TRUE);
3095 }
3096
3097 if (u_save(first - 1, last + 1) == FAIL)
3098 {
3099 rettv->vval.v_number = 1; /* FAIL */
3100 return;
3101 }
3102
3103 for (lnum = first; lnum <= last; ++lnum)
3104 ml_delete(first, TRUE);
3105
3106 FOR_ALL_TAB_WINDOWS(tp, wp)
3107 if (wp->w_buffer == buf)
3108 {
3109 if (wp->w_cursor.lnum > last)
3110 wp->w_cursor.lnum -= count;
3111 else if (wp->w_cursor.lnum> first)
3112 wp->w_cursor.lnum = first;
3113 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
3114 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
3115 }
3116 check_cursor_col();
3117 deleted_lines_mark(first, count);
3118
3119 if (!is_curbuf)
3120 {
3121 curbuf = curbuf_save;
3122 curwin = curwin_save;
3123 }
3124}
3125
3126/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003127 * "did_filetype()" function
3128 */
3129 static void
3130f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3131{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003132 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003133}
3134
3135/*
3136 * "diff_filler()" function
3137 */
3138 static void
3139f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3140{
3141#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003142 rettv->vval.v_number = diff_check_fill(curwin, tv_get_lnum(argvars));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003143#endif
3144}
3145
3146/*
3147 * "diff_hlID()" function
3148 */
3149 static void
3150f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3151{
3152#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003153 linenr_T lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003154 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01003155 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003156 static int fnum = 0;
3157 static int change_start = 0;
3158 static int change_end = 0;
3159 static hlf_T hlID = (hlf_T)0;
3160 int filler_lines;
3161 int col;
3162
3163 if (lnum < 0) /* ignore type error in {lnum} arg */
3164 lnum = 0;
3165 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003166 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003167 || fnum != curbuf->b_fnum)
3168 {
3169 /* New line, buffer, change: need to get the values. */
3170 filler_lines = diff_check(curwin, lnum);
3171 if (filler_lines < 0)
3172 {
3173 if (filler_lines == -1)
3174 {
3175 change_start = MAXCOL;
3176 change_end = -1;
3177 if (diff_find_change(curwin, lnum, &change_start, &change_end))
3178 hlID = HLF_ADD; /* added line */
3179 else
3180 hlID = HLF_CHD; /* changed line */
3181 }
3182 else
3183 hlID = HLF_ADD; /* added line */
3184 }
3185 else
3186 hlID = (hlf_T)0;
3187 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003188 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003189 fnum = curbuf->b_fnum;
3190 }
3191
3192 if (hlID == HLF_CHD || hlID == HLF_TXD)
3193 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003194 col = tv_get_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003195 if (col >= change_start && col <= change_end)
3196 hlID = HLF_TXD; /* changed text */
3197 else
3198 hlID = HLF_CHD; /* changed line */
3199 }
3200 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
3201#endif
3202}
3203
3204/*
3205 * "empty({expr})" function
3206 */
3207 static void
3208f_empty(typval_T *argvars, typval_T *rettv)
3209{
3210 int n = FALSE;
3211
3212 switch (argvars[0].v_type)
3213 {
3214 case VAR_STRING:
3215 case VAR_FUNC:
3216 n = argvars[0].vval.v_string == NULL
3217 || *argvars[0].vval.v_string == NUL;
3218 break;
3219 case VAR_PARTIAL:
3220 n = FALSE;
3221 break;
3222 case VAR_NUMBER:
3223 n = argvars[0].vval.v_number == 0;
3224 break;
3225 case VAR_FLOAT:
3226#ifdef FEAT_FLOAT
3227 n = argvars[0].vval.v_float == 0.0;
3228 break;
3229#endif
3230 case VAR_LIST:
3231 n = argvars[0].vval.v_list == NULL
3232 || argvars[0].vval.v_list->lv_first == NULL;
3233 break;
3234 case VAR_DICT:
3235 n = argvars[0].vval.v_dict == NULL
3236 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
3237 break;
3238 case VAR_SPECIAL:
3239 n = argvars[0].vval.v_number != VVAL_TRUE;
3240 break;
3241
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003242 case VAR_BLOB:
3243 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003244 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
3245 break;
3246
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003247 case VAR_JOB:
3248#ifdef FEAT_JOB_CHANNEL
3249 n = argvars[0].vval.v_job == NULL
3250 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
3251 break;
3252#endif
3253 case VAR_CHANNEL:
3254#ifdef FEAT_JOB_CHANNEL
3255 n = argvars[0].vval.v_channel == NULL
3256 || !channel_is_open(argvars[0].vval.v_channel);
3257 break;
3258#endif
3259 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01003260 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003261 n = TRUE;
3262 break;
3263 }
3264
3265 rettv->vval.v_number = n;
3266}
3267
3268/*
3269 * "escape({string}, {chars})" function
3270 */
3271 static void
3272f_escape(typval_T *argvars, typval_T *rettv)
3273{
3274 char_u buf[NUMBUFLEN];
3275
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003276 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
3277 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003278 rettv->v_type = VAR_STRING;
3279}
3280
3281/*
3282 * "eval()" function
3283 */
3284 static void
3285f_eval(typval_T *argvars, typval_T *rettv)
3286{
3287 char_u *s, *p;
3288
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003289 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003290 if (s != NULL)
3291 s = skipwhite(s);
3292
3293 p = s;
3294 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3295 {
3296 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003297 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003298 need_clr_eos = FALSE;
3299 rettv->v_type = VAR_NUMBER;
3300 rettv->vval.v_number = 0;
3301 }
3302 else if (*s != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003303 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003304}
3305
3306/*
3307 * "eventhandler()" function
3308 */
3309 static void
3310f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3311{
3312 rettv->vval.v_number = vgetc_busy;
3313}
3314
3315/*
3316 * "executable()" function
3317 */
3318 static void
3319f_executable(typval_T *argvars, typval_T *rettv)
3320{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003321 char_u *name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003322
3323 /* Check in $PATH and also check directly if there is a directory name. */
3324 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
3325 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
3326}
3327
3328static garray_T redir_execute_ga;
3329
3330/*
3331 * Append "value[value_len]" to the execute() output.
3332 */
3333 void
3334execute_redir_str(char_u *value, int value_len)
3335{
3336 int len;
3337
3338 if (value_len == -1)
3339 len = (int)STRLEN(value); /* Append the entire string */
3340 else
3341 len = value_len; /* Append only "value_len" characters */
3342 if (ga_grow(&redir_execute_ga, len) == OK)
3343 {
3344 mch_memmove((char *)redir_execute_ga.ga_data
3345 + redir_execute_ga.ga_len, value, len);
3346 redir_execute_ga.ga_len += len;
3347 }
3348}
3349
3350/*
3351 * Get next line from a list.
3352 * Called by do_cmdline() to get the next line.
3353 * Returns allocated string, or NULL for end of function.
3354 */
3355
3356 static char_u *
3357get_list_line(
3358 int c UNUSED,
3359 void *cookie,
3360 int indent UNUSED)
3361{
3362 listitem_T **p = (listitem_T **)cookie;
3363 listitem_T *item = *p;
3364 char_u buf[NUMBUFLEN];
3365 char_u *s;
3366
3367 if (item == NULL)
3368 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003369 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003370 *p = item->li_next;
3371 return s == NULL ? NULL : vim_strsave(s);
3372}
3373
3374/*
3375 * "execute()" function
3376 */
3377 static void
3378f_execute(typval_T *argvars, typval_T *rettv)
3379{
3380 char_u *cmd = NULL;
3381 list_T *list = NULL;
3382 int save_msg_silent = msg_silent;
3383 int save_emsg_silent = emsg_silent;
3384 int save_emsg_noredir = emsg_noredir;
3385 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003386 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003387 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003388 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003389 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003390
3391 rettv->vval.v_string = NULL;
3392 rettv->v_type = VAR_STRING;
3393
3394 if (argvars[0].v_type == VAR_LIST)
3395 {
3396 list = argvars[0].vval.v_list;
3397 if (list == NULL || list->lv_first == NULL)
3398 /* empty list, no commands, empty output */
3399 return;
3400 ++list->lv_refcount;
3401 }
3402 else
3403 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003404 cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003405 if (cmd == NULL)
3406 return;
3407 }
3408
3409 if (argvars[1].v_type != VAR_UNKNOWN)
3410 {
3411 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003412 char_u *s = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003413
3414 if (s == NULL)
3415 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003416 if (*s == NUL)
3417 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003418 if (STRNCMP(s, "silent", 6) == 0)
3419 ++msg_silent;
3420 if (STRCMP(s, "silent!") == 0)
3421 {
3422 emsg_silent = TRUE;
3423 emsg_noredir = TRUE;
3424 }
3425 }
3426 else
3427 ++msg_silent;
3428
3429 if (redir_execute)
3430 save_ga = redir_execute_ga;
3431 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3432 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003433 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003434 if (!echo_output)
3435 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003436
3437 if (cmd != NULL)
3438 do_cmdline_cmd(cmd);
3439 else
3440 {
3441 listitem_T *item = list->lv_first;
3442
3443 do_cmdline(NULL, get_list_line, (void *)&item,
3444 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3445 --list->lv_refcount;
3446 }
3447
Bram Moolenaard297f352017-01-29 20:31:21 +01003448 /* Need to append a NUL to the result. */
3449 if (ga_grow(&redir_execute_ga, 1) == OK)
3450 {
3451 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3452 rettv->vval.v_string = redir_execute_ga.ga_data;
3453 }
3454 else
3455 {
3456 ga_clear(&redir_execute_ga);
3457 rettv->vval.v_string = NULL;
3458 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003459 msg_silent = save_msg_silent;
3460 emsg_silent = save_emsg_silent;
3461 emsg_noredir = save_emsg_noredir;
3462
3463 redir_execute = save_redir_execute;
3464 if (redir_execute)
3465 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003466 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003467
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003468 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003469 if (echo_output)
3470 // When not working silently: put it in column zero. A following
3471 // "echon" will overwrite the message, unavoidably.
3472 msg_col = 0;
3473 else
3474 // When working silently: Put it back where it was, since nothing
3475 // should have been written.
3476 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003477}
3478
3479/*
3480 * "exepath()" function
3481 */
3482 static void
3483f_exepath(typval_T *argvars, typval_T *rettv)
3484{
3485 char_u *p = NULL;
3486
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003487 (void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003488 rettv->v_type = VAR_STRING;
3489 rettv->vval.v_string = p;
3490}
3491
3492/*
3493 * "exists()" function
3494 */
3495 static void
3496f_exists(typval_T *argvars, typval_T *rettv)
3497{
3498 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003499 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003500
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003501 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003502 if (*p == '$') /* environment variable */
3503 {
3504 /* first try "normal" environment variables (fast) */
3505 if (mch_getenv(p + 1) != NULL)
3506 n = TRUE;
3507 else
3508 {
3509 /* try expanding things like $VIM and ${HOME} */
3510 p = expand_env_save(p);
3511 if (p != NULL && *p != '$')
3512 n = TRUE;
3513 vim_free(p);
3514 }
3515 }
3516 else if (*p == '&' || *p == '+') /* option */
3517 {
3518 n = (get_option_tv(&p, NULL, TRUE) == OK);
3519 if (*skipwhite(p) != NUL)
3520 n = FALSE; /* trailing garbage */
3521 }
3522 else if (*p == '*') /* internal or user defined function */
3523 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003524 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003525 }
3526 else if (*p == ':')
3527 {
3528 n = cmd_exists(p + 1);
3529 }
3530 else if (*p == '#')
3531 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003532 if (p[1] == '#')
3533 n = autocmd_supported(p + 2);
3534 else
3535 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003536 }
3537 else /* internal variable */
3538 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003539 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003540 }
3541
3542 rettv->vval.v_number = n;
3543}
3544
3545#ifdef FEAT_FLOAT
3546/*
3547 * "exp()" function
3548 */
3549 static void
3550f_exp(typval_T *argvars, typval_T *rettv)
3551{
3552 float_T f = 0.0;
3553
3554 rettv->v_type = VAR_FLOAT;
3555 if (get_float_arg(argvars, &f) == OK)
3556 rettv->vval.v_float = exp(f);
3557 else
3558 rettv->vval.v_float = 0.0;
3559}
3560#endif
3561
3562/*
3563 * "expand()" function
3564 */
3565 static void
3566f_expand(typval_T *argvars, typval_T *rettv)
3567{
3568 char_u *s;
3569 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003570 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003571 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3572 expand_T xpc;
3573 int error = FALSE;
3574 char_u *result;
3575
3576 rettv->v_type = VAR_STRING;
3577 if (argvars[1].v_type != VAR_UNKNOWN
3578 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003579 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003580 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003581 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003582
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003583 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003584 if (*s == '%' || *s == '#' || *s == '<')
3585 {
3586 ++emsg_off;
3587 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3588 --emsg_off;
3589 if (rettv->v_type == VAR_LIST)
3590 {
3591 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3592 list_append_string(rettv->vval.v_list, result, -1);
3593 else
3594 vim_free(result);
3595 }
3596 else
3597 rettv->vval.v_string = result;
3598 }
3599 else
3600 {
3601 /* When the optional second argument is non-zero, don't remove matches
3602 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3603 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003604 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003605 options |= WILD_KEEP_ALL;
3606 if (!error)
3607 {
3608 ExpandInit(&xpc);
3609 xpc.xp_context = EXPAND_FILES;
3610 if (p_wic)
3611 options += WILD_ICASE;
3612 if (rettv->v_type == VAR_STRING)
3613 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3614 options, WILD_ALL);
3615 else if (rettv_list_alloc(rettv) != FAIL)
3616 {
3617 int i;
3618
3619 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3620 for (i = 0; i < xpc.xp_numfiles; i++)
3621 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3622 ExpandCleanup(&xpc);
3623 }
3624 }
3625 else
3626 rettv->vval.v_string = NULL;
3627 }
3628}
3629
3630/*
3631 * "extend(list, list [, idx])" function
3632 * "extend(dict, dict [, action])" function
3633 */
3634 static void
3635f_extend(typval_T *argvars, typval_T *rettv)
3636{
3637 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3638
3639 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3640 {
3641 list_T *l1, *l2;
3642 listitem_T *item;
3643 long before;
3644 int error = FALSE;
3645
3646 l1 = argvars[0].vval.v_list;
3647 l2 = argvars[1].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003648 if (l1 != NULL && !var_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003649 && l2 != NULL)
3650 {
3651 if (argvars[2].v_type != VAR_UNKNOWN)
3652 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003653 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003654 if (error)
3655 return; /* type error; errmsg already given */
3656
3657 if (before == l1->lv_len)
3658 item = NULL;
3659 else
3660 {
3661 item = list_find(l1, before);
3662 if (item == NULL)
3663 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003664 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003665 return;
3666 }
3667 }
3668 }
3669 else
3670 item = NULL;
3671 list_extend(l1, l2, item);
3672
3673 copy_tv(&argvars[0], rettv);
3674 }
3675 }
3676 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3677 {
3678 dict_T *d1, *d2;
3679 char_u *action;
3680 int i;
3681
3682 d1 = argvars[0].vval.v_dict;
3683 d2 = argvars[1].vval.v_dict;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003684 if (d1 != NULL && !var_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003685 && d2 != NULL)
3686 {
3687 /* Check the third argument. */
3688 if (argvars[2].v_type != VAR_UNKNOWN)
3689 {
3690 static char *(av[]) = {"keep", "force", "error"};
3691
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003692 action = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003693 if (action == NULL)
3694 return; /* type error; errmsg already given */
3695 for (i = 0; i < 3; ++i)
3696 if (STRCMP(action, av[i]) == 0)
3697 break;
3698 if (i == 3)
3699 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003700 semsg(_(e_invarg2), action);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003701 return;
3702 }
3703 }
3704 else
3705 action = (char_u *)"force";
3706
3707 dict_extend(d1, d2, action);
3708
3709 copy_tv(&argvars[0], rettv);
3710 }
3711 }
3712 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003713 semsg(_(e_listdictarg), "extend()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003714}
3715
3716/*
3717 * "feedkeys()" function
3718 */
3719 static void
3720f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3721{
3722 int remap = TRUE;
3723 int insert = FALSE;
3724 char_u *keys, *flags;
3725 char_u nbuf[NUMBUFLEN];
3726 int typed = FALSE;
3727 int execute = FALSE;
3728 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003729 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003730 char_u *keys_esc;
3731
3732 /* This is not allowed in the sandbox. If the commands would still be
3733 * executed in the sandbox it would be OK, but it probably happens later,
3734 * when "sandbox" is no longer set. */
3735 if (check_secure())
3736 return;
3737
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003738 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003739
3740 if (argvars[1].v_type != VAR_UNKNOWN)
3741 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003742 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003743 for ( ; *flags != NUL; ++flags)
3744 {
3745 switch (*flags)
3746 {
3747 case 'n': remap = FALSE; break;
3748 case 'm': remap = TRUE; break;
3749 case 't': typed = TRUE; break;
3750 case 'i': insert = TRUE; break;
3751 case 'x': execute = TRUE; break;
3752 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003753 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003754 }
3755 }
3756 }
3757
3758 if (*keys != NUL || execute)
3759 {
3760 /* Need to escape K_SPECIAL and CSI before putting the string in the
3761 * typeahead buffer. */
3762 keys_esc = vim_strsave_escape_csi(keys);
3763 if (keys_esc != NULL)
3764 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003765 if (lowlevel)
3766 {
3767#ifdef USE_INPUT_BUF
3768 add_to_input_buf(keys, (int)STRLEN(keys));
3769#else
3770 emsg(_("E980: lowlevel input not supported"));
3771#endif
3772 }
3773 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003774 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003775 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003776 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003777 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003778#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003779 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003780#endif
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003781 )
3782 typebuf_was_filled = TRUE;
3783 }
3784 vim_free(keys_esc);
3785
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003786 if (execute)
3787 {
3788 int save_msg_scroll = msg_scroll;
3789
3790 /* Avoid a 1 second delay when the keys start Insert mode. */
3791 msg_scroll = FALSE;
3792
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003793 if (!dangerous)
3794 ++ex_normal_busy;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02003795 exec_normal(TRUE, FALSE, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003796 if (!dangerous)
3797 --ex_normal_busy;
3798
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003799 msg_scroll |= save_msg_scroll;
3800 }
3801 }
3802 }
3803}
3804
3805/*
3806 * "filereadable()" function
3807 */
3808 static void
3809f_filereadable(typval_T *argvars, typval_T *rettv)
3810{
3811 int fd;
3812 char_u *p;
3813 int n;
3814
3815#ifndef O_NONBLOCK
3816# define O_NONBLOCK 0
3817#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003818 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003819 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3820 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3821 {
3822 n = TRUE;
3823 close(fd);
3824 }
3825 else
3826 n = FALSE;
3827
3828 rettv->vval.v_number = n;
3829}
3830
3831/*
3832 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3833 * rights to write into.
3834 */
3835 static void
3836f_filewritable(typval_T *argvars, typval_T *rettv)
3837{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003838 rettv->vval.v_number = filewritable(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003839}
3840
3841 static void
3842findfilendir(
3843 typval_T *argvars UNUSED,
3844 typval_T *rettv,
3845 int find_what UNUSED)
3846{
3847#ifdef FEAT_SEARCHPATH
3848 char_u *fname;
3849 char_u *fresult = NULL;
3850 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3851 char_u *p;
3852 char_u pathbuf[NUMBUFLEN];
3853 int count = 1;
3854 int first = TRUE;
3855 int error = FALSE;
3856#endif
3857
3858 rettv->vval.v_string = NULL;
3859 rettv->v_type = VAR_STRING;
3860
3861#ifdef FEAT_SEARCHPATH
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003862 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003863
3864 if (argvars[1].v_type != VAR_UNKNOWN)
3865 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003866 p = tv_get_string_buf_chk(&argvars[1], pathbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003867 if (p == NULL)
3868 error = TRUE;
3869 else
3870 {
3871 if (*p != NUL)
3872 path = p;
3873
3874 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003875 count = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003876 }
3877 }
3878
3879 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3880 error = TRUE;
3881
3882 if (*fname != NUL && !error)
3883 {
3884 do
3885 {
3886 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3887 vim_free(fresult);
3888 fresult = find_file_in_path_option(first ? fname : NULL,
3889 first ? (int)STRLEN(fname) : 0,
3890 0, first, path,
3891 find_what,
3892 curbuf->b_ffname,
3893 find_what == FINDFILE_DIR
3894 ? (char_u *)"" : curbuf->b_p_sua);
3895 first = FALSE;
3896
3897 if (fresult != NULL && rettv->v_type == VAR_LIST)
3898 list_append_string(rettv->vval.v_list, fresult, -1);
3899
3900 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3901 }
3902
3903 if (rettv->v_type == VAR_STRING)
3904 rettv->vval.v_string = fresult;
3905#endif
3906}
3907
3908/*
3909 * "filter()" function
3910 */
3911 static void
3912f_filter(typval_T *argvars, typval_T *rettv)
3913{
3914 filter_map(argvars, rettv, FALSE);
3915}
3916
3917/*
3918 * "finddir({fname}[, {path}[, {count}]])" function
3919 */
3920 static void
3921f_finddir(typval_T *argvars, typval_T *rettv)
3922{
3923 findfilendir(argvars, rettv, FINDFILE_DIR);
3924}
3925
3926/*
3927 * "findfile({fname}[, {path}[, {count}]])" function
3928 */
3929 static void
3930f_findfile(typval_T *argvars, typval_T *rettv)
3931{
3932 findfilendir(argvars, rettv, FINDFILE_FILE);
3933}
3934
3935#ifdef FEAT_FLOAT
3936/*
3937 * "float2nr({float})" function
3938 */
3939 static void
3940f_float2nr(typval_T *argvars, typval_T *rettv)
3941{
3942 float_T f = 0.0;
3943
3944 if (get_float_arg(argvars, &f) == OK)
3945 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003946 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003947 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003948 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003949 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003950 else
3951 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003952 }
3953}
3954
3955/*
3956 * "floor({float})" function
3957 */
3958 static void
3959f_floor(typval_T *argvars, typval_T *rettv)
3960{
3961 float_T f = 0.0;
3962
3963 rettv->v_type = VAR_FLOAT;
3964 if (get_float_arg(argvars, &f) == OK)
3965 rettv->vval.v_float = floor(f);
3966 else
3967 rettv->vval.v_float = 0.0;
3968}
3969
3970/*
3971 * "fmod()" function
3972 */
3973 static void
3974f_fmod(typval_T *argvars, typval_T *rettv)
3975{
3976 float_T fx = 0.0, fy = 0.0;
3977
3978 rettv->v_type = VAR_FLOAT;
3979 if (get_float_arg(argvars, &fx) == OK
3980 && get_float_arg(&argvars[1], &fy) == OK)
3981 rettv->vval.v_float = fmod(fx, fy);
3982 else
3983 rettv->vval.v_float = 0.0;
3984}
3985#endif
3986
3987/*
3988 * "fnameescape({string})" function
3989 */
3990 static void
3991f_fnameescape(typval_T *argvars, typval_T *rettv)
3992{
3993 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003994 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003995 rettv->v_type = VAR_STRING;
3996}
3997
3998/*
3999 * "fnamemodify({fname}, {mods})" function
4000 */
4001 static void
4002f_fnamemodify(typval_T *argvars, typval_T *rettv)
4003{
4004 char_u *fname;
4005 char_u *mods;
4006 int usedlen = 0;
4007 int len;
4008 char_u *fbuf = NULL;
4009 char_u buf[NUMBUFLEN];
4010
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004011 fname = tv_get_string_chk(&argvars[0]);
4012 mods = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004013 if (fname == NULL || mods == NULL)
4014 fname = NULL;
4015 else
4016 {
4017 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02004018 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004019 }
4020
4021 rettv->v_type = VAR_STRING;
4022 if (fname == NULL)
4023 rettv->vval.v_string = NULL;
4024 else
4025 rettv->vval.v_string = vim_strnsave(fname, len);
4026 vim_free(fbuf);
4027}
4028
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004029/*
4030 * "foldclosed()" function
4031 */
4032 static void
4033foldclosed_both(
4034 typval_T *argvars UNUSED,
4035 typval_T *rettv,
4036 int end UNUSED)
4037{
4038#ifdef FEAT_FOLDING
4039 linenr_T lnum;
4040 linenr_T first, last;
4041
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004042 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004043 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4044 {
4045 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
4046 {
4047 if (end)
4048 rettv->vval.v_number = (varnumber_T)last;
4049 else
4050 rettv->vval.v_number = (varnumber_T)first;
4051 return;
4052 }
4053 }
4054#endif
4055 rettv->vval.v_number = -1;
4056}
4057
4058/*
4059 * "foldclosed()" function
4060 */
4061 static void
4062f_foldclosed(typval_T *argvars, typval_T *rettv)
4063{
4064 foldclosed_both(argvars, rettv, FALSE);
4065}
4066
4067/*
4068 * "foldclosedend()" function
4069 */
4070 static void
4071f_foldclosedend(typval_T *argvars, typval_T *rettv)
4072{
4073 foldclosed_both(argvars, rettv, TRUE);
4074}
4075
4076/*
4077 * "foldlevel()" function
4078 */
4079 static void
4080f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4081{
4082#ifdef FEAT_FOLDING
4083 linenr_T lnum;
4084
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004085 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004086 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4087 rettv->vval.v_number = foldLevel(lnum);
4088#endif
4089}
4090
4091/*
4092 * "foldtext()" function
4093 */
4094 static void
4095f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
4096{
4097#ifdef FEAT_FOLDING
4098 linenr_T foldstart;
4099 linenr_T foldend;
4100 char_u *dashes;
4101 linenr_T lnum;
4102 char_u *s;
4103 char_u *r;
4104 int len;
4105 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004106 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004107#endif
4108
4109 rettv->v_type = VAR_STRING;
4110 rettv->vval.v_string = NULL;
4111#ifdef FEAT_FOLDING
4112 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
4113 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
4114 dashes = get_vim_var_str(VV_FOLDDASHES);
4115 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
4116 && dashes != NULL)
4117 {
4118 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02004119 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004120 if (!linewhite(lnum))
4121 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004122
4123 /* Find interesting text in this line. */
4124 s = skipwhite(ml_get(lnum));
4125 /* skip C comment-start */
4126 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
4127 {
4128 s = skipwhite(s + 2);
4129 if (*skipwhite(s) == NUL
4130 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
4131 {
4132 s = skipwhite(ml_get(lnum + 1));
4133 if (*s == '*')
4134 s = skipwhite(s + 1);
4135 }
4136 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02004137 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01004138 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004139 r = alloc((unsigned)(STRLEN(txt)
4140 + STRLEN(dashes) /* for %s */
4141 + 20 /* for %3ld */
4142 + STRLEN(s))); /* concatenated */
4143 if (r != NULL)
4144 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02004145 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004146 len = (int)STRLEN(r);
4147 STRCAT(r, s);
4148 /* remove 'foldmarker' and 'commentstring' */
4149 foldtext_cleanup(r + len);
4150 rettv->vval.v_string = r;
4151 }
4152 }
4153#endif
4154}
4155
4156/*
4157 * "foldtextresult(lnum)" function
4158 */
4159 static void
4160f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
4161{
4162#ifdef FEAT_FOLDING
4163 linenr_T lnum;
4164 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004165 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004166 foldinfo_T foldinfo;
4167 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004168 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004169#endif
4170
4171 rettv->v_type = VAR_STRING;
4172 rettv->vval.v_string = NULL;
4173#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004174 if (entered)
4175 return; /* reject recursive use */
4176 entered = TRUE;
4177
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004178 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004179 /* treat illegal types and illegal string values for {lnum} the same */
4180 if (lnum < 0)
4181 lnum = 0;
4182 fold_count = foldedCount(curwin, lnum, &foldinfo);
4183 if (fold_count > 0)
4184 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01004185 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
4186 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004187 if (text == buf)
4188 text = vim_strsave(text);
4189 rettv->vval.v_string = text;
4190 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004191
4192 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004193#endif
4194}
4195
4196/*
4197 * "foreground()" function
4198 */
4199 static void
4200f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4201{
4202#ifdef FEAT_GUI
4203 if (gui.in_use)
4204 gui_mch_set_foreground();
4205#else
Bram Moolenaar4f974752019-02-17 17:44:42 +01004206# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004207 win32_set_foreground();
4208# endif
4209#endif
4210}
4211
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004212 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004213common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004214{
4215 char_u *s;
4216 char_u *name;
4217 int use_string = FALSE;
4218 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004219 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004220
4221 if (argvars[0].v_type == VAR_FUNC)
4222 {
4223 /* function(MyFunc, [arg], dict) */
4224 s = argvars[0].vval.v_string;
4225 }
4226 else if (argvars[0].v_type == VAR_PARTIAL
4227 && argvars[0].vval.v_partial != NULL)
4228 {
4229 /* function(dict.MyFunc, [arg]) */
4230 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004231 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004232 }
4233 else
4234 {
4235 /* function('MyFunc', [arg], dict) */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004236 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004237 use_string = TRUE;
4238 }
4239
Bram Moolenaar843b8842016-08-21 14:36:15 +02004240 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004241 {
4242 name = s;
4243 trans_name = trans_function_name(&name, FALSE,
4244 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4245 if (*name != NUL)
4246 s = NULL;
4247 }
4248
Bram Moolenaar843b8842016-08-21 14:36:15 +02004249 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4250 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004251 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004252 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004253 else if (trans_name != NULL && (is_funcref
4254 ? find_func(trans_name) == NULL
4255 : !translated_function_exists(trans_name)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004256 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004257 else
4258 {
4259 int dict_idx = 0;
4260 int arg_idx = 0;
4261 list_T *list = NULL;
4262
4263 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4264 {
4265 char sid_buf[25];
4266 int off = *s == 's' ? 2 : 5;
4267
4268 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4269 * also be called from another script. Using trans_function_name()
4270 * would also work, but some plugins depend on the name being
4271 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004272 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004273 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
4274 if (name != NULL)
4275 {
4276 STRCPY(name, sid_buf);
4277 STRCAT(name, s + off);
4278 }
4279 }
4280 else
4281 name = vim_strsave(s);
4282
4283 if (argvars[1].v_type != VAR_UNKNOWN)
4284 {
4285 if (argvars[2].v_type != VAR_UNKNOWN)
4286 {
4287 /* function(name, [args], dict) */
4288 arg_idx = 1;
4289 dict_idx = 2;
4290 }
4291 else if (argvars[1].v_type == VAR_DICT)
4292 /* function(name, dict) */
4293 dict_idx = 1;
4294 else
4295 /* function(name, [args]) */
4296 arg_idx = 1;
4297 if (dict_idx > 0)
4298 {
4299 if (argvars[dict_idx].v_type != VAR_DICT)
4300 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004301 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004302 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004303 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004304 }
4305 if (argvars[dict_idx].vval.v_dict == NULL)
4306 dict_idx = 0;
4307 }
4308 if (arg_idx > 0)
4309 {
4310 if (argvars[arg_idx].v_type != VAR_LIST)
4311 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004312 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004313 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004314 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004315 }
4316 list = argvars[arg_idx].vval.v_list;
4317 if (list == NULL || list->lv_len == 0)
4318 arg_idx = 0;
4319 }
4320 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004321 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004322 {
4323 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
4324
4325 /* result is a VAR_PARTIAL */
4326 if (pt == NULL)
4327 vim_free(name);
4328 else
4329 {
4330 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4331 {
4332 listitem_T *li;
4333 int i = 0;
4334 int arg_len = 0;
4335 int lv_len = 0;
4336
4337 if (arg_pt != NULL)
4338 arg_len = arg_pt->pt_argc;
4339 if (list != NULL)
4340 lv_len = list->lv_len;
4341 pt->pt_argc = arg_len + lv_len;
4342 pt->pt_argv = (typval_T *)alloc(
4343 sizeof(typval_T) * pt->pt_argc);
4344 if (pt->pt_argv == NULL)
4345 {
4346 vim_free(pt);
4347 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004348 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004349 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004350 for (i = 0; i < arg_len; i++)
4351 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4352 if (lv_len > 0)
4353 for (li = list->lv_first; li != NULL;
4354 li = li->li_next)
4355 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004356 }
4357
4358 /* For "function(dict.func, [], dict)" and "func" is a partial
4359 * use "dict". That is backwards compatible. */
4360 if (dict_idx > 0)
4361 {
4362 /* The dict is bound explicitly, pt_auto is FALSE. */
4363 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4364 ++pt->pt_dict->dv_refcount;
4365 }
4366 else if (arg_pt != NULL)
4367 {
4368 /* If the dict was bound automatically the result is also
4369 * bound automatically. */
4370 pt->pt_dict = arg_pt->pt_dict;
4371 pt->pt_auto = arg_pt->pt_auto;
4372 if (pt->pt_dict != NULL)
4373 ++pt->pt_dict->dv_refcount;
4374 }
4375
4376 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004377 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4378 {
4379 pt->pt_func = arg_pt->pt_func;
4380 func_ptr_ref(pt->pt_func);
4381 vim_free(name);
4382 }
4383 else if (is_funcref)
4384 {
4385 pt->pt_func = find_func(trans_name);
4386 func_ptr_ref(pt->pt_func);
4387 vim_free(name);
4388 }
4389 else
4390 {
4391 pt->pt_name = name;
4392 func_ref(name);
4393 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004394 }
4395 rettv->v_type = VAR_PARTIAL;
4396 rettv->vval.v_partial = pt;
4397 }
4398 else
4399 {
4400 /* result is a VAR_FUNC */
4401 rettv->v_type = VAR_FUNC;
4402 rettv->vval.v_string = name;
4403 func_ref(name);
4404 }
4405 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004406theend:
4407 vim_free(trans_name);
4408}
4409
4410/*
4411 * "funcref()" function
4412 */
4413 static void
4414f_funcref(typval_T *argvars, typval_T *rettv)
4415{
4416 common_function(argvars, rettv, TRUE);
4417}
4418
4419/*
4420 * "function()" function
4421 */
4422 static void
4423f_function(typval_T *argvars, typval_T *rettv)
4424{
4425 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004426}
4427
4428/*
4429 * "garbagecollect()" function
4430 */
4431 static void
4432f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4433{
4434 /* This is postponed until we are back at the toplevel, because we may be
4435 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4436 want_garbage_collect = TRUE;
4437
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004438 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004439 garbage_collect_at_exit = TRUE;
4440}
4441
4442/*
4443 * "get()" function
4444 */
4445 static void
4446f_get(typval_T *argvars, typval_T *rettv)
4447{
4448 listitem_T *li;
4449 list_T *l;
4450 dictitem_T *di;
4451 dict_T *d;
4452 typval_T *tv = NULL;
4453
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004454 if (argvars[0].v_type == VAR_BLOB)
4455 {
4456 int error = FALSE;
4457 int idx = tv_get_number_chk(&argvars[1], &error);
4458
4459 if (!error)
4460 {
4461 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004462 if (idx < 0)
4463 idx = blob_len(argvars[0].vval.v_blob) + idx;
4464 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
4465 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004466 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004467 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004468 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004469 tv = rettv;
4470 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004471 }
4472 }
4473 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004474 {
4475 if ((l = argvars[0].vval.v_list) != NULL)
4476 {
4477 int error = FALSE;
4478
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004479 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004480 if (!error && li != NULL)
4481 tv = &li->li_tv;
4482 }
4483 }
4484 else if (argvars[0].v_type == VAR_DICT)
4485 {
4486 if ((d = argvars[0].vval.v_dict) != NULL)
4487 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004488 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004489 if (di != NULL)
4490 tv = &di->di_tv;
4491 }
4492 }
4493 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4494 {
4495 partial_T *pt;
4496 partial_T fref_pt;
4497
4498 if (argvars[0].v_type == VAR_PARTIAL)
4499 pt = argvars[0].vval.v_partial;
4500 else
4501 {
4502 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4503 fref_pt.pt_name = argvars[0].vval.v_string;
4504 pt = &fref_pt;
4505 }
4506
4507 if (pt != NULL)
4508 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004509 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004510 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004511
4512 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4513 {
4514 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004515 n = partial_name(pt);
4516 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004517 rettv->vval.v_string = NULL;
4518 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004519 {
4520 rettv->vval.v_string = vim_strsave(n);
4521 if (rettv->v_type == VAR_FUNC)
4522 func_ref(rettv->vval.v_string);
4523 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004524 }
4525 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004526 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004527 else if (STRCMP(what, "args") == 0)
4528 {
4529 rettv->v_type = VAR_LIST;
4530 if (rettv_list_alloc(rettv) == OK)
4531 {
4532 int i;
4533
4534 for (i = 0; i < pt->pt_argc; ++i)
4535 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4536 }
4537 }
4538 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004539 semsg(_(e_invarg2), what);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004540 return;
4541 }
4542 }
4543 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004544 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004545
4546 if (tv == NULL)
4547 {
4548 if (argvars[2].v_type != VAR_UNKNOWN)
4549 copy_tv(&argvars[2], rettv);
4550 }
4551 else
4552 copy_tv(tv, rettv);
4553}
4554
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004555/*
4556 * Returns buffer options, variables and other attributes in a dictionary.
4557 */
4558 static dict_T *
4559get_buffer_info(buf_T *buf)
4560{
4561 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004562 tabpage_T *tp;
4563 win_T *wp;
4564 list_T *windows;
4565
4566 dict = dict_alloc();
4567 if (dict == NULL)
4568 return NULL;
4569
Bram Moolenaare0be1672018-07-08 16:50:37 +02004570 dict_add_number(dict, "bufnr", buf->b_fnum);
4571 dict_add_string(dict, "name", buf->b_ffname);
4572 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4573 : buflist_findlnum(buf));
4574 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4575 dict_add_number(dict, "listed", buf->b_p_bl);
4576 dict_add_number(dict, "changed", bufIsChanged(buf));
4577 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4578 dict_add_number(dict, "hidden",
4579 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004580
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004581 /* Get a reference to buffer variables */
4582 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004583
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004584 /* List of windows displaying this buffer */
4585 windows = list_alloc();
4586 if (windows != NULL)
4587 {
4588 FOR_ALL_TAB_WINDOWS(tp, wp)
4589 if (wp->w_buffer == buf)
4590 list_append_number(windows, (varnumber_T)wp->w_id);
4591 dict_add_list(dict, "windows", windows);
4592 }
4593
4594#ifdef FEAT_SIGNS
4595 if (buf->b_signlist != NULL)
4596 {
4597 /* List of signs placed in this buffer */
4598 list_T *signs = list_alloc();
4599 if (signs != NULL)
4600 {
4601 get_buffer_signs(buf, signs);
4602 dict_add_list(dict, "signs", signs);
4603 }
4604 }
4605#endif
4606
4607 return dict;
4608}
4609
4610/*
4611 * "getbufinfo()" function
4612 */
4613 static void
4614f_getbufinfo(typval_T *argvars, typval_T *rettv)
4615{
4616 buf_T *buf = NULL;
4617 buf_T *argbuf = NULL;
4618 dict_T *d;
4619 int filtered = FALSE;
4620 int sel_buflisted = FALSE;
4621 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004622 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004623
4624 if (rettv_list_alloc(rettv) != OK)
4625 return;
4626
4627 /* List of all the buffers or selected buffers */
4628 if (argvars[0].v_type == VAR_DICT)
4629 {
4630 dict_T *sel_d = argvars[0].vval.v_dict;
4631
4632 if (sel_d != NULL)
4633 {
4634 dictitem_T *di;
4635
4636 filtered = TRUE;
4637
4638 di = dict_find(sel_d, (char_u *)"buflisted", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004639 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004640 sel_buflisted = TRUE;
4641
4642 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004643 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004644 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004645
4646 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004647 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004648 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004649 }
4650 }
4651 else if (argvars[0].v_type != VAR_UNKNOWN)
4652 {
4653 /* Information about one buffer. Argument specifies the buffer */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004654 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004655 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004656 argbuf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004657 --emsg_off;
4658 if (argbuf == NULL)
4659 return;
4660 }
4661
4662 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004663 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004664 {
4665 if (argbuf != NULL && argbuf != buf)
4666 continue;
4667 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004668 || (sel_buflisted && !buf->b_p_bl)
4669 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004670 continue;
4671
4672 d = get_buffer_info(buf);
4673 if (d != NULL)
4674 list_append_dict(rettv->vval.v_list, d);
4675 if (argbuf != NULL)
4676 return;
4677 }
4678}
4679
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004680/*
4681 * Get line or list of lines from buffer "buf" into "rettv".
4682 * Return a range (from start to end) of lines in rettv from the specified
4683 * buffer.
4684 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4685 */
4686 static void
4687get_buffer_lines(
4688 buf_T *buf,
4689 linenr_T start,
4690 linenr_T end,
4691 int retlist,
4692 typval_T *rettv)
4693{
4694 char_u *p;
4695
4696 rettv->v_type = VAR_STRING;
4697 rettv->vval.v_string = NULL;
4698 if (retlist && rettv_list_alloc(rettv) == FAIL)
4699 return;
4700
4701 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4702 return;
4703
4704 if (!retlist)
4705 {
4706 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4707 p = ml_get_buf(buf, start, FALSE);
4708 else
4709 p = (char_u *)"";
4710 rettv->vval.v_string = vim_strsave(p);
4711 }
4712 else
4713 {
4714 if (end < start)
4715 return;
4716
4717 if (start < 1)
4718 start = 1;
4719 if (end > buf->b_ml.ml_line_count)
4720 end = buf->b_ml.ml_line_count;
4721 while (start <= end)
4722 if (list_append_string(rettv->vval.v_list,
4723 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4724 break;
4725 }
4726}
4727
4728/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004729 * "getbufline()" function
4730 */
4731 static void
4732f_getbufline(typval_T *argvars, typval_T *rettv)
4733{
4734 linenr_T lnum;
4735 linenr_T end;
4736 buf_T *buf;
4737
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004738 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004739 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004740 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004741 --emsg_off;
4742
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004743 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004744 if (argvars[2].v_type == VAR_UNKNOWN)
4745 end = lnum;
4746 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004747 end = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004748
4749 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4750}
4751
4752/*
4753 * "getbufvar()" function
4754 */
4755 static void
4756f_getbufvar(typval_T *argvars, typval_T *rettv)
4757{
4758 buf_T *buf;
4759 buf_T *save_curbuf;
4760 char_u *varname;
4761 dictitem_T *v;
4762 int done = FALSE;
4763
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004764 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
4765 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004766 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004767 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004768
4769 rettv->v_type = VAR_STRING;
4770 rettv->vval.v_string = NULL;
4771
4772 if (buf != NULL && varname != NULL)
4773 {
4774 /* set curbuf to be our buf, temporarily */
4775 save_curbuf = curbuf;
4776 curbuf = buf;
4777
Bram Moolenaar30567352016-08-27 21:25:44 +02004778 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004779 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004780 if (varname[1] == NUL)
4781 {
4782 /* get all buffer-local options in a dict */
4783 dict_T *opts = get_winbuf_options(TRUE);
4784
4785 if (opts != NULL)
4786 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004787 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004788 done = TRUE;
4789 }
4790 }
4791 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4792 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004793 done = TRUE;
4794 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004795 else
4796 {
4797 /* Look up the variable. */
4798 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4799 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4800 'b', varname, FALSE);
4801 if (v != NULL)
4802 {
4803 copy_tv(&v->di_tv, rettv);
4804 done = TRUE;
4805 }
4806 }
4807
4808 /* restore previous notion of curbuf */
4809 curbuf = save_curbuf;
4810 }
4811
4812 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4813 /* use the default value */
4814 copy_tv(&argvars[2], rettv);
4815
4816 --emsg_off;
4817}
4818
4819/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004820 * "getchangelist()" function
4821 */
4822 static void
4823f_getchangelist(typval_T *argvars, typval_T *rettv)
4824{
4825#ifdef FEAT_JUMPLIST
4826 buf_T *buf;
4827 int i;
4828 list_T *l;
4829 dict_T *d;
4830#endif
4831
4832 if (rettv_list_alloc(rettv) != OK)
4833 return;
4834
4835#ifdef FEAT_JUMPLIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004836 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004837 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004838 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004839 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004840 if (buf == NULL)
4841 return;
4842
4843 l = list_alloc();
4844 if (l == NULL)
4845 return;
4846
4847 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4848 return;
4849 /*
4850 * The current window change list index tracks only the position in the
4851 * current buffer change list. For other buffers, use the change list
4852 * length as the current index.
4853 */
4854 list_append_number(rettv->vval.v_list,
4855 (varnumber_T)((buf == curwin->w_buffer)
4856 ? curwin->w_changelistidx : buf->b_changelistlen));
4857
4858 for (i = 0; i < buf->b_changelistlen; ++i)
4859 {
4860 if (buf->b_changelist[i].lnum == 0)
4861 continue;
4862 if ((d = dict_alloc()) == NULL)
4863 return;
4864 if (list_append_dict(l, d) == FAIL)
4865 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004866 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4867 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004868 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004869 }
4870#endif
4871}
4872/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004873 * "getchar()" function
4874 */
4875 static void
4876f_getchar(typval_T *argvars, typval_T *rettv)
4877{
4878 varnumber_T n;
4879 int error = FALSE;
4880
Bram Moolenaar84d93902018-09-11 20:10:20 +02004881#ifdef MESSAGE_QUEUE
4882 // vpeekc() used to check for messages, but that caused problems, invoking
4883 // a callback where it was not expected. Some plugins use getchar(1) in a
4884 // loop to await a message, therefore make sure we check for messages here.
4885 parse_queued_messages();
4886#endif
4887
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004888 /* Position the cursor. Needed after a message that ends in a space. */
4889 windgoto(msg_row, msg_col);
4890
4891 ++no_mapping;
4892 ++allow_keys;
4893 for (;;)
4894 {
4895 if (argvars[0].v_type == VAR_UNKNOWN)
4896 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004897 n = plain_vgetc();
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004898 else if (tv_get_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004899 /* getchar(1): only check if char avail */
4900 n = vpeekc_any();
4901 else if (error || vpeekc_any() == NUL)
4902 /* illegal argument or getchar(0) and no char avail: return zero */
4903 n = 0;
4904 else
4905 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004906 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004907
4908 if (n == K_IGNORE)
4909 continue;
4910 break;
4911 }
4912 --no_mapping;
4913 --allow_keys;
4914
4915 set_vim_var_nr(VV_MOUSE_WIN, 0);
4916 set_vim_var_nr(VV_MOUSE_WINID, 0);
4917 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4918 set_vim_var_nr(VV_MOUSE_COL, 0);
4919
4920 rettv->vval.v_number = n;
4921 if (IS_SPECIAL(n) || mod_mask != 0)
4922 {
4923 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4924 int i = 0;
4925
4926 /* Turn a special key into three bytes, plus modifier. */
4927 if (mod_mask != 0)
4928 {
4929 temp[i++] = K_SPECIAL;
4930 temp[i++] = KS_MODIFIER;
4931 temp[i++] = mod_mask;
4932 }
4933 if (IS_SPECIAL(n))
4934 {
4935 temp[i++] = K_SPECIAL;
4936 temp[i++] = K_SECOND(n);
4937 temp[i++] = K_THIRD(n);
4938 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004939 else if (has_mbyte)
4940 i += (*mb_char2bytes)(n, temp + i);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004941 else
4942 temp[i++] = n;
4943 temp[i++] = NUL;
4944 rettv->v_type = VAR_STRING;
4945 rettv->vval.v_string = vim_strsave(temp);
4946
4947#ifdef FEAT_MOUSE
4948 if (is_mouse_key(n))
4949 {
4950 int row = mouse_row;
4951 int col = mouse_col;
4952 win_T *win;
4953 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004954 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004955 int winnr = 1;
4956
4957 if (row >= 0 && col >= 0)
4958 {
4959 /* Find the window at the mouse coordinates and compute the
4960 * text position. */
4961 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004962 if (win == NULL)
4963 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004964 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004965 for (wp = firstwin; wp != win; wp = wp->w_next)
4966 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004967 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4968 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4969 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4970 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4971 }
4972 }
4973#endif
4974 }
4975}
4976
4977/*
4978 * "getcharmod()" function
4979 */
4980 static void
4981f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4982{
4983 rettv->vval.v_number = mod_mask;
4984}
4985
4986/*
4987 * "getcharsearch()" function
4988 */
4989 static void
4990f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4991{
4992 if (rettv_dict_alloc(rettv) != FAIL)
4993 {
4994 dict_T *dict = rettv->vval.v_dict;
4995
Bram Moolenaare0be1672018-07-08 16:50:37 +02004996 dict_add_string(dict, "char", last_csearch());
4997 dict_add_number(dict, "forward", last_csearch_forward());
4998 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004999 }
5000}
5001
5002/*
5003 * "getcmdline()" function
5004 */
5005 static void
5006f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
5007{
5008 rettv->v_type = VAR_STRING;
5009 rettv->vval.v_string = get_cmdline_str();
5010}
5011
5012/*
5013 * "getcmdpos()" function
5014 */
5015 static void
5016f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
5017{
5018 rettv->vval.v_number = get_cmdline_pos() + 1;
5019}
5020
5021/*
5022 * "getcmdtype()" function
5023 */
5024 static void
5025f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
5026{
5027 rettv->v_type = VAR_STRING;
5028 rettv->vval.v_string = alloc(2);
5029 if (rettv->vval.v_string != NULL)
5030 {
5031 rettv->vval.v_string[0] = get_cmdline_type();
5032 rettv->vval.v_string[1] = NUL;
5033 }
5034}
5035
5036/*
5037 * "getcmdwintype()" function
5038 */
5039 static void
5040f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
5041{
5042 rettv->v_type = VAR_STRING;
5043 rettv->vval.v_string = NULL;
5044#ifdef FEAT_CMDWIN
5045 rettv->vval.v_string = alloc(2);
5046 if (rettv->vval.v_string != NULL)
5047 {
5048 rettv->vval.v_string[0] = cmdwin_type;
5049 rettv->vval.v_string[1] = NUL;
5050 }
5051#endif
5052}
5053
5054#if defined(FEAT_CMDL_COMPL)
5055/*
5056 * "getcompletion()" function
5057 */
5058 static void
5059f_getcompletion(typval_T *argvars, typval_T *rettv)
5060{
5061 char_u *pat;
5062 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005063 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005064 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
5065 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005066
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005067 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005068 filtered = tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005069
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005070 if (p_wic)
5071 options |= WILD_ICASE;
5072
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005073 /* For filtered results, 'wildignore' is used */
5074 if (!filtered)
5075 options |= WILD_KEEP_ALL;
5076
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005077 ExpandInit(&xpc);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005078 xpc.xp_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005079 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005080 xpc.xp_context = cmdcomplete_str_to_type(tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005081 if (xpc.xp_context == EXPAND_NOTHING)
5082 {
5083 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005084 semsg(_(e_invarg2), argvars[1].vval.v_string);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005085 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005086 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005087 return;
5088 }
5089
5090# if defined(FEAT_MENU)
5091 if (xpc.xp_context == EXPAND_MENUS)
5092 {
5093 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
5094 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5095 }
5096# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02005097#ifdef FEAT_CSCOPE
5098 if (xpc.xp_context == EXPAND_CSCOPE)
5099 {
5100 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
5101 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5102 }
5103#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02005104#ifdef FEAT_SIGNS
5105 if (xpc.xp_context == EXPAND_SIGN)
5106 {
5107 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
5108 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5109 }
5110#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005111
5112 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
5113 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
5114 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005115 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005116
5117 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
5118
5119 for (i = 0; i < xpc.xp_numfiles; i++)
5120 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5121 }
5122 vim_free(pat);
5123 ExpandCleanup(&xpc);
5124}
5125#endif
5126
5127/*
5128 * "getcwd()" function
5129 */
5130 static void
5131f_getcwd(typval_T *argvars, typval_T *rettv)
5132{
5133 win_T *wp = NULL;
5134 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01005135 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005136
5137 rettv->v_type = VAR_STRING;
5138 rettv->vval.v_string = NULL;
5139
Bram Moolenaar54591292018-02-09 20:53:59 +01005140 if (argvars[0].v_type == VAR_NUMBER && argvars[0].vval.v_number == -1)
5141 global = TRUE;
5142 else
5143 wp = find_tabwin(&argvars[0], &argvars[1]);
5144
5145 if (wp != NULL && wp->w_localdir != NULL)
5146 rettv->vval.v_string = vim_strsave(wp->w_localdir);
5147 else if (wp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005148 {
Bram Moolenaar54591292018-02-09 20:53:59 +01005149 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005150 rettv->vval.v_string = vim_strsave(globaldir);
5151 else
5152 {
5153 cwd = alloc(MAXPATHL);
5154 if (cwd != NULL)
5155 {
5156 if (mch_dirname(cwd, MAXPATHL) != FAIL)
5157 rettv->vval.v_string = vim_strsave(cwd);
5158 vim_free(cwd);
5159 }
5160 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005161 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02005162#ifdef BACKSLASH_IN_FILENAME
5163 if (rettv->vval.v_string != NULL)
5164 slash_adjust(rettv->vval.v_string);
5165#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005166}
5167
5168/*
5169 * "getfontname()" function
5170 */
5171 static void
5172f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
5173{
5174 rettv->v_type = VAR_STRING;
5175 rettv->vval.v_string = NULL;
5176#ifdef FEAT_GUI
5177 if (gui.in_use)
5178 {
5179 GuiFont font;
5180 char_u *name = NULL;
5181
5182 if (argvars[0].v_type == VAR_UNKNOWN)
5183 {
5184 /* Get the "Normal" font. Either the name saved by
5185 * hl_set_font_name() or from the font ID. */
5186 font = gui.norm_font;
5187 name = hl_get_font_name();
5188 }
5189 else
5190 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005191 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005192 if (STRCMP(name, "*") == 0) /* don't use font dialog */
5193 return;
5194 font = gui_mch_get_font(name, FALSE);
5195 if (font == NOFONT)
5196 return; /* Invalid font name, return empty string. */
5197 }
5198 rettv->vval.v_string = gui_mch_get_fontname(font, name);
5199 if (argvars[0].v_type != VAR_UNKNOWN)
5200 gui_mch_free_font(font);
5201 }
5202#endif
5203}
5204
5205/*
5206 * "getfperm({fname})" function
5207 */
5208 static void
5209f_getfperm(typval_T *argvars, typval_T *rettv)
5210{
5211 char_u *fname;
5212 stat_T st;
5213 char_u *perm = NULL;
5214 char_u flags[] = "rwx";
5215 int i;
5216
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005217 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005218
5219 rettv->v_type = VAR_STRING;
5220 if (mch_stat((char *)fname, &st) >= 0)
5221 {
5222 perm = vim_strsave((char_u *)"---------");
5223 if (perm != NULL)
5224 {
5225 for (i = 0; i < 9; i++)
5226 {
5227 if (st.st_mode & (1 << (8 - i)))
5228 perm[i] = flags[i % 3];
5229 }
5230 }
5231 }
5232 rettv->vval.v_string = perm;
5233}
5234
5235/*
5236 * "getfsize({fname})" function
5237 */
5238 static void
5239f_getfsize(typval_T *argvars, typval_T *rettv)
5240{
5241 char_u *fname;
5242 stat_T st;
5243
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005244 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005245
5246 rettv->v_type = VAR_NUMBER;
5247
5248 if (mch_stat((char *)fname, &st) >= 0)
5249 {
5250 if (mch_isdir(fname))
5251 rettv->vval.v_number = 0;
5252 else
5253 {
5254 rettv->vval.v_number = (varnumber_T)st.st_size;
5255
5256 /* non-perfect check for overflow */
5257 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5258 rettv->vval.v_number = -2;
5259 }
5260 }
5261 else
5262 rettv->vval.v_number = -1;
5263}
5264
5265/*
5266 * "getftime({fname})" function
5267 */
5268 static void
5269f_getftime(typval_T *argvars, typval_T *rettv)
5270{
5271 char_u *fname;
5272 stat_T st;
5273
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005274 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005275
5276 if (mch_stat((char *)fname, &st) >= 0)
5277 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5278 else
5279 rettv->vval.v_number = -1;
5280}
5281
5282/*
5283 * "getftype({fname})" function
5284 */
5285 static void
5286f_getftype(typval_T *argvars, typval_T *rettv)
5287{
5288 char_u *fname;
5289 stat_T st;
5290 char_u *type = NULL;
5291 char *t;
5292
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005293 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005294
5295 rettv->v_type = VAR_STRING;
5296 if (mch_lstat((char *)fname, &st) >= 0)
5297 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005298 if (S_ISREG(st.st_mode))
5299 t = "file";
5300 else if (S_ISDIR(st.st_mode))
5301 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005302 else if (S_ISLNK(st.st_mode))
5303 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005304 else if (S_ISBLK(st.st_mode))
5305 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005306 else if (S_ISCHR(st.st_mode))
5307 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005308 else if (S_ISFIFO(st.st_mode))
5309 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005310 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005311 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005312 else
5313 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005314 type = vim_strsave((char_u *)t);
5315 }
5316 rettv->vval.v_string = type;
5317}
5318
5319/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005320 * "getjumplist()" function
5321 */
5322 static void
5323f_getjumplist(typval_T *argvars, typval_T *rettv)
5324{
5325#ifdef FEAT_JUMPLIST
5326 win_T *wp;
5327 int i;
5328 list_T *l;
5329 dict_T *d;
5330#endif
5331
5332 if (rettv_list_alloc(rettv) != OK)
5333 return;
5334
5335#ifdef FEAT_JUMPLIST
5336 wp = find_tabwin(&argvars[0], &argvars[1]);
5337 if (wp == NULL)
5338 return;
5339
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01005340 cleanup_jumplist(wp, TRUE);
5341
Bram Moolenaar4f505882018-02-10 21:06:32 +01005342 l = list_alloc();
5343 if (l == NULL)
5344 return;
5345
5346 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5347 return;
5348 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5349
5350 for (i = 0; i < wp->w_jumplistlen; ++i)
5351 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005352 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5353 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005354 if ((d = dict_alloc()) == NULL)
5355 return;
5356 if (list_append_dict(l, d) == FAIL)
5357 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005358 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5359 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005360 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005361 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005362 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005363 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005364 }
5365#endif
5366}
5367
5368/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005369 * "getline(lnum, [end])" function
5370 */
5371 static void
5372f_getline(typval_T *argvars, typval_T *rettv)
5373{
5374 linenr_T lnum;
5375 linenr_T end;
5376 int retlist;
5377
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005378 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005379 if (argvars[1].v_type == VAR_UNKNOWN)
5380 {
5381 end = 0;
5382 retlist = FALSE;
5383 }
5384 else
5385 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005386 end = tv_get_lnum(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005387 retlist = TRUE;
5388 }
5389
5390 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5391}
5392
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005393#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005394 static void
5395get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5396{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005397 if (what_arg->v_type == VAR_UNKNOWN)
5398 {
5399 if (rettv_list_alloc(rettv) == OK)
5400 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005401 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005402 }
5403 else
5404 {
5405 if (rettv_dict_alloc(rettv) == OK)
5406 if (is_qf || (wp != NULL))
5407 {
5408 if (what_arg->v_type == VAR_DICT)
5409 {
5410 dict_T *d = what_arg->vval.v_dict;
5411
5412 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005413 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005414 }
5415 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005416 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +02005417 }
5418 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005419}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005420#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005421
5422/*
5423 * "getloclist()" function
5424 */
5425 static void
5426f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5427{
5428#ifdef FEAT_QUICKFIX
5429 win_T *wp;
5430
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005431 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005432 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5433#endif
5434}
5435
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005436/*
5437 * "getmatches()" function
5438 */
5439 static void
5440f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5441{
5442#ifdef FEAT_SEARCH_EXTRA
5443 dict_T *dict;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005444 matchitem_T *cur;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005445 int i;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005446 win_T *win = get_optional_window(argvars, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005447
Bram Moolenaaraff74912019-03-30 18:11:49 +01005448 if (rettv_list_alloc(rettv) == FAIL || win == NULL)
5449 return;
5450
5451 cur = win->w_match_head;
5452 while (cur != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005453 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005454 dict = dict_alloc();
5455 if (dict == NULL)
5456 return;
5457 if (cur->match.regprog == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005458 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005459 /* match added with matchaddpos() */
5460 for (i = 0; i < MAXPOSMATCH; ++i)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005461 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005462 llpos_T *llpos;
5463 char buf[6];
5464 list_T *l;
5465
5466 llpos = &cur->pos.pos[i];
5467 if (llpos->lnum == 0)
5468 break;
5469 l = list_alloc();
5470 if (l == NULL)
5471 break;
5472 list_append_number(l, (varnumber_T)llpos->lnum);
5473 if (llpos->col > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005474 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005475 list_append_number(l, (varnumber_T)llpos->col);
5476 list_append_number(l, (varnumber_T)llpos->len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005477 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005478 sprintf(buf, "pos%d", i + 1);
5479 dict_add_list(dict, buf, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005480 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005481 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005482 else
5483 {
5484 dict_add_string(dict, "pattern", cur->pattern);
5485 }
5486 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
5487 dict_add_number(dict, "priority", (long)cur->priority);
5488 dict_add_number(dict, "id", (long)cur->id);
5489# if defined(FEAT_CONCEAL)
5490 if (cur->conceal_char)
5491 {
5492 char_u buf[MB_MAXBYTES + 1];
5493
5494 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
5495 dict_add_string(dict, "conceal", (char_u *)&buf);
5496 }
5497# endif
5498 list_append_dict(rettv->vval.v_list, dict);
5499 cur = cur->next;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005500 }
5501#endif
5502}
5503
5504/*
5505 * "getpid()" function
5506 */
5507 static void
5508f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5509{
5510 rettv->vval.v_number = mch_get_pid();
5511}
5512
5513 static void
5514getpos_both(
5515 typval_T *argvars,
5516 typval_T *rettv,
5517 int getcurpos)
5518{
5519 pos_T *fp;
5520 list_T *l;
5521 int fnum = -1;
5522
5523 if (rettv_list_alloc(rettv) == OK)
5524 {
5525 l = rettv->vval.v_list;
5526 if (getcurpos)
5527 fp = &curwin->w_cursor;
5528 else
5529 fp = var2fpos(&argvars[0], TRUE, &fnum);
5530 if (fnum != -1)
5531 list_append_number(l, (varnumber_T)fnum);
5532 else
5533 list_append_number(l, (varnumber_T)0);
5534 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5535 : (varnumber_T)0);
5536 list_append_number(l, (fp != NULL)
5537 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5538 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005539 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005540 (varnumber_T)0);
5541 if (getcurpos)
5542 {
Bram Moolenaar19a66852019-03-07 11:25:32 +01005543 int save_set_curswant = curwin->w_set_curswant;
5544 colnr_T save_curswant = curwin->w_curswant;
5545 colnr_T save_virtcol = curwin->w_virtcol;
5546
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005547 update_curswant();
5548 list_append_number(l, curwin->w_curswant == MAXCOL ?
5549 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar19a66852019-03-07 11:25:32 +01005550
5551 // Do not change "curswant", as it is unexpected that a get
5552 // function has a side effect.
5553 if (save_set_curswant)
5554 {
5555 curwin->w_set_curswant = save_set_curswant;
5556 curwin->w_curswant = save_curswant;
5557 curwin->w_virtcol = save_virtcol;
5558 curwin->w_valid &= ~VALID_VIRTCOL;
5559 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005560 }
5561 }
5562 else
5563 rettv->vval.v_number = FALSE;
5564}
5565
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005566/*
5567 * "getcurpos()" function
5568 */
5569 static void
5570f_getcurpos(typval_T *argvars, typval_T *rettv)
5571{
5572 getpos_both(argvars, rettv, TRUE);
5573}
5574
5575/*
5576 * "getpos(string)" function
5577 */
5578 static void
5579f_getpos(typval_T *argvars, typval_T *rettv)
5580{
5581 getpos_both(argvars, rettv, FALSE);
5582}
5583
5584/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005585 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005586 */
5587 static void
5588f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5589{
5590#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005591 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005592#endif
5593}
5594
5595/*
5596 * "getreg()" function
5597 */
5598 static void
5599f_getreg(typval_T *argvars, typval_T *rettv)
5600{
5601 char_u *strregname;
5602 int regname;
5603 int arg2 = FALSE;
5604 int return_list = FALSE;
5605 int error = FALSE;
5606
5607 if (argvars[0].v_type != VAR_UNKNOWN)
5608 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005609 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005610 error = strregname == NULL;
5611 if (argvars[1].v_type != VAR_UNKNOWN)
5612 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005613 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005614 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005615 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005616 }
5617 }
5618 else
5619 strregname = get_vim_var_str(VV_REG);
5620
5621 if (error)
5622 return;
5623
5624 regname = (strregname == NULL ? '"' : *strregname);
5625 if (regname == 0)
5626 regname = '"';
5627
5628 if (return_list)
5629 {
5630 rettv->v_type = VAR_LIST;
5631 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5632 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5633 if (rettv->vval.v_list == NULL)
5634 (void)rettv_list_alloc(rettv);
5635 else
5636 ++rettv->vval.v_list->lv_refcount;
5637 }
5638 else
5639 {
5640 rettv->v_type = VAR_STRING;
5641 rettv->vval.v_string = get_reg_contents(regname,
5642 arg2 ? GREG_EXPR_SRC : 0);
5643 }
5644}
5645
5646/*
5647 * "getregtype()" function
5648 */
5649 static void
5650f_getregtype(typval_T *argvars, typval_T *rettv)
5651{
5652 char_u *strregname;
5653 int regname;
5654 char_u buf[NUMBUFLEN + 2];
5655 long reglen = 0;
5656
5657 if (argvars[0].v_type != VAR_UNKNOWN)
5658 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005659 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005660 if (strregname == NULL) /* type error; errmsg already given */
5661 {
5662 rettv->v_type = VAR_STRING;
5663 rettv->vval.v_string = NULL;
5664 return;
5665 }
5666 }
5667 else
5668 /* Default to v:register */
5669 strregname = get_vim_var_str(VV_REG);
5670
5671 regname = (strregname == NULL ? '"' : *strregname);
5672 if (regname == 0)
5673 regname = '"';
5674
5675 buf[0] = NUL;
5676 buf[1] = NUL;
5677 switch (get_reg_type(regname, &reglen))
5678 {
5679 case MLINE: buf[0] = 'V'; break;
5680 case MCHAR: buf[0] = 'v'; break;
5681 case MBLOCK:
5682 buf[0] = Ctrl_V;
5683 sprintf((char *)buf + 1, "%ld", reglen + 1);
5684 break;
5685 }
5686 rettv->v_type = VAR_STRING;
5687 rettv->vval.v_string = vim_strsave(buf);
5688}
5689
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005690/*
5691 * Returns information (variables, options, etc.) about a tab page
5692 * as a dictionary.
5693 */
5694 static dict_T *
5695get_tabpage_info(tabpage_T *tp, int tp_idx)
5696{
5697 win_T *wp;
5698 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005699 list_T *l;
5700
5701 dict = dict_alloc();
5702 if (dict == NULL)
5703 return NULL;
5704
Bram Moolenaare0be1672018-07-08 16:50:37 +02005705 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005706
5707 l = list_alloc();
5708 if (l != NULL)
5709 {
5710 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5711 wp; wp = wp->w_next)
5712 list_append_number(l, (varnumber_T)wp->w_id);
5713 dict_add_list(dict, "windows", l);
5714 }
5715
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005716 /* Make a reference to tabpage variables */
5717 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005718
5719 return dict;
5720}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005721
5722/*
5723 * "gettabinfo()" function
5724 */
5725 static void
5726f_gettabinfo(typval_T *argvars, typval_T *rettv)
5727{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005728 tabpage_T *tp, *tparg = NULL;
5729 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005730 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005731
5732 if (rettv_list_alloc(rettv) != OK)
5733 return;
5734
5735 if (argvars[0].v_type != VAR_UNKNOWN)
5736 {
5737 /* Information about one tab page */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005738 tparg = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005739 if (tparg == NULL)
5740 return;
5741 }
5742
5743 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005744 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005745 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005746 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005747 if (tparg != NULL && tp != tparg)
5748 continue;
5749 d = get_tabpage_info(tp, tpnr);
5750 if (d != NULL)
5751 list_append_dict(rettv->vval.v_list, d);
5752 if (tparg != NULL)
5753 return;
5754 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005755}
5756
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005757/*
5758 * "gettabvar()" function
5759 */
5760 static void
5761f_gettabvar(typval_T *argvars, typval_T *rettv)
5762{
5763 win_T *oldcurwin;
5764 tabpage_T *tp, *oldtabpage;
5765 dictitem_T *v;
5766 char_u *varname;
5767 int done = FALSE;
5768
5769 rettv->v_type = VAR_STRING;
5770 rettv->vval.v_string = NULL;
5771
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005772 varname = tv_get_string_chk(&argvars[1]);
5773 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005774 if (tp != NULL && varname != NULL)
5775 {
5776 /* Set tp to be our tabpage, temporarily. Also set the window to the
5777 * first window in the tabpage, otherwise the window is not valid. */
5778 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005779 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5780 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005781 {
5782 /* look up the variable */
5783 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5784 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5785 if (v != NULL)
5786 {
5787 copy_tv(&v->di_tv, rettv);
5788 done = TRUE;
5789 }
5790 }
5791
5792 /* restore previous notion of curwin */
5793 restore_win(oldcurwin, oldtabpage, TRUE);
5794 }
5795
5796 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5797 copy_tv(&argvars[2], rettv);
5798}
5799
5800/*
5801 * "gettabwinvar()" function
5802 */
5803 static void
5804f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5805{
5806 getwinvar(argvars, rettv, 1);
5807}
5808
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005809/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01005810 * "gettagstack()" function
5811 */
5812 static void
5813f_gettagstack(typval_T *argvars, typval_T *rettv)
5814{
5815 win_T *wp = curwin; // default is current window
5816
5817 if (rettv_dict_alloc(rettv) != OK)
5818 return;
5819
5820 if (argvars[0].v_type != VAR_UNKNOWN)
5821 {
5822 wp = find_win_by_nr_or_id(&argvars[0]);
5823 if (wp == NULL)
5824 return;
5825 }
5826
5827 get_tagstack(wp, rettv->vval.v_dict);
5828}
5829
5830/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005831 * Returns information about a window as a dictionary.
5832 */
5833 static dict_T *
5834get_win_info(win_T *wp, short tpnr, short winnr)
5835{
5836 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005837
5838 dict = dict_alloc();
5839 if (dict == NULL)
5840 return NULL;
5841
Bram Moolenaare0be1672018-07-08 16:50:37 +02005842 dict_add_number(dict, "tabnr", tpnr);
5843 dict_add_number(dict, "winnr", winnr);
5844 dict_add_number(dict, "winid", wp->w_id);
5845 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005846 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01005847 dict_add_number(dict, "topline", wp->w_topline);
5848 dict_add_number(dict, "botline", wp->w_botline - 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005849#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02005850 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005851#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005852 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005853 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005854 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005855
Bram Moolenaar69905d12017-08-13 18:14:47 +02005856#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02005857 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02005858#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005859#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02005860 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
5861 dict_add_number(dict, "loclist",
5862 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02005863#endif
5864
Bram Moolenaar30567352016-08-27 21:25:44 +02005865 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005866 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005867
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005868 return dict;
5869}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005870
5871/*
5872 * "getwininfo()" function
5873 */
5874 static void
5875f_getwininfo(typval_T *argvars, typval_T *rettv)
5876{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005877 tabpage_T *tp;
5878 win_T *wp = NULL, *wparg = NULL;
5879 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005880 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005881
5882 if (rettv_list_alloc(rettv) != OK)
5883 return;
5884
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005885 if (argvars[0].v_type != VAR_UNKNOWN)
5886 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01005887 wparg = win_id2wp(tv_get_number(&argvars[0]));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005888 if (wparg == NULL)
5889 return;
5890 }
5891
5892 /* Collect information about either all the windows across all the tab
5893 * pages or one particular window.
5894 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005895 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005896 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005897 tabnr++;
5898 winnr = 0;
5899 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005900 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005901 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005902 if (wparg != NULL && wp != wparg)
5903 continue;
5904 d = get_win_info(wp, tabnr, winnr);
5905 if (d != NULL)
5906 list_append_dict(rettv->vval.v_list, d);
5907 if (wparg != NULL)
5908 /* found information about a specific window */
5909 return;
5910 }
5911 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005912}
5913
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005914/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005915 * "win_findbuf()" function
5916 */
5917 static void
5918f_win_findbuf(typval_T *argvars, typval_T *rettv)
5919{
5920 if (rettv_list_alloc(rettv) != FAIL)
5921 win_findbuf(argvars, rettv->vval.v_list);
5922}
5923
5924/*
5925 * "win_getid()" function
5926 */
5927 static void
5928f_win_getid(typval_T *argvars, typval_T *rettv)
5929{
5930 rettv->vval.v_number = win_getid(argvars);
5931}
5932
5933/*
5934 * "win_gotoid()" function
5935 */
5936 static void
5937f_win_gotoid(typval_T *argvars, typval_T *rettv)
5938{
5939 rettv->vval.v_number = win_gotoid(argvars);
5940}
5941
5942/*
5943 * "win_id2tabwin()" function
5944 */
5945 static void
5946f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5947{
5948 if (rettv_list_alloc(rettv) != FAIL)
5949 win_id2tabwin(argvars, rettv->vval.v_list);
5950}
5951
5952/*
5953 * "win_id2win()" function
5954 */
5955 static void
5956f_win_id2win(typval_T *argvars, typval_T *rettv)
5957{
5958 rettv->vval.v_number = win_id2win(argvars);
5959}
5960
5961/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005962 * "win_screenpos()" function
5963 */
5964 static void
5965f_win_screenpos(typval_T *argvars, typval_T *rettv)
5966{
5967 win_T *wp;
5968
5969 if (rettv_list_alloc(rettv) == FAIL)
5970 return;
5971
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005972 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005973 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5974 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5975}
5976
5977/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005978 * "getwinpos({timeout})" function
5979 */
5980 static void
5981f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
5982{
5983 int x = -1;
5984 int y = -1;
5985
5986 if (rettv_list_alloc(rettv) == FAIL)
5987 return;
5988#ifdef FEAT_GUI
5989 if (gui.in_use)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01005990 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005991# if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5992 else
5993# endif
5994#endif
5995#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5996 {
5997 varnumber_T timeout = 100;
5998
5999 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006000 timeout = tv_get_number(&argvars[0]);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006001 term_get_winpos(&x, &y, timeout);
6002 }
6003#endif
6004 list_append_number(rettv->vval.v_list, (varnumber_T)x);
6005 list_append_number(rettv->vval.v_list, (varnumber_T)y);
6006}
6007
6008
6009/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006010 * "getwinposx()" function
6011 */
6012 static void
6013f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
6014{
6015 rettv->vval.v_number = -1;
6016#ifdef FEAT_GUI
6017 if (gui.in_use)
6018 {
6019 int x, y;
6020
6021 if (gui_mch_get_winpos(&x, &y) == OK)
6022 rettv->vval.v_number = x;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02006023 return;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006024 }
6025#endif
6026#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
6027 {
6028 int x, y;
6029
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006030 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006031 rettv->vval.v_number = x;
6032 }
6033#endif
6034}
6035
6036/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006037 * "getwinposy()" function
6038 */
6039 static void
6040f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
6041{
6042 rettv->vval.v_number = -1;
6043#ifdef FEAT_GUI
6044 if (gui.in_use)
6045 {
6046 int x, y;
6047
6048 if (gui_mch_get_winpos(&x, &y) == OK)
6049 rettv->vval.v_number = y;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02006050 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006051 }
6052#endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006053#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
6054 {
6055 int x, y;
6056
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006057 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006058 rettv->vval.v_number = y;
6059 }
6060#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006061}
6062
6063/*
6064 * "getwinvar()" function
6065 */
6066 static void
6067f_getwinvar(typval_T *argvars, typval_T *rettv)
6068{
6069 getwinvar(argvars, rettv, 0);
6070}
6071
6072/*
6073 * "glob()" function
6074 */
6075 static void
6076f_glob(typval_T *argvars, typval_T *rettv)
6077{
6078 int options = WILD_SILENT|WILD_USE_NL;
6079 expand_T xpc;
6080 int error = FALSE;
6081
6082 /* When the optional second argument is non-zero, don't remove matches
6083 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6084 rettv->v_type = VAR_STRING;
6085 if (argvars[1].v_type != VAR_UNKNOWN)
6086 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006087 if (tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006088 options |= WILD_KEEP_ALL;
6089 if (argvars[2].v_type != VAR_UNKNOWN)
6090 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006091 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006092 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006093 if (argvars[3].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006094 && tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006095 options |= WILD_ALLLINKS;
6096 }
6097 }
6098 if (!error)
6099 {
6100 ExpandInit(&xpc);
6101 xpc.xp_context = EXPAND_FILES;
6102 if (p_wic)
6103 options += WILD_ICASE;
6104 if (rettv->v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006105 rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006106 NULL, options, WILD_ALL);
6107 else if (rettv_list_alloc(rettv) != FAIL)
6108 {
6109 int i;
6110
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006111 ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006112 NULL, options, WILD_ALL_KEEP);
6113 for (i = 0; i < xpc.xp_numfiles; i++)
6114 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
6115
6116 ExpandCleanup(&xpc);
6117 }
6118 }
6119 else
6120 rettv->vval.v_string = NULL;
6121}
6122
6123/*
6124 * "globpath()" function
6125 */
6126 static void
6127f_globpath(typval_T *argvars, typval_T *rettv)
6128{
6129 int flags = 0;
6130 char_u buf1[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006131 char_u *file = tv_get_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006132 int error = FALSE;
6133 garray_T ga;
6134 int i;
6135
6136 /* When the optional second argument is non-zero, don't remove matches
6137 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6138 rettv->v_type = VAR_STRING;
6139 if (argvars[2].v_type != VAR_UNKNOWN)
6140 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006141 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006142 flags |= WILD_KEEP_ALL;
6143 if (argvars[3].v_type != VAR_UNKNOWN)
6144 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006145 if (tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006146 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006147 if (argvars[4].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006148 && tv_get_number_chk(&argvars[4], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006149 flags |= WILD_ALLLINKS;
6150 }
6151 }
6152 if (file != NULL && !error)
6153 {
6154 ga_init2(&ga, (int)sizeof(char_u *), 10);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006155 globpath(tv_get_string(&argvars[0]), file, &ga, flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006156 if (rettv->v_type == VAR_STRING)
6157 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
6158 else if (rettv_list_alloc(rettv) != FAIL)
6159 for (i = 0; i < ga.ga_len; ++i)
6160 list_append_string(rettv->vval.v_list,
6161 ((char_u **)(ga.ga_data))[i], -1);
6162 ga_clear_strings(&ga);
6163 }
6164 else
6165 rettv->vval.v_string = NULL;
6166}
6167
6168/*
6169 * "glob2regpat()" function
6170 */
6171 static void
6172f_glob2regpat(typval_T *argvars, typval_T *rettv)
6173{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006174 char_u *pat = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006175
6176 rettv->v_type = VAR_STRING;
6177 rettv->vval.v_string = (pat == NULL)
6178 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
6179}
6180
6181/* for VIM_VERSION_ defines */
6182#include "version.h"
6183
6184/*
6185 * "has()" function
6186 */
6187 static void
6188f_has(typval_T *argvars, typval_T *rettv)
6189{
6190 int i;
6191 char_u *name;
6192 int n = FALSE;
6193 static char *(has_list[]) =
6194 {
6195#ifdef AMIGA
6196 "amiga",
6197# ifdef FEAT_ARP
6198 "arp",
6199# endif
6200#endif
6201#ifdef __BEOS__
6202 "beos",
6203#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006204#if defined(BSD) && !defined(MACOS_X)
6205 "bsd",
6206#endif
6207#ifdef hpux
6208 "hpux",
6209#endif
6210#ifdef __linux__
6211 "linux",
6212#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02006213#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01006214 "mac", /* Mac OS X (and, once, Mac OS Classic) */
6215 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02006216# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01006217 "macunix", /* Mac OS X, with the darwin feature */
6218 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02006219# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006220#endif
6221#ifdef __QNX__
6222 "qnx",
6223#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006224#ifdef SUN_SYSTEM
6225 "sun",
6226#else
6227 "moon",
6228#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006229#ifdef UNIX
6230 "unix",
6231#endif
6232#ifdef VMS
6233 "vms",
6234#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006235#ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006236 "win32",
6237#endif
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006238#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006239 "win32unix",
6240#endif
Bram Moolenaar44b443c2019-02-18 22:14:18 +01006241#ifdef _WIN64
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006242 "win64",
6243#endif
6244#ifdef EBCDIC
6245 "ebcdic",
6246#endif
6247#ifndef CASE_INSENSITIVE_FILENAME
6248 "fname_case",
6249#endif
6250#ifdef HAVE_ACL
6251 "acl",
6252#endif
6253#ifdef FEAT_ARABIC
6254 "arabic",
6255#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006256 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006257#ifdef FEAT_AUTOCHDIR
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006258 "autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006259#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006260#ifdef FEAT_AUTOSERVERNAME
6261 "autoservername",
6262#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006263#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006264 "balloon_eval",
Bram Moolenaar4f974752019-02-17 17:44:42 +01006265# ifndef FEAT_GUI_MSWIN /* other GUIs always have multiline balloons */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006266 "balloon_multiline",
6267# endif
6268#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006269#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006270 "balloon_eval_term",
6271#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006272#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6273 "builtin_terms",
6274# ifdef ALL_BUILTIN_TCAPS
6275 "all_builtin_terms",
6276# endif
6277#endif
6278#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01006279 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006280 || defined(FEAT_GUI_MOTIF))
6281 "browsefilter",
6282#endif
6283#ifdef FEAT_BYTEOFF
6284 "byte_offset",
6285#endif
6286#ifdef FEAT_JOB_CHANNEL
6287 "channel",
6288#endif
6289#ifdef FEAT_CINDENT
6290 "cindent",
6291#endif
6292#ifdef FEAT_CLIENTSERVER
6293 "clientserver",
6294#endif
6295#ifdef FEAT_CLIPBOARD
6296 "clipboard",
6297#endif
6298#ifdef FEAT_CMDL_COMPL
6299 "cmdline_compl",
6300#endif
6301#ifdef FEAT_CMDHIST
6302 "cmdline_hist",
6303#endif
6304#ifdef FEAT_COMMENTS
6305 "comments",
6306#endif
6307#ifdef FEAT_CONCEAL
6308 "conceal",
6309#endif
6310#ifdef FEAT_CRYPT
6311 "cryptv",
6312 "crypt-blowfish",
6313 "crypt-blowfish2",
6314#endif
6315#ifdef FEAT_CSCOPE
6316 "cscope",
6317#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006318 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006319#ifdef CURSOR_SHAPE
6320 "cursorshape",
6321#endif
6322#ifdef DEBUG
6323 "debug",
6324#endif
6325#ifdef FEAT_CON_DIALOG
6326 "dialog_con",
6327#endif
6328#ifdef FEAT_GUI_DIALOG
6329 "dialog_gui",
6330#endif
6331#ifdef FEAT_DIFF
6332 "diff",
6333#endif
6334#ifdef FEAT_DIGRAPHS
6335 "digraphs",
6336#endif
6337#ifdef FEAT_DIRECTX
6338 "directx",
6339#endif
6340#ifdef FEAT_DND
6341 "dnd",
6342#endif
6343#ifdef FEAT_EMACS_TAGS
6344 "emacs_tags",
6345#endif
6346 "eval", /* always present, of course! */
6347 "ex_extra", /* graduated feature */
6348#ifdef FEAT_SEARCH_EXTRA
6349 "extra_search",
6350#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006351#ifdef FEAT_SEARCHPATH
6352 "file_in_path",
6353#endif
6354#ifdef FEAT_FILTERPIPE
6355 "filterpipe",
6356#endif
6357#ifdef FEAT_FIND_ID
6358 "find_in_path",
6359#endif
6360#ifdef FEAT_FLOAT
6361 "float",
6362#endif
6363#ifdef FEAT_FOLDING
6364 "folding",
6365#endif
6366#ifdef FEAT_FOOTER
6367 "footer",
6368#endif
6369#if !defined(USE_SYSTEM) && defined(UNIX)
6370 "fork",
6371#endif
6372#ifdef FEAT_GETTEXT
6373 "gettext",
6374#endif
6375#ifdef FEAT_GUI
6376 "gui",
6377#endif
6378#ifdef FEAT_GUI_ATHENA
6379# ifdef FEAT_GUI_NEXTAW
6380 "gui_neXtaw",
6381# else
6382 "gui_athena",
6383# endif
6384#endif
6385#ifdef FEAT_GUI_GTK
6386 "gui_gtk",
6387# ifdef USE_GTK3
6388 "gui_gtk3",
6389# else
6390 "gui_gtk2",
6391# endif
6392#endif
6393#ifdef FEAT_GUI_GNOME
6394 "gui_gnome",
6395#endif
6396#ifdef FEAT_GUI_MAC
6397 "gui_mac",
6398#endif
6399#ifdef FEAT_GUI_MOTIF
6400 "gui_motif",
6401#endif
6402#ifdef FEAT_GUI_PHOTON
6403 "gui_photon",
6404#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006405#ifdef FEAT_GUI_MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006406 "gui_win32",
6407#endif
6408#ifdef FEAT_HANGULIN
6409 "hangul_input",
6410#endif
6411#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6412 "iconv",
6413#endif
6414#ifdef FEAT_INS_EXPAND
6415 "insert_expand",
6416#endif
6417#ifdef FEAT_JOB_CHANNEL
6418 "job",
6419#endif
6420#ifdef FEAT_JUMPLIST
6421 "jumplist",
6422#endif
6423#ifdef FEAT_KEYMAP
6424 "keymap",
6425#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006426 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006427#ifdef FEAT_LANGMAP
6428 "langmap",
6429#endif
6430#ifdef FEAT_LIBCALL
6431 "libcall",
6432#endif
6433#ifdef FEAT_LINEBREAK
6434 "linebreak",
6435#endif
6436#ifdef FEAT_LISP
6437 "lispindent",
6438#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006439 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006440#ifdef FEAT_LOCALMAP
6441 "localmap",
6442#endif
6443#ifdef FEAT_LUA
6444# ifndef DYNAMIC_LUA
6445 "lua",
6446# endif
6447#endif
6448#ifdef FEAT_MENU
6449 "menu",
6450#endif
6451#ifdef FEAT_SESSION
6452 "mksession",
6453#endif
6454#ifdef FEAT_MODIFY_FNAME
6455 "modify_fname",
6456#endif
6457#ifdef FEAT_MOUSE
6458 "mouse",
6459#endif
6460#ifdef FEAT_MOUSESHAPE
6461 "mouseshape",
6462#endif
6463#if defined(UNIX) || defined(VMS)
6464# ifdef FEAT_MOUSE_DEC
6465 "mouse_dec",
6466# endif
6467# ifdef FEAT_MOUSE_GPM
6468 "mouse_gpm",
6469# endif
6470# ifdef FEAT_MOUSE_JSB
6471 "mouse_jsbterm",
6472# endif
6473# ifdef FEAT_MOUSE_NET
6474 "mouse_netterm",
6475# endif
6476# ifdef FEAT_MOUSE_PTERM
6477 "mouse_pterm",
6478# endif
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01006479# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006480 "mouse_sgr",
6481# endif
6482# ifdef FEAT_SYSMOUSE
6483 "mouse_sysmouse",
6484# endif
6485# ifdef FEAT_MOUSE_URXVT
6486 "mouse_urxvt",
6487# endif
6488# ifdef FEAT_MOUSE_XTERM
6489 "mouse_xterm",
6490# endif
6491#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006492 "multi_byte",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006493#ifdef FEAT_MBYTE_IME
6494 "multi_byte_ime",
6495#endif
6496#ifdef FEAT_MULTI_LANG
6497 "multi_lang",
6498#endif
6499#ifdef FEAT_MZSCHEME
6500#ifndef DYNAMIC_MZSCHEME
6501 "mzscheme",
6502#endif
6503#endif
6504#ifdef FEAT_NUM64
6505 "num64",
6506#endif
6507#ifdef FEAT_OLE
6508 "ole",
6509#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006510#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006511 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006512#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006513#ifdef FEAT_PATH_EXTRA
6514 "path_extra",
6515#endif
6516#ifdef FEAT_PERL
6517#ifndef DYNAMIC_PERL
6518 "perl",
6519#endif
6520#endif
6521#ifdef FEAT_PERSISTENT_UNDO
6522 "persistent_undo",
6523#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006524#if defined(FEAT_PYTHON)
6525 "python_compiled",
6526# if defined(DYNAMIC_PYTHON)
6527 "python_dynamic",
6528# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006529 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006530 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006531# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006532#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006533#if defined(FEAT_PYTHON3)
6534 "python3_compiled",
6535# if defined(DYNAMIC_PYTHON3)
6536 "python3_dynamic",
6537# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006538 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006539 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006540# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006541#endif
6542#ifdef FEAT_POSTSCRIPT
6543 "postscript",
6544#endif
6545#ifdef FEAT_PRINTER
6546 "printer",
6547#endif
6548#ifdef FEAT_PROFILE
6549 "profile",
6550#endif
6551#ifdef FEAT_RELTIME
6552 "reltime",
6553#endif
6554#ifdef FEAT_QUICKFIX
6555 "quickfix",
6556#endif
6557#ifdef FEAT_RIGHTLEFT
6558 "rightleft",
6559#endif
6560#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6561 "ruby",
6562#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006563 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006564#ifdef FEAT_CMDL_INFO
6565 "showcmd",
6566 "cmdline_info",
6567#endif
6568#ifdef FEAT_SIGNS
6569 "signs",
6570#endif
6571#ifdef FEAT_SMARTINDENT
6572 "smartindent",
6573#endif
6574#ifdef STARTUPTIME
6575 "startuptime",
6576#endif
6577#ifdef FEAT_STL_OPT
6578 "statusline",
6579#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006580#ifdef FEAT_NETBEANS_INTG
6581 "netbeans_intg",
6582#endif
6583#ifdef FEAT_SPELL
6584 "spell",
6585#endif
6586#ifdef FEAT_SYN_HL
6587 "syntax",
6588#endif
6589#if defined(USE_SYSTEM) || !defined(UNIX)
6590 "system",
6591#endif
6592#ifdef FEAT_TAG_BINS
6593 "tag_binary",
6594#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006595#ifdef FEAT_TCL
6596# ifndef DYNAMIC_TCL
6597 "tcl",
6598# endif
6599#endif
6600#ifdef FEAT_TERMGUICOLORS
6601 "termguicolors",
6602#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006603#if defined(FEAT_TERMINAL) && !defined(MSWIN)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006604 "terminal",
6605#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006606#ifdef TERMINFO
6607 "terminfo",
6608#endif
6609#ifdef FEAT_TERMRESPONSE
6610 "termresponse",
6611#endif
6612#ifdef FEAT_TEXTOBJ
6613 "textobjects",
6614#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006615#ifdef FEAT_TEXT_PROP
6616 "textprop",
6617#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006618#ifdef HAVE_TGETENT
6619 "tgetent",
6620#endif
6621#ifdef FEAT_TIMERS
6622 "timers",
6623#endif
6624#ifdef FEAT_TITLE
6625 "title",
6626#endif
6627#ifdef FEAT_TOOLBAR
6628 "toolbar",
6629#endif
6630#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6631 "unnamedplus",
6632#endif
6633#ifdef FEAT_USR_CMDS
6634 "user-commands", /* was accidentally included in 5.4 */
6635 "user_commands",
6636#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006637#ifdef FEAT_VARTABS
6638 "vartabs",
6639#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006640 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006641#ifdef FEAT_VIMINFO
6642 "viminfo",
6643#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006644 "vimscript-1",
6645 "vimscript-2",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006646 "virtualedit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006647 "visual",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006648 "visualextra",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006649 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006650#ifdef FEAT_VTP
6651 "vtp",
6652#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006653#ifdef FEAT_WILDIGN
6654 "wildignore",
6655#endif
6656#ifdef FEAT_WILDMENU
6657 "wildmenu",
6658#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006659 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006660#ifdef FEAT_WAK
6661 "winaltkeys",
6662#endif
6663#ifdef FEAT_WRITEBACKUP
6664 "writebackup",
6665#endif
6666#ifdef FEAT_XIM
6667 "xim",
6668#endif
6669#ifdef FEAT_XFONTSET
6670 "xfontset",
6671#endif
6672#ifdef FEAT_XPM_W32
6673 "xpm",
6674 "xpm_w32", /* for backward compatibility */
6675#else
6676# if defined(HAVE_XPM)
6677 "xpm",
6678# endif
6679#endif
6680#ifdef USE_XSMP
6681 "xsmp",
6682#endif
6683#ifdef USE_XSMP_INTERACT
6684 "xsmp_interact",
6685#endif
6686#ifdef FEAT_XCLIPBOARD
6687 "xterm_clipboard",
6688#endif
6689#ifdef FEAT_XTERM_SAVE
6690 "xterm_save",
6691#endif
6692#if defined(UNIX) && defined(FEAT_X11)
6693 "X11",
6694#endif
6695 NULL
6696 };
6697
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006698 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006699 for (i = 0; has_list[i] != NULL; ++i)
6700 if (STRICMP(name, has_list[i]) == 0)
6701 {
6702 n = TRUE;
6703 break;
6704 }
6705
6706 if (n == FALSE)
6707 {
6708 if (STRNICMP(name, "patch", 5) == 0)
6709 {
6710 if (name[5] == '-'
6711 && STRLEN(name) >= 11
6712 && vim_isdigit(name[6])
6713 && vim_isdigit(name[8])
6714 && vim_isdigit(name[10]))
6715 {
6716 int major = atoi((char *)name + 6);
6717 int minor = atoi((char *)name + 8);
6718
6719 /* Expect "patch-9.9.01234". */
6720 n = (major < VIM_VERSION_MAJOR
6721 || (major == VIM_VERSION_MAJOR
6722 && (minor < VIM_VERSION_MINOR
6723 || (minor == VIM_VERSION_MINOR
6724 && has_patch(atoi((char *)name + 10))))));
6725 }
6726 else
6727 n = has_patch(atoi((char *)name + 5));
6728 }
6729 else if (STRICMP(name, "vim_starting") == 0)
6730 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006731 else if (STRICMP(name, "ttyin") == 0)
6732 n = mch_input_isatty();
6733 else if (STRICMP(name, "ttyout") == 0)
6734 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006735 else if (STRICMP(name, "multi_byte_encoding") == 0)
6736 n = has_mbyte;
Bram Moolenaar4f974752019-02-17 17:44:42 +01006737#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006738 else if (STRICMP(name, "balloon_multiline") == 0)
6739 n = multiline_balloon_available();
6740#endif
6741#ifdef DYNAMIC_TCL
6742 else if (STRICMP(name, "tcl") == 0)
6743 n = tcl_enabled(FALSE);
6744#endif
6745#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6746 else if (STRICMP(name, "iconv") == 0)
6747 n = iconv_enabled(FALSE);
6748#endif
6749#ifdef DYNAMIC_LUA
6750 else if (STRICMP(name, "lua") == 0)
6751 n = lua_enabled(FALSE);
6752#endif
6753#ifdef DYNAMIC_MZSCHEME
6754 else if (STRICMP(name, "mzscheme") == 0)
6755 n = mzscheme_enabled(FALSE);
6756#endif
6757#ifdef DYNAMIC_RUBY
6758 else if (STRICMP(name, "ruby") == 0)
6759 n = ruby_enabled(FALSE);
6760#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006761#ifdef DYNAMIC_PYTHON
6762 else if (STRICMP(name, "python") == 0)
6763 n = python_enabled(FALSE);
6764#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006765#ifdef DYNAMIC_PYTHON3
6766 else if (STRICMP(name, "python3") == 0)
6767 n = python3_enabled(FALSE);
6768#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006769#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6770 else if (STRICMP(name, "pythonx") == 0)
6771 {
6772# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6773 if (p_pyx == 0)
6774 n = python3_enabled(FALSE) || python_enabled(FALSE);
6775 else if (p_pyx == 3)
6776 n = python3_enabled(FALSE);
6777 else if (p_pyx == 2)
6778 n = python_enabled(FALSE);
6779# elif defined(DYNAMIC_PYTHON)
6780 n = python_enabled(FALSE);
6781# elif defined(DYNAMIC_PYTHON3)
6782 n = python3_enabled(FALSE);
6783# endif
6784 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006785#endif
6786#ifdef DYNAMIC_PERL
6787 else if (STRICMP(name, "perl") == 0)
6788 n = perl_enabled(FALSE);
6789#endif
6790#ifdef FEAT_GUI
6791 else if (STRICMP(name, "gui_running") == 0)
6792 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006793# ifdef FEAT_BROWSE
6794 else if (STRICMP(name, "browse") == 0)
6795 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6796# endif
6797#endif
6798#ifdef FEAT_SYN_HL
6799 else if (STRICMP(name, "syntax_items") == 0)
6800 n = syntax_present(curwin);
6801#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006802#ifdef FEAT_VTP
6803 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02006804 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006805#endif
6806#ifdef FEAT_NETBEANS_INTG
6807 else if (STRICMP(name, "netbeans_enabled") == 0)
6808 n = netbeans_active();
6809#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006810#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02006811 else if (STRICMP(name, "terminal") == 0)
6812 n = terminal_enabled();
6813#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006814#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006815 else if (STRICMP(name, "conpty") == 0)
6816 n = use_conpty();
6817#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006818 }
6819
6820 rettv->vval.v_number = n;
6821}
6822
6823/*
6824 * "has_key()" function
6825 */
6826 static void
6827f_has_key(typval_T *argvars, typval_T *rettv)
6828{
6829 if (argvars[0].v_type != VAR_DICT)
6830 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006831 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006832 return;
6833 }
6834 if (argvars[0].vval.v_dict == NULL)
6835 return;
6836
6837 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006838 tv_get_string(&argvars[1]), -1) != NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006839}
6840
6841/*
6842 * "haslocaldir()" function
6843 */
6844 static void
6845f_haslocaldir(typval_T *argvars, typval_T *rettv)
6846{
6847 win_T *wp = NULL;
6848
6849 wp = find_tabwin(&argvars[0], &argvars[1]);
6850 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6851}
6852
6853/*
6854 * "hasmapto()" function
6855 */
6856 static void
6857f_hasmapto(typval_T *argvars, typval_T *rettv)
6858{
6859 char_u *name;
6860 char_u *mode;
6861 char_u buf[NUMBUFLEN];
6862 int abbr = FALSE;
6863
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006864 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006865 if (argvars[1].v_type == VAR_UNKNOWN)
6866 mode = (char_u *)"nvo";
6867 else
6868 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006869 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006870 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006871 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006872 }
6873
6874 if (map_to_exists(name, mode, abbr))
6875 rettv->vval.v_number = TRUE;
6876 else
6877 rettv->vval.v_number = FALSE;
6878}
6879
6880/*
6881 * "histadd()" function
6882 */
6883 static void
6884f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6885{
6886#ifdef FEAT_CMDHIST
6887 int histype;
6888 char_u *str;
6889 char_u buf[NUMBUFLEN];
6890#endif
6891
6892 rettv->vval.v_number = FALSE;
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006893 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006894 return;
6895#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006896 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006897 histype = str != NULL ? get_histtype(str) : -1;
6898 if (histype >= 0)
6899 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006900 str = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006901 if (*str != NUL)
6902 {
6903 init_history();
6904 add_to_history(histype, str, FALSE, NUL);
6905 rettv->vval.v_number = TRUE;
6906 return;
6907 }
6908 }
6909#endif
6910}
6911
6912/*
6913 * "histdel()" function
6914 */
6915 static void
6916f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6917{
6918#ifdef FEAT_CMDHIST
6919 int n;
6920 char_u buf[NUMBUFLEN];
6921 char_u *str;
6922
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006923 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006924 if (str == NULL)
6925 n = 0;
6926 else if (argvars[1].v_type == VAR_UNKNOWN)
6927 /* only one argument: clear entire history */
6928 n = clr_history(get_histtype(str));
6929 else if (argvars[1].v_type == VAR_NUMBER)
6930 /* index given: remove that entry */
6931 n = del_history_idx(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006932 (int)tv_get_number(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006933 else
6934 /* string given: remove all matching entries */
6935 n = del_history_entry(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006936 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006937 rettv->vval.v_number = n;
6938#endif
6939}
6940
6941/*
6942 * "histget()" function
6943 */
6944 static void
6945f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6946{
6947#ifdef FEAT_CMDHIST
6948 int type;
6949 int idx;
6950 char_u *str;
6951
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006952 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006953 if (str == NULL)
6954 rettv->vval.v_string = NULL;
6955 else
6956 {
6957 type = get_histtype(str);
6958 if (argvars[1].v_type == VAR_UNKNOWN)
6959 idx = get_history_idx(type);
6960 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006961 idx = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006962 /* -1 on type error */
6963 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6964 }
6965#else
6966 rettv->vval.v_string = NULL;
6967#endif
6968 rettv->v_type = VAR_STRING;
6969}
6970
6971/*
6972 * "histnr()" function
6973 */
6974 static void
6975f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6976{
6977 int i;
6978
6979#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006980 char_u *history = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006981
6982 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6983 if (i >= HIST_CMD && i < HIST_COUNT)
6984 i = get_history_idx(i);
6985 else
6986#endif
6987 i = -1;
6988 rettv->vval.v_number = i;
6989}
6990
6991/*
6992 * "highlightID(name)" function
6993 */
6994 static void
6995f_hlID(typval_T *argvars, typval_T *rettv)
6996{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006997 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006998}
6999
7000/*
7001 * "highlight_exists()" function
7002 */
7003 static void
7004f_hlexists(typval_T *argvars, typval_T *rettv)
7005{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007006 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007007}
7008
7009/*
7010 * "hostname()" function
7011 */
7012 static void
7013f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
7014{
7015 char_u hostname[256];
7016
7017 mch_get_host_name(hostname, 256);
7018 rettv->v_type = VAR_STRING;
7019 rettv->vval.v_string = vim_strsave(hostname);
7020}
7021
7022/*
7023 * iconv() function
7024 */
7025 static void
7026f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
7027{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007028 char_u buf1[NUMBUFLEN];
7029 char_u buf2[NUMBUFLEN];
7030 char_u *from, *to, *str;
7031 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007032
7033 rettv->v_type = VAR_STRING;
7034 rettv->vval.v_string = NULL;
7035
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007036 str = tv_get_string(&argvars[0]);
7037 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
7038 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007039 vimconv.vc_type = CONV_NONE;
7040 convert_setup(&vimconv, from, to);
7041
7042 /* If the encodings are equal, no conversion needed. */
7043 if (vimconv.vc_type == CONV_NONE)
7044 rettv->vval.v_string = vim_strsave(str);
7045 else
7046 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
7047
7048 convert_setup(&vimconv, NULL, NULL);
7049 vim_free(from);
7050 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007051}
7052
7053/*
7054 * "indent()" function
7055 */
7056 static void
7057f_indent(typval_T *argvars, typval_T *rettv)
7058{
7059 linenr_T lnum;
7060
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007061 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007062 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7063 rettv->vval.v_number = get_indent_lnum(lnum);
7064 else
7065 rettv->vval.v_number = -1;
7066}
7067
7068/*
7069 * "index()" function
7070 */
7071 static void
7072f_index(typval_T *argvars, typval_T *rettv)
7073{
7074 list_T *l;
7075 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007076 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007077 long idx = 0;
7078 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007079 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007080
7081 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007082 if (argvars[0].v_type == VAR_BLOB)
7083 {
7084 typval_T tv;
7085 int start = 0;
7086
7087 if (argvars[2].v_type != VAR_UNKNOWN)
7088 {
7089 start = tv_get_number_chk(&argvars[2], &error);
7090 if (error)
7091 return;
7092 }
7093 b = argvars[0].vval.v_blob;
7094 if (b == NULL)
7095 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01007096 if (start < 0)
7097 {
7098 start = blob_len(b) + start;
7099 if (start < 0)
7100 start = 0;
7101 }
7102
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007103 for (idx = start; idx < blob_len(b); ++idx)
7104 {
7105 tv.v_type = VAR_NUMBER;
7106 tv.vval.v_number = blob_get(b, idx);
7107 if (tv_equal(&tv, &argvars[1], ic, FALSE))
7108 {
7109 rettv->vval.v_number = idx;
7110 return;
7111 }
7112 }
7113 return;
7114 }
7115 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007116 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007117 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007118 return;
7119 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007120
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007121 l = argvars[0].vval.v_list;
7122 if (l != NULL)
7123 {
7124 item = l->lv_first;
7125 if (argvars[2].v_type != VAR_UNKNOWN)
7126 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007127 /* Start at specified item. Use the cached index that list_find()
7128 * sets, so that a negative number also works. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007129 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007130 idx = l->lv_idx;
7131 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007132 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007133 if (error)
7134 item = NULL;
7135 }
7136
7137 for ( ; item != NULL; item = item->li_next, ++idx)
7138 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
7139 {
7140 rettv->vval.v_number = idx;
7141 break;
7142 }
7143 }
7144}
7145
7146static int inputsecret_flag = 0;
7147
7148/*
7149 * "input()" function
7150 * Also handles inputsecret() when inputsecret is set.
7151 */
7152 static void
7153f_input(typval_T *argvars, typval_T *rettv)
7154{
7155 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
7156}
7157
7158/*
7159 * "inputdialog()" function
7160 */
7161 static void
7162f_inputdialog(typval_T *argvars, typval_T *rettv)
7163{
7164#if defined(FEAT_GUI_TEXTDIALOG)
7165 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
7166 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
7167 {
7168 char_u *message;
7169 char_u buf[NUMBUFLEN];
7170 char_u *defstr = (char_u *)"";
7171
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007172 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007173 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007174 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007175 vim_strncpy(IObuff, defstr, IOSIZE - 1);
7176 else
7177 IObuff[0] = NUL;
7178 if (message != NULL && defstr != NULL
7179 && do_dialog(VIM_QUESTION, NULL, message,
7180 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
7181 rettv->vval.v_string = vim_strsave(IObuff);
7182 else
7183 {
7184 if (message != NULL && defstr != NULL
7185 && argvars[1].v_type != VAR_UNKNOWN
7186 && argvars[2].v_type != VAR_UNKNOWN)
7187 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007188 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007189 else
7190 rettv->vval.v_string = NULL;
7191 }
7192 rettv->v_type = VAR_STRING;
7193 }
7194 else
7195#endif
7196 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
7197}
7198
7199/*
7200 * "inputlist()" function
7201 */
7202 static void
7203f_inputlist(typval_T *argvars, typval_T *rettv)
7204{
7205 listitem_T *li;
7206 int selected;
7207 int mouse_used;
7208
7209#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007210 /* While starting up, there is no place to enter text. When running tests
7211 * with --not-a-term we assume feedkeys() will be used. */
7212 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007213 return;
7214#endif
7215 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
7216 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007217 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007218 return;
7219 }
7220
7221 msg_start();
7222 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
7223 lines_left = Rows; /* avoid more prompt */
7224 msg_scroll = TRUE;
7225 msg_clr_eos();
7226
7227 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
7228 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007229 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007230 msg_putchar('\n');
7231 }
7232
7233 /* Ask for choice. */
7234 selected = prompt_for_number(&mouse_used);
7235 if (mouse_used)
7236 selected -= lines_left;
7237
7238 rettv->vval.v_number = selected;
7239}
7240
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007241static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7242
7243/*
7244 * "inputrestore()" function
7245 */
7246 static void
7247f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7248{
7249 if (ga_userinput.ga_len > 0)
7250 {
7251 --ga_userinput.ga_len;
7252 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7253 + ga_userinput.ga_len);
7254 /* default return is zero == OK */
7255 }
7256 else if (p_verbose > 1)
7257 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007258 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007259 rettv->vval.v_number = 1; /* Failed */
7260 }
7261}
7262
7263/*
7264 * "inputsave()" function
7265 */
7266 static void
7267f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7268{
7269 /* Add an entry to the stack of typeahead storage. */
7270 if (ga_grow(&ga_userinput, 1) == OK)
7271 {
7272 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7273 + ga_userinput.ga_len);
7274 ++ga_userinput.ga_len;
7275 /* default return is zero == OK */
7276 }
7277 else
7278 rettv->vval.v_number = 1; /* Failed */
7279}
7280
7281/*
7282 * "inputsecret()" function
7283 */
7284 static void
7285f_inputsecret(typval_T *argvars, typval_T *rettv)
7286{
7287 ++cmdline_star;
7288 ++inputsecret_flag;
7289 f_input(argvars, rettv);
7290 --cmdline_star;
7291 --inputsecret_flag;
7292}
7293
7294/*
7295 * "insert()" function
7296 */
7297 static void
7298f_insert(typval_T *argvars, typval_T *rettv)
7299{
7300 long before = 0;
7301 listitem_T *item;
7302 list_T *l;
7303 int error = FALSE;
7304
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007305 if (argvars[0].v_type == VAR_BLOB)
7306 {
7307 int val, len;
7308 char_u *p;
7309
7310 len = blob_len(argvars[0].vval.v_blob);
7311 if (argvars[2].v_type != VAR_UNKNOWN)
7312 {
7313 before = (long)tv_get_number_chk(&argvars[2], &error);
7314 if (error)
7315 return; // type error; errmsg already given
7316 if (before < 0 || before > len)
7317 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007318 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007319 return;
7320 }
7321 }
7322 val = tv_get_number_chk(&argvars[1], &error);
7323 if (error)
7324 return;
7325 if (val < 0 || val > 255)
7326 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007327 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007328 return;
7329 }
7330
7331 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
7332 return;
7333 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
7334 mch_memmove(p + before + 1, p + before, (size_t)len - before);
7335 *(p + before) = val;
7336 ++argvars[0].vval.v_blob->bv_ga.ga_len;
7337
7338 copy_tv(&argvars[0], rettv);
7339 }
7340 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007341 semsg(_(e_listblobarg), "insert()");
Bram Moolenaar05c00c02019-02-11 22:00:11 +01007342 else if ((l = argvars[0].vval.v_list) != NULL
7343 && !var_check_lock(l->lv_lock,
7344 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007345 {
7346 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007347 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007348 if (error)
7349 return; /* type error; errmsg already given */
7350
7351 if (before == l->lv_len)
7352 item = NULL;
7353 else
7354 {
7355 item = list_find(l, before);
7356 if (item == NULL)
7357 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007358 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007359 l = NULL;
7360 }
7361 }
7362 if (l != NULL)
7363 {
7364 list_insert_tv(l, &argvars[1], item);
7365 copy_tv(&argvars[0], rettv);
7366 }
7367 }
7368}
7369
7370/*
7371 * "invert(expr)" function
7372 */
7373 static void
7374f_invert(typval_T *argvars, typval_T *rettv)
7375{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007376 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007377}
7378
7379/*
7380 * "isdirectory()" function
7381 */
7382 static void
7383f_isdirectory(typval_T *argvars, typval_T *rettv)
7384{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007385 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007386}
7387
7388/*
7389 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7390 * or it refers to a List or Dictionary that is locked.
7391 */
7392 static int
7393tv_islocked(typval_T *tv)
7394{
7395 return (tv->v_lock & VAR_LOCKED)
7396 || (tv->v_type == VAR_LIST
7397 && tv->vval.v_list != NULL
7398 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7399 || (tv->v_type == VAR_DICT
7400 && tv->vval.v_dict != NULL
7401 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7402}
7403
7404/*
7405 * "islocked()" function
7406 */
7407 static void
7408f_islocked(typval_T *argvars, typval_T *rettv)
7409{
7410 lval_T lv;
7411 char_u *end;
7412 dictitem_T *di;
7413
7414 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007415 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007416 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007417 if (end != NULL && lv.ll_name != NULL)
7418 {
7419 if (*end != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007420 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007421 else
7422 {
7423 if (lv.ll_tv == NULL)
7424 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007425 di = find_var(lv.ll_name, NULL, TRUE);
7426 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007427 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007428 /* Consider a variable locked when:
7429 * 1. the variable itself is locked
7430 * 2. the value of the variable is locked.
7431 * 3. the List or Dict value is locked.
7432 */
7433 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7434 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007435 }
7436 }
7437 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007438 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007439 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007440 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007441 else if (lv.ll_list != NULL)
7442 /* List item. */
7443 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7444 else
7445 /* Dictionary item. */
7446 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7447 }
7448 }
7449
7450 clear_lval(&lv);
7451}
7452
7453#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7454/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02007455 * "isinf()" function
7456 */
7457 static void
7458f_isinf(typval_T *argvars, typval_T *rettv)
7459{
7460 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
7461 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
7462}
7463
7464/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007465 * "isnan()" function
7466 */
7467 static void
7468f_isnan(typval_T *argvars, typval_T *rettv)
7469{
7470 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7471 && isnan(argvars[0].vval.v_float);
7472}
7473#endif
7474
7475/*
7476 * "items(dict)" function
7477 */
7478 static void
7479f_items(typval_T *argvars, typval_T *rettv)
7480{
7481 dict_list(argvars, rettv, 2);
7482}
7483
7484#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
7485/*
7486 * Get the job from the argument.
7487 * Returns NULL if the job is invalid.
7488 */
7489 static job_T *
7490get_job_arg(typval_T *tv)
7491{
7492 job_T *job;
7493
7494 if (tv->v_type != VAR_JOB)
7495 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007496 semsg(_(e_invarg2), tv_get_string(tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007497 return NULL;
7498 }
7499 job = tv->vval.v_job;
7500
7501 if (job == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007502 emsg(_("E916: not a valid job"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007503 return job;
7504}
7505
7506/*
7507 * "job_getchannel()" function
7508 */
7509 static void
7510f_job_getchannel(typval_T *argvars, typval_T *rettv)
7511{
7512 job_T *job = get_job_arg(&argvars[0]);
7513
7514 if (job != NULL)
7515 {
7516 rettv->v_type = VAR_CHANNEL;
7517 rettv->vval.v_channel = job->jv_channel;
7518 if (job->jv_channel != NULL)
7519 ++job->jv_channel->ch_refcount;
7520 }
7521}
7522
7523/*
7524 * "job_info()" function
7525 */
7526 static void
7527f_job_info(typval_T *argvars, typval_T *rettv)
7528{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007529 if (argvars[0].v_type != VAR_UNKNOWN)
7530 {
7531 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007532
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007533 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7534 job_info(job, rettv->vval.v_dict);
7535 }
7536 else if (rettv_list_alloc(rettv) == OK)
7537 job_info_all(rettv->vval.v_list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007538}
7539
7540/*
7541 * "job_setoptions()" function
7542 */
7543 static void
7544f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7545{
7546 job_T *job = get_job_arg(&argvars[0]);
7547 jobopt_T opt;
7548
7549 if (job == NULL)
7550 return;
7551 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007552 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007553 job_set_options(job, &opt);
7554 free_job_options(&opt);
7555}
7556
7557/*
7558 * "job_start()" function
7559 */
7560 static void
7561f_job_start(typval_T *argvars, typval_T *rettv)
7562{
7563 rettv->v_type = VAR_JOB;
7564 if (check_restricted() || check_secure())
7565 return;
Bram Moolenaar493359e2018-06-12 20:25:52 +02007566 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007567}
7568
7569/*
7570 * "job_status()" function
7571 */
7572 static void
7573f_job_status(typval_T *argvars, typval_T *rettv)
7574{
7575 job_T *job = get_job_arg(&argvars[0]);
7576
7577 if (job != NULL)
7578 {
7579 rettv->v_type = VAR_STRING;
7580 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7581 }
7582}
7583
7584/*
7585 * "job_stop()" function
7586 */
7587 static void
7588f_job_stop(typval_T *argvars, typval_T *rettv)
7589{
7590 job_T *job = get_job_arg(&argvars[0]);
7591
7592 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007593 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007594}
7595#endif
7596
7597/*
7598 * "join()" function
7599 */
7600 static void
7601f_join(typval_T *argvars, typval_T *rettv)
7602{
7603 garray_T ga;
7604 char_u *sep;
7605
7606 if (argvars[0].v_type != VAR_LIST)
7607 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007608 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007609 return;
7610 }
7611 if (argvars[0].vval.v_list == NULL)
7612 return;
7613 if (argvars[1].v_type == VAR_UNKNOWN)
7614 sep = (char_u *)" ";
7615 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007616 sep = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007617
7618 rettv->v_type = VAR_STRING;
7619
7620 if (sep != NULL)
7621 {
7622 ga_init2(&ga, (int)sizeof(char), 80);
7623 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7624 ga_append(&ga, NUL);
7625 rettv->vval.v_string = (char_u *)ga.ga_data;
7626 }
7627 else
7628 rettv->vval.v_string = NULL;
7629}
7630
7631/*
7632 * "js_decode()" function
7633 */
7634 static void
7635f_js_decode(typval_T *argvars, typval_T *rettv)
7636{
7637 js_read_T reader;
7638
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007639 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007640 reader.js_fill = NULL;
7641 reader.js_used = 0;
7642 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007643 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007644}
7645
7646/*
7647 * "js_encode()" function
7648 */
7649 static void
7650f_js_encode(typval_T *argvars, typval_T *rettv)
7651{
7652 rettv->v_type = VAR_STRING;
7653 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7654}
7655
7656/*
7657 * "json_decode()" function
7658 */
7659 static void
7660f_json_decode(typval_T *argvars, typval_T *rettv)
7661{
7662 js_read_T reader;
7663
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007664 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007665 reader.js_fill = NULL;
7666 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007667 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007668}
7669
7670/*
7671 * "json_encode()" function
7672 */
7673 static void
7674f_json_encode(typval_T *argvars, typval_T *rettv)
7675{
7676 rettv->v_type = VAR_STRING;
7677 rettv->vval.v_string = json_encode(&argvars[0], 0);
7678}
7679
7680/*
7681 * "keys()" function
7682 */
7683 static void
7684f_keys(typval_T *argvars, typval_T *rettv)
7685{
7686 dict_list(argvars, rettv, 0);
7687}
7688
7689/*
7690 * "last_buffer_nr()" function.
7691 */
7692 static void
7693f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7694{
7695 int n = 0;
7696 buf_T *buf;
7697
Bram Moolenaar29323592016-07-24 22:04:11 +02007698 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007699 if (n < buf->b_fnum)
7700 n = buf->b_fnum;
7701
7702 rettv->vval.v_number = n;
7703}
7704
7705/*
7706 * "len()" function
7707 */
7708 static void
7709f_len(typval_T *argvars, typval_T *rettv)
7710{
7711 switch (argvars[0].v_type)
7712 {
7713 case VAR_STRING:
7714 case VAR_NUMBER:
7715 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007716 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007717 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007718 case VAR_BLOB:
7719 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
7720 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007721 case VAR_LIST:
7722 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7723 break;
7724 case VAR_DICT:
7725 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7726 break;
7727 case VAR_UNKNOWN:
7728 case VAR_SPECIAL:
7729 case VAR_FLOAT:
7730 case VAR_FUNC:
7731 case VAR_PARTIAL:
7732 case VAR_JOB:
7733 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007734 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007735 break;
7736 }
7737}
7738
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007739 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007740libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007741{
7742#ifdef FEAT_LIBCALL
7743 char_u *string_in;
7744 char_u **string_result;
7745 int nr_result;
7746#endif
7747
7748 rettv->v_type = type;
7749 if (type != VAR_NUMBER)
7750 rettv->vval.v_string = NULL;
7751
7752 if (check_restricted() || check_secure())
7753 return;
7754
7755#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007756 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007757 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7758 {
7759 string_in = NULL;
7760 if (argvars[2].v_type == VAR_STRING)
7761 string_in = argvars[2].vval.v_string;
7762 if (type == VAR_NUMBER)
7763 string_result = NULL;
7764 else
7765 string_result = &rettv->vval.v_string;
7766 if (mch_libcall(argvars[0].vval.v_string,
7767 argvars[1].vval.v_string,
7768 string_in,
7769 argvars[2].vval.v_number,
7770 string_result,
7771 &nr_result) == OK
7772 && type == VAR_NUMBER)
7773 rettv->vval.v_number = nr_result;
7774 }
7775#endif
7776}
7777
7778/*
7779 * "libcall()" function
7780 */
7781 static void
7782f_libcall(typval_T *argvars, typval_T *rettv)
7783{
7784 libcall_common(argvars, rettv, VAR_STRING);
7785}
7786
7787/*
7788 * "libcallnr()" function
7789 */
7790 static void
7791f_libcallnr(typval_T *argvars, typval_T *rettv)
7792{
7793 libcall_common(argvars, rettv, VAR_NUMBER);
7794}
7795
7796/*
7797 * "line(string)" function
7798 */
7799 static void
7800f_line(typval_T *argvars, typval_T *rettv)
7801{
7802 linenr_T lnum = 0;
7803 pos_T *fp;
7804 int fnum;
7805
7806 fp = var2fpos(&argvars[0], TRUE, &fnum);
7807 if (fp != NULL)
7808 lnum = fp->lnum;
7809 rettv->vval.v_number = lnum;
7810}
7811
7812/*
7813 * "line2byte(lnum)" function
7814 */
7815 static void
7816f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7817{
7818#ifndef FEAT_BYTEOFF
7819 rettv->vval.v_number = -1;
7820#else
7821 linenr_T lnum;
7822
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007823 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007824 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7825 rettv->vval.v_number = -1;
7826 else
7827 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7828 if (rettv->vval.v_number >= 0)
7829 ++rettv->vval.v_number;
7830#endif
7831}
7832
7833/*
7834 * "lispindent(lnum)" function
7835 */
7836 static void
7837f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7838{
7839#ifdef FEAT_LISP
7840 pos_T pos;
7841 linenr_T lnum;
7842
7843 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007844 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007845 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7846 {
7847 curwin->w_cursor.lnum = lnum;
7848 rettv->vval.v_number = get_lisp_indent();
7849 curwin->w_cursor = pos;
7850 }
7851 else
7852#endif
7853 rettv->vval.v_number = -1;
7854}
7855
7856/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02007857 * "list2str()" function
7858 */
7859 static void
7860f_list2str(typval_T *argvars, typval_T *rettv)
7861{
7862 list_T *l;
7863 listitem_T *li;
7864 garray_T ga;
7865 int utf8 = FALSE;
7866
7867 rettv->v_type = VAR_STRING;
7868 rettv->vval.v_string = NULL;
7869 if (argvars[0].v_type != VAR_LIST)
7870 {
7871 emsg(_(e_invarg));
7872 return;
7873 }
7874
7875 l = argvars[0].vval.v_list;
7876 if (l == NULL)
7877 return; // empty list results in empty string
7878
7879 if (argvars[1].v_type != VAR_UNKNOWN)
7880 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
7881
7882 ga_init2(&ga, 1, 80);
7883 if (has_mbyte || utf8)
7884 {
7885 char_u buf[MB_MAXBYTES + 1];
7886 int (*char2bytes)(int, char_u *);
7887
7888 if (utf8 || enc_utf8)
7889 char2bytes = utf_char2bytes;
7890 else
7891 char2bytes = mb_char2bytes;
7892
7893 for (li = l->lv_first; li != NULL; li = li->li_next)
7894 {
7895 buf[(*char2bytes)(tv_get_number(&li->li_tv), buf)] = NUL;
7896 ga_concat(&ga, buf);
7897 }
7898 ga_append(&ga, NUL);
7899 }
7900 else if (ga_grow(&ga, list_len(l) + 1) == OK)
7901 {
7902 for (li = l->lv_first; li != NULL; li = li->li_next)
7903 ga_append(&ga, tv_get_number(&li->li_tv));
7904 ga_append(&ga, NUL);
7905 }
7906
7907 rettv->v_type = VAR_STRING;
7908 rettv->vval.v_string = ga.ga_data;
7909}
7910
7911/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007912 * "localtime()" function
7913 */
7914 static void
7915f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7916{
7917 rettv->vval.v_number = (varnumber_T)time(NULL);
7918}
7919
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007920 static void
7921get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7922{
7923 char_u *keys;
7924 char_u *which;
7925 char_u buf[NUMBUFLEN];
7926 char_u *keys_buf = NULL;
7927 char_u *rhs;
7928 int mode;
7929 int abbr = FALSE;
7930 int get_dict = FALSE;
7931 mapblock_T *mp;
7932 int buffer_local;
7933
7934 /* return empty string for failure */
7935 rettv->v_type = VAR_STRING;
7936 rettv->vval.v_string = NULL;
7937
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007938 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007939 if (*keys == NUL)
7940 return;
7941
7942 if (argvars[1].v_type != VAR_UNKNOWN)
7943 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007944 which = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007945 if (argvars[2].v_type != VAR_UNKNOWN)
7946 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007947 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007948 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007949 get_dict = (int)tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007950 }
7951 }
7952 else
7953 which = (char_u *)"";
7954 if (which == NULL)
7955 return;
7956
7957 mode = get_map_mode(&which, 0);
7958
7959 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7960 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7961 vim_free(keys_buf);
7962
7963 if (!get_dict)
7964 {
7965 /* Return a string. */
7966 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02007967 {
7968 if (*rhs == NUL)
7969 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
7970 else
7971 rettv->vval.v_string = str2special_save(rhs, FALSE);
7972 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007973
7974 }
7975 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7976 {
7977 /* Return a dictionary. */
7978 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7979 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7980 dict_T *dict = rettv->vval.v_dict;
7981
Bram Moolenaare0be1672018-07-08 16:50:37 +02007982 dict_add_string(dict, "lhs", lhs);
7983 dict_add_string(dict, "rhs", mp->m_orig_str);
7984 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
7985 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
7986 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007987 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
7988 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02007989 dict_add_number(dict, "buffer", (long)buffer_local);
7990 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
7991 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007992
7993 vim_free(lhs);
7994 vim_free(mapmode);
7995 }
7996}
7997
7998#ifdef FEAT_FLOAT
7999/*
8000 * "log()" function
8001 */
8002 static void
8003f_log(typval_T *argvars, typval_T *rettv)
8004{
8005 float_T f = 0.0;
8006
8007 rettv->v_type = VAR_FLOAT;
8008 if (get_float_arg(argvars, &f) == OK)
8009 rettv->vval.v_float = log(f);
8010 else
8011 rettv->vval.v_float = 0.0;
8012}
8013
8014/*
8015 * "log10()" function
8016 */
8017 static void
8018f_log10(typval_T *argvars, typval_T *rettv)
8019{
8020 float_T f = 0.0;
8021
8022 rettv->v_type = VAR_FLOAT;
8023 if (get_float_arg(argvars, &f) == OK)
8024 rettv->vval.v_float = log10(f);
8025 else
8026 rettv->vval.v_float = 0.0;
8027}
8028#endif
8029
8030#ifdef FEAT_LUA
8031/*
8032 * "luaeval()" function
8033 */
8034 static void
8035f_luaeval(typval_T *argvars, typval_T *rettv)
8036{
8037 char_u *str;
8038 char_u buf[NUMBUFLEN];
8039
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008040 if (check_restricted() || check_secure())
8041 return;
8042
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008043 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008044 do_luaeval(str, argvars + 1, rettv);
8045}
8046#endif
8047
8048/*
8049 * "map()" function
8050 */
8051 static void
8052f_map(typval_T *argvars, typval_T *rettv)
8053{
8054 filter_map(argvars, rettv, TRUE);
8055}
8056
8057/*
8058 * "maparg()" function
8059 */
8060 static void
8061f_maparg(typval_T *argvars, typval_T *rettv)
8062{
8063 get_maparg(argvars, rettv, TRUE);
8064}
8065
8066/*
8067 * "mapcheck()" function
8068 */
8069 static void
8070f_mapcheck(typval_T *argvars, typval_T *rettv)
8071{
8072 get_maparg(argvars, rettv, FALSE);
8073}
8074
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008075typedef enum
8076{
8077 MATCH_END, /* matchend() */
8078 MATCH_MATCH, /* match() */
8079 MATCH_STR, /* matchstr() */
8080 MATCH_LIST, /* matchlist() */
8081 MATCH_POS /* matchstrpos() */
8082} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008083
8084 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008085find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008086{
8087 char_u *str = NULL;
8088 long len = 0;
8089 char_u *expr = NULL;
8090 char_u *pat;
8091 regmatch_T regmatch;
8092 char_u patbuf[NUMBUFLEN];
8093 char_u strbuf[NUMBUFLEN];
8094 char_u *save_cpo;
8095 long start = 0;
8096 long nth = 1;
8097 colnr_T startcol = 0;
8098 int match = 0;
8099 list_T *l = NULL;
8100 listitem_T *li = NULL;
8101 long idx = 0;
8102 char_u *tofree = NULL;
8103
8104 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
8105 save_cpo = p_cpo;
8106 p_cpo = (char_u *)"";
8107
8108 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008109 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008110 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008111 /* type MATCH_LIST: return empty list when there are no matches.
8112 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008113 if (rettv_list_alloc(rettv) == FAIL)
8114 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008115 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008116 && (list_append_string(rettv->vval.v_list,
8117 (char_u *)"", 0) == FAIL
8118 || list_append_number(rettv->vval.v_list,
8119 (varnumber_T)-1) == FAIL
8120 || list_append_number(rettv->vval.v_list,
8121 (varnumber_T)-1) == FAIL
8122 || list_append_number(rettv->vval.v_list,
8123 (varnumber_T)-1) == FAIL))
8124 {
8125 list_free(rettv->vval.v_list);
8126 rettv->vval.v_list = NULL;
8127 goto theend;
8128 }
8129 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008130 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008131 {
8132 rettv->v_type = VAR_STRING;
8133 rettv->vval.v_string = NULL;
8134 }
8135
8136 if (argvars[0].v_type == VAR_LIST)
8137 {
8138 if ((l = argvars[0].vval.v_list) == NULL)
8139 goto theend;
8140 li = l->lv_first;
8141 }
8142 else
8143 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008144 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008145 len = (long)STRLEN(str);
8146 }
8147
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008148 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008149 if (pat == NULL)
8150 goto theend;
8151
8152 if (argvars[2].v_type != VAR_UNKNOWN)
8153 {
8154 int error = FALSE;
8155
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008156 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008157 if (error)
8158 goto theend;
8159 if (l != NULL)
8160 {
8161 li = list_find(l, start);
8162 if (li == NULL)
8163 goto theend;
8164 idx = l->lv_idx; /* use the cached index */
8165 }
8166 else
8167 {
8168 if (start < 0)
8169 start = 0;
8170 if (start > len)
8171 goto theend;
8172 /* When "count" argument is there ignore matches before "start",
8173 * otherwise skip part of the string. Differs when pattern is "^"
8174 * or "\<". */
8175 if (argvars[3].v_type != VAR_UNKNOWN)
8176 startcol = start;
8177 else
8178 {
8179 str += start;
8180 len -= start;
8181 }
8182 }
8183
8184 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008185 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008186 if (error)
8187 goto theend;
8188 }
8189
8190 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8191 if (regmatch.regprog != NULL)
8192 {
8193 regmatch.rm_ic = p_ic;
8194
8195 for (;;)
8196 {
8197 if (l != NULL)
8198 {
8199 if (li == NULL)
8200 {
8201 match = FALSE;
8202 break;
8203 }
8204 vim_free(tofree);
8205 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
8206 if (str == NULL)
8207 break;
8208 }
8209
8210 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
8211
8212 if (match && --nth <= 0)
8213 break;
8214 if (l == NULL && !match)
8215 break;
8216
8217 /* Advance to just after the match. */
8218 if (l != NULL)
8219 {
8220 li = li->li_next;
8221 ++idx;
8222 }
8223 else
8224 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008225 startcol = (colnr_T)(regmatch.startp[0]
8226 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008227 if (startcol > (colnr_T)len
8228 || str + startcol <= regmatch.startp[0])
8229 {
8230 match = FALSE;
8231 break;
8232 }
8233 }
8234 }
8235
8236 if (match)
8237 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008238 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008239 {
8240 listitem_T *li1 = rettv->vval.v_list->lv_first;
8241 listitem_T *li2 = li1->li_next;
8242 listitem_T *li3 = li2->li_next;
8243 listitem_T *li4 = li3->li_next;
8244
8245 vim_free(li1->li_tv.vval.v_string);
8246 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
8247 (int)(regmatch.endp[0] - regmatch.startp[0]));
8248 li3->li_tv.vval.v_number =
8249 (varnumber_T)(regmatch.startp[0] - expr);
8250 li4->li_tv.vval.v_number =
8251 (varnumber_T)(regmatch.endp[0] - expr);
8252 if (l != NULL)
8253 li2->li_tv.vval.v_number = (varnumber_T)idx;
8254 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008255 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008256 {
8257 int i;
8258
8259 /* return list with matched string and submatches */
8260 for (i = 0; i < NSUBEXP; ++i)
8261 {
8262 if (regmatch.endp[i] == NULL)
8263 {
8264 if (list_append_string(rettv->vval.v_list,
8265 (char_u *)"", 0) == FAIL)
8266 break;
8267 }
8268 else if (list_append_string(rettv->vval.v_list,
8269 regmatch.startp[i],
8270 (int)(regmatch.endp[i] - regmatch.startp[i]))
8271 == FAIL)
8272 break;
8273 }
8274 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008275 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008276 {
8277 /* return matched string */
8278 if (l != NULL)
8279 copy_tv(&li->li_tv, rettv);
8280 else
8281 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
8282 (int)(regmatch.endp[0] - regmatch.startp[0]));
8283 }
8284 else if (l != NULL)
8285 rettv->vval.v_number = idx;
8286 else
8287 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008288 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008289 rettv->vval.v_number =
8290 (varnumber_T)(regmatch.startp[0] - str);
8291 else
8292 rettv->vval.v_number =
8293 (varnumber_T)(regmatch.endp[0] - str);
8294 rettv->vval.v_number += (varnumber_T)(str - expr);
8295 }
8296 }
8297 vim_regfree(regmatch.regprog);
8298 }
8299
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008300theend:
8301 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008302 /* matchstrpos() without a list: drop the second item. */
8303 listitem_remove(rettv->vval.v_list,
8304 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008305 vim_free(tofree);
8306 p_cpo = save_cpo;
8307}
8308
8309/*
8310 * "match()" function
8311 */
8312 static void
8313f_match(typval_T *argvars, typval_T *rettv)
8314{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008315 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008316}
8317
Bram Moolenaar95e51472018-07-28 16:55:56 +02008318#ifdef FEAT_SEARCH_EXTRA
8319 static int
8320matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
8321{
8322 dictitem_T *di;
8323
8324 if (tv->v_type != VAR_DICT)
8325 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008326 emsg(_(e_dictreq));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008327 return FAIL;
8328 }
8329
8330 if (dict_find(tv->vval.v_dict, (char_u *)"conceal", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01008331 *conceal_char = dict_get_string(tv->vval.v_dict,
Bram Moolenaar95e51472018-07-28 16:55:56 +02008332 (char_u *)"conceal", FALSE);
8333
8334 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
8335 {
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02008336 *win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008337 if (*win == NULL)
8338 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01008339 emsg(_(e_invalwindow));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008340 return FAIL;
8341 }
8342 }
8343
8344 return OK;
8345}
8346#endif
8347
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008348/*
8349 * "matchadd()" function
8350 */
8351 static void
8352f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8353{
8354#ifdef FEAT_SEARCH_EXTRA
8355 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008356 char_u *grp = tv_get_string_buf_chk(&argvars[0], buf); /* group */
8357 char_u *pat = tv_get_string_buf_chk(&argvars[1], buf); /* pattern */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008358 int prio = 10; /* default priority */
8359 int id = -1;
8360 int error = FALSE;
8361 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008362 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008363
8364 rettv->vval.v_number = -1;
8365
8366 if (grp == NULL || pat == NULL)
8367 return;
8368 if (argvars[2].v_type != VAR_UNKNOWN)
8369 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008370 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008371 if (argvars[3].v_type != VAR_UNKNOWN)
8372 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008373 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008374 if (argvars[4].v_type != VAR_UNKNOWN
8375 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8376 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008377 }
8378 }
8379 if (error == TRUE)
8380 return;
8381 if (id >= 1 && id <= 3)
8382 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008383 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008384 return;
8385 }
8386
Bram Moolenaar95e51472018-07-28 16:55:56 +02008387 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008388 conceal_char);
8389#endif
8390}
8391
8392/*
8393 * "matchaddpos()" function
8394 */
8395 static void
8396f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8397{
8398#ifdef FEAT_SEARCH_EXTRA
8399 char_u buf[NUMBUFLEN];
8400 char_u *group;
8401 int prio = 10;
8402 int id = -1;
8403 int error = FALSE;
8404 list_T *l;
8405 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008406 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008407
8408 rettv->vval.v_number = -1;
8409
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008410 group = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008411 if (group == NULL)
8412 return;
8413
8414 if (argvars[1].v_type != VAR_LIST)
8415 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008416 semsg(_(e_listarg), "matchaddpos()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008417 return;
8418 }
8419 l = argvars[1].vval.v_list;
8420 if (l == NULL)
8421 return;
8422
8423 if (argvars[2].v_type != VAR_UNKNOWN)
8424 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008425 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008426 if (argvars[3].v_type != VAR_UNKNOWN)
8427 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008428 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008429
8430 if (argvars[4].v_type != VAR_UNKNOWN
8431 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8432 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008433 }
8434 }
8435 if (error == TRUE)
8436 return;
8437
8438 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8439 if (id == 1 || id == 2)
8440 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008441 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008442 return;
8443 }
8444
Bram Moolenaar95e51472018-07-28 16:55:56 +02008445 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008446 conceal_char);
8447#endif
8448}
8449
8450/*
8451 * "matcharg()" function
8452 */
8453 static void
8454f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8455{
8456 if (rettv_list_alloc(rettv) == OK)
8457 {
8458#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008459 int id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008460 matchitem_T *m;
8461
8462 if (id >= 1 && id <= 3)
8463 {
8464 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8465 {
8466 list_append_string(rettv->vval.v_list,
8467 syn_id2name(m->hlg_id), -1);
8468 list_append_string(rettv->vval.v_list, m->pattern, -1);
8469 }
8470 else
8471 {
8472 list_append_string(rettv->vval.v_list, NULL, -1);
8473 list_append_string(rettv->vval.v_list, NULL, -1);
8474 }
8475 }
8476#endif
8477 }
8478}
8479
8480/*
8481 * "matchdelete()" function
8482 */
8483 static void
8484f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8485{
8486#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01008487 win_T *win = get_optional_window(argvars, 1);
8488
8489 if (win == NULL)
8490 rettv->vval.v_number = -1;
8491 else
8492 rettv->vval.v_number = match_delete(win,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008493 (int)tv_get_number(&argvars[0]), TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008494#endif
8495}
8496
8497/*
8498 * "matchend()" function
8499 */
8500 static void
8501f_matchend(typval_T *argvars, typval_T *rettv)
8502{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008503 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008504}
8505
8506/*
8507 * "matchlist()" function
8508 */
8509 static void
8510f_matchlist(typval_T *argvars, typval_T *rettv)
8511{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008512 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008513}
8514
8515/*
8516 * "matchstr()" function
8517 */
8518 static void
8519f_matchstr(typval_T *argvars, typval_T *rettv)
8520{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008521 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008522}
8523
8524/*
8525 * "matchstrpos()" function
8526 */
8527 static void
8528f_matchstrpos(typval_T *argvars, typval_T *rettv)
8529{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008530 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008531}
8532
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008533 static void
8534max_min(typval_T *argvars, typval_T *rettv, int domax)
8535{
8536 varnumber_T n = 0;
8537 varnumber_T i;
8538 int error = FALSE;
8539
8540 if (argvars[0].v_type == VAR_LIST)
8541 {
8542 list_T *l;
8543 listitem_T *li;
8544
8545 l = argvars[0].vval.v_list;
8546 if (l != NULL)
8547 {
8548 li = l->lv_first;
8549 if (li != NULL)
8550 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008551 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008552 for (;;)
8553 {
8554 li = li->li_next;
8555 if (li == NULL)
8556 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008557 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008558 if (domax ? i > n : i < n)
8559 n = i;
8560 }
8561 }
8562 }
8563 }
8564 else if (argvars[0].v_type == VAR_DICT)
8565 {
8566 dict_T *d;
8567 int first = TRUE;
8568 hashitem_T *hi;
8569 int todo;
8570
8571 d = argvars[0].vval.v_dict;
8572 if (d != NULL)
8573 {
8574 todo = (int)d->dv_hashtab.ht_used;
8575 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8576 {
8577 if (!HASHITEM_EMPTY(hi))
8578 {
8579 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008580 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008581 if (first)
8582 {
8583 n = i;
8584 first = FALSE;
8585 }
8586 else if (domax ? i > n : i < n)
8587 n = i;
8588 }
8589 }
8590 }
8591 }
8592 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008593 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008594 rettv->vval.v_number = error ? 0 : n;
8595}
8596
8597/*
8598 * "max()" function
8599 */
8600 static void
8601f_max(typval_T *argvars, typval_T *rettv)
8602{
8603 max_min(argvars, rettv, TRUE);
8604}
8605
8606/*
8607 * "min()" function
8608 */
8609 static void
8610f_min(typval_T *argvars, typval_T *rettv)
8611{
8612 max_min(argvars, rettv, FALSE);
8613}
8614
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008615/*
8616 * Create the directory in which "dir" is located, and higher levels when
8617 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008618 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008619 */
8620 static int
8621mkdir_recurse(char_u *dir, int prot)
8622{
8623 char_u *p;
8624 char_u *updir;
8625 int r = FAIL;
8626
8627 /* Get end of directory name in "dir".
8628 * We're done when it's "/" or "c:/". */
8629 p = gettail_sep(dir);
8630 if (p <= get_past_head(dir))
8631 return OK;
8632
8633 /* If the directory exists we're done. Otherwise: create it.*/
8634 updir = vim_strnsave(dir, (int)(p - dir));
8635 if (updir == NULL)
8636 return FAIL;
8637 if (mch_isdir(updir))
8638 r = OK;
8639 else if (mkdir_recurse(updir, prot) == OK)
8640 r = vim_mkdir_emsg(updir, prot);
8641 vim_free(updir);
8642 return r;
8643}
8644
8645#ifdef vim_mkdir
8646/*
8647 * "mkdir()" function
8648 */
8649 static void
8650f_mkdir(typval_T *argvars, typval_T *rettv)
8651{
8652 char_u *dir;
8653 char_u buf[NUMBUFLEN];
8654 int prot = 0755;
8655
8656 rettv->vval.v_number = FAIL;
8657 if (check_restricted() || check_secure())
8658 return;
8659
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008660 dir = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008661 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008662 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008663
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008664 if (*gettail(dir) == NUL)
8665 /* remove trailing slashes */
8666 *gettail_sep(dir) = NUL;
8667
8668 if (argvars[1].v_type != VAR_UNKNOWN)
8669 {
8670 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008671 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008672 prot = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008673 if (prot == -1)
8674 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008675 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008676 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008677 {
8678 if (mch_isdir(dir))
8679 {
8680 /* With the "p" flag it's OK if the dir already exists. */
8681 rettv->vval.v_number = OK;
8682 return;
8683 }
8684 mkdir_recurse(dir, prot);
8685 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008686 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008687 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008688}
8689#endif
8690
8691/*
8692 * "mode()" function
8693 */
8694 static void
8695f_mode(typval_T *argvars, typval_T *rettv)
8696{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008697 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008698
Bram Moolenaar612cc382018-07-29 15:34:26 +02008699 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008700
8701 if (time_for_testing == 93784)
8702 {
8703 /* Testing the two-character code. */
8704 buf[0] = 'x';
8705 buf[1] = '!';
8706 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008707#ifdef FEAT_TERMINAL
8708 else if (term_use_loop())
8709 buf[0] = 't';
8710#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008711 else if (VIsual_active)
8712 {
8713 if (VIsual_select)
8714 buf[0] = VIsual_mode + 's' - 'v';
8715 else
8716 buf[0] = VIsual_mode;
8717 }
8718 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8719 || State == CONFIRM)
8720 {
8721 buf[0] = 'r';
8722 if (State == ASKMORE)
8723 buf[1] = 'm';
8724 else if (State == CONFIRM)
8725 buf[1] = '?';
8726 }
8727 else if (State == EXTERNCMD)
8728 buf[0] = '!';
8729 else if (State & INSERT)
8730 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008731 if (State & VREPLACE_FLAG)
8732 {
8733 buf[0] = 'R';
8734 buf[1] = 'v';
8735 }
8736 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008737 {
8738 if (State & REPLACE_FLAG)
8739 buf[0] = 'R';
8740 else
8741 buf[0] = 'i';
8742#ifdef FEAT_INS_EXPAND
8743 if (ins_compl_active())
8744 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008745 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008746 buf[1] = 'x';
8747#endif
8748 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008749 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008750 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008751 {
8752 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008753 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008754 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008755 else if (exmode_active == EXMODE_NORMAL)
8756 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008757 }
8758 else
8759 {
8760 buf[0] = 'n';
8761 if (finish_op)
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008762 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008763 buf[1] = 'o';
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008764 // to be able to detect force-linewise/blockwise/characterwise operations
8765 buf[2] = motion_force;
8766 }
Bram Moolenaar612cc382018-07-29 15:34:26 +02008767 else if (restart_edit == 'I' || restart_edit == 'R'
8768 || restart_edit == 'V')
8769 {
8770 buf[1] = 'i';
8771 buf[2] = restart_edit;
8772 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008773 }
8774
8775 /* Clear out the minor mode when the argument is not a non-zero number or
8776 * non-empty string. */
8777 if (!non_zero_arg(&argvars[0]))
8778 buf[1] = NUL;
8779
8780 rettv->vval.v_string = vim_strsave(buf);
8781 rettv->v_type = VAR_STRING;
8782}
8783
8784#if defined(FEAT_MZSCHEME) || defined(PROTO)
8785/*
8786 * "mzeval()" function
8787 */
8788 static void
8789f_mzeval(typval_T *argvars, typval_T *rettv)
8790{
8791 char_u *str;
8792 char_u buf[NUMBUFLEN];
8793
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008794 if (check_restricted() || check_secure())
8795 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008796 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008797 do_mzeval(str, rettv);
8798}
8799
8800 void
8801mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8802{
8803 typval_T argvars[3];
8804
8805 argvars[0].v_type = VAR_STRING;
8806 argvars[0].vval.v_string = name;
8807 copy_tv(args, &argvars[1]);
8808 argvars[2].v_type = VAR_UNKNOWN;
8809 f_call(argvars, rettv);
8810 clear_tv(&argvars[1]);
8811}
8812#endif
8813
8814/*
8815 * "nextnonblank()" function
8816 */
8817 static void
8818f_nextnonblank(typval_T *argvars, typval_T *rettv)
8819{
8820 linenr_T lnum;
8821
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008822 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008823 {
8824 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8825 {
8826 lnum = 0;
8827 break;
8828 }
8829 if (*skipwhite(ml_get(lnum)) != NUL)
8830 break;
8831 }
8832 rettv->vval.v_number = lnum;
8833}
8834
8835/*
8836 * "nr2char()" function
8837 */
8838 static void
8839f_nr2char(typval_T *argvars, typval_T *rettv)
8840{
8841 char_u buf[NUMBUFLEN];
8842
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008843 if (has_mbyte)
8844 {
8845 int utf8 = 0;
8846
8847 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008848 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008849 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01008850 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008851 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008852 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008853 }
8854 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008855 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008856 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008857 buf[1] = NUL;
8858 }
8859 rettv->v_type = VAR_STRING;
8860 rettv->vval.v_string = vim_strsave(buf);
8861}
8862
8863/*
8864 * "or(expr, expr)" function
8865 */
8866 static void
8867f_or(typval_T *argvars, typval_T *rettv)
8868{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008869 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
8870 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008871}
8872
8873/*
8874 * "pathshorten()" function
8875 */
8876 static void
8877f_pathshorten(typval_T *argvars, typval_T *rettv)
8878{
8879 char_u *p;
8880
8881 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008882 p = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008883 if (p == NULL)
8884 rettv->vval.v_string = NULL;
8885 else
8886 {
8887 p = vim_strsave(p);
8888 rettv->vval.v_string = p;
8889 if (p != NULL)
8890 shorten_dir(p);
8891 }
8892}
8893
8894#ifdef FEAT_PERL
8895/*
8896 * "perleval()" function
8897 */
8898 static void
8899f_perleval(typval_T *argvars, typval_T *rettv)
8900{
8901 char_u *str;
8902 char_u buf[NUMBUFLEN];
8903
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008904 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008905 do_perleval(str, rettv);
8906}
8907#endif
8908
8909#ifdef FEAT_FLOAT
8910/*
8911 * "pow()" function
8912 */
8913 static void
8914f_pow(typval_T *argvars, typval_T *rettv)
8915{
8916 float_T fx = 0.0, fy = 0.0;
8917
8918 rettv->v_type = VAR_FLOAT;
8919 if (get_float_arg(argvars, &fx) == OK
8920 && get_float_arg(&argvars[1], &fy) == OK)
8921 rettv->vval.v_float = pow(fx, fy);
8922 else
8923 rettv->vval.v_float = 0.0;
8924}
8925#endif
8926
8927/*
8928 * "prevnonblank()" function
8929 */
8930 static void
8931f_prevnonblank(typval_T *argvars, typval_T *rettv)
8932{
8933 linenr_T lnum;
8934
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008935 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008936 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8937 lnum = 0;
8938 else
8939 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8940 --lnum;
8941 rettv->vval.v_number = lnum;
8942}
8943
8944/* This dummy va_list is here because:
8945 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8946 * - locally in the function results in a "used before set" warning
8947 * - using va_start() to initialize it gives "function with fixed args" error */
8948static va_list ap;
8949
8950/*
8951 * "printf()" function
8952 */
8953 static void
8954f_printf(typval_T *argvars, typval_T *rettv)
8955{
8956 char_u buf[NUMBUFLEN];
8957 int len;
8958 char_u *s;
8959 int saved_did_emsg = did_emsg;
8960 char *fmt;
8961
8962 rettv->v_type = VAR_STRING;
8963 rettv->vval.v_string = NULL;
8964
8965 /* Get the required length, allocate the buffer and do it for real. */
8966 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008967 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008968 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008969 if (!did_emsg)
8970 {
8971 s = alloc(len + 1);
8972 if (s != NULL)
8973 {
8974 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008975 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8976 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008977 }
8978 }
8979 did_emsg |= saved_did_emsg;
8980}
8981
Bram Moolenaarf2732452018-06-03 14:47:35 +02008982#ifdef FEAT_JOB_CHANNEL
8983/*
8984 * "prompt_setcallback({buffer}, {callback})" function
8985 */
8986 static void
8987f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
8988{
8989 buf_T *buf;
8990 char_u *callback;
8991 partial_T *partial;
8992
8993 if (check_secure())
8994 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01008995 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008996 if (buf == NULL)
8997 return;
8998
8999 callback = get_callback(&argvars[1], &partial);
9000 if (callback == NULL)
9001 return;
9002
9003 free_callback(buf->b_prompt_callback, buf->b_prompt_partial);
9004 if (partial == NULL)
9005 buf->b_prompt_callback = vim_strsave(callback);
9006 else
9007 /* pointer into the partial */
9008 buf->b_prompt_callback = callback;
9009 buf->b_prompt_partial = partial;
9010}
9011
9012/*
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009013 * "prompt_setinterrupt({buffer}, {callback})" function
9014 */
9015 static void
9016f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
9017{
9018 buf_T *buf;
9019 char_u *callback;
9020 partial_T *partial;
9021
9022 if (check_secure())
9023 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009024 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009025 if (buf == NULL)
9026 return;
9027
9028 callback = get_callback(&argvars[1], &partial);
9029 if (callback == NULL)
9030 return;
9031
9032 free_callback(buf->b_prompt_interrupt, buf->b_prompt_int_partial);
9033 if (partial == NULL)
9034 buf->b_prompt_interrupt = vim_strsave(callback);
9035 else
9036 /* pointer into the partial */
9037 buf->b_prompt_interrupt = callback;
9038 buf->b_prompt_int_partial = partial;
9039}
9040
9041/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02009042 * "prompt_setprompt({buffer}, {text})" function
9043 */
9044 static void
9045f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
9046{
9047 buf_T *buf;
9048 char_u *text;
9049
9050 if (check_secure())
9051 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009052 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009053 if (buf == NULL)
9054 return;
9055
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009056 text = tv_get_string(&argvars[1]);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009057 vim_free(buf->b_prompt_text);
9058 buf->b_prompt_text = vim_strsave(text);
9059}
9060#endif
9061
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009062/*
9063 * "pumvisible()" function
9064 */
9065 static void
9066f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9067{
9068#ifdef FEAT_INS_EXPAND
9069 if (pum_visible())
9070 rettv->vval.v_number = 1;
9071#endif
9072}
9073
9074#ifdef FEAT_PYTHON3
9075/*
9076 * "py3eval()" function
9077 */
9078 static void
9079f_py3eval(typval_T *argvars, typval_T *rettv)
9080{
9081 char_u *str;
9082 char_u buf[NUMBUFLEN];
9083
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009084 if (check_restricted() || check_secure())
9085 return;
9086
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009087 if (p_pyx == 0)
9088 p_pyx = 3;
9089
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009090 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009091 do_py3eval(str, rettv);
9092}
9093#endif
9094
9095#ifdef FEAT_PYTHON
9096/*
9097 * "pyeval()" function
9098 */
9099 static void
9100f_pyeval(typval_T *argvars, typval_T *rettv)
9101{
9102 char_u *str;
9103 char_u buf[NUMBUFLEN];
9104
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009105 if (check_restricted() || check_secure())
9106 return;
9107
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009108 if (p_pyx == 0)
9109 p_pyx = 2;
9110
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009111 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009112 do_pyeval(str, rettv);
9113}
9114#endif
9115
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009116#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
9117/*
9118 * "pyxeval()" function
9119 */
9120 static void
9121f_pyxeval(typval_T *argvars, typval_T *rettv)
9122{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009123 if (check_restricted() || check_secure())
9124 return;
9125
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009126# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
9127 init_pyxversion();
9128 if (p_pyx == 2)
9129 f_pyeval(argvars, rettv);
9130 else
9131 f_py3eval(argvars, rettv);
9132# elif defined(FEAT_PYTHON)
9133 f_pyeval(argvars, rettv);
9134# elif defined(FEAT_PYTHON3)
9135 f_py3eval(argvars, rettv);
9136# endif
9137}
9138#endif
9139
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009140/*
9141 * "range()" function
9142 */
9143 static void
9144f_range(typval_T *argvars, typval_T *rettv)
9145{
9146 varnumber_T start;
9147 varnumber_T end;
9148 varnumber_T stride = 1;
9149 varnumber_T i;
9150 int error = FALSE;
9151
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009152 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009153 if (argvars[1].v_type == VAR_UNKNOWN)
9154 {
9155 end = start - 1;
9156 start = 0;
9157 }
9158 else
9159 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009160 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009161 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009162 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009163 }
9164
9165 if (error)
9166 return; /* type error; errmsg already given */
9167 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009168 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009169 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009170 emsg(_("E727: Start past end"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009171 else
9172 {
9173 if (rettv_list_alloc(rettv) == OK)
9174 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
9175 if (list_append_number(rettv->vval.v_list,
9176 (varnumber_T)i) == FAIL)
9177 break;
9178 }
9179}
9180
9181/*
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009182 * Evaluate "expr" for readdir().
9183 */
9184 static int
9185readdir_checkitem(typval_T *expr, char_u *name)
9186{
9187 typval_T save_val;
9188 typval_T rettv;
9189 typval_T argv[2];
9190 int retval = 0;
9191 int error = FALSE;
9192
9193 prepare_vimvar(VV_VAL, &save_val);
9194 set_vim_var_string(VV_VAL, name, -1);
9195 argv[0].v_type = VAR_STRING;
9196 argv[0].vval.v_string = name;
9197
9198 if (eval_expr_typval(expr, argv, 1, &rettv) == FAIL)
9199 goto theend;
9200
9201 retval = tv_get_number_chk(&rettv, &error);
9202 if (error)
9203 retval = -1;
9204 clear_tv(&rettv);
9205
9206theend:
9207 set_vim_var_string(VV_VAL, NULL, 0);
9208 restore_vimvar(VV_VAL, &save_val);
9209 return retval;
9210}
9211
9212/*
9213 * "readdir()" function
9214 */
9215 static void
9216f_readdir(typval_T *argvars, typval_T *rettv)
9217{
9218 typval_T *expr;
9219 int failed = FALSE;
9220 char_u *path;
9221 garray_T ga;
9222 int i;
9223#ifdef MSWIN
9224 char_u *buf, *p;
9225 WIN32_FIND_DATA fb;
9226 int ok;
9227 HANDLE hFind = INVALID_HANDLE_VALUE;
9228 WIN32_FIND_DATAW wfb;
9229 WCHAR *wn = NULL; // UCS-2 name, NULL when not used.
9230#endif
9231
9232 if (rettv_list_alloc(rettv) == FAIL)
9233 return;
9234 path = tv_get_string(&argvars[0]);
9235 expr = &argvars[1];
9236 ga_init2(&ga, (int)sizeof(char *), 20);
9237
9238#ifdef MSWIN
9239 buf = alloc((int)MAXPATHL);
9240 if (buf == NULL)
9241 return;
9242 STRNCPY(buf, path, MAXPATHL-5);
9243 p = vim_strpbrk(path, (char_u *)"\\/");
9244 if (p != NULL)
9245 *p = NUL;
9246 STRCAT(buf, "\\*");
9247
9248 wn = enc_to_utf16(buf, NULL);
9249 if (wn != NULL)
9250 hFind = FindFirstFileW(wn, &wfb);
9251 ok = (hFind != INVALID_HANDLE_VALUE);
9252 if (!ok)
9253 smsg(_(e_notopen), path);
9254 else
9255 {
9256 while (ok)
9257 {
9258 int ignore;
9259
9260 p = utf16_to_enc(wfb.cFileName, NULL); // p is allocated here
9261 if (p == NULL)
9262 break; // out of memory
9263
9264 ignore = p[0] == '.' && (p[1] == NUL
9265 || (p[1] == '.' && p[2] == NUL));
9266 if (!ignore && expr->v_type != VAR_UNKNOWN)
9267 {
9268 int r = readdir_checkitem(expr, p);
9269
9270 if (r < 0)
9271 {
9272 vim_free(p);
9273 break;
9274 }
9275 if (r == 0)
9276 ignore = TRUE;
9277 }
9278
9279 if (!ignore)
9280 {
9281 if (ga_grow(&ga, 1) == OK)
9282 ((char_u**)ga.ga_data)[ga.ga_len++] = vim_strsave(p);
9283 else
9284 {
9285 failed = TRUE;
9286 vim_free(p);
9287 break;
9288 }
9289 }
9290
9291 vim_free(p);
9292 ok = FindNextFileW(hFind, &wfb);
9293 }
9294 FindClose(hFind);
9295 }
9296
9297 vim_free(buf);
9298 vim_free(wn);
9299#else
9300 DIR *dirp;
9301 struct dirent *dp;
9302 char_u *p;
9303
9304 dirp = opendir((char *)path);
9305 if (dirp == NULL)
9306 smsg(_(e_notopen), path);
9307 else
9308 {
9309 for (;;)
9310 {
9311 int ignore;
9312
9313 dp = readdir(dirp);
9314 if (dp == NULL)
9315 break;
9316 p = (char_u *)dp->d_name;
9317
9318 ignore = p[0] == '.' &&
9319 (p[1] == NUL ||
9320 (p[1] == '.' && p[2] == NUL));
9321 if (!ignore && expr->v_type != VAR_UNKNOWN)
9322 {
9323 int r = readdir_checkitem(expr, p);
9324
9325 if (r < 0)
9326 break;
9327 if (r == 0)
9328 ignore = TRUE;
9329 }
9330
9331 if (!ignore)
9332 {
9333 if (ga_grow(&ga, 1) == OK)
9334 ((char_u**)ga.ga_data)[ga.ga_len++] = vim_strsave(p);
9335 else
9336 {
9337 failed = TRUE;
9338 break;
9339 }
9340 }
9341 }
9342
9343 closedir(dirp);
9344 }
9345#endif
9346
9347 rettv->vval.v_list = list_alloc();
9348 if (!failed && rettv->vval.v_list != NULL)
9349 {
9350 ++rettv->vval.v_list->lv_refcount;
9351 sort_strings((char_u **)ga.ga_data, ga.ga_len);
9352 for (i = 0; i < ga.ga_len; i++)
9353 {
9354 p = ((char_u **)ga.ga_data)[i];
9355 list_append_string(rettv->vval.v_list, p, -1);
9356 }
9357 }
9358 for (i = 0; i < ga.ga_len; i++)
9359 vim_free(((char_u **)ga.ga_data)[i]);
9360
9361 ga_clear(&ga);
9362}
9363
9364/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009365 * "readfile()" function
9366 */
9367 static void
9368f_readfile(typval_T *argvars, typval_T *rettv)
9369{
9370 int binary = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009371 int blob = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009372 int failed = FALSE;
9373 char_u *fname;
9374 FILE *fd;
9375 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
9376 int io_size = sizeof(buf);
9377 int readlen; /* size of last fread() */
9378 char_u *prev = NULL; /* previously read bytes, if any */
9379 long prevlen = 0; /* length of data in prev */
9380 long prevsize = 0; /* size of prev buffer */
9381 long maxline = MAXLNUM;
9382 long cnt = 0;
9383 char_u *p; /* position in buf */
9384 char_u *start; /* start of current line */
9385
9386 if (argvars[1].v_type != VAR_UNKNOWN)
9387 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009388 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009389 binary = TRUE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009390 if (STRCMP(tv_get_string(&argvars[1]), "B") == 0)
9391 blob = TRUE;
9392
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009393 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009394 maxline = (long)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009395 }
9396
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009397 if (blob)
9398 {
9399 if (rettv_blob_alloc(rettv) == FAIL)
9400 return;
9401 }
9402 else
9403 {
9404 if (rettv_list_alloc(rettv) == FAIL)
9405 return;
9406 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009407
9408 /* Always open the file in binary mode, library functions have a mind of
9409 * their own about CR-LF conversion. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009410 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009411 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
9412 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009413 semsg(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009414 return;
9415 }
9416
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009417 if (blob)
9418 {
9419 if (read_blob(fd, rettv->vval.v_blob) == FAIL)
9420 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009421 emsg("cannot read file");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009422 blob_free(rettv->vval.v_blob);
9423 }
9424 fclose(fd);
9425 return;
9426 }
9427
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009428 while (cnt < maxline || maxline < 0)
9429 {
9430 readlen = (int)fread(buf, 1, io_size, fd);
9431
9432 /* This for loop processes what was read, but is also entered at end
9433 * of file so that either:
9434 * - an incomplete line gets written
9435 * - a "binary" file gets an empty line at the end if it ends in a
9436 * newline. */
9437 for (p = buf, start = buf;
9438 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
9439 ++p)
9440 {
9441 if (*p == '\n' || readlen <= 0)
9442 {
9443 listitem_T *li;
9444 char_u *s = NULL;
9445 long_u len = p - start;
9446
9447 /* Finished a line. Remove CRs before NL. */
9448 if (readlen > 0 && !binary)
9449 {
9450 while (len > 0 && start[len - 1] == '\r')
9451 --len;
9452 /* removal may cross back to the "prev" string */
9453 if (len == 0)
9454 while (prevlen > 0 && prev[prevlen - 1] == '\r')
9455 --prevlen;
9456 }
9457 if (prevlen == 0)
9458 s = vim_strnsave(start, (int)len);
9459 else
9460 {
9461 /* Change "prev" buffer to be the right size. This way
9462 * the bytes are only copied once, and very long lines are
9463 * allocated only once. */
9464 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
9465 {
9466 mch_memmove(s + prevlen, start, len);
9467 s[prevlen + len] = NUL;
9468 prev = NULL; /* the list will own the string */
9469 prevlen = prevsize = 0;
9470 }
9471 }
9472 if (s == NULL)
9473 {
9474 do_outofmem_msg((long_u) prevlen + len + 1);
9475 failed = TRUE;
9476 break;
9477 }
9478
9479 if ((li = listitem_alloc()) == NULL)
9480 {
9481 vim_free(s);
9482 failed = TRUE;
9483 break;
9484 }
9485 li->li_tv.v_type = VAR_STRING;
9486 li->li_tv.v_lock = 0;
9487 li->li_tv.vval.v_string = s;
9488 list_append(rettv->vval.v_list, li);
9489
9490 start = p + 1; /* step over newline */
9491 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
9492 break;
9493 }
9494 else if (*p == NUL)
9495 *p = '\n';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009496 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
9497 * when finding the BF and check the previous two bytes. */
9498 else if (*p == 0xbf && enc_utf8 && !binary)
9499 {
9500 /* Find the two bytes before the 0xbf. If p is at buf, or buf
9501 * + 1, these may be in the "prev" string. */
9502 char_u back1 = p >= buf + 1 ? p[-1]
9503 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
9504 char_u back2 = p >= buf + 2 ? p[-2]
9505 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
9506 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
9507
9508 if (back2 == 0xef && back1 == 0xbb)
9509 {
9510 char_u *dest = p - 2;
9511
9512 /* Usually a BOM is at the beginning of a file, and so at
9513 * the beginning of a line; then we can just step over it.
9514 */
9515 if (start == dest)
9516 start = p + 1;
9517 else
9518 {
9519 /* have to shuffle buf to close gap */
9520 int adjust_prevlen = 0;
9521
9522 if (dest < buf)
9523 {
9524 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
9525 dest = buf;
9526 }
9527 if (readlen > p - buf + 1)
9528 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
9529 readlen -= 3 - adjust_prevlen;
9530 prevlen -= adjust_prevlen;
9531 p = dest - 1;
9532 }
9533 }
9534 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009535 } /* for */
9536
9537 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
9538 break;
9539 if (start < p)
9540 {
9541 /* There's part of a line in buf, store it in "prev". */
9542 if (p - start + prevlen >= prevsize)
9543 {
9544 /* need bigger "prev" buffer */
9545 char_u *newprev;
9546
9547 /* A common use case is ordinary text files and "prev" gets a
9548 * fragment of a line, so the first allocation is made
9549 * small, to avoid repeatedly 'allocing' large and
9550 * 'reallocing' small. */
9551 if (prevsize == 0)
9552 prevsize = (long)(p - start);
9553 else
9554 {
9555 long grow50pc = (prevsize * 3) / 2;
9556 long growmin = (long)((p - start) * 2 + prevlen);
9557 prevsize = grow50pc > growmin ? grow50pc : growmin;
9558 }
9559 newprev = prev == NULL ? alloc(prevsize)
9560 : vim_realloc(prev, prevsize);
9561 if (newprev == NULL)
9562 {
9563 do_outofmem_msg((long_u)prevsize);
9564 failed = TRUE;
9565 break;
9566 }
9567 prev = newprev;
9568 }
9569 /* Add the line part to end of "prev". */
9570 mch_memmove(prev + prevlen, start, p - start);
9571 prevlen += (long)(p - start);
9572 }
9573 } /* while */
9574
9575 /*
9576 * For a negative line count use only the lines at the end of the file,
9577 * free the rest.
9578 */
9579 if (!failed && maxline < 0)
9580 while (cnt > -maxline)
9581 {
9582 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
9583 --cnt;
9584 }
9585
9586 if (failed)
9587 {
9588 list_free(rettv->vval.v_list);
9589 /* readfile doc says an empty list is returned on error */
9590 rettv->vval.v_list = list_alloc();
9591 }
9592
9593 vim_free(prev);
9594 fclose(fd);
9595}
9596
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009597 static void
9598return_register(int regname, typval_T *rettv)
9599{
9600 char_u buf[2] = {0, 0};
9601
9602 buf[0] = (char_u)regname;
9603 rettv->v_type = VAR_STRING;
9604 rettv->vval.v_string = vim_strsave(buf);
9605}
9606
9607/*
9608 * "reg_executing()" function
9609 */
9610 static void
9611f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9612{
9613 return_register(reg_executing, rettv);
9614}
9615
9616/*
9617 * "reg_recording()" function
9618 */
9619 static void
9620f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9621{
9622 return_register(reg_recording, rettv);
9623}
9624
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009625#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009626/*
9627 * Convert a List to proftime_T.
9628 * Return FAIL when there is something wrong.
9629 */
9630 static int
9631list2proftime(typval_T *arg, proftime_T *tm)
9632{
9633 long n1, n2;
9634 int error = FALSE;
9635
9636 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9637 || arg->vval.v_list->lv_len != 2)
9638 return FAIL;
9639 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9640 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009641# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009642 tm->HighPart = n1;
9643 tm->LowPart = n2;
9644# else
9645 tm->tv_sec = n1;
9646 tm->tv_usec = n2;
9647# endif
9648 return error ? FAIL : OK;
9649}
9650#endif /* FEAT_RELTIME */
9651
9652/*
9653 * "reltime()" function
9654 */
9655 static void
9656f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9657{
9658#ifdef FEAT_RELTIME
9659 proftime_T res;
9660 proftime_T start;
9661
9662 if (argvars[0].v_type == VAR_UNKNOWN)
9663 {
9664 /* No arguments: get current time. */
9665 profile_start(&res);
9666 }
9667 else if (argvars[1].v_type == VAR_UNKNOWN)
9668 {
9669 if (list2proftime(&argvars[0], &res) == FAIL)
9670 return;
9671 profile_end(&res);
9672 }
9673 else
9674 {
9675 /* Two arguments: compute the difference. */
9676 if (list2proftime(&argvars[0], &start) == FAIL
9677 || list2proftime(&argvars[1], &res) == FAIL)
9678 return;
9679 profile_sub(&res, &start);
9680 }
9681
9682 if (rettv_list_alloc(rettv) == OK)
9683 {
9684 long n1, n2;
9685
Bram Moolenaar4f974752019-02-17 17:44:42 +01009686# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009687 n1 = res.HighPart;
9688 n2 = res.LowPart;
9689# else
9690 n1 = res.tv_sec;
9691 n2 = res.tv_usec;
9692# endif
9693 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9694 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9695 }
9696#endif
9697}
9698
9699#ifdef FEAT_FLOAT
9700/*
9701 * "reltimefloat()" function
9702 */
9703 static void
9704f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9705{
9706# ifdef FEAT_RELTIME
9707 proftime_T tm;
9708# endif
9709
9710 rettv->v_type = VAR_FLOAT;
9711 rettv->vval.v_float = 0;
9712# ifdef FEAT_RELTIME
9713 if (list2proftime(&argvars[0], &tm) == OK)
9714 rettv->vval.v_float = profile_float(&tm);
9715# endif
9716}
9717#endif
9718
9719/*
9720 * "reltimestr()" function
9721 */
9722 static void
9723f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9724{
9725#ifdef FEAT_RELTIME
9726 proftime_T tm;
9727#endif
9728
9729 rettv->v_type = VAR_STRING;
9730 rettv->vval.v_string = NULL;
9731#ifdef FEAT_RELTIME
9732 if (list2proftime(&argvars[0], &tm) == OK)
9733 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9734#endif
9735}
9736
9737#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009738 static void
9739make_connection(void)
9740{
9741 if (X_DISPLAY == NULL
9742# ifdef FEAT_GUI
9743 && !gui.in_use
9744# endif
9745 )
9746 {
9747 x_force_connect = TRUE;
9748 setup_term_clip();
9749 x_force_connect = FALSE;
9750 }
9751}
9752
9753 static int
9754check_connection(void)
9755{
9756 make_connection();
9757 if (X_DISPLAY == NULL)
9758 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009759 emsg(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009760 return FAIL;
9761 }
9762 return OK;
9763}
9764#endif
9765
9766#ifdef FEAT_CLIENTSERVER
9767 static void
9768remote_common(typval_T *argvars, typval_T *rettv, int expr)
9769{
9770 char_u *server_name;
9771 char_u *keys;
9772 char_u *r = NULL;
9773 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009774 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009775# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009776 HWND w;
9777# else
9778 Window w;
9779# endif
9780
9781 if (check_restricted() || check_secure())
9782 return;
9783
9784# ifdef FEAT_X11
9785 if (check_connection() == FAIL)
9786 return;
9787# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009788 if (argvars[2].v_type != VAR_UNKNOWN
9789 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009790 timeout = tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009791
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009792 server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009793 if (server_name == NULL)
9794 return; /* type error; errmsg already given */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009795 keys = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009796# ifdef MSWIN
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009797 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009798# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009799 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9800 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009801# endif
9802 {
9803 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009804 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009805 emsg((char *)r); // sending worked but evaluation failed
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009806 vim_free(r);
9807 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009808 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009809 semsg(_("E241: Unable to send to %s"), server_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009810 return;
9811 }
9812
9813 rettv->vval.v_string = r;
9814
9815 if (argvars[2].v_type != VAR_UNKNOWN)
9816 {
9817 dictitem_T v;
9818 char_u str[30];
9819 char_u *idvar;
9820
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009821 idvar = tv_get_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009822 if (idvar != NULL && *idvar != NUL)
9823 {
9824 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9825 v.di_tv.v_type = VAR_STRING;
9826 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009827 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009828 vim_free(v.di_tv.vval.v_string);
9829 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009830 }
9831}
9832#endif
9833
9834/*
9835 * "remote_expr()" function
9836 */
9837 static void
9838f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9839{
9840 rettv->v_type = VAR_STRING;
9841 rettv->vval.v_string = NULL;
9842#ifdef FEAT_CLIENTSERVER
9843 remote_common(argvars, rettv, TRUE);
9844#endif
9845}
9846
9847/*
9848 * "remote_foreground()" function
9849 */
9850 static void
9851f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9852{
9853#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +01009854# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009855 /* On Win32 it's done in this application. */
9856 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009857 char_u *server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009858
9859 if (server_name != NULL)
9860 serverForeground(server_name);
9861 }
9862# else
9863 /* Send a foreground() expression to the server. */
9864 argvars[1].v_type = VAR_STRING;
9865 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9866 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009867 rettv->v_type = VAR_STRING;
9868 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009869 remote_common(argvars, rettv, TRUE);
9870 vim_free(argvars[1].vval.v_string);
9871# endif
9872#endif
9873}
9874
9875 static void
9876f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
9877{
9878#ifdef FEAT_CLIENTSERVER
9879 dictitem_T v;
9880 char_u *s = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009881# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009882 long_u n = 0;
9883# endif
9884 char_u *serverid;
9885
9886 if (check_restricted() || check_secure())
9887 {
9888 rettv->vval.v_number = -1;
9889 return;
9890 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009891 serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009892 if (serverid == NULL)
9893 {
9894 rettv->vval.v_number = -1;
9895 return; /* type error; errmsg already given */
9896 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01009897# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009898 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
9899 if (n == 0)
9900 rettv->vval.v_number = -1;
9901 else
9902 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009903 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009904 rettv->vval.v_number = (s != NULL);
9905 }
9906# else
9907 if (check_connection() == FAIL)
9908 return;
9909
9910 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
9911 serverStrToWin(serverid), &s);
9912# endif
9913
9914 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
9915 {
9916 char_u *retvar;
9917
9918 v.di_tv.v_type = VAR_STRING;
9919 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009920 retvar = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009921 if (retvar != NULL)
9922 set_var(retvar, &v.di_tv, FALSE);
9923 vim_free(v.di_tv.vval.v_string);
9924 }
9925#else
9926 rettv->vval.v_number = -1;
9927#endif
9928}
9929
9930 static void
9931f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
9932{
9933 char_u *r = NULL;
9934
9935#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009936 char_u *serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009937
9938 if (serverid != NULL && !check_restricted() && !check_secure())
9939 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009940 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009941# ifdef MSWIN
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009942 /* The server's HWND is encoded in the 'id' parameter */
9943 long_u n = 0;
9944# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009945
9946 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009947 timeout = tv_get_number(&argvars[1]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009948
Bram Moolenaar4f974752019-02-17 17:44:42 +01009949# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009950 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
9951 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009952 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009953 if (r == NULL)
9954# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009955 if (check_connection() == FAIL
9956 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
9957 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009958# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009959 emsg(_("E277: Unable to read a server reply"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009960 }
9961#endif
9962 rettv->v_type = VAR_STRING;
9963 rettv->vval.v_string = r;
9964}
9965
9966/*
9967 * "remote_send()" function
9968 */
9969 static void
9970f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9971{
9972 rettv->v_type = VAR_STRING;
9973 rettv->vval.v_string = NULL;
9974#ifdef FEAT_CLIENTSERVER
9975 remote_common(argvars, rettv, FALSE);
9976#endif
9977}
9978
9979/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009980 * "remote_startserver()" function
9981 */
9982 static void
9983f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9984{
9985#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009986 char_u *server = tv_get_string_chk(&argvars[0]);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009987
9988 if (server == NULL)
9989 return; /* type error; errmsg already given */
9990 if (serverName != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009991 emsg(_("E941: already started a server"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009992 else
9993 {
9994# ifdef FEAT_X11
9995 if (check_connection() == OK)
9996 serverRegisterName(X_DISPLAY, server);
9997# else
9998 serverSetName(server);
9999# endif
10000 }
10001#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010002 emsg(_("E942: +clientserver feature not available"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010003#endif
10004}
10005
10006/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010007 * "remove()" function
10008 */
10009 static void
10010f_remove(typval_T *argvars, typval_T *rettv)
10011{
10012 list_T *l;
10013 listitem_T *item, *item2;
10014 listitem_T *li;
10015 long idx;
10016 long end;
10017 char_u *key;
10018 dict_T *d;
10019 dictitem_T *di;
10020 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010021 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010022
10023 if (argvars[0].v_type == VAR_DICT)
10024 {
10025 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010026 semsg(_(e_toomanyarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010027 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010028 && !var_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010029 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010030 key = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010031 if (key != NULL)
10032 {
10033 di = dict_find(d, key, -1);
10034 if (di == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010035 semsg(_(e_dictkey), key);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010036 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10037 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
10038 {
10039 *rettv = di->di_tv;
10040 init_tv(&di->di_tv);
10041 dictitem_remove(d, di);
10042 }
10043 }
10044 }
10045 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010046 else if (argvars[0].v_type == VAR_BLOB)
10047 {
10048 idx = (long)tv_get_number_chk(&argvars[1], &error);
10049 if (!error)
10050 {
10051 blob_T *b = argvars[0].vval.v_blob;
10052 int len = blob_len(b);
10053 char_u *p;
10054
10055 if (idx < 0)
10056 // count from the end
10057 idx = len + idx;
10058 if (idx < 0 || idx >= len)
10059 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010060 semsg(_(e_blobidx), idx);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010061 return;
10062 }
10063 if (argvars[2].v_type == VAR_UNKNOWN)
10064 {
10065 // Remove one item, return its value.
10066 p = (char_u *)b->bv_ga.ga_data;
10067 rettv->vval.v_number = (varnumber_T) *(p + idx);
10068 mch_memmove(p + idx, p + idx + 1, (size_t)len - idx - 1);
10069 --b->bv_ga.ga_len;
10070 }
10071 else
10072 {
10073 blob_T *blob;
10074
10075 // Remove range of items, return list with values.
10076 end = (long)tv_get_number_chk(&argvars[2], &error);
10077 if (error)
10078 return;
10079 if (end < 0)
10080 // count from the end
10081 end = len + end;
10082 if (end >= len || idx > end)
10083 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010084 semsg(_(e_blobidx), end);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010085 return;
10086 }
10087 blob = blob_alloc();
10088 if (blob == NULL)
10089 return;
10090 blob->bv_ga.ga_len = end - idx + 1;
10091 if (ga_grow(&blob->bv_ga, end - idx + 1) == FAIL)
10092 {
10093 vim_free(blob);
10094 return;
10095 }
10096 p = (char_u *)b->bv_ga.ga_data;
10097 mch_memmove((char_u *)blob->bv_ga.ga_data, p + idx,
10098 (size_t)(end - idx + 1));
10099 ++blob->bv_refcount;
10100 rettv->v_type = VAR_BLOB;
10101 rettv->vval.v_blob = blob;
10102
10103 mch_memmove(p + idx, p + end + 1, (size_t)(len - end));
10104 b->bv_ga.ga_len -= end - idx + 1;
10105 }
10106 }
10107 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010108 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010109 semsg(_(e_listdictblobarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010110 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010111 && !var_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010112 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010113 idx = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010114 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010115 ; // type error: do nothing, errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010116 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010117 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010118 else
10119 {
10120 if (argvars[2].v_type == VAR_UNKNOWN)
10121 {
10122 /* Remove one item, return its value. */
10123 vimlist_remove(l, item, item);
10124 *rettv = item->li_tv;
10125 vim_free(item);
10126 }
10127 else
10128 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010129 // Remove range of items, return list with values.
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010130 end = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010131 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010132 ; // type error: do nothing
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010133 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010134 semsg(_(e_listidx), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010135 else
10136 {
10137 int cnt = 0;
10138
10139 for (li = item; li != NULL; li = li->li_next)
10140 {
10141 ++cnt;
10142 if (li == item2)
10143 break;
10144 }
10145 if (li == NULL) /* didn't find "item2" after "item" */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010146 emsg(_(e_invrange));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010147 else
10148 {
10149 vimlist_remove(l, item, item2);
10150 if (rettv_list_alloc(rettv) == OK)
10151 {
10152 l = rettv->vval.v_list;
10153 l->lv_first = item;
10154 l->lv_last = item2;
10155 item->li_prev = NULL;
10156 item2->li_next = NULL;
10157 l->lv_len = cnt;
10158 }
10159 }
10160 }
10161 }
10162 }
10163 }
10164}
10165
10166/*
10167 * "rename({from}, {to})" function
10168 */
10169 static void
10170f_rename(typval_T *argvars, typval_T *rettv)
10171{
10172 char_u buf[NUMBUFLEN];
10173
10174 if (check_restricted() || check_secure())
10175 rettv->vval.v_number = -1;
10176 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010177 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
10178 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010179}
10180
10181/*
10182 * "repeat()" function
10183 */
10184 static void
10185f_repeat(typval_T *argvars, typval_T *rettv)
10186{
10187 char_u *p;
10188 int n;
10189 int slen;
10190 int len;
10191 char_u *r;
10192 int i;
10193
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010194 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010195 if (argvars[0].v_type == VAR_LIST)
10196 {
10197 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
10198 while (n-- > 0)
10199 if (list_extend(rettv->vval.v_list,
10200 argvars[0].vval.v_list, NULL) == FAIL)
10201 break;
10202 }
10203 else
10204 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010205 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010206 rettv->v_type = VAR_STRING;
10207 rettv->vval.v_string = NULL;
10208
10209 slen = (int)STRLEN(p);
10210 len = slen * n;
10211 if (len <= 0)
10212 return;
10213
10214 r = alloc(len + 1);
10215 if (r != NULL)
10216 {
10217 for (i = 0; i < n; i++)
10218 mch_memmove(r + i * slen, p, (size_t)slen);
10219 r[len] = NUL;
10220 }
10221
10222 rettv->vval.v_string = r;
10223 }
10224}
10225
10226/*
10227 * "resolve()" function
10228 */
10229 static void
10230f_resolve(typval_T *argvars, typval_T *rettv)
10231{
10232 char_u *p;
10233#ifdef HAVE_READLINK
10234 char_u *buf = NULL;
10235#endif
10236
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010237 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010238#ifdef FEAT_SHORTCUT
10239 {
10240 char_u *v = NULL;
10241
Bram Moolenaardce1e892019-02-10 23:18:53 +010010242 v = mch_resolve_path(p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010243 if (v != NULL)
10244 rettv->vval.v_string = v;
10245 else
10246 rettv->vval.v_string = vim_strsave(p);
10247 }
10248#else
10249# ifdef HAVE_READLINK
10250 {
10251 char_u *cpy;
10252 int len;
10253 char_u *remain = NULL;
10254 char_u *q;
10255 int is_relative_to_current = FALSE;
10256 int has_trailing_pathsep = FALSE;
10257 int limit = 100;
10258
10259 p = vim_strsave(p);
10260
10261 if (p[0] == '.' && (vim_ispathsep(p[1])
10262 || (p[1] == '.' && (vim_ispathsep(p[2])))))
10263 is_relative_to_current = TRUE;
10264
10265 len = STRLEN(p);
10266 if (len > 0 && after_pathsep(p, p + len))
10267 {
10268 has_trailing_pathsep = TRUE;
10269 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
10270 }
10271
10272 q = getnextcomp(p);
10273 if (*q != NUL)
10274 {
10275 /* Separate the first path component in "p", and keep the
10276 * remainder (beginning with the path separator). */
10277 remain = vim_strsave(q - 1);
10278 q[-1] = NUL;
10279 }
10280
10281 buf = alloc(MAXPATHL + 1);
10282 if (buf == NULL)
10283 goto fail;
10284
10285 for (;;)
10286 {
10287 for (;;)
10288 {
10289 len = readlink((char *)p, (char *)buf, MAXPATHL);
10290 if (len <= 0)
10291 break;
10292 buf[len] = NUL;
10293
10294 if (limit-- == 0)
10295 {
10296 vim_free(p);
10297 vim_free(remain);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010298 emsg(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010299 rettv->vval.v_string = NULL;
10300 goto fail;
10301 }
10302
10303 /* Ensure that the result will have a trailing path separator
10304 * if the argument has one. */
10305 if (remain == NULL && has_trailing_pathsep)
10306 add_pathsep(buf);
10307
10308 /* Separate the first path component in the link value and
10309 * concatenate the remainders. */
10310 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
10311 if (*q != NUL)
10312 {
10313 if (remain == NULL)
10314 remain = vim_strsave(q - 1);
10315 else
10316 {
10317 cpy = concat_str(q - 1, remain);
10318 if (cpy != NULL)
10319 {
10320 vim_free(remain);
10321 remain = cpy;
10322 }
10323 }
10324 q[-1] = NUL;
10325 }
10326
10327 q = gettail(p);
10328 if (q > p && *q == NUL)
10329 {
10330 /* Ignore trailing path separator. */
10331 q[-1] = NUL;
10332 q = gettail(p);
10333 }
10334 if (q > p && !mch_isFullName(buf))
10335 {
10336 /* symlink is relative to directory of argument */
10337 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
10338 if (cpy != NULL)
10339 {
10340 STRCPY(cpy, p);
10341 STRCPY(gettail(cpy), buf);
10342 vim_free(p);
10343 p = cpy;
10344 }
10345 }
10346 else
10347 {
10348 vim_free(p);
10349 p = vim_strsave(buf);
10350 }
10351 }
10352
10353 if (remain == NULL)
10354 break;
10355
10356 /* Append the first path component of "remain" to "p". */
10357 q = getnextcomp(remain + 1);
10358 len = q - remain - (*q != NUL);
10359 cpy = vim_strnsave(p, STRLEN(p) + len);
10360 if (cpy != NULL)
10361 {
10362 STRNCAT(cpy, remain, len);
10363 vim_free(p);
10364 p = cpy;
10365 }
10366 /* Shorten "remain". */
10367 if (*q != NUL)
10368 STRMOVE(remain, q - 1);
10369 else
Bram Moolenaard23a8232018-02-10 18:45:26 +010010370 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010371 }
10372
10373 /* If the result is a relative path name, make it explicitly relative to
10374 * the current directory if and only if the argument had this form. */
10375 if (!vim_ispathsep(*p))
10376 {
10377 if (is_relative_to_current
10378 && *p != NUL
10379 && !(p[0] == '.'
10380 && (p[1] == NUL
10381 || vim_ispathsep(p[1])
10382 || (p[1] == '.'
10383 && (p[2] == NUL
10384 || vim_ispathsep(p[2]))))))
10385 {
10386 /* Prepend "./". */
10387 cpy = concat_str((char_u *)"./", p);
10388 if (cpy != NULL)
10389 {
10390 vim_free(p);
10391 p = cpy;
10392 }
10393 }
10394 else if (!is_relative_to_current)
10395 {
10396 /* Strip leading "./". */
10397 q = p;
10398 while (q[0] == '.' && vim_ispathsep(q[1]))
10399 q += 2;
10400 if (q > p)
10401 STRMOVE(p, p + 2);
10402 }
10403 }
10404
10405 /* Ensure that the result will have no trailing path separator
10406 * if the argument had none. But keep "/" or "//". */
10407 if (!has_trailing_pathsep)
10408 {
10409 q = p + STRLEN(p);
10410 if (after_pathsep(p, q))
10411 *gettail_sep(p) = NUL;
10412 }
10413
10414 rettv->vval.v_string = p;
10415 }
10416# else
10417 rettv->vval.v_string = vim_strsave(p);
10418# endif
10419#endif
10420
10421 simplify_filename(rettv->vval.v_string);
10422
10423#ifdef HAVE_READLINK
10424fail:
10425 vim_free(buf);
10426#endif
10427 rettv->v_type = VAR_STRING;
10428}
10429
10430/*
10431 * "reverse({list})" function
10432 */
10433 static void
10434f_reverse(typval_T *argvars, typval_T *rettv)
10435{
10436 list_T *l;
10437 listitem_T *li, *ni;
10438
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010439 if (argvars[0].v_type == VAR_BLOB)
10440 {
10441 blob_T *b = argvars[0].vval.v_blob;
10442 int i, len = blob_len(b);
10443
10444 for (i = 0; i < len / 2; i++)
10445 {
10446 int tmp = blob_get(b, i);
10447
10448 blob_set(b, i, blob_get(b, len - i - 1));
10449 blob_set(b, len - i - 1, tmp);
10450 }
10451 rettv_blob_set(rettv, b);
10452 return;
10453 }
10454
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010455 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010456 semsg(_(e_listblobarg), "reverse()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010457 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010458 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010459 (char_u *)N_("reverse() argument"), TRUE))
10460 {
10461 li = l->lv_last;
10462 l->lv_first = l->lv_last = NULL;
10463 l->lv_len = 0;
10464 while (li != NULL)
10465 {
10466 ni = li->li_prev;
10467 list_append(l, li);
10468 li = ni;
10469 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020010470 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010471 l->lv_idx = l->lv_len - l->lv_idx - 1;
10472 }
10473}
10474
10475#define SP_NOMOVE 0x01 /* don't move cursor */
10476#define SP_REPEAT 0x02 /* repeat to find outer pair */
10477#define SP_RETCOUNT 0x04 /* return matchcount */
10478#define SP_SETPCMARK 0x08 /* set previous context mark */
10479#define SP_START 0x10 /* accept match at start position */
10480#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
10481#define SP_END 0x40 /* leave cursor at end of match */
10482#define SP_COLUMN 0x80 /* start at cursor column */
10483
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010484/*
10485 * Get flags for a search function.
10486 * Possibly sets "p_ws".
10487 * Returns BACKWARD, FORWARD or zero (for an error).
10488 */
10489 static int
10490get_search_arg(typval_T *varp, int *flagsp)
10491{
10492 int dir = FORWARD;
10493 char_u *flags;
10494 char_u nbuf[NUMBUFLEN];
10495 int mask;
10496
10497 if (varp->v_type != VAR_UNKNOWN)
10498 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010499 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010500 if (flags == NULL)
10501 return 0; /* type error; errmsg already given */
10502 while (*flags != NUL)
10503 {
10504 switch (*flags)
10505 {
10506 case 'b': dir = BACKWARD; break;
10507 case 'w': p_ws = TRUE; break;
10508 case 'W': p_ws = FALSE; break;
10509 default: mask = 0;
10510 if (flagsp != NULL)
10511 switch (*flags)
10512 {
10513 case 'c': mask = SP_START; break;
10514 case 'e': mask = SP_END; break;
10515 case 'm': mask = SP_RETCOUNT; break;
10516 case 'n': mask = SP_NOMOVE; break;
10517 case 'p': mask = SP_SUBPAT; break;
10518 case 'r': mask = SP_REPEAT; break;
10519 case 's': mask = SP_SETPCMARK; break;
10520 case 'z': mask = SP_COLUMN; break;
10521 }
10522 if (mask == 0)
10523 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010524 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010525 dir = 0;
10526 }
10527 else
10528 *flagsp |= mask;
10529 }
10530 if (dir == 0)
10531 break;
10532 ++flags;
10533 }
10534 }
10535 return dir;
10536}
10537
10538/*
10539 * Shared by search() and searchpos() functions.
10540 */
10541 static int
10542search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
10543{
10544 int flags;
10545 char_u *pat;
10546 pos_T pos;
10547 pos_T save_cursor;
10548 int save_p_ws = p_ws;
10549 int dir;
10550 int retval = 0; /* default: FAIL */
10551 long lnum_stop = 0;
10552 proftime_T tm;
10553#ifdef FEAT_RELTIME
10554 long time_limit = 0;
10555#endif
10556 int options = SEARCH_KEEP;
10557 int subpatnum;
10558
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010559 pat = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010560 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
10561 if (dir == 0)
10562 goto theend;
10563 flags = *flagsp;
10564 if (flags & SP_START)
10565 options |= SEARCH_START;
10566 if (flags & SP_END)
10567 options |= SEARCH_END;
10568 if (flags & SP_COLUMN)
10569 options |= SEARCH_COL;
10570
10571 /* Optional arguments: line number to stop searching and timeout. */
10572 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
10573 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010574 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010575 if (lnum_stop < 0)
10576 goto theend;
10577#ifdef FEAT_RELTIME
10578 if (argvars[3].v_type != VAR_UNKNOWN)
10579 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010580 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010581 if (time_limit < 0)
10582 goto theend;
10583 }
10584#endif
10585 }
10586
10587#ifdef FEAT_RELTIME
10588 /* Set the time limit, if there is one. */
10589 profile_setlimit(time_limit, &tm);
10590#endif
10591
10592 /*
10593 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
10594 * Check to make sure only those flags are set.
10595 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
10596 * flags cannot be set. Check for that condition also.
10597 */
10598 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
10599 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10600 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010601 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010602 goto theend;
10603 }
10604
10605 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010606 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010607 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010608 if (subpatnum != FAIL)
10609 {
10610 if (flags & SP_SUBPAT)
10611 retval = subpatnum;
10612 else
10613 retval = pos.lnum;
10614 if (flags & SP_SETPCMARK)
10615 setpcmark();
10616 curwin->w_cursor = pos;
10617 if (match_pos != NULL)
10618 {
10619 /* Store the match cursor position */
10620 match_pos->lnum = pos.lnum;
10621 match_pos->col = pos.col + 1;
10622 }
10623 /* "/$" will put the cursor after the end of the line, may need to
10624 * correct that here */
10625 check_cursor();
10626 }
10627
10628 /* If 'n' flag is used: restore cursor position. */
10629 if (flags & SP_NOMOVE)
10630 curwin->w_cursor = save_cursor;
10631 else
10632 curwin->w_set_curswant = TRUE;
10633theend:
10634 p_ws = save_p_ws;
10635
10636 return retval;
10637}
10638
10639#ifdef FEAT_FLOAT
10640
10641/*
10642 * round() is not in C90, use ceil() or floor() instead.
10643 */
10644 float_T
10645vim_round(float_T f)
10646{
10647 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
10648}
10649
10650/*
10651 * "round({float})" function
10652 */
10653 static void
10654f_round(typval_T *argvars, typval_T *rettv)
10655{
10656 float_T f = 0.0;
10657
10658 rettv->v_type = VAR_FLOAT;
10659 if (get_float_arg(argvars, &f) == OK)
10660 rettv->vval.v_float = vim_round(f);
10661 else
10662 rettv->vval.v_float = 0.0;
10663}
10664#endif
10665
Bram Moolenaare99be0e2019-03-26 22:51:09 +010010666#ifdef FEAT_RUBY
10667/*
10668 * "rubyeval()" function
10669 */
10670 static void
10671f_rubyeval(typval_T *argvars, typval_T *rettv)
10672{
10673 char_u *str;
10674 char_u buf[NUMBUFLEN];
10675
10676 str = tv_get_string_buf(&argvars[0], buf);
10677 do_rubyeval(str, rettv);
10678}
10679#endif
10680
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010681/*
10682 * "screenattr()" function
10683 */
10684 static void
10685f_screenattr(typval_T *argvars, typval_T *rettv)
10686{
10687 int row;
10688 int col;
10689 int c;
10690
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010691 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10692 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010693 if (row < 0 || row >= screen_Rows
10694 || col < 0 || col >= screen_Columns)
10695 c = -1;
10696 else
10697 c = ScreenAttrs[LineOffset[row] + col];
10698 rettv->vval.v_number = c;
10699}
10700
10701/*
10702 * "screenchar()" function
10703 */
10704 static void
10705f_screenchar(typval_T *argvars, typval_T *rettv)
10706{
10707 int row;
10708 int col;
10709 int off;
10710 int c;
10711
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010712 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10713 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010714 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010715 c = -1;
10716 else
10717 {
10718 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010719 if (enc_utf8 && ScreenLinesUC[off] != 0)
10720 c = ScreenLinesUC[off];
10721 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010722 c = ScreenLines[off];
10723 }
10724 rettv->vval.v_number = c;
10725}
10726
10727/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010728 * "screenchars()" function
10729 */
10730 static void
10731f_screenchars(typval_T *argvars, typval_T *rettv)
10732{
10733 int row;
10734 int col;
10735 int off;
10736 int c;
10737 int i;
10738
10739 if (rettv_list_alloc(rettv) == FAIL)
10740 return;
10741 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10742 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10743 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10744 return;
10745
10746 off = LineOffset[row] + col;
10747 if (enc_utf8 && ScreenLinesUC[off] != 0)
10748 c = ScreenLinesUC[off];
10749 else
10750 c = ScreenLines[off];
10751 list_append_number(rettv->vval.v_list, (varnumber_T)c);
10752
10753 if (enc_utf8)
10754
10755 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10756 list_append_number(rettv->vval.v_list,
10757 (varnumber_T)ScreenLinesC[i][off]);
10758}
10759
10760/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010761 * "screencol()" function
10762 *
10763 * First column is 1 to be consistent with virtcol().
10764 */
10765 static void
10766f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10767{
10768 rettv->vval.v_number = screen_screencol() + 1;
10769}
10770
10771/*
10772 * "screenrow()" function
10773 */
10774 static void
10775f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10776{
10777 rettv->vval.v_number = screen_screenrow() + 1;
10778}
10779
10780/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010781 * "screenstring()" function
10782 */
10783 static void
10784f_screenstring(typval_T *argvars, typval_T *rettv)
10785{
10786 int row;
10787 int col;
10788 int off;
10789 int c;
10790 int i;
10791 char_u buf[MB_MAXBYTES + 1];
10792 int buflen = 0;
10793
10794 rettv->vval.v_string = NULL;
10795 rettv->v_type = VAR_STRING;
10796
10797 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10798 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10799 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10800 return;
10801
10802 off = LineOffset[row] + col;
10803 if (enc_utf8 && ScreenLinesUC[off] != 0)
10804 c = ScreenLinesUC[off];
10805 else
10806 c = ScreenLines[off];
10807 buflen += mb_char2bytes(c, buf);
10808
10809 if (enc_utf8)
10810 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10811 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
10812
10813 buf[buflen] = NUL;
10814 rettv->vval.v_string = vim_strsave(buf);
10815}
10816
10817/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010818 * "search()" function
10819 */
10820 static void
10821f_search(typval_T *argvars, typval_T *rettv)
10822{
10823 int flags = 0;
10824
10825 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10826}
10827
10828/*
10829 * "searchdecl()" function
10830 */
10831 static void
10832f_searchdecl(typval_T *argvars, typval_T *rettv)
10833{
10834 int locally = 1;
10835 int thisblock = 0;
10836 int error = FALSE;
10837 char_u *name;
10838
10839 rettv->vval.v_number = 1; /* default: FAIL */
10840
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010841 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010842 if (argvars[1].v_type != VAR_UNKNOWN)
10843 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010844 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010845 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010846 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010847 }
10848 if (!error && name != NULL)
10849 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10850 locally, thisblock, SEARCH_KEEP) == FAIL;
10851}
10852
10853/*
10854 * Used by searchpair() and searchpairpos()
10855 */
10856 static int
10857searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10858{
10859 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010860 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010861 int save_p_ws = p_ws;
10862 int dir;
10863 int flags = 0;
10864 char_u nbuf1[NUMBUFLEN];
10865 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010866 int retval = 0; /* default: FAIL */
10867 long lnum_stop = 0;
10868 long time_limit = 0;
10869
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010870 /* Get the three pattern arguments: start, middle, end. Will result in an
10871 * error if not a valid argument. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010872 spat = tv_get_string_chk(&argvars[0]);
10873 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
10874 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010875 if (spat == NULL || mpat == NULL || epat == NULL)
10876 goto theend; /* type error */
10877
10878 /* Handle the optional fourth argument: flags */
10879 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
10880 if (dir == 0)
10881 goto theend;
10882
10883 /* Don't accept SP_END or SP_SUBPAT.
10884 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
10885 */
10886 if ((flags & (SP_END | SP_SUBPAT)) != 0
10887 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10888 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010889 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010890 goto theend;
10891 }
10892
10893 /* Using 'r' implies 'W', otherwise it doesn't work. */
10894 if (flags & SP_REPEAT)
10895 p_ws = FALSE;
10896
10897 /* Optional fifth argument: skip expression */
10898 if (argvars[3].v_type == VAR_UNKNOWN
10899 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010010900 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010901 else
10902 {
Bram Moolenaar48570482017-10-30 21:48:41 +010010903 skip = &argvars[4];
10904 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
10905 && skip->v_type != VAR_STRING)
10906 {
10907 /* Type error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010908 semsg(_(e_invarg2), tv_get_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010010909 goto theend;
10910 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010911 if (argvars[5].v_type != VAR_UNKNOWN)
10912 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010913 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010914 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010915 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010916 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010917 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010918 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010919#ifdef FEAT_RELTIME
10920 if (argvars[6].v_type != VAR_UNKNOWN)
10921 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010922 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010923 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010924 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010925 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010926 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010927 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010928 }
10929#endif
10930 }
10931 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010932
10933 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
10934 match_pos, lnum_stop, time_limit);
10935
10936theend:
10937 p_ws = save_p_ws;
10938
10939 return retval;
10940}
10941
10942/*
10943 * "searchpair()" function
10944 */
10945 static void
10946f_searchpair(typval_T *argvars, typval_T *rettv)
10947{
10948 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
10949}
10950
10951/*
10952 * "searchpairpos()" function
10953 */
10954 static void
10955f_searchpairpos(typval_T *argvars, typval_T *rettv)
10956{
10957 pos_T match_pos;
10958 int lnum = 0;
10959 int col = 0;
10960
10961 if (rettv_list_alloc(rettv) == FAIL)
10962 return;
10963
10964 if (searchpair_cmn(argvars, &match_pos) > 0)
10965 {
10966 lnum = match_pos.lnum;
10967 col = match_pos.col;
10968 }
10969
10970 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10971 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10972}
10973
10974/*
10975 * Search for a start/middle/end thing.
10976 * Used by searchpair(), see its documentation for the details.
10977 * Returns 0 or -1 for no match,
10978 */
10979 long
10980do_searchpair(
10981 char_u *spat, /* start pattern */
10982 char_u *mpat, /* middle pattern */
10983 char_u *epat, /* end pattern */
10984 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010010985 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010986 int flags, /* SP_SETPCMARK and other SP_ values */
10987 pos_T *match_pos,
10988 linenr_T lnum_stop, /* stop at this line if not zero */
10989 long time_limit UNUSED) /* stop after this many msec */
10990{
10991 char_u *save_cpo;
10992 char_u *pat, *pat2 = NULL, *pat3 = NULL;
10993 long retval = 0;
10994 pos_T pos;
10995 pos_T firstpos;
10996 pos_T foundpos;
10997 pos_T save_cursor;
10998 pos_T save_pos;
10999 int n;
11000 int r;
11001 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010011002 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011003 int err;
11004 int options = SEARCH_KEEP;
11005 proftime_T tm;
11006
11007 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11008 save_cpo = p_cpo;
11009 p_cpo = empty_option;
11010
11011#ifdef FEAT_RELTIME
11012 /* Set the time limit, if there is one. */
11013 profile_setlimit(time_limit, &tm);
11014#endif
11015
11016 /* Make two search patterns: start/end (pat2, for in nested pairs) and
11017 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +010011018 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
11019 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011020 if (pat2 == NULL || pat3 == NULL)
11021 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010011022 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011023 if (*mpat == NUL)
11024 STRCPY(pat3, pat2);
11025 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010011026 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011027 spat, epat, mpat);
11028 if (flags & SP_START)
11029 options |= SEARCH_START;
11030
Bram Moolenaar48570482017-10-30 21:48:41 +010011031 if (skip != NULL)
11032 {
11033 /* Empty string means to not use the skip expression. */
11034 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
11035 use_skip = skip->vval.v_string != NULL
11036 && *skip->vval.v_string != NUL;
11037 }
11038
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011039 save_cursor = curwin->w_cursor;
11040 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011041 CLEAR_POS(&firstpos);
11042 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011043 pat = pat3;
11044 for (;;)
11045 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +010011046 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020011047 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011048 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011049 /* didn't find it or found the first match again: FAIL */
11050 break;
11051
11052 if (firstpos.lnum == 0)
11053 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011054 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011055 {
11056 /* Found the same position again. Can happen with a pattern that
11057 * has "\zs" at the end and searching backwards. Advance one
11058 * character and try again. */
11059 if (dir == BACKWARD)
11060 decl(&pos);
11061 else
11062 incl(&pos);
11063 }
11064 foundpos = pos;
11065
11066 /* clear the start flag to avoid getting stuck here */
11067 options &= ~SEARCH_START;
11068
11069 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010011070 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011071 {
11072 save_pos = curwin->w_cursor;
11073 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010011074 err = FALSE;
11075 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011076 curwin->w_cursor = save_pos;
11077 if (err)
11078 {
11079 /* Evaluating {skip} caused an error, break here. */
11080 curwin->w_cursor = save_cursor;
11081 retval = -1;
11082 break;
11083 }
11084 if (r)
11085 continue;
11086 }
11087
11088 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
11089 {
11090 /* Found end when searching backwards or start when searching
11091 * forward: nested pair. */
11092 ++nest;
11093 pat = pat2; /* nested, don't search for middle */
11094 }
11095 else
11096 {
11097 /* Found end when searching forward or start when searching
11098 * backward: end of (nested) pair; or found middle in outer pair. */
11099 if (--nest == 1)
11100 pat = pat3; /* outer level, search for middle */
11101 }
11102
11103 if (nest == 0)
11104 {
11105 /* Found the match: return matchcount or line number. */
11106 if (flags & SP_RETCOUNT)
11107 ++retval;
11108 else
11109 retval = pos.lnum;
11110 if (flags & SP_SETPCMARK)
11111 setpcmark();
11112 curwin->w_cursor = pos;
11113 if (!(flags & SP_REPEAT))
11114 break;
11115 nest = 1; /* search for next unmatched */
11116 }
11117 }
11118
11119 if (match_pos != NULL)
11120 {
11121 /* Store the match cursor position */
11122 match_pos->lnum = curwin->w_cursor.lnum;
11123 match_pos->col = curwin->w_cursor.col + 1;
11124 }
11125
11126 /* If 'n' flag is used or search failed: restore cursor position. */
11127 if ((flags & SP_NOMOVE) || retval == 0)
11128 curwin->w_cursor = save_cursor;
11129
11130theend:
11131 vim_free(pat2);
11132 vim_free(pat3);
11133 if (p_cpo == empty_option)
11134 p_cpo = save_cpo;
11135 else
11136 /* Darn, evaluating the {skip} expression changed the value. */
11137 free_string_option(save_cpo);
11138
11139 return retval;
11140}
11141
11142/*
11143 * "searchpos()" function
11144 */
11145 static void
11146f_searchpos(typval_T *argvars, typval_T *rettv)
11147{
11148 pos_T match_pos;
11149 int lnum = 0;
11150 int col = 0;
11151 int n;
11152 int flags = 0;
11153
11154 if (rettv_list_alloc(rettv) == FAIL)
11155 return;
11156
11157 n = search_cmn(argvars, &match_pos, &flags);
11158 if (n > 0)
11159 {
11160 lnum = match_pos.lnum;
11161 col = match_pos.col;
11162 }
11163
11164 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
11165 list_append_number(rettv->vval.v_list, (varnumber_T)col);
11166 if (flags & SP_SUBPAT)
11167 list_append_number(rettv->vval.v_list, (varnumber_T)n);
11168}
11169
11170 static void
11171f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
11172{
11173#ifdef FEAT_CLIENTSERVER
11174 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011175 char_u *server = tv_get_string_chk(&argvars[0]);
11176 char_u *reply = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011177
11178 rettv->vval.v_number = -1;
11179 if (server == NULL || reply == NULL)
11180 return;
11181 if (check_restricted() || check_secure())
11182 return;
11183# ifdef FEAT_X11
11184 if (check_connection() == FAIL)
11185 return;
11186# endif
11187
11188 if (serverSendReply(server, reply) < 0)
11189 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011190 emsg(_("E258: Unable to send to client"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011191 return;
11192 }
11193 rettv->vval.v_number = 0;
11194#else
11195 rettv->vval.v_number = -1;
11196#endif
11197}
11198
11199 static void
11200f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
11201{
11202 char_u *r = NULL;
11203
11204#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +010011205# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011206 r = serverGetVimNames();
11207# else
11208 make_connection();
11209 if (X_DISPLAY != NULL)
11210 r = serverGetVimNames(X_DISPLAY);
11211# endif
11212#endif
11213 rettv->v_type = VAR_STRING;
11214 rettv->vval.v_string = r;
11215}
11216
11217/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011218 * "setbufline()" function
11219 */
11220 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020011221f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011222{
11223 linenr_T lnum;
11224 buf_T *buf;
11225
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011226 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011227 if (buf == NULL)
11228 rettv->vval.v_number = 1; /* FAIL */
11229 else
11230 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011231 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020011232 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011233 }
11234}
11235
11236/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011237 * "setbufvar()" function
11238 */
11239 static void
11240f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
11241{
11242 buf_T *buf;
11243 char_u *varname, *bufvarname;
11244 typval_T *varp;
11245 char_u nbuf[NUMBUFLEN];
11246
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011247 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011248 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011249 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
11250 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011251 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011252 varp = &argvars[2];
11253
11254 if (buf != NULL && varname != NULL && varp != NULL)
11255 {
11256 if (*varname == '&')
11257 {
11258 long numval;
11259 char_u *strval;
11260 int error = FALSE;
11261 aco_save_T aco;
11262
11263 /* set curbuf to be our buf, temporarily */
11264 aucmd_prepbuf(&aco, buf);
11265
11266 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011267 numval = (long)tv_get_number_chk(varp, &error);
11268 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011269 if (!error && strval != NULL)
11270 set_option_value(varname, numval, strval, OPT_LOCAL);
11271
11272 /* reset notion of buffer */
11273 aucmd_restbuf(&aco);
11274 }
11275 else
11276 {
11277 buf_T *save_curbuf = curbuf;
11278
11279 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
11280 if (bufvarname != NULL)
11281 {
11282 curbuf = buf;
11283 STRCPY(bufvarname, "b:");
11284 STRCPY(bufvarname + 2, varname);
11285 set_var(bufvarname, varp, TRUE);
11286 vim_free(bufvarname);
11287 curbuf = save_curbuf;
11288 }
11289 }
11290 }
11291}
11292
11293 static void
11294f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
11295{
11296 dict_T *d;
11297 dictitem_T *di;
11298 char_u *csearch;
11299
11300 if (argvars[0].v_type != VAR_DICT)
11301 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011302 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011303 return;
11304 }
11305
11306 if ((d = argvars[0].vval.v_dict) != NULL)
11307 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010011308 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011309 if (csearch != NULL)
11310 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011311 if (enc_utf8)
11312 {
11313 int pcc[MAX_MCO];
11314 int c = utfc_ptr2char(csearch, pcc);
11315
11316 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
11317 }
11318 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011319 set_last_csearch(PTR2CHAR(csearch),
11320 csearch, MB_PTR2LEN(csearch));
11321 }
11322
11323 di = dict_find(d, (char_u *)"forward", -1);
11324 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011325 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011326 ? FORWARD : BACKWARD);
11327
11328 di = dict_find(d, (char_u *)"until", -1);
11329 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011330 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011331 }
11332}
11333
11334/*
11335 * "setcmdpos()" function
11336 */
11337 static void
11338f_setcmdpos(typval_T *argvars, typval_T *rettv)
11339{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011340 int pos = (int)tv_get_number(&argvars[0]) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011341
11342 if (pos >= 0)
11343 rettv->vval.v_number = set_cmdline_pos(pos);
11344}
11345
11346/*
11347 * "setfperm({fname}, {mode})" function
11348 */
11349 static void
11350f_setfperm(typval_T *argvars, typval_T *rettv)
11351{
11352 char_u *fname;
11353 char_u modebuf[NUMBUFLEN];
11354 char_u *mode_str;
11355 int i;
11356 int mask;
11357 int mode = 0;
11358
11359 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011360 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011361 if (fname == NULL)
11362 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011363 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011364 if (mode_str == NULL)
11365 return;
11366 if (STRLEN(mode_str) != 9)
11367 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011368 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011369 return;
11370 }
11371
11372 mask = 1;
11373 for (i = 8; i >= 0; --i)
11374 {
11375 if (mode_str[i] != '-')
11376 mode |= mask;
11377 mask = mask << 1;
11378 }
11379 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
11380}
11381
11382/*
11383 * "setline()" function
11384 */
11385 static void
11386f_setline(typval_T *argvars, typval_T *rettv)
11387{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011388 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011389
Bram Moolenaarca851592018-06-06 21:04:07 +020011390 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011391}
11392
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011393/*
11394 * Used by "setqflist()" and "setloclist()" functions
11395 */
11396 static void
11397set_qf_ll_list(
11398 win_T *wp UNUSED,
11399 typval_T *list_arg UNUSED,
11400 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020011401 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011402 typval_T *rettv)
11403{
11404#ifdef FEAT_QUICKFIX
11405 static char *e_invact = N_("E927: Invalid action: '%s'");
11406 char_u *act;
11407 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011408 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011409#endif
11410
11411 rettv->vval.v_number = -1;
11412
11413#ifdef FEAT_QUICKFIX
11414 if (list_arg->v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011415 emsg(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011416 else if (recursive != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011417 emsg(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011418 else
11419 {
11420 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011421 dict_T *d = NULL;
11422 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011423
11424 if (action_arg->v_type == VAR_STRING)
11425 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011426 act = tv_get_string_chk(action_arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011427 if (act == NULL)
11428 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020011429 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
11430 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011431 action = *act;
11432 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011433 semsg(_(e_invact), act);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011434 }
11435 else if (action_arg->v_type == VAR_UNKNOWN)
11436 action = ' ';
11437 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011438 emsg(_(e_stringreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011439
Bram Moolenaard823fa92016-08-12 16:29:27 +020011440 if (action_arg->v_type != VAR_UNKNOWN
11441 && what_arg->v_type != VAR_UNKNOWN)
11442 {
11443 if (what_arg->v_type == VAR_DICT)
11444 d = what_arg->vval.v_dict;
11445 else
11446 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011447 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +020011448 valid_dict = FALSE;
11449 }
11450 }
11451
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011452 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011453 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011454 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
11455 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011456 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011457 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011458 }
11459#endif
11460}
11461
11462/*
11463 * "setloclist()" function
11464 */
11465 static void
11466f_setloclist(typval_T *argvars, typval_T *rettv)
11467{
11468 win_T *win;
11469
11470 rettv->vval.v_number = -1;
11471
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020011472 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011473 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020011474 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011475}
11476
11477/*
11478 * "setmatches()" function
11479 */
11480 static void
11481f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
11482{
11483#ifdef FEAT_SEARCH_EXTRA
11484 list_T *l;
11485 listitem_T *li;
11486 dict_T *d;
11487 list_T *s = NULL;
Bram Moolenaaraff74912019-03-30 18:11:49 +010011488 win_T *win = get_optional_window(argvars, 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011489
11490 rettv->vval.v_number = -1;
11491 if (argvars[0].v_type != VAR_LIST)
11492 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011493 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011494 return;
11495 }
Bram Moolenaaraff74912019-03-30 18:11:49 +010011496 if (win == NULL)
11497 return;
11498
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011499 if ((l = argvars[0].vval.v_list) != NULL)
11500 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011501 /* To some extent make sure that we are dealing with a list from
11502 * "getmatches()". */
11503 li = l->lv_first;
11504 while (li != NULL)
11505 {
11506 if (li->li_tv.v_type != VAR_DICT
11507 || (d = li->li_tv.vval.v_dict) == NULL)
11508 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011509 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011510 return;
11511 }
11512 if (!(dict_find(d, (char_u *)"group", -1) != NULL
11513 && (dict_find(d, (char_u *)"pattern", -1) != NULL
11514 || dict_find(d, (char_u *)"pos1", -1) != NULL)
11515 && dict_find(d, (char_u *)"priority", -1) != NULL
11516 && dict_find(d, (char_u *)"id", -1) != NULL))
11517 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011518 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011519 return;
11520 }
11521 li = li->li_next;
11522 }
11523
Bram Moolenaaraff74912019-03-30 18:11:49 +010011524 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011525 li = l->lv_first;
11526 while (li != NULL)
11527 {
11528 int i = 0;
11529 char_u buf[5];
11530 dictitem_T *di;
11531 char_u *group;
11532 int priority;
11533 int id;
11534 char_u *conceal;
11535
11536 d = li->li_tv.vval.v_dict;
11537 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
11538 {
11539 if (s == NULL)
11540 {
11541 s = list_alloc();
11542 if (s == NULL)
11543 return;
11544 }
11545
11546 /* match from matchaddpos() */
11547 for (i = 1; i < 9; i++)
11548 {
11549 sprintf((char *)buf, (char *)"pos%d", i);
11550 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
11551 {
11552 if (di->di_tv.v_type != VAR_LIST)
11553 return;
11554
11555 list_append_tv(s, &di->di_tv);
11556 s->lv_refcount++;
11557 }
11558 else
11559 break;
11560 }
11561 }
11562
Bram Moolenaar8f667172018-12-14 15:38:31 +010011563 group = dict_get_string(d, (char_u *)"group", TRUE);
11564 priority = (int)dict_get_number(d, (char_u *)"priority");
11565 id = (int)dict_get_number(d, (char_u *)"id");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011566 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar8f667172018-12-14 15:38:31 +010011567 ? dict_get_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011568 : NULL;
11569 if (i == 0)
11570 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011571 match_add(win, group,
Bram Moolenaar8f667172018-12-14 15:38:31 +010011572 dict_get_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011573 priority, id, NULL, conceal);
11574 }
11575 else
11576 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011577 match_add(win, group, NULL, priority, id, s, conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011578 list_unref(s);
11579 s = NULL;
11580 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020011581 vim_free(group);
11582 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011583
11584 li = li->li_next;
11585 }
11586 rettv->vval.v_number = 0;
11587 }
11588#endif
11589}
11590
11591/*
11592 * "setpos()" function
11593 */
11594 static void
11595f_setpos(typval_T *argvars, typval_T *rettv)
11596{
11597 pos_T pos;
11598 int fnum;
11599 char_u *name;
11600 colnr_T curswant = -1;
11601
11602 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011603 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011604 if (name != NULL)
11605 {
11606 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
11607 {
11608 if (--pos.col < 0)
11609 pos.col = 0;
11610 if (name[0] == '.' && name[1] == NUL)
11611 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011612 /* set cursor; "fnum" is ignored */
11613 curwin->w_cursor = pos;
11614 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011615 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011616 curwin->w_curswant = curswant - 1;
11617 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011618 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011619 check_cursor();
11620 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011621 }
11622 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
11623 {
11624 /* set mark */
11625 if (setmark_pos(name[1], &pos, fnum) == OK)
11626 rettv->vval.v_number = 0;
11627 }
11628 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011629 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011630 }
11631 }
11632}
11633
11634/*
11635 * "setqflist()" function
11636 */
11637 static void
11638f_setqflist(typval_T *argvars, typval_T *rettv)
11639{
Bram Moolenaard823fa92016-08-12 16:29:27 +020011640 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011641}
11642
11643/*
11644 * "setreg()" function
11645 */
11646 static void
11647f_setreg(typval_T *argvars, typval_T *rettv)
11648{
11649 int regname;
11650 char_u *strregname;
11651 char_u *stropt;
11652 char_u *strval;
11653 int append;
11654 char_u yank_type;
11655 long block_len;
11656
11657 block_len = -1;
11658 yank_type = MAUTO;
11659 append = FALSE;
11660
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011661 strregname = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011662 rettv->vval.v_number = 1; /* FAIL is default */
11663
11664 if (strregname == NULL)
11665 return; /* type error; errmsg already given */
11666 regname = *strregname;
11667 if (regname == 0 || regname == '@')
11668 regname = '"';
11669
11670 if (argvars[2].v_type != VAR_UNKNOWN)
11671 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011672 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011673 if (stropt == NULL)
11674 return; /* type error */
11675 for (; *stropt != NUL; ++stropt)
11676 switch (*stropt)
11677 {
11678 case 'a': case 'A': /* append */
11679 append = TRUE;
11680 break;
11681 case 'v': case 'c': /* character-wise selection */
11682 yank_type = MCHAR;
11683 break;
11684 case 'V': case 'l': /* line-wise selection */
11685 yank_type = MLINE;
11686 break;
11687 case 'b': case Ctrl_V: /* block-wise selection */
11688 yank_type = MBLOCK;
11689 if (VIM_ISDIGIT(stropt[1]))
11690 {
11691 ++stropt;
11692 block_len = getdigits(&stropt) - 1;
11693 --stropt;
11694 }
11695 break;
11696 }
11697 }
11698
11699 if (argvars[1].v_type == VAR_LIST)
11700 {
11701 char_u **lstval;
11702 char_u **allocval;
11703 char_u buf[NUMBUFLEN];
11704 char_u **curval;
11705 char_u **curallocval;
11706 list_T *ll = argvars[1].vval.v_list;
11707 listitem_T *li;
11708 int len;
11709
11710 /* If the list is NULL handle like an empty list. */
11711 len = ll == NULL ? 0 : ll->lv_len;
11712
11713 /* First half: use for pointers to result lines; second half: use for
11714 * pointers to allocated copies. */
11715 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
11716 if (lstval == NULL)
11717 return;
11718 curval = lstval;
11719 allocval = lstval + len + 2;
11720 curallocval = allocval;
11721
11722 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
11723 li = li->li_next)
11724 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011725 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011726 if (strval == NULL)
11727 goto free_lstval;
11728 if (strval == buf)
11729 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011730 /* Need to make a copy, next tv_get_string_buf_chk() will
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011731 * overwrite the string. */
11732 strval = vim_strsave(buf);
11733 if (strval == NULL)
11734 goto free_lstval;
11735 *curallocval++ = strval;
11736 }
11737 *curval++ = strval;
11738 }
11739 *curval++ = NULL;
11740
11741 write_reg_contents_lst(regname, lstval, -1,
11742 append, yank_type, block_len);
11743free_lstval:
11744 while (curallocval > allocval)
11745 vim_free(*--curallocval);
11746 vim_free(lstval);
11747 }
11748 else
11749 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011750 strval = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011751 if (strval == NULL)
11752 return;
11753 write_reg_contents_ex(regname, strval, -1,
11754 append, yank_type, block_len);
11755 }
11756 rettv->vval.v_number = 0;
11757}
11758
11759/*
11760 * "settabvar()" function
11761 */
11762 static void
11763f_settabvar(typval_T *argvars, typval_T *rettv)
11764{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011765 tabpage_T *save_curtab;
11766 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011767 char_u *varname, *tabvarname;
11768 typval_T *varp;
11769
11770 rettv->vval.v_number = 0;
11771
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011772 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011773 return;
11774
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011775 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
11776 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011777 varp = &argvars[2];
11778
Bram Moolenaar4033c552017-09-16 20:54:51 +020011779 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011780 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011781 save_curtab = curtab;
11782 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011783
11784 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
11785 if (tabvarname != NULL)
11786 {
11787 STRCPY(tabvarname, "t:");
11788 STRCPY(tabvarname + 2, varname);
11789 set_var(tabvarname, varp, TRUE);
11790 vim_free(tabvarname);
11791 }
11792
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011793 /* Restore current tabpage */
11794 if (valid_tabpage(save_curtab))
11795 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011796 }
11797}
11798
11799/*
11800 * "settabwinvar()" function
11801 */
11802 static void
11803f_settabwinvar(typval_T *argvars, typval_T *rettv)
11804{
11805 setwinvar(argvars, rettv, 1);
11806}
11807
11808/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011809 * "settagstack()" function
11810 */
11811 static void
11812f_settagstack(typval_T *argvars, typval_T *rettv)
11813{
11814 static char *e_invact2 = N_("E962: Invalid action: '%s'");
11815 win_T *wp;
11816 dict_T *d;
11817 int action = 'r';
11818
11819 rettv->vval.v_number = -1;
11820
11821 // first argument: window number or id
11822 wp = find_win_by_nr_or_id(&argvars[0]);
11823 if (wp == NULL)
11824 return;
11825
11826 // second argument: dict with items to set in the tag stack
11827 if (argvars[1].v_type != VAR_DICT)
11828 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011829 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011830 return;
11831 }
11832 d = argvars[1].vval.v_dict;
11833 if (d == NULL)
11834 return;
11835
11836 // third argument: action - 'a' for append and 'r' for replace.
11837 // default is to replace the stack.
11838 if (argvars[2].v_type == VAR_UNKNOWN)
11839 action = 'r';
11840 else if (argvars[2].v_type == VAR_STRING)
11841 {
11842 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011843 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011844 if (actstr == NULL)
11845 return;
11846 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
11847 action = *actstr;
11848 else
11849 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011850 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011851 return;
11852 }
11853 }
11854 else
11855 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011856 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011857 return;
11858 }
11859
11860 if (set_tagstack(wp, d, action) == OK)
11861 rettv->vval.v_number = 0;
11862}
11863
11864/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011865 * "setwinvar()" function
11866 */
11867 static void
11868f_setwinvar(typval_T *argvars, typval_T *rettv)
11869{
11870 setwinvar(argvars, rettv, 0);
11871}
11872
11873#ifdef FEAT_CRYPT
11874/*
11875 * "sha256({string})" function
11876 */
11877 static void
11878f_sha256(typval_T *argvars, typval_T *rettv)
11879{
11880 char_u *p;
11881
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011882 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011883 rettv->vval.v_string = vim_strsave(
11884 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
11885 rettv->v_type = VAR_STRING;
11886}
11887#endif /* FEAT_CRYPT */
11888
11889/*
11890 * "shellescape({string})" function
11891 */
11892 static void
11893f_shellescape(typval_T *argvars, typval_T *rettv)
11894{
Bram Moolenaar20615522017-06-05 18:46:26 +020011895 int do_special = non_zero_arg(&argvars[1]);
11896
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011897 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011898 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011899 rettv->v_type = VAR_STRING;
11900}
11901
11902/*
11903 * shiftwidth() function
11904 */
11905 static void
11906f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
11907{
Bram Moolenaarf9514162018-11-22 03:08:29 +010011908 rettv->vval.v_number = 0;
11909
11910 if (argvars[0].v_type != VAR_UNKNOWN)
11911 {
11912 long col;
11913
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011914 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +010011915 if (col < 0)
11916 return; // type error; errmsg already given
11917#ifdef FEAT_VARTABS
11918 rettv->vval.v_number = get_sw_value_col(curbuf, col);
11919 return;
11920#endif
11921 }
11922
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011923 rettv->vval.v_number = get_sw_value(curbuf);
11924}
11925
Bram Moolenaar162b7142018-12-21 15:17:36 +010011926#ifdef FEAT_SIGNS
11927/*
11928 * "sign_define()" function
11929 */
11930 static void
11931f_sign_define(typval_T *argvars, typval_T *rettv)
11932{
11933 char_u *name;
11934 dict_T *dict;
11935 char_u *icon = NULL;
11936 char_u *linehl = NULL;
11937 char_u *text = NULL;
11938 char_u *texthl = NULL;
11939
11940 rettv->vval.v_number = -1;
11941
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011942 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011943 if (name == NULL)
11944 return;
11945
11946 if (argvars[1].v_type != VAR_UNKNOWN)
11947 {
11948 if (argvars[1].v_type != VAR_DICT)
11949 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011950 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011951 return;
11952 }
11953
11954 // sign attributes
11955 dict = argvars[1].vval.v_dict;
11956 if (dict_find(dict, (char_u *)"icon", -1) != NULL)
11957 icon = dict_get_string(dict, (char_u *)"icon", TRUE);
11958 if (dict_find(dict, (char_u *)"linehl", -1) != NULL)
11959 linehl = dict_get_string(dict, (char_u *)"linehl", TRUE);
11960 if (dict_find(dict, (char_u *)"text", -1) != NULL)
11961 text = dict_get_string(dict, (char_u *)"text", TRUE);
11962 if (dict_find(dict, (char_u *)"texthl", -1) != NULL)
11963 texthl = dict_get_string(dict, (char_u *)"texthl", TRUE);
11964 }
11965
11966 if (sign_define_by_name(name, icon, linehl, text, texthl) == OK)
11967 rettv->vval.v_number = 0;
11968
11969 vim_free(icon);
11970 vim_free(linehl);
11971 vim_free(text);
11972 vim_free(texthl);
11973}
11974
11975/*
11976 * "sign_getdefined()" function
11977 */
11978 static void
11979f_sign_getdefined(typval_T *argvars, typval_T *rettv)
11980{
11981 char_u *name = NULL;
11982
11983 if (rettv_list_alloc_id(rettv, aid_sign_getdefined) != OK)
11984 return;
11985
11986 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011987 name = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011988
11989 sign_getlist(name, rettv->vval.v_list);
11990}
11991
11992/*
11993 * "sign_getplaced()" function
11994 */
11995 static void
11996f_sign_getplaced(typval_T *argvars, typval_T *rettv)
11997{
11998 buf_T *buf = NULL;
11999 dict_T *dict;
12000 dictitem_T *di;
12001 linenr_T lnum = 0;
12002 int sign_id = 0;
12003 char_u *group = NULL;
12004 int notanum = FALSE;
12005
12006 if (rettv_list_alloc_id(rettv, aid_sign_getplaced) != OK)
12007 return;
12008
12009 if (argvars[0].v_type != VAR_UNKNOWN)
12010 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012011 // get signs placed in the specified buffer
12012 buf = get_buf_arg(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012013 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012014 return;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012015
12016 if (argvars[1].v_type != VAR_UNKNOWN)
12017 {
12018 if (argvars[1].v_type != VAR_DICT ||
12019 ((dict = argvars[1].vval.v_dict) == NULL))
12020 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012021 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012022 return;
12023 }
12024 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
12025 {
12026 // get signs placed at this line
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012027 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012028 if (notanum)
12029 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012030 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012031 }
12032 if ((di = dict_find(dict, (char_u *)"id", -1)) != NULL)
12033 {
12034 // get sign placed with this identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012035 sign_id = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012036 if (notanum)
12037 return;
12038 }
12039 if ((di = dict_find(dict, (char_u *)"group", -1)) != NULL)
12040 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012041 group = tv_get_string_chk(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012042 if (group == NULL)
12043 return;
Bram Moolenaar6436cd82018-12-27 00:28:33 +010012044 if (*group == '\0') // empty string means global group
12045 group = NULL;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012046 }
12047 }
12048 }
12049
12050 sign_get_placed(buf, lnum, sign_id, group, rettv->vval.v_list);
12051}
12052
12053/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012054 * "sign_jump()" function
12055 */
12056 static void
12057f_sign_jump(typval_T *argvars, typval_T *rettv)
12058{
12059 int sign_id;
12060 char_u *sign_group = NULL;
12061 buf_T *buf;
12062 int notanum = FALSE;
12063
12064 rettv->vval.v_number = -1;
12065
Bram Moolenaarbdace832019-03-02 10:13:42 +010012066 // Sign identifier
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012067 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
12068 if (notanum)
12069 return;
12070 if (sign_id <= 0)
12071 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012072 emsg(_(e_invarg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012073 return;
12074 }
12075
12076 // Sign group
12077 sign_group = tv_get_string_chk(&argvars[1]);
12078 if (sign_group == NULL)
12079 return;
12080 if (sign_group[0] == '\0')
12081 sign_group = NULL; // global sign group
12082 else
12083 {
12084 sign_group = vim_strsave(sign_group);
12085 if (sign_group == NULL)
12086 return;
12087 }
12088
12089 // Buffer to place the sign
12090 buf = get_buf_arg(&argvars[2]);
12091 if (buf == NULL)
12092 goto cleanup;
12093
12094 rettv->vval.v_number = sign_jump(sign_id, sign_group, buf);
12095
12096cleanup:
12097 vim_free(sign_group);
12098}
12099
12100/*
Bram Moolenaar162b7142018-12-21 15:17:36 +010012101 * "sign_place()" function
12102 */
12103 static void
12104f_sign_place(typval_T *argvars, typval_T *rettv)
12105{
12106 int sign_id;
12107 char_u *group = NULL;
12108 char_u *sign_name;
12109 buf_T *buf;
12110 dict_T *dict;
12111 dictitem_T *di;
12112 linenr_T lnum = 0;
12113 int prio = SIGN_DEF_PRIO;
12114 int notanum = FALSE;
12115
12116 rettv->vval.v_number = -1;
12117
Bram Moolenaarbdace832019-03-02 10:13:42 +010012118 // Sign identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012119 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012120 if (notanum)
12121 return;
12122 if (sign_id < 0)
12123 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012124 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012125 return;
12126 }
12127
12128 // Sign group
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012129 group = tv_get_string_chk(&argvars[1]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012130 if (group == NULL)
12131 return;
12132 if (group[0] == '\0')
12133 group = NULL; // global sign group
12134 else
12135 {
12136 group = vim_strsave(group);
12137 if (group == NULL)
12138 return;
12139 }
12140
12141 // Sign name
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012142 sign_name = tv_get_string_chk(&argvars[2]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012143 if (sign_name == NULL)
12144 goto cleanup;
12145
12146 // Buffer to place the sign
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012147 buf = get_buf_arg(&argvars[3]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012148 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012149 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012150
12151 if (argvars[4].v_type != VAR_UNKNOWN)
12152 {
12153 if (argvars[4].v_type != VAR_DICT ||
12154 ((dict = argvars[4].vval.v_dict) == NULL))
12155 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012156 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012157 goto cleanup;
12158 }
12159
12160 // Line number where the sign is to be placed
12161 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
12162 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012163 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012164 if (notanum)
12165 goto cleanup;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012166 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012167 }
12168 if ((di = dict_find(dict, (char_u *)"priority", -1)) != NULL)
12169 {
12170 // Sign priority
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012171 prio = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012172 if (notanum)
12173 goto cleanup;
12174 }
12175 }
12176
12177 if (sign_place(&sign_id, group, sign_name, buf, lnum, prio) == OK)
12178 rettv->vval.v_number = sign_id;
12179
12180cleanup:
12181 vim_free(group);
12182}
12183
12184/*
12185 * "sign_undefine()" function
12186 */
12187 static void
12188f_sign_undefine(typval_T *argvars, typval_T *rettv)
12189{
12190 char_u *name;
12191
12192 rettv->vval.v_number = -1;
12193
12194 if (argvars[0].v_type == VAR_UNKNOWN)
12195 {
12196 // Free all the signs
12197 free_signs();
12198 rettv->vval.v_number = 0;
12199 }
12200 else
12201 {
12202 // Free only the specified sign
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012203 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012204 if (name == NULL)
12205 return;
12206
12207 if (sign_undefine_by_name(name) == OK)
12208 rettv->vval.v_number = 0;
12209 }
12210}
12211
12212/*
12213 * "sign_unplace()" function
12214 */
12215 static void
12216f_sign_unplace(typval_T *argvars, typval_T *rettv)
12217{
12218 dict_T *dict;
12219 dictitem_T *di;
12220 int sign_id = 0;
12221 buf_T *buf = NULL;
12222 char_u *group = NULL;
12223
12224 rettv->vval.v_number = -1;
12225
12226 if (argvars[0].v_type != VAR_STRING)
12227 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012228 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012229 return;
12230 }
12231
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012232 group = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012233 if (group[0] == '\0')
12234 group = NULL; // global sign group
12235 else
12236 {
12237 group = vim_strsave(group);
12238 if (group == NULL)
12239 return;
12240 }
12241
12242 if (argvars[1].v_type != VAR_UNKNOWN)
12243 {
12244 if (argvars[1].v_type != VAR_DICT)
12245 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012246 emsg(_(e_dictreq));
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012247 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012248 }
12249 dict = argvars[1].vval.v_dict;
12250
12251 if ((di = dict_find(dict, (char_u *)"buffer", -1)) != NULL)
12252 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012253 buf = get_buf_arg(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012254 if (buf == NULL)
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012255 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012256 }
12257 if (dict_find(dict, (char_u *)"id", -1) != NULL)
12258 sign_id = dict_get_number(dict, (char_u *)"id");
12259 }
12260
12261 if (buf == NULL)
12262 {
12263 // Delete the sign in all the buffers
12264 FOR_ALL_BUFFERS(buf)
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010012265 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012266 rettv->vval.v_number = 0;
12267 }
12268 else
12269 {
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010012270 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012271 rettv->vval.v_number = 0;
12272 }
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012273
12274cleanup:
Bram Moolenaar162b7142018-12-21 15:17:36 +010012275 vim_free(group);
12276}
12277#endif
12278
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012279/*
12280 * "simplify()" function
12281 */
12282 static void
12283f_simplify(typval_T *argvars, typval_T *rettv)
12284{
12285 char_u *p;
12286
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012287 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012288 rettv->vval.v_string = vim_strsave(p);
12289 simplify_filename(rettv->vval.v_string); /* simplify in place */
12290 rettv->v_type = VAR_STRING;
12291}
12292
12293#ifdef FEAT_FLOAT
12294/*
12295 * "sin()" function
12296 */
12297 static void
12298f_sin(typval_T *argvars, typval_T *rettv)
12299{
12300 float_T f = 0.0;
12301
12302 rettv->v_type = VAR_FLOAT;
12303 if (get_float_arg(argvars, &f) == OK)
12304 rettv->vval.v_float = sin(f);
12305 else
12306 rettv->vval.v_float = 0.0;
12307}
12308
12309/*
12310 * "sinh()" function
12311 */
12312 static void
12313f_sinh(typval_T *argvars, typval_T *rettv)
12314{
12315 float_T f = 0.0;
12316
12317 rettv->v_type = VAR_FLOAT;
12318 if (get_float_arg(argvars, &f) == OK)
12319 rettv->vval.v_float = sinh(f);
12320 else
12321 rettv->vval.v_float = 0.0;
12322}
12323#endif
12324
12325static int
12326#ifdef __BORLANDC__
12327 _RTLENTRYF
12328#endif
12329 item_compare(const void *s1, const void *s2);
12330static int
12331#ifdef __BORLANDC__
12332 _RTLENTRYF
12333#endif
12334 item_compare2(const void *s1, const void *s2);
12335
12336/* struct used in the array that's given to qsort() */
12337typedef struct
12338{
12339 listitem_T *item;
12340 int idx;
12341} sortItem_T;
12342
12343/* struct storing information about current sort */
12344typedef struct
12345{
12346 int item_compare_ic;
12347 int item_compare_numeric;
12348 int item_compare_numbers;
12349#ifdef FEAT_FLOAT
12350 int item_compare_float;
12351#endif
12352 char_u *item_compare_func;
12353 partial_T *item_compare_partial;
12354 dict_T *item_compare_selfdict;
12355 int item_compare_func_err;
12356 int item_compare_keep_zero;
12357} sortinfo_T;
12358static sortinfo_T *sortinfo = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012359#define ITEM_COMPARE_FAIL 999
12360
12361/*
12362 * Compare functions for f_sort() and f_uniq() below.
12363 */
12364 static int
12365#ifdef __BORLANDC__
12366_RTLENTRYF
12367#endif
12368item_compare(const void *s1, const void *s2)
12369{
12370 sortItem_T *si1, *si2;
12371 typval_T *tv1, *tv2;
12372 char_u *p1, *p2;
12373 char_u *tofree1 = NULL, *tofree2 = NULL;
12374 int res;
12375 char_u numbuf1[NUMBUFLEN];
12376 char_u numbuf2[NUMBUFLEN];
12377
12378 si1 = (sortItem_T *)s1;
12379 si2 = (sortItem_T *)s2;
12380 tv1 = &si1->item->li_tv;
12381 tv2 = &si2->item->li_tv;
12382
12383 if (sortinfo->item_compare_numbers)
12384 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012385 varnumber_T v1 = tv_get_number(tv1);
12386 varnumber_T v2 = tv_get_number(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012387
12388 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
12389 }
12390
12391#ifdef FEAT_FLOAT
12392 if (sortinfo->item_compare_float)
12393 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012394 float_T v1 = tv_get_float(tv1);
12395 float_T v2 = tv_get_float(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012396
12397 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
12398 }
12399#endif
12400
12401 /* tv2string() puts quotes around a string and allocates memory. Don't do
12402 * that for string variables. Use a single quote when comparing with a
12403 * non-string to do what the docs promise. */
12404 if (tv1->v_type == VAR_STRING)
12405 {
12406 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12407 p1 = (char_u *)"'";
12408 else
12409 p1 = tv1->vval.v_string;
12410 }
12411 else
12412 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
12413 if (tv2->v_type == VAR_STRING)
12414 {
12415 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12416 p2 = (char_u *)"'";
12417 else
12418 p2 = tv2->vval.v_string;
12419 }
12420 else
12421 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
12422 if (p1 == NULL)
12423 p1 = (char_u *)"";
12424 if (p2 == NULL)
12425 p2 = (char_u *)"";
12426 if (!sortinfo->item_compare_numeric)
12427 {
12428 if (sortinfo->item_compare_ic)
12429 res = STRICMP(p1, p2);
12430 else
12431 res = STRCMP(p1, p2);
12432 }
12433 else
12434 {
12435 double n1, n2;
12436 n1 = strtod((char *)p1, (char **)&p1);
12437 n2 = strtod((char *)p2, (char **)&p2);
12438 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
12439 }
12440
12441 /* When the result would be zero, compare the item indexes. Makes the
12442 * sort stable. */
12443 if (res == 0 && !sortinfo->item_compare_keep_zero)
12444 res = si1->idx > si2->idx ? 1 : -1;
12445
12446 vim_free(tofree1);
12447 vim_free(tofree2);
12448 return res;
12449}
12450
12451 static int
12452#ifdef __BORLANDC__
12453_RTLENTRYF
12454#endif
12455item_compare2(const void *s1, const void *s2)
12456{
12457 sortItem_T *si1, *si2;
12458 int res;
12459 typval_T rettv;
12460 typval_T argv[3];
12461 int dummy;
12462 char_u *func_name;
12463 partial_T *partial = sortinfo->item_compare_partial;
12464
12465 /* shortcut after failure in previous call; compare all items equal */
12466 if (sortinfo->item_compare_func_err)
12467 return 0;
12468
12469 si1 = (sortItem_T *)s1;
12470 si2 = (sortItem_T *)s2;
12471
12472 if (partial == NULL)
12473 func_name = sortinfo->item_compare_func;
12474 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012475 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012476
12477 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
12478 * in the copy without changing the original list items. */
12479 copy_tv(&si1->item->li_tv, &argv[0]);
12480 copy_tv(&si2->item->li_tv, &argv[1]);
12481
12482 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
12483 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020012484 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012485 partial, sortinfo->item_compare_selfdict);
12486 clear_tv(&argv[0]);
12487 clear_tv(&argv[1]);
12488
12489 if (res == FAIL)
12490 res = ITEM_COMPARE_FAIL;
12491 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012492 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012493 if (sortinfo->item_compare_func_err)
12494 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
12495 clear_tv(&rettv);
12496
12497 /* When the result would be zero, compare the pointers themselves. Makes
12498 * the sort stable. */
12499 if (res == 0 && !sortinfo->item_compare_keep_zero)
12500 res = si1->idx > si2->idx ? 1 : -1;
12501
12502 return res;
12503}
12504
12505/*
12506 * "sort({list})" function
12507 */
12508 static void
12509do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
12510{
12511 list_T *l;
12512 listitem_T *li;
12513 sortItem_T *ptrs;
12514 sortinfo_T *old_sortinfo;
12515 sortinfo_T info;
12516 long len;
12517 long i;
12518
12519 /* Pointer to current info struct used in compare function. Save and
12520 * restore the current one for nested calls. */
12521 old_sortinfo = sortinfo;
12522 sortinfo = &info;
12523
12524 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012525 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012526 else
12527 {
12528 l = argvars[0].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +010012529 if (l == NULL || var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012530 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
12531 TRUE))
12532 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012533 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012534
12535 len = list_len(l);
12536 if (len <= 1)
12537 goto theend; /* short list sorts pretty quickly */
12538
12539 info.item_compare_ic = FALSE;
12540 info.item_compare_numeric = FALSE;
12541 info.item_compare_numbers = FALSE;
12542#ifdef FEAT_FLOAT
12543 info.item_compare_float = FALSE;
12544#endif
12545 info.item_compare_func = NULL;
12546 info.item_compare_partial = NULL;
12547 info.item_compare_selfdict = NULL;
12548 if (argvars[1].v_type != VAR_UNKNOWN)
12549 {
12550 /* optional second argument: {func} */
12551 if (argvars[1].v_type == VAR_FUNC)
12552 info.item_compare_func = argvars[1].vval.v_string;
12553 else if (argvars[1].v_type == VAR_PARTIAL)
12554 info.item_compare_partial = argvars[1].vval.v_partial;
12555 else
12556 {
12557 int error = FALSE;
12558
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012559 i = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012560 if (error)
12561 goto theend; /* type error; errmsg already given */
12562 if (i == 1)
12563 info.item_compare_ic = TRUE;
12564 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012565 info.item_compare_func = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012566 else if (i != 0)
12567 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012568 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012569 goto theend;
12570 }
12571 if (info.item_compare_func != NULL)
12572 {
12573 if (*info.item_compare_func == NUL)
12574 {
12575 /* empty string means default sort */
12576 info.item_compare_func = NULL;
12577 }
12578 else if (STRCMP(info.item_compare_func, "n") == 0)
12579 {
12580 info.item_compare_func = NULL;
12581 info.item_compare_numeric = TRUE;
12582 }
12583 else if (STRCMP(info.item_compare_func, "N") == 0)
12584 {
12585 info.item_compare_func = NULL;
12586 info.item_compare_numbers = TRUE;
12587 }
12588#ifdef FEAT_FLOAT
12589 else if (STRCMP(info.item_compare_func, "f") == 0)
12590 {
12591 info.item_compare_func = NULL;
12592 info.item_compare_float = TRUE;
12593 }
12594#endif
12595 else if (STRCMP(info.item_compare_func, "i") == 0)
12596 {
12597 info.item_compare_func = NULL;
12598 info.item_compare_ic = TRUE;
12599 }
12600 }
12601 }
12602
12603 if (argvars[2].v_type != VAR_UNKNOWN)
12604 {
12605 /* optional third argument: {dict} */
12606 if (argvars[2].v_type != VAR_DICT)
12607 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012608 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012609 goto theend;
12610 }
12611 info.item_compare_selfdict = argvars[2].vval.v_dict;
12612 }
12613 }
12614
12615 /* Make an array with each entry pointing to an item in the List. */
12616 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
12617 if (ptrs == NULL)
12618 goto theend;
12619
12620 i = 0;
12621 if (sort)
12622 {
12623 /* sort(): ptrs will be the list to sort */
12624 for (li = l->lv_first; li != NULL; li = li->li_next)
12625 {
12626 ptrs[i].item = li;
12627 ptrs[i].idx = i;
12628 ++i;
12629 }
12630
12631 info.item_compare_func_err = FALSE;
12632 info.item_compare_keep_zero = FALSE;
12633 /* test the compare function */
12634 if ((info.item_compare_func != NULL
12635 || info.item_compare_partial != NULL)
12636 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
12637 == ITEM_COMPARE_FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012638 emsg(_("E702: Sort compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012639 else
12640 {
12641 /* Sort the array with item pointers. */
12642 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
12643 info.item_compare_func == NULL
12644 && info.item_compare_partial == NULL
12645 ? item_compare : item_compare2);
12646
12647 if (!info.item_compare_func_err)
12648 {
12649 /* Clear the List and append the items in sorted order. */
12650 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
12651 l->lv_len = 0;
12652 for (i = 0; i < len; ++i)
12653 list_append(l, ptrs[i].item);
12654 }
12655 }
12656 }
12657 else
12658 {
12659 int (*item_compare_func_ptr)(const void *, const void *);
12660
12661 /* f_uniq(): ptrs will be a stack of items to remove */
12662 info.item_compare_func_err = FALSE;
12663 info.item_compare_keep_zero = TRUE;
12664 item_compare_func_ptr = info.item_compare_func != NULL
12665 || info.item_compare_partial != NULL
12666 ? item_compare2 : item_compare;
12667
12668 for (li = l->lv_first; li != NULL && li->li_next != NULL;
12669 li = li->li_next)
12670 {
12671 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
12672 == 0)
12673 ptrs[i++].item = li;
12674 if (info.item_compare_func_err)
12675 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012676 emsg(_("E882: Uniq compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012677 break;
12678 }
12679 }
12680
12681 if (!info.item_compare_func_err)
12682 {
12683 while (--i >= 0)
12684 {
12685 li = ptrs[i].item->li_next;
12686 ptrs[i].item->li_next = li->li_next;
12687 if (li->li_next != NULL)
12688 li->li_next->li_prev = ptrs[i].item;
12689 else
12690 l->lv_last = ptrs[i].item;
12691 list_fix_watch(l, li);
12692 listitem_free(li);
12693 l->lv_len--;
12694 }
12695 }
12696 }
12697
12698 vim_free(ptrs);
12699 }
12700theend:
12701 sortinfo = old_sortinfo;
12702}
12703
12704/*
12705 * "sort({list})" function
12706 */
12707 static void
12708f_sort(typval_T *argvars, typval_T *rettv)
12709{
12710 do_sort_uniq(argvars, rettv, TRUE);
12711}
12712
12713/*
12714 * "uniq({list})" function
12715 */
12716 static void
12717f_uniq(typval_T *argvars, typval_T *rettv)
12718{
12719 do_sort_uniq(argvars, rettv, FALSE);
12720}
12721
12722/*
12723 * "soundfold({word})" function
12724 */
12725 static void
12726f_soundfold(typval_T *argvars, typval_T *rettv)
12727{
12728 char_u *s;
12729
12730 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012731 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012732#ifdef FEAT_SPELL
12733 rettv->vval.v_string = eval_soundfold(s);
12734#else
12735 rettv->vval.v_string = vim_strsave(s);
12736#endif
12737}
12738
12739/*
12740 * "spellbadword()" function
12741 */
12742 static void
12743f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
12744{
12745 char_u *word = (char_u *)"";
12746 hlf_T attr = HLF_COUNT;
12747 int len = 0;
12748
12749 if (rettv_list_alloc(rettv) == FAIL)
12750 return;
12751
12752#ifdef FEAT_SPELL
12753 if (argvars[0].v_type == VAR_UNKNOWN)
12754 {
12755 /* Find the start and length of the badly spelled word. */
12756 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
12757 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012758 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012759 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012760 curwin->w_set_curswant = TRUE;
12761 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012762 }
12763 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
12764 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012765 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012766 int capcol = -1;
12767
12768 if (str != NULL)
12769 {
12770 /* Check the argument for spelling. */
12771 while (*str != NUL)
12772 {
12773 len = spell_check(curwin, str, &attr, &capcol, FALSE);
12774 if (attr != HLF_COUNT)
12775 {
12776 word = str;
12777 break;
12778 }
12779 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020012780 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012781 }
12782 }
12783 }
12784#endif
12785
12786 list_append_string(rettv->vval.v_list, word, len);
12787 list_append_string(rettv->vval.v_list, (char_u *)(
12788 attr == HLF_SPB ? "bad" :
12789 attr == HLF_SPR ? "rare" :
12790 attr == HLF_SPL ? "local" :
12791 attr == HLF_SPC ? "caps" :
12792 ""), -1);
12793}
12794
12795/*
12796 * "spellsuggest()" function
12797 */
12798 static void
12799f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
12800{
12801#ifdef FEAT_SPELL
12802 char_u *str;
12803 int typeerr = FALSE;
12804 int maxcount;
12805 garray_T ga;
12806 int i;
12807 listitem_T *li;
12808 int need_capital = FALSE;
12809#endif
12810
12811 if (rettv_list_alloc(rettv) == FAIL)
12812 return;
12813
12814#ifdef FEAT_SPELL
12815 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
12816 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012817 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012818 if (argvars[1].v_type != VAR_UNKNOWN)
12819 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012820 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012821 if (maxcount <= 0)
12822 return;
12823 if (argvars[2].v_type != VAR_UNKNOWN)
12824 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012825 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012826 if (typeerr)
12827 return;
12828 }
12829 }
12830 else
12831 maxcount = 25;
12832
12833 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
12834
12835 for (i = 0; i < ga.ga_len; ++i)
12836 {
12837 str = ((char_u **)ga.ga_data)[i];
12838
12839 li = listitem_alloc();
12840 if (li == NULL)
12841 vim_free(str);
12842 else
12843 {
12844 li->li_tv.v_type = VAR_STRING;
12845 li->li_tv.v_lock = 0;
12846 li->li_tv.vval.v_string = str;
12847 list_append(rettv->vval.v_list, li);
12848 }
12849 }
12850 ga_clear(&ga);
12851 }
12852#endif
12853}
12854
12855 static void
12856f_split(typval_T *argvars, typval_T *rettv)
12857{
12858 char_u *str;
12859 char_u *end;
12860 char_u *pat = NULL;
12861 regmatch_T regmatch;
12862 char_u patbuf[NUMBUFLEN];
12863 char_u *save_cpo;
12864 int match;
12865 colnr_T col = 0;
12866 int keepempty = FALSE;
12867 int typeerr = FALSE;
12868
12869 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12870 save_cpo = p_cpo;
12871 p_cpo = (char_u *)"";
12872
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012873 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012874 if (argvars[1].v_type != VAR_UNKNOWN)
12875 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012876 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012877 if (pat == NULL)
12878 typeerr = TRUE;
12879 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012880 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012881 }
12882 if (pat == NULL || *pat == NUL)
12883 pat = (char_u *)"[\\x01- ]\\+";
12884
12885 if (rettv_list_alloc(rettv) == FAIL)
12886 return;
12887 if (typeerr)
12888 return;
12889
12890 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12891 if (regmatch.regprog != NULL)
12892 {
12893 regmatch.rm_ic = FALSE;
12894 while (*str != NUL || keepempty)
12895 {
12896 if (*str == NUL)
12897 match = FALSE; /* empty item at the end */
12898 else
12899 match = vim_regexec_nl(&regmatch, str, col);
12900 if (match)
12901 end = regmatch.startp[0];
12902 else
12903 end = str + STRLEN(str);
12904 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
12905 && *str != NUL && match && end < regmatch.endp[0]))
12906 {
12907 if (list_append_string(rettv->vval.v_list, str,
12908 (int)(end - str)) == FAIL)
12909 break;
12910 }
12911 if (!match)
12912 break;
Bram Moolenaar13505972019-01-24 15:04:48 +010012913 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012914 if (regmatch.endp[0] > str)
12915 col = 0;
12916 else
Bram Moolenaar13505972019-01-24 15:04:48 +010012917 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012918 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012919 str = regmatch.endp[0];
12920 }
12921
12922 vim_regfree(regmatch.regprog);
12923 }
12924
12925 p_cpo = save_cpo;
12926}
12927
12928#ifdef FEAT_FLOAT
12929/*
12930 * "sqrt()" function
12931 */
12932 static void
12933f_sqrt(typval_T *argvars, typval_T *rettv)
12934{
12935 float_T f = 0.0;
12936
12937 rettv->v_type = VAR_FLOAT;
12938 if (get_float_arg(argvars, &f) == OK)
12939 rettv->vval.v_float = sqrt(f);
12940 else
12941 rettv->vval.v_float = 0.0;
12942}
12943
12944/*
12945 * "str2float()" function
12946 */
12947 static void
12948f_str2float(typval_T *argvars, typval_T *rettv)
12949{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012950 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010012951 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012952
Bram Moolenaar08243d22017-01-10 16:12:29 +010012953 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012954 p = skipwhite(p + 1);
12955 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010012956 if (isneg)
12957 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012958 rettv->v_type = VAR_FLOAT;
12959}
12960#endif
12961
12962/*
Bram Moolenaar9d401282019-04-06 13:18:12 +020012963 * "str2list()" function
12964 */
12965 static void
12966f_str2list(typval_T *argvars, typval_T *rettv)
12967{
12968 char_u *p;
12969 int utf8 = FALSE;
12970
12971 if (rettv_list_alloc(rettv) == FAIL)
12972 return;
12973
12974 if (argvars[1].v_type != VAR_UNKNOWN)
12975 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
12976
12977 p = tv_get_string(&argvars[0]);
12978
12979 if (has_mbyte || utf8)
12980 {
12981 int (*ptr2len)(char_u *);
12982 int (*ptr2char)(char_u *);
12983
12984 if (utf8 || enc_utf8)
12985 {
12986 ptr2len = utf_ptr2len;
12987 ptr2char = utf_ptr2char;
12988 }
12989 else
12990 {
12991 ptr2len = mb_ptr2len;
12992 ptr2char = mb_ptr2char;
12993 }
12994
12995 for ( ; *p != NUL; p += (*ptr2len)(p))
12996 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
12997 }
12998 else
12999 for ( ; *p != NUL; ++p)
13000 list_append_number(rettv->vval.v_list, *p);
13001}
13002
13003/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013004 * "str2nr()" function
13005 */
13006 static void
13007f_str2nr(typval_T *argvars, typval_T *rettv)
13008{
13009 int base = 10;
13010 char_u *p;
13011 varnumber_T n;
13012 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010013013 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013014
13015 if (argvars[1].v_type != VAR_UNKNOWN)
13016 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013017 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013018 if (base != 2 && base != 8 && base != 10 && base != 16)
13019 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013020 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013021 return;
13022 }
13023 }
13024
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013025 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010013026 isneg = (*p == '-');
13027 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013028 p = skipwhite(p + 1);
13029 switch (base)
13030 {
13031 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
13032 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
13033 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
13034 default: what = 0;
13035 }
13036 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010013037 if (isneg)
13038 rettv->vval.v_number = -n;
13039 else
13040 rettv->vval.v_number = n;
13041
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013042}
13043
13044#ifdef HAVE_STRFTIME
13045/*
13046 * "strftime({format}[, {time}])" function
13047 */
13048 static void
13049f_strftime(typval_T *argvars, typval_T *rettv)
13050{
13051 char_u result_buf[256];
13052 struct tm *curtime;
13053 time_t seconds;
13054 char_u *p;
13055
13056 rettv->v_type = VAR_STRING;
13057
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013058 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013059 if (argvars[1].v_type == VAR_UNKNOWN)
13060 seconds = time(NULL);
13061 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013062 seconds = (time_t)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013063 curtime = localtime(&seconds);
13064 /* MSVC returns NULL for an invalid value of seconds. */
13065 if (curtime == NULL)
13066 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
13067 else
13068 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013069 vimconv_T conv;
13070 char_u *enc;
13071
13072 conv.vc_type = CONV_NONE;
13073 enc = enc_locale();
13074 convert_setup(&conv, p_enc, enc);
13075 if (conv.vc_type != CONV_NONE)
13076 p = string_convert(&conv, p, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013077 if (p != NULL)
13078 (void)strftime((char *)result_buf, sizeof(result_buf),
13079 (char *)p, curtime);
13080 else
13081 result_buf[0] = NUL;
13082
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013083 if (conv.vc_type != CONV_NONE)
13084 vim_free(p);
13085 convert_setup(&conv, enc, p_enc);
13086 if (conv.vc_type != CONV_NONE)
13087 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
13088 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013089 rettv->vval.v_string = vim_strsave(result_buf);
13090
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013091 /* Release conversion descriptors */
13092 convert_setup(&conv, NULL, NULL);
13093 vim_free(enc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013094 }
13095}
13096#endif
13097
13098/*
13099 * "strgetchar()" function
13100 */
13101 static void
13102f_strgetchar(typval_T *argvars, typval_T *rettv)
13103{
13104 char_u *str;
13105 int len;
13106 int error = FALSE;
13107 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +010013108 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013109
13110 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013111 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013112 if (str == NULL)
13113 return;
13114 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013115 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013116 if (error)
13117 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013118
Bram Moolenaar13505972019-01-24 15:04:48 +010013119 while (charidx >= 0 && byteidx < len)
13120 {
13121 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013122 {
Bram Moolenaar13505972019-01-24 15:04:48 +010013123 rettv->vval.v_number = mb_ptr2char(str + byteidx);
13124 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013125 }
Bram Moolenaar13505972019-01-24 15:04:48 +010013126 --charidx;
13127 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013128 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013129}
13130
13131/*
13132 * "stridx()" function
13133 */
13134 static void
13135f_stridx(typval_T *argvars, typval_T *rettv)
13136{
13137 char_u buf[NUMBUFLEN];
13138 char_u *needle;
13139 char_u *haystack;
13140 char_u *save_haystack;
13141 char_u *pos;
13142 int start_idx;
13143
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013144 needle = tv_get_string_chk(&argvars[1]);
13145 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013146 rettv->vval.v_number = -1;
13147 if (needle == NULL || haystack == NULL)
13148 return; /* type error; errmsg already given */
13149
13150 if (argvars[2].v_type != VAR_UNKNOWN)
13151 {
13152 int error = FALSE;
13153
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013154 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013155 if (error || start_idx >= (int)STRLEN(haystack))
13156 return;
13157 if (start_idx >= 0)
13158 haystack += start_idx;
13159 }
13160
13161 pos = (char_u *)strstr((char *)haystack, (char *)needle);
13162 if (pos != NULL)
13163 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
13164}
13165
13166/*
13167 * "string()" function
13168 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010013169 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013170f_string(typval_T *argvars, typval_T *rettv)
13171{
13172 char_u *tofree;
13173 char_u numbuf[NUMBUFLEN];
13174
13175 rettv->v_type = VAR_STRING;
13176 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
13177 get_copyID());
13178 /* Make a copy if we have a value but it's not in allocated memory. */
13179 if (rettv->vval.v_string != NULL && tofree == NULL)
13180 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
13181}
13182
13183/*
13184 * "strlen()" function
13185 */
13186 static void
13187f_strlen(typval_T *argvars, typval_T *rettv)
13188{
13189 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013190 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013191}
13192
13193/*
13194 * "strchars()" function
13195 */
13196 static void
13197f_strchars(typval_T *argvars, typval_T *rettv)
13198{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013199 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013200 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013201 varnumber_T len = 0;
13202 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013203
13204 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013205 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013206 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013207 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013208 else
13209 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013210 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
13211 while (*s != NUL)
13212 {
13213 func_mb_ptr2char_adv(&s);
13214 ++len;
13215 }
13216 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013217 }
13218}
13219
13220/*
13221 * "strdisplaywidth()" function
13222 */
13223 static void
13224f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
13225{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013226 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013227 int col = 0;
13228
13229 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013230 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013231
13232 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
13233}
13234
13235/*
13236 * "strwidth()" function
13237 */
13238 static void
13239f_strwidth(typval_T *argvars, typval_T *rettv)
13240{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013241 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013242
Bram Moolenaar13505972019-01-24 15:04:48 +010013243 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013244}
13245
13246/*
13247 * "strcharpart()" function
13248 */
13249 static void
13250f_strcharpart(typval_T *argvars, typval_T *rettv)
13251{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013252 char_u *p;
13253 int nchar;
13254 int nbyte = 0;
13255 int charlen;
13256 int len = 0;
13257 int slen;
13258 int error = FALSE;
13259
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013260 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013261 slen = (int)STRLEN(p);
13262
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013263 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013264 if (!error)
13265 {
13266 if (nchar > 0)
13267 while (nchar > 0 && nbyte < slen)
13268 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020013269 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013270 --nchar;
13271 }
13272 else
13273 nbyte = nchar;
13274 if (argvars[2].v_type != VAR_UNKNOWN)
13275 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013276 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013277 while (charlen > 0 && nbyte + len < slen)
13278 {
13279 int off = nbyte + len;
13280
13281 if (off < 0)
13282 len += 1;
13283 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020013284 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013285 --charlen;
13286 }
13287 }
13288 else
13289 len = slen - nbyte; /* default: all bytes that are available. */
13290 }
13291
13292 /*
13293 * Only return the overlap between the specified part and the actual
13294 * string.
13295 */
13296 if (nbyte < 0)
13297 {
13298 len += nbyte;
13299 nbyte = 0;
13300 }
13301 else if (nbyte > slen)
13302 nbyte = slen;
13303 if (len < 0)
13304 len = 0;
13305 else if (nbyte + len > slen)
13306 len = slen - nbyte;
13307
13308 rettv->v_type = VAR_STRING;
13309 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013310}
13311
13312/*
13313 * "strpart()" function
13314 */
13315 static void
13316f_strpart(typval_T *argvars, typval_T *rettv)
13317{
13318 char_u *p;
13319 int n;
13320 int len;
13321 int slen;
13322 int error = FALSE;
13323
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013324 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013325 slen = (int)STRLEN(p);
13326
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013327 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013328 if (error)
13329 len = 0;
13330 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013331 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013332 else
13333 len = slen - n; /* default len: all bytes that are available. */
13334
13335 /*
13336 * Only return the overlap between the specified part and the actual
13337 * string.
13338 */
13339 if (n < 0)
13340 {
13341 len += n;
13342 n = 0;
13343 }
13344 else if (n > slen)
13345 n = slen;
13346 if (len < 0)
13347 len = 0;
13348 else if (n + len > slen)
13349 len = slen - n;
13350
13351 rettv->v_type = VAR_STRING;
13352 rettv->vval.v_string = vim_strnsave(p + n, len);
13353}
13354
13355/*
13356 * "strridx()" function
13357 */
13358 static void
13359f_strridx(typval_T *argvars, typval_T *rettv)
13360{
13361 char_u buf[NUMBUFLEN];
13362 char_u *needle;
13363 char_u *haystack;
13364 char_u *rest;
13365 char_u *lastmatch = NULL;
13366 int haystack_len, end_idx;
13367
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013368 needle = tv_get_string_chk(&argvars[1]);
13369 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013370
13371 rettv->vval.v_number = -1;
13372 if (needle == NULL || haystack == NULL)
13373 return; /* type error; errmsg already given */
13374
13375 haystack_len = (int)STRLEN(haystack);
13376 if (argvars[2].v_type != VAR_UNKNOWN)
13377 {
13378 /* Third argument: upper limit for index */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013379 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013380 if (end_idx < 0)
13381 return; /* can never find a match */
13382 }
13383 else
13384 end_idx = haystack_len;
13385
13386 if (*needle == NUL)
13387 {
13388 /* Empty string matches past the end. */
13389 lastmatch = haystack + end_idx;
13390 }
13391 else
13392 {
13393 for (rest = haystack; *rest != '\0'; ++rest)
13394 {
13395 rest = (char_u *)strstr((char *)rest, (char *)needle);
13396 if (rest == NULL || rest > haystack + end_idx)
13397 break;
13398 lastmatch = rest;
13399 }
13400 }
13401
13402 if (lastmatch == NULL)
13403 rettv->vval.v_number = -1;
13404 else
13405 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
13406}
13407
13408/*
13409 * "strtrans()" function
13410 */
13411 static void
13412f_strtrans(typval_T *argvars, typval_T *rettv)
13413{
13414 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013415 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013416}
13417
13418/*
13419 * "submatch()" function
13420 */
13421 static void
13422f_submatch(typval_T *argvars, typval_T *rettv)
13423{
13424 int error = FALSE;
13425 int no;
13426 int retList = 0;
13427
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013428 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013429 if (error)
13430 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013431 if (no < 0 || no >= NSUBEXP)
13432 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013433 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +010013434 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013435 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013436 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013437 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013438 if (error)
13439 return;
13440
13441 if (retList == 0)
13442 {
13443 rettv->v_type = VAR_STRING;
13444 rettv->vval.v_string = reg_submatch(no);
13445 }
13446 else
13447 {
13448 rettv->v_type = VAR_LIST;
13449 rettv->vval.v_list = reg_submatch_list(no);
13450 }
13451}
13452
13453/*
13454 * "substitute()" function
13455 */
13456 static void
13457f_substitute(typval_T *argvars, typval_T *rettv)
13458{
13459 char_u patbuf[NUMBUFLEN];
13460 char_u subbuf[NUMBUFLEN];
13461 char_u flagsbuf[NUMBUFLEN];
13462
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013463 char_u *str = tv_get_string_chk(&argvars[0]);
13464 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013465 char_u *sub = NULL;
13466 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013467 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013468
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013469 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
13470 expr = &argvars[2];
13471 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013472 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013473
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013474 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013475 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
13476 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013477 rettv->vval.v_string = NULL;
13478 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013479 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013480}
13481
13482/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013483 * "swapinfo(swap_filename)" function
13484 */
13485 static void
13486f_swapinfo(typval_T *argvars, typval_T *rettv)
13487{
13488 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013489 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013490}
13491
13492/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020013493 * "swapname(expr)" function
13494 */
13495 static void
13496f_swapname(typval_T *argvars, typval_T *rettv)
13497{
13498 buf_T *buf;
13499
13500 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010013501 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +020013502 if (buf == NULL || buf->b_ml.ml_mfp == NULL
13503 || buf->b_ml.ml_mfp->mf_fname == NULL)
13504 rettv->vval.v_string = NULL;
13505 else
13506 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
13507}
13508
13509/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013510 * "synID(lnum, col, trans)" function
13511 */
13512 static void
13513f_synID(typval_T *argvars UNUSED, typval_T *rettv)
13514{
13515 int id = 0;
13516#ifdef FEAT_SYN_HL
13517 linenr_T lnum;
13518 colnr_T col;
13519 int trans;
13520 int transerr = FALSE;
13521
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013522 lnum = tv_get_lnum(argvars); /* -1 on type error */
13523 col = (linenr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
13524 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013525
13526 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13527 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
13528 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
13529#endif
13530
13531 rettv->vval.v_number = id;
13532}
13533
13534/*
13535 * "synIDattr(id, what [, mode])" function
13536 */
13537 static void
13538f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
13539{
13540 char_u *p = NULL;
13541#ifdef FEAT_SYN_HL
13542 int id;
13543 char_u *what;
13544 char_u *mode;
13545 char_u modebuf[NUMBUFLEN];
13546 int modec;
13547
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013548 id = (int)tv_get_number(&argvars[0]);
13549 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013550 if (argvars[2].v_type != VAR_UNKNOWN)
13551 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013552 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013553 modec = TOLOWER_ASC(mode[0]);
13554 if (modec != 't' && modec != 'c' && modec != 'g')
13555 modec = 0; /* replace invalid with current */
13556 }
13557 else
13558 {
13559#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
13560 if (USE_24BIT)
13561 modec = 'g';
13562 else
13563#endif
13564 if (t_colors > 1)
13565 modec = 'c';
13566 else
13567 modec = 't';
13568 }
13569
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013570 switch (TOLOWER_ASC(what[0]))
13571 {
13572 case 'b':
13573 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
13574 p = highlight_color(id, what, modec);
13575 else /* bold */
13576 p = highlight_has_attr(id, HL_BOLD, modec);
13577 break;
13578
13579 case 'f': /* fg[#] or font */
13580 p = highlight_color(id, what, modec);
13581 break;
13582
13583 case 'i':
13584 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
13585 p = highlight_has_attr(id, HL_INVERSE, modec);
13586 else /* italic */
13587 p = highlight_has_attr(id, HL_ITALIC, modec);
13588 break;
13589
13590 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020013591 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013592 break;
13593
13594 case 'r': /* reverse */
13595 p = highlight_has_attr(id, HL_INVERSE, modec);
13596 break;
13597
13598 case 's':
13599 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
13600 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020013601 /* strikeout */
13602 else if (TOLOWER_ASC(what[1]) == 't' &&
13603 TOLOWER_ASC(what[2]) == 'r')
13604 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013605 else /* standout */
13606 p = highlight_has_attr(id, HL_STANDOUT, modec);
13607 break;
13608
13609 case 'u':
13610 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
13611 /* underline */
13612 p = highlight_has_attr(id, HL_UNDERLINE, modec);
13613 else
13614 /* undercurl */
13615 p = highlight_has_attr(id, HL_UNDERCURL, modec);
13616 break;
13617 }
13618
13619 if (p != NULL)
13620 p = vim_strsave(p);
13621#endif
13622 rettv->v_type = VAR_STRING;
13623 rettv->vval.v_string = p;
13624}
13625
13626/*
13627 * "synIDtrans(id)" function
13628 */
13629 static void
13630f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
13631{
13632 int id;
13633
13634#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013635 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013636
13637 if (id > 0)
13638 id = syn_get_final_id(id);
13639 else
13640#endif
13641 id = 0;
13642
13643 rettv->vval.v_number = id;
13644}
13645
13646/*
13647 * "synconcealed(lnum, col)" function
13648 */
13649 static void
13650f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
13651{
13652#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
13653 linenr_T lnum;
13654 colnr_T col;
13655 int syntax_flags = 0;
13656 int cchar;
13657 int matchid = 0;
13658 char_u str[NUMBUFLEN];
13659#endif
13660
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013661 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013662
13663#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013664 lnum = tv_get_lnum(argvars); /* -1 on type error */
13665 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013666
13667 vim_memset(str, NUL, sizeof(str));
13668
13669 if (rettv_list_alloc(rettv) != FAIL)
13670 {
13671 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13672 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13673 && curwin->w_p_cole > 0)
13674 {
13675 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
13676 syntax_flags = get_syntax_info(&matchid);
13677
13678 /* get the conceal character */
13679 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
13680 {
13681 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020013682 if (cchar == NUL && curwin->w_p_cole == 1)
13683 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013684 if (cchar != NUL)
13685 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013686 if (has_mbyte)
13687 (*mb_char2bytes)(cchar, str);
13688 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013689 str[0] = cchar;
13690 }
13691 }
13692 }
13693
13694 list_append_number(rettv->vval.v_list,
13695 (syntax_flags & HL_CONCEAL) != 0);
13696 /* -1 to auto-determine strlen */
13697 list_append_string(rettv->vval.v_list, str, -1);
13698 list_append_number(rettv->vval.v_list, matchid);
13699 }
13700#endif
13701}
13702
13703/*
13704 * "synstack(lnum, col)" function
13705 */
13706 static void
13707f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
13708{
13709#ifdef FEAT_SYN_HL
13710 linenr_T lnum;
13711 colnr_T col;
13712 int i;
13713 int id;
13714#endif
13715
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013716 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013717
13718#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013719 lnum = tv_get_lnum(argvars); /* -1 on type error */
13720 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013721
13722 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13723 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13724 && rettv_list_alloc(rettv) != FAIL)
13725 {
13726 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
13727 for (i = 0; ; ++i)
13728 {
13729 id = syn_get_stack_item(i);
13730 if (id < 0)
13731 break;
13732 if (list_append_number(rettv->vval.v_list, id) == FAIL)
13733 break;
13734 }
13735 }
13736#endif
13737}
13738
13739 static void
13740get_cmd_output_as_rettv(
13741 typval_T *argvars,
13742 typval_T *rettv,
13743 int retlist)
13744{
13745 char_u *res = NULL;
13746 char_u *p;
13747 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013748 int err = FALSE;
13749 FILE *fd;
13750 list_T *list = NULL;
13751 int flags = SHELL_SILENT;
13752
13753 rettv->v_type = VAR_STRING;
13754 rettv->vval.v_string = NULL;
13755 if (check_restricted() || check_secure())
13756 goto errret;
13757
13758 if (argvars[1].v_type != VAR_UNKNOWN)
13759 {
13760 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010013761 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013762 * command.
13763 */
13764 if ((infile = vim_tempname('i', TRUE)) == NULL)
13765 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013766 emsg(_(e_notmp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013767 goto errret;
13768 }
13769
13770 fd = mch_fopen((char *)infile, WRITEBIN);
13771 if (fd == NULL)
13772 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013773 semsg(_(e_notopen), infile);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013774 goto errret;
13775 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010013776 if (argvars[1].v_type == VAR_NUMBER)
13777 {
13778 linenr_T lnum;
13779 buf_T *buf;
13780
13781 buf = buflist_findnr(argvars[1].vval.v_number);
13782 if (buf == NULL)
13783 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013784 semsg(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010013785 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010013786 goto errret;
13787 }
13788
13789 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
13790 {
13791 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
13792 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
13793 {
13794 err = TRUE;
13795 break;
13796 }
13797 if (putc(NL, fd) == EOF)
13798 {
13799 err = TRUE;
13800 break;
13801 }
13802 }
13803 }
13804 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013805 {
13806 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
13807 err = TRUE;
13808 }
13809 else
13810 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010013811 size_t len;
13812 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013813
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013814 p = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013815 if (p == NULL)
13816 {
13817 fclose(fd);
13818 goto errret; /* type error; errmsg already given */
13819 }
13820 len = STRLEN(p);
13821 if (len > 0 && fwrite(p, len, 1, fd) != 1)
13822 err = TRUE;
13823 }
13824 if (fclose(fd) != 0)
13825 err = TRUE;
13826 if (err)
13827 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013828 emsg(_("E677: Error writing temp file"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013829 goto errret;
13830 }
13831 }
13832
13833 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
13834 * echoes typeahead, that messes up the display. */
13835 if (!msg_silent)
13836 flags += SHELL_COOKED;
13837
13838 if (retlist)
13839 {
13840 int len;
13841 listitem_T *li;
13842 char_u *s = NULL;
13843 char_u *start;
13844 char_u *end;
13845 int i;
13846
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013847 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013848 if (res == NULL)
13849 goto errret;
13850
13851 list = list_alloc();
13852 if (list == NULL)
13853 goto errret;
13854
13855 for (i = 0; i < len; ++i)
13856 {
13857 start = res + i;
13858 while (i < len && res[i] != NL)
13859 ++i;
13860 end = res + i;
13861
13862 s = alloc((unsigned)(end - start + 1));
13863 if (s == NULL)
13864 goto errret;
13865
13866 for (p = s; start < end; ++p, ++start)
13867 *p = *start == NUL ? NL : *start;
13868 *p = NUL;
13869
13870 li = listitem_alloc();
13871 if (li == NULL)
13872 {
13873 vim_free(s);
13874 goto errret;
13875 }
13876 li->li_tv.v_type = VAR_STRING;
13877 li->li_tv.v_lock = 0;
13878 li->li_tv.vval.v_string = s;
13879 list_append(list, li);
13880 }
13881
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013882 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013883 list = NULL;
13884 }
13885 else
13886 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013887 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar00590742019-02-15 21:06:09 +010013888#ifdef USE_CRNL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013889 /* translate <CR><NL> into <NL> */
13890 if (res != NULL)
13891 {
13892 char_u *s, *d;
13893
13894 d = res;
13895 for (s = res; *s; ++s)
13896 {
13897 if (s[0] == CAR && s[1] == NL)
13898 ++s;
13899 *d++ = *s;
13900 }
13901 *d = NUL;
13902 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013903#endif
13904 rettv->vval.v_string = res;
13905 res = NULL;
13906 }
13907
13908errret:
13909 if (infile != NULL)
13910 {
13911 mch_remove(infile);
13912 vim_free(infile);
13913 }
13914 if (res != NULL)
13915 vim_free(res);
13916 if (list != NULL)
13917 list_free(list);
13918}
13919
13920/*
13921 * "system()" function
13922 */
13923 static void
13924f_system(typval_T *argvars, typval_T *rettv)
13925{
13926 get_cmd_output_as_rettv(argvars, rettv, FALSE);
13927}
13928
13929/*
13930 * "systemlist()" function
13931 */
13932 static void
13933f_systemlist(typval_T *argvars, typval_T *rettv)
13934{
13935 get_cmd_output_as_rettv(argvars, rettv, TRUE);
13936}
13937
13938/*
13939 * "tabpagebuflist()" function
13940 */
13941 static void
13942f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13943{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013944 tabpage_T *tp;
13945 win_T *wp = NULL;
13946
13947 if (argvars[0].v_type == VAR_UNKNOWN)
13948 wp = firstwin;
13949 else
13950 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013951 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013952 if (tp != NULL)
13953 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
13954 }
13955 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
13956 {
13957 for (; wp != NULL; wp = wp->w_next)
13958 if (list_append_number(rettv->vval.v_list,
13959 wp->w_buffer->b_fnum) == FAIL)
13960 break;
13961 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013962}
13963
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013964/*
13965 * "tabpagenr()" function
13966 */
13967 static void
13968f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
13969{
13970 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013971 char_u *arg;
13972
13973 if (argvars[0].v_type != VAR_UNKNOWN)
13974 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013975 arg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013976 nr = 0;
13977 if (arg != NULL)
13978 {
13979 if (STRCMP(arg, "$") == 0)
13980 nr = tabpage_index(NULL) - 1;
13981 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013982 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013983 }
13984 }
13985 else
13986 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013987 rettv->vval.v_number = nr;
13988}
13989
13990
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013991/*
13992 * Common code for tabpagewinnr() and winnr().
13993 */
13994 static int
13995get_winnr(tabpage_T *tp, typval_T *argvar)
13996{
13997 win_T *twin;
13998 int nr = 1;
13999 win_T *wp;
14000 char_u *arg;
14001
14002 twin = (tp == curtab) ? curwin : tp->tp_curwin;
14003 if (argvar->v_type != VAR_UNKNOWN)
14004 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014005 arg = tv_get_string_chk(argvar);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014006 if (arg == NULL)
14007 nr = 0; /* type error; errmsg already given */
14008 else if (STRCMP(arg, "$") == 0)
14009 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
14010 else if (STRCMP(arg, "#") == 0)
14011 {
14012 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
14013 if (twin == NULL)
14014 nr = 0;
14015 }
14016 else
14017 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014018 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014019 nr = 0;
14020 }
14021 }
14022
14023 if (nr > 0)
14024 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
14025 wp != twin; wp = wp->w_next)
14026 {
14027 if (wp == NULL)
14028 {
14029 /* didn't find it in this tabpage */
14030 nr = 0;
14031 break;
14032 }
14033 ++nr;
14034 }
14035 return nr;
14036}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014037
14038/*
14039 * "tabpagewinnr()" function
14040 */
14041 static void
14042f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
14043{
14044 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014045 tabpage_T *tp;
14046
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014047 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014048 if (tp == NULL)
14049 nr = 0;
14050 else
14051 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014052 rettv->vval.v_number = nr;
14053}
14054
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014055/*
14056 * "tagfiles()" function
14057 */
14058 static void
14059f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
14060{
14061 char_u *fname;
14062 tagname_T tn;
14063 int first;
14064
14065 if (rettv_list_alloc(rettv) == FAIL)
14066 return;
14067 fname = alloc(MAXPATHL);
14068 if (fname == NULL)
14069 return;
14070
14071 for (first = TRUE; ; first = FALSE)
14072 if (get_tagfname(&tn, first, fname) == FAIL
14073 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
14074 break;
14075 tagname_free(&tn);
14076 vim_free(fname);
14077}
14078
14079/*
14080 * "taglist()" function
14081 */
14082 static void
14083f_taglist(typval_T *argvars, typval_T *rettv)
14084{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014085 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014086 char_u *tag_pattern;
14087
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014088 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014089
14090 rettv->vval.v_number = FALSE;
14091 if (*tag_pattern == NUL)
14092 return;
14093
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014094 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014095 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014096 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014097 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014098}
14099
14100/*
14101 * "tempname()" function
14102 */
14103 static void
14104f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
14105{
14106 static int x = 'A';
14107
14108 rettv->v_type = VAR_STRING;
14109 rettv->vval.v_string = vim_tempname(x, FALSE);
14110
14111 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
14112 * names. Skip 'I' and 'O', they are used for shell redirection. */
14113 do
14114 {
14115 if (x == 'Z')
14116 x = '0';
14117 else if (x == '9')
14118 x = 'A';
14119 else
14120 {
14121#ifdef EBCDIC
14122 if (x == 'I')
14123 x = 'J';
14124 else if (x == 'R')
14125 x = 'S';
14126 else
14127#endif
14128 ++x;
14129 }
14130 } while (x == 'I' || x == 'O');
14131}
14132
14133#ifdef FEAT_FLOAT
14134/*
14135 * "tan()" function
14136 */
14137 static void
14138f_tan(typval_T *argvars, typval_T *rettv)
14139{
14140 float_T f = 0.0;
14141
14142 rettv->v_type = VAR_FLOAT;
14143 if (get_float_arg(argvars, &f) == OK)
14144 rettv->vval.v_float = tan(f);
14145 else
14146 rettv->vval.v_float = 0.0;
14147}
14148
14149/*
14150 * "tanh()" function
14151 */
14152 static void
14153f_tanh(typval_T *argvars, typval_T *rettv)
14154{
14155 float_T f = 0.0;
14156
14157 rettv->v_type = VAR_FLOAT;
14158 if (get_float_arg(argvars, &f) == OK)
14159 rettv->vval.v_float = tanh(f);
14160 else
14161 rettv->vval.v_float = 0.0;
14162}
14163#endif
14164
14165/*
14166 * "test_alloc_fail(id, countdown, repeat)" function
14167 */
14168 static void
14169f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
14170{
14171 if (argvars[0].v_type != VAR_NUMBER
14172 || argvars[0].vval.v_number <= 0
14173 || argvars[1].v_type != VAR_NUMBER
14174 || argvars[1].vval.v_number < 0
14175 || argvars[2].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014176 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014177 else
14178 {
14179 alloc_fail_id = argvars[0].vval.v_number;
14180 if (alloc_fail_id >= aid_last)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014181 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014182 alloc_fail_countdown = argvars[1].vval.v_number;
14183 alloc_fail_repeat = argvars[2].vval.v_number;
14184 did_outofmem_msg = FALSE;
14185 }
14186}
14187
14188/*
14189 * "test_autochdir()"
14190 */
14191 static void
14192f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14193{
14194#if defined(FEAT_AUTOCHDIR)
14195 test_autochdir = TRUE;
14196#endif
14197}
14198
14199/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020014200 * "test_feedinput()"
14201 */
14202 static void
14203f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
14204{
14205#ifdef USE_INPUT_BUF
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014206 char_u *val = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5e80de32017-09-03 15:48:12 +020014207
14208 if (val != NULL)
14209 {
14210 trash_input_buf();
14211 add_to_input_buf_csi(val, (int)STRLEN(val));
14212 }
14213#endif
14214}
14215
14216/*
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014217 * "test_option_not_set({name})" function
14218 */
14219 static void
14220f_test_option_not_set(typval_T *argvars, typval_T *rettv UNUSED)
14221{
14222 char_u *name = (char_u *)"";
14223
14224 if (argvars[0].v_type != VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014225 emsg(_(e_invarg));
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014226 else
14227 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014228 name = tv_get_string(&argvars[0]);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014229 if (reset_option_was_set(name) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014230 semsg(_(e_invarg2), name);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014231 }
14232}
14233
14234/*
14235 * "test_override({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014236 */
14237 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014238f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014239{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014240 char_u *name = (char_u *)"";
14241 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014242 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014243
14244 if (argvars[0].v_type != VAR_STRING
14245 || (argvars[1].v_type) != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014246 emsg(_(e_invarg));
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014247 else
14248 {
Bram Moolenaare38197d2018-12-24 23:35:13 +010014249 name = tv_get_string(&argvars[0]);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014250 val = (int)tv_get_number(&argvars[1]);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014251
14252 if (STRCMP(name, (char_u *)"redraw") == 0)
14253 disable_redraw_for_testing = val;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020014254 else if (STRCMP(name, (char_u *)"redraw_flag") == 0)
14255 ignore_redraw_flag_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014256 else if (STRCMP(name, (char_u *)"char_avail") == 0)
14257 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014258 else if (STRCMP(name, (char_u *)"starting") == 0)
14259 {
14260 if (val)
14261 {
14262 if (save_starting < 0)
14263 save_starting = starting;
14264 starting = 0;
14265 }
14266 else
14267 {
14268 starting = save_starting;
14269 save_starting = -1;
14270 }
14271 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +020014272 else if (STRCMP(name, (char_u *)"nfa_fail") == 0)
14273 nfa_fail_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014274 else if (STRCMP(name, (char_u *)"ALL") == 0)
14275 {
14276 disable_char_avail_for_testing = FALSE;
14277 disable_redraw_for_testing = FALSE;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020014278 ignore_redraw_flag_for_testing = FALSE;
Bram Moolenaarbcf94422018-06-23 14:21:42 +020014279 nfa_fail_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014280 if (save_starting >= 0)
14281 {
14282 starting = save_starting;
14283 save_starting = -1;
14284 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014285 }
14286 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014287 semsg(_(e_invarg2), name);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014288 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014289}
14290
14291/*
Bram Moolenaarc3e92c12019-03-23 14:23:07 +010014292 * "test_refcount({expr})" function
14293 */
14294 static void
14295f_test_refcount(typval_T *argvars, typval_T *rettv)
14296{
14297 int retval = -1;
14298
14299 switch (argvars[0].v_type)
14300 {
14301 case VAR_UNKNOWN:
14302 case VAR_NUMBER:
14303 case VAR_FLOAT:
14304 case VAR_SPECIAL:
14305 case VAR_STRING:
14306 break;
14307 case VAR_JOB:
14308#ifdef FEAT_JOB_CHANNEL
14309 if (argvars[0].vval.v_job != NULL)
14310 retval = argvars[0].vval.v_job->jv_refcount - 1;
14311#endif
14312 break;
14313 case VAR_CHANNEL:
14314#ifdef FEAT_JOB_CHANNEL
14315 if (argvars[0].vval.v_channel != NULL)
14316 retval = argvars[0].vval.v_channel->ch_refcount - 1;
14317#endif
14318 break;
14319 case VAR_FUNC:
14320 if (argvars[0].vval.v_string != NULL)
14321 {
14322 ufunc_T *fp;
14323
14324 fp = find_func(argvars[0].vval.v_string);
14325 if (fp != NULL)
14326 retval = fp->uf_refcount;
14327 }
14328 break;
14329 case VAR_PARTIAL:
14330 if (argvars[0].vval.v_partial != NULL)
14331 retval = argvars[0].vval.v_partial->pt_refcount - 1;
14332 break;
14333 case VAR_BLOB:
14334 if (argvars[0].vval.v_blob != NULL)
14335 retval = argvars[0].vval.v_blob->bv_refcount - 1;
14336 break;
14337 case VAR_LIST:
14338 if (argvars[0].vval.v_list != NULL)
14339 retval = argvars[0].vval.v_list->lv_refcount - 1;
14340 break;
14341 case VAR_DICT:
14342 if (argvars[0].vval.v_dict != NULL)
14343 retval = argvars[0].vval.v_dict->dv_refcount - 1;
14344 break;
14345 }
14346
14347 rettv->v_type = VAR_NUMBER;
14348 rettv->vval.v_number = retval;
14349
14350}
14351
14352/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014353 * "test_garbagecollect_now()" function
14354 */
14355 static void
14356f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14357{
14358 /* This is dangerous, any Lists and Dicts used internally may be freed
14359 * while still in use. */
14360 garbage_collect(TRUE);
14361}
14362
Bram Moolenaare0c31f62017-03-01 15:07:05 +010014363/*
14364 * "test_ignore_error()" function
14365 */
14366 static void
14367f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
14368{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014369 ignore_error_for_testing(tv_get_string(&argvars[0]));
Bram Moolenaare0c31f62017-03-01 15:07:05 +010014370}
14371
Bram Moolenaarc0f5a782019-01-13 15:16:13 +010014372 static void
14373f_test_null_blob(typval_T *argvars UNUSED, typval_T *rettv)
14374{
14375 rettv->v_type = VAR_BLOB;
14376 rettv->vval.v_blob = NULL;
14377}
14378
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014379#ifdef FEAT_JOB_CHANNEL
14380 static void
14381f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
14382{
14383 rettv->v_type = VAR_CHANNEL;
14384 rettv->vval.v_channel = NULL;
14385}
14386#endif
14387
14388 static void
14389f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
14390{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014391 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014392}
14393
14394#ifdef FEAT_JOB_CHANNEL
14395 static void
14396f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
14397{
14398 rettv->v_type = VAR_JOB;
14399 rettv->vval.v_job = NULL;
14400}
14401#endif
14402
14403 static void
14404f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
14405{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014406 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014407}
14408
14409 static void
14410f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
14411{
14412 rettv->v_type = VAR_PARTIAL;
14413 rettv->vval.v_partial = NULL;
14414}
14415
14416 static void
14417f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
14418{
14419 rettv->v_type = VAR_STRING;
14420 rettv->vval.v_string = NULL;
14421}
14422
Bram Moolenaarab186732018-09-14 21:27:06 +020014423#ifdef FEAT_GUI
14424 static void
14425f_test_scrollbar(typval_T *argvars, typval_T *rettv UNUSED)
14426{
14427 char_u *which;
14428 long value;
14429 int dragging;
14430 scrollbar_T *sb = NULL;
14431
14432 if (argvars[0].v_type != VAR_STRING
14433 || (argvars[1].v_type) != VAR_NUMBER
14434 || (argvars[2].v_type) != VAR_NUMBER)
14435 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014436 emsg(_(e_invarg));
Bram Moolenaarab186732018-09-14 21:27:06 +020014437 return;
14438 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014439 which = tv_get_string(&argvars[0]);
14440 value = tv_get_number(&argvars[1]);
14441 dragging = tv_get_number(&argvars[2]);
Bram Moolenaarab186732018-09-14 21:27:06 +020014442
14443 if (STRCMP(which, "left") == 0)
14444 sb = &curwin->w_scrollbars[SBAR_LEFT];
14445 else if (STRCMP(which, "right") == 0)
14446 sb = &curwin->w_scrollbars[SBAR_RIGHT];
14447 else if (STRCMP(which, "hor") == 0)
14448 sb = &gui.bottom_sbar;
14449 if (sb == NULL)
14450 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014451 semsg(_(e_invarg2), which);
Bram Moolenaarab186732018-09-14 21:27:06 +020014452 return;
14453 }
14454 gui_drag_scrollbar(sb, value, dragging);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020014455# ifndef USE_ON_FLY_SCROLL
14456 // need to loop through normal_cmd() to handle the scroll events
14457 exec_normal(FALSE, TRUE, FALSE);
14458# endif
Bram Moolenaarab186732018-09-14 21:27:06 +020014459}
14460#endif
14461
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014462 static void
14463f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
14464{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014465 time_for_testing = (time_t)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014466}
14467
14468#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
14469/*
14470 * Get a callback from "arg". It can be a Funcref or a function name.
14471 * When "arg" is zero return an empty string.
14472 * Return NULL for an invalid argument.
14473 */
14474 char_u *
14475get_callback(typval_T *arg, partial_T **pp)
14476{
14477 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
14478 {
14479 *pp = arg->vval.v_partial;
14480 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020014481 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014482 }
14483 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020014484 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014485 {
14486 func_ref(arg->vval.v_string);
14487 return arg->vval.v_string;
14488 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014489 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
14490 return (char_u *)"";
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014491 emsg(_("E921: Invalid callback argument"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014492 return NULL;
14493}
14494
14495/*
Bram Moolenaard5359b22018-04-05 22:44:39 +020014496 * Unref/free "callback" and "partial" returned by get_callback().
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014497 */
14498 void
14499free_callback(char_u *callback, partial_T *partial)
14500{
14501 if (partial != NULL)
14502 partial_unref(partial);
14503 else if (callback != NULL)
14504 {
14505 func_unref(callback);
14506 vim_free(callback);
14507 }
14508}
14509#endif
14510
14511#ifdef FEAT_TIMERS
14512/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014513 * "timer_info([timer])" function
14514 */
14515 static void
14516f_timer_info(typval_T *argvars, typval_T *rettv)
14517{
14518 timer_T *timer = NULL;
14519
14520 if (rettv_list_alloc(rettv) != OK)
14521 return;
14522 if (argvars[0].v_type != VAR_UNKNOWN)
14523 {
14524 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014525 emsg(_(e_number_exp));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014526 else
14527 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014528 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014529 if (timer != NULL)
14530 add_timer_info(rettv, timer);
14531 }
14532 }
14533 else
14534 add_timer_info_all(rettv);
14535}
14536
14537/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014538 * "timer_pause(timer, paused)" function
14539 */
14540 static void
14541f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
14542{
14543 timer_T *timer = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014544 int paused = (int)tv_get_number(&argvars[1]);
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014545
14546 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014547 emsg(_(e_number_exp));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014548 else
14549 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014550 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014551 if (timer != NULL)
14552 timer->tr_paused = paused;
14553 }
14554}
14555
14556/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014557 * "timer_start(time, callback [, options])" function
14558 */
14559 static void
14560f_timer_start(typval_T *argvars, typval_T *rettv)
14561{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014562 long msec = (long)tv_get_number(&argvars[0]);
Bram Moolenaar75537a92016-09-05 22:45:28 +020014563 timer_T *timer;
14564 int repeat = 0;
14565 char_u *callback;
14566 dict_T *dict;
14567 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014568
Bram Moolenaar75537a92016-09-05 22:45:28 +020014569 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014570 if (check_secure())
14571 return;
14572 if (argvars[2].v_type != VAR_UNKNOWN)
14573 {
14574 if (argvars[2].v_type != VAR_DICT
14575 || (dict = argvars[2].vval.v_dict) == NULL)
14576 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014577 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014578 return;
14579 }
14580 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014581 repeat = dict_get_number(dict, (char_u *)"repeat");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014582 }
14583
Bram Moolenaar75537a92016-09-05 22:45:28 +020014584 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014585 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020014586 return;
14587
14588 timer = create_timer(msec, repeat);
14589 if (timer == NULL)
14590 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014591 else
14592 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020014593 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020014594 timer->tr_callback = vim_strsave(callback);
14595 else
14596 /* pointer into the partial */
14597 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020014598 timer->tr_partial = partial;
14599 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014600 }
14601}
14602
14603/*
14604 * "timer_stop(timer)" function
14605 */
14606 static void
14607f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
14608{
14609 timer_T *timer;
14610
14611 if (argvars[0].v_type != VAR_NUMBER)
14612 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014613 emsg(_(e_number_exp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014614 return;
14615 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014616 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014617 if (timer != NULL)
14618 stop_timer(timer);
14619}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014620
14621/*
14622 * "timer_stopall()" function
14623 */
14624 static void
14625f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14626{
14627 stop_all_timers();
14628}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014629#endif
14630
14631/*
14632 * "tolower(string)" function
14633 */
14634 static void
14635f_tolower(typval_T *argvars, typval_T *rettv)
14636{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014637 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014638 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014639}
14640
14641/*
14642 * "toupper(string)" function
14643 */
14644 static void
14645f_toupper(typval_T *argvars, typval_T *rettv)
14646{
14647 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014648 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014649}
14650
14651/*
14652 * "tr(string, fromstr, tostr)" function
14653 */
14654 static void
14655f_tr(typval_T *argvars, typval_T *rettv)
14656{
14657 char_u *in_str;
14658 char_u *fromstr;
14659 char_u *tostr;
14660 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014661 int inlen;
14662 int fromlen;
14663 int tolen;
14664 int idx;
14665 char_u *cpstr;
14666 int cplen;
14667 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014668 char_u buf[NUMBUFLEN];
14669 char_u buf2[NUMBUFLEN];
14670 garray_T ga;
14671
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014672 in_str = tv_get_string(&argvars[0]);
14673 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
14674 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014675
14676 /* Default return value: empty string. */
14677 rettv->v_type = VAR_STRING;
14678 rettv->vval.v_string = NULL;
14679 if (fromstr == NULL || tostr == NULL)
14680 return; /* type error; errmsg already given */
14681 ga_init2(&ga, (int)sizeof(char), 80);
14682
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014683 if (!has_mbyte)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014684 /* not multi-byte: fromstr and tostr must be the same length */
14685 if (STRLEN(fromstr) != STRLEN(tostr))
14686 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014687error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014688 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014689 ga_clear(&ga);
14690 return;
14691 }
14692
14693 /* fromstr and tostr have to contain the same number of chars */
14694 while (*in_str != NUL)
14695 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014696 if (has_mbyte)
14697 {
14698 inlen = (*mb_ptr2len)(in_str);
14699 cpstr = in_str;
14700 cplen = inlen;
14701 idx = 0;
14702 for (p = fromstr; *p != NUL; p += fromlen)
14703 {
14704 fromlen = (*mb_ptr2len)(p);
14705 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
14706 {
14707 for (p = tostr; *p != NUL; p += tolen)
14708 {
14709 tolen = (*mb_ptr2len)(p);
14710 if (idx-- == 0)
14711 {
14712 cplen = tolen;
14713 cpstr = p;
14714 break;
14715 }
14716 }
14717 if (*p == NUL) /* tostr is shorter than fromstr */
14718 goto error;
14719 break;
14720 }
14721 ++idx;
14722 }
14723
14724 if (first && cpstr == in_str)
14725 {
14726 /* Check that fromstr and tostr have the same number of
14727 * (multi-byte) characters. Done only once when a character
14728 * of in_str doesn't appear in fromstr. */
14729 first = FALSE;
14730 for (p = tostr; *p != NUL; p += tolen)
14731 {
14732 tolen = (*mb_ptr2len)(p);
14733 --idx;
14734 }
14735 if (idx != 0)
14736 goto error;
14737 }
14738
14739 (void)ga_grow(&ga, cplen);
14740 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
14741 ga.ga_len += cplen;
14742
14743 in_str += inlen;
14744 }
14745 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014746 {
14747 /* When not using multi-byte chars we can do it faster. */
14748 p = vim_strchr(fromstr, *in_str);
14749 if (p != NULL)
14750 ga_append(&ga, tostr[p - fromstr]);
14751 else
14752 ga_append(&ga, *in_str);
14753 ++in_str;
14754 }
14755 }
14756
14757 /* add a terminating NUL */
14758 (void)ga_grow(&ga, 1);
14759 ga_append(&ga, NUL);
14760
14761 rettv->vval.v_string = ga.ga_data;
14762}
14763
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014764/*
14765 * "trim({expr})" function
14766 */
14767 static void
14768f_trim(typval_T *argvars, typval_T *rettv)
14769{
14770 char_u buf1[NUMBUFLEN];
14771 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014772 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014773 char_u *mask = NULL;
14774 char_u *tail;
14775 char_u *prev;
14776 char_u *p;
14777 int c1;
14778
14779 rettv->v_type = VAR_STRING;
14780 if (head == NULL)
14781 {
14782 rettv->vval.v_string = NULL;
14783 return;
14784 }
14785
14786 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014787 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014788
14789 while (*head != NUL)
14790 {
14791 c1 = PTR2CHAR(head);
14792 if (mask == NULL)
14793 {
14794 if (c1 > ' ' && c1 != 0xa0)
14795 break;
14796 }
14797 else
14798 {
14799 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14800 if (c1 == PTR2CHAR(p))
14801 break;
14802 if (*p == NUL)
14803 break;
14804 }
14805 MB_PTR_ADV(head);
14806 }
14807
14808 for (tail = head + STRLEN(head); tail > head; tail = prev)
14809 {
14810 prev = tail;
14811 MB_PTR_BACK(head, prev);
14812 c1 = PTR2CHAR(prev);
14813 if (mask == NULL)
14814 {
14815 if (c1 > ' ' && c1 != 0xa0)
14816 break;
14817 }
14818 else
14819 {
14820 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14821 if (c1 == PTR2CHAR(p))
14822 break;
14823 if (*p == NUL)
14824 break;
14825 }
14826 }
14827 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
14828}
14829
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014830#ifdef FEAT_FLOAT
14831/*
14832 * "trunc({float})" function
14833 */
14834 static void
14835f_trunc(typval_T *argvars, typval_T *rettv)
14836{
14837 float_T f = 0.0;
14838
14839 rettv->v_type = VAR_FLOAT;
14840 if (get_float_arg(argvars, &f) == OK)
14841 /* trunc() is not in C90, use floor() or ceil() instead. */
14842 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
14843 else
14844 rettv->vval.v_float = 0.0;
14845}
14846#endif
14847
14848/*
14849 * "type(expr)" function
14850 */
14851 static void
14852f_type(typval_T *argvars, typval_T *rettv)
14853{
14854 int n = -1;
14855
14856 switch (argvars[0].v_type)
14857 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020014858 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
14859 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014860 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020014861 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
14862 case VAR_LIST: n = VAR_TYPE_LIST; break;
14863 case VAR_DICT: n = VAR_TYPE_DICT; break;
14864 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014865 case VAR_SPECIAL:
14866 if (argvars[0].vval.v_number == VVAL_FALSE
14867 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020014868 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014869 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020014870 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014871 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020014872 case VAR_JOB: n = VAR_TYPE_JOB; break;
14873 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014874 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014875 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010014876 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014877 n = -1;
14878 break;
14879 }
14880 rettv->vval.v_number = n;
14881}
14882
14883/*
14884 * "undofile(name)" function
14885 */
14886 static void
14887f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
14888{
14889 rettv->v_type = VAR_STRING;
14890#ifdef FEAT_PERSISTENT_UNDO
14891 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014892 char_u *fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014893
14894 if (*fname == NUL)
14895 {
14896 /* If there is no file name there will be no undo file. */
14897 rettv->vval.v_string = NULL;
14898 }
14899 else
14900 {
14901 char_u *ffname = FullName_save(fname, FALSE);
14902
14903 if (ffname != NULL)
14904 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
14905 vim_free(ffname);
14906 }
14907 }
14908#else
14909 rettv->vval.v_string = NULL;
14910#endif
14911}
14912
14913/*
14914 * "undotree()" function
14915 */
14916 static void
14917f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
14918{
14919 if (rettv_dict_alloc(rettv) == OK)
14920 {
14921 dict_T *dict = rettv->vval.v_dict;
14922 list_T *list;
14923
Bram Moolenaare0be1672018-07-08 16:50:37 +020014924 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
14925 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
14926 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
14927 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
14928 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
14929 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014930
14931 list = list_alloc();
14932 if (list != NULL)
14933 {
14934 u_eval_tree(curbuf->b_u_oldhead, list);
14935 dict_add_list(dict, "entries", list);
14936 }
14937 }
14938}
14939
14940/*
14941 * "values(dict)" function
14942 */
14943 static void
14944f_values(typval_T *argvars, typval_T *rettv)
14945{
14946 dict_list(argvars, rettv, 1);
14947}
14948
14949/*
14950 * "virtcol(string)" function
14951 */
14952 static void
14953f_virtcol(typval_T *argvars, typval_T *rettv)
14954{
14955 colnr_T vcol = 0;
14956 pos_T *fp;
14957 int fnum = curbuf->b_fnum;
14958
14959 fp = var2fpos(&argvars[0], FALSE, &fnum);
14960 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
14961 && fnum == curbuf->b_fnum)
14962 {
14963 getvvcol(curwin, fp, NULL, NULL, &vcol);
14964 ++vcol;
14965 }
14966
14967 rettv->vval.v_number = vcol;
14968}
14969
14970/*
14971 * "visualmode()" function
14972 */
14973 static void
14974f_visualmode(typval_T *argvars, typval_T *rettv)
14975{
14976 char_u str[2];
14977
14978 rettv->v_type = VAR_STRING;
14979 str[0] = curbuf->b_visual_mode_eval;
14980 str[1] = NUL;
14981 rettv->vval.v_string = vim_strsave(str);
14982
14983 /* A non-zero number or non-empty string argument: reset mode. */
14984 if (non_zero_arg(&argvars[0]))
14985 curbuf->b_visual_mode_eval = NUL;
14986}
14987
14988/*
14989 * "wildmenumode()" function
14990 */
14991 static void
14992f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14993{
14994#ifdef FEAT_WILDMENU
14995 if (wild_menu_showing)
14996 rettv->vval.v_number = 1;
14997#endif
14998}
14999
15000/*
15001 * "winbufnr(nr)" function
15002 */
15003 static void
15004f_winbufnr(typval_T *argvars, typval_T *rettv)
15005{
15006 win_T *wp;
15007
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015008 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015009 if (wp == NULL)
15010 rettv->vval.v_number = -1;
15011 else
15012 rettv->vval.v_number = wp->w_buffer->b_fnum;
15013}
15014
15015/*
15016 * "wincol()" function
15017 */
15018 static void
15019f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
15020{
15021 validate_cursor();
15022 rettv->vval.v_number = curwin->w_wcol + 1;
15023}
15024
15025/*
15026 * "winheight(nr)" function
15027 */
15028 static void
15029f_winheight(typval_T *argvars, typval_T *rettv)
15030{
15031 win_T *wp;
15032
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015033 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015034 if (wp == NULL)
15035 rettv->vval.v_number = -1;
15036 else
15037 rettv->vval.v_number = wp->w_height;
15038}
15039
15040/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020015041 * "winlayout()" function
15042 */
15043 static void
15044f_winlayout(typval_T *argvars, typval_T *rettv)
15045{
15046 tabpage_T *tp;
15047
15048 if (rettv_list_alloc(rettv) != OK)
15049 return;
15050
15051 if (argvars[0].v_type == VAR_UNKNOWN)
15052 tp = curtab;
15053 else
15054 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015055 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020015056 if (tp == NULL)
15057 return;
15058 }
15059
15060 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
15061}
15062
15063/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015064 * "winline()" function
15065 */
15066 static void
15067f_winline(typval_T *argvars UNUSED, typval_T *rettv)
15068{
15069 validate_cursor();
15070 rettv->vval.v_number = curwin->w_wrow + 1;
15071}
15072
15073/*
15074 * "winnr()" function
15075 */
15076 static void
15077f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
15078{
15079 int nr = 1;
15080
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015081 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015082 rettv->vval.v_number = nr;
15083}
15084
15085/*
15086 * "winrestcmd()" function
15087 */
15088 static void
15089f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
15090{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015091 win_T *wp;
15092 int winnr = 1;
15093 garray_T ga;
15094 char_u buf[50];
15095
15096 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020015097 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015098 {
15099 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
15100 ga_concat(&ga, buf);
15101 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
15102 ga_concat(&ga, buf);
15103 ++winnr;
15104 }
15105 ga_append(&ga, NUL);
15106
15107 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015108 rettv->v_type = VAR_STRING;
15109}
15110
15111/*
15112 * "winrestview()" function
15113 */
15114 static void
15115f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
15116{
15117 dict_T *dict;
15118
15119 if (argvars[0].v_type != VAR_DICT
15120 || (dict = argvars[0].vval.v_dict) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015121 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015122 else
15123 {
15124 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015125 curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015126 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015127 curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015128 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015129 curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015130 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
15131 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010015132 curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015133 curwin->w_set_curswant = FALSE;
15134 }
15135
15136 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015137 set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015138#ifdef FEAT_DIFF
15139 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015140 curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015141#endif
15142 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015143 curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015144 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015145 curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015146
15147 check_cursor();
15148 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020015149 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015150 changed_window_setting();
15151
15152 if (curwin->w_topline <= 0)
15153 curwin->w_topline = 1;
15154 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
15155 curwin->w_topline = curbuf->b_ml.ml_line_count;
15156#ifdef FEAT_DIFF
15157 check_topfill(curwin, TRUE);
15158#endif
15159 }
15160}
15161
15162/*
15163 * "winsaveview()" function
15164 */
15165 static void
15166f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
15167{
15168 dict_T *dict;
15169
15170 if (rettv_dict_alloc(rettv) == FAIL)
15171 return;
15172 dict = rettv->vval.v_dict;
15173
Bram Moolenaare0be1672018-07-08 16:50:37 +020015174 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
15175 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +020015176 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015177 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020015178 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015179
Bram Moolenaare0be1672018-07-08 16:50:37 +020015180 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015181#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020015182 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015183#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020015184 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
15185 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015186}
15187
15188/*
15189 * "winwidth(nr)" function
15190 */
15191 static void
15192f_winwidth(typval_T *argvars, typval_T *rettv)
15193{
15194 win_T *wp;
15195
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015196 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015197 if (wp == NULL)
15198 rettv->vval.v_number = -1;
15199 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015200 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015201}
15202
15203/*
15204 * "wordcount()" function
15205 */
15206 static void
15207f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
15208{
15209 if (rettv_dict_alloc(rettv) == FAIL)
15210 return;
15211 cursor_pos_info(rettv->vval.v_dict);
15212}
15213
15214/*
15215 * "writefile()" function
15216 */
15217 static void
15218f_writefile(typval_T *argvars, typval_T *rettv)
15219{
15220 int binary = FALSE;
15221 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015222#ifdef HAVE_FSYNC
15223 int do_fsync = p_fs;
15224#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015225 char_u *fname;
15226 FILE *fd;
15227 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015228 listitem_T *li;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015229 list_T *list = NULL;
15230 blob_T *blob = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015231
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015232 rettv->vval.v_number = -1;
Bram Moolenaar8c62a082019-02-08 14:34:10 +010015233 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015234 return;
15235
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015236 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015237 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015238 list = argvars[0].vval.v_list;
15239 if (list == NULL)
15240 return;
15241 for (li = list->lv_first; li != NULL; li = li->li_next)
15242 if (tv_get_string_chk(&li->li_tv) == NULL)
15243 return;
15244 }
15245 else if (argvars[0].v_type == VAR_BLOB)
15246 {
15247 blob = argvars[0].vval.v_blob;
15248 if (blob == NULL)
15249 return;
15250 }
15251 else
15252 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015253 semsg(_(e_invarg2), "writefile()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015254 return;
15255 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015256
15257 if (argvars[2].v_type != VAR_UNKNOWN)
15258 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015259 char_u *arg2 = tv_get_string_chk(&argvars[2]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015260
15261 if (arg2 == NULL)
15262 return;
15263 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015264 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015265 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015266 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015267#ifdef HAVE_FSYNC
15268 if (vim_strchr(arg2, 's') != NULL)
15269 do_fsync = TRUE;
15270 else if (vim_strchr(arg2, 'S') != NULL)
15271 do_fsync = FALSE;
15272#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015273 }
15274
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015275 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015276 if (fname == NULL)
15277 return;
15278
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015279 /* Always open the file in binary mode, library functions have a mind of
15280 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015281 if (*fname == NUL || (fd = mch_fopen((char *)fname,
15282 append ? APPENDBIN : WRITEBIN)) == NULL)
15283 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015284 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015285 ret = -1;
15286 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015287 else if (blob)
15288 {
15289 if (write_blob(fd, blob) == FAIL)
15290 ret = -1;
15291#ifdef HAVE_FSYNC
15292 else if (do_fsync)
15293 // Ignore the error, the user wouldn't know what to do about it.
15294 // May happen for a device.
Bram Moolenaara7870192019-02-14 12:56:36 +010015295 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015296#endif
15297 fclose(fd);
15298 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015299 else
15300 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015301 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015302 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015303#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010015304 else if (do_fsync)
15305 /* Ignore the error, the user wouldn't know what to do about it.
15306 * May happen for a device. */
Bram Moolenaara7870192019-02-14 12:56:36 +010015307 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015308#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015309 fclose(fd);
15310 }
15311
15312 rettv->vval.v_number = ret;
15313}
15314
15315/*
15316 * "xor(expr, expr)" function
15317 */
15318 static void
15319f_xor(typval_T *argvars, typval_T *rettv)
15320{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015321 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
15322 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015323}
15324
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015325#endif /* FEAT_EVAL */