blob: 0e00a0a41219e1af2fd28feaa80ac659d94a74ff [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);
265static void f_localtime(typval_T *argvars, typval_T *rettv);
266#ifdef FEAT_FLOAT
267static void f_log(typval_T *argvars, typval_T *rettv);
268static void f_log10(typval_T *argvars, typval_T *rettv);
269#endif
270#ifdef FEAT_LUA
271static void f_luaeval(typval_T *argvars, typval_T *rettv);
272#endif
273static void f_map(typval_T *argvars, typval_T *rettv);
274static void f_maparg(typval_T *argvars, typval_T *rettv);
275static void f_mapcheck(typval_T *argvars, typval_T *rettv);
276static void f_match(typval_T *argvars, typval_T *rettv);
277static void f_matchadd(typval_T *argvars, typval_T *rettv);
278static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
279static void f_matcharg(typval_T *argvars, typval_T *rettv);
280static void f_matchdelete(typval_T *argvars, typval_T *rettv);
281static void f_matchend(typval_T *argvars, typval_T *rettv);
282static void f_matchlist(typval_T *argvars, typval_T *rettv);
283static void f_matchstr(typval_T *argvars, typval_T *rettv);
284static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
285static void f_max(typval_T *argvars, typval_T *rettv);
286static void f_min(typval_T *argvars, typval_T *rettv);
287#ifdef vim_mkdir
288static void f_mkdir(typval_T *argvars, typval_T *rettv);
289#endif
290static void f_mode(typval_T *argvars, typval_T *rettv);
291#ifdef FEAT_MZSCHEME
292static void f_mzeval(typval_T *argvars, typval_T *rettv);
293#endif
294static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
295static void f_nr2char(typval_T *argvars, typval_T *rettv);
296static void f_or(typval_T *argvars, typval_T *rettv);
297static void f_pathshorten(typval_T *argvars, typval_T *rettv);
298#ifdef FEAT_PERL
299static void f_perleval(typval_T *argvars, typval_T *rettv);
300#endif
301#ifdef FEAT_FLOAT
302static void f_pow(typval_T *argvars, typval_T *rettv);
303#endif
304static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
305static void f_printf(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200306#ifdef FEAT_JOB_CHANNEL
307static void f_prompt_setcallback(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200308static void f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200309static void f_prompt_setprompt(typval_T *argvars, typval_T *rettv);
310#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200311static void f_pumvisible(typval_T *argvars, typval_T *rettv);
312#ifdef FEAT_PYTHON3
313static void f_py3eval(typval_T *argvars, typval_T *rettv);
314#endif
315#ifdef FEAT_PYTHON
316static void f_pyeval(typval_T *argvars, typval_T *rettv);
317#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100318#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
319static void f_pyxeval(typval_T *argvars, typval_T *rettv);
320#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200321static void f_range(typval_T *argvars, typval_T *rettv);
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200322static void f_readdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200323static void f_readfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200324static void f_reg_executing(typval_T *argvars, typval_T *rettv);
325static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200326static void f_reltime(typval_T *argvars, typval_T *rettv);
327#ifdef FEAT_FLOAT
328static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
329#endif
330static void f_reltimestr(typval_T *argvars, typval_T *rettv);
331static void f_remote_expr(typval_T *argvars, typval_T *rettv);
332static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
333static void f_remote_peek(typval_T *argvars, typval_T *rettv);
334static void f_remote_read(typval_T *argvars, typval_T *rettv);
335static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100336static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200337static void f_remove(typval_T *argvars, typval_T *rettv);
338static void f_rename(typval_T *argvars, typval_T *rettv);
339static void f_repeat(typval_T *argvars, typval_T *rettv);
340static void f_resolve(typval_T *argvars, typval_T *rettv);
341static void f_reverse(typval_T *argvars, typval_T *rettv);
342#ifdef FEAT_FLOAT
343static void f_round(typval_T *argvars, typval_T *rettv);
344#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100345#ifdef FEAT_RUBY
346static void f_rubyeval(typval_T *argvars, typval_T *rettv);
347#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200348static void f_screenattr(typval_T *argvars, typval_T *rettv);
349static void f_screenchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100350static void f_screenchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200351static void f_screencol(typval_T *argvars, typval_T *rettv);
352static void f_screenrow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100353static void f_screenstring(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200354static void f_search(typval_T *argvars, typval_T *rettv);
355static void f_searchdecl(typval_T *argvars, typval_T *rettv);
356static void f_searchpair(typval_T *argvars, typval_T *rettv);
357static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
358static void f_searchpos(typval_T *argvars, typval_T *rettv);
359static void f_server2client(typval_T *argvars, typval_T *rettv);
360static void f_serverlist(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200361static void f_setbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200362static void f_setbufvar(typval_T *argvars, typval_T *rettv);
363static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
364static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
365static void f_setfperm(typval_T *argvars, typval_T *rettv);
366static void f_setline(typval_T *argvars, typval_T *rettv);
367static void f_setloclist(typval_T *argvars, typval_T *rettv);
368static void f_setmatches(typval_T *argvars, typval_T *rettv);
369static void f_setpos(typval_T *argvars, typval_T *rettv);
370static void f_setqflist(typval_T *argvars, typval_T *rettv);
371static void f_setreg(typval_T *argvars, typval_T *rettv);
372static void f_settabvar(typval_T *argvars, typval_T *rettv);
373static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100374static void f_settagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200375static void f_setwinvar(typval_T *argvars, typval_T *rettv);
376#ifdef FEAT_CRYPT
377static void f_sha256(typval_T *argvars, typval_T *rettv);
378#endif /* FEAT_CRYPT */
379static void f_shellescape(typval_T *argvars, typval_T *rettv);
380static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
Bram Moolenaar162b7142018-12-21 15:17:36 +0100381#ifdef FEAT_SIGNS
382static void f_sign_define(typval_T *argvars, typval_T *rettv);
383static void f_sign_getdefined(typval_T *argvars, typval_T *rettv);
384static void f_sign_getplaced(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100385static void f_sign_jump(typval_T *argvars, typval_T *rettv);
Bram Moolenaar162b7142018-12-21 15:17:36 +0100386static void f_sign_place(typval_T *argvars, typval_T *rettv);
387static void f_sign_undefine(typval_T *argvars, typval_T *rettv);
388static void f_sign_unplace(typval_T *argvars, typval_T *rettv);
389#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200390static void f_simplify(typval_T *argvars, typval_T *rettv);
391#ifdef FEAT_FLOAT
392static void f_sin(typval_T *argvars, typval_T *rettv);
393static void f_sinh(typval_T *argvars, typval_T *rettv);
394#endif
395static void f_sort(typval_T *argvars, typval_T *rettv);
396static void f_soundfold(typval_T *argvars, typval_T *rettv);
397static void f_spellbadword(typval_T *argvars, typval_T *rettv);
398static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
399static void f_split(typval_T *argvars, typval_T *rettv);
400#ifdef FEAT_FLOAT
401static void f_sqrt(typval_T *argvars, typval_T *rettv);
402static void f_str2float(typval_T *argvars, typval_T *rettv);
403#endif
404static void f_str2nr(typval_T *argvars, typval_T *rettv);
405static void f_strchars(typval_T *argvars, typval_T *rettv);
406#ifdef HAVE_STRFTIME
407static void f_strftime(typval_T *argvars, typval_T *rettv);
408#endif
409static void f_strgetchar(typval_T *argvars, typval_T *rettv);
410static void f_stridx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200411static void f_strlen(typval_T *argvars, typval_T *rettv);
412static void f_strcharpart(typval_T *argvars, typval_T *rettv);
413static void f_strpart(typval_T *argvars, typval_T *rettv);
414static void f_strridx(typval_T *argvars, typval_T *rettv);
415static void f_strtrans(typval_T *argvars, typval_T *rettv);
416static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
417static void f_strwidth(typval_T *argvars, typval_T *rettv);
418static void f_submatch(typval_T *argvars, typval_T *rettv);
419static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200420static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200421static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200422static void f_synID(typval_T *argvars, typval_T *rettv);
423static void f_synIDattr(typval_T *argvars, typval_T *rettv);
424static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
425static void f_synstack(typval_T *argvars, typval_T *rettv);
426static void f_synconcealed(typval_T *argvars, typval_T *rettv);
427static void f_system(typval_T *argvars, typval_T *rettv);
428static void f_systemlist(typval_T *argvars, typval_T *rettv);
429static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
430static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
431static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
432static void f_taglist(typval_T *argvars, typval_T *rettv);
433static void f_tagfiles(typval_T *argvars, typval_T *rettv);
434static void f_tempname(typval_T *argvars, typval_T *rettv);
435static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
436static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200437static void f_test_feedinput(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200438static void f_test_option_not_set(typval_T *argvars, typval_T *rettv);
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100439static void f_test_override(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc3e92c12019-03-23 14:23:07 +0100440static void f_test_refcount(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200441static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100442static void f_test_ignore_error(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100443static void f_test_null_blob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200444#ifdef FEAT_JOB_CHANNEL
445static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
446#endif
447static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
448#ifdef FEAT_JOB_CHANNEL
449static void f_test_null_job(typval_T *argvars, typval_T *rettv);
450#endif
451static void f_test_null_list(typval_T *argvars, typval_T *rettv);
452static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
453static void f_test_null_string(typval_T *argvars, typval_T *rettv);
Bram Moolenaarab186732018-09-14 21:27:06 +0200454#ifdef FEAT_GUI
455static void f_test_scrollbar(typval_T *argvars, typval_T *rettv);
456#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200457static void f_test_settime(typval_T *argvars, typval_T *rettv);
458#ifdef FEAT_FLOAT
459static void f_tan(typval_T *argvars, typval_T *rettv);
460static void f_tanh(typval_T *argvars, typval_T *rettv);
461#endif
462#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200463static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200464static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200465static void f_timer_start(typval_T *argvars, typval_T *rettv);
466static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200467static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200468#endif
469static void f_tolower(typval_T *argvars, typval_T *rettv);
470static void f_toupper(typval_T *argvars, typval_T *rettv);
471static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100472static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200473#ifdef FEAT_FLOAT
474static void f_trunc(typval_T *argvars, typval_T *rettv);
475#endif
476static void f_type(typval_T *argvars, typval_T *rettv);
477static void f_undofile(typval_T *argvars, typval_T *rettv);
478static void f_undotree(typval_T *argvars, typval_T *rettv);
479static void f_uniq(typval_T *argvars, typval_T *rettv);
480static void f_values(typval_T *argvars, typval_T *rettv);
481static void f_virtcol(typval_T *argvars, typval_T *rettv);
482static void f_visualmode(typval_T *argvars, typval_T *rettv);
483static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
484static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
485static void f_win_getid(typval_T *argvars, typval_T *rettv);
486static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
487static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
488static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100489static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200490static void f_winbufnr(typval_T *argvars, typval_T *rettv);
491static void f_wincol(typval_T *argvars, typval_T *rettv);
492static void f_winheight(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200493static void f_winlayout(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200494static void f_winline(typval_T *argvars, typval_T *rettv);
495static void f_winnr(typval_T *argvars, typval_T *rettv);
496static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
497static void f_winrestview(typval_T *argvars, typval_T *rettv);
498static void f_winsaveview(typval_T *argvars, typval_T *rettv);
499static void f_winwidth(typval_T *argvars, typval_T *rettv);
500static void f_writefile(typval_T *argvars, typval_T *rettv);
501static void f_wordcount(typval_T *argvars, typval_T *rettv);
502static void f_xor(typval_T *argvars, typval_T *rettv);
503
504/*
505 * Array with names and number of arguments of all internal functions
506 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
507 */
508static struct fst
509{
510 char *f_name; /* function name */
511 char f_min_argc; /* minimal number of arguments */
512 char f_max_argc; /* maximal number of arguments */
513 void (*f_func)(typval_T *args, typval_T *rvar);
514 /* implementation of function */
515} functions[] =
516{
517#ifdef FEAT_FLOAT
518 {"abs", 1, 1, f_abs},
519 {"acos", 1, 1, f_acos}, /* WJMc */
520#endif
521 {"add", 2, 2, f_add},
522 {"and", 2, 2, f_and},
523 {"append", 2, 2, f_append},
Bram Moolenaarca851592018-06-06 21:04:07 +0200524 {"appendbufline", 3, 3, f_appendbufline},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200525 {"argc", 0, 1, f_argc},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200526 {"argidx", 0, 0, f_argidx},
527 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200528 {"argv", 0, 2, f_argv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200529#ifdef FEAT_FLOAT
530 {"asin", 1, 1, f_asin}, /* WJMc */
531#endif
Bram Moolenaarb48e96f2018-02-13 12:26:14 +0100532 {"assert_beeps", 1, 2, f_assert_beeps},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200533 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaard96ff162018-02-18 22:13:29 +0100534 {"assert_equalfile", 2, 2, f_assert_equalfile},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200535 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaar1307d1c2018-10-07 20:16:49 +0200536 {"assert_fails", 1, 3, f_assert_fails},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200537 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100538 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200539 {"assert_match", 2, 3, f_assert_match},
540 {"assert_notequal", 2, 3, f_assert_notequal},
541 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100542 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200543 {"assert_true", 1, 2, f_assert_true},
544#ifdef FEAT_FLOAT
545 {"atan", 1, 1, f_atan},
546 {"atan2", 2, 2, f_atan2},
547#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100548#ifdef FEAT_BEVAL
549 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100550# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100551 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100552# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100553#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200554 {"browse", 4, 4, f_browse},
555 {"browsedir", 2, 2, f_browsedir},
556 {"bufexists", 1, 1, f_bufexists},
557 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
558 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
559 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
560 {"buflisted", 1, 1, f_buflisted},
561 {"bufloaded", 1, 1, f_bufloaded},
562 {"bufname", 1, 1, f_bufname},
563 {"bufnr", 1, 2, f_bufnr},
564 {"bufwinid", 1, 1, f_bufwinid},
565 {"bufwinnr", 1, 1, f_bufwinnr},
566 {"byte2line", 1, 1, f_byte2line},
567 {"byteidx", 2, 2, f_byteidx},
568 {"byteidxcomp", 2, 2, f_byteidxcomp},
569 {"call", 2, 3, f_call},
570#ifdef FEAT_FLOAT
571 {"ceil", 1, 1, f_ceil},
572#endif
573#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100574 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200575 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200576 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200577 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
578 {"ch_evalraw", 2, 3, f_ch_evalraw},
579 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
580 {"ch_getjob", 1, 1, f_ch_getjob},
581 {"ch_info", 1, 1, f_ch_info},
582 {"ch_log", 1, 2, f_ch_log},
583 {"ch_logfile", 1, 2, f_ch_logfile},
584 {"ch_open", 1, 2, f_ch_open},
585 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100586 {"ch_readblob", 1, 2, f_ch_readblob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200587 {"ch_readraw", 1, 2, f_ch_readraw},
588 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
589 {"ch_sendraw", 2, 3, f_ch_sendraw},
590 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200591 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200592#endif
593 {"changenr", 0, 0, f_changenr},
594 {"char2nr", 1, 2, f_char2nr},
595 {"cindent", 1, 1, f_cindent},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100596 {"clearmatches", 0, 1, f_clearmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200597 {"col", 1, 1, f_col},
598#if defined(FEAT_INS_EXPAND)
599 {"complete", 2, 2, f_complete},
600 {"complete_add", 1, 1, f_complete_add},
601 {"complete_check", 0, 0, f_complete_check},
Bram Moolenaarfd133322019-03-29 12:20:27 +0100602 {"complete_info", 0, 1, f_complete_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200603#endif
604 {"confirm", 1, 4, f_confirm},
605 {"copy", 1, 1, f_copy},
606#ifdef FEAT_FLOAT
607 {"cos", 1, 1, f_cos},
608 {"cosh", 1, 1, f_cosh},
609#endif
610 {"count", 2, 4, f_count},
611 {"cscope_connection",0,3, f_cscope_connection},
612 {"cursor", 1, 3, f_cursor},
Bram Moolenaar4f974752019-02-17 17:44:42 +0100613#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200614 {"debugbreak", 1, 1, f_debugbreak},
615#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200616 {"deepcopy", 1, 2, f_deepcopy},
617 {"delete", 1, 2, f_delete},
Bram Moolenaard79a2622018-06-07 18:17:46 +0200618 {"deletebufline", 2, 3, f_deletebufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200619 {"did_filetype", 0, 0, f_did_filetype},
620 {"diff_filler", 1, 1, f_diff_filler},
621 {"diff_hlID", 2, 2, f_diff_hlID},
622 {"empty", 1, 1, f_empty},
623 {"escape", 2, 2, f_escape},
624 {"eval", 1, 1, f_eval},
625 {"eventhandler", 0, 0, f_eventhandler},
626 {"executable", 1, 1, f_executable},
627 {"execute", 1, 2, f_execute},
628 {"exepath", 1, 1, f_exepath},
629 {"exists", 1, 1, f_exists},
630#ifdef FEAT_FLOAT
631 {"exp", 1, 1, f_exp},
632#endif
633 {"expand", 1, 3, f_expand},
634 {"extend", 2, 3, f_extend},
635 {"feedkeys", 1, 2, f_feedkeys},
636 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
637 {"filereadable", 1, 1, f_filereadable},
638 {"filewritable", 1, 1, f_filewritable},
639 {"filter", 2, 2, f_filter},
640 {"finddir", 1, 3, f_finddir},
641 {"findfile", 1, 3, f_findfile},
642#ifdef FEAT_FLOAT
643 {"float2nr", 1, 1, f_float2nr},
644 {"floor", 1, 1, f_floor},
645 {"fmod", 2, 2, f_fmod},
646#endif
647 {"fnameescape", 1, 1, f_fnameescape},
648 {"fnamemodify", 2, 2, f_fnamemodify},
649 {"foldclosed", 1, 1, f_foldclosed},
650 {"foldclosedend", 1, 1, f_foldclosedend},
651 {"foldlevel", 1, 1, f_foldlevel},
652 {"foldtext", 0, 0, f_foldtext},
653 {"foldtextresult", 1, 1, f_foldtextresult},
654 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200655 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200656 {"function", 1, 3, f_function},
657 {"garbagecollect", 0, 1, f_garbagecollect},
658 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200659 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200660 {"getbufline", 2, 3, f_getbufline},
661 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100662 {"getchangelist", 1, 1, f_getchangelist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200663 {"getchar", 0, 1, f_getchar},
664 {"getcharmod", 0, 0, f_getcharmod},
665 {"getcharsearch", 0, 0, f_getcharsearch},
666 {"getcmdline", 0, 0, f_getcmdline},
667 {"getcmdpos", 0, 0, f_getcmdpos},
668 {"getcmdtype", 0, 0, f_getcmdtype},
669 {"getcmdwintype", 0, 0, f_getcmdwintype},
670#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200671 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200672#endif
673 {"getcurpos", 0, 0, f_getcurpos},
674 {"getcwd", 0, 2, f_getcwd},
675 {"getfontname", 0, 1, f_getfontname},
676 {"getfperm", 1, 1, f_getfperm},
677 {"getfsize", 1, 1, f_getfsize},
678 {"getftime", 1, 1, f_getftime},
679 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100680 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200681 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200682 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100683 {"getmatches", 0, 1, f_getmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200684 {"getpid", 0, 0, f_getpid},
685 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200686 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200687 {"getreg", 0, 3, f_getreg},
688 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200689 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200690 {"gettabvar", 2, 3, f_gettabvar},
691 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100692 {"gettagstack", 0, 1, f_gettagstack},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200693 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100694 {"getwinpos", 0, 1, f_getwinpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200695 {"getwinposx", 0, 0, f_getwinposx},
696 {"getwinposy", 0, 0, f_getwinposy},
697 {"getwinvar", 2, 3, f_getwinvar},
698 {"glob", 1, 4, f_glob},
699 {"glob2regpat", 1, 1, f_glob2regpat},
700 {"globpath", 2, 5, f_globpath},
701 {"has", 1, 1, f_has},
702 {"has_key", 2, 2, f_has_key},
703 {"haslocaldir", 0, 2, f_haslocaldir},
704 {"hasmapto", 1, 3, f_hasmapto},
705 {"highlightID", 1, 1, f_hlID}, /* obsolete */
706 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
707 {"histadd", 2, 2, f_histadd},
708 {"histdel", 1, 2, f_histdel},
709 {"histget", 1, 2, f_histget},
710 {"histnr", 1, 1, f_histnr},
711 {"hlID", 1, 1, f_hlID},
712 {"hlexists", 1, 1, f_hlexists},
713 {"hostname", 0, 0, f_hostname},
714 {"iconv", 3, 3, f_iconv},
715 {"indent", 1, 1, f_indent},
716 {"index", 2, 4, f_index},
717 {"input", 1, 3, f_input},
718 {"inputdialog", 1, 3, f_inputdialog},
719 {"inputlist", 1, 1, f_inputlist},
720 {"inputrestore", 0, 0, f_inputrestore},
721 {"inputsave", 0, 0, f_inputsave},
722 {"inputsecret", 1, 2, f_inputsecret},
723 {"insert", 2, 3, f_insert},
724 {"invert", 1, 1, f_invert},
725 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200726#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
727 {"isinf", 1, 1, f_isinf},
728#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200729 {"islocked", 1, 1, f_islocked},
730#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
731 {"isnan", 1, 1, f_isnan},
732#endif
733 {"items", 1, 1, f_items},
734#ifdef FEAT_JOB_CHANNEL
735 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200736 {"job_info", 0, 1, f_job_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200737 {"job_setoptions", 2, 2, f_job_setoptions},
738 {"job_start", 1, 2, f_job_start},
739 {"job_status", 1, 1, f_job_status},
740 {"job_stop", 1, 2, f_job_stop},
741#endif
742 {"join", 1, 2, f_join},
743 {"js_decode", 1, 1, f_js_decode},
744 {"js_encode", 1, 1, f_js_encode},
745 {"json_decode", 1, 1, f_json_decode},
746 {"json_encode", 1, 1, f_json_encode},
747 {"keys", 1, 1, f_keys},
748 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
749 {"len", 1, 1, f_len},
750 {"libcall", 3, 3, f_libcall},
751 {"libcallnr", 3, 3, f_libcallnr},
752 {"line", 1, 1, f_line},
753 {"line2byte", 1, 1, f_line2byte},
754 {"lispindent", 1, 1, f_lispindent},
755 {"localtime", 0, 0, f_localtime},
756#ifdef FEAT_FLOAT
757 {"log", 1, 1, f_log},
758 {"log10", 1, 1, f_log10},
759#endif
760#ifdef FEAT_LUA
761 {"luaeval", 1, 2, f_luaeval},
762#endif
763 {"map", 2, 2, f_map},
764 {"maparg", 1, 4, f_maparg},
765 {"mapcheck", 1, 3, f_mapcheck},
766 {"match", 2, 4, f_match},
767 {"matchadd", 2, 5, f_matchadd},
768 {"matchaddpos", 2, 5, f_matchaddpos},
769 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100770 {"matchdelete", 1, 2, f_matchdelete},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200771 {"matchend", 2, 4, f_matchend},
772 {"matchlist", 2, 4, f_matchlist},
773 {"matchstr", 2, 4, f_matchstr},
774 {"matchstrpos", 2, 4, f_matchstrpos},
775 {"max", 1, 1, f_max},
776 {"min", 1, 1, f_min},
777#ifdef vim_mkdir
778 {"mkdir", 1, 3, f_mkdir},
779#endif
780 {"mode", 0, 1, f_mode},
781#ifdef FEAT_MZSCHEME
782 {"mzeval", 1, 1, f_mzeval},
783#endif
784 {"nextnonblank", 1, 1, f_nextnonblank},
785 {"nr2char", 1, 2, f_nr2char},
786 {"or", 2, 2, f_or},
787 {"pathshorten", 1, 1, f_pathshorten},
788#ifdef FEAT_PERL
789 {"perleval", 1, 1, f_perleval},
790#endif
791#ifdef FEAT_FLOAT
792 {"pow", 2, 2, f_pow},
793#endif
794 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100795 {"printf", 1, 19, f_printf},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200796#ifdef FEAT_JOB_CHANNEL
797 {"prompt_setcallback", 2, 2, f_prompt_setcallback},
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200798 {"prompt_setinterrupt", 2, 2, f_prompt_setinterrupt},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200799 {"prompt_setprompt", 2, 2, f_prompt_setprompt},
800#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100801#ifdef FEAT_TEXT_PROP
802 {"prop_add", 3, 3, f_prop_add},
803 {"prop_clear", 1, 3, f_prop_clear},
804 {"prop_list", 1, 2, f_prop_list},
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100805 {"prop_remove", 1, 3, f_prop_remove},
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100806 {"prop_type_add", 2, 2, f_prop_type_add},
807 {"prop_type_change", 2, 2, f_prop_type_change},
808 {"prop_type_delete", 1, 2, f_prop_type_delete},
809 {"prop_type_get", 1, 2, f_prop_type_get},
810 {"prop_type_list", 0, 1, f_prop_type_list},
811#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200812 {"pumvisible", 0, 0, f_pumvisible},
813#ifdef FEAT_PYTHON3
814 {"py3eval", 1, 1, f_py3eval},
815#endif
816#ifdef FEAT_PYTHON
817 {"pyeval", 1, 1, f_pyeval},
818#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100819#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
820 {"pyxeval", 1, 1, f_pyxeval},
821#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200822 {"range", 1, 3, f_range},
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200823 {"readdir", 1, 2, f_readdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200824 {"readfile", 1, 3, f_readfile},
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200825 {"reg_executing", 0, 0, f_reg_executing},
826 {"reg_recording", 0, 0, f_reg_recording},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200827 {"reltime", 0, 2, f_reltime},
828#ifdef FEAT_FLOAT
829 {"reltimefloat", 1, 1, f_reltimefloat},
830#endif
831 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100832 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200833 {"remote_foreground", 1, 1, f_remote_foreground},
834 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100835 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200836 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100837 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200838 {"remove", 2, 3, f_remove},
839 {"rename", 2, 2, f_rename},
840 {"repeat", 2, 2, f_repeat},
841 {"resolve", 1, 1, f_resolve},
842 {"reverse", 1, 1, f_reverse},
843#ifdef FEAT_FLOAT
844 {"round", 1, 1, f_round},
845#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100846#ifdef FEAT_RUBY
847 {"rubyeval", 1, 1, f_rubyeval},
848#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200849 {"screenattr", 2, 2, f_screenattr},
850 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100851 {"screenchars", 2, 2, f_screenchars},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200852 {"screencol", 0, 0, f_screencol},
853 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100854 {"screenstring", 2, 2, f_screenstring},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200855 {"search", 1, 4, f_search},
856 {"searchdecl", 1, 3, f_searchdecl},
857 {"searchpair", 3, 7, f_searchpair},
858 {"searchpairpos", 3, 7, f_searchpairpos},
859 {"searchpos", 1, 4, f_searchpos},
860 {"server2client", 2, 2, f_server2client},
861 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200862 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200863 {"setbufvar", 3, 3, f_setbufvar},
864 {"setcharsearch", 1, 1, f_setcharsearch},
865 {"setcmdpos", 1, 1, f_setcmdpos},
866 {"setfperm", 2, 2, f_setfperm},
867 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200868 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100869 {"setmatches", 1, 2, f_setmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200870 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200871 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200872 {"setreg", 2, 3, f_setreg},
873 {"settabvar", 3, 3, f_settabvar},
874 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100875 {"settagstack", 2, 3, f_settagstack},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200876 {"setwinvar", 3, 3, f_setwinvar},
877#ifdef FEAT_CRYPT
878 {"sha256", 1, 1, f_sha256},
879#endif
880 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaarf9514162018-11-22 03:08:29 +0100881 {"shiftwidth", 0, 1, f_shiftwidth},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100882#ifdef FEAT_SIGNS
883 {"sign_define", 1, 2, f_sign_define},
884 {"sign_getdefined", 0, 1, f_sign_getdefined},
885 {"sign_getplaced", 0, 2, f_sign_getplaced},
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100886 {"sign_jump", 3, 3, f_sign_jump},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100887 {"sign_place", 4, 5, f_sign_place},
888 {"sign_undefine", 0, 1, f_sign_undefine},
889 {"sign_unplace", 1, 2, f_sign_unplace},
890#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200891 {"simplify", 1, 1, f_simplify},
892#ifdef FEAT_FLOAT
893 {"sin", 1, 1, f_sin},
894 {"sinh", 1, 1, f_sinh},
895#endif
896 {"sort", 1, 3, f_sort},
897 {"soundfold", 1, 1, f_soundfold},
898 {"spellbadword", 0, 1, f_spellbadword},
899 {"spellsuggest", 1, 3, f_spellsuggest},
900 {"split", 1, 3, f_split},
901#ifdef FEAT_FLOAT
902 {"sqrt", 1, 1, f_sqrt},
903 {"str2float", 1, 1, f_str2float},
904#endif
905 {"str2nr", 1, 2, f_str2nr},
906 {"strcharpart", 2, 3, f_strcharpart},
907 {"strchars", 1, 2, f_strchars},
908 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
909#ifdef HAVE_STRFTIME
910 {"strftime", 1, 2, f_strftime},
911#endif
912 {"strgetchar", 2, 2, f_strgetchar},
913 {"stridx", 2, 3, f_stridx},
914 {"string", 1, 1, f_string},
915 {"strlen", 1, 1, f_strlen},
916 {"strpart", 2, 3, f_strpart},
917 {"strridx", 2, 3, f_strridx},
918 {"strtrans", 1, 1, f_strtrans},
919 {"strwidth", 1, 1, f_strwidth},
920 {"submatch", 1, 2, f_submatch},
921 {"substitute", 4, 4, f_substitute},
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200922 {"swapinfo", 1, 1, f_swapinfo},
Bram Moolenaar110bd602018-09-16 18:46:59 +0200923 {"swapname", 1, 1, f_swapname},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200924 {"synID", 3, 3, f_synID},
925 {"synIDattr", 2, 3, f_synIDattr},
926 {"synIDtrans", 1, 1, f_synIDtrans},
927 {"synconcealed", 2, 2, f_synconcealed},
928 {"synstack", 2, 2, f_synstack},
929 {"system", 1, 2, f_system},
930 {"systemlist", 1, 2, f_systemlist},
931 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
932 {"tabpagenr", 0, 1, f_tabpagenr},
933 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
934 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100935 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200936#ifdef FEAT_FLOAT
937 {"tan", 1, 1, f_tan},
938 {"tanh", 1, 1, f_tanh},
939#endif
940 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200941#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100942 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
943 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100944 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200945 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200946# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
947 {"term_getansicolors", 1, 1, f_term_getansicolors},
948# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200949 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200950 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200951 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200952 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200953 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200954 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200955 {"term_getstatus", 1, 1, f_term_getstatus},
956 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200957 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200958 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200959 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200960 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200961# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
962 {"term_setansicolors", 2, 2, f_term_setansicolors},
963# endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100964 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100965 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaara42d3632018-04-14 17:05:38 +0200966 {"term_setsize", 3, 3, f_term_setsize},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200967 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200968 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200969#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200970 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
971 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200972 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200973 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100974 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +0100975 {"test_null_blob", 0, 0, f_test_null_blob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200976#ifdef FEAT_JOB_CHANNEL
977 {"test_null_channel", 0, 0, f_test_null_channel},
978#endif
979 {"test_null_dict", 0, 0, f_test_null_dict},
980#ifdef FEAT_JOB_CHANNEL
981 {"test_null_job", 0, 0, f_test_null_job},
982#endif
983 {"test_null_list", 0, 0, f_test_null_list},
984 {"test_null_partial", 0, 0, f_test_null_partial},
985 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200986 {"test_option_not_set", 1, 1, f_test_option_not_set},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +0100987 {"test_override", 2, 2, f_test_override},
988 {"test_refcount", 1, 1, f_test_refcount},
Bram Moolenaarab186732018-09-14 21:27:06 +0200989#ifdef FEAT_GUI
990 {"test_scrollbar", 3, 3, f_test_scrollbar},
991#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200992 {"test_settime", 1, 1, f_test_settime},
993#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200994 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200995 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200996 {"timer_start", 2, 3, f_timer_start},
997 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200998 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200999#endif
1000 {"tolower", 1, 1, f_tolower},
1001 {"toupper", 1, 1, f_toupper},
1002 {"tr", 3, 3, f_tr},
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01001003 {"trim", 1, 2, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001004#ifdef FEAT_FLOAT
1005 {"trunc", 1, 1, f_trunc},
1006#endif
1007 {"type", 1, 1, f_type},
1008 {"undofile", 1, 1, f_undofile},
1009 {"undotree", 0, 0, f_undotree},
1010 {"uniq", 1, 3, f_uniq},
1011 {"values", 1, 1, f_values},
1012 {"virtcol", 1, 1, f_virtcol},
1013 {"visualmode", 0, 1, f_visualmode},
1014 {"wildmenumode", 0, 0, f_wildmenumode},
1015 {"win_findbuf", 1, 1, f_win_findbuf},
1016 {"win_getid", 0, 2, f_win_getid},
1017 {"win_gotoid", 1, 1, f_win_gotoid},
1018 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
1019 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +01001020 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001021 {"winbufnr", 1, 1, f_winbufnr},
1022 {"wincol", 0, 0, f_wincol},
1023 {"winheight", 1, 1, f_winheight},
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +02001024 {"winlayout", 0, 1, f_winlayout},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001025 {"winline", 0, 0, f_winline},
1026 {"winnr", 0, 1, f_winnr},
1027 {"winrestcmd", 0, 0, f_winrestcmd},
1028 {"winrestview", 1, 1, f_winrestview},
1029 {"winsaveview", 0, 0, f_winsaveview},
1030 {"winwidth", 1, 1, f_winwidth},
1031 {"wordcount", 0, 0, f_wordcount},
1032 {"writefile", 2, 3, f_writefile},
1033 {"xor", 2, 2, f_xor},
1034};
1035
1036#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
1037
1038/*
1039 * Function given to ExpandGeneric() to obtain the list of internal
1040 * or user defined function names.
1041 */
1042 char_u *
1043get_function_name(expand_T *xp, int idx)
1044{
1045 static int intidx = -1;
1046 char_u *name;
1047
1048 if (idx == 0)
1049 intidx = -1;
1050 if (intidx < 0)
1051 {
1052 name = get_user_func_name(xp, idx);
1053 if (name != NULL)
1054 return name;
1055 }
1056 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
1057 {
1058 STRCPY(IObuff, functions[intidx].f_name);
1059 STRCAT(IObuff, "(");
1060 if (functions[intidx].f_max_argc == 0)
1061 STRCAT(IObuff, ")");
1062 return IObuff;
1063 }
1064
1065 return NULL;
1066}
1067
1068/*
1069 * Function given to ExpandGeneric() to obtain the list of internal or
1070 * user defined variable or function names.
1071 */
1072 char_u *
1073get_expr_name(expand_T *xp, int idx)
1074{
1075 static int intidx = -1;
1076 char_u *name;
1077
1078 if (idx == 0)
1079 intidx = -1;
1080 if (intidx < 0)
1081 {
1082 name = get_function_name(xp, idx);
1083 if (name != NULL)
1084 return name;
1085 }
1086 return get_user_var_name(xp, ++intidx);
1087}
1088
1089#endif /* FEAT_CMDL_COMPL */
1090
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001091/*
1092 * Find internal function in table above.
1093 * Return index, or -1 if not found
1094 */
1095 int
1096find_internal_func(
1097 char_u *name) /* name of the function */
1098{
1099 int first = 0;
1100 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
1101 int cmp;
1102 int x;
1103
1104 /*
1105 * Find the function name in the table. Binary search.
1106 */
1107 while (first <= last)
1108 {
1109 x = first + ((unsigned)(last - first) >> 1);
1110 cmp = STRCMP(name, functions[x].f_name);
1111 if (cmp < 0)
1112 last = x - 1;
1113 else if (cmp > 0)
1114 first = x + 1;
1115 else
1116 return x;
1117 }
1118 return -1;
1119}
1120
1121 int
1122call_internal_func(
1123 char_u *name,
1124 int argcount,
1125 typval_T *argvars,
1126 typval_T *rettv)
1127{
1128 int i;
1129
1130 i = find_internal_func(name);
1131 if (i < 0)
1132 return ERROR_UNKNOWN;
1133 if (argcount < functions[i].f_min_argc)
1134 return ERROR_TOOFEW;
1135 if (argcount > functions[i].f_max_argc)
1136 return ERROR_TOOMANY;
1137 argvars[argcount].v_type = VAR_UNKNOWN;
1138 functions[i].f_func(argvars, rettv);
1139 return ERROR_NONE;
1140}
1141
1142/*
1143 * Return TRUE for a non-zero Number and a non-empty String.
1144 */
1145 static int
1146non_zero_arg(typval_T *argvars)
1147{
1148 return ((argvars[0].v_type == VAR_NUMBER
1149 && argvars[0].vval.v_number != 0)
1150 || (argvars[0].v_type == VAR_SPECIAL
1151 && argvars[0].vval.v_number == VVAL_TRUE)
1152 || (argvars[0].v_type == VAR_STRING
1153 && argvars[0].vval.v_string != NULL
1154 && *argvars[0].vval.v_string != NUL));
1155}
1156
1157/*
1158 * Get the lnum from the first argument.
1159 * Also accepts ".", "$", etc., but that only works for the current buffer.
1160 * Returns -1 on error.
1161 */
1162 static linenr_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001163tv_get_lnum(typval_T *argvars)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001164{
1165 typval_T rettv;
1166 linenr_T lnum;
1167
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001168 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001169 if (lnum == 0) /* no valid number, try using line() */
1170 {
1171 rettv.v_type = VAR_NUMBER;
1172 f_line(argvars, &rettv);
1173 lnum = (linenr_T)rettv.vval.v_number;
1174 clear_tv(&rettv);
1175 }
1176 return lnum;
1177}
1178
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001179/*
1180 * Get the lnum from the first argument.
1181 * Also accepts "$", then "buf" is used.
1182 * Returns 0 on error.
1183 */
1184 static linenr_T
1185tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
1186{
1187 if (argvars[0].v_type == VAR_STRING
1188 && argvars[0].vval.v_string != NULL
1189 && argvars[0].vval.v_string[0] == '$'
1190 && buf != NULL)
1191 return buf->b_ml.ml_line_count;
1192 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1193}
1194
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001195#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001196/*
1197 * Get the float value of "argvars[0]" into "f".
1198 * Returns FAIL when the argument is not a Number or Float.
1199 */
1200 static int
1201get_float_arg(typval_T *argvars, float_T *f)
1202{
1203 if (argvars[0].v_type == VAR_FLOAT)
1204 {
1205 *f = argvars[0].vval.v_float;
1206 return OK;
1207 }
1208 if (argvars[0].v_type == VAR_NUMBER)
1209 {
1210 *f = (float_T)argvars[0].vval.v_number;
1211 return OK;
1212 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001213 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001214 return FAIL;
1215}
1216
1217/*
1218 * "abs(expr)" function
1219 */
1220 static void
1221f_abs(typval_T *argvars, typval_T *rettv)
1222{
1223 if (argvars[0].v_type == VAR_FLOAT)
1224 {
1225 rettv->v_type = VAR_FLOAT;
1226 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1227 }
1228 else
1229 {
1230 varnumber_T n;
1231 int error = FALSE;
1232
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001233 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001234 if (error)
1235 rettv->vval.v_number = -1;
1236 else if (n > 0)
1237 rettv->vval.v_number = n;
1238 else
1239 rettv->vval.v_number = -n;
1240 }
1241}
1242
1243/*
1244 * "acos()" function
1245 */
1246 static void
1247f_acos(typval_T *argvars, typval_T *rettv)
1248{
1249 float_T f = 0.0;
1250
1251 rettv->v_type = VAR_FLOAT;
1252 if (get_float_arg(argvars, &f) == OK)
1253 rettv->vval.v_float = acos(f);
1254 else
1255 rettv->vval.v_float = 0.0;
1256}
1257#endif
1258
1259/*
1260 * "add(list, item)" function
1261 */
1262 static void
1263f_add(typval_T *argvars, typval_T *rettv)
1264{
1265 list_T *l;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001266 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001267
1268 rettv->vval.v_number = 1; /* Default: Failed */
1269 if (argvars[0].v_type == VAR_LIST)
1270 {
1271 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001272 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001273 (char_u *)N_("add() argument"), TRUE)
1274 && list_append_tv(l, &argvars[1]) == OK)
1275 copy_tv(&argvars[0], rettv);
1276 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001277 else if (argvars[0].v_type == VAR_BLOB)
1278 {
1279 if ((b = argvars[0].vval.v_blob) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001280 && !var_check_lock(b->bv_lock,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001281 (char_u *)N_("add() argument"), TRUE))
1282 {
Bram Moolenaar05500ec2019-01-13 19:10:33 +01001283 int error = FALSE;
1284 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
1285
1286 if (!error)
1287 {
1288 ga_append(&b->bv_ga, (int)n);
1289 copy_tv(&argvars[0], rettv);
1290 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001291 }
1292 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001293 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01001294 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001295}
1296
1297/*
1298 * "and(expr, expr)" function
1299 */
1300 static void
1301f_and(typval_T *argvars, typval_T *rettv)
1302{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001303 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1304 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001305}
1306
1307/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02001308 * If there is a window for "curbuf", make it the current window.
1309 */
1310 static void
1311find_win_for_curbuf(void)
1312{
1313 wininfo_T *wip;
1314
1315 for (wip = curbuf->b_wininfo; wip != NULL; wip = wip->wi_next)
1316 {
1317 if (wip->wi_win != NULL)
1318 {
1319 curwin = wip->wi_win;
1320 break;
1321 }
1322 }
1323}
1324
1325/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001326 * Set line or list of lines in buffer "buf".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001327 */
1328 static void
Bram Moolenaarca851592018-06-06 21:04:07 +02001329set_buffer_lines(
1330 buf_T *buf,
1331 linenr_T lnum_arg,
1332 int append,
1333 typval_T *lines,
1334 typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001335{
Bram Moolenaarca851592018-06-06 21:04:07 +02001336 linenr_T lnum = lnum_arg + (append ? 1 : 0);
1337 char_u *line = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001338 list_T *l = NULL;
1339 listitem_T *li = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001340 long added = 0;
Bram Moolenaarca851592018-06-06 21:04:07 +02001341 linenr_T append_lnum;
1342 buf_T *curbuf_save = NULL;
1343 win_T *curwin_save = NULL;
1344 int is_curbuf = buf == curbuf;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001345
Bram Moolenaarca851592018-06-06 21:04:07 +02001346 /* When using the current buffer ml_mfp will be set if needed. Useful when
1347 * setline() is used on startup. For other buffers the buffer must be
1348 * loaded. */
1349 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001350 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001351 rettv->vval.v_number = 1; /* FAIL */
1352 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001353 }
1354
Bram Moolenaarca851592018-06-06 21:04:07 +02001355 if (!is_curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001356 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001357 curbuf_save = curbuf;
1358 curwin_save = curwin;
1359 curbuf = buf;
Bram Moolenaard79a2622018-06-07 18:17:46 +02001360 find_win_for_curbuf();
Bram Moolenaarca851592018-06-06 21:04:07 +02001361 }
1362
1363 if (append)
1364 // appendbufline() uses the line number below which we insert
1365 append_lnum = lnum - 1;
1366 else
1367 // setbufline() uses the line number above which we insert, we only
1368 // append if it's below the last line
1369 append_lnum = curbuf->b_ml.ml_line_count;
1370
1371 if (lines->v_type == VAR_LIST)
1372 {
1373 l = lines->vval.v_list;
1374 li = l->lv_first;
1375 }
1376 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001377 line = tv_get_string_chk(lines);
Bram Moolenaarca851592018-06-06 21:04:07 +02001378
1379 /* default result is zero == OK */
1380 for (;;)
1381 {
1382 if (l != NULL)
1383 {
1384 /* list argument, get next string */
1385 if (li == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001386 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001387 line = tv_get_string_chk(&li->li_tv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001388 li = li->li_next;
1389 }
1390
Bram Moolenaarca851592018-06-06 21:04:07 +02001391 rettv->vval.v_number = 1; /* FAIL */
1392 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
1393 break;
1394
1395 /* When coming here from Insert mode, sync undo, so that this can be
1396 * undone separately from what was previously inserted. */
1397 if (u_sync_once == 2)
1398 {
1399 u_sync_once = 1; /* notify that u_sync() was called */
1400 u_sync(TRUE);
1401 }
1402
1403 if (!append && lnum <= curbuf->b_ml.ml_line_count)
1404 {
Bram Moolenaar21b50382019-01-04 18:07:24 +01001405 // Existing line, replace it.
1406 // Removes any existing text properties.
1407 if (u_savesub(lnum) == OK && ml_replace_len(
1408 lnum, line, (colnr_T)STRLEN(line) + 1, TRUE, TRUE) == OK)
Bram Moolenaarca851592018-06-06 21:04:07 +02001409 {
1410 changed_bytes(lnum, 0);
1411 if (is_curbuf && lnum == curwin->w_cursor.lnum)
1412 check_cursor_col();
1413 rettv->vval.v_number = 0; /* OK */
1414 }
1415 }
1416 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
1417 {
1418 /* append the line */
1419 ++added;
1420 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
1421 rettv->vval.v_number = 0; /* OK */
1422 }
1423
1424 if (l == NULL) /* only one string argument */
1425 break;
1426 ++lnum;
1427 }
1428
1429 if (added > 0)
1430 {
1431 win_T *wp;
1432 tabpage_T *tp;
1433
1434 appended_lines_mark(append_lnum, added);
1435 FOR_ALL_TAB_WINDOWS(tp, wp)
1436 if (wp->w_buffer == buf && wp->w_cursor.lnum > append_lnum)
1437 wp->w_cursor.lnum += added;
1438 check_cursor_col();
1439
Bram Moolenaarf2732452018-06-03 14:47:35 +02001440#ifdef FEAT_JOB_CHANNEL
1441 if (bt_prompt(curbuf) && (State & INSERT))
1442 // show the line with the prompt
1443 update_topline();
1444#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001445 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001446
1447 if (!is_curbuf)
1448 {
1449 curbuf = curbuf_save;
1450 curwin = curwin_save;
1451 }
1452}
1453
1454/*
1455 * "append(lnum, string/list)" function
1456 */
1457 static void
1458f_append(typval_T *argvars, typval_T *rettv)
1459{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001460 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaarca851592018-06-06 21:04:07 +02001461
1462 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1463}
1464
1465/*
1466 * "appendbufline(buf, lnum, string/list)" function
1467 */
1468 static void
1469f_appendbufline(typval_T *argvars, typval_T *rettv)
1470{
1471 linenr_T lnum;
1472 buf_T *buf;
1473
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001474 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarca851592018-06-06 21:04:07 +02001475 if (buf == NULL)
1476 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001477 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001478 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001479 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +02001480 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1481 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001482}
1483
1484/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001485 * "argc([window id])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001486 */
1487 static void
Bram Moolenaare6e39892018-10-25 12:32:11 +02001488f_argc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001489{
Bram Moolenaare6e39892018-10-25 12:32:11 +02001490 win_T *wp;
1491
1492 if (argvars[0].v_type == VAR_UNKNOWN)
1493 // use the current window
1494 rettv->vval.v_number = ARGCOUNT;
1495 else if (argvars[0].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001496 && tv_get_number(&argvars[0]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001497 // use the global argument list
1498 rettv->vval.v_number = GARGCOUNT;
1499 else
1500 {
1501 // use the argument list of the specified window
1502 wp = find_win_by_nr_or_id(&argvars[0]);
1503 if (wp != NULL)
1504 rettv->vval.v_number = WARGCOUNT(wp);
1505 else
1506 rettv->vval.v_number = -1;
1507 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001508}
1509
1510/*
1511 * "argidx()" function
1512 */
1513 static void
1514f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1515{
1516 rettv->vval.v_number = curwin->w_arg_idx;
1517}
1518
1519/*
1520 * "arglistid()" function
1521 */
1522 static void
1523f_arglistid(typval_T *argvars, typval_T *rettv)
1524{
1525 win_T *wp;
1526
1527 rettv->vval.v_number = -1;
1528 wp = find_tabwin(&argvars[0], &argvars[1]);
1529 if (wp != NULL)
1530 rettv->vval.v_number = wp->w_alist->id;
1531}
1532
1533/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001534 * Get the argument list for a given window
1535 */
1536 static void
1537get_arglist_as_rettv(aentry_T *arglist, int argcount, typval_T *rettv)
1538{
1539 int idx;
1540
1541 if (rettv_list_alloc(rettv) == OK && arglist != NULL)
1542 for (idx = 0; idx < argcount; ++idx)
1543 list_append_string(rettv->vval.v_list,
1544 alist_name(&arglist[idx]), -1);
1545}
1546
1547/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001548 * "argv(nr)" function
1549 */
1550 static void
1551f_argv(typval_T *argvars, typval_T *rettv)
1552{
1553 int idx;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001554 aentry_T *arglist = NULL;
1555 int argcount = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001556
1557 if (argvars[0].v_type != VAR_UNKNOWN)
1558 {
Bram Moolenaare6e39892018-10-25 12:32:11 +02001559 if (argvars[1].v_type == VAR_UNKNOWN)
1560 {
1561 arglist = ARGLIST;
1562 argcount = ARGCOUNT;
1563 }
1564 else if (argvars[1].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001565 && tv_get_number(&argvars[1]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001566 {
1567 arglist = GARGLIST;
1568 argcount = GARGCOUNT;
1569 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001570 else
Bram Moolenaare6e39892018-10-25 12:32:11 +02001571 {
1572 win_T *wp = find_win_by_nr_or_id(&argvars[1]);
1573
1574 if (wp != NULL)
1575 {
1576 /* Use the argument list of the specified window */
1577 arglist = WARGLIST(wp);
1578 argcount = WARGCOUNT(wp);
1579 }
1580 }
1581
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001582 rettv->v_type = VAR_STRING;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001583 rettv->vval.v_string = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001584 idx = tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02001585 if (arglist != NULL && idx >= 0 && idx < argcount)
1586 rettv->vval.v_string = vim_strsave(alist_name(&arglist[idx]));
1587 else if (idx == -1)
1588 get_arglist_as_rettv(arglist, argcount, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001589 }
Bram Moolenaare6e39892018-10-25 12:32:11 +02001590 else
1591 get_arglist_as_rettv(ARGLIST, ARGCOUNT, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001592}
1593
1594/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001595 * "assert_beeps(cmd [, error])" function
1596 */
1597 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001598f_assert_beeps(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001599{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001600 rettv->vval.v_number = assert_beeps(argvars);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001601}
1602
1603/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001604 * "assert_equal(expected, actual[, msg])" function
1605 */
1606 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001607f_assert_equal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001608{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001609 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_EQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001610}
1611
1612/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01001613 * "assert_equalfile(fname-one, fname-two)" function
1614 */
1615 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001616f_assert_equalfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaard96ff162018-02-18 22:13:29 +01001617{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001618 rettv->vval.v_number = assert_equalfile(argvars);
Bram Moolenaard96ff162018-02-18 22:13:29 +01001619}
1620
1621/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001622 * "assert_notequal(expected, actual[, msg])" function
1623 */
1624 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001625f_assert_notequal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001626{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001627 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001628}
1629
1630/*
1631 * "assert_exception(string[, msg])" function
1632 */
1633 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001634f_assert_exception(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001635{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001636 rettv->vval.v_number = assert_exception(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001637}
1638
1639/*
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02001640 * "assert_fails(cmd [, error[, msg]])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001641 */
1642 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001643f_assert_fails(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001644{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001645 rettv->vval.v_number = assert_fails(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001646}
1647
1648/*
1649 * "assert_false(actual[, msg])" function
1650 */
1651 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001652f_assert_false(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001653{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001654 rettv->vval.v_number = assert_bool(argvars, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001655}
1656
1657/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001658 * "assert_inrange(lower, upper[, msg])" function
1659 */
1660 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001661f_assert_inrange(typval_T *argvars, typval_T *rettv)
Bram Moolenaar61c04492016-07-23 15:35:35 +02001662{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001663 rettv->vval.v_number = assert_inrange(argvars);
Bram Moolenaar61c04492016-07-23 15:35:35 +02001664}
1665
1666/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001667 * "assert_match(pattern, actual[, msg])" function
1668 */
1669 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001670f_assert_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001671{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001672 rettv->vval.v_number = assert_match_common(argvars, ASSERT_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001673}
1674
1675/*
1676 * "assert_notmatch(pattern, actual[, msg])" function
1677 */
1678 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001679f_assert_notmatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001680{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001681 rettv->vval.v_number = assert_match_common(argvars, ASSERT_NOTMATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001682}
1683
1684/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001685 * "assert_report(msg)" function
1686 */
1687 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001688f_assert_report(typval_T *argvars, typval_T *rettv)
Bram Moolenaar42205552017-03-18 19:42:22 +01001689{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001690 rettv->vval.v_number = assert_report(argvars);
Bram Moolenaar42205552017-03-18 19:42:22 +01001691}
1692
1693/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001694 * "assert_true(actual[, msg])" function
1695 */
1696 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001697f_assert_true(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001698{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001699 rettv->vval.v_number = assert_bool(argvars, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001700}
1701
1702#ifdef FEAT_FLOAT
1703/*
1704 * "asin()" function
1705 */
1706 static void
1707f_asin(typval_T *argvars, typval_T *rettv)
1708{
1709 float_T f = 0.0;
1710
1711 rettv->v_type = VAR_FLOAT;
1712 if (get_float_arg(argvars, &f) == OK)
1713 rettv->vval.v_float = asin(f);
1714 else
1715 rettv->vval.v_float = 0.0;
1716}
1717
1718/*
1719 * "atan()" function
1720 */
1721 static void
1722f_atan(typval_T *argvars, typval_T *rettv)
1723{
1724 float_T f = 0.0;
1725
1726 rettv->v_type = VAR_FLOAT;
1727 if (get_float_arg(argvars, &f) == OK)
1728 rettv->vval.v_float = atan(f);
1729 else
1730 rettv->vval.v_float = 0.0;
1731}
1732
1733/*
1734 * "atan2()" function
1735 */
1736 static void
1737f_atan2(typval_T *argvars, typval_T *rettv)
1738{
1739 float_T fx = 0.0, fy = 0.0;
1740
1741 rettv->v_type = VAR_FLOAT;
1742 if (get_float_arg(argvars, &fx) == OK
1743 && get_float_arg(&argvars[1], &fy) == OK)
1744 rettv->vval.v_float = atan2(fx, fy);
1745 else
1746 rettv->vval.v_float = 0.0;
1747}
1748#endif
1749
1750/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001751 * "balloon_show()" function
1752 */
1753#ifdef FEAT_BEVAL
1754 static void
1755f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1756{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001757 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001758 {
1759 if (argvars[0].v_type == VAR_LIST
1760# ifdef FEAT_GUI
1761 && !gui.in_use
1762# endif
1763 )
1764 post_balloon(balloonEval, NULL, argvars[0].vval.v_list);
1765 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001766 post_balloon(balloonEval, tv_get_string_chk(&argvars[0]), NULL);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001767 }
1768}
1769
Bram Moolenaar669a8282017-11-19 20:13:05 +01001770# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001771 static void
1772f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1773{
1774 if (rettv_list_alloc(rettv) == OK)
1775 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001776 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001777
1778 if (msg != NULL)
1779 {
1780 pumitem_T *array;
1781 int size = split_message(msg, &array);
1782 int i;
1783
1784 /* Skip the first and last item, they are always empty. */
1785 for (i = 1; i < size - 1; ++i)
1786 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001787 while (size > 0)
1788 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001789 vim_free(array);
1790 }
1791 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001792}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001793# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001794#endif
1795
1796/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001797 * "browse(save, title, initdir, default)" function
1798 */
1799 static void
1800f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1801{
1802#ifdef FEAT_BROWSE
1803 int save;
1804 char_u *title;
1805 char_u *initdir;
1806 char_u *defname;
1807 char_u buf[NUMBUFLEN];
1808 char_u buf2[NUMBUFLEN];
1809 int error = FALSE;
1810
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001811 save = (int)tv_get_number_chk(&argvars[0], &error);
1812 title = tv_get_string_chk(&argvars[1]);
1813 initdir = tv_get_string_buf_chk(&argvars[2], buf);
1814 defname = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001815
1816 if (error || title == NULL || initdir == NULL || defname == NULL)
1817 rettv->vval.v_string = NULL;
1818 else
1819 rettv->vval.v_string =
1820 do_browse(save ? BROWSE_SAVE : 0,
1821 title, defname, NULL, initdir, NULL, curbuf);
1822#else
1823 rettv->vval.v_string = NULL;
1824#endif
1825 rettv->v_type = VAR_STRING;
1826}
1827
1828/*
1829 * "browsedir(title, initdir)" function
1830 */
1831 static void
1832f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1833{
1834#ifdef FEAT_BROWSE
1835 char_u *title;
1836 char_u *initdir;
1837 char_u buf[NUMBUFLEN];
1838
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001839 title = tv_get_string_chk(&argvars[0]);
1840 initdir = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001841
1842 if (title == NULL || initdir == NULL)
1843 rettv->vval.v_string = NULL;
1844 else
1845 rettv->vval.v_string = do_browse(BROWSE_DIR,
1846 title, NULL, NULL, initdir, NULL, curbuf);
1847#else
1848 rettv->vval.v_string = NULL;
1849#endif
1850 rettv->v_type = VAR_STRING;
1851}
1852
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001853/*
1854 * Find a buffer by number or exact name.
1855 */
1856 static buf_T *
1857find_buffer(typval_T *avar)
1858{
1859 buf_T *buf = NULL;
1860
1861 if (avar->v_type == VAR_NUMBER)
1862 buf = buflist_findnr((int)avar->vval.v_number);
1863 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1864 {
1865 buf = buflist_findname_exp(avar->vval.v_string);
1866 if (buf == NULL)
1867 {
1868 /* No full path name match, try a match with a URL or a "nofile"
1869 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001870 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001871 if (buf->b_fname != NULL
1872 && (path_with_url(buf->b_fname)
1873#ifdef FEAT_QUICKFIX
1874 || bt_nofile(buf)
1875#endif
1876 )
1877 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1878 break;
1879 }
1880 }
1881 return buf;
1882}
1883
1884/*
1885 * "bufexists(expr)" function
1886 */
1887 static void
1888f_bufexists(typval_T *argvars, typval_T *rettv)
1889{
1890 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1891}
1892
1893/*
1894 * "buflisted(expr)" function
1895 */
1896 static void
1897f_buflisted(typval_T *argvars, typval_T *rettv)
1898{
1899 buf_T *buf;
1900
1901 buf = find_buffer(&argvars[0]);
1902 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1903}
1904
1905/*
1906 * "bufloaded(expr)" function
1907 */
1908 static void
1909f_bufloaded(typval_T *argvars, typval_T *rettv)
1910{
1911 buf_T *buf;
1912
1913 buf = find_buffer(&argvars[0]);
1914 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1915}
1916
1917 buf_T *
1918buflist_find_by_name(char_u *name, int curtab_only)
1919{
1920 int save_magic;
1921 char_u *save_cpo;
1922 buf_T *buf;
1923
1924 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1925 save_magic = p_magic;
1926 p_magic = TRUE;
1927 save_cpo = p_cpo;
1928 p_cpo = (char_u *)"";
1929
1930 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1931 TRUE, FALSE, curtab_only));
1932
1933 p_magic = save_magic;
1934 p_cpo = save_cpo;
1935 return buf;
1936}
1937
1938/*
1939 * Get buffer by number or pattern.
1940 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001941 buf_T *
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001942tv_get_buf(typval_T *tv, int curtab_only)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001943{
1944 char_u *name = tv->vval.v_string;
1945 buf_T *buf;
1946
1947 if (tv->v_type == VAR_NUMBER)
1948 return buflist_findnr((int)tv->vval.v_number);
1949 if (tv->v_type != VAR_STRING)
1950 return NULL;
1951 if (name == NULL || *name == NUL)
1952 return curbuf;
1953 if (name[0] == '$' && name[1] == NUL)
1954 return lastbuf;
1955
1956 buf = buflist_find_by_name(name, curtab_only);
1957
1958 /* If not found, try expanding the name, like done for bufexists(). */
1959 if (buf == NULL)
1960 buf = find_buffer(tv);
1961
1962 return buf;
1963}
1964
Bram Moolenaarec9d3002019-01-12 13:50:31 +01001965#ifdef FEAT_SIGNS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001966/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001967 * Get the buffer from "arg" and give an error and return NULL if it is not
1968 * valid.
1969 */
1970 static buf_T *
1971get_buf_arg(typval_T *arg)
1972{
1973 buf_T *buf;
1974
1975 ++emsg_off;
1976 buf = tv_get_buf(arg, FALSE);
1977 --emsg_off;
1978 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001979 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001980 return buf;
1981}
Bram Moolenaarec9d3002019-01-12 13:50:31 +01001982#endif
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001983
1984/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001985 * "bufname(expr)" function
1986 */
1987 static void
1988f_bufname(typval_T *argvars, typval_T *rettv)
1989{
1990 buf_T *buf;
1991
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001992 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001993 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001994 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001995 rettv->v_type = VAR_STRING;
1996 if (buf != NULL && buf->b_fname != NULL)
1997 rettv->vval.v_string = vim_strsave(buf->b_fname);
1998 else
1999 rettv->vval.v_string = NULL;
2000 --emsg_off;
2001}
2002
2003/*
2004 * "bufnr(expr)" function
2005 */
2006 static void
2007f_bufnr(typval_T *argvars, typval_T *rettv)
2008{
2009 buf_T *buf;
2010 int error = FALSE;
2011 char_u *name;
2012
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002013 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002014 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002015 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002016 --emsg_off;
2017
2018 /* If the buffer isn't found and the second argument is not zero create a
2019 * new buffer. */
2020 if (buf == NULL
2021 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002022 && tv_get_number_chk(&argvars[1], &error) != 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002023 && !error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002024 && (name = tv_get_string_chk(&argvars[0])) != NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002025 && !error)
2026 buf = buflist_new(name, NULL, (linenr_T)1, 0);
2027
2028 if (buf != NULL)
2029 rettv->vval.v_number = buf->b_fnum;
2030 else
2031 rettv->vval.v_number = -1;
2032}
2033
2034 static void
2035buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
2036{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002037 win_T *wp;
2038 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002039 buf_T *buf;
2040
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002041 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002042 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002043 buf = tv_get_buf(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02002044 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002045 {
2046 ++winnr;
2047 if (wp->w_buffer == buf)
2048 break;
2049 }
2050 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002051 --emsg_off;
2052}
2053
2054/*
2055 * "bufwinid(nr)" function
2056 */
2057 static void
2058f_bufwinid(typval_T *argvars, typval_T *rettv)
2059{
2060 buf_win_common(argvars, rettv, FALSE);
2061}
2062
2063/*
2064 * "bufwinnr(nr)" function
2065 */
2066 static void
2067f_bufwinnr(typval_T *argvars, typval_T *rettv)
2068{
2069 buf_win_common(argvars, rettv, TRUE);
2070}
2071
2072/*
2073 * "byte2line(byte)" function
2074 */
2075 static void
2076f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2077{
2078#ifndef FEAT_BYTEOFF
2079 rettv->vval.v_number = -1;
2080#else
2081 long boff = 0;
2082
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002083 boff = tv_get_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002084 if (boff < 0)
2085 rettv->vval.v_number = -1;
2086 else
2087 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2088 (linenr_T)0, &boff);
2089#endif
2090}
2091
2092 static void
2093byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2094{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002095 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002096 char_u *str;
2097 varnumber_T idx;
2098
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002099 str = tv_get_string_chk(&argvars[0]);
2100 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002101 rettv->vval.v_number = -1;
2102 if (str == NULL || idx < 0)
2103 return;
2104
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002105 t = str;
2106 for ( ; idx > 0; idx--)
2107 {
2108 if (*t == NUL) /* EOL reached */
2109 return;
2110 if (enc_utf8 && comp)
2111 t += utf_ptr2len(t);
2112 else
2113 t += (*mb_ptr2len)(t);
2114 }
2115 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002116}
2117
2118/*
2119 * "byteidx()" function
2120 */
2121 static void
2122f_byteidx(typval_T *argvars, typval_T *rettv)
2123{
2124 byteidx(argvars, rettv, FALSE);
2125}
2126
2127/*
2128 * "byteidxcomp()" function
2129 */
2130 static void
2131f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2132{
2133 byteidx(argvars, rettv, TRUE);
2134}
2135
2136/*
2137 * "call(func, arglist [, dict])" function
2138 */
2139 static void
2140f_call(typval_T *argvars, typval_T *rettv)
2141{
2142 char_u *func;
2143 partial_T *partial = NULL;
2144 dict_T *selfdict = NULL;
2145
2146 if (argvars[1].v_type != VAR_LIST)
2147 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002148 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002149 return;
2150 }
2151 if (argvars[1].vval.v_list == NULL)
2152 return;
2153
2154 if (argvars[0].v_type == VAR_FUNC)
2155 func = argvars[0].vval.v_string;
2156 else if (argvars[0].v_type == VAR_PARTIAL)
2157 {
2158 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002159 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002160 }
2161 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002162 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002163 if (*func == NUL)
2164 return; /* type error or empty name */
2165
2166 if (argvars[2].v_type != VAR_UNKNOWN)
2167 {
2168 if (argvars[2].v_type != VAR_DICT)
2169 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002170 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002171 return;
2172 }
2173 selfdict = argvars[2].vval.v_dict;
2174 }
2175
2176 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2177}
2178
2179#ifdef FEAT_FLOAT
2180/*
2181 * "ceil({float})" function
2182 */
2183 static void
2184f_ceil(typval_T *argvars, typval_T *rettv)
2185{
2186 float_T f = 0.0;
2187
2188 rettv->v_type = VAR_FLOAT;
2189 if (get_float_arg(argvars, &f) == OK)
2190 rettv->vval.v_float = ceil(f);
2191 else
2192 rettv->vval.v_float = 0.0;
2193}
2194#endif
2195
2196#ifdef FEAT_JOB_CHANNEL
2197/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002198 * "ch_canread()" function
2199 */
2200 static void
2201f_ch_canread(typval_T *argvars, typval_T *rettv)
2202{
Bram Moolenaar958dc692016-12-01 15:34:12 +01002203 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002204
2205 rettv->vval.v_number = 0;
2206 if (channel != NULL)
2207 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
2208 || channel_has_readahead(channel, PART_OUT)
2209 || channel_has_readahead(channel, PART_ERR);
2210}
2211
2212/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002213 * "ch_close()" function
2214 */
2215 static void
2216f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
2217{
2218 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2219
2220 if (channel != NULL)
2221 {
2222 channel_close(channel, FALSE);
2223 channel_clear(channel);
2224 }
2225}
2226
2227/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002228 * "ch_close()" function
2229 */
2230 static void
2231f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
2232{
2233 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2234
2235 if (channel != NULL)
2236 channel_close_in(channel);
2237}
2238
2239/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002240 * "ch_getbufnr()" function
2241 */
2242 static void
2243f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
2244{
2245 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2246
2247 rettv->vval.v_number = -1;
2248 if (channel != NULL)
2249 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002250 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002251 int part;
2252
2253 if (STRCMP(what, "err") == 0)
2254 part = PART_ERR;
2255 else if (STRCMP(what, "out") == 0)
2256 part = PART_OUT;
2257 else if (STRCMP(what, "in") == 0)
2258 part = PART_IN;
2259 else
2260 part = PART_SOCK;
2261 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
2262 rettv->vval.v_number =
2263 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
2264 }
2265}
2266
2267/*
2268 * "ch_getjob()" function
2269 */
2270 static void
2271f_ch_getjob(typval_T *argvars, typval_T *rettv)
2272{
2273 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2274
2275 if (channel != NULL)
2276 {
2277 rettv->v_type = VAR_JOB;
2278 rettv->vval.v_job = channel->ch_job;
2279 if (channel->ch_job != NULL)
2280 ++channel->ch_job->jv_refcount;
2281 }
2282}
2283
2284/*
2285 * "ch_info()" function
2286 */
2287 static void
2288f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
2289{
2290 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2291
2292 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
2293 channel_info(channel, rettv->vval.v_dict);
2294}
2295
2296/*
2297 * "ch_log()" function
2298 */
2299 static void
2300f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
2301{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002302 char_u *msg = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002303 channel_T *channel = NULL;
2304
2305 if (argvars[1].v_type != VAR_UNKNOWN)
2306 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
2307
Bram Moolenaard5359b22018-04-05 22:44:39 +02002308 ch_log(channel, "%s", msg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002309}
2310
2311/*
2312 * "ch_logfile()" function
2313 */
2314 static void
2315f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
2316{
2317 char_u *fname;
2318 char_u *opt = (char_u *)"";
2319 char_u buf[NUMBUFLEN];
2320
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02002321 /* Don't open a file in restricted mode. */
2322 if (check_restricted() || check_secure())
2323 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002324 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002325 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002326 opt = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002327 ch_logfile(fname, opt);
2328}
2329
2330/*
2331 * "ch_open()" function
2332 */
2333 static void
2334f_ch_open(typval_T *argvars, typval_T *rettv)
2335{
2336 rettv->v_type = VAR_CHANNEL;
2337 if (check_restricted() || check_secure())
2338 return;
2339 rettv->vval.v_channel = channel_open_func(argvars);
2340}
2341
2342/*
2343 * "ch_read()" function
2344 */
2345 static void
2346f_ch_read(typval_T *argvars, typval_T *rettv)
2347{
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002348 common_channel_read(argvars, rettv, FALSE, FALSE);
2349}
2350
2351/*
2352 * "ch_readblob()" function
2353 */
2354 static void
2355f_ch_readblob(typval_T *argvars, typval_T *rettv)
2356{
2357 common_channel_read(argvars, rettv, TRUE, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002358}
2359
2360/*
2361 * "ch_readraw()" function
2362 */
2363 static void
2364f_ch_readraw(typval_T *argvars, typval_T *rettv)
2365{
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002366 common_channel_read(argvars, rettv, TRUE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002367}
2368
2369/*
2370 * "ch_evalexpr()" function
2371 */
2372 static void
2373f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2374{
2375 ch_expr_common(argvars, rettv, TRUE);
2376}
2377
2378/*
2379 * "ch_sendexpr()" function
2380 */
2381 static void
2382f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2383{
2384 ch_expr_common(argvars, rettv, FALSE);
2385}
2386
2387/*
2388 * "ch_evalraw()" function
2389 */
2390 static void
2391f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2392{
2393 ch_raw_common(argvars, rettv, TRUE);
2394}
2395
2396/*
2397 * "ch_sendraw()" function
2398 */
2399 static void
2400f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2401{
2402 ch_raw_common(argvars, rettv, FALSE);
2403}
2404
2405/*
2406 * "ch_setoptions()" function
2407 */
2408 static void
2409f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2410{
2411 channel_T *channel;
2412 jobopt_T opt;
2413
2414 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2415 if (channel == NULL)
2416 return;
2417 clear_job_options(&opt);
2418 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002419 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002420 channel_set_options(channel, &opt);
2421 free_job_options(&opt);
2422}
2423
2424/*
2425 * "ch_status()" function
2426 */
2427 static void
2428f_ch_status(typval_T *argvars, typval_T *rettv)
2429{
2430 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002431 jobopt_T opt;
2432 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002433
2434 /* return an empty string by default */
2435 rettv->v_type = VAR_STRING;
2436 rettv->vval.v_string = NULL;
2437
2438 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002439
2440 if (argvars[1].v_type != VAR_UNKNOWN)
2441 {
2442 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002443 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002444 && (opt.jo_set & JO_PART))
2445 part = opt.jo_part;
2446 }
2447
2448 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002449}
2450#endif
2451
2452/*
2453 * "changenr()" function
2454 */
2455 static void
2456f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2457{
2458 rettv->vval.v_number = curbuf->b_u_seq_cur;
2459}
2460
2461/*
2462 * "char2nr(string)" function
2463 */
2464 static void
2465f_char2nr(typval_T *argvars, typval_T *rettv)
2466{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002467 if (has_mbyte)
2468 {
2469 int utf8 = 0;
2470
2471 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002472 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002473
2474 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002475 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002476 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002477 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002478 }
2479 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002480 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002481}
2482
2483/*
2484 * "cindent(lnum)" function
2485 */
2486 static void
2487f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2488{
2489#ifdef FEAT_CINDENT
2490 pos_T pos;
2491 linenr_T lnum;
2492
2493 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002494 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002495 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2496 {
2497 curwin->w_cursor.lnum = lnum;
2498 rettv->vval.v_number = get_c_indent();
2499 curwin->w_cursor = pos;
2500 }
2501 else
2502#endif
2503 rettv->vval.v_number = -1;
2504}
2505
Bram Moolenaaraff74912019-03-30 18:11:49 +01002506 static win_T *
2507get_optional_window(typval_T *argvars, int idx)
2508{
2509 win_T *win = curwin;
2510
2511 if (argvars[idx].v_type != VAR_UNKNOWN)
2512 {
2513 win = find_win_by_nr_or_id(&argvars[idx]);
2514 if (win == NULL)
2515 {
2516 emsg(_(e_invalwindow));
2517 return NULL;
2518 }
2519 }
2520 return win;
2521}
2522
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002523/*
2524 * "clearmatches()" function
2525 */
2526 static void
2527f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2528{
2529#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01002530 win_T *win = get_optional_window(argvars, 0);
2531
2532 if (win != NULL)
2533 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002534#endif
2535}
2536
2537/*
2538 * "col(string)" function
2539 */
2540 static void
2541f_col(typval_T *argvars, typval_T *rettv)
2542{
2543 colnr_T col = 0;
2544 pos_T *fp;
2545 int fnum = curbuf->b_fnum;
2546
2547 fp = var2fpos(&argvars[0], FALSE, &fnum);
2548 if (fp != NULL && fnum == curbuf->b_fnum)
2549 {
2550 if (fp->col == MAXCOL)
2551 {
2552 /* '> can be MAXCOL, get the length of the line then */
2553 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2554 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2555 else
2556 col = MAXCOL;
2557 }
2558 else
2559 {
2560 col = fp->col + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002561 /* col(".") when the cursor is on the NUL at the end of the line
2562 * because of "coladd" can be seen as an extra column. */
2563 if (virtual_active() && fp == &curwin->w_cursor)
2564 {
2565 char_u *p = ml_get_cursor();
2566
2567 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2568 curwin->w_virtcol - curwin->w_cursor.coladd))
2569 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002570 int l;
2571
2572 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2573 col += l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002574 }
2575 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002576 }
2577 }
2578 rettv->vval.v_number = col;
2579}
2580
2581#if defined(FEAT_INS_EXPAND)
2582/*
2583 * "complete()" function
2584 */
2585 static void
2586f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2587{
2588 int startcol;
2589
2590 if ((State & INSERT) == 0)
2591 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002592 emsg(_("E785: complete() can only be used in Insert mode"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002593 return;
2594 }
2595
2596 /* Check for undo allowed here, because if something was already inserted
2597 * the line was already saved for undo and this check isn't done. */
2598 if (!undo_allowed())
2599 return;
2600
2601 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2602 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002603 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002604 return;
2605 }
2606
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002607 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002608 if (startcol <= 0)
2609 return;
2610
2611 set_completion(startcol - 1, argvars[1].vval.v_list);
2612}
2613
2614/*
2615 * "complete_add()" function
2616 */
2617 static void
2618f_complete_add(typval_T *argvars, typval_T *rettv)
2619{
2620 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2621}
2622
2623/*
2624 * "complete_check()" function
2625 */
2626 static void
2627f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2628{
2629 int saved = RedrawingDisabled;
2630
2631 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002632 ins_compl_check_keys(0, TRUE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002633 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002634 RedrawingDisabled = saved;
2635}
Bram Moolenaarfd133322019-03-29 12:20:27 +01002636
2637/*
2638 * "complete_info()" function
2639 */
2640 static void
2641f_complete_info(typval_T *argvars, typval_T *rettv)
2642{
2643 list_T *what_list = NULL;
2644
2645 if (rettv_dict_alloc(rettv) != OK)
2646 return;
2647
2648 if (argvars[0].v_type != VAR_UNKNOWN)
2649 {
2650 if (argvars[0].v_type != VAR_LIST)
2651 {
2652 emsg(_(e_listreq));
2653 return;
2654 }
2655 what_list = argvars[0].vval.v_list;
2656 }
2657 get_complete_info(what_list, rettv->vval.v_dict);
2658}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002659#endif
2660
2661/*
2662 * "confirm(message, buttons[, default [, type]])" function
2663 */
2664 static void
2665f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2666{
2667#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2668 char_u *message;
2669 char_u *buttons = NULL;
2670 char_u buf[NUMBUFLEN];
2671 char_u buf2[NUMBUFLEN];
2672 int def = 1;
2673 int type = VIM_GENERIC;
2674 char_u *typestr;
2675 int error = FALSE;
2676
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002677 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002678 if (message == NULL)
2679 error = TRUE;
2680 if (argvars[1].v_type != VAR_UNKNOWN)
2681 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002682 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002683 if (buttons == NULL)
2684 error = TRUE;
2685 if (argvars[2].v_type != VAR_UNKNOWN)
2686 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002687 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002688 if (argvars[3].v_type != VAR_UNKNOWN)
2689 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002690 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002691 if (typestr == NULL)
2692 error = TRUE;
2693 else
2694 {
2695 switch (TOUPPER_ASC(*typestr))
2696 {
2697 case 'E': type = VIM_ERROR; break;
2698 case 'Q': type = VIM_QUESTION; break;
2699 case 'I': type = VIM_INFO; break;
2700 case 'W': type = VIM_WARNING; break;
2701 case 'G': type = VIM_GENERIC; break;
2702 }
2703 }
2704 }
2705 }
2706 }
2707
2708 if (buttons == NULL || *buttons == NUL)
2709 buttons = (char_u *)_("&Ok");
2710
2711 if (!error)
2712 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2713 def, NULL, FALSE);
2714#endif
2715}
2716
2717/*
2718 * "copy()" function
2719 */
2720 static void
2721f_copy(typval_T *argvars, typval_T *rettv)
2722{
2723 item_copy(&argvars[0], rettv, FALSE, 0);
2724}
2725
2726#ifdef FEAT_FLOAT
2727/*
2728 * "cos()" function
2729 */
2730 static void
2731f_cos(typval_T *argvars, typval_T *rettv)
2732{
2733 float_T f = 0.0;
2734
2735 rettv->v_type = VAR_FLOAT;
2736 if (get_float_arg(argvars, &f) == OK)
2737 rettv->vval.v_float = cos(f);
2738 else
2739 rettv->vval.v_float = 0.0;
2740}
2741
2742/*
2743 * "cosh()" function
2744 */
2745 static void
2746f_cosh(typval_T *argvars, typval_T *rettv)
2747{
2748 float_T f = 0.0;
2749
2750 rettv->v_type = VAR_FLOAT;
2751 if (get_float_arg(argvars, &f) == OK)
2752 rettv->vval.v_float = cosh(f);
2753 else
2754 rettv->vval.v_float = 0.0;
2755}
2756#endif
2757
2758/*
2759 * "count()" function
2760 */
2761 static void
2762f_count(typval_T *argvars, typval_T *rettv)
2763{
2764 long n = 0;
2765 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002766 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002767
Bram Moolenaar9966b212017-07-28 16:46:57 +02002768 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002769 ic = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002770
2771 if (argvars[0].v_type == VAR_STRING)
2772 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002773 char_u *expr = tv_get_string_chk(&argvars[1]);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002774 char_u *p = argvars[0].vval.v_string;
2775 char_u *next;
2776
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002777 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002778 {
2779 if (ic)
2780 {
2781 size_t len = STRLEN(expr);
2782
2783 while (*p != NUL)
2784 {
2785 if (MB_STRNICMP(p, expr, len) == 0)
2786 {
2787 ++n;
2788 p += len;
2789 }
2790 else
2791 MB_PTR_ADV(p);
2792 }
2793 }
2794 else
2795 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2796 != NULL)
2797 {
2798 ++n;
2799 p = next + STRLEN(expr);
2800 }
2801 }
2802
2803 }
2804 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002805 {
2806 listitem_T *li;
2807 list_T *l;
2808 long idx;
2809
2810 if ((l = argvars[0].vval.v_list) != NULL)
2811 {
2812 li = l->lv_first;
2813 if (argvars[2].v_type != VAR_UNKNOWN)
2814 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002815 if (argvars[3].v_type != VAR_UNKNOWN)
2816 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002817 idx = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002818 if (!error)
2819 {
2820 li = list_find(l, idx);
2821 if (li == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002822 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002823 }
2824 }
2825 if (error)
2826 li = NULL;
2827 }
2828
2829 for ( ; li != NULL; li = li->li_next)
2830 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2831 ++n;
2832 }
2833 }
2834 else if (argvars[0].v_type == VAR_DICT)
2835 {
2836 int todo;
2837 dict_T *d;
2838 hashitem_T *hi;
2839
2840 if ((d = argvars[0].vval.v_dict) != NULL)
2841 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002842 if (argvars[2].v_type != VAR_UNKNOWN)
2843 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002844 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002845 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002846 }
2847
2848 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2849 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2850 {
2851 if (!HASHITEM_EMPTY(hi))
2852 {
2853 --todo;
2854 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2855 ++n;
2856 }
2857 }
2858 }
2859 }
2860 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002861 semsg(_(e_listdictarg), "count()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002862 rettv->vval.v_number = n;
2863}
2864
2865/*
2866 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2867 *
2868 * Checks the existence of a cscope connection.
2869 */
2870 static void
2871f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2872{
2873#ifdef FEAT_CSCOPE
2874 int num = 0;
2875 char_u *dbpath = NULL;
2876 char_u *prepend = NULL;
2877 char_u buf[NUMBUFLEN];
2878
2879 if (argvars[0].v_type != VAR_UNKNOWN
2880 && argvars[1].v_type != VAR_UNKNOWN)
2881 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002882 num = (int)tv_get_number(&argvars[0]);
2883 dbpath = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002884 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002885 prepend = tv_get_string_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002886 }
2887
2888 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2889#endif
2890}
2891
2892/*
2893 * "cursor(lnum, col)" function, or
2894 * "cursor(list)"
2895 *
2896 * Moves the cursor to the specified line and column.
2897 * Returns 0 when the position could be set, -1 otherwise.
2898 */
2899 static void
2900f_cursor(typval_T *argvars, typval_T *rettv)
2901{
2902 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002903 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002904 int set_curswant = TRUE;
2905
2906 rettv->vval.v_number = -1;
2907 if (argvars[1].v_type == VAR_UNKNOWN)
2908 {
2909 pos_T pos;
2910 colnr_T curswant = -1;
2911
2912 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2913 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002914 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002915 return;
2916 }
2917 line = pos.lnum;
2918 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002919 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002920 if (curswant >= 0)
2921 {
2922 curwin->w_curswant = curswant - 1;
2923 set_curswant = FALSE;
2924 }
2925 }
2926 else
2927 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002928 line = tv_get_lnum(argvars);
2929 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002930 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002931 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002932 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002933 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002934 return; /* type error; errmsg already given */
2935 if (line > 0)
2936 curwin->w_cursor.lnum = line;
2937 if (col > 0)
2938 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002939 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002940
2941 /* Make sure the cursor is in a valid position. */
2942 check_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002943 /* Correct cursor for multi-byte character. */
2944 if (has_mbyte)
2945 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002946
2947 curwin->w_set_curswant = set_curswant;
2948 rettv->vval.v_number = 0;
2949}
2950
Bram Moolenaar4f974752019-02-17 17:44:42 +01002951#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002952/*
2953 * "debugbreak()" function
2954 */
2955 static void
2956f_debugbreak(typval_T *argvars, typval_T *rettv)
2957{
2958 int pid;
2959
2960 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002961 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002962 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002963 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002964 else
2965 {
2966 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2967
2968 if (hProcess != NULL)
2969 {
2970 DebugBreakProcess(hProcess);
2971 CloseHandle(hProcess);
2972 rettv->vval.v_number = OK;
2973 }
2974 }
2975}
2976#endif
2977
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002978/*
2979 * "deepcopy()" function
2980 */
2981 static void
2982f_deepcopy(typval_T *argvars, typval_T *rettv)
2983{
2984 int noref = 0;
2985 int copyID;
2986
2987 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002988 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002989 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002990 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002991 else
2992 {
2993 copyID = get_copyID();
2994 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2995 }
2996}
2997
2998/*
2999 * "delete()" function
3000 */
3001 static void
3002f_delete(typval_T *argvars, typval_T *rettv)
3003{
3004 char_u nbuf[NUMBUFLEN];
3005 char_u *name;
3006 char_u *flags;
3007
3008 rettv->vval.v_number = -1;
3009 if (check_restricted() || check_secure())
3010 return;
3011
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003012 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003013 if (name == NULL || *name == NUL)
3014 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003015 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003016 return;
3017 }
3018
3019 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003020 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003021 else
3022 flags = (char_u *)"";
3023
3024 if (*flags == NUL)
3025 /* delete a file */
3026 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
3027 else if (STRCMP(flags, "d") == 0)
3028 /* delete an empty directory */
3029 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
3030 else if (STRCMP(flags, "rf") == 0)
3031 /* delete a directory recursively */
3032 rettv->vval.v_number = delete_recursive(name);
3033 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003034 semsg(_(e_invexpr2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003035}
3036
3037/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02003038 * "deletebufline()" function
3039 */
3040 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02003041f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02003042{
3043 buf_T *buf;
3044 linenr_T first, last;
3045 linenr_T lnum;
3046 long count;
3047 int is_curbuf;
3048 buf_T *curbuf_save = NULL;
3049 win_T *curwin_save = NULL;
3050 tabpage_T *tp;
3051 win_T *wp;
3052
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01003053 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003054 if (buf == NULL)
3055 {
3056 rettv->vval.v_number = 1; /* FAIL */
3057 return;
3058 }
3059 is_curbuf = buf == curbuf;
3060
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003061 first = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003062 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003063 last = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003064 else
3065 last = first;
3066
3067 if (buf->b_ml.ml_mfp == NULL || first < 1
3068 || first > buf->b_ml.ml_line_count || last < first)
3069 {
3070 rettv->vval.v_number = 1; /* FAIL */
3071 return;
3072 }
3073
3074 if (!is_curbuf)
3075 {
3076 curbuf_save = curbuf;
3077 curwin_save = curwin;
3078 curbuf = buf;
3079 find_win_for_curbuf();
3080 }
3081 if (last > curbuf->b_ml.ml_line_count)
3082 last = curbuf->b_ml.ml_line_count;
3083 count = last - first + 1;
3084
3085 // When coming here from Insert mode, sync undo, so that this can be
3086 // undone separately from what was previously inserted.
3087 if (u_sync_once == 2)
3088 {
3089 u_sync_once = 1; // notify that u_sync() was called
3090 u_sync(TRUE);
3091 }
3092
3093 if (u_save(first - 1, last + 1) == FAIL)
3094 {
3095 rettv->vval.v_number = 1; /* FAIL */
3096 return;
3097 }
3098
3099 for (lnum = first; lnum <= last; ++lnum)
3100 ml_delete(first, TRUE);
3101
3102 FOR_ALL_TAB_WINDOWS(tp, wp)
3103 if (wp->w_buffer == buf)
3104 {
3105 if (wp->w_cursor.lnum > last)
3106 wp->w_cursor.lnum -= count;
3107 else if (wp->w_cursor.lnum> first)
3108 wp->w_cursor.lnum = first;
3109 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
3110 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
3111 }
3112 check_cursor_col();
3113 deleted_lines_mark(first, count);
3114
3115 if (!is_curbuf)
3116 {
3117 curbuf = curbuf_save;
3118 curwin = curwin_save;
3119 }
3120}
3121
3122/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003123 * "did_filetype()" function
3124 */
3125 static void
3126f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3127{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003128 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003129}
3130
3131/*
3132 * "diff_filler()" function
3133 */
3134 static void
3135f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3136{
3137#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003138 rettv->vval.v_number = diff_check_fill(curwin, tv_get_lnum(argvars));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003139#endif
3140}
3141
3142/*
3143 * "diff_hlID()" function
3144 */
3145 static void
3146f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3147{
3148#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003149 linenr_T lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003150 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01003151 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003152 static int fnum = 0;
3153 static int change_start = 0;
3154 static int change_end = 0;
3155 static hlf_T hlID = (hlf_T)0;
3156 int filler_lines;
3157 int col;
3158
3159 if (lnum < 0) /* ignore type error in {lnum} arg */
3160 lnum = 0;
3161 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003162 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003163 || fnum != curbuf->b_fnum)
3164 {
3165 /* New line, buffer, change: need to get the values. */
3166 filler_lines = diff_check(curwin, lnum);
3167 if (filler_lines < 0)
3168 {
3169 if (filler_lines == -1)
3170 {
3171 change_start = MAXCOL;
3172 change_end = -1;
3173 if (diff_find_change(curwin, lnum, &change_start, &change_end))
3174 hlID = HLF_ADD; /* added line */
3175 else
3176 hlID = HLF_CHD; /* changed line */
3177 }
3178 else
3179 hlID = HLF_ADD; /* added line */
3180 }
3181 else
3182 hlID = (hlf_T)0;
3183 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003184 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003185 fnum = curbuf->b_fnum;
3186 }
3187
3188 if (hlID == HLF_CHD || hlID == HLF_TXD)
3189 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003190 col = tv_get_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003191 if (col >= change_start && col <= change_end)
3192 hlID = HLF_TXD; /* changed text */
3193 else
3194 hlID = HLF_CHD; /* changed line */
3195 }
3196 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
3197#endif
3198}
3199
3200/*
3201 * "empty({expr})" function
3202 */
3203 static void
3204f_empty(typval_T *argvars, typval_T *rettv)
3205{
3206 int n = FALSE;
3207
3208 switch (argvars[0].v_type)
3209 {
3210 case VAR_STRING:
3211 case VAR_FUNC:
3212 n = argvars[0].vval.v_string == NULL
3213 || *argvars[0].vval.v_string == NUL;
3214 break;
3215 case VAR_PARTIAL:
3216 n = FALSE;
3217 break;
3218 case VAR_NUMBER:
3219 n = argvars[0].vval.v_number == 0;
3220 break;
3221 case VAR_FLOAT:
3222#ifdef FEAT_FLOAT
3223 n = argvars[0].vval.v_float == 0.0;
3224 break;
3225#endif
3226 case VAR_LIST:
3227 n = argvars[0].vval.v_list == NULL
3228 || argvars[0].vval.v_list->lv_first == NULL;
3229 break;
3230 case VAR_DICT:
3231 n = argvars[0].vval.v_dict == NULL
3232 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
3233 break;
3234 case VAR_SPECIAL:
3235 n = argvars[0].vval.v_number != VVAL_TRUE;
3236 break;
3237
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003238 case VAR_BLOB:
3239 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003240 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
3241 break;
3242
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003243 case VAR_JOB:
3244#ifdef FEAT_JOB_CHANNEL
3245 n = argvars[0].vval.v_job == NULL
3246 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
3247 break;
3248#endif
3249 case VAR_CHANNEL:
3250#ifdef FEAT_JOB_CHANNEL
3251 n = argvars[0].vval.v_channel == NULL
3252 || !channel_is_open(argvars[0].vval.v_channel);
3253 break;
3254#endif
3255 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01003256 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003257 n = TRUE;
3258 break;
3259 }
3260
3261 rettv->vval.v_number = n;
3262}
3263
3264/*
3265 * "escape({string}, {chars})" function
3266 */
3267 static void
3268f_escape(typval_T *argvars, typval_T *rettv)
3269{
3270 char_u buf[NUMBUFLEN];
3271
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003272 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
3273 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003274 rettv->v_type = VAR_STRING;
3275}
3276
3277/*
3278 * "eval()" function
3279 */
3280 static void
3281f_eval(typval_T *argvars, typval_T *rettv)
3282{
3283 char_u *s, *p;
3284
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003285 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003286 if (s != NULL)
3287 s = skipwhite(s);
3288
3289 p = s;
3290 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3291 {
3292 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003293 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003294 need_clr_eos = FALSE;
3295 rettv->v_type = VAR_NUMBER;
3296 rettv->vval.v_number = 0;
3297 }
3298 else if (*s != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003299 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003300}
3301
3302/*
3303 * "eventhandler()" function
3304 */
3305 static void
3306f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3307{
3308 rettv->vval.v_number = vgetc_busy;
3309}
3310
3311/*
3312 * "executable()" function
3313 */
3314 static void
3315f_executable(typval_T *argvars, typval_T *rettv)
3316{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003317 char_u *name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003318
3319 /* Check in $PATH and also check directly if there is a directory name. */
3320 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
3321 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
3322}
3323
3324static garray_T redir_execute_ga;
3325
3326/*
3327 * Append "value[value_len]" to the execute() output.
3328 */
3329 void
3330execute_redir_str(char_u *value, int value_len)
3331{
3332 int len;
3333
3334 if (value_len == -1)
3335 len = (int)STRLEN(value); /* Append the entire string */
3336 else
3337 len = value_len; /* Append only "value_len" characters */
3338 if (ga_grow(&redir_execute_ga, len) == OK)
3339 {
3340 mch_memmove((char *)redir_execute_ga.ga_data
3341 + redir_execute_ga.ga_len, value, len);
3342 redir_execute_ga.ga_len += len;
3343 }
3344}
3345
3346/*
3347 * Get next line from a list.
3348 * Called by do_cmdline() to get the next line.
3349 * Returns allocated string, or NULL for end of function.
3350 */
3351
3352 static char_u *
3353get_list_line(
3354 int c UNUSED,
3355 void *cookie,
3356 int indent UNUSED)
3357{
3358 listitem_T **p = (listitem_T **)cookie;
3359 listitem_T *item = *p;
3360 char_u buf[NUMBUFLEN];
3361 char_u *s;
3362
3363 if (item == NULL)
3364 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003365 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003366 *p = item->li_next;
3367 return s == NULL ? NULL : vim_strsave(s);
3368}
3369
3370/*
3371 * "execute()" function
3372 */
3373 static void
3374f_execute(typval_T *argvars, typval_T *rettv)
3375{
3376 char_u *cmd = NULL;
3377 list_T *list = NULL;
3378 int save_msg_silent = msg_silent;
3379 int save_emsg_silent = emsg_silent;
3380 int save_emsg_noredir = emsg_noredir;
3381 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003382 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003383 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003384 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003385 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003386
3387 rettv->vval.v_string = NULL;
3388 rettv->v_type = VAR_STRING;
3389
3390 if (argvars[0].v_type == VAR_LIST)
3391 {
3392 list = argvars[0].vval.v_list;
3393 if (list == NULL || list->lv_first == NULL)
3394 /* empty list, no commands, empty output */
3395 return;
3396 ++list->lv_refcount;
3397 }
3398 else
3399 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003400 cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003401 if (cmd == NULL)
3402 return;
3403 }
3404
3405 if (argvars[1].v_type != VAR_UNKNOWN)
3406 {
3407 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003408 char_u *s = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003409
3410 if (s == NULL)
3411 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003412 if (*s == NUL)
3413 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003414 if (STRNCMP(s, "silent", 6) == 0)
3415 ++msg_silent;
3416 if (STRCMP(s, "silent!") == 0)
3417 {
3418 emsg_silent = TRUE;
3419 emsg_noredir = TRUE;
3420 }
3421 }
3422 else
3423 ++msg_silent;
3424
3425 if (redir_execute)
3426 save_ga = redir_execute_ga;
3427 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3428 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003429 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003430 if (!echo_output)
3431 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003432
3433 if (cmd != NULL)
3434 do_cmdline_cmd(cmd);
3435 else
3436 {
3437 listitem_T *item = list->lv_first;
3438
3439 do_cmdline(NULL, get_list_line, (void *)&item,
3440 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3441 --list->lv_refcount;
3442 }
3443
Bram Moolenaard297f352017-01-29 20:31:21 +01003444 /* Need to append a NUL to the result. */
3445 if (ga_grow(&redir_execute_ga, 1) == OK)
3446 {
3447 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3448 rettv->vval.v_string = redir_execute_ga.ga_data;
3449 }
3450 else
3451 {
3452 ga_clear(&redir_execute_ga);
3453 rettv->vval.v_string = NULL;
3454 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003455 msg_silent = save_msg_silent;
3456 emsg_silent = save_emsg_silent;
3457 emsg_noredir = save_emsg_noredir;
3458
3459 redir_execute = save_redir_execute;
3460 if (redir_execute)
3461 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003462 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003463
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003464 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003465 if (echo_output)
3466 // When not working silently: put it in column zero. A following
3467 // "echon" will overwrite the message, unavoidably.
3468 msg_col = 0;
3469 else
3470 // When working silently: Put it back where it was, since nothing
3471 // should have been written.
3472 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003473}
3474
3475/*
3476 * "exepath()" function
3477 */
3478 static void
3479f_exepath(typval_T *argvars, typval_T *rettv)
3480{
3481 char_u *p = NULL;
3482
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003483 (void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003484 rettv->v_type = VAR_STRING;
3485 rettv->vval.v_string = p;
3486}
3487
3488/*
3489 * "exists()" function
3490 */
3491 static void
3492f_exists(typval_T *argvars, typval_T *rettv)
3493{
3494 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003495 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003496
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003497 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003498 if (*p == '$') /* environment variable */
3499 {
3500 /* first try "normal" environment variables (fast) */
3501 if (mch_getenv(p + 1) != NULL)
3502 n = TRUE;
3503 else
3504 {
3505 /* try expanding things like $VIM and ${HOME} */
3506 p = expand_env_save(p);
3507 if (p != NULL && *p != '$')
3508 n = TRUE;
3509 vim_free(p);
3510 }
3511 }
3512 else if (*p == '&' || *p == '+') /* option */
3513 {
3514 n = (get_option_tv(&p, NULL, TRUE) == OK);
3515 if (*skipwhite(p) != NUL)
3516 n = FALSE; /* trailing garbage */
3517 }
3518 else if (*p == '*') /* internal or user defined function */
3519 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003520 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003521 }
3522 else if (*p == ':')
3523 {
3524 n = cmd_exists(p + 1);
3525 }
3526 else if (*p == '#')
3527 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003528 if (p[1] == '#')
3529 n = autocmd_supported(p + 2);
3530 else
3531 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003532 }
3533 else /* internal variable */
3534 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003535 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003536 }
3537
3538 rettv->vval.v_number = n;
3539}
3540
3541#ifdef FEAT_FLOAT
3542/*
3543 * "exp()" function
3544 */
3545 static void
3546f_exp(typval_T *argvars, typval_T *rettv)
3547{
3548 float_T f = 0.0;
3549
3550 rettv->v_type = VAR_FLOAT;
3551 if (get_float_arg(argvars, &f) == OK)
3552 rettv->vval.v_float = exp(f);
3553 else
3554 rettv->vval.v_float = 0.0;
3555}
3556#endif
3557
3558/*
3559 * "expand()" function
3560 */
3561 static void
3562f_expand(typval_T *argvars, typval_T *rettv)
3563{
3564 char_u *s;
3565 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003566 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003567 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3568 expand_T xpc;
3569 int error = FALSE;
3570 char_u *result;
3571
3572 rettv->v_type = VAR_STRING;
3573 if (argvars[1].v_type != VAR_UNKNOWN
3574 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003575 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003576 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003577 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003578
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003579 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003580 if (*s == '%' || *s == '#' || *s == '<')
3581 {
3582 ++emsg_off;
3583 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3584 --emsg_off;
3585 if (rettv->v_type == VAR_LIST)
3586 {
3587 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3588 list_append_string(rettv->vval.v_list, result, -1);
3589 else
3590 vim_free(result);
3591 }
3592 else
3593 rettv->vval.v_string = result;
3594 }
3595 else
3596 {
3597 /* When the optional second argument is non-zero, don't remove matches
3598 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3599 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003600 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003601 options |= WILD_KEEP_ALL;
3602 if (!error)
3603 {
3604 ExpandInit(&xpc);
3605 xpc.xp_context = EXPAND_FILES;
3606 if (p_wic)
3607 options += WILD_ICASE;
3608 if (rettv->v_type == VAR_STRING)
3609 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3610 options, WILD_ALL);
3611 else if (rettv_list_alloc(rettv) != FAIL)
3612 {
3613 int i;
3614
3615 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3616 for (i = 0; i < xpc.xp_numfiles; i++)
3617 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3618 ExpandCleanup(&xpc);
3619 }
3620 }
3621 else
3622 rettv->vval.v_string = NULL;
3623 }
3624}
3625
3626/*
3627 * "extend(list, list [, idx])" function
3628 * "extend(dict, dict [, action])" function
3629 */
3630 static void
3631f_extend(typval_T *argvars, typval_T *rettv)
3632{
3633 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3634
3635 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3636 {
3637 list_T *l1, *l2;
3638 listitem_T *item;
3639 long before;
3640 int error = FALSE;
3641
3642 l1 = argvars[0].vval.v_list;
3643 l2 = argvars[1].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003644 if (l1 != NULL && !var_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003645 && l2 != NULL)
3646 {
3647 if (argvars[2].v_type != VAR_UNKNOWN)
3648 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003649 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003650 if (error)
3651 return; /* type error; errmsg already given */
3652
3653 if (before == l1->lv_len)
3654 item = NULL;
3655 else
3656 {
3657 item = list_find(l1, before);
3658 if (item == NULL)
3659 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003660 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003661 return;
3662 }
3663 }
3664 }
3665 else
3666 item = NULL;
3667 list_extend(l1, l2, item);
3668
3669 copy_tv(&argvars[0], rettv);
3670 }
3671 }
3672 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3673 {
3674 dict_T *d1, *d2;
3675 char_u *action;
3676 int i;
3677
3678 d1 = argvars[0].vval.v_dict;
3679 d2 = argvars[1].vval.v_dict;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003680 if (d1 != NULL && !var_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003681 && d2 != NULL)
3682 {
3683 /* Check the third argument. */
3684 if (argvars[2].v_type != VAR_UNKNOWN)
3685 {
3686 static char *(av[]) = {"keep", "force", "error"};
3687
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003688 action = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003689 if (action == NULL)
3690 return; /* type error; errmsg already given */
3691 for (i = 0; i < 3; ++i)
3692 if (STRCMP(action, av[i]) == 0)
3693 break;
3694 if (i == 3)
3695 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003696 semsg(_(e_invarg2), action);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003697 return;
3698 }
3699 }
3700 else
3701 action = (char_u *)"force";
3702
3703 dict_extend(d1, d2, action);
3704
3705 copy_tv(&argvars[0], rettv);
3706 }
3707 }
3708 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003709 semsg(_(e_listdictarg), "extend()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003710}
3711
3712/*
3713 * "feedkeys()" function
3714 */
3715 static void
3716f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3717{
3718 int remap = TRUE;
3719 int insert = FALSE;
3720 char_u *keys, *flags;
3721 char_u nbuf[NUMBUFLEN];
3722 int typed = FALSE;
3723 int execute = FALSE;
3724 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003725 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003726 char_u *keys_esc;
3727
3728 /* This is not allowed in the sandbox. If the commands would still be
3729 * executed in the sandbox it would be OK, but it probably happens later,
3730 * when "sandbox" is no longer set. */
3731 if (check_secure())
3732 return;
3733
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003734 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003735
3736 if (argvars[1].v_type != VAR_UNKNOWN)
3737 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003738 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003739 for ( ; *flags != NUL; ++flags)
3740 {
3741 switch (*flags)
3742 {
3743 case 'n': remap = FALSE; break;
3744 case 'm': remap = TRUE; break;
3745 case 't': typed = TRUE; break;
3746 case 'i': insert = TRUE; break;
3747 case 'x': execute = TRUE; break;
3748 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003749 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003750 }
3751 }
3752 }
3753
3754 if (*keys != NUL || execute)
3755 {
3756 /* Need to escape K_SPECIAL and CSI before putting the string in the
3757 * typeahead buffer. */
3758 keys_esc = vim_strsave_escape_csi(keys);
3759 if (keys_esc != NULL)
3760 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003761 if (lowlevel)
3762 {
3763#ifdef USE_INPUT_BUF
3764 add_to_input_buf(keys, (int)STRLEN(keys));
3765#else
3766 emsg(_("E980: lowlevel input not supported"));
3767#endif
3768 }
3769 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003770 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003771 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003772 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003773 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003774#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003775 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003776#endif
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003777 )
3778 typebuf_was_filled = TRUE;
3779 }
3780 vim_free(keys_esc);
3781
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003782 if (execute)
3783 {
3784 int save_msg_scroll = msg_scroll;
3785
3786 /* Avoid a 1 second delay when the keys start Insert mode. */
3787 msg_scroll = FALSE;
3788
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003789 if (!dangerous)
3790 ++ex_normal_busy;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02003791 exec_normal(TRUE, FALSE, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003792 if (!dangerous)
3793 --ex_normal_busy;
3794
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003795 msg_scroll |= save_msg_scroll;
3796 }
3797 }
3798 }
3799}
3800
3801/*
3802 * "filereadable()" function
3803 */
3804 static void
3805f_filereadable(typval_T *argvars, typval_T *rettv)
3806{
3807 int fd;
3808 char_u *p;
3809 int n;
3810
3811#ifndef O_NONBLOCK
3812# define O_NONBLOCK 0
3813#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003814 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003815 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3816 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3817 {
3818 n = TRUE;
3819 close(fd);
3820 }
3821 else
3822 n = FALSE;
3823
3824 rettv->vval.v_number = n;
3825}
3826
3827/*
3828 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3829 * rights to write into.
3830 */
3831 static void
3832f_filewritable(typval_T *argvars, typval_T *rettv)
3833{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003834 rettv->vval.v_number = filewritable(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003835}
3836
3837 static void
3838findfilendir(
3839 typval_T *argvars UNUSED,
3840 typval_T *rettv,
3841 int find_what UNUSED)
3842{
3843#ifdef FEAT_SEARCHPATH
3844 char_u *fname;
3845 char_u *fresult = NULL;
3846 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3847 char_u *p;
3848 char_u pathbuf[NUMBUFLEN];
3849 int count = 1;
3850 int first = TRUE;
3851 int error = FALSE;
3852#endif
3853
3854 rettv->vval.v_string = NULL;
3855 rettv->v_type = VAR_STRING;
3856
3857#ifdef FEAT_SEARCHPATH
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003858 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003859
3860 if (argvars[1].v_type != VAR_UNKNOWN)
3861 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003862 p = tv_get_string_buf_chk(&argvars[1], pathbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003863 if (p == NULL)
3864 error = TRUE;
3865 else
3866 {
3867 if (*p != NUL)
3868 path = p;
3869
3870 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003871 count = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003872 }
3873 }
3874
3875 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3876 error = TRUE;
3877
3878 if (*fname != NUL && !error)
3879 {
3880 do
3881 {
3882 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3883 vim_free(fresult);
3884 fresult = find_file_in_path_option(first ? fname : NULL,
3885 first ? (int)STRLEN(fname) : 0,
3886 0, first, path,
3887 find_what,
3888 curbuf->b_ffname,
3889 find_what == FINDFILE_DIR
3890 ? (char_u *)"" : curbuf->b_p_sua);
3891 first = FALSE;
3892
3893 if (fresult != NULL && rettv->v_type == VAR_LIST)
3894 list_append_string(rettv->vval.v_list, fresult, -1);
3895
3896 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3897 }
3898
3899 if (rettv->v_type == VAR_STRING)
3900 rettv->vval.v_string = fresult;
3901#endif
3902}
3903
3904/*
3905 * "filter()" function
3906 */
3907 static void
3908f_filter(typval_T *argvars, typval_T *rettv)
3909{
3910 filter_map(argvars, rettv, FALSE);
3911}
3912
3913/*
3914 * "finddir({fname}[, {path}[, {count}]])" function
3915 */
3916 static void
3917f_finddir(typval_T *argvars, typval_T *rettv)
3918{
3919 findfilendir(argvars, rettv, FINDFILE_DIR);
3920}
3921
3922/*
3923 * "findfile({fname}[, {path}[, {count}]])" function
3924 */
3925 static void
3926f_findfile(typval_T *argvars, typval_T *rettv)
3927{
3928 findfilendir(argvars, rettv, FINDFILE_FILE);
3929}
3930
3931#ifdef FEAT_FLOAT
3932/*
3933 * "float2nr({float})" function
3934 */
3935 static void
3936f_float2nr(typval_T *argvars, typval_T *rettv)
3937{
3938 float_T f = 0.0;
3939
3940 if (get_float_arg(argvars, &f) == OK)
3941 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003942 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003943 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003944 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003945 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003946 else
3947 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003948 }
3949}
3950
3951/*
3952 * "floor({float})" function
3953 */
3954 static void
3955f_floor(typval_T *argvars, typval_T *rettv)
3956{
3957 float_T f = 0.0;
3958
3959 rettv->v_type = VAR_FLOAT;
3960 if (get_float_arg(argvars, &f) == OK)
3961 rettv->vval.v_float = floor(f);
3962 else
3963 rettv->vval.v_float = 0.0;
3964}
3965
3966/*
3967 * "fmod()" function
3968 */
3969 static void
3970f_fmod(typval_T *argvars, typval_T *rettv)
3971{
3972 float_T fx = 0.0, fy = 0.0;
3973
3974 rettv->v_type = VAR_FLOAT;
3975 if (get_float_arg(argvars, &fx) == OK
3976 && get_float_arg(&argvars[1], &fy) == OK)
3977 rettv->vval.v_float = fmod(fx, fy);
3978 else
3979 rettv->vval.v_float = 0.0;
3980}
3981#endif
3982
3983/*
3984 * "fnameescape({string})" function
3985 */
3986 static void
3987f_fnameescape(typval_T *argvars, typval_T *rettv)
3988{
3989 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003990 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003991 rettv->v_type = VAR_STRING;
3992}
3993
3994/*
3995 * "fnamemodify({fname}, {mods})" function
3996 */
3997 static void
3998f_fnamemodify(typval_T *argvars, typval_T *rettv)
3999{
4000 char_u *fname;
4001 char_u *mods;
4002 int usedlen = 0;
4003 int len;
4004 char_u *fbuf = NULL;
4005 char_u buf[NUMBUFLEN];
4006
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004007 fname = tv_get_string_chk(&argvars[0]);
4008 mods = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004009 if (fname == NULL || mods == NULL)
4010 fname = NULL;
4011 else
4012 {
4013 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02004014 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004015 }
4016
4017 rettv->v_type = VAR_STRING;
4018 if (fname == NULL)
4019 rettv->vval.v_string = NULL;
4020 else
4021 rettv->vval.v_string = vim_strnsave(fname, len);
4022 vim_free(fbuf);
4023}
4024
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004025/*
4026 * "foldclosed()" function
4027 */
4028 static void
4029foldclosed_both(
4030 typval_T *argvars UNUSED,
4031 typval_T *rettv,
4032 int end UNUSED)
4033{
4034#ifdef FEAT_FOLDING
4035 linenr_T lnum;
4036 linenr_T first, last;
4037
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004038 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004039 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4040 {
4041 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
4042 {
4043 if (end)
4044 rettv->vval.v_number = (varnumber_T)last;
4045 else
4046 rettv->vval.v_number = (varnumber_T)first;
4047 return;
4048 }
4049 }
4050#endif
4051 rettv->vval.v_number = -1;
4052}
4053
4054/*
4055 * "foldclosed()" function
4056 */
4057 static void
4058f_foldclosed(typval_T *argvars, typval_T *rettv)
4059{
4060 foldclosed_both(argvars, rettv, FALSE);
4061}
4062
4063/*
4064 * "foldclosedend()" function
4065 */
4066 static void
4067f_foldclosedend(typval_T *argvars, typval_T *rettv)
4068{
4069 foldclosed_both(argvars, rettv, TRUE);
4070}
4071
4072/*
4073 * "foldlevel()" function
4074 */
4075 static void
4076f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4077{
4078#ifdef FEAT_FOLDING
4079 linenr_T lnum;
4080
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004081 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004082 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4083 rettv->vval.v_number = foldLevel(lnum);
4084#endif
4085}
4086
4087/*
4088 * "foldtext()" function
4089 */
4090 static void
4091f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
4092{
4093#ifdef FEAT_FOLDING
4094 linenr_T foldstart;
4095 linenr_T foldend;
4096 char_u *dashes;
4097 linenr_T lnum;
4098 char_u *s;
4099 char_u *r;
4100 int len;
4101 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004102 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004103#endif
4104
4105 rettv->v_type = VAR_STRING;
4106 rettv->vval.v_string = NULL;
4107#ifdef FEAT_FOLDING
4108 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
4109 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
4110 dashes = get_vim_var_str(VV_FOLDDASHES);
4111 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
4112 && dashes != NULL)
4113 {
4114 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02004115 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004116 if (!linewhite(lnum))
4117 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004118
4119 /* Find interesting text in this line. */
4120 s = skipwhite(ml_get(lnum));
4121 /* skip C comment-start */
4122 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
4123 {
4124 s = skipwhite(s + 2);
4125 if (*skipwhite(s) == NUL
4126 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
4127 {
4128 s = skipwhite(ml_get(lnum + 1));
4129 if (*s == '*')
4130 s = skipwhite(s + 1);
4131 }
4132 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02004133 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01004134 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004135 r = alloc((unsigned)(STRLEN(txt)
4136 + STRLEN(dashes) /* for %s */
4137 + 20 /* for %3ld */
4138 + STRLEN(s))); /* concatenated */
4139 if (r != NULL)
4140 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02004141 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004142 len = (int)STRLEN(r);
4143 STRCAT(r, s);
4144 /* remove 'foldmarker' and 'commentstring' */
4145 foldtext_cleanup(r + len);
4146 rettv->vval.v_string = r;
4147 }
4148 }
4149#endif
4150}
4151
4152/*
4153 * "foldtextresult(lnum)" function
4154 */
4155 static void
4156f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
4157{
4158#ifdef FEAT_FOLDING
4159 linenr_T lnum;
4160 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004161 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004162 foldinfo_T foldinfo;
4163 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004164 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004165#endif
4166
4167 rettv->v_type = VAR_STRING;
4168 rettv->vval.v_string = NULL;
4169#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004170 if (entered)
4171 return; /* reject recursive use */
4172 entered = TRUE;
4173
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004174 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004175 /* treat illegal types and illegal string values for {lnum} the same */
4176 if (lnum < 0)
4177 lnum = 0;
4178 fold_count = foldedCount(curwin, lnum, &foldinfo);
4179 if (fold_count > 0)
4180 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01004181 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
4182 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004183 if (text == buf)
4184 text = vim_strsave(text);
4185 rettv->vval.v_string = text;
4186 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004187
4188 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004189#endif
4190}
4191
4192/*
4193 * "foreground()" function
4194 */
4195 static void
4196f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4197{
4198#ifdef FEAT_GUI
4199 if (gui.in_use)
4200 gui_mch_set_foreground();
4201#else
Bram Moolenaar4f974752019-02-17 17:44:42 +01004202# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004203 win32_set_foreground();
4204# endif
4205#endif
4206}
4207
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004208 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004209common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004210{
4211 char_u *s;
4212 char_u *name;
4213 int use_string = FALSE;
4214 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004215 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004216
4217 if (argvars[0].v_type == VAR_FUNC)
4218 {
4219 /* function(MyFunc, [arg], dict) */
4220 s = argvars[0].vval.v_string;
4221 }
4222 else if (argvars[0].v_type == VAR_PARTIAL
4223 && argvars[0].vval.v_partial != NULL)
4224 {
4225 /* function(dict.MyFunc, [arg]) */
4226 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004227 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004228 }
4229 else
4230 {
4231 /* function('MyFunc', [arg], dict) */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004232 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004233 use_string = TRUE;
4234 }
4235
Bram Moolenaar843b8842016-08-21 14:36:15 +02004236 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004237 {
4238 name = s;
4239 trans_name = trans_function_name(&name, FALSE,
4240 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4241 if (*name != NUL)
4242 s = NULL;
4243 }
4244
Bram Moolenaar843b8842016-08-21 14:36:15 +02004245 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4246 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004247 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004248 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004249 else if (trans_name != NULL && (is_funcref
4250 ? find_func(trans_name) == NULL
4251 : !translated_function_exists(trans_name)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004252 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004253 else
4254 {
4255 int dict_idx = 0;
4256 int arg_idx = 0;
4257 list_T *list = NULL;
4258
4259 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4260 {
4261 char sid_buf[25];
4262 int off = *s == 's' ? 2 : 5;
4263
4264 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4265 * also be called from another script. Using trans_function_name()
4266 * would also work, but some plugins depend on the name being
4267 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004268 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004269 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
4270 if (name != NULL)
4271 {
4272 STRCPY(name, sid_buf);
4273 STRCAT(name, s + off);
4274 }
4275 }
4276 else
4277 name = vim_strsave(s);
4278
4279 if (argvars[1].v_type != VAR_UNKNOWN)
4280 {
4281 if (argvars[2].v_type != VAR_UNKNOWN)
4282 {
4283 /* function(name, [args], dict) */
4284 arg_idx = 1;
4285 dict_idx = 2;
4286 }
4287 else if (argvars[1].v_type == VAR_DICT)
4288 /* function(name, dict) */
4289 dict_idx = 1;
4290 else
4291 /* function(name, [args]) */
4292 arg_idx = 1;
4293 if (dict_idx > 0)
4294 {
4295 if (argvars[dict_idx].v_type != VAR_DICT)
4296 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004297 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004298 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004299 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004300 }
4301 if (argvars[dict_idx].vval.v_dict == NULL)
4302 dict_idx = 0;
4303 }
4304 if (arg_idx > 0)
4305 {
4306 if (argvars[arg_idx].v_type != VAR_LIST)
4307 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004308 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004309 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004310 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004311 }
4312 list = argvars[arg_idx].vval.v_list;
4313 if (list == NULL || list->lv_len == 0)
4314 arg_idx = 0;
4315 }
4316 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004317 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004318 {
4319 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
4320
4321 /* result is a VAR_PARTIAL */
4322 if (pt == NULL)
4323 vim_free(name);
4324 else
4325 {
4326 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4327 {
4328 listitem_T *li;
4329 int i = 0;
4330 int arg_len = 0;
4331 int lv_len = 0;
4332
4333 if (arg_pt != NULL)
4334 arg_len = arg_pt->pt_argc;
4335 if (list != NULL)
4336 lv_len = list->lv_len;
4337 pt->pt_argc = arg_len + lv_len;
4338 pt->pt_argv = (typval_T *)alloc(
4339 sizeof(typval_T) * pt->pt_argc);
4340 if (pt->pt_argv == NULL)
4341 {
4342 vim_free(pt);
4343 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004344 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004345 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004346 for (i = 0; i < arg_len; i++)
4347 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4348 if (lv_len > 0)
4349 for (li = list->lv_first; li != NULL;
4350 li = li->li_next)
4351 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004352 }
4353
4354 /* For "function(dict.func, [], dict)" and "func" is a partial
4355 * use "dict". That is backwards compatible. */
4356 if (dict_idx > 0)
4357 {
4358 /* The dict is bound explicitly, pt_auto is FALSE. */
4359 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4360 ++pt->pt_dict->dv_refcount;
4361 }
4362 else if (arg_pt != NULL)
4363 {
4364 /* If the dict was bound automatically the result is also
4365 * bound automatically. */
4366 pt->pt_dict = arg_pt->pt_dict;
4367 pt->pt_auto = arg_pt->pt_auto;
4368 if (pt->pt_dict != NULL)
4369 ++pt->pt_dict->dv_refcount;
4370 }
4371
4372 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004373 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4374 {
4375 pt->pt_func = arg_pt->pt_func;
4376 func_ptr_ref(pt->pt_func);
4377 vim_free(name);
4378 }
4379 else if (is_funcref)
4380 {
4381 pt->pt_func = find_func(trans_name);
4382 func_ptr_ref(pt->pt_func);
4383 vim_free(name);
4384 }
4385 else
4386 {
4387 pt->pt_name = name;
4388 func_ref(name);
4389 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004390 }
4391 rettv->v_type = VAR_PARTIAL;
4392 rettv->vval.v_partial = pt;
4393 }
4394 else
4395 {
4396 /* result is a VAR_FUNC */
4397 rettv->v_type = VAR_FUNC;
4398 rettv->vval.v_string = name;
4399 func_ref(name);
4400 }
4401 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004402theend:
4403 vim_free(trans_name);
4404}
4405
4406/*
4407 * "funcref()" function
4408 */
4409 static void
4410f_funcref(typval_T *argvars, typval_T *rettv)
4411{
4412 common_function(argvars, rettv, TRUE);
4413}
4414
4415/*
4416 * "function()" function
4417 */
4418 static void
4419f_function(typval_T *argvars, typval_T *rettv)
4420{
4421 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004422}
4423
4424/*
4425 * "garbagecollect()" function
4426 */
4427 static void
4428f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4429{
4430 /* This is postponed until we are back at the toplevel, because we may be
4431 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4432 want_garbage_collect = TRUE;
4433
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004434 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004435 garbage_collect_at_exit = TRUE;
4436}
4437
4438/*
4439 * "get()" function
4440 */
4441 static void
4442f_get(typval_T *argvars, typval_T *rettv)
4443{
4444 listitem_T *li;
4445 list_T *l;
4446 dictitem_T *di;
4447 dict_T *d;
4448 typval_T *tv = NULL;
4449
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004450 if (argvars[0].v_type == VAR_BLOB)
4451 {
4452 int error = FALSE;
4453 int idx = tv_get_number_chk(&argvars[1], &error);
4454
4455 if (!error)
4456 {
4457 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004458 if (idx < 0)
4459 idx = blob_len(argvars[0].vval.v_blob) + idx;
4460 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
4461 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004462 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004463 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004464 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004465 tv = rettv;
4466 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004467 }
4468 }
4469 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004470 {
4471 if ((l = argvars[0].vval.v_list) != NULL)
4472 {
4473 int error = FALSE;
4474
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004475 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004476 if (!error && li != NULL)
4477 tv = &li->li_tv;
4478 }
4479 }
4480 else if (argvars[0].v_type == VAR_DICT)
4481 {
4482 if ((d = argvars[0].vval.v_dict) != NULL)
4483 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004484 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004485 if (di != NULL)
4486 tv = &di->di_tv;
4487 }
4488 }
4489 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4490 {
4491 partial_T *pt;
4492 partial_T fref_pt;
4493
4494 if (argvars[0].v_type == VAR_PARTIAL)
4495 pt = argvars[0].vval.v_partial;
4496 else
4497 {
4498 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4499 fref_pt.pt_name = argvars[0].vval.v_string;
4500 pt = &fref_pt;
4501 }
4502
4503 if (pt != NULL)
4504 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004505 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004506 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004507
4508 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4509 {
4510 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004511 n = partial_name(pt);
4512 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004513 rettv->vval.v_string = NULL;
4514 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004515 {
4516 rettv->vval.v_string = vim_strsave(n);
4517 if (rettv->v_type == VAR_FUNC)
4518 func_ref(rettv->vval.v_string);
4519 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004520 }
4521 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004522 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004523 else if (STRCMP(what, "args") == 0)
4524 {
4525 rettv->v_type = VAR_LIST;
4526 if (rettv_list_alloc(rettv) == OK)
4527 {
4528 int i;
4529
4530 for (i = 0; i < pt->pt_argc; ++i)
4531 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4532 }
4533 }
4534 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004535 semsg(_(e_invarg2), what);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004536 return;
4537 }
4538 }
4539 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004540 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004541
4542 if (tv == NULL)
4543 {
4544 if (argvars[2].v_type != VAR_UNKNOWN)
4545 copy_tv(&argvars[2], rettv);
4546 }
4547 else
4548 copy_tv(tv, rettv);
4549}
4550
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004551/*
4552 * Returns buffer options, variables and other attributes in a dictionary.
4553 */
4554 static dict_T *
4555get_buffer_info(buf_T *buf)
4556{
4557 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004558 tabpage_T *tp;
4559 win_T *wp;
4560 list_T *windows;
4561
4562 dict = dict_alloc();
4563 if (dict == NULL)
4564 return NULL;
4565
Bram Moolenaare0be1672018-07-08 16:50:37 +02004566 dict_add_number(dict, "bufnr", buf->b_fnum);
4567 dict_add_string(dict, "name", buf->b_ffname);
4568 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4569 : buflist_findlnum(buf));
4570 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4571 dict_add_number(dict, "listed", buf->b_p_bl);
4572 dict_add_number(dict, "changed", bufIsChanged(buf));
4573 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4574 dict_add_number(dict, "hidden",
4575 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004576
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004577 /* Get a reference to buffer variables */
4578 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004579
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004580 /* List of windows displaying this buffer */
4581 windows = list_alloc();
4582 if (windows != NULL)
4583 {
4584 FOR_ALL_TAB_WINDOWS(tp, wp)
4585 if (wp->w_buffer == buf)
4586 list_append_number(windows, (varnumber_T)wp->w_id);
4587 dict_add_list(dict, "windows", windows);
4588 }
4589
4590#ifdef FEAT_SIGNS
4591 if (buf->b_signlist != NULL)
4592 {
4593 /* List of signs placed in this buffer */
4594 list_T *signs = list_alloc();
4595 if (signs != NULL)
4596 {
4597 get_buffer_signs(buf, signs);
4598 dict_add_list(dict, "signs", signs);
4599 }
4600 }
4601#endif
4602
4603 return dict;
4604}
4605
4606/*
4607 * "getbufinfo()" function
4608 */
4609 static void
4610f_getbufinfo(typval_T *argvars, typval_T *rettv)
4611{
4612 buf_T *buf = NULL;
4613 buf_T *argbuf = NULL;
4614 dict_T *d;
4615 int filtered = FALSE;
4616 int sel_buflisted = FALSE;
4617 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004618 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004619
4620 if (rettv_list_alloc(rettv) != OK)
4621 return;
4622
4623 /* List of all the buffers or selected buffers */
4624 if (argvars[0].v_type == VAR_DICT)
4625 {
4626 dict_T *sel_d = argvars[0].vval.v_dict;
4627
4628 if (sel_d != NULL)
4629 {
4630 dictitem_T *di;
4631
4632 filtered = TRUE;
4633
4634 di = dict_find(sel_d, (char_u *)"buflisted", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004635 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004636 sel_buflisted = TRUE;
4637
4638 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004639 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004640 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004641
4642 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004643 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004644 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004645 }
4646 }
4647 else if (argvars[0].v_type != VAR_UNKNOWN)
4648 {
4649 /* Information about one buffer. Argument specifies the buffer */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004650 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004651 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004652 argbuf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004653 --emsg_off;
4654 if (argbuf == NULL)
4655 return;
4656 }
4657
4658 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004659 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004660 {
4661 if (argbuf != NULL && argbuf != buf)
4662 continue;
4663 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004664 || (sel_buflisted && !buf->b_p_bl)
4665 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004666 continue;
4667
4668 d = get_buffer_info(buf);
4669 if (d != NULL)
4670 list_append_dict(rettv->vval.v_list, d);
4671 if (argbuf != NULL)
4672 return;
4673 }
4674}
4675
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004676/*
4677 * Get line or list of lines from buffer "buf" into "rettv".
4678 * Return a range (from start to end) of lines in rettv from the specified
4679 * buffer.
4680 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4681 */
4682 static void
4683get_buffer_lines(
4684 buf_T *buf,
4685 linenr_T start,
4686 linenr_T end,
4687 int retlist,
4688 typval_T *rettv)
4689{
4690 char_u *p;
4691
4692 rettv->v_type = VAR_STRING;
4693 rettv->vval.v_string = NULL;
4694 if (retlist && rettv_list_alloc(rettv) == FAIL)
4695 return;
4696
4697 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4698 return;
4699
4700 if (!retlist)
4701 {
4702 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4703 p = ml_get_buf(buf, start, FALSE);
4704 else
4705 p = (char_u *)"";
4706 rettv->vval.v_string = vim_strsave(p);
4707 }
4708 else
4709 {
4710 if (end < start)
4711 return;
4712
4713 if (start < 1)
4714 start = 1;
4715 if (end > buf->b_ml.ml_line_count)
4716 end = buf->b_ml.ml_line_count;
4717 while (start <= end)
4718 if (list_append_string(rettv->vval.v_list,
4719 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4720 break;
4721 }
4722}
4723
4724/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004725 * "getbufline()" function
4726 */
4727 static void
4728f_getbufline(typval_T *argvars, typval_T *rettv)
4729{
4730 linenr_T lnum;
4731 linenr_T end;
4732 buf_T *buf;
4733
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004734 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004735 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004736 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004737 --emsg_off;
4738
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004739 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004740 if (argvars[2].v_type == VAR_UNKNOWN)
4741 end = lnum;
4742 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004743 end = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004744
4745 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4746}
4747
4748/*
4749 * "getbufvar()" function
4750 */
4751 static void
4752f_getbufvar(typval_T *argvars, typval_T *rettv)
4753{
4754 buf_T *buf;
4755 buf_T *save_curbuf;
4756 char_u *varname;
4757 dictitem_T *v;
4758 int done = FALSE;
4759
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004760 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
4761 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004762 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004763 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004764
4765 rettv->v_type = VAR_STRING;
4766 rettv->vval.v_string = NULL;
4767
4768 if (buf != NULL && varname != NULL)
4769 {
4770 /* set curbuf to be our buf, temporarily */
4771 save_curbuf = curbuf;
4772 curbuf = buf;
4773
Bram Moolenaar30567352016-08-27 21:25:44 +02004774 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004775 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004776 if (varname[1] == NUL)
4777 {
4778 /* get all buffer-local options in a dict */
4779 dict_T *opts = get_winbuf_options(TRUE);
4780
4781 if (opts != NULL)
4782 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004783 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004784 done = TRUE;
4785 }
4786 }
4787 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4788 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004789 done = TRUE;
4790 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004791 else
4792 {
4793 /* Look up the variable. */
4794 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4795 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4796 'b', varname, FALSE);
4797 if (v != NULL)
4798 {
4799 copy_tv(&v->di_tv, rettv);
4800 done = TRUE;
4801 }
4802 }
4803
4804 /* restore previous notion of curbuf */
4805 curbuf = save_curbuf;
4806 }
4807
4808 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4809 /* use the default value */
4810 copy_tv(&argvars[2], rettv);
4811
4812 --emsg_off;
4813}
4814
4815/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004816 * "getchangelist()" function
4817 */
4818 static void
4819f_getchangelist(typval_T *argvars, typval_T *rettv)
4820{
4821#ifdef FEAT_JUMPLIST
4822 buf_T *buf;
4823 int i;
4824 list_T *l;
4825 dict_T *d;
4826#endif
4827
4828 if (rettv_list_alloc(rettv) != OK)
4829 return;
4830
4831#ifdef FEAT_JUMPLIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004832 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004833 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004834 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004835 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004836 if (buf == NULL)
4837 return;
4838
4839 l = list_alloc();
4840 if (l == NULL)
4841 return;
4842
4843 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4844 return;
4845 /*
4846 * The current window change list index tracks only the position in the
4847 * current buffer change list. For other buffers, use the change list
4848 * length as the current index.
4849 */
4850 list_append_number(rettv->vval.v_list,
4851 (varnumber_T)((buf == curwin->w_buffer)
4852 ? curwin->w_changelistidx : buf->b_changelistlen));
4853
4854 for (i = 0; i < buf->b_changelistlen; ++i)
4855 {
4856 if (buf->b_changelist[i].lnum == 0)
4857 continue;
4858 if ((d = dict_alloc()) == NULL)
4859 return;
4860 if (list_append_dict(l, d) == FAIL)
4861 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004862 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4863 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004864 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004865 }
4866#endif
4867}
4868/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004869 * "getchar()" function
4870 */
4871 static void
4872f_getchar(typval_T *argvars, typval_T *rettv)
4873{
4874 varnumber_T n;
4875 int error = FALSE;
4876
Bram Moolenaar84d93902018-09-11 20:10:20 +02004877#ifdef MESSAGE_QUEUE
4878 // vpeekc() used to check for messages, but that caused problems, invoking
4879 // a callback where it was not expected. Some plugins use getchar(1) in a
4880 // loop to await a message, therefore make sure we check for messages here.
4881 parse_queued_messages();
4882#endif
4883
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004884 /* Position the cursor. Needed after a message that ends in a space. */
4885 windgoto(msg_row, msg_col);
4886
4887 ++no_mapping;
4888 ++allow_keys;
4889 for (;;)
4890 {
4891 if (argvars[0].v_type == VAR_UNKNOWN)
4892 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004893 n = plain_vgetc();
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004894 else if (tv_get_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004895 /* getchar(1): only check if char avail */
4896 n = vpeekc_any();
4897 else if (error || vpeekc_any() == NUL)
4898 /* illegal argument or getchar(0) and no char avail: return zero */
4899 n = 0;
4900 else
4901 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004902 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004903
4904 if (n == K_IGNORE)
4905 continue;
4906 break;
4907 }
4908 --no_mapping;
4909 --allow_keys;
4910
4911 set_vim_var_nr(VV_MOUSE_WIN, 0);
4912 set_vim_var_nr(VV_MOUSE_WINID, 0);
4913 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4914 set_vim_var_nr(VV_MOUSE_COL, 0);
4915
4916 rettv->vval.v_number = n;
4917 if (IS_SPECIAL(n) || mod_mask != 0)
4918 {
4919 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4920 int i = 0;
4921
4922 /* Turn a special key into three bytes, plus modifier. */
4923 if (mod_mask != 0)
4924 {
4925 temp[i++] = K_SPECIAL;
4926 temp[i++] = KS_MODIFIER;
4927 temp[i++] = mod_mask;
4928 }
4929 if (IS_SPECIAL(n))
4930 {
4931 temp[i++] = K_SPECIAL;
4932 temp[i++] = K_SECOND(n);
4933 temp[i++] = K_THIRD(n);
4934 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004935 else if (has_mbyte)
4936 i += (*mb_char2bytes)(n, temp + i);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004937 else
4938 temp[i++] = n;
4939 temp[i++] = NUL;
4940 rettv->v_type = VAR_STRING;
4941 rettv->vval.v_string = vim_strsave(temp);
4942
4943#ifdef FEAT_MOUSE
4944 if (is_mouse_key(n))
4945 {
4946 int row = mouse_row;
4947 int col = mouse_col;
4948 win_T *win;
4949 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004950 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004951 int winnr = 1;
4952
4953 if (row >= 0 && col >= 0)
4954 {
4955 /* Find the window at the mouse coordinates and compute the
4956 * text position. */
4957 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004958 if (win == NULL)
4959 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004960 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004961 for (wp = firstwin; wp != win; wp = wp->w_next)
4962 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004963 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4964 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4965 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4966 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4967 }
4968 }
4969#endif
4970 }
4971}
4972
4973/*
4974 * "getcharmod()" function
4975 */
4976 static void
4977f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4978{
4979 rettv->vval.v_number = mod_mask;
4980}
4981
4982/*
4983 * "getcharsearch()" function
4984 */
4985 static void
4986f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4987{
4988 if (rettv_dict_alloc(rettv) != FAIL)
4989 {
4990 dict_T *dict = rettv->vval.v_dict;
4991
Bram Moolenaare0be1672018-07-08 16:50:37 +02004992 dict_add_string(dict, "char", last_csearch());
4993 dict_add_number(dict, "forward", last_csearch_forward());
4994 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004995 }
4996}
4997
4998/*
4999 * "getcmdline()" function
5000 */
5001 static void
5002f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
5003{
5004 rettv->v_type = VAR_STRING;
5005 rettv->vval.v_string = get_cmdline_str();
5006}
5007
5008/*
5009 * "getcmdpos()" function
5010 */
5011 static void
5012f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
5013{
5014 rettv->vval.v_number = get_cmdline_pos() + 1;
5015}
5016
5017/*
5018 * "getcmdtype()" function
5019 */
5020 static void
5021f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
5022{
5023 rettv->v_type = VAR_STRING;
5024 rettv->vval.v_string = alloc(2);
5025 if (rettv->vval.v_string != NULL)
5026 {
5027 rettv->vval.v_string[0] = get_cmdline_type();
5028 rettv->vval.v_string[1] = NUL;
5029 }
5030}
5031
5032/*
5033 * "getcmdwintype()" function
5034 */
5035 static void
5036f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
5037{
5038 rettv->v_type = VAR_STRING;
5039 rettv->vval.v_string = NULL;
5040#ifdef FEAT_CMDWIN
5041 rettv->vval.v_string = alloc(2);
5042 if (rettv->vval.v_string != NULL)
5043 {
5044 rettv->vval.v_string[0] = cmdwin_type;
5045 rettv->vval.v_string[1] = NUL;
5046 }
5047#endif
5048}
5049
5050#if defined(FEAT_CMDL_COMPL)
5051/*
5052 * "getcompletion()" function
5053 */
5054 static void
5055f_getcompletion(typval_T *argvars, typval_T *rettv)
5056{
5057 char_u *pat;
5058 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005059 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005060 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
5061 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005062
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005063 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005064 filtered = tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005065
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005066 if (p_wic)
5067 options |= WILD_ICASE;
5068
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005069 /* For filtered results, 'wildignore' is used */
5070 if (!filtered)
5071 options |= WILD_KEEP_ALL;
5072
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005073 ExpandInit(&xpc);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005074 xpc.xp_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005075 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005076 xpc.xp_context = cmdcomplete_str_to_type(tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005077 if (xpc.xp_context == EXPAND_NOTHING)
5078 {
5079 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005080 semsg(_(e_invarg2), argvars[1].vval.v_string);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005081 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005082 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005083 return;
5084 }
5085
5086# if defined(FEAT_MENU)
5087 if (xpc.xp_context == EXPAND_MENUS)
5088 {
5089 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
5090 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5091 }
5092# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02005093#ifdef FEAT_CSCOPE
5094 if (xpc.xp_context == EXPAND_CSCOPE)
5095 {
5096 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
5097 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5098 }
5099#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02005100#ifdef FEAT_SIGNS
5101 if (xpc.xp_context == EXPAND_SIGN)
5102 {
5103 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
5104 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5105 }
5106#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005107
5108 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
5109 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
5110 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005111 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005112
5113 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
5114
5115 for (i = 0; i < xpc.xp_numfiles; i++)
5116 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5117 }
5118 vim_free(pat);
5119 ExpandCleanup(&xpc);
5120}
5121#endif
5122
5123/*
5124 * "getcwd()" function
5125 */
5126 static void
5127f_getcwd(typval_T *argvars, typval_T *rettv)
5128{
5129 win_T *wp = NULL;
5130 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01005131 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005132
5133 rettv->v_type = VAR_STRING;
5134 rettv->vval.v_string = NULL;
5135
Bram Moolenaar54591292018-02-09 20:53:59 +01005136 if (argvars[0].v_type == VAR_NUMBER && argvars[0].vval.v_number == -1)
5137 global = TRUE;
5138 else
5139 wp = find_tabwin(&argvars[0], &argvars[1]);
5140
5141 if (wp != NULL && wp->w_localdir != NULL)
5142 rettv->vval.v_string = vim_strsave(wp->w_localdir);
5143 else if (wp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005144 {
Bram Moolenaar54591292018-02-09 20:53:59 +01005145 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005146 rettv->vval.v_string = vim_strsave(globaldir);
5147 else
5148 {
5149 cwd = alloc(MAXPATHL);
5150 if (cwd != NULL)
5151 {
5152 if (mch_dirname(cwd, MAXPATHL) != FAIL)
5153 rettv->vval.v_string = vim_strsave(cwd);
5154 vim_free(cwd);
5155 }
5156 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005157 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02005158#ifdef BACKSLASH_IN_FILENAME
5159 if (rettv->vval.v_string != NULL)
5160 slash_adjust(rettv->vval.v_string);
5161#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005162}
5163
5164/*
5165 * "getfontname()" function
5166 */
5167 static void
5168f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
5169{
5170 rettv->v_type = VAR_STRING;
5171 rettv->vval.v_string = NULL;
5172#ifdef FEAT_GUI
5173 if (gui.in_use)
5174 {
5175 GuiFont font;
5176 char_u *name = NULL;
5177
5178 if (argvars[0].v_type == VAR_UNKNOWN)
5179 {
5180 /* Get the "Normal" font. Either the name saved by
5181 * hl_set_font_name() or from the font ID. */
5182 font = gui.norm_font;
5183 name = hl_get_font_name();
5184 }
5185 else
5186 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005187 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005188 if (STRCMP(name, "*") == 0) /* don't use font dialog */
5189 return;
5190 font = gui_mch_get_font(name, FALSE);
5191 if (font == NOFONT)
5192 return; /* Invalid font name, return empty string. */
5193 }
5194 rettv->vval.v_string = gui_mch_get_fontname(font, name);
5195 if (argvars[0].v_type != VAR_UNKNOWN)
5196 gui_mch_free_font(font);
5197 }
5198#endif
5199}
5200
5201/*
5202 * "getfperm({fname})" function
5203 */
5204 static void
5205f_getfperm(typval_T *argvars, typval_T *rettv)
5206{
5207 char_u *fname;
5208 stat_T st;
5209 char_u *perm = NULL;
5210 char_u flags[] = "rwx";
5211 int i;
5212
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005213 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005214
5215 rettv->v_type = VAR_STRING;
5216 if (mch_stat((char *)fname, &st) >= 0)
5217 {
5218 perm = vim_strsave((char_u *)"---------");
5219 if (perm != NULL)
5220 {
5221 for (i = 0; i < 9; i++)
5222 {
5223 if (st.st_mode & (1 << (8 - i)))
5224 perm[i] = flags[i % 3];
5225 }
5226 }
5227 }
5228 rettv->vval.v_string = perm;
5229}
5230
5231/*
5232 * "getfsize({fname})" function
5233 */
5234 static void
5235f_getfsize(typval_T *argvars, typval_T *rettv)
5236{
5237 char_u *fname;
5238 stat_T st;
5239
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005240 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005241
5242 rettv->v_type = VAR_NUMBER;
5243
5244 if (mch_stat((char *)fname, &st) >= 0)
5245 {
5246 if (mch_isdir(fname))
5247 rettv->vval.v_number = 0;
5248 else
5249 {
5250 rettv->vval.v_number = (varnumber_T)st.st_size;
5251
5252 /* non-perfect check for overflow */
5253 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5254 rettv->vval.v_number = -2;
5255 }
5256 }
5257 else
5258 rettv->vval.v_number = -1;
5259}
5260
5261/*
5262 * "getftime({fname})" function
5263 */
5264 static void
5265f_getftime(typval_T *argvars, typval_T *rettv)
5266{
5267 char_u *fname;
5268 stat_T st;
5269
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005270 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005271
5272 if (mch_stat((char *)fname, &st) >= 0)
5273 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5274 else
5275 rettv->vval.v_number = -1;
5276}
5277
5278/*
5279 * "getftype({fname})" function
5280 */
5281 static void
5282f_getftype(typval_T *argvars, typval_T *rettv)
5283{
5284 char_u *fname;
5285 stat_T st;
5286 char_u *type = NULL;
5287 char *t;
5288
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005289 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005290
5291 rettv->v_type = VAR_STRING;
5292 if (mch_lstat((char *)fname, &st) >= 0)
5293 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005294 if (S_ISREG(st.st_mode))
5295 t = "file";
5296 else if (S_ISDIR(st.st_mode))
5297 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005298 else if (S_ISLNK(st.st_mode))
5299 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005300 else if (S_ISBLK(st.st_mode))
5301 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005302 else if (S_ISCHR(st.st_mode))
5303 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005304 else if (S_ISFIFO(st.st_mode))
5305 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005306 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005307 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005308 else
5309 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005310 type = vim_strsave((char_u *)t);
5311 }
5312 rettv->vval.v_string = type;
5313}
5314
5315/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005316 * "getjumplist()" function
5317 */
5318 static void
5319f_getjumplist(typval_T *argvars, typval_T *rettv)
5320{
5321#ifdef FEAT_JUMPLIST
5322 win_T *wp;
5323 int i;
5324 list_T *l;
5325 dict_T *d;
5326#endif
5327
5328 if (rettv_list_alloc(rettv) != OK)
5329 return;
5330
5331#ifdef FEAT_JUMPLIST
5332 wp = find_tabwin(&argvars[0], &argvars[1]);
5333 if (wp == NULL)
5334 return;
5335
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01005336 cleanup_jumplist(wp, TRUE);
5337
Bram Moolenaar4f505882018-02-10 21:06:32 +01005338 l = list_alloc();
5339 if (l == NULL)
5340 return;
5341
5342 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5343 return;
5344 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5345
5346 for (i = 0; i < wp->w_jumplistlen; ++i)
5347 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005348 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5349 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005350 if ((d = dict_alloc()) == NULL)
5351 return;
5352 if (list_append_dict(l, d) == FAIL)
5353 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005354 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5355 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005356 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005357 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005358 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005359 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005360 }
5361#endif
5362}
5363
5364/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005365 * "getline(lnum, [end])" function
5366 */
5367 static void
5368f_getline(typval_T *argvars, typval_T *rettv)
5369{
5370 linenr_T lnum;
5371 linenr_T end;
5372 int retlist;
5373
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005374 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005375 if (argvars[1].v_type == VAR_UNKNOWN)
5376 {
5377 end = 0;
5378 retlist = FALSE;
5379 }
5380 else
5381 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005382 end = tv_get_lnum(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005383 retlist = TRUE;
5384 }
5385
5386 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5387}
5388
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005389#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005390 static void
5391get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5392{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005393 if (what_arg->v_type == VAR_UNKNOWN)
5394 {
5395 if (rettv_list_alloc(rettv) == OK)
5396 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005397 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005398 }
5399 else
5400 {
5401 if (rettv_dict_alloc(rettv) == OK)
5402 if (is_qf || (wp != NULL))
5403 {
5404 if (what_arg->v_type == VAR_DICT)
5405 {
5406 dict_T *d = what_arg->vval.v_dict;
5407
5408 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005409 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005410 }
5411 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005412 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +02005413 }
5414 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005415}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005416#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005417
5418/*
5419 * "getloclist()" function
5420 */
5421 static void
5422f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5423{
5424#ifdef FEAT_QUICKFIX
5425 win_T *wp;
5426
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005427 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005428 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5429#endif
5430}
5431
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005432/*
5433 * "getmatches()" function
5434 */
5435 static void
5436f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5437{
5438#ifdef FEAT_SEARCH_EXTRA
5439 dict_T *dict;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005440 matchitem_T *cur;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005441 int i;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005442 win_T *win = get_optional_window(argvars, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005443
Bram Moolenaaraff74912019-03-30 18:11:49 +01005444 if (rettv_list_alloc(rettv) == FAIL || win == NULL)
5445 return;
5446
5447 cur = win->w_match_head;
5448 while (cur != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005449 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005450 dict = dict_alloc();
5451 if (dict == NULL)
5452 return;
5453 if (cur->match.regprog == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005454 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005455 /* match added with matchaddpos() */
5456 for (i = 0; i < MAXPOSMATCH; ++i)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005457 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005458 llpos_T *llpos;
5459 char buf[6];
5460 list_T *l;
5461
5462 llpos = &cur->pos.pos[i];
5463 if (llpos->lnum == 0)
5464 break;
5465 l = list_alloc();
5466 if (l == NULL)
5467 break;
5468 list_append_number(l, (varnumber_T)llpos->lnum);
5469 if (llpos->col > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005470 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005471 list_append_number(l, (varnumber_T)llpos->col);
5472 list_append_number(l, (varnumber_T)llpos->len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005473 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005474 sprintf(buf, "pos%d", i + 1);
5475 dict_add_list(dict, buf, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005476 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005477 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005478 else
5479 {
5480 dict_add_string(dict, "pattern", cur->pattern);
5481 }
5482 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
5483 dict_add_number(dict, "priority", (long)cur->priority);
5484 dict_add_number(dict, "id", (long)cur->id);
5485# if defined(FEAT_CONCEAL)
5486 if (cur->conceal_char)
5487 {
5488 char_u buf[MB_MAXBYTES + 1];
5489
5490 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
5491 dict_add_string(dict, "conceal", (char_u *)&buf);
5492 }
5493# endif
5494 list_append_dict(rettv->vval.v_list, dict);
5495 cur = cur->next;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005496 }
5497#endif
5498}
5499
5500/*
5501 * "getpid()" function
5502 */
5503 static void
5504f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5505{
5506 rettv->vval.v_number = mch_get_pid();
5507}
5508
5509 static void
5510getpos_both(
5511 typval_T *argvars,
5512 typval_T *rettv,
5513 int getcurpos)
5514{
5515 pos_T *fp;
5516 list_T *l;
5517 int fnum = -1;
5518
5519 if (rettv_list_alloc(rettv) == OK)
5520 {
5521 l = rettv->vval.v_list;
5522 if (getcurpos)
5523 fp = &curwin->w_cursor;
5524 else
5525 fp = var2fpos(&argvars[0], TRUE, &fnum);
5526 if (fnum != -1)
5527 list_append_number(l, (varnumber_T)fnum);
5528 else
5529 list_append_number(l, (varnumber_T)0);
5530 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5531 : (varnumber_T)0);
5532 list_append_number(l, (fp != NULL)
5533 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5534 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005535 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005536 (varnumber_T)0);
5537 if (getcurpos)
5538 {
Bram Moolenaar19a66852019-03-07 11:25:32 +01005539 int save_set_curswant = curwin->w_set_curswant;
5540 colnr_T save_curswant = curwin->w_curswant;
5541 colnr_T save_virtcol = curwin->w_virtcol;
5542
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005543 update_curswant();
5544 list_append_number(l, curwin->w_curswant == MAXCOL ?
5545 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar19a66852019-03-07 11:25:32 +01005546
5547 // Do not change "curswant", as it is unexpected that a get
5548 // function has a side effect.
5549 if (save_set_curswant)
5550 {
5551 curwin->w_set_curswant = save_set_curswant;
5552 curwin->w_curswant = save_curswant;
5553 curwin->w_virtcol = save_virtcol;
5554 curwin->w_valid &= ~VALID_VIRTCOL;
5555 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005556 }
5557 }
5558 else
5559 rettv->vval.v_number = FALSE;
5560}
5561
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005562/*
5563 * "getcurpos()" function
5564 */
5565 static void
5566f_getcurpos(typval_T *argvars, typval_T *rettv)
5567{
5568 getpos_both(argvars, rettv, TRUE);
5569}
5570
5571/*
5572 * "getpos(string)" function
5573 */
5574 static void
5575f_getpos(typval_T *argvars, typval_T *rettv)
5576{
5577 getpos_both(argvars, rettv, FALSE);
5578}
5579
5580/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005581 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005582 */
5583 static void
5584f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5585{
5586#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005587 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005588#endif
5589}
5590
5591/*
5592 * "getreg()" function
5593 */
5594 static void
5595f_getreg(typval_T *argvars, typval_T *rettv)
5596{
5597 char_u *strregname;
5598 int regname;
5599 int arg2 = FALSE;
5600 int return_list = FALSE;
5601 int error = FALSE;
5602
5603 if (argvars[0].v_type != VAR_UNKNOWN)
5604 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005605 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005606 error = strregname == NULL;
5607 if (argvars[1].v_type != VAR_UNKNOWN)
5608 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005609 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005610 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005611 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005612 }
5613 }
5614 else
5615 strregname = get_vim_var_str(VV_REG);
5616
5617 if (error)
5618 return;
5619
5620 regname = (strregname == NULL ? '"' : *strregname);
5621 if (regname == 0)
5622 regname = '"';
5623
5624 if (return_list)
5625 {
5626 rettv->v_type = VAR_LIST;
5627 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5628 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5629 if (rettv->vval.v_list == NULL)
5630 (void)rettv_list_alloc(rettv);
5631 else
5632 ++rettv->vval.v_list->lv_refcount;
5633 }
5634 else
5635 {
5636 rettv->v_type = VAR_STRING;
5637 rettv->vval.v_string = get_reg_contents(regname,
5638 arg2 ? GREG_EXPR_SRC : 0);
5639 }
5640}
5641
5642/*
5643 * "getregtype()" function
5644 */
5645 static void
5646f_getregtype(typval_T *argvars, typval_T *rettv)
5647{
5648 char_u *strregname;
5649 int regname;
5650 char_u buf[NUMBUFLEN + 2];
5651 long reglen = 0;
5652
5653 if (argvars[0].v_type != VAR_UNKNOWN)
5654 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005655 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005656 if (strregname == NULL) /* type error; errmsg already given */
5657 {
5658 rettv->v_type = VAR_STRING;
5659 rettv->vval.v_string = NULL;
5660 return;
5661 }
5662 }
5663 else
5664 /* Default to v:register */
5665 strregname = get_vim_var_str(VV_REG);
5666
5667 regname = (strregname == NULL ? '"' : *strregname);
5668 if (regname == 0)
5669 regname = '"';
5670
5671 buf[0] = NUL;
5672 buf[1] = NUL;
5673 switch (get_reg_type(regname, &reglen))
5674 {
5675 case MLINE: buf[0] = 'V'; break;
5676 case MCHAR: buf[0] = 'v'; break;
5677 case MBLOCK:
5678 buf[0] = Ctrl_V;
5679 sprintf((char *)buf + 1, "%ld", reglen + 1);
5680 break;
5681 }
5682 rettv->v_type = VAR_STRING;
5683 rettv->vval.v_string = vim_strsave(buf);
5684}
5685
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005686/*
5687 * Returns information (variables, options, etc.) about a tab page
5688 * as a dictionary.
5689 */
5690 static dict_T *
5691get_tabpage_info(tabpage_T *tp, int tp_idx)
5692{
5693 win_T *wp;
5694 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005695 list_T *l;
5696
5697 dict = dict_alloc();
5698 if (dict == NULL)
5699 return NULL;
5700
Bram Moolenaare0be1672018-07-08 16:50:37 +02005701 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005702
5703 l = list_alloc();
5704 if (l != NULL)
5705 {
5706 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5707 wp; wp = wp->w_next)
5708 list_append_number(l, (varnumber_T)wp->w_id);
5709 dict_add_list(dict, "windows", l);
5710 }
5711
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005712 /* Make a reference to tabpage variables */
5713 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005714
5715 return dict;
5716}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005717
5718/*
5719 * "gettabinfo()" function
5720 */
5721 static void
5722f_gettabinfo(typval_T *argvars, typval_T *rettv)
5723{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005724 tabpage_T *tp, *tparg = NULL;
5725 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005726 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005727
5728 if (rettv_list_alloc(rettv) != OK)
5729 return;
5730
5731 if (argvars[0].v_type != VAR_UNKNOWN)
5732 {
5733 /* Information about one tab page */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005734 tparg = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005735 if (tparg == NULL)
5736 return;
5737 }
5738
5739 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005740 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005741 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005742 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005743 if (tparg != NULL && tp != tparg)
5744 continue;
5745 d = get_tabpage_info(tp, tpnr);
5746 if (d != NULL)
5747 list_append_dict(rettv->vval.v_list, d);
5748 if (tparg != NULL)
5749 return;
5750 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005751}
5752
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005753/*
5754 * "gettabvar()" function
5755 */
5756 static void
5757f_gettabvar(typval_T *argvars, typval_T *rettv)
5758{
5759 win_T *oldcurwin;
5760 tabpage_T *tp, *oldtabpage;
5761 dictitem_T *v;
5762 char_u *varname;
5763 int done = FALSE;
5764
5765 rettv->v_type = VAR_STRING;
5766 rettv->vval.v_string = NULL;
5767
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005768 varname = tv_get_string_chk(&argvars[1]);
5769 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005770 if (tp != NULL && varname != NULL)
5771 {
5772 /* Set tp to be our tabpage, temporarily. Also set the window to the
5773 * first window in the tabpage, otherwise the window is not valid. */
5774 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005775 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5776 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005777 {
5778 /* look up the variable */
5779 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5780 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5781 if (v != NULL)
5782 {
5783 copy_tv(&v->di_tv, rettv);
5784 done = TRUE;
5785 }
5786 }
5787
5788 /* restore previous notion of curwin */
5789 restore_win(oldcurwin, oldtabpage, TRUE);
5790 }
5791
5792 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5793 copy_tv(&argvars[2], rettv);
5794}
5795
5796/*
5797 * "gettabwinvar()" function
5798 */
5799 static void
5800f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5801{
5802 getwinvar(argvars, rettv, 1);
5803}
5804
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005805/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01005806 * "gettagstack()" function
5807 */
5808 static void
5809f_gettagstack(typval_T *argvars, typval_T *rettv)
5810{
5811 win_T *wp = curwin; // default is current window
5812
5813 if (rettv_dict_alloc(rettv) != OK)
5814 return;
5815
5816 if (argvars[0].v_type != VAR_UNKNOWN)
5817 {
5818 wp = find_win_by_nr_or_id(&argvars[0]);
5819 if (wp == NULL)
5820 return;
5821 }
5822
5823 get_tagstack(wp, rettv->vval.v_dict);
5824}
5825
5826/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005827 * Returns information about a window as a dictionary.
5828 */
5829 static dict_T *
5830get_win_info(win_T *wp, short tpnr, short winnr)
5831{
5832 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005833
5834 dict = dict_alloc();
5835 if (dict == NULL)
5836 return NULL;
5837
Bram Moolenaare0be1672018-07-08 16:50:37 +02005838 dict_add_number(dict, "tabnr", tpnr);
5839 dict_add_number(dict, "winnr", winnr);
5840 dict_add_number(dict, "winid", wp->w_id);
5841 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005842 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01005843 dict_add_number(dict, "topline", wp->w_topline);
5844 dict_add_number(dict, "botline", wp->w_botline - 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005845#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02005846 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005847#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005848 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005849 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005850 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005851
Bram Moolenaar69905d12017-08-13 18:14:47 +02005852#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02005853 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02005854#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005855#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02005856 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
5857 dict_add_number(dict, "loclist",
5858 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02005859#endif
5860
Bram Moolenaar30567352016-08-27 21:25:44 +02005861 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005862 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005863
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005864 return dict;
5865}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005866
5867/*
5868 * "getwininfo()" function
5869 */
5870 static void
5871f_getwininfo(typval_T *argvars, typval_T *rettv)
5872{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005873 tabpage_T *tp;
5874 win_T *wp = NULL, *wparg = NULL;
5875 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005876 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005877
5878 if (rettv_list_alloc(rettv) != OK)
5879 return;
5880
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005881 if (argvars[0].v_type != VAR_UNKNOWN)
5882 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01005883 wparg = win_id2wp(tv_get_number(&argvars[0]));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005884 if (wparg == NULL)
5885 return;
5886 }
5887
5888 /* Collect information about either all the windows across all the tab
5889 * pages or one particular window.
5890 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005891 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005892 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005893 tabnr++;
5894 winnr = 0;
5895 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005896 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005897 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005898 if (wparg != NULL && wp != wparg)
5899 continue;
5900 d = get_win_info(wp, tabnr, winnr);
5901 if (d != NULL)
5902 list_append_dict(rettv->vval.v_list, d);
5903 if (wparg != NULL)
5904 /* found information about a specific window */
5905 return;
5906 }
5907 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005908}
5909
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005910/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005911 * "win_findbuf()" function
5912 */
5913 static void
5914f_win_findbuf(typval_T *argvars, typval_T *rettv)
5915{
5916 if (rettv_list_alloc(rettv) != FAIL)
5917 win_findbuf(argvars, rettv->vval.v_list);
5918}
5919
5920/*
5921 * "win_getid()" function
5922 */
5923 static void
5924f_win_getid(typval_T *argvars, typval_T *rettv)
5925{
5926 rettv->vval.v_number = win_getid(argvars);
5927}
5928
5929/*
5930 * "win_gotoid()" function
5931 */
5932 static void
5933f_win_gotoid(typval_T *argvars, typval_T *rettv)
5934{
5935 rettv->vval.v_number = win_gotoid(argvars);
5936}
5937
5938/*
5939 * "win_id2tabwin()" function
5940 */
5941 static void
5942f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5943{
5944 if (rettv_list_alloc(rettv) != FAIL)
5945 win_id2tabwin(argvars, rettv->vval.v_list);
5946}
5947
5948/*
5949 * "win_id2win()" function
5950 */
5951 static void
5952f_win_id2win(typval_T *argvars, typval_T *rettv)
5953{
5954 rettv->vval.v_number = win_id2win(argvars);
5955}
5956
5957/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005958 * "win_screenpos()" function
5959 */
5960 static void
5961f_win_screenpos(typval_T *argvars, typval_T *rettv)
5962{
5963 win_T *wp;
5964
5965 if (rettv_list_alloc(rettv) == FAIL)
5966 return;
5967
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005968 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005969 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5970 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5971}
5972
5973/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005974 * "getwinpos({timeout})" function
5975 */
5976 static void
5977f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
5978{
5979 int x = -1;
5980 int y = -1;
5981
5982 if (rettv_list_alloc(rettv) == FAIL)
5983 return;
5984#ifdef FEAT_GUI
5985 if (gui.in_use)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01005986 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005987# if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5988 else
5989# endif
5990#endif
5991#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5992 {
5993 varnumber_T timeout = 100;
5994
5995 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005996 timeout = tv_get_number(&argvars[0]);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005997 term_get_winpos(&x, &y, timeout);
5998 }
5999#endif
6000 list_append_number(rettv->vval.v_list, (varnumber_T)x);
6001 list_append_number(rettv->vval.v_list, (varnumber_T)y);
6002}
6003
6004
6005/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006006 * "getwinposx()" function
6007 */
6008 static void
6009f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
6010{
6011 rettv->vval.v_number = -1;
6012#ifdef FEAT_GUI
6013 if (gui.in_use)
6014 {
6015 int x, y;
6016
6017 if (gui_mch_get_winpos(&x, &y) == OK)
6018 rettv->vval.v_number = x;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02006019 return;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006020 }
6021#endif
6022#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
6023 {
6024 int x, y;
6025
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006026 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006027 rettv->vval.v_number = x;
6028 }
6029#endif
6030}
6031
6032/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006033 * "getwinposy()" function
6034 */
6035 static void
6036f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
6037{
6038 rettv->vval.v_number = -1;
6039#ifdef FEAT_GUI
6040 if (gui.in_use)
6041 {
6042 int x, y;
6043
6044 if (gui_mch_get_winpos(&x, &y) == OK)
6045 rettv->vval.v_number = y;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02006046 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006047 }
6048#endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006049#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
6050 {
6051 int x, y;
6052
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006053 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006054 rettv->vval.v_number = y;
6055 }
6056#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006057}
6058
6059/*
6060 * "getwinvar()" function
6061 */
6062 static void
6063f_getwinvar(typval_T *argvars, typval_T *rettv)
6064{
6065 getwinvar(argvars, rettv, 0);
6066}
6067
6068/*
6069 * "glob()" function
6070 */
6071 static void
6072f_glob(typval_T *argvars, typval_T *rettv)
6073{
6074 int options = WILD_SILENT|WILD_USE_NL;
6075 expand_T xpc;
6076 int error = FALSE;
6077
6078 /* When the optional second argument is non-zero, don't remove matches
6079 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6080 rettv->v_type = VAR_STRING;
6081 if (argvars[1].v_type != VAR_UNKNOWN)
6082 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006083 if (tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006084 options |= WILD_KEEP_ALL;
6085 if (argvars[2].v_type != VAR_UNKNOWN)
6086 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006087 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006088 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006089 if (argvars[3].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006090 && tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006091 options |= WILD_ALLLINKS;
6092 }
6093 }
6094 if (!error)
6095 {
6096 ExpandInit(&xpc);
6097 xpc.xp_context = EXPAND_FILES;
6098 if (p_wic)
6099 options += WILD_ICASE;
6100 if (rettv->v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006101 rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006102 NULL, options, WILD_ALL);
6103 else if (rettv_list_alloc(rettv) != FAIL)
6104 {
6105 int i;
6106
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006107 ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006108 NULL, options, WILD_ALL_KEEP);
6109 for (i = 0; i < xpc.xp_numfiles; i++)
6110 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
6111
6112 ExpandCleanup(&xpc);
6113 }
6114 }
6115 else
6116 rettv->vval.v_string = NULL;
6117}
6118
6119/*
6120 * "globpath()" function
6121 */
6122 static void
6123f_globpath(typval_T *argvars, typval_T *rettv)
6124{
6125 int flags = 0;
6126 char_u buf1[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006127 char_u *file = tv_get_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006128 int error = FALSE;
6129 garray_T ga;
6130 int i;
6131
6132 /* When the optional second argument is non-zero, don't remove matches
6133 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6134 rettv->v_type = VAR_STRING;
6135 if (argvars[2].v_type != VAR_UNKNOWN)
6136 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006137 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006138 flags |= WILD_KEEP_ALL;
6139 if (argvars[3].v_type != VAR_UNKNOWN)
6140 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006141 if (tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006142 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006143 if (argvars[4].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006144 && tv_get_number_chk(&argvars[4], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006145 flags |= WILD_ALLLINKS;
6146 }
6147 }
6148 if (file != NULL && !error)
6149 {
6150 ga_init2(&ga, (int)sizeof(char_u *), 10);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006151 globpath(tv_get_string(&argvars[0]), file, &ga, flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006152 if (rettv->v_type == VAR_STRING)
6153 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
6154 else if (rettv_list_alloc(rettv) != FAIL)
6155 for (i = 0; i < ga.ga_len; ++i)
6156 list_append_string(rettv->vval.v_list,
6157 ((char_u **)(ga.ga_data))[i], -1);
6158 ga_clear_strings(&ga);
6159 }
6160 else
6161 rettv->vval.v_string = NULL;
6162}
6163
6164/*
6165 * "glob2regpat()" function
6166 */
6167 static void
6168f_glob2regpat(typval_T *argvars, typval_T *rettv)
6169{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006170 char_u *pat = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006171
6172 rettv->v_type = VAR_STRING;
6173 rettv->vval.v_string = (pat == NULL)
6174 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
6175}
6176
6177/* for VIM_VERSION_ defines */
6178#include "version.h"
6179
6180/*
6181 * "has()" function
6182 */
6183 static void
6184f_has(typval_T *argvars, typval_T *rettv)
6185{
6186 int i;
6187 char_u *name;
6188 int n = FALSE;
6189 static char *(has_list[]) =
6190 {
6191#ifdef AMIGA
6192 "amiga",
6193# ifdef FEAT_ARP
6194 "arp",
6195# endif
6196#endif
6197#ifdef __BEOS__
6198 "beos",
6199#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006200#if defined(BSD) && !defined(MACOS_X)
6201 "bsd",
6202#endif
6203#ifdef hpux
6204 "hpux",
6205#endif
6206#ifdef __linux__
6207 "linux",
6208#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02006209#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01006210 "mac", /* Mac OS X (and, once, Mac OS Classic) */
6211 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02006212# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01006213 "macunix", /* Mac OS X, with the darwin feature */
6214 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02006215# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006216#endif
6217#ifdef __QNX__
6218 "qnx",
6219#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006220#ifdef SUN_SYSTEM
6221 "sun",
6222#else
6223 "moon",
6224#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006225#ifdef UNIX
6226 "unix",
6227#endif
6228#ifdef VMS
6229 "vms",
6230#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006231#ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006232 "win32",
6233#endif
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006234#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006235 "win32unix",
6236#endif
Bram Moolenaar44b443c2019-02-18 22:14:18 +01006237#ifdef _WIN64
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006238 "win64",
6239#endif
6240#ifdef EBCDIC
6241 "ebcdic",
6242#endif
6243#ifndef CASE_INSENSITIVE_FILENAME
6244 "fname_case",
6245#endif
6246#ifdef HAVE_ACL
6247 "acl",
6248#endif
6249#ifdef FEAT_ARABIC
6250 "arabic",
6251#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006252 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006253#ifdef FEAT_AUTOCHDIR
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006254 "autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006255#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006256#ifdef FEAT_AUTOSERVERNAME
6257 "autoservername",
6258#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006259#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006260 "balloon_eval",
Bram Moolenaar4f974752019-02-17 17:44:42 +01006261# ifndef FEAT_GUI_MSWIN /* other GUIs always have multiline balloons */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006262 "balloon_multiline",
6263# endif
6264#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006265#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006266 "balloon_eval_term",
6267#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006268#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6269 "builtin_terms",
6270# ifdef ALL_BUILTIN_TCAPS
6271 "all_builtin_terms",
6272# endif
6273#endif
6274#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01006275 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006276 || defined(FEAT_GUI_MOTIF))
6277 "browsefilter",
6278#endif
6279#ifdef FEAT_BYTEOFF
6280 "byte_offset",
6281#endif
6282#ifdef FEAT_JOB_CHANNEL
6283 "channel",
6284#endif
6285#ifdef FEAT_CINDENT
6286 "cindent",
6287#endif
6288#ifdef FEAT_CLIENTSERVER
6289 "clientserver",
6290#endif
6291#ifdef FEAT_CLIPBOARD
6292 "clipboard",
6293#endif
6294#ifdef FEAT_CMDL_COMPL
6295 "cmdline_compl",
6296#endif
6297#ifdef FEAT_CMDHIST
6298 "cmdline_hist",
6299#endif
6300#ifdef FEAT_COMMENTS
6301 "comments",
6302#endif
6303#ifdef FEAT_CONCEAL
6304 "conceal",
6305#endif
6306#ifdef FEAT_CRYPT
6307 "cryptv",
6308 "crypt-blowfish",
6309 "crypt-blowfish2",
6310#endif
6311#ifdef FEAT_CSCOPE
6312 "cscope",
6313#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006314 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006315#ifdef CURSOR_SHAPE
6316 "cursorshape",
6317#endif
6318#ifdef DEBUG
6319 "debug",
6320#endif
6321#ifdef FEAT_CON_DIALOG
6322 "dialog_con",
6323#endif
6324#ifdef FEAT_GUI_DIALOG
6325 "dialog_gui",
6326#endif
6327#ifdef FEAT_DIFF
6328 "diff",
6329#endif
6330#ifdef FEAT_DIGRAPHS
6331 "digraphs",
6332#endif
6333#ifdef FEAT_DIRECTX
6334 "directx",
6335#endif
6336#ifdef FEAT_DND
6337 "dnd",
6338#endif
6339#ifdef FEAT_EMACS_TAGS
6340 "emacs_tags",
6341#endif
6342 "eval", /* always present, of course! */
6343 "ex_extra", /* graduated feature */
6344#ifdef FEAT_SEARCH_EXTRA
6345 "extra_search",
6346#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006347#ifdef FEAT_SEARCHPATH
6348 "file_in_path",
6349#endif
6350#ifdef FEAT_FILTERPIPE
6351 "filterpipe",
6352#endif
6353#ifdef FEAT_FIND_ID
6354 "find_in_path",
6355#endif
6356#ifdef FEAT_FLOAT
6357 "float",
6358#endif
6359#ifdef FEAT_FOLDING
6360 "folding",
6361#endif
6362#ifdef FEAT_FOOTER
6363 "footer",
6364#endif
6365#if !defined(USE_SYSTEM) && defined(UNIX)
6366 "fork",
6367#endif
6368#ifdef FEAT_GETTEXT
6369 "gettext",
6370#endif
6371#ifdef FEAT_GUI
6372 "gui",
6373#endif
6374#ifdef FEAT_GUI_ATHENA
6375# ifdef FEAT_GUI_NEXTAW
6376 "gui_neXtaw",
6377# else
6378 "gui_athena",
6379# endif
6380#endif
6381#ifdef FEAT_GUI_GTK
6382 "gui_gtk",
6383# ifdef USE_GTK3
6384 "gui_gtk3",
6385# else
6386 "gui_gtk2",
6387# endif
6388#endif
6389#ifdef FEAT_GUI_GNOME
6390 "gui_gnome",
6391#endif
6392#ifdef FEAT_GUI_MAC
6393 "gui_mac",
6394#endif
6395#ifdef FEAT_GUI_MOTIF
6396 "gui_motif",
6397#endif
6398#ifdef FEAT_GUI_PHOTON
6399 "gui_photon",
6400#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006401#ifdef FEAT_GUI_MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006402 "gui_win32",
6403#endif
6404#ifdef FEAT_HANGULIN
6405 "hangul_input",
6406#endif
6407#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6408 "iconv",
6409#endif
6410#ifdef FEAT_INS_EXPAND
6411 "insert_expand",
6412#endif
6413#ifdef FEAT_JOB_CHANNEL
6414 "job",
6415#endif
6416#ifdef FEAT_JUMPLIST
6417 "jumplist",
6418#endif
6419#ifdef FEAT_KEYMAP
6420 "keymap",
6421#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006422 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006423#ifdef FEAT_LANGMAP
6424 "langmap",
6425#endif
6426#ifdef FEAT_LIBCALL
6427 "libcall",
6428#endif
6429#ifdef FEAT_LINEBREAK
6430 "linebreak",
6431#endif
6432#ifdef FEAT_LISP
6433 "lispindent",
6434#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006435 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006436#ifdef FEAT_LOCALMAP
6437 "localmap",
6438#endif
6439#ifdef FEAT_LUA
6440# ifndef DYNAMIC_LUA
6441 "lua",
6442# endif
6443#endif
6444#ifdef FEAT_MENU
6445 "menu",
6446#endif
6447#ifdef FEAT_SESSION
6448 "mksession",
6449#endif
6450#ifdef FEAT_MODIFY_FNAME
6451 "modify_fname",
6452#endif
6453#ifdef FEAT_MOUSE
6454 "mouse",
6455#endif
6456#ifdef FEAT_MOUSESHAPE
6457 "mouseshape",
6458#endif
6459#if defined(UNIX) || defined(VMS)
6460# ifdef FEAT_MOUSE_DEC
6461 "mouse_dec",
6462# endif
6463# ifdef FEAT_MOUSE_GPM
6464 "mouse_gpm",
6465# endif
6466# ifdef FEAT_MOUSE_JSB
6467 "mouse_jsbterm",
6468# endif
6469# ifdef FEAT_MOUSE_NET
6470 "mouse_netterm",
6471# endif
6472# ifdef FEAT_MOUSE_PTERM
6473 "mouse_pterm",
6474# endif
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01006475# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006476 "mouse_sgr",
6477# endif
6478# ifdef FEAT_SYSMOUSE
6479 "mouse_sysmouse",
6480# endif
6481# ifdef FEAT_MOUSE_URXVT
6482 "mouse_urxvt",
6483# endif
6484# ifdef FEAT_MOUSE_XTERM
6485 "mouse_xterm",
6486# endif
6487#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006488 "multi_byte",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006489#ifdef FEAT_MBYTE_IME
6490 "multi_byte_ime",
6491#endif
6492#ifdef FEAT_MULTI_LANG
6493 "multi_lang",
6494#endif
6495#ifdef FEAT_MZSCHEME
6496#ifndef DYNAMIC_MZSCHEME
6497 "mzscheme",
6498#endif
6499#endif
6500#ifdef FEAT_NUM64
6501 "num64",
6502#endif
6503#ifdef FEAT_OLE
6504 "ole",
6505#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006506#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006507 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006508#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006509#ifdef FEAT_PATH_EXTRA
6510 "path_extra",
6511#endif
6512#ifdef FEAT_PERL
6513#ifndef DYNAMIC_PERL
6514 "perl",
6515#endif
6516#endif
6517#ifdef FEAT_PERSISTENT_UNDO
6518 "persistent_undo",
6519#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006520#if defined(FEAT_PYTHON)
6521 "python_compiled",
6522# if defined(DYNAMIC_PYTHON)
6523 "python_dynamic",
6524# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006525 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006526 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006527# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006528#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006529#if defined(FEAT_PYTHON3)
6530 "python3_compiled",
6531# if defined(DYNAMIC_PYTHON3)
6532 "python3_dynamic",
6533# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006534 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006535 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006536# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006537#endif
6538#ifdef FEAT_POSTSCRIPT
6539 "postscript",
6540#endif
6541#ifdef FEAT_PRINTER
6542 "printer",
6543#endif
6544#ifdef FEAT_PROFILE
6545 "profile",
6546#endif
6547#ifdef FEAT_RELTIME
6548 "reltime",
6549#endif
6550#ifdef FEAT_QUICKFIX
6551 "quickfix",
6552#endif
6553#ifdef FEAT_RIGHTLEFT
6554 "rightleft",
6555#endif
6556#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6557 "ruby",
6558#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006559 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006560#ifdef FEAT_CMDL_INFO
6561 "showcmd",
6562 "cmdline_info",
6563#endif
6564#ifdef FEAT_SIGNS
6565 "signs",
6566#endif
6567#ifdef FEAT_SMARTINDENT
6568 "smartindent",
6569#endif
6570#ifdef STARTUPTIME
6571 "startuptime",
6572#endif
6573#ifdef FEAT_STL_OPT
6574 "statusline",
6575#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006576#ifdef FEAT_NETBEANS_INTG
6577 "netbeans_intg",
6578#endif
6579#ifdef FEAT_SPELL
6580 "spell",
6581#endif
6582#ifdef FEAT_SYN_HL
6583 "syntax",
6584#endif
6585#if defined(USE_SYSTEM) || !defined(UNIX)
6586 "system",
6587#endif
6588#ifdef FEAT_TAG_BINS
6589 "tag_binary",
6590#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006591#ifdef FEAT_TCL
6592# ifndef DYNAMIC_TCL
6593 "tcl",
6594# endif
6595#endif
6596#ifdef FEAT_TERMGUICOLORS
6597 "termguicolors",
6598#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006599#if defined(FEAT_TERMINAL) && !defined(MSWIN)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006600 "terminal",
6601#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006602#ifdef TERMINFO
6603 "terminfo",
6604#endif
6605#ifdef FEAT_TERMRESPONSE
6606 "termresponse",
6607#endif
6608#ifdef FEAT_TEXTOBJ
6609 "textobjects",
6610#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006611#ifdef FEAT_TEXT_PROP
6612 "textprop",
6613#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006614#ifdef HAVE_TGETENT
6615 "tgetent",
6616#endif
6617#ifdef FEAT_TIMERS
6618 "timers",
6619#endif
6620#ifdef FEAT_TITLE
6621 "title",
6622#endif
6623#ifdef FEAT_TOOLBAR
6624 "toolbar",
6625#endif
6626#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6627 "unnamedplus",
6628#endif
6629#ifdef FEAT_USR_CMDS
6630 "user-commands", /* was accidentally included in 5.4 */
6631 "user_commands",
6632#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006633#ifdef FEAT_VARTABS
6634 "vartabs",
6635#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006636 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006637#ifdef FEAT_VIMINFO
6638 "viminfo",
6639#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006640 "vimscript-1",
6641 "vimscript-2",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006642 "virtualedit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006643 "visual",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006644 "visualextra",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006645 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006646#ifdef FEAT_VTP
6647 "vtp",
6648#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006649#ifdef FEAT_WILDIGN
6650 "wildignore",
6651#endif
6652#ifdef FEAT_WILDMENU
6653 "wildmenu",
6654#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006655 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006656#ifdef FEAT_WAK
6657 "winaltkeys",
6658#endif
6659#ifdef FEAT_WRITEBACKUP
6660 "writebackup",
6661#endif
6662#ifdef FEAT_XIM
6663 "xim",
6664#endif
6665#ifdef FEAT_XFONTSET
6666 "xfontset",
6667#endif
6668#ifdef FEAT_XPM_W32
6669 "xpm",
6670 "xpm_w32", /* for backward compatibility */
6671#else
6672# if defined(HAVE_XPM)
6673 "xpm",
6674# endif
6675#endif
6676#ifdef USE_XSMP
6677 "xsmp",
6678#endif
6679#ifdef USE_XSMP_INTERACT
6680 "xsmp_interact",
6681#endif
6682#ifdef FEAT_XCLIPBOARD
6683 "xterm_clipboard",
6684#endif
6685#ifdef FEAT_XTERM_SAVE
6686 "xterm_save",
6687#endif
6688#if defined(UNIX) && defined(FEAT_X11)
6689 "X11",
6690#endif
6691 NULL
6692 };
6693
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006694 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006695 for (i = 0; has_list[i] != NULL; ++i)
6696 if (STRICMP(name, has_list[i]) == 0)
6697 {
6698 n = TRUE;
6699 break;
6700 }
6701
6702 if (n == FALSE)
6703 {
6704 if (STRNICMP(name, "patch", 5) == 0)
6705 {
6706 if (name[5] == '-'
6707 && STRLEN(name) >= 11
6708 && vim_isdigit(name[6])
6709 && vim_isdigit(name[8])
6710 && vim_isdigit(name[10]))
6711 {
6712 int major = atoi((char *)name + 6);
6713 int minor = atoi((char *)name + 8);
6714
6715 /* Expect "patch-9.9.01234". */
6716 n = (major < VIM_VERSION_MAJOR
6717 || (major == VIM_VERSION_MAJOR
6718 && (minor < VIM_VERSION_MINOR
6719 || (minor == VIM_VERSION_MINOR
6720 && has_patch(atoi((char *)name + 10))))));
6721 }
6722 else
6723 n = has_patch(atoi((char *)name + 5));
6724 }
6725 else if (STRICMP(name, "vim_starting") == 0)
6726 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006727 else if (STRICMP(name, "ttyin") == 0)
6728 n = mch_input_isatty();
6729 else if (STRICMP(name, "ttyout") == 0)
6730 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006731 else if (STRICMP(name, "multi_byte_encoding") == 0)
6732 n = has_mbyte;
Bram Moolenaar4f974752019-02-17 17:44:42 +01006733#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006734 else if (STRICMP(name, "balloon_multiline") == 0)
6735 n = multiline_balloon_available();
6736#endif
6737#ifdef DYNAMIC_TCL
6738 else if (STRICMP(name, "tcl") == 0)
6739 n = tcl_enabled(FALSE);
6740#endif
6741#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6742 else if (STRICMP(name, "iconv") == 0)
6743 n = iconv_enabled(FALSE);
6744#endif
6745#ifdef DYNAMIC_LUA
6746 else if (STRICMP(name, "lua") == 0)
6747 n = lua_enabled(FALSE);
6748#endif
6749#ifdef DYNAMIC_MZSCHEME
6750 else if (STRICMP(name, "mzscheme") == 0)
6751 n = mzscheme_enabled(FALSE);
6752#endif
6753#ifdef DYNAMIC_RUBY
6754 else if (STRICMP(name, "ruby") == 0)
6755 n = ruby_enabled(FALSE);
6756#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006757#ifdef DYNAMIC_PYTHON
6758 else if (STRICMP(name, "python") == 0)
6759 n = python_enabled(FALSE);
6760#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006761#ifdef DYNAMIC_PYTHON3
6762 else if (STRICMP(name, "python3") == 0)
6763 n = python3_enabled(FALSE);
6764#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006765#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6766 else if (STRICMP(name, "pythonx") == 0)
6767 {
6768# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6769 if (p_pyx == 0)
6770 n = python3_enabled(FALSE) || python_enabled(FALSE);
6771 else if (p_pyx == 3)
6772 n = python3_enabled(FALSE);
6773 else if (p_pyx == 2)
6774 n = python_enabled(FALSE);
6775# elif defined(DYNAMIC_PYTHON)
6776 n = python_enabled(FALSE);
6777# elif defined(DYNAMIC_PYTHON3)
6778 n = python3_enabled(FALSE);
6779# endif
6780 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006781#endif
6782#ifdef DYNAMIC_PERL
6783 else if (STRICMP(name, "perl") == 0)
6784 n = perl_enabled(FALSE);
6785#endif
6786#ifdef FEAT_GUI
6787 else if (STRICMP(name, "gui_running") == 0)
6788 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006789# ifdef FEAT_BROWSE
6790 else if (STRICMP(name, "browse") == 0)
6791 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6792# endif
6793#endif
6794#ifdef FEAT_SYN_HL
6795 else if (STRICMP(name, "syntax_items") == 0)
6796 n = syntax_present(curwin);
6797#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006798#ifdef FEAT_VTP
6799 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02006800 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006801#endif
6802#ifdef FEAT_NETBEANS_INTG
6803 else if (STRICMP(name, "netbeans_enabled") == 0)
6804 n = netbeans_active();
6805#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006806#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02006807 else if (STRICMP(name, "terminal") == 0)
6808 n = terminal_enabled();
6809#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006810#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006811 else if (STRICMP(name, "conpty") == 0)
6812 n = use_conpty();
6813#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006814 }
6815
6816 rettv->vval.v_number = n;
6817}
6818
6819/*
6820 * "has_key()" function
6821 */
6822 static void
6823f_has_key(typval_T *argvars, typval_T *rettv)
6824{
6825 if (argvars[0].v_type != VAR_DICT)
6826 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006827 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006828 return;
6829 }
6830 if (argvars[0].vval.v_dict == NULL)
6831 return;
6832
6833 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006834 tv_get_string(&argvars[1]), -1) != NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006835}
6836
6837/*
6838 * "haslocaldir()" function
6839 */
6840 static void
6841f_haslocaldir(typval_T *argvars, typval_T *rettv)
6842{
6843 win_T *wp = NULL;
6844
6845 wp = find_tabwin(&argvars[0], &argvars[1]);
6846 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6847}
6848
6849/*
6850 * "hasmapto()" function
6851 */
6852 static void
6853f_hasmapto(typval_T *argvars, typval_T *rettv)
6854{
6855 char_u *name;
6856 char_u *mode;
6857 char_u buf[NUMBUFLEN];
6858 int abbr = FALSE;
6859
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006860 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006861 if (argvars[1].v_type == VAR_UNKNOWN)
6862 mode = (char_u *)"nvo";
6863 else
6864 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006865 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006866 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006867 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006868 }
6869
6870 if (map_to_exists(name, mode, abbr))
6871 rettv->vval.v_number = TRUE;
6872 else
6873 rettv->vval.v_number = FALSE;
6874}
6875
6876/*
6877 * "histadd()" function
6878 */
6879 static void
6880f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6881{
6882#ifdef FEAT_CMDHIST
6883 int histype;
6884 char_u *str;
6885 char_u buf[NUMBUFLEN];
6886#endif
6887
6888 rettv->vval.v_number = FALSE;
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006889 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006890 return;
6891#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006892 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006893 histype = str != NULL ? get_histtype(str) : -1;
6894 if (histype >= 0)
6895 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006896 str = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006897 if (*str != NUL)
6898 {
6899 init_history();
6900 add_to_history(histype, str, FALSE, NUL);
6901 rettv->vval.v_number = TRUE;
6902 return;
6903 }
6904 }
6905#endif
6906}
6907
6908/*
6909 * "histdel()" function
6910 */
6911 static void
6912f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6913{
6914#ifdef FEAT_CMDHIST
6915 int n;
6916 char_u buf[NUMBUFLEN];
6917 char_u *str;
6918
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006919 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006920 if (str == NULL)
6921 n = 0;
6922 else if (argvars[1].v_type == VAR_UNKNOWN)
6923 /* only one argument: clear entire history */
6924 n = clr_history(get_histtype(str));
6925 else if (argvars[1].v_type == VAR_NUMBER)
6926 /* index given: remove that entry */
6927 n = del_history_idx(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006928 (int)tv_get_number(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006929 else
6930 /* string given: remove all matching entries */
6931 n = del_history_entry(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006932 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006933 rettv->vval.v_number = n;
6934#endif
6935}
6936
6937/*
6938 * "histget()" function
6939 */
6940 static void
6941f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6942{
6943#ifdef FEAT_CMDHIST
6944 int type;
6945 int idx;
6946 char_u *str;
6947
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006948 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006949 if (str == NULL)
6950 rettv->vval.v_string = NULL;
6951 else
6952 {
6953 type = get_histtype(str);
6954 if (argvars[1].v_type == VAR_UNKNOWN)
6955 idx = get_history_idx(type);
6956 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006957 idx = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006958 /* -1 on type error */
6959 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6960 }
6961#else
6962 rettv->vval.v_string = NULL;
6963#endif
6964 rettv->v_type = VAR_STRING;
6965}
6966
6967/*
6968 * "histnr()" function
6969 */
6970 static void
6971f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6972{
6973 int i;
6974
6975#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006976 char_u *history = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006977
6978 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6979 if (i >= HIST_CMD && i < HIST_COUNT)
6980 i = get_history_idx(i);
6981 else
6982#endif
6983 i = -1;
6984 rettv->vval.v_number = i;
6985}
6986
6987/*
6988 * "highlightID(name)" function
6989 */
6990 static void
6991f_hlID(typval_T *argvars, typval_T *rettv)
6992{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006993 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006994}
6995
6996/*
6997 * "highlight_exists()" function
6998 */
6999 static void
7000f_hlexists(typval_T *argvars, typval_T *rettv)
7001{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007002 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007003}
7004
7005/*
7006 * "hostname()" function
7007 */
7008 static void
7009f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
7010{
7011 char_u hostname[256];
7012
7013 mch_get_host_name(hostname, 256);
7014 rettv->v_type = VAR_STRING;
7015 rettv->vval.v_string = vim_strsave(hostname);
7016}
7017
7018/*
7019 * iconv() function
7020 */
7021 static void
7022f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
7023{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007024 char_u buf1[NUMBUFLEN];
7025 char_u buf2[NUMBUFLEN];
7026 char_u *from, *to, *str;
7027 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007028
7029 rettv->v_type = VAR_STRING;
7030 rettv->vval.v_string = NULL;
7031
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007032 str = tv_get_string(&argvars[0]);
7033 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
7034 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007035 vimconv.vc_type = CONV_NONE;
7036 convert_setup(&vimconv, from, to);
7037
7038 /* If the encodings are equal, no conversion needed. */
7039 if (vimconv.vc_type == CONV_NONE)
7040 rettv->vval.v_string = vim_strsave(str);
7041 else
7042 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
7043
7044 convert_setup(&vimconv, NULL, NULL);
7045 vim_free(from);
7046 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007047}
7048
7049/*
7050 * "indent()" function
7051 */
7052 static void
7053f_indent(typval_T *argvars, typval_T *rettv)
7054{
7055 linenr_T lnum;
7056
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007057 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007058 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7059 rettv->vval.v_number = get_indent_lnum(lnum);
7060 else
7061 rettv->vval.v_number = -1;
7062}
7063
7064/*
7065 * "index()" function
7066 */
7067 static void
7068f_index(typval_T *argvars, typval_T *rettv)
7069{
7070 list_T *l;
7071 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007072 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007073 long idx = 0;
7074 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007075 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007076
7077 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007078 if (argvars[0].v_type == VAR_BLOB)
7079 {
7080 typval_T tv;
7081 int start = 0;
7082
7083 if (argvars[2].v_type != VAR_UNKNOWN)
7084 {
7085 start = tv_get_number_chk(&argvars[2], &error);
7086 if (error)
7087 return;
7088 }
7089 b = argvars[0].vval.v_blob;
7090 if (b == NULL)
7091 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01007092 if (start < 0)
7093 {
7094 start = blob_len(b) + start;
7095 if (start < 0)
7096 start = 0;
7097 }
7098
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007099 for (idx = start; idx < blob_len(b); ++idx)
7100 {
7101 tv.v_type = VAR_NUMBER;
7102 tv.vval.v_number = blob_get(b, idx);
7103 if (tv_equal(&tv, &argvars[1], ic, FALSE))
7104 {
7105 rettv->vval.v_number = idx;
7106 return;
7107 }
7108 }
7109 return;
7110 }
7111 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007112 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007113 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007114 return;
7115 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007116
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007117 l = argvars[0].vval.v_list;
7118 if (l != NULL)
7119 {
7120 item = l->lv_first;
7121 if (argvars[2].v_type != VAR_UNKNOWN)
7122 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007123 /* Start at specified item. Use the cached index that list_find()
7124 * sets, so that a negative number also works. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007125 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007126 idx = l->lv_idx;
7127 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007128 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007129 if (error)
7130 item = NULL;
7131 }
7132
7133 for ( ; item != NULL; item = item->li_next, ++idx)
7134 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
7135 {
7136 rettv->vval.v_number = idx;
7137 break;
7138 }
7139 }
7140}
7141
7142static int inputsecret_flag = 0;
7143
7144/*
7145 * "input()" function
7146 * Also handles inputsecret() when inputsecret is set.
7147 */
7148 static void
7149f_input(typval_T *argvars, typval_T *rettv)
7150{
7151 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
7152}
7153
7154/*
7155 * "inputdialog()" function
7156 */
7157 static void
7158f_inputdialog(typval_T *argvars, typval_T *rettv)
7159{
7160#if defined(FEAT_GUI_TEXTDIALOG)
7161 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
7162 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
7163 {
7164 char_u *message;
7165 char_u buf[NUMBUFLEN];
7166 char_u *defstr = (char_u *)"";
7167
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007168 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007169 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007170 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007171 vim_strncpy(IObuff, defstr, IOSIZE - 1);
7172 else
7173 IObuff[0] = NUL;
7174 if (message != NULL && defstr != NULL
7175 && do_dialog(VIM_QUESTION, NULL, message,
7176 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
7177 rettv->vval.v_string = vim_strsave(IObuff);
7178 else
7179 {
7180 if (message != NULL && defstr != NULL
7181 && argvars[1].v_type != VAR_UNKNOWN
7182 && argvars[2].v_type != VAR_UNKNOWN)
7183 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007184 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007185 else
7186 rettv->vval.v_string = NULL;
7187 }
7188 rettv->v_type = VAR_STRING;
7189 }
7190 else
7191#endif
7192 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
7193}
7194
7195/*
7196 * "inputlist()" function
7197 */
7198 static void
7199f_inputlist(typval_T *argvars, typval_T *rettv)
7200{
7201 listitem_T *li;
7202 int selected;
7203 int mouse_used;
7204
7205#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007206 /* While starting up, there is no place to enter text. When running tests
7207 * with --not-a-term we assume feedkeys() will be used. */
7208 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007209 return;
7210#endif
7211 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
7212 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007213 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007214 return;
7215 }
7216
7217 msg_start();
7218 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
7219 lines_left = Rows; /* avoid more prompt */
7220 msg_scroll = TRUE;
7221 msg_clr_eos();
7222
7223 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
7224 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007225 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007226 msg_putchar('\n');
7227 }
7228
7229 /* Ask for choice. */
7230 selected = prompt_for_number(&mouse_used);
7231 if (mouse_used)
7232 selected -= lines_left;
7233
7234 rettv->vval.v_number = selected;
7235}
7236
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007237static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7238
7239/*
7240 * "inputrestore()" function
7241 */
7242 static void
7243f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7244{
7245 if (ga_userinput.ga_len > 0)
7246 {
7247 --ga_userinput.ga_len;
7248 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7249 + ga_userinput.ga_len);
7250 /* default return is zero == OK */
7251 }
7252 else if (p_verbose > 1)
7253 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007254 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007255 rettv->vval.v_number = 1; /* Failed */
7256 }
7257}
7258
7259/*
7260 * "inputsave()" function
7261 */
7262 static void
7263f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7264{
7265 /* Add an entry to the stack of typeahead storage. */
7266 if (ga_grow(&ga_userinput, 1) == OK)
7267 {
7268 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7269 + ga_userinput.ga_len);
7270 ++ga_userinput.ga_len;
7271 /* default return is zero == OK */
7272 }
7273 else
7274 rettv->vval.v_number = 1; /* Failed */
7275}
7276
7277/*
7278 * "inputsecret()" function
7279 */
7280 static void
7281f_inputsecret(typval_T *argvars, typval_T *rettv)
7282{
7283 ++cmdline_star;
7284 ++inputsecret_flag;
7285 f_input(argvars, rettv);
7286 --cmdline_star;
7287 --inputsecret_flag;
7288}
7289
7290/*
7291 * "insert()" function
7292 */
7293 static void
7294f_insert(typval_T *argvars, typval_T *rettv)
7295{
7296 long before = 0;
7297 listitem_T *item;
7298 list_T *l;
7299 int error = FALSE;
7300
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007301 if (argvars[0].v_type == VAR_BLOB)
7302 {
7303 int val, len;
7304 char_u *p;
7305
7306 len = blob_len(argvars[0].vval.v_blob);
7307 if (argvars[2].v_type != VAR_UNKNOWN)
7308 {
7309 before = (long)tv_get_number_chk(&argvars[2], &error);
7310 if (error)
7311 return; // type error; errmsg already given
7312 if (before < 0 || before > len)
7313 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007314 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007315 return;
7316 }
7317 }
7318 val = tv_get_number_chk(&argvars[1], &error);
7319 if (error)
7320 return;
7321 if (val < 0 || val > 255)
7322 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007323 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007324 return;
7325 }
7326
7327 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
7328 return;
7329 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
7330 mch_memmove(p + before + 1, p + before, (size_t)len - before);
7331 *(p + before) = val;
7332 ++argvars[0].vval.v_blob->bv_ga.ga_len;
7333
7334 copy_tv(&argvars[0], rettv);
7335 }
7336 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007337 semsg(_(e_listblobarg), "insert()");
Bram Moolenaar05c00c02019-02-11 22:00:11 +01007338 else if ((l = argvars[0].vval.v_list) != NULL
7339 && !var_check_lock(l->lv_lock,
7340 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007341 {
7342 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007343 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007344 if (error)
7345 return; /* type error; errmsg already given */
7346
7347 if (before == l->lv_len)
7348 item = NULL;
7349 else
7350 {
7351 item = list_find(l, before);
7352 if (item == NULL)
7353 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007354 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007355 l = NULL;
7356 }
7357 }
7358 if (l != NULL)
7359 {
7360 list_insert_tv(l, &argvars[1], item);
7361 copy_tv(&argvars[0], rettv);
7362 }
7363 }
7364}
7365
7366/*
7367 * "invert(expr)" function
7368 */
7369 static void
7370f_invert(typval_T *argvars, typval_T *rettv)
7371{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007372 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007373}
7374
7375/*
7376 * "isdirectory()" function
7377 */
7378 static void
7379f_isdirectory(typval_T *argvars, typval_T *rettv)
7380{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007381 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007382}
7383
7384/*
7385 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7386 * or it refers to a List or Dictionary that is locked.
7387 */
7388 static int
7389tv_islocked(typval_T *tv)
7390{
7391 return (tv->v_lock & VAR_LOCKED)
7392 || (tv->v_type == VAR_LIST
7393 && tv->vval.v_list != NULL
7394 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7395 || (tv->v_type == VAR_DICT
7396 && tv->vval.v_dict != NULL
7397 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7398}
7399
7400/*
7401 * "islocked()" function
7402 */
7403 static void
7404f_islocked(typval_T *argvars, typval_T *rettv)
7405{
7406 lval_T lv;
7407 char_u *end;
7408 dictitem_T *di;
7409
7410 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007411 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007412 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007413 if (end != NULL && lv.ll_name != NULL)
7414 {
7415 if (*end != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007416 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007417 else
7418 {
7419 if (lv.ll_tv == NULL)
7420 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007421 di = find_var(lv.ll_name, NULL, TRUE);
7422 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007423 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007424 /* Consider a variable locked when:
7425 * 1. the variable itself is locked
7426 * 2. the value of the variable is locked.
7427 * 3. the List or Dict value is locked.
7428 */
7429 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7430 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007431 }
7432 }
7433 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007434 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007435 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007436 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007437 else if (lv.ll_list != NULL)
7438 /* List item. */
7439 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7440 else
7441 /* Dictionary item. */
7442 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7443 }
7444 }
7445
7446 clear_lval(&lv);
7447}
7448
7449#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7450/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02007451 * "isinf()" function
7452 */
7453 static void
7454f_isinf(typval_T *argvars, typval_T *rettv)
7455{
7456 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
7457 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
7458}
7459
7460/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007461 * "isnan()" function
7462 */
7463 static void
7464f_isnan(typval_T *argvars, typval_T *rettv)
7465{
7466 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7467 && isnan(argvars[0].vval.v_float);
7468}
7469#endif
7470
7471/*
7472 * "items(dict)" function
7473 */
7474 static void
7475f_items(typval_T *argvars, typval_T *rettv)
7476{
7477 dict_list(argvars, rettv, 2);
7478}
7479
7480#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
7481/*
7482 * Get the job from the argument.
7483 * Returns NULL if the job is invalid.
7484 */
7485 static job_T *
7486get_job_arg(typval_T *tv)
7487{
7488 job_T *job;
7489
7490 if (tv->v_type != VAR_JOB)
7491 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007492 semsg(_(e_invarg2), tv_get_string(tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007493 return NULL;
7494 }
7495 job = tv->vval.v_job;
7496
7497 if (job == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007498 emsg(_("E916: not a valid job"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007499 return job;
7500}
7501
7502/*
7503 * "job_getchannel()" function
7504 */
7505 static void
7506f_job_getchannel(typval_T *argvars, typval_T *rettv)
7507{
7508 job_T *job = get_job_arg(&argvars[0]);
7509
7510 if (job != NULL)
7511 {
7512 rettv->v_type = VAR_CHANNEL;
7513 rettv->vval.v_channel = job->jv_channel;
7514 if (job->jv_channel != NULL)
7515 ++job->jv_channel->ch_refcount;
7516 }
7517}
7518
7519/*
7520 * "job_info()" function
7521 */
7522 static void
7523f_job_info(typval_T *argvars, typval_T *rettv)
7524{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007525 if (argvars[0].v_type != VAR_UNKNOWN)
7526 {
7527 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007528
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007529 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7530 job_info(job, rettv->vval.v_dict);
7531 }
7532 else if (rettv_list_alloc(rettv) == OK)
7533 job_info_all(rettv->vval.v_list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007534}
7535
7536/*
7537 * "job_setoptions()" function
7538 */
7539 static void
7540f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7541{
7542 job_T *job = get_job_arg(&argvars[0]);
7543 jobopt_T opt;
7544
7545 if (job == NULL)
7546 return;
7547 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007548 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007549 job_set_options(job, &opt);
7550 free_job_options(&opt);
7551}
7552
7553/*
7554 * "job_start()" function
7555 */
7556 static void
7557f_job_start(typval_T *argvars, typval_T *rettv)
7558{
7559 rettv->v_type = VAR_JOB;
7560 if (check_restricted() || check_secure())
7561 return;
Bram Moolenaar493359e2018-06-12 20:25:52 +02007562 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007563}
7564
7565/*
7566 * "job_status()" function
7567 */
7568 static void
7569f_job_status(typval_T *argvars, typval_T *rettv)
7570{
7571 job_T *job = get_job_arg(&argvars[0]);
7572
7573 if (job != NULL)
7574 {
7575 rettv->v_type = VAR_STRING;
7576 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7577 }
7578}
7579
7580/*
7581 * "job_stop()" function
7582 */
7583 static void
7584f_job_stop(typval_T *argvars, typval_T *rettv)
7585{
7586 job_T *job = get_job_arg(&argvars[0]);
7587
7588 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007589 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007590}
7591#endif
7592
7593/*
7594 * "join()" function
7595 */
7596 static void
7597f_join(typval_T *argvars, typval_T *rettv)
7598{
7599 garray_T ga;
7600 char_u *sep;
7601
7602 if (argvars[0].v_type != VAR_LIST)
7603 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007604 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007605 return;
7606 }
7607 if (argvars[0].vval.v_list == NULL)
7608 return;
7609 if (argvars[1].v_type == VAR_UNKNOWN)
7610 sep = (char_u *)" ";
7611 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007612 sep = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007613
7614 rettv->v_type = VAR_STRING;
7615
7616 if (sep != NULL)
7617 {
7618 ga_init2(&ga, (int)sizeof(char), 80);
7619 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7620 ga_append(&ga, NUL);
7621 rettv->vval.v_string = (char_u *)ga.ga_data;
7622 }
7623 else
7624 rettv->vval.v_string = NULL;
7625}
7626
7627/*
7628 * "js_decode()" function
7629 */
7630 static void
7631f_js_decode(typval_T *argvars, typval_T *rettv)
7632{
7633 js_read_T reader;
7634
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007635 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007636 reader.js_fill = NULL;
7637 reader.js_used = 0;
7638 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007639 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007640}
7641
7642/*
7643 * "js_encode()" function
7644 */
7645 static void
7646f_js_encode(typval_T *argvars, typval_T *rettv)
7647{
7648 rettv->v_type = VAR_STRING;
7649 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7650}
7651
7652/*
7653 * "json_decode()" function
7654 */
7655 static void
7656f_json_decode(typval_T *argvars, typval_T *rettv)
7657{
7658 js_read_T reader;
7659
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007660 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007661 reader.js_fill = NULL;
7662 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007663 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007664}
7665
7666/*
7667 * "json_encode()" function
7668 */
7669 static void
7670f_json_encode(typval_T *argvars, typval_T *rettv)
7671{
7672 rettv->v_type = VAR_STRING;
7673 rettv->vval.v_string = json_encode(&argvars[0], 0);
7674}
7675
7676/*
7677 * "keys()" function
7678 */
7679 static void
7680f_keys(typval_T *argvars, typval_T *rettv)
7681{
7682 dict_list(argvars, rettv, 0);
7683}
7684
7685/*
7686 * "last_buffer_nr()" function.
7687 */
7688 static void
7689f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7690{
7691 int n = 0;
7692 buf_T *buf;
7693
Bram Moolenaar29323592016-07-24 22:04:11 +02007694 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007695 if (n < buf->b_fnum)
7696 n = buf->b_fnum;
7697
7698 rettv->vval.v_number = n;
7699}
7700
7701/*
7702 * "len()" function
7703 */
7704 static void
7705f_len(typval_T *argvars, typval_T *rettv)
7706{
7707 switch (argvars[0].v_type)
7708 {
7709 case VAR_STRING:
7710 case VAR_NUMBER:
7711 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007712 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007713 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007714 case VAR_BLOB:
7715 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
7716 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007717 case VAR_LIST:
7718 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7719 break;
7720 case VAR_DICT:
7721 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7722 break;
7723 case VAR_UNKNOWN:
7724 case VAR_SPECIAL:
7725 case VAR_FLOAT:
7726 case VAR_FUNC:
7727 case VAR_PARTIAL:
7728 case VAR_JOB:
7729 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007730 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007731 break;
7732 }
7733}
7734
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007735 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007736libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007737{
7738#ifdef FEAT_LIBCALL
7739 char_u *string_in;
7740 char_u **string_result;
7741 int nr_result;
7742#endif
7743
7744 rettv->v_type = type;
7745 if (type != VAR_NUMBER)
7746 rettv->vval.v_string = NULL;
7747
7748 if (check_restricted() || check_secure())
7749 return;
7750
7751#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007752 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007753 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7754 {
7755 string_in = NULL;
7756 if (argvars[2].v_type == VAR_STRING)
7757 string_in = argvars[2].vval.v_string;
7758 if (type == VAR_NUMBER)
7759 string_result = NULL;
7760 else
7761 string_result = &rettv->vval.v_string;
7762 if (mch_libcall(argvars[0].vval.v_string,
7763 argvars[1].vval.v_string,
7764 string_in,
7765 argvars[2].vval.v_number,
7766 string_result,
7767 &nr_result) == OK
7768 && type == VAR_NUMBER)
7769 rettv->vval.v_number = nr_result;
7770 }
7771#endif
7772}
7773
7774/*
7775 * "libcall()" function
7776 */
7777 static void
7778f_libcall(typval_T *argvars, typval_T *rettv)
7779{
7780 libcall_common(argvars, rettv, VAR_STRING);
7781}
7782
7783/*
7784 * "libcallnr()" function
7785 */
7786 static void
7787f_libcallnr(typval_T *argvars, typval_T *rettv)
7788{
7789 libcall_common(argvars, rettv, VAR_NUMBER);
7790}
7791
7792/*
7793 * "line(string)" function
7794 */
7795 static void
7796f_line(typval_T *argvars, typval_T *rettv)
7797{
7798 linenr_T lnum = 0;
7799 pos_T *fp;
7800 int fnum;
7801
7802 fp = var2fpos(&argvars[0], TRUE, &fnum);
7803 if (fp != NULL)
7804 lnum = fp->lnum;
7805 rettv->vval.v_number = lnum;
7806}
7807
7808/*
7809 * "line2byte(lnum)" function
7810 */
7811 static void
7812f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7813{
7814#ifndef FEAT_BYTEOFF
7815 rettv->vval.v_number = -1;
7816#else
7817 linenr_T lnum;
7818
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007819 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007820 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7821 rettv->vval.v_number = -1;
7822 else
7823 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7824 if (rettv->vval.v_number >= 0)
7825 ++rettv->vval.v_number;
7826#endif
7827}
7828
7829/*
7830 * "lispindent(lnum)" function
7831 */
7832 static void
7833f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7834{
7835#ifdef FEAT_LISP
7836 pos_T pos;
7837 linenr_T lnum;
7838
7839 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007840 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007841 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7842 {
7843 curwin->w_cursor.lnum = lnum;
7844 rettv->vval.v_number = get_lisp_indent();
7845 curwin->w_cursor = pos;
7846 }
7847 else
7848#endif
7849 rettv->vval.v_number = -1;
7850}
7851
7852/*
7853 * "localtime()" function
7854 */
7855 static void
7856f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7857{
7858 rettv->vval.v_number = (varnumber_T)time(NULL);
7859}
7860
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007861 static void
7862get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7863{
7864 char_u *keys;
7865 char_u *which;
7866 char_u buf[NUMBUFLEN];
7867 char_u *keys_buf = NULL;
7868 char_u *rhs;
7869 int mode;
7870 int abbr = FALSE;
7871 int get_dict = FALSE;
7872 mapblock_T *mp;
7873 int buffer_local;
7874
7875 /* return empty string for failure */
7876 rettv->v_type = VAR_STRING;
7877 rettv->vval.v_string = NULL;
7878
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007879 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007880 if (*keys == NUL)
7881 return;
7882
7883 if (argvars[1].v_type != VAR_UNKNOWN)
7884 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007885 which = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007886 if (argvars[2].v_type != VAR_UNKNOWN)
7887 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007888 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007889 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007890 get_dict = (int)tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007891 }
7892 }
7893 else
7894 which = (char_u *)"";
7895 if (which == NULL)
7896 return;
7897
7898 mode = get_map_mode(&which, 0);
7899
7900 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7901 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7902 vim_free(keys_buf);
7903
7904 if (!get_dict)
7905 {
7906 /* Return a string. */
7907 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02007908 {
7909 if (*rhs == NUL)
7910 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
7911 else
7912 rettv->vval.v_string = str2special_save(rhs, FALSE);
7913 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007914
7915 }
7916 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7917 {
7918 /* Return a dictionary. */
7919 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7920 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7921 dict_T *dict = rettv->vval.v_dict;
7922
Bram Moolenaare0be1672018-07-08 16:50:37 +02007923 dict_add_string(dict, "lhs", lhs);
7924 dict_add_string(dict, "rhs", mp->m_orig_str);
7925 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
7926 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
7927 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007928 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
7929 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02007930 dict_add_number(dict, "buffer", (long)buffer_local);
7931 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
7932 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007933
7934 vim_free(lhs);
7935 vim_free(mapmode);
7936 }
7937}
7938
7939#ifdef FEAT_FLOAT
7940/*
7941 * "log()" function
7942 */
7943 static void
7944f_log(typval_T *argvars, typval_T *rettv)
7945{
7946 float_T f = 0.0;
7947
7948 rettv->v_type = VAR_FLOAT;
7949 if (get_float_arg(argvars, &f) == OK)
7950 rettv->vval.v_float = log(f);
7951 else
7952 rettv->vval.v_float = 0.0;
7953}
7954
7955/*
7956 * "log10()" function
7957 */
7958 static void
7959f_log10(typval_T *argvars, typval_T *rettv)
7960{
7961 float_T f = 0.0;
7962
7963 rettv->v_type = VAR_FLOAT;
7964 if (get_float_arg(argvars, &f) == OK)
7965 rettv->vval.v_float = log10(f);
7966 else
7967 rettv->vval.v_float = 0.0;
7968}
7969#endif
7970
7971#ifdef FEAT_LUA
7972/*
7973 * "luaeval()" function
7974 */
7975 static void
7976f_luaeval(typval_T *argvars, typval_T *rettv)
7977{
7978 char_u *str;
7979 char_u buf[NUMBUFLEN];
7980
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007981 if (check_restricted() || check_secure())
7982 return;
7983
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007984 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007985 do_luaeval(str, argvars + 1, rettv);
7986}
7987#endif
7988
7989/*
7990 * "map()" function
7991 */
7992 static void
7993f_map(typval_T *argvars, typval_T *rettv)
7994{
7995 filter_map(argvars, rettv, TRUE);
7996}
7997
7998/*
7999 * "maparg()" function
8000 */
8001 static void
8002f_maparg(typval_T *argvars, typval_T *rettv)
8003{
8004 get_maparg(argvars, rettv, TRUE);
8005}
8006
8007/*
8008 * "mapcheck()" function
8009 */
8010 static void
8011f_mapcheck(typval_T *argvars, typval_T *rettv)
8012{
8013 get_maparg(argvars, rettv, FALSE);
8014}
8015
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008016typedef enum
8017{
8018 MATCH_END, /* matchend() */
8019 MATCH_MATCH, /* match() */
8020 MATCH_STR, /* matchstr() */
8021 MATCH_LIST, /* matchlist() */
8022 MATCH_POS /* matchstrpos() */
8023} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008024
8025 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008026find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008027{
8028 char_u *str = NULL;
8029 long len = 0;
8030 char_u *expr = NULL;
8031 char_u *pat;
8032 regmatch_T regmatch;
8033 char_u patbuf[NUMBUFLEN];
8034 char_u strbuf[NUMBUFLEN];
8035 char_u *save_cpo;
8036 long start = 0;
8037 long nth = 1;
8038 colnr_T startcol = 0;
8039 int match = 0;
8040 list_T *l = NULL;
8041 listitem_T *li = NULL;
8042 long idx = 0;
8043 char_u *tofree = NULL;
8044
8045 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
8046 save_cpo = p_cpo;
8047 p_cpo = (char_u *)"";
8048
8049 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008050 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008051 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008052 /* type MATCH_LIST: return empty list when there are no matches.
8053 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008054 if (rettv_list_alloc(rettv) == FAIL)
8055 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008056 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008057 && (list_append_string(rettv->vval.v_list,
8058 (char_u *)"", 0) == FAIL
8059 || list_append_number(rettv->vval.v_list,
8060 (varnumber_T)-1) == FAIL
8061 || list_append_number(rettv->vval.v_list,
8062 (varnumber_T)-1) == FAIL
8063 || list_append_number(rettv->vval.v_list,
8064 (varnumber_T)-1) == FAIL))
8065 {
8066 list_free(rettv->vval.v_list);
8067 rettv->vval.v_list = NULL;
8068 goto theend;
8069 }
8070 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008071 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008072 {
8073 rettv->v_type = VAR_STRING;
8074 rettv->vval.v_string = NULL;
8075 }
8076
8077 if (argvars[0].v_type == VAR_LIST)
8078 {
8079 if ((l = argvars[0].vval.v_list) == NULL)
8080 goto theend;
8081 li = l->lv_first;
8082 }
8083 else
8084 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008085 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008086 len = (long)STRLEN(str);
8087 }
8088
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008089 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008090 if (pat == NULL)
8091 goto theend;
8092
8093 if (argvars[2].v_type != VAR_UNKNOWN)
8094 {
8095 int error = FALSE;
8096
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008097 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008098 if (error)
8099 goto theend;
8100 if (l != NULL)
8101 {
8102 li = list_find(l, start);
8103 if (li == NULL)
8104 goto theend;
8105 idx = l->lv_idx; /* use the cached index */
8106 }
8107 else
8108 {
8109 if (start < 0)
8110 start = 0;
8111 if (start > len)
8112 goto theend;
8113 /* When "count" argument is there ignore matches before "start",
8114 * otherwise skip part of the string. Differs when pattern is "^"
8115 * or "\<". */
8116 if (argvars[3].v_type != VAR_UNKNOWN)
8117 startcol = start;
8118 else
8119 {
8120 str += start;
8121 len -= start;
8122 }
8123 }
8124
8125 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008126 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008127 if (error)
8128 goto theend;
8129 }
8130
8131 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8132 if (regmatch.regprog != NULL)
8133 {
8134 regmatch.rm_ic = p_ic;
8135
8136 for (;;)
8137 {
8138 if (l != NULL)
8139 {
8140 if (li == NULL)
8141 {
8142 match = FALSE;
8143 break;
8144 }
8145 vim_free(tofree);
8146 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
8147 if (str == NULL)
8148 break;
8149 }
8150
8151 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
8152
8153 if (match && --nth <= 0)
8154 break;
8155 if (l == NULL && !match)
8156 break;
8157
8158 /* Advance to just after the match. */
8159 if (l != NULL)
8160 {
8161 li = li->li_next;
8162 ++idx;
8163 }
8164 else
8165 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008166 startcol = (colnr_T)(regmatch.startp[0]
8167 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008168 if (startcol > (colnr_T)len
8169 || str + startcol <= regmatch.startp[0])
8170 {
8171 match = FALSE;
8172 break;
8173 }
8174 }
8175 }
8176
8177 if (match)
8178 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008179 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008180 {
8181 listitem_T *li1 = rettv->vval.v_list->lv_first;
8182 listitem_T *li2 = li1->li_next;
8183 listitem_T *li3 = li2->li_next;
8184 listitem_T *li4 = li3->li_next;
8185
8186 vim_free(li1->li_tv.vval.v_string);
8187 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
8188 (int)(regmatch.endp[0] - regmatch.startp[0]));
8189 li3->li_tv.vval.v_number =
8190 (varnumber_T)(regmatch.startp[0] - expr);
8191 li4->li_tv.vval.v_number =
8192 (varnumber_T)(regmatch.endp[0] - expr);
8193 if (l != NULL)
8194 li2->li_tv.vval.v_number = (varnumber_T)idx;
8195 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008196 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008197 {
8198 int i;
8199
8200 /* return list with matched string and submatches */
8201 for (i = 0; i < NSUBEXP; ++i)
8202 {
8203 if (regmatch.endp[i] == NULL)
8204 {
8205 if (list_append_string(rettv->vval.v_list,
8206 (char_u *)"", 0) == FAIL)
8207 break;
8208 }
8209 else if (list_append_string(rettv->vval.v_list,
8210 regmatch.startp[i],
8211 (int)(regmatch.endp[i] - regmatch.startp[i]))
8212 == FAIL)
8213 break;
8214 }
8215 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008216 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008217 {
8218 /* return matched string */
8219 if (l != NULL)
8220 copy_tv(&li->li_tv, rettv);
8221 else
8222 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
8223 (int)(regmatch.endp[0] - regmatch.startp[0]));
8224 }
8225 else if (l != NULL)
8226 rettv->vval.v_number = idx;
8227 else
8228 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008229 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008230 rettv->vval.v_number =
8231 (varnumber_T)(regmatch.startp[0] - str);
8232 else
8233 rettv->vval.v_number =
8234 (varnumber_T)(regmatch.endp[0] - str);
8235 rettv->vval.v_number += (varnumber_T)(str - expr);
8236 }
8237 }
8238 vim_regfree(regmatch.regprog);
8239 }
8240
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008241theend:
8242 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008243 /* matchstrpos() without a list: drop the second item. */
8244 listitem_remove(rettv->vval.v_list,
8245 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008246 vim_free(tofree);
8247 p_cpo = save_cpo;
8248}
8249
8250/*
8251 * "match()" function
8252 */
8253 static void
8254f_match(typval_T *argvars, typval_T *rettv)
8255{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008256 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008257}
8258
Bram Moolenaar95e51472018-07-28 16:55:56 +02008259#ifdef FEAT_SEARCH_EXTRA
8260 static int
8261matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
8262{
8263 dictitem_T *di;
8264
8265 if (tv->v_type != VAR_DICT)
8266 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008267 emsg(_(e_dictreq));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008268 return FAIL;
8269 }
8270
8271 if (dict_find(tv->vval.v_dict, (char_u *)"conceal", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01008272 *conceal_char = dict_get_string(tv->vval.v_dict,
Bram Moolenaar95e51472018-07-28 16:55:56 +02008273 (char_u *)"conceal", FALSE);
8274
8275 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
8276 {
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02008277 *win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008278 if (*win == NULL)
8279 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01008280 emsg(_(e_invalwindow));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008281 return FAIL;
8282 }
8283 }
8284
8285 return OK;
8286}
8287#endif
8288
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008289/*
8290 * "matchadd()" function
8291 */
8292 static void
8293f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8294{
8295#ifdef FEAT_SEARCH_EXTRA
8296 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008297 char_u *grp = tv_get_string_buf_chk(&argvars[0], buf); /* group */
8298 char_u *pat = tv_get_string_buf_chk(&argvars[1], buf); /* pattern */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008299 int prio = 10; /* default priority */
8300 int id = -1;
8301 int error = FALSE;
8302 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008303 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008304
8305 rettv->vval.v_number = -1;
8306
8307 if (grp == NULL || pat == NULL)
8308 return;
8309 if (argvars[2].v_type != VAR_UNKNOWN)
8310 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008311 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008312 if (argvars[3].v_type != VAR_UNKNOWN)
8313 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008314 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008315 if (argvars[4].v_type != VAR_UNKNOWN
8316 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8317 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008318 }
8319 }
8320 if (error == TRUE)
8321 return;
8322 if (id >= 1 && id <= 3)
8323 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008324 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008325 return;
8326 }
8327
Bram Moolenaar95e51472018-07-28 16:55:56 +02008328 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008329 conceal_char);
8330#endif
8331}
8332
8333/*
8334 * "matchaddpos()" function
8335 */
8336 static void
8337f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8338{
8339#ifdef FEAT_SEARCH_EXTRA
8340 char_u buf[NUMBUFLEN];
8341 char_u *group;
8342 int prio = 10;
8343 int id = -1;
8344 int error = FALSE;
8345 list_T *l;
8346 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008347 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008348
8349 rettv->vval.v_number = -1;
8350
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008351 group = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008352 if (group == NULL)
8353 return;
8354
8355 if (argvars[1].v_type != VAR_LIST)
8356 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008357 semsg(_(e_listarg), "matchaddpos()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008358 return;
8359 }
8360 l = argvars[1].vval.v_list;
8361 if (l == NULL)
8362 return;
8363
8364 if (argvars[2].v_type != VAR_UNKNOWN)
8365 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008366 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008367 if (argvars[3].v_type != VAR_UNKNOWN)
8368 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008369 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008370
8371 if (argvars[4].v_type != VAR_UNKNOWN
8372 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8373 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008374 }
8375 }
8376 if (error == TRUE)
8377 return;
8378
8379 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8380 if (id == 1 || id == 2)
8381 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008382 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008383 return;
8384 }
8385
Bram Moolenaar95e51472018-07-28 16:55:56 +02008386 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008387 conceal_char);
8388#endif
8389}
8390
8391/*
8392 * "matcharg()" function
8393 */
8394 static void
8395f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8396{
8397 if (rettv_list_alloc(rettv) == OK)
8398 {
8399#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008400 int id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008401 matchitem_T *m;
8402
8403 if (id >= 1 && id <= 3)
8404 {
8405 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8406 {
8407 list_append_string(rettv->vval.v_list,
8408 syn_id2name(m->hlg_id), -1);
8409 list_append_string(rettv->vval.v_list, m->pattern, -1);
8410 }
8411 else
8412 {
8413 list_append_string(rettv->vval.v_list, NULL, -1);
8414 list_append_string(rettv->vval.v_list, NULL, -1);
8415 }
8416 }
8417#endif
8418 }
8419}
8420
8421/*
8422 * "matchdelete()" function
8423 */
8424 static void
8425f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8426{
8427#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01008428 win_T *win = get_optional_window(argvars, 1);
8429
8430 if (win == NULL)
8431 rettv->vval.v_number = -1;
8432 else
8433 rettv->vval.v_number = match_delete(win,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008434 (int)tv_get_number(&argvars[0]), TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008435#endif
8436}
8437
8438/*
8439 * "matchend()" function
8440 */
8441 static void
8442f_matchend(typval_T *argvars, typval_T *rettv)
8443{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008444 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008445}
8446
8447/*
8448 * "matchlist()" function
8449 */
8450 static void
8451f_matchlist(typval_T *argvars, typval_T *rettv)
8452{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008453 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008454}
8455
8456/*
8457 * "matchstr()" function
8458 */
8459 static void
8460f_matchstr(typval_T *argvars, typval_T *rettv)
8461{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008462 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008463}
8464
8465/*
8466 * "matchstrpos()" function
8467 */
8468 static void
8469f_matchstrpos(typval_T *argvars, typval_T *rettv)
8470{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008471 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008472}
8473
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008474 static void
8475max_min(typval_T *argvars, typval_T *rettv, int domax)
8476{
8477 varnumber_T n = 0;
8478 varnumber_T i;
8479 int error = FALSE;
8480
8481 if (argvars[0].v_type == VAR_LIST)
8482 {
8483 list_T *l;
8484 listitem_T *li;
8485
8486 l = argvars[0].vval.v_list;
8487 if (l != NULL)
8488 {
8489 li = l->lv_first;
8490 if (li != NULL)
8491 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008492 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008493 for (;;)
8494 {
8495 li = li->li_next;
8496 if (li == NULL)
8497 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008498 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008499 if (domax ? i > n : i < n)
8500 n = i;
8501 }
8502 }
8503 }
8504 }
8505 else if (argvars[0].v_type == VAR_DICT)
8506 {
8507 dict_T *d;
8508 int first = TRUE;
8509 hashitem_T *hi;
8510 int todo;
8511
8512 d = argvars[0].vval.v_dict;
8513 if (d != NULL)
8514 {
8515 todo = (int)d->dv_hashtab.ht_used;
8516 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8517 {
8518 if (!HASHITEM_EMPTY(hi))
8519 {
8520 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008521 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008522 if (first)
8523 {
8524 n = i;
8525 first = FALSE;
8526 }
8527 else if (domax ? i > n : i < n)
8528 n = i;
8529 }
8530 }
8531 }
8532 }
8533 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008534 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008535 rettv->vval.v_number = error ? 0 : n;
8536}
8537
8538/*
8539 * "max()" function
8540 */
8541 static void
8542f_max(typval_T *argvars, typval_T *rettv)
8543{
8544 max_min(argvars, rettv, TRUE);
8545}
8546
8547/*
8548 * "min()" function
8549 */
8550 static void
8551f_min(typval_T *argvars, typval_T *rettv)
8552{
8553 max_min(argvars, rettv, FALSE);
8554}
8555
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008556/*
8557 * Create the directory in which "dir" is located, and higher levels when
8558 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008559 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008560 */
8561 static int
8562mkdir_recurse(char_u *dir, int prot)
8563{
8564 char_u *p;
8565 char_u *updir;
8566 int r = FAIL;
8567
8568 /* Get end of directory name in "dir".
8569 * We're done when it's "/" or "c:/". */
8570 p = gettail_sep(dir);
8571 if (p <= get_past_head(dir))
8572 return OK;
8573
8574 /* If the directory exists we're done. Otherwise: create it.*/
8575 updir = vim_strnsave(dir, (int)(p - dir));
8576 if (updir == NULL)
8577 return FAIL;
8578 if (mch_isdir(updir))
8579 r = OK;
8580 else if (mkdir_recurse(updir, prot) == OK)
8581 r = vim_mkdir_emsg(updir, prot);
8582 vim_free(updir);
8583 return r;
8584}
8585
8586#ifdef vim_mkdir
8587/*
8588 * "mkdir()" function
8589 */
8590 static void
8591f_mkdir(typval_T *argvars, typval_T *rettv)
8592{
8593 char_u *dir;
8594 char_u buf[NUMBUFLEN];
8595 int prot = 0755;
8596
8597 rettv->vval.v_number = FAIL;
8598 if (check_restricted() || check_secure())
8599 return;
8600
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008601 dir = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008602 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008603 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008604
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008605 if (*gettail(dir) == NUL)
8606 /* remove trailing slashes */
8607 *gettail_sep(dir) = NUL;
8608
8609 if (argvars[1].v_type != VAR_UNKNOWN)
8610 {
8611 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008612 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008613 prot = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008614 if (prot == -1)
8615 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008616 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008617 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008618 {
8619 if (mch_isdir(dir))
8620 {
8621 /* With the "p" flag it's OK if the dir already exists. */
8622 rettv->vval.v_number = OK;
8623 return;
8624 }
8625 mkdir_recurse(dir, prot);
8626 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008627 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008628 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008629}
8630#endif
8631
8632/*
8633 * "mode()" function
8634 */
8635 static void
8636f_mode(typval_T *argvars, typval_T *rettv)
8637{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008638 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008639
Bram Moolenaar612cc382018-07-29 15:34:26 +02008640 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008641
8642 if (time_for_testing == 93784)
8643 {
8644 /* Testing the two-character code. */
8645 buf[0] = 'x';
8646 buf[1] = '!';
8647 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008648#ifdef FEAT_TERMINAL
8649 else if (term_use_loop())
8650 buf[0] = 't';
8651#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008652 else if (VIsual_active)
8653 {
8654 if (VIsual_select)
8655 buf[0] = VIsual_mode + 's' - 'v';
8656 else
8657 buf[0] = VIsual_mode;
8658 }
8659 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8660 || State == CONFIRM)
8661 {
8662 buf[0] = 'r';
8663 if (State == ASKMORE)
8664 buf[1] = 'm';
8665 else if (State == CONFIRM)
8666 buf[1] = '?';
8667 }
8668 else if (State == EXTERNCMD)
8669 buf[0] = '!';
8670 else if (State & INSERT)
8671 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008672 if (State & VREPLACE_FLAG)
8673 {
8674 buf[0] = 'R';
8675 buf[1] = 'v';
8676 }
8677 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008678 {
8679 if (State & REPLACE_FLAG)
8680 buf[0] = 'R';
8681 else
8682 buf[0] = 'i';
8683#ifdef FEAT_INS_EXPAND
8684 if (ins_compl_active())
8685 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008686 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008687 buf[1] = 'x';
8688#endif
8689 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008690 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008691 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008692 {
8693 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008694 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008695 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008696 else if (exmode_active == EXMODE_NORMAL)
8697 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008698 }
8699 else
8700 {
8701 buf[0] = 'n';
8702 if (finish_op)
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008703 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008704 buf[1] = 'o';
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008705 // to be able to detect force-linewise/blockwise/characterwise operations
8706 buf[2] = motion_force;
8707 }
Bram Moolenaar612cc382018-07-29 15:34:26 +02008708 else if (restart_edit == 'I' || restart_edit == 'R'
8709 || restart_edit == 'V')
8710 {
8711 buf[1] = 'i';
8712 buf[2] = restart_edit;
8713 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008714 }
8715
8716 /* Clear out the minor mode when the argument is not a non-zero number or
8717 * non-empty string. */
8718 if (!non_zero_arg(&argvars[0]))
8719 buf[1] = NUL;
8720
8721 rettv->vval.v_string = vim_strsave(buf);
8722 rettv->v_type = VAR_STRING;
8723}
8724
8725#if defined(FEAT_MZSCHEME) || defined(PROTO)
8726/*
8727 * "mzeval()" function
8728 */
8729 static void
8730f_mzeval(typval_T *argvars, typval_T *rettv)
8731{
8732 char_u *str;
8733 char_u buf[NUMBUFLEN];
8734
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008735 if (check_restricted() || check_secure())
8736 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008737 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008738 do_mzeval(str, rettv);
8739}
8740
8741 void
8742mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8743{
8744 typval_T argvars[3];
8745
8746 argvars[0].v_type = VAR_STRING;
8747 argvars[0].vval.v_string = name;
8748 copy_tv(args, &argvars[1]);
8749 argvars[2].v_type = VAR_UNKNOWN;
8750 f_call(argvars, rettv);
8751 clear_tv(&argvars[1]);
8752}
8753#endif
8754
8755/*
8756 * "nextnonblank()" function
8757 */
8758 static void
8759f_nextnonblank(typval_T *argvars, typval_T *rettv)
8760{
8761 linenr_T lnum;
8762
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008763 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008764 {
8765 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8766 {
8767 lnum = 0;
8768 break;
8769 }
8770 if (*skipwhite(ml_get(lnum)) != NUL)
8771 break;
8772 }
8773 rettv->vval.v_number = lnum;
8774}
8775
8776/*
8777 * "nr2char()" function
8778 */
8779 static void
8780f_nr2char(typval_T *argvars, typval_T *rettv)
8781{
8782 char_u buf[NUMBUFLEN];
8783
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008784 if (has_mbyte)
8785 {
8786 int utf8 = 0;
8787
8788 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008789 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008790 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01008791 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008792 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008793 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008794 }
8795 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008796 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008797 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008798 buf[1] = NUL;
8799 }
8800 rettv->v_type = VAR_STRING;
8801 rettv->vval.v_string = vim_strsave(buf);
8802}
8803
8804/*
8805 * "or(expr, expr)" function
8806 */
8807 static void
8808f_or(typval_T *argvars, typval_T *rettv)
8809{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008810 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
8811 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008812}
8813
8814/*
8815 * "pathshorten()" function
8816 */
8817 static void
8818f_pathshorten(typval_T *argvars, typval_T *rettv)
8819{
8820 char_u *p;
8821
8822 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008823 p = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008824 if (p == NULL)
8825 rettv->vval.v_string = NULL;
8826 else
8827 {
8828 p = vim_strsave(p);
8829 rettv->vval.v_string = p;
8830 if (p != NULL)
8831 shorten_dir(p);
8832 }
8833}
8834
8835#ifdef FEAT_PERL
8836/*
8837 * "perleval()" function
8838 */
8839 static void
8840f_perleval(typval_T *argvars, typval_T *rettv)
8841{
8842 char_u *str;
8843 char_u buf[NUMBUFLEN];
8844
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008845 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008846 do_perleval(str, rettv);
8847}
8848#endif
8849
8850#ifdef FEAT_FLOAT
8851/*
8852 * "pow()" function
8853 */
8854 static void
8855f_pow(typval_T *argvars, typval_T *rettv)
8856{
8857 float_T fx = 0.0, fy = 0.0;
8858
8859 rettv->v_type = VAR_FLOAT;
8860 if (get_float_arg(argvars, &fx) == OK
8861 && get_float_arg(&argvars[1], &fy) == OK)
8862 rettv->vval.v_float = pow(fx, fy);
8863 else
8864 rettv->vval.v_float = 0.0;
8865}
8866#endif
8867
8868/*
8869 * "prevnonblank()" function
8870 */
8871 static void
8872f_prevnonblank(typval_T *argvars, typval_T *rettv)
8873{
8874 linenr_T lnum;
8875
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008876 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008877 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8878 lnum = 0;
8879 else
8880 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8881 --lnum;
8882 rettv->vval.v_number = lnum;
8883}
8884
8885/* This dummy va_list is here because:
8886 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8887 * - locally in the function results in a "used before set" warning
8888 * - using va_start() to initialize it gives "function with fixed args" error */
8889static va_list ap;
8890
8891/*
8892 * "printf()" function
8893 */
8894 static void
8895f_printf(typval_T *argvars, typval_T *rettv)
8896{
8897 char_u buf[NUMBUFLEN];
8898 int len;
8899 char_u *s;
8900 int saved_did_emsg = did_emsg;
8901 char *fmt;
8902
8903 rettv->v_type = VAR_STRING;
8904 rettv->vval.v_string = NULL;
8905
8906 /* Get the required length, allocate the buffer and do it for real. */
8907 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008908 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008909 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008910 if (!did_emsg)
8911 {
8912 s = alloc(len + 1);
8913 if (s != NULL)
8914 {
8915 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008916 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8917 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008918 }
8919 }
8920 did_emsg |= saved_did_emsg;
8921}
8922
Bram Moolenaarf2732452018-06-03 14:47:35 +02008923#ifdef FEAT_JOB_CHANNEL
8924/*
8925 * "prompt_setcallback({buffer}, {callback})" function
8926 */
8927 static void
8928f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
8929{
8930 buf_T *buf;
8931 char_u *callback;
8932 partial_T *partial;
8933
8934 if (check_secure())
8935 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01008936 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008937 if (buf == NULL)
8938 return;
8939
8940 callback = get_callback(&argvars[1], &partial);
8941 if (callback == NULL)
8942 return;
8943
8944 free_callback(buf->b_prompt_callback, buf->b_prompt_partial);
8945 if (partial == NULL)
8946 buf->b_prompt_callback = vim_strsave(callback);
8947 else
8948 /* pointer into the partial */
8949 buf->b_prompt_callback = callback;
8950 buf->b_prompt_partial = partial;
8951}
8952
8953/*
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02008954 * "prompt_setinterrupt({buffer}, {callback})" function
8955 */
8956 static void
8957f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
8958{
8959 buf_T *buf;
8960 char_u *callback;
8961 partial_T *partial;
8962
8963 if (check_secure())
8964 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01008965 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02008966 if (buf == NULL)
8967 return;
8968
8969 callback = get_callback(&argvars[1], &partial);
8970 if (callback == NULL)
8971 return;
8972
8973 free_callback(buf->b_prompt_interrupt, buf->b_prompt_int_partial);
8974 if (partial == NULL)
8975 buf->b_prompt_interrupt = vim_strsave(callback);
8976 else
8977 /* pointer into the partial */
8978 buf->b_prompt_interrupt = callback;
8979 buf->b_prompt_int_partial = partial;
8980}
8981
8982/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02008983 * "prompt_setprompt({buffer}, {text})" function
8984 */
8985 static void
8986f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
8987{
8988 buf_T *buf;
8989 char_u *text;
8990
8991 if (check_secure())
8992 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01008993 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008994 if (buf == NULL)
8995 return;
8996
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008997 text = tv_get_string(&argvars[1]);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008998 vim_free(buf->b_prompt_text);
8999 buf->b_prompt_text = vim_strsave(text);
9000}
9001#endif
9002
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009003/*
9004 * "pumvisible()" function
9005 */
9006 static void
9007f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9008{
9009#ifdef FEAT_INS_EXPAND
9010 if (pum_visible())
9011 rettv->vval.v_number = 1;
9012#endif
9013}
9014
9015#ifdef FEAT_PYTHON3
9016/*
9017 * "py3eval()" function
9018 */
9019 static void
9020f_py3eval(typval_T *argvars, typval_T *rettv)
9021{
9022 char_u *str;
9023 char_u buf[NUMBUFLEN];
9024
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009025 if (check_restricted() || check_secure())
9026 return;
9027
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009028 if (p_pyx == 0)
9029 p_pyx = 3;
9030
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009031 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009032 do_py3eval(str, rettv);
9033}
9034#endif
9035
9036#ifdef FEAT_PYTHON
9037/*
9038 * "pyeval()" function
9039 */
9040 static void
9041f_pyeval(typval_T *argvars, typval_T *rettv)
9042{
9043 char_u *str;
9044 char_u buf[NUMBUFLEN];
9045
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009046 if (check_restricted() || check_secure())
9047 return;
9048
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009049 if (p_pyx == 0)
9050 p_pyx = 2;
9051
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009052 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009053 do_pyeval(str, rettv);
9054}
9055#endif
9056
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009057#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
9058/*
9059 * "pyxeval()" function
9060 */
9061 static void
9062f_pyxeval(typval_T *argvars, typval_T *rettv)
9063{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009064 if (check_restricted() || check_secure())
9065 return;
9066
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009067# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
9068 init_pyxversion();
9069 if (p_pyx == 2)
9070 f_pyeval(argvars, rettv);
9071 else
9072 f_py3eval(argvars, rettv);
9073# elif defined(FEAT_PYTHON)
9074 f_pyeval(argvars, rettv);
9075# elif defined(FEAT_PYTHON3)
9076 f_py3eval(argvars, rettv);
9077# endif
9078}
9079#endif
9080
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009081/*
9082 * "range()" function
9083 */
9084 static void
9085f_range(typval_T *argvars, typval_T *rettv)
9086{
9087 varnumber_T start;
9088 varnumber_T end;
9089 varnumber_T stride = 1;
9090 varnumber_T i;
9091 int error = FALSE;
9092
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009093 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009094 if (argvars[1].v_type == VAR_UNKNOWN)
9095 {
9096 end = start - 1;
9097 start = 0;
9098 }
9099 else
9100 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009101 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009102 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009103 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009104 }
9105
9106 if (error)
9107 return; /* type error; errmsg already given */
9108 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009109 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009110 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009111 emsg(_("E727: Start past end"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009112 else
9113 {
9114 if (rettv_list_alloc(rettv) == OK)
9115 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
9116 if (list_append_number(rettv->vval.v_list,
9117 (varnumber_T)i) == FAIL)
9118 break;
9119 }
9120}
9121
9122/*
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009123 * Evaluate "expr" for readdir().
9124 */
9125 static int
9126readdir_checkitem(typval_T *expr, char_u *name)
9127{
9128 typval_T save_val;
9129 typval_T rettv;
9130 typval_T argv[2];
9131 int retval = 0;
9132 int error = FALSE;
9133
9134 prepare_vimvar(VV_VAL, &save_val);
9135 set_vim_var_string(VV_VAL, name, -1);
9136 argv[0].v_type = VAR_STRING;
9137 argv[0].vval.v_string = name;
9138
9139 if (eval_expr_typval(expr, argv, 1, &rettv) == FAIL)
9140 goto theend;
9141
9142 retval = tv_get_number_chk(&rettv, &error);
9143 if (error)
9144 retval = -1;
9145 clear_tv(&rettv);
9146
9147theend:
9148 set_vim_var_string(VV_VAL, NULL, 0);
9149 restore_vimvar(VV_VAL, &save_val);
9150 return retval;
9151}
9152
9153/*
9154 * "readdir()" function
9155 */
9156 static void
9157f_readdir(typval_T *argvars, typval_T *rettv)
9158{
9159 typval_T *expr;
9160 int failed = FALSE;
9161 char_u *path;
9162 garray_T ga;
9163 int i;
9164#ifdef MSWIN
9165 char_u *buf, *p;
9166 WIN32_FIND_DATA fb;
9167 int ok;
9168 HANDLE hFind = INVALID_HANDLE_VALUE;
9169 WIN32_FIND_DATAW wfb;
9170 WCHAR *wn = NULL; // UCS-2 name, NULL when not used.
9171#endif
9172
9173 if (rettv_list_alloc(rettv) == FAIL)
9174 return;
9175 path = tv_get_string(&argvars[0]);
9176 expr = &argvars[1];
9177 ga_init2(&ga, (int)sizeof(char *), 20);
9178
9179#ifdef MSWIN
9180 buf = alloc((int)MAXPATHL);
9181 if (buf == NULL)
9182 return;
9183 STRNCPY(buf, path, MAXPATHL-5);
9184 p = vim_strpbrk(path, (char_u *)"\\/");
9185 if (p != NULL)
9186 *p = NUL;
9187 STRCAT(buf, "\\*");
9188
9189 wn = enc_to_utf16(buf, NULL);
9190 if (wn != NULL)
9191 hFind = FindFirstFileW(wn, &wfb);
9192 ok = (hFind != INVALID_HANDLE_VALUE);
9193 if (!ok)
9194 smsg(_(e_notopen), path);
9195 else
9196 {
9197 while (ok)
9198 {
9199 int ignore;
9200
9201 p = utf16_to_enc(wfb.cFileName, NULL); // p is allocated here
9202 if (p == NULL)
9203 break; // out of memory
9204
9205 ignore = p[0] == '.' && (p[1] == NUL
9206 || (p[1] == '.' && p[2] == NUL));
9207 if (!ignore && expr->v_type != VAR_UNKNOWN)
9208 {
9209 int r = readdir_checkitem(expr, p);
9210
9211 if (r < 0)
9212 {
9213 vim_free(p);
9214 break;
9215 }
9216 if (r == 0)
9217 ignore = TRUE;
9218 }
9219
9220 if (!ignore)
9221 {
9222 if (ga_grow(&ga, 1) == OK)
9223 ((char_u**)ga.ga_data)[ga.ga_len++] = vim_strsave(p);
9224 else
9225 {
9226 failed = TRUE;
9227 vim_free(p);
9228 break;
9229 }
9230 }
9231
9232 vim_free(p);
9233 ok = FindNextFileW(hFind, &wfb);
9234 }
9235 FindClose(hFind);
9236 }
9237
9238 vim_free(buf);
9239 vim_free(wn);
9240#else
9241 DIR *dirp;
9242 struct dirent *dp;
9243 char_u *p;
9244
9245 dirp = opendir((char *)path);
9246 if (dirp == NULL)
9247 smsg(_(e_notopen), path);
9248 else
9249 {
9250 for (;;)
9251 {
9252 int ignore;
9253
9254 dp = readdir(dirp);
9255 if (dp == NULL)
9256 break;
9257 p = (char_u *)dp->d_name;
9258
9259 ignore = p[0] == '.' &&
9260 (p[1] == NUL ||
9261 (p[1] == '.' && p[2] == NUL));
9262 if (!ignore && expr->v_type != VAR_UNKNOWN)
9263 {
9264 int r = readdir_checkitem(expr, p);
9265
9266 if (r < 0)
9267 break;
9268 if (r == 0)
9269 ignore = TRUE;
9270 }
9271
9272 if (!ignore)
9273 {
9274 if (ga_grow(&ga, 1) == OK)
9275 ((char_u**)ga.ga_data)[ga.ga_len++] = vim_strsave(p);
9276 else
9277 {
9278 failed = TRUE;
9279 break;
9280 }
9281 }
9282 }
9283
9284 closedir(dirp);
9285 }
9286#endif
9287
9288 rettv->vval.v_list = list_alloc();
9289 if (!failed && rettv->vval.v_list != NULL)
9290 {
9291 ++rettv->vval.v_list->lv_refcount;
9292 sort_strings((char_u **)ga.ga_data, ga.ga_len);
9293 for (i = 0; i < ga.ga_len; i++)
9294 {
9295 p = ((char_u **)ga.ga_data)[i];
9296 list_append_string(rettv->vval.v_list, p, -1);
9297 }
9298 }
9299 for (i = 0; i < ga.ga_len; i++)
9300 vim_free(((char_u **)ga.ga_data)[i]);
9301
9302 ga_clear(&ga);
9303}
9304
9305/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009306 * "readfile()" function
9307 */
9308 static void
9309f_readfile(typval_T *argvars, typval_T *rettv)
9310{
9311 int binary = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009312 int blob = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009313 int failed = FALSE;
9314 char_u *fname;
9315 FILE *fd;
9316 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
9317 int io_size = sizeof(buf);
9318 int readlen; /* size of last fread() */
9319 char_u *prev = NULL; /* previously read bytes, if any */
9320 long prevlen = 0; /* length of data in prev */
9321 long prevsize = 0; /* size of prev buffer */
9322 long maxline = MAXLNUM;
9323 long cnt = 0;
9324 char_u *p; /* position in buf */
9325 char_u *start; /* start of current line */
9326
9327 if (argvars[1].v_type != VAR_UNKNOWN)
9328 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009329 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009330 binary = TRUE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009331 if (STRCMP(tv_get_string(&argvars[1]), "B") == 0)
9332 blob = TRUE;
9333
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009334 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009335 maxline = (long)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009336 }
9337
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009338 if (blob)
9339 {
9340 if (rettv_blob_alloc(rettv) == FAIL)
9341 return;
9342 }
9343 else
9344 {
9345 if (rettv_list_alloc(rettv) == FAIL)
9346 return;
9347 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009348
9349 /* Always open the file in binary mode, library functions have a mind of
9350 * their own about CR-LF conversion. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009351 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009352 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
9353 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009354 semsg(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009355 return;
9356 }
9357
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009358 if (blob)
9359 {
9360 if (read_blob(fd, rettv->vval.v_blob) == FAIL)
9361 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009362 emsg("cannot read file");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009363 blob_free(rettv->vval.v_blob);
9364 }
9365 fclose(fd);
9366 return;
9367 }
9368
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009369 while (cnt < maxline || maxline < 0)
9370 {
9371 readlen = (int)fread(buf, 1, io_size, fd);
9372
9373 /* This for loop processes what was read, but is also entered at end
9374 * of file so that either:
9375 * - an incomplete line gets written
9376 * - a "binary" file gets an empty line at the end if it ends in a
9377 * newline. */
9378 for (p = buf, start = buf;
9379 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
9380 ++p)
9381 {
9382 if (*p == '\n' || readlen <= 0)
9383 {
9384 listitem_T *li;
9385 char_u *s = NULL;
9386 long_u len = p - start;
9387
9388 /* Finished a line. Remove CRs before NL. */
9389 if (readlen > 0 && !binary)
9390 {
9391 while (len > 0 && start[len - 1] == '\r')
9392 --len;
9393 /* removal may cross back to the "prev" string */
9394 if (len == 0)
9395 while (prevlen > 0 && prev[prevlen - 1] == '\r')
9396 --prevlen;
9397 }
9398 if (prevlen == 0)
9399 s = vim_strnsave(start, (int)len);
9400 else
9401 {
9402 /* Change "prev" buffer to be the right size. This way
9403 * the bytes are only copied once, and very long lines are
9404 * allocated only once. */
9405 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
9406 {
9407 mch_memmove(s + prevlen, start, len);
9408 s[prevlen + len] = NUL;
9409 prev = NULL; /* the list will own the string */
9410 prevlen = prevsize = 0;
9411 }
9412 }
9413 if (s == NULL)
9414 {
9415 do_outofmem_msg((long_u) prevlen + len + 1);
9416 failed = TRUE;
9417 break;
9418 }
9419
9420 if ((li = listitem_alloc()) == NULL)
9421 {
9422 vim_free(s);
9423 failed = TRUE;
9424 break;
9425 }
9426 li->li_tv.v_type = VAR_STRING;
9427 li->li_tv.v_lock = 0;
9428 li->li_tv.vval.v_string = s;
9429 list_append(rettv->vval.v_list, li);
9430
9431 start = p + 1; /* step over newline */
9432 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
9433 break;
9434 }
9435 else if (*p == NUL)
9436 *p = '\n';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009437 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
9438 * when finding the BF and check the previous two bytes. */
9439 else if (*p == 0xbf && enc_utf8 && !binary)
9440 {
9441 /* Find the two bytes before the 0xbf. If p is at buf, or buf
9442 * + 1, these may be in the "prev" string. */
9443 char_u back1 = p >= buf + 1 ? p[-1]
9444 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
9445 char_u back2 = p >= buf + 2 ? p[-2]
9446 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
9447 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
9448
9449 if (back2 == 0xef && back1 == 0xbb)
9450 {
9451 char_u *dest = p - 2;
9452
9453 /* Usually a BOM is at the beginning of a file, and so at
9454 * the beginning of a line; then we can just step over it.
9455 */
9456 if (start == dest)
9457 start = p + 1;
9458 else
9459 {
9460 /* have to shuffle buf to close gap */
9461 int adjust_prevlen = 0;
9462
9463 if (dest < buf)
9464 {
9465 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
9466 dest = buf;
9467 }
9468 if (readlen > p - buf + 1)
9469 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
9470 readlen -= 3 - adjust_prevlen;
9471 prevlen -= adjust_prevlen;
9472 p = dest - 1;
9473 }
9474 }
9475 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009476 } /* for */
9477
9478 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
9479 break;
9480 if (start < p)
9481 {
9482 /* There's part of a line in buf, store it in "prev". */
9483 if (p - start + prevlen >= prevsize)
9484 {
9485 /* need bigger "prev" buffer */
9486 char_u *newprev;
9487
9488 /* A common use case is ordinary text files and "prev" gets a
9489 * fragment of a line, so the first allocation is made
9490 * small, to avoid repeatedly 'allocing' large and
9491 * 'reallocing' small. */
9492 if (prevsize == 0)
9493 prevsize = (long)(p - start);
9494 else
9495 {
9496 long grow50pc = (prevsize * 3) / 2;
9497 long growmin = (long)((p - start) * 2 + prevlen);
9498 prevsize = grow50pc > growmin ? grow50pc : growmin;
9499 }
9500 newprev = prev == NULL ? alloc(prevsize)
9501 : vim_realloc(prev, prevsize);
9502 if (newprev == NULL)
9503 {
9504 do_outofmem_msg((long_u)prevsize);
9505 failed = TRUE;
9506 break;
9507 }
9508 prev = newprev;
9509 }
9510 /* Add the line part to end of "prev". */
9511 mch_memmove(prev + prevlen, start, p - start);
9512 prevlen += (long)(p - start);
9513 }
9514 } /* while */
9515
9516 /*
9517 * For a negative line count use only the lines at the end of the file,
9518 * free the rest.
9519 */
9520 if (!failed && maxline < 0)
9521 while (cnt > -maxline)
9522 {
9523 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
9524 --cnt;
9525 }
9526
9527 if (failed)
9528 {
9529 list_free(rettv->vval.v_list);
9530 /* readfile doc says an empty list is returned on error */
9531 rettv->vval.v_list = list_alloc();
9532 }
9533
9534 vim_free(prev);
9535 fclose(fd);
9536}
9537
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009538 static void
9539return_register(int regname, typval_T *rettv)
9540{
9541 char_u buf[2] = {0, 0};
9542
9543 buf[0] = (char_u)regname;
9544 rettv->v_type = VAR_STRING;
9545 rettv->vval.v_string = vim_strsave(buf);
9546}
9547
9548/*
9549 * "reg_executing()" function
9550 */
9551 static void
9552f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9553{
9554 return_register(reg_executing, rettv);
9555}
9556
9557/*
9558 * "reg_recording()" function
9559 */
9560 static void
9561f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9562{
9563 return_register(reg_recording, rettv);
9564}
9565
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009566#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009567/*
9568 * Convert a List to proftime_T.
9569 * Return FAIL when there is something wrong.
9570 */
9571 static int
9572list2proftime(typval_T *arg, proftime_T *tm)
9573{
9574 long n1, n2;
9575 int error = FALSE;
9576
9577 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9578 || arg->vval.v_list->lv_len != 2)
9579 return FAIL;
9580 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9581 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009582# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009583 tm->HighPart = n1;
9584 tm->LowPart = n2;
9585# else
9586 tm->tv_sec = n1;
9587 tm->tv_usec = n2;
9588# endif
9589 return error ? FAIL : OK;
9590}
9591#endif /* FEAT_RELTIME */
9592
9593/*
9594 * "reltime()" function
9595 */
9596 static void
9597f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9598{
9599#ifdef FEAT_RELTIME
9600 proftime_T res;
9601 proftime_T start;
9602
9603 if (argvars[0].v_type == VAR_UNKNOWN)
9604 {
9605 /* No arguments: get current time. */
9606 profile_start(&res);
9607 }
9608 else if (argvars[1].v_type == VAR_UNKNOWN)
9609 {
9610 if (list2proftime(&argvars[0], &res) == FAIL)
9611 return;
9612 profile_end(&res);
9613 }
9614 else
9615 {
9616 /* Two arguments: compute the difference. */
9617 if (list2proftime(&argvars[0], &start) == FAIL
9618 || list2proftime(&argvars[1], &res) == FAIL)
9619 return;
9620 profile_sub(&res, &start);
9621 }
9622
9623 if (rettv_list_alloc(rettv) == OK)
9624 {
9625 long n1, n2;
9626
Bram Moolenaar4f974752019-02-17 17:44:42 +01009627# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009628 n1 = res.HighPart;
9629 n2 = res.LowPart;
9630# else
9631 n1 = res.tv_sec;
9632 n2 = res.tv_usec;
9633# endif
9634 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9635 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9636 }
9637#endif
9638}
9639
9640#ifdef FEAT_FLOAT
9641/*
9642 * "reltimefloat()" function
9643 */
9644 static void
9645f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9646{
9647# ifdef FEAT_RELTIME
9648 proftime_T tm;
9649# endif
9650
9651 rettv->v_type = VAR_FLOAT;
9652 rettv->vval.v_float = 0;
9653# ifdef FEAT_RELTIME
9654 if (list2proftime(&argvars[0], &tm) == OK)
9655 rettv->vval.v_float = profile_float(&tm);
9656# endif
9657}
9658#endif
9659
9660/*
9661 * "reltimestr()" function
9662 */
9663 static void
9664f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9665{
9666#ifdef FEAT_RELTIME
9667 proftime_T tm;
9668#endif
9669
9670 rettv->v_type = VAR_STRING;
9671 rettv->vval.v_string = NULL;
9672#ifdef FEAT_RELTIME
9673 if (list2proftime(&argvars[0], &tm) == OK)
9674 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9675#endif
9676}
9677
9678#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009679 static void
9680make_connection(void)
9681{
9682 if (X_DISPLAY == NULL
9683# ifdef FEAT_GUI
9684 && !gui.in_use
9685# endif
9686 )
9687 {
9688 x_force_connect = TRUE;
9689 setup_term_clip();
9690 x_force_connect = FALSE;
9691 }
9692}
9693
9694 static int
9695check_connection(void)
9696{
9697 make_connection();
9698 if (X_DISPLAY == NULL)
9699 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009700 emsg(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009701 return FAIL;
9702 }
9703 return OK;
9704}
9705#endif
9706
9707#ifdef FEAT_CLIENTSERVER
9708 static void
9709remote_common(typval_T *argvars, typval_T *rettv, int expr)
9710{
9711 char_u *server_name;
9712 char_u *keys;
9713 char_u *r = NULL;
9714 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009715 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009716# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009717 HWND w;
9718# else
9719 Window w;
9720# endif
9721
9722 if (check_restricted() || check_secure())
9723 return;
9724
9725# ifdef FEAT_X11
9726 if (check_connection() == FAIL)
9727 return;
9728# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009729 if (argvars[2].v_type != VAR_UNKNOWN
9730 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009731 timeout = tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009732
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009733 server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009734 if (server_name == NULL)
9735 return; /* type error; errmsg already given */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009736 keys = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009737# ifdef MSWIN
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009738 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009739# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009740 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9741 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009742# endif
9743 {
9744 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009745 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009746 emsg((char *)r); // sending worked but evaluation failed
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009747 vim_free(r);
9748 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009749 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009750 semsg(_("E241: Unable to send to %s"), server_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009751 return;
9752 }
9753
9754 rettv->vval.v_string = r;
9755
9756 if (argvars[2].v_type != VAR_UNKNOWN)
9757 {
9758 dictitem_T v;
9759 char_u str[30];
9760 char_u *idvar;
9761
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009762 idvar = tv_get_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009763 if (idvar != NULL && *idvar != NUL)
9764 {
9765 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9766 v.di_tv.v_type = VAR_STRING;
9767 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009768 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009769 vim_free(v.di_tv.vval.v_string);
9770 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009771 }
9772}
9773#endif
9774
9775/*
9776 * "remote_expr()" function
9777 */
9778 static void
9779f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9780{
9781 rettv->v_type = VAR_STRING;
9782 rettv->vval.v_string = NULL;
9783#ifdef FEAT_CLIENTSERVER
9784 remote_common(argvars, rettv, TRUE);
9785#endif
9786}
9787
9788/*
9789 * "remote_foreground()" function
9790 */
9791 static void
9792f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9793{
9794#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +01009795# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009796 /* On Win32 it's done in this application. */
9797 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009798 char_u *server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009799
9800 if (server_name != NULL)
9801 serverForeground(server_name);
9802 }
9803# else
9804 /* Send a foreground() expression to the server. */
9805 argvars[1].v_type = VAR_STRING;
9806 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9807 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009808 rettv->v_type = VAR_STRING;
9809 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009810 remote_common(argvars, rettv, TRUE);
9811 vim_free(argvars[1].vval.v_string);
9812# endif
9813#endif
9814}
9815
9816 static void
9817f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
9818{
9819#ifdef FEAT_CLIENTSERVER
9820 dictitem_T v;
9821 char_u *s = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009822# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009823 long_u n = 0;
9824# endif
9825 char_u *serverid;
9826
9827 if (check_restricted() || check_secure())
9828 {
9829 rettv->vval.v_number = -1;
9830 return;
9831 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009832 serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009833 if (serverid == NULL)
9834 {
9835 rettv->vval.v_number = -1;
9836 return; /* type error; errmsg already given */
9837 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01009838# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009839 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
9840 if (n == 0)
9841 rettv->vval.v_number = -1;
9842 else
9843 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009844 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009845 rettv->vval.v_number = (s != NULL);
9846 }
9847# else
9848 if (check_connection() == FAIL)
9849 return;
9850
9851 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
9852 serverStrToWin(serverid), &s);
9853# endif
9854
9855 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
9856 {
9857 char_u *retvar;
9858
9859 v.di_tv.v_type = VAR_STRING;
9860 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009861 retvar = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009862 if (retvar != NULL)
9863 set_var(retvar, &v.di_tv, FALSE);
9864 vim_free(v.di_tv.vval.v_string);
9865 }
9866#else
9867 rettv->vval.v_number = -1;
9868#endif
9869}
9870
9871 static void
9872f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
9873{
9874 char_u *r = NULL;
9875
9876#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009877 char_u *serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009878
9879 if (serverid != NULL && !check_restricted() && !check_secure())
9880 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009881 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009882# ifdef MSWIN
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009883 /* The server's HWND is encoded in the 'id' parameter */
9884 long_u n = 0;
9885# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009886
9887 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009888 timeout = tv_get_number(&argvars[1]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009889
Bram Moolenaar4f974752019-02-17 17:44:42 +01009890# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009891 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
9892 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009893 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009894 if (r == NULL)
9895# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009896 if (check_connection() == FAIL
9897 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
9898 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009899# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009900 emsg(_("E277: Unable to read a server reply"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009901 }
9902#endif
9903 rettv->v_type = VAR_STRING;
9904 rettv->vval.v_string = r;
9905}
9906
9907/*
9908 * "remote_send()" function
9909 */
9910 static void
9911f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9912{
9913 rettv->v_type = VAR_STRING;
9914 rettv->vval.v_string = NULL;
9915#ifdef FEAT_CLIENTSERVER
9916 remote_common(argvars, rettv, FALSE);
9917#endif
9918}
9919
9920/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009921 * "remote_startserver()" function
9922 */
9923 static void
9924f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9925{
9926#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009927 char_u *server = tv_get_string_chk(&argvars[0]);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009928
9929 if (server == NULL)
9930 return; /* type error; errmsg already given */
9931 if (serverName != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009932 emsg(_("E941: already started a server"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009933 else
9934 {
9935# ifdef FEAT_X11
9936 if (check_connection() == OK)
9937 serverRegisterName(X_DISPLAY, server);
9938# else
9939 serverSetName(server);
9940# endif
9941 }
9942#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009943 emsg(_("E942: +clientserver feature not available"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009944#endif
9945}
9946
9947/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009948 * "remove()" function
9949 */
9950 static void
9951f_remove(typval_T *argvars, typval_T *rettv)
9952{
9953 list_T *l;
9954 listitem_T *item, *item2;
9955 listitem_T *li;
9956 long idx;
9957 long end;
9958 char_u *key;
9959 dict_T *d;
9960 dictitem_T *di;
9961 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009962 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009963
9964 if (argvars[0].v_type == VAR_DICT)
9965 {
9966 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009967 semsg(_(e_toomanyarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009968 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01009969 && !var_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009970 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009971 key = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009972 if (key != NULL)
9973 {
9974 di = dict_find(d, key, -1);
9975 if (di == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009976 semsg(_(e_dictkey), key);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009977 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
9978 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
9979 {
9980 *rettv = di->di_tv;
9981 init_tv(&di->di_tv);
9982 dictitem_remove(d, di);
9983 }
9984 }
9985 }
9986 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009987 else if (argvars[0].v_type == VAR_BLOB)
9988 {
9989 idx = (long)tv_get_number_chk(&argvars[1], &error);
9990 if (!error)
9991 {
9992 blob_T *b = argvars[0].vval.v_blob;
9993 int len = blob_len(b);
9994 char_u *p;
9995
9996 if (idx < 0)
9997 // count from the end
9998 idx = len + idx;
9999 if (idx < 0 || idx >= len)
10000 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010001 semsg(_(e_blobidx), idx);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010002 return;
10003 }
10004 if (argvars[2].v_type == VAR_UNKNOWN)
10005 {
10006 // Remove one item, return its value.
10007 p = (char_u *)b->bv_ga.ga_data;
10008 rettv->vval.v_number = (varnumber_T) *(p + idx);
10009 mch_memmove(p + idx, p + idx + 1, (size_t)len - idx - 1);
10010 --b->bv_ga.ga_len;
10011 }
10012 else
10013 {
10014 blob_T *blob;
10015
10016 // Remove range of items, return list with values.
10017 end = (long)tv_get_number_chk(&argvars[2], &error);
10018 if (error)
10019 return;
10020 if (end < 0)
10021 // count from the end
10022 end = len + end;
10023 if (end >= len || idx > end)
10024 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010025 semsg(_(e_blobidx), end);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010026 return;
10027 }
10028 blob = blob_alloc();
10029 if (blob == NULL)
10030 return;
10031 blob->bv_ga.ga_len = end - idx + 1;
10032 if (ga_grow(&blob->bv_ga, end - idx + 1) == FAIL)
10033 {
10034 vim_free(blob);
10035 return;
10036 }
10037 p = (char_u *)b->bv_ga.ga_data;
10038 mch_memmove((char_u *)blob->bv_ga.ga_data, p + idx,
10039 (size_t)(end - idx + 1));
10040 ++blob->bv_refcount;
10041 rettv->v_type = VAR_BLOB;
10042 rettv->vval.v_blob = blob;
10043
10044 mch_memmove(p + idx, p + end + 1, (size_t)(len - end));
10045 b->bv_ga.ga_len -= end - idx + 1;
10046 }
10047 }
10048 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010049 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010050 semsg(_(e_listdictblobarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010051 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010052 && !var_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010053 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010054 idx = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010055 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010056 ; // type error: do nothing, errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010057 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010058 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010059 else
10060 {
10061 if (argvars[2].v_type == VAR_UNKNOWN)
10062 {
10063 /* Remove one item, return its value. */
10064 vimlist_remove(l, item, item);
10065 *rettv = item->li_tv;
10066 vim_free(item);
10067 }
10068 else
10069 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010070 // Remove range of items, return list with values.
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010071 end = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010072 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010073 ; // type error: do nothing
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010074 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010075 semsg(_(e_listidx), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010076 else
10077 {
10078 int cnt = 0;
10079
10080 for (li = item; li != NULL; li = li->li_next)
10081 {
10082 ++cnt;
10083 if (li == item2)
10084 break;
10085 }
10086 if (li == NULL) /* didn't find "item2" after "item" */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010087 emsg(_(e_invrange));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010088 else
10089 {
10090 vimlist_remove(l, item, item2);
10091 if (rettv_list_alloc(rettv) == OK)
10092 {
10093 l = rettv->vval.v_list;
10094 l->lv_first = item;
10095 l->lv_last = item2;
10096 item->li_prev = NULL;
10097 item2->li_next = NULL;
10098 l->lv_len = cnt;
10099 }
10100 }
10101 }
10102 }
10103 }
10104 }
10105}
10106
10107/*
10108 * "rename({from}, {to})" function
10109 */
10110 static void
10111f_rename(typval_T *argvars, typval_T *rettv)
10112{
10113 char_u buf[NUMBUFLEN];
10114
10115 if (check_restricted() || check_secure())
10116 rettv->vval.v_number = -1;
10117 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010118 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
10119 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010120}
10121
10122/*
10123 * "repeat()" function
10124 */
10125 static void
10126f_repeat(typval_T *argvars, typval_T *rettv)
10127{
10128 char_u *p;
10129 int n;
10130 int slen;
10131 int len;
10132 char_u *r;
10133 int i;
10134
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010135 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010136 if (argvars[0].v_type == VAR_LIST)
10137 {
10138 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
10139 while (n-- > 0)
10140 if (list_extend(rettv->vval.v_list,
10141 argvars[0].vval.v_list, NULL) == FAIL)
10142 break;
10143 }
10144 else
10145 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010146 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010147 rettv->v_type = VAR_STRING;
10148 rettv->vval.v_string = NULL;
10149
10150 slen = (int)STRLEN(p);
10151 len = slen * n;
10152 if (len <= 0)
10153 return;
10154
10155 r = alloc(len + 1);
10156 if (r != NULL)
10157 {
10158 for (i = 0; i < n; i++)
10159 mch_memmove(r + i * slen, p, (size_t)slen);
10160 r[len] = NUL;
10161 }
10162
10163 rettv->vval.v_string = r;
10164 }
10165}
10166
10167/*
10168 * "resolve()" function
10169 */
10170 static void
10171f_resolve(typval_T *argvars, typval_T *rettv)
10172{
10173 char_u *p;
10174#ifdef HAVE_READLINK
10175 char_u *buf = NULL;
10176#endif
10177
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010178 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010179#ifdef FEAT_SHORTCUT
10180 {
10181 char_u *v = NULL;
10182
Bram Moolenaardce1e892019-02-10 23:18:53 +010010183 v = mch_resolve_path(p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010184 if (v != NULL)
10185 rettv->vval.v_string = v;
10186 else
10187 rettv->vval.v_string = vim_strsave(p);
10188 }
10189#else
10190# ifdef HAVE_READLINK
10191 {
10192 char_u *cpy;
10193 int len;
10194 char_u *remain = NULL;
10195 char_u *q;
10196 int is_relative_to_current = FALSE;
10197 int has_trailing_pathsep = FALSE;
10198 int limit = 100;
10199
10200 p = vim_strsave(p);
10201
10202 if (p[0] == '.' && (vim_ispathsep(p[1])
10203 || (p[1] == '.' && (vim_ispathsep(p[2])))))
10204 is_relative_to_current = TRUE;
10205
10206 len = STRLEN(p);
10207 if (len > 0 && after_pathsep(p, p + len))
10208 {
10209 has_trailing_pathsep = TRUE;
10210 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
10211 }
10212
10213 q = getnextcomp(p);
10214 if (*q != NUL)
10215 {
10216 /* Separate the first path component in "p", and keep the
10217 * remainder (beginning with the path separator). */
10218 remain = vim_strsave(q - 1);
10219 q[-1] = NUL;
10220 }
10221
10222 buf = alloc(MAXPATHL + 1);
10223 if (buf == NULL)
10224 goto fail;
10225
10226 for (;;)
10227 {
10228 for (;;)
10229 {
10230 len = readlink((char *)p, (char *)buf, MAXPATHL);
10231 if (len <= 0)
10232 break;
10233 buf[len] = NUL;
10234
10235 if (limit-- == 0)
10236 {
10237 vim_free(p);
10238 vim_free(remain);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010239 emsg(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010240 rettv->vval.v_string = NULL;
10241 goto fail;
10242 }
10243
10244 /* Ensure that the result will have a trailing path separator
10245 * if the argument has one. */
10246 if (remain == NULL && has_trailing_pathsep)
10247 add_pathsep(buf);
10248
10249 /* Separate the first path component in the link value and
10250 * concatenate the remainders. */
10251 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
10252 if (*q != NUL)
10253 {
10254 if (remain == NULL)
10255 remain = vim_strsave(q - 1);
10256 else
10257 {
10258 cpy = concat_str(q - 1, remain);
10259 if (cpy != NULL)
10260 {
10261 vim_free(remain);
10262 remain = cpy;
10263 }
10264 }
10265 q[-1] = NUL;
10266 }
10267
10268 q = gettail(p);
10269 if (q > p && *q == NUL)
10270 {
10271 /* Ignore trailing path separator. */
10272 q[-1] = NUL;
10273 q = gettail(p);
10274 }
10275 if (q > p && !mch_isFullName(buf))
10276 {
10277 /* symlink is relative to directory of argument */
10278 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
10279 if (cpy != NULL)
10280 {
10281 STRCPY(cpy, p);
10282 STRCPY(gettail(cpy), buf);
10283 vim_free(p);
10284 p = cpy;
10285 }
10286 }
10287 else
10288 {
10289 vim_free(p);
10290 p = vim_strsave(buf);
10291 }
10292 }
10293
10294 if (remain == NULL)
10295 break;
10296
10297 /* Append the first path component of "remain" to "p". */
10298 q = getnextcomp(remain + 1);
10299 len = q - remain - (*q != NUL);
10300 cpy = vim_strnsave(p, STRLEN(p) + len);
10301 if (cpy != NULL)
10302 {
10303 STRNCAT(cpy, remain, len);
10304 vim_free(p);
10305 p = cpy;
10306 }
10307 /* Shorten "remain". */
10308 if (*q != NUL)
10309 STRMOVE(remain, q - 1);
10310 else
Bram Moolenaard23a8232018-02-10 18:45:26 +010010311 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010312 }
10313
10314 /* If the result is a relative path name, make it explicitly relative to
10315 * the current directory if and only if the argument had this form. */
10316 if (!vim_ispathsep(*p))
10317 {
10318 if (is_relative_to_current
10319 && *p != NUL
10320 && !(p[0] == '.'
10321 && (p[1] == NUL
10322 || vim_ispathsep(p[1])
10323 || (p[1] == '.'
10324 && (p[2] == NUL
10325 || vim_ispathsep(p[2]))))))
10326 {
10327 /* Prepend "./". */
10328 cpy = concat_str((char_u *)"./", p);
10329 if (cpy != NULL)
10330 {
10331 vim_free(p);
10332 p = cpy;
10333 }
10334 }
10335 else if (!is_relative_to_current)
10336 {
10337 /* Strip leading "./". */
10338 q = p;
10339 while (q[0] == '.' && vim_ispathsep(q[1]))
10340 q += 2;
10341 if (q > p)
10342 STRMOVE(p, p + 2);
10343 }
10344 }
10345
10346 /* Ensure that the result will have no trailing path separator
10347 * if the argument had none. But keep "/" or "//". */
10348 if (!has_trailing_pathsep)
10349 {
10350 q = p + STRLEN(p);
10351 if (after_pathsep(p, q))
10352 *gettail_sep(p) = NUL;
10353 }
10354
10355 rettv->vval.v_string = p;
10356 }
10357# else
10358 rettv->vval.v_string = vim_strsave(p);
10359# endif
10360#endif
10361
10362 simplify_filename(rettv->vval.v_string);
10363
10364#ifdef HAVE_READLINK
10365fail:
10366 vim_free(buf);
10367#endif
10368 rettv->v_type = VAR_STRING;
10369}
10370
10371/*
10372 * "reverse({list})" function
10373 */
10374 static void
10375f_reverse(typval_T *argvars, typval_T *rettv)
10376{
10377 list_T *l;
10378 listitem_T *li, *ni;
10379
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010380 if (argvars[0].v_type == VAR_BLOB)
10381 {
10382 blob_T *b = argvars[0].vval.v_blob;
10383 int i, len = blob_len(b);
10384
10385 for (i = 0; i < len / 2; i++)
10386 {
10387 int tmp = blob_get(b, i);
10388
10389 blob_set(b, i, blob_get(b, len - i - 1));
10390 blob_set(b, len - i - 1, tmp);
10391 }
10392 rettv_blob_set(rettv, b);
10393 return;
10394 }
10395
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010396 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010397 semsg(_(e_listblobarg), "reverse()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010398 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010399 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010400 (char_u *)N_("reverse() argument"), TRUE))
10401 {
10402 li = l->lv_last;
10403 l->lv_first = l->lv_last = NULL;
10404 l->lv_len = 0;
10405 while (li != NULL)
10406 {
10407 ni = li->li_prev;
10408 list_append(l, li);
10409 li = ni;
10410 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020010411 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010412 l->lv_idx = l->lv_len - l->lv_idx - 1;
10413 }
10414}
10415
10416#define SP_NOMOVE 0x01 /* don't move cursor */
10417#define SP_REPEAT 0x02 /* repeat to find outer pair */
10418#define SP_RETCOUNT 0x04 /* return matchcount */
10419#define SP_SETPCMARK 0x08 /* set previous context mark */
10420#define SP_START 0x10 /* accept match at start position */
10421#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
10422#define SP_END 0x40 /* leave cursor at end of match */
10423#define SP_COLUMN 0x80 /* start at cursor column */
10424
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010425/*
10426 * Get flags for a search function.
10427 * Possibly sets "p_ws".
10428 * Returns BACKWARD, FORWARD or zero (for an error).
10429 */
10430 static int
10431get_search_arg(typval_T *varp, int *flagsp)
10432{
10433 int dir = FORWARD;
10434 char_u *flags;
10435 char_u nbuf[NUMBUFLEN];
10436 int mask;
10437
10438 if (varp->v_type != VAR_UNKNOWN)
10439 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010440 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010441 if (flags == NULL)
10442 return 0; /* type error; errmsg already given */
10443 while (*flags != NUL)
10444 {
10445 switch (*flags)
10446 {
10447 case 'b': dir = BACKWARD; break;
10448 case 'w': p_ws = TRUE; break;
10449 case 'W': p_ws = FALSE; break;
10450 default: mask = 0;
10451 if (flagsp != NULL)
10452 switch (*flags)
10453 {
10454 case 'c': mask = SP_START; break;
10455 case 'e': mask = SP_END; break;
10456 case 'm': mask = SP_RETCOUNT; break;
10457 case 'n': mask = SP_NOMOVE; break;
10458 case 'p': mask = SP_SUBPAT; break;
10459 case 'r': mask = SP_REPEAT; break;
10460 case 's': mask = SP_SETPCMARK; break;
10461 case 'z': mask = SP_COLUMN; break;
10462 }
10463 if (mask == 0)
10464 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010465 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010466 dir = 0;
10467 }
10468 else
10469 *flagsp |= mask;
10470 }
10471 if (dir == 0)
10472 break;
10473 ++flags;
10474 }
10475 }
10476 return dir;
10477}
10478
10479/*
10480 * Shared by search() and searchpos() functions.
10481 */
10482 static int
10483search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
10484{
10485 int flags;
10486 char_u *pat;
10487 pos_T pos;
10488 pos_T save_cursor;
10489 int save_p_ws = p_ws;
10490 int dir;
10491 int retval = 0; /* default: FAIL */
10492 long lnum_stop = 0;
10493 proftime_T tm;
10494#ifdef FEAT_RELTIME
10495 long time_limit = 0;
10496#endif
10497 int options = SEARCH_KEEP;
10498 int subpatnum;
10499
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010500 pat = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010501 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
10502 if (dir == 0)
10503 goto theend;
10504 flags = *flagsp;
10505 if (flags & SP_START)
10506 options |= SEARCH_START;
10507 if (flags & SP_END)
10508 options |= SEARCH_END;
10509 if (flags & SP_COLUMN)
10510 options |= SEARCH_COL;
10511
10512 /* Optional arguments: line number to stop searching and timeout. */
10513 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
10514 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010515 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010516 if (lnum_stop < 0)
10517 goto theend;
10518#ifdef FEAT_RELTIME
10519 if (argvars[3].v_type != VAR_UNKNOWN)
10520 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010521 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010522 if (time_limit < 0)
10523 goto theend;
10524 }
10525#endif
10526 }
10527
10528#ifdef FEAT_RELTIME
10529 /* Set the time limit, if there is one. */
10530 profile_setlimit(time_limit, &tm);
10531#endif
10532
10533 /*
10534 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
10535 * Check to make sure only those flags are set.
10536 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
10537 * flags cannot be set. Check for that condition also.
10538 */
10539 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
10540 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10541 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010542 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010543 goto theend;
10544 }
10545
10546 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010547 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010548 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010549 if (subpatnum != FAIL)
10550 {
10551 if (flags & SP_SUBPAT)
10552 retval = subpatnum;
10553 else
10554 retval = pos.lnum;
10555 if (flags & SP_SETPCMARK)
10556 setpcmark();
10557 curwin->w_cursor = pos;
10558 if (match_pos != NULL)
10559 {
10560 /* Store the match cursor position */
10561 match_pos->lnum = pos.lnum;
10562 match_pos->col = pos.col + 1;
10563 }
10564 /* "/$" will put the cursor after the end of the line, may need to
10565 * correct that here */
10566 check_cursor();
10567 }
10568
10569 /* If 'n' flag is used: restore cursor position. */
10570 if (flags & SP_NOMOVE)
10571 curwin->w_cursor = save_cursor;
10572 else
10573 curwin->w_set_curswant = TRUE;
10574theend:
10575 p_ws = save_p_ws;
10576
10577 return retval;
10578}
10579
10580#ifdef FEAT_FLOAT
10581
10582/*
10583 * round() is not in C90, use ceil() or floor() instead.
10584 */
10585 float_T
10586vim_round(float_T f)
10587{
10588 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
10589}
10590
10591/*
10592 * "round({float})" function
10593 */
10594 static void
10595f_round(typval_T *argvars, typval_T *rettv)
10596{
10597 float_T f = 0.0;
10598
10599 rettv->v_type = VAR_FLOAT;
10600 if (get_float_arg(argvars, &f) == OK)
10601 rettv->vval.v_float = vim_round(f);
10602 else
10603 rettv->vval.v_float = 0.0;
10604}
10605#endif
10606
Bram Moolenaare99be0e2019-03-26 22:51:09 +010010607#ifdef FEAT_RUBY
10608/*
10609 * "rubyeval()" function
10610 */
10611 static void
10612f_rubyeval(typval_T *argvars, typval_T *rettv)
10613{
10614 char_u *str;
10615 char_u buf[NUMBUFLEN];
10616
10617 str = tv_get_string_buf(&argvars[0], buf);
10618 do_rubyeval(str, rettv);
10619}
10620#endif
10621
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010622/*
10623 * "screenattr()" function
10624 */
10625 static void
10626f_screenattr(typval_T *argvars, typval_T *rettv)
10627{
10628 int row;
10629 int col;
10630 int c;
10631
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010632 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10633 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010634 if (row < 0 || row >= screen_Rows
10635 || col < 0 || col >= screen_Columns)
10636 c = -1;
10637 else
10638 c = ScreenAttrs[LineOffset[row] + col];
10639 rettv->vval.v_number = c;
10640}
10641
10642/*
10643 * "screenchar()" function
10644 */
10645 static void
10646f_screenchar(typval_T *argvars, typval_T *rettv)
10647{
10648 int row;
10649 int col;
10650 int off;
10651 int c;
10652
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010653 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10654 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010655 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010656 c = -1;
10657 else
10658 {
10659 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010660 if (enc_utf8 && ScreenLinesUC[off] != 0)
10661 c = ScreenLinesUC[off];
10662 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010663 c = ScreenLines[off];
10664 }
10665 rettv->vval.v_number = c;
10666}
10667
10668/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010669 * "screenchars()" function
10670 */
10671 static void
10672f_screenchars(typval_T *argvars, typval_T *rettv)
10673{
10674 int row;
10675 int col;
10676 int off;
10677 int c;
10678 int i;
10679
10680 if (rettv_list_alloc(rettv) == FAIL)
10681 return;
10682 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10683 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10684 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10685 return;
10686
10687 off = LineOffset[row] + col;
10688 if (enc_utf8 && ScreenLinesUC[off] != 0)
10689 c = ScreenLinesUC[off];
10690 else
10691 c = ScreenLines[off];
10692 list_append_number(rettv->vval.v_list, (varnumber_T)c);
10693
10694 if (enc_utf8)
10695
10696 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10697 list_append_number(rettv->vval.v_list,
10698 (varnumber_T)ScreenLinesC[i][off]);
10699}
10700
10701/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010702 * "screencol()" function
10703 *
10704 * First column is 1 to be consistent with virtcol().
10705 */
10706 static void
10707f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10708{
10709 rettv->vval.v_number = screen_screencol() + 1;
10710}
10711
10712/*
10713 * "screenrow()" function
10714 */
10715 static void
10716f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10717{
10718 rettv->vval.v_number = screen_screenrow() + 1;
10719}
10720
10721/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010722 * "screenstring()" function
10723 */
10724 static void
10725f_screenstring(typval_T *argvars, typval_T *rettv)
10726{
10727 int row;
10728 int col;
10729 int off;
10730 int c;
10731 int i;
10732 char_u buf[MB_MAXBYTES + 1];
10733 int buflen = 0;
10734
10735 rettv->vval.v_string = NULL;
10736 rettv->v_type = VAR_STRING;
10737
10738 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10739 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10740 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10741 return;
10742
10743 off = LineOffset[row] + col;
10744 if (enc_utf8 && ScreenLinesUC[off] != 0)
10745 c = ScreenLinesUC[off];
10746 else
10747 c = ScreenLines[off];
10748 buflen += mb_char2bytes(c, buf);
10749
10750 if (enc_utf8)
10751 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10752 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
10753
10754 buf[buflen] = NUL;
10755 rettv->vval.v_string = vim_strsave(buf);
10756}
10757
10758/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010759 * "search()" function
10760 */
10761 static void
10762f_search(typval_T *argvars, typval_T *rettv)
10763{
10764 int flags = 0;
10765
10766 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10767}
10768
10769/*
10770 * "searchdecl()" function
10771 */
10772 static void
10773f_searchdecl(typval_T *argvars, typval_T *rettv)
10774{
10775 int locally = 1;
10776 int thisblock = 0;
10777 int error = FALSE;
10778 char_u *name;
10779
10780 rettv->vval.v_number = 1; /* default: FAIL */
10781
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010782 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010783 if (argvars[1].v_type != VAR_UNKNOWN)
10784 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010785 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010786 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010787 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010788 }
10789 if (!error && name != NULL)
10790 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10791 locally, thisblock, SEARCH_KEEP) == FAIL;
10792}
10793
10794/*
10795 * Used by searchpair() and searchpairpos()
10796 */
10797 static int
10798searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10799{
10800 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010801 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010802 int save_p_ws = p_ws;
10803 int dir;
10804 int flags = 0;
10805 char_u nbuf1[NUMBUFLEN];
10806 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010807 int retval = 0; /* default: FAIL */
10808 long lnum_stop = 0;
10809 long time_limit = 0;
10810
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010811 /* Get the three pattern arguments: start, middle, end. Will result in an
10812 * error if not a valid argument. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010813 spat = tv_get_string_chk(&argvars[0]);
10814 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
10815 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010816 if (spat == NULL || mpat == NULL || epat == NULL)
10817 goto theend; /* type error */
10818
10819 /* Handle the optional fourth argument: flags */
10820 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
10821 if (dir == 0)
10822 goto theend;
10823
10824 /* Don't accept SP_END or SP_SUBPAT.
10825 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
10826 */
10827 if ((flags & (SP_END | SP_SUBPAT)) != 0
10828 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10829 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010830 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010831 goto theend;
10832 }
10833
10834 /* Using 'r' implies 'W', otherwise it doesn't work. */
10835 if (flags & SP_REPEAT)
10836 p_ws = FALSE;
10837
10838 /* Optional fifth argument: skip expression */
10839 if (argvars[3].v_type == VAR_UNKNOWN
10840 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010010841 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010842 else
10843 {
Bram Moolenaar48570482017-10-30 21:48:41 +010010844 skip = &argvars[4];
10845 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
10846 && skip->v_type != VAR_STRING)
10847 {
10848 /* Type error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010849 semsg(_(e_invarg2), tv_get_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010010850 goto theend;
10851 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010852 if (argvars[5].v_type != VAR_UNKNOWN)
10853 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010854 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010855 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010856 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010857 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010858 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010859 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010860#ifdef FEAT_RELTIME
10861 if (argvars[6].v_type != VAR_UNKNOWN)
10862 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010863 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010864 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010865 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010866 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010867 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010868 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010869 }
10870#endif
10871 }
10872 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010873
10874 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
10875 match_pos, lnum_stop, time_limit);
10876
10877theend:
10878 p_ws = save_p_ws;
10879
10880 return retval;
10881}
10882
10883/*
10884 * "searchpair()" function
10885 */
10886 static void
10887f_searchpair(typval_T *argvars, typval_T *rettv)
10888{
10889 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
10890}
10891
10892/*
10893 * "searchpairpos()" function
10894 */
10895 static void
10896f_searchpairpos(typval_T *argvars, typval_T *rettv)
10897{
10898 pos_T match_pos;
10899 int lnum = 0;
10900 int col = 0;
10901
10902 if (rettv_list_alloc(rettv) == FAIL)
10903 return;
10904
10905 if (searchpair_cmn(argvars, &match_pos) > 0)
10906 {
10907 lnum = match_pos.lnum;
10908 col = match_pos.col;
10909 }
10910
10911 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10912 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10913}
10914
10915/*
10916 * Search for a start/middle/end thing.
10917 * Used by searchpair(), see its documentation for the details.
10918 * Returns 0 or -1 for no match,
10919 */
10920 long
10921do_searchpair(
10922 char_u *spat, /* start pattern */
10923 char_u *mpat, /* middle pattern */
10924 char_u *epat, /* end pattern */
10925 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010010926 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010927 int flags, /* SP_SETPCMARK and other SP_ values */
10928 pos_T *match_pos,
10929 linenr_T lnum_stop, /* stop at this line if not zero */
10930 long time_limit UNUSED) /* stop after this many msec */
10931{
10932 char_u *save_cpo;
10933 char_u *pat, *pat2 = NULL, *pat3 = NULL;
10934 long retval = 0;
10935 pos_T pos;
10936 pos_T firstpos;
10937 pos_T foundpos;
10938 pos_T save_cursor;
10939 pos_T save_pos;
10940 int n;
10941 int r;
10942 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010010943 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010944 int err;
10945 int options = SEARCH_KEEP;
10946 proftime_T tm;
10947
10948 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10949 save_cpo = p_cpo;
10950 p_cpo = empty_option;
10951
10952#ifdef FEAT_RELTIME
10953 /* Set the time limit, if there is one. */
10954 profile_setlimit(time_limit, &tm);
10955#endif
10956
10957 /* Make two search patterns: start/end (pat2, for in nested pairs) and
10958 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010959 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
10960 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010961 if (pat2 == NULL || pat3 == NULL)
10962 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010963 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010964 if (*mpat == NUL)
10965 STRCPY(pat3, pat2);
10966 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010967 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010968 spat, epat, mpat);
10969 if (flags & SP_START)
10970 options |= SEARCH_START;
10971
Bram Moolenaar48570482017-10-30 21:48:41 +010010972 if (skip != NULL)
10973 {
10974 /* Empty string means to not use the skip expression. */
10975 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
10976 use_skip = skip->vval.v_string != NULL
10977 && *skip->vval.v_string != NUL;
10978 }
10979
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010980 save_cursor = curwin->w_cursor;
10981 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010982 CLEAR_POS(&firstpos);
10983 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010984 pat = pat3;
10985 for (;;)
10986 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010987 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010988 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010989 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010990 /* didn't find it or found the first match again: FAIL */
10991 break;
10992
10993 if (firstpos.lnum == 0)
10994 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010995 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010996 {
10997 /* Found the same position again. Can happen with a pattern that
10998 * has "\zs" at the end and searching backwards. Advance one
10999 * character and try again. */
11000 if (dir == BACKWARD)
11001 decl(&pos);
11002 else
11003 incl(&pos);
11004 }
11005 foundpos = pos;
11006
11007 /* clear the start flag to avoid getting stuck here */
11008 options &= ~SEARCH_START;
11009
11010 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010011011 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011012 {
11013 save_pos = curwin->w_cursor;
11014 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010011015 err = FALSE;
11016 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011017 curwin->w_cursor = save_pos;
11018 if (err)
11019 {
11020 /* Evaluating {skip} caused an error, break here. */
11021 curwin->w_cursor = save_cursor;
11022 retval = -1;
11023 break;
11024 }
11025 if (r)
11026 continue;
11027 }
11028
11029 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
11030 {
11031 /* Found end when searching backwards or start when searching
11032 * forward: nested pair. */
11033 ++nest;
11034 pat = pat2; /* nested, don't search for middle */
11035 }
11036 else
11037 {
11038 /* Found end when searching forward or start when searching
11039 * backward: end of (nested) pair; or found middle in outer pair. */
11040 if (--nest == 1)
11041 pat = pat3; /* outer level, search for middle */
11042 }
11043
11044 if (nest == 0)
11045 {
11046 /* Found the match: return matchcount or line number. */
11047 if (flags & SP_RETCOUNT)
11048 ++retval;
11049 else
11050 retval = pos.lnum;
11051 if (flags & SP_SETPCMARK)
11052 setpcmark();
11053 curwin->w_cursor = pos;
11054 if (!(flags & SP_REPEAT))
11055 break;
11056 nest = 1; /* search for next unmatched */
11057 }
11058 }
11059
11060 if (match_pos != NULL)
11061 {
11062 /* Store the match cursor position */
11063 match_pos->lnum = curwin->w_cursor.lnum;
11064 match_pos->col = curwin->w_cursor.col + 1;
11065 }
11066
11067 /* If 'n' flag is used or search failed: restore cursor position. */
11068 if ((flags & SP_NOMOVE) || retval == 0)
11069 curwin->w_cursor = save_cursor;
11070
11071theend:
11072 vim_free(pat2);
11073 vim_free(pat3);
11074 if (p_cpo == empty_option)
11075 p_cpo = save_cpo;
11076 else
11077 /* Darn, evaluating the {skip} expression changed the value. */
11078 free_string_option(save_cpo);
11079
11080 return retval;
11081}
11082
11083/*
11084 * "searchpos()" function
11085 */
11086 static void
11087f_searchpos(typval_T *argvars, typval_T *rettv)
11088{
11089 pos_T match_pos;
11090 int lnum = 0;
11091 int col = 0;
11092 int n;
11093 int flags = 0;
11094
11095 if (rettv_list_alloc(rettv) == FAIL)
11096 return;
11097
11098 n = search_cmn(argvars, &match_pos, &flags);
11099 if (n > 0)
11100 {
11101 lnum = match_pos.lnum;
11102 col = match_pos.col;
11103 }
11104
11105 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
11106 list_append_number(rettv->vval.v_list, (varnumber_T)col);
11107 if (flags & SP_SUBPAT)
11108 list_append_number(rettv->vval.v_list, (varnumber_T)n);
11109}
11110
11111 static void
11112f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
11113{
11114#ifdef FEAT_CLIENTSERVER
11115 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011116 char_u *server = tv_get_string_chk(&argvars[0]);
11117 char_u *reply = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011118
11119 rettv->vval.v_number = -1;
11120 if (server == NULL || reply == NULL)
11121 return;
11122 if (check_restricted() || check_secure())
11123 return;
11124# ifdef FEAT_X11
11125 if (check_connection() == FAIL)
11126 return;
11127# endif
11128
11129 if (serverSendReply(server, reply) < 0)
11130 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011131 emsg(_("E258: Unable to send to client"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011132 return;
11133 }
11134 rettv->vval.v_number = 0;
11135#else
11136 rettv->vval.v_number = -1;
11137#endif
11138}
11139
11140 static void
11141f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
11142{
11143 char_u *r = NULL;
11144
11145#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +010011146# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011147 r = serverGetVimNames();
11148# else
11149 make_connection();
11150 if (X_DISPLAY != NULL)
11151 r = serverGetVimNames(X_DISPLAY);
11152# endif
11153#endif
11154 rettv->v_type = VAR_STRING;
11155 rettv->vval.v_string = r;
11156}
11157
11158/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011159 * "setbufline()" function
11160 */
11161 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020011162f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011163{
11164 linenr_T lnum;
11165 buf_T *buf;
11166
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011167 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011168 if (buf == NULL)
11169 rettv->vval.v_number = 1; /* FAIL */
11170 else
11171 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011172 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020011173 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011174 }
11175}
11176
11177/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011178 * "setbufvar()" function
11179 */
11180 static void
11181f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
11182{
11183 buf_T *buf;
11184 char_u *varname, *bufvarname;
11185 typval_T *varp;
11186 char_u nbuf[NUMBUFLEN];
11187
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011188 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011189 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011190 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
11191 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011192 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011193 varp = &argvars[2];
11194
11195 if (buf != NULL && varname != NULL && varp != NULL)
11196 {
11197 if (*varname == '&')
11198 {
11199 long numval;
11200 char_u *strval;
11201 int error = FALSE;
11202 aco_save_T aco;
11203
11204 /* set curbuf to be our buf, temporarily */
11205 aucmd_prepbuf(&aco, buf);
11206
11207 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011208 numval = (long)tv_get_number_chk(varp, &error);
11209 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011210 if (!error && strval != NULL)
11211 set_option_value(varname, numval, strval, OPT_LOCAL);
11212
11213 /* reset notion of buffer */
11214 aucmd_restbuf(&aco);
11215 }
11216 else
11217 {
11218 buf_T *save_curbuf = curbuf;
11219
11220 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
11221 if (bufvarname != NULL)
11222 {
11223 curbuf = buf;
11224 STRCPY(bufvarname, "b:");
11225 STRCPY(bufvarname + 2, varname);
11226 set_var(bufvarname, varp, TRUE);
11227 vim_free(bufvarname);
11228 curbuf = save_curbuf;
11229 }
11230 }
11231 }
11232}
11233
11234 static void
11235f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
11236{
11237 dict_T *d;
11238 dictitem_T *di;
11239 char_u *csearch;
11240
11241 if (argvars[0].v_type != VAR_DICT)
11242 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011243 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011244 return;
11245 }
11246
11247 if ((d = argvars[0].vval.v_dict) != NULL)
11248 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010011249 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011250 if (csearch != NULL)
11251 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011252 if (enc_utf8)
11253 {
11254 int pcc[MAX_MCO];
11255 int c = utfc_ptr2char(csearch, pcc);
11256
11257 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
11258 }
11259 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011260 set_last_csearch(PTR2CHAR(csearch),
11261 csearch, MB_PTR2LEN(csearch));
11262 }
11263
11264 di = dict_find(d, (char_u *)"forward", -1);
11265 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011266 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011267 ? FORWARD : BACKWARD);
11268
11269 di = dict_find(d, (char_u *)"until", -1);
11270 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011271 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011272 }
11273}
11274
11275/*
11276 * "setcmdpos()" function
11277 */
11278 static void
11279f_setcmdpos(typval_T *argvars, typval_T *rettv)
11280{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011281 int pos = (int)tv_get_number(&argvars[0]) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011282
11283 if (pos >= 0)
11284 rettv->vval.v_number = set_cmdline_pos(pos);
11285}
11286
11287/*
11288 * "setfperm({fname}, {mode})" function
11289 */
11290 static void
11291f_setfperm(typval_T *argvars, typval_T *rettv)
11292{
11293 char_u *fname;
11294 char_u modebuf[NUMBUFLEN];
11295 char_u *mode_str;
11296 int i;
11297 int mask;
11298 int mode = 0;
11299
11300 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011301 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011302 if (fname == NULL)
11303 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011304 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011305 if (mode_str == NULL)
11306 return;
11307 if (STRLEN(mode_str) != 9)
11308 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011309 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011310 return;
11311 }
11312
11313 mask = 1;
11314 for (i = 8; i >= 0; --i)
11315 {
11316 if (mode_str[i] != '-')
11317 mode |= mask;
11318 mask = mask << 1;
11319 }
11320 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
11321}
11322
11323/*
11324 * "setline()" function
11325 */
11326 static void
11327f_setline(typval_T *argvars, typval_T *rettv)
11328{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011329 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011330
Bram Moolenaarca851592018-06-06 21:04:07 +020011331 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011332}
11333
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011334/*
11335 * Used by "setqflist()" and "setloclist()" functions
11336 */
11337 static void
11338set_qf_ll_list(
11339 win_T *wp UNUSED,
11340 typval_T *list_arg UNUSED,
11341 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020011342 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011343 typval_T *rettv)
11344{
11345#ifdef FEAT_QUICKFIX
11346 static char *e_invact = N_("E927: Invalid action: '%s'");
11347 char_u *act;
11348 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011349 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011350#endif
11351
11352 rettv->vval.v_number = -1;
11353
11354#ifdef FEAT_QUICKFIX
11355 if (list_arg->v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011356 emsg(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011357 else if (recursive != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011358 emsg(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011359 else
11360 {
11361 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011362 dict_T *d = NULL;
11363 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011364
11365 if (action_arg->v_type == VAR_STRING)
11366 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011367 act = tv_get_string_chk(action_arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011368 if (act == NULL)
11369 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020011370 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
11371 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011372 action = *act;
11373 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011374 semsg(_(e_invact), act);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011375 }
11376 else if (action_arg->v_type == VAR_UNKNOWN)
11377 action = ' ';
11378 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011379 emsg(_(e_stringreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011380
Bram Moolenaard823fa92016-08-12 16:29:27 +020011381 if (action_arg->v_type != VAR_UNKNOWN
11382 && what_arg->v_type != VAR_UNKNOWN)
11383 {
11384 if (what_arg->v_type == VAR_DICT)
11385 d = what_arg->vval.v_dict;
11386 else
11387 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011388 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +020011389 valid_dict = FALSE;
11390 }
11391 }
11392
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011393 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011394 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011395 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
11396 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011397 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011398 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011399 }
11400#endif
11401}
11402
11403/*
11404 * "setloclist()" function
11405 */
11406 static void
11407f_setloclist(typval_T *argvars, typval_T *rettv)
11408{
11409 win_T *win;
11410
11411 rettv->vval.v_number = -1;
11412
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020011413 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011414 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020011415 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011416}
11417
11418/*
11419 * "setmatches()" function
11420 */
11421 static void
11422f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
11423{
11424#ifdef FEAT_SEARCH_EXTRA
11425 list_T *l;
11426 listitem_T *li;
11427 dict_T *d;
11428 list_T *s = NULL;
Bram Moolenaaraff74912019-03-30 18:11:49 +010011429 win_T *win = get_optional_window(argvars, 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011430
11431 rettv->vval.v_number = -1;
11432 if (argvars[0].v_type != VAR_LIST)
11433 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011434 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011435 return;
11436 }
Bram Moolenaaraff74912019-03-30 18:11:49 +010011437 if (win == NULL)
11438 return;
11439
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011440 if ((l = argvars[0].vval.v_list) != NULL)
11441 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011442 /* To some extent make sure that we are dealing with a list from
11443 * "getmatches()". */
11444 li = l->lv_first;
11445 while (li != NULL)
11446 {
11447 if (li->li_tv.v_type != VAR_DICT
11448 || (d = li->li_tv.vval.v_dict) == NULL)
11449 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011450 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011451 return;
11452 }
11453 if (!(dict_find(d, (char_u *)"group", -1) != NULL
11454 && (dict_find(d, (char_u *)"pattern", -1) != NULL
11455 || dict_find(d, (char_u *)"pos1", -1) != NULL)
11456 && dict_find(d, (char_u *)"priority", -1) != NULL
11457 && dict_find(d, (char_u *)"id", -1) != NULL))
11458 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011459 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011460 return;
11461 }
11462 li = li->li_next;
11463 }
11464
Bram Moolenaaraff74912019-03-30 18:11:49 +010011465 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011466 li = l->lv_first;
11467 while (li != NULL)
11468 {
11469 int i = 0;
11470 char_u buf[5];
11471 dictitem_T *di;
11472 char_u *group;
11473 int priority;
11474 int id;
11475 char_u *conceal;
11476
11477 d = li->li_tv.vval.v_dict;
11478 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
11479 {
11480 if (s == NULL)
11481 {
11482 s = list_alloc();
11483 if (s == NULL)
11484 return;
11485 }
11486
11487 /* match from matchaddpos() */
11488 for (i = 1; i < 9; i++)
11489 {
11490 sprintf((char *)buf, (char *)"pos%d", i);
11491 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
11492 {
11493 if (di->di_tv.v_type != VAR_LIST)
11494 return;
11495
11496 list_append_tv(s, &di->di_tv);
11497 s->lv_refcount++;
11498 }
11499 else
11500 break;
11501 }
11502 }
11503
Bram Moolenaar8f667172018-12-14 15:38:31 +010011504 group = dict_get_string(d, (char_u *)"group", TRUE);
11505 priority = (int)dict_get_number(d, (char_u *)"priority");
11506 id = (int)dict_get_number(d, (char_u *)"id");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011507 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar8f667172018-12-14 15:38:31 +010011508 ? dict_get_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011509 : NULL;
11510 if (i == 0)
11511 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011512 match_add(win, group,
Bram Moolenaar8f667172018-12-14 15:38:31 +010011513 dict_get_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011514 priority, id, NULL, conceal);
11515 }
11516 else
11517 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011518 match_add(win, group, NULL, priority, id, s, conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011519 list_unref(s);
11520 s = NULL;
11521 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020011522 vim_free(group);
11523 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011524
11525 li = li->li_next;
11526 }
11527 rettv->vval.v_number = 0;
11528 }
11529#endif
11530}
11531
11532/*
11533 * "setpos()" function
11534 */
11535 static void
11536f_setpos(typval_T *argvars, typval_T *rettv)
11537{
11538 pos_T pos;
11539 int fnum;
11540 char_u *name;
11541 colnr_T curswant = -1;
11542
11543 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011544 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011545 if (name != NULL)
11546 {
11547 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
11548 {
11549 if (--pos.col < 0)
11550 pos.col = 0;
11551 if (name[0] == '.' && name[1] == NUL)
11552 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011553 /* set cursor; "fnum" is ignored */
11554 curwin->w_cursor = pos;
11555 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011556 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011557 curwin->w_curswant = curswant - 1;
11558 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011559 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011560 check_cursor();
11561 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011562 }
11563 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
11564 {
11565 /* set mark */
11566 if (setmark_pos(name[1], &pos, fnum) == OK)
11567 rettv->vval.v_number = 0;
11568 }
11569 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011570 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011571 }
11572 }
11573}
11574
11575/*
11576 * "setqflist()" function
11577 */
11578 static void
11579f_setqflist(typval_T *argvars, typval_T *rettv)
11580{
Bram Moolenaard823fa92016-08-12 16:29:27 +020011581 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011582}
11583
11584/*
11585 * "setreg()" function
11586 */
11587 static void
11588f_setreg(typval_T *argvars, typval_T *rettv)
11589{
11590 int regname;
11591 char_u *strregname;
11592 char_u *stropt;
11593 char_u *strval;
11594 int append;
11595 char_u yank_type;
11596 long block_len;
11597
11598 block_len = -1;
11599 yank_type = MAUTO;
11600 append = FALSE;
11601
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011602 strregname = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011603 rettv->vval.v_number = 1; /* FAIL is default */
11604
11605 if (strregname == NULL)
11606 return; /* type error; errmsg already given */
11607 regname = *strregname;
11608 if (regname == 0 || regname == '@')
11609 regname = '"';
11610
11611 if (argvars[2].v_type != VAR_UNKNOWN)
11612 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011613 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011614 if (stropt == NULL)
11615 return; /* type error */
11616 for (; *stropt != NUL; ++stropt)
11617 switch (*stropt)
11618 {
11619 case 'a': case 'A': /* append */
11620 append = TRUE;
11621 break;
11622 case 'v': case 'c': /* character-wise selection */
11623 yank_type = MCHAR;
11624 break;
11625 case 'V': case 'l': /* line-wise selection */
11626 yank_type = MLINE;
11627 break;
11628 case 'b': case Ctrl_V: /* block-wise selection */
11629 yank_type = MBLOCK;
11630 if (VIM_ISDIGIT(stropt[1]))
11631 {
11632 ++stropt;
11633 block_len = getdigits(&stropt) - 1;
11634 --stropt;
11635 }
11636 break;
11637 }
11638 }
11639
11640 if (argvars[1].v_type == VAR_LIST)
11641 {
11642 char_u **lstval;
11643 char_u **allocval;
11644 char_u buf[NUMBUFLEN];
11645 char_u **curval;
11646 char_u **curallocval;
11647 list_T *ll = argvars[1].vval.v_list;
11648 listitem_T *li;
11649 int len;
11650
11651 /* If the list is NULL handle like an empty list. */
11652 len = ll == NULL ? 0 : ll->lv_len;
11653
11654 /* First half: use for pointers to result lines; second half: use for
11655 * pointers to allocated copies. */
11656 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
11657 if (lstval == NULL)
11658 return;
11659 curval = lstval;
11660 allocval = lstval + len + 2;
11661 curallocval = allocval;
11662
11663 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
11664 li = li->li_next)
11665 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011666 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011667 if (strval == NULL)
11668 goto free_lstval;
11669 if (strval == buf)
11670 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011671 /* Need to make a copy, next tv_get_string_buf_chk() will
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011672 * overwrite the string. */
11673 strval = vim_strsave(buf);
11674 if (strval == NULL)
11675 goto free_lstval;
11676 *curallocval++ = strval;
11677 }
11678 *curval++ = strval;
11679 }
11680 *curval++ = NULL;
11681
11682 write_reg_contents_lst(regname, lstval, -1,
11683 append, yank_type, block_len);
11684free_lstval:
11685 while (curallocval > allocval)
11686 vim_free(*--curallocval);
11687 vim_free(lstval);
11688 }
11689 else
11690 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011691 strval = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011692 if (strval == NULL)
11693 return;
11694 write_reg_contents_ex(regname, strval, -1,
11695 append, yank_type, block_len);
11696 }
11697 rettv->vval.v_number = 0;
11698}
11699
11700/*
11701 * "settabvar()" function
11702 */
11703 static void
11704f_settabvar(typval_T *argvars, typval_T *rettv)
11705{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011706 tabpage_T *save_curtab;
11707 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011708 char_u *varname, *tabvarname;
11709 typval_T *varp;
11710
11711 rettv->vval.v_number = 0;
11712
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011713 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011714 return;
11715
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011716 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
11717 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011718 varp = &argvars[2];
11719
Bram Moolenaar4033c552017-09-16 20:54:51 +020011720 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011721 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011722 save_curtab = curtab;
11723 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011724
11725 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
11726 if (tabvarname != NULL)
11727 {
11728 STRCPY(tabvarname, "t:");
11729 STRCPY(tabvarname + 2, varname);
11730 set_var(tabvarname, varp, TRUE);
11731 vim_free(tabvarname);
11732 }
11733
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011734 /* Restore current tabpage */
11735 if (valid_tabpage(save_curtab))
11736 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011737 }
11738}
11739
11740/*
11741 * "settabwinvar()" function
11742 */
11743 static void
11744f_settabwinvar(typval_T *argvars, typval_T *rettv)
11745{
11746 setwinvar(argvars, rettv, 1);
11747}
11748
11749/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011750 * "settagstack()" function
11751 */
11752 static void
11753f_settagstack(typval_T *argvars, typval_T *rettv)
11754{
11755 static char *e_invact2 = N_("E962: Invalid action: '%s'");
11756 win_T *wp;
11757 dict_T *d;
11758 int action = 'r';
11759
11760 rettv->vval.v_number = -1;
11761
11762 // first argument: window number or id
11763 wp = find_win_by_nr_or_id(&argvars[0]);
11764 if (wp == NULL)
11765 return;
11766
11767 // second argument: dict with items to set in the tag stack
11768 if (argvars[1].v_type != VAR_DICT)
11769 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011770 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011771 return;
11772 }
11773 d = argvars[1].vval.v_dict;
11774 if (d == NULL)
11775 return;
11776
11777 // third argument: action - 'a' for append and 'r' for replace.
11778 // default is to replace the stack.
11779 if (argvars[2].v_type == VAR_UNKNOWN)
11780 action = 'r';
11781 else if (argvars[2].v_type == VAR_STRING)
11782 {
11783 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011784 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011785 if (actstr == NULL)
11786 return;
11787 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
11788 action = *actstr;
11789 else
11790 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011791 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011792 return;
11793 }
11794 }
11795 else
11796 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011797 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011798 return;
11799 }
11800
11801 if (set_tagstack(wp, d, action) == OK)
11802 rettv->vval.v_number = 0;
11803}
11804
11805/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011806 * "setwinvar()" function
11807 */
11808 static void
11809f_setwinvar(typval_T *argvars, typval_T *rettv)
11810{
11811 setwinvar(argvars, rettv, 0);
11812}
11813
11814#ifdef FEAT_CRYPT
11815/*
11816 * "sha256({string})" function
11817 */
11818 static void
11819f_sha256(typval_T *argvars, typval_T *rettv)
11820{
11821 char_u *p;
11822
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011823 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011824 rettv->vval.v_string = vim_strsave(
11825 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
11826 rettv->v_type = VAR_STRING;
11827}
11828#endif /* FEAT_CRYPT */
11829
11830/*
11831 * "shellescape({string})" function
11832 */
11833 static void
11834f_shellescape(typval_T *argvars, typval_T *rettv)
11835{
Bram Moolenaar20615522017-06-05 18:46:26 +020011836 int do_special = non_zero_arg(&argvars[1]);
11837
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011838 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011839 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011840 rettv->v_type = VAR_STRING;
11841}
11842
11843/*
11844 * shiftwidth() function
11845 */
11846 static void
11847f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
11848{
Bram Moolenaarf9514162018-11-22 03:08:29 +010011849 rettv->vval.v_number = 0;
11850
11851 if (argvars[0].v_type != VAR_UNKNOWN)
11852 {
11853 long col;
11854
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011855 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +010011856 if (col < 0)
11857 return; // type error; errmsg already given
11858#ifdef FEAT_VARTABS
11859 rettv->vval.v_number = get_sw_value_col(curbuf, col);
11860 return;
11861#endif
11862 }
11863
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011864 rettv->vval.v_number = get_sw_value(curbuf);
11865}
11866
Bram Moolenaar162b7142018-12-21 15:17:36 +010011867#ifdef FEAT_SIGNS
11868/*
11869 * "sign_define()" function
11870 */
11871 static void
11872f_sign_define(typval_T *argvars, typval_T *rettv)
11873{
11874 char_u *name;
11875 dict_T *dict;
11876 char_u *icon = NULL;
11877 char_u *linehl = NULL;
11878 char_u *text = NULL;
11879 char_u *texthl = NULL;
11880
11881 rettv->vval.v_number = -1;
11882
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011883 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011884 if (name == NULL)
11885 return;
11886
11887 if (argvars[1].v_type != VAR_UNKNOWN)
11888 {
11889 if (argvars[1].v_type != VAR_DICT)
11890 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011891 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011892 return;
11893 }
11894
11895 // sign attributes
11896 dict = argvars[1].vval.v_dict;
11897 if (dict_find(dict, (char_u *)"icon", -1) != NULL)
11898 icon = dict_get_string(dict, (char_u *)"icon", TRUE);
11899 if (dict_find(dict, (char_u *)"linehl", -1) != NULL)
11900 linehl = dict_get_string(dict, (char_u *)"linehl", TRUE);
11901 if (dict_find(dict, (char_u *)"text", -1) != NULL)
11902 text = dict_get_string(dict, (char_u *)"text", TRUE);
11903 if (dict_find(dict, (char_u *)"texthl", -1) != NULL)
11904 texthl = dict_get_string(dict, (char_u *)"texthl", TRUE);
11905 }
11906
11907 if (sign_define_by_name(name, icon, linehl, text, texthl) == OK)
11908 rettv->vval.v_number = 0;
11909
11910 vim_free(icon);
11911 vim_free(linehl);
11912 vim_free(text);
11913 vim_free(texthl);
11914}
11915
11916/*
11917 * "sign_getdefined()" function
11918 */
11919 static void
11920f_sign_getdefined(typval_T *argvars, typval_T *rettv)
11921{
11922 char_u *name = NULL;
11923
11924 if (rettv_list_alloc_id(rettv, aid_sign_getdefined) != OK)
11925 return;
11926
11927 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011928 name = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011929
11930 sign_getlist(name, rettv->vval.v_list);
11931}
11932
11933/*
11934 * "sign_getplaced()" function
11935 */
11936 static void
11937f_sign_getplaced(typval_T *argvars, typval_T *rettv)
11938{
11939 buf_T *buf = NULL;
11940 dict_T *dict;
11941 dictitem_T *di;
11942 linenr_T lnum = 0;
11943 int sign_id = 0;
11944 char_u *group = NULL;
11945 int notanum = FALSE;
11946
11947 if (rettv_list_alloc_id(rettv, aid_sign_getplaced) != OK)
11948 return;
11949
11950 if (argvars[0].v_type != VAR_UNKNOWN)
11951 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010011952 // get signs placed in the specified buffer
11953 buf = get_buf_arg(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011954 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010011955 return;
Bram Moolenaar162b7142018-12-21 15:17:36 +010011956
11957 if (argvars[1].v_type != VAR_UNKNOWN)
11958 {
11959 if (argvars[1].v_type != VAR_DICT ||
11960 ((dict = argvars[1].vval.v_dict) == NULL))
11961 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011962 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011963 return;
11964 }
11965 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
11966 {
11967 // get signs placed at this line
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011968 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011969 if (notanum)
11970 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011971 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011972 }
11973 if ((di = dict_find(dict, (char_u *)"id", -1)) != NULL)
11974 {
11975 // get sign placed with this identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011976 sign_id = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011977 if (notanum)
11978 return;
11979 }
11980 if ((di = dict_find(dict, (char_u *)"group", -1)) != NULL)
11981 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011982 group = tv_get_string_chk(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011983 if (group == NULL)
11984 return;
Bram Moolenaar6436cd82018-12-27 00:28:33 +010011985 if (*group == '\0') // empty string means global group
11986 group = NULL;
Bram Moolenaar162b7142018-12-21 15:17:36 +010011987 }
11988 }
11989 }
11990
11991 sign_get_placed(buf, lnum, sign_id, group, rettv->vval.v_list);
11992}
11993
11994/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010011995 * "sign_jump()" function
11996 */
11997 static void
11998f_sign_jump(typval_T *argvars, typval_T *rettv)
11999{
12000 int sign_id;
12001 char_u *sign_group = NULL;
12002 buf_T *buf;
12003 int notanum = FALSE;
12004
12005 rettv->vval.v_number = -1;
12006
Bram Moolenaarbdace832019-03-02 10:13:42 +010012007 // Sign identifier
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012008 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
12009 if (notanum)
12010 return;
12011 if (sign_id <= 0)
12012 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012013 emsg(_(e_invarg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012014 return;
12015 }
12016
12017 // Sign group
12018 sign_group = tv_get_string_chk(&argvars[1]);
12019 if (sign_group == NULL)
12020 return;
12021 if (sign_group[0] == '\0')
12022 sign_group = NULL; // global sign group
12023 else
12024 {
12025 sign_group = vim_strsave(sign_group);
12026 if (sign_group == NULL)
12027 return;
12028 }
12029
12030 // Buffer to place the sign
12031 buf = get_buf_arg(&argvars[2]);
12032 if (buf == NULL)
12033 goto cleanup;
12034
12035 rettv->vval.v_number = sign_jump(sign_id, sign_group, buf);
12036
12037cleanup:
12038 vim_free(sign_group);
12039}
12040
12041/*
Bram Moolenaar162b7142018-12-21 15:17:36 +010012042 * "sign_place()" function
12043 */
12044 static void
12045f_sign_place(typval_T *argvars, typval_T *rettv)
12046{
12047 int sign_id;
12048 char_u *group = NULL;
12049 char_u *sign_name;
12050 buf_T *buf;
12051 dict_T *dict;
12052 dictitem_T *di;
12053 linenr_T lnum = 0;
12054 int prio = SIGN_DEF_PRIO;
12055 int notanum = FALSE;
12056
12057 rettv->vval.v_number = -1;
12058
Bram Moolenaarbdace832019-03-02 10:13:42 +010012059 // Sign identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012060 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012061 if (notanum)
12062 return;
12063 if (sign_id < 0)
12064 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012065 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012066 return;
12067 }
12068
12069 // Sign group
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012070 group = tv_get_string_chk(&argvars[1]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012071 if (group == NULL)
12072 return;
12073 if (group[0] == '\0')
12074 group = NULL; // global sign group
12075 else
12076 {
12077 group = vim_strsave(group);
12078 if (group == NULL)
12079 return;
12080 }
12081
12082 // Sign name
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012083 sign_name = tv_get_string_chk(&argvars[2]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012084 if (sign_name == NULL)
12085 goto cleanup;
12086
12087 // Buffer to place the sign
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012088 buf = get_buf_arg(&argvars[3]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012089 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012090 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012091
12092 if (argvars[4].v_type != VAR_UNKNOWN)
12093 {
12094 if (argvars[4].v_type != VAR_DICT ||
12095 ((dict = argvars[4].vval.v_dict) == NULL))
12096 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012097 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012098 goto cleanup;
12099 }
12100
12101 // Line number where the sign is to be placed
12102 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
12103 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012104 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012105 if (notanum)
12106 goto cleanup;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012107 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012108 }
12109 if ((di = dict_find(dict, (char_u *)"priority", -1)) != NULL)
12110 {
12111 // Sign priority
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012112 prio = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012113 if (notanum)
12114 goto cleanup;
12115 }
12116 }
12117
12118 if (sign_place(&sign_id, group, sign_name, buf, lnum, prio) == OK)
12119 rettv->vval.v_number = sign_id;
12120
12121cleanup:
12122 vim_free(group);
12123}
12124
12125/*
12126 * "sign_undefine()" function
12127 */
12128 static void
12129f_sign_undefine(typval_T *argvars, typval_T *rettv)
12130{
12131 char_u *name;
12132
12133 rettv->vval.v_number = -1;
12134
12135 if (argvars[0].v_type == VAR_UNKNOWN)
12136 {
12137 // Free all the signs
12138 free_signs();
12139 rettv->vval.v_number = 0;
12140 }
12141 else
12142 {
12143 // Free only the specified sign
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012144 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012145 if (name == NULL)
12146 return;
12147
12148 if (sign_undefine_by_name(name) == OK)
12149 rettv->vval.v_number = 0;
12150 }
12151}
12152
12153/*
12154 * "sign_unplace()" function
12155 */
12156 static void
12157f_sign_unplace(typval_T *argvars, typval_T *rettv)
12158{
12159 dict_T *dict;
12160 dictitem_T *di;
12161 int sign_id = 0;
12162 buf_T *buf = NULL;
12163 char_u *group = NULL;
12164
12165 rettv->vval.v_number = -1;
12166
12167 if (argvars[0].v_type != VAR_STRING)
12168 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012169 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012170 return;
12171 }
12172
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012173 group = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012174 if (group[0] == '\0')
12175 group = NULL; // global sign group
12176 else
12177 {
12178 group = vim_strsave(group);
12179 if (group == NULL)
12180 return;
12181 }
12182
12183 if (argvars[1].v_type != VAR_UNKNOWN)
12184 {
12185 if (argvars[1].v_type != VAR_DICT)
12186 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012187 emsg(_(e_dictreq));
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012188 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012189 }
12190 dict = argvars[1].vval.v_dict;
12191
12192 if ((di = dict_find(dict, (char_u *)"buffer", -1)) != NULL)
12193 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012194 buf = get_buf_arg(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012195 if (buf == NULL)
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012196 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012197 }
12198 if (dict_find(dict, (char_u *)"id", -1) != NULL)
12199 sign_id = dict_get_number(dict, (char_u *)"id");
12200 }
12201
12202 if (buf == NULL)
12203 {
12204 // Delete the sign in all the buffers
12205 FOR_ALL_BUFFERS(buf)
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010012206 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012207 rettv->vval.v_number = 0;
12208 }
12209 else
12210 {
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010012211 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012212 rettv->vval.v_number = 0;
12213 }
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012214
12215cleanup:
Bram Moolenaar162b7142018-12-21 15:17:36 +010012216 vim_free(group);
12217}
12218#endif
12219
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012220/*
12221 * "simplify()" function
12222 */
12223 static void
12224f_simplify(typval_T *argvars, typval_T *rettv)
12225{
12226 char_u *p;
12227
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012228 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012229 rettv->vval.v_string = vim_strsave(p);
12230 simplify_filename(rettv->vval.v_string); /* simplify in place */
12231 rettv->v_type = VAR_STRING;
12232}
12233
12234#ifdef FEAT_FLOAT
12235/*
12236 * "sin()" function
12237 */
12238 static void
12239f_sin(typval_T *argvars, typval_T *rettv)
12240{
12241 float_T f = 0.0;
12242
12243 rettv->v_type = VAR_FLOAT;
12244 if (get_float_arg(argvars, &f) == OK)
12245 rettv->vval.v_float = sin(f);
12246 else
12247 rettv->vval.v_float = 0.0;
12248}
12249
12250/*
12251 * "sinh()" function
12252 */
12253 static void
12254f_sinh(typval_T *argvars, typval_T *rettv)
12255{
12256 float_T f = 0.0;
12257
12258 rettv->v_type = VAR_FLOAT;
12259 if (get_float_arg(argvars, &f) == OK)
12260 rettv->vval.v_float = sinh(f);
12261 else
12262 rettv->vval.v_float = 0.0;
12263}
12264#endif
12265
12266static int
12267#ifdef __BORLANDC__
12268 _RTLENTRYF
12269#endif
12270 item_compare(const void *s1, const void *s2);
12271static int
12272#ifdef __BORLANDC__
12273 _RTLENTRYF
12274#endif
12275 item_compare2(const void *s1, const void *s2);
12276
12277/* struct used in the array that's given to qsort() */
12278typedef struct
12279{
12280 listitem_T *item;
12281 int idx;
12282} sortItem_T;
12283
12284/* struct storing information about current sort */
12285typedef struct
12286{
12287 int item_compare_ic;
12288 int item_compare_numeric;
12289 int item_compare_numbers;
12290#ifdef FEAT_FLOAT
12291 int item_compare_float;
12292#endif
12293 char_u *item_compare_func;
12294 partial_T *item_compare_partial;
12295 dict_T *item_compare_selfdict;
12296 int item_compare_func_err;
12297 int item_compare_keep_zero;
12298} sortinfo_T;
12299static sortinfo_T *sortinfo = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012300#define ITEM_COMPARE_FAIL 999
12301
12302/*
12303 * Compare functions for f_sort() and f_uniq() below.
12304 */
12305 static int
12306#ifdef __BORLANDC__
12307_RTLENTRYF
12308#endif
12309item_compare(const void *s1, const void *s2)
12310{
12311 sortItem_T *si1, *si2;
12312 typval_T *tv1, *tv2;
12313 char_u *p1, *p2;
12314 char_u *tofree1 = NULL, *tofree2 = NULL;
12315 int res;
12316 char_u numbuf1[NUMBUFLEN];
12317 char_u numbuf2[NUMBUFLEN];
12318
12319 si1 = (sortItem_T *)s1;
12320 si2 = (sortItem_T *)s2;
12321 tv1 = &si1->item->li_tv;
12322 tv2 = &si2->item->li_tv;
12323
12324 if (sortinfo->item_compare_numbers)
12325 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012326 varnumber_T v1 = tv_get_number(tv1);
12327 varnumber_T v2 = tv_get_number(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012328
12329 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
12330 }
12331
12332#ifdef FEAT_FLOAT
12333 if (sortinfo->item_compare_float)
12334 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012335 float_T v1 = tv_get_float(tv1);
12336 float_T v2 = tv_get_float(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012337
12338 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
12339 }
12340#endif
12341
12342 /* tv2string() puts quotes around a string and allocates memory. Don't do
12343 * that for string variables. Use a single quote when comparing with a
12344 * non-string to do what the docs promise. */
12345 if (tv1->v_type == VAR_STRING)
12346 {
12347 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12348 p1 = (char_u *)"'";
12349 else
12350 p1 = tv1->vval.v_string;
12351 }
12352 else
12353 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
12354 if (tv2->v_type == VAR_STRING)
12355 {
12356 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12357 p2 = (char_u *)"'";
12358 else
12359 p2 = tv2->vval.v_string;
12360 }
12361 else
12362 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
12363 if (p1 == NULL)
12364 p1 = (char_u *)"";
12365 if (p2 == NULL)
12366 p2 = (char_u *)"";
12367 if (!sortinfo->item_compare_numeric)
12368 {
12369 if (sortinfo->item_compare_ic)
12370 res = STRICMP(p1, p2);
12371 else
12372 res = STRCMP(p1, p2);
12373 }
12374 else
12375 {
12376 double n1, n2;
12377 n1 = strtod((char *)p1, (char **)&p1);
12378 n2 = strtod((char *)p2, (char **)&p2);
12379 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
12380 }
12381
12382 /* When the result would be zero, compare the item indexes. Makes the
12383 * sort stable. */
12384 if (res == 0 && !sortinfo->item_compare_keep_zero)
12385 res = si1->idx > si2->idx ? 1 : -1;
12386
12387 vim_free(tofree1);
12388 vim_free(tofree2);
12389 return res;
12390}
12391
12392 static int
12393#ifdef __BORLANDC__
12394_RTLENTRYF
12395#endif
12396item_compare2(const void *s1, const void *s2)
12397{
12398 sortItem_T *si1, *si2;
12399 int res;
12400 typval_T rettv;
12401 typval_T argv[3];
12402 int dummy;
12403 char_u *func_name;
12404 partial_T *partial = sortinfo->item_compare_partial;
12405
12406 /* shortcut after failure in previous call; compare all items equal */
12407 if (sortinfo->item_compare_func_err)
12408 return 0;
12409
12410 si1 = (sortItem_T *)s1;
12411 si2 = (sortItem_T *)s2;
12412
12413 if (partial == NULL)
12414 func_name = sortinfo->item_compare_func;
12415 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012416 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012417
12418 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
12419 * in the copy without changing the original list items. */
12420 copy_tv(&si1->item->li_tv, &argv[0]);
12421 copy_tv(&si2->item->li_tv, &argv[1]);
12422
12423 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
12424 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020012425 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012426 partial, sortinfo->item_compare_selfdict);
12427 clear_tv(&argv[0]);
12428 clear_tv(&argv[1]);
12429
12430 if (res == FAIL)
12431 res = ITEM_COMPARE_FAIL;
12432 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012433 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012434 if (sortinfo->item_compare_func_err)
12435 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
12436 clear_tv(&rettv);
12437
12438 /* When the result would be zero, compare the pointers themselves. Makes
12439 * the sort stable. */
12440 if (res == 0 && !sortinfo->item_compare_keep_zero)
12441 res = si1->idx > si2->idx ? 1 : -1;
12442
12443 return res;
12444}
12445
12446/*
12447 * "sort({list})" function
12448 */
12449 static void
12450do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
12451{
12452 list_T *l;
12453 listitem_T *li;
12454 sortItem_T *ptrs;
12455 sortinfo_T *old_sortinfo;
12456 sortinfo_T info;
12457 long len;
12458 long i;
12459
12460 /* Pointer to current info struct used in compare function. Save and
12461 * restore the current one for nested calls. */
12462 old_sortinfo = sortinfo;
12463 sortinfo = &info;
12464
12465 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012466 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012467 else
12468 {
12469 l = argvars[0].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +010012470 if (l == NULL || var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012471 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
12472 TRUE))
12473 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012474 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012475
12476 len = list_len(l);
12477 if (len <= 1)
12478 goto theend; /* short list sorts pretty quickly */
12479
12480 info.item_compare_ic = FALSE;
12481 info.item_compare_numeric = FALSE;
12482 info.item_compare_numbers = FALSE;
12483#ifdef FEAT_FLOAT
12484 info.item_compare_float = FALSE;
12485#endif
12486 info.item_compare_func = NULL;
12487 info.item_compare_partial = NULL;
12488 info.item_compare_selfdict = NULL;
12489 if (argvars[1].v_type != VAR_UNKNOWN)
12490 {
12491 /* optional second argument: {func} */
12492 if (argvars[1].v_type == VAR_FUNC)
12493 info.item_compare_func = argvars[1].vval.v_string;
12494 else if (argvars[1].v_type == VAR_PARTIAL)
12495 info.item_compare_partial = argvars[1].vval.v_partial;
12496 else
12497 {
12498 int error = FALSE;
12499
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012500 i = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012501 if (error)
12502 goto theend; /* type error; errmsg already given */
12503 if (i == 1)
12504 info.item_compare_ic = TRUE;
12505 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012506 info.item_compare_func = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012507 else if (i != 0)
12508 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012509 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012510 goto theend;
12511 }
12512 if (info.item_compare_func != NULL)
12513 {
12514 if (*info.item_compare_func == NUL)
12515 {
12516 /* empty string means default sort */
12517 info.item_compare_func = NULL;
12518 }
12519 else if (STRCMP(info.item_compare_func, "n") == 0)
12520 {
12521 info.item_compare_func = NULL;
12522 info.item_compare_numeric = TRUE;
12523 }
12524 else if (STRCMP(info.item_compare_func, "N") == 0)
12525 {
12526 info.item_compare_func = NULL;
12527 info.item_compare_numbers = TRUE;
12528 }
12529#ifdef FEAT_FLOAT
12530 else if (STRCMP(info.item_compare_func, "f") == 0)
12531 {
12532 info.item_compare_func = NULL;
12533 info.item_compare_float = TRUE;
12534 }
12535#endif
12536 else if (STRCMP(info.item_compare_func, "i") == 0)
12537 {
12538 info.item_compare_func = NULL;
12539 info.item_compare_ic = TRUE;
12540 }
12541 }
12542 }
12543
12544 if (argvars[2].v_type != VAR_UNKNOWN)
12545 {
12546 /* optional third argument: {dict} */
12547 if (argvars[2].v_type != VAR_DICT)
12548 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012549 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012550 goto theend;
12551 }
12552 info.item_compare_selfdict = argvars[2].vval.v_dict;
12553 }
12554 }
12555
12556 /* Make an array with each entry pointing to an item in the List. */
12557 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
12558 if (ptrs == NULL)
12559 goto theend;
12560
12561 i = 0;
12562 if (sort)
12563 {
12564 /* sort(): ptrs will be the list to sort */
12565 for (li = l->lv_first; li != NULL; li = li->li_next)
12566 {
12567 ptrs[i].item = li;
12568 ptrs[i].idx = i;
12569 ++i;
12570 }
12571
12572 info.item_compare_func_err = FALSE;
12573 info.item_compare_keep_zero = FALSE;
12574 /* test the compare function */
12575 if ((info.item_compare_func != NULL
12576 || info.item_compare_partial != NULL)
12577 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
12578 == ITEM_COMPARE_FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012579 emsg(_("E702: Sort compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012580 else
12581 {
12582 /* Sort the array with item pointers. */
12583 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
12584 info.item_compare_func == NULL
12585 && info.item_compare_partial == NULL
12586 ? item_compare : item_compare2);
12587
12588 if (!info.item_compare_func_err)
12589 {
12590 /* Clear the List and append the items in sorted order. */
12591 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
12592 l->lv_len = 0;
12593 for (i = 0; i < len; ++i)
12594 list_append(l, ptrs[i].item);
12595 }
12596 }
12597 }
12598 else
12599 {
12600 int (*item_compare_func_ptr)(const void *, const void *);
12601
12602 /* f_uniq(): ptrs will be a stack of items to remove */
12603 info.item_compare_func_err = FALSE;
12604 info.item_compare_keep_zero = TRUE;
12605 item_compare_func_ptr = info.item_compare_func != NULL
12606 || info.item_compare_partial != NULL
12607 ? item_compare2 : item_compare;
12608
12609 for (li = l->lv_first; li != NULL && li->li_next != NULL;
12610 li = li->li_next)
12611 {
12612 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
12613 == 0)
12614 ptrs[i++].item = li;
12615 if (info.item_compare_func_err)
12616 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012617 emsg(_("E882: Uniq compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012618 break;
12619 }
12620 }
12621
12622 if (!info.item_compare_func_err)
12623 {
12624 while (--i >= 0)
12625 {
12626 li = ptrs[i].item->li_next;
12627 ptrs[i].item->li_next = li->li_next;
12628 if (li->li_next != NULL)
12629 li->li_next->li_prev = ptrs[i].item;
12630 else
12631 l->lv_last = ptrs[i].item;
12632 list_fix_watch(l, li);
12633 listitem_free(li);
12634 l->lv_len--;
12635 }
12636 }
12637 }
12638
12639 vim_free(ptrs);
12640 }
12641theend:
12642 sortinfo = old_sortinfo;
12643}
12644
12645/*
12646 * "sort({list})" function
12647 */
12648 static void
12649f_sort(typval_T *argvars, typval_T *rettv)
12650{
12651 do_sort_uniq(argvars, rettv, TRUE);
12652}
12653
12654/*
12655 * "uniq({list})" function
12656 */
12657 static void
12658f_uniq(typval_T *argvars, typval_T *rettv)
12659{
12660 do_sort_uniq(argvars, rettv, FALSE);
12661}
12662
12663/*
12664 * "soundfold({word})" function
12665 */
12666 static void
12667f_soundfold(typval_T *argvars, typval_T *rettv)
12668{
12669 char_u *s;
12670
12671 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012672 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012673#ifdef FEAT_SPELL
12674 rettv->vval.v_string = eval_soundfold(s);
12675#else
12676 rettv->vval.v_string = vim_strsave(s);
12677#endif
12678}
12679
12680/*
12681 * "spellbadword()" function
12682 */
12683 static void
12684f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
12685{
12686 char_u *word = (char_u *)"";
12687 hlf_T attr = HLF_COUNT;
12688 int len = 0;
12689
12690 if (rettv_list_alloc(rettv) == FAIL)
12691 return;
12692
12693#ifdef FEAT_SPELL
12694 if (argvars[0].v_type == VAR_UNKNOWN)
12695 {
12696 /* Find the start and length of the badly spelled word. */
12697 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
12698 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012699 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012700 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012701 curwin->w_set_curswant = TRUE;
12702 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012703 }
12704 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
12705 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012706 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012707 int capcol = -1;
12708
12709 if (str != NULL)
12710 {
12711 /* Check the argument for spelling. */
12712 while (*str != NUL)
12713 {
12714 len = spell_check(curwin, str, &attr, &capcol, FALSE);
12715 if (attr != HLF_COUNT)
12716 {
12717 word = str;
12718 break;
12719 }
12720 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020012721 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012722 }
12723 }
12724 }
12725#endif
12726
12727 list_append_string(rettv->vval.v_list, word, len);
12728 list_append_string(rettv->vval.v_list, (char_u *)(
12729 attr == HLF_SPB ? "bad" :
12730 attr == HLF_SPR ? "rare" :
12731 attr == HLF_SPL ? "local" :
12732 attr == HLF_SPC ? "caps" :
12733 ""), -1);
12734}
12735
12736/*
12737 * "spellsuggest()" function
12738 */
12739 static void
12740f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
12741{
12742#ifdef FEAT_SPELL
12743 char_u *str;
12744 int typeerr = FALSE;
12745 int maxcount;
12746 garray_T ga;
12747 int i;
12748 listitem_T *li;
12749 int need_capital = FALSE;
12750#endif
12751
12752 if (rettv_list_alloc(rettv) == FAIL)
12753 return;
12754
12755#ifdef FEAT_SPELL
12756 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
12757 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012758 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012759 if (argvars[1].v_type != VAR_UNKNOWN)
12760 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012761 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012762 if (maxcount <= 0)
12763 return;
12764 if (argvars[2].v_type != VAR_UNKNOWN)
12765 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012766 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012767 if (typeerr)
12768 return;
12769 }
12770 }
12771 else
12772 maxcount = 25;
12773
12774 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
12775
12776 for (i = 0; i < ga.ga_len; ++i)
12777 {
12778 str = ((char_u **)ga.ga_data)[i];
12779
12780 li = listitem_alloc();
12781 if (li == NULL)
12782 vim_free(str);
12783 else
12784 {
12785 li->li_tv.v_type = VAR_STRING;
12786 li->li_tv.v_lock = 0;
12787 li->li_tv.vval.v_string = str;
12788 list_append(rettv->vval.v_list, li);
12789 }
12790 }
12791 ga_clear(&ga);
12792 }
12793#endif
12794}
12795
12796 static void
12797f_split(typval_T *argvars, typval_T *rettv)
12798{
12799 char_u *str;
12800 char_u *end;
12801 char_u *pat = NULL;
12802 regmatch_T regmatch;
12803 char_u patbuf[NUMBUFLEN];
12804 char_u *save_cpo;
12805 int match;
12806 colnr_T col = 0;
12807 int keepempty = FALSE;
12808 int typeerr = FALSE;
12809
12810 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12811 save_cpo = p_cpo;
12812 p_cpo = (char_u *)"";
12813
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012814 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012815 if (argvars[1].v_type != VAR_UNKNOWN)
12816 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012817 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012818 if (pat == NULL)
12819 typeerr = TRUE;
12820 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012821 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012822 }
12823 if (pat == NULL || *pat == NUL)
12824 pat = (char_u *)"[\\x01- ]\\+";
12825
12826 if (rettv_list_alloc(rettv) == FAIL)
12827 return;
12828 if (typeerr)
12829 return;
12830
12831 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12832 if (regmatch.regprog != NULL)
12833 {
12834 regmatch.rm_ic = FALSE;
12835 while (*str != NUL || keepempty)
12836 {
12837 if (*str == NUL)
12838 match = FALSE; /* empty item at the end */
12839 else
12840 match = vim_regexec_nl(&regmatch, str, col);
12841 if (match)
12842 end = regmatch.startp[0];
12843 else
12844 end = str + STRLEN(str);
12845 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
12846 && *str != NUL && match && end < regmatch.endp[0]))
12847 {
12848 if (list_append_string(rettv->vval.v_list, str,
12849 (int)(end - str)) == FAIL)
12850 break;
12851 }
12852 if (!match)
12853 break;
Bram Moolenaar13505972019-01-24 15:04:48 +010012854 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012855 if (regmatch.endp[0] > str)
12856 col = 0;
12857 else
Bram Moolenaar13505972019-01-24 15:04:48 +010012858 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012859 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012860 str = regmatch.endp[0];
12861 }
12862
12863 vim_regfree(regmatch.regprog);
12864 }
12865
12866 p_cpo = save_cpo;
12867}
12868
12869#ifdef FEAT_FLOAT
12870/*
12871 * "sqrt()" function
12872 */
12873 static void
12874f_sqrt(typval_T *argvars, typval_T *rettv)
12875{
12876 float_T f = 0.0;
12877
12878 rettv->v_type = VAR_FLOAT;
12879 if (get_float_arg(argvars, &f) == OK)
12880 rettv->vval.v_float = sqrt(f);
12881 else
12882 rettv->vval.v_float = 0.0;
12883}
12884
12885/*
12886 * "str2float()" function
12887 */
12888 static void
12889f_str2float(typval_T *argvars, typval_T *rettv)
12890{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012891 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010012892 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012893
Bram Moolenaar08243d22017-01-10 16:12:29 +010012894 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012895 p = skipwhite(p + 1);
12896 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010012897 if (isneg)
12898 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012899 rettv->v_type = VAR_FLOAT;
12900}
12901#endif
12902
12903/*
12904 * "str2nr()" function
12905 */
12906 static void
12907f_str2nr(typval_T *argvars, typval_T *rettv)
12908{
12909 int base = 10;
12910 char_u *p;
12911 varnumber_T n;
12912 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010012913 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012914
12915 if (argvars[1].v_type != VAR_UNKNOWN)
12916 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012917 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012918 if (base != 2 && base != 8 && base != 10 && base != 16)
12919 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012920 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012921 return;
12922 }
12923 }
12924
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012925 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010012926 isneg = (*p == '-');
12927 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012928 p = skipwhite(p + 1);
12929 switch (base)
12930 {
12931 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
12932 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
12933 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
12934 default: what = 0;
12935 }
12936 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010012937 if (isneg)
12938 rettv->vval.v_number = -n;
12939 else
12940 rettv->vval.v_number = n;
12941
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012942}
12943
12944#ifdef HAVE_STRFTIME
12945/*
12946 * "strftime({format}[, {time}])" function
12947 */
12948 static void
12949f_strftime(typval_T *argvars, typval_T *rettv)
12950{
12951 char_u result_buf[256];
12952 struct tm *curtime;
12953 time_t seconds;
12954 char_u *p;
12955
12956 rettv->v_type = VAR_STRING;
12957
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012958 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012959 if (argvars[1].v_type == VAR_UNKNOWN)
12960 seconds = time(NULL);
12961 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012962 seconds = (time_t)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012963 curtime = localtime(&seconds);
12964 /* MSVC returns NULL for an invalid value of seconds. */
12965 if (curtime == NULL)
12966 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
12967 else
12968 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012969 vimconv_T conv;
12970 char_u *enc;
12971
12972 conv.vc_type = CONV_NONE;
12973 enc = enc_locale();
12974 convert_setup(&conv, p_enc, enc);
12975 if (conv.vc_type != CONV_NONE)
12976 p = string_convert(&conv, p, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012977 if (p != NULL)
12978 (void)strftime((char *)result_buf, sizeof(result_buf),
12979 (char *)p, curtime);
12980 else
12981 result_buf[0] = NUL;
12982
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012983 if (conv.vc_type != CONV_NONE)
12984 vim_free(p);
12985 convert_setup(&conv, enc, p_enc);
12986 if (conv.vc_type != CONV_NONE)
12987 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
12988 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012989 rettv->vval.v_string = vim_strsave(result_buf);
12990
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012991 /* Release conversion descriptors */
12992 convert_setup(&conv, NULL, NULL);
12993 vim_free(enc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012994 }
12995}
12996#endif
12997
12998/*
12999 * "strgetchar()" function
13000 */
13001 static void
13002f_strgetchar(typval_T *argvars, typval_T *rettv)
13003{
13004 char_u *str;
13005 int len;
13006 int error = FALSE;
13007 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +010013008 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013009
13010 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013011 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013012 if (str == NULL)
13013 return;
13014 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013015 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013016 if (error)
13017 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013018
Bram Moolenaar13505972019-01-24 15:04:48 +010013019 while (charidx >= 0 && byteidx < len)
13020 {
13021 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013022 {
Bram Moolenaar13505972019-01-24 15:04:48 +010013023 rettv->vval.v_number = mb_ptr2char(str + byteidx);
13024 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013025 }
Bram Moolenaar13505972019-01-24 15:04:48 +010013026 --charidx;
13027 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013028 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013029}
13030
13031/*
13032 * "stridx()" function
13033 */
13034 static void
13035f_stridx(typval_T *argvars, typval_T *rettv)
13036{
13037 char_u buf[NUMBUFLEN];
13038 char_u *needle;
13039 char_u *haystack;
13040 char_u *save_haystack;
13041 char_u *pos;
13042 int start_idx;
13043
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013044 needle = tv_get_string_chk(&argvars[1]);
13045 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013046 rettv->vval.v_number = -1;
13047 if (needle == NULL || haystack == NULL)
13048 return; /* type error; errmsg already given */
13049
13050 if (argvars[2].v_type != VAR_UNKNOWN)
13051 {
13052 int error = FALSE;
13053
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013054 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013055 if (error || start_idx >= (int)STRLEN(haystack))
13056 return;
13057 if (start_idx >= 0)
13058 haystack += start_idx;
13059 }
13060
13061 pos = (char_u *)strstr((char *)haystack, (char *)needle);
13062 if (pos != NULL)
13063 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
13064}
13065
13066/*
13067 * "string()" function
13068 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010013069 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013070f_string(typval_T *argvars, typval_T *rettv)
13071{
13072 char_u *tofree;
13073 char_u numbuf[NUMBUFLEN];
13074
13075 rettv->v_type = VAR_STRING;
13076 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
13077 get_copyID());
13078 /* Make a copy if we have a value but it's not in allocated memory. */
13079 if (rettv->vval.v_string != NULL && tofree == NULL)
13080 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
13081}
13082
13083/*
13084 * "strlen()" function
13085 */
13086 static void
13087f_strlen(typval_T *argvars, typval_T *rettv)
13088{
13089 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013090 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013091}
13092
13093/*
13094 * "strchars()" function
13095 */
13096 static void
13097f_strchars(typval_T *argvars, typval_T *rettv)
13098{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013099 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013100 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013101 varnumber_T len = 0;
13102 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013103
13104 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013105 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013106 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013107 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013108 else
13109 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013110 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
13111 while (*s != NUL)
13112 {
13113 func_mb_ptr2char_adv(&s);
13114 ++len;
13115 }
13116 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013117 }
13118}
13119
13120/*
13121 * "strdisplaywidth()" function
13122 */
13123 static void
13124f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
13125{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013126 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013127 int col = 0;
13128
13129 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013130 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013131
13132 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
13133}
13134
13135/*
13136 * "strwidth()" function
13137 */
13138 static void
13139f_strwidth(typval_T *argvars, typval_T *rettv)
13140{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013141 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013142
Bram Moolenaar13505972019-01-24 15:04:48 +010013143 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013144}
13145
13146/*
13147 * "strcharpart()" function
13148 */
13149 static void
13150f_strcharpart(typval_T *argvars, typval_T *rettv)
13151{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013152 char_u *p;
13153 int nchar;
13154 int nbyte = 0;
13155 int charlen;
13156 int len = 0;
13157 int slen;
13158 int error = FALSE;
13159
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013160 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013161 slen = (int)STRLEN(p);
13162
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013163 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013164 if (!error)
13165 {
13166 if (nchar > 0)
13167 while (nchar > 0 && nbyte < slen)
13168 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020013169 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013170 --nchar;
13171 }
13172 else
13173 nbyte = nchar;
13174 if (argvars[2].v_type != VAR_UNKNOWN)
13175 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013176 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013177 while (charlen > 0 && nbyte + len < slen)
13178 {
13179 int off = nbyte + len;
13180
13181 if (off < 0)
13182 len += 1;
13183 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020013184 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013185 --charlen;
13186 }
13187 }
13188 else
13189 len = slen - nbyte; /* default: all bytes that are available. */
13190 }
13191
13192 /*
13193 * Only return the overlap between the specified part and the actual
13194 * string.
13195 */
13196 if (nbyte < 0)
13197 {
13198 len += nbyte;
13199 nbyte = 0;
13200 }
13201 else if (nbyte > slen)
13202 nbyte = slen;
13203 if (len < 0)
13204 len = 0;
13205 else if (nbyte + len > slen)
13206 len = slen - nbyte;
13207
13208 rettv->v_type = VAR_STRING;
13209 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013210}
13211
13212/*
13213 * "strpart()" function
13214 */
13215 static void
13216f_strpart(typval_T *argvars, typval_T *rettv)
13217{
13218 char_u *p;
13219 int n;
13220 int len;
13221 int slen;
13222 int error = FALSE;
13223
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013224 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013225 slen = (int)STRLEN(p);
13226
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013227 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013228 if (error)
13229 len = 0;
13230 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013231 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013232 else
13233 len = slen - n; /* default len: all bytes that are available. */
13234
13235 /*
13236 * Only return the overlap between the specified part and the actual
13237 * string.
13238 */
13239 if (n < 0)
13240 {
13241 len += n;
13242 n = 0;
13243 }
13244 else if (n > slen)
13245 n = slen;
13246 if (len < 0)
13247 len = 0;
13248 else if (n + len > slen)
13249 len = slen - n;
13250
13251 rettv->v_type = VAR_STRING;
13252 rettv->vval.v_string = vim_strnsave(p + n, len);
13253}
13254
13255/*
13256 * "strridx()" function
13257 */
13258 static void
13259f_strridx(typval_T *argvars, typval_T *rettv)
13260{
13261 char_u buf[NUMBUFLEN];
13262 char_u *needle;
13263 char_u *haystack;
13264 char_u *rest;
13265 char_u *lastmatch = NULL;
13266 int haystack_len, end_idx;
13267
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013268 needle = tv_get_string_chk(&argvars[1]);
13269 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013270
13271 rettv->vval.v_number = -1;
13272 if (needle == NULL || haystack == NULL)
13273 return; /* type error; errmsg already given */
13274
13275 haystack_len = (int)STRLEN(haystack);
13276 if (argvars[2].v_type != VAR_UNKNOWN)
13277 {
13278 /* Third argument: upper limit for index */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013279 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013280 if (end_idx < 0)
13281 return; /* can never find a match */
13282 }
13283 else
13284 end_idx = haystack_len;
13285
13286 if (*needle == NUL)
13287 {
13288 /* Empty string matches past the end. */
13289 lastmatch = haystack + end_idx;
13290 }
13291 else
13292 {
13293 for (rest = haystack; *rest != '\0'; ++rest)
13294 {
13295 rest = (char_u *)strstr((char *)rest, (char *)needle);
13296 if (rest == NULL || rest > haystack + end_idx)
13297 break;
13298 lastmatch = rest;
13299 }
13300 }
13301
13302 if (lastmatch == NULL)
13303 rettv->vval.v_number = -1;
13304 else
13305 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
13306}
13307
13308/*
13309 * "strtrans()" function
13310 */
13311 static void
13312f_strtrans(typval_T *argvars, typval_T *rettv)
13313{
13314 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013315 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013316}
13317
13318/*
13319 * "submatch()" function
13320 */
13321 static void
13322f_submatch(typval_T *argvars, typval_T *rettv)
13323{
13324 int error = FALSE;
13325 int no;
13326 int retList = 0;
13327
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013328 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013329 if (error)
13330 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013331 if (no < 0 || no >= NSUBEXP)
13332 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013333 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +010013334 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013335 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013336 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013337 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013338 if (error)
13339 return;
13340
13341 if (retList == 0)
13342 {
13343 rettv->v_type = VAR_STRING;
13344 rettv->vval.v_string = reg_submatch(no);
13345 }
13346 else
13347 {
13348 rettv->v_type = VAR_LIST;
13349 rettv->vval.v_list = reg_submatch_list(no);
13350 }
13351}
13352
13353/*
13354 * "substitute()" function
13355 */
13356 static void
13357f_substitute(typval_T *argvars, typval_T *rettv)
13358{
13359 char_u patbuf[NUMBUFLEN];
13360 char_u subbuf[NUMBUFLEN];
13361 char_u flagsbuf[NUMBUFLEN];
13362
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013363 char_u *str = tv_get_string_chk(&argvars[0]);
13364 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013365 char_u *sub = NULL;
13366 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013367 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013368
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013369 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
13370 expr = &argvars[2];
13371 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013372 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013373
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013374 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013375 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
13376 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013377 rettv->vval.v_string = NULL;
13378 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013379 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013380}
13381
13382/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013383 * "swapinfo(swap_filename)" function
13384 */
13385 static void
13386f_swapinfo(typval_T *argvars, typval_T *rettv)
13387{
13388 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013389 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013390}
13391
13392/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020013393 * "swapname(expr)" function
13394 */
13395 static void
13396f_swapname(typval_T *argvars, typval_T *rettv)
13397{
13398 buf_T *buf;
13399
13400 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010013401 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +020013402 if (buf == NULL || buf->b_ml.ml_mfp == NULL
13403 || buf->b_ml.ml_mfp->mf_fname == NULL)
13404 rettv->vval.v_string = NULL;
13405 else
13406 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
13407}
13408
13409/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013410 * "synID(lnum, col, trans)" function
13411 */
13412 static void
13413f_synID(typval_T *argvars UNUSED, typval_T *rettv)
13414{
13415 int id = 0;
13416#ifdef FEAT_SYN_HL
13417 linenr_T lnum;
13418 colnr_T col;
13419 int trans;
13420 int transerr = FALSE;
13421
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013422 lnum = tv_get_lnum(argvars); /* -1 on type error */
13423 col = (linenr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
13424 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013425
13426 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13427 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
13428 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
13429#endif
13430
13431 rettv->vval.v_number = id;
13432}
13433
13434/*
13435 * "synIDattr(id, what [, mode])" function
13436 */
13437 static void
13438f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
13439{
13440 char_u *p = NULL;
13441#ifdef FEAT_SYN_HL
13442 int id;
13443 char_u *what;
13444 char_u *mode;
13445 char_u modebuf[NUMBUFLEN];
13446 int modec;
13447
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013448 id = (int)tv_get_number(&argvars[0]);
13449 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013450 if (argvars[2].v_type != VAR_UNKNOWN)
13451 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013452 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013453 modec = TOLOWER_ASC(mode[0]);
13454 if (modec != 't' && modec != 'c' && modec != 'g')
13455 modec = 0; /* replace invalid with current */
13456 }
13457 else
13458 {
13459#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
13460 if (USE_24BIT)
13461 modec = 'g';
13462 else
13463#endif
13464 if (t_colors > 1)
13465 modec = 'c';
13466 else
13467 modec = 't';
13468 }
13469
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013470 switch (TOLOWER_ASC(what[0]))
13471 {
13472 case 'b':
13473 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
13474 p = highlight_color(id, what, modec);
13475 else /* bold */
13476 p = highlight_has_attr(id, HL_BOLD, modec);
13477 break;
13478
13479 case 'f': /* fg[#] or font */
13480 p = highlight_color(id, what, modec);
13481 break;
13482
13483 case 'i':
13484 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
13485 p = highlight_has_attr(id, HL_INVERSE, modec);
13486 else /* italic */
13487 p = highlight_has_attr(id, HL_ITALIC, modec);
13488 break;
13489
13490 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020013491 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013492 break;
13493
13494 case 'r': /* reverse */
13495 p = highlight_has_attr(id, HL_INVERSE, modec);
13496 break;
13497
13498 case 's':
13499 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
13500 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020013501 /* strikeout */
13502 else if (TOLOWER_ASC(what[1]) == 't' &&
13503 TOLOWER_ASC(what[2]) == 'r')
13504 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013505 else /* standout */
13506 p = highlight_has_attr(id, HL_STANDOUT, modec);
13507 break;
13508
13509 case 'u':
13510 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
13511 /* underline */
13512 p = highlight_has_attr(id, HL_UNDERLINE, modec);
13513 else
13514 /* undercurl */
13515 p = highlight_has_attr(id, HL_UNDERCURL, modec);
13516 break;
13517 }
13518
13519 if (p != NULL)
13520 p = vim_strsave(p);
13521#endif
13522 rettv->v_type = VAR_STRING;
13523 rettv->vval.v_string = p;
13524}
13525
13526/*
13527 * "synIDtrans(id)" function
13528 */
13529 static void
13530f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
13531{
13532 int id;
13533
13534#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013535 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013536
13537 if (id > 0)
13538 id = syn_get_final_id(id);
13539 else
13540#endif
13541 id = 0;
13542
13543 rettv->vval.v_number = id;
13544}
13545
13546/*
13547 * "synconcealed(lnum, col)" function
13548 */
13549 static void
13550f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
13551{
13552#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
13553 linenr_T lnum;
13554 colnr_T col;
13555 int syntax_flags = 0;
13556 int cchar;
13557 int matchid = 0;
13558 char_u str[NUMBUFLEN];
13559#endif
13560
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013561 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013562
13563#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013564 lnum = tv_get_lnum(argvars); /* -1 on type error */
13565 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013566
13567 vim_memset(str, NUL, sizeof(str));
13568
13569 if (rettv_list_alloc(rettv) != FAIL)
13570 {
13571 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13572 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13573 && curwin->w_p_cole > 0)
13574 {
13575 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
13576 syntax_flags = get_syntax_info(&matchid);
13577
13578 /* get the conceal character */
13579 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
13580 {
13581 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020013582 if (cchar == NUL && curwin->w_p_cole == 1)
13583 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013584 if (cchar != NUL)
13585 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013586 if (has_mbyte)
13587 (*mb_char2bytes)(cchar, str);
13588 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013589 str[0] = cchar;
13590 }
13591 }
13592 }
13593
13594 list_append_number(rettv->vval.v_list,
13595 (syntax_flags & HL_CONCEAL) != 0);
13596 /* -1 to auto-determine strlen */
13597 list_append_string(rettv->vval.v_list, str, -1);
13598 list_append_number(rettv->vval.v_list, matchid);
13599 }
13600#endif
13601}
13602
13603/*
13604 * "synstack(lnum, col)" function
13605 */
13606 static void
13607f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
13608{
13609#ifdef FEAT_SYN_HL
13610 linenr_T lnum;
13611 colnr_T col;
13612 int i;
13613 int id;
13614#endif
13615
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013616 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013617
13618#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013619 lnum = tv_get_lnum(argvars); /* -1 on type error */
13620 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013621
13622 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13623 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13624 && rettv_list_alloc(rettv) != FAIL)
13625 {
13626 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
13627 for (i = 0; ; ++i)
13628 {
13629 id = syn_get_stack_item(i);
13630 if (id < 0)
13631 break;
13632 if (list_append_number(rettv->vval.v_list, id) == FAIL)
13633 break;
13634 }
13635 }
13636#endif
13637}
13638
13639 static void
13640get_cmd_output_as_rettv(
13641 typval_T *argvars,
13642 typval_T *rettv,
13643 int retlist)
13644{
13645 char_u *res = NULL;
13646 char_u *p;
13647 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013648 int err = FALSE;
13649 FILE *fd;
13650 list_T *list = NULL;
13651 int flags = SHELL_SILENT;
13652
13653 rettv->v_type = VAR_STRING;
13654 rettv->vval.v_string = NULL;
13655 if (check_restricted() || check_secure())
13656 goto errret;
13657
13658 if (argvars[1].v_type != VAR_UNKNOWN)
13659 {
13660 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010013661 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013662 * command.
13663 */
13664 if ((infile = vim_tempname('i', TRUE)) == NULL)
13665 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013666 emsg(_(e_notmp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013667 goto errret;
13668 }
13669
13670 fd = mch_fopen((char *)infile, WRITEBIN);
13671 if (fd == NULL)
13672 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013673 semsg(_(e_notopen), infile);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013674 goto errret;
13675 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010013676 if (argvars[1].v_type == VAR_NUMBER)
13677 {
13678 linenr_T lnum;
13679 buf_T *buf;
13680
13681 buf = buflist_findnr(argvars[1].vval.v_number);
13682 if (buf == NULL)
13683 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013684 semsg(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010013685 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010013686 goto errret;
13687 }
13688
13689 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
13690 {
13691 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
13692 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
13693 {
13694 err = TRUE;
13695 break;
13696 }
13697 if (putc(NL, fd) == EOF)
13698 {
13699 err = TRUE;
13700 break;
13701 }
13702 }
13703 }
13704 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013705 {
13706 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
13707 err = TRUE;
13708 }
13709 else
13710 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010013711 size_t len;
13712 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013713
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013714 p = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013715 if (p == NULL)
13716 {
13717 fclose(fd);
13718 goto errret; /* type error; errmsg already given */
13719 }
13720 len = STRLEN(p);
13721 if (len > 0 && fwrite(p, len, 1, fd) != 1)
13722 err = TRUE;
13723 }
13724 if (fclose(fd) != 0)
13725 err = TRUE;
13726 if (err)
13727 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013728 emsg(_("E677: Error writing temp file"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013729 goto errret;
13730 }
13731 }
13732
13733 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
13734 * echoes typeahead, that messes up the display. */
13735 if (!msg_silent)
13736 flags += SHELL_COOKED;
13737
13738 if (retlist)
13739 {
13740 int len;
13741 listitem_T *li;
13742 char_u *s = NULL;
13743 char_u *start;
13744 char_u *end;
13745 int i;
13746
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013747 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013748 if (res == NULL)
13749 goto errret;
13750
13751 list = list_alloc();
13752 if (list == NULL)
13753 goto errret;
13754
13755 for (i = 0; i < len; ++i)
13756 {
13757 start = res + i;
13758 while (i < len && res[i] != NL)
13759 ++i;
13760 end = res + i;
13761
13762 s = alloc((unsigned)(end - start + 1));
13763 if (s == NULL)
13764 goto errret;
13765
13766 for (p = s; start < end; ++p, ++start)
13767 *p = *start == NUL ? NL : *start;
13768 *p = NUL;
13769
13770 li = listitem_alloc();
13771 if (li == NULL)
13772 {
13773 vim_free(s);
13774 goto errret;
13775 }
13776 li->li_tv.v_type = VAR_STRING;
13777 li->li_tv.v_lock = 0;
13778 li->li_tv.vval.v_string = s;
13779 list_append(list, li);
13780 }
13781
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013782 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013783 list = NULL;
13784 }
13785 else
13786 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013787 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar00590742019-02-15 21:06:09 +010013788#ifdef USE_CRNL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013789 /* translate <CR><NL> into <NL> */
13790 if (res != NULL)
13791 {
13792 char_u *s, *d;
13793
13794 d = res;
13795 for (s = res; *s; ++s)
13796 {
13797 if (s[0] == CAR && s[1] == NL)
13798 ++s;
13799 *d++ = *s;
13800 }
13801 *d = NUL;
13802 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013803#endif
13804 rettv->vval.v_string = res;
13805 res = NULL;
13806 }
13807
13808errret:
13809 if (infile != NULL)
13810 {
13811 mch_remove(infile);
13812 vim_free(infile);
13813 }
13814 if (res != NULL)
13815 vim_free(res);
13816 if (list != NULL)
13817 list_free(list);
13818}
13819
13820/*
13821 * "system()" function
13822 */
13823 static void
13824f_system(typval_T *argvars, typval_T *rettv)
13825{
13826 get_cmd_output_as_rettv(argvars, rettv, FALSE);
13827}
13828
13829/*
13830 * "systemlist()" function
13831 */
13832 static void
13833f_systemlist(typval_T *argvars, typval_T *rettv)
13834{
13835 get_cmd_output_as_rettv(argvars, rettv, TRUE);
13836}
13837
13838/*
13839 * "tabpagebuflist()" function
13840 */
13841 static void
13842f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13843{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013844 tabpage_T *tp;
13845 win_T *wp = NULL;
13846
13847 if (argvars[0].v_type == VAR_UNKNOWN)
13848 wp = firstwin;
13849 else
13850 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013851 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013852 if (tp != NULL)
13853 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
13854 }
13855 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
13856 {
13857 for (; wp != NULL; wp = wp->w_next)
13858 if (list_append_number(rettv->vval.v_list,
13859 wp->w_buffer->b_fnum) == FAIL)
13860 break;
13861 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013862}
13863
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013864/*
13865 * "tabpagenr()" function
13866 */
13867 static void
13868f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
13869{
13870 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013871 char_u *arg;
13872
13873 if (argvars[0].v_type != VAR_UNKNOWN)
13874 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013875 arg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013876 nr = 0;
13877 if (arg != NULL)
13878 {
13879 if (STRCMP(arg, "$") == 0)
13880 nr = tabpage_index(NULL) - 1;
13881 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013882 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013883 }
13884 }
13885 else
13886 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013887 rettv->vval.v_number = nr;
13888}
13889
13890
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013891/*
13892 * Common code for tabpagewinnr() and winnr().
13893 */
13894 static int
13895get_winnr(tabpage_T *tp, typval_T *argvar)
13896{
13897 win_T *twin;
13898 int nr = 1;
13899 win_T *wp;
13900 char_u *arg;
13901
13902 twin = (tp == curtab) ? curwin : tp->tp_curwin;
13903 if (argvar->v_type != VAR_UNKNOWN)
13904 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013905 arg = tv_get_string_chk(argvar);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013906 if (arg == NULL)
13907 nr = 0; /* type error; errmsg already given */
13908 else if (STRCMP(arg, "$") == 0)
13909 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
13910 else if (STRCMP(arg, "#") == 0)
13911 {
13912 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
13913 if (twin == NULL)
13914 nr = 0;
13915 }
13916 else
13917 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013918 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013919 nr = 0;
13920 }
13921 }
13922
13923 if (nr > 0)
13924 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
13925 wp != twin; wp = wp->w_next)
13926 {
13927 if (wp == NULL)
13928 {
13929 /* didn't find it in this tabpage */
13930 nr = 0;
13931 break;
13932 }
13933 ++nr;
13934 }
13935 return nr;
13936}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013937
13938/*
13939 * "tabpagewinnr()" function
13940 */
13941 static void
13942f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
13943{
13944 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013945 tabpage_T *tp;
13946
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013947 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013948 if (tp == NULL)
13949 nr = 0;
13950 else
13951 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013952 rettv->vval.v_number = nr;
13953}
13954
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013955/*
13956 * "tagfiles()" function
13957 */
13958 static void
13959f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
13960{
13961 char_u *fname;
13962 tagname_T tn;
13963 int first;
13964
13965 if (rettv_list_alloc(rettv) == FAIL)
13966 return;
13967 fname = alloc(MAXPATHL);
13968 if (fname == NULL)
13969 return;
13970
13971 for (first = TRUE; ; first = FALSE)
13972 if (get_tagfname(&tn, first, fname) == FAIL
13973 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
13974 break;
13975 tagname_free(&tn);
13976 vim_free(fname);
13977}
13978
13979/*
13980 * "taglist()" function
13981 */
13982 static void
13983f_taglist(typval_T *argvars, typval_T *rettv)
13984{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013985 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013986 char_u *tag_pattern;
13987
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013988 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013989
13990 rettv->vval.v_number = FALSE;
13991 if (*tag_pattern == NUL)
13992 return;
13993
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013994 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013995 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013996 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013997 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013998}
13999
14000/*
14001 * "tempname()" function
14002 */
14003 static void
14004f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
14005{
14006 static int x = 'A';
14007
14008 rettv->v_type = VAR_STRING;
14009 rettv->vval.v_string = vim_tempname(x, FALSE);
14010
14011 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
14012 * names. Skip 'I' and 'O', they are used for shell redirection. */
14013 do
14014 {
14015 if (x == 'Z')
14016 x = '0';
14017 else if (x == '9')
14018 x = 'A';
14019 else
14020 {
14021#ifdef EBCDIC
14022 if (x == 'I')
14023 x = 'J';
14024 else if (x == 'R')
14025 x = 'S';
14026 else
14027#endif
14028 ++x;
14029 }
14030 } while (x == 'I' || x == 'O');
14031}
14032
14033#ifdef FEAT_FLOAT
14034/*
14035 * "tan()" function
14036 */
14037 static void
14038f_tan(typval_T *argvars, typval_T *rettv)
14039{
14040 float_T f = 0.0;
14041
14042 rettv->v_type = VAR_FLOAT;
14043 if (get_float_arg(argvars, &f) == OK)
14044 rettv->vval.v_float = tan(f);
14045 else
14046 rettv->vval.v_float = 0.0;
14047}
14048
14049/*
14050 * "tanh()" function
14051 */
14052 static void
14053f_tanh(typval_T *argvars, typval_T *rettv)
14054{
14055 float_T f = 0.0;
14056
14057 rettv->v_type = VAR_FLOAT;
14058 if (get_float_arg(argvars, &f) == OK)
14059 rettv->vval.v_float = tanh(f);
14060 else
14061 rettv->vval.v_float = 0.0;
14062}
14063#endif
14064
14065/*
14066 * "test_alloc_fail(id, countdown, repeat)" function
14067 */
14068 static void
14069f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
14070{
14071 if (argvars[0].v_type != VAR_NUMBER
14072 || argvars[0].vval.v_number <= 0
14073 || argvars[1].v_type != VAR_NUMBER
14074 || argvars[1].vval.v_number < 0
14075 || argvars[2].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014076 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014077 else
14078 {
14079 alloc_fail_id = argvars[0].vval.v_number;
14080 if (alloc_fail_id >= aid_last)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014081 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014082 alloc_fail_countdown = argvars[1].vval.v_number;
14083 alloc_fail_repeat = argvars[2].vval.v_number;
14084 did_outofmem_msg = FALSE;
14085 }
14086}
14087
14088/*
14089 * "test_autochdir()"
14090 */
14091 static void
14092f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14093{
14094#if defined(FEAT_AUTOCHDIR)
14095 test_autochdir = TRUE;
14096#endif
14097}
14098
14099/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020014100 * "test_feedinput()"
14101 */
14102 static void
14103f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
14104{
14105#ifdef USE_INPUT_BUF
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014106 char_u *val = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5e80de32017-09-03 15:48:12 +020014107
14108 if (val != NULL)
14109 {
14110 trash_input_buf();
14111 add_to_input_buf_csi(val, (int)STRLEN(val));
14112 }
14113#endif
14114}
14115
14116/*
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014117 * "test_option_not_set({name})" function
14118 */
14119 static void
14120f_test_option_not_set(typval_T *argvars, typval_T *rettv UNUSED)
14121{
14122 char_u *name = (char_u *)"";
14123
14124 if (argvars[0].v_type != VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014125 emsg(_(e_invarg));
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014126 else
14127 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014128 name = tv_get_string(&argvars[0]);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014129 if (reset_option_was_set(name) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014130 semsg(_(e_invarg2), name);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014131 }
14132}
14133
14134/*
14135 * "test_override({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014136 */
14137 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014138f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014139{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014140 char_u *name = (char_u *)"";
14141 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014142 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014143
14144 if (argvars[0].v_type != VAR_STRING
14145 || (argvars[1].v_type) != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014146 emsg(_(e_invarg));
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014147 else
14148 {
Bram Moolenaare38197d2018-12-24 23:35:13 +010014149 name = tv_get_string(&argvars[0]);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014150 val = (int)tv_get_number(&argvars[1]);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014151
14152 if (STRCMP(name, (char_u *)"redraw") == 0)
14153 disable_redraw_for_testing = val;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020014154 else if (STRCMP(name, (char_u *)"redraw_flag") == 0)
14155 ignore_redraw_flag_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014156 else if (STRCMP(name, (char_u *)"char_avail") == 0)
14157 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014158 else if (STRCMP(name, (char_u *)"starting") == 0)
14159 {
14160 if (val)
14161 {
14162 if (save_starting < 0)
14163 save_starting = starting;
14164 starting = 0;
14165 }
14166 else
14167 {
14168 starting = save_starting;
14169 save_starting = -1;
14170 }
14171 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +020014172 else if (STRCMP(name, (char_u *)"nfa_fail") == 0)
14173 nfa_fail_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014174 else if (STRCMP(name, (char_u *)"ALL") == 0)
14175 {
14176 disable_char_avail_for_testing = FALSE;
14177 disable_redraw_for_testing = FALSE;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020014178 ignore_redraw_flag_for_testing = FALSE;
Bram Moolenaarbcf94422018-06-23 14:21:42 +020014179 nfa_fail_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014180 if (save_starting >= 0)
14181 {
14182 starting = save_starting;
14183 save_starting = -1;
14184 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014185 }
14186 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014187 semsg(_(e_invarg2), name);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014188 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014189}
14190
14191/*
Bram Moolenaarc3e92c12019-03-23 14:23:07 +010014192 * "test_refcount({expr})" function
14193 */
14194 static void
14195f_test_refcount(typval_T *argvars, typval_T *rettv)
14196{
14197 int retval = -1;
14198
14199 switch (argvars[0].v_type)
14200 {
14201 case VAR_UNKNOWN:
14202 case VAR_NUMBER:
14203 case VAR_FLOAT:
14204 case VAR_SPECIAL:
14205 case VAR_STRING:
14206 break;
14207 case VAR_JOB:
14208#ifdef FEAT_JOB_CHANNEL
14209 if (argvars[0].vval.v_job != NULL)
14210 retval = argvars[0].vval.v_job->jv_refcount - 1;
14211#endif
14212 break;
14213 case VAR_CHANNEL:
14214#ifdef FEAT_JOB_CHANNEL
14215 if (argvars[0].vval.v_channel != NULL)
14216 retval = argvars[0].vval.v_channel->ch_refcount - 1;
14217#endif
14218 break;
14219 case VAR_FUNC:
14220 if (argvars[0].vval.v_string != NULL)
14221 {
14222 ufunc_T *fp;
14223
14224 fp = find_func(argvars[0].vval.v_string);
14225 if (fp != NULL)
14226 retval = fp->uf_refcount;
14227 }
14228 break;
14229 case VAR_PARTIAL:
14230 if (argvars[0].vval.v_partial != NULL)
14231 retval = argvars[0].vval.v_partial->pt_refcount - 1;
14232 break;
14233 case VAR_BLOB:
14234 if (argvars[0].vval.v_blob != NULL)
14235 retval = argvars[0].vval.v_blob->bv_refcount - 1;
14236 break;
14237 case VAR_LIST:
14238 if (argvars[0].vval.v_list != NULL)
14239 retval = argvars[0].vval.v_list->lv_refcount - 1;
14240 break;
14241 case VAR_DICT:
14242 if (argvars[0].vval.v_dict != NULL)
14243 retval = argvars[0].vval.v_dict->dv_refcount - 1;
14244 break;
14245 }
14246
14247 rettv->v_type = VAR_NUMBER;
14248 rettv->vval.v_number = retval;
14249
14250}
14251
14252/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014253 * "test_garbagecollect_now()" function
14254 */
14255 static void
14256f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14257{
14258 /* This is dangerous, any Lists and Dicts used internally may be freed
14259 * while still in use. */
14260 garbage_collect(TRUE);
14261}
14262
Bram Moolenaare0c31f62017-03-01 15:07:05 +010014263/*
14264 * "test_ignore_error()" function
14265 */
14266 static void
14267f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
14268{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014269 ignore_error_for_testing(tv_get_string(&argvars[0]));
Bram Moolenaare0c31f62017-03-01 15:07:05 +010014270}
14271
Bram Moolenaarc0f5a782019-01-13 15:16:13 +010014272 static void
14273f_test_null_blob(typval_T *argvars UNUSED, typval_T *rettv)
14274{
14275 rettv->v_type = VAR_BLOB;
14276 rettv->vval.v_blob = NULL;
14277}
14278
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014279#ifdef FEAT_JOB_CHANNEL
14280 static void
14281f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
14282{
14283 rettv->v_type = VAR_CHANNEL;
14284 rettv->vval.v_channel = NULL;
14285}
14286#endif
14287
14288 static void
14289f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
14290{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014291 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014292}
14293
14294#ifdef FEAT_JOB_CHANNEL
14295 static void
14296f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
14297{
14298 rettv->v_type = VAR_JOB;
14299 rettv->vval.v_job = NULL;
14300}
14301#endif
14302
14303 static void
14304f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
14305{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014306 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014307}
14308
14309 static void
14310f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
14311{
14312 rettv->v_type = VAR_PARTIAL;
14313 rettv->vval.v_partial = NULL;
14314}
14315
14316 static void
14317f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
14318{
14319 rettv->v_type = VAR_STRING;
14320 rettv->vval.v_string = NULL;
14321}
14322
Bram Moolenaarab186732018-09-14 21:27:06 +020014323#ifdef FEAT_GUI
14324 static void
14325f_test_scrollbar(typval_T *argvars, typval_T *rettv UNUSED)
14326{
14327 char_u *which;
14328 long value;
14329 int dragging;
14330 scrollbar_T *sb = NULL;
14331
14332 if (argvars[0].v_type != VAR_STRING
14333 || (argvars[1].v_type) != VAR_NUMBER
14334 || (argvars[2].v_type) != VAR_NUMBER)
14335 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014336 emsg(_(e_invarg));
Bram Moolenaarab186732018-09-14 21:27:06 +020014337 return;
14338 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014339 which = tv_get_string(&argvars[0]);
14340 value = tv_get_number(&argvars[1]);
14341 dragging = tv_get_number(&argvars[2]);
Bram Moolenaarab186732018-09-14 21:27:06 +020014342
14343 if (STRCMP(which, "left") == 0)
14344 sb = &curwin->w_scrollbars[SBAR_LEFT];
14345 else if (STRCMP(which, "right") == 0)
14346 sb = &curwin->w_scrollbars[SBAR_RIGHT];
14347 else if (STRCMP(which, "hor") == 0)
14348 sb = &gui.bottom_sbar;
14349 if (sb == NULL)
14350 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014351 semsg(_(e_invarg2), which);
Bram Moolenaarab186732018-09-14 21:27:06 +020014352 return;
14353 }
14354 gui_drag_scrollbar(sb, value, dragging);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020014355# ifndef USE_ON_FLY_SCROLL
14356 // need to loop through normal_cmd() to handle the scroll events
14357 exec_normal(FALSE, TRUE, FALSE);
14358# endif
Bram Moolenaarab186732018-09-14 21:27:06 +020014359}
14360#endif
14361
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014362 static void
14363f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
14364{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014365 time_for_testing = (time_t)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014366}
14367
14368#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
14369/*
14370 * Get a callback from "arg". It can be a Funcref or a function name.
14371 * When "arg" is zero return an empty string.
14372 * Return NULL for an invalid argument.
14373 */
14374 char_u *
14375get_callback(typval_T *arg, partial_T **pp)
14376{
14377 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
14378 {
14379 *pp = arg->vval.v_partial;
14380 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020014381 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014382 }
14383 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020014384 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014385 {
14386 func_ref(arg->vval.v_string);
14387 return arg->vval.v_string;
14388 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014389 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
14390 return (char_u *)"";
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014391 emsg(_("E921: Invalid callback argument"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014392 return NULL;
14393}
14394
14395/*
Bram Moolenaard5359b22018-04-05 22:44:39 +020014396 * Unref/free "callback" and "partial" returned by get_callback().
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014397 */
14398 void
14399free_callback(char_u *callback, partial_T *partial)
14400{
14401 if (partial != NULL)
14402 partial_unref(partial);
14403 else if (callback != NULL)
14404 {
14405 func_unref(callback);
14406 vim_free(callback);
14407 }
14408}
14409#endif
14410
14411#ifdef FEAT_TIMERS
14412/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014413 * "timer_info([timer])" function
14414 */
14415 static void
14416f_timer_info(typval_T *argvars, typval_T *rettv)
14417{
14418 timer_T *timer = NULL;
14419
14420 if (rettv_list_alloc(rettv) != OK)
14421 return;
14422 if (argvars[0].v_type != VAR_UNKNOWN)
14423 {
14424 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014425 emsg(_(e_number_exp));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014426 else
14427 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014428 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014429 if (timer != NULL)
14430 add_timer_info(rettv, timer);
14431 }
14432 }
14433 else
14434 add_timer_info_all(rettv);
14435}
14436
14437/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014438 * "timer_pause(timer, paused)" function
14439 */
14440 static void
14441f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
14442{
14443 timer_T *timer = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014444 int paused = (int)tv_get_number(&argvars[1]);
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014445
14446 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014447 emsg(_(e_number_exp));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014448 else
14449 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014450 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014451 if (timer != NULL)
14452 timer->tr_paused = paused;
14453 }
14454}
14455
14456/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014457 * "timer_start(time, callback [, options])" function
14458 */
14459 static void
14460f_timer_start(typval_T *argvars, typval_T *rettv)
14461{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014462 long msec = (long)tv_get_number(&argvars[0]);
Bram Moolenaar75537a92016-09-05 22:45:28 +020014463 timer_T *timer;
14464 int repeat = 0;
14465 char_u *callback;
14466 dict_T *dict;
14467 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014468
Bram Moolenaar75537a92016-09-05 22:45:28 +020014469 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014470 if (check_secure())
14471 return;
14472 if (argvars[2].v_type != VAR_UNKNOWN)
14473 {
14474 if (argvars[2].v_type != VAR_DICT
14475 || (dict = argvars[2].vval.v_dict) == NULL)
14476 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014477 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014478 return;
14479 }
14480 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014481 repeat = dict_get_number(dict, (char_u *)"repeat");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014482 }
14483
Bram Moolenaar75537a92016-09-05 22:45:28 +020014484 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014485 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020014486 return;
14487
14488 timer = create_timer(msec, repeat);
14489 if (timer == NULL)
14490 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014491 else
14492 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020014493 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020014494 timer->tr_callback = vim_strsave(callback);
14495 else
14496 /* pointer into the partial */
14497 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020014498 timer->tr_partial = partial;
14499 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014500 }
14501}
14502
14503/*
14504 * "timer_stop(timer)" function
14505 */
14506 static void
14507f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
14508{
14509 timer_T *timer;
14510
14511 if (argvars[0].v_type != VAR_NUMBER)
14512 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014513 emsg(_(e_number_exp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014514 return;
14515 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014516 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014517 if (timer != NULL)
14518 stop_timer(timer);
14519}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014520
14521/*
14522 * "timer_stopall()" function
14523 */
14524 static void
14525f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14526{
14527 stop_all_timers();
14528}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014529#endif
14530
14531/*
14532 * "tolower(string)" function
14533 */
14534 static void
14535f_tolower(typval_T *argvars, typval_T *rettv)
14536{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014537 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014538 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014539}
14540
14541/*
14542 * "toupper(string)" function
14543 */
14544 static void
14545f_toupper(typval_T *argvars, typval_T *rettv)
14546{
14547 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014548 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014549}
14550
14551/*
14552 * "tr(string, fromstr, tostr)" function
14553 */
14554 static void
14555f_tr(typval_T *argvars, typval_T *rettv)
14556{
14557 char_u *in_str;
14558 char_u *fromstr;
14559 char_u *tostr;
14560 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014561 int inlen;
14562 int fromlen;
14563 int tolen;
14564 int idx;
14565 char_u *cpstr;
14566 int cplen;
14567 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014568 char_u buf[NUMBUFLEN];
14569 char_u buf2[NUMBUFLEN];
14570 garray_T ga;
14571
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014572 in_str = tv_get_string(&argvars[0]);
14573 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
14574 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014575
14576 /* Default return value: empty string. */
14577 rettv->v_type = VAR_STRING;
14578 rettv->vval.v_string = NULL;
14579 if (fromstr == NULL || tostr == NULL)
14580 return; /* type error; errmsg already given */
14581 ga_init2(&ga, (int)sizeof(char), 80);
14582
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014583 if (!has_mbyte)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014584 /* not multi-byte: fromstr and tostr must be the same length */
14585 if (STRLEN(fromstr) != STRLEN(tostr))
14586 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014587error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014588 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014589 ga_clear(&ga);
14590 return;
14591 }
14592
14593 /* fromstr and tostr have to contain the same number of chars */
14594 while (*in_str != NUL)
14595 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014596 if (has_mbyte)
14597 {
14598 inlen = (*mb_ptr2len)(in_str);
14599 cpstr = in_str;
14600 cplen = inlen;
14601 idx = 0;
14602 for (p = fromstr; *p != NUL; p += fromlen)
14603 {
14604 fromlen = (*mb_ptr2len)(p);
14605 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
14606 {
14607 for (p = tostr; *p != NUL; p += tolen)
14608 {
14609 tolen = (*mb_ptr2len)(p);
14610 if (idx-- == 0)
14611 {
14612 cplen = tolen;
14613 cpstr = p;
14614 break;
14615 }
14616 }
14617 if (*p == NUL) /* tostr is shorter than fromstr */
14618 goto error;
14619 break;
14620 }
14621 ++idx;
14622 }
14623
14624 if (first && cpstr == in_str)
14625 {
14626 /* Check that fromstr and tostr have the same number of
14627 * (multi-byte) characters. Done only once when a character
14628 * of in_str doesn't appear in fromstr. */
14629 first = FALSE;
14630 for (p = tostr; *p != NUL; p += tolen)
14631 {
14632 tolen = (*mb_ptr2len)(p);
14633 --idx;
14634 }
14635 if (idx != 0)
14636 goto error;
14637 }
14638
14639 (void)ga_grow(&ga, cplen);
14640 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
14641 ga.ga_len += cplen;
14642
14643 in_str += inlen;
14644 }
14645 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014646 {
14647 /* When not using multi-byte chars we can do it faster. */
14648 p = vim_strchr(fromstr, *in_str);
14649 if (p != NULL)
14650 ga_append(&ga, tostr[p - fromstr]);
14651 else
14652 ga_append(&ga, *in_str);
14653 ++in_str;
14654 }
14655 }
14656
14657 /* add a terminating NUL */
14658 (void)ga_grow(&ga, 1);
14659 ga_append(&ga, NUL);
14660
14661 rettv->vval.v_string = ga.ga_data;
14662}
14663
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014664/*
14665 * "trim({expr})" function
14666 */
14667 static void
14668f_trim(typval_T *argvars, typval_T *rettv)
14669{
14670 char_u buf1[NUMBUFLEN];
14671 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014672 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014673 char_u *mask = NULL;
14674 char_u *tail;
14675 char_u *prev;
14676 char_u *p;
14677 int c1;
14678
14679 rettv->v_type = VAR_STRING;
14680 if (head == NULL)
14681 {
14682 rettv->vval.v_string = NULL;
14683 return;
14684 }
14685
14686 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014687 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014688
14689 while (*head != NUL)
14690 {
14691 c1 = PTR2CHAR(head);
14692 if (mask == NULL)
14693 {
14694 if (c1 > ' ' && c1 != 0xa0)
14695 break;
14696 }
14697 else
14698 {
14699 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14700 if (c1 == PTR2CHAR(p))
14701 break;
14702 if (*p == NUL)
14703 break;
14704 }
14705 MB_PTR_ADV(head);
14706 }
14707
14708 for (tail = head + STRLEN(head); tail > head; tail = prev)
14709 {
14710 prev = tail;
14711 MB_PTR_BACK(head, prev);
14712 c1 = PTR2CHAR(prev);
14713 if (mask == NULL)
14714 {
14715 if (c1 > ' ' && c1 != 0xa0)
14716 break;
14717 }
14718 else
14719 {
14720 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14721 if (c1 == PTR2CHAR(p))
14722 break;
14723 if (*p == NUL)
14724 break;
14725 }
14726 }
14727 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
14728}
14729
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014730#ifdef FEAT_FLOAT
14731/*
14732 * "trunc({float})" function
14733 */
14734 static void
14735f_trunc(typval_T *argvars, typval_T *rettv)
14736{
14737 float_T f = 0.0;
14738
14739 rettv->v_type = VAR_FLOAT;
14740 if (get_float_arg(argvars, &f) == OK)
14741 /* trunc() is not in C90, use floor() or ceil() instead. */
14742 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
14743 else
14744 rettv->vval.v_float = 0.0;
14745}
14746#endif
14747
14748/*
14749 * "type(expr)" function
14750 */
14751 static void
14752f_type(typval_T *argvars, typval_T *rettv)
14753{
14754 int n = -1;
14755
14756 switch (argvars[0].v_type)
14757 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020014758 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
14759 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014760 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020014761 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
14762 case VAR_LIST: n = VAR_TYPE_LIST; break;
14763 case VAR_DICT: n = VAR_TYPE_DICT; break;
14764 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014765 case VAR_SPECIAL:
14766 if (argvars[0].vval.v_number == VVAL_FALSE
14767 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020014768 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014769 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020014770 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014771 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020014772 case VAR_JOB: n = VAR_TYPE_JOB; break;
14773 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014774 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014775 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010014776 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014777 n = -1;
14778 break;
14779 }
14780 rettv->vval.v_number = n;
14781}
14782
14783/*
14784 * "undofile(name)" function
14785 */
14786 static void
14787f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
14788{
14789 rettv->v_type = VAR_STRING;
14790#ifdef FEAT_PERSISTENT_UNDO
14791 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014792 char_u *fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014793
14794 if (*fname == NUL)
14795 {
14796 /* If there is no file name there will be no undo file. */
14797 rettv->vval.v_string = NULL;
14798 }
14799 else
14800 {
14801 char_u *ffname = FullName_save(fname, FALSE);
14802
14803 if (ffname != NULL)
14804 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
14805 vim_free(ffname);
14806 }
14807 }
14808#else
14809 rettv->vval.v_string = NULL;
14810#endif
14811}
14812
14813/*
14814 * "undotree()" function
14815 */
14816 static void
14817f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
14818{
14819 if (rettv_dict_alloc(rettv) == OK)
14820 {
14821 dict_T *dict = rettv->vval.v_dict;
14822 list_T *list;
14823
Bram Moolenaare0be1672018-07-08 16:50:37 +020014824 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
14825 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
14826 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
14827 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
14828 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
14829 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014830
14831 list = list_alloc();
14832 if (list != NULL)
14833 {
14834 u_eval_tree(curbuf->b_u_oldhead, list);
14835 dict_add_list(dict, "entries", list);
14836 }
14837 }
14838}
14839
14840/*
14841 * "values(dict)" function
14842 */
14843 static void
14844f_values(typval_T *argvars, typval_T *rettv)
14845{
14846 dict_list(argvars, rettv, 1);
14847}
14848
14849/*
14850 * "virtcol(string)" function
14851 */
14852 static void
14853f_virtcol(typval_T *argvars, typval_T *rettv)
14854{
14855 colnr_T vcol = 0;
14856 pos_T *fp;
14857 int fnum = curbuf->b_fnum;
14858
14859 fp = var2fpos(&argvars[0], FALSE, &fnum);
14860 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
14861 && fnum == curbuf->b_fnum)
14862 {
14863 getvvcol(curwin, fp, NULL, NULL, &vcol);
14864 ++vcol;
14865 }
14866
14867 rettv->vval.v_number = vcol;
14868}
14869
14870/*
14871 * "visualmode()" function
14872 */
14873 static void
14874f_visualmode(typval_T *argvars, typval_T *rettv)
14875{
14876 char_u str[2];
14877
14878 rettv->v_type = VAR_STRING;
14879 str[0] = curbuf->b_visual_mode_eval;
14880 str[1] = NUL;
14881 rettv->vval.v_string = vim_strsave(str);
14882
14883 /* A non-zero number or non-empty string argument: reset mode. */
14884 if (non_zero_arg(&argvars[0]))
14885 curbuf->b_visual_mode_eval = NUL;
14886}
14887
14888/*
14889 * "wildmenumode()" function
14890 */
14891 static void
14892f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14893{
14894#ifdef FEAT_WILDMENU
14895 if (wild_menu_showing)
14896 rettv->vval.v_number = 1;
14897#endif
14898}
14899
14900/*
14901 * "winbufnr(nr)" function
14902 */
14903 static void
14904f_winbufnr(typval_T *argvars, typval_T *rettv)
14905{
14906 win_T *wp;
14907
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014908 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014909 if (wp == NULL)
14910 rettv->vval.v_number = -1;
14911 else
14912 rettv->vval.v_number = wp->w_buffer->b_fnum;
14913}
14914
14915/*
14916 * "wincol()" function
14917 */
14918 static void
14919f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
14920{
14921 validate_cursor();
14922 rettv->vval.v_number = curwin->w_wcol + 1;
14923}
14924
14925/*
14926 * "winheight(nr)" function
14927 */
14928 static void
14929f_winheight(typval_T *argvars, typval_T *rettv)
14930{
14931 win_T *wp;
14932
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014933 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014934 if (wp == NULL)
14935 rettv->vval.v_number = -1;
14936 else
14937 rettv->vval.v_number = wp->w_height;
14938}
14939
14940/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020014941 * "winlayout()" function
14942 */
14943 static void
14944f_winlayout(typval_T *argvars, typval_T *rettv)
14945{
14946 tabpage_T *tp;
14947
14948 if (rettv_list_alloc(rettv) != OK)
14949 return;
14950
14951 if (argvars[0].v_type == VAR_UNKNOWN)
14952 tp = curtab;
14953 else
14954 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014955 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020014956 if (tp == NULL)
14957 return;
14958 }
14959
14960 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
14961}
14962
14963/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014964 * "winline()" function
14965 */
14966 static void
14967f_winline(typval_T *argvars UNUSED, typval_T *rettv)
14968{
14969 validate_cursor();
14970 rettv->vval.v_number = curwin->w_wrow + 1;
14971}
14972
14973/*
14974 * "winnr()" function
14975 */
14976 static void
14977f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
14978{
14979 int nr = 1;
14980
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014981 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014982 rettv->vval.v_number = nr;
14983}
14984
14985/*
14986 * "winrestcmd()" function
14987 */
14988 static void
14989f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
14990{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014991 win_T *wp;
14992 int winnr = 1;
14993 garray_T ga;
14994 char_u buf[50];
14995
14996 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020014997 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014998 {
14999 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
15000 ga_concat(&ga, buf);
15001 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
15002 ga_concat(&ga, buf);
15003 ++winnr;
15004 }
15005 ga_append(&ga, NUL);
15006
15007 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015008 rettv->v_type = VAR_STRING;
15009}
15010
15011/*
15012 * "winrestview()" function
15013 */
15014 static void
15015f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
15016{
15017 dict_T *dict;
15018
15019 if (argvars[0].v_type != VAR_DICT
15020 || (dict = argvars[0].vval.v_dict) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015021 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015022 else
15023 {
15024 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015025 curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015026 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015027 curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015028 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015029 curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015030 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
15031 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010015032 curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015033 curwin->w_set_curswant = FALSE;
15034 }
15035
15036 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015037 set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015038#ifdef FEAT_DIFF
15039 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015040 curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015041#endif
15042 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015043 curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015044 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015045 curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015046
15047 check_cursor();
15048 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020015049 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015050 changed_window_setting();
15051
15052 if (curwin->w_topline <= 0)
15053 curwin->w_topline = 1;
15054 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
15055 curwin->w_topline = curbuf->b_ml.ml_line_count;
15056#ifdef FEAT_DIFF
15057 check_topfill(curwin, TRUE);
15058#endif
15059 }
15060}
15061
15062/*
15063 * "winsaveview()" function
15064 */
15065 static void
15066f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
15067{
15068 dict_T *dict;
15069
15070 if (rettv_dict_alloc(rettv) == FAIL)
15071 return;
15072 dict = rettv->vval.v_dict;
15073
Bram Moolenaare0be1672018-07-08 16:50:37 +020015074 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
15075 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +020015076 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015077 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020015078 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015079
Bram Moolenaare0be1672018-07-08 16:50:37 +020015080 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015081#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020015082 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015083#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020015084 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
15085 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015086}
15087
15088/*
15089 * "winwidth(nr)" function
15090 */
15091 static void
15092f_winwidth(typval_T *argvars, typval_T *rettv)
15093{
15094 win_T *wp;
15095
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015096 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015097 if (wp == NULL)
15098 rettv->vval.v_number = -1;
15099 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015100 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015101}
15102
15103/*
15104 * "wordcount()" function
15105 */
15106 static void
15107f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
15108{
15109 if (rettv_dict_alloc(rettv) == FAIL)
15110 return;
15111 cursor_pos_info(rettv->vval.v_dict);
15112}
15113
15114/*
15115 * "writefile()" function
15116 */
15117 static void
15118f_writefile(typval_T *argvars, typval_T *rettv)
15119{
15120 int binary = FALSE;
15121 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015122#ifdef HAVE_FSYNC
15123 int do_fsync = p_fs;
15124#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015125 char_u *fname;
15126 FILE *fd;
15127 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015128 listitem_T *li;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015129 list_T *list = NULL;
15130 blob_T *blob = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015131
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015132 rettv->vval.v_number = -1;
Bram Moolenaar8c62a082019-02-08 14:34:10 +010015133 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015134 return;
15135
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015136 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015137 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015138 list = argvars[0].vval.v_list;
15139 if (list == NULL)
15140 return;
15141 for (li = list->lv_first; li != NULL; li = li->li_next)
15142 if (tv_get_string_chk(&li->li_tv) == NULL)
15143 return;
15144 }
15145 else if (argvars[0].v_type == VAR_BLOB)
15146 {
15147 blob = argvars[0].vval.v_blob;
15148 if (blob == NULL)
15149 return;
15150 }
15151 else
15152 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015153 semsg(_(e_invarg2), "writefile()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015154 return;
15155 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015156
15157 if (argvars[2].v_type != VAR_UNKNOWN)
15158 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015159 char_u *arg2 = tv_get_string_chk(&argvars[2]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015160
15161 if (arg2 == NULL)
15162 return;
15163 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015164 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015165 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015166 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015167#ifdef HAVE_FSYNC
15168 if (vim_strchr(arg2, 's') != NULL)
15169 do_fsync = TRUE;
15170 else if (vim_strchr(arg2, 'S') != NULL)
15171 do_fsync = FALSE;
15172#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015173 }
15174
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015175 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015176 if (fname == NULL)
15177 return;
15178
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015179 /* Always open the file in binary mode, library functions have a mind of
15180 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015181 if (*fname == NUL || (fd = mch_fopen((char *)fname,
15182 append ? APPENDBIN : WRITEBIN)) == NULL)
15183 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015184 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015185 ret = -1;
15186 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015187 else if (blob)
15188 {
15189 if (write_blob(fd, blob) == FAIL)
15190 ret = -1;
15191#ifdef HAVE_FSYNC
15192 else if (do_fsync)
15193 // Ignore the error, the user wouldn't know what to do about it.
15194 // May happen for a device.
Bram Moolenaara7870192019-02-14 12:56:36 +010015195 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015196#endif
15197 fclose(fd);
15198 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015199 else
15200 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015201 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015202 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015203#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010015204 else if (do_fsync)
15205 /* Ignore the error, the user wouldn't know what to do about it.
15206 * May happen for a device. */
Bram Moolenaara7870192019-02-14 12:56:36 +010015207 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015208#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015209 fclose(fd);
15210 }
15211
15212 rettv->vval.v_number = ret;
15213}
15214
15215/*
15216 * "xor(expr, expr)" function
15217 */
15218 static void
15219f_xor(typval_T *argvars, typval_T *rettv)
15220{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015221 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
15222 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015223}
15224
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015225#endif /* FEAT_EVAL */