blob: e693ef9137b9a8a6301454e06e7b82be95a48211 [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 Moolenaar73dad1e2016-07-17 22:13:49 +020034
35#ifdef FEAT_FLOAT
36static void f_abs(typval_T *argvars, typval_T *rettv);
37static void f_acos(typval_T *argvars, typval_T *rettv);
38#endif
39static void f_add(typval_T *argvars, typval_T *rettv);
40static void f_and(typval_T *argvars, typval_T *rettv);
41static void f_append(typval_T *argvars, typval_T *rettv);
Bram Moolenaarca851592018-06-06 21:04:07 +020042static void f_appendbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020043static void f_argc(typval_T *argvars, typval_T *rettv);
44static void f_argidx(typval_T *argvars, typval_T *rettv);
45static void f_arglistid(typval_T *argvars, typval_T *rettv);
46static void f_argv(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +010047static void f_assert_beeps(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020048static void f_assert_equal(typval_T *argvars, typval_T *rettv);
Bram Moolenaard96ff162018-02-18 22:13:29 +010049static void f_assert_equalfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020050static void f_assert_exception(typval_T *argvars, typval_T *rettv);
51static void f_assert_fails(typval_T *argvars, typval_T *rettv);
52static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaar61c04492016-07-23 15:35:35 +020053static void f_assert_inrange(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020054static void f_assert_match(typval_T *argvars, typval_T *rettv);
55static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
56static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar42205552017-03-18 19:42:22 +010057static void f_assert_report(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020058static void f_assert_true(typval_T *argvars, typval_T *rettv);
59#ifdef FEAT_FLOAT
60static void f_asin(typval_T *argvars, typval_T *rettv);
61static void f_atan(typval_T *argvars, typval_T *rettv);
62static void f_atan2(typval_T *argvars, typval_T *rettv);
63#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010064#ifdef FEAT_BEVAL
65static void f_balloon_show(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010066# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010067static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010068# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010069#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020070static void f_browse(typval_T *argvars, typval_T *rettv);
71static void f_browsedir(typval_T *argvars, typval_T *rettv);
72static void f_bufexists(typval_T *argvars, typval_T *rettv);
73static void f_buflisted(typval_T *argvars, typval_T *rettv);
74static void f_bufloaded(typval_T *argvars, typval_T *rettv);
75static void f_bufname(typval_T *argvars, typval_T *rettv);
76static void f_bufnr(typval_T *argvars, typval_T *rettv);
77static void f_bufwinid(typval_T *argvars, typval_T *rettv);
78static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
79static void f_byte2line(typval_T *argvars, typval_T *rettv);
80static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
81static void f_byteidx(typval_T *argvars, typval_T *rettv);
82static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
83static void f_call(typval_T *argvars, typval_T *rettv);
84#ifdef FEAT_FLOAT
85static void f_ceil(typval_T *argvars, typval_T *rettv);
86#endif
87#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +010088static void f_ch_canread(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020089static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0874a832016-09-01 15:11:51 +020090static void f_ch_close_in(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020091static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
92static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
93static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
94static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
95static void f_ch_info(typval_T *argvars, typval_T *rettv);
96static void f_ch_log(typval_T *argvars, typval_T *rettv);
97static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
98static void f_ch_open(typval_T *argvars, typval_T *rettv);
99static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100100static void f_ch_readblob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200101static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
102static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
103static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
104static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
105static void f_ch_status(typval_T *argvars, typval_T *rettv);
106#endif
107static void f_changenr(typval_T *argvars, typval_T *rettv);
108static void f_char2nr(typval_T *argvars, typval_T *rettv);
109static void f_cindent(typval_T *argvars, typval_T *rettv);
110static void f_clearmatches(typval_T *argvars, typval_T *rettv);
111static void f_col(typval_T *argvars, typval_T *rettv);
112#if defined(FEAT_INS_EXPAND)
113static void f_complete(typval_T *argvars, typval_T *rettv);
114static void f_complete_add(typval_T *argvars, typval_T *rettv);
115static void f_complete_check(typval_T *argvars, typval_T *rettv);
116#endif
117static void f_confirm(typval_T *argvars, typval_T *rettv);
118static void f_copy(typval_T *argvars, typval_T *rettv);
119#ifdef FEAT_FLOAT
120static void f_cos(typval_T *argvars, typval_T *rettv);
121static void f_cosh(typval_T *argvars, typval_T *rettv);
122#endif
123static void f_count(typval_T *argvars, typval_T *rettv);
124static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
125static void f_cursor(typval_T *argsvars, typval_T *rettv);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200126#ifdef WIN3264
127static void f_debugbreak(typval_T *argvars, typval_T *rettv);
128#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200129static void f_deepcopy(typval_T *argvars, typval_T *rettv);
130static void f_delete(typval_T *argvars, typval_T *rettv);
Bram Moolenaard79a2622018-06-07 18:17:46 +0200131static void f_deletebufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200132static void f_did_filetype(typval_T *argvars, typval_T *rettv);
133static void f_diff_filler(typval_T *argvars, typval_T *rettv);
134static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
135static void f_empty(typval_T *argvars, typval_T *rettv);
136static void f_escape(typval_T *argvars, typval_T *rettv);
137static void f_eval(typval_T *argvars, typval_T *rettv);
138static void f_eventhandler(typval_T *argvars, typval_T *rettv);
139static void f_executable(typval_T *argvars, typval_T *rettv);
140static void f_execute(typval_T *argvars, typval_T *rettv);
141static void f_exepath(typval_T *argvars, typval_T *rettv);
142static void f_exists(typval_T *argvars, typval_T *rettv);
143#ifdef FEAT_FLOAT
144static void f_exp(typval_T *argvars, typval_T *rettv);
145#endif
146static void f_expand(typval_T *argvars, typval_T *rettv);
147static void f_extend(typval_T *argvars, typval_T *rettv);
148static void f_feedkeys(typval_T *argvars, typval_T *rettv);
149static void f_filereadable(typval_T *argvars, typval_T *rettv);
150static void f_filewritable(typval_T *argvars, typval_T *rettv);
151static void f_filter(typval_T *argvars, typval_T *rettv);
152static void f_finddir(typval_T *argvars, typval_T *rettv);
153static void f_findfile(typval_T *argvars, typval_T *rettv);
154#ifdef FEAT_FLOAT
155static void f_float2nr(typval_T *argvars, typval_T *rettv);
156static void f_floor(typval_T *argvars, typval_T *rettv);
157static void f_fmod(typval_T *argvars, typval_T *rettv);
158#endif
159static void f_fnameescape(typval_T *argvars, typval_T *rettv);
160static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
161static void f_foldclosed(typval_T *argvars, typval_T *rettv);
162static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
163static void f_foldlevel(typval_T *argvars, typval_T *rettv);
164static void f_foldtext(typval_T *argvars, typval_T *rettv);
165static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
166static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200167static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200168static void f_function(typval_T *argvars, typval_T *rettv);
169static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
170static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200171static void f_getbufinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200172static void f_getbufline(typval_T *argvars, typval_T *rettv);
173static void f_getbufvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100174static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200175static void f_getchar(typval_T *argvars, typval_T *rettv);
176static void f_getcharmod(typval_T *argvars, typval_T *rettv);
177static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
178static void f_getcmdline(typval_T *argvars, typval_T *rettv);
179#if defined(FEAT_CMDL_COMPL)
180static void f_getcompletion(typval_T *argvars, typval_T *rettv);
181#endif
182static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
183static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
184static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
185static void f_getcwd(typval_T *argvars, typval_T *rettv);
186static void f_getfontname(typval_T *argvars, typval_T *rettv);
187static void f_getfperm(typval_T *argvars, typval_T *rettv);
188static void f_getfsize(typval_T *argvars, typval_T *rettv);
189static void f_getftime(typval_T *argvars, typval_T *rettv);
190static void f_getftype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +0100191static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200192static void f_getline(typval_T *argvars, typval_T *rettv);
Bram Moolenaard823fa92016-08-12 16:29:27 +0200193static void f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200194static void f_getmatches(typval_T *argvars, typval_T *rettv);
195static void f_getpid(typval_T *argvars, typval_T *rettv);
196static void f_getcurpos(typval_T *argvars, typval_T *rettv);
197static void f_getpos(typval_T *argvars, typval_T *rettv);
198static void f_getqflist(typval_T *argvars, typval_T *rettv);
199static void f_getreg(typval_T *argvars, typval_T *rettv);
200static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200201static void f_gettabinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200202static void f_gettabvar(typval_T *argvars, typval_T *rettv);
203static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100204static void f_gettagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200205static void f_getwininfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100206static void f_getwinpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200207static void f_getwinposx(typval_T *argvars, typval_T *rettv);
208static void f_getwinposy(typval_T *argvars, typval_T *rettv);
209static void f_getwinvar(typval_T *argvars, typval_T *rettv);
210static void f_glob(typval_T *argvars, typval_T *rettv);
211static void f_globpath(typval_T *argvars, typval_T *rettv);
212static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
213static void f_has(typval_T *argvars, typval_T *rettv);
214static void f_has_key(typval_T *argvars, typval_T *rettv);
215static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
216static void f_hasmapto(typval_T *argvars, typval_T *rettv);
217static void f_histadd(typval_T *argvars, typval_T *rettv);
218static void f_histdel(typval_T *argvars, typval_T *rettv);
219static void f_histget(typval_T *argvars, typval_T *rettv);
220static void f_histnr(typval_T *argvars, typval_T *rettv);
221static void f_hlID(typval_T *argvars, typval_T *rettv);
222static void f_hlexists(typval_T *argvars, typval_T *rettv);
223static void f_hostname(typval_T *argvars, typval_T *rettv);
224static void f_iconv(typval_T *argvars, typval_T *rettv);
225static void f_indent(typval_T *argvars, typval_T *rettv);
226static void f_index(typval_T *argvars, typval_T *rettv);
227static void f_input(typval_T *argvars, typval_T *rettv);
228static void f_inputdialog(typval_T *argvars, typval_T *rettv);
229static void f_inputlist(typval_T *argvars, typval_T *rettv);
230static void f_inputrestore(typval_T *argvars, typval_T *rettv);
231static void f_inputsave(typval_T *argvars, typval_T *rettv);
232static void f_inputsecret(typval_T *argvars, typval_T *rettv);
233static void f_insert(typval_T *argvars, typval_T *rettv);
234static void f_invert(typval_T *argvars, typval_T *rettv);
235static void f_isdirectory(typval_T *argvars, typval_T *rettv);
236static void f_islocked(typval_T *argvars, typval_T *rettv);
237#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
238static void f_isnan(typval_T *argvars, typval_T *rettv);
239#endif
240static void f_items(typval_T *argvars, typval_T *rettv);
241#ifdef FEAT_JOB_CHANNEL
242static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
243static void f_job_info(typval_T *argvars, typval_T *rettv);
244static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
245static void f_job_start(typval_T *argvars, typval_T *rettv);
246static void f_job_stop(typval_T *argvars, typval_T *rettv);
247static void f_job_status(typval_T *argvars, typval_T *rettv);
248#endif
249static void f_join(typval_T *argvars, typval_T *rettv);
250static void f_js_decode(typval_T *argvars, typval_T *rettv);
251static void f_js_encode(typval_T *argvars, typval_T *rettv);
252static void f_json_decode(typval_T *argvars, typval_T *rettv);
253static void f_json_encode(typval_T *argvars, typval_T *rettv);
254static void f_keys(typval_T *argvars, typval_T *rettv);
255static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
256static void f_len(typval_T *argvars, typval_T *rettv);
257static void f_libcall(typval_T *argvars, typval_T *rettv);
258static void f_libcallnr(typval_T *argvars, typval_T *rettv);
259static void f_line(typval_T *argvars, typval_T *rettv);
260static void f_line2byte(typval_T *argvars, typval_T *rettv);
261static void f_lispindent(typval_T *argvars, typval_T *rettv);
262static void f_localtime(typval_T *argvars, typval_T *rettv);
263#ifdef FEAT_FLOAT
264static void f_log(typval_T *argvars, typval_T *rettv);
265static void f_log10(typval_T *argvars, typval_T *rettv);
266#endif
267#ifdef FEAT_LUA
268static void f_luaeval(typval_T *argvars, typval_T *rettv);
269#endif
270static void f_map(typval_T *argvars, typval_T *rettv);
271static void f_maparg(typval_T *argvars, typval_T *rettv);
272static void f_mapcheck(typval_T *argvars, typval_T *rettv);
273static void f_match(typval_T *argvars, typval_T *rettv);
274static void f_matchadd(typval_T *argvars, typval_T *rettv);
275static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
276static void f_matcharg(typval_T *argvars, typval_T *rettv);
277static void f_matchdelete(typval_T *argvars, typval_T *rettv);
278static void f_matchend(typval_T *argvars, typval_T *rettv);
279static void f_matchlist(typval_T *argvars, typval_T *rettv);
280static void f_matchstr(typval_T *argvars, typval_T *rettv);
281static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
282static void f_max(typval_T *argvars, typval_T *rettv);
283static void f_min(typval_T *argvars, typval_T *rettv);
284#ifdef vim_mkdir
285static void f_mkdir(typval_T *argvars, typval_T *rettv);
286#endif
287static void f_mode(typval_T *argvars, typval_T *rettv);
288#ifdef FEAT_MZSCHEME
289static void f_mzeval(typval_T *argvars, typval_T *rettv);
290#endif
291static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
292static void f_nr2char(typval_T *argvars, typval_T *rettv);
293static void f_or(typval_T *argvars, typval_T *rettv);
294static void f_pathshorten(typval_T *argvars, typval_T *rettv);
295#ifdef FEAT_PERL
296static void f_perleval(typval_T *argvars, typval_T *rettv);
297#endif
298#ifdef FEAT_FLOAT
299static void f_pow(typval_T *argvars, typval_T *rettv);
300#endif
301static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
302static void f_printf(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200303#ifdef FEAT_JOB_CHANNEL
304static void f_prompt_setcallback(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200305static void f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200306static void f_prompt_setprompt(typval_T *argvars, typval_T *rettv);
307#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200308static void f_pumvisible(typval_T *argvars, typval_T *rettv);
309#ifdef FEAT_PYTHON3
310static void f_py3eval(typval_T *argvars, typval_T *rettv);
311#endif
312#ifdef FEAT_PYTHON
313static void f_pyeval(typval_T *argvars, typval_T *rettv);
314#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100315#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
316static void f_pyxeval(typval_T *argvars, typval_T *rettv);
317#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200318static void f_range(typval_T *argvars, typval_T *rettv);
319static void f_readfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200320static void f_reg_executing(typval_T *argvars, typval_T *rettv);
321static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200322static void f_reltime(typval_T *argvars, typval_T *rettv);
323#ifdef FEAT_FLOAT
324static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
325#endif
326static void f_reltimestr(typval_T *argvars, typval_T *rettv);
327static void f_remote_expr(typval_T *argvars, typval_T *rettv);
328static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
329static void f_remote_peek(typval_T *argvars, typval_T *rettv);
330static void f_remote_read(typval_T *argvars, typval_T *rettv);
331static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100332static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200333static void f_remove(typval_T *argvars, typval_T *rettv);
334static void f_rename(typval_T *argvars, typval_T *rettv);
335static void f_repeat(typval_T *argvars, typval_T *rettv);
336static void f_resolve(typval_T *argvars, typval_T *rettv);
337static void f_reverse(typval_T *argvars, typval_T *rettv);
338#ifdef FEAT_FLOAT
339static void f_round(typval_T *argvars, typval_T *rettv);
340#endif
341static void f_screenattr(typval_T *argvars, typval_T *rettv);
342static void f_screenchar(typval_T *argvars, typval_T *rettv);
343static void f_screencol(typval_T *argvars, typval_T *rettv);
344static void f_screenrow(typval_T *argvars, typval_T *rettv);
345static void f_search(typval_T *argvars, typval_T *rettv);
346static void f_searchdecl(typval_T *argvars, typval_T *rettv);
347static void f_searchpair(typval_T *argvars, typval_T *rettv);
348static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
349static void f_searchpos(typval_T *argvars, typval_T *rettv);
350static void f_server2client(typval_T *argvars, typval_T *rettv);
351static void f_serverlist(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200352static void f_setbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200353static void f_setbufvar(typval_T *argvars, typval_T *rettv);
354static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
355static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
356static void f_setfperm(typval_T *argvars, typval_T *rettv);
357static void f_setline(typval_T *argvars, typval_T *rettv);
358static void f_setloclist(typval_T *argvars, typval_T *rettv);
359static void f_setmatches(typval_T *argvars, typval_T *rettv);
360static void f_setpos(typval_T *argvars, typval_T *rettv);
361static void f_setqflist(typval_T *argvars, typval_T *rettv);
362static void f_setreg(typval_T *argvars, typval_T *rettv);
363static void f_settabvar(typval_T *argvars, typval_T *rettv);
364static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100365static void f_settagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200366static void f_setwinvar(typval_T *argvars, typval_T *rettv);
367#ifdef FEAT_CRYPT
368static void f_sha256(typval_T *argvars, typval_T *rettv);
369#endif /* FEAT_CRYPT */
370static void f_shellescape(typval_T *argvars, typval_T *rettv);
371static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
Bram Moolenaar162b7142018-12-21 15:17:36 +0100372#ifdef FEAT_SIGNS
373static void f_sign_define(typval_T *argvars, typval_T *rettv);
374static void f_sign_getdefined(typval_T *argvars, typval_T *rettv);
375static void f_sign_getplaced(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100376static void f_sign_jump(typval_T *argvars, typval_T *rettv);
Bram Moolenaar162b7142018-12-21 15:17:36 +0100377static void f_sign_place(typval_T *argvars, typval_T *rettv);
378static void f_sign_undefine(typval_T *argvars, typval_T *rettv);
379static void f_sign_unplace(typval_T *argvars, typval_T *rettv);
380#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200381static void f_simplify(typval_T *argvars, typval_T *rettv);
382#ifdef FEAT_FLOAT
383static void f_sin(typval_T *argvars, typval_T *rettv);
384static void f_sinh(typval_T *argvars, typval_T *rettv);
385#endif
386static void f_sort(typval_T *argvars, typval_T *rettv);
387static void f_soundfold(typval_T *argvars, typval_T *rettv);
388static void f_spellbadword(typval_T *argvars, typval_T *rettv);
389static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
390static void f_split(typval_T *argvars, typval_T *rettv);
391#ifdef FEAT_FLOAT
392static void f_sqrt(typval_T *argvars, typval_T *rettv);
393static void f_str2float(typval_T *argvars, typval_T *rettv);
394#endif
395static void f_str2nr(typval_T *argvars, typval_T *rettv);
396static void f_strchars(typval_T *argvars, typval_T *rettv);
397#ifdef HAVE_STRFTIME
398static void f_strftime(typval_T *argvars, typval_T *rettv);
399#endif
400static void f_strgetchar(typval_T *argvars, typval_T *rettv);
401static void f_stridx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200402static void f_strlen(typval_T *argvars, typval_T *rettv);
403static void f_strcharpart(typval_T *argvars, typval_T *rettv);
404static void f_strpart(typval_T *argvars, typval_T *rettv);
405static void f_strridx(typval_T *argvars, typval_T *rettv);
406static void f_strtrans(typval_T *argvars, typval_T *rettv);
407static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
408static void f_strwidth(typval_T *argvars, typval_T *rettv);
409static void f_submatch(typval_T *argvars, typval_T *rettv);
410static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200411static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200412static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200413static void f_synID(typval_T *argvars, typval_T *rettv);
414static void f_synIDattr(typval_T *argvars, typval_T *rettv);
415static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
416static void f_synstack(typval_T *argvars, typval_T *rettv);
417static void f_synconcealed(typval_T *argvars, typval_T *rettv);
418static void f_system(typval_T *argvars, typval_T *rettv);
419static void f_systemlist(typval_T *argvars, typval_T *rettv);
420static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
421static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
422static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
423static void f_taglist(typval_T *argvars, typval_T *rettv);
424static void f_tagfiles(typval_T *argvars, typval_T *rettv);
425static void f_tempname(typval_T *argvars, typval_T *rettv);
426static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
427static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200428static void f_test_feedinput(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200429static void f_test_option_not_set(typval_T *argvars, typval_T *rettv);
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100430static void f_test_override(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200431static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100432static void f_test_ignore_error(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100433static void f_test_null_blob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200434#ifdef FEAT_JOB_CHANNEL
435static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
436#endif
437static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
438#ifdef FEAT_JOB_CHANNEL
439static void f_test_null_job(typval_T *argvars, typval_T *rettv);
440#endif
441static void f_test_null_list(typval_T *argvars, typval_T *rettv);
442static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
443static void f_test_null_string(typval_T *argvars, typval_T *rettv);
Bram Moolenaarab186732018-09-14 21:27:06 +0200444#ifdef FEAT_GUI
445static void f_test_scrollbar(typval_T *argvars, typval_T *rettv);
446#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200447static void f_test_settime(typval_T *argvars, typval_T *rettv);
448#ifdef FEAT_FLOAT
449static void f_tan(typval_T *argvars, typval_T *rettv);
450static void f_tanh(typval_T *argvars, typval_T *rettv);
451#endif
452#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200453static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200454static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200455static void f_timer_start(typval_T *argvars, typval_T *rettv);
456static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200457static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200458#endif
459static void f_tolower(typval_T *argvars, typval_T *rettv);
460static void f_toupper(typval_T *argvars, typval_T *rettv);
461static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100462static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200463#ifdef FEAT_FLOAT
464static void f_trunc(typval_T *argvars, typval_T *rettv);
465#endif
466static void f_type(typval_T *argvars, typval_T *rettv);
467static void f_undofile(typval_T *argvars, typval_T *rettv);
468static void f_undotree(typval_T *argvars, typval_T *rettv);
469static void f_uniq(typval_T *argvars, typval_T *rettv);
470static void f_values(typval_T *argvars, typval_T *rettv);
471static void f_virtcol(typval_T *argvars, typval_T *rettv);
472static void f_visualmode(typval_T *argvars, typval_T *rettv);
473static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
474static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
475static void f_win_getid(typval_T *argvars, typval_T *rettv);
476static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
477static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
478static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100479static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200480static void f_winbufnr(typval_T *argvars, typval_T *rettv);
481static void f_wincol(typval_T *argvars, typval_T *rettv);
482static void f_winheight(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200483static void f_winlayout(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200484static void f_winline(typval_T *argvars, typval_T *rettv);
485static void f_winnr(typval_T *argvars, typval_T *rettv);
486static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
487static void f_winrestview(typval_T *argvars, typval_T *rettv);
488static void f_winsaveview(typval_T *argvars, typval_T *rettv);
489static void f_winwidth(typval_T *argvars, typval_T *rettv);
490static void f_writefile(typval_T *argvars, typval_T *rettv);
491static void f_wordcount(typval_T *argvars, typval_T *rettv);
492static void f_xor(typval_T *argvars, typval_T *rettv);
493
494/*
495 * Array with names and number of arguments of all internal functions
496 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
497 */
498static struct fst
499{
500 char *f_name; /* function name */
501 char f_min_argc; /* minimal number of arguments */
502 char f_max_argc; /* maximal number of arguments */
503 void (*f_func)(typval_T *args, typval_T *rvar);
504 /* implementation of function */
505} functions[] =
506{
507#ifdef FEAT_FLOAT
508 {"abs", 1, 1, f_abs},
509 {"acos", 1, 1, f_acos}, /* WJMc */
510#endif
511 {"add", 2, 2, f_add},
512 {"and", 2, 2, f_and},
513 {"append", 2, 2, f_append},
Bram Moolenaarca851592018-06-06 21:04:07 +0200514 {"appendbufline", 3, 3, f_appendbufline},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200515 {"argc", 0, 1, f_argc},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200516 {"argidx", 0, 0, f_argidx},
517 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200518 {"argv", 0, 2, f_argv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200519#ifdef FEAT_FLOAT
520 {"asin", 1, 1, f_asin}, /* WJMc */
521#endif
Bram Moolenaarb48e96f2018-02-13 12:26:14 +0100522 {"assert_beeps", 1, 2, f_assert_beeps},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200523 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaard96ff162018-02-18 22:13:29 +0100524 {"assert_equalfile", 2, 2, f_assert_equalfile},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200525 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaar1307d1c2018-10-07 20:16:49 +0200526 {"assert_fails", 1, 3, f_assert_fails},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200527 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100528 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200529 {"assert_match", 2, 3, f_assert_match},
530 {"assert_notequal", 2, 3, f_assert_notequal},
531 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100532 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200533 {"assert_true", 1, 2, f_assert_true},
534#ifdef FEAT_FLOAT
535 {"atan", 1, 1, f_atan},
536 {"atan2", 2, 2, f_atan2},
537#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100538#ifdef FEAT_BEVAL
539 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100540# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100541 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100542# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100543#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200544 {"browse", 4, 4, f_browse},
545 {"browsedir", 2, 2, f_browsedir},
546 {"bufexists", 1, 1, f_bufexists},
547 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
548 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
549 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
550 {"buflisted", 1, 1, f_buflisted},
551 {"bufloaded", 1, 1, f_bufloaded},
552 {"bufname", 1, 1, f_bufname},
553 {"bufnr", 1, 2, f_bufnr},
554 {"bufwinid", 1, 1, f_bufwinid},
555 {"bufwinnr", 1, 1, f_bufwinnr},
556 {"byte2line", 1, 1, f_byte2line},
557 {"byteidx", 2, 2, f_byteidx},
558 {"byteidxcomp", 2, 2, f_byteidxcomp},
559 {"call", 2, 3, f_call},
560#ifdef FEAT_FLOAT
561 {"ceil", 1, 1, f_ceil},
562#endif
563#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100564 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200565 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200566 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200567 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
568 {"ch_evalraw", 2, 3, f_ch_evalraw},
569 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
570 {"ch_getjob", 1, 1, f_ch_getjob},
571 {"ch_info", 1, 1, f_ch_info},
572 {"ch_log", 1, 2, f_ch_log},
573 {"ch_logfile", 1, 2, f_ch_logfile},
574 {"ch_open", 1, 2, f_ch_open},
575 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100576 {"ch_readblob", 1, 2, f_ch_readblob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200577 {"ch_readraw", 1, 2, f_ch_readraw},
578 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
579 {"ch_sendraw", 2, 3, f_ch_sendraw},
580 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200581 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200582#endif
583 {"changenr", 0, 0, f_changenr},
584 {"char2nr", 1, 2, f_char2nr},
585 {"cindent", 1, 1, f_cindent},
586 {"clearmatches", 0, 0, f_clearmatches},
587 {"col", 1, 1, f_col},
588#if defined(FEAT_INS_EXPAND)
589 {"complete", 2, 2, f_complete},
590 {"complete_add", 1, 1, f_complete_add},
591 {"complete_check", 0, 0, f_complete_check},
592#endif
593 {"confirm", 1, 4, f_confirm},
594 {"copy", 1, 1, f_copy},
595#ifdef FEAT_FLOAT
596 {"cos", 1, 1, f_cos},
597 {"cosh", 1, 1, f_cosh},
598#endif
599 {"count", 2, 4, f_count},
600 {"cscope_connection",0,3, f_cscope_connection},
601 {"cursor", 1, 3, f_cursor},
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200602#ifdef WIN3264
603 {"debugbreak", 1, 1, f_debugbreak},
604#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200605 {"deepcopy", 1, 2, f_deepcopy},
606 {"delete", 1, 2, f_delete},
Bram Moolenaard79a2622018-06-07 18:17:46 +0200607 {"deletebufline", 2, 3, f_deletebufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200608 {"did_filetype", 0, 0, f_did_filetype},
609 {"diff_filler", 1, 1, f_diff_filler},
610 {"diff_hlID", 2, 2, f_diff_hlID},
611 {"empty", 1, 1, f_empty},
612 {"escape", 2, 2, f_escape},
613 {"eval", 1, 1, f_eval},
614 {"eventhandler", 0, 0, f_eventhandler},
615 {"executable", 1, 1, f_executable},
616 {"execute", 1, 2, f_execute},
617 {"exepath", 1, 1, f_exepath},
618 {"exists", 1, 1, f_exists},
619#ifdef FEAT_FLOAT
620 {"exp", 1, 1, f_exp},
621#endif
622 {"expand", 1, 3, f_expand},
623 {"extend", 2, 3, f_extend},
624 {"feedkeys", 1, 2, f_feedkeys},
625 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
626 {"filereadable", 1, 1, f_filereadable},
627 {"filewritable", 1, 1, f_filewritable},
628 {"filter", 2, 2, f_filter},
629 {"finddir", 1, 3, f_finddir},
630 {"findfile", 1, 3, f_findfile},
631#ifdef FEAT_FLOAT
632 {"float2nr", 1, 1, f_float2nr},
633 {"floor", 1, 1, f_floor},
634 {"fmod", 2, 2, f_fmod},
635#endif
636 {"fnameescape", 1, 1, f_fnameescape},
637 {"fnamemodify", 2, 2, f_fnamemodify},
638 {"foldclosed", 1, 1, f_foldclosed},
639 {"foldclosedend", 1, 1, f_foldclosedend},
640 {"foldlevel", 1, 1, f_foldlevel},
641 {"foldtext", 0, 0, f_foldtext},
642 {"foldtextresult", 1, 1, f_foldtextresult},
643 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200644 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200645 {"function", 1, 3, f_function},
646 {"garbagecollect", 0, 1, f_garbagecollect},
647 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200648 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200649 {"getbufline", 2, 3, f_getbufline},
650 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100651 {"getchangelist", 1, 1, f_getchangelist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200652 {"getchar", 0, 1, f_getchar},
653 {"getcharmod", 0, 0, f_getcharmod},
654 {"getcharsearch", 0, 0, f_getcharsearch},
655 {"getcmdline", 0, 0, f_getcmdline},
656 {"getcmdpos", 0, 0, f_getcmdpos},
657 {"getcmdtype", 0, 0, f_getcmdtype},
658 {"getcmdwintype", 0, 0, f_getcmdwintype},
659#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200660 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200661#endif
662 {"getcurpos", 0, 0, f_getcurpos},
663 {"getcwd", 0, 2, f_getcwd},
664 {"getfontname", 0, 1, f_getfontname},
665 {"getfperm", 1, 1, f_getfperm},
666 {"getfsize", 1, 1, f_getfsize},
667 {"getftime", 1, 1, f_getftime},
668 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100669 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200670 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200671 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200672 {"getmatches", 0, 0, f_getmatches},
673 {"getpid", 0, 0, f_getpid},
674 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200675 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200676 {"getreg", 0, 3, f_getreg},
677 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200678 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200679 {"gettabvar", 2, 3, f_gettabvar},
680 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100681 {"gettagstack", 0, 1, f_gettagstack},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200682 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100683 {"getwinpos", 0, 1, f_getwinpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200684 {"getwinposx", 0, 0, f_getwinposx},
685 {"getwinposy", 0, 0, f_getwinposy},
686 {"getwinvar", 2, 3, f_getwinvar},
687 {"glob", 1, 4, f_glob},
688 {"glob2regpat", 1, 1, f_glob2regpat},
689 {"globpath", 2, 5, f_globpath},
690 {"has", 1, 1, f_has},
691 {"has_key", 2, 2, f_has_key},
692 {"haslocaldir", 0, 2, f_haslocaldir},
693 {"hasmapto", 1, 3, f_hasmapto},
694 {"highlightID", 1, 1, f_hlID}, /* obsolete */
695 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
696 {"histadd", 2, 2, f_histadd},
697 {"histdel", 1, 2, f_histdel},
698 {"histget", 1, 2, f_histget},
699 {"histnr", 1, 1, f_histnr},
700 {"hlID", 1, 1, f_hlID},
701 {"hlexists", 1, 1, f_hlexists},
702 {"hostname", 0, 0, f_hostname},
703 {"iconv", 3, 3, f_iconv},
704 {"indent", 1, 1, f_indent},
705 {"index", 2, 4, f_index},
706 {"input", 1, 3, f_input},
707 {"inputdialog", 1, 3, f_inputdialog},
708 {"inputlist", 1, 1, f_inputlist},
709 {"inputrestore", 0, 0, f_inputrestore},
710 {"inputsave", 0, 0, f_inputsave},
711 {"inputsecret", 1, 2, f_inputsecret},
712 {"insert", 2, 3, f_insert},
713 {"invert", 1, 1, f_invert},
714 {"isdirectory", 1, 1, f_isdirectory},
715 {"islocked", 1, 1, f_islocked},
716#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
717 {"isnan", 1, 1, f_isnan},
718#endif
719 {"items", 1, 1, f_items},
720#ifdef FEAT_JOB_CHANNEL
721 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200722 {"job_info", 0, 1, f_job_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200723 {"job_setoptions", 2, 2, f_job_setoptions},
724 {"job_start", 1, 2, f_job_start},
725 {"job_status", 1, 1, f_job_status},
726 {"job_stop", 1, 2, f_job_stop},
727#endif
728 {"join", 1, 2, f_join},
729 {"js_decode", 1, 1, f_js_decode},
730 {"js_encode", 1, 1, f_js_encode},
731 {"json_decode", 1, 1, f_json_decode},
732 {"json_encode", 1, 1, f_json_encode},
733 {"keys", 1, 1, f_keys},
734 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
735 {"len", 1, 1, f_len},
736 {"libcall", 3, 3, f_libcall},
737 {"libcallnr", 3, 3, f_libcallnr},
738 {"line", 1, 1, f_line},
739 {"line2byte", 1, 1, f_line2byte},
740 {"lispindent", 1, 1, f_lispindent},
741 {"localtime", 0, 0, f_localtime},
742#ifdef FEAT_FLOAT
743 {"log", 1, 1, f_log},
744 {"log10", 1, 1, f_log10},
745#endif
746#ifdef FEAT_LUA
747 {"luaeval", 1, 2, f_luaeval},
748#endif
749 {"map", 2, 2, f_map},
750 {"maparg", 1, 4, f_maparg},
751 {"mapcheck", 1, 3, f_mapcheck},
752 {"match", 2, 4, f_match},
753 {"matchadd", 2, 5, f_matchadd},
754 {"matchaddpos", 2, 5, f_matchaddpos},
755 {"matcharg", 1, 1, f_matcharg},
756 {"matchdelete", 1, 1, f_matchdelete},
757 {"matchend", 2, 4, f_matchend},
758 {"matchlist", 2, 4, f_matchlist},
759 {"matchstr", 2, 4, f_matchstr},
760 {"matchstrpos", 2, 4, f_matchstrpos},
761 {"max", 1, 1, f_max},
762 {"min", 1, 1, f_min},
763#ifdef vim_mkdir
764 {"mkdir", 1, 3, f_mkdir},
765#endif
766 {"mode", 0, 1, f_mode},
767#ifdef FEAT_MZSCHEME
768 {"mzeval", 1, 1, f_mzeval},
769#endif
770 {"nextnonblank", 1, 1, f_nextnonblank},
771 {"nr2char", 1, 2, f_nr2char},
772 {"or", 2, 2, f_or},
773 {"pathshorten", 1, 1, f_pathshorten},
774#ifdef FEAT_PERL
775 {"perleval", 1, 1, f_perleval},
776#endif
777#ifdef FEAT_FLOAT
778 {"pow", 2, 2, f_pow},
779#endif
780 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100781 {"printf", 1, 19, f_printf},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200782#ifdef FEAT_JOB_CHANNEL
783 {"prompt_setcallback", 2, 2, f_prompt_setcallback},
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200784 {"prompt_setinterrupt", 2, 2, f_prompt_setinterrupt},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200785 {"prompt_setprompt", 2, 2, f_prompt_setprompt},
786#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100787#ifdef FEAT_TEXT_PROP
788 {"prop_add", 3, 3, f_prop_add},
789 {"prop_clear", 1, 3, f_prop_clear},
790 {"prop_list", 1, 2, f_prop_list},
791 {"prop_remove", 2, 3, f_prop_remove},
792 {"prop_type_add", 2, 2, f_prop_type_add},
793 {"prop_type_change", 2, 2, f_prop_type_change},
794 {"prop_type_delete", 1, 2, f_prop_type_delete},
795 {"prop_type_get", 1, 2, f_prop_type_get},
796 {"prop_type_list", 0, 1, f_prop_type_list},
797#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200798 {"pumvisible", 0, 0, f_pumvisible},
799#ifdef FEAT_PYTHON3
800 {"py3eval", 1, 1, f_py3eval},
801#endif
802#ifdef FEAT_PYTHON
803 {"pyeval", 1, 1, f_pyeval},
804#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100805#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
806 {"pyxeval", 1, 1, f_pyxeval},
807#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200808 {"range", 1, 3, f_range},
809 {"readfile", 1, 3, f_readfile},
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200810 {"reg_executing", 0, 0, f_reg_executing},
811 {"reg_recording", 0, 0, f_reg_recording},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200812 {"reltime", 0, 2, f_reltime},
813#ifdef FEAT_FLOAT
814 {"reltimefloat", 1, 1, f_reltimefloat},
815#endif
816 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100817 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200818 {"remote_foreground", 1, 1, f_remote_foreground},
819 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100820 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200821 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100822 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200823 {"remove", 2, 3, f_remove},
824 {"rename", 2, 2, f_rename},
825 {"repeat", 2, 2, f_repeat},
826 {"resolve", 1, 1, f_resolve},
827 {"reverse", 1, 1, f_reverse},
828#ifdef FEAT_FLOAT
829 {"round", 1, 1, f_round},
830#endif
831 {"screenattr", 2, 2, f_screenattr},
832 {"screenchar", 2, 2, f_screenchar},
833 {"screencol", 0, 0, f_screencol},
834 {"screenrow", 0, 0, f_screenrow},
835 {"search", 1, 4, f_search},
836 {"searchdecl", 1, 3, f_searchdecl},
837 {"searchpair", 3, 7, f_searchpair},
838 {"searchpairpos", 3, 7, f_searchpairpos},
839 {"searchpos", 1, 4, f_searchpos},
840 {"server2client", 2, 2, f_server2client},
841 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200842 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200843 {"setbufvar", 3, 3, f_setbufvar},
844 {"setcharsearch", 1, 1, f_setcharsearch},
845 {"setcmdpos", 1, 1, f_setcmdpos},
846 {"setfperm", 2, 2, f_setfperm},
847 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200848 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200849 {"setmatches", 1, 1, f_setmatches},
850 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200851 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200852 {"setreg", 2, 3, f_setreg},
853 {"settabvar", 3, 3, f_settabvar},
854 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100855 {"settagstack", 2, 3, f_settagstack},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200856 {"setwinvar", 3, 3, f_setwinvar},
857#ifdef FEAT_CRYPT
858 {"sha256", 1, 1, f_sha256},
859#endif
860 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaarf9514162018-11-22 03:08:29 +0100861 {"shiftwidth", 0, 1, f_shiftwidth},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100862#ifdef FEAT_SIGNS
863 {"sign_define", 1, 2, f_sign_define},
864 {"sign_getdefined", 0, 1, f_sign_getdefined},
865 {"sign_getplaced", 0, 2, f_sign_getplaced},
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100866 {"sign_jump", 3, 3, f_sign_jump},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100867 {"sign_place", 4, 5, f_sign_place},
868 {"sign_undefine", 0, 1, f_sign_undefine},
869 {"sign_unplace", 1, 2, f_sign_unplace},
870#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200871 {"simplify", 1, 1, f_simplify},
872#ifdef FEAT_FLOAT
873 {"sin", 1, 1, f_sin},
874 {"sinh", 1, 1, f_sinh},
875#endif
876 {"sort", 1, 3, f_sort},
877 {"soundfold", 1, 1, f_soundfold},
878 {"spellbadword", 0, 1, f_spellbadword},
879 {"spellsuggest", 1, 3, f_spellsuggest},
880 {"split", 1, 3, f_split},
881#ifdef FEAT_FLOAT
882 {"sqrt", 1, 1, f_sqrt},
883 {"str2float", 1, 1, f_str2float},
884#endif
885 {"str2nr", 1, 2, f_str2nr},
886 {"strcharpart", 2, 3, f_strcharpart},
887 {"strchars", 1, 2, f_strchars},
888 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
889#ifdef HAVE_STRFTIME
890 {"strftime", 1, 2, f_strftime},
891#endif
892 {"strgetchar", 2, 2, f_strgetchar},
893 {"stridx", 2, 3, f_stridx},
894 {"string", 1, 1, f_string},
895 {"strlen", 1, 1, f_strlen},
896 {"strpart", 2, 3, f_strpart},
897 {"strridx", 2, 3, f_strridx},
898 {"strtrans", 1, 1, f_strtrans},
899 {"strwidth", 1, 1, f_strwidth},
900 {"submatch", 1, 2, f_submatch},
901 {"substitute", 4, 4, f_substitute},
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200902 {"swapinfo", 1, 1, f_swapinfo},
Bram Moolenaar110bd602018-09-16 18:46:59 +0200903 {"swapname", 1, 1, f_swapname},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200904 {"synID", 3, 3, f_synID},
905 {"synIDattr", 2, 3, f_synIDattr},
906 {"synIDtrans", 1, 1, f_synIDtrans},
907 {"synconcealed", 2, 2, f_synconcealed},
908 {"synstack", 2, 2, f_synstack},
909 {"system", 1, 2, f_system},
910 {"systemlist", 1, 2, f_systemlist},
911 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
912 {"tabpagenr", 0, 1, f_tabpagenr},
913 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
914 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100915 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200916#ifdef FEAT_FLOAT
917 {"tan", 1, 1, f_tan},
918 {"tanh", 1, 1, f_tanh},
919#endif
920 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200921#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100922 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
923 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100924 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200925 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200926# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
927 {"term_getansicolors", 1, 1, f_term_getansicolors},
928# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200929 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200930 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200931 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200932 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200933 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200934 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200935 {"term_getstatus", 1, 1, f_term_getstatus},
936 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200937 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200938 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200939 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200940 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200941# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
942 {"term_setansicolors", 2, 2, f_term_setansicolors},
943# endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100944 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100945 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaara42d3632018-04-14 17:05:38 +0200946 {"term_setsize", 3, 3, f_term_setsize},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200947 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200948 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200949#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200950 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
951 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200952 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200953 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100954 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100955 {"test_null_blob", 0, 0, f_test_null_blob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200956#ifdef FEAT_JOB_CHANNEL
957 {"test_null_channel", 0, 0, f_test_null_channel},
958#endif
959 {"test_null_dict", 0, 0, f_test_null_dict},
960#ifdef FEAT_JOB_CHANNEL
961 {"test_null_job", 0, 0, f_test_null_job},
962#endif
963 {"test_null_list", 0, 0, f_test_null_list},
964 {"test_null_partial", 0, 0, f_test_null_partial},
965 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200966 {"test_option_not_set", 1, 1, f_test_option_not_set},
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100967 {"test_override", 2, 2, f_test_override},
Bram Moolenaarab186732018-09-14 21:27:06 +0200968#ifdef FEAT_GUI
969 {"test_scrollbar", 3, 3, f_test_scrollbar},
970#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200971 {"test_settime", 1, 1, f_test_settime},
972#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200973 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200974 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200975 {"timer_start", 2, 3, f_timer_start},
976 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200977 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200978#endif
979 {"tolower", 1, 1, f_tolower},
980 {"toupper", 1, 1, f_toupper},
981 {"tr", 3, 3, f_tr},
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100982 {"trim", 1, 2, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200983#ifdef FEAT_FLOAT
984 {"trunc", 1, 1, f_trunc},
985#endif
986 {"type", 1, 1, f_type},
987 {"undofile", 1, 1, f_undofile},
988 {"undotree", 0, 0, f_undotree},
989 {"uniq", 1, 3, f_uniq},
990 {"values", 1, 1, f_values},
991 {"virtcol", 1, 1, f_virtcol},
992 {"visualmode", 0, 1, f_visualmode},
993 {"wildmenumode", 0, 0, f_wildmenumode},
994 {"win_findbuf", 1, 1, f_win_findbuf},
995 {"win_getid", 0, 2, f_win_getid},
996 {"win_gotoid", 1, 1, f_win_gotoid},
997 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
998 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100999 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001000 {"winbufnr", 1, 1, f_winbufnr},
1001 {"wincol", 0, 0, f_wincol},
1002 {"winheight", 1, 1, f_winheight},
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +02001003 {"winlayout", 0, 1, f_winlayout},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001004 {"winline", 0, 0, f_winline},
1005 {"winnr", 0, 1, f_winnr},
1006 {"winrestcmd", 0, 0, f_winrestcmd},
1007 {"winrestview", 1, 1, f_winrestview},
1008 {"winsaveview", 0, 0, f_winsaveview},
1009 {"winwidth", 1, 1, f_winwidth},
1010 {"wordcount", 0, 0, f_wordcount},
1011 {"writefile", 2, 3, f_writefile},
1012 {"xor", 2, 2, f_xor},
1013};
1014
1015#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
1016
1017/*
1018 * Function given to ExpandGeneric() to obtain the list of internal
1019 * or user defined function names.
1020 */
1021 char_u *
1022get_function_name(expand_T *xp, int idx)
1023{
1024 static int intidx = -1;
1025 char_u *name;
1026
1027 if (idx == 0)
1028 intidx = -1;
1029 if (intidx < 0)
1030 {
1031 name = get_user_func_name(xp, idx);
1032 if (name != NULL)
1033 return name;
1034 }
1035 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
1036 {
1037 STRCPY(IObuff, functions[intidx].f_name);
1038 STRCAT(IObuff, "(");
1039 if (functions[intidx].f_max_argc == 0)
1040 STRCAT(IObuff, ")");
1041 return IObuff;
1042 }
1043
1044 return NULL;
1045}
1046
1047/*
1048 * Function given to ExpandGeneric() to obtain the list of internal or
1049 * user defined variable or function names.
1050 */
1051 char_u *
1052get_expr_name(expand_T *xp, int idx)
1053{
1054 static int intidx = -1;
1055 char_u *name;
1056
1057 if (idx == 0)
1058 intidx = -1;
1059 if (intidx < 0)
1060 {
1061 name = get_function_name(xp, idx);
1062 if (name != NULL)
1063 return name;
1064 }
1065 return get_user_var_name(xp, ++intidx);
1066}
1067
1068#endif /* FEAT_CMDL_COMPL */
1069
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001070/*
1071 * Find internal function in table above.
1072 * Return index, or -1 if not found
1073 */
1074 int
1075find_internal_func(
1076 char_u *name) /* name of the function */
1077{
1078 int first = 0;
1079 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
1080 int cmp;
1081 int x;
1082
1083 /*
1084 * Find the function name in the table. Binary search.
1085 */
1086 while (first <= last)
1087 {
1088 x = first + ((unsigned)(last - first) >> 1);
1089 cmp = STRCMP(name, functions[x].f_name);
1090 if (cmp < 0)
1091 last = x - 1;
1092 else if (cmp > 0)
1093 first = x + 1;
1094 else
1095 return x;
1096 }
1097 return -1;
1098}
1099
1100 int
1101call_internal_func(
1102 char_u *name,
1103 int argcount,
1104 typval_T *argvars,
1105 typval_T *rettv)
1106{
1107 int i;
1108
1109 i = find_internal_func(name);
1110 if (i < 0)
1111 return ERROR_UNKNOWN;
1112 if (argcount < functions[i].f_min_argc)
1113 return ERROR_TOOFEW;
1114 if (argcount > functions[i].f_max_argc)
1115 return ERROR_TOOMANY;
1116 argvars[argcount].v_type = VAR_UNKNOWN;
1117 functions[i].f_func(argvars, rettv);
1118 return ERROR_NONE;
1119}
1120
1121/*
1122 * Return TRUE for a non-zero Number and a non-empty String.
1123 */
1124 static int
1125non_zero_arg(typval_T *argvars)
1126{
1127 return ((argvars[0].v_type == VAR_NUMBER
1128 && argvars[0].vval.v_number != 0)
1129 || (argvars[0].v_type == VAR_SPECIAL
1130 && argvars[0].vval.v_number == VVAL_TRUE)
1131 || (argvars[0].v_type == VAR_STRING
1132 && argvars[0].vval.v_string != NULL
1133 && *argvars[0].vval.v_string != NUL));
1134}
1135
1136/*
1137 * Get the lnum from the first argument.
1138 * Also accepts ".", "$", etc., but that only works for the current buffer.
1139 * Returns -1 on error.
1140 */
1141 static linenr_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001142tv_get_lnum(typval_T *argvars)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001143{
1144 typval_T rettv;
1145 linenr_T lnum;
1146
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001147 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001148 if (lnum == 0) /* no valid number, try using line() */
1149 {
1150 rettv.v_type = VAR_NUMBER;
1151 f_line(argvars, &rettv);
1152 lnum = (linenr_T)rettv.vval.v_number;
1153 clear_tv(&rettv);
1154 }
1155 return lnum;
1156}
1157
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001158/*
1159 * Get the lnum from the first argument.
1160 * Also accepts "$", then "buf" is used.
1161 * Returns 0 on error.
1162 */
1163 static linenr_T
1164tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
1165{
1166 if (argvars[0].v_type == VAR_STRING
1167 && argvars[0].vval.v_string != NULL
1168 && argvars[0].vval.v_string[0] == '$'
1169 && buf != NULL)
1170 return buf->b_ml.ml_line_count;
1171 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1172}
1173
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001174#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001175/*
1176 * Get the float value of "argvars[0]" into "f".
1177 * Returns FAIL when the argument is not a Number or Float.
1178 */
1179 static int
1180get_float_arg(typval_T *argvars, float_T *f)
1181{
1182 if (argvars[0].v_type == VAR_FLOAT)
1183 {
1184 *f = argvars[0].vval.v_float;
1185 return OK;
1186 }
1187 if (argvars[0].v_type == VAR_NUMBER)
1188 {
1189 *f = (float_T)argvars[0].vval.v_number;
1190 return OK;
1191 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001192 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001193 return FAIL;
1194}
1195
1196/*
1197 * "abs(expr)" function
1198 */
1199 static void
1200f_abs(typval_T *argvars, typval_T *rettv)
1201{
1202 if (argvars[0].v_type == VAR_FLOAT)
1203 {
1204 rettv->v_type = VAR_FLOAT;
1205 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1206 }
1207 else
1208 {
1209 varnumber_T n;
1210 int error = FALSE;
1211
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001212 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001213 if (error)
1214 rettv->vval.v_number = -1;
1215 else if (n > 0)
1216 rettv->vval.v_number = n;
1217 else
1218 rettv->vval.v_number = -n;
1219 }
1220}
1221
1222/*
1223 * "acos()" function
1224 */
1225 static void
1226f_acos(typval_T *argvars, typval_T *rettv)
1227{
1228 float_T f = 0.0;
1229
1230 rettv->v_type = VAR_FLOAT;
1231 if (get_float_arg(argvars, &f) == OK)
1232 rettv->vval.v_float = acos(f);
1233 else
1234 rettv->vval.v_float = 0.0;
1235}
1236#endif
1237
1238/*
1239 * "add(list, item)" function
1240 */
1241 static void
1242f_add(typval_T *argvars, typval_T *rettv)
1243{
1244 list_T *l;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001245 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001246
1247 rettv->vval.v_number = 1; /* Default: Failed */
1248 if (argvars[0].v_type == VAR_LIST)
1249 {
1250 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001251 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001252 (char_u *)N_("add() argument"), TRUE)
1253 && list_append_tv(l, &argvars[1]) == OK)
1254 copy_tv(&argvars[0], rettv);
1255 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001256 else if (argvars[0].v_type == VAR_BLOB)
1257 {
1258 if ((b = argvars[0].vval.v_blob) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001259 && !var_check_lock(b->bv_lock,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001260 (char_u *)N_("add() argument"), TRUE))
1261 {
Bram Moolenaar05500ec2019-01-13 19:10:33 +01001262 int error = FALSE;
1263 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
1264
1265 if (!error)
1266 {
1267 ga_append(&b->bv_ga, (int)n);
1268 copy_tv(&argvars[0], rettv);
1269 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001270 }
1271 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001272 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01001273 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001274}
1275
1276/*
1277 * "and(expr, expr)" function
1278 */
1279 static void
1280f_and(typval_T *argvars, typval_T *rettv)
1281{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001282 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1283 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001284}
1285
1286/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02001287 * If there is a window for "curbuf", make it the current window.
1288 */
1289 static void
1290find_win_for_curbuf(void)
1291{
1292 wininfo_T *wip;
1293
1294 for (wip = curbuf->b_wininfo; wip != NULL; wip = wip->wi_next)
1295 {
1296 if (wip->wi_win != NULL)
1297 {
1298 curwin = wip->wi_win;
1299 break;
1300 }
1301 }
1302}
1303
1304/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001305 * Set line or list of lines in buffer "buf".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001306 */
1307 static void
Bram Moolenaarca851592018-06-06 21:04:07 +02001308set_buffer_lines(
1309 buf_T *buf,
1310 linenr_T lnum_arg,
1311 int append,
1312 typval_T *lines,
1313 typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001314{
Bram Moolenaarca851592018-06-06 21:04:07 +02001315 linenr_T lnum = lnum_arg + (append ? 1 : 0);
1316 char_u *line = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001317 list_T *l = NULL;
1318 listitem_T *li = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001319 long added = 0;
Bram Moolenaarca851592018-06-06 21:04:07 +02001320 linenr_T append_lnum;
1321 buf_T *curbuf_save = NULL;
1322 win_T *curwin_save = NULL;
1323 int is_curbuf = buf == curbuf;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001324
Bram Moolenaarca851592018-06-06 21:04:07 +02001325 /* When using the current buffer ml_mfp will be set if needed. Useful when
1326 * setline() is used on startup. For other buffers the buffer must be
1327 * loaded. */
1328 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001329 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001330 rettv->vval.v_number = 1; /* FAIL */
1331 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001332 }
1333
Bram Moolenaarca851592018-06-06 21:04:07 +02001334 if (!is_curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001335 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001336 curbuf_save = curbuf;
1337 curwin_save = curwin;
1338 curbuf = buf;
Bram Moolenaard79a2622018-06-07 18:17:46 +02001339 find_win_for_curbuf();
Bram Moolenaarca851592018-06-06 21:04:07 +02001340 }
1341
1342 if (append)
1343 // appendbufline() uses the line number below which we insert
1344 append_lnum = lnum - 1;
1345 else
1346 // setbufline() uses the line number above which we insert, we only
1347 // append if it's below the last line
1348 append_lnum = curbuf->b_ml.ml_line_count;
1349
1350 if (lines->v_type == VAR_LIST)
1351 {
1352 l = lines->vval.v_list;
1353 li = l->lv_first;
1354 }
1355 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001356 line = tv_get_string_chk(lines);
Bram Moolenaarca851592018-06-06 21:04:07 +02001357
1358 /* default result is zero == OK */
1359 for (;;)
1360 {
1361 if (l != NULL)
1362 {
1363 /* list argument, get next string */
1364 if (li == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001365 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001366 line = tv_get_string_chk(&li->li_tv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001367 li = li->li_next;
1368 }
1369
Bram Moolenaarca851592018-06-06 21:04:07 +02001370 rettv->vval.v_number = 1; /* FAIL */
1371 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
1372 break;
1373
1374 /* When coming here from Insert mode, sync undo, so that this can be
1375 * undone separately from what was previously inserted. */
1376 if (u_sync_once == 2)
1377 {
1378 u_sync_once = 1; /* notify that u_sync() was called */
1379 u_sync(TRUE);
1380 }
1381
1382 if (!append && lnum <= curbuf->b_ml.ml_line_count)
1383 {
Bram Moolenaar21b50382019-01-04 18:07:24 +01001384 // Existing line, replace it.
1385 // Removes any existing text properties.
1386 if (u_savesub(lnum) == OK && ml_replace_len(
1387 lnum, line, (colnr_T)STRLEN(line) + 1, TRUE, TRUE) == OK)
Bram Moolenaarca851592018-06-06 21:04:07 +02001388 {
1389 changed_bytes(lnum, 0);
1390 if (is_curbuf && lnum == curwin->w_cursor.lnum)
1391 check_cursor_col();
1392 rettv->vval.v_number = 0; /* OK */
1393 }
1394 }
1395 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
1396 {
1397 /* append the line */
1398 ++added;
1399 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
1400 rettv->vval.v_number = 0; /* OK */
1401 }
1402
1403 if (l == NULL) /* only one string argument */
1404 break;
1405 ++lnum;
1406 }
1407
1408 if (added > 0)
1409 {
1410 win_T *wp;
1411 tabpage_T *tp;
1412
1413 appended_lines_mark(append_lnum, added);
1414 FOR_ALL_TAB_WINDOWS(tp, wp)
1415 if (wp->w_buffer == buf && wp->w_cursor.lnum > append_lnum)
1416 wp->w_cursor.lnum += added;
1417 check_cursor_col();
1418
Bram Moolenaarf2732452018-06-03 14:47:35 +02001419#ifdef FEAT_JOB_CHANNEL
1420 if (bt_prompt(curbuf) && (State & INSERT))
1421 // show the line with the prompt
1422 update_topline();
1423#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001424 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001425
1426 if (!is_curbuf)
1427 {
1428 curbuf = curbuf_save;
1429 curwin = curwin_save;
1430 }
1431}
1432
1433/*
1434 * "append(lnum, string/list)" function
1435 */
1436 static void
1437f_append(typval_T *argvars, typval_T *rettv)
1438{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001439 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaarca851592018-06-06 21:04:07 +02001440
1441 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1442}
1443
1444/*
1445 * "appendbufline(buf, lnum, string/list)" function
1446 */
1447 static void
1448f_appendbufline(typval_T *argvars, typval_T *rettv)
1449{
1450 linenr_T lnum;
1451 buf_T *buf;
1452
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001453 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarca851592018-06-06 21:04:07 +02001454 if (buf == NULL)
1455 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001456 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001457 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001458 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +02001459 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1460 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001461}
1462
1463/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001464 * "argc([window id])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001465 */
1466 static void
Bram Moolenaare6e39892018-10-25 12:32:11 +02001467f_argc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001468{
Bram Moolenaare6e39892018-10-25 12:32:11 +02001469 win_T *wp;
1470
1471 if (argvars[0].v_type == VAR_UNKNOWN)
1472 // use the current window
1473 rettv->vval.v_number = ARGCOUNT;
1474 else if (argvars[0].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001475 && tv_get_number(&argvars[0]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001476 // use the global argument list
1477 rettv->vval.v_number = GARGCOUNT;
1478 else
1479 {
1480 // use the argument list of the specified window
1481 wp = find_win_by_nr_or_id(&argvars[0]);
1482 if (wp != NULL)
1483 rettv->vval.v_number = WARGCOUNT(wp);
1484 else
1485 rettv->vval.v_number = -1;
1486 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001487}
1488
1489/*
1490 * "argidx()" function
1491 */
1492 static void
1493f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1494{
1495 rettv->vval.v_number = curwin->w_arg_idx;
1496}
1497
1498/*
1499 * "arglistid()" function
1500 */
1501 static void
1502f_arglistid(typval_T *argvars, typval_T *rettv)
1503{
1504 win_T *wp;
1505
1506 rettv->vval.v_number = -1;
1507 wp = find_tabwin(&argvars[0], &argvars[1]);
1508 if (wp != NULL)
1509 rettv->vval.v_number = wp->w_alist->id;
1510}
1511
1512/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001513 * Get the argument list for a given window
1514 */
1515 static void
1516get_arglist_as_rettv(aentry_T *arglist, int argcount, typval_T *rettv)
1517{
1518 int idx;
1519
1520 if (rettv_list_alloc(rettv) == OK && arglist != NULL)
1521 for (idx = 0; idx < argcount; ++idx)
1522 list_append_string(rettv->vval.v_list,
1523 alist_name(&arglist[idx]), -1);
1524}
1525
1526/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001527 * "argv(nr)" function
1528 */
1529 static void
1530f_argv(typval_T *argvars, typval_T *rettv)
1531{
1532 int idx;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001533 aentry_T *arglist = NULL;
1534 int argcount = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001535
1536 if (argvars[0].v_type != VAR_UNKNOWN)
1537 {
Bram Moolenaare6e39892018-10-25 12:32:11 +02001538 if (argvars[1].v_type == VAR_UNKNOWN)
1539 {
1540 arglist = ARGLIST;
1541 argcount = ARGCOUNT;
1542 }
1543 else if (argvars[1].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001544 && tv_get_number(&argvars[1]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001545 {
1546 arglist = GARGLIST;
1547 argcount = GARGCOUNT;
1548 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001549 else
Bram Moolenaare6e39892018-10-25 12:32:11 +02001550 {
1551 win_T *wp = find_win_by_nr_or_id(&argvars[1]);
1552
1553 if (wp != NULL)
1554 {
1555 /* Use the argument list of the specified window */
1556 arglist = WARGLIST(wp);
1557 argcount = WARGCOUNT(wp);
1558 }
1559 }
1560
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001561 rettv->v_type = VAR_STRING;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001562 rettv->vval.v_string = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001563 idx = tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02001564 if (arglist != NULL && idx >= 0 && idx < argcount)
1565 rettv->vval.v_string = vim_strsave(alist_name(&arglist[idx]));
1566 else if (idx == -1)
1567 get_arglist_as_rettv(arglist, argcount, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001568 }
Bram Moolenaare6e39892018-10-25 12:32:11 +02001569 else
1570 get_arglist_as_rettv(ARGLIST, ARGCOUNT, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001571}
1572
1573/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001574 * "assert_beeps(cmd [, error])" function
1575 */
1576 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001577f_assert_beeps(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001578{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001579 rettv->vval.v_number = assert_beeps(argvars);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001580}
1581
1582/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001583 * "assert_equal(expected, actual[, msg])" function
1584 */
1585 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001586f_assert_equal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001587{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001588 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_EQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001589}
1590
1591/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01001592 * "assert_equalfile(fname-one, fname-two)" function
1593 */
1594 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001595f_assert_equalfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaard96ff162018-02-18 22:13:29 +01001596{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001597 rettv->vval.v_number = assert_equalfile(argvars);
Bram Moolenaard96ff162018-02-18 22:13:29 +01001598}
1599
1600/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001601 * "assert_notequal(expected, actual[, msg])" function
1602 */
1603 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001604f_assert_notequal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001605{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001606 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001607}
1608
1609/*
1610 * "assert_exception(string[, msg])" function
1611 */
1612 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001613f_assert_exception(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001614{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001615 rettv->vval.v_number = assert_exception(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001616}
1617
1618/*
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02001619 * "assert_fails(cmd [, error[, msg]])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001620 */
1621 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001622f_assert_fails(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001623{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001624 rettv->vval.v_number = assert_fails(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001625}
1626
1627/*
1628 * "assert_false(actual[, msg])" function
1629 */
1630 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001631f_assert_false(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001632{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001633 rettv->vval.v_number = assert_bool(argvars, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001634}
1635
1636/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001637 * "assert_inrange(lower, upper[, msg])" function
1638 */
1639 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001640f_assert_inrange(typval_T *argvars, typval_T *rettv)
Bram Moolenaar61c04492016-07-23 15:35:35 +02001641{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001642 rettv->vval.v_number = assert_inrange(argvars);
Bram Moolenaar61c04492016-07-23 15:35:35 +02001643}
1644
1645/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001646 * "assert_match(pattern, actual[, msg])" function
1647 */
1648 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001649f_assert_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001650{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001651 rettv->vval.v_number = assert_match_common(argvars, ASSERT_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001652}
1653
1654/*
1655 * "assert_notmatch(pattern, actual[, msg])" function
1656 */
1657 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001658f_assert_notmatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001659{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001660 rettv->vval.v_number = assert_match_common(argvars, ASSERT_NOTMATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001661}
1662
1663/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001664 * "assert_report(msg)" function
1665 */
1666 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001667f_assert_report(typval_T *argvars, typval_T *rettv)
Bram Moolenaar42205552017-03-18 19:42:22 +01001668{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001669 rettv->vval.v_number = assert_report(argvars);
Bram Moolenaar42205552017-03-18 19:42:22 +01001670}
1671
1672/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001673 * "assert_true(actual[, msg])" function
1674 */
1675 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001676f_assert_true(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001677{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001678 rettv->vval.v_number = assert_bool(argvars, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001679}
1680
1681#ifdef FEAT_FLOAT
1682/*
1683 * "asin()" function
1684 */
1685 static void
1686f_asin(typval_T *argvars, typval_T *rettv)
1687{
1688 float_T f = 0.0;
1689
1690 rettv->v_type = VAR_FLOAT;
1691 if (get_float_arg(argvars, &f) == OK)
1692 rettv->vval.v_float = asin(f);
1693 else
1694 rettv->vval.v_float = 0.0;
1695}
1696
1697/*
1698 * "atan()" function
1699 */
1700 static void
1701f_atan(typval_T *argvars, typval_T *rettv)
1702{
1703 float_T f = 0.0;
1704
1705 rettv->v_type = VAR_FLOAT;
1706 if (get_float_arg(argvars, &f) == OK)
1707 rettv->vval.v_float = atan(f);
1708 else
1709 rettv->vval.v_float = 0.0;
1710}
1711
1712/*
1713 * "atan2()" function
1714 */
1715 static void
1716f_atan2(typval_T *argvars, typval_T *rettv)
1717{
1718 float_T fx = 0.0, fy = 0.0;
1719
1720 rettv->v_type = VAR_FLOAT;
1721 if (get_float_arg(argvars, &fx) == OK
1722 && get_float_arg(&argvars[1], &fy) == OK)
1723 rettv->vval.v_float = atan2(fx, fy);
1724 else
1725 rettv->vval.v_float = 0.0;
1726}
1727#endif
1728
1729/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001730 * "balloon_show()" function
1731 */
1732#ifdef FEAT_BEVAL
1733 static void
1734f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1735{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001736 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001737 {
1738 if (argvars[0].v_type == VAR_LIST
1739# ifdef FEAT_GUI
1740 && !gui.in_use
1741# endif
1742 )
1743 post_balloon(balloonEval, NULL, argvars[0].vval.v_list);
1744 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001745 post_balloon(balloonEval, tv_get_string_chk(&argvars[0]), NULL);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001746 }
1747}
1748
Bram Moolenaar669a8282017-11-19 20:13:05 +01001749# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001750 static void
1751f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1752{
1753 if (rettv_list_alloc(rettv) == OK)
1754 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001755 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001756
1757 if (msg != NULL)
1758 {
1759 pumitem_T *array;
1760 int size = split_message(msg, &array);
1761 int i;
1762
1763 /* Skip the first and last item, they are always empty. */
1764 for (i = 1; i < size - 1; ++i)
1765 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001766 while (size > 0)
1767 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001768 vim_free(array);
1769 }
1770 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001771}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001772# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001773#endif
1774
1775/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001776 * "browse(save, title, initdir, default)" function
1777 */
1778 static void
1779f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1780{
1781#ifdef FEAT_BROWSE
1782 int save;
1783 char_u *title;
1784 char_u *initdir;
1785 char_u *defname;
1786 char_u buf[NUMBUFLEN];
1787 char_u buf2[NUMBUFLEN];
1788 int error = FALSE;
1789
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001790 save = (int)tv_get_number_chk(&argvars[0], &error);
1791 title = tv_get_string_chk(&argvars[1]);
1792 initdir = tv_get_string_buf_chk(&argvars[2], buf);
1793 defname = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001794
1795 if (error || title == NULL || initdir == NULL || defname == NULL)
1796 rettv->vval.v_string = NULL;
1797 else
1798 rettv->vval.v_string =
1799 do_browse(save ? BROWSE_SAVE : 0,
1800 title, defname, NULL, initdir, NULL, curbuf);
1801#else
1802 rettv->vval.v_string = NULL;
1803#endif
1804 rettv->v_type = VAR_STRING;
1805}
1806
1807/*
1808 * "browsedir(title, initdir)" function
1809 */
1810 static void
1811f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1812{
1813#ifdef FEAT_BROWSE
1814 char_u *title;
1815 char_u *initdir;
1816 char_u buf[NUMBUFLEN];
1817
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001818 title = tv_get_string_chk(&argvars[0]);
1819 initdir = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001820
1821 if (title == NULL || initdir == NULL)
1822 rettv->vval.v_string = NULL;
1823 else
1824 rettv->vval.v_string = do_browse(BROWSE_DIR,
1825 title, NULL, NULL, initdir, NULL, curbuf);
1826#else
1827 rettv->vval.v_string = NULL;
1828#endif
1829 rettv->v_type = VAR_STRING;
1830}
1831
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001832/*
1833 * Find a buffer by number or exact name.
1834 */
1835 static buf_T *
1836find_buffer(typval_T *avar)
1837{
1838 buf_T *buf = NULL;
1839
1840 if (avar->v_type == VAR_NUMBER)
1841 buf = buflist_findnr((int)avar->vval.v_number);
1842 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1843 {
1844 buf = buflist_findname_exp(avar->vval.v_string);
1845 if (buf == NULL)
1846 {
1847 /* No full path name match, try a match with a URL or a "nofile"
1848 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001849 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001850 if (buf->b_fname != NULL
1851 && (path_with_url(buf->b_fname)
1852#ifdef FEAT_QUICKFIX
1853 || bt_nofile(buf)
1854#endif
1855 )
1856 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1857 break;
1858 }
1859 }
1860 return buf;
1861}
1862
1863/*
1864 * "bufexists(expr)" function
1865 */
1866 static void
1867f_bufexists(typval_T *argvars, typval_T *rettv)
1868{
1869 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1870}
1871
1872/*
1873 * "buflisted(expr)" function
1874 */
1875 static void
1876f_buflisted(typval_T *argvars, typval_T *rettv)
1877{
1878 buf_T *buf;
1879
1880 buf = find_buffer(&argvars[0]);
1881 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1882}
1883
1884/*
1885 * "bufloaded(expr)" function
1886 */
1887 static void
1888f_bufloaded(typval_T *argvars, typval_T *rettv)
1889{
1890 buf_T *buf;
1891
1892 buf = find_buffer(&argvars[0]);
1893 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1894}
1895
1896 buf_T *
1897buflist_find_by_name(char_u *name, int curtab_only)
1898{
1899 int save_magic;
1900 char_u *save_cpo;
1901 buf_T *buf;
1902
1903 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1904 save_magic = p_magic;
1905 p_magic = TRUE;
1906 save_cpo = p_cpo;
1907 p_cpo = (char_u *)"";
1908
1909 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1910 TRUE, FALSE, curtab_only));
1911
1912 p_magic = save_magic;
1913 p_cpo = save_cpo;
1914 return buf;
1915}
1916
1917/*
1918 * Get buffer by number or pattern.
1919 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001920 buf_T *
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001921tv_get_buf(typval_T *tv, int curtab_only)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001922{
1923 char_u *name = tv->vval.v_string;
1924 buf_T *buf;
1925
1926 if (tv->v_type == VAR_NUMBER)
1927 return buflist_findnr((int)tv->vval.v_number);
1928 if (tv->v_type != VAR_STRING)
1929 return NULL;
1930 if (name == NULL || *name == NUL)
1931 return curbuf;
1932 if (name[0] == '$' && name[1] == NUL)
1933 return lastbuf;
1934
1935 buf = buflist_find_by_name(name, curtab_only);
1936
1937 /* If not found, try expanding the name, like done for bufexists(). */
1938 if (buf == NULL)
1939 buf = find_buffer(tv);
1940
1941 return buf;
1942}
1943
Bram Moolenaarec9d3002019-01-12 13:50:31 +01001944#ifdef FEAT_SIGNS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001945/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001946 * Get the buffer from "arg" and give an error and return NULL if it is not
1947 * valid.
1948 */
1949 static buf_T *
1950get_buf_arg(typval_T *arg)
1951{
1952 buf_T *buf;
1953
1954 ++emsg_off;
1955 buf = tv_get_buf(arg, FALSE);
1956 --emsg_off;
1957 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001958 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001959 return buf;
1960}
Bram Moolenaarec9d3002019-01-12 13:50:31 +01001961#endif
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001962
1963/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001964 * "bufname(expr)" function
1965 */
1966 static void
1967f_bufname(typval_T *argvars, typval_T *rettv)
1968{
1969 buf_T *buf;
1970
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001971 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001972 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001973 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001974 rettv->v_type = VAR_STRING;
1975 if (buf != NULL && buf->b_fname != NULL)
1976 rettv->vval.v_string = vim_strsave(buf->b_fname);
1977 else
1978 rettv->vval.v_string = NULL;
1979 --emsg_off;
1980}
1981
1982/*
1983 * "bufnr(expr)" function
1984 */
1985 static void
1986f_bufnr(typval_T *argvars, typval_T *rettv)
1987{
1988 buf_T *buf;
1989 int error = FALSE;
1990 char_u *name;
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 --emsg_off;
1996
1997 /* If the buffer isn't found and the second argument is not zero create a
1998 * new buffer. */
1999 if (buf == NULL
2000 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002001 && tv_get_number_chk(&argvars[1], &error) != 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002002 && !error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002003 && (name = tv_get_string_chk(&argvars[0])) != NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002004 && !error)
2005 buf = buflist_new(name, NULL, (linenr_T)1, 0);
2006
2007 if (buf != NULL)
2008 rettv->vval.v_number = buf->b_fnum;
2009 else
2010 rettv->vval.v_number = -1;
2011}
2012
2013 static void
2014buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
2015{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002016 win_T *wp;
2017 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002018 buf_T *buf;
2019
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002020 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002021 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002022 buf = tv_get_buf(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02002023 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002024 {
2025 ++winnr;
2026 if (wp->w_buffer == buf)
2027 break;
2028 }
2029 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002030 --emsg_off;
2031}
2032
2033/*
2034 * "bufwinid(nr)" function
2035 */
2036 static void
2037f_bufwinid(typval_T *argvars, typval_T *rettv)
2038{
2039 buf_win_common(argvars, rettv, FALSE);
2040}
2041
2042/*
2043 * "bufwinnr(nr)" function
2044 */
2045 static void
2046f_bufwinnr(typval_T *argvars, typval_T *rettv)
2047{
2048 buf_win_common(argvars, rettv, TRUE);
2049}
2050
2051/*
2052 * "byte2line(byte)" function
2053 */
2054 static void
2055f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2056{
2057#ifndef FEAT_BYTEOFF
2058 rettv->vval.v_number = -1;
2059#else
2060 long boff = 0;
2061
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002062 boff = tv_get_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002063 if (boff < 0)
2064 rettv->vval.v_number = -1;
2065 else
2066 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2067 (linenr_T)0, &boff);
2068#endif
2069}
2070
2071 static void
2072byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2073{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002074 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002075 char_u *str;
2076 varnumber_T idx;
2077
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002078 str = tv_get_string_chk(&argvars[0]);
2079 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002080 rettv->vval.v_number = -1;
2081 if (str == NULL || idx < 0)
2082 return;
2083
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002084 t = str;
2085 for ( ; idx > 0; idx--)
2086 {
2087 if (*t == NUL) /* EOL reached */
2088 return;
2089 if (enc_utf8 && comp)
2090 t += utf_ptr2len(t);
2091 else
2092 t += (*mb_ptr2len)(t);
2093 }
2094 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002095}
2096
2097/*
2098 * "byteidx()" function
2099 */
2100 static void
2101f_byteidx(typval_T *argvars, typval_T *rettv)
2102{
2103 byteidx(argvars, rettv, FALSE);
2104}
2105
2106/*
2107 * "byteidxcomp()" function
2108 */
2109 static void
2110f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2111{
2112 byteidx(argvars, rettv, TRUE);
2113}
2114
2115/*
2116 * "call(func, arglist [, dict])" function
2117 */
2118 static void
2119f_call(typval_T *argvars, typval_T *rettv)
2120{
2121 char_u *func;
2122 partial_T *partial = NULL;
2123 dict_T *selfdict = NULL;
2124
2125 if (argvars[1].v_type != VAR_LIST)
2126 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002127 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002128 return;
2129 }
2130 if (argvars[1].vval.v_list == NULL)
2131 return;
2132
2133 if (argvars[0].v_type == VAR_FUNC)
2134 func = argvars[0].vval.v_string;
2135 else if (argvars[0].v_type == VAR_PARTIAL)
2136 {
2137 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002138 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002139 }
2140 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002141 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002142 if (*func == NUL)
2143 return; /* type error or empty name */
2144
2145 if (argvars[2].v_type != VAR_UNKNOWN)
2146 {
2147 if (argvars[2].v_type != VAR_DICT)
2148 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002149 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002150 return;
2151 }
2152 selfdict = argvars[2].vval.v_dict;
2153 }
2154
2155 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2156}
2157
2158#ifdef FEAT_FLOAT
2159/*
2160 * "ceil({float})" function
2161 */
2162 static void
2163f_ceil(typval_T *argvars, typval_T *rettv)
2164{
2165 float_T f = 0.0;
2166
2167 rettv->v_type = VAR_FLOAT;
2168 if (get_float_arg(argvars, &f) == OK)
2169 rettv->vval.v_float = ceil(f);
2170 else
2171 rettv->vval.v_float = 0.0;
2172}
2173#endif
2174
2175#ifdef FEAT_JOB_CHANNEL
2176/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002177 * "ch_canread()" function
2178 */
2179 static void
2180f_ch_canread(typval_T *argvars, typval_T *rettv)
2181{
Bram Moolenaar958dc692016-12-01 15:34:12 +01002182 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002183
2184 rettv->vval.v_number = 0;
2185 if (channel != NULL)
2186 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
2187 || channel_has_readahead(channel, PART_OUT)
2188 || channel_has_readahead(channel, PART_ERR);
2189}
2190
2191/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002192 * "ch_close()" function
2193 */
2194 static void
2195f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
2196{
2197 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2198
2199 if (channel != NULL)
2200 {
2201 channel_close(channel, FALSE);
2202 channel_clear(channel);
2203 }
2204}
2205
2206/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002207 * "ch_close()" function
2208 */
2209 static void
2210f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
2211{
2212 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2213
2214 if (channel != NULL)
2215 channel_close_in(channel);
2216}
2217
2218/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002219 * "ch_getbufnr()" function
2220 */
2221 static void
2222f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
2223{
2224 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2225
2226 rettv->vval.v_number = -1;
2227 if (channel != NULL)
2228 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002229 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002230 int part;
2231
2232 if (STRCMP(what, "err") == 0)
2233 part = PART_ERR;
2234 else if (STRCMP(what, "out") == 0)
2235 part = PART_OUT;
2236 else if (STRCMP(what, "in") == 0)
2237 part = PART_IN;
2238 else
2239 part = PART_SOCK;
2240 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
2241 rettv->vval.v_number =
2242 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
2243 }
2244}
2245
2246/*
2247 * "ch_getjob()" function
2248 */
2249 static void
2250f_ch_getjob(typval_T *argvars, typval_T *rettv)
2251{
2252 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2253
2254 if (channel != NULL)
2255 {
2256 rettv->v_type = VAR_JOB;
2257 rettv->vval.v_job = channel->ch_job;
2258 if (channel->ch_job != NULL)
2259 ++channel->ch_job->jv_refcount;
2260 }
2261}
2262
2263/*
2264 * "ch_info()" function
2265 */
2266 static void
2267f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
2268{
2269 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2270
2271 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
2272 channel_info(channel, rettv->vval.v_dict);
2273}
2274
2275/*
2276 * "ch_log()" function
2277 */
2278 static void
2279f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
2280{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002281 char_u *msg = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002282 channel_T *channel = NULL;
2283
2284 if (argvars[1].v_type != VAR_UNKNOWN)
2285 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
2286
Bram Moolenaard5359b22018-04-05 22:44:39 +02002287 ch_log(channel, "%s", msg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002288}
2289
2290/*
2291 * "ch_logfile()" function
2292 */
2293 static void
2294f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
2295{
2296 char_u *fname;
2297 char_u *opt = (char_u *)"";
2298 char_u buf[NUMBUFLEN];
2299
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02002300 /* Don't open a file in restricted mode. */
2301 if (check_restricted() || check_secure())
2302 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002303 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002304 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002305 opt = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002306 ch_logfile(fname, opt);
2307}
2308
2309/*
2310 * "ch_open()" function
2311 */
2312 static void
2313f_ch_open(typval_T *argvars, typval_T *rettv)
2314{
2315 rettv->v_type = VAR_CHANNEL;
2316 if (check_restricted() || check_secure())
2317 return;
2318 rettv->vval.v_channel = channel_open_func(argvars);
2319}
2320
2321/*
2322 * "ch_read()" function
2323 */
2324 static void
2325f_ch_read(typval_T *argvars, typval_T *rettv)
2326{
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002327 common_channel_read(argvars, rettv, FALSE, FALSE);
2328}
2329
2330/*
2331 * "ch_readblob()" function
2332 */
2333 static void
2334f_ch_readblob(typval_T *argvars, typval_T *rettv)
2335{
2336 common_channel_read(argvars, rettv, TRUE, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002337}
2338
2339/*
2340 * "ch_readraw()" function
2341 */
2342 static void
2343f_ch_readraw(typval_T *argvars, typval_T *rettv)
2344{
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002345 common_channel_read(argvars, rettv, TRUE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002346}
2347
2348/*
2349 * "ch_evalexpr()" function
2350 */
2351 static void
2352f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2353{
2354 ch_expr_common(argvars, rettv, TRUE);
2355}
2356
2357/*
2358 * "ch_sendexpr()" function
2359 */
2360 static void
2361f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2362{
2363 ch_expr_common(argvars, rettv, FALSE);
2364}
2365
2366/*
2367 * "ch_evalraw()" function
2368 */
2369 static void
2370f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2371{
2372 ch_raw_common(argvars, rettv, TRUE);
2373}
2374
2375/*
2376 * "ch_sendraw()" function
2377 */
2378 static void
2379f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2380{
2381 ch_raw_common(argvars, rettv, FALSE);
2382}
2383
2384/*
2385 * "ch_setoptions()" function
2386 */
2387 static void
2388f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2389{
2390 channel_T *channel;
2391 jobopt_T opt;
2392
2393 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2394 if (channel == NULL)
2395 return;
2396 clear_job_options(&opt);
2397 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002398 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002399 channel_set_options(channel, &opt);
2400 free_job_options(&opt);
2401}
2402
2403/*
2404 * "ch_status()" function
2405 */
2406 static void
2407f_ch_status(typval_T *argvars, typval_T *rettv)
2408{
2409 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002410 jobopt_T opt;
2411 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002412
2413 /* return an empty string by default */
2414 rettv->v_type = VAR_STRING;
2415 rettv->vval.v_string = NULL;
2416
2417 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002418
2419 if (argvars[1].v_type != VAR_UNKNOWN)
2420 {
2421 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002422 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002423 && (opt.jo_set & JO_PART))
2424 part = opt.jo_part;
2425 }
2426
2427 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002428}
2429#endif
2430
2431/*
2432 * "changenr()" function
2433 */
2434 static void
2435f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2436{
2437 rettv->vval.v_number = curbuf->b_u_seq_cur;
2438}
2439
2440/*
2441 * "char2nr(string)" function
2442 */
2443 static void
2444f_char2nr(typval_T *argvars, typval_T *rettv)
2445{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002446 if (has_mbyte)
2447 {
2448 int utf8 = 0;
2449
2450 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002451 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002452
2453 if (utf8)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002454 rettv->vval.v_number = (*utf_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002455 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002456 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002457 }
2458 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002459 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002460}
2461
2462/*
2463 * "cindent(lnum)" function
2464 */
2465 static void
2466f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2467{
2468#ifdef FEAT_CINDENT
2469 pos_T pos;
2470 linenr_T lnum;
2471
2472 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002473 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002474 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2475 {
2476 curwin->w_cursor.lnum = lnum;
2477 rettv->vval.v_number = get_c_indent();
2478 curwin->w_cursor = pos;
2479 }
2480 else
2481#endif
2482 rettv->vval.v_number = -1;
2483}
2484
2485/*
2486 * "clearmatches()" function
2487 */
2488 static void
2489f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2490{
2491#ifdef FEAT_SEARCH_EXTRA
2492 clear_matches(curwin);
2493#endif
2494}
2495
2496/*
2497 * "col(string)" function
2498 */
2499 static void
2500f_col(typval_T *argvars, typval_T *rettv)
2501{
2502 colnr_T col = 0;
2503 pos_T *fp;
2504 int fnum = curbuf->b_fnum;
2505
2506 fp = var2fpos(&argvars[0], FALSE, &fnum);
2507 if (fp != NULL && fnum == curbuf->b_fnum)
2508 {
2509 if (fp->col == MAXCOL)
2510 {
2511 /* '> can be MAXCOL, get the length of the line then */
2512 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2513 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2514 else
2515 col = MAXCOL;
2516 }
2517 else
2518 {
2519 col = fp->col + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002520 /* col(".") when the cursor is on the NUL at the end of the line
2521 * because of "coladd" can be seen as an extra column. */
2522 if (virtual_active() && fp == &curwin->w_cursor)
2523 {
2524 char_u *p = ml_get_cursor();
2525
2526 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2527 curwin->w_virtcol - curwin->w_cursor.coladd))
2528 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002529 int l;
2530
2531 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2532 col += l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002533 }
2534 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002535 }
2536 }
2537 rettv->vval.v_number = col;
2538}
2539
2540#if defined(FEAT_INS_EXPAND)
2541/*
2542 * "complete()" function
2543 */
2544 static void
2545f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2546{
2547 int startcol;
2548
2549 if ((State & INSERT) == 0)
2550 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002551 emsg(_("E785: complete() can only be used in Insert mode"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002552 return;
2553 }
2554
2555 /* Check for undo allowed here, because if something was already inserted
2556 * the line was already saved for undo and this check isn't done. */
2557 if (!undo_allowed())
2558 return;
2559
2560 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2561 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002562 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002563 return;
2564 }
2565
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002566 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002567 if (startcol <= 0)
2568 return;
2569
2570 set_completion(startcol - 1, argvars[1].vval.v_list);
2571}
2572
2573/*
2574 * "complete_add()" function
2575 */
2576 static void
2577f_complete_add(typval_T *argvars, typval_T *rettv)
2578{
2579 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2580}
2581
2582/*
2583 * "complete_check()" function
2584 */
2585 static void
2586f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2587{
2588 int saved = RedrawingDisabled;
2589
2590 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002591 ins_compl_check_keys(0, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002592 rettv->vval.v_number = compl_interrupted;
2593 RedrawingDisabled = saved;
2594}
2595#endif
2596
2597/*
2598 * "confirm(message, buttons[, default [, type]])" function
2599 */
2600 static void
2601f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2602{
2603#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2604 char_u *message;
2605 char_u *buttons = NULL;
2606 char_u buf[NUMBUFLEN];
2607 char_u buf2[NUMBUFLEN];
2608 int def = 1;
2609 int type = VIM_GENERIC;
2610 char_u *typestr;
2611 int error = FALSE;
2612
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002613 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002614 if (message == NULL)
2615 error = TRUE;
2616 if (argvars[1].v_type != VAR_UNKNOWN)
2617 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002618 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002619 if (buttons == NULL)
2620 error = TRUE;
2621 if (argvars[2].v_type != VAR_UNKNOWN)
2622 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002623 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002624 if (argvars[3].v_type != VAR_UNKNOWN)
2625 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002626 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002627 if (typestr == NULL)
2628 error = TRUE;
2629 else
2630 {
2631 switch (TOUPPER_ASC(*typestr))
2632 {
2633 case 'E': type = VIM_ERROR; break;
2634 case 'Q': type = VIM_QUESTION; break;
2635 case 'I': type = VIM_INFO; break;
2636 case 'W': type = VIM_WARNING; break;
2637 case 'G': type = VIM_GENERIC; break;
2638 }
2639 }
2640 }
2641 }
2642 }
2643
2644 if (buttons == NULL || *buttons == NUL)
2645 buttons = (char_u *)_("&Ok");
2646
2647 if (!error)
2648 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2649 def, NULL, FALSE);
2650#endif
2651}
2652
2653/*
2654 * "copy()" function
2655 */
2656 static void
2657f_copy(typval_T *argvars, typval_T *rettv)
2658{
2659 item_copy(&argvars[0], rettv, FALSE, 0);
2660}
2661
2662#ifdef FEAT_FLOAT
2663/*
2664 * "cos()" function
2665 */
2666 static void
2667f_cos(typval_T *argvars, typval_T *rettv)
2668{
2669 float_T f = 0.0;
2670
2671 rettv->v_type = VAR_FLOAT;
2672 if (get_float_arg(argvars, &f) == OK)
2673 rettv->vval.v_float = cos(f);
2674 else
2675 rettv->vval.v_float = 0.0;
2676}
2677
2678/*
2679 * "cosh()" function
2680 */
2681 static void
2682f_cosh(typval_T *argvars, typval_T *rettv)
2683{
2684 float_T f = 0.0;
2685
2686 rettv->v_type = VAR_FLOAT;
2687 if (get_float_arg(argvars, &f) == OK)
2688 rettv->vval.v_float = cosh(f);
2689 else
2690 rettv->vval.v_float = 0.0;
2691}
2692#endif
2693
2694/*
2695 * "count()" function
2696 */
2697 static void
2698f_count(typval_T *argvars, typval_T *rettv)
2699{
2700 long n = 0;
2701 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002702 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002703
Bram Moolenaar9966b212017-07-28 16:46:57 +02002704 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002705 ic = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002706
2707 if (argvars[0].v_type == VAR_STRING)
2708 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002709 char_u *expr = tv_get_string_chk(&argvars[1]);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002710 char_u *p = argvars[0].vval.v_string;
2711 char_u *next;
2712
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002713 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002714 {
2715 if (ic)
2716 {
2717 size_t len = STRLEN(expr);
2718
2719 while (*p != NUL)
2720 {
2721 if (MB_STRNICMP(p, expr, len) == 0)
2722 {
2723 ++n;
2724 p += len;
2725 }
2726 else
2727 MB_PTR_ADV(p);
2728 }
2729 }
2730 else
2731 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2732 != NULL)
2733 {
2734 ++n;
2735 p = next + STRLEN(expr);
2736 }
2737 }
2738
2739 }
2740 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002741 {
2742 listitem_T *li;
2743 list_T *l;
2744 long idx;
2745
2746 if ((l = argvars[0].vval.v_list) != NULL)
2747 {
2748 li = l->lv_first;
2749 if (argvars[2].v_type != VAR_UNKNOWN)
2750 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002751 if (argvars[3].v_type != VAR_UNKNOWN)
2752 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002753 idx = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002754 if (!error)
2755 {
2756 li = list_find(l, idx);
2757 if (li == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002758 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002759 }
2760 }
2761 if (error)
2762 li = NULL;
2763 }
2764
2765 for ( ; li != NULL; li = li->li_next)
2766 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2767 ++n;
2768 }
2769 }
2770 else if (argvars[0].v_type == VAR_DICT)
2771 {
2772 int todo;
2773 dict_T *d;
2774 hashitem_T *hi;
2775
2776 if ((d = argvars[0].vval.v_dict) != NULL)
2777 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002778 if (argvars[2].v_type != VAR_UNKNOWN)
2779 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002780 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002781 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002782 }
2783
2784 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2785 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2786 {
2787 if (!HASHITEM_EMPTY(hi))
2788 {
2789 --todo;
2790 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2791 ++n;
2792 }
2793 }
2794 }
2795 }
2796 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002797 semsg(_(e_listdictarg), "count()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002798 rettv->vval.v_number = n;
2799}
2800
2801/*
2802 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2803 *
2804 * Checks the existence of a cscope connection.
2805 */
2806 static void
2807f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2808{
2809#ifdef FEAT_CSCOPE
2810 int num = 0;
2811 char_u *dbpath = NULL;
2812 char_u *prepend = NULL;
2813 char_u buf[NUMBUFLEN];
2814
2815 if (argvars[0].v_type != VAR_UNKNOWN
2816 && argvars[1].v_type != VAR_UNKNOWN)
2817 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002818 num = (int)tv_get_number(&argvars[0]);
2819 dbpath = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002820 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002821 prepend = tv_get_string_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002822 }
2823
2824 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2825#endif
2826}
2827
2828/*
2829 * "cursor(lnum, col)" function, or
2830 * "cursor(list)"
2831 *
2832 * Moves the cursor to the specified line and column.
2833 * Returns 0 when the position could be set, -1 otherwise.
2834 */
2835 static void
2836f_cursor(typval_T *argvars, typval_T *rettv)
2837{
2838 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002839 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002840 int set_curswant = TRUE;
2841
2842 rettv->vval.v_number = -1;
2843 if (argvars[1].v_type == VAR_UNKNOWN)
2844 {
2845 pos_T pos;
2846 colnr_T curswant = -1;
2847
2848 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2849 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002850 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002851 return;
2852 }
2853 line = pos.lnum;
2854 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002855 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002856 if (curswant >= 0)
2857 {
2858 curwin->w_curswant = curswant - 1;
2859 set_curswant = FALSE;
2860 }
2861 }
2862 else
2863 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002864 line = tv_get_lnum(argvars);
2865 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002866 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002867 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002868 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002869 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002870 return; /* type error; errmsg already given */
2871 if (line > 0)
2872 curwin->w_cursor.lnum = line;
2873 if (col > 0)
2874 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002875 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002876
2877 /* Make sure the cursor is in a valid position. */
2878 check_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002879 /* Correct cursor for multi-byte character. */
2880 if (has_mbyte)
2881 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002882
2883 curwin->w_set_curswant = set_curswant;
2884 rettv->vval.v_number = 0;
2885}
2886
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002887#ifdef WIN3264
2888/*
2889 * "debugbreak()" function
2890 */
2891 static void
2892f_debugbreak(typval_T *argvars, typval_T *rettv)
2893{
2894 int pid;
2895
2896 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002897 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002898 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002899 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002900 else
2901 {
2902 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2903
2904 if (hProcess != NULL)
2905 {
2906 DebugBreakProcess(hProcess);
2907 CloseHandle(hProcess);
2908 rettv->vval.v_number = OK;
2909 }
2910 }
2911}
2912#endif
2913
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002914/*
2915 * "deepcopy()" function
2916 */
2917 static void
2918f_deepcopy(typval_T *argvars, typval_T *rettv)
2919{
2920 int noref = 0;
2921 int copyID;
2922
2923 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002924 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002925 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002926 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002927 else
2928 {
2929 copyID = get_copyID();
2930 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2931 }
2932}
2933
2934/*
2935 * "delete()" function
2936 */
2937 static void
2938f_delete(typval_T *argvars, typval_T *rettv)
2939{
2940 char_u nbuf[NUMBUFLEN];
2941 char_u *name;
2942 char_u *flags;
2943
2944 rettv->vval.v_number = -1;
2945 if (check_restricted() || check_secure())
2946 return;
2947
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002948 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002949 if (name == NULL || *name == NUL)
2950 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002951 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002952 return;
2953 }
2954
2955 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002956 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002957 else
2958 flags = (char_u *)"";
2959
2960 if (*flags == NUL)
2961 /* delete a file */
2962 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2963 else if (STRCMP(flags, "d") == 0)
2964 /* delete an empty directory */
2965 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2966 else if (STRCMP(flags, "rf") == 0)
2967 /* delete a directory recursively */
2968 rettv->vval.v_number = delete_recursive(name);
2969 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002970 semsg(_(e_invexpr2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002971}
2972
2973/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02002974 * "deletebufline()" function
2975 */
2976 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02002977f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02002978{
2979 buf_T *buf;
2980 linenr_T first, last;
2981 linenr_T lnum;
2982 long count;
2983 int is_curbuf;
2984 buf_T *curbuf_save = NULL;
2985 win_T *curwin_save = NULL;
2986 tabpage_T *tp;
2987 win_T *wp;
2988
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002989 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002990 if (buf == NULL)
2991 {
2992 rettv->vval.v_number = 1; /* FAIL */
2993 return;
2994 }
2995 is_curbuf = buf == curbuf;
2996
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002997 first = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002998 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002999 last = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003000 else
3001 last = first;
3002
3003 if (buf->b_ml.ml_mfp == NULL || first < 1
3004 || first > buf->b_ml.ml_line_count || last < first)
3005 {
3006 rettv->vval.v_number = 1; /* FAIL */
3007 return;
3008 }
3009
3010 if (!is_curbuf)
3011 {
3012 curbuf_save = curbuf;
3013 curwin_save = curwin;
3014 curbuf = buf;
3015 find_win_for_curbuf();
3016 }
3017 if (last > curbuf->b_ml.ml_line_count)
3018 last = curbuf->b_ml.ml_line_count;
3019 count = last - first + 1;
3020
3021 // When coming here from Insert mode, sync undo, so that this can be
3022 // undone separately from what was previously inserted.
3023 if (u_sync_once == 2)
3024 {
3025 u_sync_once = 1; // notify that u_sync() was called
3026 u_sync(TRUE);
3027 }
3028
3029 if (u_save(first - 1, last + 1) == FAIL)
3030 {
3031 rettv->vval.v_number = 1; /* FAIL */
3032 return;
3033 }
3034
3035 for (lnum = first; lnum <= last; ++lnum)
3036 ml_delete(first, TRUE);
3037
3038 FOR_ALL_TAB_WINDOWS(tp, wp)
3039 if (wp->w_buffer == buf)
3040 {
3041 if (wp->w_cursor.lnum > last)
3042 wp->w_cursor.lnum -= count;
3043 else if (wp->w_cursor.lnum> first)
3044 wp->w_cursor.lnum = first;
3045 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
3046 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
3047 }
3048 check_cursor_col();
3049 deleted_lines_mark(first, count);
3050
3051 if (!is_curbuf)
3052 {
3053 curbuf = curbuf_save;
3054 curwin = curwin_save;
3055 }
3056}
3057
3058/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003059 * "did_filetype()" function
3060 */
3061 static void
3062f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3063{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003064 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003065}
3066
3067/*
3068 * "diff_filler()" function
3069 */
3070 static void
3071f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3072{
3073#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003074 rettv->vval.v_number = diff_check_fill(curwin, tv_get_lnum(argvars));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003075#endif
3076}
3077
3078/*
3079 * "diff_hlID()" function
3080 */
3081 static void
3082f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3083{
3084#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003085 linenr_T lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003086 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01003087 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003088 static int fnum = 0;
3089 static int change_start = 0;
3090 static int change_end = 0;
3091 static hlf_T hlID = (hlf_T)0;
3092 int filler_lines;
3093 int col;
3094
3095 if (lnum < 0) /* ignore type error in {lnum} arg */
3096 lnum = 0;
3097 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003098 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003099 || fnum != curbuf->b_fnum)
3100 {
3101 /* New line, buffer, change: need to get the values. */
3102 filler_lines = diff_check(curwin, lnum);
3103 if (filler_lines < 0)
3104 {
3105 if (filler_lines == -1)
3106 {
3107 change_start = MAXCOL;
3108 change_end = -1;
3109 if (diff_find_change(curwin, lnum, &change_start, &change_end))
3110 hlID = HLF_ADD; /* added line */
3111 else
3112 hlID = HLF_CHD; /* changed line */
3113 }
3114 else
3115 hlID = HLF_ADD; /* added line */
3116 }
3117 else
3118 hlID = (hlf_T)0;
3119 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003120 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003121 fnum = curbuf->b_fnum;
3122 }
3123
3124 if (hlID == HLF_CHD || hlID == HLF_TXD)
3125 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003126 col = tv_get_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003127 if (col >= change_start && col <= change_end)
3128 hlID = HLF_TXD; /* changed text */
3129 else
3130 hlID = HLF_CHD; /* changed line */
3131 }
3132 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
3133#endif
3134}
3135
3136/*
3137 * "empty({expr})" function
3138 */
3139 static void
3140f_empty(typval_T *argvars, typval_T *rettv)
3141{
3142 int n = FALSE;
3143
3144 switch (argvars[0].v_type)
3145 {
3146 case VAR_STRING:
3147 case VAR_FUNC:
3148 n = argvars[0].vval.v_string == NULL
3149 || *argvars[0].vval.v_string == NUL;
3150 break;
3151 case VAR_PARTIAL:
3152 n = FALSE;
3153 break;
3154 case VAR_NUMBER:
3155 n = argvars[0].vval.v_number == 0;
3156 break;
3157 case VAR_FLOAT:
3158#ifdef FEAT_FLOAT
3159 n = argvars[0].vval.v_float == 0.0;
3160 break;
3161#endif
3162 case VAR_LIST:
3163 n = argvars[0].vval.v_list == NULL
3164 || argvars[0].vval.v_list->lv_first == NULL;
3165 break;
3166 case VAR_DICT:
3167 n = argvars[0].vval.v_dict == NULL
3168 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
3169 break;
3170 case VAR_SPECIAL:
3171 n = argvars[0].vval.v_number != VVAL_TRUE;
3172 break;
3173
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003174 case VAR_BLOB:
3175 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003176 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
3177 break;
3178
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003179 case VAR_JOB:
3180#ifdef FEAT_JOB_CHANNEL
3181 n = argvars[0].vval.v_job == NULL
3182 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
3183 break;
3184#endif
3185 case VAR_CHANNEL:
3186#ifdef FEAT_JOB_CHANNEL
3187 n = argvars[0].vval.v_channel == NULL
3188 || !channel_is_open(argvars[0].vval.v_channel);
3189 break;
3190#endif
3191 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01003192 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003193 n = TRUE;
3194 break;
3195 }
3196
3197 rettv->vval.v_number = n;
3198}
3199
3200/*
3201 * "escape({string}, {chars})" function
3202 */
3203 static void
3204f_escape(typval_T *argvars, typval_T *rettv)
3205{
3206 char_u buf[NUMBUFLEN];
3207
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003208 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
3209 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003210 rettv->v_type = VAR_STRING;
3211}
3212
3213/*
3214 * "eval()" function
3215 */
3216 static void
3217f_eval(typval_T *argvars, typval_T *rettv)
3218{
3219 char_u *s, *p;
3220
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003221 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003222 if (s != NULL)
3223 s = skipwhite(s);
3224
3225 p = s;
3226 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3227 {
3228 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003229 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003230 need_clr_eos = FALSE;
3231 rettv->v_type = VAR_NUMBER;
3232 rettv->vval.v_number = 0;
3233 }
3234 else if (*s != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003235 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003236}
3237
3238/*
3239 * "eventhandler()" function
3240 */
3241 static void
3242f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3243{
3244 rettv->vval.v_number = vgetc_busy;
3245}
3246
3247/*
3248 * "executable()" function
3249 */
3250 static void
3251f_executable(typval_T *argvars, typval_T *rettv)
3252{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003253 char_u *name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003254
3255 /* Check in $PATH and also check directly if there is a directory name. */
3256 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
3257 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
3258}
3259
3260static garray_T redir_execute_ga;
3261
3262/*
3263 * Append "value[value_len]" to the execute() output.
3264 */
3265 void
3266execute_redir_str(char_u *value, int value_len)
3267{
3268 int len;
3269
3270 if (value_len == -1)
3271 len = (int)STRLEN(value); /* Append the entire string */
3272 else
3273 len = value_len; /* Append only "value_len" characters */
3274 if (ga_grow(&redir_execute_ga, len) == OK)
3275 {
3276 mch_memmove((char *)redir_execute_ga.ga_data
3277 + redir_execute_ga.ga_len, value, len);
3278 redir_execute_ga.ga_len += len;
3279 }
3280}
3281
3282/*
3283 * Get next line from a list.
3284 * Called by do_cmdline() to get the next line.
3285 * Returns allocated string, or NULL for end of function.
3286 */
3287
3288 static char_u *
3289get_list_line(
3290 int c UNUSED,
3291 void *cookie,
3292 int indent UNUSED)
3293{
3294 listitem_T **p = (listitem_T **)cookie;
3295 listitem_T *item = *p;
3296 char_u buf[NUMBUFLEN];
3297 char_u *s;
3298
3299 if (item == NULL)
3300 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003301 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003302 *p = item->li_next;
3303 return s == NULL ? NULL : vim_strsave(s);
3304}
3305
3306/*
3307 * "execute()" function
3308 */
3309 static void
3310f_execute(typval_T *argvars, typval_T *rettv)
3311{
3312 char_u *cmd = NULL;
3313 list_T *list = NULL;
3314 int save_msg_silent = msg_silent;
3315 int save_emsg_silent = emsg_silent;
3316 int save_emsg_noredir = emsg_noredir;
3317 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003318 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003319 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003320 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003321 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003322
3323 rettv->vval.v_string = NULL;
3324 rettv->v_type = VAR_STRING;
3325
3326 if (argvars[0].v_type == VAR_LIST)
3327 {
3328 list = argvars[0].vval.v_list;
3329 if (list == NULL || list->lv_first == NULL)
3330 /* empty list, no commands, empty output */
3331 return;
3332 ++list->lv_refcount;
3333 }
3334 else
3335 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003336 cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003337 if (cmd == NULL)
3338 return;
3339 }
3340
3341 if (argvars[1].v_type != VAR_UNKNOWN)
3342 {
3343 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003344 char_u *s = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003345
3346 if (s == NULL)
3347 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003348 if (*s == NUL)
3349 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003350 if (STRNCMP(s, "silent", 6) == 0)
3351 ++msg_silent;
3352 if (STRCMP(s, "silent!") == 0)
3353 {
3354 emsg_silent = TRUE;
3355 emsg_noredir = TRUE;
3356 }
3357 }
3358 else
3359 ++msg_silent;
3360
3361 if (redir_execute)
3362 save_ga = redir_execute_ga;
3363 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3364 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003365 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003366 if (!echo_output)
3367 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003368
3369 if (cmd != NULL)
3370 do_cmdline_cmd(cmd);
3371 else
3372 {
3373 listitem_T *item = list->lv_first;
3374
3375 do_cmdline(NULL, get_list_line, (void *)&item,
3376 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3377 --list->lv_refcount;
3378 }
3379
Bram Moolenaard297f352017-01-29 20:31:21 +01003380 /* Need to append a NUL to the result. */
3381 if (ga_grow(&redir_execute_ga, 1) == OK)
3382 {
3383 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3384 rettv->vval.v_string = redir_execute_ga.ga_data;
3385 }
3386 else
3387 {
3388 ga_clear(&redir_execute_ga);
3389 rettv->vval.v_string = NULL;
3390 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003391 msg_silent = save_msg_silent;
3392 emsg_silent = save_emsg_silent;
3393 emsg_noredir = save_emsg_noredir;
3394
3395 redir_execute = save_redir_execute;
3396 if (redir_execute)
3397 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003398 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003399
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003400 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003401 if (echo_output)
3402 // When not working silently: put it in column zero. A following
3403 // "echon" will overwrite the message, unavoidably.
3404 msg_col = 0;
3405 else
3406 // When working silently: Put it back where it was, since nothing
3407 // should have been written.
3408 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003409}
3410
3411/*
3412 * "exepath()" function
3413 */
3414 static void
3415f_exepath(typval_T *argvars, typval_T *rettv)
3416{
3417 char_u *p = NULL;
3418
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003419 (void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003420 rettv->v_type = VAR_STRING;
3421 rettv->vval.v_string = p;
3422}
3423
3424/*
3425 * "exists()" function
3426 */
3427 static void
3428f_exists(typval_T *argvars, typval_T *rettv)
3429{
3430 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003431 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003432
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003433 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003434 if (*p == '$') /* environment variable */
3435 {
3436 /* first try "normal" environment variables (fast) */
3437 if (mch_getenv(p + 1) != NULL)
3438 n = TRUE;
3439 else
3440 {
3441 /* try expanding things like $VIM and ${HOME} */
3442 p = expand_env_save(p);
3443 if (p != NULL && *p != '$')
3444 n = TRUE;
3445 vim_free(p);
3446 }
3447 }
3448 else if (*p == '&' || *p == '+') /* option */
3449 {
3450 n = (get_option_tv(&p, NULL, TRUE) == OK);
3451 if (*skipwhite(p) != NUL)
3452 n = FALSE; /* trailing garbage */
3453 }
3454 else if (*p == '*') /* internal or user defined function */
3455 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003456 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003457 }
3458 else if (*p == ':')
3459 {
3460 n = cmd_exists(p + 1);
3461 }
3462 else if (*p == '#')
3463 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003464 if (p[1] == '#')
3465 n = autocmd_supported(p + 2);
3466 else
3467 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003468 }
3469 else /* internal variable */
3470 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003471 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003472 }
3473
3474 rettv->vval.v_number = n;
3475}
3476
3477#ifdef FEAT_FLOAT
3478/*
3479 * "exp()" function
3480 */
3481 static void
3482f_exp(typval_T *argvars, typval_T *rettv)
3483{
3484 float_T f = 0.0;
3485
3486 rettv->v_type = VAR_FLOAT;
3487 if (get_float_arg(argvars, &f) == OK)
3488 rettv->vval.v_float = exp(f);
3489 else
3490 rettv->vval.v_float = 0.0;
3491}
3492#endif
3493
3494/*
3495 * "expand()" function
3496 */
3497 static void
3498f_expand(typval_T *argvars, typval_T *rettv)
3499{
3500 char_u *s;
3501 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003502 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003503 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3504 expand_T xpc;
3505 int error = FALSE;
3506 char_u *result;
3507
3508 rettv->v_type = VAR_STRING;
3509 if (argvars[1].v_type != VAR_UNKNOWN
3510 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003511 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003512 && !error)
3513 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003514 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003515 }
3516
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003517 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003518 if (*s == '%' || *s == '#' || *s == '<')
3519 {
3520 ++emsg_off;
3521 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3522 --emsg_off;
3523 if (rettv->v_type == VAR_LIST)
3524 {
3525 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3526 list_append_string(rettv->vval.v_list, result, -1);
3527 else
3528 vim_free(result);
3529 }
3530 else
3531 rettv->vval.v_string = result;
3532 }
3533 else
3534 {
3535 /* When the optional second argument is non-zero, don't remove matches
3536 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3537 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003538 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003539 options |= WILD_KEEP_ALL;
3540 if (!error)
3541 {
3542 ExpandInit(&xpc);
3543 xpc.xp_context = EXPAND_FILES;
3544 if (p_wic)
3545 options += WILD_ICASE;
3546 if (rettv->v_type == VAR_STRING)
3547 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3548 options, WILD_ALL);
3549 else if (rettv_list_alloc(rettv) != FAIL)
3550 {
3551 int i;
3552
3553 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3554 for (i = 0; i < xpc.xp_numfiles; i++)
3555 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3556 ExpandCleanup(&xpc);
3557 }
3558 }
3559 else
3560 rettv->vval.v_string = NULL;
3561 }
3562}
3563
3564/*
3565 * "extend(list, list [, idx])" function
3566 * "extend(dict, dict [, action])" function
3567 */
3568 static void
3569f_extend(typval_T *argvars, typval_T *rettv)
3570{
3571 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3572
3573 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3574 {
3575 list_T *l1, *l2;
3576 listitem_T *item;
3577 long before;
3578 int error = FALSE;
3579
3580 l1 = argvars[0].vval.v_list;
3581 l2 = argvars[1].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003582 if (l1 != NULL && !var_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003583 && l2 != NULL)
3584 {
3585 if (argvars[2].v_type != VAR_UNKNOWN)
3586 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003587 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003588 if (error)
3589 return; /* type error; errmsg already given */
3590
3591 if (before == l1->lv_len)
3592 item = NULL;
3593 else
3594 {
3595 item = list_find(l1, before);
3596 if (item == NULL)
3597 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003598 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003599 return;
3600 }
3601 }
3602 }
3603 else
3604 item = NULL;
3605 list_extend(l1, l2, item);
3606
3607 copy_tv(&argvars[0], rettv);
3608 }
3609 }
3610 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3611 {
3612 dict_T *d1, *d2;
3613 char_u *action;
3614 int i;
3615
3616 d1 = argvars[0].vval.v_dict;
3617 d2 = argvars[1].vval.v_dict;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003618 if (d1 != NULL && !var_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003619 && d2 != NULL)
3620 {
3621 /* Check the third argument. */
3622 if (argvars[2].v_type != VAR_UNKNOWN)
3623 {
3624 static char *(av[]) = {"keep", "force", "error"};
3625
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003626 action = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003627 if (action == NULL)
3628 return; /* type error; errmsg already given */
3629 for (i = 0; i < 3; ++i)
3630 if (STRCMP(action, av[i]) == 0)
3631 break;
3632 if (i == 3)
3633 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003634 semsg(_(e_invarg2), action);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003635 return;
3636 }
3637 }
3638 else
3639 action = (char_u *)"force";
3640
3641 dict_extend(d1, d2, action);
3642
3643 copy_tv(&argvars[0], rettv);
3644 }
3645 }
3646 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003647 semsg(_(e_listdictarg), "extend()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003648}
3649
3650/*
3651 * "feedkeys()" function
3652 */
3653 static void
3654f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3655{
3656 int remap = TRUE;
3657 int insert = FALSE;
3658 char_u *keys, *flags;
3659 char_u nbuf[NUMBUFLEN];
3660 int typed = FALSE;
3661 int execute = FALSE;
3662 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003663 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003664 char_u *keys_esc;
3665
3666 /* This is not allowed in the sandbox. If the commands would still be
3667 * executed in the sandbox it would be OK, but it probably happens later,
3668 * when "sandbox" is no longer set. */
3669 if (check_secure())
3670 return;
3671
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003672 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003673
3674 if (argvars[1].v_type != VAR_UNKNOWN)
3675 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003676 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003677 for ( ; *flags != NUL; ++flags)
3678 {
3679 switch (*flags)
3680 {
3681 case 'n': remap = FALSE; break;
3682 case 'm': remap = TRUE; break;
3683 case 't': typed = TRUE; break;
3684 case 'i': insert = TRUE; break;
3685 case 'x': execute = TRUE; break;
3686 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003687 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003688 }
3689 }
3690 }
3691
3692 if (*keys != NUL || execute)
3693 {
3694 /* Need to escape K_SPECIAL and CSI before putting the string in the
3695 * typeahead buffer. */
3696 keys_esc = vim_strsave_escape_csi(keys);
3697 if (keys_esc != NULL)
3698 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003699 if (lowlevel)
3700 {
3701#ifdef USE_INPUT_BUF
3702 add_to_input_buf(keys, (int)STRLEN(keys));
3703#else
3704 emsg(_("E980: lowlevel input not supported"));
3705#endif
3706 }
3707 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003708 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003709 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003710 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003711 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003712#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003713 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003714#endif
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003715 )
3716 typebuf_was_filled = TRUE;
3717 }
3718 vim_free(keys_esc);
3719
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003720 if (execute)
3721 {
3722 int save_msg_scroll = msg_scroll;
3723
3724 /* Avoid a 1 second delay when the keys start Insert mode. */
3725 msg_scroll = FALSE;
3726
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003727 if (!dangerous)
3728 ++ex_normal_busy;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02003729 exec_normal(TRUE, FALSE, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003730 if (!dangerous)
3731 --ex_normal_busy;
3732
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003733 msg_scroll |= save_msg_scroll;
3734 }
3735 }
3736 }
3737}
3738
3739/*
3740 * "filereadable()" function
3741 */
3742 static void
3743f_filereadable(typval_T *argvars, typval_T *rettv)
3744{
3745 int fd;
3746 char_u *p;
3747 int n;
3748
3749#ifndef O_NONBLOCK
3750# define O_NONBLOCK 0
3751#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003752 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003753 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3754 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3755 {
3756 n = TRUE;
3757 close(fd);
3758 }
3759 else
3760 n = FALSE;
3761
3762 rettv->vval.v_number = n;
3763}
3764
3765/*
3766 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3767 * rights to write into.
3768 */
3769 static void
3770f_filewritable(typval_T *argvars, typval_T *rettv)
3771{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003772 rettv->vval.v_number = filewritable(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003773}
3774
3775 static void
3776findfilendir(
3777 typval_T *argvars UNUSED,
3778 typval_T *rettv,
3779 int find_what UNUSED)
3780{
3781#ifdef FEAT_SEARCHPATH
3782 char_u *fname;
3783 char_u *fresult = NULL;
3784 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3785 char_u *p;
3786 char_u pathbuf[NUMBUFLEN];
3787 int count = 1;
3788 int first = TRUE;
3789 int error = FALSE;
3790#endif
3791
3792 rettv->vval.v_string = NULL;
3793 rettv->v_type = VAR_STRING;
3794
3795#ifdef FEAT_SEARCHPATH
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003796 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003797
3798 if (argvars[1].v_type != VAR_UNKNOWN)
3799 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003800 p = tv_get_string_buf_chk(&argvars[1], pathbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003801 if (p == NULL)
3802 error = TRUE;
3803 else
3804 {
3805 if (*p != NUL)
3806 path = p;
3807
3808 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003809 count = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003810 }
3811 }
3812
3813 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3814 error = TRUE;
3815
3816 if (*fname != NUL && !error)
3817 {
3818 do
3819 {
3820 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3821 vim_free(fresult);
3822 fresult = find_file_in_path_option(first ? fname : NULL,
3823 first ? (int)STRLEN(fname) : 0,
3824 0, first, path,
3825 find_what,
3826 curbuf->b_ffname,
3827 find_what == FINDFILE_DIR
3828 ? (char_u *)"" : curbuf->b_p_sua);
3829 first = FALSE;
3830
3831 if (fresult != NULL && rettv->v_type == VAR_LIST)
3832 list_append_string(rettv->vval.v_list, fresult, -1);
3833
3834 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3835 }
3836
3837 if (rettv->v_type == VAR_STRING)
3838 rettv->vval.v_string = fresult;
3839#endif
3840}
3841
3842/*
3843 * "filter()" function
3844 */
3845 static void
3846f_filter(typval_T *argvars, typval_T *rettv)
3847{
3848 filter_map(argvars, rettv, FALSE);
3849}
3850
3851/*
3852 * "finddir({fname}[, {path}[, {count}]])" function
3853 */
3854 static void
3855f_finddir(typval_T *argvars, typval_T *rettv)
3856{
3857 findfilendir(argvars, rettv, FINDFILE_DIR);
3858}
3859
3860/*
3861 * "findfile({fname}[, {path}[, {count}]])" function
3862 */
3863 static void
3864f_findfile(typval_T *argvars, typval_T *rettv)
3865{
3866 findfilendir(argvars, rettv, FINDFILE_FILE);
3867}
3868
3869#ifdef FEAT_FLOAT
3870/*
3871 * "float2nr({float})" function
3872 */
3873 static void
3874f_float2nr(typval_T *argvars, typval_T *rettv)
3875{
3876 float_T f = 0.0;
3877
3878 if (get_float_arg(argvars, &f) == OK)
3879 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003880 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003881 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003882 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003883 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003884 else
3885 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003886 }
3887}
3888
3889/*
3890 * "floor({float})" function
3891 */
3892 static void
3893f_floor(typval_T *argvars, typval_T *rettv)
3894{
3895 float_T f = 0.0;
3896
3897 rettv->v_type = VAR_FLOAT;
3898 if (get_float_arg(argvars, &f) == OK)
3899 rettv->vval.v_float = floor(f);
3900 else
3901 rettv->vval.v_float = 0.0;
3902}
3903
3904/*
3905 * "fmod()" function
3906 */
3907 static void
3908f_fmod(typval_T *argvars, typval_T *rettv)
3909{
3910 float_T fx = 0.0, fy = 0.0;
3911
3912 rettv->v_type = VAR_FLOAT;
3913 if (get_float_arg(argvars, &fx) == OK
3914 && get_float_arg(&argvars[1], &fy) == OK)
3915 rettv->vval.v_float = fmod(fx, fy);
3916 else
3917 rettv->vval.v_float = 0.0;
3918}
3919#endif
3920
3921/*
3922 * "fnameescape({string})" function
3923 */
3924 static void
3925f_fnameescape(typval_T *argvars, typval_T *rettv)
3926{
3927 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003928 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003929 rettv->v_type = VAR_STRING;
3930}
3931
3932/*
3933 * "fnamemodify({fname}, {mods})" function
3934 */
3935 static void
3936f_fnamemodify(typval_T *argvars, typval_T *rettv)
3937{
3938 char_u *fname;
3939 char_u *mods;
3940 int usedlen = 0;
3941 int len;
3942 char_u *fbuf = NULL;
3943 char_u buf[NUMBUFLEN];
3944
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003945 fname = tv_get_string_chk(&argvars[0]);
3946 mods = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003947 if (fname == NULL || mods == NULL)
3948 fname = NULL;
3949 else
3950 {
3951 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02003952 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003953 }
3954
3955 rettv->v_type = VAR_STRING;
3956 if (fname == NULL)
3957 rettv->vval.v_string = NULL;
3958 else
3959 rettv->vval.v_string = vim_strnsave(fname, len);
3960 vim_free(fbuf);
3961}
3962
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003963/*
3964 * "foldclosed()" function
3965 */
3966 static void
3967foldclosed_both(
3968 typval_T *argvars UNUSED,
3969 typval_T *rettv,
3970 int end UNUSED)
3971{
3972#ifdef FEAT_FOLDING
3973 linenr_T lnum;
3974 linenr_T first, last;
3975
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003976 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003977 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3978 {
3979 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3980 {
3981 if (end)
3982 rettv->vval.v_number = (varnumber_T)last;
3983 else
3984 rettv->vval.v_number = (varnumber_T)first;
3985 return;
3986 }
3987 }
3988#endif
3989 rettv->vval.v_number = -1;
3990}
3991
3992/*
3993 * "foldclosed()" function
3994 */
3995 static void
3996f_foldclosed(typval_T *argvars, typval_T *rettv)
3997{
3998 foldclosed_both(argvars, rettv, FALSE);
3999}
4000
4001/*
4002 * "foldclosedend()" function
4003 */
4004 static void
4005f_foldclosedend(typval_T *argvars, typval_T *rettv)
4006{
4007 foldclosed_both(argvars, rettv, TRUE);
4008}
4009
4010/*
4011 * "foldlevel()" function
4012 */
4013 static void
4014f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4015{
4016#ifdef FEAT_FOLDING
4017 linenr_T lnum;
4018
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004019 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004020 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4021 rettv->vval.v_number = foldLevel(lnum);
4022#endif
4023}
4024
4025/*
4026 * "foldtext()" function
4027 */
4028 static void
4029f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
4030{
4031#ifdef FEAT_FOLDING
4032 linenr_T foldstart;
4033 linenr_T foldend;
4034 char_u *dashes;
4035 linenr_T lnum;
4036 char_u *s;
4037 char_u *r;
4038 int len;
4039 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004040 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004041#endif
4042
4043 rettv->v_type = VAR_STRING;
4044 rettv->vval.v_string = NULL;
4045#ifdef FEAT_FOLDING
4046 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
4047 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
4048 dashes = get_vim_var_str(VV_FOLDDASHES);
4049 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
4050 && dashes != NULL)
4051 {
4052 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02004053 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004054 if (!linewhite(lnum))
4055 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004056
4057 /* Find interesting text in this line. */
4058 s = skipwhite(ml_get(lnum));
4059 /* skip C comment-start */
4060 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
4061 {
4062 s = skipwhite(s + 2);
4063 if (*skipwhite(s) == NUL
4064 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
4065 {
4066 s = skipwhite(ml_get(lnum + 1));
4067 if (*s == '*')
4068 s = skipwhite(s + 1);
4069 }
4070 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02004071 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01004072 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004073 r = alloc((unsigned)(STRLEN(txt)
4074 + STRLEN(dashes) /* for %s */
4075 + 20 /* for %3ld */
4076 + STRLEN(s))); /* concatenated */
4077 if (r != NULL)
4078 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02004079 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004080 len = (int)STRLEN(r);
4081 STRCAT(r, s);
4082 /* remove 'foldmarker' and 'commentstring' */
4083 foldtext_cleanup(r + len);
4084 rettv->vval.v_string = r;
4085 }
4086 }
4087#endif
4088}
4089
4090/*
4091 * "foldtextresult(lnum)" function
4092 */
4093 static void
4094f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
4095{
4096#ifdef FEAT_FOLDING
4097 linenr_T lnum;
4098 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004099 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004100 foldinfo_T foldinfo;
4101 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004102 static int entered = FALSE;
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
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004108 if (entered)
4109 return; /* reject recursive use */
4110 entered = TRUE;
4111
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004112 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004113 /* treat illegal types and illegal string values for {lnum} the same */
4114 if (lnum < 0)
4115 lnum = 0;
4116 fold_count = foldedCount(curwin, lnum, &foldinfo);
4117 if (fold_count > 0)
4118 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01004119 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
4120 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004121 if (text == buf)
4122 text = vim_strsave(text);
4123 rettv->vval.v_string = text;
4124 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004125
4126 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004127#endif
4128}
4129
4130/*
4131 * "foreground()" function
4132 */
4133 static void
4134f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4135{
4136#ifdef FEAT_GUI
4137 if (gui.in_use)
4138 gui_mch_set_foreground();
4139#else
4140# ifdef WIN32
4141 win32_set_foreground();
4142# endif
4143#endif
4144}
4145
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004146 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004147common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004148{
4149 char_u *s;
4150 char_u *name;
4151 int use_string = FALSE;
4152 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004153 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004154
4155 if (argvars[0].v_type == VAR_FUNC)
4156 {
4157 /* function(MyFunc, [arg], dict) */
4158 s = argvars[0].vval.v_string;
4159 }
4160 else if (argvars[0].v_type == VAR_PARTIAL
4161 && argvars[0].vval.v_partial != NULL)
4162 {
4163 /* function(dict.MyFunc, [arg]) */
4164 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004165 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004166 }
4167 else
4168 {
4169 /* function('MyFunc', [arg], dict) */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004170 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004171 use_string = TRUE;
4172 }
4173
Bram Moolenaar843b8842016-08-21 14:36:15 +02004174 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004175 {
4176 name = s;
4177 trans_name = trans_function_name(&name, FALSE,
4178 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4179 if (*name != NUL)
4180 s = NULL;
4181 }
4182
Bram Moolenaar843b8842016-08-21 14:36:15 +02004183 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4184 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004185 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004186 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004187 else if (trans_name != NULL && (is_funcref
4188 ? find_func(trans_name) == NULL
4189 : !translated_function_exists(trans_name)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004190 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004191 else
4192 {
4193 int dict_idx = 0;
4194 int arg_idx = 0;
4195 list_T *list = NULL;
4196
4197 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4198 {
4199 char sid_buf[25];
4200 int off = *s == 's' ? 2 : 5;
4201
4202 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4203 * also be called from another script. Using trans_function_name()
4204 * would also work, but some plugins depend on the name being
4205 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004206 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004207 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
4208 if (name != NULL)
4209 {
4210 STRCPY(name, sid_buf);
4211 STRCAT(name, s + off);
4212 }
4213 }
4214 else
4215 name = vim_strsave(s);
4216
4217 if (argvars[1].v_type != VAR_UNKNOWN)
4218 {
4219 if (argvars[2].v_type != VAR_UNKNOWN)
4220 {
4221 /* function(name, [args], dict) */
4222 arg_idx = 1;
4223 dict_idx = 2;
4224 }
4225 else if (argvars[1].v_type == VAR_DICT)
4226 /* function(name, dict) */
4227 dict_idx = 1;
4228 else
4229 /* function(name, [args]) */
4230 arg_idx = 1;
4231 if (dict_idx > 0)
4232 {
4233 if (argvars[dict_idx].v_type != VAR_DICT)
4234 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004235 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004236 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004237 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004238 }
4239 if (argvars[dict_idx].vval.v_dict == NULL)
4240 dict_idx = 0;
4241 }
4242 if (arg_idx > 0)
4243 {
4244 if (argvars[arg_idx].v_type != VAR_LIST)
4245 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004246 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004247 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004248 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004249 }
4250 list = argvars[arg_idx].vval.v_list;
4251 if (list == NULL || list->lv_len == 0)
4252 arg_idx = 0;
4253 }
4254 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004255 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004256 {
4257 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
4258
4259 /* result is a VAR_PARTIAL */
4260 if (pt == NULL)
4261 vim_free(name);
4262 else
4263 {
4264 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4265 {
4266 listitem_T *li;
4267 int i = 0;
4268 int arg_len = 0;
4269 int lv_len = 0;
4270
4271 if (arg_pt != NULL)
4272 arg_len = arg_pt->pt_argc;
4273 if (list != NULL)
4274 lv_len = list->lv_len;
4275 pt->pt_argc = arg_len + lv_len;
4276 pt->pt_argv = (typval_T *)alloc(
4277 sizeof(typval_T) * pt->pt_argc);
4278 if (pt->pt_argv == NULL)
4279 {
4280 vim_free(pt);
4281 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004282 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004283 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004284 for (i = 0; i < arg_len; i++)
4285 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4286 if (lv_len > 0)
4287 for (li = list->lv_first; li != NULL;
4288 li = li->li_next)
4289 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004290 }
4291
4292 /* For "function(dict.func, [], dict)" and "func" is a partial
4293 * use "dict". That is backwards compatible. */
4294 if (dict_idx > 0)
4295 {
4296 /* The dict is bound explicitly, pt_auto is FALSE. */
4297 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4298 ++pt->pt_dict->dv_refcount;
4299 }
4300 else if (arg_pt != NULL)
4301 {
4302 /* If the dict was bound automatically the result is also
4303 * bound automatically. */
4304 pt->pt_dict = arg_pt->pt_dict;
4305 pt->pt_auto = arg_pt->pt_auto;
4306 if (pt->pt_dict != NULL)
4307 ++pt->pt_dict->dv_refcount;
4308 }
4309
4310 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004311 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4312 {
4313 pt->pt_func = arg_pt->pt_func;
4314 func_ptr_ref(pt->pt_func);
4315 vim_free(name);
4316 }
4317 else if (is_funcref)
4318 {
4319 pt->pt_func = find_func(trans_name);
4320 func_ptr_ref(pt->pt_func);
4321 vim_free(name);
4322 }
4323 else
4324 {
4325 pt->pt_name = name;
4326 func_ref(name);
4327 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004328 }
4329 rettv->v_type = VAR_PARTIAL;
4330 rettv->vval.v_partial = pt;
4331 }
4332 else
4333 {
4334 /* result is a VAR_FUNC */
4335 rettv->v_type = VAR_FUNC;
4336 rettv->vval.v_string = name;
4337 func_ref(name);
4338 }
4339 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004340theend:
4341 vim_free(trans_name);
4342}
4343
4344/*
4345 * "funcref()" function
4346 */
4347 static void
4348f_funcref(typval_T *argvars, typval_T *rettv)
4349{
4350 common_function(argvars, rettv, TRUE);
4351}
4352
4353/*
4354 * "function()" function
4355 */
4356 static void
4357f_function(typval_T *argvars, typval_T *rettv)
4358{
4359 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004360}
4361
4362/*
4363 * "garbagecollect()" function
4364 */
4365 static void
4366f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4367{
4368 /* This is postponed until we are back at the toplevel, because we may be
4369 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4370 want_garbage_collect = TRUE;
4371
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004372 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004373 garbage_collect_at_exit = TRUE;
4374}
4375
4376/*
4377 * "get()" function
4378 */
4379 static void
4380f_get(typval_T *argvars, typval_T *rettv)
4381{
4382 listitem_T *li;
4383 list_T *l;
4384 dictitem_T *di;
4385 dict_T *d;
4386 typval_T *tv = NULL;
4387
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004388 if (argvars[0].v_type == VAR_BLOB)
4389 {
4390 int error = FALSE;
4391 int idx = tv_get_number_chk(&argvars[1], &error);
4392
4393 if (!error)
4394 {
4395 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004396 if (idx < 0)
4397 idx = blob_len(argvars[0].vval.v_blob) + idx;
4398 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
4399 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004400 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004401 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004402 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004403 tv = rettv;
4404 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004405 }
4406 }
4407 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004408 {
4409 if ((l = argvars[0].vval.v_list) != NULL)
4410 {
4411 int error = FALSE;
4412
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004413 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004414 if (!error && li != NULL)
4415 tv = &li->li_tv;
4416 }
4417 }
4418 else if (argvars[0].v_type == VAR_DICT)
4419 {
4420 if ((d = argvars[0].vval.v_dict) != NULL)
4421 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004422 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004423 if (di != NULL)
4424 tv = &di->di_tv;
4425 }
4426 }
4427 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4428 {
4429 partial_T *pt;
4430 partial_T fref_pt;
4431
4432 if (argvars[0].v_type == VAR_PARTIAL)
4433 pt = argvars[0].vval.v_partial;
4434 else
4435 {
4436 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4437 fref_pt.pt_name = argvars[0].vval.v_string;
4438 pt = &fref_pt;
4439 }
4440
4441 if (pt != NULL)
4442 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004443 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004444 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004445
4446 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4447 {
4448 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004449 n = partial_name(pt);
4450 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004451 rettv->vval.v_string = NULL;
4452 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004453 {
4454 rettv->vval.v_string = vim_strsave(n);
4455 if (rettv->v_type == VAR_FUNC)
4456 func_ref(rettv->vval.v_string);
4457 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004458 }
4459 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004460 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004461 else if (STRCMP(what, "args") == 0)
4462 {
4463 rettv->v_type = VAR_LIST;
4464 if (rettv_list_alloc(rettv) == OK)
4465 {
4466 int i;
4467
4468 for (i = 0; i < pt->pt_argc; ++i)
4469 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4470 }
4471 }
4472 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004473 semsg(_(e_invarg2), what);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004474 return;
4475 }
4476 }
4477 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004478 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004479
4480 if (tv == NULL)
4481 {
4482 if (argvars[2].v_type != VAR_UNKNOWN)
4483 copy_tv(&argvars[2], rettv);
4484 }
4485 else
4486 copy_tv(tv, rettv);
4487}
4488
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004489/*
4490 * Returns buffer options, variables and other attributes in a dictionary.
4491 */
4492 static dict_T *
4493get_buffer_info(buf_T *buf)
4494{
4495 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004496 tabpage_T *tp;
4497 win_T *wp;
4498 list_T *windows;
4499
4500 dict = dict_alloc();
4501 if (dict == NULL)
4502 return NULL;
4503
Bram Moolenaare0be1672018-07-08 16:50:37 +02004504 dict_add_number(dict, "bufnr", buf->b_fnum);
4505 dict_add_string(dict, "name", buf->b_ffname);
4506 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4507 : buflist_findlnum(buf));
4508 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4509 dict_add_number(dict, "listed", buf->b_p_bl);
4510 dict_add_number(dict, "changed", bufIsChanged(buf));
4511 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4512 dict_add_number(dict, "hidden",
4513 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004514
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004515 /* Get a reference to buffer variables */
4516 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004517
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004518 /* List of windows displaying this buffer */
4519 windows = list_alloc();
4520 if (windows != NULL)
4521 {
4522 FOR_ALL_TAB_WINDOWS(tp, wp)
4523 if (wp->w_buffer == buf)
4524 list_append_number(windows, (varnumber_T)wp->w_id);
4525 dict_add_list(dict, "windows", windows);
4526 }
4527
4528#ifdef FEAT_SIGNS
4529 if (buf->b_signlist != NULL)
4530 {
4531 /* List of signs placed in this buffer */
4532 list_T *signs = list_alloc();
4533 if (signs != NULL)
4534 {
4535 get_buffer_signs(buf, signs);
4536 dict_add_list(dict, "signs", signs);
4537 }
4538 }
4539#endif
4540
4541 return dict;
4542}
4543
4544/*
4545 * "getbufinfo()" function
4546 */
4547 static void
4548f_getbufinfo(typval_T *argvars, typval_T *rettv)
4549{
4550 buf_T *buf = NULL;
4551 buf_T *argbuf = NULL;
4552 dict_T *d;
4553 int filtered = FALSE;
4554 int sel_buflisted = FALSE;
4555 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004556 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004557
4558 if (rettv_list_alloc(rettv) != OK)
4559 return;
4560
4561 /* List of all the buffers or selected buffers */
4562 if (argvars[0].v_type == VAR_DICT)
4563 {
4564 dict_T *sel_d = argvars[0].vval.v_dict;
4565
4566 if (sel_d != NULL)
4567 {
4568 dictitem_T *di;
4569
4570 filtered = TRUE;
4571
4572 di = dict_find(sel_d, (char_u *)"buflisted", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004573 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004574 sel_buflisted = TRUE;
4575
4576 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004577 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004578 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004579
4580 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004581 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004582 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004583 }
4584 }
4585 else if (argvars[0].v_type != VAR_UNKNOWN)
4586 {
4587 /* Information about one buffer. Argument specifies the buffer */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004588 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004589 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004590 argbuf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004591 --emsg_off;
4592 if (argbuf == NULL)
4593 return;
4594 }
4595
4596 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004597 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004598 {
4599 if (argbuf != NULL && argbuf != buf)
4600 continue;
4601 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004602 || (sel_buflisted && !buf->b_p_bl)
4603 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004604 continue;
4605
4606 d = get_buffer_info(buf);
4607 if (d != NULL)
4608 list_append_dict(rettv->vval.v_list, d);
4609 if (argbuf != NULL)
4610 return;
4611 }
4612}
4613
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004614/*
4615 * Get line or list of lines from buffer "buf" into "rettv".
4616 * Return a range (from start to end) of lines in rettv from the specified
4617 * buffer.
4618 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4619 */
4620 static void
4621get_buffer_lines(
4622 buf_T *buf,
4623 linenr_T start,
4624 linenr_T end,
4625 int retlist,
4626 typval_T *rettv)
4627{
4628 char_u *p;
4629
4630 rettv->v_type = VAR_STRING;
4631 rettv->vval.v_string = NULL;
4632 if (retlist && rettv_list_alloc(rettv) == FAIL)
4633 return;
4634
4635 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4636 return;
4637
4638 if (!retlist)
4639 {
4640 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4641 p = ml_get_buf(buf, start, FALSE);
4642 else
4643 p = (char_u *)"";
4644 rettv->vval.v_string = vim_strsave(p);
4645 }
4646 else
4647 {
4648 if (end < start)
4649 return;
4650
4651 if (start < 1)
4652 start = 1;
4653 if (end > buf->b_ml.ml_line_count)
4654 end = buf->b_ml.ml_line_count;
4655 while (start <= end)
4656 if (list_append_string(rettv->vval.v_list,
4657 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4658 break;
4659 }
4660}
4661
4662/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004663 * "getbufline()" function
4664 */
4665 static void
4666f_getbufline(typval_T *argvars, typval_T *rettv)
4667{
4668 linenr_T lnum;
4669 linenr_T end;
4670 buf_T *buf;
4671
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004672 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004673 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004674 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004675 --emsg_off;
4676
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004677 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004678 if (argvars[2].v_type == VAR_UNKNOWN)
4679 end = lnum;
4680 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004681 end = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004682
4683 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4684}
4685
4686/*
4687 * "getbufvar()" function
4688 */
4689 static void
4690f_getbufvar(typval_T *argvars, typval_T *rettv)
4691{
4692 buf_T *buf;
4693 buf_T *save_curbuf;
4694 char_u *varname;
4695 dictitem_T *v;
4696 int done = FALSE;
4697
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004698 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
4699 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004700 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004701 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004702
4703 rettv->v_type = VAR_STRING;
4704 rettv->vval.v_string = NULL;
4705
4706 if (buf != NULL && varname != NULL)
4707 {
4708 /* set curbuf to be our buf, temporarily */
4709 save_curbuf = curbuf;
4710 curbuf = buf;
4711
Bram Moolenaar30567352016-08-27 21:25:44 +02004712 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004713 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004714 if (varname[1] == NUL)
4715 {
4716 /* get all buffer-local options in a dict */
4717 dict_T *opts = get_winbuf_options(TRUE);
4718
4719 if (opts != NULL)
4720 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004721 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004722 done = TRUE;
4723 }
4724 }
4725 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4726 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004727 done = TRUE;
4728 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004729 else
4730 {
4731 /* Look up the variable. */
4732 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4733 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4734 'b', varname, FALSE);
4735 if (v != NULL)
4736 {
4737 copy_tv(&v->di_tv, rettv);
4738 done = TRUE;
4739 }
4740 }
4741
4742 /* restore previous notion of curbuf */
4743 curbuf = save_curbuf;
4744 }
4745
4746 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4747 /* use the default value */
4748 copy_tv(&argvars[2], rettv);
4749
4750 --emsg_off;
4751}
4752
4753/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004754 * "getchangelist()" function
4755 */
4756 static void
4757f_getchangelist(typval_T *argvars, typval_T *rettv)
4758{
4759#ifdef FEAT_JUMPLIST
4760 buf_T *buf;
4761 int i;
4762 list_T *l;
4763 dict_T *d;
4764#endif
4765
4766 if (rettv_list_alloc(rettv) != OK)
4767 return;
4768
4769#ifdef FEAT_JUMPLIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004770 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004771 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004772 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004773 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004774 if (buf == NULL)
4775 return;
4776
4777 l = list_alloc();
4778 if (l == NULL)
4779 return;
4780
4781 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4782 return;
4783 /*
4784 * The current window change list index tracks only the position in the
4785 * current buffer change list. For other buffers, use the change list
4786 * length as the current index.
4787 */
4788 list_append_number(rettv->vval.v_list,
4789 (varnumber_T)((buf == curwin->w_buffer)
4790 ? curwin->w_changelistidx : buf->b_changelistlen));
4791
4792 for (i = 0; i < buf->b_changelistlen; ++i)
4793 {
4794 if (buf->b_changelist[i].lnum == 0)
4795 continue;
4796 if ((d = dict_alloc()) == NULL)
4797 return;
4798 if (list_append_dict(l, d) == FAIL)
4799 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004800 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4801 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004802 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004803 }
4804#endif
4805}
4806/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004807 * "getchar()" function
4808 */
4809 static void
4810f_getchar(typval_T *argvars, typval_T *rettv)
4811{
4812 varnumber_T n;
4813 int error = FALSE;
4814
Bram Moolenaar84d93902018-09-11 20:10:20 +02004815#ifdef MESSAGE_QUEUE
4816 // vpeekc() used to check for messages, but that caused problems, invoking
4817 // a callback where it was not expected. Some plugins use getchar(1) in a
4818 // loop to await a message, therefore make sure we check for messages here.
4819 parse_queued_messages();
4820#endif
4821
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004822 /* Position the cursor. Needed after a message that ends in a space. */
4823 windgoto(msg_row, msg_col);
4824
4825 ++no_mapping;
4826 ++allow_keys;
4827 for (;;)
4828 {
4829 if (argvars[0].v_type == VAR_UNKNOWN)
4830 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004831 n = plain_vgetc();
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004832 else if (tv_get_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004833 /* getchar(1): only check if char avail */
4834 n = vpeekc_any();
4835 else if (error || vpeekc_any() == NUL)
4836 /* illegal argument or getchar(0) and no char avail: return zero */
4837 n = 0;
4838 else
4839 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004840 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004841
4842 if (n == K_IGNORE)
4843 continue;
4844 break;
4845 }
4846 --no_mapping;
4847 --allow_keys;
4848
4849 set_vim_var_nr(VV_MOUSE_WIN, 0);
4850 set_vim_var_nr(VV_MOUSE_WINID, 0);
4851 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4852 set_vim_var_nr(VV_MOUSE_COL, 0);
4853
4854 rettv->vval.v_number = n;
4855 if (IS_SPECIAL(n) || mod_mask != 0)
4856 {
4857 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4858 int i = 0;
4859
4860 /* Turn a special key into three bytes, plus modifier. */
4861 if (mod_mask != 0)
4862 {
4863 temp[i++] = K_SPECIAL;
4864 temp[i++] = KS_MODIFIER;
4865 temp[i++] = mod_mask;
4866 }
4867 if (IS_SPECIAL(n))
4868 {
4869 temp[i++] = K_SPECIAL;
4870 temp[i++] = K_SECOND(n);
4871 temp[i++] = K_THIRD(n);
4872 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004873 else if (has_mbyte)
4874 i += (*mb_char2bytes)(n, temp + i);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004875 else
4876 temp[i++] = n;
4877 temp[i++] = NUL;
4878 rettv->v_type = VAR_STRING;
4879 rettv->vval.v_string = vim_strsave(temp);
4880
4881#ifdef FEAT_MOUSE
4882 if (is_mouse_key(n))
4883 {
4884 int row = mouse_row;
4885 int col = mouse_col;
4886 win_T *win;
4887 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004888 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004889 int winnr = 1;
4890
4891 if (row >= 0 && col >= 0)
4892 {
4893 /* Find the window at the mouse coordinates and compute the
4894 * text position. */
4895 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004896 if (win == NULL)
4897 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004898 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004899 for (wp = firstwin; wp != win; wp = wp->w_next)
4900 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004901 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4902 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4903 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4904 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4905 }
4906 }
4907#endif
4908 }
4909}
4910
4911/*
4912 * "getcharmod()" function
4913 */
4914 static void
4915f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4916{
4917 rettv->vval.v_number = mod_mask;
4918}
4919
4920/*
4921 * "getcharsearch()" function
4922 */
4923 static void
4924f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4925{
4926 if (rettv_dict_alloc(rettv) != FAIL)
4927 {
4928 dict_T *dict = rettv->vval.v_dict;
4929
Bram Moolenaare0be1672018-07-08 16:50:37 +02004930 dict_add_string(dict, "char", last_csearch());
4931 dict_add_number(dict, "forward", last_csearch_forward());
4932 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004933 }
4934}
4935
4936/*
4937 * "getcmdline()" function
4938 */
4939 static void
4940f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4941{
4942 rettv->v_type = VAR_STRING;
4943 rettv->vval.v_string = get_cmdline_str();
4944}
4945
4946/*
4947 * "getcmdpos()" function
4948 */
4949 static void
4950f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4951{
4952 rettv->vval.v_number = get_cmdline_pos() + 1;
4953}
4954
4955/*
4956 * "getcmdtype()" function
4957 */
4958 static void
4959f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4960{
4961 rettv->v_type = VAR_STRING;
4962 rettv->vval.v_string = alloc(2);
4963 if (rettv->vval.v_string != NULL)
4964 {
4965 rettv->vval.v_string[0] = get_cmdline_type();
4966 rettv->vval.v_string[1] = NUL;
4967 }
4968}
4969
4970/*
4971 * "getcmdwintype()" function
4972 */
4973 static void
4974f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4975{
4976 rettv->v_type = VAR_STRING;
4977 rettv->vval.v_string = NULL;
4978#ifdef FEAT_CMDWIN
4979 rettv->vval.v_string = alloc(2);
4980 if (rettv->vval.v_string != NULL)
4981 {
4982 rettv->vval.v_string[0] = cmdwin_type;
4983 rettv->vval.v_string[1] = NUL;
4984 }
4985#endif
4986}
4987
4988#if defined(FEAT_CMDL_COMPL)
4989/*
4990 * "getcompletion()" function
4991 */
4992 static void
4993f_getcompletion(typval_T *argvars, typval_T *rettv)
4994{
4995 char_u *pat;
4996 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004997 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004998 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
4999 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005000
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005001 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005002 filtered = tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005003
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005004 if (p_wic)
5005 options |= WILD_ICASE;
5006
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005007 /* For filtered results, 'wildignore' is used */
5008 if (!filtered)
5009 options |= WILD_KEEP_ALL;
5010
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005011 ExpandInit(&xpc);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005012 xpc.xp_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005013 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005014 xpc.xp_context = cmdcomplete_str_to_type(tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005015 if (xpc.xp_context == EXPAND_NOTHING)
5016 {
5017 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005018 semsg(_(e_invarg2), argvars[1].vval.v_string);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005019 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005020 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005021 return;
5022 }
5023
5024# if defined(FEAT_MENU)
5025 if (xpc.xp_context == EXPAND_MENUS)
5026 {
5027 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
5028 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5029 }
5030# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02005031#ifdef FEAT_CSCOPE
5032 if (xpc.xp_context == EXPAND_CSCOPE)
5033 {
5034 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
5035 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5036 }
5037#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02005038#ifdef FEAT_SIGNS
5039 if (xpc.xp_context == EXPAND_SIGN)
5040 {
5041 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
5042 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5043 }
5044#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005045
5046 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
5047 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
5048 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005049 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005050
5051 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
5052
5053 for (i = 0; i < xpc.xp_numfiles; i++)
5054 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5055 }
5056 vim_free(pat);
5057 ExpandCleanup(&xpc);
5058}
5059#endif
5060
5061/*
5062 * "getcwd()" function
5063 */
5064 static void
5065f_getcwd(typval_T *argvars, typval_T *rettv)
5066{
5067 win_T *wp = NULL;
5068 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01005069 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005070
5071 rettv->v_type = VAR_STRING;
5072 rettv->vval.v_string = NULL;
5073
Bram Moolenaar54591292018-02-09 20:53:59 +01005074 if (argvars[0].v_type == VAR_NUMBER && argvars[0].vval.v_number == -1)
5075 global = TRUE;
5076 else
5077 wp = find_tabwin(&argvars[0], &argvars[1]);
5078
5079 if (wp != NULL && wp->w_localdir != NULL)
5080 rettv->vval.v_string = vim_strsave(wp->w_localdir);
5081 else if (wp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005082 {
Bram Moolenaar54591292018-02-09 20:53:59 +01005083 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005084 rettv->vval.v_string = vim_strsave(globaldir);
5085 else
5086 {
5087 cwd = alloc(MAXPATHL);
5088 if (cwd != NULL)
5089 {
5090 if (mch_dirname(cwd, MAXPATHL) != FAIL)
5091 rettv->vval.v_string = vim_strsave(cwd);
5092 vim_free(cwd);
5093 }
5094 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005095 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02005096#ifdef BACKSLASH_IN_FILENAME
5097 if (rettv->vval.v_string != NULL)
5098 slash_adjust(rettv->vval.v_string);
5099#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005100}
5101
5102/*
5103 * "getfontname()" function
5104 */
5105 static void
5106f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
5107{
5108 rettv->v_type = VAR_STRING;
5109 rettv->vval.v_string = NULL;
5110#ifdef FEAT_GUI
5111 if (gui.in_use)
5112 {
5113 GuiFont font;
5114 char_u *name = NULL;
5115
5116 if (argvars[0].v_type == VAR_UNKNOWN)
5117 {
5118 /* Get the "Normal" font. Either the name saved by
5119 * hl_set_font_name() or from the font ID. */
5120 font = gui.norm_font;
5121 name = hl_get_font_name();
5122 }
5123 else
5124 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005125 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005126 if (STRCMP(name, "*") == 0) /* don't use font dialog */
5127 return;
5128 font = gui_mch_get_font(name, FALSE);
5129 if (font == NOFONT)
5130 return; /* Invalid font name, return empty string. */
5131 }
5132 rettv->vval.v_string = gui_mch_get_fontname(font, name);
5133 if (argvars[0].v_type != VAR_UNKNOWN)
5134 gui_mch_free_font(font);
5135 }
5136#endif
5137}
5138
5139/*
5140 * "getfperm({fname})" function
5141 */
5142 static void
5143f_getfperm(typval_T *argvars, typval_T *rettv)
5144{
5145 char_u *fname;
5146 stat_T st;
5147 char_u *perm = NULL;
5148 char_u flags[] = "rwx";
5149 int i;
5150
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005151 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005152
5153 rettv->v_type = VAR_STRING;
5154 if (mch_stat((char *)fname, &st) >= 0)
5155 {
5156 perm = vim_strsave((char_u *)"---------");
5157 if (perm != NULL)
5158 {
5159 for (i = 0; i < 9; i++)
5160 {
5161 if (st.st_mode & (1 << (8 - i)))
5162 perm[i] = flags[i % 3];
5163 }
5164 }
5165 }
5166 rettv->vval.v_string = perm;
5167}
5168
5169/*
5170 * "getfsize({fname})" function
5171 */
5172 static void
5173f_getfsize(typval_T *argvars, typval_T *rettv)
5174{
5175 char_u *fname;
5176 stat_T st;
5177
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005178 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005179
5180 rettv->v_type = VAR_NUMBER;
5181
5182 if (mch_stat((char *)fname, &st) >= 0)
5183 {
5184 if (mch_isdir(fname))
5185 rettv->vval.v_number = 0;
5186 else
5187 {
5188 rettv->vval.v_number = (varnumber_T)st.st_size;
5189
5190 /* non-perfect check for overflow */
5191 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5192 rettv->vval.v_number = -2;
5193 }
5194 }
5195 else
5196 rettv->vval.v_number = -1;
5197}
5198
5199/*
5200 * "getftime({fname})" function
5201 */
5202 static void
5203f_getftime(typval_T *argvars, typval_T *rettv)
5204{
5205 char_u *fname;
5206 stat_T st;
5207
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005208 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005209
5210 if (mch_stat((char *)fname, &st) >= 0)
5211 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5212 else
5213 rettv->vval.v_number = -1;
5214}
5215
5216/*
5217 * "getftype({fname})" function
5218 */
5219 static void
5220f_getftype(typval_T *argvars, typval_T *rettv)
5221{
5222 char_u *fname;
5223 stat_T st;
5224 char_u *type = NULL;
5225 char *t;
5226
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005227 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005228
5229 rettv->v_type = VAR_STRING;
5230 if (mch_lstat((char *)fname, &st) >= 0)
5231 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005232 if (S_ISREG(st.st_mode))
5233 t = "file";
5234 else if (S_ISDIR(st.st_mode))
5235 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005236 else if (S_ISLNK(st.st_mode))
5237 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005238 else if (S_ISBLK(st.st_mode))
5239 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005240 else if (S_ISCHR(st.st_mode))
5241 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005242 else if (S_ISFIFO(st.st_mode))
5243 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005244 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005245 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005246 else
5247 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005248 type = vim_strsave((char_u *)t);
5249 }
5250 rettv->vval.v_string = type;
5251}
5252
5253/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005254 * "getjumplist()" function
5255 */
5256 static void
5257f_getjumplist(typval_T *argvars, typval_T *rettv)
5258{
5259#ifdef FEAT_JUMPLIST
5260 win_T *wp;
5261 int i;
5262 list_T *l;
5263 dict_T *d;
5264#endif
5265
5266 if (rettv_list_alloc(rettv) != OK)
5267 return;
5268
5269#ifdef FEAT_JUMPLIST
5270 wp = find_tabwin(&argvars[0], &argvars[1]);
5271 if (wp == NULL)
5272 return;
5273
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01005274 cleanup_jumplist(wp, TRUE);
5275
Bram Moolenaar4f505882018-02-10 21:06:32 +01005276 l = list_alloc();
5277 if (l == NULL)
5278 return;
5279
5280 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5281 return;
5282 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5283
5284 for (i = 0; i < wp->w_jumplistlen; ++i)
5285 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005286 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5287 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005288 if ((d = dict_alloc()) == NULL)
5289 return;
5290 if (list_append_dict(l, d) == FAIL)
5291 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005292 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5293 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005294 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005295 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005296 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005297 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005298 }
5299#endif
5300}
5301
5302/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005303 * "getline(lnum, [end])" function
5304 */
5305 static void
5306f_getline(typval_T *argvars, typval_T *rettv)
5307{
5308 linenr_T lnum;
5309 linenr_T end;
5310 int retlist;
5311
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005312 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005313 if (argvars[1].v_type == VAR_UNKNOWN)
5314 {
5315 end = 0;
5316 retlist = FALSE;
5317 }
5318 else
5319 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005320 end = tv_get_lnum(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005321 retlist = TRUE;
5322 }
5323
5324 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5325}
5326
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005327#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005328 static void
5329get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5330{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005331 if (what_arg->v_type == VAR_UNKNOWN)
5332 {
5333 if (rettv_list_alloc(rettv) == OK)
5334 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005335 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005336 }
5337 else
5338 {
5339 if (rettv_dict_alloc(rettv) == OK)
5340 if (is_qf || (wp != NULL))
5341 {
5342 if (what_arg->v_type == VAR_DICT)
5343 {
5344 dict_T *d = what_arg->vval.v_dict;
5345
5346 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005347 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005348 }
5349 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005350 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +02005351 }
5352 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005353}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005354#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005355
5356/*
5357 * "getloclist()" function
5358 */
5359 static void
5360f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5361{
5362#ifdef FEAT_QUICKFIX
5363 win_T *wp;
5364
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005365 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005366 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5367#endif
5368}
5369
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005370/*
5371 * "getmatches()" function
5372 */
5373 static void
5374f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5375{
5376#ifdef FEAT_SEARCH_EXTRA
5377 dict_T *dict;
5378 matchitem_T *cur = curwin->w_match_head;
5379 int i;
5380
5381 if (rettv_list_alloc(rettv) == OK)
5382 {
5383 while (cur != NULL)
5384 {
5385 dict = dict_alloc();
5386 if (dict == NULL)
5387 return;
5388 if (cur->match.regprog == NULL)
5389 {
5390 /* match added with matchaddpos() */
5391 for (i = 0; i < MAXPOSMATCH; ++i)
5392 {
5393 llpos_T *llpos;
5394 char buf[6];
5395 list_T *l;
5396
5397 llpos = &cur->pos.pos[i];
5398 if (llpos->lnum == 0)
5399 break;
5400 l = list_alloc();
5401 if (l == NULL)
5402 break;
5403 list_append_number(l, (varnumber_T)llpos->lnum);
5404 if (llpos->col > 0)
5405 {
5406 list_append_number(l, (varnumber_T)llpos->col);
5407 list_append_number(l, (varnumber_T)llpos->len);
5408 }
5409 sprintf(buf, "pos%d", i + 1);
5410 dict_add_list(dict, buf, l);
5411 }
5412 }
5413 else
5414 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02005415 dict_add_string(dict, "pattern", cur->pattern);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005416 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02005417 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
5418 dict_add_number(dict, "priority", (long)cur->priority);
5419 dict_add_number(dict, "id", (long)cur->id);
Bram Moolenaar13505972019-01-24 15:04:48 +01005420# if defined(FEAT_CONCEAL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005421 if (cur->conceal_char)
5422 {
5423 char_u buf[MB_MAXBYTES + 1];
5424
5425 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005426 dict_add_string(dict, "conceal", (char_u *)&buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005427 }
5428# endif
5429 list_append_dict(rettv->vval.v_list, dict);
5430 cur = cur->next;
5431 }
5432 }
5433#endif
5434}
5435
5436/*
5437 * "getpid()" function
5438 */
5439 static void
5440f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5441{
5442 rettv->vval.v_number = mch_get_pid();
5443}
5444
5445 static void
5446getpos_both(
5447 typval_T *argvars,
5448 typval_T *rettv,
5449 int getcurpos)
5450{
5451 pos_T *fp;
5452 list_T *l;
5453 int fnum = -1;
5454
5455 if (rettv_list_alloc(rettv) == OK)
5456 {
5457 l = rettv->vval.v_list;
5458 if (getcurpos)
5459 fp = &curwin->w_cursor;
5460 else
5461 fp = var2fpos(&argvars[0], TRUE, &fnum);
5462 if (fnum != -1)
5463 list_append_number(l, (varnumber_T)fnum);
5464 else
5465 list_append_number(l, (varnumber_T)0);
5466 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5467 : (varnumber_T)0);
5468 list_append_number(l, (fp != NULL)
5469 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5470 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005471 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005472 (varnumber_T)0);
5473 if (getcurpos)
5474 {
5475 update_curswant();
5476 list_append_number(l, curwin->w_curswant == MAXCOL ?
5477 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
5478 }
5479 }
5480 else
5481 rettv->vval.v_number = FALSE;
5482}
5483
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005484/*
5485 * "getcurpos()" function
5486 */
5487 static void
5488f_getcurpos(typval_T *argvars, typval_T *rettv)
5489{
5490 getpos_both(argvars, rettv, TRUE);
5491}
5492
5493/*
5494 * "getpos(string)" function
5495 */
5496 static void
5497f_getpos(typval_T *argvars, typval_T *rettv)
5498{
5499 getpos_both(argvars, rettv, FALSE);
5500}
5501
5502/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005503 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005504 */
5505 static void
5506f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5507{
5508#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005509 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005510#endif
5511}
5512
5513/*
5514 * "getreg()" function
5515 */
5516 static void
5517f_getreg(typval_T *argvars, typval_T *rettv)
5518{
5519 char_u *strregname;
5520 int regname;
5521 int arg2 = FALSE;
5522 int return_list = FALSE;
5523 int error = FALSE;
5524
5525 if (argvars[0].v_type != VAR_UNKNOWN)
5526 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005527 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005528 error = strregname == NULL;
5529 if (argvars[1].v_type != VAR_UNKNOWN)
5530 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005531 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005532 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005533 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005534 }
5535 }
5536 else
5537 strregname = get_vim_var_str(VV_REG);
5538
5539 if (error)
5540 return;
5541
5542 regname = (strregname == NULL ? '"' : *strregname);
5543 if (regname == 0)
5544 regname = '"';
5545
5546 if (return_list)
5547 {
5548 rettv->v_type = VAR_LIST;
5549 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5550 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5551 if (rettv->vval.v_list == NULL)
5552 (void)rettv_list_alloc(rettv);
5553 else
5554 ++rettv->vval.v_list->lv_refcount;
5555 }
5556 else
5557 {
5558 rettv->v_type = VAR_STRING;
5559 rettv->vval.v_string = get_reg_contents(regname,
5560 arg2 ? GREG_EXPR_SRC : 0);
5561 }
5562}
5563
5564/*
5565 * "getregtype()" function
5566 */
5567 static void
5568f_getregtype(typval_T *argvars, typval_T *rettv)
5569{
5570 char_u *strregname;
5571 int regname;
5572 char_u buf[NUMBUFLEN + 2];
5573 long reglen = 0;
5574
5575 if (argvars[0].v_type != VAR_UNKNOWN)
5576 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005577 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005578 if (strregname == NULL) /* type error; errmsg already given */
5579 {
5580 rettv->v_type = VAR_STRING;
5581 rettv->vval.v_string = NULL;
5582 return;
5583 }
5584 }
5585 else
5586 /* Default to v:register */
5587 strregname = get_vim_var_str(VV_REG);
5588
5589 regname = (strregname == NULL ? '"' : *strregname);
5590 if (regname == 0)
5591 regname = '"';
5592
5593 buf[0] = NUL;
5594 buf[1] = NUL;
5595 switch (get_reg_type(regname, &reglen))
5596 {
5597 case MLINE: buf[0] = 'V'; break;
5598 case MCHAR: buf[0] = 'v'; break;
5599 case MBLOCK:
5600 buf[0] = Ctrl_V;
5601 sprintf((char *)buf + 1, "%ld", reglen + 1);
5602 break;
5603 }
5604 rettv->v_type = VAR_STRING;
5605 rettv->vval.v_string = vim_strsave(buf);
5606}
5607
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005608/*
5609 * Returns information (variables, options, etc.) about a tab page
5610 * as a dictionary.
5611 */
5612 static dict_T *
5613get_tabpage_info(tabpage_T *tp, int tp_idx)
5614{
5615 win_T *wp;
5616 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005617 list_T *l;
5618
5619 dict = dict_alloc();
5620 if (dict == NULL)
5621 return NULL;
5622
Bram Moolenaare0be1672018-07-08 16:50:37 +02005623 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005624
5625 l = list_alloc();
5626 if (l != NULL)
5627 {
5628 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5629 wp; wp = wp->w_next)
5630 list_append_number(l, (varnumber_T)wp->w_id);
5631 dict_add_list(dict, "windows", l);
5632 }
5633
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005634 /* Make a reference to tabpage variables */
5635 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005636
5637 return dict;
5638}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005639
5640/*
5641 * "gettabinfo()" function
5642 */
5643 static void
5644f_gettabinfo(typval_T *argvars, typval_T *rettv)
5645{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005646 tabpage_T *tp, *tparg = NULL;
5647 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005648 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005649
5650 if (rettv_list_alloc(rettv) != OK)
5651 return;
5652
5653 if (argvars[0].v_type != VAR_UNKNOWN)
5654 {
5655 /* Information about one tab page */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005656 tparg = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005657 if (tparg == NULL)
5658 return;
5659 }
5660
5661 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005662 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005663 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005664 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005665 if (tparg != NULL && tp != tparg)
5666 continue;
5667 d = get_tabpage_info(tp, tpnr);
5668 if (d != NULL)
5669 list_append_dict(rettv->vval.v_list, d);
5670 if (tparg != NULL)
5671 return;
5672 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005673}
5674
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005675/*
5676 * "gettabvar()" function
5677 */
5678 static void
5679f_gettabvar(typval_T *argvars, typval_T *rettv)
5680{
5681 win_T *oldcurwin;
5682 tabpage_T *tp, *oldtabpage;
5683 dictitem_T *v;
5684 char_u *varname;
5685 int done = FALSE;
5686
5687 rettv->v_type = VAR_STRING;
5688 rettv->vval.v_string = NULL;
5689
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005690 varname = tv_get_string_chk(&argvars[1]);
5691 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005692 if (tp != NULL && varname != NULL)
5693 {
5694 /* Set tp to be our tabpage, temporarily. Also set the window to the
5695 * first window in the tabpage, otherwise the window is not valid. */
5696 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005697 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5698 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005699 {
5700 /* look up the variable */
5701 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5702 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5703 if (v != NULL)
5704 {
5705 copy_tv(&v->di_tv, rettv);
5706 done = TRUE;
5707 }
5708 }
5709
5710 /* restore previous notion of curwin */
5711 restore_win(oldcurwin, oldtabpage, TRUE);
5712 }
5713
5714 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5715 copy_tv(&argvars[2], rettv);
5716}
5717
5718/*
5719 * "gettabwinvar()" function
5720 */
5721 static void
5722f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5723{
5724 getwinvar(argvars, rettv, 1);
5725}
5726
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005727/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01005728 * "gettagstack()" function
5729 */
5730 static void
5731f_gettagstack(typval_T *argvars, typval_T *rettv)
5732{
5733 win_T *wp = curwin; // default is current window
5734
5735 if (rettv_dict_alloc(rettv) != OK)
5736 return;
5737
5738 if (argvars[0].v_type != VAR_UNKNOWN)
5739 {
5740 wp = find_win_by_nr_or_id(&argvars[0]);
5741 if (wp == NULL)
5742 return;
5743 }
5744
5745 get_tagstack(wp, rettv->vval.v_dict);
5746}
5747
5748/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005749 * Returns information about a window as a dictionary.
5750 */
5751 static dict_T *
5752get_win_info(win_T *wp, short tpnr, short winnr)
5753{
5754 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005755
5756 dict = dict_alloc();
5757 if (dict == NULL)
5758 return NULL;
5759
Bram Moolenaare0be1672018-07-08 16:50:37 +02005760 dict_add_number(dict, "tabnr", tpnr);
5761 dict_add_number(dict, "winnr", winnr);
5762 dict_add_number(dict, "winid", wp->w_id);
5763 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005764 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005765#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02005766 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005767#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005768 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005769 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005770 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005771
Bram Moolenaar69905d12017-08-13 18:14:47 +02005772#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02005773 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02005774#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005775#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02005776 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
5777 dict_add_number(dict, "loclist",
5778 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02005779#endif
5780
Bram Moolenaar30567352016-08-27 21:25:44 +02005781 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005782 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005783
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005784 return dict;
5785}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005786
5787/*
5788 * "getwininfo()" function
5789 */
5790 static void
5791f_getwininfo(typval_T *argvars, typval_T *rettv)
5792{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005793 tabpage_T *tp;
5794 win_T *wp = NULL, *wparg = NULL;
5795 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005796 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005797
5798 if (rettv_list_alloc(rettv) != OK)
5799 return;
5800
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005801 if (argvars[0].v_type != VAR_UNKNOWN)
5802 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01005803 wparg = win_id2wp(tv_get_number(&argvars[0]));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005804 if (wparg == NULL)
5805 return;
5806 }
5807
5808 /* Collect information about either all the windows across all the tab
5809 * pages or one particular window.
5810 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005811 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005812 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005813 tabnr++;
5814 winnr = 0;
5815 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005816 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005817 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005818 if (wparg != NULL && wp != wparg)
5819 continue;
5820 d = get_win_info(wp, tabnr, winnr);
5821 if (d != NULL)
5822 list_append_dict(rettv->vval.v_list, d);
5823 if (wparg != NULL)
5824 /* found information about a specific window */
5825 return;
5826 }
5827 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005828}
5829
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005830/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005831 * "win_findbuf()" function
5832 */
5833 static void
5834f_win_findbuf(typval_T *argvars, typval_T *rettv)
5835{
5836 if (rettv_list_alloc(rettv) != FAIL)
5837 win_findbuf(argvars, rettv->vval.v_list);
5838}
5839
5840/*
5841 * "win_getid()" function
5842 */
5843 static void
5844f_win_getid(typval_T *argvars, typval_T *rettv)
5845{
5846 rettv->vval.v_number = win_getid(argvars);
5847}
5848
5849/*
5850 * "win_gotoid()" function
5851 */
5852 static void
5853f_win_gotoid(typval_T *argvars, typval_T *rettv)
5854{
5855 rettv->vval.v_number = win_gotoid(argvars);
5856}
5857
5858/*
5859 * "win_id2tabwin()" function
5860 */
5861 static void
5862f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5863{
5864 if (rettv_list_alloc(rettv) != FAIL)
5865 win_id2tabwin(argvars, rettv->vval.v_list);
5866}
5867
5868/*
5869 * "win_id2win()" function
5870 */
5871 static void
5872f_win_id2win(typval_T *argvars, typval_T *rettv)
5873{
5874 rettv->vval.v_number = win_id2win(argvars);
5875}
5876
5877/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005878 * "win_screenpos()" function
5879 */
5880 static void
5881f_win_screenpos(typval_T *argvars, typval_T *rettv)
5882{
5883 win_T *wp;
5884
5885 if (rettv_list_alloc(rettv) == FAIL)
5886 return;
5887
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005888 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005889 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5890 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5891}
5892
5893/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005894 * "getwinpos({timeout})" function
5895 */
5896 static void
5897f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
5898{
5899 int x = -1;
5900 int y = -1;
5901
5902 if (rettv_list_alloc(rettv) == FAIL)
5903 return;
5904#ifdef FEAT_GUI
5905 if (gui.in_use)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01005906 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005907# if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5908 else
5909# endif
5910#endif
5911#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5912 {
5913 varnumber_T timeout = 100;
5914
5915 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005916 timeout = tv_get_number(&argvars[0]);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005917 term_get_winpos(&x, &y, timeout);
5918 }
5919#endif
5920 list_append_number(rettv->vval.v_list, (varnumber_T)x);
5921 list_append_number(rettv->vval.v_list, (varnumber_T)y);
5922}
5923
5924
5925/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005926 * "getwinposx()" function
5927 */
5928 static void
5929f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5930{
5931 rettv->vval.v_number = -1;
5932#ifdef FEAT_GUI
5933 if (gui.in_use)
5934 {
5935 int x, y;
5936
5937 if (gui_mch_get_winpos(&x, &y) == OK)
5938 rettv->vval.v_number = x;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005939 return;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005940 }
5941#endif
5942#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5943 {
5944 int x, y;
5945
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005946 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005947 rettv->vval.v_number = x;
5948 }
5949#endif
5950}
5951
5952/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005953 * "getwinposy()" function
5954 */
5955 static void
5956f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5957{
5958 rettv->vval.v_number = -1;
5959#ifdef FEAT_GUI
5960 if (gui.in_use)
5961 {
5962 int x, y;
5963
5964 if (gui_mch_get_winpos(&x, &y) == OK)
5965 rettv->vval.v_number = y;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005966 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005967 }
5968#endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005969#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5970 {
5971 int x, y;
5972
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005973 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005974 rettv->vval.v_number = y;
5975 }
5976#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005977}
5978
5979/*
5980 * "getwinvar()" function
5981 */
5982 static void
5983f_getwinvar(typval_T *argvars, typval_T *rettv)
5984{
5985 getwinvar(argvars, rettv, 0);
5986}
5987
5988/*
5989 * "glob()" function
5990 */
5991 static void
5992f_glob(typval_T *argvars, typval_T *rettv)
5993{
5994 int options = WILD_SILENT|WILD_USE_NL;
5995 expand_T xpc;
5996 int error = FALSE;
5997
5998 /* When the optional second argument is non-zero, don't remove matches
5999 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6000 rettv->v_type = VAR_STRING;
6001 if (argvars[1].v_type != VAR_UNKNOWN)
6002 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006003 if (tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006004 options |= WILD_KEEP_ALL;
6005 if (argvars[2].v_type != VAR_UNKNOWN)
6006 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006007 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006008 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006009 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006010 }
6011 if (argvars[3].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006012 && tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006013 options |= WILD_ALLLINKS;
6014 }
6015 }
6016 if (!error)
6017 {
6018 ExpandInit(&xpc);
6019 xpc.xp_context = EXPAND_FILES;
6020 if (p_wic)
6021 options += WILD_ICASE;
6022 if (rettv->v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006023 rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006024 NULL, options, WILD_ALL);
6025 else if (rettv_list_alloc(rettv) != FAIL)
6026 {
6027 int i;
6028
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006029 ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006030 NULL, options, WILD_ALL_KEEP);
6031 for (i = 0; i < xpc.xp_numfiles; i++)
6032 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
6033
6034 ExpandCleanup(&xpc);
6035 }
6036 }
6037 else
6038 rettv->vval.v_string = NULL;
6039}
6040
6041/*
6042 * "globpath()" function
6043 */
6044 static void
6045f_globpath(typval_T *argvars, typval_T *rettv)
6046{
6047 int flags = 0;
6048 char_u buf1[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006049 char_u *file = tv_get_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006050 int error = FALSE;
6051 garray_T ga;
6052 int i;
6053
6054 /* When the optional second argument is non-zero, don't remove matches
6055 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6056 rettv->v_type = VAR_STRING;
6057 if (argvars[2].v_type != VAR_UNKNOWN)
6058 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006059 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006060 flags |= WILD_KEEP_ALL;
6061 if (argvars[3].v_type != VAR_UNKNOWN)
6062 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006063 if (tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006064 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006065 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006066 }
6067 if (argvars[4].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006068 && tv_get_number_chk(&argvars[4], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006069 flags |= WILD_ALLLINKS;
6070 }
6071 }
6072 if (file != NULL && !error)
6073 {
6074 ga_init2(&ga, (int)sizeof(char_u *), 10);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006075 globpath(tv_get_string(&argvars[0]), file, &ga, flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006076 if (rettv->v_type == VAR_STRING)
6077 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
6078 else if (rettv_list_alloc(rettv) != FAIL)
6079 for (i = 0; i < ga.ga_len; ++i)
6080 list_append_string(rettv->vval.v_list,
6081 ((char_u **)(ga.ga_data))[i], -1);
6082 ga_clear_strings(&ga);
6083 }
6084 else
6085 rettv->vval.v_string = NULL;
6086}
6087
6088/*
6089 * "glob2regpat()" function
6090 */
6091 static void
6092f_glob2regpat(typval_T *argvars, typval_T *rettv)
6093{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006094 char_u *pat = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006095
6096 rettv->v_type = VAR_STRING;
6097 rettv->vval.v_string = (pat == NULL)
6098 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
6099}
6100
6101/* for VIM_VERSION_ defines */
6102#include "version.h"
6103
6104/*
6105 * "has()" function
6106 */
6107 static void
6108f_has(typval_T *argvars, typval_T *rettv)
6109{
6110 int i;
6111 char_u *name;
6112 int n = FALSE;
6113 static char *(has_list[]) =
6114 {
6115#ifdef AMIGA
6116 "amiga",
6117# ifdef FEAT_ARP
6118 "arp",
6119# endif
6120#endif
6121#ifdef __BEOS__
6122 "beos",
6123#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006124#if defined(BSD) && !defined(MACOS_X)
6125 "bsd",
6126#endif
6127#ifdef hpux
6128 "hpux",
6129#endif
6130#ifdef __linux__
6131 "linux",
6132#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02006133#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01006134 "mac", /* Mac OS X (and, once, Mac OS Classic) */
6135 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02006136# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01006137 "macunix", /* Mac OS X, with the darwin feature */
6138 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02006139# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006140#endif
6141#ifdef __QNX__
6142 "qnx",
6143#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006144#ifdef SUN_SYSTEM
6145 "sun",
6146#else
6147 "moon",
6148#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006149#ifdef UNIX
6150 "unix",
6151#endif
6152#ifdef VMS
6153 "vms",
6154#endif
6155#ifdef WIN32
6156 "win32",
6157#endif
6158#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
6159 "win32unix",
6160#endif
6161#if defined(WIN64) || defined(_WIN64)
6162 "win64",
6163#endif
6164#ifdef EBCDIC
6165 "ebcdic",
6166#endif
6167#ifndef CASE_INSENSITIVE_FILENAME
6168 "fname_case",
6169#endif
6170#ifdef HAVE_ACL
6171 "acl",
6172#endif
6173#ifdef FEAT_ARABIC
6174 "arabic",
6175#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006176 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006177#ifdef FEAT_AUTOCHDIR
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006178 "autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006179#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006180#ifdef FEAT_AUTOSERVERNAME
6181 "autoservername",
6182#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006183#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006184 "balloon_eval",
6185# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
6186 "balloon_multiline",
6187# endif
6188#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006189#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006190 "balloon_eval_term",
6191#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006192#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6193 "builtin_terms",
6194# ifdef ALL_BUILTIN_TCAPS
6195 "all_builtin_terms",
6196# endif
6197#endif
6198#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
6199 || defined(FEAT_GUI_W32) \
6200 || defined(FEAT_GUI_MOTIF))
6201 "browsefilter",
6202#endif
6203#ifdef FEAT_BYTEOFF
6204 "byte_offset",
6205#endif
6206#ifdef FEAT_JOB_CHANNEL
6207 "channel",
6208#endif
6209#ifdef FEAT_CINDENT
6210 "cindent",
6211#endif
6212#ifdef FEAT_CLIENTSERVER
6213 "clientserver",
6214#endif
6215#ifdef FEAT_CLIPBOARD
6216 "clipboard",
6217#endif
6218#ifdef FEAT_CMDL_COMPL
6219 "cmdline_compl",
6220#endif
6221#ifdef FEAT_CMDHIST
6222 "cmdline_hist",
6223#endif
6224#ifdef FEAT_COMMENTS
6225 "comments",
6226#endif
6227#ifdef FEAT_CONCEAL
6228 "conceal",
6229#endif
6230#ifdef FEAT_CRYPT
6231 "cryptv",
6232 "crypt-blowfish",
6233 "crypt-blowfish2",
6234#endif
6235#ifdef FEAT_CSCOPE
6236 "cscope",
6237#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006238 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006239#ifdef CURSOR_SHAPE
6240 "cursorshape",
6241#endif
6242#ifdef DEBUG
6243 "debug",
6244#endif
6245#ifdef FEAT_CON_DIALOG
6246 "dialog_con",
6247#endif
6248#ifdef FEAT_GUI_DIALOG
6249 "dialog_gui",
6250#endif
6251#ifdef FEAT_DIFF
6252 "diff",
6253#endif
6254#ifdef FEAT_DIGRAPHS
6255 "digraphs",
6256#endif
6257#ifdef FEAT_DIRECTX
6258 "directx",
6259#endif
6260#ifdef FEAT_DND
6261 "dnd",
6262#endif
6263#ifdef FEAT_EMACS_TAGS
6264 "emacs_tags",
6265#endif
6266 "eval", /* always present, of course! */
6267 "ex_extra", /* graduated feature */
6268#ifdef FEAT_SEARCH_EXTRA
6269 "extra_search",
6270#endif
6271#ifdef FEAT_FKMAP
6272 "farsi",
6273#endif
6274#ifdef FEAT_SEARCHPATH
6275 "file_in_path",
6276#endif
6277#ifdef FEAT_FILTERPIPE
6278 "filterpipe",
6279#endif
6280#ifdef FEAT_FIND_ID
6281 "find_in_path",
6282#endif
6283#ifdef FEAT_FLOAT
6284 "float",
6285#endif
6286#ifdef FEAT_FOLDING
6287 "folding",
6288#endif
6289#ifdef FEAT_FOOTER
6290 "footer",
6291#endif
6292#if !defined(USE_SYSTEM) && defined(UNIX)
6293 "fork",
6294#endif
6295#ifdef FEAT_GETTEXT
6296 "gettext",
6297#endif
6298#ifdef FEAT_GUI
6299 "gui",
6300#endif
6301#ifdef FEAT_GUI_ATHENA
6302# ifdef FEAT_GUI_NEXTAW
6303 "gui_neXtaw",
6304# else
6305 "gui_athena",
6306# endif
6307#endif
6308#ifdef FEAT_GUI_GTK
6309 "gui_gtk",
6310# ifdef USE_GTK3
6311 "gui_gtk3",
6312# else
6313 "gui_gtk2",
6314# endif
6315#endif
6316#ifdef FEAT_GUI_GNOME
6317 "gui_gnome",
6318#endif
6319#ifdef FEAT_GUI_MAC
6320 "gui_mac",
6321#endif
6322#ifdef FEAT_GUI_MOTIF
6323 "gui_motif",
6324#endif
6325#ifdef FEAT_GUI_PHOTON
6326 "gui_photon",
6327#endif
6328#ifdef FEAT_GUI_W32
6329 "gui_win32",
6330#endif
6331#ifdef FEAT_HANGULIN
6332 "hangul_input",
6333#endif
6334#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6335 "iconv",
6336#endif
6337#ifdef FEAT_INS_EXPAND
6338 "insert_expand",
6339#endif
6340#ifdef FEAT_JOB_CHANNEL
6341 "job",
6342#endif
6343#ifdef FEAT_JUMPLIST
6344 "jumplist",
6345#endif
6346#ifdef FEAT_KEYMAP
6347 "keymap",
6348#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006349 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006350#ifdef FEAT_LANGMAP
6351 "langmap",
6352#endif
6353#ifdef FEAT_LIBCALL
6354 "libcall",
6355#endif
6356#ifdef FEAT_LINEBREAK
6357 "linebreak",
6358#endif
6359#ifdef FEAT_LISP
6360 "lispindent",
6361#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006362 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006363#ifdef FEAT_LOCALMAP
6364 "localmap",
6365#endif
6366#ifdef FEAT_LUA
6367# ifndef DYNAMIC_LUA
6368 "lua",
6369# endif
6370#endif
6371#ifdef FEAT_MENU
6372 "menu",
6373#endif
6374#ifdef FEAT_SESSION
6375 "mksession",
6376#endif
6377#ifdef FEAT_MODIFY_FNAME
6378 "modify_fname",
6379#endif
6380#ifdef FEAT_MOUSE
6381 "mouse",
6382#endif
6383#ifdef FEAT_MOUSESHAPE
6384 "mouseshape",
6385#endif
6386#if defined(UNIX) || defined(VMS)
6387# ifdef FEAT_MOUSE_DEC
6388 "mouse_dec",
6389# endif
6390# ifdef FEAT_MOUSE_GPM
6391 "mouse_gpm",
6392# endif
6393# ifdef FEAT_MOUSE_JSB
6394 "mouse_jsbterm",
6395# endif
6396# ifdef FEAT_MOUSE_NET
6397 "mouse_netterm",
6398# endif
6399# ifdef FEAT_MOUSE_PTERM
6400 "mouse_pterm",
6401# endif
6402# ifdef FEAT_MOUSE_SGR
6403 "mouse_sgr",
6404# endif
6405# ifdef FEAT_SYSMOUSE
6406 "mouse_sysmouse",
6407# endif
6408# ifdef FEAT_MOUSE_URXVT
6409 "mouse_urxvt",
6410# endif
6411# ifdef FEAT_MOUSE_XTERM
6412 "mouse_xterm",
6413# endif
6414#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006415 "multi_byte",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006416#ifdef FEAT_MBYTE_IME
6417 "multi_byte_ime",
6418#endif
6419#ifdef FEAT_MULTI_LANG
6420 "multi_lang",
6421#endif
6422#ifdef FEAT_MZSCHEME
6423#ifndef DYNAMIC_MZSCHEME
6424 "mzscheme",
6425#endif
6426#endif
6427#ifdef FEAT_NUM64
6428 "num64",
6429#endif
6430#ifdef FEAT_OLE
6431 "ole",
6432#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006433#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006434 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006435#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006436#ifdef FEAT_PATH_EXTRA
6437 "path_extra",
6438#endif
6439#ifdef FEAT_PERL
6440#ifndef DYNAMIC_PERL
6441 "perl",
6442#endif
6443#endif
6444#ifdef FEAT_PERSISTENT_UNDO
6445 "persistent_undo",
6446#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006447#if defined(FEAT_PYTHON)
6448 "python_compiled",
6449# if defined(DYNAMIC_PYTHON)
6450 "python_dynamic",
6451# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006452 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006453 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006454# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006455#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006456#if defined(FEAT_PYTHON3)
6457 "python3_compiled",
6458# if defined(DYNAMIC_PYTHON3)
6459 "python3_dynamic",
6460# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006461 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006462 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006463# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006464#endif
6465#ifdef FEAT_POSTSCRIPT
6466 "postscript",
6467#endif
6468#ifdef FEAT_PRINTER
6469 "printer",
6470#endif
6471#ifdef FEAT_PROFILE
6472 "profile",
6473#endif
6474#ifdef FEAT_RELTIME
6475 "reltime",
6476#endif
6477#ifdef FEAT_QUICKFIX
6478 "quickfix",
6479#endif
6480#ifdef FEAT_RIGHTLEFT
6481 "rightleft",
6482#endif
6483#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6484 "ruby",
6485#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006486 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006487#ifdef FEAT_CMDL_INFO
6488 "showcmd",
6489 "cmdline_info",
6490#endif
6491#ifdef FEAT_SIGNS
6492 "signs",
6493#endif
6494#ifdef FEAT_SMARTINDENT
6495 "smartindent",
6496#endif
6497#ifdef STARTUPTIME
6498 "startuptime",
6499#endif
6500#ifdef FEAT_STL_OPT
6501 "statusline",
6502#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006503#ifdef FEAT_NETBEANS_INTG
6504 "netbeans_intg",
6505#endif
6506#ifdef FEAT_SPELL
6507 "spell",
6508#endif
6509#ifdef FEAT_SYN_HL
6510 "syntax",
6511#endif
6512#if defined(USE_SYSTEM) || !defined(UNIX)
6513 "system",
6514#endif
6515#ifdef FEAT_TAG_BINS
6516 "tag_binary",
6517#endif
6518#ifdef FEAT_TAG_OLDSTATIC
6519 "tag_old_static",
6520#endif
6521#ifdef FEAT_TAG_ANYWHITE
6522 "tag_any_white",
6523#endif
6524#ifdef FEAT_TCL
6525# ifndef DYNAMIC_TCL
6526 "tcl",
6527# endif
6528#endif
6529#ifdef FEAT_TERMGUICOLORS
6530 "termguicolors",
6531#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006532#if defined(FEAT_TERMINAL) && !defined(WIN3264)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006533 "terminal",
6534#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006535#ifdef TERMINFO
6536 "terminfo",
6537#endif
6538#ifdef FEAT_TERMRESPONSE
6539 "termresponse",
6540#endif
6541#ifdef FEAT_TEXTOBJ
6542 "textobjects",
6543#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006544#ifdef FEAT_TEXT_PROP
6545 "textprop",
6546#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006547#ifdef HAVE_TGETENT
6548 "tgetent",
6549#endif
6550#ifdef FEAT_TIMERS
6551 "timers",
6552#endif
6553#ifdef FEAT_TITLE
6554 "title",
6555#endif
6556#ifdef FEAT_TOOLBAR
6557 "toolbar",
6558#endif
6559#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6560 "unnamedplus",
6561#endif
6562#ifdef FEAT_USR_CMDS
6563 "user-commands", /* was accidentally included in 5.4 */
6564 "user_commands",
6565#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006566#ifdef FEAT_VARTABS
6567 "vartabs",
6568#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006569#ifdef FEAT_VIMINFO
6570 "viminfo",
6571#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006572 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006573 "virtualedit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006574 "visual",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006575 "visualextra",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006576 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006577#ifdef FEAT_VTP
6578 "vtp",
6579#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006580#ifdef FEAT_WILDIGN
6581 "wildignore",
6582#endif
6583#ifdef FEAT_WILDMENU
6584 "wildmenu",
6585#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006586 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006587#ifdef FEAT_WAK
6588 "winaltkeys",
6589#endif
6590#ifdef FEAT_WRITEBACKUP
6591 "writebackup",
6592#endif
6593#ifdef FEAT_XIM
6594 "xim",
6595#endif
6596#ifdef FEAT_XFONTSET
6597 "xfontset",
6598#endif
6599#ifdef FEAT_XPM_W32
6600 "xpm",
6601 "xpm_w32", /* for backward compatibility */
6602#else
6603# if defined(HAVE_XPM)
6604 "xpm",
6605# endif
6606#endif
6607#ifdef USE_XSMP
6608 "xsmp",
6609#endif
6610#ifdef USE_XSMP_INTERACT
6611 "xsmp_interact",
6612#endif
6613#ifdef FEAT_XCLIPBOARD
6614 "xterm_clipboard",
6615#endif
6616#ifdef FEAT_XTERM_SAVE
6617 "xterm_save",
6618#endif
6619#if defined(UNIX) && defined(FEAT_X11)
6620 "X11",
6621#endif
6622 NULL
6623 };
6624
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006625 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006626 for (i = 0; has_list[i] != NULL; ++i)
6627 if (STRICMP(name, has_list[i]) == 0)
6628 {
6629 n = TRUE;
6630 break;
6631 }
6632
6633 if (n == FALSE)
6634 {
6635 if (STRNICMP(name, "patch", 5) == 0)
6636 {
6637 if (name[5] == '-'
6638 && STRLEN(name) >= 11
6639 && vim_isdigit(name[6])
6640 && vim_isdigit(name[8])
6641 && vim_isdigit(name[10]))
6642 {
6643 int major = atoi((char *)name + 6);
6644 int minor = atoi((char *)name + 8);
6645
6646 /* Expect "patch-9.9.01234". */
6647 n = (major < VIM_VERSION_MAJOR
6648 || (major == VIM_VERSION_MAJOR
6649 && (minor < VIM_VERSION_MINOR
6650 || (minor == VIM_VERSION_MINOR
6651 && has_patch(atoi((char *)name + 10))))));
6652 }
6653 else
6654 n = has_patch(atoi((char *)name + 5));
6655 }
6656 else if (STRICMP(name, "vim_starting") == 0)
6657 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006658 else if (STRICMP(name, "ttyin") == 0)
6659 n = mch_input_isatty();
6660 else if (STRICMP(name, "ttyout") == 0)
6661 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006662 else if (STRICMP(name, "multi_byte_encoding") == 0)
6663 n = has_mbyte;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006664#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
6665 else if (STRICMP(name, "balloon_multiline") == 0)
6666 n = multiline_balloon_available();
6667#endif
6668#ifdef DYNAMIC_TCL
6669 else if (STRICMP(name, "tcl") == 0)
6670 n = tcl_enabled(FALSE);
6671#endif
6672#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6673 else if (STRICMP(name, "iconv") == 0)
6674 n = iconv_enabled(FALSE);
6675#endif
6676#ifdef DYNAMIC_LUA
6677 else if (STRICMP(name, "lua") == 0)
6678 n = lua_enabled(FALSE);
6679#endif
6680#ifdef DYNAMIC_MZSCHEME
6681 else if (STRICMP(name, "mzscheme") == 0)
6682 n = mzscheme_enabled(FALSE);
6683#endif
6684#ifdef DYNAMIC_RUBY
6685 else if (STRICMP(name, "ruby") == 0)
6686 n = ruby_enabled(FALSE);
6687#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006688#ifdef DYNAMIC_PYTHON
6689 else if (STRICMP(name, "python") == 0)
6690 n = python_enabled(FALSE);
6691#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006692#ifdef DYNAMIC_PYTHON3
6693 else if (STRICMP(name, "python3") == 0)
6694 n = python3_enabled(FALSE);
6695#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006696#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6697 else if (STRICMP(name, "pythonx") == 0)
6698 {
6699# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6700 if (p_pyx == 0)
6701 n = python3_enabled(FALSE) || python_enabled(FALSE);
6702 else if (p_pyx == 3)
6703 n = python3_enabled(FALSE);
6704 else if (p_pyx == 2)
6705 n = python_enabled(FALSE);
6706# elif defined(DYNAMIC_PYTHON)
6707 n = python_enabled(FALSE);
6708# elif defined(DYNAMIC_PYTHON3)
6709 n = python3_enabled(FALSE);
6710# endif
6711 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006712#endif
6713#ifdef DYNAMIC_PERL
6714 else if (STRICMP(name, "perl") == 0)
6715 n = perl_enabled(FALSE);
6716#endif
6717#ifdef FEAT_GUI
6718 else if (STRICMP(name, "gui_running") == 0)
6719 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006720# ifdef FEAT_BROWSE
6721 else if (STRICMP(name, "browse") == 0)
6722 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6723# endif
6724#endif
6725#ifdef FEAT_SYN_HL
6726 else if (STRICMP(name, "syntax_items") == 0)
6727 n = syntax_present(curwin);
6728#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006729#ifdef FEAT_VTP
6730 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02006731 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006732#endif
6733#ifdef FEAT_NETBEANS_INTG
6734 else if (STRICMP(name, "netbeans_enabled") == 0)
6735 n = netbeans_active();
6736#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006737#if defined(FEAT_TERMINAL) && defined(WIN3264)
6738 else if (STRICMP(name, "terminal") == 0)
6739 n = terminal_enabled();
6740#endif
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006741#if defined(FEAT_TERMINAL) && defined(WIN3264)
6742 else if (STRICMP(name, "conpty") == 0)
6743 n = use_conpty();
6744#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006745 }
6746
6747 rettv->vval.v_number = n;
6748}
6749
6750/*
6751 * "has_key()" function
6752 */
6753 static void
6754f_has_key(typval_T *argvars, typval_T *rettv)
6755{
6756 if (argvars[0].v_type != VAR_DICT)
6757 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006758 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006759 return;
6760 }
6761 if (argvars[0].vval.v_dict == NULL)
6762 return;
6763
6764 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006765 tv_get_string(&argvars[1]), -1) != NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006766}
6767
6768/*
6769 * "haslocaldir()" function
6770 */
6771 static void
6772f_haslocaldir(typval_T *argvars, typval_T *rettv)
6773{
6774 win_T *wp = NULL;
6775
6776 wp = find_tabwin(&argvars[0], &argvars[1]);
6777 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6778}
6779
6780/*
6781 * "hasmapto()" function
6782 */
6783 static void
6784f_hasmapto(typval_T *argvars, typval_T *rettv)
6785{
6786 char_u *name;
6787 char_u *mode;
6788 char_u buf[NUMBUFLEN];
6789 int abbr = FALSE;
6790
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006791 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006792 if (argvars[1].v_type == VAR_UNKNOWN)
6793 mode = (char_u *)"nvo";
6794 else
6795 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006796 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006797 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006798 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006799 }
6800
6801 if (map_to_exists(name, mode, abbr))
6802 rettv->vval.v_number = TRUE;
6803 else
6804 rettv->vval.v_number = FALSE;
6805}
6806
6807/*
6808 * "histadd()" function
6809 */
6810 static void
6811f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6812{
6813#ifdef FEAT_CMDHIST
6814 int histype;
6815 char_u *str;
6816 char_u buf[NUMBUFLEN];
6817#endif
6818
6819 rettv->vval.v_number = FALSE;
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006820 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006821 return;
6822#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006823 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006824 histype = str != NULL ? get_histtype(str) : -1;
6825 if (histype >= 0)
6826 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006827 str = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006828 if (*str != NUL)
6829 {
6830 init_history();
6831 add_to_history(histype, str, FALSE, NUL);
6832 rettv->vval.v_number = TRUE;
6833 return;
6834 }
6835 }
6836#endif
6837}
6838
6839/*
6840 * "histdel()" function
6841 */
6842 static void
6843f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6844{
6845#ifdef FEAT_CMDHIST
6846 int n;
6847 char_u buf[NUMBUFLEN];
6848 char_u *str;
6849
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006850 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006851 if (str == NULL)
6852 n = 0;
6853 else if (argvars[1].v_type == VAR_UNKNOWN)
6854 /* only one argument: clear entire history */
6855 n = clr_history(get_histtype(str));
6856 else if (argvars[1].v_type == VAR_NUMBER)
6857 /* index given: remove that entry */
6858 n = del_history_idx(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006859 (int)tv_get_number(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006860 else
6861 /* string given: remove all matching entries */
6862 n = del_history_entry(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006863 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006864 rettv->vval.v_number = n;
6865#endif
6866}
6867
6868/*
6869 * "histget()" function
6870 */
6871 static void
6872f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6873{
6874#ifdef FEAT_CMDHIST
6875 int type;
6876 int idx;
6877 char_u *str;
6878
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006879 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006880 if (str == NULL)
6881 rettv->vval.v_string = NULL;
6882 else
6883 {
6884 type = get_histtype(str);
6885 if (argvars[1].v_type == VAR_UNKNOWN)
6886 idx = get_history_idx(type);
6887 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006888 idx = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006889 /* -1 on type error */
6890 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6891 }
6892#else
6893 rettv->vval.v_string = NULL;
6894#endif
6895 rettv->v_type = VAR_STRING;
6896}
6897
6898/*
6899 * "histnr()" function
6900 */
6901 static void
6902f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6903{
6904 int i;
6905
6906#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006907 char_u *history = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006908
6909 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6910 if (i >= HIST_CMD && i < HIST_COUNT)
6911 i = get_history_idx(i);
6912 else
6913#endif
6914 i = -1;
6915 rettv->vval.v_number = i;
6916}
6917
6918/*
6919 * "highlightID(name)" function
6920 */
6921 static void
6922f_hlID(typval_T *argvars, typval_T *rettv)
6923{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006924 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006925}
6926
6927/*
6928 * "highlight_exists()" function
6929 */
6930 static void
6931f_hlexists(typval_T *argvars, typval_T *rettv)
6932{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006933 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006934}
6935
6936/*
6937 * "hostname()" function
6938 */
6939 static void
6940f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6941{
6942 char_u hostname[256];
6943
6944 mch_get_host_name(hostname, 256);
6945 rettv->v_type = VAR_STRING;
6946 rettv->vval.v_string = vim_strsave(hostname);
6947}
6948
6949/*
6950 * iconv() function
6951 */
6952 static void
6953f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6954{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006955 char_u buf1[NUMBUFLEN];
6956 char_u buf2[NUMBUFLEN];
6957 char_u *from, *to, *str;
6958 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006959
6960 rettv->v_type = VAR_STRING;
6961 rettv->vval.v_string = NULL;
6962
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006963 str = tv_get_string(&argvars[0]);
6964 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
6965 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006966 vimconv.vc_type = CONV_NONE;
6967 convert_setup(&vimconv, from, to);
6968
6969 /* If the encodings are equal, no conversion needed. */
6970 if (vimconv.vc_type == CONV_NONE)
6971 rettv->vval.v_string = vim_strsave(str);
6972 else
6973 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6974
6975 convert_setup(&vimconv, NULL, NULL);
6976 vim_free(from);
6977 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006978}
6979
6980/*
6981 * "indent()" function
6982 */
6983 static void
6984f_indent(typval_T *argvars, typval_T *rettv)
6985{
6986 linenr_T lnum;
6987
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006988 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006989 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6990 rettv->vval.v_number = get_indent_lnum(lnum);
6991 else
6992 rettv->vval.v_number = -1;
6993}
6994
6995/*
6996 * "index()" function
6997 */
6998 static void
6999f_index(typval_T *argvars, typval_T *rettv)
7000{
7001 list_T *l;
7002 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007003 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007004 long idx = 0;
7005 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007006 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007007
7008 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007009 if (argvars[0].v_type == VAR_BLOB)
7010 {
7011 typval_T tv;
7012 int start = 0;
7013
7014 if (argvars[2].v_type != VAR_UNKNOWN)
7015 {
7016 start = tv_get_number_chk(&argvars[2], &error);
7017 if (error)
7018 return;
7019 }
7020 b = argvars[0].vval.v_blob;
7021 if (b == NULL)
7022 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01007023 if (start < 0)
7024 {
7025 start = blob_len(b) + start;
7026 if (start < 0)
7027 start = 0;
7028 }
7029
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007030 for (idx = start; idx < blob_len(b); ++idx)
7031 {
7032 tv.v_type = VAR_NUMBER;
7033 tv.vval.v_number = blob_get(b, idx);
7034 if (tv_equal(&tv, &argvars[1], ic, FALSE))
7035 {
7036 rettv->vval.v_number = idx;
7037 return;
7038 }
7039 }
7040 return;
7041 }
7042 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007043 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007044 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007045 return;
7046 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007047
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007048 l = argvars[0].vval.v_list;
7049 if (l != NULL)
7050 {
7051 item = l->lv_first;
7052 if (argvars[2].v_type != VAR_UNKNOWN)
7053 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007054 /* Start at specified item. Use the cached index that list_find()
7055 * sets, so that a negative number also works. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007056 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007057 idx = l->lv_idx;
7058 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007059 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007060 if (error)
7061 item = NULL;
7062 }
7063
7064 for ( ; item != NULL; item = item->li_next, ++idx)
7065 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
7066 {
7067 rettv->vval.v_number = idx;
7068 break;
7069 }
7070 }
7071}
7072
7073static int inputsecret_flag = 0;
7074
7075/*
7076 * "input()" function
7077 * Also handles inputsecret() when inputsecret is set.
7078 */
7079 static void
7080f_input(typval_T *argvars, typval_T *rettv)
7081{
7082 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
7083}
7084
7085/*
7086 * "inputdialog()" function
7087 */
7088 static void
7089f_inputdialog(typval_T *argvars, typval_T *rettv)
7090{
7091#if defined(FEAT_GUI_TEXTDIALOG)
7092 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
7093 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
7094 {
7095 char_u *message;
7096 char_u buf[NUMBUFLEN];
7097 char_u *defstr = (char_u *)"";
7098
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007099 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007100 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007101 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007102 vim_strncpy(IObuff, defstr, IOSIZE - 1);
7103 else
7104 IObuff[0] = NUL;
7105 if (message != NULL && defstr != NULL
7106 && do_dialog(VIM_QUESTION, NULL, message,
7107 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
7108 rettv->vval.v_string = vim_strsave(IObuff);
7109 else
7110 {
7111 if (message != NULL && defstr != NULL
7112 && argvars[1].v_type != VAR_UNKNOWN
7113 && argvars[2].v_type != VAR_UNKNOWN)
7114 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007115 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007116 else
7117 rettv->vval.v_string = NULL;
7118 }
7119 rettv->v_type = VAR_STRING;
7120 }
7121 else
7122#endif
7123 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
7124}
7125
7126/*
7127 * "inputlist()" function
7128 */
7129 static void
7130f_inputlist(typval_T *argvars, typval_T *rettv)
7131{
7132 listitem_T *li;
7133 int selected;
7134 int mouse_used;
7135
7136#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007137 /* While starting up, there is no place to enter text. When running tests
7138 * with --not-a-term we assume feedkeys() will be used. */
7139 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007140 return;
7141#endif
7142 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
7143 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007144 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007145 return;
7146 }
7147
7148 msg_start();
7149 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
7150 lines_left = Rows; /* avoid more prompt */
7151 msg_scroll = TRUE;
7152 msg_clr_eos();
7153
7154 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
7155 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007156 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007157 msg_putchar('\n');
7158 }
7159
7160 /* Ask for choice. */
7161 selected = prompt_for_number(&mouse_used);
7162 if (mouse_used)
7163 selected -= lines_left;
7164
7165 rettv->vval.v_number = selected;
7166}
7167
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007168static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7169
7170/*
7171 * "inputrestore()" function
7172 */
7173 static void
7174f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7175{
7176 if (ga_userinput.ga_len > 0)
7177 {
7178 --ga_userinput.ga_len;
7179 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7180 + ga_userinput.ga_len);
7181 /* default return is zero == OK */
7182 }
7183 else if (p_verbose > 1)
7184 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007185 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007186 rettv->vval.v_number = 1; /* Failed */
7187 }
7188}
7189
7190/*
7191 * "inputsave()" function
7192 */
7193 static void
7194f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7195{
7196 /* Add an entry to the stack of typeahead storage. */
7197 if (ga_grow(&ga_userinput, 1) == OK)
7198 {
7199 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7200 + ga_userinput.ga_len);
7201 ++ga_userinput.ga_len;
7202 /* default return is zero == OK */
7203 }
7204 else
7205 rettv->vval.v_number = 1; /* Failed */
7206}
7207
7208/*
7209 * "inputsecret()" function
7210 */
7211 static void
7212f_inputsecret(typval_T *argvars, typval_T *rettv)
7213{
7214 ++cmdline_star;
7215 ++inputsecret_flag;
7216 f_input(argvars, rettv);
7217 --cmdline_star;
7218 --inputsecret_flag;
7219}
7220
7221/*
7222 * "insert()" function
7223 */
7224 static void
7225f_insert(typval_T *argvars, typval_T *rettv)
7226{
7227 long before = 0;
7228 listitem_T *item;
7229 list_T *l;
7230 int error = FALSE;
7231
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007232 if (argvars[0].v_type == VAR_BLOB)
7233 {
7234 int val, len;
7235 char_u *p;
7236
7237 len = blob_len(argvars[0].vval.v_blob);
7238 if (argvars[2].v_type != VAR_UNKNOWN)
7239 {
7240 before = (long)tv_get_number_chk(&argvars[2], &error);
7241 if (error)
7242 return; // type error; errmsg already given
7243 if (before < 0 || before > len)
7244 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007245 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007246 return;
7247 }
7248 }
7249 val = tv_get_number_chk(&argvars[1], &error);
7250 if (error)
7251 return;
7252 if (val < 0 || val > 255)
7253 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007254 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007255 return;
7256 }
7257
7258 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
7259 return;
7260 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
7261 mch_memmove(p + before + 1, p + before, (size_t)len - before);
7262 *(p + before) = val;
7263 ++argvars[0].vval.v_blob->bv_ga.ga_len;
7264
7265 copy_tv(&argvars[0], rettv);
7266 }
7267 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007268 semsg(_(e_listblobarg), "insert()");
Bram Moolenaar05c00c02019-02-11 22:00:11 +01007269 else if ((l = argvars[0].vval.v_list) != NULL
7270 && !var_check_lock(l->lv_lock,
7271 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007272 {
7273 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007274 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007275 if (error)
7276 return; /* type error; errmsg already given */
7277
7278 if (before == l->lv_len)
7279 item = NULL;
7280 else
7281 {
7282 item = list_find(l, before);
7283 if (item == NULL)
7284 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007285 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007286 l = NULL;
7287 }
7288 }
7289 if (l != NULL)
7290 {
7291 list_insert_tv(l, &argvars[1], item);
7292 copy_tv(&argvars[0], rettv);
7293 }
7294 }
7295}
7296
7297/*
7298 * "invert(expr)" function
7299 */
7300 static void
7301f_invert(typval_T *argvars, typval_T *rettv)
7302{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007303 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007304}
7305
7306/*
7307 * "isdirectory()" function
7308 */
7309 static void
7310f_isdirectory(typval_T *argvars, typval_T *rettv)
7311{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007312 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007313}
7314
7315/*
7316 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7317 * or it refers to a List or Dictionary that is locked.
7318 */
7319 static int
7320tv_islocked(typval_T *tv)
7321{
7322 return (tv->v_lock & VAR_LOCKED)
7323 || (tv->v_type == VAR_LIST
7324 && tv->vval.v_list != NULL
7325 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7326 || (tv->v_type == VAR_DICT
7327 && tv->vval.v_dict != NULL
7328 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7329}
7330
7331/*
7332 * "islocked()" function
7333 */
7334 static void
7335f_islocked(typval_T *argvars, typval_T *rettv)
7336{
7337 lval_T lv;
7338 char_u *end;
7339 dictitem_T *di;
7340
7341 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007342 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007343 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007344 if (end != NULL && lv.ll_name != NULL)
7345 {
7346 if (*end != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007347 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007348 else
7349 {
7350 if (lv.ll_tv == NULL)
7351 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007352 di = find_var(lv.ll_name, NULL, TRUE);
7353 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007354 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007355 /* Consider a variable locked when:
7356 * 1. the variable itself is locked
7357 * 2. the value of the variable is locked.
7358 * 3. the List or Dict value is locked.
7359 */
7360 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7361 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007362 }
7363 }
7364 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007365 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007366 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007367 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007368 else if (lv.ll_list != NULL)
7369 /* List item. */
7370 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7371 else
7372 /* Dictionary item. */
7373 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7374 }
7375 }
7376
7377 clear_lval(&lv);
7378}
7379
7380#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7381/*
7382 * "isnan()" function
7383 */
7384 static void
7385f_isnan(typval_T *argvars, typval_T *rettv)
7386{
7387 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7388 && isnan(argvars[0].vval.v_float);
7389}
7390#endif
7391
7392/*
7393 * "items(dict)" function
7394 */
7395 static void
7396f_items(typval_T *argvars, typval_T *rettv)
7397{
7398 dict_list(argvars, rettv, 2);
7399}
7400
7401#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
7402/*
7403 * Get the job from the argument.
7404 * Returns NULL if the job is invalid.
7405 */
7406 static job_T *
7407get_job_arg(typval_T *tv)
7408{
7409 job_T *job;
7410
7411 if (tv->v_type != VAR_JOB)
7412 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007413 semsg(_(e_invarg2), tv_get_string(tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007414 return NULL;
7415 }
7416 job = tv->vval.v_job;
7417
7418 if (job == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007419 emsg(_("E916: not a valid job"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007420 return job;
7421}
7422
7423/*
7424 * "job_getchannel()" function
7425 */
7426 static void
7427f_job_getchannel(typval_T *argvars, typval_T *rettv)
7428{
7429 job_T *job = get_job_arg(&argvars[0]);
7430
7431 if (job != NULL)
7432 {
7433 rettv->v_type = VAR_CHANNEL;
7434 rettv->vval.v_channel = job->jv_channel;
7435 if (job->jv_channel != NULL)
7436 ++job->jv_channel->ch_refcount;
7437 }
7438}
7439
7440/*
7441 * "job_info()" function
7442 */
7443 static void
7444f_job_info(typval_T *argvars, typval_T *rettv)
7445{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007446 if (argvars[0].v_type != VAR_UNKNOWN)
7447 {
7448 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007449
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007450 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7451 job_info(job, rettv->vval.v_dict);
7452 }
7453 else if (rettv_list_alloc(rettv) == OK)
7454 job_info_all(rettv->vval.v_list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007455}
7456
7457/*
7458 * "job_setoptions()" function
7459 */
7460 static void
7461f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7462{
7463 job_T *job = get_job_arg(&argvars[0]);
7464 jobopt_T opt;
7465
7466 if (job == NULL)
7467 return;
7468 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007469 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007470 job_set_options(job, &opt);
7471 free_job_options(&opt);
7472}
7473
7474/*
7475 * "job_start()" function
7476 */
7477 static void
7478f_job_start(typval_T *argvars, typval_T *rettv)
7479{
7480 rettv->v_type = VAR_JOB;
7481 if (check_restricted() || check_secure())
7482 return;
Bram Moolenaar493359e2018-06-12 20:25:52 +02007483 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007484}
7485
7486/*
7487 * "job_status()" function
7488 */
7489 static void
7490f_job_status(typval_T *argvars, typval_T *rettv)
7491{
7492 job_T *job = get_job_arg(&argvars[0]);
7493
7494 if (job != NULL)
7495 {
7496 rettv->v_type = VAR_STRING;
7497 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7498 }
7499}
7500
7501/*
7502 * "job_stop()" function
7503 */
7504 static void
7505f_job_stop(typval_T *argvars, typval_T *rettv)
7506{
7507 job_T *job = get_job_arg(&argvars[0]);
7508
7509 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007510 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007511}
7512#endif
7513
7514/*
7515 * "join()" function
7516 */
7517 static void
7518f_join(typval_T *argvars, typval_T *rettv)
7519{
7520 garray_T ga;
7521 char_u *sep;
7522
7523 if (argvars[0].v_type != VAR_LIST)
7524 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007525 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007526 return;
7527 }
7528 if (argvars[0].vval.v_list == NULL)
7529 return;
7530 if (argvars[1].v_type == VAR_UNKNOWN)
7531 sep = (char_u *)" ";
7532 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007533 sep = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007534
7535 rettv->v_type = VAR_STRING;
7536
7537 if (sep != NULL)
7538 {
7539 ga_init2(&ga, (int)sizeof(char), 80);
7540 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7541 ga_append(&ga, NUL);
7542 rettv->vval.v_string = (char_u *)ga.ga_data;
7543 }
7544 else
7545 rettv->vval.v_string = NULL;
7546}
7547
7548/*
7549 * "js_decode()" function
7550 */
7551 static void
7552f_js_decode(typval_T *argvars, typval_T *rettv)
7553{
7554 js_read_T reader;
7555
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007556 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007557 reader.js_fill = NULL;
7558 reader.js_used = 0;
7559 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007560 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007561}
7562
7563/*
7564 * "js_encode()" function
7565 */
7566 static void
7567f_js_encode(typval_T *argvars, typval_T *rettv)
7568{
7569 rettv->v_type = VAR_STRING;
7570 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7571}
7572
7573/*
7574 * "json_decode()" function
7575 */
7576 static void
7577f_json_decode(typval_T *argvars, typval_T *rettv)
7578{
7579 js_read_T reader;
7580
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007581 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007582 reader.js_fill = NULL;
7583 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007584 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007585}
7586
7587/*
7588 * "json_encode()" function
7589 */
7590 static void
7591f_json_encode(typval_T *argvars, typval_T *rettv)
7592{
7593 rettv->v_type = VAR_STRING;
7594 rettv->vval.v_string = json_encode(&argvars[0], 0);
7595}
7596
7597/*
7598 * "keys()" function
7599 */
7600 static void
7601f_keys(typval_T *argvars, typval_T *rettv)
7602{
7603 dict_list(argvars, rettv, 0);
7604}
7605
7606/*
7607 * "last_buffer_nr()" function.
7608 */
7609 static void
7610f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7611{
7612 int n = 0;
7613 buf_T *buf;
7614
Bram Moolenaar29323592016-07-24 22:04:11 +02007615 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007616 if (n < buf->b_fnum)
7617 n = buf->b_fnum;
7618
7619 rettv->vval.v_number = n;
7620}
7621
7622/*
7623 * "len()" function
7624 */
7625 static void
7626f_len(typval_T *argvars, typval_T *rettv)
7627{
7628 switch (argvars[0].v_type)
7629 {
7630 case VAR_STRING:
7631 case VAR_NUMBER:
7632 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007633 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007634 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007635 case VAR_BLOB:
7636 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
7637 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007638 case VAR_LIST:
7639 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7640 break;
7641 case VAR_DICT:
7642 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7643 break;
7644 case VAR_UNKNOWN:
7645 case VAR_SPECIAL:
7646 case VAR_FLOAT:
7647 case VAR_FUNC:
7648 case VAR_PARTIAL:
7649 case VAR_JOB:
7650 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007651 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007652 break;
7653 }
7654}
7655
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007656 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007657libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007658{
7659#ifdef FEAT_LIBCALL
7660 char_u *string_in;
7661 char_u **string_result;
7662 int nr_result;
7663#endif
7664
7665 rettv->v_type = type;
7666 if (type != VAR_NUMBER)
7667 rettv->vval.v_string = NULL;
7668
7669 if (check_restricted() || check_secure())
7670 return;
7671
7672#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007673 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007674 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7675 {
7676 string_in = NULL;
7677 if (argvars[2].v_type == VAR_STRING)
7678 string_in = argvars[2].vval.v_string;
7679 if (type == VAR_NUMBER)
7680 string_result = NULL;
7681 else
7682 string_result = &rettv->vval.v_string;
7683 if (mch_libcall(argvars[0].vval.v_string,
7684 argvars[1].vval.v_string,
7685 string_in,
7686 argvars[2].vval.v_number,
7687 string_result,
7688 &nr_result) == OK
7689 && type == VAR_NUMBER)
7690 rettv->vval.v_number = nr_result;
7691 }
7692#endif
7693}
7694
7695/*
7696 * "libcall()" function
7697 */
7698 static void
7699f_libcall(typval_T *argvars, typval_T *rettv)
7700{
7701 libcall_common(argvars, rettv, VAR_STRING);
7702}
7703
7704/*
7705 * "libcallnr()" function
7706 */
7707 static void
7708f_libcallnr(typval_T *argvars, typval_T *rettv)
7709{
7710 libcall_common(argvars, rettv, VAR_NUMBER);
7711}
7712
7713/*
7714 * "line(string)" function
7715 */
7716 static void
7717f_line(typval_T *argvars, typval_T *rettv)
7718{
7719 linenr_T lnum = 0;
7720 pos_T *fp;
7721 int fnum;
7722
7723 fp = var2fpos(&argvars[0], TRUE, &fnum);
7724 if (fp != NULL)
7725 lnum = fp->lnum;
7726 rettv->vval.v_number = lnum;
7727}
7728
7729/*
7730 * "line2byte(lnum)" function
7731 */
7732 static void
7733f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7734{
7735#ifndef FEAT_BYTEOFF
7736 rettv->vval.v_number = -1;
7737#else
7738 linenr_T lnum;
7739
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007740 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007741 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7742 rettv->vval.v_number = -1;
7743 else
7744 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7745 if (rettv->vval.v_number >= 0)
7746 ++rettv->vval.v_number;
7747#endif
7748}
7749
7750/*
7751 * "lispindent(lnum)" function
7752 */
7753 static void
7754f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7755{
7756#ifdef FEAT_LISP
7757 pos_T pos;
7758 linenr_T lnum;
7759
7760 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007761 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007762 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7763 {
7764 curwin->w_cursor.lnum = lnum;
7765 rettv->vval.v_number = get_lisp_indent();
7766 curwin->w_cursor = pos;
7767 }
7768 else
7769#endif
7770 rettv->vval.v_number = -1;
7771}
7772
7773/*
7774 * "localtime()" function
7775 */
7776 static void
7777f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7778{
7779 rettv->vval.v_number = (varnumber_T)time(NULL);
7780}
7781
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007782 static void
7783get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7784{
7785 char_u *keys;
7786 char_u *which;
7787 char_u buf[NUMBUFLEN];
7788 char_u *keys_buf = NULL;
7789 char_u *rhs;
7790 int mode;
7791 int abbr = FALSE;
7792 int get_dict = FALSE;
7793 mapblock_T *mp;
7794 int buffer_local;
7795
7796 /* return empty string for failure */
7797 rettv->v_type = VAR_STRING;
7798 rettv->vval.v_string = NULL;
7799
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007800 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007801 if (*keys == NUL)
7802 return;
7803
7804 if (argvars[1].v_type != VAR_UNKNOWN)
7805 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007806 which = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007807 if (argvars[2].v_type != VAR_UNKNOWN)
7808 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007809 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007810 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007811 get_dict = (int)tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007812 }
7813 }
7814 else
7815 which = (char_u *)"";
7816 if (which == NULL)
7817 return;
7818
7819 mode = get_map_mode(&which, 0);
7820
7821 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7822 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7823 vim_free(keys_buf);
7824
7825 if (!get_dict)
7826 {
7827 /* Return a string. */
7828 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02007829 {
7830 if (*rhs == NUL)
7831 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
7832 else
7833 rettv->vval.v_string = str2special_save(rhs, FALSE);
7834 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007835
7836 }
7837 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7838 {
7839 /* Return a dictionary. */
7840 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7841 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7842 dict_T *dict = rettv->vval.v_dict;
7843
Bram Moolenaare0be1672018-07-08 16:50:37 +02007844 dict_add_string(dict, "lhs", lhs);
7845 dict_add_string(dict, "rhs", mp->m_orig_str);
7846 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
7847 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
7848 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007849 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
7850 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02007851 dict_add_number(dict, "buffer", (long)buffer_local);
7852 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
7853 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007854
7855 vim_free(lhs);
7856 vim_free(mapmode);
7857 }
7858}
7859
7860#ifdef FEAT_FLOAT
7861/*
7862 * "log()" function
7863 */
7864 static void
7865f_log(typval_T *argvars, typval_T *rettv)
7866{
7867 float_T f = 0.0;
7868
7869 rettv->v_type = VAR_FLOAT;
7870 if (get_float_arg(argvars, &f) == OK)
7871 rettv->vval.v_float = log(f);
7872 else
7873 rettv->vval.v_float = 0.0;
7874}
7875
7876/*
7877 * "log10()" function
7878 */
7879 static void
7880f_log10(typval_T *argvars, typval_T *rettv)
7881{
7882 float_T f = 0.0;
7883
7884 rettv->v_type = VAR_FLOAT;
7885 if (get_float_arg(argvars, &f) == OK)
7886 rettv->vval.v_float = log10(f);
7887 else
7888 rettv->vval.v_float = 0.0;
7889}
7890#endif
7891
7892#ifdef FEAT_LUA
7893/*
7894 * "luaeval()" function
7895 */
7896 static void
7897f_luaeval(typval_T *argvars, typval_T *rettv)
7898{
7899 char_u *str;
7900 char_u buf[NUMBUFLEN];
7901
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007902 if (check_restricted() || check_secure())
7903 return;
7904
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007905 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007906 do_luaeval(str, argvars + 1, rettv);
7907}
7908#endif
7909
7910/*
7911 * "map()" function
7912 */
7913 static void
7914f_map(typval_T *argvars, typval_T *rettv)
7915{
7916 filter_map(argvars, rettv, TRUE);
7917}
7918
7919/*
7920 * "maparg()" function
7921 */
7922 static void
7923f_maparg(typval_T *argvars, typval_T *rettv)
7924{
7925 get_maparg(argvars, rettv, TRUE);
7926}
7927
7928/*
7929 * "mapcheck()" function
7930 */
7931 static void
7932f_mapcheck(typval_T *argvars, typval_T *rettv)
7933{
7934 get_maparg(argvars, rettv, FALSE);
7935}
7936
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007937typedef enum
7938{
7939 MATCH_END, /* matchend() */
7940 MATCH_MATCH, /* match() */
7941 MATCH_STR, /* matchstr() */
7942 MATCH_LIST, /* matchlist() */
7943 MATCH_POS /* matchstrpos() */
7944} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007945
7946 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007947find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007948{
7949 char_u *str = NULL;
7950 long len = 0;
7951 char_u *expr = NULL;
7952 char_u *pat;
7953 regmatch_T regmatch;
7954 char_u patbuf[NUMBUFLEN];
7955 char_u strbuf[NUMBUFLEN];
7956 char_u *save_cpo;
7957 long start = 0;
7958 long nth = 1;
7959 colnr_T startcol = 0;
7960 int match = 0;
7961 list_T *l = NULL;
7962 listitem_T *li = NULL;
7963 long idx = 0;
7964 char_u *tofree = NULL;
7965
7966 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7967 save_cpo = p_cpo;
7968 p_cpo = (char_u *)"";
7969
7970 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007971 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007972 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007973 /* type MATCH_LIST: return empty list when there are no matches.
7974 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007975 if (rettv_list_alloc(rettv) == FAIL)
7976 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007977 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007978 && (list_append_string(rettv->vval.v_list,
7979 (char_u *)"", 0) == FAIL
7980 || list_append_number(rettv->vval.v_list,
7981 (varnumber_T)-1) == FAIL
7982 || list_append_number(rettv->vval.v_list,
7983 (varnumber_T)-1) == FAIL
7984 || list_append_number(rettv->vval.v_list,
7985 (varnumber_T)-1) == FAIL))
7986 {
7987 list_free(rettv->vval.v_list);
7988 rettv->vval.v_list = NULL;
7989 goto theend;
7990 }
7991 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007992 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007993 {
7994 rettv->v_type = VAR_STRING;
7995 rettv->vval.v_string = NULL;
7996 }
7997
7998 if (argvars[0].v_type == VAR_LIST)
7999 {
8000 if ((l = argvars[0].vval.v_list) == NULL)
8001 goto theend;
8002 li = l->lv_first;
8003 }
8004 else
8005 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008006 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008007 len = (long)STRLEN(str);
8008 }
8009
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008010 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008011 if (pat == NULL)
8012 goto theend;
8013
8014 if (argvars[2].v_type != VAR_UNKNOWN)
8015 {
8016 int error = FALSE;
8017
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008018 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008019 if (error)
8020 goto theend;
8021 if (l != NULL)
8022 {
8023 li = list_find(l, start);
8024 if (li == NULL)
8025 goto theend;
8026 idx = l->lv_idx; /* use the cached index */
8027 }
8028 else
8029 {
8030 if (start < 0)
8031 start = 0;
8032 if (start > len)
8033 goto theend;
8034 /* When "count" argument is there ignore matches before "start",
8035 * otherwise skip part of the string. Differs when pattern is "^"
8036 * or "\<". */
8037 if (argvars[3].v_type != VAR_UNKNOWN)
8038 startcol = start;
8039 else
8040 {
8041 str += start;
8042 len -= start;
8043 }
8044 }
8045
8046 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008047 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008048 if (error)
8049 goto theend;
8050 }
8051
8052 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8053 if (regmatch.regprog != NULL)
8054 {
8055 regmatch.rm_ic = p_ic;
8056
8057 for (;;)
8058 {
8059 if (l != NULL)
8060 {
8061 if (li == NULL)
8062 {
8063 match = FALSE;
8064 break;
8065 }
8066 vim_free(tofree);
8067 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
8068 if (str == NULL)
8069 break;
8070 }
8071
8072 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
8073
8074 if (match && --nth <= 0)
8075 break;
8076 if (l == NULL && !match)
8077 break;
8078
8079 /* Advance to just after the match. */
8080 if (l != NULL)
8081 {
8082 li = li->li_next;
8083 ++idx;
8084 }
8085 else
8086 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008087 startcol = (colnr_T)(regmatch.startp[0]
8088 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008089 if (startcol > (colnr_T)len
8090 || str + startcol <= regmatch.startp[0])
8091 {
8092 match = FALSE;
8093 break;
8094 }
8095 }
8096 }
8097
8098 if (match)
8099 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008100 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008101 {
8102 listitem_T *li1 = rettv->vval.v_list->lv_first;
8103 listitem_T *li2 = li1->li_next;
8104 listitem_T *li3 = li2->li_next;
8105 listitem_T *li4 = li3->li_next;
8106
8107 vim_free(li1->li_tv.vval.v_string);
8108 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
8109 (int)(regmatch.endp[0] - regmatch.startp[0]));
8110 li3->li_tv.vval.v_number =
8111 (varnumber_T)(regmatch.startp[0] - expr);
8112 li4->li_tv.vval.v_number =
8113 (varnumber_T)(regmatch.endp[0] - expr);
8114 if (l != NULL)
8115 li2->li_tv.vval.v_number = (varnumber_T)idx;
8116 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008117 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008118 {
8119 int i;
8120
8121 /* return list with matched string and submatches */
8122 for (i = 0; i < NSUBEXP; ++i)
8123 {
8124 if (regmatch.endp[i] == NULL)
8125 {
8126 if (list_append_string(rettv->vval.v_list,
8127 (char_u *)"", 0) == FAIL)
8128 break;
8129 }
8130 else if (list_append_string(rettv->vval.v_list,
8131 regmatch.startp[i],
8132 (int)(regmatch.endp[i] - regmatch.startp[i]))
8133 == FAIL)
8134 break;
8135 }
8136 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008137 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008138 {
8139 /* return matched string */
8140 if (l != NULL)
8141 copy_tv(&li->li_tv, rettv);
8142 else
8143 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
8144 (int)(regmatch.endp[0] - regmatch.startp[0]));
8145 }
8146 else if (l != NULL)
8147 rettv->vval.v_number = idx;
8148 else
8149 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008150 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008151 rettv->vval.v_number =
8152 (varnumber_T)(regmatch.startp[0] - str);
8153 else
8154 rettv->vval.v_number =
8155 (varnumber_T)(regmatch.endp[0] - str);
8156 rettv->vval.v_number += (varnumber_T)(str - expr);
8157 }
8158 }
8159 vim_regfree(regmatch.regprog);
8160 }
8161
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008162theend:
8163 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008164 /* matchstrpos() without a list: drop the second item. */
8165 listitem_remove(rettv->vval.v_list,
8166 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008167 vim_free(tofree);
8168 p_cpo = save_cpo;
8169}
8170
8171/*
8172 * "match()" function
8173 */
8174 static void
8175f_match(typval_T *argvars, typval_T *rettv)
8176{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008177 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008178}
8179
Bram Moolenaar95e51472018-07-28 16:55:56 +02008180#ifdef FEAT_SEARCH_EXTRA
8181 static int
8182matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
8183{
8184 dictitem_T *di;
8185
8186 if (tv->v_type != VAR_DICT)
8187 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008188 emsg(_(e_dictreq));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008189 return FAIL;
8190 }
8191
8192 if (dict_find(tv->vval.v_dict, (char_u *)"conceal", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01008193 *conceal_char = dict_get_string(tv->vval.v_dict,
Bram Moolenaar95e51472018-07-28 16:55:56 +02008194 (char_u *)"conceal", FALSE);
8195
8196 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
8197 {
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02008198 *win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008199 if (*win == NULL)
8200 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008201 emsg(_("E957: Invalid window number"));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008202 return FAIL;
8203 }
8204 }
8205
8206 return OK;
8207}
8208#endif
8209
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008210/*
8211 * "matchadd()" function
8212 */
8213 static void
8214f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8215{
8216#ifdef FEAT_SEARCH_EXTRA
8217 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008218 char_u *grp = tv_get_string_buf_chk(&argvars[0], buf); /* group */
8219 char_u *pat = tv_get_string_buf_chk(&argvars[1], buf); /* pattern */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008220 int prio = 10; /* default priority */
8221 int id = -1;
8222 int error = FALSE;
8223 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008224 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008225
8226 rettv->vval.v_number = -1;
8227
8228 if (grp == NULL || pat == NULL)
8229 return;
8230 if (argvars[2].v_type != VAR_UNKNOWN)
8231 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008232 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008233 if (argvars[3].v_type != VAR_UNKNOWN)
8234 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008235 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008236 if (argvars[4].v_type != VAR_UNKNOWN
8237 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8238 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008239 }
8240 }
8241 if (error == TRUE)
8242 return;
8243 if (id >= 1 && id <= 3)
8244 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008245 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008246 return;
8247 }
8248
Bram Moolenaar95e51472018-07-28 16:55:56 +02008249 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008250 conceal_char);
8251#endif
8252}
8253
8254/*
8255 * "matchaddpos()" function
8256 */
8257 static void
8258f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8259{
8260#ifdef FEAT_SEARCH_EXTRA
8261 char_u buf[NUMBUFLEN];
8262 char_u *group;
8263 int prio = 10;
8264 int id = -1;
8265 int error = FALSE;
8266 list_T *l;
8267 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008268 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008269
8270 rettv->vval.v_number = -1;
8271
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008272 group = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008273 if (group == NULL)
8274 return;
8275
8276 if (argvars[1].v_type != VAR_LIST)
8277 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008278 semsg(_(e_listarg), "matchaddpos()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008279 return;
8280 }
8281 l = argvars[1].vval.v_list;
8282 if (l == NULL)
8283 return;
8284
8285 if (argvars[2].v_type != VAR_UNKNOWN)
8286 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008287 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008288 if (argvars[3].v_type != VAR_UNKNOWN)
8289 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008290 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008291
8292 if (argvars[4].v_type != VAR_UNKNOWN
8293 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8294 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008295 }
8296 }
8297 if (error == TRUE)
8298 return;
8299
8300 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8301 if (id == 1 || id == 2)
8302 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008303 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008304 return;
8305 }
8306
Bram Moolenaar95e51472018-07-28 16:55:56 +02008307 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008308 conceal_char);
8309#endif
8310}
8311
8312/*
8313 * "matcharg()" function
8314 */
8315 static void
8316f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8317{
8318 if (rettv_list_alloc(rettv) == OK)
8319 {
8320#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008321 int id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008322 matchitem_T *m;
8323
8324 if (id >= 1 && id <= 3)
8325 {
8326 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8327 {
8328 list_append_string(rettv->vval.v_list,
8329 syn_id2name(m->hlg_id), -1);
8330 list_append_string(rettv->vval.v_list, m->pattern, -1);
8331 }
8332 else
8333 {
8334 list_append_string(rettv->vval.v_list, NULL, -1);
8335 list_append_string(rettv->vval.v_list, NULL, -1);
8336 }
8337 }
8338#endif
8339 }
8340}
8341
8342/*
8343 * "matchdelete()" function
8344 */
8345 static void
8346f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8347{
8348#ifdef FEAT_SEARCH_EXTRA
8349 rettv->vval.v_number = match_delete(curwin,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008350 (int)tv_get_number(&argvars[0]), TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008351#endif
8352}
8353
8354/*
8355 * "matchend()" function
8356 */
8357 static void
8358f_matchend(typval_T *argvars, typval_T *rettv)
8359{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008360 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008361}
8362
8363/*
8364 * "matchlist()" function
8365 */
8366 static void
8367f_matchlist(typval_T *argvars, typval_T *rettv)
8368{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008369 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008370}
8371
8372/*
8373 * "matchstr()" function
8374 */
8375 static void
8376f_matchstr(typval_T *argvars, typval_T *rettv)
8377{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008378 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008379}
8380
8381/*
8382 * "matchstrpos()" function
8383 */
8384 static void
8385f_matchstrpos(typval_T *argvars, typval_T *rettv)
8386{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008387 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008388}
8389
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008390 static void
8391max_min(typval_T *argvars, typval_T *rettv, int domax)
8392{
8393 varnumber_T n = 0;
8394 varnumber_T i;
8395 int error = FALSE;
8396
8397 if (argvars[0].v_type == VAR_LIST)
8398 {
8399 list_T *l;
8400 listitem_T *li;
8401
8402 l = argvars[0].vval.v_list;
8403 if (l != NULL)
8404 {
8405 li = l->lv_first;
8406 if (li != NULL)
8407 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008408 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008409 for (;;)
8410 {
8411 li = li->li_next;
8412 if (li == NULL)
8413 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008414 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008415 if (domax ? i > n : i < n)
8416 n = i;
8417 }
8418 }
8419 }
8420 }
8421 else if (argvars[0].v_type == VAR_DICT)
8422 {
8423 dict_T *d;
8424 int first = TRUE;
8425 hashitem_T *hi;
8426 int todo;
8427
8428 d = argvars[0].vval.v_dict;
8429 if (d != NULL)
8430 {
8431 todo = (int)d->dv_hashtab.ht_used;
8432 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8433 {
8434 if (!HASHITEM_EMPTY(hi))
8435 {
8436 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008437 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008438 if (first)
8439 {
8440 n = i;
8441 first = FALSE;
8442 }
8443 else if (domax ? i > n : i < n)
8444 n = i;
8445 }
8446 }
8447 }
8448 }
8449 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008450 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008451 rettv->vval.v_number = error ? 0 : n;
8452}
8453
8454/*
8455 * "max()" function
8456 */
8457 static void
8458f_max(typval_T *argvars, typval_T *rettv)
8459{
8460 max_min(argvars, rettv, TRUE);
8461}
8462
8463/*
8464 * "min()" function
8465 */
8466 static void
8467f_min(typval_T *argvars, typval_T *rettv)
8468{
8469 max_min(argvars, rettv, FALSE);
8470}
8471
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008472/*
8473 * Create the directory in which "dir" is located, and higher levels when
8474 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008475 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008476 */
8477 static int
8478mkdir_recurse(char_u *dir, int prot)
8479{
8480 char_u *p;
8481 char_u *updir;
8482 int r = FAIL;
8483
8484 /* Get end of directory name in "dir".
8485 * We're done when it's "/" or "c:/". */
8486 p = gettail_sep(dir);
8487 if (p <= get_past_head(dir))
8488 return OK;
8489
8490 /* If the directory exists we're done. Otherwise: create it.*/
8491 updir = vim_strnsave(dir, (int)(p - dir));
8492 if (updir == NULL)
8493 return FAIL;
8494 if (mch_isdir(updir))
8495 r = OK;
8496 else if (mkdir_recurse(updir, prot) == OK)
8497 r = vim_mkdir_emsg(updir, prot);
8498 vim_free(updir);
8499 return r;
8500}
8501
8502#ifdef vim_mkdir
8503/*
8504 * "mkdir()" function
8505 */
8506 static void
8507f_mkdir(typval_T *argvars, typval_T *rettv)
8508{
8509 char_u *dir;
8510 char_u buf[NUMBUFLEN];
8511 int prot = 0755;
8512
8513 rettv->vval.v_number = FAIL;
8514 if (check_restricted() || check_secure())
8515 return;
8516
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008517 dir = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008518 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008519 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008520
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008521 if (*gettail(dir) == NUL)
8522 /* remove trailing slashes */
8523 *gettail_sep(dir) = NUL;
8524
8525 if (argvars[1].v_type != VAR_UNKNOWN)
8526 {
8527 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008528 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008529 prot = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008530 if (prot == -1)
8531 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008532 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008533 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008534 {
8535 if (mch_isdir(dir))
8536 {
8537 /* With the "p" flag it's OK if the dir already exists. */
8538 rettv->vval.v_number = OK;
8539 return;
8540 }
8541 mkdir_recurse(dir, prot);
8542 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008543 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008544 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008545}
8546#endif
8547
8548/*
8549 * "mode()" function
8550 */
8551 static void
8552f_mode(typval_T *argvars, typval_T *rettv)
8553{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008554 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008555
Bram Moolenaar612cc382018-07-29 15:34:26 +02008556 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008557
8558 if (time_for_testing == 93784)
8559 {
8560 /* Testing the two-character code. */
8561 buf[0] = 'x';
8562 buf[1] = '!';
8563 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008564#ifdef FEAT_TERMINAL
8565 else if (term_use_loop())
8566 buf[0] = 't';
8567#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008568 else if (VIsual_active)
8569 {
8570 if (VIsual_select)
8571 buf[0] = VIsual_mode + 's' - 'v';
8572 else
8573 buf[0] = VIsual_mode;
8574 }
8575 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8576 || State == CONFIRM)
8577 {
8578 buf[0] = 'r';
8579 if (State == ASKMORE)
8580 buf[1] = 'm';
8581 else if (State == CONFIRM)
8582 buf[1] = '?';
8583 }
8584 else if (State == EXTERNCMD)
8585 buf[0] = '!';
8586 else if (State & INSERT)
8587 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008588 if (State & VREPLACE_FLAG)
8589 {
8590 buf[0] = 'R';
8591 buf[1] = 'v';
8592 }
8593 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008594 {
8595 if (State & REPLACE_FLAG)
8596 buf[0] = 'R';
8597 else
8598 buf[0] = 'i';
8599#ifdef FEAT_INS_EXPAND
8600 if (ins_compl_active())
8601 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008602 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008603 buf[1] = 'x';
8604#endif
8605 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008606 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008607 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008608 {
8609 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008610 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008611 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008612 else if (exmode_active == EXMODE_NORMAL)
8613 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008614 }
8615 else
8616 {
8617 buf[0] = 'n';
8618 if (finish_op)
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008619 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008620 buf[1] = 'o';
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008621 // to be able to detect force-linewise/blockwise/characterwise operations
8622 buf[2] = motion_force;
8623 }
Bram Moolenaar612cc382018-07-29 15:34:26 +02008624 else if (restart_edit == 'I' || restart_edit == 'R'
8625 || restart_edit == 'V')
8626 {
8627 buf[1] = 'i';
8628 buf[2] = restart_edit;
8629 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008630 }
8631
8632 /* Clear out the minor mode when the argument is not a non-zero number or
8633 * non-empty string. */
8634 if (!non_zero_arg(&argvars[0]))
8635 buf[1] = NUL;
8636
8637 rettv->vval.v_string = vim_strsave(buf);
8638 rettv->v_type = VAR_STRING;
8639}
8640
8641#if defined(FEAT_MZSCHEME) || defined(PROTO)
8642/*
8643 * "mzeval()" function
8644 */
8645 static void
8646f_mzeval(typval_T *argvars, typval_T *rettv)
8647{
8648 char_u *str;
8649 char_u buf[NUMBUFLEN];
8650
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008651 if (check_restricted() || check_secure())
8652 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008653 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008654 do_mzeval(str, rettv);
8655}
8656
8657 void
8658mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8659{
8660 typval_T argvars[3];
8661
8662 argvars[0].v_type = VAR_STRING;
8663 argvars[0].vval.v_string = name;
8664 copy_tv(args, &argvars[1]);
8665 argvars[2].v_type = VAR_UNKNOWN;
8666 f_call(argvars, rettv);
8667 clear_tv(&argvars[1]);
8668}
8669#endif
8670
8671/*
8672 * "nextnonblank()" function
8673 */
8674 static void
8675f_nextnonblank(typval_T *argvars, typval_T *rettv)
8676{
8677 linenr_T lnum;
8678
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008679 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008680 {
8681 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8682 {
8683 lnum = 0;
8684 break;
8685 }
8686 if (*skipwhite(ml_get(lnum)) != NUL)
8687 break;
8688 }
8689 rettv->vval.v_number = lnum;
8690}
8691
8692/*
8693 * "nr2char()" function
8694 */
8695 static void
8696f_nr2char(typval_T *argvars, typval_T *rettv)
8697{
8698 char_u buf[NUMBUFLEN];
8699
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008700 if (has_mbyte)
8701 {
8702 int utf8 = 0;
8703
8704 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008705 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008706 if (utf8)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008707 buf[(*utf_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008708 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008709 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008710 }
8711 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008712 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008713 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008714 buf[1] = NUL;
8715 }
8716 rettv->v_type = VAR_STRING;
8717 rettv->vval.v_string = vim_strsave(buf);
8718}
8719
8720/*
8721 * "or(expr, expr)" function
8722 */
8723 static void
8724f_or(typval_T *argvars, typval_T *rettv)
8725{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008726 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
8727 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008728}
8729
8730/*
8731 * "pathshorten()" function
8732 */
8733 static void
8734f_pathshorten(typval_T *argvars, typval_T *rettv)
8735{
8736 char_u *p;
8737
8738 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008739 p = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008740 if (p == NULL)
8741 rettv->vval.v_string = NULL;
8742 else
8743 {
8744 p = vim_strsave(p);
8745 rettv->vval.v_string = p;
8746 if (p != NULL)
8747 shorten_dir(p);
8748 }
8749}
8750
8751#ifdef FEAT_PERL
8752/*
8753 * "perleval()" function
8754 */
8755 static void
8756f_perleval(typval_T *argvars, typval_T *rettv)
8757{
8758 char_u *str;
8759 char_u buf[NUMBUFLEN];
8760
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008761 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008762 do_perleval(str, rettv);
8763}
8764#endif
8765
8766#ifdef FEAT_FLOAT
8767/*
8768 * "pow()" function
8769 */
8770 static void
8771f_pow(typval_T *argvars, typval_T *rettv)
8772{
8773 float_T fx = 0.0, fy = 0.0;
8774
8775 rettv->v_type = VAR_FLOAT;
8776 if (get_float_arg(argvars, &fx) == OK
8777 && get_float_arg(&argvars[1], &fy) == OK)
8778 rettv->vval.v_float = pow(fx, fy);
8779 else
8780 rettv->vval.v_float = 0.0;
8781}
8782#endif
8783
8784/*
8785 * "prevnonblank()" function
8786 */
8787 static void
8788f_prevnonblank(typval_T *argvars, typval_T *rettv)
8789{
8790 linenr_T lnum;
8791
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008792 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008793 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8794 lnum = 0;
8795 else
8796 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8797 --lnum;
8798 rettv->vval.v_number = lnum;
8799}
8800
8801/* This dummy va_list is here because:
8802 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8803 * - locally in the function results in a "used before set" warning
8804 * - using va_start() to initialize it gives "function with fixed args" error */
8805static va_list ap;
8806
8807/*
8808 * "printf()" function
8809 */
8810 static void
8811f_printf(typval_T *argvars, typval_T *rettv)
8812{
8813 char_u buf[NUMBUFLEN];
8814 int len;
8815 char_u *s;
8816 int saved_did_emsg = did_emsg;
8817 char *fmt;
8818
8819 rettv->v_type = VAR_STRING;
8820 rettv->vval.v_string = NULL;
8821
8822 /* Get the required length, allocate the buffer and do it for real. */
8823 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008824 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008825 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008826 if (!did_emsg)
8827 {
8828 s = alloc(len + 1);
8829 if (s != NULL)
8830 {
8831 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008832 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8833 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008834 }
8835 }
8836 did_emsg |= saved_did_emsg;
8837}
8838
Bram Moolenaarf2732452018-06-03 14:47:35 +02008839#ifdef FEAT_JOB_CHANNEL
8840/*
8841 * "prompt_setcallback({buffer}, {callback})" function
8842 */
8843 static void
8844f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
8845{
8846 buf_T *buf;
8847 char_u *callback;
8848 partial_T *partial;
8849
8850 if (check_secure())
8851 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01008852 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008853 if (buf == NULL)
8854 return;
8855
8856 callback = get_callback(&argvars[1], &partial);
8857 if (callback == NULL)
8858 return;
8859
8860 free_callback(buf->b_prompt_callback, buf->b_prompt_partial);
8861 if (partial == NULL)
8862 buf->b_prompt_callback = vim_strsave(callback);
8863 else
8864 /* pointer into the partial */
8865 buf->b_prompt_callback = callback;
8866 buf->b_prompt_partial = partial;
8867}
8868
8869/*
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02008870 * "prompt_setinterrupt({buffer}, {callback})" function
8871 */
8872 static void
8873f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
8874{
8875 buf_T *buf;
8876 char_u *callback;
8877 partial_T *partial;
8878
8879 if (check_secure())
8880 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01008881 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02008882 if (buf == NULL)
8883 return;
8884
8885 callback = get_callback(&argvars[1], &partial);
8886 if (callback == NULL)
8887 return;
8888
8889 free_callback(buf->b_prompt_interrupt, buf->b_prompt_int_partial);
8890 if (partial == NULL)
8891 buf->b_prompt_interrupt = vim_strsave(callback);
8892 else
8893 /* pointer into the partial */
8894 buf->b_prompt_interrupt = callback;
8895 buf->b_prompt_int_partial = partial;
8896}
8897
8898/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02008899 * "prompt_setprompt({buffer}, {text})" function
8900 */
8901 static void
8902f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
8903{
8904 buf_T *buf;
8905 char_u *text;
8906
8907 if (check_secure())
8908 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01008909 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008910 if (buf == NULL)
8911 return;
8912
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008913 text = tv_get_string(&argvars[1]);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008914 vim_free(buf->b_prompt_text);
8915 buf->b_prompt_text = vim_strsave(text);
8916}
8917#endif
8918
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008919/*
8920 * "pumvisible()" function
8921 */
8922 static void
8923f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8924{
8925#ifdef FEAT_INS_EXPAND
8926 if (pum_visible())
8927 rettv->vval.v_number = 1;
8928#endif
8929}
8930
8931#ifdef FEAT_PYTHON3
8932/*
8933 * "py3eval()" function
8934 */
8935 static void
8936f_py3eval(typval_T *argvars, typval_T *rettv)
8937{
8938 char_u *str;
8939 char_u buf[NUMBUFLEN];
8940
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008941 if (check_restricted() || check_secure())
8942 return;
8943
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008944 if (p_pyx == 0)
8945 p_pyx = 3;
8946
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008947 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008948 do_py3eval(str, rettv);
8949}
8950#endif
8951
8952#ifdef FEAT_PYTHON
8953/*
8954 * "pyeval()" function
8955 */
8956 static void
8957f_pyeval(typval_T *argvars, typval_T *rettv)
8958{
8959 char_u *str;
8960 char_u buf[NUMBUFLEN];
8961
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008962 if (check_restricted() || check_secure())
8963 return;
8964
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008965 if (p_pyx == 0)
8966 p_pyx = 2;
8967
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008968 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008969 do_pyeval(str, rettv);
8970}
8971#endif
8972
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008973#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8974/*
8975 * "pyxeval()" function
8976 */
8977 static void
8978f_pyxeval(typval_T *argvars, typval_T *rettv)
8979{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008980 if (check_restricted() || check_secure())
8981 return;
8982
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008983# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8984 init_pyxversion();
8985 if (p_pyx == 2)
8986 f_pyeval(argvars, rettv);
8987 else
8988 f_py3eval(argvars, rettv);
8989# elif defined(FEAT_PYTHON)
8990 f_pyeval(argvars, rettv);
8991# elif defined(FEAT_PYTHON3)
8992 f_py3eval(argvars, rettv);
8993# endif
8994}
8995#endif
8996
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008997/*
8998 * "range()" function
8999 */
9000 static void
9001f_range(typval_T *argvars, typval_T *rettv)
9002{
9003 varnumber_T start;
9004 varnumber_T end;
9005 varnumber_T stride = 1;
9006 varnumber_T i;
9007 int error = FALSE;
9008
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009009 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009010 if (argvars[1].v_type == VAR_UNKNOWN)
9011 {
9012 end = start - 1;
9013 start = 0;
9014 }
9015 else
9016 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009017 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009018 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009019 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009020 }
9021
9022 if (error)
9023 return; /* type error; errmsg already given */
9024 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009025 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009026 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009027 emsg(_("E727: Start past end"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009028 else
9029 {
9030 if (rettv_list_alloc(rettv) == OK)
9031 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
9032 if (list_append_number(rettv->vval.v_list,
9033 (varnumber_T)i) == FAIL)
9034 break;
9035 }
9036}
9037
9038/*
9039 * "readfile()" function
9040 */
9041 static void
9042f_readfile(typval_T *argvars, typval_T *rettv)
9043{
9044 int binary = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009045 int blob = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009046 int failed = FALSE;
9047 char_u *fname;
9048 FILE *fd;
9049 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
9050 int io_size = sizeof(buf);
9051 int readlen; /* size of last fread() */
9052 char_u *prev = NULL; /* previously read bytes, if any */
9053 long prevlen = 0; /* length of data in prev */
9054 long prevsize = 0; /* size of prev buffer */
9055 long maxline = MAXLNUM;
9056 long cnt = 0;
9057 char_u *p; /* position in buf */
9058 char_u *start; /* start of current line */
9059
9060 if (argvars[1].v_type != VAR_UNKNOWN)
9061 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009062 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009063 binary = TRUE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009064 if (STRCMP(tv_get_string(&argvars[1]), "B") == 0)
9065 blob = TRUE;
9066
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009067 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009068 maxline = (long)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009069 }
9070
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009071 if (blob)
9072 {
9073 if (rettv_blob_alloc(rettv) == FAIL)
9074 return;
9075 }
9076 else
9077 {
9078 if (rettv_list_alloc(rettv) == FAIL)
9079 return;
9080 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009081
9082 /* Always open the file in binary mode, library functions have a mind of
9083 * their own about CR-LF conversion. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009084 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009085 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
9086 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009087 semsg(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009088 return;
9089 }
9090
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009091 if (blob)
9092 {
9093 if (read_blob(fd, rettv->vval.v_blob) == FAIL)
9094 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009095 emsg("cannot read file");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009096 blob_free(rettv->vval.v_blob);
9097 }
9098 fclose(fd);
9099 return;
9100 }
9101
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009102 while (cnt < maxline || maxline < 0)
9103 {
9104 readlen = (int)fread(buf, 1, io_size, fd);
9105
9106 /* This for loop processes what was read, but is also entered at end
9107 * of file so that either:
9108 * - an incomplete line gets written
9109 * - a "binary" file gets an empty line at the end if it ends in a
9110 * newline. */
9111 for (p = buf, start = buf;
9112 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
9113 ++p)
9114 {
9115 if (*p == '\n' || readlen <= 0)
9116 {
9117 listitem_T *li;
9118 char_u *s = NULL;
9119 long_u len = p - start;
9120
9121 /* Finished a line. Remove CRs before NL. */
9122 if (readlen > 0 && !binary)
9123 {
9124 while (len > 0 && start[len - 1] == '\r')
9125 --len;
9126 /* removal may cross back to the "prev" string */
9127 if (len == 0)
9128 while (prevlen > 0 && prev[prevlen - 1] == '\r')
9129 --prevlen;
9130 }
9131 if (prevlen == 0)
9132 s = vim_strnsave(start, (int)len);
9133 else
9134 {
9135 /* Change "prev" buffer to be the right size. This way
9136 * the bytes are only copied once, and very long lines are
9137 * allocated only once. */
9138 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
9139 {
9140 mch_memmove(s + prevlen, start, len);
9141 s[prevlen + len] = NUL;
9142 prev = NULL; /* the list will own the string */
9143 prevlen = prevsize = 0;
9144 }
9145 }
9146 if (s == NULL)
9147 {
9148 do_outofmem_msg((long_u) prevlen + len + 1);
9149 failed = TRUE;
9150 break;
9151 }
9152
9153 if ((li = listitem_alloc()) == NULL)
9154 {
9155 vim_free(s);
9156 failed = TRUE;
9157 break;
9158 }
9159 li->li_tv.v_type = VAR_STRING;
9160 li->li_tv.v_lock = 0;
9161 li->li_tv.vval.v_string = s;
9162 list_append(rettv->vval.v_list, li);
9163
9164 start = p + 1; /* step over newline */
9165 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
9166 break;
9167 }
9168 else if (*p == NUL)
9169 *p = '\n';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009170 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
9171 * when finding the BF and check the previous two bytes. */
9172 else if (*p == 0xbf && enc_utf8 && !binary)
9173 {
9174 /* Find the two bytes before the 0xbf. If p is at buf, or buf
9175 * + 1, these may be in the "prev" string. */
9176 char_u back1 = p >= buf + 1 ? p[-1]
9177 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
9178 char_u back2 = p >= buf + 2 ? p[-2]
9179 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
9180 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
9181
9182 if (back2 == 0xef && back1 == 0xbb)
9183 {
9184 char_u *dest = p - 2;
9185
9186 /* Usually a BOM is at the beginning of a file, and so at
9187 * the beginning of a line; then we can just step over it.
9188 */
9189 if (start == dest)
9190 start = p + 1;
9191 else
9192 {
9193 /* have to shuffle buf to close gap */
9194 int adjust_prevlen = 0;
9195
9196 if (dest < buf)
9197 {
9198 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
9199 dest = buf;
9200 }
9201 if (readlen > p - buf + 1)
9202 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
9203 readlen -= 3 - adjust_prevlen;
9204 prevlen -= adjust_prevlen;
9205 p = dest - 1;
9206 }
9207 }
9208 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009209 } /* for */
9210
9211 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
9212 break;
9213 if (start < p)
9214 {
9215 /* There's part of a line in buf, store it in "prev". */
9216 if (p - start + prevlen >= prevsize)
9217 {
9218 /* need bigger "prev" buffer */
9219 char_u *newprev;
9220
9221 /* A common use case is ordinary text files and "prev" gets a
9222 * fragment of a line, so the first allocation is made
9223 * small, to avoid repeatedly 'allocing' large and
9224 * 'reallocing' small. */
9225 if (prevsize == 0)
9226 prevsize = (long)(p - start);
9227 else
9228 {
9229 long grow50pc = (prevsize * 3) / 2;
9230 long growmin = (long)((p - start) * 2 + prevlen);
9231 prevsize = grow50pc > growmin ? grow50pc : growmin;
9232 }
9233 newprev = prev == NULL ? alloc(prevsize)
9234 : vim_realloc(prev, prevsize);
9235 if (newprev == NULL)
9236 {
9237 do_outofmem_msg((long_u)prevsize);
9238 failed = TRUE;
9239 break;
9240 }
9241 prev = newprev;
9242 }
9243 /* Add the line part to end of "prev". */
9244 mch_memmove(prev + prevlen, start, p - start);
9245 prevlen += (long)(p - start);
9246 }
9247 } /* while */
9248
9249 /*
9250 * For a negative line count use only the lines at the end of the file,
9251 * free the rest.
9252 */
9253 if (!failed && maxline < 0)
9254 while (cnt > -maxline)
9255 {
9256 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
9257 --cnt;
9258 }
9259
9260 if (failed)
9261 {
9262 list_free(rettv->vval.v_list);
9263 /* readfile doc says an empty list is returned on error */
9264 rettv->vval.v_list = list_alloc();
9265 }
9266
9267 vim_free(prev);
9268 fclose(fd);
9269}
9270
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009271 static void
9272return_register(int regname, typval_T *rettv)
9273{
9274 char_u buf[2] = {0, 0};
9275
9276 buf[0] = (char_u)regname;
9277 rettv->v_type = VAR_STRING;
9278 rettv->vval.v_string = vim_strsave(buf);
9279}
9280
9281/*
9282 * "reg_executing()" function
9283 */
9284 static void
9285f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9286{
9287 return_register(reg_executing, rettv);
9288}
9289
9290/*
9291 * "reg_recording()" function
9292 */
9293 static void
9294f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9295{
9296 return_register(reg_recording, rettv);
9297}
9298
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009299#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009300/*
9301 * Convert a List to proftime_T.
9302 * Return FAIL when there is something wrong.
9303 */
9304 static int
9305list2proftime(typval_T *arg, proftime_T *tm)
9306{
9307 long n1, n2;
9308 int error = FALSE;
9309
9310 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9311 || arg->vval.v_list->lv_len != 2)
9312 return FAIL;
9313 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9314 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
9315# ifdef WIN3264
9316 tm->HighPart = n1;
9317 tm->LowPart = n2;
9318# else
9319 tm->tv_sec = n1;
9320 tm->tv_usec = n2;
9321# endif
9322 return error ? FAIL : OK;
9323}
9324#endif /* FEAT_RELTIME */
9325
9326/*
9327 * "reltime()" function
9328 */
9329 static void
9330f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9331{
9332#ifdef FEAT_RELTIME
9333 proftime_T res;
9334 proftime_T start;
9335
9336 if (argvars[0].v_type == VAR_UNKNOWN)
9337 {
9338 /* No arguments: get current time. */
9339 profile_start(&res);
9340 }
9341 else if (argvars[1].v_type == VAR_UNKNOWN)
9342 {
9343 if (list2proftime(&argvars[0], &res) == FAIL)
9344 return;
9345 profile_end(&res);
9346 }
9347 else
9348 {
9349 /* Two arguments: compute the difference. */
9350 if (list2proftime(&argvars[0], &start) == FAIL
9351 || list2proftime(&argvars[1], &res) == FAIL)
9352 return;
9353 profile_sub(&res, &start);
9354 }
9355
9356 if (rettv_list_alloc(rettv) == OK)
9357 {
9358 long n1, n2;
9359
9360# ifdef WIN3264
9361 n1 = res.HighPart;
9362 n2 = res.LowPart;
9363# else
9364 n1 = res.tv_sec;
9365 n2 = res.tv_usec;
9366# endif
9367 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9368 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9369 }
9370#endif
9371}
9372
9373#ifdef FEAT_FLOAT
9374/*
9375 * "reltimefloat()" function
9376 */
9377 static void
9378f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9379{
9380# ifdef FEAT_RELTIME
9381 proftime_T tm;
9382# endif
9383
9384 rettv->v_type = VAR_FLOAT;
9385 rettv->vval.v_float = 0;
9386# ifdef FEAT_RELTIME
9387 if (list2proftime(&argvars[0], &tm) == OK)
9388 rettv->vval.v_float = profile_float(&tm);
9389# endif
9390}
9391#endif
9392
9393/*
9394 * "reltimestr()" function
9395 */
9396 static void
9397f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9398{
9399#ifdef FEAT_RELTIME
9400 proftime_T tm;
9401#endif
9402
9403 rettv->v_type = VAR_STRING;
9404 rettv->vval.v_string = NULL;
9405#ifdef FEAT_RELTIME
9406 if (list2proftime(&argvars[0], &tm) == OK)
9407 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9408#endif
9409}
9410
9411#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009412 static void
9413make_connection(void)
9414{
9415 if (X_DISPLAY == NULL
9416# ifdef FEAT_GUI
9417 && !gui.in_use
9418# endif
9419 )
9420 {
9421 x_force_connect = TRUE;
9422 setup_term_clip();
9423 x_force_connect = FALSE;
9424 }
9425}
9426
9427 static int
9428check_connection(void)
9429{
9430 make_connection();
9431 if (X_DISPLAY == NULL)
9432 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009433 emsg(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009434 return FAIL;
9435 }
9436 return OK;
9437}
9438#endif
9439
9440#ifdef FEAT_CLIENTSERVER
9441 static void
9442remote_common(typval_T *argvars, typval_T *rettv, int expr)
9443{
9444 char_u *server_name;
9445 char_u *keys;
9446 char_u *r = NULL;
9447 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009448 int timeout = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009449# ifdef WIN32
9450 HWND w;
9451# else
9452 Window w;
9453# endif
9454
9455 if (check_restricted() || check_secure())
9456 return;
9457
9458# ifdef FEAT_X11
9459 if (check_connection() == FAIL)
9460 return;
9461# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009462 if (argvars[2].v_type != VAR_UNKNOWN
9463 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009464 timeout = tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009465
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009466 server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009467 if (server_name == NULL)
9468 return; /* type error; errmsg already given */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009469 keys = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009470# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009471 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009472# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009473 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9474 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009475# endif
9476 {
9477 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009478 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009479 emsg((char *)r); // sending worked but evaluation failed
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009480 vim_free(r);
9481 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009482 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009483 semsg(_("E241: Unable to send to %s"), server_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009484 return;
9485 }
9486
9487 rettv->vval.v_string = r;
9488
9489 if (argvars[2].v_type != VAR_UNKNOWN)
9490 {
9491 dictitem_T v;
9492 char_u str[30];
9493 char_u *idvar;
9494
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009495 idvar = tv_get_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009496 if (idvar != NULL && *idvar != NUL)
9497 {
9498 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9499 v.di_tv.v_type = VAR_STRING;
9500 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009501 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009502 vim_free(v.di_tv.vval.v_string);
9503 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009504 }
9505}
9506#endif
9507
9508/*
9509 * "remote_expr()" function
9510 */
9511 static void
9512f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9513{
9514 rettv->v_type = VAR_STRING;
9515 rettv->vval.v_string = NULL;
9516#ifdef FEAT_CLIENTSERVER
9517 remote_common(argvars, rettv, TRUE);
9518#endif
9519}
9520
9521/*
9522 * "remote_foreground()" function
9523 */
9524 static void
9525f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9526{
9527#ifdef FEAT_CLIENTSERVER
9528# ifdef WIN32
9529 /* On Win32 it's done in this application. */
9530 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009531 char_u *server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009532
9533 if (server_name != NULL)
9534 serverForeground(server_name);
9535 }
9536# else
9537 /* Send a foreground() expression to the server. */
9538 argvars[1].v_type = VAR_STRING;
9539 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9540 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009541 rettv->v_type = VAR_STRING;
9542 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009543 remote_common(argvars, rettv, TRUE);
9544 vim_free(argvars[1].vval.v_string);
9545# endif
9546#endif
9547}
9548
9549 static void
9550f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
9551{
9552#ifdef FEAT_CLIENTSERVER
9553 dictitem_T v;
9554 char_u *s = NULL;
9555# ifdef WIN32
9556 long_u n = 0;
9557# endif
9558 char_u *serverid;
9559
9560 if (check_restricted() || check_secure())
9561 {
9562 rettv->vval.v_number = -1;
9563 return;
9564 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009565 serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009566 if (serverid == NULL)
9567 {
9568 rettv->vval.v_number = -1;
9569 return; /* type error; errmsg already given */
9570 }
9571# ifdef WIN32
9572 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
9573 if (n == 0)
9574 rettv->vval.v_number = -1;
9575 else
9576 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009577 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009578 rettv->vval.v_number = (s != NULL);
9579 }
9580# else
9581 if (check_connection() == FAIL)
9582 return;
9583
9584 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
9585 serverStrToWin(serverid), &s);
9586# endif
9587
9588 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
9589 {
9590 char_u *retvar;
9591
9592 v.di_tv.v_type = VAR_STRING;
9593 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009594 retvar = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009595 if (retvar != NULL)
9596 set_var(retvar, &v.di_tv, FALSE);
9597 vim_free(v.di_tv.vval.v_string);
9598 }
9599#else
9600 rettv->vval.v_number = -1;
9601#endif
9602}
9603
9604 static void
9605f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
9606{
9607 char_u *r = NULL;
9608
9609#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009610 char_u *serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009611
9612 if (serverid != NULL && !check_restricted() && !check_secure())
9613 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009614 int timeout = 0;
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009615# ifdef WIN32
9616 /* The server's HWND is encoded in the 'id' parameter */
9617 long_u n = 0;
9618# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009619
9620 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009621 timeout = tv_get_number(&argvars[1]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009622
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009623# ifdef WIN32
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009624 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
9625 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009626 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009627 if (r == NULL)
9628# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009629 if (check_connection() == FAIL
9630 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
9631 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009632# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009633 emsg(_("E277: Unable to read a server reply"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009634 }
9635#endif
9636 rettv->v_type = VAR_STRING;
9637 rettv->vval.v_string = r;
9638}
9639
9640/*
9641 * "remote_send()" function
9642 */
9643 static void
9644f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9645{
9646 rettv->v_type = VAR_STRING;
9647 rettv->vval.v_string = NULL;
9648#ifdef FEAT_CLIENTSERVER
9649 remote_common(argvars, rettv, FALSE);
9650#endif
9651}
9652
9653/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009654 * "remote_startserver()" function
9655 */
9656 static void
9657f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9658{
9659#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009660 char_u *server = tv_get_string_chk(&argvars[0]);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009661
9662 if (server == NULL)
9663 return; /* type error; errmsg already given */
9664 if (serverName != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009665 emsg(_("E941: already started a server"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009666 else
9667 {
9668# ifdef FEAT_X11
9669 if (check_connection() == OK)
9670 serverRegisterName(X_DISPLAY, server);
9671# else
9672 serverSetName(server);
9673# endif
9674 }
9675#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009676 emsg(_("E942: +clientserver feature not available"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009677#endif
9678}
9679
9680/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009681 * "remove()" function
9682 */
9683 static void
9684f_remove(typval_T *argvars, typval_T *rettv)
9685{
9686 list_T *l;
9687 listitem_T *item, *item2;
9688 listitem_T *li;
9689 long idx;
9690 long end;
9691 char_u *key;
9692 dict_T *d;
9693 dictitem_T *di;
9694 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009695 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009696
9697 if (argvars[0].v_type == VAR_DICT)
9698 {
9699 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009700 semsg(_(e_toomanyarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009701 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01009702 && !var_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009703 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009704 key = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009705 if (key != NULL)
9706 {
9707 di = dict_find(d, key, -1);
9708 if (di == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009709 semsg(_(e_dictkey), key);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009710 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
9711 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
9712 {
9713 *rettv = di->di_tv;
9714 init_tv(&di->di_tv);
9715 dictitem_remove(d, di);
9716 }
9717 }
9718 }
9719 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009720 else if (argvars[0].v_type == VAR_BLOB)
9721 {
9722 idx = (long)tv_get_number_chk(&argvars[1], &error);
9723 if (!error)
9724 {
9725 blob_T *b = argvars[0].vval.v_blob;
9726 int len = blob_len(b);
9727 char_u *p;
9728
9729 if (idx < 0)
9730 // count from the end
9731 idx = len + idx;
9732 if (idx < 0 || idx >= len)
9733 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009734 semsg(_(e_blobidx), idx);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009735 return;
9736 }
9737 if (argvars[2].v_type == VAR_UNKNOWN)
9738 {
9739 // Remove one item, return its value.
9740 p = (char_u *)b->bv_ga.ga_data;
9741 rettv->vval.v_number = (varnumber_T) *(p + idx);
9742 mch_memmove(p + idx, p + idx + 1, (size_t)len - idx - 1);
9743 --b->bv_ga.ga_len;
9744 }
9745 else
9746 {
9747 blob_T *blob;
9748
9749 // Remove range of items, return list with values.
9750 end = (long)tv_get_number_chk(&argvars[2], &error);
9751 if (error)
9752 return;
9753 if (end < 0)
9754 // count from the end
9755 end = len + end;
9756 if (end >= len || idx > end)
9757 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009758 semsg(_(e_blobidx), end);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009759 return;
9760 }
9761 blob = blob_alloc();
9762 if (blob == NULL)
9763 return;
9764 blob->bv_ga.ga_len = end - idx + 1;
9765 if (ga_grow(&blob->bv_ga, end - idx + 1) == FAIL)
9766 {
9767 vim_free(blob);
9768 return;
9769 }
9770 p = (char_u *)b->bv_ga.ga_data;
9771 mch_memmove((char_u *)blob->bv_ga.ga_data, p + idx,
9772 (size_t)(end - idx + 1));
9773 ++blob->bv_refcount;
9774 rettv->v_type = VAR_BLOB;
9775 rettv->vval.v_blob = blob;
9776
9777 mch_memmove(p + idx, p + end + 1, (size_t)(len - end));
9778 b->bv_ga.ga_len -= end - idx + 1;
9779 }
9780 }
9781 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009782 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01009783 semsg(_(e_listdictblobarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009784 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01009785 && !var_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009786 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009787 idx = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009788 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009789 ; // type error: do nothing, errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009790 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009791 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009792 else
9793 {
9794 if (argvars[2].v_type == VAR_UNKNOWN)
9795 {
9796 /* Remove one item, return its value. */
9797 vimlist_remove(l, item, item);
9798 *rettv = item->li_tv;
9799 vim_free(item);
9800 }
9801 else
9802 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009803 // Remove range of items, return list with values.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009804 end = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009805 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009806 ; // type error: do nothing
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009807 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009808 semsg(_(e_listidx), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009809 else
9810 {
9811 int cnt = 0;
9812
9813 for (li = item; li != NULL; li = li->li_next)
9814 {
9815 ++cnt;
9816 if (li == item2)
9817 break;
9818 }
9819 if (li == NULL) /* didn't find "item2" after "item" */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009820 emsg(_(e_invrange));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009821 else
9822 {
9823 vimlist_remove(l, item, item2);
9824 if (rettv_list_alloc(rettv) == OK)
9825 {
9826 l = rettv->vval.v_list;
9827 l->lv_first = item;
9828 l->lv_last = item2;
9829 item->li_prev = NULL;
9830 item2->li_next = NULL;
9831 l->lv_len = cnt;
9832 }
9833 }
9834 }
9835 }
9836 }
9837 }
9838}
9839
9840/*
9841 * "rename({from}, {to})" function
9842 */
9843 static void
9844f_rename(typval_T *argvars, typval_T *rettv)
9845{
9846 char_u buf[NUMBUFLEN];
9847
9848 if (check_restricted() || check_secure())
9849 rettv->vval.v_number = -1;
9850 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009851 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
9852 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009853}
9854
9855/*
9856 * "repeat()" function
9857 */
9858 static void
9859f_repeat(typval_T *argvars, typval_T *rettv)
9860{
9861 char_u *p;
9862 int n;
9863 int slen;
9864 int len;
9865 char_u *r;
9866 int i;
9867
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009868 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009869 if (argvars[0].v_type == VAR_LIST)
9870 {
9871 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9872 while (n-- > 0)
9873 if (list_extend(rettv->vval.v_list,
9874 argvars[0].vval.v_list, NULL) == FAIL)
9875 break;
9876 }
9877 else
9878 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009879 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009880 rettv->v_type = VAR_STRING;
9881 rettv->vval.v_string = NULL;
9882
9883 slen = (int)STRLEN(p);
9884 len = slen * n;
9885 if (len <= 0)
9886 return;
9887
9888 r = alloc(len + 1);
9889 if (r != NULL)
9890 {
9891 for (i = 0; i < n; i++)
9892 mch_memmove(r + i * slen, p, (size_t)slen);
9893 r[len] = NUL;
9894 }
9895
9896 rettv->vval.v_string = r;
9897 }
9898}
9899
9900/*
9901 * "resolve()" function
9902 */
9903 static void
9904f_resolve(typval_T *argvars, typval_T *rettv)
9905{
9906 char_u *p;
9907#ifdef HAVE_READLINK
9908 char_u *buf = NULL;
9909#endif
9910
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009911 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009912#ifdef FEAT_SHORTCUT
9913 {
9914 char_u *v = NULL;
9915
Bram Moolenaardce1e892019-02-10 23:18:53 +01009916 v = mch_resolve_path(p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009917 if (v != NULL)
9918 rettv->vval.v_string = v;
9919 else
9920 rettv->vval.v_string = vim_strsave(p);
9921 }
9922#else
9923# ifdef HAVE_READLINK
9924 {
9925 char_u *cpy;
9926 int len;
9927 char_u *remain = NULL;
9928 char_u *q;
9929 int is_relative_to_current = FALSE;
9930 int has_trailing_pathsep = FALSE;
9931 int limit = 100;
9932
9933 p = vim_strsave(p);
9934
9935 if (p[0] == '.' && (vim_ispathsep(p[1])
9936 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9937 is_relative_to_current = TRUE;
9938
9939 len = STRLEN(p);
9940 if (len > 0 && after_pathsep(p, p + len))
9941 {
9942 has_trailing_pathsep = TRUE;
9943 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9944 }
9945
9946 q = getnextcomp(p);
9947 if (*q != NUL)
9948 {
9949 /* Separate the first path component in "p", and keep the
9950 * remainder (beginning with the path separator). */
9951 remain = vim_strsave(q - 1);
9952 q[-1] = NUL;
9953 }
9954
9955 buf = alloc(MAXPATHL + 1);
9956 if (buf == NULL)
9957 goto fail;
9958
9959 for (;;)
9960 {
9961 for (;;)
9962 {
9963 len = readlink((char *)p, (char *)buf, MAXPATHL);
9964 if (len <= 0)
9965 break;
9966 buf[len] = NUL;
9967
9968 if (limit-- == 0)
9969 {
9970 vim_free(p);
9971 vim_free(remain);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009972 emsg(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009973 rettv->vval.v_string = NULL;
9974 goto fail;
9975 }
9976
9977 /* Ensure that the result will have a trailing path separator
9978 * if the argument has one. */
9979 if (remain == NULL && has_trailing_pathsep)
9980 add_pathsep(buf);
9981
9982 /* Separate the first path component in the link value and
9983 * concatenate the remainders. */
9984 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9985 if (*q != NUL)
9986 {
9987 if (remain == NULL)
9988 remain = vim_strsave(q - 1);
9989 else
9990 {
9991 cpy = concat_str(q - 1, remain);
9992 if (cpy != NULL)
9993 {
9994 vim_free(remain);
9995 remain = cpy;
9996 }
9997 }
9998 q[-1] = NUL;
9999 }
10000
10001 q = gettail(p);
10002 if (q > p && *q == NUL)
10003 {
10004 /* Ignore trailing path separator. */
10005 q[-1] = NUL;
10006 q = gettail(p);
10007 }
10008 if (q > p && !mch_isFullName(buf))
10009 {
10010 /* symlink is relative to directory of argument */
10011 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
10012 if (cpy != NULL)
10013 {
10014 STRCPY(cpy, p);
10015 STRCPY(gettail(cpy), buf);
10016 vim_free(p);
10017 p = cpy;
10018 }
10019 }
10020 else
10021 {
10022 vim_free(p);
10023 p = vim_strsave(buf);
10024 }
10025 }
10026
10027 if (remain == NULL)
10028 break;
10029
10030 /* Append the first path component of "remain" to "p". */
10031 q = getnextcomp(remain + 1);
10032 len = q - remain - (*q != NUL);
10033 cpy = vim_strnsave(p, STRLEN(p) + len);
10034 if (cpy != NULL)
10035 {
10036 STRNCAT(cpy, remain, len);
10037 vim_free(p);
10038 p = cpy;
10039 }
10040 /* Shorten "remain". */
10041 if (*q != NUL)
10042 STRMOVE(remain, q - 1);
10043 else
Bram Moolenaard23a8232018-02-10 18:45:26 +010010044 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010045 }
10046
10047 /* If the result is a relative path name, make it explicitly relative to
10048 * the current directory if and only if the argument had this form. */
10049 if (!vim_ispathsep(*p))
10050 {
10051 if (is_relative_to_current
10052 && *p != NUL
10053 && !(p[0] == '.'
10054 && (p[1] == NUL
10055 || vim_ispathsep(p[1])
10056 || (p[1] == '.'
10057 && (p[2] == NUL
10058 || vim_ispathsep(p[2]))))))
10059 {
10060 /* Prepend "./". */
10061 cpy = concat_str((char_u *)"./", p);
10062 if (cpy != NULL)
10063 {
10064 vim_free(p);
10065 p = cpy;
10066 }
10067 }
10068 else if (!is_relative_to_current)
10069 {
10070 /* Strip leading "./". */
10071 q = p;
10072 while (q[0] == '.' && vim_ispathsep(q[1]))
10073 q += 2;
10074 if (q > p)
10075 STRMOVE(p, p + 2);
10076 }
10077 }
10078
10079 /* Ensure that the result will have no trailing path separator
10080 * if the argument had none. But keep "/" or "//". */
10081 if (!has_trailing_pathsep)
10082 {
10083 q = p + STRLEN(p);
10084 if (after_pathsep(p, q))
10085 *gettail_sep(p) = NUL;
10086 }
10087
10088 rettv->vval.v_string = p;
10089 }
10090# else
10091 rettv->vval.v_string = vim_strsave(p);
10092# endif
10093#endif
10094
10095 simplify_filename(rettv->vval.v_string);
10096
10097#ifdef HAVE_READLINK
10098fail:
10099 vim_free(buf);
10100#endif
10101 rettv->v_type = VAR_STRING;
10102}
10103
10104/*
10105 * "reverse({list})" function
10106 */
10107 static void
10108f_reverse(typval_T *argvars, typval_T *rettv)
10109{
10110 list_T *l;
10111 listitem_T *li, *ni;
10112
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010113 if (argvars[0].v_type == VAR_BLOB)
10114 {
10115 blob_T *b = argvars[0].vval.v_blob;
10116 int i, len = blob_len(b);
10117
10118 for (i = 0; i < len / 2; i++)
10119 {
10120 int tmp = blob_get(b, i);
10121
10122 blob_set(b, i, blob_get(b, len - i - 1));
10123 blob_set(b, len - i - 1, tmp);
10124 }
10125 rettv_blob_set(rettv, b);
10126 return;
10127 }
10128
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010129 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010130 semsg(_(e_listblobarg), "reverse()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010131 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010132 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010133 (char_u *)N_("reverse() argument"), TRUE))
10134 {
10135 li = l->lv_last;
10136 l->lv_first = l->lv_last = NULL;
10137 l->lv_len = 0;
10138 while (li != NULL)
10139 {
10140 ni = li->li_prev;
10141 list_append(l, li);
10142 li = ni;
10143 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020010144 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010145 l->lv_idx = l->lv_len - l->lv_idx - 1;
10146 }
10147}
10148
10149#define SP_NOMOVE 0x01 /* don't move cursor */
10150#define SP_REPEAT 0x02 /* repeat to find outer pair */
10151#define SP_RETCOUNT 0x04 /* return matchcount */
10152#define SP_SETPCMARK 0x08 /* set previous context mark */
10153#define SP_START 0x10 /* accept match at start position */
10154#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
10155#define SP_END 0x40 /* leave cursor at end of match */
10156#define SP_COLUMN 0x80 /* start at cursor column */
10157
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010158/*
10159 * Get flags for a search function.
10160 * Possibly sets "p_ws".
10161 * Returns BACKWARD, FORWARD or zero (for an error).
10162 */
10163 static int
10164get_search_arg(typval_T *varp, int *flagsp)
10165{
10166 int dir = FORWARD;
10167 char_u *flags;
10168 char_u nbuf[NUMBUFLEN];
10169 int mask;
10170
10171 if (varp->v_type != VAR_UNKNOWN)
10172 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010173 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010174 if (flags == NULL)
10175 return 0; /* type error; errmsg already given */
10176 while (*flags != NUL)
10177 {
10178 switch (*flags)
10179 {
10180 case 'b': dir = BACKWARD; break;
10181 case 'w': p_ws = TRUE; break;
10182 case 'W': p_ws = FALSE; break;
10183 default: mask = 0;
10184 if (flagsp != NULL)
10185 switch (*flags)
10186 {
10187 case 'c': mask = SP_START; break;
10188 case 'e': mask = SP_END; break;
10189 case 'm': mask = SP_RETCOUNT; break;
10190 case 'n': mask = SP_NOMOVE; break;
10191 case 'p': mask = SP_SUBPAT; break;
10192 case 'r': mask = SP_REPEAT; break;
10193 case 's': mask = SP_SETPCMARK; break;
10194 case 'z': mask = SP_COLUMN; break;
10195 }
10196 if (mask == 0)
10197 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010198 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010199 dir = 0;
10200 }
10201 else
10202 *flagsp |= mask;
10203 }
10204 if (dir == 0)
10205 break;
10206 ++flags;
10207 }
10208 }
10209 return dir;
10210}
10211
10212/*
10213 * Shared by search() and searchpos() functions.
10214 */
10215 static int
10216search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
10217{
10218 int flags;
10219 char_u *pat;
10220 pos_T pos;
10221 pos_T save_cursor;
10222 int save_p_ws = p_ws;
10223 int dir;
10224 int retval = 0; /* default: FAIL */
10225 long lnum_stop = 0;
10226 proftime_T tm;
10227#ifdef FEAT_RELTIME
10228 long time_limit = 0;
10229#endif
10230 int options = SEARCH_KEEP;
10231 int subpatnum;
10232
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010233 pat = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010234 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
10235 if (dir == 0)
10236 goto theend;
10237 flags = *flagsp;
10238 if (flags & SP_START)
10239 options |= SEARCH_START;
10240 if (flags & SP_END)
10241 options |= SEARCH_END;
10242 if (flags & SP_COLUMN)
10243 options |= SEARCH_COL;
10244
10245 /* Optional arguments: line number to stop searching and timeout. */
10246 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
10247 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010248 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010249 if (lnum_stop < 0)
10250 goto theend;
10251#ifdef FEAT_RELTIME
10252 if (argvars[3].v_type != VAR_UNKNOWN)
10253 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010254 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010255 if (time_limit < 0)
10256 goto theend;
10257 }
10258#endif
10259 }
10260
10261#ifdef FEAT_RELTIME
10262 /* Set the time limit, if there is one. */
10263 profile_setlimit(time_limit, &tm);
10264#endif
10265
10266 /*
10267 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
10268 * Check to make sure only those flags are set.
10269 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
10270 * flags cannot be set. Check for that condition also.
10271 */
10272 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
10273 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10274 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010275 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010276 goto theend;
10277 }
10278
10279 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010280 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010281 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010282 if (subpatnum != FAIL)
10283 {
10284 if (flags & SP_SUBPAT)
10285 retval = subpatnum;
10286 else
10287 retval = pos.lnum;
10288 if (flags & SP_SETPCMARK)
10289 setpcmark();
10290 curwin->w_cursor = pos;
10291 if (match_pos != NULL)
10292 {
10293 /* Store the match cursor position */
10294 match_pos->lnum = pos.lnum;
10295 match_pos->col = pos.col + 1;
10296 }
10297 /* "/$" will put the cursor after the end of the line, may need to
10298 * correct that here */
10299 check_cursor();
10300 }
10301
10302 /* If 'n' flag is used: restore cursor position. */
10303 if (flags & SP_NOMOVE)
10304 curwin->w_cursor = save_cursor;
10305 else
10306 curwin->w_set_curswant = TRUE;
10307theend:
10308 p_ws = save_p_ws;
10309
10310 return retval;
10311}
10312
10313#ifdef FEAT_FLOAT
10314
10315/*
10316 * round() is not in C90, use ceil() or floor() instead.
10317 */
10318 float_T
10319vim_round(float_T f)
10320{
10321 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
10322}
10323
10324/*
10325 * "round({float})" function
10326 */
10327 static void
10328f_round(typval_T *argvars, typval_T *rettv)
10329{
10330 float_T f = 0.0;
10331
10332 rettv->v_type = VAR_FLOAT;
10333 if (get_float_arg(argvars, &f) == OK)
10334 rettv->vval.v_float = vim_round(f);
10335 else
10336 rettv->vval.v_float = 0.0;
10337}
10338#endif
10339
10340/*
10341 * "screenattr()" function
10342 */
10343 static void
10344f_screenattr(typval_T *argvars, typval_T *rettv)
10345{
10346 int row;
10347 int col;
10348 int c;
10349
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010350 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10351 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010352 if (row < 0 || row >= screen_Rows
10353 || col < 0 || col >= screen_Columns)
10354 c = -1;
10355 else
10356 c = ScreenAttrs[LineOffset[row] + col];
10357 rettv->vval.v_number = c;
10358}
10359
10360/*
10361 * "screenchar()" function
10362 */
10363 static void
10364f_screenchar(typval_T *argvars, typval_T *rettv)
10365{
10366 int row;
10367 int col;
10368 int off;
10369 int c;
10370
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010371 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10372 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010373 if (row < 0 || row >= screen_Rows
10374 || col < 0 || col >= screen_Columns)
10375 c = -1;
10376 else
10377 {
10378 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010379 if (enc_utf8 && ScreenLinesUC[off] != 0)
10380 c = ScreenLinesUC[off];
10381 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010382 c = ScreenLines[off];
10383 }
10384 rettv->vval.v_number = c;
10385}
10386
10387/*
10388 * "screencol()" function
10389 *
10390 * First column is 1 to be consistent with virtcol().
10391 */
10392 static void
10393f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10394{
10395 rettv->vval.v_number = screen_screencol() + 1;
10396}
10397
10398/*
10399 * "screenrow()" function
10400 */
10401 static void
10402f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10403{
10404 rettv->vval.v_number = screen_screenrow() + 1;
10405}
10406
10407/*
10408 * "search()" function
10409 */
10410 static void
10411f_search(typval_T *argvars, typval_T *rettv)
10412{
10413 int flags = 0;
10414
10415 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10416}
10417
10418/*
10419 * "searchdecl()" function
10420 */
10421 static void
10422f_searchdecl(typval_T *argvars, typval_T *rettv)
10423{
10424 int locally = 1;
10425 int thisblock = 0;
10426 int error = FALSE;
10427 char_u *name;
10428
10429 rettv->vval.v_number = 1; /* default: FAIL */
10430
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010431 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010432 if (argvars[1].v_type != VAR_UNKNOWN)
10433 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010434 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010435 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010436 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010437 }
10438 if (!error && name != NULL)
10439 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10440 locally, thisblock, SEARCH_KEEP) == FAIL;
10441}
10442
10443/*
10444 * Used by searchpair() and searchpairpos()
10445 */
10446 static int
10447searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10448{
10449 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010450 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010451 int save_p_ws = p_ws;
10452 int dir;
10453 int flags = 0;
10454 char_u nbuf1[NUMBUFLEN];
10455 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010456 int retval = 0; /* default: FAIL */
10457 long lnum_stop = 0;
10458 long time_limit = 0;
10459
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010460 /* Get the three pattern arguments: start, middle, end. Will result in an
10461 * error if not a valid argument. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010462 spat = tv_get_string_chk(&argvars[0]);
10463 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
10464 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010465 if (spat == NULL || mpat == NULL || epat == NULL)
10466 goto theend; /* type error */
10467
10468 /* Handle the optional fourth argument: flags */
10469 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
10470 if (dir == 0)
10471 goto theend;
10472
10473 /* Don't accept SP_END or SP_SUBPAT.
10474 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
10475 */
10476 if ((flags & (SP_END | SP_SUBPAT)) != 0
10477 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10478 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010479 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010480 goto theend;
10481 }
10482
10483 /* Using 'r' implies 'W', otherwise it doesn't work. */
10484 if (flags & SP_REPEAT)
10485 p_ws = FALSE;
10486
10487 /* Optional fifth argument: skip expression */
10488 if (argvars[3].v_type == VAR_UNKNOWN
10489 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010010490 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010491 else
10492 {
Bram Moolenaar48570482017-10-30 21:48:41 +010010493 skip = &argvars[4];
10494 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
10495 && skip->v_type != VAR_STRING)
10496 {
10497 /* Type error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010498 semsg(_(e_invarg2), tv_get_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010010499 goto theend;
10500 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010501 if (argvars[5].v_type != VAR_UNKNOWN)
10502 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010503 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010504 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010505 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010506 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010507 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010508 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010509#ifdef FEAT_RELTIME
10510 if (argvars[6].v_type != VAR_UNKNOWN)
10511 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010512 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010513 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010514 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010515 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010516 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010517 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010518 }
10519#endif
10520 }
10521 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010522
10523 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
10524 match_pos, lnum_stop, time_limit);
10525
10526theend:
10527 p_ws = save_p_ws;
10528
10529 return retval;
10530}
10531
10532/*
10533 * "searchpair()" function
10534 */
10535 static void
10536f_searchpair(typval_T *argvars, typval_T *rettv)
10537{
10538 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
10539}
10540
10541/*
10542 * "searchpairpos()" function
10543 */
10544 static void
10545f_searchpairpos(typval_T *argvars, typval_T *rettv)
10546{
10547 pos_T match_pos;
10548 int lnum = 0;
10549 int col = 0;
10550
10551 if (rettv_list_alloc(rettv) == FAIL)
10552 return;
10553
10554 if (searchpair_cmn(argvars, &match_pos) > 0)
10555 {
10556 lnum = match_pos.lnum;
10557 col = match_pos.col;
10558 }
10559
10560 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10561 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10562}
10563
10564/*
10565 * Search for a start/middle/end thing.
10566 * Used by searchpair(), see its documentation for the details.
10567 * Returns 0 or -1 for no match,
10568 */
10569 long
10570do_searchpair(
10571 char_u *spat, /* start pattern */
10572 char_u *mpat, /* middle pattern */
10573 char_u *epat, /* end pattern */
10574 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010010575 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010576 int flags, /* SP_SETPCMARK and other SP_ values */
10577 pos_T *match_pos,
10578 linenr_T lnum_stop, /* stop at this line if not zero */
10579 long time_limit UNUSED) /* stop after this many msec */
10580{
10581 char_u *save_cpo;
10582 char_u *pat, *pat2 = NULL, *pat3 = NULL;
10583 long retval = 0;
10584 pos_T pos;
10585 pos_T firstpos;
10586 pos_T foundpos;
10587 pos_T save_cursor;
10588 pos_T save_pos;
10589 int n;
10590 int r;
10591 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010010592 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010593 int err;
10594 int options = SEARCH_KEEP;
10595 proftime_T tm;
10596
10597 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10598 save_cpo = p_cpo;
10599 p_cpo = empty_option;
10600
10601#ifdef FEAT_RELTIME
10602 /* Set the time limit, if there is one. */
10603 profile_setlimit(time_limit, &tm);
10604#endif
10605
10606 /* Make two search patterns: start/end (pat2, for in nested pairs) and
10607 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010608 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
10609 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010610 if (pat2 == NULL || pat3 == NULL)
10611 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010612 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010613 if (*mpat == NUL)
10614 STRCPY(pat3, pat2);
10615 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010616 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010617 spat, epat, mpat);
10618 if (flags & SP_START)
10619 options |= SEARCH_START;
10620
Bram Moolenaar48570482017-10-30 21:48:41 +010010621 if (skip != NULL)
10622 {
10623 /* Empty string means to not use the skip expression. */
10624 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
10625 use_skip = skip->vval.v_string != NULL
10626 && *skip->vval.v_string != NUL;
10627 }
10628
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010629 save_cursor = curwin->w_cursor;
10630 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010631 CLEAR_POS(&firstpos);
10632 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010633 pat = pat3;
10634 for (;;)
10635 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010636 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010637 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010638 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010639 /* didn't find it or found the first match again: FAIL */
10640 break;
10641
10642 if (firstpos.lnum == 0)
10643 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010644 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010645 {
10646 /* Found the same position again. Can happen with a pattern that
10647 * has "\zs" at the end and searching backwards. Advance one
10648 * character and try again. */
10649 if (dir == BACKWARD)
10650 decl(&pos);
10651 else
10652 incl(&pos);
10653 }
10654 foundpos = pos;
10655
10656 /* clear the start flag to avoid getting stuck here */
10657 options &= ~SEARCH_START;
10658
10659 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010010660 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010661 {
10662 save_pos = curwin->w_cursor;
10663 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010010664 err = FALSE;
10665 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010666 curwin->w_cursor = save_pos;
10667 if (err)
10668 {
10669 /* Evaluating {skip} caused an error, break here. */
10670 curwin->w_cursor = save_cursor;
10671 retval = -1;
10672 break;
10673 }
10674 if (r)
10675 continue;
10676 }
10677
10678 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
10679 {
10680 /* Found end when searching backwards or start when searching
10681 * forward: nested pair. */
10682 ++nest;
10683 pat = pat2; /* nested, don't search for middle */
10684 }
10685 else
10686 {
10687 /* Found end when searching forward or start when searching
10688 * backward: end of (nested) pair; or found middle in outer pair. */
10689 if (--nest == 1)
10690 pat = pat3; /* outer level, search for middle */
10691 }
10692
10693 if (nest == 0)
10694 {
10695 /* Found the match: return matchcount or line number. */
10696 if (flags & SP_RETCOUNT)
10697 ++retval;
10698 else
10699 retval = pos.lnum;
10700 if (flags & SP_SETPCMARK)
10701 setpcmark();
10702 curwin->w_cursor = pos;
10703 if (!(flags & SP_REPEAT))
10704 break;
10705 nest = 1; /* search for next unmatched */
10706 }
10707 }
10708
10709 if (match_pos != NULL)
10710 {
10711 /* Store the match cursor position */
10712 match_pos->lnum = curwin->w_cursor.lnum;
10713 match_pos->col = curwin->w_cursor.col + 1;
10714 }
10715
10716 /* If 'n' flag is used or search failed: restore cursor position. */
10717 if ((flags & SP_NOMOVE) || retval == 0)
10718 curwin->w_cursor = save_cursor;
10719
10720theend:
10721 vim_free(pat2);
10722 vim_free(pat3);
10723 if (p_cpo == empty_option)
10724 p_cpo = save_cpo;
10725 else
10726 /* Darn, evaluating the {skip} expression changed the value. */
10727 free_string_option(save_cpo);
10728
10729 return retval;
10730}
10731
10732/*
10733 * "searchpos()" function
10734 */
10735 static void
10736f_searchpos(typval_T *argvars, typval_T *rettv)
10737{
10738 pos_T match_pos;
10739 int lnum = 0;
10740 int col = 0;
10741 int n;
10742 int flags = 0;
10743
10744 if (rettv_list_alloc(rettv) == FAIL)
10745 return;
10746
10747 n = search_cmn(argvars, &match_pos, &flags);
10748 if (n > 0)
10749 {
10750 lnum = match_pos.lnum;
10751 col = match_pos.col;
10752 }
10753
10754 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10755 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10756 if (flags & SP_SUBPAT)
10757 list_append_number(rettv->vval.v_list, (varnumber_T)n);
10758}
10759
10760 static void
10761f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
10762{
10763#ifdef FEAT_CLIENTSERVER
10764 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010765 char_u *server = tv_get_string_chk(&argvars[0]);
10766 char_u *reply = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010767
10768 rettv->vval.v_number = -1;
10769 if (server == NULL || reply == NULL)
10770 return;
10771 if (check_restricted() || check_secure())
10772 return;
10773# ifdef FEAT_X11
10774 if (check_connection() == FAIL)
10775 return;
10776# endif
10777
10778 if (serverSendReply(server, reply) < 0)
10779 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010780 emsg(_("E258: Unable to send to client"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010781 return;
10782 }
10783 rettv->vval.v_number = 0;
10784#else
10785 rettv->vval.v_number = -1;
10786#endif
10787}
10788
10789 static void
10790f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
10791{
10792 char_u *r = NULL;
10793
10794#ifdef FEAT_CLIENTSERVER
10795# ifdef WIN32
10796 r = serverGetVimNames();
10797# else
10798 make_connection();
10799 if (X_DISPLAY != NULL)
10800 r = serverGetVimNames(X_DISPLAY);
10801# endif
10802#endif
10803 rettv->v_type = VAR_STRING;
10804 rettv->vval.v_string = r;
10805}
10806
10807/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010808 * "setbufline()" function
10809 */
10810 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020010811f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010812{
10813 linenr_T lnum;
10814 buf_T *buf;
10815
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010010816 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010817 if (buf == NULL)
10818 rettv->vval.v_number = 1; /* FAIL */
10819 else
10820 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010821 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020010822 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010823 }
10824}
10825
10826/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010827 * "setbufvar()" function
10828 */
10829 static void
10830f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10831{
10832 buf_T *buf;
10833 char_u *varname, *bufvarname;
10834 typval_T *varp;
10835 char_u nbuf[NUMBUFLEN];
10836
Bram Moolenaar8c62a082019-02-08 14:34:10 +010010837 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010838 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010839 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
10840 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010010841 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010842 varp = &argvars[2];
10843
10844 if (buf != NULL && varname != NULL && varp != NULL)
10845 {
10846 if (*varname == '&')
10847 {
10848 long numval;
10849 char_u *strval;
10850 int error = FALSE;
10851 aco_save_T aco;
10852
10853 /* set curbuf to be our buf, temporarily */
10854 aucmd_prepbuf(&aco, buf);
10855
10856 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010857 numval = (long)tv_get_number_chk(varp, &error);
10858 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010859 if (!error && strval != NULL)
10860 set_option_value(varname, numval, strval, OPT_LOCAL);
10861
10862 /* reset notion of buffer */
10863 aucmd_restbuf(&aco);
10864 }
10865 else
10866 {
10867 buf_T *save_curbuf = curbuf;
10868
10869 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
10870 if (bufvarname != NULL)
10871 {
10872 curbuf = buf;
10873 STRCPY(bufvarname, "b:");
10874 STRCPY(bufvarname + 2, varname);
10875 set_var(bufvarname, varp, TRUE);
10876 vim_free(bufvarname);
10877 curbuf = save_curbuf;
10878 }
10879 }
10880 }
10881}
10882
10883 static void
10884f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10885{
10886 dict_T *d;
10887 dictitem_T *di;
10888 char_u *csearch;
10889
10890 if (argvars[0].v_type != VAR_DICT)
10891 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010892 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010893 return;
10894 }
10895
10896 if ((d = argvars[0].vval.v_dict) != NULL)
10897 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010010898 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010899 if (csearch != NULL)
10900 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010901 if (enc_utf8)
10902 {
10903 int pcc[MAX_MCO];
10904 int c = utfc_ptr2char(csearch, pcc);
10905
10906 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
10907 }
10908 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010909 set_last_csearch(PTR2CHAR(csearch),
10910 csearch, MB_PTR2LEN(csearch));
10911 }
10912
10913 di = dict_find(d, (char_u *)"forward", -1);
10914 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010915 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010916 ? FORWARD : BACKWARD);
10917
10918 di = dict_find(d, (char_u *)"until", -1);
10919 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010920 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010921 }
10922}
10923
10924/*
10925 * "setcmdpos()" function
10926 */
10927 static void
10928f_setcmdpos(typval_T *argvars, typval_T *rettv)
10929{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010930 int pos = (int)tv_get_number(&argvars[0]) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010931
10932 if (pos >= 0)
10933 rettv->vval.v_number = set_cmdline_pos(pos);
10934}
10935
10936/*
10937 * "setfperm({fname}, {mode})" function
10938 */
10939 static void
10940f_setfperm(typval_T *argvars, typval_T *rettv)
10941{
10942 char_u *fname;
10943 char_u modebuf[NUMBUFLEN];
10944 char_u *mode_str;
10945 int i;
10946 int mask;
10947 int mode = 0;
10948
10949 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010950 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010951 if (fname == NULL)
10952 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010953 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010954 if (mode_str == NULL)
10955 return;
10956 if (STRLEN(mode_str) != 9)
10957 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010958 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010959 return;
10960 }
10961
10962 mask = 1;
10963 for (i = 8; i >= 0; --i)
10964 {
10965 if (mode_str[i] != '-')
10966 mode |= mask;
10967 mask = mask << 1;
10968 }
10969 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
10970}
10971
10972/*
10973 * "setline()" function
10974 */
10975 static void
10976f_setline(typval_T *argvars, typval_T *rettv)
10977{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010978 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010979
Bram Moolenaarca851592018-06-06 21:04:07 +020010980 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010981}
10982
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010983/*
10984 * Used by "setqflist()" and "setloclist()" functions
10985 */
10986 static void
10987set_qf_ll_list(
10988 win_T *wp UNUSED,
10989 typval_T *list_arg UNUSED,
10990 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020010991 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010992 typval_T *rettv)
10993{
10994#ifdef FEAT_QUICKFIX
10995 static char *e_invact = N_("E927: Invalid action: '%s'");
10996 char_u *act;
10997 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010998 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010999#endif
11000
11001 rettv->vval.v_number = -1;
11002
11003#ifdef FEAT_QUICKFIX
11004 if (list_arg->v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011005 emsg(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011006 else if (recursive != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011007 emsg(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011008 else
11009 {
11010 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011011 dict_T *d = NULL;
11012 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011013
11014 if (action_arg->v_type == VAR_STRING)
11015 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011016 act = tv_get_string_chk(action_arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011017 if (act == NULL)
11018 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020011019 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
11020 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011021 action = *act;
11022 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011023 semsg(_(e_invact), act);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011024 }
11025 else if (action_arg->v_type == VAR_UNKNOWN)
11026 action = ' ';
11027 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011028 emsg(_(e_stringreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011029
Bram Moolenaard823fa92016-08-12 16:29:27 +020011030 if (action_arg->v_type != VAR_UNKNOWN
11031 && what_arg->v_type != VAR_UNKNOWN)
11032 {
11033 if (what_arg->v_type == VAR_DICT)
11034 d = what_arg->vval.v_dict;
11035 else
11036 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011037 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +020011038 valid_dict = FALSE;
11039 }
11040 }
11041
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011042 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011043 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011044 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
11045 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011046 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011047 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011048 }
11049#endif
11050}
11051
11052/*
11053 * "setloclist()" function
11054 */
11055 static void
11056f_setloclist(typval_T *argvars, typval_T *rettv)
11057{
11058 win_T *win;
11059
11060 rettv->vval.v_number = -1;
11061
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020011062 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011063 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020011064 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011065}
11066
11067/*
11068 * "setmatches()" function
11069 */
11070 static void
11071f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
11072{
11073#ifdef FEAT_SEARCH_EXTRA
11074 list_T *l;
11075 listitem_T *li;
11076 dict_T *d;
11077 list_T *s = NULL;
11078
11079 rettv->vval.v_number = -1;
11080 if (argvars[0].v_type != VAR_LIST)
11081 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011082 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011083 return;
11084 }
11085 if ((l = argvars[0].vval.v_list) != NULL)
11086 {
11087
11088 /* To some extent make sure that we are dealing with a list from
11089 * "getmatches()". */
11090 li = l->lv_first;
11091 while (li != NULL)
11092 {
11093 if (li->li_tv.v_type != VAR_DICT
11094 || (d = li->li_tv.vval.v_dict) == NULL)
11095 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011096 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011097 return;
11098 }
11099 if (!(dict_find(d, (char_u *)"group", -1) != NULL
11100 && (dict_find(d, (char_u *)"pattern", -1) != NULL
11101 || dict_find(d, (char_u *)"pos1", -1) != NULL)
11102 && dict_find(d, (char_u *)"priority", -1) != NULL
11103 && dict_find(d, (char_u *)"id", -1) != NULL))
11104 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011105 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011106 return;
11107 }
11108 li = li->li_next;
11109 }
11110
11111 clear_matches(curwin);
11112 li = l->lv_first;
11113 while (li != NULL)
11114 {
11115 int i = 0;
11116 char_u buf[5];
11117 dictitem_T *di;
11118 char_u *group;
11119 int priority;
11120 int id;
11121 char_u *conceal;
11122
11123 d = li->li_tv.vval.v_dict;
11124 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
11125 {
11126 if (s == NULL)
11127 {
11128 s = list_alloc();
11129 if (s == NULL)
11130 return;
11131 }
11132
11133 /* match from matchaddpos() */
11134 for (i = 1; i < 9; i++)
11135 {
11136 sprintf((char *)buf, (char *)"pos%d", i);
11137 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
11138 {
11139 if (di->di_tv.v_type != VAR_LIST)
11140 return;
11141
11142 list_append_tv(s, &di->di_tv);
11143 s->lv_refcount++;
11144 }
11145 else
11146 break;
11147 }
11148 }
11149
Bram Moolenaar8f667172018-12-14 15:38:31 +010011150 group = dict_get_string(d, (char_u *)"group", TRUE);
11151 priority = (int)dict_get_number(d, (char_u *)"priority");
11152 id = (int)dict_get_number(d, (char_u *)"id");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011153 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar8f667172018-12-14 15:38:31 +010011154 ? dict_get_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011155 : NULL;
11156 if (i == 0)
11157 {
11158 match_add(curwin, group,
Bram Moolenaar8f667172018-12-14 15:38:31 +010011159 dict_get_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011160 priority, id, NULL, conceal);
11161 }
11162 else
11163 {
11164 match_add(curwin, group, NULL, priority, id, s, conceal);
11165 list_unref(s);
11166 s = NULL;
11167 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020011168 vim_free(group);
11169 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011170
11171 li = li->li_next;
11172 }
11173 rettv->vval.v_number = 0;
11174 }
11175#endif
11176}
11177
11178/*
11179 * "setpos()" function
11180 */
11181 static void
11182f_setpos(typval_T *argvars, typval_T *rettv)
11183{
11184 pos_T pos;
11185 int fnum;
11186 char_u *name;
11187 colnr_T curswant = -1;
11188
11189 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011190 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011191 if (name != NULL)
11192 {
11193 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
11194 {
11195 if (--pos.col < 0)
11196 pos.col = 0;
11197 if (name[0] == '.' && name[1] == NUL)
11198 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011199 /* set cursor; "fnum" is ignored */
11200 curwin->w_cursor = pos;
11201 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011202 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011203 curwin->w_curswant = curswant - 1;
11204 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011205 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011206 check_cursor();
11207 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011208 }
11209 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
11210 {
11211 /* set mark */
11212 if (setmark_pos(name[1], &pos, fnum) == OK)
11213 rettv->vval.v_number = 0;
11214 }
11215 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011216 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011217 }
11218 }
11219}
11220
11221/*
11222 * "setqflist()" function
11223 */
11224 static void
11225f_setqflist(typval_T *argvars, typval_T *rettv)
11226{
Bram Moolenaard823fa92016-08-12 16:29:27 +020011227 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011228}
11229
11230/*
11231 * "setreg()" function
11232 */
11233 static void
11234f_setreg(typval_T *argvars, typval_T *rettv)
11235{
11236 int regname;
11237 char_u *strregname;
11238 char_u *stropt;
11239 char_u *strval;
11240 int append;
11241 char_u yank_type;
11242 long block_len;
11243
11244 block_len = -1;
11245 yank_type = MAUTO;
11246 append = FALSE;
11247
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011248 strregname = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011249 rettv->vval.v_number = 1; /* FAIL is default */
11250
11251 if (strregname == NULL)
11252 return; /* type error; errmsg already given */
11253 regname = *strregname;
11254 if (regname == 0 || regname == '@')
11255 regname = '"';
11256
11257 if (argvars[2].v_type != VAR_UNKNOWN)
11258 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011259 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011260 if (stropt == NULL)
11261 return; /* type error */
11262 for (; *stropt != NUL; ++stropt)
11263 switch (*stropt)
11264 {
11265 case 'a': case 'A': /* append */
11266 append = TRUE;
11267 break;
11268 case 'v': case 'c': /* character-wise selection */
11269 yank_type = MCHAR;
11270 break;
11271 case 'V': case 'l': /* line-wise selection */
11272 yank_type = MLINE;
11273 break;
11274 case 'b': case Ctrl_V: /* block-wise selection */
11275 yank_type = MBLOCK;
11276 if (VIM_ISDIGIT(stropt[1]))
11277 {
11278 ++stropt;
11279 block_len = getdigits(&stropt) - 1;
11280 --stropt;
11281 }
11282 break;
11283 }
11284 }
11285
11286 if (argvars[1].v_type == VAR_LIST)
11287 {
11288 char_u **lstval;
11289 char_u **allocval;
11290 char_u buf[NUMBUFLEN];
11291 char_u **curval;
11292 char_u **curallocval;
11293 list_T *ll = argvars[1].vval.v_list;
11294 listitem_T *li;
11295 int len;
11296
11297 /* If the list is NULL handle like an empty list. */
11298 len = ll == NULL ? 0 : ll->lv_len;
11299
11300 /* First half: use for pointers to result lines; second half: use for
11301 * pointers to allocated copies. */
11302 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
11303 if (lstval == NULL)
11304 return;
11305 curval = lstval;
11306 allocval = lstval + len + 2;
11307 curallocval = allocval;
11308
11309 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
11310 li = li->li_next)
11311 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011312 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011313 if (strval == NULL)
11314 goto free_lstval;
11315 if (strval == buf)
11316 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011317 /* Need to make a copy, next tv_get_string_buf_chk() will
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011318 * overwrite the string. */
11319 strval = vim_strsave(buf);
11320 if (strval == NULL)
11321 goto free_lstval;
11322 *curallocval++ = strval;
11323 }
11324 *curval++ = strval;
11325 }
11326 *curval++ = NULL;
11327
11328 write_reg_contents_lst(regname, lstval, -1,
11329 append, yank_type, block_len);
11330free_lstval:
11331 while (curallocval > allocval)
11332 vim_free(*--curallocval);
11333 vim_free(lstval);
11334 }
11335 else
11336 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011337 strval = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011338 if (strval == NULL)
11339 return;
11340 write_reg_contents_ex(regname, strval, -1,
11341 append, yank_type, block_len);
11342 }
11343 rettv->vval.v_number = 0;
11344}
11345
11346/*
11347 * "settabvar()" function
11348 */
11349 static void
11350f_settabvar(typval_T *argvars, typval_T *rettv)
11351{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011352 tabpage_T *save_curtab;
11353 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011354 char_u *varname, *tabvarname;
11355 typval_T *varp;
11356
11357 rettv->vval.v_number = 0;
11358
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011359 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011360 return;
11361
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011362 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
11363 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011364 varp = &argvars[2];
11365
Bram Moolenaar4033c552017-09-16 20:54:51 +020011366 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011367 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011368 save_curtab = curtab;
11369 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011370
11371 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
11372 if (tabvarname != NULL)
11373 {
11374 STRCPY(tabvarname, "t:");
11375 STRCPY(tabvarname + 2, varname);
11376 set_var(tabvarname, varp, TRUE);
11377 vim_free(tabvarname);
11378 }
11379
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011380 /* Restore current tabpage */
11381 if (valid_tabpage(save_curtab))
11382 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011383 }
11384}
11385
11386/*
11387 * "settabwinvar()" function
11388 */
11389 static void
11390f_settabwinvar(typval_T *argvars, typval_T *rettv)
11391{
11392 setwinvar(argvars, rettv, 1);
11393}
11394
11395/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011396 * "settagstack()" function
11397 */
11398 static void
11399f_settagstack(typval_T *argvars, typval_T *rettv)
11400{
11401 static char *e_invact2 = N_("E962: Invalid action: '%s'");
11402 win_T *wp;
11403 dict_T *d;
11404 int action = 'r';
11405
11406 rettv->vval.v_number = -1;
11407
11408 // first argument: window number or id
11409 wp = find_win_by_nr_or_id(&argvars[0]);
11410 if (wp == NULL)
11411 return;
11412
11413 // second argument: dict with items to set in the tag stack
11414 if (argvars[1].v_type != VAR_DICT)
11415 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011416 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011417 return;
11418 }
11419 d = argvars[1].vval.v_dict;
11420 if (d == NULL)
11421 return;
11422
11423 // third argument: action - 'a' for append and 'r' for replace.
11424 // default is to replace the stack.
11425 if (argvars[2].v_type == VAR_UNKNOWN)
11426 action = 'r';
11427 else if (argvars[2].v_type == VAR_STRING)
11428 {
11429 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011430 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011431 if (actstr == NULL)
11432 return;
11433 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
11434 action = *actstr;
11435 else
11436 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011437 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011438 return;
11439 }
11440 }
11441 else
11442 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011443 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011444 return;
11445 }
11446
11447 if (set_tagstack(wp, d, action) == OK)
11448 rettv->vval.v_number = 0;
11449}
11450
11451/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011452 * "setwinvar()" function
11453 */
11454 static void
11455f_setwinvar(typval_T *argvars, typval_T *rettv)
11456{
11457 setwinvar(argvars, rettv, 0);
11458}
11459
11460#ifdef FEAT_CRYPT
11461/*
11462 * "sha256({string})" function
11463 */
11464 static void
11465f_sha256(typval_T *argvars, typval_T *rettv)
11466{
11467 char_u *p;
11468
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011469 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011470 rettv->vval.v_string = vim_strsave(
11471 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
11472 rettv->v_type = VAR_STRING;
11473}
11474#endif /* FEAT_CRYPT */
11475
11476/*
11477 * "shellescape({string})" function
11478 */
11479 static void
11480f_shellescape(typval_T *argvars, typval_T *rettv)
11481{
Bram Moolenaar20615522017-06-05 18:46:26 +020011482 int do_special = non_zero_arg(&argvars[1]);
11483
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011484 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011485 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011486 rettv->v_type = VAR_STRING;
11487}
11488
11489/*
11490 * shiftwidth() function
11491 */
11492 static void
11493f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
11494{
Bram Moolenaarf9514162018-11-22 03:08:29 +010011495 rettv->vval.v_number = 0;
11496
11497 if (argvars[0].v_type != VAR_UNKNOWN)
11498 {
11499 long col;
11500
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011501 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +010011502 if (col < 0)
11503 return; // type error; errmsg already given
11504#ifdef FEAT_VARTABS
11505 rettv->vval.v_number = get_sw_value_col(curbuf, col);
11506 return;
11507#endif
11508 }
11509
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011510 rettv->vval.v_number = get_sw_value(curbuf);
11511}
11512
Bram Moolenaar162b7142018-12-21 15:17:36 +010011513#ifdef FEAT_SIGNS
11514/*
11515 * "sign_define()" function
11516 */
11517 static void
11518f_sign_define(typval_T *argvars, typval_T *rettv)
11519{
11520 char_u *name;
11521 dict_T *dict;
11522 char_u *icon = NULL;
11523 char_u *linehl = NULL;
11524 char_u *text = NULL;
11525 char_u *texthl = NULL;
11526
11527 rettv->vval.v_number = -1;
11528
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011529 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011530 if (name == NULL)
11531 return;
11532
11533 if (argvars[1].v_type != VAR_UNKNOWN)
11534 {
11535 if (argvars[1].v_type != VAR_DICT)
11536 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011537 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011538 return;
11539 }
11540
11541 // sign attributes
11542 dict = argvars[1].vval.v_dict;
11543 if (dict_find(dict, (char_u *)"icon", -1) != NULL)
11544 icon = dict_get_string(dict, (char_u *)"icon", TRUE);
11545 if (dict_find(dict, (char_u *)"linehl", -1) != NULL)
11546 linehl = dict_get_string(dict, (char_u *)"linehl", TRUE);
11547 if (dict_find(dict, (char_u *)"text", -1) != NULL)
11548 text = dict_get_string(dict, (char_u *)"text", TRUE);
11549 if (dict_find(dict, (char_u *)"texthl", -1) != NULL)
11550 texthl = dict_get_string(dict, (char_u *)"texthl", TRUE);
11551 }
11552
11553 if (sign_define_by_name(name, icon, linehl, text, texthl) == OK)
11554 rettv->vval.v_number = 0;
11555
11556 vim_free(icon);
11557 vim_free(linehl);
11558 vim_free(text);
11559 vim_free(texthl);
11560}
11561
11562/*
11563 * "sign_getdefined()" function
11564 */
11565 static void
11566f_sign_getdefined(typval_T *argvars, typval_T *rettv)
11567{
11568 char_u *name = NULL;
11569
11570 if (rettv_list_alloc_id(rettv, aid_sign_getdefined) != OK)
11571 return;
11572
11573 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011574 name = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011575
11576 sign_getlist(name, rettv->vval.v_list);
11577}
11578
11579/*
11580 * "sign_getplaced()" function
11581 */
11582 static void
11583f_sign_getplaced(typval_T *argvars, typval_T *rettv)
11584{
11585 buf_T *buf = NULL;
11586 dict_T *dict;
11587 dictitem_T *di;
11588 linenr_T lnum = 0;
11589 int sign_id = 0;
11590 char_u *group = NULL;
11591 int notanum = FALSE;
11592
11593 if (rettv_list_alloc_id(rettv, aid_sign_getplaced) != OK)
11594 return;
11595
11596 if (argvars[0].v_type != VAR_UNKNOWN)
11597 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010011598 // get signs placed in the specified buffer
11599 buf = get_buf_arg(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011600 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010011601 return;
Bram Moolenaar162b7142018-12-21 15:17:36 +010011602
11603 if (argvars[1].v_type != VAR_UNKNOWN)
11604 {
11605 if (argvars[1].v_type != VAR_DICT ||
11606 ((dict = argvars[1].vval.v_dict) == NULL))
11607 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011608 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011609 return;
11610 }
11611 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
11612 {
11613 // get signs placed at this line
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011614 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011615 if (notanum)
11616 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011617 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011618 }
11619 if ((di = dict_find(dict, (char_u *)"id", -1)) != NULL)
11620 {
11621 // get sign placed with this identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011622 sign_id = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011623 if (notanum)
11624 return;
11625 }
11626 if ((di = dict_find(dict, (char_u *)"group", -1)) != NULL)
11627 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011628 group = tv_get_string_chk(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011629 if (group == NULL)
11630 return;
Bram Moolenaar6436cd82018-12-27 00:28:33 +010011631 if (*group == '\0') // empty string means global group
11632 group = NULL;
Bram Moolenaar162b7142018-12-21 15:17:36 +010011633 }
11634 }
11635 }
11636
11637 sign_get_placed(buf, lnum, sign_id, group, rettv->vval.v_list);
11638}
11639
11640/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010011641 * "sign_jump()" function
11642 */
11643 static void
11644f_sign_jump(typval_T *argvars, typval_T *rettv)
11645{
11646 int sign_id;
11647 char_u *sign_group = NULL;
11648 buf_T *buf;
11649 int notanum = FALSE;
11650
11651 rettv->vval.v_number = -1;
11652
11653 // Sign identifer
11654 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
11655 if (notanum)
11656 return;
11657 if (sign_id <= 0)
11658 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011659 emsg(_(e_invarg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010011660 return;
11661 }
11662
11663 // Sign group
11664 sign_group = tv_get_string_chk(&argvars[1]);
11665 if (sign_group == NULL)
11666 return;
11667 if (sign_group[0] == '\0')
11668 sign_group = NULL; // global sign group
11669 else
11670 {
11671 sign_group = vim_strsave(sign_group);
11672 if (sign_group == NULL)
11673 return;
11674 }
11675
11676 // Buffer to place the sign
11677 buf = get_buf_arg(&argvars[2]);
11678 if (buf == NULL)
11679 goto cleanup;
11680
11681 rettv->vval.v_number = sign_jump(sign_id, sign_group, buf);
11682
11683cleanup:
11684 vim_free(sign_group);
11685}
11686
11687/*
Bram Moolenaar162b7142018-12-21 15:17:36 +010011688 * "sign_place()" function
11689 */
11690 static void
11691f_sign_place(typval_T *argvars, typval_T *rettv)
11692{
11693 int sign_id;
11694 char_u *group = NULL;
11695 char_u *sign_name;
11696 buf_T *buf;
11697 dict_T *dict;
11698 dictitem_T *di;
11699 linenr_T lnum = 0;
11700 int prio = SIGN_DEF_PRIO;
11701 int notanum = FALSE;
11702
11703 rettv->vval.v_number = -1;
11704
11705 // Sign identifer
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011706 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011707 if (notanum)
11708 return;
11709 if (sign_id < 0)
11710 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011711 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011712 return;
11713 }
11714
11715 // Sign group
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011716 group = tv_get_string_chk(&argvars[1]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011717 if (group == NULL)
11718 return;
11719 if (group[0] == '\0')
11720 group = NULL; // global sign group
11721 else
11722 {
11723 group = vim_strsave(group);
11724 if (group == NULL)
11725 return;
11726 }
11727
11728 // Sign name
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011729 sign_name = tv_get_string_chk(&argvars[2]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011730 if (sign_name == NULL)
11731 goto cleanup;
11732
11733 // Buffer to place the sign
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010011734 buf = get_buf_arg(&argvars[3]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011735 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010011736 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010011737
11738 if (argvars[4].v_type != VAR_UNKNOWN)
11739 {
11740 if (argvars[4].v_type != VAR_DICT ||
11741 ((dict = argvars[4].vval.v_dict) == NULL))
11742 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011743 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011744 goto cleanup;
11745 }
11746
11747 // Line number where the sign is to be placed
11748 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
11749 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011750 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011751 if (notanum)
11752 goto cleanup;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011753 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011754 }
11755 if ((di = dict_find(dict, (char_u *)"priority", -1)) != NULL)
11756 {
11757 // Sign priority
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011758 prio = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011759 if (notanum)
11760 goto cleanup;
11761 }
11762 }
11763
11764 if (sign_place(&sign_id, group, sign_name, buf, lnum, prio) == OK)
11765 rettv->vval.v_number = sign_id;
11766
11767cleanup:
11768 vim_free(group);
11769}
11770
11771/*
11772 * "sign_undefine()" function
11773 */
11774 static void
11775f_sign_undefine(typval_T *argvars, typval_T *rettv)
11776{
11777 char_u *name;
11778
11779 rettv->vval.v_number = -1;
11780
11781 if (argvars[0].v_type == VAR_UNKNOWN)
11782 {
11783 // Free all the signs
11784 free_signs();
11785 rettv->vval.v_number = 0;
11786 }
11787 else
11788 {
11789 // Free only the specified sign
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011790 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011791 if (name == NULL)
11792 return;
11793
11794 if (sign_undefine_by_name(name) == OK)
11795 rettv->vval.v_number = 0;
11796 }
11797}
11798
11799/*
11800 * "sign_unplace()" function
11801 */
11802 static void
11803f_sign_unplace(typval_T *argvars, typval_T *rettv)
11804{
11805 dict_T *dict;
11806 dictitem_T *di;
11807 int sign_id = 0;
11808 buf_T *buf = NULL;
11809 char_u *group = NULL;
11810
11811 rettv->vval.v_number = -1;
11812
11813 if (argvars[0].v_type != VAR_STRING)
11814 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011815 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011816 return;
11817 }
11818
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011819 group = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011820 if (group[0] == '\0')
11821 group = NULL; // global sign group
11822 else
11823 {
11824 group = vim_strsave(group);
11825 if (group == NULL)
11826 return;
11827 }
11828
11829 if (argvars[1].v_type != VAR_UNKNOWN)
11830 {
11831 if (argvars[1].v_type != VAR_DICT)
11832 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011833 emsg(_(e_dictreq));
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010011834 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010011835 }
11836 dict = argvars[1].vval.v_dict;
11837
11838 if ((di = dict_find(dict, (char_u *)"buffer", -1)) != NULL)
11839 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010011840 buf = get_buf_arg(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011841 if (buf == NULL)
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010011842 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010011843 }
11844 if (dict_find(dict, (char_u *)"id", -1) != NULL)
11845 sign_id = dict_get_number(dict, (char_u *)"id");
11846 }
11847
11848 if (buf == NULL)
11849 {
11850 // Delete the sign in all the buffers
11851 FOR_ALL_BUFFERS(buf)
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010011852 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010011853 rettv->vval.v_number = 0;
11854 }
11855 else
11856 {
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010011857 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010011858 rettv->vval.v_number = 0;
11859 }
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010011860
11861cleanup:
Bram Moolenaar162b7142018-12-21 15:17:36 +010011862 vim_free(group);
11863}
11864#endif
11865
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011866/*
11867 * "simplify()" function
11868 */
11869 static void
11870f_simplify(typval_T *argvars, typval_T *rettv)
11871{
11872 char_u *p;
11873
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011874 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011875 rettv->vval.v_string = vim_strsave(p);
11876 simplify_filename(rettv->vval.v_string); /* simplify in place */
11877 rettv->v_type = VAR_STRING;
11878}
11879
11880#ifdef FEAT_FLOAT
11881/*
11882 * "sin()" function
11883 */
11884 static void
11885f_sin(typval_T *argvars, typval_T *rettv)
11886{
11887 float_T f = 0.0;
11888
11889 rettv->v_type = VAR_FLOAT;
11890 if (get_float_arg(argvars, &f) == OK)
11891 rettv->vval.v_float = sin(f);
11892 else
11893 rettv->vval.v_float = 0.0;
11894}
11895
11896/*
11897 * "sinh()" function
11898 */
11899 static void
11900f_sinh(typval_T *argvars, typval_T *rettv)
11901{
11902 float_T f = 0.0;
11903
11904 rettv->v_type = VAR_FLOAT;
11905 if (get_float_arg(argvars, &f) == OK)
11906 rettv->vval.v_float = sinh(f);
11907 else
11908 rettv->vval.v_float = 0.0;
11909}
11910#endif
11911
11912static int
11913#ifdef __BORLANDC__
11914 _RTLENTRYF
11915#endif
11916 item_compare(const void *s1, const void *s2);
11917static int
11918#ifdef __BORLANDC__
11919 _RTLENTRYF
11920#endif
11921 item_compare2(const void *s1, const void *s2);
11922
11923/* struct used in the array that's given to qsort() */
11924typedef struct
11925{
11926 listitem_T *item;
11927 int idx;
11928} sortItem_T;
11929
11930/* struct storing information about current sort */
11931typedef struct
11932{
11933 int item_compare_ic;
11934 int item_compare_numeric;
11935 int item_compare_numbers;
11936#ifdef FEAT_FLOAT
11937 int item_compare_float;
11938#endif
11939 char_u *item_compare_func;
11940 partial_T *item_compare_partial;
11941 dict_T *item_compare_selfdict;
11942 int item_compare_func_err;
11943 int item_compare_keep_zero;
11944} sortinfo_T;
11945static sortinfo_T *sortinfo = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011946#define ITEM_COMPARE_FAIL 999
11947
11948/*
11949 * Compare functions for f_sort() and f_uniq() below.
11950 */
11951 static int
11952#ifdef __BORLANDC__
11953_RTLENTRYF
11954#endif
11955item_compare(const void *s1, const void *s2)
11956{
11957 sortItem_T *si1, *si2;
11958 typval_T *tv1, *tv2;
11959 char_u *p1, *p2;
11960 char_u *tofree1 = NULL, *tofree2 = NULL;
11961 int res;
11962 char_u numbuf1[NUMBUFLEN];
11963 char_u numbuf2[NUMBUFLEN];
11964
11965 si1 = (sortItem_T *)s1;
11966 si2 = (sortItem_T *)s2;
11967 tv1 = &si1->item->li_tv;
11968 tv2 = &si2->item->li_tv;
11969
11970 if (sortinfo->item_compare_numbers)
11971 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011972 varnumber_T v1 = tv_get_number(tv1);
11973 varnumber_T v2 = tv_get_number(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011974
11975 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11976 }
11977
11978#ifdef FEAT_FLOAT
11979 if (sortinfo->item_compare_float)
11980 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011981 float_T v1 = tv_get_float(tv1);
11982 float_T v2 = tv_get_float(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011983
11984 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11985 }
11986#endif
11987
11988 /* tv2string() puts quotes around a string and allocates memory. Don't do
11989 * that for string variables. Use a single quote when comparing with a
11990 * non-string to do what the docs promise. */
11991 if (tv1->v_type == VAR_STRING)
11992 {
11993 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11994 p1 = (char_u *)"'";
11995 else
11996 p1 = tv1->vval.v_string;
11997 }
11998 else
11999 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
12000 if (tv2->v_type == VAR_STRING)
12001 {
12002 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12003 p2 = (char_u *)"'";
12004 else
12005 p2 = tv2->vval.v_string;
12006 }
12007 else
12008 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
12009 if (p1 == NULL)
12010 p1 = (char_u *)"";
12011 if (p2 == NULL)
12012 p2 = (char_u *)"";
12013 if (!sortinfo->item_compare_numeric)
12014 {
12015 if (sortinfo->item_compare_ic)
12016 res = STRICMP(p1, p2);
12017 else
12018 res = STRCMP(p1, p2);
12019 }
12020 else
12021 {
12022 double n1, n2;
12023 n1 = strtod((char *)p1, (char **)&p1);
12024 n2 = strtod((char *)p2, (char **)&p2);
12025 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
12026 }
12027
12028 /* When the result would be zero, compare the item indexes. Makes the
12029 * sort stable. */
12030 if (res == 0 && !sortinfo->item_compare_keep_zero)
12031 res = si1->idx > si2->idx ? 1 : -1;
12032
12033 vim_free(tofree1);
12034 vim_free(tofree2);
12035 return res;
12036}
12037
12038 static int
12039#ifdef __BORLANDC__
12040_RTLENTRYF
12041#endif
12042item_compare2(const void *s1, const void *s2)
12043{
12044 sortItem_T *si1, *si2;
12045 int res;
12046 typval_T rettv;
12047 typval_T argv[3];
12048 int dummy;
12049 char_u *func_name;
12050 partial_T *partial = sortinfo->item_compare_partial;
12051
12052 /* shortcut after failure in previous call; compare all items equal */
12053 if (sortinfo->item_compare_func_err)
12054 return 0;
12055
12056 si1 = (sortItem_T *)s1;
12057 si2 = (sortItem_T *)s2;
12058
12059 if (partial == NULL)
12060 func_name = sortinfo->item_compare_func;
12061 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012062 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012063
12064 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
12065 * in the copy without changing the original list items. */
12066 copy_tv(&si1->item->li_tv, &argv[0]);
12067 copy_tv(&si2->item->li_tv, &argv[1]);
12068
12069 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
12070 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020012071 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012072 partial, sortinfo->item_compare_selfdict);
12073 clear_tv(&argv[0]);
12074 clear_tv(&argv[1]);
12075
12076 if (res == FAIL)
12077 res = ITEM_COMPARE_FAIL;
12078 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012079 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012080 if (sortinfo->item_compare_func_err)
12081 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
12082 clear_tv(&rettv);
12083
12084 /* When the result would be zero, compare the pointers themselves. Makes
12085 * the sort stable. */
12086 if (res == 0 && !sortinfo->item_compare_keep_zero)
12087 res = si1->idx > si2->idx ? 1 : -1;
12088
12089 return res;
12090}
12091
12092/*
12093 * "sort({list})" function
12094 */
12095 static void
12096do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
12097{
12098 list_T *l;
12099 listitem_T *li;
12100 sortItem_T *ptrs;
12101 sortinfo_T *old_sortinfo;
12102 sortinfo_T info;
12103 long len;
12104 long i;
12105
12106 /* Pointer to current info struct used in compare function. Save and
12107 * restore the current one for nested calls. */
12108 old_sortinfo = sortinfo;
12109 sortinfo = &info;
12110
12111 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012112 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012113 else
12114 {
12115 l = argvars[0].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +010012116 if (l == NULL || var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012117 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
12118 TRUE))
12119 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012120 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012121
12122 len = list_len(l);
12123 if (len <= 1)
12124 goto theend; /* short list sorts pretty quickly */
12125
12126 info.item_compare_ic = FALSE;
12127 info.item_compare_numeric = FALSE;
12128 info.item_compare_numbers = FALSE;
12129#ifdef FEAT_FLOAT
12130 info.item_compare_float = FALSE;
12131#endif
12132 info.item_compare_func = NULL;
12133 info.item_compare_partial = NULL;
12134 info.item_compare_selfdict = NULL;
12135 if (argvars[1].v_type != VAR_UNKNOWN)
12136 {
12137 /* optional second argument: {func} */
12138 if (argvars[1].v_type == VAR_FUNC)
12139 info.item_compare_func = argvars[1].vval.v_string;
12140 else if (argvars[1].v_type == VAR_PARTIAL)
12141 info.item_compare_partial = argvars[1].vval.v_partial;
12142 else
12143 {
12144 int error = FALSE;
12145
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012146 i = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012147 if (error)
12148 goto theend; /* type error; errmsg already given */
12149 if (i == 1)
12150 info.item_compare_ic = TRUE;
12151 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012152 info.item_compare_func = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012153 else if (i != 0)
12154 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012155 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012156 goto theend;
12157 }
12158 if (info.item_compare_func != NULL)
12159 {
12160 if (*info.item_compare_func == NUL)
12161 {
12162 /* empty string means default sort */
12163 info.item_compare_func = NULL;
12164 }
12165 else if (STRCMP(info.item_compare_func, "n") == 0)
12166 {
12167 info.item_compare_func = NULL;
12168 info.item_compare_numeric = TRUE;
12169 }
12170 else if (STRCMP(info.item_compare_func, "N") == 0)
12171 {
12172 info.item_compare_func = NULL;
12173 info.item_compare_numbers = TRUE;
12174 }
12175#ifdef FEAT_FLOAT
12176 else if (STRCMP(info.item_compare_func, "f") == 0)
12177 {
12178 info.item_compare_func = NULL;
12179 info.item_compare_float = TRUE;
12180 }
12181#endif
12182 else if (STRCMP(info.item_compare_func, "i") == 0)
12183 {
12184 info.item_compare_func = NULL;
12185 info.item_compare_ic = TRUE;
12186 }
12187 }
12188 }
12189
12190 if (argvars[2].v_type != VAR_UNKNOWN)
12191 {
12192 /* optional third argument: {dict} */
12193 if (argvars[2].v_type != VAR_DICT)
12194 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012195 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012196 goto theend;
12197 }
12198 info.item_compare_selfdict = argvars[2].vval.v_dict;
12199 }
12200 }
12201
12202 /* Make an array with each entry pointing to an item in the List. */
12203 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
12204 if (ptrs == NULL)
12205 goto theend;
12206
12207 i = 0;
12208 if (sort)
12209 {
12210 /* sort(): ptrs will be the list to sort */
12211 for (li = l->lv_first; li != NULL; li = li->li_next)
12212 {
12213 ptrs[i].item = li;
12214 ptrs[i].idx = i;
12215 ++i;
12216 }
12217
12218 info.item_compare_func_err = FALSE;
12219 info.item_compare_keep_zero = FALSE;
12220 /* test the compare function */
12221 if ((info.item_compare_func != NULL
12222 || info.item_compare_partial != NULL)
12223 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
12224 == ITEM_COMPARE_FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012225 emsg(_("E702: Sort compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012226 else
12227 {
12228 /* Sort the array with item pointers. */
12229 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
12230 info.item_compare_func == NULL
12231 && info.item_compare_partial == NULL
12232 ? item_compare : item_compare2);
12233
12234 if (!info.item_compare_func_err)
12235 {
12236 /* Clear the List and append the items in sorted order. */
12237 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
12238 l->lv_len = 0;
12239 for (i = 0; i < len; ++i)
12240 list_append(l, ptrs[i].item);
12241 }
12242 }
12243 }
12244 else
12245 {
12246 int (*item_compare_func_ptr)(const void *, const void *);
12247
12248 /* f_uniq(): ptrs will be a stack of items to remove */
12249 info.item_compare_func_err = FALSE;
12250 info.item_compare_keep_zero = TRUE;
12251 item_compare_func_ptr = info.item_compare_func != NULL
12252 || info.item_compare_partial != NULL
12253 ? item_compare2 : item_compare;
12254
12255 for (li = l->lv_first; li != NULL && li->li_next != NULL;
12256 li = li->li_next)
12257 {
12258 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
12259 == 0)
12260 ptrs[i++].item = li;
12261 if (info.item_compare_func_err)
12262 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012263 emsg(_("E882: Uniq compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012264 break;
12265 }
12266 }
12267
12268 if (!info.item_compare_func_err)
12269 {
12270 while (--i >= 0)
12271 {
12272 li = ptrs[i].item->li_next;
12273 ptrs[i].item->li_next = li->li_next;
12274 if (li->li_next != NULL)
12275 li->li_next->li_prev = ptrs[i].item;
12276 else
12277 l->lv_last = ptrs[i].item;
12278 list_fix_watch(l, li);
12279 listitem_free(li);
12280 l->lv_len--;
12281 }
12282 }
12283 }
12284
12285 vim_free(ptrs);
12286 }
12287theend:
12288 sortinfo = old_sortinfo;
12289}
12290
12291/*
12292 * "sort({list})" function
12293 */
12294 static void
12295f_sort(typval_T *argvars, typval_T *rettv)
12296{
12297 do_sort_uniq(argvars, rettv, TRUE);
12298}
12299
12300/*
12301 * "uniq({list})" function
12302 */
12303 static void
12304f_uniq(typval_T *argvars, typval_T *rettv)
12305{
12306 do_sort_uniq(argvars, rettv, FALSE);
12307}
12308
12309/*
12310 * "soundfold({word})" function
12311 */
12312 static void
12313f_soundfold(typval_T *argvars, typval_T *rettv)
12314{
12315 char_u *s;
12316
12317 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012318 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012319#ifdef FEAT_SPELL
12320 rettv->vval.v_string = eval_soundfold(s);
12321#else
12322 rettv->vval.v_string = vim_strsave(s);
12323#endif
12324}
12325
12326/*
12327 * "spellbadword()" function
12328 */
12329 static void
12330f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
12331{
12332 char_u *word = (char_u *)"";
12333 hlf_T attr = HLF_COUNT;
12334 int len = 0;
12335
12336 if (rettv_list_alloc(rettv) == FAIL)
12337 return;
12338
12339#ifdef FEAT_SPELL
12340 if (argvars[0].v_type == VAR_UNKNOWN)
12341 {
12342 /* Find the start and length of the badly spelled word. */
12343 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
12344 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012345 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012346 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012347 curwin->w_set_curswant = TRUE;
12348 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012349 }
12350 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
12351 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012352 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012353 int capcol = -1;
12354
12355 if (str != NULL)
12356 {
12357 /* Check the argument for spelling. */
12358 while (*str != NUL)
12359 {
12360 len = spell_check(curwin, str, &attr, &capcol, FALSE);
12361 if (attr != HLF_COUNT)
12362 {
12363 word = str;
12364 break;
12365 }
12366 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020012367 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012368 }
12369 }
12370 }
12371#endif
12372
12373 list_append_string(rettv->vval.v_list, word, len);
12374 list_append_string(rettv->vval.v_list, (char_u *)(
12375 attr == HLF_SPB ? "bad" :
12376 attr == HLF_SPR ? "rare" :
12377 attr == HLF_SPL ? "local" :
12378 attr == HLF_SPC ? "caps" :
12379 ""), -1);
12380}
12381
12382/*
12383 * "spellsuggest()" function
12384 */
12385 static void
12386f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
12387{
12388#ifdef FEAT_SPELL
12389 char_u *str;
12390 int typeerr = FALSE;
12391 int maxcount;
12392 garray_T ga;
12393 int i;
12394 listitem_T *li;
12395 int need_capital = FALSE;
12396#endif
12397
12398 if (rettv_list_alloc(rettv) == FAIL)
12399 return;
12400
12401#ifdef FEAT_SPELL
12402 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
12403 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012404 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012405 if (argvars[1].v_type != VAR_UNKNOWN)
12406 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012407 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012408 if (maxcount <= 0)
12409 return;
12410 if (argvars[2].v_type != VAR_UNKNOWN)
12411 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012412 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012413 if (typeerr)
12414 return;
12415 }
12416 }
12417 else
12418 maxcount = 25;
12419
12420 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
12421
12422 for (i = 0; i < ga.ga_len; ++i)
12423 {
12424 str = ((char_u **)ga.ga_data)[i];
12425
12426 li = listitem_alloc();
12427 if (li == NULL)
12428 vim_free(str);
12429 else
12430 {
12431 li->li_tv.v_type = VAR_STRING;
12432 li->li_tv.v_lock = 0;
12433 li->li_tv.vval.v_string = str;
12434 list_append(rettv->vval.v_list, li);
12435 }
12436 }
12437 ga_clear(&ga);
12438 }
12439#endif
12440}
12441
12442 static void
12443f_split(typval_T *argvars, typval_T *rettv)
12444{
12445 char_u *str;
12446 char_u *end;
12447 char_u *pat = NULL;
12448 regmatch_T regmatch;
12449 char_u patbuf[NUMBUFLEN];
12450 char_u *save_cpo;
12451 int match;
12452 colnr_T col = 0;
12453 int keepempty = FALSE;
12454 int typeerr = FALSE;
12455
12456 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12457 save_cpo = p_cpo;
12458 p_cpo = (char_u *)"";
12459
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012460 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012461 if (argvars[1].v_type != VAR_UNKNOWN)
12462 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012463 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012464 if (pat == NULL)
12465 typeerr = TRUE;
12466 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012467 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012468 }
12469 if (pat == NULL || *pat == NUL)
12470 pat = (char_u *)"[\\x01- ]\\+";
12471
12472 if (rettv_list_alloc(rettv) == FAIL)
12473 return;
12474 if (typeerr)
12475 return;
12476
12477 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12478 if (regmatch.regprog != NULL)
12479 {
12480 regmatch.rm_ic = FALSE;
12481 while (*str != NUL || keepempty)
12482 {
12483 if (*str == NUL)
12484 match = FALSE; /* empty item at the end */
12485 else
12486 match = vim_regexec_nl(&regmatch, str, col);
12487 if (match)
12488 end = regmatch.startp[0];
12489 else
12490 end = str + STRLEN(str);
12491 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
12492 && *str != NUL && match && end < regmatch.endp[0]))
12493 {
12494 if (list_append_string(rettv->vval.v_list, str,
12495 (int)(end - str)) == FAIL)
12496 break;
12497 }
12498 if (!match)
12499 break;
Bram Moolenaar13505972019-01-24 15:04:48 +010012500 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012501 if (regmatch.endp[0] > str)
12502 col = 0;
12503 else
Bram Moolenaar13505972019-01-24 15:04:48 +010012504 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012505 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012506 str = regmatch.endp[0];
12507 }
12508
12509 vim_regfree(regmatch.regprog);
12510 }
12511
12512 p_cpo = save_cpo;
12513}
12514
12515#ifdef FEAT_FLOAT
12516/*
12517 * "sqrt()" function
12518 */
12519 static void
12520f_sqrt(typval_T *argvars, typval_T *rettv)
12521{
12522 float_T f = 0.0;
12523
12524 rettv->v_type = VAR_FLOAT;
12525 if (get_float_arg(argvars, &f) == OK)
12526 rettv->vval.v_float = sqrt(f);
12527 else
12528 rettv->vval.v_float = 0.0;
12529}
12530
12531/*
12532 * "str2float()" function
12533 */
12534 static void
12535f_str2float(typval_T *argvars, typval_T *rettv)
12536{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012537 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010012538 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012539
Bram Moolenaar08243d22017-01-10 16:12:29 +010012540 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012541 p = skipwhite(p + 1);
12542 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010012543 if (isneg)
12544 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012545 rettv->v_type = VAR_FLOAT;
12546}
12547#endif
12548
12549/*
12550 * "str2nr()" function
12551 */
12552 static void
12553f_str2nr(typval_T *argvars, typval_T *rettv)
12554{
12555 int base = 10;
12556 char_u *p;
12557 varnumber_T n;
12558 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010012559 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012560
12561 if (argvars[1].v_type != VAR_UNKNOWN)
12562 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012563 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012564 if (base != 2 && base != 8 && base != 10 && base != 16)
12565 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012566 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012567 return;
12568 }
12569 }
12570
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012571 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010012572 isneg = (*p == '-');
12573 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012574 p = skipwhite(p + 1);
12575 switch (base)
12576 {
12577 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
12578 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
12579 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
12580 default: what = 0;
12581 }
12582 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010012583 if (isneg)
12584 rettv->vval.v_number = -n;
12585 else
12586 rettv->vval.v_number = n;
12587
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012588}
12589
12590#ifdef HAVE_STRFTIME
12591/*
12592 * "strftime({format}[, {time}])" function
12593 */
12594 static void
12595f_strftime(typval_T *argvars, typval_T *rettv)
12596{
12597 char_u result_buf[256];
12598 struct tm *curtime;
12599 time_t seconds;
12600 char_u *p;
12601
12602 rettv->v_type = VAR_STRING;
12603
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012604 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012605 if (argvars[1].v_type == VAR_UNKNOWN)
12606 seconds = time(NULL);
12607 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012608 seconds = (time_t)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012609 curtime = localtime(&seconds);
12610 /* MSVC returns NULL for an invalid value of seconds. */
12611 if (curtime == NULL)
12612 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
12613 else
12614 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012615 vimconv_T conv;
12616 char_u *enc;
12617
12618 conv.vc_type = CONV_NONE;
12619 enc = enc_locale();
12620 convert_setup(&conv, p_enc, enc);
12621 if (conv.vc_type != CONV_NONE)
12622 p = string_convert(&conv, p, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012623 if (p != NULL)
12624 (void)strftime((char *)result_buf, sizeof(result_buf),
12625 (char *)p, curtime);
12626 else
12627 result_buf[0] = NUL;
12628
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012629 if (conv.vc_type != CONV_NONE)
12630 vim_free(p);
12631 convert_setup(&conv, enc, p_enc);
12632 if (conv.vc_type != CONV_NONE)
12633 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
12634 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012635 rettv->vval.v_string = vim_strsave(result_buf);
12636
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012637 /* Release conversion descriptors */
12638 convert_setup(&conv, NULL, NULL);
12639 vim_free(enc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012640 }
12641}
12642#endif
12643
12644/*
12645 * "strgetchar()" function
12646 */
12647 static void
12648f_strgetchar(typval_T *argvars, typval_T *rettv)
12649{
12650 char_u *str;
12651 int len;
12652 int error = FALSE;
12653 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +010012654 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012655
12656 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012657 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012658 if (str == NULL)
12659 return;
12660 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012661 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012662 if (error)
12663 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012664
Bram Moolenaar13505972019-01-24 15:04:48 +010012665 while (charidx >= 0 && byteidx < len)
12666 {
12667 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012668 {
Bram Moolenaar13505972019-01-24 15:04:48 +010012669 rettv->vval.v_number = mb_ptr2char(str + byteidx);
12670 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012671 }
Bram Moolenaar13505972019-01-24 15:04:48 +010012672 --charidx;
12673 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012674 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012675}
12676
12677/*
12678 * "stridx()" function
12679 */
12680 static void
12681f_stridx(typval_T *argvars, typval_T *rettv)
12682{
12683 char_u buf[NUMBUFLEN];
12684 char_u *needle;
12685 char_u *haystack;
12686 char_u *save_haystack;
12687 char_u *pos;
12688 int start_idx;
12689
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012690 needle = tv_get_string_chk(&argvars[1]);
12691 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012692 rettv->vval.v_number = -1;
12693 if (needle == NULL || haystack == NULL)
12694 return; /* type error; errmsg already given */
12695
12696 if (argvars[2].v_type != VAR_UNKNOWN)
12697 {
12698 int error = FALSE;
12699
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012700 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012701 if (error || start_idx >= (int)STRLEN(haystack))
12702 return;
12703 if (start_idx >= 0)
12704 haystack += start_idx;
12705 }
12706
12707 pos = (char_u *)strstr((char *)haystack, (char *)needle);
12708 if (pos != NULL)
12709 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
12710}
12711
12712/*
12713 * "string()" function
12714 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010012715 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012716f_string(typval_T *argvars, typval_T *rettv)
12717{
12718 char_u *tofree;
12719 char_u numbuf[NUMBUFLEN];
12720
12721 rettv->v_type = VAR_STRING;
12722 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
12723 get_copyID());
12724 /* Make a copy if we have a value but it's not in allocated memory. */
12725 if (rettv->vval.v_string != NULL && tofree == NULL)
12726 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
12727}
12728
12729/*
12730 * "strlen()" function
12731 */
12732 static void
12733f_strlen(typval_T *argvars, typval_T *rettv)
12734{
12735 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012736 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012737}
12738
12739/*
12740 * "strchars()" function
12741 */
12742 static void
12743f_strchars(typval_T *argvars, typval_T *rettv)
12744{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012745 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012746 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012747 varnumber_T len = 0;
12748 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012749
12750 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012751 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012752 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012753 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012754 else
12755 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012756 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
12757 while (*s != NUL)
12758 {
12759 func_mb_ptr2char_adv(&s);
12760 ++len;
12761 }
12762 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012763 }
12764}
12765
12766/*
12767 * "strdisplaywidth()" function
12768 */
12769 static void
12770f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
12771{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012772 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012773 int col = 0;
12774
12775 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012776 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012777
12778 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
12779}
12780
12781/*
12782 * "strwidth()" function
12783 */
12784 static void
12785f_strwidth(typval_T *argvars, typval_T *rettv)
12786{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012787 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012788
Bram Moolenaar13505972019-01-24 15:04:48 +010012789 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012790}
12791
12792/*
12793 * "strcharpart()" function
12794 */
12795 static void
12796f_strcharpart(typval_T *argvars, typval_T *rettv)
12797{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012798 char_u *p;
12799 int nchar;
12800 int nbyte = 0;
12801 int charlen;
12802 int len = 0;
12803 int slen;
12804 int error = FALSE;
12805
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012806 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012807 slen = (int)STRLEN(p);
12808
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012809 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012810 if (!error)
12811 {
12812 if (nchar > 0)
12813 while (nchar > 0 && nbyte < slen)
12814 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012815 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012816 --nchar;
12817 }
12818 else
12819 nbyte = nchar;
12820 if (argvars[2].v_type != VAR_UNKNOWN)
12821 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012822 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012823 while (charlen > 0 && nbyte + len < slen)
12824 {
12825 int off = nbyte + len;
12826
12827 if (off < 0)
12828 len += 1;
12829 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012830 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012831 --charlen;
12832 }
12833 }
12834 else
12835 len = slen - nbyte; /* default: all bytes that are available. */
12836 }
12837
12838 /*
12839 * Only return the overlap between the specified part and the actual
12840 * string.
12841 */
12842 if (nbyte < 0)
12843 {
12844 len += nbyte;
12845 nbyte = 0;
12846 }
12847 else if (nbyte > slen)
12848 nbyte = slen;
12849 if (len < 0)
12850 len = 0;
12851 else if (nbyte + len > slen)
12852 len = slen - nbyte;
12853
12854 rettv->v_type = VAR_STRING;
12855 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012856}
12857
12858/*
12859 * "strpart()" function
12860 */
12861 static void
12862f_strpart(typval_T *argvars, typval_T *rettv)
12863{
12864 char_u *p;
12865 int n;
12866 int len;
12867 int slen;
12868 int error = FALSE;
12869
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012870 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012871 slen = (int)STRLEN(p);
12872
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012873 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012874 if (error)
12875 len = 0;
12876 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012877 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012878 else
12879 len = slen - n; /* default len: all bytes that are available. */
12880
12881 /*
12882 * Only return the overlap between the specified part and the actual
12883 * string.
12884 */
12885 if (n < 0)
12886 {
12887 len += n;
12888 n = 0;
12889 }
12890 else if (n > slen)
12891 n = slen;
12892 if (len < 0)
12893 len = 0;
12894 else if (n + len > slen)
12895 len = slen - n;
12896
12897 rettv->v_type = VAR_STRING;
12898 rettv->vval.v_string = vim_strnsave(p + n, len);
12899}
12900
12901/*
12902 * "strridx()" function
12903 */
12904 static void
12905f_strridx(typval_T *argvars, typval_T *rettv)
12906{
12907 char_u buf[NUMBUFLEN];
12908 char_u *needle;
12909 char_u *haystack;
12910 char_u *rest;
12911 char_u *lastmatch = NULL;
12912 int haystack_len, end_idx;
12913
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012914 needle = tv_get_string_chk(&argvars[1]);
12915 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012916
12917 rettv->vval.v_number = -1;
12918 if (needle == NULL || haystack == NULL)
12919 return; /* type error; errmsg already given */
12920
12921 haystack_len = (int)STRLEN(haystack);
12922 if (argvars[2].v_type != VAR_UNKNOWN)
12923 {
12924 /* Third argument: upper limit for index */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012925 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012926 if (end_idx < 0)
12927 return; /* can never find a match */
12928 }
12929 else
12930 end_idx = haystack_len;
12931
12932 if (*needle == NUL)
12933 {
12934 /* Empty string matches past the end. */
12935 lastmatch = haystack + end_idx;
12936 }
12937 else
12938 {
12939 for (rest = haystack; *rest != '\0'; ++rest)
12940 {
12941 rest = (char_u *)strstr((char *)rest, (char *)needle);
12942 if (rest == NULL || rest > haystack + end_idx)
12943 break;
12944 lastmatch = rest;
12945 }
12946 }
12947
12948 if (lastmatch == NULL)
12949 rettv->vval.v_number = -1;
12950 else
12951 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
12952}
12953
12954/*
12955 * "strtrans()" function
12956 */
12957 static void
12958f_strtrans(typval_T *argvars, typval_T *rettv)
12959{
12960 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012961 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012962}
12963
12964/*
12965 * "submatch()" function
12966 */
12967 static void
12968f_submatch(typval_T *argvars, typval_T *rettv)
12969{
12970 int error = FALSE;
12971 int no;
12972 int retList = 0;
12973
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012974 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012975 if (error)
12976 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012977 if (no < 0 || no >= NSUBEXP)
12978 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012979 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +010012980 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012981 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012982 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012983 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012984 if (error)
12985 return;
12986
12987 if (retList == 0)
12988 {
12989 rettv->v_type = VAR_STRING;
12990 rettv->vval.v_string = reg_submatch(no);
12991 }
12992 else
12993 {
12994 rettv->v_type = VAR_LIST;
12995 rettv->vval.v_list = reg_submatch_list(no);
12996 }
12997}
12998
12999/*
13000 * "substitute()" function
13001 */
13002 static void
13003f_substitute(typval_T *argvars, typval_T *rettv)
13004{
13005 char_u patbuf[NUMBUFLEN];
13006 char_u subbuf[NUMBUFLEN];
13007 char_u flagsbuf[NUMBUFLEN];
13008
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013009 char_u *str = tv_get_string_chk(&argvars[0]);
13010 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013011 char_u *sub = NULL;
13012 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013013 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013014
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013015 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
13016 expr = &argvars[2];
13017 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013018 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013019
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013020 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013021 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
13022 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013023 rettv->vval.v_string = NULL;
13024 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013025 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013026}
13027
13028/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013029 * "swapinfo(swap_filename)" function
13030 */
13031 static void
13032f_swapinfo(typval_T *argvars, typval_T *rettv)
13033{
13034 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013035 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013036}
13037
13038/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020013039 * "swapname(expr)" function
13040 */
13041 static void
13042f_swapname(typval_T *argvars, typval_T *rettv)
13043{
13044 buf_T *buf;
13045
13046 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010013047 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +020013048 if (buf == NULL || buf->b_ml.ml_mfp == NULL
13049 || buf->b_ml.ml_mfp->mf_fname == NULL)
13050 rettv->vval.v_string = NULL;
13051 else
13052 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
13053}
13054
13055/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013056 * "synID(lnum, col, trans)" function
13057 */
13058 static void
13059f_synID(typval_T *argvars UNUSED, typval_T *rettv)
13060{
13061 int id = 0;
13062#ifdef FEAT_SYN_HL
13063 linenr_T lnum;
13064 colnr_T col;
13065 int trans;
13066 int transerr = FALSE;
13067
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013068 lnum = tv_get_lnum(argvars); /* -1 on type error */
13069 col = (linenr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
13070 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013071
13072 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13073 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
13074 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
13075#endif
13076
13077 rettv->vval.v_number = id;
13078}
13079
13080/*
13081 * "synIDattr(id, what [, mode])" function
13082 */
13083 static void
13084f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
13085{
13086 char_u *p = NULL;
13087#ifdef FEAT_SYN_HL
13088 int id;
13089 char_u *what;
13090 char_u *mode;
13091 char_u modebuf[NUMBUFLEN];
13092 int modec;
13093
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013094 id = (int)tv_get_number(&argvars[0]);
13095 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013096 if (argvars[2].v_type != VAR_UNKNOWN)
13097 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013098 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013099 modec = TOLOWER_ASC(mode[0]);
13100 if (modec != 't' && modec != 'c' && modec != 'g')
13101 modec = 0; /* replace invalid with current */
13102 }
13103 else
13104 {
13105#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
13106 if (USE_24BIT)
13107 modec = 'g';
13108 else
13109#endif
13110 if (t_colors > 1)
13111 modec = 'c';
13112 else
13113 modec = 't';
13114 }
13115
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013116 switch (TOLOWER_ASC(what[0]))
13117 {
13118 case 'b':
13119 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
13120 p = highlight_color(id, what, modec);
13121 else /* bold */
13122 p = highlight_has_attr(id, HL_BOLD, modec);
13123 break;
13124
13125 case 'f': /* fg[#] or font */
13126 p = highlight_color(id, what, modec);
13127 break;
13128
13129 case 'i':
13130 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
13131 p = highlight_has_attr(id, HL_INVERSE, modec);
13132 else /* italic */
13133 p = highlight_has_attr(id, HL_ITALIC, modec);
13134 break;
13135
13136 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020013137 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013138 break;
13139
13140 case 'r': /* reverse */
13141 p = highlight_has_attr(id, HL_INVERSE, modec);
13142 break;
13143
13144 case 's':
13145 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
13146 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020013147 /* strikeout */
13148 else if (TOLOWER_ASC(what[1]) == 't' &&
13149 TOLOWER_ASC(what[2]) == 'r')
13150 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013151 else /* standout */
13152 p = highlight_has_attr(id, HL_STANDOUT, modec);
13153 break;
13154
13155 case 'u':
13156 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
13157 /* underline */
13158 p = highlight_has_attr(id, HL_UNDERLINE, modec);
13159 else
13160 /* undercurl */
13161 p = highlight_has_attr(id, HL_UNDERCURL, modec);
13162 break;
13163 }
13164
13165 if (p != NULL)
13166 p = vim_strsave(p);
13167#endif
13168 rettv->v_type = VAR_STRING;
13169 rettv->vval.v_string = p;
13170}
13171
13172/*
13173 * "synIDtrans(id)" function
13174 */
13175 static void
13176f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
13177{
13178 int id;
13179
13180#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013181 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013182
13183 if (id > 0)
13184 id = syn_get_final_id(id);
13185 else
13186#endif
13187 id = 0;
13188
13189 rettv->vval.v_number = id;
13190}
13191
13192/*
13193 * "synconcealed(lnum, col)" function
13194 */
13195 static void
13196f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
13197{
13198#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
13199 linenr_T lnum;
13200 colnr_T col;
13201 int syntax_flags = 0;
13202 int cchar;
13203 int matchid = 0;
13204 char_u str[NUMBUFLEN];
13205#endif
13206
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013207 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013208
13209#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013210 lnum = tv_get_lnum(argvars); /* -1 on type error */
13211 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013212
13213 vim_memset(str, NUL, sizeof(str));
13214
13215 if (rettv_list_alloc(rettv) != FAIL)
13216 {
13217 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13218 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13219 && curwin->w_p_cole > 0)
13220 {
13221 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
13222 syntax_flags = get_syntax_info(&matchid);
13223
13224 /* get the conceal character */
13225 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
13226 {
13227 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020013228 if (cchar == NUL && curwin->w_p_cole == 1)
13229 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013230 if (cchar != NUL)
13231 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013232 if (has_mbyte)
13233 (*mb_char2bytes)(cchar, str);
13234 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013235 str[0] = cchar;
13236 }
13237 }
13238 }
13239
13240 list_append_number(rettv->vval.v_list,
13241 (syntax_flags & HL_CONCEAL) != 0);
13242 /* -1 to auto-determine strlen */
13243 list_append_string(rettv->vval.v_list, str, -1);
13244 list_append_number(rettv->vval.v_list, matchid);
13245 }
13246#endif
13247}
13248
13249/*
13250 * "synstack(lnum, col)" function
13251 */
13252 static void
13253f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
13254{
13255#ifdef FEAT_SYN_HL
13256 linenr_T lnum;
13257 colnr_T col;
13258 int i;
13259 int id;
13260#endif
13261
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013262 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013263
13264#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013265 lnum = tv_get_lnum(argvars); /* -1 on type error */
13266 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013267
13268 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13269 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13270 && rettv_list_alloc(rettv) != FAIL)
13271 {
13272 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
13273 for (i = 0; ; ++i)
13274 {
13275 id = syn_get_stack_item(i);
13276 if (id < 0)
13277 break;
13278 if (list_append_number(rettv->vval.v_list, id) == FAIL)
13279 break;
13280 }
13281 }
13282#endif
13283}
13284
13285 static void
13286get_cmd_output_as_rettv(
13287 typval_T *argvars,
13288 typval_T *rettv,
13289 int retlist)
13290{
13291 char_u *res = NULL;
13292 char_u *p;
13293 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013294 int err = FALSE;
13295 FILE *fd;
13296 list_T *list = NULL;
13297 int flags = SHELL_SILENT;
13298
13299 rettv->v_type = VAR_STRING;
13300 rettv->vval.v_string = NULL;
13301 if (check_restricted() || check_secure())
13302 goto errret;
13303
13304 if (argvars[1].v_type != VAR_UNKNOWN)
13305 {
13306 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010013307 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013308 * command.
13309 */
13310 if ((infile = vim_tempname('i', TRUE)) == NULL)
13311 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013312 emsg(_(e_notmp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013313 goto errret;
13314 }
13315
13316 fd = mch_fopen((char *)infile, WRITEBIN);
13317 if (fd == NULL)
13318 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013319 semsg(_(e_notopen), infile);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013320 goto errret;
13321 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010013322 if (argvars[1].v_type == VAR_NUMBER)
13323 {
13324 linenr_T lnum;
13325 buf_T *buf;
13326
13327 buf = buflist_findnr(argvars[1].vval.v_number);
13328 if (buf == NULL)
13329 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013330 semsg(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010013331 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010013332 goto errret;
13333 }
13334
13335 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
13336 {
13337 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
13338 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
13339 {
13340 err = TRUE;
13341 break;
13342 }
13343 if (putc(NL, fd) == EOF)
13344 {
13345 err = TRUE;
13346 break;
13347 }
13348 }
13349 }
13350 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013351 {
13352 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
13353 err = TRUE;
13354 }
13355 else
13356 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010013357 size_t len;
13358 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013359
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013360 p = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013361 if (p == NULL)
13362 {
13363 fclose(fd);
13364 goto errret; /* type error; errmsg already given */
13365 }
13366 len = STRLEN(p);
13367 if (len > 0 && fwrite(p, len, 1, fd) != 1)
13368 err = TRUE;
13369 }
13370 if (fclose(fd) != 0)
13371 err = TRUE;
13372 if (err)
13373 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013374 emsg(_("E677: Error writing temp file"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013375 goto errret;
13376 }
13377 }
13378
13379 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
13380 * echoes typeahead, that messes up the display. */
13381 if (!msg_silent)
13382 flags += SHELL_COOKED;
13383
13384 if (retlist)
13385 {
13386 int len;
13387 listitem_T *li;
13388 char_u *s = NULL;
13389 char_u *start;
13390 char_u *end;
13391 int i;
13392
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013393 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013394 if (res == NULL)
13395 goto errret;
13396
13397 list = list_alloc();
13398 if (list == NULL)
13399 goto errret;
13400
13401 for (i = 0; i < len; ++i)
13402 {
13403 start = res + i;
13404 while (i < len && res[i] != NL)
13405 ++i;
13406 end = res + i;
13407
13408 s = alloc((unsigned)(end - start + 1));
13409 if (s == NULL)
13410 goto errret;
13411
13412 for (p = s; start < end; ++p, ++start)
13413 *p = *start == NUL ? NL : *start;
13414 *p = NUL;
13415
13416 li = listitem_alloc();
13417 if (li == NULL)
13418 {
13419 vim_free(s);
13420 goto errret;
13421 }
13422 li->li_tv.v_type = VAR_STRING;
13423 li->li_tv.v_lock = 0;
13424 li->li_tv.vval.v_string = s;
13425 list_append(list, li);
13426 }
13427
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013428 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013429 list = NULL;
13430 }
13431 else
13432 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013433 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013434#ifdef USE_CR
13435 /* translate <CR> into <NL> */
13436 if (res != NULL)
13437 {
13438 char_u *s;
13439
13440 for (s = res; *s; ++s)
13441 {
13442 if (*s == CAR)
13443 *s = NL;
13444 }
13445 }
13446#else
13447# ifdef USE_CRNL
13448 /* translate <CR><NL> into <NL> */
13449 if (res != NULL)
13450 {
13451 char_u *s, *d;
13452
13453 d = res;
13454 for (s = res; *s; ++s)
13455 {
13456 if (s[0] == CAR && s[1] == NL)
13457 ++s;
13458 *d++ = *s;
13459 }
13460 *d = NUL;
13461 }
13462# endif
13463#endif
13464 rettv->vval.v_string = res;
13465 res = NULL;
13466 }
13467
13468errret:
13469 if (infile != NULL)
13470 {
13471 mch_remove(infile);
13472 vim_free(infile);
13473 }
13474 if (res != NULL)
13475 vim_free(res);
13476 if (list != NULL)
13477 list_free(list);
13478}
13479
13480/*
13481 * "system()" function
13482 */
13483 static void
13484f_system(typval_T *argvars, typval_T *rettv)
13485{
13486 get_cmd_output_as_rettv(argvars, rettv, FALSE);
13487}
13488
13489/*
13490 * "systemlist()" function
13491 */
13492 static void
13493f_systemlist(typval_T *argvars, typval_T *rettv)
13494{
13495 get_cmd_output_as_rettv(argvars, rettv, TRUE);
13496}
13497
13498/*
13499 * "tabpagebuflist()" function
13500 */
13501 static void
13502f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13503{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013504 tabpage_T *tp;
13505 win_T *wp = NULL;
13506
13507 if (argvars[0].v_type == VAR_UNKNOWN)
13508 wp = firstwin;
13509 else
13510 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013511 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013512 if (tp != NULL)
13513 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
13514 }
13515 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
13516 {
13517 for (; wp != NULL; wp = wp->w_next)
13518 if (list_append_number(rettv->vval.v_list,
13519 wp->w_buffer->b_fnum) == FAIL)
13520 break;
13521 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013522}
13523
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013524/*
13525 * "tabpagenr()" function
13526 */
13527 static void
13528f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
13529{
13530 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013531 char_u *arg;
13532
13533 if (argvars[0].v_type != VAR_UNKNOWN)
13534 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013535 arg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013536 nr = 0;
13537 if (arg != NULL)
13538 {
13539 if (STRCMP(arg, "$") == 0)
13540 nr = tabpage_index(NULL) - 1;
13541 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013542 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013543 }
13544 }
13545 else
13546 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013547 rettv->vval.v_number = nr;
13548}
13549
13550
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013551/*
13552 * Common code for tabpagewinnr() and winnr().
13553 */
13554 static int
13555get_winnr(tabpage_T *tp, typval_T *argvar)
13556{
13557 win_T *twin;
13558 int nr = 1;
13559 win_T *wp;
13560 char_u *arg;
13561
13562 twin = (tp == curtab) ? curwin : tp->tp_curwin;
13563 if (argvar->v_type != VAR_UNKNOWN)
13564 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013565 arg = tv_get_string_chk(argvar);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013566 if (arg == NULL)
13567 nr = 0; /* type error; errmsg already given */
13568 else if (STRCMP(arg, "$") == 0)
13569 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
13570 else if (STRCMP(arg, "#") == 0)
13571 {
13572 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
13573 if (twin == NULL)
13574 nr = 0;
13575 }
13576 else
13577 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013578 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013579 nr = 0;
13580 }
13581 }
13582
13583 if (nr > 0)
13584 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
13585 wp != twin; wp = wp->w_next)
13586 {
13587 if (wp == NULL)
13588 {
13589 /* didn't find it in this tabpage */
13590 nr = 0;
13591 break;
13592 }
13593 ++nr;
13594 }
13595 return nr;
13596}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013597
13598/*
13599 * "tabpagewinnr()" function
13600 */
13601 static void
13602f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
13603{
13604 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013605 tabpage_T *tp;
13606
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013607 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013608 if (tp == NULL)
13609 nr = 0;
13610 else
13611 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013612 rettv->vval.v_number = nr;
13613}
13614
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013615/*
13616 * "tagfiles()" function
13617 */
13618 static void
13619f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
13620{
13621 char_u *fname;
13622 tagname_T tn;
13623 int first;
13624
13625 if (rettv_list_alloc(rettv) == FAIL)
13626 return;
13627 fname = alloc(MAXPATHL);
13628 if (fname == NULL)
13629 return;
13630
13631 for (first = TRUE; ; first = FALSE)
13632 if (get_tagfname(&tn, first, fname) == FAIL
13633 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
13634 break;
13635 tagname_free(&tn);
13636 vim_free(fname);
13637}
13638
13639/*
13640 * "taglist()" function
13641 */
13642 static void
13643f_taglist(typval_T *argvars, typval_T *rettv)
13644{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013645 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013646 char_u *tag_pattern;
13647
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013648 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013649
13650 rettv->vval.v_number = FALSE;
13651 if (*tag_pattern == NUL)
13652 return;
13653
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013654 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013655 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013656 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013657 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013658}
13659
13660/*
13661 * "tempname()" function
13662 */
13663 static void
13664f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
13665{
13666 static int x = 'A';
13667
13668 rettv->v_type = VAR_STRING;
13669 rettv->vval.v_string = vim_tempname(x, FALSE);
13670
13671 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
13672 * names. Skip 'I' and 'O', they are used for shell redirection. */
13673 do
13674 {
13675 if (x == 'Z')
13676 x = '0';
13677 else if (x == '9')
13678 x = 'A';
13679 else
13680 {
13681#ifdef EBCDIC
13682 if (x == 'I')
13683 x = 'J';
13684 else if (x == 'R')
13685 x = 'S';
13686 else
13687#endif
13688 ++x;
13689 }
13690 } while (x == 'I' || x == 'O');
13691}
13692
13693#ifdef FEAT_FLOAT
13694/*
13695 * "tan()" function
13696 */
13697 static void
13698f_tan(typval_T *argvars, typval_T *rettv)
13699{
13700 float_T f = 0.0;
13701
13702 rettv->v_type = VAR_FLOAT;
13703 if (get_float_arg(argvars, &f) == OK)
13704 rettv->vval.v_float = tan(f);
13705 else
13706 rettv->vval.v_float = 0.0;
13707}
13708
13709/*
13710 * "tanh()" function
13711 */
13712 static void
13713f_tanh(typval_T *argvars, typval_T *rettv)
13714{
13715 float_T f = 0.0;
13716
13717 rettv->v_type = VAR_FLOAT;
13718 if (get_float_arg(argvars, &f) == OK)
13719 rettv->vval.v_float = tanh(f);
13720 else
13721 rettv->vval.v_float = 0.0;
13722}
13723#endif
13724
13725/*
13726 * "test_alloc_fail(id, countdown, repeat)" function
13727 */
13728 static void
13729f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
13730{
13731 if (argvars[0].v_type != VAR_NUMBER
13732 || argvars[0].vval.v_number <= 0
13733 || argvars[1].v_type != VAR_NUMBER
13734 || argvars[1].vval.v_number < 0
13735 || argvars[2].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013736 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013737 else
13738 {
13739 alloc_fail_id = argvars[0].vval.v_number;
13740 if (alloc_fail_id >= aid_last)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013741 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013742 alloc_fail_countdown = argvars[1].vval.v_number;
13743 alloc_fail_repeat = argvars[2].vval.v_number;
13744 did_outofmem_msg = FALSE;
13745 }
13746}
13747
13748/*
13749 * "test_autochdir()"
13750 */
13751 static void
13752f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13753{
13754#if defined(FEAT_AUTOCHDIR)
13755 test_autochdir = TRUE;
13756#endif
13757}
13758
13759/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020013760 * "test_feedinput()"
13761 */
13762 static void
13763f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
13764{
13765#ifdef USE_INPUT_BUF
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013766 char_u *val = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5e80de32017-09-03 15:48:12 +020013767
13768 if (val != NULL)
13769 {
13770 trash_input_buf();
13771 add_to_input_buf_csi(val, (int)STRLEN(val));
13772 }
13773#endif
13774}
13775
13776/*
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013777 * "test_option_not_set({name})" function
13778 */
13779 static void
13780f_test_option_not_set(typval_T *argvars, typval_T *rettv UNUSED)
13781{
13782 char_u *name = (char_u *)"";
13783
13784 if (argvars[0].v_type != VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013785 emsg(_(e_invarg));
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013786 else
13787 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013788 name = tv_get_string(&argvars[0]);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013789 if (reset_option_was_set(name) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013790 semsg(_(e_invarg2), name);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013791 }
13792}
13793
13794/*
13795 * "test_override({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013796 */
13797 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013798f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013799{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013800 char_u *name = (char_u *)"";
13801 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013802 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013803
13804 if (argvars[0].v_type != VAR_STRING
13805 || (argvars[1].v_type) != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013806 emsg(_(e_invarg));
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013807 else
13808 {
Bram Moolenaare38197d2018-12-24 23:35:13 +010013809 name = tv_get_string(&argvars[0]);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013810 val = (int)tv_get_number(&argvars[1]);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013811
13812 if (STRCMP(name, (char_u *)"redraw") == 0)
13813 disable_redraw_for_testing = val;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020013814 else if (STRCMP(name, (char_u *)"redraw_flag") == 0)
13815 ignore_redraw_flag_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013816 else if (STRCMP(name, (char_u *)"char_avail") == 0)
13817 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013818 else if (STRCMP(name, (char_u *)"starting") == 0)
13819 {
13820 if (val)
13821 {
13822 if (save_starting < 0)
13823 save_starting = starting;
13824 starting = 0;
13825 }
13826 else
13827 {
13828 starting = save_starting;
13829 save_starting = -1;
13830 }
13831 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013832 else if (STRCMP(name, (char_u *)"nfa_fail") == 0)
13833 nfa_fail_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013834 else if (STRCMP(name, (char_u *)"ALL") == 0)
13835 {
13836 disable_char_avail_for_testing = FALSE;
13837 disable_redraw_for_testing = FALSE;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020013838 ignore_redraw_flag_for_testing = FALSE;
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013839 nfa_fail_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013840 if (save_starting >= 0)
13841 {
13842 starting = save_starting;
13843 save_starting = -1;
13844 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013845 }
13846 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013847 semsg(_(e_invarg2), name);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013848 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013849}
13850
13851/*
13852 * "test_garbagecollect_now()" function
13853 */
13854 static void
13855f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13856{
13857 /* This is dangerous, any Lists and Dicts used internally may be freed
13858 * while still in use. */
13859 garbage_collect(TRUE);
13860}
13861
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013862/*
13863 * "test_ignore_error()" function
13864 */
13865 static void
13866f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
13867{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013868 ignore_error_for_testing(tv_get_string(&argvars[0]));
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013869}
13870
Bram Moolenaarc0f5a782019-01-13 15:16:13 +010013871 static void
13872f_test_null_blob(typval_T *argvars UNUSED, typval_T *rettv)
13873{
13874 rettv->v_type = VAR_BLOB;
13875 rettv->vval.v_blob = NULL;
13876}
13877
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013878#ifdef FEAT_JOB_CHANNEL
13879 static void
13880f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
13881{
13882 rettv->v_type = VAR_CHANNEL;
13883 rettv->vval.v_channel = NULL;
13884}
13885#endif
13886
13887 static void
13888f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
13889{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013890 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013891}
13892
13893#ifdef FEAT_JOB_CHANNEL
13894 static void
13895f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
13896{
13897 rettv->v_type = VAR_JOB;
13898 rettv->vval.v_job = NULL;
13899}
13900#endif
13901
13902 static void
13903f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
13904{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013905 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013906}
13907
13908 static void
13909f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
13910{
13911 rettv->v_type = VAR_PARTIAL;
13912 rettv->vval.v_partial = NULL;
13913}
13914
13915 static void
13916f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
13917{
13918 rettv->v_type = VAR_STRING;
13919 rettv->vval.v_string = NULL;
13920}
13921
Bram Moolenaarab186732018-09-14 21:27:06 +020013922#ifdef FEAT_GUI
13923 static void
13924f_test_scrollbar(typval_T *argvars, typval_T *rettv UNUSED)
13925{
13926 char_u *which;
13927 long value;
13928 int dragging;
13929 scrollbar_T *sb = NULL;
13930
13931 if (argvars[0].v_type != VAR_STRING
13932 || (argvars[1].v_type) != VAR_NUMBER
13933 || (argvars[2].v_type) != VAR_NUMBER)
13934 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013935 emsg(_(e_invarg));
Bram Moolenaarab186732018-09-14 21:27:06 +020013936 return;
13937 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013938 which = tv_get_string(&argvars[0]);
13939 value = tv_get_number(&argvars[1]);
13940 dragging = tv_get_number(&argvars[2]);
Bram Moolenaarab186732018-09-14 21:27:06 +020013941
13942 if (STRCMP(which, "left") == 0)
13943 sb = &curwin->w_scrollbars[SBAR_LEFT];
13944 else if (STRCMP(which, "right") == 0)
13945 sb = &curwin->w_scrollbars[SBAR_RIGHT];
13946 else if (STRCMP(which, "hor") == 0)
13947 sb = &gui.bottom_sbar;
13948 if (sb == NULL)
13949 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013950 semsg(_(e_invarg2), which);
Bram Moolenaarab186732018-09-14 21:27:06 +020013951 return;
13952 }
13953 gui_drag_scrollbar(sb, value, dragging);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020013954# ifndef USE_ON_FLY_SCROLL
13955 // need to loop through normal_cmd() to handle the scroll events
13956 exec_normal(FALSE, TRUE, FALSE);
13957# endif
Bram Moolenaarab186732018-09-14 21:27:06 +020013958}
13959#endif
13960
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013961 static void
13962f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
13963{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013964 time_for_testing = (time_t)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013965}
13966
13967#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
13968/*
13969 * Get a callback from "arg". It can be a Funcref or a function name.
13970 * When "arg" is zero return an empty string.
13971 * Return NULL for an invalid argument.
13972 */
13973 char_u *
13974get_callback(typval_T *arg, partial_T **pp)
13975{
13976 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
13977 {
13978 *pp = arg->vval.v_partial;
13979 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020013980 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013981 }
13982 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020013983 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013984 {
13985 func_ref(arg->vval.v_string);
13986 return arg->vval.v_string;
13987 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013988 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
13989 return (char_u *)"";
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013990 emsg(_("E921: Invalid callback argument"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013991 return NULL;
13992}
13993
13994/*
Bram Moolenaard5359b22018-04-05 22:44:39 +020013995 * Unref/free "callback" and "partial" returned by get_callback().
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013996 */
13997 void
13998free_callback(char_u *callback, partial_T *partial)
13999{
14000 if (partial != NULL)
14001 partial_unref(partial);
14002 else if (callback != NULL)
14003 {
14004 func_unref(callback);
14005 vim_free(callback);
14006 }
14007}
14008#endif
14009
14010#ifdef FEAT_TIMERS
14011/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014012 * "timer_info([timer])" function
14013 */
14014 static void
14015f_timer_info(typval_T *argvars, typval_T *rettv)
14016{
14017 timer_T *timer = NULL;
14018
14019 if (rettv_list_alloc(rettv) != OK)
14020 return;
14021 if (argvars[0].v_type != VAR_UNKNOWN)
14022 {
14023 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014024 emsg(_(e_number_exp));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014025 else
14026 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014027 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014028 if (timer != NULL)
14029 add_timer_info(rettv, timer);
14030 }
14031 }
14032 else
14033 add_timer_info_all(rettv);
14034}
14035
14036/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014037 * "timer_pause(timer, paused)" function
14038 */
14039 static void
14040f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
14041{
14042 timer_T *timer = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014043 int paused = (int)tv_get_number(&argvars[1]);
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014044
14045 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014046 emsg(_(e_number_exp));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014047 else
14048 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014049 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014050 if (timer != NULL)
14051 timer->tr_paused = paused;
14052 }
14053}
14054
14055/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014056 * "timer_start(time, callback [, options])" function
14057 */
14058 static void
14059f_timer_start(typval_T *argvars, typval_T *rettv)
14060{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014061 long msec = (long)tv_get_number(&argvars[0]);
Bram Moolenaar75537a92016-09-05 22:45:28 +020014062 timer_T *timer;
14063 int repeat = 0;
14064 char_u *callback;
14065 dict_T *dict;
14066 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014067
Bram Moolenaar75537a92016-09-05 22:45:28 +020014068 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014069 if (check_secure())
14070 return;
14071 if (argvars[2].v_type != VAR_UNKNOWN)
14072 {
14073 if (argvars[2].v_type != VAR_DICT
14074 || (dict = argvars[2].vval.v_dict) == NULL)
14075 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014076 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014077 return;
14078 }
14079 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014080 repeat = dict_get_number(dict, (char_u *)"repeat");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014081 }
14082
Bram Moolenaar75537a92016-09-05 22:45:28 +020014083 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014084 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020014085 return;
14086
14087 timer = create_timer(msec, repeat);
14088 if (timer == NULL)
14089 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014090 else
14091 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020014092 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020014093 timer->tr_callback = vim_strsave(callback);
14094 else
14095 /* pointer into the partial */
14096 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020014097 timer->tr_partial = partial;
14098 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014099 }
14100}
14101
14102/*
14103 * "timer_stop(timer)" function
14104 */
14105 static void
14106f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
14107{
14108 timer_T *timer;
14109
14110 if (argvars[0].v_type != VAR_NUMBER)
14111 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014112 emsg(_(e_number_exp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014113 return;
14114 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014115 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014116 if (timer != NULL)
14117 stop_timer(timer);
14118}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014119
14120/*
14121 * "timer_stopall()" function
14122 */
14123 static void
14124f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14125{
14126 stop_all_timers();
14127}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014128#endif
14129
14130/*
14131 * "tolower(string)" function
14132 */
14133 static void
14134f_tolower(typval_T *argvars, typval_T *rettv)
14135{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014136 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014137 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014138}
14139
14140/*
14141 * "toupper(string)" function
14142 */
14143 static void
14144f_toupper(typval_T *argvars, typval_T *rettv)
14145{
14146 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014147 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014148}
14149
14150/*
14151 * "tr(string, fromstr, tostr)" function
14152 */
14153 static void
14154f_tr(typval_T *argvars, typval_T *rettv)
14155{
14156 char_u *in_str;
14157 char_u *fromstr;
14158 char_u *tostr;
14159 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014160 int inlen;
14161 int fromlen;
14162 int tolen;
14163 int idx;
14164 char_u *cpstr;
14165 int cplen;
14166 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014167 char_u buf[NUMBUFLEN];
14168 char_u buf2[NUMBUFLEN];
14169 garray_T ga;
14170
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014171 in_str = tv_get_string(&argvars[0]);
14172 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
14173 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014174
14175 /* Default return value: empty string. */
14176 rettv->v_type = VAR_STRING;
14177 rettv->vval.v_string = NULL;
14178 if (fromstr == NULL || tostr == NULL)
14179 return; /* type error; errmsg already given */
14180 ga_init2(&ga, (int)sizeof(char), 80);
14181
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014182 if (!has_mbyte)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014183 /* not multi-byte: fromstr and tostr must be the same length */
14184 if (STRLEN(fromstr) != STRLEN(tostr))
14185 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014186error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014187 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014188 ga_clear(&ga);
14189 return;
14190 }
14191
14192 /* fromstr and tostr have to contain the same number of chars */
14193 while (*in_str != NUL)
14194 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014195 if (has_mbyte)
14196 {
14197 inlen = (*mb_ptr2len)(in_str);
14198 cpstr = in_str;
14199 cplen = inlen;
14200 idx = 0;
14201 for (p = fromstr; *p != NUL; p += fromlen)
14202 {
14203 fromlen = (*mb_ptr2len)(p);
14204 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
14205 {
14206 for (p = tostr; *p != NUL; p += tolen)
14207 {
14208 tolen = (*mb_ptr2len)(p);
14209 if (idx-- == 0)
14210 {
14211 cplen = tolen;
14212 cpstr = p;
14213 break;
14214 }
14215 }
14216 if (*p == NUL) /* tostr is shorter than fromstr */
14217 goto error;
14218 break;
14219 }
14220 ++idx;
14221 }
14222
14223 if (first && cpstr == in_str)
14224 {
14225 /* Check that fromstr and tostr have the same number of
14226 * (multi-byte) characters. Done only once when a character
14227 * of in_str doesn't appear in fromstr. */
14228 first = FALSE;
14229 for (p = tostr; *p != NUL; p += tolen)
14230 {
14231 tolen = (*mb_ptr2len)(p);
14232 --idx;
14233 }
14234 if (idx != 0)
14235 goto error;
14236 }
14237
14238 (void)ga_grow(&ga, cplen);
14239 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
14240 ga.ga_len += cplen;
14241
14242 in_str += inlen;
14243 }
14244 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014245 {
14246 /* When not using multi-byte chars we can do it faster. */
14247 p = vim_strchr(fromstr, *in_str);
14248 if (p != NULL)
14249 ga_append(&ga, tostr[p - fromstr]);
14250 else
14251 ga_append(&ga, *in_str);
14252 ++in_str;
14253 }
14254 }
14255
14256 /* add a terminating NUL */
14257 (void)ga_grow(&ga, 1);
14258 ga_append(&ga, NUL);
14259
14260 rettv->vval.v_string = ga.ga_data;
14261}
14262
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014263/*
14264 * "trim({expr})" function
14265 */
14266 static void
14267f_trim(typval_T *argvars, typval_T *rettv)
14268{
14269 char_u buf1[NUMBUFLEN];
14270 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014271 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014272 char_u *mask = NULL;
14273 char_u *tail;
14274 char_u *prev;
14275 char_u *p;
14276 int c1;
14277
14278 rettv->v_type = VAR_STRING;
14279 if (head == NULL)
14280 {
14281 rettv->vval.v_string = NULL;
14282 return;
14283 }
14284
14285 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014286 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014287
14288 while (*head != NUL)
14289 {
14290 c1 = PTR2CHAR(head);
14291 if (mask == NULL)
14292 {
14293 if (c1 > ' ' && c1 != 0xa0)
14294 break;
14295 }
14296 else
14297 {
14298 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14299 if (c1 == PTR2CHAR(p))
14300 break;
14301 if (*p == NUL)
14302 break;
14303 }
14304 MB_PTR_ADV(head);
14305 }
14306
14307 for (tail = head + STRLEN(head); tail > head; tail = prev)
14308 {
14309 prev = tail;
14310 MB_PTR_BACK(head, prev);
14311 c1 = PTR2CHAR(prev);
14312 if (mask == NULL)
14313 {
14314 if (c1 > ' ' && c1 != 0xa0)
14315 break;
14316 }
14317 else
14318 {
14319 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14320 if (c1 == PTR2CHAR(p))
14321 break;
14322 if (*p == NUL)
14323 break;
14324 }
14325 }
14326 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
14327}
14328
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014329#ifdef FEAT_FLOAT
14330/*
14331 * "trunc({float})" function
14332 */
14333 static void
14334f_trunc(typval_T *argvars, typval_T *rettv)
14335{
14336 float_T f = 0.0;
14337
14338 rettv->v_type = VAR_FLOAT;
14339 if (get_float_arg(argvars, &f) == OK)
14340 /* trunc() is not in C90, use floor() or ceil() instead. */
14341 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
14342 else
14343 rettv->vval.v_float = 0.0;
14344}
14345#endif
14346
14347/*
14348 * "type(expr)" function
14349 */
14350 static void
14351f_type(typval_T *argvars, typval_T *rettv)
14352{
14353 int n = -1;
14354
14355 switch (argvars[0].v_type)
14356 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020014357 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
14358 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014359 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020014360 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
14361 case VAR_LIST: n = VAR_TYPE_LIST; break;
14362 case VAR_DICT: n = VAR_TYPE_DICT; break;
14363 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014364 case VAR_SPECIAL:
14365 if (argvars[0].vval.v_number == VVAL_FALSE
14366 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020014367 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014368 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020014369 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014370 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020014371 case VAR_JOB: n = VAR_TYPE_JOB; break;
14372 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014373 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014374 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010014375 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014376 n = -1;
14377 break;
14378 }
14379 rettv->vval.v_number = n;
14380}
14381
14382/*
14383 * "undofile(name)" function
14384 */
14385 static void
14386f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
14387{
14388 rettv->v_type = VAR_STRING;
14389#ifdef FEAT_PERSISTENT_UNDO
14390 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014391 char_u *fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014392
14393 if (*fname == NUL)
14394 {
14395 /* If there is no file name there will be no undo file. */
14396 rettv->vval.v_string = NULL;
14397 }
14398 else
14399 {
14400 char_u *ffname = FullName_save(fname, FALSE);
14401
14402 if (ffname != NULL)
14403 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
14404 vim_free(ffname);
14405 }
14406 }
14407#else
14408 rettv->vval.v_string = NULL;
14409#endif
14410}
14411
14412/*
14413 * "undotree()" function
14414 */
14415 static void
14416f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
14417{
14418 if (rettv_dict_alloc(rettv) == OK)
14419 {
14420 dict_T *dict = rettv->vval.v_dict;
14421 list_T *list;
14422
Bram Moolenaare0be1672018-07-08 16:50:37 +020014423 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
14424 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
14425 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
14426 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
14427 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
14428 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014429
14430 list = list_alloc();
14431 if (list != NULL)
14432 {
14433 u_eval_tree(curbuf->b_u_oldhead, list);
14434 dict_add_list(dict, "entries", list);
14435 }
14436 }
14437}
14438
14439/*
14440 * "values(dict)" function
14441 */
14442 static void
14443f_values(typval_T *argvars, typval_T *rettv)
14444{
14445 dict_list(argvars, rettv, 1);
14446}
14447
14448/*
14449 * "virtcol(string)" function
14450 */
14451 static void
14452f_virtcol(typval_T *argvars, typval_T *rettv)
14453{
14454 colnr_T vcol = 0;
14455 pos_T *fp;
14456 int fnum = curbuf->b_fnum;
14457
14458 fp = var2fpos(&argvars[0], FALSE, &fnum);
14459 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
14460 && fnum == curbuf->b_fnum)
14461 {
14462 getvvcol(curwin, fp, NULL, NULL, &vcol);
14463 ++vcol;
14464 }
14465
14466 rettv->vval.v_number = vcol;
14467}
14468
14469/*
14470 * "visualmode()" function
14471 */
14472 static void
14473f_visualmode(typval_T *argvars, typval_T *rettv)
14474{
14475 char_u str[2];
14476
14477 rettv->v_type = VAR_STRING;
14478 str[0] = curbuf->b_visual_mode_eval;
14479 str[1] = NUL;
14480 rettv->vval.v_string = vim_strsave(str);
14481
14482 /* A non-zero number or non-empty string argument: reset mode. */
14483 if (non_zero_arg(&argvars[0]))
14484 curbuf->b_visual_mode_eval = NUL;
14485}
14486
14487/*
14488 * "wildmenumode()" function
14489 */
14490 static void
14491f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14492{
14493#ifdef FEAT_WILDMENU
14494 if (wild_menu_showing)
14495 rettv->vval.v_number = 1;
14496#endif
14497}
14498
14499/*
14500 * "winbufnr(nr)" function
14501 */
14502 static void
14503f_winbufnr(typval_T *argvars, typval_T *rettv)
14504{
14505 win_T *wp;
14506
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014507 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014508 if (wp == NULL)
14509 rettv->vval.v_number = -1;
14510 else
14511 rettv->vval.v_number = wp->w_buffer->b_fnum;
14512}
14513
14514/*
14515 * "wincol()" function
14516 */
14517 static void
14518f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
14519{
14520 validate_cursor();
14521 rettv->vval.v_number = curwin->w_wcol + 1;
14522}
14523
14524/*
14525 * "winheight(nr)" function
14526 */
14527 static void
14528f_winheight(typval_T *argvars, typval_T *rettv)
14529{
14530 win_T *wp;
14531
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014532 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014533 if (wp == NULL)
14534 rettv->vval.v_number = -1;
14535 else
14536 rettv->vval.v_number = wp->w_height;
14537}
14538
14539/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020014540 * "winlayout()" function
14541 */
14542 static void
14543f_winlayout(typval_T *argvars, typval_T *rettv)
14544{
14545 tabpage_T *tp;
14546
14547 if (rettv_list_alloc(rettv) != OK)
14548 return;
14549
14550 if (argvars[0].v_type == VAR_UNKNOWN)
14551 tp = curtab;
14552 else
14553 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014554 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020014555 if (tp == NULL)
14556 return;
14557 }
14558
14559 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
14560}
14561
14562/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014563 * "winline()" function
14564 */
14565 static void
14566f_winline(typval_T *argvars UNUSED, typval_T *rettv)
14567{
14568 validate_cursor();
14569 rettv->vval.v_number = curwin->w_wrow + 1;
14570}
14571
14572/*
14573 * "winnr()" function
14574 */
14575 static void
14576f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
14577{
14578 int nr = 1;
14579
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014580 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014581 rettv->vval.v_number = nr;
14582}
14583
14584/*
14585 * "winrestcmd()" function
14586 */
14587 static void
14588f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
14589{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014590 win_T *wp;
14591 int winnr = 1;
14592 garray_T ga;
14593 char_u buf[50];
14594
14595 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020014596 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014597 {
14598 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
14599 ga_concat(&ga, buf);
14600 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
14601 ga_concat(&ga, buf);
14602 ++winnr;
14603 }
14604 ga_append(&ga, NUL);
14605
14606 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014607 rettv->v_type = VAR_STRING;
14608}
14609
14610/*
14611 * "winrestview()" function
14612 */
14613 static void
14614f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
14615{
14616 dict_T *dict;
14617
14618 if (argvars[0].v_type != VAR_DICT
14619 || (dict = argvars[0].vval.v_dict) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014620 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014621 else
14622 {
14623 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014624 curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014625 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014626 curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014627 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014628 curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014629 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
14630 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010014631 curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014632 curwin->w_set_curswant = FALSE;
14633 }
14634
14635 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014636 set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014637#ifdef FEAT_DIFF
14638 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014639 curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014640#endif
14641 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014642 curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014643 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014644 curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014645
14646 check_cursor();
14647 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020014648 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014649 changed_window_setting();
14650
14651 if (curwin->w_topline <= 0)
14652 curwin->w_topline = 1;
14653 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
14654 curwin->w_topline = curbuf->b_ml.ml_line_count;
14655#ifdef FEAT_DIFF
14656 check_topfill(curwin, TRUE);
14657#endif
14658 }
14659}
14660
14661/*
14662 * "winsaveview()" function
14663 */
14664 static void
14665f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
14666{
14667 dict_T *dict;
14668
14669 if (rettv_dict_alloc(rettv) == FAIL)
14670 return;
14671 dict = rettv->vval.v_dict;
14672
Bram Moolenaare0be1672018-07-08 16:50:37 +020014673 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
14674 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +020014675 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014676 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020014677 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014678
Bram Moolenaare0be1672018-07-08 16:50:37 +020014679 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014680#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020014681 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014682#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020014683 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
14684 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014685}
14686
14687/*
14688 * "winwidth(nr)" function
14689 */
14690 static void
14691f_winwidth(typval_T *argvars, typval_T *rettv)
14692{
14693 win_T *wp;
14694
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014695 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014696 if (wp == NULL)
14697 rettv->vval.v_number = -1;
14698 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014699 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014700}
14701
14702/*
14703 * "wordcount()" function
14704 */
14705 static void
14706f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
14707{
14708 if (rettv_dict_alloc(rettv) == FAIL)
14709 return;
14710 cursor_pos_info(rettv->vval.v_dict);
14711}
14712
14713/*
14714 * "writefile()" function
14715 */
14716 static void
14717f_writefile(typval_T *argvars, typval_T *rettv)
14718{
14719 int binary = FALSE;
14720 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014721#ifdef HAVE_FSYNC
14722 int do_fsync = p_fs;
14723#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014724 char_u *fname;
14725 FILE *fd;
14726 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014727 listitem_T *li;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014728 list_T *list = NULL;
14729 blob_T *blob = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014730
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014731 rettv->vval.v_number = -1;
Bram Moolenaar8c62a082019-02-08 14:34:10 +010014732 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014733 return;
14734
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014735 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014736 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014737 list = argvars[0].vval.v_list;
14738 if (list == NULL)
14739 return;
14740 for (li = list->lv_first; li != NULL; li = li->li_next)
14741 if (tv_get_string_chk(&li->li_tv) == NULL)
14742 return;
14743 }
14744 else if (argvars[0].v_type == VAR_BLOB)
14745 {
14746 blob = argvars[0].vval.v_blob;
14747 if (blob == NULL)
14748 return;
14749 }
14750 else
14751 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014752 semsg(_(e_invarg2), "writefile()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014753 return;
14754 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014755
14756 if (argvars[2].v_type != VAR_UNKNOWN)
14757 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014758 char_u *arg2 = tv_get_string_chk(&argvars[2]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014759
14760 if (arg2 == NULL)
14761 return;
14762 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014763 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014764 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014765 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014766#ifdef HAVE_FSYNC
14767 if (vim_strchr(arg2, 's') != NULL)
14768 do_fsync = TRUE;
14769 else if (vim_strchr(arg2, 'S') != NULL)
14770 do_fsync = FALSE;
14771#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014772 }
14773
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014774 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014775 if (fname == NULL)
14776 return;
14777
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014778 /* Always open the file in binary mode, library functions have a mind of
14779 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014780 if (*fname == NUL || (fd = mch_fopen((char *)fname,
14781 append ? APPENDBIN : WRITEBIN)) == NULL)
14782 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014783 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014784 ret = -1;
14785 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014786 else if (blob)
14787 {
14788 if (write_blob(fd, blob) == FAIL)
14789 ret = -1;
14790#ifdef HAVE_FSYNC
14791 else if (do_fsync)
14792 // Ignore the error, the user wouldn't know what to do about it.
14793 // May happen for a device.
Bram Moolenaara7870192019-02-14 12:56:36 +010014794 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014795#endif
14796 fclose(fd);
14797 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014798 else
14799 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014800 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014801 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014802#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010014803 else if (do_fsync)
14804 /* Ignore the error, the user wouldn't know what to do about it.
14805 * May happen for a device. */
Bram Moolenaara7870192019-02-14 12:56:36 +010014806 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014807#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014808 fclose(fd);
14809 }
14810
14811 rettv->vval.v_number = ret;
14812}
14813
14814/*
14815 * "xor(expr, expr)" function
14816 */
14817 static void
14818f_xor(typval_T *argvars, typval_T *rettv)
14819{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014820 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
14821 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014822}
14823
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014824#endif /* FEAT_EVAL */