blob: 58b8492e569e9927300ccaca9af852217aca1955 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalfunc.c: Builtin functions
12 */
13#define USING_FLOAT_STUFF
14
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
23#ifdef VMS
24# include <float.h>
25#endif
26
Bram Moolenaard0573012017-10-28 21:11:06 +020027#ifdef MACOS_X
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020028# include <time.h> /* for time_t */
29#endif
30
31static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaarbf821bc2019-01-23 21:15:02 +010032static char *e_listblobarg = N_("E899: Argument of %s must be a List or Blob");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020033static char *e_stringreq = N_("E928: String required");
Bram Moolenaaraff74912019-03-30 18:11:49 +010034static char *e_invalwindow = N_("E957: Invalid window number");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020035
36#ifdef FEAT_FLOAT
37static void f_abs(typval_T *argvars, typval_T *rettv);
38static void f_acos(typval_T *argvars, typval_T *rettv);
39#endif
40static void f_add(typval_T *argvars, typval_T *rettv);
41static void f_and(typval_T *argvars, typval_T *rettv);
42static void f_append(typval_T *argvars, typval_T *rettv);
Bram Moolenaarca851592018-06-06 21:04:07 +020043static void f_appendbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020044static void f_argc(typval_T *argvars, typval_T *rettv);
45static void f_argidx(typval_T *argvars, typval_T *rettv);
46static void f_arglistid(typval_T *argvars, typval_T *rettv);
47static void f_argv(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +010048static void f_assert_beeps(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020049static void f_assert_equal(typval_T *argvars, typval_T *rettv);
Bram Moolenaard96ff162018-02-18 22:13:29 +010050static void f_assert_equalfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020051static void f_assert_exception(typval_T *argvars, typval_T *rettv);
52static void f_assert_fails(typval_T *argvars, typval_T *rettv);
53static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaar61c04492016-07-23 15:35:35 +020054static void f_assert_inrange(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020055static void f_assert_match(typval_T *argvars, typval_T *rettv);
56static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
57static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar42205552017-03-18 19:42:22 +010058static void f_assert_report(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020059static void f_assert_true(typval_T *argvars, typval_T *rettv);
60#ifdef FEAT_FLOAT
61static void f_asin(typval_T *argvars, typval_T *rettv);
62static void f_atan(typval_T *argvars, typval_T *rettv);
63static void f_atan2(typval_T *argvars, typval_T *rettv);
64#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010065#ifdef FEAT_BEVAL
66static void f_balloon_show(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010067# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010068static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010069# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010070#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020071static void f_browse(typval_T *argvars, typval_T *rettv);
72static void f_browsedir(typval_T *argvars, typval_T *rettv);
73static void f_bufexists(typval_T *argvars, typval_T *rettv);
74static void f_buflisted(typval_T *argvars, typval_T *rettv);
75static void f_bufloaded(typval_T *argvars, typval_T *rettv);
76static void f_bufname(typval_T *argvars, typval_T *rettv);
77static void f_bufnr(typval_T *argvars, typval_T *rettv);
78static void f_bufwinid(typval_T *argvars, typval_T *rettv);
79static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
80static void f_byte2line(typval_T *argvars, typval_T *rettv);
81static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
82static void f_byteidx(typval_T *argvars, typval_T *rettv);
83static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
84static void f_call(typval_T *argvars, typval_T *rettv);
85#ifdef FEAT_FLOAT
86static void f_ceil(typval_T *argvars, typval_T *rettv);
87#endif
88#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +010089static void f_ch_canread(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020090static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0874a832016-09-01 15:11:51 +020091static void f_ch_close_in(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020092static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
93static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
94static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
95static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
96static void f_ch_info(typval_T *argvars, typval_T *rettv);
97static void f_ch_log(typval_T *argvars, typval_T *rettv);
98static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
99static void f_ch_open(typval_T *argvars, typval_T *rettv);
100static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100101static void f_ch_readblob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200102static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
103static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
104static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
105static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
106static void f_ch_status(typval_T *argvars, typval_T *rettv);
107#endif
108static void f_changenr(typval_T *argvars, typval_T *rettv);
109static void f_char2nr(typval_T *argvars, typval_T *rettv);
110static void f_cindent(typval_T *argvars, typval_T *rettv);
111static void f_clearmatches(typval_T *argvars, typval_T *rettv);
112static void f_col(typval_T *argvars, typval_T *rettv);
113#if defined(FEAT_INS_EXPAND)
114static void f_complete(typval_T *argvars, typval_T *rettv);
115static void f_complete_add(typval_T *argvars, typval_T *rettv);
116static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfd133322019-03-29 12:20:27 +0100117static void f_complete_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200118#endif
119static void f_confirm(typval_T *argvars, typval_T *rettv);
120static void f_copy(typval_T *argvars, typval_T *rettv);
121#ifdef FEAT_FLOAT
122static void f_cos(typval_T *argvars, typval_T *rettv);
123static void f_cosh(typval_T *argvars, typval_T *rettv);
124#endif
125static void f_count(typval_T *argvars, typval_T *rettv);
126static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
127static void f_cursor(typval_T *argsvars, typval_T *rettv);
Bram Moolenaar4f974752019-02-17 17:44:42 +0100128#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200129static void f_debugbreak(typval_T *argvars, typval_T *rettv);
130#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200131static void f_deepcopy(typval_T *argvars, typval_T *rettv);
132static void f_delete(typval_T *argvars, typval_T *rettv);
Bram Moolenaard79a2622018-06-07 18:17:46 +0200133static void f_deletebufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200134static void f_did_filetype(typval_T *argvars, typval_T *rettv);
135static void f_diff_filler(typval_T *argvars, typval_T *rettv);
136static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
137static void f_empty(typval_T *argvars, typval_T *rettv);
138static void f_escape(typval_T *argvars, typval_T *rettv);
139static void f_eval(typval_T *argvars, typval_T *rettv);
140static void f_eventhandler(typval_T *argvars, typval_T *rettv);
141static void f_executable(typval_T *argvars, typval_T *rettv);
142static void f_execute(typval_T *argvars, typval_T *rettv);
143static void f_exepath(typval_T *argvars, typval_T *rettv);
144static void f_exists(typval_T *argvars, typval_T *rettv);
145#ifdef FEAT_FLOAT
146static void f_exp(typval_T *argvars, typval_T *rettv);
147#endif
148static void f_expand(typval_T *argvars, typval_T *rettv);
149static void f_extend(typval_T *argvars, typval_T *rettv);
150static void f_feedkeys(typval_T *argvars, typval_T *rettv);
151static void f_filereadable(typval_T *argvars, typval_T *rettv);
152static void f_filewritable(typval_T *argvars, typval_T *rettv);
153static void f_filter(typval_T *argvars, typval_T *rettv);
154static void f_finddir(typval_T *argvars, typval_T *rettv);
155static void f_findfile(typval_T *argvars, typval_T *rettv);
156#ifdef FEAT_FLOAT
157static void f_float2nr(typval_T *argvars, typval_T *rettv);
158static void f_floor(typval_T *argvars, typval_T *rettv);
159static void f_fmod(typval_T *argvars, typval_T *rettv);
160#endif
161static void f_fnameescape(typval_T *argvars, typval_T *rettv);
162static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
163static void f_foldclosed(typval_T *argvars, typval_T *rettv);
164static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
165static void f_foldlevel(typval_T *argvars, typval_T *rettv);
166static void f_foldtext(typval_T *argvars, typval_T *rettv);
167static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
168static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200169static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200170static void f_function(typval_T *argvars, typval_T *rettv);
171static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
172static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200173static void f_getbufinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200174static void f_getbufline(typval_T *argvars, typval_T *rettv);
175static void f_getbufvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100176static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200177static void f_getchar(typval_T *argvars, typval_T *rettv);
178static void f_getcharmod(typval_T *argvars, typval_T *rettv);
179static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
180static void f_getcmdline(typval_T *argvars, typval_T *rettv);
181#if defined(FEAT_CMDL_COMPL)
182static void f_getcompletion(typval_T *argvars, typval_T *rettv);
183#endif
184static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
185static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
186static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
187static void f_getcwd(typval_T *argvars, typval_T *rettv);
188static void f_getfontname(typval_T *argvars, typval_T *rettv);
189static void f_getfperm(typval_T *argvars, typval_T *rettv);
190static void f_getfsize(typval_T *argvars, typval_T *rettv);
191static void f_getftime(typval_T *argvars, typval_T *rettv);
192static void f_getftype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +0100193static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200194static void f_getline(typval_T *argvars, typval_T *rettv);
Bram Moolenaard823fa92016-08-12 16:29:27 +0200195static void f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200196static void f_getmatches(typval_T *argvars, typval_T *rettv);
197static void f_getpid(typval_T *argvars, typval_T *rettv);
198static void f_getcurpos(typval_T *argvars, typval_T *rettv);
199static void f_getpos(typval_T *argvars, typval_T *rettv);
200static void f_getqflist(typval_T *argvars, typval_T *rettv);
201static void f_getreg(typval_T *argvars, typval_T *rettv);
202static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200203static void f_gettabinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200204static void f_gettabvar(typval_T *argvars, typval_T *rettv);
205static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100206static void f_gettagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200207static void f_getwininfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100208static void f_getwinpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200209static void f_getwinposx(typval_T *argvars, typval_T *rettv);
210static void f_getwinposy(typval_T *argvars, typval_T *rettv);
211static void f_getwinvar(typval_T *argvars, typval_T *rettv);
212static void f_glob(typval_T *argvars, typval_T *rettv);
213static void f_globpath(typval_T *argvars, typval_T *rettv);
214static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
215static void f_has(typval_T *argvars, typval_T *rettv);
216static void f_has_key(typval_T *argvars, typval_T *rettv);
217static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
218static void f_hasmapto(typval_T *argvars, typval_T *rettv);
219static void f_histadd(typval_T *argvars, typval_T *rettv);
220static void f_histdel(typval_T *argvars, typval_T *rettv);
221static void f_histget(typval_T *argvars, typval_T *rettv);
222static void f_histnr(typval_T *argvars, typval_T *rettv);
223static void f_hlID(typval_T *argvars, typval_T *rettv);
224static void f_hlexists(typval_T *argvars, typval_T *rettv);
225static void f_hostname(typval_T *argvars, typval_T *rettv);
226static void f_iconv(typval_T *argvars, typval_T *rettv);
227static void f_indent(typval_T *argvars, typval_T *rettv);
228static void f_index(typval_T *argvars, typval_T *rettv);
229static void f_input(typval_T *argvars, typval_T *rettv);
230static void f_inputdialog(typval_T *argvars, typval_T *rettv);
231static void f_inputlist(typval_T *argvars, typval_T *rettv);
232static void f_inputrestore(typval_T *argvars, typval_T *rettv);
233static void f_inputsave(typval_T *argvars, typval_T *rettv);
234static void f_inputsecret(typval_T *argvars, typval_T *rettv);
235static void f_insert(typval_T *argvars, typval_T *rettv);
236static void f_invert(typval_T *argvars, typval_T *rettv);
237static void f_isdirectory(typval_T *argvars, typval_T *rettv);
238static void f_islocked(typval_T *argvars, typval_T *rettv);
239#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200240static void f_isinf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200241static void f_isnan(typval_T *argvars, typval_T *rettv);
242#endif
243static void f_items(typval_T *argvars, typval_T *rettv);
244#ifdef FEAT_JOB_CHANNEL
245static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
246static void f_job_info(typval_T *argvars, typval_T *rettv);
247static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
248static void f_job_start(typval_T *argvars, typval_T *rettv);
249static void f_job_stop(typval_T *argvars, typval_T *rettv);
250static void f_job_status(typval_T *argvars, typval_T *rettv);
251#endif
252static void f_join(typval_T *argvars, typval_T *rettv);
253static void f_js_decode(typval_T *argvars, typval_T *rettv);
254static void f_js_encode(typval_T *argvars, typval_T *rettv);
255static void f_json_decode(typval_T *argvars, typval_T *rettv);
256static void f_json_encode(typval_T *argvars, typval_T *rettv);
257static void f_keys(typval_T *argvars, typval_T *rettv);
258static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
259static void f_len(typval_T *argvars, typval_T *rettv);
260static void f_libcall(typval_T *argvars, typval_T *rettv);
261static void f_libcallnr(typval_T *argvars, typval_T *rettv);
262static void f_line(typval_T *argvars, typval_T *rettv);
263static void f_line2byte(typval_T *argvars, typval_T *rettv);
264static void f_lispindent(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9d401282019-04-06 13:18:12 +0200265static void f_list2str(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200266static void f_localtime(typval_T *argvars, typval_T *rettv);
267#ifdef FEAT_FLOAT
268static void f_log(typval_T *argvars, typval_T *rettv);
269static void f_log10(typval_T *argvars, typval_T *rettv);
270#endif
271#ifdef FEAT_LUA
272static void f_luaeval(typval_T *argvars, typval_T *rettv);
273#endif
274static void f_map(typval_T *argvars, typval_T *rettv);
275static void f_maparg(typval_T *argvars, typval_T *rettv);
276static void f_mapcheck(typval_T *argvars, typval_T *rettv);
277static void f_match(typval_T *argvars, typval_T *rettv);
278static void f_matchadd(typval_T *argvars, typval_T *rettv);
279static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
280static void f_matcharg(typval_T *argvars, typval_T *rettv);
281static void f_matchdelete(typval_T *argvars, typval_T *rettv);
282static void f_matchend(typval_T *argvars, typval_T *rettv);
283static void f_matchlist(typval_T *argvars, typval_T *rettv);
284static void f_matchstr(typval_T *argvars, typval_T *rettv);
285static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
286static void f_max(typval_T *argvars, typval_T *rettv);
287static void f_min(typval_T *argvars, typval_T *rettv);
288#ifdef vim_mkdir
289static void f_mkdir(typval_T *argvars, typval_T *rettv);
290#endif
291static void f_mode(typval_T *argvars, typval_T *rettv);
292#ifdef FEAT_MZSCHEME
293static void f_mzeval(typval_T *argvars, typval_T *rettv);
294#endif
295static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
296static void f_nr2char(typval_T *argvars, typval_T *rettv);
297static void f_or(typval_T *argvars, typval_T *rettv);
298static void f_pathshorten(typval_T *argvars, typval_T *rettv);
299#ifdef FEAT_PERL
300static void f_perleval(typval_T *argvars, typval_T *rettv);
301#endif
302#ifdef FEAT_FLOAT
303static void f_pow(typval_T *argvars, typval_T *rettv);
304#endif
305static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
306static void f_printf(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200307#ifdef FEAT_JOB_CHANNEL
308static void f_prompt_setcallback(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200309static void f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200310static void f_prompt_setprompt(typval_T *argvars, typval_T *rettv);
311#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200312static void f_pumvisible(typval_T *argvars, typval_T *rettv);
313#ifdef FEAT_PYTHON3
314static void f_py3eval(typval_T *argvars, typval_T *rettv);
315#endif
316#ifdef FEAT_PYTHON
317static void f_pyeval(typval_T *argvars, typval_T *rettv);
318#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100319#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
320static void f_pyxeval(typval_T *argvars, typval_T *rettv);
321#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200322static void f_range(typval_T *argvars, typval_T *rettv);
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200323static void f_readdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200324static void f_readfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200325static void f_reg_executing(typval_T *argvars, typval_T *rettv);
326static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200327static void f_reltime(typval_T *argvars, typval_T *rettv);
328#ifdef FEAT_FLOAT
329static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
330#endif
331static void f_reltimestr(typval_T *argvars, typval_T *rettv);
332static void f_remote_expr(typval_T *argvars, typval_T *rettv);
333static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
334static void f_remote_peek(typval_T *argvars, typval_T *rettv);
335static void f_remote_read(typval_T *argvars, typval_T *rettv);
336static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100337static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200338static void f_remove(typval_T *argvars, typval_T *rettv);
339static void f_rename(typval_T *argvars, typval_T *rettv);
340static void f_repeat(typval_T *argvars, typval_T *rettv);
341static void f_resolve(typval_T *argvars, typval_T *rettv);
342static void f_reverse(typval_T *argvars, typval_T *rettv);
343#ifdef FEAT_FLOAT
344static void f_round(typval_T *argvars, typval_T *rettv);
345#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100346#ifdef FEAT_RUBY
347static void f_rubyeval(typval_T *argvars, typval_T *rettv);
348#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200349static void f_screenattr(typval_T *argvars, typval_T *rettv);
350static void f_screenchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100351static void f_screenchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200352static void f_screencol(typval_T *argvars, typval_T *rettv);
353static void f_screenrow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100354static void f_screenstring(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200355static void f_search(typval_T *argvars, typval_T *rettv);
356static void f_searchdecl(typval_T *argvars, typval_T *rettv);
357static void f_searchpair(typval_T *argvars, typval_T *rettv);
358static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
359static void f_searchpos(typval_T *argvars, typval_T *rettv);
360static void f_server2client(typval_T *argvars, typval_T *rettv);
361static void f_serverlist(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200362static void f_setbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200363static void f_setbufvar(typval_T *argvars, typval_T *rettv);
364static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
365static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
366static void f_setfperm(typval_T *argvars, typval_T *rettv);
367static void f_setline(typval_T *argvars, typval_T *rettv);
368static void f_setloclist(typval_T *argvars, typval_T *rettv);
369static void f_setmatches(typval_T *argvars, typval_T *rettv);
370static void f_setpos(typval_T *argvars, typval_T *rettv);
371static void f_setqflist(typval_T *argvars, typval_T *rettv);
372static void f_setreg(typval_T *argvars, typval_T *rettv);
373static void f_settabvar(typval_T *argvars, typval_T *rettv);
374static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100375static void f_settagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200376static void f_setwinvar(typval_T *argvars, typval_T *rettv);
377#ifdef FEAT_CRYPT
378static void f_sha256(typval_T *argvars, typval_T *rettv);
379#endif /* FEAT_CRYPT */
380static void f_shellescape(typval_T *argvars, typval_T *rettv);
381static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
Bram Moolenaar162b7142018-12-21 15:17:36 +0100382#ifdef FEAT_SIGNS
383static void f_sign_define(typval_T *argvars, typval_T *rettv);
384static void f_sign_getdefined(typval_T *argvars, typval_T *rettv);
385static void f_sign_getplaced(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100386static void f_sign_jump(typval_T *argvars, typval_T *rettv);
Bram Moolenaar162b7142018-12-21 15:17:36 +0100387static void f_sign_place(typval_T *argvars, typval_T *rettv);
388static void f_sign_undefine(typval_T *argvars, typval_T *rettv);
389static void f_sign_unplace(typval_T *argvars, typval_T *rettv);
390#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200391static void f_simplify(typval_T *argvars, typval_T *rettv);
392#ifdef FEAT_FLOAT
393static void f_sin(typval_T *argvars, typval_T *rettv);
394static void f_sinh(typval_T *argvars, typval_T *rettv);
395#endif
396static void f_sort(typval_T *argvars, typval_T *rettv);
397static void f_soundfold(typval_T *argvars, typval_T *rettv);
398static void f_spellbadword(typval_T *argvars, typval_T *rettv);
399static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
400static void f_split(typval_T *argvars, typval_T *rettv);
401#ifdef FEAT_FLOAT
402static void f_sqrt(typval_T *argvars, typval_T *rettv);
403static void f_str2float(typval_T *argvars, typval_T *rettv);
404#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200405static void f_str2list(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200406static void f_str2nr(typval_T *argvars, typval_T *rettv);
407static void f_strchars(typval_T *argvars, typval_T *rettv);
408#ifdef HAVE_STRFTIME
409static void f_strftime(typval_T *argvars, typval_T *rettv);
410#endif
411static void f_strgetchar(typval_T *argvars, typval_T *rettv);
412static void f_stridx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200413static void f_strlen(typval_T *argvars, typval_T *rettv);
414static void f_strcharpart(typval_T *argvars, typval_T *rettv);
415static void f_strpart(typval_T *argvars, typval_T *rettv);
416static void f_strridx(typval_T *argvars, typval_T *rettv);
417static void f_strtrans(typval_T *argvars, typval_T *rettv);
418static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
419static void f_strwidth(typval_T *argvars, typval_T *rettv);
420static void f_submatch(typval_T *argvars, typval_T *rettv);
421static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200422static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200423static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200424static void f_synID(typval_T *argvars, typval_T *rettv);
425static void f_synIDattr(typval_T *argvars, typval_T *rettv);
426static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
427static void f_synstack(typval_T *argvars, typval_T *rettv);
428static void f_synconcealed(typval_T *argvars, typval_T *rettv);
429static void f_system(typval_T *argvars, typval_T *rettv);
430static void f_systemlist(typval_T *argvars, typval_T *rettv);
431static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
432static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
433static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
434static void f_taglist(typval_T *argvars, typval_T *rettv);
435static void f_tagfiles(typval_T *argvars, typval_T *rettv);
436static void f_tempname(typval_T *argvars, typval_T *rettv);
437static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
438static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200439static void f_test_feedinput(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200440static void f_test_option_not_set(typval_T *argvars, typval_T *rettv);
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100441static void f_test_override(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc3e92c12019-03-23 14:23:07 +0100442static void f_test_refcount(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200443static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100444static void f_test_ignore_error(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100445static void f_test_null_blob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200446#ifdef FEAT_JOB_CHANNEL
447static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
448#endif
449static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
450#ifdef FEAT_JOB_CHANNEL
451static void f_test_null_job(typval_T *argvars, typval_T *rettv);
452#endif
453static void f_test_null_list(typval_T *argvars, typval_T *rettv);
454static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
455static void f_test_null_string(typval_T *argvars, typval_T *rettv);
Bram Moolenaarab186732018-09-14 21:27:06 +0200456#ifdef FEAT_GUI
457static void f_test_scrollbar(typval_T *argvars, typval_T *rettv);
458#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200459static void f_test_settime(typval_T *argvars, typval_T *rettv);
460#ifdef FEAT_FLOAT
461static void f_tan(typval_T *argvars, typval_T *rettv);
462static void f_tanh(typval_T *argvars, typval_T *rettv);
463#endif
464#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200465static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200466static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200467static void f_timer_start(typval_T *argvars, typval_T *rettv);
468static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200469static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200470#endif
471static void f_tolower(typval_T *argvars, typval_T *rettv);
472static void f_toupper(typval_T *argvars, typval_T *rettv);
473static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100474static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200475#ifdef FEAT_FLOAT
476static void f_trunc(typval_T *argvars, typval_T *rettv);
477#endif
478static void f_type(typval_T *argvars, typval_T *rettv);
479static void f_undofile(typval_T *argvars, typval_T *rettv);
480static void f_undotree(typval_T *argvars, typval_T *rettv);
481static void f_uniq(typval_T *argvars, typval_T *rettv);
482static void f_values(typval_T *argvars, typval_T *rettv);
483static void f_virtcol(typval_T *argvars, typval_T *rettv);
484static void f_visualmode(typval_T *argvars, typval_T *rettv);
485static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
486static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
487static void f_win_getid(typval_T *argvars, typval_T *rettv);
488static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
489static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
490static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100491static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200492static void f_winbufnr(typval_T *argvars, typval_T *rettv);
493static void f_wincol(typval_T *argvars, typval_T *rettv);
494static void f_winheight(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200495static void f_winlayout(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200496static void f_winline(typval_T *argvars, typval_T *rettv);
497static void f_winnr(typval_T *argvars, typval_T *rettv);
498static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
499static void f_winrestview(typval_T *argvars, typval_T *rettv);
500static void f_winsaveview(typval_T *argvars, typval_T *rettv);
501static void f_winwidth(typval_T *argvars, typval_T *rettv);
502static void f_writefile(typval_T *argvars, typval_T *rettv);
503static void f_wordcount(typval_T *argvars, typval_T *rettv);
504static void f_xor(typval_T *argvars, typval_T *rettv);
505
506/*
507 * Array with names and number of arguments of all internal functions
508 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
509 */
510static struct fst
511{
512 char *f_name; /* function name */
513 char f_min_argc; /* minimal number of arguments */
514 char f_max_argc; /* maximal number of arguments */
515 void (*f_func)(typval_T *args, typval_T *rvar);
516 /* implementation of function */
517} functions[] =
518{
519#ifdef FEAT_FLOAT
520 {"abs", 1, 1, f_abs},
521 {"acos", 1, 1, f_acos}, /* WJMc */
522#endif
523 {"add", 2, 2, f_add},
524 {"and", 2, 2, f_and},
525 {"append", 2, 2, f_append},
Bram Moolenaarca851592018-06-06 21:04:07 +0200526 {"appendbufline", 3, 3, f_appendbufline},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200527 {"argc", 0, 1, f_argc},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200528 {"argidx", 0, 0, f_argidx},
529 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200530 {"argv", 0, 2, f_argv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200531#ifdef FEAT_FLOAT
532 {"asin", 1, 1, f_asin}, /* WJMc */
533#endif
Bram Moolenaarb48e96f2018-02-13 12:26:14 +0100534 {"assert_beeps", 1, 2, f_assert_beeps},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200535 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaard96ff162018-02-18 22:13:29 +0100536 {"assert_equalfile", 2, 2, f_assert_equalfile},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200537 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaar1307d1c2018-10-07 20:16:49 +0200538 {"assert_fails", 1, 3, f_assert_fails},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200539 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100540 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200541 {"assert_match", 2, 3, f_assert_match},
542 {"assert_notequal", 2, 3, f_assert_notequal},
543 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100544 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200545 {"assert_true", 1, 2, f_assert_true},
546#ifdef FEAT_FLOAT
547 {"atan", 1, 1, f_atan},
548 {"atan2", 2, 2, f_atan2},
549#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100550#ifdef FEAT_BEVAL
551 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100552# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100553 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100554# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100555#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200556 {"browse", 4, 4, f_browse},
557 {"browsedir", 2, 2, f_browsedir},
558 {"bufexists", 1, 1, f_bufexists},
559 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
560 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
561 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
562 {"buflisted", 1, 1, f_buflisted},
563 {"bufloaded", 1, 1, f_bufloaded},
564 {"bufname", 1, 1, f_bufname},
565 {"bufnr", 1, 2, f_bufnr},
566 {"bufwinid", 1, 1, f_bufwinid},
567 {"bufwinnr", 1, 1, f_bufwinnr},
568 {"byte2line", 1, 1, f_byte2line},
569 {"byteidx", 2, 2, f_byteidx},
570 {"byteidxcomp", 2, 2, f_byteidxcomp},
571 {"call", 2, 3, f_call},
572#ifdef FEAT_FLOAT
573 {"ceil", 1, 1, f_ceil},
574#endif
575#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100576 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200577 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200578 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200579 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
580 {"ch_evalraw", 2, 3, f_ch_evalraw},
581 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
582 {"ch_getjob", 1, 1, f_ch_getjob},
583 {"ch_info", 1, 1, f_ch_info},
584 {"ch_log", 1, 2, f_ch_log},
585 {"ch_logfile", 1, 2, f_ch_logfile},
586 {"ch_open", 1, 2, f_ch_open},
587 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100588 {"ch_readblob", 1, 2, f_ch_readblob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200589 {"ch_readraw", 1, 2, f_ch_readraw},
590 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
591 {"ch_sendraw", 2, 3, f_ch_sendraw},
592 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200593 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200594#endif
595 {"changenr", 0, 0, f_changenr},
596 {"char2nr", 1, 2, f_char2nr},
597 {"cindent", 1, 1, f_cindent},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100598 {"clearmatches", 0, 1, f_clearmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200599 {"col", 1, 1, f_col},
600#if defined(FEAT_INS_EXPAND)
601 {"complete", 2, 2, f_complete},
602 {"complete_add", 1, 1, f_complete_add},
603 {"complete_check", 0, 0, f_complete_check},
Bram Moolenaarfd133322019-03-29 12:20:27 +0100604 {"complete_info", 0, 1, f_complete_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200605#endif
606 {"confirm", 1, 4, f_confirm},
607 {"copy", 1, 1, f_copy},
608#ifdef FEAT_FLOAT
609 {"cos", 1, 1, f_cos},
610 {"cosh", 1, 1, f_cosh},
611#endif
612 {"count", 2, 4, f_count},
613 {"cscope_connection",0,3, f_cscope_connection},
614 {"cursor", 1, 3, f_cursor},
Bram Moolenaar4f974752019-02-17 17:44:42 +0100615#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200616 {"debugbreak", 1, 1, f_debugbreak},
617#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200618 {"deepcopy", 1, 2, f_deepcopy},
619 {"delete", 1, 2, f_delete},
Bram Moolenaard79a2622018-06-07 18:17:46 +0200620 {"deletebufline", 2, 3, f_deletebufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200621 {"did_filetype", 0, 0, f_did_filetype},
622 {"diff_filler", 1, 1, f_diff_filler},
623 {"diff_hlID", 2, 2, f_diff_hlID},
624 {"empty", 1, 1, f_empty},
625 {"escape", 2, 2, f_escape},
626 {"eval", 1, 1, f_eval},
627 {"eventhandler", 0, 0, f_eventhandler},
628 {"executable", 1, 1, f_executable},
629 {"execute", 1, 2, f_execute},
630 {"exepath", 1, 1, f_exepath},
631 {"exists", 1, 1, f_exists},
632#ifdef FEAT_FLOAT
633 {"exp", 1, 1, f_exp},
634#endif
635 {"expand", 1, 3, f_expand},
636 {"extend", 2, 3, f_extend},
637 {"feedkeys", 1, 2, f_feedkeys},
638 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
639 {"filereadable", 1, 1, f_filereadable},
640 {"filewritable", 1, 1, f_filewritable},
641 {"filter", 2, 2, f_filter},
642 {"finddir", 1, 3, f_finddir},
643 {"findfile", 1, 3, f_findfile},
644#ifdef FEAT_FLOAT
645 {"float2nr", 1, 1, f_float2nr},
646 {"floor", 1, 1, f_floor},
647 {"fmod", 2, 2, f_fmod},
648#endif
649 {"fnameescape", 1, 1, f_fnameescape},
650 {"fnamemodify", 2, 2, f_fnamemodify},
651 {"foldclosed", 1, 1, f_foldclosed},
652 {"foldclosedend", 1, 1, f_foldclosedend},
653 {"foldlevel", 1, 1, f_foldlevel},
654 {"foldtext", 0, 0, f_foldtext},
655 {"foldtextresult", 1, 1, f_foldtextresult},
656 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200657 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200658 {"function", 1, 3, f_function},
659 {"garbagecollect", 0, 1, f_garbagecollect},
660 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200661 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200662 {"getbufline", 2, 3, f_getbufline},
663 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100664 {"getchangelist", 1, 1, f_getchangelist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200665 {"getchar", 0, 1, f_getchar},
666 {"getcharmod", 0, 0, f_getcharmod},
667 {"getcharsearch", 0, 0, f_getcharsearch},
668 {"getcmdline", 0, 0, f_getcmdline},
669 {"getcmdpos", 0, 0, f_getcmdpos},
670 {"getcmdtype", 0, 0, f_getcmdtype},
671 {"getcmdwintype", 0, 0, f_getcmdwintype},
672#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200673 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200674#endif
675 {"getcurpos", 0, 0, f_getcurpos},
676 {"getcwd", 0, 2, f_getcwd},
677 {"getfontname", 0, 1, f_getfontname},
678 {"getfperm", 1, 1, f_getfperm},
679 {"getfsize", 1, 1, f_getfsize},
680 {"getftime", 1, 1, f_getftime},
681 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100682 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200683 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200684 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100685 {"getmatches", 0, 1, f_getmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200686 {"getpid", 0, 0, f_getpid},
687 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200688 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200689 {"getreg", 0, 3, f_getreg},
690 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200691 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200692 {"gettabvar", 2, 3, f_gettabvar},
693 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100694 {"gettagstack", 0, 1, f_gettagstack},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200695 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100696 {"getwinpos", 0, 1, f_getwinpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200697 {"getwinposx", 0, 0, f_getwinposx},
698 {"getwinposy", 0, 0, f_getwinposy},
699 {"getwinvar", 2, 3, f_getwinvar},
700 {"glob", 1, 4, f_glob},
701 {"glob2regpat", 1, 1, f_glob2regpat},
702 {"globpath", 2, 5, f_globpath},
703 {"has", 1, 1, f_has},
704 {"has_key", 2, 2, f_has_key},
705 {"haslocaldir", 0, 2, f_haslocaldir},
706 {"hasmapto", 1, 3, f_hasmapto},
707 {"highlightID", 1, 1, f_hlID}, /* obsolete */
708 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
709 {"histadd", 2, 2, f_histadd},
710 {"histdel", 1, 2, f_histdel},
711 {"histget", 1, 2, f_histget},
712 {"histnr", 1, 1, f_histnr},
713 {"hlID", 1, 1, f_hlID},
714 {"hlexists", 1, 1, f_hlexists},
715 {"hostname", 0, 0, f_hostname},
716 {"iconv", 3, 3, f_iconv},
717 {"indent", 1, 1, f_indent},
718 {"index", 2, 4, f_index},
719 {"input", 1, 3, f_input},
720 {"inputdialog", 1, 3, f_inputdialog},
721 {"inputlist", 1, 1, f_inputlist},
722 {"inputrestore", 0, 0, f_inputrestore},
723 {"inputsave", 0, 0, f_inputsave},
724 {"inputsecret", 1, 2, f_inputsecret},
725 {"insert", 2, 3, f_insert},
726 {"invert", 1, 1, f_invert},
727 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200728#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
729 {"isinf", 1, 1, f_isinf},
730#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200731 {"islocked", 1, 1, f_islocked},
732#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
733 {"isnan", 1, 1, f_isnan},
734#endif
735 {"items", 1, 1, f_items},
736#ifdef FEAT_JOB_CHANNEL
737 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200738 {"job_info", 0, 1, f_job_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200739 {"job_setoptions", 2, 2, f_job_setoptions},
740 {"job_start", 1, 2, f_job_start},
741 {"job_status", 1, 1, f_job_status},
742 {"job_stop", 1, 2, f_job_stop},
743#endif
744 {"join", 1, 2, f_join},
745 {"js_decode", 1, 1, f_js_decode},
746 {"js_encode", 1, 1, f_js_encode},
747 {"json_decode", 1, 1, f_json_decode},
748 {"json_encode", 1, 1, f_json_encode},
749 {"keys", 1, 1, f_keys},
750 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
751 {"len", 1, 1, f_len},
752 {"libcall", 3, 3, f_libcall},
753 {"libcallnr", 3, 3, f_libcallnr},
754 {"line", 1, 1, f_line},
755 {"line2byte", 1, 1, f_line2byte},
756 {"lispindent", 1, 1, f_lispindent},
Bram Moolenaar9d401282019-04-06 13:18:12 +0200757 {"list2str", 1, 2, f_list2str},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200758 {"localtime", 0, 0, f_localtime},
759#ifdef FEAT_FLOAT
760 {"log", 1, 1, f_log},
761 {"log10", 1, 1, f_log10},
762#endif
763#ifdef FEAT_LUA
764 {"luaeval", 1, 2, f_luaeval},
765#endif
766 {"map", 2, 2, f_map},
767 {"maparg", 1, 4, f_maparg},
768 {"mapcheck", 1, 3, f_mapcheck},
769 {"match", 2, 4, f_match},
770 {"matchadd", 2, 5, f_matchadd},
771 {"matchaddpos", 2, 5, f_matchaddpos},
772 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100773 {"matchdelete", 1, 2, f_matchdelete},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200774 {"matchend", 2, 4, f_matchend},
775 {"matchlist", 2, 4, f_matchlist},
776 {"matchstr", 2, 4, f_matchstr},
777 {"matchstrpos", 2, 4, f_matchstrpos},
778 {"max", 1, 1, f_max},
779 {"min", 1, 1, f_min},
780#ifdef vim_mkdir
781 {"mkdir", 1, 3, f_mkdir},
782#endif
783 {"mode", 0, 1, f_mode},
784#ifdef FEAT_MZSCHEME
785 {"mzeval", 1, 1, f_mzeval},
786#endif
787 {"nextnonblank", 1, 1, f_nextnonblank},
788 {"nr2char", 1, 2, f_nr2char},
789 {"or", 2, 2, f_or},
790 {"pathshorten", 1, 1, f_pathshorten},
791#ifdef FEAT_PERL
792 {"perleval", 1, 1, f_perleval},
793#endif
794#ifdef FEAT_FLOAT
795 {"pow", 2, 2, f_pow},
796#endif
797 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100798 {"printf", 1, 19, f_printf},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200799#ifdef FEAT_JOB_CHANNEL
800 {"prompt_setcallback", 2, 2, f_prompt_setcallback},
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200801 {"prompt_setinterrupt", 2, 2, f_prompt_setinterrupt},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200802 {"prompt_setprompt", 2, 2, f_prompt_setprompt},
803#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100804#ifdef FEAT_TEXT_PROP
805 {"prop_add", 3, 3, f_prop_add},
806 {"prop_clear", 1, 3, f_prop_clear},
807 {"prop_list", 1, 2, f_prop_list},
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100808 {"prop_remove", 1, 3, f_prop_remove},
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100809 {"prop_type_add", 2, 2, f_prop_type_add},
810 {"prop_type_change", 2, 2, f_prop_type_change},
811 {"prop_type_delete", 1, 2, f_prop_type_delete},
812 {"prop_type_get", 1, 2, f_prop_type_get},
813 {"prop_type_list", 0, 1, f_prop_type_list},
814#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200815 {"pumvisible", 0, 0, f_pumvisible},
816#ifdef FEAT_PYTHON3
817 {"py3eval", 1, 1, f_py3eval},
818#endif
819#ifdef FEAT_PYTHON
820 {"pyeval", 1, 1, f_pyeval},
821#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100822#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
823 {"pyxeval", 1, 1, f_pyxeval},
824#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200825 {"range", 1, 3, f_range},
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200826 {"readdir", 1, 2, f_readdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200827 {"readfile", 1, 3, f_readfile},
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200828 {"reg_executing", 0, 0, f_reg_executing},
829 {"reg_recording", 0, 0, f_reg_recording},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200830 {"reltime", 0, 2, f_reltime},
831#ifdef FEAT_FLOAT
832 {"reltimefloat", 1, 1, f_reltimefloat},
833#endif
834 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100835 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200836 {"remote_foreground", 1, 1, f_remote_foreground},
837 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100838 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200839 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100840 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200841 {"remove", 2, 3, f_remove},
842 {"rename", 2, 2, f_rename},
843 {"repeat", 2, 2, f_repeat},
844 {"resolve", 1, 1, f_resolve},
845 {"reverse", 1, 1, f_reverse},
846#ifdef FEAT_FLOAT
847 {"round", 1, 1, f_round},
848#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100849#ifdef FEAT_RUBY
850 {"rubyeval", 1, 1, f_rubyeval},
851#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200852 {"screenattr", 2, 2, f_screenattr},
853 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100854 {"screenchars", 2, 2, f_screenchars},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200855 {"screencol", 0, 0, f_screencol},
856 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100857 {"screenstring", 2, 2, f_screenstring},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200858 {"search", 1, 4, f_search},
859 {"searchdecl", 1, 3, f_searchdecl},
860 {"searchpair", 3, 7, f_searchpair},
861 {"searchpairpos", 3, 7, f_searchpairpos},
862 {"searchpos", 1, 4, f_searchpos},
863 {"server2client", 2, 2, f_server2client},
864 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200865 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200866 {"setbufvar", 3, 3, f_setbufvar},
867 {"setcharsearch", 1, 1, f_setcharsearch},
868 {"setcmdpos", 1, 1, f_setcmdpos},
869 {"setfperm", 2, 2, f_setfperm},
870 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200871 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100872 {"setmatches", 1, 2, f_setmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200873 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200874 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200875 {"setreg", 2, 3, f_setreg},
876 {"settabvar", 3, 3, f_settabvar},
877 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100878 {"settagstack", 2, 3, f_settagstack},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200879 {"setwinvar", 3, 3, f_setwinvar},
880#ifdef FEAT_CRYPT
881 {"sha256", 1, 1, f_sha256},
882#endif
883 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaarf9514162018-11-22 03:08:29 +0100884 {"shiftwidth", 0, 1, f_shiftwidth},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100885#ifdef FEAT_SIGNS
886 {"sign_define", 1, 2, f_sign_define},
887 {"sign_getdefined", 0, 1, f_sign_getdefined},
888 {"sign_getplaced", 0, 2, f_sign_getplaced},
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100889 {"sign_jump", 3, 3, f_sign_jump},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100890 {"sign_place", 4, 5, f_sign_place},
891 {"sign_undefine", 0, 1, f_sign_undefine},
892 {"sign_unplace", 1, 2, f_sign_unplace},
893#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200894 {"simplify", 1, 1, f_simplify},
895#ifdef FEAT_FLOAT
896 {"sin", 1, 1, f_sin},
897 {"sinh", 1, 1, f_sinh},
898#endif
899 {"sort", 1, 3, f_sort},
900 {"soundfold", 1, 1, f_soundfold},
901 {"spellbadword", 0, 1, f_spellbadword},
902 {"spellsuggest", 1, 3, f_spellsuggest},
903 {"split", 1, 3, f_split},
904#ifdef FEAT_FLOAT
905 {"sqrt", 1, 1, f_sqrt},
906 {"str2float", 1, 1, f_str2float},
907#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200908 {"str2list", 1, 2, f_str2list},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200909 {"str2nr", 1, 2, f_str2nr},
910 {"strcharpart", 2, 3, f_strcharpart},
911 {"strchars", 1, 2, f_strchars},
912 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
913#ifdef HAVE_STRFTIME
914 {"strftime", 1, 2, f_strftime},
915#endif
916 {"strgetchar", 2, 2, f_strgetchar},
917 {"stridx", 2, 3, f_stridx},
918 {"string", 1, 1, f_string},
919 {"strlen", 1, 1, f_strlen},
920 {"strpart", 2, 3, f_strpart},
921 {"strridx", 2, 3, f_strridx},
922 {"strtrans", 1, 1, f_strtrans},
923 {"strwidth", 1, 1, f_strwidth},
924 {"submatch", 1, 2, f_submatch},
925 {"substitute", 4, 4, f_substitute},
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200926 {"swapinfo", 1, 1, f_swapinfo},
Bram Moolenaar110bd602018-09-16 18:46:59 +0200927 {"swapname", 1, 1, f_swapname},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200928 {"synID", 3, 3, f_synID},
929 {"synIDattr", 2, 3, f_synIDattr},
930 {"synIDtrans", 1, 1, f_synIDtrans},
931 {"synconcealed", 2, 2, f_synconcealed},
932 {"synstack", 2, 2, f_synstack},
933 {"system", 1, 2, f_system},
934 {"systemlist", 1, 2, f_systemlist},
935 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
936 {"tabpagenr", 0, 1, f_tabpagenr},
937 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
938 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100939 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200940#ifdef FEAT_FLOAT
941 {"tan", 1, 1, f_tan},
942 {"tanh", 1, 1, f_tanh},
943#endif
944 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200945#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100946 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
947 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100948 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200949 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200950# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
951 {"term_getansicolors", 1, 1, f_term_getansicolors},
952# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200953 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200954 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200955 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200956 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200957 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200958 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200959 {"term_getstatus", 1, 1, f_term_getstatus},
960 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200961 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200962 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200963 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200964 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200965# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
966 {"term_setansicolors", 2, 2, f_term_setansicolors},
967# endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100968 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100969 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaara42d3632018-04-14 17:05:38 +0200970 {"term_setsize", 3, 3, f_term_setsize},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200971 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200972 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200973#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200974 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
975 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200976 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200977 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100978 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +0100979 {"test_null_blob", 0, 0, f_test_null_blob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200980#ifdef FEAT_JOB_CHANNEL
981 {"test_null_channel", 0, 0, f_test_null_channel},
982#endif
983 {"test_null_dict", 0, 0, f_test_null_dict},
984#ifdef FEAT_JOB_CHANNEL
985 {"test_null_job", 0, 0, f_test_null_job},
986#endif
987 {"test_null_list", 0, 0, f_test_null_list},
988 {"test_null_partial", 0, 0, f_test_null_partial},
989 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200990 {"test_option_not_set", 1, 1, f_test_option_not_set},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +0100991 {"test_override", 2, 2, f_test_override},
992 {"test_refcount", 1, 1, f_test_refcount},
Bram Moolenaarab186732018-09-14 21:27:06 +0200993#ifdef FEAT_GUI
994 {"test_scrollbar", 3, 3, f_test_scrollbar},
995#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200996 {"test_settime", 1, 1, f_test_settime},
997#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200998 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200999 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001000 {"timer_start", 2, 3, f_timer_start},
1001 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +02001002 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001003#endif
1004 {"tolower", 1, 1, f_tolower},
1005 {"toupper", 1, 1, f_toupper},
1006 {"tr", 3, 3, f_tr},
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01001007 {"trim", 1, 2, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001008#ifdef FEAT_FLOAT
1009 {"trunc", 1, 1, f_trunc},
1010#endif
1011 {"type", 1, 1, f_type},
1012 {"undofile", 1, 1, f_undofile},
1013 {"undotree", 0, 0, f_undotree},
1014 {"uniq", 1, 3, f_uniq},
1015 {"values", 1, 1, f_values},
1016 {"virtcol", 1, 1, f_virtcol},
1017 {"visualmode", 0, 1, f_visualmode},
1018 {"wildmenumode", 0, 0, f_wildmenumode},
1019 {"win_findbuf", 1, 1, f_win_findbuf},
1020 {"win_getid", 0, 2, f_win_getid},
1021 {"win_gotoid", 1, 1, f_win_gotoid},
1022 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
1023 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +01001024 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001025 {"winbufnr", 1, 1, f_winbufnr},
1026 {"wincol", 0, 0, f_wincol},
1027 {"winheight", 1, 1, f_winheight},
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +02001028 {"winlayout", 0, 1, f_winlayout},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001029 {"winline", 0, 0, f_winline},
1030 {"winnr", 0, 1, f_winnr},
1031 {"winrestcmd", 0, 0, f_winrestcmd},
1032 {"winrestview", 1, 1, f_winrestview},
1033 {"winsaveview", 0, 0, f_winsaveview},
1034 {"winwidth", 1, 1, f_winwidth},
1035 {"wordcount", 0, 0, f_wordcount},
1036 {"writefile", 2, 3, f_writefile},
1037 {"xor", 2, 2, f_xor},
1038};
1039
1040#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
1041
1042/*
1043 * Function given to ExpandGeneric() to obtain the list of internal
1044 * or user defined function names.
1045 */
1046 char_u *
1047get_function_name(expand_T *xp, int idx)
1048{
1049 static int intidx = -1;
1050 char_u *name;
1051
1052 if (idx == 0)
1053 intidx = -1;
1054 if (intidx < 0)
1055 {
1056 name = get_user_func_name(xp, idx);
1057 if (name != NULL)
1058 return name;
1059 }
1060 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
1061 {
1062 STRCPY(IObuff, functions[intidx].f_name);
1063 STRCAT(IObuff, "(");
1064 if (functions[intidx].f_max_argc == 0)
1065 STRCAT(IObuff, ")");
1066 return IObuff;
1067 }
1068
1069 return NULL;
1070}
1071
1072/*
1073 * Function given to ExpandGeneric() to obtain the list of internal or
1074 * user defined variable or function names.
1075 */
1076 char_u *
1077get_expr_name(expand_T *xp, int idx)
1078{
1079 static int intidx = -1;
1080 char_u *name;
1081
1082 if (idx == 0)
1083 intidx = -1;
1084 if (intidx < 0)
1085 {
1086 name = get_function_name(xp, idx);
1087 if (name != NULL)
1088 return name;
1089 }
1090 return get_user_var_name(xp, ++intidx);
1091}
1092
1093#endif /* FEAT_CMDL_COMPL */
1094
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001095/*
1096 * Find internal function in table above.
1097 * Return index, or -1 if not found
1098 */
1099 int
1100find_internal_func(
1101 char_u *name) /* name of the function */
1102{
1103 int first = 0;
1104 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
1105 int cmp;
1106 int x;
1107
1108 /*
1109 * Find the function name in the table. Binary search.
1110 */
1111 while (first <= last)
1112 {
1113 x = first + ((unsigned)(last - first) >> 1);
1114 cmp = STRCMP(name, functions[x].f_name);
1115 if (cmp < 0)
1116 last = x - 1;
1117 else if (cmp > 0)
1118 first = x + 1;
1119 else
1120 return x;
1121 }
1122 return -1;
1123}
1124
1125 int
1126call_internal_func(
1127 char_u *name,
1128 int argcount,
1129 typval_T *argvars,
1130 typval_T *rettv)
1131{
1132 int i;
1133
1134 i = find_internal_func(name);
1135 if (i < 0)
1136 return ERROR_UNKNOWN;
1137 if (argcount < functions[i].f_min_argc)
1138 return ERROR_TOOFEW;
1139 if (argcount > functions[i].f_max_argc)
1140 return ERROR_TOOMANY;
1141 argvars[argcount].v_type = VAR_UNKNOWN;
1142 functions[i].f_func(argvars, rettv);
1143 return ERROR_NONE;
1144}
1145
1146/*
1147 * Return TRUE for a non-zero Number and a non-empty String.
1148 */
1149 static int
1150non_zero_arg(typval_T *argvars)
1151{
1152 return ((argvars[0].v_type == VAR_NUMBER
1153 && argvars[0].vval.v_number != 0)
1154 || (argvars[0].v_type == VAR_SPECIAL
1155 && argvars[0].vval.v_number == VVAL_TRUE)
1156 || (argvars[0].v_type == VAR_STRING
1157 && argvars[0].vval.v_string != NULL
1158 && *argvars[0].vval.v_string != NUL));
1159}
1160
1161/*
1162 * Get the lnum from the first argument.
1163 * Also accepts ".", "$", etc., but that only works for the current buffer.
1164 * Returns -1 on error.
1165 */
1166 static linenr_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001167tv_get_lnum(typval_T *argvars)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001168{
1169 typval_T rettv;
1170 linenr_T lnum;
1171
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001172 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001173 if (lnum == 0) /* no valid number, try using line() */
1174 {
1175 rettv.v_type = VAR_NUMBER;
1176 f_line(argvars, &rettv);
1177 lnum = (linenr_T)rettv.vval.v_number;
1178 clear_tv(&rettv);
1179 }
1180 return lnum;
1181}
1182
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001183/*
1184 * Get the lnum from the first argument.
1185 * Also accepts "$", then "buf" is used.
1186 * Returns 0 on error.
1187 */
1188 static linenr_T
1189tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
1190{
1191 if (argvars[0].v_type == VAR_STRING
1192 && argvars[0].vval.v_string != NULL
1193 && argvars[0].vval.v_string[0] == '$'
1194 && buf != NULL)
1195 return buf->b_ml.ml_line_count;
1196 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1197}
1198
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001199#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001200/*
1201 * Get the float value of "argvars[0]" into "f".
1202 * Returns FAIL when the argument is not a Number or Float.
1203 */
1204 static int
1205get_float_arg(typval_T *argvars, float_T *f)
1206{
1207 if (argvars[0].v_type == VAR_FLOAT)
1208 {
1209 *f = argvars[0].vval.v_float;
1210 return OK;
1211 }
1212 if (argvars[0].v_type == VAR_NUMBER)
1213 {
1214 *f = (float_T)argvars[0].vval.v_number;
1215 return OK;
1216 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001217 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001218 return FAIL;
1219}
1220
1221/*
1222 * "abs(expr)" function
1223 */
1224 static void
1225f_abs(typval_T *argvars, typval_T *rettv)
1226{
1227 if (argvars[0].v_type == VAR_FLOAT)
1228 {
1229 rettv->v_type = VAR_FLOAT;
1230 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1231 }
1232 else
1233 {
1234 varnumber_T n;
1235 int error = FALSE;
1236
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001237 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001238 if (error)
1239 rettv->vval.v_number = -1;
1240 else if (n > 0)
1241 rettv->vval.v_number = n;
1242 else
1243 rettv->vval.v_number = -n;
1244 }
1245}
1246
1247/*
1248 * "acos()" function
1249 */
1250 static void
1251f_acos(typval_T *argvars, typval_T *rettv)
1252{
1253 float_T f = 0.0;
1254
1255 rettv->v_type = VAR_FLOAT;
1256 if (get_float_arg(argvars, &f) == OK)
1257 rettv->vval.v_float = acos(f);
1258 else
1259 rettv->vval.v_float = 0.0;
1260}
1261#endif
1262
1263/*
1264 * "add(list, item)" function
1265 */
1266 static void
1267f_add(typval_T *argvars, typval_T *rettv)
1268{
1269 list_T *l;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001270 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001271
1272 rettv->vval.v_number = 1; /* Default: Failed */
1273 if (argvars[0].v_type == VAR_LIST)
1274 {
1275 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001276 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001277 (char_u *)N_("add() argument"), TRUE)
1278 && list_append_tv(l, &argvars[1]) == OK)
1279 copy_tv(&argvars[0], rettv);
1280 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001281 else if (argvars[0].v_type == VAR_BLOB)
1282 {
1283 if ((b = argvars[0].vval.v_blob) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001284 && !var_check_lock(b->bv_lock,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001285 (char_u *)N_("add() argument"), TRUE))
1286 {
Bram Moolenaar05500ec2019-01-13 19:10:33 +01001287 int error = FALSE;
1288 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
1289
1290 if (!error)
1291 {
1292 ga_append(&b->bv_ga, (int)n);
1293 copy_tv(&argvars[0], rettv);
1294 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001295 }
1296 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001297 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01001298 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001299}
1300
1301/*
1302 * "and(expr, expr)" function
1303 */
1304 static void
1305f_and(typval_T *argvars, typval_T *rettv)
1306{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001307 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1308 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001309}
1310
1311/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02001312 * If there is a window for "curbuf", make it the current window.
1313 */
1314 static void
1315find_win_for_curbuf(void)
1316{
1317 wininfo_T *wip;
1318
1319 for (wip = curbuf->b_wininfo; wip != NULL; wip = wip->wi_next)
1320 {
1321 if (wip->wi_win != NULL)
1322 {
1323 curwin = wip->wi_win;
1324 break;
1325 }
1326 }
1327}
1328
1329/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001330 * Set line or list of lines in buffer "buf".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001331 */
1332 static void
Bram Moolenaarca851592018-06-06 21:04:07 +02001333set_buffer_lines(
1334 buf_T *buf,
1335 linenr_T lnum_arg,
1336 int append,
1337 typval_T *lines,
1338 typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001339{
Bram Moolenaarca851592018-06-06 21:04:07 +02001340 linenr_T lnum = lnum_arg + (append ? 1 : 0);
1341 char_u *line = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001342 list_T *l = NULL;
1343 listitem_T *li = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001344 long added = 0;
Bram Moolenaarca851592018-06-06 21:04:07 +02001345 linenr_T append_lnum;
1346 buf_T *curbuf_save = NULL;
1347 win_T *curwin_save = NULL;
1348 int is_curbuf = buf == curbuf;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001349
Bram Moolenaarca851592018-06-06 21:04:07 +02001350 /* When using the current buffer ml_mfp will be set if needed. Useful when
1351 * setline() is used on startup. For other buffers the buffer must be
1352 * loaded. */
1353 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001354 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001355 rettv->vval.v_number = 1; /* FAIL */
1356 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001357 }
1358
Bram Moolenaarca851592018-06-06 21:04:07 +02001359 if (!is_curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001360 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001361 curbuf_save = curbuf;
1362 curwin_save = curwin;
1363 curbuf = buf;
Bram Moolenaard79a2622018-06-07 18:17:46 +02001364 find_win_for_curbuf();
Bram Moolenaarca851592018-06-06 21:04:07 +02001365 }
1366
1367 if (append)
1368 // appendbufline() uses the line number below which we insert
1369 append_lnum = lnum - 1;
1370 else
1371 // setbufline() uses the line number above which we insert, we only
1372 // append if it's below the last line
1373 append_lnum = curbuf->b_ml.ml_line_count;
1374
1375 if (lines->v_type == VAR_LIST)
1376 {
1377 l = lines->vval.v_list;
1378 li = l->lv_first;
1379 }
1380 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001381 line = tv_get_string_chk(lines);
Bram Moolenaarca851592018-06-06 21:04:07 +02001382
1383 /* default result is zero == OK */
1384 for (;;)
1385 {
1386 if (l != NULL)
1387 {
1388 /* list argument, get next string */
1389 if (li == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001390 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001391 line = tv_get_string_chk(&li->li_tv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001392 li = li->li_next;
1393 }
1394
Bram Moolenaarca851592018-06-06 21:04:07 +02001395 rettv->vval.v_number = 1; /* FAIL */
1396 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
1397 break;
1398
1399 /* When coming here from Insert mode, sync undo, so that this can be
1400 * undone separately from what was previously inserted. */
1401 if (u_sync_once == 2)
1402 {
1403 u_sync_once = 1; /* notify that u_sync() was called */
1404 u_sync(TRUE);
1405 }
1406
1407 if (!append && lnum <= curbuf->b_ml.ml_line_count)
1408 {
Bram Moolenaar21b50382019-01-04 18:07:24 +01001409 // Existing line, replace it.
1410 // Removes any existing text properties.
1411 if (u_savesub(lnum) == OK && ml_replace_len(
1412 lnum, line, (colnr_T)STRLEN(line) + 1, TRUE, TRUE) == OK)
Bram Moolenaarca851592018-06-06 21:04:07 +02001413 {
1414 changed_bytes(lnum, 0);
1415 if (is_curbuf && lnum == curwin->w_cursor.lnum)
1416 check_cursor_col();
1417 rettv->vval.v_number = 0; /* OK */
1418 }
1419 }
1420 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
1421 {
1422 /* append the line */
1423 ++added;
1424 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
1425 rettv->vval.v_number = 0; /* OK */
1426 }
1427
1428 if (l == NULL) /* only one string argument */
1429 break;
1430 ++lnum;
1431 }
1432
1433 if (added > 0)
1434 {
1435 win_T *wp;
1436 tabpage_T *tp;
1437
1438 appended_lines_mark(append_lnum, added);
1439 FOR_ALL_TAB_WINDOWS(tp, wp)
1440 if (wp->w_buffer == buf && wp->w_cursor.lnum > append_lnum)
1441 wp->w_cursor.lnum += added;
1442 check_cursor_col();
1443
Bram Moolenaarf2732452018-06-03 14:47:35 +02001444#ifdef FEAT_JOB_CHANNEL
1445 if (bt_prompt(curbuf) && (State & INSERT))
1446 // show the line with the prompt
1447 update_topline();
1448#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001449 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001450
1451 if (!is_curbuf)
1452 {
1453 curbuf = curbuf_save;
1454 curwin = curwin_save;
1455 }
1456}
1457
1458/*
1459 * "append(lnum, string/list)" function
1460 */
1461 static void
1462f_append(typval_T *argvars, typval_T *rettv)
1463{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001464 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaarca851592018-06-06 21:04:07 +02001465
1466 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1467}
1468
1469/*
1470 * "appendbufline(buf, lnum, string/list)" function
1471 */
1472 static void
1473f_appendbufline(typval_T *argvars, typval_T *rettv)
1474{
1475 linenr_T lnum;
1476 buf_T *buf;
1477
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001478 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarca851592018-06-06 21:04:07 +02001479 if (buf == NULL)
1480 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001481 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001482 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001483 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +02001484 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1485 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001486}
1487
1488/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001489 * "argc([window id])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001490 */
1491 static void
Bram Moolenaare6e39892018-10-25 12:32:11 +02001492f_argc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001493{
Bram Moolenaare6e39892018-10-25 12:32:11 +02001494 win_T *wp;
1495
1496 if (argvars[0].v_type == VAR_UNKNOWN)
1497 // use the current window
1498 rettv->vval.v_number = ARGCOUNT;
1499 else if (argvars[0].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001500 && tv_get_number(&argvars[0]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001501 // use the global argument list
1502 rettv->vval.v_number = GARGCOUNT;
1503 else
1504 {
1505 // use the argument list of the specified window
1506 wp = find_win_by_nr_or_id(&argvars[0]);
1507 if (wp != NULL)
1508 rettv->vval.v_number = WARGCOUNT(wp);
1509 else
1510 rettv->vval.v_number = -1;
1511 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001512}
1513
1514/*
1515 * "argidx()" function
1516 */
1517 static void
1518f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1519{
1520 rettv->vval.v_number = curwin->w_arg_idx;
1521}
1522
1523/*
1524 * "arglistid()" function
1525 */
1526 static void
1527f_arglistid(typval_T *argvars, typval_T *rettv)
1528{
1529 win_T *wp;
1530
1531 rettv->vval.v_number = -1;
1532 wp = find_tabwin(&argvars[0], &argvars[1]);
1533 if (wp != NULL)
1534 rettv->vval.v_number = wp->w_alist->id;
1535}
1536
1537/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001538 * Get the argument list for a given window
1539 */
1540 static void
1541get_arglist_as_rettv(aentry_T *arglist, int argcount, typval_T *rettv)
1542{
1543 int idx;
1544
1545 if (rettv_list_alloc(rettv) == OK && arglist != NULL)
1546 for (idx = 0; idx < argcount; ++idx)
1547 list_append_string(rettv->vval.v_list,
1548 alist_name(&arglist[idx]), -1);
1549}
1550
1551/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001552 * "argv(nr)" function
1553 */
1554 static void
1555f_argv(typval_T *argvars, typval_T *rettv)
1556{
1557 int idx;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001558 aentry_T *arglist = NULL;
1559 int argcount = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001560
1561 if (argvars[0].v_type != VAR_UNKNOWN)
1562 {
Bram Moolenaare6e39892018-10-25 12:32:11 +02001563 if (argvars[1].v_type == VAR_UNKNOWN)
1564 {
1565 arglist = ARGLIST;
1566 argcount = ARGCOUNT;
1567 }
1568 else if (argvars[1].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001569 && tv_get_number(&argvars[1]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001570 {
1571 arglist = GARGLIST;
1572 argcount = GARGCOUNT;
1573 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001574 else
Bram Moolenaare6e39892018-10-25 12:32:11 +02001575 {
1576 win_T *wp = find_win_by_nr_or_id(&argvars[1]);
1577
1578 if (wp != NULL)
1579 {
1580 /* Use the argument list of the specified window */
1581 arglist = WARGLIST(wp);
1582 argcount = WARGCOUNT(wp);
1583 }
1584 }
1585
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001586 rettv->v_type = VAR_STRING;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001587 rettv->vval.v_string = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001588 idx = tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02001589 if (arglist != NULL && idx >= 0 && idx < argcount)
1590 rettv->vval.v_string = vim_strsave(alist_name(&arglist[idx]));
1591 else if (idx == -1)
1592 get_arglist_as_rettv(arglist, argcount, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001593 }
Bram Moolenaare6e39892018-10-25 12:32:11 +02001594 else
1595 get_arglist_as_rettv(ARGLIST, ARGCOUNT, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001596}
1597
1598/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001599 * "assert_beeps(cmd [, error])" function
1600 */
1601 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001602f_assert_beeps(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001603{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001604 rettv->vval.v_number = assert_beeps(argvars);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001605}
1606
1607/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001608 * "assert_equal(expected, actual[, msg])" function
1609 */
1610 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001611f_assert_equal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001612{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001613 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_EQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001614}
1615
1616/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01001617 * "assert_equalfile(fname-one, fname-two)" function
1618 */
1619 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001620f_assert_equalfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaard96ff162018-02-18 22:13:29 +01001621{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001622 rettv->vval.v_number = assert_equalfile(argvars);
Bram Moolenaard96ff162018-02-18 22:13:29 +01001623}
1624
1625/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001626 * "assert_notequal(expected, actual[, msg])" function
1627 */
1628 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001629f_assert_notequal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001630{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001631 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001632}
1633
1634/*
1635 * "assert_exception(string[, msg])" function
1636 */
1637 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001638f_assert_exception(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001639{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001640 rettv->vval.v_number = assert_exception(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001641}
1642
1643/*
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02001644 * "assert_fails(cmd [, error[, msg]])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001645 */
1646 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001647f_assert_fails(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001648{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001649 rettv->vval.v_number = assert_fails(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001650}
1651
1652/*
1653 * "assert_false(actual[, msg])" function
1654 */
1655 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001656f_assert_false(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001657{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001658 rettv->vval.v_number = assert_bool(argvars, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001659}
1660
1661/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001662 * "assert_inrange(lower, upper[, msg])" function
1663 */
1664 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001665f_assert_inrange(typval_T *argvars, typval_T *rettv)
Bram Moolenaar61c04492016-07-23 15:35:35 +02001666{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001667 rettv->vval.v_number = assert_inrange(argvars);
Bram Moolenaar61c04492016-07-23 15:35:35 +02001668}
1669
1670/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001671 * "assert_match(pattern, actual[, msg])" function
1672 */
1673 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001674f_assert_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001675{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001676 rettv->vval.v_number = assert_match_common(argvars, ASSERT_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001677}
1678
1679/*
1680 * "assert_notmatch(pattern, actual[, msg])" function
1681 */
1682 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001683f_assert_notmatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001684{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001685 rettv->vval.v_number = assert_match_common(argvars, ASSERT_NOTMATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001686}
1687
1688/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001689 * "assert_report(msg)" function
1690 */
1691 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001692f_assert_report(typval_T *argvars, typval_T *rettv)
Bram Moolenaar42205552017-03-18 19:42:22 +01001693{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001694 rettv->vval.v_number = assert_report(argvars);
Bram Moolenaar42205552017-03-18 19:42:22 +01001695}
1696
1697/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001698 * "assert_true(actual[, msg])" function
1699 */
1700 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001701f_assert_true(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001702{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001703 rettv->vval.v_number = assert_bool(argvars, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001704}
1705
1706#ifdef FEAT_FLOAT
1707/*
1708 * "asin()" function
1709 */
1710 static void
1711f_asin(typval_T *argvars, typval_T *rettv)
1712{
1713 float_T f = 0.0;
1714
1715 rettv->v_type = VAR_FLOAT;
1716 if (get_float_arg(argvars, &f) == OK)
1717 rettv->vval.v_float = asin(f);
1718 else
1719 rettv->vval.v_float = 0.0;
1720}
1721
1722/*
1723 * "atan()" function
1724 */
1725 static void
1726f_atan(typval_T *argvars, typval_T *rettv)
1727{
1728 float_T f = 0.0;
1729
1730 rettv->v_type = VAR_FLOAT;
1731 if (get_float_arg(argvars, &f) == OK)
1732 rettv->vval.v_float = atan(f);
1733 else
1734 rettv->vval.v_float = 0.0;
1735}
1736
1737/*
1738 * "atan2()" function
1739 */
1740 static void
1741f_atan2(typval_T *argvars, typval_T *rettv)
1742{
1743 float_T fx = 0.0, fy = 0.0;
1744
1745 rettv->v_type = VAR_FLOAT;
1746 if (get_float_arg(argvars, &fx) == OK
1747 && get_float_arg(&argvars[1], &fy) == OK)
1748 rettv->vval.v_float = atan2(fx, fy);
1749 else
1750 rettv->vval.v_float = 0.0;
1751}
1752#endif
1753
1754/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001755 * "balloon_show()" function
1756 */
1757#ifdef FEAT_BEVAL
1758 static void
1759f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1760{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001761 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001762 {
1763 if (argvars[0].v_type == VAR_LIST
1764# ifdef FEAT_GUI
1765 && !gui.in_use
1766# endif
1767 )
1768 post_balloon(balloonEval, NULL, argvars[0].vval.v_list);
1769 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001770 post_balloon(balloonEval, tv_get_string_chk(&argvars[0]), NULL);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001771 }
1772}
1773
Bram Moolenaar669a8282017-11-19 20:13:05 +01001774# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001775 static void
1776f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1777{
1778 if (rettv_list_alloc(rettv) == OK)
1779 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001780 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001781
1782 if (msg != NULL)
1783 {
1784 pumitem_T *array;
1785 int size = split_message(msg, &array);
1786 int i;
1787
1788 /* Skip the first and last item, they are always empty. */
1789 for (i = 1; i < size - 1; ++i)
1790 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001791 while (size > 0)
1792 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001793 vim_free(array);
1794 }
1795 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001796}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001797# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001798#endif
1799
1800/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001801 * "browse(save, title, initdir, default)" function
1802 */
1803 static void
1804f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1805{
1806#ifdef FEAT_BROWSE
1807 int save;
1808 char_u *title;
1809 char_u *initdir;
1810 char_u *defname;
1811 char_u buf[NUMBUFLEN];
1812 char_u buf2[NUMBUFLEN];
1813 int error = FALSE;
1814
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001815 save = (int)tv_get_number_chk(&argvars[0], &error);
1816 title = tv_get_string_chk(&argvars[1]);
1817 initdir = tv_get_string_buf_chk(&argvars[2], buf);
1818 defname = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001819
1820 if (error || title == NULL || initdir == NULL || defname == NULL)
1821 rettv->vval.v_string = NULL;
1822 else
1823 rettv->vval.v_string =
1824 do_browse(save ? BROWSE_SAVE : 0,
1825 title, defname, NULL, initdir, NULL, curbuf);
1826#else
1827 rettv->vval.v_string = NULL;
1828#endif
1829 rettv->v_type = VAR_STRING;
1830}
1831
1832/*
1833 * "browsedir(title, initdir)" function
1834 */
1835 static void
1836f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1837{
1838#ifdef FEAT_BROWSE
1839 char_u *title;
1840 char_u *initdir;
1841 char_u buf[NUMBUFLEN];
1842
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001843 title = tv_get_string_chk(&argvars[0]);
1844 initdir = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001845
1846 if (title == NULL || initdir == NULL)
1847 rettv->vval.v_string = NULL;
1848 else
1849 rettv->vval.v_string = do_browse(BROWSE_DIR,
1850 title, NULL, NULL, initdir, NULL, curbuf);
1851#else
1852 rettv->vval.v_string = NULL;
1853#endif
1854 rettv->v_type = VAR_STRING;
1855}
1856
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001857/*
1858 * Find a buffer by number or exact name.
1859 */
1860 static buf_T *
1861find_buffer(typval_T *avar)
1862{
1863 buf_T *buf = NULL;
1864
1865 if (avar->v_type == VAR_NUMBER)
1866 buf = buflist_findnr((int)avar->vval.v_number);
1867 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1868 {
1869 buf = buflist_findname_exp(avar->vval.v_string);
1870 if (buf == NULL)
1871 {
1872 /* No full path name match, try a match with a URL or a "nofile"
1873 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001874 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001875 if (buf->b_fname != NULL
1876 && (path_with_url(buf->b_fname)
1877#ifdef FEAT_QUICKFIX
1878 || bt_nofile(buf)
1879#endif
1880 )
1881 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1882 break;
1883 }
1884 }
1885 return buf;
1886}
1887
1888/*
1889 * "bufexists(expr)" function
1890 */
1891 static void
1892f_bufexists(typval_T *argvars, typval_T *rettv)
1893{
1894 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1895}
1896
1897/*
1898 * "buflisted(expr)" function
1899 */
1900 static void
1901f_buflisted(typval_T *argvars, typval_T *rettv)
1902{
1903 buf_T *buf;
1904
1905 buf = find_buffer(&argvars[0]);
1906 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1907}
1908
1909/*
1910 * "bufloaded(expr)" function
1911 */
1912 static void
1913f_bufloaded(typval_T *argvars, typval_T *rettv)
1914{
1915 buf_T *buf;
1916
1917 buf = find_buffer(&argvars[0]);
1918 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1919}
1920
1921 buf_T *
1922buflist_find_by_name(char_u *name, int curtab_only)
1923{
1924 int save_magic;
1925 char_u *save_cpo;
1926 buf_T *buf;
1927
1928 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1929 save_magic = p_magic;
1930 p_magic = TRUE;
1931 save_cpo = p_cpo;
1932 p_cpo = (char_u *)"";
1933
1934 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1935 TRUE, FALSE, curtab_only));
1936
1937 p_magic = save_magic;
1938 p_cpo = save_cpo;
1939 return buf;
1940}
1941
1942/*
1943 * Get buffer by number or pattern.
1944 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001945 buf_T *
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001946tv_get_buf(typval_T *tv, int curtab_only)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001947{
1948 char_u *name = tv->vval.v_string;
1949 buf_T *buf;
1950
1951 if (tv->v_type == VAR_NUMBER)
1952 return buflist_findnr((int)tv->vval.v_number);
1953 if (tv->v_type != VAR_STRING)
1954 return NULL;
1955 if (name == NULL || *name == NUL)
1956 return curbuf;
1957 if (name[0] == '$' && name[1] == NUL)
1958 return lastbuf;
1959
1960 buf = buflist_find_by_name(name, curtab_only);
1961
1962 /* If not found, try expanding the name, like done for bufexists(). */
1963 if (buf == NULL)
1964 buf = find_buffer(tv);
1965
1966 return buf;
1967}
1968
Bram Moolenaarec9d3002019-01-12 13:50:31 +01001969#ifdef FEAT_SIGNS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001970/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001971 * Get the buffer from "arg" and give an error and return NULL if it is not
1972 * valid.
1973 */
1974 static buf_T *
1975get_buf_arg(typval_T *arg)
1976{
1977 buf_T *buf;
1978
1979 ++emsg_off;
1980 buf = tv_get_buf(arg, FALSE);
1981 --emsg_off;
1982 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001983 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001984 return buf;
1985}
Bram Moolenaarec9d3002019-01-12 13:50:31 +01001986#endif
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001987
1988/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001989 * "bufname(expr)" function
1990 */
1991 static void
1992f_bufname(typval_T *argvars, typval_T *rettv)
1993{
1994 buf_T *buf;
1995
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001996 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001997 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001998 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001999 rettv->v_type = VAR_STRING;
2000 if (buf != NULL && buf->b_fname != NULL)
2001 rettv->vval.v_string = vim_strsave(buf->b_fname);
2002 else
2003 rettv->vval.v_string = NULL;
2004 --emsg_off;
2005}
2006
2007/*
2008 * "bufnr(expr)" function
2009 */
2010 static void
2011f_bufnr(typval_T *argvars, typval_T *rettv)
2012{
2013 buf_T *buf;
2014 int error = FALSE;
2015 char_u *name;
2016
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002017 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002018 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002019 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002020 --emsg_off;
2021
2022 /* If the buffer isn't found and the second argument is not zero create a
2023 * new buffer. */
2024 if (buf == NULL
2025 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002026 && tv_get_number_chk(&argvars[1], &error) != 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002027 && !error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002028 && (name = tv_get_string_chk(&argvars[0])) != NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002029 && !error)
2030 buf = buflist_new(name, NULL, (linenr_T)1, 0);
2031
2032 if (buf != NULL)
2033 rettv->vval.v_number = buf->b_fnum;
2034 else
2035 rettv->vval.v_number = -1;
2036}
2037
2038 static void
2039buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
2040{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002041 win_T *wp;
2042 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002043 buf_T *buf;
2044
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002045 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002046 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002047 buf = tv_get_buf(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02002048 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002049 {
2050 ++winnr;
2051 if (wp->w_buffer == buf)
2052 break;
2053 }
2054 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002055 --emsg_off;
2056}
2057
2058/*
2059 * "bufwinid(nr)" function
2060 */
2061 static void
2062f_bufwinid(typval_T *argvars, typval_T *rettv)
2063{
2064 buf_win_common(argvars, rettv, FALSE);
2065}
2066
2067/*
2068 * "bufwinnr(nr)" function
2069 */
2070 static void
2071f_bufwinnr(typval_T *argvars, typval_T *rettv)
2072{
2073 buf_win_common(argvars, rettv, TRUE);
2074}
2075
2076/*
2077 * "byte2line(byte)" function
2078 */
2079 static void
2080f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2081{
2082#ifndef FEAT_BYTEOFF
2083 rettv->vval.v_number = -1;
2084#else
2085 long boff = 0;
2086
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002087 boff = tv_get_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002088 if (boff < 0)
2089 rettv->vval.v_number = -1;
2090 else
2091 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2092 (linenr_T)0, &boff);
2093#endif
2094}
2095
2096 static void
2097byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2098{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002099 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002100 char_u *str;
2101 varnumber_T idx;
2102
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002103 str = tv_get_string_chk(&argvars[0]);
2104 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002105 rettv->vval.v_number = -1;
2106 if (str == NULL || idx < 0)
2107 return;
2108
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002109 t = str;
2110 for ( ; idx > 0; idx--)
2111 {
2112 if (*t == NUL) /* EOL reached */
2113 return;
2114 if (enc_utf8 && comp)
2115 t += utf_ptr2len(t);
2116 else
2117 t += (*mb_ptr2len)(t);
2118 }
2119 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002120}
2121
2122/*
2123 * "byteidx()" function
2124 */
2125 static void
2126f_byteidx(typval_T *argvars, typval_T *rettv)
2127{
2128 byteidx(argvars, rettv, FALSE);
2129}
2130
2131/*
2132 * "byteidxcomp()" function
2133 */
2134 static void
2135f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2136{
2137 byteidx(argvars, rettv, TRUE);
2138}
2139
2140/*
2141 * "call(func, arglist [, dict])" function
2142 */
2143 static void
2144f_call(typval_T *argvars, typval_T *rettv)
2145{
2146 char_u *func;
2147 partial_T *partial = NULL;
2148 dict_T *selfdict = NULL;
2149
2150 if (argvars[1].v_type != VAR_LIST)
2151 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002152 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002153 return;
2154 }
2155 if (argvars[1].vval.v_list == NULL)
2156 return;
2157
2158 if (argvars[0].v_type == VAR_FUNC)
2159 func = argvars[0].vval.v_string;
2160 else if (argvars[0].v_type == VAR_PARTIAL)
2161 {
2162 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002163 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002164 }
2165 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002166 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002167 if (*func == NUL)
2168 return; /* type error or empty name */
2169
2170 if (argvars[2].v_type != VAR_UNKNOWN)
2171 {
2172 if (argvars[2].v_type != VAR_DICT)
2173 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002174 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002175 return;
2176 }
2177 selfdict = argvars[2].vval.v_dict;
2178 }
2179
2180 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2181}
2182
2183#ifdef FEAT_FLOAT
2184/*
2185 * "ceil({float})" function
2186 */
2187 static void
2188f_ceil(typval_T *argvars, typval_T *rettv)
2189{
2190 float_T f = 0.0;
2191
2192 rettv->v_type = VAR_FLOAT;
2193 if (get_float_arg(argvars, &f) == OK)
2194 rettv->vval.v_float = ceil(f);
2195 else
2196 rettv->vval.v_float = 0.0;
2197}
2198#endif
2199
2200#ifdef FEAT_JOB_CHANNEL
2201/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002202 * "ch_canread()" function
2203 */
2204 static void
2205f_ch_canread(typval_T *argvars, typval_T *rettv)
2206{
Bram Moolenaar958dc692016-12-01 15:34:12 +01002207 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002208
2209 rettv->vval.v_number = 0;
2210 if (channel != NULL)
2211 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
2212 || channel_has_readahead(channel, PART_OUT)
2213 || channel_has_readahead(channel, PART_ERR);
2214}
2215
2216/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002217 * "ch_close()" function
2218 */
2219 static void
2220f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
2221{
2222 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2223
2224 if (channel != NULL)
2225 {
2226 channel_close(channel, FALSE);
2227 channel_clear(channel);
2228 }
2229}
2230
2231/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002232 * "ch_close()" function
2233 */
2234 static void
2235f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
2236{
2237 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2238
2239 if (channel != NULL)
2240 channel_close_in(channel);
2241}
2242
2243/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002244 * "ch_getbufnr()" function
2245 */
2246 static void
2247f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
2248{
2249 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2250
2251 rettv->vval.v_number = -1;
2252 if (channel != NULL)
2253 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002254 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002255 int part;
2256
2257 if (STRCMP(what, "err") == 0)
2258 part = PART_ERR;
2259 else if (STRCMP(what, "out") == 0)
2260 part = PART_OUT;
2261 else if (STRCMP(what, "in") == 0)
2262 part = PART_IN;
2263 else
2264 part = PART_SOCK;
2265 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
2266 rettv->vval.v_number =
2267 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
2268 }
2269}
2270
2271/*
2272 * "ch_getjob()" function
2273 */
2274 static void
2275f_ch_getjob(typval_T *argvars, typval_T *rettv)
2276{
2277 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2278
2279 if (channel != NULL)
2280 {
2281 rettv->v_type = VAR_JOB;
2282 rettv->vval.v_job = channel->ch_job;
2283 if (channel->ch_job != NULL)
2284 ++channel->ch_job->jv_refcount;
2285 }
2286}
2287
2288/*
2289 * "ch_info()" function
2290 */
2291 static void
2292f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
2293{
2294 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2295
2296 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
2297 channel_info(channel, rettv->vval.v_dict);
2298}
2299
2300/*
2301 * "ch_log()" function
2302 */
2303 static void
2304f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
2305{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002306 char_u *msg = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002307 channel_T *channel = NULL;
2308
2309 if (argvars[1].v_type != VAR_UNKNOWN)
2310 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
2311
Bram Moolenaard5359b22018-04-05 22:44:39 +02002312 ch_log(channel, "%s", msg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002313}
2314
2315/*
2316 * "ch_logfile()" function
2317 */
2318 static void
2319f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
2320{
2321 char_u *fname;
2322 char_u *opt = (char_u *)"";
2323 char_u buf[NUMBUFLEN];
2324
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02002325 /* Don't open a file in restricted mode. */
2326 if (check_restricted() || check_secure())
2327 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002328 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002329 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002330 opt = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002331 ch_logfile(fname, opt);
2332}
2333
2334/*
2335 * "ch_open()" function
2336 */
2337 static void
2338f_ch_open(typval_T *argvars, typval_T *rettv)
2339{
2340 rettv->v_type = VAR_CHANNEL;
2341 if (check_restricted() || check_secure())
2342 return;
2343 rettv->vval.v_channel = channel_open_func(argvars);
2344}
2345
2346/*
2347 * "ch_read()" function
2348 */
2349 static void
2350f_ch_read(typval_T *argvars, typval_T *rettv)
2351{
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002352 common_channel_read(argvars, rettv, FALSE, FALSE);
2353}
2354
2355/*
2356 * "ch_readblob()" function
2357 */
2358 static void
2359f_ch_readblob(typval_T *argvars, typval_T *rettv)
2360{
2361 common_channel_read(argvars, rettv, TRUE, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002362}
2363
2364/*
2365 * "ch_readraw()" function
2366 */
2367 static void
2368f_ch_readraw(typval_T *argvars, typval_T *rettv)
2369{
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002370 common_channel_read(argvars, rettv, TRUE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002371}
2372
2373/*
2374 * "ch_evalexpr()" function
2375 */
2376 static void
2377f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2378{
2379 ch_expr_common(argvars, rettv, TRUE);
2380}
2381
2382/*
2383 * "ch_sendexpr()" function
2384 */
2385 static void
2386f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2387{
2388 ch_expr_common(argvars, rettv, FALSE);
2389}
2390
2391/*
2392 * "ch_evalraw()" function
2393 */
2394 static void
2395f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2396{
2397 ch_raw_common(argvars, rettv, TRUE);
2398}
2399
2400/*
2401 * "ch_sendraw()" function
2402 */
2403 static void
2404f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2405{
2406 ch_raw_common(argvars, rettv, FALSE);
2407}
2408
2409/*
2410 * "ch_setoptions()" function
2411 */
2412 static void
2413f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2414{
2415 channel_T *channel;
2416 jobopt_T opt;
2417
2418 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2419 if (channel == NULL)
2420 return;
2421 clear_job_options(&opt);
2422 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002423 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002424 channel_set_options(channel, &opt);
2425 free_job_options(&opt);
2426}
2427
2428/*
2429 * "ch_status()" function
2430 */
2431 static void
2432f_ch_status(typval_T *argvars, typval_T *rettv)
2433{
2434 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002435 jobopt_T opt;
2436 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002437
2438 /* return an empty string by default */
2439 rettv->v_type = VAR_STRING;
2440 rettv->vval.v_string = NULL;
2441
2442 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002443
2444 if (argvars[1].v_type != VAR_UNKNOWN)
2445 {
2446 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002447 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002448 && (opt.jo_set & JO_PART))
2449 part = opt.jo_part;
2450 }
2451
2452 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002453}
2454#endif
2455
2456/*
2457 * "changenr()" function
2458 */
2459 static void
2460f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2461{
2462 rettv->vval.v_number = curbuf->b_u_seq_cur;
2463}
2464
2465/*
2466 * "char2nr(string)" function
2467 */
2468 static void
2469f_char2nr(typval_T *argvars, typval_T *rettv)
2470{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002471 if (has_mbyte)
2472 {
2473 int utf8 = 0;
2474
2475 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002476 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002477
2478 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002479 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002480 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002481 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002482 }
2483 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002484 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002485}
2486
2487/*
2488 * "cindent(lnum)" function
2489 */
2490 static void
2491f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2492{
2493#ifdef FEAT_CINDENT
2494 pos_T pos;
2495 linenr_T lnum;
2496
2497 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002498 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002499 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2500 {
2501 curwin->w_cursor.lnum = lnum;
2502 rettv->vval.v_number = get_c_indent();
2503 curwin->w_cursor = pos;
2504 }
2505 else
2506#endif
2507 rettv->vval.v_number = -1;
2508}
2509
Bram Moolenaaraff74912019-03-30 18:11:49 +01002510 static win_T *
2511get_optional_window(typval_T *argvars, int idx)
2512{
2513 win_T *win = curwin;
2514
2515 if (argvars[idx].v_type != VAR_UNKNOWN)
2516 {
2517 win = find_win_by_nr_or_id(&argvars[idx]);
2518 if (win == NULL)
2519 {
2520 emsg(_(e_invalwindow));
2521 return NULL;
2522 }
2523 }
2524 return win;
2525}
2526
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002527/*
2528 * "clearmatches()" function
2529 */
2530 static void
2531f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2532{
2533#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01002534 win_T *win = get_optional_window(argvars, 0);
2535
2536 if (win != NULL)
2537 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002538#endif
2539}
2540
2541/*
2542 * "col(string)" function
2543 */
2544 static void
2545f_col(typval_T *argvars, typval_T *rettv)
2546{
2547 colnr_T col = 0;
2548 pos_T *fp;
2549 int fnum = curbuf->b_fnum;
2550
2551 fp = var2fpos(&argvars[0], FALSE, &fnum);
2552 if (fp != NULL && fnum == curbuf->b_fnum)
2553 {
2554 if (fp->col == MAXCOL)
2555 {
2556 /* '> can be MAXCOL, get the length of the line then */
2557 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2558 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2559 else
2560 col = MAXCOL;
2561 }
2562 else
2563 {
2564 col = fp->col + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002565 /* col(".") when the cursor is on the NUL at the end of the line
2566 * because of "coladd" can be seen as an extra column. */
2567 if (virtual_active() && fp == &curwin->w_cursor)
2568 {
2569 char_u *p = ml_get_cursor();
2570
2571 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2572 curwin->w_virtcol - curwin->w_cursor.coladd))
2573 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002574 int l;
2575
2576 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2577 col += l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002578 }
2579 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002580 }
2581 }
2582 rettv->vval.v_number = col;
2583}
2584
2585#if defined(FEAT_INS_EXPAND)
2586/*
2587 * "complete()" function
2588 */
2589 static void
2590f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2591{
2592 int startcol;
2593
2594 if ((State & INSERT) == 0)
2595 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002596 emsg(_("E785: complete() can only be used in Insert mode"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002597 return;
2598 }
2599
2600 /* Check for undo allowed here, because if something was already inserted
2601 * the line was already saved for undo and this check isn't done. */
2602 if (!undo_allowed())
2603 return;
2604
2605 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2606 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002607 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002608 return;
2609 }
2610
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002611 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002612 if (startcol <= 0)
2613 return;
2614
2615 set_completion(startcol - 1, argvars[1].vval.v_list);
2616}
2617
2618/*
2619 * "complete_add()" function
2620 */
2621 static void
2622f_complete_add(typval_T *argvars, typval_T *rettv)
2623{
2624 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2625}
2626
2627/*
2628 * "complete_check()" function
2629 */
2630 static void
2631f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2632{
2633 int saved = RedrawingDisabled;
2634
2635 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002636 ins_compl_check_keys(0, TRUE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002637 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002638 RedrawingDisabled = saved;
2639}
Bram Moolenaarfd133322019-03-29 12:20:27 +01002640
2641/*
2642 * "complete_info()" function
2643 */
2644 static void
2645f_complete_info(typval_T *argvars, typval_T *rettv)
2646{
2647 list_T *what_list = NULL;
2648
2649 if (rettv_dict_alloc(rettv) != OK)
2650 return;
2651
2652 if (argvars[0].v_type != VAR_UNKNOWN)
2653 {
2654 if (argvars[0].v_type != VAR_LIST)
2655 {
2656 emsg(_(e_listreq));
2657 return;
2658 }
2659 what_list = argvars[0].vval.v_list;
2660 }
2661 get_complete_info(what_list, rettv->vval.v_dict);
2662}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002663#endif
2664
2665/*
2666 * "confirm(message, buttons[, default [, type]])" function
2667 */
2668 static void
2669f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2670{
2671#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2672 char_u *message;
2673 char_u *buttons = NULL;
2674 char_u buf[NUMBUFLEN];
2675 char_u buf2[NUMBUFLEN];
2676 int def = 1;
2677 int type = VIM_GENERIC;
2678 char_u *typestr;
2679 int error = FALSE;
2680
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002681 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002682 if (message == NULL)
2683 error = TRUE;
2684 if (argvars[1].v_type != VAR_UNKNOWN)
2685 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002686 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002687 if (buttons == NULL)
2688 error = TRUE;
2689 if (argvars[2].v_type != VAR_UNKNOWN)
2690 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002691 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002692 if (argvars[3].v_type != VAR_UNKNOWN)
2693 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002694 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002695 if (typestr == NULL)
2696 error = TRUE;
2697 else
2698 {
2699 switch (TOUPPER_ASC(*typestr))
2700 {
2701 case 'E': type = VIM_ERROR; break;
2702 case 'Q': type = VIM_QUESTION; break;
2703 case 'I': type = VIM_INFO; break;
2704 case 'W': type = VIM_WARNING; break;
2705 case 'G': type = VIM_GENERIC; break;
2706 }
2707 }
2708 }
2709 }
2710 }
2711
2712 if (buttons == NULL || *buttons == NUL)
2713 buttons = (char_u *)_("&Ok");
2714
2715 if (!error)
2716 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2717 def, NULL, FALSE);
2718#endif
2719}
2720
2721/*
2722 * "copy()" function
2723 */
2724 static void
2725f_copy(typval_T *argvars, typval_T *rettv)
2726{
2727 item_copy(&argvars[0], rettv, FALSE, 0);
2728}
2729
2730#ifdef FEAT_FLOAT
2731/*
2732 * "cos()" function
2733 */
2734 static void
2735f_cos(typval_T *argvars, typval_T *rettv)
2736{
2737 float_T f = 0.0;
2738
2739 rettv->v_type = VAR_FLOAT;
2740 if (get_float_arg(argvars, &f) == OK)
2741 rettv->vval.v_float = cos(f);
2742 else
2743 rettv->vval.v_float = 0.0;
2744}
2745
2746/*
2747 * "cosh()" function
2748 */
2749 static void
2750f_cosh(typval_T *argvars, typval_T *rettv)
2751{
2752 float_T f = 0.0;
2753
2754 rettv->v_type = VAR_FLOAT;
2755 if (get_float_arg(argvars, &f) == OK)
2756 rettv->vval.v_float = cosh(f);
2757 else
2758 rettv->vval.v_float = 0.0;
2759}
2760#endif
2761
2762/*
2763 * "count()" function
2764 */
2765 static void
2766f_count(typval_T *argvars, typval_T *rettv)
2767{
2768 long n = 0;
2769 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002770 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002771
Bram Moolenaar9966b212017-07-28 16:46:57 +02002772 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002773 ic = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002774
2775 if (argvars[0].v_type == VAR_STRING)
2776 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002777 char_u *expr = tv_get_string_chk(&argvars[1]);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002778 char_u *p = argvars[0].vval.v_string;
2779 char_u *next;
2780
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002781 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002782 {
2783 if (ic)
2784 {
2785 size_t len = STRLEN(expr);
2786
2787 while (*p != NUL)
2788 {
2789 if (MB_STRNICMP(p, expr, len) == 0)
2790 {
2791 ++n;
2792 p += len;
2793 }
2794 else
2795 MB_PTR_ADV(p);
2796 }
2797 }
2798 else
2799 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2800 != NULL)
2801 {
2802 ++n;
2803 p = next + STRLEN(expr);
2804 }
2805 }
2806
2807 }
2808 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002809 {
2810 listitem_T *li;
2811 list_T *l;
2812 long idx;
2813
2814 if ((l = argvars[0].vval.v_list) != NULL)
2815 {
2816 li = l->lv_first;
2817 if (argvars[2].v_type != VAR_UNKNOWN)
2818 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002819 if (argvars[3].v_type != VAR_UNKNOWN)
2820 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002821 idx = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002822 if (!error)
2823 {
2824 li = list_find(l, idx);
2825 if (li == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002826 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002827 }
2828 }
2829 if (error)
2830 li = NULL;
2831 }
2832
2833 for ( ; li != NULL; li = li->li_next)
2834 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2835 ++n;
2836 }
2837 }
2838 else if (argvars[0].v_type == VAR_DICT)
2839 {
2840 int todo;
2841 dict_T *d;
2842 hashitem_T *hi;
2843
2844 if ((d = argvars[0].vval.v_dict) != NULL)
2845 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002846 if (argvars[2].v_type != VAR_UNKNOWN)
2847 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002848 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002849 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002850 }
2851
2852 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2853 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2854 {
2855 if (!HASHITEM_EMPTY(hi))
2856 {
2857 --todo;
2858 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2859 ++n;
2860 }
2861 }
2862 }
2863 }
2864 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002865 semsg(_(e_listdictarg), "count()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002866 rettv->vval.v_number = n;
2867}
2868
2869/*
2870 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2871 *
2872 * Checks the existence of a cscope connection.
2873 */
2874 static void
2875f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2876{
2877#ifdef FEAT_CSCOPE
2878 int num = 0;
2879 char_u *dbpath = NULL;
2880 char_u *prepend = NULL;
2881 char_u buf[NUMBUFLEN];
2882
2883 if (argvars[0].v_type != VAR_UNKNOWN
2884 && argvars[1].v_type != VAR_UNKNOWN)
2885 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002886 num = (int)tv_get_number(&argvars[0]);
2887 dbpath = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002888 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002889 prepend = tv_get_string_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002890 }
2891
2892 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2893#endif
2894}
2895
2896/*
2897 * "cursor(lnum, col)" function, or
2898 * "cursor(list)"
2899 *
2900 * Moves the cursor to the specified line and column.
2901 * Returns 0 when the position could be set, -1 otherwise.
2902 */
2903 static void
2904f_cursor(typval_T *argvars, typval_T *rettv)
2905{
2906 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002907 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002908 int set_curswant = TRUE;
2909
2910 rettv->vval.v_number = -1;
2911 if (argvars[1].v_type == VAR_UNKNOWN)
2912 {
2913 pos_T pos;
2914 colnr_T curswant = -1;
2915
2916 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2917 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002918 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002919 return;
2920 }
2921 line = pos.lnum;
2922 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002923 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002924 if (curswant >= 0)
2925 {
2926 curwin->w_curswant = curswant - 1;
2927 set_curswant = FALSE;
2928 }
2929 }
2930 else
2931 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002932 line = tv_get_lnum(argvars);
2933 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002934 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002935 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002936 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002937 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002938 return; /* type error; errmsg already given */
2939 if (line > 0)
2940 curwin->w_cursor.lnum = line;
2941 if (col > 0)
2942 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002943 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002944
2945 /* Make sure the cursor is in a valid position. */
2946 check_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002947 /* Correct cursor for multi-byte character. */
2948 if (has_mbyte)
2949 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002950
2951 curwin->w_set_curswant = set_curswant;
2952 rettv->vval.v_number = 0;
2953}
2954
Bram Moolenaar4f974752019-02-17 17:44:42 +01002955#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002956/*
2957 * "debugbreak()" function
2958 */
2959 static void
2960f_debugbreak(typval_T *argvars, typval_T *rettv)
2961{
2962 int pid;
2963
2964 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002965 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002966 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002967 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002968 else
2969 {
2970 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2971
2972 if (hProcess != NULL)
2973 {
2974 DebugBreakProcess(hProcess);
2975 CloseHandle(hProcess);
2976 rettv->vval.v_number = OK;
2977 }
2978 }
2979}
2980#endif
2981
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002982/*
2983 * "deepcopy()" function
2984 */
2985 static void
2986f_deepcopy(typval_T *argvars, typval_T *rettv)
2987{
2988 int noref = 0;
2989 int copyID;
2990
2991 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002992 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002993 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002994 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002995 else
2996 {
2997 copyID = get_copyID();
2998 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2999 }
3000}
3001
3002/*
3003 * "delete()" function
3004 */
3005 static void
3006f_delete(typval_T *argvars, typval_T *rettv)
3007{
3008 char_u nbuf[NUMBUFLEN];
3009 char_u *name;
3010 char_u *flags;
3011
3012 rettv->vval.v_number = -1;
3013 if (check_restricted() || check_secure())
3014 return;
3015
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003016 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003017 if (name == NULL || *name == NUL)
3018 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003019 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003020 return;
3021 }
3022
3023 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003024 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003025 else
3026 flags = (char_u *)"";
3027
3028 if (*flags == NUL)
3029 /* delete a file */
3030 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
3031 else if (STRCMP(flags, "d") == 0)
3032 /* delete an empty directory */
3033 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
3034 else if (STRCMP(flags, "rf") == 0)
3035 /* delete a directory recursively */
3036 rettv->vval.v_number = delete_recursive(name);
3037 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003038 semsg(_(e_invexpr2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003039}
3040
3041/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02003042 * "deletebufline()" function
3043 */
3044 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02003045f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02003046{
3047 buf_T *buf;
3048 linenr_T first, last;
3049 linenr_T lnum;
3050 long count;
3051 int is_curbuf;
3052 buf_T *curbuf_save = NULL;
3053 win_T *curwin_save = NULL;
3054 tabpage_T *tp;
3055 win_T *wp;
3056
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01003057 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003058 if (buf == NULL)
3059 {
3060 rettv->vval.v_number = 1; /* FAIL */
3061 return;
3062 }
3063 is_curbuf = buf == curbuf;
3064
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003065 first = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003066 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003067 last = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003068 else
3069 last = first;
3070
3071 if (buf->b_ml.ml_mfp == NULL || first < 1
3072 || first > buf->b_ml.ml_line_count || last < first)
3073 {
3074 rettv->vval.v_number = 1; /* FAIL */
3075 return;
3076 }
3077
3078 if (!is_curbuf)
3079 {
3080 curbuf_save = curbuf;
3081 curwin_save = curwin;
3082 curbuf = buf;
3083 find_win_for_curbuf();
3084 }
3085 if (last > curbuf->b_ml.ml_line_count)
3086 last = curbuf->b_ml.ml_line_count;
3087 count = last - first + 1;
3088
3089 // When coming here from Insert mode, sync undo, so that this can be
3090 // undone separately from what was previously inserted.
3091 if (u_sync_once == 2)
3092 {
3093 u_sync_once = 1; // notify that u_sync() was called
3094 u_sync(TRUE);
3095 }
3096
3097 if (u_save(first - 1, last + 1) == FAIL)
3098 {
3099 rettv->vval.v_number = 1; /* FAIL */
3100 return;
3101 }
3102
3103 for (lnum = first; lnum <= last; ++lnum)
3104 ml_delete(first, TRUE);
3105
3106 FOR_ALL_TAB_WINDOWS(tp, wp)
3107 if (wp->w_buffer == buf)
3108 {
3109 if (wp->w_cursor.lnum > last)
3110 wp->w_cursor.lnum -= count;
3111 else if (wp->w_cursor.lnum> first)
3112 wp->w_cursor.lnum = first;
3113 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
3114 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
3115 }
3116 check_cursor_col();
3117 deleted_lines_mark(first, count);
3118
3119 if (!is_curbuf)
3120 {
3121 curbuf = curbuf_save;
3122 curwin = curwin_save;
3123 }
3124}
3125
3126/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003127 * "did_filetype()" function
3128 */
3129 static void
3130f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3131{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003132 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003133}
3134
3135/*
3136 * "diff_filler()" function
3137 */
3138 static void
3139f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3140{
3141#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003142 rettv->vval.v_number = diff_check_fill(curwin, tv_get_lnum(argvars));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003143#endif
3144}
3145
3146/*
3147 * "diff_hlID()" function
3148 */
3149 static void
3150f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3151{
3152#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003153 linenr_T lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003154 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01003155 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003156 static int fnum = 0;
3157 static int change_start = 0;
3158 static int change_end = 0;
3159 static hlf_T hlID = (hlf_T)0;
3160 int filler_lines;
3161 int col;
3162
3163 if (lnum < 0) /* ignore type error in {lnum} arg */
3164 lnum = 0;
3165 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003166 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003167 || fnum != curbuf->b_fnum)
3168 {
3169 /* New line, buffer, change: need to get the values. */
3170 filler_lines = diff_check(curwin, lnum);
3171 if (filler_lines < 0)
3172 {
3173 if (filler_lines == -1)
3174 {
3175 change_start = MAXCOL;
3176 change_end = -1;
3177 if (diff_find_change(curwin, lnum, &change_start, &change_end))
3178 hlID = HLF_ADD; /* added line */
3179 else
3180 hlID = HLF_CHD; /* changed line */
3181 }
3182 else
3183 hlID = HLF_ADD; /* added line */
3184 }
3185 else
3186 hlID = (hlf_T)0;
3187 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003188 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003189 fnum = curbuf->b_fnum;
3190 }
3191
3192 if (hlID == HLF_CHD || hlID == HLF_TXD)
3193 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003194 col = tv_get_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003195 if (col >= change_start && col <= change_end)
3196 hlID = HLF_TXD; /* changed text */
3197 else
3198 hlID = HLF_CHD; /* changed line */
3199 }
3200 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
3201#endif
3202}
3203
3204/*
3205 * "empty({expr})" function
3206 */
3207 static void
3208f_empty(typval_T *argvars, typval_T *rettv)
3209{
3210 int n = FALSE;
3211
3212 switch (argvars[0].v_type)
3213 {
3214 case VAR_STRING:
3215 case VAR_FUNC:
3216 n = argvars[0].vval.v_string == NULL
3217 || *argvars[0].vval.v_string == NUL;
3218 break;
3219 case VAR_PARTIAL:
3220 n = FALSE;
3221 break;
3222 case VAR_NUMBER:
3223 n = argvars[0].vval.v_number == 0;
3224 break;
3225 case VAR_FLOAT:
3226#ifdef FEAT_FLOAT
3227 n = argvars[0].vval.v_float == 0.0;
3228 break;
3229#endif
3230 case VAR_LIST:
3231 n = argvars[0].vval.v_list == NULL
3232 || argvars[0].vval.v_list->lv_first == NULL;
3233 break;
3234 case VAR_DICT:
3235 n = argvars[0].vval.v_dict == NULL
3236 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
3237 break;
3238 case VAR_SPECIAL:
3239 n = argvars[0].vval.v_number != VVAL_TRUE;
3240 break;
3241
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003242 case VAR_BLOB:
3243 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003244 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
3245 break;
3246
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003247 case VAR_JOB:
3248#ifdef FEAT_JOB_CHANNEL
3249 n = argvars[0].vval.v_job == NULL
3250 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
3251 break;
3252#endif
3253 case VAR_CHANNEL:
3254#ifdef FEAT_JOB_CHANNEL
3255 n = argvars[0].vval.v_channel == NULL
3256 || !channel_is_open(argvars[0].vval.v_channel);
3257 break;
3258#endif
3259 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01003260 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003261 n = TRUE;
3262 break;
3263 }
3264
3265 rettv->vval.v_number = n;
3266}
3267
3268/*
3269 * "escape({string}, {chars})" function
3270 */
3271 static void
3272f_escape(typval_T *argvars, typval_T *rettv)
3273{
3274 char_u buf[NUMBUFLEN];
3275
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003276 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
3277 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003278 rettv->v_type = VAR_STRING;
3279}
3280
3281/*
3282 * "eval()" function
3283 */
3284 static void
3285f_eval(typval_T *argvars, typval_T *rettv)
3286{
3287 char_u *s, *p;
3288
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003289 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003290 if (s != NULL)
3291 s = skipwhite(s);
3292
3293 p = s;
3294 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3295 {
3296 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003297 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003298 need_clr_eos = FALSE;
3299 rettv->v_type = VAR_NUMBER;
3300 rettv->vval.v_number = 0;
3301 }
3302 else if (*s != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003303 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003304}
3305
3306/*
3307 * "eventhandler()" function
3308 */
3309 static void
3310f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3311{
3312 rettv->vval.v_number = vgetc_busy;
3313}
3314
3315/*
3316 * "executable()" function
3317 */
3318 static void
3319f_executable(typval_T *argvars, typval_T *rettv)
3320{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003321 char_u *name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003322
3323 /* Check in $PATH and also check directly if there is a directory name. */
3324 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
3325 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
3326}
3327
3328static garray_T redir_execute_ga;
3329
3330/*
3331 * Append "value[value_len]" to the execute() output.
3332 */
3333 void
3334execute_redir_str(char_u *value, int value_len)
3335{
3336 int len;
3337
3338 if (value_len == -1)
3339 len = (int)STRLEN(value); /* Append the entire string */
3340 else
3341 len = value_len; /* Append only "value_len" characters */
3342 if (ga_grow(&redir_execute_ga, len) == OK)
3343 {
3344 mch_memmove((char *)redir_execute_ga.ga_data
3345 + redir_execute_ga.ga_len, value, len);
3346 redir_execute_ga.ga_len += len;
3347 }
3348}
3349
3350/*
3351 * Get next line from a list.
3352 * Called by do_cmdline() to get the next line.
3353 * Returns allocated string, or NULL for end of function.
3354 */
3355
3356 static char_u *
3357get_list_line(
3358 int c UNUSED,
3359 void *cookie,
3360 int indent UNUSED)
3361{
3362 listitem_T **p = (listitem_T **)cookie;
3363 listitem_T *item = *p;
3364 char_u buf[NUMBUFLEN];
3365 char_u *s;
3366
3367 if (item == NULL)
3368 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003369 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003370 *p = item->li_next;
3371 return s == NULL ? NULL : vim_strsave(s);
3372}
3373
3374/*
3375 * "execute()" function
3376 */
3377 static void
3378f_execute(typval_T *argvars, typval_T *rettv)
3379{
3380 char_u *cmd = NULL;
3381 list_T *list = NULL;
3382 int save_msg_silent = msg_silent;
3383 int save_emsg_silent = emsg_silent;
3384 int save_emsg_noredir = emsg_noredir;
3385 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003386 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003387 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003388 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003389 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003390
3391 rettv->vval.v_string = NULL;
3392 rettv->v_type = VAR_STRING;
3393
3394 if (argvars[0].v_type == VAR_LIST)
3395 {
3396 list = argvars[0].vval.v_list;
3397 if (list == NULL || list->lv_first == NULL)
3398 /* empty list, no commands, empty output */
3399 return;
3400 ++list->lv_refcount;
3401 }
3402 else
3403 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003404 cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003405 if (cmd == NULL)
3406 return;
3407 }
3408
3409 if (argvars[1].v_type != VAR_UNKNOWN)
3410 {
3411 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003412 char_u *s = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003413
3414 if (s == NULL)
3415 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003416 if (*s == NUL)
3417 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003418 if (STRNCMP(s, "silent", 6) == 0)
3419 ++msg_silent;
3420 if (STRCMP(s, "silent!") == 0)
3421 {
3422 emsg_silent = TRUE;
3423 emsg_noredir = TRUE;
3424 }
3425 }
3426 else
3427 ++msg_silent;
3428
3429 if (redir_execute)
3430 save_ga = redir_execute_ga;
3431 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3432 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003433 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003434 if (!echo_output)
3435 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003436
3437 if (cmd != NULL)
3438 do_cmdline_cmd(cmd);
3439 else
3440 {
3441 listitem_T *item = list->lv_first;
3442
3443 do_cmdline(NULL, get_list_line, (void *)&item,
3444 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3445 --list->lv_refcount;
3446 }
3447
Bram Moolenaard297f352017-01-29 20:31:21 +01003448 /* Need to append a NUL to the result. */
3449 if (ga_grow(&redir_execute_ga, 1) == OK)
3450 {
3451 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3452 rettv->vval.v_string = redir_execute_ga.ga_data;
3453 }
3454 else
3455 {
3456 ga_clear(&redir_execute_ga);
3457 rettv->vval.v_string = NULL;
3458 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003459 msg_silent = save_msg_silent;
3460 emsg_silent = save_emsg_silent;
3461 emsg_noredir = save_emsg_noredir;
3462
3463 redir_execute = save_redir_execute;
3464 if (redir_execute)
3465 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003466 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003467
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003468 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003469 if (echo_output)
3470 // When not working silently: put it in column zero. A following
3471 // "echon" will overwrite the message, unavoidably.
3472 msg_col = 0;
3473 else
3474 // When working silently: Put it back where it was, since nothing
3475 // should have been written.
3476 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003477}
3478
3479/*
3480 * "exepath()" function
3481 */
3482 static void
3483f_exepath(typval_T *argvars, typval_T *rettv)
3484{
3485 char_u *p = NULL;
3486
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003487 (void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003488 rettv->v_type = VAR_STRING;
3489 rettv->vval.v_string = p;
3490}
3491
3492/*
3493 * "exists()" function
3494 */
3495 static void
3496f_exists(typval_T *argvars, typval_T *rettv)
3497{
3498 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003499 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003500
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003501 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003502 if (*p == '$') /* environment variable */
3503 {
3504 /* first try "normal" environment variables (fast) */
3505 if (mch_getenv(p + 1) != NULL)
3506 n = TRUE;
3507 else
3508 {
3509 /* try expanding things like $VIM and ${HOME} */
3510 p = expand_env_save(p);
3511 if (p != NULL && *p != '$')
3512 n = TRUE;
3513 vim_free(p);
3514 }
3515 }
3516 else if (*p == '&' || *p == '+') /* option */
3517 {
3518 n = (get_option_tv(&p, NULL, TRUE) == OK);
3519 if (*skipwhite(p) != NUL)
3520 n = FALSE; /* trailing garbage */
3521 }
3522 else if (*p == '*') /* internal or user defined function */
3523 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003524 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003525 }
3526 else if (*p == ':')
3527 {
3528 n = cmd_exists(p + 1);
3529 }
3530 else if (*p == '#')
3531 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003532 if (p[1] == '#')
3533 n = autocmd_supported(p + 2);
3534 else
3535 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003536 }
3537 else /* internal variable */
3538 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003539 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003540 }
3541
3542 rettv->vval.v_number = n;
3543}
3544
3545#ifdef FEAT_FLOAT
3546/*
3547 * "exp()" function
3548 */
3549 static void
3550f_exp(typval_T *argvars, typval_T *rettv)
3551{
3552 float_T f = 0.0;
3553
3554 rettv->v_type = VAR_FLOAT;
3555 if (get_float_arg(argvars, &f) == OK)
3556 rettv->vval.v_float = exp(f);
3557 else
3558 rettv->vval.v_float = 0.0;
3559}
3560#endif
3561
3562/*
3563 * "expand()" function
3564 */
3565 static void
3566f_expand(typval_T *argvars, typval_T *rettv)
3567{
3568 char_u *s;
3569 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003570 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003571 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3572 expand_T xpc;
3573 int error = FALSE;
3574 char_u *result;
3575
3576 rettv->v_type = VAR_STRING;
3577 if (argvars[1].v_type != VAR_UNKNOWN
3578 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003579 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003580 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003581 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003582
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003583 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003584 if (*s == '%' || *s == '#' || *s == '<')
3585 {
3586 ++emsg_off;
3587 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3588 --emsg_off;
3589 if (rettv->v_type == VAR_LIST)
3590 {
3591 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3592 list_append_string(rettv->vval.v_list, result, -1);
3593 else
3594 vim_free(result);
3595 }
3596 else
3597 rettv->vval.v_string = result;
3598 }
3599 else
3600 {
3601 /* When the optional second argument is non-zero, don't remove matches
3602 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3603 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003604 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003605 options |= WILD_KEEP_ALL;
3606 if (!error)
3607 {
3608 ExpandInit(&xpc);
3609 xpc.xp_context = EXPAND_FILES;
3610 if (p_wic)
3611 options += WILD_ICASE;
3612 if (rettv->v_type == VAR_STRING)
3613 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3614 options, WILD_ALL);
3615 else if (rettv_list_alloc(rettv) != FAIL)
3616 {
3617 int i;
3618
3619 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3620 for (i = 0; i < xpc.xp_numfiles; i++)
3621 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3622 ExpandCleanup(&xpc);
3623 }
3624 }
3625 else
3626 rettv->vval.v_string = NULL;
3627 }
3628}
3629
3630/*
3631 * "extend(list, list [, idx])" function
3632 * "extend(dict, dict [, action])" function
3633 */
3634 static void
3635f_extend(typval_T *argvars, typval_T *rettv)
3636{
3637 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3638
3639 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3640 {
3641 list_T *l1, *l2;
3642 listitem_T *item;
3643 long before;
3644 int error = FALSE;
3645
3646 l1 = argvars[0].vval.v_list;
3647 l2 = argvars[1].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003648 if (l1 != NULL && !var_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003649 && l2 != NULL)
3650 {
3651 if (argvars[2].v_type != VAR_UNKNOWN)
3652 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003653 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003654 if (error)
3655 return; /* type error; errmsg already given */
3656
3657 if (before == l1->lv_len)
3658 item = NULL;
3659 else
3660 {
3661 item = list_find(l1, before);
3662 if (item == NULL)
3663 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003664 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003665 return;
3666 }
3667 }
3668 }
3669 else
3670 item = NULL;
3671 list_extend(l1, l2, item);
3672
3673 copy_tv(&argvars[0], rettv);
3674 }
3675 }
3676 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3677 {
3678 dict_T *d1, *d2;
3679 char_u *action;
3680 int i;
3681
3682 d1 = argvars[0].vval.v_dict;
3683 d2 = argvars[1].vval.v_dict;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003684 if (d1 != NULL && !var_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003685 && d2 != NULL)
3686 {
3687 /* Check the third argument. */
3688 if (argvars[2].v_type != VAR_UNKNOWN)
3689 {
3690 static char *(av[]) = {"keep", "force", "error"};
3691
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003692 action = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003693 if (action == NULL)
3694 return; /* type error; errmsg already given */
3695 for (i = 0; i < 3; ++i)
3696 if (STRCMP(action, av[i]) == 0)
3697 break;
3698 if (i == 3)
3699 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003700 semsg(_(e_invarg2), action);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003701 return;
3702 }
3703 }
3704 else
3705 action = (char_u *)"force";
3706
3707 dict_extend(d1, d2, action);
3708
3709 copy_tv(&argvars[0], rettv);
3710 }
3711 }
3712 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003713 semsg(_(e_listdictarg), "extend()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003714}
3715
3716/*
3717 * "feedkeys()" function
3718 */
3719 static void
3720f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3721{
3722 int remap = TRUE;
3723 int insert = FALSE;
3724 char_u *keys, *flags;
3725 char_u nbuf[NUMBUFLEN];
3726 int typed = FALSE;
3727 int execute = FALSE;
3728 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003729 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003730 char_u *keys_esc;
3731
3732 /* This is not allowed in the sandbox. If the commands would still be
3733 * executed in the sandbox it would be OK, but it probably happens later,
3734 * when "sandbox" is no longer set. */
3735 if (check_secure())
3736 return;
3737
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003738 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003739
3740 if (argvars[1].v_type != VAR_UNKNOWN)
3741 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003742 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003743 for ( ; *flags != NUL; ++flags)
3744 {
3745 switch (*flags)
3746 {
3747 case 'n': remap = FALSE; break;
3748 case 'm': remap = TRUE; break;
3749 case 't': typed = TRUE; break;
3750 case 'i': insert = TRUE; break;
3751 case 'x': execute = TRUE; break;
3752 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003753 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003754 }
3755 }
3756 }
3757
3758 if (*keys != NUL || execute)
3759 {
3760 /* Need to escape K_SPECIAL and CSI before putting the string in the
3761 * typeahead buffer. */
3762 keys_esc = vim_strsave_escape_csi(keys);
3763 if (keys_esc != NULL)
3764 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003765 if (lowlevel)
3766 {
3767#ifdef USE_INPUT_BUF
3768 add_to_input_buf(keys, (int)STRLEN(keys));
3769#else
3770 emsg(_("E980: lowlevel input not supported"));
3771#endif
3772 }
3773 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003774 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003775 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003776 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003777 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003778#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003779 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003780#endif
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003781 )
3782 typebuf_was_filled = TRUE;
3783 }
3784 vim_free(keys_esc);
3785
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003786 if (execute)
3787 {
3788 int save_msg_scroll = msg_scroll;
3789
3790 /* Avoid a 1 second delay when the keys start Insert mode. */
3791 msg_scroll = FALSE;
3792
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003793 if (!dangerous)
3794 ++ex_normal_busy;
Bram Moolenaar905dd902019-04-07 14:21:47 +02003795 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003796 if (!dangerous)
3797 --ex_normal_busy;
3798
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003799 msg_scroll |= save_msg_scroll;
3800 }
3801 }
3802 }
3803}
3804
3805/*
3806 * "filereadable()" function
3807 */
3808 static void
3809f_filereadable(typval_T *argvars, typval_T *rettv)
3810{
3811 int fd;
3812 char_u *p;
3813 int n;
3814
3815#ifndef O_NONBLOCK
3816# define O_NONBLOCK 0
3817#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003818 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003819 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3820 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3821 {
3822 n = TRUE;
3823 close(fd);
3824 }
3825 else
3826 n = FALSE;
3827
3828 rettv->vval.v_number = n;
3829}
3830
3831/*
3832 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3833 * rights to write into.
3834 */
3835 static void
3836f_filewritable(typval_T *argvars, typval_T *rettv)
3837{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003838 rettv->vval.v_number = filewritable(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003839}
3840
3841 static void
3842findfilendir(
3843 typval_T *argvars UNUSED,
3844 typval_T *rettv,
3845 int find_what UNUSED)
3846{
3847#ifdef FEAT_SEARCHPATH
3848 char_u *fname;
3849 char_u *fresult = NULL;
3850 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3851 char_u *p;
3852 char_u pathbuf[NUMBUFLEN];
3853 int count = 1;
3854 int first = TRUE;
3855 int error = FALSE;
3856#endif
3857
3858 rettv->vval.v_string = NULL;
3859 rettv->v_type = VAR_STRING;
3860
3861#ifdef FEAT_SEARCHPATH
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003862 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003863
3864 if (argvars[1].v_type != VAR_UNKNOWN)
3865 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003866 p = tv_get_string_buf_chk(&argvars[1], pathbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003867 if (p == NULL)
3868 error = TRUE;
3869 else
3870 {
3871 if (*p != NUL)
3872 path = p;
3873
3874 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003875 count = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003876 }
3877 }
3878
3879 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3880 error = TRUE;
3881
3882 if (*fname != NUL && !error)
3883 {
3884 do
3885 {
3886 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3887 vim_free(fresult);
3888 fresult = find_file_in_path_option(first ? fname : NULL,
3889 first ? (int)STRLEN(fname) : 0,
3890 0, first, path,
3891 find_what,
3892 curbuf->b_ffname,
3893 find_what == FINDFILE_DIR
3894 ? (char_u *)"" : curbuf->b_p_sua);
3895 first = FALSE;
3896
3897 if (fresult != NULL && rettv->v_type == VAR_LIST)
3898 list_append_string(rettv->vval.v_list, fresult, -1);
3899
3900 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3901 }
3902
3903 if (rettv->v_type == VAR_STRING)
3904 rettv->vval.v_string = fresult;
3905#endif
3906}
3907
3908/*
3909 * "filter()" function
3910 */
3911 static void
3912f_filter(typval_T *argvars, typval_T *rettv)
3913{
3914 filter_map(argvars, rettv, FALSE);
3915}
3916
3917/*
3918 * "finddir({fname}[, {path}[, {count}]])" function
3919 */
3920 static void
3921f_finddir(typval_T *argvars, typval_T *rettv)
3922{
3923 findfilendir(argvars, rettv, FINDFILE_DIR);
3924}
3925
3926/*
3927 * "findfile({fname}[, {path}[, {count}]])" function
3928 */
3929 static void
3930f_findfile(typval_T *argvars, typval_T *rettv)
3931{
3932 findfilendir(argvars, rettv, FINDFILE_FILE);
3933}
3934
3935#ifdef FEAT_FLOAT
3936/*
3937 * "float2nr({float})" function
3938 */
3939 static void
3940f_float2nr(typval_T *argvars, typval_T *rettv)
3941{
3942 float_T f = 0.0;
3943
3944 if (get_float_arg(argvars, &f) == OK)
3945 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003946 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003947 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003948 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003949 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003950 else
3951 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003952 }
3953}
3954
3955/*
3956 * "floor({float})" function
3957 */
3958 static void
3959f_floor(typval_T *argvars, typval_T *rettv)
3960{
3961 float_T f = 0.0;
3962
3963 rettv->v_type = VAR_FLOAT;
3964 if (get_float_arg(argvars, &f) == OK)
3965 rettv->vval.v_float = floor(f);
3966 else
3967 rettv->vval.v_float = 0.0;
3968}
3969
3970/*
3971 * "fmod()" function
3972 */
3973 static void
3974f_fmod(typval_T *argvars, typval_T *rettv)
3975{
3976 float_T fx = 0.0, fy = 0.0;
3977
3978 rettv->v_type = VAR_FLOAT;
3979 if (get_float_arg(argvars, &fx) == OK
3980 && get_float_arg(&argvars[1], &fy) == OK)
3981 rettv->vval.v_float = fmod(fx, fy);
3982 else
3983 rettv->vval.v_float = 0.0;
3984}
3985#endif
3986
3987/*
3988 * "fnameescape({string})" function
3989 */
3990 static void
3991f_fnameescape(typval_T *argvars, typval_T *rettv)
3992{
3993 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003994 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003995 rettv->v_type = VAR_STRING;
3996}
3997
3998/*
3999 * "fnamemodify({fname}, {mods})" function
4000 */
4001 static void
4002f_fnamemodify(typval_T *argvars, typval_T *rettv)
4003{
4004 char_u *fname;
4005 char_u *mods;
4006 int usedlen = 0;
4007 int len;
4008 char_u *fbuf = NULL;
4009 char_u buf[NUMBUFLEN];
4010
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004011 fname = tv_get_string_chk(&argvars[0]);
4012 mods = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004013 if (fname == NULL || mods == NULL)
4014 fname = NULL;
4015 else
4016 {
4017 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02004018 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004019 }
4020
4021 rettv->v_type = VAR_STRING;
4022 if (fname == NULL)
4023 rettv->vval.v_string = NULL;
4024 else
4025 rettv->vval.v_string = vim_strnsave(fname, len);
4026 vim_free(fbuf);
4027}
4028
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004029/*
4030 * "foldclosed()" function
4031 */
4032 static void
4033foldclosed_both(
4034 typval_T *argvars UNUSED,
4035 typval_T *rettv,
4036 int end UNUSED)
4037{
4038#ifdef FEAT_FOLDING
4039 linenr_T lnum;
4040 linenr_T first, last;
4041
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004042 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004043 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4044 {
4045 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
4046 {
4047 if (end)
4048 rettv->vval.v_number = (varnumber_T)last;
4049 else
4050 rettv->vval.v_number = (varnumber_T)first;
4051 return;
4052 }
4053 }
4054#endif
4055 rettv->vval.v_number = -1;
4056}
4057
4058/*
4059 * "foldclosed()" function
4060 */
4061 static void
4062f_foldclosed(typval_T *argvars, typval_T *rettv)
4063{
4064 foldclosed_both(argvars, rettv, FALSE);
4065}
4066
4067/*
4068 * "foldclosedend()" function
4069 */
4070 static void
4071f_foldclosedend(typval_T *argvars, typval_T *rettv)
4072{
4073 foldclosed_both(argvars, rettv, TRUE);
4074}
4075
4076/*
4077 * "foldlevel()" function
4078 */
4079 static void
4080f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4081{
4082#ifdef FEAT_FOLDING
4083 linenr_T lnum;
4084
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004085 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004086 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4087 rettv->vval.v_number = foldLevel(lnum);
4088#endif
4089}
4090
4091/*
4092 * "foldtext()" function
4093 */
4094 static void
4095f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
4096{
4097#ifdef FEAT_FOLDING
4098 linenr_T foldstart;
4099 linenr_T foldend;
4100 char_u *dashes;
4101 linenr_T lnum;
4102 char_u *s;
4103 char_u *r;
4104 int len;
4105 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004106 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004107#endif
4108
4109 rettv->v_type = VAR_STRING;
4110 rettv->vval.v_string = NULL;
4111#ifdef FEAT_FOLDING
4112 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
4113 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
4114 dashes = get_vim_var_str(VV_FOLDDASHES);
4115 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
4116 && dashes != NULL)
4117 {
4118 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02004119 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004120 if (!linewhite(lnum))
4121 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004122
4123 /* Find interesting text in this line. */
4124 s = skipwhite(ml_get(lnum));
4125 /* skip C comment-start */
4126 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
4127 {
4128 s = skipwhite(s + 2);
4129 if (*skipwhite(s) == NUL
4130 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
4131 {
4132 s = skipwhite(ml_get(lnum + 1));
4133 if (*s == '*')
4134 s = skipwhite(s + 1);
4135 }
4136 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02004137 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01004138 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004139 r = alloc((unsigned)(STRLEN(txt)
4140 + STRLEN(dashes) /* for %s */
4141 + 20 /* for %3ld */
4142 + STRLEN(s))); /* concatenated */
4143 if (r != NULL)
4144 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02004145 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004146 len = (int)STRLEN(r);
4147 STRCAT(r, s);
4148 /* remove 'foldmarker' and 'commentstring' */
4149 foldtext_cleanup(r + len);
4150 rettv->vval.v_string = r;
4151 }
4152 }
4153#endif
4154}
4155
4156/*
4157 * "foldtextresult(lnum)" function
4158 */
4159 static void
4160f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
4161{
4162#ifdef FEAT_FOLDING
4163 linenr_T lnum;
4164 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004165 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004166 foldinfo_T foldinfo;
4167 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004168 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004169#endif
4170
4171 rettv->v_type = VAR_STRING;
4172 rettv->vval.v_string = NULL;
4173#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004174 if (entered)
4175 return; /* reject recursive use */
4176 entered = TRUE;
4177
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004178 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004179 /* treat illegal types and illegal string values for {lnum} the same */
4180 if (lnum < 0)
4181 lnum = 0;
4182 fold_count = foldedCount(curwin, lnum, &foldinfo);
4183 if (fold_count > 0)
4184 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01004185 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
4186 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004187 if (text == buf)
4188 text = vim_strsave(text);
4189 rettv->vval.v_string = text;
4190 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004191
4192 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004193#endif
4194}
4195
4196/*
4197 * "foreground()" function
4198 */
4199 static void
4200f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4201{
4202#ifdef FEAT_GUI
4203 if (gui.in_use)
4204 gui_mch_set_foreground();
4205#else
Bram Moolenaar4f974752019-02-17 17:44:42 +01004206# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004207 win32_set_foreground();
4208# endif
4209#endif
4210}
4211
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004212 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004213common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004214{
4215 char_u *s;
4216 char_u *name;
4217 int use_string = FALSE;
4218 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004219 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004220
4221 if (argvars[0].v_type == VAR_FUNC)
4222 {
4223 /* function(MyFunc, [arg], dict) */
4224 s = argvars[0].vval.v_string;
4225 }
4226 else if (argvars[0].v_type == VAR_PARTIAL
4227 && argvars[0].vval.v_partial != NULL)
4228 {
4229 /* function(dict.MyFunc, [arg]) */
4230 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004231 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004232 }
4233 else
4234 {
4235 /* function('MyFunc', [arg], dict) */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004236 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004237 use_string = TRUE;
4238 }
4239
Bram Moolenaar843b8842016-08-21 14:36:15 +02004240 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004241 {
4242 name = s;
4243 trans_name = trans_function_name(&name, FALSE,
4244 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4245 if (*name != NUL)
4246 s = NULL;
4247 }
4248
Bram Moolenaar843b8842016-08-21 14:36:15 +02004249 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4250 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004251 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004252 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004253 else if (trans_name != NULL && (is_funcref
4254 ? find_func(trans_name) == NULL
4255 : !translated_function_exists(trans_name)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004256 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004257 else
4258 {
4259 int dict_idx = 0;
4260 int arg_idx = 0;
4261 list_T *list = NULL;
4262
4263 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4264 {
4265 char sid_buf[25];
4266 int off = *s == 's' ? 2 : 5;
4267
4268 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4269 * also be called from another script. Using trans_function_name()
4270 * would also work, but some plugins depend on the name being
4271 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004272 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004273 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
4274 if (name != NULL)
4275 {
4276 STRCPY(name, sid_buf);
4277 STRCAT(name, s + off);
4278 }
4279 }
4280 else
4281 name = vim_strsave(s);
4282
4283 if (argvars[1].v_type != VAR_UNKNOWN)
4284 {
4285 if (argvars[2].v_type != VAR_UNKNOWN)
4286 {
4287 /* function(name, [args], dict) */
4288 arg_idx = 1;
4289 dict_idx = 2;
4290 }
4291 else if (argvars[1].v_type == VAR_DICT)
4292 /* function(name, dict) */
4293 dict_idx = 1;
4294 else
4295 /* function(name, [args]) */
4296 arg_idx = 1;
4297 if (dict_idx > 0)
4298 {
4299 if (argvars[dict_idx].v_type != VAR_DICT)
4300 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004301 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004302 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004303 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004304 }
4305 if (argvars[dict_idx].vval.v_dict == NULL)
4306 dict_idx = 0;
4307 }
4308 if (arg_idx > 0)
4309 {
4310 if (argvars[arg_idx].v_type != VAR_LIST)
4311 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004312 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004313 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004314 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004315 }
4316 list = argvars[arg_idx].vval.v_list;
4317 if (list == NULL || list->lv_len == 0)
4318 arg_idx = 0;
4319 }
4320 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004321 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004322 {
4323 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
4324
4325 /* result is a VAR_PARTIAL */
4326 if (pt == NULL)
4327 vim_free(name);
4328 else
4329 {
4330 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4331 {
4332 listitem_T *li;
4333 int i = 0;
4334 int arg_len = 0;
4335 int lv_len = 0;
4336
4337 if (arg_pt != NULL)
4338 arg_len = arg_pt->pt_argc;
4339 if (list != NULL)
4340 lv_len = list->lv_len;
4341 pt->pt_argc = arg_len + lv_len;
4342 pt->pt_argv = (typval_T *)alloc(
4343 sizeof(typval_T) * pt->pt_argc);
4344 if (pt->pt_argv == NULL)
4345 {
4346 vim_free(pt);
4347 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004348 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004349 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004350 for (i = 0; i < arg_len; i++)
4351 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4352 if (lv_len > 0)
4353 for (li = list->lv_first; li != NULL;
4354 li = li->li_next)
4355 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004356 }
4357
4358 /* For "function(dict.func, [], dict)" and "func" is a partial
4359 * use "dict". That is backwards compatible. */
4360 if (dict_idx > 0)
4361 {
4362 /* The dict is bound explicitly, pt_auto is FALSE. */
4363 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4364 ++pt->pt_dict->dv_refcount;
4365 }
4366 else if (arg_pt != NULL)
4367 {
4368 /* If the dict was bound automatically the result is also
4369 * bound automatically. */
4370 pt->pt_dict = arg_pt->pt_dict;
4371 pt->pt_auto = arg_pt->pt_auto;
4372 if (pt->pt_dict != NULL)
4373 ++pt->pt_dict->dv_refcount;
4374 }
4375
4376 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004377 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4378 {
4379 pt->pt_func = arg_pt->pt_func;
4380 func_ptr_ref(pt->pt_func);
4381 vim_free(name);
4382 }
4383 else if (is_funcref)
4384 {
4385 pt->pt_func = find_func(trans_name);
4386 func_ptr_ref(pt->pt_func);
4387 vim_free(name);
4388 }
4389 else
4390 {
4391 pt->pt_name = name;
4392 func_ref(name);
4393 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004394 }
4395 rettv->v_type = VAR_PARTIAL;
4396 rettv->vval.v_partial = pt;
4397 }
4398 else
4399 {
4400 /* result is a VAR_FUNC */
4401 rettv->v_type = VAR_FUNC;
4402 rettv->vval.v_string = name;
4403 func_ref(name);
4404 }
4405 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004406theend:
4407 vim_free(trans_name);
4408}
4409
4410/*
4411 * "funcref()" function
4412 */
4413 static void
4414f_funcref(typval_T *argvars, typval_T *rettv)
4415{
4416 common_function(argvars, rettv, TRUE);
4417}
4418
4419/*
4420 * "function()" function
4421 */
4422 static void
4423f_function(typval_T *argvars, typval_T *rettv)
4424{
4425 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004426}
4427
4428/*
4429 * "garbagecollect()" function
4430 */
4431 static void
4432f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4433{
4434 /* This is postponed until we are back at the toplevel, because we may be
4435 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4436 want_garbage_collect = TRUE;
4437
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004438 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004439 garbage_collect_at_exit = TRUE;
4440}
4441
4442/*
4443 * "get()" function
4444 */
4445 static void
4446f_get(typval_T *argvars, typval_T *rettv)
4447{
4448 listitem_T *li;
4449 list_T *l;
4450 dictitem_T *di;
4451 dict_T *d;
4452 typval_T *tv = NULL;
4453
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004454 if (argvars[0].v_type == VAR_BLOB)
4455 {
4456 int error = FALSE;
4457 int idx = tv_get_number_chk(&argvars[1], &error);
4458
4459 if (!error)
4460 {
4461 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004462 if (idx < 0)
4463 idx = blob_len(argvars[0].vval.v_blob) + idx;
4464 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
4465 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004466 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004467 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004468 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004469 tv = rettv;
4470 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004471 }
4472 }
4473 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004474 {
4475 if ((l = argvars[0].vval.v_list) != NULL)
4476 {
4477 int error = FALSE;
4478
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004479 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004480 if (!error && li != NULL)
4481 tv = &li->li_tv;
4482 }
4483 }
4484 else if (argvars[0].v_type == VAR_DICT)
4485 {
4486 if ((d = argvars[0].vval.v_dict) != NULL)
4487 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004488 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004489 if (di != NULL)
4490 tv = &di->di_tv;
4491 }
4492 }
4493 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4494 {
4495 partial_T *pt;
4496 partial_T fref_pt;
4497
4498 if (argvars[0].v_type == VAR_PARTIAL)
4499 pt = argvars[0].vval.v_partial;
4500 else
4501 {
4502 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4503 fref_pt.pt_name = argvars[0].vval.v_string;
4504 pt = &fref_pt;
4505 }
4506
4507 if (pt != NULL)
4508 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004509 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004510 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004511
4512 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4513 {
4514 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004515 n = partial_name(pt);
4516 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004517 rettv->vval.v_string = NULL;
4518 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004519 {
4520 rettv->vval.v_string = vim_strsave(n);
4521 if (rettv->v_type == VAR_FUNC)
4522 func_ref(rettv->vval.v_string);
4523 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004524 }
4525 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004526 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004527 else if (STRCMP(what, "args") == 0)
4528 {
4529 rettv->v_type = VAR_LIST;
4530 if (rettv_list_alloc(rettv) == OK)
4531 {
4532 int i;
4533
4534 for (i = 0; i < pt->pt_argc; ++i)
4535 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4536 }
4537 }
4538 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004539 semsg(_(e_invarg2), what);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004540 return;
4541 }
4542 }
4543 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004544 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004545
4546 if (tv == NULL)
4547 {
4548 if (argvars[2].v_type != VAR_UNKNOWN)
4549 copy_tv(&argvars[2], rettv);
4550 }
4551 else
4552 copy_tv(tv, rettv);
4553}
4554
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004555/*
4556 * Returns buffer options, variables and other attributes in a dictionary.
4557 */
4558 static dict_T *
4559get_buffer_info(buf_T *buf)
4560{
4561 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004562 tabpage_T *tp;
4563 win_T *wp;
4564 list_T *windows;
4565
4566 dict = dict_alloc();
4567 if (dict == NULL)
4568 return NULL;
4569
Bram Moolenaare0be1672018-07-08 16:50:37 +02004570 dict_add_number(dict, "bufnr", buf->b_fnum);
4571 dict_add_string(dict, "name", buf->b_ffname);
4572 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4573 : buflist_findlnum(buf));
4574 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4575 dict_add_number(dict, "listed", buf->b_p_bl);
4576 dict_add_number(dict, "changed", bufIsChanged(buf));
4577 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4578 dict_add_number(dict, "hidden",
4579 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004580
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004581 /* Get a reference to buffer variables */
4582 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004583
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004584 /* List of windows displaying this buffer */
4585 windows = list_alloc();
4586 if (windows != NULL)
4587 {
4588 FOR_ALL_TAB_WINDOWS(tp, wp)
4589 if (wp->w_buffer == buf)
4590 list_append_number(windows, (varnumber_T)wp->w_id);
4591 dict_add_list(dict, "windows", windows);
4592 }
4593
4594#ifdef FEAT_SIGNS
4595 if (buf->b_signlist != NULL)
4596 {
4597 /* List of signs placed in this buffer */
4598 list_T *signs = list_alloc();
4599 if (signs != NULL)
4600 {
4601 get_buffer_signs(buf, signs);
4602 dict_add_list(dict, "signs", signs);
4603 }
4604 }
4605#endif
4606
4607 return dict;
4608}
4609
4610/*
4611 * "getbufinfo()" function
4612 */
4613 static void
4614f_getbufinfo(typval_T *argvars, typval_T *rettv)
4615{
4616 buf_T *buf = NULL;
4617 buf_T *argbuf = NULL;
4618 dict_T *d;
4619 int filtered = FALSE;
4620 int sel_buflisted = FALSE;
4621 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004622 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004623
4624 if (rettv_list_alloc(rettv) != OK)
4625 return;
4626
4627 /* List of all the buffers or selected buffers */
4628 if (argvars[0].v_type == VAR_DICT)
4629 {
4630 dict_T *sel_d = argvars[0].vval.v_dict;
4631
4632 if (sel_d != NULL)
4633 {
4634 dictitem_T *di;
4635
4636 filtered = TRUE;
4637
4638 di = dict_find(sel_d, (char_u *)"buflisted", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004639 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004640 sel_buflisted = TRUE;
4641
4642 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004643 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004644 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004645
4646 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004647 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004648 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004649 }
4650 }
4651 else if (argvars[0].v_type != VAR_UNKNOWN)
4652 {
4653 /* Information about one buffer. Argument specifies the buffer */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004654 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004655 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004656 argbuf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004657 --emsg_off;
4658 if (argbuf == NULL)
4659 return;
4660 }
4661
4662 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004663 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004664 {
4665 if (argbuf != NULL && argbuf != buf)
4666 continue;
4667 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004668 || (sel_buflisted && !buf->b_p_bl)
4669 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004670 continue;
4671
4672 d = get_buffer_info(buf);
4673 if (d != NULL)
4674 list_append_dict(rettv->vval.v_list, d);
4675 if (argbuf != NULL)
4676 return;
4677 }
4678}
4679
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004680/*
4681 * Get line or list of lines from buffer "buf" into "rettv".
4682 * Return a range (from start to end) of lines in rettv from the specified
4683 * buffer.
4684 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4685 */
4686 static void
4687get_buffer_lines(
4688 buf_T *buf,
4689 linenr_T start,
4690 linenr_T end,
4691 int retlist,
4692 typval_T *rettv)
4693{
4694 char_u *p;
4695
4696 rettv->v_type = VAR_STRING;
4697 rettv->vval.v_string = NULL;
4698 if (retlist && rettv_list_alloc(rettv) == FAIL)
4699 return;
4700
4701 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4702 return;
4703
4704 if (!retlist)
4705 {
4706 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4707 p = ml_get_buf(buf, start, FALSE);
4708 else
4709 p = (char_u *)"";
4710 rettv->vval.v_string = vim_strsave(p);
4711 }
4712 else
4713 {
4714 if (end < start)
4715 return;
4716
4717 if (start < 1)
4718 start = 1;
4719 if (end > buf->b_ml.ml_line_count)
4720 end = buf->b_ml.ml_line_count;
4721 while (start <= end)
4722 if (list_append_string(rettv->vval.v_list,
4723 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4724 break;
4725 }
4726}
4727
4728/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004729 * "getbufline()" function
4730 */
4731 static void
4732f_getbufline(typval_T *argvars, typval_T *rettv)
4733{
4734 linenr_T lnum;
4735 linenr_T end;
4736 buf_T *buf;
4737
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004738 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004739 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004740 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004741 --emsg_off;
4742
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004743 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004744 if (argvars[2].v_type == VAR_UNKNOWN)
4745 end = lnum;
4746 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004747 end = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004748
4749 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4750}
4751
4752/*
4753 * "getbufvar()" function
4754 */
4755 static void
4756f_getbufvar(typval_T *argvars, typval_T *rettv)
4757{
4758 buf_T *buf;
4759 buf_T *save_curbuf;
4760 char_u *varname;
4761 dictitem_T *v;
4762 int done = FALSE;
4763
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004764 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
4765 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004766 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004767 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004768
4769 rettv->v_type = VAR_STRING;
4770 rettv->vval.v_string = NULL;
4771
4772 if (buf != NULL && varname != NULL)
4773 {
4774 /* set curbuf to be our buf, temporarily */
4775 save_curbuf = curbuf;
4776 curbuf = buf;
4777
Bram Moolenaar30567352016-08-27 21:25:44 +02004778 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004779 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004780 if (varname[1] == NUL)
4781 {
4782 /* get all buffer-local options in a dict */
4783 dict_T *opts = get_winbuf_options(TRUE);
4784
4785 if (opts != NULL)
4786 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004787 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004788 done = TRUE;
4789 }
4790 }
4791 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4792 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004793 done = TRUE;
4794 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004795 else
4796 {
4797 /* Look up the variable. */
4798 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4799 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4800 'b', varname, FALSE);
4801 if (v != NULL)
4802 {
4803 copy_tv(&v->di_tv, rettv);
4804 done = TRUE;
4805 }
4806 }
4807
4808 /* restore previous notion of curbuf */
4809 curbuf = save_curbuf;
4810 }
4811
4812 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4813 /* use the default value */
4814 copy_tv(&argvars[2], rettv);
4815
4816 --emsg_off;
4817}
4818
4819/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004820 * "getchangelist()" function
4821 */
4822 static void
4823f_getchangelist(typval_T *argvars, typval_T *rettv)
4824{
4825#ifdef FEAT_JUMPLIST
4826 buf_T *buf;
4827 int i;
4828 list_T *l;
4829 dict_T *d;
4830#endif
4831
4832 if (rettv_list_alloc(rettv) != OK)
4833 return;
4834
4835#ifdef FEAT_JUMPLIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004836 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004837 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004838 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004839 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004840 if (buf == NULL)
4841 return;
4842
4843 l = list_alloc();
4844 if (l == NULL)
4845 return;
4846
4847 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4848 return;
4849 /*
4850 * The current window change list index tracks only the position in the
4851 * current buffer change list. For other buffers, use the change list
4852 * length as the current index.
4853 */
4854 list_append_number(rettv->vval.v_list,
4855 (varnumber_T)((buf == curwin->w_buffer)
4856 ? curwin->w_changelistidx : buf->b_changelistlen));
4857
4858 for (i = 0; i < buf->b_changelistlen; ++i)
4859 {
4860 if (buf->b_changelist[i].lnum == 0)
4861 continue;
4862 if ((d = dict_alloc()) == NULL)
4863 return;
4864 if (list_append_dict(l, d) == FAIL)
4865 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004866 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4867 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004868 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004869 }
4870#endif
4871}
4872/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004873 * "getchar()" function
4874 */
4875 static void
4876f_getchar(typval_T *argvars, typval_T *rettv)
4877{
4878 varnumber_T n;
4879 int error = FALSE;
4880
Bram Moolenaar84d93902018-09-11 20:10:20 +02004881#ifdef MESSAGE_QUEUE
4882 // vpeekc() used to check for messages, but that caused problems, invoking
4883 // a callback where it was not expected. Some plugins use getchar(1) in a
4884 // loop to await a message, therefore make sure we check for messages here.
4885 parse_queued_messages();
4886#endif
4887
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004888 /* Position the cursor. Needed after a message that ends in a space. */
4889 windgoto(msg_row, msg_col);
4890
4891 ++no_mapping;
4892 ++allow_keys;
4893 for (;;)
4894 {
4895 if (argvars[0].v_type == VAR_UNKNOWN)
4896 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004897 n = plain_vgetc();
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004898 else if (tv_get_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004899 /* getchar(1): only check if char avail */
4900 n = vpeekc_any();
4901 else if (error || vpeekc_any() == NUL)
4902 /* illegal argument or getchar(0) and no char avail: return zero */
4903 n = 0;
4904 else
4905 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004906 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004907
4908 if (n == K_IGNORE)
4909 continue;
4910 break;
4911 }
4912 --no_mapping;
4913 --allow_keys;
4914
4915 set_vim_var_nr(VV_MOUSE_WIN, 0);
4916 set_vim_var_nr(VV_MOUSE_WINID, 0);
4917 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4918 set_vim_var_nr(VV_MOUSE_COL, 0);
4919
4920 rettv->vval.v_number = n;
4921 if (IS_SPECIAL(n) || mod_mask != 0)
4922 {
4923 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4924 int i = 0;
4925
4926 /* Turn a special key into three bytes, plus modifier. */
4927 if (mod_mask != 0)
4928 {
4929 temp[i++] = K_SPECIAL;
4930 temp[i++] = KS_MODIFIER;
4931 temp[i++] = mod_mask;
4932 }
4933 if (IS_SPECIAL(n))
4934 {
4935 temp[i++] = K_SPECIAL;
4936 temp[i++] = K_SECOND(n);
4937 temp[i++] = K_THIRD(n);
4938 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004939 else if (has_mbyte)
4940 i += (*mb_char2bytes)(n, temp + i);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004941 else
4942 temp[i++] = n;
4943 temp[i++] = NUL;
4944 rettv->v_type = VAR_STRING;
4945 rettv->vval.v_string = vim_strsave(temp);
4946
4947#ifdef FEAT_MOUSE
4948 if (is_mouse_key(n))
4949 {
4950 int row = mouse_row;
4951 int col = mouse_col;
4952 win_T *win;
4953 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004954 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004955 int winnr = 1;
4956
4957 if (row >= 0 && col >= 0)
4958 {
4959 /* Find the window at the mouse coordinates and compute the
4960 * text position. */
4961 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004962 if (win == NULL)
4963 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004964 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004965 for (wp = firstwin; wp != win; wp = wp->w_next)
4966 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004967 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4968 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4969 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4970 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4971 }
4972 }
4973#endif
4974 }
4975}
4976
4977/*
4978 * "getcharmod()" function
4979 */
4980 static void
4981f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4982{
4983 rettv->vval.v_number = mod_mask;
4984}
4985
4986/*
4987 * "getcharsearch()" function
4988 */
4989 static void
4990f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4991{
4992 if (rettv_dict_alloc(rettv) != FAIL)
4993 {
4994 dict_T *dict = rettv->vval.v_dict;
4995
Bram Moolenaare0be1672018-07-08 16:50:37 +02004996 dict_add_string(dict, "char", last_csearch());
4997 dict_add_number(dict, "forward", last_csearch_forward());
4998 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004999 }
5000}
5001
5002/*
5003 * "getcmdline()" function
5004 */
5005 static void
5006f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
5007{
5008 rettv->v_type = VAR_STRING;
5009 rettv->vval.v_string = get_cmdline_str();
5010}
5011
5012/*
5013 * "getcmdpos()" function
5014 */
5015 static void
5016f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
5017{
5018 rettv->vval.v_number = get_cmdline_pos() + 1;
5019}
5020
5021/*
5022 * "getcmdtype()" function
5023 */
5024 static void
5025f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
5026{
5027 rettv->v_type = VAR_STRING;
5028 rettv->vval.v_string = alloc(2);
5029 if (rettv->vval.v_string != NULL)
5030 {
5031 rettv->vval.v_string[0] = get_cmdline_type();
5032 rettv->vval.v_string[1] = NUL;
5033 }
5034}
5035
5036/*
5037 * "getcmdwintype()" function
5038 */
5039 static void
5040f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
5041{
5042 rettv->v_type = VAR_STRING;
5043 rettv->vval.v_string = NULL;
5044#ifdef FEAT_CMDWIN
5045 rettv->vval.v_string = alloc(2);
5046 if (rettv->vval.v_string != NULL)
5047 {
5048 rettv->vval.v_string[0] = cmdwin_type;
5049 rettv->vval.v_string[1] = NUL;
5050 }
5051#endif
5052}
5053
5054#if defined(FEAT_CMDL_COMPL)
5055/*
5056 * "getcompletion()" function
5057 */
5058 static void
5059f_getcompletion(typval_T *argvars, typval_T *rettv)
5060{
5061 char_u *pat;
5062 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005063 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005064 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
5065 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005066
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005067 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005068 filtered = tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005069
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005070 if (p_wic)
5071 options |= WILD_ICASE;
5072
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005073 /* For filtered results, 'wildignore' is used */
5074 if (!filtered)
5075 options |= WILD_KEEP_ALL;
5076
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005077 ExpandInit(&xpc);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005078 xpc.xp_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005079 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005080 xpc.xp_context = cmdcomplete_str_to_type(tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005081 if (xpc.xp_context == EXPAND_NOTHING)
5082 {
5083 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005084 semsg(_(e_invarg2), argvars[1].vval.v_string);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005085 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005086 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005087 return;
5088 }
5089
5090# if defined(FEAT_MENU)
5091 if (xpc.xp_context == EXPAND_MENUS)
5092 {
5093 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
5094 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5095 }
5096# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02005097#ifdef FEAT_CSCOPE
5098 if (xpc.xp_context == EXPAND_CSCOPE)
5099 {
5100 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
5101 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5102 }
5103#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02005104#ifdef FEAT_SIGNS
5105 if (xpc.xp_context == EXPAND_SIGN)
5106 {
5107 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
5108 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5109 }
5110#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005111
5112 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
5113 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
5114 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005115 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005116
5117 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
5118
5119 for (i = 0; i < xpc.xp_numfiles; i++)
5120 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5121 }
5122 vim_free(pat);
5123 ExpandCleanup(&xpc);
5124}
5125#endif
5126
5127/*
5128 * "getcwd()" function
5129 */
5130 static void
5131f_getcwd(typval_T *argvars, typval_T *rettv)
5132{
5133 win_T *wp = NULL;
5134 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01005135 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005136
5137 rettv->v_type = VAR_STRING;
5138 rettv->vval.v_string = NULL;
5139
Bram Moolenaar54591292018-02-09 20:53:59 +01005140 if (argvars[0].v_type == VAR_NUMBER && argvars[0].vval.v_number == -1)
5141 global = TRUE;
5142 else
5143 wp = find_tabwin(&argvars[0], &argvars[1]);
5144
5145 if (wp != NULL && wp->w_localdir != NULL)
5146 rettv->vval.v_string = vim_strsave(wp->w_localdir);
5147 else if (wp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005148 {
Bram Moolenaar54591292018-02-09 20:53:59 +01005149 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005150 rettv->vval.v_string = vim_strsave(globaldir);
5151 else
5152 {
5153 cwd = alloc(MAXPATHL);
5154 if (cwd != NULL)
5155 {
5156 if (mch_dirname(cwd, MAXPATHL) != FAIL)
5157 rettv->vval.v_string = vim_strsave(cwd);
5158 vim_free(cwd);
5159 }
5160 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005161 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02005162#ifdef BACKSLASH_IN_FILENAME
5163 if (rettv->vval.v_string != NULL)
5164 slash_adjust(rettv->vval.v_string);
5165#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005166}
5167
5168/*
5169 * "getfontname()" function
5170 */
5171 static void
5172f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
5173{
5174 rettv->v_type = VAR_STRING;
5175 rettv->vval.v_string = NULL;
5176#ifdef FEAT_GUI
5177 if (gui.in_use)
5178 {
5179 GuiFont font;
5180 char_u *name = NULL;
5181
5182 if (argvars[0].v_type == VAR_UNKNOWN)
5183 {
5184 /* Get the "Normal" font. Either the name saved by
5185 * hl_set_font_name() or from the font ID. */
5186 font = gui.norm_font;
5187 name = hl_get_font_name();
5188 }
5189 else
5190 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005191 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005192 if (STRCMP(name, "*") == 0) /* don't use font dialog */
5193 return;
5194 font = gui_mch_get_font(name, FALSE);
5195 if (font == NOFONT)
5196 return; /* Invalid font name, return empty string. */
5197 }
5198 rettv->vval.v_string = gui_mch_get_fontname(font, name);
5199 if (argvars[0].v_type != VAR_UNKNOWN)
5200 gui_mch_free_font(font);
5201 }
5202#endif
5203}
5204
5205/*
5206 * "getfperm({fname})" function
5207 */
5208 static void
5209f_getfperm(typval_T *argvars, typval_T *rettv)
5210{
5211 char_u *fname;
5212 stat_T st;
5213 char_u *perm = NULL;
5214 char_u flags[] = "rwx";
5215 int i;
5216
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005217 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005218
5219 rettv->v_type = VAR_STRING;
5220 if (mch_stat((char *)fname, &st) >= 0)
5221 {
5222 perm = vim_strsave((char_u *)"---------");
5223 if (perm != NULL)
5224 {
5225 for (i = 0; i < 9; i++)
5226 {
5227 if (st.st_mode & (1 << (8 - i)))
5228 perm[i] = flags[i % 3];
5229 }
5230 }
5231 }
5232 rettv->vval.v_string = perm;
5233}
5234
5235/*
5236 * "getfsize({fname})" function
5237 */
5238 static void
5239f_getfsize(typval_T *argvars, typval_T *rettv)
5240{
5241 char_u *fname;
5242 stat_T st;
5243
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005244 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005245
5246 rettv->v_type = VAR_NUMBER;
5247
5248 if (mch_stat((char *)fname, &st) >= 0)
5249 {
5250 if (mch_isdir(fname))
5251 rettv->vval.v_number = 0;
5252 else
5253 {
5254 rettv->vval.v_number = (varnumber_T)st.st_size;
5255
5256 /* non-perfect check for overflow */
5257 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5258 rettv->vval.v_number = -2;
5259 }
5260 }
5261 else
5262 rettv->vval.v_number = -1;
5263}
5264
5265/*
5266 * "getftime({fname})" function
5267 */
5268 static void
5269f_getftime(typval_T *argvars, typval_T *rettv)
5270{
5271 char_u *fname;
5272 stat_T st;
5273
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005274 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005275
5276 if (mch_stat((char *)fname, &st) >= 0)
5277 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5278 else
5279 rettv->vval.v_number = -1;
5280}
5281
5282/*
5283 * "getftype({fname})" function
5284 */
5285 static void
5286f_getftype(typval_T *argvars, typval_T *rettv)
5287{
5288 char_u *fname;
5289 stat_T st;
5290 char_u *type = NULL;
5291 char *t;
5292
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005293 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005294
5295 rettv->v_type = VAR_STRING;
5296 if (mch_lstat((char *)fname, &st) >= 0)
5297 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005298 if (S_ISREG(st.st_mode))
5299 t = "file";
5300 else if (S_ISDIR(st.st_mode))
5301 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005302 else if (S_ISLNK(st.st_mode))
5303 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005304 else if (S_ISBLK(st.st_mode))
5305 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005306 else if (S_ISCHR(st.st_mode))
5307 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005308 else if (S_ISFIFO(st.st_mode))
5309 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005310 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005311 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005312 else
5313 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005314 type = vim_strsave((char_u *)t);
5315 }
5316 rettv->vval.v_string = type;
5317}
5318
5319/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005320 * "getjumplist()" function
5321 */
5322 static void
5323f_getjumplist(typval_T *argvars, typval_T *rettv)
5324{
5325#ifdef FEAT_JUMPLIST
5326 win_T *wp;
5327 int i;
5328 list_T *l;
5329 dict_T *d;
5330#endif
5331
5332 if (rettv_list_alloc(rettv) != OK)
5333 return;
5334
5335#ifdef FEAT_JUMPLIST
5336 wp = find_tabwin(&argvars[0], &argvars[1]);
5337 if (wp == NULL)
5338 return;
5339
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01005340 cleanup_jumplist(wp, TRUE);
5341
Bram Moolenaar4f505882018-02-10 21:06:32 +01005342 l = list_alloc();
5343 if (l == NULL)
5344 return;
5345
5346 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5347 return;
5348 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5349
5350 for (i = 0; i < wp->w_jumplistlen; ++i)
5351 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005352 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5353 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005354 if ((d = dict_alloc()) == NULL)
5355 return;
5356 if (list_append_dict(l, d) == FAIL)
5357 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005358 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5359 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005360 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005361 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005362 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005363 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005364 }
5365#endif
5366}
5367
5368/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005369 * "getline(lnum, [end])" function
5370 */
5371 static void
5372f_getline(typval_T *argvars, typval_T *rettv)
5373{
5374 linenr_T lnum;
5375 linenr_T end;
5376 int retlist;
5377
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005378 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005379 if (argvars[1].v_type == VAR_UNKNOWN)
5380 {
5381 end = 0;
5382 retlist = FALSE;
5383 }
5384 else
5385 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005386 end = tv_get_lnum(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005387 retlist = TRUE;
5388 }
5389
5390 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5391}
5392
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005393#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005394 static void
5395get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5396{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005397 if (what_arg->v_type == VAR_UNKNOWN)
5398 {
5399 if (rettv_list_alloc(rettv) == OK)
5400 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005401 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005402 }
5403 else
5404 {
5405 if (rettv_dict_alloc(rettv) == OK)
5406 if (is_qf || (wp != NULL))
5407 {
5408 if (what_arg->v_type == VAR_DICT)
5409 {
5410 dict_T *d = what_arg->vval.v_dict;
5411
5412 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005413 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005414 }
5415 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005416 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +02005417 }
5418 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005419}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005420#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005421
5422/*
5423 * "getloclist()" function
5424 */
5425 static void
5426f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5427{
5428#ifdef FEAT_QUICKFIX
5429 win_T *wp;
5430
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005431 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005432 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5433#endif
5434}
5435
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005436/*
5437 * "getmatches()" function
5438 */
5439 static void
5440f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5441{
5442#ifdef FEAT_SEARCH_EXTRA
5443 dict_T *dict;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005444 matchitem_T *cur;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005445 int i;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005446 win_T *win = get_optional_window(argvars, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005447
Bram Moolenaaraff74912019-03-30 18:11:49 +01005448 if (rettv_list_alloc(rettv) == FAIL || win == NULL)
5449 return;
5450
5451 cur = win->w_match_head;
5452 while (cur != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005453 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005454 dict = dict_alloc();
5455 if (dict == NULL)
5456 return;
5457 if (cur->match.regprog == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005458 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005459 /* match added with matchaddpos() */
5460 for (i = 0; i < MAXPOSMATCH; ++i)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005461 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005462 llpos_T *llpos;
Bram Moolenaar54315892019-04-26 22:33:49 +02005463 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaaraff74912019-03-30 18:11:49 +01005464 list_T *l;
5465
5466 llpos = &cur->pos.pos[i];
5467 if (llpos->lnum == 0)
5468 break;
5469 l = list_alloc();
5470 if (l == NULL)
5471 break;
5472 list_append_number(l, (varnumber_T)llpos->lnum);
5473 if (llpos->col > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005474 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005475 list_append_number(l, (varnumber_T)llpos->col);
5476 list_append_number(l, (varnumber_T)llpos->len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005477 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005478 sprintf(buf, "pos%d", i + 1);
5479 dict_add_list(dict, buf, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005480 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005481 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005482 else
5483 {
5484 dict_add_string(dict, "pattern", cur->pattern);
5485 }
5486 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
5487 dict_add_number(dict, "priority", (long)cur->priority);
5488 dict_add_number(dict, "id", (long)cur->id);
5489# if defined(FEAT_CONCEAL)
5490 if (cur->conceal_char)
5491 {
5492 char_u buf[MB_MAXBYTES + 1];
5493
5494 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
5495 dict_add_string(dict, "conceal", (char_u *)&buf);
5496 }
5497# endif
5498 list_append_dict(rettv->vval.v_list, dict);
5499 cur = cur->next;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005500 }
5501#endif
5502}
5503
5504/*
5505 * "getpid()" function
5506 */
5507 static void
5508f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5509{
5510 rettv->vval.v_number = mch_get_pid();
5511}
5512
5513 static void
5514getpos_both(
5515 typval_T *argvars,
5516 typval_T *rettv,
5517 int getcurpos)
5518{
5519 pos_T *fp;
5520 list_T *l;
5521 int fnum = -1;
5522
5523 if (rettv_list_alloc(rettv) == OK)
5524 {
5525 l = rettv->vval.v_list;
5526 if (getcurpos)
5527 fp = &curwin->w_cursor;
5528 else
5529 fp = var2fpos(&argvars[0], TRUE, &fnum);
5530 if (fnum != -1)
5531 list_append_number(l, (varnumber_T)fnum);
5532 else
5533 list_append_number(l, (varnumber_T)0);
5534 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5535 : (varnumber_T)0);
5536 list_append_number(l, (fp != NULL)
5537 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5538 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005539 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005540 (varnumber_T)0);
5541 if (getcurpos)
5542 {
Bram Moolenaar19a66852019-03-07 11:25:32 +01005543 int save_set_curswant = curwin->w_set_curswant;
5544 colnr_T save_curswant = curwin->w_curswant;
5545 colnr_T save_virtcol = curwin->w_virtcol;
5546
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005547 update_curswant();
5548 list_append_number(l, curwin->w_curswant == MAXCOL ?
5549 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar19a66852019-03-07 11:25:32 +01005550
5551 // Do not change "curswant", as it is unexpected that a get
5552 // function has a side effect.
5553 if (save_set_curswant)
5554 {
5555 curwin->w_set_curswant = save_set_curswant;
5556 curwin->w_curswant = save_curswant;
5557 curwin->w_virtcol = save_virtcol;
5558 curwin->w_valid &= ~VALID_VIRTCOL;
5559 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005560 }
5561 }
5562 else
5563 rettv->vval.v_number = FALSE;
5564}
5565
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005566/*
5567 * "getcurpos()" function
5568 */
5569 static void
5570f_getcurpos(typval_T *argvars, typval_T *rettv)
5571{
5572 getpos_both(argvars, rettv, TRUE);
5573}
5574
5575/*
5576 * "getpos(string)" function
5577 */
5578 static void
5579f_getpos(typval_T *argvars, typval_T *rettv)
5580{
5581 getpos_both(argvars, rettv, FALSE);
5582}
5583
5584/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005585 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005586 */
5587 static void
5588f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5589{
5590#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005591 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005592#endif
5593}
5594
5595/*
5596 * "getreg()" function
5597 */
5598 static void
5599f_getreg(typval_T *argvars, typval_T *rettv)
5600{
5601 char_u *strregname;
5602 int regname;
5603 int arg2 = FALSE;
5604 int return_list = FALSE;
5605 int error = FALSE;
5606
5607 if (argvars[0].v_type != VAR_UNKNOWN)
5608 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005609 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005610 error = strregname == NULL;
5611 if (argvars[1].v_type != VAR_UNKNOWN)
5612 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005613 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005614 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005615 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005616 }
5617 }
5618 else
5619 strregname = get_vim_var_str(VV_REG);
5620
5621 if (error)
5622 return;
5623
5624 regname = (strregname == NULL ? '"' : *strregname);
5625 if (regname == 0)
5626 regname = '"';
5627
5628 if (return_list)
5629 {
5630 rettv->v_type = VAR_LIST;
5631 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5632 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5633 if (rettv->vval.v_list == NULL)
5634 (void)rettv_list_alloc(rettv);
5635 else
5636 ++rettv->vval.v_list->lv_refcount;
5637 }
5638 else
5639 {
5640 rettv->v_type = VAR_STRING;
5641 rettv->vval.v_string = get_reg_contents(regname,
5642 arg2 ? GREG_EXPR_SRC : 0);
5643 }
5644}
5645
5646/*
5647 * "getregtype()" function
5648 */
5649 static void
5650f_getregtype(typval_T *argvars, typval_T *rettv)
5651{
5652 char_u *strregname;
5653 int regname;
5654 char_u buf[NUMBUFLEN + 2];
5655 long reglen = 0;
5656
5657 if (argvars[0].v_type != VAR_UNKNOWN)
5658 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005659 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005660 if (strregname == NULL) /* type error; errmsg already given */
5661 {
5662 rettv->v_type = VAR_STRING;
5663 rettv->vval.v_string = NULL;
5664 return;
5665 }
5666 }
5667 else
5668 /* Default to v:register */
5669 strregname = get_vim_var_str(VV_REG);
5670
5671 regname = (strregname == NULL ? '"' : *strregname);
5672 if (regname == 0)
5673 regname = '"';
5674
5675 buf[0] = NUL;
5676 buf[1] = NUL;
5677 switch (get_reg_type(regname, &reglen))
5678 {
5679 case MLINE: buf[0] = 'V'; break;
5680 case MCHAR: buf[0] = 'v'; break;
5681 case MBLOCK:
5682 buf[0] = Ctrl_V;
5683 sprintf((char *)buf + 1, "%ld", reglen + 1);
5684 break;
5685 }
5686 rettv->v_type = VAR_STRING;
5687 rettv->vval.v_string = vim_strsave(buf);
5688}
5689
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005690/*
5691 * Returns information (variables, options, etc.) about a tab page
5692 * as a dictionary.
5693 */
5694 static dict_T *
5695get_tabpage_info(tabpage_T *tp, int tp_idx)
5696{
5697 win_T *wp;
5698 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005699 list_T *l;
5700
5701 dict = dict_alloc();
5702 if (dict == NULL)
5703 return NULL;
5704
Bram Moolenaare0be1672018-07-08 16:50:37 +02005705 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005706
5707 l = list_alloc();
5708 if (l != NULL)
5709 {
5710 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5711 wp; wp = wp->w_next)
5712 list_append_number(l, (varnumber_T)wp->w_id);
5713 dict_add_list(dict, "windows", l);
5714 }
5715
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005716 /* Make a reference to tabpage variables */
5717 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005718
5719 return dict;
5720}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005721
5722/*
5723 * "gettabinfo()" function
5724 */
5725 static void
5726f_gettabinfo(typval_T *argvars, typval_T *rettv)
5727{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005728 tabpage_T *tp, *tparg = NULL;
5729 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005730 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005731
5732 if (rettv_list_alloc(rettv) != OK)
5733 return;
5734
5735 if (argvars[0].v_type != VAR_UNKNOWN)
5736 {
5737 /* Information about one tab page */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005738 tparg = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005739 if (tparg == NULL)
5740 return;
5741 }
5742
5743 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005744 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005745 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005746 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005747 if (tparg != NULL && tp != tparg)
5748 continue;
5749 d = get_tabpage_info(tp, tpnr);
5750 if (d != NULL)
5751 list_append_dict(rettv->vval.v_list, d);
5752 if (tparg != NULL)
5753 return;
5754 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005755}
5756
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005757/*
5758 * "gettabvar()" function
5759 */
5760 static void
5761f_gettabvar(typval_T *argvars, typval_T *rettv)
5762{
5763 win_T *oldcurwin;
5764 tabpage_T *tp, *oldtabpage;
5765 dictitem_T *v;
5766 char_u *varname;
5767 int done = FALSE;
5768
5769 rettv->v_type = VAR_STRING;
5770 rettv->vval.v_string = NULL;
5771
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005772 varname = tv_get_string_chk(&argvars[1]);
5773 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005774 if (tp != NULL && varname != NULL)
5775 {
5776 /* Set tp to be our tabpage, temporarily. Also set the window to the
5777 * first window in the tabpage, otherwise the window is not valid. */
5778 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005779 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5780 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005781 {
5782 /* look up the variable */
5783 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5784 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5785 if (v != NULL)
5786 {
5787 copy_tv(&v->di_tv, rettv);
5788 done = TRUE;
5789 }
5790 }
5791
5792 /* restore previous notion of curwin */
5793 restore_win(oldcurwin, oldtabpage, TRUE);
5794 }
5795
5796 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5797 copy_tv(&argvars[2], rettv);
5798}
5799
5800/*
5801 * "gettabwinvar()" function
5802 */
5803 static void
5804f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5805{
5806 getwinvar(argvars, rettv, 1);
5807}
5808
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005809/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01005810 * "gettagstack()" function
5811 */
5812 static void
5813f_gettagstack(typval_T *argvars, typval_T *rettv)
5814{
5815 win_T *wp = curwin; // default is current window
5816
5817 if (rettv_dict_alloc(rettv) != OK)
5818 return;
5819
5820 if (argvars[0].v_type != VAR_UNKNOWN)
5821 {
5822 wp = find_win_by_nr_or_id(&argvars[0]);
5823 if (wp == NULL)
5824 return;
5825 }
5826
5827 get_tagstack(wp, rettv->vval.v_dict);
5828}
5829
5830/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005831 * Returns information about a window as a dictionary.
5832 */
5833 static dict_T *
5834get_win_info(win_T *wp, short tpnr, short winnr)
5835{
5836 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005837
5838 dict = dict_alloc();
5839 if (dict == NULL)
5840 return NULL;
5841
Bram Moolenaare0be1672018-07-08 16:50:37 +02005842 dict_add_number(dict, "tabnr", tpnr);
5843 dict_add_number(dict, "winnr", winnr);
5844 dict_add_number(dict, "winid", wp->w_id);
5845 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005846 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01005847 dict_add_number(dict, "topline", wp->w_topline);
5848 dict_add_number(dict, "botline", wp->w_botline - 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005849#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02005850 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005851#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005852 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005853 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005854 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005855
Bram Moolenaar69905d12017-08-13 18:14:47 +02005856#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02005857 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02005858#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005859#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02005860 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
5861 dict_add_number(dict, "loclist",
5862 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02005863#endif
5864
Bram Moolenaar30567352016-08-27 21:25:44 +02005865 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005866 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005867
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005868 return dict;
5869}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005870
5871/*
5872 * "getwininfo()" function
5873 */
5874 static void
5875f_getwininfo(typval_T *argvars, typval_T *rettv)
5876{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005877 tabpage_T *tp;
5878 win_T *wp = NULL, *wparg = NULL;
5879 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005880 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005881
5882 if (rettv_list_alloc(rettv) != OK)
5883 return;
5884
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005885 if (argvars[0].v_type != VAR_UNKNOWN)
5886 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01005887 wparg = win_id2wp(tv_get_number(&argvars[0]));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005888 if (wparg == NULL)
5889 return;
5890 }
5891
5892 /* Collect information about either all the windows across all the tab
5893 * pages or one particular window.
5894 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005895 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005896 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005897 tabnr++;
5898 winnr = 0;
5899 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005900 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005901 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005902 if (wparg != NULL && wp != wparg)
5903 continue;
5904 d = get_win_info(wp, tabnr, winnr);
5905 if (d != NULL)
5906 list_append_dict(rettv->vval.v_list, d);
5907 if (wparg != NULL)
5908 /* found information about a specific window */
5909 return;
5910 }
5911 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005912}
5913
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005914/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005915 * "win_findbuf()" function
5916 */
5917 static void
5918f_win_findbuf(typval_T *argvars, typval_T *rettv)
5919{
5920 if (rettv_list_alloc(rettv) != FAIL)
5921 win_findbuf(argvars, rettv->vval.v_list);
5922}
5923
5924/*
5925 * "win_getid()" function
5926 */
5927 static void
5928f_win_getid(typval_T *argvars, typval_T *rettv)
5929{
5930 rettv->vval.v_number = win_getid(argvars);
5931}
5932
5933/*
5934 * "win_gotoid()" function
5935 */
5936 static void
5937f_win_gotoid(typval_T *argvars, typval_T *rettv)
5938{
5939 rettv->vval.v_number = win_gotoid(argvars);
5940}
5941
5942/*
5943 * "win_id2tabwin()" function
5944 */
5945 static void
5946f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5947{
5948 if (rettv_list_alloc(rettv) != FAIL)
5949 win_id2tabwin(argvars, rettv->vval.v_list);
5950}
5951
5952/*
5953 * "win_id2win()" function
5954 */
5955 static void
5956f_win_id2win(typval_T *argvars, typval_T *rettv)
5957{
5958 rettv->vval.v_number = win_id2win(argvars);
5959}
5960
5961/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005962 * "win_screenpos()" function
5963 */
5964 static void
5965f_win_screenpos(typval_T *argvars, typval_T *rettv)
5966{
5967 win_T *wp;
5968
5969 if (rettv_list_alloc(rettv) == FAIL)
5970 return;
5971
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005972 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005973 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5974 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5975}
5976
5977/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005978 * "getwinpos({timeout})" function
5979 */
5980 static void
5981f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
5982{
5983 int x = -1;
5984 int y = -1;
5985
5986 if (rettv_list_alloc(rettv) == FAIL)
5987 return;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02005988#if defined(FEAT_GUI) \
5989 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
5990 || defined(MSWIN)
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005991 {
5992 varnumber_T timeout = 100;
5993
5994 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005995 timeout = tv_get_number(&argvars[0]);
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02005996
5997 (void)ui_get_winpos(&x, &y, timeout);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005998 }
5999#endif
6000 list_append_number(rettv->vval.v_list, (varnumber_T)x);
6001 list_append_number(rettv->vval.v_list, (varnumber_T)y);
6002}
6003
6004
6005/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006006 * "getwinposx()" function
6007 */
6008 static void
6009f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
6010{
6011 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006012#if defined(FEAT_GUI) \
6013 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6014 || defined(MSWIN)
6015
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006016 {
6017 int x, y;
6018
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006019 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006020 rettv->vval.v_number = x;
6021 }
6022#endif
6023}
6024
6025/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006026 * "getwinposy()" function
6027 */
6028 static void
6029f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
6030{
6031 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006032#if defined(FEAT_GUI) \
6033 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6034 || defined(MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006035 {
6036 int x, y;
6037
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006038 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006039 rettv->vval.v_number = y;
6040 }
6041#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006042}
6043
6044/*
6045 * "getwinvar()" function
6046 */
6047 static void
6048f_getwinvar(typval_T *argvars, typval_T *rettv)
6049{
6050 getwinvar(argvars, rettv, 0);
6051}
6052
6053/*
6054 * "glob()" function
6055 */
6056 static void
6057f_glob(typval_T *argvars, typval_T *rettv)
6058{
6059 int options = WILD_SILENT|WILD_USE_NL;
6060 expand_T xpc;
6061 int error = FALSE;
6062
6063 /* When the optional second argument is non-zero, don't remove matches
6064 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6065 rettv->v_type = VAR_STRING;
6066 if (argvars[1].v_type != VAR_UNKNOWN)
6067 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006068 if (tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006069 options |= WILD_KEEP_ALL;
6070 if (argvars[2].v_type != VAR_UNKNOWN)
6071 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006072 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006073 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006074 if (argvars[3].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006075 && tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006076 options |= WILD_ALLLINKS;
6077 }
6078 }
6079 if (!error)
6080 {
6081 ExpandInit(&xpc);
6082 xpc.xp_context = EXPAND_FILES;
6083 if (p_wic)
6084 options += WILD_ICASE;
6085 if (rettv->v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006086 rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006087 NULL, options, WILD_ALL);
6088 else if (rettv_list_alloc(rettv) != FAIL)
6089 {
6090 int i;
6091
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006092 ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006093 NULL, options, WILD_ALL_KEEP);
6094 for (i = 0; i < xpc.xp_numfiles; i++)
6095 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
6096
6097 ExpandCleanup(&xpc);
6098 }
6099 }
6100 else
6101 rettv->vval.v_string = NULL;
6102}
6103
6104/*
6105 * "globpath()" function
6106 */
6107 static void
6108f_globpath(typval_T *argvars, typval_T *rettv)
6109{
6110 int flags = 0;
6111 char_u buf1[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006112 char_u *file = tv_get_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006113 int error = FALSE;
6114 garray_T ga;
6115 int i;
6116
6117 /* When the optional second argument is non-zero, don't remove matches
6118 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6119 rettv->v_type = VAR_STRING;
6120 if (argvars[2].v_type != VAR_UNKNOWN)
6121 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006122 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006123 flags |= WILD_KEEP_ALL;
6124 if (argvars[3].v_type != VAR_UNKNOWN)
6125 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006126 if (tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006127 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006128 if (argvars[4].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006129 && tv_get_number_chk(&argvars[4], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006130 flags |= WILD_ALLLINKS;
6131 }
6132 }
6133 if (file != NULL && !error)
6134 {
6135 ga_init2(&ga, (int)sizeof(char_u *), 10);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006136 globpath(tv_get_string(&argvars[0]), file, &ga, flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006137 if (rettv->v_type == VAR_STRING)
6138 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
6139 else if (rettv_list_alloc(rettv) != FAIL)
6140 for (i = 0; i < ga.ga_len; ++i)
6141 list_append_string(rettv->vval.v_list,
6142 ((char_u **)(ga.ga_data))[i], -1);
6143 ga_clear_strings(&ga);
6144 }
6145 else
6146 rettv->vval.v_string = NULL;
6147}
6148
6149/*
6150 * "glob2regpat()" function
6151 */
6152 static void
6153f_glob2regpat(typval_T *argvars, typval_T *rettv)
6154{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006155 char_u *pat = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006156
6157 rettv->v_type = VAR_STRING;
6158 rettv->vval.v_string = (pat == NULL)
6159 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
6160}
6161
6162/* for VIM_VERSION_ defines */
6163#include "version.h"
6164
6165/*
6166 * "has()" function
6167 */
6168 static void
6169f_has(typval_T *argvars, typval_T *rettv)
6170{
6171 int i;
6172 char_u *name;
6173 int n = FALSE;
6174 static char *(has_list[]) =
6175 {
6176#ifdef AMIGA
6177 "amiga",
6178# ifdef FEAT_ARP
6179 "arp",
6180# endif
6181#endif
6182#ifdef __BEOS__
6183 "beos",
6184#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006185#if defined(BSD) && !defined(MACOS_X)
6186 "bsd",
6187#endif
6188#ifdef hpux
6189 "hpux",
6190#endif
6191#ifdef __linux__
6192 "linux",
6193#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02006194#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01006195 "mac", /* Mac OS X (and, once, Mac OS Classic) */
6196 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02006197# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01006198 "macunix", /* Mac OS X, with the darwin feature */
6199 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02006200# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006201#endif
6202#ifdef __QNX__
6203 "qnx",
6204#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006205#ifdef SUN_SYSTEM
6206 "sun",
6207#else
6208 "moon",
6209#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006210#ifdef UNIX
6211 "unix",
6212#endif
6213#ifdef VMS
6214 "vms",
6215#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006216#ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006217 "win32",
6218#endif
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006219#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006220 "win32unix",
6221#endif
Bram Moolenaar44b443c2019-02-18 22:14:18 +01006222#ifdef _WIN64
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006223 "win64",
6224#endif
6225#ifdef EBCDIC
6226 "ebcdic",
6227#endif
6228#ifndef CASE_INSENSITIVE_FILENAME
6229 "fname_case",
6230#endif
6231#ifdef HAVE_ACL
6232 "acl",
6233#endif
6234#ifdef FEAT_ARABIC
6235 "arabic",
6236#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006237 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006238#ifdef FEAT_AUTOCHDIR
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006239 "autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006240#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006241#ifdef FEAT_AUTOSERVERNAME
6242 "autoservername",
6243#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006244#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006245 "balloon_eval",
Bram Moolenaar4f974752019-02-17 17:44:42 +01006246# ifndef FEAT_GUI_MSWIN /* other GUIs always have multiline balloons */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006247 "balloon_multiline",
6248# endif
6249#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006250#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006251 "balloon_eval_term",
6252#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006253#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6254 "builtin_terms",
6255# ifdef ALL_BUILTIN_TCAPS
6256 "all_builtin_terms",
6257# endif
6258#endif
6259#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01006260 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006261 || defined(FEAT_GUI_MOTIF))
6262 "browsefilter",
6263#endif
6264#ifdef FEAT_BYTEOFF
6265 "byte_offset",
6266#endif
6267#ifdef FEAT_JOB_CHANNEL
6268 "channel",
6269#endif
6270#ifdef FEAT_CINDENT
6271 "cindent",
6272#endif
6273#ifdef FEAT_CLIENTSERVER
6274 "clientserver",
6275#endif
6276#ifdef FEAT_CLIPBOARD
6277 "clipboard",
6278#endif
6279#ifdef FEAT_CMDL_COMPL
6280 "cmdline_compl",
6281#endif
6282#ifdef FEAT_CMDHIST
6283 "cmdline_hist",
6284#endif
6285#ifdef FEAT_COMMENTS
6286 "comments",
6287#endif
6288#ifdef FEAT_CONCEAL
6289 "conceal",
6290#endif
6291#ifdef FEAT_CRYPT
6292 "cryptv",
6293 "crypt-blowfish",
6294 "crypt-blowfish2",
6295#endif
6296#ifdef FEAT_CSCOPE
6297 "cscope",
6298#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006299 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006300#ifdef CURSOR_SHAPE
6301 "cursorshape",
6302#endif
6303#ifdef DEBUG
6304 "debug",
6305#endif
6306#ifdef FEAT_CON_DIALOG
6307 "dialog_con",
6308#endif
6309#ifdef FEAT_GUI_DIALOG
6310 "dialog_gui",
6311#endif
6312#ifdef FEAT_DIFF
6313 "diff",
6314#endif
6315#ifdef FEAT_DIGRAPHS
6316 "digraphs",
6317#endif
6318#ifdef FEAT_DIRECTX
6319 "directx",
6320#endif
6321#ifdef FEAT_DND
6322 "dnd",
6323#endif
6324#ifdef FEAT_EMACS_TAGS
6325 "emacs_tags",
6326#endif
6327 "eval", /* always present, of course! */
6328 "ex_extra", /* graduated feature */
6329#ifdef FEAT_SEARCH_EXTRA
6330 "extra_search",
6331#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006332#ifdef FEAT_SEARCHPATH
6333 "file_in_path",
6334#endif
6335#ifdef FEAT_FILTERPIPE
6336 "filterpipe",
6337#endif
6338#ifdef FEAT_FIND_ID
6339 "find_in_path",
6340#endif
6341#ifdef FEAT_FLOAT
6342 "float",
6343#endif
6344#ifdef FEAT_FOLDING
6345 "folding",
6346#endif
6347#ifdef FEAT_FOOTER
6348 "footer",
6349#endif
6350#if !defined(USE_SYSTEM) && defined(UNIX)
6351 "fork",
6352#endif
6353#ifdef FEAT_GETTEXT
6354 "gettext",
6355#endif
6356#ifdef FEAT_GUI
6357 "gui",
6358#endif
6359#ifdef FEAT_GUI_ATHENA
6360# ifdef FEAT_GUI_NEXTAW
6361 "gui_neXtaw",
6362# else
6363 "gui_athena",
6364# endif
6365#endif
6366#ifdef FEAT_GUI_GTK
6367 "gui_gtk",
6368# ifdef USE_GTK3
6369 "gui_gtk3",
6370# else
6371 "gui_gtk2",
6372# endif
6373#endif
6374#ifdef FEAT_GUI_GNOME
6375 "gui_gnome",
6376#endif
6377#ifdef FEAT_GUI_MAC
6378 "gui_mac",
6379#endif
6380#ifdef FEAT_GUI_MOTIF
6381 "gui_motif",
6382#endif
6383#ifdef FEAT_GUI_PHOTON
6384 "gui_photon",
6385#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006386#ifdef FEAT_GUI_MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006387 "gui_win32",
6388#endif
6389#ifdef FEAT_HANGULIN
6390 "hangul_input",
6391#endif
6392#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6393 "iconv",
6394#endif
6395#ifdef FEAT_INS_EXPAND
6396 "insert_expand",
6397#endif
6398#ifdef FEAT_JOB_CHANNEL
6399 "job",
6400#endif
6401#ifdef FEAT_JUMPLIST
6402 "jumplist",
6403#endif
6404#ifdef FEAT_KEYMAP
6405 "keymap",
6406#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006407 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006408#ifdef FEAT_LANGMAP
6409 "langmap",
6410#endif
6411#ifdef FEAT_LIBCALL
6412 "libcall",
6413#endif
6414#ifdef FEAT_LINEBREAK
6415 "linebreak",
6416#endif
6417#ifdef FEAT_LISP
6418 "lispindent",
6419#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006420 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006421#ifdef FEAT_LOCALMAP
6422 "localmap",
6423#endif
6424#ifdef FEAT_LUA
6425# ifndef DYNAMIC_LUA
6426 "lua",
6427# endif
6428#endif
6429#ifdef FEAT_MENU
6430 "menu",
6431#endif
6432#ifdef FEAT_SESSION
6433 "mksession",
6434#endif
6435#ifdef FEAT_MODIFY_FNAME
6436 "modify_fname",
6437#endif
6438#ifdef FEAT_MOUSE
6439 "mouse",
6440#endif
6441#ifdef FEAT_MOUSESHAPE
6442 "mouseshape",
6443#endif
6444#if defined(UNIX) || defined(VMS)
6445# ifdef FEAT_MOUSE_DEC
6446 "mouse_dec",
6447# endif
6448# ifdef FEAT_MOUSE_GPM
6449 "mouse_gpm",
6450# endif
6451# ifdef FEAT_MOUSE_JSB
6452 "mouse_jsbterm",
6453# endif
6454# ifdef FEAT_MOUSE_NET
6455 "mouse_netterm",
6456# endif
6457# ifdef FEAT_MOUSE_PTERM
6458 "mouse_pterm",
6459# endif
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01006460# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006461 "mouse_sgr",
6462# endif
6463# ifdef FEAT_SYSMOUSE
6464 "mouse_sysmouse",
6465# endif
6466# ifdef FEAT_MOUSE_URXVT
6467 "mouse_urxvt",
6468# endif
6469# ifdef FEAT_MOUSE_XTERM
6470 "mouse_xterm",
6471# endif
6472#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006473 "multi_byte",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006474#ifdef FEAT_MBYTE_IME
6475 "multi_byte_ime",
6476#endif
6477#ifdef FEAT_MULTI_LANG
6478 "multi_lang",
6479#endif
6480#ifdef FEAT_MZSCHEME
6481#ifndef DYNAMIC_MZSCHEME
6482 "mzscheme",
6483#endif
6484#endif
6485#ifdef FEAT_NUM64
6486 "num64",
6487#endif
6488#ifdef FEAT_OLE
6489 "ole",
6490#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006491#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006492 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006493#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006494#ifdef FEAT_PATH_EXTRA
6495 "path_extra",
6496#endif
6497#ifdef FEAT_PERL
6498#ifndef DYNAMIC_PERL
6499 "perl",
6500#endif
6501#endif
6502#ifdef FEAT_PERSISTENT_UNDO
6503 "persistent_undo",
6504#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006505#if defined(FEAT_PYTHON)
6506 "python_compiled",
6507# if defined(DYNAMIC_PYTHON)
6508 "python_dynamic",
6509# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006510 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006511 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006512# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006513#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006514#if defined(FEAT_PYTHON3)
6515 "python3_compiled",
6516# if defined(DYNAMIC_PYTHON3)
6517 "python3_dynamic",
6518# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006519 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006520 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006521# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006522#endif
6523#ifdef FEAT_POSTSCRIPT
6524 "postscript",
6525#endif
6526#ifdef FEAT_PRINTER
6527 "printer",
6528#endif
6529#ifdef FEAT_PROFILE
6530 "profile",
6531#endif
6532#ifdef FEAT_RELTIME
6533 "reltime",
6534#endif
6535#ifdef FEAT_QUICKFIX
6536 "quickfix",
6537#endif
6538#ifdef FEAT_RIGHTLEFT
6539 "rightleft",
6540#endif
6541#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6542 "ruby",
6543#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006544 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006545#ifdef FEAT_CMDL_INFO
6546 "showcmd",
6547 "cmdline_info",
6548#endif
6549#ifdef FEAT_SIGNS
6550 "signs",
6551#endif
6552#ifdef FEAT_SMARTINDENT
6553 "smartindent",
6554#endif
6555#ifdef STARTUPTIME
6556 "startuptime",
6557#endif
6558#ifdef FEAT_STL_OPT
6559 "statusline",
6560#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006561#ifdef FEAT_NETBEANS_INTG
6562 "netbeans_intg",
6563#endif
6564#ifdef FEAT_SPELL
6565 "spell",
6566#endif
6567#ifdef FEAT_SYN_HL
6568 "syntax",
6569#endif
6570#if defined(USE_SYSTEM) || !defined(UNIX)
6571 "system",
6572#endif
6573#ifdef FEAT_TAG_BINS
6574 "tag_binary",
6575#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006576#ifdef FEAT_TCL
6577# ifndef DYNAMIC_TCL
6578 "tcl",
6579# endif
6580#endif
6581#ifdef FEAT_TERMGUICOLORS
6582 "termguicolors",
6583#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006584#if defined(FEAT_TERMINAL) && !defined(MSWIN)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006585 "terminal",
6586#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006587#ifdef TERMINFO
6588 "terminfo",
6589#endif
6590#ifdef FEAT_TERMRESPONSE
6591 "termresponse",
6592#endif
6593#ifdef FEAT_TEXTOBJ
6594 "textobjects",
6595#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006596#ifdef FEAT_TEXT_PROP
6597 "textprop",
6598#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006599#ifdef HAVE_TGETENT
6600 "tgetent",
6601#endif
6602#ifdef FEAT_TIMERS
6603 "timers",
6604#endif
6605#ifdef FEAT_TITLE
6606 "title",
6607#endif
6608#ifdef FEAT_TOOLBAR
6609 "toolbar",
6610#endif
6611#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6612 "unnamedplus",
6613#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006614 "user-commands", /* was accidentally included in 5.4 */
6615 "user_commands",
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006616#ifdef FEAT_VARTABS
6617 "vartabs",
6618#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006619 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006620#ifdef FEAT_VIMINFO
6621 "viminfo",
6622#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006623 "vimscript-1",
6624 "vimscript-2",
Bram Moolenaar93a48792019-04-20 21:54:28 +02006625 "vimscript-3",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006626 "virtualedit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006627 "visual",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006628 "visualextra",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006629 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006630#ifdef FEAT_VTP
6631 "vtp",
6632#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006633#ifdef FEAT_WILDIGN
6634 "wildignore",
6635#endif
6636#ifdef FEAT_WILDMENU
6637 "wildmenu",
6638#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006639 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006640#ifdef FEAT_WAK
6641 "winaltkeys",
6642#endif
6643#ifdef FEAT_WRITEBACKUP
6644 "writebackup",
6645#endif
6646#ifdef FEAT_XIM
6647 "xim",
6648#endif
6649#ifdef FEAT_XFONTSET
6650 "xfontset",
6651#endif
6652#ifdef FEAT_XPM_W32
6653 "xpm",
6654 "xpm_w32", /* for backward compatibility */
6655#else
6656# if defined(HAVE_XPM)
6657 "xpm",
6658# endif
6659#endif
6660#ifdef USE_XSMP
6661 "xsmp",
6662#endif
6663#ifdef USE_XSMP_INTERACT
6664 "xsmp_interact",
6665#endif
6666#ifdef FEAT_XCLIPBOARD
6667 "xterm_clipboard",
6668#endif
6669#ifdef FEAT_XTERM_SAVE
6670 "xterm_save",
6671#endif
6672#if defined(UNIX) && defined(FEAT_X11)
6673 "X11",
6674#endif
6675 NULL
6676 };
6677
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006678 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006679 for (i = 0; has_list[i] != NULL; ++i)
6680 if (STRICMP(name, has_list[i]) == 0)
6681 {
6682 n = TRUE;
6683 break;
6684 }
6685
6686 if (n == FALSE)
6687 {
6688 if (STRNICMP(name, "patch", 5) == 0)
6689 {
6690 if (name[5] == '-'
6691 && STRLEN(name) >= 11
6692 && vim_isdigit(name[6])
6693 && vim_isdigit(name[8])
6694 && vim_isdigit(name[10]))
6695 {
6696 int major = atoi((char *)name + 6);
6697 int minor = atoi((char *)name + 8);
6698
6699 /* Expect "patch-9.9.01234". */
6700 n = (major < VIM_VERSION_MAJOR
6701 || (major == VIM_VERSION_MAJOR
6702 && (minor < VIM_VERSION_MINOR
6703 || (minor == VIM_VERSION_MINOR
6704 && has_patch(atoi((char *)name + 10))))));
6705 }
6706 else
6707 n = has_patch(atoi((char *)name + 5));
6708 }
6709 else if (STRICMP(name, "vim_starting") == 0)
6710 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006711 else if (STRICMP(name, "ttyin") == 0)
6712 n = mch_input_isatty();
6713 else if (STRICMP(name, "ttyout") == 0)
6714 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006715 else if (STRICMP(name, "multi_byte_encoding") == 0)
6716 n = has_mbyte;
Bram Moolenaar4f974752019-02-17 17:44:42 +01006717#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006718 else if (STRICMP(name, "balloon_multiline") == 0)
6719 n = multiline_balloon_available();
6720#endif
6721#ifdef DYNAMIC_TCL
6722 else if (STRICMP(name, "tcl") == 0)
6723 n = tcl_enabled(FALSE);
6724#endif
6725#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6726 else if (STRICMP(name, "iconv") == 0)
6727 n = iconv_enabled(FALSE);
6728#endif
6729#ifdef DYNAMIC_LUA
6730 else if (STRICMP(name, "lua") == 0)
6731 n = lua_enabled(FALSE);
6732#endif
6733#ifdef DYNAMIC_MZSCHEME
6734 else if (STRICMP(name, "mzscheme") == 0)
6735 n = mzscheme_enabled(FALSE);
6736#endif
6737#ifdef DYNAMIC_RUBY
6738 else if (STRICMP(name, "ruby") == 0)
6739 n = ruby_enabled(FALSE);
6740#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006741#ifdef DYNAMIC_PYTHON
6742 else if (STRICMP(name, "python") == 0)
6743 n = python_enabled(FALSE);
6744#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006745#ifdef DYNAMIC_PYTHON3
6746 else if (STRICMP(name, "python3") == 0)
6747 n = python3_enabled(FALSE);
6748#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006749#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6750 else if (STRICMP(name, "pythonx") == 0)
6751 {
6752# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6753 if (p_pyx == 0)
6754 n = python3_enabled(FALSE) || python_enabled(FALSE);
6755 else if (p_pyx == 3)
6756 n = python3_enabled(FALSE);
6757 else if (p_pyx == 2)
6758 n = python_enabled(FALSE);
6759# elif defined(DYNAMIC_PYTHON)
6760 n = python_enabled(FALSE);
6761# elif defined(DYNAMIC_PYTHON3)
6762 n = python3_enabled(FALSE);
6763# endif
6764 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006765#endif
6766#ifdef DYNAMIC_PERL
6767 else if (STRICMP(name, "perl") == 0)
6768 n = perl_enabled(FALSE);
6769#endif
6770#ifdef FEAT_GUI
6771 else if (STRICMP(name, "gui_running") == 0)
6772 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006773# ifdef FEAT_BROWSE
6774 else if (STRICMP(name, "browse") == 0)
6775 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6776# endif
6777#endif
6778#ifdef FEAT_SYN_HL
6779 else if (STRICMP(name, "syntax_items") == 0)
6780 n = syntax_present(curwin);
6781#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006782#ifdef FEAT_VTP
6783 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02006784 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006785#endif
6786#ifdef FEAT_NETBEANS_INTG
6787 else if (STRICMP(name, "netbeans_enabled") == 0)
6788 n = netbeans_active();
6789#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006790#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02006791 else if (STRICMP(name, "terminal") == 0)
6792 n = terminal_enabled();
6793#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006794#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006795 else if (STRICMP(name, "conpty") == 0)
6796 n = use_conpty();
6797#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006798 }
6799
6800 rettv->vval.v_number = n;
6801}
6802
6803/*
6804 * "has_key()" function
6805 */
6806 static void
6807f_has_key(typval_T *argvars, typval_T *rettv)
6808{
6809 if (argvars[0].v_type != VAR_DICT)
6810 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006811 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006812 return;
6813 }
6814 if (argvars[0].vval.v_dict == NULL)
6815 return;
6816
6817 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006818 tv_get_string(&argvars[1]), -1) != NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006819}
6820
6821/*
6822 * "haslocaldir()" function
6823 */
6824 static void
6825f_haslocaldir(typval_T *argvars, typval_T *rettv)
6826{
6827 win_T *wp = NULL;
6828
6829 wp = find_tabwin(&argvars[0], &argvars[1]);
6830 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6831}
6832
6833/*
6834 * "hasmapto()" function
6835 */
6836 static void
6837f_hasmapto(typval_T *argvars, typval_T *rettv)
6838{
6839 char_u *name;
6840 char_u *mode;
6841 char_u buf[NUMBUFLEN];
6842 int abbr = FALSE;
6843
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006844 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006845 if (argvars[1].v_type == VAR_UNKNOWN)
6846 mode = (char_u *)"nvo";
6847 else
6848 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006849 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006850 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006851 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006852 }
6853
6854 if (map_to_exists(name, mode, abbr))
6855 rettv->vval.v_number = TRUE;
6856 else
6857 rettv->vval.v_number = FALSE;
6858}
6859
6860/*
6861 * "histadd()" function
6862 */
6863 static void
6864f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6865{
6866#ifdef FEAT_CMDHIST
6867 int histype;
6868 char_u *str;
6869 char_u buf[NUMBUFLEN];
6870#endif
6871
6872 rettv->vval.v_number = FALSE;
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006873 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006874 return;
6875#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006876 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006877 histype = str != NULL ? get_histtype(str) : -1;
6878 if (histype >= 0)
6879 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006880 str = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006881 if (*str != NUL)
6882 {
6883 init_history();
6884 add_to_history(histype, str, FALSE, NUL);
6885 rettv->vval.v_number = TRUE;
6886 return;
6887 }
6888 }
6889#endif
6890}
6891
6892/*
6893 * "histdel()" function
6894 */
6895 static void
6896f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6897{
6898#ifdef FEAT_CMDHIST
6899 int n;
6900 char_u buf[NUMBUFLEN];
6901 char_u *str;
6902
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006903 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006904 if (str == NULL)
6905 n = 0;
6906 else if (argvars[1].v_type == VAR_UNKNOWN)
6907 /* only one argument: clear entire history */
6908 n = clr_history(get_histtype(str));
6909 else if (argvars[1].v_type == VAR_NUMBER)
6910 /* index given: remove that entry */
6911 n = del_history_idx(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006912 (int)tv_get_number(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006913 else
6914 /* string given: remove all matching entries */
6915 n = del_history_entry(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006916 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006917 rettv->vval.v_number = n;
6918#endif
6919}
6920
6921/*
6922 * "histget()" function
6923 */
6924 static void
6925f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6926{
6927#ifdef FEAT_CMDHIST
6928 int type;
6929 int idx;
6930 char_u *str;
6931
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006932 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006933 if (str == NULL)
6934 rettv->vval.v_string = NULL;
6935 else
6936 {
6937 type = get_histtype(str);
6938 if (argvars[1].v_type == VAR_UNKNOWN)
6939 idx = get_history_idx(type);
6940 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006941 idx = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006942 /* -1 on type error */
6943 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6944 }
6945#else
6946 rettv->vval.v_string = NULL;
6947#endif
6948 rettv->v_type = VAR_STRING;
6949}
6950
6951/*
6952 * "histnr()" function
6953 */
6954 static void
6955f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6956{
6957 int i;
6958
6959#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006960 char_u *history = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006961
6962 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6963 if (i >= HIST_CMD && i < HIST_COUNT)
6964 i = get_history_idx(i);
6965 else
6966#endif
6967 i = -1;
6968 rettv->vval.v_number = i;
6969}
6970
6971/*
6972 * "highlightID(name)" function
6973 */
6974 static void
6975f_hlID(typval_T *argvars, typval_T *rettv)
6976{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006977 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006978}
6979
6980/*
6981 * "highlight_exists()" function
6982 */
6983 static void
6984f_hlexists(typval_T *argvars, typval_T *rettv)
6985{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006986 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006987}
6988
6989/*
6990 * "hostname()" function
6991 */
6992 static void
6993f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6994{
6995 char_u hostname[256];
6996
6997 mch_get_host_name(hostname, 256);
6998 rettv->v_type = VAR_STRING;
6999 rettv->vval.v_string = vim_strsave(hostname);
7000}
7001
7002/*
7003 * iconv() function
7004 */
7005 static void
7006f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
7007{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007008 char_u buf1[NUMBUFLEN];
7009 char_u buf2[NUMBUFLEN];
7010 char_u *from, *to, *str;
7011 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007012
7013 rettv->v_type = VAR_STRING;
7014 rettv->vval.v_string = NULL;
7015
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007016 str = tv_get_string(&argvars[0]);
7017 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
7018 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007019 vimconv.vc_type = CONV_NONE;
7020 convert_setup(&vimconv, from, to);
7021
7022 /* If the encodings are equal, no conversion needed. */
7023 if (vimconv.vc_type == CONV_NONE)
7024 rettv->vval.v_string = vim_strsave(str);
7025 else
7026 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
7027
7028 convert_setup(&vimconv, NULL, NULL);
7029 vim_free(from);
7030 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007031}
7032
7033/*
7034 * "indent()" function
7035 */
7036 static void
7037f_indent(typval_T *argvars, typval_T *rettv)
7038{
7039 linenr_T lnum;
7040
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007041 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007042 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7043 rettv->vval.v_number = get_indent_lnum(lnum);
7044 else
7045 rettv->vval.v_number = -1;
7046}
7047
7048/*
7049 * "index()" function
7050 */
7051 static void
7052f_index(typval_T *argvars, typval_T *rettv)
7053{
7054 list_T *l;
7055 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007056 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007057 long idx = 0;
7058 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007059 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007060
7061 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007062 if (argvars[0].v_type == VAR_BLOB)
7063 {
7064 typval_T tv;
7065 int start = 0;
7066
7067 if (argvars[2].v_type != VAR_UNKNOWN)
7068 {
7069 start = tv_get_number_chk(&argvars[2], &error);
7070 if (error)
7071 return;
7072 }
7073 b = argvars[0].vval.v_blob;
7074 if (b == NULL)
7075 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01007076 if (start < 0)
7077 {
7078 start = blob_len(b) + start;
7079 if (start < 0)
7080 start = 0;
7081 }
7082
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007083 for (idx = start; idx < blob_len(b); ++idx)
7084 {
7085 tv.v_type = VAR_NUMBER;
7086 tv.vval.v_number = blob_get(b, idx);
7087 if (tv_equal(&tv, &argvars[1], ic, FALSE))
7088 {
7089 rettv->vval.v_number = idx;
7090 return;
7091 }
7092 }
7093 return;
7094 }
7095 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007096 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007097 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007098 return;
7099 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007100
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007101 l = argvars[0].vval.v_list;
7102 if (l != NULL)
7103 {
7104 item = l->lv_first;
7105 if (argvars[2].v_type != VAR_UNKNOWN)
7106 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007107 /* Start at specified item. Use the cached index that list_find()
7108 * sets, so that a negative number also works. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007109 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007110 idx = l->lv_idx;
7111 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007112 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007113 if (error)
7114 item = NULL;
7115 }
7116
7117 for ( ; item != NULL; item = item->li_next, ++idx)
7118 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
7119 {
7120 rettv->vval.v_number = idx;
7121 break;
7122 }
7123 }
7124}
7125
7126static int inputsecret_flag = 0;
7127
7128/*
7129 * "input()" function
7130 * Also handles inputsecret() when inputsecret is set.
7131 */
7132 static void
7133f_input(typval_T *argvars, typval_T *rettv)
7134{
7135 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
7136}
7137
7138/*
7139 * "inputdialog()" function
7140 */
7141 static void
7142f_inputdialog(typval_T *argvars, typval_T *rettv)
7143{
7144#if defined(FEAT_GUI_TEXTDIALOG)
7145 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
7146 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
7147 {
7148 char_u *message;
7149 char_u buf[NUMBUFLEN];
7150 char_u *defstr = (char_u *)"";
7151
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007152 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007153 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007154 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007155 vim_strncpy(IObuff, defstr, IOSIZE - 1);
7156 else
7157 IObuff[0] = NUL;
7158 if (message != NULL && defstr != NULL
7159 && do_dialog(VIM_QUESTION, NULL, message,
7160 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
7161 rettv->vval.v_string = vim_strsave(IObuff);
7162 else
7163 {
7164 if (message != NULL && defstr != NULL
7165 && argvars[1].v_type != VAR_UNKNOWN
7166 && argvars[2].v_type != VAR_UNKNOWN)
7167 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007168 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007169 else
7170 rettv->vval.v_string = NULL;
7171 }
7172 rettv->v_type = VAR_STRING;
7173 }
7174 else
7175#endif
7176 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
7177}
7178
7179/*
7180 * "inputlist()" function
7181 */
7182 static void
7183f_inputlist(typval_T *argvars, typval_T *rettv)
7184{
7185 listitem_T *li;
7186 int selected;
7187 int mouse_used;
7188
7189#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007190 /* While starting up, there is no place to enter text. When running tests
7191 * with --not-a-term we assume feedkeys() will be used. */
7192 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007193 return;
7194#endif
7195 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
7196 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007197 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007198 return;
7199 }
7200
7201 msg_start();
7202 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
7203 lines_left = Rows; /* avoid more prompt */
7204 msg_scroll = TRUE;
7205 msg_clr_eos();
7206
7207 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
7208 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007209 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007210 msg_putchar('\n');
7211 }
7212
7213 /* Ask for choice. */
7214 selected = prompt_for_number(&mouse_used);
7215 if (mouse_used)
7216 selected -= lines_left;
7217
7218 rettv->vval.v_number = selected;
7219}
7220
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007221static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7222
7223/*
7224 * "inputrestore()" function
7225 */
7226 static void
7227f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7228{
7229 if (ga_userinput.ga_len > 0)
7230 {
7231 --ga_userinput.ga_len;
7232 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7233 + ga_userinput.ga_len);
7234 /* default return is zero == OK */
7235 }
7236 else if (p_verbose > 1)
7237 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007238 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007239 rettv->vval.v_number = 1; /* Failed */
7240 }
7241}
7242
7243/*
7244 * "inputsave()" function
7245 */
7246 static void
7247f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7248{
7249 /* Add an entry to the stack of typeahead storage. */
7250 if (ga_grow(&ga_userinput, 1) == OK)
7251 {
7252 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7253 + ga_userinput.ga_len);
7254 ++ga_userinput.ga_len;
7255 /* default return is zero == OK */
7256 }
7257 else
7258 rettv->vval.v_number = 1; /* Failed */
7259}
7260
7261/*
7262 * "inputsecret()" function
7263 */
7264 static void
7265f_inputsecret(typval_T *argvars, typval_T *rettv)
7266{
7267 ++cmdline_star;
7268 ++inputsecret_flag;
7269 f_input(argvars, rettv);
7270 --cmdline_star;
7271 --inputsecret_flag;
7272}
7273
7274/*
7275 * "insert()" function
7276 */
7277 static void
7278f_insert(typval_T *argvars, typval_T *rettv)
7279{
7280 long before = 0;
7281 listitem_T *item;
7282 list_T *l;
7283 int error = FALSE;
7284
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007285 if (argvars[0].v_type == VAR_BLOB)
7286 {
7287 int val, len;
7288 char_u *p;
7289
7290 len = blob_len(argvars[0].vval.v_blob);
7291 if (argvars[2].v_type != VAR_UNKNOWN)
7292 {
7293 before = (long)tv_get_number_chk(&argvars[2], &error);
7294 if (error)
7295 return; // type error; errmsg already given
7296 if (before < 0 || before > len)
7297 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007298 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007299 return;
7300 }
7301 }
7302 val = tv_get_number_chk(&argvars[1], &error);
7303 if (error)
7304 return;
7305 if (val < 0 || val > 255)
7306 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007307 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007308 return;
7309 }
7310
7311 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
7312 return;
7313 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
7314 mch_memmove(p + before + 1, p + before, (size_t)len - before);
7315 *(p + before) = val;
7316 ++argvars[0].vval.v_blob->bv_ga.ga_len;
7317
7318 copy_tv(&argvars[0], rettv);
7319 }
7320 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007321 semsg(_(e_listblobarg), "insert()");
Bram Moolenaar05c00c02019-02-11 22:00:11 +01007322 else if ((l = argvars[0].vval.v_list) != NULL
7323 && !var_check_lock(l->lv_lock,
7324 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007325 {
7326 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007327 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007328 if (error)
7329 return; /* type error; errmsg already given */
7330
7331 if (before == l->lv_len)
7332 item = NULL;
7333 else
7334 {
7335 item = list_find(l, before);
7336 if (item == NULL)
7337 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007338 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007339 l = NULL;
7340 }
7341 }
7342 if (l != NULL)
7343 {
7344 list_insert_tv(l, &argvars[1], item);
7345 copy_tv(&argvars[0], rettv);
7346 }
7347 }
7348}
7349
7350/*
7351 * "invert(expr)" function
7352 */
7353 static void
7354f_invert(typval_T *argvars, typval_T *rettv)
7355{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007356 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007357}
7358
7359/*
7360 * "isdirectory()" function
7361 */
7362 static void
7363f_isdirectory(typval_T *argvars, typval_T *rettv)
7364{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007365 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007366}
7367
7368/*
7369 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7370 * or it refers to a List or Dictionary that is locked.
7371 */
7372 static int
7373tv_islocked(typval_T *tv)
7374{
7375 return (tv->v_lock & VAR_LOCKED)
7376 || (tv->v_type == VAR_LIST
7377 && tv->vval.v_list != NULL
7378 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7379 || (tv->v_type == VAR_DICT
7380 && tv->vval.v_dict != NULL
7381 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7382}
7383
7384/*
7385 * "islocked()" function
7386 */
7387 static void
7388f_islocked(typval_T *argvars, typval_T *rettv)
7389{
7390 lval_T lv;
7391 char_u *end;
7392 dictitem_T *di;
7393
7394 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007395 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007396 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007397 if (end != NULL && lv.ll_name != NULL)
7398 {
7399 if (*end != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007400 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007401 else
7402 {
7403 if (lv.ll_tv == NULL)
7404 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007405 di = find_var(lv.ll_name, NULL, TRUE);
7406 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007407 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007408 /* Consider a variable locked when:
7409 * 1. the variable itself is locked
7410 * 2. the value of the variable is locked.
7411 * 3. the List or Dict value is locked.
7412 */
7413 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7414 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007415 }
7416 }
7417 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007418 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007419 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007420 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007421 else if (lv.ll_list != NULL)
7422 /* List item. */
7423 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7424 else
7425 /* Dictionary item. */
7426 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7427 }
7428 }
7429
7430 clear_lval(&lv);
7431}
7432
7433#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7434/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02007435 * "isinf()" function
7436 */
7437 static void
7438f_isinf(typval_T *argvars, typval_T *rettv)
7439{
7440 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
7441 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
7442}
7443
7444/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007445 * "isnan()" function
7446 */
7447 static void
7448f_isnan(typval_T *argvars, typval_T *rettv)
7449{
7450 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7451 && isnan(argvars[0].vval.v_float);
7452}
7453#endif
7454
7455/*
7456 * "items(dict)" function
7457 */
7458 static void
7459f_items(typval_T *argvars, typval_T *rettv)
7460{
7461 dict_list(argvars, rettv, 2);
7462}
7463
7464#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
7465/*
7466 * Get the job from the argument.
7467 * Returns NULL if the job is invalid.
7468 */
7469 static job_T *
7470get_job_arg(typval_T *tv)
7471{
7472 job_T *job;
7473
7474 if (tv->v_type != VAR_JOB)
7475 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007476 semsg(_(e_invarg2), tv_get_string(tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007477 return NULL;
7478 }
7479 job = tv->vval.v_job;
7480
7481 if (job == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007482 emsg(_("E916: not a valid job"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007483 return job;
7484}
7485
7486/*
7487 * "job_getchannel()" function
7488 */
7489 static void
7490f_job_getchannel(typval_T *argvars, typval_T *rettv)
7491{
7492 job_T *job = get_job_arg(&argvars[0]);
7493
7494 if (job != NULL)
7495 {
7496 rettv->v_type = VAR_CHANNEL;
7497 rettv->vval.v_channel = job->jv_channel;
7498 if (job->jv_channel != NULL)
7499 ++job->jv_channel->ch_refcount;
7500 }
7501}
7502
7503/*
7504 * "job_info()" function
7505 */
7506 static void
7507f_job_info(typval_T *argvars, typval_T *rettv)
7508{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007509 if (argvars[0].v_type != VAR_UNKNOWN)
7510 {
7511 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007512
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007513 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7514 job_info(job, rettv->vval.v_dict);
7515 }
7516 else if (rettv_list_alloc(rettv) == OK)
7517 job_info_all(rettv->vval.v_list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007518}
7519
7520/*
7521 * "job_setoptions()" function
7522 */
7523 static void
7524f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7525{
7526 job_T *job = get_job_arg(&argvars[0]);
7527 jobopt_T opt;
7528
7529 if (job == NULL)
7530 return;
7531 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007532 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007533 job_set_options(job, &opt);
7534 free_job_options(&opt);
7535}
7536
7537/*
7538 * "job_start()" function
7539 */
7540 static void
7541f_job_start(typval_T *argvars, typval_T *rettv)
7542{
7543 rettv->v_type = VAR_JOB;
7544 if (check_restricted() || check_secure())
7545 return;
Bram Moolenaar493359e2018-06-12 20:25:52 +02007546 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007547}
7548
7549/*
7550 * "job_status()" function
7551 */
7552 static void
7553f_job_status(typval_T *argvars, typval_T *rettv)
7554{
7555 job_T *job = get_job_arg(&argvars[0]);
7556
7557 if (job != NULL)
7558 {
7559 rettv->v_type = VAR_STRING;
7560 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7561 }
7562}
7563
7564/*
7565 * "job_stop()" function
7566 */
7567 static void
7568f_job_stop(typval_T *argvars, typval_T *rettv)
7569{
7570 job_T *job = get_job_arg(&argvars[0]);
7571
7572 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007573 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007574}
7575#endif
7576
7577/*
7578 * "join()" function
7579 */
7580 static void
7581f_join(typval_T *argvars, typval_T *rettv)
7582{
7583 garray_T ga;
7584 char_u *sep;
7585
7586 if (argvars[0].v_type != VAR_LIST)
7587 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007588 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007589 return;
7590 }
7591 if (argvars[0].vval.v_list == NULL)
7592 return;
7593 if (argvars[1].v_type == VAR_UNKNOWN)
7594 sep = (char_u *)" ";
7595 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007596 sep = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007597
7598 rettv->v_type = VAR_STRING;
7599
7600 if (sep != NULL)
7601 {
7602 ga_init2(&ga, (int)sizeof(char), 80);
7603 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7604 ga_append(&ga, NUL);
7605 rettv->vval.v_string = (char_u *)ga.ga_data;
7606 }
7607 else
7608 rettv->vval.v_string = NULL;
7609}
7610
7611/*
7612 * "js_decode()" function
7613 */
7614 static void
7615f_js_decode(typval_T *argvars, typval_T *rettv)
7616{
7617 js_read_T reader;
7618
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007619 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007620 reader.js_fill = NULL;
7621 reader.js_used = 0;
7622 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007623 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007624}
7625
7626/*
7627 * "js_encode()" function
7628 */
7629 static void
7630f_js_encode(typval_T *argvars, typval_T *rettv)
7631{
7632 rettv->v_type = VAR_STRING;
7633 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7634}
7635
7636/*
7637 * "json_decode()" function
7638 */
7639 static void
7640f_json_decode(typval_T *argvars, typval_T *rettv)
7641{
7642 js_read_T reader;
7643
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007644 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007645 reader.js_fill = NULL;
7646 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007647 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007648}
7649
7650/*
7651 * "json_encode()" function
7652 */
7653 static void
7654f_json_encode(typval_T *argvars, typval_T *rettv)
7655{
7656 rettv->v_type = VAR_STRING;
7657 rettv->vval.v_string = json_encode(&argvars[0], 0);
7658}
7659
7660/*
7661 * "keys()" function
7662 */
7663 static void
7664f_keys(typval_T *argvars, typval_T *rettv)
7665{
7666 dict_list(argvars, rettv, 0);
7667}
7668
7669/*
7670 * "last_buffer_nr()" function.
7671 */
7672 static void
7673f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7674{
7675 int n = 0;
7676 buf_T *buf;
7677
Bram Moolenaar29323592016-07-24 22:04:11 +02007678 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007679 if (n < buf->b_fnum)
7680 n = buf->b_fnum;
7681
7682 rettv->vval.v_number = n;
7683}
7684
7685/*
7686 * "len()" function
7687 */
7688 static void
7689f_len(typval_T *argvars, typval_T *rettv)
7690{
7691 switch (argvars[0].v_type)
7692 {
7693 case VAR_STRING:
7694 case VAR_NUMBER:
7695 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007696 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007697 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007698 case VAR_BLOB:
7699 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
7700 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007701 case VAR_LIST:
7702 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7703 break;
7704 case VAR_DICT:
7705 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7706 break;
7707 case VAR_UNKNOWN:
7708 case VAR_SPECIAL:
7709 case VAR_FLOAT:
7710 case VAR_FUNC:
7711 case VAR_PARTIAL:
7712 case VAR_JOB:
7713 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007714 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007715 break;
7716 }
7717}
7718
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007719 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007720libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007721{
7722#ifdef FEAT_LIBCALL
7723 char_u *string_in;
7724 char_u **string_result;
7725 int nr_result;
7726#endif
7727
7728 rettv->v_type = type;
7729 if (type != VAR_NUMBER)
7730 rettv->vval.v_string = NULL;
7731
7732 if (check_restricted() || check_secure())
7733 return;
7734
7735#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007736 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007737 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7738 {
7739 string_in = NULL;
7740 if (argvars[2].v_type == VAR_STRING)
7741 string_in = argvars[2].vval.v_string;
7742 if (type == VAR_NUMBER)
7743 string_result = NULL;
7744 else
7745 string_result = &rettv->vval.v_string;
7746 if (mch_libcall(argvars[0].vval.v_string,
7747 argvars[1].vval.v_string,
7748 string_in,
7749 argvars[2].vval.v_number,
7750 string_result,
7751 &nr_result) == OK
7752 && type == VAR_NUMBER)
7753 rettv->vval.v_number = nr_result;
7754 }
7755#endif
7756}
7757
7758/*
7759 * "libcall()" function
7760 */
7761 static void
7762f_libcall(typval_T *argvars, typval_T *rettv)
7763{
7764 libcall_common(argvars, rettv, VAR_STRING);
7765}
7766
7767/*
7768 * "libcallnr()" function
7769 */
7770 static void
7771f_libcallnr(typval_T *argvars, typval_T *rettv)
7772{
7773 libcall_common(argvars, rettv, VAR_NUMBER);
7774}
7775
7776/*
7777 * "line(string)" function
7778 */
7779 static void
7780f_line(typval_T *argvars, typval_T *rettv)
7781{
7782 linenr_T lnum = 0;
7783 pos_T *fp;
7784 int fnum;
7785
7786 fp = var2fpos(&argvars[0], TRUE, &fnum);
7787 if (fp != NULL)
7788 lnum = fp->lnum;
7789 rettv->vval.v_number = lnum;
7790}
7791
7792/*
7793 * "line2byte(lnum)" function
7794 */
7795 static void
7796f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7797{
7798#ifndef FEAT_BYTEOFF
7799 rettv->vval.v_number = -1;
7800#else
7801 linenr_T lnum;
7802
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007803 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007804 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7805 rettv->vval.v_number = -1;
7806 else
7807 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7808 if (rettv->vval.v_number >= 0)
7809 ++rettv->vval.v_number;
7810#endif
7811}
7812
7813/*
7814 * "lispindent(lnum)" function
7815 */
7816 static void
7817f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7818{
7819#ifdef FEAT_LISP
7820 pos_T pos;
7821 linenr_T lnum;
7822
7823 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007824 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007825 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7826 {
7827 curwin->w_cursor.lnum = lnum;
7828 rettv->vval.v_number = get_lisp_indent();
7829 curwin->w_cursor = pos;
7830 }
7831 else
7832#endif
7833 rettv->vval.v_number = -1;
7834}
7835
7836/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02007837 * "list2str()" function
7838 */
7839 static void
7840f_list2str(typval_T *argvars, typval_T *rettv)
7841{
7842 list_T *l;
7843 listitem_T *li;
7844 garray_T ga;
7845 int utf8 = FALSE;
7846
7847 rettv->v_type = VAR_STRING;
7848 rettv->vval.v_string = NULL;
7849 if (argvars[0].v_type != VAR_LIST)
7850 {
7851 emsg(_(e_invarg));
7852 return;
7853 }
7854
7855 l = argvars[0].vval.v_list;
7856 if (l == NULL)
7857 return; // empty list results in empty string
7858
7859 if (argvars[1].v_type != VAR_UNKNOWN)
7860 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
7861
7862 ga_init2(&ga, 1, 80);
7863 if (has_mbyte || utf8)
7864 {
7865 char_u buf[MB_MAXBYTES + 1];
7866 int (*char2bytes)(int, char_u *);
7867
7868 if (utf8 || enc_utf8)
7869 char2bytes = utf_char2bytes;
7870 else
7871 char2bytes = mb_char2bytes;
7872
7873 for (li = l->lv_first; li != NULL; li = li->li_next)
7874 {
7875 buf[(*char2bytes)(tv_get_number(&li->li_tv), buf)] = NUL;
7876 ga_concat(&ga, buf);
7877 }
7878 ga_append(&ga, NUL);
7879 }
7880 else if (ga_grow(&ga, list_len(l) + 1) == OK)
7881 {
7882 for (li = l->lv_first; li != NULL; li = li->li_next)
7883 ga_append(&ga, tv_get_number(&li->li_tv));
7884 ga_append(&ga, NUL);
7885 }
7886
7887 rettv->v_type = VAR_STRING;
7888 rettv->vval.v_string = ga.ga_data;
7889}
7890
7891/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007892 * "localtime()" function
7893 */
7894 static void
7895f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7896{
7897 rettv->vval.v_number = (varnumber_T)time(NULL);
7898}
7899
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007900 static void
7901get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7902{
7903 char_u *keys;
7904 char_u *which;
7905 char_u buf[NUMBUFLEN];
7906 char_u *keys_buf = NULL;
7907 char_u *rhs;
7908 int mode;
7909 int abbr = FALSE;
7910 int get_dict = FALSE;
7911 mapblock_T *mp;
7912 int buffer_local;
7913
7914 /* return empty string for failure */
7915 rettv->v_type = VAR_STRING;
7916 rettv->vval.v_string = NULL;
7917
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007918 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007919 if (*keys == NUL)
7920 return;
7921
7922 if (argvars[1].v_type != VAR_UNKNOWN)
7923 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007924 which = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007925 if (argvars[2].v_type != VAR_UNKNOWN)
7926 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007927 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007928 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007929 get_dict = (int)tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007930 }
7931 }
7932 else
7933 which = (char_u *)"";
7934 if (which == NULL)
7935 return;
7936
7937 mode = get_map_mode(&which, 0);
7938
7939 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7940 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7941 vim_free(keys_buf);
7942
7943 if (!get_dict)
7944 {
7945 /* Return a string. */
7946 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02007947 {
7948 if (*rhs == NUL)
7949 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
7950 else
7951 rettv->vval.v_string = str2special_save(rhs, FALSE);
7952 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007953
7954 }
7955 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7956 {
7957 /* Return a dictionary. */
7958 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7959 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7960 dict_T *dict = rettv->vval.v_dict;
7961
Bram Moolenaare0be1672018-07-08 16:50:37 +02007962 dict_add_string(dict, "lhs", lhs);
7963 dict_add_string(dict, "rhs", mp->m_orig_str);
7964 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
7965 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
7966 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007967 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
7968 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02007969 dict_add_number(dict, "buffer", (long)buffer_local);
7970 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
7971 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007972
7973 vim_free(lhs);
7974 vim_free(mapmode);
7975 }
7976}
7977
7978#ifdef FEAT_FLOAT
7979/*
7980 * "log()" function
7981 */
7982 static void
7983f_log(typval_T *argvars, typval_T *rettv)
7984{
7985 float_T f = 0.0;
7986
7987 rettv->v_type = VAR_FLOAT;
7988 if (get_float_arg(argvars, &f) == OK)
7989 rettv->vval.v_float = log(f);
7990 else
7991 rettv->vval.v_float = 0.0;
7992}
7993
7994/*
7995 * "log10()" function
7996 */
7997 static void
7998f_log10(typval_T *argvars, typval_T *rettv)
7999{
8000 float_T f = 0.0;
8001
8002 rettv->v_type = VAR_FLOAT;
8003 if (get_float_arg(argvars, &f) == OK)
8004 rettv->vval.v_float = log10(f);
8005 else
8006 rettv->vval.v_float = 0.0;
8007}
8008#endif
8009
8010#ifdef FEAT_LUA
8011/*
8012 * "luaeval()" function
8013 */
8014 static void
8015f_luaeval(typval_T *argvars, typval_T *rettv)
8016{
8017 char_u *str;
8018 char_u buf[NUMBUFLEN];
8019
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008020 if (check_restricted() || check_secure())
8021 return;
8022
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008023 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008024 do_luaeval(str, argvars + 1, rettv);
8025}
8026#endif
8027
8028/*
8029 * "map()" function
8030 */
8031 static void
8032f_map(typval_T *argvars, typval_T *rettv)
8033{
8034 filter_map(argvars, rettv, TRUE);
8035}
8036
8037/*
8038 * "maparg()" function
8039 */
8040 static void
8041f_maparg(typval_T *argvars, typval_T *rettv)
8042{
8043 get_maparg(argvars, rettv, TRUE);
8044}
8045
8046/*
8047 * "mapcheck()" function
8048 */
8049 static void
8050f_mapcheck(typval_T *argvars, typval_T *rettv)
8051{
8052 get_maparg(argvars, rettv, FALSE);
8053}
8054
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008055typedef enum
8056{
8057 MATCH_END, /* matchend() */
8058 MATCH_MATCH, /* match() */
8059 MATCH_STR, /* matchstr() */
8060 MATCH_LIST, /* matchlist() */
8061 MATCH_POS /* matchstrpos() */
8062} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008063
8064 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008065find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008066{
8067 char_u *str = NULL;
8068 long len = 0;
8069 char_u *expr = NULL;
8070 char_u *pat;
8071 regmatch_T regmatch;
8072 char_u patbuf[NUMBUFLEN];
8073 char_u strbuf[NUMBUFLEN];
8074 char_u *save_cpo;
8075 long start = 0;
8076 long nth = 1;
8077 colnr_T startcol = 0;
8078 int match = 0;
8079 list_T *l = NULL;
8080 listitem_T *li = NULL;
8081 long idx = 0;
8082 char_u *tofree = NULL;
8083
8084 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
8085 save_cpo = p_cpo;
8086 p_cpo = (char_u *)"";
8087
8088 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008089 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008090 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008091 /* type MATCH_LIST: return empty list when there are no matches.
8092 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008093 if (rettv_list_alloc(rettv) == FAIL)
8094 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008095 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008096 && (list_append_string(rettv->vval.v_list,
8097 (char_u *)"", 0) == FAIL
8098 || list_append_number(rettv->vval.v_list,
8099 (varnumber_T)-1) == FAIL
8100 || list_append_number(rettv->vval.v_list,
8101 (varnumber_T)-1) == FAIL
8102 || list_append_number(rettv->vval.v_list,
8103 (varnumber_T)-1) == FAIL))
8104 {
8105 list_free(rettv->vval.v_list);
8106 rettv->vval.v_list = NULL;
8107 goto theend;
8108 }
8109 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008110 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008111 {
8112 rettv->v_type = VAR_STRING;
8113 rettv->vval.v_string = NULL;
8114 }
8115
8116 if (argvars[0].v_type == VAR_LIST)
8117 {
8118 if ((l = argvars[0].vval.v_list) == NULL)
8119 goto theend;
8120 li = l->lv_first;
8121 }
8122 else
8123 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008124 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008125 len = (long)STRLEN(str);
8126 }
8127
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008128 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008129 if (pat == NULL)
8130 goto theend;
8131
8132 if (argvars[2].v_type != VAR_UNKNOWN)
8133 {
8134 int error = FALSE;
8135
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008136 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008137 if (error)
8138 goto theend;
8139 if (l != NULL)
8140 {
8141 li = list_find(l, start);
8142 if (li == NULL)
8143 goto theend;
8144 idx = l->lv_idx; /* use the cached index */
8145 }
8146 else
8147 {
8148 if (start < 0)
8149 start = 0;
8150 if (start > len)
8151 goto theend;
8152 /* When "count" argument is there ignore matches before "start",
8153 * otherwise skip part of the string. Differs when pattern is "^"
8154 * or "\<". */
8155 if (argvars[3].v_type != VAR_UNKNOWN)
8156 startcol = start;
8157 else
8158 {
8159 str += start;
8160 len -= start;
8161 }
8162 }
8163
8164 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008165 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008166 if (error)
8167 goto theend;
8168 }
8169
8170 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8171 if (regmatch.regprog != NULL)
8172 {
8173 regmatch.rm_ic = p_ic;
8174
8175 for (;;)
8176 {
8177 if (l != NULL)
8178 {
8179 if (li == NULL)
8180 {
8181 match = FALSE;
8182 break;
8183 }
8184 vim_free(tofree);
8185 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
8186 if (str == NULL)
8187 break;
8188 }
8189
8190 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
8191
8192 if (match && --nth <= 0)
8193 break;
8194 if (l == NULL && !match)
8195 break;
8196
8197 /* Advance to just after the match. */
8198 if (l != NULL)
8199 {
8200 li = li->li_next;
8201 ++idx;
8202 }
8203 else
8204 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008205 startcol = (colnr_T)(regmatch.startp[0]
8206 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008207 if (startcol > (colnr_T)len
8208 || str + startcol <= regmatch.startp[0])
8209 {
8210 match = FALSE;
8211 break;
8212 }
8213 }
8214 }
8215
8216 if (match)
8217 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008218 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008219 {
8220 listitem_T *li1 = rettv->vval.v_list->lv_first;
8221 listitem_T *li2 = li1->li_next;
8222 listitem_T *li3 = li2->li_next;
8223 listitem_T *li4 = li3->li_next;
8224
8225 vim_free(li1->li_tv.vval.v_string);
8226 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
8227 (int)(regmatch.endp[0] - regmatch.startp[0]));
8228 li3->li_tv.vval.v_number =
8229 (varnumber_T)(regmatch.startp[0] - expr);
8230 li4->li_tv.vval.v_number =
8231 (varnumber_T)(regmatch.endp[0] - expr);
8232 if (l != NULL)
8233 li2->li_tv.vval.v_number = (varnumber_T)idx;
8234 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008235 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008236 {
8237 int i;
8238
8239 /* return list with matched string and submatches */
8240 for (i = 0; i < NSUBEXP; ++i)
8241 {
8242 if (regmatch.endp[i] == NULL)
8243 {
8244 if (list_append_string(rettv->vval.v_list,
8245 (char_u *)"", 0) == FAIL)
8246 break;
8247 }
8248 else if (list_append_string(rettv->vval.v_list,
8249 regmatch.startp[i],
8250 (int)(regmatch.endp[i] - regmatch.startp[i]))
8251 == FAIL)
8252 break;
8253 }
8254 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008255 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008256 {
8257 /* return matched string */
8258 if (l != NULL)
8259 copy_tv(&li->li_tv, rettv);
8260 else
8261 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
8262 (int)(regmatch.endp[0] - regmatch.startp[0]));
8263 }
8264 else if (l != NULL)
8265 rettv->vval.v_number = idx;
8266 else
8267 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008268 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008269 rettv->vval.v_number =
8270 (varnumber_T)(regmatch.startp[0] - str);
8271 else
8272 rettv->vval.v_number =
8273 (varnumber_T)(regmatch.endp[0] - str);
8274 rettv->vval.v_number += (varnumber_T)(str - expr);
8275 }
8276 }
8277 vim_regfree(regmatch.regprog);
8278 }
8279
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008280theend:
8281 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008282 /* matchstrpos() without a list: drop the second item. */
8283 listitem_remove(rettv->vval.v_list,
8284 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008285 vim_free(tofree);
8286 p_cpo = save_cpo;
8287}
8288
8289/*
8290 * "match()" function
8291 */
8292 static void
8293f_match(typval_T *argvars, typval_T *rettv)
8294{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008295 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008296}
8297
Bram Moolenaar95e51472018-07-28 16:55:56 +02008298#ifdef FEAT_SEARCH_EXTRA
8299 static int
8300matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
8301{
8302 dictitem_T *di;
8303
8304 if (tv->v_type != VAR_DICT)
8305 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008306 emsg(_(e_dictreq));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008307 return FAIL;
8308 }
8309
8310 if (dict_find(tv->vval.v_dict, (char_u *)"conceal", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01008311 *conceal_char = dict_get_string(tv->vval.v_dict,
Bram Moolenaar95e51472018-07-28 16:55:56 +02008312 (char_u *)"conceal", FALSE);
8313
8314 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
8315 {
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02008316 *win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008317 if (*win == NULL)
8318 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01008319 emsg(_(e_invalwindow));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008320 return FAIL;
8321 }
8322 }
8323
8324 return OK;
8325}
8326#endif
8327
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008328/*
8329 * "matchadd()" function
8330 */
8331 static void
8332f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8333{
8334#ifdef FEAT_SEARCH_EXTRA
8335 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008336 char_u *grp = tv_get_string_buf_chk(&argvars[0], buf); /* group */
8337 char_u *pat = tv_get_string_buf_chk(&argvars[1], buf); /* pattern */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008338 int prio = 10; /* default priority */
8339 int id = -1;
8340 int error = FALSE;
8341 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008342 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008343
8344 rettv->vval.v_number = -1;
8345
8346 if (grp == NULL || pat == NULL)
8347 return;
8348 if (argvars[2].v_type != VAR_UNKNOWN)
8349 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008350 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008351 if (argvars[3].v_type != VAR_UNKNOWN)
8352 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008353 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008354 if (argvars[4].v_type != VAR_UNKNOWN
8355 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8356 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008357 }
8358 }
8359 if (error == TRUE)
8360 return;
8361 if (id >= 1 && id <= 3)
8362 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008363 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008364 return;
8365 }
8366
Bram Moolenaar95e51472018-07-28 16:55:56 +02008367 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008368 conceal_char);
8369#endif
8370}
8371
8372/*
8373 * "matchaddpos()" function
8374 */
8375 static void
8376f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8377{
8378#ifdef FEAT_SEARCH_EXTRA
8379 char_u buf[NUMBUFLEN];
8380 char_u *group;
8381 int prio = 10;
8382 int id = -1;
8383 int error = FALSE;
8384 list_T *l;
8385 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008386 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008387
8388 rettv->vval.v_number = -1;
8389
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008390 group = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008391 if (group == NULL)
8392 return;
8393
8394 if (argvars[1].v_type != VAR_LIST)
8395 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008396 semsg(_(e_listarg), "matchaddpos()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008397 return;
8398 }
8399 l = argvars[1].vval.v_list;
8400 if (l == NULL)
8401 return;
8402
8403 if (argvars[2].v_type != VAR_UNKNOWN)
8404 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008405 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008406 if (argvars[3].v_type != VAR_UNKNOWN)
8407 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008408 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008409
8410 if (argvars[4].v_type != VAR_UNKNOWN
8411 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8412 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008413 }
8414 }
8415 if (error == TRUE)
8416 return;
8417
8418 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8419 if (id == 1 || id == 2)
8420 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008421 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008422 return;
8423 }
8424
Bram Moolenaar95e51472018-07-28 16:55:56 +02008425 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008426 conceal_char);
8427#endif
8428}
8429
8430/*
8431 * "matcharg()" function
8432 */
8433 static void
8434f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8435{
8436 if (rettv_list_alloc(rettv) == OK)
8437 {
8438#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008439 int id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008440 matchitem_T *m;
8441
8442 if (id >= 1 && id <= 3)
8443 {
8444 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8445 {
8446 list_append_string(rettv->vval.v_list,
8447 syn_id2name(m->hlg_id), -1);
8448 list_append_string(rettv->vval.v_list, m->pattern, -1);
8449 }
8450 else
8451 {
8452 list_append_string(rettv->vval.v_list, NULL, -1);
8453 list_append_string(rettv->vval.v_list, NULL, -1);
8454 }
8455 }
8456#endif
8457 }
8458}
8459
8460/*
8461 * "matchdelete()" function
8462 */
8463 static void
8464f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8465{
8466#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01008467 win_T *win = get_optional_window(argvars, 1);
8468
8469 if (win == NULL)
8470 rettv->vval.v_number = -1;
8471 else
8472 rettv->vval.v_number = match_delete(win,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008473 (int)tv_get_number(&argvars[0]), TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008474#endif
8475}
8476
8477/*
8478 * "matchend()" function
8479 */
8480 static void
8481f_matchend(typval_T *argvars, typval_T *rettv)
8482{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008483 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008484}
8485
8486/*
8487 * "matchlist()" function
8488 */
8489 static void
8490f_matchlist(typval_T *argvars, typval_T *rettv)
8491{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008492 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008493}
8494
8495/*
8496 * "matchstr()" function
8497 */
8498 static void
8499f_matchstr(typval_T *argvars, typval_T *rettv)
8500{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008501 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008502}
8503
8504/*
8505 * "matchstrpos()" function
8506 */
8507 static void
8508f_matchstrpos(typval_T *argvars, typval_T *rettv)
8509{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008510 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008511}
8512
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008513 static void
8514max_min(typval_T *argvars, typval_T *rettv, int domax)
8515{
8516 varnumber_T n = 0;
8517 varnumber_T i;
8518 int error = FALSE;
8519
8520 if (argvars[0].v_type == VAR_LIST)
8521 {
8522 list_T *l;
8523 listitem_T *li;
8524
8525 l = argvars[0].vval.v_list;
8526 if (l != NULL)
8527 {
8528 li = l->lv_first;
8529 if (li != NULL)
8530 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008531 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008532 for (;;)
8533 {
8534 li = li->li_next;
8535 if (li == NULL)
8536 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008537 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008538 if (domax ? i > n : i < n)
8539 n = i;
8540 }
8541 }
8542 }
8543 }
8544 else if (argvars[0].v_type == VAR_DICT)
8545 {
8546 dict_T *d;
8547 int first = TRUE;
8548 hashitem_T *hi;
8549 int todo;
8550
8551 d = argvars[0].vval.v_dict;
8552 if (d != NULL)
8553 {
8554 todo = (int)d->dv_hashtab.ht_used;
8555 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8556 {
8557 if (!HASHITEM_EMPTY(hi))
8558 {
8559 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008560 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008561 if (first)
8562 {
8563 n = i;
8564 first = FALSE;
8565 }
8566 else if (domax ? i > n : i < n)
8567 n = i;
8568 }
8569 }
8570 }
8571 }
8572 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008573 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008574 rettv->vval.v_number = error ? 0 : n;
8575}
8576
8577/*
8578 * "max()" function
8579 */
8580 static void
8581f_max(typval_T *argvars, typval_T *rettv)
8582{
8583 max_min(argvars, rettv, TRUE);
8584}
8585
8586/*
8587 * "min()" function
8588 */
8589 static void
8590f_min(typval_T *argvars, typval_T *rettv)
8591{
8592 max_min(argvars, rettv, FALSE);
8593}
8594
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008595/*
8596 * Create the directory in which "dir" is located, and higher levels when
8597 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008598 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008599 */
8600 static int
8601mkdir_recurse(char_u *dir, int prot)
8602{
8603 char_u *p;
8604 char_u *updir;
8605 int r = FAIL;
8606
8607 /* Get end of directory name in "dir".
8608 * We're done when it's "/" or "c:/". */
8609 p = gettail_sep(dir);
8610 if (p <= get_past_head(dir))
8611 return OK;
8612
8613 /* If the directory exists we're done. Otherwise: create it.*/
8614 updir = vim_strnsave(dir, (int)(p - dir));
8615 if (updir == NULL)
8616 return FAIL;
8617 if (mch_isdir(updir))
8618 r = OK;
8619 else if (mkdir_recurse(updir, prot) == OK)
8620 r = vim_mkdir_emsg(updir, prot);
8621 vim_free(updir);
8622 return r;
8623}
8624
8625#ifdef vim_mkdir
8626/*
8627 * "mkdir()" function
8628 */
8629 static void
8630f_mkdir(typval_T *argvars, typval_T *rettv)
8631{
8632 char_u *dir;
8633 char_u buf[NUMBUFLEN];
8634 int prot = 0755;
8635
8636 rettv->vval.v_number = FAIL;
8637 if (check_restricted() || check_secure())
8638 return;
8639
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008640 dir = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008641 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008642 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008643
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008644 if (*gettail(dir) == NUL)
8645 /* remove trailing slashes */
8646 *gettail_sep(dir) = NUL;
8647
8648 if (argvars[1].v_type != VAR_UNKNOWN)
8649 {
8650 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008651 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008652 prot = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008653 if (prot == -1)
8654 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008655 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008656 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008657 {
8658 if (mch_isdir(dir))
8659 {
8660 /* With the "p" flag it's OK if the dir already exists. */
8661 rettv->vval.v_number = OK;
8662 return;
8663 }
8664 mkdir_recurse(dir, prot);
8665 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008666 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008667 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008668}
8669#endif
8670
8671/*
8672 * "mode()" function
8673 */
8674 static void
8675f_mode(typval_T *argvars, typval_T *rettv)
8676{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008677 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008678
Bram Moolenaar612cc382018-07-29 15:34:26 +02008679 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008680
8681 if (time_for_testing == 93784)
8682 {
8683 /* Testing the two-character code. */
8684 buf[0] = 'x';
8685 buf[1] = '!';
8686 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008687#ifdef FEAT_TERMINAL
8688 else if (term_use_loop())
8689 buf[0] = 't';
8690#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008691 else if (VIsual_active)
8692 {
8693 if (VIsual_select)
8694 buf[0] = VIsual_mode + 's' - 'v';
8695 else
8696 buf[0] = VIsual_mode;
8697 }
8698 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8699 || State == CONFIRM)
8700 {
8701 buf[0] = 'r';
8702 if (State == ASKMORE)
8703 buf[1] = 'm';
8704 else if (State == CONFIRM)
8705 buf[1] = '?';
8706 }
8707 else if (State == EXTERNCMD)
8708 buf[0] = '!';
8709 else if (State & INSERT)
8710 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008711 if (State & VREPLACE_FLAG)
8712 {
8713 buf[0] = 'R';
8714 buf[1] = 'v';
8715 }
8716 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008717 {
8718 if (State & REPLACE_FLAG)
8719 buf[0] = 'R';
8720 else
8721 buf[0] = 'i';
8722#ifdef FEAT_INS_EXPAND
8723 if (ins_compl_active())
8724 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008725 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008726 buf[1] = 'x';
8727#endif
8728 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008729 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008730 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008731 {
8732 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008733 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008734 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008735 else if (exmode_active == EXMODE_NORMAL)
8736 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008737 }
8738 else
8739 {
8740 buf[0] = 'n';
8741 if (finish_op)
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008742 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008743 buf[1] = 'o';
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008744 // to be able to detect force-linewise/blockwise/characterwise operations
8745 buf[2] = motion_force;
8746 }
Bram Moolenaar612cc382018-07-29 15:34:26 +02008747 else if (restart_edit == 'I' || restart_edit == 'R'
8748 || restart_edit == 'V')
8749 {
8750 buf[1] = 'i';
8751 buf[2] = restart_edit;
8752 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008753 }
8754
8755 /* Clear out the minor mode when the argument is not a non-zero number or
8756 * non-empty string. */
8757 if (!non_zero_arg(&argvars[0]))
8758 buf[1] = NUL;
8759
8760 rettv->vval.v_string = vim_strsave(buf);
8761 rettv->v_type = VAR_STRING;
8762}
8763
8764#if defined(FEAT_MZSCHEME) || defined(PROTO)
8765/*
8766 * "mzeval()" function
8767 */
8768 static void
8769f_mzeval(typval_T *argvars, typval_T *rettv)
8770{
8771 char_u *str;
8772 char_u buf[NUMBUFLEN];
8773
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008774 if (check_restricted() || check_secure())
8775 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008776 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008777 do_mzeval(str, rettv);
8778}
8779
8780 void
8781mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8782{
8783 typval_T argvars[3];
8784
8785 argvars[0].v_type = VAR_STRING;
8786 argvars[0].vval.v_string = name;
8787 copy_tv(args, &argvars[1]);
8788 argvars[2].v_type = VAR_UNKNOWN;
8789 f_call(argvars, rettv);
8790 clear_tv(&argvars[1]);
8791}
8792#endif
8793
8794/*
8795 * "nextnonblank()" function
8796 */
8797 static void
8798f_nextnonblank(typval_T *argvars, typval_T *rettv)
8799{
8800 linenr_T lnum;
8801
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008802 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008803 {
8804 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8805 {
8806 lnum = 0;
8807 break;
8808 }
8809 if (*skipwhite(ml_get(lnum)) != NUL)
8810 break;
8811 }
8812 rettv->vval.v_number = lnum;
8813}
8814
8815/*
8816 * "nr2char()" function
8817 */
8818 static void
8819f_nr2char(typval_T *argvars, typval_T *rettv)
8820{
8821 char_u buf[NUMBUFLEN];
8822
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008823 if (has_mbyte)
8824 {
8825 int utf8 = 0;
8826
8827 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008828 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008829 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01008830 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008831 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008832 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008833 }
8834 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008835 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008836 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008837 buf[1] = NUL;
8838 }
8839 rettv->v_type = VAR_STRING;
8840 rettv->vval.v_string = vim_strsave(buf);
8841}
8842
8843/*
8844 * "or(expr, expr)" function
8845 */
8846 static void
8847f_or(typval_T *argvars, typval_T *rettv)
8848{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008849 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
8850 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008851}
8852
8853/*
8854 * "pathshorten()" function
8855 */
8856 static void
8857f_pathshorten(typval_T *argvars, typval_T *rettv)
8858{
8859 char_u *p;
8860
8861 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008862 p = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008863 if (p == NULL)
8864 rettv->vval.v_string = NULL;
8865 else
8866 {
8867 p = vim_strsave(p);
8868 rettv->vval.v_string = p;
8869 if (p != NULL)
8870 shorten_dir(p);
8871 }
8872}
8873
8874#ifdef FEAT_PERL
8875/*
8876 * "perleval()" function
8877 */
8878 static void
8879f_perleval(typval_T *argvars, typval_T *rettv)
8880{
8881 char_u *str;
8882 char_u buf[NUMBUFLEN];
8883
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008884 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008885 do_perleval(str, rettv);
8886}
8887#endif
8888
8889#ifdef FEAT_FLOAT
8890/*
8891 * "pow()" function
8892 */
8893 static void
8894f_pow(typval_T *argvars, typval_T *rettv)
8895{
8896 float_T fx = 0.0, fy = 0.0;
8897
8898 rettv->v_type = VAR_FLOAT;
8899 if (get_float_arg(argvars, &fx) == OK
8900 && get_float_arg(&argvars[1], &fy) == OK)
8901 rettv->vval.v_float = pow(fx, fy);
8902 else
8903 rettv->vval.v_float = 0.0;
8904}
8905#endif
8906
8907/*
8908 * "prevnonblank()" function
8909 */
8910 static void
8911f_prevnonblank(typval_T *argvars, typval_T *rettv)
8912{
8913 linenr_T lnum;
8914
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008915 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008916 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8917 lnum = 0;
8918 else
8919 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8920 --lnum;
8921 rettv->vval.v_number = lnum;
8922}
8923
8924/* This dummy va_list is here because:
8925 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8926 * - locally in the function results in a "used before set" warning
8927 * - using va_start() to initialize it gives "function with fixed args" error */
8928static va_list ap;
8929
8930/*
8931 * "printf()" function
8932 */
8933 static void
8934f_printf(typval_T *argvars, typval_T *rettv)
8935{
8936 char_u buf[NUMBUFLEN];
8937 int len;
8938 char_u *s;
8939 int saved_did_emsg = did_emsg;
8940 char *fmt;
8941
8942 rettv->v_type = VAR_STRING;
8943 rettv->vval.v_string = NULL;
8944
8945 /* Get the required length, allocate the buffer and do it for real. */
8946 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008947 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008948 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008949 if (!did_emsg)
8950 {
8951 s = alloc(len + 1);
8952 if (s != NULL)
8953 {
8954 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008955 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8956 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008957 }
8958 }
8959 did_emsg |= saved_did_emsg;
8960}
8961
Bram Moolenaarf2732452018-06-03 14:47:35 +02008962#ifdef FEAT_JOB_CHANNEL
8963/*
8964 * "prompt_setcallback({buffer}, {callback})" function
8965 */
8966 static void
8967f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
8968{
8969 buf_T *buf;
8970 char_u *callback;
8971 partial_T *partial;
8972
8973 if (check_secure())
8974 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01008975 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008976 if (buf == NULL)
8977 return;
8978
8979 callback = get_callback(&argvars[1], &partial);
8980 if (callback == NULL)
8981 return;
8982
8983 free_callback(buf->b_prompt_callback, buf->b_prompt_partial);
8984 if (partial == NULL)
8985 buf->b_prompt_callback = vim_strsave(callback);
8986 else
8987 /* pointer into the partial */
8988 buf->b_prompt_callback = callback;
8989 buf->b_prompt_partial = partial;
8990}
8991
8992/*
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02008993 * "prompt_setinterrupt({buffer}, {callback})" function
8994 */
8995 static void
8996f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
8997{
8998 buf_T *buf;
8999 char_u *callback;
9000 partial_T *partial;
9001
9002 if (check_secure())
9003 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009004 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009005 if (buf == NULL)
9006 return;
9007
9008 callback = get_callback(&argvars[1], &partial);
9009 if (callback == NULL)
9010 return;
9011
9012 free_callback(buf->b_prompt_interrupt, buf->b_prompt_int_partial);
9013 if (partial == NULL)
9014 buf->b_prompt_interrupt = vim_strsave(callback);
9015 else
9016 /* pointer into the partial */
9017 buf->b_prompt_interrupt = callback;
9018 buf->b_prompt_int_partial = partial;
9019}
9020
9021/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02009022 * "prompt_setprompt({buffer}, {text})" function
9023 */
9024 static void
9025f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
9026{
9027 buf_T *buf;
9028 char_u *text;
9029
9030 if (check_secure())
9031 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009032 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009033 if (buf == NULL)
9034 return;
9035
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009036 text = tv_get_string(&argvars[1]);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009037 vim_free(buf->b_prompt_text);
9038 buf->b_prompt_text = vim_strsave(text);
9039}
9040#endif
9041
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009042/*
9043 * "pumvisible()" function
9044 */
9045 static void
9046f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9047{
9048#ifdef FEAT_INS_EXPAND
9049 if (pum_visible())
9050 rettv->vval.v_number = 1;
9051#endif
9052}
9053
9054#ifdef FEAT_PYTHON3
9055/*
9056 * "py3eval()" function
9057 */
9058 static void
9059f_py3eval(typval_T *argvars, typval_T *rettv)
9060{
9061 char_u *str;
9062 char_u buf[NUMBUFLEN];
9063
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009064 if (check_restricted() || check_secure())
9065 return;
9066
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009067 if (p_pyx == 0)
9068 p_pyx = 3;
9069
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009070 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009071 do_py3eval(str, rettv);
9072}
9073#endif
9074
9075#ifdef FEAT_PYTHON
9076/*
9077 * "pyeval()" function
9078 */
9079 static void
9080f_pyeval(typval_T *argvars, typval_T *rettv)
9081{
9082 char_u *str;
9083 char_u buf[NUMBUFLEN];
9084
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009085 if (check_restricted() || check_secure())
9086 return;
9087
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009088 if (p_pyx == 0)
9089 p_pyx = 2;
9090
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009091 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009092 do_pyeval(str, rettv);
9093}
9094#endif
9095
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009096#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
9097/*
9098 * "pyxeval()" function
9099 */
9100 static void
9101f_pyxeval(typval_T *argvars, typval_T *rettv)
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 defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
9107 init_pyxversion();
9108 if (p_pyx == 2)
9109 f_pyeval(argvars, rettv);
9110 else
9111 f_py3eval(argvars, rettv);
9112# elif defined(FEAT_PYTHON)
9113 f_pyeval(argvars, rettv);
9114# elif defined(FEAT_PYTHON3)
9115 f_py3eval(argvars, rettv);
9116# endif
9117}
9118#endif
9119
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009120/*
9121 * "range()" function
9122 */
9123 static void
9124f_range(typval_T *argvars, typval_T *rettv)
9125{
9126 varnumber_T start;
9127 varnumber_T end;
9128 varnumber_T stride = 1;
9129 varnumber_T i;
9130 int error = FALSE;
9131
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009132 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009133 if (argvars[1].v_type == VAR_UNKNOWN)
9134 {
9135 end = start - 1;
9136 start = 0;
9137 }
9138 else
9139 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009140 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009141 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009142 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009143 }
9144
9145 if (error)
9146 return; /* type error; errmsg already given */
9147 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009148 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009149 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009150 emsg(_("E727: Start past end"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009151 else
9152 {
9153 if (rettv_list_alloc(rettv) == OK)
9154 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
9155 if (list_append_number(rettv->vval.v_list,
9156 (varnumber_T)i) == FAIL)
9157 break;
9158 }
9159}
9160
9161/*
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009162 * Evaluate "expr" for readdir().
9163 */
9164 static int
9165readdir_checkitem(typval_T *expr, char_u *name)
9166{
9167 typval_T save_val;
9168 typval_T rettv;
9169 typval_T argv[2];
9170 int retval = 0;
9171 int error = FALSE;
9172
9173 prepare_vimvar(VV_VAL, &save_val);
9174 set_vim_var_string(VV_VAL, name, -1);
9175 argv[0].v_type = VAR_STRING;
9176 argv[0].vval.v_string = name;
9177
9178 if (eval_expr_typval(expr, argv, 1, &rettv) == FAIL)
9179 goto theend;
9180
9181 retval = tv_get_number_chk(&rettv, &error);
9182 if (error)
9183 retval = -1;
9184 clear_tv(&rettv);
9185
9186theend:
9187 set_vim_var_string(VV_VAL, NULL, 0);
9188 restore_vimvar(VV_VAL, &save_val);
9189 return retval;
9190}
9191
9192/*
9193 * "readdir()" function
9194 */
9195 static void
9196f_readdir(typval_T *argvars, typval_T *rettv)
9197{
9198 typval_T *expr;
9199 int failed = FALSE;
9200 char_u *path;
9201 garray_T ga;
9202 int i;
9203#ifdef MSWIN
9204 char_u *buf, *p;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009205 int ok;
9206 HANDLE hFind = INVALID_HANDLE_VALUE;
9207 WIN32_FIND_DATAW wfb;
9208 WCHAR *wn = NULL; // UCS-2 name, NULL when not used.
9209#endif
9210
9211 if (rettv_list_alloc(rettv) == FAIL)
9212 return;
9213 path = tv_get_string(&argvars[0]);
9214 expr = &argvars[1];
9215 ga_init2(&ga, (int)sizeof(char *), 20);
9216
9217#ifdef MSWIN
9218 buf = alloc((int)MAXPATHL);
9219 if (buf == NULL)
9220 return;
9221 STRNCPY(buf, path, MAXPATHL-5);
9222 p = vim_strpbrk(path, (char_u *)"\\/");
9223 if (p != NULL)
9224 *p = NUL;
9225 STRCAT(buf, "\\*");
9226
9227 wn = enc_to_utf16(buf, NULL);
9228 if (wn != NULL)
9229 hFind = FindFirstFileW(wn, &wfb);
9230 ok = (hFind != INVALID_HANDLE_VALUE);
9231 if (!ok)
9232 smsg(_(e_notopen), path);
9233 else
9234 {
9235 while (ok)
9236 {
9237 int ignore;
9238
9239 p = utf16_to_enc(wfb.cFileName, NULL); // p is allocated here
9240 if (p == NULL)
9241 break; // out of memory
9242
9243 ignore = p[0] == '.' && (p[1] == NUL
9244 || (p[1] == '.' && p[2] == NUL));
9245 if (!ignore && expr->v_type != VAR_UNKNOWN)
9246 {
9247 int r = readdir_checkitem(expr, p);
9248
9249 if (r < 0)
9250 {
9251 vim_free(p);
9252 break;
9253 }
9254 if (r == 0)
9255 ignore = TRUE;
9256 }
9257
9258 if (!ignore)
9259 {
9260 if (ga_grow(&ga, 1) == OK)
9261 ((char_u**)ga.ga_data)[ga.ga_len++] = vim_strsave(p);
9262 else
9263 {
9264 failed = TRUE;
9265 vim_free(p);
9266 break;
9267 }
9268 }
9269
9270 vim_free(p);
9271 ok = FindNextFileW(hFind, &wfb);
9272 }
9273 FindClose(hFind);
9274 }
9275
9276 vim_free(buf);
9277 vim_free(wn);
9278#else
9279 DIR *dirp;
9280 struct dirent *dp;
9281 char_u *p;
9282
9283 dirp = opendir((char *)path);
9284 if (dirp == NULL)
9285 smsg(_(e_notopen), path);
9286 else
9287 {
9288 for (;;)
9289 {
9290 int ignore;
9291
9292 dp = readdir(dirp);
9293 if (dp == NULL)
9294 break;
9295 p = (char_u *)dp->d_name;
9296
9297 ignore = p[0] == '.' &&
9298 (p[1] == NUL ||
9299 (p[1] == '.' && p[2] == NUL));
9300 if (!ignore && expr->v_type != VAR_UNKNOWN)
9301 {
9302 int r = readdir_checkitem(expr, p);
9303
9304 if (r < 0)
9305 break;
9306 if (r == 0)
9307 ignore = TRUE;
9308 }
9309
9310 if (!ignore)
9311 {
9312 if (ga_grow(&ga, 1) == OK)
9313 ((char_u**)ga.ga_data)[ga.ga_len++] = vim_strsave(p);
9314 else
9315 {
9316 failed = TRUE;
9317 break;
9318 }
9319 }
9320 }
9321
9322 closedir(dirp);
9323 }
9324#endif
9325
Bram Moolenaar334ad412019-04-19 15:20:46 +02009326 if (!failed && rettv->vval.v_list != NULL && ga.ga_len > 0)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009327 {
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009328 sort_strings((char_u **)ga.ga_data, ga.ga_len);
9329 for (i = 0; i < ga.ga_len; i++)
9330 {
9331 p = ((char_u **)ga.ga_data)[i];
9332 list_append_string(rettv->vval.v_list, p, -1);
9333 }
9334 }
Bram Moolenaar334ad412019-04-19 15:20:46 +02009335 ga_clear_strings(&ga);
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009336}
9337
9338/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009339 * "readfile()" function
9340 */
9341 static void
9342f_readfile(typval_T *argvars, typval_T *rettv)
9343{
9344 int binary = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009345 int blob = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009346 int failed = FALSE;
9347 char_u *fname;
9348 FILE *fd;
9349 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
9350 int io_size = sizeof(buf);
9351 int readlen; /* size of last fread() */
9352 char_u *prev = NULL; /* previously read bytes, if any */
9353 long prevlen = 0; /* length of data in prev */
9354 long prevsize = 0; /* size of prev buffer */
9355 long maxline = MAXLNUM;
9356 long cnt = 0;
9357 char_u *p; /* position in buf */
9358 char_u *start; /* start of current line */
9359
9360 if (argvars[1].v_type != VAR_UNKNOWN)
9361 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009362 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009363 binary = TRUE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009364 if (STRCMP(tv_get_string(&argvars[1]), "B") == 0)
9365 blob = TRUE;
9366
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009367 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009368 maxline = (long)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009369 }
9370
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009371 if (blob)
9372 {
9373 if (rettv_blob_alloc(rettv) == FAIL)
9374 return;
9375 }
9376 else
9377 {
9378 if (rettv_list_alloc(rettv) == FAIL)
9379 return;
9380 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009381
9382 /* Always open the file in binary mode, library functions have a mind of
9383 * their own about CR-LF conversion. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009384 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009385 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
9386 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009387 semsg(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009388 return;
9389 }
9390
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009391 if (blob)
9392 {
9393 if (read_blob(fd, rettv->vval.v_blob) == FAIL)
9394 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009395 emsg("cannot read file");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009396 blob_free(rettv->vval.v_blob);
9397 }
9398 fclose(fd);
9399 return;
9400 }
9401
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009402 while (cnt < maxline || maxline < 0)
9403 {
9404 readlen = (int)fread(buf, 1, io_size, fd);
9405
9406 /* This for loop processes what was read, but is also entered at end
9407 * of file so that either:
9408 * - an incomplete line gets written
9409 * - a "binary" file gets an empty line at the end if it ends in a
9410 * newline. */
9411 for (p = buf, start = buf;
9412 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
9413 ++p)
9414 {
9415 if (*p == '\n' || readlen <= 0)
9416 {
9417 listitem_T *li;
9418 char_u *s = NULL;
9419 long_u len = p - start;
9420
9421 /* Finished a line. Remove CRs before NL. */
9422 if (readlen > 0 && !binary)
9423 {
9424 while (len > 0 && start[len - 1] == '\r')
9425 --len;
9426 /* removal may cross back to the "prev" string */
9427 if (len == 0)
9428 while (prevlen > 0 && prev[prevlen - 1] == '\r')
9429 --prevlen;
9430 }
9431 if (prevlen == 0)
9432 s = vim_strnsave(start, (int)len);
9433 else
9434 {
9435 /* Change "prev" buffer to be the right size. This way
9436 * the bytes are only copied once, and very long lines are
9437 * allocated only once. */
9438 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
9439 {
9440 mch_memmove(s + prevlen, start, len);
9441 s[prevlen + len] = NUL;
9442 prev = NULL; /* the list will own the string */
9443 prevlen = prevsize = 0;
9444 }
9445 }
9446 if (s == NULL)
9447 {
9448 do_outofmem_msg((long_u) prevlen + len + 1);
9449 failed = TRUE;
9450 break;
9451 }
9452
9453 if ((li = listitem_alloc()) == NULL)
9454 {
9455 vim_free(s);
9456 failed = TRUE;
9457 break;
9458 }
9459 li->li_tv.v_type = VAR_STRING;
9460 li->li_tv.v_lock = 0;
9461 li->li_tv.vval.v_string = s;
9462 list_append(rettv->vval.v_list, li);
9463
9464 start = p + 1; /* step over newline */
9465 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
9466 break;
9467 }
9468 else if (*p == NUL)
9469 *p = '\n';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009470 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
9471 * when finding the BF and check the previous two bytes. */
9472 else if (*p == 0xbf && enc_utf8 && !binary)
9473 {
9474 /* Find the two bytes before the 0xbf. If p is at buf, or buf
9475 * + 1, these may be in the "prev" string. */
9476 char_u back1 = p >= buf + 1 ? p[-1]
9477 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
9478 char_u back2 = p >= buf + 2 ? p[-2]
9479 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
9480 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
9481
9482 if (back2 == 0xef && back1 == 0xbb)
9483 {
9484 char_u *dest = p - 2;
9485
9486 /* Usually a BOM is at the beginning of a file, and so at
9487 * the beginning of a line; then we can just step over it.
9488 */
9489 if (start == dest)
9490 start = p + 1;
9491 else
9492 {
9493 /* have to shuffle buf to close gap */
9494 int adjust_prevlen = 0;
9495
9496 if (dest < buf)
9497 {
9498 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
9499 dest = buf;
9500 }
9501 if (readlen > p - buf + 1)
9502 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
9503 readlen -= 3 - adjust_prevlen;
9504 prevlen -= adjust_prevlen;
9505 p = dest - 1;
9506 }
9507 }
9508 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009509 } /* for */
9510
9511 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
9512 break;
9513 if (start < p)
9514 {
9515 /* There's part of a line in buf, store it in "prev". */
9516 if (p - start + prevlen >= prevsize)
9517 {
9518 /* need bigger "prev" buffer */
9519 char_u *newprev;
9520
9521 /* A common use case is ordinary text files and "prev" gets a
9522 * fragment of a line, so the first allocation is made
9523 * small, to avoid repeatedly 'allocing' large and
9524 * 'reallocing' small. */
9525 if (prevsize == 0)
9526 prevsize = (long)(p - start);
9527 else
9528 {
9529 long grow50pc = (prevsize * 3) / 2;
9530 long growmin = (long)((p - start) * 2 + prevlen);
9531 prevsize = grow50pc > growmin ? grow50pc : growmin;
9532 }
9533 newprev = prev == NULL ? alloc(prevsize)
9534 : vim_realloc(prev, prevsize);
9535 if (newprev == NULL)
9536 {
9537 do_outofmem_msg((long_u)prevsize);
9538 failed = TRUE;
9539 break;
9540 }
9541 prev = newprev;
9542 }
9543 /* Add the line part to end of "prev". */
9544 mch_memmove(prev + prevlen, start, p - start);
9545 prevlen += (long)(p - start);
9546 }
9547 } /* while */
9548
9549 /*
9550 * For a negative line count use only the lines at the end of the file,
9551 * free the rest.
9552 */
9553 if (!failed && maxline < 0)
9554 while (cnt > -maxline)
9555 {
9556 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
9557 --cnt;
9558 }
9559
9560 if (failed)
9561 {
9562 list_free(rettv->vval.v_list);
9563 /* readfile doc says an empty list is returned on error */
9564 rettv->vval.v_list = list_alloc();
9565 }
9566
9567 vim_free(prev);
9568 fclose(fd);
9569}
9570
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009571 static void
9572return_register(int regname, typval_T *rettv)
9573{
9574 char_u buf[2] = {0, 0};
9575
9576 buf[0] = (char_u)regname;
9577 rettv->v_type = VAR_STRING;
9578 rettv->vval.v_string = vim_strsave(buf);
9579}
9580
9581/*
9582 * "reg_executing()" function
9583 */
9584 static void
9585f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9586{
9587 return_register(reg_executing, rettv);
9588}
9589
9590/*
9591 * "reg_recording()" function
9592 */
9593 static void
9594f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9595{
9596 return_register(reg_recording, rettv);
9597}
9598
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009599#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009600/*
9601 * Convert a List to proftime_T.
9602 * Return FAIL when there is something wrong.
9603 */
9604 static int
9605list2proftime(typval_T *arg, proftime_T *tm)
9606{
9607 long n1, n2;
9608 int error = FALSE;
9609
9610 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9611 || arg->vval.v_list->lv_len != 2)
9612 return FAIL;
9613 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9614 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009615# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009616 tm->HighPart = n1;
9617 tm->LowPart = n2;
9618# else
9619 tm->tv_sec = n1;
9620 tm->tv_usec = n2;
9621# endif
9622 return error ? FAIL : OK;
9623}
9624#endif /* FEAT_RELTIME */
9625
9626/*
9627 * "reltime()" function
9628 */
9629 static void
9630f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9631{
9632#ifdef FEAT_RELTIME
9633 proftime_T res;
9634 proftime_T start;
9635
9636 if (argvars[0].v_type == VAR_UNKNOWN)
9637 {
9638 /* No arguments: get current time. */
9639 profile_start(&res);
9640 }
9641 else if (argvars[1].v_type == VAR_UNKNOWN)
9642 {
9643 if (list2proftime(&argvars[0], &res) == FAIL)
9644 return;
9645 profile_end(&res);
9646 }
9647 else
9648 {
9649 /* Two arguments: compute the difference. */
9650 if (list2proftime(&argvars[0], &start) == FAIL
9651 || list2proftime(&argvars[1], &res) == FAIL)
9652 return;
9653 profile_sub(&res, &start);
9654 }
9655
9656 if (rettv_list_alloc(rettv) == OK)
9657 {
9658 long n1, n2;
9659
Bram Moolenaar4f974752019-02-17 17:44:42 +01009660# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009661 n1 = res.HighPart;
9662 n2 = res.LowPart;
9663# else
9664 n1 = res.tv_sec;
9665 n2 = res.tv_usec;
9666# endif
9667 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9668 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9669 }
9670#endif
9671}
9672
9673#ifdef FEAT_FLOAT
9674/*
9675 * "reltimefloat()" function
9676 */
9677 static void
9678f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9679{
9680# ifdef FEAT_RELTIME
9681 proftime_T tm;
9682# endif
9683
9684 rettv->v_type = VAR_FLOAT;
9685 rettv->vval.v_float = 0;
9686# ifdef FEAT_RELTIME
9687 if (list2proftime(&argvars[0], &tm) == OK)
9688 rettv->vval.v_float = profile_float(&tm);
9689# endif
9690}
9691#endif
9692
9693/*
9694 * "reltimestr()" function
9695 */
9696 static void
9697f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9698{
9699#ifdef FEAT_RELTIME
9700 proftime_T tm;
9701#endif
9702
9703 rettv->v_type = VAR_STRING;
9704 rettv->vval.v_string = NULL;
9705#ifdef FEAT_RELTIME
9706 if (list2proftime(&argvars[0], &tm) == OK)
9707 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9708#endif
9709}
9710
9711#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009712 static void
9713make_connection(void)
9714{
9715 if (X_DISPLAY == NULL
9716# ifdef FEAT_GUI
9717 && !gui.in_use
9718# endif
9719 )
9720 {
9721 x_force_connect = TRUE;
9722 setup_term_clip();
9723 x_force_connect = FALSE;
9724 }
9725}
9726
9727 static int
9728check_connection(void)
9729{
9730 make_connection();
9731 if (X_DISPLAY == NULL)
9732 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009733 emsg(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009734 return FAIL;
9735 }
9736 return OK;
9737}
9738#endif
9739
9740#ifdef FEAT_CLIENTSERVER
9741 static void
9742remote_common(typval_T *argvars, typval_T *rettv, int expr)
9743{
9744 char_u *server_name;
9745 char_u *keys;
9746 char_u *r = NULL;
9747 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009748 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009749# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009750 HWND w;
9751# else
9752 Window w;
9753# endif
9754
9755 if (check_restricted() || check_secure())
9756 return;
9757
9758# ifdef FEAT_X11
9759 if (check_connection() == FAIL)
9760 return;
9761# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009762 if (argvars[2].v_type != VAR_UNKNOWN
9763 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009764 timeout = tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009765
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009766 server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009767 if (server_name == NULL)
9768 return; /* type error; errmsg already given */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009769 keys = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009770# ifdef MSWIN
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009771 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009772# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009773 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9774 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009775# endif
9776 {
9777 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009778 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009779 emsg((char *)r); // sending worked but evaluation failed
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009780 vim_free(r);
9781 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009782 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009783 semsg(_("E241: Unable to send to %s"), server_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009784 return;
9785 }
9786
9787 rettv->vval.v_string = r;
9788
9789 if (argvars[2].v_type != VAR_UNKNOWN)
9790 {
9791 dictitem_T v;
9792 char_u str[30];
9793 char_u *idvar;
9794
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009795 idvar = tv_get_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009796 if (idvar != NULL && *idvar != NUL)
9797 {
9798 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9799 v.di_tv.v_type = VAR_STRING;
9800 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009801 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009802 vim_free(v.di_tv.vval.v_string);
9803 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009804 }
9805}
9806#endif
9807
9808/*
9809 * "remote_expr()" function
9810 */
9811 static void
9812f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9813{
9814 rettv->v_type = VAR_STRING;
9815 rettv->vval.v_string = NULL;
9816#ifdef FEAT_CLIENTSERVER
9817 remote_common(argvars, rettv, TRUE);
9818#endif
9819}
9820
9821/*
9822 * "remote_foreground()" function
9823 */
9824 static void
9825f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9826{
9827#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +01009828# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009829 /* On Win32 it's done in this application. */
9830 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009831 char_u *server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009832
9833 if (server_name != NULL)
9834 serverForeground(server_name);
9835 }
9836# else
9837 /* Send a foreground() expression to the server. */
9838 argvars[1].v_type = VAR_STRING;
9839 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9840 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009841 rettv->v_type = VAR_STRING;
9842 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009843 remote_common(argvars, rettv, TRUE);
9844 vim_free(argvars[1].vval.v_string);
9845# endif
9846#endif
9847}
9848
9849 static void
9850f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
9851{
9852#ifdef FEAT_CLIENTSERVER
9853 dictitem_T v;
9854 char_u *s = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009855# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009856 long_u n = 0;
9857# endif
9858 char_u *serverid;
9859
9860 if (check_restricted() || check_secure())
9861 {
9862 rettv->vval.v_number = -1;
9863 return;
9864 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009865 serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009866 if (serverid == NULL)
9867 {
9868 rettv->vval.v_number = -1;
9869 return; /* type error; errmsg already given */
9870 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01009871# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009872 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
9873 if (n == 0)
9874 rettv->vval.v_number = -1;
9875 else
9876 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009877 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009878 rettv->vval.v_number = (s != NULL);
9879 }
9880# else
9881 if (check_connection() == FAIL)
9882 return;
9883
9884 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
9885 serverStrToWin(serverid), &s);
9886# endif
9887
9888 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
9889 {
9890 char_u *retvar;
9891
9892 v.di_tv.v_type = VAR_STRING;
9893 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009894 retvar = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009895 if (retvar != NULL)
9896 set_var(retvar, &v.di_tv, FALSE);
9897 vim_free(v.di_tv.vval.v_string);
9898 }
9899#else
9900 rettv->vval.v_number = -1;
9901#endif
9902}
9903
9904 static void
9905f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
9906{
9907 char_u *r = NULL;
9908
9909#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009910 char_u *serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009911
9912 if (serverid != NULL && !check_restricted() && !check_secure())
9913 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009914 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009915# ifdef MSWIN
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009916 /* The server's HWND is encoded in the 'id' parameter */
9917 long_u n = 0;
9918# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009919
9920 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009921 timeout = tv_get_number(&argvars[1]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009922
Bram Moolenaar4f974752019-02-17 17:44:42 +01009923# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009924 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
9925 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009926 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009927 if (r == NULL)
9928# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009929 if (check_connection() == FAIL
9930 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
9931 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009932# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009933 emsg(_("E277: Unable to read a server reply"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009934 }
9935#endif
9936 rettv->v_type = VAR_STRING;
9937 rettv->vval.v_string = r;
9938}
9939
9940/*
9941 * "remote_send()" function
9942 */
9943 static void
9944f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9945{
9946 rettv->v_type = VAR_STRING;
9947 rettv->vval.v_string = NULL;
9948#ifdef FEAT_CLIENTSERVER
9949 remote_common(argvars, rettv, FALSE);
9950#endif
9951}
9952
9953/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009954 * "remote_startserver()" function
9955 */
9956 static void
9957f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9958{
9959#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009960 char_u *server = tv_get_string_chk(&argvars[0]);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009961
9962 if (server == NULL)
9963 return; /* type error; errmsg already given */
9964 if (serverName != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009965 emsg(_("E941: already started a server"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009966 else
9967 {
9968# ifdef FEAT_X11
9969 if (check_connection() == OK)
9970 serverRegisterName(X_DISPLAY, server);
9971# else
9972 serverSetName(server);
9973# endif
9974 }
9975#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009976 emsg(_("E942: +clientserver feature not available"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009977#endif
9978}
9979
9980/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009981 * "remove()" function
9982 */
9983 static void
9984f_remove(typval_T *argvars, typval_T *rettv)
9985{
9986 list_T *l;
9987 listitem_T *item, *item2;
9988 listitem_T *li;
9989 long idx;
9990 long end;
9991 char_u *key;
9992 dict_T *d;
9993 dictitem_T *di;
9994 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009995 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009996
9997 if (argvars[0].v_type == VAR_DICT)
9998 {
9999 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010000 semsg(_(e_toomanyarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010001 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010002 && !var_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010003 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010004 key = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010005 if (key != NULL)
10006 {
10007 di = dict_find(d, key, -1);
10008 if (di == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010009 semsg(_(e_dictkey), key);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010010 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10011 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
10012 {
10013 *rettv = di->di_tv;
10014 init_tv(&di->di_tv);
10015 dictitem_remove(d, di);
10016 }
10017 }
10018 }
10019 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010020 else if (argvars[0].v_type == VAR_BLOB)
10021 {
10022 idx = (long)tv_get_number_chk(&argvars[1], &error);
10023 if (!error)
10024 {
10025 blob_T *b = argvars[0].vval.v_blob;
10026 int len = blob_len(b);
10027 char_u *p;
10028
10029 if (idx < 0)
10030 // count from the end
10031 idx = len + idx;
10032 if (idx < 0 || idx >= len)
10033 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010034 semsg(_(e_blobidx), idx);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010035 return;
10036 }
10037 if (argvars[2].v_type == VAR_UNKNOWN)
10038 {
10039 // Remove one item, return its value.
10040 p = (char_u *)b->bv_ga.ga_data;
10041 rettv->vval.v_number = (varnumber_T) *(p + idx);
10042 mch_memmove(p + idx, p + idx + 1, (size_t)len - idx - 1);
10043 --b->bv_ga.ga_len;
10044 }
10045 else
10046 {
10047 blob_T *blob;
10048
10049 // Remove range of items, return list with values.
10050 end = (long)tv_get_number_chk(&argvars[2], &error);
10051 if (error)
10052 return;
10053 if (end < 0)
10054 // count from the end
10055 end = len + end;
10056 if (end >= len || idx > end)
10057 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010058 semsg(_(e_blobidx), end);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010059 return;
10060 }
10061 blob = blob_alloc();
10062 if (blob == NULL)
10063 return;
10064 blob->bv_ga.ga_len = end - idx + 1;
10065 if (ga_grow(&blob->bv_ga, end - idx + 1) == FAIL)
10066 {
10067 vim_free(blob);
10068 return;
10069 }
10070 p = (char_u *)b->bv_ga.ga_data;
10071 mch_memmove((char_u *)blob->bv_ga.ga_data, p + idx,
10072 (size_t)(end - idx + 1));
10073 ++blob->bv_refcount;
10074 rettv->v_type = VAR_BLOB;
10075 rettv->vval.v_blob = blob;
10076
10077 mch_memmove(p + idx, p + end + 1, (size_t)(len - end));
10078 b->bv_ga.ga_len -= end - idx + 1;
10079 }
10080 }
10081 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010082 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010083 semsg(_(e_listdictblobarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010084 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010085 && !var_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010086 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010087 idx = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010088 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010089 ; // type error: do nothing, errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010090 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010091 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010092 else
10093 {
10094 if (argvars[2].v_type == VAR_UNKNOWN)
10095 {
10096 /* Remove one item, return its value. */
10097 vimlist_remove(l, item, item);
10098 *rettv = item->li_tv;
10099 vim_free(item);
10100 }
10101 else
10102 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010103 // Remove range of items, return list with values.
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010104 end = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010105 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010106 ; // type error: do nothing
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010107 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010108 semsg(_(e_listidx), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010109 else
10110 {
10111 int cnt = 0;
10112
10113 for (li = item; li != NULL; li = li->li_next)
10114 {
10115 ++cnt;
10116 if (li == item2)
10117 break;
10118 }
10119 if (li == NULL) /* didn't find "item2" after "item" */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010120 emsg(_(e_invrange));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010121 else
10122 {
10123 vimlist_remove(l, item, item2);
10124 if (rettv_list_alloc(rettv) == OK)
10125 {
10126 l = rettv->vval.v_list;
10127 l->lv_first = item;
10128 l->lv_last = item2;
10129 item->li_prev = NULL;
10130 item2->li_next = NULL;
10131 l->lv_len = cnt;
10132 }
10133 }
10134 }
10135 }
10136 }
10137 }
10138}
10139
10140/*
10141 * "rename({from}, {to})" function
10142 */
10143 static void
10144f_rename(typval_T *argvars, typval_T *rettv)
10145{
10146 char_u buf[NUMBUFLEN];
10147
10148 if (check_restricted() || check_secure())
10149 rettv->vval.v_number = -1;
10150 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010151 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
10152 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010153}
10154
10155/*
10156 * "repeat()" function
10157 */
10158 static void
10159f_repeat(typval_T *argvars, typval_T *rettv)
10160{
10161 char_u *p;
10162 int n;
10163 int slen;
10164 int len;
10165 char_u *r;
10166 int i;
10167
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010168 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010169 if (argvars[0].v_type == VAR_LIST)
10170 {
10171 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
10172 while (n-- > 0)
10173 if (list_extend(rettv->vval.v_list,
10174 argvars[0].vval.v_list, NULL) == FAIL)
10175 break;
10176 }
10177 else
10178 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010179 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010180 rettv->v_type = VAR_STRING;
10181 rettv->vval.v_string = NULL;
10182
10183 slen = (int)STRLEN(p);
10184 len = slen * n;
10185 if (len <= 0)
10186 return;
10187
10188 r = alloc(len + 1);
10189 if (r != NULL)
10190 {
10191 for (i = 0; i < n; i++)
10192 mch_memmove(r + i * slen, p, (size_t)slen);
10193 r[len] = NUL;
10194 }
10195
10196 rettv->vval.v_string = r;
10197 }
10198}
10199
10200/*
10201 * "resolve()" function
10202 */
10203 static void
10204f_resolve(typval_T *argvars, typval_T *rettv)
10205{
10206 char_u *p;
10207#ifdef HAVE_READLINK
10208 char_u *buf = NULL;
10209#endif
10210
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010211 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010212#ifdef FEAT_SHORTCUT
10213 {
10214 char_u *v = NULL;
10215
Bram Moolenaardce1e892019-02-10 23:18:53 +010010216 v = mch_resolve_path(p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010217 if (v != NULL)
10218 rettv->vval.v_string = v;
10219 else
10220 rettv->vval.v_string = vim_strsave(p);
10221 }
10222#else
10223# ifdef HAVE_READLINK
10224 {
10225 char_u *cpy;
10226 int len;
10227 char_u *remain = NULL;
10228 char_u *q;
10229 int is_relative_to_current = FALSE;
10230 int has_trailing_pathsep = FALSE;
10231 int limit = 100;
10232
10233 p = vim_strsave(p);
10234
10235 if (p[0] == '.' && (vim_ispathsep(p[1])
10236 || (p[1] == '.' && (vim_ispathsep(p[2])))))
10237 is_relative_to_current = TRUE;
10238
10239 len = STRLEN(p);
10240 if (len > 0 && after_pathsep(p, p + len))
10241 {
10242 has_trailing_pathsep = TRUE;
10243 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
10244 }
10245
10246 q = getnextcomp(p);
10247 if (*q != NUL)
10248 {
10249 /* Separate the first path component in "p", and keep the
10250 * remainder (beginning with the path separator). */
10251 remain = vim_strsave(q - 1);
10252 q[-1] = NUL;
10253 }
10254
10255 buf = alloc(MAXPATHL + 1);
10256 if (buf == NULL)
10257 goto fail;
10258
10259 for (;;)
10260 {
10261 for (;;)
10262 {
10263 len = readlink((char *)p, (char *)buf, MAXPATHL);
10264 if (len <= 0)
10265 break;
10266 buf[len] = NUL;
10267
10268 if (limit-- == 0)
10269 {
10270 vim_free(p);
10271 vim_free(remain);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010272 emsg(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010273 rettv->vval.v_string = NULL;
10274 goto fail;
10275 }
10276
10277 /* Ensure that the result will have a trailing path separator
10278 * if the argument has one. */
10279 if (remain == NULL && has_trailing_pathsep)
10280 add_pathsep(buf);
10281
10282 /* Separate the first path component in the link value and
10283 * concatenate the remainders. */
10284 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
10285 if (*q != NUL)
10286 {
10287 if (remain == NULL)
10288 remain = vim_strsave(q - 1);
10289 else
10290 {
10291 cpy = concat_str(q - 1, remain);
10292 if (cpy != NULL)
10293 {
10294 vim_free(remain);
10295 remain = cpy;
10296 }
10297 }
10298 q[-1] = NUL;
10299 }
10300
10301 q = gettail(p);
10302 if (q > p && *q == NUL)
10303 {
10304 /* Ignore trailing path separator. */
10305 q[-1] = NUL;
10306 q = gettail(p);
10307 }
10308 if (q > p && !mch_isFullName(buf))
10309 {
10310 /* symlink is relative to directory of argument */
10311 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
10312 if (cpy != NULL)
10313 {
10314 STRCPY(cpy, p);
10315 STRCPY(gettail(cpy), buf);
10316 vim_free(p);
10317 p = cpy;
10318 }
10319 }
10320 else
10321 {
10322 vim_free(p);
10323 p = vim_strsave(buf);
10324 }
10325 }
10326
10327 if (remain == NULL)
10328 break;
10329
10330 /* Append the first path component of "remain" to "p". */
10331 q = getnextcomp(remain + 1);
10332 len = q - remain - (*q != NUL);
10333 cpy = vim_strnsave(p, STRLEN(p) + len);
10334 if (cpy != NULL)
10335 {
10336 STRNCAT(cpy, remain, len);
10337 vim_free(p);
10338 p = cpy;
10339 }
10340 /* Shorten "remain". */
10341 if (*q != NUL)
10342 STRMOVE(remain, q - 1);
10343 else
Bram Moolenaard23a8232018-02-10 18:45:26 +010010344 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010345 }
10346
10347 /* If the result is a relative path name, make it explicitly relative to
10348 * the current directory if and only if the argument had this form. */
10349 if (!vim_ispathsep(*p))
10350 {
10351 if (is_relative_to_current
10352 && *p != NUL
10353 && !(p[0] == '.'
10354 && (p[1] == NUL
10355 || vim_ispathsep(p[1])
10356 || (p[1] == '.'
10357 && (p[2] == NUL
10358 || vim_ispathsep(p[2]))))))
10359 {
10360 /* Prepend "./". */
10361 cpy = concat_str((char_u *)"./", p);
10362 if (cpy != NULL)
10363 {
10364 vim_free(p);
10365 p = cpy;
10366 }
10367 }
10368 else if (!is_relative_to_current)
10369 {
10370 /* Strip leading "./". */
10371 q = p;
10372 while (q[0] == '.' && vim_ispathsep(q[1]))
10373 q += 2;
10374 if (q > p)
10375 STRMOVE(p, p + 2);
10376 }
10377 }
10378
10379 /* Ensure that the result will have no trailing path separator
10380 * if the argument had none. But keep "/" or "//". */
10381 if (!has_trailing_pathsep)
10382 {
10383 q = p + STRLEN(p);
10384 if (after_pathsep(p, q))
10385 *gettail_sep(p) = NUL;
10386 }
10387
10388 rettv->vval.v_string = p;
10389 }
10390# else
10391 rettv->vval.v_string = vim_strsave(p);
10392# endif
10393#endif
10394
10395 simplify_filename(rettv->vval.v_string);
10396
10397#ifdef HAVE_READLINK
10398fail:
10399 vim_free(buf);
10400#endif
10401 rettv->v_type = VAR_STRING;
10402}
10403
10404/*
10405 * "reverse({list})" function
10406 */
10407 static void
10408f_reverse(typval_T *argvars, typval_T *rettv)
10409{
10410 list_T *l;
10411 listitem_T *li, *ni;
10412
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010413 if (argvars[0].v_type == VAR_BLOB)
10414 {
10415 blob_T *b = argvars[0].vval.v_blob;
10416 int i, len = blob_len(b);
10417
10418 for (i = 0; i < len / 2; i++)
10419 {
10420 int tmp = blob_get(b, i);
10421
10422 blob_set(b, i, blob_get(b, len - i - 1));
10423 blob_set(b, len - i - 1, tmp);
10424 }
10425 rettv_blob_set(rettv, b);
10426 return;
10427 }
10428
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010429 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010430 semsg(_(e_listblobarg), "reverse()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010431 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010432 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010433 (char_u *)N_("reverse() argument"), TRUE))
10434 {
10435 li = l->lv_last;
10436 l->lv_first = l->lv_last = NULL;
10437 l->lv_len = 0;
10438 while (li != NULL)
10439 {
10440 ni = li->li_prev;
10441 list_append(l, li);
10442 li = ni;
10443 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020010444 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010445 l->lv_idx = l->lv_len - l->lv_idx - 1;
10446 }
10447}
10448
10449#define SP_NOMOVE 0x01 /* don't move cursor */
10450#define SP_REPEAT 0x02 /* repeat to find outer pair */
10451#define SP_RETCOUNT 0x04 /* return matchcount */
10452#define SP_SETPCMARK 0x08 /* set previous context mark */
10453#define SP_START 0x10 /* accept match at start position */
10454#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
10455#define SP_END 0x40 /* leave cursor at end of match */
10456#define SP_COLUMN 0x80 /* start at cursor column */
10457
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010458/*
10459 * Get flags for a search function.
10460 * Possibly sets "p_ws".
10461 * Returns BACKWARD, FORWARD or zero (for an error).
10462 */
10463 static int
10464get_search_arg(typval_T *varp, int *flagsp)
10465{
10466 int dir = FORWARD;
10467 char_u *flags;
10468 char_u nbuf[NUMBUFLEN];
10469 int mask;
10470
10471 if (varp->v_type != VAR_UNKNOWN)
10472 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010473 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010474 if (flags == NULL)
10475 return 0; /* type error; errmsg already given */
10476 while (*flags != NUL)
10477 {
10478 switch (*flags)
10479 {
10480 case 'b': dir = BACKWARD; break;
10481 case 'w': p_ws = TRUE; break;
10482 case 'W': p_ws = FALSE; break;
10483 default: mask = 0;
10484 if (flagsp != NULL)
10485 switch (*flags)
10486 {
10487 case 'c': mask = SP_START; break;
10488 case 'e': mask = SP_END; break;
10489 case 'm': mask = SP_RETCOUNT; break;
10490 case 'n': mask = SP_NOMOVE; break;
10491 case 'p': mask = SP_SUBPAT; break;
10492 case 'r': mask = SP_REPEAT; break;
10493 case 's': mask = SP_SETPCMARK; break;
10494 case 'z': mask = SP_COLUMN; break;
10495 }
10496 if (mask == 0)
10497 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010498 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010499 dir = 0;
10500 }
10501 else
10502 *flagsp |= mask;
10503 }
10504 if (dir == 0)
10505 break;
10506 ++flags;
10507 }
10508 }
10509 return dir;
10510}
10511
10512/*
10513 * Shared by search() and searchpos() functions.
10514 */
10515 static int
10516search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
10517{
10518 int flags;
10519 char_u *pat;
10520 pos_T pos;
10521 pos_T save_cursor;
10522 int save_p_ws = p_ws;
10523 int dir;
10524 int retval = 0; /* default: FAIL */
10525 long lnum_stop = 0;
10526 proftime_T tm;
10527#ifdef FEAT_RELTIME
10528 long time_limit = 0;
10529#endif
10530 int options = SEARCH_KEEP;
10531 int subpatnum;
10532
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010533 pat = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010534 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
10535 if (dir == 0)
10536 goto theend;
10537 flags = *flagsp;
10538 if (flags & SP_START)
10539 options |= SEARCH_START;
10540 if (flags & SP_END)
10541 options |= SEARCH_END;
10542 if (flags & SP_COLUMN)
10543 options |= SEARCH_COL;
10544
10545 /* Optional arguments: line number to stop searching and timeout. */
10546 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
10547 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010548 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010549 if (lnum_stop < 0)
10550 goto theend;
10551#ifdef FEAT_RELTIME
10552 if (argvars[3].v_type != VAR_UNKNOWN)
10553 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010554 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010555 if (time_limit < 0)
10556 goto theend;
10557 }
10558#endif
10559 }
10560
10561#ifdef FEAT_RELTIME
10562 /* Set the time limit, if there is one. */
10563 profile_setlimit(time_limit, &tm);
10564#endif
10565
10566 /*
10567 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
10568 * Check to make sure only those flags are set.
10569 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
10570 * flags cannot be set. Check for that condition also.
10571 */
10572 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
10573 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10574 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010575 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010576 goto theend;
10577 }
10578
10579 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010580 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010581 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010582 if (subpatnum != FAIL)
10583 {
10584 if (flags & SP_SUBPAT)
10585 retval = subpatnum;
10586 else
10587 retval = pos.lnum;
10588 if (flags & SP_SETPCMARK)
10589 setpcmark();
10590 curwin->w_cursor = pos;
10591 if (match_pos != NULL)
10592 {
10593 /* Store the match cursor position */
10594 match_pos->lnum = pos.lnum;
10595 match_pos->col = pos.col + 1;
10596 }
10597 /* "/$" will put the cursor after the end of the line, may need to
10598 * correct that here */
10599 check_cursor();
10600 }
10601
10602 /* If 'n' flag is used: restore cursor position. */
10603 if (flags & SP_NOMOVE)
10604 curwin->w_cursor = save_cursor;
10605 else
10606 curwin->w_set_curswant = TRUE;
10607theend:
10608 p_ws = save_p_ws;
10609
10610 return retval;
10611}
10612
10613#ifdef FEAT_FLOAT
10614
10615/*
10616 * round() is not in C90, use ceil() or floor() instead.
10617 */
10618 float_T
10619vim_round(float_T f)
10620{
10621 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
10622}
10623
10624/*
10625 * "round({float})" function
10626 */
10627 static void
10628f_round(typval_T *argvars, typval_T *rettv)
10629{
10630 float_T f = 0.0;
10631
10632 rettv->v_type = VAR_FLOAT;
10633 if (get_float_arg(argvars, &f) == OK)
10634 rettv->vval.v_float = vim_round(f);
10635 else
10636 rettv->vval.v_float = 0.0;
10637}
10638#endif
10639
Bram Moolenaare99be0e2019-03-26 22:51:09 +010010640#ifdef FEAT_RUBY
10641/*
10642 * "rubyeval()" function
10643 */
10644 static void
10645f_rubyeval(typval_T *argvars, typval_T *rettv)
10646{
10647 char_u *str;
10648 char_u buf[NUMBUFLEN];
10649
10650 str = tv_get_string_buf(&argvars[0], buf);
10651 do_rubyeval(str, rettv);
10652}
10653#endif
10654
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010655/*
10656 * "screenattr()" function
10657 */
10658 static void
10659f_screenattr(typval_T *argvars, typval_T *rettv)
10660{
10661 int row;
10662 int col;
10663 int c;
10664
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010665 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10666 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010667 if (row < 0 || row >= screen_Rows
10668 || col < 0 || col >= screen_Columns)
10669 c = -1;
10670 else
10671 c = ScreenAttrs[LineOffset[row] + col];
10672 rettv->vval.v_number = c;
10673}
10674
10675/*
10676 * "screenchar()" function
10677 */
10678 static void
10679f_screenchar(typval_T *argvars, typval_T *rettv)
10680{
10681 int row;
10682 int col;
10683 int off;
10684 int c;
10685
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010686 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10687 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010688 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010689 c = -1;
10690 else
10691 {
10692 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010693 if (enc_utf8 && ScreenLinesUC[off] != 0)
10694 c = ScreenLinesUC[off];
10695 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010696 c = ScreenLines[off];
10697 }
10698 rettv->vval.v_number = c;
10699}
10700
10701/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010702 * "screenchars()" function
10703 */
10704 static void
10705f_screenchars(typval_T *argvars, typval_T *rettv)
10706{
10707 int row;
10708 int col;
10709 int off;
10710 int c;
10711 int i;
10712
10713 if (rettv_list_alloc(rettv) == FAIL)
10714 return;
10715 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10716 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10717 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10718 return;
10719
10720 off = LineOffset[row] + col;
10721 if (enc_utf8 && ScreenLinesUC[off] != 0)
10722 c = ScreenLinesUC[off];
10723 else
10724 c = ScreenLines[off];
10725 list_append_number(rettv->vval.v_list, (varnumber_T)c);
10726
10727 if (enc_utf8)
10728
10729 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10730 list_append_number(rettv->vval.v_list,
10731 (varnumber_T)ScreenLinesC[i][off]);
10732}
10733
10734/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010735 * "screencol()" function
10736 *
10737 * First column is 1 to be consistent with virtcol().
10738 */
10739 static void
10740f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10741{
10742 rettv->vval.v_number = screen_screencol() + 1;
10743}
10744
10745/*
10746 * "screenrow()" function
10747 */
10748 static void
10749f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10750{
10751 rettv->vval.v_number = screen_screenrow() + 1;
10752}
10753
10754/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010755 * "screenstring()" function
10756 */
10757 static void
10758f_screenstring(typval_T *argvars, typval_T *rettv)
10759{
10760 int row;
10761 int col;
10762 int off;
10763 int c;
10764 int i;
10765 char_u buf[MB_MAXBYTES + 1];
10766 int buflen = 0;
10767
10768 rettv->vval.v_string = NULL;
10769 rettv->v_type = VAR_STRING;
10770
10771 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10772 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10773 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10774 return;
10775
10776 off = LineOffset[row] + col;
10777 if (enc_utf8 && ScreenLinesUC[off] != 0)
10778 c = ScreenLinesUC[off];
10779 else
10780 c = ScreenLines[off];
10781 buflen += mb_char2bytes(c, buf);
10782
10783 if (enc_utf8)
10784 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10785 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
10786
10787 buf[buflen] = NUL;
10788 rettv->vval.v_string = vim_strsave(buf);
10789}
10790
10791/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010792 * "search()" function
10793 */
10794 static void
10795f_search(typval_T *argvars, typval_T *rettv)
10796{
10797 int flags = 0;
10798
10799 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10800}
10801
10802/*
10803 * "searchdecl()" function
10804 */
10805 static void
10806f_searchdecl(typval_T *argvars, typval_T *rettv)
10807{
10808 int locally = 1;
10809 int thisblock = 0;
10810 int error = FALSE;
10811 char_u *name;
10812
10813 rettv->vval.v_number = 1; /* default: FAIL */
10814
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010815 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010816 if (argvars[1].v_type != VAR_UNKNOWN)
10817 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010818 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010819 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010820 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010821 }
10822 if (!error && name != NULL)
10823 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10824 locally, thisblock, SEARCH_KEEP) == FAIL;
10825}
10826
10827/*
10828 * Used by searchpair() and searchpairpos()
10829 */
10830 static int
10831searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10832{
10833 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010834 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010835 int save_p_ws = p_ws;
10836 int dir;
10837 int flags = 0;
10838 char_u nbuf1[NUMBUFLEN];
10839 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010840 int retval = 0; /* default: FAIL */
10841 long lnum_stop = 0;
10842 long time_limit = 0;
10843
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010844 /* Get the three pattern arguments: start, middle, end. Will result in an
10845 * error if not a valid argument. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010846 spat = tv_get_string_chk(&argvars[0]);
10847 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
10848 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010849 if (spat == NULL || mpat == NULL || epat == NULL)
10850 goto theend; /* type error */
10851
10852 /* Handle the optional fourth argument: flags */
10853 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
10854 if (dir == 0)
10855 goto theend;
10856
10857 /* Don't accept SP_END or SP_SUBPAT.
10858 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
10859 */
10860 if ((flags & (SP_END | SP_SUBPAT)) != 0
10861 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10862 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010863 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010864 goto theend;
10865 }
10866
10867 /* Using 'r' implies 'W', otherwise it doesn't work. */
10868 if (flags & SP_REPEAT)
10869 p_ws = FALSE;
10870
10871 /* Optional fifth argument: skip expression */
10872 if (argvars[3].v_type == VAR_UNKNOWN
10873 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010010874 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010875 else
10876 {
Bram Moolenaar48570482017-10-30 21:48:41 +010010877 skip = &argvars[4];
10878 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
10879 && skip->v_type != VAR_STRING)
10880 {
10881 /* Type error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010882 semsg(_(e_invarg2), tv_get_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010010883 goto theend;
10884 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010885 if (argvars[5].v_type != VAR_UNKNOWN)
10886 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010887 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010888 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010889 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010890 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010891 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010892 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010893#ifdef FEAT_RELTIME
10894 if (argvars[6].v_type != VAR_UNKNOWN)
10895 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010896 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010897 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010898 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010899 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010900 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010901 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010902 }
10903#endif
10904 }
10905 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010906
10907 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
10908 match_pos, lnum_stop, time_limit);
10909
10910theend:
10911 p_ws = save_p_ws;
10912
10913 return retval;
10914}
10915
10916/*
10917 * "searchpair()" function
10918 */
10919 static void
10920f_searchpair(typval_T *argvars, typval_T *rettv)
10921{
10922 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
10923}
10924
10925/*
10926 * "searchpairpos()" function
10927 */
10928 static void
10929f_searchpairpos(typval_T *argvars, typval_T *rettv)
10930{
10931 pos_T match_pos;
10932 int lnum = 0;
10933 int col = 0;
10934
10935 if (rettv_list_alloc(rettv) == FAIL)
10936 return;
10937
10938 if (searchpair_cmn(argvars, &match_pos) > 0)
10939 {
10940 lnum = match_pos.lnum;
10941 col = match_pos.col;
10942 }
10943
10944 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10945 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10946}
10947
10948/*
10949 * Search for a start/middle/end thing.
10950 * Used by searchpair(), see its documentation for the details.
10951 * Returns 0 or -1 for no match,
10952 */
10953 long
10954do_searchpair(
10955 char_u *spat, /* start pattern */
10956 char_u *mpat, /* middle pattern */
10957 char_u *epat, /* end pattern */
10958 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010010959 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010960 int flags, /* SP_SETPCMARK and other SP_ values */
10961 pos_T *match_pos,
10962 linenr_T lnum_stop, /* stop at this line if not zero */
10963 long time_limit UNUSED) /* stop after this many msec */
10964{
10965 char_u *save_cpo;
10966 char_u *pat, *pat2 = NULL, *pat3 = NULL;
10967 long retval = 0;
10968 pos_T pos;
10969 pos_T firstpos;
10970 pos_T foundpos;
10971 pos_T save_cursor;
10972 pos_T save_pos;
10973 int n;
10974 int r;
10975 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010010976 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010977 int err;
10978 int options = SEARCH_KEEP;
10979 proftime_T tm;
10980
10981 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10982 save_cpo = p_cpo;
10983 p_cpo = empty_option;
10984
10985#ifdef FEAT_RELTIME
10986 /* Set the time limit, if there is one. */
10987 profile_setlimit(time_limit, &tm);
10988#endif
10989
10990 /* Make two search patterns: start/end (pat2, for in nested pairs) and
10991 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010992 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
10993 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010994 if (pat2 == NULL || pat3 == NULL)
10995 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010996 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010997 if (*mpat == NUL)
10998 STRCPY(pat3, pat2);
10999 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010011000 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011001 spat, epat, mpat);
11002 if (flags & SP_START)
11003 options |= SEARCH_START;
11004
Bram Moolenaar48570482017-10-30 21:48:41 +010011005 if (skip != NULL)
11006 {
11007 /* Empty string means to not use the skip expression. */
11008 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
11009 use_skip = skip->vval.v_string != NULL
11010 && *skip->vval.v_string != NUL;
11011 }
11012
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011013 save_cursor = curwin->w_cursor;
11014 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011015 CLEAR_POS(&firstpos);
11016 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011017 pat = pat3;
11018 for (;;)
11019 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +010011020 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020011021 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011022 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011023 /* didn't find it or found the first match again: FAIL */
11024 break;
11025
11026 if (firstpos.lnum == 0)
11027 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011028 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011029 {
11030 /* Found the same position again. Can happen with a pattern that
11031 * has "\zs" at the end and searching backwards. Advance one
11032 * character and try again. */
11033 if (dir == BACKWARD)
11034 decl(&pos);
11035 else
11036 incl(&pos);
11037 }
11038 foundpos = pos;
11039
11040 /* clear the start flag to avoid getting stuck here */
11041 options &= ~SEARCH_START;
11042
11043 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010011044 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011045 {
11046 save_pos = curwin->w_cursor;
11047 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010011048 err = FALSE;
11049 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011050 curwin->w_cursor = save_pos;
11051 if (err)
11052 {
11053 /* Evaluating {skip} caused an error, break here. */
11054 curwin->w_cursor = save_cursor;
11055 retval = -1;
11056 break;
11057 }
11058 if (r)
11059 continue;
11060 }
11061
11062 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
11063 {
11064 /* Found end when searching backwards or start when searching
11065 * forward: nested pair. */
11066 ++nest;
11067 pat = pat2; /* nested, don't search for middle */
11068 }
11069 else
11070 {
11071 /* Found end when searching forward or start when searching
11072 * backward: end of (nested) pair; or found middle in outer pair. */
11073 if (--nest == 1)
11074 pat = pat3; /* outer level, search for middle */
11075 }
11076
11077 if (nest == 0)
11078 {
11079 /* Found the match: return matchcount or line number. */
11080 if (flags & SP_RETCOUNT)
11081 ++retval;
11082 else
11083 retval = pos.lnum;
11084 if (flags & SP_SETPCMARK)
11085 setpcmark();
11086 curwin->w_cursor = pos;
11087 if (!(flags & SP_REPEAT))
11088 break;
11089 nest = 1; /* search for next unmatched */
11090 }
11091 }
11092
11093 if (match_pos != NULL)
11094 {
11095 /* Store the match cursor position */
11096 match_pos->lnum = curwin->w_cursor.lnum;
11097 match_pos->col = curwin->w_cursor.col + 1;
11098 }
11099
11100 /* If 'n' flag is used or search failed: restore cursor position. */
11101 if ((flags & SP_NOMOVE) || retval == 0)
11102 curwin->w_cursor = save_cursor;
11103
11104theend:
11105 vim_free(pat2);
11106 vim_free(pat3);
11107 if (p_cpo == empty_option)
11108 p_cpo = save_cpo;
11109 else
11110 /* Darn, evaluating the {skip} expression changed the value. */
11111 free_string_option(save_cpo);
11112
11113 return retval;
11114}
11115
11116/*
11117 * "searchpos()" function
11118 */
11119 static void
11120f_searchpos(typval_T *argvars, typval_T *rettv)
11121{
11122 pos_T match_pos;
11123 int lnum = 0;
11124 int col = 0;
11125 int n;
11126 int flags = 0;
11127
11128 if (rettv_list_alloc(rettv) == FAIL)
11129 return;
11130
11131 n = search_cmn(argvars, &match_pos, &flags);
11132 if (n > 0)
11133 {
11134 lnum = match_pos.lnum;
11135 col = match_pos.col;
11136 }
11137
11138 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
11139 list_append_number(rettv->vval.v_list, (varnumber_T)col);
11140 if (flags & SP_SUBPAT)
11141 list_append_number(rettv->vval.v_list, (varnumber_T)n);
11142}
11143
11144 static void
11145f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
11146{
11147#ifdef FEAT_CLIENTSERVER
11148 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011149 char_u *server = tv_get_string_chk(&argvars[0]);
11150 char_u *reply = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011151
11152 rettv->vval.v_number = -1;
11153 if (server == NULL || reply == NULL)
11154 return;
11155 if (check_restricted() || check_secure())
11156 return;
11157# ifdef FEAT_X11
11158 if (check_connection() == FAIL)
11159 return;
11160# endif
11161
11162 if (serverSendReply(server, reply) < 0)
11163 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011164 emsg(_("E258: Unable to send to client"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011165 return;
11166 }
11167 rettv->vval.v_number = 0;
11168#else
11169 rettv->vval.v_number = -1;
11170#endif
11171}
11172
11173 static void
11174f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
11175{
11176 char_u *r = NULL;
11177
11178#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +010011179# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011180 r = serverGetVimNames();
11181# else
11182 make_connection();
11183 if (X_DISPLAY != NULL)
11184 r = serverGetVimNames(X_DISPLAY);
11185# endif
11186#endif
11187 rettv->v_type = VAR_STRING;
11188 rettv->vval.v_string = r;
11189}
11190
11191/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011192 * "setbufline()" function
11193 */
11194 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020011195f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011196{
11197 linenr_T lnum;
11198 buf_T *buf;
11199
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011200 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011201 if (buf == NULL)
11202 rettv->vval.v_number = 1; /* FAIL */
11203 else
11204 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011205 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020011206 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011207 }
11208}
11209
11210/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011211 * "setbufvar()" function
11212 */
11213 static void
11214f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
11215{
11216 buf_T *buf;
11217 char_u *varname, *bufvarname;
11218 typval_T *varp;
11219 char_u nbuf[NUMBUFLEN];
11220
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011221 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011222 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011223 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
11224 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011225 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011226 varp = &argvars[2];
11227
11228 if (buf != NULL && varname != NULL && varp != NULL)
11229 {
11230 if (*varname == '&')
11231 {
11232 long numval;
11233 char_u *strval;
11234 int error = FALSE;
11235 aco_save_T aco;
11236
11237 /* set curbuf to be our buf, temporarily */
11238 aucmd_prepbuf(&aco, buf);
11239
11240 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011241 numval = (long)tv_get_number_chk(varp, &error);
11242 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011243 if (!error && strval != NULL)
11244 set_option_value(varname, numval, strval, OPT_LOCAL);
11245
11246 /* reset notion of buffer */
11247 aucmd_restbuf(&aco);
11248 }
11249 else
11250 {
11251 buf_T *save_curbuf = curbuf;
11252
11253 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
11254 if (bufvarname != NULL)
11255 {
11256 curbuf = buf;
11257 STRCPY(bufvarname, "b:");
11258 STRCPY(bufvarname + 2, varname);
11259 set_var(bufvarname, varp, TRUE);
11260 vim_free(bufvarname);
11261 curbuf = save_curbuf;
11262 }
11263 }
11264 }
11265}
11266
11267 static void
11268f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
11269{
11270 dict_T *d;
11271 dictitem_T *di;
11272 char_u *csearch;
11273
11274 if (argvars[0].v_type != VAR_DICT)
11275 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011276 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011277 return;
11278 }
11279
11280 if ((d = argvars[0].vval.v_dict) != NULL)
11281 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010011282 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011283 if (csearch != NULL)
11284 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011285 if (enc_utf8)
11286 {
11287 int pcc[MAX_MCO];
11288 int c = utfc_ptr2char(csearch, pcc);
11289
11290 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
11291 }
11292 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011293 set_last_csearch(PTR2CHAR(csearch),
11294 csearch, MB_PTR2LEN(csearch));
11295 }
11296
11297 di = dict_find(d, (char_u *)"forward", -1);
11298 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011299 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011300 ? FORWARD : BACKWARD);
11301
11302 di = dict_find(d, (char_u *)"until", -1);
11303 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011304 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011305 }
11306}
11307
11308/*
11309 * "setcmdpos()" function
11310 */
11311 static void
11312f_setcmdpos(typval_T *argvars, typval_T *rettv)
11313{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011314 int pos = (int)tv_get_number(&argvars[0]) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011315
11316 if (pos >= 0)
11317 rettv->vval.v_number = set_cmdline_pos(pos);
11318}
11319
11320/*
11321 * "setfperm({fname}, {mode})" function
11322 */
11323 static void
11324f_setfperm(typval_T *argvars, typval_T *rettv)
11325{
11326 char_u *fname;
11327 char_u modebuf[NUMBUFLEN];
11328 char_u *mode_str;
11329 int i;
11330 int mask;
11331 int mode = 0;
11332
11333 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011334 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011335 if (fname == NULL)
11336 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011337 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011338 if (mode_str == NULL)
11339 return;
11340 if (STRLEN(mode_str) != 9)
11341 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011342 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011343 return;
11344 }
11345
11346 mask = 1;
11347 for (i = 8; i >= 0; --i)
11348 {
11349 if (mode_str[i] != '-')
11350 mode |= mask;
11351 mask = mask << 1;
11352 }
11353 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
11354}
11355
11356/*
11357 * "setline()" function
11358 */
11359 static void
11360f_setline(typval_T *argvars, typval_T *rettv)
11361{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011362 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011363
Bram Moolenaarca851592018-06-06 21:04:07 +020011364 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011365}
11366
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011367/*
11368 * Used by "setqflist()" and "setloclist()" functions
11369 */
11370 static void
11371set_qf_ll_list(
11372 win_T *wp UNUSED,
11373 typval_T *list_arg UNUSED,
11374 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020011375 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011376 typval_T *rettv)
11377{
11378#ifdef FEAT_QUICKFIX
11379 static char *e_invact = N_("E927: Invalid action: '%s'");
11380 char_u *act;
11381 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011382 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011383#endif
11384
11385 rettv->vval.v_number = -1;
11386
11387#ifdef FEAT_QUICKFIX
11388 if (list_arg->v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011389 emsg(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011390 else if (recursive != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011391 emsg(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011392 else
11393 {
11394 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011395 dict_T *d = NULL;
11396 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011397
11398 if (action_arg->v_type == VAR_STRING)
11399 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011400 act = tv_get_string_chk(action_arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011401 if (act == NULL)
11402 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020011403 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
11404 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011405 action = *act;
11406 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011407 semsg(_(e_invact), act);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011408 }
11409 else if (action_arg->v_type == VAR_UNKNOWN)
11410 action = ' ';
11411 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011412 emsg(_(e_stringreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011413
Bram Moolenaard823fa92016-08-12 16:29:27 +020011414 if (action_arg->v_type != VAR_UNKNOWN
11415 && what_arg->v_type != VAR_UNKNOWN)
11416 {
11417 if (what_arg->v_type == VAR_DICT)
11418 d = what_arg->vval.v_dict;
11419 else
11420 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011421 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +020011422 valid_dict = FALSE;
11423 }
11424 }
11425
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011426 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011427 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011428 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
11429 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011430 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011431 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011432 }
11433#endif
11434}
11435
11436/*
11437 * "setloclist()" function
11438 */
11439 static void
11440f_setloclist(typval_T *argvars, typval_T *rettv)
11441{
11442 win_T *win;
11443
11444 rettv->vval.v_number = -1;
11445
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020011446 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011447 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020011448 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011449}
11450
11451/*
11452 * "setmatches()" function
11453 */
11454 static void
11455f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
11456{
11457#ifdef FEAT_SEARCH_EXTRA
11458 list_T *l;
11459 listitem_T *li;
11460 dict_T *d;
11461 list_T *s = NULL;
Bram Moolenaaraff74912019-03-30 18:11:49 +010011462 win_T *win = get_optional_window(argvars, 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011463
11464 rettv->vval.v_number = -1;
11465 if (argvars[0].v_type != VAR_LIST)
11466 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011467 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011468 return;
11469 }
Bram Moolenaaraff74912019-03-30 18:11:49 +010011470 if (win == NULL)
11471 return;
11472
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011473 if ((l = argvars[0].vval.v_list) != NULL)
11474 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011475 /* To some extent make sure that we are dealing with a list from
11476 * "getmatches()". */
11477 li = l->lv_first;
11478 while (li != NULL)
11479 {
11480 if (li->li_tv.v_type != VAR_DICT
11481 || (d = li->li_tv.vval.v_dict) == NULL)
11482 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011483 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011484 return;
11485 }
11486 if (!(dict_find(d, (char_u *)"group", -1) != NULL
11487 && (dict_find(d, (char_u *)"pattern", -1) != NULL
11488 || dict_find(d, (char_u *)"pos1", -1) != NULL)
11489 && dict_find(d, (char_u *)"priority", -1) != NULL
11490 && dict_find(d, (char_u *)"id", -1) != NULL))
11491 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011492 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011493 return;
11494 }
11495 li = li->li_next;
11496 }
11497
Bram Moolenaaraff74912019-03-30 18:11:49 +010011498 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011499 li = l->lv_first;
11500 while (li != NULL)
11501 {
11502 int i = 0;
Bram Moolenaar54315892019-04-26 22:33:49 +020011503 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011504 dictitem_T *di;
11505 char_u *group;
11506 int priority;
11507 int id;
11508 char_u *conceal;
11509
11510 d = li->li_tv.vval.v_dict;
11511 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
11512 {
11513 if (s == NULL)
11514 {
11515 s = list_alloc();
11516 if (s == NULL)
11517 return;
11518 }
11519
11520 /* match from matchaddpos() */
11521 for (i = 1; i < 9; i++)
11522 {
11523 sprintf((char *)buf, (char *)"pos%d", i);
11524 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
11525 {
11526 if (di->di_tv.v_type != VAR_LIST)
11527 return;
11528
11529 list_append_tv(s, &di->di_tv);
11530 s->lv_refcount++;
11531 }
11532 else
11533 break;
11534 }
11535 }
11536
Bram Moolenaar8f667172018-12-14 15:38:31 +010011537 group = dict_get_string(d, (char_u *)"group", TRUE);
11538 priority = (int)dict_get_number(d, (char_u *)"priority");
11539 id = (int)dict_get_number(d, (char_u *)"id");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011540 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar8f667172018-12-14 15:38:31 +010011541 ? dict_get_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011542 : NULL;
11543 if (i == 0)
11544 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011545 match_add(win, group,
Bram Moolenaar8f667172018-12-14 15:38:31 +010011546 dict_get_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011547 priority, id, NULL, conceal);
11548 }
11549 else
11550 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011551 match_add(win, group, NULL, priority, id, s, conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011552 list_unref(s);
11553 s = NULL;
11554 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020011555 vim_free(group);
11556 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011557
11558 li = li->li_next;
11559 }
11560 rettv->vval.v_number = 0;
11561 }
11562#endif
11563}
11564
11565/*
11566 * "setpos()" function
11567 */
11568 static void
11569f_setpos(typval_T *argvars, typval_T *rettv)
11570{
11571 pos_T pos;
11572 int fnum;
11573 char_u *name;
11574 colnr_T curswant = -1;
11575
11576 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011577 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011578 if (name != NULL)
11579 {
11580 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
11581 {
11582 if (--pos.col < 0)
11583 pos.col = 0;
11584 if (name[0] == '.' && name[1] == NUL)
11585 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011586 /* set cursor; "fnum" is ignored */
11587 curwin->w_cursor = pos;
11588 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011589 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011590 curwin->w_curswant = curswant - 1;
11591 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011592 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011593 check_cursor();
11594 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011595 }
11596 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
11597 {
11598 /* set mark */
11599 if (setmark_pos(name[1], &pos, fnum) == OK)
11600 rettv->vval.v_number = 0;
11601 }
11602 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011603 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011604 }
11605 }
11606}
11607
11608/*
11609 * "setqflist()" function
11610 */
11611 static void
11612f_setqflist(typval_T *argvars, typval_T *rettv)
11613{
Bram Moolenaard823fa92016-08-12 16:29:27 +020011614 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011615}
11616
11617/*
11618 * "setreg()" function
11619 */
11620 static void
11621f_setreg(typval_T *argvars, typval_T *rettv)
11622{
11623 int regname;
11624 char_u *strregname;
11625 char_u *stropt;
11626 char_u *strval;
11627 int append;
11628 char_u yank_type;
11629 long block_len;
11630
11631 block_len = -1;
11632 yank_type = MAUTO;
11633 append = FALSE;
11634
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011635 strregname = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011636 rettv->vval.v_number = 1; /* FAIL is default */
11637
11638 if (strregname == NULL)
11639 return; /* type error; errmsg already given */
11640 regname = *strregname;
11641 if (regname == 0 || regname == '@')
11642 regname = '"';
11643
11644 if (argvars[2].v_type != VAR_UNKNOWN)
11645 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011646 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011647 if (stropt == NULL)
11648 return; /* type error */
11649 for (; *stropt != NUL; ++stropt)
11650 switch (*stropt)
11651 {
11652 case 'a': case 'A': /* append */
11653 append = TRUE;
11654 break;
11655 case 'v': case 'c': /* character-wise selection */
11656 yank_type = MCHAR;
11657 break;
11658 case 'V': case 'l': /* line-wise selection */
11659 yank_type = MLINE;
11660 break;
11661 case 'b': case Ctrl_V: /* block-wise selection */
11662 yank_type = MBLOCK;
11663 if (VIM_ISDIGIT(stropt[1]))
11664 {
11665 ++stropt;
11666 block_len = getdigits(&stropt) - 1;
11667 --stropt;
11668 }
11669 break;
11670 }
11671 }
11672
11673 if (argvars[1].v_type == VAR_LIST)
11674 {
11675 char_u **lstval;
11676 char_u **allocval;
11677 char_u buf[NUMBUFLEN];
11678 char_u **curval;
11679 char_u **curallocval;
11680 list_T *ll = argvars[1].vval.v_list;
11681 listitem_T *li;
11682 int len;
11683
11684 /* If the list is NULL handle like an empty list. */
11685 len = ll == NULL ? 0 : ll->lv_len;
11686
11687 /* First half: use for pointers to result lines; second half: use for
11688 * pointers to allocated copies. */
11689 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
11690 if (lstval == NULL)
11691 return;
11692 curval = lstval;
11693 allocval = lstval + len + 2;
11694 curallocval = allocval;
11695
11696 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
11697 li = li->li_next)
11698 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011699 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011700 if (strval == NULL)
11701 goto free_lstval;
11702 if (strval == buf)
11703 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011704 /* Need to make a copy, next tv_get_string_buf_chk() will
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011705 * overwrite the string. */
11706 strval = vim_strsave(buf);
11707 if (strval == NULL)
11708 goto free_lstval;
11709 *curallocval++ = strval;
11710 }
11711 *curval++ = strval;
11712 }
11713 *curval++ = NULL;
11714
11715 write_reg_contents_lst(regname, lstval, -1,
11716 append, yank_type, block_len);
11717free_lstval:
11718 while (curallocval > allocval)
11719 vim_free(*--curallocval);
11720 vim_free(lstval);
11721 }
11722 else
11723 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011724 strval = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011725 if (strval == NULL)
11726 return;
11727 write_reg_contents_ex(regname, strval, -1,
11728 append, yank_type, block_len);
11729 }
11730 rettv->vval.v_number = 0;
11731}
11732
11733/*
11734 * "settabvar()" function
11735 */
11736 static void
11737f_settabvar(typval_T *argvars, typval_T *rettv)
11738{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011739 tabpage_T *save_curtab;
11740 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011741 char_u *varname, *tabvarname;
11742 typval_T *varp;
11743
11744 rettv->vval.v_number = 0;
11745
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011746 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011747 return;
11748
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011749 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
11750 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011751 varp = &argvars[2];
11752
Bram Moolenaar4033c552017-09-16 20:54:51 +020011753 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011754 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011755 save_curtab = curtab;
11756 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011757
11758 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
11759 if (tabvarname != NULL)
11760 {
11761 STRCPY(tabvarname, "t:");
11762 STRCPY(tabvarname + 2, varname);
11763 set_var(tabvarname, varp, TRUE);
11764 vim_free(tabvarname);
11765 }
11766
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011767 /* Restore current tabpage */
11768 if (valid_tabpage(save_curtab))
11769 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011770 }
11771}
11772
11773/*
11774 * "settabwinvar()" function
11775 */
11776 static void
11777f_settabwinvar(typval_T *argvars, typval_T *rettv)
11778{
11779 setwinvar(argvars, rettv, 1);
11780}
11781
11782/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011783 * "settagstack()" function
11784 */
11785 static void
11786f_settagstack(typval_T *argvars, typval_T *rettv)
11787{
11788 static char *e_invact2 = N_("E962: Invalid action: '%s'");
11789 win_T *wp;
11790 dict_T *d;
11791 int action = 'r';
11792
11793 rettv->vval.v_number = -1;
11794
11795 // first argument: window number or id
11796 wp = find_win_by_nr_or_id(&argvars[0]);
11797 if (wp == NULL)
11798 return;
11799
11800 // second argument: dict with items to set in the tag stack
11801 if (argvars[1].v_type != VAR_DICT)
11802 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011803 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011804 return;
11805 }
11806 d = argvars[1].vval.v_dict;
11807 if (d == NULL)
11808 return;
11809
11810 // third argument: action - 'a' for append and 'r' for replace.
11811 // default is to replace the stack.
11812 if (argvars[2].v_type == VAR_UNKNOWN)
11813 action = 'r';
11814 else if (argvars[2].v_type == VAR_STRING)
11815 {
11816 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011817 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011818 if (actstr == NULL)
11819 return;
11820 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
11821 action = *actstr;
11822 else
11823 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011824 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011825 return;
11826 }
11827 }
11828 else
11829 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011830 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011831 return;
11832 }
11833
11834 if (set_tagstack(wp, d, action) == OK)
11835 rettv->vval.v_number = 0;
11836}
11837
11838/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011839 * "setwinvar()" function
11840 */
11841 static void
11842f_setwinvar(typval_T *argvars, typval_T *rettv)
11843{
11844 setwinvar(argvars, rettv, 0);
11845}
11846
11847#ifdef FEAT_CRYPT
11848/*
11849 * "sha256({string})" function
11850 */
11851 static void
11852f_sha256(typval_T *argvars, typval_T *rettv)
11853{
11854 char_u *p;
11855
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011856 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011857 rettv->vval.v_string = vim_strsave(
11858 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
11859 rettv->v_type = VAR_STRING;
11860}
11861#endif /* FEAT_CRYPT */
11862
11863/*
11864 * "shellescape({string})" function
11865 */
11866 static void
11867f_shellescape(typval_T *argvars, typval_T *rettv)
11868{
Bram Moolenaar20615522017-06-05 18:46:26 +020011869 int do_special = non_zero_arg(&argvars[1]);
11870
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011871 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011872 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011873 rettv->v_type = VAR_STRING;
11874}
11875
11876/*
11877 * shiftwidth() function
11878 */
11879 static void
11880f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
11881{
Bram Moolenaarf9514162018-11-22 03:08:29 +010011882 rettv->vval.v_number = 0;
11883
11884 if (argvars[0].v_type != VAR_UNKNOWN)
11885 {
11886 long col;
11887
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011888 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +010011889 if (col < 0)
11890 return; // type error; errmsg already given
11891#ifdef FEAT_VARTABS
11892 rettv->vval.v_number = get_sw_value_col(curbuf, col);
11893 return;
11894#endif
11895 }
11896
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011897 rettv->vval.v_number = get_sw_value(curbuf);
11898}
11899
Bram Moolenaar162b7142018-12-21 15:17:36 +010011900#ifdef FEAT_SIGNS
11901/*
11902 * "sign_define()" function
11903 */
11904 static void
11905f_sign_define(typval_T *argvars, typval_T *rettv)
11906{
11907 char_u *name;
11908 dict_T *dict;
11909 char_u *icon = NULL;
11910 char_u *linehl = NULL;
11911 char_u *text = NULL;
11912 char_u *texthl = NULL;
11913
11914 rettv->vval.v_number = -1;
11915
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011916 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011917 if (name == NULL)
11918 return;
11919
11920 if (argvars[1].v_type != VAR_UNKNOWN)
11921 {
11922 if (argvars[1].v_type != VAR_DICT)
11923 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011924 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011925 return;
11926 }
11927
11928 // sign attributes
11929 dict = argvars[1].vval.v_dict;
11930 if (dict_find(dict, (char_u *)"icon", -1) != NULL)
11931 icon = dict_get_string(dict, (char_u *)"icon", TRUE);
11932 if (dict_find(dict, (char_u *)"linehl", -1) != NULL)
11933 linehl = dict_get_string(dict, (char_u *)"linehl", TRUE);
11934 if (dict_find(dict, (char_u *)"text", -1) != NULL)
11935 text = dict_get_string(dict, (char_u *)"text", TRUE);
11936 if (dict_find(dict, (char_u *)"texthl", -1) != NULL)
11937 texthl = dict_get_string(dict, (char_u *)"texthl", TRUE);
11938 }
11939
11940 if (sign_define_by_name(name, icon, linehl, text, texthl) == OK)
11941 rettv->vval.v_number = 0;
11942
11943 vim_free(icon);
11944 vim_free(linehl);
11945 vim_free(text);
11946 vim_free(texthl);
11947}
11948
11949/*
11950 * "sign_getdefined()" function
11951 */
11952 static void
11953f_sign_getdefined(typval_T *argvars, typval_T *rettv)
11954{
11955 char_u *name = NULL;
11956
11957 if (rettv_list_alloc_id(rettv, aid_sign_getdefined) != OK)
11958 return;
11959
11960 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011961 name = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011962
11963 sign_getlist(name, rettv->vval.v_list);
11964}
11965
11966/*
11967 * "sign_getplaced()" function
11968 */
11969 static void
11970f_sign_getplaced(typval_T *argvars, typval_T *rettv)
11971{
11972 buf_T *buf = NULL;
11973 dict_T *dict;
11974 dictitem_T *di;
11975 linenr_T lnum = 0;
11976 int sign_id = 0;
11977 char_u *group = NULL;
11978 int notanum = FALSE;
11979
11980 if (rettv_list_alloc_id(rettv, aid_sign_getplaced) != OK)
11981 return;
11982
11983 if (argvars[0].v_type != VAR_UNKNOWN)
11984 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010011985 // get signs placed in the specified buffer
11986 buf = get_buf_arg(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011987 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010011988 return;
Bram Moolenaar162b7142018-12-21 15:17:36 +010011989
11990 if (argvars[1].v_type != VAR_UNKNOWN)
11991 {
11992 if (argvars[1].v_type != VAR_DICT ||
11993 ((dict = argvars[1].vval.v_dict) == NULL))
11994 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011995 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011996 return;
11997 }
11998 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
11999 {
12000 // get signs placed at this line
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012001 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012002 if (notanum)
12003 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012004 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012005 }
12006 if ((di = dict_find(dict, (char_u *)"id", -1)) != NULL)
12007 {
12008 // get sign placed with this identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012009 sign_id = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012010 if (notanum)
12011 return;
12012 }
12013 if ((di = dict_find(dict, (char_u *)"group", -1)) != NULL)
12014 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012015 group = tv_get_string_chk(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012016 if (group == NULL)
12017 return;
Bram Moolenaar6436cd82018-12-27 00:28:33 +010012018 if (*group == '\0') // empty string means global group
12019 group = NULL;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012020 }
12021 }
12022 }
12023
12024 sign_get_placed(buf, lnum, sign_id, group, rettv->vval.v_list);
12025}
12026
12027/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012028 * "sign_jump()" function
12029 */
12030 static void
12031f_sign_jump(typval_T *argvars, typval_T *rettv)
12032{
12033 int sign_id;
12034 char_u *sign_group = NULL;
12035 buf_T *buf;
12036 int notanum = FALSE;
12037
12038 rettv->vval.v_number = -1;
12039
Bram Moolenaarbdace832019-03-02 10:13:42 +010012040 // Sign identifier
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012041 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
12042 if (notanum)
12043 return;
12044 if (sign_id <= 0)
12045 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012046 emsg(_(e_invarg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012047 return;
12048 }
12049
12050 // Sign group
12051 sign_group = tv_get_string_chk(&argvars[1]);
12052 if (sign_group == NULL)
12053 return;
12054 if (sign_group[0] == '\0')
12055 sign_group = NULL; // global sign group
12056 else
12057 {
12058 sign_group = vim_strsave(sign_group);
12059 if (sign_group == NULL)
12060 return;
12061 }
12062
12063 // Buffer to place the sign
12064 buf = get_buf_arg(&argvars[2]);
12065 if (buf == NULL)
12066 goto cleanup;
12067
12068 rettv->vval.v_number = sign_jump(sign_id, sign_group, buf);
12069
12070cleanup:
12071 vim_free(sign_group);
12072}
12073
12074/*
Bram Moolenaar162b7142018-12-21 15:17:36 +010012075 * "sign_place()" function
12076 */
12077 static void
12078f_sign_place(typval_T *argvars, typval_T *rettv)
12079{
12080 int sign_id;
12081 char_u *group = NULL;
12082 char_u *sign_name;
12083 buf_T *buf;
12084 dict_T *dict;
12085 dictitem_T *di;
12086 linenr_T lnum = 0;
12087 int prio = SIGN_DEF_PRIO;
12088 int notanum = FALSE;
12089
12090 rettv->vval.v_number = -1;
12091
Bram Moolenaarbdace832019-03-02 10:13:42 +010012092 // Sign identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012093 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012094 if (notanum)
12095 return;
12096 if (sign_id < 0)
12097 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012098 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012099 return;
12100 }
12101
12102 // Sign group
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012103 group = tv_get_string_chk(&argvars[1]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012104 if (group == NULL)
12105 return;
12106 if (group[0] == '\0')
12107 group = NULL; // global sign group
12108 else
12109 {
12110 group = vim_strsave(group);
12111 if (group == NULL)
12112 return;
12113 }
12114
12115 // Sign name
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012116 sign_name = tv_get_string_chk(&argvars[2]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012117 if (sign_name == NULL)
12118 goto cleanup;
12119
12120 // Buffer to place the sign
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012121 buf = get_buf_arg(&argvars[3]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012122 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012123 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012124
12125 if (argvars[4].v_type != VAR_UNKNOWN)
12126 {
12127 if (argvars[4].v_type != VAR_DICT ||
12128 ((dict = argvars[4].vval.v_dict) == NULL))
12129 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012130 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012131 goto cleanup;
12132 }
12133
12134 // Line number where the sign is to be placed
12135 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
12136 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012137 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012138 if (notanum)
12139 goto cleanup;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012140 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012141 }
12142 if ((di = dict_find(dict, (char_u *)"priority", -1)) != NULL)
12143 {
12144 // Sign priority
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012145 prio = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012146 if (notanum)
12147 goto cleanup;
12148 }
12149 }
12150
12151 if (sign_place(&sign_id, group, sign_name, buf, lnum, prio) == OK)
12152 rettv->vval.v_number = sign_id;
12153
12154cleanup:
12155 vim_free(group);
12156}
12157
12158/*
12159 * "sign_undefine()" function
12160 */
12161 static void
12162f_sign_undefine(typval_T *argvars, typval_T *rettv)
12163{
12164 char_u *name;
12165
12166 rettv->vval.v_number = -1;
12167
12168 if (argvars[0].v_type == VAR_UNKNOWN)
12169 {
12170 // Free all the signs
12171 free_signs();
12172 rettv->vval.v_number = 0;
12173 }
12174 else
12175 {
12176 // Free only the specified sign
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012177 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012178 if (name == NULL)
12179 return;
12180
12181 if (sign_undefine_by_name(name) == OK)
12182 rettv->vval.v_number = 0;
12183 }
12184}
12185
12186/*
12187 * "sign_unplace()" function
12188 */
12189 static void
12190f_sign_unplace(typval_T *argvars, typval_T *rettv)
12191{
12192 dict_T *dict;
12193 dictitem_T *di;
12194 int sign_id = 0;
12195 buf_T *buf = NULL;
12196 char_u *group = NULL;
12197
12198 rettv->vval.v_number = -1;
12199
12200 if (argvars[0].v_type != VAR_STRING)
12201 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012202 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012203 return;
12204 }
12205
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012206 group = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012207 if (group[0] == '\0')
12208 group = NULL; // global sign group
12209 else
12210 {
12211 group = vim_strsave(group);
12212 if (group == NULL)
12213 return;
12214 }
12215
12216 if (argvars[1].v_type != VAR_UNKNOWN)
12217 {
12218 if (argvars[1].v_type != VAR_DICT)
12219 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012220 emsg(_(e_dictreq));
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012221 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012222 }
12223 dict = argvars[1].vval.v_dict;
12224
12225 if ((di = dict_find(dict, (char_u *)"buffer", -1)) != NULL)
12226 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012227 buf = get_buf_arg(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012228 if (buf == NULL)
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012229 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012230 }
12231 if (dict_find(dict, (char_u *)"id", -1) != NULL)
12232 sign_id = dict_get_number(dict, (char_u *)"id");
12233 }
12234
12235 if (buf == NULL)
12236 {
12237 // Delete the sign in all the buffers
12238 FOR_ALL_BUFFERS(buf)
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010012239 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012240 rettv->vval.v_number = 0;
12241 }
12242 else
12243 {
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010012244 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012245 rettv->vval.v_number = 0;
12246 }
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012247
12248cleanup:
Bram Moolenaar162b7142018-12-21 15:17:36 +010012249 vim_free(group);
12250}
12251#endif
12252
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012253/*
12254 * "simplify()" function
12255 */
12256 static void
12257f_simplify(typval_T *argvars, typval_T *rettv)
12258{
12259 char_u *p;
12260
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012261 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012262 rettv->vval.v_string = vim_strsave(p);
12263 simplify_filename(rettv->vval.v_string); /* simplify in place */
12264 rettv->v_type = VAR_STRING;
12265}
12266
12267#ifdef FEAT_FLOAT
12268/*
12269 * "sin()" function
12270 */
12271 static void
12272f_sin(typval_T *argvars, typval_T *rettv)
12273{
12274 float_T f = 0.0;
12275
12276 rettv->v_type = VAR_FLOAT;
12277 if (get_float_arg(argvars, &f) == OK)
12278 rettv->vval.v_float = sin(f);
12279 else
12280 rettv->vval.v_float = 0.0;
12281}
12282
12283/*
12284 * "sinh()" function
12285 */
12286 static void
12287f_sinh(typval_T *argvars, typval_T *rettv)
12288{
12289 float_T f = 0.0;
12290
12291 rettv->v_type = VAR_FLOAT;
12292 if (get_float_arg(argvars, &f) == OK)
12293 rettv->vval.v_float = sinh(f);
12294 else
12295 rettv->vval.v_float = 0.0;
12296}
12297#endif
12298
12299static int
12300#ifdef __BORLANDC__
12301 _RTLENTRYF
12302#endif
12303 item_compare(const void *s1, const void *s2);
12304static int
12305#ifdef __BORLANDC__
12306 _RTLENTRYF
12307#endif
12308 item_compare2(const void *s1, const void *s2);
12309
12310/* struct used in the array that's given to qsort() */
12311typedef struct
12312{
12313 listitem_T *item;
12314 int idx;
12315} sortItem_T;
12316
12317/* struct storing information about current sort */
12318typedef struct
12319{
12320 int item_compare_ic;
12321 int item_compare_numeric;
12322 int item_compare_numbers;
12323#ifdef FEAT_FLOAT
12324 int item_compare_float;
12325#endif
12326 char_u *item_compare_func;
12327 partial_T *item_compare_partial;
12328 dict_T *item_compare_selfdict;
12329 int item_compare_func_err;
12330 int item_compare_keep_zero;
12331} sortinfo_T;
12332static sortinfo_T *sortinfo = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012333#define ITEM_COMPARE_FAIL 999
12334
12335/*
12336 * Compare functions for f_sort() and f_uniq() below.
12337 */
12338 static int
12339#ifdef __BORLANDC__
12340_RTLENTRYF
12341#endif
12342item_compare(const void *s1, const void *s2)
12343{
12344 sortItem_T *si1, *si2;
12345 typval_T *tv1, *tv2;
12346 char_u *p1, *p2;
12347 char_u *tofree1 = NULL, *tofree2 = NULL;
12348 int res;
12349 char_u numbuf1[NUMBUFLEN];
12350 char_u numbuf2[NUMBUFLEN];
12351
12352 si1 = (sortItem_T *)s1;
12353 si2 = (sortItem_T *)s2;
12354 tv1 = &si1->item->li_tv;
12355 tv2 = &si2->item->li_tv;
12356
12357 if (sortinfo->item_compare_numbers)
12358 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012359 varnumber_T v1 = tv_get_number(tv1);
12360 varnumber_T v2 = tv_get_number(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012361
12362 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
12363 }
12364
12365#ifdef FEAT_FLOAT
12366 if (sortinfo->item_compare_float)
12367 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012368 float_T v1 = tv_get_float(tv1);
12369 float_T v2 = tv_get_float(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012370
12371 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
12372 }
12373#endif
12374
12375 /* tv2string() puts quotes around a string and allocates memory. Don't do
12376 * that for string variables. Use a single quote when comparing with a
12377 * non-string to do what the docs promise. */
12378 if (tv1->v_type == VAR_STRING)
12379 {
12380 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12381 p1 = (char_u *)"'";
12382 else
12383 p1 = tv1->vval.v_string;
12384 }
12385 else
12386 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
12387 if (tv2->v_type == VAR_STRING)
12388 {
12389 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12390 p2 = (char_u *)"'";
12391 else
12392 p2 = tv2->vval.v_string;
12393 }
12394 else
12395 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
12396 if (p1 == NULL)
12397 p1 = (char_u *)"";
12398 if (p2 == NULL)
12399 p2 = (char_u *)"";
12400 if (!sortinfo->item_compare_numeric)
12401 {
12402 if (sortinfo->item_compare_ic)
12403 res = STRICMP(p1, p2);
12404 else
12405 res = STRCMP(p1, p2);
12406 }
12407 else
12408 {
12409 double n1, n2;
12410 n1 = strtod((char *)p1, (char **)&p1);
12411 n2 = strtod((char *)p2, (char **)&p2);
12412 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
12413 }
12414
12415 /* When the result would be zero, compare the item indexes. Makes the
12416 * sort stable. */
12417 if (res == 0 && !sortinfo->item_compare_keep_zero)
12418 res = si1->idx > si2->idx ? 1 : -1;
12419
12420 vim_free(tofree1);
12421 vim_free(tofree2);
12422 return res;
12423}
12424
12425 static int
12426#ifdef __BORLANDC__
12427_RTLENTRYF
12428#endif
12429item_compare2(const void *s1, const void *s2)
12430{
12431 sortItem_T *si1, *si2;
12432 int res;
12433 typval_T rettv;
12434 typval_T argv[3];
12435 int dummy;
12436 char_u *func_name;
12437 partial_T *partial = sortinfo->item_compare_partial;
12438
12439 /* shortcut after failure in previous call; compare all items equal */
12440 if (sortinfo->item_compare_func_err)
12441 return 0;
12442
12443 si1 = (sortItem_T *)s1;
12444 si2 = (sortItem_T *)s2;
12445
12446 if (partial == NULL)
12447 func_name = sortinfo->item_compare_func;
12448 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012449 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012450
12451 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
12452 * in the copy without changing the original list items. */
12453 copy_tv(&si1->item->li_tv, &argv[0]);
12454 copy_tv(&si2->item->li_tv, &argv[1]);
12455
12456 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
12457 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020012458 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012459 partial, sortinfo->item_compare_selfdict);
12460 clear_tv(&argv[0]);
12461 clear_tv(&argv[1]);
12462
12463 if (res == FAIL)
12464 res = ITEM_COMPARE_FAIL;
12465 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012466 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012467 if (sortinfo->item_compare_func_err)
12468 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
12469 clear_tv(&rettv);
12470
12471 /* When the result would be zero, compare the pointers themselves. Makes
12472 * the sort stable. */
12473 if (res == 0 && !sortinfo->item_compare_keep_zero)
12474 res = si1->idx > si2->idx ? 1 : -1;
12475
12476 return res;
12477}
12478
12479/*
12480 * "sort({list})" function
12481 */
12482 static void
12483do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
12484{
12485 list_T *l;
12486 listitem_T *li;
12487 sortItem_T *ptrs;
12488 sortinfo_T *old_sortinfo;
12489 sortinfo_T info;
12490 long len;
12491 long i;
12492
12493 /* Pointer to current info struct used in compare function. Save and
12494 * restore the current one for nested calls. */
12495 old_sortinfo = sortinfo;
12496 sortinfo = &info;
12497
12498 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012499 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012500 else
12501 {
12502 l = argvars[0].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +010012503 if (l == NULL || var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012504 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
12505 TRUE))
12506 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012507 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012508
12509 len = list_len(l);
12510 if (len <= 1)
12511 goto theend; /* short list sorts pretty quickly */
12512
12513 info.item_compare_ic = FALSE;
12514 info.item_compare_numeric = FALSE;
12515 info.item_compare_numbers = FALSE;
12516#ifdef FEAT_FLOAT
12517 info.item_compare_float = FALSE;
12518#endif
12519 info.item_compare_func = NULL;
12520 info.item_compare_partial = NULL;
12521 info.item_compare_selfdict = NULL;
12522 if (argvars[1].v_type != VAR_UNKNOWN)
12523 {
12524 /* optional second argument: {func} */
12525 if (argvars[1].v_type == VAR_FUNC)
12526 info.item_compare_func = argvars[1].vval.v_string;
12527 else if (argvars[1].v_type == VAR_PARTIAL)
12528 info.item_compare_partial = argvars[1].vval.v_partial;
12529 else
12530 {
12531 int error = FALSE;
12532
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012533 i = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012534 if (error)
12535 goto theend; /* type error; errmsg already given */
12536 if (i == 1)
12537 info.item_compare_ic = TRUE;
12538 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012539 info.item_compare_func = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012540 else if (i != 0)
12541 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012542 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012543 goto theend;
12544 }
12545 if (info.item_compare_func != NULL)
12546 {
12547 if (*info.item_compare_func == NUL)
12548 {
12549 /* empty string means default sort */
12550 info.item_compare_func = NULL;
12551 }
12552 else if (STRCMP(info.item_compare_func, "n") == 0)
12553 {
12554 info.item_compare_func = NULL;
12555 info.item_compare_numeric = TRUE;
12556 }
12557 else if (STRCMP(info.item_compare_func, "N") == 0)
12558 {
12559 info.item_compare_func = NULL;
12560 info.item_compare_numbers = TRUE;
12561 }
12562#ifdef FEAT_FLOAT
12563 else if (STRCMP(info.item_compare_func, "f") == 0)
12564 {
12565 info.item_compare_func = NULL;
12566 info.item_compare_float = TRUE;
12567 }
12568#endif
12569 else if (STRCMP(info.item_compare_func, "i") == 0)
12570 {
12571 info.item_compare_func = NULL;
12572 info.item_compare_ic = TRUE;
12573 }
12574 }
12575 }
12576
12577 if (argvars[2].v_type != VAR_UNKNOWN)
12578 {
12579 /* optional third argument: {dict} */
12580 if (argvars[2].v_type != VAR_DICT)
12581 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012582 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012583 goto theend;
12584 }
12585 info.item_compare_selfdict = argvars[2].vval.v_dict;
12586 }
12587 }
12588
12589 /* Make an array with each entry pointing to an item in the List. */
12590 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
12591 if (ptrs == NULL)
12592 goto theend;
12593
12594 i = 0;
12595 if (sort)
12596 {
12597 /* sort(): ptrs will be the list to sort */
12598 for (li = l->lv_first; li != NULL; li = li->li_next)
12599 {
12600 ptrs[i].item = li;
12601 ptrs[i].idx = i;
12602 ++i;
12603 }
12604
12605 info.item_compare_func_err = FALSE;
12606 info.item_compare_keep_zero = FALSE;
12607 /* test the compare function */
12608 if ((info.item_compare_func != NULL
12609 || info.item_compare_partial != NULL)
12610 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
12611 == ITEM_COMPARE_FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012612 emsg(_("E702: Sort compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012613 else
12614 {
12615 /* Sort the array with item pointers. */
12616 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
12617 info.item_compare_func == NULL
12618 && info.item_compare_partial == NULL
12619 ? item_compare : item_compare2);
12620
12621 if (!info.item_compare_func_err)
12622 {
12623 /* Clear the List and append the items in sorted order. */
12624 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
12625 l->lv_len = 0;
12626 for (i = 0; i < len; ++i)
12627 list_append(l, ptrs[i].item);
12628 }
12629 }
12630 }
12631 else
12632 {
12633 int (*item_compare_func_ptr)(const void *, const void *);
12634
12635 /* f_uniq(): ptrs will be a stack of items to remove */
12636 info.item_compare_func_err = FALSE;
12637 info.item_compare_keep_zero = TRUE;
12638 item_compare_func_ptr = info.item_compare_func != NULL
12639 || info.item_compare_partial != NULL
12640 ? item_compare2 : item_compare;
12641
12642 for (li = l->lv_first; li != NULL && li->li_next != NULL;
12643 li = li->li_next)
12644 {
12645 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
12646 == 0)
12647 ptrs[i++].item = li;
12648 if (info.item_compare_func_err)
12649 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012650 emsg(_("E882: Uniq compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012651 break;
12652 }
12653 }
12654
12655 if (!info.item_compare_func_err)
12656 {
12657 while (--i >= 0)
12658 {
12659 li = ptrs[i].item->li_next;
12660 ptrs[i].item->li_next = li->li_next;
12661 if (li->li_next != NULL)
12662 li->li_next->li_prev = ptrs[i].item;
12663 else
12664 l->lv_last = ptrs[i].item;
12665 list_fix_watch(l, li);
12666 listitem_free(li);
12667 l->lv_len--;
12668 }
12669 }
12670 }
12671
12672 vim_free(ptrs);
12673 }
12674theend:
12675 sortinfo = old_sortinfo;
12676}
12677
12678/*
12679 * "sort({list})" function
12680 */
12681 static void
12682f_sort(typval_T *argvars, typval_T *rettv)
12683{
12684 do_sort_uniq(argvars, rettv, TRUE);
12685}
12686
12687/*
12688 * "uniq({list})" function
12689 */
12690 static void
12691f_uniq(typval_T *argvars, typval_T *rettv)
12692{
12693 do_sort_uniq(argvars, rettv, FALSE);
12694}
12695
12696/*
12697 * "soundfold({word})" function
12698 */
12699 static void
12700f_soundfold(typval_T *argvars, typval_T *rettv)
12701{
12702 char_u *s;
12703
12704 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012705 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012706#ifdef FEAT_SPELL
12707 rettv->vval.v_string = eval_soundfold(s);
12708#else
12709 rettv->vval.v_string = vim_strsave(s);
12710#endif
12711}
12712
12713/*
12714 * "spellbadword()" function
12715 */
12716 static void
12717f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
12718{
12719 char_u *word = (char_u *)"";
12720 hlf_T attr = HLF_COUNT;
12721 int len = 0;
12722
12723 if (rettv_list_alloc(rettv) == FAIL)
12724 return;
12725
12726#ifdef FEAT_SPELL
12727 if (argvars[0].v_type == VAR_UNKNOWN)
12728 {
12729 /* Find the start and length of the badly spelled word. */
12730 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
12731 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012732 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012733 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012734 curwin->w_set_curswant = TRUE;
12735 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012736 }
12737 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
12738 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012739 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012740 int capcol = -1;
12741
12742 if (str != NULL)
12743 {
12744 /* Check the argument for spelling. */
12745 while (*str != NUL)
12746 {
12747 len = spell_check(curwin, str, &attr, &capcol, FALSE);
12748 if (attr != HLF_COUNT)
12749 {
12750 word = str;
12751 break;
12752 }
12753 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020012754 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012755 }
12756 }
12757 }
12758#endif
12759
12760 list_append_string(rettv->vval.v_list, word, len);
12761 list_append_string(rettv->vval.v_list, (char_u *)(
12762 attr == HLF_SPB ? "bad" :
12763 attr == HLF_SPR ? "rare" :
12764 attr == HLF_SPL ? "local" :
12765 attr == HLF_SPC ? "caps" :
12766 ""), -1);
12767}
12768
12769/*
12770 * "spellsuggest()" function
12771 */
12772 static void
12773f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
12774{
12775#ifdef FEAT_SPELL
12776 char_u *str;
12777 int typeerr = FALSE;
12778 int maxcount;
12779 garray_T ga;
12780 int i;
12781 listitem_T *li;
12782 int need_capital = FALSE;
12783#endif
12784
12785 if (rettv_list_alloc(rettv) == FAIL)
12786 return;
12787
12788#ifdef FEAT_SPELL
12789 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
12790 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012791 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012792 if (argvars[1].v_type != VAR_UNKNOWN)
12793 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012794 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012795 if (maxcount <= 0)
12796 return;
12797 if (argvars[2].v_type != VAR_UNKNOWN)
12798 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012799 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012800 if (typeerr)
12801 return;
12802 }
12803 }
12804 else
12805 maxcount = 25;
12806
12807 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
12808
12809 for (i = 0; i < ga.ga_len; ++i)
12810 {
12811 str = ((char_u **)ga.ga_data)[i];
12812
12813 li = listitem_alloc();
12814 if (li == NULL)
12815 vim_free(str);
12816 else
12817 {
12818 li->li_tv.v_type = VAR_STRING;
12819 li->li_tv.v_lock = 0;
12820 li->li_tv.vval.v_string = str;
12821 list_append(rettv->vval.v_list, li);
12822 }
12823 }
12824 ga_clear(&ga);
12825 }
12826#endif
12827}
12828
12829 static void
12830f_split(typval_T *argvars, typval_T *rettv)
12831{
12832 char_u *str;
12833 char_u *end;
12834 char_u *pat = NULL;
12835 regmatch_T regmatch;
12836 char_u patbuf[NUMBUFLEN];
12837 char_u *save_cpo;
12838 int match;
12839 colnr_T col = 0;
12840 int keepempty = FALSE;
12841 int typeerr = FALSE;
12842
12843 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12844 save_cpo = p_cpo;
12845 p_cpo = (char_u *)"";
12846
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012847 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012848 if (argvars[1].v_type != VAR_UNKNOWN)
12849 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012850 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012851 if (pat == NULL)
12852 typeerr = TRUE;
12853 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012854 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012855 }
12856 if (pat == NULL || *pat == NUL)
12857 pat = (char_u *)"[\\x01- ]\\+";
12858
12859 if (rettv_list_alloc(rettv) == FAIL)
12860 return;
12861 if (typeerr)
12862 return;
12863
12864 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12865 if (regmatch.regprog != NULL)
12866 {
12867 regmatch.rm_ic = FALSE;
12868 while (*str != NUL || keepempty)
12869 {
12870 if (*str == NUL)
12871 match = FALSE; /* empty item at the end */
12872 else
12873 match = vim_regexec_nl(&regmatch, str, col);
12874 if (match)
12875 end = regmatch.startp[0];
12876 else
12877 end = str + STRLEN(str);
12878 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
12879 && *str != NUL && match && end < regmatch.endp[0]))
12880 {
12881 if (list_append_string(rettv->vval.v_list, str,
12882 (int)(end - str)) == FAIL)
12883 break;
12884 }
12885 if (!match)
12886 break;
Bram Moolenaar13505972019-01-24 15:04:48 +010012887 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012888 if (regmatch.endp[0] > str)
12889 col = 0;
12890 else
Bram Moolenaar13505972019-01-24 15:04:48 +010012891 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012892 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012893 str = regmatch.endp[0];
12894 }
12895
12896 vim_regfree(regmatch.regprog);
12897 }
12898
12899 p_cpo = save_cpo;
12900}
12901
12902#ifdef FEAT_FLOAT
12903/*
12904 * "sqrt()" function
12905 */
12906 static void
12907f_sqrt(typval_T *argvars, typval_T *rettv)
12908{
12909 float_T f = 0.0;
12910
12911 rettv->v_type = VAR_FLOAT;
12912 if (get_float_arg(argvars, &f) == OK)
12913 rettv->vval.v_float = sqrt(f);
12914 else
12915 rettv->vval.v_float = 0.0;
12916}
12917
12918/*
12919 * "str2float()" function
12920 */
12921 static void
12922f_str2float(typval_T *argvars, typval_T *rettv)
12923{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012924 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010012925 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012926
Bram Moolenaar08243d22017-01-10 16:12:29 +010012927 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012928 p = skipwhite(p + 1);
12929 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010012930 if (isneg)
12931 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012932 rettv->v_type = VAR_FLOAT;
12933}
12934#endif
12935
12936/*
Bram Moolenaar9d401282019-04-06 13:18:12 +020012937 * "str2list()" function
12938 */
12939 static void
12940f_str2list(typval_T *argvars, typval_T *rettv)
12941{
12942 char_u *p;
12943 int utf8 = FALSE;
12944
12945 if (rettv_list_alloc(rettv) == FAIL)
12946 return;
12947
12948 if (argvars[1].v_type != VAR_UNKNOWN)
12949 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
12950
12951 p = tv_get_string(&argvars[0]);
12952
12953 if (has_mbyte || utf8)
12954 {
12955 int (*ptr2len)(char_u *);
12956 int (*ptr2char)(char_u *);
12957
12958 if (utf8 || enc_utf8)
12959 {
12960 ptr2len = utf_ptr2len;
12961 ptr2char = utf_ptr2char;
12962 }
12963 else
12964 {
12965 ptr2len = mb_ptr2len;
12966 ptr2char = mb_ptr2char;
12967 }
12968
12969 for ( ; *p != NUL; p += (*ptr2len)(p))
12970 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
12971 }
12972 else
12973 for ( ; *p != NUL; ++p)
12974 list_append_number(rettv->vval.v_list, *p);
12975}
12976
12977/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012978 * "str2nr()" function
12979 */
12980 static void
12981f_str2nr(typval_T *argvars, typval_T *rettv)
12982{
12983 int base = 10;
12984 char_u *p;
12985 varnumber_T n;
12986 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010012987 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012988
12989 if (argvars[1].v_type != VAR_UNKNOWN)
12990 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012991 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012992 if (base != 2 && base != 8 && base != 10 && base != 16)
12993 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012994 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012995 return;
12996 }
12997 }
12998
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012999 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010013000 isneg = (*p == '-');
13001 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013002 p = skipwhite(p + 1);
13003 switch (base)
13004 {
13005 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
13006 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
13007 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
13008 default: what = 0;
13009 }
13010 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010013011 if (isneg)
13012 rettv->vval.v_number = -n;
13013 else
13014 rettv->vval.v_number = n;
13015
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013016}
13017
13018#ifdef HAVE_STRFTIME
13019/*
13020 * "strftime({format}[, {time}])" function
13021 */
13022 static void
13023f_strftime(typval_T *argvars, typval_T *rettv)
13024{
13025 char_u result_buf[256];
13026 struct tm *curtime;
13027 time_t seconds;
13028 char_u *p;
13029
13030 rettv->v_type = VAR_STRING;
13031
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013032 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013033 if (argvars[1].v_type == VAR_UNKNOWN)
13034 seconds = time(NULL);
13035 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013036 seconds = (time_t)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013037 curtime = localtime(&seconds);
13038 /* MSVC returns NULL for an invalid value of seconds. */
13039 if (curtime == NULL)
13040 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
13041 else
13042 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013043 vimconv_T conv;
13044 char_u *enc;
13045
13046 conv.vc_type = CONV_NONE;
13047 enc = enc_locale();
13048 convert_setup(&conv, p_enc, enc);
13049 if (conv.vc_type != CONV_NONE)
13050 p = string_convert(&conv, p, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013051 if (p != NULL)
13052 (void)strftime((char *)result_buf, sizeof(result_buf),
13053 (char *)p, curtime);
13054 else
13055 result_buf[0] = NUL;
13056
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013057 if (conv.vc_type != CONV_NONE)
13058 vim_free(p);
13059 convert_setup(&conv, enc, p_enc);
13060 if (conv.vc_type != CONV_NONE)
13061 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
13062 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013063 rettv->vval.v_string = vim_strsave(result_buf);
13064
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013065 /* Release conversion descriptors */
13066 convert_setup(&conv, NULL, NULL);
13067 vim_free(enc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013068 }
13069}
13070#endif
13071
13072/*
13073 * "strgetchar()" function
13074 */
13075 static void
13076f_strgetchar(typval_T *argvars, typval_T *rettv)
13077{
13078 char_u *str;
13079 int len;
13080 int error = FALSE;
13081 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +010013082 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013083
13084 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013085 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013086 if (str == NULL)
13087 return;
13088 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013089 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013090 if (error)
13091 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013092
Bram Moolenaar13505972019-01-24 15:04:48 +010013093 while (charidx >= 0 && byteidx < len)
13094 {
13095 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013096 {
Bram Moolenaar13505972019-01-24 15:04:48 +010013097 rettv->vval.v_number = mb_ptr2char(str + byteidx);
13098 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013099 }
Bram Moolenaar13505972019-01-24 15:04:48 +010013100 --charidx;
13101 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013102 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013103}
13104
13105/*
13106 * "stridx()" function
13107 */
13108 static void
13109f_stridx(typval_T *argvars, typval_T *rettv)
13110{
13111 char_u buf[NUMBUFLEN];
13112 char_u *needle;
13113 char_u *haystack;
13114 char_u *save_haystack;
13115 char_u *pos;
13116 int start_idx;
13117
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013118 needle = tv_get_string_chk(&argvars[1]);
13119 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013120 rettv->vval.v_number = -1;
13121 if (needle == NULL || haystack == NULL)
13122 return; /* type error; errmsg already given */
13123
13124 if (argvars[2].v_type != VAR_UNKNOWN)
13125 {
13126 int error = FALSE;
13127
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013128 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013129 if (error || start_idx >= (int)STRLEN(haystack))
13130 return;
13131 if (start_idx >= 0)
13132 haystack += start_idx;
13133 }
13134
13135 pos = (char_u *)strstr((char *)haystack, (char *)needle);
13136 if (pos != NULL)
13137 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
13138}
13139
13140/*
13141 * "string()" function
13142 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010013143 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013144f_string(typval_T *argvars, typval_T *rettv)
13145{
13146 char_u *tofree;
13147 char_u numbuf[NUMBUFLEN];
13148
13149 rettv->v_type = VAR_STRING;
13150 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
13151 get_copyID());
13152 /* Make a copy if we have a value but it's not in allocated memory. */
13153 if (rettv->vval.v_string != NULL && tofree == NULL)
13154 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
13155}
13156
13157/*
13158 * "strlen()" function
13159 */
13160 static void
13161f_strlen(typval_T *argvars, typval_T *rettv)
13162{
13163 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013164 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013165}
13166
13167/*
13168 * "strchars()" function
13169 */
13170 static void
13171f_strchars(typval_T *argvars, typval_T *rettv)
13172{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013173 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013174 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013175 varnumber_T len = 0;
13176 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013177
13178 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013179 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013180 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013181 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013182 else
13183 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013184 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
13185 while (*s != NUL)
13186 {
13187 func_mb_ptr2char_adv(&s);
13188 ++len;
13189 }
13190 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013191 }
13192}
13193
13194/*
13195 * "strdisplaywidth()" function
13196 */
13197 static void
13198f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
13199{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013200 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013201 int col = 0;
13202
13203 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013204 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013205
13206 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
13207}
13208
13209/*
13210 * "strwidth()" function
13211 */
13212 static void
13213f_strwidth(typval_T *argvars, typval_T *rettv)
13214{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013215 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013216
Bram Moolenaar13505972019-01-24 15:04:48 +010013217 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013218}
13219
13220/*
13221 * "strcharpart()" function
13222 */
13223 static void
13224f_strcharpart(typval_T *argvars, typval_T *rettv)
13225{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013226 char_u *p;
13227 int nchar;
13228 int nbyte = 0;
13229 int charlen;
13230 int len = 0;
13231 int slen;
13232 int error = FALSE;
13233
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013234 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013235 slen = (int)STRLEN(p);
13236
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013237 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013238 if (!error)
13239 {
13240 if (nchar > 0)
13241 while (nchar > 0 && nbyte < slen)
13242 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020013243 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013244 --nchar;
13245 }
13246 else
13247 nbyte = nchar;
13248 if (argvars[2].v_type != VAR_UNKNOWN)
13249 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013250 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013251 while (charlen > 0 && nbyte + len < slen)
13252 {
13253 int off = nbyte + len;
13254
13255 if (off < 0)
13256 len += 1;
13257 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020013258 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013259 --charlen;
13260 }
13261 }
13262 else
13263 len = slen - nbyte; /* default: all bytes that are available. */
13264 }
13265
13266 /*
13267 * Only return the overlap between the specified part and the actual
13268 * string.
13269 */
13270 if (nbyte < 0)
13271 {
13272 len += nbyte;
13273 nbyte = 0;
13274 }
13275 else if (nbyte > slen)
13276 nbyte = slen;
13277 if (len < 0)
13278 len = 0;
13279 else if (nbyte + len > slen)
13280 len = slen - nbyte;
13281
13282 rettv->v_type = VAR_STRING;
13283 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013284}
13285
13286/*
13287 * "strpart()" function
13288 */
13289 static void
13290f_strpart(typval_T *argvars, typval_T *rettv)
13291{
13292 char_u *p;
13293 int n;
13294 int len;
13295 int slen;
13296 int error = FALSE;
13297
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013298 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013299 slen = (int)STRLEN(p);
13300
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013301 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013302 if (error)
13303 len = 0;
13304 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013305 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013306 else
13307 len = slen - n; /* default len: all bytes that are available. */
13308
13309 /*
13310 * Only return the overlap between the specified part and the actual
13311 * string.
13312 */
13313 if (n < 0)
13314 {
13315 len += n;
13316 n = 0;
13317 }
13318 else if (n > slen)
13319 n = slen;
13320 if (len < 0)
13321 len = 0;
13322 else if (n + len > slen)
13323 len = slen - n;
13324
13325 rettv->v_type = VAR_STRING;
13326 rettv->vval.v_string = vim_strnsave(p + n, len);
13327}
13328
13329/*
13330 * "strridx()" function
13331 */
13332 static void
13333f_strridx(typval_T *argvars, typval_T *rettv)
13334{
13335 char_u buf[NUMBUFLEN];
13336 char_u *needle;
13337 char_u *haystack;
13338 char_u *rest;
13339 char_u *lastmatch = NULL;
13340 int haystack_len, end_idx;
13341
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013342 needle = tv_get_string_chk(&argvars[1]);
13343 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013344
13345 rettv->vval.v_number = -1;
13346 if (needle == NULL || haystack == NULL)
13347 return; /* type error; errmsg already given */
13348
13349 haystack_len = (int)STRLEN(haystack);
13350 if (argvars[2].v_type != VAR_UNKNOWN)
13351 {
13352 /* Third argument: upper limit for index */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013353 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013354 if (end_idx < 0)
13355 return; /* can never find a match */
13356 }
13357 else
13358 end_idx = haystack_len;
13359
13360 if (*needle == NUL)
13361 {
13362 /* Empty string matches past the end. */
13363 lastmatch = haystack + end_idx;
13364 }
13365 else
13366 {
13367 for (rest = haystack; *rest != '\0'; ++rest)
13368 {
13369 rest = (char_u *)strstr((char *)rest, (char *)needle);
13370 if (rest == NULL || rest > haystack + end_idx)
13371 break;
13372 lastmatch = rest;
13373 }
13374 }
13375
13376 if (lastmatch == NULL)
13377 rettv->vval.v_number = -1;
13378 else
13379 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
13380}
13381
13382/*
13383 * "strtrans()" function
13384 */
13385 static void
13386f_strtrans(typval_T *argvars, typval_T *rettv)
13387{
13388 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013389 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013390}
13391
13392/*
13393 * "submatch()" function
13394 */
13395 static void
13396f_submatch(typval_T *argvars, typval_T *rettv)
13397{
13398 int error = FALSE;
13399 int no;
13400 int retList = 0;
13401
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013402 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013403 if (error)
13404 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013405 if (no < 0 || no >= NSUBEXP)
13406 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013407 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +010013408 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013409 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013410 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013411 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013412 if (error)
13413 return;
13414
13415 if (retList == 0)
13416 {
13417 rettv->v_type = VAR_STRING;
13418 rettv->vval.v_string = reg_submatch(no);
13419 }
13420 else
13421 {
13422 rettv->v_type = VAR_LIST;
13423 rettv->vval.v_list = reg_submatch_list(no);
13424 }
13425}
13426
13427/*
13428 * "substitute()" function
13429 */
13430 static void
13431f_substitute(typval_T *argvars, typval_T *rettv)
13432{
13433 char_u patbuf[NUMBUFLEN];
13434 char_u subbuf[NUMBUFLEN];
13435 char_u flagsbuf[NUMBUFLEN];
13436
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013437 char_u *str = tv_get_string_chk(&argvars[0]);
13438 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013439 char_u *sub = NULL;
13440 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013441 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013442
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013443 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
13444 expr = &argvars[2];
13445 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013446 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013447
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013448 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013449 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
13450 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013451 rettv->vval.v_string = NULL;
13452 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013453 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013454}
13455
13456/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013457 * "swapinfo(swap_filename)" function
13458 */
13459 static void
13460f_swapinfo(typval_T *argvars, typval_T *rettv)
13461{
13462 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013463 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013464}
13465
13466/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020013467 * "swapname(expr)" function
13468 */
13469 static void
13470f_swapname(typval_T *argvars, typval_T *rettv)
13471{
13472 buf_T *buf;
13473
13474 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010013475 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +020013476 if (buf == NULL || buf->b_ml.ml_mfp == NULL
13477 || buf->b_ml.ml_mfp->mf_fname == NULL)
13478 rettv->vval.v_string = NULL;
13479 else
13480 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
13481}
13482
13483/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013484 * "synID(lnum, col, trans)" function
13485 */
13486 static void
13487f_synID(typval_T *argvars UNUSED, typval_T *rettv)
13488{
13489 int id = 0;
13490#ifdef FEAT_SYN_HL
13491 linenr_T lnum;
13492 colnr_T col;
13493 int trans;
13494 int transerr = FALSE;
13495
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013496 lnum = tv_get_lnum(argvars); /* -1 on type error */
13497 col = (linenr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
13498 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013499
13500 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13501 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
13502 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
13503#endif
13504
13505 rettv->vval.v_number = id;
13506}
13507
13508/*
13509 * "synIDattr(id, what [, mode])" function
13510 */
13511 static void
13512f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
13513{
13514 char_u *p = NULL;
13515#ifdef FEAT_SYN_HL
13516 int id;
13517 char_u *what;
13518 char_u *mode;
13519 char_u modebuf[NUMBUFLEN];
13520 int modec;
13521
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013522 id = (int)tv_get_number(&argvars[0]);
13523 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013524 if (argvars[2].v_type != VAR_UNKNOWN)
13525 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013526 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013527 modec = TOLOWER_ASC(mode[0]);
13528 if (modec != 't' && modec != 'c' && modec != 'g')
13529 modec = 0; /* replace invalid with current */
13530 }
13531 else
13532 {
13533#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
13534 if (USE_24BIT)
13535 modec = 'g';
13536 else
13537#endif
13538 if (t_colors > 1)
13539 modec = 'c';
13540 else
13541 modec = 't';
13542 }
13543
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013544 switch (TOLOWER_ASC(what[0]))
13545 {
13546 case 'b':
13547 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
13548 p = highlight_color(id, what, modec);
13549 else /* bold */
13550 p = highlight_has_attr(id, HL_BOLD, modec);
13551 break;
13552
13553 case 'f': /* fg[#] or font */
13554 p = highlight_color(id, what, modec);
13555 break;
13556
13557 case 'i':
13558 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
13559 p = highlight_has_attr(id, HL_INVERSE, modec);
13560 else /* italic */
13561 p = highlight_has_attr(id, HL_ITALIC, modec);
13562 break;
13563
13564 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020013565 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013566 break;
13567
13568 case 'r': /* reverse */
13569 p = highlight_has_attr(id, HL_INVERSE, modec);
13570 break;
13571
13572 case 's':
13573 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
13574 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020013575 /* strikeout */
13576 else if (TOLOWER_ASC(what[1]) == 't' &&
13577 TOLOWER_ASC(what[2]) == 'r')
13578 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013579 else /* standout */
13580 p = highlight_has_attr(id, HL_STANDOUT, modec);
13581 break;
13582
13583 case 'u':
13584 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
13585 /* underline */
13586 p = highlight_has_attr(id, HL_UNDERLINE, modec);
13587 else
13588 /* undercurl */
13589 p = highlight_has_attr(id, HL_UNDERCURL, modec);
13590 break;
13591 }
13592
13593 if (p != NULL)
13594 p = vim_strsave(p);
13595#endif
13596 rettv->v_type = VAR_STRING;
13597 rettv->vval.v_string = p;
13598}
13599
13600/*
13601 * "synIDtrans(id)" function
13602 */
13603 static void
13604f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
13605{
13606 int id;
13607
13608#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013609 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013610
13611 if (id > 0)
13612 id = syn_get_final_id(id);
13613 else
13614#endif
13615 id = 0;
13616
13617 rettv->vval.v_number = id;
13618}
13619
13620/*
13621 * "synconcealed(lnum, col)" function
13622 */
13623 static void
13624f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
13625{
13626#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
13627 linenr_T lnum;
13628 colnr_T col;
13629 int syntax_flags = 0;
13630 int cchar;
13631 int matchid = 0;
13632 char_u str[NUMBUFLEN];
13633#endif
13634
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013635 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013636
13637#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013638 lnum = tv_get_lnum(argvars); /* -1 on type error */
13639 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013640
13641 vim_memset(str, NUL, sizeof(str));
13642
13643 if (rettv_list_alloc(rettv) != FAIL)
13644 {
13645 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13646 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13647 && curwin->w_p_cole > 0)
13648 {
13649 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
13650 syntax_flags = get_syntax_info(&matchid);
13651
13652 /* get the conceal character */
13653 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
13654 {
13655 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020013656 if (cchar == NUL && curwin->w_p_cole == 1)
13657 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013658 if (cchar != NUL)
13659 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013660 if (has_mbyte)
13661 (*mb_char2bytes)(cchar, str);
13662 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013663 str[0] = cchar;
13664 }
13665 }
13666 }
13667
13668 list_append_number(rettv->vval.v_list,
13669 (syntax_flags & HL_CONCEAL) != 0);
13670 /* -1 to auto-determine strlen */
13671 list_append_string(rettv->vval.v_list, str, -1);
13672 list_append_number(rettv->vval.v_list, matchid);
13673 }
13674#endif
13675}
13676
13677/*
13678 * "synstack(lnum, col)" function
13679 */
13680 static void
13681f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
13682{
13683#ifdef FEAT_SYN_HL
13684 linenr_T lnum;
13685 colnr_T col;
13686 int i;
13687 int id;
13688#endif
13689
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013690 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013691
13692#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013693 lnum = tv_get_lnum(argvars); /* -1 on type error */
13694 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013695
13696 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13697 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13698 && rettv_list_alloc(rettv) != FAIL)
13699 {
13700 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
13701 for (i = 0; ; ++i)
13702 {
13703 id = syn_get_stack_item(i);
13704 if (id < 0)
13705 break;
13706 if (list_append_number(rettv->vval.v_list, id) == FAIL)
13707 break;
13708 }
13709 }
13710#endif
13711}
13712
13713 static void
13714get_cmd_output_as_rettv(
13715 typval_T *argvars,
13716 typval_T *rettv,
13717 int retlist)
13718{
13719 char_u *res = NULL;
13720 char_u *p;
13721 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013722 int err = FALSE;
13723 FILE *fd;
13724 list_T *list = NULL;
13725 int flags = SHELL_SILENT;
13726
13727 rettv->v_type = VAR_STRING;
13728 rettv->vval.v_string = NULL;
13729 if (check_restricted() || check_secure())
13730 goto errret;
13731
13732 if (argvars[1].v_type != VAR_UNKNOWN)
13733 {
13734 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010013735 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013736 * command.
13737 */
13738 if ((infile = vim_tempname('i', TRUE)) == NULL)
13739 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013740 emsg(_(e_notmp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013741 goto errret;
13742 }
13743
13744 fd = mch_fopen((char *)infile, WRITEBIN);
13745 if (fd == NULL)
13746 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013747 semsg(_(e_notopen), infile);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013748 goto errret;
13749 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010013750 if (argvars[1].v_type == VAR_NUMBER)
13751 {
13752 linenr_T lnum;
13753 buf_T *buf;
13754
13755 buf = buflist_findnr(argvars[1].vval.v_number);
13756 if (buf == NULL)
13757 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013758 semsg(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010013759 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010013760 goto errret;
13761 }
13762
13763 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
13764 {
13765 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
13766 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
13767 {
13768 err = TRUE;
13769 break;
13770 }
13771 if (putc(NL, fd) == EOF)
13772 {
13773 err = TRUE;
13774 break;
13775 }
13776 }
13777 }
13778 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013779 {
13780 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
13781 err = TRUE;
13782 }
13783 else
13784 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010013785 size_t len;
13786 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013787
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013788 p = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013789 if (p == NULL)
13790 {
13791 fclose(fd);
13792 goto errret; /* type error; errmsg already given */
13793 }
13794 len = STRLEN(p);
13795 if (len > 0 && fwrite(p, len, 1, fd) != 1)
13796 err = TRUE;
13797 }
13798 if (fclose(fd) != 0)
13799 err = TRUE;
13800 if (err)
13801 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013802 emsg(_("E677: Error writing temp file"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013803 goto errret;
13804 }
13805 }
13806
13807 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
13808 * echoes typeahead, that messes up the display. */
13809 if (!msg_silent)
13810 flags += SHELL_COOKED;
13811
13812 if (retlist)
13813 {
13814 int len;
13815 listitem_T *li;
13816 char_u *s = NULL;
13817 char_u *start;
13818 char_u *end;
13819 int i;
13820
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013821 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013822 if (res == NULL)
13823 goto errret;
13824
13825 list = list_alloc();
13826 if (list == NULL)
13827 goto errret;
13828
13829 for (i = 0; i < len; ++i)
13830 {
13831 start = res + i;
13832 while (i < len && res[i] != NL)
13833 ++i;
13834 end = res + i;
13835
13836 s = alloc((unsigned)(end - start + 1));
13837 if (s == NULL)
13838 goto errret;
13839
13840 for (p = s; start < end; ++p, ++start)
13841 *p = *start == NUL ? NL : *start;
13842 *p = NUL;
13843
13844 li = listitem_alloc();
13845 if (li == NULL)
13846 {
13847 vim_free(s);
13848 goto errret;
13849 }
13850 li->li_tv.v_type = VAR_STRING;
13851 li->li_tv.v_lock = 0;
13852 li->li_tv.vval.v_string = s;
13853 list_append(list, li);
13854 }
13855
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013856 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013857 list = NULL;
13858 }
13859 else
13860 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013861 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar00590742019-02-15 21:06:09 +010013862#ifdef USE_CRNL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013863 /* translate <CR><NL> into <NL> */
13864 if (res != NULL)
13865 {
13866 char_u *s, *d;
13867
13868 d = res;
13869 for (s = res; *s; ++s)
13870 {
13871 if (s[0] == CAR && s[1] == NL)
13872 ++s;
13873 *d++ = *s;
13874 }
13875 *d = NUL;
13876 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013877#endif
13878 rettv->vval.v_string = res;
13879 res = NULL;
13880 }
13881
13882errret:
13883 if (infile != NULL)
13884 {
13885 mch_remove(infile);
13886 vim_free(infile);
13887 }
13888 if (res != NULL)
13889 vim_free(res);
13890 if (list != NULL)
13891 list_free(list);
13892}
13893
13894/*
13895 * "system()" function
13896 */
13897 static void
13898f_system(typval_T *argvars, typval_T *rettv)
13899{
13900 get_cmd_output_as_rettv(argvars, rettv, FALSE);
13901}
13902
13903/*
13904 * "systemlist()" function
13905 */
13906 static void
13907f_systemlist(typval_T *argvars, typval_T *rettv)
13908{
13909 get_cmd_output_as_rettv(argvars, rettv, TRUE);
13910}
13911
13912/*
13913 * "tabpagebuflist()" function
13914 */
13915 static void
13916f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13917{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013918 tabpage_T *tp;
13919 win_T *wp = NULL;
13920
13921 if (argvars[0].v_type == VAR_UNKNOWN)
13922 wp = firstwin;
13923 else
13924 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013925 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013926 if (tp != NULL)
13927 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
13928 }
13929 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
13930 {
13931 for (; wp != NULL; wp = wp->w_next)
13932 if (list_append_number(rettv->vval.v_list,
13933 wp->w_buffer->b_fnum) == FAIL)
13934 break;
13935 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013936}
13937
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013938/*
13939 * "tabpagenr()" function
13940 */
13941 static void
13942f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
13943{
13944 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013945 char_u *arg;
13946
13947 if (argvars[0].v_type != VAR_UNKNOWN)
13948 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013949 arg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013950 nr = 0;
13951 if (arg != NULL)
13952 {
13953 if (STRCMP(arg, "$") == 0)
13954 nr = tabpage_index(NULL) - 1;
13955 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013956 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013957 }
13958 }
13959 else
13960 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013961 rettv->vval.v_number = nr;
13962}
13963
13964
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013965/*
13966 * Common code for tabpagewinnr() and winnr().
13967 */
13968 static int
13969get_winnr(tabpage_T *tp, typval_T *argvar)
13970{
13971 win_T *twin;
13972 int nr = 1;
13973 win_T *wp;
13974 char_u *arg;
13975
13976 twin = (tp == curtab) ? curwin : tp->tp_curwin;
13977 if (argvar->v_type != VAR_UNKNOWN)
13978 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020013979 int invalid_arg = FALSE;
13980
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013981 arg = tv_get_string_chk(argvar);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013982 if (arg == NULL)
13983 nr = 0; /* type error; errmsg already given */
13984 else if (STRCMP(arg, "$") == 0)
13985 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
13986 else if (STRCMP(arg, "#") == 0)
13987 {
13988 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
13989 if (twin == NULL)
13990 nr = 0;
13991 }
13992 else
13993 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020013994 long count;
13995 char_u *endp;
13996
13997 // Extract the window count (if specified). e.g. winnr('3j')
13998 count = strtol((char *)arg, (char **)&endp, 10);
13999 if (count <= 0)
14000 count = 1; // if count is not specified, default to 1
14001 if (endp != NULL && *endp != '\0')
14002 {
14003 if (STRCMP(endp, "j") == 0)
14004 twin = win_vert_neighbor(tp, twin, FALSE, count);
14005 else if (STRCMP(endp, "k") == 0)
14006 twin = win_vert_neighbor(tp, twin, TRUE, count);
14007 else if (STRCMP(endp, "h") == 0)
14008 twin = win_horz_neighbor(tp, twin, TRUE, count);
14009 else if (STRCMP(endp, "l") == 0)
14010 twin = win_horz_neighbor(tp, twin, FALSE, count);
14011 else
14012 invalid_arg = TRUE;
14013 }
14014 else
14015 invalid_arg = TRUE;
14016 }
14017
14018 if (invalid_arg)
14019 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014020 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014021 nr = 0;
14022 }
14023 }
14024
14025 if (nr > 0)
14026 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
14027 wp != twin; wp = wp->w_next)
14028 {
14029 if (wp == NULL)
14030 {
14031 /* didn't find it in this tabpage */
14032 nr = 0;
14033 break;
14034 }
14035 ++nr;
14036 }
14037 return nr;
14038}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014039
14040/*
14041 * "tabpagewinnr()" function
14042 */
14043 static void
14044f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
14045{
14046 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014047 tabpage_T *tp;
14048
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014049 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014050 if (tp == NULL)
14051 nr = 0;
14052 else
14053 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014054 rettv->vval.v_number = nr;
14055}
14056
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014057/*
14058 * "tagfiles()" function
14059 */
14060 static void
14061f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
14062{
14063 char_u *fname;
14064 tagname_T tn;
14065 int first;
14066
14067 if (rettv_list_alloc(rettv) == FAIL)
14068 return;
14069 fname = alloc(MAXPATHL);
14070 if (fname == NULL)
14071 return;
14072
14073 for (first = TRUE; ; first = FALSE)
14074 if (get_tagfname(&tn, first, fname) == FAIL
14075 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
14076 break;
14077 tagname_free(&tn);
14078 vim_free(fname);
14079}
14080
14081/*
14082 * "taglist()" function
14083 */
14084 static void
14085f_taglist(typval_T *argvars, typval_T *rettv)
14086{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014087 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014088 char_u *tag_pattern;
14089
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014090 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014091
14092 rettv->vval.v_number = FALSE;
14093 if (*tag_pattern == NUL)
14094 return;
14095
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014096 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014097 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014098 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014099 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014100}
14101
14102/*
14103 * "tempname()" function
14104 */
14105 static void
14106f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
14107{
14108 static int x = 'A';
14109
14110 rettv->v_type = VAR_STRING;
14111 rettv->vval.v_string = vim_tempname(x, FALSE);
14112
14113 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
14114 * names. Skip 'I' and 'O', they are used for shell redirection. */
14115 do
14116 {
14117 if (x == 'Z')
14118 x = '0';
14119 else if (x == '9')
14120 x = 'A';
14121 else
14122 {
14123#ifdef EBCDIC
14124 if (x == 'I')
14125 x = 'J';
14126 else if (x == 'R')
14127 x = 'S';
14128 else
14129#endif
14130 ++x;
14131 }
14132 } while (x == 'I' || x == 'O');
14133}
14134
14135#ifdef FEAT_FLOAT
14136/*
14137 * "tan()" function
14138 */
14139 static void
14140f_tan(typval_T *argvars, typval_T *rettv)
14141{
14142 float_T f = 0.0;
14143
14144 rettv->v_type = VAR_FLOAT;
14145 if (get_float_arg(argvars, &f) == OK)
14146 rettv->vval.v_float = tan(f);
14147 else
14148 rettv->vval.v_float = 0.0;
14149}
14150
14151/*
14152 * "tanh()" function
14153 */
14154 static void
14155f_tanh(typval_T *argvars, typval_T *rettv)
14156{
14157 float_T f = 0.0;
14158
14159 rettv->v_type = VAR_FLOAT;
14160 if (get_float_arg(argvars, &f) == OK)
14161 rettv->vval.v_float = tanh(f);
14162 else
14163 rettv->vval.v_float = 0.0;
14164}
14165#endif
14166
14167/*
14168 * "test_alloc_fail(id, countdown, repeat)" function
14169 */
14170 static void
14171f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
14172{
14173 if (argvars[0].v_type != VAR_NUMBER
14174 || argvars[0].vval.v_number <= 0
14175 || argvars[1].v_type != VAR_NUMBER
14176 || argvars[1].vval.v_number < 0
14177 || argvars[2].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014178 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014179 else
14180 {
14181 alloc_fail_id = argvars[0].vval.v_number;
14182 if (alloc_fail_id >= aid_last)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014183 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014184 alloc_fail_countdown = argvars[1].vval.v_number;
14185 alloc_fail_repeat = argvars[2].vval.v_number;
14186 did_outofmem_msg = FALSE;
14187 }
14188}
14189
14190/*
14191 * "test_autochdir()"
14192 */
14193 static void
14194f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14195{
14196#if defined(FEAT_AUTOCHDIR)
14197 test_autochdir = TRUE;
14198#endif
14199}
14200
14201/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020014202 * "test_feedinput()"
14203 */
14204 static void
14205f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
14206{
14207#ifdef USE_INPUT_BUF
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014208 char_u *val = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5e80de32017-09-03 15:48:12 +020014209
14210 if (val != NULL)
14211 {
14212 trash_input_buf();
14213 add_to_input_buf_csi(val, (int)STRLEN(val));
14214 }
14215#endif
14216}
14217
14218/*
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014219 * "test_option_not_set({name})" function
14220 */
14221 static void
14222f_test_option_not_set(typval_T *argvars, typval_T *rettv UNUSED)
14223{
14224 char_u *name = (char_u *)"";
14225
14226 if (argvars[0].v_type != VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014227 emsg(_(e_invarg));
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014228 else
14229 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014230 name = tv_get_string(&argvars[0]);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014231 if (reset_option_was_set(name) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014232 semsg(_(e_invarg2), name);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014233 }
14234}
14235
14236/*
14237 * "test_override({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014238 */
14239 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014240f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014241{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014242 char_u *name = (char_u *)"";
14243 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014244 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014245
14246 if (argvars[0].v_type != VAR_STRING
14247 || (argvars[1].v_type) != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014248 emsg(_(e_invarg));
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014249 else
14250 {
Bram Moolenaare38197d2018-12-24 23:35:13 +010014251 name = tv_get_string(&argvars[0]);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014252 val = (int)tv_get_number(&argvars[1]);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014253
14254 if (STRCMP(name, (char_u *)"redraw") == 0)
14255 disable_redraw_for_testing = val;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020014256 else if (STRCMP(name, (char_u *)"redraw_flag") == 0)
14257 ignore_redraw_flag_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014258 else if (STRCMP(name, (char_u *)"char_avail") == 0)
14259 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014260 else if (STRCMP(name, (char_u *)"starting") == 0)
14261 {
14262 if (val)
14263 {
14264 if (save_starting < 0)
14265 save_starting = starting;
14266 starting = 0;
14267 }
14268 else
14269 {
14270 starting = save_starting;
14271 save_starting = -1;
14272 }
14273 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +020014274 else if (STRCMP(name, (char_u *)"nfa_fail") == 0)
14275 nfa_fail_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014276 else if (STRCMP(name, (char_u *)"ALL") == 0)
14277 {
14278 disable_char_avail_for_testing = FALSE;
14279 disable_redraw_for_testing = FALSE;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020014280 ignore_redraw_flag_for_testing = FALSE;
Bram Moolenaarbcf94422018-06-23 14:21:42 +020014281 nfa_fail_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014282 if (save_starting >= 0)
14283 {
14284 starting = save_starting;
14285 save_starting = -1;
14286 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014287 }
14288 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014289 semsg(_(e_invarg2), name);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014290 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014291}
14292
14293/*
Bram Moolenaarc3e92c12019-03-23 14:23:07 +010014294 * "test_refcount({expr})" function
14295 */
14296 static void
14297f_test_refcount(typval_T *argvars, typval_T *rettv)
14298{
14299 int retval = -1;
14300
14301 switch (argvars[0].v_type)
14302 {
14303 case VAR_UNKNOWN:
14304 case VAR_NUMBER:
14305 case VAR_FLOAT:
14306 case VAR_SPECIAL:
14307 case VAR_STRING:
14308 break;
14309 case VAR_JOB:
14310#ifdef FEAT_JOB_CHANNEL
14311 if (argvars[0].vval.v_job != NULL)
14312 retval = argvars[0].vval.v_job->jv_refcount - 1;
14313#endif
14314 break;
14315 case VAR_CHANNEL:
14316#ifdef FEAT_JOB_CHANNEL
14317 if (argvars[0].vval.v_channel != NULL)
14318 retval = argvars[0].vval.v_channel->ch_refcount - 1;
14319#endif
14320 break;
14321 case VAR_FUNC:
14322 if (argvars[0].vval.v_string != NULL)
14323 {
14324 ufunc_T *fp;
14325
14326 fp = find_func(argvars[0].vval.v_string);
14327 if (fp != NULL)
14328 retval = fp->uf_refcount;
14329 }
14330 break;
14331 case VAR_PARTIAL:
14332 if (argvars[0].vval.v_partial != NULL)
14333 retval = argvars[0].vval.v_partial->pt_refcount - 1;
14334 break;
14335 case VAR_BLOB:
14336 if (argvars[0].vval.v_blob != NULL)
14337 retval = argvars[0].vval.v_blob->bv_refcount - 1;
14338 break;
14339 case VAR_LIST:
14340 if (argvars[0].vval.v_list != NULL)
14341 retval = argvars[0].vval.v_list->lv_refcount - 1;
14342 break;
14343 case VAR_DICT:
14344 if (argvars[0].vval.v_dict != NULL)
14345 retval = argvars[0].vval.v_dict->dv_refcount - 1;
14346 break;
14347 }
14348
14349 rettv->v_type = VAR_NUMBER;
14350 rettv->vval.v_number = retval;
14351
14352}
14353
14354/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014355 * "test_garbagecollect_now()" function
14356 */
14357 static void
14358f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14359{
14360 /* This is dangerous, any Lists and Dicts used internally may be freed
14361 * while still in use. */
14362 garbage_collect(TRUE);
14363}
14364
Bram Moolenaare0c31f62017-03-01 15:07:05 +010014365/*
14366 * "test_ignore_error()" function
14367 */
14368 static void
14369f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
14370{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014371 ignore_error_for_testing(tv_get_string(&argvars[0]));
Bram Moolenaare0c31f62017-03-01 15:07:05 +010014372}
14373
Bram Moolenaarc0f5a782019-01-13 15:16:13 +010014374 static void
14375f_test_null_blob(typval_T *argvars UNUSED, typval_T *rettv)
14376{
14377 rettv->v_type = VAR_BLOB;
14378 rettv->vval.v_blob = NULL;
14379}
14380
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014381#ifdef FEAT_JOB_CHANNEL
14382 static void
14383f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
14384{
14385 rettv->v_type = VAR_CHANNEL;
14386 rettv->vval.v_channel = NULL;
14387}
14388#endif
14389
14390 static void
14391f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
14392{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014393 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014394}
14395
14396#ifdef FEAT_JOB_CHANNEL
14397 static void
14398f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
14399{
14400 rettv->v_type = VAR_JOB;
14401 rettv->vval.v_job = NULL;
14402}
14403#endif
14404
14405 static void
14406f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
14407{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014408 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014409}
14410
14411 static void
14412f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
14413{
14414 rettv->v_type = VAR_PARTIAL;
14415 rettv->vval.v_partial = NULL;
14416}
14417
14418 static void
14419f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
14420{
14421 rettv->v_type = VAR_STRING;
14422 rettv->vval.v_string = NULL;
14423}
14424
Bram Moolenaarab186732018-09-14 21:27:06 +020014425#ifdef FEAT_GUI
14426 static void
14427f_test_scrollbar(typval_T *argvars, typval_T *rettv UNUSED)
14428{
14429 char_u *which;
14430 long value;
14431 int dragging;
14432 scrollbar_T *sb = NULL;
14433
14434 if (argvars[0].v_type != VAR_STRING
14435 || (argvars[1].v_type) != VAR_NUMBER
14436 || (argvars[2].v_type) != VAR_NUMBER)
14437 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014438 emsg(_(e_invarg));
Bram Moolenaarab186732018-09-14 21:27:06 +020014439 return;
14440 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014441 which = tv_get_string(&argvars[0]);
14442 value = tv_get_number(&argvars[1]);
14443 dragging = tv_get_number(&argvars[2]);
Bram Moolenaarab186732018-09-14 21:27:06 +020014444
14445 if (STRCMP(which, "left") == 0)
14446 sb = &curwin->w_scrollbars[SBAR_LEFT];
14447 else if (STRCMP(which, "right") == 0)
14448 sb = &curwin->w_scrollbars[SBAR_RIGHT];
14449 else if (STRCMP(which, "hor") == 0)
14450 sb = &gui.bottom_sbar;
14451 if (sb == NULL)
14452 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014453 semsg(_(e_invarg2), which);
Bram Moolenaarab186732018-09-14 21:27:06 +020014454 return;
14455 }
14456 gui_drag_scrollbar(sb, value, dragging);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020014457# ifndef USE_ON_FLY_SCROLL
14458 // need to loop through normal_cmd() to handle the scroll events
14459 exec_normal(FALSE, TRUE, FALSE);
14460# endif
Bram Moolenaarab186732018-09-14 21:27:06 +020014461}
14462#endif
14463
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014464 static void
14465f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
14466{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014467 time_for_testing = (time_t)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014468}
14469
14470#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
14471/*
14472 * Get a callback from "arg". It can be a Funcref or a function name.
14473 * When "arg" is zero return an empty string.
14474 * Return NULL for an invalid argument.
14475 */
14476 char_u *
14477get_callback(typval_T *arg, partial_T **pp)
14478{
14479 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
14480 {
14481 *pp = arg->vval.v_partial;
14482 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020014483 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014484 }
14485 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020014486 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014487 {
14488 func_ref(arg->vval.v_string);
14489 return arg->vval.v_string;
14490 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014491 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
14492 return (char_u *)"";
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014493 emsg(_("E921: Invalid callback argument"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014494 return NULL;
14495}
14496
14497/*
Bram Moolenaard5359b22018-04-05 22:44:39 +020014498 * Unref/free "callback" and "partial" returned by get_callback().
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014499 */
14500 void
14501free_callback(char_u *callback, partial_T *partial)
14502{
14503 if (partial != NULL)
14504 partial_unref(partial);
14505 else if (callback != NULL)
14506 {
14507 func_unref(callback);
14508 vim_free(callback);
14509 }
14510}
14511#endif
14512
14513#ifdef FEAT_TIMERS
14514/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014515 * "timer_info([timer])" function
14516 */
14517 static void
14518f_timer_info(typval_T *argvars, typval_T *rettv)
14519{
14520 timer_T *timer = NULL;
14521
14522 if (rettv_list_alloc(rettv) != OK)
14523 return;
14524 if (argvars[0].v_type != VAR_UNKNOWN)
14525 {
14526 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014527 emsg(_(e_number_exp));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014528 else
14529 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014530 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014531 if (timer != NULL)
14532 add_timer_info(rettv, timer);
14533 }
14534 }
14535 else
14536 add_timer_info_all(rettv);
14537}
14538
14539/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014540 * "timer_pause(timer, paused)" function
14541 */
14542 static void
14543f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
14544{
14545 timer_T *timer = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014546 int paused = (int)tv_get_number(&argvars[1]);
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014547
14548 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014549 emsg(_(e_number_exp));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014550 else
14551 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014552 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014553 if (timer != NULL)
14554 timer->tr_paused = paused;
14555 }
14556}
14557
14558/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014559 * "timer_start(time, callback [, options])" function
14560 */
14561 static void
14562f_timer_start(typval_T *argvars, typval_T *rettv)
14563{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014564 long msec = (long)tv_get_number(&argvars[0]);
Bram Moolenaar75537a92016-09-05 22:45:28 +020014565 timer_T *timer;
14566 int repeat = 0;
14567 char_u *callback;
14568 dict_T *dict;
14569 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014570
Bram Moolenaar75537a92016-09-05 22:45:28 +020014571 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014572 if (check_secure())
14573 return;
14574 if (argvars[2].v_type != VAR_UNKNOWN)
14575 {
14576 if (argvars[2].v_type != VAR_DICT
14577 || (dict = argvars[2].vval.v_dict) == NULL)
14578 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014579 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014580 return;
14581 }
14582 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014583 repeat = dict_get_number(dict, (char_u *)"repeat");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014584 }
14585
Bram Moolenaar75537a92016-09-05 22:45:28 +020014586 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014587 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020014588 return;
14589
14590 timer = create_timer(msec, repeat);
14591 if (timer == NULL)
14592 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014593 else
14594 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020014595 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020014596 timer->tr_callback = vim_strsave(callback);
14597 else
14598 /* pointer into the partial */
14599 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020014600 timer->tr_partial = partial;
14601 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014602 }
14603}
14604
14605/*
14606 * "timer_stop(timer)" function
14607 */
14608 static void
14609f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
14610{
14611 timer_T *timer;
14612
14613 if (argvars[0].v_type != VAR_NUMBER)
14614 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014615 emsg(_(e_number_exp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014616 return;
14617 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014618 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014619 if (timer != NULL)
14620 stop_timer(timer);
14621}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014622
14623/*
14624 * "timer_stopall()" function
14625 */
14626 static void
14627f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14628{
14629 stop_all_timers();
14630}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014631#endif
14632
14633/*
14634 * "tolower(string)" function
14635 */
14636 static void
14637f_tolower(typval_T *argvars, typval_T *rettv)
14638{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014639 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014640 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014641}
14642
14643/*
14644 * "toupper(string)" function
14645 */
14646 static void
14647f_toupper(typval_T *argvars, typval_T *rettv)
14648{
14649 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014650 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014651}
14652
14653/*
14654 * "tr(string, fromstr, tostr)" function
14655 */
14656 static void
14657f_tr(typval_T *argvars, typval_T *rettv)
14658{
14659 char_u *in_str;
14660 char_u *fromstr;
14661 char_u *tostr;
14662 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014663 int inlen;
14664 int fromlen;
14665 int tolen;
14666 int idx;
14667 char_u *cpstr;
14668 int cplen;
14669 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014670 char_u buf[NUMBUFLEN];
14671 char_u buf2[NUMBUFLEN];
14672 garray_T ga;
14673
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014674 in_str = tv_get_string(&argvars[0]);
14675 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
14676 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014677
14678 /* Default return value: empty string. */
14679 rettv->v_type = VAR_STRING;
14680 rettv->vval.v_string = NULL;
14681 if (fromstr == NULL || tostr == NULL)
14682 return; /* type error; errmsg already given */
14683 ga_init2(&ga, (int)sizeof(char), 80);
14684
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014685 if (!has_mbyte)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014686 /* not multi-byte: fromstr and tostr must be the same length */
14687 if (STRLEN(fromstr) != STRLEN(tostr))
14688 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014689error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014690 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014691 ga_clear(&ga);
14692 return;
14693 }
14694
14695 /* fromstr and tostr have to contain the same number of chars */
14696 while (*in_str != NUL)
14697 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014698 if (has_mbyte)
14699 {
14700 inlen = (*mb_ptr2len)(in_str);
14701 cpstr = in_str;
14702 cplen = inlen;
14703 idx = 0;
14704 for (p = fromstr; *p != NUL; p += fromlen)
14705 {
14706 fromlen = (*mb_ptr2len)(p);
14707 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
14708 {
14709 for (p = tostr; *p != NUL; p += tolen)
14710 {
14711 tolen = (*mb_ptr2len)(p);
14712 if (idx-- == 0)
14713 {
14714 cplen = tolen;
14715 cpstr = p;
14716 break;
14717 }
14718 }
14719 if (*p == NUL) /* tostr is shorter than fromstr */
14720 goto error;
14721 break;
14722 }
14723 ++idx;
14724 }
14725
14726 if (first && cpstr == in_str)
14727 {
14728 /* Check that fromstr and tostr have the same number of
14729 * (multi-byte) characters. Done only once when a character
14730 * of in_str doesn't appear in fromstr. */
14731 first = FALSE;
14732 for (p = tostr; *p != NUL; p += tolen)
14733 {
14734 tolen = (*mb_ptr2len)(p);
14735 --idx;
14736 }
14737 if (idx != 0)
14738 goto error;
14739 }
14740
14741 (void)ga_grow(&ga, cplen);
14742 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
14743 ga.ga_len += cplen;
14744
14745 in_str += inlen;
14746 }
14747 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014748 {
14749 /* When not using multi-byte chars we can do it faster. */
14750 p = vim_strchr(fromstr, *in_str);
14751 if (p != NULL)
14752 ga_append(&ga, tostr[p - fromstr]);
14753 else
14754 ga_append(&ga, *in_str);
14755 ++in_str;
14756 }
14757 }
14758
14759 /* add a terminating NUL */
14760 (void)ga_grow(&ga, 1);
14761 ga_append(&ga, NUL);
14762
14763 rettv->vval.v_string = ga.ga_data;
14764}
14765
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014766/*
14767 * "trim({expr})" function
14768 */
14769 static void
14770f_trim(typval_T *argvars, typval_T *rettv)
14771{
14772 char_u buf1[NUMBUFLEN];
14773 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014774 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014775 char_u *mask = NULL;
14776 char_u *tail;
14777 char_u *prev;
14778 char_u *p;
14779 int c1;
14780
14781 rettv->v_type = VAR_STRING;
14782 if (head == NULL)
14783 {
14784 rettv->vval.v_string = NULL;
14785 return;
14786 }
14787
14788 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014789 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014790
14791 while (*head != NUL)
14792 {
14793 c1 = PTR2CHAR(head);
14794 if (mask == NULL)
14795 {
14796 if (c1 > ' ' && c1 != 0xa0)
14797 break;
14798 }
14799 else
14800 {
14801 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14802 if (c1 == PTR2CHAR(p))
14803 break;
14804 if (*p == NUL)
14805 break;
14806 }
14807 MB_PTR_ADV(head);
14808 }
14809
14810 for (tail = head + STRLEN(head); tail > head; tail = prev)
14811 {
14812 prev = tail;
14813 MB_PTR_BACK(head, prev);
14814 c1 = PTR2CHAR(prev);
14815 if (mask == NULL)
14816 {
14817 if (c1 > ' ' && c1 != 0xa0)
14818 break;
14819 }
14820 else
14821 {
14822 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14823 if (c1 == PTR2CHAR(p))
14824 break;
14825 if (*p == NUL)
14826 break;
14827 }
14828 }
14829 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
14830}
14831
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014832#ifdef FEAT_FLOAT
14833/*
14834 * "trunc({float})" function
14835 */
14836 static void
14837f_trunc(typval_T *argvars, typval_T *rettv)
14838{
14839 float_T f = 0.0;
14840
14841 rettv->v_type = VAR_FLOAT;
14842 if (get_float_arg(argvars, &f) == OK)
14843 /* trunc() is not in C90, use floor() or ceil() instead. */
14844 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
14845 else
14846 rettv->vval.v_float = 0.0;
14847}
14848#endif
14849
14850/*
14851 * "type(expr)" function
14852 */
14853 static void
14854f_type(typval_T *argvars, typval_T *rettv)
14855{
14856 int n = -1;
14857
14858 switch (argvars[0].v_type)
14859 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020014860 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
14861 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014862 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020014863 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
14864 case VAR_LIST: n = VAR_TYPE_LIST; break;
14865 case VAR_DICT: n = VAR_TYPE_DICT; break;
14866 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014867 case VAR_SPECIAL:
14868 if (argvars[0].vval.v_number == VVAL_FALSE
14869 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020014870 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014871 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020014872 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014873 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020014874 case VAR_JOB: n = VAR_TYPE_JOB; break;
14875 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014876 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014877 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010014878 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014879 n = -1;
14880 break;
14881 }
14882 rettv->vval.v_number = n;
14883}
14884
14885/*
14886 * "undofile(name)" function
14887 */
14888 static void
14889f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
14890{
14891 rettv->v_type = VAR_STRING;
14892#ifdef FEAT_PERSISTENT_UNDO
14893 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014894 char_u *fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014895
14896 if (*fname == NUL)
14897 {
14898 /* If there is no file name there will be no undo file. */
14899 rettv->vval.v_string = NULL;
14900 }
14901 else
14902 {
14903 char_u *ffname = FullName_save(fname, FALSE);
14904
14905 if (ffname != NULL)
14906 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
14907 vim_free(ffname);
14908 }
14909 }
14910#else
14911 rettv->vval.v_string = NULL;
14912#endif
14913}
14914
14915/*
14916 * "undotree()" function
14917 */
14918 static void
14919f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
14920{
14921 if (rettv_dict_alloc(rettv) == OK)
14922 {
14923 dict_T *dict = rettv->vval.v_dict;
14924 list_T *list;
14925
Bram Moolenaare0be1672018-07-08 16:50:37 +020014926 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
14927 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
14928 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
14929 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
14930 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
14931 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014932
14933 list = list_alloc();
14934 if (list != NULL)
14935 {
14936 u_eval_tree(curbuf->b_u_oldhead, list);
14937 dict_add_list(dict, "entries", list);
14938 }
14939 }
14940}
14941
14942/*
14943 * "values(dict)" function
14944 */
14945 static void
14946f_values(typval_T *argvars, typval_T *rettv)
14947{
14948 dict_list(argvars, rettv, 1);
14949}
14950
14951/*
14952 * "virtcol(string)" function
14953 */
14954 static void
14955f_virtcol(typval_T *argvars, typval_T *rettv)
14956{
14957 colnr_T vcol = 0;
14958 pos_T *fp;
14959 int fnum = curbuf->b_fnum;
14960
14961 fp = var2fpos(&argvars[0], FALSE, &fnum);
14962 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
14963 && fnum == curbuf->b_fnum)
14964 {
14965 getvvcol(curwin, fp, NULL, NULL, &vcol);
14966 ++vcol;
14967 }
14968
14969 rettv->vval.v_number = vcol;
14970}
14971
14972/*
14973 * "visualmode()" function
14974 */
14975 static void
14976f_visualmode(typval_T *argvars, typval_T *rettv)
14977{
14978 char_u str[2];
14979
14980 rettv->v_type = VAR_STRING;
14981 str[0] = curbuf->b_visual_mode_eval;
14982 str[1] = NUL;
14983 rettv->vval.v_string = vim_strsave(str);
14984
14985 /* A non-zero number or non-empty string argument: reset mode. */
14986 if (non_zero_arg(&argvars[0]))
14987 curbuf->b_visual_mode_eval = NUL;
14988}
14989
14990/*
14991 * "wildmenumode()" function
14992 */
14993 static void
14994f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14995{
14996#ifdef FEAT_WILDMENU
14997 if (wild_menu_showing)
14998 rettv->vval.v_number = 1;
14999#endif
15000}
15001
15002/*
15003 * "winbufnr(nr)" function
15004 */
15005 static void
15006f_winbufnr(typval_T *argvars, typval_T *rettv)
15007{
15008 win_T *wp;
15009
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015010 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015011 if (wp == NULL)
15012 rettv->vval.v_number = -1;
15013 else
15014 rettv->vval.v_number = wp->w_buffer->b_fnum;
15015}
15016
15017/*
15018 * "wincol()" function
15019 */
15020 static void
15021f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
15022{
15023 validate_cursor();
15024 rettv->vval.v_number = curwin->w_wcol + 1;
15025}
15026
15027/*
15028 * "winheight(nr)" function
15029 */
15030 static void
15031f_winheight(typval_T *argvars, typval_T *rettv)
15032{
15033 win_T *wp;
15034
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015035 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015036 if (wp == NULL)
15037 rettv->vval.v_number = -1;
15038 else
15039 rettv->vval.v_number = wp->w_height;
15040}
15041
15042/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020015043 * "winlayout()" function
15044 */
15045 static void
15046f_winlayout(typval_T *argvars, typval_T *rettv)
15047{
15048 tabpage_T *tp;
15049
15050 if (rettv_list_alloc(rettv) != OK)
15051 return;
15052
15053 if (argvars[0].v_type == VAR_UNKNOWN)
15054 tp = curtab;
15055 else
15056 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015057 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020015058 if (tp == NULL)
15059 return;
15060 }
15061
15062 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
15063}
15064
15065/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015066 * "winline()" function
15067 */
15068 static void
15069f_winline(typval_T *argvars UNUSED, typval_T *rettv)
15070{
15071 validate_cursor();
15072 rettv->vval.v_number = curwin->w_wrow + 1;
15073}
15074
15075/*
15076 * "winnr()" function
15077 */
15078 static void
15079f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
15080{
15081 int nr = 1;
15082
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015083 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015084 rettv->vval.v_number = nr;
15085}
15086
15087/*
15088 * "winrestcmd()" function
15089 */
15090 static void
15091f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
15092{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015093 win_T *wp;
15094 int winnr = 1;
15095 garray_T ga;
15096 char_u buf[50];
15097
15098 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020015099 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015100 {
15101 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
15102 ga_concat(&ga, buf);
15103 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
15104 ga_concat(&ga, buf);
15105 ++winnr;
15106 }
15107 ga_append(&ga, NUL);
15108
15109 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015110 rettv->v_type = VAR_STRING;
15111}
15112
15113/*
15114 * "winrestview()" function
15115 */
15116 static void
15117f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
15118{
15119 dict_T *dict;
15120
15121 if (argvars[0].v_type != VAR_DICT
15122 || (dict = argvars[0].vval.v_dict) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015123 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015124 else
15125 {
15126 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015127 curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015128 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015129 curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015130 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015131 curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015132 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
15133 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010015134 curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015135 curwin->w_set_curswant = FALSE;
15136 }
15137
15138 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015139 set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015140#ifdef FEAT_DIFF
15141 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015142 curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015143#endif
15144 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015145 curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015146 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015147 curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015148
15149 check_cursor();
15150 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020015151 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015152 changed_window_setting();
15153
15154 if (curwin->w_topline <= 0)
15155 curwin->w_topline = 1;
15156 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
15157 curwin->w_topline = curbuf->b_ml.ml_line_count;
15158#ifdef FEAT_DIFF
15159 check_topfill(curwin, TRUE);
15160#endif
15161 }
15162}
15163
15164/*
15165 * "winsaveview()" function
15166 */
15167 static void
15168f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
15169{
15170 dict_T *dict;
15171
15172 if (rettv_dict_alloc(rettv) == FAIL)
15173 return;
15174 dict = rettv->vval.v_dict;
15175
Bram Moolenaare0be1672018-07-08 16:50:37 +020015176 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
15177 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +020015178 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015179 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020015180 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015181
Bram Moolenaare0be1672018-07-08 16:50:37 +020015182 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015183#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020015184 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015185#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020015186 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
15187 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015188}
15189
15190/*
15191 * "winwidth(nr)" function
15192 */
15193 static void
15194f_winwidth(typval_T *argvars, typval_T *rettv)
15195{
15196 win_T *wp;
15197
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015198 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015199 if (wp == NULL)
15200 rettv->vval.v_number = -1;
15201 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015202 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015203}
15204
15205/*
15206 * "wordcount()" function
15207 */
15208 static void
15209f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
15210{
15211 if (rettv_dict_alloc(rettv) == FAIL)
15212 return;
15213 cursor_pos_info(rettv->vval.v_dict);
15214}
15215
15216/*
15217 * "writefile()" function
15218 */
15219 static void
15220f_writefile(typval_T *argvars, typval_T *rettv)
15221{
15222 int binary = FALSE;
15223 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015224#ifdef HAVE_FSYNC
15225 int do_fsync = p_fs;
15226#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015227 char_u *fname;
15228 FILE *fd;
15229 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015230 listitem_T *li;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015231 list_T *list = NULL;
15232 blob_T *blob = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015233
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015234 rettv->vval.v_number = -1;
Bram Moolenaar8c62a082019-02-08 14:34:10 +010015235 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015236 return;
15237
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015238 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015239 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015240 list = argvars[0].vval.v_list;
15241 if (list == NULL)
15242 return;
15243 for (li = list->lv_first; li != NULL; li = li->li_next)
15244 if (tv_get_string_chk(&li->li_tv) == NULL)
15245 return;
15246 }
15247 else if (argvars[0].v_type == VAR_BLOB)
15248 {
15249 blob = argvars[0].vval.v_blob;
15250 if (blob == NULL)
15251 return;
15252 }
15253 else
15254 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015255 semsg(_(e_invarg2), "writefile()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015256 return;
15257 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015258
15259 if (argvars[2].v_type != VAR_UNKNOWN)
15260 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015261 char_u *arg2 = tv_get_string_chk(&argvars[2]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015262
15263 if (arg2 == NULL)
15264 return;
15265 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015266 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015267 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015268 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015269#ifdef HAVE_FSYNC
15270 if (vim_strchr(arg2, 's') != NULL)
15271 do_fsync = TRUE;
15272 else if (vim_strchr(arg2, 'S') != NULL)
15273 do_fsync = FALSE;
15274#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015275 }
15276
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015277 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015278 if (fname == NULL)
15279 return;
15280
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015281 /* Always open the file in binary mode, library functions have a mind of
15282 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015283 if (*fname == NUL || (fd = mch_fopen((char *)fname,
15284 append ? APPENDBIN : WRITEBIN)) == NULL)
15285 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015286 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015287 ret = -1;
15288 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015289 else if (blob)
15290 {
15291 if (write_blob(fd, blob) == FAIL)
15292 ret = -1;
15293#ifdef HAVE_FSYNC
15294 else if (do_fsync)
15295 // Ignore the error, the user wouldn't know what to do about it.
15296 // May happen for a device.
Bram Moolenaara7870192019-02-14 12:56:36 +010015297 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015298#endif
15299 fclose(fd);
15300 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015301 else
15302 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015303 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015304 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015305#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010015306 else if (do_fsync)
15307 /* Ignore the error, the user wouldn't know what to do about it.
15308 * May happen for a device. */
Bram Moolenaara7870192019-02-14 12:56:36 +010015309 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015310#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015311 fclose(fd);
15312 }
15313
15314 rettv->vval.v_number = ret;
15315}
15316
15317/*
15318 * "xor(expr, expr)" function
15319 */
15320 static void
15321f_xor(typval_T *argvars, typval_T *rettv)
15322{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015323 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
15324 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015325}
15326
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015327#endif /* FEAT_EVAL */