blob: ba3db95c9fd6d54748b95412347068b2fec69edc [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 Moolenaar7e1a5af2019-05-07 16:28:13 +0200459#ifdef FEAT_MOUSE
Bram Moolenaarbb8476b2019-05-04 15:47:48 +0200460static void f_test_setmouse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +0200461#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200462static void f_test_settime(typval_T *argvars, typval_T *rettv);
463#ifdef FEAT_FLOAT
464static void f_tan(typval_T *argvars, typval_T *rettv);
465static void f_tanh(typval_T *argvars, typval_T *rettv);
466#endif
467#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200468static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200469static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200470static void f_timer_start(typval_T *argvars, typval_T *rettv);
471static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200472static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200473#endif
474static void f_tolower(typval_T *argvars, typval_T *rettv);
475static void f_toupper(typval_T *argvars, typval_T *rettv);
476static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100477static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200478#ifdef FEAT_FLOAT
479static void f_trunc(typval_T *argvars, typval_T *rettv);
480#endif
481static void f_type(typval_T *argvars, typval_T *rettv);
482static void f_undofile(typval_T *argvars, typval_T *rettv);
483static void f_undotree(typval_T *argvars, typval_T *rettv);
484static void f_uniq(typval_T *argvars, typval_T *rettv);
485static void f_values(typval_T *argvars, typval_T *rettv);
486static void f_virtcol(typval_T *argvars, typval_T *rettv);
487static void f_visualmode(typval_T *argvars, typval_T *rettv);
488static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
489static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
490static void f_win_getid(typval_T *argvars, typval_T *rettv);
491static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
492static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
493static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100494static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200495static void f_winbufnr(typval_T *argvars, typval_T *rettv);
496static void f_wincol(typval_T *argvars, typval_T *rettv);
497static void f_winheight(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200498static void f_winlayout(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200499static void f_winline(typval_T *argvars, typval_T *rettv);
500static void f_winnr(typval_T *argvars, typval_T *rettv);
501static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
502static void f_winrestview(typval_T *argvars, typval_T *rettv);
503static void f_winsaveview(typval_T *argvars, typval_T *rettv);
504static void f_winwidth(typval_T *argvars, typval_T *rettv);
505static void f_writefile(typval_T *argvars, typval_T *rettv);
506static void f_wordcount(typval_T *argvars, typval_T *rettv);
507static void f_xor(typval_T *argvars, typval_T *rettv);
508
509/*
510 * Array with names and number of arguments of all internal functions
511 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
512 */
513static struct fst
514{
515 char *f_name; /* function name */
516 char f_min_argc; /* minimal number of arguments */
517 char f_max_argc; /* maximal number of arguments */
518 void (*f_func)(typval_T *args, typval_T *rvar);
519 /* implementation of function */
520} functions[] =
521{
522#ifdef FEAT_FLOAT
523 {"abs", 1, 1, f_abs},
524 {"acos", 1, 1, f_acos}, /* WJMc */
525#endif
526 {"add", 2, 2, f_add},
527 {"and", 2, 2, f_and},
528 {"append", 2, 2, f_append},
Bram Moolenaarca851592018-06-06 21:04:07 +0200529 {"appendbufline", 3, 3, f_appendbufline},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200530 {"argc", 0, 1, f_argc},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200531 {"argidx", 0, 0, f_argidx},
532 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200533 {"argv", 0, 2, f_argv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200534#ifdef FEAT_FLOAT
535 {"asin", 1, 1, f_asin}, /* WJMc */
536#endif
Bram Moolenaarb48e96f2018-02-13 12:26:14 +0100537 {"assert_beeps", 1, 2, f_assert_beeps},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200538 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaard96ff162018-02-18 22:13:29 +0100539 {"assert_equalfile", 2, 2, f_assert_equalfile},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200540 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaar1307d1c2018-10-07 20:16:49 +0200541 {"assert_fails", 1, 3, f_assert_fails},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200542 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100543 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200544 {"assert_match", 2, 3, f_assert_match},
545 {"assert_notequal", 2, 3, f_assert_notequal},
546 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100547 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200548 {"assert_true", 1, 2, f_assert_true},
549#ifdef FEAT_FLOAT
550 {"atan", 1, 1, f_atan},
551 {"atan2", 2, 2, f_atan2},
552#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100553#ifdef FEAT_BEVAL
554 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100555# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100556 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100557# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100558#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200559 {"browse", 4, 4, f_browse},
560 {"browsedir", 2, 2, f_browsedir},
561 {"bufexists", 1, 1, f_bufexists},
562 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
563 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
564 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
565 {"buflisted", 1, 1, f_buflisted},
566 {"bufloaded", 1, 1, f_bufloaded},
567 {"bufname", 1, 1, f_bufname},
568 {"bufnr", 1, 2, f_bufnr},
569 {"bufwinid", 1, 1, f_bufwinid},
570 {"bufwinnr", 1, 1, f_bufwinnr},
571 {"byte2line", 1, 1, f_byte2line},
572 {"byteidx", 2, 2, f_byteidx},
573 {"byteidxcomp", 2, 2, f_byteidxcomp},
574 {"call", 2, 3, f_call},
575#ifdef FEAT_FLOAT
576 {"ceil", 1, 1, f_ceil},
577#endif
578#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100579 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200580 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200581 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200582 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
583 {"ch_evalraw", 2, 3, f_ch_evalraw},
584 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
585 {"ch_getjob", 1, 1, f_ch_getjob},
586 {"ch_info", 1, 1, f_ch_info},
587 {"ch_log", 1, 2, f_ch_log},
588 {"ch_logfile", 1, 2, f_ch_logfile},
589 {"ch_open", 1, 2, f_ch_open},
590 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100591 {"ch_readblob", 1, 2, f_ch_readblob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200592 {"ch_readraw", 1, 2, f_ch_readraw},
593 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
594 {"ch_sendraw", 2, 3, f_ch_sendraw},
595 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200596 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200597#endif
598 {"changenr", 0, 0, f_changenr},
599 {"char2nr", 1, 2, f_char2nr},
600 {"cindent", 1, 1, f_cindent},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100601 {"clearmatches", 0, 1, f_clearmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200602 {"col", 1, 1, f_col},
603#if defined(FEAT_INS_EXPAND)
604 {"complete", 2, 2, f_complete},
605 {"complete_add", 1, 1, f_complete_add},
606 {"complete_check", 0, 0, f_complete_check},
Bram Moolenaarfd133322019-03-29 12:20:27 +0100607 {"complete_info", 0, 1, f_complete_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200608#endif
609 {"confirm", 1, 4, f_confirm},
610 {"copy", 1, 1, f_copy},
611#ifdef FEAT_FLOAT
612 {"cos", 1, 1, f_cos},
613 {"cosh", 1, 1, f_cosh},
614#endif
615 {"count", 2, 4, f_count},
616 {"cscope_connection",0,3, f_cscope_connection},
617 {"cursor", 1, 3, f_cursor},
Bram Moolenaar4f974752019-02-17 17:44:42 +0100618#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200619 {"debugbreak", 1, 1, f_debugbreak},
620#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200621 {"deepcopy", 1, 2, f_deepcopy},
622 {"delete", 1, 2, f_delete},
Bram Moolenaard79a2622018-06-07 18:17:46 +0200623 {"deletebufline", 2, 3, f_deletebufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200624 {"did_filetype", 0, 0, f_did_filetype},
625 {"diff_filler", 1, 1, f_diff_filler},
626 {"diff_hlID", 2, 2, f_diff_hlID},
627 {"empty", 1, 1, f_empty},
628 {"escape", 2, 2, f_escape},
629 {"eval", 1, 1, f_eval},
630 {"eventhandler", 0, 0, f_eventhandler},
631 {"executable", 1, 1, f_executable},
632 {"execute", 1, 2, f_execute},
633 {"exepath", 1, 1, f_exepath},
634 {"exists", 1, 1, f_exists},
635#ifdef FEAT_FLOAT
636 {"exp", 1, 1, f_exp},
637#endif
638 {"expand", 1, 3, f_expand},
639 {"extend", 2, 3, f_extend},
640 {"feedkeys", 1, 2, f_feedkeys},
641 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
642 {"filereadable", 1, 1, f_filereadable},
643 {"filewritable", 1, 1, f_filewritable},
644 {"filter", 2, 2, f_filter},
645 {"finddir", 1, 3, f_finddir},
646 {"findfile", 1, 3, f_findfile},
647#ifdef FEAT_FLOAT
648 {"float2nr", 1, 1, f_float2nr},
649 {"floor", 1, 1, f_floor},
650 {"fmod", 2, 2, f_fmod},
651#endif
652 {"fnameescape", 1, 1, f_fnameescape},
653 {"fnamemodify", 2, 2, f_fnamemodify},
654 {"foldclosed", 1, 1, f_foldclosed},
655 {"foldclosedend", 1, 1, f_foldclosedend},
656 {"foldlevel", 1, 1, f_foldlevel},
657 {"foldtext", 0, 0, f_foldtext},
658 {"foldtextresult", 1, 1, f_foldtextresult},
659 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200660 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200661 {"function", 1, 3, f_function},
662 {"garbagecollect", 0, 1, f_garbagecollect},
663 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200664 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200665 {"getbufline", 2, 3, f_getbufline},
666 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100667 {"getchangelist", 1, 1, f_getchangelist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200668 {"getchar", 0, 1, f_getchar},
669 {"getcharmod", 0, 0, f_getcharmod},
670 {"getcharsearch", 0, 0, f_getcharsearch},
671 {"getcmdline", 0, 0, f_getcmdline},
672 {"getcmdpos", 0, 0, f_getcmdpos},
673 {"getcmdtype", 0, 0, f_getcmdtype},
674 {"getcmdwintype", 0, 0, f_getcmdwintype},
675#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200676 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200677#endif
678 {"getcurpos", 0, 0, f_getcurpos},
679 {"getcwd", 0, 2, f_getcwd},
680 {"getfontname", 0, 1, f_getfontname},
681 {"getfperm", 1, 1, f_getfperm},
682 {"getfsize", 1, 1, f_getfsize},
683 {"getftime", 1, 1, f_getftime},
684 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100685 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200686 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200687 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100688 {"getmatches", 0, 1, f_getmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200689 {"getpid", 0, 0, f_getpid},
690 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200691 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200692 {"getreg", 0, 3, f_getreg},
693 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200694 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200695 {"gettabvar", 2, 3, f_gettabvar},
696 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100697 {"gettagstack", 0, 1, f_gettagstack},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200698 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100699 {"getwinpos", 0, 1, f_getwinpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200700 {"getwinposx", 0, 0, f_getwinposx},
701 {"getwinposy", 0, 0, f_getwinposy},
702 {"getwinvar", 2, 3, f_getwinvar},
703 {"glob", 1, 4, f_glob},
704 {"glob2regpat", 1, 1, f_glob2regpat},
705 {"globpath", 2, 5, f_globpath},
706 {"has", 1, 1, f_has},
707 {"has_key", 2, 2, f_has_key},
708 {"haslocaldir", 0, 2, f_haslocaldir},
709 {"hasmapto", 1, 3, f_hasmapto},
710 {"highlightID", 1, 1, f_hlID}, /* obsolete */
711 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
712 {"histadd", 2, 2, f_histadd},
713 {"histdel", 1, 2, f_histdel},
714 {"histget", 1, 2, f_histget},
715 {"histnr", 1, 1, f_histnr},
716 {"hlID", 1, 1, f_hlID},
717 {"hlexists", 1, 1, f_hlexists},
718 {"hostname", 0, 0, f_hostname},
719 {"iconv", 3, 3, f_iconv},
720 {"indent", 1, 1, f_indent},
721 {"index", 2, 4, f_index},
722 {"input", 1, 3, f_input},
723 {"inputdialog", 1, 3, f_inputdialog},
724 {"inputlist", 1, 1, f_inputlist},
725 {"inputrestore", 0, 0, f_inputrestore},
726 {"inputsave", 0, 0, f_inputsave},
727 {"inputsecret", 1, 2, f_inputsecret},
728 {"insert", 2, 3, f_insert},
729 {"invert", 1, 1, f_invert},
730 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200731#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
732 {"isinf", 1, 1, f_isinf},
733#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200734 {"islocked", 1, 1, f_islocked},
735#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
736 {"isnan", 1, 1, f_isnan},
737#endif
738 {"items", 1, 1, f_items},
739#ifdef FEAT_JOB_CHANNEL
740 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200741 {"job_info", 0, 1, f_job_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200742 {"job_setoptions", 2, 2, f_job_setoptions},
743 {"job_start", 1, 2, f_job_start},
744 {"job_status", 1, 1, f_job_status},
745 {"job_stop", 1, 2, f_job_stop},
746#endif
747 {"join", 1, 2, f_join},
748 {"js_decode", 1, 1, f_js_decode},
749 {"js_encode", 1, 1, f_js_encode},
750 {"json_decode", 1, 1, f_json_decode},
751 {"json_encode", 1, 1, f_json_encode},
752 {"keys", 1, 1, f_keys},
753 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
754 {"len", 1, 1, f_len},
755 {"libcall", 3, 3, f_libcall},
756 {"libcallnr", 3, 3, f_libcallnr},
757 {"line", 1, 1, f_line},
758 {"line2byte", 1, 1, f_line2byte},
759 {"lispindent", 1, 1, f_lispindent},
Bram Moolenaar9d401282019-04-06 13:18:12 +0200760 {"list2str", 1, 2, f_list2str},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200761 {"localtime", 0, 0, f_localtime},
762#ifdef FEAT_FLOAT
763 {"log", 1, 1, f_log},
764 {"log10", 1, 1, f_log10},
765#endif
766#ifdef FEAT_LUA
767 {"luaeval", 1, 2, f_luaeval},
768#endif
769 {"map", 2, 2, f_map},
770 {"maparg", 1, 4, f_maparg},
771 {"mapcheck", 1, 3, f_mapcheck},
772 {"match", 2, 4, f_match},
773 {"matchadd", 2, 5, f_matchadd},
774 {"matchaddpos", 2, 5, f_matchaddpos},
775 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100776 {"matchdelete", 1, 2, f_matchdelete},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200777 {"matchend", 2, 4, f_matchend},
778 {"matchlist", 2, 4, f_matchlist},
779 {"matchstr", 2, 4, f_matchstr},
780 {"matchstrpos", 2, 4, f_matchstrpos},
781 {"max", 1, 1, f_max},
782 {"min", 1, 1, f_min},
783#ifdef vim_mkdir
784 {"mkdir", 1, 3, f_mkdir},
785#endif
786 {"mode", 0, 1, f_mode},
787#ifdef FEAT_MZSCHEME
788 {"mzeval", 1, 1, f_mzeval},
789#endif
790 {"nextnonblank", 1, 1, f_nextnonblank},
791 {"nr2char", 1, 2, f_nr2char},
792 {"or", 2, 2, f_or},
793 {"pathshorten", 1, 1, f_pathshorten},
794#ifdef FEAT_PERL
795 {"perleval", 1, 1, f_perleval},
796#endif
797#ifdef FEAT_FLOAT
798 {"pow", 2, 2, f_pow},
799#endif
800 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100801 {"printf", 1, 19, f_printf},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200802#ifdef FEAT_JOB_CHANNEL
803 {"prompt_setcallback", 2, 2, f_prompt_setcallback},
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200804 {"prompt_setinterrupt", 2, 2, f_prompt_setinterrupt},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200805 {"prompt_setprompt", 2, 2, f_prompt_setprompt},
806#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100807#ifdef FEAT_TEXT_PROP
808 {"prop_add", 3, 3, f_prop_add},
809 {"prop_clear", 1, 3, f_prop_clear},
810 {"prop_list", 1, 2, f_prop_list},
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100811 {"prop_remove", 1, 3, f_prop_remove},
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100812 {"prop_type_add", 2, 2, f_prop_type_add},
813 {"prop_type_change", 2, 2, f_prop_type_change},
814 {"prop_type_delete", 1, 2, f_prop_type_delete},
815 {"prop_type_get", 1, 2, f_prop_type_get},
816 {"prop_type_list", 0, 1, f_prop_type_list},
817#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200818 {"pumvisible", 0, 0, f_pumvisible},
819#ifdef FEAT_PYTHON3
820 {"py3eval", 1, 1, f_py3eval},
821#endif
822#ifdef FEAT_PYTHON
823 {"pyeval", 1, 1, f_pyeval},
824#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100825#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
826 {"pyxeval", 1, 1, f_pyxeval},
827#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200828 {"range", 1, 3, f_range},
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200829 {"readdir", 1, 2, f_readdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200830 {"readfile", 1, 3, f_readfile},
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200831 {"reg_executing", 0, 0, f_reg_executing},
832 {"reg_recording", 0, 0, f_reg_recording},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200833 {"reltime", 0, 2, f_reltime},
834#ifdef FEAT_FLOAT
835 {"reltimefloat", 1, 1, f_reltimefloat},
836#endif
837 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100838 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200839 {"remote_foreground", 1, 1, f_remote_foreground},
840 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100841 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200842 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100843 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200844 {"remove", 2, 3, f_remove},
845 {"rename", 2, 2, f_rename},
846 {"repeat", 2, 2, f_repeat},
847 {"resolve", 1, 1, f_resolve},
848 {"reverse", 1, 1, f_reverse},
849#ifdef FEAT_FLOAT
850 {"round", 1, 1, f_round},
851#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100852#ifdef FEAT_RUBY
853 {"rubyeval", 1, 1, f_rubyeval},
854#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200855 {"screenattr", 2, 2, f_screenattr},
856 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100857 {"screenchars", 2, 2, f_screenchars},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200858 {"screencol", 0, 0, f_screencol},
859 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100860 {"screenstring", 2, 2, f_screenstring},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200861 {"search", 1, 4, f_search},
862 {"searchdecl", 1, 3, f_searchdecl},
863 {"searchpair", 3, 7, f_searchpair},
864 {"searchpairpos", 3, 7, f_searchpairpos},
865 {"searchpos", 1, 4, f_searchpos},
866 {"server2client", 2, 2, f_server2client},
867 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200868 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200869 {"setbufvar", 3, 3, f_setbufvar},
870 {"setcharsearch", 1, 1, f_setcharsearch},
871 {"setcmdpos", 1, 1, f_setcmdpos},
872 {"setfperm", 2, 2, f_setfperm},
873 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200874 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100875 {"setmatches", 1, 2, f_setmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200876 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200877 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200878 {"setreg", 2, 3, f_setreg},
879 {"settabvar", 3, 3, f_settabvar},
880 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100881 {"settagstack", 2, 3, f_settagstack},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200882 {"setwinvar", 3, 3, f_setwinvar},
883#ifdef FEAT_CRYPT
884 {"sha256", 1, 1, f_sha256},
885#endif
886 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaarf9514162018-11-22 03:08:29 +0100887 {"shiftwidth", 0, 1, f_shiftwidth},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100888#ifdef FEAT_SIGNS
889 {"sign_define", 1, 2, f_sign_define},
890 {"sign_getdefined", 0, 1, f_sign_getdefined},
891 {"sign_getplaced", 0, 2, f_sign_getplaced},
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100892 {"sign_jump", 3, 3, f_sign_jump},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100893 {"sign_place", 4, 5, f_sign_place},
894 {"sign_undefine", 0, 1, f_sign_undefine},
895 {"sign_unplace", 1, 2, f_sign_unplace},
896#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200897 {"simplify", 1, 1, f_simplify},
898#ifdef FEAT_FLOAT
899 {"sin", 1, 1, f_sin},
900 {"sinh", 1, 1, f_sinh},
901#endif
902 {"sort", 1, 3, f_sort},
903 {"soundfold", 1, 1, f_soundfold},
904 {"spellbadword", 0, 1, f_spellbadword},
905 {"spellsuggest", 1, 3, f_spellsuggest},
906 {"split", 1, 3, f_split},
907#ifdef FEAT_FLOAT
908 {"sqrt", 1, 1, f_sqrt},
909 {"str2float", 1, 1, f_str2float},
910#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200911 {"str2list", 1, 2, f_str2list},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200912 {"str2nr", 1, 2, f_str2nr},
913 {"strcharpart", 2, 3, f_strcharpart},
914 {"strchars", 1, 2, f_strchars},
915 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
916#ifdef HAVE_STRFTIME
917 {"strftime", 1, 2, f_strftime},
918#endif
919 {"strgetchar", 2, 2, f_strgetchar},
920 {"stridx", 2, 3, f_stridx},
921 {"string", 1, 1, f_string},
922 {"strlen", 1, 1, f_strlen},
923 {"strpart", 2, 3, f_strpart},
924 {"strridx", 2, 3, f_strridx},
925 {"strtrans", 1, 1, f_strtrans},
926 {"strwidth", 1, 1, f_strwidth},
927 {"submatch", 1, 2, f_submatch},
928 {"substitute", 4, 4, f_substitute},
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200929 {"swapinfo", 1, 1, f_swapinfo},
Bram Moolenaar110bd602018-09-16 18:46:59 +0200930 {"swapname", 1, 1, f_swapname},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200931 {"synID", 3, 3, f_synID},
932 {"synIDattr", 2, 3, f_synIDattr},
933 {"synIDtrans", 1, 1, f_synIDtrans},
934 {"synconcealed", 2, 2, f_synconcealed},
935 {"synstack", 2, 2, f_synstack},
936 {"system", 1, 2, f_system},
937 {"systemlist", 1, 2, f_systemlist},
938 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
939 {"tabpagenr", 0, 1, f_tabpagenr},
940 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
941 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100942 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200943#ifdef FEAT_FLOAT
944 {"tan", 1, 1, f_tan},
945 {"tanh", 1, 1, f_tanh},
946#endif
947 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200948#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100949 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
950 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100951 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200952 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200953# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
954 {"term_getansicolors", 1, 1, f_term_getansicolors},
955# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200956 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200957 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200958 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200959 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200960 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200961 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200962 {"term_getstatus", 1, 1, f_term_getstatus},
963 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200964 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200965 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200966 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200967 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200968# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
969 {"term_setansicolors", 2, 2, f_term_setansicolors},
970# endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100971 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100972 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaara42d3632018-04-14 17:05:38 +0200973 {"term_setsize", 3, 3, f_term_setsize},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200974 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200975 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200976#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200977 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
978 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200979 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200980 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100981 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +0100982 {"test_null_blob", 0, 0, f_test_null_blob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200983#ifdef FEAT_JOB_CHANNEL
984 {"test_null_channel", 0, 0, f_test_null_channel},
985#endif
986 {"test_null_dict", 0, 0, f_test_null_dict},
987#ifdef FEAT_JOB_CHANNEL
988 {"test_null_job", 0, 0, f_test_null_job},
989#endif
990 {"test_null_list", 0, 0, f_test_null_list},
991 {"test_null_partial", 0, 0, f_test_null_partial},
992 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200993 {"test_option_not_set", 1, 1, f_test_option_not_set},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +0100994 {"test_override", 2, 2, f_test_override},
995 {"test_refcount", 1, 1, f_test_refcount},
Bram Moolenaarab186732018-09-14 21:27:06 +0200996#ifdef FEAT_GUI
997 {"test_scrollbar", 3, 3, f_test_scrollbar},
998#endif
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +0200999#ifdef FEAT_MOUSE
Bram Moolenaarbb8476b2019-05-04 15:47:48 +02001000 {"test_setmouse", 2, 2, f_test_setmouse},
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +02001001#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001002 {"test_settime", 1, 1, f_test_settime},
1003#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +02001004 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +02001005 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001006 {"timer_start", 2, 3, f_timer_start},
1007 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +02001008 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001009#endif
1010 {"tolower", 1, 1, f_tolower},
1011 {"toupper", 1, 1, f_toupper},
1012 {"tr", 3, 3, f_tr},
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01001013 {"trim", 1, 2, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001014#ifdef FEAT_FLOAT
1015 {"trunc", 1, 1, f_trunc},
1016#endif
1017 {"type", 1, 1, f_type},
1018 {"undofile", 1, 1, f_undofile},
1019 {"undotree", 0, 0, f_undotree},
1020 {"uniq", 1, 3, f_uniq},
1021 {"values", 1, 1, f_values},
1022 {"virtcol", 1, 1, f_virtcol},
1023 {"visualmode", 0, 1, f_visualmode},
1024 {"wildmenumode", 0, 0, f_wildmenumode},
1025 {"win_findbuf", 1, 1, f_win_findbuf},
1026 {"win_getid", 0, 2, f_win_getid},
1027 {"win_gotoid", 1, 1, f_win_gotoid},
1028 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
1029 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +01001030 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001031 {"winbufnr", 1, 1, f_winbufnr},
1032 {"wincol", 0, 0, f_wincol},
1033 {"winheight", 1, 1, f_winheight},
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +02001034 {"winlayout", 0, 1, f_winlayout},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001035 {"winline", 0, 0, f_winline},
1036 {"winnr", 0, 1, f_winnr},
1037 {"winrestcmd", 0, 0, f_winrestcmd},
1038 {"winrestview", 1, 1, f_winrestview},
1039 {"winsaveview", 0, 0, f_winsaveview},
1040 {"winwidth", 1, 1, f_winwidth},
1041 {"wordcount", 0, 0, f_wordcount},
1042 {"writefile", 2, 3, f_writefile},
1043 {"xor", 2, 2, f_xor},
1044};
1045
1046#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
1047
1048/*
1049 * Function given to ExpandGeneric() to obtain the list of internal
1050 * or user defined function names.
1051 */
1052 char_u *
1053get_function_name(expand_T *xp, int idx)
1054{
1055 static int intidx = -1;
1056 char_u *name;
1057
1058 if (idx == 0)
1059 intidx = -1;
1060 if (intidx < 0)
1061 {
1062 name = get_user_func_name(xp, idx);
1063 if (name != NULL)
1064 return name;
1065 }
1066 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
1067 {
1068 STRCPY(IObuff, functions[intidx].f_name);
1069 STRCAT(IObuff, "(");
1070 if (functions[intidx].f_max_argc == 0)
1071 STRCAT(IObuff, ")");
1072 return IObuff;
1073 }
1074
1075 return NULL;
1076}
1077
1078/*
1079 * Function given to ExpandGeneric() to obtain the list of internal or
1080 * user defined variable or function names.
1081 */
1082 char_u *
1083get_expr_name(expand_T *xp, int idx)
1084{
1085 static int intidx = -1;
1086 char_u *name;
1087
1088 if (idx == 0)
1089 intidx = -1;
1090 if (intidx < 0)
1091 {
1092 name = get_function_name(xp, idx);
1093 if (name != NULL)
1094 return name;
1095 }
1096 return get_user_var_name(xp, ++intidx);
1097}
1098
1099#endif /* FEAT_CMDL_COMPL */
1100
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001101/*
1102 * Find internal function in table above.
1103 * Return index, or -1 if not found
1104 */
1105 int
1106find_internal_func(
1107 char_u *name) /* name of the function */
1108{
1109 int first = 0;
1110 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
1111 int cmp;
1112 int x;
1113
1114 /*
1115 * Find the function name in the table. Binary search.
1116 */
1117 while (first <= last)
1118 {
1119 x = first + ((unsigned)(last - first) >> 1);
1120 cmp = STRCMP(name, functions[x].f_name);
1121 if (cmp < 0)
1122 last = x - 1;
1123 else if (cmp > 0)
1124 first = x + 1;
1125 else
1126 return x;
1127 }
1128 return -1;
1129}
1130
1131 int
1132call_internal_func(
1133 char_u *name,
1134 int argcount,
1135 typval_T *argvars,
1136 typval_T *rettv)
1137{
1138 int i;
1139
1140 i = find_internal_func(name);
1141 if (i < 0)
1142 return ERROR_UNKNOWN;
1143 if (argcount < functions[i].f_min_argc)
1144 return ERROR_TOOFEW;
1145 if (argcount > functions[i].f_max_argc)
1146 return ERROR_TOOMANY;
1147 argvars[argcount].v_type = VAR_UNKNOWN;
1148 functions[i].f_func(argvars, rettv);
1149 return ERROR_NONE;
1150}
1151
1152/*
1153 * Return TRUE for a non-zero Number and a non-empty String.
1154 */
1155 static int
1156non_zero_arg(typval_T *argvars)
1157{
1158 return ((argvars[0].v_type == VAR_NUMBER
1159 && argvars[0].vval.v_number != 0)
1160 || (argvars[0].v_type == VAR_SPECIAL
1161 && argvars[0].vval.v_number == VVAL_TRUE)
1162 || (argvars[0].v_type == VAR_STRING
1163 && argvars[0].vval.v_string != NULL
1164 && *argvars[0].vval.v_string != NUL));
1165}
1166
1167/*
1168 * Get the lnum from the first argument.
1169 * Also accepts ".", "$", etc., but that only works for the current buffer.
1170 * Returns -1 on error.
1171 */
1172 static linenr_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001173tv_get_lnum(typval_T *argvars)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001174{
1175 typval_T rettv;
1176 linenr_T lnum;
1177
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001178 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001179 if (lnum == 0) /* no valid number, try using line() */
1180 {
1181 rettv.v_type = VAR_NUMBER;
1182 f_line(argvars, &rettv);
1183 lnum = (linenr_T)rettv.vval.v_number;
1184 clear_tv(&rettv);
1185 }
1186 return lnum;
1187}
1188
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001189/*
1190 * Get the lnum from the first argument.
1191 * Also accepts "$", then "buf" is used.
1192 * Returns 0 on error.
1193 */
1194 static linenr_T
1195tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
1196{
1197 if (argvars[0].v_type == VAR_STRING
1198 && argvars[0].vval.v_string != NULL
1199 && argvars[0].vval.v_string[0] == '$'
1200 && buf != NULL)
1201 return buf->b_ml.ml_line_count;
1202 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1203}
1204
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001205#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001206/*
1207 * Get the float value of "argvars[0]" into "f".
1208 * Returns FAIL when the argument is not a Number or Float.
1209 */
1210 static int
1211get_float_arg(typval_T *argvars, float_T *f)
1212{
1213 if (argvars[0].v_type == VAR_FLOAT)
1214 {
1215 *f = argvars[0].vval.v_float;
1216 return OK;
1217 }
1218 if (argvars[0].v_type == VAR_NUMBER)
1219 {
1220 *f = (float_T)argvars[0].vval.v_number;
1221 return OK;
1222 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001223 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001224 return FAIL;
1225}
1226
1227/*
1228 * "abs(expr)" function
1229 */
1230 static void
1231f_abs(typval_T *argvars, typval_T *rettv)
1232{
1233 if (argvars[0].v_type == VAR_FLOAT)
1234 {
1235 rettv->v_type = VAR_FLOAT;
1236 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1237 }
1238 else
1239 {
1240 varnumber_T n;
1241 int error = FALSE;
1242
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001243 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001244 if (error)
1245 rettv->vval.v_number = -1;
1246 else if (n > 0)
1247 rettv->vval.v_number = n;
1248 else
1249 rettv->vval.v_number = -n;
1250 }
1251}
1252
1253/*
1254 * "acos()" function
1255 */
1256 static void
1257f_acos(typval_T *argvars, typval_T *rettv)
1258{
1259 float_T f = 0.0;
1260
1261 rettv->v_type = VAR_FLOAT;
1262 if (get_float_arg(argvars, &f) == OK)
1263 rettv->vval.v_float = acos(f);
1264 else
1265 rettv->vval.v_float = 0.0;
1266}
1267#endif
1268
1269/*
1270 * "add(list, item)" function
1271 */
1272 static void
1273f_add(typval_T *argvars, typval_T *rettv)
1274{
1275 list_T *l;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001276 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001277
1278 rettv->vval.v_number = 1; /* Default: Failed */
1279 if (argvars[0].v_type == VAR_LIST)
1280 {
1281 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001282 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001283 (char_u *)N_("add() argument"), TRUE)
1284 && list_append_tv(l, &argvars[1]) == OK)
1285 copy_tv(&argvars[0], rettv);
1286 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001287 else if (argvars[0].v_type == VAR_BLOB)
1288 {
1289 if ((b = argvars[0].vval.v_blob) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001290 && !var_check_lock(b->bv_lock,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001291 (char_u *)N_("add() argument"), TRUE))
1292 {
Bram Moolenaar05500ec2019-01-13 19:10:33 +01001293 int error = FALSE;
1294 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
1295
1296 if (!error)
1297 {
1298 ga_append(&b->bv_ga, (int)n);
1299 copy_tv(&argvars[0], rettv);
1300 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001301 }
1302 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001303 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01001304 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001305}
1306
1307/*
1308 * "and(expr, expr)" function
1309 */
1310 static void
1311f_and(typval_T *argvars, typval_T *rettv)
1312{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001313 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1314 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001315}
1316
1317/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02001318 * If there is a window for "curbuf", make it the current window.
1319 */
1320 static void
1321find_win_for_curbuf(void)
1322{
1323 wininfo_T *wip;
1324
1325 for (wip = curbuf->b_wininfo; wip != NULL; wip = wip->wi_next)
1326 {
1327 if (wip->wi_win != NULL)
1328 {
1329 curwin = wip->wi_win;
1330 break;
1331 }
1332 }
1333}
1334
1335/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001336 * Set line or list of lines in buffer "buf".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001337 */
1338 static void
Bram Moolenaarca851592018-06-06 21:04:07 +02001339set_buffer_lines(
1340 buf_T *buf,
1341 linenr_T lnum_arg,
1342 int append,
1343 typval_T *lines,
1344 typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001345{
Bram Moolenaarca851592018-06-06 21:04:07 +02001346 linenr_T lnum = lnum_arg + (append ? 1 : 0);
1347 char_u *line = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001348 list_T *l = NULL;
1349 listitem_T *li = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001350 long added = 0;
Bram Moolenaarca851592018-06-06 21:04:07 +02001351 linenr_T append_lnum;
1352 buf_T *curbuf_save = NULL;
1353 win_T *curwin_save = NULL;
1354 int is_curbuf = buf == curbuf;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001355
Bram Moolenaarca851592018-06-06 21:04:07 +02001356 /* When using the current buffer ml_mfp will be set if needed. Useful when
1357 * setline() is used on startup. For other buffers the buffer must be
1358 * loaded. */
1359 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001360 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001361 rettv->vval.v_number = 1; /* FAIL */
1362 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001363 }
1364
Bram Moolenaarca851592018-06-06 21:04:07 +02001365 if (!is_curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001366 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001367 curbuf_save = curbuf;
1368 curwin_save = curwin;
1369 curbuf = buf;
Bram Moolenaard79a2622018-06-07 18:17:46 +02001370 find_win_for_curbuf();
Bram Moolenaarca851592018-06-06 21:04:07 +02001371 }
1372
1373 if (append)
1374 // appendbufline() uses the line number below which we insert
1375 append_lnum = lnum - 1;
1376 else
1377 // setbufline() uses the line number above which we insert, we only
1378 // append if it's below the last line
1379 append_lnum = curbuf->b_ml.ml_line_count;
1380
1381 if (lines->v_type == VAR_LIST)
1382 {
1383 l = lines->vval.v_list;
1384 li = l->lv_first;
1385 }
1386 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001387 line = tv_get_string_chk(lines);
Bram Moolenaarca851592018-06-06 21:04:07 +02001388
1389 /* default result is zero == OK */
1390 for (;;)
1391 {
1392 if (l != NULL)
1393 {
1394 /* list argument, get next string */
1395 if (li == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001396 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001397 line = tv_get_string_chk(&li->li_tv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001398 li = li->li_next;
1399 }
1400
Bram Moolenaarca851592018-06-06 21:04:07 +02001401 rettv->vval.v_number = 1; /* FAIL */
1402 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
1403 break;
1404
1405 /* When coming here from Insert mode, sync undo, so that this can be
1406 * undone separately from what was previously inserted. */
1407 if (u_sync_once == 2)
1408 {
1409 u_sync_once = 1; /* notify that u_sync() was called */
1410 u_sync(TRUE);
1411 }
1412
1413 if (!append && lnum <= curbuf->b_ml.ml_line_count)
1414 {
Bram Moolenaar21b50382019-01-04 18:07:24 +01001415 // Existing line, replace it.
1416 // Removes any existing text properties.
1417 if (u_savesub(lnum) == OK && ml_replace_len(
1418 lnum, line, (colnr_T)STRLEN(line) + 1, TRUE, TRUE) == OK)
Bram Moolenaarca851592018-06-06 21:04:07 +02001419 {
1420 changed_bytes(lnum, 0);
1421 if (is_curbuf && lnum == curwin->w_cursor.lnum)
1422 check_cursor_col();
1423 rettv->vval.v_number = 0; /* OK */
1424 }
1425 }
1426 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
1427 {
1428 /* append the line */
1429 ++added;
1430 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
1431 rettv->vval.v_number = 0; /* OK */
1432 }
1433
1434 if (l == NULL) /* only one string argument */
1435 break;
1436 ++lnum;
1437 }
1438
1439 if (added > 0)
1440 {
1441 win_T *wp;
1442 tabpage_T *tp;
1443
1444 appended_lines_mark(append_lnum, added);
1445 FOR_ALL_TAB_WINDOWS(tp, wp)
1446 if (wp->w_buffer == buf && wp->w_cursor.lnum > append_lnum)
1447 wp->w_cursor.lnum += added;
1448 check_cursor_col();
1449
Bram Moolenaarf2732452018-06-03 14:47:35 +02001450#ifdef FEAT_JOB_CHANNEL
1451 if (bt_prompt(curbuf) && (State & INSERT))
1452 // show the line with the prompt
1453 update_topline();
1454#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001455 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001456
1457 if (!is_curbuf)
1458 {
1459 curbuf = curbuf_save;
1460 curwin = curwin_save;
1461 }
1462}
1463
1464/*
1465 * "append(lnum, string/list)" function
1466 */
1467 static void
1468f_append(typval_T *argvars, typval_T *rettv)
1469{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001470 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaarca851592018-06-06 21:04:07 +02001471
1472 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1473}
1474
1475/*
1476 * "appendbufline(buf, lnum, string/list)" function
1477 */
1478 static void
1479f_appendbufline(typval_T *argvars, typval_T *rettv)
1480{
1481 linenr_T lnum;
1482 buf_T *buf;
1483
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001484 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarca851592018-06-06 21:04:07 +02001485 if (buf == NULL)
1486 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001487 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001488 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001489 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +02001490 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1491 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001492}
1493
1494/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001495 * "argc([window id])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001496 */
1497 static void
Bram Moolenaare6e39892018-10-25 12:32:11 +02001498f_argc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001499{
Bram Moolenaare6e39892018-10-25 12:32:11 +02001500 win_T *wp;
1501
1502 if (argvars[0].v_type == VAR_UNKNOWN)
1503 // use the current window
1504 rettv->vval.v_number = ARGCOUNT;
1505 else if (argvars[0].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001506 && tv_get_number(&argvars[0]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001507 // use the global argument list
1508 rettv->vval.v_number = GARGCOUNT;
1509 else
1510 {
1511 // use the argument list of the specified window
1512 wp = find_win_by_nr_or_id(&argvars[0]);
1513 if (wp != NULL)
1514 rettv->vval.v_number = WARGCOUNT(wp);
1515 else
1516 rettv->vval.v_number = -1;
1517 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001518}
1519
1520/*
1521 * "argidx()" function
1522 */
1523 static void
1524f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1525{
1526 rettv->vval.v_number = curwin->w_arg_idx;
1527}
1528
1529/*
1530 * "arglistid()" function
1531 */
1532 static void
1533f_arglistid(typval_T *argvars, typval_T *rettv)
1534{
1535 win_T *wp;
1536
1537 rettv->vval.v_number = -1;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02001538 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001539 if (wp != NULL)
1540 rettv->vval.v_number = wp->w_alist->id;
1541}
1542
1543/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001544 * Get the argument list for a given window
1545 */
1546 static void
1547get_arglist_as_rettv(aentry_T *arglist, int argcount, typval_T *rettv)
1548{
1549 int idx;
1550
1551 if (rettv_list_alloc(rettv) == OK && arglist != NULL)
1552 for (idx = 0; idx < argcount; ++idx)
1553 list_append_string(rettv->vval.v_list,
1554 alist_name(&arglist[idx]), -1);
1555}
1556
1557/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001558 * "argv(nr)" function
1559 */
1560 static void
1561f_argv(typval_T *argvars, typval_T *rettv)
1562{
1563 int idx;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001564 aentry_T *arglist = NULL;
1565 int argcount = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001566
1567 if (argvars[0].v_type != VAR_UNKNOWN)
1568 {
Bram Moolenaare6e39892018-10-25 12:32:11 +02001569 if (argvars[1].v_type == VAR_UNKNOWN)
1570 {
1571 arglist = ARGLIST;
1572 argcount = ARGCOUNT;
1573 }
1574 else if (argvars[1].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001575 && tv_get_number(&argvars[1]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001576 {
1577 arglist = GARGLIST;
1578 argcount = GARGCOUNT;
1579 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001580 else
Bram Moolenaare6e39892018-10-25 12:32:11 +02001581 {
1582 win_T *wp = find_win_by_nr_or_id(&argvars[1]);
1583
1584 if (wp != NULL)
1585 {
1586 /* Use the argument list of the specified window */
1587 arglist = WARGLIST(wp);
1588 argcount = WARGCOUNT(wp);
1589 }
1590 }
1591
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001592 rettv->v_type = VAR_STRING;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001593 rettv->vval.v_string = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001594 idx = tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02001595 if (arglist != NULL && idx >= 0 && idx < argcount)
1596 rettv->vval.v_string = vim_strsave(alist_name(&arglist[idx]));
1597 else if (idx == -1)
1598 get_arglist_as_rettv(arglist, argcount, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001599 }
Bram Moolenaare6e39892018-10-25 12:32:11 +02001600 else
1601 get_arglist_as_rettv(ARGLIST, ARGCOUNT, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001602}
1603
1604/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001605 * "assert_beeps(cmd [, error])" function
1606 */
1607 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001608f_assert_beeps(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001609{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001610 rettv->vval.v_number = assert_beeps(argvars);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001611}
1612
1613/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001614 * "assert_equal(expected, actual[, msg])" function
1615 */
1616 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001617f_assert_equal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001618{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001619 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_EQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001620}
1621
1622/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01001623 * "assert_equalfile(fname-one, fname-two)" function
1624 */
1625 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001626f_assert_equalfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaard96ff162018-02-18 22:13:29 +01001627{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001628 rettv->vval.v_number = assert_equalfile(argvars);
Bram Moolenaard96ff162018-02-18 22:13:29 +01001629}
1630
1631/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001632 * "assert_notequal(expected, actual[, msg])" function
1633 */
1634 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001635f_assert_notequal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001636{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001637 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001638}
1639
1640/*
1641 * "assert_exception(string[, msg])" function
1642 */
1643 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001644f_assert_exception(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001645{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001646 rettv->vval.v_number = assert_exception(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001647}
1648
1649/*
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02001650 * "assert_fails(cmd [, error[, msg]])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001651 */
1652 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001653f_assert_fails(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001654{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001655 rettv->vval.v_number = assert_fails(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001656}
1657
1658/*
1659 * "assert_false(actual[, msg])" function
1660 */
1661 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001662f_assert_false(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001663{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001664 rettv->vval.v_number = assert_bool(argvars, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001665}
1666
1667/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001668 * "assert_inrange(lower, upper[, msg])" function
1669 */
1670 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001671f_assert_inrange(typval_T *argvars, typval_T *rettv)
Bram Moolenaar61c04492016-07-23 15:35:35 +02001672{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001673 rettv->vval.v_number = assert_inrange(argvars);
Bram Moolenaar61c04492016-07-23 15:35:35 +02001674}
1675
1676/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001677 * "assert_match(pattern, actual[, msg])" function
1678 */
1679 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001680f_assert_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001681{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001682 rettv->vval.v_number = assert_match_common(argvars, ASSERT_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001683}
1684
1685/*
1686 * "assert_notmatch(pattern, actual[, msg])" function
1687 */
1688 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001689f_assert_notmatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001690{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001691 rettv->vval.v_number = assert_match_common(argvars, ASSERT_NOTMATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001692}
1693
1694/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001695 * "assert_report(msg)" function
1696 */
1697 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001698f_assert_report(typval_T *argvars, typval_T *rettv)
Bram Moolenaar42205552017-03-18 19:42:22 +01001699{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001700 rettv->vval.v_number = assert_report(argvars);
Bram Moolenaar42205552017-03-18 19:42:22 +01001701}
1702
1703/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001704 * "assert_true(actual[, msg])" function
1705 */
1706 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001707f_assert_true(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001708{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001709 rettv->vval.v_number = assert_bool(argvars, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001710}
1711
1712#ifdef FEAT_FLOAT
1713/*
1714 * "asin()" function
1715 */
1716 static void
1717f_asin(typval_T *argvars, typval_T *rettv)
1718{
1719 float_T f = 0.0;
1720
1721 rettv->v_type = VAR_FLOAT;
1722 if (get_float_arg(argvars, &f) == OK)
1723 rettv->vval.v_float = asin(f);
1724 else
1725 rettv->vval.v_float = 0.0;
1726}
1727
1728/*
1729 * "atan()" function
1730 */
1731 static void
1732f_atan(typval_T *argvars, typval_T *rettv)
1733{
1734 float_T f = 0.0;
1735
1736 rettv->v_type = VAR_FLOAT;
1737 if (get_float_arg(argvars, &f) == OK)
1738 rettv->vval.v_float = atan(f);
1739 else
1740 rettv->vval.v_float = 0.0;
1741}
1742
1743/*
1744 * "atan2()" function
1745 */
1746 static void
1747f_atan2(typval_T *argvars, typval_T *rettv)
1748{
1749 float_T fx = 0.0, fy = 0.0;
1750
1751 rettv->v_type = VAR_FLOAT;
1752 if (get_float_arg(argvars, &fx) == OK
1753 && get_float_arg(&argvars[1], &fy) == OK)
1754 rettv->vval.v_float = atan2(fx, fy);
1755 else
1756 rettv->vval.v_float = 0.0;
1757}
1758#endif
1759
1760/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001761 * "balloon_show()" function
1762 */
1763#ifdef FEAT_BEVAL
1764 static void
1765f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1766{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001767 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001768 {
1769 if (argvars[0].v_type == VAR_LIST
1770# ifdef FEAT_GUI
1771 && !gui.in_use
1772# endif
1773 )
1774 post_balloon(balloonEval, NULL, argvars[0].vval.v_list);
1775 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001776 post_balloon(balloonEval, tv_get_string_chk(&argvars[0]), NULL);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001777 }
1778}
1779
Bram Moolenaar669a8282017-11-19 20:13:05 +01001780# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001781 static void
1782f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1783{
1784 if (rettv_list_alloc(rettv) == OK)
1785 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001786 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001787
1788 if (msg != NULL)
1789 {
1790 pumitem_T *array;
1791 int size = split_message(msg, &array);
1792 int i;
1793
1794 /* Skip the first and last item, they are always empty. */
1795 for (i = 1; i < size - 1; ++i)
1796 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001797 while (size > 0)
1798 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001799 vim_free(array);
1800 }
1801 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001802}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001803# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001804#endif
1805
1806/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001807 * "browse(save, title, initdir, default)" function
1808 */
1809 static void
1810f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1811{
1812#ifdef FEAT_BROWSE
1813 int save;
1814 char_u *title;
1815 char_u *initdir;
1816 char_u *defname;
1817 char_u buf[NUMBUFLEN];
1818 char_u buf2[NUMBUFLEN];
1819 int error = FALSE;
1820
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001821 save = (int)tv_get_number_chk(&argvars[0], &error);
1822 title = tv_get_string_chk(&argvars[1]);
1823 initdir = tv_get_string_buf_chk(&argvars[2], buf);
1824 defname = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001825
1826 if (error || title == NULL || initdir == NULL || defname == NULL)
1827 rettv->vval.v_string = NULL;
1828 else
1829 rettv->vval.v_string =
1830 do_browse(save ? BROWSE_SAVE : 0,
1831 title, defname, NULL, initdir, NULL, curbuf);
1832#else
1833 rettv->vval.v_string = NULL;
1834#endif
1835 rettv->v_type = VAR_STRING;
1836}
1837
1838/*
1839 * "browsedir(title, initdir)" function
1840 */
1841 static void
1842f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1843{
1844#ifdef FEAT_BROWSE
1845 char_u *title;
1846 char_u *initdir;
1847 char_u buf[NUMBUFLEN];
1848
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001849 title = tv_get_string_chk(&argvars[0]);
1850 initdir = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001851
1852 if (title == NULL || initdir == NULL)
1853 rettv->vval.v_string = NULL;
1854 else
1855 rettv->vval.v_string = do_browse(BROWSE_DIR,
1856 title, NULL, NULL, initdir, NULL, curbuf);
1857#else
1858 rettv->vval.v_string = NULL;
1859#endif
1860 rettv->v_type = VAR_STRING;
1861}
1862
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001863/*
1864 * Find a buffer by number or exact name.
1865 */
1866 static buf_T *
1867find_buffer(typval_T *avar)
1868{
1869 buf_T *buf = NULL;
1870
1871 if (avar->v_type == VAR_NUMBER)
1872 buf = buflist_findnr((int)avar->vval.v_number);
1873 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1874 {
1875 buf = buflist_findname_exp(avar->vval.v_string);
1876 if (buf == NULL)
1877 {
1878 /* No full path name match, try a match with a URL or a "nofile"
1879 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001880 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001881 if (buf->b_fname != NULL
1882 && (path_with_url(buf->b_fname)
1883#ifdef FEAT_QUICKFIX
1884 || bt_nofile(buf)
1885#endif
1886 )
1887 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1888 break;
1889 }
1890 }
1891 return buf;
1892}
1893
1894/*
1895 * "bufexists(expr)" function
1896 */
1897 static void
1898f_bufexists(typval_T *argvars, typval_T *rettv)
1899{
1900 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1901}
1902
1903/*
1904 * "buflisted(expr)" function
1905 */
1906 static void
1907f_buflisted(typval_T *argvars, typval_T *rettv)
1908{
1909 buf_T *buf;
1910
1911 buf = find_buffer(&argvars[0]);
1912 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1913}
1914
1915/*
1916 * "bufloaded(expr)" function
1917 */
1918 static void
1919f_bufloaded(typval_T *argvars, typval_T *rettv)
1920{
1921 buf_T *buf;
1922
1923 buf = find_buffer(&argvars[0]);
1924 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1925}
1926
1927 buf_T *
1928buflist_find_by_name(char_u *name, int curtab_only)
1929{
1930 int save_magic;
1931 char_u *save_cpo;
1932 buf_T *buf;
1933
1934 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1935 save_magic = p_magic;
1936 p_magic = TRUE;
1937 save_cpo = p_cpo;
1938 p_cpo = (char_u *)"";
1939
1940 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1941 TRUE, FALSE, curtab_only));
1942
1943 p_magic = save_magic;
1944 p_cpo = save_cpo;
1945 return buf;
1946}
1947
1948/*
1949 * Get buffer by number or pattern.
1950 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001951 buf_T *
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001952tv_get_buf(typval_T *tv, int curtab_only)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001953{
1954 char_u *name = tv->vval.v_string;
1955 buf_T *buf;
1956
1957 if (tv->v_type == VAR_NUMBER)
1958 return buflist_findnr((int)tv->vval.v_number);
1959 if (tv->v_type != VAR_STRING)
1960 return NULL;
1961 if (name == NULL || *name == NUL)
1962 return curbuf;
1963 if (name[0] == '$' && name[1] == NUL)
1964 return lastbuf;
1965
1966 buf = buflist_find_by_name(name, curtab_only);
1967
1968 /* If not found, try expanding the name, like done for bufexists(). */
1969 if (buf == NULL)
1970 buf = find_buffer(tv);
1971
1972 return buf;
1973}
1974
Bram Moolenaarec9d3002019-01-12 13:50:31 +01001975#ifdef FEAT_SIGNS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001976/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001977 * Get the buffer from "arg" and give an error and return NULL if it is not
1978 * valid.
1979 */
1980 static buf_T *
1981get_buf_arg(typval_T *arg)
1982{
1983 buf_T *buf;
1984
1985 ++emsg_off;
1986 buf = tv_get_buf(arg, FALSE);
1987 --emsg_off;
1988 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001989 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001990 return buf;
1991}
Bram Moolenaarec9d3002019-01-12 13:50:31 +01001992#endif
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001993
1994/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001995 * "bufname(expr)" function
1996 */
1997 static void
1998f_bufname(typval_T *argvars, typval_T *rettv)
1999{
2000 buf_T *buf;
2001
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002002 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002003 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002004 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002005 rettv->v_type = VAR_STRING;
2006 if (buf != NULL && buf->b_fname != NULL)
2007 rettv->vval.v_string = vim_strsave(buf->b_fname);
2008 else
2009 rettv->vval.v_string = NULL;
2010 --emsg_off;
2011}
2012
2013/*
2014 * "bufnr(expr)" function
2015 */
2016 static void
2017f_bufnr(typval_T *argvars, typval_T *rettv)
2018{
2019 buf_T *buf;
2020 int error = FALSE;
2021 char_u *name;
2022
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002023 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002024 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002025 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002026 --emsg_off;
2027
2028 /* If the buffer isn't found and the second argument is not zero create a
2029 * new buffer. */
2030 if (buf == NULL
2031 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002032 && tv_get_number_chk(&argvars[1], &error) != 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002033 && !error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002034 && (name = tv_get_string_chk(&argvars[0])) != NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002035 && !error)
2036 buf = buflist_new(name, NULL, (linenr_T)1, 0);
2037
2038 if (buf != NULL)
2039 rettv->vval.v_number = buf->b_fnum;
2040 else
2041 rettv->vval.v_number = -1;
2042}
2043
2044 static void
2045buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
2046{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002047 win_T *wp;
2048 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002049 buf_T *buf;
2050
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002051 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002052 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002053 buf = tv_get_buf(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02002054 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002055 {
2056 ++winnr;
2057 if (wp->w_buffer == buf)
2058 break;
2059 }
2060 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002061 --emsg_off;
2062}
2063
2064/*
2065 * "bufwinid(nr)" function
2066 */
2067 static void
2068f_bufwinid(typval_T *argvars, typval_T *rettv)
2069{
2070 buf_win_common(argvars, rettv, FALSE);
2071}
2072
2073/*
2074 * "bufwinnr(nr)" function
2075 */
2076 static void
2077f_bufwinnr(typval_T *argvars, typval_T *rettv)
2078{
2079 buf_win_common(argvars, rettv, TRUE);
2080}
2081
2082/*
2083 * "byte2line(byte)" function
2084 */
2085 static void
2086f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2087{
2088#ifndef FEAT_BYTEOFF
2089 rettv->vval.v_number = -1;
2090#else
2091 long boff = 0;
2092
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002093 boff = tv_get_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002094 if (boff < 0)
2095 rettv->vval.v_number = -1;
2096 else
2097 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2098 (linenr_T)0, &boff);
2099#endif
2100}
2101
2102 static void
2103byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2104{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002105 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002106 char_u *str;
2107 varnumber_T idx;
2108
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002109 str = tv_get_string_chk(&argvars[0]);
2110 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002111 rettv->vval.v_number = -1;
2112 if (str == NULL || idx < 0)
2113 return;
2114
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002115 t = str;
2116 for ( ; idx > 0; idx--)
2117 {
2118 if (*t == NUL) /* EOL reached */
2119 return;
2120 if (enc_utf8 && comp)
2121 t += utf_ptr2len(t);
2122 else
2123 t += (*mb_ptr2len)(t);
2124 }
2125 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002126}
2127
2128/*
2129 * "byteidx()" function
2130 */
2131 static void
2132f_byteidx(typval_T *argvars, typval_T *rettv)
2133{
2134 byteidx(argvars, rettv, FALSE);
2135}
2136
2137/*
2138 * "byteidxcomp()" function
2139 */
2140 static void
2141f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2142{
2143 byteidx(argvars, rettv, TRUE);
2144}
2145
2146/*
2147 * "call(func, arglist [, dict])" function
2148 */
2149 static void
2150f_call(typval_T *argvars, typval_T *rettv)
2151{
2152 char_u *func;
2153 partial_T *partial = NULL;
2154 dict_T *selfdict = NULL;
2155
2156 if (argvars[1].v_type != VAR_LIST)
2157 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002158 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002159 return;
2160 }
2161 if (argvars[1].vval.v_list == NULL)
2162 return;
2163
2164 if (argvars[0].v_type == VAR_FUNC)
2165 func = argvars[0].vval.v_string;
2166 else if (argvars[0].v_type == VAR_PARTIAL)
2167 {
2168 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002169 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002170 }
2171 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002172 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002173 if (*func == NUL)
2174 return; /* type error or empty name */
2175
2176 if (argvars[2].v_type != VAR_UNKNOWN)
2177 {
2178 if (argvars[2].v_type != VAR_DICT)
2179 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002180 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002181 return;
2182 }
2183 selfdict = argvars[2].vval.v_dict;
2184 }
2185
2186 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2187}
2188
2189#ifdef FEAT_FLOAT
2190/*
2191 * "ceil({float})" function
2192 */
2193 static void
2194f_ceil(typval_T *argvars, typval_T *rettv)
2195{
2196 float_T f = 0.0;
2197
2198 rettv->v_type = VAR_FLOAT;
2199 if (get_float_arg(argvars, &f) == OK)
2200 rettv->vval.v_float = ceil(f);
2201 else
2202 rettv->vval.v_float = 0.0;
2203}
2204#endif
2205
2206#ifdef FEAT_JOB_CHANNEL
2207/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002208 * "ch_canread()" function
2209 */
2210 static void
2211f_ch_canread(typval_T *argvars, typval_T *rettv)
2212{
Bram Moolenaar958dc692016-12-01 15:34:12 +01002213 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002214
2215 rettv->vval.v_number = 0;
2216 if (channel != NULL)
2217 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
2218 || channel_has_readahead(channel, PART_OUT)
2219 || channel_has_readahead(channel, PART_ERR);
2220}
2221
2222/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002223 * "ch_close()" function
2224 */
2225 static void
2226f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
2227{
2228 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2229
2230 if (channel != NULL)
2231 {
2232 channel_close(channel, FALSE);
2233 channel_clear(channel);
2234 }
2235}
2236
2237/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002238 * "ch_close()" function
2239 */
2240 static void
2241f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
2242{
2243 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2244
2245 if (channel != NULL)
2246 channel_close_in(channel);
2247}
2248
2249/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002250 * "ch_getbufnr()" function
2251 */
2252 static void
2253f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
2254{
2255 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2256
2257 rettv->vval.v_number = -1;
2258 if (channel != NULL)
2259 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002260 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002261 int part;
2262
2263 if (STRCMP(what, "err") == 0)
2264 part = PART_ERR;
2265 else if (STRCMP(what, "out") == 0)
2266 part = PART_OUT;
2267 else if (STRCMP(what, "in") == 0)
2268 part = PART_IN;
2269 else
2270 part = PART_SOCK;
2271 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
2272 rettv->vval.v_number =
2273 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
2274 }
2275}
2276
2277/*
2278 * "ch_getjob()" function
2279 */
2280 static void
2281f_ch_getjob(typval_T *argvars, typval_T *rettv)
2282{
2283 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2284
2285 if (channel != NULL)
2286 {
2287 rettv->v_type = VAR_JOB;
2288 rettv->vval.v_job = channel->ch_job;
2289 if (channel->ch_job != NULL)
2290 ++channel->ch_job->jv_refcount;
2291 }
2292}
2293
2294/*
2295 * "ch_info()" function
2296 */
2297 static void
2298f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
2299{
2300 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2301
2302 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
2303 channel_info(channel, rettv->vval.v_dict);
2304}
2305
2306/*
2307 * "ch_log()" function
2308 */
2309 static void
2310f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
2311{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002312 char_u *msg = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002313 channel_T *channel = NULL;
2314
2315 if (argvars[1].v_type != VAR_UNKNOWN)
2316 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
2317
Bram Moolenaard5359b22018-04-05 22:44:39 +02002318 ch_log(channel, "%s", msg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002319}
2320
2321/*
2322 * "ch_logfile()" function
2323 */
2324 static void
2325f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
2326{
2327 char_u *fname;
2328 char_u *opt = (char_u *)"";
2329 char_u buf[NUMBUFLEN];
2330
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02002331 /* Don't open a file in restricted mode. */
2332 if (check_restricted() || check_secure())
2333 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002334 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002335 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002336 opt = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002337 ch_logfile(fname, opt);
2338}
2339
2340/*
2341 * "ch_open()" function
2342 */
2343 static void
2344f_ch_open(typval_T *argvars, typval_T *rettv)
2345{
2346 rettv->v_type = VAR_CHANNEL;
2347 if (check_restricted() || check_secure())
2348 return;
2349 rettv->vval.v_channel = channel_open_func(argvars);
2350}
2351
2352/*
2353 * "ch_read()" function
2354 */
2355 static void
2356f_ch_read(typval_T *argvars, typval_T *rettv)
2357{
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002358 common_channel_read(argvars, rettv, FALSE, FALSE);
2359}
2360
2361/*
2362 * "ch_readblob()" function
2363 */
2364 static void
2365f_ch_readblob(typval_T *argvars, typval_T *rettv)
2366{
2367 common_channel_read(argvars, rettv, TRUE, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002368}
2369
2370/*
2371 * "ch_readraw()" function
2372 */
2373 static void
2374f_ch_readraw(typval_T *argvars, typval_T *rettv)
2375{
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002376 common_channel_read(argvars, rettv, TRUE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002377}
2378
2379/*
2380 * "ch_evalexpr()" function
2381 */
2382 static void
2383f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2384{
2385 ch_expr_common(argvars, rettv, TRUE);
2386}
2387
2388/*
2389 * "ch_sendexpr()" function
2390 */
2391 static void
2392f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2393{
2394 ch_expr_common(argvars, rettv, FALSE);
2395}
2396
2397/*
2398 * "ch_evalraw()" function
2399 */
2400 static void
2401f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2402{
2403 ch_raw_common(argvars, rettv, TRUE);
2404}
2405
2406/*
2407 * "ch_sendraw()" function
2408 */
2409 static void
2410f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2411{
2412 ch_raw_common(argvars, rettv, FALSE);
2413}
2414
2415/*
2416 * "ch_setoptions()" function
2417 */
2418 static void
2419f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2420{
2421 channel_T *channel;
2422 jobopt_T opt;
2423
2424 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2425 if (channel == NULL)
2426 return;
2427 clear_job_options(&opt);
2428 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002429 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002430 channel_set_options(channel, &opt);
2431 free_job_options(&opt);
2432}
2433
2434/*
2435 * "ch_status()" function
2436 */
2437 static void
2438f_ch_status(typval_T *argvars, typval_T *rettv)
2439{
2440 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002441 jobopt_T opt;
2442 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002443
2444 /* return an empty string by default */
2445 rettv->v_type = VAR_STRING;
2446 rettv->vval.v_string = NULL;
2447
2448 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002449
2450 if (argvars[1].v_type != VAR_UNKNOWN)
2451 {
2452 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002453 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002454 && (opt.jo_set & JO_PART))
2455 part = opt.jo_part;
2456 }
2457
2458 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002459}
2460#endif
2461
2462/*
2463 * "changenr()" function
2464 */
2465 static void
2466f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2467{
2468 rettv->vval.v_number = curbuf->b_u_seq_cur;
2469}
2470
2471/*
2472 * "char2nr(string)" function
2473 */
2474 static void
2475f_char2nr(typval_T *argvars, typval_T *rettv)
2476{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002477 if (has_mbyte)
2478 {
2479 int utf8 = 0;
2480
2481 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002482 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002483
2484 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002485 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002486 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002487 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002488 }
2489 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002490 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002491}
2492
2493/*
2494 * "cindent(lnum)" function
2495 */
2496 static void
2497f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2498{
2499#ifdef FEAT_CINDENT
2500 pos_T pos;
2501 linenr_T lnum;
2502
2503 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002504 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002505 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2506 {
2507 curwin->w_cursor.lnum = lnum;
2508 rettv->vval.v_number = get_c_indent();
2509 curwin->w_cursor = pos;
2510 }
2511 else
2512#endif
2513 rettv->vval.v_number = -1;
2514}
2515
Bram Moolenaaraff74912019-03-30 18:11:49 +01002516 static win_T *
2517get_optional_window(typval_T *argvars, int idx)
2518{
2519 win_T *win = curwin;
2520
2521 if (argvars[idx].v_type != VAR_UNKNOWN)
2522 {
2523 win = find_win_by_nr_or_id(&argvars[idx]);
2524 if (win == NULL)
2525 {
2526 emsg(_(e_invalwindow));
2527 return NULL;
2528 }
2529 }
2530 return win;
2531}
2532
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002533/*
2534 * "clearmatches()" function
2535 */
2536 static void
2537f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2538{
2539#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01002540 win_T *win = get_optional_window(argvars, 0);
2541
2542 if (win != NULL)
2543 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002544#endif
2545}
2546
2547/*
2548 * "col(string)" function
2549 */
2550 static void
2551f_col(typval_T *argvars, typval_T *rettv)
2552{
2553 colnr_T col = 0;
2554 pos_T *fp;
2555 int fnum = curbuf->b_fnum;
2556
2557 fp = var2fpos(&argvars[0], FALSE, &fnum);
2558 if (fp != NULL && fnum == curbuf->b_fnum)
2559 {
2560 if (fp->col == MAXCOL)
2561 {
2562 /* '> can be MAXCOL, get the length of the line then */
2563 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2564 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2565 else
2566 col = MAXCOL;
2567 }
2568 else
2569 {
2570 col = fp->col + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002571 /* col(".") when the cursor is on the NUL at the end of the line
2572 * because of "coladd" can be seen as an extra column. */
2573 if (virtual_active() && fp == &curwin->w_cursor)
2574 {
2575 char_u *p = ml_get_cursor();
2576
2577 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2578 curwin->w_virtcol - curwin->w_cursor.coladd))
2579 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002580 int l;
2581
2582 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2583 col += l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002584 }
2585 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002586 }
2587 }
2588 rettv->vval.v_number = col;
2589}
2590
2591#if defined(FEAT_INS_EXPAND)
2592/*
2593 * "complete()" function
2594 */
2595 static void
2596f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2597{
2598 int startcol;
2599
2600 if ((State & INSERT) == 0)
2601 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002602 emsg(_("E785: complete() can only be used in Insert mode"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002603 return;
2604 }
2605
2606 /* Check for undo allowed here, because if something was already inserted
2607 * the line was already saved for undo and this check isn't done. */
2608 if (!undo_allowed())
2609 return;
2610
2611 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2612 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002613 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002614 return;
2615 }
2616
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002617 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002618 if (startcol <= 0)
2619 return;
2620
2621 set_completion(startcol - 1, argvars[1].vval.v_list);
2622}
2623
2624/*
2625 * "complete_add()" function
2626 */
2627 static void
2628f_complete_add(typval_T *argvars, typval_T *rettv)
2629{
2630 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2631}
2632
2633/*
2634 * "complete_check()" function
2635 */
2636 static void
2637f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2638{
2639 int saved = RedrawingDisabled;
2640
2641 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002642 ins_compl_check_keys(0, TRUE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002643 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002644 RedrawingDisabled = saved;
2645}
Bram Moolenaarfd133322019-03-29 12:20:27 +01002646
2647/*
2648 * "complete_info()" function
2649 */
2650 static void
2651f_complete_info(typval_T *argvars, typval_T *rettv)
2652{
2653 list_T *what_list = NULL;
2654
2655 if (rettv_dict_alloc(rettv) != OK)
2656 return;
2657
2658 if (argvars[0].v_type != VAR_UNKNOWN)
2659 {
2660 if (argvars[0].v_type != VAR_LIST)
2661 {
2662 emsg(_(e_listreq));
2663 return;
2664 }
2665 what_list = argvars[0].vval.v_list;
2666 }
2667 get_complete_info(what_list, rettv->vval.v_dict);
2668}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002669#endif
2670
2671/*
2672 * "confirm(message, buttons[, default [, type]])" function
2673 */
2674 static void
2675f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2676{
2677#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2678 char_u *message;
2679 char_u *buttons = NULL;
2680 char_u buf[NUMBUFLEN];
2681 char_u buf2[NUMBUFLEN];
2682 int def = 1;
2683 int type = VIM_GENERIC;
2684 char_u *typestr;
2685 int error = FALSE;
2686
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002687 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002688 if (message == NULL)
2689 error = TRUE;
2690 if (argvars[1].v_type != VAR_UNKNOWN)
2691 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002692 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002693 if (buttons == NULL)
2694 error = TRUE;
2695 if (argvars[2].v_type != VAR_UNKNOWN)
2696 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002697 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002698 if (argvars[3].v_type != VAR_UNKNOWN)
2699 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002700 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002701 if (typestr == NULL)
2702 error = TRUE;
2703 else
2704 {
2705 switch (TOUPPER_ASC(*typestr))
2706 {
2707 case 'E': type = VIM_ERROR; break;
2708 case 'Q': type = VIM_QUESTION; break;
2709 case 'I': type = VIM_INFO; break;
2710 case 'W': type = VIM_WARNING; break;
2711 case 'G': type = VIM_GENERIC; break;
2712 }
2713 }
2714 }
2715 }
2716 }
2717
2718 if (buttons == NULL || *buttons == NUL)
2719 buttons = (char_u *)_("&Ok");
2720
2721 if (!error)
2722 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2723 def, NULL, FALSE);
2724#endif
2725}
2726
2727/*
2728 * "copy()" function
2729 */
2730 static void
2731f_copy(typval_T *argvars, typval_T *rettv)
2732{
2733 item_copy(&argvars[0], rettv, FALSE, 0);
2734}
2735
2736#ifdef FEAT_FLOAT
2737/*
2738 * "cos()" function
2739 */
2740 static void
2741f_cos(typval_T *argvars, typval_T *rettv)
2742{
2743 float_T f = 0.0;
2744
2745 rettv->v_type = VAR_FLOAT;
2746 if (get_float_arg(argvars, &f) == OK)
2747 rettv->vval.v_float = cos(f);
2748 else
2749 rettv->vval.v_float = 0.0;
2750}
2751
2752/*
2753 * "cosh()" function
2754 */
2755 static void
2756f_cosh(typval_T *argvars, typval_T *rettv)
2757{
2758 float_T f = 0.0;
2759
2760 rettv->v_type = VAR_FLOAT;
2761 if (get_float_arg(argvars, &f) == OK)
2762 rettv->vval.v_float = cosh(f);
2763 else
2764 rettv->vval.v_float = 0.0;
2765}
2766#endif
2767
2768/*
2769 * "count()" function
2770 */
2771 static void
2772f_count(typval_T *argvars, typval_T *rettv)
2773{
2774 long n = 0;
2775 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002776 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002777
Bram Moolenaar9966b212017-07-28 16:46:57 +02002778 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002779 ic = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002780
2781 if (argvars[0].v_type == VAR_STRING)
2782 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002783 char_u *expr = tv_get_string_chk(&argvars[1]);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002784 char_u *p = argvars[0].vval.v_string;
2785 char_u *next;
2786
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002787 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002788 {
2789 if (ic)
2790 {
2791 size_t len = STRLEN(expr);
2792
2793 while (*p != NUL)
2794 {
2795 if (MB_STRNICMP(p, expr, len) == 0)
2796 {
2797 ++n;
2798 p += len;
2799 }
2800 else
2801 MB_PTR_ADV(p);
2802 }
2803 }
2804 else
2805 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2806 != NULL)
2807 {
2808 ++n;
2809 p = next + STRLEN(expr);
2810 }
2811 }
2812
2813 }
2814 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002815 {
2816 listitem_T *li;
2817 list_T *l;
2818 long idx;
2819
2820 if ((l = argvars[0].vval.v_list) != NULL)
2821 {
2822 li = l->lv_first;
2823 if (argvars[2].v_type != VAR_UNKNOWN)
2824 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002825 if (argvars[3].v_type != VAR_UNKNOWN)
2826 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002827 idx = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002828 if (!error)
2829 {
2830 li = list_find(l, idx);
2831 if (li == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002832 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002833 }
2834 }
2835 if (error)
2836 li = NULL;
2837 }
2838
2839 for ( ; li != NULL; li = li->li_next)
2840 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2841 ++n;
2842 }
2843 }
2844 else if (argvars[0].v_type == VAR_DICT)
2845 {
2846 int todo;
2847 dict_T *d;
2848 hashitem_T *hi;
2849
2850 if ((d = argvars[0].vval.v_dict) != NULL)
2851 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002852 if (argvars[2].v_type != VAR_UNKNOWN)
2853 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002854 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002855 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002856 }
2857
2858 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2859 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2860 {
2861 if (!HASHITEM_EMPTY(hi))
2862 {
2863 --todo;
2864 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2865 ++n;
2866 }
2867 }
2868 }
2869 }
2870 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002871 semsg(_(e_listdictarg), "count()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002872 rettv->vval.v_number = n;
2873}
2874
2875/*
2876 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2877 *
2878 * Checks the existence of a cscope connection.
2879 */
2880 static void
2881f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2882{
2883#ifdef FEAT_CSCOPE
2884 int num = 0;
2885 char_u *dbpath = NULL;
2886 char_u *prepend = NULL;
2887 char_u buf[NUMBUFLEN];
2888
2889 if (argvars[0].v_type != VAR_UNKNOWN
2890 && argvars[1].v_type != VAR_UNKNOWN)
2891 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002892 num = (int)tv_get_number(&argvars[0]);
2893 dbpath = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002894 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002895 prepend = tv_get_string_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002896 }
2897
2898 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2899#endif
2900}
2901
2902/*
2903 * "cursor(lnum, col)" function, or
2904 * "cursor(list)"
2905 *
2906 * Moves the cursor to the specified line and column.
2907 * Returns 0 when the position could be set, -1 otherwise.
2908 */
2909 static void
2910f_cursor(typval_T *argvars, typval_T *rettv)
2911{
2912 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002913 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002914 int set_curswant = TRUE;
2915
2916 rettv->vval.v_number = -1;
2917 if (argvars[1].v_type == VAR_UNKNOWN)
2918 {
2919 pos_T pos;
2920 colnr_T curswant = -1;
2921
2922 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2923 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002924 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002925 return;
2926 }
2927 line = pos.lnum;
2928 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002929 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002930 if (curswant >= 0)
2931 {
2932 curwin->w_curswant = curswant - 1;
2933 set_curswant = FALSE;
2934 }
2935 }
2936 else
2937 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002938 line = tv_get_lnum(argvars);
2939 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002940 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002941 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002942 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002943 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002944 return; /* type error; errmsg already given */
2945 if (line > 0)
2946 curwin->w_cursor.lnum = line;
2947 if (col > 0)
2948 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002949 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002950
2951 /* Make sure the cursor is in a valid position. */
2952 check_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002953 /* Correct cursor for multi-byte character. */
2954 if (has_mbyte)
2955 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002956
2957 curwin->w_set_curswant = set_curswant;
2958 rettv->vval.v_number = 0;
2959}
2960
Bram Moolenaar4f974752019-02-17 17:44:42 +01002961#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002962/*
2963 * "debugbreak()" function
2964 */
2965 static void
2966f_debugbreak(typval_T *argvars, typval_T *rettv)
2967{
2968 int pid;
2969
2970 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002971 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002972 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002973 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002974 else
2975 {
2976 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2977
2978 if (hProcess != NULL)
2979 {
2980 DebugBreakProcess(hProcess);
2981 CloseHandle(hProcess);
2982 rettv->vval.v_number = OK;
2983 }
2984 }
2985}
2986#endif
2987
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002988/*
2989 * "deepcopy()" function
2990 */
2991 static void
2992f_deepcopy(typval_T *argvars, typval_T *rettv)
2993{
2994 int noref = 0;
2995 int copyID;
2996
2997 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002998 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002999 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003000 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003001 else
3002 {
3003 copyID = get_copyID();
3004 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
3005 }
3006}
3007
3008/*
3009 * "delete()" function
3010 */
3011 static void
3012f_delete(typval_T *argvars, typval_T *rettv)
3013{
3014 char_u nbuf[NUMBUFLEN];
3015 char_u *name;
3016 char_u *flags;
3017
3018 rettv->vval.v_number = -1;
3019 if (check_restricted() || check_secure())
3020 return;
3021
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003022 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003023 if (name == NULL || *name == NUL)
3024 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003025 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003026 return;
3027 }
3028
3029 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003030 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003031 else
3032 flags = (char_u *)"";
3033
3034 if (*flags == NUL)
3035 /* delete a file */
3036 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
3037 else if (STRCMP(flags, "d") == 0)
3038 /* delete an empty directory */
3039 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
3040 else if (STRCMP(flags, "rf") == 0)
3041 /* delete a directory recursively */
3042 rettv->vval.v_number = delete_recursive(name);
3043 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003044 semsg(_(e_invexpr2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003045}
3046
3047/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02003048 * "deletebufline()" function
3049 */
3050 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02003051f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02003052{
3053 buf_T *buf;
3054 linenr_T first, last;
3055 linenr_T lnum;
3056 long count;
3057 int is_curbuf;
3058 buf_T *curbuf_save = NULL;
3059 win_T *curwin_save = NULL;
3060 tabpage_T *tp;
3061 win_T *wp;
3062
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01003063 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003064 if (buf == NULL)
3065 {
3066 rettv->vval.v_number = 1; /* FAIL */
3067 return;
3068 }
3069 is_curbuf = buf == curbuf;
3070
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003071 first = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003072 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003073 last = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003074 else
3075 last = first;
3076
3077 if (buf->b_ml.ml_mfp == NULL || first < 1
3078 || first > buf->b_ml.ml_line_count || last < first)
3079 {
3080 rettv->vval.v_number = 1; /* FAIL */
3081 return;
3082 }
3083
3084 if (!is_curbuf)
3085 {
3086 curbuf_save = curbuf;
3087 curwin_save = curwin;
3088 curbuf = buf;
3089 find_win_for_curbuf();
3090 }
3091 if (last > curbuf->b_ml.ml_line_count)
3092 last = curbuf->b_ml.ml_line_count;
3093 count = last - first + 1;
3094
3095 // When coming here from Insert mode, sync undo, so that this can be
3096 // undone separately from what was previously inserted.
3097 if (u_sync_once == 2)
3098 {
3099 u_sync_once = 1; // notify that u_sync() was called
3100 u_sync(TRUE);
3101 }
3102
3103 if (u_save(first - 1, last + 1) == FAIL)
3104 {
3105 rettv->vval.v_number = 1; /* FAIL */
3106 return;
3107 }
3108
3109 for (lnum = first; lnum <= last; ++lnum)
3110 ml_delete(first, TRUE);
3111
3112 FOR_ALL_TAB_WINDOWS(tp, wp)
3113 if (wp->w_buffer == buf)
3114 {
3115 if (wp->w_cursor.lnum > last)
3116 wp->w_cursor.lnum -= count;
3117 else if (wp->w_cursor.lnum> first)
3118 wp->w_cursor.lnum = first;
3119 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
3120 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
3121 }
3122 check_cursor_col();
3123 deleted_lines_mark(first, count);
3124
3125 if (!is_curbuf)
3126 {
3127 curbuf = curbuf_save;
3128 curwin = curwin_save;
3129 }
3130}
3131
3132/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003133 * "did_filetype()" function
3134 */
3135 static void
3136f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3137{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003138 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003139}
3140
3141/*
3142 * "diff_filler()" function
3143 */
3144 static void
3145f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3146{
3147#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003148 rettv->vval.v_number = diff_check_fill(curwin, tv_get_lnum(argvars));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003149#endif
3150}
3151
3152/*
3153 * "diff_hlID()" function
3154 */
3155 static void
3156f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3157{
3158#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003159 linenr_T lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003160 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01003161 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003162 static int fnum = 0;
3163 static int change_start = 0;
3164 static int change_end = 0;
3165 static hlf_T hlID = (hlf_T)0;
3166 int filler_lines;
3167 int col;
3168
3169 if (lnum < 0) /* ignore type error in {lnum} arg */
3170 lnum = 0;
3171 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003172 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003173 || fnum != curbuf->b_fnum)
3174 {
3175 /* New line, buffer, change: need to get the values. */
3176 filler_lines = diff_check(curwin, lnum);
3177 if (filler_lines < 0)
3178 {
3179 if (filler_lines == -1)
3180 {
3181 change_start = MAXCOL;
3182 change_end = -1;
3183 if (diff_find_change(curwin, lnum, &change_start, &change_end))
3184 hlID = HLF_ADD; /* added line */
3185 else
3186 hlID = HLF_CHD; /* changed line */
3187 }
3188 else
3189 hlID = HLF_ADD; /* added line */
3190 }
3191 else
3192 hlID = (hlf_T)0;
3193 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003194 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003195 fnum = curbuf->b_fnum;
3196 }
3197
3198 if (hlID == HLF_CHD || hlID == HLF_TXD)
3199 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003200 col = tv_get_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003201 if (col >= change_start && col <= change_end)
3202 hlID = HLF_TXD; /* changed text */
3203 else
3204 hlID = HLF_CHD; /* changed line */
3205 }
3206 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
3207#endif
3208}
3209
3210/*
3211 * "empty({expr})" function
3212 */
3213 static void
3214f_empty(typval_T *argvars, typval_T *rettv)
3215{
3216 int n = FALSE;
3217
3218 switch (argvars[0].v_type)
3219 {
3220 case VAR_STRING:
3221 case VAR_FUNC:
3222 n = argvars[0].vval.v_string == NULL
3223 || *argvars[0].vval.v_string == NUL;
3224 break;
3225 case VAR_PARTIAL:
3226 n = FALSE;
3227 break;
3228 case VAR_NUMBER:
3229 n = argvars[0].vval.v_number == 0;
3230 break;
3231 case VAR_FLOAT:
3232#ifdef FEAT_FLOAT
3233 n = argvars[0].vval.v_float == 0.0;
3234 break;
3235#endif
3236 case VAR_LIST:
3237 n = argvars[0].vval.v_list == NULL
3238 || argvars[0].vval.v_list->lv_first == NULL;
3239 break;
3240 case VAR_DICT:
3241 n = argvars[0].vval.v_dict == NULL
3242 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
3243 break;
3244 case VAR_SPECIAL:
3245 n = argvars[0].vval.v_number != VVAL_TRUE;
3246 break;
3247
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003248 case VAR_BLOB:
3249 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003250 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
3251 break;
3252
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003253 case VAR_JOB:
3254#ifdef FEAT_JOB_CHANNEL
3255 n = argvars[0].vval.v_job == NULL
3256 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
3257 break;
3258#endif
3259 case VAR_CHANNEL:
3260#ifdef FEAT_JOB_CHANNEL
3261 n = argvars[0].vval.v_channel == NULL
3262 || !channel_is_open(argvars[0].vval.v_channel);
3263 break;
3264#endif
3265 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01003266 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003267 n = TRUE;
3268 break;
3269 }
3270
3271 rettv->vval.v_number = n;
3272}
3273
3274/*
3275 * "escape({string}, {chars})" function
3276 */
3277 static void
3278f_escape(typval_T *argvars, typval_T *rettv)
3279{
3280 char_u buf[NUMBUFLEN];
3281
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003282 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
3283 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003284 rettv->v_type = VAR_STRING;
3285}
3286
3287/*
3288 * "eval()" function
3289 */
3290 static void
3291f_eval(typval_T *argvars, typval_T *rettv)
3292{
3293 char_u *s, *p;
3294
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003295 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003296 if (s != NULL)
3297 s = skipwhite(s);
3298
3299 p = s;
3300 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3301 {
3302 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003303 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003304 need_clr_eos = FALSE;
3305 rettv->v_type = VAR_NUMBER;
3306 rettv->vval.v_number = 0;
3307 }
3308 else if (*s != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003309 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003310}
3311
3312/*
3313 * "eventhandler()" function
3314 */
3315 static void
3316f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3317{
3318 rettv->vval.v_number = vgetc_busy;
3319}
3320
3321/*
3322 * "executable()" function
3323 */
3324 static void
3325f_executable(typval_T *argvars, typval_T *rettv)
3326{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003327 char_u *name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003328
3329 /* Check in $PATH and also check directly if there is a directory name. */
3330 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
3331 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
3332}
3333
3334static garray_T redir_execute_ga;
3335
3336/*
3337 * Append "value[value_len]" to the execute() output.
3338 */
3339 void
3340execute_redir_str(char_u *value, int value_len)
3341{
3342 int len;
3343
3344 if (value_len == -1)
3345 len = (int)STRLEN(value); /* Append the entire string */
3346 else
3347 len = value_len; /* Append only "value_len" characters */
3348 if (ga_grow(&redir_execute_ga, len) == OK)
3349 {
3350 mch_memmove((char *)redir_execute_ga.ga_data
3351 + redir_execute_ga.ga_len, value, len);
3352 redir_execute_ga.ga_len += len;
3353 }
3354}
3355
3356/*
3357 * Get next line from a list.
3358 * Called by do_cmdline() to get the next line.
3359 * Returns allocated string, or NULL for end of function.
3360 */
3361
3362 static char_u *
3363get_list_line(
3364 int c UNUSED,
3365 void *cookie,
3366 int indent UNUSED)
3367{
3368 listitem_T **p = (listitem_T **)cookie;
3369 listitem_T *item = *p;
3370 char_u buf[NUMBUFLEN];
3371 char_u *s;
3372
3373 if (item == NULL)
3374 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003375 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003376 *p = item->li_next;
3377 return s == NULL ? NULL : vim_strsave(s);
3378}
3379
3380/*
3381 * "execute()" function
3382 */
3383 static void
3384f_execute(typval_T *argvars, typval_T *rettv)
3385{
3386 char_u *cmd = NULL;
3387 list_T *list = NULL;
3388 int save_msg_silent = msg_silent;
3389 int save_emsg_silent = emsg_silent;
3390 int save_emsg_noredir = emsg_noredir;
3391 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003392 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003393 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003394 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003395 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003396
3397 rettv->vval.v_string = NULL;
3398 rettv->v_type = VAR_STRING;
3399
3400 if (argvars[0].v_type == VAR_LIST)
3401 {
3402 list = argvars[0].vval.v_list;
3403 if (list == NULL || list->lv_first == NULL)
3404 /* empty list, no commands, empty output */
3405 return;
3406 ++list->lv_refcount;
3407 }
3408 else
3409 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003410 cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003411 if (cmd == NULL)
3412 return;
3413 }
3414
3415 if (argvars[1].v_type != VAR_UNKNOWN)
3416 {
3417 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003418 char_u *s = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003419
3420 if (s == NULL)
3421 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003422 if (*s == NUL)
3423 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003424 if (STRNCMP(s, "silent", 6) == 0)
3425 ++msg_silent;
3426 if (STRCMP(s, "silent!") == 0)
3427 {
3428 emsg_silent = TRUE;
3429 emsg_noredir = TRUE;
3430 }
3431 }
3432 else
3433 ++msg_silent;
3434
3435 if (redir_execute)
3436 save_ga = redir_execute_ga;
3437 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3438 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003439 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003440 if (!echo_output)
3441 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003442
3443 if (cmd != NULL)
3444 do_cmdline_cmd(cmd);
3445 else
3446 {
3447 listitem_T *item = list->lv_first;
3448
3449 do_cmdline(NULL, get_list_line, (void *)&item,
3450 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3451 --list->lv_refcount;
3452 }
3453
Bram Moolenaard297f352017-01-29 20:31:21 +01003454 /* Need to append a NUL to the result. */
3455 if (ga_grow(&redir_execute_ga, 1) == OK)
3456 {
3457 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3458 rettv->vval.v_string = redir_execute_ga.ga_data;
3459 }
3460 else
3461 {
3462 ga_clear(&redir_execute_ga);
3463 rettv->vval.v_string = NULL;
3464 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003465 msg_silent = save_msg_silent;
3466 emsg_silent = save_emsg_silent;
3467 emsg_noredir = save_emsg_noredir;
3468
3469 redir_execute = save_redir_execute;
3470 if (redir_execute)
3471 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003472 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003473
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003474 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003475 if (echo_output)
3476 // When not working silently: put it in column zero. A following
3477 // "echon" will overwrite the message, unavoidably.
3478 msg_col = 0;
3479 else
3480 // When working silently: Put it back where it was, since nothing
3481 // should have been written.
3482 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003483}
3484
3485/*
3486 * "exepath()" function
3487 */
3488 static void
3489f_exepath(typval_T *argvars, typval_T *rettv)
3490{
3491 char_u *p = NULL;
3492
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003493 (void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003494 rettv->v_type = VAR_STRING;
3495 rettv->vval.v_string = p;
3496}
3497
3498/*
3499 * "exists()" function
3500 */
3501 static void
3502f_exists(typval_T *argvars, typval_T *rettv)
3503{
3504 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003505 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003506
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003507 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003508 if (*p == '$') /* environment variable */
3509 {
3510 /* first try "normal" environment variables (fast) */
3511 if (mch_getenv(p + 1) != NULL)
3512 n = TRUE;
3513 else
3514 {
3515 /* try expanding things like $VIM and ${HOME} */
3516 p = expand_env_save(p);
3517 if (p != NULL && *p != '$')
3518 n = TRUE;
3519 vim_free(p);
3520 }
3521 }
3522 else if (*p == '&' || *p == '+') /* option */
3523 {
3524 n = (get_option_tv(&p, NULL, TRUE) == OK);
3525 if (*skipwhite(p) != NUL)
3526 n = FALSE; /* trailing garbage */
3527 }
3528 else if (*p == '*') /* internal or user defined function */
3529 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003530 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003531 }
3532 else if (*p == ':')
3533 {
3534 n = cmd_exists(p + 1);
3535 }
3536 else if (*p == '#')
3537 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003538 if (p[1] == '#')
3539 n = autocmd_supported(p + 2);
3540 else
3541 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003542 }
3543 else /* internal variable */
3544 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003545 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003546 }
3547
3548 rettv->vval.v_number = n;
3549}
3550
3551#ifdef FEAT_FLOAT
3552/*
3553 * "exp()" function
3554 */
3555 static void
3556f_exp(typval_T *argvars, typval_T *rettv)
3557{
3558 float_T f = 0.0;
3559
3560 rettv->v_type = VAR_FLOAT;
3561 if (get_float_arg(argvars, &f) == OK)
3562 rettv->vval.v_float = exp(f);
3563 else
3564 rettv->vval.v_float = 0.0;
3565}
3566#endif
3567
3568/*
3569 * "expand()" function
3570 */
3571 static void
3572f_expand(typval_T *argvars, typval_T *rettv)
3573{
3574 char_u *s;
3575 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003576 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003577 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3578 expand_T xpc;
3579 int error = FALSE;
3580 char_u *result;
3581
3582 rettv->v_type = VAR_STRING;
3583 if (argvars[1].v_type != VAR_UNKNOWN
3584 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003585 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003586 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003587 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003588
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003589 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003590 if (*s == '%' || *s == '#' || *s == '<')
3591 {
3592 ++emsg_off;
3593 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3594 --emsg_off;
3595 if (rettv->v_type == VAR_LIST)
3596 {
3597 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3598 list_append_string(rettv->vval.v_list, result, -1);
3599 else
3600 vim_free(result);
3601 }
3602 else
3603 rettv->vval.v_string = result;
3604 }
3605 else
3606 {
3607 /* When the optional second argument is non-zero, don't remove matches
3608 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3609 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003610 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003611 options |= WILD_KEEP_ALL;
3612 if (!error)
3613 {
3614 ExpandInit(&xpc);
3615 xpc.xp_context = EXPAND_FILES;
3616 if (p_wic)
3617 options += WILD_ICASE;
3618 if (rettv->v_type == VAR_STRING)
3619 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3620 options, WILD_ALL);
3621 else if (rettv_list_alloc(rettv) != FAIL)
3622 {
3623 int i;
3624
3625 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3626 for (i = 0; i < xpc.xp_numfiles; i++)
3627 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3628 ExpandCleanup(&xpc);
3629 }
3630 }
3631 else
3632 rettv->vval.v_string = NULL;
3633 }
3634}
3635
3636/*
3637 * "extend(list, list [, idx])" function
3638 * "extend(dict, dict [, action])" function
3639 */
3640 static void
3641f_extend(typval_T *argvars, typval_T *rettv)
3642{
3643 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3644
3645 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3646 {
3647 list_T *l1, *l2;
3648 listitem_T *item;
3649 long before;
3650 int error = FALSE;
3651
3652 l1 = argvars[0].vval.v_list;
3653 l2 = argvars[1].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003654 if (l1 != NULL && !var_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003655 && l2 != NULL)
3656 {
3657 if (argvars[2].v_type != VAR_UNKNOWN)
3658 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003659 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003660 if (error)
3661 return; /* type error; errmsg already given */
3662
3663 if (before == l1->lv_len)
3664 item = NULL;
3665 else
3666 {
3667 item = list_find(l1, before);
3668 if (item == NULL)
3669 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003670 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003671 return;
3672 }
3673 }
3674 }
3675 else
3676 item = NULL;
3677 list_extend(l1, l2, item);
3678
3679 copy_tv(&argvars[0], rettv);
3680 }
3681 }
3682 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3683 {
3684 dict_T *d1, *d2;
3685 char_u *action;
3686 int i;
3687
3688 d1 = argvars[0].vval.v_dict;
3689 d2 = argvars[1].vval.v_dict;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003690 if (d1 != NULL && !var_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003691 && d2 != NULL)
3692 {
3693 /* Check the third argument. */
3694 if (argvars[2].v_type != VAR_UNKNOWN)
3695 {
3696 static char *(av[]) = {"keep", "force", "error"};
3697
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003698 action = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003699 if (action == NULL)
3700 return; /* type error; errmsg already given */
3701 for (i = 0; i < 3; ++i)
3702 if (STRCMP(action, av[i]) == 0)
3703 break;
3704 if (i == 3)
3705 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003706 semsg(_(e_invarg2), action);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003707 return;
3708 }
3709 }
3710 else
3711 action = (char_u *)"force";
3712
3713 dict_extend(d1, d2, action);
3714
3715 copy_tv(&argvars[0], rettv);
3716 }
3717 }
3718 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003719 semsg(_(e_listdictarg), "extend()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003720}
3721
3722/*
3723 * "feedkeys()" function
3724 */
3725 static void
3726f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3727{
3728 int remap = TRUE;
3729 int insert = FALSE;
3730 char_u *keys, *flags;
3731 char_u nbuf[NUMBUFLEN];
3732 int typed = FALSE;
3733 int execute = FALSE;
3734 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003735 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003736 char_u *keys_esc;
3737
3738 /* This is not allowed in the sandbox. If the commands would still be
3739 * executed in the sandbox it would be OK, but it probably happens later,
3740 * when "sandbox" is no longer set. */
3741 if (check_secure())
3742 return;
3743
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003744 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003745
3746 if (argvars[1].v_type != VAR_UNKNOWN)
3747 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003748 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003749 for ( ; *flags != NUL; ++flags)
3750 {
3751 switch (*flags)
3752 {
3753 case 'n': remap = FALSE; break;
3754 case 'm': remap = TRUE; break;
3755 case 't': typed = TRUE; break;
3756 case 'i': insert = TRUE; break;
3757 case 'x': execute = TRUE; break;
3758 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003759 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003760 }
3761 }
3762 }
3763
3764 if (*keys != NUL || execute)
3765 {
3766 /* Need to escape K_SPECIAL and CSI before putting the string in the
3767 * typeahead buffer. */
3768 keys_esc = vim_strsave_escape_csi(keys);
3769 if (keys_esc != NULL)
3770 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003771 if (lowlevel)
3772 {
3773#ifdef USE_INPUT_BUF
3774 add_to_input_buf(keys, (int)STRLEN(keys));
3775#else
3776 emsg(_("E980: lowlevel input not supported"));
3777#endif
3778 }
3779 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003780 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003781 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003782 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003783 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003784#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003785 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003786#endif
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003787 )
3788 typebuf_was_filled = TRUE;
3789 }
3790 vim_free(keys_esc);
3791
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003792 if (execute)
3793 {
3794 int save_msg_scroll = msg_scroll;
3795
3796 /* Avoid a 1 second delay when the keys start Insert mode. */
3797 msg_scroll = FALSE;
3798
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003799 if (!dangerous)
3800 ++ex_normal_busy;
Bram Moolenaar905dd902019-04-07 14:21:47 +02003801 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003802 if (!dangerous)
3803 --ex_normal_busy;
3804
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003805 msg_scroll |= save_msg_scroll;
3806 }
3807 }
3808 }
3809}
3810
3811/*
3812 * "filereadable()" function
3813 */
3814 static void
3815f_filereadable(typval_T *argvars, typval_T *rettv)
3816{
3817 int fd;
3818 char_u *p;
3819 int n;
3820
3821#ifndef O_NONBLOCK
3822# define O_NONBLOCK 0
3823#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003824 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003825 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3826 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3827 {
3828 n = TRUE;
3829 close(fd);
3830 }
3831 else
3832 n = FALSE;
3833
3834 rettv->vval.v_number = n;
3835}
3836
3837/*
3838 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3839 * rights to write into.
3840 */
3841 static void
3842f_filewritable(typval_T *argvars, typval_T *rettv)
3843{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003844 rettv->vval.v_number = filewritable(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003845}
3846
3847 static void
3848findfilendir(
3849 typval_T *argvars UNUSED,
3850 typval_T *rettv,
3851 int find_what UNUSED)
3852{
3853#ifdef FEAT_SEARCHPATH
3854 char_u *fname;
3855 char_u *fresult = NULL;
3856 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3857 char_u *p;
3858 char_u pathbuf[NUMBUFLEN];
3859 int count = 1;
3860 int first = TRUE;
3861 int error = FALSE;
3862#endif
3863
3864 rettv->vval.v_string = NULL;
3865 rettv->v_type = VAR_STRING;
3866
3867#ifdef FEAT_SEARCHPATH
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003868 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003869
3870 if (argvars[1].v_type != VAR_UNKNOWN)
3871 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003872 p = tv_get_string_buf_chk(&argvars[1], pathbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003873 if (p == NULL)
3874 error = TRUE;
3875 else
3876 {
3877 if (*p != NUL)
3878 path = p;
3879
3880 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003881 count = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003882 }
3883 }
3884
3885 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3886 error = TRUE;
3887
3888 if (*fname != NUL && !error)
3889 {
3890 do
3891 {
3892 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3893 vim_free(fresult);
3894 fresult = find_file_in_path_option(first ? fname : NULL,
3895 first ? (int)STRLEN(fname) : 0,
3896 0, first, path,
3897 find_what,
3898 curbuf->b_ffname,
3899 find_what == FINDFILE_DIR
3900 ? (char_u *)"" : curbuf->b_p_sua);
3901 first = FALSE;
3902
3903 if (fresult != NULL && rettv->v_type == VAR_LIST)
3904 list_append_string(rettv->vval.v_list, fresult, -1);
3905
3906 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3907 }
3908
3909 if (rettv->v_type == VAR_STRING)
3910 rettv->vval.v_string = fresult;
3911#endif
3912}
3913
3914/*
3915 * "filter()" function
3916 */
3917 static void
3918f_filter(typval_T *argvars, typval_T *rettv)
3919{
3920 filter_map(argvars, rettv, FALSE);
3921}
3922
3923/*
3924 * "finddir({fname}[, {path}[, {count}]])" function
3925 */
3926 static void
3927f_finddir(typval_T *argvars, typval_T *rettv)
3928{
3929 findfilendir(argvars, rettv, FINDFILE_DIR);
3930}
3931
3932/*
3933 * "findfile({fname}[, {path}[, {count}]])" function
3934 */
3935 static void
3936f_findfile(typval_T *argvars, typval_T *rettv)
3937{
3938 findfilendir(argvars, rettv, FINDFILE_FILE);
3939}
3940
3941#ifdef FEAT_FLOAT
3942/*
3943 * "float2nr({float})" function
3944 */
3945 static void
3946f_float2nr(typval_T *argvars, typval_T *rettv)
3947{
3948 float_T f = 0.0;
3949
3950 if (get_float_arg(argvars, &f) == OK)
3951 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003952 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003953 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003954 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003955 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003956 else
3957 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003958 }
3959}
3960
3961/*
3962 * "floor({float})" function
3963 */
3964 static void
3965f_floor(typval_T *argvars, typval_T *rettv)
3966{
3967 float_T f = 0.0;
3968
3969 rettv->v_type = VAR_FLOAT;
3970 if (get_float_arg(argvars, &f) == OK)
3971 rettv->vval.v_float = floor(f);
3972 else
3973 rettv->vval.v_float = 0.0;
3974}
3975
3976/*
3977 * "fmod()" function
3978 */
3979 static void
3980f_fmod(typval_T *argvars, typval_T *rettv)
3981{
3982 float_T fx = 0.0, fy = 0.0;
3983
3984 rettv->v_type = VAR_FLOAT;
3985 if (get_float_arg(argvars, &fx) == OK
3986 && get_float_arg(&argvars[1], &fy) == OK)
3987 rettv->vval.v_float = fmod(fx, fy);
3988 else
3989 rettv->vval.v_float = 0.0;
3990}
3991#endif
3992
3993/*
3994 * "fnameescape({string})" function
3995 */
3996 static void
3997f_fnameescape(typval_T *argvars, typval_T *rettv)
3998{
3999 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004000 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004001 rettv->v_type = VAR_STRING;
4002}
4003
4004/*
4005 * "fnamemodify({fname}, {mods})" function
4006 */
4007 static void
4008f_fnamemodify(typval_T *argvars, typval_T *rettv)
4009{
4010 char_u *fname;
4011 char_u *mods;
4012 int usedlen = 0;
4013 int len;
4014 char_u *fbuf = NULL;
4015 char_u buf[NUMBUFLEN];
4016
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004017 fname = tv_get_string_chk(&argvars[0]);
4018 mods = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004019 if (fname == NULL || mods == NULL)
4020 fname = NULL;
4021 else
4022 {
4023 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02004024 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004025 }
4026
4027 rettv->v_type = VAR_STRING;
4028 if (fname == NULL)
4029 rettv->vval.v_string = NULL;
4030 else
4031 rettv->vval.v_string = vim_strnsave(fname, len);
4032 vim_free(fbuf);
4033}
4034
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004035/*
4036 * "foldclosed()" function
4037 */
4038 static void
4039foldclosed_both(
4040 typval_T *argvars UNUSED,
4041 typval_T *rettv,
4042 int end UNUSED)
4043{
4044#ifdef FEAT_FOLDING
4045 linenr_T lnum;
4046 linenr_T first, last;
4047
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004048 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004049 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4050 {
4051 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
4052 {
4053 if (end)
4054 rettv->vval.v_number = (varnumber_T)last;
4055 else
4056 rettv->vval.v_number = (varnumber_T)first;
4057 return;
4058 }
4059 }
4060#endif
4061 rettv->vval.v_number = -1;
4062}
4063
4064/*
4065 * "foldclosed()" function
4066 */
4067 static void
4068f_foldclosed(typval_T *argvars, typval_T *rettv)
4069{
4070 foldclosed_both(argvars, rettv, FALSE);
4071}
4072
4073/*
4074 * "foldclosedend()" function
4075 */
4076 static void
4077f_foldclosedend(typval_T *argvars, typval_T *rettv)
4078{
4079 foldclosed_both(argvars, rettv, TRUE);
4080}
4081
4082/*
4083 * "foldlevel()" function
4084 */
4085 static void
4086f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4087{
4088#ifdef FEAT_FOLDING
4089 linenr_T lnum;
4090
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004091 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004092 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4093 rettv->vval.v_number = foldLevel(lnum);
4094#endif
4095}
4096
4097/*
4098 * "foldtext()" function
4099 */
4100 static void
4101f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
4102{
4103#ifdef FEAT_FOLDING
4104 linenr_T foldstart;
4105 linenr_T foldend;
4106 char_u *dashes;
4107 linenr_T lnum;
4108 char_u *s;
4109 char_u *r;
4110 int len;
4111 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004112 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004113#endif
4114
4115 rettv->v_type = VAR_STRING;
4116 rettv->vval.v_string = NULL;
4117#ifdef FEAT_FOLDING
4118 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
4119 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
4120 dashes = get_vim_var_str(VV_FOLDDASHES);
4121 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
4122 && dashes != NULL)
4123 {
4124 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02004125 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004126 if (!linewhite(lnum))
4127 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004128
4129 /* Find interesting text in this line. */
4130 s = skipwhite(ml_get(lnum));
4131 /* skip C comment-start */
4132 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
4133 {
4134 s = skipwhite(s + 2);
4135 if (*skipwhite(s) == NUL
4136 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
4137 {
4138 s = skipwhite(ml_get(lnum + 1));
4139 if (*s == '*')
4140 s = skipwhite(s + 1);
4141 }
4142 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02004143 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01004144 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004145 r = alloc((unsigned)(STRLEN(txt)
4146 + STRLEN(dashes) /* for %s */
4147 + 20 /* for %3ld */
4148 + STRLEN(s))); /* concatenated */
4149 if (r != NULL)
4150 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02004151 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004152 len = (int)STRLEN(r);
4153 STRCAT(r, s);
4154 /* remove 'foldmarker' and 'commentstring' */
4155 foldtext_cleanup(r + len);
4156 rettv->vval.v_string = r;
4157 }
4158 }
4159#endif
4160}
4161
4162/*
4163 * "foldtextresult(lnum)" function
4164 */
4165 static void
4166f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
4167{
4168#ifdef FEAT_FOLDING
4169 linenr_T lnum;
4170 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004171 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004172 foldinfo_T foldinfo;
4173 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004174 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004175#endif
4176
4177 rettv->v_type = VAR_STRING;
4178 rettv->vval.v_string = NULL;
4179#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004180 if (entered)
4181 return; /* reject recursive use */
4182 entered = TRUE;
4183
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004184 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004185 /* treat illegal types and illegal string values for {lnum} the same */
4186 if (lnum < 0)
4187 lnum = 0;
4188 fold_count = foldedCount(curwin, lnum, &foldinfo);
4189 if (fold_count > 0)
4190 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01004191 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
4192 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004193 if (text == buf)
4194 text = vim_strsave(text);
4195 rettv->vval.v_string = text;
4196 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004197
4198 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004199#endif
4200}
4201
4202/*
4203 * "foreground()" function
4204 */
4205 static void
4206f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4207{
4208#ifdef FEAT_GUI
4209 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004210 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004211 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004212 return;
4213 }
4214#endif
4215#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004216 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004217#endif
4218}
4219
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004220 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004221common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004222{
4223 char_u *s;
4224 char_u *name;
4225 int use_string = FALSE;
4226 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004227 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004228
4229 if (argvars[0].v_type == VAR_FUNC)
4230 {
4231 /* function(MyFunc, [arg], dict) */
4232 s = argvars[0].vval.v_string;
4233 }
4234 else if (argvars[0].v_type == VAR_PARTIAL
4235 && argvars[0].vval.v_partial != NULL)
4236 {
4237 /* function(dict.MyFunc, [arg]) */
4238 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004239 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004240 }
4241 else
4242 {
4243 /* function('MyFunc', [arg], dict) */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004244 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004245 use_string = TRUE;
4246 }
4247
Bram Moolenaar843b8842016-08-21 14:36:15 +02004248 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004249 {
4250 name = s;
4251 trans_name = trans_function_name(&name, FALSE,
4252 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4253 if (*name != NUL)
4254 s = NULL;
4255 }
4256
Bram Moolenaar843b8842016-08-21 14:36:15 +02004257 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4258 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004259 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004260 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004261 else if (trans_name != NULL && (is_funcref
4262 ? find_func(trans_name) == NULL
4263 : !translated_function_exists(trans_name)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004264 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004265 else
4266 {
4267 int dict_idx = 0;
4268 int arg_idx = 0;
4269 list_T *list = NULL;
4270
4271 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4272 {
4273 char sid_buf[25];
4274 int off = *s == 's' ? 2 : 5;
4275
4276 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4277 * also be called from another script. Using trans_function_name()
4278 * would also work, but some plugins depend on the name being
4279 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004280 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004281 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
4282 if (name != NULL)
4283 {
4284 STRCPY(name, sid_buf);
4285 STRCAT(name, s + off);
4286 }
4287 }
4288 else
4289 name = vim_strsave(s);
4290
4291 if (argvars[1].v_type != VAR_UNKNOWN)
4292 {
4293 if (argvars[2].v_type != VAR_UNKNOWN)
4294 {
4295 /* function(name, [args], dict) */
4296 arg_idx = 1;
4297 dict_idx = 2;
4298 }
4299 else if (argvars[1].v_type == VAR_DICT)
4300 /* function(name, dict) */
4301 dict_idx = 1;
4302 else
4303 /* function(name, [args]) */
4304 arg_idx = 1;
4305 if (dict_idx > 0)
4306 {
4307 if (argvars[dict_idx].v_type != VAR_DICT)
4308 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004309 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004310 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004311 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004312 }
4313 if (argvars[dict_idx].vval.v_dict == NULL)
4314 dict_idx = 0;
4315 }
4316 if (arg_idx > 0)
4317 {
4318 if (argvars[arg_idx].v_type != VAR_LIST)
4319 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004320 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004321 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004322 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004323 }
4324 list = argvars[arg_idx].vval.v_list;
4325 if (list == NULL || list->lv_len == 0)
4326 arg_idx = 0;
4327 }
4328 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004329 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004330 {
4331 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
4332
4333 /* result is a VAR_PARTIAL */
4334 if (pt == NULL)
4335 vim_free(name);
4336 else
4337 {
4338 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4339 {
4340 listitem_T *li;
4341 int i = 0;
4342 int arg_len = 0;
4343 int lv_len = 0;
4344
4345 if (arg_pt != NULL)
4346 arg_len = arg_pt->pt_argc;
4347 if (list != NULL)
4348 lv_len = list->lv_len;
4349 pt->pt_argc = arg_len + lv_len;
4350 pt->pt_argv = (typval_T *)alloc(
4351 sizeof(typval_T) * pt->pt_argc);
4352 if (pt->pt_argv == NULL)
4353 {
4354 vim_free(pt);
4355 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004356 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004357 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004358 for (i = 0; i < arg_len; i++)
4359 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4360 if (lv_len > 0)
4361 for (li = list->lv_first; li != NULL;
4362 li = li->li_next)
4363 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004364 }
4365
4366 /* For "function(dict.func, [], dict)" and "func" is a partial
4367 * use "dict". That is backwards compatible. */
4368 if (dict_idx > 0)
4369 {
4370 /* The dict is bound explicitly, pt_auto is FALSE. */
4371 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4372 ++pt->pt_dict->dv_refcount;
4373 }
4374 else if (arg_pt != NULL)
4375 {
4376 /* If the dict was bound automatically the result is also
4377 * bound automatically. */
4378 pt->pt_dict = arg_pt->pt_dict;
4379 pt->pt_auto = arg_pt->pt_auto;
4380 if (pt->pt_dict != NULL)
4381 ++pt->pt_dict->dv_refcount;
4382 }
4383
4384 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004385 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4386 {
4387 pt->pt_func = arg_pt->pt_func;
4388 func_ptr_ref(pt->pt_func);
4389 vim_free(name);
4390 }
4391 else if (is_funcref)
4392 {
4393 pt->pt_func = find_func(trans_name);
4394 func_ptr_ref(pt->pt_func);
4395 vim_free(name);
4396 }
4397 else
4398 {
4399 pt->pt_name = name;
4400 func_ref(name);
4401 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004402 }
4403 rettv->v_type = VAR_PARTIAL;
4404 rettv->vval.v_partial = pt;
4405 }
4406 else
4407 {
4408 /* result is a VAR_FUNC */
4409 rettv->v_type = VAR_FUNC;
4410 rettv->vval.v_string = name;
4411 func_ref(name);
4412 }
4413 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004414theend:
4415 vim_free(trans_name);
4416}
4417
4418/*
4419 * "funcref()" function
4420 */
4421 static void
4422f_funcref(typval_T *argvars, typval_T *rettv)
4423{
4424 common_function(argvars, rettv, TRUE);
4425}
4426
4427/*
4428 * "function()" function
4429 */
4430 static void
4431f_function(typval_T *argvars, typval_T *rettv)
4432{
4433 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004434}
4435
4436/*
4437 * "garbagecollect()" function
4438 */
4439 static void
4440f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4441{
4442 /* This is postponed until we are back at the toplevel, because we may be
4443 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4444 want_garbage_collect = TRUE;
4445
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004446 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004447 garbage_collect_at_exit = TRUE;
4448}
4449
4450/*
4451 * "get()" function
4452 */
4453 static void
4454f_get(typval_T *argvars, typval_T *rettv)
4455{
4456 listitem_T *li;
4457 list_T *l;
4458 dictitem_T *di;
4459 dict_T *d;
4460 typval_T *tv = NULL;
4461
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004462 if (argvars[0].v_type == VAR_BLOB)
4463 {
4464 int error = FALSE;
4465 int idx = tv_get_number_chk(&argvars[1], &error);
4466
4467 if (!error)
4468 {
4469 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004470 if (idx < 0)
4471 idx = blob_len(argvars[0].vval.v_blob) + idx;
4472 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
4473 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004474 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004475 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004476 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004477 tv = rettv;
4478 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004479 }
4480 }
4481 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004482 {
4483 if ((l = argvars[0].vval.v_list) != NULL)
4484 {
4485 int error = FALSE;
4486
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004487 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004488 if (!error && li != NULL)
4489 tv = &li->li_tv;
4490 }
4491 }
4492 else if (argvars[0].v_type == VAR_DICT)
4493 {
4494 if ((d = argvars[0].vval.v_dict) != NULL)
4495 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004496 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004497 if (di != NULL)
4498 tv = &di->di_tv;
4499 }
4500 }
4501 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4502 {
4503 partial_T *pt;
4504 partial_T fref_pt;
4505
4506 if (argvars[0].v_type == VAR_PARTIAL)
4507 pt = argvars[0].vval.v_partial;
4508 else
4509 {
4510 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4511 fref_pt.pt_name = argvars[0].vval.v_string;
4512 pt = &fref_pt;
4513 }
4514
4515 if (pt != NULL)
4516 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004517 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004518 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004519
4520 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4521 {
4522 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004523 n = partial_name(pt);
4524 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004525 rettv->vval.v_string = NULL;
4526 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004527 {
4528 rettv->vval.v_string = vim_strsave(n);
4529 if (rettv->v_type == VAR_FUNC)
4530 func_ref(rettv->vval.v_string);
4531 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004532 }
4533 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004534 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004535 else if (STRCMP(what, "args") == 0)
4536 {
4537 rettv->v_type = VAR_LIST;
4538 if (rettv_list_alloc(rettv) == OK)
4539 {
4540 int i;
4541
4542 for (i = 0; i < pt->pt_argc; ++i)
4543 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4544 }
4545 }
4546 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004547 semsg(_(e_invarg2), what);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004548 return;
4549 }
4550 }
4551 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004552 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004553
4554 if (tv == NULL)
4555 {
4556 if (argvars[2].v_type != VAR_UNKNOWN)
4557 copy_tv(&argvars[2], rettv);
4558 }
4559 else
4560 copy_tv(tv, rettv);
4561}
4562
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004563/*
4564 * Returns buffer options, variables and other attributes in a dictionary.
4565 */
4566 static dict_T *
4567get_buffer_info(buf_T *buf)
4568{
4569 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004570 tabpage_T *tp;
4571 win_T *wp;
4572 list_T *windows;
4573
4574 dict = dict_alloc();
4575 if (dict == NULL)
4576 return NULL;
4577
Bram Moolenaare0be1672018-07-08 16:50:37 +02004578 dict_add_number(dict, "bufnr", buf->b_fnum);
4579 dict_add_string(dict, "name", buf->b_ffname);
4580 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4581 : buflist_findlnum(buf));
4582 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4583 dict_add_number(dict, "listed", buf->b_p_bl);
4584 dict_add_number(dict, "changed", bufIsChanged(buf));
4585 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4586 dict_add_number(dict, "hidden",
4587 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004588
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004589 /* Get a reference to buffer variables */
4590 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004591
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004592 /* List of windows displaying this buffer */
4593 windows = list_alloc();
4594 if (windows != NULL)
4595 {
4596 FOR_ALL_TAB_WINDOWS(tp, wp)
4597 if (wp->w_buffer == buf)
4598 list_append_number(windows, (varnumber_T)wp->w_id);
4599 dict_add_list(dict, "windows", windows);
4600 }
4601
4602#ifdef FEAT_SIGNS
4603 if (buf->b_signlist != NULL)
4604 {
4605 /* List of signs placed in this buffer */
4606 list_T *signs = list_alloc();
4607 if (signs != NULL)
4608 {
4609 get_buffer_signs(buf, signs);
4610 dict_add_list(dict, "signs", signs);
4611 }
4612 }
4613#endif
4614
4615 return dict;
4616}
4617
4618/*
4619 * "getbufinfo()" function
4620 */
4621 static void
4622f_getbufinfo(typval_T *argvars, typval_T *rettv)
4623{
4624 buf_T *buf = NULL;
4625 buf_T *argbuf = NULL;
4626 dict_T *d;
4627 int filtered = FALSE;
4628 int sel_buflisted = FALSE;
4629 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004630 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004631
4632 if (rettv_list_alloc(rettv) != OK)
4633 return;
4634
4635 /* List of all the buffers or selected buffers */
4636 if (argvars[0].v_type == VAR_DICT)
4637 {
4638 dict_T *sel_d = argvars[0].vval.v_dict;
4639
4640 if (sel_d != NULL)
4641 {
4642 dictitem_T *di;
4643
4644 filtered = TRUE;
4645
4646 di = dict_find(sel_d, (char_u *)"buflisted", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004647 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004648 sel_buflisted = TRUE;
4649
4650 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004651 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004652 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004653
4654 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004655 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004656 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004657 }
4658 }
4659 else if (argvars[0].v_type != VAR_UNKNOWN)
4660 {
4661 /* Information about one buffer. Argument specifies the buffer */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004662 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004663 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004664 argbuf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004665 --emsg_off;
4666 if (argbuf == NULL)
4667 return;
4668 }
4669
4670 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004671 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004672 {
4673 if (argbuf != NULL && argbuf != buf)
4674 continue;
4675 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004676 || (sel_buflisted && !buf->b_p_bl)
4677 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004678 continue;
4679
4680 d = get_buffer_info(buf);
4681 if (d != NULL)
4682 list_append_dict(rettv->vval.v_list, d);
4683 if (argbuf != NULL)
4684 return;
4685 }
4686}
4687
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004688/*
4689 * Get line or list of lines from buffer "buf" into "rettv".
4690 * Return a range (from start to end) of lines in rettv from the specified
4691 * buffer.
4692 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4693 */
4694 static void
4695get_buffer_lines(
4696 buf_T *buf,
4697 linenr_T start,
4698 linenr_T end,
4699 int retlist,
4700 typval_T *rettv)
4701{
4702 char_u *p;
4703
4704 rettv->v_type = VAR_STRING;
4705 rettv->vval.v_string = NULL;
4706 if (retlist && rettv_list_alloc(rettv) == FAIL)
4707 return;
4708
4709 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4710 return;
4711
4712 if (!retlist)
4713 {
4714 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4715 p = ml_get_buf(buf, start, FALSE);
4716 else
4717 p = (char_u *)"";
4718 rettv->vval.v_string = vim_strsave(p);
4719 }
4720 else
4721 {
4722 if (end < start)
4723 return;
4724
4725 if (start < 1)
4726 start = 1;
4727 if (end > buf->b_ml.ml_line_count)
4728 end = buf->b_ml.ml_line_count;
4729 while (start <= end)
4730 if (list_append_string(rettv->vval.v_list,
4731 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4732 break;
4733 }
4734}
4735
4736/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004737 * "getbufline()" function
4738 */
4739 static void
4740f_getbufline(typval_T *argvars, typval_T *rettv)
4741{
4742 linenr_T lnum;
4743 linenr_T end;
4744 buf_T *buf;
4745
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004746 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004747 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004748 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004749 --emsg_off;
4750
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004751 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004752 if (argvars[2].v_type == VAR_UNKNOWN)
4753 end = lnum;
4754 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004755 end = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004756
4757 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4758}
4759
4760/*
4761 * "getbufvar()" function
4762 */
4763 static void
4764f_getbufvar(typval_T *argvars, typval_T *rettv)
4765{
4766 buf_T *buf;
4767 buf_T *save_curbuf;
4768 char_u *varname;
4769 dictitem_T *v;
4770 int done = FALSE;
4771
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004772 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
4773 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004774 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004775 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004776
4777 rettv->v_type = VAR_STRING;
4778 rettv->vval.v_string = NULL;
4779
4780 if (buf != NULL && varname != NULL)
4781 {
4782 /* set curbuf to be our buf, temporarily */
4783 save_curbuf = curbuf;
4784 curbuf = buf;
4785
Bram Moolenaar30567352016-08-27 21:25:44 +02004786 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004787 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004788 if (varname[1] == NUL)
4789 {
4790 /* get all buffer-local options in a dict */
4791 dict_T *opts = get_winbuf_options(TRUE);
4792
4793 if (opts != NULL)
4794 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004795 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004796 done = TRUE;
4797 }
4798 }
4799 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4800 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004801 done = TRUE;
4802 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004803 else
4804 {
4805 /* Look up the variable. */
4806 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4807 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4808 'b', varname, FALSE);
4809 if (v != NULL)
4810 {
4811 copy_tv(&v->di_tv, rettv);
4812 done = TRUE;
4813 }
4814 }
4815
4816 /* restore previous notion of curbuf */
4817 curbuf = save_curbuf;
4818 }
4819
4820 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4821 /* use the default value */
4822 copy_tv(&argvars[2], rettv);
4823
4824 --emsg_off;
4825}
4826
4827/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004828 * "getchangelist()" function
4829 */
4830 static void
4831f_getchangelist(typval_T *argvars, typval_T *rettv)
4832{
4833#ifdef FEAT_JUMPLIST
4834 buf_T *buf;
4835 int i;
4836 list_T *l;
4837 dict_T *d;
4838#endif
4839
4840 if (rettv_list_alloc(rettv) != OK)
4841 return;
4842
4843#ifdef FEAT_JUMPLIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004844 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004845 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004846 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004847 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004848 if (buf == NULL)
4849 return;
4850
4851 l = list_alloc();
4852 if (l == NULL)
4853 return;
4854
4855 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4856 return;
4857 /*
4858 * The current window change list index tracks only the position in the
4859 * current buffer change list. For other buffers, use the change list
4860 * length as the current index.
4861 */
4862 list_append_number(rettv->vval.v_list,
4863 (varnumber_T)((buf == curwin->w_buffer)
4864 ? curwin->w_changelistidx : buf->b_changelistlen));
4865
4866 for (i = 0; i < buf->b_changelistlen; ++i)
4867 {
4868 if (buf->b_changelist[i].lnum == 0)
4869 continue;
4870 if ((d = dict_alloc()) == NULL)
4871 return;
4872 if (list_append_dict(l, d) == FAIL)
4873 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004874 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4875 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004876 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004877 }
4878#endif
4879}
4880/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004881 * "getchar()" function
4882 */
4883 static void
4884f_getchar(typval_T *argvars, typval_T *rettv)
4885{
4886 varnumber_T n;
4887 int error = FALSE;
4888
Bram Moolenaar84d93902018-09-11 20:10:20 +02004889#ifdef MESSAGE_QUEUE
4890 // vpeekc() used to check for messages, but that caused problems, invoking
4891 // a callback where it was not expected. Some plugins use getchar(1) in a
4892 // loop to await a message, therefore make sure we check for messages here.
4893 parse_queued_messages();
4894#endif
4895
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004896 /* Position the cursor. Needed after a message that ends in a space. */
4897 windgoto(msg_row, msg_col);
4898
4899 ++no_mapping;
4900 ++allow_keys;
4901 for (;;)
4902 {
4903 if (argvars[0].v_type == VAR_UNKNOWN)
4904 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004905 n = plain_vgetc();
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004906 else if (tv_get_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004907 /* getchar(1): only check if char avail */
4908 n = vpeekc_any();
4909 else if (error || vpeekc_any() == NUL)
4910 /* illegal argument or getchar(0) and no char avail: return zero */
4911 n = 0;
4912 else
4913 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004914 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004915
4916 if (n == K_IGNORE)
4917 continue;
4918 break;
4919 }
4920 --no_mapping;
4921 --allow_keys;
4922
4923 set_vim_var_nr(VV_MOUSE_WIN, 0);
4924 set_vim_var_nr(VV_MOUSE_WINID, 0);
4925 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4926 set_vim_var_nr(VV_MOUSE_COL, 0);
4927
4928 rettv->vval.v_number = n;
4929 if (IS_SPECIAL(n) || mod_mask != 0)
4930 {
4931 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4932 int i = 0;
4933
4934 /* Turn a special key into three bytes, plus modifier. */
4935 if (mod_mask != 0)
4936 {
4937 temp[i++] = K_SPECIAL;
4938 temp[i++] = KS_MODIFIER;
4939 temp[i++] = mod_mask;
4940 }
4941 if (IS_SPECIAL(n))
4942 {
4943 temp[i++] = K_SPECIAL;
4944 temp[i++] = K_SECOND(n);
4945 temp[i++] = K_THIRD(n);
4946 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004947 else if (has_mbyte)
4948 i += (*mb_char2bytes)(n, temp + i);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004949 else
4950 temp[i++] = n;
4951 temp[i++] = NUL;
4952 rettv->v_type = VAR_STRING;
4953 rettv->vval.v_string = vim_strsave(temp);
4954
4955#ifdef FEAT_MOUSE
4956 if (is_mouse_key(n))
4957 {
4958 int row = mouse_row;
4959 int col = mouse_col;
4960 win_T *win;
4961 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004962 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004963 int winnr = 1;
4964
4965 if (row >= 0 && col >= 0)
4966 {
4967 /* Find the window at the mouse coordinates and compute the
4968 * text position. */
4969 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004970 if (win == NULL)
4971 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004972 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004973 for (wp = firstwin; wp != win; wp = wp->w_next)
4974 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004975 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4976 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4977 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4978 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4979 }
4980 }
4981#endif
4982 }
4983}
4984
4985/*
4986 * "getcharmod()" function
4987 */
4988 static void
4989f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4990{
4991 rettv->vval.v_number = mod_mask;
4992}
4993
4994/*
4995 * "getcharsearch()" function
4996 */
4997 static void
4998f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4999{
5000 if (rettv_dict_alloc(rettv) != FAIL)
5001 {
5002 dict_T *dict = rettv->vval.v_dict;
5003
Bram Moolenaare0be1672018-07-08 16:50:37 +02005004 dict_add_string(dict, "char", last_csearch());
5005 dict_add_number(dict, "forward", last_csearch_forward());
5006 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005007 }
5008}
5009
5010/*
5011 * "getcmdline()" function
5012 */
5013 static void
5014f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
5015{
5016 rettv->v_type = VAR_STRING;
5017 rettv->vval.v_string = get_cmdline_str();
5018}
5019
5020/*
5021 * "getcmdpos()" function
5022 */
5023 static void
5024f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
5025{
5026 rettv->vval.v_number = get_cmdline_pos() + 1;
5027}
5028
5029/*
5030 * "getcmdtype()" function
5031 */
5032 static void
5033f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
5034{
5035 rettv->v_type = VAR_STRING;
5036 rettv->vval.v_string = alloc(2);
5037 if (rettv->vval.v_string != NULL)
5038 {
5039 rettv->vval.v_string[0] = get_cmdline_type();
5040 rettv->vval.v_string[1] = NUL;
5041 }
5042}
5043
5044/*
5045 * "getcmdwintype()" function
5046 */
5047 static void
5048f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
5049{
5050 rettv->v_type = VAR_STRING;
5051 rettv->vval.v_string = NULL;
5052#ifdef FEAT_CMDWIN
5053 rettv->vval.v_string = alloc(2);
5054 if (rettv->vval.v_string != NULL)
5055 {
5056 rettv->vval.v_string[0] = cmdwin_type;
5057 rettv->vval.v_string[1] = NUL;
5058 }
5059#endif
5060}
5061
5062#if defined(FEAT_CMDL_COMPL)
5063/*
5064 * "getcompletion()" function
5065 */
5066 static void
5067f_getcompletion(typval_T *argvars, typval_T *rettv)
5068{
5069 char_u *pat;
5070 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005071 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005072 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
5073 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005074
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005075 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005076 filtered = tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005077
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005078 if (p_wic)
5079 options |= WILD_ICASE;
5080
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005081 /* For filtered results, 'wildignore' is used */
5082 if (!filtered)
5083 options |= WILD_KEEP_ALL;
5084
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005085 ExpandInit(&xpc);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005086 xpc.xp_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005087 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005088 xpc.xp_context = cmdcomplete_str_to_type(tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005089 if (xpc.xp_context == EXPAND_NOTHING)
5090 {
5091 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005092 semsg(_(e_invarg2), argvars[1].vval.v_string);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005093 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005094 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005095 return;
5096 }
5097
5098# if defined(FEAT_MENU)
5099 if (xpc.xp_context == EXPAND_MENUS)
5100 {
5101 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
5102 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5103 }
5104# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02005105#ifdef FEAT_CSCOPE
5106 if (xpc.xp_context == EXPAND_CSCOPE)
5107 {
5108 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
5109 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5110 }
5111#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02005112#ifdef FEAT_SIGNS
5113 if (xpc.xp_context == EXPAND_SIGN)
5114 {
5115 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
5116 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5117 }
5118#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005119
5120 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
5121 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
5122 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005123 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005124
5125 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
5126
5127 for (i = 0; i < xpc.xp_numfiles; i++)
5128 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5129 }
5130 vim_free(pat);
5131 ExpandCleanup(&xpc);
5132}
5133#endif
5134
5135/*
5136 * "getcwd()" function
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005137 *
5138 * Return the current working directory of a window in a tab page.
5139 * First optional argument 'winnr' is the window number or -1 and the second
5140 * optional argument 'tabnr' is the tab page number.
5141 *
5142 * If no arguments are supplied, then return the directory of the current
5143 * window.
5144 * If only 'winnr' is specified and is not -1 or 0 then return the directory of
5145 * the specified window.
5146 * If 'winnr' is 0 then return the directory of the current window.
5147 * If both 'winnr and 'tabnr' are specified and 'winnr' is -1 then return the
5148 * directory of the specified tab page. Otherwise return the directory of the
5149 * specified window in the specified tab page.
5150 * If the window or the tab page doesn't exist then return NULL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005151 */
5152 static void
5153f_getcwd(typval_T *argvars, typval_T *rettv)
5154{
5155 win_T *wp = NULL;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005156 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005157 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01005158 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005159
5160 rettv->v_type = VAR_STRING;
5161 rettv->vval.v_string = NULL;
5162
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005163 if (argvars[0].v_type == VAR_NUMBER
5164 && argvars[0].vval.v_number == -1
5165 && argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar54591292018-02-09 20:53:59 +01005166 global = TRUE;
5167 else
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005168 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
Bram Moolenaar54591292018-02-09 20:53:59 +01005169
5170 if (wp != NULL && wp->w_localdir != NULL)
5171 rettv->vval.v_string = vim_strsave(wp->w_localdir);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005172 else if (tp != NULL && tp->tp_localdir != NULL)
5173 rettv->vval.v_string = vim_strsave(tp->tp_localdir);
5174 else if (wp != NULL || tp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005175 {
Bram Moolenaar54591292018-02-09 20:53:59 +01005176 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005177 rettv->vval.v_string = vim_strsave(globaldir);
5178 else
5179 {
5180 cwd = alloc(MAXPATHL);
5181 if (cwd != NULL)
5182 {
5183 if (mch_dirname(cwd, MAXPATHL) != FAIL)
5184 rettv->vval.v_string = vim_strsave(cwd);
5185 vim_free(cwd);
5186 }
5187 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005188 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02005189#ifdef BACKSLASH_IN_FILENAME
5190 if (rettv->vval.v_string != NULL)
5191 slash_adjust(rettv->vval.v_string);
5192#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005193}
5194
5195/*
5196 * "getfontname()" function
5197 */
5198 static void
5199f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
5200{
5201 rettv->v_type = VAR_STRING;
5202 rettv->vval.v_string = NULL;
5203#ifdef FEAT_GUI
5204 if (gui.in_use)
5205 {
5206 GuiFont font;
5207 char_u *name = NULL;
5208
5209 if (argvars[0].v_type == VAR_UNKNOWN)
5210 {
5211 /* Get the "Normal" font. Either the name saved by
5212 * hl_set_font_name() or from the font ID. */
5213 font = gui.norm_font;
5214 name = hl_get_font_name();
5215 }
5216 else
5217 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005218 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005219 if (STRCMP(name, "*") == 0) /* don't use font dialog */
5220 return;
5221 font = gui_mch_get_font(name, FALSE);
5222 if (font == NOFONT)
5223 return; /* Invalid font name, return empty string. */
5224 }
5225 rettv->vval.v_string = gui_mch_get_fontname(font, name);
5226 if (argvars[0].v_type != VAR_UNKNOWN)
5227 gui_mch_free_font(font);
5228 }
5229#endif
5230}
5231
5232/*
5233 * "getfperm({fname})" function
5234 */
5235 static void
5236f_getfperm(typval_T *argvars, typval_T *rettv)
5237{
5238 char_u *fname;
5239 stat_T st;
5240 char_u *perm = NULL;
5241 char_u flags[] = "rwx";
5242 int i;
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_STRING;
5247 if (mch_stat((char *)fname, &st) >= 0)
5248 {
5249 perm = vim_strsave((char_u *)"---------");
5250 if (perm != NULL)
5251 {
5252 for (i = 0; i < 9; i++)
5253 {
5254 if (st.st_mode & (1 << (8 - i)))
5255 perm[i] = flags[i % 3];
5256 }
5257 }
5258 }
5259 rettv->vval.v_string = perm;
5260}
5261
5262/*
5263 * "getfsize({fname})" function
5264 */
5265 static void
5266f_getfsize(typval_T *argvars, typval_T *rettv)
5267{
5268 char_u *fname;
5269 stat_T st;
5270
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005271 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005272
5273 rettv->v_type = VAR_NUMBER;
5274
5275 if (mch_stat((char *)fname, &st) >= 0)
5276 {
5277 if (mch_isdir(fname))
5278 rettv->vval.v_number = 0;
5279 else
5280 {
5281 rettv->vval.v_number = (varnumber_T)st.st_size;
5282
5283 /* non-perfect check for overflow */
5284 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5285 rettv->vval.v_number = -2;
5286 }
5287 }
5288 else
5289 rettv->vval.v_number = -1;
5290}
5291
5292/*
5293 * "getftime({fname})" function
5294 */
5295 static void
5296f_getftime(typval_T *argvars, typval_T *rettv)
5297{
5298 char_u *fname;
5299 stat_T st;
5300
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005301 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005302
5303 if (mch_stat((char *)fname, &st) >= 0)
5304 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5305 else
5306 rettv->vval.v_number = -1;
5307}
5308
5309/*
5310 * "getftype({fname})" function
5311 */
5312 static void
5313f_getftype(typval_T *argvars, typval_T *rettv)
5314{
5315 char_u *fname;
5316 stat_T st;
5317 char_u *type = NULL;
5318 char *t;
5319
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005320 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005321
5322 rettv->v_type = VAR_STRING;
5323 if (mch_lstat((char *)fname, &st) >= 0)
5324 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005325 if (S_ISREG(st.st_mode))
5326 t = "file";
5327 else if (S_ISDIR(st.st_mode))
5328 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005329 else if (S_ISLNK(st.st_mode))
5330 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005331 else if (S_ISBLK(st.st_mode))
5332 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005333 else if (S_ISCHR(st.st_mode))
5334 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005335 else if (S_ISFIFO(st.st_mode))
5336 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005337 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005338 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005339 else
5340 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005341 type = vim_strsave((char_u *)t);
5342 }
5343 rettv->vval.v_string = type;
5344}
5345
5346/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005347 * "getjumplist()" function
5348 */
5349 static void
5350f_getjumplist(typval_T *argvars, typval_T *rettv)
5351{
5352#ifdef FEAT_JUMPLIST
5353 win_T *wp;
5354 int i;
5355 list_T *l;
5356 dict_T *d;
5357#endif
5358
5359 if (rettv_list_alloc(rettv) != OK)
5360 return;
5361
5362#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005363 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005364 if (wp == NULL)
5365 return;
5366
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01005367 cleanup_jumplist(wp, TRUE);
5368
Bram Moolenaar4f505882018-02-10 21:06:32 +01005369 l = list_alloc();
5370 if (l == NULL)
5371 return;
5372
5373 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5374 return;
5375 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5376
5377 for (i = 0; i < wp->w_jumplistlen; ++i)
5378 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005379 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5380 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005381 if ((d = dict_alloc()) == NULL)
5382 return;
5383 if (list_append_dict(l, d) == FAIL)
5384 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005385 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5386 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005387 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005388 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005389 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005390 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005391 }
5392#endif
5393}
5394
5395/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005396 * "getline(lnum, [end])" function
5397 */
5398 static void
5399f_getline(typval_T *argvars, typval_T *rettv)
5400{
5401 linenr_T lnum;
5402 linenr_T end;
5403 int retlist;
5404
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005405 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005406 if (argvars[1].v_type == VAR_UNKNOWN)
5407 {
5408 end = 0;
5409 retlist = FALSE;
5410 }
5411 else
5412 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005413 end = tv_get_lnum(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005414 retlist = TRUE;
5415 }
5416
5417 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5418}
5419
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005420#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005421 static void
5422get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5423{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005424 if (what_arg->v_type == VAR_UNKNOWN)
5425 {
5426 if (rettv_list_alloc(rettv) == OK)
5427 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005428 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005429 }
5430 else
5431 {
5432 if (rettv_dict_alloc(rettv) == OK)
5433 if (is_qf || (wp != NULL))
5434 {
5435 if (what_arg->v_type == VAR_DICT)
5436 {
5437 dict_T *d = what_arg->vval.v_dict;
5438
5439 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005440 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005441 }
5442 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005443 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +02005444 }
5445 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005446}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005447#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005448
5449/*
5450 * "getloclist()" function
5451 */
5452 static void
5453f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5454{
5455#ifdef FEAT_QUICKFIX
5456 win_T *wp;
5457
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005458 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005459 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5460#endif
5461}
5462
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005463/*
5464 * "getmatches()" function
5465 */
5466 static void
5467f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5468{
5469#ifdef FEAT_SEARCH_EXTRA
5470 dict_T *dict;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005471 matchitem_T *cur;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005472 int i;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005473 win_T *win = get_optional_window(argvars, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005474
Bram Moolenaaraff74912019-03-30 18:11:49 +01005475 if (rettv_list_alloc(rettv) == FAIL || win == NULL)
5476 return;
5477
5478 cur = win->w_match_head;
5479 while (cur != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005480 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005481 dict = dict_alloc();
5482 if (dict == NULL)
5483 return;
5484 if (cur->match.regprog == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005485 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005486 /* match added with matchaddpos() */
5487 for (i = 0; i < MAXPOSMATCH; ++i)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005488 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005489 llpos_T *llpos;
Bram Moolenaar54315892019-04-26 22:33:49 +02005490 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaaraff74912019-03-30 18:11:49 +01005491 list_T *l;
5492
5493 llpos = &cur->pos.pos[i];
5494 if (llpos->lnum == 0)
5495 break;
5496 l = list_alloc();
5497 if (l == NULL)
5498 break;
5499 list_append_number(l, (varnumber_T)llpos->lnum);
5500 if (llpos->col > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005501 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005502 list_append_number(l, (varnumber_T)llpos->col);
5503 list_append_number(l, (varnumber_T)llpos->len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005504 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005505 sprintf(buf, "pos%d", i + 1);
5506 dict_add_list(dict, buf, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005507 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005508 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005509 else
5510 {
5511 dict_add_string(dict, "pattern", cur->pattern);
5512 }
5513 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
5514 dict_add_number(dict, "priority", (long)cur->priority);
5515 dict_add_number(dict, "id", (long)cur->id);
5516# if defined(FEAT_CONCEAL)
5517 if (cur->conceal_char)
5518 {
5519 char_u buf[MB_MAXBYTES + 1];
5520
5521 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
5522 dict_add_string(dict, "conceal", (char_u *)&buf);
5523 }
5524# endif
5525 list_append_dict(rettv->vval.v_list, dict);
5526 cur = cur->next;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005527 }
5528#endif
5529}
5530
5531/*
5532 * "getpid()" function
5533 */
5534 static void
5535f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5536{
5537 rettv->vval.v_number = mch_get_pid();
5538}
5539
5540 static void
5541getpos_both(
5542 typval_T *argvars,
5543 typval_T *rettv,
5544 int getcurpos)
5545{
5546 pos_T *fp;
5547 list_T *l;
5548 int fnum = -1;
5549
5550 if (rettv_list_alloc(rettv) == OK)
5551 {
5552 l = rettv->vval.v_list;
5553 if (getcurpos)
5554 fp = &curwin->w_cursor;
5555 else
5556 fp = var2fpos(&argvars[0], TRUE, &fnum);
5557 if (fnum != -1)
5558 list_append_number(l, (varnumber_T)fnum);
5559 else
5560 list_append_number(l, (varnumber_T)0);
5561 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5562 : (varnumber_T)0);
5563 list_append_number(l, (fp != NULL)
5564 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5565 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005566 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005567 (varnumber_T)0);
5568 if (getcurpos)
5569 {
Bram Moolenaar19a66852019-03-07 11:25:32 +01005570 int save_set_curswant = curwin->w_set_curswant;
5571 colnr_T save_curswant = curwin->w_curswant;
5572 colnr_T save_virtcol = curwin->w_virtcol;
5573
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005574 update_curswant();
5575 list_append_number(l, curwin->w_curswant == MAXCOL ?
5576 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar19a66852019-03-07 11:25:32 +01005577
5578 // Do not change "curswant", as it is unexpected that a get
5579 // function has a side effect.
5580 if (save_set_curswant)
5581 {
5582 curwin->w_set_curswant = save_set_curswant;
5583 curwin->w_curswant = save_curswant;
5584 curwin->w_virtcol = save_virtcol;
5585 curwin->w_valid &= ~VALID_VIRTCOL;
5586 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005587 }
5588 }
5589 else
5590 rettv->vval.v_number = FALSE;
5591}
5592
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005593/*
5594 * "getcurpos()" function
5595 */
5596 static void
5597f_getcurpos(typval_T *argvars, typval_T *rettv)
5598{
5599 getpos_both(argvars, rettv, TRUE);
5600}
5601
5602/*
5603 * "getpos(string)" function
5604 */
5605 static void
5606f_getpos(typval_T *argvars, typval_T *rettv)
5607{
5608 getpos_both(argvars, rettv, FALSE);
5609}
5610
5611/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005612 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005613 */
5614 static void
5615f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5616{
5617#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005618 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005619#endif
5620}
5621
5622/*
5623 * "getreg()" function
5624 */
5625 static void
5626f_getreg(typval_T *argvars, typval_T *rettv)
5627{
5628 char_u *strregname;
5629 int regname;
5630 int arg2 = FALSE;
5631 int return_list = FALSE;
5632 int error = FALSE;
5633
5634 if (argvars[0].v_type != VAR_UNKNOWN)
5635 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005636 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005637 error = strregname == NULL;
5638 if (argvars[1].v_type != VAR_UNKNOWN)
5639 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005640 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005641 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005642 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005643 }
5644 }
5645 else
5646 strregname = get_vim_var_str(VV_REG);
5647
5648 if (error)
5649 return;
5650
5651 regname = (strregname == NULL ? '"' : *strregname);
5652 if (regname == 0)
5653 regname = '"';
5654
5655 if (return_list)
5656 {
5657 rettv->v_type = VAR_LIST;
5658 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5659 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5660 if (rettv->vval.v_list == NULL)
5661 (void)rettv_list_alloc(rettv);
5662 else
5663 ++rettv->vval.v_list->lv_refcount;
5664 }
5665 else
5666 {
5667 rettv->v_type = VAR_STRING;
5668 rettv->vval.v_string = get_reg_contents(regname,
5669 arg2 ? GREG_EXPR_SRC : 0);
5670 }
5671}
5672
5673/*
5674 * "getregtype()" function
5675 */
5676 static void
5677f_getregtype(typval_T *argvars, typval_T *rettv)
5678{
5679 char_u *strregname;
5680 int regname;
5681 char_u buf[NUMBUFLEN + 2];
5682 long reglen = 0;
5683
5684 if (argvars[0].v_type != VAR_UNKNOWN)
5685 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005686 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005687 if (strregname == NULL) /* type error; errmsg already given */
5688 {
5689 rettv->v_type = VAR_STRING;
5690 rettv->vval.v_string = NULL;
5691 return;
5692 }
5693 }
5694 else
5695 /* Default to v:register */
5696 strregname = get_vim_var_str(VV_REG);
5697
5698 regname = (strregname == NULL ? '"' : *strregname);
5699 if (regname == 0)
5700 regname = '"';
5701
5702 buf[0] = NUL;
5703 buf[1] = NUL;
5704 switch (get_reg_type(regname, &reglen))
5705 {
5706 case MLINE: buf[0] = 'V'; break;
5707 case MCHAR: buf[0] = 'v'; break;
5708 case MBLOCK:
5709 buf[0] = Ctrl_V;
5710 sprintf((char *)buf + 1, "%ld", reglen + 1);
5711 break;
5712 }
5713 rettv->v_type = VAR_STRING;
5714 rettv->vval.v_string = vim_strsave(buf);
5715}
5716
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005717/*
5718 * Returns information (variables, options, etc.) about a tab page
5719 * as a dictionary.
5720 */
5721 static dict_T *
5722get_tabpage_info(tabpage_T *tp, int tp_idx)
5723{
5724 win_T *wp;
5725 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005726 list_T *l;
5727
5728 dict = dict_alloc();
5729 if (dict == NULL)
5730 return NULL;
5731
Bram Moolenaare0be1672018-07-08 16:50:37 +02005732 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005733
5734 l = list_alloc();
5735 if (l != NULL)
5736 {
5737 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5738 wp; wp = wp->w_next)
5739 list_append_number(l, (varnumber_T)wp->w_id);
5740 dict_add_list(dict, "windows", l);
5741 }
5742
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005743 /* Make a reference to tabpage variables */
5744 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005745
5746 return dict;
5747}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005748
5749/*
5750 * "gettabinfo()" function
5751 */
5752 static void
5753f_gettabinfo(typval_T *argvars, typval_T *rettv)
5754{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005755 tabpage_T *tp, *tparg = NULL;
5756 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005757 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005758
5759 if (rettv_list_alloc(rettv) != OK)
5760 return;
5761
5762 if (argvars[0].v_type != VAR_UNKNOWN)
5763 {
5764 /* Information about one tab page */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005765 tparg = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005766 if (tparg == NULL)
5767 return;
5768 }
5769
5770 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005771 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005772 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005773 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005774 if (tparg != NULL && tp != tparg)
5775 continue;
5776 d = get_tabpage_info(tp, tpnr);
5777 if (d != NULL)
5778 list_append_dict(rettv->vval.v_list, d);
5779 if (tparg != NULL)
5780 return;
5781 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005782}
5783
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005784/*
5785 * "gettabvar()" function
5786 */
5787 static void
5788f_gettabvar(typval_T *argvars, typval_T *rettv)
5789{
5790 win_T *oldcurwin;
5791 tabpage_T *tp, *oldtabpage;
5792 dictitem_T *v;
5793 char_u *varname;
5794 int done = FALSE;
5795
5796 rettv->v_type = VAR_STRING;
5797 rettv->vval.v_string = NULL;
5798
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005799 varname = tv_get_string_chk(&argvars[1]);
5800 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005801 if (tp != NULL && varname != NULL)
5802 {
5803 /* Set tp to be our tabpage, temporarily. Also set the window to the
5804 * first window in the tabpage, otherwise the window is not valid. */
5805 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005806 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5807 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005808 {
5809 /* look up the variable */
5810 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5811 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5812 if (v != NULL)
5813 {
5814 copy_tv(&v->di_tv, rettv);
5815 done = TRUE;
5816 }
5817 }
5818
5819 /* restore previous notion of curwin */
5820 restore_win(oldcurwin, oldtabpage, TRUE);
5821 }
5822
5823 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5824 copy_tv(&argvars[2], rettv);
5825}
5826
5827/*
5828 * "gettabwinvar()" function
5829 */
5830 static void
5831f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5832{
5833 getwinvar(argvars, rettv, 1);
5834}
5835
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005836/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01005837 * "gettagstack()" function
5838 */
5839 static void
5840f_gettagstack(typval_T *argvars, typval_T *rettv)
5841{
5842 win_T *wp = curwin; // default is current window
5843
5844 if (rettv_dict_alloc(rettv) != OK)
5845 return;
5846
5847 if (argvars[0].v_type != VAR_UNKNOWN)
5848 {
5849 wp = find_win_by_nr_or_id(&argvars[0]);
5850 if (wp == NULL)
5851 return;
5852 }
5853
5854 get_tagstack(wp, rettv->vval.v_dict);
5855}
5856
5857/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005858 * Returns information about a window as a dictionary.
5859 */
5860 static dict_T *
5861get_win_info(win_T *wp, short tpnr, short winnr)
5862{
5863 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005864
5865 dict = dict_alloc();
5866 if (dict == NULL)
5867 return NULL;
5868
Bram Moolenaare0be1672018-07-08 16:50:37 +02005869 dict_add_number(dict, "tabnr", tpnr);
5870 dict_add_number(dict, "winnr", winnr);
5871 dict_add_number(dict, "winid", wp->w_id);
5872 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005873 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01005874 dict_add_number(dict, "topline", wp->w_topline);
5875 dict_add_number(dict, "botline", wp->w_botline - 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005876#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02005877 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005878#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005879 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005880 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005881 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005882
Bram Moolenaar69905d12017-08-13 18:14:47 +02005883#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02005884 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02005885#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005886#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02005887 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
5888 dict_add_number(dict, "loclist",
5889 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02005890#endif
5891
Bram Moolenaar30567352016-08-27 21:25:44 +02005892 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005893 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005894
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005895 return dict;
5896}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005897
5898/*
5899 * "getwininfo()" function
5900 */
5901 static void
5902f_getwininfo(typval_T *argvars, typval_T *rettv)
5903{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005904 tabpage_T *tp;
5905 win_T *wp = NULL, *wparg = NULL;
5906 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005907 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005908
5909 if (rettv_list_alloc(rettv) != OK)
5910 return;
5911
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005912 if (argvars[0].v_type != VAR_UNKNOWN)
5913 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01005914 wparg = win_id2wp(tv_get_number(&argvars[0]));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005915 if (wparg == NULL)
5916 return;
5917 }
5918
5919 /* Collect information about either all the windows across all the tab
5920 * pages or one particular window.
5921 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005922 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005923 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005924 tabnr++;
5925 winnr = 0;
5926 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005927 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005928 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005929 if (wparg != NULL && wp != wparg)
5930 continue;
5931 d = get_win_info(wp, tabnr, winnr);
5932 if (d != NULL)
5933 list_append_dict(rettv->vval.v_list, d);
5934 if (wparg != NULL)
5935 /* found information about a specific window */
5936 return;
5937 }
5938 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005939}
5940
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005941/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005942 * "win_findbuf()" function
5943 */
5944 static void
5945f_win_findbuf(typval_T *argvars, typval_T *rettv)
5946{
5947 if (rettv_list_alloc(rettv) != FAIL)
5948 win_findbuf(argvars, rettv->vval.v_list);
5949}
5950
5951/*
5952 * "win_getid()" function
5953 */
5954 static void
5955f_win_getid(typval_T *argvars, typval_T *rettv)
5956{
5957 rettv->vval.v_number = win_getid(argvars);
5958}
5959
5960/*
5961 * "win_gotoid()" function
5962 */
5963 static void
5964f_win_gotoid(typval_T *argvars, typval_T *rettv)
5965{
5966 rettv->vval.v_number = win_gotoid(argvars);
5967}
5968
5969/*
5970 * "win_id2tabwin()" function
5971 */
5972 static void
5973f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5974{
5975 if (rettv_list_alloc(rettv) != FAIL)
5976 win_id2tabwin(argvars, rettv->vval.v_list);
5977}
5978
5979/*
5980 * "win_id2win()" function
5981 */
5982 static void
5983f_win_id2win(typval_T *argvars, typval_T *rettv)
5984{
5985 rettv->vval.v_number = win_id2win(argvars);
5986}
5987
5988/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005989 * "win_screenpos()" function
5990 */
5991 static void
5992f_win_screenpos(typval_T *argvars, typval_T *rettv)
5993{
5994 win_T *wp;
5995
5996 if (rettv_list_alloc(rettv) == FAIL)
5997 return;
5998
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005999 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar22044dc2017-12-02 15:43:37 +01006000 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
6001 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
6002}
6003
6004/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006005 * "getwinpos({timeout})" function
6006 */
6007 static void
6008f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
6009{
6010 int x = -1;
6011 int y = -1;
6012
6013 if (rettv_list_alloc(rettv) == FAIL)
6014 return;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006015#if defined(FEAT_GUI) \
6016 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6017 || defined(MSWIN)
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006018 {
6019 varnumber_T timeout = 100;
6020
6021 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006022 timeout = tv_get_number(&argvars[0]);
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006023
6024 (void)ui_get_winpos(&x, &y, timeout);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006025 }
6026#endif
6027 list_append_number(rettv->vval.v_list, (varnumber_T)x);
6028 list_append_number(rettv->vval.v_list, (varnumber_T)y);
6029}
6030
6031
6032/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006033 * "getwinposx()" function
6034 */
6035 static void
6036f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
6037{
6038 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006039#if defined(FEAT_GUI) \
6040 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6041 || defined(MSWIN)
6042
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006043 {
6044 int x, y;
6045
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006046 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006047 rettv->vval.v_number = x;
6048 }
6049#endif
6050}
6051
6052/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006053 * "getwinposy()" function
6054 */
6055 static void
6056f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
6057{
6058 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006059#if defined(FEAT_GUI) \
6060 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6061 || defined(MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006062 {
6063 int x, y;
6064
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006065 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006066 rettv->vval.v_number = y;
6067 }
6068#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006069}
6070
6071/*
6072 * "getwinvar()" function
6073 */
6074 static void
6075f_getwinvar(typval_T *argvars, typval_T *rettv)
6076{
6077 getwinvar(argvars, rettv, 0);
6078}
6079
6080/*
6081 * "glob()" function
6082 */
6083 static void
6084f_glob(typval_T *argvars, typval_T *rettv)
6085{
6086 int options = WILD_SILENT|WILD_USE_NL;
6087 expand_T xpc;
6088 int error = FALSE;
6089
6090 /* When the optional second argument is non-zero, don't remove matches
6091 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6092 rettv->v_type = VAR_STRING;
6093 if (argvars[1].v_type != VAR_UNKNOWN)
6094 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006095 if (tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006096 options |= WILD_KEEP_ALL;
6097 if (argvars[2].v_type != VAR_UNKNOWN)
6098 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006099 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006100 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006101 if (argvars[3].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006102 && tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006103 options |= WILD_ALLLINKS;
6104 }
6105 }
6106 if (!error)
6107 {
6108 ExpandInit(&xpc);
6109 xpc.xp_context = EXPAND_FILES;
6110 if (p_wic)
6111 options += WILD_ICASE;
6112 if (rettv->v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006113 rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006114 NULL, options, WILD_ALL);
6115 else if (rettv_list_alloc(rettv) != FAIL)
6116 {
6117 int i;
6118
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006119 ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006120 NULL, options, WILD_ALL_KEEP);
6121 for (i = 0; i < xpc.xp_numfiles; i++)
6122 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
6123
6124 ExpandCleanup(&xpc);
6125 }
6126 }
6127 else
6128 rettv->vval.v_string = NULL;
6129}
6130
6131/*
6132 * "globpath()" function
6133 */
6134 static void
6135f_globpath(typval_T *argvars, typval_T *rettv)
6136{
6137 int flags = 0;
6138 char_u buf1[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006139 char_u *file = tv_get_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006140 int error = FALSE;
6141 garray_T ga;
6142 int i;
6143
6144 /* When the optional second argument is non-zero, don't remove matches
6145 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6146 rettv->v_type = VAR_STRING;
6147 if (argvars[2].v_type != VAR_UNKNOWN)
6148 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006149 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006150 flags |= WILD_KEEP_ALL;
6151 if (argvars[3].v_type != VAR_UNKNOWN)
6152 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006153 if (tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006154 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006155 if (argvars[4].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006156 && tv_get_number_chk(&argvars[4], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006157 flags |= WILD_ALLLINKS;
6158 }
6159 }
6160 if (file != NULL && !error)
6161 {
6162 ga_init2(&ga, (int)sizeof(char_u *), 10);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006163 globpath(tv_get_string(&argvars[0]), file, &ga, flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006164 if (rettv->v_type == VAR_STRING)
6165 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
6166 else if (rettv_list_alloc(rettv) != FAIL)
6167 for (i = 0; i < ga.ga_len; ++i)
6168 list_append_string(rettv->vval.v_list,
6169 ((char_u **)(ga.ga_data))[i], -1);
6170 ga_clear_strings(&ga);
6171 }
6172 else
6173 rettv->vval.v_string = NULL;
6174}
6175
6176/*
6177 * "glob2regpat()" function
6178 */
6179 static void
6180f_glob2regpat(typval_T *argvars, typval_T *rettv)
6181{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006182 char_u *pat = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006183
6184 rettv->v_type = VAR_STRING;
6185 rettv->vval.v_string = (pat == NULL)
6186 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
6187}
6188
6189/* for VIM_VERSION_ defines */
6190#include "version.h"
6191
6192/*
6193 * "has()" function
6194 */
6195 static void
6196f_has(typval_T *argvars, typval_T *rettv)
6197{
6198 int i;
6199 char_u *name;
6200 int n = FALSE;
6201 static char *(has_list[]) =
6202 {
6203#ifdef AMIGA
6204 "amiga",
6205# ifdef FEAT_ARP
6206 "arp",
6207# endif
6208#endif
6209#ifdef __BEOS__
6210 "beos",
6211#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006212#if defined(BSD) && !defined(MACOS_X)
6213 "bsd",
6214#endif
6215#ifdef hpux
6216 "hpux",
6217#endif
6218#ifdef __linux__
6219 "linux",
6220#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02006221#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01006222 "mac", /* Mac OS X (and, once, Mac OS Classic) */
6223 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02006224# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01006225 "macunix", /* Mac OS X, with the darwin feature */
6226 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02006227# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006228#endif
6229#ifdef __QNX__
6230 "qnx",
6231#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006232#ifdef SUN_SYSTEM
6233 "sun",
6234#else
6235 "moon",
6236#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006237#ifdef UNIX
6238 "unix",
6239#endif
6240#ifdef VMS
6241 "vms",
6242#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006243#ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006244 "win32",
6245#endif
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006246#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006247 "win32unix",
6248#endif
Bram Moolenaar44b443c2019-02-18 22:14:18 +01006249#ifdef _WIN64
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006250 "win64",
6251#endif
6252#ifdef EBCDIC
6253 "ebcdic",
6254#endif
6255#ifndef CASE_INSENSITIVE_FILENAME
6256 "fname_case",
6257#endif
6258#ifdef HAVE_ACL
6259 "acl",
6260#endif
6261#ifdef FEAT_ARABIC
6262 "arabic",
6263#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006264 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006265#ifdef FEAT_AUTOCHDIR
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006266 "autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006267#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006268#ifdef FEAT_AUTOSERVERNAME
6269 "autoservername",
6270#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006271#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006272 "balloon_eval",
Bram Moolenaar4f974752019-02-17 17:44:42 +01006273# ifndef FEAT_GUI_MSWIN /* other GUIs always have multiline balloons */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006274 "balloon_multiline",
6275# endif
6276#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006277#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006278 "balloon_eval_term",
6279#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006280#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6281 "builtin_terms",
6282# ifdef ALL_BUILTIN_TCAPS
6283 "all_builtin_terms",
6284# endif
6285#endif
6286#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01006287 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006288 || defined(FEAT_GUI_MOTIF))
6289 "browsefilter",
6290#endif
6291#ifdef FEAT_BYTEOFF
6292 "byte_offset",
6293#endif
6294#ifdef FEAT_JOB_CHANNEL
6295 "channel",
6296#endif
6297#ifdef FEAT_CINDENT
6298 "cindent",
6299#endif
6300#ifdef FEAT_CLIENTSERVER
6301 "clientserver",
6302#endif
6303#ifdef FEAT_CLIPBOARD
6304 "clipboard",
6305#endif
6306#ifdef FEAT_CMDL_COMPL
6307 "cmdline_compl",
6308#endif
6309#ifdef FEAT_CMDHIST
6310 "cmdline_hist",
6311#endif
6312#ifdef FEAT_COMMENTS
6313 "comments",
6314#endif
6315#ifdef FEAT_CONCEAL
6316 "conceal",
6317#endif
6318#ifdef FEAT_CRYPT
6319 "cryptv",
6320 "crypt-blowfish",
6321 "crypt-blowfish2",
6322#endif
6323#ifdef FEAT_CSCOPE
6324 "cscope",
6325#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006326 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006327#ifdef CURSOR_SHAPE
6328 "cursorshape",
6329#endif
6330#ifdef DEBUG
6331 "debug",
6332#endif
6333#ifdef FEAT_CON_DIALOG
6334 "dialog_con",
6335#endif
6336#ifdef FEAT_GUI_DIALOG
6337 "dialog_gui",
6338#endif
6339#ifdef FEAT_DIFF
6340 "diff",
6341#endif
6342#ifdef FEAT_DIGRAPHS
6343 "digraphs",
6344#endif
6345#ifdef FEAT_DIRECTX
6346 "directx",
6347#endif
6348#ifdef FEAT_DND
6349 "dnd",
6350#endif
6351#ifdef FEAT_EMACS_TAGS
6352 "emacs_tags",
6353#endif
6354 "eval", /* always present, of course! */
6355 "ex_extra", /* graduated feature */
6356#ifdef FEAT_SEARCH_EXTRA
6357 "extra_search",
6358#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006359#ifdef FEAT_SEARCHPATH
6360 "file_in_path",
6361#endif
6362#ifdef FEAT_FILTERPIPE
6363 "filterpipe",
6364#endif
6365#ifdef FEAT_FIND_ID
6366 "find_in_path",
6367#endif
6368#ifdef FEAT_FLOAT
6369 "float",
6370#endif
6371#ifdef FEAT_FOLDING
6372 "folding",
6373#endif
6374#ifdef FEAT_FOOTER
6375 "footer",
6376#endif
6377#if !defined(USE_SYSTEM) && defined(UNIX)
6378 "fork",
6379#endif
6380#ifdef FEAT_GETTEXT
6381 "gettext",
6382#endif
6383#ifdef FEAT_GUI
6384 "gui",
6385#endif
6386#ifdef FEAT_GUI_ATHENA
6387# ifdef FEAT_GUI_NEXTAW
6388 "gui_neXtaw",
6389# else
6390 "gui_athena",
6391# endif
6392#endif
6393#ifdef FEAT_GUI_GTK
6394 "gui_gtk",
6395# ifdef USE_GTK3
6396 "gui_gtk3",
6397# else
6398 "gui_gtk2",
6399# endif
6400#endif
6401#ifdef FEAT_GUI_GNOME
6402 "gui_gnome",
6403#endif
6404#ifdef FEAT_GUI_MAC
6405 "gui_mac",
6406#endif
6407#ifdef FEAT_GUI_MOTIF
6408 "gui_motif",
6409#endif
6410#ifdef FEAT_GUI_PHOTON
6411 "gui_photon",
6412#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006413#ifdef FEAT_GUI_MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006414 "gui_win32",
6415#endif
6416#ifdef FEAT_HANGULIN
6417 "hangul_input",
6418#endif
6419#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6420 "iconv",
6421#endif
6422#ifdef FEAT_INS_EXPAND
6423 "insert_expand",
6424#endif
6425#ifdef FEAT_JOB_CHANNEL
6426 "job",
6427#endif
6428#ifdef FEAT_JUMPLIST
6429 "jumplist",
6430#endif
6431#ifdef FEAT_KEYMAP
6432 "keymap",
6433#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006434 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006435#ifdef FEAT_LANGMAP
6436 "langmap",
6437#endif
6438#ifdef FEAT_LIBCALL
6439 "libcall",
6440#endif
6441#ifdef FEAT_LINEBREAK
6442 "linebreak",
6443#endif
6444#ifdef FEAT_LISP
6445 "lispindent",
6446#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006447 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006448#ifdef FEAT_LOCALMAP
6449 "localmap",
6450#endif
6451#ifdef FEAT_LUA
6452# ifndef DYNAMIC_LUA
6453 "lua",
6454# endif
6455#endif
6456#ifdef FEAT_MENU
6457 "menu",
6458#endif
6459#ifdef FEAT_SESSION
6460 "mksession",
6461#endif
6462#ifdef FEAT_MODIFY_FNAME
6463 "modify_fname",
6464#endif
6465#ifdef FEAT_MOUSE
6466 "mouse",
6467#endif
6468#ifdef FEAT_MOUSESHAPE
6469 "mouseshape",
6470#endif
6471#if defined(UNIX) || defined(VMS)
6472# ifdef FEAT_MOUSE_DEC
6473 "mouse_dec",
6474# endif
6475# ifdef FEAT_MOUSE_GPM
6476 "mouse_gpm",
6477# endif
6478# ifdef FEAT_MOUSE_JSB
6479 "mouse_jsbterm",
6480# endif
6481# ifdef FEAT_MOUSE_NET
6482 "mouse_netterm",
6483# endif
6484# ifdef FEAT_MOUSE_PTERM
6485 "mouse_pterm",
6486# endif
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01006487# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006488 "mouse_sgr",
6489# endif
6490# ifdef FEAT_SYSMOUSE
6491 "mouse_sysmouse",
6492# endif
6493# ifdef FEAT_MOUSE_URXVT
6494 "mouse_urxvt",
6495# endif
6496# ifdef FEAT_MOUSE_XTERM
6497 "mouse_xterm",
6498# endif
6499#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006500 "multi_byte",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006501#ifdef FEAT_MBYTE_IME
6502 "multi_byte_ime",
6503#endif
6504#ifdef FEAT_MULTI_LANG
6505 "multi_lang",
6506#endif
6507#ifdef FEAT_MZSCHEME
6508#ifndef DYNAMIC_MZSCHEME
6509 "mzscheme",
6510#endif
6511#endif
6512#ifdef FEAT_NUM64
6513 "num64",
6514#endif
6515#ifdef FEAT_OLE
6516 "ole",
6517#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006518#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006519 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006520#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006521#ifdef FEAT_PATH_EXTRA
6522 "path_extra",
6523#endif
6524#ifdef FEAT_PERL
6525#ifndef DYNAMIC_PERL
6526 "perl",
6527#endif
6528#endif
6529#ifdef FEAT_PERSISTENT_UNDO
6530 "persistent_undo",
6531#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006532#if defined(FEAT_PYTHON)
6533 "python_compiled",
6534# if defined(DYNAMIC_PYTHON)
6535 "python_dynamic",
6536# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006537 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006538 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006539# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006540#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006541#if defined(FEAT_PYTHON3)
6542 "python3_compiled",
6543# if defined(DYNAMIC_PYTHON3)
6544 "python3_dynamic",
6545# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006546 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006547 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006548# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006549#endif
6550#ifdef FEAT_POSTSCRIPT
6551 "postscript",
6552#endif
6553#ifdef FEAT_PRINTER
6554 "printer",
6555#endif
6556#ifdef FEAT_PROFILE
6557 "profile",
6558#endif
6559#ifdef FEAT_RELTIME
6560 "reltime",
6561#endif
6562#ifdef FEAT_QUICKFIX
6563 "quickfix",
6564#endif
6565#ifdef FEAT_RIGHTLEFT
6566 "rightleft",
6567#endif
6568#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6569 "ruby",
6570#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006571 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006572#ifdef FEAT_CMDL_INFO
6573 "showcmd",
6574 "cmdline_info",
6575#endif
6576#ifdef FEAT_SIGNS
6577 "signs",
6578#endif
6579#ifdef FEAT_SMARTINDENT
6580 "smartindent",
6581#endif
6582#ifdef STARTUPTIME
6583 "startuptime",
6584#endif
6585#ifdef FEAT_STL_OPT
6586 "statusline",
6587#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006588#ifdef FEAT_NETBEANS_INTG
6589 "netbeans_intg",
6590#endif
6591#ifdef FEAT_SPELL
6592 "spell",
6593#endif
6594#ifdef FEAT_SYN_HL
6595 "syntax",
6596#endif
6597#if defined(USE_SYSTEM) || !defined(UNIX)
6598 "system",
6599#endif
6600#ifdef FEAT_TAG_BINS
6601 "tag_binary",
6602#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006603#ifdef FEAT_TCL
6604# ifndef DYNAMIC_TCL
6605 "tcl",
6606# endif
6607#endif
6608#ifdef FEAT_TERMGUICOLORS
6609 "termguicolors",
6610#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006611#if defined(FEAT_TERMINAL) && !defined(MSWIN)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006612 "terminal",
6613#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006614#ifdef TERMINFO
6615 "terminfo",
6616#endif
6617#ifdef FEAT_TERMRESPONSE
6618 "termresponse",
6619#endif
6620#ifdef FEAT_TEXTOBJ
6621 "textobjects",
6622#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006623#ifdef FEAT_TEXT_PROP
6624 "textprop",
6625#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006626#ifdef HAVE_TGETENT
6627 "tgetent",
6628#endif
6629#ifdef FEAT_TIMERS
6630 "timers",
6631#endif
6632#ifdef FEAT_TITLE
6633 "title",
6634#endif
6635#ifdef FEAT_TOOLBAR
6636 "toolbar",
6637#endif
6638#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6639 "unnamedplus",
6640#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006641 "user-commands", /* was accidentally included in 5.4 */
6642 "user_commands",
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006643#ifdef FEAT_VARTABS
6644 "vartabs",
6645#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006646 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006647#ifdef FEAT_VIMINFO
6648 "viminfo",
6649#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006650 "vimscript-1",
6651 "vimscript-2",
Bram Moolenaar93a48792019-04-20 21:54:28 +02006652 "vimscript-3",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006653 "virtualedit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006654 "visual",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006655 "visualextra",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006656 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006657#ifdef FEAT_VTP
6658 "vtp",
6659#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006660#ifdef FEAT_WILDIGN
6661 "wildignore",
6662#endif
6663#ifdef FEAT_WILDMENU
6664 "wildmenu",
6665#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006666 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006667#ifdef FEAT_WAK
6668 "winaltkeys",
6669#endif
6670#ifdef FEAT_WRITEBACKUP
6671 "writebackup",
6672#endif
6673#ifdef FEAT_XIM
6674 "xim",
6675#endif
6676#ifdef FEAT_XFONTSET
6677 "xfontset",
6678#endif
6679#ifdef FEAT_XPM_W32
6680 "xpm",
6681 "xpm_w32", /* for backward compatibility */
6682#else
6683# if defined(HAVE_XPM)
6684 "xpm",
6685# endif
6686#endif
6687#ifdef USE_XSMP
6688 "xsmp",
6689#endif
6690#ifdef USE_XSMP_INTERACT
6691 "xsmp_interact",
6692#endif
6693#ifdef FEAT_XCLIPBOARD
6694 "xterm_clipboard",
6695#endif
6696#ifdef FEAT_XTERM_SAVE
6697 "xterm_save",
6698#endif
6699#if defined(UNIX) && defined(FEAT_X11)
6700 "X11",
6701#endif
6702 NULL
6703 };
6704
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006705 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006706 for (i = 0; has_list[i] != NULL; ++i)
6707 if (STRICMP(name, has_list[i]) == 0)
6708 {
6709 n = TRUE;
6710 break;
6711 }
6712
6713 if (n == FALSE)
6714 {
6715 if (STRNICMP(name, "patch", 5) == 0)
6716 {
6717 if (name[5] == '-'
6718 && STRLEN(name) >= 11
6719 && vim_isdigit(name[6])
6720 && vim_isdigit(name[8])
6721 && vim_isdigit(name[10]))
6722 {
6723 int major = atoi((char *)name + 6);
6724 int minor = atoi((char *)name + 8);
6725
6726 /* Expect "patch-9.9.01234". */
6727 n = (major < VIM_VERSION_MAJOR
6728 || (major == VIM_VERSION_MAJOR
6729 && (minor < VIM_VERSION_MINOR
6730 || (minor == VIM_VERSION_MINOR
6731 && has_patch(atoi((char *)name + 10))))));
6732 }
6733 else
6734 n = has_patch(atoi((char *)name + 5));
6735 }
6736 else if (STRICMP(name, "vim_starting") == 0)
6737 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006738 else if (STRICMP(name, "ttyin") == 0)
6739 n = mch_input_isatty();
6740 else if (STRICMP(name, "ttyout") == 0)
6741 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006742 else if (STRICMP(name, "multi_byte_encoding") == 0)
6743 n = has_mbyte;
Bram Moolenaar4f974752019-02-17 17:44:42 +01006744#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006745 else if (STRICMP(name, "balloon_multiline") == 0)
6746 n = multiline_balloon_available();
6747#endif
6748#ifdef DYNAMIC_TCL
6749 else if (STRICMP(name, "tcl") == 0)
6750 n = tcl_enabled(FALSE);
6751#endif
6752#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6753 else if (STRICMP(name, "iconv") == 0)
6754 n = iconv_enabled(FALSE);
6755#endif
6756#ifdef DYNAMIC_LUA
6757 else if (STRICMP(name, "lua") == 0)
6758 n = lua_enabled(FALSE);
6759#endif
6760#ifdef DYNAMIC_MZSCHEME
6761 else if (STRICMP(name, "mzscheme") == 0)
6762 n = mzscheme_enabled(FALSE);
6763#endif
6764#ifdef DYNAMIC_RUBY
6765 else if (STRICMP(name, "ruby") == 0)
6766 n = ruby_enabled(FALSE);
6767#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006768#ifdef DYNAMIC_PYTHON
6769 else if (STRICMP(name, "python") == 0)
6770 n = python_enabled(FALSE);
6771#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006772#ifdef DYNAMIC_PYTHON3
6773 else if (STRICMP(name, "python3") == 0)
6774 n = python3_enabled(FALSE);
6775#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006776#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6777 else if (STRICMP(name, "pythonx") == 0)
6778 {
6779# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6780 if (p_pyx == 0)
6781 n = python3_enabled(FALSE) || python_enabled(FALSE);
6782 else if (p_pyx == 3)
6783 n = python3_enabled(FALSE);
6784 else if (p_pyx == 2)
6785 n = python_enabled(FALSE);
6786# elif defined(DYNAMIC_PYTHON)
6787 n = python_enabled(FALSE);
6788# elif defined(DYNAMIC_PYTHON3)
6789 n = python3_enabled(FALSE);
6790# endif
6791 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006792#endif
6793#ifdef DYNAMIC_PERL
6794 else if (STRICMP(name, "perl") == 0)
6795 n = perl_enabled(FALSE);
6796#endif
6797#ifdef FEAT_GUI
6798 else if (STRICMP(name, "gui_running") == 0)
6799 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006800# ifdef FEAT_BROWSE
6801 else if (STRICMP(name, "browse") == 0)
6802 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6803# endif
6804#endif
6805#ifdef FEAT_SYN_HL
6806 else if (STRICMP(name, "syntax_items") == 0)
6807 n = syntax_present(curwin);
6808#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006809#ifdef FEAT_VTP
6810 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02006811 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006812#endif
6813#ifdef FEAT_NETBEANS_INTG
6814 else if (STRICMP(name, "netbeans_enabled") == 0)
6815 n = netbeans_active();
6816#endif
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02006817#ifdef FEAT_MOUSE_GPM
6818 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
6819 n = gpm_enabled();
6820#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006821#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02006822 else if (STRICMP(name, "terminal") == 0)
6823 n = terminal_enabled();
6824#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006825#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006826 else if (STRICMP(name, "conpty") == 0)
6827 n = use_conpty();
6828#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006829 }
6830
6831 rettv->vval.v_number = n;
6832}
6833
6834/*
6835 * "has_key()" function
6836 */
6837 static void
6838f_has_key(typval_T *argvars, typval_T *rettv)
6839{
6840 if (argvars[0].v_type != VAR_DICT)
6841 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006842 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006843 return;
6844 }
6845 if (argvars[0].vval.v_dict == NULL)
6846 return;
6847
6848 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006849 tv_get_string(&argvars[1]), -1) != NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006850}
6851
6852/*
6853 * "haslocaldir()" function
6854 */
6855 static void
6856f_haslocaldir(typval_T *argvars, typval_T *rettv)
6857{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006858 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006859 win_T *wp = NULL;
6860
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006861 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
6862
6863 // Check for window-local and tab-local directories
6864 if (wp != NULL && wp->w_localdir != NULL)
6865 rettv->vval.v_number = 1;
6866 else if (tp != NULL && tp->tp_localdir != NULL)
6867 rettv->vval.v_number = 2;
6868 else
6869 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006870}
6871
6872/*
6873 * "hasmapto()" function
6874 */
6875 static void
6876f_hasmapto(typval_T *argvars, typval_T *rettv)
6877{
6878 char_u *name;
6879 char_u *mode;
6880 char_u buf[NUMBUFLEN];
6881 int abbr = FALSE;
6882
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006883 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006884 if (argvars[1].v_type == VAR_UNKNOWN)
6885 mode = (char_u *)"nvo";
6886 else
6887 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006888 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006889 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006890 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006891 }
6892
6893 if (map_to_exists(name, mode, abbr))
6894 rettv->vval.v_number = TRUE;
6895 else
6896 rettv->vval.v_number = FALSE;
6897}
6898
6899/*
6900 * "histadd()" function
6901 */
6902 static void
6903f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6904{
6905#ifdef FEAT_CMDHIST
6906 int histype;
6907 char_u *str;
6908 char_u buf[NUMBUFLEN];
6909#endif
6910
6911 rettv->vval.v_number = FALSE;
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006912 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006913 return;
6914#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006915 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006916 histype = str != NULL ? get_histtype(str) : -1;
6917 if (histype >= 0)
6918 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006919 str = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006920 if (*str != NUL)
6921 {
6922 init_history();
6923 add_to_history(histype, str, FALSE, NUL);
6924 rettv->vval.v_number = TRUE;
6925 return;
6926 }
6927 }
6928#endif
6929}
6930
6931/*
6932 * "histdel()" function
6933 */
6934 static void
6935f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6936{
6937#ifdef FEAT_CMDHIST
6938 int n;
6939 char_u buf[NUMBUFLEN];
6940 char_u *str;
6941
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006942 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006943 if (str == NULL)
6944 n = 0;
6945 else if (argvars[1].v_type == VAR_UNKNOWN)
6946 /* only one argument: clear entire history */
6947 n = clr_history(get_histtype(str));
6948 else if (argvars[1].v_type == VAR_NUMBER)
6949 /* index given: remove that entry */
6950 n = del_history_idx(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006951 (int)tv_get_number(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006952 else
6953 /* string given: remove all matching entries */
6954 n = del_history_entry(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006955 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006956 rettv->vval.v_number = n;
6957#endif
6958}
6959
6960/*
6961 * "histget()" function
6962 */
6963 static void
6964f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6965{
6966#ifdef FEAT_CMDHIST
6967 int type;
6968 int idx;
6969 char_u *str;
6970
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006971 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006972 if (str == NULL)
6973 rettv->vval.v_string = NULL;
6974 else
6975 {
6976 type = get_histtype(str);
6977 if (argvars[1].v_type == VAR_UNKNOWN)
6978 idx = get_history_idx(type);
6979 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006980 idx = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006981 /* -1 on type error */
6982 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6983 }
6984#else
6985 rettv->vval.v_string = NULL;
6986#endif
6987 rettv->v_type = VAR_STRING;
6988}
6989
6990/*
6991 * "histnr()" function
6992 */
6993 static void
6994f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6995{
6996 int i;
6997
6998#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006999 char_u *history = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007000
7001 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
7002 if (i >= HIST_CMD && i < HIST_COUNT)
7003 i = get_history_idx(i);
7004 else
7005#endif
7006 i = -1;
7007 rettv->vval.v_number = i;
7008}
7009
7010/*
7011 * "highlightID(name)" function
7012 */
7013 static void
7014f_hlID(typval_T *argvars, typval_T *rettv)
7015{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007016 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007017}
7018
7019/*
7020 * "highlight_exists()" function
7021 */
7022 static void
7023f_hlexists(typval_T *argvars, typval_T *rettv)
7024{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007025 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007026}
7027
7028/*
7029 * "hostname()" function
7030 */
7031 static void
7032f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
7033{
7034 char_u hostname[256];
7035
7036 mch_get_host_name(hostname, 256);
7037 rettv->v_type = VAR_STRING;
7038 rettv->vval.v_string = vim_strsave(hostname);
7039}
7040
7041/*
7042 * iconv() function
7043 */
7044 static void
7045f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
7046{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007047 char_u buf1[NUMBUFLEN];
7048 char_u buf2[NUMBUFLEN];
7049 char_u *from, *to, *str;
7050 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007051
7052 rettv->v_type = VAR_STRING;
7053 rettv->vval.v_string = NULL;
7054
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007055 str = tv_get_string(&argvars[0]);
7056 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
7057 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007058 vimconv.vc_type = CONV_NONE;
7059 convert_setup(&vimconv, from, to);
7060
7061 /* If the encodings are equal, no conversion needed. */
7062 if (vimconv.vc_type == CONV_NONE)
7063 rettv->vval.v_string = vim_strsave(str);
7064 else
7065 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
7066
7067 convert_setup(&vimconv, NULL, NULL);
7068 vim_free(from);
7069 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007070}
7071
7072/*
7073 * "indent()" function
7074 */
7075 static void
7076f_indent(typval_T *argvars, typval_T *rettv)
7077{
7078 linenr_T lnum;
7079
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007080 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007081 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7082 rettv->vval.v_number = get_indent_lnum(lnum);
7083 else
7084 rettv->vval.v_number = -1;
7085}
7086
7087/*
7088 * "index()" function
7089 */
7090 static void
7091f_index(typval_T *argvars, typval_T *rettv)
7092{
7093 list_T *l;
7094 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007095 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007096 long idx = 0;
7097 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007098 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007099
7100 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007101 if (argvars[0].v_type == VAR_BLOB)
7102 {
7103 typval_T tv;
7104 int start = 0;
7105
7106 if (argvars[2].v_type != VAR_UNKNOWN)
7107 {
7108 start = tv_get_number_chk(&argvars[2], &error);
7109 if (error)
7110 return;
7111 }
7112 b = argvars[0].vval.v_blob;
7113 if (b == NULL)
7114 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01007115 if (start < 0)
7116 {
7117 start = blob_len(b) + start;
7118 if (start < 0)
7119 start = 0;
7120 }
7121
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007122 for (idx = start; idx < blob_len(b); ++idx)
7123 {
7124 tv.v_type = VAR_NUMBER;
7125 tv.vval.v_number = blob_get(b, idx);
7126 if (tv_equal(&tv, &argvars[1], ic, FALSE))
7127 {
7128 rettv->vval.v_number = idx;
7129 return;
7130 }
7131 }
7132 return;
7133 }
7134 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007135 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007136 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007137 return;
7138 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007139
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007140 l = argvars[0].vval.v_list;
7141 if (l != NULL)
7142 {
7143 item = l->lv_first;
7144 if (argvars[2].v_type != VAR_UNKNOWN)
7145 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007146 /* Start at specified item. Use the cached index that list_find()
7147 * sets, so that a negative number also works. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007148 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007149 idx = l->lv_idx;
7150 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007151 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007152 if (error)
7153 item = NULL;
7154 }
7155
7156 for ( ; item != NULL; item = item->li_next, ++idx)
7157 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
7158 {
7159 rettv->vval.v_number = idx;
7160 break;
7161 }
7162 }
7163}
7164
7165static int inputsecret_flag = 0;
7166
7167/*
7168 * "input()" function
7169 * Also handles inputsecret() when inputsecret is set.
7170 */
7171 static void
7172f_input(typval_T *argvars, typval_T *rettv)
7173{
7174 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
7175}
7176
7177/*
7178 * "inputdialog()" function
7179 */
7180 static void
7181f_inputdialog(typval_T *argvars, typval_T *rettv)
7182{
7183#if defined(FEAT_GUI_TEXTDIALOG)
7184 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
7185 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
7186 {
7187 char_u *message;
7188 char_u buf[NUMBUFLEN];
7189 char_u *defstr = (char_u *)"";
7190
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007191 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007192 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007193 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007194 vim_strncpy(IObuff, defstr, IOSIZE - 1);
7195 else
7196 IObuff[0] = NUL;
7197 if (message != NULL && defstr != NULL
7198 && do_dialog(VIM_QUESTION, NULL, message,
7199 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
7200 rettv->vval.v_string = vim_strsave(IObuff);
7201 else
7202 {
7203 if (message != NULL && defstr != NULL
7204 && argvars[1].v_type != VAR_UNKNOWN
7205 && argvars[2].v_type != VAR_UNKNOWN)
7206 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007207 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007208 else
7209 rettv->vval.v_string = NULL;
7210 }
7211 rettv->v_type = VAR_STRING;
7212 }
7213 else
7214#endif
7215 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
7216}
7217
7218/*
7219 * "inputlist()" function
7220 */
7221 static void
7222f_inputlist(typval_T *argvars, typval_T *rettv)
7223{
7224 listitem_T *li;
7225 int selected;
7226 int mouse_used;
7227
7228#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007229 /* While starting up, there is no place to enter text. When running tests
7230 * with --not-a-term we assume feedkeys() will be used. */
7231 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007232 return;
7233#endif
7234 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
7235 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007236 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007237 return;
7238 }
7239
7240 msg_start();
7241 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
7242 lines_left = Rows; /* avoid more prompt */
7243 msg_scroll = TRUE;
7244 msg_clr_eos();
7245
7246 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
7247 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007248 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007249 msg_putchar('\n');
7250 }
7251
7252 /* Ask for choice. */
7253 selected = prompt_for_number(&mouse_used);
7254 if (mouse_used)
7255 selected -= lines_left;
7256
7257 rettv->vval.v_number = selected;
7258}
7259
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007260static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7261
7262/*
7263 * "inputrestore()" function
7264 */
7265 static void
7266f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7267{
7268 if (ga_userinput.ga_len > 0)
7269 {
7270 --ga_userinput.ga_len;
7271 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7272 + ga_userinput.ga_len);
7273 /* default return is zero == OK */
7274 }
7275 else if (p_verbose > 1)
7276 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007277 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007278 rettv->vval.v_number = 1; /* Failed */
7279 }
7280}
7281
7282/*
7283 * "inputsave()" function
7284 */
7285 static void
7286f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7287{
7288 /* Add an entry to the stack of typeahead storage. */
7289 if (ga_grow(&ga_userinput, 1) == OK)
7290 {
7291 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7292 + ga_userinput.ga_len);
7293 ++ga_userinput.ga_len;
7294 /* default return is zero == OK */
7295 }
7296 else
7297 rettv->vval.v_number = 1; /* Failed */
7298}
7299
7300/*
7301 * "inputsecret()" function
7302 */
7303 static void
7304f_inputsecret(typval_T *argvars, typval_T *rettv)
7305{
7306 ++cmdline_star;
7307 ++inputsecret_flag;
7308 f_input(argvars, rettv);
7309 --cmdline_star;
7310 --inputsecret_flag;
7311}
7312
7313/*
7314 * "insert()" function
7315 */
7316 static void
7317f_insert(typval_T *argvars, typval_T *rettv)
7318{
7319 long before = 0;
7320 listitem_T *item;
7321 list_T *l;
7322 int error = FALSE;
7323
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007324 if (argvars[0].v_type == VAR_BLOB)
7325 {
7326 int val, len;
7327 char_u *p;
7328
7329 len = blob_len(argvars[0].vval.v_blob);
7330 if (argvars[2].v_type != VAR_UNKNOWN)
7331 {
7332 before = (long)tv_get_number_chk(&argvars[2], &error);
7333 if (error)
7334 return; // type error; errmsg already given
7335 if (before < 0 || before > len)
7336 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007337 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007338 return;
7339 }
7340 }
7341 val = tv_get_number_chk(&argvars[1], &error);
7342 if (error)
7343 return;
7344 if (val < 0 || val > 255)
7345 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007346 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007347 return;
7348 }
7349
7350 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
7351 return;
7352 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
7353 mch_memmove(p + before + 1, p + before, (size_t)len - before);
7354 *(p + before) = val;
7355 ++argvars[0].vval.v_blob->bv_ga.ga_len;
7356
7357 copy_tv(&argvars[0], rettv);
7358 }
7359 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007360 semsg(_(e_listblobarg), "insert()");
Bram Moolenaar05c00c02019-02-11 22:00:11 +01007361 else if ((l = argvars[0].vval.v_list) != NULL
7362 && !var_check_lock(l->lv_lock,
7363 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007364 {
7365 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007366 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007367 if (error)
7368 return; /* type error; errmsg already given */
7369
7370 if (before == l->lv_len)
7371 item = NULL;
7372 else
7373 {
7374 item = list_find(l, before);
7375 if (item == NULL)
7376 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007377 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007378 l = NULL;
7379 }
7380 }
7381 if (l != NULL)
7382 {
7383 list_insert_tv(l, &argvars[1], item);
7384 copy_tv(&argvars[0], rettv);
7385 }
7386 }
7387}
7388
7389/*
7390 * "invert(expr)" function
7391 */
7392 static void
7393f_invert(typval_T *argvars, typval_T *rettv)
7394{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007395 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007396}
7397
7398/*
7399 * "isdirectory()" function
7400 */
7401 static void
7402f_isdirectory(typval_T *argvars, typval_T *rettv)
7403{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007404 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007405}
7406
7407/*
7408 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7409 * or it refers to a List or Dictionary that is locked.
7410 */
7411 static int
7412tv_islocked(typval_T *tv)
7413{
7414 return (tv->v_lock & VAR_LOCKED)
7415 || (tv->v_type == VAR_LIST
7416 && tv->vval.v_list != NULL
7417 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7418 || (tv->v_type == VAR_DICT
7419 && tv->vval.v_dict != NULL
7420 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7421}
7422
7423/*
7424 * "islocked()" function
7425 */
7426 static void
7427f_islocked(typval_T *argvars, typval_T *rettv)
7428{
7429 lval_T lv;
7430 char_u *end;
7431 dictitem_T *di;
7432
7433 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007434 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007435 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007436 if (end != NULL && lv.ll_name != NULL)
7437 {
7438 if (*end != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007439 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007440 else
7441 {
7442 if (lv.ll_tv == NULL)
7443 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007444 di = find_var(lv.ll_name, NULL, TRUE);
7445 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007446 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007447 /* Consider a variable locked when:
7448 * 1. the variable itself is locked
7449 * 2. the value of the variable is locked.
7450 * 3. the List or Dict value is locked.
7451 */
7452 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7453 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007454 }
7455 }
7456 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007457 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007458 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007459 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007460 else if (lv.ll_list != NULL)
7461 /* List item. */
7462 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7463 else
7464 /* Dictionary item. */
7465 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7466 }
7467 }
7468
7469 clear_lval(&lv);
7470}
7471
7472#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7473/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02007474 * "isinf()" function
7475 */
7476 static void
7477f_isinf(typval_T *argvars, typval_T *rettv)
7478{
7479 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
7480 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
7481}
7482
7483/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007484 * "isnan()" function
7485 */
7486 static void
7487f_isnan(typval_T *argvars, typval_T *rettv)
7488{
7489 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7490 && isnan(argvars[0].vval.v_float);
7491}
7492#endif
7493
7494/*
7495 * "items(dict)" function
7496 */
7497 static void
7498f_items(typval_T *argvars, typval_T *rettv)
7499{
7500 dict_list(argvars, rettv, 2);
7501}
7502
7503#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
7504/*
7505 * Get the job from the argument.
7506 * Returns NULL if the job is invalid.
7507 */
7508 static job_T *
7509get_job_arg(typval_T *tv)
7510{
7511 job_T *job;
7512
7513 if (tv->v_type != VAR_JOB)
7514 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007515 semsg(_(e_invarg2), tv_get_string(tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007516 return NULL;
7517 }
7518 job = tv->vval.v_job;
7519
7520 if (job == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007521 emsg(_("E916: not a valid job"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007522 return job;
7523}
7524
7525/*
7526 * "job_getchannel()" function
7527 */
7528 static void
7529f_job_getchannel(typval_T *argvars, typval_T *rettv)
7530{
7531 job_T *job = get_job_arg(&argvars[0]);
7532
7533 if (job != NULL)
7534 {
7535 rettv->v_type = VAR_CHANNEL;
7536 rettv->vval.v_channel = job->jv_channel;
7537 if (job->jv_channel != NULL)
7538 ++job->jv_channel->ch_refcount;
7539 }
7540}
7541
7542/*
7543 * "job_info()" function
7544 */
7545 static void
7546f_job_info(typval_T *argvars, typval_T *rettv)
7547{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007548 if (argvars[0].v_type != VAR_UNKNOWN)
7549 {
7550 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007551
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007552 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7553 job_info(job, rettv->vval.v_dict);
7554 }
7555 else if (rettv_list_alloc(rettv) == OK)
7556 job_info_all(rettv->vval.v_list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007557}
7558
7559/*
7560 * "job_setoptions()" function
7561 */
7562 static void
7563f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7564{
7565 job_T *job = get_job_arg(&argvars[0]);
7566 jobopt_T opt;
7567
7568 if (job == NULL)
7569 return;
7570 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007571 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007572 job_set_options(job, &opt);
7573 free_job_options(&opt);
7574}
7575
7576/*
7577 * "job_start()" function
7578 */
7579 static void
7580f_job_start(typval_T *argvars, typval_T *rettv)
7581{
7582 rettv->v_type = VAR_JOB;
7583 if (check_restricted() || check_secure())
7584 return;
Bram Moolenaar493359e2018-06-12 20:25:52 +02007585 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007586}
7587
7588/*
7589 * "job_status()" function
7590 */
7591 static void
7592f_job_status(typval_T *argvars, typval_T *rettv)
7593{
7594 job_T *job = get_job_arg(&argvars[0]);
7595
7596 if (job != NULL)
7597 {
7598 rettv->v_type = VAR_STRING;
7599 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7600 }
7601}
7602
7603/*
7604 * "job_stop()" function
7605 */
7606 static void
7607f_job_stop(typval_T *argvars, typval_T *rettv)
7608{
7609 job_T *job = get_job_arg(&argvars[0]);
7610
7611 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007612 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007613}
7614#endif
7615
7616/*
7617 * "join()" function
7618 */
7619 static void
7620f_join(typval_T *argvars, typval_T *rettv)
7621{
7622 garray_T ga;
7623 char_u *sep;
7624
7625 if (argvars[0].v_type != VAR_LIST)
7626 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007627 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007628 return;
7629 }
7630 if (argvars[0].vval.v_list == NULL)
7631 return;
7632 if (argvars[1].v_type == VAR_UNKNOWN)
7633 sep = (char_u *)" ";
7634 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007635 sep = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007636
7637 rettv->v_type = VAR_STRING;
7638
7639 if (sep != NULL)
7640 {
7641 ga_init2(&ga, (int)sizeof(char), 80);
7642 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7643 ga_append(&ga, NUL);
7644 rettv->vval.v_string = (char_u *)ga.ga_data;
7645 }
7646 else
7647 rettv->vval.v_string = NULL;
7648}
7649
7650/*
7651 * "js_decode()" function
7652 */
7653 static void
7654f_js_decode(typval_T *argvars, typval_T *rettv)
7655{
7656 js_read_T reader;
7657
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007658 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007659 reader.js_fill = NULL;
7660 reader.js_used = 0;
7661 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007662 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007663}
7664
7665/*
7666 * "js_encode()" function
7667 */
7668 static void
7669f_js_encode(typval_T *argvars, typval_T *rettv)
7670{
7671 rettv->v_type = VAR_STRING;
7672 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7673}
7674
7675/*
7676 * "json_decode()" function
7677 */
7678 static void
7679f_json_decode(typval_T *argvars, typval_T *rettv)
7680{
7681 js_read_T reader;
7682
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007683 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007684 reader.js_fill = NULL;
7685 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007686 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007687}
7688
7689/*
7690 * "json_encode()" function
7691 */
7692 static void
7693f_json_encode(typval_T *argvars, typval_T *rettv)
7694{
7695 rettv->v_type = VAR_STRING;
7696 rettv->vval.v_string = json_encode(&argvars[0], 0);
7697}
7698
7699/*
7700 * "keys()" function
7701 */
7702 static void
7703f_keys(typval_T *argvars, typval_T *rettv)
7704{
7705 dict_list(argvars, rettv, 0);
7706}
7707
7708/*
7709 * "last_buffer_nr()" function.
7710 */
7711 static void
7712f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7713{
7714 int n = 0;
7715 buf_T *buf;
7716
Bram Moolenaar29323592016-07-24 22:04:11 +02007717 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007718 if (n < buf->b_fnum)
7719 n = buf->b_fnum;
7720
7721 rettv->vval.v_number = n;
7722}
7723
7724/*
7725 * "len()" function
7726 */
7727 static void
7728f_len(typval_T *argvars, typval_T *rettv)
7729{
7730 switch (argvars[0].v_type)
7731 {
7732 case VAR_STRING:
7733 case VAR_NUMBER:
7734 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007735 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007736 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007737 case VAR_BLOB:
7738 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
7739 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007740 case VAR_LIST:
7741 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7742 break;
7743 case VAR_DICT:
7744 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7745 break;
7746 case VAR_UNKNOWN:
7747 case VAR_SPECIAL:
7748 case VAR_FLOAT:
7749 case VAR_FUNC:
7750 case VAR_PARTIAL:
7751 case VAR_JOB:
7752 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007753 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007754 break;
7755 }
7756}
7757
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007758 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007759libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007760{
7761#ifdef FEAT_LIBCALL
7762 char_u *string_in;
7763 char_u **string_result;
7764 int nr_result;
7765#endif
7766
7767 rettv->v_type = type;
7768 if (type != VAR_NUMBER)
7769 rettv->vval.v_string = NULL;
7770
7771 if (check_restricted() || check_secure())
7772 return;
7773
7774#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007775 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007776 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7777 {
7778 string_in = NULL;
7779 if (argvars[2].v_type == VAR_STRING)
7780 string_in = argvars[2].vval.v_string;
7781 if (type == VAR_NUMBER)
7782 string_result = NULL;
7783 else
7784 string_result = &rettv->vval.v_string;
7785 if (mch_libcall(argvars[0].vval.v_string,
7786 argvars[1].vval.v_string,
7787 string_in,
7788 argvars[2].vval.v_number,
7789 string_result,
7790 &nr_result) == OK
7791 && type == VAR_NUMBER)
7792 rettv->vval.v_number = nr_result;
7793 }
7794#endif
7795}
7796
7797/*
7798 * "libcall()" function
7799 */
7800 static void
7801f_libcall(typval_T *argvars, typval_T *rettv)
7802{
7803 libcall_common(argvars, rettv, VAR_STRING);
7804}
7805
7806/*
7807 * "libcallnr()" function
7808 */
7809 static void
7810f_libcallnr(typval_T *argvars, typval_T *rettv)
7811{
7812 libcall_common(argvars, rettv, VAR_NUMBER);
7813}
7814
7815/*
7816 * "line(string)" function
7817 */
7818 static void
7819f_line(typval_T *argvars, typval_T *rettv)
7820{
7821 linenr_T lnum = 0;
7822 pos_T *fp;
7823 int fnum;
7824
7825 fp = var2fpos(&argvars[0], TRUE, &fnum);
7826 if (fp != NULL)
7827 lnum = fp->lnum;
7828 rettv->vval.v_number = lnum;
7829}
7830
7831/*
7832 * "line2byte(lnum)" function
7833 */
7834 static void
7835f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7836{
7837#ifndef FEAT_BYTEOFF
7838 rettv->vval.v_number = -1;
7839#else
7840 linenr_T lnum;
7841
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007842 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007843 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7844 rettv->vval.v_number = -1;
7845 else
7846 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7847 if (rettv->vval.v_number >= 0)
7848 ++rettv->vval.v_number;
7849#endif
7850}
7851
7852/*
7853 * "lispindent(lnum)" function
7854 */
7855 static void
7856f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7857{
7858#ifdef FEAT_LISP
7859 pos_T pos;
7860 linenr_T lnum;
7861
7862 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007863 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007864 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7865 {
7866 curwin->w_cursor.lnum = lnum;
7867 rettv->vval.v_number = get_lisp_indent();
7868 curwin->w_cursor = pos;
7869 }
7870 else
7871#endif
7872 rettv->vval.v_number = -1;
7873}
7874
7875/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02007876 * "list2str()" function
7877 */
7878 static void
7879f_list2str(typval_T *argvars, typval_T *rettv)
7880{
7881 list_T *l;
7882 listitem_T *li;
7883 garray_T ga;
7884 int utf8 = FALSE;
7885
7886 rettv->v_type = VAR_STRING;
7887 rettv->vval.v_string = NULL;
7888 if (argvars[0].v_type != VAR_LIST)
7889 {
7890 emsg(_(e_invarg));
7891 return;
7892 }
7893
7894 l = argvars[0].vval.v_list;
7895 if (l == NULL)
7896 return; // empty list results in empty string
7897
7898 if (argvars[1].v_type != VAR_UNKNOWN)
7899 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
7900
7901 ga_init2(&ga, 1, 80);
7902 if (has_mbyte || utf8)
7903 {
7904 char_u buf[MB_MAXBYTES + 1];
7905 int (*char2bytes)(int, char_u *);
7906
7907 if (utf8 || enc_utf8)
7908 char2bytes = utf_char2bytes;
7909 else
7910 char2bytes = mb_char2bytes;
7911
7912 for (li = l->lv_first; li != NULL; li = li->li_next)
7913 {
7914 buf[(*char2bytes)(tv_get_number(&li->li_tv), buf)] = NUL;
7915 ga_concat(&ga, buf);
7916 }
7917 ga_append(&ga, NUL);
7918 }
7919 else if (ga_grow(&ga, list_len(l) + 1) == OK)
7920 {
7921 for (li = l->lv_first; li != NULL; li = li->li_next)
7922 ga_append(&ga, tv_get_number(&li->li_tv));
7923 ga_append(&ga, NUL);
7924 }
7925
7926 rettv->v_type = VAR_STRING;
7927 rettv->vval.v_string = ga.ga_data;
7928}
7929
7930/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007931 * "localtime()" function
7932 */
7933 static void
7934f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7935{
7936 rettv->vval.v_number = (varnumber_T)time(NULL);
7937}
7938
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007939 static void
7940get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7941{
7942 char_u *keys;
7943 char_u *which;
7944 char_u buf[NUMBUFLEN];
7945 char_u *keys_buf = NULL;
7946 char_u *rhs;
7947 int mode;
7948 int abbr = FALSE;
7949 int get_dict = FALSE;
7950 mapblock_T *mp;
7951 int buffer_local;
7952
7953 /* return empty string for failure */
7954 rettv->v_type = VAR_STRING;
7955 rettv->vval.v_string = NULL;
7956
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007957 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007958 if (*keys == NUL)
7959 return;
7960
7961 if (argvars[1].v_type != VAR_UNKNOWN)
7962 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007963 which = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007964 if (argvars[2].v_type != VAR_UNKNOWN)
7965 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007966 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007967 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007968 get_dict = (int)tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007969 }
7970 }
7971 else
7972 which = (char_u *)"";
7973 if (which == NULL)
7974 return;
7975
7976 mode = get_map_mode(&which, 0);
7977
7978 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7979 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7980 vim_free(keys_buf);
7981
7982 if (!get_dict)
7983 {
7984 /* Return a string. */
7985 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02007986 {
7987 if (*rhs == NUL)
7988 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
7989 else
7990 rettv->vval.v_string = str2special_save(rhs, FALSE);
7991 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007992
7993 }
7994 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7995 {
7996 /* Return a dictionary. */
7997 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7998 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7999 dict_T *dict = rettv->vval.v_dict;
8000
Bram Moolenaare0be1672018-07-08 16:50:37 +02008001 dict_add_string(dict, "lhs", lhs);
8002 dict_add_string(dict, "rhs", mp->m_orig_str);
8003 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
8004 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
8005 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008006 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
8007 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02008008 dict_add_number(dict, "buffer", (long)buffer_local);
8009 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
8010 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008011
8012 vim_free(lhs);
8013 vim_free(mapmode);
8014 }
8015}
8016
8017#ifdef FEAT_FLOAT
8018/*
8019 * "log()" function
8020 */
8021 static void
8022f_log(typval_T *argvars, typval_T *rettv)
8023{
8024 float_T f = 0.0;
8025
8026 rettv->v_type = VAR_FLOAT;
8027 if (get_float_arg(argvars, &f) == OK)
8028 rettv->vval.v_float = log(f);
8029 else
8030 rettv->vval.v_float = 0.0;
8031}
8032
8033/*
8034 * "log10()" function
8035 */
8036 static void
8037f_log10(typval_T *argvars, typval_T *rettv)
8038{
8039 float_T f = 0.0;
8040
8041 rettv->v_type = VAR_FLOAT;
8042 if (get_float_arg(argvars, &f) == OK)
8043 rettv->vval.v_float = log10(f);
8044 else
8045 rettv->vval.v_float = 0.0;
8046}
8047#endif
8048
8049#ifdef FEAT_LUA
8050/*
8051 * "luaeval()" function
8052 */
8053 static void
8054f_luaeval(typval_T *argvars, typval_T *rettv)
8055{
8056 char_u *str;
8057 char_u buf[NUMBUFLEN];
8058
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008059 if (check_restricted() || check_secure())
8060 return;
8061
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008062 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008063 do_luaeval(str, argvars + 1, rettv);
8064}
8065#endif
8066
8067/*
8068 * "map()" function
8069 */
8070 static void
8071f_map(typval_T *argvars, typval_T *rettv)
8072{
8073 filter_map(argvars, rettv, TRUE);
8074}
8075
8076/*
8077 * "maparg()" function
8078 */
8079 static void
8080f_maparg(typval_T *argvars, typval_T *rettv)
8081{
8082 get_maparg(argvars, rettv, TRUE);
8083}
8084
8085/*
8086 * "mapcheck()" function
8087 */
8088 static void
8089f_mapcheck(typval_T *argvars, typval_T *rettv)
8090{
8091 get_maparg(argvars, rettv, FALSE);
8092}
8093
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008094typedef enum
8095{
8096 MATCH_END, /* matchend() */
8097 MATCH_MATCH, /* match() */
8098 MATCH_STR, /* matchstr() */
8099 MATCH_LIST, /* matchlist() */
8100 MATCH_POS /* matchstrpos() */
8101} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008102
8103 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008104find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008105{
8106 char_u *str = NULL;
8107 long len = 0;
8108 char_u *expr = NULL;
8109 char_u *pat;
8110 regmatch_T regmatch;
8111 char_u patbuf[NUMBUFLEN];
8112 char_u strbuf[NUMBUFLEN];
8113 char_u *save_cpo;
8114 long start = 0;
8115 long nth = 1;
8116 colnr_T startcol = 0;
8117 int match = 0;
8118 list_T *l = NULL;
8119 listitem_T *li = NULL;
8120 long idx = 0;
8121 char_u *tofree = NULL;
8122
8123 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
8124 save_cpo = p_cpo;
8125 p_cpo = (char_u *)"";
8126
8127 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008128 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008129 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008130 /* type MATCH_LIST: return empty list when there are no matches.
8131 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008132 if (rettv_list_alloc(rettv) == FAIL)
8133 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008134 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008135 && (list_append_string(rettv->vval.v_list,
8136 (char_u *)"", 0) == FAIL
8137 || list_append_number(rettv->vval.v_list,
8138 (varnumber_T)-1) == FAIL
8139 || list_append_number(rettv->vval.v_list,
8140 (varnumber_T)-1) == FAIL
8141 || list_append_number(rettv->vval.v_list,
8142 (varnumber_T)-1) == FAIL))
8143 {
8144 list_free(rettv->vval.v_list);
8145 rettv->vval.v_list = NULL;
8146 goto theend;
8147 }
8148 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008149 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008150 {
8151 rettv->v_type = VAR_STRING;
8152 rettv->vval.v_string = NULL;
8153 }
8154
8155 if (argvars[0].v_type == VAR_LIST)
8156 {
8157 if ((l = argvars[0].vval.v_list) == NULL)
8158 goto theend;
8159 li = l->lv_first;
8160 }
8161 else
8162 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008163 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008164 len = (long)STRLEN(str);
8165 }
8166
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008167 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008168 if (pat == NULL)
8169 goto theend;
8170
8171 if (argvars[2].v_type != VAR_UNKNOWN)
8172 {
8173 int error = FALSE;
8174
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008175 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008176 if (error)
8177 goto theend;
8178 if (l != NULL)
8179 {
8180 li = list_find(l, start);
8181 if (li == NULL)
8182 goto theend;
8183 idx = l->lv_idx; /* use the cached index */
8184 }
8185 else
8186 {
8187 if (start < 0)
8188 start = 0;
8189 if (start > len)
8190 goto theend;
8191 /* When "count" argument is there ignore matches before "start",
8192 * otherwise skip part of the string. Differs when pattern is "^"
8193 * or "\<". */
8194 if (argvars[3].v_type != VAR_UNKNOWN)
8195 startcol = start;
8196 else
8197 {
8198 str += start;
8199 len -= start;
8200 }
8201 }
8202
8203 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008204 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008205 if (error)
8206 goto theend;
8207 }
8208
8209 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8210 if (regmatch.regprog != NULL)
8211 {
8212 regmatch.rm_ic = p_ic;
8213
8214 for (;;)
8215 {
8216 if (l != NULL)
8217 {
8218 if (li == NULL)
8219 {
8220 match = FALSE;
8221 break;
8222 }
8223 vim_free(tofree);
8224 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
8225 if (str == NULL)
8226 break;
8227 }
8228
8229 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
8230
8231 if (match && --nth <= 0)
8232 break;
8233 if (l == NULL && !match)
8234 break;
8235
8236 /* Advance to just after the match. */
8237 if (l != NULL)
8238 {
8239 li = li->li_next;
8240 ++idx;
8241 }
8242 else
8243 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008244 startcol = (colnr_T)(regmatch.startp[0]
8245 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008246 if (startcol > (colnr_T)len
8247 || str + startcol <= regmatch.startp[0])
8248 {
8249 match = FALSE;
8250 break;
8251 }
8252 }
8253 }
8254
8255 if (match)
8256 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008257 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008258 {
8259 listitem_T *li1 = rettv->vval.v_list->lv_first;
8260 listitem_T *li2 = li1->li_next;
8261 listitem_T *li3 = li2->li_next;
8262 listitem_T *li4 = li3->li_next;
8263
8264 vim_free(li1->li_tv.vval.v_string);
8265 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
8266 (int)(regmatch.endp[0] - regmatch.startp[0]));
8267 li3->li_tv.vval.v_number =
8268 (varnumber_T)(regmatch.startp[0] - expr);
8269 li4->li_tv.vval.v_number =
8270 (varnumber_T)(regmatch.endp[0] - expr);
8271 if (l != NULL)
8272 li2->li_tv.vval.v_number = (varnumber_T)idx;
8273 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008274 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008275 {
8276 int i;
8277
8278 /* return list with matched string and submatches */
8279 for (i = 0; i < NSUBEXP; ++i)
8280 {
8281 if (regmatch.endp[i] == NULL)
8282 {
8283 if (list_append_string(rettv->vval.v_list,
8284 (char_u *)"", 0) == FAIL)
8285 break;
8286 }
8287 else if (list_append_string(rettv->vval.v_list,
8288 regmatch.startp[i],
8289 (int)(regmatch.endp[i] - regmatch.startp[i]))
8290 == FAIL)
8291 break;
8292 }
8293 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008294 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008295 {
8296 /* return matched string */
8297 if (l != NULL)
8298 copy_tv(&li->li_tv, rettv);
8299 else
8300 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
8301 (int)(regmatch.endp[0] - regmatch.startp[0]));
8302 }
8303 else if (l != NULL)
8304 rettv->vval.v_number = idx;
8305 else
8306 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008307 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008308 rettv->vval.v_number =
8309 (varnumber_T)(regmatch.startp[0] - str);
8310 else
8311 rettv->vval.v_number =
8312 (varnumber_T)(regmatch.endp[0] - str);
8313 rettv->vval.v_number += (varnumber_T)(str - expr);
8314 }
8315 }
8316 vim_regfree(regmatch.regprog);
8317 }
8318
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008319theend:
8320 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008321 /* matchstrpos() without a list: drop the second item. */
8322 listitem_remove(rettv->vval.v_list,
8323 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008324 vim_free(tofree);
8325 p_cpo = save_cpo;
8326}
8327
8328/*
8329 * "match()" function
8330 */
8331 static void
8332f_match(typval_T *argvars, typval_T *rettv)
8333{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008334 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008335}
8336
Bram Moolenaar95e51472018-07-28 16:55:56 +02008337#ifdef FEAT_SEARCH_EXTRA
8338 static int
8339matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
8340{
8341 dictitem_T *di;
8342
8343 if (tv->v_type != VAR_DICT)
8344 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008345 emsg(_(e_dictreq));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008346 return FAIL;
8347 }
8348
8349 if (dict_find(tv->vval.v_dict, (char_u *)"conceal", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01008350 *conceal_char = dict_get_string(tv->vval.v_dict,
Bram Moolenaar95e51472018-07-28 16:55:56 +02008351 (char_u *)"conceal", FALSE);
8352
8353 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
8354 {
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02008355 *win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008356 if (*win == NULL)
8357 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01008358 emsg(_(e_invalwindow));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008359 return FAIL;
8360 }
8361 }
8362
8363 return OK;
8364}
8365#endif
8366
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008367/*
8368 * "matchadd()" function
8369 */
8370 static void
8371f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8372{
8373#ifdef FEAT_SEARCH_EXTRA
8374 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008375 char_u *grp = tv_get_string_buf_chk(&argvars[0], buf); /* group */
8376 char_u *pat = tv_get_string_buf_chk(&argvars[1], buf); /* pattern */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008377 int prio = 10; /* default priority */
8378 int id = -1;
8379 int error = FALSE;
8380 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008381 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008382
8383 rettv->vval.v_number = -1;
8384
8385 if (grp == NULL || pat == NULL)
8386 return;
8387 if (argvars[2].v_type != VAR_UNKNOWN)
8388 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008389 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008390 if (argvars[3].v_type != VAR_UNKNOWN)
8391 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008392 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008393 if (argvars[4].v_type != VAR_UNKNOWN
8394 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8395 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008396 }
8397 }
8398 if (error == TRUE)
8399 return;
8400 if (id >= 1 && id <= 3)
8401 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008402 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008403 return;
8404 }
8405
Bram Moolenaar95e51472018-07-28 16:55:56 +02008406 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008407 conceal_char);
8408#endif
8409}
8410
8411/*
8412 * "matchaddpos()" function
8413 */
8414 static void
8415f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8416{
8417#ifdef FEAT_SEARCH_EXTRA
8418 char_u buf[NUMBUFLEN];
8419 char_u *group;
8420 int prio = 10;
8421 int id = -1;
8422 int error = FALSE;
8423 list_T *l;
8424 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008425 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008426
8427 rettv->vval.v_number = -1;
8428
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008429 group = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008430 if (group == NULL)
8431 return;
8432
8433 if (argvars[1].v_type != VAR_LIST)
8434 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008435 semsg(_(e_listarg), "matchaddpos()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008436 return;
8437 }
8438 l = argvars[1].vval.v_list;
8439 if (l == NULL)
8440 return;
8441
8442 if (argvars[2].v_type != VAR_UNKNOWN)
8443 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008444 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008445 if (argvars[3].v_type != VAR_UNKNOWN)
8446 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008447 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008448
8449 if (argvars[4].v_type != VAR_UNKNOWN
8450 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8451 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008452 }
8453 }
8454 if (error == TRUE)
8455 return;
8456
8457 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8458 if (id == 1 || id == 2)
8459 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008460 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008461 return;
8462 }
8463
Bram Moolenaar95e51472018-07-28 16:55:56 +02008464 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008465 conceal_char);
8466#endif
8467}
8468
8469/*
8470 * "matcharg()" function
8471 */
8472 static void
8473f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8474{
8475 if (rettv_list_alloc(rettv) == OK)
8476 {
8477#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008478 int id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008479 matchitem_T *m;
8480
8481 if (id >= 1 && id <= 3)
8482 {
8483 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8484 {
8485 list_append_string(rettv->vval.v_list,
8486 syn_id2name(m->hlg_id), -1);
8487 list_append_string(rettv->vval.v_list, m->pattern, -1);
8488 }
8489 else
8490 {
8491 list_append_string(rettv->vval.v_list, NULL, -1);
8492 list_append_string(rettv->vval.v_list, NULL, -1);
8493 }
8494 }
8495#endif
8496 }
8497}
8498
8499/*
8500 * "matchdelete()" function
8501 */
8502 static void
8503f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8504{
8505#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01008506 win_T *win = get_optional_window(argvars, 1);
8507
8508 if (win == NULL)
8509 rettv->vval.v_number = -1;
8510 else
8511 rettv->vval.v_number = match_delete(win,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008512 (int)tv_get_number(&argvars[0]), TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008513#endif
8514}
8515
8516/*
8517 * "matchend()" function
8518 */
8519 static void
8520f_matchend(typval_T *argvars, typval_T *rettv)
8521{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008522 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008523}
8524
8525/*
8526 * "matchlist()" function
8527 */
8528 static void
8529f_matchlist(typval_T *argvars, typval_T *rettv)
8530{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008531 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008532}
8533
8534/*
8535 * "matchstr()" function
8536 */
8537 static void
8538f_matchstr(typval_T *argvars, typval_T *rettv)
8539{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008540 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008541}
8542
8543/*
8544 * "matchstrpos()" function
8545 */
8546 static void
8547f_matchstrpos(typval_T *argvars, typval_T *rettv)
8548{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008549 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008550}
8551
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008552 static void
8553max_min(typval_T *argvars, typval_T *rettv, int domax)
8554{
8555 varnumber_T n = 0;
8556 varnumber_T i;
8557 int error = FALSE;
8558
8559 if (argvars[0].v_type == VAR_LIST)
8560 {
8561 list_T *l;
8562 listitem_T *li;
8563
8564 l = argvars[0].vval.v_list;
8565 if (l != NULL)
8566 {
8567 li = l->lv_first;
8568 if (li != NULL)
8569 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008570 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008571 for (;;)
8572 {
8573 li = li->li_next;
8574 if (li == NULL)
8575 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008576 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008577 if (domax ? i > n : i < n)
8578 n = i;
8579 }
8580 }
8581 }
8582 }
8583 else if (argvars[0].v_type == VAR_DICT)
8584 {
8585 dict_T *d;
8586 int first = TRUE;
8587 hashitem_T *hi;
8588 int todo;
8589
8590 d = argvars[0].vval.v_dict;
8591 if (d != NULL)
8592 {
8593 todo = (int)d->dv_hashtab.ht_used;
8594 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8595 {
8596 if (!HASHITEM_EMPTY(hi))
8597 {
8598 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008599 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008600 if (first)
8601 {
8602 n = i;
8603 first = FALSE;
8604 }
8605 else if (domax ? i > n : i < n)
8606 n = i;
8607 }
8608 }
8609 }
8610 }
8611 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008612 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008613 rettv->vval.v_number = error ? 0 : n;
8614}
8615
8616/*
8617 * "max()" function
8618 */
8619 static void
8620f_max(typval_T *argvars, typval_T *rettv)
8621{
8622 max_min(argvars, rettv, TRUE);
8623}
8624
8625/*
8626 * "min()" function
8627 */
8628 static void
8629f_min(typval_T *argvars, typval_T *rettv)
8630{
8631 max_min(argvars, rettv, FALSE);
8632}
8633
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008634/*
8635 * Create the directory in which "dir" is located, and higher levels when
8636 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008637 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008638 */
8639 static int
8640mkdir_recurse(char_u *dir, int prot)
8641{
8642 char_u *p;
8643 char_u *updir;
8644 int r = FAIL;
8645
8646 /* Get end of directory name in "dir".
8647 * We're done when it's "/" or "c:/". */
8648 p = gettail_sep(dir);
8649 if (p <= get_past_head(dir))
8650 return OK;
8651
8652 /* If the directory exists we're done. Otherwise: create it.*/
8653 updir = vim_strnsave(dir, (int)(p - dir));
8654 if (updir == NULL)
8655 return FAIL;
8656 if (mch_isdir(updir))
8657 r = OK;
8658 else if (mkdir_recurse(updir, prot) == OK)
8659 r = vim_mkdir_emsg(updir, prot);
8660 vim_free(updir);
8661 return r;
8662}
8663
8664#ifdef vim_mkdir
8665/*
8666 * "mkdir()" function
8667 */
8668 static void
8669f_mkdir(typval_T *argvars, typval_T *rettv)
8670{
8671 char_u *dir;
8672 char_u buf[NUMBUFLEN];
8673 int prot = 0755;
8674
8675 rettv->vval.v_number = FAIL;
8676 if (check_restricted() || check_secure())
8677 return;
8678
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008679 dir = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008680 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008681 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008682
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008683 if (*gettail(dir) == NUL)
8684 /* remove trailing slashes */
8685 *gettail_sep(dir) = NUL;
8686
8687 if (argvars[1].v_type != VAR_UNKNOWN)
8688 {
8689 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008690 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008691 prot = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008692 if (prot == -1)
8693 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008694 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008695 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008696 {
8697 if (mch_isdir(dir))
8698 {
8699 /* With the "p" flag it's OK if the dir already exists. */
8700 rettv->vval.v_number = OK;
8701 return;
8702 }
8703 mkdir_recurse(dir, prot);
8704 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008705 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008706 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008707}
8708#endif
8709
8710/*
8711 * "mode()" function
8712 */
8713 static void
8714f_mode(typval_T *argvars, typval_T *rettv)
8715{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008716 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008717
Bram Moolenaar612cc382018-07-29 15:34:26 +02008718 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008719
8720 if (time_for_testing == 93784)
8721 {
8722 /* Testing the two-character code. */
8723 buf[0] = 'x';
8724 buf[1] = '!';
8725 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008726#ifdef FEAT_TERMINAL
8727 else if (term_use_loop())
8728 buf[0] = 't';
8729#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008730 else if (VIsual_active)
8731 {
8732 if (VIsual_select)
8733 buf[0] = VIsual_mode + 's' - 'v';
8734 else
8735 buf[0] = VIsual_mode;
8736 }
8737 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8738 || State == CONFIRM)
8739 {
8740 buf[0] = 'r';
8741 if (State == ASKMORE)
8742 buf[1] = 'm';
8743 else if (State == CONFIRM)
8744 buf[1] = '?';
8745 }
8746 else if (State == EXTERNCMD)
8747 buf[0] = '!';
8748 else if (State & INSERT)
8749 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008750 if (State & VREPLACE_FLAG)
8751 {
8752 buf[0] = 'R';
8753 buf[1] = 'v';
8754 }
8755 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008756 {
8757 if (State & REPLACE_FLAG)
8758 buf[0] = 'R';
8759 else
8760 buf[0] = 'i';
8761#ifdef FEAT_INS_EXPAND
8762 if (ins_compl_active())
8763 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008764 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008765 buf[1] = 'x';
8766#endif
8767 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008768 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008769 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008770 {
8771 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008772 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008773 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008774 else if (exmode_active == EXMODE_NORMAL)
8775 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008776 }
8777 else
8778 {
8779 buf[0] = 'n';
8780 if (finish_op)
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008781 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008782 buf[1] = 'o';
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008783 // to be able to detect force-linewise/blockwise/characterwise operations
8784 buf[2] = motion_force;
8785 }
Bram Moolenaar612cc382018-07-29 15:34:26 +02008786 else if (restart_edit == 'I' || restart_edit == 'R'
8787 || restart_edit == 'V')
8788 {
8789 buf[1] = 'i';
8790 buf[2] = restart_edit;
8791 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008792 }
8793
8794 /* Clear out the minor mode when the argument is not a non-zero number or
8795 * non-empty string. */
8796 if (!non_zero_arg(&argvars[0]))
8797 buf[1] = NUL;
8798
8799 rettv->vval.v_string = vim_strsave(buf);
8800 rettv->v_type = VAR_STRING;
8801}
8802
8803#if defined(FEAT_MZSCHEME) || defined(PROTO)
8804/*
8805 * "mzeval()" function
8806 */
8807 static void
8808f_mzeval(typval_T *argvars, typval_T *rettv)
8809{
8810 char_u *str;
8811 char_u buf[NUMBUFLEN];
8812
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008813 if (check_restricted() || check_secure())
8814 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008815 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008816 do_mzeval(str, rettv);
8817}
8818
8819 void
8820mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8821{
8822 typval_T argvars[3];
8823
8824 argvars[0].v_type = VAR_STRING;
8825 argvars[0].vval.v_string = name;
8826 copy_tv(args, &argvars[1]);
8827 argvars[2].v_type = VAR_UNKNOWN;
8828 f_call(argvars, rettv);
8829 clear_tv(&argvars[1]);
8830}
8831#endif
8832
8833/*
8834 * "nextnonblank()" function
8835 */
8836 static void
8837f_nextnonblank(typval_T *argvars, typval_T *rettv)
8838{
8839 linenr_T lnum;
8840
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008841 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008842 {
8843 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8844 {
8845 lnum = 0;
8846 break;
8847 }
8848 if (*skipwhite(ml_get(lnum)) != NUL)
8849 break;
8850 }
8851 rettv->vval.v_number = lnum;
8852}
8853
8854/*
8855 * "nr2char()" function
8856 */
8857 static void
8858f_nr2char(typval_T *argvars, typval_T *rettv)
8859{
8860 char_u buf[NUMBUFLEN];
8861
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008862 if (has_mbyte)
8863 {
8864 int utf8 = 0;
8865
8866 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008867 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008868 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01008869 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008870 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008871 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008872 }
8873 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008874 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008875 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008876 buf[1] = NUL;
8877 }
8878 rettv->v_type = VAR_STRING;
8879 rettv->vval.v_string = vim_strsave(buf);
8880}
8881
8882/*
8883 * "or(expr, expr)" function
8884 */
8885 static void
8886f_or(typval_T *argvars, typval_T *rettv)
8887{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008888 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
8889 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008890}
8891
8892/*
8893 * "pathshorten()" function
8894 */
8895 static void
8896f_pathshorten(typval_T *argvars, typval_T *rettv)
8897{
8898 char_u *p;
8899
8900 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008901 p = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008902 if (p == NULL)
8903 rettv->vval.v_string = NULL;
8904 else
8905 {
8906 p = vim_strsave(p);
8907 rettv->vval.v_string = p;
8908 if (p != NULL)
8909 shorten_dir(p);
8910 }
8911}
8912
8913#ifdef FEAT_PERL
8914/*
8915 * "perleval()" function
8916 */
8917 static void
8918f_perleval(typval_T *argvars, typval_T *rettv)
8919{
8920 char_u *str;
8921 char_u buf[NUMBUFLEN];
8922
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008923 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008924 do_perleval(str, rettv);
8925}
8926#endif
8927
8928#ifdef FEAT_FLOAT
8929/*
8930 * "pow()" function
8931 */
8932 static void
8933f_pow(typval_T *argvars, typval_T *rettv)
8934{
8935 float_T fx = 0.0, fy = 0.0;
8936
8937 rettv->v_type = VAR_FLOAT;
8938 if (get_float_arg(argvars, &fx) == OK
8939 && get_float_arg(&argvars[1], &fy) == OK)
8940 rettv->vval.v_float = pow(fx, fy);
8941 else
8942 rettv->vval.v_float = 0.0;
8943}
8944#endif
8945
8946/*
8947 * "prevnonblank()" function
8948 */
8949 static void
8950f_prevnonblank(typval_T *argvars, typval_T *rettv)
8951{
8952 linenr_T lnum;
8953
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008954 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008955 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8956 lnum = 0;
8957 else
8958 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8959 --lnum;
8960 rettv->vval.v_number = lnum;
8961}
8962
8963/* This dummy va_list is here because:
8964 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8965 * - locally in the function results in a "used before set" warning
8966 * - using va_start() to initialize it gives "function with fixed args" error */
8967static va_list ap;
8968
8969/*
8970 * "printf()" function
8971 */
8972 static void
8973f_printf(typval_T *argvars, typval_T *rettv)
8974{
8975 char_u buf[NUMBUFLEN];
8976 int len;
8977 char_u *s;
8978 int saved_did_emsg = did_emsg;
8979 char *fmt;
8980
8981 rettv->v_type = VAR_STRING;
8982 rettv->vval.v_string = NULL;
8983
8984 /* Get the required length, allocate the buffer and do it for real. */
8985 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008986 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008987 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008988 if (!did_emsg)
8989 {
8990 s = alloc(len + 1);
8991 if (s != NULL)
8992 {
8993 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008994 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8995 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008996 }
8997 }
8998 did_emsg |= saved_did_emsg;
8999}
9000
Bram Moolenaarf2732452018-06-03 14:47:35 +02009001#ifdef FEAT_JOB_CHANNEL
9002/*
9003 * "prompt_setcallback({buffer}, {callback})" function
9004 */
9005 static void
9006f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
9007{
9008 buf_T *buf;
9009 char_u *callback;
9010 partial_T *partial;
9011
9012 if (check_secure())
9013 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009014 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009015 if (buf == NULL)
9016 return;
9017
9018 callback = get_callback(&argvars[1], &partial);
9019 if (callback == NULL)
9020 return;
9021
9022 free_callback(buf->b_prompt_callback, buf->b_prompt_partial);
9023 if (partial == NULL)
9024 buf->b_prompt_callback = vim_strsave(callback);
9025 else
9026 /* pointer into the partial */
9027 buf->b_prompt_callback = callback;
9028 buf->b_prompt_partial = partial;
9029}
9030
9031/*
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009032 * "prompt_setinterrupt({buffer}, {callback})" function
9033 */
9034 static void
9035f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
9036{
9037 buf_T *buf;
9038 char_u *callback;
9039 partial_T *partial;
9040
9041 if (check_secure())
9042 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009043 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009044 if (buf == NULL)
9045 return;
9046
9047 callback = get_callback(&argvars[1], &partial);
9048 if (callback == NULL)
9049 return;
9050
9051 free_callback(buf->b_prompt_interrupt, buf->b_prompt_int_partial);
9052 if (partial == NULL)
9053 buf->b_prompt_interrupt = vim_strsave(callback);
9054 else
9055 /* pointer into the partial */
9056 buf->b_prompt_interrupt = callback;
9057 buf->b_prompt_int_partial = partial;
9058}
9059
9060/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02009061 * "prompt_setprompt({buffer}, {text})" function
9062 */
9063 static void
9064f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
9065{
9066 buf_T *buf;
9067 char_u *text;
9068
9069 if (check_secure())
9070 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009071 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009072 if (buf == NULL)
9073 return;
9074
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009075 text = tv_get_string(&argvars[1]);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009076 vim_free(buf->b_prompt_text);
9077 buf->b_prompt_text = vim_strsave(text);
9078}
9079#endif
9080
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009081/*
9082 * "pumvisible()" function
9083 */
9084 static void
9085f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9086{
9087#ifdef FEAT_INS_EXPAND
9088 if (pum_visible())
9089 rettv->vval.v_number = 1;
9090#endif
9091}
9092
9093#ifdef FEAT_PYTHON3
9094/*
9095 * "py3eval()" function
9096 */
9097 static void
9098f_py3eval(typval_T *argvars, typval_T *rettv)
9099{
9100 char_u *str;
9101 char_u buf[NUMBUFLEN];
9102
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009103 if (check_restricted() || check_secure())
9104 return;
9105
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009106 if (p_pyx == 0)
9107 p_pyx = 3;
9108
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009109 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009110 do_py3eval(str, rettv);
9111}
9112#endif
9113
9114#ifdef FEAT_PYTHON
9115/*
9116 * "pyeval()" function
9117 */
9118 static void
9119f_pyeval(typval_T *argvars, typval_T *rettv)
9120{
9121 char_u *str;
9122 char_u buf[NUMBUFLEN];
9123
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009124 if (check_restricted() || check_secure())
9125 return;
9126
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009127 if (p_pyx == 0)
9128 p_pyx = 2;
9129
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009130 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009131 do_pyeval(str, rettv);
9132}
9133#endif
9134
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009135#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
9136/*
9137 * "pyxeval()" function
9138 */
9139 static void
9140f_pyxeval(typval_T *argvars, typval_T *rettv)
9141{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009142 if (check_restricted() || check_secure())
9143 return;
9144
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009145# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
9146 init_pyxversion();
9147 if (p_pyx == 2)
9148 f_pyeval(argvars, rettv);
9149 else
9150 f_py3eval(argvars, rettv);
9151# elif defined(FEAT_PYTHON)
9152 f_pyeval(argvars, rettv);
9153# elif defined(FEAT_PYTHON3)
9154 f_py3eval(argvars, rettv);
9155# endif
9156}
9157#endif
9158
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009159/*
9160 * "range()" function
9161 */
9162 static void
9163f_range(typval_T *argvars, typval_T *rettv)
9164{
9165 varnumber_T start;
9166 varnumber_T end;
9167 varnumber_T stride = 1;
9168 varnumber_T i;
9169 int error = FALSE;
9170
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009171 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009172 if (argvars[1].v_type == VAR_UNKNOWN)
9173 {
9174 end = start - 1;
9175 start = 0;
9176 }
9177 else
9178 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009179 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009180 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009181 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009182 }
9183
9184 if (error)
9185 return; /* type error; errmsg already given */
9186 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009187 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009188 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009189 emsg(_("E727: Start past end"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009190 else
9191 {
9192 if (rettv_list_alloc(rettv) == OK)
9193 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
9194 if (list_append_number(rettv->vval.v_list,
9195 (varnumber_T)i) == FAIL)
9196 break;
9197 }
9198}
9199
9200/*
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009201 * Evaluate "expr" for readdir().
9202 */
9203 static int
9204readdir_checkitem(typval_T *expr, char_u *name)
9205{
9206 typval_T save_val;
9207 typval_T rettv;
9208 typval_T argv[2];
9209 int retval = 0;
9210 int error = FALSE;
9211
9212 prepare_vimvar(VV_VAL, &save_val);
9213 set_vim_var_string(VV_VAL, name, -1);
9214 argv[0].v_type = VAR_STRING;
9215 argv[0].vval.v_string = name;
9216
9217 if (eval_expr_typval(expr, argv, 1, &rettv) == FAIL)
9218 goto theend;
9219
9220 retval = tv_get_number_chk(&rettv, &error);
9221 if (error)
9222 retval = -1;
9223 clear_tv(&rettv);
9224
9225theend:
9226 set_vim_var_string(VV_VAL, NULL, 0);
9227 restore_vimvar(VV_VAL, &save_val);
9228 return retval;
9229}
9230
9231/*
9232 * "readdir()" function
9233 */
9234 static void
9235f_readdir(typval_T *argvars, typval_T *rettv)
9236{
9237 typval_T *expr;
9238 int failed = FALSE;
9239 char_u *path;
9240 garray_T ga;
9241 int i;
9242#ifdef MSWIN
9243 char_u *buf, *p;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009244 int ok;
9245 HANDLE hFind = INVALID_HANDLE_VALUE;
9246 WIN32_FIND_DATAW wfb;
9247 WCHAR *wn = NULL; // UCS-2 name, NULL when not used.
9248#endif
9249
9250 if (rettv_list_alloc(rettv) == FAIL)
9251 return;
9252 path = tv_get_string(&argvars[0]);
9253 expr = &argvars[1];
9254 ga_init2(&ga, (int)sizeof(char *), 20);
9255
9256#ifdef MSWIN
9257 buf = alloc((int)MAXPATHL);
9258 if (buf == NULL)
9259 return;
9260 STRNCPY(buf, path, MAXPATHL-5);
9261 p = vim_strpbrk(path, (char_u *)"\\/");
9262 if (p != NULL)
9263 *p = NUL;
9264 STRCAT(buf, "\\*");
9265
9266 wn = enc_to_utf16(buf, NULL);
9267 if (wn != NULL)
9268 hFind = FindFirstFileW(wn, &wfb);
9269 ok = (hFind != INVALID_HANDLE_VALUE);
9270 if (!ok)
9271 smsg(_(e_notopen), path);
9272 else
9273 {
9274 while (ok)
9275 {
9276 int ignore;
9277
9278 p = utf16_to_enc(wfb.cFileName, NULL); // p is allocated here
9279 if (p == NULL)
9280 break; // out of memory
9281
9282 ignore = p[0] == '.' && (p[1] == NUL
9283 || (p[1] == '.' && p[2] == NUL));
9284 if (!ignore && expr->v_type != VAR_UNKNOWN)
9285 {
9286 int r = readdir_checkitem(expr, p);
9287
9288 if (r < 0)
9289 {
9290 vim_free(p);
9291 break;
9292 }
9293 if (r == 0)
9294 ignore = TRUE;
9295 }
9296
9297 if (!ignore)
9298 {
9299 if (ga_grow(&ga, 1) == OK)
9300 ((char_u**)ga.ga_data)[ga.ga_len++] = vim_strsave(p);
9301 else
9302 {
9303 failed = TRUE;
9304 vim_free(p);
9305 break;
9306 }
9307 }
9308
9309 vim_free(p);
9310 ok = FindNextFileW(hFind, &wfb);
9311 }
9312 FindClose(hFind);
9313 }
9314
9315 vim_free(buf);
9316 vim_free(wn);
9317#else
9318 DIR *dirp;
9319 struct dirent *dp;
9320 char_u *p;
9321
9322 dirp = opendir((char *)path);
9323 if (dirp == NULL)
9324 smsg(_(e_notopen), path);
9325 else
9326 {
9327 for (;;)
9328 {
9329 int ignore;
9330
9331 dp = readdir(dirp);
9332 if (dp == NULL)
9333 break;
9334 p = (char_u *)dp->d_name;
9335
9336 ignore = p[0] == '.' &&
9337 (p[1] == NUL ||
9338 (p[1] == '.' && p[2] == NUL));
9339 if (!ignore && expr->v_type != VAR_UNKNOWN)
9340 {
9341 int r = readdir_checkitem(expr, p);
9342
9343 if (r < 0)
9344 break;
9345 if (r == 0)
9346 ignore = TRUE;
9347 }
9348
9349 if (!ignore)
9350 {
9351 if (ga_grow(&ga, 1) == OK)
9352 ((char_u**)ga.ga_data)[ga.ga_len++] = vim_strsave(p);
9353 else
9354 {
9355 failed = TRUE;
9356 break;
9357 }
9358 }
9359 }
9360
9361 closedir(dirp);
9362 }
9363#endif
9364
Bram Moolenaar334ad412019-04-19 15:20:46 +02009365 if (!failed && rettv->vval.v_list != NULL && ga.ga_len > 0)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009366 {
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009367 sort_strings((char_u **)ga.ga_data, ga.ga_len);
9368 for (i = 0; i < ga.ga_len; i++)
9369 {
9370 p = ((char_u **)ga.ga_data)[i];
9371 list_append_string(rettv->vval.v_list, p, -1);
9372 }
9373 }
Bram Moolenaar334ad412019-04-19 15:20:46 +02009374 ga_clear_strings(&ga);
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009375}
9376
9377/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009378 * "readfile()" function
9379 */
9380 static void
9381f_readfile(typval_T *argvars, typval_T *rettv)
9382{
9383 int binary = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009384 int blob = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009385 int failed = FALSE;
9386 char_u *fname;
9387 FILE *fd;
9388 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
9389 int io_size = sizeof(buf);
9390 int readlen; /* size of last fread() */
9391 char_u *prev = NULL; /* previously read bytes, if any */
9392 long prevlen = 0; /* length of data in prev */
9393 long prevsize = 0; /* size of prev buffer */
9394 long maxline = MAXLNUM;
9395 long cnt = 0;
9396 char_u *p; /* position in buf */
9397 char_u *start; /* start of current line */
9398
9399 if (argvars[1].v_type != VAR_UNKNOWN)
9400 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009401 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009402 binary = TRUE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009403 if (STRCMP(tv_get_string(&argvars[1]), "B") == 0)
9404 blob = TRUE;
9405
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009406 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009407 maxline = (long)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009408 }
9409
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009410 if (blob)
9411 {
9412 if (rettv_blob_alloc(rettv) == FAIL)
9413 return;
9414 }
9415 else
9416 {
9417 if (rettv_list_alloc(rettv) == FAIL)
9418 return;
9419 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009420
9421 /* Always open the file in binary mode, library functions have a mind of
9422 * their own about CR-LF conversion. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009423 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009424 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
9425 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009426 semsg(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009427 return;
9428 }
9429
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009430 if (blob)
9431 {
9432 if (read_blob(fd, rettv->vval.v_blob) == FAIL)
9433 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009434 emsg("cannot read file");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009435 blob_free(rettv->vval.v_blob);
9436 }
9437 fclose(fd);
9438 return;
9439 }
9440
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009441 while (cnt < maxline || maxline < 0)
9442 {
9443 readlen = (int)fread(buf, 1, io_size, fd);
9444
9445 /* This for loop processes what was read, but is also entered at end
9446 * of file so that either:
9447 * - an incomplete line gets written
9448 * - a "binary" file gets an empty line at the end if it ends in a
9449 * newline. */
9450 for (p = buf, start = buf;
9451 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
9452 ++p)
9453 {
9454 if (*p == '\n' || readlen <= 0)
9455 {
9456 listitem_T *li;
9457 char_u *s = NULL;
9458 long_u len = p - start;
9459
9460 /* Finished a line. Remove CRs before NL. */
9461 if (readlen > 0 && !binary)
9462 {
9463 while (len > 0 && start[len - 1] == '\r')
9464 --len;
9465 /* removal may cross back to the "prev" string */
9466 if (len == 0)
9467 while (prevlen > 0 && prev[prevlen - 1] == '\r')
9468 --prevlen;
9469 }
9470 if (prevlen == 0)
9471 s = vim_strnsave(start, (int)len);
9472 else
9473 {
9474 /* Change "prev" buffer to be the right size. This way
9475 * the bytes are only copied once, and very long lines are
9476 * allocated only once. */
9477 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
9478 {
9479 mch_memmove(s + prevlen, start, len);
9480 s[prevlen + len] = NUL;
9481 prev = NULL; /* the list will own the string */
9482 prevlen = prevsize = 0;
9483 }
9484 }
9485 if (s == NULL)
9486 {
9487 do_outofmem_msg((long_u) prevlen + len + 1);
9488 failed = TRUE;
9489 break;
9490 }
9491
9492 if ((li = listitem_alloc()) == NULL)
9493 {
9494 vim_free(s);
9495 failed = TRUE;
9496 break;
9497 }
9498 li->li_tv.v_type = VAR_STRING;
9499 li->li_tv.v_lock = 0;
9500 li->li_tv.vval.v_string = s;
9501 list_append(rettv->vval.v_list, li);
9502
9503 start = p + 1; /* step over newline */
9504 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
9505 break;
9506 }
9507 else if (*p == NUL)
9508 *p = '\n';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009509 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
9510 * when finding the BF and check the previous two bytes. */
9511 else if (*p == 0xbf && enc_utf8 && !binary)
9512 {
9513 /* Find the two bytes before the 0xbf. If p is at buf, or buf
9514 * + 1, these may be in the "prev" string. */
9515 char_u back1 = p >= buf + 1 ? p[-1]
9516 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
9517 char_u back2 = p >= buf + 2 ? p[-2]
9518 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
9519 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
9520
9521 if (back2 == 0xef && back1 == 0xbb)
9522 {
9523 char_u *dest = p - 2;
9524
9525 /* Usually a BOM is at the beginning of a file, and so at
9526 * the beginning of a line; then we can just step over it.
9527 */
9528 if (start == dest)
9529 start = p + 1;
9530 else
9531 {
9532 /* have to shuffle buf to close gap */
9533 int adjust_prevlen = 0;
9534
9535 if (dest < buf)
9536 {
9537 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
9538 dest = buf;
9539 }
9540 if (readlen > p - buf + 1)
9541 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
9542 readlen -= 3 - adjust_prevlen;
9543 prevlen -= adjust_prevlen;
9544 p = dest - 1;
9545 }
9546 }
9547 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009548 } /* for */
9549
9550 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
9551 break;
9552 if (start < p)
9553 {
9554 /* There's part of a line in buf, store it in "prev". */
9555 if (p - start + prevlen >= prevsize)
9556 {
9557 /* need bigger "prev" buffer */
9558 char_u *newprev;
9559
9560 /* A common use case is ordinary text files and "prev" gets a
9561 * fragment of a line, so the first allocation is made
9562 * small, to avoid repeatedly 'allocing' large and
9563 * 'reallocing' small. */
9564 if (prevsize == 0)
9565 prevsize = (long)(p - start);
9566 else
9567 {
9568 long grow50pc = (prevsize * 3) / 2;
9569 long growmin = (long)((p - start) * 2 + prevlen);
9570 prevsize = grow50pc > growmin ? grow50pc : growmin;
9571 }
9572 newprev = prev == NULL ? alloc(prevsize)
9573 : vim_realloc(prev, prevsize);
9574 if (newprev == NULL)
9575 {
9576 do_outofmem_msg((long_u)prevsize);
9577 failed = TRUE;
9578 break;
9579 }
9580 prev = newprev;
9581 }
9582 /* Add the line part to end of "prev". */
9583 mch_memmove(prev + prevlen, start, p - start);
9584 prevlen += (long)(p - start);
9585 }
9586 } /* while */
9587
9588 /*
9589 * For a negative line count use only the lines at the end of the file,
9590 * free the rest.
9591 */
9592 if (!failed && maxline < 0)
9593 while (cnt > -maxline)
9594 {
9595 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
9596 --cnt;
9597 }
9598
9599 if (failed)
9600 {
9601 list_free(rettv->vval.v_list);
9602 /* readfile doc says an empty list is returned on error */
9603 rettv->vval.v_list = list_alloc();
9604 }
9605
9606 vim_free(prev);
9607 fclose(fd);
9608}
9609
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009610 static void
9611return_register(int regname, typval_T *rettv)
9612{
9613 char_u buf[2] = {0, 0};
9614
9615 buf[0] = (char_u)regname;
9616 rettv->v_type = VAR_STRING;
9617 rettv->vval.v_string = vim_strsave(buf);
9618}
9619
9620/*
9621 * "reg_executing()" function
9622 */
9623 static void
9624f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9625{
9626 return_register(reg_executing, rettv);
9627}
9628
9629/*
9630 * "reg_recording()" function
9631 */
9632 static void
9633f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9634{
9635 return_register(reg_recording, rettv);
9636}
9637
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009638#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009639/*
9640 * Convert a List to proftime_T.
9641 * Return FAIL when there is something wrong.
9642 */
9643 static int
9644list2proftime(typval_T *arg, proftime_T *tm)
9645{
9646 long n1, n2;
9647 int error = FALSE;
9648
9649 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9650 || arg->vval.v_list->lv_len != 2)
9651 return FAIL;
9652 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9653 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009654# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009655 tm->HighPart = n1;
9656 tm->LowPart = n2;
9657# else
9658 tm->tv_sec = n1;
9659 tm->tv_usec = n2;
9660# endif
9661 return error ? FAIL : OK;
9662}
9663#endif /* FEAT_RELTIME */
9664
9665/*
9666 * "reltime()" function
9667 */
9668 static void
9669f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9670{
9671#ifdef FEAT_RELTIME
9672 proftime_T res;
9673 proftime_T start;
9674
9675 if (argvars[0].v_type == VAR_UNKNOWN)
9676 {
9677 /* No arguments: get current time. */
9678 profile_start(&res);
9679 }
9680 else if (argvars[1].v_type == VAR_UNKNOWN)
9681 {
9682 if (list2proftime(&argvars[0], &res) == FAIL)
9683 return;
9684 profile_end(&res);
9685 }
9686 else
9687 {
9688 /* Two arguments: compute the difference. */
9689 if (list2proftime(&argvars[0], &start) == FAIL
9690 || list2proftime(&argvars[1], &res) == FAIL)
9691 return;
9692 profile_sub(&res, &start);
9693 }
9694
9695 if (rettv_list_alloc(rettv) == OK)
9696 {
9697 long n1, n2;
9698
Bram Moolenaar4f974752019-02-17 17:44:42 +01009699# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009700 n1 = res.HighPart;
9701 n2 = res.LowPart;
9702# else
9703 n1 = res.tv_sec;
9704 n2 = res.tv_usec;
9705# endif
9706 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9707 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9708 }
9709#endif
9710}
9711
9712#ifdef FEAT_FLOAT
9713/*
9714 * "reltimefloat()" function
9715 */
9716 static void
9717f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9718{
9719# ifdef FEAT_RELTIME
9720 proftime_T tm;
9721# endif
9722
9723 rettv->v_type = VAR_FLOAT;
9724 rettv->vval.v_float = 0;
9725# ifdef FEAT_RELTIME
9726 if (list2proftime(&argvars[0], &tm) == OK)
9727 rettv->vval.v_float = profile_float(&tm);
9728# endif
9729}
9730#endif
9731
9732/*
9733 * "reltimestr()" function
9734 */
9735 static void
9736f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9737{
9738#ifdef FEAT_RELTIME
9739 proftime_T tm;
9740#endif
9741
9742 rettv->v_type = VAR_STRING;
9743 rettv->vval.v_string = NULL;
9744#ifdef FEAT_RELTIME
9745 if (list2proftime(&argvars[0], &tm) == OK)
9746 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9747#endif
9748}
9749
9750#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009751 static void
9752make_connection(void)
9753{
9754 if (X_DISPLAY == NULL
9755# ifdef FEAT_GUI
9756 && !gui.in_use
9757# endif
9758 )
9759 {
9760 x_force_connect = TRUE;
9761 setup_term_clip();
9762 x_force_connect = FALSE;
9763 }
9764}
9765
9766 static int
9767check_connection(void)
9768{
9769 make_connection();
9770 if (X_DISPLAY == NULL)
9771 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009772 emsg(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009773 return FAIL;
9774 }
9775 return OK;
9776}
9777#endif
9778
9779#ifdef FEAT_CLIENTSERVER
9780 static void
9781remote_common(typval_T *argvars, typval_T *rettv, int expr)
9782{
9783 char_u *server_name;
9784 char_u *keys;
9785 char_u *r = NULL;
9786 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009787 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009788# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009789 HWND w;
9790# else
9791 Window w;
9792# endif
9793
9794 if (check_restricted() || check_secure())
9795 return;
9796
9797# ifdef FEAT_X11
9798 if (check_connection() == FAIL)
9799 return;
9800# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009801 if (argvars[2].v_type != VAR_UNKNOWN
9802 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009803 timeout = tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009804
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009805 server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009806 if (server_name == NULL)
9807 return; /* type error; errmsg already given */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009808 keys = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009809# ifdef MSWIN
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009810 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009811# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009812 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9813 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009814# endif
9815 {
9816 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009817 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009818 emsg((char *)r); // sending worked but evaluation failed
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009819 vim_free(r);
9820 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009821 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009822 semsg(_("E241: Unable to send to %s"), server_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009823 return;
9824 }
9825
9826 rettv->vval.v_string = r;
9827
9828 if (argvars[2].v_type != VAR_UNKNOWN)
9829 {
9830 dictitem_T v;
9831 char_u str[30];
9832 char_u *idvar;
9833
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009834 idvar = tv_get_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009835 if (idvar != NULL && *idvar != NUL)
9836 {
9837 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9838 v.di_tv.v_type = VAR_STRING;
9839 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009840 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009841 vim_free(v.di_tv.vval.v_string);
9842 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009843 }
9844}
9845#endif
9846
9847/*
9848 * "remote_expr()" function
9849 */
9850 static void
9851f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9852{
9853 rettv->v_type = VAR_STRING;
9854 rettv->vval.v_string = NULL;
9855#ifdef FEAT_CLIENTSERVER
9856 remote_common(argvars, rettv, TRUE);
9857#endif
9858}
9859
9860/*
9861 * "remote_foreground()" function
9862 */
9863 static void
9864f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9865{
9866#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +01009867# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009868 /* On Win32 it's done in this application. */
9869 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009870 char_u *server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009871
9872 if (server_name != NULL)
9873 serverForeground(server_name);
9874 }
9875# else
9876 /* Send a foreground() expression to the server. */
9877 argvars[1].v_type = VAR_STRING;
9878 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9879 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009880 rettv->v_type = VAR_STRING;
9881 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009882 remote_common(argvars, rettv, TRUE);
9883 vim_free(argvars[1].vval.v_string);
9884# endif
9885#endif
9886}
9887
9888 static void
9889f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
9890{
9891#ifdef FEAT_CLIENTSERVER
9892 dictitem_T v;
9893 char_u *s = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009894# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009895 long_u n = 0;
9896# endif
9897 char_u *serverid;
9898
9899 if (check_restricted() || check_secure())
9900 {
9901 rettv->vval.v_number = -1;
9902 return;
9903 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009904 serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009905 if (serverid == NULL)
9906 {
9907 rettv->vval.v_number = -1;
9908 return; /* type error; errmsg already given */
9909 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01009910# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009911 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
9912 if (n == 0)
9913 rettv->vval.v_number = -1;
9914 else
9915 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009916 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009917 rettv->vval.v_number = (s != NULL);
9918 }
9919# else
9920 if (check_connection() == FAIL)
9921 return;
9922
9923 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
9924 serverStrToWin(serverid), &s);
9925# endif
9926
9927 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
9928 {
9929 char_u *retvar;
9930
9931 v.di_tv.v_type = VAR_STRING;
9932 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009933 retvar = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009934 if (retvar != NULL)
9935 set_var(retvar, &v.di_tv, FALSE);
9936 vim_free(v.di_tv.vval.v_string);
9937 }
9938#else
9939 rettv->vval.v_number = -1;
9940#endif
9941}
9942
9943 static void
9944f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
9945{
9946 char_u *r = NULL;
9947
9948#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009949 char_u *serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009950
9951 if (serverid != NULL && !check_restricted() && !check_secure())
9952 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009953 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009954# ifdef MSWIN
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009955 /* The server's HWND is encoded in the 'id' parameter */
9956 long_u n = 0;
9957# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009958
9959 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009960 timeout = tv_get_number(&argvars[1]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009961
Bram Moolenaar4f974752019-02-17 17:44:42 +01009962# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009963 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
9964 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009965 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009966 if (r == NULL)
9967# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009968 if (check_connection() == FAIL
9969 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
9970 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009971# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009972 emsg(_("E277: Unable to read a server reply"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009973 }
9974#endif
9975 rettv->v_type = VAR_STRING;
9976 rettv->vval.v_string = r;
9977}
9978
9979/*
9980 * "remote_send()" function
9981 */
9982 static void
9983f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9984{
9985 rettv->v_type = VAR_STRING;
9986 rettv->vval.v_string = NULL;
9987#ifdef FEAT_CLIENTSERVER
9988 remote_common(argvars, rettv, FALSE);
9989#endif
9990}
9991
9992/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009993 * "remote_startserver()" function
9994 */
9995 static void
9996f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9997{
9998#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009999 char_u *server = tv_get_string_chk(&argvars[0]);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010000
10001 if (server == NULL)
10002 return; /* type error; errmsg already given */
10003 if (serverName != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010004 emsg(_("E941: already started a server"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010005 else
10006 {
10007# ifdef FEAT_X11
10008 if (check_connection() == OK)
10009 serverRegisterName(X_DISPLAY, server);
10010# else
10011 serverSetName(server);
10012# endif
10013 }
10014#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010015 emsg(_("E942: +clientserver feature not available"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010016#endif
10017}
10018
10019/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010020 * "remove()" function
10021 */
10022 static void
10023f_remove(typval_T *argvars, typval_T *rettv)
10024{
10025 list_T *l;
10026 listitem_T *item, *item2;
10027 listitem_T *li;
10028 long idx;
10029 long end;
10030 char_u *key;
10031 dict_T *d;
10032 dictitem_T *di;
10033 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010034 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010035
10036 if (argvars[0].v_type == VAR_DICT)
10037 {
10038 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010039 semsg(_(e_toomanyarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010040 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010041 && !var_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010042 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010043 key = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010044 if (key != NULL)
10045 {
10046 di = dict_find(d, key, -1);
10047 if (di == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010048 semsg(_(e_dictkey), key);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010049 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10050 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
10051 {
10052 *rettv = di->di_tv;
10053 init_tv(&di->di_tv);
10054 dictitem_remove(d, di);
10055 }
10056 }
10057 }
10058 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010059 else if (argvars[0].v_type == VAR_BLOB)
10060 {
10061 idx = (long)tv_get_number_chk(&argvars[1], &error);
10062 if (!error)
10063 {
10064 blob_T *b = argvars[0].vval.v_blob;
10065 int len = blob_len(b);
10066 char_u *p;
10067
10068 if (idx < 0)
10069 // count from the end
10070 idx = len + idx;
10071 if (idx < 0 || idx >= len)
10072 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010073 semsg(_(e_blobidx), idx);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010074 return;
10075 }
10076 if (argvars[2].v_type == VAR_UNKNOWN)
10077 {
10078 // Remove one item, return its value.
10079 p = (char_u *)b->bv_ga.ga_data;
10080 rettv->vval.v_number = (varnumber_T) *(p + idx);
10081 mch_memmove(p + idx, p + idx + 1, (size_t)len - idx - 1);
10082 --b->bv_ga.ga_len;
10083 }
10084 else
10085 {
10086 blob_T *blob;
10087
10088 // Remove range of items, return list with values.
10089 end = (long)tv_get_number_chk(&argvars[2], &error);
10090 if (error)
10091 return;
10092 if (end < 0)
10093 // count from the end
10094 end = len + end;
10095 if (end >= len || idx > end)
10096 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010097 semsg(_(e_blobidx), end);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010098 return;
10099 }
10100 blob = blob_alloc();
10101 if (blob == NULL)
10102 return;
10103 blob->bv_ga.ga_len = end - idx + 1;
10104 if (ga_grow(&blob->bv_ga, end - idx + 1) == FAIL)
10105 {
10106 vim_free(blob);
10107 return;
10108 }
10109 p = (char_u *)b->bv_ga.ga_data;
10110 mch_memmove((char_u *)blob->bv_ga.ga_data, p + idx,
10111 (size_t)(end - idx + 1));
10112 ++blob->bv_refcount;
10113 rettv->v_type = VAR_BLOB;
10114 rettv->vval.v_blob = blob;
10115
10116 mch_memmove(p + idx, p + end + 1, (size_t)(len - end));
10117 b->bv_ga.ga_len -= end - idx + 1;
10118 }
10119 }
10120 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010121 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010122 semsg(_(e_listdictblobarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010123 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010124 && !var_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010125 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010126 idx = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010127 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010128 ; // type error: do nothing, errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010129 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010130 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010131 else
10132 {
10133 if (argvars[2].v_type == VAR_UNKNOWN)
10134 {
10135 /* Remove one item, return its value. */
10136 vimlist_remove(l, item, item);
10137 *rettv = item->li_tv;
10138 vim_free(item);
10139 }
10140 else
10141 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010142 // Remove range of items, return list with values.
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010143 end = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010144 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010145 ; // type error: do nothing
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010146 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010147 semsg(_(e_listidx), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010148 else
10149 {
10150 int cnt = 0;
10151
10152 for (li = item; li != NULL; li = li->li_next)
10153 {
10154 ++cnt;
10155 if (li == item2)
10156 break;
10157 }
10158 if (li == NULL) /* didn't find "item2" after "item" */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010159 emsg(_(e_invrange));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010160 else
10161 {
10162 vimlist_remove(l, item, item2);
10163 if (rettv_list_alloc(rettv) == OK)
10164 {
10165 l = rettv->vval.v_list;
10166 l->lv_first = item;
10167 l->lv_last = item2;
10168 item->li_prev = NULL;
10169 item2->li_next = NULL;
10170 l->lv_len = cnt;
10171 }
10172 }
10173 }
10174 }
10175 }
10176 }
10177}
10178
10179/*
10180 * "rename({from}, {to})" function
10181 */
10182 static void
10183f_rename(typval_T *argvars, typval_T *rettv)
10184{
10185 char_u buf[NUMBUFLEN];
10186
10187 if (check_restricted() || check_secure())
10188 rettv->vval.v_number = -1;
10189 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010190 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
10191 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010192}
10193
10194/*
10195 * "repeat()" function
10196 */
10197 static void
10198f_repeat(typval_T *argvars, typval_T *rettv)
10199{
10200 char_u *p;
10201 int n;
10202 int slen;
10203 int len;
10204 char_u *r;
10205 int i;
10206
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010207 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010208 if (argvars[0].v_type == VAR_LIST)
10209 {
10210 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
10211 while (n-- > 0)
10212 if (list_extend(rettv->vval.v_list,
10213 argvars[0].vval.v_list, NULL) == FAIL)
10214 break;
10215 }
10216 else
10217 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010218 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010219 rettv->v_type = VAR_STRING;
10220 rettv->vval.v_string = NULL;
10221
10222 slen = (int)STRLEN(p);
10223 len = slen * n;
10224 if (len <= 0)
10225 return;
10226
10227 r = alloc(len + 1);
10228 if (r != NULL)
10229 {
10230 for (i = 0; i < n; i++)
10231 mch_memmove(r + i * slen, p, (size_t)slen);
10232 r[len] = NUL;
10233 }
10234
10235 rettv->vval.v_string = r;
10236 }
10237}
10238
10239/*
10240 * "resolve()" function
10241 */
10242 static void
10243f_resolve(typval_T *argvars, typval_T *rettv)
10244{
10245 char_u *p;
10246#ifdef HAVE_READLINK
10247 char_u *buf = NULL;
10248#endif
10249
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010250 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010251#ifdef FEAT_SHORTCUT
10252 {
10253 char_u *v = NULL;
10254
Bram Moolenaardce1e892019-02-10 23:18:53 +010010255 v = mch_resolve_path(p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010256 if (v != NULL)
10257 rettv->vval.v_string = v;
10258 else
10259 rettv->vval.v_string = vim_strsave(p);
10260 }
10261#else
10262# ifdef HAVE_READLINK
10263 {
10264 char_u *cpy;
10265 int len;
10266 char_u *remain = NULL;
10267 char_u *q;
10268 int is_relative_to_current = FALSE;
10269 int has_trailing_pathsep = FALSE;
10270 int limit = 100;
10271
10272 p = vim_strsave(p);
10273
10274 if (p[0] == '.' && (vim_ispathsep(p[1])
10275 || (p[1] == '.' && (vim_ispathsep(p[2])))))
10276 is_relative_to_current = TRUE;
10277
10278 len = STRLEN(p);
10279 if (len > 0 && after_pathsep(p, p + len))
10280 {
10281 has_trailing_pathsep = TRUE;
10282 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
10283 }
10284
10285 q = getnextcomp(p);
10286 if (*q != NUL)
10287 {
10288 /* Separate the first path component in "p", and keep the
10289 * remainder (beginning with the path separator). */
10290 remain = vim_strsave(q - 1);
10291 q[-1] = NUL;
10292 }
10293
10294 buf = alloc(MAXPATHL + 1);
10295 if (buf == NULL)
10296 goto fail;
10297
10298 for (;;)
10299 {
10300 for (;;)
10301 {
10302 len = readlink((char *)p, (char *)buf, MAXPATHL);
10303 if (len <= 0)
10304 break;
10305 buf[len] = NUL;
10306
10307 if (limit-- == 0)
10308 {
10309 vim_free(p);
10310 vim_free(remain);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010311 emsg(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010312 rettv->vval.v_string = NULL;
10313 goto fail;
10314 }
10315
10316 /* Ensure that the result will have a trailing path separator
10317 * if the argument has one. */
10318 if (remain == NULL && has_trailing_pathsep)
10319 add_pathsep(buf);
10320
10321 /* Separate the first path component in the link value and
10322 * concatenate the remainders. */
10323 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
10324 if (*q != NUL)
10325 {
10326 if (remain == NULL)
10327 remain = vim_strsave(q - 1);
10328 else
10329 {
10330 cpy = concat_str(q - 1, remain);
10331 if (cpy != NULL)
10332 {
10333 vim_free(remain);
10334 remain = cpy;
10335 }
10336 }
10337 q[-1] = NUL;
10338 }
10339
10340 q = gettail(p);
10341 if (q > p && *q == NUL)
10342 {
10343 /* Ignore trailing path separator. */
10344 q[-1] = NUL;
10345 q = gettail(p);
10346 }
10347 if (q > p && !mch_isFullName(buf))
10348 {
10349 /* symlink is relative to directory of argument */
10350 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
10351 if (cpy != NULL)
10352 {
10353 STRCPY(cpy, p);
10354 STRCPY(gettail(cpy), buf);
10355 vim_free(p);
10356 p = cpy;
10357 }
10358 }
10359 else
10360 {
10361 vim_free(p);
10362 p = vim_strsave(buf);
10363 }
10364 }
10365
10366 if (remain == NULL)
10367 break;
10368
10369 /* Append the first path component of "remain" to "p". */
10370 q = getnextcomp(remain + 1);
10371 len = q - remain - (*q != NUL);
10372 cpy = vim_strnsave(p, STRLEN(p) + len);
10373 if (cpy != NULL)
10374 {
10375 STRNCAT(cpy, remain, len);
10376 vim_free(p);
10377 p = cpy;
10378 }
10379 /* Shorten "remain". */
10380 if (*q != NUL)
10381 STRMOVE(remain, q - 1);
10382 else
Bram Moolenaard23a8232018-02-10 18:45:26 +010010383 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010384 }
10385
10386 /* If the result is a relative path name, make it explicitly relative to
10387 * the current directory if and only if the argument had this form. */
10388 if (!vim_ispathsep(*p))
10389 {
10390 if (is_relative_to_current
10391 && *p != NUL
10392 && !(p[0] == '.'
10393 && (p[1] == NUL
10394 || vim_ispathsep(p[1])
10395 || (p[1] == '.'
10396 && (p[2] == NUL
10397 || vim_ispathsep(p[2]))))))
10398 {
10399 /* Prepend "./". */
10400 cpy = concat_str((char_u *)"./", p);
10401 if (cpy != NULL)
10402 {
10403 vim_free(p);
10404 p = cpy;
10405 }
10406 }
10407 else if (!is_relative_to_current)
10408 {
10409 /* Strip leading "./". */
10410 q = p;
10411 while (q[0] == '.' && vim_ispathsep(q[1]))
10412 q += 2;
10413 if (q > p)
10414 STRMOVE(p, p + 2);
10415 }
10416 }
10417
10418 /* Ensure that the result will have no trailing path separator
10419 * if the argument had none. But keep "/" or "//". */
10420 if (!has_trailing_pathsep)
10421 {
10422 q = p + STRLEN(p);
10423 if (after_pathsep(p, q))
10424 *gettail_sep(p) = NUL;
10425 }
10426
10427 rettv->vval.v_string = p;
10428 }
10429# else
10430 rettv->vval.v_string = vim_strsave(p);
10431# endif
10432#endif
10433
10434 simplify_filename(rettv->vval.v_string);
10435
10436#ifdef HAVE_READLINK
10437fail:
10438 vim_free(buf);
10439#endif
10440 rettv->v_type = VAR_STRING;
10441}
10442
10443/*
10444 * "reverse({list})" function
10445 */
10446 static void
10447f_reverse(typval_T *argvars, typval_T *rettv)
10448{
10449 list_T *l;
10450 listitem_T *li, *ni;
10451
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010452 if (argvars[0].v_type == VAR_BLOB)
10453 {
10454 blob_T *b = argvars[0].vval.v_blob;
10455 int i, len = blob_len(b);
10456
10457 for (i = 0; i < len / 2; i++)
10458 {
10459 int tmp = blob_get(b, i);
10460
10461 blob_set(b, i, blob_get(b, len - i - 1));
10462 blob_set(b, len - i - 1, tmp);
10463 }
10464 rettv_blob_set(rettv, b);
10465 return;
10466 }
10467
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010468 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010469 semsg(_(e_listblobarg), "reverse()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010470 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010471 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010472 (char_u *)N_("reverse() argument"), TRUE))
10473 {
10474 li = l->lv_last;
10475 l->lv_first = l->lv_last = NULL;
10476 l->lv_len = 0;
10477 while (li != NULL)
10478 {
10479 ni = li->li_prev;
10480 list_append(l, li);
10481 li = ni;
10482 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020010483 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010484 l->lv_idx = l->lv_len - l->lv_idx - 1;
10485 }
10486}
10487
10488#define SP_NOMOVE 0x01 /* don't move cursor */
10489#define SP_REPEAT 0x02 /* repeat to find outer pair */
10490#define SP_RETCOUNT 0x04 /* return matchcount */
10491#define SP_SETPCMARK 0x08 /* set previous context mark */
10492#define SP_START 0x10 /* accept match at start position */
10493#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
10494#define SP_END 0x40 /* leave cursor at end of match */
10495#define SP_COLUMN 0x80 /* start at cursor column */
10496
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010497/*
10498 * Get flags for a search function.
10499 * Possibly sets "p_ws".
10500 * Returns BACKWARD, FORWARD or zero (for an error).
10501 */
10502 static int
10503get_search_arg(typval_T *varp, int *flagsp)
10504{
10505 int dir = FORWARD;
10506 char_u *flags;
10507 char_u nbuf[NUMBUFLEN];
10508 int mask;
10509
10510 if (varp->v_type != VAR_UNKNOWN)
10511 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010512 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010513 if (flags == NULL)
10514 return 0; /* type error; errmsg already given */
10515 while (*flags != NUL)
10516 {
10517 switch (*flags)
10518 {
10519 case 'b': dir = BACKWARD; break;
10520 case 'w': p_ws = TRUE; break;
10521 case 'W': p_ws = FALSE; break;
10522 default: mask = 0;
10523 if (flagsp != NULL)
10524 switch (*flags)
10525 {
10526 case 'c': mask = SP_START; break;
10527 case 'e': mask = SP_END; break;
10528 case 'm': mask = SP_RETCOUNT; break;
10529 case 'n': mask = SP_NOMOVE; break;
10530 case 'p': mask = SP_SUBPAT; break;
10531 case 'r': mask = SP_REPEAT; break;
10532 case 's': mask = SP_SETPCMARK; break;
10533 case 'z': mask = SP_COLUMN; break;
10534 }
10535 if (mask == 0)
10536 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010537 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010538 dir = 0;
10539 }
10540 else
10541 *flagsp |= mask;
10542 }
10543 if (dir == 0)
10544 break;
10545 ++flags;
10546 }
10547 }
10548 return dir;
10549}
10550
10551/*
10552 * Shared by search() and searchpos() functions.
10553 */
10554 static int
10555search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
10556{
10557 int flags;
10558 char_u *pat;
10559 pos_T pos;
10560 pos_T save_cursor;
10561 int save_p_ws = p_ws;
10562 int dir;
10563 int retval = 0; /* default: FAIL */
10564 long lnum_stop = 0;
10565 proftime_T tm;
10566#ifdef FEAT_RELTIME
10567 long time_limit = 0;
10568#endif
10569 int options = SEARCH_KEEP;
10570 int subpatnum;
10571
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010572 pat = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010573 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
10574 if (dir == 0)
10575 goto theend;
10576 flags = *flagsp;
10577 if (flags & SP_START)
10578 options |= SEARCH_START;
10579 if (flags & SP_END)
10580 options |= SEARCH_END;
10581 if (flags & SP_COLUMN)
10582 options |= SEARCH_COL;
10583
10584 /* Optional arguments: line number to stop searching and timeout. */
10585 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
10586 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010587 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010588 if (lnum_stop < 0)
10589 goto theend;
10590#ifdef FEAT_RELTIME
10591 if (argvars[3].v_type != VAR_UNKNOWN)
10592 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010593 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010594 if (time_limit < 0)
10595 goto theend;
10596 }
10597#endif
10598 }
10599
10600#ifdef FEAT_RELTIME
10601 /* Set the time limit, if there is one. */
10602 profile_setlimit(time_limit, &tm);
10603#endif
10604
10605 /*
10606 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
10607 * Check to make sure only those flags are set.
10608 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
10609 * flags cannot be set. Check for that condition also.
10610 */
10611 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
10612 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10613 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010614 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010615 goto theend;
10616 }
10617
10618 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010619 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010620 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010621 if (subpatnum != FAIL)
10622 {
10623 if (flags & SP_SUBPAT)
10624 retval = subpatnum;
10625 else
10626 retval = pos.lnum;
10627 if (flags & SP_SETPCMARK)
10628 setpcmark();
10629 curwin->w_cursor = pos;
10630 if (match_pos != NULL)
10631 {
10632 /* Store the match cursor position */
10633 match_pos->lnum = pos.lnum;
10634 match_pos->col = pos.col + 1;
10635 }
10636 /* "/$" will put the cursor after the end of the line, may need to
10637 * correct that here */
10638 check_cursor();
10639 }
10640
10641 /* If 'n' flag is used: restore cursor position. */
10642 if (flags & SP_NOMOVE)
10643 curwin->w_cursor = save_cursor;
10644 else
10645 curwin->w_set_curswant = TRUE;
10646theend:
10647 p_ws = save_p_ws;
10648
10649 return retval;
10650}
10651
10652#ifdef FEAT_FLOAT
10653
10654/*
10655 * round() is not in C90, use ceil() or floor() instead.
10656 */
10657 float_T
10658vim_round(float_T f)
10659{
10660 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
10661}
10662
10663/*
10664 * "round({float})" function
10665 */
10666 static void
10667f_round(typval_T *argvars, typval_T *rettv)
10668{
10669 float_T f = 0.0;
10670
10671 rettv->v_type = VAR_FLOAT;
10672 if (get_float_arg(argvars, &f) == OK)
10673 rettv->vval.v_float = vim_round(f);
10674 else
10675 rettv->vval.v_float = 0.0;
10676}
10677#endif
10678
Bram Moolenaare99be0e2019-03-26 22:51:09 +010010679#ifdef FEAT_RUBY
10680/*
10681 * "rubyeval()" function
10682 */
10683 static void
10684f_rubyeval(typval_T *argvars, typval_T *rettv)
10685{
10686 char_u *str;
10687 char_u buf[NUMBUFLEN];
10688
10689 str = tv_get_string_buf(&argvars[0], buf);
10690 do_rubyeval(str, rettv);
10691}
10692#endif
10693
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010694/*
10695 * "screenattr()" function
10696 */
10697 static void
10698f_screenattr(typval_T *argvars, typval_T *rettv)
10699{
10700 int row;
10701 int col;
10702 int c;
10703
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010704 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10705 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010706 if (row < 0 || row >= screen_Rows
10707 || col < 0 || col >= screen_Columns)
10708 c = -1;
10709 else
10710 c = ScreenAttrs[LineOffset[row] + col];
10711 rettv->vval.v_number = c;
10712}
10713
10714/*
10715 * "screenchar()" function
10716 */
10717 static void
10718f_screenchar(typval_T *argvars, typval_T *rettv)
10719{
10720 int row;
10721 int col;
10722 int off;
10723 int c;
10724
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010725 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10726 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010727 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010728 c = -1;
10729 else
10730 {
10731 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010732 if (enc_utf8 && ScreenLinesUC[off] != 0)
10733 c = ScreenLinesUC[off];
10734 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010735 c = ScreenLines[off];
10736 }
10737 rettv->vval.v_number = c;
10738}
10739
10740/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010741 * "screenchars()" function
10742 */
10743 static void
10744f_screenchars(typval_T *argvars, typval_T *rettv)
10745{
10746 int row;
10747 int col;
10748 int off;
10749 int c;
10750 int i;
10751
10752 if (rettv_list_alloc(rettv) == FAIL)
10753 return;
10754 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10755 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10756 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10757 return;
10758
10759 off = LineOffset[row] + col;
10760 if (enc_utf8 && ScreenLinesUC[off] != 0)
10761 c = ScreenLinesUC[off];
10762 else
10763 c = ScreenLines[off];
10764 list_append_number(rettv->vval.v_list, (varnumber_T)c);
10765
10766 if (enc_utf8)
10767
10768 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10769 list_append_number(rettv->vval.v_list,
10770 (varnumber_T)ScreenLinesC[i][off]);
10771}
10772
10773/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010774 * "screencol()" function
10775 *
10776 * First column is 1 to be consistent with virtcol().
10777 */
10778 static void
10779f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10780{
10781 rettv->vval.v_number = screen_screencol() + 1;
10782}
10783
10784/*
10785 * "screenrow()" function
10786 */
10787 static void
10788f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10789{
10790 rettv->vval.v_number = screen_screenrow() + 1;
10791}
10792
10793/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010794 * "screenstring()" function
10795 */
10796 static void
10797f_screenstring(typval_T *argvars, typval_T *rettv)
10798{
10799 int row;
10800 int col;
10801 int off;
10802 int c;
10803 int i;
10804 char_u buf[MB_MAXBYTES + 1];
10805 int buflen = 0;
10806
10807 rettv->vval.v_string = NULL;
10808 rettv->v_type = VAR_STRING;
10809
10810 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10811 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10812 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10813 return;
10814
10815 off = LineOffset[row] + col;
10816 if (enc_utf8 && ScreenLinesUC[off] != 0)
10817 c = ScreenLinesUC[off];
10818 else
10819 c = ScreenLines[off];
10820 buflen += mb_char2bytes(c, buf);
10821
10822 if (enc_utf8)
10823 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10824 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
10825
10826 buf[buflen] = NUL;
10827 rettv->vval.v_string = vim_strsave(buf);
10828}
10829
10830/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010831 * "search()" function
10832 */
10833 static void
10834f_search(typval_T *argvars, typval_T *rettv)
10835{
10836 int flags = 0;
10837
10838 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10839}
10840
10841/*
10842 * "searchdecl()" function
10843 */
10844 static void
10845f_searchdecl(typval_T *argvars, typval_T *rettv)
10846{
10847 int locally = 1;
10848 int thisblock = 0;
10849 int error = FALSE;
10850 char_u *name;
10851
10852 rettv->vval.v_number = 1; /* default: FAIL */
10853
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010854 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010855 if (argvars[1].v_type != VAR_UNKNOWN)
10856 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010857 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010858 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010859 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010860 }
10861 if (!error && name != NULL)
10862 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10863 locally, thisblock, SEARCH_KEEP) == FAIL;
10864}
10865
10866/*
10867 * Used by searchpair() and searchpairpos()
10868 */
10869 static int
10870searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10871{
10872 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010873 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010874 int save_p_ws = p_ws;
10875 int dir;
10876 int flags = 0;
10877 char_u nbuf1[NUMBUFLEN];
10878 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010879 int retval = 0; /* default: FAIL */
10880 long lnum_stop = 0;
10881 long time_limit = 0;
10882
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010883 /* Get the three pattern arguments: start, middle, end. Will result in an
10884 * error if not a valid argument. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010885 spat = tv_get_string_chk(&argvars[0]);
10886 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
10887 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010888 if (spat == NULL || mpat == NULL || epat == NULL)
10889 goto theend; /* type error */
10890
10891 /* Handle the optional fourth argument: flags */
10892 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
10893 if (dir == 0)
10894 goto theend;
10895
10896 /* Don't accept SP_END or SP_SUBPAT.
10897 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
10898 */
10899 if ((flags & (SP_END | SP_SUBPAT)) != 0
10900 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10901 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010902 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010903 goto theend;
10904 }
10905
10906 /* Using 'r' implies 'W', otherwise it doesn't work. */
10907 if (flags & SP_REPEAT)
10908 p_ws = FALSE;
10909
10910 /* Optional fifth argument: skip expression */
10911 if (argvars[3].v_type == VAR_UNKNOWN
10912 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010010913 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010914 else
10915 {
Bram Moolenaar48570482017-10-30 21:48:41 +010010916 skip = &argvars[4];
10917 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
10918 && skip->v_type != VAR_STRING)
10919 {
10920 /* Type error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010921 semsg(_(e_invarg2), tv_get_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010010922 goto theend;
10923 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010924 if (argvars[5].v_type != VAR_UNKNOWN)
10925 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010926 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010927 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010928 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010929 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010930 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010931 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010932#ifdef FEAT_RELTIME
10933 if (argvars[6].v_type != VAR_UNKNOWN)
10934 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010935 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010936 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010937 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010938 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010939 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010940 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010941 }
10942#endif
10943 }
10944 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010945
10946 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
10947 match_pos, lnum_stop, time_limit);
10948
10949theend:
10950 p_ws = save_p_ws;
10951
10952 return retval;
10953}
10954
10955/*
10956 * "searchpair()" function
10957 */
10958 static void
10959f_searchpair(typval_T *argvars, typval_T *rettv)
10960{
10961 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
10962}
10963
10964/*
10965 * "searchpairpos()" function
10966 */
10967 static void
10968f_searchpairpos(typval_T *argvars, typval_T *rettv)
10969{
10970 pos_T match_pos;
10971 int lnum = 0;
10972 int col = 0;
10973
10974 if (rettv_list_alloc(rettv) == FAIL)
10975 return;
10976
10977 if (searchpair_cmn(argvars, &match_pos) > 0)
10978 {
10979 lnum = match_pos.lnum;
10980 col = match_pos.col;
10981 }
10982
10983 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10984 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10985}
10986
10987/*
10988 * Search for a start/middle/end thing.
10989 * Used by searchpair(), see its documentation for the details.
10990 * Returns 0 or -1 for no match,
10991 */
10992 long
10993do_searchpair(
10994 char_u *spat, /* start pattern */
10995 char_u *mpat, /* middle pattern */
10996 char_u *epat, /* end pattern */
10997 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010010998 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010999 int flags, /* SP_SETPCMARK and other SP_ values */
11000 pos_T *match_pos,
11001 linenr_T lnum_stop, /* stop at this line if not zero */
11002 long time_limit UNUSED) /* stop after this many msec */
11003{
11004 char_u *save_cpo;
11005 char_u *pat, *pat2 = NULL, *pat3 = NULL;
11006 long retval = 0;
11007 pos_T pos;
11008 pos_T firstpos;
11009 pos_T foundpos;
11010 pos_T save_cursor;
11011 pos_T save_pos;
11012 int n;
11013 int r;
11014 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010011015 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011016 int err;
11017 int options = SEARCH_KEEP;
11018 proftime_T tm;
11019
11020 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11021 save_cpo = p_cpo;
11022 p_cpo = empty_option;
11023
11024#ifdef FEAT_RELTIME
11025 /* Set the time limit, if there is one. */
11026 profile_setlimit(time_limit, &tm);
11027#endif
11028
11029 /* Make two search patterns: start/end (pat2, for in nested pairs) and
11030 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +010011031 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
11032 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011033 if (pat2 == NULL || pat3 == NULL)
11034 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010011035 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011036 if (*mpat == NUL)
11037 STRCPY(pat3, pat2);
11038 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010011039 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011040 spat, epat, mpat);
11041 if (flags & SP_START)
11042 options |= SEARCH_START;
11043
Bram Moolenaar48570482017-10-30 21:48:41 +010011044 if (skip != NULL)
11045 {
11046 /* Empty string means to not use the skip expression. */
11047 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
11048 use_skip = skip->vval.v_string != NULL
11049 && *skip->vval.v_string != NUL;
11050 }
11051
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011052 save_cursor = curwin->w_cursor;
11053 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011054 CLEAR_POS(&firstpos);
11055 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011056 pat = pat3;
11057 for (;;)
11058 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +010011059 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020011060 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011061 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011062 /* didn't find it or found the first match again: FAIL */
11063 break;
11064
11065 if (firstpos.lnum == 0)
11066 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011067 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011068 {
11069 /* Found the same position again. Can happen with a pattern that
11070 * has "\zs" at the end and searching backwards. Advance one
11071 * character and try again. */
11072 if (dir == BACKWARD)
11073 decl(&pos);
11074 else
11075 incl(&pos);
11076 }
11077 foundpos = pos;
11078
11079 /* clear the start flag to avoid getting stuck here */
11080 options &= ~SEARCH_START;
11081
11082 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010011083 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011084 {
11085 save_pos = curwin->w_cursor;
11086 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010011087 err = FALSE;
11088 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011089 curwin->w_cursor = save_pos;
11090 if (err)
11091 {
11092 /* Evaluating {skip} caused an error, break here. */
11093 curwin->w_cursor = save_cursor;
11094 retval = -1;
11095 break;
11096 }
11097 if (r)
11098 continue;
11099 }
11100
11101 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
11102 {
11103 /* Found end when searching backwards or start when searching
11104 * forward: nested pair. */
11105 ++nest;
11106 pat = pat2; /* nested, don't search for middle */
11107 }
11108 else
11109 {
11110 /* Found end when searching forward or start when searching
11111 * backward: end of (nested) pair; or found middle in outer pair. */
11112 if (--nest == 1)
11113 pat = pat3; /* outer level, search for middle */
11114 }
11115
11116 if (nest == 0)
11117 {
11118 /* Found the match: return matchcount or line number. */
11119 if (flags & SP_RETCOUNT)
11120 ++retval;
11121 else
11122 retval = pos.lnum;
11123 if (flags & SP_SETPCMARK)
11124 setpcmark();
11125 curwin->w_cursor = pos;
11126 if (!(flags & SP_REPEAT))
11127 break;
11128 nest = 1; /* search for next unmatched */
11129 }
11130 }
11131
11132 if (match_pos != NULL)
11133 {
11134 /* Store the match cursor position */
11135 match_pos->lnum = curwin->w_cursor.lnum;
11136 match_pos->col = curwin->w_cursor.col + 1;
11137 }
11138
11139 /* If 'n' flag is used or search failed: restore cursor position. */
11140 if ((flags & SP_NOMOVE) || retval == 0)
11141 curwin->w_cursor = save_cursor;
11142
11143theend:
11144 vim_free(pat2);
11145 vim_free(pat3);
11146 if (p_cpo == empty_option)
11147 p_cpo = save_cpo;
11148 else
11149 /* Darn, evaluating the {skip} expression changed the value. */
11150 free_string_option(save_cpo);
11151
11152 return retval;
11153}
11154
11155/*
11156 * "searchpos()" function
11157 */
11158 static void
11159f_searchpos(typval_T *argvars, typval_T *rettv)
11160{
11161 pos_T match_pos;
11162 int lnum = 0;
11163 int col = 0;
11164 int n;
11165 int flags = 0;
11166
11167 if (rettv_list_alloc(rettv) == FAIL)
11168 return;
11169
11170 n = search_cmn(argvars, &match_pos, &flags);
11171 if (n > 0)
11172 {
11173 lnum = match_pos.lnum;
11174 col = match_pos.col;
11175 }
11176
11177 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
11178 list_append_number(rettv->vval.v_list, (varnumber_T)col);
11179 if (flags & SP_SUBPAT)
11180 list_append_number(rettv->vval.v_list, (varnumber_T)n);
11181}
11182
11183 static void
11184f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
11185{
11186#ifdef FEAT_CLIENTSERVER
11187 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011188 char_u *server = tv_get_string_chk(&argvars[0]);
11189 char_u *reply = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011190
11191 rettv->vval.v_number = -1;
11192 if (server == NULL || reply == NULL)
11193 return;
11194 if (check_restricted() || check_secure())
11195 return;
11196# ifdef FEAT_X11
11197 if (check_connection() == FAIL)
11198 return;
11199# endif
11200
11201 if (serverSendReply(server, reply) < 0)
11202 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011203 emsg(_("E258: Unable to send to client"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011204 return;
11205 }
11206 rettv->vval.v_number = 0;
11207#else
11208 rettv->vval.v_number = -1;
11209#endif
11210}
11211
11212 static void
11213f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
11214{
11215 char_u *r = NULL;
11216
11217#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +010011218# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011219 r = serverGetVimNames();
11220# else
11221 make_connection();
11222 if (X_DISPLAY != NULL)
11223 r = serverGetVimNames(X_DISPLAY);
11224# endif
11225#endif
11226 rettv->v_type = VAR_STRING;
11227 rettv->vval.v_string = r;
11228}
11229
11230/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011231 * "setbufline()" function
11232 */
11233 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020011234f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011235{
11236 linenr_T lnum;
11237 buf_T *buf;
11238
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011239 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011240 if (buf == NULL)
11241 rettv->vval.v_number = 1; /* FAIL */
11242 else
11243 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011244 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020011245 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011246 }
11247}
11248
11249/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011250 * "setbufvar()" function
11251 */
11252 static void
11253f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
11254{
11255 buf_T *buf;
11256 char_u *varname, *bufvarname;
11257 typval_T *varp;
11258 char_u nbuf[NUMBUFLEN];
11259
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011260 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011261 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011262 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
11263 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011264 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011265 varp = &argvars[2];
11266
11267 if (buf != NULL && varname != NULL && varp != NULL)
11268 {
11269 if (*varname == '&')
11270 {
11271 long numval;
11272 char_u *strval;
11273 int error = FALSE;
11274 aco_save_T aco;
11275
11276 /* set curbuf to be our buf, temporarily */
11277 aucmd_prepbuf(&aco, buf);
11278
11279 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011280 numval = (long)tv_get_number_chk(varp, &error);
11281 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011282 if (!error && strval != NULL)
11283 set_option_value(varname, numval, strval, OPT_LOCAL);
11284
11285 /* reset notion of buffer */
11286 aucmd_restbuf(&aco);
11287 }
11288 else
11289 {
11290 buf_T *save_curbuf = curbuf;
11291
11292 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
11293 if (bufvarname != NULL)
11294 {
11295 curbuf = buf;
11296 STRCPY(bufvarname, "b:");
11297 STRCPY(bufvarname + 2, varname);
11298 set_var(bufvarname, varp, TRUE);
11299 vim_free(bufvarname);
11300 curbuf = save_curbuf;
11301 }
11302 }
11303 }
11304}
11305
11306 static void
11307f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
11308{
11309 dict_T *d;
11310 dictitem_T *di;
11311 char_u *csearch;
11312
11313 if (argvars[0].v_type != VAR_DICT)
11314 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011315 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011316 return;
11317 }
11318
11319 if ((d = argvars[0].vval.v_dict) != NULL)
11320 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010011321 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011322 if (csearch != NULL)
11323 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011324 if (enc_utf8)
11325 {
11326 int pcc[MAX_MCO];
11327 int c = utfc_ptr2char(csearch, pcc);
11328
11329 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
11330 }
11331 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011332 set_last_csearch(PTR2CHAR(csearch),
11333 csearch, MB_PTR2LEN(csearch));
11334 }
11335
11336 di = dict_find(d, (char_u *)"forward", -1);
11337 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011338 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011339 ? FORWARD : BACKWARD);
11340
11341 di = dict_find(d, (char_u *)"until", -1);
11342 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011343 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011344 }
11345}
11346
11347/*
11348 * "setcmdpos()" function
11349 */
11350 static void
11351f_setcmdpos(typval_T *argvars, typval_T *rettv)
11352{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011353 int pos = (int)tv_get_number(&argvars[0]) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011354
11355 if (pos >= 0)
11356 rettv->vval.v_number = set_cmdline_pos(pos);
11357}
11358
11359/*
11360 * "setfperm({fname}, {mode})" function
11361 */
11362 static void
11363f_setfperm(typval_T *argvars, typval_T *rettv)
11364{
11365 char_u *fname;
11366 char_u modebuf[NUMBUFLEN];
11367 char_u *mode_str;
11368 int i;
11369 int mask;
11370 int mode = 0;
11371
11372 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011373 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011374 if (fname == NULL)
11375 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011376 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011377 if (mode_str == NULL)
11378 return;
11379 if (STRLEN(mode_str) != 9)
11380 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011381 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011382 return;
11383 }
11384
11385 mask = 1;
11386 for (i = 8; i >= 0; --i)
11387 {
11388 if (mode_str[i] != '-')
11389 mode |= mask;
11390 mask = mask << 1;
11391 }
11392 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
11393}
11394
11395/*
11396 * "setline()" function
11397 */
11398 static void
11399f_setline(typval_T *argvars, typval_T *rettv)
11400{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011401 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011402
Bram Moolenaarca851592018-06-06 21:04:07 +020011403 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011404}
11405
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011406/*
11407 * Used by "setqflist()" and "setloclist()" functions
11408 */
11409 static void
11410set_qf_ll_list(
11411 win_T *wp UNUSED,
11412 typval_T *list_arg UNUSED,
11413 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020011414 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011415 typval_T *rettv)
11416{
11417#ifdef FEAT_QUICKFIX
11418 static char *e_invact = N_("E927: Invalid action: '%s'");
11419 char_u *act;
11420 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011421 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011422#endif
11423
11424 rettv->vval.v_number = -1;
11425
11426#ifdef FEAT_QUICKFIX
11427 if (list_arg->v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011428 emsg(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011429 else if (recursive != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011430 emsg(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011431 else
11432 {
11433 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011434 dict_T *d = NULL;
11435 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011436
11437 if (action_arg->v_type == VAR_STRING)
11438 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011439 act = tv_get_string_chk(action_arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011440 if (act == NULL)
11441 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020011442 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
11443 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011444 action = *act;
11445 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011446 semsg(_(e_invact), act);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011447 }
11448 else if (action_arg->v_type == VAR_UNKNOWN)
11449 action = ' ';
11450 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011451 emsg(_(e_stringreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011452
Bram Moolenaard823fa92016-08-12 16:29:27 +020011453 if (action_arg->v_type != VAR_UNKNOWN
11454 && what_arg->v_type != VAR_UNKNOWN)
11455 {
11456 if (what_arg->v_type == VAR_DICT)
11457 d = what_arg->vval.v_dict;
11458 else
11459 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011460 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +020011461 valid_dict = FALSE;
11462 }
11463 }
11464
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011465 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011466 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011467 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
11468 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011469 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011470 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011471 }
11472#endif
11473}
11474
11475/*
11476 * "setloclist()" function
11477 */
11478 static void
11479f_setloclist(typval_T *argvars, typval_T *rettv)
11480{
11481 win_T *win;
11482
11483 rettv->vval.v_number = -1;
11484
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020011485 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011486 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020011487 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011488}
11489
11490/*
11491 * "setmatches()" function
11492 */
11493 static void
11494f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
11495{
11496#ifdef FEAT_SEARCH_EXTRA
11497 list_T *l;
11498 listitem_T *li;
11499 dict_T *d;
11500 list_T *s = NULL;
Bram Moolenaaraff74912019-03-30 18:11:49 +010011501 win_T *win = get_optional_window(argvars, 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011502
11503 rettv->vval.v_number = -1;
11504 if (argvars[0].v_type != VAR_LIST)
11505 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011506 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011507 return;
11508 }
Bram Moolenaaraff74912019-03-30 18:11:49 +010011509 if (win == NULL)
11510 return;
11511
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011512 if ((l = argvars[0].vval.v_list) != NULL)
11513 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011514 /* To some extent make sure that we are dealing with a list from
11515 * "getmatches()". */
11516 li = l->lv_first;
11517 while (li != NULL)
11518 {
11519 if (li->li_tv.v_type != VAR_DICT
11520 || (d = li->li_tv.vval.v_dict) == NULL)
11521 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011522 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011523 return;
11524 }
11525 if (!(dict_find(d, (char_u *)"group", -1) != NULL
11526 && (dict_find(d, (char_u *)"pattern", -1) != NULL
11527 || dict_find(d, (char_u *)"pos1", -1) != NULL)
11528 && dict_find(d, (char_u *)"priority", -1) != NULL
11529 && dict_find(d, (char_u *)"id", -1) != NULL))
11530 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011531 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011532 return;
11533 }
11534 li = li->li_next;
11535 }
11536
Bram Moolenaaraff74912019-03-30 18:11:49 +010011537 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011538 li = l->lv_first;
11539 while (li != NULL)
11540 {
11541 int i = 0;
Bram Moolenaar54315892019-04-26 22:33:49 +020011542 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011543 dictitem_T *di;
11544 char_u *group;
11545 int priority;
11546 int id;
11547 char_u *conceal;
11548
11549 d = li->li_tv.vval.v_dict;
11550 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
11551 {
11552 if (s == NULL)
11553 {
11554 s = list_alloc();
11555 if (s == NULL)
11556 return;
11557 }
11558
11559 /* match from matchaddpos() */
11560 for (i = 1; i < 9; i++)
11561 {
11562 sprintf((char *)buf, (char *)"pos%d", i);
11563 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
11564 {
11565 if (di->di_tv.v_type != VAR_LIST)
11566 return;
11567
11568 list_append_tv(s, &di->di_tv);
11569 s->lv_refcount++;
11570 }
11571 else
11572 break;
11573 }
11574 }
11575
Bram Moolenaar8f667172018-12-14 15:38:31 +010011576 group = dict_get_string(d, (char_u *)"group", TRUE);
11577 priority = (int)dict_get_number(d, (char_u *)"priority");
11578 id = (int)dict_get_number(d, (char_u *)"id");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011579 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar8f667172018-12-14 15:38:31 +010011580 ? dict_get_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011581 : NULL;
11582 if (i == 0)
11583 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011584 match_add(win, group,
Bram Moolenaar8f667172018-12-14 15:38:31 +010011585 dict_get_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011586 priority, id, NULL, conceal);
11587 }
11588 else
11589 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011590 match_add(win, group, NULL, priority, id, s, conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011591 list_unref(s);
11592 s = NULL;
11593 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020011594 vim_free(group);
11595 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011596
11597 li = li->li_next;
11598 }
11599 rettv->vval.v_number = 0;
11600 }
11601#endif
11602}
11603
11604/*
11605 * "setpos()" function
11606 */
11607 static void
11608f_setpos(typval_T *argvars, typval_T *rettv)
11609{
11610 pos_T pos;
11611 int fnum;
11612 char_u *name;
11613 colnr_T curswant = -1;
11614
11615 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011616 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011617 if (name != NULL)
11618 {
11619 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
11620 {
11621 if (--pos.col < 0)
11622 pos.col = 0;
11623 if (name[0] == '.' && name[1] == NUL)
11624 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011625 /* set cursor; "fnum" is ignored */
11626 curwin->w_cursor = pos;
11627 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011628 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011629 curwin->w_curswant = curswant - 1;
11630 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011631 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011632 check_cursor();
11633 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011634 }
11635 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
11636 {
11637 /* set mark */
11638 if (setmark_pos(name[1], &pos, fnum) == OK)
11639 rettv->vval.v_number = 0;
11640 }
11641 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011642 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011643 }
11644 }
11645}
11646
11647/*
11648 * "setqflist()" function
11649 */
11650 static void
11651f_setqflist(typval_T *argvars, typval_T *rettv)
11652{
Bram Moolenaard823fa92016-08-12 16:29:27 +020011653 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011654}
11655
11656/*
11657 * "setreg()" function
11658 */
11659 static void
11660f_setreg(typval_T *argvars, typval_T *rettv)
11661{
11662 int regname;
11663 char_u *strregname;
11664 char_u *stropt;
11665 char_u *strval;
11666 int append;
11667 char_u yank_type;
11668 long block_len;
11669
11670 block_len = -1;
11671 yank_type = MAUTO;
11672 append = FALSE;
11673
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011674 strregname = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011675 rettv->vval.v_number = 1; /* FAIL is default */
11676
11677 if (strregname == NULL)
11678 return; /* type error; errmsg already given */
11679 regname = *strregname;
11680 if (regname == 0 || regname == '@')
11681 regname = '"';
11682
11683 if (argvars[2].v_type != VAR_UNKNOWN)
11684 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011685 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011686 if (stropt == NULL)
11687 return; /* type error */
11688 for (; *stropt != NUL; ++stropt)
11689 switch (*stropt)
11690 {
11691 case 'a': case 'A': /* append */
11692 append = TRUE;
11693 break;
11694 case 'v': case 'c': /* character-wise selection */
11695 yank_type = MCHAR;
11696 break;
11697 case 'V': case 'l': /* line-wise selection */
11698 yank_type = MLINE;
11699 break;
11700 case 'b': case Ctrl_V: /* block-wise selection */
11701 yank_type = MBLOCK;
11702 if (VIM_ISDIGIT(stropt[1]))
11703 {
11704 ++stropt;
11705 block_len = getdigits(&stropt) - 1;
11706 --stropt;
11707 }
11708 break;
11709 }
11710 }
11711
11712 if (argvars[1].v_type == VAR_LIST)
11713 {
11714 char_u **lstval;
11715 char_u **allocval;
11716 char_u buf[NUMBUFLEN];
11717 char_u **curval;
11718 char_u **curallocval;
11719 list_T *ll = argvars[1].vval.v_list;
11720 listitem_T *li;
11721 int len;
11722
11723 /* If the list is NULL handle like an empty list. */
11724 len = ll == NULL ? 0 : ll->lv_len;
11725
11726 /* First half: use for pointers to result lines; second half: use for
11727 * pointers to allocated copies. */
11728 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
11729 if (lstval == NULL)
11730 return;
11731 curval = lstval;
11732 allocval = lstval + len + 2;
11733 curallocval = allocval;
11734
11735 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
11736 li = li->li_next)
11737 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011738 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011739 if (strval == NULL)
11740 goto free_lstval;
11741 if (strval == buf)
11742 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011743 /* Need to make a copy, next tv_get_string_buf_chk() will
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011744 * overwrite the string. */
11745 strval = vim_strsave(buf);
11746 if (strval == NULL)
11747 goto free_lstval;
11748 *curallocval++ = strval;
11749 }
11750 *curval++ = strval;
11751 }
11752 *curval++ = NULL;
11753
11754 write_reg_contents_lst(regname, lstval, -1,
11755 append, yank_type, block_len);
11756free_lstval:
11757 while (curallocval > allocval)
11758 vim_free(*--curallocval);
11759 vim_free(lstval);
11760 }
11761 else
11762 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011763 strval = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011764 if (strval == NULL)
11765 return;
11766 write_reg_contents_ex(regname, strval, -1,
11767 append, yank_type, block_len);
11768 }
11769 rettv->vval.v_number = 0;
11770}
11771
11772/*
11773 * "settabvar()" function
11774 */
11775 static void
11776f_settabvar(typval_T *argvars, typval_T *rettv)
11777{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011778 tabpage_T *save_curtab;
11779 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011780 char_u *varname, *tabvarname;
11781 typval_T *varp;
11782
11783 rettv->vval.v_number = 0;
11784
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011785 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011786 return;
11787
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011788 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
11789 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011790 varp = &argvars[2];
11791
Bram Moolenaar4033c552017-09-16 20:54:51 +020011792 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011793 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011794 save_curtab = curtab;
11795 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011796
11797 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
11798 if (tabvarname != NULL)
11799 {
11800 STRCPY(tabvarname, "t:");
11801 STRCPY(tabvarname + 2, varname);
11802 set_var(tabvarname, varp, TRUE);
11803 vim_free(tabvarname);
11804 }
11805
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011806 /* Restore current tabpage */
11807 if (valid_tabpage(save_curtab))
11808 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011809 }
11810}
11811
11812/*
11813 * "settabwinvar()" function
11814 */
11815 static void
11816f_settabwinvar(typval_T *argvars, typval_T *rettv)
11817{
11818 setwinvar(argvars, rettv, 1);
11819}
11820
11821/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011822 * "settagstack()" function
11823 */
11824 static void
11825f_settagstack(typval_T *argvars, typval_T *rettv)
11826{
11827 static char *e_invact2 = N_("E962: Invalid action: '%s'");
11828 win_T *wp;
11829 dict_T *d;
11830 int action = 'r';
11831
11832 rettv->vval.v_number = -1;
11833
11834 // first argument: window number or id
11835 wp = find_win_by_nr_or_id(&argvars[0]);
11836 if (wp == NULL)
11837 return;
11838
11839 // second argument: dict with items to set in the tag stack
11840 if (argvars[1].v_type != VAR_DICT)
11841 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011842 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011843 return;
11844 }
11845 d = argvars[1].vval.v_dict;
11846 if (d == NULL)
11847 return;
11848
11849 // third argument: action - 'a' for append and 'r' for replace.
11850 // default is to replace the stack.
11851 if (argvars[2].v_type == VAR_UNKNOWN)
11852 action = 'r';
11853 else if (argvars[2].v_type == VAR_STRING)
11854 {
11855 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011856 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011857 if (actstr == NULL)
11858 return;
11859 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
11860 action = *actstr;
11861 else
11862 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011863 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011864 return;
11865 }
11866 }
11867 else
11868 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011869 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011870 return;
11871 }
11872
11873 if (set_tagstack(wp, d, action) == OK)
11874 rettv->vval.v_number = 0;
11875}
11876
11877/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011878 * "setwinvar()" function
11879 */
11880 static void
11881f_setwinvar(typval_T *argvars, typval_T *rettv)
11882{
11883 setwinvar(argvars, rettv, 0);
11884}
11885
11886#ifdef FEAT_CRYPT
11887/*
11888 * "sha256({string})" function
11889 */
11890 static void
11891f_sha256(typval_T *argvars, typval_T *rettv)
11892{
11893 char_u *p;
11894
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011895 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011896 rettv->vval.v_string = vim_strsave(
11897 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
11898 rettv->v_type = VAR_STRING;
11899}
11900#endif /* FEAT_CRYPT */
11901
11902/*
11903 * "shellescape({string})" function
11904 */
11905 static void
11906f_shellescape(typval_T *argvars, typval_T *rettv)
11907{
Bram Moolenaar20615522017-06-05 18:46:26 +020011908 int do_special = non_zero_arg(&argvars[1]);
11909
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011910 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011911 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011912 rettv->v_type = VAR_STRING;
11913}
11914
11915/*
11916 * shiftwidth() function
11917 */
11918 static void
11919f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
11920{
Bram Moolenaarf9514162018-11-22 03:08:29 +010011921 rettv->vval.v_number = 0;
11922
11923 if (argvars[0].v_type != VAR_UNKNOWN)
11924 {
11925 long col;
11926
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011927 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +010011928 if (col < 0)
11929 return; // type error; errmsg already given
11930#ifdef FEAT_VARTABS
11931 rettv->vval.v_number = get_sw_value_col(curbuf, col);
11932 return;
11933#endif
11934 }
11935
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011936 rettv->vval.v_number = get_sw_value(curbuf);
11937}
11938
Bram Moolenaar162b7142018-12-21 15:17:36 +010011939#ifdef FEAT_SIGNS
11940/*
11941 * "sign_define()" function
11942 */
11943 static void
11944f_sign_define(typval_T *argvars, typval_T *rettv)
11945{
11946 char_u *name;
11947 dict_T *dict;
11948 char_u *icon = NULL;
11949 char_u *linehl = NULL;
11950 char_u *text = NULL;
11951 char_u *texthl = NULL;
11952
11953 rettv->vval.v_number = -1;
11954
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011955 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011956 if (name == NULL)
11957 return;
11958
11959 if (argvars[1].v_type != VAR_UNKNOWN)
11960 {
11961 if (argvars[1].v_type != VAR_DICT)
11962 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011963 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011964 return;
11965 }
11966
11967 // sign attributes
11968 dict = argvars[1].vval.v_dict;
11969 if (dict_find(dict, (char_u *)"icon", -1) != NULL)
11970 icon = dict_get_string(dict, (char_u *)"icon", TRUE);
11971 if (dict_find(dict, (char_u *)"linehl", -1) != NULL)
11972 linehl = dict_get_string(dict, (char_u *)"linehl", TRUE);
11973 if (dict_find(dict, (char_u *)"text", -1) != NULL)
11974 text = dict_get_string(dict, (char_u *)"text", TRUE);
11975 if (dict_find(dict, (char_u *)"texthl", -1) != NULL)
11976 texthl = dict_get_string(dict, (char_u *)"texthl", TRUE);
11977 }
11978
11979 if (sign_define_by_name(name, icon, linehl, text, texthl) == OK)
11980 rettv->vval.v_number = 0;
11981
11982 vim_free(icon);
11983 vim_free(linehl);
11984 vim_free(text);
11985 vim_free(texthl);
11986}
11987
11988/*
11989 * "sign_getdefined()" function
11990 */
11991 static void
11992f_sign_getdefined(typval_T *argvars, typval_T *rettv)
11993{
11994 char_u *name = NULL;
11995
11996 if (rettv_list_alloc_id(rettv, aid_sign_getdefined) != OK)
11997 return;
11998
11999 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012000 name = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012001
12002 sign_getlist(name, rettv->vval.v_list);
12003}
12004
12005/*
12006 * "sign_getplaced()" function
12007 */
12008 static void
12009f_sign_getplaced(typval_T *argvars, typval_T *rettv)
12010{
12011 buf_T *buf = NULL;
12012 dict_T *dict;
12013 dictitem_T *di;
12014 linenr_T lnum = 0;
12015 int sign_id = 0;
12016 char_u *group = NULL;
12017 int notanum = FALSE;
12018
12019 if (rettv_list_alloc_id(rettv, aid_sign_getplaced) != OK)
12020 return;
12021
12022 if (argvars[0].v_type != VAR_UNKNOWN)
12023 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012024 // get signs placed in the specified buffer
12025 buf = get_buf_arg(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012026 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012027 return;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012028
12029 if (argvars[1].v_type != VAR_UNKNOWN)
12030 {
12031 if (argvars[1].v_type != VAR_DICT ||
12032 ((dict = argvars[1].vval.v_dict) == NULL))
12033 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012034 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012035 return;
12036 }
12037 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
12038 {
12039 // get signs placed at this line
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012040 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012041 if (notanum)
12042 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012043 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012044 }
12045 if ((di = dict_find(dict, (char_u *)"id", -1)) != NULL)
12046 {
12047 // get sign placed with this identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012048 sign_id = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012049 if (notanum)
12050 return;
12051 }
12052 if ((di = dict_find(dict, (char_u *)"group", -1)) != NULL)
12053 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012054 group = tv_get_string_chk(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012055 if (group == NULL)
12056 return;
Bram Moolenaar6436cd82018-12-27 00:28:33 +010012057 if (*group == '\0') // empty string means global group
12058 group = NULL;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012059 }
12060 }
12061 }
12062
12063 sign_get_placed(buf, lnum, sign_id, group, rettv->vval.v_list);
12064}
12065
12066/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012067 * "sign_jump()" function
12068 */
12069 static void
12070f_sign_jump(typval_T *argvars, typval_T *rettv)
12071{
12072 int sign_id;
12073 char_u *sign_group = NULL;
12074 buf_T *buf;
12075 int notanum = FALSE;
12076
12077 rettv->vval.v_number = -1;
12078
Bram Moolenaarbdace832019-03-02 10:13:42 +010012079 // Sign identifier
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012080 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
12081 if (notanum)
12082 return;
12083 if (sign_id <= 0)
12084 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012085 emsg(_(e_invarg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012086 return;
12087 }
12088
12089 // Sign group
12090 sign_group = tv_get_string_chk(&argvars[1]);
12091 if (sign_group == NULL)
12092 return;
12093 if (sign_group[0] == '\0')
12094 sign_group = NULL; // global sign group
12095 else
12096 {
12097 sign_group = vim_strsave(sign_group);
12098 if (sign_group == NULL)
12099 return;
12100 }
12101
12102 // Buffer to place the sign
12103 buf = get_buf_arg(&argvars[2]);
12104 if (buf == NULL)
12105 goto cleanup;
12106
12107 rettv->vval.v_number = sign_jump(sign_id, sign_group, buf);
12108
12109cleanup:
12110 vim_free(sign_group);
12111}
12112
12113/*
Bram Moolenaar162b7142018-12-21 15:17:36 +010012114 * "sign_place()" function
12115 */
12116 static void
12117f_sign_place(typval_T *argvars, typval_T *rettv)
12118{
12119 int sign_id;
12120 char_u *group = NULL;
12121 char_u *sign_name;
12122 buf_T *buf;
12123 dict_T *dict;
12124 dictitem_T *di;
12125 linenr_T lnum = 0;
12126 int prio = SIGN_DEF_PRIO;
12127 int notanum = FALSE;
12128
12129 rettv->vval.v_number = -1;
12130
Bram Moolenaarbdace832019-03-02 10:13:42 +010012131 // Sign identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012132 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012133 if (notanum)
12134 return;
12135 if (sign_id < 0)
12136 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012137 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012138 return;
12139 }
12140
12141 // Sign group
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012142 group = tv_get_string_chk(&argvars[1]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012143 if (group == NULL)
12144 return;
12145 if (group[0] == '\0')
12146 group = NULL; // global sign group
12147 else
12148 {
12149 group = vim_strsave(group);
12150 if (group == NULL)
12151 return;
12152 }
12153
12154 // Sign name
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012155 sign_name = tv_get_string_chk(&argvars[2]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012156 if (sign_name == NULL)
12157 goto cleanup;
12158
12159 // Buffer to place the sign
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012160 buf = get_buf_arg(&argvars[3]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012161 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012162 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012163
12164 if (argvars[4].v_type != VAR_UNKNOWN)
12165 {
12166 if (argvars[4].v_type != VAR_DICT ||
12167 ((dict = argvars[4].vval.v_dict) == NULL))
12168 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012169 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012170 goto cleanup;
12171 }
12172
12173 // Line number where the sign is to be placed
12174 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
12175 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012176 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012177 if (notanum)
12178 goto cleanup;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012179 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012180 }
12181 if ((di = dict_find(dict, (char_u *)"priority", -1)) != NULL)
12182 {
12183 // Sign priority
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012184 prio = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012185 if (notanum)
12186 goto cleanup;
12187 }
12188 }
12189
12190 if (sign_place(&sign_id, group, sign_name, buf, lnum, prio) == OK)
12191 rettv->vval.v_number = sign_id;
12192
12193cleanup:
12194 vim_free(group);
12195}
12196
12197/*
12198 * "sign_undefine()" function
12199 */
12200 static void
12201f_sign_undefine(typval_T *argvars, typval_T *rettv)
12202{
12203 char_u *name;
12204
12205 rettv->vval.v_number = -1;
12206
12207 if (argvars[0].v_type == VAR_UNKNOWN)
12208 {
12209 // Free all the signs
12210 free_signs();
12211 rettv->vval.v_number = 0;
12212 }
12213 else
12214 {
12215 // Free only the specified sign
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012216 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012217 if (name == NULL)
12218 return;
12219
12220 if (sign_undefine_by_name(name) == OK)
12221 rettv->vval.v_number = 0;
12222 }
12223}
12224
12225/*
12226 * "sign_unplace()" function
12227 */
12228 static void
12229f_sign_unplace(typval_T *argvars, typval_T *rettv)
12230{
12231 dict_T *dict;
12232 dictitem_T *di;
12233 int sign_id = 0;
12234 buf_T *buf = NULL;
12235 char_u *group = NULL;
12236
12237 rettv->vval.v_number = -1;
12238
12239 if (argvars[0].v_type != VAR_STRING)
12240 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012241 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012242 return;
12243 }
12244
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012245 group = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012246 if (group[0] == '\0')
12247 group = NULL; // global sign group
12248 else
12249 {
12250 group = vim_strsave(group);
12251 if (group == NULL)
12252 return;
12253 }
12254
12255 if (argvars[1].v_type != VAR_UNKNOWN)
12256 {
12257 if (argvars[1].v_type != VAR_DICT)
12258 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012259 emsg(_(e_dictreq));
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012260 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012261 }
12262 dict = argvars[1].vval.v_dict;
12263
12264 if ((di = dict_find(dict, (char_u *)"buffer", -1)) != NULL)
12265 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012266 buf = get_buf_arg(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012267 if (buf == NULL)
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012268 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012269 }
12270 if (dict_find(dict, (char_u *)"id", -1) != NULL)
12271 sign_id = dict_get_number(dict, (char_u *)"id");
12272 }
12273
12274 if (buf == NULL)
12275 {
12276 // Delete the sign in all the buffers
12277 FOR_ALL_BUFFERS(buf)
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010012278 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012279 rettv->vval.v_number = 0;
12280 }
12281 else
12282 {
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010012283 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012284 rettv->vval.v_number = 0;
12285 }
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012286
12287cleanup:
Bram Moolenaar162b7142018-12-21 15:17:36 +010012288 vim_free(group);
12289}
12290#endif
12291
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012292/*
12293 * "simplify()" function
12294 */
12295 static void
12296f_simplify(typval_T *argvars, typval_T *rettv)
12297{
12298 char_u *p;
12299
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012300 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012301 rettv->vval.v_string = vim_strsave(p);
12302 simplify_filename(rettv->vval.v_string); /* simplify in place */
12303 rettv->v_type = VAR_STRING;
12304}
12305
12306#ifdef FEAT_FLOAT
12307/*
12308 * "sin()" function
12309 */
12310 static void
12311f_sin(typval_T *argvars, typval_T *rettv)
12312{
12313 float_T f = 0.0;
12314
12315 rettv->v_type = VAR_FLOAT;
12316 if (get_float_arg(argvars, &f) == OK)
12317 rettv->vval.v_float = sin(f);
12318 else
12319 rettv->vval.v_float = 0.0;
12320}
12321
12322/*
12323 * "sinh()" function
12324 */
12325 static void
12326f_sinh(typval_T *argvars, typval_T *rettv)
12327{
12328 float_T f = 0.0;
12329
12330 rettv->v_type = VAR_FLOAT;
12331 if (get_float_arg(argvars, &f) == OK)
12332 rettv->vval.v_float = sinh(f);
12333 else
12334 rettv->vval.v_float = 0.0;
12335}
12336#endif
12337
12338static int
12339#ifdef __BORLANDC__
12340 _RTLENTRYF
12341#endif
12342 item_compare(const void *s1, const void *s2);
12343static int
12344#ifdef __BORLANDC__
12345 _RTLENTRYF
12346#endif
12347 item_compare2(const void *s1, const void *s2);
12348
12349/* struct used in the array that's given to qsort() */
12350typedef struct
12351{
12352 listitem_T *item;
12353 int idx;
12354} sortItem_T;
12355
12356/* struct storing information about current sort */
12357typedef struct
12358{
12359 int item_compare_ic;
12360 int item_compare_numeric;
12361 int item_compare_numbers;
12362#ifdef FEAT_FLOAT
12363 int item_compare_float;
12364#endif
12365 char_u *item_compare_func;
12366 partial_T *item_compare_partial;
12367 dict_T *item_compare_selfdict;
12368 int item_compare_func_err;
12369 int item_compare_keep_zero;
12370} sortinfo_T;
12371static sortinfo_T *sortinfo = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012372#define ITEM_COMPARE_FAIL 999
12373
12374/*
12375 * Compare functions for f_sort() and f_uniq() below.
12376 */
12377 static int
12378#ifdef __BORLANDC__
12379_RTLENTRYF
12380#endif
12381item_compare(const void *s1, const void *s2)
12382{
12383 sortItem_T *si1, *si2;
12384 typval_T *tv1, *tv2;
12385 char_u *p1, *p2;
12386 char_u *tofree1 = NULL, *tofree2 = NULL;
12387 int res;
12388 char_u numbuf1[NUMBUFLEN];
12389 char_u numbuf2[NUMBUFLEN];
12390
12391 si1 = (sortItem_T *)s1;
12392 si2 = (sortItem_T *)s2;
12393 tv1 = &si1->item->li_tv;
12394 tv2 = &si2->item->li_tv;
12395
12396 if (sortinfo->item_compare_numbers)
12397 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012398 varnumber_T v1 = tv_get_number(tv1);
12399 varnumber_T v2 = tv_get_number(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012400
12401 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
12402 }
12403
12404#ifdef FEAT_FLOAT
12405 if (sortinfo->item_compare_float)
12406 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012407 float_T v1 = tv_get_float(tv1);
12408 float_T v2 = tv_get_float(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012409
12410 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
12411 }
12412#endif
12413
12414 /* tv2string() puts quotes around a string and allocates memory. Don't do
12415 * that for string variables. Use a single quote when comparing with a
12416 * non-string to do what the docs promise. */
12417 if (tv1->v_type == VAR_STRING)
12418 {
12419 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12420 p1 = (char_u *)"'";
12421 else
12422 p1 = tv1->vval.v_string;
12423 }
12424 else
12425 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
12426 if (tv2->v_type == VAR_STRING)
12427 {
12428 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12429 p2 = (char_u *)"'";
12430 else
12431 p2 = tv2->vval.v_string;
12432 }
12433 else
12434 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
12435 if (p1 == NULL)
12436 p1 = (char_u *)"";
12437 if (p2 == NULL)
12438 p2 = (char_u *)"";
12439 if (!sortinfo->item_compare_numeric)
12440 {
12441 if (sortinfo->item_compare_ic)
12442 res = STRICMP(p1, p2);
12443 else
12444 res = STRCMP(p1, p2);
12445 }
12446 else
12447 {
12448 double n1, n2;
12449 n1 = strtod((char *)p1, (char **)&p1);
12450 n2 = strtod((char *)p2, (char **)&p2);
12451 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
12452 }
12453
12454 /* When the result would be zero, compare the item indexes. Makes the
12455 * sort stable. */
12456 if (res == 0 && !sortinfo->item_compare_keep_zero)
12457 res = si1->idx > si2->idx ? 1 : -1;
12458
12459 vim_free(tofree1);
12460 vim_free(tofree2);
12461 return res;
12462}
12463
12464 static int
12465#ifdef __BORLANDC__
12466_RTLENTRYF
12467#endif
12468item_compare2(const void *s1, const void *s2)
12469{
12470 sortItem_T *si1, *si2;
12471 int res;
12472 typval_T rettv;
12473 typval_T argv[3];
12474 int dummy;
12475 char_u *func_name;
12476 partial_T *partial = sortinfo->item_compare_partial;
12477
12478 /* shortcut after failure in previous call; compare all items equal */
12479 if (sortinfo->item_compare_func_err)
12480 return 0;
12481
12482 si1 = (sortItem_T *)s1;
12483 si2 = (sortItem_T *)s2;
12484
12485 if (partial == NULL)
12486 func_name = sortinfo->item_compare_func;
12487 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012488 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012489
12490 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
12491 * in the copy without changing the original list items. */
12492 copy_tv(&si1->item->li_tv, &argv[0]);
12493 copy_tv(&si2->item->li_tv, &argv[1]);
12494
12495 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
12496 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020012497 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012498 partial, sortinfo->item_compare_selfdict);
12499 clear_tv(&argv[0]);
12500 clear_tv(&argv[1]);
12501
12502 if (res == FAIL)
12503 res = ITEM_COMPARE_FAIL;
12504 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012505 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012506 if (sortinfo->item_compare_func_err)
12507 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
12508 clear_tv(&rettv);
12509
12510 /* When the result would be zero, compare the pointers themselves. Makes
12511 * the sort stable. */
12512 if (res == 0 && !sortinfo->item_compare_keep_zero)
12513 res = si1->idx > si2->idx ? 1 : -1;
12514
12515 return res;
12516}
12517
12518/*
12519 * "sort({list})" function
12520 */
12521 static void
12522do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
12523{
12524 list_T *l;
12525 listitem_T *li;
12526 sortItem_T *ptrs;
12527 sortinfo_T *old_sortinfo;
12528 sortinfo_T info;
12529 long len;
12530 long i;
12531
12532 /* Pointer to current info struct used in compare function. Save and
12533 * restore the current one for nested calls. */
12534 old_sortinfo = sortinfo;
12535 sortinfo = &info;
12536
12537 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012538 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012539 else
12540 {
12541 l = argvars[0].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +010012542 if (l == NULL || var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012543 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
12544 TRUE))
12545 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012546 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012547
12548 len = list_len(l);
12549 if (len <= 1)
12550 goto theend; /* short list sorts pretty quickly */
12551
12552 info.item_compare_ic = FALSE;
12553 info.item_compare_numeric = FALSE;
12554 info.item_compare_numbers = FALSE;
12555#ifdef FEAT_FLOAT
12556 info.item_compare_float = FALSE;
12557#endif
12558 info.item_compare_func = NULL;
12559 info.item_compare_partial = NULL;
12560 info.item_compare_selfdict = NULL;
12561 if (argvars[1].v_type != VAR_UNKNOWN)
12562 {
12563 /* optional second argument: {func} */
12564 if (argvars[1].v_type == VAR_FUNC)
12565 info.item_compare_func = argvars[1].vval.v_string;
12566 else if (argvars[1].v_type == VAR_PARTIAL)
12567 info.item_compare_partial = argvars[1].vval.v_partial;
12568 else
12569 {
12570 int error = FALSE;
12571
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012572 i = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012573 if (error)
12574 goto theend; /* type error; errmsg already given */
12575 if (i == 1)
12576 info.item_compare_ic = TRUE;
12577 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012578 info.item_compare_func = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012579 else if (i != 0)
12580 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012581 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012582 goto theend;
12583 }
12584 if (info.item_compare_func != NULL)
12585 {
12586 if (*info.item_compare_func == NUL)
12587 {
12588 /* empty string means default sort */
12589 info.item_compare_func = NULL;
12590 }
12591 else if (STRCMP(info.item_compare_func, "n") == 0)
12592 {
12593 info.item_compare_func = NULL;
12594 info.item_compare_numeric = TRUE;
12595 }
12596 else if (STRCMP(info.item_compare_func, "N") == 0)
12597 {
12598 info.item_compare_func = NULL;
12599 info.item_compare_numbers = TRUE;
12600 }
12601#ifdef FEAT_FLOAT
12602 else if (STRCMP(info.item_compare_func, "f") == 0)
12603 {
12604 info.item_compare_func = NULL;
12605 info.item_compare_float = TRUE;
12606 }
12607#endif
12608 else if (STRCMP(info.item_compare_func, "i") == 0)
12609 {
12610 info.item_compare_func = NULL;
12611 info.item_compare_ic = TRUE;
12612 }
12613 }
12614 }
12615
12616 if (argvars[2].v_type != VAR_UNKNOWN)
12617 {
12618 /* optional third argument: {dict} */
12619 if (argvars[2].v_type != VAR_DICT)
12620 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012621 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012622 goto theend;
12623 }
12624 info.item_compare_selfdict = argvars[2].vval.v_dict;
12625 }
12626 }
12627
12628 /* Make an array with each entry pointing to an item in the List. */
12629 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
12630 if (ptrs == NULL)
12631 goto theend;
12632
12633 i = 0;
12634 if (sort)
12635 {
12636 /* sort(): ptrs will be the list to sort */
12637 for (li = l->lv_first; li != NULL; li = li->li_next)
12638 {
12639 ptrs[i].item = li;
12640 ptrs[i].idx = i;
12641 ++i;
12642 }
12643
12644 info.item_compare_func_err = FALSE;
12645 info.item_compare_keep_zero = FALSE;
12646 /* test the compare function */
12647 if ((info.item_compare_func != NULL
12648 || info.item_compare_partial != NULL)
12649 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
12650 == ITEM_COMPARE_FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012651 emsg(_("E702: Sort compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012652 else
12653 {
12654 /* Sort the array with item pointers. */
12655 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
12656 info.item_compare_func == NULL
12657 && info.item_compare_partial == NULL
12658 ? item_compare : item_compare2);
12659
12660 if (!info.item_compare_func_err)
12661 {
12662 /* Clear the List and append the items in sorted order. */
12663 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
12664 l->lv_len = 0;
12665 for (i = 0; i < len; ++i)
12666 list_append(l, ptrs[i].item);
12667 }
12668 }
12669 }
12670 else
12671 {
12672 int (*item_compare_func_ptr)(const void *, const void *);
12673
12674 /* f_uniq(): ptrs will be a stack of items to remove */
12675 info.item_compare_func_err = FALSE;
12676 info.item_compare_keep_zero = TRUE;
12677 item_compare_func_ptr = info.item_compare_func != NULL
12678 || info.item_compare_partial != NULL
12679 ? item_compare2 : item_compare;
12680
12681 for (li = l->lv_first; li != NULL && li->li_next != NULL;
12682 li = li->li_next)
12683 {
12684 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
12685 == 0)
12686 ptrs[i++].item = li;
12687 if (info.item_compare_func_err)
12688 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012689 emsg(_("E882: Uniq compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012690 break;
12691 }
12692 }
12693
12694 if (!info.item_compare_func_err)
12695 {
12696 while (--i >= 0)
12697 {
12698 li = ptrs[i].item->li_next;
12699 ptrs[i].item->li_next = li->li_next;
12700 if (li->li_next != NULL)
12701 li->li_next->li_prev = ptrs[i].item;
12702 else
12703 l->lv_last = ptrs[i].item;
12704 list_fix_watch(l, li);
12705 listitem_free(li);
12706 l->lv_len--;
12707 }
12708 }
12709 }
12710
12711 vim_free(ptrs);
12712 }
12713theend:
12714 sortinfo = old_sortinfo;
12715}
12716
12717/*
12718 * "sort({list})" function
12719 */
12720 static void
12721f_sort(typval_T *argvars, typval_T *rettv)
12722{
12723 do_sort_uniq(argvars, rettv, TRUE);
12724}
12725
12726/*
12727 * "uniq({list})" function
12728 */
12729 static void
12730f_uniq(typval_T *argvars, typval_T *rettv)
12731{
12732 do_sort_uniq(argvars, rettv, FALSE);
12733}
12734
12735/*
12736 * "soundfold({word})" function
12737 */
12738 static void
12739f_soundfold(typval_T *argvars, typval_T *rettv)
12740{
12741 char_u *s;
12742
12743 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012744 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012745#ifdef FEAT_SPELL
12746 rettv->vval.v_string = eval_soundfold(s);
12747#else
12748 rettv->vval.v_string = vim_strsave(s);
12749#endif
12750}
12751
12752/*
12753 * "spellbadword()" function
12754 */
12755 static void
12756f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
12757{
12758 char_u *word = (char_u *)"";
12759 hlf_T attr = HLF_COUNT;
12760 int len = 0;
12761
12762 if (rettv_list_alloc(rettv) == FAIL)
12763 return;
12764
12765#ifdef FEAT_SPELL
12766 if (argvars[0].v_type == VAR_UNKNOWN)
12767 {
12768 /* Find the start and length of the badly spelled word. */
12769 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
12770 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012771 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012772 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012773 curwin->w_set_curswant = TRUE;
12774 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012775 }
12776 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
12777 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012778 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012779 int capcol = -1;
12780
12781 if (str != NULL)
12782 {
12783 /* Check the argument for spelling. */
12784 while (*str != NUL)
12785 {
12786 len = spell_check(curwin, str, &attr, &capcol, FALSE);
12787 if (attr != HLF_COUNT)
12788 {
12789 word = str;
12790 break;
12791 }
12792 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020012793 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012794 }
12795 }
12796 }
12797#endif
12798
12799 list_append_string(rettv->vval.v_list, word, len);
12800 list_append_string(rettv->vval.v_list, (char_u *)(
12801 attr == HLF_SPB ? "bad" :
12802 attr == HLF_SPR ? "rare" :
12803 attr == HLF_SPL ? "local" :
12804 attr == HLF_SPC ? "caps" :
12805 ""), -1);
12806}
12807
12808/*
12809 * "spellsuggest()" function
12810 */
12811 static void
12812f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
12813{
12814#ifdef FEAT_SPELL
12815 char_u *str;
12816 int typeerr = FALSE;
12817 int maxcount;
12818 garray_T ga;
12819 int i;
12820 listitem_T *li;
12821 int need_capital = FALSE;
12822#endif
12823
12824 if (rettv_list_alloc(rettv) == FAIL)
12825 return;
12826
12827#ifdef FEAT_SPELL
12828 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
12829 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012830 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012831 if (argvars[1].v_type != VAR_UNKNOWN)
12832 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012833 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012834 if (maxcount <= 0)
12835 return;
12836 if (argvars[2].v_type != VAR_UNKNOWN)
12837 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012838 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012839 if (typeerr)
12840 return;
12841 }
12842 }
12843 else
12844 maxcount = 25;
12845
12846 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
12847
12848 for (i = 0; i < ga.ga_len; ++i)
12849 {
12850 str = ((char_u **)ga.ga_data)[i];
12851
12852 li = listitem_alloc();
12853 if (li == NULL)
12854 vim_free(str);
12855 else
12856 {
12857 li->li_tv.v_type = VAR_STRING;
12858 li->li_tv.v_lock = 0;
12859 li->li_tv.vval.v_string = str;
12860 list_append(rettv->vval.v_list, li);
12861 }
12862 }
12863 ga_clear(&ga);
12864 }
12865#endif
12866}
12867
12868 static void
12869f_split(typval_T *argvars, typval_T *rettv)
12870{
12871 char_u *str;
12872 char_u *end;
12873 char_u *pat = NULL;
12874 regmatch_T regmatch;
12875 char_u patbuf[NUMBUFLEN];
12876 char_u *save_cpo;
12877 int match;
12878 colnr_T col = 0;
12879 int keepempty = FALSE;
12880 int typeerr = FALSE;
12881
12882 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12883 save_cpo = p_cpo;
12884 p_cpo = (char_u *)"";
12885
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012886 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012887 if (argvars[1].v_type != VAR_UNKNOWN)
12888 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012889 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012890 if (pat == NULL)
12891 typeerr = TRUE;
12892 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012893 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012894 }
12895 if (pat == NULL || *pat == NUL)
12896 pat = (char_u *)"[\\x01- ]\\+";
12897
12898 if (rettv_list_alloc(rettv) == FAIL)
12899 return;
12900 if (typeerr)
12901 return;
12902
12903 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12904 if (regmatch.regprog != NULL)
12905 {
12906 regmatch.rm_ic = FALSE;
12907 while (*str != NUL || keepempty)
12908 {
12909 if (*str == NUL)
12910 match = FALSE; /* empty item at the end */
12911 else
12912 match = vim_regexec_nl(&regmatch, str, col);
12913 if (match)
12914 end = regmatch.startp[0];
12915 else
12916 end = str + STRLEN(str);
12917 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
12918 && *str != NUL && match && end < regmatch.endp[0]))
12919 {
12920 if (list_append_string(rettv->vval.v_list, str,
12921 (int)(end - str)) == FAIL)
12922 break;
12923 }
12924 if (!match)
12925 break;
Bram Moolenaar13505972019-01-24 15:04:48 +010012926 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012927 if (regmatch.endp[0] > str)
12928 col = 0;
12929 else
Bram Moolenaar13505972019-01-24 15:04:48 +010012930 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012931 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012932 str = regmatch.endp[0];
12933 }
12934
12935 vim_regfree(regmatch.regprog);
12936 }
12937
12938 p_cpo = save_cpo;
12939}
12940
12941#ifdef FEAT_FLOAT
12942/*
12943 * "sqrt()" function
12944 */
12945 static void
12946f_sqrt(typval_T *argvars, typval_T *rettv)
12947{
12948 float_T f = 0.0;
12949
12950 rettv->v_type = VAR_FLOAT;
12951 if (get_float_arg(argvars, &f) == OK)
12952 rettv->vval.v_float = sqrt(f);
12953 else
12954 rettv->vval.v_float = 0.0;
12955}
12956
12957/*
12958 * "str2float()" function
12959 */
12960 static void
12961f_str2float(typval_T *argvars, typval_T *rettv)
12962{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012963 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010012964 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012965
Bram Moolenaar08243d22017-01-10 16:12:29 +010012966 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012967 p = skipwhite(p + 1);
12968 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010012969 if (isneg)
12970 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012971 rettv->v_type = VAR_FLOAT;
12972}
12973#endif
12974
12975/*
Bram Moolenaar9d401282019-04-06 13:18:12 +020012976 * "str2list()" function
12977 */
12978 static void
12979f_str2list(typval_T *argvars, typval_T *rettv)
12980{
12981 char_u *p;
12982 int utf8 = FALSE;
12983
12984 if (rettv_list_alloc(rettv) == FAIL)
12985 return;
12986
12987 if (argvars[1].v_type != VAR_UNKNOWN)
12988 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
12989
12990 p = tv_get_string(&argvars[0]);
12991
12992 if (has_mbyte || utf8)
12993 {
12994 int (*ptr2len)(char_u *);
12995 int (*ptr2char)(char_u *);
12996
12997 if (utf8 || enc_utf8)
12998 {
12999 ptr2len = utf_ptr2len;
13000 ptr2char = utf_ptr2char;
13001 }
13002 else
13003 {
13004 ptr2len = mb_ptr2len;
13005 ptr2char = mb_ptr2char;
13006 }
13007
13008 for ( ; *p != NUL; p += (*ptr2len)(p))
13009 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
13010 }
13011 else
13012 for ( ; *p != NUL; ++p)
13013 list_append_number(rettv->vval.v_list, *p);
13014}
13015
13016/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013017 * "str2nr()" function
13018 */
13019 static void
13020f_str2nr(typval_T *argvars, typval_T *rettv)
13021{
13022 int base = 10;
13023 char_u *p;
13024 varnumber_T n;
13025 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010013026 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013027
13028 if (argvars[1].v_type != VAR_UNKNOWN)
13029 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013030 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013031 if (base != 2 && base != 8 && base != 10 && base != 16)
13032 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013033 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013034 return;
13035 }
13036 }
13037
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013038 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010013039 isneg = (*p == '-');
13040 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013041 p = skipwhite(p + 1);
13042 switch (base)
13043 {
13044 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
13045 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
13046 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
13047 default: what = 0;
13048 }
13049 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010013050 if (isneg)
13051 rettv->vval.v_number = -n;
13052 else
13053 rettv->vval.v_number = n;
13054
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013055}
13056
13057#ifdef HAVE_STRFTIME
13058/*
13059 * "strftime({format}[, {time}])" function
13060 */
13061 static void
13062f_strftime(typval_T *argvars, typval_T *rettv)
13063{
13064 char_u result_buf[256];
13065 struct tm *curtime;
13066 time_t seconds;
13067 char_u *p;
13068
13069 rettv->v_type = VAR_STRING;
13070
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013071 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013072 if (argvars[1].v_type == VAR_UNKNOWN)
13073 seconds = time(NULL);
13074 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013075 seconds = (time_t)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013076 curtime = localtime(&seconds);
13077 /* MSVC returns NULL for an invalid value of seconds. */
13078 if (curtime == NULL)
13079 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
13080 else
13081 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013082 vimconv_T conv;
13083 char_u *enc;
13084
13085 conv.vc_type = CONV_NONE;
13086 enc = enc_locale();
13087 convert_setup(&conv, p_enc, enc);
13088 if (conv.vc_type != CONV_NONE)
13089 p = string_convert(&conv, p, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013090 if (p != NULL)
13091 (void)strftime((char *)result_buf, sizeof(result_buf),
13092 (char *)p, curtime);
13093 else
13094 result_buf[0] = NUL;
13095
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013096 if (conv.vc_type != CONV_NONE)
13097 vim_free(p);
13098 convert_setup(&conv, enc, p_enc);
13099 if (conv.vc_type != CONV_NONE)
13100 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
13101 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013102 rettv->vval.v_string = vim_strsave(result_buf);
13103
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013104 /* Release conversion descriptors */
13105 convert_setup(&conv, NULL, NULL);
13106 vim_free(enc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013107 }
13108}
13109#endif
13110
13111/*
13112 * "strgetchar()" function
13113 */
13114 static void
13115f_strgetchar(typval_T *argvars, typval_T *rettv)
13116{
13117 char_u *str;
13118 int len;
13119 int error = FALSE;
13120 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +010013121 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013122
13123 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013124 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013125 if (str == NULL)
13126 return;
13127 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013128 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013129 if (error)
13130 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013131
Bram Moolenaar13505972019-01-24 15:04:48 +010013132 while (charidx >= 0 && byteidx < len)
13133 {
13134 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013135 {
Bram Moolenaar13505972019-01-24 15:04:48 +010013136 rettv->vval.v_number = mb_ptr2char(str + byteidx);
13137 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013138 }
Bram Moolenaar13505972019-01-24 15:04:48 +010013139 --charidx;
13140 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013141 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013142}
13143
13144/*
13145 * "stridx()" function
13146 */
13147 static void
13148f_stridx(typval_T *argvars, typval_T *rettv)
13149{
13150 char_u buf[NUMBUFLEN];
13151 char_u *needle;
13152 char_u *haystack;
13153 char_u *save_haystack;
13154 char_u *pos;
13155 int start_idx;
13156
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013157 needle = tv_get_string_chk(&argvars[1]);
13158 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013159 rettv->vval.v_number = -1;
13160 if (needle == NULL || haystack == NULL)
13161 return; /* type error; errmsg already given */
13162
13163 if (argvars[2].v_type != VAR_UNKNOWN)
13164 {
13165 int error = FALSE;
13166
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013167 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013168 if (error || start_idx >= (int)STRLEN(haystack))
13169 return;
13170 if (start_idx >= 0)
13171 haystack += start_idx;
13172 }
13173
13174 pos = (char_u *)strstr((char *)haystack, (char *)needle);
13175 if (pos != NULL)
13176 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
13177}
13178
13179/*
13180 * "string()" function
13181 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010013182 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013183f_string(typval_T *argvars, typval_T *rettv)
13184{
13185 char_u *tofree;
13186 char_u numbuf[NUMBUFLEN];
13187
13188 rettv->v_type = VAR_STRING;
13189 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
13190 get_copyID());
13191 /* Make a copy if we have a value but it's not in allocated memory. */
13192 if (rettv->vval.v_string != NULL && tofree == NULL)
13193 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
13194}
13195
13196/*
13197 * "strlen()" function
13198 */
13199 static void
13200f_strlen(typval_T *argvars, typval_T *rettv)
13201{
13202 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013203 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013204}
13205
13206/*
13207 * "strchars()" function
13208 */
13209 static void
13210f_strchars(typval_T *argvars, typval_T *rettv)
13211{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013212 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013213 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013214 varnumber_T len = 0;
13215 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013216
13217 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013218 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013219 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013220 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013221 else
13222 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013223 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
13224 while (*s != NUL)
13225 {
13226 func_mb_ptr2char_adv(&s);
13227 ++len;
13228 }
13229 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013230 }
13231}
13232
13233/*
13234 * "strdisplaywidth()" function
13235 */
13236 static void
13237f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
13238{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013239 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013240 int col = 0;
13241
13242 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013243 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013244
13245 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
13246}
13247
13248/*
13249 * "strwidth()" function
13250 */
13251 static void
13252f_strwidth(typval_T *argvars, typval_T *rettv)
13253{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013254 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013255
Bram Moolenaar13505972019-01-24 15:04:48 +010013256 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013257}
13258
13259/*
13260 * "strcharpart()" function
13261 */
13262 static void
13263f_strcharpart(typval_T *argvars, typval_T *rettv)
13264{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013265 char_u *p;
13266 int nchar;
13267 int nbyte = 0;
13268 int charlen;
13269 int len = 0;
13270 int slen;
13271 int error = FALSE;
13272
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013273 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013274 slen = (int)STRLEN(p);
13275
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013276 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013277 if (!error)
13278 {
13279 if (nchar > 0)
13280 while (nchar > 0 && nbyte < slen)
13281 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020013282 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013283 --nchar;
13284 }
13285 else
13286 nbyte = nchar;
13287 if (argvars[2].v_type != VAR_UNKNOWN)
13288 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013289 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013290 while (charlen > 0 && nbyte + len < slen)
13291 {
13292 int off = nbyte + len;
13293
13294 if (off < 0)
13295 len += 1;
13296 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020013297 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013298 --charlen;
13299 }
13300 }
13301 else
13302 len = slen - nbyte; /* default: all bytes that are available. */
13303 }
13304
13305 /*
13306 * Only return the overlap between the specified part and the actual
13307 * string.
13308 */
13309 if (nbyte < 0)
13310 {
13311 len += nbyte;
13312 nbyte = 0;
13313 }
13314 else if (nbyte > slen)
13315 nbyte = slen;
13316 if (len < 0)
13317 len = 0;
13318 else if (nbyte + len > slen)
13319 len = slen - nbyte;
13320
13321 rettv->v_type = VAR_STRING;
13322 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013323}
13324
13325/*
13326 * "strpart()" function
13327 */
13328 static void
13329f_strpart(typval_T *argvars, typval_T *rettv)
13330{
13331 char_u *p;
13332 int n;
13333 int len;
13334 int slen;
13335 int error = FALSE;
13336
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013337 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013338 slen = (int)STRLEN(p);
13339
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013340 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013341 if (error)
13342 len = 0;
13343 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013344 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013345 else
13346 len = slen - n; /* default len: all bytes that are available. */
13347
13348 /*
13349 * Only return the overlap between the specified part and the actual
13350 * string.
13351 */
13352 if (n < 0)
13353 {
13354 len += n;
13355 n = 0;
13356 }
13357 else if (n > slen)
13358 n = slen;
13359 if (len < 0)
13360 len = 0;
13361 else if (n + len > slen)
13362 len = slen - n;
13363
13364 rettv->v_type = VAR_STRING;
13365 rettv->vval.v_string = vim_strnsave(p + n, len);
13366}
13367
13368/*
13369 * "strridx()" function
13370 */
13371 static void
13372f_strridx(typval_T *argvars, typval_T *rettv)
13373{
13374 char_u buf[NUMBUFLEN];
13375 char_u *needle;
13376 char_u *haystack;
13377 char_u *rest;
13378 char_u *lastmatch = NULL;
13379 int haystack_len, end_idx;
13380
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013381 needle = tv_get_string_chk(&argvars[1]);
13382 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013383
13384 rettv->vval.v_number = -1;
13385 if (needle == NULL || haystack == NULL)
13386 return; /* type error; errmsg already given */
13387
13388 haystack_len = (int)STRLEN(haystack);
13389 if (argvars[2].v_type != VAR_UNKNOWN)
13390 {
13391 /* Third argument: upper limit for index */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013392 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013393 if (end_idx < 0)
13394 return; /* can never find a match */
13395 }
13396 else
13397 end_idx = haystack_len;
13398
13399 if (*needle == NUL)
13400 {
13401 /* Empty string matches past the end. */
13402 lastmatch = haystack + end_idx;
13403 }
13404 else
13405 {
13406 for (rest = haystack; *rest != '\0'; ++rest)
13407 {
13408 rest = (char_u *)strstr((char *)rest, (char *)needle);
13409 if (rest == NULL || rest > haystack + end_idx)
13410 break;
13411 lastmatch = rest;
13412 }
13413 }
13414
13415 if (lastmatch == NULL)
13416 rettv->vval.v_number = -1;
13417 else
13418 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
13419}
13420
13421/*
13422 * "strtrans()" function
13423 */
13424 static void
13425f_strtrans(typval_T *argvars, typval_T *rettv)
13426{
13427 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013428 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013429}
13430
13431/*
13432 * "submatch()" function
13433 */
13434 static void
13435f_submatch(typval_T *argvars, typval_T *rettv)
13436{
13437 int error = FALSE;
13438 int no;
13439 int retList = 0;
13440
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013441 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013442 if (error)
13443 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013444 if (no < 0 || no >= NSUBEXP)
13445 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013446 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +010013447 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013448 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013449 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013450 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013451 if (error)
13452 return;
13453
13454 if (retList == 0)
13455 {
13456 rettv->v_type = VAR_STRING;
13457 rettv->vval.v_string = reg_submatch(no);
13458 }
13459 else
13460 {
13461 rettv->v_type = VAR_LIST;
13462 rettv->vval.v_list = reg_submatch_list(no);
13463 }
13464}
13465
13466/*
13467 * "substitute()" function
13468 */
13469 static void
13470f_substitute(typval_T *argvars, typval_T *rettv)
13471{
13472 char_u patbuf[NUMBUFLEN];
13473 char_u subbuf[NUMBUFLEN];
13474 char_u flagsbuf[NUMBUFLEN];
13475
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013476 char_u *str = tv_get_string_chk(&argvars[0]);
13477 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013478 char_u *sub = NULL;
13479 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013480 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013481
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013482 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
13483 expr = &argvars[2];
13484 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013485 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013486
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013487 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013488 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
13489 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013490 rettv->vval.v_string = NULL;
13491 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013492 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013493}
13494
13495/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013496 * "swapinfo(swap_filename)" function
13497 */
13498 static void
13499f_swapinfo(typval_T *argvars, typval_T *rettv)
13500{
13501 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013502 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013503}
13504
13505/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020013506 * "swapname(expr)" function
13507 */
13508 static void
13509f_swapname(typval_T *argvars, typval_T *rettv)
13510{
13511 buf_T *buf;
13512
13513 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010013514 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +020013515 if (buf == NULL || buf->b_ml.ml_mfp == NULL
13516 || buf->b_ml.ml_mfp->mf_fname == NULL)
13517 rettv->vval.v_string = NULL;
13518 else
13519 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
13520}
13521
13522/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013523 * "synID(lnum, col, trans)" function
13524 */
13525 static void
13526f_synID(typval_T *argvars UNUSED, typval_T *rettv)
13527{
13528 int id = 0;
13529#ifdef FEAT_SYN_HL
13530 linenr_T lnum;
13531 colnr_T col;
13532 int trans;
13533 int transerr = FALSE;
13534
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013535 lnum = tv_get_lnum(argvars); /* -1 on type error */
13536 col = (linenr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
13537 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013538
13539 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13540 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
13541 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
13542#endif
13543
13544 rettv->vval.v_number = id;
13545}
13546
13547/*
13548 * "synIDattr(id, what [, mode])" function
13549 */
13550 static void
13551f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
13552{
13553 char_u *p = NULL;
13554#ifdef FEAT_SYN_HL
13555 int id;
13556 char_u *what;
13557 char_u *mode;
13558 char_u modebuf[NUMBUFLEN];
13559 int modec;
13560
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013561 id = (int)tv_get_number(&argvars[0]);
13562 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013563 if (argvars[2].v_type != VAR_UNKNOWN)
13564 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013565 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013566 modec = TOLOWER_ASC(mode[0]);
13567 if (modec != 't' && modec != 'c' && modec != 'g')
13568 modec = 0; /* replace invalid with current */
13569 }
13570 else
13571 {
13572#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
13573 if (USE_24BIT)
13574 modec = 'g';
13575 else
13576#endif
13577 if (t_colors > 1)
13578 modec = 'c';
13579 else
13580 modec = 't';
13581 }
13582
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013583 switch (TOLOWER_ASC(what[0]))
13584 {
13585 case 'b':
13586 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
13587 p = highlight_color(id, what, modec);
13588 else /* bold */
13589 p = highlight_has_attr(id, HL_BOLD, modec);
13590 break;
13591
13592 case 'f': /* fg[#] or font */
13593 p = highlight_color(id, what, modec);
13594 break;
13595
13596 case 'i':
13597 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
13598 p = highlight_has_attr(id, HL_INVERSE, modec);
13599 else /* italic */
13600 p = highlight_has_attr(id, HL_ITALIC, modec);
13601 break;
13602
13603 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020013604 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013605 break;
13606
13607 case 'r': /* reverse */
13608 p = highlight_has_attr(id, HL_INVERSE, modec);
13609 break;
13610
13611 case 's':
13612 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
13613 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020013614 /* strikeout */
13615 else if (TOLOWER_ASC(what[1]) == 't' &&
13616 TOLOWER_ASC(what[2]) == 'r')
13617 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013618 else /* standout */
13619 p = highlight_has_attr(id, HL_STANDOUT, modec);
13620 break;
13621
13622 case 'u':
13623 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
13624 /* underline */
13625 p = highlight_has_attr(id, HL_UNDERLINE, modec);
13626 else
13627 /* undercurl */
13628 p = highlight_has_attr(id, HL_UNDERCURL, modec);
13629 break;
13630 }
13631
13632 if (p != NULL)
13633 p = vim_strsave(p);
13634#endif
13635 rettv->v_type = VAR_STRING;
13636 rettv->vval.v_string = p;
13637}
13638
13639/*
13640 * "synIDtrans(id)" function
13641 */
13642 static void
13643f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
13644{
13645 int id;
13646
13647#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013648 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013649
13650 if (id > 0)
13651 id = syn_get_final_id(id);
13652 else
13653#endif
13654 id = 0;
13655
13656 rettv->vval.v_number = id;
13657}
13658
13659/*
13660 * "synconcealed(lnum, col)" function
13661 */
13662 static void
13663f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
13664{
13665#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
13666 linenr_T lnum;
13667 colnr_T col;
13668 int syntax_flags = 0;
13669 int cchar;
13670 int matchid = 0;
13671 char_u str[NUMBUFLEN];
13672#endif
13673
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013674 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013675
13676#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013677 lnum = tv_get_lnum(argvars); /* -1 on type error */
13678 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013679
13680 vim_memset(str, NUL, sizeof(str));
13681
13682 if (rettv_list_alloc(rettv) != FAIL)
13683 {
13684 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13685 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13686 && curwin->w_p_cole > 0)
13687 {
13688 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
13689 syntax_flags = get_syntax_info(&matchid);
13690
13691 /* get the conceal character */
13692 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
13693 {
13694 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020013695 if (cchar == NUL && curwin->w_p_cole == 1)
13696 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013697 if (cchar != NUL)
13698 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013699 if (has_mbyte)
13700 (*mb_char2bytes)(cchar, str);
13701 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013702 str[0] = cchar;
13703 }
13704 }
13705 }
13706
13707 list_append_number(rettv->vval.v_list,
13708 (syntax_flags & HL_CONCEAL) != 0);
13709 /* -1 to auto-determine strlen */
13710 list_append_string(rettv->vval.v_list, str, -1);
13711 list_append_number(rettv->vval.v_list, matchid);
13712 }
13713#endif
13714}
13715
13716/*
13717 * "synstack(lnum, col)" function
13718 */
13719 static void
13720f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
13721{
13722#ifdef FEAT_SYN_HL
13723 linenr_T lnum;
13724 colnr_T col;
13725 int i;
13726 int id;
13727#endif
13728
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013729 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013730
13731#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013732 lnum = tv_get_lnum(argvars); /* -1 on type error */
13733 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013734
13735 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13736 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13737 && rettv_list_alloc(rettv) != FAIL)
13738 {
13739 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
13740 for (i = 0; ; ++i)
13741 {
13742 id = syn_get_stack_item(i);
13743 if (id < 0)
13744 break;
13745 if (list_append_number(rettv->vval.v_list, id) == FAIL)
13746 break;
13747 }
13748 }
13749#endif
13750}
13751
13752 static void
13753get_cmd_output_as_rettv(
13754 typval_T *argvars,
13755 typval_T *rettv,
13756 int retlist)
13757{
13758 char_u *res = NULL;
13759 char_u *p;
13760 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013761 int err = FALSE;
13762 FILE *fd;
13763 list_T *list = NULL;
13764 int flags = SHELL_SILENT;
13765
13766 rettv->v_type = VAR_STRING;
13767 rettv->vval.v_string = NULL;
13768 if (check_restricted() || check_secure())
13769 goto errret;
13770
13771 if (argvars[1].v_type != VAR_UNKNOWN)
13772 {
13773 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010013774 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013775 * command.
13776 */
13777 if ((infile = vim_tempname('i', TRUE)) == NULL)
13778 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013779 emsg(_(e_notmp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013780 goto errret;
13781 }
13782
13783 fd = mch_fopen((char *)infile, WRITEBIN);
13784 if (fd == NULL)
13785 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013786 semsg(_(e_notopen), infile);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013787 goto errret;
13788 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010013789 if (argvars[1].v_type == VAR_NUMBER)
13790 {
13791 linenr_T lnum;
13792 buf_T *buf;
13793
13794 buf = buflist_findnr(argvars[1].vval.v_number);
13795 if (buf == NULL)
13796 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013797 semsg(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010013798 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010013799 goto errret;
13800 }
13801
13802 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
13803 {
13804 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
13805 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
13806 {
13807 err = TRUE;
13808 break;
13809 }
13810 if (putc(NL, fd) == EOF)
13811 {
13812 err = TRUE;
13813 break;
13814 }
13815 }
13816 }
13817 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013818 {
13819 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
13820 err = TRUE;
13821 }
13822 else
13823 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010013824 size_t len;
13825 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013826
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013827 p = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013828 if (p == NULL)
13829 {
13830 fclose(fd);
13831 goto errret; /* type error; errmsg already given */
13832 }
13833 len = STRLEN(p);
13834 if (len > 0 && fwrite(p, len, 1, fd) != 1)
13835 err = TRUE;
13836 }
13837 if (fclose(fd) != 0)
13838 err = TRUE;
13839 if (err)
13840 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013841 emsg(_("E677: Error writing temp file"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013842 goto errret;
13843 }
13844 }
13845
13846 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
13847 * echoes typeahead, that messes up the display. */
13848 if (!msg_silent)
13849 flags += SHELL_COOKED;
13850
13851 if (retlist)
13852 {
13853 int len;
13854 listitem_T *li;
13855 char_u *s = NULL;
13856 char_u *start;
13857 char_u *end;
13858 int i;
13859
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013860 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013861 if (res == NULL)
13862 goto errret;
13863
13864 list = list_alloc();
13865 if (list == NULL)
13866 goto errret;
13867
13868 for (i = 0; i < len; ++i)
13869 {
13870 start = res + i;
13871 while (i < len && res[i] != NL)
13872 ++i;
13873 end = res + i;
13874
13875 s = alloc((unsigned)(end - start + 1));
13876 if (s == NULL)
13877 goto errret;
13878
13879 for (p = s; start < end; ++p, ++start)
13880 *p = *start == NUL ? NL : *start;
13881 *p = NUL;
13882
13883 li = listitem_alloc();
13884 if (li == NULL)
13885 {
13886 vim_free(s);
13887 goto errret;
13888 }
13889 li->li_tv.v_type = VAR_STRING;
13890 li->li_tv.v_lock = 0;
13891 li->li_tv.vval.v_string = s;
13892 list_append(list, li);
13893 }
13894
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013895 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013896 list = NULL;
13897 }
13898 else
13899 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013900 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar00590742019-02-15 21:06:09 +010013901#ifdef USE_CRNL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013902 /* translate <CR><NL> into <NL> */
13903 if (res != NULL)
13904 {
13905 char_u *s, *d;
13906
13907 d = res;
13908 for (s = res; *s; ++s)
13909 {
13910 if (s[0] == CAR && s[1] == NL)
13911 ++s;
13912 *d++ = *s;
13913 }
13914 *d = NUL;
13915 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013916#endif
13917 rettv->vval.v_string = res;
13918 res = NULL;
13919 }
13920
13921errret:
13922 if (infile != NULL)
13923 {
13924 mch_remove(infile);
13925 vim_free(infile);
13926 }
13927 if (res != NULL)
13928 vim_free(res);
13929 if (list != NULL)
13930 list_free(list);
13931}
13932
13933/*
13934 * "system()" function
13935 */
13936 static void
13937f_system(typval_T *argvars, typval_T *rettv)
13938{
13939 get_cmd_output_as_rettv(argvars, rettv, FALSE);
13940}
13941
13942/*
13943 * "systemlist()" function
13944 */
13945 static void
13946f_systemlist(typval_T *argvars, typval_T *rettv)
13947{
13948 get_cmd_output_as_rettv(argvars, rettv, TRUE);
13949}
13950
13951/*
13952 * "tabpagebuflist()" function
13953 */
13954 static void
13955f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13956{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013957 tabpage_T *tp;
13958 win_T *wp = NULL;
13959
13960 if (argvars[0].v_type == VAR_UNKNOWN)
13961 wp = firstwin;
13962 else
13963 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013964 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013965 if (tp != NULL)
13966 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
13967 }
13968 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
13969 {
13970 for (; wp != NULL; wp = wp->w_next)
13971 if (list_append_number(rettv->vval.v_list,
13972 wp->w_buffer->b_fnum) == FAIL)
13973 break;
13974 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013975}
13976
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013977/*
13978 * "tabpagenr()" function
13979 */
13980 static void
13981f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
13982{
13983 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013984 char_u *arg;
13985
13986 if (argvars[0].v_type != VAR_UNKNOWN)
13987 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013988 arg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013989 nr = 0;
13990 if (arg != NULL)
13991 {
13992 if (STRCMP(arg, "$") == 0)
13993 nr = tabpage_index(NULL) - 1;
13994 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013995 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013996 }
13997 }
13998 else
13999 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014000 rettv->vval.v_number = nr;
14001}
14002
14003
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014004/*
14005 * Common code for tabpagewinnr() and winnr().
14006 */
14007 static int
14008get_winnr(tabpage_T *tp, typval_T *argvar)
14009{
14010 win_T *twin;
14011 int nr = 1;
14012 win_T *wp;
14013 char_u *arg;
14014
14015 twin = (tp == curtab) ? curwin : tp->tp_curwin;
14016 if (argvar->v_type != VAR_UNKNOWN)
14017 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020014018 int invalid_arg = FALSE;
14019
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014020 arg = tv_get_string_chk(argvar);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014021 if (arg == NULL)
14022 nr = 0; /* type error; errmsg already given */
14023 else if (STRCMP(arg, "$") == 0)
14024 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
14025 else if (STRCMP(arg, "#") == 0)
14026 {
14027 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
14028 if (twin == NULL)
14029 nr = 0;
14030 }
14031 else
14032 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020014033 long count;
14034 char_u *endp;
14035
14036 // Extract the window count (if specified). e.g. winnr('3j')
14037 count = strtol((char *)arg, (char **)&endp, 10);
14038 if (count <= 0)
14039 count = 1; // if count is not specified, default to 1
14040 if (endp != NULL && *endp != '\0')
14041 {
14042 if (STRCMP(endp, "j") == 0)
14043 twin = win_vert_neighbor(tp, twin, FALSE, count);
14044 else if (STRCMP(endp, "k") == 0)
14045 twin = win_vert_neighbor(tp, twin, TRUE, count);
14046 else if (STRCMP(endp, "h") == 0)
14047 twin = win_horz_neighbor(tp, twin, TRUE, count);
14048 else if (STRCMP(endp, "l") == 0)
14049 twin = win_horz_neighbor(tp, twin, FALSE, count);
14050 else
14051 invalid_arg = TRUE;
14052 }
14053 else
14054 invalid_arg = TRUE;
14055 }
14056
14057 if (invalid_arg)
14058 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014059 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014060 nr = 0;
14061 }
14062 }
14063
14064 if (nr > 0)
14065 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
14066 wp != twin; wp = wp->w_next)
14067 {
14068 if (wp == NULL)
14069 {
14070 /* didn't find it in this tabpage */
14071 nr = 0;
14072 break;
14073 }
14074 ++nr;
14075 }
14076 return nr;
14077}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014078
14079/*
14080 * "tabpagewinnr()" function
14081 */
14082 static void
14083f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
14084{
14085 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014086 tabpage_T *tp;
14087
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014088 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014089 if (tp == NULL)
14090 nr = 0;
14091 else
14092 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014093 rettv->vval.v_number = nr;
14094}
14095
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014096/*
14097 * "tagfiles()" function
14098 */
14099 static void
14100f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
14101{
14102 char_u *fname;
14103 tagname_T tn;
14104 int first;
14105
14106 if (rettv_list_alloc(rettv) == FAIL)
14107 return;
14108 fname = alloc(MAXPATHL);
14109 if (fname == NULL)
14110 return;
14111
14112 for (first = TRUE; ; first = FALSE)
14113 if (get_tagfname(&tn, first, fname) == FAIL
14114 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
14115 break;
14116 tagname_free(&tn);
14117 vim_free(fname);
14118}
14119
14120/*
14121 * "taglist()" function
14122 */
14123 static void
14124f_taglist(typval_T *argvars, typval_T *rettv)
14125{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014126 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014127 char_u *tag_pattern;
14128
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014129 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014130
14131 rettv->vval.v_number = FALSE;
14132 if (*tag_pattern == NUL)
14133 return;
14134
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014135 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014136 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014137 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014138 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014139}
14140
14141/*
14142 * "tempname()" function
14143 */
14144 static void
14145f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
14146{
14147 static int x = 'A';
14148
14149 rettv->v_type = VAR_STRING;
14150 rettv->vval.v_string = vim_tempname(x, FALSE);
14151
14152 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
14153 * names. Skip 'I' and 'O', they are used for shell redirection. */
14154 do
14155 {
14156 if (x == 'Z')
14157 x = '0';
14158 else if (x == '9')
14159 x = 'A';
14160 else
14161 {
14162#ifdef EBCDIC
14163 if (x == 'I')
14164 x = 'J';
14165 else if (x == 'R')
14166 x = 'S';
14167 else
14168#endif
14169 ++x;
14170 }
14171 } while (x == 'I' || x == 'O');
14172}
14173
14174#ifdef FEAT_FLOAT
14175/*
14176 * "tan()" function
14177 */
14178 static void
14179f_tan(typval_T *argvars, typval_T *rettv)
14180{
14181 float_T f = 0.0;
14182
14183 rettv->v_type = VAR_FLOAT;
14184 if (get_float_arg(argvars, &f) == OK)
14185 rettv->vval.v_float = tan(f);
14186 else
14187 rettv->vval.v_float = 0.0;
14188}
14189
14190/*
14191 * "tanh()" function
14192 */
14193 static void
14194f_tanh(typval_T *argvars, typval_T *rettv)
14195{
14196 float_T f = 0.0;
14197
14198 rettv->v_type = VAR_FLOAT;
14199 if (get_float_arg(argvars, &f) == OK)
14200 rettv->vval.v_float = tanh(f);
14201 else
14202 rettv->vval.v_float = 0.0;
14203}
14204#endif
14205
14206/*
14207 * "test_alloc_fail(id, countdown, repeat)" function
14208 */
14209 static void
14210f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
14211{
14212 if (argvars[0].v_type != VAR_NUMBER
14213 || argvars[0].vval.v_number <= 0
14214 || argvars[1].v_type != VAR_NUMBER
14215 || argvars[1].vval.v_number < 0
14216 || argvars[2].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014217 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014218 else
14219 {
14220 alloc_fail_id = argvars[0].vval.v_number;
14221 if (alloc_fail_id >= aid_last)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014222 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014223 alloc_fail_countdown = argvars[1].vval.v_number;
14224 alloc_fail_repeat = argvars[2].vval.v_number;
14225 did_outofmem_msg = FALSE;
14226 }
14227}
14228
14229/*
14230 * "test_autochdir()"
14231 */
14232 static void
14233f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14234{
14235#if defined(FEAT_AUTOCHDIR)
14236 test_autochdir = TRUE;
14237#endif
14238}
14239
14240/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020014241 * "test_feedinput()"
14242 */
14243 static void
14244f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
14245{
14246#ifdef USE_INPUT_BUF
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014247 char_u *val = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5e80de32017-09-03 15:48:12 +020014248
14249 if (val != NULL)
14250 {
14251 trash_input_buf();
14252 add_to_input_buf_csi(val, (int)STRLEN(val));
14253 }
14254#endif
14255}
14256
14257/*
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014258 * "test_option_not_set({name})" function
14259 */
14260 static void
14261f_test_option_not_set(typval_T *argvars, typval_T *rettv UNUSED)
14262{
14263 char_u *name = (char_u *)"";
14264
14265 if (argvars[0].v_type != VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014266 emsg(_(e_invarg));
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014267 else
14268 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014269 name = tv_get_string(&argvars[0]);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014270 if (reset_option_was_set(name) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014271 semsg(_(e_invarg2), name);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014272 }
14273}
14274
14275/*
14276 * "test_override({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014277 */
14278 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014279f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014280{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014281 char_u *name = (char_u *)"";
14282 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014283 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014284
14285 if (argvars[0].v_type != VAR_STRING
14286 || (argvars[1].v_type) != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014287 emsg(_(e_invarg));
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014288 else
14289 {
Bram Moolenaare38197d2018-12-24 23:35:13 +010014290 name = tv_get_string(&argvars[0]);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014291 val = (int)tv_get_number(&argvars[1]);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014292
14293 if (STRCMP(name, (char_u *)"redraw") == 0)
14294 disable_redraw_for_testing = val;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020014295 else if (STRCMP(name, (char_u *)"redraw_flag") == 0)
14296 ignore_redraw_flag_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014297 else if (STRCMP(name, (char_u *)"char_avail") == 0)
14298 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014299 else if (STRCMP(name, (char_u *)"starting") == 0)
14300 {
14301 if (val)
14302 {
14303 if (save_starting < 0)
14304 save_starting = starting;
14305 starting = 0;
14306 }
14307 else
14308 {
14309 starting = save_starting;
14310 save_starting = -1;
14311 }
14312 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +020014313 else if (STRCMP(name, (char_u *)"nfa_fail") == 0)
14314 nfa_fail_for_testing = val;
Bram Moolenaar92fd5992019-05-02 23:00:22 +020014315 else if (STRCMP(name, (char_u *)"no_query_mouse") == 0)
14316 no_query_mouse_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014317 else if (STRCMP(name, (char_u *)"ALL") == 0)
14318 {
14319 disable_char_avail_for_testing = FALSE;
14320 disable_redraw_for_testing = FALSE;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020014321 ignore_redraw_flag_for_testing = FALSE;
Bram Moolenaarbcf94422018-06-23 14:21:42 +020014322 nfa_fail_for_testing = FALSE;
Bram Moolenaar92fd5992019-05-02 23:00:22 +020014323 no_query_mouse_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014324 if (save_starting >= 0)
14325 {
14326 starting = save_starting;
14327 save_starting = -1;
14328 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014329 }
14330 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014331 semsg(_(e_invarg2), name);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014332 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014333}
14334
14335/*
Bram Moolenaarc3e92c12019-03-23 14:23:07 +010014336 * "test_refcount({expr})" function
14337 */
14338 static void
14339f_test_refcount(typval_T *argvars, typval_T *rettv)
14340{
14341 int retval = -1;
14342
14343 switch (argvars[0].v_type)
14344 {
14345 case VAR_UNKNOWN:
14346 case VAR_NUMBER:
14347 case VAR_FLOAT:
14348 case VAR_SPECIAL:
14349 case VAR_STRING:
14350 break;
14351 case VAR_JOB:
14352#ifdef FEAT_JOB_CHANNEL
14353 if (argvars[0].vval.v_job != NULL)
14354 retval = argvars[0].vval.v_job->jv_refcount - 1;
14355#endif
14356 break;
14357 case VAR_CHANNEL:
14358#ifdef FEAT_JOB_CHANNEL
14359 if (argvars[0].vval.v_channel != NULL)
14360 retval = argvars[0].vval.v_channel->ch_refcount - 1;
14361#endif
14362 break;
14363 case VAR_FUNC:
14364 if (argvars[0].vval.v_string != NULL)
14365 {
14366 ufunc_T *fp;
14367
14368 fp = find_func(argvars[0].vval.v_string);
14369 if (fp != NULL)
14370 retval = fp->uf_refcount;
14371 }
14372 break;
14373 case VAR_PARTIAL:
14374 if (argvars[0].vval.v_partial != NULL)
14375 retval = argvars[0].vval.v_partial->pt_refcount - 1;
14376 break;
14377 case VAR_BLOB:
14378 if (argvars[0].vval.v_blob != NULL)
14379 retval = argvars[0].vval.v_blob->bv_refcount - 1;
14380 break;
14381 case VAR_LIST:
14382 if (argvars[0].vval.v_list != NULL)
14383 retval = argvars[0].vval.v_list->lv_refcount - 1;
14384 break;
14385 case VAR_DICT:
14386 if (argvars[0].vval.v_dict != NULL)
14387 retval = argvars[0].vval.v_dict->dv_refcount - 1;
14388 break;
14389 }
14390
14391 rettv->v_type = VAR_NUMBER;
14392 rettv->vval.v_number = retval;
14393
14394}
14395
14396/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014397 * "test_garbagecollect_now()" function
14398 */
14399 static void
14400f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14401{
14402 /* This is dangerous, any Lists and Dicts used internally may be freed
14403 * while still in use. */
14404 garbage_collect(TRUE);
14405}
14406
Bram Moolenaare0c31f62017-03-01 15:07:05 +010014407/*
14408 * "test_ignore_error()" function
14409 */
14410 static void
14411f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
14412{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014413 ignore_error_for_testing(tv_get_string(&argvars[0]));
Bram Moolenaare0c31f62017-03-01 15:07:05 +010014414}
14415
Bram Moolenaarc0f5a782019-01-13 15:16:13 +010014416 static void
14417f_test_null_blob(typval_T *argvars UNUSED, typval_T *rettv)
14418{
14419 rettv->v_type = VAR_BLOB;
14420 rettv->vval.v_blob = NULL;
14421}
14422
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014423#ifdef FEAT_JOB_CHANNEL
14424 static void
14425f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
14426{
14427 rettv->v_type = VAR_CHANNEL;
14428 rettv->vval.v_channel = NULL;
14429}
14430#endif
14431
14432 static void
14433f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
14434{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014435 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014436}
14437
14438#ifdef FEAT_JOB_CHANNEL
14439 static void
14440f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
14441{
14442 rettv->v_type = VAR_JOB;
14443 rettv->vval.v_job = NULL;
14444}
14445#endif
14446
14447 static void
14448f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
14449{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014450 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014451}
14452
14453 static void
14454f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
14455{
14456 rettv->v_type = VAR_PARTIAL;
14457 rettv->vval.v_partial = NULL;
14458}
14459
14460 static void
14461f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
14462{
14463 rettv->v_type = VAR_STRING;
14464 rettv->vval.v_string = NULL;
14465}
14466
Bram Moolenaarab186732018-09-14 21:27:06 +020014467#ifdef FEAT_GUI
14468 static void
14469f_test_scrollbar(typval_T *argvars, typval_T *rettv UNUSED)
14470{
14471 char_u *which;
14472 long value;
14473 int dragging;
14474 scrollbar_T *sb = NULL;
14475
14476 if (argvars[0].v_type != VAR_STRING
14477 || (argvars[1].v_type) != VAR_NUMBER
14478 || (argvars[2].v_type) != VAR_NUMBER)
14479 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014480 emsg(_(e_invarg));
Bram Moolenaarab186732018-09-14 21:27:06 +020014481 return;
14482 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014483 which = tv_get_string(&argvars[0]);
14484 value = tv_get_number(&argvars[1]);
14485 dragging = tv_get_number(&argvars[2]);
Bram Moolenaarab186732018-09-14 21:27:06 +020014486
14487 if (STRCMP(which, "left") == 0)
14488 sb = &curwin->w_scrollbars[SBAR_LEFT];
14489 else if (STRCMP(which, "right") == 0)
14490 sb = &curwin->w_scrollbars[SBAR_RIGHT];
14491 else if (STRCMP(which, "hor") == 0)
14492 sb = &gui.bottom_sbar;
14493 if (sb == NULL)
14494 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014495 semsg(_(e_invarg2), which);
Bram Moolenaarab186732018-09-14 21:27:06 +020014496 return;
14497 }
14498 gui_drag_scrollbar(sb, value, dragging);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020014499# ifndef USE_ON_FLY_SCROLL
14500 // need to loop through normal_cmd() to handle the scroll events
14501 exec_normal(FALSE, TRUE, FALSE);
14502# endif
Bram Moolenaarab186732018-09-14 21:27:06 +020014503}
14504#endif
14505
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +020014506#ifdef FEAT_MOUSE
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014507 static void
Bram Moolenaarbb8476b2019-05-04 15:47:48 +020014508f_test_setmouse(typval_T *argvars, typval_T *rettv UNUSED)
14509{
14510 mouse_row = (time_t)tv_get_number(&argvars[0]) - 1;
14511 mouse_col = (time_t)tv_get_number(&argvars[1]) - 1;
14512}
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +020014513#endif
Bram Moolenaarbb8476b2019-05-04 15:47:48 +020014514
14515 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014516f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
14517{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014518 time_for_testing = (time_t)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014519}
14520
14521#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
14522/*
14523 * Get a callback from "arg". It can be a Funcref or a function name.
14524 * When "arg" is zero return an empty string.
14525 * Return NULL for an invalid argument.
14526 */
14527 char_u *
14528get_callback(typval_T *arg, partial_T **pp)
14529{
14530 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
14531 {
14532 *pp = arg->vval.v_partial;
14533 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020014534 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014535 }
14536 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020014537 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014538 {
14539 func_ref(arg->vval.v_string);
14540 return arg->vval.v_string;
14541 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014542 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
14543 return (char_u *)"";
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014544 emsg(_("E921: Invalid callback argument"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014545 return NULL;
14546}
14547
14548/*
Bram Moolenaard5359b22018-04-05 22:44:39 +020014549 * Unref/free "callback" and "partial" returned by get_callback().
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014550 */
14551 void
14552free_callback(char_u *callback, partial_T *partial)
14553{
14554 if (partial != NULL)
14555 partial_unref(partial);
14556 else if (callback != NULL)
14557 {
14558 func_unref(callback);
14559 vim_free(callback);
14560 }
14561}
14562#endif
14563
14564#ifdef FEAT_TIMERS
14565/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014566 * "timer_info([timer])" function
14567 */
14568 static void
14569f_timer_info(typval_T *argvars, typval_T *rettv)
14570{
14571 timer_T *timer = NULL;
14572
14573 if (rettv_list_alloc(rettv) != OK)
14574 return;
14575 if (argvars[0].v_type != VAR_UNKNOWN)
14576 {
14577 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014578 emsg(_(e_number_exp));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014579 else
14580 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014581 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014582 if (timer != NULL)
14583 add_timer_info(rettv, timer);
14584 }
14585 }
14586 else
14587 add_timer_info_all(rettv);
14588}
14589
14590/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014591 * "timer_pause(timer, paused)" function
14592 */
14593 static void
14594f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
14595{
14596 timer_T *timer = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014597 int paused = (int)tv_get_number(&argvars[1]);
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014598
14599 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014600 emsg(_(e_number_exp));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014601 else
14602 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014603 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014604 if (timer != NULL)
14605 timer->tr_paused = paused;
14606 }
14607}
14608
14609/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014610 * "timer_start(time, callback [, options])" function
14611 */
14612 static void
14613f_timer_start(typval_T *argvars, typval_T *rettv)
14614{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014615 long msec = (long)tv_get_number(&argvars[0]);
Bram Moolenaar75537a92016-09-05 22:45:28 +020014616 timer_T *timer;
14617 int repeat = 0;
14618 char_u *callback;
14619 dict_T *dict;
14620 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014621
Bram Moolenaar75537a92016-09-05 22:45:28 +020014622 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014623 if (check_secure())
14624 return;
14625 if (argvars[2].v_type != VAR_UNKNOWN)
14626 {
14627 if (argvars[2].v_type != VAR_DICT
14628 || (dict = argvars[2].vval.v_dict) == NULL)
14629 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014630 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014631 return;
14632 }
14633 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014634 repeat = dict_get_number(dict, (char_u *)"repeat");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014635 }
14636
Bram Moolenaar75537a92016-09-05 22:45:28 +020014637 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014638 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020014639 return;
14640
14641 timer = create_timer(msec, repeat);
14642 if (timer == NULL)
14643 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014644 else
14645 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020014646 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020014647 timer->tr_callback = vim_strsave(callback);
14648 else
14649 /* pointer into the partial */
14650 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020014651 timer->tr_partial = partial;
14652 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014653 }
14654}
14655
14656/*
14657 * "timer_stop(timer)" function
14658 */
14659 static void
14660f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
14661{
14662 timer_T *timer;
14663
14664 if (argvars[0].v_type != VAR_NUMBER)
14665 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014666 emsg(_(e_number_exp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014667 return;
14668 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014669 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014670 if (timer != NULL)
14671 stop_timer(timer);
14672}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014673
14674/*
14675 * "timer_stopall()" function
14676 */
14677 static void
14678f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14679{
14680 stop_all_timers();
14681}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014682#endif
14683
14684/*
14685 * "tolower(string)" function
14686 */
14687 static void
14688f_tolower(typval_T *argvars, typval_T *rettv)
14689{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014690 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014691 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014692}
14693
14694/*
14695 * "toupper(string)" function
14696 */
14697 static void
14698f_toupper(typval_T *argvars, typval_T *rettv)
14699{
14700 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014701 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014702}
14703
14704/*
14705 * "tr(string, fromstr, tostr)" function
14706 */
14707 static void
14708f_tr(typval_T *argvars, typval_T *rettv)
14709{
14710 char_u *in_str;
14711 char_u *fromstr;
14712 char_u *tostr;
14713 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014714 int inlen;
14715 int fromlen;
14716 int tolen;
14717 int idx;
14718 char_u *cpstr;
14719 int cplen;
14720 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014721 char_u buf[NUMBUFLEN];
14722 char_u buf2[NUMBUFLEN];
14723 garray_T ga;
14724
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014725 in_str = tv_get_string(&argvars[0]);
14726 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
14727 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014728
14729 /* Default return value: empty string. */
14730 rettv->v_type = VAR_STRING;
14731 rettv->vval.v_string = NULL;
14732 if (fromstr == NULL || tostr == NULL)
14733 return; /* type error; errmsg already given */
14734 ga_init2(&ga, (int)sizeof(char), 80);
14735
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014736 if (!has_mbyte)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014737 /* not multi-byte: fromstr and tostr must be the same length */
14738 if (STRLEN(fromstr) != STRLEN(tostr))
14739 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014740error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014741 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014742 ga_clear(&ga);
14743 return;
14744 }
14745
14746 /* fromstr and tostr have to contain the same number of chars */
14747 while (*in_str != NUL)
14748 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014749 if (has_mbyte)
14750 {
14751 inlen = (*mb_ptr2len)(in_str);
14752 cpstr = in_str;
14753 cplen = inlen;
14754 idx = 0;
14755 for (p = fromstr; *p != NUL; p += fromlen)
14756 {
14757 fromlen = (*mb_ptr2len)(p);
14758 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
14759 {
14760 for (p = tostr; *p != NUL; p += tolen)
14761 {
14762 tolen = (*mb_ptr2len)(p);
14763 if (idx-- == 0)
14764 {
14765 cplen = tolen;
14766 cpstr = p;
14767 break;
14768 }
14769 }
14770 if (*p == NUL) /* tostr is shorter than fromstr */
14771 goto error;
14772 break;
14773 }
14774 ++idx;
14775 }
14776
14777 if (first && cpstr == in_str)
14778 {
14779 /* Check that fromstr and tostr have the same number of
14780 * (multi-byte) characters. Done only once when a character
14781 * of in_str doesn't appear in fromstr. */
14782 first = FALSE;
14783 for (p = tostr; *p != NUL; p += tolen)
14784 {
14785 tolen = (*mb_ptr2len)(p);
14786 --idx;
14787 }
14788 if (idx != 0)
14789 goto error;
14790 }
14791
14792 (void)ga_grow(&ga, cplen);
14793 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
14794 ga.ga_len += cplen;
14795
14796 in_str += inlen;
14797 }
14798 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014799 {
14800 /* When not using multi-byte chars we can do it faster. */
14801 p = vim_strchr(fromstr, *in_str);
14802 if (p != NULL)
14803 ga_append(&ga, tostr[p - fromstr]);
14804 else
14805 ga_append(&ga, *in_str);
14806 ++in_str;
14807 }
14808 }
14809
14810 /* add a terminating NUL */
14811 (void)ga_grow(&ga, 1);
14812 ga_append(&ga, NUL);
14813
14814 rettv->vval.v_string = ga.ga_data;
14815}
14816
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014817/*
14818 * "trim({expr})" function
14819 */
14820 static void
14821f_trim(typval_T *argvars, typval_T *rettv)
14822{
14823 char_u buf1[NUMBUFLEN];
14824 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014825 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014826 char_u *mask = NULL;
14827 char_u *tail;
14828 char_u *prev;
14829 char_u *p;
14830 int c1;
14831
14832 rettv->v_type = VAR_STRING;
14833 if (head == NULL)
14834 {
14835 rettv->vval.v_string = NULL;
14836 return;
14837 }
14838
14839 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014840 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014841
14842 while (*head != NUL)
14843 {
14844 c1 = PTR2CHAR(head);
14845 if (mask == NULL)
14846 {
14847 if (c1 > ' ' && c1 != 0xa0)
14848 break;
14849 }
14850 else
14851 {
14852 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14853 if (c1 == PTR2CHAR(p))
14854 break;
14855 if (*p == NUL)
14856 break;
14857 }
14858 MB_PTR_ADV(head);
14859 }
14860
14861 for (tail = head + STRLEN(head); tail > head; tail = prev)
14862 {
14863 prev = tail;
14864 MB_PTR_BACK(head, prev);
14865 c1 = PTR2CHAR(prev);
14866 if (mask == NULL)
14867 {
14868 if (c1 > ' ' && c1 != 0xa0)
14869 break;
14870 }
14871 else
14872 {
14873 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14874 if (c1 == PTR2CHAR(p))
14875 break;
14876 if (*p == NUL)
14877 break;
14878 }
14879 }
14880 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
14881}
14882
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014883#ifdef FEAT_FLOAT
14884/*
14885 * "trunc({float})" function
14886 */
14887 static void
14888f_trunc(typval_T *argvars, typval_T *rettv)
14889{
14890 float_T f = 0.0;
14891
14892 rettv->v_type = VAR_FLOAT;
14893 if (get_float_arg(argvars, &f) == OK)
14894 /* trunc() is not in C90, use floor() or ceil() instead. */
14895 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
14896 else
14897 rettv->vval.v_float = 0.0;
14898}
14899#endif
14900
14901/*
14902 * "type(expr)" function
14903 */
14904 static void
14905f_type(typval_T *argvars, typval_T *rettv)
14906{
14907 int n = -1;
14908
14909 switch (argvars[0].v_type)
14910 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020014911 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
14912 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014913 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020014914 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
14915 case VAR_LIST: n = VAR_TYPE_LIST; break;
14916 case VAR_DICT: n = VAR_TYPE_DICT; break;
14917 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014918 case VAR_SPECIAL:
14919 if (argvars[0].vval.v_number == VVAL_FALSE
14920 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020014921 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014922 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020014923 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014924 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020014925 case VAR_JOB: n = VAR_TYPE_JOB; break;
14926 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014927 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014928 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010014929 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014930 n = -1;
14931 break;
14932 }
14933 rettv->vval.v_number = n;
14934}
14935
14936/*
14937 * "undofile(name)" function
14938 */
14939 static void
14940f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
14941{
14942 rettv->v_type = VAR_STRING;
14943#ifdef FEAT_PERSISTENT_UNDO
14944 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014945 char_u *fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014946
14947 if (*fname == NUL)
14948 {
14949 /* If there is no file name there will be no undo file. */
14950 rettv->vval.v_string = NULL;
14951 }
14952 else
14953 {
14954 char_u *ffname = FullName_save(fname, FALSE);
14955
14956 if (ffname != NULL)
14957 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
14958 vim_free(ffname);
14959 }
14960 }
14961#else
14962 rettv->vval.v_string = NULL;
14963#endif
14964}
14965
14966/*
14967 * "undotree()" function
14968 */
14969 static void
14970f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
14971{
14972 if (rettv_dict_alloc(rettv) == OK)
14973 {
14974 dict_T *dict = rettv->vval.v_dict;
14975 list_T *list;
14976
Bram Moolenaare0be1672018-07-08 16:50:37 +020014977 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
14978 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
14979 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
14980 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
14981 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
14982 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014983
14984 list = list_alloc();
14985 if (list != NULL)
14986 {
14987 u_eval_tree(curbuf->b_u_oldhead, list);
14988 dict_add_list(dict, "entries", list);
14989 }
14990 }
14991}
14992
14993/*
14994 * "values(dict)" function
14995 */
14996 static void
14997f_values(typval_T *argvars, typval_T *rettv)
14998{
14999 dict_list(argvars, rettv, 1);
15000}
15001
15002/*
15003 * "virtcol(string)" function
15004 */
15005 static void
15006f_virtcol(typval_T *argvars, typval_T *rettv)
15007{
15008 colnr_T vcol = 0;
15009 pos_T *fp;
15010 int fnum = curbuf->b_fnum;
15011
15012 fp = var2fpos(&argvars[0], FALSE, &fnum);
15013 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
15014 && fnum == curbuf->b_fnum)
15015 {
15016 getvvcol(curwin, fp, NULL, NULL, &vcol);
15017 ++vcol;
15018 }
15019
15020 rettv->vval.v_number = vcol;
15021}
15022
15023/*
15024 * "visualmode()" function
15025 */
15026 static void
15027f_visualmode(typval_T *argvars, typval_T *rettv)
15028{
15029 char_u str[2];
15030
15031 rettv->v_type = VAR_STRING;
15032 str[0] = curbuf->b_visual_mode_eval;
15033 str[1] = NUL;
15034 rettv->vval.v_string = vim_strsave(str);
15035
15036 /* A non-zero number or non-empty string argument: reset mode. */
15037 if (non_zero_arg(&argvars[0]))
15038 curbuf->b_visual_mode_eval = NUL;
15039}
15040
15041/*
15042 * "wildmenumode()" function
15043 */
15044 static void
15045f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
15046{
15047#ifdef FEAT_WILDMENU
15048 if (wild_menu_showing)
15049 rettv->vval.v_number = 1;
15050#endif
15051}
15052
15053/*
15054 * "winbufnr(nr)" function
15055 */
15056 static void
15057f_winbufnr(typval_T *argvars, typval_T *rettv)
15058{
15059 win_T *wp;
15060
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015061 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015062 if (wp == NULL)
15063 rettv->vval.v_number = -1;
15064 else
15065 rettv->vval.v_number = wp->w_buffer->b_fnum;
15066}
15067
15068/*
15069 * "wincol()" function
15070 */
15071 static void
15072f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
15073{
15074 validate_cursor();
15075 rettv->vval.v_number = curwin->w_wcol + 1;
15076}
15077
15078/*
15079 * "winheight(nr)" function
15080 */
15081 static void
15082f_winheight(typval_T *argvars, typval_T *rettv)
15083{
15084 win_T *wp;
15085
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015086 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015087 if (wp == NULL)
15088 rettv->vval.v_number = -1;
15089 else
15090 rettv->vval.v_number = wp->w_height;
15091}
15092
15093/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020015094 * "winlayout()" function
15095 */
15096 static void
15097f_winlayout(typval_T *argvars, typval_T *rettv)
15098{
15099 tabpage_T *tp;
15100
15101 if (rettv_list_alloc(rettv) != OK)
15102 return;
15103
15104 if (argvars[0].v_type == VAR_UNKNOWN)
15105 tp = curtab;
15106 else
15107 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015108 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020015109 if (tp == NULL)
15110 return;
15111 }
15112
15113 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
15114}
15115
15116/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015117 * "winline()" function
15118 */
15119 static void
15120f_winline(typval_T *argvars UNUSED, typval_T *rettv)
15121{
15122 validate_cursor();
15123 rettv->vval.v_number = curwin->w_wrow + 1;
15124}
15125
15126/*
15127 * "winnr()" function
15128 */
15129 static void
15130f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
15131{
15132 int nr = 1;
15133
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015134 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015135 rettv->vval.v_number = nr;
15136}
15137
15138/*
15139 * "winrestcmd()" function
15140 */
15141 static void
15142f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
15143{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015144 win_T *wp;
15145 int winnr = 1;
15146 garray_T ga;
15147 char_u buf[50];
15148
15149 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020015150 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015151 {
15152 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
15153 ga_concat(&ga, buf);
15154 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
15155 ga_concat(&ga, buf);
15156 ++winnr;
15157 }
15158 ga_append(&ga, NUL);
15159
15160 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015161 rettv->v_type = VAR_STRING;
15162}
15163
15164/*
15165 * "winrestview()" function
15166 */
15167 static void
15168f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
15169{
15170 dict_T *dict;
15171
15172 if (argvars[0].v_type != VAR_DICT
15173 || (dict = argvars[0].vval.v_dict) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015174 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015175 else
15176 {
15177 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015178 curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015179 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015180 curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015181 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015182 curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015183 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
15184 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010015185 curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015186 curwin->w_set_curswant = FALSE;
15187 }
15188
15189 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015190 set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015191#ifdef FEAT_DIFF
15192 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015193 curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015194#endif
15195 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015196 curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015197 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015198 curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015199
15200 check_cursor();
15201 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020015202 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015203 changed_window_setting();
15204
15205 if (curwin->w_topline <= 0)
15206 curwin->w_topline = 1;
15207 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
15208 curwin->w_topline = curbuf->b_ml.ml_line_count;
15209#ifdef FEAT_DIFF
15210 check_topfill(curwin, TRUE);
15211#endif
15212 }
15213}
15214
15215/*
15216 * "winsaveview()" function
15217 */
15218 static void
15219f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
15220{
15221 dict_T *dict;
15222
15223 if (rettv_dict_alloc(rettv) == FAIL)
15224 return;
15225 dict = rettv->vval.v_dict;
15226
Bram Moolenaare0be1672018-07-08 16:50:37 +020015227 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
15228 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +020015229 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015230 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020015231 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015232
Bram Moolenaare0be1672018-07-08 16:50:37 +020015233 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015234#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020015235 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015236#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020015237 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
15238 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015239}
15240
15241/*
15242 * "winwidth(nr)" function
15243 */
15244 static void
15245f_winwidth(typval_T *argvars, typval_T *rettv)
15246{
15247 win_T *wp;
15248
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015249 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015250 if (wp == NULL)
15251 rettv->vval.v_number = -1;
15252 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015253 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015254}
15255
15256/*
15257 * "wordcount()" function
15258 */
15259 static void
15260f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
15261{
15262 if (rettv_dict_alloc(rettv) == FAIL)
15263 return;
15264 cursor_pos_info(rettv->vval.v_dict);
15265}
15266
15267/*
15268 * "writefile()" function
15269 */
15270 static void
15271f_writefile(typval_T *argvars, typval_T *rettv)
15272{
15273 int binary = FALSE;
15274 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015275#ifdef HAVE_FSYNC
15276 int do_fsync = p_fs;
15277#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015278 char_u *fname;
15279 FILE *fd;
15280 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015281 listitem_T *li;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015282 list_T *list = NULL;
15283 blob_T *blob = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015284
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015285 rettv->vval.v_number = -1;
Bram Moolenaar8c62a082019-02-08 14:34:10 +010015286 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015287 return;
15288
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015289 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015290 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015291 list = argvars[0].vval.v_list;
15292 if (list == NULL)
15293 return;
15294 for (li = list->lv_first; li != NULL; li = li->li_next)
15295 if (tv_get_string_chk(&li->li_tv) == NULL)
15296 return;
15297 }
15298 else if (argvars[0].v_type == VAR_BLOB)
15299 {
15300 blob = argvars[0].vval.v_blob;
15301 if (blob == NULL)
15302 return;
15303 }
15304 else
15305 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015306 semsg(_(e_invarg2), "writefile()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015307 return;
15308 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015309
15310 if (argvars[2].v_type != VAR_UNKNOWN)
15311 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015312 char_u *arg2 = tv_get_string_chk(&argvars[2]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015313
15314 if (arg2 == NULL)
15315 return;
15316 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015317 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015318 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015319 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015320#ifdef HAVE_FSYNC
15321 if (vim_strchr(arg2, 's') != NULL)
15322 do_fsync = TRUE;
15323 else if (vim_strchr(arg2, 'S') != NULL)
15324 do_fsync = FALSE;
15325#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015326 }
15327
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015328 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015329 if (fname == NULL)
15330 return;
15331
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015332 /* Always open the file in binary mode, library functions have a mind of
15333 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015334 if (*fname == NUL || (fd = mch_fopen((char *)fname,
15335 append ? APPENDBIN : WRITEBIN)) == NULL)
15336 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015337 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015338 ret = -1;
15339 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015340 else if (blob)
15341 {
15342 if (write_blob(fd, blob) == FAIL)
15343 ret = -1;
15344#ifdef HAVE_FSYNC
15345 else if (do_fsync)
15346 // Ignore the error, the user wouldn't know what to do about it.
15347 // May happen for a device.
Bram Moolenaara7870192019-02-14 12:56:36 +010015348 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015349#endif
15350 fclose(fd);
15351 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015352 else
15353 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015354 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015355 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015356#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010015357 else if (do_fsync)
15358 /* Ignore the error, the user wouldn't know what to do about it.
15359 * May happen for a device. */
Bram Moolenaara7870192019-02-14 12:56:36 +010015360 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015361#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015362 fclose(fd);
15363 }
15364
15365 rettv->vval.v_number = ret;
15366}
15367
15368/*
15369 * "xor(expr, expr)" function
15370 */
15371 static void
15372f_xor(typval_T *argvars, typval_T *rettv)
15373{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015374 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
15375 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015376}
15377
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015378#endif /* FEAT_EVAL */