blob: e65ab94e384655a27eb582686e4b9336b8e4828e [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;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02001532 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001533 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)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004204 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004205 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004206 return;
4207 }
4208#endif
4209#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004210 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004211#endif
4212}
4213
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004214 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004215common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004216{
4217 char_u *s;
4218 char_u *name;
4219 int use_string = FALSE;
4220 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004221 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004222
4223 if (argvars[0].v_type == VAR_FUNC)
4224 {
4225 /* function(MyFunc, [arg], dict) */
4226 s = argvars[0].vval.v_string;
4227 }
4228 else if (argvars[0].v_type == VAR_PARTIAL
4229 && argvars[0].vval.v_partial != NULL)
4230 {
4231 /* function(dict.MyFunc, [arg]) */
4232 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004233 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004234 }
4235 else
4236 {
4237 /* function('MyFunc', [arg], dict) */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004238 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004239 use_string = TRUE;
4240 }
4241
Bram Moolenaar843b8842016-08-21 14:36:15 +02004242 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004243 {
4244 name = s;
4245 trans_name = trans_function_name(&name, FALSE,
4246 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4247 if (*name != NUL)
4248 s = NULL;
4249 }
4250
Bram Moolenaar843b8842016-08-21 14:36:15 +02004251 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4252 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004253 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004254 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004255 else if (trans_name != NULL && (is_funcref
4256 ? find_func(trans_name) == NULL
4257 : !translated_function_exists(trans_name)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004258 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004259 else
4260 {
4261 int dict_idx = 0;
4262 int arg_idx = 0;
4263 list_T *list = NULL;
4264
4265 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4266 {
4267 char sid_buf[25];
4268 int off = *s == 's' ? 2 : 5;
4269
4270 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4271 * also be called from another script. Using trans_function_name()
4272 * would also work, but some plugins depend on the name being
4273 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004274 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004275 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
4276 if (name != NULL)
4277 {
4278 STRCPY(name, sid_buf);
4279 STRCAT(name, s + off);
4280 }
4281 }
4282 else
4283 name = vim_strsave(s);
4284
4285 if (argvars[1].v_type != VAR_UNKNOWN)
4286 {
4287 if (argvars[2].v_type != VAR_UNKNOWN)
4288 {
4289 /* function(name, [args], dict) */
4290 arg_idx = 1;
4291 dict_idx = 2;
4292 }
4293 else if (argvars[1].v_type == VAR_DICT)
4294 /* function(name, dict) */
4295 dict_idx = 1;
4296 else
4297 /* function(name, [args]) */
4298 arg_idx = 1;
4299 if (dict_idx > 0)
4300 {
4301 if (argvars[dict_idx].v_type != VAR_DICT)
4302 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004303 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004304 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004305 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004306 }
4307 if (argvars[dict_idx].vval.v_dict == NULL)
4308 dict_idx = 0;
4309 }
4310 if (arg_idx > 0)
4311 {
4312 if (argvars[arg_idx].v_type != VAR_LIST)
4313 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004314 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004315 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004316 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004317 }
4318 list = argvars[arg_idx].vval.v_list;
4319 if (list == NULL || list->lv_len == 0)
4320 arg_idx = 0;
4321 }
4322 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004323 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004324 {
4325 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
4326
4327 /* result is a VAR_PARTIAL */
4328 if (pt == NULL)
4329 vim_free(name);
4330 else
4331 {
4332 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4333 {
4334 listitem_T *li;
4335 int i = 0;
4336 int arg_len = 0;
4337 int lv_len = 0;
4338
4339 if (arg_pt != NULL)
4340 arg_len = arg_pt->pt_argc;
4341 if (list != NULL)
4342 lv_len = list->lv_len;
4343 pt->pt_argc = arg_len + lv_len;
4344 pt->pt_argv = (typval_T *)alloc(
4345 sizeof(typval_T) * pt->pt_argc);
4346 if (pt->pt_argv == NULL)
4347 {
4348 vim_free(pt);
4349 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004350 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004351 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004352 for (i = 0; i < arg_len; i++)
4353 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4354 if (lv_len > 0)
4355 for (li = list->lv_first; li != NULL;
4356 li = li->li_next)
4357 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004358 }
4359
4360 /* For "function(dict.func, [], dict)" and "func" is a partial
4361 * use "dict". That is backwards compatible. */
4362 if (dict_idx > 0)
4363 {
4364 /* The dict is bound explicitly, pt_auto is FALSE. */
4365 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4366 ++pt->pt_dict->dv_refcount;
4367 }
4368 else if (arg_pt != NULL)
4369 {
4370 /* If the dict was bound automatically the result is also
4371 * bound automatically. */
4372 pt->pt_dict = arg_pt->pt_dict;
4373 pt->pt_auto = arg_pt->pt_auto;
4374 if (pt->pt_dict != NULL)
4375 ++pt->pt_dict->dv_refcount;
4376 }
4377
4378 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004379 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4380 {
4381 pt->pt_func = arg_pt->pt_func;
4382 func_ptr_ref(pt->pt_func);
4383 vim_free(name);
4384 }
4385 else if (is_funcref)
4386 {
4387 pt->pt_func = find_func(trans_name);
4388 func_ptr_ref(pt->pt_func);
4389 vim_free(name);
4390 }
4391 else
4392 {
4393 pt->pt_name = name;
4394 func_ref(name);
4395 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004396 }
4397 rettv->v_type = VAR_PARTIAL;
4398 rettv->vval.v_partial = pt;
4399 }
4400 else
4401 {
4402 /* result is a VAR_FUNC */
4403 rettv->v_type = VAR_FUNC;
4404 rettv->vval.v_string = name;
4405 func_ref(name);
4406 }
4407 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004408theend:
4409 vim_free(trans_name);
4410}
4411
4412/*
4413 * "funcref()" function
4414 */
4415 static void
4416f_funcref(typval_T *argvars, typval_T *rettv)
4417{
4418 common_function(argvars, rettv, TRUE);
4419}
4420
4421/*
4422 * "function()" function
4423 */
4424 static void
4425f_function(typval_T *argvars, typval_T *rettv)
4426{
4427 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004428}
4429
4430/*
4431 * "garbagecollect()" function
4432 */
4433 static void
4434f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4435{
4436 /* This is postponed until we are back at the toplevel, because we may be
4437 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4438 want_garbage_collect = TRUE;
4439
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004440 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004441 garbage_collect_at_exit = TRUE;
4442}
4443
4444/*
4445 * "get()" function
4446 */
4447 static void
4448f_get(typval_T *argvars, typval_T *rettv)
4449{
4450 listitem_T *li;
4451 list_T *l;
4452 dictitem_T *di;
4453 dict_T *d;
4454 typval_T *tv = NULL;
4455
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004456 if (argvars[0].v_type == VAR_BLOB)
4457 {
4458 int error = FALSE;
4459 int idx = tv_get_number_chk(&argvars[1], &error);
4460
4461 if (!error)
4462 {
4463 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004464 if (idx < 0)
4465 idx = blob_len(argvars[0].vval.v_blob) + idx;
4466 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
4467 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004468 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004469 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004470 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004471 tv = rettv;
4472 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004473 }
4474 }
4475 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004476 {
4477 if ((l = argvars[0].vval.v_list) != NULL)
4478 {
4479 int error = FALSE;
4480
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004481 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004482 if (!error && li != NULL)
4483 tv = &li->li_tv;
4484 }
4485 }
4486 else if (argvars[0].v_type == VAR_DICT)
4487 {
4488 if ((d = argvars[0].vval.v_dict) != NULL)
4489 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004490 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004491 if (di != NULL)
4492 tv = &di->di_tv;
4493 }
4494 }
4495 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4496 {
4497 partial_T *pt;
4498 partial_T fref_pt;
4499
4500 if (argvars[0].v_type == VAR_PARTIAL)
4501 pt = argvars[0].vval.v_partial;
4502 else
4503 {
4504 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4505 fref_pt.pt_name = argvars[0].vval.v_string;
4506 pt = &fref_pt;
4507 }
4508
4509 if (pt != NULL)
4510 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004511 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004512 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004513
4514 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4515 {
4516 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004517 n = partial_name(pt);
4518 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004519 rettv->vval.v_string = NULL;
4520 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004521 {
4522 rettv->vval.v_string = vim_strsave(n);
4523 if (rettv->v_type == VAR_FUNC)
4524 func_ref(rettv->vval.v_string);
4525 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004526 }
4527 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004528 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004529 else if (STRCMP(what, "args") == 0)
4530 {
4531 rettv->v_type = VAR_LIST;
4532 if (rettv_list_alloc(rettv) == OK)
4533 {
4534 int i;
4535
4536 for (i = 0; i < pt->pt_argc; ++i)
4537 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4538 }
4539 }
4540 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004541 semsg(_(e_invarg2), what);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004542 return;
4543 }
4544 }
4545 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004546 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004547
4548 if (tv == NULL)
4549 {
4550 if (argvars[2].v_type != VAR_UNKNOWN)
4551 copy_tv(&argvars[2], rettv);
4552 }
4553 else
4554 copy_tv(tv, rettv);
4555}
4556
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004557/*
4558 * Returns buffer options, variables and other attributes in a dictionary.
4559 */
4560 static dict_T *
4561get_buffer_info(buf_T *buf)
4562{
4563 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004564 tabpage_T *tp;
4565 win_T *wp;
4566 list_T *windows;
4567
4568 dict = dict_alloc();
4569 if (dict == NULL)
4570 return NULL;
4571
Bram Moolenaare0be1672018-07-08 16:50:37 +02004572 dict_add_number(dict, "bufnr", buf->b_fnum);
4573 dict_add_string(dict, "name", buf->b_ffname);
4574 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4575 : buflist_findlnum(buf));
4576 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4577 dict_add_number(dict, "listed", buf->b_p_bl);
4578 dict_add_number(dict, "changed", bufIsChanged(buf));
4579 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4580 dict_add_number(dict, "hidden",
4581 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004582
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004583 /* Get a reference to buffer variables */
4584 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004585
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004586 /* List of windows displaying this buffer */
4587 windows = list_alloc();
4588 if (windows != NULL)
4589 {
4590 FOR_ALL_TAB_WINDOWS(tp, wp)
4591 if (wp->w_buffer == buf)
4592 list_append_number(windows, (varnumber_T)wp->w_id);
4593 dict_add_list(dict, "windows", windows);
4594 }
4595
4596#ifdef FEAT_SIGNS
4597 if (buf->b_signlist != NULL)
4598 {
4599 /* List of signs placed in this buffer */
4600 list_T *signs = list_alloc();
4601 if (signs != NULL)
4602 {
4603 get_buffer_signs(buf, signs);
4604 dict_add_list(dict, "signs", signs);
4605 }
4606 }
4607#endif
4608
4609 return dict;
4610}
4611
4612/*
4613 * "getbufinfo()" function
4614 */
4615 static void
4616f_getbufinfo(typval_T *argvars, typval_T *rettv)
4617{
4618 buf_T *buf = NULL;
4619 buf_T *argbuf = NULL;
4620 dict_T *d;
4621 int filtered = FALSE;
4622 int sel_buflisted = FALSE;
4623 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004624 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004625
4626 if (rettv_list_alloc(rettv) != OK)
4627 return;
4628
4629 /* List of all the buffers or selected buffers */
4630 if (argvars[0].v_type == VAR_DICT)
4631 {
4632 dict_T *sel_d = argvars[0].vval.v_dict;
4633
4634 if (sel_d != NULL)
4635 {
4636 dictitem_T *di;
4637
4638 filtered = TRUE;
4639
4640 di = dict_find(sel_d, (char_u *)"buflisted", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004641 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004642 sel_buflisted = TRUE;
4643
4644 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004645 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004646 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004647
4648 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004649 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004650 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004651 }
4652 }
4653 else if (argvars[0].v_type != VAR_UNKNOWN)
4654 {
4655 /* Information about one buffer. Argument specifies the buffer */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004656 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004657 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004658 argbuf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004659 --emsg_off;
4660 if (argbuf == NULL)
4661 return;
4662 }
4663
4664 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004665 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004666 {
4667 if (argbuf != NULL && argbuf != buf)
4668 continue;
4669 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004670 || (sel_buflisted && !buf->b_p_bl)
4671 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004672 continue;
4673
4674 d = get_buffer_info(buf);
4675 if (d != NULL)
4676 list_append_dict(rettv->vval.v_list, d);
4677 if (argbuf != NULL)
4678 return;
4679 }
4680}
4681
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004682/*
4683 * Get line or list of lines from buffer "buf" into "rettv".
4684 * Return a range (from start to end) of lines in rettv from the specified
4685 * buffer.
4686 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4687 */
4688 static void
4689get_buffer_lines(
4690 buf_T *buf,
4691 linenr_T start,
4692 linenr_T end,
4693 int retlist,
4694 typval_T *rettv)
4695{
4696 char_u *p;
4697
4698 rettv->v_type = VAR_STRING;
4699 rettv->vval.v_string = NULL;
4700 if (retlist && rettv_list_alloc(rettv) == FAIL)
4701 return;
4702
4703 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4704 return;
4705
4706 if (!retlist)
4707 {
4708 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4709 p = ml_get_buf(buf, start, FALSE);
4710 else
4711 p = (char_u *)"";
4712 rettv->vval.v_string = vim_strsave(p);
4713 }
4714 else
4715 {
4716 if (end < start)
4717 return;
4718
4719 if (start < 1)
4720 start = 1;
4721 if (end > buf->b_ml.ml_line_count)
4722 end = buf->b_ml.ml_line_count;
4723 while (start <= end)
4724 if (list_append_string(rettv->vval.v_list,
4725 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4726 break;
4727 }
4728}
4729
4730/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004731 * "getbufline()" function
4732 */
4733 static void
4734f_getbufline(typval_T *argvars, typval_T *rettv)
4735{
4736 linenr_T lnum;
4737 linenr_T end;
4738 buf_T *buf;
4739
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004740 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004741 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004742 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004743 --emsg_off;
4744
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004745 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004746 if (argvars[2].v_type == VAR_UNKNOWN)
4747 end = lnum;
4748 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004749 end = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004750
4751 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4752}
4753
4754/*
4755 * "getbufvar()" function
4756 */
4757 static void
4758f_getbufvar(typval_T *argvars, typval_T *rettv)
4759{
4760 buf_T *buf;
4761 buf_T *save_curbuf;
4762 char_u *varname;
4763 dictitem_T *v;
4764 int done = FALSE;
4765
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004766 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
4767 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004768 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004769 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004770
4771 rettv->v_type = VAR_STRING;
4772 rettv->vval.v_string = NULL;
4773
4774 if (buf != NULL && varname != NULL)
4775 {
4776 /* set curbuf to be our buf, temporarily */
4777 save_curbuf = curbuf;
4778 curbuf = buf;
4779
Bram Moolenaar30567352016-08-27 21:25:44 +02004780 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004781 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004782 if (varname[1] == NUL)
4783 {
4784 /* get all buffer-local options in a dict */
4785 dict_T *opts = get_winbuf_options(TRUE);
4786
4787 if (opts != NULL)
4788 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004789 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004790 done = TRUE;
4791 }
4792 }
4793 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4794 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004795 done = TRUE;
4796 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004797 else
4798 {
4799 /* Look up the variable. */
4800 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4801 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4802 'b', varname, FALSE);
4803 if (v != NULL)
4804 {
4805 copy_tv(&v->di_tv, rettv);
4806 done = TRUE;
4807 }
4808 }
4809
4810 /* restore previous notion of curbuf */
4811 curbuf = save_curbuf;
4812 }
4813
4814 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4815 /* use the default value */
4816 copy_tv(&argvars[2], rettv);
4817
4818 --emsg_off;
4819}
4820
4821/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004822 * "getchangelist()" function
4823 */
4824 static void
4825f_getchangelist(typval_T *argvars, typval_T *rettv)
4826{
4827#ifdef FEAT_JUMPLIST
4828 buf_T *buf;
4829 int i;
4830 list_T *l;
4831 dict_T *d;
4832#endif
4833
4834 if (rettv_list_alloc(rettv) != OK)
4835 return;
4836
4837#ifdef FEAT_JUMPLIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004838 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004839 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004840 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004841 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004842 if (buf == NULL)
4843 return;
4844
4845 l = list_alloc();
4846 if (l == NULL)
4847 return;
4848
4849 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4850 return;
4851 /*
4852 * The current window change list index tracks only the position in the
4853 * current buffer change list. For other buffers, use the change list
4854 * length as the current index.
4855 */
4856 list_append_number(rettv->vval.v_list,
4857 (varnumber_T)((buf == curwin->w_buffer)
4858 ? curwin->w_changelistidx : buf->b_changelistlen));
4859
4860 for (i = 0; i < buf->b_changelistlen; ++i)
4861 {
4862 if (buf->b_changelist[i].lnum == 0)
4863 continue;
4864 if ((d = dict_alloc()) == NULL)
4865 return;
4866 if (list_append_dict(l, d) == FAIL)
4867 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004868 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4869 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004870 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004871 }
4872#endif
4873}
4874/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004875 * "getchar()" function
4876 */
4877 static void
4878f_getchar(typval_T *argvars, typval_T *rettv)
4879{
4880 varnumber_T n;
4881 int error = FALSE;
4882
Bram Moolenaar84d93902018-09-11 20:10:20 +02004883#ifdef MESSAGE_QUEUE
4884 // vpeekc() used to check for messages, but that caused problems, invoking
4885 // a callback where it was not expected. Some plugins use getchar(1) in a
4886 // loop to await a message, therefore make sure we check for messages here.
4887 parse_queued_messages();
4888#endif
4889
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004890 /* Position the cursor. Needed after a message that ends in a space. */
4891 windgoto(msg_row, msg_col);
4892
4893 ++no_mapping;
4894 ++allow_keys;
4895 for (;;)
4896 {
4897 if (argvars[0].v_type == VAR_UNKNOWN)
4898 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004899 n = plain_vgetc();
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004900 else if (tv_get_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004901 /* getchar(1): only check if char avail */
4902 n = vpeekc_any();
4903 else if (error || vpeekc_any() == NUL)
4904 /* illegal argument or getchar(0) and no char avail: return zero */
4905 n = 0;
4906 else
4907 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004908 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004909
4910 if (n == K_IGNORE)
4911 continue;
4912 break;
4913 }
4914 --no_mapping;
4915 --allow_keys;
4916
4917 set_vim_var_nr(VV_MOUSE_WIN, 0);
4918 set_vim_var_nr(VV_MOUSE_WINID, 0);
4919 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4920 set_vim_var_nr(VV_MOUSE_COL, 0);
4921
4922 rettv->vval.v_number = n;
4923 if (IS_SPECIAL(n) || mod_mask != 0)
4924 {
4925 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4926 int i = 0;
4927
4928 /* Turn a special key into three bytes, plus modifier. */
4929 if (mod_mask != 0)
4930 {
4931 temp[i++] = K_SPECIAL;
4932 temp[i++] = KS_MODIFIER;
4933 temp[i++] = mod_mask;
4934 }
4935 if (IS_SPECIAL(n))
4936 {
4937 temp[i++] = K_SPECIAL;
4938 temp[i++] = K_SECOND(n);
4939 temp[i++] = K_THIRD(n);
4940 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004941 else if (has_mbyte)
4942 i += (*mb_char2bytes)(n, temp + i);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004943 else
4944 temp[i++] = n;
4945 temp[i++] = NUL;
4946 rettv->v_type = VAR_STRING;
4947 rettv->vval.v_string = vim_strsave(temp);
4948
4949#ifdef FEAT_MOUSE
4950 if (is_mouse_key(n))
4951 {
4952 int row = mouse_row;
4953 int col = mouse_col;
4954 win_T *win;
4955 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004956 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004957 int winnr = 1;
4958
4959 if (row >= 0 && col >= 0)
4960 {
4961 /* Find the window at the mouse coordinates and compute the
4962 * text position. */
4963 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004964 if (win == NULL)
4965 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004966 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004967 for (wp = firstwin; wp != win; wp = wp->w_next)
4968 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004969 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4970 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4971 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4972 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4973 }
4974 }
4975#endif
4976 }
4977}
4978
4979/*
4980 * "getcharmod()" function
4981 */
4982 static void
4983f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4984{
4985 rettv->vval.v_number = mod_mask;
4986}
4987
4988/*
4989 * "getcharsearch()" function
4990 */
4991 static void
4992f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4993{
4994 if (rettv_dict_alloc(rettv) != FAIL)
4995 {
4996 dict_T *dict = rettv->vval.v_dict;
4997
Bram Moolenaare0be1672018-07-08 16:50:37 +02004998 dict_add_string(dict, "char", last_csearch());
4999 dict_add_number(dict, "forward", last_csearch_forward());
5000 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005001 }
5002}
5003
5004/*
5005 * "getcmdline()" function
5006 */
5007 static void
5008f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
5009{
5010 rettv->v_type = VAR_STRING;
5011 rettv->vval.v_string = get_cmdline_str();
5012}
5013
5014/*
5015 * "getcmdpos()" function
5016 */
5017 static void
5018f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
5019{
5020 rettv->vval.v_number = get_cmdline_pos() + 1;
5021}
5022
5023/*
5024 * "getcmdtype()" function
5025 */
5026 static void
5027f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
5028{
5029 rettv->v_type = VAR_STRING;
5030 rettv->vval.v_string = alloc(2);
5031 if (rettv->vval.v_string != NULL)
5032 {
5033 rettv->vval.v_string[0] = get_cmdline_type();
5034 rettv->vval.v_string[1] = NUL;
5035 }
5036}
5037
5038/*
5039 * "getcmdwintype()" function
5040 */
5041 static void
5042f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
5043{
5044 rettv->v_type = VAR_STRING;
5045 rettv->vval.v_string = NULL;
5046#ifdef FEAT_CMDWIN
5047 rettv->vval.v_string = alloc(2);
5048 if (rettv->vval.v_string != NULL)
5049 {
5050 rettv->vval.v_string[0] = cmdwin_type;
5051 rettv->vval.v_string[1] = NUL;
5052 }
5053#endif
5054}
5055
5056#if defined(FEAT_CMDL_COMPL)
5057/*
5058 * "getcompletion()" function
5059 */
5060 static void
5061f_getcompletion(typval_T *argvars, typval_T *rettv)
5062{
5063 char_u *pat;
5064 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005065 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005066 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
5067 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005068
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005069 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005070 filtered = tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005071
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005072 if (p_wic)
5073 options |= WILD_ICASE;
5074
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005075 /* For filtered results, 'wildignore' is used */
5076 if (!filtered)
5077 options |= WILD_KEEP_ALL;
5078
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005079 ExpandInit(&xpc);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005080 xpc.xp_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005081 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005082 xpc.xp_context = cmdcomplete_str_to_type(tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005083 if (xpc.xp_context == EXPAND_NOTHING)
5084 {
5085 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005086 semsg(_(e_invarg2), argvars[1].vval.v_string);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005087 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005088 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005089 return;
5090 }
5091
5092# if defined(FEAT_MENU)
5093 if (xpc.xp_context == EXPAND_MENUS)
5094 {
5095 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
5096 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5097 }
5098# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02005099#ifdef FEAT_CSCOPE
5100 if (xpc.xp_context == EXPAND_CSCOPE)
5101 {
5102 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
5103 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5104 }
5105#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02005106#ifdef FEAT_SIGNS
5107 if (xpc.xp_context == EXPAND_SIGN)
5108 {
5109 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
5110 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5111 }
5112#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005113
5114 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
5115 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
5116 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005117 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005118
5119 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
5120
5121 for (i = 0; i < xpc.xp_numfiles; i++)
5122 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5123 }
5124 vim_free(pat);
5125 ExpandCleanup(&xpc);
5126}
5127#endif
5128
5129/*
5130 * "getcwd()" function
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005131 *
5132 * Return the current working directory of a window in a tab page.
5133 * First optional argument 'winnr' is the window number or -1 and the second
5134 * optional argument 'tabnr' is the tab page number.
5135 *
5136 * If no arguments are supplied, then return the directory of the current
5137 * window.
5138 * If only 'winnr' is specified and is not -1 or 0 then return the directory of
5139 * the specified window.
5140 * If 'winnr' is 0 then return the directory of the current window.
5141 * If both 'winnr and 'tabnr' are specified and 'winnr' is -1 then return the
5142 * directory of the specified tab page. Otherwise return the directory of the
5143 * specified window in the specified tab page.
5144 * If the window or the tab page doesn't exist then return NULL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005145 */
5146 static void
5147f_getcwd(typval_T *argvars, typval_T *rettv)
5148{
5149 win_T *wp = NULL;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005150 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005151 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01005152 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005153
5154 rettv->v_type = VAR_STRING;
5155 rettv->vval.v_string = NULL;
5156
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005157 if (argvars[0].v_type == VAR_NUMBER
5158 && argvars[0].vval.v_number == -1
5159 && argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar54591292018-02-09 20:53:59 +01005160 global = TRUE;
5161 else
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005162 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
Bram Moolenaar54591292018-02-09 20:53:59 +01005163
5164 if (wp != NULL && wp->w_localdir != NULL)
5165 rettv->vval.v_string = vim_strsave(wp->w_localdir);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005166 else if (tp != NULL && tp->tp_localdir != NULL)
5167 rettv->vval.v_string = vim_strsave(tp->tp_localdir);
5168 else if (wp != NULL || tp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005169 {
Bram Moolenaar54591292018-02-09 20:53:59 +01005170 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005171 rettv->vval.v_string = vim_strsave(globaldir);
5172 else
5173 {
5174 cwd = alloc(MAXPATHL);
5175 if (cwd != NULL)
5176 {
5177 if (mch_dirname(cwd, MAXPATHL) != FAIL)
5178 rettv->vval.v_string = vim_strsave(cwd);
5179 vim_free(cwd);
5180 }
5181 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005182 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02005183#ifdef BACKSLASH_IN_FILENAME
5184 if (rettv->vval.v_string != NULL)
5185 slash_adjust(rettv->vval.v_string);
5186#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005187}
5188
5189/*
5190 * "getfontname()" function
5191 */
5192 static void
5193f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
5194{
5195 rettv->v_type = VAR_STRING;
5196 rettv->vval.v_string = NULL;
5197#ifdef FEAT_GUI
5198 if (gui.in_use)
5199 {
5200 GuiFont font;
5201 char_u *name = NULL;
5202
5203 if (argvars[0].v_type == VAR_UNKNOWN)
5204 {
5205 /* Get the "Normal" font. Either the name saved by
5206 * hl_set_font_name() or from the font ID. */
5207 font = gui.norm_font;
5208 name = hl_get_font_name();
5209 }
5210 else
5211 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005212 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005213 if (STRCMP(name, "*") == 0) /* don't use font dialog */
5214 return;
5215 font = gui_mch_get_font(name, FALSE);
5216 if (font == NOFONT)
5217 return; /* Invalid font name, return empty string. */
5218 }
5219 rettv->vval.v_string = gui_mch_get_fontname(font, name);
5220 if (argvars[0].v_type != VAR_UNKNOWN)
5221 gui_mch_free_font(font);
5222 }
5223#endif
5224}
5225
5226/*
5227 * "getfperm({fname})" function
5228 */
5229 static void
5230f_getfperm(typval_T *argvars, typval_T *rettv)
5231{
5232 char_u *fname;
5233 stat_T st;
5234 char_u *perm = NULL;
5235 char_u flags[] = "rwx";
5236 int i;
5237
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005238 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005239
5240 rettv->v_type = VAR_STRING;
5241 if (mch_stat((char *)fname, &st) >= 0)
5242 {
5243 perm = vim_strsave((char_u *)"---------");
5244 if (perm != NULL)
5245 {
5246 for (i = 0; i < 9; i++)
5247 {
5248 if (st.st_mode & (1 << (8 - i)))
5249 perm[i] = flags[i % 3];
5250 }
5251 }
5252 }
5253 rettv->vval.v_string = perm;
5254}
5255
5256/*
5257 * "getfsize({fname})" function
5258 */
5259 static void
5260f_getfsize(typval_T *argvars, typval_T *rettv)
5261{
5262 char_u *fname;
5263 stat_T st;
5264
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005265 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005266
5267 rettv->v_type = VAR_NUMBER;
5268
5269 if (mch_stat((char *)fname, &st) >= 0)
5270 {
5271 if (mch_isdir(fname))
5272 rettv->vval.v_number = 0;
5273 else
5274 {
5275 rettv->vval.v_number = (varnumber_T)st.st_size;
5276
5277 /* non-perfect check for overflow */
5278 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5279 rettv->vval.v_number = -2;
5280 }
5281 }
5282 else
5283 rettv->vval.v_number = -1;
5284}
5285
5286/*
5287 * "getftime({fname})" function
5288 */
5289 static void
5290f_getftime(typval_T *argvars, typval_T *rettv)
5291{
5292 char_u *fname;
5293 stat_T st;
5294
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005295 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005296
5297 if (mch_stat((char *)fname, &st) >= 0)
5298 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5299 else
5300 rettv->vval.v_number = -1;
5301}
5302
5303/*
5304 * "getftype({fname})" function
5305 */
5306 static void
5307f_getftype(typval_T *argvars, typval_T *rettv)
5308{
5309 char_u *fname;
5310 stat_T st;
5311 char_u *type = NULL;
5312 char *t;
5313
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005314 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005315
5316 rettv->v_type = VAR_STRING;
5317 if (mch_lstat((char *)fname, &st) >= 0)
5318 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005319 if (S_ISREG(st.st_mode))
5320 t = "file";
5321 else if (S_ISDIR(st.st_mode))
5322 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005323 else if (S_ISLNK(st.st_mode))
5324 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005325 else if (S_ISBLK(st.st_mode))
5326 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005327 else if (S_ISCHR(st.st_mode))
5328 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005329 else if (S_ISFIFO(st.st_mode))
5330 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005331 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005332 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005333 else
5334 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005335 type = vim_strsave((char_u *)t);
5336 }
5337 rettv->vval.v_string = type;
5338}
5339
5340/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005341 * "getjumplist()" function
5342 */
5343 static void
5344f_getjumplist(typval_T *argvars, typval_T *rettv)
5345{
5346#ifdef FEAT_JUMPLIST
5347 win_T *wp;
5348 int i;
5349 list_T *l;
5350 dict_T *d;
5351#endif
5352
5353 if (rettv_list_alloc(rettv) != OK)
5354 return;
5355
5356#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005357 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005358 if (wp == NULL)
5359 return;
5360
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01005361 cleanup_jumplist(wp, TRUE);
5362
Bram Moolenaar4f505882018-02-10 21:06:32 +01005363 l = list_alloc();
5364 if (l == NULL)
5365 return;
5366
5367 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5368 return;
5369 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5370
5371 for (i = 0; i < wp->w_jumplistlen; ++i)
5372 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005373 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5374 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005375 if ((d = dict_alloc()) == NULL)
5376 return;
5377 if (list_append_dict(l, d) == FAIL)
5378 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005379 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5380 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005381 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005382 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005383 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005384 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005385 }
5386#endif
5387}
5388
5389/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005390 * "getline(lnum, [end])" function
5391 */
5392 static void
5393f_getline(typval_T *argvars, typval_T *rettv)
5394{
5395 linenr_T lnum;
5396 linenr_T end;
5397 int retlist;
5398
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005399 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005400 if (argvars[1].v_type == VAR_UNKNOWN)
5401 {
5402 end = 0;
5403 retlist = FALSE;
5404 }
5405 else
5406 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005407 end = tv_get_lnum(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005408 retlist = TRUE;
5409 }
5410
5411 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5412}
5413
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005414#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005415 static void
5416get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5417{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005418 if (what_arg->v_type == VAR_UNKNOWN)
5419 {
5420 if (rettv_list_alloc(rettv) == OK)
5421 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005422 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005423 }
5424 else
5425 {
5426 if (rettv_dict_alloc(rettv) == OK)
5427 if (is_qf || (wp != NULL))
5428 {
5429 if (what_arg->v_type == VAR_DICT)
5430 {
5431 dict_T *d = what_arg->vval.v_dict;
5432
5433 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005434 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005435 }
5436 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005437 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +02005438 }
5439 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005440}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005441#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005442
5443/*
5444 * "getloclist()" function
5445 */
5446 static void
5447f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5448{
5449#ifdef FEAT_QUICKFIX
5450 win_T *wp;
5451
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005452 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005453 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5454#endif
5455}
5456
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005457/*
5458 * "getmatches()" function
5459 */
5460 static void
5461f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5462{
5463#ifdef FEAT_SEARCH_EXTRA
5464 dict_T *dict;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005465 matchitem_T *cur;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005466 int i;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005467 win_T *win = get_optional_window(argvars, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005468
Bram Moolenaaraff74912019-03-30 18:11:49 +01005469 if (rettv_list_alloc(rettv) == FAIL || win == NULL)
5470 return;
5471
5472 cur = win->w_match_head;
5473 while (cur != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005474 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005475 dict = dict_alloc();
5476 if (dict == NULL)
5477 return;
5478 if (cur->match.regprog == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005479 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005480 /* match added with matchaddpos() */
5481 for (i = 0; i < MAXPOSMATCH; ++i)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005482 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005483 llpos_T *llpos;
Bram Moolenaar54315892019-04-26 22:33:49 +02005484 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaaraff74912019-03-30 18:11:49 +01005485 list_T *l;
5486
5487 llpos = &cur->pos.pos[i];
5488 if (llpos->lnum == 0)
5489 break;
5490 l = list_alloc();
5491 if (l == NULL)
5492 break;
5493 list_append_number(l, (varnumber_T)llpos->lnum);
5494 if (llpos->col > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005495 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005496 list_append_number(l, (varnumber_T)llpos->col);
5497 list_append_number(l, (varnumber_T)llpos->len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005498 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005499 sprintf(buf, "pos%d", i + 1);
5500 dict_add_list(dict, buf, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005501 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005502 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005503 else
5504 {
5505 dict_add_string(dict, "pattern", cur->pattern);
5506 }
5507 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
5508 dict_add_number(dict, "priority", (long)cur->priority);
5509 dict_add_number(dict, "id", (long)cur->id);
5510# if defined(FEAT_CONCEAL)
5511 if (cur->conceal_char)
5512 {
5513 char_u buf[MB_MAXBYTES + 1];
5514
5515 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
5516 dict_add_string(dict, "conceal", (char_u *)&buf);
5517 }
5518# endif
5519 list_append_dict(rettv->vval.v_list, dict);
5520 cur = cur->next;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005521 }
5522#endif
5523}
5524
5525/*
5526 * "getpid()" function
5527 */
5528 static void
5529f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5530{
5531 rettv->vval.v_number = mch_get_pid();
5532}
5533
5534 static void
5535getpos_both(
5536 typval_T *argvars,
5537 typval_T *rettv,
5538 int getcurpos)
5539{
5540 pos_T *fp;
5541 list_T *l;
5542 int fnum = -1;
5543
5544 if (rettv_list_alloc(rettv) == OK)
5545 {
5546 l = rettv->vval.v_list;
5547 if (getcurpos)
5548 fp = &curwin->w_cursor;
5549 else
5550 fp = var2fpos(&argvars[0], TRUE, &fnum);
5551 if (fnum != -1)
5552 list_append_number(l, (varnumber_T)fnum);
5553 else
5554 list_append_number(l, (varnumber_T)0);
5555 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5556 : (varnumber_T)0);
5557 list_append_number(l, (fp != NULL)
5558 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5559 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005560 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005561 (varnumber_T)0);
5562 if (getcurpos)
5563 {
Bram Moolenaar19a66852019-03-07 11:25:32 +01005564 int save_set_curswant = curwin->w_set_curswant;
5565 colnr_T save_curswant = curwin->w_curswant;
5566 colnr_T save_virtcol = curwin->w_virtcol;
5567
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005568 update_curswant();
5569 list_append_number(l, curwin->w_curswant == MAXCOL ?
5570 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar19a66852019-03-07 11:25:32 +01005571
5572 // Do not change "curswant", as it is unexpected that a get
5573 // function has a side effect.
5574 if (save_set_curswant)
5575 {
5576 curwin->w_set_curswant = save_set_curswant;
5577 curwin->w_curswant = save_curswant;
5578 curwin->w_virtcol = save_virtcol;
5579 curwin->w_valid &= ~VALID_VIRTCOL;
5580 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005581 }
5582 }
5583 else
5584 rettv->vval.v_number = FALSE;
5585}
5586
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005587/*
5588 * "getcurpos()" function
5589 */
5590 static void
5591f_getcurpos(typval_T *argvars, typval_T *rettv)
5592{
5593 getpos_both(argvars, rettv, TRUE);
5594}
5595
5596/*
5597 * "getpos(string)" function
5598 */
5599 static void
5600f_getpos(typval_T *argvars, typval_T *rettv)
5601{
5602 getpos_both(argvars, rettv, FALSE);
5603}
5604
5605/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005606 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005607 */
5608 static void
5609f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5610{
5611#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005612 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005613#endif
5614}
5615
5616/*
5617 * "getreg()" function
5618 */
5619 static void
5620f_getreg(typval_T *argvars, typval_T *rettv)
5621{
5622 char_u *strregname;
5623 int regname;
5624 int arg2 = FALSE;
5625 int return_list = FALSE;
5626 int error = FALSE;
5627
5628 if (argvars[0].v_type != VAR_UNKNOWN)
5629 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005630 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005631 error = strregname == NULL;
5632 if (argvars[1].v_type != VAR_UNKNOWN)
5633 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005634 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005635 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005636 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005637 }
5638 }
5639 else
5640 strregname = get_vim_var_str(VV_REG);
5641
5642 if (error)
5643 return;
5644
5645 regname = (strregname == NULL ? '"' : *strregname);
5646 if (regname == 0)
5647 regname = '"';
5648
5649 if (return_list)
5650 {
5651 rettv->v_type = VAR_LIST;
5652 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5653 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5654 if (rettv->vval.v_list == NULL)
5655 (void)rettv_list_alloc(rettv);
5656 else
5657 ++rettv->vval.v_list->lv_refcount;
5658 }
5659 else
5660 {
5661 rettv->v_type = VAR_STRING;
5662 rettv->vval.v_string = get_reg_contents(regname,
5663 arg2 ? GREG_EXPR_SRC : 0);
5664 }
5665}
5666
5667/*
5668 * "getregtype()" function
5669 */
5670 static void
5671f_getregtype(typval_T *argvars, typval_T *rettv)
5672{
5673 char_u *strregname;
5674 int regname;
5675 char_u buf[NUMBUFLEN + 2];
5676 long reglen = 0;
5677
5678 if (argvars[0].v_type != VAR_UNKNOWN)
5679 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005680 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005681 if (strregname == NULL) /* type error; errmsg already given */
5682 {
5683 rettv->v_type = VAR_STRING;
5684 rettv->vval.v_string = NULL;
5685 return;
5686 }
5687 }
5688 else
5689 /* Default to v:register */
5690 strregname = get_vim_var_str(VV_REG);
5691
5692 regname = (strregname == NULL ? '"' : *strregname);
5693 if (regname == 0)
5694 regname = '"';
5695
5696 buf[0] = NUL;
5697 buf[1] = NUL;
5698 switch (get_reg_type(regname, &reglen))
5699 {
5700 case MLINE: buf[0] = 'V'; break;
5701 case MCHAR: buf[0] = 'v'; break;
5702 case MBLOCK:
5703 buf[0] = Ctrl_V;
5704 sprintf((char *)buf + 1, "%ld", reglen + 1);
5705 break;
5706 }
5707 rettv->v_type = VAR_STRING;
5708 rettv->vval.v_string = vim_strsave(buf);
5709}
5710
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005711/*
5712 * Returns information (variables, options, etc.) about a tab page
5713 * as a dictionary.
5714 */
5715 static dict_T *
5716get_tabpage_info(tabpage_T *tp, int tp_idx)
5717{
5718 win_T *wp;
5719 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005720 list_T *l;
5721
5722 dict = dict_alloc();
5723 if (dict == NULL)
5724 return NULL;
5725
Bram Moolenaare0be1672018-07-08 16:50:37 +02005726 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005727
5728 l = list_alloc();
5729 if (l != NULL)
5730 {
5731 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5732 wp; wp = wp->w_next)
5733 list_append_number(l, (varnumber_T)wp->w_id);
5734 dict_add_list(dict, "windows", l);
5735 }
5736
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005737 /* Make a reference to tabpage variables */
5738 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005739
5740 return dict;
5741}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005742
5743/*
5744 * "gettabinfo()" function
5745 */
5746 static void
5747f_gettabinfo(typval_T *argvars, typval_T *rettv)
5748{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005749 tabpage_T *tp, *tparg = NULL;
5750 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005751 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005752
5753 if (rettv_list_alloc(rettv) != OK)
5754 return;
5755
5756 if (argvars[0].v_type != VAR_UNKNOWN)
5757 {
5758 /* Information about one tab page */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005759 tparg = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005760 if (tparg == NULL)
5761 return;
5762 }
5763
5764 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005765 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005766 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005767 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005768 if (tparg != NULL && tp != tparg)
5769 continue;
5770 d = get_tabpage_info(tp, tpnr);
5771 if (d != NULL)
5772 list_append_dict(rettv->vval.v_list, d);
5773 if (tparg != NULL)
5774 return;
5775 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005776}
5777
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005778/*
5779 * "gettabvar()" function
5780 */
5781 static void
5782f_gettabvar(typval_T *argvars, typval_T *rettv)
5783{
5784 win_T *oldcurwin;
5785 tabpage_T *tp, *oldtabpage;
5786 dictitem_T *v;
5787 char_u *varname;
5788 int done = FALSE;
5789
5790 rettv->v_type = VAR_STRING;
5791 rettv->vval.v_string = NULL;
5792
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005793 varname = tv_get_string_chk(&argvars[1]);
5794 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005795 if (tp != NULL && varname != NULL)
5796 {
5797 /* Set tp to be our tabpage, temporarily. Also set the window to the
5798 * first window in the tabpage, otherwise the window is not valid. */
5799 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005800 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5801 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005802 {
5803 /* look up the variable */
5804 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5805 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5806 if (v != NULL)
5807 {
5808 copy_tv(&v->di_tv, rettv);
5809 done = TRUE;
5810 }
5811 }
5812
5813 /* restore previous notion of curwin */
5814 restore_win(oldcurwin, oldtabpage, TRUE);
5815 }
5816
5817 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5818 copy_tv(&argvars[2], rettv);
5819}
5820
5821/*
5822 * "gettabwinvar()" function
5823 */
5824 static void
5825f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5826{
5827 getwinvar(argvars, rettv, 1);
5828}
5829
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005830/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01005831 * "gettagstack()" function
5832 */
5833 static void
5834f_gettagstack(typval_T *argvars, typval_T *rettv)
5835{
5836 win_T *wp = curwin; // default is current window
5837
5838 if (rettv_dict_alloc(rettv) != OK)
5839 return;
5840
5841 if (argvars[0].v_type != VAR_UNKNOWN)
5842 {
5843 wp = find_win_by_nr_or_id(&argvars[0]);
5844 if (wp == NULL)
5845 return;
5846 }
5847
5848 get_tagstack(wp, rettv->vval.v_dict);
5849}
5850
5851/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005852 * Returns information about a window as a dictionary.
5853 */
5854 static dict_T *
5855get_win_info(win_T *wp, short tpnr, short winnr)
5856{
5857 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005858
5859 dict = dict_alloc();
5860 if (dict == NULL)
5861 return NULL;
5862
Bram Moolenaare0be1672018-07-08 16:50:37 +02005863 dict_add_number(dict, "tabnr", tpnr);
5864 dict_add_number(dict, "winnr", winnr);
5865 dict_add_number(dict, "winid", wp->w_id);
5866 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005867 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01005868 dict_add_number(dict, "topline", wp->w_topline);
5869 dict_add_number(dict, "botline", wp->w_botline - 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005870#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02005871 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005872#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005873 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005874 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005875 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005876
Bram Moolenaar69905d12017-08-13 18:14:47 +02005877#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02005878 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02005879#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005880#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02005881 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
5882 dict_add_number(dict, "loclist",
5883 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02005884#endif
5885
Bram Moolenaar30567352016-08-27 21:25:44 +02005886 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005887 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005888
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005889 return dict;
5890}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005891
5892/*
5893 * "getwininfo()" function
5894 */
5895 static void
5896f_getwininfo(typval_T *argvars, typval_T *rettv)
5897{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005898 tabpage_T *tp;
5899 win_T *wp = NULL, *wparg = NULL;
5900 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005901 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005902
5903 if (rettv_list_alloc(rettv) != OK)
5904 return;
5905
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005906 if (argvars[0].v_type != VAR_UNKNOWN)
5907 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01005908 wparg = win_id2wp(tv_get_number(&argvars[0]));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005909 if (wparg == NULL)
5910 return;
5911 }
5912
5913 /* Collect information about either all the windows across all the tab
5914 * pages or one particular window.
5915 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005916 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005917 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005918 tabnr++;
5919 winnr = 0;
5920 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005921 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005922 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005923 if (wparg != NULL && wp != wparg)
5924 continue;
5925 d = get_win_info(wp, tabnr, winnr);
5926 if (d != NULL)
5927 list_append_dict(rettv->vval.v_list, d);
5928 if (wparg != NULL)
5929 /* found information about a specific window */
5930 return;
5931 }
5932 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005933}
5934
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005935/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005936 * "win_findbuf()" function
5937 */
5938 static void
5939f_win_findbuf(typval_T *argvars, typval_T *rettv)
5940{
5941 if (rettv_list_alloc(rettv) != FAIL)
5942 win_findbuf(argvars, rettv->vval.v_list);
5943}
5944
5945/*
5946 * "win_getid()" function
5947 */
5948 static void
5949f_win_getid(typval_T *argvars, typval_T *rettv)
5950{
5951 rettv->vval.v_number = win_getid(argvars);
5952}
5953
5954/*
5955 * "win_gotoid()" function
5956 */
5957 static void
5958f_win_gotoid(typval_T *argvars, typval_T *rettv)
5959{
5960 rettv->vval.v_number = win_gotoid(argvars);
5961}
5962
5963/*
5964 * "win_id2tabwin()" function
5965 */
5966 static void
5967f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5968{
5969 if (rettv_list_alloc(rettv) != FAIL)
5970 win_id2tabwin(argvars, rettv->vval.v_list);
5971}
5972
5973/*
5974 * "win_id2win()" function
5975 */
5976 static void
5977f_win_id2win(typval_T *argvars, typval_T *rettv)
5978{
5979 rettv->vval.v_number = win_id2win(argvars);
5980}
5981
5982/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005983 * "win_screenpos()" function
5984 */
5985 static void
5986f_win_screenpos(typval_T *argvars, typval_T *rettv)
5987{
5988 win_T *wp;
5989
5990 if (rettv_list_alloc(rettv) == FAIL)
5991 return;
5992
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005993 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005994 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5995 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5996}
5997
5998/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005999 * "getwinpos({timeout})" function
6000 */
6001 static void
6002f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
6003{
6004 int x = -1;
6005 int y = -1;
6006
6007 if (rettv_list_alloc(rettv) == FAIL)
6008 return;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006009#if defined(FEAT_GUI) \
6010 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6011 || defined(MSWIN)
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006012 {
6013 varnumber_T timeout = 100;
6014
6015 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006016 timeout = tv_get_number(&argvars[0]);
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006017
6018 (void)ui_get_winpos(&x, &y, timeout);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006019 }
6020#endif
6021 list_append_number(rettv->vval.v_list, (varnumber_T)x);
6022 list_append_number(rettv->vval.v_list, (varnumber_T)y);
6023}
6024
6025
6026/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006027 * "getwinposx()" function
6028 */
6029 static void
6030f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
6031{
6032 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006033#if defined(FEAT_GUI) \
6034 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6035 || defined(MSWIN)
6036
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006037 {
6038 int x, y;
6039
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006040 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006041 rettv->vval.v_number = x;
6042 }
6043#endif
6044}
6045
6046/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006047 * "getwinposy()" function
6048 */
6049 static void
6050f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
6051{
6052 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006053#if defined(FEAT_GUI) \
6054 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6055 || defined(MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006056 {
6057 int x, y;
6058
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006059 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006060 rettv->vval.v_number = y;
6061 }
6062#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006063}
6064
6065/*
6066 * "getwinvar()" function
6067 */
6068 static void
6069f_getwinvar(typval_T *argvars, typval_T *rettv)
6070{
6071 getwinvar(argvars, rettv, 0);
6072}
6073
6074/*
6075 * "glob()" function
6076 */
6077 static void
6078f_glob(typval_T *argvars, typval_T *rettv)
6079{
6080 int options = WILD_SILENT|WILD_USE_NL;
6081 expand_T xpc;
6082 int error = FALSE;
6083
6084 /* When the optional second argument is non-zero, don't remove matches
6085 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6086 rettv->v_type = VAR_STRING;
6087 if (argvars[1].v_type != VAR_UNKNOWN)
6088 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006089 if (tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006090 options |= WILD_KEEP_ALL;
6091 if (argvars[2].v_type != VAR_UNKNOWN)
6092 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006093 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006094 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006095 if (argvars[3].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006096 && tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006097 options |= WILD_ALLLINKS;
6098 }
6099 }
6100 if (!error)
6101 {
6102 ExpandInit(&xpc);
6103 xpc.xp_context = EXPAND_FILES;
6104 if (p_wic)
6105 options += WILD_ICASE;
6106 if (rettv->v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006107 rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006108 NULL, options, WILD_ALL);
6109 else if (rettv_list_alloc(rettv) != FAIL)
6110 {
6111 int i;
6112
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006113 ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006114 NULL, options, WILD_ALL_KEEP);
6115 for (i = 0; i < xpc.xp_numfiles; i++)
6116 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
6117
6118 ExpandCleanup(&xpc);
6119 }
6120 }
6121 else
6122 rettv->vval.v_string = NULL;
6123}
6124
6125/*
6126 * "globpath()" function
6127 */
6128 static void
6129f_globpath(typval_T *argvars, typval_T *rettv)
6130{
6131 int flags = 0;
6132 char_u buf1[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006133 char_u *file = tv_get_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006134 int error = FALSE;
6135 garray_T ga;
6136 int i;
6137
6138 /* When the optional second argument is non-zero, don't remove matches
6139 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6140 rettv->v_type = VAR_STRING;
6141 if (argvars[2].v_type != VAR_UNKNOWN)
6142 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006143 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006144 flags |= WILD_KEEP_ALL;
6145 if (argvars[3].v_type != VAR_UNKNOWN)
6146 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006147 if (tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006148 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006149 if (argvars[4].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006150 && tv_get_number_chk(&argvars[4], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006151 flags |= WILD_ALLLINKS;
6152 }
6153 }
6154 if (file != NULL && !error)
6155 {
6156 ga_init2(&ga, (int)sizeof(char_u *), 10);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006157 globpath(tv_get_string(&argvars[0]), file, &ga, flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006158 if (rettv->v_type == VAR_STRING)
6159 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
6160 else if (rettv_list_alloc(rettv) != FAIL)
6161 for (i = 0; i < ga.ga_len; ++i)
6162 list_append_string(rettv->vval.v_list,
6163 ((char_u **)(ga.ga_data))[i], -1);
6164 ga_clear_strings(&ga);
6165 }
6166 else
6167 rettv->vval.v_string = NULL;
6168}
6169
6170/*
6171 * "glob2regpat()" function
6172 */
6173 static void
6174f_glob2regpat(typval_T *argvars, typval_T *rettv)
6175{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006176 char_u *pat = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006177
6178 rettv->v_type = VAR_STRING;
6179 rettv->vval.v_string = (pat == NULL)
6180 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
6181}
6182
6183/* for VIM_VERSION_ defines */
6184#include "version.h"
6185
6186/*
6187 * "has()" function
6188 */
6189 static void
6190f_has(typval_T *argvars, typval_T *rettv)
6191{
6192 int i;
6193 char_u *name;
6194 int n = FALSE;
6195 static char *(has_list[]) =
6196 {
6197#ifdef AMIGA
6198 "amiga",
6199# ifdef FEAT_ARP
6200 "arp",
6201# endif
6202#endif
6203#ifdef __BEOS__
6204 "beos",
6205#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006206#if defined(BSD) && !defined(MACOS_X)
6207 "bsd",
6208#endif
6209#ifdef hpux
6210 "hpux",
6211#endif
6212#ifdef __linux__
6213 "linux",
6214#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02006215#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01006216 "mac", /* Mac OS X (and, once, Mac OS Classic) */
6217 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02006218# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01006219 "macunix", /* Mac OS X, with the darwin feature */
6220 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02006221# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006222#endif
6223#ifdef __QNX__
6224 "qnx",
6225#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006226#ifdef SUN_SYSTEM
6227 "sun",
6228#else
6229 "moon",
6230#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006231#ifdef UNIX
6232 "unix",
6233#endif
6234#ifdef VMS
6235 "vms",
6236#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006237#ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006238 "win32",
6239#endif
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006240#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006241 "win32unix",
6242#endif
Bram Moolenaar44b443c2019-02-18 22:14:18 +01006243#ifdef _WIN64
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006244 "win64",
6245#endif
6246#ifdef EBCDIC
6247 "ebcdic",
6248#endif
6249#ifndef CASE_INSENSITIVE_FILENAME
6250 "fname_case",
6251#endif
6252#ifdef HAVE_ACL
6253 "acl",
6254#endif
6255#ifdef FEAT_ARABIC
6256 "arabic",
6257#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006258 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006259#ifdef FEAT_AUTOCHDIR
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006260 "autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006261#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006262#ifdef FEAT_AUTOSERVERNAME
6263 "autoservername",
6264#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006265#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006266 "balloon_eval",
Bram Moolenaar4f974752019-02-17 17:44:42 +01006267# ifndef FEAT_GUI_MSWIN /* other GUIs always have multiline balloons */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006268 "balloon_multiline",
6269# endif
6270#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006271#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006272 "balloon_eval_term",
6273#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006274#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6275 "builtin_terms",
6276# ifdef ALL_BUILTIN_TCAPS
6277 "all_builtin_terms",
6278# endif
6279#endif
6280#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01006281 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006282 || defined(FEAT_GUI_MOTIF))
6283 "browsefilter",
6284#endif
6285#ifdef FEAT_BYTEOFF
6286 "byte_offset",
6287#endif
6288#ifdef FEAT_JOB_CHANNEL
6289 "channel",
6290#endif
6291#ifdef FEAT_CINDENT
6292 "cindent",
6293#endif
6294#ifdef FEAT_CLIENTSERVER
6295 "clientserver",
6296#endif
6297#ifdef FEAT_CLIPBOARD
6298 "clipboard",
6299#endif
6300#ifdef FEAT_CMDL_COMPL
6301 "cmdline_compl",
6302#endif
6303#ifdef FEAT_CMDHIST
6304 "cmdline_hist",
6305#endif
6306#ifdef FEAT_COMMENTS
6307 "comments",
6308#endif
6309#ifdef FEAT_CONCEAL
6310 "conceal",
6311#endif
6312#ifdef FEAT_CRYPT
6313 "cryptv",
6314 "crypt-blowfish",
6315 "crypt-blowfish2",
6316#endif
6317#ifdef FEAT_CSCOPE
6318 "cscope",
6319#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006320 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006321#ifdef CURSOR_SHAPE
6322 "cursorshape",
6323#endif
6324#ifdef DEBUG
6325 "debug",
6326#endif
6327#ifdef FEAT_CON_DIALOG
6328 "dialog_con",
6329#endif
6330#ifdef FEAT_GUI_DIALOG
6331 "dialog_gui",
6332#endif
6333#ifdef FEAT_DIFF
6334 "diff",
6335#endif
6336#ifdef FEAT_DIGRAPHS
6337 "digraphs",
6338#endif
6339#ifdef FEAT_DIRECTX
6340 "directx",
6341#endif
6342#ifdef FEAT_DND
6343 "dnd",
6344#endif
6345#ifdef FEAT_EMACS_TAGS
6346 "emacs_tags",
6347#endif
6348 "eval", /* always present, of course! */
6349 "ex_extra", /* graduated feature */
6350#ifdef FEAT_SEARCH_EXTRA
6351 "extra_search",
6352#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006353#ifdef FEAT_SEARCHPATH
6354 "file_in_path",
6355#endif
6356#ifdef FEAT_FILTERPIPE
6357 "filterpipe",
6358#endif
6359#ifdef FEAT_FIND_ID
6360 "find_in_path",
6361#endif
6362#ifdef FEAT_FLOAT
6363 "float",
6364#endif
6365#ifdef FEAT_FOLDING
6366 "folding",
6367#endif
6368#ifdef FEAT_FOOTER
6369 "footer",
6370#endif
6371#if !defined(USE_SYSTEM) && defined(UNIX)
6372 "fork",
6373#endif
6374#ifdef FEAT_GETTEXT
6375 "gettext",
6376#endif
6377#ifdef FEAT_GUI
6378 "gui",
6379#endif
6380#ifdef FEAT_GUI_ATHENA
6381# ifdef FEAT_GUI_NEXTAW
6382 "gui_neXtaw",
6383# else
6384 "gui_athena",
6385# endif
6386#endif
6387#ifdef FEAT_GUI_GTK
6388 "gui_gtk",
6389# ifdef USE_GTK3
6390 "gui_gtk3",
6391# else
6392 "gui_gtk2",
6393# endif
6394#endif
6395#ifdef FEAT_GUI_GNOME
6396 "gui_gnome",
6397#endif
6398#ifdef FEAT_GUI_MAC
6399 "gui_mac",
6400#endif
6401#ifdef FEAT_GUI_MOTIF
6402 "gui_motif",
6403#endif
6404#ifdef FEAT_GUI_PHOTON
6405 "gui_photon",
6406#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006407#ifdef FEAT_GUI_MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006408 "gui_win32",
6409#endif
6410#ifdef FEAT_HANGULIN
6411 "hangul_input",
6412#endif
6413#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6414 "iconv",
6415#endif
6416#ifdef FEAT_INS_EXPAND
6417 "insert_expand",
6418#endif
6419#ifdef FEAT_JOB_CHANNEL
6420 "job",
6421#endif
6422#ifdef FEAT_JUMPLIST
6423 "jumplist",
6424#endif
6425#ifdef FEAT_KEYMAP
6426 "keymap",
6427#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006428 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006429#ifdef FEAT_LANGMAP
6430 "langmap",
6431#endif
6432#ifdef FEAT_LIBCALL
6433 "libcall",
6434#endif
6435#ifdef FEAT_LINEBREAK
6436 "linebreak",
6437#endif
6438#ifdef FEAT_LISP
6439 "lispindent",
6440#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006441 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006442#ifdef FEAT_LOCALMAP
6443 "localmap",
6444#endif
6445#ifdef FEAT_LUA
6446# ifndef DYNAMIC_LUA
6447 "lua",
6448# endif
6449#endif
6450#ifdef FEAT_MENU
6451 "menu",
6452#endif
6453#ifdef FEAT_SESSION
6454 "mksession",
6455#endif
6456#ifdef FEAT_MODIFY_FNAME
6457 "modify_fname",
6458#endif
6459#ifdef FEAT_MOUSE
6460 "mouse",
6461#endif
6462#ifdef FEAT_MOUSESHAPE
6463 "mouseshape",
6464#endif
6465#if defined(UNIX) || defined(VMS)
6466# ifdef FEAT_MOUSE_DEC
6467 "mouse_dec",
6468# endif
6469# ifdef FEAT_MOUSE_GPM
6470 "mouse_gpm",
6471# endif
6472# ifdef FEAT_MOUSE_JSB
6473 "mouse_jsbterm",
6474# endif
6475# ifdef FEAT_MOUSE_NET
6476 "mouse_netterm",
6477# endif
6478# ifdef FEAT_MOUSE_PTERM
6479 "mouse_pterm",
6480# endif
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01006481# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006482 "mouse_sgr",
6483# endif
6484# ifdef FEAT_SYSMOUSE
6485 "mouse_sysmouse",
6486# endif
6487# ifdef FEAT_MOUSE_URXVT
6488 "mouse_urxvt",
6489# endif
6490# ifdef FEAT_MOUSE_XTERM
6491 "mouse_xterm",
6492# endif
6493#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006494 "multi_byte",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006495#ifdef FEAT_MBYTE_IME
6496 "multi_byte_ime",
6497#endif
6498#ifdef FEAT_MULTI_LANG
6499 "multi_lang",
6500#endif
6501#ifdef FEAT_MZSCHEME
6502#ifndef DYNAMIC_MZSCHEME
6503 "mzscheme",
6504#endif
6505#endif
6506#ifdef FEAT_NUM64
6507 "num64",
6508#endif
6509#ifdef FEAT_OLE
6510 "ole",
6511#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006512#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006513 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006514#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006515#ifdef FEAT_PATH_EXTRA
6516 "path_extra",
6517#endif
6518#ifdef FEAT_PERL
6519#ifndef DYNAMIC_PERL
6520 "perl",
6521#endif
6522#endif
6523#ifdef FEAT_PERSISTENT_UNDO
6524 "persistent_undo",
6525#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006526#if defined(FEAT_PYTHON)
6527 "python_compiled",
6528# if defined(DYNAMIC_PYTHON)
6529 "python_dynamic",
6530# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006531 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006532 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006533# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006534#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006535#if defined(FEAT_PYTHON3)
6536 "python3_compiled",
6537# if defined(DYNAMIC_PYTHON3)
6538 "python3_dynamic",
6539# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006540 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006541 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006542# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006543#endif
6544#ifdef FEAT_POSTSCRIPT
6545 "postscript",
6546#endif
6547#ifdef FEAT_PRINTER
6548 "printer",
6549#endif
6550#ifdef FEAT_PROFILE
6551 "profile",
6552#endif
6553#ifdef FEAT_RELTIME
6554 "reltime",
6555#endif
6556#ifdef FEAT_QUICKFIX
6557 "quickfix",
6558#endif
6559#ifdef FEAT_RIGHTLEFT
6560 "rightleft",
6561#endif
6562#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6563 "ruby",
6564#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006565 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006566#ifdef FEAT_CMDL_INFO
6567 "showcmd",
6568 "cmdline_info",
6569#endif
6570#ifdef FEAT_SIGNS
6571 "signs",
6572#endif
6573#ifdef FEAT_SMARTINDENT
6574 "smartindent",
6575#endif
6576#ifdef STARTUPTIME
6577 "startuptime",
6578#endif
6579#ifdef FEAT_STL_OPT
6580 "statusline",
6581#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006582#ifdef FEAT_NETBEANS_INTG
6583 "netbeans_intg",
6584#endif
6585#ifdef FEAT_SPELL
6586 "spell",
6587#endif
6588#ifdef FEAT_SYN_HL
6589 "syntax",
6590#endif
6591#if defined(USE_SYSTEM) || !defined(UNIX)
6592 "system",
6593#endif
6594#ifdef FEAT_TAG_BINS
6595 "tag_binary",
6596#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006597#ifdef FEAT_TCL
6598# ifndef DYNAMIC_TCL
6599 "tcl",
6600# endif
6601#endif
6602#ifdef FEAT_TERMGUICOLORS
6603 "termguicolors",
6604#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006605#if defined(FEAT_TERMINAL) && !defined(MSWIN)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006606 "terminal",
6607#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006608#ifdef TERMINFO
6609 "terminfo",
6610#endif
6611#ifdef FEAT_TERMRESPONSE
6612 "termresponse",
6613#endif
6614#ifdef FEAT_TEXTOBJ
6615 "textobjects",
6616#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006617#ifdef FEAT_TEXT_PROP
6618 "textprop",
6619#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006620#ifdef HAVE_TGETENT
6621 "tgetent",
6622#endif
6623#ifdef FEAT_TIMERS
6624 "timers",
6625#endif
6626#ifdef FEAT_TITLE
6627 "title",
6628#endif
6629#ifdef FEAT_TOOLBAR
6630 "toolbar",
6631#endif
6632#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6633 "unnamedplus",
6634#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006635 "user-commands", /* was accidentally included in 5.4 */
6636 "user_commands",
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006637#ifdef FEAT_VARTABS
6638 "vartabs",
6639#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006640 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006641#ifdef FEAT_VIMINFO
6642 "viminfo",
6643#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006644 "vimscript-1",
6645 "vimscript-2",
Bram Moolenaar93a48792019-04-20 21:54:28 +02006646 "vimscript-3",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006647 "virtualedit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006648 "visual",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006649 "visualextra",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006650 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006651#ifdef FEAT_VTP
6652 "vtp",
6653#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006654#ifdef FEAT_WILDIGN
6655 "wildignore",
6656#endif
6657#ifdef FEAT_WILDMENU
6658 "wildmenu",
6659#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006660 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006661#ifdef FEAT_WAK
6662 "winaltkeys",
6663#endif
6664#ifdef FEAT_WRITEBACKUP
6665 "writebackup",
6666#endif
6667#ifdef FEAT_XIM
6668 "xim",
6669#endif
6670#ifdef FEAT_XFONTSET
6671 "xfontset",
6672#endif
6673#ifdef FEAT_XPM_W32
6674 "xpm",
6675 "xpm_w32", /* for backward compatibility */
6676#else
6677# if defined(HAVE_XPM)
6678 "xpm",
6679# endif
6680#endif
6681#ifdef USE_XSMP
6682 "xsmp",
6683#endif
6684#ifdef USE_XSMP_INTERACT
6685 "xsmp_interact",
6686#endif
6687#ifdef FEAT_XCLIPBOARD
6688 "xterm_clipboard",
6689#endif
6690#ifdef FEAT_XTERM_SAVE
6691 "xterm_save",
6692#endif
6693#if defined(UNIX) && defined(FEAT_X11)
6694 "X11",
6695#endif
6696 NULL
6697 };
6698
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006699 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006700 for (i = 0; has_list[i] != NULL; ++i)
6701 if (STRICMP(name, has_list[i]) == 0)
6702 {
6703 n = TRUE;
6704 break;
6705 }
6706
6707 if (n == FALSE)
6708 {
6709 if (STRNICMP(name, "patch", 5) == 0)
6710 {
6711 if (name[5] == '-'
6712 && STRLEN(name) >= 11
6713 && vim_isdigit(name[6])
6714 && vim_isdigit(name[8])
6715 && vim_isdigit(name[10]))
6716 {
6717 int major = atoi((char *)name + 6);
6718 int minor = atoi((char *)name + 8);
6719
6720 /* Expect "patch-9.9.01234". */
6721 n = (major < VIM_VERSION_MAJOR
6722 || (major == VIM_VERSION_MAJOR
6723 && (minor < VIM_VERSION_MINOR
6724 || (minor == VIM_VERSION_MINOR
6725 && has_patch(atoi((char *)name + 10))))));
6726 }
6727 else
6728 n = has_patch(atoi((char *)name + 5));
6729 }
6730 else if (STRICMP(name, "vim_starting") == 0)
6731 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006732 else if (STRICMP(name, "ttyin") == 0)
6733 n = mch_input_isatty();
6734 else if (STRICMP(name, "ttyout") == 0)
6735 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006736 else if (STRICMP(name, "multi_byte_encoding") == 0)
6737 n = has_mbyte;
Bram Moolenaar4f974752019-02-17 17:44:42 +01006738#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006739 else if (STRICMP(name, "balloon_multiline") == 0)
6740 n = multiline_balloon_available();
6741#endif
6742#ifdef DYNAMIC_TCL
6743 else if (STRICMP(name, "tcl") == 0)
6744 n = tcl_enabled(FALSE);
6745#endif
6746#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6747 else if (STRICMP(name, "iconv") == 0)
6748 n = iconv_enabled(FALSE);
6749#endif
6750#ifdef DYNAMIC_LUA
6751 else if (STRICMP(name, "lua") == 0)
6752 n = lua_enabled(FALSE);
6753#endif
6754#ifdef DYNAMIC_MZSCHEME
6755 else if (STRICMP(name, "mzscheme") == 0)
6756 n = mzscheme_enabled(FALSE);
6757#endif
6758#ifdef DYNAMIC_RUBY
6759 else if (STRICMP(name, "ruby") == 0)
6760 n = ruby_enabled(FALSE);
6761#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006762#ifdef DYNAMIC_PYTHON
6763 else if (STRICMP(name, "python") == 0)
6764 n = python_enabled(FALSE);
6765#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006766#ifdef DYNAMIC_PYTHON3
6767 else if (STRICMP(name, "python3") == 0)
6768 n = python3_enabled(FALSE);
6769#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006770#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6771 else if (STRICMP(name, "pythonx") == 0)
6772 {
6773# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6774 if (p_pyx == 0)
6775 n = python3_enabled(FALSE) || python_enabled(FALSE);
6776 else if (p_pyx == 3)
6777 n = python3_enabled(FALSE);
6778 else if (p_pyx == 2)
6779 n = python_enabled(FALSE);
6780# elif defined(DYNAMIC_PYTHON)
6781 n = python_enabled(FALSE);
6782# elif defined(DYNAMIC_PYTHON3)
6783 n = python3_enabled(FALSE);
6784# endif
6785 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006786#endif
6787#ifdef DYNAMIC_PERL
6788 else if (STRICMP(name, "perl") == 0)
6789 n = perl_enabled(FALSE);
6790#endif
6791#ifdef FEAT_GUI
6792 else if (STRICMP(name, "gui_running") == 0)
6793 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006794# ifdef FEAT_BROWSE
6795 else if (STRICMP(name, "browse") == 0)
6796 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6797# endif
6798#endif
6799#ifdef FEAT_SYN_HL
6800 else if (STRICMP(name, "syntax_items") == 0)
6801 n = syntax_present(curwin);
6802#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006803#ifdef FEAT_VTP
6804 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02006805 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006806#endif
6807#ifdef FEAT_NETBEANS_INTG
6808 else if (STRICMP(name, "netbeans_enabled") == 0)
6809 n = netbeans_active();
6810#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006811#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02006812 else if (STRICMP(name, "terminal") == 0)
6813 n = terminal_enabled();
6814#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006815#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006816 else if (STRICMP(name, "conpty") == 0)
6817 n = use_conpty();
6818#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006819 }
6820
6821 rettv->vval.v_number = n;
6822}
6823
6824/*
6825 * "has_key()" function
6826 */
6827 static void
6828f_has_key(typval_T *argvars, typval_T *rettv)
6829{
6830 if (argvars[0].v_type != VAR_DICT)
6831 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006832 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006833 return;
6834 }
6835 if (argvars[0].vval.v_dict == NULL)
6836 return;
6837
6838 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006839 tv_get_string(&argvars[1]), -1) != NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006840}
6841
6842/*
6843 * "haslocaldir()" function
6844 */
6845 static void
6846f_haslocaldir(typval_T *argvars, typval_T *rettv)
6847{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006848 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006849 win_T *wp = NULL;
6850
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006851 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
6852
6853 // Check for window-local and tab-local directories
6854 if (wp != NULL && wp->w_localdir != NULL)
6855 rettv->vval.v_number = 1;
6856 else if (tp != NULL && tp->tp_localdir != NULL)
6857 rettv->vval.v_number = 2;
6858 else
6859 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006860}
6861
6862/*
6863 * "hasmapto()" function
6864 */
6865 static void
6866f_hasmapto(typval_T *argvars, typval_T *rettv)
6867{
6868 char_u *name;
6869 char_u *mode;
6870 char_u buf[NUMBUFLEN];
6871 int abbr = FALSE;
6872
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006873 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006874 if (argvars[1].v_type == VAR_UNKNOWN)
6875 mode = (char_u *)"nvo";
6876 else
6877 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006878 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006879 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006880 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006881 }
6882
6883 if (map_to_exists(name, mode, abbr))
6884 rettv->vval.v_number = TRUE;
6885 else
6886 rettv->vval.v_number = FALSE;
6887}
6888
6889/*
6890 * "histadd()" function
6891 */
6892 static void
6893f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6894{
6895#ifdef FEAT_CMDHIST
6896 int histype;
6897 char_u *str;
6898 char_u buf[NUMBUFLEN];
6899#endif
6900
6901 rettv->vval.v_number = FALSE;
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006902 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006903 return;
6904#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006905 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006906 histype = str != NULL ? get_histtype(str) : -1;
6907 if (histype >= 0)
6908 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006909 str = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006910 if (*str != NUL)
6911 {
6912 init_history();
6913 add_to_history(histype, str, FALSE, NUL);
6914 rettv->vval.v_number = TRUE;
6915 return;
6916 }
6917 }
6918#endif
6919}
6920
6921/*
6922 * "histdel()" function
6923 */
6924 static void
6925f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6926{
6927#ifdef FEAT_CMDHIST
6928 int n;
6929 char_u buf[NUMBUFLEN];
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 n = 0;
6935 else if (argvars[1].v_type == VAR_UNKNOWN)
6936 /* only one argument: clear entire history */
6937 n = clr_history(get_histtype(str));
6938 else if (argvars[1].v_type == VAR_NUMBER)
6939 /* index given: remove that entry */
6940 n = del_history_idx(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006941 (int)tv_get_number(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006942 else
6943 /* string given: remove all matching entries */
6944 n = del_history_entry(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006945 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006946 rettv->vval.v_number = n;
6947#endif
6948}
6949
6950/*
6951 * "histget()" function
6952 */
6953 static void
6954f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6955{
6956#ifdef FEAT_CMDHIST
6957 int type;
6958 int idx;
6959 char_u *str;
6960
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006961 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006962 if (str == NULL)
6963 rettv->vval.v_string = NULL;
6964 else
6965 {
6966 type = get_histtype(str);
6967 if (argvars[1].v_type == VAR_UNKNOWN)
6968 idx = get_history_idx(type);
6969 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006970 idx = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006971 /* -1 on type error */
6972 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6973 }
6974#else
6975 rettv->vval.v_string = NULL;
6976#endif
6977 rettv->v_type = VAR_STRING;
6978}
6979
6980/*
6981 * "histnr()" function
6982 */
6983 static void
6984f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6985{
6986 int i;
6987
6988#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006989 char_u *history = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006990
6991 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6992 if (i >= HIST_CMD && i < HIST_COUNT)
6993 i = get_history_idx(i);
6994 else
6995#endif
6996 i = -1;
6997 rettv->vval.v_number = i;
6998}
6999
7000/*
7001 * "highlightID(name)" function
7002 */
7003 static void
7004f_hlID(typval_T *argvars, typval_T *rettv)
7005{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007006 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007007}
7008
7009/*
7010 * "highlight_exists()" function
7011 */
7012 static void
7013f_hlexists(typval_T *argvars, typval_T *rettv)
7014{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007015 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007016}
7017
7018/*
7019 * "hostname()" function
7020 */
7021 static void
7022f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
7023{
7024 char_u hostname[256];
7025
7026 mch_get_host_name(hostname, 256);
7027 rettv->v_type = VAR_STRING;
7028 rettv->vval.v_string = vim_strsave(hostname);
7029}
7030
7031/*
7032 * iconv() function
7033 */
7034 static void
7035f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
7036{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007037 char_u buf1[NUMBUFLEN];
7038 char_u buf2[NUMBUFLEN];
7039 char_u *from, *to, *str;
7040 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007041
7042 rettv->v_type = VAR_STRING;
7043 rettv->vval.v_string = NULL;
7044
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007045 str = tv_get_string(&argvars[0]);
7046 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
7047 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007048 vimconv.vc_type = CONV_NONE;
7049 convert_setup(&vimconv, from, to);
7050
7051 /* If the encodings are equal, no conversion needed. */
7052 if (vimconv.vc_type == CONV_NONE)
7053 rettv->vval.v_string = vim_strsave(str);
7054 else
7055 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
7056
7057 convert_setup(&vimconv, NULL, NULL);
7058 vim_free(from);
7059 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007060}
7061
7062/*
7063 * "indent()" function
7064 */
7065 static void
7066f_indent(typval_T *argvars, typval_T *rettv)
7067{
7068 linenr_T lnum;
7069
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007070 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007071 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7072 rettv->vval.v_number = get_indent_lnum(lnum);
7073 else
7074 rettv->vval.v_number = -1;
7075}
7076
7077/*
7078 * "index()" function
7079 */
7080 static void
7081f_index(typval_T *argvars, typval_T *rettv)
7082{
7083 list_T *l;
7084 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007085 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007086 long idx = 0;
7087 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007088 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007089
7090 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007091 if (argvars[0].v_type == VAR_BLOB)
7092 {
7093 typval_T tv;
7094 int start = 0;
7095
7096 if (argvars[2].v_type != VAR_UNKNOWN)
7097 {
7098 start = tv_get_number_chk(&argvars[2], &error);
7099 if (error)
7100 return;
7101 }
7102 b = argvars[0].vval.v_blob;
7103 if (b == NULL)
7104 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01007105 if (start < 0)
7106 {
7107 start = blob_len(b) + start;
7108 if (start < 0)
7109 start = 0;
7110 }
7111
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007112 for (idx = start; idx < blob_len(b); ++idx)
7113 {
7114 tv.v_type = VAR_NUMBER;
7115 tv.vval.v_number = blob_get(b, idx);
7116 if (tv_equal(&tv, &argvars[1], ic, FALSE))
7117 {
7118 rettv->vval.v_number = idx;
7119 return;
7120 }
7121 }
7122 return;
7123 }
7124 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007125 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007126 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007127 return;
7128 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007129
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007130 l = argvars[0].vval.v_list;
7131 if (l != NULL)
7132 {
7133 item = l->lv_first;
7134 if (argvars[2].v_type != VAR_UNKNOWN)
7135 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007136 /* Start at specified item. Use the cached index that list_find()
7137 * sets, so that a negative number also works. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007138 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007139 idx = l->lv_idx;
7140 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007141 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007142 if (error)
7143 item = NULL;
7144 }
7145
7146 for ( ; item != NULL; item = item->li_next, ++idx)
7147 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
7148 {
7149 rettv->vval.v_number = idx;
7150 break;
7151 }
7152 }
7153}
7154
7155static int inputsecret_flag = 0;
7156
7157/*
7158 * "input()" function
7159 * Also handles inputsecret() when inputsecret is set.
7160 */
7161 static void
7162f_input(typval_T *argvars, typval_T *rettv)
7163{
7164 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
7165}
7166
7167/*
7168 * "inputdialog()" function
7169 */
7170 static void
7171f_inputdialog(typval_T *argvars, typval_T *rettv)
7172{
7173#if defined(FEAT_GUI_TEXTDIALOG)
7174 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
7175 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
7176 {
7177 char_u *message;
7178 char_u buf[NUMBUFLEN];
7179 char_u *defstr = (char_u *)"";
7180
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007181 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007182 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007183 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007184 vim_strncpy(IObuff, defstr, IOSIZE - 1);
7185 else
7186 IObuff[0] = NUL;
7187 if (message != NULL && defstr != NULL
7188 && do_dialog(VIM_QUESTION, NULL, message,
7189 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
7190 rettv->vval.v_string = vim_strsave(IObuff);
7191 else
7192 {
7193 if (message != NULL && defstr != NULL
7194 && argvars[1].v_type != VAR_UNKNOWN
7195 && argvars[2].v_type != VAR_UNKNOWN)
7196 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007197 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007198 else
7199 rettv->vval.v_string = NULL;
7200 }
7201 rettv->v_type = VAR_STRING;
7202 }
7203 else
7204#endif
7205 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
7206}
7207
7208/*
7209 * "inputlist()" function
7210 */
7211 static void
7212f_inputlist(typval_T *argvars, typval_T *rettv)
7213{
7214 listitem_T *li;
7215 int selected;
7216 int mouse_used;
7217
7218#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007219 /* While starting up, there is no place to enter text. When running tests
7220 * with --not-a-term we assume feedkeys() will be used. */
7221 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007222 return;
7223#endif
7224 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
7225 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007226 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007227 return;
7228 }
7229
7230 msg_start();
7231 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
7232 lines_left = Rows; /* avoid more prompt */
7233 msg_scroll = TRUE;
7234 msg_clr_eos();
7235
7236 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
7237 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007238 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007239 msg_putchar('\n');
7240 }
7241
7242 /* Ask for choice. */
7243 selected = prompt_for_number(&mouse_used);
7244 if (mouse_used)
7245 selected -= lines_left;
7246
7247 rettv->vval.v_number = selected;
7248}
7249
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007250static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7251
7252/*
7253 * "inputrestore()" function
7254 */
7255 static void
7256f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7257{
7258 if (ga_userinput.ga_len > 0)
7259 {
7260 --ga_userinput.ga_len;
7261 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7262 + ga_userinput.ga_len);
7263 /* default return is zero == OK */
7264 }
7265 else if (p_verbose > 1)
7266 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007267 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007268 rettv->vval.v_number = 1; /* Failed */
7269 }
7270}
7271
7272/*
7273 * "inputsave()" function
7274 */
7275 static void
7276f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7277{
7278 /* Add an entry to the stack of typeahead storage. */
7279 if (ga_grow(&ga_userinput, 1) == OK)
7280 {
7281 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7282 + ga_userinput.ga_len);
7283 ++ga_userinput.ga_len;
7284 /* default return is zero == OK */
7285 }
7286 else
7287 rettv->vval.v_number = 1; /* Failed */
7288}
7289
7290/*
7291 * "inputsecret()" function
7292 */
7293 static void
7294f_inputsecret(typval_T *argvars, typval_T *rettv)
7295{
7296 ++cmdline_star;
7297 ++inputsecret_flag;
7298 f_input(argvars, rettv);
7299 --cmdline_star;
7300 --inputsecret_flag;
7301}
7302
7303/*
7304 * "insert()" function
7305 */
7306 static void
7307f_insert(typval_T *argvars, typval_T *rettv)
7308{
7309 long before = 0;
7310 listitem_T *item;
7311 list_T *l;
7312 int error = FALSE;
7313
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007314 if (argvars[0].v_type == VAR_BLOB)
7315 {
7316 int val, len;
7317 char_u *p;
7318
7319 len = blob_len(argvars[0].vval.v_blob);
7320 if (argvars[2].v_type != VAR_UNKNOWN)
7321 {
7322 before = (long)tv_get_number_chk(&argvars[2], &error);
7323 if (error)
7324 return; // type error; errmsg already given
7325 if (before < 0 || before > len)
7326 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007327 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007328 return;
7329 }
7330 }
7331 val = tv_get_number_chk(&argvars[1], &error);
7332 if (error)
7333 return;
7334 if (val < 0 || val > 255)
7335 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007336 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007337 return;
7338 }
7339
7340 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
7341 return;
7342 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
7343 mch_memmove(p + before + 1, p + before, (size_t)len - before);
7344 *(p + before) = val;
7345 ++argvars[0].vval.v_blob->bv_ga.ga_len;
7346
7347 copy_tv(&argvars[0], rettv);
7348 }
7349 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007350 semsg(_(e_listblobarg), "insert()");
Bram Moolenaar05c00c02019-02-11 22:00:11 +01007351 else if ((l = argvars[0].vval.v_list) != NULL
7352 && !var_check_lock(l->lv_lock,
7353 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007354 {
7355 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007356 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007357 if (error)
7358 return; /* type error; errmsg already given */
7359
7360 if (before == l->lv_len)
7361 item = NULL;
7362 else
7363 {
7364 item = list_find(l, before);
7365 if (item == NULL)
7366 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007367 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007368 l = NULL;
7369 }
7370 }
7371 if (l != NULL)
7372 {
7373 list_insert_tv(l, &argvars[1], item);
7374 copy_tv(&argvars[0], rettv);
7375 }
7376 }
7377}
7378
7379/*
7380 * "invert(expr)" function
7381 */
7382 static void
7383f_invert(typval_T *argvars, typval_T *rettv)
7384{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007385 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007386}
7387
7388/*
7389 * "isdirectory()" function
7390 */
7391 static void
7392f_isdirectory(typval_T *argvars, typval_T *rettv)
7393{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007394 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007395}
7396
7397/*
7398 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7399 * or it refers to a List or Dictionary that is locked.
7400 */
7401 static int
7402tv_islocked(typval_T *tv)
7403{
7404 return (tv->v_lock & VAR_LOCKED)
7405 || (tv->v_type == VAR_LIST
7406 && tv->vval.v_list != NULL
7407 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7408 || (tv->v_type == VAR_DICT
7409 && tv->vval.v_dict != NULL
7410 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7411}
7412
7413/*
7414 * "islocked()" function
7415 */
7416 static void
7417f_islocked(typval_T *argvars, typval_T *rettv)
7418{
7419 lval_T lv;
7420 char_u *end;
7421 dictitem_T *di;
7422
7423 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007424 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007425 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007426 if (end != NULL && lv.ll_name != NULL)
7427 {
7428 if (*end != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007429 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007430 else
7431 {
7432 if (lv.ll_tv == NULL)
7433 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007434 di = find_var(lv.ll_name, NULL, TRUE);
7435 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007436 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007437 /* Consider a variable locked when:
7438 * 1. the variable itself is locked
7439 * 2. the value of the variable is locked.
7440 * 3. the List or Dict value is locked.
7441 */
7442 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7443 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007444 }
7445 }
7446 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007447 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007448 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007449 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007450 else if (lv.ll_list != NULL)
7451 /* List item. */
7452 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7453 else
7454 /* Dictionary item. */
7455 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7456 }
7457 }
7458
7459 clear_lval(&lv);
7460}
7461
7462#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7463/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02007464 * "isinf()" function
7465 */
7466 static void
7467f_isinf(typval_T *argvars, typval_T *rettv)
7468{
7469 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
7470 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
7471}
7472
7473/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007474 * "isnan()" function
7475 */
7476 static void
7477f_isnan(typval_T *argvars, typval_T *rettv)
7478{
7479 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7480 && isnan(argvars[0].vval.v_float);
7481}
7482#endif
7483
7484/*
7485 * "items(dict)" function
7486 */
7487 static void
7488f_items(typval_T *argvars, typval_T *rettv)
7489{
7490 dict_list(argvars, rettv, 2);
7491}
7492
7493#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
7494/*
7495 * Get the job from the argument.
7496 * Returns NULL if the job is invalid.
7497 */
7498 static job_T *
7499get_job_arg(typval_T *tv)
7500{
7501 job_T *job;
7502
7503 if (tv->v_type != VAR_JOB)
7504 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007505 semsg(_(e_invarg2), tv_get_string(tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007506 return NULL;
7507 }
7508 job = tv->vval.v_job;
7509
7510 if (job == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007511 emsg(_("E916: not a valid job"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007512 return job;
7513}
7514
7515/*
7516 * "job_getchannel()" function
7517 */
7518 static void
7519f_job_getchannel(typval_T *argvars, typval_T *rettv)
7520{
7521 job_T *job = get_job_arg(&argvars[0]);
7522
7523 if (job != NULL)
7524 {
7525 rettv->v_type = VAR_CHANNEL;
7526 rettv->vval.v_channel = job->jv_channel;
7527 if (job->jv_channel != NULL)
7528 ++job->jv_channel->ch_refcount;
7529 }
7530}
7531
7532/*
7533 * "job_info()" function
7534 */
7535 static void
7536f_job_info(typval_T *argvars, typval_T *rettv)
7537{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007538 if (argvars[0].v_type != VAR_UNKNOWN)
7539 {
7540 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007541
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007542 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7543 job_info(job, rettv->vval.v_dict);
7544 }
7545 else if (rettv_list_alloc(rettv) == OK)
7546 job_info_all(rettv->vval.v_list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007547}
7548
7549/*
7550 * "job_setoptions()" function
7551 */
7552 static void
7553f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7554{
7555 job_T *job = get_job_arg(&argvars[0]);
7556 jobopt_T opt;
7557
7558 if (job == NULL)
7559 return;
7560 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007561 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007562 job_set_options(job, &opt);
7563 free_job_options(&opt);
7564}
7565
7566/*
7567 * "job_start()" function
7568 */
7569 static void
7570f_job_start(typval_T *argvars, typval_T *rettv)
7571{
7572 rettv->v_type = VAR_JOB;
7573 if (check_restricted() || check_secure())
7574 return;
Bram Moolenaar493359e2018-06-12 20:25:52 +02007575 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007576}
7577
7578/*
7579 * "job_status()" function
7580 */
7581 static void
7582f_job_status(typval_T *argvars, typval_T *rettv)
7583{
7584 job_T *job = get_job_arg(&argvars[0]);
7585
7586 if (job != NULL)
7587 {
7588 rettv->v_type = VAR_STRING;
7589 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7590 }
7591}
7592
7593/*
7594 * "job_stop()" function
7595 */
7596 static void
7597f_job_stop(typval_T *argvars, typval_T *rettv)
7598{
7599 job_T *job = get_job_arg(&argvars[0]);
7600
7601 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007602 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007603}
7604#endif
7605
7606/*
7607 * "join()" function
7608 */
7609 static void
7610f_join(typval_T *argvars, typval_T *rettv)
7611{
7612 garray_T ga;
7613 char_u *sep;
7614
7615 if (argvars[0].v_type != VAR_LIST)
7616 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007617 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007618 return;
7619 }
7620 if (argvars[0].vval.v_list == NULL)
7621 return;
7622 if (argvars[1].v_type == VAR_UNKNOWN)
7623 sep = (char_u *)" ";
7624 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007625 sep = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007626
7627 rettv->v_type = VAR_STRING;
7628
7629 if (sep != NULL)
7630 {
7631 ga_init2(&ga, (int)sizeof(char), 80);
7632 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7633 ga_append(&ga, NUL);
7634 rettv->vval.v_string = (char_u *)ga.ga_data;
7635 }
7636 else
7637 rettv->vval.v_string = NULL;
7638}
7639
7640/*
7641 * "js_decode()" function
7642 */
7643 static void
7644f_js_decode(typval_T *argvars, typval_T *rettv)
7645{
7646 js_read_T reader;
7647
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007648 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007649 reader.js_fill = NULL;
7650 reader.js_used = 0;
7651 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007652 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007653}
7654
7655/*
7656 * "js_encode()" function
7657 */
7658 static void
7659f_js_encode(typval_T *argvars, typval_T *rettv)
7660{
7661 rettv->v_type = VAR_STRING;
7662 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7663}
7664
7665/*
7666 * "json_decode()" function
7667 */
7668 static void
7669f_json_decode(typval_T *argvars, typval_T *rettv)
7670{
7671 js_read_T reader;
7672
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007673 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007674 reader.js_fill = NULL;
7675 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007676 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007677}
7678
7679/*
7680 * "json_encode()" function
7681 */
7682 static void
7683f_json_encode(typval_T *argvars, typval_T *rettv)
7684{
7685 rettv->v_type = VAR_STRING;
7686 rettv->vval.v_string = json_encode(&argvars[0], 0);
7687}
7688
7689/*
7690 * "keys()" function
7691 */
7692 static void
7693f_keys(typval_T *argvars, typval_T *rettv)
7694{
7695 dict_list(argvars, rettv, 0);
7696}
7697
7698/*
7699 * "last_buffer_nr()" function.
7700 */
7701 static void
7702f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7703{
7704 int n = 0;
7705 buf_T *buf;
7706
Bram Moolenaar29323592016-07-24 22:04:11 +02007707 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007708 if (n < buf->b_fnum)
7709 n = buf->b_fnum;
7710
7711 rettv->vval.v_number = n;
7712}
7713
7714/*
7715 * "len()" function
7716 */
7717 static void
7718f_len(typval_T *argvars, typval_T *rettv)
7719{
7720 switch (argvars[0].v_type)
7721 {
7722 case VAR_STRING:
7723 case VAR_NUMBER:
7724 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007725 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007726 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007727 case VAR_BLOB:
7728 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
7729 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007730 case VAR_LIST:
7731 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7732 break;
7733 case VAR_DICT:
7734 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7735 break;
7736 case VAR_UNKNOWN:
7737 case VAR_SPECIAL:
7738 case VAR_FLOAT:
7739 case VAR_FUNC:
7740 case VAR_PARTIAL:
7741 case VAR_JOB:
7742 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007743 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007744 break;
7745 }
7746}
7747
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007748 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007749libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007750{
7751#ifdef FEAT_LIBCALL
7752 char_u *string_in;
7753 char_u **string_result;
7754 int nr_result;
7755#endif
7756
7757 rettv->v_type = type;
7758 if (type != VAR_NUMBER)
7759 rettv->vval.v_string = NULL;
7760
7761 if (check_restricted() || check_secure())
7762 return;
7763
7764#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007765 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007766 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7767 {
7768 string_in = NULL;
7769 if (argvars[2].v_type == VAR_STRING)
7770 string_in = argvars[2].vval.v_string;
7771 if (type == VAR_NUMBER)
7772 string_result = NULL;
7773 else
7774 string_result = &rettv->vval.v_string;
7775 if (mch_libcall(argvars[0].vval.v_string,
7776 argvars[1].vval.v_string,
7777 string_in,
7778 argvars[2].vval.v_number,
7779 string_result,
7780 &nr_result) == OK
7781 && type == VAR_NUMBER)
7782 rettv->vval.v_number = nr_result;
7783 }
7784#endif
7785}
7786
7787/*
7788 * "libcall()" function
7789 */
7790 static void
7791f_libcall(typval_T *argvars, typval_T *rettv)
7792{
7793 libcall_common(argvars, rettv, VAR_STRING);
7794}
7795
7796/*
7797 * "libcallnr()" function
7798 */
7799 static void
7800f_libcallnr(typval_T *argvars, typval_T *rettv)
7801{
7802 libcall_common(argvars, rettv, VAR_NUMBER);
7803}
7804
7805/*
7806 * "line(string)" function
7807 */
7808 static void
7809f_line(typval_T *argvars, typval_T *rettv)
7810{
7811 linenr_T lnum = 0;
7812 pos_T *fp;
7813 int fnum;
7814
7815 fp = var2fpos(&argvars[0], TRUE, &fnum);
7816 if (fp != NULL)
7817 lnum = fp->lnum;
7818 rettv->vval.v_number = lnum;
7819}
7820
7821/*
7822 * "line2byte(lnum)" function
7823 */
7824 static void
7825f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7826{
7827#ifndef FEAT_BYTEOFF
7828 rettv->vval.v_number = -1;
7829#else
7830 linenr_T lnum;
7831
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007832 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007833 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7834 rettv->vval.v_number = -1;
7835 else
7836 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7837 if (rettv->vval.v_number >= 0)
7838 ++rettv->vval.v_number;
7839#endif
7840}
7841
7842/*
7843 * "lispindent(lnum)" function
7844 */
7845 static void
7846f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7847{
7848#ifdef FEAT_LISP
7849 pos_T pos;
7850 linenr_T lnum;
7851
7852 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007853 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007854 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7855 {
7856 curwin->w_cursor.lnum = lnum;
7857 rettv->vval.v_number = get_lisp_indent();
7858 curwin->w_cursor = pos;
7859 }
7860 else
7861#endif
7862 rettv->vval.v_number = -1;
7863}
7864
7865/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02007866 * "list2str()" function
7867 */
7868 static void
7869f_list2str(typval_T *argvars, typval_T *rettv)
7870{
7871 list_T *l;
7872 listitem_T *li;
7873 garray_T ga;
7874 int utf8 = FALSE;
7875
7876 rettv->v_type = VAR_STRING;
7877 rettv->vval.v_string = NULL;
7878 if (argvars[0].v_type != VAR_LIST)
7879 {
7880 emsg(_(e_invarg));
7881 return;
7882 }
7883
7884 l = argvars[0].vval.v_list;
7885 if (l == NULL)
7886 return; // empty list results in empty string
7887
7888 if (argvars[1].v_type != VAR_UNKNOWN)
7889 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
7890
7891 ga_init2(&ga, 1, 80);
7892 if (has_mbyte || utf8)
7893 {
7894 char_u buf[MB_MAXBYTES + 1];
7895 int (*char2bytes)(int, char_u *);
7896
7897 if (utf8 || enc_utf8)
7898 char2bytes = utf_char2bytes;
7899 else
7900 char2bytes = mb_char2bytes;
7901
7902 for (li = l->lv_first; li != NULL; li = li->li_next)
7903 {
7904 buf[(*char2bytes)(tv_get_number(&li->li_tv), buf)] = NUL;
7905 ga_concat(&ga, buf);
7906 }
7907 ga_append(&ga, NUL);
7908 }
7909 else if (ga_grow(&ga, list_len(l) + 1) == OK)
7910 {
7911 for (li = l->lv_first; li != NULL; li = li->li_next)
7912 ga_append(&ga, tv_get_number(&li->li_tv));
7913 ga_append(&ga, NUL);
7914 }
7915
7916 rettv->v_type = VAR_STRING;
7917 rettv->vval.v_string = ga.ga_data;
7918}
7919
7920/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007921 * "localtime()" function
7922 */
7923 static void
7924f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7925{
7926 rettv->vval.v_number = (varnumber_T)time(NULL);
7927}
7928
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007929 static void
7930get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7931{
7932 char_u *keys;
7933 char_u *which;
7934 char_u buf[NUMBUFLEN];
7935 char_u *keys_buf = NULL;
7936 char_u *rhs;
7937 int mode;
7938 int abbr = FALSE;
7939 int get_dict = FALSE;
7940 mapblock_T *mp;
7941 int buffer_local;
7942
7943 /* return empty string for failure */
7944 rettv->v_type = VAR_STRING;
7945 rettv->vval.v_string = NULL;
7946
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007947 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007948 if (*keys == NUL)
7949 return;
7950
7951 if (argvars[1].v_type != VAR_UNKNOWN)
7952 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007953 which = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007954 if (argvars[2].v_type != VAR_UNKNOWN)
7955 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007956 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007957 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007958 get_dict = (int)tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007959 }
7960 }
7961 else
7962 which = (char_u *)"";
7963 if (which == NULL)
7964 return;
7965
7966 mode = get_map_mode(&which, 0);
7967
7968 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7969 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7970 vim_free(keys_buf);
7971
7972 if (!get_dict)
7973 {
7974 /* Return a string. */
7975 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02007976 {
7977 if (*rhs == NUL)
7978 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
7979 else
7980 rettv->vval.v_string = str2special_save(rhs, FALSE);
7981 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007982
7983 }
7984 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7985 {
7986 /* Return a dictionary. */
7987 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7988 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7989 dict_T *dict = rettv->vval.v_dict;
7990
Bram Moolenaare0be1672018-07-08 16:50:37 +02007991 dict_add_string(dict, "lhs", lhs);
7992 dict_add_string(dict, "rhs", mp->m_orig_str);
7993 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
7994 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
7995 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007996 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
7997 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02007998 dict_add_number(dict, "buffer", (long)buffer_local);
7999 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
8000 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008001
8002 vim_free(lhs);
8003 vim_free(mapmode);
8004 }
8005}
8006
8007#ifdef FEAT_FLOAT
8008/*
8009 * "log()" function
8010 */
8011 static void
8012f_log(typval_T *argvars, typval_T *rettv)
8013{
8014 float_T f = 0.0;
8015
8016 rettv->v_type = VAR_FLOAT;
8017 if (get_float_arg(argvars, &f) == OK)
8018 rettv->vval.v_float = log(f);
8019 else
8020 rettv->vval.v_float = 0.0;
8021}
8022
8023/*
8024 * "log10()" function
8025 */
8026 static void
8027f_log10(typval_T *argvars, typval_T *rettv)
8028{
8029 float_T f = 0.0;
8030
8031 rettv->v_type = VAR_FLOAT;
8032 if (get_float_arg(argvars, &f) == OK)
8033 rettv->vval.v_float = log10(f);
8034 else
8035 rettv->vval.v_float = 0.0;
8036}
8037#endif
8038
8039#ifdef FEAT_LUA
8040/*
8041 * "luaeval()" function
8042 */
8043 static void
8044f_luaeval(typval_T *argvars, typval_T *rettv)
8045{
8046 char_u *str;
8047 char_u buf[NUMBUFLEN];
8048
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008049 if (check_restricted() || check_secure())
8050 return;
8051
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008052 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008053 do_luaeval(str, argvars + 1, rettv);
8054}
8055#endif
8056
8057/*
8058 * "map()" function
8059 */
8060 static void
8061f_map(typval_T *argvars, typval_T *rettv)
8062{
8063 filter_map(argvars, rettv, TRUE);
8064}
8065
8066/*
8067 * "maparg()" function
8068 */
8069 static void
8070f_maparg(typval_T *argvars, typval_T *rettv)
8071{
8072 get_maparg(argvars, rettv, TRUE);
8073}
8074
8075/*
8076 * "mapcheck()" function
8077 */
8078 static void
8079f_mapcheck(typval_T *argvars, typval_T *rettv)
8080{
8081 get_maparg(argvars, rettv, FALSE);
8082}
8083
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008084typedef enum
8085{
8086 MATCH_END, /* matchend() */
8087 MATCH_MATCH, /* match() */
8088 MATCH_STR, /* matchstr() */
8089 MATCH_LIST, /* matchlist() */
8090 MATCH_POS /* matchstrpos() */
8091} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008092
8093 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008094find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008095{
8096 char_u *str = NULL;
8097 long len = 0;
8098 char_u *expr = NULL;
8099 char_u *pat;
8100 regmatch_T regmatch;
8101 char_u patbuf[NUMBUFLEN];
8102 char_u strbuf[NUMBUFLEN];
8103 char_u *save_cpo;
8104 long start = 0;
8105 long nth = 1;
8106 colnr_T startcol = 0;
8107 int match = 0;
8108 list_T *l = NULL;
8109 listitem_T *li = NULL;
8110 long idx = 0;
8111 char_u *tofree = NULL;
8112
8113 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
8114 save_cpo = p_cpo;
8115 p_cpo = (char_u *)"";
8116
8117 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008118 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008119 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008120 /* type MATCH_LIST: return empty list when there are no matches.
8121 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008122 if (rettv_list_alloc(rettv) == FAIL)
8123 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008124 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008125 && (list_append_string(rettv->vval.v_list,
8126 (char_u *)"", 0) == FAIL
8127 || list_append_number(rettv->vval.v_list,
8128 (varnumber_T)-1) == FAIL
8129 || list_append_number(rettv->vval.v_list,
8130 (varnumber_T)-1) == FAIL
8131 || list_append_number(rettv->vval.v_list,
8132 (varnumber_T)-1) == FAIL))
8133 {
8134 list_free(rettv->vval.v_list);
8135 rettv->vval.v_list = NULL;
8136 goto theend;
8137 }
8138 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008139 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008140 {
8141 rettv->v_type = VAR_STRING;
8142 rettv->vval.v_string = NULL;
8143 }
8144
8145 if (argvars[0].v_type == VAR_LIST)
8146 {
8147 if ((l = argvars[0].vval.v_list) == NULL)
8148 goto theend;
8149 li = l->lv_first;
8150 }
8151 else
8152 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008153 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008154 len = (long)STRLEN(str);
8155 }
8156
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008157 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008158 if (pat == NULL)
8159 goto theend;
8160
8161 if (argvars[2].v_type != VAR_UNKNOWN)
8162 {
8163 int error = FALSE;
8164
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008165 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008166 if (error)
8167 goto theend;
8168 if (l != NULL)
8169 {
8170 li = list_find(l, start);
8171 if (li == NULL)
8172 goto theend;
8173 idx = l->lv_idx; /* use the cached index */
8174 }
8175 else
8176 {
8177 if (start < 0)
8178 start = 0;
8179 if (start > len)
8180 goto theend;
8181 /* When "count" argument is there ignore matches before "start",
8182 * otherwise skip part of the string. Differs when pattern is "^"
8183 * or "\<". */
8184 if (argvars[3].v_type != VAR_UNKNOWN)
8185 startcol = start;
8186 else
8187 {
8188 str += start;
8189 len -= start;
8190 }
8191 }
8192
8193 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008194 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008195 if (error)
8196 goto theend;
8197 }
8198
8199 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8200 if (regmatch.regprog != NULL)
8201 {
8202 regmatch.rm_ic = p_ic;
8203
8204 for (;;)
8205 {
8206 if (l != NULL)
8207 {
8208 if (li == NULL)
8209 {
8210 match = FALSE;
8211 break;
8212 }
8213 vim_free(tofree);
8214 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
8215 if (str == NULL)
8216 break;
8217 }
8218
8219 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
8220
8221 if (match && --nth <= 0)
8222 break;
8223 if (l == NULL && !match)
8224 break;
8225
8226 /* Advance to just after the match. */
8227 if (l != NULL)
8228 {
8229 li = li->li_next;
8230 ++idx;
8231 }
8232 else
8233 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008234 startcol = (colnr_T)(regmatch.startp[0]
8235 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008236 if (startcol > (colnr_T)len
8237 || str + startcol <= regmatch.startp[0])
8238 {
8239 match = FALSE;
8240 break;
8241 }
8242 }
8243 }
8244
8245 if (match)
8246 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008247 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008248 {
8249 listitem_T *li1 = rettv->vval.v_list->lv_first;
8250 listitem_T *li2 = li1->li_next;
8251 listitem_T *li3 = li2->li_next;
8252 listitem_T *li4 = li3->li_next;
8253
8254 vim_free(li1->li_tv.vval.v_string);
8255 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
8256 (int)(regmatch.endp[0] - regmatch.startp[0]));
8257 li3->li_tv.vval.v_number =
8258 (varnumber_T)(regmatch.startp[0] - expr);
8259 li4->li_tv.vval.v_number =
8260 (varnumber_T)(regmatch.endp[0] - expr);
8261 if (l != NULL)
8262 li2->li_tv.vval.v_number = (varnumber_T)idx;
8263 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008264 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008265 {
8266 int i;
8267
8268 /* return list with matched string and submatches */
8269 for (i = 0; i < NSUBEXP; ++i)
8270 {
8271 if (regmatch.endp[i] == NULL)
8272 {
8273 if (list_append_string(rettv->vval.v_list,
8274 (char_u *)"", 0) == FAIL)
8275 break;
8276 }
8277 else if (list_append_string(rettv->vval.v_list,
8278 regmatch.startp[i],
8279 (int)(regmatch.endp[i] - regmatch.startp[i]))
8280 == FAIL)
8281 break;
8282 }
8283 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008284 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008285 {
8286 /* return matched string */
8287 if (l != NULL)
8288 copy_tv(&li->li_tv, rettv);
8289 else
8290 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
8291 (int)(regmatch.endp[0] - regmatch.startp[0]));
8292 }
8293 else if (l != NULL)
8294 rettv->vval.v_number = idx;
8295 else
8296 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008297 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008298 rettv->vval.v_number =
8299 (varnumber_T)(regmatch.startp[0] - str);
8300 else
8301 rettv->vval.v_number =
8302 (varnumber_T)(regmatch.endp[0] - str);
8303 rettv->vval.v_number += (varnumber_T)(str - expr);
8304 }
8305 }
8306 vim_regfree(regmatch.regprog);
8307 }
8308
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008309theend:
8310 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008311 /* matchstrpos() without a list: drop the second item. */
8312 listitem_remove(rettv->vval.v_list,
8313 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008314 vim_free(tofree);
8315 p_cpo = save_cpo;
8316}
8317
8318/*
8319 * "match()" function
8320 */
8321 static void
8322f_match(typval_T *argvars, typval_T *rettv)
8323{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008324 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008325}
8326
Bram Moolenaar95e51472018-07-28 16:55:56 +02008327#ifdef FEAT_SEARCH_EXTRA
8328 static int
8329matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
8330{
8331 dictitem_T *di;
8332
8333 if (tv->v_type != VAR_DICT)
8334 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008335 emsg(_(e_dictreq));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008336 return FAIL;
8337 }
8338
8339 if (dict_find(tv->vval.v_dict, (char_u *)"conceal", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01008340 *conceal_char = dict_get_string(tv->vval.v_dict,
Bram Moolenaar95e51472018-07-28 16:55:56 +02008341 (char_u *)"conceal", FALSE);
8342
8343 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
8344 {
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02008345 *win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008346 if (*win == NULL)
8347 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01008348 emsg(_(e_invalwindow));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008349 return FAIL;
8350 }
8351 }
8352
8353 return OK;
8354}
8355#endif
8356
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008357/*
8358 * "matchadd()" function
8359 */
8360 static void
8361f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8362{
8363#ifdef FEAT_SEARCH_EXTRA
8364 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008365 char_u *grp = tv_get_string_buf_chk(&argvars[0], buf); /* group */
8366 char_u *pat = tv_get_string_buf_chk(&argvars[1], buf); /* pattern */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008367 int prio = 10; /* default priority */
8368 int id = -1;
8369 int error = FALSE;
8370 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008371 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008372
8373 rettv->vval.v_number = -1;
8374
8375 if (grp == NULL || pat == NULL)
8376 return;
8377 if (argvars[2].v_type != VAR_UNKNOWN)
8378 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008379 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008380 if (argvars[3].v_type != VAR_UNKNOWN)
8381 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008382 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008383 if (argvars[4].v_type != VAR_UNKNOWN
8384 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8385 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008386 }
8387 }
8388 if (error == TRUE)
8389 return;
8390 if (id >= 1 && id <= 3)
8391 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008392 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008393 return;
8394 }
8395
Bram Moolenaar95e51472018-07-28 16:55:56 +02008396 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008397 conceal_char);
8398#endif
8399}
8400
8401/*
8402 * "matchaddpos()" function
8403 */
8404 static void
8405f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8406{
8407#ifdef FEAT_SEARCH_EXTRA
8408 char_u buf[NUMBUFLEN];
8409 char_u *group;
8410 int prio = 10;
8411 int id = -1;
8412 int error = FALSE;
8413 list_T *l;
8414 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008415 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008416
8417 rettv->vval.v_number = -1;
8418
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008419 group = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008420 if (group == NULL)
8421 return;
8422
8423 if (argvars[1].v_type != VAR_LIST)
8424 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008425 semsg(_(e_listarg), "matchaddpos()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008426 return;
8427 }
8428 l = argvars[1].vval.v_list;
8429 if (l == NULL)
8430 return;
8431
8432 if (argvars[2].v_type != VAR_UNKNOWN)
8433 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008434 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008435 if (argvars[3].v_type != VAR_UNKNOWN)
8436 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008437 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008438
8439 if (argvars[4].v_type != VAR_UNKNOWN
8440 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8441 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008442 }
8443 }
8444 if (error == TRUE)
8445 return;
8446
8447 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8448 if (id == 1 || id == 2)
8449 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008450 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008451 return;
8452 }
8453
Bram Moolenaar95e51472018-07-28 16:55:56 +02008454 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008455 conceal_char);
8456#endif
8457}
8458
8459/*
8460 * "matcharg()" function
8461 */
8462 static void
8463f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8464{
8465 if (rettv_list_alloc(rettv) == OK)
8466 {
8467#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008468 int id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008469 matchitem_T *m;
8470
8471 if (id >= 1 && id <= 3)
8472 {
8473 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8474 {
8475 list_append_string(rettv->vval.v_list,
8476 syn_id2name(m->hlg_id), -1);
8477 list_append_string(rettv->vval.v_list, m->pattern, -1);
8478 }
8479 else
8480 {
8481 list_append_string(rettv->vval.v_list, NULL, -1);
8482 list_append_string(rettv->vval.v_list, NULL, -1);
8483 }
8484 }
8485#endif
8486 }
8487}
8488
8489/*
8490 * "matchdelete()" function
8491 */
8492 static void
8493f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8494{
8495#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01008496 win_T *win = get_optional_window(argvars, 1);
8497
8498 if (win == NULL)
8499 rettv->vval.v_number = -1;
8500 else
8501 rettv->vval.v_number = match_delete(win,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008502 (int)tv_get_number(&argvars[0]), TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008503#endif
8504}
8505
8506/*
8507 * "matchend()" function
8508 */
8509 static void
8510f_matchend(typval_T *argvars, typval_T *rettv)
8511{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008512 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008513}
8514
8515/*
8516 * "matchlist()" function
8517 */
8518 static void
8519f_matchlist(typval_T *argvars, typval_T *rettv)
8520{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008521 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008522}
8523
8524/*
8525 * "matchstr()" function
8526 */
8527 static void
8528f_matchstr(typval_T *argvars, typval_T *rettv)
8529{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008530 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008531}
8532
8533/*
8534 * "matchstrpos()" function
8535 */
8536 static void
8537f_matchstrpos(typval_T *argvars, typval_T *rettv)
8538{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008539 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008540}
8541
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008542 static void
8543max_min(typval_T *argvars, typval_T *rettv, int domax)
8544{
8545 varnumber_T n = 0;
8546 varnumber_T i;
8547 int error = FALSE;
8548
8549 if (argvars[0].v_type == VAR_LIST)
8550 {
8551 list_T *l;
8552 listitem_T *li;
8553
8554 l = argvars[0].vval.v_list;
8555 if (l != NULL)
8556 {
8557 li = l->lv_first;
8558 if (li != NULL)
8559 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008560 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008561 for (;;)
8562 {
8563 li = li->li_next;
8564 if (li == NULL)
8565 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008566 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008567 if (domax ? i > n : i < n)
8568 n = i;
8569 }
8570 }
8571 }
8572 }
8573 else if (argvars[0].v_type == VAR_DICT)
8574 {
8575 dict_T *d;
8576 int first = TRUE;
8577 hashitem_T *hi;
8578 int todo;
8579
8580 d = argvars[0].vval.v_dict;
8581 if (d != NULL)
8582 {
8583 todo = (int)d->dv_hashtab.ht_used;
8584 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8585 {
8586 if (!HASHITEM_EMPTY(hi))
8587 {
8588 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008589 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008590 if (first)
8591 {
8592 n = i;
8593 first = FALSE;
8594 }
8595 else if (domax ? i > n : i < n)
8596 n = i;
8597 }
8598 }
8599 }
8600 }
8601 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008602 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008603 rettv->vval.v_number = error ? 0 : n;
8604}
8605
8606/*
8607 * "max()" function
8608 */
8609 static void
8610f_max(typval_T *argvars, typval_T *rettv)
8611{
8612 max_min(argvars, rettv, TRUE);
8613}
8614
8615/*
8616 * "min()" function
8617 */
8618 static void
8619f_min(typval_T *argvars, typval_T *rettv)
8620{
8621 max_min(argvars, rettv, FALSE);
8622}
8623
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008624/*
8625 * Create the directory in which "dir" is located, and higher levels when
8626 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008627 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008628 */
8629 static int
8630mkdir_recurse(char_u *dir, int prot)
8631{
8632 char_u *p;
8633 char_u *updir;
8634 int r = FAIL;
8635
8636 /* Get end of directory name in "dir".
8637 * We're done when it's "/" or "c:/". */
8638 p = gettail_sep(dir);
8639 if (p <= get_past_head(dir))
8640 return OK;
8641
8642 /* If the directory exists we're done. Otherwise: create it.*/
8643 updir = vim_strnsave(dir, (int)(p - dir));
8644 if (updir == NULL)
8645 return FAIL;
8646 if (mch_isdir(updir))
8647 r = OK;
8648 else if (mkdir_recurse(updir, prot) == OK)
8649 r = vim_mkdir_emsg(updir, prot);
8650 vim_free(updir);
8651 return r;
8652}
8653
8654#ifdef vim_mkdir
8655/*
8656 * "mkdir()" function
8657 */
8658 static void
8659f_mkdir(typval_T *argvars, typval_T *rettv)
8660{
8661 char_u *dir;
8662 char_u buf[NUMBUFLEN];
8663 int prot = 0755;
8664
8665 rettv->vval.v_number = FAIL;
8666 if (check_restricted() || check_secure())
8667 return;
8668
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008669 dir = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008670 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008671 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008672
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008673 if (*gettail(dir) == NUL)
8674 /* remove trailing slashes */
8675 *gettail_sep(dir) = NUL;
8676
8677 if (argvars[1].v_type != VAR_UNKNOWN)
8678 {
8679 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008680 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008681 prot = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008682 if (prot == -1)
8683 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008684 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008685 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008686 {
8687 if (mch_isdir(dir))
8688 {
8689 /* With the "p" flag it's OK if the dir already exists. */
8690 rettv->vval.v_number = OK;
8691 return;
8692 }
8693 mkdir_recurse(dir, prot);
8694 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008695 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008696 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008697}
8698#endif
8699
8700/*
8701 * "mode()" function
8702 */
8703 static void
8704f_mode(typval_T *argvars, typval_T *rettv)
8705{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008706 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008707
Bram Moolenaar612cc382018-07-29 15:34:26 +02008708 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008709
8710 if (time_for_testing == 93784)
8711 {
8712 /* Testing the two-character code. */
8713 buf[0] = 'x';
8714 buf[1] = '!';
8715 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008716#ifdef FEAT_TERMINAL
8717 else if (term_use_loop())
8718 buf[0] = 't';
8719#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008720 else if (VIsual_active)
8721 {
8722 if (VIsual_select)
8723 buf[0] = VIsual_mode + 's' - 'v';
8724 else
8725 buf[0] = VIsual_mode;
8726 }
8727 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8728 || State == CONFIRM)
8729 {
8730 buf[0] = 'r';
8731 if (State == ASKMORE)
8732 buf[1] = 'm';
8733 else if (State == CONFIRM)
8734 buf[1] = '?';
8735 }
8736 else if (State == EXTERNCMD)
8737 buf[0] = '!';
8738 else if (State & INSERT)
8739 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008740 if (State & VREPLACE_FLAG)
8741 {
8742 buf[0] = 'R';
8743 buf[1] = 'v';
8744 }
8745 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008746 {
8747 if (State & REPLACE_FLAG)
8748 buf[0] = 'R';
8749 else
8750 buf[0] = 'i';
8751#ifdef FEAT_INS_EXPAND
8752 if (ins_compl_active())
8753 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008754 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008755 buf[1] = 'x';
8756#endif
8757 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008758 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008759 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008760 {
8761 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008762 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008763 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008764 else if (exmode_active == EXMODE_NORMAL)
8765 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008766 }
8767 else
8768 {
8769 buf[0] = 'n';
8770 if (finish_op)
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008771 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008772 buf[1] = 'o';
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008773 // to be able to detect force-linewise/blockwise/characterwise operations
8774 buf[2] = motion_force;
8775 }
Bram Moolenaar612cc382018-07-29 15:34:26 +02008776 else if (restart_edit == 'I' || restart_edit == 'R'
8777 || restart_edit == 'V')
8778 {
8779 buf[1] = 'i';
8780 buf[2] = restart_edit;
8781 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008782 }
8783
8784 /* Clear out the minor mode when the argument is not a non-zero number or
8785 * non-empty string. */
8786 if (!non_zero_arg(&argvars[0]))
8787 buf[1] = NUL;
8788
8789 rettv->vval.v_string = vim_strsave(buf);
8790 rettv->v_type = VAR_STRING;
8791}
8792
8793#if defined(FEAT_MZSCHEME) || defined(PROTO)
8794/*
8795 * "mzeval()" function
8796 */
8797 static void
8798f_mzeval(typval_T *argvars, typval_T *rettv)
8799{
8800 char_u *str;
8801 char_u buf[NUMBUFLEN];
8802
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008803 if (check_restricted() || check_secure())
8804 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008805 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008806 do_mzeval(str, rettv);
8807}
8808
8809 void
8810mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8811{
8812 typval_T argvars[3];
8813
8814 argvars[0].v_type = VAR_STRING;
8815 argvars[0].vval.v_string = name;
8816 copy_tv(args, &argvars[1]);
8817 argvars[2].v_type = VAR_UNKNOWN;
8818 f_call(argvars, rettv);
8819 clear_tv(&argvars[1]);
8820}
8821#endif
8822
8823/*
8824 * "nextnonblank()" function
8825 */
8826 static void
8827f_nextnonblank(typval_T *argvars, typval_T *rettv)
8828{
8829 linenr_T lnum;
8830
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008831 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008832 {
8833 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8834 {
8835 lnum = 0;
8836 break;
8837 }
8838 if (*skipwhite(ml_get(lnum)) != NUL)
8839 break;
8840 }
8841 rettv->vval.v_number = lnum;
8842}
8843
8844/*
8845 * "nr2char()" function
8846 */
8847 static void
8848f_nr2char(typval_T *argvars, typval_T *rettv)
8849{
8850 char_u buf[NUMBUFLEN];
8851
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008852 if (has_mbyte)
8853 {
8854 int utf8 = 0;
8855
8856 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008857 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008858 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01008859 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008860 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008861 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008862 }
8863 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008864 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008865 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008866 buf[1] = NUL;
8867 }
8868 rettv->v_type = VAR_STRING;
8869 rettv->vval.v_string = vim_strsave(buf);
8870}
8871
8872/*
8873 * "or(expr, expr)" function
8874 */
8875 static void
8876f_or(typval_T *argvars, typval_T *rettv)
8877{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008878 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
8879 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008880}
8881
8882/*
8883 * "pathshorten()" function
8884 */
8885 static void
8886f_pathshorten(typval_T *argvars, typval_T *rettv)
8887{
8888 char_u *p;
8889
8890 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008891 p = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008892 if (p == NULL)
8893 rettv->vval.v_string = NULL;
8894 else
8895 {
8896 p = vim_strsave(p);
8897 rettv->vval.v_string = p;
8898 if (p != NULL)
8899 shorten_dir(p);
8900 }
8901}
8902
8903#ifdef FEAT_PERL
8904/*
8905 * "perleval()" function
8906 */
8907 static void
8908f_perleval(typval_T *argvars, typval_T *rettv)
8909{
8910 char_u *str;
8911 char_u buf[NUMBUFLEN];
8912
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008913 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008914 do_perleval(str, rettv);
8915}
8916#endif
8917
8918#ifdef FEAT_FLOAT
8919/*
8920 * "pow()" function
8921 */
8922 static void
8923f_pow(typval_T *argvars, typval_T *rettv)
8924{
8925 float_T fx = 0.0, fy = 0.0;
8926
8927 rettv->v_type = VAR_FLOAT;
8928 if (get_float_arg(argvars, &fx) == OK
8929 && get_float_arg(&argvars[1], &fy) == OK)
8930 rettv->vval.v_float = pow(fx, fy);
8931 else
8932 rettv->vval.v_float = 0.0;
8933}
8934#endif
8935
8936/*
8937 * "prevnonblank()" function
8938 */
8939 static void
8940f_prevnonblank(typval_T *argvars, typval_T *rettv)
8941{
8942 linenr_T lnum;
8943
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008944 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008945 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8946 lnum = 0;
8947 else
8948 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8949 --lnum;
8950 rettv->vval.v_number = lnum;
8951}
8952
8953/* This dummy va_list is here because:
8954 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8955 * - locally in the function results in a "used before set" warning
8956 * - using va_start() to initialize it gives "function with fixed args" error */
8957static va_list ap;
8958
8959/*
8960 * "printf()" function
8961 */
8962 static void
8963f_printf(typval_T *argvars, typval_T *rettv)
8964{
8965 char_u buf[NUMBUFLEN];
8966 int len;
8967 char_u *s;
8968 int saved_did_emsg = did_emsg;
8969 char *fmt;
8970
8971 rettv->v_type = VAR_STRING;
8972 rettv->vval.v_string = NULL;
8973
8974 /* Get the required length, allocate the buffer and do it for real. */
8975 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008976 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008977 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008978 if (!did_emsg)
8979 {
8980 s = alloc(len + 1);
8981 if (s != NULL)
8982 {
8983 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008984 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8985 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008986 }
8987 }
8988 did_emsg |= saved_did_emsg;
8989}
8990
Bram Moolenaarf2732452018-06-03 14:47:35 +02008991#ifdef FEAT_JOB_CHANNEL
8992/*
8993 * "prompt_setcallback({buffer}, {callback})" function
8994 */
8995 static void
8996f_prompt_setcallback(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 Moolenaarf2732452018-06-03 14:47:35 +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_callback, buf->b_prompt_partial);
9013 if (partial == NULL)
9014 buf->b_prompt_callback = vim_strsave(callback);
9015 else
9016 /* pointer into the partial */
9017 buf->b_prompt_callback = callback;
9018 buf->b_prompt_partial = partial;
9019}
9020
9021/*
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009022 * "prompt_setinterrupt({buffer}, {callback})" function
9023 */
9024 static void
9025f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
9026{
9027 buf_T *buf;
9028 char_u *callback;
9029 partial_T *partial;
9030
9031 if (check_secure())
9032 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009033 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009034 if (buf == NULL)
9035 return;
9036
9037 callback = get_callback(&argvars[1], &partial);
9038 if (callback == NULL)
9039 return;
9040
9041 free_callback(buf->b_prompt_interrupt, buf->b_prompt_int_partial);
9042 if (partial == NULL)
9043 buf->b_prompt_interrupt = vim_strsave(callback);
9044 else
9045 /* pointer into the partial */
9046 buf->b_prompt_interrupt = callback;
9047 buf->b_prompt_int_partial = partial;
9048}
9049
9050/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02009051 * "prompt_setprompt({buffer}, {text})" function
9052 */
9053 static void
9054f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
9055{
9056 buf_T *buf;
9057 char_u *text;
9058
9059 if (check_secure())
9060 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009061 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009062 if (buf == NULL)
9063 return;
9064
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009065 text = tv_get_string(&argvars[1]);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009066 vim_free(buf->b_prompt_text);
9067 buf->b_prompt_text = vim_strsave(text);
9068}
9069#endif
9070
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009071/*
9072 * "pumvisible()" function
9073 */
9074 static void
9075f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9076{
9077#ifdef FEAT_INS_EXPAND
9078 if (pum_visible())
9079 rettv->vval.v_number = 1;
9080#endif
9081}
9082
9083#ifdef FEAT_PYTHON3
9084/*
9085 * "py3eval()" function
9086 */
9087 static void
9088f_py3eval(typval_T *argvars, typval_T *rettv)
9089{
9090 char_u *str;
9091 char_u buf[NUMBUFLEN];
9092
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009093 if (check_restricted() || check_secure())
9094 return;
9095
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009096 if (p_pyx == 0)
9097 p_pyx = 3;
9098
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009099 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009100 do_py3eval(str, rettv);
9101}
9102#endif
9103
9104#ifdef FEAT_PYTHON
9105/*
9106 * "pyeval()" function
9107 */
9108 static void
9109f_pyeval(typval_T *argvars, typval_T *rettv)
9110{
9111 char_u *str;
9112 char_u buf[NUMBUFLEN];
9113
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009114 if (check_restricted() || check_secure())
9115 return;
9116
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009117 if (p_pyx == 0)
9118 p_pyx = 2;
9119
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009120 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009121 do_pyeval(str, rettv);
9122}
9123#endif
9124
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009125#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
9126/*
9127 * "pyxeval()" function
9128 */
9129 static void
9130f_pyxeval(typval_T *argvars, typval_T *rettv)
9131{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009132 if (check_restricted() || check_secure())
9133 return;
9134
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009135# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
9136 init_pyxversion();
9137 if (p_pyx == 2)
9138 f_pyeval(argvars, rettv);
9139 else
9140 f_py3eval(argvars, rettv);
9141# elif defined(FEAT_PYTHON)
9142 f_pyeval(argvars, rettv);
9143# elif defined(FEAT_PYTHON3)
9144 f_py3eval(argvars, rettv);
9145# endif
9146}
9147#endif
9148
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009149/*
9150 * "range()" function
9151 */
9152 static void
9153f_range(typval_T *argvars, typval_T *rettv)
9154{
9155 varnumber_T start;
9156 varnumber_T end;
9157 varnumber_T stride = 1;
9158 varnumber_T i;
9159 int error = FALSE;
9160
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009161 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009162 if (argvars[1].v_type == VAR_UNKNOWN)
9163 {
9164 end = start - 1;
9165 start = 0;
9166 }
9167 else
9168 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009169 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009170 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009171 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009172 }
9173
9174 if (error)
9175 return; /* type error; errmsg already given */
9176 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009177 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009178 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009179 emsg(_("E727: Start past end"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009180 else
9181 {
9182 if (rettv_list_alloc(rettv) == OK)
9183 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
9184 if (list_append_number(rettv->vval.v_list,
9185 (varnumber_T)i) == FAIL)
9186 break;
9187 }
9188}
9189
9190/*
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009191 * Evaluate "expr" for readdir().
9192 */
9193 static int
9194readdir_checkitem(typval_T *expr, char_u *name)
9195{
9196 typval_T save_val;
9197 typval_T rettv;
9198 typval_T argv[2];
9199 int retval = 0;
9200 int error = FALSE;
9201
9202 prepare_vimvar(VV_VAL, &save_val);
9203 set_vim_var_string(VV_VAL, name, -1);
9204 argv[0].v_type = VAR_STRING;
9205 argv[0].vval.v_string = name;
9206
9207 if (eval_expr_typval(expr, argv, 1, &rettv) == FAIL)
9208 goto theend;
9209
9210 retval = tv_get_number_chk(&rettv, &error);
9211 if (error)
9212 retval = -1;
9213 clear_tv(&rettv);
9214
9215theend:
9216 set_vim_var_string(VV_VAL, NULL, 0);
9217 restore_vimvar(VV_VAL, &save_val);
9218 return retval;
9219}
9220
9221/*
9222 * "readdir()" function
9223 */
9224 static void
9225f_readdir(typval_T *argvars, typval_T *rettv)
9226{
9227 typval_T *expr;
9228 int failed = FALSE;
9229 char_u *path;
9230 garray_T ga;
9231 int i;
9232#ifdef MSWIN
9233 char_u *buf, *p;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009234 int ok;
9235 HANDLE hFind = INVALID_HANDLE_VALUE;
9236 WIN32_FIND_DATAW wfb;
9237 WCHAR *wn = NULL; // UCS-2 name, NULL when not used.
9238#endif
9239
9240 if (rettv_list_alloc(rettv) == FAIL)
9241 return;
9242 path = tv_get_string(&argvars[0]);
9243 expr = &argvars[1];
9244 ga_init2(&ga, (int)sizeof(char *), 20);
9245
9246#ifdef MSWIN
9247 buf = alloc((int)MAXPATHL);
9248 if (buf == NULL)
9249 return;
9250 STRNCPY(buf, path, MAXPATHL-5);
9251 p = vim_strpbrk(path, (char_u *)"\\/");
9252 if (p != NULL)
9253 *p = NUL;
9254 STRCAT(buf, "\\*");
9255
9256 wn = enc_to_utf16(buf, NULL);
9257 if (wn != NULL)
9258 hFind = FindFirstFileW(wn, &wfb);
9259 ok = (hFind != INVALID_HANDLE_VALUE);
9260 if (!ok)
9261 smsg(_(e_notopen), path);
9262 else
9263 {
9264 while (ok)
9265 {
9266 int ignore;
9267
9268 p = utf16_to_enc(wfb.cFileName, NULL); // p is allocated here
9269 if (p == NULL)
9270 break; // out of memory
9271
9272 ignore = p[0] == '.' && (p[1] == NUL
9273 || (p[1] == '.' && p[2] == NUL));
9274 if (!ignore && expr->v_type != VAR_UNKNOWN)
9275 {
9276 int r = readdir_checkitem(expr, p);
9277
9278 if (r < 0)
9279 {
9280 vim_free(p);
9281 break;
9282 }
9283 if (r == 0)
9284 ignore = TRUE;
9285 }
9286
9287 if (!ignore)
9288 {
9289 if (ga_grow(&ga, 1) == OK)
9290 ((char_u**)ga.ga_data)[ga.ga_len++] = vim_strsave(p);
9291 else
9292 {
9293 failed = TRUE;
9294 vim_free(p);
9295 break;
9296 }
9297 }
9298
9299 vim_free(p);
9300 ok = FindNextFileW(hFind, &wfb);
9301 }
9302 FindClose(hFind);
9303 }
9304
9305 vim_free(buf);
9306 vim_free(wn);
9307#else
9308 DIR *dirp;
9309 struct dirent *dp;
9310 char_u *p;
9311
9312 dirp = opendir((char *)path);
9313 if (dirp == NULL)
9314 smsg(_(e_notopen), path);
9315 else
9316 {
9317 for (;;)
9318 {
9319 int ignore;
9320
9321 dp = readdir(dirp);
9322 if (dp == NULL)
9323 break;
9324 p = (char_u *)dp->d_name;
9325
9326 ignore = p[0] == '.' &&
9327 (p[1] == NUL ||
9328 (p[1] == '.' && p[2] == NUL));
9329 if (!ignore && expr->v_type != VAR_UNKNOWN)
9330 {
9331 int r = readdir_checkitem(expr, p);
9332
9333 if (r < 0)
9334 break;
9335 if (r == 0)
9336 ignore = TRUE;
9337 }
9338
9339 if (!ignore)
9340 {
9341 if (ga_grow(&ga, 1) == OK)
9342 ((char_u**)ga.ga_data)[ga.ga_len++] = vim_strsave(p);
9343 else
9344 {
9345 failed = TRUE;
9346 break;
9347 }
9348 }
9349 }
9350
9351 closedir(dirp);
9352 }
9353#endif
9354
Bram Moolenaar334ad412019-04-19 15:20:46 +02009355 if (!failed && rettv->vval.v_list != NULL && ga.ga_len > 0)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009356 {
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009357 sort_strings((char_u **)ga.ga_data, ga.ga_len);
9358 for (i = 0; i < ga.ga_len; i++)
9359 {
9360 p = ((char_u **)ga.ga_data)[i];
9361 list_append_string(rettv->vval.v_list, p, -1);
9362 }
9363 }
Bram Moolenaar334ad412019-04-19 15:20:46 +02009364 ga_clear_strings(&ga);
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009365}
9366
9367/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009368 * "readfile()" function
9369 */
9370 static void
9371f_readfile(typval_T *argvars, typval_T *rettv)
9372{
9373 int binary = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009374 int blob = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009375 int failed = FALSE;
9376 char_u *fname;
9377 FILE *fd;
9378 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
9379 int io_size = sizeof(buf);
9380 int readlen; /* size of last fread() */
9381 char_u *prev = NULL; /* previously read bytes, if any */
9382 long prevlen = 0; /* length of data in prev */
9383 long prevsize = 0; /* size of prev buffer */
9384 long maxline = MAXLNUM;
9385 long cnt = 0;
9386 char_u *p; /* position in buf */
9387 char_u *start; /* start of current line */
9388
9389 if (argvars[1].v_type != VAR_UNKNOWN)
9390 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009391 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009392 binary = TRUE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009393 if (STRCMP(tv_get_string(&argvars[1]), "B") == 0)
9394 blob = TRUE;
9395
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009396 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009397 maxline = (long)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009398 }
9399
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009400 if (blob)
9401 {
9402 if (rettv_blob_alloc(rettv) == FAIL)
9403 return;
9404 }
9405 else
9406 {
9407 if (rettv_list_alloc(rettv) == FAIL)
9408 return;
9409 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009410
9411 /* Always open the file in binary mode, library functions have a mind of
9412 * their own about CR-LF conversion. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009413 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009414 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
9415 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009416 semsg(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009417 return;
9418 }
9419
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009420 if (blob)
9421 {
9422 if (read_blob(fd, rettv->vval.v_blob) == FAIL)
9423 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009424 emsg("cannot read file");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009425 blob_free(rettv->vval.v_blob);
9426 }
9427 fclose(fd);
9428 return;
9429 }
9430
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009431 while (cnt < maxline || maxline < 0)
9432 {
9433 readlen = (int)fread(buf, 1, io_size, fd);
9434
9435 /* This for loop processes what was read, but is also entered at end
9436 * of file so that either:
9437 * - an incomplete line gets written
9438 * - a "binary" file gets an empty line at the end if it ends in a
9439 * newline. */
9440 for (p = buf, start = buf;
9441 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
9442 ++p)
9443 {
9444 if (*p == '\n' || readlen <= 0)
9445 {
9446 listitem_T *li;
9447 char_u *s = NULL;
9448 long_u len = p - start;
9449
9450 /* Finished a line. Remove CRs before NL. */
9451 if (readlen > 0 && !binary)
9452 {
9453 while (len > 0 && start[len - 1] == '\r')
9454 --len;
9455 /* removal may cross back to the "prev" string */
9456 if (len == 0)
9457 while (prevlen > 0 && prev[prevlen - 1] == '\r')
9458 --prevlen;
9459 }
9460 if (prevlen == 0)
9461 s = vim_strnsave(start, (int)len);
9462 else
9463 {
9464 /* Change "prev" buffer to be the right size. This way
9465 * the bytes are only copied once, and very long lines are
9466 * allocated only once. */
9467 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
9468 {
9469 mch_memmove(s + prevlen, start, len);
9470 s[prevlen + len] = NUL;
9471 prev = NULL; /* the list will own the string */
9472 prevlen = prevsize = 0;
9473 }
9474 }
9475 if (s == NULL)
9476 {
9477 do_outofmem_msg((long_u) prevlen + len + 1);
9478 failed = TRUE;
9479 break;
9480 }
9481
9482 if ((li = listitem_alloc()) == NULL)
9483 {
9484 vim_free(s);
9485 failed = TRUE;
9486 break;
9487 }
9488 li->li_tv.v_type = VAR_STRING;
9489 li->li_tv.v_lock = 0;
9490 li->li_tv.vval.v_string = s;
9491 list_append(rettv->vval.v_list, li);
9492
9493 start = p + 1; /* step over newline */
9494 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
9495 break;
9496 }
9497 else if (*p == NUL)
9498 *p = '\n';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009499 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
9500 * when finding the BF and check the previous two bytes. */
9501 else if (*p == 0xbf && enc_utf8 && !binary)
9502 {
9503 /* Find the two bytes before the 0xbf. If p is at buf, or buf
9504 * + 1, these may be in the "prev" string. */
9505 char_u back1 = p >= buf + 1 ? p[-1]
9506 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
9507 char_u back2 = p >= buf + 2 ? p[-2]
9508 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
9509 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
9510
9511 if (back2 == 0xef && back1 == 0xbb)
9512 {
9513 char_u *dest = p - 2;
9514
9515 /* Usually a BOM is at the beginning of a file, and so at
9516 * the beginning of a line; then we can just step over it.
9517 */
9518 if (start == dest)
9519 start = p + 1;
9520 else
9521 {
9522 /* have to shuffle buf to close gap */
9523 int adjust_prevlen = 0;
9524
9525 if (dest < buf)
9526 {
9527 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
9528 dest = buf;
9529 }
9530 if (readlen > p - buf + 1)
9531 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
9532 readlen -= 3 - adjust_prevlen;
9533 prevlen -= adjust_prevlen;
9534 p = dest - 1;
9535 }
9536 }
9537 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009538 } /* for */
9539
9540 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
9541 break;
9542 if (start < p)
9543 {
9544 /* There's part of a line in buf, store it in "prev". */
9545 if (p - start + prevlen >= prevsize)
9546 {
9547 /* need bigger "prev" buffer */
9548 char_u *newprev;
9549
9550 /* A common use case is ordinary text files and "prev" gets a
9551 * fragment of a line, so the first allocation is made
9552 * small, to avoid repeatedly 'allocing' large and
9553 * 'reallocing' small. */
9554 if (prevsize == 0)
9555 prevsize = (long)(p - start);
9556 else
9557 {
9558 long grow50pc = (prevsize * 3) / 2;
9559 long growmin = (long)((p - start) * 2 + prevlen);
9560 prevsize = grow50pc > growmin ? grow50pc : growmin;
9561 }
9562 newprev = prev == NULL ? alloc(prevsize)
9563 : vim_realloc(prev, prevsize);
9564 if (newprev == NULL)
9565 {
9566 do_outofmem_msg((long_u)prevsize);
9567 failed = TRUE;
9568 break;
9569 }
9570 prev = newprev;
9571 }
9572 /* Add the line part to end of "prev". */
9573 mch_memmove(prev + prevlen, start, p - start);
9574 prevlen += (long)(p - start);
9575 }
9576 } /* while */
9577
9578 /*
9579 * For a negative line count use only the lines at the end of the file,
9580 * free the rest.
9581 */
9582 if (!failed && maxline < 0)
9583 while (cnt > -maxline)
9584 {
9585 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
9586 --cnt;
9587 }
9588
9589 if (failed)
9590 {
9591 list_free(rettv->vval.v_list);
9592 /* readfile doc says an empty list is returned on error */
9593 rettv->vval.v_list = list_alloc();
9594 }
9595
9596 vim_free(prev);
9597 fclose(fd);
9598}
9599
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009600 static void
9601return_register(int regname, typval_T *rettv)
9602{
9603 char_u buf[2] = {0, 0};
9604
9605 buf[0] = (char_u)regname;
9606 rettv->v_type = VAR_STRING;
9607 rettv->vval.v_string = vim_strsave(buf);
9608}
9609
9610/*
9611 * "reg_executing()" function
9612 */
9613 static void
9614f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9615{
9616 return_register(reg_executing, rettv);
9617}
9618
9619/*
9620 * "reg_recording()" function
9621 */
9622 static void
9623f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9624{
9625 return_register(reg_recording, rettv);
9626}
9627
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009628#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009629/*
9630 * Convert a List to proftime_T.
9631 * Return FAIL when there is something wrong.
9632 */
9633 static int
9634list2proftime(typval_T *arg, proftime_T *tm)
9635{
9636 long n1, n2;
9637 int error = FALSE;
9638
9639 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9640 || arg->vval.v_list->lv_len != 2)
9641 return FAIL;
9642 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9643 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009644# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009645 tm->HighPart = n1;
9646 tm->LowPart = n2;
9647# else
9648 tm->tv_sec = n1;
9649 tm->tv_usec = n2;
9650# endif
9651 return error ? FAIL : OK;
9652}
9653#endif /* FEAT_RELTIME */
9654
9655/*
9656 * "reltime()" function
9657 */
9658 static void
9659f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9660{
9661#ifdef FEAT_RELTIME
9662 proftime_T res;
9663 proftime_T start;
9664
9665 if (argvars[0].v_type == VAR_UNKNOWN)
9666 {
9667 /* No arguments: get current time. */
9668 profile_start(&res);
9669 }
9670 else if (argvars[1].v_type == VAR_UNKNOWN)
9671 {
9672 if (list2proftime(&argvars[0], &res) == FAIL)
9673 return;
9674 profile_end(&res);
9675 }
9676 else
9677 {
9678 /* Two arguments: compute the difference. */
9679 if (list2proftime(&argvars[0], &start) == FAIL
9680 || list2proftime(&argvars[1], &res) == FAIL)
9681 return;
9682 profile_sub(&res, &start);
9683 }
9684
9685 if (rettv_list_alloc(rettv) == OK)
9686 {
9687 long n1, n2;
9688
Bram Moolenaar4f974752019-02-17 17:44:42 +01009689# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009690 n1 = res.HighPart;
9691 n2 = res.LowPart;
9692# else
9693 n1 = res.tv_sec;
9694 n2 = res.tv_usec;
9695# endif
9696 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9697 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9698 }
9699#endif
9700}
9701
9702#ifdef FEAT_FLOAT
9703/*
9704 * "reltimefloat()" function
9705 */
9706 static void
9707f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9708{
9709# ifdef FEAT_RELTIME
9710 proftime_T tm;
9711# endif
9712
9713 rettv->v_type = VAR_FLOAT;
9714 rettv->vval.v_float = 0;
9715# ifdef FEAT_RELTIME
9716 if (list2proftime(&argvars[0], &tm) == OK)
9717 rettv->vval.v_float = profile_float(&tm);
9718# endif
9719}
9720#endif
9721
9722/*
9723 * "reltimestr()" function
9724 */
9725 static void
9726f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9727{
9728#ifdef FEAT_RELTIME
9729 proftime_T tm;
9730#endif
9731
9732 rettv->v_type = VAR_STRING;
9733 rettv->vval.v_string = NULL;
9734#ifdef FEAT_RELTIME
9735 if (list2proftime(&argvars[0], &tm) == OK)
9736 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9737#endif
9738}
9739
9740#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009741 static void
9742make_connection(void)
9743{
9744 if (X_DISPLAY == NULL
9745# ifdef FEAT_GUI
9746 && !gui.in_use
9747# endif
9748 )
9749 {
9750 x_force_connect = TRUE;
9751 setup_term_clip();
9752 x_force_connect = FALSE;
9753 }
9754}
9755
9756 static int
9757check_connection(void)
9758{
9759 make_connection();
9760 if (X_DISPLAY == NULL)
9761 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009762 emsg(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009763 return FAIL;
9764 }
9765 return OK;
9766}
9767#endif
9768
9769#ifdef FEAT_CLIENTSERVER
9770 static void
9771remote_common(typval_T *argvars, typval_T *rettv, int expr)
9772{
9773 char_u *server_name;
9774 char_u *keys;
9775 char_u *r = NULL;
9776 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009777 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009778# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009779 HWND w;
9780# else
9781 Window w;
9782# endif
9783
9784 if (check_restricted() || check_secure())
9785 return;
9786
9787# ifdef FEAT_X11
9788 if (check_connection() == FAIL)
9789 return;
9790# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009791 if (argvars[2].v_type != VAR_UNKNOWN
9792 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009793 timeout = tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009794
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009795 server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009796 if (server_name == NULL)
9797 return; /* type error; errmsg already given */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009798 keys = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009799# ifdef MSWIN
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009800 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009801# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009802 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9803 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009804# endif
9805 {
9806 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009807 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009808 emsg((char *)r); // sending worked but evaluation failed
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009809 vim_free(r);
9810 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009811 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009812 semsg(_("E241: Unable to send to %s"), server_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009813 return;
9814 }
9815
9816 rettv->vval.v_string = r;
9817
9818 if (argvars[2].v_type != VAR_UNKNOWN)
9819 {
9820 dictitem_T v;
9821 char_u str[30];
9822 char_u *idvar;
9823
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009824 idvar = tv_get_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009825 if (idvar != NULL && *idvar != NUL)
9826 {
9827 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9828 v.di_tv.v_type = VAR_STRING;
9829 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009830 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009831 vim_free(v.di_tv.vval.v_string);
9832 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009833 }
9834}
9835#endif
9836
9837/*
9838 * "remote_expr()" function
9839 */
9840 static void
9841f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9842{
9843 rettv->v_type = VAR_STRING;
9844 rettv->vval.v_string = NULL;
9845#ifdef FEAT_CLIENTSERVER
9846 remote_common(argvars, rettv, TRUE);
9847#endif
9848}
9849
9850/*
9851 * "remote_foreground()" function
9852 */
9853 static void
9854f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9855{
9856#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +01009857# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009858 /* On Win32 it's done in this application. */
9859 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009860 char_u *server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009861
9862 if (server_name != NULL)
9863 serverForeground(server_name);
9864 }
9865# else
9866 /* Send a foreground() expression to the server. */
9867 argvars[1].v_type = VAR_STRING;
9868 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9869 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009870 rettv->v_type = VAR_STRING;
9871 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009872 remote_common(argvars, rettv, TRUE);
9873 vim_free(argvars[1].vval.v_string);
9874# endif
9875#endif
9876}
9877
9878 static void
9879f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
9880{
9881#ifdef FEAT_CLIENTSERVER
9882 dictitem_T v;
9883 char_u *s = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009884# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009885 long_u n = 0;
9886# endif
9887 char_u *serverid;
9888
9889 if (check_restricted() || check_secure())
9890 {
9891 rettv->vval.v_number = -1;
9892 return;
9893 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009894 serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009895 if (serverid == NULL)
9896 {
9897 rettv->vval.v_number = -1;
9898 return; /* type error; errmsg already given */
9899 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01009900# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009901 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
9902 if (n == 0)
9903 rettv->vval.v_number = -1;
9904 else
9905 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009906 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009907 rettv->vval.v_number = (s != NULL);
9908 }
9909# else
9910 if (check_connection() == FAIL)
9911 return;
9912
9913 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
9914 serverStrToWin(serverid), &s);
9915# endif
9916
9917 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
9918 {
9919 char_u *retvar;
9920
9921 v.di_tv.v_type = VAR_STRING;
9922 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009923 retvar = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009924 if (retvar != NULL)
9925 set_var(retvar, &v.di_tv, FALSE);
9926 vim_free(v.di_tv.vval.v_string);
9927 }
9928#else
9929 rettv->vval.v_number = -1;
9930#endif
9931}
9932
9933 static void
9934f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
9935{
9936 char_u *r = NULL;
9937
9938#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009939 char_u *serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009940
9941 if (serverid != NULL && !check_restricted() && !check_secure())
9942 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009943 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009944# ifdef MSWIN
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009945 /* The server's HWND is encoded in the 'id' parameter */
9946 long_u n = 0;
9947# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009948
9949 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009950 timeout = tv_get_number(&argvars[1]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009951
Bram Moolenaar4f974752019-02-17 17:44:42 +01009952# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009953 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
9954 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009955 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009956 if (r == NULL)
9957# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009958 if (check_connection() == FAIL
9959 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
9960 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009961# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009962 emsg(_("E277: Unable to read a server reply"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009963 }
9964#endif
9965 rettv->v_type = VAR_STRING;
9966 rettv->vval.v_string = r;
9967}
9968
9969/*
9970 * "remote_send()" function
9971 */
9972 static void
9973f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9974{
9975 rettv->v_type = VAR_STRING;
9976 rettv->vval.v_string = NULL;
9977#ifdef FEAT_CLIENTSERVER
9978 remote_common(argvars, rettv, FALSE);
9979#endif
9980}
9981
9982/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009983 * "remote_startserver()" function
9984 */
9985 static void
9986f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9987{
9988#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009989 char_u *server = tv_get_string_chk(&argvars[0]);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009990
9991 if (server == NULL)
9992 return; /* type error; errmsg already given */
9993 if (serverName != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009994 emsg(_("E941: already started a server"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009995 else
9996 {
9997# ifdef FEAT_X11
9998 if (check_connection() == OK)
9999 serverRegisterName(X_DISPLAY, server);
10000# else
10001 serverSetName(server);
10002# endif
10003 }
10004#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010005 emsg(_("E942: +clientserver feature not available"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010006#endif
10007}
10008
10009/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010010 * "remove()" function
10011 */
10012 static void
10013f_remove(typval_T *argvars, typval_T *rettv)
10014{
10015 list_T *l;
10016 listitem_T *item, *item2;
10017 listitem_T *li;
10018 long idx;
10019 long end;
10020 char_u *key;
10021 dict_T *d;
10022 dictitem_T *di;
10023 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010024 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010025
10026 if (argvars[0].v_type == VAR_DICT)
10027 {
10028 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010029 semsg(_(e_toomanyarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010030 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010031 && !var_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010032 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010033 key = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010034 if (key != NULL)
10035 {
10036 di = dict_find(d, key, -1);
10037 if (di == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010038 semsg(_(e_dictkey), key);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010039 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10040 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
10041 {
10042 *rettv = di->di_tv;
10043 init_tv(&di->di_tv);
10044 dictitem_remove(d, di);
10045 }
10046 }
10047 }
10048 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010049 else if (argvars[0].v_type == VAR_BLOB)
10050 {
10051 idx = (long)tv_get_number_chk(&argvars[1], &error);
10052 if (!error)
10053 {
10054 blob_T *b = argvars[0].vval.v_blob;
10055 int len = blob_len(b);
10056 char_u *p;
10057
10058 if (idx < 0)
10059 // count from the end
10060 idx = len + idx;
10061 if (idx < 0 || idx >= len)
10062 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010063 semsg(_(e_blobidx), idx);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010064 return;
10065 }
10066 if (argvars[2].v_type == VAR_UNKNOWN)
10067 {
10068 // Remove one item, return its value.
10069 p = (char_u *)b->bv_ga.ga_data;
10070 rettv->vval.v_number = (varnumber_T) *(p + idx);
10071 mch_memmove(p + idx, p + idx + 1, (size_t)len - idx - 1);
10072 --b->bv_ga.ga_len;
10073 }
10074 else
10075 {
10076 blob_T *blob;
10077
10078 // Remove range of items, return list with values.
10079 end = (long)tv_get_number_chk(&argvars[2], &error);
10080 if (error)
10081 return;
10082 if (end < 0)
10083 // count from the end
10084 end = len + end;
10085 if (end >= len || idx > end)
10086 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010087 semsg(_(e_blobidx), end);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010088 return;
10089 }
10090 blob = blob_alloc();
10091 if (blob == NULL)
10092 return;
10093 blob->bv_ga.ga_len = end - idx + 1;
10094 if (ga_grow(&blob->bv_ga, end - idx + 1) == FAIL)
10095 {
10096 vim_free(blob);
10097 return;
10098 }
10099 p = (char_u *)b->bv_ga.ga_data;
10100 mch_memmove((char_u *)blob->bv_ga.ga_data, p + idx,
10101 (size_t)(end - idx + 1));
10102 ++blob->bv_refcount;
10103 rettv->v_type = VAR_BLOB;
10104 rettv->vval.v_blob = blob;
10105
10106 mch_memmove(p + idx, p + end + 1, (size_t)(len - end));
10107 b->bv_ga.ga_len -= end - idx + 1;
10108 }
10109 }
10110 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010111 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010112 semsg(_(e_listdictblobarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010113 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010114 && !var_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010115 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010116 idx = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010117 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010118 ; // type error: do nothing, errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010119 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010120 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010121 else
10122 {
10123 if (argvars[2].v_type == VAR_UNKNOWN)
10124 {
10125 /* Remove one item, return its value. */
10126 vimlist_remove(l, item, item);
10127 *rettv = item->li_tv;
10128 vim_free(item);
10129 }
10130 else
10131 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010132 // Remove range of items, return list with values.
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010133 end = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010134 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010135 ; // type error: do nothing
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010136 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010137 semsg(_(e_listidx), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010138 else
10139 {
10140 int cnt = 0;
10141
10142 for (li = item; li != NULL; li = li->li_next)
10143 {
10144 ++cnt;
10145 if (li == item2)
10146 break;
10147 }
10148 if (li == NULL) /* didn't find "item2" after "item" */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010149 emsg(_(e_invrange));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010150 else
10151 {
10152 vimlist_remove(l, item, item2);
10153 if (rettv_list_alloc(rettv) == OK)
10154 {
10155 l = rettv->vval.v_list;
10156 l->lv_first = item;
10157 l->lv_last = item2;
10158 item->li_prev = NULL;
10159 item2->li_next = NULL;
10160 l->lv_len = cnt;
10161 }
10162 }
10163 }
10164 }
10165 }
10166 }
10167}
10168
10169/*
10170 * "rename({from}, {to})" function
10171 */
10172 static void
10173f_rename(typval_T *argvars, typval_T *rettv)
10174{
10175 char_u buf[NUMBUFLEN];
10176
10177 if (check_restricted() || check_secure())
10178 rettv->vval.v_number = -1;
10179 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010180 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
10181 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010182}
10183
10184/*
10185 * "repeat()" function
10186 */
10187 static void
10188f_repeat(typval_T *argvars, typval_T *rettv)
10189{
10190 char_u *p;
10191 int n;
10192 int slen;
10193 int len;
10194 char_u *r;
10195 int i;
10196
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010197 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010198 if (argvars[0].v_type == VAR_LIST)
10199 {
10200 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
10201 while (n-- > 0)
10202 if (list_extend(rettv->vval.v_list,
10203 argvars[0].vval.v_list, NULL) == FAIL)
10204 break;
10205 }
10206 else
10207 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010208 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010209 rettv->v_type = VAR_STRING;
10210 rettv->vval.v_string = NULL;
10211
10212 slen = (int)STRLEN(p);
10213 len = slen * n;
10214 if (len <= 0)
10215 return;
10216
10217 r = alloc(len + 1);
10218 if (r != NULL)
10219 {
10220 for (i = 0; i < n; i++)
10221 mch_memmove(r + i * slen, p, (size_t)slen);
10222 r[len] = NUL;
10223 }
10224
10225 rettv->vval.v_string = r;
10226 }
10227}
10228
10229/*
10230 * "resolve()" function
10231 */
10232 static void
10233f_resolve(typval_T *argvars, typval_T *rettv)
10234{
10235 char_u *p;
10236#ifdef HAVE_READLINK
10237 char_u *buf = NULL;
10238#endif
10239
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010240 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010241#ifdef FEAT_SHORTCUT
10242 {
10243 char_u *v = NULL;
10244
Bram Moolenaardce1e892019-02-10 23:18:53 +010010245 v = mch_resolve_path(p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010246 if (v != NULL)
10247 rettv->vval.v_string = v;
10248 else
10249 rettv->vval.v_string = vim_strsave(p);
10250 }
10251#else
10252# ifdef HAVE_READLINK
10253 {
10254 char_u *cpy;
10255 int len;
10256 char_u *remain = NULL;
10257 char_u *q;
10258 int is_relative_to_current = FALSE;
10259 int has_trailing_pathsep = FALSE;
10260 int limit = 100;
10261
10262 p = vim_strsave(p);
10263
10264 if (p[0] == '.' && (vim_ispathsep(p[1])
10265 || (p[1] == '.' && (vim_ispathsep(p[2])))))
10266 is_relative_to_current = TRUE;
10267
10268 len = STRLEN(p);
10269 if (len > 0 && after_pathsep(p, p + len))
10270 {
10271 has_trailing_pathsep = TRUE;
10272 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
10273 }
10274
10275 q = getnextcomp(p);
10276 if (*q != NUL)
10277 {
10278 /* Separate the first path component in "p", and keep the
10279 * remainder (beginning with the path separator). */
10280 remain = vim_strsave(q - 1);
10281 q[-1] = NUL;
10282 }
10283
10284 buf = alloc(MAXPATHL + 1);
10285 if (buf == NULL)
10286 goto fail;
10287
10288 for (;;)
10289 {
10290 for (;;)
10291 {
10292 len = readlink((char *)p, (char *)buf, MAXPATHL);
10293 if (len <= 0)
10294 break;
10295 buf[len] = NUL;
10296
10297 if (limit-- == 0)
10298 {
10299 vim_free(p);
10300 vim_free(remain);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010301 emsg(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010302 rettv->vval.v_string = NULL;
10303 goto fail;
10304 }
10305
10306 /* Ensure that the result will have a trailing path separator
10307 * if the argument has one. */
10308 if (remain == NULL && has_trailing_pathsep)
10309 add_pathsep(buf);
10310
10311 /* Separate the first path component in the link value and
10312 * concatenate the remainders. */
10313 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
10314 if (*q != NUL)
10315 {
10316 if (remain == NULL)
10317 remain = vim_strsave(q - 1);
10318 else
10319 {
10320 cpy = concat_str(q - 1, remain);
10321 if (cpy != NULL)
10322 {
10323 vim_free(remain);
10324 remain = cpy;
10325 }
10326 }
10327 q[-1] = NUL;
10328 }
10329
10330 q = gettail(p);
10331 if (q > p && *q == NUL)
10332 {
10333 /* Ignore trailing path separator. */
10334 q[-1] = NUL;
10335 q = gettail(p);
10336 }
10337 if (q > p && !mch_isFullName(buf))
10338 {
10339 /* symlink is relative to directory of argument */
10340 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
10341 if (cpy != NULL)
10342 {
10343 STRCPY(cpy, p);
10344 STRCPY(gettail(cpy), buf);
10345 vim_free(p);
10346 p = cpy;
10347 }
10348 }
10349 else
10350 {
10351 vim_free(p);
10352 p = vim_strsave(buf);
10353 }
10354 }
10355
10356 if (remain == NULL)
10357 break;
10358
10359 /* Append the first path component of "remain" to "p". */
10360 q = getnextcomp(remain + 1);
10361 len = q - remain - (*q != NUL);
10362 cpy = vim_strnsave(p, STRLEN(p) + len);
10363 if (cpy != NULL)
10364 {
10365 STRNCAT(cpy, remain, len);
10366 vim_free(p);
10367 p = cpy;
10368 }
10369 /* Shorten "remain". */
10370 if (*q != NUL)
10371 STRMOVE(remain, q - 1);
10372 else
Bram Moolenaard23a8232018-02-10 18:45:26 +010010373 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010374 }
10375
10376 /* If the result is a relative path name, make it explicitly relative to
10377 * the current directory if and only if the argument had this form. */
10378 if (!vim_ispathsep(*p))
10379 {
10380 if (is_relative_to_current
10381 && *p != NUL
10382 && !(p[0] == '.'
10383 && (p[1] == NUL
10384 || vim_ispathsep(p[1])
10385 || (p[1] == '.'
10386 && (p[2] == NUL
10387 || vim_ispathsep(p[2]))))))
10388 {
10389 /* Prepend "./". */
10390 cpy = concat_str((char_u *)"./", p);
10391 if (cpy != NULL)
10392 {
10393 vim_free(p);
10394 p = cpy;
10395 }
10396 }
10397 else if (!is_relative_to_current)
10398 {
10399 /* Strip leading "./". */
10400 q = p;
10401 while (q[0] == '.' && vim_ispathsep(q[1]))
10402 q += 2;
10403 if (q > p)
10404 STRMOVE(p, p + 2);
10405 }
10406 }
10407
10408 /* Ensure that the result will have no trailing path separator
10409 * if the argument had none. But keep "/" or "//". */
10410 if (!has_trailing_pathsep)
10411 {
10412 q = p + STRLEN(p);
10413 if (after_pathsep(p, q))
10414 *gettail_sep(p) = NUL;
10415 }
10416
10417 rettv->vval.v_string = p;
10418 }
10419# else
10420 rettv->vval.v_string = vim_strsave(p);
10421# endif
10422#endif
10423
10424 simplify_filename(rettv->vval.v_string);
10425
10426#ifdef HAVE_READLINK
10427fail:
10428 vim_free(buf);
10429#endif
10430 rettv->v_type = VAR_STRING;
10431}
10432
10433/*
10434 * "reverse({list})" function
10435 */
10436 static void
10437f_reverse(typval_T *argvars, typval_T *rettv)
10438{
10439 list_T *l;
10440 listitem_T *li, *ni;
10441
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010442 if (argvars[0].v_type == VAR_BLOB)
10443 {
10444 blob_T *b = argvars[0].vval.v_blob;
10445 int i, len = blob_len(b);
10446
10447 for (i = 0; i < len / 2; i++)
10448 {
10449 int tmp = blob_get(b, i);
10450
10451 blob_set(b, i, blob_get(b, len - i - 1));
10452 blob_set(b, len - i - 1, tmp);
10453 }
10454 rettv_blob_set(rettv, b);
10455 return;
10456 }
10457
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010458 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010459 semsg(_(e_listblobarg), "reverse()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010460 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010461 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010462 (char_u *)N_("reverse() argument"), TRUE))
10463 {
10464 li = l->lv_last;
10465 l->lv_first = l->lv_last = NULL;
10466 l->lv_len = 0;
10467 while (li != NULL)
10468 {
10469 ni = li->li_prev;
10470 list_append(l, li);
10471 li = ni;
10472 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020010473 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010474 l->lv_idx = l->lv_len - l->lv_idx - 1;
10475 }
10476}
10477
10478#define SP_NOMOVE 0x01 /* don't move cursor */
10479#define SP_REPEAT 0x02 /* repeat to find outer pair */
10480#define SP_RETCOUNT 0x04 /* return matchcount */
10481#define SP_SETPCMARK 0x08 /* set previous context mark */
10482#define SP_START 0x10 /* accept match at start position */
10483#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
10484#define SP_END 0x40 /* leave cursor at end of match */
10485#define SP_COLUMN 0x80 /* start at cursor column */
10486
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010487/*
10488 * Get flags for a search function.
10489 * Possibly sets "p_ws".
10490 * Returns BACKWARD, FORWARD or zero (for an error).
10491 */
10492 static int
10493get_search_arg(typval_T *varp, int *flagsp)
10494{
10495 int dir = FORWARD;
10496 char_u *flags;
10497 char_u nbuf[NUMBUFLEN];
10498 int mask;
10499
10500 if (varp->v_type != VAR_UNKNOWN)
10501 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010502 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010503 if (flags == NULL)
10504 return 0; /* type error; errmsg already given */
10505 while (*flags != NUL)
10506 {
10507 switch (*flags)
10508 {
10509 case 'b': dir = BACKWARD; break;
10510 case 'w': p_ws = TRUE; break;
10511 case 'W': p_ws = FALSE; break;
10512 default: mask = 0;
10513 if (flagsp != NULL)
10514 switch (*flags)
10515 {
10516 case 'c': mask = SP_START; break;
10517 case 'e': mask = SP_END; break;
10518 case 'm': mask = SP_RETCOUNT; break;
10519 case 'n': mask = SP_NOMOVE; break;
10520 case 'p': mask = SP_SUBPAT; break;
10521 case 'r': mask = SP_REPEAT; break;
10522 case 's': mask = SP_SETPCMARK; break;
10523 case 'z': mask = SP_COLUMN; break;
10524 }
10525 if (mask == 0)
10526 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010527 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010528 dir = 0;
10529 }
10530 else
10531 *flagsp |= mask;
10532 }
10533 if (dir == 0)
10534 break;
10535 ++flags;
10536 }
10537 }
10538 return dir;
10539}
10540
10541/*
10542 * Shared by search() and searchpos() functions.
10543 */
10544 static int
10545search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
10546{
10547 int flags;
10548 char_u *pat;
10549 pos_T pos;
10550 pos_T save_cursor;
10551 int save_p_ws = p_ws;
10552 int dir;
10553 int retval = 0; /* default: FAIL */
10554 long lnum_stop = 0;
10555 proftime_T tm;
10556#ifdef FEAT_RELTIME
10557 long time_limit = 0;
10558#endif
10559 int options = SEARCH_KEEP;
10560 int subpatnum;
10561
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010562 pat = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010563 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
10564 if (dir == 0)
10565 goto theend;
10566 flags = *flagsp;
10567 if (flags & SP_START)
10568 options |= SEARCH_START;
10569 if (flags & SP_END)
10570 options |= SEARCH_END;
10571 if (flags & SP_COLUMN)
10572 options |= SEARCH_COL;
10573
10574 /* Optional arguments: line number to stop searching and timeout. */
10575 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
10576 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010577 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010578 if (lnum_stop < 0)
10579 goto theend;
10580#ifdef FEAT_RELTIME
10581 if (argvars[3].v_type != VAR_UNKNOWN)
10582 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010583 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010584 if (time_limit < 0)
10585 goto theend;
10586 }
10587#endif
10588 }
10589
10590#ifdef FEAT_RELTIME
10591 /* Set the time limit, if there is one. */
10592 profile_setlimit(time_limit, &tm);
10593#endif
10594
10595 /*
10596 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
10597 * Check to make sure only those flags are set.
10598 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
10599 * flags cannot be set. Check for that condition also.
10600 */
10601 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
10602 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10603 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010604 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010605 goto theend;
10606 }
10607
10608 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010609 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010610 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010611 if (subpatnum != FAIL)
10612 {
10613 if (flags & SP_SUBPAT)
10614 retval = subpatnum;
10615 else
10616 retval = pos.lnum;
10617 if (flags & SP_SETPCMARK)
10618 setpcmark();
10619 curwin->w_cursor = pos;
10620 if (match_pos != NULL)
10621 {
10622 /* Store the match cursor position */
10623 match_pos->lnum = pos.lnum;
10624 match_pos->col = pos.col + 1;
10625 }
10626 /* "/$" will put the cursor after the end of the line, may need to
10627 * correct that here */
10628 check_cursor();
10629 }
10630
10631 /* If 'n' flag is used: restore cursor position. */
10632 if (flags & SP_NOMOVE)
10633 curwin->w_cursor = save_cursor;
10634 else
10635 curwin->w_set_curswant = TRUE;
10636theend:
10637 p_ws = save_p_ws;
10638
10639 return retval;
10640}
10641
10642#ifdef FEAT_FLOAT
10643
10644/*
10645 * round() is not in C90, use ceil() or floor() instead.
10646 */
10647 float_T
10648vim_round(float_T f)
10649{
10650 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
10651}
10652
10653/*
10654 * "round({float})" function
10655 */
10656 static void
10657f_round(typval_T *argvars, typval_T *rettv)
10658{
10659 float_T f = 0.0;
10660
10661 rettv->v_type = VAR_FLOAT;
10662 if (get_float_arg(argvars, &f) == OK)
10663 rettv->vval.v_float = vim_round(f);
10664 else
10665 rettv->vval.v_float = 0.0;
10666}
10667#endif
10668
Bram Moolenaare99be0e2019-03-26 22:51:09 +010010669#ifdef FEAT_RUBY
10670/*
10671 * "rubyeval()" function
10672 */
10673 static void
10674f_rubyeval(typval_T *argvars, typval_T *rettv)
10675{
10676 char_u *str;
10677 char_u buf[NUMBUFLEN];
10678
10679 str = tv_get_string_buf(&argvars[0], buf);
10680 do_rubyeval(str, rettv);
10681}
10682#endif
10683
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010684/*
10685 * "screenattr()" function
10686 */
10687 static void
10688f_screenattr(typval_T *argvars, typval_T *rettv)
10689{
10690 int row;
10691 int col;
10692 int c;
10693
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010694 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10695 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010696 if (row < 0 || row >= screen_Rows
10697 || col < 0 || col >= screen_Columns)
10698 c = -1;
10699 else
10700 c = ScreenAttrs[LineOffset[row] + col];
10701 rettv->vval.v_number = c;
10702}
10703
10704/*
10705 * "screenchar()" function
10706 */
10707 static void
10708f_screenchar(typval_T *argvars, typval_T *rettv)
10709{
10710 int row;
10711 int col;
10712 int off;
10713 int c;
10714
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010715 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10716 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010717 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010718 c = -1;
10719 else
10720 {
10721 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010722 if (enc_utf8 && ScreenLinesUC[off] != 0)
10723 c = ScreenLinesUC[off];
10724 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010725 c = ScreenLines[off];
10726 }
10727 rettv->vval.v_number = c;
10728}
10729
10730/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010731 * "screenchars()" function
10732 */
10733 static void
10734f_screenchars(typval_T *argvars, typval_T *rettv)
10735{
10736 int row;
10737 int col;
10738 int off;
10739 int c;
10740 int i;
10741
10742 if (rettv_list_alloc(rettv) == FAIL)
10743 return;
10744 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10745 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10746 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10747 return;
10748
10749 off = LineOffset[row] + col;
10750 if (enc_utf8 && ScreenLinesUC[off] != 0)
10751 c = ScreenLinesUC[off];
10752 else
10753 c = ScreenLines[off];
10754 list_append_number(rettv->vval.v_list, (varnumber_T)c);
10755
10756 if (enc_utf8)
10757
10758 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10759 list_append_number(rettv->vval.v_list,
10760 (varnumber_T)ScreenLinesC[i][off]);
10761}
10762
10763/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010764 * "screencol()" function
10765 *
10766 * First column is 1 to be consistent with virtcol().
10767 */
10768 static void
10769f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10770{
10771 rettv->vval.v_number = screen_screencol() + 1;
10772}
10773
10774/*
10775 * "screenrow()" function
10776 */
10777 static void
10778f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10779{
10780 rettv->vval.v_number = screen_screenrow() + 1;
10781}
10782
10783/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010784 * "screenstring()" function
10785 */
10786 static void
10787f_screenstring(typval_T *argvars, typval_T *rettv)
10788{
10789 int row;
10790 int col;
10791 int off;
10792 int c;
10793 int i;
10794 char_u buf[MB_MAXBYTES + 1];
10795 int buflen = 0;
10796
10797 rettv->vval.v_string = NULL;
10798 rettv->v_type = VAR_STRING;
10799
10800 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10801 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10802 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10803 return;
10804
10805 off = LineOffset[row] + col;
10806 if (enc_utf8 && ScreenLinesUC[off] != 0)
10807 c = ScreenLinesUC[off];
10808 else
10809 c = ScreenLines[off];
10810 buflen += mb_char2bytes(c, buf);
10811
10812 if (enc_utf8)
10813 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10814 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
10815
10816 buf[buflen] = NUL;
10817 rettv->vval.v_string = vim_strsave(buf);
10818}
10819
10820/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010821 * "search()" function
10822 */
10823 static void
10824f_search(typval_T *argvars, typval_T *rettv)
10825{
10826 int flags = 0;
10827
10828 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10829}
10830
10831/*
10832 * "searchdecl()" function
10833 */
10834 static void
10835f_searchdecl(typval_T *argvars, typval_T *rettv)
10836{
10837 int locally = 1;
10838 int thisblock = 0;
10839 int error = FALSE;
10840 char_u *name;
10841
10842 rettv->vval.v_number = 1; /* default: FAIL */
10843
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010844 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010845 if (argvars[1].v_type != VAR_UNKNOWN)
10846 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010847 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010848 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010849 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010850 }
10851 if (!error && name != NULL)
10852 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10853 locally, thisblock, SEARCH_KEEP) == FAIL;
10854}
10855
10856/*
10857 * Used by searchpair() and searchpairpos()
10858 */
10859 static int
10860searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10861{
10862 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010863 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010864 int save_p_ws = p_ws;
10865 int dir;
10866 int flags = 0;
10867 char_u nbuf1[NUMBUFLEN];
10868 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010869 int retval = 0; /* default: FAIL */
10870 long lnum_stop = 0;
10871 long time_limit = 0;
10872
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010873 /* Get the three pattern arguments: start, middle, end. Will result in an
10874 * error if not a valid argument. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010875 spat = tv_get_string_chk(&argvars[0]);
10876 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
10877 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010878 if (spat == NULL || mpat == NULL || epat == NULL)
10879 goto theend; /* type error */
10880
10881 /* Handle the optional fourth argument: flags */
10882 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
10883 if (dir == 0)
10884 goto theend;
10885
10886 /* Don't accept SP_END or SP_SUBPAT.
10887 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
10888 */
10889 if ((flags & (SP_END | SP_SUBPAT)) != 0
10890 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10891 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010892 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010893 goto theend;
10894 }
10895
10896 /* Using 'r' implies 'W', otherwise it doesn't work. */
10897 if (flags & SP_REPEAT)
10898 p_ws = FALSE;
10899
10900 /* Optional fifth argument: skip expression */
10901 if (argvars[3].v_type == VAR_UNKNOWN
10902 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010010903 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010904 else
10905 {
Bram Moolenaar48570482017-10-30 21:48:41 +010010906 skip = &argvars[4];
10907 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
10908 && skip->v_type != VAR_STRING)
10909 {
10910 /* Type error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010911 semsg(_(e_invarg2), tv_get_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010010912 goto theend;
10913 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010914 if (argvars[5].v_type != VAR_UNKNOWN)
10915 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010916 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010917 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010918 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010919 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010920 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010921 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010922#ifdef FEAT_RELTIME
10923 if (argvars[6].v_type != VAR_UNKNOWN)
10924 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010925 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010926 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010927 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010928 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010929 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010930 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010931 }
10932#endif
10933 }
10934 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010935
10936 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
10937 match_pos, lnum_stop, time_limit);
10938
10939theend:
10940 p_ws = save_p_ws;
10941
10942 return retval;
10943}
10944
10945/*
10946 * "searchpair()" function
10947 */
10948 static void
10949f_searchpair(typval_T *argvars, typval_T *rettv)
10950{
10951 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
10952}
10953
10954/*
10955 * "searchpairpos()" function
10956 */
10957 static void
10958f_searchpairpos(typval_T *argvars, typval_T *rettv)
10959{
10960 pos_T match_pos;
10961 int lnum = 0;
10962 int col = 0;
10963
10964 if (rettv_list_alloc(rettv) == FAIL)
10965 return;
10966
10967 if (searchpair_cmn(argvars, &match_pos) > 0)
10968 {
10969 lnum = match_pos.lnum;
10970 col = match_pos.col;
10971 }
10972
10973 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10974 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10975}
10976
10977/*
10978 * Search for a start/middle/end thing.
10979 * Used by searchpair(), see its documentation for the details.
10980 * Returns 0 or -1 for no match,
10981 */
10982 long
10983do_searchpair(
10984 char_u *spat, /* start pattern */
10985 char_u *mpat, /* middle pattern */
10986 char_u *epat, /* end pattern */
10987 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010010988 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010989 int flags, /* SP_SETPCMARK and other SP_ values */
10990 pos_T *match_pos,
10991 linenr_T lnum_stop, /* stop at this line if not zero */
10992 long time_limit UNUSED) /* stop after this many msec */
10993{
10994 char_u *save_cpo;
10995 char_u *pat, *pat2 = NULL, *pat3 = NULL;
10996 long retval = 0;
10997 pos_T pos;
10998 pos_T firstpos;
10999 pos_T foundpos;
11000 pos_T save_cursor;
11001 pos_T save_pos;
11002 int n;
11003 int r;
11004 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010011005 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011006 int err;
11007 int options = SEARCH_KEEP;
11008 proftime_T tm;
11009
11010 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11011 save_cpo = p_cpo;
11012 p_cpo = empty_option;
11013
11014#ifdef FEAT_RELTIME
11015 /* Set the time limit, if there is one. */
11016 profile_setlimit(time_limit, &tm);
11017#endif
11018
11019 /* Make two search patterns: start/end (pat2, for in nested pairs) and
11020 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +010011021 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
11022 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011023 if (pat2 == NULL || pat3 == NULL)
11024 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010011025 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011026 if (*mpat == NUL)
11027 STRCPY(pat3, pat2);
11028 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010011029 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011030 spat, epat, mpat);
11031 if (flags & SP_START)
11032 options |= SEARCH_START;
11033
Bram Moolenaar48570482017-10-30 21:48:41 +010011034 if (skip != NULL)
11035 {
11036 /* Empty string means to not use the skip expression. */
11037 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
11038 use_skip = skip->vval.v_string != NULL
11039 && *skip->vval.v_string != NUL;
11040 }
11041
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011042 save_cursor = curwin->w_cursor;
11043 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011044 CLEAR_POS(&firstpos);
11045 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011046 pat = pat3;
11047 for (;;)
11048 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +010011049 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020011050 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011051 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011052 /* didn't find it or found the first match again: FAIL */
11053 break;
11054
11055 if (firstpos.lnum == 0)
11056 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011057 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011058 {
11059 /* Found the same position again. Can happen with a pattern that
11060 * has "\zs" at the end and searching backwards. Advance one
11061 * character and try again. */
11062 if (dir == BACKWARD)
11063 decl(&pos);
11064 else
11065 incl(&pos);
11066 }
11067 foundpos = pos;
11068
11069 /* clear the start flag to avoid getting stuck here */
11070 options &= ~SEARCH_START;
11071
11072 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010011073 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011074 {
11075 save_pos = curwin->w_cursor;
11076 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010011077 err = FALSE;
11078 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011079 curwin->w_cursor = save_pos;
11080 if (err)
11081 {
11082 /* Evaluating {skip} caused an error, break here. */
11083 curwin->w_cursor = save_cursor;
11084 retval = -1;
11085 break;
11086 }
11087 if (r)
11088 continue;
11089 }
11090
11091 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
11092 {
11093 /* Found end when searching backwards or start when searching
11094 * forward: nested pair. */
11095 ++nest;
11096 pat = pat2; /* nested, don't search for middle */
11097 }
11098 else
11099 {
11100 /* Found end when searching forward or start when searching
11101 * backward: end of (nested) pair; or found middle in outer pair. */
11102 if (--nest == 1)
11103 pat = pat3; /* outer level, search for middle */
11104 }
11105
11106 if (nest == 0)
11107 {
11108 /* Found the match: return matchcount or line number. */
11109 if (flags & SP_RETCOUNT)
11110 ++retval;
11111 else
11112 retval = pos.lnum;
11113 if (flags & SP_SETPCMARK)
11114 setpcmark();
11115 curwin->w_cursor = pos;
11116 if (!(flags & SP_REPEAT))
11117 break;
11118 nest = 1; /* search for next unmatched */
11119 }
11120 }
11121
11122 if (match_pos != NULL)
11123 {
11124 /* Store the match cursor position */
11125 match_pos->lnum = curwin->w_cursor.lnum;
11126 match_pos->col = curwin->w_cursor.col + 1;
11127 }
11128
11129 /* If 'n' flag is used or search failed: restore cursor position. */
11130 if ((flags & SP_NOMOVE) || retval == 0)
11131 curwin->w_cursor = save_cursor;
11132
11133theend:
11134 vim_free(pat2);
11135 vim_free(pat3);
11136 if (p_cpo == empty_option)
11137 p_cpo = save_cpo;
11138 else
11139 /* Darn, evaluating the {skip} expression changed the value. */
11140 free_string_option(save_cpo);
11141
11142 return retval;
11143}
11144
11145/*
11146 * "searchpos()" function
11147 */
11148 static void
11149f_searchpos(typval_T *argvars, typval_T *rettv)
11150{
11151 pos_T match_pos;
11152 int lnum = 0;
11153 int col = 0;
11154 int n;
11155 int flags = 0;
11156
11157 if (rettv_list_alloc(rettv) == FAIL)
11158 return;
11159
11160 n = search_cmn(argvars, &match_pos, &flags);
11161 if (n > 0)
11162 {
11163 lnum = match_pos.lnum;
11164 col = match_pos.col;
11165 }
11166
11167 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
11168 list_append_number(rettv->vval.v_list, (varnumber_T)col);
11169 if (flags & SP_SUBPAT)
11170 list_append_number(rettv->vval.v_list, (varnumber_T)n);
11171}
11172
11173 static void
11174f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
11175{
11176#ifdef FEAT_CLIENTSERVER
11177 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011178 char_u *server = tv_get_string_chk(&argvars[0]);
11179 char_u *reply = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011180
11181 rettv->vval.v_number = -1;
11182 if (server == NULL || reply == NULL)
11183 return;
11184 if (check_restricted() || check_secure())
11185 return;
11186# ifdef FEAT_X11
11187 if (check_connection() == FAIL)
11188 return;
11189# endif
11190
11191 if (serverSendReply(server, reply) < 0)
11192 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011193 emsg(_("E258: Unable to send to client"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011194 return;
11195 }
11196 rettv->vval.v_number = 0;
11197#else
11198 rettv->vval.v_number = -1;
11199#endif
11200}
11201
11202 static void
11203f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
11204{
11205 char_u *r = NULL;
11206
11207#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +010011208# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011209 r = serverGetVimNames();
11210# else
11211 make_connection();
11212 if (X_DISPLAY != NULL)
11213 r = serverGetVimNames(X_DISPLAY);
11214# endif
11215#endif
11216 rettv->v_type = VAR_STRING;
11217 rettv->vval.v_string = r;
11218}
11219
11220/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011221 * "setbufline()" function
11222 */
11223 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020011224f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011225{
11226 linenr_T lnum;
11227 buf_T *buf;
11228
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011229 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011230 if (buf == NULL)
11231 rettv->vval.v_number = 1; /* FAIL */
11232 else
11233 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011234 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020011235 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011236 }
11237}
11238
11239/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011240 * "setbufvar()" function
11241 */
11242 static void
11243f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
11244{
11245 buf_T *buf;
11246 char_u *varname, *bufvarname;
11247 typval_T *varp;
11248 char_u nbuf[NUMBUFLEN];
11249
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011250 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011251 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011252 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
11253 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011254 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011255 varp = &argvars[2];
11256
11257 if (buf != NULL && varname != NULL && varp != NULL)
11258 {
11259 if (*varname == '&')
11260 {
11261 long numval;
11262 char_u *strval;
11263 int error = FALSE;
11264 aco_save_T aco;
11265
11266 /* set curbuf to be our buf, temporarily */
11267 aucmd_prepbuf(&aco, buf);
11268
11269 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011270 numval = (long)tv_get_number_chk(varp, &error);
11271 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011272 if (!error && strval != NULL)
11273 set_option_value(varname, numval, strval, OPT_LOCAL);
11274
11275 /* reset notion of buffer */
11276 aucmd_restbuf(&aco);
11277 }
11278 else
11279 {
11280 buf_T *save_curbuf = curbuf;
11281
11282 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
11283 if (bufvarname != NULL)
11284 {
11285 curbuf = buf;
11286 STRCPY(bufvarname, "b:");
11287 STRCPY(bufvarname + 2, varname);
11288 set_var(bufvarname, varp, TRUE);
11289 vim_free(bufvarname);
11290 curbuf = save_curbuf;
11291 }
11292 }
11293 }
11294}
11295
11296 static void
11297f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
11298{
11299 dict_T *d;
11300 dictitem_T *di;
11301 char_u *csearch;
11302
11303 if (argvars[0].v_type != VAR_DICT)
11304 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011305 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011306 return;
11307 }
11308
11309 if ((d = argvars[0].vval.v_dict) != NULL)
11310 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010011311 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011312 if (csearch != NULL)
11313 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011314 if (enc_utf8)
11315 {
11316 int pcc[MAX_MCO];
11317 int c = utfc_ptr2char(csearch, pcc);
11318
11319 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
11320 }
11321 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011322 set_last_csearch(PTR2CHAR(csearch),
11323 csearch, MB_PTR2LEN(csearch));
11324 }
11325
11326 di = dict_find(d, (char_u *)"forward", -1);
11327 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011328 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011329 ? FORWARD : BACKWARD);
11330
11331 di = dict_find(d, (char_u *)"until", -1);
11332 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011333 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011334 }
11335}
11336
11337/*
11338 * "setcmdpos()" function
11339 */
11340 static void
11341f_setcmdpos(typval_T *argvars, typval_T *rettv)
11342{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011343 int pos = (int)tv_get_number(&argvars[0]) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011344
11345 if (pos >= 0)
11346 rettv->vval.v_number = set_cmdline_pos(pos);
11347}
11348
11349/*
11350 * "setfperm({fname}, {mode})" function
11351 */
11352 static void
11353f_setfperm(typval_T *argvars, typval_T *rettv)
11354{
11355 char_u *fname;
11356 char_u modebuf[NUMBUFLEN];
11357 char_u *mode_str;
11358 int i;
11359 int mask;
11360 int mode = 0;
11361
11362 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011363 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011364 if (fname == NULL)
11365 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011366 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011367 if (mode_str == NULL)
11368 return;
11369 if (STRLEN(mode_str) != 9)
11370 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011371 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011372 return;
11373 }
11374
11375 mask = 1;
11376 for (i = 8; i >= 0; --i)
11377 {
11378 if (mode_str[i] != '-')
11379 mode |= mask;
11380 mask = mask << 1;
11381 }
11382 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
11383}
11384
11385/*
11386 * "setline()" function
11387 */
11388 static void
11389f_setline(typval_T *argvars, typval_T *rettv)
11390{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011391 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011392
Bram Moolenaarca851592018-06-06 21:04:07 +020011393 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011394}
11395
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011396/*
11397 * Used by "setqflist()" and "setloclist()" functions
11398 */
11399 static void
11400set_qf_ll_list(
11401 win_T *wp UNUSED,
11402 typval_T *list_arg UNUSED,
11403 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020011404 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011405 typval_T *rettv)
11406{
11407#ifdef FEAT_QUICKFIX
11408 static char *e_invact = N_("E927: Invalid action: '%s'");
11409 char_u *act;
11410 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011411 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011412#endif
11413
11414 rettv->vval.v_number = -1;
11415
11416#ifdef FEAT_QUICKFIX
11417 if (list_arg->v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011418 emsg(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011419 else if (recursive != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011420 emsg(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011421 else
11422 {
11423 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011424 dict_T *d = NULL;
11425 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011426
11427 if (action_arg->v_type == VAR_STRING)
11428 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011429 act = tv_get_string_chk(action_arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011430 if (act == NULL)
11431 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020011432 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
11433 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011434 action = *act;
11435 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011436 semsg(_(e_invact), act);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011437 }
11438 else if (action_arg->v_type == VAR_UNKNOWN)
11439 action = ' ';
11440 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011441 emsg(_(e_stringreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011442
Bram Moolenaard823fa92016-08-12 16:29:27 +020011443 if (action_arg->v_type != VAR_UNKNOWN
11444 && what_arg->v_type != VAR_UNKNOWN)
11445 {
11446 if (what_arg->v_type == VAR_DICT)
11447 d = what_arg->vval.v_dict;
11448 else
11449 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011450 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +020011451 valid_dict = FALSE;
11452 }
11453 }
11454
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011455 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011456 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011457 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
11458 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011459 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011460 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011461 }
11462#endif
11463}
11464
11465/*
11466 * "setloclist()" function
11467 */
11468 static void
11469f_setloclist(typval_T *argvars, typval_T *rettv)
11470{
11471 win_T *win;
11472
11473 rettv->vval.v_number = -1;
11474
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020011475 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011476 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020011477 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011478}
11479
11480/*
11481 * "setmatches()" function
11482 */
11483 static void
11484f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
11485{
11486#ifdef FEAT_SEARCH_EXTRA
11487 list_T *l;
11488 listitem_T *li;
11489 dict_T *d;
11490 list_T *s = NULL;
Bram Moolenaaraff74912019-03-30 18:11:49 +010011491 win_T *win = get_optional_window(argvars, 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011492
11493 rettv->vval.v_number = -1;
11494 if (argvars[0].v_type != VAR_LIST)
11495 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011496 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011497 return;
11498 }
Bram Moolenaaraff74912019-03-30 18:11:49 +010011499 if (win == NULL)
11500 return;
11501
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011502 if ((l = argvars[0].vval.v_list) != NULL)
11503 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011504 /* To some extent make sure that we are dealing with a list from
11505 * "getmatches()". */
11506 li = l->lv_first;
11507 while (li != NULL)
11508 {
11509 if (li->li_tv.v_type != VAR_DICT
11510 || (d = li->li_tv.vval.v_dict) == NULL)
11511 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011512 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011513 return;
11514 }
11515 if (!(dict_find(d, (char_u *)"group", -1) != NULL
11516 && (dict_find(d, (char_u *)"pattern", -1) != NULL
11517 || dict_find(d, (char_u *)"pos1", -1) != NULL)
11518 && dict_find(d, (char_u *)"priority", -1) != NULL
11519 && dict_find(d, (char_u *)"id", -1) != NULL))
11520 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011521 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011522 return;
11523 }
11524 li = li->li_next;
11525 }
11526
Bram Moolenaaraff74912019-03-30 18:11:49 +010011527 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011528 li = l->lv_first;
11529 while (li != NULL)
11530 {
11531 int i = 0;
Bram Moolenaar54315892019-04-26 22:33:49 +020011532 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011533 dictitem_T *di;
11534 char_u *group;
11535 int priority;
11536 int id;
11537 char_u *conceal;
11538
11539 d = li->li_tv.vval.v_dict;
11540 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
11541 {
11542 if (s == NULL)
11543 {
11544 s = list_alloc();
11545 if (s == NULL)
11546 return;
11547 }
11548
11549 /* match from matchaddpos() */
11550 for (i = 1; i < 9; i++)
11551 {
11552 sprintf((char *)buf, (char *)"pos%d", i);
11553 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
11554 {
11555 if (di->di_tv.v_type != VAR_LIST)
11556 return;
11557
11558 list_append_tv(s, &di->di_tv);
11559 s->lv_refcount++;
11560 }
11561 else
11562 break;
11563 }
11564 }
11565
Bram Moolenaar8f667172018-12-14 15:38:31 +010011566 group = dict_get_string(d, (char_u *)"group", TRUE);
11567 priority = (int)dict_get_number(d, (char_u *)"priority");
11568 id = (int)dict_get_number(d, (char_u *)"id");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011569 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar8f667172018-12-14 15:38:31 +010011570 ? dict_get_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011571 : NULL;
11572 if (i == 0)
11573 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011574 match_add(win, group,
Bram Moolenaar8f667172018-12-14 15:38:31 +010011575 dict_get_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011576 priority, id, NULL, conceal);
11577 }
11578 else
11579 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011580 match_add(win, group, NULL, priority, id, s, conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011581 list_unref(s);
11582 s = NULL;
11583 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020011584 vim_free(group);
11585 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011586
11587 li = li->li_next;
11588 }
11589 rettv->vval.v_number = 0;
11590 }
11591#endif
11592}
11593
11594/*
11595 * "setpos()" function
11596 */
11597 static void
11598f_setpos(typval_T *argvars, typval_T *rettv)
11599{
11600 pos_T pos;
11601 int fnum;
11602 char_u *name;
11603 colnr_T curswant = -1;
11604
11605 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011606 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011607 if (name != NULL)
11608 {
11609 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
11610 {
11611 if (--pos.col < 0)
11612 pos.col = 0;
11613 if (name[0] == '.' && name[1] == NUL)
11614 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011615 /* set cursor; "fnum" is ignored */
11616 curwin->w_cursor = pos;
11617 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011618 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011619 curwin->w_curswant = curswant - 1;
11620 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011621 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011622 check_cursor();
11623 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011624 }
11625 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
11626 {
11627 /* set mark */
11628 if (setmark_pos(name[1], &pos, fnum) == OK)
11629 rettv->vval.v_number = 0;
11630 }
11631 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011632 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011633 }
11634 }
11635}
11636
11637/*
11638 * "setqflist()" function
11639 */
11640 static void
11641f_setqflist(typval_T *argvars, typval_T *rettv)
11642{
Bram Moolenaard823fa92016-08-12 16:29:27 +020011643 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011644}
11645
11646/*
11647 * "setreg()" function
11648 */
11649 static void
11650f_setreg(typval_T *argvars, typval_T *rettv)
11651{
11652 int regname;
11653 char_u *strregname;
11654 char_u *stropt;
11655 char_u *strval;
11656 int append;
11657 char_u yank_type;
11658 long block_len;
11659
11660 block_len = -1;
11661 yank_type = MAUTO;
11662 append = FALSE;
11663
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011664 strregname = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011665 rettv->vval.v_number = 1; /* FAIL is default */
11666
11667 if (strregname == NULL)
11668 return; /* type error; errmsg already given */
11669 regname = *strregname;
11670 if (regname == 0 || regname == '@')
11671 regname = '"';
11672
11673 if (argvars[2].v_type != VAR_UNKNOWN)
11674 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011675 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011676 if (stropt == NULL)
11677 return; /* type error */
11678 for (; *stropt != NUL; ++stropt)
11679 switch (*stropt)
11680 {
11681 case 'a': case 'A': /* append */
11682 append = TRUE;
11683 break;
11684 case 'v': case 'c': /* character-wise selection */
11685 yank_type = MCHAR;
11686 break;
11687 case 'V': case 'l': /* line-wise selection */
11688 yank_type = MLINE;
11689 break;
11690 case 'b': case Ctrl_V: /* block-wise selection */
11691 yank_type = MBLOCK;
11692 if (VIM_ISDIGIT(stropt[1]))
11693 {
11694 ++stropt;
11695 block_len = getdigits(&stropt) - 1;
11696 --stropt;
11697 }
11698 break;
11699 }
11700 }
11701
11702 if (argvars[1].v_type == VAR_LIST)
11703 {
11704 char_u **lstval;
11705 char_u **allocval;
11706 char_u buf[NUMBUFLEN];
11707 char_u **curval;
11708 char_u **curallocval;
11709 list_T *ll = argvars[1].vval.v_list;
11710 listitem_T *li;
11711 int len;
11712
11713 /* If the list is NULL handle like an empty list. */
11714 len = ll == NULL ? 0 : ll->lv_len;
11715
11716 /* First half: use for pointers to result lines; second half: use for
11717 * pointers to allocated copies. */
11718 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
11719 if (lstval == NULL)
11720 return;
11721 curval = lstval;
11722 allocval = lstval + len + 2;
11723 curallocval = allocval;
11724
11725 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
11726 li = li->li_next)
11727 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011728 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011729 if (strval == NULL)
11730 goto free_lstval;
11731 if (strval == buf)
11732 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011733 /* Need to make a copy, next tv_get_string_buf_chk() will
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011734 * overwrite the string. */
11735 strval = vim_strsave(buf);
11736 if (strval == NULL)
11737 goto free_lstval;
11738 *curallocval++ = strval;
11739 }
11740 *curval++ = strval;
11741 }
11742 *curval++ = NULL;
11743
11744 write_reg_contents_lst(regname, lstval, -1,
11745 append, yank_type, block_len);
11746free_lstval:
11747 while (curallocval > allocval)
11748 vim_free(*--curallocval);
11749 vim_free(lstval);
11750 }
11751 else
11752 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011753 strval = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011754 if (strval == NULL)
11755 return;
11756 write_reg_contents_ex(regname, strval, -1,
11757 append, yank_type, block_len);
11758 }
11759 rettv->vval.v_number = 0;
11760}
11761
11762/*
11763 * "settabvar()" function
11764 */
11765 static void
11766f_settabvar(typval_T *argvars, typval_T *rettv)
11767{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011768 tabpage_T *save_curtab;
11769 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011770 char_u *varname, *tabvarname;
11771 typval_T *varp;
11772
11773 rettv->vval.v_number = 0;
11774
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011775 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011776 return;
11777
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011778 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
11779 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011780 varp = &argvars[2];
11781
Bram Moolenaar4033c552017-09-16 20:54:51 +020011782 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011783 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011784 save_curtab = curtab;
11785 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011786
11787 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
11788 if (tabvarname != NULL)
11789 {
11790 STRCPY(tabvarname, "t:");
11791 STRCPY(tabvarname + 2, varname);
11792 set_var(tabvarname, varp, TRUE);
11793 vim_free(tabvarname);
11794 }
11795
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011796 /* Restore current tabpage */
11797 if (valid_tabpage(save_curtab))
11798 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011799 }
11800}
11801
11802/*
11803 * "settabwinvar()" function
11804 */
11805 static void
11806f_settabwinvar(typval_T *argvars, typval_T *rettv)
11807{
11808 setwinvar(argvars, rettv, 1);
11809}
11810
11811/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011812 * "settagstack()" function
11813 */
11814 static void
11815f_settagstack(typval_T *argvars, typval_T *rettv)
11816{
11817 static char *e_invact2 = N_("E962: Invalid action: '%s'");
11818 win_T *wp;
11819 dict_T *d;
11820 int action = 'r';
11821
11822 rettv->vval.v_number = -1;
11823
11824 // first argument: window number or id
11825 wp = find_win_by_nr_or_id(&argvars[0]);
11826 if (wp == NULL)
11827 return;
11828
11829 // second argument: dict with items to set in the tag stack
11830 if (argvars[1].v_type != VAR_DICT)
11831 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011832 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011833 return;
11834 }
11835 d = argvars[1].vval.v_dict;
11836 if (d == NULL)
11837 return;
11838
11839 // third argument: action - 'a' for append and 'r' for replace.
11840 // default is to replace the stack.
11841 if (argvars[2].v_type == VAR_UNKNOWN)
11842 action = 'r';
11843 else if (argvars[2].v_type == VAR_STRING)
11844 {
11845 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011846 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011847 if (actstr == NULL)
11848 return;
11849 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
11850 action = *actstr;
11851 else
11852 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011853 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011854 return;
11855 }
11856 }
11857 else
11858 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011859 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011860 return;
11861 }
11862
11863 if (set_tagstack(wp, d, action) == OK)
11864 rettv->vval.v_number = 0;
11865}
11866
11867/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011868 * "setwinvar()" function
11869 */
11870 static void
11871f_setwinvar(typval_T *argvars, typval_T *rettv)
11872{
11873 setwinvar(argvars, rettv, 0);
11874}
11875
11876#ifdef FEAT_CRYPT
11877/*
11878 * "sha256({string})" function
11879 */
11880 static void
11881f_sha256(typval_T *argvars, typval_T *rettv)
11882{
11883 char_u *p;
11884
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011885 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011886 rettv->vval.v_string = vim_strsave(
11887 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
11888 rettv->v_type = VAR_STRING;
11889}
11890#endif /* FEAT_CRYPT */
11891
11892/*
11893 * "shellescape({string})" function
11894 */
11895 static void
11896f_shellescape(typval_T *argvars, typval_T *rettv)
11897{
Bram Moolenaar20615522017-06-05 18:46:26 +020011898 int do_special = non_zero_arg(&argvars[1]);
11899
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011900 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011901 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011902 rettv->v_type = VAR_STRING;
11903}
11904
11905/*
11906 * shiftwidth() function
11907 */
11908 static void
11909f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
11910{
Bram Moolenaarf9514162018-11-22 03:08:29 +010011911 rettv->vval.v_number = 0;
11912
11913 if (argvars[0].v_type != VAR_UNKNOWN)
11914 {
11915 long col;
11916
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011917 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +010011918 if (col < 0)
11919 return; // type error; errmsg already given
11920#ifdef FEAT_VARTABS
11921 rettv->vval.v_number = get_sw_value_col(curbuf, col);
11922 return;
11923#endif
11924 }
11925
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011926 rettv->vval.v_number = get_sw_value(curbuf);
11927}
11928
Bram Moolenaar162b7142018-12-21 15:17:36 +010011929#ifdef FEAT_SIGNS
11930/*
11931 * "sign_define()" function
11932 */
11933 static void
11934f_sign_define(typval_T *argvars, typval_T *rettv)
11935{
11936 char_u *name;
11937 dict_T *dict;
11938 char_u *icon = NULL;
11939 char_u *linehl = NULL;
11940 char_u *text = NULL;
11941 char_u *texthl = NULL;
11942
11943 rettv->vval.v_number = -1;
11944
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011945 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011946 if (name == NULL)
11947 return;
11948
11949 if (argvars[1].v_type != VAR_UNKNOWN)
11950 {
11951 if (argvars[1].v_type != VAR_DICT)
11952 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011953 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011954 return;
11955 }
11956
11957 // sign attributes
11958 dict = argvars[1].vval.v_dict;
11959 if (dict_find(dict, (char_u *)"icon", -1) != NULL)
11960 icon = dict_get_string(dict, (char_u *)"icon", TRUE);
11961 if (dict_find(dict, (char_u *)"linehl", -1) != NULL)
11962 linehl = dict_get_string(dict, (char_u *)"linehl", TRUE);
11963 if (dict_find(dict, (char_u *)"text", -1) != NULL)
11964 text = dict_get_string(dict, (char_u *)"text", TRUE);
11965 if (dict_find(dict, (char_u *)"texthl", -1) != NULL)
11966 texthl = dict_get_string(dict, (char_u *)"texthl", TRUE);
11967 }
11968
11969 if (sign_define_by_name(name, icon, linehl, text, texthl) == OK)
11970 rettv->vval.v_number = 0;
11971
11972 vim_free(icon);
11973 vim_free(linehl);
11974 vim_free(text);
11975 vim_free(texthl);
11976}
11977
11978/*
11979 * "sign_getdefined()" function
11980 */
11981 static void
11982f_sign_getdefined(typval_T *argvars, typval_T *rettv)
11983{
11984 char_u *name = NULL;
11985
11986 if (rettv_list_alloc_id(rettv, aid_sign_getdefined) != OK)
11987 return;
11988
11989 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011990 name = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011991
11992 sign_getlist(name, rettv->vval.v_list);
11993}
11994
11995/*
11996 * "sign_getplaced()" function
11997 */
11998 static void
11999f_sign_getplaced(typval_T *argvars, typval_T *rettv)
12000{
12001 buf_T *buf = NULL;
12002 dict_T *dict;
12003 dictitem_T *di;
12004 linenr_T lnum = 0;
12005 int sign_id = 0;
12006 char_u *group = NULL;
12007 int notanum = FALSE;
12008
12009 if (rettv_list_alloc_id(rettv, aid_sign_getplaced) != OK)
12010 return;
12011
12012 if (argvars[0].v_type != VAR_UNKNOWN)
12013 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012014 // get signs placed in the specified buffer
12015 buf = get_buf_arg(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012016 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012017 return;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012018
12019 if (argvars[1].v_type != VAR_UNKNOWN)
12020 {
12021 if (argvars[1].v_type != VAR_DICT ||
12022 ((dict = argvars[1].vval.v_dict) == NULL))
12023 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012024 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012025 return;
12026 }
12027 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
12028 {
12029 // get signs placed at this line
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012030 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012031 if (notanum)
12032 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012033 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012034 }
12035 if ((di = dict_find(dict, (char_u *)"id", -1)) != NULL)
12036 {
12037 // get sign placed with this identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012038 sign_id = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012039 if (notanum)
12040 return;
12041 }
12042 if ((di = dict_find(dict, (char_u *)"group", -1)) != NULL)
12043 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012044 group = tv_get_string_chk(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012045 if (group == NULL)
12046 return;
Bram Moolenaar6436cd82018-12-27 00:28:33 +010012047 if (*group == '\0') // empty string means global group
12048 group = NULL;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012049 }
12050 }
12051 }
12052
12053 sign_get_placed(buf, lnum, sign_id, group, rettv->vval.v_list);
12054}
12055
12056/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012057 * "sign_jump()" function
12058 */
12059 static void
12060f_sign_jump(typval_T *argvars, typval_T *rettv)
12061{
12062 int sign_id;
12063 char_u *sign_group = NULL;
12064 buf_T *buf;
12065 int notanum = FALSE;
12066
12067 rettv->vval.v_number = -1;
12068
Bram Moolenaarbdace832019-03-02 10:13:42 +010012069 // Sign identifier
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012070 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
12071 if (notanum)
12072 return;
12073 if (sign_id <= 0)
12074 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012075 emsg(_(e_invarg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012076 return;
12077 }
12078
12079 // Sign group
12080 sign_group = tv_get_string_chk(&argvars[1]);
12081 if (sign_group == NULL)
12082 return;
12083 if (sign_group[0] == '\0')
12084 sign_group = NULL; // global sign group
12085 else
12086 {
12087 sign_group = vim_strsave(sign_group);
12088 if (sign_group == NULL)
12089 return;
12090 }
12091
12092 // Buffer to place the sign
12093 buf = get_buf_arg(&argvars[2]);
12094 if (buf == NULL)
12095 goto cleanup;
12096
12097 rettv->vval.v_number = sign_jump(sign_id, sign_group, buf);
12098
12099cleanup:
12100 vim_free(sign_group);
12101}
12102
12103/*
Bram Moolenaar162b7142018-12-21 15:17:36 +010012104 * "sign_place()" function
12105 */
12106 static void
12107f_sign_place(typval_T *argvars, typval_T *rettv)
12108{
12109 int sign_id;
12110 char_u *group = NULL;
12111 char_u *sign_name;
12112 buf_T *buf;
12113 dict_T *dict;
12114 dictitem_T *di;
12115 linenr_T lnum = 0;
12116 int prio = SIGN_DEF_PRIO;
12117 int notanum = FALSE;
12118
12119 rettv->vval.v_number = -1;
12120
Bram Moolenaarbdace832019-03-02 10:13:42 +010012121 // Sign identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012122 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012123 if (notanum)
12124 return;
12125 if (sign_id < 0)
12126 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012127 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012128 return;
12129 }
12130
12131 // Sign group
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012132 group = tv_get_string_chk(&argvars[1]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012133 if (group == NULL)
12134 return;
12135 if (group[0] == '\0')
12136 group = NULL; // global sign group
12137 else
12138 {
12139 group = vim_strsave(group);
12140 if (group == NULL)
12141 return;
12142 }
12143
12144 // Sign name
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012145 sign_name = tv_get_string_chk(&argvars[2]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012146 if (sign_name == NULL)
12147 goto cleanup;
12148
12149 // Buffer to place the sign
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012150 buf = get_buf_arg(&argvars[3]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012151 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012152 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012153
12154 if (argvars[4].v_type != VAR_UNKNOWN)
12155 {
12156 if (argvars[4].v_type != VAR_DICT ||
12157 ((dict = argvars[4].vval.v_dict) == NULL))
12158 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012159 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012160 goto cleanup;
12161 }
12162
12163 // Line number where the sign is to be placed
12164 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
12165 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012166 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012167 if (notanum)
12168 goto cleanup;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012169 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012170 }
12171 if ((di = dict_find(dict, (char_u *)"priority", -1)) != NULL)
12172 {
12173 // Sign priority
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012174 prio = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012175 if (notanum)
12176 goto cleanup;
12177 }
12178 }
12179
12180 if (sign_place(&sign_id, group, sign_name, buf, lnum, prio) == OK)
12181 rettv->vval.v_number = sign_id;
12182
12183cleanup:
12184 vim_free(group);
12185}
12186
12187/*
12188 * "sign_undefine()" function
12189 */
12190 static void
12191f_sign_undefine(typval_T *argvars, typval_T *rettv)
12192{
12193 char_u *name;
12194
12195 rettv->vval.v_number = -1;
12196
12197 if (argvars[0].v_type == VAR_UNKNOWN)
12198 {
12199 // Free all the signs
12200 free_signs();
12201 rettv->vval.v_number = 0;
12202 }
12203 else
12204 {
12205 // Free only the specified sign
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012206 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012207 if (name == NULL)
12208 return;
12209
12210 if (sign_undefine_by_name(name) == OK)
12211 rettv->vval.v_number = 0;
12212 }
12213}
12214
12215/*
12216 * "sign_unplace()" function
12217 */
12218 static void
12219f_sign_unplace(typval_T *argvars, typval_T *rettv)
12220{
12221 dict_T *dict;
12222 dictitem_T *di;
12223 int sign_id = 0;
12224 buf_T *buf = NULL;
12225 char_u *group = NULL;
12226
12227 rettv->vval.v_number = -1;
12228
12229 if (argvars[0].v_type != VAR_STRING)
12230 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012231 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012232 return;
12233 }
12234
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012235 group = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012236 if (group[0] == '\0')
12237 group = NULL; // global sign group
12238 else
12239 {
12240 group = vim_strsave(group);
12241 if (group == NULL)
12242 return;
12243 }
12244
12245 if (argvars[1].v_type != VAR_UNKNOWN)
12246 {
12247 if (argvars[1].v_type != VAR_DICT)
12248 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012249 emsg(_(e_dictreq));
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012250 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012251 }
12252 dict = argvars[1].vval.v_dict;
12253
12254 if ((di = dict_find(dict, (char_u *)"buffer", -1)) != NULL)
12255 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012256 buf = get_buf_arg(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012257 if (buf == NULL)
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012258 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012259 }
12260 if (dict_find(dict, (char_u *)"id", -1) != NULL)
12261 sign_id = dict_get_number(dict, (char_u *)"id");
12262 }
12263
12264 if (buf == NULL)
12265 {
12266 // Delete the sign in all the buffers
12267 FOR_ALL_BUFFERS(buf)
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010012268 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012269 rettv->vval.v_number = 0;
12270 }
12271 else
12272 {
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010012273 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012274 rettv->vval.v_number = 0;
12275 }
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012276
12277cleanup:
Bram Moolenaar162b7142018-12-21 15:17:36 +010012278 vim_free(group);
12279}
12280#endif
12281
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012282/*
12283 * "simplify()" function
12284 */
12285 static void
12286f_simplify(typval_T *argvars, typval_T *rettv)
12287{
12288 char_u *p;
12289
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012290 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012291 rettv->vval.v_string = vim_strsave(p);
12292 simplify_filename(rettv->vval.v_string); /* simplify in place */
12293 rettv->v_type = VAR_STRING;
12294}
12295
12296#ifdef FEAT_FLOAT
12297/*
12298 * "sin()" function
12299 */
12300 static void
12301f_sin(typval_T *argvars, typval_T *rettv)
12302{
12303 float_T f = 0.0;
12304
12305 rettv->v_type = VAR_FLOAT;
12306 if (get_float_arg(argvars, &f) == OK)
12307 rettv->vval.v_float = sin(f);
12308 else
12309 rettv->vval.v_float = 0.0;
12310}
12311
12312/*
12313 * "sinh()" function
12314 */
12315 static void
12316f_sinh(typval_T *argvars, typval_T *rettv)
12317{
12318 float_T f = 0.0;
12319
12320 rettv->v_type = VAR_FLOAT;
12321 if (get_float_arg(argvars, &f) == OK)
12322 rettv->vval.v_float = sinh(f);
12323 else
12324 rettv->vval.v_float = 0.0;
12325}
12326#endif
12327
12328static int
12329#ifdef __BORLANDC__
12330 _RTLENTRYF
12331#endif
12332 item_compare(const void *s1, const void *s2);
12333static int
12334#ifdef __BORLANDC__
12335 _RTLENTRYF
12336#endif
12337 item_compare2(const void *s1, const void *s2);
12338
12339/* struct used in the array that's given to qsort() */
12340typedef struct
12341{
12342 listitem_T *item;
12343 int idx;
12344} sortItem_T;
12345
12346/* struct storing information about current sort */
12347typedef struct
12348{
12349 int item_compare_ic;
12350 int item_compare_numeric;
12351 int item_compare_numbers;
12352#ifdef FEAT_FLOAT
12353 int item_compare_float;
12354#endif
12355 char_u *item_compare_func;
12356 partial_T *item_compare_partial;
12357 dict_T *item_compare_selfdict;
12358 int item_compare_func_err;
12359 int item_compare_keep_zero;
12360} sortinfo_T;
12361static sortinfo_T *sortinfo = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012362#define ITEM_COMPARE_FAIL 999
12363
12364/*
12365 * Compare functions for f_sort() and f_uniq() below.
12366 */
12367 static int
12368#ifdef __BORLANDC__
12369_RTLENTRYF
12370#endif
12371item_compare(const void *s1, const void *s2)
12372{
12373 sortItem_T *si1, *si2;
12374 typval_T *tv1, *tv2;
12375 char_u *p1, *p2;
12376 char_u *tofree1 = NULL, *tofree2 = NULL;
12377 int res;
12378 char_u numbuf1[NUMBUFLEN];
12379 char_u numbuf2[NUMBUFLEN];
12380
12381 si1 = (sortItem_T *)s1;
12382 si2 = (sortItem_T *)s2;
12383 tv1 = &si1->item->li_tv;
12384 tv2 = &si2->item->li_tv;
12385
12386 if (sortinfo->item_compare_numbers)
12387 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012388 varnumber_T v1 = tv_get_number(tv1);
12389 varnumber_T v2 = tv_get_number(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012390
12391 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
12392 }
12393
12394#ifdef FEAT_FLOAT
12395 if (sortinfo->item_compare_float)
12396 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012397 float_T v1 = tv_get_float(tv1);
12398 float_T v2 = tv_get_float(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012399
12400 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
12401 }
12402#endif
12403
12404 /* tv2string() puts quotes around a string and allocates memory. Don't do
12405 * that for string variables. Use a single quote when comparing with a
12406 * non-string to do what the docs promise. */
12407 if (tv1->v_type == VAR_STRING)
12408 {
12409 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12410 p1 = (char_u *)"'";
12411 else
12412 p1 = tv1->vval.v_string;
12413 }
12414 else
12415 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
12416 if (tv2->v_type == VAR_STRING)
12417 {
12418 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12419 p2 = (char_u *)"'";
12420 else
12421 p2 = tv2->vval.v_string;
12422 }
12423 else
12424 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
12425 if (p1 == NULL)
12426 p1 = (char_u *)"";
12427 if (p2 == NULL)
12428 p2 = (char_u *)"";
12429 if (!sortinfo->item_compare_numeric)
12430 {
12431 if (sortinfo->item_compare_ic)
12432 res = STRICMP(p1, p2);
12433 else
12434 res = STRCMP(p1, p2);
12435 }
12436 else
12437 {
12438 double n1, n2;
12439 n1 = strtod((char *)p1, (char **)&p1);
12440 n2 = strtod((char *)p2, (char **)&p2);
12441 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
12442 }
12443
12444 /* When the result would be zero, compare the item indexes. Makes the
12445 * sort stable. */
12446 if (res == 0 && !sortinfo->item_compare_keep_zero)
12447 res = si1->idx > si2->idx ? 1 : -1;
12448
12449 vim_free(tofree1);
12450 vim_free(tofree2);
12451 return res;
12452}
12453
12454 static int
12455#ifdef __BORLANDC__
12456_RTLENTRYF
12457#endif
12458item_compare2(const void *s1, const void *s2)
12459{
12460 sortItem_T *si1, *si2;
12461 int res;
12462 typval_T rettv;
12463 typval_T argv[3];
12464 int dummy;
12465 char_u *func_name;
12466 partial_T *partial = sortinfo->item_compare_partial;
12467
12468 /* shortcut after failure in previous call; compare all items equal */
12469 if (sortinfo->item_compare_func_err)
12470 return 0;
12471
12472 si1 = (sortItem_T *)s1;
12473 si2 = (sortItem_T *)s2;
12474
12475 if (partial == NULL)
12476 func_name = sortinfo->item_compare_func;
12477 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012478 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012479
12480 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
12481 * in the copy without changing the original list items. */
12482 copy_tv(&si1->item->li_tv, &argv[0]);
12483 copy_tv(&si2->item->li_tv, &argv[1]);
12484
12485 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
12486 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020012487 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012488 partial, sortinfo->item_compare_selfdict);
12489 clear_tv(&argv[0]);
12490 clear_tv(&argv[1]);
12491
12492 if (res == FAIL)
12493 res = ITEM_COMPARE_FAIL;
12494 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012495 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012496 if (sortinfo->item_compare_func_err)
12497 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
12498 clear_tv(&rettv);
12499
12500 /* When the result would be zero, compare the pointers themselves. Makes
12501 * the sort stable. */
12502 if (res == 0 && !sortinfo->item_compare_keep_zero)
12503 res = si1->idx > si2->idx ? 1 : -1;
12504
12505 return res;
12506}
12507
12508/*
12509 * "sort({list})" function
12510 */
12511 static void
12512do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
12513{
12514 list_T *l;
12515 listitem_T *li;
12516 sortItem_T *ptrs;
12517 sortinfo_T *old_sortinfo;
12518 sortinfo_T info;
12519 long len;
12520 long i;
12521
12522 /* Pointer to current info struct used in compare function. Save and
12523 * restore the current one for nested calls. */
12524 old_sortinfo = sortinfo;
12525 sortinfo = &info;
12526
12527 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012528 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012529 else
12530 {
12531 l = argvars[0].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +010012532 if (l == NULL || var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012533 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
12534 TRUE))
12535 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012536 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012537
12538 len = list_len(l);
12539 if (len <= 1)
12540 goto theend; /* short list sorts pretty quickly */
12541
12542 info.item_compare_ic = FALSE;
12543 info.item_compare_numeric = FALSE;
12544 info.item_compare_numbers = FALSE;
12545#ifdef FEAT_FLOAT
12546 info.item_compare_float = FALSE;
12547#endif
12548 info.item_compare_func = NULL;
12549 info.item_compare_partial = NULL;
12550 info.item_compare_selfdict = NULL;
12551 if (argvars[1].v_type != VAR_UNKNOWN)
12552 {
12553 /* optional second argument: {func} */
12554 if (argvars[1].v_type == VAR_FUNC)
12555 info.item_compare_func = argvars[1].vval.v_string;
12556 else if (argvars[1].v_type == VAR_PARTIAL)
12557 info.item_compare_partial = argvars[1].vval.v_partial;
12558 else
12559 {
12560 int error = FALSE;
12561
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012562 i = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012563 if (error)
12564 goto theend; /* type error; errmsg already given */
12565 if (i == 1)
12566 info.item_compare_ic = TRUE;
12567 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012568 info.item_compare_func = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012569 else if (i != 0)
12570 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012571 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012572 goto theend;
12573 }
12574 if (info.item_compare_func != NULL)
12575 {
12576 if (*info.item_compare_func == NUL)
12577 {
12578 /* empty string means default sort */
12579 info.item_compare_func = NULL;
12580 }
12581 else if (STRCMP(info.item_compare_func, "n") == 0)
12582 {
12583 info.item_compare_func = NULL;
12584 info.item_compare_numeric = TRUE;
12585 }
12586 else if (STRCMP(info.item_compare_func, "N") == 0)
12587 {
12588 info.item_compare_func = NULL;
12589 info.item_compare_numbers = TRUE;
12590 }
12591#ifdef FEAT_FLOAT
12592 else if (STRCMP(info.item_compare_func, "f") == 0)
12593 {
12594 info.item_compare_func = NULL;
12595 info.item_compare_float = TRUE;
12596 }
12597#endif
12598 else if (STRCMP(info.item_compare_func, "i") == 0)
12599 {
12600 info.item_compare_func = NULL;
12601 info.item_compare_ic = TRUE;
12602 }
12603 }
12604 }
12605
12606 if (argvars[2].v_type != VAR_UNKNOWN)
12607 {
12608 /* optional third argument: {dict} */
12609 if (argvars[2].v_type != VAR_DICT)
12610 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012611 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012612 goto theend;
12613 }
12614 info.item_compare_selfdict = argvars[2].vval.v_dict;
12615 }
12616 }
12617
12618 /* Make an array with each entry pointing to an item in the List. */
12619 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
12620 if (ptrs == NULL)
12621 goto theend;
12622
12623 i = 0;
12624 if (sort)
12625 {
12626 /* sort(): ptrs will be the list to sort */
12627 for (li = l->lv_first; li != NULL; li = li->li_next)
12628 {
12629 ptrs[i].item = li;
12630 ptrs[i].idx = i;
12631 ++i;
12632 }
12633
12634 info.item_compare_func_err = FALSE;
12635 info.item_compare_keep_zero = FALSE;
12636 /* test the compare function */
12637 if ((info.item_compare_func != NULL
12638 || info.item_compare_partial != NULL)
12639 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
12640 == ITEM_COMPARE_FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012641 emsg(_("E702: Sort compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012642 else
12643 {
12644 /* Sort the array with item pointers. */
12645 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
12646 info.item_compare_func == NULL
12647 && info.item_compare_partial == NULL
12648 ? item_compare : item_compare2);
12649
12650 if (!info.item_compare_func_err)
12651 {
12652 /* Clear the List and append the items in sorted order. */
12653 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
12654 l->lv_len = 0;
12655 for (i = 0; i < len; ++i)
12656 list_append(l, ptrs[i].item);
12657 }
12658 }
12659 }
12660 else
12661 {
12662 int (*item_compare_func_ptr)(const void *, const void *);
12663
12664 /* f_uniq(): ptrs will be a stack of items to remove */
12665 info.item_compare_func_err = FALSE;
12666 info.item_compare_keep_zero = TRUE;
12667 item_compare_func_ptr = info.item_compare_func != NULL
12668 || info.item_compare_partial != NULL
12669 ? item_compare2 : item_compare;
12670
12671 for (li = l->lv_first; li != NULL && li->li_next != NULL;
12672 li = li->li_next)
12673 {
12674 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
12675 == 0)
12676 ptrs[i++].item = li;
12677 if (info.item_compare_func_err)
12678 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012679 emsg(_("E882: Uniq compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012680 break;
12681 }
12682 }
12683
12684 if (!info.item_compare_func_err)
12685 {
12686 while (--i >= 0)
12687 {
12688 li = ptrs[i].item->li_next;
12689 ptrs[i].item->li_next = li->li_next;
12690 if (li->li_next != NULL)
12691 li->li_next->li_prev = ptrs[i].item;
12692 else
12693 l->lv_last = ptrs[i].item;
12694 list_fix_watch(l, li);
12695 listitem_free(li);
12696 l->lv_len--;
12697 }
12698 }
12699 }
12700
12701 vim_free(ptrs);
12702 }
12703theend:
12704 sortinfo = old_sortinfo;
12705}
12706
12707/*
12708 * "sort({list})" function
12709 */
12710 static void
12711f_sort(typval_T *argvars, typval_T *rettv)
12712{
12713 do_sort_uniq(argvars, rettv, TRUE);
12714}
12715
12716/*
12717 * "uniq({list})" function
12718 */
12719 static void
12720f_uniq(typval_T *argvars, typval_T *rettv)
12721{
12722 do_sort_uniq(argvars, rettv, FALSE);
12723}
12724
12725/*
12726 * "soundfold({word})" function
12727 */
12728 static void
12729f_soundfold(typval_T *argvars, typval_T *rettv)
12730{
12731 char_u *s;
12732
12733 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012734 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012735#ifdef FEAT_SPELL
12736 rettv->vval.v_string = eval_soundfold(s);
12737#else
12738 rettv->vval.v_string = vim_strsave(s);
12739#endif
12740}
12741
12742/*
12743 * "spellbadword()" function
12744 */
12745 static void
12746f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
12747{
12748 char_u *word = (char_u *)"";
12749 hlf_T attr = HLF_COUNT;
12750 int len = 0;
12751
12752 if (rettv_list_alloc(rettv) == FAIL)
12753 return;
12754
12755#ifdef FEAT_SPELL
12756 if (argvars[0].v_type == VAR_UNKNOWN)
12757 {
12758 /* Find the start and length of the badly spelled word. */
12759 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
12760 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012761 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012762 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012763 curwin->w_set_curswant = TRUE;
12764 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012765 }
12766 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
12767 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012768 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012769 int capcol = -1;
12770
12771 if (str != NULL)
12772 {
12773 /* Check the argument for spelling. */
12774 while (*str != NUL)
12775 {
12776 len = spell_check(curwin, str, &attr, &capcol, FALSE);
12777 if (attr != HLF_COUNT)
12778 {
12779 word = str;
12780 break;
12781 }
12782 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020012783 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012784 }
12785 }
12786 }
12787#endif
12788
12789 list_append_string(rettv->vval.v_list, word, len);
12790 list_append_string(rettv->vval.v_list, (char_u *)(
12791 attr == HLF_SPB ? "bad" :
12792 attr == HLF_SPR ? "rare" :
12793 attr == HLF_SPL ? "local" :
12794 attr == HLF_SPC ? "caps" :
12795 ""), -1);
12796}
12797
12798/*
12799 * "spellsuggest()" function
12800 */
12801 static void
12802f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
12803{
12804#ifdef FEAT_SPELL
12805 char_u *str;
12806 int typeerr = FALSE;
12807 int maxcount;
12808 garray_T ga;
12809 int i;
12810 listitem_T *li;
12811 int need_capital = FALSE;
12812#endif
12813
12814 if (rettv_list_alloc(rettv) == FAIL)
12815 return;
12816
12817#ifdef FEAT_SPELL
12818 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
12819 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012820 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012821 if (argvars[1].v_type != VAR_UNKNOWN)
12822 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012823 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012824 if (maxcount <= 0)
12825 return;
12826 if (argvars[2].v_type != VAR_UNKNOWN)
12827 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012828 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012829 if (typeerr)
12830 return;
12831 }
12832 }
12833 else
12834 maxcount = 25;
12835
12836 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
12837
12838 for (i = 0; i < ga.ga_len; ++i)
12839 {
12840 str = ((char_u **)ga.ga_data)[i];
12841
12842 li = listitem_alloc();
12843 if (li == NULL)
12844 vim_free(str);
12845 else
12846 {
12847 li->li_tv.v_type = VAR_STRING;
12848 li->li_tv.v_lock = 0;
12849 li->li_tv.vval.v_string = str;
12850 list_append(rettv->vval.v_list, li);
12851 }
12852 }
12853 ga_clear(&ga);
12854 }
12855#endif
12856}
12857
12858 static void
12859f_split(typval_T *argvars, typval_T *rettv)
12860{
12861 char_u *str;
12862 char_u *end;
12863 char_u *pat = NULL;
12864 regmatch_T regmatch;
12865 char_u patbuf[NUMBUFLEN];
12866 char_u *save_cpo;
12867 int match;
12868 colnr_T col = 0;
12869 int keepempty = FALSE;
12870 int typeerr = FALSE;
12871
12872 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12873 save_cpo = p_cpo;
12874 p_cpo = (char_u *)"";
12875
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012876 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012877 if (argvars[1].v_type != VAR_UNKNOWN)
12878 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012879 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012880 if (pat == NULL)
12881 typeerr = TRUE;
12882 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012883 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012884 }
12885 if (pat == NULL || *pat == NUL)
12886 pat = (char_u *)"[\\x01- ]\\+";
12887
12888 if (rettv_list_alloc(rettv) == FAIL)
12889 return;
12890 if (typeerr)
12891 return;
12892
12893 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12894 if (regmatch.regprog != NULL)
12895 {
12896 regmatch.rm_ic = FALSE;
12897 while (*str != NUL || keepempty)
12898 {
12899 if (*str == NUL)
12900 match = FALSE; /* empty item at the end */
12901 else
12902 match = vim_regexec_nl(&regmatch, str, col);
12903 if (match)
12904 end = regmatch.startp[0];
12905 else
12906 end = str + STRLEN(str);
12907 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
12908 && *str != NUL && match && end < regmatch.endp[0]))
12909 {
12910 if (list_append_string(rettv->vval.v_list, str,
12911 (int)(end - str)) == FAIL)
12912 break;
12913 }
12914 if (!match)
12915 break;
Bram Moolenaar13505972019-01-24 15:04:48 +010012916 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012917 if (regmatch.endp[0] > str)
12918 col = 0;
12919 else
Bram Moolenaar13505972019-01-24 15:04:48 +010012920 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012921 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012922 str = regmatch.endp[0];
12923 }
12924
12925 vim_regfree(regmatch.regprog);
12926 }
12927
12928 p_cpo = save_cpo;
12929}
12930
12931#ifdef FEAT_FLOAT
12932/*
12933 * "sqrt()" function
12934 */
12935 static void
12936f_sqrt(typval_T *argvars, typval_T *rettv)
12937{
12938 float_T f = 0.0;
12939
12940 rettv->v_type = VAR_FLOAT;
12941 if (get_float_arg(argvars, &f) == OK)
12942 rettv->vval.v_float = sqrt(f);
12943 else
12944 rettv->vval.v_float = 0.0;
12945}
12946
12947/*
12948 * "str2float()" function
12949 */
12950 static void
12951f_str2float(typval_T *argvars, typval_T *rettv)
12952{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012953 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010012954 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012955
Bram Moolenaar08243d22017-01-10 16:12:29 +010012956 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012957 p = skipwhite(p + 1);
12958 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010012959 if (isneg)
12960 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012961 rettv->v_type = VAR_FLOAT;
12962}
12963#endif
12964
12965/*
Bram Moolenaar9d401282019-04-06 13:18:12 +020012966 * "str2list()" function
12967 */
12968 static void
12969f_str2list(typval_T *argvars, typval_T *rettv)
12970{
12971 char_u *p;
12972 int utf8 = FALSE;
12973
12974 if (rettv_list_alloc(rettv) == FAIL)
12975 return;
12976
12977 if (argvars[1].v_type != VAR_UNKNOWN)
12978 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
12979
12980 p = tv_get_string(&argvars[0]);
12981
12982 if (has_mbyte || utf8)
12983 {
12984 int (*ptr2len)(char_u *);
12985 int (*ptr2char)(char_u *);
12986
12987 if (utf8 || enc_utf8)
12988 {
12989 ptr2len = utf_ptr2len;
12990 ptr2char = utf_ptr2char;
12991 }
12992 else
12993 {
12994 ptr2len = mb_ptr2len;
12995 ptr2char = mb_ptr2char;
12996 }
12997
12998 for ( ; *p != NUL; p += (*ptr2len)(p))
12999 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
13000 }
13001 else
13002 for ( ; *p != NUL; ++p)
13003 list_append_number(rettv->vval.v_list, *p);
13004}
13005
13006/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013007 * "str2nr()" function
13008 */
13009 static void
13010f_str2nr(typval_T *argvars, typval_T *rettv)
13011{
13012 int base = 10;
13013 char_u *p;
13014 varnumber_T n;
13015 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010013016 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013017
13018 if (argvars[1].v_type != VAR_UNKNOWN)
13019 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013020 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013021 if (base != 2 && base != 8 && base != 10 && base != 16)
13022 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013023 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013024 return;
13025 }
13026 }
13027
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013028 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010013029 isneg = (*p == '-');
13030 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013031 p = skipwhite(p + 1);
13032 switch (base)
13033 {
13034 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
13035 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
13036 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
13037 default: what = 0;
13038 }
13039 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010013040 if (isneg)
13041 rettv->vval.v_number = -n;
13042 else
13043 rettv->vval.v_number = n;
13044
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013045}
13046
13047#ifdef HAVE_STRFTIME
13048/*
13049 * "strftime({format}[, {time}])" function
13050 */
13051 static void
13052f_strftime(typval_T *argvars, typval_T *rettv)
13053{
13054 char_u result_buf[256];
13055 struct tm *curtime;
13056 time_t seconds;
13057 char_u *p;
13058
13059 rettv->v_type = VAR_STRING;
13060
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013061 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013062 if (argvars[1].v_type == VAR_UNKNOWN)
13063 seconds = time(NULL);
13064 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013065 seconds = (time_t)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013066 curtime = localtime(&seconds);
13067 /* MSVC returns NULL for an invalid value of seconds. */
13068 if (curtime == NULL)
13069 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
13070 else
13071 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013072 vimconv_T conv;
13073 char_u *enc;
13074
13075 conv.vc_type = CONV_NONE;
13076 enc = enc_locale();
13077 convert_setup(&conv, p_enc, enc);
13078 if (conv.vc_type != CONV_NONE)
13079 p = string_convert(&conv, p, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013080 if (p != NULL)
13081 (void)strftime((char *)result_buf, sizeof(result_buf),
13082 (char *)p, curtime);
13083 else
13084 result_buf[0] = NUL;
13085
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013086 if (conv.vc_type != CONV_NONE)
13087 vim_free(p);
13088 convert_setup(&conv, enc, p_enc);
13089 if (conv.vc_type != CONV_NONE)
13090 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
13091 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013092 rettv->vval.v_string = vim_strsave(result_buf);
13093
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013094 /* Release conversion descriptors */
13095 convert_setup(&conv, NULL, NULL);
13096 vim_free(enc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013097 }
13098}
13099#endif
13100
13101/*
13102 * "strgetchar()" function
13103 */
13104 static void
13105f_strgetchar(typval_T *argvars, typval_T *rettv)
13106{
13107 char_u *str;
13108 int len;
13109 int error = FALSE;
13110 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +010013111 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013112
13113 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013114 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013115 if (str == NULL)
13116 return;
13117 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013118 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013119 if (error)
13120 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013121
Bram Moolenaar13505972019-01-24 15:04:48 +010013122 while (charidx >= 0 && byteidx < len)
13123 {
13124 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013125 {
Bram Moolenaar13505972019-01-24 15:04:48 +010013126 rettv->vval.v_number = mb_ptr2char(str + byteidx);
13127 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013128 }
Bram Moolenaar13505972019-01-24 15:04:48 +010013129 --charidx;
13130 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013131 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013132}
13133
13134/*
13135 * "stridx()" function
13136 */
13137 static void
13138f_stridx(typval_T *argvars, typval_T *rettv)
13139{
13140 char_u buf[NUMBUFLEN];
13141 char_u *needle;
13142 char_u *haystack;
13143 char_u *save_haystack;
13144 char_u *pos;
13145 int start_idx;
13146
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013147 needle = tv_get_string_chk(&argvars[1]);
13148 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013149 rettv->vval.v_number = -1;
13150 if (needle == NULL || haystack == NULL)
13151 return; /* type error; errmsg already given */
13152
13153 if (argvars[2].v_type != VAR_UNKNOWN)
13154 {
13155 int error = FALSE;
13156
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013157 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013158 if (error || start_idx >= (int)STRLEN(haystack))
13159 return;
13160 if (start_idx >= 0)
13161 haystack += start_idx;
13162 }
13163
13164 pos = (char_u *)strstr((char *)haystack, (char *)needle);
13165 if (pos != NULL)
13166 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
13167}
13168
13169/*
13170 * "string()" function
13171 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010013172 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013173f_string(typval_T *argvars, typval_T *rettv)
13174{
13175 char_u *tofree;
13176 char_u numbuf[NUMBUFLEN];
13177
13178 rettv->v_type = VAR_STRING;
13179 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
13180 get_copyID());
13181 /* Make a copy if we have a value but it's not in allocated memory. */
13182 if (rettv->vval.v_string != NULL && tofree == NULL)
13183 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
13184}
13185
13186/*
13187 * "strlen()" function
13188 */
13189 static void
13190f_strlen(typval_T *argvars, typval_T *rettv)
13191{
13192 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013193 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013194}
13195
13196/*
13197 * "strchars()" function
13198 */
13199 static void
13200f_strchars(typval_T *argvars, typval_T *rettv)
13201{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013202 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013203 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013204 varnumber_T len = 0;
13205 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013206
13207 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013208 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013209 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013210 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013211 else
13212 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013213 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
13214 while (*s != NUL)
13215 {
13216 func_mb_ptr2char_adv(&s);
13217 ++len;
13218 }
13219 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013220 }
13221}
13222
13223/*
13224 * "strdisplaywidth()" function
13225 */
13226 static void
13227f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
13228{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013229 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013230 int col = 0;
13231
13232 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013233 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013234
13235 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
13236}
13237
13238/*
13239 * "strwidth()" function
13240 */
13241 static void
13242f_strwidth(typval_T *argvars, typval_T *rettv)
13243{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013244 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013245
Bram Moolenaar13505972019-01-24 15:04:48 +010013246 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013247}
13248
13249/*
13250 * "strcharpart()" function
13251 */
13252 static void
13253f_strcharpart(typval_T *argvars, typval_T *rettv)
13254{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013255 char_u *p;
13256 int nchar;
13257 int nbyte = 0;
13258 int charlen;
13259 int len = 0;
13260 int slen;
13261 int error = FALSE;
13262
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013263 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013264 slen = (int)STRLEN(p);
13265
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013266 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013267 if (!error)
13268 {
13269 if (nchar > 0)
13270 while (nchar > 0 && nbyte < slen)
13271 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020013272 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013273 --nchar;
13274 }
13275 else
13276 nbyte = nchar;
13277 if (argvars[2].v_type != VAR_UNKNOWN)
13278 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013279 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013280 while (charlen > 0 && nbyte + len < slen)
13281 {
13282 int off = nbyte + len;
13283
13284 if (off < 0)
13285 len += 1;
13286 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020013287 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013288 --charlen;
13289 }
13290 }
13291 else
13292 len = slen - nbyte; /* default: all bytes that are available. */
13293 }
13294
13295 /*
13296 * Only return the overlap between the specified part and the actual
13297 * string.
13298 */
13299 if (nbyte < 0)
13300 {
13301 len += nbyte;
13302 nbyte = 0;
13303 }
13304 else if (nbyte > slen)
13305 nbyte = slen;
13306 if (len < 0)
13307 len = 0;
13308 else if (nbyte + len > slen)
13309 len = slen - nbyte;
13310
13311 rettv->v_type = VAR_STRING;
13312 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013313}
13314
13315/*
13316 * "strpart()" function
13317 */
13318 static void
13319f_strpart(typval_T *argvars, typval_T *rettv)
13320{
13321 char_u *p;
13322 int n;
13323 int len;
13324 int slen;
13325 int error = FALSE;
13326
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013327 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013328 slen = (int)STRLEN(p);
13329
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013330 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013331 if (error)
13332 len = 0;
13333 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013334 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013335 else
13336 len = slen - n; /* default len: all bytes that are available. */
13337
13338 /*
13339 * Only return the overlap between the specified part and the actual
13340 * string.
13341 */
13342 if (n < 0)
13343 {
13344 len += n;
13345 n = 0;
13346 }
13347 else if (n > slen)
13348 n = slen;
13349 if (len < 0)
13350 len = 0;
13351 else if (n + len > slen)
13352 len = slen - n;
13353
13354 rettv->v_type = VAR_STRING;
13355 rettv->vval.v_string = vim_strnsave(p + n, len);
13356}
13357
13358/*
13359 * "strridx()" function
13360 */
13361 static void
13362f_strridx(typval_T *argvars, typval_T *rettv)
13363{
13364 char_u buf[NUMBUFLEN];
13365 char_u *needle;
13366 char_u *haystack;
13367 char_u *rest;
13368 char_u *lastmatch = NULL;
13369 int haystack_len, end_idx;
13370
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013371 needle = tv_get_string_chk(&argvars[1]);
13372 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013373
13374 rettv->vval.v_number = -1;
13375 if (needle == NULL || haystack == NULL)
13376 return; /* type error; errmsg already given */
13377
13378 haystack_len = (int)STRLEN(haystack);
13379 if (argvars[2].v_type != VAR_UNKNOWN)
13380 {
13381 /* Third argument: upper limit for index */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013382 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013383 if (end_idx < 0)
13384 return; /* can never find a match */
13385 }
13386 else
13387 end_idx = haystack_len;
13388
13389 if (*needle == NUL)
13390 {
13391 /* Empty string matches past the end. */
13392 lastmatch = haystack + end_idx;
13393 }
13394 else
13395 {
13396 for (rest = haystack; *rest != '\0'; ++rest)
13397 {
13398 rest = (char_u *)strstr((char *)rest, (char *)needle);
13399 if (rest == NULL || rest > haystack + end_idx)
13400 break;
13401 lastmatch = rest;
13402 }
13403 }
13404
13405 if (lastmatch == NULL)
13406 rettv->vval.v_number = -1;
13407 else
13408 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
13409}
13410
13411/*
13412 * "strtrans()" function
13413 */
13414 static void
13415f_strtrans(typval_T *argvars, typval_T *rettv)
13416{
13417 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013418 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013419}
13420
13421/*
13422 * "submatch()" function
13423 */
13424 static void
13425f_submatch(typval_T *argvars, typval_T *rettv)
13426{
13427 int error = FALSE;
13428 int no;
13429 int retList = 0;
13430
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013431 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013432 if (error)
13433 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013434 if (no < 0 || no >= NSUBEXP)
13435 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013436 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +010013437 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013438 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013439 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013440 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013441 if (error)
13442 return;
13443
13444 if (retList == 0)
13445 {
13446 rettv->v_type = VAR_STRING;
13447 rettv->vval.v_string = reg_submatch(no);
13448 }
13449 else
13450 {
13451 rettv->v_type = VAR_LIST;
13452 rettv->vval.v_list = reg_submatch_list(no);
13453 }
13454}
13455
13456/*
13457 * "substitute()" function
13458 */
13459 static void
13460f_substitute(typval_T *argvars, typval_T *rettv)
13461{
13462 char_u patbuf[NUMBUFLEN];
13463 char_u subbuf[NUMBUFLEN];
13464 char_u flagsbuf[NUMBUFLEN];
13465
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013466 char_u *str = tv_get_string_chk(&argvars[0]);
13467 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013468 char_u *sub = NULL;
13469 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013470 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013471
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013472 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
13473 expr = &argvars[2];
13474 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013475 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013476
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013477 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013478 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
13479 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013480 rettv->vval.v_string = NULL;
13481 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013482 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013483}
13484
13485/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013486 * "swapinfo(swap_filename)" function
13487 */
13488 static void
13489f_swapinfo(typval_T *argvars, typval_T *rettv)
13490{
13491 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013492 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013493}
13494
13495/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020013496 * "swapname(expr)" function
13497 */
13498 static void
13499f_swapname(typval_T *argvars, typval_T *rettv)
13500{
13501 buf_T *buf;
13502
13503 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010013504 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +020013505 if (buf == NULL || buf->b_ml.ml_mfp == NULL
13506 || buf->b_ml.ml_mfp->mf_fname == NULL)
13507 rettv->vval.v_string = NULL;
13508 else
13509 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
13510}
13511
13512/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013513 * "synID(lnum, col, trans)" function
13514 */
13515 static void
13516f_synID(typval_T *argvars UNUSED, typval_T *rettv)
13517{
13518 int id = 0;
13519#ifdef FEAT_SYN_HL
13520 linenr_T lnum;
13521 colnr_T col;
13522 int trans;
13523 int transerr = FALSE;
13524
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013525 lnum = tv_get_lnum(argvars); /* -1 on type error */
13526 col = (linenr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
13527 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013528
13529 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13530 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
13531 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
13532#endif
13533
13534 rettv->vval.v_number = id;
13535}
13536
13537/*
13538 * "synIDattr(id, what [, mode])" function
13539 */
13540 static void
13541f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
13542{
13543 char_u *p = NULL;
13544#ifdef FEAT_SYN_HL
13545 int id;
13546 char_u *what;
13547 char_u *mode;
13548 char_u modebuf[NUMBUFLEN];
13549 int modec;
13550
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013551 id = (int)tv_get_number(&argvars[0]);
13552 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013553 if (argvars[2].v_type != VAR_UNKNOWN)
13554 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013555 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013556 modec = TOLOWER_ASC(mode[0]);
13557 if (modec != 't' && modec != 'c' && modec != 'g')
13558 modec = 0; /* replace invalid with current */
13559 }
13560 else
13561 {
13562#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
13563 if (USE_24BIT)
13564 modec = 'g';
13565 else
13566#endif
13567 if (t_colors > 1)
13568 modec = 'c';
13569 else
13570 modec = 't';
13571 }
13572
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013573 switch (TOLOWER_ASC(what[0]))
13574 {
13575 case 'b':
13576 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
13577 p = highlight_color(id, what, modec);
13578 else /* bold */
13579 p = highlight_has_attr(id, HL_BOLD, modec);
13580 break;
13581
13582 case 'f': /* fg[#] or font */
13583 p = highlight_color(id, what, modec);
13584 break;
13585
13586 case 'i':
13587 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
13588 p = highlight_has_attr(id, HL_INVERSE, modec);
13589 else /* italic */
13590 p = highlight_has_attr(id, HL_ITALIC, modec);
13591 break;
13592
13593 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020013594 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013595 break;
13596
13597 case 'r': /* reverse */
13598 p = highlight_has_attr(id, HL_INVERSE, modec);
13599 break;
13600
13601 case 's':
13602 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
13603 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020013604 /* strikeout */
13605 else if (TOLOWER_ASC(what[1]) == 't' &&
13606 TOLOWER_ASC(what[2]) == 'r')
13607 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013608 else /* standout */
13609 p = highlight_has_attr(id, HL_STANDOUT, modec);
13610 break;
13611
13612 case 'u':
13613 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
13614 /* underline */
13615 p = highlight_has_attr(id, HL_UNDERLINE, modec);
13616 else
13617 /* undercurl */
13618 p = highlight_has_attr(id, HL_UNDERCURL, modec);
13619 break;
13620 }
13621
13622 if (p != NULL)
13623 p = vim_strsave(p);
13624#endif
13625 rettv->v_type = VAR_STRING;
13626 rettv->vval.v_string = p;
13627}
13628
13629/*
13630 * "synIDtrans(id)" function
13631 */
13632 static void
13633f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
13634{
13635 int id;
13636
13637#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013638 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013639
13640 if (id > 0)
13641 id = syn_get_final_id(id);
13642 else
13643#endif
13644 id = 0;
13645
13646 rettv->vval.v_number = id;
13647}
13648
13649/*
13650 * "synconcealed(lnum, col)" function
13651 */
13652 static void
13653f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
13654{
13655#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
13656 linenr_T lnum;
13657 colnr_T col;
13658 int syntax_flags = 0;
13659 int cchar;
13660 int matchid = 0;
13661 char_u str[NUMBUFLEN];
13662#endif
13663
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013664 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013665
13666#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013667 lnum = tv_get_lnum(argvars); /* -1 on type error */
13668 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013669
13670 vim_memset(str, NUL, sizeof(str));
13671
13672 if (rettv_list_alloc(rettv) != FAIL)
13673 {
13674 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13675 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13676 && curwin->w_p_cole > 0)
13677 {
13678 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
13679 syntax_flags = get_syntax_info(&matchid);
13680
13681 /* get the conceal character */
13682 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
13683 {
13684 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020013685 if (cchar == NUL && curwin->w_p_cole == 1)
13686 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013687 if (cchar != NUL)
13688 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013689 if (has_mbyte)
13690 (*mb_char2bytes)(cchar, str);
13691 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013692 str[0] = cchar;
13693 }
13694 }
13695 }
13696
13697 list_append_number(rettv->vval.v_list,
13698 (syntax_flags & HL_CONCEAL) != 0);
13699 /* -1 to auto-determine strlen */
13700 list_append_string(rettv->vval.v_list, str, -1);
13701 list_append_number(rettv->vval.v_list, matchid);
13702 }
13703#endif
13704}
13705
13706/*
13707 * "synstack(lnum, col)" function
13708 */
13709 static void
13710f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
13711{
13712#ifdef FEAT_SYN_HL
13713 linenr_T lnum;
13714 colnr_T col;
13715 int i;
13716 int id;
13717#endif
13718
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013719 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013720
13721#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013722 lnum = tv_get_lnum(argvars); /* -1 on type error */
13723 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013724
13725 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13726 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13727 && rettv_list_alloc(rettv) != FAIL)
13728 {
13729 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
13730 for (i = 0; ; ++i)
13731 {
13732 id = syn_get_stack_item(i);
13733 if (id < 0)
13734 break;
13735 if (list_append_number(rettv->vval.v_list, id) == FAIL)
13736 break;
13737 }
13738 }
13739#endif
13740}
13741
13742 static void
13743get_cmd_output_as_rettv(
13744 typval_T *argvars,
13745 typval_T *rettv,
13746 int retlist)
13747{
13748 char_u *res = NULL;
13749 char_u *p;
13750 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013751 int err = FALSE;
13752 FILE *fd;
13753 list_T *list = NULL;
13754 int flags = SHELL_SILENT;
13755
13756 rettv->v_type = VAR_STRING;
13757 rettv->vval.v_string = NULL;
13758 if (check_restricted() || check_secure())
13759 goto errret;
13760
13761 if (argvars[1].v_type != VAR_UNKNOWN)
13762 {
13763 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010013764 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013765 * command.
13766 */
13767 if ((infile = vim_tempname('i', TRUE)) == NULL)
13768 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013769 emsg(_(e_notmp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013770 goto errret;
13771 }
13772
13773 fd = mch_fopen((char *)infile, WRITEBIN);
13774 if (fd == NULL)
13775 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013776 semsg(_(e_notopen), infile);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013777 goto errret;
13778 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010013779 if (argvars[1].v_type == VAR_NUMBER)
13780 {
13781 linenr_T lnum;
13782 buf_T *buf;
13783
13784 buf = buflist_findnr(argvars[1].vval.v_number);
13785 if (buf == NULL)
13786 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013787 semsg(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010013788 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010013789 goto errret;
13790 }
13791
13792 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
13793 {
13794 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
13795 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
13796 {
13797 err = TRUE;
13798 break;
13799 }
13800 if (putc(NL, fd) == EOF)
13801 {
13802 err = TRUE;
13803 break;
13804 }
13805 }
13806 }
13807 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013808 {
13809 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
13810 err = TRUE;
13811 }
13812 else
13813 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010013814 size_t len;
13815 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013816
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013817 p = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013818 if (p == NULL)
13819 {
13820 fclose(fd);
13821 goto errret; /* type error; errmsg already given */
13822 }
13823 len = STRLEN(p);
13824 if (len > 0 && fwrite(p, len, 1, fd) != 1)
13825 err = TRUE;
13826 }
13827 if (fclose(fd) != 0)
13828 err = TRUE;
13829 if (err)
13830 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013831 emsg(_("E677: Error writing temp file"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013832 goto errret;
13833 }
13834 }
13835
13836 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
13837 * echoes typeahead, that messes up the display. */
13838 if (!msg_silent)
13839 flags += SHELL_COOKED;
13840
13841 if (retlist)
13842 {
13843 int len;
13844 listitem_T *li;
13845 char_u *s = NULL;
13846 char_u *start;
13847 char_u *end;
13848 int i;
13849
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013850 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013851 if (res == NULL)
13852 goto errret;
13853
13854 list = list_alloc();
13855 if (list == NULL)
13856 goto errret;
13857
13858 for (i = 0; i < len; ++i)
13859 {
13860 start = res + i;
13861 while (i < len && res[i] != NL)
13862 ++i;
13863 end = res + i;
13864
13865 s = alloc((unsigned)(end - start + 1));
13866 if (s == NULL)
13867 goto errret;
13868
13869 for (p = s; start < end; ++p, ++start)
13870 *p = *start == NUL ? NL : *start;
13871 *p = NUL;
13872
13873 li = listitem_alloc();
13874 if (li == NULL)
13875 {
13876 vim_free(s);
13877 goto errret;
13878 }
13879 li->li_tv.v_type = VAR_STRING;
13880 li->li_tv.v_lock = 0;
13881 li->li_tv.vval.v_string = s;
13882 list_append(list, li);
13883 }
13884
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013885 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013886 list = NULL;
13887 }
13888 else
13889 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013890 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar00590742019-02-15 21:06:09 +010013891#ifdef USE_CRNL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013892 /* translate <CR><NL> into <NL> */
13893 if (res != NULL)
13894 {
13895 char_u *s, *d;
13896
13897 d = res;
13898 for (s = res; *s; ++s)
13899 {
13900 if (s[0] == CAR && s[1] == NL)
13901 ++s;
13902 *d++ = *s;
13903 }
13904 *d = NUL;
13905 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013906#endif
13907 rettv->vval.v_string = res;
13908 res = NULL;
13909 }
13910
13911errret:
13912 if (infile != NULL)
13913 {
13914 mch_remove(infile);
13915 vim_free(infile);
13916 }
13917 if (res != NULL)
13918 vim_free(res);
13919 if (list != NULL)
13920 list_free(list);
13921}
13922
13923/*
13924 * "system()" function
13925 */
13926 static void
13927f_system(typval_T *argvars, typval_T *rettv)
13928{
13929 get_cmd_output_as_rettv(argvars, rettv, FALSE);
13930}
13931
13932/*
13933 * "systemlist()" function
13934 */
13935 static void
13936f_systemlist(typval_T *argvars, typval_T *rettv)
13937{
13938 get_cmd_output_as_rettv(argvars, rettv, TRUE);
13939}
13940
13941/*
13942 * "tabpagebuflist()" function
13943 */
13944 static void
13945f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13946{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013947 tabpage_T *tp;
13948 win_T *wp = NULL;
13949
13950 if (argvars[0].v_type == VAR_UNKNOWN)
13951 wp = firstwin;
13952 else
13953 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013954 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013955 if (tp != NULL)
13956 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
13957 }
13958 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
13959 {
13960 for (; wp != NULL; wp = wp->w_next)
13961 if (list_append_number(rettv->vval.v_list,
13962 wp->w_buffer->b_fnum) == FAIL)
13963 break;
13964 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013965}
13966
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013967/*
13968 * "tabpagenr()" function
13969 */
13970 static void
13971f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
13972{
13973 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013974 char_u *arg;
13975
13976 if (argvars[0].v_type != VAR_UNKNOWN)
13977 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013978 arg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013979 nr = 0;
13980 if (arg != NULL)
13981 {
13982 if (STRCMP(arg, "$") == 0)
13983 nr = tabpage_index(NULL) - 1;
13984 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013985 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013986 }
13987 }
13988 else
13989 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013990 rettv->vval.v_number = nr;
13991}
13992
13993
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013994/*
13995 * Common code for tabpagewinnr() and winnr().
13996 */
13997 static int
13998get_winnr(tabpage_T *tp, typval_T *argvar)
13999{
14000 win_T *twin;
14001 int nr = 1;
14002 win_T *wp;
14003 char_u *arg;
14004
14005 twin = (tp == curtab) ? curwin : tp->tp_curwin;
14006 if (argvar->v_type != VAR_UNKNOWN)
14007 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020014008 int invalid_arg = FALSE;
14009
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014010 arg = tv_get_string_chk(argvar);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014011 if (arg == NULL)
14012 nr = 0; /* type error; errmsg already given */
14013 else if (STRCMP(arg, "$") == 0)
14014 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
14015 else if (STRCMP(arg, "#") == 0)
14016 {
14017 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
14018 if (twin == NULL)
14019 nr = 0;
14020 }
14021 else
14022 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020014023 long count;
14024 char_u *endp;
14025
14026 // Extract the window count (if specified). e.g. winnr('3j')
14027 count = strtol((char *)arg, (char **)&endp, 10);
14028 if (count <= 0)
14029 count = 1; // if count is not specified, default to 1
14030 if (endp != NULL && *endp != '\0')
14031 {
14032 if (STRCMP(endp, "j") == 0)
14033 twin = win_vert_neighbor(tp, twin, FALSE, count);
14034 else if (STRCMP(endp, "k") == 0)
14035 twin = win_vert_neighbor(tp, twin, TRUE, count);
14036 else if (STRCMP(endp, "h") == 0)
14037 twin = win_horz_neighbor(tp, twin, TRUE, count);
14038 else if (STRCMP(endp, "l") == 0)
14039 twin = win_horz_neighbor(tp, twin, FALSE, count);
14040 else
14041 invalid_arg = TRUE;
14042 }
14043 else
14044 invalid_arg = TRUE;
14045 }
14046
14047 if (invalid_arg)
14048 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014049 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014050 nr = 0;
14051 }
14052 }
14053
14054 if (nr > 0)
14055 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
14056 wp != twin; wp = wp->w_next)
14057 {
14058 if (wp == NULL)
14059 {
14060 /* didn't find it in this tabpage */
14061 nr = 0;
14062 break;
14063 }
14064 ++nr;
14065 }
14066 return nr;
14067}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014068
14069/*
14070 * "tabpagewinnr()" function
14071 */
14072 static void
14073f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
14074{
14075 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014076 tabpage_T *tp;
14077
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014078 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014079 if (tp == NULL)
14080 nr = 0;
14081 else
14082 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014083 rettv->vval.v_number = nr;
14084}
14085
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014086/*
14087 * "tagfiles()" function
14088 */
14089 static void
14090f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
14091{
14092 char_u *fname;
14093 tagname_T tn;
14094 int first;
14095
14096 if (rettv_list_alloc(rettv) == FAIL)
14097 return;
14098 fname = alloc(MAXPATHL);
14099 if (fname == NULL)
14100 return;
14101
14102 for (first = TRUE; ; first = FALSE)
14103 if (get_tagfname(&tn, first, fname) == FAIL
14104 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
14105 break;
14106 tagname_free(&tn);
14107 vim_free(fname);
14108}
14109
14110/*
14111 * "taglist()" function
14112 */
14113 static void
14114f_taglist(typval_T *argvars, typval_T *rettv)
14115{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014116 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014117 char_u *tag_pattern;
14118
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014119 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014120
14121 rettv->vval.v_number = FALSE;
14122 if (*tag_pattern == NUL)
14123 return;
14124
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014125 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014126 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014127 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014128 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014129}
14130
14131/*
14132 * "tempname()" function
14133 */
14134 static void
14135f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
14136{
14137 static int x = 'A';
14138
14139 rettv->v_type = VAR_STRING;
14140 rettv->vval.v_string = vim_tempname(x, FALSE);
14141
14142 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
14143 * names. Skip 'I' and 'O', they are used for shell redirection. */
14144 do
14145 {
14146 if (x == 'Z')
14147 x = '0';
14148 else if (x == '9')
14149 x = 'A';
14150 else
14151 {
14152#ifdef EBCDIC
14153 if (x == 'I')
14154 x = 'J';
14155 else if (x == 'R')
14156 x = 'S';
14157 else
14158#endif
14159 ++x;
14160 }
14161 } while (x == 'I' || x == 'O');
14162}
14163
14164#ifdef FEAT_FLOAT
14165/*
14166 * "tan()" function
14167 */
14168 static void
14169f_tan(typval_T *argvars, typval_T *rettv)
14170{
14171 float_T f = 0.0;
14172
14173 rettv->v_type = VAR_FLOAT;
14174 if (get_float_arg(argvars, &f) == OK)
14175 rettv->vval.v_float = tan(f);
14176 else
14177 rettv->vval.v_float = 0.0;
14178}
14179
14180/*
14181 * "tanh()" function
14182 */
14183 static void
14184f_tanh(typval_T *argvars, typval_T *rettv)
14185{
14186 float_T f = 0.0;
14187
14188 rettv->v_type = VAR_FLOAT;
14189 if (get_float_arg(argvars, &f) == OK)
14190 rettv->vval.v_float = tanh(f);
14191 else
14192 rettv->vval.v_float = 0.0;
14193}
14194#endif
14195
14196/*
14197 * "test_alloc_fail(id, countdown, repeat)" function
14198 */
14199 static void
14200f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
14201{
14202 if (argvars[0].v_type != VAR_NUMBER
14203 || argvars[0].vval.v_number <= 0
14204 || argvars[1].v_type != VAR_NUMBER
14205 || argvars[1].vval.v_number < 0
14206 || argvars[2].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014207 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014208 else
14209 {
14210 alloc_fail_id = argvars[0].vval.v_number;
14211 if (alloc_fail_id >= aid_last)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014212 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014213 alloc_fail_countdown = argvars[1].vval.v_number;
14214 alloc_fail_repeat = argvars[2].vval.v_number;
14215 did_outofmem_msg = FALSE;
14216 }
14217}
14218
14219/*
14220 * "test_autochdir()"
14221 */
14222 static void
14223f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14224{
14225#if defined(FEAT_AUTOCHDIR)
14226 test_autochdir = TRUE;
14227#endif
14228}
14229
14230/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020014231 * "test_feedinput()"
14232 */
14233 static void
14234f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
14235{
14236#ifdef USE_INPUT_BUF
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014237 char_u *val = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5e80de32017-09-03 15:48:12 +020014238
14239 if (val != NULL)
14240 {
14241 trash_input_buf();
14242 add_to_input_buf_csi(val, (int)STRLEN(val));
14243 }
14244#endif
14245}
14246
14247/*
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014248 * "test_option_not_set({name})" function
14249 */
14250 static void
14251f_test_option_not_set(typval_T *argvars, typval_T *rettv UNUSED)
14252{
14253 char_u *name = (char_u *)"";
14254
14255 if (argvars[0].v_type != VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014256 emsg(_(e_invarg));
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014257 else
14258 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014259 name = tv_get_string(&argvars[0]);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014260 if (reset_option_was_set(name) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014261 semsg(_(e_invarg2), name);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014262 }
14263}
14264
14265/*
14266 * "test_override({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014267 */
14268 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014269f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014270{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014271 char_u *name = (char_u *)"";
14272 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014273 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014274
14275 if (argvars[0].v_type != VAR_STRING
14276 || (argvars[1].v_type) != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014277 emsg(_(e_invarg));
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014278 else
14279 {
Bram Moolenaare38197d2018-12-24 23:35:13 +010014280 name = tv_get_string(&argvars[0]);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014281 val = (int)tv_get_number(&argvars[1]);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014282
14283 if (STRCMP(name, (char_u *)"redraw") == 0)
14284 disable_redraw_for_testing = val;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020014285 else if (STRCMP(name, (char_u *)"redraw_flag") == 0)
14286 ignore_redraw_flag_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014287 else if (STRCMP(name, (char_u *)"char_avail") == 0)
14288 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014289 else if (STRCMP(name, (char_u *)"starting") == 0)
14290 {
14291 if (val)
14292 {
14293 if (save_starting < 0)
14294 save_starting = starting;
14295 starting = 0;
14296 }
14297 else
14298 {
14299 starting = save_starting;
14300 save_starting = -1;
14301 }
14302 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +020014303 else if (STRCMP(name, (char_u *)"nfa_fail") == 0)
14304 nfa_fail_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014305 else if (STRCMP(name, (char_u *)"ALL") == 0)
14306 {
14307 disable_char_avail_for_testing = FALSE;
14308 disable_redraw_for_testing = FALSE;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020014309 ignore_redraw_flag_for_testing = FALSE;
Bram Moolenaarbcf94422018-06-23 14:21:42 +020014310 nfa_fail_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014311 if (save_starting >= 0)
14312 {
14313 starting = save_starting;
14314 save_starting = -1;
14315 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014316 }
14317 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014318 semsg(_(e_invarg2), name);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014319 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014320}
14321
14322/*
Bram Moolenaarc3e92c12019-03-23 14:23:07 +010014323 * "test_refcount({expr})" function
14324 */
14325 static void
14326f_test_refcount(typval_T *argvars, typval_T *rettv)
14327{
14328 int retval = -1;
14329
14330 switch (argvars[0].v_type)
14331 {
14332 case VAR_UNKNOWN:
14333 case VAR_NUMBER:
14334 case VAR_FLOAT:
14335 case VAR_SPECIAL:
14336 case VAR_STRING:
14337 break;
14338 case VAR_JOB:
14339#ifdef FEAT_JOB_CHANNEL
14340 if (argvars[0].vval.v_job != NULL)
14341 retval = argvars[0].vval.v_job->jv_refcount - 1;
14342#endif
14343 break;
14344 case VAR_CHANNEL:
14345#ifdef FEAT_JOB_CHANNEL
14346 if (argvars[0].vval.v_channel != NULL)
14347 retval = argvars[0].vval.v_channel->ch_refcount - 1;
14348#endif
14349 break;
14350 case VAR_FUNC:
14351 if (argvars[0].vval.v_string != NULL)
14352 {
14353 ufunc_T *fp;
14354
14355 fp = find_func(argvars[0].vval.v_string);
14356 if (fp != NULL)
14357 retval = fp->uf_refcount;
14358 }
14359 break;
14360 case VAR_PARTIAL:
14361 if (argvars[0].vval.v_partial != NULL)
14362 retval = argvars[0].vval.v_partial->pt_refcount - 1;
14363 break;
14364 case VAR_BLOB:
14365 if (argvars[0].vval.v_blob != NULL)
14366 retval = argvars[0].vval.v_blob->bv_refcount - 1;
14367 break;
14368 case VAR_LIST:
14369 if (argvars[0].vval.v_list != NULL)
14370 retval = argvars[0].vval.v_list->lv_refcount - 1;
14371 break;
14372 case VAR_DICT:
14373 if (argvars[0].vval.v_dict != NULL)
14374 retval = argvars[0].vval.v_dict->dv_refcount - 1;
14375 break;
14376 }
14377
14378 rettv->v_type = VAR_NUMBER;
14379 rettv->vval.v_number = retval;
14380
14381}
14382
14383/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014384 * "test_garbagecollect_now()" function
14385 */
14386 static void
14387f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14388{
14389 /* This is dangerous, any Lists and Dicts used internally may be freed
14390 * while still in use. */
14391 garbage_collect(TRUE);
14392}
14393
Bram Moolenaare0c31f62017-03-01 15:07:05 +010014394/*
14395 * "test_ignore_error()" function
14396 */
14397 static void
14398f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
14399{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014400 ignore_error_for_testing(tv_get_string(&argvars[0]));
Bram Moolenaare0c31f62017-03-01 15:07:05 +010014401}
14402
Bram Moolenaarc0f5a782019-01-13 15:16:13 +010014403 static void
14404f_test_null_blob(typval_T *argvars UNUSED, typval_T *rettv)
14405{
14406 rettv->v_type = VAR_BLOB;
14407 rettv->vval.v_blob = NULL;
14408}
14409
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014410#ifdef FEAT_JOB_CHANNEL
14411 static void
14412f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
14413{
14414 rettv->v_type = VAR_CHANNEL;
14415 rettv->vval.v_channel = NULL;
14416}
14417#endif
14418
14419 static void
14420f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
14421{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014422 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014423}
14424
14425#ifdef FEAT_JOB_CHANNEL
14426 static void
14427f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
14428{
14429 rettv->v_type = VAR_JOB;
14430 rettv->vval.v_job = NULL;
14431}
14432#endif
14433
14434 static void
14435f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
14436{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014437 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014438}
14439
14440 static void
14441f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
14442{
14443 rettv->v_type = VAR_PARTIAL;
14444 rettv->vval.v_partial = NULL;
14445}
14446
14447 static void
14448f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
14449{
14450 rettv->v_type = VAR_STRING;
14451 rettv->vval.v_string = NULL;
14452}
14453
Bram Moolenaarab186732018-09-14 21:27:06 +020014454#ifdef FEAT_GUI
14455 static void
14456f_test_scrollbar(typval_T *argvars, typval_T *rettv UNUSED)
14457{
14458 char_u *which;
14459 long value;
14460 int dragging;
14461 scrollbar_T *sb = NULL;
14462
14463 if (argvars[0].v_type != VAR_STRING
14464 || (argvars[1].v_type) != VAR_NUMBER
14465 || (argvars[2].v_type) != VAR_NUMBER)
14466 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014467 emsg(_(e_invarg));
Bram Moolenaarab186732018-09-14 21:27:06 +020014468 return;
14469 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014470 which = tv_get_string(&argvars[0]);
14471 value = tv_get_number(&argvars[1]);
14472 dragging = tv_get_number(&argvars[2]);
Bram Moolenaarab186732018-09-14 21:27:06 +020014473
14474 if (STRCMP(which, "left") == 0)
14475 sb = &curwin->w_scrollbars[SBAR_LEFT];
14476 else if (STRCMP(which, "right") == 0)
14477 sb = &curwin->w_scrollbars[SBAR_RIGHT];
14478 else if (STRCMP(which, "hor") == 0)
14479 sb = &gui.bottom_sbar;
14480 if (sb == NULL)
14481 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014482 semsg(_(e_invarg2), which);
Bram Moolenaarab186732018-09-14 21:27:06 +020014483 return;
14484 }
14485 gui_drag_scrollbar(sb, value, dragging);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020014486# ifndef USE_ON_FLY_SCROLL
14487 // need to loop through normal_cmd() to handle the scroll events
14488 exec_normal(FALSE, TRUE, FALSE);
14489# endif
Bram Moolenaarab186732018-09-14 21:27:06 +020014490}
14491#endif
14492
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014493 static void
14494f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
14495{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014496 time_for_testing = (time_t)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014497}
14498
14499#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
14500/*
14501 * Get a callback from "arg". It can be a Funcref or a function name.
14502 * When "arg" is zero return an empty string.
14503 * Return NULL for an invalid argument.
14504 */
14505 char_u *
14506get_callback(typval_T *arg, partial_T **pp)
14507{
14508 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
14509 {
14510 *pp = arg->vval.v_partial;
14511 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020014512 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014513 }
14514 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020014515 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014516 {
14517 func_ref(arg->vval.v_string);
14518 return arg->vval.v_string;
14519 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014520 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
14521 return (char_u *)"";
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014522 emsg(_("E921: Invalid callback argument"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014523 return NULL;
14524}
14525
14526/*
Bram Moolenaard5359b22018-04-05 22:44:39 +020014527 * Unref/free "callback" and "partial" returned by get_callback().
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014528 */
14529 void
14530free_callback(char_u *callback, partial_T *partial)
14531{
14532 if (partial != NULL)
14533 partial_unref(partial);
14534 else if (callback != NULL)
14535 {
14536 func_unref(callback);
14537 vim_free(callback);
14538 }
14539}
14540#endif
14541
14542#ifdef FEAT_TIMERS
14543/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014544 * "timer_info([timer])" function
14545 */
14546 static void
14547f_timer_info(typval_T *argvars, typval_T *rettv)
14548{
14549 timer_T *timer = NULL;
14550
14551 if (rettv_list_alloc(rettv) != OK)
14552 return;
14553 if (argvars[0].v_type != VAR_UNKNOWN)
14554 {
14555 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014556 emsg(_(e_number_exp));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014557 else
14558 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014559 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014560 if (timer != NULL)
14561 add_timer_info(rettv, timer);
14562 }
14563 }
14564 else
14565 add_timer_info_all(rettv);
14566}
14567
14568/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014569 * "timer_pause(timer, paused)" function
14570 */
14571 static void
14572f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
14573{
14574 timer_T *timer = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014575 int paused = (int)tv_get_number(&argvars[1]);
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014576
14577 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014578 emsg(_(e_number_exp));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014579 else
14580 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014581 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014582 if (timer != NULL)
14583 timer->tr_paused = paused;
14584 }
14585}
14586
14587/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014588 * "timer_start(time, callback [, options])" function
14589 */
14590 static void
14591f_timer_start(typval_T *argvars, typval_T *rettv)
14592{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014593 long msec = (long)tv_get_number(&argvars[0]);
Bram Moolenaar75537a92016-09-05 22:45:28 +020014594 timer_T *timer;
14595 int repeat = 0;
14596 char_u *callback;
14597 dict_T *dict;
14598 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014599
Bram Moolenaar75537a92016-09-05 22:45:28 +020014600 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014601 if (check_secure())
14602 return;
14603 if (argvars[2].v_type != VAR_UNKNOWN)
14604 {
14605 if (argvars[2].v_type != VAR_DICT
14606 || (dict = argvars[2].vval.v_dict) == NULL)
14607 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014608 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014609 return;
14610 }
14611 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014612 repeat = dict_get_number(dict, (char_u *)"repeat");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014613 }
14614
Bram Moolenaar75537a92016-09-05 22:45:28 +020014615 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014616 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020014617 return;
14618
14619 timer = create_timer(msec, repeat);
14620 if (timer == NULL)
14621 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014622 else
14623 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020014624 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020014625 timer->tr_callback = vim_strsave(callback);
14626 else
14627 /* pointer into the partial */
14628 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020014629 timer->tr_partial = partial;
14630 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014631 }
14632}
14633
14634/*
14635 * "timer_stop(timer)" function
14636 */
14637 static void
14638f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
14639{
14640 timer_T *timer;
14641
14642 if (argvars[0].v_type != VAR_NUMBER)
14643 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014644 emsg(_(e_number_exp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014645 return;
14646 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014647 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014648 if (timer != NULL)
14649 stop_timer(timer);
14650}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014651
14652/*
14653 * "timer_stopall()" function
14654 */
14655 static void
14656f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14657{
14658 stop_all_timers();
14659}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014660#endif
14661
14662/*
14663 * "tolower(string)" function
14664 */
14665 static void
14666f_tolower(typval_T *argvars, typval_T *rettv)
14667{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014668 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014669 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014670}
14671
14672/*
14673 * "toupper(string)" function
14674 */
14675 static void
14676f_toupper(typval_T *argvars, typval_T *rettv)
14677{
14678 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014679 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014680}
14681
14682/*
14683 * "tr(string, fromstr, tostr)" function
14684 */
14685 static void
14686f_tr(typval_T *argvars, typval_T *rettv)
14687{
14688 char_u *in_str;
14689 char_u *fromstr;
14690 char_u *tostr;
14691 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014692 int inlen;
14693 int fromlen;
14694 int tolen;
14695 int idx;
14696 char_u *cpstr;
14697 int cplen;
14698 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014699 char_u buf[NUMBUFLEN];
14700 char_u buf2[NUMBUFLEN];
14701 garray_T ga;
14702
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014703 in_str = tv_get_string(&argvars[0]);
14704 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
14705 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014706
14707 /* Default return value: empty string. */
14708 rettv->v_type = VAR_STRING;
14709 rettv->vval.v_string = NULL;
14710 if (fromstr == NULL || tostr == NULL)
14711 return; /* type error; errmsg already given */
14712 ga_init2(&ga, (int)sizeof(char), 80);
14713
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014714 if (!has_mbyte)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014715 /* not multi-byte: fromstr and tostr must be the same length */
14716 if (STRLEN(fromstr) != STRLEN(tostr))
14717 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014718error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014719 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014720 ga_clear(&ga);
14721 return;
14722 }
14723
14724 /* fromstr and tostr have to contain the same number of chars */
14725 while (*in_str != NUL)
14726 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014727 if (has_mbyte)
14728 {
14729 inlen = (*mb_ptr2len)(in_str);
14730 cpstr = in_str;
14731 cplen = inlen;
14732 idx = 0;
14733 for (p = fromstr; *p != NUL; p += fromlen)
14734 {
14735 fromlen = (*mb_ptr2len)(p);
14736 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
14737 {
14738 for (p = tostr; *p != NUL; p += tolen)
14739 {
14740 tolen = (*mb_ptr2len)(p);
14741 if (idx-- == 0)
14742 {
14743 cplen = tolen;
14744 cpstr = p;
14745 break;
14746 }
14747 }
14748 if (*p == NUL) /* tostr is shorter than fromstr */
14749 goto error;
14750 break;
14751 }
14752 ++idx;
14753 }
14754
14755 if (first && cpstr == in_str)
14756 {
14757 /* Check that fromstr and tostr have the same number of
14758 * (multi-byte) characters. Done only once when a character
14759 * of in_str doesn't appear in fromstr. */
14760 first = FALSE;
14761 for (p = tostr; *p != NUL; p += tolen)
14762 {
14763 tolen = (*mb_ptr2len)(p);
14764 --idx;
14765 }
14766 if (idx != 0)
14767 goto error;
14768 }
14769
14770 (void)ga_grow(&ga, cplen);
14771 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
14772 ga.ga_len += cplen;
14773
14774 in_str += inlen;
14775 }
14776 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014777 {
14778 /* When not using multi-byte chars we can do it faster. */
14779 p = vim_strchr(fromstr, *in_str);
14780 if (p != NULL)
14781 ga_append(&ga, tostr[p - fromstr]);
14782 else
14783 ga_append(&ga, *in_str);
14784 ++in_str;
14785 }
14786 }
14787
14788 /* add a terminating NUL */
14789 (void)ga_grow(&ga, 1);
14790 ga_append(&ga, NUL);
14791
14792 rettv->vval.v_string = ga.ga_data;
14793}
14794
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014795/*
14796 * "trim({expr})" function
14797 */
14798 static void
14799f_trim(typval_T *argvars, typval_T *rettv)
14800{
14801 char_u buf1[NUMBUFLEN];
14802 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014803 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014804 char_u *mask = NULL;
14805 char_u *tail;
14806 char_u *prev;
14807 char_u *p;
14808 int c1;
14809
14810 rettv->v_type = VAR_STRING;
14811 if (head == NULL)
14812 {
14813 rettv->vval.v_string = NULL;
14814 return;
14815 }
14816
14817 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014818 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014819
14820 while (*head != NUL)
14821 {
14822 c1 = PTR2CHAR(head);
14823 if (mask == NULL)
14824 {
14825 if (c1 > ' ' && c1 != 0xa0)
14826 break;
14827 }
14828 else
14829 {
14830 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14831 if (c1 == PTR2CHAR(p))
14832 break;
14833 if (*p == NUL)
14834 break;
14835 }
14836 MB_PTR_ADV(head);
14837 }
14838
14839 for (tail = head + STRLEN(head); tail > head; tail = prev)
14840 {
14841 prev = tail;
14842 MB_PTR_BACK(head, prev);
14843 c1 = PTR2CHAR(prev);
14844 if (mask == NULL)
14845 {
14846 if (c1 > ' ' && c1 != 0xa0)
14847 break;
14848 }
14849 else
14850 {
14851 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14852 if (c1 == PTR2CHAR(p))
14853 break;
14854 if (*p == NUL)
14855 break;
14856 }
14857 }
14858 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
14859}
14860
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014861#ifdef FEAT_FLOAT
14862/*
14863 * "trunc({float})" function
14864 */
14865 static void
14866f_trunc(typval_T *argvars, typval_T *rettv)
14867{
14868 float_T f = 0.0;
14869
14870 rettv->v_type = VAR_FLOAT;
14871 if (get_float_arg(argvars, &f) == OK)
14872 /* trunc() is not in C90, use floor() or ceil() instead. */
14873 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
14874 else
14875 rettv->vval.v_float = 0.0;
14876}
14877#endif
14878
14879/*
14880 * "type(expr)" function
14881 */
14882 static void
14883f_type(typval_T *argvars, typval_T *rettv)
14884{
14885 int n = -1;
14886
14887 switch (argvars[0].v_type)
14888 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020014889 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
14890 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014891 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020014892 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
14893 case VAR_LIST: n = VAR_TYPE_LIST; break;
14894 case VAR_DICT: n = VAR_TYPE_DICT; break;
14895 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014896 case VAR_SPECIAL:
14897 if (argvars[0].vval.v_number == VVAL_FALSE
14898 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020014899 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014900 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020014901 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014902 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020014903 case VAR_JOB: n = VAR_TYPE_JOB; break;
14904 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014905 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014906 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010014907 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014908 n = -1;
14909 break;
14910 }
14911 rettv->vval.v_number = n;
14912}
14913
14914/*
14915 * "undofile(name)" function
14916 */
14917 static void
14918f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
14919{
14920 rettv->v_type = VAR_STRING;
14921#ifdef FEAT_PERSISTENT_UNDO
14922 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014923 char_u *fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014924
14925 if (*fname == NUL)
14926 {
14927 /* If there is no file name there will be no undo file. */
14928 rettv->vval.v_string = NULL;
14929 }
14930 else
14931 {
14932 char_u *ffname = FullName_save(fname, FALSE);
14933
14934 if (ffname != NULL)
14935 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
14936 vim_free(ffname);
14937 }
14938 }
14939#else
14940 rettv->vval.v_string = NULL;
14941#endif
14942}
14943
14944/*
14945 * "undotree()" function
14946 */
14947 static void
14948f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
14949{
14950 if (rettv_dict_alloc(rettv) == OK)
14951 {
14952 dict_T *dict = rettv->vval.v_dict;
14953 list_T *list;
14954
Bram Moolenaare0be1672018-07-08 16:50:37 +020014955 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
14956 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
14957 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
14958 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
14959 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
14960 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014961
14962 list = list_alloc();
14963 if (list != NULL)
14964 {
14965 u_eval_tree(curbuf->b_u_oldhead, list);
14966 dict_add_list(dict, "entries", list);
14967 }
14968 }
14969}
14970
14971/*
14972 * "values(dict)" function
14973 */
14974 static void
14975f_values(typval_T *argvars, typval_T *rettv)
14976{
14977 dict_list(argvars, rettv, 1);
14978}
14979
14980/*
14981 * "virtcol(string)" function
14982 */
14983 static void
14984f_virtcol(typval_T *argvars, typval_T *rettv)
14985{
14986 colnr_T vcol = 0;
14987 pos_T *fp;
14988 int fnum = curbuf->b_fnum;
14989
14990 fp = var2fpos(&argvars[0], FALSE, &fnum);
14991 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
14992 && fnum == curbuf->b_fnum)
14993 {
14994 getvvcol(curwin, fp, NULL, NULL, &vcol);
14995 ++vcol;
14996 }
14997
14998 rettv->vval.v_number = vcol;
14999}
15000
15001/*
15002 * "visualmode()" function
15003 */
15004 static void
15005f_visualmode(typval_T *argvars, typval_T *rettv)
15006{
15007 char_u str[2];
15008
15009 rettv->v_type = VAR_STRING;
15010 str[0] = curbuf->b_visual_mode_eval;
15011 str[1] = NUL;
15012 rettv->vval.v_string = vim_strsave(str);
15013
15014 /* A non-zero number or non-empty string argument: reset mode. */
15015 if (non_zero_arg(&argvars[0]))
15016 curbuf->b_visual_mode_eval = NUL;
15017}
15018
15019/*
15020 * "wildmenumode()" function
15021 */
15022 static void
15023f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
15024{
15025#ifdef FEAT_WILDMENU
15026 if (wild_menu_showing)
15027 rettv->vval.v_number = 1;
15028#endif
15029}
15030
15031/*
15032 * "winbufnr(nr)" function
15033 */
15034 static void
15035f_winbufnr(typval_T *argvars, typval_T *rettv)
15036{
15037 win_T *wp;
15038
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015039 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015040 if (wp == NULL)
15041 rettv->vval.v_number = -1;
15042 else
15043 rettv->vval.v_number = wp->w_buffer->b_fnum;
15044}
15045
15046/*
15047 * "wincol()" function
15048 */
15049 static void
15050f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
15051{
15052 validate_cursor();
15053 rettv->vval.v_number = curwin->w_wcol + 1;
15054}
15055
15056/*
15057 * "winheight(nr)" function
15058 */
15059 static void
15060f_winheight(typval_T *argvars, typval_T *rettv)
15061{
15062 win_T *wp;
15063
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015064 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015065 if (wp == NULL)
15066 rettv->vval.v_number = -1;
15067 else
15068 rettv->vval.v_number = wp->w_height;
15069}
15070
15071/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020015072 * "winlayout()" function
15073 */
15074 static void
15075f_winlayout(typval_T *argvars, typval_T *rettv)
15076{
15077 tabpage_T *tp;
15078
15079 if (rettv_list_alloc(rettv) != OK)
15080 return;
15081
15082 if (argvars[0].v_type == VAR_UNKNOWN)
15083 tp = curtab;
15084 else
15085 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015086 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020015087 if (tp == NULL)
15088 return;
15089 }
15090
15091 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
15092}
15093
15094/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015095 * "winline()" function
15096 */
15097 static void
15098f_winline(typval_T *argvars UNUSED, typval_T *rettv)
15099{
15100 validate_cursor();
15101 rettv->vval.v_number = curwin->w_wrow + 1;
15102}
15103
15104/*
15105 * "winnr()" function
15106 */
15107 static void
15108f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
15109{
15110 int nr = 1;
15111
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015112 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015113 rettv->vval.v_number = nr;
15114}
15115
15116/*
15117 * "winrestcmd()" function
15118 */
15119 static void
15120f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
15121{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015122 win_T *wp;
15123 int winnr = 1;
15124 garray_T ga;
15125 char_u buf[50];
15126
15127 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020015128 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015129 {
15130 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
15131 ga_concat(&ga, buf);
15132 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
15133 ga_concat(&ga, buf);
15134 ++winnr;
15135 }
15136 ga_append(&ga, NUL);
15137
15138 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015139 rettv->v_type = VAR_STRING;
15140}
15141
15142/*
15143 * "winrestview()" function
15144 */
15145 static void
15146f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
15147{
15148 dict_T *dict;
15149
15150 if (argvars[0].v_type != VAR_DICT
15151 || (dict = argvars[0].vval.v_dict) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015152 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015153 else
15154 {
15155 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015156 curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015157 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015158 curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015159 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015160 curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015161 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
15162 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010015163 curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015164 curwin->w_set_curswant = FALSE;
15165 }
15166
15167 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015168 set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015169#ifdef FEAT_DIFF
15170 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015171 curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015172#endif
15173 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015174 curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015175 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015176 curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015177
15178 check_cursor();
15179 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020015180 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015181 changed_window_setting();
15182
15183 if (curwin->w_topline <= 0)
15184 curwin->w_topline = 1;
15185 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
15186 curwin->w_topline = curbuf->b_ml.ml_line_count;
15187#ifdef FEAT_DIFF
15188 check_topfill(curwin, TRUE);
15189#endif
15190 }
15191}
15192
15193/*
15194 * "winsaveview()" function
15195 */
15196 static void
15197f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
15198{
15199 dict_T *dict;
15200
15201 if (rettv_dict_alloc(rettv) == FAIL)
15202 return;
15203 dict = rettv->vval.v_dict;
15204
Bram Moolenaare0be1672018-07-08 16:50:37 +020015205 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
15206 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +020015207 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015208 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020015209 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015210
Bram Moolenaare0be1672018-07-08 16:50:37 +020015211 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015212#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020015213 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015214#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020015215 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
15216 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015217}
15218
15219/*
15220 * "winwidth(nr)" function
15221 */
15222 static void
15223f_winwidth(typval_T *argvars, typval_T *rettv)
15224{
15225 win_T *wp;
15226
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015227 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015228 if (wp == NULL)
15229 rettv->vval.v_number = -1;
15230 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015231 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015232}
15233
15234/*
15235 * "wordcount()" function
15236 */
15237 static void
15238f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
15239{
15240 if (rettv_dict_alloc(rettv) == FAIL)
15241 return;
15242 cursor_pos_info(rettv->vval.v_dict);
15243}
15244
15245/*
15246 * "writefile()" function
15247 */
15248 static void
15249f_writefile(typval_T *argvars, typval_T *rettv)
15250{
15251 int binary = FALSE;
15252 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015253#ifdef HAVE_FSYNC
15254 int do_fsync = p_fs;
15255#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015256 char_u *fname;
15257 FILE *fd;
15258 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015259 listitem_T *li;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015260 list_T *list = NULL;
15261 blob_T *blob = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015262
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015263 rettv->vval.v_number = -1;
Bram Moolenaar8c62a082019-02-08 14:34:10 +010015264 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015265 return;
15266
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015267 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015268 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015269 list = argvars[0].vval.v_list;
15270 if (list == NULL)
15271 return;
15272 for (li = list->lv_first; li != NULL; li = li->li_next)
15273 if (tv_get_string_chk(&li->li_tv) == NULL)
15274 return;
15275 }
15276 else if (argvars[0].v_type == VAR_BLOB)
15277 {
15278 blob = argvars[0].vval.v_blob;
15279 if (blob == NULL)
15280 return;
15281 }
15282 else
15283 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015284 semsg(_(e_invarg2), "writefile()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015285 return;
15286 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015287
15288 if (argvars[2].v_type != VAR_UNKNOWN)
15289 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015290 char_u *arg2 = tv_get_string_chk(&argvars[2]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015291
15292 if (arg2 == NULL)
15293 return;
15294 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015295 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015296 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015297 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015298#ifdef HAVE_FSYNC
15299 if (vim_strchr(arg2, 's') != NULL)
15300 do_fsync = TRUE;
15301 else if (vim_strchr(arg2, 'S') != NULL)
15302 do_fsync = FALSE;
15303#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015304 }
15305
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015306 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015307 if (fname == NULL)
15308 return;
15309
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015310 /* Always open the file in binary mode, library functions have a mind of
15311 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015312 if (*fname == NUL || (fd = mch_fopen((char *)fname,
15313 append ? APPENDBIN : WRITEBIN)) == NULL)
15314 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015315 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015316 ret = -1;
15317 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015318 else if (blob)
15319 {
15320 if (write_blob(fd, blob) == FAIL)
15321 ret = -1;
15322#ifdef HAVE_FSYNC
15323 else if (do_fsync)
15324 // Ignore the error, the user wouldn't know what to do about it.
15325 // May happen for a device.
Bram Moolenaara7870192019-02-14 12:56:36 +010015326 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015327#endif
15328 fclose(fd);
15329 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015330 else
15331 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015332 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015333 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015334#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010015335 else if (do_fsync)
15336 /* Ignore the error, the user wouldn't know what to do about it.
15337 * May happen for a device. */
Bram Moolenaara7870192019-02-14 12:56:36 +010015338 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015339#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015340 fclose(fd);
15341 }
15342
15343 rettv->vval.v_number = ret;
15344}
15345
15346/*
15347 * "xor(expr, expr)" function
15348 */
15349 static void
15350f_xor(typval_T *argvars, typval_T *rettv)
15351{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015352 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
15353 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015354}
15355
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015356#endif /* FEAT_EVAL */