blob: a4d6ebf64835adb09719ba9a4532b2d74cbcde35 [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 Moolenaar905dd902019-04-07 14:21:47 +02003795 exec_normal(TRUE, lowlevel, 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 Moolenaar16c34c32019-04-06 22:01:24 +02005988#if defined(FEAT_GUI) \
5989 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
5990 || defined(MSWIN)
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005991 {
5992 varnumber_T timeout = 100;
5993
5994 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005995 timeout = tv_get_number(&argvars[0]);
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02005996
5997 (void)ui_get_winpos(&x, &y, timeout);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005998 }
5999#endif
6000 list_append_number(rettv->vval.v_list, (varnumber_T)x);
6001 list_append_number(rettv->vval.v_list, (varnumber_T)y);
6002}
6003
6004
6005/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006006 * "getwinposx()" function
6007 */
6008 static void
6009f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
6010{
6011 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006012#if defined(FEAT_GUI) \
6013 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6014 || defined(MSWIN)
6015
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006016 {
6017 int x, y;
6018
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006019 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006020 rettv->vval.v_number = x;
6021 }
6022#endif
6023}
6024
6025/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006026 * "getwinposy()" function
6027 */
6028 static void
6029f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
6030{
6031 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006032#if defined(FEAT_GUI) \
6033 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6034 || defined(MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006035 {
6036 int x, y;
6037
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006038 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006039 rettv->vval.v_number = y;
6040 }
6041#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006042}
6043
6044/*
6045 * "getwinvar()" function
6046 */
6047 static void
6048f_getwinvar(typval_T *argvars, typval_T *rettv)
6049{
6050 getwinvar(argvars, rettv, 0);
6051}
6052
6053/*
6054 * "glob()" function
6055 */
6056 static void
6057f_glob(typval_T *argvars, typval_T *rettv)
6058{
6059 int options = WILD_SILENT|WILD_USE_NL;
6060 expand_T xpc;
6061 int error = FALSE;
6062
6063 /* When the optional second argument is non-zero, don't remove matches
6064 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6065 rettv->v_type = VAR_STRING;
6066 if (argvars[1].v_type != VAR_UNKNOWN)
6067 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006068 if (tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006069 options |= WILD_KEEP_ALL;
6070 if (argvars[2].v_type != VAR_UNKNOWN)
6071 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006072 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006073 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006074 if (argvars[3].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006075 && tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006076 options |= WILD_ALLLINKS;
6077 }
6078 }
6079 if (!error)
6080 {
6081 ExpandInit(&xpc);
6082 xpc.xp_context = EXPAND_FILES;
6083 if (p_wic)
6084 options += WILD_ICASE;
6085 if (rettv->v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006086 rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006087 NULL, options, WILD_ALL);
6088 else if (rettv_list_alloc(rettv) != FAIL)
6089 {
6090 int i;
6091
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006092 ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006093 NULL, options, WILD_ALL_KEEP);
6094 for (i = 0; i < xpc.xp_numfiles; i++)
6095 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
6096
6097 ExpandCleanup(&xpc);
6098 }
6099 }
6100 else
6101 rettv->vval.v_string = NULL;
6102}
6103
6104/*
6105 * "globpath()" function
6106 */
6107 static void
6108f_globpath(typval_T *argvars, typval_T *rettv)
6109{
6110 int flags = 0;
6111 char_u buf1[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006112 char_u *file = tv_get_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006113 int error = FALSE;
6114 garray_T ga;
6115 int i;
6116
6117 /* When the optional second argument is non-zero, don't remove matches
6118 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6119 rettv->v_type = VAR_STRING;
6120 if (argvars[2].v_type != VAR_UNKNOWN)
6121 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006122 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006123 flags |= WILD_KEEP_ALL;
6124 if (argvars[3].v_type != VAR_UNKNOWN)
6125 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006126 if (tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006127 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006128 if (argvars[4].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006129 && tv_get_number_chk(&argvars[4], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006130 flags |= WILD_ALLLINKS;
6131 }
6132 }
6133 if (file != NULL && !error)
6134 {
6135 ga_init2(&ga, (int)sizeof(char_u *), 10);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006136 globpath(tv_get_string(&argvars[0]), file, &ga, flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006137 if (rettv->v_type == VAR_STRING)
6138 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
6139 else if (rettv_list_alloc(rettv) != FAIL)
6140 for (i = 0; i < ga.ga_len; ++i)
6141 list_append_string(rettv->vval.v_list,
6142 ((char_u **)(ga.ga_data))[i], -1);
6143 ga_clear_strings(&ga);
6144 }
6145 else
6146 rettv->vval.v_string = NULL;
6147}
6148
6149/*
6150 * "glob2regpat()" function
6151 */
6152 static void
6153f_glob2regpat(typval_T *argvars, typval_T *rettv)
6154{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006155 char_u *pat = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006156
6157 rettv->v_type = VAR_STRING;
6158 rettv->vval.v_string = (pat == NULL)
6159 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
6160}
6161
6162/* for VIM_VERSION_ defines */
6163#include "version.h"
6164
6165/*
6166 * "has()" function
6167 */
6168 static void
6169f_has(typval_T *argvars, typval_T *rettv)
6170{
6171 int i;
6172 char_u *name;
6173 int n = FALSE;
6174 static char *(has_list[]) =
6175 {
6176#ifdef AMIGA
6177 "amiga",
6178# ifdef FEAT_ARP
6179 "arp",
6180# endif
6181#endif
6182#ifdef __BEOS__
6183 "beos",
6184#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006185#if defined(BSD) && !defined(MACOS_X)
6186 "bsd",
6187#endif
6188#ifdef hpux
6189 "hpux",
6190#endif
6191#ifdef __linux__
6192 "linux",
6193#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02006194#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01006195 "mac", /* Mac OS X (and, once, Mac OS Classic) */
6196 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02006197# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01006198 "macunix", /* Mac OS X, with the darwin feature */
6199 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02006200# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006201#endif
6202#ifdef __QNX__
6203 "qnx",
6204#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006205#ifdef SUN_SYSTEM
6206 "sun",
6207#else
6208 "moon",
6209#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006210#ifdef UNIX
6211 "unix",
6212#endif
6213#ifdef VMS
6214 "vms",
6215#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006216#ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006217 "win32",
6218#endif
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006219#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006220 "win32unix",
6221#endif
Bram Moolenaar44b443c2019-02-18 22:14:18 +01006222#ifdef _WIN64
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006223 "win64",
6224#endif
6225#ifdef EBCDIC
6226 "ebcdic",
6227#endif
6228#ifndef CASE_INSENSITIVE_FILENAME
6229 "fname_case",
6230#endif
6231#ifdef HAVE_ACL
6232 "acl",
6233#endif
6234#ifdef FEAT_ARABIC
6235 "arabic",
6236#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006237 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006238#ifdef FEAT_AUTOCHDIR
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006239 "autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006240#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006241#ifdef FEAT_AUTOSERVERNAME
6242 "autoservername",
6243#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006244#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006245 "balloon_eval",
Bram Moolenaar4f974752019-02-17 17:44:42 +01006246# ifndef FEAT_GUI_MSWIN /* other GUIs always have multiline balloons */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006247 "balloon_multiline",
6248# endif
6249#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006250#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006251 "balloon_eval_term",
6252#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006253#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6254 "builtin_terms",
6255# ifdef ALL_BUILTIN_TCAPS
6256 "all_builtin_terms",
6257# endif
6258#endif
6259#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01006260 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006261 || defined(FEAT_GUI_MOTIF))
6262 "browsefilter",
6263#endif
6264#ifdef FEAT_BYTEOFF
6265 "byte_offset",
6266#endif
6267#ifdef FEAT_JOB_CHANNEL
6268 "channel",
6269#endif
6270#ifdef FEAT_CINDENT
6271 "cindent",
6272#endif
6273#ifdef FEAT_CLIENTSERVER
6274 "clientserver",
6275#endif
6276#ifdef FEAT_CLIPBOARD
6277 "clipboard",
6278#endif
6279#ifdef FEAT_CMDL_COMPL
6280 "cmdline_compl",
6281#endif
6282#ifdef FEAT_CMDHIST
6283 "cmdline_hist",
6284#endif
6285#ifdef FEAT_COMMENTS
6286 "comments",
6287#endif
6288#ifdef FEAT_CONCEAL
6289 "conceal",
6290#endif
6291#ifdef FEAT_CRYPT
6292 "cryptv",
6293 "crypt-blowfish",
6294 "crypt-blowfish2",
6295#endif
6296#ifdef FEAT_CSCOPE
6297 "cscope",
6298#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006299 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006300#ifdef CURSOR_SHAPE
6301 "cursorshape",
6302#endif
6303#ifdef DEBUG
6304 "debug",
6305#endif
6306#ifdef FEAT_CON_DIALOG
6307 "dialog_con",
6308#endif
6309#ifdef FEAT_GUI_DIALOG
6310 "dialog_gui",
6311#endif
6312#ifdef FEAT_DIFF
6313 "diff",
6314#endif
6315#ifdef FEAT_DIGRAPHS
6316 "digraphs",
6317#endif
6318#ifdef FEAT_DIRECTX
6319 "directx",
6320#endif
6321#ifdef FEAT_DND
6322 "dnd",
6323#endif
6324#ifdef FEAT_EMACS_TAGS
6325 "emacs_tags",
6326#endif
6327 "eval", /* always present, of course! */
6328 "ex_extra", /* graduated feature */
6329#ifdef FEAT_SEARCH_EXTRA
6330 "extra_search",
6331#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006332#ifdef FEAT_SEARCHPATH
6333 "file_in_path",
6334#endif
6335#ifdef FEAT_FILTERPIPE
6336 "filterpipe",
6337#endif
6338#ifdef FEAT_FIND_ID
6339 "find_in_path",
6340#endif
6341#ifdef FEAT_FLOAT
6342 "float",
6343#endif
6344#ifdef FEAT_FOLDING
6345 "folding",
6346#endif
6347#ifdef FEAT_FOOTER
6348 "footer",
6349#endif
6350#if !defined(USE_SYSTEM) && defined(UNIX)
6351 "fork",
6352#endif
6353#ifdef FEAT_GETTEXT
6354 "gettext",
6355#endif
6356#ifdef FEAT_GUI
6357 "gui",
6358#endif
6359#ifdef FEAT_GUI_ATHENA
6360# ifdef FEAT_GUI_NEXTAW
6361 "gui_neXtaw",
6362# else
6363 "gui_athena",
6364# endif
6365#endif
6366#ifdef FEAT_GUI_GTK
6367 "gui_gtk",
6368# ifdef USE_GTK3
6369 "gui_gtk3",
6370# else
6371 "gui_gtk2",
6372# endif
6373#endif
6374#ifdef FEAT_GUI_GNOME
6375 "gui_gnome",
6376#endif
6377#ifdef FEAT_GUI_MAC
6378 "gui_mac",
6379#endif
6380#ifdef FEAT_GUI_MOTIF
6381 "gui_motif",
6382#endif
6383#ifdef FEAT_GUI_PHOTON
6384 "gui_photon",
6385#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006386#ifdef FEAT_GUI_MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006387 "gui_win32",
6388#endif
6389#ifdef FEAT_HANGULIN
6390 "hangul_input",
6391#endif
6392#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6393 "iconv",
6394#endif
6395#ifdef FEAT_INS_EXPAND
6396 "insert_expand",
6397#endif
6398#ifdef FEAT_JOB_CHANNEL
6399 "job",
6400#endif
6401#ifdef FEAT_JUMPLIST
6402 "jumplist",
6403#endif
6404#ifdef FEAT_KEYMAP
6405 "keymap",
6406#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006407 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006408#ifdef FEAT_LANGMAP
6409 "langmap",
6410#endif
6411#ifdef FEAT_LIBCALL
6412 "libcall",
6413#endif
6414#ifdef FEAT_LINEBREAK
6415 "linebreak",
6416#endif
6417#ifdef FEAT_LISP
6418 "lispindent",
6419#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006420 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006421#ifdef FEAT_LOCALMAP
6422 "localmap",
6423#endif
6424#ifdef FEAT_LUA
6425# ifndef DYNAMIC_LUA
6426 "lua",
6427# endif
6428#endif
6429#ifdef FEAT_MENU
6430 "menu",
6431#endif
6432#ifdef FEAT_SESSION
6433 "mksession",
6434#endif
6435#ifdef FEAT_MODIFY_FNAME
6436 "modify_fname",
6437#endif
6438#ifdef FEAT_MOUSE
6439 "mouse",
6440#endif
6441#ifdef FEAT_MOUSESHAPE
6442 "mouseshape",
6443#endif
6444#if defined(UNIX) || defined(VMS)
6445# ifdef FEAT_MOUSE_DEC
6446 "mouse_dec",
6447# endif
6448# ifdef FEAT_MOUSE_GPM
6449 "mouse_gpm",
6450# endif
6451# ifdef FEAT_MOUSE_JSB
6452 "mouse_jsbterm",
6453# endif
6454# ifdef FEAT_MOUSE_NET
6455 "mouse_netterm",
6456# endif
6457# ifdef FEAT_MOUSE_PTERM
6458 "mouse_pterm",
6459# endif
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01006460# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006461 "mouse_sgr",
6462# endif
6463# ifdef FEAT_SYSMOUSE
6464 "mouse_sysmouse",
6465# endif
6466# ifdef FEAT_MOUSE_URXVT
6467 "mouse_urxvt",
6468# endif
6469# ifdef FEAT_MOUSE_XTERM
6470 "mouse_xterm",
6471# endif
6472#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006473 "multi_byte",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006474#ifdef FEAT_MBYTE_IME
6475 "multi_byte_ime",
6476#endif
6477#ifdef FEAT_MULTI_LANG
6478 "multi_lang",
6479#endif
6480#ifdef FEAT_MZSCHEME
6481#ifndef DYNAMIC_MZSCHEME
6482 "mzscheme",
6483#endif
6484#endif
6485#ifdef FEAT_NUM64
6486 "num64",
6487#endif
6488#ifdef FEAT_OLE
6489 "ole",
6490#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006491#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006492 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006493#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006494#ifdef FEAT_PATH_EXTRA
6495 "path_extra",
6496#endif
6497#ifdef FEAT_PERL
6498#ifndef DYNAMIC_PERL
6499 "perl",
6500#endif
6501#endif
6502#ifdef FEAT_PERSISTENT_UNDO
6503 "persistent_undo",
6504#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006505#if defined(FEAT_PYTHON)
6506 "python_compiled",
6507# if defined(DYNAMIC_PYTHON)
6508 "python_dynamic",
6509# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006510 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006511 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006512# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006513#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006514#if defined(FEAT_PYTHON3)
6515 "python3_compiled",
6516# if defined(DYNAMIC_PYTHON3)
6517 "python3_dynamic",
6518# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006519 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006520 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006521# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006522#endif
6523#ifdef FEAT_POSTSCRIPT
6524 "postscript",
6525#endif
6526#ifdef FEAT_PRINTER
6527 "printer",
6528#endif
6529#ifdef FEAT_PROFILE
6530 "profile",
6531#endif
6532#ifdef FEAT_RELTIME
6533 "reltime",
6534#endif
6535#ifdef FEAT_QUICKFIX
6536 "quickfix",
6537#endif
6538#ifdef FEAT_RIGHTLEFT
6539 "rightleft",
6540#endif
6541#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6542 "ruby",
6543#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006544 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006545#ifdef FEAT_CMDL_INFO
6546 "showcmd",
6547 "cmdline_info",
6548#endif
6549#ifdef FEAT_SIGNS
6550 "signs",
6551#endif
6552#ifdef FEAT_SMARTINDENT
6553 "smartindent",
6554#endif
6555#ifdef STARTUPTIME
6556 "startuptime",
6557#endif
6558#ifdef FEAT_STL_OPT
6559 "statusline",
6560#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006561#ifdef FEAT_NETBEANS_INTG
6562 "netbeans_intg",
6563#endif
6564#ifdef FEAT_SPELL
6565 "spell",
6566#endif
6567#ifdef FEAT_SYN_HL
6568 "syntax",
6569#endif
6570#if defined(USE_SYSTEM) || !defined(UNIX)
6571 "system",
6572#endif
6573#ifdef FEAT_TAG_BINS
6574 "tag_binary",
6575#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006576#ifdef FEAT_TCL
6577# ifndef DYNAMIC_TCL
6578 "tcl",
6579# endif
6580#endif
6581#ifdef FEAT_TERMGUICOLORS
6582 "termguicolors",
6583#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006584#if defined(FEAT_TERMINAL) && !defined(MSWIN)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006585 "terminal",
6586#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006587#ifdef TERMINFO
6588 "terminfo",
6589#endif
6590#ifdef FEAT_TERMRESPONSE
6591 "termresponse",
6592#endif
6593#ifdef FEAT_TEXTOBJ
6594 "textobjects",
6595#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006596#ifdef FEAT_TEXT_PROP
6597 "textprop",
6598#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006599#ifdef HAVE_TGETENT
6600 "tgetent",
6601#endif
6602#ifdef FEAT_TIMERS
6603 "timers",
6604#endif
6605#ifdef FEAT_TITLE
6606 "title",
6607#endif
6608#ifdef FEAT_TOOLBAR
6609 "toolbar",
6610#endif
6611#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6612 "unnamedplus",
6613#endif
6614#ifdef FEAT_USR_CMDS
6615 "user-commands", /* was accidentally included in 5.4 */
6616 "user_commands",
6617#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006618#ifdef FEAT_VARTABS
6619 "vartabs",
6620#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006621 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006622#ifdef FEAT_VIMINFO
6623 "viminfo",
6624#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006625 "vimscript-1",
6626 "vimscript-2",
Bram Moolenaar93a48792019-04-20 21:54:28 +02006627 "vimscript-3",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006628 "virtualedit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006629 "visual",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006630 "visualextra",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006631 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006632#ifdef FEAT_VTP
6633 "vtp",
6634#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006635#ifdef FEAT_WILDIGN
6636 "wildignore",
6637#endif
6638#ifdef FEAT_WILDMENU
6639 "wildmenu",
6640#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006641 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006642#ifdef FEAT_WAK
6643 "winaltkeys",
6644#endif
6645#ifdef FEAT_WRITEBACKUP
6646 "writebackup",
6647#endif
6648#ifdef FEAT_XIM
6649 "xim",
6650#endif
6651#ifdef FEAT_XFONTSET
6652 "xfontset",
6653#endif
6654#ifdef FEAT_XPM_W32
6655 "xpm",
6656 "xpm_w32", /* for backward compatibility */
6657#else
6658# if defined(HAVE_XPM)
6659 "xpm",
6660# endif
6661#endif
6662#ifdef USE_XSMP
6663 "xsmp",
6664#endif
6665#ifdef USE_XSMP_INTERACT
6666 "xsmp_interact",
6667#endif
6668#ifdef FEAT_XCLIPBOARD
6669 "xterm_clipboard",
6670#endif
6671#ifdef FEAT_XTERM_SAVE
6672 "xterm_save",
6673#endif
6674#if defined(UNIX) && defined(FEAT_X11)
6675 "X11",
6676#endif
6677 NULL
6678 };
6679
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006680 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006681 for (i = 0; has_list[i] != NULL; ++i)
6682 if (STRICMP(name, has_list[i]) == 0)
6683 {
6684 n = TRUE;
6685 break;
6686 }
6687
6688 if (n == FALSE)
6689 {
6690 if (STRNICMP(name, "patch", 5) == 0)
6691 {
6692 if (name[5] == '-'
6693 && STRLEN(name) >= 11
6694 && vim_isdigit(name[6])
6695 && vim_isdigit(name[8])
6696 && vim_isdigit(name[10]))
6697 {
6698 int major = atoi((char *)name + 6);
6699 int minor = atoi((char *)name + 8);
6700
6701 /* Expect "patch-9.9.01234". */
6702 n = (major < VIM_VERSION_MAJOR
6703 || (major == VIM_VERSION_MAJOR
6704 && (minor < VIM_VERSION_MINOR
6705 || (minor == VIM_VERSION_MINOR
6706 && has_patch(atoi((char *)name + 10))))));
6707 }
6708 else
6709 n = has_patch(atoi((char *)name + 5));
6710 }
6711 else if (STRICMP(name, "vim_starting") == 0)
6712 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006713 else if (STRICMP(name, "ttyin") == 0)
6714 n = mch_input_isatty();
6715 else if (STRICMP(name, "ttyout") == 0)
6716 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006717 else if (STRICMP(name, "multi_byte_encoding") == 0)
6718 n = has_mbyte;
Bram Moolenaar4f974752019-02-17 17:44:42 +01006719#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006720 else if (STRICMP(name, "balloon_multiline") == 0)
6721 n = multiline_balloon_available();
6722#endif
6723#ifdef DYNAMIC_TCL
6724 else if (STRICMP(name, "tcl") == 0)
6725 n = tcl_enabled(FALSE);
6726#endif
6727#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6728 else if (STRICMP(name, "iconv") == 0)
6729 n = iconv_enabled(FALSE);
6730#endif
6731#ifdef DYNAMIC_LUA
6732 else if (STRICMP(name, "lua") == 0)
6733 n = lua_enabled(FALSE);
6734#endif
6735#ifdef DYNAMIC_MZSCHEME
6736 else if (STRICMP(name, "mzscheme") == 0)
6737 n = mzscheme_enabled(FALSE);
6738#endif
6739#ifdef DYNAMIC_RUBY
6740 else if (STRICMP(name, "ruby") == 0)
6741 n = ruby_enabled(FALSE);
6742#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006743#ifdef DYNAMIC_PYTHON
6744 else if (STRICMP(name, "python") == 0)
6745 n = python_enabled(FALSE);
6746#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006747#ifdef DYNAMIC_PYTHON3
6748 else if (STRICMP(name, "python3") == 0)
6749 n = python3_enabled(FALSE);
6750#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006751#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6752 else if (STRICMP(name, "pythonx") == 0)
6753 {
6754# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6755 if (p_pyx == 0)
6756 n = python3_enabled(FALSE) || python_enabled(FALSE);
6757 else if (p_pyx == 3)
6758 n = python3_enabled(FALSE);
6759 else if (p_pyx == 2)
6760 n = python_enabled(FALSE);
6761# elif defined(DYNAMIC_PYTHON)
6762 n = python_enabled(FALSE);
6763# elif defined(DYNAMIC_PYTHON3)
6764 n = python3_enabled(FALSE);
6765# endif
6766 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006767#endif
6768#ifdef DYNAMIC_PERL
6769 else if (STRICMP(name, "perl") == 0)
6770 n = perl_enabled(FALSE);
6771#endif
6772#ifdef FEAT_GUI
6773 else if (STRICMP(name, "gui_running") == 0)
6774 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006775# ifdef FEAT_BROWSE
6776 else if (STRICMP(name, "browse") == 0)
6777 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6778# endif
6779#endif
6780#ifdef FEAT_SYN_HL
6781 else if (STRICMP(name, "syntax_items") == 0)
6782 n = syntax_present(curwin);
6783#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006784#ifdef FEAT_VTP
6785 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02006786 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006787#endif
6788#ifdef FEAT_NETBEANS_INTG
6789 else if (STRICMP(name, "netbeans_enabled") == 0)
6790 n = netbeans_active();
6791#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006792#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02006793 else if (STRICMP(name, "terminal") == 0)
6794 n = terminal_enabled();
6795#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006796#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006797 else if (STRICMP(name, "conpty") == 0)
6798 n = use_conpty();
6799#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006800 }
6801
6802 rettv->vval.v_number = n;
6803}
6804
6805/*
6806 * "has_key()" function
6807 */
6808 static void
6809f_has_key(typval_T *argvars, typval_T *rettv)
6810{
6811 if (argvars[0].v_type != VAR_DICT)
6812 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006813 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006814 return;
6815 }
6816 if (argvars[0].vval.v_dict == NULL)
6817 return;
6818
6819 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006820 tv_get_string(&argvars[1]), -1) != NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006821}
6822
6823/*
6824 * "haslocaldir()" function
6825 */
6826 static void
6827f_haslocaldir(typval_T *argvars, typval_T *rettv)
6828{
6829 win_T *wp = NULL;
6830
6831 wp = find_tabwin(&argvars[0], &argvars[1]);
6832 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6833}
6834
6835/*
6836 * "hasmapto()" function
6837 */
6838 static void
6839f_hasmapto(typval_T *argvars, typval_T *rettv)
6840{
6841 char_u *name;
6842 char_u *mode;
6843 char_u buf[NUMBUFLEN];
6844 int abbr = FALSE;
6845
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006846 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006847 if (argvars[1].v_type == VAR_UNKNOWN)
6848 mode = (char_u *)"nvo";
6849 else
6850 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006851 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006852 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006853 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006854 }
6855
6856 if (map_to_exists(name, mode, abbr))
6857 rettv->vval.v_number = TRUE;
6858 else
6859 rettv->vval.v_number = FALSE;
6860}
6861
6862/*
6863 * "histadd()" function
6864 */
6865 static void
6866f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6867{
6868#ifdef FEAT_CMDHIST
6869 int histype;
6870 char_u *str;
6871 char_u buf[NUMBUFLEN];
6872#endif
6873
6874 rettv->vval.v_number = FALSE;
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006875 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006876 return;
6877#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006878 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006879 histype = str != NULL ? get_histtype(str) : -1;
6880 if (histype >= 0)
6881 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006882 str = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006883 if (*str != NUL)
6884 {
6885 init_history();
6886 add_to_history(histype, str, FALSE, NUL);
6887 rettv->vval.v_number = TRUE;
6888 return;
6889 }
6890 }
6891#endif
6892}
6893
6894/*
6895 * "histdel()" function
6896 */
6897 static void
6898f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6899{
6900#ifdef FEAT_CMDHIST
6901 int n;
6902 char_u buf[NUMBUFLEN];
6903 char_u *str;
6904
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006905 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006906 if (str == NULL)
6907 n = 0;
6908 else if (argvars[1].v_type == VAR_UNKNOWN)
6909 /* only one argument: clear entire history */
6910 n = clr_history(get_histtype(str));
6911 else if (argvars[1].v_type == VAR_NUMBER)
6912 /* index given: remove that entry */
6913 n = del_history_idx(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006914 (int)tv_get_number(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006915 else
6916 /* string given: remove all matching entries */
6917 n = del_history_entry(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006918 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006919 rettv->vval.v_number = n;
6920#endif
6921}
6922
6923/*
6924 * "histget()" function
6925 */
6926 static void
6927f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6928{
6929#ifdef FEAT_CMDHIST
6930 int type;
6931 int idx;
6932 char_u *str;
6933
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006934 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006935 if (str == NULL)
6936 rettv->vval.v_string = NULL;
6937 else
6938 {
6939 type = get_histtype(str);
6940 if (argvars[1].v_type == VAR_UNKNOWN)
6941 idx = get_history_idx(type);
6942 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006943 idx = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006944 /* -1 on type error */
6945 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6946 }
6947#else
6948 rettv->vval.v_string = NULL;
6949#endif
6950 rettv->v_type = VAR_STRING;
6951}
6952
6953/*
6954 * "histnr()" function
6955 */
6956 static void
6957f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6958{
6959 int i;
6960
6961#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006962 char_u *history = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006963
6964 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6965 if (i >= HIST_CMD && i < HIST_COUNT)
6966 i = get_history_idx(i);
6967 else
6968#endif
6969 i = -1;
6970 rettv->vval.v_number = i;
6971}
6972
6973/*
6974 * "highlightID(name)" function
6975 */
6976 static void
6977f_hlID(typval_T *argvars, typval_T *rettv)
6978{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006979 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006980}
6981
6982/*
6983 * "highlight_exists()" function
6984 */
6985 static void
6986f_hlexists(typval_T *argvars, typval_T *rettv)
6987{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006988 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006989}
6990
6991/*
6992 * "hostname()" function
6993 */
6994 static void
6995f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6996{
6997 char_u hostname[256];
6998
6999 mch_get_host_name(hostname, 256);
7000 rettv->v_type = VAR_STRING;
7001 rettv->vval.v_string = vim_strsave(hostname);
7002}
7003
7004/*
7005 * iconv() function
7006 */
7007 static void
7008f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
7009{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007010 char_u buf1[NUMBUFLEN];
7011 char_u buf2[NUMBUFLEN];
7012 char_u *from, *to, *str;
7013 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007014
7015 rettv->v_type = VAR_STRING;
7016 rettv->vval.v_string = NULL;
7017
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007018 str = tv_get_string(&argvars[0]);
7019 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
7020 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007021 vimconv.vc_type = CONV_NONE;
7022 convert_setup(&vimconv, from, to);
7023
7024 /* If the encodings are equal, no conversion needed. */
7025 if (vimconv.vc_type == CONV_NONE)
7026 rettv->vval.v_string = vim_strsave(str);
7027 else
7028 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
7029
7030 convert_setup(&vimconv, NULL, NULL);
7031 vim_free(from);
7032 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007033}
7034
7035/*
7036 * "indent()" function
7037 */
7038 static void
7039f_indent(typval_T *argvars, typval_T *rettv)
7040{
7041 linenr_T lnum;
7042
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007043 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007044 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7045 rettv->vval.v_number = get_indent_lnum(lnum);
7046 else
7047 rettv->vval.v_number = -1;
7048}
7049
7050/*
7051 * "index()" function
7052 */
7053 static void
7054f_index(typval_T *argvars, typval_T *rettv)
7055{
7056 list_T *l;
7057 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007058 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007059 long idx = 0;
7060 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007061 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007062
7063 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007064 if (argvars[0].v_type == VAR_BLOB)
7065 {
7066 typval_T tv;
7067 int start = 0;
7068
7069 if (argvars[2].v_type != VAR_UNKNOWN)
7070 {
7071 start = tv_get_number_chk(&argvars[2], &error);
7072 if (error)
7073 return;
7074 }
7075 b = argvars[0].vval.v_blob;
7076 if (b == NULL)
7077 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01007078 if (start < 0)
7079 {
7080 start = blob_len(b) + start;
7081 if (start < 0)
7082 start = 0;
7083 }
7084
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007085 for (idx = start; idx < blob_len(b); ++idx)
7086 {
7087 tv.v_type = VAR_NUMBER;
7088 tv.vval.v_number = blob_get(b, idx);
7089 if (tv_equal(&tv, &argvars[1], ic, FALSE))
7090 {
7091 rettv->vval.v_number = idx;
7092 return;
7093 }
7094 }
7095 return;
7096 }
7097 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007098 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007099 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007100 return;
7101 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007102
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007103 l = argvars[0].vval.v_list;
7104 if (l != NULL)
7105 {
7106 item = l->lv_first;
7107 if (argvars[2].v_type != VAR_UNKNOWN)
7108 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007109 /* Start at specified item. Use the cached index that list_find()
7110 * sets, so that a negative number also works. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007111 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007112 idx = l->lv_idx;
7113 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007114 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007115 if (error)
7116 item = NULL;
7117 }
7118
7119 for ( ; item != NULL; item = item->li_next, ++idx)
7120 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
7121 {
7122 rettv->vval.v_number = idx;
7123 break;
7124 }
7125 }
7126}
7127
7128static int inputsecret_flag = 0;
7129
7130/*
7131 * "input()" function
7132 * Also handles inputsecret() when inputsecret is set.
7133 */
7134 static void
7135f_input(typval_T *argvars, typval_T *rettv)
7136{
7137 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
7138}
7139
7140/*
7141 * "inputdialog()" function
7142 */
7143 static void
7144f_inputdialog(typval_T *argvars, typval_T *rettv)
7145{
7146#if defined(FEAT_GUI_TEXTDIALOG)
7147 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
7148 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
7149 {
7150 char_u *message;
7151 char_u buf[NUMBUFLEN];
7152 char_u *defstr = (char_u *)"";
7153
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007154 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007155 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007156 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007157 vim_strncpy(IObuff, defstr, IOSIZE - 1);
7158 else
7159 IObuff[0] = NUL;
7160 if (message != NULL && defstr != NULL
7161 && do_dialog(VIM_QUESTION, NULL, message,
7162 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
7163 rettv->vval.v_string = vim_strsave(IObuff);
7164 else
7165 {
7166 if (message != NULL && defstr != NULL
7167 && argvars[1].v_type != VAR_UNKNOWN
7168 && argvars[2].v_type != VAR_UNKNOWN)
7169 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007170 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007171 else
7172 rettv->vval.v_string = NULL;
7173 }
7174 rettv->v_type = VAR_STRING;
7175 }
7176 else
7177#endif
7178 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
7179}
7180
7181/*
7182 * "inputlist()" function
7183 */
7184 static void
7185f_inputlist(typval_T *argvars, typval_T *rettv)
7186{
7187 listitem_T *li;
7188 int selected;
7189 int mouse_used;
7190
7191#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007192 /* While starting up, there is no place to enter text. When running tests
7193 * with --not-a-term we assume feedkeys() will be used. */
7194 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007195 return;
7196#endif
7197 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
7198 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007199 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007200 return;
7201 }
7202
7203 msg_start();
7204 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
7205 lines_left = Rows; /* avoid more prompt */
7206 msg_scroll = TRUE;
7207 msg_clr_eos();
7208
7209 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
7210 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007211 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007212 msg_putchar('\n');
7213 }
7214
7215 /* Ask for choice. */
7216 selected = prompt_for_number(&mouse_used);
7217 if (mouse_used)
7218 selected -= lines_left;
7219
7220 rettv->vval.v_number = selected;
7221}
7222
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007223static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7224
7225/*
7226 * "inputrestore()" function
7227 */
7228 static void
7229f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7230{
7231 if (ga_userinput.ga_len > 0)
7232 {
7233 --ga_userinput.ga_len;
7234 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7235 + ga_userinput.ga_len);
7236 /* default return is zero == OK */
7237 }
7238 else if (p_verbose > 1)
7239 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007240 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007241 rettv->vval.v_number = 1; /* Failed */
7242 }
7243}
7244
7245/*
7246 * "inputsave()" function
7247 */
7248 static void
7249f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7250{
7251 /* Add an entry to the stack of typeahead storage. */
7252 if (ga_grow(&ga_userinput, 1) == OK)
7253 {
7254 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7255 + ga_userinput.ga_len);
7256 ++ga_userinput.ga_len;
7257 /* default return is zero == OK */
7258 }
7259 else
7260 rettv->vval.v_number = 1; /* Failed */
7261}
7262
7263/*
7264 * "inputsecret()" function
7265 */
7266 static void
7267f_inputsecret(typval_T *argvars, typval_T *rettv)
7268{
7269 ++cmdline_star;
7270 ++inputsecret_flag;
7271 f_input(argvars, rettv);
7272 --cmdline_star;
7273 --inputsecret_flag;
7274}
7275
7276/*
7277 * "insert()" function
7278 */
7279 static void
7280f_insert(typval_T *argvars, typval_T *rettv)
7281{
7282 long before = 0;
7283 listitem_T *item;
7284 list_T *l;
7285 int error = FALSE;
7286
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007287 if (argvars[0].v_type == VAR_BLOB)
7288 {
7289 int val, len;
7290 char_u *p;
7291
7292 len = blob_len(argvars[0].vval.v_blob);
7293 if (argvars[2].v_type != VAR_UNKNOWN)
7294 {
7295 before = (long)tv_get_number_chk(&argvars[2], &error);
7296 if (error)
7297 return; // type error; errmsg already given
7298 if (before < 0 || before > len)
7299 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007300 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007301 return;
7302 }
7303 }
7304 val = tv_get_number_chk(&argvars[1], &error);
7305 if (error)
7306 return;
7307 if (val < 0 || val > 255)
7308 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007309 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007310 return;
7311 }
7312
7313 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
7314 return;
7315 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
7316 mch_memmove(p + before + 1, p + before, (size_t)len - before);
7317 *(p + before) = val;
7318 ++argvars[0].vval.v_blob->bv_ga.ga_len;
7319
7320 copy_tv(&argvars[0], rettv);
7321 }
7322 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007323 semsg(_(e_listblobarg), "insert()");
Bram Moolenaar05c00c02019-02-11 22:00:11 +01007324 else if ((l = argvars[0].vval.v_list) != NULL
7325 && !var_check_lock(l->lv_lock,
7326 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007327 {
7328 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007329 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007330 if (error)
7331 return; /* type error; errmsg already given */
7332
7333 if (before == l->lv_len)
7334 item = NULL;
7335 else
7336 {
7337 item = list_find(l, before);
7338 if (item == NULL)
7339 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007340 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007341 l = NULL;
7342 }
7343 }
7344 if (l != NULL)
7345 {
7346 list_insert_tv(l, &argvars[1], item);
7347 copy_tv(&argvars[0], rettv);
7348 }
7349 }
7350}
7351
7352/*
7353 * "invert(expr)" function
7354 */
7355 static void
7356f_invert(typval_T *argvars, typval_T *rettv)
7357{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007358 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007359}
7360
7361/*
7362 * "isdirectory()" function
7363 */
7364 static void
7365f_isdirectory(typval_T *argvars, typval_T *rettv)
7366{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007367 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007368}
7369
7370/*
7371 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7372 * or it refers to a List or Dictionary that is locked.
7373 */
7374 static int
7375tv_islocked(typval_T *tv)
7376{
7377 return (tv->v_lock & VAR_LOCKED)
7378 || (tv->v_type == VAR_LIST
7379 && tv->vval.v_list != NULL
7380 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7381 || (tv->v_type == VAR_DICT
7382 && tv->vval.v_dict != NULL
7383 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7384}
7385
7386/*
7387 * "islocked()" function
7388 */
7389 static void
7390f_islocked(typval_T *argvars, typval_T *rettv)
7391{
7392 lval_T lv;
7393 char_u *end;
7394 dictitem_T *di;
7395
7396 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007397 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007398 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007399 if (end != NULL && lv.ll_name != NULL)
7400 {
7401 if (*end != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007402 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007403 else
7404 {
7405 if (lv.ll_tv == NULL)
7406 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007407 di = find_var(lv.ll_name, NULL, TRUE);
7408 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007409 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007410 /* Consider a variable locked when:
7411 * 1. the variable itself is locked
7412 * 2. the value of the variable is locked.
7413 * 3. the List or Dict value is locked.
7414 */
7415 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7416 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007417 }
7418 }
7419 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007420 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007421 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007422 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007423 else if (lv.ll_list != NULL)
7424 /* List item. */
7425 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7426 else
7427 /* Dictionary item. */
7428 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7429 }
7430 }
7431
7432 clear_lval(&lv);
7433}
7434
7435#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7436/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02007437 * "isinf()" function
7438 */
7439 static void
7440f_isinf(typval_T *argvars, typval_T *rettv)
7441{
7442 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
7443 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
7444}
7445
7446/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007447 * "isnan()" function
7448 */
7449 static void
7450f_isnan(typval_T *argvars, typval_T *rettv)
7451{
7452 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7453 && isnan(argvars[0].vval.v_float);
7454}
7455#endif
7456
7457/*
7458 * "items(dict)" function
7459 */
7460 static void
7461f_items(typval_T *argvars, typval_T *rettv)
7462{
7463 dict_list(argvars, rettv, 2);
7464}
7465
7466#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
7467/*
7468 * Get the job from the argument.
7469 * Returns NULL if the job is invalid.
7470 */
7471 static job_T *
7472get_job_arg(typval_T *tv)
7473{
7474 job_T *job;
7475
7476 if (tv->v_type != VAR_JOB)
7477 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007478 semsg(_(e_invarg2), tv_get_string(tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007479 return NULL;
7480 }
7481 job = tv->vval.v_job;
7482
7483 if (job == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007484 emsg(_("E916: not a valid job"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007485 return job;
7486}
7487
7488/*
7489 * "job_getchannel()" function
7490 */
7491 static void
7492f_job_getchannel(typval_T *argvars, typval_T *rettv)
7493{
7494 job_T *job = get_job_arg(&argvars[0]);
7495
7496 if (job != NULL)
7497 {
7498 rettv->v_type = VAR_CHANNEL;
7499 rettv->vval.v_channel = job->jv_channel;
7500 if (job->jv_channel != NULL)
7501 ++job->jv_channel->ch_refcount;
7502 }
7503}
7504
7505/*
7506 * "job_info()" function
7507 */
7508 static void
7509f_job_info(typval_T *argvars, typval_T *rettv)
7510{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007511 if (argvars[0].v_type != VAR_UNKNOWN)
7512 {
7513 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007514
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007515 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7516 job_info(job, rettv->vval.v_dict);
7517 }
7518 else if (rettv_list_alloc(rettv) == OK)
7519 job_info_all(rettv->vval.v_list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007520}
7521
7522/*
7523 * "job_setoptions()" function
7524 */
7525 static void
7526f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7527{
7528 job_T *job = get_job_arg(&argvars[0]);
7529 jobopt_T opt;
7530
7531 if (job == NULL)
7532 return;
7533 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007534 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007535 job_set_options(job, &opt);
7536 free_job_options(&opt);
7537}
7538
7539/*
7540 * "job_start()" function
7541 */
7542 static void
7543f_job_start(typval_T *argvars, typval_T *rettv)
7544{
7545 rettv->v_type = VAR_JOB;
7546 if (check_restricted() || check_secure())
7547 return;
Bram Moolenaar493359e2018-06-12 20:25:52 +02007548 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007549}
7550
7551/*
7552 * "job_status()" function
7553 */
7554 static void
7555f_job_status(typval_T *argvars, typval_T *rettv)
7556{
7557 job_T *job = get_job_arg(&argvars[0]);
7558
7559 if (job != NULL)
7560 {
7561 rettv->v_type = VAR_STRING;
7562 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7563 }
7564}
7565
7566/*
7567 * "job_stop()" function
7568 */
7569 static void
7570f_job_stop(typval_T *argvars, typval_T *rettv)
7571{
7572 job_T *job = get_job_arg(&argvars[0]);
7573
7574 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007575 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007576}
7577#endif
7578
7579/*
7580 * "join()" function
7581 */
7582 static void
7583f_join(typval_T *argvars, typval_T *rettv)
7584{
7585 garray_T ga;
7586 char_u *sep;
7587
7588 if (argvars[0].v_type != VAR_LIST)
7589 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007590 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007591 return;
7592 }
7593 if (argvars[0].vval.v_list == NULL)
7594 return;
7595 if (argvars[1].v_type == VAR_UNKNOWN)
7596 sep = (char_u *)" ";
7597 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007598 sep = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007599
7600 rettv->v_type = VAR_STRING;
7601
7602 if (sep != NULL)
7603 {
7604 ga_init2(&ga, (int)sizeof(char), 80);
7605 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7606 ga_append(&ga, NUL);
7607 rettv->vval.v_string = (char_u *)ga.ga_data;
7608 }
7609 else
7610 rettv->vval.v_string = NULL;
7611}
7612
7613/*
7614 * "js_decode()" function
7615 */
7616 static void
7617f_js_decode(typval_T *argvars, typval_T *rettv)
7618{
7619 js_read_T reader;
7620
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007621 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007622 reader.js_fill = NULL;
7623 reader.js_used = 0;
7624 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007625 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007626}
7627
7628/*
7629 * "js_encode()" function
7630 */
7631 static void
7632f_js_encode(typval_T *argvars, typval_T *rettv)
7633{
7634 rettv->v_type = VAR_STRING;
7635 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7636}
7637
7638/*
7639 * "json_decode()" function
7640 */
7641 static void
7642f_json_decode(typval_T *argvars, typval_T *rettv)
7643{
7644 js_read_T reader;
7645
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007646 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007647 reader.js_fill = NULL;
7648 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007649 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007650}
7651
7652/*
7653 * "json_encode()" function
7654 */
7655 static void
7656f_json_encode(typval_T *argvars, typval_T *rettv)
7657{
7658 rettv->v_type = VAR_STRING;
7659 rettv->vval.v_string = json_encode(&argvars[0], 0);
7660}
7661
7662/*
7663 * "keys()" function
7664 */
7665 static void
7666f_keys(typval_T *argvars, typval_T *rettv)
7667{
7668 dict_list(argvars, rettv, 0);
7669}
7670
7671/*
7672 * "last_buffer_nr()" function.
7673 */
7674 static void
7675f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7676{
7677 int n = 0;
7678 buf_T *buf;
7679
Bram Moolenaar29323592016-07-24 22:04:11 +02007680 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007681 if (n < buf->b_fnum)
7682 n = buf->b_fnum;
7683
7684 rettv->vval.v_number = n;
7685}
7686
7687/*
7688 * "len()" function
7689 */
7690 static void
7691f_len(typval_T *argvars, typval_T *rettv)
7692{
7693 switch (argvars[0].v_type)
7694 {
7695 case VAR_STRING:
7696 case VAR_NUMBER:
7697 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007698 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007699 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007700 case VAR_BLOB:
7701 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
7702 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007703 case VAR_LIST:
7704 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7705 break;
7706 case VAR_DICT:
7707 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7708 break;
7709 case VAR_UNKNOWN:
7710 case VAR_SPECIAL:
7711 case VAR_FLOAT:
7712 case VAR_FUNC:
7713 case VAR_PARTIAL:
7714 case VAR_JOB:
7715 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007716 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007717 break;
7718 }
7719}
7720
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007721 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007722libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007723{
7724#ifdef FEAT_LIBCALL
7725 char_u *string_in;
7726 char_u **string_result;
7727 int nr_result;
7728#endif
7729
7730 rettv->v_type = type;
7731 if (type != VAR_NUMBER)
7732 rettv->vval.v_string = NULL;
7733
7734 if (check_restricted() || check_secure())
7735 return;
7736
7737#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007738 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007739 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7740 {
7741 string_in = NULL;
7742 if (argvars[2].v_type == VAR_STRING)
7743 string_in = argvars[2].vval.v_string;
7744 if (type == VAR_NUMBER)
7745 string_result = NULL;
7746 else
7747 string_result = &rettv->vval.v_string;
7748 if (mch_libcall(argvars[0].vval.v_string,
7749 argvars[1].vval.v_string,
7750 string_in,
7751 argvars[2].vval.v_number,
7752 string_result,
7753 &nr_result) == OK
7754 && type == VAR_NUMBER)
7755 rettv->vval.v_number = nr_result;
7756 }
7757#endif
7758}
7759
7760/*
7761 * "libcall()" function
7762 */
7763 static void
7764f_libcall(typval_T *argvars, typval_T *rettv)
7765{
7766 libcall_common(argvars, rettv, VAR_STRING);
7767}
7768
7769/*
7770 * "libcallnr()" function
7771 */
7772 static void
7773f_libcallnr(typval_T *argvars, typval_T *rettv)
7774{
7775 libcall_common(argvars, rettv, VAR_NUMBER);
7776}
7777
7778/*
7779 * "line(string)" function
7780 */
7781 static void
7782f_line(typval_T *argvars, typval_T *rettv)
7783{
7784 linenr_T lnum = 0;
7785 pos_T *fp;
7786 int fnum;
7787
7788 fp = var2fpos(&argvars[0], TRUE, &fnum);
7789 if (fp != NULL)
7790 lnum = fp->lnum;
7791 rettv->vval.v_number = lnum;
7792}
7793
7794/*
7795 * "line2byte(lnum)" function
7796 */
7797 static void
7798f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7799{
7800#ifndef FEAT_BYTEOFF
7801 rettv->vval.v_number = -1;
7802#else
7803 linenr_T lnum;
7804
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007805 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007806 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7807 rettv->vval.v_number = -1;
7808 else
7809 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7810 if (rettv->vval.v_number >= 0)
7811 ++rettv->vval.v_number;
7812#endif
7813}
7814
7815/*
7816 * "lispindent(lnum)" function
7817 */
7818 static void
7819f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7820{
7821#ifdef FEAT_LISP
7822 pos_T pos;
7823 linenr_T lnum;
7824
7825 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007826 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007827 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7828 {
7829 curwin->w_cursor.lnum = lnum;
7830 rettv->vval.v_number = get_lisp_indent();
7831 curwin->w_cursor = pos;
7832 }
7833 else
7834#endif
7835 rettv->vval.v_number = -1;
7836}
7837
7838/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02007839 * "list2str()" function
7840 */
7841 static void
7842f_list2str(typval_T *argvars, typval_T *rettv)
7843{
7844 list_T *l;
7845 listitem_T *li;
7846 garray_T ga;
7847 int utf8 = FALSE;
7848
7849 rettv->v_type = VAR_STRING;
7850 rettv->vval.v_string = NULL;
7851 if (argvars[0].v_type != VAR_LIST)
7852 {
7853 emsg(_(e_invarg));
7854 return;
7855 }
7856
7857 l = argvars[0].vval.v_list;
7858 if (l == NULL)
7859 return; // empty list results in empty string
7860
7861 if (argvars[1].v_type != VAR_UNKNOWN)
7862 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
7863
7864 ga_init2(&ga, 1, 80);
7865 if (has_mbyte || utf8)
7866 {
7867 char_u buf[MB_MAXBYTES + 1];
7868 int (*char2bytes)(int, char_u *);
7869
7870 if (utf8 || enc_utf8)
7871 char2bytes = utf_char2bytes;
7872 else
7873 char2bytes = mb_char2bytes;
7874
7875 for (li = l->lv_first; li != NULL; li = li->li_next)
7876 {
7877 buf[(*char2bytes)(tv_get_number(&li->li_tv), buf)] = NUL;
7878 ga_concat(&ga, buf);
7879 }
7880 ga_append(&ga, NUL);
7881 }
7882 else if (ga_grow(&ga, list_len(l) + 1) == OK)
7883 {
7884 for (li = l->lv_first; li != NULL; li = li->li_next)
7885 ga_append(&ga, tv_get_number(&li->li_tv));
7886 ga_append(&ga, NUL);
7887 }
7888
7889 rettv->v_type = VAR_STRING;
7890 rettv->vval.v_string = ga.ga_data;
7891}
7892
7893/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007894 * "localtime()" function
7895 */
7896 static void
7897f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7898{
7899 rettv->vval.v_number = (varnumber_T)time(NULL);
7900}
7901
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007902 static void
7903get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7904{
7905 char_u *keys;
7906 char_u *which;
7907 char_u buf[NUMBUFLEN];
7908 char_u *keys_buf = NULL;
7909 char_u *rhs;
7910 int mode;
7911 int abbr = FALSE;
7912 int get_dict = FALSE;
7913 mapblock_T *mp;
7914 int buffer_local;
7915
7916 /* return empty string for failure */
7917 rettv->v_type = VAR_STRING;
7918 rettv->vval.v_string = NULL;
7919
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007920 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007921 if (*keys == NUL)
7922 return;
7923
7924 if (argvars[1].v_type != VAR_UNKNOWN)
7925 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007926 which = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007927 if (argvars[2].v_type != VAR_UNKNOWN)
7928 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007929 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007930 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007931 get_dict = (int)tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007932 }
7933 }
7934 else
7935 which = (char_u *)"";
7936 if (which == NULL)
7937 return;
7938
7939 mode = get_map_mode(&which, 0);
7940
7941 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7942 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7943 vim_free(keys_buf);
7944
7945 if (!get_dict)
7946 {
7947 /* Return a string. */
7948 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02007949 {
7950 if (*rhs == NUL)
7951 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
7952 else
7953 rettv->vval.v_string = str2special_save(rhs, FALSE);
7954 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007955
7956 }
7957 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7958 {
7959 /* Return a dictionary. */
7960 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7961 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7962 dict_T *dict = rettv->vval.v_dict;
7963
Bram Moolenaare0be1672018-07-08 16:50:37 +02007964 dict_add_string(dict, "lhs", lhs);
7965 dict_add_string(dict, "rhs", mp->m_orig_str);
7966 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
7967 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
7968 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007969 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
7970 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02007971 dict_add_number(dict, "buffer", (long)buffer_local);
7972 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
7973 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007974
7975 vim_free(lhs);
7976 vim_free(mapmode);
7977 }
7978}
7979
7980#ifdef FEAT_FLOAT
7981/*
7982 * "log()" function
7983 */
7984 static void
7985f_log(typval_T *argvars, typval_T *rettv)
7986{
7987 float_T f = 0.0;
7988
7989 rettv->v_type = VAR_FLOAT;
7990 if (get_float_arg(argvars, &f) == OK)
7991 rettv->vval.v_float = log(f);
7992 else
7993 rettv->vval.v_float = 0.0;
7994}
7995
7996/*
7997 * "log10()" function
7998 */
7999 static void
8000f_log10(typval_T *argvars, typval_T *rettv)
8001{
8002 float_T f = 0.0;
8003
8004 rettv->v_type = VAR_FLOAT;
8005 if (get_float_arg(argvars, &f) == OK)
8006 rettv->vval.v_float = log10(f);
8007 else
8008 rettv->vval.v_float = 0.0;
8009}
8010#endif
8011
8012#ifdef FEAT_LUA
8013/*
8014 * "luaeval()" function
8015 */
8016 static void
8017f_luaeval(typval_T *argvars, typval_T *rettv)
8018{
8019 char_u *str;
8020 char_u buf[NUMBUFLEN];
8021
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008022 if (check_restricted() || check_secure())
8023 return;
8024
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008025 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008026 do_luaeval(str, argvars + 1, rettv);
8027}
8028#endif
8029
8030/*
8031 * "map()" function
8032 */
8033 static void
8034f_map(typval_T *argvars, typval_T *rettv)
8035{
8036 filter_map(argvars, rettv, TRUE);
8037}
8038
8039/*
8040 * "maparg()" function
8041 */
8042 static void
8043f_maparg(typval_T *argvars, typval_T *rettv)
8044{
8045 get_maparg(argvars, rettv, TRUE);
8046}
8047
8048/*
8049 * "mapcheck()" function
8050 */
8051 static void
8052f_mapcheck(typval_T *argvars, typval_T *rettv)
8053{
8054 get_maparg(argvars, rettv, FALSE);
8055}
8056
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008057typedef enum
8058{
8059 MATCH_END, /* matchend() */
8060 MATCH_MATCH, /* match() */
8061 MATCH_STR, /* matchstr() */
8062 MATCH_LIST, /* matchlist() */
8063 MATCH_POS /* matchstrpos() */
8064} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008065
8066 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008067find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008068{
8069 char_u *str = NULL;
8070 long len = 0;
8071 char_u *expr = NULL;
8072 char_u *pat;
8073 regmatch_T regmatch;
8074 char_u patbuf[NUMBUFLEN];
8075 char_u strbuf[NUMBUFLEN];
8076 char_u *save_cpo;
8077 long start = 0;
8078 long nth = 1;
8079 colnr_T startcol = 0;
8080 int match = 0;
8081 list_T *l = NULL;
8082 listitem_T *li = NULL;
8083 long idx = 0;
8084 char_u *tofree = NULL;
8085
8086 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
8087 save_cpo = p_cpo;
8088 p_cpo = (char_u *)"";
8089
8090 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008091 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008092 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008093 /* type MATCH_LIST: return empty list when there are no matches.
8094 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008095 if (rettv_list_alloc(rettv) == FAIL)
8096 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008097 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008098 && (list_append_string(rettv->vval.v_list,
8099 (char_u *)"", 0) == FAIL
8100 || list_append_number(rettv->vval.v_list,
8101 (varnumber_T)-1) == FAIL
8102 || list_append_number(rettv->vval.v_list,
8103 (varnumber_T)-1) == FAIL
8104 || list_append_number(rettv->vval.v_list,
8105 (varnumber_T)-1) == FAIL))
8106 {
8107 list_free(rettv->vval.v_list);
8108 rettv->vval.v_list = NULL;
8109 goto theend;
8110 }
8111 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008112 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008113 {
8114 rettv->v_type = VAR_STRING;
8115 rettv->vval.v_string = NULL;
8116 }
8117
8118 if (argvars[0].v_type == VAR_LIST)
8119 {
8120 if ((l = argvars[0].vval.v_list) == NULL)
8121 goto theend;
8122 li = l->lv_first;
8123 }
8124 else
8125 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008126 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008127 len = (long)STRLEN(str);
8128 }
8129
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008130 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008131 if (pat == NULL)
8132 goto theend;
8133
8134 if (argvars[2].v_type != VAR_UNKNOWN)
8135 {
8136 int error = FALSE;
8137
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008138 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008139 if (error)
8140 goto theend;
8141 if (l != NULL)
8142 {
8143 li = list_find(l, start);
8144 if (li == NULL)
8145 goto theend;
8146 idx = l->lv_idx; /* use the cached index */
8147 }
8148 else
8149 {
8150 if (start < 0)
8151 start = 0;
8152 if (start > len)
8153 goto theend;
8154 /* When "count" argument is there ignore matches before "start",
8155 * otherwise skip part of the string. Differs when pattern is "^"
8156 * or "\<". */
8157 if (argvars[3].v_type != VAR_UNKNOWN)
8158 startcol = start;
8159 else
8160 {
8161 str += start;
8162 len -= start;
8163 }
8164 }
8165
8166 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008167 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008168 if (error)
8169 goto theend;
8170 }
8171
8172 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8173 if (regmatch.regprog != NULL)
8174 {
8175 regmatch.rm_ic = p_ic;
8176
8177 for (;;)
8178 {
8179 if (l != NULL)
8180 {
8181 if (li == NULL)
8182 {
8183 match = FALSE;
8184 break;
8185 }
8186 vim_free(tofree);
8187 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
8188 if (str == NULL)
8189 break;
8190 }
8191
8192 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
8193
8194 if (match && --nth <= 0)
8195 break;
8196 if (l == NULL && !match)
8197 break;
8198
8199 /* Advance to just after the match. */
8200 if (l != NULL)
8201 {
8202 li = li->li_next;
8203 ++idx;
8204 }
8205 else
8206 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008207 startcol = (colnr_T)(regmatch.startp[0]
8208 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008209 if (startcol > (colnr_T)len
8210 || str + startcol <= regmatch.startp[0])
8211 {
8212 match = FALSE;
8213 break;
8214 }
8215 }
8216 }
8217
8218 if (match)
8219 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008220 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008221 {
8222 listitem_T *li1 = rettv->vval.v_list->lv_first;
8223 listitem_T *li2 = li1->li_next;
8224 listitem_T *li3 = li2->li_next;
8225 listitem_T *li4 = li3->li_next;
8226
8227 vim_free(li1->li_tv.vval.v_string);
8228 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
8229 (int)(regmatch.endp[0] - regmatch.startp[0]));
8230 li3->li_tv.vval.v_number =
8231 (varnumber_T)(regmatch.startp[0] - expr);
8232 li4->li_tv.vval.v_number =
8233 (varnumber_T)(regmatch.endp[0] - expr);
8234 if (l != NULL)
8235 li2->li_tv.vval.v_number = (varnumber_T)idx;
8236 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008237 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008238 {
8239 int i;
8240
8241 /* return list with matched string and submatches */
8242 for (i = 0; i < NSUBEXP; ++i)
8243 {
8244 if (regmatch.endp[i] == NULL)
8245 {
8246 if (list_append_string(rettv->vval.v_list,
8247 (char_u *)"", 0) == FAIL)
8248 break;
8249 }
8250 else if (list_append_string(rettv->vval.v_list,
8251 regmatch.startp[i],
8252 (int)(regmatch.endp[i] - regmatch.startp[i]))
8253 == FAIL)
8254 break;
8255 }
8256 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008257 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008258 {
8259 /* return matched string */
8260 if (l != NULL)
8261 copy_tv(&li->li_tv, rettv);
8262 else
8263 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
8264 (int)(regmatch.endp[0] - regmatch.startp[0]));
8265 }
8266 else if (l != NULL)
8267 rettv->vval.v_number = idx;
8268 else
8269 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008270 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008271 rettv->vval.v_number =
8272 (varnumber_T)(regmatch.startp[0] - str);
8273 else
8274 rettv->vval.v_number =
8275 (varnumber_T)(regmatch.endp[0] - str);
8276 rettv->vval.v_number += (varnumber_T)(str - expr);
8277 }
8278 }
8279 vim_regfree(regmatch.regprog);
8280 }
8281
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008282theend:
8283 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008284 /* matchstrpos() without a list: drop the second item. */
8285 listitem_remove(rettv->vval.v_list,
8286 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008287 vim_free(tofree);
8288 p_cpo = save_cpo;
8289}
8290
8291/*
8292 * "match()" function
8293 */
8294 static void
8295f_match(typval_T *argvars, typval_T *rettv)
8296{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008297 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008298}
8299
Bram Moolenaar95e51472018-07-28 16:55:56 +02008300#ifdef FEAT_SEARCH_EXTRA
8301 static int
8302matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
8303{
8304 dictitem_T *di;
8305
8306 if (tv->v_type != VAR_DICT)
8307 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008308 emsg(_(e_dictreq));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008309 return FAIL;
8310 }
8311
8312 if (dict_find(tv->vval.v_dict, (char_u *)"conceal", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01008313 *conceal_char = dict_get_string(tv->vval.v_dict,
Bram Moolenaar95e51472018-07-28 16:55:56 +02008314 (char_u *)"conceal", FALSE);
8315
8316 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
8317 {
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02008318 *win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008319 if (*win == NULL)
8320 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01008321 emsg(_(e_invalwindow));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008322 return FAIL;
8323 }
8324 }
8325
8326 return OK;
8327}
8328#endif
8329
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008330/*
8331 * "matchadd()" function
8332 */
8333 static void
8334f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8335{
8336#ifdef FEAT_SEARCH_EXTRA
8337 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008338 char_u *grp = tv_get_string_buf_chk(&argvars[0], buf); /* group */
8339 char_u *pat = tv_get_string_buf_chk(&argvars[1], buf); /* pattern */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008340 int prio = 10; /* default priority */
8341 int id = -1;
8342 int error = FALSE;
8343 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008344 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008345
8346 rettv->vval.v_number = -1;
8347
8348 if (grp == NULL || pat == NULL)
8349 return;
8350 if (argvars[2].v_type != VAR_UNKNOWN)
8351 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008352 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008353 if (argvars[3].v_type != VAR_UNKNOWN)
8354 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008355 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008356 if (argvars[4].v_type != VAR_UNKNOWN
8357 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8358 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008359 }
8360 }
8361 if (error == TRUE)
8362 return;
8363 if (id >= 1 && id <= 3)
8364 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008365 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008366 return;
8367 }
8368
Bram Moolenaar95e51472018-07-28 16:55:56 +02008369 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008370 conceal_char);
8371#endif
8372}
8373
8374/*
8375 * "matchaddpos()" function
8376 */
8377 static void
8378f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8379{
8380#ifdef FEAT_SEARCH_EXTRA
8381 char_u buf[NUMBUFLEN];
8382 char_u *group;
8383 int prio = 10;
8384 int id = -1;
8385 int error = FALSE;
8386 list_T *l;
8387 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008388 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008389
8390 rettv->vval.v_number = -1;
8391
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008392 group = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008393 if (group == NULL)
8394 return;
8395
8396 if (argvars[1].v_type != VAR_LIST)
8397 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008398 semsg(_(e_listarg), "matchaddpos()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008399 return;
8400 }
8401 l = argvars[1].vval.v_list;
8402 if (l == NULL)
8403 return;
8404
8405 if (argvars[2].v_type != VAR_UNKNOWN)
8406 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008407 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008408 if (argvars[3].v_type != VAR_UNKNOWN)
8409 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008410 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008411
8412 if (argvars[4].v_type != VAR_UNKNOWN
8413 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8414 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008415 }
8416 }
8417 if (error == TRUE)
8418 return;
8419
8420 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8421 if (id == 1 || id == 2)
8422 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008423 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008424 return;
8425 }
8426
Bram Moolenaar95e51472018-07-28 16:55:56 +02008427 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008428 conceal_char);
8429#endif
8430}
8431
8432/*
8433 * "matcharg()" function
8434 */
8435 static void
8436f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8437{
8438 if (rettv_list_alloc(rettv) == OK)
8439 {
8440#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008441 int id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008442 matchitem_T *m;
8443
8444 if (id >= 1 && id <= 3)
8445 {
8446 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8447 {
8448 list_append_string(rettv->vval.v_list,
8449 syn_id2name(m->hlg_id), -1);
8450 list_append_string(rettv->vval.v_list, m->pattern, -1);
8451 }
8452 else
8453 {
8454 list_append_string(rettv->vval.v_list, NULL, -1);
8455 list_append_string(rettv->vval.v_list, NULL, -1);
8456 }
8457 }
8458#endif
8459 }
8460}
8461
8462/*
8463 * "matchdelete()" function
8464 */
8465 static void
8466f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8467{
8468#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01008469 win_T *win = get_optional_window(argvars, 1);
8470
8471 if (win == NULL)
8472 rettv->vval.v_number = -1;
8473 else
8474 rettv->vval.v_number = match_delete(win,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008475 (int)tv_get_number(&argvars[0]), TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008476#endif
8477}
8478
8479/*
8480 * "matchend()" function
8481 */
8482 static void
8483f_matchend(typval_T *argvars, typval_T *rettv)
8484{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008485 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008486}
8487
8488/*
8489 * "matchlist()" function
8490 */
8491 static void
8492f_matchlist(typval_T *argvars, typval_T *rettv)
8493{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008494 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008495}
8496
8497/*
8498 * "matchstr()" function
8499 */
8500 static void
8501f_matchstr(typval_T *argvars, typval_T *rettv)
8502{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008503 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008504}
8505
8506/*
8507 * "matchstrpos()" function
8508 */
8509 static void
8510f_matchstrpos(typval_T *argvars, typval_T *rettv)
8511{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008512 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008513}
8514
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008515 static void
8516max_min(typval_T *argvars, typval_T *rettv, int domax)
8517{
8518 varnumber_T n = 0;
8519 varnumber_T i;
8520 int error = FALSE;
8521
8522 if (argvars[0].v_type == VAR_LIST)
8523 {
8524 list_T *l;
8525 listitem_T *li;
8526
8527 l = argvars[0].vval.v_list;
8528 if (l != NULL)
8529 {
8530 li = l->lv_first;
8531 if (li != NULL)
8532 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008533 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008534 for (;;)
8535 {
8536 li = li->li_next;
8537 if (li == NULL)
8538 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008539 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008540 if (domax ? i > n : i < n)
8541 n = i;
8542 }
8543 }
8544 }
8545 }
8546 else if (argvars[0].v_type == VAR_DICT)
8547 {
8548 dict_T *d;
8549 int first = TRUE;
8550 hashitem_T *hi;
8551 int todo;
8552
8553 d = argvars[0].vval.v_dict;
8554 if (d != NULL)
8555 {
8556 todo = (int)d->dv_hashtab.ht_used;
8557 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8558 {
8559 if (!HASHITEM_EMPTY(hi))
8560 {
8561 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008562 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008563 if (first)
8564 {
8565 n = i;
8566 first = FALSE;
8567 }
8568 else if (domax ? i > n : i < n)
8569 n = i;
8570 }
8571 }
8572 }
8573 }
8574 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008575 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008576 rettv->vval.v_number = error ? 0 : n;
8577}
8578
8579/*
8580 * "max()" function
8581 */
8582 static void
8583f_max(typval_T *argvars, typval_T *rettv)
8584{
8585 max_min(argvars, rettv, TRUE);
8586}
8587
8588/*
8589 * "min()" function
8590 */
8591 static void
8592f_min(typval_T *argvars, typval_T *rettv)
8593{
8594 max_min(argvars, rettv, FALSE);
8595}
8596
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008597/*
8598 * Create the directory in which "dir" is located, and higher levels when
8599 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008600 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008601 */
8602 static int
8603mkdir_recurse(char_u *dir, int prot)
8604{
8605 char_u *p;
8606 char_u *updir;
8607 int r = FAIL;
8608
8609 /* Get end of directory name in "dir".
8610 * We're done when it's "/" or "c:/". */
8611 p = gettail_sep(dir);
8612 if (p <= get_past_head(dir))
8613 return OK;
8614
8615 /* If the directory exists we're done. Otherwise: create it.*/
8616 updir = vim_strnsave(dir, (int)(p - dir));
8617 if (updir == NULL)
8618 return FAIL;
8619 if (mch_isdir(updir))
8620 r = OK;
8621 else if (mkdir_recurse(updir, prot) == OK)
8622 r = vim_mkdir_emsg(updir, prot);
8623 vim_free(updir);
8624 return r;
8625}
8626
8627#ifdef vim_mkdir
8628/*
8629 * "mkdir()" function
8630 */
8631 static void
8632f_mkdir(typval_T *argvars, typval_T *rettv)
8633{
8634 char_u *dir;
8635 char_u buf[NUMBUFLEN];
8636 int prot = 0755;
8637
8638 rettv->vval.v_number = FAIL;
8639 if (check_restricted() || check_secure())
8640 return;
8641
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008642 dir = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008643 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008644 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008645
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008646 if (*gettail(dir) == NUL)
8647 /* remove trailing slashes */
8648 *gettail_sep(dir) = NUL;
8649
8650 if (argvars[1].v_type != VAR_UNKNOWN)
8651 {
8652 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008653 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008654 prot = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008655 if (prot == -1)
8656 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008657 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008658 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008659 {
8660 if (mch_isdir(dir))
8661 {
8662 /* With the "p" flag it's OK if the dir already exists. */
8663 rettv->vval.v_number = OK;
8664 return;
8665 }
8666 mkdir_recurse(dir, prot);
8667 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008668 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008669 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008670}
8671#endif
8672
8673/*
8674 * "mode()" function
8675 */
8676 static void
8677f_mode(typval_T *argvars, typval_T *rettv)
8678{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008679 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008680
Bram Moolenaar612cc382018-07-29 15:34:26 +02008681 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008682
8683 if (time_for_testing == 93784)
8684 {
8685 /* Testing the two-character code. */
8686 buf[0] = 'x';
8687 buf[1] = '!';
8688 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008689#ifdef FEAT_TERMINAL
8690 else if (term_use_loop())
8691 buf[0] = 't';
8692#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008693 else if (VIsual_active)
8694 {
8695 if (VIsual_select)
8696 buf[0] = VIsual_mode + 's' - 'v';
8697 else
8698 buf[0] = VIsual_mode;
8699 }
8700 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8701 || State == CONFIRM)
8702 {
8703 buf[0] = 'r';
8704 if (State == ASKMORE)
8705 buf[1] = 'm';
8706 else if (State == CONFIRM)
8707 buf[1] = '?';
8708 }
8709 else if (State == EXTERNCMD)
8710 buf[0] = '!';
8711 else if (State & INSERT)
8712 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008713 if (State & VREPLACE_FLAG)
8714 {
8715 buf[0] = 'R';
8716 buf[1] = 'v';
8717 }
8718 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008719 {
8720 if (State & REPLACE_FLAG)
8721 buf[0] = 'R';
8722 else
8723 buf[0] = 'i';
8724#ifdef FEAT_INS_EXPAND
8725 if (ins_compl_active())
8726 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008727 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008728 buf[1] = 'x';
8729#endif
8730 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008731 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008732 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008733 {
8734 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008735 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008736 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008737 else if (exmode_active == EXMODE_NORMAL)
8738 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008739 }
8740 else
8741 {
8742 buf[0] = 'n';
8743 if (finish_op)
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008744 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008745 buf[1] = 'o';
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008746 // to be able to detect force-linewise/blockwise/characterwise operations
8747 buf[2] = motion_force;
8748 }
Bram Moolenaar612cc382018-07-29 15:34:26 +02008749 else if (restart_edit == 'I' || restart_edit == 'R'
8750 || restart_edit == 'V')
8751 {
8752 buf[1] = 'i';
8753 buf[2] = restart_edit;
8754 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008755 }
8756
8757 /* Clear out the minor mode when the argument is not a non-zero number or
8758 * non-empty string. */
8759 if (!non_zero_arg(&argvars[0]))
8760 buf[1] = NUL;
8761
8762 rettv->vval.v_string = vim_strsave(buf);
8763 rettv->v_type = VAR_STRING;
8764}
8765
8766#if defined(FEAT_MZSCHEME) || defined(PROTO)
8767/*
8768 * "mzeval()" function
8769 */
8770 static void
8771f_mzeval(typval_T *argvars, typval_T *rettv)
8772{
8773 char_u *str;
8774 char_u buf[NUMBUFLEN];
8775
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008776 if (check_restricted() || check_secure())
8777 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008778 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008779 do_mzeval(str, rettv);
8780}
8781
8782 void
8783mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8784{
8785 typval_T argvars[3];
8786
8787 argvars[0].v_type = VAR_STRING;
8788 argvars[0].vval.v_string = name;
8789 copy_tv(args, &argvars[1]);
8790 argvars[2].v_type = VAR_UNKNOWN;
8791 f_call(argvars, rettv);
8792 clear_tv(&argvars[1]);
8793}
8794#endif
8795
8796/*
8797 * "nextnonblank()" function
8798 */
8799 static void
8800f_nextnonblank(typval_T *argvars, typval_T *rettv)
8801{
8802 linenr_T lnum;
8803
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008804 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008805 {
8806 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8807 {
8808 lnum = 0;
8809 break;
8810 }
8811 if (*skipwhite(ml_get(lnum)) != NUL)
8812 break;
8813 }
8814 rettv->vval.v_number = lnum;
8815}
8816
8817/*
8818 * "nr2char()" function
8819 */
8820 static void
8821f_nr2char(typval_T *argvars, typval_T *rettv)
8822{
8823 char_u buf[NUMBUFLEN];
8824
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008825 if (has_mbyte)
8826 {
8827 int utf8 = 0;
8828
8829 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008830 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008831 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01008832 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008833 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008834 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008835 }
8836 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008837 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008838 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008839 buf[1] = NUL;
8840 }
8841 rettv->v_type = VAR_STRING;
8842 rettv->vval.v_string = vim_strsave(buf);
8843}
8844
8845/*
8846 * "or(expr, expr)" function
8847 */
8848 static void
8849f_or(typval_T *argvars, typval_T *rettv)
8850{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008851 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
8852 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008853}
8854
8855/*
8856 * "pathshorten()" function
8857 */
8858 static void
8859f_pathshorten(typval_T *argvars, typval_T *rettv)
8860{
8861 char_u *p;
8862
8863 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008864 p = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008865 if (p == NULL)
8866 rettv->vval.v_string = NULL;
8867 else
8868 {
8869 p = vim_strsave(p);
8870 rettv->vval.v_string = p;
8871 if (p != NULL)
8872 shorten_dir(p);
8873 }
8874}
8875
8876#ifdef FEAT_PERL
8877/*
8878 * "perleval()" function
8879 */
8880 static void
8881f_perleval(typval_T *argvars, typval_T *rettv)
8882{
8883 char_u *str;
8884 char_u buf[NUMBUFLEN];
8885
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008886 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008887 do_perleval(str, rettv);
8888}
8889#endif
8890
8891#ifdef FEAT_FLOAT
8892/*
8893 * "pow()" function
8894 */
8895 static void
8896f_pow(typval_T *argvars, typval_T *rettv)
8897{
8898 float_T fx = 0.0, fy = 0.0;
8899
8900 rettv->v_type = VAR_FLOAT;
8901 if (get_float_arg(argvars, &fx) == OK
8902 && get_float_arg(&argvars[1], &fy) == OK)
8903 rettv->vval.v_float = pow(fx, fy);
8904 else
8905 rettv->vval.v_float = 0.0;
8906}
8907#endif
8908
8909/*
8910 * "prevnonblank()" function
8911 */
8912 static void
8913f_prevnonblank(typval_T *argvars, typval_T *rettv)
8914{
8915 linenr_T lnum;
8916
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008917 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008918 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8919 lnum = 0;
8920 else
8921 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8922 --lnum;
8923 rettv->vval.v_number = lnum;
8924}
8925
8926/* This dummy va_list is here because:
8927 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8928 * - locally in the function results in a "used before set" warning
8929 * - using va_start() to initialize it gives "function with fixed args" error */
8930static va_list ap;
8931
8932/*
8933 * "printf()" function
8934 */
8935 static void
8936f_printf(typval_T *argvars, typval_T *rettv)
8937{
8938 char_u buf[NUMBUFLEN];
8939 int len;
8940 char_u *s;
8941 int saved_did_emsg = did_emsg;
8942 char *fmt;
8943
8944 rettv->v_type = VAR_STRING;
8945 rettv->vval.v_string = NULL;
8946
8947 /* Get the required length, allocate the buffer and do it for real. */
8948 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008949 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008950 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008951 if (!did_emsg)
8952 {
8953 s = alloc(len + 1);
8954 if (s != NULL)
8955 {
8956 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008957 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8958 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008959 }
8960 }
8961 did_emsg |= saved_did_emsg;
8962}
8963
Bram Moolenaarf2732452018-06-03 14:47:35 +02008964#ifdef FEAT_JOB_CHANNEL
8965/*
8966 * "prompt_setcallback({buffer}, {callback})" function
8967 */
8968 static void
8969f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
8970{
8971 buf_T *buf;
8972 char_u *callback;
8973 partial_T *partial;
8974
8975 if (check_secure())
8976 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01008977 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008978 if (buf == NULL)
8979 return;
8980
8981 callback = get_callback(&argvars[1], &partial);
8982 if (callback == NULL)
8983 return;
8984
8985 free_callback(buf->b_prompt_callback, buf->b_prompt_partial);
8986 if (partial == NULL)
8987 buf->b_prompt_callback = vim_strsave(callback);
8988 else
8989 /* pointer into the partial */
8990 buf->b_prompt_callback = callback;
8991 buf->b_prompt_partial = partial;
8992}
8993
8994/*
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02008995 * "prompt_setinterrupt({buffer}, {callback})" function
8996 */
8997 static void
8998f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
8999{
9000 buf_T *buf;
9001 char_u *callback;
9002 partial_T *partial;
9003
9004 if (check_secure())
9005 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009006 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009007 if (buf == NULL)
9008 return;
9009
9010 callback = get_callback(&argvars[1], &partial);
9011 if (callback == NULL)
9012 return;
9013
9014 free_callback(buf->b_prompt_interrupt, buf->b_prompt_int_partial);
9015 if (partial == NULL)
9016 buf->b_prompt_interrupt = vim_strsave(callback);
9017 else
9018 /* pointer into the partial */
9019 buf->b_prompt_interrupt = callback;
9020 buf->b_prompt_int_partial = partial;
9021}
9022
9023/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02009024 * "prompt_setprompt({buffer}, {text})" function
9025 */
9026 static void
9027f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
9028{
9029 buf_T *buf;
9030 char_u *text;
9031
9032 if (check_secure())
9033 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009034 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009035 if (buf == NULL)
9036 return;
9037
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009038 text = tv_get_string(&argvars[1]);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009039 vim_free(buf->b_prompt_text);
9040 buf->b_prompt_text = vim_strsave(text);
9041}
9042#endif
9043
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009044/*
9045 * "pumvisible()" function
9046 */
9047 static void
9048f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9049{
9050#ifdef FEAT_INS_EXPAND
9051 if (pum_visible())
9052 rettv->vval.v_number = 1;
9053#endif
9054}
9055
9056#ifdef FEAT_PYTHON3
9057/*
9058 * "py3eval()" function
9059 */
9060 static void
9061f_py3eval(typval_T *argvars, typval_T *rettv)
9062{
9063 char_u *str;
9064 char_u buf[NUMBUFLEN];
9065
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009066 if (check_restricted() || check_secure())
9067 return;
9068
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009069 if (p_pyx == 0)
9070 p_pyx = 3;
9071
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009072 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009073 do_py3eval(str, rettv);
9074}
9075#endif
9076
9077#ifdef FEAT_PYTHON
9078/*
9079 * "pyeval()" function
9080 */
9081 static void
9082f_pyeval(typval_T *argvars, typval_T *rettv)
9083{
9084 char_u *str;
9085 char_u buf[NUMBUFLEN];
9086
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009087 if (check_restricted() || check_secure())
9088 return;
9089
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009090 if (p_pyx == 0)
9091 p_pyx = 2;
9092
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009093 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009094 do_pyeval(str, rettv);
9095}
9096#endif
9097
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009098#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
9099/*
9100 * "pyxeval()" function
9101 */
9102 static void
9103f_pyxeval(typval_T *argvars, typval_T *rettv)
9104{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009105 if (check_restricted() || check_secure())
9106 return;
9107
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009108# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
9109 init_pyxversion();
9110 if (p_pyx == 2)
9111 f_pyeval(argvars, rettv);
9112 else
9113 f_py3eval(argvars, rettv);
9114# elif defined(FEAT_PYTHON)
9115 f_pyeval(argvars, rettv);
9116# elif defined(FEAT_PYTHON3)
9117 f_py3eval(argvars, rettv);
9118# endif
9119}
9120#endif
9121
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009122/*
9123 * "range()" function
9124 */
9125 static void
9126f_range(typval_T *argvars, typval_T *rettv)
9127{
9128 varnumber_T start;
9129 varnumber_T end;
9130 varnumber_T stride = 1;
9131 varnumber_T i;
9132 int error = FALSE;
9133
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009134 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009135 if (argvars[1].v_type == VAR_UNKNOWN)
9136 {
9137 end = start - 1;
9138 start = 0;
9139 }
9140 else
9141 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009142 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009143 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009144 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009145 }
9146
9147 if (error)
9148 return; /* type error; errmsg already given */
9149 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009150 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009151 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009152 emsg(_("E727: Start past end"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009153 else
9154 {
9155 if (rettv_list_alloc(rettv) == OK)
9156 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
9157 if (list_append_number(rettv->vval.v_list,
9158 (varnumber_T)i) == FAIL)
9159 break;
9160 }
9161}
9162
9163/*
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009164 * Evaluate "expr" for readdir().
9165 */
9166 static int
9167readdir_checkitem(typval_T *expr, char_u *name)
9168{
9169 typval_T save_val;
9170 typval_T rettv;
9171 typval_T argv[2];
9172 int retval = 0;
9173 int error = FALSE;
9174
9175 prepare_vimvar(VV_VAL, &save_val);
9176 set_vim_var_string(VV_VAL, name, -1);
9177 argv[0].v_type = VAR_STRING;
9178 argv[0].vval.v_string = name;
9179
9180 if (eval_expr_typval(expr, argv, 1, &rettv) == FAIL)
9181 goto theend;
9182
9183 retval = tv_get_number_chk(&rettv, &error);
9184 if (error)
9185 retval = -1;
9186 clear_tv(&rettv);
9187
9188theend:
9189 set_vim_var_string(VV_VAL, NULL, 0);
9190 restore_vimvar(VV_VAL, &save_val);
9191 return retval;
9192}
9193
9194/*
9195 * "readdir()" function
9196 */
9197 static void
9198f_readdir(typval_T *argvars, typval_T *rettv)
9199{
9200 typval_T *expr;
9201 int failed = FALSE;
9202 char_u *path;
9203 garray_T ga;
9204 int i;
9205#ifdef MSWIN
9206 char_u *buf, *p;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009207 int ok;
9208 HANDLE hFind = INVALID_HANDLE_VALUE;
9209 WIN32_FIND_DATAW wfb;
9210 WCHAR *wn = NULL; // UCS-2 name, NULL when not used.
9211#endif
9212
9213 if (rettv_list_alloc(rettv) == FAIL)
9214 return;
9215 path = tv_get_string(&argvars[0]);
9216 expr = &argvars[1];
9217 ga_init2(&ga, (int)sizeof(char *), 20);
9218
9219#ifdef MSWIN
9220 buf = alloc((int)MAXPATHL);
9221 if (buf == NULL)
9222 return;
9223 STRNCPY(buf, path, MAXPATHL-5);
9224 p = vim_strpbrk(path, (char_u *)"\\/");
9225 if (p != NULL)
9226 *p = NUL;
9227 STRCAT(buf, "\\*");
9228
9229 wn = enc_to_utf16(buf, NULL);
9230 if (wn != NULL)
9231 hFind = FindFirstFileW(wn, &wfb);
9232 ok = (hFind != INVALID_HANDLE_VALUE);
9233 if (!ok)
9234 smsg(_(e_notopen), path);
9235 else
9236 {
9237 while (ok)
9238 {
9239 int ignore;
9240
9241 p = utf16_to_enc(wfb.cFileName, NULL); // p is allocated here
9242 if (p == NULL)
9243 break; // out of memory
9244
9245 ignore = p[0] == '.' && (p[1] == NUL
9246 || (p[1] == '.' && p[2] == NUL));
9247 if (!ignore && expr->v_type != VAR_UNKNOWN)
9248 {
9249 int r = readdir_checkitem(expr, p);
9250
9251 if (r < 0)
9252 {
9253 vim_free(p);
9254 break;
9255 }
9256 if (r == 0)
9257 ignore = TRUE;
9258 }
9259
9260 if (!ignore)
9261 {
9262 if (ga_grow(&ga, 1) == OK)
9263 ((char_u**)ga.ga_data)[ga.ga_len++] = vim_strsave(p);
9264 else
9265 {
9266 failed = TRUE;
9267 vim_free(p);
9268 break;
9269 }
9270 }
9271
9272 vim_free(p);
9273 ok = FindNextFileW(hFind, &wfb);
9274 }
9275 FindClose(hFind);
9276 }
9277
9278 vim_free(buf);
9279 vim_free(wn);
9280#else
9281 DIR *dirp;
9282 struct dirent *dp;
9283 char_u *p;
9284
9285 dirp = opendir((char *)path);
9286 if (dirp == NULL)
9287 smsg(_(e_notopen), path);
9288 else
9289 {
9290 for (;;)
9291 {
9292 int ignore;
9293
9294 dp = readdir(dirp);
9295 if (dp == NULL)
9296 break;
9297 p = (char_u *)dp->d_name;
9298
9299 ignore = p[0] == '.' &&
9300 (p[1] == NUL ||
9301 (p[1] == '.' && p[2] == NUL));
9302 if (!ignore && expr->v_type != VAR_UNKNOWN)
9303 {
9304 int r = readdir_checkitem(expr, p);
9305
9306 if (r < 0)
9307 break;
9308 if (r == 0)
9309 ignore = TRUE;
9310 }
9311
9312 if (!ignore)
9313 {
9314 if (ga_grow(&ga, 1) == OK)
9315 ((char_u**)ga.ga_data)[ga.ga_len++] = vim_strsave(p);
9316 else
9317 {
9318 failed = TRUE;
9319 break;
9320 }
9321 }
9322 }
9323
9324 closedir(dirp);
9325 }
9326#endif
9327
Bram Moolenaar334ad412019-04-19 15:20:46 +02009328 if (!failed && rettv->vval.v_list != NULL && ga.ga_len > 0)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009329 {
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009330 sort_strings((char_u **)ga.ga_data, ga.ga_len);
9331 for (i = 0; i < ga.ga_len; i++)
9332 {
9333 p = ((char_u **)ga.ga_data)[i];
9334 list_append_string(rettv->vval.v_list, p, -1);
9335 }
9336 }
Bram Moolenaar334ad412019-04-19 15:20:46 +02009337 ga_clear_strings(&ga);
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009338}
9339
9340/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009341 * "readfile()" function
9342 */
9343 static void
9344f_readfile(typval_T *argvars, typval_T *rettv)
9345{
9346 int binary = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009347 int blob = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009348 int failed = FALSE;
9349 char_u *fname;
9350 FILE *fd;
9351 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
9352 int io_size = sizeof(buf);
9353 int readlen; /* size of last fread() */
9354 char_u *prev = NULL; /* previously read bytes, if any */
9355 long prevlen = 0; /* length of data in prev */
9356 long prevsize = 0; /* size of prev buffer */
9357 long maxline = MAXLNUM;
9358 long cnt = 0;
9359 char_u *p; /* position in buf */
9360 char_u *start; /* start of current line */
9361
9362 if (argvars[1].v_type != VAR_UNKNOWN)
9363 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009364 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009365 binary = TRUE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009366 if (STRCMP(tv_get_string(&argvars[1]), "B") == 0)
9367 blob = TRUE;
9368
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009369 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009370 maxline = (long)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009371 }
9372
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009373 if (blob)
9374 {
9375 if (rettv_blob_alloc(rettv) == FAIL)
9376 return;
9377 }
9378 else
9379 {
9380 if (rettv_list_alloc(rettv) == FAIL)
9381 return;
9382 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009383
9384 /* Always open the file in binary mode, library functions have a mind of
9385 * their own about CR-LF conversion. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009386 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009387 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
9388 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009389 semsg(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009390 return;
9391 }
9392
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009393 if (blob)
9394 {
9395 if (read_blob(fd, rettv->vval.v_blob) == FAIL)
9396 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009397 emsg("cannot read file");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009398 blob_free(rettv->vval.v_blob);
9399 }
9400 fclose(fd);
9401 return;
9402 }
9403
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009404 while (cnt < maxline || maxline < 0)
9405 {
9406 readlen = (int)fread(buf, 1, io_size, fd);
9407
9408 /* This for loop processes what was read, but is also entered at end
9409 * of file so that either:
9410 * - an incomplete line gets written
9411 * - a "binary" file gets an empty line at the end if it ends in a
9412 * newline. */
9413 for (p = buf, start = buf;
9414 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
9415 ++p)
9416 {
9417 if (*p == '\n' || readlen <= 0)
9418 {
9419 listitem_T *li;
9420 char_u *s = NULL;
9421 long_u len = p - start;
9422
9423 /* Finished a line. Remove CRs before NL. */
9424 if (readlen > 0 && !binary)
9425 {
9426 while (len > 0 && start[len - 1] == '\r')
9427 --len;
9428 /* removal may cross back to the "prev" string */
9429 if (len == 0)
9430 while (prevlen > 0 && prev[prevlen - 1] == '\r')
9431 --prevlen;
9432 }
9433 if (prevlen == 0)
9434 s = vim_strnsave(start, (int)len);
9435 else
9436 {
9437 /* Change "prev" buffer to be the right size. This way
9438 * the bytes are only copied once, and very long lines are
9439 * allocated only once. */
9440 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
9441 {
9442 mch_memmove(s + prevlen, start, len);
9443 s[prevlen + len] = NUL;
9444 prev = NULL; /* the list will own the string */
9445 prevlen = prevsize = 0;
9446 }
9447 }
9448 if (s == NULL)
9449 {
9450 do_outofmem_msg((long_u) prevlen + len + 1);
9451 failed = TRUE;
9452 break;
9453 }
9454
9455 if ((li = listitem_alloc()) == NULL)
9456 {
9457 vim_free(s);
9458 failed = TRUE;
9459 break;
9460 }
9461 li->li_tv.v_type = VAR_STRING;
9462 li->li_tv.v_lock = 0;
9463 li->li_tv.vval.v_string = s;
9464 list_append(rettv->vval.v_list, li);
9465
9466 start = p + 1; /* step over newline */
9467 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
9468 break;
9469 }
9470 else if (*p == NUL)
9471 *p = '\n';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009472 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
9473 * when finding the BF and check the previous two bytes. */
9474 else if (*p == 0xbf && enc_utf8 && !binary)
9475 {
9476 /* Find the two bytes before the 0xbf. If p is at buf, or buf
9477 * + 1, these may be in the "prev" string. */
9478 char_u back1 = p >= buf + 1 ? p[-1]
9479 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
9480 char_u back2 = p >= buf + 2 ? p[-2]
9481 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
9482 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
9483
9484 if (back2 == 0xef && back1 == 0xbb)
9485 {
9486 char_u *dest = p - 2;
9487
9488 /* Usually a BOM is at the beginning of a file, and so at
9489 * the beginning of a line; then we can just step over it.
9490 */
9491 if (start == dest)
9492 start = p + 1;
9493 else
9494 {
9495 /* have to shuffle buf to close gap */
9496 int adjust_prevlen = 0;
9497
9498 if (dest < buf)
9499 {
9500 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
9501 dest = buf;
9502 }
9503 if (readlen > p - buf + 1)
9504 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
9505 readlen -= 3 - adjust_prevlen;
9506 prevlen -= adjust_prevlen;
9507 p = dest - 1;
9508 }
9509 }
9510 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009511 } /* for */
9512
9513 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
9514 break;
9515 if (start < p)
9516 {
9517 /* There's part of a line in buf, store it in "prev". */
9518 if (p - start + prevlen >= prevsize)
9519 {
9520 /* need bigger "prev" buffer */
9521 char_u *newprev;
9522
9523 /* A common use case is ordinary text files and "prev" gets a
9524 * fragment of a line, so the first allocation is made
9525 * small, to avoid repeatedly 'allocing' large and
9526 * 'reallocing' small. */
9527 if (prevsize == 0)
9528 prevsize = (long)(p - start);
9529 else
9530 {
9531 long grow50pc = (prevsize * 3) / 2;
9532 long growmin = (long)((p - start) * 2 + prevlen);
9533 prevsize = grow50pc > growmin ? grow50pc : growmin;
9534 }
9535 newprev = prev == NULL ? alloc(prevsize)
9536 : vim_realloc(prev, prevsize);
9537 if (newprev == NULL)
9538 {
9539 do_outofmem_msg((long_u)prevsize);
9540 failed = TRUE;
9541 break;
9542 }
9543 prev = newprev;
9544 }
9545 /* Add the line part to end of "prev". */
9546 mch_memmove(prev + prevlen, start, p - start);
9547 prevlen += (long)(p - start);
9548 }
9549 } /* while */
9550
9551 /*
9552 * For a negative line count use only the lines at the end of the file,
9553 * free the rest.
9554 */
9555 if (!failed && maxline < 0)
9556 while (cnt > -maxline)
9557 {
9558 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
9559 --cnt;
9560 }
9561
9562 if (failed)
9563 {
9564 list_free(rettv->vval.v_list);
9565 /* readfile doc says an empty list is returned on error */
9566 rettv->vval.v_list = list_alloc();
9567 }
9568
9569 vim_free(prev);
9570 fclose(fd);
9571}
9572
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009573 static void
9574return_register(int regname, typval_T *rettv)
9575{
9576 char_u buf[2] = {0, 0};
9577
9578 buf[0] = (char_u)regname;
9579 rettv->v_type = VAR_STRING;
9580 rettv->vval.v_string = vim_strsave(buf);
9581}
9582
9583/*
9584 * "reg_executing()" function
9585 */
9586 static void
9587f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9588{
9589 return_register(reg_executing, rettv);
9590}
9591
9592/*
9593 * "reg_recording()" function
9594 */
9595 static void
9596f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9597{
9598 return_register(reg_recording, rettv);
9599}
9600
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009601#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009602/*
9603 * Convert a List to proftime_T.
9604 * Return FAIL when there is something wrong.
9605 */
9606 static int
9607list2proftime(typval_T *arg, proftime_T *tm)
9608{
9609 long n1, n2;
9610 int error = FALSE;
9611
9612 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9613 || arg->vval.v_list->lv_len != 2)
9614 return FAIL;
9615 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9616 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009617# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009618 tm->HighPart = n1;
9619 tm->LowPart = n2;
9620# else
9621 tm->tv_sec = n1;
9622 tm->tv_usec = n2;
9623# endif
9624 return error ? FAIL : OK;
9625}
9626#endif /* FEAT_RELTIME */
9627
9628/*
9629 * "reltime()" function
9630 */
9631 static void
9632f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9633{
9634#ifdef FEAT_RELTIME
9635 proftime_T res;
9636 proftime_T start;
9637
9638 if (argvars[0].v_type == VAR_UNKNOWN)
9639 {
9640 /* No arguments: get current time. */
9641 profile_start(&res);
9642 }
9643 else if (argvars[1].v_type == VAR_UNKNOWN)
9644 {
9645 if (list2proftime(&argvars[0], &res) == FAIL)
9646 return;
9647 profile_end(&res);
9648 }
9649 else
9650 {
9651 /* Two arguments: compute the difference. */
9652 if (list2proftime(&argvars[0], &start) == FAIL
9653 || list2proftime(&argvars[1], &res) == FAIL)
9654 return;
9655 profile_sub(&res, &start);
9656 }
9657
9658 if (rettv_list_alloc(rettv) == OK)
9659 {
9660 long n1, n2;
9661
Bram Moolenaar4f974752019-02-17 17:44:42 +01009662# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009663 n1 = res.HighPart;
9664 n2 = res.LowPart;
9665# else
9666 n1 = res.tv_sec;
9667 n2 = res.tv_usec;
9668# endif
9669 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9670 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9671 }
9672#endif
9673}
9674
9675#ifdef FEAT_FLOAT
9676/*
9677 * "reltimefloat()" function
9678 */
9679 static void
9680f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9681{
9682# ifdef FEAT_RELTIME
9683 proftime_T tm;
9684# endif
9685
9686 rettv->v_type = VAR_FLOAT;
9687 rettv->vval.v_float = 0;
9688# ifdef FEAT_RELTIME
9689 if (list2proftime(&argvars[0], &tm) == OK)
9690 rettv->vval.v_float = profile_float(&tm);
9691# endif
9692}
9693#endif
9694
9695/*
9696 * "reltimestr()" function
9697 */
9698 static void
9699f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9700{
9701#ifdef FEAT_RELTIME
9702 proftime_T tm;
9703#endif
9704
9705 rettv->v_type = VAR_STRING;
9706 rettv->vval.v_string = NULL;
9707#ifdef FEAT_RELTIME
9708 if (list2proftime(&argvars[0], &tm) == OK)
9709 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9710#endif
9711}
9712
9713#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009714 static void
9715make_connection(void)
9716{
9717 if (X_DISPLAY == NULL
9718# ifdef FEAT_GUI
9719 && !gui.in_use
9720# endif
9721 )
9722 {
9723 x_force_connect = TRUE;
9724 setup_term_clip();
9725 x_force_connect = FALSE;
9726 }
9727}
9728
9729 static int
9730check_connection(void)
9731{
9732 make_connection();
9733 if (X_DISPLAY == NULL)
9734 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009735 emsg(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009736 return FAIL;
9737 }
9738 return OK;
9739}
9740#endif
9741
9742#ifdef FEAT_CLIENTSERVER
9743 static void
9744remote_common(typval_T *argvars, typval_T *rettv, int expr)
9745{
9746 char_u *server_name;
9747 char_u *keys;
9748 char_u *r = NULL;
9749 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009750 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009751# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009752 HWND w;
9753# else
9754 Window w;
9755# endif
9756
9757 if (check_restricted() || check_secure())
9758 return;
9759
9760# ifdef FEAT_X11
9761 if (check_connection() == FAIL)
9762 return;
9763# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009764 if (argvars[2].v_type != VAR_UNKNOWN
9765 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009766 timeout = tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009767
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009768 server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009769 if (server_name == NULL)
9770 return; /* type error; errmsg already given */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009771 keys = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009772# ifdef MSWIN
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009773 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009774# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009775 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9776 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009777# endif
9778 {
9779 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009780 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009781 emsg((char *)r); // sending worked but evaluation failed
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009782 vim_free(r);
9783 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009784 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009785 semsg(_("E241: Unable to send to %s"), server_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009786 return;
9787 }
9788
9789 rettv->vval.v_string = r;
9790
9791 if (argvars[2].v_type != VAR_UNKNOWN)
9792 {
9793 dictitem_T v;
9794 char_u str[30];
9795 char_u *idvar;
9796
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009797 idvar = tv_get_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009798 if (idvar != NULL && *idvar != NUL)
9799 {
9800 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9801 v.di_tv.v_type = VAR_STRING;
9802 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009803 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009804 vim_free(v.di_tv.vval.v_string);
9805 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009806 }
9807}
9808#endif
9809
9810/*
9811 * "remote_expr()" function
9812 */
9813 static void
9814f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9815{
9816 rettv->v_type = VAR_STRING;
9817 rettv->vval.v_string = NULL;
9818#ifdef FEAT_CLIENTSERVER
9819 remote_common(argvars, rettv, TRUE);
9820#endif
9821}
9822
9823/*
9824 * "remote_foreground()" function
9825 */
9826 static void
9827f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9828{
9829#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +01009830# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009831 /* On Win32 it's done in this application. */
9832 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009833 char_u *server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009834
9835 if (server_name != NULL)
9836 serverForeground(server_name);
9837 }
9838# else
9839 /* Send a foreground() expression to the server. */
9840 argvars[1].v_type = VAR_STRING;
9841 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9842 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009843 rettv->v_type = VAR_STRING;
9844 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009845 remote_common(argvars, rettv, TRUE);
9846 vim_free(argvars[1].vval.v_string);
9847# endif
9848#endif
9849}
9850
9851 static void
9852f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
9853{
9854#ifdef FEAT_CLIENTSERVER
9855 dictitem_T v;
9856 char_u *s = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009857# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009858 long_u n = 0;
9859# endif
9860 char_u *serverid;
9861
9862 if (check_restricted() || check_secure())
9863 {
9864 rettv->vval.v_number = -1;
9865 return;
9866 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009867 serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009868 if (serverid == NULL)
9869 {
9870 rettv->vval.v_number = -1;
9871 return; /* type error; errmsg already given */
9872 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01009873# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009874 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
9875 if (n == 0)
9876 rettv->vval.v_number = -1;
9877 else
9878 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009879 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009880 rettv->vval.v_number = (s != NULL);
9881 }
9882# else
9883 if (check_connection() == FAIL)
9884 return;
9885
9886 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
9887 serverStrToWin(serverid), &s);
9888# endif
9889
9890 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
9891 {
9892 char_u *retvar;
9893
9894 v.di_tv.v_type = VAR_STRING;
9895 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009896 retvar = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009897 if (retvar != NULL)
9898 set_var(retvar, &v.di_tv, FALSE);
9899 vim_free(v.di_tv.vval.v_string);
9900 }
9901#else
9902 rettv->vval.v_number = -1;
9903#endif
9904}
9905
9906 static void
9907f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
9908{
9909 char_u *r = NULL;
9910
9911#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009912 char_u *serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009913
9914 if (serverid != NULL && !check_restricted() && !check_secure())
9915 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009916 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009917# ifdef MSWIN
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009918 /* The server's HWND is encoded in the 'id' parameter */
9919 long_u n = 0;
9920# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009921
9922 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009923 timeout = tv_get_number(&argvars[1]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009924
Bram Moolenaar4f974752019-02-17 17:44:42 +01009925# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009926 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
9927 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009928 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009929 if (r == NULL)
9930# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009931 if (check_connection() == FAIL
9932 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
9933 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009934# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009935 emsg(_("E277: Unable to read a server reply"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009936 }
9937#endif
9938 rettv->v_type = VAR_STRING;
9939 rettv->vval.v_string = r;
9940}
9941
9942/*
9943 * "remote_send()" function
9944 */
9945 static void
9946f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9947{
9948 rettv->v_type = VAR_STRING;
9949 rettv->vval.v_string = NULL;
9950#ifdef FEAT_CLIENTSERVER
9951 remote_common(argvars, rettv, FALSE);
9952#endif
9953}
9954
9955/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009956 * "remote_startserver()" function
9957 */
9958 static void
9959f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9960{
9961#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009962 char_u *server = tv_get_string_chk(&argvars[0]);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009963
9964 if (server == NULL)
9965 return; /* type error; errmsg already given */
9966 if (serverName != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009967 emsg(_("E941: already started a server"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009968 else
9969 {
9970# ifdef FEAT_X11
9971 if (check_connection() == OK)
9972 serverRegisterName(X_DISPLAY, server);
9973# else
9974 serverSetName(server);
9975# endif
9976 }
9977#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009978 emsg(_("E942: +clientserver feature not available"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009979#endif
9980}
9981
9982/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009983 * "remove()" function
9984 */
9985 static void
9986f_remove(typval_T *argvars, typval_T *rettv)
9987{
9988 list_T *l;
9989 listitem_T *item, *item2;
9990 listitem_T *li;
9991 long idx;
9992 long end;
9993 char_u *key;
9994 dict_T *d;
9995 dictitem_T *di;
9996 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009997 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009998
9999 if (argvars[0].v_type == VAR_DICT)
10000 {
10001 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010002 semsg(_(e_toomanyarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010003 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010004 && !var_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010005 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010006 key = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010007 if (key != NULL)
10008 {
10009 di = dict_find(d, key, -1);
10010 if (di == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010011 semsg(_(e_dictkey), key);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010012 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10013 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
10014 {
10015 *rettv = di->di_tv;
10016 init_tv(&di->di_tv);
10017 dictitem_remove(d, di);
10018 }
10019 }
10020 }
10021 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010022 else if (argvars[0].v_type == VAR_BLOB)
10023 {
10024 idx = (long)tv_get_number_chk(&argvars[1], &error);
10025 if (!error)
10026 {
10027 blob_T *b = argvars[0].vval.v_blob;
10028 int len = blob_len(b);
10029 char_u *p;
10030
10031 if (idx < 0)
10032 // count from the end
10033 idx = len + idx;
10034 if (idx < 0 || idx >= len)
10035 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010036 semsg(_(e_blobidx), idx);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010037 return;
10038 }
10039 if (argvars[2].v_type == VAR_UNKNOWN)
10040 {
10041 // Remove one item, return its value.
10042 p = (char_u *)b->bv_ga.ga_data;
10043 rettv->vval.v_number = (varnumber_T) *(p + idx);
10044 mch_memmove(p + idx, p + idx + 1, (size_t)len - idx - 1);
10045 --b->bv_ga.ga_len;
10046 }
10047 else
10048 {
10049 blob_T *blob;
10050
10051 // Remove range of items, return list with values.
10052 end = (long)tv_get_number_chk(&argvars[2], &error);
10053 if (error)
10054 return;
10055 if (end < 0)
10056 // count from the end
10057 end = len + end;
10058 if (end >= len || idx > end)
10059 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010060 semsg(_(e_blobidx), end);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010061 return;
10062 }
10063 blob = blob_alloc();
10064 if (blob == NULL)
10065 return;
10066 blob->bv_ga.ga_len = end - idx + 1;
10067 if (ga_grow(&blob->bv_ga, end - idx + 1) == FAIL)
10068 {
10069 vim_free(blob);
10070 return;
10071 }
10072 p = (char_u *)b->bv_ga.ga_data;
10073 mch_memmove((char_u *)blob->bv_ga.ga_data, p + idx,
10074 (size_t)(end - idx + 1));
10075 ++blob->bv_refcount;
10076 rettv->v_type = VAR_BLOB;
10077 rettv->vval.v_blob = blob;
10078
10079 mch_memmove(p + idx, p + end + 1, (size_t)(len - end));
10080 b->bv_ga.ga_len -= end - idx + 1;
10081 }
10082 }
10083 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010084 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010085 semsg(_(e_listdictblobarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010086 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010087 && !var_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010088 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010089 idx = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010090 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010091 ; // type error: do nothing, errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010092 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010093 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010094 else
10095 {
10096 if (argvars[2].v_type == VAR_UNKNOWN)
10097 {
10098 /* Remove one item, return its value. */
10099 vimlist_remove(l, item, item);
10100 *rettv = item->li_tv;
10101 vim_free(item);
10102 }
10103 else
10104 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010105 // Remove range of items, return list with values.
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010106 end = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010107 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010108 ; // type error: do nothing
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010109 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010110 semsg(_(e_listidx), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010111 else
10112 {
10113 int cnt = 0;
10114
10115 for (li = item; li != NULL; li = li->li_next)
10116 {
10117 ++cnt;
10118 if (li == item2)
10119 break;
10120 }
10121 if (li == NULL) /* didn't find "item2" after "item" */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010122 emsg(_(e_invrange));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010123 else
10124 {
10125 vimlist_remove(l, item, item2);
10126 if (rettv_list_alloc(rettv) == OK)
10127 {
10128 l = rettv->vval.v_list;
10129 l->lv_first = item;
10130 l->lv_last = item2;
10131 item->li_prev = NULL;
10132 item2->li_next = NULL;
10133 l->lv_len = cnt;
10134 }
10135 }
10136 }
10137 }
10138 }
10139 }
10140}
10141
10142/*
10143 * "rename({from}, {to})" function
10144 */
10145 static void
10146f_rename(typval_T *argvars, typval_T *rettv)
10147{
10148 char_u buf[NUMBUFLEN];
10149
10150 if (check_restricted() || check_secure())
10151 rettv->vval.v_number = -1;
10152 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010153 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
10154 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010155}
10156
10157/*
10158 * "repeat()" function
10159 */
10160 static void
10161f_repeat(typval_T *argvars, typval_T *rettv)
10162{
10163 char_u *p;
10164 int n;
10165 int slen;
10166 int len;
10167 char_u *r;
10168 int i;
10169
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010170 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010171 if (argvars[0].v_type == VAR_LIST)
10172 {
10173 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
10174 while (n-- > 0)
10175 if (list_extend(rettv->vval.v_list,
10176 argvars[0].vval.v_list, NULL) == FAIL)
10177 break;
10178 }
10179 else
10180 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010181 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010182 rettv->v_type = VAR_STRING;
10183 rettv->vval.v_string = NULL;
10184
10185 slen = (int)STRLEN(p);
10186 len = slen * n;
10187 if (len <= 0)
10188 return;
10189
10190 r = alloc(len + 1);
10191 if (r != NULL)
10192 {
10193 for (i = 0; i < n; i++)
10194 mch_memmove(r + i * slen, p, (size_t)slen);
10195 r[len] = NUL;
10196 }
10197
10198 rettv->vval.v_string = r;
10199 }
10200}
10201
10202/*
10203 * "resolve()" function
10204 */
10205 static void
10206f_resolve(typval_T *argvars, typval_T *rettv)
10207{
10208 char_u *p;
10209#ifdef HAVE_READLINK
10210 char_u *buf = NULL;
10211#endif
10212
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010213 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010214#ifdef FEAT_SHORTCUT
10215 {
10216 char_u *v = NULL;
10217
Bram Moolenaardce1e892019-02-10 23:18:53 +010010218 v = mch_resolve_path(p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010219 if (v != NULL)
10220 rettv->vval.v_string = v;
10221 else
10222 rettv->vval.v_string = vim_strsave(p);
10223 }
10224#else
10225# ifdef HAVE_READLINK
10226 {
10227 char_u *cpy;
10228 int len;
10229 char_u *remain = NULL;
10230 char_u *q;
10231 int is_relative_to_current = FALSE;
10232 int has_trailing_pathsep = FALSE;
10233 int limit = 100;
10234
10235 p = vim_strsave(p);
10236
10237 if (p[0] == '.' && (vim_ispathsep(p[1])
10238 || (p[1] == '.' && (vim_ispathsep(p[2])))))
10239 is_relative_to_current = TRUE;
10240
10241 len = STRLEN(p);
10242 if (len > 0 && after_pathsep(p, p + len))
10243 {
10244 has_trailing_pathsep = TRUE;
10245 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
10246 }
10247
10248 q = getnextcomp(p);
10249 if (*q != NUL)
10250 {
10251 /* Separate the first path component in "p", and keep the
10252 * remainder (beginning with the path separator). */
10253 remain = vim_strsave(q - 1);
10254 q[-1] = NUL;
10255 }
10256
10257 buf = alloc(MAXPATHL + 1);
10258 if (buf == NULL)
10259 goto fail;
10260
10261 for (;;)
10262 {
10263 for (;;)
10264 {
10265 len = readlink((char *)p, (char *)buf, MAXPATHL);
10266 if (len <= 0)
10267 break;
10268 buf[len] = NUL;
10269
10270 if (limit-- == 0)
10271 {
10272 vim_free(p);
10273 vim_free(remain);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010274 emsg(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010275 rettv->vval.v_string = NULL;
10276 goto fail;
10277 }
10278
10279 /* Ensure that the result will have a trailing path separator
10280 * if the argument has one. */
10281 if (remain == NULL && has_trailing_pathsep)
10282 add_pathsep(buf);
10283
10284 /* Separate the first path component in the link value and
10285 * concatenate the remainders. */
10286 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
10287 if (*q != NUL)
10288 {
10289 if (remain == NULL)
10290 remain = vim_strsave(q - 1);
10291 else
10292 {
10293 cpy = concat_str(q - 1, remain);
10294 if (cpy != NULL)
10295 {
10296 vim_free(remain);
10297 remain = cpy;
10298 }
10299 }
10300 q[-1] = NUL;
10301 }
10302
10303 q = gettail(p);
10304 if (q > p && *q == NUL)
10305 {
10306 /* Ignore trailing path separator. */
10307 q[-1] = NUL;
10308 q = gettail(p);
10309 }
10310 if (q > p && !mch_isFullName(buf))
10311 {
10312 /* symlink is relative to directory of argument */
10313 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
10314 if (cpy != NULL)
10315 {
10316 STRCPY(cpy, p);
10317 STRCPY(gettail(cpy), buf);
10318 vim_free(p);
10319 p = cpy;
10320 }
10321 }
10322 else
10323 {
10324 vim_free(p);
10325 p = vim_strsave(buf);
10326 }
10327 }
10328
10329 if (remain == NULL)
10330 break;
10331
10332 /* Append the first path component of "remain" to "p". */
10333 q = getnextcomp(remain + 1);
10334 len = q - remain - (*q != NUL);
10335 cpy = vim_strnsave(p, STRLEN(p) + len);
10336 if (cpy != NULL)
10337 {
10338 STRNCAT(cpy, remain, len);
10339 vim_free(p);
10340 p = cpy;
10341 }
10342 /* Shorten "remain". */
10343 if (*q != NUL)
10344 STRMOVE(remain, q - 1);
10345 else
Bram Moolenaard23a8232018-02-10 18:45:26 +010010346 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010347 }
10348
10349 /* If the result is a relative path name, make it explicitly relative to
10350 * the current directory if and only if the argument had this form. */
10351 if (!vim_ispathsep(*p))
10352 {
10353 if (is_relative_to_current
10354 && *p != NUL
10355 && !(p[0] == '.'
10356 && (p[1] == NUL
10357 || vim_ispathsep(p[1])
10358 || (p[1] == '.'
10359 && (p[2] == NUL
10360 || vim_ispathsep(p[2]))))))
10361 {
10362 /* Prepend "./". */
10363 cpy = concat_str((char_u *)"./", p);
10364 if (cpy != NULL)
10365 {
10366 vim_free(p);
10367 p = cpy;
10368 }
10369 }
10370 else if (!is_relative_to_current)
10371 {
10372 /* Strip leading "./". */
10373 q = p;
10374 while (q[0] == '.' && vim_ispathsep(q[1]))
10375 q += 2;
10376 if (q > p)
10377 STRMOVE(p, p + 2);
10378 }
10379 }
10380
10381 /* Ensure that the result will have no trailing path separator
10382 * if the argument had none. But keep "/" or "//". */
10383 if (!has_trailing_pathsep)
10384 {
10385 q = p + STRLEN(p);
10386 if (after_pathsep(p, q))
10387 *gettail_sep(p) = NUL;
10388 }
10389
10390 rettv->vval.v_string = p;
10391 }
10392# else
10393 rettv->vval.v_string = vim_strsave(p);
10394# endif
10395#endif
10396
10397 simplify_filename(rettv->vval.v_string);
10398
10399#ifdef HAVE_READLINK
10400fail:
10401 vim_free(buf);
10402#endif
10403 rettv->v_type = VAR_STRING;
10404}
10405
10406/*
10407 * "reverse({list})" function
10408 */
10409 static void
10410f_reverse(typval_T *argvars, typval_T *rettv)
10411{
10412 list_T *l;
10413 listitem_T *li, *ni;
10414
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010415 if (argvars[0].v_type == VAR_BLOB)
10416 {
10417 blob_T *b = argvars[0].vval.v_blob;
10418 int i, len = blob_len(b);
10419
10420 for (i = 0; i < len / 2; i++)
10421 {
10422 int tmp = blob_get(b, i);
10423
10424 blob_set(b, i, blob_get(b, len - i - 1));
10425 blob_set(b, len - i - 1, tmp);
10426 }
10427 rettv_blob_set(rettv, b);
10428 return;
10429 }
10430
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010431 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010432 semsg(_(e_listblobarg), "reverse()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010433 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010434 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010435 (char_u *)N_("reverse() argument"), TRUE))
10436 {
10437 li = l->lv_last;
10438 l->lv_first = l->lv_last = NULL;
10439 l->lv_len = 0;
10440 while (li != NULL)
10441 {
10442 ni = li->li_prev;
10443 list_append(l, li);
10444 li = ni;
10445 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020010446 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010447 l->lv_idx = l->lv_len - l->lv_idx - 1;
10448 }
10449}
10450
10451#define SP_NOMOVE 0x01 /* don't move cursor */
10452#define SP_REPEAT 0x02 /* repeat to find outer pair */
10453#define SP_RETCOUNT 0x04 /* return matchcount */
10454#define SP_SETPCMARK 0x08 /* set previous context mark */
10455#define SP_START 0x10 /* accept match at start position */
10456#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
10457#define SP_END 0x40 /* leave cursor at end of match */
10458#define SP_COLUMN 0x80 /* start at cursor column */
10459
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010460/*
10461 * Get flags for a search function.
10462 * Possibly sets "p_ws".
10463 * Returns BACKWARD, FORWARD or zero (for an error).
10464 */
10465 static int
10466get_search_arg(typval_T *varp, int *flagsp)
10467{
10468 int dir = FORWARD;
10469 char_u *flags;
10470 char_u nbuf[NUMBUFLEN];
10471 int mask;
10472
10473 if (varp->v_type != VAR_UNKNOWN)
10474 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010475 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010476 if (flags == NULL)
10477 return 0; /* type error; errmsg already given */
10478 while (*flags != NUL)
10479 {
10480 switch (*flags)
10481 {
10482 case 'b': dir = BACKWARD; break;
10483 case 'w': p_ws = TRUE; break;
10484 case 'W': p_ws = FALSE; break;
10485 default: mask = 0;
10486 if (flagsp != NULL)
10487 switch (*flags)
10488 {
10489 case 'c': mask = SP_START; break;
10490 case 'e': mask = SP_END; break;
10491 case 'm': mask = SP_RETCOUNT; break;
10492 case 'n': mask = SP_NOMOVE; break;
10493 case 'p': mask = SP_SUBPAT; break;
10494 case 'r': mask = SP_REPEAT; break;
10495 case 's': mask = SP_SETPCMARK; break;
10496 case 'z': mask = SP_COLUMN; break;
10497 }
10498 if (mask == 0)
10499 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010500 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010501 dir = 0;
10502 }
10503 else
10504 *flagsp |= mask;
10505 }
10506 if (dir == 0)
10507 break;
10508 ++flags;
10509 }
10510 }
10511 return dir;
10512}
10513
10514/*
10515 * Shared by search() and searchpos() functions.
10516 */
10517 static int
10518search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
10519{
10520 int flags;
10521 char_u *pat;
10522 pos_T pos;
10523 pos_T save_cursor;
10524 int save_p_ws = p_ws;
10525 int dir;
10526 int retval = 0; /* default: FAIL */
10527 long lnum_stop = 0;
10528 proftime_T tm;
10529#ifdef FEAT_RELTIME
10530 long time_limit = 0;
10531#endif
10532 int options = SEARCH_KEEP;
10533 int subpatnum;
10534
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010535 pat = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010536 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
10537 if (dir == 0)
10538 goto theend;
10539 flags = *flagsp;
10540 if (flags & SP_START)
10541 options |= SEARCH_START;
10542 if (flags & SP_END)
10543 options |= SEARCH_END;
10544 if (flags & SP_COLUMN)
10545 options |= SEARCH_COL;
10546
10547 /* Optional arguments: line number to stop searching and timeout. */
10548 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
10549 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010550 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010551 if (lnum_stop < 0)
10552 goto theend;
10553#ifdef FEAT_RELTIME
10554 if (argvars[3].v_type != VAR_UNKNOWN)
10555 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010556 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010557 if (time_limit < 0)
10558 goto theend;
10559 }
10560#endif
10561 }
10562
10563#ifdef FEAT_RELTIME
10564 /* Set the time limit, if there is one. */
10565 profile_setlimit(time_limit, &tm);
10566#endif
10567
10568 /*
10569 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
10570 * Check to make sure only those flags are set.
10571 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
10572 * flags cannot be set. Check for that condition also.
10573 */
10574 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
10575 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10576 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010577 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010578 goto theend;
10579 }
10580
10581 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010582 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010583 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010584 if (subpatnum != FAIL)
10585 {
10586 if (flags & SP_SUBPAT)
10587 retval = subpatnum;
10588 else
10589 retval = pos.lnum;
10590 if (flags & SP_SETPCMARK)
10591 setpcmark();
10592 curwin->w_cursor = pos;
10593 if (match_pos != NULL)
10594 {
10595 /* Store the match cursor position */
10596 match_pos->lnum = pos.lnum;
10597 match_pos->col = pos.col + 1;
10598 }
10599 /* "/$" will put the cursor after the end of the line, may need to
10600 * correct that here */
10601 check_cursor();
10602 }
10603
10604 /* If 'n' flag is used: restore cursor position. */
10605 if (flags & SP_NOMOVE)
10606 curwin->w_cursor = save_cursor;
10607 else
10608 curwin->w_set_curswant = TRUE;
10609theend:
10610 p_ws = save_p_ws;
10611
10612 return retval;
10613}
10614
10615#ifdef FEAT_FLOAT
10616
10617/*
10618 * round() is not in C90, use ceil() or floor() instead.
10619 */
10620 float_T
10621vim_round(float_T f)
10622{
10623 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
10624}
10625
10626/*
10627 * "round({float})" function
10628 */
10629 static void
10630f_round(typval_T *argvars, typval_T *rettv)
10631{
10632 float_T f = 0.0;
10633
10634 rettv->v_type = VAR_FLOAT;
10635 if (get_float_arg(argvars, &f) == OK)
10636 rettv->vval.v_float = vim_round(f);
10637 else
10638 rettv->vval.v_float = 0.0;
10639}
10640#endif
10641
Bram Moolenaare99be0e2019-03-26 22:51:09 +010010642#ifdef FEAT_RUBY
10643/*
10644 * "rubyeval()" function
10645 */
10646 static void
10647f_rubyeval(typval_T *argvars, typval_T *rettv)
10648{
10649 char_u *str;
10650 char_u buf[NUMBUFLEN];
10651
10652 str = tv_get_string_buf(&argvars[0], buf);
10653 do_rubyeval(str, rettv);
10654}
10655#endif
10656
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010657/*
10658 * "screenattr()" function
10659 */
10660 static void
10661f_screenattr(typval_T *argvars, typval_T *rettv)
10662{
10663 int row;
10664 int col;
10665 int c;
10666
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010667 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10668 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010669 if (row < 0 || row >= screen_Rows
10670 || col < 0 || col >= screen_Columns)
10671 c = -1;
10672 else
10673 c = ScreenAttrs[LineOffset[row] + col];
10674 rettv->vval.v_number = c;
10675}
10676
10677/*
10678 * "screenchar()" function
10679 */
10680 static void
10681f_screenchar(typval_T *argvars, typval_T *rettv)
10682{
10683 int row;
10684 int col;
10685 int off;
10686 int c;
10687
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010688 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10689 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010690 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010691 c = -1;
10692 else
10693 {
10694 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010695 if (enc_utf8 && ScreenLinesUC[off] != 0)
10696 c = ScreenLinesUC[off];
10697 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010698 c = ScreenLines[off];
10699 }
10700 rettv->vval.v_number = c;
10701}
10702
10703/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010704 * "screenchars()" function
10705 */
10706 static void
10707f_screenchars(typval_T *argvars, typval_T *rettv)
10708{
10709 int row;
10710 int col;
10711 int off;
10712 int c;
10713 int i;
10714
10715 if (rettv_list_alloc(rettv) == FAIL)
10716 return;
10717 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10718 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10719 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10720 return;
10721
10722 off = LineOffset[row] + col;
10723 if (enc_utf8 && ScreenLinesUC[off] != 0)
10724 c = ScreenLinesUC[off];
10725 else
10726 c = ScreenLines[off];
10727 list_append_number(rettv->vval.v_list, (varnumber_T)c);
10728
10729 if (enc_utf8)
10730
10731 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10732 list_append_number(rettv->vval.v_list,
10733 (varnumber_T)ScreenLinesC[i][off]);
10734}
10735
10736/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010737 * "screencol()" function
10738 *
10739 * First column is 1 to be consistent with virtcol().
10740 */
10741 static void
10742f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10743{
10744 rettv->vval.v_number = screen_screencol() + 1;
10745}
10746
10747/*
10748 * "screenrow()" function
10749 */
10750 static void
10751f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10752{
10753 rettv->vval.v_number = screen_screenrow() + 1;
10754}
10755
10756/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010757 * "screenstring()" function
10758 */
10759 static void
10760f_screenstring(typval_T *argvars, typval_T *rettv)
10761{
10762 int row;
10763 int col;
10764 int off;
10765 int c;
10766 int i;
10767 char_u buf[MB_MAXBYTES + 1];
10768 int buflen = 0;
10769
10770 rettv->vval.v_string = NULL;
10771 rettv->v_type = VAR_STRING;
10772
10773 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10774 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10775 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10776 return;
10777
10778 off = LineOffset[row] + col;
10779 if (enc_utf8 && ScreenLinesUC[off] != 0)
10780 c = ScreenLinesUC[off];
10781 else
10782 c = ScreenLines[off];
10783 buflen += mb_char2bytes(c, buf);
10784
10785 if (enc_utf8)
10786 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10787 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
10788
10789 buf[buflen] = NUL;
10790 rettv->vval.v_string = vim_strsave(buf);
10791}
10792
10793/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010794 * "search()" function
10795 */
10796 static void
10797f_search(typval_T *argvars, typval_T *rettv)
10798{
10799 int flags = 0;
10800
10801 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10802}
10803
10804/*
10805 * "searchdecl()" function
10806 */
10807 static void
10808f_searchdecl(typval_T *argvars, typval_T *rettv)
10809{
10810 int locally = 1;
10811 int thisblock = 0;
10812 int error = FALSE;
10813 char_u *name;
10814
10815 rettv->vval.v_number = 1; /* default: FAIL */
10816
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010817 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010818 if (argvars[1].v_type != VAR_UNKNOWN)
10819 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010820 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010821 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010822 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010823 }
10824 if (!error && name != NULL)
10825 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10826 locally, thisblock, SEARCH_KEEP) == FAIL;
10827}
10828
10829/*
10830 * Used by searchpair() and searchpairpos()
10831 */
10832 static int
10833searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10834{
10835 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010836 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010837 int save_p_ws = p_ws;
10838 int dir;
10839 int flags = 0;
10840 char_u nbuf1[NUMBUFLEN];
10841 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010842 int retval = 0; /* default: FAIL */
10843 long lnum_stop = 0;
10844 long time_limit = 0;
10845
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010846 /* Get the three pattern arguments: start, middle, end. Will result in an
10847 * error if not a valid argument. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010848 spat = tv_get_string_chk(&argvars[0]);
10849 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
10850 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010851 if (spat == NULL || mpat == NULL || epat == NULL)
10852 goto theend; /* type error */
10853
10854 /* Handle the optional fourth argument: flags */
10855 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
10856 if (dir == 0)
10857 goto theend;
10858
10859 /* Don't accept SP_END or SP_SUBPAT.
10860 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
10861 */
10862 if ((flags & (SP_END | SP_SUBPAT)) != 0
10863 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10864 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010865 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010866 goto theend;
10867 }
10868
10869 /* Using 'r' implies 'W', otherwise it doesn't work. */
10870 if (flags & SP_REPEAT)
10871 p_ws = FALSE;
10872
10873 /* Optional fifth argument: skip expression */
10874 if (argvars[3].v_type == VAR_UNKNOWN
10875 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010010876 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010877 else
10878 {
Bram Moolenaar48570482017-10-30 21:48:41 +010010879 skip = &argvars[4];
10880 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
10881 && skip->v_type != VAR_STRING)
10882 {
10883 /* Type error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010884 semsg(_(e_invarg2), tv_get_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010010885 goto theend;
10886 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010887 if (argvars[5].v_type != VAR_UNKNOWN)
10888 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010889 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010890 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010891 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010892 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010893 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010894 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010895#ifdef FEAT_RELTIME
10896 if (argvars[6].v_type != VAR_UNKNOWN)
10897 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010898 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010899 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010900 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010901 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010902 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010903 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010904 }
10905#endif
10906 }
10907 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010908
10909 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
10910 match_pos, lnum_stop, time_limit);
10911
10912theend:
10913 p_ws = save_p_ws;
10914
10915 return retval;
10916}
10917
10918/*
10919 * "searchpair()" function
10920 */
10921 static void
10922f_searchpair(typval_T *argvars, typval_T *rettv)
10923{
10924 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
10925}
10926
10927/*
10928 * "searchpairpos()" function
10929 */
10930 static void
10931f_searchpairpos(typval_T *argvars, typval_T *rettv)
10932{
10933 pos_T match_pos;
10934 int lnum = 0;
10935 int col = 0;
10936
10937 if (rettv_list_alloc(rettv) == FAIL)
10938 return;
10939
10940 if (searchpair_cmn(argvars, &match_pos) > 0)
10941 {
10942 lnum = match_pos.lnum;
10943 col = match_pos.col;
10944 }
10945
10946 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10947 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10948}
10949
10950/*
10951 * Search for a start/middle/end thing.
10952 * Used by searchpair(), see its documentation for the details.
10953 * Returns 0 or -1 for no match,
10954 */
10955 long
10956do_searchpair(
10957 char_u *spat, /* start pattern */
10958 char_u *mpat, /* middle pattern */
10959 char_u *epat, /* end pattern */
10960 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010010961 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010962 int flags, /* SP_SETPCMARK and other SP_ values */
10963 pos_T *match_pos,
10964 linenr_T lnum_stop, /* stop at this line if not zero */
10965 long time_limit UNUSED) /* stop after this many msec */
10966{
10967 char_u *save_cpo;
10968 char_u *pat, *pat2 = NULL, *pat3 = NULL;
10969 long retval = 0;
10970 pos_T pos;
10971 pos_T firstpos;
10972 pos_T foundpos;
10973 pos_T save_cursor;
10974 pos_T save_pos;
10975 int n;
10976 int r;
10977 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010010978 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010979 int err;
10980 int options = SEARCH_KEEP;
10981 proftime_T tm;
10982
10983 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10984 save_cpo = p_cpo;
10985 p_cpo = empty_option;
10986
10987#ifdef FEAT_RELTIME
10988 /* Set the time limit, if there is one. */
10989 profile_setlimit(time_limit, &tm);
10990#endif
10991
10992 /* Make two search patterns: start/end (pat2, for in nested pairs) and
10993 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010994 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
10995 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010996 if (pat2 == NULL || pat3 == NULL)
10997 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010998 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010999 if (*mpat == NUL)
11000 STRCPY(pat3, pat2);
11001 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010011002 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011003 spat, epat, mpat);
11004 if (flags & SP_START)
11005 options |= SEARCH_START;
11006
Bram Moolenaar48570482017-10-30 21:48:41 +010011007 if (skip != NULL)
11008 {
11009 /* Empty string means to not use the skip expression. */
11010 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
11011 use_skip = skip->vval.v_string != NULL
11012 && *skip->vval.v_string != NUL;
11013 }
11014
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011015 save_cursor = curwin->w_cursor;
11016 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011017 CLEAR_POS(&firstpos);
11018 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011019 pat = pat3;
11020 for (;;)
11021 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +010011022 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020011023 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011024 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011025 /* didn't find it or found the first match again: FAIL */
11026 break;
11027
11028 if (firstpos.lnum == 0)
11029 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011030 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011031 {
11032 /* Found the same position again. Can happen with a pattern that
11033 * has "\zs" at the end and searching backwards. Advance one
11034 * character and try again. */
11035 if (dir == BACKWARD)
11036 decl(&pos);
11037 else
11038 incl(&pos);
11039 }
11040 foundpos = pos;
11041
11042 /* clear the start flag to avoid getting stuck here */
11043 options &= ~SEARCH_START;
11044
11045 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010011046 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011047 {
11048 save_pos = curwin->w_cursor;
11049 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010011050 err = FALSE;
11051 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011052 curwin->w_cursor = save_pos;
11053 if (err)
11054 {
11055 /* Evaluating {skip} caused an error, break here. */
11056 curwin->w_cursor = save_cursor;
11057 retval = -1;
11058 break;
11059 }
11060 if (r)
11061 continue;
11062 }
11063
11064 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
11065 {
11066 /* Found end when searching backwards or start when searching
11067 * forward: nested pair. */
11068 ++nest;
11069 pat = pat2; /* nested, don't search for middle */
11070 }
11071 else
11072 {
11073 /* Found end when searching forward or start when searching
11074 * backward: end of (nested) pair; or found middle in outer pair. */
11075 if (--nest == 1)
11076 pat = pat3; /* outer level, search for middle */
11077 }
11078
11079 if (nest == 0)
11080 {
11081 /* Found the match: return matchcount or line number. */
11082 if (flags & SP_RETCOUNT)
11083 ++retval;
11084 else
11085 retval = pos.lnum;
11086 if (flags & SP_SETPCMARK)
11087 setpcmark();
11088 curwin->w_cursor = pos;
11089 if (!(flags & SP_REPEAT))
11090 break;
11091 nest = 1; /* search for next unmatched */
11092 }
11093 }
11094
11095 if (match_pos != NULL)
11096 {
11097 /* Store the match cursor position */
11098 match_pos->lnum = curwin->w_cursor.lnum;
11099 match_pos->col = curwin->w_cursor.col + 1;
11100 }
11101
11102 /* If 'n' flag is used or search failed: restore cursor position. */
11103 if ((flags & SP_NOMOVE) || retval == 0)
11104 curwin->w_cursor = save_cursor;
11105
11106theend:
11107 vim_free(pat2);
11108 vim_free(pat3);
11109 if (p_cpo == empty_option)
11110 p_cpo = save_cpo;
11111 else
11112 /* Darn, evaluating the {skip} expression changed the value. */
11113 free_string_option(save_cpo);
11114
11115 return retval;
11116}
11117
11118/*
11119 * "searchpos()" function
11120 */
11121 static void
11122f_searchpos(typval_T *argvars, typval_T *rettv)
11123{
11124 pos_T match_pos;
11125 int lnum = 0;
11126 int col = 0;
11127 int n;
11128 int flags = 0;
11129
11130 if (rettv_list_alloc(rettv) == FAIL)
11131 return;
11132
11133 n = search_cmn(argvars, &match_pos, &flags);
11134 if (n > 0)
11135 {
11136 lnum = match_pos.lnum;
11137 col = match_pos.col;
11138 }
11139
11140 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
11141 list_append_number(rettv->vval.v_list, (varnumber_T)col);
11142 if (flags & SP_SUBPAT)
11143 list_append_number(rettv->vval.v_list, (varnumber_T)n);
11144}
11145
11146 static void
11147f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
11148{
11149#ifdef FEAT_CLIENTSERVER
11150 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011151 char_u *server = tv_get_string_chk(&argvars[0]);
11152 char_u *reply = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011153
11154 rettv->vval.v_number = -1;
11155 if (server == NULL || reply == NULL)
11156 return;
11157 if (check_restricted() || check_secure())
11158 return;
11159# ifdef FEAT_X11
11160 if (check_connection() == FAIL)
11161 return;
11162# endif
11163
11164 if (serverSendReply(server, reply) < 0)
11165 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011166 emsg(_("E258: Unable to send to client"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011167 return;
11168 }
11169 rettv->vval.v_number = 0;
11170#else
11171 rettv->vval.v_number = -1;
11172#endif
11173}
11174
11175 static void
11176f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
11177{
11178 char_u *r = NULL;
11179
11180#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +010011181# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011182 r = serverGetVimNames();
11183# else
11184 make_connection();
11185 if (X_DISPLAY != NULL)
11186 r = serverGetVimNames(X_DISPLAY);
11187# endif
11188#endif
11189 rettv->v_type = VAR_STRING;
11190 rettv->vval.v_string = r;
11191}
11192
11193/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011194 * "setbufline()" function
11195 */
11196 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020011197f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011198{
11199 linenr_T lnum;
11200 buf_T *buf;
11201
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011202 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011203 if (buf == NULL)
11204 rettv->vval.v_number = 1; /* FAIL */
11205 else
11206 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011207 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020011208 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011209 }
11210}
11211
11212/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011213 * "setbufvar()" function
11214 */
11215 static void
11216f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
11217{
11218 buf_T *buf;
11219 char_u *varname, *bufvarname;
11220 typval_T *varp;
11221 char_u nbuf[NUMBUFLEN];
11222
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011223 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011224 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011225 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
11226 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011227 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011228 varp = &argvars[2];
11229
11230 if (buf != NULL && varname != NULL && varp != NULL)
11231 {
11232 if (*varname == '&')
11233 {
11234 long numval;
11235 char_u *strval;
11236 int error = FALSE;
11237 aco_save_T aco;
11238
11239 /* set curbuf to be our buf, temporarily */
11240 aucmd_prepbuf(&aco, buf);
11241
11242 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011243 numval = (long)tv_get_number_chk(varp, &error);
11244 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011245 if (!error && strval != NULL)
11246 set_option_value(varname, numval, strval, OPT_LOCAL);
11247
11248 /* reset notion of buffer */
11249 aucmd_restbuf(&aco);
11250 }
11251 else
11252 {
11253 buf_T *save_curbuf = curbuf;
11254
11255 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
11256 if (bufvarname != NULL)
11257 {
11258 curbuf = buf;
11259 STRCPY(bufvarname, "b:");
11260 STRCPY(bufvarname + 2, varname);
11261 set_var(bufvarname, varp, TRUE);
11262 vim_free(bufvarname);
11263 curbuf = save_curbuf;
11264 }
11265 }
11266 }
11267}
11268
11269 static void
11270f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
11271{
11272 dict_T *d;
11273 dictitem_T *di;
11274 char_u *csearch;
11275
11276 if (argvars[0].v_type != VAR_DICT)
11277 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011278 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011279 return;
11280 }
11281
11282 if ((d = argvars[0].vval.v_dict) != NULL)
11283 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010011284 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011285 if (csearch != NULL)
11286 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011287 if (enc_utf8)
11288 {
11289 int pcc[MAX_MCO];
11290 int c = utfc_ptr2char(csearch, pcc);
11291
11292 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
11293 }
11294 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011295 set_last_csearch(PTR2CHAR(csearch),
11296 csearch, MB_PTR2LEN(csearch));
11297 }
11298
11299 di = dict_find(d, (char_u *)"forward", -1);
11300 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011301 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011302 ? FORWARD : BACKWARD);
11303
11304 di = dict_find(d, (char_u *)"until", -1);
11305 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011306 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011307 }
11308}
11309
11310/*
11311 * "setcmdpos()" function
11312 */
11313 static void
11314f_setcmdpos(typval_T *argvars, typval_T *rettv)
11315{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011316 int pos = (int)tv_get_number(&argvars[0]) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011317
11318 if (pos >= 0)
11319 rettv->vval.v_number = set_cmdline_pos(pos);
11320}
11321
11322/*
11323 * "setfperm({fname}, {mode})" function
11324 */
11325 static void
11326f_setfperm(typval_T *argvars, typval_T *rettv)
11327{
11328 char_u *fname;
11329 char_u modebuf[NUMBUFLEN];
11330 char_u *mode_str;
11331 int i;
11332 int mask;
11333 int mode = 0;
11334
11335 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011336 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011337 if (fname == NULL)
11338 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011339 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011340 if (mode_str == NULL)
11341 return;
11342 if (STRLEN(mode_str) != 9)
11343 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011344 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011345 return;
11346 }
11347
11348 mask = 1;
11349 for (i = 8; i >= 0; --i)
11350 {
11351 if (mode_str[i] != '-')
11352 mode |= mask;
11353 mask = mask << 1;
11354 }
11355 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
11356}
11357
11358/*
11359 * "setline()" function
11360 */
11361 static void
11362f_setline(typval_T *argvars, typval_T *rettv)
11363{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011364 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011365
Bram Moolenaarca851592018-06-06 21:04:07 +020011366 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011367}
11368
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011369/*
11370 * Used by "setqflist()" and "setloclist()" functions
11371 */
11372 static void
11373set_qf_ll_list(
11374 win_T *wp UNUSED,
11375 typval_T *list_arg UNUSED,
11376 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020011377 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011378 typval_T *rettv)
11379{
11380#ifdef FEAT_QUICKFIX
11381 static char *e_invact = N_("E927: Invalid action: '%s'");
11382 char_u *act;
11383 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011384 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011385#endif
11386
11387 rettv->vval.v_number = -1;
11388
11389#ifdef FEAT_QUICKFIX
11390 if (list_arg->v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011391 emsg(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011392 else if (recursive != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011393 emsg(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011394 else
11395 {
11396 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011397 dict_T *d = NULL;
11398 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011399
11400 if (action_arg->v_type == VAR_STRING)
11401 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011402 act = tv_get_string_chk(action_arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011403 if (act == NULL)
11404 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020011405 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
11406 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011407 action = *act;
11408 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011409 semsg(_(e_invact), act);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011410 }
11411 else if (action_arg->v_type == VAR_UNKNOWN)
11412 action = ' ';
11413 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011414 emsg(_(e_stringreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011415
Bram Moolenaard823fa92016-08-12 16:29:27 +020011416 if (action_arg->v_type != VAR_UNKNOWN
11417 && what_arg->v_type != VAR_UNKNOWN)
11418 {
11419 if (what_arg->v_type == VAR_DICT)
11420 d = what_arg->vval.v_dict;
11421 else
11422 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011423 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +020011424 valid_dict = FALSE;
11425 }
11426 }
11427
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011428 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011429 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011430 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
11431 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011432 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011433 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011434 }
11435#endif
11436}
11437
11438/*
11439 * "setloclist()" function
11440 */
11441 static void
11442f_setloclist(typval_T *argvars, typval_T *rettv)
11443{
11444 win_T *win;
11445
11446 rettv->vval.v_number = -1;
11447
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020011448 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011449 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020011450 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011451}
11452
11453/*
11454 * "setmatches()" function
11455 */
11456 static void
11457f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
11458{
11459#ifdef FEAT_SEARCH_EXTRA
11460 list_T *l;
11461 listitem_T *li;
11462 dict_T *d;
11463 list_T *s = NULL;
Bram Moolenaaraff74912019-03-30 18:11:49 +010011464 win_T *win = get_optional_window(argvars, 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011465
11466 rettv->vval.v_number = -1;
11467 if (argvars[0].v_type != VAR_LIST)
11468 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011469 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011470 return;
11471 }
Bram Moolenaaraff74912019-03-30 18:11:49 +010011472 if (win == NULL)
11473 return;
11474
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011475 if ((l = argvars[0].vval.v_list) != NULL)
11476 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011477 /* To some extent make sure that we are dealing with a list from
11478 * "getmatches()". */
11479 li = l->lv_first;
11480 while (li != NULL)
11481 {
11482 if (li->li_tv.v_type != VAR_DICT
11483 || (d = li->li_tv.vval.v_dict) == NULL)
11484 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011485 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011486 return;
11487 }
11488 if (!(dict_find(d, (char_u *)"group", -1) != NULL
11489 && (dict_find(d, (char_u *)"pattern", -1) != NULL
11490 || dict_find(d, (char_u *)"pos1", -1) != NULL)
11491 && dict_find(d, (char_u *)"priority", -1) != NULL
11492 && dict_find(d, (char_u *)"id", -1) != NULL))
11493 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011494 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011495 return;
11496 }
11497 li = li->li_next;
11498 }
11499
Bram Moolenaaraff74912019-03-30 18:11:49 +010011500 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011501 li = l->lv_first;
11502 while (li != NULL)
11503 {
11504 int i = 0;
11505 char_u buf[5];
11506 dictitem_T *di;
11507 char_u *group;
11508 int priority;
11509 int id;
11510 char_u *conceal;
11511
11512 d = li->li_tv.vval.v_dict;
11513 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
11514 {
11515 if (s == NULL)
11516 {
11517 s = list_alloc();
11518 if (s == NULL)
11519 return;
11520 }
11521
11522 /* match from matchaddpos() */
11523 for (i = 1; i < 9; i++)
11524 {
11525 sprintf((char *)buf, (char *)"pos%d", i);
11526 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
11527 {
11528 if (di->di_tv.v_type != VAR_LIST)
11529 return;
11530
11531 list_append_tv(s, &di->di_tv);
11532 s->lv_refcount++;
11533 }
11534 else
11535 break;
11536 }
11537 }
11538
Bram Moolenaar8f667172018-12-14 15:38:31 +010011539 group = dict_get_string(d, (char_u *)"group", TRUE);
11540 priority = (int)dict_get_number(d, (char_u *)"priority");
11541 id = (int)dict_get_number(d, (char_u *)"id");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011542 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar8f667172018-12-14 15:38:31 +010011543 ? dict_get_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011544 : NULL;
11545 if (i == 0)
11546 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011547 match_add(win, group,
Bram Moolenaar8f667172018-12-14 15:38:31 +010011548 dict_get_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011549 priority, id, NULL, conceal);
11550 }
11551 else
11552 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011553 match_add(win, group, NULL, priority, id, s, conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011554 list_unref(s);
11555 s = NULL;
11556 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020011557 vim_free(group);
11558 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011559
11560 li = li->li_next;
11561 }
11562 rettv->vval.v_number = 0;
11563 }
11564#endif
11565}
11566
11567/*
11568 * "setpos()" function
11569 */
11570 static void
11571f_setpos(typval_T *argvars, typval_T *rettv)
11572{
11573 pos_T pos;
11574 int fnum;
11575 char_u *name;
11576 colnr_T curswant = -1;
11577
11578 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011579 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011580 if (name != NULL)
11581 {
11582 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
11583 {
11584 if (--pos.col < 0)
11585 pos.col = 0;
11586 if (name[0] == '.' && name[1] == NUL)
11587 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011588 /* set cursor; "fnum" is ignored */
11589 curwin->w_cursor = pos;
11590 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011591 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011592 curwin->w_curswant = curswant - 1;
11593 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011594 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011595 check_cursor();
11596 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011597 }
11598 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
11599 {
11600 /* set mark */
11601 if (setmark_pos(name[1], &pos, fnum) == OK)
11602 rettv->vval.v_number = 0;
11603 }
11604 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011605 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011606 }
11607 }
11608}
11609
11610/*
11611 * "setqflist()" function
11612 */
11613 static void
11614f_setqflist(typval_T *argvars, typval_T *rettv)
11615{
Bram Moolenaard823fa92016-08-12 16:29:27 +020011616 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011617}
11618
11619/*
11620 * "setreg()" function
11621 */
11622 static void
11623f_setreg(typval_T *argvars, typval_T *rettv)
11624{
11625 int regname;
11626 char_u *strregname;
11627 char_u *stropt;
11628 char_u *strval;
11629 int append;
11630 char_u yank_type;
11631 long block_len;
11632
11633 block_len = -1;
11634 yank_type = MAUTO;
11635 append = FALSE;
11636
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011637 strregname = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011638 rettv->vval.v_number = 1; /* FAIL is default */
11639
11640 if (strregname == NULL)
11641 return; /* type error; errmsg already given */
11642 regname = *strregname;
11643 if (regname == 0 || regname == '@')
11644 regname = '"';
11645
11646 if (argvars[2].v_type != VAR_UNKNOWN)
11647 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011648 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011649 if (stropt == NULL)
11650 return; /* type error */
11651 for (; *stropt != NUL; ++stropt)
11652 switch (*stropt)
11653 {
11654 case 'a': case 'A': /* append */
11655 append = TRUE;
11656 break;
11657 case 'v': case 'c': /* character-wise selection */
11658 yank_type = MCHAR;
11659 break;
11660 case 'V': case 'l': /* line-wise selection */
11661 yank_type = MLINE;
11662 break;
11663 case 'b': case Ctrl_V: /* block-wise selection */
11664 yank_type = MBLOCK;
11665 if (VIM_ISDIGIT(stropt[1]))
11666 {
11667 ++stropt;
11668 block_len = getdigits(&stropt) - 1;
11669 --stropt;
11670 }
11671 break;
11672 }
11673 }
11674
11675 if (argvars[1].v_type == VAR_LIST)
11676 {
11677 char_u **lstval;
11678 char_u **allocval;
11679 char_u buf[NUMBUFLEN];
11680 char_u **curval;
11681 char_u **curallocval;
11682 list_T *ll = argvars[1].vval.v_list;
11683 listitem_T *li;
11684 int len;
11685
11686 /* If the list is NULL handle like an empty list. */
11687 len = ll == NULL ? 0 : ll->lv_len;
11688
11689 /* First half: use for pointers to result lines; second half: use for
11690 * pointers to allocated copies. */
11691 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
11692 if (lstval == NULL)
11693 return;
11694 curval = lstval;
11695 allocval = lstval + len + 2;
11696 curallocval = allocval;
11697
11698 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
11699 li = li->li_next)
11700 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011701 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011702 if (strval == NULL)
11703 goto free_lstval;
11704 if (strval == buf)
11705 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011706 /* Need to make a copy, next tv_get_string_buf_chk() will
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011707 * overwrite the string. */
11708 strval = vim_strsave(buf);
11709 if (strval == NULL)
11710 goto free_lstval;
11711 *curallocval++ = strval;
11712 }
11713 *curval++ = strval;
11714 }
11715 *curval++ = NULL;
11716
11717 write_reg_contents_lst(regname, lstval, -1,
11718 append, yank_type, block_len);
11719free_lstval:
11720 while (curallocval > allocval)
11721 vim_free(*--curallocval);
11722 vim_free(lstval);
11723 }
11724 else
11725 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011726 strval = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011727 if (strval == NULL)
11728 return;
11729 write_reg_contents_ex(regname, strval, -1,
11730 append, yank_type, block_len);
11731 }
11732 rettv->vval.v_number = 0;
11733}
11734
11735/*
11736 * "settabvar()" function
11737 */
11738 static void
11739f_settabvar(typval_T *argvars, typval_T *rettv)
11740{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011741 tabpage_T *save_curtab;
11742 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011743 char_u *varname, *tabvarname;
11744 typval_T *varp;
11745
11746 rettv->vval.v_number = 0;
11747
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011748 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011749 return;
11750
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011751 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
11752 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011753 varp = &argvars[2];
11754
Bram Moolenaar4033c552017-09-16 20:54:51 +020011755 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011756 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011757 save_curtab = curtab;
11758 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011759
11760 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
11761 if (tabvarname != NULL)
11762 {
11763 STRCPY(tabvarname, "t:");
11764 STRCPY(tabvarname + 2, varname);
11765 set_var(tabvarname, varp, TRUE);
11766 vim_free(tabvarname);
11767 }
11768
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011769 /* Restore current tabpage */
11770 if (valid_tabpage(save_curtab))
11771 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011772 }
11773}
11774
11775/*
11776 * "settabwinvar()" function
11777 */
11778 static void
11779f_settabwinvar(typval_T *argvars, typval_T *rettv)
11780{
11781 setwinvar(argvars, rettv, 1);
11782}
11783
11784/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011785 * "settagstack()" function
11786 */
11787 static void
11788f_settagstack(typval_T *argvars, typval_T *rettv)
11789{
11790 static char *e_invact2 = N_("E962: Invalid action: '%s'");
11791 win_T *wp;
11792 dict_T *d;
11793 int action = 'r';
11794
11795 rettv->vval.v_number = -1;
11796
11797 // first argument: window number or id
11798 wp = find_win_by_nr_or_id(&argvars[0]);
11799 if (wp == NULL)
11800 return;
11801
11802 // second argument: dict with items to set in the tag stack
11803 if (argvars[1].v_type != VAR_DICT)
11804 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011805 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011806 return;
11807 }
11808 d = argvars[1].vval.v_dict;
11809 if (d == NULL)
11810 return;
11811
11812 // third argument: action - 'a' for append and 'r' for replace.
11813 // default is to replace the stack.
11814 if (argvars[2].v_type == VAR_UNKNOWN)
11815 action = 'r';
11816 else if (argvars[2].v_type == VAR_STRING)
11817 {
11818 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011819 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011820 if (actstr == NULL)
11821 return;
11822 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
11823 action = *actstr;
11824 else
11825 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011826 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011827 return;
11828 }
11829 }
11830 else
11831 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011832 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011833 return;
11834 }
11835
11836 if (set_tagstack(wp, d, action) == OK)
11837 rettv->vval.v_number = 0;
11838}
11839
11840/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011841 * "setwinvar()" function
11842 */
11843 static void
11844f_setwinvar(typval_T *argvars, typval_T *rettv)
11845{
11846 setwinvar(argvars, rettv, 0);
11847}
11848
11849#ifdef FEAT_CRYPT
11850/*
11851 * "sha256({string})" function
11852 */
11853 static void
11854f_sha256(typval_T *argvars, typval_T *rettv)
11855{
11856 char_u *p;
11857
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011858 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011859 rettv->vval.v_string = vim_strsave(
11860 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
11861 rettv->v_type = VAR_STRING;
11862}
11863#endif /* FEAT_CRYPT */
11864
11865/*
11866 * "shellescape({string})" function
11867 */
11868 static void
11869f_shellescape(typval_T *argvars, typval_T *rettv)
11870{
Bram Moolenaar20615522017-06-05 18:46:26 +020011871 int do_special = non_zero_arg(&argvars[1]);
11872
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011873 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011874 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011875 rettv->v_type = VAR_STRING;
11876}
11877
11878/*
11879 * shiftwidth() function
11880 */
11881 static void
11882f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
11883{
Bram Moolenaarf9514162018-11-22 03:08:29 +010011884 rettv->vval.v_number = 0;
11885
11886 if (argvars[0].v_type != VAR_UNKNOWN)
11887 {
11888 long col;
11889
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011890 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +010011891 if (col < 0)
11892 return; // type error; errmsg already given
11893#ifdef FEAT_VARTABS
11894 rettv->vval.v_number = get_sw_value_col(curbuf, col);
11895 return;
11896#endif
11897 }
11898
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011899 rettv->vval.v_number = get_sw_value(curbuf);
11900}
11901
Bram Moolenaar162b7142018-12-21 15:17:36 +010011902#ifdef FEAT_SIGNS
11903/*
11904 * "sign_define()" function
11905 */
11906 static void
11907f_sign_define(typval_T *argvars, typval_T *rettv)
11908{
11909 char_u *name;
11910 dict_T *dict;
11911 char_u *icon = NULL;
11912 char_u *linehl = NULL;
11913 char_u *text = NULL;
11914 char_u *texthl = NULL;
11915
11916 rettv->vval.v_number = -1;
11917
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011918 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011919 if (name == NULL)
11920 return;
11921
11922 if (argvars[1].v_type != VAR_UNKNOWN)
11923 {
11924 if (argvars[1].v_type != VAR_DICT)
11925 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011926 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011927 return;
11928 }
11929
11930 // sign attributes
11931 dict = argvars[1].vval.v_dict;
11932 if (dict_find(dict, (char_u *)"icon", -1) != NULL)
11933 icon = dict_get_string(dict, (char_u *)"icon", TRUE);
11934 if (dict_find(dict, (char_u *)"linehl", -1) != NULL)
11935 linehl = dict_get_string(dict, (char_u *)"linehl", TRUE);
11936 if (dict_find(dict, (char_u *)"text", -1) != NULL)
11937 text = dict_get_string(dict, (char_u *)"text", TRUE);
11938 if (dict_find(dict, (char_u *)"texthl", -1) != NULL)
11939 texthl = dict_get_string(dict, (char_u *)"texthl", TRUE);
11940 }
11941
11942 if (sign_define_by_name(name, icon, linehl, text, texthl) == OK)
11943 rettv->vval.v_number = 0;
11944
11945 vim_free(icon);
11946 vim_free(linehl);
11947 vim_free(text);
11948 vim_free(texthl);
11949}
11950
11951/*
11952 * "sign_getdefined()" function
11953 */
11954 static void
11955f_sign_getdefined(typval_T *argvars, typval_T *rettv)
11956{
11957 char_u *name = NULL;
11958
11959 if (rettv_list_alloc_id(rettv, aid_sign_getdefined) != OK)
11960 return;
11961
11962 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011963 name = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011964
11965 sign_getlist(name, rettv->vval.v_list);
11966}
11967
11968/*
11969 * "sign_getplaced()" function
11970 */
11971 static void
11972f_sign_getplaced(typval_T *argvars, typval_T *rettv)
11973{
11974 buf_T *buf = NULL;
11975 dict_T *dict;
11976 dictitem_T *di;
11977 linenr_T lnum = 0;
11978 int sign_id = 0;
11979 char_u *group = NULL;
11980 int notanum = FALSE;
11981
11982 if (rettv_list_alloc_id(rettv, aid_sign_getplaced) != OK)
11983 return;
11984
11985 if (argvars[0].v_type != VAR_UNKNOWN)
11986 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010011987 // get signs placed in the specified buffer
11988 buf = get_buf_arg(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011989 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010011990 return;
Bram Moolenaar162b7142018-12-21 15:17:36 +010011991
11992 if (argvars[1].v_type != VAR_UNKNOWN)
11993 {
11994 if (argvars[1].v_type != VAR_DICT ||
11995 ((dict = argvars[1].vval.v_dict) == NULL))
11996 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011997 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011998 return;
11999 }
12000 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
12001 {
12002 // get signs placed at this line
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012003 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012004 if (notanum)
12005 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012006 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012007 }
12008 if ((di = dict_find(dict, (char_u *)"id", -1)) != NULL)
12009 {
12010 // get sign placed with this identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012011 sign_id = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012012 if (notanum)
12013 return;
12014 }
12015 if ((di = dict_find(dict, (char_u *)"group", -1)) != NULL)
12016 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012017 group = tv_get_string_chk(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012018 if (group == NULL)
12019 return;
Bram Moolenaar6436cd82018-12-27 00:28:33 +010012020 if (*group == '\0') // empty string means global group
12021 group = NULL;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012022 }
12023 }
12024 }
12025
12026 sign_get_placed(buf, lnum, sign_id, group, rettv->vval.v_list);
12027}
12028
12029/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012030 * "sign_jump()" function
12031 */
12032 static void
12033f_sign_jump(typval_T *argvars, typval_T *rettv)
12034{
12035 int sign_id;
12036 char_u *sign_group = NULL;
12037 buf_T *buf;
12038 int notanum = FALSE;
12039
12040 rettv->vval.v_number = -1;
12041
Bram Moolenaarbdace832019-03-02 10:13:42 +010012042 // Sign identifier
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012043 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
12044 if (notanum)
12045 return;
12046 if (sign_id <= 0)
12047 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012048 emsg(_(e_invarg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012049 return;
12050 }
12051
12052 // Sign group
12053 sign_group = tv_get_string_chk(&argvars[1]);
12054 if (sign_group == NULL)
12055 return;
12056 if (sign_group[0] == '\0')
12057 sign_group = NULL; // global sign group
12058 else
12059 {
12060 sign_group = vim_strsave(sign_group);
12061 if (sign_group == NULL)
12062 return;
12063 }
12064
12065 // Buffer to place the sign
12066 buf = get_buf_arg(&argvars[2]);
12067 if (buf == NULL)
12068 goto cleanup;
12069
12070 rettv->vval.v_number = sign_jump(sign_id, sign_group, buf);
12071
12072cleanup:
12073 vim_free(sign_group);
12074}
12075
12076/*
Bram Moolenaar162b7142018-12-21 15:17:36 +010012077 * "sign_place()" function
12078 */
12079 static void
12080f_sign_place(typval_T *argvars, typval_T *rettv)
12081{
12082 int sign_id;
12083 char_u *group = NULL;
12084 char_u *sign_name;
12085 buf_T *buf;
12086 dict_T *dict;
12087 dictitem_T *di;
12088 linenr_T lnum = 0;
12089 int prio = SIGN_DEF_PRIO;
12090 int notanum = FALSE;
12091
12092 rettv->vval.v_number = -1;
12093
Bram Moolenaarbdace832019-03-02 10:13:42 +010012094 // Sign identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012095 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012096 if (notanum)
12097 return;
12098 if (sign_id < 0)
12099 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012100 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012101 return;
12102 }
12103
12104 // Sign group
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012105 group = tv_get_string_chk(&argvars[1]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012106 if (group == NULL)
12107 return;
12108 if (group[0] == '\0')
12109 group = NULL; // global sign group
12110 else
12111 {
12112 group = vim_strsave(group);
12113 if (group == NULL)
12114 return;
12115 }
12116
12117 // Sign name
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012118 sign_name = tv_get_string_chk(&argvars[2]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012119 if (sign_name == NULL)
12120 goto cleanup;
12121
12122 // Buffer to place the sign
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012123 buf = get_buf_arg(&argvars[3]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012124 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012125 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012126
12127 if (argvars[4].v_type != VAR_UNKNOWN)
12128 {
12129 if (argvars[4].v_type != VAR_DICT ||
12130 ((dict = argvars[4].vval.v_dict) == NULL))
12131 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012132 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012133 goto cleanup;
12134 }
12135
12136 // Line number where the sign is to be placed
12137 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
12138 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012139 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012140 if (notanum)
12141 goto cleanup;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012142 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012143 }
12144 if ((di = dict_find(dict, (char_u *)"priority", -1)) != NULL)
12145 {
12146 // Sign priority
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012147 prio = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012148 if (notanum)
12149 goto cleanup;
12150 }
12151 }
12152
12153 if (sign_place(&sign_id, group, sign_name, buf, lnum, prio) == OK)
12154 rettv->vval.v_number = sign_id;
12155
12156cleanup:
12157 vim_free(group);
12158}
12159
12160/*
12161 * "sign_undefine()" function
12162 */
12163 static void
12164f_sign_undefine(typval_T *argvars, typval_T *rettv)
12165{
12166 char_u *name;
12167
12168 rettv->vval.v_number = -1;
12169
12170 if (argvars[0].v_type == VAR_UNKNOWN)
12171 {
12172 // Free all the signs
12173 free_signs();
12174 rettv->vval.v_number = 0;
12175 }
12176 else
12177 {
12178 // Free only the specified sign
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012179 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012180 if (name == NULL)
12181 return;
12182
12183 if (sign_undefine_by_name(name) == OK)
12184 rettv->vval.v_number = 0;
12185 }
12186}
12187
12188/*
12189 * "sign_unplace()" function
12190 */
12191 static void
12192f_sign_unplace(typval_T *argvars, typval_T *rettv)
12193{
12194 dict_T *dict;
12195 dictitem_T *di;
12196 int sign_id = 0;
12197 buf_T *buf = NULL;
12198 char_u *group = NULL;
12199
12200 rettv->vval.v_number = -1;
12201
12202 if (argvars[0].v_type != VAR_STRING)
12203 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012204 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012205 return;
12206 }
12207
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012208 group = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012209 if (group[0] == '\0')
12210 group = NULL; // global sign group
12211 else
12212 {
12213 group = vim_strsave(group);
12214 if (group == NULL)
12215 return;
12216 }
12217
12218 if (argvars[1].v_type != VAR_UNKNOWN)
12219 {
12220 if (argvars[1].v_type != VAR_DICT)
12221 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012222 emsg(_(e_dictreq));
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012223 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012224 }
12225 dict = argvars[1].vval.v_dict;
12226
12227 if ((di = dict_find(dict, (char_u *)"buffer", -1)) != NULL)
12228 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012229 buf = get_buf_arg(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012230 if (buf == NULL)
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012231 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012232 }
12233 if (dict_find(dict, (char_u *)"id", -1) != NULL)
12234 sign_id = dict_get_number(dict, (char_u *)"id");
12235 }
12236
12237 if (buf == NULL)
12238 {
12239 // Delete the sign in all the buffers
12240 FOR_ALL_BUFFERS(buf)
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010012241 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012242 rettv->vval.v_number = 0;
12243 }
12244 else
12245 {
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010012246 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012247 rettv->vval.v_number = 0;
12248 }
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012249
12250cleanup:
Bram Moolenaar162b7142018-12-21 15:17:36 +010012251 vim_free(group);
12252}
12253#endif
12254
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012255/*
12256 * "simplify()" function
12257 */
12258 static void
12259f_simplify(typval_T *argvars, typval_T *rettv)
12260{
12261 char_u *p;
12262
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012263 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012264 rettv->vval.v_string = vim_strsave(p);
12265 simplify_filename(rettv->vval.v_string); /* simplify in place */
12266 rettv->v_type = VAR_STRING;
12267}
12268
12269#ifdef FEAT_FLOAT
12270/*
12271 * "sin()" function
12272 */
12273 static void
12274f_sin(typval_T *argvars, typval_T *rettv)
12275{
12276 float_T f = 0.0;
12277
12278 rettv->v_type = VAR_FLOAT;
12279 if (get_float_arg(argvars, &f) == OK)
12280 rettv->vval.v_float = sin(f);
12281 else
12282 rettv->vval.v_float = 0.0;
12283}
12284
12285/*
12286 * "sinh()" function
12287 */
12288 static void
12289f_sinh(typval_T *argvars, typval_T *rettv)
12290{
12291 float_T f = 0.0;
12292
12293 rettv->v_type = VAR_FLOAT;
12294 if (get_float_arg(argvars, &f) == OK)
12295 rettv->vval.v_float = sinh(f);
12296 else
12297 rettv->vval.v_float = 0.0;
12298}
12299#endif
12300
12301static int
12302#ifdef __BORLANDC__
12303 _RTLENTRYF
12304#endif
12305 item_compare(const void *s1, const void *s2);
12306static int
12307#ifdef __BORLANDC__
12308 _RTLENTRYF
12309#endif
12310 item_compare2(const void *s1, const void *s2);
12311
12312/* struct used in the array that's given to qsort() */
12313typedef struct
12314{
12315 listitem_T *item;
12316 int idx;
12317} sortItem_T;
12318
12319/* struct storing information about current sort */
12320typedef struct
12321{
12322 int item_compare_ic;
12323 int item_compare_numeric;
12324 int item_compare_numbers;
12325#ifdef FEAT_FLOAT
12326 int item_compare_float;
12327#endif
12328 char_u *item_compare_func;
12329 partial_T *item_compare_partial;
12330 dict_T *item_compare_selfdict;
12331 int item_compare_func_err;
12332 int item_compare_keep_zero;
12333} sortinfo_T;
12334static sortinfo_T *sortinfo = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012335#define ITEM_COMPARE_FAIL 999
12336
12337/*
12338 * Compare functions for f_sort() and f_uniq() below.
12339 */
12340 static int
12341#ifdef __BORLANDC__
12342_RTLENTRYF
12343#endif
12344item_compare(const void *s1, const void *s2)
12345{
12346 sortItem_T *si1, *si2;
12347 typval_T *tv1, *tv2;
12348 char_u *p1, *p2;
12349 char_u *tofree1 = NULL, *tofree2 = NULL;
12350 int res;
12351 char_u numbuf1[NUMBUFLEN];
12352 char_u numbuf2[NUMBUFLEN];
12353
12354 si1 = (sortItem_T *)s1;
12355 si2 = (sortItem_T *)s2;
12356 tv1 = &si1->item->li_tv;
12357 tv2 = &si2->item->li_tv;
12358
12359 if (sortinfo->item_compare_numbers)
12360 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012361 varnumber_T v1 = tv_get_number(tv1);
12362 varnumber_T v2 = tv_get_number(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012363
12364 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
12365 }
12366
12367#ifdef FEAT_FLOAT
12368 if (sortinfo->item_compare_float)
12369 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012370 float_T v1 = tv_get_float(tv1);
12371 float_T v2 = tv_get_float(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012372
12373 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
12374 }
12375#endif
12376
12377 /* tv2string() puts quotes around a string and allocates memory. Don't do
12378 * that for string variables. Use a single quote when comparing with a
12379 * non-string to do what the docs promise. */
12380 if (tv1->v_type == VAR_STRING)
12381 {
12382 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12383 p1 = (char_u *)"'";
12384 else
12385 p1 = tv1->vval.v_string;
12386 }
12387 else
12388 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
12389 if (tv2->v_type == VAR_STRING)
12390 {
12391 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12392 p2 = (char_u *)"'";
12393 else
12394 p2 = tv2->vval.v_string;
12395 }
12396 else
12397 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
12398 if (p1 == NULL)
12399 p1 = (char_u *)"";
12400 if (p2 == NULL)
12401 p2 = (char_u *)"";
12402 if (!sortinfo->item_compare_numeric)
12403 {
12404 if (sortinfo->item_compare_ic)
12405 res = STRICMP(p1, p2);
12406 else
12407 res = STRCMP(p1, p2);
12408 }
12409 else
12410 {
12411 double n1, n2;
12412 n1 = strtod((char *)p1, (char **)&p1);
12413 n2 = strtod((char *)p2, (char **)&p2);
12414 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
12415 }
12416
12417 /* When the result would be zero, compare the item indexes. Makes the
12418 * sort stable. */
12419 if (res == 0 && !sortinfo->item_compare_keep_zero)
12420 res = si1->idx > si2->idx ? 1 : -1;
12421
12422 vim_free(tofree1);
12423 vim_free(tofree2);
12424 return res;
12425}
12426
12427 static int
12428#ifdef __BORLANDC__
12429_RTLENTRYF
12430#endif
12431item_compare2(const void *s1, const void *s2)
12432{
12433 sortItem_T *si1, *si2;
12434 int res;
12435 typval_T rettv;
12436 typval_T argv[3];
12437 int dummy;
12438 char_u *func_name;
12439 partial_T *partial = sortinfo->item_compare_partial;
12440
12441 /* shortcut after failure in previous call; compare all items equal */
12442 if (sortinfo->item_compare_func_err)
12443 return 0;
12444
12445 si1 = (sortItem_T *)s1;
12446 si2 = (sortItem_T *)s2;
12447
12448 if (partial == NULL)
12449 func_name = sortinfo->item_compare_func;
12450 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012451 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012452
12453 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
12454 * in the copy without changing the original list items. */
12455 copy_tv(&si1->item->li_tv, &argv[0]);
12456 copy_tv(&si2->item->li_tv, &argv[1]);
12457
12458 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
12459 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020012460 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012461 partial, sortinfo->item_compare_selfdict);
12462 clear_tv(&argv[0]);
12463 clear_tv(&argv[1]);
12464
12465 if (res == FAIL)
12466 res = ITEM_COMPARE_FAIL;
12467 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012468 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012469 if (sortinfo->item_compare_func_err)
12470 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
12471 clear_tv(&rettv);
12472
12473 /* When the result would be zero, compare the pointers themselves. Makes
12474 * the sort stable. */
12475 if (res == 0 && !sortinfo->item_compare_keep_zero)
12476 res = si1->idx > si2->idx ? 1 : -1;
12477
12478 return res;
12479}
12480
12481/*
12482 * "sort({list})" function
12483 */
12484 static void
12485do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
12486{
12487 list_T *l;
12488 listitem_T *li;
12489 sortItem_T *ptrs;
12490 sortinfo_T *old_sortinfo;
12491 sortinfo_T info;
12492 long len;
12493 long i;
12494
12495 /* Pointer to current info struct used in compare function. Save and
12496 * restore the current one for nested calls. */
12497 old_sortinfo = sortinfo;
12498 sortinfo = &info;
12499
12500 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012501 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012502 else
12503 {
12504 l = argvars[0].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +010012505 if (l == NULL || var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012506 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
12507 TRUE))
12508 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012509 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012510
12511 len = list_len(l);
12512 if (len <= 1)
12513 goto theend; /* short list sorts pretty quickly */
12514
12515 info.item_compare_ic = FALSE;
12516 info.item_compare_numeric = FALSE;
12517 info.item_compare_numbers = FALSE;
12518#ifdef FEAT_FLOAT
12519 info.item_compare_float = FALSE;
12520#endif
12521 info.item_compare_func = NULL;
12522 info.item_compare_partial = NULL;
12523 info.item_compare_selfdict = NULL;
12524 if (argvars[1].v_type != VAR_UNKNOWN)
12525 {
12526 /* optional second argument: {func} */
12527 if (argvars[1].v_type == VAR_FUNC)
12528 info.item_compare_func = argvars[1].vval.v_string;
12529 else if (argvars[1].v_type == VAR_PARTIAL)
12530 info.item_compare_partial = argvars[1].vval.v_partial;
12531 else
12532 {
12533 int error = FALSE;
12534
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012535 i = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012536 if (error)
12537 goto theend; /* type error; errmsg already given */
12538 if (i == 1)
12539 info.item_compare_ic = TRUE;
12540 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012541 info.item_compare_func = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012542 else if (i != 0)
12543 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012544 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012545 goto theend;
12546 }
12547 if (info.item_compare_func != NULL)
12548 {
12549 if (*info.item_compare_func == NUL)
12550 {
12551 /* empty string means default sort */
12552 info.item_compare_func = NULL;
12553 }
12554 else if (STRCMP(info.item_compare_func, "n") == 0)
12555 {
12556 info.item_compare_func = NULL;
12557 info.item_compare_numeric = TRUE;
12558 }
12559 else if (STRCMP(info.item_compare_func, "N") == 0)
12560 {
12561 info.item_compare_func = NULL;
12562 info.item_compare_numbers = TRUE;
12563 }
12564#ifdef FEAT_FLOAT
12565 else if (STRCMP(info.item_compare_func, "f") == 0)
12566 {
12567 info.item_compare_func = NULL;
12568 info.item_compare_float = TRUE;
12569 }
12570#endif
12571 else if (STRCMP(info.item_compare_func, "i") == 0)
12572 {
12573 info.item_compare_func = NULL;
12574 info.item_compare_ic = TRUE;
12575 }
12576 }
12577 }
12578
12579 if (argvars[2].v_type != VAR_UNKNOWN)
12580 {
12581 /* optional third argument: {dict} */
12582 if (argvars[2].v_type != VAR_DICT)
12583 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012584 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012585 goto theend;
12586 }
12587 info.item_compare_selfdict = argvars[2].vval.v_dict;
12588 }
12589 }
12590
12591 /* Make an array with each entry pointing to an item in the List. */
12592 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
12593 if (ptrs == NULL)
12594 goto theend;
12595
12596 i = 0;
12597 if (sort)
12598 {
12599 /* sort(): ptrs will be the list to sort */
12600 for (li = l->lv_first; li != NULL; li = li->li_next)
12601 {
12602 ptrs[i].item = li;
12603 ptrs[i].idx = i;
12604 ++i;
12605 }
12606
12607 info.item_compare_func_err = FALSE;
12608 info.item_compare_keep_zero = FALSE;
12609 /* test the compare function */
12610 if ((info.item_compare_func != NULL
12611 || info.item_compare_partial != NULL)
12612 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
12613 == ITEM_COMPARE_FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012614 emsg(_("E702: Sort compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012615 else
12616 {
12617 /* Sort the array with item pointers. */
12618 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
12619 info.item_compare_func == NULL
12620 && info.item_compare_partial == NULL
12621 ? item_compare : item_compare2);
12622
12623 if (!info.item_compare_func_err)
12624 {
12625 /* Clear the List and append the items in sorted order. */
12626 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
12627 l->lv_len = 0;
12628 for (i = 0; i < len; ++i)
12629 list_append(l, ptrs[i].item);
12630 }
12631 }
12632 }
12633 else
12634 {
12635 int (*item_compare_func_ptr)(const void *, const void *);
12636
12637 /* f_uniq(): ptrs will be a stack of items to remove */
12638 info.item_compare_func_err = FALSE;
12639 info.item_compare_keep_zero = TRUE;
12640 item_compare_func_ptr = info.item_compare_func != NULL
12641 || info.item_compare_partial != NULL
12642 ? item_compare2 : item_compare;
12643
12644 for (li = l->lv_first; li != NULL && li->li_next != NULL;
12645 li = li->li_next)
12646 {
12647 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
12648 == 0)
12649 ptrs[i++].item = li;
12650 if (info.item_compare_func_err)
12651 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012652 emsg(_("E882: Uniq compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012653 break;
12654 }
12655 }
12656
12657 if (!info.item_compare_func_err)
12658 {
12659 while (--i >= 0)
12660 {
12661 li = ptrs[i].item->li_next;
12662 ptrs[i].item->li_next = li->li_next;
12663 if (li->li_next != NULL)
12664 li->li_next->li_prev = ptrs[i].item;
12665 else
12666 l->lv_last = ptrs[i].item;
12667 list_fix_watch(l, li);
12668 listitem_free(li);
12669 l->lv_len--;
12670 }
12671 }
12672 }
12673
12674 vim_free(ptrs);
12675 }
12676theend:
12677 sortinfo = old_sortinfo;
12678}
12679
12680/*
12681 * "sort({list})" function
12682 */
12683 static void
12684f_sort(typval_T *argvars, typval_T *rettv)
12685{
12686 do_sort_uniq(argvars, rettv, TRUE);
12687}
12688
12689/*
12690 * "uniq({list})" function
12691 */
12692 static void
12693f_uniq(typval_T *argvars, typval_T *rettv)
12694{
12695 do_sort_uniq(argvars, rettv, FALSE);
12696}
12697
12698/*
12699 * "soundfold({word})" function
12700 */
12701 static void
12702f_soundfold(typval_T *argvars, typval_T *rettv)
12703{
12704 char_u *s;
12705
12706 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012707 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012708#ifdef FEAT_SPELL
12709 rettv->vval.v_string = eval_soundfold(s);
12710#else
12711 rettv->vval.v_string = vim_strsave(s);
12712#endif
12713}
12714
12715/*
12716 * "spellbadword()" function
12717 */
12718 static void
12719f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
12720{
12721 char_u *word = (char_u *)"";
12722 hlf_T attr = HLF_COUNT;
12723 int len = 0;
12724
12725 if (rettv_list_alloc(rettv) == FAIL)
12726 return;
12727
12728#ifdef FEAT_SPELL
12729 if (argvars[0].v_type == VAR_UNKNOWN)
12730 {
12731 /* Find the start and length of the badly spelled word. */
12732 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
12733 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012734 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012735 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012736 curwin->w_set_curswant = TRUE;
12737 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012738 }
12739 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
12740 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012741 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012742 int capcol = -1;
12743
12744 if (str != NULL)
12745 {
12746 /* Check the argument for spelling. */
12747 while (*str != NUL)
12748 {
12749 len = spell_check(curwin, str, &attr, &capcol, FALSE);
12750 if (attr != HLF_COUNT)
12751 {
12752 word = str;
12753 break;
12754 }
12755 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020012756 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012757 }
12758 }
12759 }
12760#endif
12761
12762 list_append_string(rettv->vval.v_list, word, len);
12763 list_append_string(rettv->vval.v_list, (char_u *)(
12764 attr == HLF_SPB ? "bad" :
12765 attr == HLF_SPR ? "rare" :
12766 attr == HLF_SPL ? "local" :
12767 attr == HLF_SPC ? "caps" :
12768 ""), -1);
12769}
12770
12771/*
12772 * "spellsuggest()" function
12773 */
12774 static void
12775f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
12776{
12777#ifdef FEAT_SPELL
12778 char_u *str;
12779 int typeerr = FALSE;
12780 int maxcount;
12781 garray_T ga;
12782 int i;
12783 listitem_T *li;
12784 int need_capital = FALSE;
12785#endif
12786
12787 if (rettv_list_alloc(rettv) == FAIL)
12788 return;
12789
12790#ifdef FEAT_SPELL
12791 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
12792 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012793 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012794 if (argvars[1].v_type != VAR_UNKNOWN)
12795 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012796 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012797 if (maxcount <= 0)
12798 return;
12799 if (argvars[2].v_type != VAR_UNKNOWN)
12800 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012801 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012802 if (typeerr)
12803 return;
12804 }
12805 }
12806 else
12807 maxcount = 25;
12808
12809 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
12810
12811 for (i = 0; i < ga.ga_len; ++i)
12812 {
12813 str = ((char_u **)ga.ga_data)[i];
12814
12815 li = listitem_alloc();
12816 if (li == NULL)
12817 vim_free(str);
12818 else
12819 {
12820 li->li_tv.v_type = VAR_STRING;
12821 li->li_tv.v_lock = 0;
12822 li->li_tv.vval.v_string = str;
12823 list_append(rettv->vval.v_list, li);
12824 }
12825 }
12826 ga_clear(&ga);
12827 }
12828#endif
12829}
12830
12831 static void
12832f_split(typval_T *argvars, typval_T *rettv)
12833{
12834 char_u *str;
12835 char_u *end;
12836 char_u *pat = NULL;
12837 regmatch_T regmatch;
12838 char_u patbuf[NUMBUFLEN];
12839 char_u *save_cpo;
12840 int match;
12841 colnr_T col = 0;
12842 int keepempty = FALSE;
12843 int typeerr = FALSE;
12844
12845 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12846 save_cpo = p_cpo;
12847 p_cpo = (char_u *)"";
12848
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012849 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012850 if (argvars[1].v_type != VAR_UNKNOWN)
12851 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012852 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012853 if (pat == NULL)
12854 typeerr = TRUE;
12855 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012856 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012857 }
12858 if (pat == NULL || *pat == NUL)
12859 pat = (char_u *)"[\\x01- ]\\+";
12860
12861 if (rettv_list_alloc(rettv) == FAIL)
12862 return;
12863 if (typeerr)
12864 return;
12865
12866 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12867 if (regmatch.regprog != NULL)
12868 {
12869 regmatch.rm_ic = FALSE;
12870 while (*str != NUL || keepempty)
12871 {
12872 if (*str == NUL)
12873 match = FALSE; /* empty item at the end */
12874 else
12875 match = vim_regexec_nl(&regmatch, str, col);
12876 if (match)
12877 end = regmatch.startp[0];
12878 else
12879 end = str + STRLEN(str);
12880 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
12881 && *str != NUL && match && end < regmatch.endp[0]))
12882 {
12883 if (list_append_string(rettv->vval.v_list, str,
12884 (int)(end - str)) == FAIL)
12885 break;
12886 }
12887 if (!match)
12888 break;
Bram Moolenaar13505972019-01-24 15:04:48 +010012889 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012890 if (regmatch.endp[0] > str)
12891 col = 0;
12892 else
Bram Moolenaar13505972019-01-24 15:04:48 +010012893 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012894 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012895 str = regmatch.endp[0];
12896 }
12897
12898 vim_regfree(regmatch.regprog);
12899 }
12900
12901 p_cpo = save_cpo;
12902}
12903
12904#ifdef FEAT_FLOAT
12905/*
12906 * "sqrt()" function
12907 */
12908 static void
12909f_sqrt(typval_T *argvars, typval_T *rettv)
12910{
12911 float_T f = 0.0;
12912
12913 rettv->v_type = VAR_FLOAT;
12914 if (get_float_arg(argvars, &f) == OK)
12915 rettv->vval.v_float = sqrt(f);
12916 else
12917 rettv->vval.v_float = 0.0;
12918}
12919
12920/*
12921 * "str2float()" function
12922 */
12923 static void
12924f_str2float(typval_T *argvars, typval_T *rettv)
12925{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012926 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010012927 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012928
Bram Moolenaar08243d22017-01-10 16:12:29 +010012929 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012930 p = skipwhite(p + 1);
12931 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010012932 if (isneg)
12933 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012934 rettv->v_type = VAR_FLOAT;
12935}
12936#endif
12937
12938/*
Bram Moolenaar9d401282019-04-06 13:18:12 +020012939 * "str2list()" function
12940 */
12941 static void
12942f_str2list(typval_T *argvars, typval_T *rettv)
12943{
12944 char_u *p;
12945 int utf8 = FALSE;
12946
12947 if (rettv_list_alloc(rettv) == FAIL)
12948 return;
12949
12950 if (argvars[1].v_type != VAR_UNKNOWN)
12951 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
12952
12953 p = tv_get_string(&argvars[0]);
12954
12955 if (has_mbyte || utf8)
12956 {
12957 int (*ptr2len)(char_u *);
12958 int (*ptr2char)(char_u *);
12959
12960 if (utf8 || enc_utf8)
12961 {
12962 ptr2len = utf_ptr2len;
12963 ptr2char = utf_ptr2char;
12964 }
12965 else
12966 {
12967 ptr2len = mb_ptr2len;
12968 ptr2char = mb_ptr2char;
12969 }
12970
12971 for ( ; *p != NUL; p += (*ptr2len)(p))
12972 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
12973 }
12974 else
12975 for ( ; *p != NUL; ++p)
12976 list_append_number(rettv->vval.v_list, *p);
12977}
12978
12979/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012980 * "str2nr()" function
12981 */
12982 static void
12983f_str2nr(typval_T *argvars, typval_T *rettv)
12984{
12985 int base = 10;
12986 char_u *p;
12987 varnumber_T n;
12988 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010012989 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012990
12991 if (argvars[1].v_type != VAR_UNKNOWN)
12992 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012993 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012994 if (base != 2 && base != 8 && base != 10 && base != 16)
12995 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012996 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012997 return;
12998 }
12999 }
13000
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013001 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010013002 isneg = (*p == '-');
13003 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013004 p = skipwhite(p + 1);
13005 switch (base)
13006 {
13007 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
13008 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
13009 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
13010 default: what = 0;
13011 }
13012 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010013013 if (isneg)
13014 rettv->vval.v_number = -n;
13015 else
13016 rettv->vval.v_number = n;
13017
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013018}
13019
13020#ifdef HAVE_STRFTIME
13021/*
13022 * "strftime({format}[, {time}])" function
13023 */
13024 static void
13025f_strftime(typval_T *argvars, typval_T *rettv)
13026{
13027 char_u result_buf[256];
13028 struct tm *curtime;
13029 time_t seconds;
13030 char_u *p;
13031
13032 rettv->v_type = VAR_STRING;
13033
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013034 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013035 if (argvars[1].v_type == VAR_UNKNOWN)
13036 seconds = time(NULL);
13037 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013038 seconds = (time_t)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013039 curtime = localtime(&seconds);
13040 /* MSVC returns NULL for an invalid value of seconds. */
13041 if (curtime == NULL)
13042 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
13043 else
13044 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013045 vimconv_T conv;
13046 char_u *enc;
13047
13048 conv.vc_type = CONV_NONE;
13049 enc = enc_locale();
13050 convert_setup(&conv, p_enc, enc);
13051 if (conv.vc_type != CONV_NONE)
13052 p = string_convert(&conv, p, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013053 if (p != NULL)
13054 (void)strftime((char *)result_buf, sizeof(result_buf),
13055 (char *)p, curtime);
13056 else
13057 result_buf[0] = NUL;
13058
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013059 if (conv.vc_type != CONV_NONE)
13060 vim_free(p);
13061 convert_setup(&conv, enc, p_enc);
13062 if (conv.vc_type != CONV_NONE)
13063 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
13064 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013065 rettv->vval.v_string = vim_strsave(result_buf);
13066
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013067 /* Release conversion descriptors */
13068 convert_setup(&conv, NULL, NULL);
13069 vim_free(enc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013070 }
13071}
13072#endif
13073
13074/*
13075 * "strgetchar()" function
13076 */
13077 static void
13078f_strgetchar(typval_T *argvars, typval_T *rettv)
13079{
13080 char_u *str;
13081 int len;
13082 int error = FALSE;
13083 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +010013084 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013085
13086 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013087 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013088 if (str == NULL)
13089 return;
13090 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013091 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013092 if (error)
13093 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013094
Bram Moolenaar13505972019-01-24 15:04:48 +010013095 while (charidx >= 0 && byteidx < len)
13096 {
13097 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013098 {
Bram Moolenaar13505972019-01-24 15:04:48 +010013099 rettv->vval.v_number = mb_ptr2char(str + byteidx);
13100 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013101 }
Bram Moolenaar13505972019-01-24 15:04:48 +010013102 --charidx;
13103 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013104 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013105}
13106
13107/*
13108 * "stridx()" function
13109 */
13110 static void
13111f_stridx(typval_T *argvars, typval_T *rettv)
13112{
13113 char_u buf[NUMBUFLEN];
13114 char_u *needle;
13115 char_u *haystack;
13116 char_u *save_haystack;
13117 char_u *pos;
13118 int start_idx;
13119
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013120 needle = tv_get_string_chk(&argvars[1]);
13121 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013122 rettv->vval.v_number = -1;
13123 if (needle == NULL || haystack == NULL)
13124 return; /* type error; errmsg already given */
13125
13126 if (argvars[2].v_type != VAR_UNKNOWN)
13127 {
13128 int error = FALSE;
13129
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013130 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013131 if (error || start_idx >= (int)STRLEN(haystack))
13132 return;
13133 if (start_idx >= 0)
13134 haystack += start_idx;
13135 }
13136
13137 pos = (char_u *)strstr((char *)haystack, (char *)needle);
13138 if (pos != NULL)
13139 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
13140}
13141
13142/*
13143 * "string()" function
13144 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010013145 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013146f_string(typval_T *argvars, typval_T *rettv)
13147{
13148 char_u *tofree;
13149 char_u numbuf[NUMBUFLEN];
13150
13151 rettv->v_type = VAR_STRING;
13152 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
13153 get_copyID());
13154 /* Make a copy if we have a value but it's not in allocated memory. */
13155 if (rettv->vval.v_string != NULL && tofree == NULL)
13156 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
13157}
13158
13159/*
13160 * "strlen()" function
13161 */
13162 static void
13163f_strlen(typval_T *argvars, typval_T *rettv)
13164{
13165 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013166 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013167}
13168
13169/*
13170 * "strchars()" function
13171 */
13172 static void
13173f_strchars(typval_T *argvars, typval_T *rettv)
13174{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013175 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013176 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013177 varnumber_T len = 0;
13178 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013179
13180 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013181 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013182 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013183 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013184 else
13185 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013186 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
13187 while (*s != NUL)
13188 {
13189 func_mb_ptr2char_adv(&s);
13190 ++len;
13191 }
13192 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013193 }
13194}
13195
13196/*
13197 * "strdisplaywidth()" function
13198 */
13199 static void
13200f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
13201{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013202 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013203 int col = 0;
13204
13205 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013206 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013207
13208 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
13209}
13210
13211/*
13212 * "strwidth()" function
13213 */
13214 static void
13215f_strwidth(typval_T *argvars, typval_T *rettv)
13216{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013217 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013218
Bram Moolenaar13505972019-01-24 15:04:48 +010013219 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013220}
13221
13222/*
13223 * "strcharpart()" function
13224 */
13225 static void
13226f_strcharpart(typval_T *argvars, typval_T *rettv)
13227{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013228 char_u *p;
13229 int nchar;
13230 int nbyte = 0;
13231 int charlen;
13232 int len = 0;
13233 int slen;
13234 int error = FALSE;
13235
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013236 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013237 slen = (int)STRLEN(p);
13238
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013239 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013240 if (!error)
13241 {
13242 if (nchar > 0)
13243 while (nchar > 0 && nbyte < slen)
13244 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020013245 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013246 --nchar;
13247 }
13248 else
13249 nbyte = nchar;
13250 if (argvars[2].v_type != VAR_UNKNOWN)
13251 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013252 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013253 while (charlen > 0 && nbyte + len < slen)
13254 {
13255 int off = nbyte + len;
13256
13257 if (off < 0)
13258 len += 1;
13259 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020013260 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013261 --charlen;
13262 }
13263 }
13264 else
13265 len = slen - nbyte; /* default: all bytes that are available. */
13266 }
13267
13268 /*
13269 * Only return the overlap between the specified part and the actual
13270 * string.
13271 */
13272 if (nbyte < 0)
13273 {
13274 len += nbyte;
13275 nbyte = 0;
13276 }
13277 else if (nbyte > slen)
13278 nbyte = slen;
13279 if (len < 0)
13280 len = 0;
13281 else if (nbyte + len > slen)
13282 len = slen - nbyte;
13283
13284 rettv->v_type = VAR_STRING;
13285 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013286}
13287
13288/*
13289 * "strpart()" function
13290 */
13291 static void
13292f_strpart(typval_T *argvars, typval_T *rettv)
13293{
13294 char_u *p;
13295 int n;
13296 int len;
13297 int slen;
13298 int error = FALSE;
13299
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013300 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013301 slen = (int)STRLEN(p);
13302
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013303 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013304 if (error)
13305 len = 0;
13306 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013307 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013308 else
13309 len = slen - n; /* default len: all bytes that are available. */
13310
13311 /*
13312 * Only return the overlap between the specified part and the actual
13313 * string.
13314 */
13315 if (n < 0)
13316 {
13317 len += n;
13318 n = 0;
13319 }
13320 else if (n > slen)
13321 n = slen;
13322 if (len < 0)
13323 len = 0;
13324 else if (n + len > slen)
13325 len = slen - n;
13326
13327 rettv->v_type = VAR_STRING;
13328 rettv->vval.v_string = vim_strnsave(p + n, len);
13329}
13330
13331/*
13332 * "strridx()" function
13333 */
13334 static void
13335f_strridx(typval_T *argvars, typval_T *rettv)
13336{
13337 char_u buf[NUMBUFLEN];
13338 char_u *needle;
13339 char_u *haystack;
13340 char_u *rest;
13341 char_u *lastmatch = NULL;
13342 int haystack_len, end_idx;
13343
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013344 needle = tv_get_string_chk(&argvars[1]);
13345 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013346
13347 rettv->vval.v_number = -1;
13348 if (needle == NULL || haystack == NULL)
13349 return; /* type error; errmsg already given */
13350
13351 haystack_len = (int)STRLEN(haystack);
13352 if (argvars[2].v_type != VAR_UNKNOWN)
13353 {
13354 /* Third argument: upper limit for index */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013355 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013356 if (end_idx < 0)
13357 return; /* can never find a match */
13358 }
13359 else
13360 end_idx = haystack_len;
13361
13362 if (*needle == NUL)
13363 {
13364 /* Empty string matches past the end. */
13365 lastmatch = haystack + end_idx;
13366 }
13367 else
13368 {
13369 for (rest = haystack; *rest != '\0'; ++rest)
13370 {
13371 rest = (char_u *)strstr((char *)rest, (char *)needle);
13372 if (rest == NULL || rest > haystack + end_idx)
13373 break;
13374 lastmatch = rest;
13375 }
13376 }
13377
13378 if (lastmatch == NULL)
13379 rettv->vval.v_number = -1;
13380 else
13381 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
13382}
13383
13384/*
13385 * "strtrans()" function
13386 */
13387 static void
13388f_strtrans(typval_T *argvars, typval_T *rettv)
13389{
13390 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013391 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013392}
13393
13394/*
13395 * "submatch()" function
13396 */
13397 static void
13398f_submatch(typval_T *argvars, typval_T *rettv)
13399{
13400 int error = FALSE;
13401 int no;
13402 int retList = 0;
13403
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013404 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013405 if (error)
13406 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013407 if (no < 0 || no >= NSUBEXP)
13408 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013409 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +010013410 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013411 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013412 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013413 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013414 if (error)
13415 return;
13416
13417 if (retList == 0)
13418 {
13419 rettv->v_type = VAR_STRING;
13420 rettv->vval.v_string = reg_submatch(no);
13421 }
13422 else
13423 {
13424 rettv->v_type = VAR_LIST;
13425 rettv->vval.v_list = reg_submatch_list(no);
13426 }
13427}
13428
13429/*
13430 * "substitute()" function
13431 */
13432 static void
13433f_substitute(typval_T *argvars, typval_T *rettv)
13434{
13435 char_u patbuf[NUMBUFLEN];
13436 char_u subbuf[NUMBUFLEN];
13437 char_u flagsbuf[NUMBUFLEN];
13438
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013439 char_u *str = tv_get_string_chk(&argvars[0]);
13440 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013441 char_u *sub = NULL;
13442 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013443 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013444
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013445 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
13446 expr = &argvars[2];
13447 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013448 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013449
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013450 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013451 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
13452 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013453 rettv->vval.v_string = NULL;
13454 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013455 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013456}
13457
13458/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013459 * "swapinfo(swap_filename)" function
13460 */
13461 static void
13462f_swapinfo(typval_T *argvars, typval_T *rettv)
13463{
13464 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013465 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013466}
13467
13468/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020013469 * "swapname(expr)" function
13470 */
13471 static void
13472f_swapname(typval_T *argvars, typval_T *rettv)
13473{
13474 buf_T *buf;
13475
13476 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010013477 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +020013478 if (buf == NULL || buf->b_ml.ml_mfp == NULL
13479 || buf->b_ml.ml_mfp->mf_fname == NULL)
13480 rettv->vval.v_string = NULL;
13481 else
13482 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
13483}
13484
13485/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013486 * "synID(lnum, col, trans)" function
13487 */
13488 static void
13489f_synID(typval_T *argvars UNUSED, typval_T *rettv)
13490{
13491 int id = 0;
13492#ifdef FEAT_SYN_HL
13493 linenr_T lnum;
13494 colnr_T col;
13495 int trans;
13496 int transerr = FALSE;
13497
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013498 lnum = tv_get_lnum(argvars); /* -1 on type error */
13499 col = (linenr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
13500 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013501
13502 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13503 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
13504 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
13505#endif
13506
13507 rettv->vval.v_number = id;
13508}
13509
13510/*
13511 * "synIDattr(id, what [, mode])" function
13512 */
13513 static void
13514f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
13515{
13516 char_u *p = NULL;
13517#ifdef FEAT_SYN_HL
13518 int id;
13519 char_u *what;
13520 char_u *mode;
13521 char_u modebuf[NUMBUFLEN];
13522 int modec;
13523
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013524 id = (int)tv_get_number(&argvars[0]);
13525 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013526 if (argvars[2].v_type != VAR_UNKNOWN)
13527 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013528 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013529 modec = TOLOWER_ASC(mode[0]);
13530 if (modec != 't' && modec != 'c' && modec != 'g')
13531 modec = 0; /* replace invalid with current */
13532 }
13533 else
13534 {
13535#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
13536 if (USE_24BIT)
13537 modec = 'g';
13538 else
13539#endif
13540 if (t_colors > 1)
13541 modec = 'c';
13542 else
13543 modec = 't';
13544 }
13545
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013546 switch (TOLOWER_ASC(what[0]))
13547 {
13548 case 'b':
13549 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
13550 p = highlight_color(id, what, modec);
13551 else /* bold */
13552 p = highlight_has_attr(id, HL_BOLD, modec);
13553 break;
13554
13555 case 'f': /* fg[#] or font */
13556 p = highlight_color(id, what, modec);
13557 break;
13558
13559 case 'i':
13560 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
13561 p = highlight_has_attr(id, HL_INVERSE, modec);
13562 else /* italic */
13563 p = highlight_has_attr(id, HL_ITALIC, modec);
13564 break;
13565
13566 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020013567 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013568 break;
13569
13570 case 'r': /* reverse */
13571 p = highlight_has_attr(id, HL_INVERSE, modec);
13572 break;
13573
13574 case 's':
13575 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
13576 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020013577 /* strikeout */
13578 else if (TOLOWER_ASC(what[1]) == 't' &&
13579 TOLOWER_ASC(what[2]) == 'r')
13580 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013581 else /* standout */
13582 p = highlight_has_attr(id, HL_STANDOUT, modec);
13583 break;
13584
13585 case 'u':
13586 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
13587 /* underline */
13588 p = highlight_has_attr(id, HL_UNDERLINE, modec);
13589 else
13590 /* undercurl */
13591 p = highlight_has_attr(id, HL_UNDERCURL, modec);
13592 break;
13593 }
13594
13595 if (p != NULL)
13596 p = vim_strsave(p);
13597#endif
13598 rettv->v_type = VAR_STRING;
13599 rettv->vval.v_string = p;
13600}
13601
13602/*
13603 * "synIDtrans(id)" function
13604 */
13605 static void
13606f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
13607{
13608 int id;
13609
13610#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013611 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013612
13613 if (id > 0)
13614 id = syn_get_final_id(id);
13615 else
13616#endif
13617 id = 0;
13618
13619 rettv->vval.v_number = id;
13620}
13621
13622/*
13623 * "synconcealed(lnum, col)" function
13624 */
13625 static void
13626f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
13627{
13628#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
13629 linenr_T lnum;
13630 colnr_T col;
13631 int syntax_flags = 0;
13632 int cchar;
13633 int matchid = 0;
13634 char_u str[NUMBUFLEN];
13635#endif
13636
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013637 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013638
13639#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013640 lnum = tv_get_lnum(argvars); /* -1 on type error */
13641 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013642
13643 vim_memset(str, NUL, sizeof(str));
13644
13645 if (rettv_list_alloc(rettv) != FAIL)
13646 {
13647 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13648 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13649 && curwin->w_p_cole > 0)
13650 {
13651 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
13652 syntax_flags = get_syntax_info(&matchid);
13653
13654 /* get the conceal character */
13655 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
13656 {
13657 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020013658 if (cchar == NUL && curwin->w_p_cole == 1)
13659 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013660 if (cchar != NUL)
13661 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013662 if (has_mbyte)
13663 (*mb_char2bytes)(cchar, str);
13664 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013665 str[0] = cchar;
13666 }
13667 }
13668 }
13669
13670 list_append_number(rettv->vval.v_list,
13671 (syntax_flags & HL_CONCEAL) != 0);
13672 /* -1 to auto-determine strlen */
13673 list_append_string(rettv->vval.v_list, str, -1);
13674 list_append_number(rettv->vval.v_list, matchid);
13675 }
13676#endif
13677}
13678
13679/*
13680 * "synstack(lnum, col)" function
13681 */
13682 static void
13683f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
13684{
13685#ifdef FEAT_SYN_HL
13686 linenr_T lnum;
13687 colnr_T col;
13688 int i;
13689 int id;
13690#endif
13691
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013692 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013693
13694#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013695 lnum = tv_get_lnum(argvars); /* -1 on type error */
13696 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013697
13698 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13699 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13700 && rettv_list_alloc(rettv) != FAIL)
13701 {
13702 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
13703 for (i = 0; ; ++i)
13704 {
13705 id = syn_get_stack_item(i);
13706 if (id < 0)
13707 break;
13708 if (list_append_number(rettv->vval.v_list, id) == FAIL)
13709 break;
13710 }
13711 }
13712#endif
13713}
13714
13715 static void
13716get_cmd_output_as_rettv(
13717 typval_T *argvars,
13718 typval_T *rettv,
13719 int retlist)
13720{
13721 char_u *res = NULL;
13722 char_u *p;
13723 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013724 int err = FALSE;
13725 FILE *fd;
13726 list_T *list = NULL;
13727 int flags = SHELL_SILENT;
13728
13729 rettv->v_type = VAR_STRING;
13730 rettv->vval.v_string = NULL;
13731 if (check_restricted() || check_secure())
13732 goto errret;
13733
13734 if (argvars[1].v_type != VAR_UNKNOWN)
13735 {
13736 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010013737 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013738 * command.
13739 */
13740 if ((infile = vim_tempname('i', TRUE)) == NULL)
13741 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013742 emsg(_(e_notmp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013743 goto errret;
13744 }
13745
13746 fd = mch_fopen((char *)infile, WRITEBIN);
13747 if (fd == NULL)
13748 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013749 semsg(_(e_notopen), infile);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013750 goto errret;
13751 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010013752 if (argvars[1].v_type == VAR_NUMBER)
13753 {
13754 linenr_T lnum;
13755 buf_T *buf;
13756
13757 buf = buflist_findnr(argvars[1].vval.v_number);
13758 if (buf == NULL)
13759 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013760 semsg(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010013761 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010013762 goto errret;
13763 }
13764
13765 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
13766 {
13767 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
13768 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
13769 {
13770 err = TRUE;
13771 break;
13772 }
13773 if (putc(NL, fd) == EOF)
13774 {
13775 err = TRUE;
13776 break;
13777 }
13778 }
13779 }
13780 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013781 {
13782 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
13783 err = TRUE;
13784 }
13785 else
13786 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010013787 size_t len;
13788 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013789
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013790 p = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013791 if (p == NULL)
13792 {
13793 fclose(fd);
13794 goto errret; /* type error; errmsg already given */
13795 }
13796 len = STRLEN(p);
13797 if (len > 0 && fwrite(p, len, 1, fd) != 1)
13798 err = TRUE;
13799 }
13800 if (fclose(fd) != 0)
13801 err = TRUE;
13802 if (err)
13803 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013804 emsg(_("E677: Error writing temp file"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013805 goto errret;
13806 }
13807 }
13808
13809 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
13810 * echoes typeahead, that messes up the display. */
13811 if (!msg_silent)
13812 flags += SHELL_COOKED;
13813
13814 if (retlist)
13815 {
13816 int len;
13817 listitem_T *li;
13818 char_u *s = NULL;
13819 char_u *start;
13820 char_u *end;
13821 int i;
13822
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013823 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013824 if (res == NULL)
13825 goto errret;
13826
13827 list = list_alloc();
13828 if (list == NULL)
13829 goto errret;
13830
13831 for (i = 0; i < len; ++i)
13832 {
13833 start = res + i;
13834 while (i < len && res[i] != NL)
13835 ++i;
13836 end = res + i;
13837
13838 s = alloc((unsigned)(end - start + 1));
13839 if (s == NULL)
13840 goto errret;
13841
13842 for (p = s; start < end; ++p, ++start)
13843 *p = *start == NUL ? NL : *start;
13844 *p = NUL;
13845
13846 li = listitem_alloc();
13847 if (li == NULL)
13848 {
13849 vim_free(s);
13850 goto errret;
13851 }
13852 li->li_tv.v_type = VAR_STRING;
13853 li->li_tv.v_lock = 0;
13854 li->li_tv.vval.v_string = s;
13855 list_append(list, li);
13856 }
13857
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013858 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013859 list = NULL;
13860 }
13861 else
13862 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013863 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar00590742019-02-15 21:06:09 +010013864#ifdef USE_CRNL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013865 /* translate <CR><NL> into <NL> */
13866 if (res != NULL)
13867 {
13868 char_u *s, *d;
13869
13870 d = res;
13871 for (s = res; *s; ++s)
13872 {
13873 if (s[0] == CAR && s[1] == NL)
13874 ++s;
13875 *d++ = *s;
13876 }
13877 *d = NUL;
13878 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013879#endif
13880 rettv->vval.v_string = res;
13881 res = NULL;
13882 }
13883
13884errret:
13885 if (infile != NULL)
13886 {
13887 mch_remove(infile);
13888 vim_free(infile);
13889 }
13890 if (res != NULL)
13891 vim_free(res);
13892 if (list != NULL)
13893 list_free(list);
13894}
13895
13896/*
13897 * "system()" function
13898 */
13899 static void
13900f_system(typval_T *argvars, typval_T *rettv)
13901{
13902 get_cmd_output_as_rettv(argvars, rettv, FALSE);
13903}
13904
13905/*
13906 * "systemlist()" function
13907 */
13908 static void
13909f_systemlist(typval_T *argvars, typval_T *rettv)
13910{
13911 get_cmd_output_as_rettv(argvars, rettv, TRUE);
13912}
13913
13914/*
13915 * "tabpagebuflist()" function
13916 */
13917 static void
13918f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13919{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013920 tabpage_T *tp;
13921 win_T *wp = NULL;
13922
13923 if (argvars[0].v_type == VAR_UNKNOWN)
13924 wp = firstwin;
13925 else
13926 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013927 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013928 if (tp != NULL)
13929 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
13930 }
13931 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
13932 {
13933 for (; wp != NULL; wp = wp->w_next)
13934 if (list_append_number(rettv->vval.v_list,
13935 wp->w_buffer->b_fnum) == FAIL)
13936 break;
13937 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013938}
13939
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013940/*
13941 * "tabpagenr()" function
13942 */
13943 static void
13944f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
13945{
13946 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013947 char_u *arg;
13948
13949 if (argvars[0].v_type != VAR_UNKNOWN)
13950 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013951 arg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013952 nr = 0;
13953 if (arg != NULL)
13954 {
13955 if (STRCMP(arg, "$") == 0)
13956 nr = tabpage_index(NULL) - 1;
13957 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013958 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013959 }
13960 }
13961 else
13962 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013963 rettv->vval.v_number = nr;
13964}
13965
13966
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013967/*
13968 * Common code for tabpagewinnr() and winnr().
13969 */
13970 static int
13971get_winnr(tabpage_T *tp, typval_T *argvar)
13972{
13973 win_T *twin;
13974 int nr = 1;
13975 win_T *wp;
13976 char_u *arg;
13977
13978 twin = (tp == curtab) ? curwin : tp->tp_curwin;
13979 if (argvar->v_type != VAR_UNKNOWN)
13980 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020013981 int invalid_arg = FALSE;
13982
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013983 arg = tv_get_string_chk(argvar);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013984 if (arg == NULL)
13985 nr = 0; /* type error; errmsg already given */
13986 else if (STRCMP(arg, "$") == 0)
13987 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
13988 else if (STRCMP(arg, "#") == 0)
13989 {
13990 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
13991 if (twin == NULL)
13992 nr = 0;
13993 }
13994 else
13995 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020013996 long count;
13997 char_u *endp;
13998
13999 // Extract the window count (if specified). e.g. winnr('3j')
14000 count = strtol((char *)arg, (char **)&endp, 10);
14001 if (count <= 0)
14002 count = 1; // if count is not specified, default to 1
14003 if (endp != NULL && *endp != '\0')
14004 {
14005 if (STRCMP(endp, "j") == 0)
14006 twin = win_vert_neighbor(tp, twin, FALSE, count);
14007 else if (STRCMP(endp, "k") == 0)
14008 twin = win_vert_neighbor(tp, twin, TRUE, count);
14009 else if (STRCMP(endp, "h") == 0)
14010 twin = win_horz_neighbor(tp, twin, TRUE, count);
14011 else if (STRCMP(endp, "l") == 0)
14012 twin = win_horz_neighbor(tp, twin, FALSE, count);
14013 else
14014 invalid_arg = TRUE;
14015 }
14016 else
14017 invalid_arg = TRUE;
14018 }
14019
14020 if (invalid_arg)
14021 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014022 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014023 nr = 0;
14024 }
14025 }
14026
14027 if (nr > 0)
14028 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
14029 wp != twin; wp = wp->w_next)
14030 {
14031 if (wp == NULL)
14032 {
14033 /* didn't find it in this tabpage */
14034 nr = 0;
14035 break;
14036 }
14037 ++nr;
14038 }
14039 return nr;
14040}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014041
14042/*
14043 * "tabpagewinnr()" function
14044 */
14045 static void
14046f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
14047{
14048 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014049 tabpage_T *tp;
14050
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014051 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014052 if (tp == NULL)
14053 nr = 0;
14054 else
14055 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014056 rettv->vval.v_number = nr;
14057}
14058
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014059/*
14060 * "tagfiles()" function
14061 */
14062 static void
14063f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
14064{
14065 char_u *fname;
14066 tagname_T tn;
14067 int first;
14068
14069 if (rettv_list_alloc(rettv) == FAIL)
14070 return;
14071 fname = alloc(MAXPATHL);
14072 if (fname == NULL)
14073 return;
14074
14075 for (first = TRUE; ; first = FALSE)
14076 if (get_tagfname(&tn, first, fname) == FAIL
14077 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
14078 break;
14079 tagname_free(&tn);
14080 vim_free(fname);
14081}
14082
14083/*
14084 * "taglist()" function
14085 */
14086 static void
14087f_taglist(typval_T *argvars, typval_T *rettv)
14088{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014089 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014090 char_u *tag_pattern;
14091
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014092 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014093
14094 rettv->vval.v_number = FALSE;
14095 if (*tag_pattern == NUL)
14096 return;
14097
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014098 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014099 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014100 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014101 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014102}
14103
14104/*
14105 * "tempname()" function
14106 */
14107 static void
14108f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
14109{
14110 static int x = 'A';
14111
14112 rettv->v_type = VAR_STRING;
14113 rettv->vval.v_string = vim_tempname(x, FALSE);
14114
14115 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
14116 * names. Skip 'I' and 'O', they are used for shell redirection. */
14117 do
14118 {
14119 if (x == 'Z')
14120 x = '0';
14121 else if (x == '9')
14122 x = 'A';
14123 else
14124 {
14125#ifdef EBCDIC
14126 if (x == 'I')
14127 x = 'J';
14128 else if (x == 'R')
14129 x = 'S';
14130 else
14131#endif
14132 ++x;
14133 }
14134 } while (x == 'I' || x == 'O');
14135}
14136
14137#ifdef FEAT_FLOAT
14138/*
14139 * "tan()" function
14140 */
14141 static void
14142f_tan(typval_T *argvars, typval_T *rettv)
14143{
14144 float_T f = 0.0;
14145
14146 rettv->v_type = VAR_FLOAT;
14147 if (get_float_arg(argvars, &f) == OK)
14148 rettv->vval.v_float = tan(f);
14149 else
14150 rettv->vval.v_float = 0.0;
14151}
14152
14153/*
14154 * "tanh()" function
14155 */
14156 static void
14157f_tanh(typval_T *argvars, typval_T *rettv)
14158{
14159 float_T f = 0.0;
14160
14161 rettv->v_type = VAR_FLOAT;
14162 if (get_float_arg(argvars, &f) == OK)
14163 rettv->vval.v_float = tanh(f);
14164 else
14165 rettv->vval.v_float = 0.0;
14166}
14167#endif
14168
14169/*
14170 * "test_alloc_fail(id, countdown, repeat)" function
14171 */
14172 static void
14173f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
14174{
14175 if (argvars[0].v_type != VAR_NUMBER
14176 || argvars[0].vval.v_number <= 0
14177 || argvars[1].v_type != VAR_NUMBER
14178 || argvars[1].vval.v_number < 0
14179 || argvars[2].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014180 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014181 else
14182 {
14183 alloc_fail_id = argvars[0].vval.v_number;
14184 if (alloc_fail_id >= aid_last)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014185 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014186 alloc_fail_countdown = argvars[1].vval.v_number;
14187 alloc_fail_repeat = argvars[2].vval.v_number;
14188 did_outofmem_msg = FALSE;
14189 }
14190}
14191
14192/*
14193 * "test_autochdir()"
14194 */
14195 static void
14196f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14197{
14198#if defined(FEAT_AUTOCHDIR)
14199 test_autochdir = TRUE;
14200#endif
14201}
14202
14203/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020014204 * "test_feedinput()"
14205 */
14206 static void
14207f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
14208{
14209#ifdef USE_INPUT_BUF
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014210 char_u *val = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5e80de32017-09-03 15:48:12 +020014211
14212 if (val != NULL)
14213 {
14214 trash_input_buf();
14215 add_to_input_buf_csi(val, (int)STRLEN(val));
14216 }
14217#endif
14218}
14219
14220/*
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014221 * "test_option_not_set({name})" function
14222 */
14223 static void
14224f_test_option_not_set(typval_T *argvars, typval_T *rettv UNUSED)
14225{
14226 char_u *name = (char_u *)"";
14227
14228 if (argvars[0].v_type != VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014229 emsg(_(e_invarg));
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014230 else
14231 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014232 name = tv_get_string(&argvars[0]);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014233 if (reset_option_was_set(name) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014234 semsg(_(e_invarg2), name);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014235 }
14236}
14237
14238/*
14239 * "test_override({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014240 */
14241 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014242f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014243{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014244 char_u *name = (char_u *)"";
14245 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014246 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014247
14248 if (argvars[0].v_type != VAR_STRING
14249 || (argvars[1].v_type) != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014250 emsg(_(e_invarg));
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014251 else
14252 {
Bram Moolenaare38197d2018-12-24 23:35:13 +010014253 name = tv_get_string(&argvars[0]);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014254 val = (int)tv_get_number(&argvars[1]);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014255
14256 if (STRCMP(name, (char_u *)"redraw") == 0)
14257 disable_redraw_for_testing = val;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020014258 else if (STRCMP(name, (char_u *)"redraw_flag") == 0)
14259 ignore_redraw_flag_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014260 else if (STRCMP(name, (char_u *)"char_avail") == 0)
14261 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014262 else if (STRCMP(name, (char_u *)"starting") == 0)
14263 {
14264 if (val)
14265 {
14266 if (save_starting < 0)
14267 save_starting = starting;
14268 starting = 0;
14269 }
14270 else
14271 {
14272 starting = save_starting;
14273 save_starting = -1;
14274 }
14275 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +020014276 else if (STRCMP(name, (char_u *)"nfa_fail") == 0)
14277 nfa_fail_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014278 else if (STRCMP(name, (char_u *)"ALL") == 0)
14279 {
14280 disable_char_avail_for_testing = FALSE;
14281 disable_redraw_for_testing = FALSE;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020014282 ignore_redraw_flag_for_testing = FALSE;
Bram Moolenaarbcf94422018-06-23 14:21:42 +020014283 nfa_fail_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014284 if (save_starting >= 0)
14285 {
14286 starting = save_starting;
14287 save_starting = -1;
14288 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014289 }
14290 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014291 semsg(_(e_invarg2), name);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014292 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014293}
14294
14295/*
Bram Moolenaarc3e92c12019-03-23 14:23:07 +010014296 * "test_refcount({expr})" function
14297 */
14298 static void
14299f_test_refcount(typval_T *argvars, typval_T *rettv)
14300{
14301 int retval = -1;
14302
14303 switch (argvars[0].v_type)
14304 {
14305 case VAR_UNKNOWN:
14306 case VAR_NUMBER:
14307 case VAR_FLOAT:
14308 case VAR_SPECIAL:
14309 case VAR_STRING:
14310 break;
14311 case VAR_JOB:
14312#ifdef FEAT_JOB_CHANNEL
14313 if (argvars[0].vval.v_job != NULL)
14314 retval = argvars[0].vval.v_job->jv_refcount - 1;
14315#endif
14316 break;
14317 case VAR_CHANNEL:
14318#ifdef FEAT_JOB_CHANNEL
14319 if (argvars[0].vval.v_channel != NULL)
14320 retval = argvars[0].vval.v_channel->ch_refcount - 1;
14321#endif
14322 break;
14323 case VAR_FUNC:
14324 if (argvars[0].vval.v_string != NULL)
14325 {
14326 ufunc_T *fp;
14327
14328 fp = find_func(argvars[0].vval.v_string);
14329 if (fp != NULL)
14330 retval = fp->uf_refcount;
14331 }
14332 break;
14333 case VAR_PARTIAL:
14334 if (argvars[0].vval.v_partial != NULL)
14335 retval = argvars[0].vval.v_partial->pt_refcount - 1;
14336 break;
14337 case VAR_BLOB:
14338 if (argvars[0].vval.v_blob != NULL)
14339 retval = argvars[0].vval.v_blob->bv_refcount - 1;
14340 break;
14341 case VAR_LIST:
14342 if (argvars[0].vval.v_list != NULL)
14343 retval = argvars[0].vval.v_list->lv_refcount - 1;
14344 break;
14345 case VAR_DICT:
14346 if (argvars[0].vval.v_dict != NULL)
14347 retval = argvars[0].vval.v_dict->dv_refcount - 1;
14348 break;
14349 }
14350
14351 rettv->v_type = VAR_NUMBER;
14352 rettv->vval.v_number = retval;
14353
14354}
14355
14356/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014357 * "test_garbagecollect_now()" function
14358 */
14359 static void
14360f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14361{
14362 /* This is dangerous, any Lists and Dicts used internally may be freed
14363 * while still in use. */
14364 garbage_collect(TRUE);
14365}
14366
Bram Moolenaare0c31f62017-03-01 15:07:05 +010014367/*
14368 * "test_ignore_error()" function
14369 */
14370 static void
14371f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
14372{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014373 ignore_error_for_testing(tv_get_string(&argvars[0]));
Bram Moolenaare0c31f62017-03-01 15:07:05 +010014374}
14375
Bram Moolenaarc0f5a782019-01-13 15:16:13 +010014376 static void
14377f_test_null_blob(typval_T *argvars UNUSED, typval_T *rettv)
14378{
14379 rettv->v_type = VAR_BLOB;
14380 rettv->vval.v_blob = NULL;
14381}
14382
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014383#ifdef FEAT_JOB_CHANNEL
14384 static void
14385f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
14386{
14387 rettv->v_type = VAR_CHANNEL;
14388 rettv->vval.v_channel = NULL;
14389}
14390#endif
14391
14392 static void
14393f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
14394{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014395 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014396}
14397
14398#ifdef FEAT_JOB_CHANNEL
14399 static void
14400f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
14401{
14402 rettv->v_type = VAR_JOB;
14403 rettv->vval.v_job = NULL;
14404}
14405#endif
14406
14407 static void
14408f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
14409{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014410 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014411}
14412
14413 static void
14414f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
14415{
14416 rettv->v_type = VAR_PARTIAL;
14417 rettv->vval.v_partial = NULL;
14418}
14419
14420 static void
14421f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
14422{
14423 rettv->v_type = VAR_STRING;
14424 rettv->vval.v_string = NULL;
14425}
14426
Bram Moolenaarab186732018-09-14 21:27:06 +020014427#ifdef FEAT_GUI
14428 static void
14429f_test_scrollbar(typval_T *argvars, typval_T *rettv UNUSED)
14430{
14431 char_u *which;
14432 long value;
14433 int dragging;
14434 scrollbar_T *sb = NULL;
14435
14436 if (argvars[0].v_type != VAR_STRING
14437 || (argvars[1].v_type) != VAR_NUMBER
14438 || (argvars[2].v_type) != VAR_NUMBER)
14439 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014440 emsg(_(e_invarg));
Bram Moolenaarab186732018-09-14 21:27:06 +020014441 return;
14442 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014443 which = tv_get_string(&argvars[0]);
14444 value = tv_get_number(&argvars[1]);
14445 dragging = tv_get_number(&argvars[2]);
Bram Moolenaarab186732018-09-14 21:27:06 +020014446
14447 if (STRCMP(which, "left") == 0)
14448 sb = &curwin->w_scrollbars[SBAR_LEFT];
14449 else if (STRCMP(which, "right") == 0)
14450 sb = &curwin->w_scrollbars[SBAR_RIGHT];
14451 else if (STRCMP(which, "hor") == 0)
14452 sb = &gui.bottom_sbar;
14453 if (sb == NULL)
14454 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014455 semsg(_(e_invarg2), which);
Bram Moolenaarab186732018-09-14 21:27:06 +020014456 return;
14457 }
14458 gui_drag_scrollbar(sb, value, dragging);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020014459# ifndef USE_ON_FLY_SCROLL
14460 // need to loop through normal_cmd() to handle the scroll events
14461 exec_normal(FALSE, TRUE, FALSE);
14462# endif
Bram Moolenaarab186732018-09-14 21:27:06 +020014463}
14464#endif
14465
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014466 static void
14467f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
14468{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014469 time_for_testing = (time_t)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014470}
14471
14472#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
14473/*
14474 * Get a callback from "arg". It can be a Funcref or a function name.
14475 * When "arg" is zero return an empty string.
14476 * Return NULL for an invalid argument.
14477 */
14478 char_u *
14479get_callback(typval_T *arg, partial_T **pp)
14480{
14481 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
14482 {
14483 *pp = arg->vval.v_partial;
14484 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020014485 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014486 }
14487 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020014488 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014489 {
14490 func_ref(arg->vval.v_string);
14491 return arg->vval.v_string;
14492 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014493 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
14494 return (char_u *)"";
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014495 emsg(_("E921: Invalid callback argument"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014496 return NULL;
14497}
14498
14499/*
Bram Moolenaard5359b22018-04-05 22:44:39 +020014500 * Unref/free "callback" and "partial" returned by get_callback().
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014501 */
14502 void
14503free_callback(char_u *callback, partial_T *partial)
14504{
14505 if (partial != NULL)
14506 partial_unref(partial);
14507 else if (callback != NULL)
14508 {
14509 func_unref(callback);
14510 vim_free(callback);
14511 }
14512}
14513#endif
14514
14515#ifdef FEAT_TIMERS
14516/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014517 * "timer_info([timer])" function
14518 */
14519 static void
14520f_timer_info(typval_T *argvars, typval_T *rettv)
14521{
14522 timer_T *timer = NULL;
14523
14524 if (rettv_list_alloc(rettv) != OK)
14525 return;
14526 if (argvars[0].v_type != VAR_UNKNOWN)
14527 {
14528 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014529 emsg(_(e_number_exp));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014530 else
14531 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014532 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014533 if (timer != NULL)
14534 add_timer_info(rettv, timer);
14535 }
14536 }
14537 else
14538 add_timer_info_all(rettv);
14539}
14540
14541/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014542 * "timer_pause(timer, paused)" function
14543 */
14544 static void
14545f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
14546{
14547 timer_T *timer = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014548 int paused = (int)tv_get_number(&argvars[1]);
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014549
14550 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014551 emsg(_(e_number_exp));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014552 else
14553 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014554 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014555 if (timer != NULL)
14556 timer->tr_paused = paused;
14557 }
14558}
14559
14560/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014561 * "timer_start(time, callback [, options])" function
14562 */
14563 static void
14564f_timer_start(typval_T *argvars, typval_T *rettv)
14565{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014566 long msec = (long)tv_get_number(&argvars[0]);
Bram Moolenaar75537a92016-09-05 22:45:28 +020014567 timer_T *timer;
14568 int repeat = 0;
14569 char_u *callback;
14570 dict_T *dict;
14571 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014572
Bram Moolenaar75537a92016-09-05 22:45:28 +020014573 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014574 if (check_secure())
14575 return;
14576 if (argvars[2].v_type != VAR_UNKNOWN)
14577 {
14578 if (argvars[2].v_type != VAR_DICT
14579 || (dict = argvars[2].vval.v_dict) == NULL)
14580 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014581 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014582 return;
14583 }
14584 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014585 repeat = dict_get_number(dict, (char_u *)"repeat");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014586 }
14587
Bram Moolenaar75537a92016-09-05 22:45:28 +020014588 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014589 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020014590 return;
14591
14592 timer = create_timer(msec, repeat);
14593 if (timer == NULL)
14594 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014595 else
14596 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020014597 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020014598 timer->tr_callback = vim_strsave(callback);
14599 else
14600 /* pointer into the partial */
14601 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020014602 timer->tr_partial = partial;
14603 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014604 }
14605}
14606
14607/*
14608 * "timer_stop(timer)" function
14609 */
14610 static void
14611f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
14612{
14613 timer_T *timer;
14614
14615 if (argvars[0].v_type != VAR_NUMBER)
14616 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014617 emsg(_(e_number_exp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014618 return;
14619 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014620 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014621 if (timer != NULL)
14622 stop_timer(timer);
14623}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014624
14625/*
14626 * "timer_stopall()" function
14627 */
14628 static void
14629f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14630{
14631 stop_all_timers();
14632}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014633#endif
14634
14635/*
14636 * "tolower(string)" function
14637 */
14638 static void
14639f_tolower(typval_T *argvars, typval_T *rettv)
14640{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014641 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014642 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014643}
14644
14645/*
14646 * "toupper(string)" function
14647 */
14648 static void
14649f_toupper(typval_T *argvars, typval_T *rettv)
14650{
14651 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014652 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014653}
14654
14655/*
14656 * "tr(string, fromstr, tostr)" function
14657 */
14658 static void
14659f_tr(typval_T *argvars, typval_T *rettv)
14660{
14661 char_u *in_str;
14662 char_u *fromstr;
14663 char_u *tostr;
14664 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014665 int inlen;
14666 int fromlen;
14667 int tolen;
14668 int idx;
14669 char_u *cpstr;
14670 int cplen;
14671 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014672 char_u buf[NUMBUFLEN];
14673 char_u buf2[NUMBUFLEN];
14674 garray_T ga;
14675
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014676 in_str = tv_get_string(&argvars[0]);
14677 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
14678 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014679
14680 /* Default return value: empty string. */
14681 rettv->v_type = VAR_STRING;
14682 rettv->vval.v_string = NULL;
14683 if (fromstr == NULL || tostr == NULL)
14684 return; /* type error; errmsg already given */
14685 ga_init2(&ga, (int)sizeof(char), 80);
14686
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014687 if (!has_mbyte)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014688 /* not multi-byte: fromstr and tostr must be the same length */
14689 if (STRLEN(fromstr) != STRLEN(tostr))
14690 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014691error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014692 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014693 ga_clear(&ga);
14694 return;
14695 }
14696
14697 /* fromstr and tostr have to contain the same number of chars */
14698 while (*in_str != NUL)
14699 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014700 if (has_mbyte)
14701 {
14702 inlen = (*mb_ptr2len)(in_str);
14703 cpstr = in_str;
14704 cplen = inlen;
14705 idx = 0;
14706 for (p = fromstr; *p != NUL; p += fromlen)
14707 {
14708 fromlen = (*mb_ptr2len)(p);
14709 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
14710 {
14711 for (p = tostr; *p != NUL; p += tolen)
14712 {
14713 tolen = (*mb_ptr2len)(p);
14714 if (idx-- == 0)
14715 {
14716 cplen = tolen;
14717 cpstr = p;
14718 break;
14719 }
14720 }
14721 if (*p == NUL) /* tostr is shorter than fromstr */
14722 goto error;
14723 break;
14724 }
14725 ++idx;
14726 }
14727
14728 if (first && cpstr == in_str)
14729 {
14730 /* Check that fromstr and tostr have the same number of
14731 * (multi-byte) characters. Done only once when a character
14732 * of in_str doesn't appear in fromstr. */
14733 first = FALSE;
14734 for (p = tostr; *p != NUL; p += tolen)
14735 {
14736 tolen = (*mb_ptr2len)(p);
14737 --idx;
14738 }
14739 if (idx != 0)
14740 goto error;
14741 }
14742
14743 (void)ga_grow(&ga, cplen);
14744 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
14745 ga.ga_len += cplen;
14746
14747 in_str += inlen;
14748 }
14749 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014750 {
14751 /* When not using multi-byte chars we can do it faster. */
14752 p = vim_strchr(fromstr, *in_str);
14753 if (p != NULL)
14754 ga_append(&ga, tostr[p - fromstr]);
14755 else
14756 ga_append(&ga, *in_str);
14757 ++in_str;
14758 }
14759 }
14760
14761 /* add a terminating NUL */
14762 (void)ga_grow(&ga, 1);
14763 ga_append(&ga, NUL);
14764
14765 rettv->vval.v_string = ga.ga_data;
14766}
14767
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014768/*
14769 * "trim({expr})" function
14770 */
14771 static void
14772f_trim(typval_T *argvars, typval_T *rettv)
14773{
14774 char_u buf1[NUMBUFLEN];
14775 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014776 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014777 char_u *mask = NULL;
14778 char_u *tail;
14779 char_u *prev;
14780 char_u *p;
14781 int c1;
14782
14783 rettv->v_type = VAR_STRING;
14784 if (head == NULL)
14785 {
14786 rettv->vval.v_string = NULL;
14787 return;
14788 }
14789
14790 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014791 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014792
14793 while (*head != NUL)
14794 {
14795 c1 = PTR2CHAR(head);
14796 if (mask == NULL)
14797 {
14798 if (c1 > ' ' && c1 != 0xa0)
14799 break;
14800 }
14801 else
14802 {
14803 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14804 if (c1 == PTR2CHAR(p))
14805 break;
14806 if (*p == NUL)
14807 break;
14808 }
14809 MB_PTR_ADV(head);
14810 }
14811
14812 for (tail = head + STRLEN(head); tail > head; tail = prev)
14813 {
14814 prev = tail;
14815 MB_PTR_BACK(head, prev);
14816 c1 = PTR2CHAR(prev);
14817 if (mask == NULL)
14818 {
14819 if (c1 > ' ' && c1 != 0xa0)
14820 break;
14821 }
14822 else
14823 {
14824 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14825 if (c1 == PTR2CHAR(p))
14826 break;
14827 if (*p == NUL)
14828 break;
14829 }
14830 }
14831 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
14832}
14833
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014834#ifdef FEAT_FLOAT
14835/*
14836 * "trunc({float})" function
14837 */
14838 static void
14839f_trunc(typval_T *argvars, typval_T *rettv)
14840{
14841 float_T f = 0.0;
14842
14843 rettv->v_type = VAR_FLOAT;
14844 if (get_float_arg(argvars, &f) == OK)
14845 /* trunc() is not in C90, use floor() or ceil() instead. */
14846 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
14847 else
14848 rettv->vval.v_float = 0.0;
14849}
14850#endif
14851
14852/*
14853 * "type(expr)" function
14854 */
14855 static void
14856f_type(typval_T *argvars, typval_T *rettv)
14857{
14858 int n = -1;
14859
14860 switch (argvars[0].v_type)
14861 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020014862 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
14863 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014864 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020014865 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
14866 case VAR_LIST: n = VAR_TYPE_LIST; break;
14867 case VAR_DICT: n = VAR_TYPE_DICT; break;
14868 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014869 case VAR_SPECIAL:
14870 if (argvars[0].vval.v_number == VVAL_FALSE
14871 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020014872 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014873 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020014874 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014875 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020014876 case VAR_JOB: n = VAR_TYPE_JOB; break;
14877 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014878 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014879 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010014880 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014881 n = -1;
14882 break;
14883 }
14884 rettv->vval.v_number = n;
14885}
14886
14887/*
14888 * "undofile(name)" function
14889 */
14890 static void
14891f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
14892{
14893 rettv->v_type = VAR_STRING;
14894#ifdef FEAT_PERSISTENT_UNDO
14895 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014896 char_u *fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014897
14898 if (*fname == NUL)
14899 {
14900 /* If there is no file name there will be no undo file. */
14901 rettv->vval.v_string = NULL;
14902 }
14903 else
14904 {
14905 char_u *ffname = FullName_save(fname, FALSE);
14906
14907 if (ffname != NULL)
14908 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
14909 vim_free(ffname);
14910 }
14911 }
14912#else
14913 rettv->vval.v_string = NULL;
14914#endif
14915}
14916
14917/*
14918 * "undotree()" function
14919 */
14920 static void
14921f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
14922{
14923 if (rettv_dict_alloc(rettv) == OK)
14924 {
14925 dict_T *dict = rettv->vval.v_dict;
14926 list_T *list;
14927
Bram Moolenaare0be1672018-07-08 16:50:37 +020014928 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
14929 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
14930 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
14931 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
14932 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
14933 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014934
14935 list = list_alloc();
14936 if (list != NULL)
14937 {
14938 u_eval_tree(curbuf->b_u_oldhead, list);
14939 dict_add_list(dict, "entries", list);
14940 }
14941 }
14942}
14943
14944/*
14945 * "values(dict)" function
14946 */
14947 static void
14948f_values(typval_T *argvars, typval_T *rettv)
14949{
14950 dict_list(argvars, rettv, 1);
14951}
14952
14953/*
14954 * "virtcol(string)" function
14955 */
14956 static void
14957f_virtcol(typval_T *argvars, typval_T *rettv)
14958{
14959 colnr_T vcol = 0;
14960 pos_T *fp;
14961 int fnum = curbuf->b_fnum;
14962
14963 fp = var2fpos(&argvars[0], FALSE, &fnum);
14964 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
14965 && fnum == curbuf->b_fnum)
14966 {
14967 getvvcol(curwin, fp, NULL, NULL, &vcol);
14968 ++vcol;
14969 }
14970
14971 rettv->vval.v_number = vcol;
14972}
14973
14974/*
14975 * "visualmode()" function
14976 */
14977 static void
14978f_visualmode(typval_T *argvars, typval_T *rettv)
14979{
14980 char_u str[2];
14981
14982 rettv->v_type = VAR_STRING;
14983 str[0] = curbuf->b_visual_mode_eval;
14984 str[1] = NUL;
14985 rettv->vval.v_string = vim_strsave(str);
14986
14987 /* A non-zero number or non-empty string argument: reset mode. */
14988 if (non_zero_arg(&argvars[0]))
14989 curbuf->b_visual_mode_eval = NUL;
14990}
14991
14992/*
14993 * "wildmenumode()" function
14994 */
14995 static void
14996f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14997{
14998#ifdef FEAT_WILDMENU
14999 if (wild_menu_showing)
15000 rettv->vval.v_number = 1;
15001#endif
15002}
15003
15004/*
15005 * "winbufnr(nr)" function
15006 */
15007 static void
15008f_winbufnr(typval_T *argvars, typval_T *rettv)
15009{
15010 win_T *wp;
15011
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015012 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015013 if (wp == NULL)
15014 rettv->vval.v_number = -1;
15015 else
15016 rettv->vval.v_number = wp->w_buffer->b_fnum;
15017}
15018
15019/*
15020 * "wincol()" function
15021 */
15022 static void
15023f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
15024{
15025 validate_cursor();
15026 rettv->vval.v_number = curwin->w_wcol + 1;
15027}
15028
15029/*
15030 * "winheight(nr)" function
15031 */
15032 static void
15033f_winheight(typval_T *argvars, typval_T *rettv)
15034{
15035 win_T *wp;
15036
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015037 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015038 if (wp == NULL)
15039 rettv->vval.v_number = -1;
15040 else
15041 rettv->vval.v_number = wp->w_height;
15042}
15043
15044/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020015045 * "winlayout()" function
15046 */
15047 static void
15048f_winlayout(typval_T *argvars, typval_T *rettv)
15049{
15050 tabpage_T *tp;
15051
15052 if (rettv_list_alloc(rettv) != OK)
15053 return;
15054
15055 if (argvars[0].v_type == VAR_UNKNOWN)
15056 tp = curtab;
15057 else
15058 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015059 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020015060 if (tp == NULL)
15061 return;
15062 }
15063
15064 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
15065}
15066
15067/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015068 * "winline()" function
15069 */
15070 static void
15071f_winline(typval_T *argvars UNUSED, typval_T *rettv)
15072{
15073 validate_cursor();
15074 rettv->vval.v_number = curwin->w_wrow + 1;
15075}
15076
15077/*
15078 * "winnr()" function
15079 */
15080 static void
15081f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
15082{
15083 int nr = 1;
15084
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015085 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015086 rettv->vval.v_number = nr;
15087}
15088
15089/*
15090 * "winrestcmd()" function
15091 */
15092 static void
15093f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
15094{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015095 win_T *wp;
15096 int winnr = 1;
15097 garray_T ga;
15098 char_u buf[50];
15099
15100 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020015101 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015102 {
15103 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
15104 ga_concat(&ga, buf);
15105 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
15106 ga_concat(&ga, buf);
15107 ++winnr;
15108 }
15109 ga_append(&ga, NUL);
15110
15111 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015112 rettv->v_type = VAR_STRING;
15113}
15114
15115/*
15116 * "winrestview()" function
15117 */
15118 static void
15119f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
15120{
15121 dict_T *dict;
15122
15123 if (argvars[0].v_type != VAR_DICT
15124 || (dict = argvars[0].vval.v_dict) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015125 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015126 else
15127 {
15128 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015129 curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015130 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015131 curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015132 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015133 curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015134 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
15135 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010015136 curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015137 curwin->w_set_curswant = FALSE;
15138 }
15139
15140 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015141 set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015142#ifdef FEAT_DIFF
15143 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015144 curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015145#endif
15146 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015147 curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015148 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015149 curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015150
15151 check_cursor();
15152 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020015153 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015154 changed_window_setting();
15155
15156 if (curwin->w_topline <= 0)
15157 curwin->w_topline = 1;
15158 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
15159 curwin->w_topline = curbuf->b_ml.ml_line_count;
15160#ifdef FEAT_DIFF
15161 check_topfill(curwin, TRUE);
15162#endif
15163 }
15164}
15165
15166/*
15167 * "winsaveview()" function
15168 */
15169 static void
15170f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
15171{
15172 dict_T *dict;
15173
15174 if (rettv_dict_alloc(rettv) == FAIL)
15175 return;
15176 dict = rettv->vval.v_dict;
15177
Bram Moolenaare0be1672018-07-08 16:50:37 +020015178 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
15179 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +020015180 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015181 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020015182 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015183
Bram Moolenaare0be1672018-07-08 16:50:37 +020015184 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015185#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020015186 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015187#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020015188 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
15189 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015190}
15191
15192/*
15193 * "winwidth(nr)" function
15194 */
15195 static void
15196f_winwidth(typval_T *argvars, typval_T *rettv)
15197{
15198 win_T *wp;
15199
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015200 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015201 if (wp == NULL)
15202 rettv->vval.v_number = -1;
15203 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015204 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015205}
15206
15207/*
15208 * "wordcount()" function
15209 */
15210 static void
15211f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
15212{
15213 if (rettv_dict_alloc(rettv) == FAIL)
15214 return;
15215 cursor_pos_info(rettv->vval.v_dict);
15216}
15217
15218/*
15219 * "writefile()" function
15220 */
15221 static void
15222f_writefile(typval_T *argvars, typval_T *rettv)
15223{
15224 int binary = FALSE;
15225 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015226#ifdef HAVE_FSYNC
15227 int do_fsync = p_fs;
15228#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015229 char_u *fname;
15230 FILE *fd;
15231 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015232 listitem_T *li;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015233 list_T *list = NULL;
15234 blob_T *blob = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015235
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015236 rettv->vval.v_number = -1;
Bram Moolenaar8c62a082019-02-08 14:34:10 +010015237 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015238 return;
15239
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015240 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015241 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015242 list = argvars[0].vval.v_list;
15243 if (list == NULL)
15244 return;
15245 for (li = list->lv_first; li != NULL; li = li->li_next)
15246 if (tv_get_string_chk(&li->li_tv) == NULL)
15247 return;
15248 }
15249 else if (argvars[0].v_type == VAR_BLOB)
15250 {
15251 blob = argvars[0].vval.v_blob;
15252 if (blob == NULL)
15253 return;
15254 }
15255 else
15256 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015257 semsg(_(e_invarg2), "writefile()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015258 return;
15259 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015260
15261 if (argvars[2].v_type != VAR_UNKNOWN)
15262 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015263 char_u *arg2 = tv_get_string_chk(&argvars[2]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015264
15265 if (arg2 == NULL)
15266 return;
15267 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015268 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015269 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015270 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015271#ifdef HAVE_FSYNC
15272 if (vim_strchr(arg2, 's') != NULL)
15273 do_fsync = TRUE;
15274 else if (vim_strchr(arg2, 'S') != NULL)
15275 do_fsync = FALSE;
15276#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015277 }
15278
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015279 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015280 if (fname == NULL)
15281 return;
15282
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015283 /* Always open the file in binary mode, library functions have a mind of
15284 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015285 if (*fname == NUL || (fd = mch_fopen((char *)fname,
15286 append ? APPENDBIN : WRITEBIN)) == NULL)
15287 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015288 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015289 ret = -1;
15290 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015291 else if (blob)
15292 {
15293 if (write_blob(fd, blob) == FAIL)
15294 ret = -1;
15295#ifdef HAVE_FSYNC
15296 else if (do_fsync)
15297 // Ignore the error, the user wouldn't know what to do about it.
15298 // May happen for a device.
Bram Moolenaara7870192019-02-14 12:56:36 +010015299 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015300#endif
15301 fclose(fd);
15302 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015303 else
15304 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015305 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015306 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015307#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010015308 else if (do_fsync)
15309 /* Ignore the error, the user wouldn't know what to do about it.
15310 * May happen for a device. */
Bram Moolenaara7870192019-02-14 12:56:36 +010015311 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015312#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015313 fclose(fd);
15314 }
15315
15316 rettv->vval.v_number = ret;
15317}
15318
15319/*
15320 * "xor(expr, expr)" function
15321 */
15322 static void
15323f_xor(typval_T *argvars, typval_T *rettv)
15324{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015325 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
15326 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015327}
15328
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015329#endif /* FEAT_EVAL */