blob: 0f126d7734f2c0f862bc81785a111333294d25a5 [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;
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02005988#if defined(FEAT_GUI) || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE))
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005989 {
5990 varnumber_T timeout = 100;
5991
5992 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005993 timeout = tv_get_number(&argvars[0]);
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02005994
5995 (void)ui_get_winpos(&x, &y, timeout);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005996 }
5997#endif
5998 list_append_number(rettv->vval.v_list, (varnumber_T)x);
5999 list_append_number(rettv->vval.v_list, (varnumber_T)y);
6000}
6001
6002
6003/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006004 * "getwinposx()" function
6005 */
6006 static void
6007f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
6008{
6009 rettv->vval.v_number = -1;
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006010#if defined(FEAT_GUI) || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE))
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006011 {
6012 int x, y;
6013
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006014 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006015 rettv->vval.v_number = x;
6016 }
6017#endif
6018}
6019
6020/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006021 * "getwinposy()" function
6022 */
6023 static void
6024f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
6025{
6026 rettv->vval.v_number = -1;
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006027#if defined(FEAT_GUI) || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006028 {
6029 int x, y;
6030
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006031 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006032 rettv->vval.v_number = y;
6033 }
6034#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006035}
6036
6037/*
6038 * "getwinvar()" function
6039 */
6040 static void
6041f_getwinvar(typval_T *argvars, typval_T *rettv)
6042{
6043 getwinvar(argvars, rettv, 0);
6044}
6045
6046/*
6047 * "glob()" function
6048 */
6049 static void
6050f_glob(typval_T *argvars, typval_T *rettv)
6051{
6052 int options = WILD_SILENT|WILD_USE_NL;
6053 expand_T xpc;
6054 int error = FALSE;
6055
6056 /* When the optional second argument is non-zero, don't remove matches
6057 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6058 rettv->v_type = VAR_STRING;
6059 if (argvars[1].v_type != VAR_UNKNOWN)
6060 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006061 if (tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006062 options |= WILD_KEEP_ALL;
6063 if (argvars[2].v_type != VAR_UNKNOWN)
6064 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006065 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006066 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006067 if (argvars[3].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006068 && tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006069 options |= WILD_ALLLINKS;
6070 }
6071 }
6072 if (!error)
6073 {
6074 ExpandInit(&xpc);
6075 xpc.xp_context = EXPAND_FILES;
6076 if (p_wic)
6077 options += WILD_ICASE;
6078 if (rettv->v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006079 rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006080 NULL, options, WILD_ALL);
6081 else if (rettv_list_alloc(rettv) != FAIL)
6082 {
6083 int i;
6084
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006085 ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006086 NULL, options, WILD_ALL_KEEP);
6087 for (i = 0; i < xpc.xp_numfiles; i++)
6088 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
6089
6090 ExpandCleanup(&xpc);
6091 }
6092 }
6093 else
6094 rettv->vval.v_string = NULL;
6095}
6096
6097/*
6098 * "globpath()" function
6099 */
6100 static void
6101f_globpath(typval_T *argvars, typval_T *rettv)
6102{
6103 int flags = 0;
6104 char_u buf1[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006105 char_u *file = tv_get_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006106 int error = FALSE;
6107 garray_T ga;
6108 int i;
6109
6110 /* When the optional second argument is non-zero, don't remove matches
6111 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6112 rettv->v_type = VAR_STRING;
6113 if (argvars[2].v_type != VAR_UNKNOWN)
6114 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006115 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006116 flags |= WILD_KEEP_ALL;
6117 if (argvars[3].v_type != VAR_UNKNOWN)
6118 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006119 if (tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006120 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006121 if (argvars[4].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006122 && tv_get_number_chk(&argvars[4], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006123 flags |= WILD_ALLLINKS;
6124 }
6125 }
6126 if (file != NULL && !error)
6127 {
6128 ga_init2(&ga, (int)sizeof(char_u *), 10);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006129 globpath(tv_get_string(&argvars[0]), file, &ga, flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006130 if (rettv->v_type == VAR_STRING)
6131 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
6132 else if (rettv_list_alloc(rettv) != FAIL)
6133 for (i = 0; i < ga.ga_len; ++i)
6134 list_append_string(rettv->vval.v_list,
6135 ((char_u **)(ga.ga_data))[i], -1);
6136 ga_clear_strings(&ga);
6137 }
6138 else
6139 rettv->vval.v_string = NULL;
6140}
6141
6142/*
6143 * "glob2regpat()" function
6144 */
6145 static void
6146f_glob2regpat(typval_T *argvars, typval_T *rettv)
6147{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006148 char_u *pat = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006149
6150 rettv->v_type = VAR_STRING;
6151 rettv->vval.v_string = (pat == NULL)
6152 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
6153}
6154
6155/* for VIM_VERSION_ defines */
6156#include "version.h"
6157
6158/*
6159 * "has()" function
6160 */
6161 static void
6162f_has(typval_T *argvars, typval_T *rettv)
6163{
6164 int i;
6165 char_u *name;
6166 int n = FALSE;
6167 static char *(has_list[]) =
6168 {
6169#ifdef AMIGA
6170 "amiga",
6171# ifdef FEAT_ARP
6172 "arp",
6173# endif
6174#endif
6175#ifdef __BEOS__
6176 "beos",
6177#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006178#if defined(BSD) && !defined(MACOS_X)
6179 "bsd",
6180#endif
6181#ifdef hpux
6182 "hpux",
6183#endif
6184#ifdef __linux__
6185 "linux",
6186#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02006187#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01006188 "mac", /* Mac OS X (and, once, Mac OS Classic) */
6189 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02006190# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01006191 "macunix", /* Mac OS X, with the darwin feature */
6192 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02006193# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006194#endif
6195#ifdef __QNX__
6196 "qnx",
6197#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006198#ifdef SUN_SYSTEM
6199 "sun",
6200#else
6201 "moon",
6202#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006203#ifdef UNIX
6204 "unix",
6205#endif
6206#ifdef VMS
6207 "vms",
6208#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006209#ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006210 "win32",
6211#endif
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006212#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006213 "win32unix",
6214#endif
Bram Moolenaar44b443c2019-02-18 22:14:18 +01006215#ifdef _WIN64
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006216 "win64",
6217#endif
6218#ifdef EBCDIC
6219 "ebcdic",
6220#endif
6221#ifndef CASE_INSENSITIVE_FILENAME
6222 "fname_case",
6223#endif
6224#ifdef HAVE_ACL
6225 "acl",
6226#endif
6227#ifdef FEAT_ARABIC
6228 "arabic",
6229#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006230 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006231#ifdef FEAT_AUTOCHDIR
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006232 "autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006233#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006234#ifdef FEAT_AUTOSERVERNAME
6235 "autoservername",
6236#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006237#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006238 "balloon_eval",
Bram Moolenaar4f974752019-02-17 17:44:42 +01006239# ifndef FEAT_GUI_MSWIN /* other GUIs always have multiline balloons */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006240 "balloon_multiline",
6241# endif
6242#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006243#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006244 "balloon_eval_term",
6245#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006246#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6247 "builtin_terms",
6248# ifdef ALL_BUILTIN_TCAPS
6249 "all_builtin_terms",
6250# endif
6251#endif
6252#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01006253 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006254 || defined(FEAT_GUI_MOTIF))
6255 "browsefilter",
6256#endif
6257#ifdef FEAT_BYTEOFF
6258 "byte_offset",
6259#endif
6260#ifdef FEAT_JOB_CHANNEL
6261 "channel",
6262#endif
6263#ifdef FEAT_CINDENT
6264 "cindent",
6265#endif
6266#ifdef FEAT_CLIENTSERVER
6267 "clientserver",
6268#endif
6269#ifdef FEAT_CLIPBOARD
6270 "clipboard",
6271#endif
6272#ifdef FEAT_CMDL_COMPL
6273 "cmdline_compl",
6274#endif
6275#ifdef FEAT_CMDHIST
6276 "cmdline_hist",
6277#endif
6278#ifdef FEAT_COMMENTS
6279 "comments",
6280#endif
6281#ifdef FEAT_CONCEAL
6282 "conceal",
6283#endif
6284#ifdef FEAT_CRYPT
6285 "cryptv",
6286 "crypt-blowfish",
6287 "crypt-blowfish2",
6288#endif
6289#ifdef FEAT_CSCOPE
6290 "cscope",
6291#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006292 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006293#ifdef CURSOR_SHAPE
6294 "cursorshape",
6295#endif
6296#ifdef DEBUG
6297 "debug",
6298#endif
6299#ifdef FEAT_CON_DIALOG
6300 "dialog_con",
6301#endif
6302#ifdef FEAT_GUI_DIALOG
6303 "dialog_gui",
6304#endif
6305#ifdef FEAT_DIFF
6306 "diff",
6307#endif
6308#ifdef FEAT_DIGRAPHS
6309 "digraphs",
6310#endif
6311#ifdef FEAT_DIRECTX
6312 "directx",
6313#endif
6314#ifdef FEAT_DND
6315 "dnd",
6316#endif
6317#ifdef FEAT_EMACS_TAGS
6318 "emacs_tags",
6319#endif
6320 "eval", /* always present, of course! */
6321 "ex_extra", /* graduated feature */
6322#ifdef FEAT_SEARCH_EXTRA
6323 "extra_search",
6324#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006325#ifdef FEAT_SEARCHPATH
6326 "file_in_path",
6327#endif
6328#ifdef FEAT_FILTERPIPE
6329 "filterpipe",
6330#endif
6331#ifdef FEAT_FIND_ID
6332 "find_in_path",
6333#endif
6334#ifdef FEAT_FLOAT
6335 "float",
6336#endif
6337#ifdef FEAT_FOLDING
6338 "folding",
6339#endif
6340#ifdef FEAT_FOOTER
6341 "footer",
6342#endif
6343#if !defined(USE_SYSTEM) && defined(UNIX)
6344 "fork",
6345#endif
6346#ifdef FEAT_GETTEXT
6347 "gettext",
6348#endif
6349#ifdef FEAT_GUI
6350 "gui",
6351#endif
6352#ifdef FEAT_GUI_ATHENA
6353# ifdef FEAT_GUI_NEXTAW
6354 "gui_neXtaw",
6355# else
6356 "gui_athena",
6357# endif
6358#endif
6359#ifdef FEAT_GUI_GTK
6360 "gui_gtk",
6361# ifdef USE_GTK3
6362 "gui_gtk3",
6363# else
6364 "gui_gtk2",
6365# endif
6366#endif
6367#ifdef FEAT_GUI_GNOME
6368 "gui_gnome",
6369#endif
6370#ifdef FEAT_GUI_MAC
6371 "gui_mac",
6372#endif
6373#ifdef FEAT_GUI_MOTIF
6374 "gui_motif",
6375#endif
6376#ifdef FEAT_GUI_PHOTON
6377 "gui_photon",
6378#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006379#ifdef FEAT_GUI_MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006380 "gui_win32",
6381#endif
6382#ifdef FEAT_HANGULIN
6383 "hangul_input",
6384#endif
6385#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6386 "iconv",
6387#endif
6388#ifdef FEAT_INS_EXPAND
6389 "insert_expand",
6390#endif
6391#ifdef FEAT_JOB_CHANNEL
6392 "job",
6393#endif
6394#ifdef FEAT_JUMPLIST
6395 "jumplist",
6396#endif
6397#ifdef FEAT_KEYMAP
6398 "keymap",
6399#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006400 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006401#ifdef FEAT_LANGMAP
6402 "langmap",
6403#endif
6404#ifdef FEAT_LIBCALL
6405 "libcall",
6406#endif
6407#ifdef FEAT_LINEBREAK
6408 "linebreak",
6409#endif
6410#ifdef FEAT_LISP
6411 "lispindent",
6412#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006413 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006414#ifdef FEAT_LOCALMAP
6415 "localmap",
6416#endif
6417#ifdef FEAT_LUA
6418# ifndef DYNAMIC_LUA
6419 "lua",
6420# endif
6421#endif
6422#ifdef FEAT_MENU
6423 "menu",
6424#endif
6425#ifdef FEAT_SESSION
6426 "mksession",
6427#endif
6428#ifdef FEAT_MODIFY_FNAME
6429 "modify_fname",
6430#endif
6431#ifdef FEAT_MOUSE
6432 "mouse",
6433#endif
6434#ifdef FEAT_MOUSESHAPE
6435 "mouseshape",
6436#endif
6437#if defined(UNIX) || defined(VMS)
6438# ifdef FEAT_MOUSE_DEC
6439 "mouse_dec",
6440# endif
6441# ifdef FEAT_MOUSE_GPM
6442 "mouse_gpm",
6443# endif
6444# ifdef FEAT_MOUSE_JSB
6445 "mouse_jsbterm",
6446# endif
6447# ifdef FEAT_MOUSE_NET
6448 "mouse_netterm",
6449# endif
6450# ifdef FEAT_MOUSE_PTERM
6451 "mouse_pterm",
6452# endif
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01006453# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006454 "mouse_sgr",
6455# endif
6456# ifdef FEAT_SYSMOUSE
6457 "mouse_sysmouse",
6458# endif
6459# ifdef FEAT_MOUSE_URXVT
6460 "mouse_urxvt",
6461# endif
6462# ifdef FEAT_MOUSE_XTERM
6463 "mouse_xterm",
6464# endif
6465#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006466 "multi_byte",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006467#ifdef FEAT_MBYTE_IME
6468 "multi_byte_ime",
6469#endif
6470#ifdef FEAT_MULTI_LANG
6471 "multi_lang",
6472#endif
6473#ifdef FEAT_MZSCHEME
6474#ifndef DYNAMIC_MZSCHEME
6475 "mzscheme",
6476#endif
6477#endif
6478#ifdef FEAT_NUM64
6479 "num64",
6480#endif
6481#ifdef FEAT_OLE
6482 "ole",
6483#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006484#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006485 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006486#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006487#ifdef FEAT_PATH_EXTRA
6488 "path_extra",
6489#endif
6490#ifdef FEAT_PERL
6491#ifndef DYNAMIC_PERL
6492 "perl",
6493#endif
6494#endif
6495#ifdef FEAT_PERSISTENT_UNDO
6496 "persistent_undo",
6497#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006498#if defined(FEAT_PYTHON)
6499 "python_compiled",
6500# if defined(DYNAMIC_PYTHON)
6501 "python_dynamic",
6502# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006503 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006504 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006505# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006506#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006507#if defined(FEAT_PYTHON3)
6508 "python3_compiled",
6509# if defined(DYNAMIC_PYTHON3)
6510 "python3_dynamic",
6511# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006512 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006513 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006514# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006515#endif
6516#ifdef FEAT_POSTSCRIPT
6517 "postscript",
6518#endif
6519#ifdef FEAT_PRINTER
6520 "printer",
6521#endif
6522#ifdef FEAT_PROFILE
6523 "profile",
6524#endif
6525#ifdef FEAT_RELTIME
6526 "reltime",
6527#endif
6528#ifdef FEAT_QUICKFIX
6529 "quickfix",
6530#endif
6531#ifdef FEAT_RIGHTLEFT
6532 "rightleft",
6533#endif
6534#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6535 "ruby",
6536#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006537 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006538#ifdef FEAT_CMDL_INFO
6539 "showcmd",
6540 "cmdline_info",
6541#endif
6542#ifdef FEAT_SIGNS
6543 "signs",
6544#endif
6545#ifdef FEAT_SMARTINDENT
6546 "smartindent",
6547#endif
6548#ifdef STARTUPTIME
6549 "startuptime",
6550#endif
6551#ifdef FEAT_STL_OPT
6552 "statusline",
6553#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006554#ifdef FEAT_NETBEANS_INTG
6555 "netbeans_intg",
6556#endif
6557#ifdef FEAT_SPELL
6558 "spell",
6559#endif
6560#ifdef FEAT_SYN_HL
6561 "syntax",
6562#endif
6563#if defined(USE_SYSTEM) || !defined(UNIX)
6564 "system",
6565#endif
6566#ifdef FEAT_TAG_BINS
6567 "tag_binary",
6568#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006569#ifdef FEAT_TCL
6570# ifndef DYNAMIC_TCL
6571 "tcl",
6572# endif
6573#endif
6574#ifdef FEAT_TERMGUICOLORS
6575 "termguicolors",
6576#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006577#if defined(FEAT_TERMINAL) && !defined(MSWIN)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006578 "terminal",
6579#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006580#ifdef TERMINFO
6581 "terminfo",
6582#endif
6583#ifdef FEAT_TERMRESPONSE
6584 "termresponse",
6585#endif
6586#ifdef FEAT_TEXTOBJ
6587 "textobjects",
6588#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006589#ifdef FEAT_TEXT_PROP
6590 "textprop",
6591#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006592#ifdef HAVE_TGETENT
6593 "tgetent",
6594#endif
6595#ifdef FEAT_TIMERS
6596 "timers",
6597#endif
6598#ifdef FEAT_TITLE
6599 "title",
6600#endif
6601#ifdef FEAT_TOOLBAR
6602 "toolbar",
6603#endif
6604#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6605 "unnamedplus",
6606#endif
6607#ifdef FEAT_USR_CMDS
6608 "user-commands", /* was accidentally included in 5.4 */
6609 "user_commands",
6610#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006611#ifdef FEAT_VARTABS
6612 "vartabs",
6613#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006614 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006615#ifdef FEAT_VIMINFO
6616 "viminfo",
6617#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006618 "vimscript-1",
6619 "vimscript-2",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006620 "virtualedit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006621 "visual",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006622 "visualextra",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006623 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006624#ifdef FEAT_VTP
6625 "vtp",
6626#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006627#ifdef FEAT_WILDIGN
6628 "wildignore",
6629#endif
6630#ifdef FEAT_WILDMENU
6631 "wildmenu",
6632#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006633 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006634#ifdef FEAT_WAK
6635 "winaltkeys",
6636#endif
6637#ifdef FEAT_WRITEBACKUP
6638 "writebackup",
6639#endif
6640#ifdef FEAT_XIM
6641 "xim",
6642#endif
6643#ifdef FEAT_XFONTSET
6644 "xfontset",
6645#endif
6646#ifdef FEAT_XPM_W32
6647 "xpm",
6648 "xpm_w32", /* for backward compatibility */
6649#else
6650# if defined(HAVE_XPM)
6651 "xpm",
6652# endif
6653#endif
6654#ifdef USE_XSMP
6655 "xsmp",
6656#endif
6657#ifdef USE_XSMP_INTERACT
6658 "xsmp_interact",
6659#endif
6660#ifdef FEAT_XCLIPBOARD
6661 "xterm_clipboard",
6662#endif
6663#ifdef FEAT_XTERM_SAVE
6664 "xterm_save",
6665#endif
6666#if defined(UNIX) && defined(FEAT_X11)
6667 "X11",
6668#endif
6669 NULL
6670 };
6671
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006672 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006673 for (i = 0; has_list[i] != NULL; ++i)
6674 if (STRICMP(name, has_list[i]) == 0)
6675 {
6676 n = TRUE;
6677 break;
6678 }
6679
6680 if (n == FALSE)
6681 {
6682 if (STRNICMP(name, "patch", 5) == 0)
6683 {
6684 if (name[5] == '-'
6685 && STRLEN(name) >= 11
6686 && vim_isdigit(name[6])
6687 && vim_isdigit(name[8])
6688 && vim_isdigit(name[10]))
6689 {
6690 int major = atoi((char *)name + 6);
6691 int minor = atoi((char *)name + 8);
6692
6693 /* Expect "patch-9.9.01234". */
6694 n = (major < VIM_VERSION_MAJOR
6695 || (major == VIM_VERSION_MAJOR
6696 && (minor < VIM_VERSION_MINOR
6697 || (minor == VIM_VERSION_MINOR
6698 && has_patch(atoi((char *)name + 10))))));
6699 }
6700 else
6701 n = has_patch(atoi((char *)name + 5));
6702 }
6703 else if (STRICMP(name, "vim_starting") == 0)
6704 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006705 else if (STRICMP(name, "ttyin") == 0)
6706 n = mch_input_isatty();
6707 else if (STRICMP(name, "ttyout") == 0)
6708 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006709 else if (STRICMP(name, "multi_byte_encoding") == 0)
6710 n = has_mbyte;
Bram Moolenaar4f974752019-02-17 17:44:42 +01006711#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006712 else if (STRICMP(name, "balloon_multiline") == 0)
6713 n = multiline_balloon_available();
6714#endif
6715#ifdef DYNAMIC_TCL
6716 else if (STRICMP(name, "tcl") == 0)
6717 n = tcl_enabled(FALSE);
6718#endif
6719#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6720 else if (STRICMP(name, "iconv") == 0)
6721 n = iconv_enabled(FALSE);
6722#endif
6723#ifdef DYNAMIC_LUA
6724 else if (STRICMP(name, "lua") == 0)
6725 n = lua_enabled(FALSE);
6726#endif
6727#ifdef DYNAMIC_MZSCHEME
6728 else if (STRICMP(name, "mzscheme") == 0)
6729 n = mzscheme_enabled(FALSE);
6730#endif
6731#ifdef DYNAMIC_RUBY
6732 else if (STRICMP(name, "ruby") == 0)
6733 n = ruby_enabled(FALSE);
6734#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006735#ifdef DYNAMIC_PYTHON
6736 else if (STRICMP(name, "python") == 0)
6737 n = python_enabled(FALSE);
6738#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006739#ifdef DYNAMIC_PYTHON3
6740 else if (STRICMP(name, "python3") == 0)
6741 n = python3_enabled(FALSE);
6742#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006743#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6744 else if (STRICMP(name, "pythonx") == 0)
6745 {
6746# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6747 if (p_pyx == 0)
6748 n = python3_enabled(FALSE) || python_enabled(FALSE);
6749 else if (p_pyx == 3)
6750 n = python3_enabled(FALSE);
6751 else if (p_pyx == 2)
6752 n = python_enabled(FALSE);
6753# elif defined(DYNAMIC_PYTHON)
6754 n = python_enabled(FALSE);
6755# elif defined(DYNAMIC_PYTHON3)
6756 n = python3_enabled(FALSE);
6757# endif
6758 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006759#endif
6760#ifdef DYNAMIC_PERL
6761 else if (STRICMP(name, "perl") == 0)
6762 n = perl_enabled(FALSE);
6763#endif
6764#ifdef FEAT_GUI
6765 else if (STRICMP(name, "gui_running") == 0)
6766 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006767# ifdef FEAT_BROWSE
6768 else if (STRICMP(name, "browse") == 0)
6769 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6770# endif
6771#endif
6772#ifdef FEAT_SYN_HL
6773 else if (STRICMP(name, "syntax_items") == 0)
6774 n = syntax_present(curwin);
6775#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006776#ifdef FEAT_VTP
6777 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02006778 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006779#endif
6780#ifdef FEAT_NETBEANS_INTG
6781 else if (STRICMP(name, "netbeans_enabled") == 0)
6782 n = netbeans_active();
6783#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006784#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02006785 else if (STRICMP(name, "terminal") == 0)
6786 n = terminal_enabled();
6787#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006788#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006789 else if (STRICMP(name, "conpty") == 0)
6790 n = use_conpty();
6791#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006792 }
6793
6794 rettv->vval.v_number = n;
6795}
6796
6797/*
6798 * "has_key()" function
6799 */
6800 static void
6801f_has_key(typval_T *argvars, typval_T *rettv)
6802{
6803 if (argvars[0].v_type != VAR_DICT)
6804 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006805 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006806 return;
6807 }
6808 if (argvars[0].vval.v_dict == NULL)
6809 return;
6810
6811 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006812 tv_get_string(&argvars[1]), -1) != NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006813}
6814
6815/*
6816 * "haslocaldir()" function
6817 */
6818 static void
6819f_haslocaldir(typval_T *argvars, typval_T *rettv)
6820{
6821 win_T *wp = NULL;
6822
6823 wp = find_tabwin(&argvars[0], &argvars[1]);
6824 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6825}
6826
6827/*
6828 * "hasmapto()" function
6829 */
6830 static void
6831f_hasmapto(typval_T *argvars, typval_T *rettv)
6832{
6833 char_u *name;
6834 char_u *mode;
6835 char_u buf[NUMBUFLEN];
6836 int abbr = FALSE;
6837
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006838 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006839 if (argvars[1].v_type == VAR_UNKNOWN)
6840 mode = (char_u *)"nvo";
6841 else
6842 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006843 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006844 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006845 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006846 }
6847
6848 if (map_to_exists(name, mode, abbr))
6849 rettv->vval.v_number = TRUE;
6850 else
6851 rettv->vval.v_number = FALSE;
6852}
6853
6854/*
6855 * "histadd()" function
6856 */
6857 static void
6858f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6859{
6860#ifdef FEAT_CMDHIST
6861 int histype;
6862 char_u *str;
6863 char_u buf[NUMBUFLEN];
6864#endif
6865
6866 rettv->vval.v_number = FALSE;
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006867 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006868 return;
6869#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006870 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006871 histype = str != NULL ? get_histtype(str) : -1;
6872 if (histype >= 0)
6873 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006874 str = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006875 if (*str != NUL)
6876 {
6877 init_history();
6878 add_to_history(histype, str, FALSE, NUL);
6879 rettv->vval.v_number = TRUE;
6880 return;
6881 }
6882 }
6883#endif
6884}
6885
6886/*
6887 * "histdel()" function
6888 */
6889 static void
6890f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6891{
6892#ifdef FEAT_CMDHIST
6893 int n;
6894 char_u buf[NUMBUFLEN];
6895 char_u *str;
6896
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006897 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006898 if (str == NULL)
6899 n = 0;
6900 else if (argvars[1].v_type == VAR_UNKNOWN)
6901 /* only one argument: clear entire history */
6902 n = clr_history(get_histtype(str));
6903 else if (argvars[1].v_type == VAR_NUMBER)
6904 /* index given: remove that entry */
6905 n = del_history_idx(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006906 (int)tv_get_number(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006907 else
6908 /* string given: remove all matching entries */
6909 n = del_history_entry(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006910 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006911 rettv->vval.v_number = n;
6912#endif
6913}
6914
6915/*
6916 * "histget()" function
6917 */
6918 static void
6919f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6920{
6921#ifdef FEAT_CMDHIST
6922 int type;
6923 int idx;
6924 char_u *str;
6925
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006926 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006927 if (str == NULL)
6928 rettv->vval.v_string = NULL;
6929 else
6930 {
6931 type = get_histtype(str);
6932 if (argvars[1].v_type == VAR_UNKNOWN)
6933 idx = get_history_idx(type);
6934 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006935 idx = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006936 /* -1 on type error */
6937 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6938 }
6939#else
6940 rettv->vval.v_string = NULL;
6941#endif
6942 rettv->v_type = VAR_STRING;
6943}
6944
6945/*
6946 * "histnr()" function
6947 */
6948 static void
6949f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6950{
6951 int i;
6952
6953#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006954 char_u *history = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006955
6956 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6957 if (i >= HIST_CMD && i < HIST_COUNT)
6958 i = get_history_idx(i);
6959 else
6960#endif
6961 i = -1;
6962 rettv->vval.v_number = i;
6963}
6964
6965/*
6966 * "highlightID(name)" function
6967 */
6968 static void
6969f_hlID(typval_T *argvars, typval_T *rettv)
6970{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006971 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006972}
6973
6974/*
6975 * "highlight_exists()" function
6976 */
6977 static void
6978f_hlexists(typval_T *argvars, typval_T *rettv)
6979{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006980 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006981}
6982
6983/*
6984 * "hostname()" function
6985 */
6986 static void
6987f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6988{
6989 char_u hostname[256];
6990
6991 mch_get_host_name(hostname, 256);
6992 rettv->v_type = VAR_STRING;
6993 rettv->vval.v_string = vim_strsave(hostname);
6994}
6995
6996/*
6997 * iconv() function
6998 */
6999 static void
7000f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
7001{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007002 char_u buf1[NUMBUFLEN];
7003 char_u buf2[NUMBUFLEN];
7004 char_u *from, *to, *str;
7005 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007006
7007 rettv->v_type = VAR_STRING;
7008 rettv->vval.v_string = NULL;
7009
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007010 str = tv_get_string(&argvars[0]);
7011 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
7012 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007013 vimconv.vc_type = CONV_NONE;
7014 convert_setup(&vimconv, from, to);
7015
7016 /* If the encodings are equal, no conversion needed. */
7017 if (vimconv.vc_type == CONV_NONE)
7018 rettv->vval.v_string = vim_strsave(str);
7019 else
7020 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
7021
7022 convert_setup(&vimconv, NULL, NULL);
7023 vim_free(from);
7024 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007025}
7026
7027/*
7028 * "indent()" function
7029 */
7030 static void
7031f_indent(typval_T *argvars, typval_T *rettv)
7032{
7033 linenr_T lnum;
7034
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007035 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007036 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7037 rettv->vval.v_number = get_indent_lnum(lnum);
7038 else
7039 rettv->vval.v_number = -1;
7040}
7041
7042/*
7043 * "index()" function
7044 */
7045 static void
7046f_index(typval_T *argvars, typval_T *rettv)
7047{
7048 list_T *l;
7049 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007050 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007051 long idx = 0;
7052 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007053 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007054
7055 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007056 if (argvars[0].v_type == VAR_BLOB)
7057 {
7058 typval_T tv;
7059 int start = 0;
7060
7061 if (argvars[2].v_type != VAR_UNKNOWN)
7062 {
7063 start = tv_get_number_chk(&argvars[2], &error);
7064 if (error)
7065 return;
7066 }
7067 b = argvars[0].vval.v_blob;
7068 if (b == NULL)
7069 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01007070 if (start < 0)
7071 {
7072 start = blob_len(b) + start;
7073 if (start < 0)
7074 start = 0;
7075 }
7076
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007077 for (idx = start; idx < blob_len(b); ++idx)
7078 {
7079 tv.v_type = VAR_NUMBER;
7080 tv.vval.v_number = blob_get(b, idx);
7081 if (tv_equal(&tv, &argvars[1], ic, FALSE))
7082 {
7083 rettv->vval.v_number = idx;
7084 return;
7085 }
7086 }
7087 return;
7088 }
7089 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007090 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007091 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007092 return;
7093 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007094
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007095 l = argvars[0].vval.v_list;
7096 if (l != NULL)
7097 {
7098 item = l->lv_first;
7099 if (argvars[2].v_type != VAR_UNKNOWN)
7100 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007101 /* Start at specified item. Use the cached index that list_find()
7102 * sets, so that a negative number also works. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007103 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007104 idx = l->lv_idx;
7105 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007106 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007107 if (error)
7108 item = NULL;
7109 }
7110
7111 for ( ; item != NULL; item = item->li_next, ++idx)
7112 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
7113 {
7114 rettv->vval.v_number = idx;
7115 break;
7116 }
7117 }
7118}
7119
7120static int inputsecret_flag = 0;
7121
7122/*
7123 * "input()" function
7124 * Also handles inputsecret() when inputsecret is set.
7125 */
7126 static void
7127f_input(typval_T *argvars, typval_T *rettv)
7128{
7129 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
7130}
7131
7132/*
7133 * "inputdialog()" function
7134 */
7135 static void
7136f_inputdialog(typval_T *argvars, typval_T *rettv)
7137{
7138#if defined(FEAT_GUI_TEXTDIALOG)
7139 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
7140 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
7141 {
7142 char_u *message;
7143 char_u buf[NUMBUFLEN];
7144 char_u *defstr = (char_u *)"";
7145
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007146 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007147 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007148 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007149 vim_strncpy(IObuff, defstr, IOSIZE - 1);
7150 else
7151 IObuff[0] = NUL;
7152 if (message != NULL && defstr != NULL
7153 && do_dialog(VIM_QUESTION, NULL, message,
7154 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
7155 rettv->vval.v_string = vim_strsave(IObuff);
7156 else
7157 {
7158 if (message != NULL && defstr != NULL
7159 && argvars[1].v_type != VAR_UNKNOWN
7160 && argvars[2].v_type != VAR_UNKNOWN)
7161 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007162 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007163 else
7164 rettv->vval.v_string = NULL;
7165 }
7166 rettv->v_type = VAR_STRING;
7167 }
7168 else
7169#endif
7170 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
7171}
7172
7173/*
7174 * "inputlist()" function
7175 */
7176 static void
7177f_inputlist(typval_T *argvars, typval_T *rettv)
7178{
7179 listitem_T *li;
7180 int selected;
7181 int mouse_used;
7182
7183#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007184 /* While starting up, there is no place to enter text. When running tests
7185 * with --not-a-term we assume feedkeys() will be used. */
7186 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007187 return;
7188#endif
7189 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
7190 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007191 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007192 return;
7193 }
7194
7195 msg_start();
7196 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
7197 lines_left = Rows; /* avoid more prompt */
7198 msg_scroll = TRUE;
7199 msg_clr_eos();
7200
7201 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
7202 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007203 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007204 msg_putchar('\n');
7205 }
7206
7207 /* Ask for choice. */
7208 selected = prompt_for_number(&mouse_used);
7209 if (mouse_used)
7210 selected -= lines_left;
7211
7212 rettv->vval.v_number = selected;
7213}
7214
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007215static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7216
7217/*
7218 * "inputrestore()" function
7219 */
7220 static void
7221f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7222{
7223 if (ga_userinput.ga_len > 0)
7224 {
7225 --ga_userinput.ga_len;
7226 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7227 + ga_userinput.ga_len);
7228 /* default return is zero == OK */
7229 }
7230 else if (p_verbose > 1)
7231 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007232 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007233 rettv->vval.v_number = 1; /* Failed */
7234 }
7235}
7236
7237/*
7238 * "inputsave()" function
7239 */
7240 static void
7241f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7242{
7243 /* Add an entry to the stack of typeahead storage. */
7244 if (ga_grow(&ga_userinput, 1) == OK)
7245 {
7246 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7247 + ga_userinput.ga_len);
7248 ++ga_userinput.ga_len;
7249 /* default return is zero == OK */
7250 }
7251 else
7252 rettv->vval.v_number = 1; /* Failed */
7253}
7254
7255/*
7256 * "inputsecret()" function
7257 */
7258 static void
7259f_inputsecret(typval_T *argvars, typval_T *rettv)
7260{
7261 ++cmdline_star;
7262 ++inputsecret_flag;
7263 f_input(argvars, rettv);
7264 --cmdline_star;
7265 --inputsecret_flag;
7266}
7267
7268/*
7269 * "insert()" function
7270 */
7271 static void
7272f_insert(typval_T *argvars, typval_T *rettv)
7273{
7274 long before = 0;
7275 listitem_T *item;
7276 list_T *l;
7277 int error = FALSE;
7278
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007279 if (argvars[0].v_type == VAR_BLOB)
7280 {
7281 int val, len;
7282 char_u *p;
7283
7284 len = blob_len(argvars[0].vval.v_blob);
7285 if (argvars[2].v_type != VAR_UNKNOWN)
7286 {
7287 before = (long)tv_get_number_chk(&argvars[2], &error);
7288 if (error)
7289 return; // type error; errmsg already given
7290 if (before < 0 || before > len)
7291 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007292 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007293 return;
7294 }
7295 }
7296 val = tv_get_number_chk(&argvars[1], &error);
7297 if (error)
7298 return;
7299 if (val < 0 || val > 255)
7300 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007301 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007302 return;
7303 }
7304
7305 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
7306 return;
7307 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
7308 mch_memmove(p + before + 1, p + before, (size_t)len - before);
7309 *(p + before) = val;
7310 ++argvars[0].vval.v_blob->bv_ga.ga_len;
7311
7312 copy_tv(&argvars[0], rettv);
7313 }
7314 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007315 semsg(_(e_listblobarg), "insert()");
Bram Moolenaar05c00c02019-02-11 22:00:11 +01007316 else if ((l = argvars[0].vval.v_list) != NULL
7317 && !var_check_lock(l->lv_lock,
7318 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007319 {
7320 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007321 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007322 if (error)
7323 return; /* type error; errmsg already given */
7324
7325 if (before == l->lv_len)
7326 item = NULL;
7327 else
7328 {
7329 item = list_find(l, before);
7330 if (item == NULL)
7331 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007332 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007333 l = NULL;
7334 }
7335 }
7336 if (l != NULL)
7337 {
7338 list_insert_tv(l, &argvars[1], item);
7339 copy_tv(&argvars[0], rettv);
7340 }
7341 }
7342}
7343
7344/*
7345 * "invert(expr)" function
7346 */
7347 static void
7348f_invert(typval_T *argvars, typval_T *rettv)
7349{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007350 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007351}
7352
7353/*
7354 * "isdirectory()" function
7355 */
7356 static void
7357f_isdirectory(typval_T *argvars, typval_T *rettv)
7358{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007359 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007360}
7361
7362/*
7363 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7364 * or it refers to a List or Dictionary that is locked.
7365 */
7366 static int
7367tv_islocked(typval_T *tv)
7368{
7369 return (tv->v_lock & VAR_LOCKED)
7370 || (tv->v_type == VAR_LIST
7371 && tv->vval.v_list != NULL
7372 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7373 || (tv->v_type == VAR_DICT
7374 && tv->vval.v_dict != NULL
7375 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7376}
7377
7378/*
7379 * "islocked()" function
7380 */
7381 static void
7382f_islocked(typval_T *argvars, typval_T *rettv)
7383{
7384 lval_T lv;
7385 char_u *end;
7386 dictitem_T *di;
7387
7388 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007389 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007390 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007391 if (end != NULL && lv.ll_name != NULL)
7392 {
7393 if (*end != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007394 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007395 else
7396 {
7397 if (lv.ll_tv == NULL)
7398 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007399 di = find_var(lv.ll_name, NULL, TRUE);
7400 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007401 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007402 /* Consider a variable locked when:
7403 * 1. the variable itself is locked
7404 * 2. the value of the variable is locked.
7405 * 3. the List or Dict value is locked.
7406 */
7407 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7408 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007409 }
7410 }
7411 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007412 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007413 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007414 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007415 else if (lv.ll_list != NULL)
7416 /* List item. */
7417 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7418 else
7419 /* Dictionary item. */
7420 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7421 }
7422 }
7423
7424 clear_lval(&lv);
7425}
7426
7427#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7428/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02007429 * "isinf()" function
7430 */
7431 static void
7432f_isinf(typval_T *argvars, typval_T *rettv)
7433{
7434 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
7435 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
7436}
7437
7438/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007439 * "isnan()" function
7440 */
7441 static void
7442f_isnan(typval_T *argvars, typval_T *rettv)
7443{
7444 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7445 && isnan(argvars[0].vval.v_float);
7446}
7447#endif
7448
7449/*
7450 * "items(dict)" function
7451 */
7452 static void
7453f_items(typval_T *argvars, typval_T *rettv)
7454{
7455 dict_list(argvars, rettv, 2);
7456}
7457
7458#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
7459/*
7460 * Get the job from the argument.
7461 * Returns NULL if the job is invalid.
7462 */
7463 static job_T *
7464get_job_arg(typval_T *tv)
7465{
7466 job_T *job;
7467
7468 if (tv->v_type != VAR_JOB)
7469 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007470 semsg(_(e_invarg2), tv_get_string(tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007471 return NULL;
7472 }
7473 job = tv->vval.v_job;
7474
7475 if (job == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007476 emsg(_("E916: not a valid job"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007477 return job;
7478}
7479
7480/*
7481 * "job_getchannel()" function
7482 */
7483 static void
7484f_job_getchannel(typval_T *argvars, typval_T *rettv)
7485{
7486 job_T *job = get_job_arg(&argvars[0]);
7487
7488 if (job != NULL)
7489 {
7490 rettv->v_type = VAR_CHANNEL;
7491 rettv->vval.v_channel = job->jv_channel;
7492 if (job->jv_channel != NULL)
7493 ++job->jv_channel->ch_refcount;
7494 }
7495}
7496
7497/*
7498 * "job_info()" function
7499 */
7500 static void
7501f_job_info(typval_T *argvars, typval_T *rettv)
7502{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007503 if (argvars[0].v_type != VAR_UNKNOWN)
7504 {
7505 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007506
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007507 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7508 job_info(job, rettv->vval.v_dict);
7509 }
7510 else if (rettv_list_alloc(rettv) == OK)
7511 job_info_all(rettv->vval.v_list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007512}
7513
7514/*
7515 * "job_setoptions()" function
7516 */
7517 static void
7518f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7519{
7520 job_T *job = get_job_arg(&argvars[0]);
7521 jobopt_T opt;
7522
7523 if (job == NULL)
7524 return;
7525 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007526 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007527 job_set_options(job, &opt);
7528 free_job_options(&opt);
7529}
7530
7531/*
7532 * "job_start()" function
7533 */
7534 static void
7535f_job_start(typval_T *argvars, typval_T *rettv)
7536{
7537 rettv->v_type = VAR_JOB;
7538 if (check_restricted() || check_secure())
7539 return;
Bram Moolenaar493359e2018-06-12 20:25:52 +02007540 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007541}
7542
7543/*
7544 * "job_status()" function
7545 */
7546 static void
7547f_job_status(typval_T *argvars, typval_T *rettv)
7548{
7549 job_T *job = get_job_arg(&argvars[0]);
7550
7551 if (job != NULL)
7552 {
7553 rettv->v_type = VAR_STRING;
7554 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7555 }
7556}
7557
7558/*
7559 * "job_stop()" function
7560 */
7561 static void
7562f_job_stop(typval_T *argvars, typval_T *rettv)
7563{
7564 job_T *job = get_job_arg(&argvars[0]);
7565
7566 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007567 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007568}
7569#endif
7570
7571/*
7572 * "join()" function
7573 */
7574 static void
7575f_join(typval_T *argvars, typval_T *rettv)
7576{
7577 garray_T ga;
7578 char_u *sep;
7579
7580 if (argvars[0].v_type != VAR_LIST)
7581 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007582 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007583 return;
7584 }
7585 if (argvars[0].vval.v_list == NULL)
7586 return;
7587 if (argvars[1].v_type == VAR_UNKNOWN)
7588 sep = (char_u *)" ";
7589 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007590 sep = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007591
7592 rettv->v_type = VAR_STRING;
7593
7594 if (sep != NULL)
7595 {
7596 ga_init2(&ga, (int)sizeof(char), 80);
7597 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7598 ga_append(&ga, NUL);
7599 rettv->vval.v_string = (char_u *)ga.ga_data;
7600 }
7601 else
7602 rettv->vval.v_string = NULL;
7603}
7604
7605/*
7606 * "js_decode()" function
7607 */
7608 static void
7609f_js_decode(typval_T *argvars, typval_T *rettv)
7610{
7611 js_read_T reader;
7612
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007613 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007614 reader.js_fill = NULL;
7615 reader.js_used = 0;
7616 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007617 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007618}
7619
7620/*
7621 * "js_encode()" function
7622 */
7623 static void
7624f_js_encode(typval_T *argvars, typval_T *rettv)
7625{
7626 rettv->v_type = VAR_STRING;
7627 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7628}
7629
7630/*
7631 * "json_decode()" function
7632 */
7633 static void
7634f_json_decode(typval_T *argvars, typval_T *rettv)
7635{
7636 js_read_T reader;
7637
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007638 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007639 reader.js_fill = NULL;
7640 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007641 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007642}
7643
7644/*
7645 * "json_encode()" function
7646 */
7647 static void
7648f_json_encode(typval_T *argvars, typval_T *rettv)
7649{
7650 rettv->v_type = VAR_STRING;
7651 rettv->vval.v_string = json_encode(&argvars[0], 0);
7652}
7653
7654/*
7655 * "keys()" function
7656 */
7657 static void
7658f_keys(typval_T *argvars, typval_T *rettv)
7659{
7660 dict_list(argvars, rettv, 0);
7661}
7662
7663/*
7664 * "last_buffer_nr()" function.
7665 */
7666 static void
7667f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7668{
7669 int n = 0;
7670 buf_T *buf;
7671
Bram Moolenaar29323592016-07-24 22:04:11 +02007672 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007673 if (n < buf->b_fnum)
7674 n = buf->b_fnum;
7675
7676 rettv->vval.v_number = n;
7677}
7678
7679/*
7680 * "len()" function
7681 */
7682 static void
7683f_len(typval_T *argvars, typval_T *rettv)
7684{
7685 switch (argvars[0].v_type)
7686 {
7687 case VAR_STRING:
7688 case VAR_NUMBER:
7689 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007690 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007691 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007692 case VAR_BLOB:
7693 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
7694 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007695 case VAR_LIST:
7696 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7697 break;
7698 case VAR_DICT:
7699 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7700 break;
7701 case VAR_UNKNOWN:
7702 case VAR_SPECIAL:
7703 case VAR_FLOAT:
7704 case VAR_FUNC:
7705 case VAR_PARTIAL:
7706 case VAR_JOB:
7707 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007708 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007709 break;
7710 }
7711}
7712
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007713 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007714libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007715{
7716#ifdef FEAT_LIBCALL
7717 char_u *string_in;
7718 char_u **string_result;
7719 int nr_result;
7720#endif
7721
7722 rettv->v_type = type;
7723 if (type != VAR_NUMBER)
7724 rettv->vval.v_string = NULL;
7725
7726 if (check_restricted() || check_secure())
7727 return;
7728
7729#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007730 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007731 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7732 {
7733 string_in = NULL;
7734 if (argvars[2].v_type == VAR_STRING)
7735 string_in = argvars[2].vval.v_string;
7736 if (type == VAR_NUMBER)
7737 string_result = NULL;
7738 else
7739 string_result = &rettv->vval.v_string;
7740 if (mch_libcall(argvars[0].vval.v_string,
7741 argvars[1].vval.v_string,
7742 string_in,
7743 argvars[2].vval.v_number,
7744 string_result,
7745 &nr_result) == OK
7746 && type == VAR_NUMBER)
7747 rettv->vval.v_number = nr_result;
7748 }
7749#endif
7750}
7751
7752/*
7753 * "libcall()" function
7754 */
7755 static void
7756f_libcall(typval_T *argvars, typval_T *rettv)
7757{
7758 libcall_common(argvars, rettv, VAR_STRING);
7759}
7760
7761/*
7762 * "libcallnr()" function
7763 */
7764 static void
7765f_libcallnr(typval_T *argvars, typval_T *rettv)
7766{
7767 libcall_common(argvars, rettv, VAR_NUMBER);
7768}
7769
7770/*
7771 * "line(string)" function
7772 */
7773 static void
7774f_line(typval_T *argvars, typval_T *rettv)
7775{
7776 linenr_T lnum = 0;
7777 pos_T *fp;
7778 int fnum;
7779
7780 fp = var2fpos(&argvars[0], TRUE, &fnum);
7781 if (fp != NULL)
7782 lnum = fp->lnum;
7783 rettv->vval.v_number = lnum;
7784}
7785
7786/*
7787 * "line2byte(lnum)" function
7788 */
7789 static void
7790f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7791{
7792#ifndef FEAT_BYTEOFF
7793 rettv->vval.v_number = -1;
7794#else
7795 linenr_T lnum;
7796
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007797 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007798 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7799 rettv->vval.v_number = -1;
7800 else
7801 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7802 if (rettv->vval.v_number >= 0)
7803 ++rettv->vval.v_number;
7804#endif
7805}
7806
7807/*
7808 * "lispindent(lnum)" function
7809 */
7810 static void
7811f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7812{
7813#ifdef FEAT_LISP
7814 pos_T pos;
7815 linenr_T lnum;
7816
7817 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007818 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007819 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7820 {
7821 curwin->w_cursor.lnum = lnum;
7822 rettv->vval.v_number = get_lisp_indent();
7823 curwin->w_cursor = pos;
7824 }
7825 else
7826#endif
7827 rettv->vval.v_number = -1;
7828}
7829
7830/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02007831 * "list2str()" function
7832 */
7833 static void
7834f_list2str(typval_T *argvars, typval_T *rettv)
7835{
7836 list_T *l;
7837 listitem_T *li;
7838 garray_T ga;
7839 int utf8 = FALSE;
7840
7841 rettv->v_type = VAR_STRING;
7842 rettv->vval.v_string = NULL;
7843 if (argvars[0].v_type != VAR_LIST)
7844 {
7845 emsg(_(e_invarg));
7846 return;
7847 }
7848
7849 l = argvars[0].vval.v_list;
7850 if (l == NULL)
7851 return; // empty list results in empty string
7852
7853 if (argvars[1].v_type != VAR_UNKNOWN)
7854 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
7855
7856 ga_init2(&ga, 1, 80);
7857 if (has_mbyte || utf8)
7858 {
7859 char_u buf[MB_MAXBYTES + 1];
7860 int (*char2bytes)(int, char_u *);
7861
7862 if (utf8 || enc_utf8)
7863 char2bytes = utf_char2bytes;
7864 else
7865 char2bytes = mb_char2bytes;
7866
7867 for (li = l->lv_first; li != NULL; li = li->li_next)
7868 {
7869 buf[(*char2bytes)(tv_get_number(&li->li_tv), buf)] = NUL;
7870 ga_concat(&ga, buf);
7871 }
7872 ga_append(&ga, NUL);
7873 }
7874 else if (ga_grow(&ga, list_len(l) + 1) == OK)
7875 {
7876 for (li = l->lv_first; li != NULL; li = li->li_next)
7877 ga_append(&ga, tv_get_number(&li->li_tv));
7878 ga_append(&ga, NUL);
7879 }
7880
7881 rettv->v_type = VAR_STRING;
7882 rettv->vval.v_string = ga.ga_data;
7883}
7884
7885/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007886 * "localtime()" function
7887 */
7888 static void
7889f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7890{
7891 rettv->vval.v_number = (varnumber_T)time(NULL);
7892}
7893
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007894 static void
7895get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7896{
7897 char_u *keys;
7898 char_u *which;
7899 char_u buf[NUMBUFLEN];
7900 char_u *keys_buf = NULL;
7901 char_u *rhs;
7902 int mode;
7903 int abbr = FALSE;
7904 int get_dict = FALSE;
7905 mapblock_T *mp;
7906 int buffer_local;
7907
7908 /* return empty string for failure */
7909 rettv->v_type = VAR_STRING;
7910 rettv->vval.v_string = NULL;
7911
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007912 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007913 if (*keys == NUL)
7914 return;
7915
7916 if (argvars[1].v_type != VAR_UNKNOWN)
7917 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007918 which = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007919 if (argvars[2].v_type != VAR_UNKNOWN)
7920 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007921 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007922 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007923 get_dict = (int)tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007924 }
7925 }
7926 else
7927 which = (char_u *)"";
7928 if (which == NULL)
7929 return;
7930
7931 mode = get_map_mode(&which, 0);
7932
7933 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7934 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7935 vim_free(keys_buf);
7936
7937 if (!get_dict)
7938 {
7939 /* Return a string. */
7940 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02007941 {
7942 if (*rhs == NUL)
7943 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
7944 else
7945 rettv->vval.v_string = str2special_save(rhs, FALSE);
7946 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007947
7948 }
7949 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7950 {
7951 /* Return a dictionary. */
7952 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7953 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7954 dict_T *dict = rettv->vval.v_dict;
7955
Bram Moolenaare0be1672018-07-08 16:50:37 +02007956 dict_add_string(dict, "lhs", lhs);
7957 dict_add_string(dict, "rhs", mp->m_orig_str);
7958 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
7959 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
7960 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007961 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
7962 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02007963 dict_add_number(dict, "buffer", (long)buffer_local);
7964 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
7965 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007966
7967 vim_free(lhs);
7968 vim_free(mapmode);
7969 }
7970}
7971
7972#ifdef FEAT_FLOAT
7973/*
7974 * "log()" function
7975 */
7976 static void
7977f_log(typval_T *argvars, typval_T *rettv)
7978{
7979 float_T f = 0.0;
7980
7981 rettv->v_type = VAR_FLOAT;
7982 if (get_float_arg(argvars, &f) == OK)
7983 rettv->vval.v_float = log(f);
7984 else
7985 rettv->vval.v_float = 0.0;
7986}
7987
7988/*
7989 * "log10()" function
7990 */
7991 static void
7992f_log10(typval_T *argvars, typval_T *rettv)
7993{
7994 float_T f = 0.0;
7995
7996 rettv->v_type = VAR_FLOAT;
7997 if (get_float_arg(argvars, &f) == OK)
7998 rettv->vval.v_float = log10(f);
7999 else
8000 rettv->vval.v_float = 0.0;
8001}
8002#endif
8003
8004#ifdef FEAT_LUA
8005/*
8006 * "luaeval()" function
8007 */
8008 static void
8009f_luaeval(typval_T *argvars, typval_T *rettv)
8010{
8011 char_u *str;
8012 char_u buf[NUMBUFLEN];
8013
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008014 if (check_restricted() || check_secure())
8015 return;
8016
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008017 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008018 do_luaeval(str, argvars + 1, rettv);
8019}
8020#endif
8021
8022/*
8023 * "map()" function
8024 */
8025 static void
8026f_map(typval_T *argvars, typval_T *rettv)
8027{
8028 filter_map(argvars, rettv, TRUE);
8029}
8030
8031/*
8032 * "maparg()" function
8033 */
8034 static void
8035f_maparg(typval_T *argvars, typval_T *rettv)
8036{
8037 get_maparg(argvars, rettv, TRUE);
8038}
8039
8040/*
8041 * "mapcheck()" function
8042 */
8043 static void
8044f_mapcheck(typval_T *argvars, typval_T *rettv)
8045{
8046 get_maparg(argvars, rettv, FALSE);
8047}
8048
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008049typedef enum
8050{
8051 MATCH_END, /* matchend() */
8052 MATCH_MATCH, /* match() */
8053 MATCH_STR, /* matchstr() */
8054 MATCH_LIST, /* matchlist() */
8055 MATCH_POS /* matchstrpos() */
8056} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008057
8058 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008059find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008060{
8061 char_u *str = NULL;
8062 long len = 0;
8063 char_u *expr = NULL;
8064 char_u *pat;
8065 regmatch_T regmatch;
8066 char_u patbuf[NUMBUFLEN];
8067 char_u strbuf[NUMBUFLEN];
8068 char_u *save_cpo;
8069 long start = 0;
8070 long nth = 1;
8071 colnr_T startcol = 0;
8072 int match = 0;
8073 list_T *l = NULL;
8074 listitem_T *li = NULL;
8075 long idx = 0;
8076 char_u *tofree = NULL;
8077
8078 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
8079 save_cpo = p_cpo;
8080 p_cpo = (char_u *)"";
8081
8082 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008083 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008084 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008085 /* type MATCH_LIST: return empty list when there are no matches.
8086 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008087 if (rettv_list_alloc(rettv) == FAIL)
8088 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008089 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008090 && (list_append_string(rettv->vval.v_list,
8091 (char_u *)"", 0) == FAIL
8092 || list_append_number(rettv->vval.v_list,
8093 (varnumber_T)-1) == FAIL
8094 || list_append_number(rettv->vval.v_list,
8095 (varnumber_T)-1) == FAIL
8096 || list_append_number(rettv->vval.v_list,
8097 (varnumber_T)-1) == FAIL))
8098 {
8099 list_free(rettv->vval.v_list);
8100 rettv->vval.v_list = NULL;
8101 goto theend;
8102 }
8103 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008104 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008105 {
8106 rettv->v_type = VAR_STRING;
8107 rettv->vval.v_string = NULL;
8108 }
8109
8110 if (argvars[0].v_type == VAR_LIST)
8111 {
8112 if ((l = argvars[0].vval.v_list) == NULL)
8113 goto theend;
8114 li = l->lv_first;
8115 }
8116 else
8117 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008118 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008119 len = (long)STRLEN(str);
8120 }
8121
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008122 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008123 if (pat == NULL)
8124 goto theend;
8125
8126 if (argvars[2].v_type != VAR_UNKNOWN)
8127 {
8128 int error = FALSE;
8129
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008130 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008131 if (error)
8132 goto theend;
8133 if (l != NULL)
8134 {
8135 li = list_find(l, start);
8136 if (li == NULL)
8137 goto theend;
8138 idx = l->lv_idx; /* use the cached index */
8139 }
8140 else
8141 {
8142 if (start < 0)
8143 start = 0;
8144 if (start > len)
8145 goto theend;
8146 /* When "count" argument is there ignore matches before "start",
8147 * otherwise skip part of the string. Differs when pattern is "^"
8148 * or "\<". */
8149 if (argvars[3].v_type != VAR_UNKNOWN)
8150 startcol = start;
8151 else
8152 {
8153 str += start;
8154 len -= start;
8155 }
8156 }
8157
8158 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008159 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008160 if (error)
8161 goto theend;
8162 }
8163
8164 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8165 if (regmatch.regprog != NULL)
8166 {
8167 regmatch.rm_ic = p_ic;
8168
8169 for (;;)
8170 {
8171 if (l != NULL)
8172 {
8173 if (li == NULL)
8174 {
8175 match = FALSE;
8176 break;
8177 }
8178 vim_free(tofree);
8179 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
8180 if (str == NULL)
8181 break;
8182 }
8183
8184 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
8185
8186 if (match && --nth <= 0)
8187 break;
8188 if (l == NULL && !match)
8189 break;
8190
8191 /* Advance to just after the match. */
8192 if (l != NULL)
8193 {
8194 li = li->li_next;
8195 ++idx;
8196 }
8197 else
8198 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008199 startcol = (colnr_T)(regmatch.startp[0]
8200 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008201 if (startcol > (colnr_T)len
8202 || str + startcol <= regmatch.startp[0])
8203 {
8204 match = FALSE;
8205 break;
8206 }
8207 }
8208 }
8209
8210 if (match)
8211 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008212 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008213 {
8214 listitem_T *li1 = rettv->vval.v_list->lv_first;
8215 listitem_T *li2 = li1->li_next;
8216 listitem_T *li3 = li2->li_next;
8217 listitem_T *li4 = li3->li_next;
8218
8219 vim_free(li1->li_tv.vval.v_string);
8220 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
8221 (int)(regmatch.endp[0] - regmatch.startp[0]));
8222 li3->li_tv.vval.v_number =
8223 (varnumber_T)(regmatch.startp[0] - expr);
8224 li4->li_tv.vval.v_number =
8225 (varnumber_T)(regmatch.endp[0] - expr);
8226 if (l != NULL)
8227 li2->li_tv.vval.v_number = (varnumber_T)idx;
8228 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008229 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008230 {
8231 int i;
8232
8233 /* return list with matched string and submatches */
8234 for (i = 0; i < NSUBEXP; ++i)
8235 {
8236 if (regmatch.endp[i] == NULL)
8237 {
8238 if (list_append_string(rettv->vval.v_list,
8239 (char_u *)"", 0) == FAIL)
8240 break;
8241 }
8242 else if (list_append_string(rettv->vval.v_list,
8243 regmatch.startp[i],
8244 (int)(regmatch.endp[i] - regmatch.startp[i]))
8245 == FAIL)
8246 break;
8247 }
8248 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008249 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008250 {
8251 /* return matched string */
8252 if (l != NULL)
8253 copy_tv(&li->li_tv, rettv);
8254 else
8255 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
8256 (int)(regmatch.endp[0] - regmatch.startp[0]));
8257 }
8258 else if (l != NULL)
8259 rettv->vval.v_number = idx;
8260 else
8261 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008262 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008263 rettv->vval.v_number =
8264 (varnumber_T)(regmatch.startp[0] - str);
8265 else
8266 rettv->vval.v_number =
8267 (varnumber_T)(regmatch.endp[0] - str);
8268 rettv->vval.v_number += (varnumber_T)(str - expr);
8269 }
8270 }
8271 vim_regfree(regmatch.regprog);
8272 }
8273
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008274theend:
8275 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008276 /* matchstrpos() without a list: drop the second item. */
8277 listitem_remove(rettv->vval.v_list,
8278 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008279 vim_free(tofree);
8280 p_cpo = save_cpo;
8281}
8282
8283/*
8284 * "match()" function
8285 */
8286 static void
8287f_match(typval_T *argvars, typval_T *rettv)
8288{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008289 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008290}
8291
Bram Moolenaar95e51472018-07-28 16:55:56 +02008292#ifdef FEAT_SEARCH_EXTRA
8293 static int
8294matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
8295{
8296 dictitem_T *di;
8297
8298 if (tv->v_type != VAR_DICT)
8299 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008300 emsg(_(e_dictreq));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008301 return FAIL;
8302 }
8303
8304 if (dict_find(tv->vval.v_dict, (char_u *)"conceal", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01008305 *conceal_char = dict_get_string(tv->vval.v_dict,
Bram Moolenaar95e51472018-07-28 16:55:56 +02008306 (char_u *)"conceal", FALSE);
8307
8308 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
8309 {
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02008310 *win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008311 if (*win == NULL)
8312 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01008313 emsg(_(e_invalwindow));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008314 return FAIL;
8315 }
8316 }
8317
8318 return OK;
8319}
8320#endif
8321
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008322/*
8323 * "matchadd()" function
8324 */
8325 static void
8326f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8327{
8328#ifdef FEAT_SEARCH_EXTRA
8329 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008330 char_u *grp = tv_get_string_buf_chk(&argvars[0], buf); /* group */
8331 char_u *pat = tv_get_string_buf_chk(&argvars[1], buf); /* pattern */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008332 int prio = 10; /* default priority */
8333 int id = -1;
8334 int error = FALSE;
8335 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008336 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008337
8338 rettv->vval.v_number = -1;
8339
8340 if (grp == NULL || pat == NULL)
8341 return;
8342 if (argvars[2].v_type != VAR_UNKNOWN)
8343 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008344 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008345 if (argvars[3].v_type != VAR_UNKNOWN)
8346 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008347 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008348 if (argvars[4].v_type != VAR_UNKNOWN
8349 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8350 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008351 }
8352 }
8353 if (error == TRUE)
8354 return;
8355 if (id >= 1 && id <= 3)
8356 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008357 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008358 return;
8359 }
8360
Bram Moolenaar95e51472018-07-28 16:55:56 +02008361 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008362 conceal_char);
8363#endif
8364}
8365
8366/*
8367 * "matchaddpos()" function
8368 */
8369 static void
8370f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8371{
8372#ifdef FEAT_SEARCH_EXTRA
8373 char_u buf[NUMBUFLEN];
8374 char_u *group;
8375 int prio = 10;
8376 int id = -1;
8377 int error = FALSE;
8378 list_T *l;
8379 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008380 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008381
8382 rettv->vval.v_number = -1;
8383
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008384 group = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008385 if (group == NULL)
8386 return;
8387
8388 if (argvars[1].v_type != VAR_LIST)
8389 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008390 semsg(_(e_listarg), "matchaddpos()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008391 return;
8392 }
8393 l = argvars[1].vval.v_list;
8394 if (l == NULL)
8395 return;
8396
8397 if (argvars[2].v_type != VAR_UNKNOWN)
8398 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008399 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008400 if (argvars[3].v_type != VAR_UNKNOWN)
8401 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008402 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008403
8404 if (argvars[4].v_type != VAR_UNKNOWN
8405 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8406 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008407 }
8408 }
8409 if (error == TRUE)
8410 return;
8411
8412 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8413 if (id == 1 || id == 2)
8414 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008415 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008416 return;
8417 }
8418
Bram Moolenaar95e51472018-07-28 16:55:56 +02008419 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008420 conceal_char);
8421#endif
8422}
8423
8424/*
8425 * "matcharg()" function
8426 */
8427 static void
8428f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8429{
8430 if (rettv_list_alloc(rettv) == OK)
8431 {
8432#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008433 int id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008434 matchitem_T *m;
8435
8436 if (id >= 1 && id <= 3)
8437 {
8438 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8439 {
8440 list_append_string(rettv->vval.v_list,
8441 syn_id2name(m->hlg_id), -1);
8442 list_append_string(rettv->vval.v_list, m->pattern, -1);
8443 }
8444 else
8445 {
8446 list_append_string(rettv->vval.v_list, NULL, -1);
8447 list_append_string(rettv->vval.v_list, NULL, -1);
8448 }
8449 }
8450#endif
8451 }
8452}
8453
8454/*
8455 * "matchdelete()" function
8456 */
8457 static void
8458f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8459{
8460#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01008461 win_T *win = get_optional_window(argvars, 1);
8462
8463 if (win == NULL)
8464 rettv->vval.v_number = -1;
8465 else
8466 rettv->vval.v_number = match_delete(win,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008467 (int)tv_get_number(&argvars[0]), TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008468#endif
8469}
8470
8471/*
8472 * "matchend()" function
8473 */
8474 static void
8475f_matchend(typval_T *argvars, typval_T *rettv)
8476{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008477 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008478}
8479
8480/*
8481 * "matchlist()" function
8482 */
8483 static void
8484f_matchlist(typval_T *argvars, typval_T *rettv)
8485{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008486 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008487}
8488
8489/*
8490 * "matchstr()" function
8491 */
8492 static void
8493f_matchstr(typval_T *argvars, typval_T *rettv)
8494{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008495 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008496}
8497
8498/*
8499 * "matchstrpos()" function
8500 */
8501 static void
8502f_matchstrpos(typval_T *argvars, typval_T *rettv)
8503{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008504 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008505}
8506
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008507 static void
8508max_min(typval_T *argvars, typval_T *rettv, int domax)
8509{
8510 varnumber_T n = 0;
8511 varnumber_T i;
8512 int error = FALSE;
8513
8514 if (argvars[0].v_type == VAR_LIST)
8515 {
8516 list_T *l;
8517 listitem_T *li;
8518
8519 l = argvars[0].vval.v_list;
8520 if (l != NULL)
8521 {
8522 li = l->lv_first;
8523 if (li != NULL)
8524 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008525 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008526 for (;;)
8527 {
8528 li = li->li_next;
8529 if (li == NULL)
8530 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008531 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008532 if (domax ? i > n : i < n)
8533 n = i;
8534 }
8535 }
8536 }
8537 }
8538 else if (argvars[0].v_type == VAR_DICT)
8539 {
8540 dict_T *d;
8541 int first = TRUE;
8542 hashitem_T *hi;
8543 int todo;
8544
8545 d = argvars[0].vval.v_dict;
8546 if (d != NULL)
8547 {
8548 todo = (int)d->dv_hashtab.ht_used;
8549 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8550 {
8551 if (!HASHITEM_EMPTY(hi))
8552 {
8553 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008554 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008555 if (first)
8556 {
8557 n = i;
8558 first = FALSE;
8559 }
8560 else if (domax ? i > n : i < n)
8561 n = i;
8562 }
8563 }
8564 }
8565 }
8566 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008567 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008568 rettv->vval.v_number = error ? 0 : n;
8569}
8570
8571/*
8572 * "max()" function
8573 */
8574 static void
8575f_max(typval_T *argvars, typval_T *rettv)
8576{
8577 max_min(argvars, rettv, TRUE);
8578}
8579
8580/*
8581 * "min()" function
8582 */
8583 static void
8584f_min(typval_T *argvars, typval_T *rettv)
8585{
8586 max_min(argvars, rettv, FALSE);
8587}
8588
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008589/*
8590 * Create the directory in which "dir" is located, and higher levels when
8591 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008592 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008593 */
8594 static int
8595mkdir_recurse(char_u *dir, int prot)
8596{
8597 char_u *p;
8598 char_u *updir;
8599 int r = FAIL;
8600
8601 /* Get end of directory name in "dir".
8602 * We're done when it's "/" or "c:/". */
8603 p = gettail_sep(dir);
8604 if (p <= get_past_head(dir))
8605 return OK;
8606
8607 /* If the directory exists we're done. Otherwise: create it.*/
8608 updir = vim_strnsave(dir, (int)(p - dir));
8609 if (updir == NULL)
8610 return FAIL;
8611 if (mch_isdir(updir))
8612 r = OK;
8613 else if (mkdir_recurse(updir, prot) == OK)
8614 r = vim_mkdir_emsg(updir, prot);
8615 vim_free(updir);
8616 return r;
8617}
8618
8619#ifdef vim_mkdir
8620/*
8621 * "mkdir()" function
8622 */
8623 static void
8624f_mkdir(typval_T *argvars, typval_T *rettv)
8625{
8626 char_u *dir;
8627 char_u buf[NUMBUFLEN];
8628 int prot = 0755;
8629
8630 rettv->vval.v_number = FAIL;
8631 if (check_restricted() || check_secure())
8632 return;
8633
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008634 dir = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008635 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008636 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008637
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008638 if (*gettail(dir) == NUL)
8639 /* remove trailing slashes */
8640 *gettail_sep(dir) = NUL;
8641
8642 if (argvars[1].v_type != VAR_UNKNOWN)
8643 {
8644 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008645 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008646 prot = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008647 if (prot == -1)
8648 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008649 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008650 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008651 {
8652 if (mch_isdir(dir))
8653 {
8654 /* With the "p" flag it's OK if the dir already exists. */
8655 rettv->vval.v_number = OK;
8656 return;
8657 }
8658 mkdir_recurse(dir, prot);
8659 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008660 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008661 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008662}
8663#endif
8664
8665/*
8666 * "mode()" function
8667 */
8668 static void
8669f_mode(typval_T *argvars, typval_T *rettv)
8670{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008671 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008672
Bram Moolenaar612cc382018-07-29 15:34:26 +02008673 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008674
8675 if (time_for_testing == 93784)
8676 {
8677 /* Testing the two-character code. */
8678 buf[0] = 'x';
8679 buf[1] = '!';
8680 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008681#ifdef FEAT_TERMINAL
8682 else if (term_use_loop())
8683 buf[0] = 't';
8684#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008685 else if (VIsual_active)
8686 {
8687 if (VIsual_select)
8688 buf[0] = VIsual_mode + 's' - 'v';
8689 else
8690 buf[0] = VIsual_mode;
8691 }
8692 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8693 || State == CONFIRM)
8694 {
8695 buf[0] = 'r';
8696 if (State == ASKMORE)
8697 buf[1] = 'm';
8698 else if (State == CONFIRM)
8699 buf[1] = '?';
8700 }
8701 else if (State == EXTERNCMD)
8702 buf[0] = '!';
8703 else if (State & INSERT)
8704 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008705 if (State & VREPLACE_FLAG)
8706 {
8707 buf[0] = 'R';
8708 buf[1] = 'v';
8709 }
8710 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008711 {
8712 if (State & REPLACE_FLAG)
8713 buf[0] = 'R';
8714 else
8715 buf[0] = 'i';
8716#ifdef FEAT_INS_EXPAND
8717 if (ins_compl_active())
8718 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008719 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008720 buf[1] = 'x';
8721#endif
8722 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008723 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008724 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008725 {
8726 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008727 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008728 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008729 else if (exmode_active == EXMODE_NORMAL)
8730 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008731 }
8732 else
8733 {
8734 buf[0] = 'n';
8735 if (finish_op)
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008736 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008737 buf[1] = 'o';
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008738 // to be able to detect force-linewise/blockwise/characterwise operations
8739 buf[2] = motion_force;
8740 }
Bram Moolenaar612cc382018-07-29 15:34:26 +02008741 else if (restart_edit == 'I' || restart_edit == 'R'
8742 || restart_edit == 'V')
8743 {
8744 buf[1] = 'i';
8745 buf[2] = restart_edit;
8746 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008747 }
8748
8749 /* Clear out the minor mode when the argument is not a non-zero number or
8750 * non-empty string. */
8751 if (!non_zero_arg(&argvars[0]))
8752 buf[1] = NUL;
8753
8754 rettv->vval.v_string = vim_strsave(buf);
8755 rettv->v_type = VAR_STRING;
8756}
8757
8758#if defined(FEAT_MZSCHEME) || defined(PROTO)
8759/*
8760 * "mzeval()" function
8761 */
8762 static void
8763f_mzeval(typval_T *argvars, typval_T *rettv)
8764{
8765 char_u *str;
8766 char_u buf[NUMBUFLEN];
8767
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008768 if (check_restricted() || check_secure())
8769 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008770 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008771 do_mzeval(str, rettv);
8772}
8773
8774 void
8775mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8776{
8777 typval_T argvars[3];
8778
8779 argvars[0].v_type = VAR_STRING;
8780 argvars[0].vval.v_string = name;
8781 copy_tv(args, &argvars[1]);
8782 argvars[2].v_type = VAR_UNKNOWN;
8783 f_call(argvars, rettv);
8784 clear_tv(&argvars[1]);
8785}
8786#endif
8787
8788/*
8789 * "nextnonblank()" function
8790 */
8791 static void
8792f_nextnonblank(typval_T *argvars, typval_T *rettv)
8793{
8794 linenr_T lnum;
8795
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008796 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008797 {
8798 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8799 {
8800 lnum = 0;
8801 break;
8802 }
8803 if (*skipwhite(ml_get(lnum)) != NUL)
8804 break;
8805 }
8806 rettv->vval.v_number = lnum;
8807}
8808
8809/*
8810 * "nr2char()" function
8811 */
8812 static void
8813f_nr2char(typval_T *argvars, typval_T *rettv)
8814{
8815 char_u buf[NUMBUFLEN];
8816
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008817 if (has_mbyte)
8818 {
8819 int utf8 = 0;
8820
8821 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008822 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008823 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01008824 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008825 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008826 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008827 }
8828 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008829 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008830 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008831 buf[1] = NUL;
8832 }
8833 rettv->v_type = VAR_STRING;
8834 rettv->vval.v_string = vim_strsave(buf);
8835}
8836
8837/*
8838 * "or(expr, expr)" function
8839 */
8840 static void
8841f_or(typval_T *argvars, typval_T *rettv)
8842{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008843 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
8844 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008845}
8846
8847/*
8848 * "pathshorten()" function
8849 */
8850 static void
8851f_pathshorten(typval_T *argvars, typval_T *rettv)
8852{
8853 char_u *p;
8854
8855 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008856 p = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008857 if (p == NULL)
8858 rettv->vval.v_string = NULL;
8859 else
8860 {
8861 p = vim_strsave(p);
8862 rettv->vval.v_string = p;
8863 if (p != NULL)
8864 shorten_dir(p);
8865 }
8866}
8867
8868#ifdef FEAT_PERL
8869/*
8870 * "perleval()" function
8871 */
8872 static void
8873f_perleval(typval_T *argvars, typval_T *rettv)
8874{
8875 char_u *str;
8876 char_u buf[NUMBUFLEN];
8877
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008878 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008879 do_perleval(str, rettv);
8880}
8881#endif
8882
8883#ifdef FEAT_FLOAT
8884/*
8885 * "pow()" function
8886 */
8887 static void
8888f_pow(typval_T *argvars, typval_T *rettv)
8889{
8890 float_T fx = 0.0, fy = 0.0;
8891
8892 rettv->v_type = VAR_FLOAT;
8893 if (get_float_arg(argvars, &fx) == OK
8894 && get_float_arg(&argvars[1], &fy) == OK)
8895 rettv->vval.v_float = pow(fx, fy);
8896 else
8897 rettv->vval.v_float = 0.0;
8898}
8899#endif
8900
8901/*
8902 * "prevnonblank()" function
8903 */
8904 static void
8905f_prevnonblank(typval_T *argvars, typval_T *rettv)
8906{
8907 linenr_T lnum;
8908
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008909 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008910 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8911 lnum = 0;
8912 else
8913 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8914 --lnum;
8915 rettv->vval.v_number = lnum;
8916}
8917
8918/* This dummy va_list is here because:
8919 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8920 * - locally in the function results in a "used before set" warning
8921 * - using va_start() to initialize it gives "function with fixed args" error */
8922static va_list ap;
8923
8924/*
8925 * "printf()" function
8926 */
8927 static void
8928f_printf(typval_T *argvars, typval_T *rettv)
8929{
8930 char_u buf[NUMBUFLEN];
8931 int len;
8932 char_u *s;
8933 int saved_did_emsg = did_emsg;
8934 char *fmt;
8935
8936 rettv->v_type = VAR_STRING;
8937 rettv->vval.v_string = NULL;
8938
8939 /* Get the required length, allocate the buffer and do it for real. */
8940 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008941 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008942 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008943 if (!did_emsg)
8944 {
8945 s = alloc(len + 1);
8946 if (s != NULL)
8947 {
8948 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008949 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8950 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008951 }
8952 }
8953 did_emsg |= saved_did_emsg;
8954}
8955
Bram Moolenaarf2732452018-06-03 14:47:35 +02008956#ifdef FEAT_JOB_CHANNEL
8957/*
8958 * "prompt_setcallback({buffer}, {callback})" function
8959 */
8960 static void
8961f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
8962{
8963 buf_T *buf;
8964 char_u *callback;
8965 partial_T *partial;
8966
8967 if (check_secure())
8968 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01008969 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008970 if (buf == NULL)
8971 return;
8972
8973 callback = get_callback(&argvars[1], &partial);
8974 if (callback == NULL)
8975 return;
8976
8977 free_callback(buf->b_prompt_callback, buf->b_prompt_partial);
8978 if (partial == NULL)
8979 buf->b_prompt_callback = vim_strsave(callback);
8980 else
8981 /* pointer into the partial */
8982 buf->b_prompt_callback = callback;
8983 buf->b_prompt_partial = partial;
8984}
8985
8986/*
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02008987 * "prompt_setinterrupt({buffer}, {callback})" function
8988 */
8989 static void
8990f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
8991{
8992 buf_T *buf;
8993 char_u *callback;
8994 partial_T *partial;
8995
8996 if (check_secure())
8997 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01008998 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02008999 if (buf == NULL)
9000 return;
9001
9002 callback = get_callback(&argvars[1], &partial);
9003 if (callback == NULL)
9004 return;
9005
9006 free_callback(buf->b_prompt_interrupt, buf->b_prompt_int_partial);
9007 if (partial == NULL)
9008 buf->b_prompt_interrupt = vim_strsave(callback);
9009 else
9010 /* pointer into the partial */
9011 buf->b_prompt_interrupt = callback;
9012 buf->b_prompt_int_partial = partial;
9013}
9014
9015/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02009016 * "prompt_setprompt({buffer}, {text})" function
9017 */
9018 static void
9019f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
9020{
9021 buf_T *buf;
9022 char_u *text;
9023
9024 if (check_secure())
9025 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009026 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009027 if (buf == NULL)
9028 return;
9029
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009030 text = tv_get_string(&argvars[1]);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009031 vim_free(buf->b_prompt_text);
9032 buf->b_prompt_text = vim_strsave(text);
9033}
9034#endif
9035
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009036/*
9037 * "pumvisible()" function
9038 */
9039 static void
9040f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9041{
9042#ifdef FEAT_INS_EXPAND
9043 if (pum_visible())
9044 rettv->vval.v_number = 1;
9045#endif
9046}
9047
9048#ifdef FEAT_PYTHON3
9049/*
9050 * "py3eval()" function
9051 */
9052 static void
9053f_py3eval(typval_T *argvars, typval_T *rettv)
9054{
9055 char_u *str;
9056 char_u buf[NUMBUFLEN];
9057
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009058 if (check_restricted() || check_secure())
9059 return;
9060
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009061 if (p_pyx == 0)
9062 p_pyx = 3;
9063
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009064 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009065 do_py3eval(str, rettv);
9066}
9067#endif
9068
9069#ifdef FEAT_PYTHON
9070/*
9071 * "pyeval()" function
9072 */
9073 static void
9074f_pyeval(typval_T *argvars, typval_T *rettv)
9075{
9076 char_u *str;
9077 char_u buf[NUMBUFLEN];
9078
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009079 if (check_restricted() || check_secure())
9080 return;
9081
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009082 if (p_pyx == 0)
9083 p_pyx = 2;
9084
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009085 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009086 do_pyeval(str, rettv);
9087}
9088#endif
9089
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009090#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
9091/*
9092 * "pyxeval()" function
9093 */
9094 static void
9095f_pyxeval(typval_T *argvars, typval_T *rettv)
9096{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009097 if (check_restricted() || check_secure())
9098 return;
9099
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009100# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
9101 init_pyxversion();
9102 if (p_pyx == 2)
9103 f_pyeval(argvars, rettv);
9104 else
9105 f_py3eval(argvars, rettv);
9106# elif defined(FEAT_PYTHON)
9107 f_pyeval(argvars, rettv);
9108# elif defined(FEAT_PYTHON3)
9109 f_py3eval(argvars, rettv);
9110# endif
9111}
9112#endif
9113
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009114/*
9115 * "range()" function
9116 */
9117 static void
9118f_range(typval_T *argvars, typval_T *rettv)
9119{
9120 varnumber_T start;
9121 varnumber_T end;
9122 varnumber_T stride = 1;
9123 varnumber_T i;
9124 int error = FALSE;
9125
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009126 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009127 if (argvars[1].v_type == VAR_UNKNOWN)
9128 {
9129 end = start - 1;
9130 start = 0;
9131 }
9132 else
9133 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009134 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009135 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009136 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009137 }
9138
9139 if (error)
9140 return; /* type error; errmsg already given */
9141 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009142 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009143 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009144 emsg(_("E727: Start past end"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009145 else
9146 {
9147 if (rettv_list_alloc(rettv) == OK)
9148 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
9149 if (list_append_number(rettv->vval.v_list,
9150 (varnumber_T)i) == FAIL)
9151 break;
9152 }
9153}
9154
9155/*
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009156 * Evaluate "expr" for readdir().
9157 */
9158 static int
9159readdir_checkitem(typval_T *expr, char_u *name)
9160{
9161 typval_T save_val;
9162 typval_T rettv;
9163 typval_T argv[2];
9164 int retval = 0;
9165 int error = FALSE;
9166
9167 prepare_vimvar(VV_VAL, &save_val);
9168 set_vim_var_string(VV_VAL, name, -1);
9169 argv[0].v_type = VAR_STRING;
9170 argv[0].vval.v_string = name;
9171
9172 if (eval_expr_typval(expr, argv, 1, &rettv) == FAIL)
9173 goto theend;
9174
9175 retval = tv_get_number_chk(&rettv, &error);
9176 if (error)
9177 retval = -1;
9178 clear_tv(&rettv);
9179
9180theend:
9181 set_vim_var_string(VV_VAL, NULL, 0);
9182 restore_vimvar(VV_VAL, &save_val);
9183 return retval;
9184}
9185
9186/*
9187 * "readdir()" function
9188 */
9189 static void
9190f_readdir(typval_T *argvars, typval_T *rettv)
9191{
9192 typval_T *expr;
9193 int failed = FALSE;
9194 char_u *path;
9195 garray_T ga;
9196 int i;
9197#ifdef MSWIN
9198 char_u *buf, *p;
9199 WIN32_FIND_DATA fb;
9200 int ok;
9201 HANDLE hFind = INVALID_HANDLE_VALUE;
9202 WIN32_FIND_DATAW wfb;
9203 WCHAR *wn = NULL; // UCS-2 name, NULL when not used.
9204#endif
9205
9206 if (rettv_list_alloc(rettv) == FAIL)
9207 return;
9208 path = tv_get_string(&argvars[0]);
9209 expr = &argvars[1];
9210 ga_init2(&ga, (int)sizeof(char *), 20);
9211
9212#ifdef MSWIN
9213 buf = alloc((int)MAXPATHL);
9214 if (buf == NULL)
9215 return;
9216 STRNCPY(buf, path, MAXPATHL-5);
9217 p = vim_strpbrk(path, (char_u *)"\\/");
9218 if (p != NULL)
9219 *p = NUL;
9220 STRCAT(buf, "\\*");
9221
9222 wn = enc_to_utf16(buf, NULL);
9223 if (wn != NULL)
9224 hFind = FindFirstFileW(wn, &wfb);
9225 ok = (hFind != INVALID_HANDLE_VALUE);
9226 if (!ok)
9227 smsg(_(e_notopen), path);
9228 else
9229 {
9230 while (ok)
9231 {
9232 int ignore;
9233
9234 p = utf16_to_enc(wfb.cFileName, NULL); // p is allocated here
9235 if (p == NULL)
9236 break; // out of memory
9237
9238 ignore = p[0] == '.' && (p[1] == NUL
9239 || (p[1] == '.' && p[2] == NUL));
9240 if (!ignore && expr->v_type != VAR_UNKNOWN)
9241 {
9242 int r = readdir_checkitem(expr, p);
9243
9244 if (r < 0)
9245 {
9246 vim_free(p);
9247 break;
9248 }
9249 if (r == 0)
9250 ignore = TRUE;
9251 }
9252
9253 if (!ignore)
9254 {
9255 if (ga_grow(&ga, 1) == OK)
9256 ((char_u**)ga.ga_data)[ga.ga_len++] = vim_strsave(p);
9257 else
9258 {
9259 failed = TRUE;
9260 vim_free(p);
9261 break;
9262 }
9263 }
9264
9265 vim_free(p);
9266 ok = FindNextFileW(hFind, &wfb);
9267 }
9268 FindClose(hFind);
9269 }
9270
9271 vim_free(buf);
9272 vim_free(wn);
9273#else
9274 DIR *dirp;
9275 struct dirent *dp;
9276 char_u *p;
9277
9278 dirp = opendir((char *)path);
9279 if (dirp == NULL)
9280 smsg(_(e_notopen), path);
9281 else
9282 {
9283 for (;;)
9284 {
9285 int ignore;
9286
9287 dp = readdir(dirp);
9288 if (dp == NULL)
9289 break;
9290 p = (char_u *)dp->d_name;
9291
9292 ignore = p[0] == '.' &&
9293 (p[1] == NUL ||
9294 (p[1] == '.' && p[2] == NUL));
9295 if (!ignore && expr->v_type != VAR_UNKNOWN)
9296 {
9297 int r = readdir_checkitem(expr, p);
9298
9299 if (r < 0)
9300 break;
9301 if (r == 0)
9302 ignore = TRUE;
9303 }
9304
9305 if (!ignore)
9306 {
9307 if (ga_grow(&ga, 1) == OK)
9308 ((char_u**)ga.ga_data)[ga.ga_len++] = vim_strsave(p);
9309 else
9310 {
9311 failed = TRUE;
9312 break;
9313 }
9314 }
9315 }
9316
9317 closedir(dirp);
9318 }
9319#endif
9320
9321 rettv->vval.v_list = list_alloc();
9322 if (!failed && rettv->vval.v_list != NULL)
9323 {
9324 ++rettv->vval.v_list->lv_refcount;
9325 sort_strings((char_u **)ga.ga_data, ga.ga_len);
9326 for (i = 0; i < ga.ga_len; i++)
9327 {
9328 p = ((char_u **)ga.ga_data)[i];
9329 list_append_string(rettv->vval.v_list, p, -1);
9330 }
9331 }
9332 for (i = 0; i < ga.ga_len; i++)
9333 vim_free(((char_u **)ga.ga_data)[i]);
9334
9335 ga_clear(&ga);
9336}
9337
9338/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009339 * "readfile()" function
9340 */
9341 static void
9342f_readfile(typval_T *argvars, typval_T *rettv)
9343{
9344 int binary = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009345 int blob = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009346 int failed = FALSE;
9347 char_u *fname;
9348 FILE *fd;
9349 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
9350 int io_size = sizeof(buf);
9351 int readlen; /* size of last fread() */
9352 char_u *prev = NULL; /* previously read bytes, if any */
9353 long prevlen = 0; /* length of data in prev */
9354 long prevsize = 0; /* size of prev buffer */
9355 long maxline = MAXLNUM;
9356 long cnt = 0;
9357 char_u *p; /* position in buf */
9358 char_u *start; /* start of current line */
9359
9360 if (argvars[1].v_type != VAR_UNKNOWN)
9361 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009362 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009363 binary = TRUE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009364 if (STRCMP(tv_get_string(&argvars[1]), "B") == 0)
9365 blob = TRUE;
9366
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009367 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009368 maxline = (long)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009369 }
9370
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009371 if (blob)
9372 {
9373 if (rettv_blob_alloc(rettv) == FAIL)
9374 return;
9375 }
9376 else
9377 {
9378 if (rettv_list_alloc(rettv) == FAIL)
9379 return;
9380 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009381
9382 /* Always open the file in binary mode, library functions have a mind of
9383 * their own about CR-LF conversion. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009384 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009385 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
9386 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009387 semsg(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009388 return;
9389 }
9390
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009391 if (blob)
9392 {
9393 if (read_blob(fd, rettv->vval.v_blob) == FAIL)
9394 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009395 emsg("cannot read file");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009396 blob_free(rettv->vval.v_blob);
9397 }
9398 fclose(fd);
9399 return;
9400 }
9401
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009402 while (cnt < maxline || maxline < 0)
9403 {
9404 readlen = (int)fread(buf, 1, io_size, fd);
9405
9406 /* This for loop processes what was read, but is also entered at end
9407 * of file so that either:
9408 * - an incomplete line gets written
9409 * - a "binary" file gets an empty line at the end if it ends in a
9410 * newline. */
9411 for (p = buf, start = buf;
9412 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
9413 ++p)
9414 {
9415 if (*p == '\n' || readlen <= 0)
9416 {
9417 listitem_T *li;
9418 char_u *s = NULL;
9419 long_u len = p - start;
9420
9421 /* Finished a line. Remove CRs before NL. */
9422 if (readlen > 0 && !binary)
9423 {
9424 while (len > 0 && start[len - 1] == '\r')
9425 --len;
9426 /* removal may cross back to the "prev" string */
9427 if (len == 0)
9428 while (prevlen > 0 && prev[prevlen - 1] == '\r')
9429 --prevlen;
9430 }
9431 if (prevlen == 0)
9432 s = vim_strnsave(start, (int)len);
9433 else
9434 {
9435 /* Change "prev" buffer to be the right size. This way
9436 * the bytes are only copied once, and very long lines are
9437 * allocated only once. */
9438 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
9439 {
9440 mch_memmove(s + prevlen, start, len);
9441 s[prevlen + len] = NUL;
9442 prev = NULL; /* the list will own the string */
9443 prevlen = prevsize = 0;
9444 }
9445 }
9446 if (s == NULL)
9447 {
9448 do_outofmem_msg((long_u) prevlen + len + 1);
9449 failed = TRUE;
9450 break;
9451 }
9452
9453 if ((li = listitem_alloc()) == NULL)
9454 {
9455 vim_free(s);
9456 failed = TRUE;
9457 break;
9458 }
9459 li->li_tv.v_type = VAR_STRING;
9460 li->li_tv.v_lock = 0;
9461 li->li_tv.vval.v_string = s;
9462 list_append(rettv->vval.v_list, li);
9463
9464 start = p + 1; /* step over newline */
9465 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
9466 break;
9467 }
9468 else if (*p == NUL)
9469 *p = '\n';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009470 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
9471 * when finding the BF and check the previous two bytes. */
9472 else if (*p == 0xbf && enc_utf8 && !binary)
9473 {
9474 /* Find the two bytes before the 0xbf. If p is at buf, or buf
9475 * + 1, these may be in the "prev" string. */
9476 char_u back1 = p >= buf + 1 ? p[-1]
9477 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
9478 char_u back2 = p >= buf + 2 ? p[-2]
9479 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
9480 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
9481
9482 if (back2 == 0xef && back1 == 0xbb)
9483 {
9484 char_u *dest = p - 2;
9485
9486 /* Usually a BOM is at the beginning of a file, and so at
9487 * the beginning of a line; then we can just step over it.
9488 */
9489 if (start == dest)
9490 start = p + 1;
9491 else
9492 {
9493 /* have to shuffle buf to close gap */
9494 int adjust_prevlen = 0;
9495
9496 if (dest < buf)
9497 {
9498 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
9499 dest = buf;
9500 }
9501 if (readlen > p - buf + 1)
9502 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
9503 readlen -= 3 - adjust_prevlen;
9504 prevlen -= adjust_prevlen;
9505 p = dest - 1;
9506 }
9507 }
9508 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009509 } /* for */
9510
9511 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
9512 break;
9513 if (start < p)
9514 {
9515 /* There's part of a line in buf, store it in "prev". */
9516 if (p - start + prevlen >= prevsize)
9517 {
9518 /* need bigger "prev" buffer */
9519 char_u *newprev;
9520
9521 /* A common use case is ordinary text files and "prev" gets a
9522 * fragment of a line, so the first allocation is made
9523 * small, to avoid repeatedly 'allocing' large and
9524 * 'reallocing' small. */
9525 if (prevsize == 0)
9526 prevsize = (long)(p - start);
9527 else
9528 {
9529 long grow50pc = (prevsize * 3) / 2;
9530 long growmin = (long)((p - start) * 2 + prevlen);
9531 prevsize = grow50pc > growmin ? grow50pc : growmin;
9532 }
9533 newprev = prev == NULL ? alloc(prevsize)
9534 : vim_realloc(prev, prevsize);
9535 if (newprev == NULL)
9536 {
9537 do_outofmem_msg((long_u)prevsize);
9538 failed = TRUE;
9539 break;
9540 }
9541 prev = newprev;
9542 }
9543 /* Add the line part to end of "prev". */
9544 mch_memmove(prev + prevlen, start, p - start);
9545 prevlen += (long)(p - start);
9546 }
9547 } /* while */
9548
9549 /*
9550 * For a negative line count use only the lines at the end of the file,
9551 * free the rest.
9552 */
9553 if (!failed && maxline < 0)
9554 while (cnt > -maxline)
9555 {
9556 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
9557 --cnt;
9558 }
9559
9560 if (failed)
9561 {
9562 list_free(rettv->vval.v_list);
9563 /* readfile doc says an empty list is returned on error */
9564 rettv->vval.v_list = list_alloc();
9565 }
9566
9567 vim_free(prev);
9568 fclose(fd);
9569}
9570
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009571 static void
9572return_register(int regname, typval_T *rettv)
9573{
9574 char_u buf[2] = {0, 0};
9575
9576 buf[0] = (char_u)regname;
9577 rettv->v_type = VAR_STRING;
9578 rettv->vval.v_string = vim_strsave(buf);
9579}
9580
9581/*
9582 * "reg_executing()" function
9583 */
9584 static void
9585f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9586{
9587 return_register(reg_executing, rettv);
9588}
9589
9590/*
9591 * "reg_recording()" function
9592 */
9593 static void
9594f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9595{
9596 return_register(reg_recording, rettv);
9597}
9598
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009599#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009600/*
9601 * Convert a List to proftime_T.
9602 * Return FAIL when there is something wrong.
9603 */
9604 static int
9605list2proftime(typval_T *arg, proftime_T *tm)
9606{
9607 long n1, n2;
9608 int error = FALSE;
9609
9610 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9611 || arg->vval.v_list->lv_len != 2)
9612 return FAIL;
9613 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9614 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009615# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009616 tm->HighPart = n1;
9617 tm->LowPart = n2;
9618# else
9619 tm->tv_sec = n1;
9620 tm->tv_usec = n2;
9621# endif
9622 return error ? FAIL : OK;
9623}
9624#endif /* FEAT_RELTIME */
9625
9626/*
9627 * "reltime()" function
9628 */
9629 static void
9630f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9631{
9632#ifdef FEAT_RELTIME
9633 proftime_T res;
9634 proftime_T start;
9635
9636 if (argvars[0].v_type == VAR_UNKNOWN)
9637 {
9638 /* No arguments: get current time. */
9639 profile_start(&res);
9640 }
9641 else if (argvars[1].v_type == VAR_UNKNOWN)
9642 {
9643 if (list2proftime(&argvars[0], &res) == FAIL)
9644 return;
9645 profile_end(&res);
9646 }
9647 else
9648 {
9649 /* Two arguments: compute the difference. */
9650 if (list2proftime(&argvars[0], &start) == FAIL
9651 || list2proftime(&argvars[1], &res) == FAIL)
9652 return;
9653 profile_sub(&res, &start);
9654 }
9655
9656 if (rettv_list_alloc(rettv) == OK)
9657 {
9658 long n1, n2;
9659
Bram Moolenaar4f974752019-02-17 17:44:42 +01009660# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009661 n1 = res.HighPart;
9662 n2 = res.LowPart;
9663# else
9664 n1 = res.tv_sec;
9665 n2 = res.tv_usec;
9666# endif
9667 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9668 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9669 }
9670#endif
9671}
9672
9673#ifdef FEAT_FLOAT
9674/*
9675 * "reltimefloat()" function
9676 */
9677 static void
9678f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9679{
9680# ifdef FEAT_RELTIME
9681 proftime_T tm;
9682# endif
9683
9684 rettv->v_type = VAR_FLOAT;
9685 rettv->vval.v_float = 0;
9686# ifdef FEAT_RELTIME
9687 if (list2proftime(&argvars[0], &tm) == OK)
9688 rettv->vval.v_float = profile_float(&tm);
9689# endif
9690}
9691#endif
9692
9693/*
9694 * "reltimestr()" function
9695 */
9696 static void
9697f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9698{
9699#ifdef FEAT_RELTIME
9700 proftime_T tm;
9701#endif
9702
9703 rettv->v_type = VAR_STRING;
9704 rettv->vval.v_string = NULL;
9705#ifdef FEAT_RELTIME
9706 if (list2proftime(&argvars[0], &tm) == OK)
9707 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9708#endif
9709}
9710
9711#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009712 static void
9713make_connection(void)
9714{
9715 if (X_DISPLAY == NULL
9716# ifdef FEAT_GUI
9717 && !gui.in_use
9718# endif
9719 )
9720 {
9721 x_force_connect = TRUE;
9722 setup_term_clip();
9723 x_force_connect = FALSE;
9724 }
9725}
9726
9727 static int
9728check_connection(void)
9729{
9730 make_connection();
9731 if (X_DISPLAY == NULL)
9732 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009733 emsg(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009734 return FAIL;
9735 }
9736 return OK;
9737}
9738#endif
9739
9740#ifdef FEAT_CLIENTSERVER
9741 static void
9742remote_common(typval_T *argvars, typval_T *rettv, int expr)
9743{
9744 char_u *server_name;
9745 char_u *keys;
9746 char_u *r = NULL;
9747 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009748 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009749# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009750 HWND w;
9751# else
9752 Window w;
9753# endif
9754
9755 if (check_restricted() || check_secure())
9756 return;
9757
9758# ifdef FEAT_X11
9759 if (check_connection() == FAIL)
9760 return;
9761# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009762 if (argvars[2].v_type != VAR_UNKNOWN
9763 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009764 timeout = tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009765
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009766 server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009767 if (server_name == NULL)
9768 return; /* type error; errmsg already given */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009769 keys = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009770# ifdef MSWIN
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009771 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009772# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009773 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9774 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009775# endif
9776 {
9777 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009778 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009779 emsg((char *)r); // sending worked but evaluation failed
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009780 vim_free(r);
9781 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009782 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009783 semsg(_("E241: Unable to send to %s"), server_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009784 return;
9785 }
9786
9787 rettv->vval.v_string = r;
9788
9789 if (argvars[2].v_type != VAR_UNKNOWN)
9790 {
9791 dictitem_T v;
9792 char_u str[30];
9793 char_u *idvar;
9794
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009795 idvar = tv_get_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009796 if (idvar != NULL && *idvar != NUL)
9797 {
9798 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9799 v.di_tv.v_type = VAR_STRING;
9800 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009801 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009802 vim_free(v.di_tv.vval.v_string);
9803 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009804 }
9805}
9806#endif
9807
9808/*
9809 * "remote_expr()" function
9810 */
9811 static void
9812f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9813{
9814 rettv->v_type = VAR_STRING;
9815 rettv->vval.v_string = NULL;
9816#ifdef FEAT_CLIENTSERVER
9817 remote_common(argvars, rettv, TRUE);
9818#endif
9819}
9820
9821/*
9822 * "remote_foreground()" function
9823 */
9824 static void
9825f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9826{
9827#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +01009828# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009829 /* On Win32 it's done in this application. */
9830 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009831 char_u *server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009832
9833 if (server_name != NULL)
9834 serverForeground(server_name);
9835 }
9836# else
9837 /* Send a foreground() expression to the server. */
9838 argvars[1].v_type = VAR_STRING;
9839 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9840 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009841 rettv->v_type = VAR_STRING;
9842 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009843 remote_common(argvars, rettv, TRUE);
9844 vim_free(argvars[1].vval.v_string);
9845# endif
9846#endif
9847}
9848
9849 static void
9850f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
9851{
9852#ifdef FEAT_CLIENTSERVER
9853 dictitem_T v;
9854 char_u *s = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009855# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009856 long_u n = 0;
9857# endif
9858 char_u *serverid;
9859
9860 if (check_restricted() || check_secure())
9861 {
9862 rettv->vval.v_number = -1;
9863 return;
9864 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009865 serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009866 if (serverid == NULL)
9867 {
9868 rettv->vval.v_number = -1;
9869 return; /* type error; errmsg already given */
9870 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01009871# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009872 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
9873 if (n == 0)
9874 rettv->vval.v_number = -1;
9875 else
9876 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009877 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009878 rettv->vval.v_number = (s != NULL);
9879 }
9880# else
9881 if (check_connection() == FAIL)
9882 return;
9883
9884 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
9885 serverStrToWin(serverid), &s);
9886# endif
9887
9888 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
9889 {
9890 char_u *retvar;
9891
9892 v.di_tv.v_type = VAR_STRING;
9893 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009894 retvar = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009895 if (retvar != NULL)
9896 set_var(retvar, &v.di_tv, FALSE);
9897 vim_free(v.di_tv.vval.v_string);
9898 }
9899#else
9900 rettv->vval.v_number = -1;
9901#endif
9902}
9903
9904 static void
9905f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
9906{
9907 char_u *r = NULL;
9908
9909#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009910 char_u *serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009911
9912 if (serverid != NULL && !check_restricted() && !check_secure())
9913 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009914 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009915# ifdef MSWIN
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009916 /* The server's HWND is encoded in the 'id' parameter */
9917 long_u n = 0;
9918# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009919
9920 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009921 timeout = tv_get_number(&argvars[1]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009922
Bram Moolenaar4f974752019-02-17 17:44:42 +01009923# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009924 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
9925 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009926 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009927 if (r == NULL)
9928# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009929 if (check_connection() == FAIL
9930 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
9931 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009932# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009933 emsg(_("E277: Unable to read a server reply"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009934 }
9935#endif
9936 rettv->v_type = VAR_STRING;
9937 rettv->vval.v_string = r;
9938}
9939
9940/*
9941 * "remote_send()" function
9942 */
9943 static void
9944f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9945{
9946 rettv->v_type = VAR_STRING;
9947 rettv->vval.v_string = NULL;
9948#ifdef FEAT_CLIENTSERVER
9949 remote_common(argvars, rettv, FALSE);
9950#endif
9951}
9952
9953/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009954 * "remote_startserver()" function
9955 */
9956 static void
9957f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9958{
9959#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009960 char_u *server = tv_get_string_chk(&argvars[0]);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009961
9962 if (server == NULL)
9963 return; /* type error; errmsg already given */
9964 if (serverName != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009965 emsg(_("E941: already started a server"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009966 else
9967 {
9968# ifdef FEAT_X11
9969 if (check_connection() == OK)
9970 serverRegisterName(X_DISPLAY, server);
9971# else
9972 serverSetName(server);
9973# endif
9974 }
9975#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009976 emsg(_("E942: +clientserver feature not available"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009977#endif
9978}
9979
9980/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009981 * "remove()" function
9982 */
9983 static void
9984f_remove(typval_T *argvars, typval_T *rettv)
9985{
9986 list_T *l;
9987 listitem_T *item, *item2;
9988 listitem_T *li;
9989 long idx;
9990 long end;
9991 char_u *key;
9992 dict_T *d;
9993 dictitem_T *di;
9994 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009995 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009996
9997 if (argvars[0].v_type == VAR_DICT)
9998 {
9999 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010000 semsg(_(e_toomanyarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010001 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010002 && !var_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010003 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010004 key = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010005 if (key != NULL)
10006 {
10007 di = dict_find(d, key, -1);
10008 if (di == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010009 semsg(_(e_dictkey), key);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010010 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10011 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
10012 {
10013 *rettv = di->di_tv;
10014 init_tv(&di->di_tv);
10015 dictitem_remove(d, di);
10016 }
10017 }
10018 }
10019 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010020 else if (argvars[0].v_type == VAR_BLOB)
10021 {
10022 idx = (long)tv_get_number_chk(&argvars[1], &error);
10023 if (!error)
10024 {
10025 blob_T *b = argvars[0].vval.v_blob;
10026 int len = blob_len(b);
10027 char_u *p;
10028
10029 if (idx < 0)
10030 // count from the end
10031 idx = len + idx;
10032 if (idx < 0 || idx >= len)
10033 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010034 semsg(_(e_blobidx), idx);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010035 return;
10036 }
10037 if (argvars[2].v_type == VAR_UNKNOWN)
10038 {
10039 // Remove one item, return its value.
10040 p = (char_u *)b->bv_ga.ga_data;
10041 rettv->vval.v_number = (varnumber_T) *(p + idx);
10042 mch_memmove(p + idx, p + idx + 1, (size_t)len - idx - 1);
10043 --b->bv_ga.ga_len;
10044 }
10045 else
10046 {
10047 blob_T *blob;
10048
10049 // Remove range of items, return list with values.
10050 end = (long)tv_get_number_chk(&argvars[2], &error);
10051 if (error)
10052 return;
10053 if (end < 0)
10054 // count from the end
10055 end = len + end;
10056 if (end >= len || idx > end)
10057 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010058 semsg(_(e_blobidx), end);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010059 return;
10060 }
10061 blob = blob_alloc();
10062 if (blob == NULL)
10063 return;
10064 blob->bv_ga.ga_len = end - idx + 1;
10065 if (ga_grow(&blob->bv_ga, end - idx + 1) == FAIL)
10066 {
10067 vim_free(blob);
10068 return;
10069 }
10070 p = (char_u *)b->bv_ga.ga_data;
10071 mch_memmove((char_u *)blob->bv_ga.ga_data, p + idx,
10072 (size_t)(end - idx + 1));
10073 ++blob->bv_refcount;
10074 rettv->v_type = VAR_BLOB;
10075 rettv->vval.v_blob = blob;
10076
10077 mch_memmove(p + idx, p + end + 1, (size_t)(len - end));
10078 b->bv_ga.ga_len -= end - idx + 1;
10079 }
10080 }
10081 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010082 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010083 semsg(_(e_listdictblobarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010084 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010085 && !var_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010086 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010087 idx = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010088 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010089 ; // type error: do nothing, errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010090 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010091 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010092 else
10093 {
10094 if (argvars[2].v_type == VAR_UNKNOWN)
10095 {
10096 /* Remove one item, return its value. */
10097 vimlist_remove(l, item, item);
10098 *rettv = item->li_tv;
10099 vim_free(item);
10100 }
10101 else
10102 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010103 // Remove range of items, return list with values.
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010104 end = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010105 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010106 ; // type error: do nothing
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010107 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010108 semsg(_(e_listidx), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010109 else
10110 {
10111 int cnt = 0;
10112
10113 for (li = item; li != NULL; li = li->li_next)
10114 {
10115 ++cnt;
10116 if (li == item2)
10117 break;
10118 }
10119 if (li == NULL) /* didn't find "item2" after "item" */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010120 emsg(_(e_invrange));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010121 else
10122 {
10123 vimlist_remove(l, item, item2);
10124 if (rettv_list_alloc(rettv) == OK)
10125 {
10126 l = rettv->vval.v_list;
10127 l->lv_first = item;
10128 l->lv_last = item2;
10129 item->li_prev = NULL;
10130 item2->li_next = NULL;
10131 l->lv_len = cnt;
10132 }
10133 }
10134 }
10135 }
10136 }
10137 }
10138}
10139
10140/*
10141 * "rename({from}, {to})" function
10142 */
10143 static void
10144f_rename(typval_T *argvars, typval_T *rettv)
10145{
10146 char_u buf[NUMBUFLEN];
10147
10148 if (check_restricted() || check_secure())
10149 rettv->vval.v_number = -1;
10150 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010151 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
10152 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010153}
10154
10155/*
10156 * "repeat()" function
10157 */
10158 static void
10159f_repeat(typval_T *argvars, typval_T *rettv)
10160{
10161 char_u *p;
10162 int n;
10163 int slen;
10164 int len;
10165 char_u *r;
10166 int i;
10167
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010168 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010169 if (argvars[0].v_type == VAR_LIST)
10170 {
10171 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
10172 while (n-- > 0)
10173 if (list_extend(rettv->vval.v_list,
10174 argvars[0].vval.v_list, NULL) == FAIL)
10175 break;
10176 }
10177 else
10178 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010179 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010180 rettv->v_type = VAR_STRING;
10181 rettv->vval.v_string = NULL;
10182
10183 slen = (int)STRLEN(p);
10184 len = slen * n;
10185 if (len <= 0)
10186 return;
10187
10188 r = alloc(len + 1);
10189 if (r != NULL)
10190 {
10191 for (i = 0; i < n; i++)
10192 mch_memmove(r + i * slen, p, (size_t)slen);
10193 r[len] = NUL;
10194 }
10195
10196 rettv->vval.v_string = r;
10197 }
10198}
10199
10200/*
10201 * "resolve()" function
10202 */
10203 static void
10204f_resolve(typval_T *argvars, typval_T *rettv)
10205{
10206 char_u *p;
10207#ifdef HAVE_READLINK
10208 char_u *buf = NULL;
10209#endif
10210
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010211 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010212#ifdef FEAT_SHORTCUT
10213 {
10214 char_u *v = NULL;
10215
Bram Moolenaardce1e892019-02-10 23:18:53 +010010216 v = mch_resolve_path(p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010217 if (v != NULL)
10218 rettv->vval.v_string = v;
10219 else
10220 rettv->vval.v_string = vim_strsave(p);
10221 }
10222#else
10223# ifdef HAVE_READLINK
10224 {
10225 char_u *cpy;
10226 int len;
10227 char_u *remain = NULL;
10228 char_u *q;
10229 int is_relative_to_current = FALSE;
10230 int has_trailing_pathsep = FALSE;
10231 int limit = 100;
10232
10233 p = vim_strsave(p);
10234
10235 if (p[0] == '.' && (vim_ispathsep(p[1])
10236 || (p[1] == '.' && (vim_ispathsep(p[2])))))
10237 is_relative_to_current = TRUE;
10238
10239 len = STRLEN(p);
10240 if (len > 0 && after_pathsep(p, p + len))
10241 {
10242 has_trailing_pathsep = TRUE;
10243 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
10244 }
10245
10246 q = getnextcomp(p);
10247 if (*q != NUL)
10248 {
10249 /* Separate the first path component in "p", and keep the
10250 * remainder (beginning with the path separator). */
10251 remain = vim_strsave(q - 1);
10252 q[-1] = NUL;
10253 }
10254
10255 buf = alloc(MAXPATHL + 1);
10256 if (buf == NULL)
10257 goto fail;
10258
10259 for (;;)
10260 {
10261 for (;;)
10262 {
10263 len = readlink((char *)p, (char *)buf, MAXPATHL);
10264 if (len <= 0)
10265 break;
10266 buf[len] = NUL;
10267
10268 if (limit-- == 0)
10269 {
10270 vim_free(p);
10271 vim_free(remain);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010272 emsg(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010273 rettv->vval.v_string = NULL;
10274 goto fail;
10275 }
10276
10277 /* Ensure that the result will have a trailing path separator
10278 * if the argument has one. */
10279 if (remain == NULL && has_trailing_pathsep)
10280 add_pathsep(buf);
10281
10282 /* Separate the first path component in the link value and
10283 * concatenate the remainders. */
10284 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
10285 if (*q != NUL)
10286 {
10287 if (remain == NULL)
10288 remain = vim_strsave(q - 1);
10289 else
10290 {
10291 cpy = concat_str(q - 1, remain);
10292 if (cpy != NULL)
10293 {
10294 vim_free(remain);
10295 remain = cpy;
10296 }
10297 }
10298 q[-1] = NUL;
10299 }
10300
10301 q = gettail(p);
10302 if (q > p && *q == NUL)
10303 {
10304 /* Ignore trailing path separator. */
10305 q[-1] = NUL;
10306 q = gettail(p);
10307 }
10308 if (q > p && !mch_isFullName(buf))
10309 {
10310 /* symlink is relative to directory of argument */
10311 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
10312 if (cpy != NULL)
10313 {
10314 STRCPY(cpy, p);
10315 STRCPY(gettail(cpy), buf);
10316 vim_free(p);
10317 p = cpy;
10318 }
10319 }
10320 else
10321 {
10322 vim_free(p);
10323 p = vim_strsave(buf);
10324 }
10325 }
10326
10327 if (remain == NULL)
10328 break;
10329
10330 /* Append the first path component of "remain" to "p". */
10331 q = getnextcomp(remain + 1);
10332 len = q - remain - (*q != NUL);
10333 cpy = vim_strnsave(p, STRLEN(p) + len);
10334 if (cpy != NULL)
10335 {
10336 STRNCAT(cpy, remain, len);
10337 vim_free(p);
10338 p = cpy;
10339 }
10340 /* Shorten "remain". */
10341 if (*q != NUL)
10342 STRMOVE(remain, q - 1);
10343 else
Bram Moolenaard23a8232018-02-10 18:45:26 +010010344 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010345 }
10346
10347 /* If the result is a relative path name, make it explicitly relative to
10348 * the current directory if and only if the argument had this form. */
10349 if (!vim_ispathsep(*p))
10350 {
10351 if (is_relative_to_current
10352 && *p != NUL
10353 && !(p[0] == '.'
10354 && (p[1] == NUL
10355 || vim_ispathsep(p[1])
10356 || (p[1] == '.'
10357 && (p[2] == NUL
10358 || vim_ispathsep(p[2]))))))
10359 {
10360 /* Prepend "./". */
10361 cpy = concat_str((char_u *)"./", p);
10362 if (cpy != NULL)
10363 {
10364 vim_free(p);
10365 p = cpy;
10366 }
10367 }
10368 else if (!is_relative_to_current)
10369 {
10370 /* Strip leading "./". */
10371 q = p;
10372 while (q[0] == '.' && vim_ispathsep(q[1]))
10373 q += 2;
10374 if (q > p)
10375 STRMOVE(p, p + 2);
10376 }
10377 }
10378
10379 /* Ensure that the result will have no trailing path separator
10380 * if the argument had none. But keep "/" or "//". */
10381 if (!has_trailing_pathsep)
10382 {
10383 q = p + STRLEN(p);
10384 if (after_pathsep(p, q))
10385 *gettail_sep(p) = NUL;
10386 }
10387
10388 rettv->vval.v_string = p;
10389 }
10390# else
10391 rettv->vval.v_string = vim_strsave(p);
10392# endif
10393#endif
10394
10395 simplify_filename(rettv->vval.v_string);
10396
10397#ifdef HAVE_READLINK
10398fail:
10399 vim_free(buf);
10400#endif
10401 rettv->v_type = VAR_STRING;
10402}
10403
10404/*
10405 * "reverse({list})" function
10406 */
10407 static void
10408f_reverse(typval_T *argvars, typval_T *rettv)
10409{
10410 list_T *l;
10411 listitem_T *li, *ni;
10412
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010413 if (argvars[0].v_type == VAR_BLOB)
10414 {
10415 blob_T *b = argvars[0].vval.v_blob;
10416 int i, len = blob_len(b);
10417
10418 for (i = 0; i < len / 2; i++)
10419 {
10420 int tmp = blob_get(b, i);
10421
10422 blob_set(b, i, blob_get(b, len - i - 1));
10423 blob_set(b, len - i - 1, tmp);
10424 }
10425 rettv_blob_set(rettv, b);
10426 return;
10427 }
10428
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010429 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010430 semsg(_(e_listblobarg), "reverse()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010431 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010432 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010433 (char_u *)N_("reverse() argument"), TRUE))
10434 {
10435 li = l->lv_last;
10436 l->lv_first = l->lv_last = NULL;
10437 l->lv_len = 0;
10438 while (li != NULL)
10439 {
10440 ni = li->li_prev;
10441 list_append(l, li);
10442 li = ni;
10443 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020010444 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010445 l->lv_idx = l->lv_len - l->lv_idx - 1;
10446 }
10447}
10448
10449#define SP_NOMOVE 0x01 /* don't move cursor */
10450#define SP_REPEAT 0x02 /* repeat to find outer pair */
10451#define SP_RETCOUNT 0x04 /* return matchcount */
10452#define SP_SETPCMARK 0x08 /* set previous context mark */
10453#define SP_START 0x10 /* accept match at start position */
10454#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
10455#define SP_END 0x40 /* leave cursor at end of match */
10456#define SP_COLUMN 0x80 /* start at cursor column */
10457
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010458/*
10459 * Get flags for a search function.
10460 * Possibly sets "p_ws".
10461 * Returns BACKWARD, FORWARD or zero (for an error).
10462 */
10463 static int
10464get_search_arg(typval_T *varp, int *flagsp)
10465{
10466 int dir = FORWARD;
10467 char_u *flags;
10468 char_u nbuf[NUMBUFLEN];
10469 int mask;
10470
10471 if (varp->v_type != VAR_UNKNOWN)
10472 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010473 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010474 if (flags == NULL)
10475 return 0; /* type error; errmsg already given */
10476 while (*flags != NUL)
10477 {
10478 switch (*flags)
10479 {
10480 case 'b': dir = BACKWARD; break;
10481 case 'w': p_ws = TRUE; break;
10482 case 'W': p_ws = FALSE; break;
10483 default: mask = 0;
10484 if (flagsp != NULL)
10485 switch (*flags)
10486 {
10487 case 'c': mask = SP_START; break;
10488 case 'e': mask = SP_END; break;
10489 case 'm': mask = SP_RETCOUNT; break;
10490 case 'n': mask = SP_NOMOVE; break;
10491 case 'p': mask = SP_SUBPAT; break;
10492 case 'r': mask = SP_REPEAT; break;
10493 case 's': mask = SP_SETPCMARK; break;
10494 case 'z': mask = SP_COLUMN; break;
10495 }
10496 if (mask == 0)
10497 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010498 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010499 dir = 0;
10500 }
10501 else
10502 *flagsp |= mask;
10503 }
10504 if (dir == 0)
10505 break;
10506 ++flags;
10507 }
10508 }
10509 return dir;
10510}
10511
10512/*
10513 * Shared by search() and searchpos() functions.
10514 */
10515 static int
10516search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
10517{
10518 int flags;
10519 char_u *pat;
10520 pos_T pos;
10521 pos_T save_cursor;
10522 int save_p_ws = p_ws;
10523 int dir;
10524 int retval = 0; /* default: FAIL */
10525 long lnum_stop = 0;
10526 proftime_T tm;
10527#ifdef FEAT_RELTIME
10528 long time_limit = 0;
10529#endif
10530 int options = SEARCH_KEEP;
10531 int subpatnum;
10532
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010533 pat = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010534 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
10535 if (dir == 0)
10536 goto theend;
10537 flags = *flagsp;
10538 if (flags & SP_START)
10539 options |= SEARCH_START;
10540 if (flags & SP_END)
10541 options |= SEARCH_END;
10542 if (flags & SP_COLUMN)
10543 options |= SEARCH_COL;
10544
10545 /* Optional arguments: line number to stop searching and timeout. */
10546 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
10547 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010548 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010549 if (lnum_stop < 0)
10550 goto theend;
10551#ifdef FEAT_RELTIME
10552 if (argvars[3].v_type != VAR_UNKNOWN)
10553 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010554 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010555 if (time_limit < 0)
10556 goto theend;
10557 }
10558#endif
10559 }
10560
10561#ifdef FEAT_RELTIME
10562 /* Set the time limit, if there is one. */
10563 profile_setlimit(time_limit, &tm);
10564#endif
10565
10566 /*
10567 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
10568 * Check to make sure only those flags are set.
10569 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
10570 * flags cannot be set. Check for that condition also.
10571 */
10572 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
10573 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10574 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010575 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010576 goto theend;
10577 }
10578
10579 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010580 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010581 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010582 if (subpatnum != FAIL)
10583 {
10584 if (flags & SP_SUBPAT)
10585 retval = subpatnum;
10586 else
10587 retval = pos.lnum;
10588 if (flags & SP_SETPCMARK)
10589 setpcmark();
10590 curwin->w_cursor = pos;
10591 if (match_pos != NULL)
10592 {
10593 /* Store the match cursor position */
10594 match_pos->lnum = pos.lnum;
10595 match_pos->col = pos.col + 1;
10596 }
10597 /* "/$" will put the cursor after the end of the line, may need to
10598 * correct that here */
10599 check_cursor();
10600 }
10601
10602 /* If 'n' flag is used: restore cursor position. */
10603 if (flags & SP_NOMOVE)
10604 curwin->w_cursor = save_cursor;
10605 else
10606 curwin->w_set_curswant = TRUE;
10607theend:
10608 p_ws = save_p_ws;
10609
10610 return retval;
10611}
10612
10613#ifdef FEAT_FLOAT
10614
10615/*
10616 * round() is not in C90, use ceil() or floor() instead.
10617 */
10618 float_T
10619vim_round(float_T f)
10620{
10621 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
10622}
10623
10624/*
10625 * "round({float})" function
10626 */
10627 static void
10628f_round(typval_T *argvars, typval_T *rettv)
10629{
10630 float_T f = 0.0;
10631
10632 rettv->v_type = VAR_FLOAT;
10633 if (get_float_arg(argvars, &f) == OK)
10634 rettv->vval.v_float = vim_round(f);
10635 else
10636 rettv->vval.v_float = 0.0;
10637}
10638#endif
10639
Bram Moolenaare99be0e2019-03-26 22:51:09 +010010640#ifdef FEAT_RUBY
10641/*
10642 * "rubyeval()" function
10643 */
10644 static void
10645f_rubyeval(typval_T *argvars, typval_T *rettv)
10646{
10647 char_u *str;
10648 char_u buf[NUMBUFLEN];
10649
10650 str = tv_get_string_buf(&argvars[0], buf);
10651 do_rubyeval(str, rettv);
10652}
10653#endif
10654
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010655/*
10656 * "screenattr()" function
10657 */
10658 static void
10659f_screenattr(typval_T *argvars, typval_T *rettv)
10660{
10661 int row;
10662 int col;
10663 int c;
10664
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010665 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10666 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010667 if (row < 0 || row >= screen_Rows
10668 || col < 0 || col >= screen_Columns)
10669 c = -1;
10670 else
10671 c = ScreenAttrs[LineOffset[row] + col];
10672 rettv->vval.v_number = c;
10673}
10674
10675/*
10676 * "screenchar()" function
10677 */
10678 static void
10679f_screenchar(typval_T *argvars, typval_T *rettv)
10680{
10681 int row;
10682 int col;
10683 int off;
10684 int c;
10685
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010686 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10687 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010688 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010689 c = -1;
10690 else
10691 {
10692 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010693 if (enc_utf8 && ScreenLinesUC[off] != 0)
10694 c = ScreenLinesUC[off];
10695 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010696 c = ScreenLines[off];
10697 }
10698 rettv->vval.v_number = c;
10699}
10700
10701/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010702 * "screenchars()" function
10703 */
10704 static void
10705f_screenchars(typval_T *argvars, typval_T *rettv)
10706{
10707 int row;
10708 int col;
10709 int off;
10710 int c;
10711 int i;
10712
10713 if (rettv_list_alloc(rettv) == FAIL)
10714 return;
10715 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10716 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10717 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10718 return;
10719
10720 off = LineOffset[row] + col;
10721 if (enc_utf8 && ScreenLinesUC[off] != 0)
10722 c = ScreenLinesUC[off];
10723 else
10724 c = ScreenLines[off];
10725 list_append_number(rettv->vval.v_list, (varnumber_T)c);
10726
10727 if (enc_utf8)
10728
10729 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10730 list_append_number(rettv->vval.v_list,
10731 (varnumber_T)ScreenLinesC[i][off]);
10732}
10733
10734/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010735 * "screencol()" function
10736 *
10737 * First column is 1 to be consistent with virtcol().
10738 */
10739 static void
10740f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10741{
10742 rettv->vval.v_number = screen_screencol() + 1;
10743}
10744
10745/*
10746 * "screenrow()" function
10747 */
10748 static void
10749f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10750{
10751 rettv->vval.v_number = screen_screenrow() + 1;
10752}
10753
10754/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010755 * "screenstring()" function
10756 */
10757 static void
10758f_screenstring(typval_T *argvars, typval_T *rettv)
10759{
10760 int row;
10761 int col;
10762 int off;
10763 int c;
10764 int i;
10765 char_u buf[MB_MAXBYTES + 1];
10766 int buflen = 0;
10767
10768 rettv->vval.v_string = NULL;
10769 rettv->v_type = VAR_STRING;
10770
10771 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10772 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10773 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10774 return;
10775
10776 off = LineOffset[row] + col;
10777 if (enc_utf8 && ScreenLinesUC[off] != 0)
10778 c = ScreenLinesUC[off];
10779 else
10780 c = ScreenLines[off];
10781 buflen += mb_char2bytes(c, buf);
10782
10783 if (enc_utf8)
10784 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10785 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
10786
10787 buf[buflen] = NUL;
10788 rettv->vval.v_string = vim_strsave(buf);
10789}
10790
10791/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010792 * "search()" function
10793 */
10794 static void
10795f_search(typval_T *argvars, typval_T *rettv)
10796{
10797 int flags = 0;
10798
10799 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10800}
10801
10802/*
10803 * "searchdecl()" function
10804 */
10805 static void
10806f_searchdecl(typval_T *argvars, typval_T *rettv)
10807{
10808 int locally = 1;
10809 int thisblock = 0;
10810 int error = FALSE;
10811 char_u *name;
10812
10813 rettv->vval.v_number = 1; /* default: FAIL */
10814
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010815 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010816 if (argvars[1].v_type != VAR_UNKNOWN)
10817 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010818 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010819 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010820 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010821 }
10822 if (!error && name != NULL)
10823 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10824 locally, thisblock, SEARCH_KEEP) == FAIL;
10825}
10826
10827/*
10828 * Used by searchpair() and searchpairpos()
10829 */
10830 static int
10831searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10832{
10833 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010834 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010835 int save_p_ws = p_ws;
10836 int dir;
10837 int flags = 0;
10838 char_u nbuf1[NUMBUFLEN];
10839 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010840 int retval = 0; /* default: FAIL */
10841 long lnum_stop = 0;
10842 long time_limit = 0;
10843
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010844 /* Get the three pattern arguments: start, middle, end. Will result in an
10845 * error if not a valid argument. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010846 spat = tv_get_string_chk(&argvars[0]);
10847 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
10848 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010849 if (spat == NULL || mpat == NULL || epat == NULL)
10850 goto theend; /* type error */
10851
10852 /* Handle the optional fourth argument: flags */
10853 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
10854 if (dir == 0)
10855 goto theend;
10856
10857 /* Don't accept SP_END or SP_SUBPAT.
10858 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
10859 */
10860 if ((flags & (SP_END | SP_SUBPAT)) != 0
10861 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10862 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010863 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010864 goto theend;
10865 }
10866
10867 /* Using 'r' implies 'W', otherwise it doesn't work. */
10868 if (flags & SP_REPEAT)
10869 p_ws = FALSE;
10870
10871 /* Optional fifth argument: skip expression */
10872 if (argvars[3].v_type == VAR_UNKNOWN
10873 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010010874 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010875 else
10876 {
Bram Moolenaar48570482017-10-30 21:48:41 +010010877 skip = &argvars[4];
10878 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
10879 && skip->v_type != VAR_STRING)
10880 {
10881 /* Type error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010882 semsg(_(e_invarg2), tv_get_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010010883 goto theend;
10884 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010885 if (argvars[5].v_type != VAR_UNKNOWN)
10886 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010887 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010888 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010889 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010890 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010891 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010892 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010893#ifdef FEAT_RELTIME
10894 if (argvars[6].v_type != VAR_UNKNOWN)
10895 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010896 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010897 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010898 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010899 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010900 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010901 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010902 }
10903#endif
10904 }
10905 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010906
10907 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
10908 match_pos, lnum_stop, time_limit);
10909
10910theend:
10911 p_ws = save_p_ws;
10912
10913 return retval;
10914}
10915
10916/*
10917 * "searchpair()" function
10918 */
10919 static void
10920f_searchpair(typval_T *argvars, typval_T *rettv)
10921{
10922 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
10923}
10924
10925/*
10926 * "searchpairpos()" function
10927 */
10928 static void
10929f_searchpairpos(typval_T *argvars, typval_T *rettv)
10930{
10931 pos_T match_pos;
10932 int lnum = 0;
10933 int col = 0;
10934
10935 if (rettv_list_alloc(rettv) == FAIL)
10936 return;
10937
10938 if (searchpair_cmn(argvars, &match_pos) > 0)
10939 {
10940 lnum = match_pos.lnum;
10941 col = match_pos.col;
10942 }
10943
10944 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10945 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10946}
10947
10948/*
10949 * Search for a start/middle/end thing.
10950 * Used by searchpair(), see its documentation for the details.
10951 * Returns 0 or -1 for no match,
10952 */
10953 long
10954do_searchpair(
10955 char_u *spat, /* start pattern */
10956 char_u *mpat, /* middle pattern */
10957 char_u *epat, /* end pattern */
10958 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010010959 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010960 int flags, /* SP_SETPCMARK and other SP_ values */
10961 pos_T *match_pos,
10962 linenr_T lnum_stop, /* stop at this line if not zero */
10963 long time_limit UNUSED) /* stop after this many msec */
10964{
10965 char_u *save_cpo;
10966 char_u *pat, *pat2 = NULL, *pat3 = NULL;
10967 long retval = 0;
10968 pos_T pos;
10969 pos_T firstpos;
10970 pos_T foundpos;
10971 pos_T save_cursor;
10972 pos_T save_pos;
10973 int n;
10974 int r;
10975 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010010976 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010977 int err;
10978 int options = SEARCH_KEEP;
10979 proftime_T tm;
10980
10981 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10982 save_cpo = p_cpo;
10983 p_cpo = empty_option;
10984
10985#ifdef FEAT_RELTIME
10986 /* Set the time limit, if there is one. */
10987 profile_setlimit(time_limit, &tm);
10988#endif
10989
10990 /* Make two search patterns: start/end (pat2, for in nested pairs) and
10991 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010992 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
10993 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010994 if (pat2 == NULL || pat3 == NULL)
10995 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010996 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010997 if (*mpat == NUL)
10998 STRCPY(pat3, pat2);
10999 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010011000 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011001 spat, epat, mpat);
11002 if (flags & SP_START)
11003 options |= SEARCH_START;
11004
Bram Moolenaar48570482017-10-30 21:48:41 +010011005 if (skip != NULL)
11006 {
11007 /* Empty string means to not use the skip expression. */
11008 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
11009 use_skip = skip->vval.v_string != NULL
11010 && *skip->vval.v_string != NUL;
11011 }
11012
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011013 save_cursor = curwin->w_cursor;
11014 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011015 CLEAR_POS(&firstpos);
11016 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011017 pat = pat3;
11018 for (;;)
11019 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +010011020 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020011021 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011022 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011023 /* didn't find it or found the first match again: FAIL */
11024 break;
11025
11026 if (firstpos.lnum == 0)
11027 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011028 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011029 {
11030 /* Found the same position again. Can happen with a pattern that
11031 * has "\zs" at the end and searching backwards. Advance one
11032 * character and try again. */
11033 if (dir == BACKWARD)
11034 decl(&pos);
11035 else
11036 incl(&pos);
11037 }
11038 foundpos = pos;
11039
11040 /* clear the start flag to avoid getting stuck here */
11041 options &= ~SEARCH_START;
11042
11043 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010011044 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011045 {
11046 save_pos = curwin->w_cursor;
11047 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010011048 err = FALSE;
11049 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011050 curwin->w_cursor = save_pos;
11051 if (err)
11052 {
11053 /* Evaluating {skip} caused an error, break here. */
11054 curwin->w_cursor = save_cursor;
11055 retval = -1;
11056 break;
11057 }
11058 if (r)
11059 continue;
11060 }
11061
11062 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
11063 {
11064 /* Found end when searching backwards or start when searching
11065 * forward: nested pair. */
11066 ++nest;
11067 pat = pat2; /* nested, don't search for middle */
11068 }
11069 else
11070 {
11071 /* Found end when searching forward or start when searching
11072 * backward: end of (nested) pair; or found middle in outer pair. */
11073 if (--nest == 1)
11074 pat = pat3; /* outer level, search for middle */
11075 }
11076
11077 if (nest == 0)
11078 {
11079 /* Found the match: return matchcount or line number. */
11080 if (flags & SP_RETCOUNT)
11081 ++retval;
11082 else
11083 retval = pos.lnum;
11084 if (flags & SP_SETPCMARK)
11085 setpcmark();
11086 curwin->w_cursor = pos;
11087 if (!(flags & SP_REPEAT))
11088 break;
11089 nest = 1; /* search for next unmatched */
11090 }
11091 }
11092
11093 if (match_pos != NULL)
11094 {
11095 /* Store the match cursor position */
11096 match_pos->lnum = curwin->w_cursor.lnum;
11097 match_pos->col = curwin->w_cursor.col + 1;
11098 }
11099
11100 /* If 'n' flag is used or search failed: restore cursor position. */
11101 if ((flags & SP_NOMOVE) || retval == 0)
11102 curwin->w_cursor = save_cursor;
11103
11104theend:
11105 vim_free(pat2);
11106 vim_free(pat3);
11107 if (p_cpo == empty_option)
11108 p_cpo = save_cpo;
11109 else
11110 /* Darn, evaluating the {skip} expression changed the value. */
11111 free_string_option(save_cpo);
11112
11113 return retval;
11114}
11115
11116/*
11117 * "searchpos()" function
11118 */
11119 static void
11120f_searchpos(typval_T *argvars, typval_T *rettv)
11121{
11122 pos_T match_pos;
11123 int lnum = 0;
11124 int col = 0;
11125 int n;
11126 int flags = 0;
11127
11128 if (rettv_list_alloc(rettv) == FAIL)
11129 return;
11130
11131 n = search_cmn(argvars, &match_pos, &flags);
11132 if (n > 0)
11133 {
11134 lnum = match_pos.lnum;
11135 col = match_pos.col;
11136 }
11137
11138 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
11139 list_append_number(rettv->vval.v_list, (varnumber_T)col);
11140 if (flags & SP_SUBPAT)
11141 list_append_number(rettv->vval.v_list, (varnumber_T)n);
11142}
11143
11144 static void
11145f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
11146{
11147#ifdef FEAT_CLIENTSERVER
11148 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011149 char_u *server = tv_get_string_chk(&argvars[0]);
11150 char_u *reply = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011151
11152 rettv->vval.v_number = -1;
11153 if (server == NULL || reply == NULL)
11154 return;
11155 if (check_restricted() || check_secure())
11156 return;
11157# ifdef FEAT_X11
11158 if (check_connection() == FAIL)
11159 return;
11160# endif
11161
11162 if (serverSendReply(server, reply) < 0)
11163 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011164 emsg(_("E258: Unable to send to client"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011165 return;
11166 }
11167 rettv->vval.v_number = 0;
11168#else
11169 rettv->vval.v_number = -1;
11170#endif
11171}
11172
11173 static void
11174f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
11175{
11176 char_u *r = NULL;
11177
11178#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +010011179# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011180 r = serverGetVimNames();
11181# else
11182 make_connection();
11183 if (X_DISPLAY != NULL)
11184 r = serverGetVimNames(X_DISPLAY);
11185# endif
11186#endif
11187 rettv->v_type = VAR_STRING;
11188 rettv->vval.v_string = r;
11189}
11190
11191/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011192 * "setbufline()" function
11193 */
11194 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020011195f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011196{
11197 linenr_T lnum;
11198 buf_T *buf;
11199
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011200 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011201 if (buf == NULL)
11202 rettv->vval.v_number = 1; /* FAIL */
11203 else
11204 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011205 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020011206 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011207 }
11208}
11209
11210/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011211 * "setbufvar()" function
11212 */
11213 static void
11214f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
11215{
11216 buf_T *buf;
11217 char_u *varname, *bufvarname;
11218 typval_T *varp;
11219 char_u nbuf[NUMBUFLEN];
11220
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011221 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011222 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011223 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
11224 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011225 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011226 varp = &argvars[2];
11227
11228 if (buf != NULL && varname != NULL && varp != NULL)
11229 {
11230 if (*varname == '&')
11231 {
11232 long numval;
11233 char_u *strval;
11234 int error = FALSE;
11235 aco_save_T aco;
11236
11237 /* set curbuf to be our buf, temporarily */
11238 aucmd_prepbuf(&aco, buf);
11239
11240 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011241 numval = (long)tv_get_number_chk(varp, &error);
11242 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011243 if (!error && strval != NULL)
11244 set_option_value(varname, numval, strval, OPT_LOCAL);
11245
11246 /* reset notion of buffer */
11247 aucmd_restbuf(&aco);
11248 }
11249 else
11250 {
11251 buf_T *save_curbuf = curbuf;
11252
11253 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
11254 if (bufvarname != NULL)
11255 {
11256 curbuf = buf;
11257 STRCPY(bufvarname, "b:");
11258 STRCPY(bufvarname + 2, varname);
11259 set_var(bufvarname, varp, TRUE);
11260 vim_free(bufvarname);
11261 curbuf = save_curbuf;
11262 }
11263 }
11264 }
11265}
11266
11267 static void
11268f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
11269{
11270 dict_T *d;
11271 dictitem_T *di;
11272 char_u *csearch;
11273
11274 if (argvars[0].v_type != VAR_DICT)
11275 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011276 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011277 return;
11278 }
11279
11280 if ((d = argvars[0].vval.v_dict) != NULL)
11281 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010011282 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011283 if (csearch != NULL)
11284 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011285 if (enc_utf8)
11286 {
11287 int pcc[MAX_MCO];
11288 int c = utfc_ptr2char(csearch, pcc);
11289
11290 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
11291 }
11292 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011293 set_last_csearch(PTR2CHAR(csearch),
11294 csearch, MB_PTR2LEN(csearch));
11295 }
11296
11297 di = dict_find(d, (char_u *)"forward", -1);
11298 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011299 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011300 ? FORWARD : BACKWARD);
11301
11302 di = dict_find(d, (char_u *)"until", -1);
11303 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011304 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011305 }
11306}
11307
11308/*
11309 * "setcmdpos()" function
11310 */
11311 static void
11312f_setcmdpos(typval_T *argvars, typval_T *rettv)
11313{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011314 int pos = (int)tv_get_number(&argvars[0]) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011315
11316 if (pos >= 0)
11317 rettv->vval.v_number = set_cmdline_pos(pos);
11318}
11319
11320/*
11321 * "setfperm({fname}, {mode})" function
11322 */
11323 static void
11324f_setfperm(typval_T *argvars, typval_T *rettv)
11325{
11326 char_u *fname;
11327 char_u modebuf[NUMBUFLEN];
11328 char_u *mode_str;
11329 int i;
11330 int mask;
11331 int mode = 0;
11332
11333 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011334 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011335 if (fname == NULL)
11336 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011337 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011338 if (mode_str == NULL)
11339 return;
11340 if (STRLEN(mode_str) != 9)
11341 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011342 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011343 return;
11344 }
11345
11346 mask = 1;
11347 for (i = 8; i >= 0; --i)
11348 {
11349 if (mode_str[i] != '-')
11350 mode |= mask;
11351 mask = mask << 1;
11352 }
11353 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
11354}
11355
11356/*
11357 * "setline()" function
11358 */
11359 static void
11360f_setline(typval_T *argvars, typval_T *rettv)
11361{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011362 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011363
Bram Moolenaarca851592018-06-06 21:04:07 +020011364 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011365}
11366
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011367/*
11368 * Used by "setqflist()" and "setloclist()" functions
11369 */
11370 static void
11371set_qf_ll_list(
11372 win_T *wp UNUSED,
11373 typval_T *list_arg UNUSED,
11374 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020011375 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011376 typval_T *rettv)
11377{
11378#ifdef FEAT_QUICKFIX
11379 static char *e_invact = N_("E927: Invalid action: '%s'");
11380 char_u *act;
11381 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011382 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011383#endif
11384
11385 rettv->vval.v_number = -1;
11386
11387#ifdef FEAT_QUICKFIX
11388 if (list_arg->v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011389 emsg(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011390 else if (recursive != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011391 emsg(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011392 else
11393 {
11394 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011395 dict_T *d = NULL;
11396 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011397
11398 if (action_arg->v_type == VAR_STRING)
11399 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011400 act = tv_get_string_chk(action_arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011401 if (act == NULL)
11402 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020011403 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
11404 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011405 action = *act;
11406 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011407 semsg(_(e_invact), act);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011408 }
11409 else if (action_arg->v_type == VAR_UNKNOWN)
11410 action = ' ';
11411 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011412 emsg(_(e_stringreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011413
Bram Moolenaard823fa92016-08-12 16:29:27 +020011414 if (action_arg->v_type != VAR_UNKNOWN
11415 && what_arg->v_type != VAR_UNKNOWN)
11416 {
11417 if (what_arg->v_type == VAR_DICT)
11418 d = what_arg->vval.v_dict;
11419 else
11420 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011421 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +020011422 valid_dict = FALSE;
11423 }
11424 }
11425
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011426 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011427 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011428 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
11429 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011430 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011431 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011432 }
11433#endif
11434}
11435
11436/*
11437 * "setloclist()" function
11438 */
11439 static void
11440f_setloclist(typval_T *argvars, typval_T *rettv)
11441{
11442 win_T *win;
11443
11444 rettv->vval.v_number = -1;
11445
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020011446 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011447 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020011448 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011449}
11450
11451/*
11452 * "setmatches()" function
11453 */
11454 static void
11455f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
11456{
11457#ifdef FEAT_SEARCH_EXTRA
11458 list_T *l;
11459 listitem_T *li;
11460 dict_T *d;
11461 list_T *s = NULL;
Bram Moolenaaraff74912019-03-30 18:11:49 +010011462 win_T *win = get_optional_window(argvars, 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011463
11464 rettv->vval.v_number = -1;
11465 if (argvars[0].v_type != VAR_LIST)
11466 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011467 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011468 return;
11469 }
Bram Moolenaaraff74912019-03-30 18:11:49 +010011470 if (win == NULL)
11471 return;
11472
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011473 if ((l = argvars[0].vval.v_list) != NULL)
11474 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011475 /* To some extent make sure that we are dealing with a list from
11476 * "getmatches()". */
11477 li = l->lv_first;
11478 while (li != NULL)
11479 {
11480 if (li->li_tv.v_type != VAR_DICT
11481 || (d = li->li_tv.vval.v_dict) == NULL)
11482 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011483 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011484 return;
11485 }
11486 if (!(dict_find(d, (char_u *)"group", -1) != NULL
11487 && (dict_find(d, (char_u *)"pattern", -1) != NULL
11488 || dict_find(d, (char_u *)"pos1", -1) != NULL)
11489 && dict_find(d, (char_u *)"priority", -1) != NULL
11490 && dict_find(d, (char_u *)"id", -1) != NULL))
11491 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011492 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011493 return;
11494 }
11495 li = li->li_next;
11496 }
11497
Bram Moolenaaraff74912019-03-30 18:11:49 +010011498 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011499 li = l->lv_first;
11500 while (li != NULL)
11501 {
11502 int i = 0;
11503 char_u buf[5];
11504 dictitem_T *di;
11505 char_u *group;
11506 int priority;
11507 int id;
11508 char_u *conceal;
11509
11510 d = li->li_tv.vval.v_dict;
11511 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
11512 {
11513 if (s == NULL)
11514 {
11515 s = list_alloc();
11516 if (s == NULL)
11517 return;
11518 }
11519
11520 /* match from matchaddpos() */
11521 for (i = 1; i < 9; i++)
11522 {
11523 sprintf((char *)buf, (char *)"pos%d", i);
11524 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
11525 {
11526 if (di->di_tv.v_type != VAR_LIST)
11527 return;
11528
11529 list_append_tv(s, &di->di_tv);
11530 s->lv_refcount++;
11531 }
11532 else
11533 break;
11534 }
11535 }
11536
Bram Moolenaar8f667172018-12-14 15:38:31 +010011537 group = dict_get_string(d, (char_u *)"group", TRUE);
11538 priority = (int)dict_get_number(d, (char_u *)"priority");
11539 id = (int)dict_get_number(d, (char_u *)"id");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011540 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar8f667172018-12-14 15:38:31 +010011541 ? dict_get_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011542 : NULL;
11543 if (i == 0)
11544 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011545 match_add(win, group,
Bram Moolenaar8f667172018-12-14 15:38:31 +010011546 dict_get_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011547 priority, id, NULL, conceal);
11548 }
11549 else
11550 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011551 match_add(win, group, NULL, priority, id, s, conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011552 list_unref(s);
11553 s = NULL;
11554 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020011555 vim_free(group);
11556 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011557
11558 li = li->li_next;
11559 }
11560 rettv->vval.v_number = 0;
11561 }
11562#endif
11563}
11564
11565/*
11566 * "setpos()" function
11567 */
11568 static void
11569f_setpos(typval_T *argvars, typval_T *rettv)
11570{
11571 pos_T pos;
11572 int fnum;
11573 char_u *name;
11574 colnr_T curswant = -1;
11575
11576 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011577 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011578 if (name != NULL)
11579 {
11580 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
11581 {
11582 if (--pos.col < 0)
11583 pos.col = 0;
11584 if (name[0] == '.' && name[1] == NUL)
11585 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011586 /* set cursor; "fnum" is ignored */
11587 curwin->w_cursor = pos;
11588 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011589 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011590 curwin->w_curswant = curswant - 1;
11591 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011592 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011593 check_cursor();
11594 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011595 }
11596 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
11597 {
11598 /* set mark */
11599 if (setmark_pos(name[1], &pos, fnum) == OK)
11600 rettv->vval.v_number = 0;
11601 }
11602 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011603 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011604 }
11605 }
11606}
11607
11608/*
11609 * "setqflist()" function
11610 */
11611 static void
11612f_setqflist(typval_T *argvars, typval_T *rettv)
11613{
Bram Moolenaard823fa92016-08-12 16:29:27 +020011614 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011615}
11616
11617/*
11618 * "setreg()" function
11619 */
11620 static void
11621f_setreg(typval_T *argvars, typval_T *rettv)
11622{
11623 int regname;
11624 char_u *strregname;
11625 char_u *stropt;
11626 char_u *strval;
11627 int append;
11628 char_u yank_type;
11629 long block_len;
11630
11631 block_len = -1;
11632 yank_type = MAUTO;
11633 append = FALSE;
11634
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011635 strregname = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011636 rettv->vval.v_number = 1; /* FAIL is default */
11637
11638 if (strregname == NULL)
11639 return; /* type error; errmsg already given */
11640 regname = *strregname;
11641 if (regname == 0 || regname == '@')
11642 regname = '"';
11643
11644 if (argvars[2].v_type != VAR_UNKNOWN)
11645 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011646 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011647 if (stropt == NULL)
11648 return; /* type error */
11649 for (; *stropt != NUL; ++stropt)
11650 switch (*stropt)
11651 {
11652 case 'a': case 'A': /* append */
11653 append = TRUE;
11654 break;
11655 case 'v': case 'c': /* character-wise selection */
11656 yank_type = MCHAR;
11657 break;
11658 case 'V': case 'l': /* line-wise selection */
11659 yank_type = MLINE;
11660 break;
11661 case 'b': case Ctrl_V: /* block-wise selection */
11662 yank_type = MBLOCK;
11663 if (VIM_ISDIGIT(stropt[1]))
11664 {
11665 ++stropt;
11666 block_len = getdigits(&stropt) - 1;
11667 --stropt;
11668 }
11669 break;
11670 }
11671 }
11672
11673 if (argvars[1].v_type == VAR_LIST)
11674 {
11675 char_u **lstval;
11676 char_u **allocval;
11677 char_u buf[NUMBUFLEN];
11678 char_u **curval;
11679 char_u **curallocval;
11680 list_T *ll = argvars[1].vval.v_list;
11681 listitem_T *li;
11682 int len;
11683
11684 /* If the list is NULL handle like an empty list. */
11685 len = ll == NULL ? 0 : ll->lv_len;
11686
11687 /* First half: use for pointers to result lines; second half: use for
11688 * pointers to allocated copies. */
11689 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
11690 if (lstval == NULL)
11691 return;
11692 curval = lstval;
11693 allocval = lstval + len + 2;
11694 curallocval = allocval;
11695
11696 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
11697 li = li->li_next)
11698 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011699 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011700 if (strval == NULL)
11701 goto free_lstval;
11702 if (strval == buf)
11703 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011704 /* Need to make a copy, next tv_get_string_buf_chk() will
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011705 * overwrite the string. */
11706 strval = vim_strsave(buf);
11707 if (strval == NULL)
11708 goto free_lstval;
11709 *curallocval++ = strval;
11710 }
11711 *curval++ = strval;
11712 }
11713 *curval++ = NULL;
11714
11715 write_reg_contents_lst(regname, lstval, -1,
11716 append, yank_type, block_len);
11717free_lstval:
11718 while (curallocval > allocval)
11719 vim_free(*--curallocval);
11720 vim_free(lstval);
11721 }
11722 else
11723 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011724 strval = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011725 if (strval == NULL)
11726 return;
11727 write_reg_contents_ex(regname, strval, -1,
11728 append, yank_type, block_len);
11729 }
11730 rettv->vval.v_number = 0;
11731}
11732
11733/*
11734 * "settabvar()" function
11735 */
11736 static void
11737f_settabvar(typval_T *argvars, typval_T *rettv)
11738{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011739 tabpage_T *save_curtab;
11740 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011741 char_u *varname, *tabvarname;
11742 typval_T *varp;
11743
11744 rettv->vval.v_number = 0;
11745
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011746 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011747 return;
11748
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011749 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
11750 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011751 varp = &argvars[2];
11752
Bram Moolenaar4033c552017-09-16 20:54:51 +020011753 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011754 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011755 save_curtab = curtab;
11756 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011757
11758 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
11759 if (tabvarname != NULL)
11760 {
11761 STRCPY(tabvarname, "t:");
11762 STRCPY(tabvarname + 2, varname);
11763 set_var(tabvarname, varp, TRUE);
11764 vim_free(tabvarname);
11765 }
11766
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011767 /* Restore current tabpage */
11768 if (valid_tabpage(save_curtab))
11769 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011770 }
11771}
11772
11773/*
11774 * "settabwinvar()" function
11775 */
11776 static void
11777f_settabwinvar(typval_T *argvars, typval_T *rettv)
11778{
11779 setwinvar(argvars, rettv, 1);
11780}
11781
11782/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011783 * "settagstack()" function
11784 */
11785 static void
11786f_settagstack(typval_T *argvars, typval_T *rettv)
11787{
11788 static char *e_invact2 = N_("E962: Invalid action: '%s'");
11789 win_T *wp;
11790 dict_T *d;
11791 int action = 'r';
11792
11793 rettv->vval.v_number = -1;
11794
11795 // first argument: window number or id
11796 wp = find_win_by_nr_or_id(&argvars[0]);
11797 if (wp == NULL)
11798 return;
11799
11800 // second argument: dict with items to set in the tag stack
11801 if (argvars[1].v_type != VAR_DICT)
11802 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011803 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011804 return;
11805 }
11806 d = argvars[1].vval.v_dict;
11807 if (d == NULL)
11808 return;
11809
11810 // third argument: action - 'a' for append and 'r' for replace.
11811 // default is to replace the stack.
11812 if (argvars[2].v_type == VAR_UNKNOWN)
11813 action = 'r';
11814 else if (argvars[2].v_type == VAR_STRING)
11815 {
11816 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011817 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011818 if (actstr == NULL)
11819 return;
11820 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
11821 action = *actstr;
11822 else
11823 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011824 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011825 return;
11826 }
11827 }
11828 else
11829 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011830 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011831 return;
11832 }
11833
11834 if (set_tagstack(wp, d, action) == OK)
11835 rettv->vval.v_number = 0;
11836}
11837
11838/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011839 * "setwinvar()" function
11840 */
11841 static void
11842f_setwinvar(typval_T *argvars, typval_T *rettv)
11843{
11844 setwinvar(argvars, rettv, 0);
11845}
11846
11847#ifdef FEAT_CRYPT
11848/*
11849 * "sha256({string})" function
11850 */
11851 static void
11852f_sha256(typval_T *argvars, typval_T *rettv)
11853{
11854 char_u *p;
11855
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011856 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011857 rettv->vval.v_string = vim_strsave(
11858 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
11859 rettv->v_type = VAR_STRING;
11860}
11861#endif /* FEAT_CRYPT */
11862
11863/*
11864 * "shellescape({string})" function
11865 */
11866 static void
11867f_shellescape(typval_T *argvars, typval_T *rettv)
11868{
Bram Moolenaar20615522017-06-05 18:46:26 +020011869 int do_special = non_zero_arg(&argvars[1]);
11870
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011871 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011872 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011873 rettv->v_type = VAR_STRING;
11874}
11875
11876/*
11877 * shiftwidth() function
11878 */
11879 static void
11880f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
11881{
Bram Moolenaarf9514162018-11-22 03:08:29 +010011882 rettv->vval.v_number = 0;
11883
11884 if (argvars[0].v_type != VAR_UNKNOWN)
11885 {
11886 long col;
11887
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011888 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +010011889 if (col < 0)
11890 return; // type error; errmsg already given
11891#ifdef FEAT_VARTABS
11892 rettv->vval.v_number = get_sw_value_col(curbuf, col);
11893 return;
11894#endif
11895 }
11896
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011897 rettv->vval.v_number = get_sw_value(curbuf);
11898}
11899
Bram Moolenaar162b7142018-12-21 15:17:36 +010011900#ifdef FEAT_SIGNS
11901/*
11902 * "sign_define()" function
11903 */
11904 static void
11905f_sign_define(typval_T *argvars, typval_T *rettv)
11906{
11907 char_u *name;
11908 dict_T *dict;
11909 char_u *icon = NULL;
11910 char_u *linehl = NULL;
11911 char_u *text = NULL;
11912 char_u *texthl = NULL;
11913
11914 rettv->vval.v_number = -1;
11915
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011916 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011917 if (name == NULL)
11918 return;
11919
11920 if (argvars[1].v_type != VAR_UNKNOWN)
11921 {
11922 if (argvars[1].v_type != VAR_DICT)
11923 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011924 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011925 return;
11926 }
11927
11928 // sign attributes
11929 dict = argvars[1].vval.v_dict;
11930 if (dict_find(dict, (char_u *)"icon", -1) != NULL)
11931 icon = dict_get_string(dict, (char_u *)"icon", TRUE);
11932 if (dict_find(dict, (char_u *)"linehl", -1) != NULL)
11933 linehl = dict_get_string(dict, (char_u *)"linehl", TRUE);
11934 if (dict_find(dict, (char_u *)"text", -1) != NULL)
11935 text = dict_get_string(dict, (char_u *)"text", TRUE);
11936 if (dict_find(dict, (char_u *)"texthl", -1) != NULL)
11937 texthl = dict_get_string(dict, (char_u *)"texthl", TRUE);
11938 }
11939
11940 if (sign_define_by_name(name, icon, linehl, text, texthl) == OK)
11941 rettv->vval.v_number = 0;
11942
11943 vim_free(icon);
11944 vim_free(linehl);
11945 vim_free(text);
11946 vim_free(texthl);
11947}
11948
11949/*
11950 * "sign_getdefined()" function
11951 */
11952 static void
11953f_sign_getdefined(typval_T *argvars, typval_T *rettv)
11954{
11955 char_u *name = NULL;
11956
11957 if (rettv_list_alloc_id(rettv, aid_sign_getdefined) != OK)
11958 return;
11959
11960 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011961 name = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011962
11963 sign_getlist(name, rettv->vval.v_list);
11964}
11965
11966/*
11967 * "sign_getplaced()" function
11968 */
11969 static void
11970f_sign_getplaced(typval_T *argvars, typval_T *rettv)
11971{
11972 buf_T *buf = NULL;
11973 dict_T *dict;
11974 dictitem_T *di;
11975 linenr_T lnum = 0;
11976 int sign_id = 0;
11977 char_u *group = NULL;
11978 int notanum = FALSE;
11979
11980 if (rettv_list_alloc_id(rettv, aid_sign_getplaced) != OK)
11981 return;
11982
11983 if (argvars[0].v_type != VAR_UNKNOWN)
11984 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010011985 // get signs placed in the specified buffer
11986 buf = get_buf_arg(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011987 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010011988 return;
Bram Moolenaar162b7142018-12-21 15:17:36 +010011989
11990 if (argvars[1].v_type != VAR_UNKNOWN)
11991 {
11992 if (argvars[1].v_type != VAR_DICT ||
11993 ((dict = argvars[1].vval.v_dict) == NULL))
11994 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011995 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011996 return;
11997 }
11998 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
11999 {
12000 // get signs placed at this line
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012001 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012002 if (notanum)
12003 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012004 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012005 }
12006 if ((di = dict_find(dict, (char_u *)"id", -1)) != NULL)
12007 {
12008 // get sign placed with this identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012009 sign_id = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012010 if (notanum)
12011 return;
12012 }
12013 if ((di = dict_find(dict, (char_u *)"group", -1)) != NULL)
12014 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012015 group = tv_get_string_chk(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012016 if (group == NULL)
12017 return;
Bram Moolenaar6436cd82018-12-27 00:28:33 +010012018 if (*group == '\0') // empty string means global group
12019 group = NULL;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012020 }
12021 }
12022 }
12023
12024 sign_get_placed(buf, lnum, sign_id, group, rettv->vval.v_list);
12025}
12026
12027/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012028 * "sign_jump()" function
12029 */
12030 static void
12031f_sign_jump(typval_T *argvars, typval_T *rettv)
12032{
12033 int sign_id;
12034 char_u *sign_group = NULL;
12035 buf_T *buf;
12036 int notanum = FALSE;
12037
12038 rettv->vval.v_number = -1;
12039
Bram Moolenaarbdace832019-03-02 10:13:42 +010012040 // Sign identifier
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012041 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
12042 if (notanum)
12043 return;
12044 if (sign_id <= 0)
12045 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012046 emsg(_(e_invarg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012047 return;
12048 }
12049
12050 // Sign group
12051 sign_group = tv_get_string_chk(&argvars[1]);
12052 if (sign_group == NULL)
12053 return;
12054 if (sign_group[0] == '\0')
12055 sign_group = NULL; // global sign group
12056 else
12057 {
12058 sign_group = vim_strsave(sign_group);
12059 if (sign_group == NULL)
12060 return;
12061 }
12062
12063 // Buffer to place the sign
12064 buf = get_buf_arg(&argvars[2]);
12065 if (buf == NULL)
12066 goto cleanup;
12067
12068 rettv->vval.v_number = sign_jump(sign_id, sign_group, buf);
12069
12070cleanup:
12071 vim_free(sign_group);
12072}
12073
12074/*
Bram Moolenaar162b7142018-12-21 15:17:36 +010012075 * "sign_place()" function
12076 */
12077 static void
12078f_sign_place(typval_T *argvars, typval_T *rettv)
12079{
12080 int sign_id;
12081 char_u *group = NULL;
12082 char_u *sign_name;
12083 buf_T *buf;
12084 dict_T *dict;
12085 dictitem_T *di;
12086 linenr_T lnum = 0;
12087 int prio = SIGN_DEF_PRIO;
12088 int notanum = FALSE;
12089
12090 rettv->vval.v_number = -1;
12091
Bram Moolenaarbdace832019-03-02 10:13:42 +010012092 // Sign identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012093 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012094 if (notanum)
12095 return;
12096 if (sign_id < 0)
12097 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012098 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012099 return;
12100 }
12101
12102 // Sign group
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012103 group = tv_get_string_chk(&argvars[1]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012104 if (group == NULL)
12105 return;
12106 if (group[0] == '\0')
12107 group = NULL; // global sign group
12108 else
12109 {
12110 group = vim_strsave(group);
12111 if (group == NULL)
12112 return;
12113 }
12114
12115 // Sign name
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012116 sign_name = tv_get_string_chk(&argvars[2]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012117 if (sign_name == NULL)
12118 goto cleanup;
12119
12120 // Buffer to place the sign
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012121 buf = get_buf_arg(&argvars[3]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012122 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012123 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012124
12125 if (argvars[4].v_type != VAR_UNKNOWN)
12126 {
12127 if (argvars[4].v_type != VAR_DICT ||
12128 ((dict = argvars[4].vval.v_dict) == NULL))
12129 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012130 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012131 goto cleanup;
12132 }
12133
12134 // Line number where the sign is to be placed
12135 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
12136 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012137 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012138 if (notanum)
12139 goto cleanup;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012140 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012141 }
12142 if ((di = dict_find(dict, (char_u *)"priority", -1)) != NULL)
12143 {
12144 // Sign priority
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012145 prio = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012146 if (notanum)
12147 goto cleanup;
12148 }
12149 }
12150
12151 if (sign_place(&sign_id, group, sign_name, buf, lnum, prio) == OK)
12152 rettv->vval.v_number = sign_id;
12153
12154cleanup:
12155 vim_free(group);
12156}
12157
12158/*
12159 * "sign_undefine()" function
12160 */
12161 static void
12162f_sign_undefine(typval_T *argvars, typval_T *rettv)
12163{
12164 char_u *name;
12165
12166 rettv->vval.v_number = -1;
12167
12168 if (argvars[0].v_type == VAR_UNKNOWN)
12169 {
12170 // Free all the signs
12171 free_signs();
12172 rettv->vval.v_number = 0;
12173 }
12174 else
12175 {
12176 // Free only the specified sign
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012177 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012178 if (name == NULL)
12179 return;
12180
12181 if (sign_undefine_by_name(name) == OK)
12182 rettv->vval.v_number = 0;
12183 }
12184}
12185
12186/*
12187 * "sign_unplace()" function
12188 */
12189 static void
12190f_sign_unplace(typval_T *argvars, typval_T *rettv)
12191{
12192 dict_T *dict;
12193 dictitem_T *di;
12194 int sign_id = 0;
12195 buf_T *buf = NULL;
12196 char_u *group = NULL;
12197
12198 rettv->vval.v_number = -1;
12199
12200 if (argvars[0].v_type != VAR_STRING)
12201 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012202 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012203 return;
12204 }
12205
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012206 group = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012207 if (group[0] == '\0')
12208 group = NULL; // global sign group
12209 else
12210 {
12211 group = vim_strsave(group);
12212 if (group == NULL)
12213 return;
12214 }
12215
12216 if (argvars[1].v_type != VAR_UNKNOWN)
12217 {
12218 if (argvars[1].v_type != VAR_DICT)
12219 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012220 emsg(_(e_dictreq));
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012221 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012222 }
12223 dict = argvars[1].vval.v_dict;
12224
12225 if ((di = dict_find(dict, (char_u *)"buffer", -1)) != NULL)
12226 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012227 buf = get_buf_arg(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012228 if (buf == NULL)
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012229 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012230 }
12231 if (dict_find(dict, (char_u *)"id", -1) != NULL)
12232 sign_id = dict_get_number(dict, (char_u *)"id");
12233 }
12234
12235 if (buf == NULL)
12236 {
12237 // Delete the sign in all the buffers
12238 FOR_ALL_BUFFERS(buf)
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010012239 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012240 rettv->vval.v_number = 0;
12241 }
12242 else
12243 {
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010012244 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012245 rettv->vval.v_number = 0;
12246 }
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012247
12248cleanup:
Bram Moolenaar162b7142018-12-21 15:17:36 +010012249 vim_free(group);
12250}
12251#endif
12252
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012253/*
12254 * "simplify()" function
12255 */
12256 static void
12257f_simplify(typval_T *argvars, typval_T *rettv)
12258{
12259 char_u *p;
12260
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012261 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012262 rettv->vval.v_string = vim_strsave(p);
12263 simplify_filename(rettv->vval.v_string); /* simplify in place */
12264 rettv->v_type = VAR_STRING;
12265}
12266
12267#ifdef FEAT_FLOAT
12268/*
12269 * "sin()" function
12270 */
12271 static void
12272f_sin(typval_T *argvars, typval_T *rettv)
12273{
12274 float_T f = 0.0;
12275
12276 rettv->v_type = VAR_FLOAT;
12277 if (get_float_arg(argvars, &f) == OK)
12278 rettv->vval.v_float = sin(f);
12279 else
12280 rettv->vval.v_float = 0.0;
12281}
12282
12283/*
12284 * "sinh()" function
12285 */
12286 static void
12287f_sinh(typval_T *argvars, typval_T *rettv)
12288{
12289 float_T f = 0.0;
12290
12291 rettv->v_type = VAR_FLOAT;
12292 if (get_float_arg(argvars, &f) == OK)
12293 rettv->vval.v_float = sinh(f);
12294 else
12295 rettv->vval.v_float = 0.0;
12296}
12297#endif
12298
12299static int
12300#ifdef __BORLANDC__
12301 _RTLENTRYF
12302#endif
12303 item_compare(const void *s1, const void *s2);
12304static int
12305#ifdef __BORLANDC__
12306 _RTLENTRYF
12307#endif
12308 item_compare2(const void *s1, const void *s2);
12309
12310/* struct used in the array that's given to qsort() */
12311typedef struct
12312{
12313 listitem_T *item;
12314 int idx;
12315} sortItem_T;
12316
12317/* struct storing information about current sort */
12318typedef struct
12319{
12320 int item_compare_ic;
12321 int item_compare_numeric;
12322 int item_compare_numbers;
12323#ifdef FEAT_FLOAT
12324 int item_compare_float;
12325#endif
12326 char_u *item_compare_func;
12327 partial_T *item_compare_partial;
12328 dict_T *item_compare_selfdict;
12329 int item_compare_func_err;
12330 int item_compare_keep_zero;
12331} sortinfo_T;
12332static sortinfo_T *sortinfo = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012333#define ITEM_COMPARE_FAIL 999
12334
12335/*
12336 * Compare functions for f_sort() and f_uniq() below.
12337 */
12338 static int
12339#ifdef __BORLANDC__
12340_RTLENTRYF
12341#endif
12342item_compare(const void *s1, const void *s2)
12343{
12344 sortItem_T *si1, *si2;
12345 typval_T *tv1, *tv2;
12346 char_u *p1, *p2;
12347 char_u *tofree1 = NULL, *tofree2 = NULL;
12348 int res;
12349 char_u numbuf1[NUMBUFLEN];
12350 char_u numbuf2[NUMBUFLEN];
12351
12352 si1 = (sortItem_T *)s1;
12353 si2 = (sortItem_T *)s2;
12354 tv1 = &si1->item->li_tv;
12355 tv2 = &si2->item->li_tv;
12356
12357 if (sortinfo->item_compare_numbers)
12358 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012359 varnumber_T v1 = tv_get_number(tv1);
12360 varnumber_T v2 = tv_get_number(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012361
12362 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
12363 }
12364
12365#ifdef FEAT_FLOAT
12366 if (sortinfo->item_compare_float)
12367 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012368 float_T v1 = tv_get_float(tv1);
12369 float_T v2 = tv_get_float(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012370
12371 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
12372 }
12373#endif
12374
12375 /* tv2string() puts quotes around a string and allocates memory. Don't do
12376 * that for string variables. Use a single quote when comparing with a
12377 * non-string to do what the docs promise. */
12378 if (tv1->v_type == VAR_STRING)
12379 {
12380 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12381 p1 = (char_u *)"'";
12382 else
12383 p1 = tv1->vval.v_string;
12384 }
12385 else
12386 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
12387 if (tv2->v_type == VAR_STRING)
12388 {
12389 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12390 p2 = (char_u *)"'";
12391 else
12392 p2 = tv2->vval.v_string;
12393 }
12394 else
12395 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
12396 if (p1 == NULL)
12397 p1 = (char_u *)"";
12398 if (p2 == NULL)
12399 p2 = (char_u *)"";
12400 if (!sortinfo->item_compare_numeric)
12401 {
12402 if (sortinfo->item_compare_ic)
12403 res = STRICMP(p1, p2);
12404 else
12405 res = STRCMP(p1, p2);
12406 }
12407 else
12408 {
12409 double n1, n2;
12410 n1 = strtod((char *)p1, (char **)&p1);
12411 n2 = strtod((char *)p2, (char **)&p2);
12412 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
12413 }
12414
12415 /* When the result would be zero, compare the item indexes. Makes the
12416 * sort stable. */
12417 if (res == 0 && !sortinfo->item_compare_keep_zero)
12418 res = si1->idx > si2->idx ? 1 : -1;
12419
12420 vim_free(tofree1);
12421 vim_free(tofree2);
12422 return res;
12423}
12424
12425 static int
12426#ifdef __BORLANDC__
12427_RTLENTRYF
12428#endif
12429item_compare2(const void *s1, const void *s2)
12430{
12431 sortItem_T *si1, *si2;
12432 int res;
12433 typval_T rettv;
12434 typval_T argv[3];
12435 int dummy;
12436 char_u *func_name;
12437 partial_T *partial = sortinfo->item_compare_partial;
12438
12439 /* shortcut after failure in previous call; compare all items equal */
12440 if (sortinfo->item_compare_func_err)
12441 return 0;
12442
12443 si1 = (sortItem_T *)s1;
12444 si2 = (sortItem_T *)s2;
12445
12446 if (partial == NULL)
12447 func_name = sortinfo->item_compare_func;
12448 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012449 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012450
12451 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
12452 * in the copy without changing the original list items. */
12453 copy_tv(&si1->item->li_tv, &argv[0]);
12454 copy_tv(&si2->item->li_tv, &argv[1]);
12455
12456 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
12457 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020012458 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012459 partial, sortinfo->item_compare_selfdict);
12460 clear_tv(&argv[0]);
12461 clear_tv(&argv[1]);
12462
12463 if (res == FAIL)
12464 res = ITEM_COMPARE_FAIL;
12465 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012466 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012467 if (sortinfo->item_compare_func_err)
12468 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
12469 clear_tv(&rettv);
12470
12471 /* When the result would be zero, compare the pointers themselves. Makes
12472 * the sort stable. */
12473 if (res == 0 && !sortinfo->item_compare_keep_zero)
12474 res = si1->idx > si2->idx ? 1 : -1;
12475
12476 return res;
12477}
12478
12479/*
12480 * "sort({list})" function
12481 */
12482 static void
12483do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
12484{
12485 list_T *l;
12486 listitem_T *li;
12487 sortItem_T *ptrs;
12488 sortinfo_T *old_sortinfo;
12489 sortinfo_T info;
12490 long len;
12491 long i;
12492
12493 /* Pointer to current info struct used in compare function. Save and
12494 * restore the current one for nested calls. */
12495 old_sortinfo = sortinfo;
12496 sortinfo = &info;
12497
12498 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012499 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012500 else
12501 {
12502 l = argvars[0].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +010012503 if (l == NULL || var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012504 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
12505 TRUE))
12506 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012507 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012508
12509 len = list_len(l);
12510 if (len <= 1)
12511 goto theend; /* short list sorts pretty quickly */
12512
12513 info.item_compare_ic = FALSE;
12514 info.item_compare_numeric = FALSE;
12515 info.item_compare_numbers = FALSE;
12516#ifdef FEAT_FLOAT
12517 info.item_compare_float = FALSE;
12518#endif
12519 info.item_compare_func = NULL;
12520 info.item_compare_partial = NULL;
12521 info.item_compare_selfdict = NULL;
12522 if (argvars[1].v_type != VAR_UNKNOWN)
12523 {
12524 /* optional second argument: {func} */
12525 if (argvars[1].v_type == VAR_FUNC)
12526 info.item_compare_func = argvars[1].vval.v_string;
12527 else if (argvars[1].v_type == VAR_PARTIAL)
12528 info.item_compare_partial = argvars[1].vval.v_partial;
12529 else
12530 {
12531 int error = FALSE;
12532
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012533 i = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012534 if (error)
12535 goto theend; /* type error; errmsg already given */
12536 if (i == 1)
12537 info.item_compare_ic = TRUE;
12538 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012539 info.item_compare_func = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012540 else if (i != 0)
12541 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012542 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012543 goto theend;
12544 }
12545 if (info.item_compare_func != NULL)
12546 {
12547 if (*info.item_compare_func == NUL)
12548 {
12549 /* empty string means default sort */
12550 info.item_compare_func = NULL;
12551 }
12552 else if (STRCMP(info.item_compare_func, "n") == 0)
12553 {
12554 info.item_compare_func = NULL;
12555 info.item_compare_numeric = TRUE;
12556 }
12557 else if (STRCMP(info.item_compare_func, "N") == 0)
12558 {
12559 info.item_compare_func = NULL;
12560 info.item_compare_numbers = TRUE;
12561 }
12562#ifdef FEAT_FLOAT
12563 else if (STRCMP(info.item_compare_func, "f") == 0)
12564 {
12565 info.item_compare_func = NULL;
12566 info.item_compare_float = TRUE;
12567 }
12568#endif
12569 else if (STRCMP(info.item_compare_func, "i") == 0)
12570 {
12571 info.item_compare_func = NULL;
12572 info.item_compare_ic = TRUE;
12573 }
12574 }
12575 }
12576
12577 if (argvars[2].v_type != VAR_UNKNOWN)
12578 {
12579 /* optional third argument: {dict} */
12580 if (argvars[2].v_type != VAR_DICT)
12581 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012582 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012583 goto theend;
12584 }
12585 info.item_compare_selfdict = argvars[2].vval.v_dict;
12586 }
12587 }
12588
12589 /* Make an array with each entry pointing to an item in the List. */
12590 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
12591 if (ptrs == NULL)
12592 goto theend;
12593
12594 i = 0;
12595 if (sort)
12596 {
12597 /* sort(): ptrs will be the list to sort */
12598 for (li = l->lv_first; li != NULL; li = li->li_next)
12599 {
12600 ptrs[i].item = li;
12601 ptrs[i].idx = i;
12602 ++i;
12603 }
12604
12605 info.item_compare_func_err = FALSE;
12606 info.item_compare_keep_zero = FALSE;
12607 /* test the compare function */
12608 if ((info.item_compare_func != NULL
12609 || info.item_compare_partial != NULL)
12610 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
12611 == ITEM_COMPARE_FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012612 emsg(_("E702: Sort compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012613 else
12614 {
12615 /* Sort the array with item pointers. */
12616 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
12617 info.item_compare_func == NULL
12618 && info.item_compare_partial == NULL
12619 ? item_compare : item_compare2);
12620
12621 if (!info.item_compare_func_err)
12622 {
12623 /* Clear the List and append the items in sorted order. */
12624 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
12625 l->lv_len = 0;
12626 for (i = 0; i < len; ++i)
12627 list_append(l, ptrs[i].item);
12628 }
12629 }
12630 }
12631 else
12632 {
12633 int (*item_compare_func_ptr)(const void *, const void *);
12634
12635 /* f_uniq(): ptrs will be a stack of items to remove */
12636 info.item_compare_func_err = FALSE;
12637 info.item_compare_keep_zero = TRUE;
12638 item_compare_func_ptr = info.item_compare_func != NULL
12639 || info.item_compare_partial != NULL
12640 ? item_compare2 : item_compare;
12641
12642 for (li = l->lv_first; li != NULL && li->li_next != NULL;
12643 li = li->li_next)
12644 {
12645 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
12646 == 0)
12647 ptrs[i++].item = li;
12648 if (info.item_compare_func_err)
12649 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012650 emsg(_("E882: Uniq compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012651 break;
12652 }
12653 }
12654
12655 if (!info.item_compare_func_err)
12656 {
12657 while (--i >= 0)
12658 {
12659 li = ptrs[i].item->li_next;
12660 ptrs[i].item->li_next = li->li_next;
12661 if (li->li_next != NULL)
12662 li->li_next->li_prev = ptrs[i].item;
12663 else
12664 l->lv_last = ptrs[i].item;
12665 list_fix_watch(l, li);
12666 listitem_free(li);
12667 l->lv_len--;
12668 }
12669 }
12670 }
12671
12672 vim_free(ptrs);
12673 }
12674theend:
12675 sortinfo = old_sortinfo;
12676}
12677
12678/*
12679 * "sort({list})" function
12680 */
12681 static void
12682f_sort(typval_T *argvars, typval_T *rettv)
12683{
12684 do_sort_uniq(argvars, rettv, TRUE);
12685}
12686
12687/*
12688 * "uniq({list})" function
12689 */
12690 static void
12691f_uniq(typval_T *argvars, typval_T *rettv)
12692{
12693 do_sort_uniq(argvars, rettv, FALSE);
12694}
12695
12696/*
12697 * "soundfold({word})" function
12698 */
12699 static void
12700f_soundfold(typval_T *argvars, typval_T *rettv)
12701{
12702 char_u *s;
12703
12704 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012705 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012706#ifdef FEAT_SPELL
12707 rettv->vval.v_string = eval_soundfold(s);
12708#else
12709 rettv->vval.v_string = vim_strsave(s);
12710#endif
12711}
12712
12713/*
12714 * "spellbadword()" function
12715 */
12716 static void
12717f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
12718{
12719 char_u *word = (char_u *)"";
12720 hlf_T attr = HLF_COUNT;
12721 int len = 0;
12722
12723 if (rettv_list_alloc(rettv) == FAIL)
12724 return;
12725
12726#ifdef FEAT_SPELL
12727 if (argvars[0].v_type == VAR_UNKNOWN)
12728 {
12729 /* Find the start and length of the badly spelled word. */
12730 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
12731 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012732 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012733 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012734 curwin->w_set_curswant = TRUE;
12735 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012736 }
12737 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
12738 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012739 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012740 int capcol = -1;
12741
12742 if (str != NULL)
12743 {
12744 /* Check the argument for spelling. */
12745 while (*str != NUL)
12746 {
12747 len = spell_check(curwin, str, &attr, &capcol, FALSE);
12748 if (attr != HLF_COUNT)
12749 {
12750 word = str;
12751 break;
12752 }
12753 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020012754 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012755 }
12756 }
12757 }
12758#endif
12759
12760 list_append_string(rettv->vval.v_list, word, len);
12761 list_append_string(rettv->vval.v_list, (char_u *)(
12762 attr == HLF_SPB ? "bad" :
12763 attr == HLF_SPR ? "rare" :
12764 attr == HLF_SPL ? "local" :
12765 attr == HLF_SPC ? "caps" :
12766 ""), -1);
12767}
12768
12769/*
12770 * "spellsuggest()" function
12771 */
12772 static void
12773f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
12774{
12775#ifdef FEAT_SPELL
12776 char_u *str;
12777 int typeerr = FALSE;
12778 int maxcount;
12779 garray_T ga;
12780 int i;
12781 listitem_T *li;
12782 int need_capital = FALSE;
12783#endif
12784
12785 if (rettv_list_alloc(rettv) == FAIL)
12786 return;
12787
12788#ifdef FEAT_SPELL
12789 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
12790 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012791 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012792 if (argvars[1].v_type != VAR_UNKNOWN)
12793 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012794 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012795 if (maxcount <= 0)
12796 return;
12797 if (argvars[2].v_type != VAR_UNKNOWN)
12798 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012799 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012800 if (typeerr)
12801 return;
12802 }
12803 }
12804 else
12805 maxcount = 25;
12806
12807 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
12808
12809 for (i = 0; i < ga.ga_len; ++i)
12810 {
12811 str = ((char_u **)ga.ga_data)[i];
12812
12813 li = listitem_alloc();
12814 if (li == NULL)
12815 vim_free(str);
12816 else
12817 {
12818 li->li_tv.v_type = VAR_STRING;
12819 li->li_tv.v_lock = 0;
12820 li->li_tv.vval.v_string = str;
12821 list_append(rettv->vval.v_list, li);
12822 }
12823 }
12824 ga_clear(&ga);
12825 }
12826#endif
12827}
12828
12829 static void
12830f_split(typval_T *argvars, typval_T *rettv)
12831{
12832 char_u *str;
12833 char_u *end;
12834 char_u *pat = NULL;
12835 regmatch_T regmatch;
12836 char_u patbuf[NUMBUFLEN];
12837 char_u *save_cpo;
12838 int match;
12839 colnr_T col = 0;
12840 int keepempty = FALSE;
12841 int typeerr = FALSE;
12842
12843 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12844 save_cpo = p_cpo;
12845 p_cpo = (char_u *)"";
12846
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012847 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012848 if (argvars[1].v_type != VAR_UNKNOWN)
12849 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012850 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012851 if (pat == NULL)
12852 typeerr = TRUE;
12853 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012854 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012855 }
12856 if (pat == NULL || *pat == NUL)
12857 pat = (char_u *)"[\\x01- ]\\+";
12858
12859 if (rettv_list_alloc(rettv) == FAIL)
12860 return;
12861 if (typeerr)
12862 return;
12863
12864 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12865 if (regmatch.regprog != NULL)
12866 {
12867 regmatch.rm_ic = FALSE;
12868 while (*str != NUL || keepempty)
12869 {
12870 if (*str == NUL)
12871 match = FALSE; /* empty item at the end */
12872 else
12873 match = vim_regexec_nl(&regmatch, str, col);
12874 if (match)
12875 end = regmatch.startp[0];
12876 else
12877 end = str + STRLEN(str);
12878 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
12879 && *str != NUL && match && end < regmatch.endp[0]))
12880 {
12881 if (list_append_string(rettv->vval.v_list, str,
12882 (int)(end - str)) == FAIL)
12883 break;
12884 }
12885 if (!match)
12886 break;
Bram Moolenaar13505972019-01-24 15:04:48 +010012887 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012888 if (regmatch.endp[0] > str)
12889 col = 0;
12890 else
Bram Moolenaar13505972019-01-24 15:04:48 +010012891 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012892 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012893 str = regmatch.endp[0];
12894 }
12895
12896 vim_regfree(regmatch.regprog);
12897 }
12898
12899 p_cpo = save_cpo;
12900}
12901
12902#ifdef FEAT_FLOAT
12903/*
12904 * "sqrt()" function
12905 */
12906 static void
12907f_sqrt(typval_T *argvars, typval_T *rettv)
12908{
12909 float_T f = 0.0;
12910
12911 rettv->v_type = VAR_FLOAT;
12912 if (get_float_arg(argvars, &f) == OK)
12913 rettv->vval.v_float = sqrt(f);
12914 else
12915 rettv->vval.v_float = 0.0;
12916}
12917
12918/*
12919 * "str2float()" function
12920 */
12921 static void
12922f_str2float(typval_T *argvars, typval_T *rettv)
12923{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012924 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010012925 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012926
Bram Moolenaar08243d22017-01-10 16:12:29 +010012927 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012928 p = skipwhite(p + 1);
12929 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010012930 if (isneg)
12931 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012932 rettv->v_type = VAR_FLOAT;
12933}
12934#endif
12935
12936/*
Bram Moolenaar9d401282019-04-06 13:18:12 +020012937 * "str2list()" function
12938 */
12939 static void
12940f_str2list(typval_T *argvars, typval_T *rettv)
12941{
12942 char_u *p;
12943 int utf8 = FALSE;
12944
12945 if (rettv_list_alloc(rettv) == FAIL)
12946 return;
12947
12948 if (argvars[1].v_type != VAR_UNKNOWN)
12949 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
12950
12951 p = tv_get_string(&argvars[0]);
12952
12953 if (has_mbyte || utf8)
12954 {
12955 int (*ptr2len)(char_u *);
12956 int (*ptr2char)(char_u *);
12957
12958 if (utf8 || enc_utf8)
12959 {
12960 ptr2len = utf_ptr2len;
12961 ptr2char = utf_ptr2char;
12962 }
12963 else
12964 {
12965 ptr2len = mb_ptr2len;
12966 ptr2char = mb_ptr2char;
12967 }
12968
12969 for ( ; *p != NUL; p += (*ptr2len)(p))
12970 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
12971 }
12972 else
12973 for ( ; *p != NUL; ++p)
12974 list_append_number(rettv->vval.v_list, *p);
12975}
12976
12977/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012978 * "str2nr()" function
12979 */
12980 static void
12981f_str2nr(typval_T *argvars, typval_T *rettv)
12982{
12983 int base = 10;
12984 char_u *p;
12985 varnumber_T n;
12986 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010012987 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012988
12989 if (argvars[1].v_type != VAR_UNKNOWN)
12990 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012991 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012992 if (base != 2 && base != 8 && base != 10 && base != 16)
12993 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012994 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012995 return;
12996 }
12997 }
12998
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012999 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010013000 isneg = (*p == '-');
13001 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013002 p = skipwhite(p + 1);
13003 switch (base)
13004 {
13005 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
13006 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
13007 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
13008 default: what = 0;
13009 }
13010 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010013011 if (isneg)
13012 rettv->vval.v_number = -n;
13013 else
13014 rettv->vval.v_number = n;
13015
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013016}
13017
13018#ifdef HAVE_STRFTIME
13019/*
13020 * "strftime({format}[, {time}])" function
13021 */
13022 static void
13023f_strftime(typval_T *argvars, typval_T *rettv)
13024{
13025 char_u result_buf[256];
13026 struct tm *curtime;
13027 time_t seconds;
13028 char_u *p;
13029
13030 rettv->v_type = VAR_STRING;
13031
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013032 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013033 if (argvars[1].v_type == VAR_UNKNOWN)
13034 seconds = time(NULL);
13035 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013036 seconds = (time_t)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013037 curtime = localtime(&seconds);
13038 /* MSVC returns NULL for an invalid value of seconds. */
13039 if (curtime == NULL)
13040 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
13041 else
13042 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013043 vimconv_T conv;
13044 char_u *enc;
13045
13046 conv.vc_type = CONV_NONE;
13047 enc = enc_locale();
13048 convert_setup(&conv, p_enc, enc);
13049 if (conv.vc_type != CONV_NONE)
13050 p = string_convert(&conv, p, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013051 if (p != NULL)
13052 (void)strftime((char *)result_buf, sizeof(result_buf),
13053 (char *)p, curtime);
13054 else
13055 result_buf[0] = NUL;
13056
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013057 if (conv.vc_type != CONV_NONE)
13058 vim_free(p);
13059 convert_setup(&conv, enc, p_enc);
13060 if (conv.vc_type != CONV_NONE)
13061 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
13062 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013063 rettv->vval.v_string = vim_strsave(result_buf);
13064
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013065 /* Release conversion descriptors */
13066 convert_setup(&conv, NULL, NULL);
13067 vim_free(enc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013068 }
13069}
13070#endif
13071
13072/*
13073 * "strgetchar()" function
13074 */
13075 static void
13076f_strgetchar(typval_T *argvars, typval_T *rettv)
13077{
13078 char_u *str;
13079 int len;
13080 int error = FALSE;
13081 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +010013082 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013083
13084 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013085 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013086 if (str == NULL)
13087 return;
13088 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013089 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013090 if (error)
13091 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013092
Bram Moolenaar13505972019-01-24 15:04:48 +010013093 while (charidx >= 0 && byteidx < len)
13094 {
13095 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013096 {
Bram Moolenaar13505972019-01-24 15:04:48 +010013097 rettv->vval.v_number = mb_ptr2char(str + byteidx);
13098 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013099 }
Bram Moolenaar13505972019-01-24 15:04:48 +010013100 --charidx;
13101 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013102 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013103}
13104
13105/*
13106 * "stridx()" function
13107 */
13108 static void
13109f_stridx(typval_T *argvars, typval_T *rettv)
13110{
13111 char_u buf[NUMBUFLEN];
13112 char_u *needle;
13113 char_u *haystack;
13114 char_u *save_haystack;
13115 char_u *pos;
13116 int start_idx;
13117
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013118 needle = tv_get_string_chk(&argvars[1]);
13119 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013120 rettv->vval.v_number = -1;
13121 if (needle == NULL || haystack == NULL)
13122 return; /* type error; errmsg already given */
13123
13124 if (argvars[2].v_type != VAR_UNKNOWN)
13125 {
13126 int error = FALSE;
13127
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013128 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013129 if (error || start_idx >= (int)STRLEN(haystack))
13130 return;
13131 if (start_idx >= 0)
13132 haystack += start_idx;
13133 }
13134
13135 pos = (char_u *)strstr((char *)haystack, (char *)needle);
13136 if (pos != NULL)
13137 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
13138}
13139
13140/*
13141 * "string()" function
13142 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010013143 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013144f_string(typval_T *argvars, typval_T *rettv)
13145{
13146 char_u *tofree;
13147 char_u numbuf[NUMBUFLEN];
13148
13149 rettv->v_type = VAR_STRING;
13150 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
13151 get_copyID());
13152 /* Make a copy if we have a value but it's not in allocated memory. */
13153 if (rettv->vval.v_string != NULL && tofree == NULL)
13154 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
13155}
13156
13157/*
13158 * "strlen()" function
13159 */
13160 static void
13161f_strlen(typval_T *argvars, typval_T *rettv)
13162{
13163 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013164 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013165}
13166
13167/*
13168 * "strchars()" function
13169 */
13170 static void
13171f_strchars(typval_T *argvars, typval_T *rettv)
13172{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013173 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013174 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013175 varnumber_T len = 0;
13176 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013177
13178 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013179 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013180 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013181 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013182 else
13183 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013184 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
13185 while (*s != NUL)
13186 {
13187 func_mb_ptr2char_adv(&s);
13188 ++len;
13189 }
13190 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013191 }
13192}
13193
13194/*
13195 * "strdisplaywidth()" function
13196 */
13197 static void
13198f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
13199{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013200 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013201 int col = 0;
13202
13203 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013204 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013205
13206 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
13207}
13208
13209/*
13210 * "strwidth()" function
13211 */
13212 static void
13213f_strwidth(typval_T *argvars, typval_T *rettv)
13214{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013215 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013216
Bram Moolenaar13505972019-01-24 15:04:48 +010013217 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013218}
13219
13220/*
13221 * "strcharpart()" function
13222 */
13223 static void
13224f_strcharpart(typval_T *argvars, typval_T *rettv)
13225{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013226 char_u *p;
13227 int nchar;
13228 int nbyte = 0;
13229 int charlen;
13230 int len = 0;
13231 int slen;
13232 int error = FALSE;
13233
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013234 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013235 slen = (int)STRLEN(p);
13236
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013237 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013238 if (!error)
13239 {
13240 if (nchar > 0)
13241 while (nchar > 0 && nbyte < slen)
13242 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020013243 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013244 --nchar;
13245 }
13246 else
13247 nbyte = nchar;
13248 if (argvars[2].v_type != VAR_UNKNOWN)
13249 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013250 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013251 while (charlen > 0 && nbyte + len < slen)
13252 {
13253 int off = nbyte + len;
13254
13255 if (off < 0)
13256 len += 1;
13257 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020013258 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013259 --charlen;
13260 }
13261 }
13262 else
13263 len = slen - nbyte; /* default: all bytes that are available. */
13264 }
13265
13266 /*
13267 * Only return the overlap between the specified part and the actual
13268 * string.
13269 */
13270 if (nbyte < 0)
13271 {
13272 len += nbyte;
13273 nbyte = 0;
13274 }
13275 else if (nbyte > slen)
13276 nbyte = slen;
13277 if (len < 0)
13278 len = 0;
13279 else if (nbyte + len > slen)
13280 len = slen - nbyte;
13281
13282 rettv->v_type = VAR_STRING;
13283 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013284}
13285
13286/*
13287 * "strpart()" function
13288 */
13289 static void
13290f_strpart(typval_T *argvars, typval_T *rettv)
13291{
13292 char_u *p;
13293 int n;
13294 int len;
13295 int slen;
13296 int error = FALSE;
13297
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013298 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013299 slen = (int)STRLEN(p);
13300
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013301 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013302 if (error)
13303 len = 0;
13304 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013305 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013306 else
13307 len = slen - n; /* default len: all bytes that are available. */
13308
13309 /*
13310 * Only return the overlap between the specified part and the actual
13311 * string.
13312 */
13313 if (n < 0)
13314 {
13315 len += n;
13316 n = 0;
13317 }
13318 else if (n > slen)
13319 n = slen;
13320 if (len < 0)
13321 len = 0;
13322 else if (n + len > slen)
13323 len = slen - n;
13324
13325 rettv->v_type = VAR_STRING;
13326 rettv->vval.v_string = vim_strnsave(p + n, len);
13327}
13328
13329/*
13330 * "strridx()" function
13331 */
13332 static void
13333f_strridx(typval_T *argvars, typval_T *rettv)
13334{
13335 char_u buf[NUMBUFLEN];
13336 char_u *needle;
13337 char_u *haystack;
13338 char_u *rest;
13339 char_u *lastmatch = NULL;
13340 int haystack_len, end_idx;
13341
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013342 needle = tv_get_string_chk(&argvars[1]);
13343 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013344
13345 rettv->vval.v_number = -1;
13346 if (needle == NULL || haystack == NULL)
13347 return; /* type error; errmsg already given */
13348
13349 haystack_len = (int)STRLEN(haystack);
13350 if (argvars[2].v_type != VAR_UNKNOWN)
13351 {
13352 /* Third argument: upper limit for index */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013353 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013354 if (end_idx < 0)
13355 return; /* can never find a match */
13356 }
13357 else
13358 end_idx = haystack_len;
13359
13360 if (*needle == NUL)
13361 {
13362 /* Empty string matches past the end. */
13363 lastmatch = haystack + end_idx;
13364 }
13365 else
13366 {
13367 for (rest = haystack; *rest != '\0'; ++rest)
13368 {
13369 rest = (char_u *)strstr((char *)rest, (char *)needle);
13370 if (rest == NULL || rest > haystack + end_idx)
13371 break;
13372 lastmatch = rest;
13373 }
13374 }
13375
13376 if (lastmatch == NULL)
13377 rettv->vval.v_number = -1;
13378 else
13379 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
13380}
13381
13382/*
13383 * "strtrans()" function
13384 */
13385 static void
13386f_strtrans(typval_T *argvars, typval_T *rettv)
13387{
13388 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013389 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013390}
13391
13392/*
13393 * "submatch()" function
13394 */
13395 static void
13396f_submatch(typval_T *argvars, typval_T *rettv)
13397{
13398 int error = FALSE;
13399 int no;
13400 int retList = 0;
13401
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013402 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013403 if (error)
13404 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013405 if (no < 0 || no >= NSUBEXP)
13406 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013407 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +010013408 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013409 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013410 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013411 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013412 if (error)
13413 return;
13414
13415 if (retList == 0)
13416 {
13417 rettv->v_type = VAR_STRING;
13418 rettv->vval.v_string = reg_submatch(no);
13419 }
13420 else
13421 {
13422 rettv->v_type = VAR_LIST;
13423 rettv->vval.v_list = reg_submatch_list(no);
13424 }
13425}
13426
13427/*
13428 * "substitute()" function
13429 */
13430 static void
13431f_substitute(typval_T *argvars, typval_T *rettv)
13432{
13433 char_u patbuf[NUMBUFLEN];
13434 char_u subbuf[NUMBUFLEN];
13435 char_u flagsbuf[NUMBUFLEN];
13436
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013437 char_u *str = tv_get_string_chk(&argvars[0]);
13438 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013439 char_u *sub = NULL;
13440 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013441 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013442
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013443 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
13444 expr = &argvars[2];
13445 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013446 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013447
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013448 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013449 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
13450 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013451 rettv->vval.v_string = NULL;
13452 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013453 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013454}
13455
13456/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013457 * "swapinfo(swap_filename)" function
13458 */
13459 static void
13460f_swapinfo(typval_T *argvars, typval_T *rettv)
13461{
13462 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013463 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013464}
13465
13466/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020013467 * "swapname(expr)" function
13468 */
13469 static void
13470f_swapname(typval_T *argvars, typval_T *rettv)
13471{
13472 buf_T *buf;
13473
13474 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010013475 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +020013476 if (buf == NULL || buf->b_ml.ml_mfp == NULL
13477 || buf->b_ml.ml_mfp->mf_fname == NULL)
13478 rettv->vval.v_string = NULL;
13479 else
13480 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
13481}
13482
13483/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013484 * "synID(lnum, col, trans)" function
13485 */
13486 static void
13487f_synID(typval_T *argvars UNUSED, typval_T *rettv)
13488{
13489 int id = 0;
13490#ifdef FEAT_SYN_HL
13491 linenr_T lnum;
13492 colnr_T col;
13493 int trans;
13494 int transerr = FALSE;
13495
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013496 lnum = tv_get_lnum(argvars); /* -1 on type error */
13497 col = (linenr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
13498 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013499
13500 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13501 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
13502 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
13503#endif
13504
13505 rettv->vval.v_number = id;
13506}
13507
13508/*
13509 * "synIDattr(id, what [, mode])" function
13510 */
13511 static void
13512f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
13513{
13514 char_u *p = NULL;
13515#ifdef FEAT_SYN_HL
13516 int id;
13517 char_u *what;
13518 char_u *mode;
13519 char_u modebuf[NUMBUFLEN];
13520 int modec;
13521
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013522 id = (int)tv_get_number(&argvars[0]);
13523 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013524 if (argvars[2].v_type != VAR_UNKNOWN)
13525 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013526 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013527 modec = TOLOWER_ASC(mode[0]);
13528 if (modec != 't' && modec != 'c' && modec != 'g')
13529 modec = 0; /* replace invalid with current */
13530 }
13531 else
13532 {
13533#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
13534 if (USE_24BIT)
13535 modec = 'g';
13536 else
13537#endif
13538 if (t_colors > 1)
13539 modec = 'c';
13540 else
13541 modec = 't';
13542 }
13543
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013544 switch (TOLOWER_ASC(what[0]))
13545 {
13546 case 'b':
13547 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
13548 p = highlight_color(id, what, modec);
13549 else /* bold */
13550 p = highlight_has_attr(id, HL_BOLD, modec);
13551 break;
13552
13553 case 'f': /* fg[#] or font */
13554 p = highlight_color(id, what, modec);
13555 break;
13556
13557 case 'i':
13558 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
13559 p = highlight_has_attr(id, HL_INVERSE, modec);
13560 else /* italic */
13561 p = highlight_has_attr(id, HL_ITALIC, modec);
13562 break;
13563
13564 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020013565 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013566 break;
13567
13568 case 'r': /* reverse */
13569 p = highlight_has_attr(id, HL_INVERSE, modec);
13570 break;
13571
13572 case 's':
13573 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
13574 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020013575 /* strikeout */
13576 else if (TOLOWER_ASC(what[1]) == 't' &&
13577 TOLOWER_ASC(what[2]) == 'r')
13578 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013579 else /* standout */
13580 p = highlight_has_attr(id, HL_STANDOUT, modec);
13581 break;
13582
13583 case 'u':
13584 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
13585 /* underline */
13586 p = highlight_has_attr(id, HL_UNDERLINE, modec);
13587 else
13588 /* undercurl */
13589 p = highlight_has_attr(id, HL_UNDERCURL, modec);
13590 break;
13591 }
13592
13593 if (p != NULL)
13594 p = vim_strsave(p);
13595#endif
13596 rettv->v_type = VAR_STRING;
13597 rettv->vval.v_string = p;
13598}
13599
13600/*
13601 * "synIDtrans(id)" function
13602 */
13603 static void
13604f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
13605{
13606 int id;
13607
13608#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013609 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013610
13611 if (id > 0)
13612 id = syn_get_final_id(id);
13613 else
13614#endif
13615 id = 0;
13616
13617 rettv->vval.v_number = id;
13618}
13619
13620/*
13621 * "synconcealed(lnum, col)" function
13622 */
13623 static void
13624f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
13625{
13626#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
13627 linenr_T lnum;
13628 colnr_T col;
13629 int syntax_flags = 0;
13630 int cchar;
13631 int matchid = 0;
13632 char_u str[NUMBUFLEN];
13633#endif
13634
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013635 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013636
13637#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013638 lnum = tv_get_lnum(argvars); /* -1 on type error */
13639 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013640
13641 vim_memset(str, NUL, sizeof(str));
13642
13643 if (rettv_list_alloc(rettv) != FAIL)
13644 {
13645 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13646 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13647 && curwin->w_p_cole > 0)
13648 {
13649 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
13650 syntax_flags = get_syntax_info(&matchid);
13651
13652 /* get the conceal character */
13653 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
13654 {
13655 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020013656 if (cchar == NUL && curwin->w_p_cole == 1)
13657 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013658 if (cchar != NUL)
13659 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013660 if (has_mbyte)
13661 (*mb_char2bytes)(cchar, str);
13662 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013663 str[0] = cchar;
13664 }
13665 }
13666 }
13667
13668 list_append_number(rettv->vval.v_list,
13669 (syntax_flags & HL_CONCEAL) != 0);
13670 /* -1 to auto-determine strlen */
13671 list_append_string(rettv->vval.v_list, str, -1);
13672 list_append_number(rettv->vval.v_list, matchid);
13673 }
13674#endif
13675}
13676
13677/*
13678 * "synstack(lnum, col)" function
13679 */
13680 static void
13681f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
13682{
13683#ifdef FEAT_SYN_HL
13684 linenr_T lnum;
13685 colnr_T col;
13686 int i;
13687 int id;
13688#endif
13689
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013690 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013691
13692#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013693 lnum = tv_get_lnum(argvars); /* -1 on type error */
13694 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013695
13696 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13697 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13698 && rettv_list_alloc(rettv) != FAIL)
13699 {
13700 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
13701 for (i = 0; ; ++i)
13702 {
13703 id = syn_get_stack_item(i);
13704 if (id < 0)
13705 break;
13706 if (list_append_number(rettv->vval.v_list, id) == FAIL)
13707 break;
13708 }
13709 }
13710#endif
13711}
13712
13713 static void
13714get_cmd_output_as_rettv(
13715 typval_T *argvars,
13716 typval_T *rettv,
13717 int retlist)
13718{
13719 char_u *res = NULL;
13720 char_u *p;
13721 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013722 int err = FALSE;
13723 FILE *fd;
13724 list_T *list = NULL;
13725 int flags = SHELL_SILENT;
13726
13727 rettv->v_type = VAR_STRING;
13728 rettv->vval.v_string = NULL;
13729 if (check_restricted() || check_secure())
13730 goto errret;
13731
13732 if (argvars[1].v_type != VAR_UNKNOWN)
13733 {
13734 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010013735 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013736 * command.
13737 */
13738 if ((infile = vim_tempname('i', TRUE)) == NULL)
13739 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013740 emsg(_(e_notmp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013741 goto errret;
13742 }
13743
13744 fd = mch_fopen((char *)infile, WRITEBIN);
13745 if (fd == NULL)
13746 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013747 semsg(_(e_notopen), infile);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013748 goto errret;
13749 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010013750 if (argvars[1].v_type == VAR_NUMBER)
13751 {
13752 linenr_T lnum;
13753 buf_T *buf;
13754
13755 buf = buflist_findnr(argvars[1].vval.v_number);
13756 if (buf == NULL)
13757 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013758 semsg(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010013759 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010013760 goto errret;
13761 }
13762
13763 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
13764 {
13765 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
13766 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
13767 {
13768 err = TRUE;
13769 break;
13770 }
13771 if (putc(NL, fd) == EOF)
13772 {
13773 err = TRUE;
13774 break;
13775 }
13776 }
13777 }
13778 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013779 {
13780 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
13781 err = TRUE;
13782 }
13783 else
13784 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010013785 size_t len;
13786 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013787
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013788 p = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013789 if (p == NULL)
13790 {
13791 fclose(fd);
13792 goto errret; /* type error; errmsg already given */
13793 }
13794 len = STRLEN(p);
13795 if (len > 0 && fwrite(p, len, 1, fd) != 1)
13796 err = TRUE;
13797 }
13798 if (fclose(fd) != 0)
13799 err = TRUE;
13800 if (err)
13801 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013802 emsg(_("E677: Error writing temp file"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013803 goto errret;
13804 }
13805 }
13806
13807 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
13808 * echoes typeahead, that messes up the display. */
13809 if (!msg_silent)
13810 flags += SHELL_COOKED;
13811
13812 if (retlist)
13813 {
13814 int len;
13815 listitem_T *li;
13816 char_u *s = NULL;
13817 char_u *start;
13818 char_u *end;
13819 int i;
13820
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013821 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013822 if (res == NULL)
13823 goto errret;
13824
13825 list = list_alloc();
13826 if (list == NULL)
13827 goto errret;
13828
13829 for (i = 0; i < len; ++i)
13830 {
13831 start = res + i;
13832 while (i < len && res[i] != NL)
13833 ++i;
13834 end = res + i;
13835
13836 s = alloc((unsigned)(end - start + 1));
13837 if (s == NULL)
13838 goto errret;
13839
13840 for (p = s; start < end; ++p, ++start)
13841 *p = *start == NUL ? NL : *start;
13842 *p = NUL;
13843
13844 li = listitem_alloc();
13845 if (li == NULL)
13846 {
13847 vim_free(s);
13848 goto errret;
13849 }
13850 li->li_tv.v_type = VAR_STRING;
13851 li->li_tv.v_lock = 0;
13852 li->li_tv.vval.v_string = s;
13853 list_append(list, li);
13854 }
13855
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013856 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013857 list = NULL;
13858 }
13859 else
13860 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013861 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar00590742019-02-15 21:06:09 +010013862#ifdef USE_CRNL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013863 /* translate <CR><NL> into <NL> */
13864 if (res != NULL)
13865 {
13866 char_u *s, *d;
13867
13868 d = res;
13869 for (s = res; *s; ++s)
13870 {
13871 if (s[0] == CAR && s[1] == NL)
13872 ++s;
13873 *d++ = *s;
13874 }
13875 *d = NUL;
13876 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013877#endif
13878 rettv->vval.v_string = res;
13879 res = NULL;
13880 }
13881
13882errret:
13883 if (infile != NULL)
13884 {
13885 mch_remove(infile);
13886 vim_free(infile);
13887 }
13888 if (res != NULL)
13889 vim_free(res);
13890 if (list != NULL)
13891 list_free(list);
13892}
13893
13894/*
13895 * "system()" function
13896 */
13897 static void
13898f_system(typval_T *argvars, typval_T *rettv)
13899{
13900 get_cmd_output_as_rettv(argvars, rettv, FALSE);
13901}
13902
13903/*
13904 * "systemlist()" function
13905 */
13906 static void
13907f_systemlist(typval_T *argvars, typval_T *rettv)
13908{
13909 get_cmd_output_as_rettv(argvars, rettv, TRUE);
13910}
13911
13912/*
13913 * "tabpagebuflist()" function
13914 */
13915 static void
13916f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13917{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013918 tabpage_T *tp;
13919 win_T *wp = NULL;
13920
13921 if (argvars[0].v_type == VAR_UNKNOWN)
13922 wp = firstwin;
13923 else
13924 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013925 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013926 if (tp != NULL)
13927 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
13928 }
13929 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
13930 {
13931 for (; wp != NULL; wp = wp->w_next)
13932 if (list_append_number(rettv->vval.v_list,
13933 wp->w_buffer->b_fnum) == FAIL)
13934 break;
13935 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013936}
13937
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013938/*
13939 * "tabpagenr()" function
13940 */
13941 static void
13942f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
13943{
13944 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013945 char_u *arg;
13946
13947 if (argvars[0].v_type != VAR_UNKNOWN)
13948 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013949 arg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013950 nr = 0;
13951 if (arg != NULL)
13952 {
13953 if (STRCMP(arg, "$") == 0)
13954 nr = tabpage_index(NULL) - 1;
13955 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013956 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013957 }
13958 }
13959 else
13960 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013961 rettv->vval.v_number = nr;
13962}
13963
13964
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013965/*
13966 * Common code for tabpagewinnr() and winnr().
13967 */
13968 static int
13969get_winnr(tabpage_T *tp, typval_T *argvar)
13970{
13971 win_T *twin;
13972 int nr = 1;
13973 win_T *wp;
13974 char_u *arg;
13975
13976 twin = (tp == curtab) ? curwin : tp->tp_curwin;
13977 if (argvar->v_type != VAR_UNKNOWN)
13978 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013979 arg = tv_get_string_chk(argvar);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013980 if (arg == NULL)
13981 nr = 0; /* type error; errmsg already given */
13982 else if (STRCMP(arg, "$") == 0)
13983 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
13984 else if (STRCMP(arg, "#") == 0)
13985 {
13986 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
13987 if (twin == NULL)
13988 nr = 0;
13989 }
13990 else
13991 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013992 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013993 nr = 0;
13994 }
13995 }
13996
13997 if (nr > 0)
13998 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
13999 wp != twin; wp = wp->w_next)
14000 {
14001 if (wp == NULL)
14002 {
14003 /* didn't find it in this tabpage */
14004 nr = 0;
14005 break;
14006 }
14007 ++nr;
14008 }
14009 return nr;
14010}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014011
14012/*
14013 * "tabpagewinnr()" function
14014 */
14015 static void
14016f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
14017{
14018 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014019 tabpage_T *tp;
14020
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014021 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014022 if (tp == NULL)
14023 nr = 0;
14024 else
14025 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014026 rettv->vval.v_number = nr;
14027}
14028
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014029/*
14030 * "tagfiles()" function
14031 */
14032 static void
14033f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
14034{
14035 char_u *fname;
14036 tagname_T tn;
14037 int first;
14038
14039 if (rettv_list_alloc(rettv) == FAIL)
14040 return;
14041 fname = alloc(MAXPATHL);
14042 if (fname == NULL)
14043 return;
14044
14045 for (first = TRUE; ; first = FALSE)
14046 if (get_tagfname(&tn, first, fname) == FAIL
14047 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
14048 break;
14049 tagname_free(&tn);
14050 vim_free(fname);
14051}
14052
14053/*
14054 * "taglist()" function
14055 */
14056 static void
14057f_taglist(typval_T *argvars, typval_T *rettv)
14058{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014059 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014060 char_u *tag_pattern;
14061
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014062 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014063
14064 rettv->vval.v_number = FALSE;
14065 if (*tag_pattern == NUL)
14066 return;
14067
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014068 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014069 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014070 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014071 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014072}
14073
14074/*
14075 * "tempname()" function
14076 */
14077 static void
14078f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
14079{
14080 static int x = 'A';
14081
14082 rettv->v_type = VAR_STRING;
14083 rettv->vval.v_string = vim_tempname(x, FALSE);
14084
14085 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
14086 * names. Skip 'I' and 'O', they are used for shell redirection. */
14087 do
14088 {
14089 if (x == 'Z')
14090 x = '0';
14091 else if (x == '9')
14092 x = 'A';
14093 else
14094 {
14095#ifdef EBCDIC
14096 if (x == 'I')
14097 x = 'J';
14098 else if (x == 'R')
14099 x = 'S';
14100 else
14101#endif
14102 ++x;
14103 }
14104 } while (x == 'I' || x == 'O');
14105}
14106
14107#ifdef FEAT_FLOAT
14108/*
14109 * "tan()" function
14110 */
14111 static void
14112f_tan(typval_T *argvars, typval_T *rettv)
14113{
14114 float_T f = 0.0;
14115
14116 rettv->v_type = VAR_FLOAT;
14117 if (get_float_arg(argvars, &f) == OK)
14118 rettv->vval.v_float = tan(f);
14119 else
14120 rettv->vval.v_float = 0.0;
14121}
14122
14123/*
14124 * "tanh()" function
14125 */
14126 static void
14127f_tanh(typval_T *argvars, typval_T *rettv)
14128{
14129 float_T f = 0.0;
14130
14131 rettv->v_type = VAR_FLOAT;
14132 if (get_float_arg(argvars, &f) == OK)
14133 rettv->vval.v_float = tanh(f);
14134 else
14135 rettv->vval.v_float = 0.0;
14136}
14137#endif
14138
14139/*
14140 * "test_alloc_fail(id, countdown, repeat)" function
14141 */
14142 static void
14143f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
14144{
14145 if (argvars[0].v_type != VAR_NUMBER
14146 || argvars[0].vval.v_number <= 0
14147 || argvars[1].v_type != VAR_NUMBER
14148 || argvars[1].vval.v_number < 0
14149 || argvars[2].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014150 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014151 else
14152 {
14153 alloc_fail_id = argvars[0].vval.v_number;
14154 if (alloc_fail_id >= aid_last)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014155 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014156 alloc_fail_countdown = argvars[1].vval.v_number;
14157 alloc_fail_repeat = argvars[2].vval.v_number;
14158 did_outofmem_msg = FALSE;
14159 }
14160}
14161
14162/*
14163 * "test_autochdir()"
14164 */
14165 static void
14166f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14167{
14168#if defined(FEAT_AUTOCHDIR)
14169 test_autochdir = TRUE;
14170#endif
14171}
14172
14173/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020014174 * "test_feedinput()"
14175 */
14176 static void
14177f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
14178{
14179#ifdef USE_INPUT_BUF
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014180 char_u *val = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5e80de32017-09-03 15:48:12 +020014181
14182 if (val != NULL)
14183 {
14184 trash_input_buf();
14185 add_to_input_buf_csi(val, (int)STRLEN(val));
14186 }
14187#endif
14188}
14189
14190/*
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014191 * "test_option_not_set({name})" function
14192 */
14193 static void
14194f_test_option_not_set(typval_T *argvars, typval_T *rettv UNUSED)
14195{
14196 char_u *name = (char_u *)"";
14197
14198 if (argvars[0].v_type != VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014199 emsg(_(e_invarg));
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014200 else
14201 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014202 name = tv_get_string(&argvars[0]);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014203 if (reset_option_was_set(name) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014204 semsg(_(e_invarg2), name);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014205 }
14206}
14207
14208/*
14209 * "test_override({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014210 */
14211 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014212f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014213{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014214 char_u *name = (char_u *)"";
14215 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014216 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014217
14218 if (argvars[0].v_type != VAR_STRING
14219 || (argvars[1].v_type) != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014220 emsg(_(e_invarg));
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014221 else
14222 {
Bram Moolenaare38197d2018-12-24 23:35:13 +010014223 name = tv_get_string(&argvars[0]);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014224 val = (int)tv_get_number(&argvars[1]);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014225
14226 if (STRCMP(name, (char_u *)"redraw") == 0)
14227 disable_redraw_for_testing = val;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020014228 else if (STRCMP(name, (char_u *)"redraw_flag") == 0)
14229 ignore_redraw_flag_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014230 else if (STRCMP(name, (char_u *)"char_avail") == 0)
14231 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014232 else if (STRCMP(name, (char_u *)"starting") == 0)
14233 {
14234 if (val)
14235 {
14236 if (save_starting < 0)
14237 save_starting = starting;
14238 starting = 0;
14239 }
14240 else
14241 {
14242 starting = save_starting;
14243 save_starting = -1;
14244 }
14245 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +020014246 else if (STRCMP(name, (char_u *)"nfa_fail") == 0)
14247 nfa_fail_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014248 else if (STRCMP(name, (char_u *)"ALL") == 0)
14249 {
14250 disable_char_avail_for_testing = FALSE;
14251 disable_redraw_for_testing = FALSE;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020014252 ignore_redraw_flag_for_testing = FALSE;
Bram Moolenaarbcf94422018-06-23 14:21:42 +020014253 nfa_fail_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014254 if (save_starting >= 0)
14255 {
14256 starting = save_starting;
14257 save_starting = -1;
14258 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014259 }
14260 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014261 semsg(_(e_invarg2), name);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014262 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014263}
14264
14265/*
Bram Moolenaarc3e92c12019-03-23 14:23:07 +010014266 * "test_refcount({expr})" function
14267 */
14268 static void
14269f_test_refcount(typval_T *argvars, typval_T *rettv)
14270{
14271 int retval = -1;
14272
14273 switch (argvars[0].v_type)
14274 {
14275 case VAR_UNKNOWN:
14276 case VAR_NUMBER:
14277 case VAR_FLOAT:
14278 case VAR_SPECIAL:
14279 case VAR_STRING:
14280 break;
14281 case VAR_JOB:
14282#ifdef FEAT_JOB_CHANNEL
14283 if (argvars[0].vval.v_job != NULL)
14284 retval = argvars[0].vval.v_job->jv_refcount - 1;
14285#endif
14286 break;
14287 case VAR_CHANNEL:
14288#ifdef FEAT_JOB_CHANNEL
14289 if (argvars[0].vval.v_channel != NULL)
14290 retval = argvars[0].vval.v_channel->ch_refcount - 1;
14291#endif
14292 break;
14293 case VAR_FUNC:
14294 if (argvars[0].vval.v_string != NULL)
14295 {
14296 ufunc_T *fp;
14297
14298 fp = find_func(argvars[0].vval.v_string);
14299 if (fp != NULL)
14300 retval = fp->uf_refcount;
14301 }
14302 break;
14303 case VAR_PARTIAL:
14304 if (argvars[0].vval.v_partial != NULL)
14305 retval = argvars[0].vval.v_partial->pt_refcount - 1;
14306 break;
14307 case VAR_BLOB:
14308 if (argvars[0].vval.v_blob != NULL)
14309 retval = argvars[0].vval.v_blob->bv_refcount - 1;
14310 break;
14311 case VAR_LIST:
14312 if (argvars[0].vval.v_list != NULL)
14313 retval = argvars[0].vval.v_list->lv_refcount - 1;
14314 break;
14315 case VAR_DICT:
14316 if (argvars[0].vval.v_dict != NULL)
14317 retval = argvars[0].vval.v_dict->dv_refcount - 1;
14318 break;
14319 }
14320
14321 rettv->v_type = VAR_NUMBER;
14322 rettv->vval.v_number = retval;
14323
14324}
14325
14326/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014327 * "test_garbagecollect_now()" function
14328 */
14329 static void
14330f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14331{
14332 /* This is dangerous, any Lists and Dicts used internally may be freed
14333 * while still in use. */
14334 garbage_collect(TRUE);
14335}
14336
Bram Moolenaare0c31f62017-03-01 15:07:05 +010014337/*
14338 * "test_ignore_error()" function
14339 */
14340 static void
14341f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
14342{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014343 ignore_error_for_testing(tv_get_string(&argvars[0]));
Bram Moolenaare0c31f62017-03-01 15:07:05 +010014344}
14345
Bram Moolenaarc0f5a782019-01-13 15:16:13 +010014346 static void
14347f_test_null_blob(typval_T *argvars UNUSED, typval_T *rettv)
14348{
14349 rettv->v_type = VAR_BLOB;
14350 rettv->vval.v_blob = NULL;
14351}
14352
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014353#ifdef FEAT_JOB_CHANNEL
14354 static void
14355f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
14356{
14357 rettv->v_type = VAR_CHANNEL;
14358 rettv->vval.v_channel = NULL;
14359}
14360#endif
14361
14362 static void
14363f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
14364{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014365 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014366}
14367
14368#ifdef FEAT_JOB_CHANNEL
14369 static void
14370f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
14371{
14372 rettv->v_type = VAR_JOB;
14373 rettv->vval.v_job = NULL;
14374}
14375#endif
14376
14377 static void
14378f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
14379{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014380 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014381}
14382
14383 static void
14384f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
14385{
14386 rettv->v_type = VAR_PARTIAL;
14387 rettv->vval.v_partial = NULL;
14388}
14389
14390 static void
14391f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
14392{
14393 rettv->v_type = VAR_STRING;
14394 rettv->vval.v_string = NULL;
14395}
14396
Bram Moolenaarab186732018-09-14 21:27:06 +020014397#ifdef FEAT_GUI
14398 static void
14399f_test_scrollbar(typval_T *argvars, typval_T *rettv UNUSED)
14400{
14401 char_u *which;
14402 long value;
14403 int dragging;
14404 scrollbar_T *sb = NULL;
14405
14406 if (argvars[0].v_type != VAR_STRING
14407 || (argvars[1].v_type) != VAR_NUMBER
14408 || (argvars[2].v_type) != VAR_NUMBER)
14409 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014410 emsg(_(e_invarg));
Bram Moolenaarab186732018-09-14 21:27:06 +020014411 return;
14412 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014413 which = tv_get_string(&argvars[0]);
14414 value = tv_get_number(&argvars[1]);
14415 dragging = tv_get_number(&argvars[2]);
Bram Moolenaarab186732018-09-14 21:27:06 +020014416
14417 if (STRCMP(which, "left") == 0)
14418 sb = &curwin->w_scrollbars[SBAR_LEFT];
14419 else if (STRCMP(which, "right") == 0)
14420 sb = &curwin->w_scrollbars[SBAR_RIGHT];
14421 else if (STRCMP(which, "hor") == 0)
14422 sb = &gui.bottom_sbar;
14423 if (sb == NULL)
14424 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014425 semsg(_(e_invarg2), which);
Bram Moolenaarab186732018-09-14 21:27:06 +020014426 return;
14427 }
14428 gui_drag_scrollbar(sb, value, dragging);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020014429# ifndef USE_ON_FLY_SCROLL
14430 // need to loop through normal_cmd() to handle the scroll events
14431 exec_normal(FALSE, TRUE, FALSE);
14432# endif
Bram Moolenaarab186732018-09-14 21:27:06 +020014433}
14434#endif
14435
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014436 static void
14437f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
14438{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014439 time_for_testing = (time_t)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014440}
14441
14442#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
14443/*
14444 * Get a callback from "arg". It can be a Funcref or a function name.
14445 * When "arg" is zero return an empty string.
14446 * Return NULL for an invalid argument.
14447 */
14448 char_u *
14449get_callback(typval_T *arg, partial_T **pp)
14450{
14451 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
14452 {
14453 *pp = arg->vval.v_partial;
14454 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020014455 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014456 }
14457 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020014458 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014459 {
14460 func_ref(arg->vval.v_string);
14461 return arg->vval.v_string;
14462 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014463 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
14464 return (char_u *)"";
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014465 emsg(_("E921: Invalid callback argument"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014466 return NULL;
14467}
14468
14469/*
Bram Moolenaard5359b22018-04-05 22:44:39 +020014470 * Unref/free "callback" and "partial" returned by get_callback().
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014471 */
14472 void
14473free_callback(char_u *callback, partial_T *partial)
14474{
14475 if (partial != NULL)
14476 partial_unref(partial);
14477 else if (callback != NULL)
14478 {
14479 func_unref(callback);
14480 vim_free(callback);
14481 }
14482}
14483#endif
14484
14485#ifdef FEAT_TIMERS
14486/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014487 * "timer_info([timer])" function
14488 */
14489 static void
14490f_timer_info(typval_T *argvars, typval_T *rettv)
14491{
14492 timer_T *timer = NULL;
14493
14494 if (rettv_list_alloc(rettv) != OK)
14495 return;
14496 if (argvars[0].v_type != VAR_UNKNOWN)
14497 {
14498 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014499 emsg(_(e_number_exp));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014500 else
14501 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014502 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014503 if (timer != NULL)
14504 add_timer_info(rettv, timer);
14505 }
14506 }
14507 else
14508 add_timer_info_all(rettv);
14509}
14510
14511/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014512 * "timer_pause(timer, paused)" function
14513 */
14514 static void
14515f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
14516{
14517 timer_T *timer = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014518 int paused = (int)tv_get_number(&argvars[1]);
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014519
14520 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014521 emsg(_(e_number_exp));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014522 else
14523 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014524 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014525 if (timer != NULL)
14526 timer->tr_paused = paused;
14527 }
14528}
14529
14530/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014531 * "timer_start(time, callback [, options])" function
14532 */
14533 static void
14534f_timer_start(typval_T *argvars, typval_T *rettv)
14535{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014536 long msec = (long)tv_get_number(&argvars[0]);
Bram Moolenaar75537a92016-09-05 22:45:28 +020014537 timer_T *timer;
14538 int repeat = 0;
14539 char_u *callback;
14540 dict_T *dict;
14541 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014542
Bram Moolenaar75537a92016-09-05 22:45:28 +020014543 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014544 if (check_secure())
14545 return;
14546 if (argvars[2].v_type != VAR_UNKNOWN)
14547 {
14548 if (argvars[2].v_type != VAR_DICT
14549 || (dict = argvars[2].vval.v_dict) == NULL)
14550 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014551 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014552 return;
14553 }
14554 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014555 repeat = dict_get_number(dict, (char_u *)"repeat");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014556 }
14557
Bram Moolenaar75537a92016-09-05 22:45:28 +020014558 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014559 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020014560 return;
14561
14562 timer = create_timer(msec, repeat);
14563 if (timer == NULL)
14564 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014565 else
14566 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020014567 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020014568 timer->tr_callback = vim_strsave(callback);
14569 else
14570 /* pointer into the partial */
14571 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020014572 timer->tr_partial = partial;
14573 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014574 }
14575}
14576
14577/*
14578 * "timer_stop(timer)" function
14579 */
14580 static void
14581f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
14582{
14583 timer_T *timer;
14584
14585 if (argvars[0].v_type != VAR_NUMBER)
14586 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014587 emsg(_(e_number_exp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014588 return;
14589 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014590 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014591 if (timer != NULL)
14592 stop_timer(timer);
14593}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014594
14595/*
14596 * "timer_stopall()" function
14597 */
14598 static void
14599f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14600{
14601 stop_all_timers();
14602}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014603#endif
14604
14605/*
14606 * "tolower(string)" function
14607 */
14608 static void
14609f_tolower(typval_T *argvars, typval_T *rettv)
14610{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014611 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014612 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014613}
14614
14615/*
14616 * "toupper(string)" function
14617 */
14618 static void
14619f_toupper(typval_T *argvars, typval_T *rettv)
14620{
14621 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014622 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014623}
14624
14625/*
14626 * "tr(string, fromstr, tostr)" function
14627 */
14628 static void
14629f_tr(typval_T *argvars, typval_T *rettv)
14630{
14631 char_u *in_str;
14632 char_u *fromstr;
14633 char_u *tostr;
14634 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014635 int inlen;
14636 int fromlen;
14637 int tolen;
14638 int idx;
14639 char_u *cpstr;
14640 int cplen;
14641 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014642 char_u buf[NUMBUFLEN];
14643 char_u buf2[NUMBUFLEN];
14644 garray_T ga;
14645
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014646 in_str = tv_get_string(&argvars[0]);
14647 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
14648 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014649
14650 /* Default return value: empty string. */
14651 rettv->v_type = VAR_STRING;
14652 rettv->vval.v_string = NULL;
14653 if (fromstr == NULL || tostr == NULL)
14654 return; /* type error; errmsg already given */
14655 ga_init2(&ga, (int)sizeof(char), 80);
14656
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014657 if (!has_mbyte)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014658 /* not multi-byte: fromstr and tostr must be the same length */
14659 if (STRLEN(fromstr) != STRLEN(tostr))
14660 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014661error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014662 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014663 ga_clear(&ga);
14664 return;
14665 }
14666
14667 /* fromstr and tostr have to contain the same number of chars */
14668 while (*in_str != NUL)
14669 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014670 if (has_mbyte)
14671 {
14672 inlen = (*mb_ptr2len)(in_str);
14673 cpstr = in_str;
14674 cplen = inlen;
14675 idx = 0;
14676 for (p = fromstr; *p != NUL; p += fromlen)
14677 {
14678 fromlen = (*mb_ptr2len)(p);
14679 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
14680 {
14681 for (p = tostr; *p != NUL; p += tolen)
14682 {
14683 tolen = (*mb_ptr2len)(p);
14684 if (idx-- == 0)
14685 {
14686 cplen = tolen;
14687 cpstr = p;
14688 break;
14689 }
14690 }
14691 if (*p == NUL) /* tostr is shorter than fromstr */
14692 goto error;
14693 break;
14694 }
14695 ++idx;
14696 }
14697
14698 if (first && cpstr == in_str)
14699 {
14700 /* Check that fromstr and tostr have the same number of
14701 * (multi-byte) characters. Done only once when a character
14702 * of in_str doesn't appear in fromstr. */
14703 first = FALSE;
14704 for (p = tostr; *p != NUL; p += tolen)
14705 {
14706 tolen = (*mb_ptr2len)(p);
14707 --idx;
14708 }
14709 if (idx != 0)
14710 goto error;
14711 }
14712
14713 (void)ga_grow(&ga, cplen);
14714 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
14715 ga.ga_len += cplen;
14716
14717 in_str += inlen;
14718 }
14719 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014720 {
14721 /* When not using multi-byte chars we can do it faster. */
14722 p = vim_strchr(fromstr, *in_str);
14723 if (p != NULL)
14724 ga_append(&ga, tostr[p - fromstr]);
14725 else
14726 ga_append(&ga, *in_str);
14727 ++in_str;
14728 }
14729 }
14730
14731 /* add a terminating NUL */
14732 (void)ga_grow(&ga, 1);
14733 ga_append(&ga, NUL);
14734
14735 rettv->vval.v_string = ga.ga_data;
14736}
14737
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014738/*
14739 * "trim({expr})" function
14740 */
14741 static void
14742f_trim(typval_T *argvars, typval_T *rettv)
14743{
14744 char_u buf1[NUMBUFLEN];
14745 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014746 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014747 char_u *mask = NULL;
14748 char_u *tail;
14749 char_u *prev;
14750 char_u *p;
14751 int c1;
14752
14753 rettv->v_type = VAR_STRING;
14754 if (head == NULL)
14755 {
14756 rettv->vval.v_string = NULL;
14757 return;
14758 }
14759
14760 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014761 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014762
14763 while (*head != NUL)
14764 {
14765 c1 = PTR2CHAR(head);
14766 if (mask == NULL)
14767 {
14768 if (c1 > ' ' && c1 != 0xa0)
14769 break;
14770 }
14771 else
14772 {
14773 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14774 if (c1 == PTR2CHAR(p))
14775 break;
14776 if (*p == NUL)
14777 break;
14778 }
14779 MB_PTR_ADV(head);
14780 }
14781
14782 for (tail = head + STRLEN(head); tail > head; tail = prev)
14783 {
14784 prev = tail;
14785 MB_PTR_BACK(head, prev);
14786 c1 = PTR2CHAR(prev);
14787 if (mask == NULL)
14788 {
14789 if (c1 > ' ' && c1 != 0xa0)
14790 break;
14791 }
14792 else
14793 {
14794 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14795 if (c1 == PTR2CHAR(p))
14796 break;
14797 if (*p == NUL)
14798 break;
14799 }
14800 }
14801 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
14802}
14803
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014804#ifdef FEAT_FLOAT
14805/*
14806 * "trunc({float})" function
14807 */
14808 static void
14809f_trunc(typval_T *argvars, typval_T *rettv)
14810{
14811 float_T f = 0.0;
14812
14813 rettv->v_type = VAR_FLOAT;
14814 if (get_float_arg(argvars, &f) == OK)
14815 /* trunc() is not in C90, use floor() or ceil() instead. */
14816 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
14817 else
14818 rettv->vval.v_float = 0.0;
14819}
14820#endif
14821
14822/*
14823 * "type(expr)" function
14824 */
14825 static void
14826f_type(typval_T *argvars, typval_T *rettv)
14827{
14828 int n = -1;
14829
14830 switch (argvars[0].v_type)
14831 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020014832 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
14833 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014834 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020014835 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
14836 case VAR_LIST: n = VAR_TYPE_LIST; break;
14837 case VAR_DICT: n = VAR_TYPE_DICT; break;
14838 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014839 case VAR_SPECIAL:
14840 if (argvars[0].vval.v_number == VVAL_FALSE
14841 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020014842 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014843 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020014844 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014845 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020014846 case VAR_JOB: n = VAR_TYPE_JOB; break;
14847 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014848 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014849 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010014850 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014851 n = -1;
14852 break;
14853 }
14854 rettv->vval.v_number = n;
14855}
14856
14857/*
14858 * "undofile(name)" function
14859 */
14860 static void
14861f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
14862{
14863 rettv->v_type = VAR_STRING;
14864#ifdef FEAT_PERSISTENT_UNDO
14865 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014866 char_u *fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014867
14868 if (*fname == NUL)
14869 {
14870 /* If there is no file name there will be no undo file. */
14871 rettv->vval.v_string = NULL;
14872 }
14873 else
14874 {
14875 char_u *ffname = FullName_save(fname, FALSE);
14876
14877 if (ffname != NULL)
14878 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
14879 vim_free(ffname);
14880 }
14881 }
14882#else
14883 rettv->vval.v_string = NULL;
14884#endif
14885}
14886
14887/*
14888 * "undotree()" function
14889 */
14890 static void
14891f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
14892{
14893 if (rettv_dict_alloc(rettv) == OK)
14894 {
14895 dict_T *dict = rettv->vval.v_dict;
14896 list_T *list;
14897
Bram Moolenaare0be1672018-07-08 16:50:37 +020014898 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
14899 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
14900 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
14901 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
14902 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
14903 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014904
14905 list = list_alloc();
14906 if (list != NULL)
14907 {
14908 u_eval_tree(curbuf->b_u_oldhead, list);
14909 dict_add_list(dict, "entries", list);
14910 }
14911 }
14912}
14913
14914/*
14915 * "values(dict)" function
14916 */
14917 static void
14918f_values(typval_T *argvars, typval_T *rettv)
14919{
14920 dict_list(argvars, rettv, 1);
14921}
14922
14923/*
14924 * "virtcol(string)" function
14925 */
14926 static void
14927f_virtcol(typval_T *argvars, typval_T *rettv)
14928{
14929 colnr_T vcol = 0;
14930 pos_T *fp;
14931 int fnum = curbuf->b_fnum;
14932
14933 fp = var2fpos(&argvars[0], FALSE, &fnum);
14934 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
14935 && fnum == curbuf->b_fnum)
14936 {
14937 getvvcol(curwin, fp, NULL, NULL, &vcol);
14938 ++vcol;
14939 }
14940
14941 rettv->vval.v_number = vcol;
14942}
14943
14944/*
14945 * "visualmode()" function
14946 */
14947 static void
14948f_visualmode(typval_T *argvars, typval_T *rettv)
14949{
14950 char_u str[2];
14951
14952 rettv->v_type = VAR_STRING;
14953 str[0] = curbuf->b_visual_mode_eval;
14954 str[1] = NUL;
14955 rettv->vval.v_string = vim_strsave(str);
14956
14957 /* A non-zero number or non-empty string argument: reset mode. */
14958 if (non_zero_arg(&argvars[0]))
14959 curbuf->b_visual_mode_eval = NUL;
14960}
14961
14962/*
14963 * "wildmenumode()" function
14964 */
14965 static void
14966f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14967{
14968#ifdef FEAT_WILDMENU
14969 if (wild_menu_showing)
14970 rettv->vval.v_number = 1;
14971#endif
14972}
14973
14974/*
14975 * "winbufnr(nr)" function
14976 */
14977 static void
14978f_winbufnr(typval_T *argvars, typval_T *rettv)
14979{
14980 win_T *wp;
14981
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014982 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014983 if (wp == NULL)
14984 rettv->vval.v_number = -1;
14985 else
14986 rettv->vval.v_number = wp->w_buffer->b_fnum;
14987}
14988
14989/*
14990 * "wincol()" function
14991 */
14992 static void
14993f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
14994{
14995 validate_cursor();
14996 rettv->vval.v_number = curwin->w_wcol + 1;
14997}
14998
14999/*
15000 * "winheight(nr)" function
15001 */
15002 static void
15003f_winheight(typval_T *argvars, typval_T *rettv)
15004{
15005 win_T *wp;
15006
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015007 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015008 if (wp == NULL)
15009 rettv->vval.v_number = -1;
15010 else
15011 rettv->vval.v_number = wp->w_height;
15012}
15013
15014/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020015015 * "winlayout()" function
15016 */
15017 static void
15018f_winlayout(typval_T *argvars, typval_T *rettv)
15019{
15020 tabpage_T *tp;
15021
15022 if (rettv_list_alloc(rettv) != OK)
15023 return;
15024
15025 if (argvars[0].v_type == VAR_UNKNOWN)
15026 tp = curtab;
15027 else
15028 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015029 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020015030 if (tp == NULL)
15031 return;
15032 }
15033
15034 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
15035}
15036
15037/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015038 * "winline()" function
15039 */
15040 static void
15041f_winline(typval_T *argvars UNUSED, typval_T *rettv)
15042{
15043 validate_cursor();
15044 rettv->vval.v_number = curwin->w_wrow + 1;
15045}
15046
15047/*
15048 * "winnr()" function
15049 */
15050 static void
15051f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
15052{
15053 int nr = 1;
15054
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015055 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015056 rettv->vval.v_number = nr;
15057}
15058
15059/*
15060 * "winrestcmd()" function
15061 */
15062 static void
15063f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
15064{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015065 win_T *wp;
15066 int winnr = 1;
15067 garray_T ga;
15068 char_u buf[50];
15069
15070 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020015071 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015072 {
15073 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
15074 ga_concat(&ga, buf);
15075 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
15076 ga_concat(&ga, buf);
15077 ++winnr;
15078 }
15079 ga_append(&ga, NUL);
15080
15081 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015082 rettv->v_type = VAR_STRING;
15083}
15084
15085/*
15086 * "winrestview()" function
15087 */
15088 static void
15089f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
15090{
15091 dict_T *dict;
15092
15093 if (argvars[0].v_type != VAR_DICT
15094 || (dict = argvars[0].vval.v_dict) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015095 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015096 else
15097 {
15098 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015099 curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015100 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015101 curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015102 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015103 curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015104 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
15105 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010015106 curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015107 curwin->w_set_curswant = FALSE;
15108 }
15109
15110 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015111 set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015112#ifdef FEAT_DIFF
15113 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015114 curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015115#endif
15116 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015117 curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015118 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015119 curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015120
15121 check_cursor();
15122 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020015123 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015124 changed_window_setting();
15125
15126 if (curwin->w_topline <= 0)
15127 curwin->w_topline = 1;
15128 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
15129 curwin->w_topline = curbuf->b_ml.ml_line_count;
15130#ifdef FEAT_DIFF
15131 check_topfill(curwin, TRUE);
15132#endif
15133 }
15134}
15135
15136/*
15137 * "winsaveview()" function
15138 */
15139 static void
15140f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
15141{
15142 dict_T *dict;
15143
15144 if (rettv_dict_alloc(rettv) == FAIL)
15145 return;
15146 dict = rettv->vval.v_dict;
15147
Bram Moolenaare0be1672018-07-08 16:50:37 +020015148 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
15149 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +020015150 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015151 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020015152 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015153
Bram Moolenaare0be1672018-07-08 16:50:37 +020015154 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015155#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020015156 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015157#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020015158 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
15159 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015160}
15161
15162/*
15163 * "winwidth(nr)" function
15164 */
15165 static void
15166f_winwidth(typval_T *argvars, typval_T *rettv)
15167{
15168 win_T *wp;
15169
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015170 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015171 if (wp == NULL)
15172 rettv->vval.v_number = -1;
15173 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015174 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015175}
15176
15177/*
15178 * "wordcount()" function
15179 */
15180 static void
15181f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
15182{
15183 if (rettv_dict_alloc(rettv) == FAIL)
15184 return;
15185 cursor_pos_info(rettv->vval.v_dict);
15186}
15187
15188/*
15189 * "writefile()" function
15190 */
15191 static void
15192f_writefile(typval_T *argvars, typval_T *rettv)
15193{
15194 int binary = FALSE;
15195 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015196#ifdef HAVE_FSYNC
15197 int do_fsync = p_fs;
15198#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015199 char_u *fname;
15200 FILE *fd;
15201 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015202 listitem_T *li;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015203 list_T *list = NULL;
15204 blob_T *blob = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015205
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015206 rettv->vval.v_number = -1;
Bram Moolenaar8c62a082019-02-08 14:34:10 +010015207 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015208 return;
15209
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015210 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015211 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015212 list = argvars[0].vval.v_list;
15213 if (list == NULL)
15214 return;
15215 for (li = list->lv_first; li != NULL; li = li->li_next)
15216 if (tv_get_string_chk(&li->li_tv) == NULL)
15217 return;
15218 }
15219 else if (argvars[0].v_type == VAR_BLOB)
15220 {
15221 blob = argvars[0].vval.v_blob;
15222 if (blob == NULL)
15223 return;
15224 }
15225 else
15226 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015227 semsg(_(e_invarg2), "writefile()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015228 return;
15229 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015230
15231 if (argvars[2].v_type != VAR_UNKNOWN)
15232 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015233 char_u *arg2 = tv_get_string_chk(&argvars[2]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015234
15235 if (arg2 == NULL)
15236 return;
15237 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015238 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015239 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015240 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015241#ifdef HAVE_FSYNC
15242 if (vim_strchr(arg2, 's') != NULL)
15243 do_fsync = TRUE;
15244 else if (vim_strchr(arg2, 'S') != NULL)
15245 do_fsync = FALSE;
15246#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015247 }
15248
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015249 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015250 if (fname == NULL)
15251 return;
15252
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015253 /* Always open the file in binary mode, library functions have a mind of
15254 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015255 if (*fname == NUL || (fd = mch_fopen((char *)fname,
15256 append ? APPENDBIN : WRITEBIN)) == NULL)
15257 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015258 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015259 ret = -1;
15260 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015261 else if (blob)
15262 {
15263 if (write_blob(fd, blob) == FAIL)
15264 ret = -1;
15265#ifdef HAVE_FSYNC
15266 else if (do_fsync)
15267 // Ignore the error, the user wouldn't know what to do about it.
15268 // May happen for a device.
Bram Moolenaara7870192019-02-14 12:56:36 +010015269 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015270#endif
15271 fclose(fd);
15272 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015273 else
15274 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015275 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015276 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015277#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010015278 else if (do_fsync)
15279 /* Ignore the error, the user wouldn't know what to do about it.
15280 * May happen for a device. */
Bram Moolenaara7870192019-02-14 12:56:36 +010015281 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015282#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015283 fclose(fd);
15284 }
15285
15286 rettv->vval.v_number = ret;
15287}
15288
15289/*
15290 * "xor(expr, expr)" function
15291 */
15292 static void
15293f_xor(typval_T *argvars, typval_T *rettv)
15294{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015295 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
15296 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015297}
15298
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015299#endif /* FEAT_EVAL */