blob: 8870fd4329641ddabeb944c70ecbfae474a00425 [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 Moolenaar73dad1e2016-07-17 22:13:49 +020032static char *e_stringreq = N_("E928: String required");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020033
34#ifdef FEAT_FLOAT
35static void f_abs(typval_T *argvars, typval_T *rettv);
36static void f_acos(typval_T *argvars, typval_T *rettv);
37#endif
38static void f_add(typval_T *argvars, typval_T *rettv);
39static void f_and(typval_T *argvars, typval_T *rettv);
40static void f_append(typval_T *argvars, typval_T *rettv);
Bram Moolenaarca851592018-06-06 21:04:07 +020041static void f_appendbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020042static void f_argc(typval_T *argvars, typval_T *rettv);
43static void f_argidx(typval_T *argvars, typval_T *rettv);
44static void f_arglistid(typval_T *argvars, typval_T *rettv);
45static void f_argv(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +010046static void f_assert_beeps(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020047static void f_assert_equal(typval_T *argvars, typval_T *rettv);
Bram Moolenaard96ff162018-02-18 22:13:29 +010048static void f_assert_equalfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020049static void f_assert_exception(typval_T *argvars, typval_T *rettv);
50static void f_assert_fails(typval_T *argvars, typval_T *rettv);
51static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaar61c04492016-07-23 15:35:35 +020052static void f_assert_inrange(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020053static void f_assert_match(typval_T *argvars, typval_T *rettv);
54static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
55static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar42205552017-03-18 19:42:22 +010056static void f_assert_report(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020057static void f_assert_true(typval_T *argvars, typval_T *rettv);
58#ifdef FEAT_FLOAT
59static void f_asin(typval_T *argvars, typval_T *rettv);
60static void f_atan(typval_T *argvars, typval_T *rettv);
61static void f_atan2(typval_T *argvars, typval_T *rettv);
62#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010063#ifdef FEAT_BEVAL
64static void f_balloon_show(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010065# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010066static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010067# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010068#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020069static void f_browse(typval_T *argvars, typval_T *rettv);
70static void f_browsedir(typval_T *argvars, typval_T *rettv);
71static void f_bufexists(typval_T *argvars, typval_T *rettv);
72static void f_buflisted(typval_T *argvars, typval_T *rettv);
73static void f_bufloaded(typval_T *argvars, typval_T *rettv);
74static void f_bufname(typval_T *argvars, typval_T *rettv);
75static void f_bufnr(typval_T *argvars, typval_T *rettv);
76static void f_bufwinid(typval_T *argvars, typval_T *rettv);
77static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
78static void f_byte2line(typval_T *argvars, typval_T *rettv);
79static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
80static void f_byteidx(typval_T *argvars, typval_T *rettv);
81static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
82static void f_call(typval_T *argvars, typval_T *rettv);
83#ifdef FEAT_FLOAT
84static void f_ceil(typval_T *argvars, typval_T *rettv);
85#endif
86#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +010087static void f_ch_canread(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020088static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0874a832016-09-01 15:11:51 +020089static void f_ch_close_in(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020090static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
91static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
92static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
93static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
94static void f_ch_info(typval_T *argvars, typval_T *rettv);
95static void f_ch_log(typval_T *argvars, typval_T *rettv);
96static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
97static void f_ch_open(typval_T *argvars, typval_T *rettv);
98static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010099static void f_ch_readblob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200100static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
101static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
102static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
103static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
104static void f_ch_status(typval_T *argvars, typval_T *rettv);
105#endif
106static void f_changenr(typval_T *argvars, typval_T *rettv);
107static void f_char2nr(typval_T *argvars, typval_T *rettv);
108static void f_cindent(typval_T *argvars, typval_T *rettv);
109static void f_clearmatches(typval_T *argvars, typval_T *rettv);
110static void f_col(typval_T *argvars, typval_T *rettv);
111#if defined(FEAT_INS_EXPAND)
112static void f_complete(typval_T *argvars, typval_T *rettv);
113static void f_complete_add(typval_T *argvars, typval_T *rettv);
114static void f_complete_check(typval_T *argvars, typval_T *rettv);
115#endif
116static void f_confirm(typval_T *argvars, typval_T *rettv);
117static void f_copy(typval_T *argvars, typval_T *rettv);
118#ifdef FEAT_FLOAT
119static void f_cos(typval_T *argvars, typval_T *rettv);
120static void f_cosh(typval_T *argvars, typval_T *rettv);
121#endif
122static void f_count(typval_T *argvars, typval_T *rettv);
123static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
124static void f_cursor(typval_T *argsvars, typval_T *rettv);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200125#ifdef WIN3264
126static void f_debugbreak(typval_T *argvars, typval_T *rettv);
127#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200128static void f_deepcopy(typval_T *argvars, typval_T *rettv);
129static void f_delete(typval_T *argvars, typval_T *rettv);
Bram Moolenaard79a2622018-06-07 18:17:46 +0200130static void f_deletebufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200131static void f_did_filetype(typval_T *argvars, typval_T *rettv);
132static void f_diff_filler(typval_T *argvars, typval_T *rettv);
133static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
134static void f_empty(typval_T *argvars, typval_T *rettv);
135static void f_escape(typval_T *argvars, typval_T *rettv);
136static void f_eval(typval_T *argvars, typval_T *rettv);
137static void f_eventhandler(typval_T *argvars, typval_T *rettv);
138static void f_executable(typval_T *argvars, typval_T *rettv);
139static void f_execute(typval_T *argvars, typval_T *rettv);
140static void f_exepath(typval_T *argvars, typval_T *rettv);
141static void f_exists(typval_T *argvars, typval_T *rettv);
142#ifdef FEAT_FLOAT
143static void f_exp(typval_T *argvars, typval_T *rettv);
144#endif
145static void f_expand(typval_T *argvars, typval_T *rettv);
146static void f_extend(typval_T *argvars, typval_T *rettv);
147static void f_feedkeys(typval_T *argvars, typval_T *rettv);
148static void f_filereadable(typval_T *argvars, typval_T *rettv);
149static void f_filewritable(typval_T *argvars, typval_T *rettv);
150static void f_filter(typval_T *argvars, typval_T *rettv);
151static void f_finddir(typval_T *argvars, typval_T *rettv);
152static void f_findfile(typval_T *argvars, typval_T *rettv);
153#ifdef FEAT_FLOAT
154static void f_float2nr(typval_T *argvars, typval_T *rettv);
155static void f_floor(typval_T *argvars, typval_T *rettv);
156static void f_fmod(typval_T *argvars, typval_T *rettv);
157#endif
158static void f_fnameescape(typval_T *argvars, typval_T *rettv);
159static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
160static void f_foldclosed(typval_T *argvars, typval_T *rettv);
161static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
162static void f_foldlevel(typval_T *argvars, typval_T *rettv);
163static void f_foldtext(typval_T *argvars, typval_T *rettv);
164static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
165static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200166static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200167static void f_function(typval_T *argvars, typval_T *rettv);
168static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
169static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200170static void f_getbufinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200171static void f_getbufline(typval_T *argvars, typval_T *rettv);
172static void f_getbufvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100173static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200174static void f_getchar(typval_T *argvars, typval_T *rettv);
175static void f_getcharmod(typval_T *argvars, typval_T *rettv);
176static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
177static void f_getcmdline(typval_T *argvars, typval_T *rettv);
178#if defined(FEAT_CMDL_COMPL)
179static void f_getcompletion(typval_T *argvars, typval_T *rettv);
180#endif
181static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
182static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
183static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
184static void f_getcwd(typval_T *argvars, typval_T *rettv);
185static void f_getfontname(typval_T *argvars, typval_T *rettv);
186static void f_getfperm(typval_T *argvars, typval_T *rettv);
187static void f_getfsize(typval_T *argvars, typval_T *rettv);
188static void f_getftime(typval_T *argvars, typval_T *rettv);
189static void f_getftype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +0100190static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200191static void f_getline(typval_T *argvars, typval_T *rettv);
Bram Moolenaard823fa92016-08-12 16:29:27 +0200192static void f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200193static void f_getmatches(typval_T *argvars, typval_T *rettv);
194static void f_getpid(typval_T *argvars, typval_T *rettv);
195static void f_getcurpos(typval_T *argvars, typval_T *rettv);
196static void f_getpos(typval_T *argvars, typval_T *rettv);
197static void f_getqflist(typval_T *argvars, typval_T *rettv);
198static void f_getreg(typval_T *argvars, typval_T *rettv);
199static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200200static void f_gettabinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200201static void f_gettabvar(typval_T *argvars, typval_T *rettv);
202static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100203static void f_gettagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200204static void f_getwininfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100205static void f_getwinpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200206static void f_getwinposx(typval_T *argvars, typval_T *rettv);
207static void f_getwinposy(typval_T *argvars, typval_T *rettv);
208static void f_getwinvar(typval_T *argvars, typval_T *rettv);
209static void f_glob(typval_T *argvars, typval_T *rettv);
210static void f_globpath(typval_T *argvars, typval_T *rettv);
211static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
212static void f_has(typval_T *argvars, typval_T *rettv);
213static void f_has_key(typval_T *argvars, typval_T *rettv);
214static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
215static void f_hasmapto(typval_T *argvars, typval_T *rettv);
216static void f_histadd(typval_T *argvars, typval_T *rettv);
217static void f_histdel(typval_T *argvars, typval_T *rettv);
218static void f_histget(typval_T *argvars, typval_T *rettv);
219static void f_histnr(typval_T *argvars, typval_T *rettv);
220static void f_hlID(typval_T *argvars, typval_T *rettv);
221static void f_hlexists(typval_T *argvars, typval_T *rettv);
222static void f_hostname(typval_T *argvars, typval_T *rettv);
223static void f_iconv(typval_T *argvars, typval_T *rettv);
224static void f_indent(typval_T *argvars, typval_T *rettv);
225static void f_index(typval_T *argvars, typval_T *rettv);
226static void f_input(typval_T *argvars, typval_T *rettv);
227static void f_inputdialog(typval_T *argvars, typval_T *rettv);
228static void f_inputlist(typval_T *argvars, typval_T *rettv);
229static void f_inputrestore(typval_T *argvars, typval_T *rettv);
230static void f_inputsave(typval_T *argvars, typval_T *rettv);
231static void f_inputsecret(typval_T *argvars, typval_T *rettv);
232static void f_insert(typval_T *argvars, typval_T *rettv);
233static void f_invert(typval_T *argvars, typval_T *rettv);
234static void f_isdirectory(typval_T *argvars, typval_T *rettv);
235static void f_islocked(typval_T *argvars, typval_T *rettv);
236#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
237static void f_isnan(typval_T *argvars, typval_T *rettv);
238#endif
239static void f_items(typval_T *argvars, typval_T *rettv);
240#ifdef FEAT_JOB_CHANNEL
241static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
242static void f_job_info(typval_T *argvars, typval_T *rettv);
243static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
244static void f_job_start(typval_T *argvars, typval_T *rettv);
245static void f_job_stop(typval_T *argvars, typval_T *rettv);
246static void f_job_status(typval_T *argvars, typval_T *rettv);
247#endif
248static void f_join(typval_T *argvars, typval_T *rettv);
249static void f_js_decode(typval_T *argvars, typval_T *rettv);
250static void f_js_encode(typval_T *argvars, typval_T *rettv);
251static void f_json_decode(typval_T *argvars, typval_T *rettv);
252static void f_json_encode(typval_T *argvars, typval_T *rettv);
253static void f_keys(typval_T *argvars, typval_T *rettv);
254static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
255static void f_len(typval_T *argvars, typval_T *rettv);
256static void f_libcall(typval_T *argvars, typval_T *rettv);
257static void f_libcallnr(typval_T *argvars, typval_T *rettv);
258static void f_line(typval_T *argvars, typval_T *rettv);
259static void f_line2byte(typval_T *argvars, typval_T *rettv);
260static void f_lispindent(typval_T *argvars, typval_T *rettv);
261static void f_localtime(typval_T *argvars, typval_T *rettv);
262#ifdef FEAT_FLOAT
263static void f_log(typval_T *argvars, typval_T *rettv);
264static void f_log10(typval_T *argvars, typval_T *rettv);
265#endif
266#ifdef FEAT_LUA
267static void f_luaeval(typval_T *argvars, typval_T *rettv);
268#endif
269static void f_map(typval_T *argvars, typval_T *rettv);
270static void f_maparg(typval_T *argvars, typval_T *rettv);
271static void f_mapcheck(typval_T *argvars, typval_T *rettv);
272static void f_match(typval_T *argvars, typval_T *rettv);
273static void f_matchadd(typval_T *argvars, typval_T *rettv);
274static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
275static void f_matcharg(typval_T *argvars, typval_T *rettv);
276static void f_matchdelete(typval_T *argvars, typval_T *rettv);
277static void f_matchend(typval_T *argvars, typval_T *rettv);
278static void f_matchlist(typval_T *argvars, typval_T *rettv);
279static void f_matchstr(typval_T *argvars, typval_T *rettv);
280static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
281static void f_max(typval_T *argvars, typval_T *rettv);
282static void f_min(typval_T *argvars, typval_T *rettv);
283#ifdef vim_mkdir
284static void f_mkdir(typval_T *argvars, typval_T *rettv);
285#endif
286static void f_mode(typval_T *argvars, typval_T *rettv);
287#ifdef FEAT_MZSCHEME
288static void f_mzeval(typval_T *argvars, typval_T *rettv);
289#endif
290static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
291static void f_nr2char(typval_T *argvars, typval_T *rettv);
292static void f_or(typval_T *argvars, typval_T *rettv);
293static void f_pathshorten(typval_T *argvars, typval_T *rettv);
294#ifdef FEAT_PERL
295static void f_perleval(typval_T *argvars, typval_T *rettv);
296#endif
297#ifdef FEAT_FLOAT
298static void f_pow(typval_T *argvars, typval_T *rettv);
299#endif
300static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
301static void f_printf(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200302#ifdef FEAT_JOB_CHANNEL
303static void f_prompt_setcallback(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200304static void f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200305static void f_prompt_setprompt(typval_T *argvars, typval_T *rettv);
306#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200307static void f_pumvisible(typval_T *argvars, typval_T *rettv);
308#ifdef FEAT_PYTHON3
309static void f_py3eval(typval_T *argvars, typval_T *rettv);
310#endif
311#ifdef FEAT_PYTHON
312static void f_pyeval(typval_T *argvars, typval_T *rettv);
313#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100314#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
315static void f_pyxeval(typval_T *argvars, typval_T *rettv);
316#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200317static void f_range(typval_T *argvars, typval_T *rettv);
318static void f_readfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200319static void f_reg_executing(typval_T *argvars, typval_T *rettv);
320static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200321static void f_reltime(typval_T *argvars, typval_T *rettv);
322#ifdef FEAT_FLOAT
323static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
324#endif
325static void f_reltimestr(typval_T *argvars, typval_T *rettv);
326static void f_remote_expr(typval_T *argvars, typval_T *rettv);
327static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
328static void f_remote_peek(typval_T *argvars, typval_T *rettv);
329static void f_remote_read(typval_T *argvars, typval_T *rettv);
330static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100331static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200332static void f_remove(typval_T *argvars, typval_T *rettv);
333static void f_rename(typval_T *argvars, typval_T *rettv);
334static void f_repeat(typval_T *argvars, typval_T *rettv);
335static void f_resolve(typval_T *argvars, typval_T *rettv);
336static void f_reverse(typval_T *argvars, typval_T *rettv);
337#ifdef FEAT_FLOAT
338static void f_round(typval_T *argvars, typval_T *rettv);
339#endif
340static void f_screenattr(typval_T *argvars, typval_T *rettv);
341static void f_screenchar(typval_T *argvars, typval_T *rettv);
342static void f_screencol(typval_T *argvars, typval_T *rettv);
343static void f_screenrow(typval_T *argvars, typval_T *rettv);
344static void f_search(typval_T *argvars, typval_T *rettv);
345static void f_searchdecl(typval_T *argvars, typval_T *rettv);
346static void f_searchpair(typval_T *argvars, typval_T *rettv);
347static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
348static void f_searchpos(typval_T *argvars, typval_T *rettv);
349static void f_server2client(typval_T *argvars, typval_T *rettv);
350static void f_serverlist(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200351static void f_setbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200352static void f_setbufvar(typval_T *argvars, typval_T *rettv);
353static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
354static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
355static void f_setfperm(typval_T *argvars, typval_T *rettv);
356static void f_setline(typval_T *argvars, typval_T *rettv);
357static void f_setloclist(typval_T *argvars, typval_T *rettv);
358static void f_setmatches(typval_T *argvars, typval_T *rettv);
359static void f_setpos(typval_T *argvars, typval_T *rettv);
360static void f_setqflist(typval_T *argvars, typval_T *rettv);
361static void f_setreg(typval_T *argvars, typval_T *rettv);
362static void f_settabvar(typval_T *argvars, typval_T *rettv);
363static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100364static void f_settagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200365static void f_setwinvar(typval_T *argvars, typval_T *rettv);
366#ifdef FEAT_CRYPT
367static void f_sha256(typval_T *argvars, typval_T *rettv);
368#endif /* FEAT_CRYPT */
369static void f_shellescape(typval_T *argvars, typval_T *rettv);
370static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
Bram Moolenaar162b7142018-12-21 15:17:36 +0100371#ifdef FEAT_SIGNS
372static void f_sign_define(typval_T *argvars, typval_T *rettv);
373static void f_sign_getdefined(typval_T *argvars, typval_T *rettv);
374static void f_sign_getplaced(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100375static void f_sign_jump(typval_T *argvars, typval_T *rettv);
Bram Moolenaar162b7142018-12-21 15:17:36 +0100376static void f_sign_place(typval_T *argvars, typval_T *rettv);
377static void f_sign_undefine(typval_T *argvars, typval_T *rettv);
378static void f_sign_unplace(typval_T *argvars, typval_T *rettv);
379#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200380static void f_simplify(typval_T *argvars, typval_T *rettv);
381#ifdef FEAT_FLOAT
382static void f_sin(typval_T *argvars, typval_T *rettv);
383static void f_sinh(typval_T *argvars, typval_T *rettv);
384#endif
385static void f_sort(typval_T *argvars, typval_T *rettv);
386static void f_soundfold(typval_T *argvars, typval_T *rettv);
387static void f_spellbadword(typval_T *argvars, typval_T *rettv);
388static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
389static void f_split(typval_T *argvars, typval_T *rettv);
390#ifdef FEAT_FLOAT
391static void f_sqrt(typval_T *argvars, typval_T *rettv);
392static void f_str2float(typval_T *argvars, typval_T *rettv);
393#endif
394static void f_str2nr(typval_T *argvars, typval_T *rettv);
395static void f_strchars(typval_T *argvars, typval_T *rettv);
396#ifdef HAVE_STRFTIME
397static void f_strftime(typval_T *argvars, typval_T *rettv);
398#endif
399static void f_strgetchar(typval_T *argvars, typval_T *rettv);
400static void f_stridx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200401static void f_strlen(typval_T *argvars, typval_T *rettv);
402static void f_strcharpart(typval_T *argvars, typval_T *rettv);
403static void f_strpart(typval_T *argvars, typval_T *rettv);
404static void f_strridx(typval_T *argvars, typval_T *rettv);
405static void f_strtrans(typval_T *argvars, typval_T *rettv);
406static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
407static void f_strwidth(typval_T *argvars, typval_T *rettv);
408static void f_submatch(typval_T *argvars, typval_T *rettv);
409static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200410static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200411static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200412static void f_synID(typval_T *argvars, typval_T *rettv);
413static void f_synIDattr(typval_T *argvars, typval_T *rettv);
414static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
415static void f_synstack(typval_T *argvars, typval_T *rettv);
416static void f_synconcealed(typval_T *argvars, typval_T *rettv);
417static void f_system(typval_T *argvars, typval_T *rettv);
418static void f_systemlist(typval_T *argvars, typval_T *rettv);
419static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
420static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
421static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
422static void f_taglist(typval_T *argvars, typval_T *rettv);
423static void f_tagfiles(typval_T *argvars, typval_T *rettv);
424static void f_tempname(typval_T *argvars, typval_T *rettv);
425static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
426static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200427static void f_test_feedinput(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200428static void f_test_option_not_set(typval_T *argvars, typval_T *rettv);
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100429static void f_test_override(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200430static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100431static void f_test_ignore_error(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100432static void f_test_null_blob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200433#ifdef FEAT_JOB_CHANNEL
434static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
435#endif
436static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
437#ifdef FEAT_JOB_CHANNEL
438static void f_test_null_job(typval_T *argvars, typval_T *rettv);
439#endif
440static void f_test_null_list(typval_T *argvars, typval_T *rettv);
441static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
442static void f_test_null_string(typval_T *argvars, typval_T *rettv);
Bram Moolenaarab186732018-09-14 21:27:06 +0200443#ifdef FEAT_GUI
444static void f_test_scrollbar(typval_T *argvars, typval_T *rettv);
445#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200446static void f_test_settime(typval_T *argvars, typval_T *rettv);
447#ifdef FEAT_FLOAT
448static void f_tan(typval_T *argvars, typval_T *rettv);
449static void f_tanh(typval_T *argvars, typval_T *rettv);
450#endif
451#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200452static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200453static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200454static void f_timer_start(typval_T *argvars, typval_T *rettv);
455static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200456static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200457#endif
458static void f_tolower(typval_T *argvars, typval_T *rettv);
459static void f_toupper(typval_T *argvars, typval_T *rettv);
460static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100461static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200462#ifdef FEAT_FLOAT
463static void f_trunc(typval_T *argvars, typval_T *rettv);
464#endif
465static void f_type(typval_T *argvars, typval_T *rettv);
466static void f_undofile(typval_T *argvars, typval_T *rettv);
467static void f_undotree(typval_T *argvars, typval_T *rettv);
468static void f_uniq(typval_T *argvars, typval_T *rettv);
469static void f_values(typval_T *argvars, typval_T *rettv);
470static void f_virtcol(typval_T *argvars, typval_T *rettv);
471static void f_visualmode(typval_T *argvars, typval_T *rettv);
472static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
473static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
474static void f_win_getid(typval_T *argvars, typval_T *rettv);
475static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
476static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
477static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100478static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200479static void f_winbufnr(typval_T *argvars, typval_T *rettv);
480static void f_wincol(typval_T *argvars, typval_T *rettv);
481static void f_winheight(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200482static void f_winlayout(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200483static void f_winline(typval_T *argvars, typval_T *rettv);
484static void f_winnr(typval_T *argvars, typval_T *rettv);
485static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
486static void f_winrestview(typval_T *argvars, typval_T *rettv);
487static void f_winsaveview(typval_T *argvars, typval_T *rettv);
488static void f_winwidth(typval_T *argvars, typval_T *rettv);
489static void f_writefile(typval_T *argvars, typval_T *rettv);
490static void f_wordcount(typval_T *argvars, typval_T *rettv);
491static void f_xor(typval_T *argvars, typval_T *rettv);
492
493/*
494 * Array with names and number of arguments of all internal functions
495 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
496 */
497static struct fst
498{
499 char *f_name; /* function name */
500 char f_min_argc; /* minimal number of arguments */
501 char f_max_argc; /* maximal number of arguments */
502 void (*f_func)(typval_T *args, typval_T *rvar);
503 /* implementation of function */
504} functions[] =
505{
506#ifdef FEAT_FLOAT
507 {"abs", 1, 1, f_abs},
508 {"acos", 1, 1, f_acos}, /* WJMc */
509#endif
510 {"add", 2, 2, f_add},
511 {"and", 2, 2, f_and},
512 {"append", 2, 2, f_append},
Bram Moolenaarca851592018-06-06 21:04:07 +0200513 {"appendbufline", 3, 3, f_appendbufline},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200514 {"argc", 0, 1, f_argc},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200515 {"argidx", 0, 0, f_argidx},
516 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200517 {"argv", 0, 2, f_argv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200518#ifdef FEAT_FLOAT
519 {"asin", 1, 1, f_asin}, /* WJMc */
520#endif
Bram Moolenaarb48e96f2018-02-13 12:26:14 +0100521 {"assert_beeps", 1, 2, f_assert_beeps},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200522 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaard96ff162018-02-18 22:13:29 +0100523 {"assert_equalfile", 2, 2, f_assert_equalfile},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200524 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaar1307d1c2018-10-07 20:16:49 +0200525 {"assert_fails", 1, 3, f_assert_fails},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200526 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100527 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200528 {"assert_match", 2, 3, f_assert_match},
529 {"assert_notequal", 2, 3, f_assert_notequal},
530 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100531 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200532 {"assert_true", 1, 2, f_assert_true},
533#ifdef FEAT_FLOAT
534 {"atan", 1, 1, f_atan},
535 {"atan2", 2, 2, f_atan2},
536#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100537#ifdef FEAT_BEVAL
538 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100539# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100540 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100541# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100542#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200543 {"browse", 4, 4, f_browse},
544 {"browsedir", 2, 2, f_browsedir},
545 {"bufexists", 1, 1, f_bufexists},
546 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
547 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
548 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
549 {"buflisted", 1, 1, f_buflisted},
550 {"bufloaded", 1, 1, f_bufloaded},
551 {"bufname", 1, 1, f_bufname},
552 {"bufnr", 1, 2, f_bufnr},
553 {"bufwinid", 1, 1, f_bufwinid},
554 {"bufwinnr", 1, 1, f_bufwinnr},
555 {"byte2line", 1, 1, f_byte2line},
556 {"byteidx", 2, 2, f_byteidx},
557 {"byteidxcomp", 2, 2, f_byteidxcomp},
558 {"call", 2, 3, f_call},
559#ifdef FEAT_FLOAT
560 {"ceil", 1, 1, f_ceil},
561#endif
562#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100563 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200564 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200565 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200566 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
567 {"ch_evalraw", 2, 3, f_ch_evalraw},
568 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
569 {"ch_getjob", 1, 1, f_ch_getjob},
570 {"ch_info", 1, 1, f_ch_info},
571 {"ch_log", 1, 2, f_ch_log},
572 {"ch_logfile", 1, 2, f_ch_logfile},
573 {"ch_open", 1, 2, f_ch_open},
574 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100575 {"ch_readblob", 1, 2, f_ch_readblob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200576 {"ch_readraw", 1, 2, f_ch_readraw},
577 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
578 {"ch_sendraw", 2, 3, f_ch_sendraw},
579 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200580 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200581#endif
582 {"changenr", 0, 0, f_changenr},
583 {"char2nr", 1, 2, f_char2nr},
584 {"cindent", 1, 1, f_cindent},
585 {"clearmatches", 0, 0, f_clearmatches},
586 {"col", 1, 1, f_col},
587#if defined(FEAT_INS_EXPAND)
588 {"complete", 2, 2, f_complete},
589 {"complete_add", 1, 1, f_complete_add},
590 {"complete_check", 0, 0, f_complete_check},
591#endif
592 {"confirm", 1, 4, f_confirm},
593 {"copy", 1, 1, f_copy},
594#ifdef FEAT_FLOAT
595 {"cos", 1, 1, f_cos},
596 {"cosh", 1, 1, f_cosh},
597#endif
598 {"count", 2, 4, f_count},
599 {"cscope_connection",0,3, f_cscope_connection},
600 {"cursor", 1, 3, f_cursor},
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200601#ifdef WIN3264
602 {"debugbreak", 1, 1, f_debugbreak},
603#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200604 {"deepcopy", 1, 2, f_deepcopy},
605 {"delete", 1, 2, f_delete},
Bram Moolenaard79a2622018-06-07 18:17:46 +0200606 {"deletebufline", 2, 3, f_deletebufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200607 {"did_filetype", 0, 0, f_did_filetype},
608 {"diff_filler", 1, 1, f_diff_filler},
609 {"diff_hlID", 2, 2, f_diff_hlID},
610 {"empty", 1, 1, f_empty},
611 {"escape", 2, 2, f_escape},
612 {"eval", 1, 1, f_eval},
613 {"eventhandler", 0, 0, f_eventhandler},
614 {"executable", 1, 1, f_executable},
615 {"execute", 1, 2, f_execute},
616 {"exepath", 1, 1, f_exepath},
617 {"exists", 1, 1, f_exists},
618#ifdef FEAT_FLOAT
619 {"exp", 1, 1, f_exp},
620#endif
621 {"expand", 1, 3, f_expand},
622 {"extend", 2, 3, f_extend},
623 {"feedkeys", 1, 2, f_feedkeys},
624 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
625 {"filereadable", 1, 1, f_filereadable},
626 {"filewritable", 1, 1, f_filewritable},
627 {"filter", 2, 2, f_filter},
628 {"finddir", 1, 3, f_finddir},
629 {"findfile", 1, 3, f_findfile},
630#ifdef FEAT_FLOAT
631 {"float2nr", 1, 1, f_float2nr},
632 {"floor", 1, 1, f_floor},
633 {"fmod", 2, 2, f_fmod},
634#endif
635 {"fnameescape", 1, 1, f_fnameescape},
636 {"fnamemodify", 2, 2, f_fnamemodify},
637 {"foldclosed", 1, 1, f_foldclosed},
638 {"foldclosedend", 1, 1, f_foldclosedend},
639 {"foldlevel", 1, 1, f_foldlevel},
640 {"foldtext", 0, 0, f_foldtext},
641 {"foldtextresult", 1, 1, f_foldtextresult},
642 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200643 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200644 {"function", 1, 3, f_function},
645 {"garbagecollect", 0, 1, f_garbagecollect},
646 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200647 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200648 {"getbufline", 2, 3, f_getbufline},
649 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100650 {"getchangelist", 1, 1, f_getchangelist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200651 {"getchar", 0, 1, f_getchar},
652 {"getcharmod", 0, 0, f_getcharmod},
653 {"getcharsearch", 0, 0, f_getcharsearch},
654 {"getcmdline", 0, 0, f_getcmdline},
655 {"getcmdpos", 0, 0, f_getcmdpos},
656 {"getcmdtype", 0, 0, f_getcmdtype},
657 {"getcmdwintype", 0, 0, f_getcmdwintype},
658#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200659 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200660#endif
661 {"getcurpos", 0, 0, f_getcurpos},
662 {"getcwd", 0, 2, f_getcwd},
663 {"getfontname", 0, 1, f_getfontname},
664 {"getfperm", 1, 1, f_getfperm},
665 {"getfsize", 1, 1, f_getfsize},
666 {"getftime", 1, 1, f_getftime},
667 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100668 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200669 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200670 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200671 {"getmatches", 0, 0, f_getmatches},
672 {"getpid", 0, 0, f_getpid},
673 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200674 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200675 {"getreg", 0, 3, f_getreg},
676 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200677 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200678 {"gettabvar", 2, 3, f_gettabvar},
679 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100680 {"gettagstack", 0, 1, f_gettagstack},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200681 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100682 {"getwinpos", 0, 1, f_getwinpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200683 {"getwinposx", 0, 0, f_getwinposx},
684 {"getwinposy", 0, 0, f_getwinposy},
685 {"getwinvar", 2, 3, f_getwinvar},
686 {"glob", 1, 4, f_glob},
687 {"glob2regpat", 1, 1, f_glob2regpat},
688 {"globpath", 2, 5, f_globpath},
689 {"has", 1, 1, f_has},
690 {"has_key", 2, 2, f_has_key},
691 {"haslocaldir", 0, 2, f_haslocaldir},
692 {"hasmapto", 1, 3, f_hasmapto},
693 {"highlightID", 1, 1, f_hlID}, /* obsolete */
694 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
695 {"histadd", 2, 2, f_histadd},
696 {"histdel", 1, 2, f_histdel},
697 {"histget", 1, 2, f_histget},
698 {"histnr", 1, 1, f_histnr},
699 {"hlID", 1, 1, f_hlID},
700 {"hlexists", 1, 1, f_hlexists},
701 {"hostname", 0, 0, f_hostname},
702 {"iconv", 3, 3, f_iconv},
703 {"indent", 1, 1, f_indent},
704 {"index", 2, 4, f_index},
705 {"input", 1, 3, f_input},
706 {"inputdialog", 1, 3, f_inputdialog},
707 {"inputlist", 1, 1, f_inputlist},
708 {"inputrestore", 0, 0, f_inputrestore},
709 {"inputsave", 0, 0, f_inputsave},
710 {"inputsecret", 1, 2, f_inputsecret},
711 {"insert", 2, 3, f_insert},
712 {"invert", 1, 1, f_invert},
713 {"isdirectory", 1, 1, f_isdirectory},
714 {"islocked", 1, 1, f_islocked},
715#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
716 {"isnan", 1, 1, f_isnan},
717#endif
718 {"items", 1, 1, f_items},
719#ifdef FEAT_JOB_CHANNEL
720 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200721 {"job_info", 0, 1, f_job_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200722 {"job_setoptions", 2, 2, f_job_setoptions},
723 {"job_start", 1, 2, f_job_start},
724 {"job_status", 1, 1, f_job_status},
725 {"job_stop", 1, 2, f_job_stop},
726#endif
727 {"join", 1, 2, f_join},
728 {"js_decode", 1, 1, f_js_decode},
729 {"js_encode", 1, 1, f_js_encode},
730 {"json_decode", 1, 1, f_json_decode},
731 {"json_encode", 1, 1, f_json_encode},
732 {"keys", 1, 1, f_keys},
733 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
734 {"len", 1, 1, f_len},
735 {"libcall", 3, 3, f_libcall},
736 {"libcallnr", 3, 3, f_libcallnr},
737 {"line", 1, 1, f_line},
738 {"line2byte", 1, 1, f_line2byte},
739 {"lispindent", 1, 1, f_lispindent},
740 {"localtime", 0, 0, f_localtime},
741#ifdef FEAT_FLOAT
742 {"log", 1, 1, f_log},
743 {"log10", 1, 1, f_log10},
744#endif
745#ifdef FEAT_LUA
746 {"luaeval", 1, 2, f_luaeval},
747#endif
748 {"map", 2, 2, f_map},
749 {"maparg", 1, 4, f_maparg},
750 {"mapcheck", 1, 3, f_mapcheck},
751 {"match", 2, 4, f_match},
752 {"matchadd", 2, 5, f_matchadd},
753 {"matchaddpos", 2, 5, f_matchaddpos},
754 {"matcharg", 1, 1, f_matcharg},
755 {"matchdelete", 1, 1, f_matchdelete},
756 {"matchend", 2, 4, f_matchend},
757 {"matchlist", 2, 4, f_matchlist},
758 {"matchstr", 2, 4, f_matchstr},
759 {"matchstrpos", 2, 4, f_matchstrpos},
760 {"max", 1, 1, f_max},
761 {"min", 1, 1, f_min},
762#ifdef vim_mkdir
763 {"mkdir", 1, 3, f_mkdir},
764#endif
765 {"mode", 0, 1, f_mode},
766#ifdef FEAT_MZSCHEME
767 {"mzeval", 1, 1, f_mzeval},
768#endif
769 {"nextnonblank", 1, 1, f_nextnonblank},
770 {"nr2char", 1, 2, f_nr2char},
771 {"or", 2, 2, f_or},
772 {"pathshorten", 1, 1, f_pathshorten},
773#ifdef FEAT_PERL
774 {"perleval", 1, 1, f_perleval},
775#endif
776#ifdef FEAT_FLOAT
777 {"pow", 2, 2, f_pow},
778#endif
779 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100780 {"printf", 1, 19, f_printf},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200781#ifdef FEAT_JOB_CHANNEL
782 {"prompt_setcallback", 2, 2, f_prompt_setcallback},
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200783 {"prompt_setinterrupt", 2, 2, f_prompt_setinterrupt},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200784 {"prompt_setprompt", 2, 2, f_prompt_setprompt},
785#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100786#ifdef FEAT_TEXT_PROP
787 {"prop_add", 3, 3, f_prop_add},
788 {"prop_clear", 1, 3, f_prop_clear},
789 {"prop_list", 1, 2, f_prop_list},
790 {"prop_remove", 2, 3, f_prop_remove},
791 {"prop_type_add", 2, 2, f_prop_type_add},
792 {"prop_type_change", 2, 2, f_prop_type_change},
793 {"prop_type_delete", 1, 2, f_prop_type_delete},
794 {"prop_type_get", 1, 2, f_prop_type_get},
795 {"prop_type_list", 0, 1, f_prop_type_list},
796#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200797 {"pumvisible", 0, 0, f_pumvisible},
798#ifdef FEAT_PYTHON3
799 {"py3eval", 1, 1, f_py3eval},
800#endif
801#ifdef FEAT_PYTHON
802 {"pyeval", 1, 1, f_pyeval},
803#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100804#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
805 {"pyxeval", 1, 1, f_pyxeval},
806#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200807 {"range", 1, 3, f_range},
808 {"readfile", 1, 3, f_readfile},
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200809 {"reg_executing", 0, 0, f_reg_executing},
810 {"reg_recording", 0, 0, f_reg_recording},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200811 {"reltime", 0, 2, f_reltime},
812#ifdef FEAT_FLOAT
813 {"reltimefloat", 1, 1, f_reltimefloat},
814#endif
815 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100816 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200817 {"remote_foreground", 1, 1, f_remote_foreground},
818 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100819 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200820 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100821 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200822 {"remove", 2, 3, f_remove},
823 {"rename", 2, 2, f_rename},
824 {"repeat", 2, 2, f_repeat},
825 {"resolve", 1, 1, f_resolve},
826 {"reverse", 1, 1, f_reverse},
827#ifdef FEAT_FLOAT
828 {"round", 1, 1, f_round},
829#endif
830 {"screenattr", 2, 2, f_screenattr},
831 {"screenchar", 2, 2, f_screenchar},
832 {"screencol", 0, 0, f_screencol},
833 {"screenrow", 0, 0, f_screenrow},
834 {"search", 1, 4, f_search},
835 {"searchdecl", 1, 3, f_searchdecl},
836 {"searchpair", 3, 7, f_searchpair},
837 {"searchpairpos", 3, 7, f_searchpairpos},
838 {"searchpos", 1, 4, f_searchpos},
839 {"server2client", 2, 2, f_server2client},
840 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200841 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200842 {"setbufvar", 3, 3, f_setbufvar},
843 {"setcharsearch", 1, 1, f_setcharsearch},
844 {"setcmdpos", 1, 1, f_setcmdpos},
845 {"setfperm", 2, 2, f_setfperm},
846 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200847 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200848 {"setmatches", 1, 1, f_setmatches},
849 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200850 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200851 {"setreg", 2, 3, f_setreg},
852 {"settabvar", 3, 3, f_settabvar},
853 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100854 {"settagstack", 2, 3, f_settagstack},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200855 {"setwinvar", 3, 3, f_setwinvar},
856#ifdef FEAT_CRYPT
857 {"sha256", 1, 1, f_sha256},
858#endif
859 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaarf9514162018-11-22 03:08:29 +0100860 {"shiftwidth", 0, 1, f_shiftwidth},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100861#ifdef FEAT_SIGNS
862 {"sign_define", 1, 2, f_sign_define},
863 {"sign_getdefined", 0, 1, f_sign_getdefined},
864 {"sign_getplaced", 0, 2, f_sign_getplaced},
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100865 {"sign_jump", 3, 3, f_sign_jump},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100866 {"sign_place", 4, 5, f_sign_place},
867 {"sign_undefine", 0, 1, f_sign_undefine},
868 {"sign_unplace", 1, 2, f_sign_unplace},
869#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200870 {"simplify", 1, 1, f_simplify},
871#ifdef FEAT_FLOAT
872 {"sin", 1, 1, f_sin},
873 {"sinh", 1, 1, f_sinh},
874#endif
875 {"sort", 1, 3, f_sort},
876 {"soundfold", 1, 1, f_soundfold},
877 {"spellbadword", 0, 1, f_spellbadword},
878 {"spellsuggest", 1, 3, f_spellsuggest},
879 {"split", 1, 3, f_split},
880#ifdef FEAT_FLOAT
881 {"sqrt", 1, 1, f_sqrt},
882 {"str2float", 1, 1, f_str2float},
883#endif
884 {"str2nr", 1, 2, f_str2nr},
885 {"strcharpart", 2, 3, f_strcharpart},
886 {"strchars", 1, 2, f_strchars},
887 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
888#ifdef HAVE_STRFTIME
889 {"strftime", 1, 2, f_strftime},
890#endif
891 {"strgetchar", 2, 2, f_strgetchar},
892 {"stridx", 2, 3, f_stridx},
893 {"string", 1, 1, f_string},
894 {"strlen", 1, 1, f_strlen},
895 {"strpart", 2, 3, f_strpart},
896 {"strridx", 2, 3, f_strridx},
897 {"strtrans", 1, 1, f_strtrans},
898 {"strwidth", 1, 1, f_strwidth},
899 {"submatch", 1, 2, f_submatch},
900 {"substitute", 4, 4, f_substitute},
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200901 {"swapinfo", 1, 1, f_swapinfo},
Bram Moolenaar110bd602018-09-16 18:46:59 +0200902 {"swapname", 1, 1, f_swapname},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200903 {"synID", 3, 3, f_synID},
904 {"synIDattr", 2, 3, f_synIDattr},
905 {"synIDtrans", 1, 1, f_synIDtrans},
906 {"synconcealed", 2, 2, f_synconcealed},
907 {"synstack", 2, 2, f_synstack},
908 {"system", 1, 2, f_system},
909 {"systemlist", 1, 2, f_systemlist},
910 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
911 {"tabpagenr", 0, 1, f_tabpagenr},
912 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
913 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100914 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200915#ifdef FEAT_FLOAT
916 {"tan", 1, 1, f_tan},
917 {"tanh", 1, 1, f_tanh},
918#endif
919 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200920#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100921 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
922 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100923 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200924 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200925# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
926 {"term_getansicolors", 1, 1, f_term_getansicolors},
927# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200928 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200929 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200930 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200931 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200932 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200933 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200934 {"term_getstatus", 1, 1, f_term_getstatus},
935 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200936 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200937 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200938 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200939 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200940# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
941 {"term_setansicolors", 2, 2, f_term_setansicolors},
942# endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100943 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100944 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaara42d3632018-04-14 17:05:38 +0200945 {"term_setsize", 3, 3, f_term_setsize},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200946 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200947 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200948#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200949 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
950 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200951 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200952 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100953 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100954 {"test_null_blob", 0, 0, f_test_null_blob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200955#ifdef FEAT_JOB_CHANNEL
956 {"test_null_channel", 0, 0, f_test_null_channel},
957#endif
958 {"test_null_dict", 0, 0, f_test_null_dict},
959#ifdef FEAT_JOB_CHANNEL
960 {"test_null_job", 0, 0, f_test_null_job},
961#endif
962 {"test_null_list", 0, 0, f_test_null_list},
963 {"test_null_partial", 0, 0, f_test_null_partial},
964 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200965 {"test_option_not_set", 1, 1, f_test_option_not_set},
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100966 {"test_override", 2, 2, f_test_override},
Bram Moolenaarab186732018-09-14 21:27:06 +0200967#ifdef FEAT_GUI
968 {"test_scrollbar", 3, 3, f_test_scrollbar},
969#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200970 {"test_settime", 1, 1, f_test_settime},
971#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200972 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200973 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200974 {"timer_start", 2, 3, f_timer_start},
975 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200976 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200977#endif
978 {"tolower", 1, 1, f_tolower},
979 {"toupper", 1, 1, f_toupper},
980 {"tr", 3, 3, f_tr},
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100981 {"trim", 1, 2, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200982#ifdef FEAT_FLOAT
983 {"trunc", 1, 1, f_trunc},
984#endif
985 {"type", 1, 1, f_type},
986 {"undofile", 1, 1, f_undofile},
987 {"undotree", 0, 0, f_undotree},
988 {"uniq", 1, 3, f_uniq},
989 {"values", 1, 1, f_values},
990 {"virtcol", 1, 1, f_virtcol},
991 {"visualmode", 0, 1, f_visualmode},
992 {"wildmenumode", 0, 0, f_wildmenumode},
993 {"win_findbuf", 1, 1, f_win_findbuf},
994 {"win_getid", 0, 2, f_win_getid},
995 {"win_gotoid", 1, 1, f_win_gotoid},
996 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
997 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100998 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200999 {"winbufnr", 1, 1, f_winbufnr},
1000 {"wincol", 0, 0, f_wincol},
1001 {"winheight", 1, 1, f_winheight},
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +02001002 {"winlayout", 0, 1, f_winlayout},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001003 {"winline", 0, 0, f_winline},
1004 {"winnr", 0, 1, f_winnr},
1005 {"winrestcmd", 0, 0, f_winrestcmd},
1006 {"winrestview", 1, 1, f_winrestview},
1007 {"winsaveview", 0, 0, f_winsaveview},
1008 {"winwidth", 1, 1, f_winwidth},
1009 {"wordcount", 0, 0, f_wordcount},
1010 {"writefile", 2, 3, f_writefile},
1011 {"xor", 2, 2, f_xor},
1012};
1013
1014#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
1015
1016/*
1017 * Function given to ExpandGeneric() to obtain the list of internal
1018 * or user defined function names.
1019 */
1020 char_u *
1021get_function_name(expand_T *xp, int idx)
1022{
1023 static int intidx = -1;
1024 char_u *name;
1025
1026 if (idx == 0)
1027 intidx = -1;
1028 if (intidx < 0)
1029 {
1030 name = get_user_func_name(xp, idx);
1031 if (name != NULL)
1032 return name;
1033 }
1034 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
1035 {
1036 STRCPY(IObuff, functions[intidx].f_name);
1037 STRCAT(IObuff, "(");
1038 if (functions[intidx].f_max_argc == 0)
1039 STRCAT(IObuff, ")");
1040 return IObuff;
1041 }
1042
1043 return NULL;
1044}
1045
1046/*
1047 * Function given to ExpandGeneric() to obtain the list of internal or
1048 * user defined variable or function names.
1049 */
1050 char_u *
1051get_expr_name(expand_T *xp, int idx)
1052{
1053 static int intidx = -1;
1054 char_u *name;
1055
1056 if (idx == 0)
1057 intidx = -1;
1058 if (intidx < 0)
1059 {
1060 name = get_function_name(xp, idx);
1061 if (name != NULL)
1062 return name;
1063 }
1064 return get_user_var_name(xp, ++intidx);
1065}
1066
1067#endif /* FEAT_CMDL_COMPL */
1068
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001069/*
1070 * Find internal function in table above.
1071 * Return index, or -1 if not found
1072 */
1073 int
1074find_internal_func(
1075 char_u *name) /* name of the function */
1076{
1077 int first = 0;
1078 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
1079 int cmp;
1080 int x;
1081
1082 /*
1083 * Find the function name in the table. Binary search.
1084 */
1085 while (first <= last)
1086 {
1087 x = first + ((unsigned)(last - first) >> 1);
1088 cmp = STRCMP(name, functions[x].f_name);
1089 if (cmp < 0)
1090 last = x - 1;
1091 else if (cmp > 0)
1092 first = x + 1;
1093 else
1094 return x;
1095 }
1096 return -1;
1097}
1098
1099 int
1100call_internal_func(
1101 char_u *name,
1102 int argcount,
1103 typval_T *argvars,
1104 typval_T *rettv)
1105{
1106 int i;
1107
1108 i = find_internal_func(name);
1109 if (i < 0)
1110 return ERROR_UNKNOWN;
1111 if (argcount < functions[i].f_min_argc)
1112 return ERROR_TOOFEW;
1113 if (argcount > functions[i].f_max_argc)
1114 return ERROR_TOOMANY;
1115 argvars[argcount].v_type = VAR_UNKNOWN;
1116 functions[i].f_func(argvars, rettv);
1117 return ERROR_NONE;
1118}
1119
1120/*
1121 * Return TRUE for a non-zero Number and a non-empty String.
1122 */
1123 static int
1124non_zero_arg(typval_T *argvars)
1125{
1126 return ((argvars[0].v_type == VAR_NUMBER
1127 && argvars[0].vval.v_number != 0)
1128 || (argvars[0].v_type == VAR_SPECIAL
1129 && argvars[0].vval.v_number == VVAL_TRUE)
1130 || (argvars[0].v_type == VAR_STRING
1131 && argvars[0].vval.v_string != NULL
1132 && *argvars[0].vval.v_string != NUL));
1133}
1134
1135/*
1136 * Get the lnum from the first argument.
1137 * Also accepts ".", "$", etc., but that only works for the current buffer.
1138 * Returns -1 on error.
1139 */
1140 static linenr_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001141tv_get_lnum(typval_T *argvars)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001142{
1143 typval_T rettv;
1144 linenr_T lnum;
1145
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001146 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001147 if (lnum == 0) /* no valid number, try using line() */
1148 {
1149 rettv.v_type = VAR_NUMBER;
1150 f_line(argvars, &rettv);
1151 lnum = (linenr_T)rettv.vval.v_number;
1152 clear_tv(&rettv);
1153 }
1154 return lnum;
1155}
1156
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001157/*
1158 * Get the lnum from the first argument.
1159 * Also accepts "$", then "buf" is used.
1160 * Returns 0 on error.
1161 */
1162 static linenr_T
1163tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
1164{
1165 if (argvars[0].v_type == VAR_STRING
1166 && argvars[0].vval.v_string != NULL
1167 && argvars[0].vval.v_string[0] == '$'
1168 && buf != NULL)
1169 return buf->b_ml.ml_line_count;
1170 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1171}
1172
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001173#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001174/*
1175 * Get the float value of "argvars[0]" into "f".
1176 * Returns FAIL when the argument is not a Number or Float.
1177 */
1178 static int
1179get_float_arg(typval_T *argvars, float_T *f)
1180{
1181 if (argvars[0].v_type == VAR_FLOAT)
1182 {
1183 *f = argvars[0].vval.v_float;
1184 return OK;
1185 }
1186 if (argvars[0].v_type == VAR_NUMBER)
1187 {
1188 *f = (float_T)argvars[0].vval.v_number;
1189 return OK;
1190 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001191 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001192 return FAIL;
1193}
1194
1195/*
1196 * "abs(expr)" function
1197 */
1198 static void
1199f_abs(typval_T *argvars, typval_T *rettv)
1200{
1201 if (argvars[0].v_type == VAR_FLOAT)
1202 {
1203 rettv->v_type = VAR_FLOAT;
1204 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1205 }
1206 else
1207 {
1208 varnumber_T n;
1209 int error = FALSE;
1210
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001211 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001212 if (error)
1213 rettv->vval.v_number = -1;
1214 else if (n > 0)
1215 rettv->vval.v_number = n;
1216 else
1217 rettv->vval.v_number = -n;
1218 }
1219}
1220
1221/*
1222 * "acos()" function
1223 */
1224 static void
1225f_acos(typval_T *argvars, typval_T *rettv)
1226{
1227 float_T f = 0.0;
1228
1229 rettv->v_type = VAR_FLOAT;
1230 if (get_float_arg(argvars, &f) == OK)
1231 rettv->vval.v_float = acos(f);
1232 else
1233 rettv->vval.v_float = 0.0;
1234}
1235#endif
1236
1237/*
1238 * "add(list, item)" function
1239 */
1240 static void
1241f_add(typval_T *argvars, typval_T *rettv)
1242{
1243 list_T *l;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001244 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001245
1246 rettv->vval.v_number = 1; /* Default: Failed */
1247 if (argvars[0].v_type == VAR_LIST)
1248 {
1249 if ((l = argvars[0].vval.v_list) != NULL
1250 && !tv_check_lock(l->lv_lock,
1251 (char_u *)N_("add() argument"), TRUE)
1252 && list_append_tv(l, &argvars[1]) == OK)
1253 copy_tv(&argvars[0], rettv);
1254 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001255 else if (argvars[0].v_type == VAR_BLOB)
1256 {
1257 if ((b = argvars[0].vval.v_blob) != NULL
1258 && !tv_check_lock(b->bv_lock,
1259 (char_u *)N_("add() argument"), TRUE))
1260 {
Bram Moolenaar05500ec2019-01-13 19:10:33 +01001261 int error = FALSE;
1262 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
1263
1264 if (!error)
1265 {
1266 ga_append(&b->bv_ga, (int)n);
1267 copy_tv(&argvars[0], rettv);
1268 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001269 }
1270 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001271 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001272 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001273}
1274
1275/*
1276 * "and(expr, expr)" function
1277 */
1278 static void
1279f_and(typval_T *argvars, typval_T *rettv)
1280{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001281 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1282 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001283}
1284
1285/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02001286 * If there is a window for "curbuf", make it the current window.
1287 */
1288 static void
1289find_win_for_curbuf(void)
1290{
1291 wininfo_T *wip;
1292
1293 for (wip = curbuf->b_wininfo; wip != NULL; wip = wip->wi_next)
1294 {
1295 if (wip->wi_win != NULL)
1296 {
1297 curwin = wip->wi_win;
1298 break;
1299 }
1300 }
1301}
1302
1303/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001304 * Set line or list of lines in buffer "buf".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001305 */
1306 static void
Bram Moolenaarca851592018-06-06 21:04:07 +02001307set_buffer_lines(
1308 buf_T *buf,
1309 linenr_T lnum_arg,
1310 int append,
1311 typval_T *lines,
1312 typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001313{
Bram Moolenaarca851592018-06-06 21:04:07 +02001314 linenr_T lnum = lnum_arg + (append ? 1 : 0);
1315 char_u *line = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001316 list_T *l = NULL;
1317 listitem_T *li = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001318 long added = 0;
Bram Moolenaarca851592018-06-06 21:04:07 +02001319 linenr_T append_lnum;
1320 buf_T *curbuf_save = NULL;
1321 win_T *curwin_save = NULL;
1322 int is_curbuf = buf == curbuf;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001323
Bram Moolenaarca851592018-06-06 21:04:07 +02001324 /* When using the current buffer ml_mfp will be set if needed. Useful when
1325 * setline() is used on startup. For other buffers the buffer must be
1326 * loaded. */
1327 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001328 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001329 rettv->vval.v_number = 1; /* FAIL */
1330 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001331 }
1332
Bram Moolenaarca851592018-06-06 21:04:07 +02001333 if (!is_curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001334 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001335 curbuf_save = curbuf;
1336 curwin_save = curwin;
1337 curbuf = buf;
Bram Moolenaard79a2622018-06-07 18:17:46 +02001338 find_win_for_curbuf();
Bram Moolenaarca851592018-06-06 21:04:07 +02001339 }
1340
1341 if (append)
1342 // appendbufline() uses the line number below which we insert
1343 append_lnum = lnum - 1;
1344 else
1345 // setbufline() uses the line number above which we insert, we only
1346 // append if it's below the last line
1347 append_lnum = curbuf->b_ml.ml_line_count;
1348
1349 if (lines->v_type == VAR_LIST)
1350 {
1351 l = lines->vval.v_list;
1352 li = l->lv_first;
1353 }
1354 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001355 line = tv_get_string_chk(lines);
Bram Moolenaarca851592018-06-06 21:04:07 +02001356
1357 /* default result is zero == OK */
1358 for (;;)
1359 {
1360 if (l != NULL)
1361 {
1362 /* list argument, get next string */
1363 if (li == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001364 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001365 line = tv_get_string_chk(&li->li_tv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001366 li = li->li_next;
1367 }
1368
Bram Moolenaarca851592018-06-06 21:04:07 +02001369 rettv->vval.v_number = 1; /* FAIL */
1370 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
1371 break;
1372
1373 /* When coming here from Insert mode, sync undo, so that this can be
1374 * undone separately from what was previously inserted. */
1375 if (u_sync_once == 2)
1376 {
1377 u_sync_once = 1; /* notify that u_sync() was called */
1378 u_sync(TRUE);
1379 }
1380
1381 if (!append && lnum <= curbuf->b_ml.ml_line_count)
1382 {
Bram Moolenaar21b50382019-01-04 18:07:24 +01001383 // Existing line, replace it.
1384 // Removes any existing text properties.
1385 if (u_savesub(lnum) == OK && ml_replace_len(
1386 lnum, line, (colnr_T)STRLEN(line) + 1, TRUE, TRUE) == OK)
Bram Moolenaarca851592018-06-06 21:04:07 +02001387 {
1388 changed_bytes(lnum, 0);
1389 if (is_curbuf && lnum == curwin->w_cursor.lnum)
1390 check_cursor_col();
1391 rettv->vval.v_number = 0; /* OK */
1392 }
1393 }
1394 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
1395 {
1396 /* append the line */
1397 ++added;
1398 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
1399 rettv->vval.v_number = 0; /* OK */
1400 }
1401
1402 if (l == NULL) /* only one string argument */
1403 break;
1404 ++lnum;
1405 }
1406
1407 if (added > 0)
1408 {
1409 win_T *wp;
1410 tabpage_T *tp;
1411
1412 appended_lines_mark(append_lnum, added);
1413 FOR_ALL_TAB_WINDOWS(tp, wp)
1414 if (wp->w_buffer == buf && wp->w_cursor.lnum > append_lnum)
1415 wp->w_cursor.lnum += added;
1416 check_cursor_col();
1417
Bram Moolenaarf2732452018-06-03 14:47:35 +02001418#ifdef FEAT_JOB_CHANNEL
1419 if (bt_prompt(curbuf) && (State & INSERT))
1420 // show the line with the prompt
1421 update_topline();
1422#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001423 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001424
1425 if (!is_curbuf)
1426 {
1427 curbuf = curbuf_save;
1428 curwin = curwin_save;
1429 }
1430}
1431
1432/*
1433 * "append(lnum, string/list)" function
1434 */
1435 static void
1436f_append(typval_T *argvars, typval_T *rettv)
1437{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001438 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaarca851592018-06-06 21:04:07 +02001439
1440 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1441}
1442
1443/*
1444 * "appendbufline(buf, lnum, string/list)" function
1445 */
1446 static void
1447f_appendbufline(typval_T *argvars, typval_T *rettv)
1448{
1449 linenr_T lnum;
1450 buf_T *buf;
1451
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001452 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarca851592018-06-06 21:04:07 +02001453 if (buf == NULL)
1454 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001455 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001456 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001457 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +02001458 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1459 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001460}
1461
1462/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001463 * "argc([window id])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001464 */
1465 static void
Bram Moolenaare6e39892018-10-25 12:32:11 +02001466f_argc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001467{
Bram Moolenaare6e39892018-10-25 12:32:11 +02001468 win_T *wp;
1469
1470 if (argvars[0].v_type == VAR_UNKNOWN)
1471 // use the current window
1472 rettv->vval.v_number = ARGCOUNT;
1473 else if (argvars[0].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001474 && tv_get_number(&argvars[0]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001475 // use the global argument list
1476 rettv->vval.v_number = GARGCOUNT;
1477 else
1478 {
1479 // use the argument list of the specified window
1480 wp = find_win_by_nr_or_id(&argvars[0]);
1481 if (wp != NULL)
1482 rettv->vval.v_number = WARGCOUNT(wp);
1483 else
1484 rettv->vval.v_number = -1;
1485 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001486}
1487
1488/*
1489 * "argidx()" function
1490 */
1491 static void
1492f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1493{
1494 rettv->vval.v_number = curwin->w_arg_idx;
1495}
1496
1497/*
1498 * "arglistid()" function
1499 */
1500 static void
1501f_arglistid(typval_T *argvars, typval_T *rettv)
1502{
1503 win_T *wp;
1504
1505 rettv->vval.v_number = -1;
1506 wp = find_tabwin(&argvars[0], &argvars[1]);
1507 if (wp != NULL)
1508 rettv->vval.v_number = wp->w_alist->id;
1509}
1510
1511/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001512 * Get the argument list for a given window
1513 */
1514 static void
1515get_arglist_as_rettv(aentry_T *arglist, int argcount, typval_T *rettv)
1516{
1517 int idx;
1518
1519 if (rettv_list_alloc(rettv) == OK && arglist != NULL)
1520 for (idx = 0; idx < argcount; ++idx)
1521 list_append_string(rettv->vval.v_list,
1522 alist_name(&arglist[idx]), -1);
1523}
1524
1525/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001526 * "argv(nr)" function
1527 */
1528 static void
1529f_argv(typval_T *argvars, typval_T *rettv)
1530{
1531 int idx;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001532 aentry_T *arglist = NULL;
1533 int argcount = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001534
1535 if (argvars[0].v_type != VAR_UNKNOWN)
1536 {
Bram Moolenaare6e39892018-10-25 12:32:11 +02001537 if (argvars[1].v_type == VAR_UNKNOWN)
1538 {
1539 arglist = ARGLIST;
1540 argcount = ARGCOUNT;
1541 }
1542 else if (argvars[1].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001543 && tv_get_number(&argvars[1]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001544 {
1545 arglist = GARGLIST;
1546 argcount = GARGCOUNT;
1547 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001548 else
Bram Moolenaare6e39892018-10-25 12:32:11 +02001549 {
1550 win_T *wp = find_win_by_nr_or_id(&argvars[1]);
1551
1552 if (wp != NULL)
1553 {
1554 /* Use the argument list of the specified window */
1555 arglist = WARGLIST(wp);
1556 argcount = WARGCOUNT(wp);
1557 }
1558 }
1559
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001560 rettv->v_type = VAR_STRING;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001561 rettv->vval.v_string = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001562 idx = tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02001563 if (arglist != NULL && idx >= 0 && idx < argcount)
1564 rettv->vval.v_string = vim_strsave(alist_name(&arglist[idx]));
1565 else if (idx == -1)
1566 get_arglist_as_rettv(arglist, argcount, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001567 }
Bram Moolenaare6e39892018-10-25 12:32:11 +02001568 else
1569 get_arglist_as_rettv(ARGLIST, ARGCOUNT, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001570}
1571
1572/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001573 * "assert_beeps(cmd [, error])" function
1574 */
1575 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001576f_assert_beeps(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001577{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001578 rettv->vval.v_number = assert_beeps(argvars);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001579}
1580
1581/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001582 * "assert_equal(expected, actual[, msg])" function
1583 */
1584 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001585f_assert_equal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001586{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001587 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_EQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001588}
1589
1590/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01001591 * "assert_equalfile(fname-one, fname-two)" function
1592 */
1593 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001594f_assert_equalfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaard96ff162018-02-18 22:13:29 +01001595{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001596 rettv->vval.v_number = assert_equalfile(argvars);
Bram Moolenaard96ff162018-02-18 22:13:29 +01001597}
1598
1599/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001600 * "assert_notequal(expected, actual[, msg])" function
1601 */
1602 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001603f_assert_notequal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001604{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001605 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001606}
1607
1608/*
1609 * "assert_exception(string[, msg])" function
1610 */
1611 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001612f_assert_exception(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001613{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001614 rettv->vval.v_number = assert_exception(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001615}
1616
1617/*
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02001618 * "assert_fails(cmd [, error[, msg]])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001619 */
1620 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001621f_assert_fails(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001622{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001623 rettv->vval.v_number = assert_fails(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001624}
1625
1626/*
1627 * "assert_false(actual[, msg])" function
1628 */
1629 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001630f_assert_false(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001631{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001632 rettv->vval.v_number = assert_bool(argvars, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001633}
1634
1635/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001636 * "assert_inrange(lower, upper[, msg])" function
1637 */
1638 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001639f_assert_inrange(typval_T *argvars, typval_T *rettv)
Bram Moolenaar61c04492016-07-23 15:35:35 +02001640{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001641 rettv->vval.v_number = assert_inrange(argvars);
Bram Moolenaar61c04492016-07-23 15:35:35 +02001642}
1643
1644/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001645 * "assert_match(pattern, actual[, msg])" function
1646 */
1647 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001648f_assert_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001649{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001650 rettv->vval.v_number = assert_match_common(argvars, ASSERT_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001651}
1652
1653/*
1654 * "assert_notmatch(pattern, actual[, msg])" function
1655 */
1656 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001657f_assert_notmatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001658{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001659 rettv->vval.v_number = assert_match_common(argvars, ASSERT_NOTMATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001660}
1661
1662/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001663 * "assert_report(msg)" function
1664 */
1665 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001666f_assert_report(typval_T *argvars, typval_T *rettv)
Bram Moolenaar42205552017-03-18 19:42:22 +01001667{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001668 rettv->vval.v_number = assert_report(argvars);
Bram Moolenaar42205552017-03-18 19:42:22 +01001669}
1670
1671/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001672 * "assert_true(actual[, msg])" function
1673 */
1674 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001675f_assert_true(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001676{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001677 rettv->vval.v_number = assert_bool(argvars, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001678}
1679
1680#ifdef FEAT_FLOAT
1681/*
1682 * "asin()" function
1683 */
1684 static void
1685f_asin(typval_T *argvars, typval_T *rettv)
1686{
1687 float_T f = 0.0;
1688
1689 rettv->v_type = VAR_FLOAT;
1690 if (get_float_arg(argvars, &f) == OK)
1691 rettv->vval.v_float = asin(f);
1692 else
1693 rettv->vval.v_float = 0.0;
1694}
1695
1696/*
1697 * "atan()" function
1698 */
1699 static void
1700f_atan(typval_T *argvars, typval_T *rettv)
1701{
1702 float_T f = 0.0;
1703
1704 rettv->v_type = VAR_FLOAT;
1705 if (get_float_arg(argvars, &f) == OK)
1706 rettv->vval.v_float = atan(f);
1707 else
1708 rettv->vval.v_float = 0.0;
1709}
1710
1711/*
1712 * "atan2()" function
1713 */
1714 static void
1715f_atan2(typval_T *argvars, typval_T *rettv)
1716{
1717 float_T fx = 0.0, fy = 0.0;
1718
1719 rettv->v_type = VAR_FLOAT;
1720 if (get_float_arg(argvars, &fx) == OK
1721 && get_float_arg(&argvars[1], &fy) == OK)
1722 rettv->vval.v_float = atan2(fx, fy);
1723 else
1724 rettv->vval.v_float = 0.0;
1725}
1726#endif
1727
1728/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001729 * "balloon_show()" function
1730 */
1731#ifdef FEAT_BEVAL
1732 static void
1733f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1734{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001735 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001736 {
1737 if (argvars[0].v_type == VAR_LIST
1738# ifdef FEAT_GUI
1739 && !gui.in_use
1740# endif
1741 )
1742 post_balloon(balloonEval, NULL, argvars[0].vval.v_list);
1743 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001744 post_balloon(balloonEval, tv_get_string_chk(&argvars[0]), NULL);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001745 }
1746}
1747
Bram Moolenaar669a8282017-11-19 20:13:05 +01001748# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001749 static void
1750f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1751{
1752 if (rettv_list_alloc(rettv) == OK)
1753 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001754 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001755
1756 if (msg != NULL)
1757 {
1758 pumitem_T *array;
1759 int size = split_message(msg, &array);
1760 int i;
1761
1762 /* Skip the first and last item, they are always empty. */
1763 for (i = 1; i < size - 1; ++i)
1764 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001765 while (size > 0)
1766 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001767 vim_free(array);
1768 }
1769 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001770}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001771# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001772#endif
1773
1774/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001775 * "browse(save, title, initdir, default)" function
1776 */
1777 static void
1778f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1779{
1780#ifdef FEAT_BROWSE
1781 int save;
1782 char_u *title;
1783 char_u *initdir;
1784 char_u *defname;
1785 char_u buf[NUMBUFLEN];
1786 char_u buf2[NUMBUFLEN];
1787 int error = FALSE;
1788
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001789 save = (int)tv_get_number_chk(&argvars[0], &error);
1790 title = tv_get_string_chk(&argvars[1]);
1791 initdir = tv_get_string_buf_chk(&argvars[2], buf);
1792 defname = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001793
1794 if (error || title == NULL || initdir == NULL || defname == NULL)
1795 rettv->vval.v_string = NULL;
1796 else
1797 rettv->vval.v_string =
1798 do_browse(save ? BROWSE_SAVE : 0,
1799 title, defname, NULL, initdir, NULL, curbuf);
1800#else
1801 rettv->vval.v_string = NULL;
1802#endif
1803 rettv->v_type = VAR_STRING;
1804}
1805
1806/*
1807 * "browsedir(title, initdir)" function
1808 */
1809 static void
1810f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1811{
1812#ifdef FEAT_BROWSE
1813 char_u *title;
1814 char_u *initdir;
1815 char_u buf[NUMBUFLEN];
1816
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001817 title = tv_get_string_chk(&argvars[0]);
1818 initdir = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001819
1820 if (title == NULL || initdir == NULL)
1821 rettv->vval.v_string = NULL;
1822 else
1823 rettv->vval.v_string = do_browse(BROWSE_DIR,
1824 title, NULL, NULL, initdir, NULL, curbuf);
1825#else
1826 rettv->vval.v_string = NULL;
1827#endif
1828 rettv->v_type = VAR_STRING;
1829}
1830
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001831/*
1832 * Find a buffer by number or exact name.
1833 */
1834 static buf_T *
1835find_buffer(typval_T *avar)
1836{
1837 buf_T *buf = NULL;
1838
1839 if (avar->v_type == VAR_NUMBER)
1840 buf = buflist_findnr((int)avar->vval.v_number);
1841 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1842 {
1843 buf = buflist_findname_exp(avar->vval.v_string);
1844 if (buf == NULL)
1845 {
1846 /* No full path name match, try a match with a URL or a "nofile"
1847 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001848 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001849 if (buf->b_fname != NULL
1850 && (path_with_url(buf->b_fname)
1851#ifdef FEAT_QUICKFIX
1852 || bt_nofile(buf)
1853#endif
1854 )
1855 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1856 break;
1857 }
1858 }
1859 return buf;
1860}
1861
1862/*
1863 * "bufexists(expr)" function
1864 */
1865 static void
1866f_bufexists(typval_T *argvars, typval_T *rettv)
1867{
1868 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1869}
1870
1871/*
1872 * "buflisted(expr)" function
1873 */
1874 static void
1875f_buflisted(typval_T *argvars, typval_T *rettv)
1876{
1877 buf_T *buf;
1878
1879 buf = find_buffer(&argvars[0]);
1880 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1881}
1882
1883/*
1884 * "bufloaded(expr)" function
1885 */
1886 static void
1887f_bufloaded(typval_T *argvars, typval_T *rettv)
1888{
1889 buf_T *buf;
1890
1891 buf = find_buffer(&argvars[0]);
1892 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1893}
1894
1895 buf_T *
1896buflist_find_by_name(char_u *name, int curtab_only)
1897{
1898 int save_magic;
1899 char_u *save_cpo;
1900 buf_T *buf;
1901
1902 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1903 save_magic = p_magic;
1904 p_magic = TRUE;
1905 save_cpo = p_cpo;
1906 p_cpo = (char_u *)"";
1907
1908 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1909 TRUE, FALSE, curtab_only));
1910
1911 p_magic = save_magic;
1912 p_cpo = save_cpo;
1913 return buf;
1914}
1915
1916/*
1917 * Get buffer by number or pattern.
1918 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001919 buf_T *
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001920tv_get_buf(typval_T *tv, int curtab_only)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001921{
1922 char_u *name = tv->vval.v_string;
1923 buf_T *buf;
1924
1925 if (tv->v_type == VAR_NUMBER)
1926 return buflist_findnr((int)tv->vval.v_number);
1927 if (tv->v_type != VAR_STRING)
1928 return NULL;
1929 if (name == NULL || *name == NUL)
1930 return curbuf;
1931 if (name[0] == '$' && name[1] == NUL)
1932 return lastbuf;
1933
1934 buf = buflist_find_by_name(name, curtab_only);
1935
1936 /* If not found, try expanding the name, like done for bufexists(). */
1937 if (buf == NULL)
1938 buf = find_buffer(tv);
1939
1940 return buf;
1941}
1942
Bram Moolenaarec9d3002019-01-12 13:50:31 +01001943#ifdef FEAT_SIGNS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001944/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001945 * Get the buffer from "arg" and give an error and return NULL if it is not
1946 * valid.
1947 */
1948 static buf_T *
1949get_buf_arg(typval_T *arg)
1950{
1951 buf_T *buf;
1952
1953 ++emsg_off;
1954 buf = tv_get_buf(arg, FALSE);
1955 --emsg_off;
1956 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001957 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001958 return buf;
1959}
Bram Moolenaarec9d3002019-01-12 13:50:31 +01001960#endif
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001961
1962/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001963 * "bufname(expr)" function
1964 */
1965 static void
1966f_bufname(typval_T *argvars, typval_T *rettv)
1967{
1968 buf_T *buf;
1969
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001970 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001971 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001972 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001973 rettv->v_type = VAR_STRING;
1974 if (buf != NULL && buf->b_fname != NULL)
1975 rettv->vval.v_string = vim_strsave(buf->b_fname);
1976 else
1977 rettv->vval.v_string = NULL;
1978 --emsg_off;
1979}
1980
1981/*
1982 * "bufnr(expr)" function
1983 */
1984 static void
1985f_bufnr(typval_T *argvars, typval_T *rettv)
1986{
1987 buf_T *buf;
1988 int error = FALSE;
1989 char_u *name;
1990
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001991 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001992 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001993 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001994 --emsg_off;
1995
1996 /* If the buffer isn't found and the second argument is not zero create a
1997 * new buffer. */
1998 if (buf == NULL
1999 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002000 && tv_get_number_chk(&argvars[1], &error) != 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002001 && !error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002002 && (name = tv_get_string_chk(&argvars[0])) != NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002003 && !error)
2004 buf = buflist_new(name, NULL, (linenr_T)1, 0);
2005
2006 if (buf != NULL)
2007 rettv->vval.v_number = buf->b_fnum;
2008 else
2009 rettv->vval.v_number = -1;
2010}
2011
2012 static void
2013buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
2014{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002015 win_T *wp;
2016 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002017 buf_T *buf;
2018
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002019 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002020 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002021 buf = tv_get_buf(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02002022 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002023 {
2024 ++winnr;
2025 if (wp->w_buffer == buf)
2026 break;
2027 }
2028 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002029 --emsg_off;
2030}
2031
2032/*
2033 * "bufwinid(nr)" function
2034 */
2035 static void
2036f_bufwinid(typval_T *argvars, typval_T *rettv)
2037{
2038 buf_win_common(argvars, rettv, FALSE);
2039}
2040
2041/*
2042 * "bufwinnr(nr)" function
2043 */
2044 static void
2045f_bufwinnr(typval_T *argvars, typval_T *rettv)
2046{
2047 buf_win_common(argvars, rettv, TRUE);
2048}
2049
2050/*
2051 * "byte2line(byte)" function
2052 */
2053 static void
2054f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2055{
2056#ifndef FEAT_BYTEOFF
2057 rettv->vval.v_number = -1;
2058#else
2059 long boff = 0;
2060
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002061 boff = tv_get_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002062 if (boff < 0)
2063 rettv->vval.v_number = -1;
2064 else
2065 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2066 (linenr_T)0, &boff);
2067#endif
2068}
2069
2070 static void
2071byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2072{
2073#ifdef FEAT_MBYTE
2074 char_u *t;
2075#endif
2076 char_u *str;
2077 varnumber_T idx;
2078
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002079 str = tv_get_string_chk(&argvars[0]);
2080 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002081 rettv->vval.v_number = -1;
2082 if (str == NULL || idx < 0)
2083 return;
2084
2085#ifdef FEAT_MBYTE
2086 t = str;
2087 for ( ; idx > 0; idx--)
2088 {
2089 if (*t == NUL) /* EOL reached */
2090 return;
2091 if (enc_utf8 && comp)
2092 t += utf_ptr2len(t);
2093 else
2094 t += (*mb_ptr2len)(t);
2095 }
2096 rettv->vval.v_number = (varnumber_T)(t - str);
2097#else
2098 if ((size_t)idx <= STRLEN(str))
2099 rettv->vval.v_number = idx;
2100#endif
2101}
2102
2103/*
2104 * "byteidx()" function
2105 */
2106 static void
2107f_byteidx(typval_T *argvars, typval_T *rettv)
2108{
2109 byteidx(argvars, rettv, FALSE);
2110}
2111
2112/*
2113 * "byteidxcomp()" function
2114 */
2115 static void
2116f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2117{
2118 byteidx(argvars, rettv, TRUE);
2119}
2120
2121/*
2122 * "call(func, arglist [, dict])" function
2123 */
2124 static void
2125f_call(typval_T *argvars, typval_T *rettv)
2126{
2127 char_u *func;
2128 partial_T *partial = NULL;
2129 dict_T *selfdict = NULL;
2130
2131 if (argvars[1].v_type != VAR_LIST)
2132 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002133 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002134 return;
2135 }
2136 if (argvars[1].vval.v_list == NULL)
2137 return;
2138
2139 if (argvars[0].v_type == VAR_FUNC)
2140 func = argvars[0].vval.v_string;
2141 else if (argvars[0].v_type == VAR_PARTIAL)
2142 {
2143 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002144 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002145 }
2146 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002147 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002148 if (*func == NUL)
2149 return; /* type error or empty name */
2150
2151 if (argvars[2].v_type != VAR_UNKNOWN)
2152 {
2153 if (argvars[2].v_type != VAR_DICT)
2154 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002155 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002156 return;
2157 }
2158 selfdict = argvars[2].vval.v_dict;
2159 }
2160
2161 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2162}
2163
2164#ifdef FEAT_FLOAT
2165/*
2166 * "ceil({float})" function
2167 */
2168 static void
2169f_ceil(typval_T *argvars, typval_T *rettv)
2170{
2171 float_T f = 0.0;
2172
2173 rettv->v_type = VAR_FLOAT;
2174 if (get_float_arg(argvars, &f) == OK)
2175 rettv->vval.v_float = ceil(f);
2176 else
2177 rettv->vval.v_float = 0.0;
2178}
2179#endif
2180
2181#ifdef FEAT_JOB_CHANNEL
2182/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002183 * "ch_canread()" function
2184 */
2185 static void
2186f_ch_canread(typval_T *argvars, typval_T *rettv)
2187{
Bram Moolenaar958dc692016-12-01 15:34:12 +01002188 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002189
2190 rettv->vval.v_number = 0;
2191 if (channel != NULL)
2192 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
2193 || channel_has_readahead(channel, PART_OUT)
2194 || channel_has_readahead(channel, PART_ERR);
2195}
2196
2197/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002198 * "ch_close()" function
2199 */
2200 static void
2201f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
2202{
2203 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2204
2205 if (channel != NULL)
2206 {
2207 channel_close(channel, FALSE);
2208 channel_clear(channel);
2209 }
2210}
2211
2212/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002213 * "ch_close()" function
2214 */
2215 static void
2216f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
2217{
2218 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2219
2220 if (channel != NULL)
2221 channel_close_in(channel);
2222}
2223
2224/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002225 * "ch_getbufnr()" function
2226 */
2227 static void
2228f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
2229{
2230 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2231
2232 rettv->vval.v_number = -1;
2233 if (channel != NULL)
2234 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002235 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002236 int part;
2237
2238 if (STRCMP(what, "err") == 0)
2239 part = PART_ERR;
2240 else if (STRCMP(what, "out") == 0)
2241 part = PART_OUT;
2242 else if (STRCMP(what, "in") == 0)
2243 part = PART_IN;
2244 else
2245 part = PART_SOCK;
2246 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
2247 rettv->vval.v_number =
2248 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
2249 }
2250}
2251
2252/*
2253 * "ch_getjob()" function
2254 */
2255 static void
2256f_ch_getjob(typval_T *argvars, typval_T *rettv)
2257{
2258 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2259
2260 if (channel != NULL)
2261 {
2262 rettv->v_type = VAR_JOB;
2263 rettv->vval.v_job = channel->ch_job;
2264 if (channel->ch_job != NULL)
2265 ++channel->ch_job->jv_refcount;
2266 }
2267}
2268
2269/*
2270 * "ch_info()" function
2271 */
2272 static void
2273f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
2274{
2275 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2276
2277 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
2278 channel_info(channel, rettv->vval.v_dict);
2279}
2280
2281/*
2282 * "ch_log()" function
2283 */
2284 static void
2285f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
2286{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002287 char_u *msg = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002288 channel_T *channel = NULL;
2289
2290 if (argvars[1].v_type != VAR_UNKNOWN)
2291 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
2292
Bram Moolenaard5359b22018-04-05 22:44:39 +02002293 ch_log(channel, "%s", msg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002294}
2295
2296/*
2297 * "ch_logfile()" function
2298 */
2299 static void
2300f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
2301{
2302 char_u *fname;
2303 char_u *opt = (char_u *)"";
2304 char_u buf[NUMBUFLEN];
2305
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02002306 /* Don't open a file in restricted mode. */
2307 if (check_restricted() || check_secure())
2308 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002309 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002310 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002311 opt = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002312 ch_logfile(fname, opt);
2313}
2314
2315/*
2316 * "ch_open()" function
2317 */
2318 static void
2319f_ch_open(typval_T *argvars, typval_T *rettv)
2320{
2321 rettv->v_type = VAR_CHANNEL;
2322 if (check_restricted() || check_secure())
2323 return;
2324 rettv->vval.v_channel = channel_open_func(argvars);
2325}
2326
2327/*
2328 * "ch_read()" function
2329 */
2330 static void
2331f_ch_read(typval_T *argvars, typval_T *rettv)
2332{
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002333 common_channel_read(argvars, rettv, FALSE, FALSE);
2334}
2335
2336/*
2337 * "ch_readblob()" function
2338 */
2339 static void
2340f_ch_readblob(typval_T *argvars, typval_T *rettv)
2341{
2342 common_channel_read(argvars, rettv, TRUE, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002343}
2344
2345/*
2346 * "ch_readraw()" function
2347 */
2348 static void
2349f_ch_readraw(typval_T *argvars, typval_T *rettv)
2350{
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002351 common_channel_read(argvars, rettv, TRUE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002352}
2353
2354/*
2355 * "ch_evalexpr()" function
2356 */
2357 static void
2358f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2359{
2360 ch_expr_common(argvars, rettv, TRUE);
2361}
2362
2363/*
2364 * "ch_sendexpr()" function
2365 */
2366 static void
2367f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2368{
2369 ch_expr_common(argvars, rettv, FALSE);
2370}
2371
2372/*
2373 * "ch_evalraw()" function
2374 */
2375 static void
2376f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2377{
2378 ch_raw_common(argvars, rettv, TRUE);
2379}
2380
2381/*
2382 * "ch_sendraw()" function
2383 */
2384 static void
2385f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2386{
2387 ch_raw_common(argvars, rettv, FALSE);
2388}
2389
2390/*
2391 * "ch_setoptions()" function
2392 */
2393 static void
2394f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2395{
2396 channel_T *channel;
2397 jobopt_T opt;
2398
2399 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2400 if (channel == NULL)
2401 return;
2402 clear_job_options(&opt);
2403 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002404 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002405 channel_set_options(channel, &opt);
2406 free_job_options(&opt);
2407}
2408
2409/*
2410 * "ch_status()" function
2411 */
2412 static void
2413f_ch_status(typval_T *argvars, typval_T *rettv)
2414{
2415 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002416 jobopt_T opt;
2417 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002418
2419 /* return an empty string by default */
2420 rettv->v_type = VAR_STRING;
2421 rettv->vval.v_string = NULL;
2422
2423 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002424
2425 if (argvars[1].v_type != VAR_UNKNOWN)
2426 {
2427 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002428 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002429 && (opt.jo_set & JO_PART))
2430 part = opt.jo_part;
2431 }
2432
2433 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002434}
2435#endif
2436
2437/*
2438 * "changenr()" function
2439 */
2440 static void
2441f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2442{
2443 rettv->vval.v_number = curbuf->b_u_seq_cur;
2444}
2445
2446/*
2447 * "char2nr(string)" function
2448 */
2449 static void
2450f_char2nr(typval_T *argvars, typval_T *rettv)
2451{
2452#ifdef FEAT_MBYTE
2453 if (has_mbyte)
2454 {
2455 int utf8 = 0;
2456
2457 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002458 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002459
2460 if (utf8)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002461 rettv->vval.v_number = (*utf_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002462 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002463 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002464 }
2465 else
2466#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002467 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002468}
2469
2470/*
2471 * "cindent(lnum)" function
2472 */
2473 static void
2474f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2475{
2476#ifdef FEAT_CINDENT
2477 pos_T pos;
2478 linenr_T lnum;
2479
2480 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002481 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002482 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2483 {
2484 curwin->w_cursor.lnum = lnum;
2485 rettv->vval.v_number = get_c_indent();
2486 curwin->w_cursor = pos;
2487 }
2488 else
2489#endif
2490 rettv->vval.v_number = -1;
2491}
2492
2493/*
2494 * "clearmatches()" function
2495 */
2496 static void
2497f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2498{
2499#ifdef FEAT_SEARCH_EXTRA
2500 clear_matches(curwin);
2501#endif
2502}
2503
2504/*
2505 * "col(string)" function
2506 */
2507 static void
2508f_col(typval_T *argvars, typval_T *rettv)
2509{
2510 colnr_T col = 0;
2511 pos_T *fp;
2512 int fnum = curbuf->b_fnum;
2513
2514 fp = var2fpos(&argvars[0], FALSE, &fnum);
2515 if (fp != NULL && fnum == curbuf->b_fnum)
2516 {
2517 if (fp->col == MAXCOL)
2518 {
2519 /* '> can be MAXCOL, get the length of the line then */
2520 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2521 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2522 else
2523 col = MAXCOL;
2524 }
2525 else
2526 {
2527 col = fp->col + 1;
2528#ifdef FEAT_VIRTUALEDIT
2529 /* col(".") when the cursor is on the NUL at the end of the line
2530 * because of "coladd" can be seen as an extra column. */
2531 if (virtual_active() && fp == &curwin->w_cursor)
2532 {
2533 char_u *p = ml_get_cursor();
2534
2535 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2536 curwin->w_virtcol - curwin->w_cursor.coladd))
2537 {
2538# ifdef FEAT_MBYTE
2539 int l;
2540
2541 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2542 col += l;
2543# else
2544 if (*p != NUL && p[1] == NUL)
2545 ++col;
2546# endif
2547 }
2548 }
2549#endif
2550 }
2551 }
2552 rettv->vval.v_number = col;
2553}
2554
2555#if defined(FEAT_INS_EXPAND)
2556/*
2557 * "complete()" function
2558 */
2559 static void
2560f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2561{
2562 int startcol;
2563
2564 if ((State & INSERT) == 0)
2565 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002566 emsg(_("E785: complete() can only be used in Insert mode"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002567 return;
2568 }
2569
2570 /* Check for undo allowed here, because if something was already inserted
2571 * the line was already saved for undo and this check isn't done. */
2572 if (!undo_allowed())
2573 return;
2574
2575 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2576 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002577 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002578 return;
2579 }
2580
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002581 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002582 if (startcol <= 0)
2583 return;
2584
2585 set_completion(startcol - 1, argvars[1].vval.v_list);
2586}
2587
2588/*
2589 * "complete_add()" function
2590 */
2591 static void
2592f_complete_add(typval_T *argvars, typval_T *rettv)
2593{
2594 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2595}
2596
2597/*
2598 * "complete_check()" function
2599 */
2600 static void
2601f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2602{
2603 int saved = RedrawingDisabled;
2604
2605 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002606 ins_compl_check_keys(0, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002607 rettv->vval.v_number = compl_interrupted;
2608 RedrawingDisabled = saved;
2609}
2610#endif
2611
2612/*
2613 * "confirm(message, buttons[, default [, type]])" function
2614 */
2615 static void
2616f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2617{
2618#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2619 char_u *message;
2620 char_u *buttons = NULL;
2621 char_u buf[NUMBUFLEN];
2622 char_u buf2[NUMBUFLEN];
2623 int def = 1;
2624 int type = VIM_GENERIC;
2625 char_u *typestr;
2626 int error = FALSE;
2627
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002628 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002629 if (message == NULL)
2630 error = TRUE;
2631 if (argvars[1].v_type != VAR_UNKNOWN)
2632 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002633 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002634 if (buttons == NULL)
2635 error = TRUE;
2636 if (argvars[2].v_type != VAR_UNKNOWN)
2637 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002638 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002639 if (argvars[3].v_type != VAR_UNKNOWN)
2640 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002641 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002642 if (typestr == NULL)
2643 error = TRUE;
2644 else
2645 {
2646 switch (TOUPPER_ASC(*typestr))
2647 {
2648 case 'E': type = VIM_ERROR; break;
2649 case 'Q': type = VIM_QUESTION; break;
2650 case 'I': type = VIM_INFO; break;
2651 case 'W': type = VIM_WARNING; break;
2652 case 'G': type = VIM_GENERIC; break;
2653 }
2654 }
2655 }
2656 }
2657 }
2658
2659 if (buttons == NULL || *buttons == NUL)
2660 buttons = (char_u *)_("&Ok");
2661
2662 if (!error)
2663 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2664 def, NULL, FALSE);
2665#endif
2666}
2667
2668/*
2669 * "copy()" function
2670 */
2671 static void
2672f_copy(typval_T *argvars, typval_T *rettv)
2673{
2674 item_copy(&argvars[0], rettv, FALSE, 0);
2675}
2676
2677#ifdef FEAT_FLOAT
2678/*
2679 * "cos()" function
2680 */
2681 static void
2682f_cos(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 = cos(f);
2689 else
2690 rettv->vval.v_float = 0.0;
2691}
2692
2693/*
2694 * "cosh()" function
2695 */
2696 static void
2697f_cosh(typval_T *argvars, typval_T *rettv)
2698{
2699 float_T f = 0.0;
2700
2701 rettv->v_type = VAR_FLOAT;
2702 if (get_float_arg(argvars, &f) == OK)
2703 rettv->vval.v_float = cosh(f);
2704 else
2705 rettv->vval.v_float = 0.0;
2706}
2707#endif
2708
2709/*
2710 * "count()" function
2711 */
2712 static void
2713f_count(typval_T *argvars, typval_T *rettv)
2714{
2715 long n = 0;
2716 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002717 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002718
Bram Moolenaar9966b212017-07-28 16:46:57 +02002719 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002720 ic = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002721
2722 if (argvars[0].v_type == VAR_STRING)
2723 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002724 char_u *expr = tv_get_string_chk(&argvars[1]);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002725 char_u *p = argvars[0].vval.v_string;
2726 char_u *next;
2727
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002728 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002729 {
2730 if (ic)
2731 {
2732 size_t len = STRLEN(expr);
2733
2734 while (*p != NUL)
2735 {
2736 if (MB_STRNICMP(p, expr, len) == 0)
2737 {
2738 ++n;
2739 p += len;
2740 }
2741 else
2742 MB_PTR_ADV(p);
2743 }
2744 }
2745 else
2746 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2747 != NULL)
2748 {
2749 ++n;
2750 p = next + STRLEN(expr);
2751 }
2752 }
2753
2754 }
2755 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002756 {
2757 listitem_T *li;
2758 list_T *l;
2759 long idx;
2760
2761 if ((l = argvars[0].vval.v_list) != NULL)
2762 {
2763 li = l->lv_first;
2764 if (argvars[2].v_type != VAR_UNKNOWN)
2765 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002766 if (argvars[3].v_type != VAR_UNKNOWN)
2767 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002768 idx = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002769 if (!error)
2770 {
2771 li = list_find(l, idx);
2772 if (li == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002773 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002774 }
2775 }
2776 if (error)
2777 li = NULL;
2778 }
2779
2780 for ( ; li != NULL; li = li->li_next)
2781 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2782 ++n;
2783 }
2784 }
2785 else if (argvars[0].v_type == VAR_DICT)
2786 {
2787 int todo;
2788 dict_T *d;
2789 hashitem_T *hi;
2790
2791 if ((d = argvars[0].vval.v_dict) != NULL)
2792 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002793 if (argvars[2].v_type != VAR_UNKNOWN)
2794 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002795 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002796 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002797 }
2798
2799 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2800 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2801 {
2802 if (!HASHITEM_EMPTY(hi))
2803 {
2804 --todo;
2805 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2806 ++n;
2807 }
2808 }
2809 }
2810 }
2811 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002812 semsg(_(e_listdictarg), "count()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002813 rettv->vval.v_number = n;
2814}
2815
2816/*
2817 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2818 *
2819 * Checks the existence of a cscope connection.
2820 */
2821 static void
2822f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2823{
2824#ifdef FEAT_CSCOPE
2825 int num = 0;
2826 char_u *dbpath = NULL;
2827 char_u *prepend = NULL;
2828 char_u buf[NUMBUFLEN];
2829
2830 if (argvars[0].v_type != VAR_UNKNOWN
2831 && argvars[1].v_type != VAR_UNKNOWN)
2832 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002833 num = (int)tv_get_number(&argvars[0]);
2834 dbpath = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002835 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002836 prepend = tv_get_string_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002837 }
2838
2839 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2840#endif
2841}
2842
2843/*
2844 * "cursor(lnum, col)" function, or
2845 * "cursor(list)"
2846 *
2847 * Moves the cursor to the specified line and column.
2848 * Returns 0 when the position could be set, -1 otherwise.
2849 */
2850 static void
2851f_cursor(typval_T *argvars, typval_T *rettv)
2852{
2853 long line, col;
2854#ifdef FEAT_VIRTUALEDIT
2855 long coladd = 0;
2856#endif
2857 int set_curswant = TRUE;
2858
2859 rettv->vval.v_number = -1;
2860 if (argvars[1].v_type == VAR_UNKNOWN)
2861 {
2862 pos_T pos;
2863 colnr_T curswant = -1;
2864
2865 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2866 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002867 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002868 return;
2869 }
2870 line = pos.lnum;
2871 col = pos.col;
2872#ifdef FEAT_VIRTUALEDIT
2873 coladd = pos.coladd;
2874#endif
2875 if (curswant >= 0)
2876 {
2877 curwin->w_curswant = curswant - 1;
2878 set_curswant = FALSE;
2879 }
2880 }
2881 else
2882 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002883 line = tv_get_lnum(argvars);
2884 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002885#ifdef FEAT_VIRTUALEDIT
2886 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002887 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002888#endif
2889 }
2890 if (line < 0 || col < 0
2891#ifdef FEAT_VIRTUALEDIT
2892 || coladd < 0
2893#endif
2894 )
2895 return; /* type error; errmsg already given */
2896 if (line > 0)
2897 curwin->w_cursor.lnum = line;
2898 if (col > 0)
2899 curwin->w_cursor.col = col - 1;
2900#ifdef FEAT_VIRTUALEDIT
2901 curwin->w_cursor.coladd = coladd;
2902#endif
2903
2904 /* Make sure the cursor is in a valid position. */
2905 check_cursor();
2906#ifdef FEAT_MBYTE
2907 /* Correct cursor for multi-byte character. */
2908 if (has_mbyte)
2909 mb_adjust_cursor();
2910#endif
2911
2912 curwin->w_set_curswant = set_curswant;
2913 rettv->vval.v_number = 0;
2914}
2915
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002916#ifdef WIN3264
2917/*
2918 * "debugbreak()" function
2919 */
2920 static void
2921f_debugbreak(typval_T *argvars, typval_T *rettv)
2922{
2923 int pid;
2924
2925 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002926 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002927 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002928 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002929 else
2930 {
2931 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2932
2933 if (hProcess != NULL)
2934 {
2935 DebugBreakProcess(hProcess);
2936 CloseHandle(hProcess);
2937 rettv->vval.v_number = OK;
2938 }
2939 }
2940}
2941#endif
2942
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002943/*
2944 * "deepcopy()" function
2945 */
2946 static void
2947f_deepcopy(typval_T *argvars, typval_T *rettv)
2948{
2949 int noref = 0;
2950 int copyID;
2951
2952 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002953 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002954 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002955 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002956 else
2957 {
2958 copyID = get_copyID();
2959 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2960 }
2961}
2962
2963/*
2964 * "delete()" function
2965 */
2966 static void
2967f_delete(typval_T *argvars, typval_T *rettv)
2968{
2969 char_u nbuf[NUMBUFLEN];
2970 char_u *name;
2971 char_u *flags;
2972
2973 rettv->vval.v_number = -1;
2974 if (check_restricted() || check_secure())
2975 return;
2976
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002977 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002978 if (name == NULL || *name == NUL)
2979 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002980 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002981 return;
2982 }
2983
2984 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002985 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002986 else
2987 flags = (char_u *)"";
2988
2989 if (*flags == NUL)
2990 /* delete a file */
2991 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2992 else if (STRCMP(flags, "d") == 0)
2993 /* delete an empty directory */
2994 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2995 else if (STRCMP(flags, "rf") == 0)
2996 /* delete a directory recursively */
2997 rettv->vval.v_number = delete_recursive(name);
2998 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002999 semsg(_(e_invexpr2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003000}
3001
3002/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02003003 * "deletebufline()" function
3004 */
3005 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02003006f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02003007{
3008 buf_T *buf;
3009 linenr_T first, last;
3010 linenr_T lnum;
3011 long count;
3012 int is_curbuf;
3013 buf_T *curbuf_save = NULL;
3014 win_T *curwin_save = NULL;
3015 tabpage_T *tp;
3016 win_T *wp;
3017
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01003018 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003019 if (buf == NULL)
3020 {
3021 rettv->vval.v_number = 1; /* FAIL */
3022 return;
3023 }
3024 is_curbuf = buf == curbuf;
3025
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003026 first = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003027 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003028 last = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003029 else
3030 last = first;
3031
3032 if (buf->b_ml.ml_mfp == NULL || first < 1
3033 || first > buf->b_ml.ml_line_count || last < first)
3034 {
3035 rettv->vval.v_number = 1; /* FAIL */
3036 return;
3037 }
3038
3039 if (!is_curbuf)
3040 {
3041 curbuf_save = curbuf;
3042 curwin_save = curwin;
3043 curbuf = buf;
3044 find_win_for_curbuf();
3045 }
3046 if (last > curbuf->b_ml.ml_line_count)
3047 last = curbuf->b_ml.ml_line_count;
3048 count = last - first + 1;
3049
3050 // When coming here from Insert mode, sync undo, so that this can be
3051 // undone separately from what was previously inserted.
3052 if (u_sync_once == 2)
3053 {
3054 u_sync_once = 1; // notify that u_sync() was called
3055 u_sync(TRUE);
3056 }
3057
3058 if (u_save(first - 1, last + 1) == FAIL)
3059 {
3060 rettv->vval.v_number = 1; /* FAIL */
3061 return;
3062 }
3063
3064 for (lnum = first; lnum <= last; ++lnum)
3065 ml_delete(first, TRUE);
3066
3067 FOR_ALL_TAB_WINDOWS(tp, wp)
3068 if (wp->w_buffer == buf)
3069 {
3070 if (wp->w_cursor.lnum > last)
3071 wp->w_cursor.lnum -= count;
3072 else if (wp->w_cursor.lnum> first)
3073 wp->w_cursor.lnum = first;
3074 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
3075 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
3076 }
3077 check_cursor_col();
3078 deleted_lines_mark(first, count);
3079
3080 if (!is_curbuf)
3081 {
3082 curbuf = curbuf_save;
3083 curwin = curwin_save;
3084 }
3085}
3086
3087/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003088 * "did_filetype()" function
3089 */
3090 static void
3091f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3092{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003093 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003094}
3095
3096/*
3097 * "diff_filler()" function
3098 */
3099 static void
3100f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3101{
3102#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003103 rettv->vval.v_number = diff_check_fill(curwin, tv_get_lnum(argvars));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003104#endif
3105}
3106
3107/*
3108 * "diff_hlID()" function
3109 */
3110 static void
3111f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3112{
3113#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003114 linenr_T lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003115 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01003116 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003117 static int fnum = 0;
3118 static int change_start = 0;
3119 static int change_end = 0;
3120 static hlf_T hlID = (hlf_T)0;
3121 int filler_lines;
3122 int col;
3123
3124 if (lnum < 0) /* ignore type error in {lnum} arg */
3125 lnum = 0;
3126 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003127 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003128 || fnum != curbuf->b_fnum)
3129 {
3130 /* New line, buffer, change: need to get the values. */
3131 filler_lines = diff_check(curwin, lnum);
3132 if (filler_lines < 0)
3133 {
3134 if (filler_lines == -1)
3135 {
3136 change_start = MAXCOL;
3137 change_end = -1;
3138 if (diff_find_change(curwin, lnum, &change_start, &change_end))
3139 hlID = HLF_ADD; /* added line */
3140 else
3141 hlID = HLF_CHD; /* changed line */
3142 }
3143 else
3144 hlID = HLF_ADD; /* added line */
3145 }
3146 else
3147 hlID = (hlf_T)0;
3148 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003149 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003150 fnum = curbuf->b_fnum;
3151 }
3152
3153 if (hlID == HLF_CHD || hlID == HLF_TXD)
3154 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003155 col = tv_get_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003156 if (col >= change_start && col <= change_end)
3157 hlID = HLF_TXD; /* changed text */
3158 else
3159 hlID = HLF_CHD; /* changed line */
3160 }
3161 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
3162#endif
3163}
3164
3165/*
3166 * "empty({expr})" function
3167 */
3168 static void
3169f_empty(typval_T *argvars, typval_T *rettv)
3170{
3171 int n = FALSE;
3172
3173 switch (argvars[0].v_type)
3174 {
3175 case VAR_STRING:
3176 case VAR_FUNC:
3177 n = argvars[0].vval.v_string == NULL
3178 || *argvars[0].vval.v_string == NUL;
3179 break;
3180 case VAR_PARTIAL:
3181 n = FALSE;
3182 break;
3183 case VAR_NUMBER:
3184 n = argvars[0].vval.v_number == 0;
3185 break;
3186 case VAR_FLOAT:
3187#ifdef FEAT_FLOAT
3188 n = argvars[0].vval.v_float == 0.0;
3189 break;
3190#endif
3191 case VAR_LIST:
3192 n = argvars[0].vval.v_list == NULL
3193 || argvars[0].vval.v_list->lv_first == NULL;
3194 break;
3195 case VAR_DICT:
3196 n = argvars[0].vval.v_dict == NULL
3197 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
3198 break;
3199 case VAR_SPECIAL:
3200 n = argvars[0].vval.v_number != VVAL_TRUE;
3201 break;
3202
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003203 case VAR_BLOB:
3204 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003205 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
3206 break;
3207
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003208 case VAR_JOB:
3209#ifdef FEAT_JOB_CHANNEL
3210 n = argvars[0].vval.v_job == NULL
3211 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
3212 break;
3213#endif
3214 case VAR_CHANNEL:
3215#ifdef FEAT_JOB_CHANNEL
3216 n = argvars[0].vval.v_channel == NULL
3217 || !channel_is_open(argvars[0].vval.v_channel);
3218 break;
3219#endif
3220 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01003221 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003222 n = TRUE;
3223 break;
3224 }
3225
3226 rettv->vval.v_number = n;
3227}
3228
3229/*
3230 * "escape({string}, {chars})" function
3231 */
3232 static void
3233f_escape(typval_T *argvars, typval_T *rettv)
3234{
3235 char_u buf[NUMBUFLEN];
3236
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003237 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
3238 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003239 rettv->v_type = VAR_STRING;
3240}
3241
3242/*
3243 * "eval()" function
3244 */
3245 static void
3246f_eval(typval_T *argvars, typval_T *rettv)
3247{
3248 char_u *s, *p;
3249
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003250 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003251 if (s != NULL)
3252 s = skipwhite(s);
3253
3254 p = s;
3255 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3256 {
3257 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003258 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003259 need_clr_eos = FALSE;
3260 rettv->v_type = VAR_NUMBER;
3261 rettv->vval.v_number = 0;
3262 }
3263 else if (*s != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003264 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003265}
3266
3267/*
3268 * "eventhandler()" function
3269 */
3270 static void
3271f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3272{
3273 rettv->vval.v_number = vgetc_busy;
3274}
3275
3276/*
3277 * "executable()" function
3278 */
3279 static void
3280f_executable(typval_T *argvars, typval_T *rettv)
3281{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003282 char_u *name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003283
3284 /* Check in $PATH and also check directly if there is a directory name. */
3285 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
3286 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
3287}
3288
3289static garray_T redir_execute_ga;
3290
3291/*
3292 * Append "value[value_len]" to the execute() output.
3293 */
3294 void
3295execute_redir_str(char_u *value, int value_len)
3296{
3297 int len;
3298
3299 if (value_len == -1)
3300 len = (int)STRLEN(value); /* Append the entire string */
3301 else
3302 len = value_len; /* Append only "value_len" characters */
3303 if (ga_grow(&redir_execute_ga, len) == OK)
3304 {
3305 mch_memmove((char *)redir_execute_ga.ga_data
3306 + redir_execute_ga.ga_len, value, len);
3307 redir_execute_ga.ga_len += len;
3308 }
3309}
3310
3311/*
3312 * Get next line from a list.
3313 * Called by do_cmdline() to get the next line.
3314 * Returns allocated string, or NULL for end of function.
3315 */
3316
3317 static char_u *
3318get_list_line(
3319 int c UNUSED,
3320 void *cookie,
3321 int indent UNUSED)
3322{
3323 listitem_T **p = (listitem_T **)cookie;
3324 listitem_T *item = *p;
3325 char_u buf[NUMBUFLEN];
3326 char_u *s;
3327
3328 if (item == NULL)
3329 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003330 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003331 *p = item->li_next;
3332 return s == NULL ? NULL : vim_strsave(s);
3333}
3334
3335/*
3336 * "execute()" function
3337 */
3338 static void
3339f_execute(typval_T *argvars, typval_T *rettv)
3340{
3341 char_u *cmd = NULL;
3342 list_T *list = NULL;
3343 int save_msg_silent = msg_silent;
3344 int save_emsg_silent = emsg_silent;
3345 int save_emsg_noredir = emsg_noredir;
3346 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003347 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003348 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003349 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003350 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003351
3352 rettv->vval.v_string = NULL;
3353 rettv->v_type = VAR_STRING;
3354
3355 if (argvars[0].v_type == VAR_LIST)
3356 {
3357 list = argvars[0].vval.v_list;
3358 if (list == NULL || list->lv_first == NULL)
3359 /* empty list, no commands, empty output */
3360 return;
3361 ++list->lv_refcount;
3362 }
3363 else
3364 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003365 cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003366 if (cmd == NULL)
3367 return;
3368 }
3369
3370 if (argvars[1].v_type != VAR_UNKNOWN)
3371 {
3372 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003373 char_u *s = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003374
3375 if (s == NULL)
3376 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003377 if (*s == NUL)
3378 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003379 if (STRNCMP(s, "silent", 6) == 0)
3380 ++msg_silent;
3381 if (STRCMP(s, "silent!") == 0)
3382 {
3383 emsg_silent = TRUE;
3384 emsg_noredir = TRUE;
3385 }
3386 }
3387 else
3388 ++msg_silent;
3389
3390 if (redir_execute)
3391 save_ga = redir_execute_ga;
3392 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3393 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003394 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003395 if (!echo_output)
3396 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003397
3398 if (cmd != NULL)
3399 do_cmdline_cmd(cmd);
3400 else
3401 {
3402 listitem_T *item = list->lv_first;
3403
3404 do_cmdline(NULL, get_list_line, (void *)&item,
3405 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3406 --list->lv_refcount;
3407 }
3408
Bram Moolenaard297f352017-01-29 20:31:21 +01003409 /* Need to append a NUL to the result. */
3410 if (ga_grow(&redir_execute_ga, 1) == OK)
3411 {
3412 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3413 rettv->vval.v_string = redir_execute_ga.ga_data;
3414 }
3415 else
3416 {
3417 ga_clear(&redir_execute_ga);
3418 rettv->vval.v_string = NULL;
3419 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003420 msg_silent = save_msg_silent;
3421 emsg_silent = save_emsg_silent;
3422 emsg_noredir = save_emsg_noredir;
3423
3424 redir_execute = save_redir_execute;
3425 if (redir_execute)
3426 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003427 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003428
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003429 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003430 if (echo_output)
3431 // When not working silently: put it in column zero. A following
3432 // "echon" will overwrite the message, unavoidably.
3433 msg_col = 0;
3434 else
3435 // When working silently: Put it back where it was, since nothing
3436 // should have been written.
3437 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003438}
3439
3440/*
3441 * "exepath()" function
3442 */
3443 static void
3444f_exepath(typval_T *argvars, typval_T *rettv)
3445{
3446 char_u *p = NULL;
3447
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003448 (void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003449 rettv->v_type = VAR_STRING;
3450 rettv->vval.v_string = p;
3451}
3452
3453/*
3454 * "exists()" function
3455 */
3456 static void
3457f_exists(typval_T *argvars, typval_T *rettv)
3458{
3459 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003460 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003461
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003462 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003463 if (*p == '$') /* environment variable */
3464 {
3465 /* first try "normal" environment variables (fast) */
3466 if (mch_getenv(p + 1) != NULL)
3467 n = TRUE;
3468 else
3469 {
3470 /* try expanding things like $VIM and ${HOME} */
3471 p = expand_env_save(p);
3472 if (p != NULL && *p != '$')
3473 n = TRUE;
3474 vim_free(p);
3475 }
3476 }
3477 else if (*p == '&' || *p == '+') /* option */
3478 {
3479 n = (get_option_tv(&p, NULL, TRUE) == OK);
3480 if (*skipwhite(p) != NUL)
3481 n = FALSE; /* trailing garbage */
3482 }
3483 else if (*p == '*') /* internal or user defined function */
3484 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003485 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003486 }
3487 else if (*p == ':')
3488 {
3489 n = cmd_exists(p + 1);
3490 }
3491 else if (*p == '#')
3492 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003493 if (p[1] == '#')
3494 n = autocmd_supported(p + 2);
3495 else
3496 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003497 }
3498 else /* internal variable */
3499 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003500 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003501 }
3502
3503 rettv->vval.v_number = n;
3504}
3505
3506#ifdef FEAT_FLOAT
3507/*
3508 * "exp()" function
3509 */
3510 static void
3511f_exp(typval_T *argvars, typval_T *rettv)
3512{
3513 float_T f = 0.0;
3514
3515 rettv->v_type = VAR_FLOAT;
3516 if (get_float_arg(argvars, &f) == OK)
3517 rettv->vval.v_float = exp(f);
3518 else
3519 rettv->vval.v_float = 0.0;
3520}
3521#endif
3522
3523/*
3524 * "expand()" function
3525 */
3526 static void
3527f_expand(typval_T *argvars, typval_T *rettv)
3528{
3529 char_u *s;
3530 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003531 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003532 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3533 expand_T xpc;
3534 int error = FALSE;
3535 char_u *result;
3536
3537 rettv->v_type = VAR_STRING;
3538 if (argvars[1].v_type != VAR_UNKNOWN
3539 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003540 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003541 && !error)
3542 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003543 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003544 }
3545
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003546 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003547 if (*s == '%' || *s == '#' || *s == '<')
3548 {
3549 ++emsg_off;
3550 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3551 --emsg_off;
3552 if (rettv->v_type == VAR_LIST)
3553 {
3554 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3555 list_append_string(rettv->vval.v_list, result, -1);
3556 else
3557 vim_free(result);
3558 }
3559 else
3560 rettv->vval.v_string = result;
3561 }
3562 else
3563 {
3564 /* When the optional second argument is non-zero, don't remove matches
3565 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3566 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003567 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003568 options |= WILD_KEEP_ALL;
3569 if (!error)
3570 {
3571 ExpandInit(&xpc);
3572 xpc.xp_context = EXPAND_FILES;
3573 if (p_wic)
3574 options += WILD_ICASE;
3575 if (rettv->v_type == VAR_STRING)
3576 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3577 options, WILD_ALL);
3578 else if (rettv_list_alloc(rettv) != FAIL)
3579 {
3580 int i;
3581
3582 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3583 for (i = 0; i < xpc.xp_numfiles; i++)
3584 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3585 ExpandCleanup(&xpc);
3586 }
3587 }
3588 else
3589 rettv->vval.v_string = NULL;
3590 }
3591}
3592
3593/*
3594 * "extend(list, list [, idx])" function
3595 * "extend(dict, dict [, action])" function
3596 */
3597 static void
3598f_extend(typval_T *argvars, typval_T *rettv)
3599{
3600 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3601
3602 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3603 {
3604 list_T *l1, *l2;
3605 listitem_T *item;
3606 long before;
3607 int error = FALSE;
3608
3609 l1 = argvars[0].vval.v_list;
3610 l2 = argvars[1].vval.v_list;
3611 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
3612 && l2 != NULL)
3613 {
3614 if (argvars[2].v_type != VAR_UNKNOWN)
3615 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003616 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003617 if (error)
3618 return; /* type error; errmsg already given */
3619
3620 if (before == l1->lv_len)
3621 item = NULL;
3622 else
3623 {
3624 item = list_find(l1, before);
3625 if (item == NULL)
3626 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003627 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003628 return;
3629 }
3630 }
3631 }
3632 else
3633 item = NULL;
3634 list_extend(l1, l2, item);
3635
3636 copy_tv(&argvars[0], rettv);
3637 }
3638 }
3639 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3640 {
3641 dict_T *d1, *d2;
3642 char_u *action;
3643 int i;
3644
3645 d1 = argvars[0].vval.v_dict;
3646 d2 = argvars[1].vval.v_dict;
3647 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
3648 && d2 != NULL)
3649 {
3650 /* Check the third argument. */
3651 if (argvars[2].v_type != VAR_UNKNOWN)
3652 {
3653 static char *(av[]) = {"keep", "force", "error"};
3654
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003655 action = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003656 if (action == NULL)
3657 return; /* type error; errmsg already given */
3658 for (i = 0; i < 3; ++i)
3659 if (STRCMP(action, av[i]) == 0)
3660 break;
3661 if (i == 3)
3662 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003663 semsg(_(e_invarg2), action);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003664 return;
3665 }
3666 }
3667 else
3668 action = (char_u *)"force";
3669
3670 dict_extend(d1, d2, action);
3671
3672 copy_tv(&argvars[0], rettv);
3673 }
3674 }
3675 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003676 semsg(_(e_listdictarg), "extend()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003677}
3678
3679/*
3680 * "feedkeys()" function
3681 */
3682 static void
3683f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3684{
3685 int remap = TRUE;
3686 int insert = FALSE;
3687 char_u *keys, *flags;
3688 char_u nbuf[NUMBUFLEN];
3689 int typed = FALSE;
3690 int execute = FALSE;
3691 int dangerous = FALSE;
3692 char_u *keys_esc;
3693
3694 /* This is not allowed in the sandbox. If the commands would still be
3695 * executed in the sandbox it would be OK, but it probably happens later,
3696 * when "sandbox" is no longer set. */
3697 if (check_secure())
3698 return;
3699
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003700 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003701
3702 if (argvars[1].v_type != VAR_UNKNOWN)
3703 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003704 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003705 for ( ; *flags != NUL; ++flags)
3706 {
3707 switch (*flags)
3708 {
3709 case 'n': remap = FALSE; break;
3710 case 'm': remap = TRUE; break;
3711 case 't': typed = TRUE; break;
3712 case 'i': insert = TRUE; break;
3713 case 'x': execute = TRUE; break;
3714 case '!': dangerous = TRUE; break;
3715 }
3716 }
3717 }
3718
3719 if (*keys != NUL || execute)
3720 {
3721 /* Need to escape K_SPECIAL and CSI before putting the string in the
3722 * typeahead buffer. */
3723 keys_esc = vim_strsave_escape_csi(keys);
3724 if (keys_esc != NULL)
3725 {
3726 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
3727 insert ? 0 : typebuf.tb_len, !typed, FALSE);
3728 vim_free(keys_esc);
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003729 if (vgetc_busy
3730#ifdef FEAT_TIMERS
3731 || timer_busy
3732#endif
3733 )
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003734 typebuf_was_filled = TRUE;
3735 if (execute)
3736 {
3737 int save_msg_scroll = msg_scroll;
3738
3739 /* Avoid a 1 second delay when the keys start Insert mode. */
3740 msg_scroll = FALSE;
3741
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003742 if (!dangerous)
3743 ++ex_normal_busy;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02003744 exec_normal(TRUE, FALSE, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003745 if (!dangerous)
3746 --ex_normal_busy;
3747
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003748 msg_scroll |= save_msg_scroll;
3749 }
3750 }
3751 }
3752}
3753
3754/*
3755 * "filereadable()" function
3756 */
3757 static void
3758f_filereadable(typval_T *argvars, typval_T *rettv)
3759{
3760 int fd;
3761 char_u *p;
3762 int n;
3763
3764#ifndef O_NONBLOCK
3765# define O_NONBLOCK 0
3766#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003767 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003768 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3769 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3770 {
3771 n = TRUE;
3772 close(fd);
3773 }
3774 else
3775 n = FALSE;
3776
3777 rettv->vval.v_number = n;
3778}
3779
3780/*
3781 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3782 * rights to write into.
3783 */
3784 static void
3785f_filewritable(typval_T *argvars, typval_T *rettv)
3786{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003787 rettv->vval.v_number = filewritable(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003788}
3789
3790 static void
3791findfilendir(
3792 typval_T *argvars UNUSED,
3793 typval_T *rettv,
3794 int find_what UNUSED)
3795{
3796#ifdef FEAT_SEARCHPATH
3797 char_u *fname;
3798 char_u *fresult = NULL;
3799 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3800 char_u *p;
3801 char_u pathbuf[NUMBUFLEN];
3802 int count = 1;
3803 int first = TRUE;
3804 int error = FALSE;
3805#endif
3806
3807 rettv->vval.v_string = NULL;
3808 rettv->v_type = VAR_STRING;
3809
3810#ifdef FEAT_SEARCHPATH
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003811 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003812
3813 if (argvars[1].v_type != VAR_UNKNOWN)
3814 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003815 p = tv_get_string_buf_chk(&argvars[1], pathbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003816 if (p == NULL)
3817 error = TRUE;
3818 else
3819 {
3820 if (*p != NUL)
3821 path = p;
3822
3823 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003824 count = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003825 }
3826 }
3827
3828 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3829 error = TRUE;
3830
3831 if (*fname != NUL && !error)
3832 {
3833 do
3834 {
3835 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3836 vim_free(fresult);
3837 fresult = find_file_in_path_option(first ? fname : NULL,
3838 first ? (int)STRLEN(fname) : 0,
3839 0, first, path,
3840 find_what,
3841 curbuf->b_ffname,
3842 find_what == FINDFILE_DIR
3843 ? (char_u *)"" : curbuf->b_p_sua);
3844 first = FALSE;
3845
3846 if (fresult != NULL && rettv->v_type == VAR_LIST)
3847 list_append_string(rettv->vval.v_list, fresult, -1);
3848
3849 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3850 }
3851
3852 if (rettv->v_type == VAR_STRING)
3853 rettv->vval.v_string = fresult;
3854#endif
3855}
3856
3857/*
3858 * "filter()" function
3859 */
3860 static void
3861f_filter(typval_T *argvars, typval_T *rettv)
3862{
3863 filter_map(argvars, rettv, FALSE);
3864}
3865
3866/*
3867 * "finddir({fname}[, {path}[, {count}]])" function
3868 */
3869 static void
3870f_finddir(typval_T *argvars, typval_T *rettv)
3871{
3872 findfilendir(argvars, rettv, FINDFILE_DIR);
3873}
3874
3875/*
3876 * "findfile({fname}[, {path}[, {count}]])" function
3877 */
3878 static void
3879f_findfile(typval_T *argvars, typval_T *rettv)
3880{
3881 findfilendir(argvars, rettv, FINDFILE_FILE);
3882}
3883
3884#ifdef FEAT_FLOAT
3885/*
3886 * "float2nr({float})" function
3887 */
3888 static void
3889f_float2nr(typval_T *argvars, typval_T *rettv)
3890{
3891 float_T f = 0.0;
3892
3893 if (get_float_arg(argvars, &f) == OK)
3894 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003895 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003896 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003897 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003898 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003899 else
3900 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003901 }
3902}
3903
3904/*
3905 * "floor({float})" function
3906 */
3907 static void
3908f_floor(typval_T *argvars, typval_T *rettv)
3909{
3910 float_T f = 0.0;
3911
3912 rettv->v_type = VAR_FLOAT;
3913 if (get_float_arg(argvars, &f) == OK)
3914 rettv->vval.v_float = floor(f);
3915 else
3916 rettv->vval.v_float = 0.0;
3917}
3918
3919/*
3920 * "fmod()" function
3921 */
3922 static void
3923f_fmod(typval_T *argvars, typval_T *rettv)
3924{
3925 float_T fx = 0.0, fy = 0.0;
3926
3927 rettv->v_type = VAR_FLOAT;
3928 if (get_float_arg(argvars, &fx) == OK
3929 && get_float_arg(&argvars[1], &fy) == OK)
3930 rettv->vval.v_float = fmod(fx, fy);
3931 else
3932 rettv->vval.v_float = 0.0;
3933}
3934#endif
3935
3936/*
3937 * "fnameescape({string})" function
3938 */
3939 static void
3940f_fnameescape(typval_T *argvars, typval_T *rettv)
3941{
3942 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003943 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003944 rettv->v_type = VAR_STRING;
3945}
3946
3947/*
3948 * "fnamemodify({fname}, {mods})" function
3949 */
3950 static void
3951f_fnamemodify(typval_T *argvars, typval_T *rettv)
3952{
3953 char_u *fname;
3954 char_u *mods;
3955 int usedlen = 0;
3956 int len;
3957 char_u *fbuf = NULL;
3958 char_u buf[NUMBUFLEN];
3959
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003960 fname = tv_get_string_chk(&argvars[0]);
3961 mods = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003962 if (fname == NULL || mods == NULL)
3963 fname = NULL;
3964 else
3965 {
3966 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02003967 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003968 }
3969
3970 rettv->v_type = VAR_STRING;
3971 if (fname == NULL)
3972 rettv->vval.v_string = NULL;
3973 else
3974 rettv->vval.v_string = vim_strnsave(fname, len);
3975 vim_free(fbuf);
3976}
3977
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003978/*
3979 * "foldclosed()" function
3980 */
3981 static void
3982foldclosed_both(
3983 typval_T *argvars UNUSED,
3984 typval_T *rettv,
3985 int end UNUSED)
3986{
3987#ifdef FEAT_FOLDING
3988 linenr_T lnum;
3989 linenr_T first, last;
3990
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003991 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003992 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3993 {
3994 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3995 {
3996 if (end)
3997 rettv->vval.v_number = (varnumber_T)last;
3998 else
3999 rettv->vval.v_number = (varnumber_T)first;
4000 return;
4001 }
4002 }
4003#endif
4004 rettv->vval.v_number = -1;
4005}
4006
4007/*
4008 * "foldclosed()" function
4009 */
4010 static void
4011f_foldclosed(typval_T *argvars, typval_T *rettv)
4012{
4013 foldclosed_both(argvars, rettv, FALSE);
4014}
4015
4016/*
4017 * "foldclosedend()" function
4018 */
4019 static void
4020f_foldclosedend(typval_T *argvars, typval_T *rettv)
4021{
4022 foldclosed_both(argvars, rettv, TRUE);
4023}
4024
4025/*
4026 * "foldlevel()" function
4027 */
4028 static void
4029f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4030{
4031#ifdef FEAT_FOLDING
4032 linenr_T lnum;
4033
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004034 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004035 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4036 rettv->vval.v_number = foldLevel(lnum);
4037#endif
4038}
4039
4040/*
4041 * "foldtext()" function
4042 */
4043 static void
4044f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
4045{
4046#ifdef FEAT_FOLDING
4047 linenr_T foldstart;
4048 linenr_T foldend;
4049 char_u *dashes;
4050 linenr_T lnum;
4051 char_u *s;
4052 char_u *r;
4053 int len;
4054 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004055 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004056#endif
4057
4058 rettv->v_type = VAR_STRING;
4059 rettv->vval.v_string = NULL;
4060#ifdef FEAT_FOLDING
4061 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
4062 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
4063 dashes = get_vim_var_str(VV_FOLDDASHES);
4064 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
4065 && dashes != NULL)
4066 {
4067 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02004068 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004069 if (!linewhite(lnum))
4070 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004071
4072 /* Find interesting text in this line. */
4073 s = skipwhite(ml_get(lnum));
4074 /* skip C comment-start */
4075 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
4076 {
4077 s = skipwhite(s + 2);
4078 if (*skipwhite(s) == NUL
4079 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
4080 {
4081 s = skipwhite(ml_get(lnum + 1));
4082 if (*s == '*')
4083 s = skipwhite(s + 1);
4084 }
4085 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02004086 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01004087 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004088 r = alloc((unsigned)(STRLEN(txt)
4089 + STRLEN(dashes) /* for %s */
4090 + 20 /* for %3ld */
4091 + STRLEN(s))); /* concatenated */
4092 if (r != NULL)
4093 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02004094 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004095 len = (int)STRLEN(r);
4096 STRCAT(r, s);
4097 /* remove 'foldmarker' and 'commentstring' */
4098 foldtext_cleanup(r + len);
4099 rettv->vval.v_string = r;
4100 }
4101 }
4102#endif
4103}
4104
4105/*
4106 * "foldtextresult(lnum)" function
4107 */
4108 static void
4109f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
4110{
4111#ifdef FEAT_FOLDING
4112 linenr_T lnum;
4113 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004114 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004115 foldinfo_T foldinfo;
4116 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004117 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004118#endif
4119
4120 rettv->v_type = VAR_STRING;
4121 rettv->vval.v_string = NULL;
4122#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004123 if (entered)
4124 return; /* reject recursive use */
4125 entered = TRUE;
4126
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004127 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004128 /* treat illegal types and illegal string values for {lnum} the same */
4129 if (lnum < 0)
4130 lnum = 0;
4131 fold_count = foldedCount(curwin, lnum, &foldinfo);
4132 if (fold_count > 0)
4133 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01004134 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
4135 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004136 if (text == buf)
4137 text = vim_strsave(text);
4138 rettv->vval.v_string = text;
4139 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004140
4141 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004142#endif
4143}
4144
4145/*
4146 * "foreground()" function
4147 */
4148 static void
4149f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4150{
4151#ifdef FEAT_GUI
4152 if (gui.in_use)
4153 gui_mch_set_foreground();
4154#else
4155# ifdef WIN32
4156 win32_set_foreground();
4157# endif
4158#endif
4159}
4160
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004161 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004162common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004163{
4164 char_u *s;
4165 char_u *name;
4166 int use_string = FALSE;
4167 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004168 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004169
4170 if (argvars[0].v_type == VAR_FUNC)
4171 {
4172 /* function(MyFunc, [arg], dict) */
4173 s = argvars[0].vval.v_string;
4174 }
4175 else if (argvars[0].v_type == VAR_PARTIAL
4176 && argvars[0].vval.v_partial != NULL)
4177 {
4178 /* function(dict.MyFunc, [arg]) */
4179 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004180 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004181 }
4182 else
4183 {
4184 /* function('MyFunc', [arg], dict) */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004185 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004186 use_string = TRUE;
4187 }
4188
Bram Moolenaar843b8842016-08-21 14:36:15 +02004189 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004190 {
4191 name = s;
4192 trans_name = trans_function_name(&name, FALSE,
4193 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4194 if (*name != NUL)
4195 s = NULL;
4196 }
4197
Bram Moolenaar843b8842016-08-21 14:36:15 +02004198 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4199 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004200 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004201 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004202 else if (trans_name != NULL && (is_funcref
4203 ? find_func(trans_name) == NULL
4204 : !translated_function_exists(trans_name)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004205 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004206 else
4207 {
4208 int dict_idx = 0;
4209 int arg_idx = 0;
4210 list_T *list = NULL;
4211
4212 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4213 {
4214 char sid_buf[25];
4215 int off = *s == 's' ? 2 : 5;
4216
4217 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4218 * also be called from another script. Using trans_function_name()
4219 * would also work, but some plugins depend on the name being
4220 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004221 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004222 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
4223 if (name != NULL)
4224 {
4225 STRCPY(name, sid_buf);
4226 STRCAT(name, s + off);
4227 }
4228 }
4229 else
4230 name = vim_strsave(s);
4231
4232 if (argvars[1].v_type != VAR_UNKNOWN)
4233 {
4234 if (argvars[2].v_type != VAR_UNKNOWN)
4235 {
4236 /* function(name, [args], dict) */
4237 arg_idx = 1;
4238 dict_idx = 2;
4239 }
4240 else if (argvars[1].v_type == VAR_DICT)
4241 /* function(name, dict) */
4242 dict_idx = 1;
4243 else
4244 /* function(name, [args]) */
4245 arg_idx = 1;
4246 if (dict_idx > 0)
4247 {
4248 if (argvars[dict_idx].v_type != VAR_DICT)
4249 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004250 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004251 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004252 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004253 }
4254 if (argvars[dict_idx].vval.v_dict == NULL)
4255 dict_idx = 0;
4256 }
4257 if (arg_idx > 0)
4258 {
4259 if (argvars[arg_idx].v_type != VAR_LIST)
4260 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004261 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004262 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004263 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004264 }
4265 list = argvars[arg_idx].vval.v_list;
4266 if (list == NULL || list->lv_len == 0)
4267 arg_idx = 0;
4268 }
4269 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004270 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004271 {
4272 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
4273
4274 /* result is a VAR_PARTIAL */
4275 if (pt == NULL)
4276 vim_free(name);
4277 else
4278 {
4279 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4280 {
4281 listitem_T *li;
4282 int i = 0;
4283 int arg_len = 0;
4284 int lv_len = 0;
4285
4286 if (arg_pt != NULL)
4287 arg_len = arg_pt->pt_argc;
4288 if (list != NULL)
4289 lv_len = list->lv_len;
4290 pt->pt_argc = arg_len + lv_len;
4291 pt->pt_argv = (typval_T *)alloc(
4292 sizeof(typval_T) * pt->pt_argc);
4293 if (pt->pt_argv == NULL)
4294 {
4295 vim_free(pt);
4296 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004297 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004298 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004299 for (i = 0; i < arg_len; i++)
4300 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4301 if (lv_len > 0)
4302 for (li = list->lv_first; li != NULL;
4303 li = li->li_next)
4304 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004305 }
4306
4307 /* For "function(dict.func, [], dict)" and "func" is a partial
4308 * use "dict". That is backwards compatible. */
4309 if (dict_idx > 0)
4310 {
4311 /* The dict is bound explicitly, pt_auto is FALSE. */
4312 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4313 ++pt->pt_dict->dv_refcount;
4314 }
4315 else if (arg_pt != NULL)
4316 {
4317 /* If the dict was bound automatically the result is also
4318 * bound automatically. */
4319 pt->pt_dict = arg_pt->pt_dict;
4320 pt->pt_auto = arg_pt->pt_auto;
4321 if (pt->pt_dict != NULL)
4322 ++pt->pt_dict->dv_refcount;
4323 }
4324
4325 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004326 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4327 {
4328 pt->pt_func = arg_pt->pt_func;
4329 func_ptr_ref(pt->pt_func);
4330 vim_free(name);
4331 }
4332 else if (is_funcref)
4333 {
4334 pt->pt_func = find_func(trans_name);
4335 func_ptr_ref(pt->pt_func);
4336 vim_free(name);
4337 }
4338 else
4339 {
4340 pt->pt_name = name;
4341 func_ref(name);
4342 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004343 }
4344 rettv->v_type = VAR_PARTIAL;
4345 rettv->vval.v_partial = pt;
4346 }
4347 else
4348 {
4349 /* result is a VAR_FUNC */
4350 rettv->v_type = VAR_FUNC;
4351 rettv->vval.v_string = name;
4352 func_ref(name);
4353 }
4354 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004355theend:
4356 vim_free(trans_name);
4357}
4358
4359/*
4360 * "funcref()" function
4361 */
4362 static void
4363f_funcref(typval_T *argvars, typval_T *rettv)
4364{
4365 common_function(argvars, rettv, TRUE);
4366}
4367
4368/*
4369 * "function()" function
4370 */
4371 static void
4372f_function(typval_T *argvars, typval_T *rettv)
4373{
4374 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004375}
4376
4377/*
4378 * "garbagecollect()" function
4379 */
4380 static void
4381f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4382{
4383 /* This is postponed until we are back at the toplevel, because we may be
4384 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4385 want_garbage_collect = TRUE;
4386
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004387 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004388 garbage_collect_at_exit = TRUE;
4389}
4390
4391/*
4392 * "get()" function
4393 */
4394 static void
4395f_get(typval_T *argvars, typval_T *rettv)
4396{
4397 listitem_T *li;
4398 list_T *l;
4399 dictitem_T *di;
4400 dict_T *d;
4401 typval_T *tv = NULL;
4402
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004403 if (argvars[0].v_type == VAR_BLOB)
4404 {
4405 int error = FALSE;
4406 int idx = tv_get_number_chk(&argvars[1], &error);
4407
4408 if (!error)
4409 {
4410 rettv->v_type = VAR_NUMBER;
4411 if (idx >= blob_len(argvars[0].vval.v_blob))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004412 semsg(_(e_blobidx), idx);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004413 else
4414 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
4415 }
4416 }
4417 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004418 {
4419 if ((l = argvars[0].vval.v_list) != NULL)
4420 {
4421 int error = FALSE;
4422
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004423 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004424 if (!error && li != NULL)
4425 tv = &li->li_tv;
4426 }
4427 }
4428 else if (argvars[0].v_type == VAR_DICT)
4429 {
4430 if ((d = argvars[0].vval.v_dict) != NULL)
4431 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004432 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004433 if (di != NULL)
4434 tv = &di->di_tv;
4435 }
4436 }
4437 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4438 {
4439 partial_T *pt;
4440 partial_T fref_pt;
4441
4442 if (argvars[0].v_type == VAR_PARTIAL)
4443 pt = argvars[0].vval.v_partial;
4444 else
4445 {
4446 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4447 fref_pt.pt_name = argvars[0].vval.v_string;
4448 pt = &fref_pt;
4449 }
4450
4451 if (pt != NULL)
4452 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004453 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004454 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004455
4456 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4457 {
4458 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004459 n = partial_name(pt);
4460 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004461 rettv->vval.v_string = NULL;
4462 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004463 {
4464 rettv->vval.v_string = vim_strsave(n);
4465 if (rettv->v_type == VAR_FUNC)
4466 func_ref(rettv->vval.v_string);
4467 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004468 }
4469 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004470 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004471 else if (STRCMP(what, "args") == 0)
4472 {
4473 rettv->v_type = VAR_LIST;
4474 if (rettv_list_alloc(rettv) == OK)
4475 {
4476 int i;
4477
4478 for (i = 0; i < pt->pt_argc; ++i)
4479 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4480 }
4481 }
4482 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004483 semsg(_(e_invarg2), what);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004484 return;
4485 }
4486 }
4487 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004488 semsg(_(e_listdictarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004489
4490 if (tv == NULL)
4491 {
4492 if (argvars[2].v_type != VAR_UNKNOWN)
4493 copy_tv(&argvars[2], rettv);
4494 }
4495 else
4496 copy_tv(tv, rettv);
4497}
4498
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004499/*
4500 * Returns buffer options, variables and other attributes in a dictionary.
4501 */
4502 static dict_T *
4503get_buffer_info(buf_T *buf)
4504{
4505 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004506 tabpage_T *tp;
4507 win_T *wp;
4508 list_T *windows;
4509
4510 dict = dict_alloc();
4511 if (dict == NULL)
4512 return NULL;
4513
Bram Moolenaare0be1672018-07-08 16:50:37 +02004514 dict_add_number(dict, "bufnr", buf->b_fnum);
4515 dict_add_string(dict, "name", buf->b_ffname);
4516 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4517 : buflist_findlnum(buf));
4518 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4519 dict_add_number(dict, "listed", buf->b_p_bl);
4520 dict_add_number(dict, "changed", bufIsChanged(buf));
4521 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4522 dict_add_number(dict, "hidden",
4523 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004524
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004525 /* Get a reference to buffer variables */
4526 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004527
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004528 /* List of windows displaying this buffer */
4529 windows = list_alloc();
4530 if (windows != NULL)
4531 {
4532 FOR_ALL_TAB_WINDOWS(tp, wp)
4533 if (wp->w_buffer == buf)
4534 list_append_number(windows, (varnumber_T)wp->w_id);
4535 dict_add_list(dict, "windows", windows);
4536 }
4537
4538#ifdef FEAT_SIGNS
4539 if (buf->b_signlist != NULL)
4540 {
4541 /* List of signs placed in this buffer */
4542 list_T *signs = list_alloc();
4543 if (signs != NULL)
4544 {
4545 get_buffer_signs(buf, signs);
4546 dict_add_list(dict, "signs", signs);
4547 }
4548 }
4549#endif
4550
4551 return dict;
4552}
4553
4554/*
4555 * "getbufinfo()" function
4556 */
4557 static void
4558f_getbufinfo(typval_T *argvars, typval_T *rettv)
4559{
4560 buf_T *buf = NULL;
4561 buf_T *argbuf = NULL;
4562 dict_T *d;
4563 int filtered = FALSE;
4564 int sel_buflisted = FALSE;
4565 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004566 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004567
4568 if (rettv_list_alloc(rettv) != OK)
4569 return;
4570
4571 /* List of all the buffers or selected buffers */
4572 if (argvars[0].v_type == VAR_DICT)
4573 {
4574 dict_T *sel_d = argvars[0].vval.v_dict;
4575
4576 if (sel_d != NULL)
4577 {
4578 dictitem_T *di;
4579
4580 filtered = TRUE;
4581
4582 di = dict_find(sel_d, (char_u *)"buflisted", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004583 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004584 sel_buflisted = TRUE;
4585
4586 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004587 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004588 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004589
4590 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004591 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004592 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004593 }
4594 }
4595 else if (argvars[0].v_type != VAR_UNKNOWN)
4596 {
4597 /* Information about one buffer. Argument specifies the buffer */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004598 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004599 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004600 argbuf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004601 --emsg_off;
4602 if (argbuf == NULL)
4603 return;
4604 }
4605
4606 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004607 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004608 {
4609 if (argbuf != NULL && argbuf != buf)
4610 continue;
4611 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004612 || (sel_buflisted && !buf->b_p_bl)
4613 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004614 continue;
4615
4616 d = get_buffer_info(buf);
4617 if (d != NULL)
4618 list_append_dict(rettv->vval.v_list, d);
4619 if (argbuf != NULL)
4620 return;
4621 }
4622}
4623
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004624/*
4625 * Get line or list of lines from buffer "buf" into "rettv".
4626 * Return a range (from start to end) of lines in rettv from the specified
4627 * buffer.
4628 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4629 */
4630 static void
4631get_buffer_lines(
4632 buf_T *buf,
4633 linenr_T start,
4634 linenr_T end,
4635 int retlist,
4636 typval_T *rettv)
4637{
4638 char_u *p;
4639
4640 rettv->v_type = VAR_STRING;
4641 rettv->vval.v_string = NULL;
4642 if (retlist && rettv_list_alloc(rettv) == FAIL)
4643 return;
4644
4645 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4646 return;
4647
4648 if (!retlist)
4649 {
4650 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4651 p = ml_get_buf(buf, start, FALSE);
4652 else
4653 p = (char_u *)"";
4654 rettv->vval.v_string = vim_strsave(p);
4655 }
4656 else
4657 {
4658 if (end < start)
4659 return;
4660
4661 if (start < 1)
4662 start = 1;
4663 if (end > buf->b_ml.ml_line_count)
4664 end = buf->b_ml.ml_line_count;
4665 while (start <= end)
4666 if (list_append_string(rettv->vval.v_list,
4667 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4668 break;
4669 }
4670}
4671
4672/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004673 * "getbufline()" function
4674 */
4675 static void
4676f_getbufline(typval_T *argvars, typval_T *rettv)
4677{
4678 linenr_T lnum;
4679 linenr_T end;
4680 buf_T *buf;
4681
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004682 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004683 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004684 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004685 --emsg_off;
4686
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004687 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004688 if (argvars[2].v_type == VAR_UNKNOWN)
4689 end = lnum;
4690 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004691 end = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004692
4693 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4694}
4695
4696/*
4697 * "getbufvar()" function
4698 */
4699 static void
4700f_getbufvar(typval_T *argvars, typval_T *rettv)
4701{
4702 buf_T *buf;
4703 buf_T *save_curbuf;
4704 char_u *varname;
4705 dictitem_T *v;
4706 int done = FALSE;
4707
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004708 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
4709 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004710 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004711 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004712
4713 rettv->v_type = VAR_STRING;
4714 rettv->vval.v_string = NULL;
4715
4716 if (buf != NULL && varname != NULL)
4717 {
4718 /* set curbuf to be our buf, temporarily */
4719 save_curbuf = curbuf;
4720 curbuf = buf;
4721
Bram Moolenaar30567352016-08-27 21:25:44 +02004722 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004723 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004724 if (varname[1] == NUL)
4725 {
4726 /* get all buffer-local options in a dict */
4727 dict_T *opts = get_winbuf_options(TRUE);
4728
4729 if (opts != NULL)
4730 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004731 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004732 done = TRUE;
4733 }
4734 }
4735 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4736 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004737 done = TRUE;
4738 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004739 else
4740 {
4741 /* Look up the variable. */
4742 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4743 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4744 'b', varname, FALSE);
4745 if (v != NULL)
4746 {
4747 copy_tv(&v->di_tv, rettv);
4748 done = TRUE;
4749 }
4750 }
4751
4752 /* restore previous notion of curbuf */
4753 curbuf = save_curbuf;
4754 }
4755
4756 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4757 /* use the default value */
4758 copy_tv(&argvars[2], rettv);
4759
4760 --emsg_off;
4761}
4762
4763/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004764 * "getchangelist()" function
4765 */
4766 static void
4767f_getchangelist(typval_T *argvars, typval_T *rettv)
4768{
4769#ifdef FEAT_JUMPLIST
4770 buf_T *buf;
4771 int i;
4772 list_T *l;
4773 dict_T *d;
4774#endif
4775
4776 if (rettv_list_alloc(rettv) != OK)
4777 return;
4778
4779#ifdef FEAT_JUMPLIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004780 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004781 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004782 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004783 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004784 if (buf == NULL)
4785 return;
4786
4787 l = list_alloc();
4788 if (l == NULL)
4789 return;
4790
4791 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4792 return;
4793 /*
4794 * The current window change list index tracks only the position in the
4795 * current buffer change list. For other buffers, use the change list
4796 * length as the current index.
4797 */
4798 list_append_number(rettv->vval.v_list,
4799 (varnumber_T)((buf == curwin->w_buffer)
4800 ? curwin->w_changelistidx : buf->b_changelistlen));
4801
4802 for (i = 0; i < buf->b_changelistlen; ++i)
4803 {
4804 if (buf->b_changelist[i].lnum == 0)
4805 continue;
4806 if ((d = dict_alloc()) == NULL)
4807 return;
4808 if (list_append_dict(l, d) == FAIL)
4809 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004810 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4811 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004812# ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +02004813 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004814# endif
4815 }
4816#endif
4817}
4818/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004819 * "getchar()" function
4820 */
4821 static void
4822f_getchar(typval_T *argvars, typval_T *rettv)
4823{
4824 varnumber_T n;
4825 int error = FALSE;
4826
Bram Moolenaar84d93902018-09-11 20:10:20 +02004827#ifdef MESSAGE_QUEUE
4828 // vpeekc() used to check for messages, but that caused problems, invoking
4829 // a callback where it was not expected. Some plugins use getchar(1) in a
4830 // loop to await a message, therefore make sure we check for messages here.
4831 parse_queued_messages();
4832#endif
4833
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004834 /* Position the cursor. Needed after a message that ends in a space. */
4835 windgoto(msg_row, msg_col);
4836
4837 ++no_mapping;
4838 ++allow_keys;
4839 for (;;)
4840 {
4841 if (argvars[0].v_type == VAR_UNKNOWN)
4842 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004843 n = plain_vgetc();
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004844 else if (tv_get_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004845 /* getchar(1): only check if char avail */
4846 n = vpeekc_any();
4847 else if (error || vpeekc_any() == NUL)
4848 /* illegal argument or getchar(0) and no char avail: return zero */
4849 n = 0;
4850 else
4851 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004852 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004853
4854 if (n == K_IGNORE)
4855 continue;
4856 break;
4857 }
4858 --no_mapping;
4859 --allow_keys;
4860
4861 set_vim_var_nr(VV_MOUSE_WIN, 0);
4862 set_vim_var_nr(VV_MOUSE_WINID, 0);
4863 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4864 set_vim_var_nr(VV_MOUSE_COL, 0);
4865
4866 rettv->vval.v_number = n;
4867 if (IS_SPECIAL(n) || mod_mask != 0)
4868 {
4869 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4870 int i = 0;
4871
4872 /* Turn a special key into three bytes, plus modifier. */
4873 if (mod_mask != 0)
4874 {
4875 temp[i++] = K_SPECIAL;
4876 temp[i++] = KS_MODIFIER;
4877 temp[i++] = mod_mask;
4878 }
4879 if (IS_SPECIAL(n))
4880 {
4881 temp[i++] = K_SPECIAL;
4882 temp[i++] = K_SECOND(n);
4883 temp[i++] = K_THIRD(n);
4884 }
4885#ifdef FEAT_MBYTE
4886 else if (has_mbyte)
4887 i += (*mb_char2bytes)(n, temp + i);
4888#endif
4889 else
4890 temp[i++] = n;
4891 temp[i++] = NUL;
4892 rettv->v_type = VAR_STRING;
4893 rettv->vval.v_string = vim_strsave(temp);
4894
4895#ifdef FEAT_MOUSE
4896 if (is_mouse_key(n))
4897 {
4898 int row = mouse_row;
4899 int col = mouse_col;
4900 win_T *win;
4901 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004902 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004903 int winnr = 1;
4904
4905 if (row >= 0 && col >= 0)
4906 {
4907 /* Find the window at the mouse coordinates and compute the
4908 * text position. */
4909 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004910 if (win == NULL)
4911 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004912 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004913 for (wp = firstwin; wp != win; wp = wp->w_next)
4914 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004915 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4916 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4917 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4918 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4919 }
4920 }
4921#endif
4922 }
4923}
4924
4925/*
4926 * "getcharmod()" function
4927 */
4928 static void
4929f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4930{
4931 rettv->vval.v_number = mod_mask;
4932}
4933
4934/*
4935 * "getcharsearch()" function
4936 */
4937 static void
4938f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4939{
4940 if (rettv_dict_alloc(rettv) != FAIL)
4941 {
4942 dict_T *dict = rettv->vval.v_dict;
4943
Bram Moolenaare0be1672018-07-08 16:50:37 +02004944 dict_add_string(dict, "char", last_csearch());
4945 dict_add_number(dict, "forward", last_csearch_forward());
4946 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004947 }
4948}
4949
4950/*
4951 * "getcmdline()" function
4952 */
4953 static void
4954f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4955{
4956 rettv->v_type = VAR_STRING;
4957 rettv->vval.v_string = get_cmdline_str();
4958}
4959
4960/*
4961 * "getcmdpos()" function
4962 */
4963 static void
4964f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4965{
4966 rettv->vval.v_number = get_cmdline_pos() + 1;
4967}
4968
4969/*
4970 * "getcmdtype()" function
4971 */
4972 static void
4973f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4974{
4975 rettv->v_type = VAR_STRING;
4976 rettv->vval.v_string = alloc(2);
4977 if (rettv->vval.v_string != NULL)
4978 {
4979 rettv->vval.v_string[0] = get_cmdline_type();
4980 rettv->vval.v_string[1] = NUL;
4981 }
4982}
4983
4984/*
4985 * "getcmdwintype()" function
4986 */
4987 static void
4988f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4989{
4990 rettv->v_type = VAR_STRING;
4991 rettv->vval.v_string = NULL;
4992#ifdef FEAT_CMDWIN
4993 rettv->vval.v_string = alloc(2);
4994 if (rettv->vval.v_string != NULL)
4995 {
4996 rettv->vval.v_string[0] = cmdwin_type;
4997 rettv->vval.v_string[1] = NUL;
4998 }
4999#endif
5000}
5001
5002#if defined(FEAT_CMDL_COMPL)
5003/*
5004 * "getcompletion()" function
5005 */
5006 static void
5007f_getcompletion(typval_T *argvars, typval_T *rettv)
5008{
5009 char_u *pat;
5010 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005011 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005012 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
5013 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005014
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005015 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005016 filtered = tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005017
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005018 if (p_wic)
5019 options |= WILD_ICASE;
5020
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005021 /* For filtered results, 'wildignore' is used */
5022 if (!filtered)
5023 options |= WILD_KEEP_ALL;
5024
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005025 ExpandInit(&xpc);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005026 xpc.xp_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005027 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005028 xpc.xp_context = cmdcomplete_str_to_type(tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005029 if (xpc.xp_context == EXPAND_NOTHING)
5030 {
5031 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005032 semsg(_(e_invarg2), argvars[1].vval.v_string);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005033 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005034 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005035 return;
5036 }
5037
5038# if defined(FEAT_MENU)
5039 if (xpc.xp_context == EXPAND_MENUS)
5040 {
5041 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
5042 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5043 }
5044# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02005045#ifdef FEAT_CSCOPE
5046 if (xpc.xp_context == EXPAND_CSCOPE)
5047 {
5048 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
5049 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5050 }
5051#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02005052#ifdef FEAT_SIGNS
5053 if (xpc.xp_context == EXPAND_SIGN)
5054 {
5055 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
5056 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5057 }
5058#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005059
5060 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
5061 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
5062 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005063 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005064
5065 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
5066
5067 for (i = 0; i < xpc.xp_numfiles; i++)
5068 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5069 }
5070 vim_free(pat);
5071 ExpandCleanup(&xpc);
5072}
5073#endif
5074
5075/*
5076 * "getcwd()" function
5077 */
5078 static void
5079f_getcwd(typval_T *argvars, typval_T *rettv)
5080{
5081 win_T *wp = NULL;
5082 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01005083 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005084
5085 rettv->v_type = VAR_STRING;
5086 rettv->vval.v_string = NULL;
5087
Bram Moolenaar54591292018-02-09 20:53:59 +01005088 if (argvars[0].v_type == VAR_NUMBER && argvars[0].vval.v_number == -1)
5089 global = TRUE;
5090 else
5091 wp = find_tabwin(&argvars[0], &argvars[1]);
5092
5093 if (wp != NULL && wp->w_localdir != NULL)
5094 rettv->vval.v_string = vim_strsave(wp->w_localdir);
5095 else if (wp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005096 {
Bram Moolenaar54591292018-02-09 20:53:59 +01005097 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005098 rettv->vval.v_string = vim_strsave(globaldir);
5099 else
5100 {
5101 cwd = alloc(MAXPATHL);
5102 if (cwd != NULL)
5103 {
5104 if (mch_dirname(cwd, MAXPATHL) != FAIL)
5105 rettv->vval.v_string = vim_strsave(cwd);
5106 vim_free(cwd);
5107 }
5108 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005109 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02005110#ifdef BACKSLASH_IN_FILENAME
5111 if (rettv->vval.v_string != NULL)
5112 slash_adjust(rettv->vval.v_string);
5113#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005114}
5115
5116/*
5117 * "getfontname()" function
5118 */
5119 static void
5120f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
5121{
5122 rettv->v_type = VAR_STRING;
5123 rettv->vval.v_string = NULL;
5124#ifdef FEAT_GUI
5125 if (gui.in_use)
5126 {
5127 GuiFont font;
5128 char_u *name = NULL;
5129
5130 if (argvars[0].v_type == VAR_UNKNOWN)
5131 {
5132 /* Get the "Normal" font. Either the name saved by
5133 * hl_set_font_name() or from the font ID. */
5134 font = gui.norm_font;
5135 name = hl_get_font_name();
5136 }
5137 else
5138 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005139 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005140 if (STRCMP(name, "*") == 0) /* don't use font dialog */
5141 return;
5142 font = gui_mch_get_font(name, FALSE);
5143 if (font == NOFONT)
5144 return; /* Invalid font name, return empty string. */
5145 }
5146 rettv->vval.v_string = gui_mch_get_fontname(font, name);
5147 if (argvars[0].v_type != VAR_UNKNOWN)
5148 gui_mch_free_font(font);
5149 }
5150#endif
5151}
5152
5153/*
5154 * "getfperm({fname})" function
5155 */
5156 static void
5157f_getfperm(typval_T *argvars, typval_T *rettv)
5158{
5159 char_u *fname;
5160 stat_T st;
5161 char_u *perm = NULL;
5162 char_u flags[] = "rwx";
5163 int i;
5164
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005165 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005166
5167 rettv->v_type = VAR_STRING;
5168 if (mch_stat((char *)fname, &st) >= 0)
5169 {
5170 perm = vim_strsave((char_u *)"---------");
5171 if (perm != NULL)
5172 {
5173 for (i = 0; i < 9; i++)
5174 {
5175 if (st.st_mode & (1 << (8 - i)))
5176 perm[i] = flags[i % 3];
5177 }
5178 }
5179 }
5180 rettv->vval.v_string = perm;
5181}
5182
5183/*
5184 * "getfsize({fname})" function
5185 */
5186 static void
5187f_getfsize(typval_T *argvars, typval_T *rettv)
5188{
5189 char_u *fname;
5190 stat_T st;
5191
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005192 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005193
5194 rettv->v_type = VAR_NUMBER;
5195
5196 if (mch_stat((char *)fname, &st) >= 0)
5197 {
5198 if (mch_isdir(fname))
5199 rettv->vval.v_number = 0;
5200 else
5201 {
5202 rettv->vval.v_number = (varnumber_T)st.st_size;
5203
5204 /* non-perfect check for overflow */
5205 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5206 rettv->vval.v_number = -2;
5207 }
5208 }
5209 else
5210 rettv->vval.v_number = -1;
5211}
5212
5213/*
5214 * "getftime({fname})" function
5215 */
5216 static void
5217f_getftime(typval_T *argvars, typval_T *rettv)
5218{
5219 char_u *fname;
5220 stat_T st;
5221
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005222 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005223
5224 if (mch_stat((char *)fname, &st) >= 0)
5225 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5226 else
5227 rettv->vval.v_number = -1;
5228}
5229
5230/*
5231 * "getftype({fname})" function
5232 */
5233 static void
5234f_getftype(typval_T *argvars, typval_T *rettv)
5235{
5236 char_u *fname;
5237 stat_T st;
5238 char_u *type = NULL;
5239 char *t;
5240
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005241 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005242
5243 rettv->v_type = VAR_STRING;
5244 if (mch_lstat((char *)fname, &st) >= 0)
5245 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005246 if (S_ISREG(st.st_mode))
5247 t = "file";
5248 else if (S_ISDIR(st.st_mode))
5249 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005250 else if (S_ISLNK(st.st_mode))
5251 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005252 else if (S_ISBLK(st.st_mode))
5253 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005254 else if (S_ISCHR(st.st_mode))
5255 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005256 else if (S_ISFIFO(st.st_mode))
5257 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005258 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005259 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005260 else
5261 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005262 type = vim_strsave((char_u *)t);
5263 }
5264 rettv->vval.v_string = type;
5265}
5266
5267/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005268 * "getjumplist()" function
5269 */
5270 static void
5271f_getjumplist(typval_T *argvars, typval_T *rettv)
5272{
5273#ifdef FEAT_JUMPLIST
5274 win_T *wp;
5275 int i;
5276 list_T *l;
5277 dict_T *d;
5278#endif
5279
5280 if (rettv_list_alloc(rettv) != OK)
5281 return;
5282
5283#ifdef FEAT_JUMPLIST
5284 wp = find_tabwin(&argvars[0], &argvars[1]);
5285 if (wp == NULL)
5286 return;
5287
5288 l = list_alloc();
5289 if (l == NULL)
5290 return;
5291
5292 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5293 return;
5294 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5295
Bram Moolenaar48679742018-02-13 13:33:29 +01005296 cleanup_jumplist(wp, TRUE);
5297
Bram Moolenaar4f505882018-02-10 21:06:32 +01005298 for (i = 0; i < wp->w_jumplistlen; ++i)
5299 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005300 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5301 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005302 if ((d = dict_alloc()) == NULL)
5303 return;
5304 if (list_append_dict(l, d) == FAIL)
5305 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005306 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5307 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005308# ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +02005309 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005310# endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005311 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005312 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005313 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005314 }
5315#endif
5316}
5317
5318/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005319 * "getline(lnum, [end])" function
5320 */
5321 static void
5322f_getline(typval_T *argvars, typval_T *rettv)
5323{
5324 linenr_T lnum;
5325 linenr_T end;
5326 int retlist;
5327
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005328 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005329 if (argvars[1].v_type == VAR_UNKNOWN)
5330 {
5331 end = 0;
5332 retlist = FALSE;
5333 }
5334 else
5335 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005336 end = tv_get_lnum(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005337 retlist = TRUE;
5338 }
5339
5340 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5341}
5342
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005343#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005344 static void
5345get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5346{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005347 if (what_arg->v_type == VAR_UNKNOWN)
5348 {
5349 if (rettv_list_alloc(rettv) == OK)
5350 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005351 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005352 }
5353 else
5354 {
5355 if (rettv_dict_alloc(rettv) == OK)
5356 if (is_qf || (wp != NULL))
5357 {
5358 if (what_arg->v_type == VAR_DICT)
5359 {
5360 dict_T *d = what_arg->vval.v_dict;
5361
5362 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005363 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005364 }
5365 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005366 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +02005367 }
5368 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005369}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005370#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005371
5372/*
5373 * "getloclist()" function
5374 */
5375 static void
5376f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5377{
5378#ifdef FEAT_QUICKFIX
5379 win_T *wp;
5380
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005381 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005382 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5383#endif
5384}
5385
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005386/*
5387 * "getmatches()" function
5388 */
5389 static void
5390f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5391{
5392#ifdef FEAT_SEARCH_EXTRA
5393 dict_T *dict;
5394 matchitem_T *cur = curwin->w_match_head;
5395 int i;
5396
5397 if (rettv_list_alloc(rettv) == OK)
5398 {
5399 while (cur != NULL)
5400 {
5401 dict = dict_alloc();
5402 if (dict == NULL)
5403 return;
5404 if (cur->match.regprog == NULL)
5405 {
5406 /* match added with matchaddpos() */
5407 for (i = 0; i < MAXPOSMATCH; ++i)
5408 {
5409 llpos_T *llpos;
5410 char buf[6];
5411 list_T *l;
5412
5413 llpos = &cur->pos.pos[i];
5414 if (llpos->lnum == 0)
5415 break;
5416 l = list_alloc();
5417 if (l == NULL)
5418 break;
5419 list_append_number(l, (varnumber_T)llpos->lnum);
5420 if (llpos->col > 0)
5421 {
5422 list_append_number(l, (varnumber_T)llpos->col);
5423 list_append_number(l, (varnumber_T)llpos->len);
5424 }
5425 sprintf(buf, "pos%d", i + 1);
5426 dict_add_list(dict, buf, l);
5427 }
5428 }
5429 else
5430 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02005431 dict_add_string(dict, "pattern", cur->pattern);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005432 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02005433 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
5434 dict_add_number(dict, "priority", (long)cur->priority);
5435 dict_add_number(dict, "id", (long)cur->id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005436# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
5437 if (cur->conceal_char)
5438 {
5439 char_u buf[MB_MAXBYTES + 1];
5440
5441 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005442 dict_add_string(dict, "conceal", (char_u *)&buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005443 }
5444# endif
5445 list_append_dict(rettv->vval.v_list, dict);
5446 cur = cur->next;
5447 }
5448 }
5449#endif
5450}
5451
5452/*
5453 * "getpid()" function
5454 */
5455 static void
5456f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5457{
5458 rettv->vval.v_number = mch_get_pid();
5459}
5460
5461 static void
5462getpos_both(
5463 typval_T *argvars,
5464 typval_T *rettv,
5465 int getcurpos)
5466{
5467 pos_T *fp;
5468 list_T *l;
5469 int fnum = -1;
5470
5471 if (rettv_list_alloc(rettv) == OK)
5472 {
5473 l = rettv->vval.v_list;
5474 if (getcurpos)
5475 fp = &curwin->w_cursor;
5476 else
5477 fp = var2fpos(&argvars[0], TRUE, &fnum);
5478 if (fnum != -1)
5479 list_append_number(l, (varnumber_T)fnum);
5480 else
5481 list_append_number(l, (varnumber_T)0);
5482 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5483 : (varnumber_T)0);
5484 list_append_number(l, (fp != NULL)
5485 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5486 : (varnumber_T)0);
5487 list_append_number(l,
5488#ifdef FEAT_VIRTUALEDIT
5489 (fp != NULL) ? (varnumber_T)fp->coladd :
5490#endif
5491 (varnumber_T)0);
5492 if (getcurpos)
5493 {
5494 update_curswant();
5495 list_append_number(l, curwin->w_curswant == MAXCOL ?
5496 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
5497 }
5498 }
5499 else
5500 rettv->vval.v_number = FALSE;
5501}
5502
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005503/*
5504 * "getcurpos()" function
5505 */
5506 static void
5507f_getcurpos(typval_T *argvars, typval_T *rettv)
5508{
5509 getpos_both(argvars, rettv, TRUE);
5510}
5511
5512/*
5513 * "getpos(string)" function
5514 */
5515 static void
5516f_getpos(typval_T *argvars, typval_T *rettv)
5517{
5518 getpos_both(argvars, rettv, FALSE);
5519}
5520
5521/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005522 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005523 */
5524 static void
5525f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5526{
5527#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005528 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005529#endif
5530}
5531
5532/*
5533 * "getreg()" function
5534 */
5535 static void
5536f_getreg(typval_T *argvars, typval_T *rettv)
5537{
5538 char_u *strregname;
5539 int regname;
5540 int arg2 = FALSE;
5541 int return_list = FALSE;
5542 int error = FALSE;
5543
5544 if (argvars[0].v_type != VAR_UNKNOWN)
5545 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005546 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005547 error = strregname == NULL;
5548 if (argvars[1].v_type != VAR_UNKNOWN)
5549 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005550 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005551 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005552 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005553 }
5554 }
5555 else
5556 strregname = get_vim_var_str(VV_REG);
5557
5558 if (error)
5559 return;
5560
5561 regname = (strregname == NULL ? '"' : *strregname);
5562 if (regname == 0)
5563 regname = '"';
5564
5565 if (return_list)
5566 {
5567 rettv->v_type = VAR_LIST;
5568 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5569 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5570 if (rettv->vval.v_list == NULL)
5571 (void)rettv_list_alloc(rettv);
5572 else
5573 ++rettv->vval.v_list->lv_refcount;
5574 }
5575 else
5576 {
5577 rettv->v_type = VAR_STRING;
5578 rettv->vval.v_string = get_reg_contents(regname,
5579 arg2 ? GREG_EXPR_SRC : 0);
5580 }
5581}
5582
5583/*
5584 * "getregtype()" function
5585 */
5586 static void
5587f_getregtype(typval_T *argvars, typval_T *rettv)
5588{
5589 char_u *strregname;
5590 int regname;
5591 char_u buf[NUMBUFLEN + 2];
5592 long reglen = 0;
5593
5594 if (argvars[0].v_type != VAR_UNKNOWN)
5595 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005596 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005597 if (strregname == NULL) /* type error; errmsg already given */
5598 {
5599 rettv->v_type = VAR_STRING;
5600 rettv->vval.v_string = NULL;
5601 return;
5602 }
5603 }
5604 else
5605 /* Default to v:register */
5606 strregname = get_vim_var_str(VV_REG);
5607
5608 regname = (strregname == NULL ? '"' : *strregname);
5609 if (regname == 0)
5610 regname = '"';
5611
5612 buf[0] = NUL;
5613 buf[1] = NUL;
5614 switch (get_reg_type(regname, &reglen))
5615 {
5616 case MLINE: buf[0] = 'V'; break;
5617 case MCHAR: buf[0] = 'v'; break;
5618 case MBLOCK:
5619 buf[0] = Ctrl_V;
5620 sprintf((char *)buf + 1, "%ld", reglen + 1);
5621 break;
5622 }
5623 rettv->v_type = VAR_STRING;
5624 rettv->vval.v_string = vim_strsave(buf);
5625}
5626
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005627/*
5628 * Returns information (variables, options, etc.) about a tab page
5629 * as a dictionary.
5630 */
5631 static dict_T *
5632get_tabpage_info(tabpage_T *tp, int tp_idx)
5633{
5634 win_T *wp;
5635 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005636 list_T *l;
5637
5638 dict = dict_alloc();
5639 if (dict == NULL)
5640 return NULL;
5641
Bram Moolenaare0be1672018-07-08 16:50:37 +02005642 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005643
5644 l = list_alloc();
5645 if (l != NULL)
5646 {
5647 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5648 wp; wp = wp->w_next)
5649 list_append_number(l, (varnumber_T)wp->w_id);
5650 dict_add_list(dict, "windows", l);
5651 }
5652
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005653 /* Make a reference to tabpage variables */
5654 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005655
5656 return dict;
5657}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005658
5659/*
5660 * "gettabinfo()" function
5661 */
5662 static void
5663f_gettabinfo(typval_T *argvars, typval_T *rettv)
5664{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005665 tabpage_T *tp, *tparg = NULL;
5666 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005667 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005668
5669 if (rettv_list_alloc(rettv) != OK)
5670 return;
5671
5672 if (argvars[0].v_type != VAR_UNKNOWN)
5673 {
5674 /* Information about one tab page */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005675 tparg = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005676 if (tparg == NULL)
5677 return;
5678 }
5679
5680 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005681 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005682 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005683 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005684 if (tparg != NULL && tp != tparg)
5685 continue;
5686 d = get_tabpage_info(tp, tpnr);
5687 if (d != NULL)
5688 list_append_dict(rettv->vval.v_list, d);
5689 if (tparg != NULL)
5690 return;
5691 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005692}
5693
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005694/*
5695 * "gettabvar()" function
5696 */
5697 static void
5698f_gettabvar(typval_T *argvars, typval_T *rettv)
5699{
5700 win_T *oldcurwin;
5701 tabpage_T *tp, *oldtabpage;
5702 dictitem_T *v;
5703 char_u *varname;
5704 int done = FALSE;
5705
5706 rettv->v_type = VAR_STRING;
5707 rettv->vval.v_string = NULL;
5708
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005709 varname = tv_get_string_chk(&argvars[1]);
5710 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005711 if (tp != NULL && varname != NULL)
5712 {
5713 /* Set tp to be our tabpage, temporarily. Also set the window to the
5714 * first window in the tabpage, otherwise the window is not valid. */
5715 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005716 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5717 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005718 {
5719 /* look up the variable */
5720 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5721 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5722 if (v != NULL)
5723 {
5724 copy_tv(&v->di_tv, rettv);
5725 done = TRUE;
5726 }
5727 }
5728
5729 /* restore previous notion of curwin */
5730 restore_win(oldcurwin, oldtabpage, TRUE);
5731 }
5732
5733 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5734 copy_tv(&argvars[2], rettv);
5735}
5736
5737/*
5738 * "gettabwinvar()" function
5739 */
5740 static void
5741f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5742{
5743 getwinvar(argvars, rettv, 1);
5744}
5745
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005746/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01005747 * "gettagstack()" function
5748 */
5749 static void
5750f_gettagstack(typval_T *argvars, typval_T *rettv)
5751{
5752 win_T *wp = curwin; // default is current window
5753
5754 if (rettv_dict_alloc(rettv) != OK)
5755 return;
5756
5757 if (argvars[0].v_type != VAR_UNKNOWN)
5758 {
5759 wp = find_win_by_nr_or_id(&argvars[0]);
5760 if (wp == NULL)
5761 return;
5762 }
5763
5764 get_tagstack(wp, rettv->vval.v_dict);
5765}
5766
5767/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005768 * Returns information about a window as a dictionary.
5769 */
5770 static dict_T *
5771get_win_info(win_T *wp, short tpnr, short winnr)
5772{
5773 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005774
5775 dict = dict_alloc();
5776 if (dict == NULL)
5777 return NULL;
5778
Bram Moolenaare0be1672018-07-08 16:50:37 +02005779 dict_add_number(dict, "tabnr", tpnr);
5780 dict_add_number(dict, "winnr", winnr);
5781 dict_add_number(dict, "winid", wp->w_id);
5782 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005783 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005784#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02005785 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005786#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005787 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005788 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005789 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005790
Bram Moolenaar69905d12017-08-13 18:14:47 +02005791#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02005792 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02005793#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005794#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02005795 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
5796 dict_add_number(dict, "loclist",
5797 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02005798#endif
5799
Bram Moolenaar30567352016-08-27 21:25:44 +02005800 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005801 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005802
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005803 return dict;
5804}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005805
5806/*
5807 * "getwininfo()" function
5808 */
5809 static void
5810f_getwininfo(typval_T *argvars, typval_T *rettv)
5811{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005812 tabpage_T *tp;
5813 win_T *wp = NULL, *wparg = NULL;
5814 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005815 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005816
5817 if (rettv_list_alloc(rettv) != OK)
5818 return;
5819
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005820 if (argvars[0].v_type != VAR_UNKNOWN)
5821 {
5822 wparg = win_id2wp(argvars);
5823 if (wparg == NULL)
5824 return;
5825 }
5826
5827 /* Collect information about either all the windows across all the tab
5828 * pages or one particular window.
5829 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005830 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005831 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005832 tabnr++;
5833 winnr = 0;
5834 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005835 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005836 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005837 if (wparg != NULL && wp != wparg)
5838 continue;
5839 d = get_win_info(wp, tabnr, winnr);
5840 if (d != NULL)
5841 list_append_dict(rettv->vval.v_list, d);
5842 if (wparg != NULL)
5843 /* found information about a specific window */
5844 return;
5845 }
5846 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005847}
5848
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005849/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005850 * "win_findbuf()" function
5851 */
5852 static void
5853f_win_findbuf(typval_T *argvars, typval_T *rettv)
5854{
5855 if (rettv_list_alloc(rettv) != FAIL)
5856 win_findbuf(argvars, rettv->vval.v_list);
5857}
5858
5859/*
5860 * "win_getid()" function
5861 */
5862 static void
5863f_win_getid(typval_T *argvars, typval_T *rettv)
5864{
5865 rettv->vval.v_number = win_getid(argvars);
5866}
5867
5868/*
5869 * "win_gotoid()" function
5870 */
5871 static void
5872f_win_gotoid(typval_T *argvars, typval_T *rettv)
5873{
5874 rettv->vval.v_number = win_gotoid(argvars);
5875}
5876
5877/*
5878 * "win_id2tabwin()" function
5879 */
5880 static void
5881f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5882{
5883 if (rettv_list_alloc(rettv) != FAIL)
5884 win_id2tabwin(argvars, rettv->vval.v_list);
5885}
5886
5887/*
5888 * "win_id2win()" function
5889 */
5890 static void
5891f_win_id2win(typval_T *argvars, typval_T *rettv)
5892{
5893 rettv->vval.v_number = win_id2win(argvars);
5894}
5895
5896/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005897 * "win_screenpos()" function
5898 */
5899 static void
5900f_win_screenpos(typval_T *argvars, typval_T *rettv)
5901{
5902 win_T *wp;
5903
5904 if (rettv_list_alloc(rettv) == FAIL)
5905 return;
5906
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005907 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005908 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5909 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5910}
5911
5912/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005913 * "getwinpos({timeout})" function
5914 */
5915 static void
5916f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
5917{
5918 int x = -1;
5919 int y = -1;
5920
5921 if (rettv_list_alloc(rettv) == FAIL)
5922 return;
5923#ifdef FEAT_GUI
5924 if (gui.in_use)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01005925 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005926# if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5927 else
5928# endif
5929#endif
5930#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5931 {
5932 varnumber_T timeout = 100;
5933
5934 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005935 timeout = tv_get_number(&argvars[0]);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005936 term_get_winpos(&x, &y, timeout);
5937 }
5938#endif
5939 list_append_number(rettv->vval.v_list, (varnumber_T)x);
5940 list_append_number(rettv->vval.v_list, (varnumber_T)y);
5941}
5942
5943
5944/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005945 * "getwinposx()" function
5946 */
5947 static void
5948f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5949{
5950 rettv->vval.v_number = -1;
5951#ifdef FEAT_GUI
5952 if (gui.in_use)
5953 {
5954 int x, y;
5955
5956 if (gui_mch_get_winpos(&x, &y) == OK)
5957 rettv->vval.v_number = x;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005958 return;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005959 }
5960#endif
5961#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5962 {
5963 int x, y;
5964
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005965 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005966 rettv->vval.v_number = x;
5967 }
5968#endif
5969}
5970
5971/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005972 * "getwinposy()" function
5973 */
5974 static void
5975f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5976{
5977 rettv->vval.v_number = -1;
5978#ifdef FEAT_GUI
5979 if (gui.in_use)
5980 {
5981 int x, y;
5982
5983 if (gui_mch_get_winpos(&x, &y) == OK)
5984 rettv->vval.v_number = y;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005985 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005986 }
5987#endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005988#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5989 {
5990 int x, y;
5991
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005992 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005993 rettv->vval.v_number = y;
5994 }
5995#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005996}
5997
5998/*
5999 * "getwinvar()" function
6000 */
6001 static void
6002f_getwinvar(typval_T *argvars, typval_T *rettv)
6003{
6004 getwinvar(argvars, rettv, 0);
6005}
6006
6007/*
6008 * "glob()" function
6009 */
6010 static void
6011f_glob(typval_T *argvars, typval_T *rettv)
6012{
6013 int options = WILD_SILENT|WILD_USE_NL;
6014 expand_T xpc;
6015 int error = FALSE;
6016
6017 /* When the optional second argument is non-zero, don't remove matches
6018 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6019 rettv->v_type = VAR_STRING;
6020 if (argvars[1].v_type != VAR_UNKNOWN)
6021 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006022 if (tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006023 options |= WILD_KEEP_ALL;
6024 if (argvars[2].v_type != VAR_UNKNOWN)
6025 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006026 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006027 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006028 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006029 }
6030 if (argvars[3].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006031 && tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006032 options |= WILD_ALLLINKS;
6033 }
6034 }
6035 if (!error)
6036 {
6037 ExpandInit(&xpc);
6038 xpc.xp_context = EXPAND_FILES;
6039 if (p_wic)
6040 options += WILD_ICASE;
6041 if (rettv->v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006042 rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006043 NULL, options, WILD_ALL);
6044 else if (rettv_list_alloc(rettv) != FAIL)
6045 {
6046 int i;
6047
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006048 ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006049 NULL, options, WILD_ALL_KEEP);
6050 for (i = 0; i < xpc.xp_numfiles; i++)
6051 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
6052
6053 ExpandCleanup(&xpc);
6054 }
6055 }
6056 else
6057 rettv->vval.v_string = NULL;
6058}
6059
6060/*
6061 * "globpath()" function
6062 */
6063 static void
6064f_globpath(typval_T *argvars, typval_T *rettv)
6065{
6066 int flags = 0;
6067 char_u buf1[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006068 char_u *file = tv_get_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006069 int error = FALSE;
6070 garray_T ga;
6071 int i;
6072
6073 /* When the optional second argument is non-zero, don't remove matches
6074 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6075 rettv->v_type = VAR_STRING;
6076 if (argvars[2].v_type != VAR_UNKNOWN)
6077 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006078 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006079 flags |= WILD_KEEP_ALL;
6080 if (argvars[3].v_type != VAR_UNKNOWN)
6081 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006082 if (tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006083 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006084 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006085 }
6086 if (argvars[4].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006087 && tv_get_number_chk(&argvars[4], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006088 flags |= WILD_ALLLINKS;
6089 }
6090 }
6091 if (file != NULL && !error)
6092 {
6093 ga_init2(&ga, (int)sizeof(char_u *), 10);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006094 globpath(tv_get_string(&argvars[0]), file, &ga, flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006095 if (rettv->v_type == VAR_STRING)
6096 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
6097 else if (rettv_list_alloc(rettv) != FAIL)
6098 for (i = 0; i < ga.ga_len; ++i)
6099 list_append_string(rettv->vval.v_list,
6100 ((char_u **)(ga.ga_data))[i], -1);
6101 ga_clear_strings(&ga);
6102 }
6103 else
6104 rettv->vval.v_string = NULL;
6105}
6106
6107/*
6108 * "glob2regpat()" function
6109 */
6110 static void
6111f_glob2regpat(typval_T *argvars, typval_T *rettv)
6112{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006113 char_u *pat = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006114
6115 rettv->v_type = VAR_STRING;
6116 rettv->vval.v_string = (pat == NULL)
6117 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
6118}
6119
6120/* for VIM_VERSION_ defines */
6121#include "version.h"
6122
6123/*
6124 * "has()" function
6125 */
6126 static void
6127f_has(typval_T *argvars, typval_T *rettv)
6128{
6129 int i;
6130 char_u *name;
6131 int n = FALSE;
6132 static char *(has_list[]) =
6133 {
6134#ifdef AMIGA
6135 "amiga",
6136# ifdef FEAT_ARP
6137 "arp",
6138# endif
6139#endif
6140#ifdef __BEOS__
6141 "beos",
6142#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02006143#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01006144 "mac", /* Mac OS X (and, once, Mac OS Classic) */
6145 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02006146# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01006147 "macunix", /* Mac OS X, with the darwin feature */
6148 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02006149# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006150#endif
6151#ifdef __QNX__
6152 "qnx",
6153#endif
6154#ifdef UNIX
6155 "unix",
6156#endif
6157#ifdef VMS
6158 "vms",
6159#endif
6160#ifdef WIN32
6161 "win32",
6162#endif
6163#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
6164 "win32unix",
6165#endif
6166#if defined(WIN64) || defined(_WIN64)
6167 "win64",
6168#endif
6169#ifdef EBCDIC
6170 "ebcdic",
6171#endif
6172#ifndef CASE_INSENSITIVE_FILENAME
6173 "fname_case",
6174#endif
6175#ifdef HAVE_ACL
6176 "acl",
6177#endif
6178#ifdef FEAT_ARABIC
6179 "arabic",
6180#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006181 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006182#ifdef FEAT_AUTOCHDIR
6183 "autochdir",
6184#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006185#ifdef FEAT_AUTOSERVERNAME
6186 "autoservername",
6187#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006188#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006189 "balloon_eval",
6190# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
6191 "balloon_multiline",
6192# endif
6193#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006194#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006195 "balloon_eval_term",
6196#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006197#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6198 "builtin_terms",
6199# ifdef ALL_BUILTIN_TCAPS
6200 "all_builtin_terms",
6201# endif
6202#endif
6203#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
6204 || defined(FEAT_GUI_W32) \
6205 || defined(FEAT_GUI_MOTIF))
6206 "browsefilter",
6207#endif
6208#ifdef FEAT_BYTEOFF
6209 "byte_offset",
6210#endif
6211#ifdef FEAT_JOB_CHANNEL
6212 "channel",
6213#endif
6214#ifdef FEAT_CINDENT
6215 "cindent",
6216#endif
6217#ifdef FEAT_CLIENTSERVER
6218 "clientserver",
6219#endif
6220#ifdef FEAT_CLIPBOARD
6221 "clipboard",
6222#endif
6223#ifdef FEAT_CMDL_COMPL
6224 "cmdline_compl",
6225#endif
6226#ifdef FEAT_CMDHIST
6227 "cmdline_hist",
6228#endif
6229#ifdef FEAT_COMMENTS
6230 "comments",
6231#endif
6232#ifdef FEAT_CONCEAL
6233 "conceal",
6234#endif
6235#ifdef FEAT_CRYPT
6236 "cryptv",
6237 "crypt-blowfish",
6238 "crypt-blowfish2",
6239#endif
6240#ifdef FEAT_CSCOPE
6241 "cscope",
6242#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006243 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006244#ifdef CURSOR_SHAPE
6245 "cursorshape",
6246#endif
6247#ifdef DEBUG
6248 "debug",
6249#endif
6250#ifdef FEAT_CON_DIALOG
6251 "dialog_con",
6252#endif
6253#ifdef FEAT_GUI_DIALOG
6254 "dialog_gui",
6255#endif
6256#ifdef FEAT_DIFF
6257 "diff",
6258#endif
6259#ifdef FEAT_DIGRAPHS
6260 "digraphs",
6261#endif
6262#ifdef FEAT_DIRECTX
6263 "directx",
6264#endif
6265#ifdef FEAT_DND
6266 "dnd",
6267#endif
6268#ifdef FEAT_EMACS_TAGS
6269 "emacs_tags",
6270#endif
6271 "eval", /* always present, of course! */
6272 "ex_extra", /* graduated feature */
6273#ifdef FEAT_SEARCH_EXTRA
6274 "extra_search",
6275#endif
6276#ifdef FEAT_FKMAP
6277 "farsi",
6278#endif
6279#ifdef FEAT_SEARCHPATH
6280 "file_in_path",
6281#endif
6282#ifdef FEAT_FILTERPIPE
6283 "filterpipe",
6284#endif
6285#ifdef FEAT_FIND_ID
6286 "find_in_path",
6287#endif
6288#ifdef FEAT_FLOAT
6289 "float",
6290#endif
6291#ifdef FEAT_FOLDING
6292 "folding",
6293#endif
6294#ifdef FEAT_FOOTER
6295 "footer",
6296#endif
6297#if !defined(USE_SYSTEM) && defined(UNIX)
6298 "fork",
6299#endif
6300#ifdef FEAT_GETTEXT
6301 "gettext",
6302#endif
6303#ifdef FEAT_GUI
6304 "gui",
6305#endif
6306#ifdef FEAT_GUI_ATHENA
6307# ifdef FEAT_GUI_NEXTAW
6308 "gui_neXtaw",
6309# else
6310 "gui_athena",
6311# endif
6312#endif
6313#ifdef FEAT_GUI_GTK
6314 "gui_gtk",
6315# ifdef USE_GTK3
6316 "gui_gtk3",
6317# else
6318 "gui_gtk2",
6319# endif
6320#endif
6321#ifdef FEAT_GUI_GNOME
6322 "gui_gnome",
6323#endif
6324#ifdef FEAT_GUI_MAC
6325 "gui_mac",
6326#endif
6327#ifdef FEAT_GUI_MOTIF
6328 "gui_motif",
6329#endif
6330#ifdef FEAT_GUI_PHOTON
6331 "gui_photon",
6332#endif
6333#ifdef FEAT_GUI_W32
6334 "gui_win32",
6335#endif
6336#ifdef FEAT_HANGULIN
6337 "hangul_input",
6338#endif
6339#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6340 "iconv",
6341#endif
6342#ifdef FEAT_INS_EXPAND
6343 "insert_expand",
6344#endif
6345#ifdef FEAT_JOB_CHANNEL
6346 "job",
6347#endif
6348#ifdef FEAT_JUMPLIST
6349 "jumplist",
6350#endif
6351#ifdef FEAT_KEYMAP
6352 "keymap",
6353#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006354 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006355#ifdef FEAT_LANGMAP
6356 "langmap",
6357#endif
6358#ifdef FEAT_LIBCALL
6359 "libcall",
6360#endif
6361#ifdef FEAT_LINEBREAK
6362 "linebreak",
6363#endif
6364#ifdef FEAT_LISP
6365 "lispindent",
6366#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006367 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006368#ifdef FEAT_LOCALMAP
6369 "localmap",
6370#endif
6371#ifdef FEAT_LUA
6372# ifndef DYNAMIC_LUA
6373 "lua",
6374# endif
6375#endif
6376#ifdef FEAT_MENU
6377 "menu",
6378#endif
6379#ifdef FEAT_SESSION
6380 "mksession",
6381#endif
6382#ifdef FEAT_MODIFY_FNAME
6383 "modify_fname",
6384#endif
6385#ifdef FEAT_MOUSE
6386 "mouse",
6387#endif
6388#ifdef FEAT_MOUSESHAPE
6389 "mouseshape",
6390#endif
6391#if defined(UNIX) || defined(VMS)
6392# ifdef FEAT_MOUSE_DEC
6393 "mouse_dec",
6394# endif
6395# ifdef FEAT_MOUSE_GPM
6396 "mouse_gpm",
6397# endif
6398# ifdef FEAT_MOUSE_JSB
6399 "mouse_jsbterm",
6400# endif
6401# ifdef FEAT_MOUSE_NET
6402 "mouse_netterm",
6403# endif
6404# ifdef FEAT_MOUSE_PTERM
6405 "mouse_pterm",
6406# endif
6407# ifdef FEAT_MOUSE_SGR
6408 "mouse_sgr",
6409# endif
6410# ifdef FEAT_SYSMOUSE
6411 "mouse_sysmouse",
6412# endif
6413# ifdef FEAT_MOUSE_URXVT
6414 "mouse_urxvt",
6415# endif
6416# ifdef FEAT_MOUSE_XTERM
6417 "mouse_xterm",
6418# endif
6419#endif
6420#ifdef FEAT_MBYTE
6421 "multi_byte",
6422#endif
6423#ifdef FEAT_MBYTE_IME
6424 "multi_byte_ime",
6425#endif
6426#ifdef FEAT_MULTI_LANG
6427 "multi_lang",
6428#endif
6429#ifdef FEAT_MZSCHEME
6430#ifndef DYNAMIC_MZSCHEME
6431 "mzscheme",
6432#endif
6433#endif
6434#ifdef FEAT_NUM64
6435 "num64",
6436#endif
6437#ifdef FEAT_OLE
6438 "ole",
6439#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006440#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006441 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006442#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006443#ifdef FEAT_PATH_EXTRA
6444 "path_extra",
6445#endif
6446#ifdef FEAT_PERL
6447#ifndef DYNAMIC_PERL
6448 "perl",
6449#endif
6450#endif
6451#ifdef FEAT_PERSISTENT_UNDO
6452 "persistent_undo",
6453#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006454#if defined(FEAT_PYTHON)
6455 "python_compiled",
6456# if defined(DYNAMIC_PYTHON)
6457 "python_dynamic",
6458# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006459 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006460 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006461# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006462#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006463#if defined(FEAT_PYTHON3)
6464 "python3_compiled",
6465# if defined(DYNAMIC_PYTHON3)
6466 "python3_dynamic",
6467# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006468 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006469 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006470# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006471#endif
6472#ifdef FEAT_POSTSCRIPT
6473 "postscript",
6474#endif
6475#ifdef FEAT_PRINTER
6476 "printer",
6477#endif
6478#ifdef FEAT_PROFILE
6479 "profile",
6480#endif
6481#ifdef FEAT_RELTIME
6482 "reltime",
6483#endif
6484#ifdef FEAT_QUICKFIX
6485 "quickfix",
6486#endif
6487#ifdef FEAT_RIGHTLEFT
6488 "rightleft",
6489#endif
6490#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6491 "ruby",
6492#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006493 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006494#ifdef FEAT_CMDL_INFO
6495 "showcmd",
6496 "cmdline_info",
6497#endif
6498#ifdef FEAT_SIGNS
6499 "signs",
6500#endif
6501#ifdef FEAT_SMARTINDENT
6502 "smartindent",
6503#endif
6504#ifdef STARTUPTIME
6505 "startuptime",
6506#endif
6507#ifdef FEAT_STL_OPT
6508 "statusline",
6509#endif
6510#ifdef FEAT_SUN_WORKSHOP
6511 "sun_workshop",
6512#endif
6513#ifdef FEAT_NETBEANS_INTG
6514 "netbeans_intg",
6515#endif
6516#ifdef FEAT_SPELL
6517 "spell",
6518#endif
6519#ifdef FEAT_SYN_HL
6520 "syntax",
6521#endif
6522#if defined(USE_SYSTEM) || !defined(UNIX)
6523 "system",
6524#endif
6525#ifdef FEAT_TAG_BINS
6526 "tag_binary",
6527#endif
6528#ifdef FEAT_TAG_OLDSTATIC
6529 "tag_old_static",
6530#endif
6531#ifdef FEAT_TAG_ANYWHITE
6532 "tag_any_white",
6533#endif
6534#ifdef FEAT_TCL
6535# ifndef DYNAMIC_TCL
6536 "tcl",
6537# endif
6538#endif
6539#ifdef FEAT_TERMGUICOLORS
6540 "termguicolors",
6541#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006542#if defined(FEAT_TERMINAL) && !defined(WIN3264)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006543 "terminal",
6544#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006545#ifdef TERMINFO
6546 "terminfo",
6547#endif
6548#ifdef FEAT_TERMRESPONSE
6549 "termresponse",
6550#endif
6551#ifdef FEAT_TEXTOBJ
6552 "textobjects",
6553#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006554#ifdef FEAT_TEXT_PROP
6555 "textprop",
6556#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006557#ifdef HAVE_TGETENT
6558 "tgetent",
6559#endif
6560#ifdef FEAT_TIMERS
6561 "timers",
6562#endif
6563#ifdef FEAT_TITLE
6564 "title",
6565#endif
6566#ifdef FEAT_TOOLBAR
6567 "toolbar",
6568#endif
6569#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6570 "unnamedplus",
6571#endif
6572#ifdef FEAT_USR_CMDS
6573 "user-commands", /* was accidentally included in 5.4 */
6574 "user_commands",
6575#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006576#ifdef FEAT_VARTABS
6577 "vartabs",
6578#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006579#ifdef FEAT_VIMINFO
6580 "viminfo",
6581#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006582 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006583#ifdef FEAT_VIRTUALEDIT
6584 "virtualedit",
6585#endif
6586 "visual",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006587 "visualextra",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006588 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006589#ifdef FEAT_VTP
6590 "vtp",
6591#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006592#ifdef FEAT_WILDIGN
6593 "wildignore",
6594#endif
6595#ifdef FEAT_WILDMENU
6596 "wildmenu",
6597#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006598 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006599#ifdef FEAT_WAK
6600 "winaltkeys",
6601#endif
6602#ifdef FEAT_WRITEBACKUP
6603 "writebackup",
6604#endif
6605#ifdef FEAT_XIM
6606 "xim",
6607#endif
6608#ifdef FEAT_XFONTSET
6609 "xfontset",
6610#endif
6611#ifdef FEAT_XPM_W32
6612 "xpm",
6613 "xpm_w32", /* for backward compatibility */
6614#else
6615# if defined(HAVE_XPM)
6616 "xpm",
6617# endif
6618#endif
6619#ifdef USE_XSMP
6620 "xsmp",
6621#endif
6622#ifdef USE_XSMP_INTERACT
6623 "xsmp_interact",
6624#endif
6625#ifdef FEAT_XCLIPBOARD
6626 "xterm_clipboard",
6627#endif
6628#ifdef FEAT_XTERM_SAVE
6629 "xterm_save",
6630#endif
6631#if defined(UNIX) && defined(FEAT_X11)
6632 "X11",
6633#endif
6634 NULL
6635 };
6636
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006637 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006638 for (i = 0; has_list[i] != NULL; ++i)
6639 if (STRICMP(name, has_list[i]) == 0)
6640 {
6641 n = TRUE;
6642 break;
6643 }
6644
6645 if (n == FALSE)
6646 {
6647 if (STRNICMP(name, "patch", 5) == 0)
6648 {
6649 if (name[5] == '-'
6650 && STRLEN(name) >= 11
6651 && vim_isdigit(name[6])
6652 && vim_isdigit(name[8])
6653 && vim_isdigit(name[10]))
6654 {
6655 int major = atoi((char *)name + 6);
6656 int minor = atoi((char *)name + 8);
6657
6658 /* Expect "patch-9.9.01234". */
6659 n = (major < VIM_VERSION_MAJOR
6660 || (major == VIM_VERSION_MAJOR
6661 && (minor < VIM_VERSION_MINOR
6662 || (minor == VIM_VERSION_MINOR
6663 && has_patch(atoi((char *)name + 10))))));
6664 }
6665 else
6666 n = has_patch(atoi((char *)name + 5));
6667 }
6668 else if (STRICMP(name, "vim_starting") == 0)
6669 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006670 else if (STRICMP(name, "ttyin") == 0)
6671 n = mch_input_isatty();
6672 else if (STRICMP(name, "ttyout") == 0)
6673 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006674#ifdef FEAT_MBYTE
6675 else if (STRICMP(name, "multi_byte_encoding") == 0)
6676 n = has_mbyte;
6677#endif
6678#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
6679 else if (STRICMP(name, "balloon_multiline") == 0)
6680 n = multiline_balloon_available();
6681#endif
6682#ifdef DYNAMIC_TCL
6683 else if (STRICMP(name, "tcl") == 0)
6684 n = tcl_enabled(FALSE);
6685#endif
6686#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6687 else if (STRICMP(name, "iconv") == 0)
6688 n = iconv_enabled(FALSE);
6689#endif
6690#ifdef DYNAMIC_LUA
6691 else if (STRICMP(name, "lua") == 0)
6692 n = lua_enabled(FALSE);
6693#endif
6694#ifdef DYNAMIC_MZSCHEME
6695 else if (STRICMP(name, "mzscheme") == 0)
6696 n = mzscheme_enabled(FALSE);
6697#endif
6698#ifdef DYNAMIC_RUBY
6699 else if (STRICMP(name, "ruby") == 0)
6700 n = ruby_enabled(FALSE);
6701#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006702#ifdef DYNAMIC_PYTHON
6703 else if (STRICMP(name, "python") == 0)
6704 n = python_enabled(FALSE);
6705#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006706#ifdef DYNAMIC_PYTHON3
6707 else if (STRICMP(name, "python3") == 0)
6708 n = python3_enabled(FALSE);
6709#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006710#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6711 else if (STRICMP(name, "pythonx") == 0)
6712 {
6713# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6714 if (p_pyx == 0)
6715 n = python3_enabled(FALSE) || python_enabled(FALSE);
6716 else if (p_pyx == 3)
6717 n = python3_enabled(FALSE);
6718 else if (p_pyx == 2)
6719 n = python_enabled(FALSE);
6720# elif defined(DYNAMIC_PYTHON)
6721 n = python_enabled(FALSE);
6722# elif defined(DYNAMIC_PYTHON3)
6723 n = python3_enabled(FALSE);
6724# endif
6725 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006726#endif
6727#ifdef DYNAMIC_PERL
6728 else if (STRICMP(name, "perl") == 0)
6729 n = perl_enabled(FALSE);
6730#endif
6731#ifdef FEAT_GUI
6732 else if (STRICMP(name, "gui_running") == 0)
6733 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006734# ifdef FEAT_BROWSE
6735 else if (STRICMP(name, "browse") == 0)
6736 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6737# endif
6738#endif
6739#ifdef FEAT_SYN_HL
6740 else if (STRICMP(name, "syntax_items") == 0)
6741 n = syntax_present(curwin);
6742#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006743#ifdef FEAT_VTP
6744 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02006745 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006746#endif
6747#ifdef FEAT_NETBEANS_INTG
6748 else if (STRICMP(name, "netbeans_enabled") == 0)
6749 n = netbeans_active();
6750#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006751#if defined(FEAT_TERMINAL) && defined(WIN3264)
6752 else if (STRICMP(name, "terminal") == 0)
6753 n = terminal_enabled();
6754#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006755 }
6756
6757 rettv->vval.v_number = n;
6758}
6759
6760/*
6761 * "has_key()" function
6762 */
6763 static void
6764f_has_key(typval_T *argvars, typval_T *rettv)
6765{
6766 if (argvars[0].v_type != VAR_DICT)
6767 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006768 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006769 return;
6770 }
6771 if (argvars[0].vval.v_dict == NULL)
6772 return;
6773
6774 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006775 tv_get_string(&argvars[1]), -1) != NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006776}
6777
6778/*
6779 * "haslocaldir()" function
6780 */
6781 static void
6782f_haslocaldir(typval_T *argvars, typval_T *rettv)
6783{
6784 win_T *wp = NULL;
6785
6786 wp = find_tabwin(&argvars[0], &argvars[1]);
6787 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6788}
6789
6790/*
6791 * "hasmapto()" function
6792 */
6793 static void
6794f_hasmapto(typval_T *argvars, typval_T *rettv)
6795{
6796 char_u *name;
6797 char_u *mode;
6798 char_u buf[NUMBUFLEN];
6799 int abbr = FALSE;
6800
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006801 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006802 if (argvars[1].v_type == VAR_UNKNOWN)
6803 mode = (char_u *)"nvo";
6804 else
6805 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006806 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006807 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006808 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006809 }
6810
6811 if (map_to_exists(name, mode, abbr))
6812 rettv->vval.v_number = TRUE;
6813 else
6814 rettv->vval.v_number = FALSE;
6815}
6816
6817/*
6818 * "histadd()" function
6819 */
6820 static void
6821f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6822{
6823#ifdef FEAT_CMDHIST
6824 int histype;
6825 char_u *str;
6826 char_u buf[NUMBUFLEN];
6827#endif
6828
6829 rettv->vval.v_number = FALSE;
6830 if (check_restricted() || check_secure())
6831 return;
6832#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006833 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006834 histype = str != NULL ? get_histtype(str) : -1;
6835 if (histype >= 0)
6836 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006837 str = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006838 if (*str != NUL)
6839 {
6840 init_history();
6841 add_to_history(histype, str, FALSE, NUL);
6842 rettv->vval.v_number = TRUE;
6843 return;
6844 }
6845 }
6846#endif
6847}
6848
6849/*
6850 * "histdel()" function
6851 */
6852 static void
6853f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6854{
6855#ifdef FEAT_CMDHIST
6856 int n;
6857 char_u buf[NUMBUFLEN];
6858 char_u *str;
6859
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006860 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006861 if (str == NULL)
6862 n = 0;
6863 else if (argvars[1].v_type == VAR_UNKNOWN)
6864 /* only one argument: clear entire history */
6865 n = clr_history(get_histtype(str));
6866 else if (argvars[1].v_type == VAR_NUMBER)
6867 /* index given: remove that entry */
6868 n = del_history_idx(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006869 (int)tv_get_number(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006870 else
6871 /* string given: remove all matching entries */
6872 n = del_history_entry(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006873 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006874 rettv->vval.v_number = n;
6875#endif
6876}
6877
6878/*
6879 * "histget()" function
6880 */
6881 static void
6882f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6883{
6884#ifdef FEAT_CMDHIST
6885 int type;
6886 int idx;
6887 char_u *str;
6888
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006889 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006890 if (str == NULL)
6891 rettv->vval.v_string = NULL;
6892 else
6893 {
6894 type = get_histtype(str);
6895 if (argvars[1].v_type == VAR_UNKNOWN)
6896 idx = get_history_idx(type);
6897 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006898 idx = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006899 /* -1 on type error */
6900 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6901 }
6902#else
6903 rettv->vval.v_string = NULL;
6904#endif
6905 rettv->v_type = VAR_STRING;
6906}
6907
6908/*
6909 * "histnr()" function
6910 */
6911 static void
6912f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6913{
6914 int i;
6915
6916#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006917 char_u *history = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006918
6919 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6920 if (i >= HIST_CMD && i < HIST_COUNT)
6921 i = get_history_idx(i);
6922 else
6923#endif
6924 i = -1;
6925 rettv->vval.v_number = i;
6926}
6927
6928/*
6929 * "highlightID(name)" function
6930 */
6931 static void
6932f_hlID(typval_T *argvars, typval_T *rettv)
6933{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006934 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006935}
6936
6937/*
6938 * "highlight_exists()" function
6939 */
6940 static void
6941f_hlexists(typval_T *argvars, typval_T *rettv)
6942{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006943 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006944}
6945
6946/*
6947 * "hostname()" function
6948 */
6949 static void
6950f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6951{
6952 char_u hostname[256];
6953
6954 mch_get_host_name(hostname, 256);
6955 rettv->v_type = VAR_STRING;
6956 rettv->vval.v_string = vim_strsave(hostname);
6957}
6958
6959/*
6960 * iconv() function
6961 */
6962 static void
6963f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6964{
6965#ifdef FEAT_MBYTE
6966 char_u buf1[NUMBUFLEN];
6967 char_u buf2[NUMBUFLEN];
6968 char_u *from, *to, *str;
6969 vimconv_T vimconv;
6970#endif
6971
6972 rettv->v_type = VAR_STRING;
6973 rettv->vval.v_string = NULL;
6974
6975#ifdef FEAT_MBYTE
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006976 str = tv_get_string(&argvars[0]);
6977 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
6978 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006979 vimconv.vc_type = CONV_NONE;
6980 convert_setup(&vimconv, from, to);
6981
6982 /* If the encodings are equal, no conversion needed. */
6983 if (vimconv.vc_type == CONV_NONE)
6984 rettv->vval.v_string = vim_strsave(str);
6985 else
6986 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6987
6988 convert_setup(&vimconv, NULL, NULL);
6989 vim_free(from);
6990 vim_free(to);
6991#endif
6992}
6993
6994/*
6995 * "indent()" function
6996 */
6997 static void
6998f_indent(typval_T *argvars, typval_T *rettv)
6999{
7000 linenr_T lnum;
7001
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007002 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007003 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7004 rettv->vval.v_number = get_indent_lnum(lnum);
7005 else
7006 rettv->vval.v_number = -1;
7007}
7008
7009/*
7010 * "index()" function
7011 */
7012 static void
7013f_index(typval_T *argvars, typval_T *rettv)
7014{
7015 list_T *l;
7016 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007017 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007018 long idx = 0;
7019 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007020 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007021
7022 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007023 if (argvars[0].v_type == VAR_BLOB)
7024 {
7025 typval_T tv;
7026 int start = 0;
7027
7028 if (argvars[2].v_type != VAR_UNKNOWN)
7029 {
7030 start = tv_get_number_chk(&argvars[2], &error);
7031 if (error)
7032 return;
7033 }
7034 b = argvars[0].vval.v_blob;
7035 if (b == NULL)
7036 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01007037 if (start < 0)
7038 {
7039 start = blob_len(b) + start;
7040 if (start < 0)
7041 start = 0;
7042 }
7043
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007044 for (idx = start; idx < blob_len(b); ++idx)
7045 {
7046 tv.v_type = VAR_NUMBER;
7047 tv.vval.v_number = blob_get(b, idx);
7048 if (tv_equal(&tv, &argvars[1], ic, FALSE))
7049 {
7050 rettv->vval.v_number = idx;
7051 return;
7052 }
7053 }
7054 return;
7055 }
7056 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007057 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007058 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007059 return;
7060 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007061
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007062 l = argvars[0].vval.v_list;
7063 if (l != NULL)
7064 {
7065 item = l->lv_first;
7066 if (argvars[2].v_type != VAR_UNKNOWN)
7067 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007068 /* Start at specified item. Use the cached index that list_find()
7069 * sets, so that a negative number also works. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007070 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007071 idx = l->lv_idx;
7072 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007073 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007074 if (error)
7075 item = NULL;
7076 }
7077
7078 for ( ; item != NULL; item = item->li_next, ++idx)
7079 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
7080 {
7081 rettv->vval.v_number = idx;
7082 break;
7083 }
7084 }
7085}
7086
7087static int inputsecret_flag = 0;
7088
7089/*
7090 * "input()" function
7091 * Also handles inputsecret() when inputsecret is set.
7092 */
7093 static void
7094f_input(typval_T *argvars, typval_T *rettv)
7095{
7096 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
7097}
7098
7099/*
7100 * "inputdialog()" function
7101 */
7102 static void
7103f_inputdialog(typval_T *argvars, typval_T *rettv)
7104{
7105#if defined(FEAT_GUI_TEXTDIALOG)
7106 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
7107 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
7108 {
7109 char_u *message;
7110 char_u buf[NUMBUFLEN];
7111 char_u *defstr = (char_u *)"";
7112
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007113 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007114 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007115 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007116 vim_strncpy(IObuff, defstr, IOSIZE - 1);
7117 else
7118 IObuff[0] = NUL;
7119 if (message != NULL && defstr != NULL
7120 && do_dialog(VIM_QUESTION, NULL, message,
7121 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
7122 rettv->vval.v_string = vim_strsave(IObuff);
7123 else
7124 {
7125 if (message != NULL && defstr != NULL
7126 && argvars[1].v_type != VAR_UNKNOWN
7127 && argvars[2].v_type != VAR_UNKNOWN)
7128 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007129 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007130 else
7131 rettv->vval.v_string = NULL;
7132 }
7133 rettv->v_type = VAR_STRING;
7134 }
7135 else
7136#endif
7137 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
7138}
7139
7140/*
7141 * "inputlist()" function
7142 */
7143 static void
7144f_inputlist(typval_T *argvars, typval_T *rettv)
7145{
7146 listitem_T *li;
7147 int selected;
7148 int mouse_used;
7149
7150#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007151 /* While starting up, there is no place to enter text. When running tests
7152 * with --not-a-term we assume feedkeys() will be used. */
7153 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007154 return;
7155#endif
7156 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
7157 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007158 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007159 return;
7160 }
7161
7162 msg_start();
7163 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
7164 lines_left = Rows; /* avoid more prompt */
7165 msg_scroll = TRUE;
7166 msg_clr_eos();
7167
7168 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
7169 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007170 msg_puts(tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007171 msg_putchar('\n');
7172 }
7173
7174 /* Ask for choice. */
7175 selected = prompt_for_number(&mouse_used);
7176 if (mouse_used)
7177 selected -= lines_left;
7178
7179 rettv->vval.v_number = selected;
7180}
7181
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007182static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7183
7184/*
7185 * "inputrestore()" function
7186 */
7187 static void
7188f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7189{
7190 if (ga_userinput.ga_len > 0)
7191 {
7192 --ga_userinput.ga_len;
7193 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7194 + ga_userinput.ga_len);
7195 /* default return is zero == OK */
7196 }
7197 else if (p_verbose > 1)
7198 {
7199 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
7200 rettv->vval.v_number = 1; /* Failed */
7201 }
7202}
7203
7204/*
7205 * "inputsave()" function
7206 */
7207 static void
7208f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7209{
7210 /* Add an entry to the stack of typeahead storage. */
7211 if (ga_grow(&ga_userinput, 1) == OK)
7212 {
7213 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7214 + ga_userinput.ga_len);
7215 ++ga_userinput.ga_len;
7216 /* default return is zero == OK */
7217 }
7218 else
7219 rettv->vval.v_number = 1; /* Failed */
7220}
7221
7222/*
7223 * "inputsecret()" function
7224 */
7225 static void
7226f_inputsecret(typval_T *argvars, typval_T *rettv)
7227{
7228 ++cmdline_star;
7229 ++inputsecret_flag;
7230 f_input(argvars, rettv);
7231 --cmdline_star;
7232 --inputsecret_flag;
7233}
7234
7235/*
7236 * "insert()" function
7237 */
7238 static void
7239f_insert(typval_T *argvars, typval_T *rettv)
7240{
7241 long before = 0;
7242 listitem_T *item;
7243 list_T *l;
7244 int error = FALSE;
7245
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007246 if (argvars[0].v_type == VAR_BLOB)
7247 {
7248 int val, len;
7249 char_u *p;
7250
7251 len = blob_len(argvars[0].vval.v_blob);
7252 if (argvars[2].v_type != VAR_UNKNOWN)
7253 {
7254 before = (long)tv_get_number_chk(&argvars[2], &error);
7255 if (error)
7256 return; // type error; errmsg already given
7257 if (before < 0 || before > len)
7258 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007259 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007260 return;
7261 }
7262 }
7263 val = tv_get_number_chk(&argvars[1], &error);
7264 if (error)
7265 return;
7266 if (val < 0 || val > 255)
7267 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007268 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007269 return;
7270 }
7271
7272 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
7273 return;
7274 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
7275 mch_memmove(p + before + 1, p + before, (size_t)len - before);
7276 *(p + before) = val;
7277 ++argvars[0].vval.v_blob->bv_ga.ga_len;
7278
7279 copy_tv(&argvars[0], rettv);
7280 }
7281 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007282 semsg(_(e_listarg), "insert()");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007283 else if ((l = argvars[0].vval.v_list) != NULL && !tv_check_lock(l->lv_lock,
7284 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007285 {
7286 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007287 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007288 if (error)
7289 return; /* type error; errmsg already given */
7290
7291 if (before == l->lv_len)
7292 item = NULL;
7293 else
7294 {
7295 item = list_find(l, before);
7296 if (item == NULL)
7297 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007298 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007299 l = NULL;
7300 }
7301 }
7302 if (l != NULL)
7303 {
7304 list_insert_tv(l, &argvars[1], item);
7305 copy_tv(&argvars[0], rettv);
7306 }
7307 }
7308}
7309
7310/*
7311 * "invert(expr)" function
7312 */
7313 static void
7314f_invert(typval_T *argvars, typval_T *rettv)
7315{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007316 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007317}
7318
7319/*
7320 * "isdirectory()" function
7321 */
7322 static void
7323f_isdirectory(typval_T *argvars, typval_T *rettv)
7324{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007325 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007326}
7327
7328/*
7329 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7330 * or it refers to a List or Dictionary that is locked.
7331 */
7332 static int
7333tv_islocked(typval_T *tv)
7334{
7335 return (tv->v_lock & VAR_LOCKED)
7336 || (tv->v_type == VAR_LIST
7337 && tv->vval.v_list != NULL
7338 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7339 || (tv->v_type == VAR_DICT
7340 && tv->vval.v_dict != NULL
7341 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7342}
7343
7344/*
7345 * "islocked()" function
7346 */
7347 static void
7348f_islocked(typval_T *argvars, typval_T *rettv)
7349{
7350 lval_T lv;
7351 char_u *end;
7352 dictitem_T *di;
7353
7354 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007355 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007356 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007357 if (end != NULL && lv.ll_name != NULL)
7358 {
7359 if (*end != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007360 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007361 else
7362 {
7363 if (lv.ll_tv == NULL)
7364 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007365 di = find_var(lv.ll_name, NULL, TRUE);
7366 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007367 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007368 /* Consider a variable locked when:
7369 * 1. the variable itself is locked
7370 * 2. the value of the variable is locked.
7371 * 3. the List or Dict value is locked.
7372 */
7373 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7374 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007375 }
7376 }
7377 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007378 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007379 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007380 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007381 else if (lv.ll_list != NULL)
7382 /* List item. */
7383 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7384 else
7385 /* Dictionary item. */
7386 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7387 }
7388 }
7389
7390 clear_lval(&lv);
7391}
7392
7393#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7394/*
7395 * "isnan()" function
7396 */
7397 static void
7398f_isnan(typval_T *argvars, typval_T *rettv)
7399{
7400 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7401 && isnan(argvars[0].vval.v_float);
7402}
7403#endif
7404
7405/*
7406 * "items(dict)" function
7407 */
7408 static void
7409f_items(typval_T *argvars, typval_T *rettv)
7410{
7411 dict_list(argvars, rettv, 2);
7412}
7413
7414#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
7415/*
7416 * Get the job from the argument.
7417 * Returns NULL if the job is invalid.
7418 */
7419 static job_T *
7420get_job_arg(typval_T *tv)
7421{
7422 job_T *job;
7423
7424 if (tv->v_type != VAR_JOB)
7425 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007426 semsg(_(e_invarg2), tv_get_string(tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007427 return NULL;
7428 }
7429 job = tv->vval.v_job;
7430
7431 if (job == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007432 emsg(_("E916: not a valid job"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007433 return job;
7434}
7435
7436/*
7437 * "job_getchannel()" function
7438 */
7439 static void
7440f_job_getchannel(typval_T *argvars, typval_T *rettv)
7441{
7442 job_T *job = get_job_arg(&argvars[0]);
7443
7444 if (job != NULL)
7445 {
7446 rettv->v_type = VAR_CHANNEL;
7447 rettv->vval.v_channel = job->jv_channel;
7448 if (job->jv_channel != NULL)
7449 ++job->jv_channel->ch_refcount;
7450 }
7451}
7452
7453/*
7454 * "job_info()" function
7455 */
7456 static void
7457f_job_info(typval_T *argvars, typval_T *rettv)
7458{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007459 if (argvars[0].v_type != VAR_UNKNOWN)
7460 {
7461 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007462
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007463 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7464 job_info(job, rettv->vval.v_dict);
7465 }
7466 else if (rettv_list_alloc(rettv) == OK)
7467 job_info_all(rettv->vval.v_list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007468}
7469
7470/*
7471 * "job_setoptions()" function
7472 */
7473 static void
7474f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7475{
7476 job_T *job = get_job_arg(&argvars[0]);
7477 jobopt_T opt;
7478
7479 if (job == NULL)
7480 return;
7481 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007482 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007483 job_set_options(job, &opt);
7484 free_job_options(&opt);
7485}
7486
7487/*
7488 * "job_start()" function
7489 */
7490 static void
7491f_job_start(typval_T *argvars, typval_T *rettv)
7492{
7493 rettv->v_type = VAR_JOB;
7494 if (check_restricted() || check_secure())
7495 return;
Bram Moolenaar493359e2018-06-12 20:25:52 +02007496 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007497}
7498
7499/*
7500 * "job_status()" function
7501 */
7502 static void
7503f_job_status(typval_T *argvars, typval_T *rettv)
7504{
7505 job_T *job = get_job_arg(&argvars[0]);
7506
7507 if (job != NULL)
7508 {
7509 rettv->v_type = VAR_STRING;
7510 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7511 }
7512}
7513
7514/*
7515 * "job_stop()" function
7516 */
7517 static void
7518f_job_stop(typval_T *argvars, typval_T *rettv)
7519{
7520 job_T *job = get_job_arg(&argvars[0]);
7521
7522 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007523 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007524}
7525#endif
7526
7527/*
7528 * "join()" function
7529 */
7530 static void
7531f_join(typval_T *argvars, typval_T *rettv)
7532{
7533 garray_T ga;
7534 char_u *sep;
7535
7536 if (argvars[0].v_type != VAR_LIST)
7537 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007538 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007539 return;
7540 }
7541 if (argvars[0].vval.v_list == NULL)
7542 return;
7543 if (argvars[1].v_type == VAR_UNKNOWN)
7544 sep = (char_u *)" ";
7545 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007546 sep = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007547
7548 rettv->v_type = VAR_STRING;
7549
7550 if (sep != NULL)
7551 {
7552 ga_init2(&ga, (int)sizeof(char), 80);
7553 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7554 ga_append(&ga, NUL);
7555 rettv->vval.v_string = (char_u *)ga.ga_data;
7556 }
7557 else
7558 rettv->vval.v_string = NULL;
7559}
7560
7561/*
7562 * "js_decode()" function
7563 */
7564 static void
7565f_js_decode(typval_T *argvars, typval_T *rettv)
7566{
7567 js_read_T reader;
7568
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007569 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007570 reader.js_fill = NULL;
7571 reader.js_used = 0;
7572 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007573 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007574}
7575
7576/*
7577 * "js_encode()" function
7578 */
7579 static void
7580f_js_encode(typval_T *argvars, typval_T *rettv)
7581{
7582 rettv->v_type = VAR_STRING;
7583 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7584}
7585
7586/*
7587 * "json_decode()" function
7588 */
7589 static void
7590f_json_decode(typval_T *argvars, typval_T *rettv)
7591{
7592 js_read_T reader;
7593
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007594 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007595 reader.js_fill = NULL;
7596 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007597 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007598}
7599
7600/*
7601 * "json_encode()" function
7602 */
7603 static void
7604f_json_encode(typval_T *argvars, typval_T *rettv)
7605{
7606 rettv->v_type = VAR_STRING;
7607 rettv->vval.v_string = json_encode(&argvars[0], 0);
7608}
7609
7610/*
7611 * "keys()" function
7612 */
7613 static void
7614f_keys(typval_T *argvars, typval_T *rettv)
7615{
7616 dict_list(argvars, rettv, 0);
7617}
7618
7619/*
7620 * "last_buffer_nr()" function.
7621 */
7622 static void
7623f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7624{
7625 int n = 0;
7626 buf_T *buf;
7627
Bram Moolenaar29323592016-07-24 22:04:11 +02007628 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007629 if (n < buf->b_fnum)
7630 n = buf->b_fnum;
7631
7632 rettv->vval.v_number = n;
7633}
7634
7635/*
7636 * "len()" function
7637 */
7638 static void
7639f_len(typval_T *argvars, typval_T *rettv)
7640{
7641 switch (argvars[0].v_type)
7642 {
7643 case VAR_STRING:
7644 case VAR_NUMBER:
7645 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007646 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007647 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007648 case VAR_BLOB:
7649 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
7650 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007651 case VAR_LIST:
7652 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7653 break;
7654 case VAR_DICT:
7655 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7656 break;
7657 case VAR_UNKNOWN:
7658 case VAR_SPECIAL:
7659 case VAR_FLOAT:
7660 case VAR_FUNC:
7661 case VAR_PARTIAL:
7662 case VAR_JOB:
7663 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007664 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007665 break;
7666 }
7667}
7668
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007669 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007670libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007671{
7672#ifdef FEAT_LIBCALL
7673 char_u *string_in;
7674 char_u **string_result;
7675 int nr_result;
7676#endif
7677
7678 rettv->v_type = type;
7679 if (type != VAR_NUMBER)
7680 rettv->vval.v_string = NULL;
7681
7682 if (check_restricted() || check_secure())
7683 return;
7684
7685#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007686 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007687 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7688 {
7689 string_in = NULL;
7690 if (argvars[2].v_type == VAR_STRING)
7691 string_in = argvars[2].vval.v_string;
7692 if (type == VAR_NUMBER)
7693 string_result = NULL;
7694 else
7695 string_result = &rettv->vval.v_string;
7696 if (mch_libcall(argvars[0].vval.v_string,
7697 argvars[1].vval.v_string,
7698 string_in,
7699 argvars[2].vval.v_number,
7700 string_result,
7701 &nr_result) == OK
7702 && type == VAR_NUMBER)
7703 rettv->vval.v_number = nr_result;
7704 }
7705#endif
7706}
7707
7708/*
7709 * "libcall()" function
7710 */
7711 static void
7712f_libcall(typval_T *argvars, typval_T *rettv)
7713{
7714 libcall_common(argvars, rettv, VAR_STRING);
7715}
7716
7717/*
7718 * "libcallnr()" function
7719 */
7720 static void
7721f_libcallnr(typval_T *argvars, typval_T *rettv)
7722{
7723 libcall_common(argvars, rettv, VAR_NUMBER);
7724}
7725
7726/*
7727 * "line(string)" function
7728 */
7729 static void
7730f_line(typval_T *argvars, typval_T *rettv)
7731{
7732 linenr_T lnum = 0;
7733 pos_T *fp;
7734 int fnum;
7735
7736 fp = var2fpos(&argvars[0], TRUE, &fnum);
7737 if (fp != NULL)
7738 lnum = fp->lnum;
7739 rettv->vval.v_number = lnum;
7740}
7741
7742/*
7743 * "line2byte(lnum)" function
7744 */
7745 static void
7746f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7747{
7748#ifndef FEAT_BYTEOFF
7749 rettv->vval.v_number = -1;
7750#else
7751 linenr_T lnum;
7752
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007753 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007754 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7755 rettv->vval.v_number = -1;
7756 else
7757 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7758 if (rettv->vval.v_number >= 0)
7759 ++rettv->vval.v_number;
7760#endif
7761}
7762
7763/*
7764 * "lispindent(lnum)" function
7765 */
7766 static void
7767f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7768{
7769#ifdef FEAT_LISP
7770 pos_T pos;
7771 linenr_T lnum;
7772
7773 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007774 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007775 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7776 {
7777 curwin->w_cursor.lnum = lnum;
7778 rettv->vval.v_number = get_lisp_indent();
7779 curwin->w_cursor = pos;
7780 }
7781 else
7782#endif
7783 rettv->vval.v_number = -1;
7784}
7785
7786/*
7787 * "localtime()" function
7788 */
7789 static void
7790f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7791{
7792 rettv->vval.v_number = (varnumber_T)time(NULL);
7793}
7794
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007795 static void
7796get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7797{
7798 char_u *keys;
7799 char_u *which;
7800 char_u buf[NUMBUFLEN];
7801 char_u *keys_buf = NULL;
7802 char_u *rhs;
7803 int mode;
7804 int abbr = FALSE;
7805 int get_dict = FALSE;
7806 mapblock_T *mp;
7807 int buffer_local;
7808
7809 /* return empty string for failure */
7810 rettv->v_type = VAR_STRING;
7811 rettv->vval.v_string = NULL;
7812
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007813 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007814 if (*keys == NUL)
7815 return;
7816
7817 if (argvars[1].v_type != VAR_UNKNOWN)
7818 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007819 which = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007820 if (argvars[2].v_type != VAR_UNKNOWN)
7821 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007822 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007823 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007824 get_dict = (int)tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007825 }
7826 }
7827 else
7828 which = (char_u *)"";
7829 if (which == NULL)
7830 return;
7831
7832 mode = get_map_mode(&which, 0);
7833
7834 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7835 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7836 vim_free(keys_buf);
7837
7838 if (!get_dict)
7839 {
7840 /* Return a string. */
7841 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02007842 {
7843 if (*rhs == NUL)
7844 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
7845 else
7846 rettv->vval.v_string = str2special_save(rhs, FALSE);
7847 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007848
7849 }
7850 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7851 {
7852 /* Return a dictionary. */
7853 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7854 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7855 dict_T *dict = rettv->vval.v_dict;
7856
Bram Moolenaare0be1672018-07-08 16:50:37 +02007857 dict_add_string(dict, "lhs", lhs);
7858 dict_add_string(dict, "rhs", mp->m_orig_str);
7859 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
7860 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
7861 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007862 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
7863 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02007864 dict_add_number(dict, "buffer", (long)buffer_local);
7865 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
7866 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007867
7868 vim_free(lhs);
7869 vim_free(mapmode);
7870 }
7871}
7872
7873#ifdef FEAT_FLOAT
7874/*
7875 * "log()" function
7876 */
7877 static void
7878f_log(typval_T *argvars, typval_T *rettv)
7879{
7880 float_T f = 0.0;
7881
7882 rettv->v_type = VAR_FLOAT;
7883 if (get_float_arg(argvars, &f) == OK)
7884 rettv->vval.v_float = log(f);
7885 else
7886 rettv->vval.v_float = 0.0;
7887}
7888
7889/*
7890 * "log10()" function
7891 */
7892 static void
7893f_log10(typval_T *argvars, typval_T *rettv)
7894{
7895 float_T f = 0.0;
7896
7897 rettv->v_type = VAR_FLOAT;
7898 if (get_float_arg(argvars, &f) == OK)
7899 rettv->vval.v_float = log10(f);
7900 else
7901 rettv->vval.v_float = 0.0;
7902}
7903#endif
7904
7905#ifdef FEAT_LUA
7906/*
7907 * "luaeval()" function
7908 */
7909 static void
7910f_luaeval(typval_T *argvars, typval_T *rettv)
7911{
7912 char_u *str;
7913 char_u buf[NUMBUFLEN];
7914
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007915 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007916 do_luaeval(str, argvars + 1, rettv);
7917}
7918#endif
7919
7920/*
7921 * "map()" function
7922 */
7923 static void
7924f_map(typval_T *argvars, typval_T *rettv)
7925{
7926 filter_map(argvars, rettv, TRUE);
7927}
7928
7929/*
7930 * "maparg()" function
7931 */
7932 static void
7933f_maparg(typval_T *argvars, typval_T *rettv)
7934{
7935 get_maparg(argvars, rettv, TRUE);
7936}
7937
7938/*
7939 * "mapcheck()" function
7940 */
7941 static void
7942f_mapcheck(typval_T *argvars, typval_T *rettv)
7943{
7944 get_maparg(argvars, rettv, FALSE);
7945}
7946
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007947typedef enum
7948{
7949 MATCH_END, /* matchend() */
7950 MATCH_MATCH, /* match() */
7951 MATCH_STR, /* matchstr() */
7952 MATCH_LIST, /* matchlist() */
7953 MATCH_POS /* matchstrpos() */
7954} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007955
7956 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007957find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007958{
7959 char_u *str = NULL;
7960 long len = 0;
7961 char_u *expr = NULL;
7962 char_u *pat;
7963 regmatch_T regmatch;
7964 char_u patbuf[NUMBUFLEN];
7965 char_u strbuf[NUMBUFLEN];
7966 char_u *save_cpo;
7967 long start = 0;
7968 long nth = 1;
7969 colnr_T startcol = 0;
7970 int match = 0;
7971 list_T *l = NULL;
7972 listitem_T *li = NULL;
7973 long idx = 0;
7974 char_u *tofree = NULL;
7975
7976 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7977 save_cpo = p_cpo;
7978 p_cpo = (char_u *)"";
7979
7980 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007981 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007982 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007983 /* type MATCH_LIST: return empty list when there are no matches.
7984 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007985 if (rettv_list_alloc(rettv) == FAIL)
7986 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007987 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007988 && (list_append_string(rettv->vval.v_list,
7989 (char_u *)"", 0) == FAIL
7990 || list_append_number(rettv->vval.v_list,
7991 (varnumber_T)-1) == FAIL
7992 || list_append_number(rettv->vval.v_list,
7993 (varnumber_T)-1) == FAIL
7994 || list_append_number(rettv->vval.v_list,
7995 (varnumber_T)-1) == FAIL))
7996 {
7997 list_free(rettv->vval.v_list);
7998 rettv->vval.v_list = NULL;
7999 goto theend;
8000 }
8001 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008002 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008003 {
8004 rettv->v_type = VAR_STRING;
8005 rettv->vval.v_string = NULL;
8006 }
8007
8008 if (argvars[0].v_type == VAR_LIST)
8009 {
8010 if ((l = argvars[0].vval.v_list) == NULL)
8011 goto theend;
8012 li = l->lv_first;
8013 }
8014 else
8015 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008016 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008017 len = (long)STRLEN(str);
8018 }
8019
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008020 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008021 if (pat == NULL)
8022 goto theend;
8023
8024 if (argvars[2].v_type != VAR_UNKNOWN)
8025 {
8026 int error = FALSE;
8027
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008028 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008029 if (error)
8030 goto theend;
8031 if (l != NULL)
8032 {
8033 li = list_find(l, start);
8034 if (li == NULL)
8035 goto theend;
8036 idx = l->lv_idx; /* use the cached index */
8037 }
8038 else
8039 {
8040 if (start < 0)
8041 start = 0;
8042 if (start > len)
8043 goto theend;
8044 /* When "count" argument is there ignore matches before "start",
8045 * otherwise skip part of the string. Differs when pattern is "^"
8046 * or "\<". */
8047 if (argvars[3].v_type != VAR_UNKNOWN)
8048 startcol = start;
8049 else
8050 {
8051 str += start;
8052 len -= start;
8053 }
8054 }
8055
8056 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008057 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008058 if (error)
8059 goto theend;
8060 }
8061
8062 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8063 if (regmatch.regprog != NULL)
8064 {
8065 regmatch.rm_ic = p_ic;
8066
8067 for (;;)
8068 {
8069 if (l != NULL)
8070 {
8071 if (li == NULL)
8072 {
8073 match = FALSE;
8074 break;
8075 }
8076 vim_free(tofree);
8077 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
8078 if (str == NULL)
8079 break;
8080 }
8081
8082 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
8083
8084 if (match && --nth <= 0)
8085 break;
8086 if (l == NULL && !match)
8087 break;
8088
8089 /* Advance to just after the match. */
8090 if (l != NULL)
8091 {
8092 li = li->li_next;
8093 ++idx;
8094 }
8095 else
8096 {
8097#ifdef FEAT_MBYTE
8098 startcol = (colnr_T)(regmatch.startp[0]
8099 + (*mb_ptr2len)(regmatch.startp[0]) - str);
8100#else
8101 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
8102#endif
8103 if (startcol > (colnr_T)len
8104 || str + startcol <= regmatch.startp[0])
8105 {
8106 match = FALSE;
8107 break;
8108 }
8109 }
8110 }
8111
8112 if (match)
8113 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008114 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008115 {
8116 listitem_T *li1 = rettv->vval.v_list->lv_first;
8117 listitem_T *li2 = li1->li_next;
8118 listitem_T *li3 = li2->li_next;
8119 listitem_T *li4 = li3->li_next;
8120
8121 vim_free(li1->li_tv.vval.v_string);
8122 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
8123 (int)(regmatch.endp[0] - regmatch.startp[0]));
8124 li3->li_tv.vval.v_number =
8125 (varnumber_T)(regmatch.startp[0] - expr);
8126 li4->li_tv.vval.v_number =
8127 (varnumber_T)(regmatch.endp[0] - expr);
8128 if (l != NULL)
8129 li2->li_tv.vval.v_number = (varnumber_T)idx;
8130 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008131 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008132 {
8133 int i;
8134
8135 /* return list with matched string and submatches */
8136 for (i = 0; i < NSUBEXP; ++i)
8137 {
8138 if (regmatch.endp[i] == NULL)
8139 {
8140 if (list_append_string(rettv->vval.v_list,
8141 (char_u *)"", 0) == FAIL)
8142 break;
8143 }
8144 else if (list_append_string(rettv->vval.v_list,
8145 regmatch.startp[i],
8146 (int)(regmatch.endp[i] - regmatch.startp[i]))
8147 == FAIL)
8148 break;
8149 }
8150 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008151 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008152 {
8153 /* return matched string */
8154 if (l != NULL)
8155 copy_tv(&li->li_tv, rettv);
8156 else
8157 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
8158 (int)(regmatch.endp[0] - regmatch.startp[0]));
8159 }
8160 else if (l != NULL)
8161 rettv->vval.v_number = idx;
8162 else
8163 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008164 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008165 rettv->vval.v_number =
8166 (varnumber_T)(regmatch.startp[0] - str);
8167 else
8168 rettv->vval.v_number =
8169 (varnumber_T)(regmatch.endp[0] - str);
8170 rettv->vval.v_number += (varnumber_T)(str - expr);
8171 }
8172 }
8173 vim_regfree(regmatch.regprog);
8174 }
8175
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008176theend:
8177 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008178 /* matchstrpos() without a list: drop the second item. */
8179 listitem_remove(rettv->vval.v_list,
8180 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008181 vim_free(tofree);
8182 p_cpo = save_cpo;
8183}
8184
8185/*
8186 * "match()" function
8187 */
8188 static void
8189f_match(typval_T *argvars, typval_T *rettv)
8190{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008191 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008192}
8193
Bram Moolenaar95e51472018-07-28 16:55:56 +02008194#ifdef FEAT_SEARCH_EXTRA
8195 static int
8196matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
8197{
8198 dictitem_T *di;
8199
8200 if (tv->v_type != VAR_DICT)
8201 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008202 emsg(_(e_dictreq));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008203 return FAIL;
8204 }
8205
8206 if (dict_find(tv->vval.v_dict, (char_u *)"conceal", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01008207 *conceal_char = dict_get_string(tv->vval.v_dict,
Bram Moolenaar95e51472018-07-28 16:55:56 +02008208 (char_u *)"conceal", FALSE);
8209
8210 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
8211 {
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02008212 *win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008213 if (*win == NULL)
8214 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008215 emsg(_("E957: Invalid window number"));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008216 return FAIL;
8217 }
8218 }
8219
8220 return OK;
8221}
8222#endif
8223
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008224/*
8225 * "matchadd()" function
8226 */
8227 static void
8228f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8229{
8230#ifdef FEAT_SEARCH_EXTRA
8231 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008232 char_u *grp = tv_get_string_buf_chk(&argvars[0], buf); /* group */
8233 char_u *pat = tv_get_string_buf_chk(&argvars[1], buf); /* pattern */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008234 int prio = 10; /* default priority */
8235 int id = -1;
8236 int error = FALSE;
8237 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008238 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008239
8240 rettv->vval.v_number = -1;
8241
8242 if (grp == NULL || pat == NULL)
8243 return;
8244 if (argvars[2].v_type != VAR_UNKNOWN)
8245 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008246 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008247 if (argvars[3].v_type != VAR_UNKNOWN)
8248 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008249 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008250 if (argvars[4].v_type != VAR_UNKNOWN
8251 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8252 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008253 }
8254 }
8255 if (error == TRUE)
8256 return;
8257 if (id >= 1 && id <= 3)
8258 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008259 semsg(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008260 return;
8261 }
8262
Bram Moolenaar95e51472018-07-28 16:55:56 +02008263 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008264 conceal_char);
8265#endif
8266}
8267
8268/*
8269 * "matchaddpos()" function
8270 */
8271 static void
8272f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8273{
8274#ifdef FEAT_SEARCH_EXTRA
8275 char_u buf[NUMBUFLEN];
8276 char_u *group;
8277 int prio = 10;
8278 int id = -1;
8279 int error = FALSE;
8280 list_T *l;
8281 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008282 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008283
8284 rettv->vval.v_number = -1;
8285
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008286 group = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008287 if (group == NULL)
8288 return;
8289
8290 if (argvars[1].v_type != VAR_LIST)
8291 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008292 semsg(_(e_listarg), "matchaddpos()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008293 return;
8294 }
8295 l = argvars[1].vval.v_list;
8296 if (l == NULL)
8297 return;
8298
8299 if (argvars[2].v_type != VAR_UNKNOWN)
8300 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008301 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008302 if (argvars[3].v_type != VAR_UNKNOWN)
8303 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008304 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008305
8306 if (argvars[4].v_type != VAR_UNKNOWN
8307 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8308 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008309 }
8310 }
8311 if (error == TRUE)
8312 return;
8313
8314 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8315 if (id == 1 || id == 2)
8316 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008317 semsg(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008318 return;
8319 }
8320
Bram Moolenaar95e51472018-07-28 16:55:56 +02008321 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008322 conceal_char);
8323#endif
8324}
8325
8326/*
8327 * "matcharg()" function
8328 */
8329 static void
8330f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8331{
8332 if (rettv_list_alloc(rettv) == OK)
8333 {
8334#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008335 int id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008336 matchitem_T *m;
8337
8338 if (id >= 1 && id <= 3)
8339 {
8340 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8341 {
8342 list_append_string(rettv->vval.v_list,
8343 syn_id2name(m->hlg_id), -1);
8344 list_append_string(rettv->vval.v_list, m->pattern, -1);
8345 }
8346 else
8347 {
8348 list_append_string(rettv->vval.v_list, NULL, -1);
8349 list_append_string(rettv->vval.v_list, NULL, -1);
8350 }
8351 }
8352#endif
8353 }
8354}
8355
8356/*
8357 * "matchdelete()" function
8358 */
8359 static void
8360f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8361{
8362#ifdef FEAT_SEARCH_EXTRA
8363 rettv->vval.v_number = match_delete(curwin,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008364 (int)tv_get_number(&argvars[0]), TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008365#endif
8366}
8367
8368/*
8369 * "matchend()" function
8370 */
8371 static void
8372f_matchend(typval_T *argvars, typval_T *rettv)
8373{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008374 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008375}
8376
8377/*
8378 * "matchlist()" function
8379 */
8380 static void
8381f_matchlist(typval_T *argvars, typval_T *rettv)
8382{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008383 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008384}
8385
8386/*
8387 * "matchstr()" function
8388 */
8389 static void
8390f_matchstr(typval_T *argvars, typval_T *rettv)
8391{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008392 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008393}
8394
8395/*
8396 * "matchstrpos()" function
8397 */
8398 static void
8399f_matchstrpos(typval_T *argvars, typval_T *rettv)
8400{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008401 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008402}
8403
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008404 static void
8405max_min(typval_T *argvars, typval_T *rettv, int domax)
8406{
8407 varnumber_T n = 0;
8408 varnumber_T i;
8409 int error = FALSE;
8410
8411 if (argvars[0].v_type == VAR_LIST)
8412 {
8413 list_T *l;
8414 listitem_T *li;
8415
8416 l = argvars[0].vval.v_list;
8417 if (l != NULL)
8418 {
8419 li = l->lv_first;
8420 if (li != NULL)
8421 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008422 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008423 for (;;)
8424 {
8425 li = li->li_next;
8426 if (li == NULL)
8427 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008428 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008429 if (domax ? i > n : i < n)
8430 n = i;
8431 }
8432 }
8433 }
8434 }
8435 else if (argvars[0].v_type == VAR_DICT)
8436 {
8437 dict_T *d;
8438 int first = TRUE;
8439 hashitem_T *hi;
8440 int todo;
8441
8442 d = argvars[0].vval.v_dict;
8443 if (d != NULL)
8444 {
8445 todo = (int)d->dv_hashtab.ht_used;
8446 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8447 {
8448 if (!HASHITEM_EMPTY(hi))
8449 {
8450 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008451 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008452 if (first)
8453 {
8454 n = i;
8455 first = FALSE;
8456 }
8457 else if (domax ? i > n : i < n)
8458 n = i;
8459 }
8460 }
8461 }
8462 }
8463 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008464 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008465 rettv->vval.v_number = error ? 0 : n;
8466}
8467
8468/*
8469 * "max()" function
8470 */
8471 static void
8472f_max(typval_T *argvars, typval_T *rettv)
8473{
8474 max_min(argvars, rettv, TRUE);
8475}
8476
8477/*
8478 * "min()" function
8479 */
8480 static void
8481f_min(typval_T *argvars, typval_T *rettv)
8482{
8483 max_min(argvars, rettv, FALSE);
8484}
8485
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008486/*
8487 * Create the directory in which "dir" is located, and higher levels when
8488 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008489 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008490 */
8491 static int
8492mkdir_recurse(char_u *dir, int prot)
8493{
8494 char_u *p;
8495 char_u *updir;
8496 int r = FAIL;
8497
8498 /* Get end of directory name in "dir".
8499 * We're done when it's "/" or "c:/". */
8500 p = gettail_sep(dir);
8501 if (p <= get_past_head(dir))
8502 return OK;
8503
8504 /* If the directory exists we're done. Otherwise: create it.*/
8505 updir = vim_strnsave(dir, (int)(p - dir));
8506 if (updir == NULL)
8507 return FAIL;
8508 if (mch_isdir(updir))
8509 r = OK;
8510 else if (mkdir_recurse(updir, prot) == OK)
8511 r = vim_mkdir_emsg(updir, prot);
8512 vim_free(updir);
8513 return r;
8514}
8515
8516#ifdef vim_mkdir
8517/*
8518 * "mkdir()" function
8519 */
8520 static void
8521f_mkdir(typval_T *argvars, typval_T *rettv)
8522{
8523 char_u *dir;
8524 char_u buf[NUMBUFLEN];
8525 int prot = 0755;
8526
8527 rettv->vval.v_number = FAIL;
8528 if (check_restricted() || check_secure())
8529 return;
8530
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008531 dir = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008532 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008533 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008534
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008535 if (*gettail(dir) == NUL)
8536 /* remove trailing slashes */
8537 *gettail_sep(dir) = NUL;
8538
8539 if (argvars[1].v_type != VAR_UNKNOWN)
8540 {
8541 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008542 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008543 prot = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008544 if (prot == -1)
8545 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008546 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008547 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008548 {
8549 if (mch_isdir(dir))
8550 {
8551 /* With the "p" flag it's OK if the dir already exists. */
8552 rettv->vval.v_number = OK;
8553 return;
8554 }
8555 mkdir_recurse(dir, prot);
8556 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008557 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008558 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008559}
8560#endif
8561
8562/*
8563 * "mode()" function
8564 */
8565 static void
8566f_mode(typval_T *argvars, typval_T *rettv)
8567{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008568 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008569
Bram Moolenaar612cc382018-07-29 15:34:26 +02008570 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008571
8572 if (time_for_testing == 93784)
8573 {
8574 /* Testing the two-character code. */
8575 buf[0] = 'x';
8576 buf[1] = '!';
8577 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008578#ifdef FEAT_TERMINAL
8579 else if (term_use_loop())
8580 buf[0] = 't';
8581#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008582 else if (VIsual_active)
8583 {
8584 if (VIsual_select)
8585 buf[0] = VIsual_mode + 's' - 'v';
8586 else
8587 buf[0] = VIsual_mode;
8588 }
8589 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8590 || State == CONFIRM)
8591 {
8592 buf[0] = 'r';
8593 if (State == ASKMORE)
8594 buf[1] = 'm';
8595 else if (State == CONFIRM)
8596 buf[1] = '?';
8597 }
8598 else if (State == EXTERNCMD)
8599 buf[0] = '!';
8600 else if (State & INSERT)
8601 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008602 if (State & VREPLACE_FLAG)
8603 {
8604 buf[0] = 'R';
8605 buf[1] = 'v';
8606 }
8607 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008608 {
8609 if (State & REPLACE_FLAG)
8610 buf[0] = 'R';
8611 else
8612 buf[0] = 'i';
8613#ifdef FEAT_INS_EXPAND
8614 if (ins_compl_active())
8615 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008616 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008617 buf[1] = 'x';
8618#endif
8619 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008620 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008621 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008622 {
8623 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008624 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008625 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008626 else if (exmode_active == EXMODE_NORMAL)
8627 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008628 }
8629 else
8630 {
8631 buf[0] = 'n';
8632 if (finish_op)
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008633 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008634 buf[1] = 'o';
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008635 // to be able to detect force-linewise/blockwise/characterwise operations
8636 buf[2] = motion_force;
8637 }
Bram Moolenaar612cc382018-07-29 15:34:26 +02008638 else if (restart_edit == 'I' || restart_edit == 'R'
8639 || restart_edit == 'V')
8640 {
8641 buf[1] = 'i';
8642 buf[2] = restart_edit;
8643 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008644 }
8645
8646 /* Clear out the minor mode when the argument is not a non-zero number or
8647 * non-empty string. */
8648 if (!non_zero_arg(&argvars[0]))
8649 buf[1] = NUL;
8650
8651 rettv->vval.v_string = vim_strsave(buf);
8652 rettv->v_type = VAR_STRING;
8653}
8654
8655#if defined(FEAT_MZSCHEME) || defined(PROTO)
8656/*
8657 * "mzeval()" function
8658 */
8659 static void
8660f_mzeval(typval_T *argvars, typval_T *rettv)
8661{
8662 char_u *str;
8663 char_u buf[NUMBUFLEN];
8664
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008665 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008666 do_mzeval(str, rettv);
8667}
8668
8669 void
8670mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8671{
8672 typval_T argvars[3];
8673
8674 argvars[0].v_type = VAR_STRING;
8675 argvars[0].vval.v_string = name;
8676 copy_tv(args, &argvars[1]);
8677 argvars[2].v_type = VAR_UNKNOWN;
8678 f_call(argvars, rettv);
8679 clear_tv(&argvars[1]);
8680}
8681#endif
8682
8683/*
8684 * "nextnonblank()" function
8685 */
8686 static void
8687f_nextnonblank(typval_T *argvars, typval_T *rettv)
8688{
8689 linenr_T lnum;
8690
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008691 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008692 {
8693 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8694 {
8695 lnum = 0;
8696 break;
8697 }
8698 if (*skipwhite(ml_get(lnum)) != NUL)
8699 break;
8700 }
8701 rettv->vval.v_number = lnum;
8702}
8703
8704/*
8705 * "nr2char()" function
8706 */
8707 static void
8708f_nr2char(typval_T *argvars, typval_T *rettv)
8709{
8710 char_u buf[NUMBUFLEN];
8711
8712#ifdef FEAT_MBYTE
8713 if (has_mbyte)
8714 {
8715 int utf8 = 0;
8716
8717 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008718 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008719 if (utf8)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008720 buf[(*utf_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008721 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008722 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008723 }
8724 else
8725#endif
8726 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008727 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008728 buf[1] = NUL;
8729 }
8730 rettv->v_type = VAR_STRING;
8731 rettv->vval.v_string = vim_strsave(buf);
8732}
8733
8734/*
8735 * "or(expr, expr)" function
8736 */
8737 static void
8738f_or(typval_T *argvars, typval_T *rettv)
8739{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008740 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
8741 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008742}
8743
8744/*
8745 * "pathshorten()" function
8746 */
8747 static void
8748f_pathshorten(typval_T *argvars, typval_T *rettv)
8749{
8750 char_u *p;
8751
8752 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008753 p = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008754 if (p == NULL)
8755 rettv->vval.v_string = NULL;
8756 else
8757 {
8758 p = vim_strsave(p);
8759 rettv->vval.v_string = p;
8760 if (p != NULL)
8761 shorten_dir(p);
8762 }
8763}
8764
8765#ifdef FEAT_PERL
8766/*
8767 * "perleval()" function
8768 */
8769 static void
8770f_perleval(typval_T *argvars, typval_T *rettv)
8771{
8772 char_u *str;
8773 char_u buf[NUMBUFLEN];
8774
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008775 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008776 do_perleval(str, rettv);
8777}
8778#endif
8779
8780#ifdef FEAT_FLOAT
8781/*
8782 * "pow()" function
8783 */
8784 static void
8785f_pow(typval_T *argvars, typval_T *rettv)
8786{
8787 float_T fx = 0.0, fy = 0.0;
8788
8789 rettv->v_type = VAR_FLOAT;
8790 if (get_float_arg(argvars, &fx) == OK
8791 && get_float_arg(&argvars[1], &fy) == OK)
8792 rettv->vval.v_float = pow(fx, fy);
8793 else
8794 rettv->vval.v_float = 0.0;
8795}
8796#endif
8797
8798/*
8799 * "prevnonblank()" function
8800 */
8801 static void
8802f_prevnonblank(typval_T *argvars, typval_T *rettv)
8803{
8804 linenr_T lnum;
8805
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008806 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008807 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8808 lnum = 0;
8809 else
8810 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8811 --lnum;
8812 rettv->vval.v_number = lnum;
8813}
8814
8815/* This dummy va_list is here because:
8816 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8817 * - locally in the function results in a "used before set" warning
8818 * - using va_start() to initialize it gives "function with fixed args" error */
8819static va_list ap;
8820
8821/*
8822 * "printf()" function
8823 */
8824 static void
8825f_printf(typval_T *argvars, typval_T *rettv)
8826{
8827 char_u buf[NUMBUFLEN];
8828 int len;
8829 char_u *s;
8830 int saved_did_emsg = did_emsg;
8831 char *fmt;
8832
8833 rettv->v_type = VAR_STRING;
8834 rettv->vval.v_string = NULL;
8835
8836 /* Get the required length, allocate the buffer and do it for real. */
8837 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008838 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008839 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008840 if (!did_emsg)
8841 {
8842 s = alloc(len + 1);
8843 if (s != NULL)
8844 {
8845 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008846 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8847 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008848 }
8849 }
8850 did_emsg |= saved_did_emsg;
8851}
8852
Bram Moolenaarf2732452018-06-03 14:47:35 +02008853#ifdef FEAT_JOB_CHANNEL
8854/*
8855 * "prompt_setcallback({buffer}, {callback})" function
8856 */
8857 static void
8858f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
8859{
8860 buf_T *buf;
8861 char_u *callback;
8862 partial_T *partial;
8863
8864 if (check_secure())
8865 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01008866 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008867 if (buf == NULL)
8868 return;
8869
8870 callback = get_callback(&argvars[1], &partial);
8871 if (callback == NULL)
8872 return;
8873
8874 free_callback(buf->b_prompt_callback, buf->b_prompt_partial);
8875 if (partial == NULL)
8876 buf->b_prompt_callback = vim_strsave(callback);
8877 else
8878 /* pointer into the partial */
8879 buf->b_prompt_callback = callback;
8880 buf->b_prompt_partial = partial;
8881}
8882
8883/*
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02008884 * "prompt_setinterrupt({buffer}, {callback})" function
8885 */
8886 static void
8887f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
8888{
8889 buf_T *buf;
8890 char_u *callback;
8891 partial_T *partial;
8892
8893 if (check_secure())
8894 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01008895 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02008896 if (buf == NULL)
8897 return;
8898
8899 callback = get_callback(&argvars[1], &partial);
8900 if (callback == NULL)
8901 return;
8902
8903 free_callback(buf->b_prompt_interrupt, buf->b_prompt_int_partial);
8904 if (partial == NULL)
8905 buf->b_prompt_interrupt = vim_strsave(callback);
8906 else
8907 /* pointer into the partial */
8908 buf->b_prompt_interrupt = callback;
8909 buf->b_prompt_int_partial = partial;
8910}
8911
8912/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02008913 * "prompt_setprompt({buffer}, {text})" function
8914 */
8915 static void
8916f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
8917{
8918 buf_T *buf;
8919 char_u *text;
8920
8921 if (check_secure())
8922 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01008923 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008924 if (buf == NULL)
8925 return;
8926
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008927 text = tv_get_string(&argvars[1]);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008928 vim_free(buf->b_prompt_text);
8929 buf->b_prompt_text = vim_strsave(text);
8930}
8931#endif
8932
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008933/*
8934 * "pumvisible()" function
8935 */
8936 static void
8937f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8938{
8939#ifdef FEAT_INS_EXPAND
8940 if (pum_visible())
8941 rettv->vval.v_number = 1;
8942#endif
8943}
8944
8945#ifdef FEAT_PYTHON3
8946/*
8947 * "py3eval()" function
8948 */
8949 static void
8950f_py3eval(typval_T *argvars, typval_T *rettv)
8951{
8952 char_u *str;
8953 char_u buf[NUMBUFLEN];
8954
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008955 if (p_pyx == 0)
8956 p_pyx = 3;
8957
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008958 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008959 do_py3eval(str, rettv);
8960}
8961#endif
8962
8963#ifdef FEAT_PYTHON
8964/*
8965 * "pyeval()" function
8966 */
8967 static void
8968f_pyeval(typval_T *argvars, typval_T *rettv)
8969{
8970 char_u *str;
8971 char_u buf[NUMBUFLEN];
8972
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008973 if (p_pyx == 0)
8974 p_pyx = 2;
8975
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008976 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008977 do_pyeval(str, rettv);
8978}
8979#endif
8980
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008981#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8982/*
8983 * "pyxeval()" function
8984 */
8985 static void
8986f_pyxeval(typval_T *argvars, typval_T *rettv)
8987{
8988# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8989 init_pyxversion();
8990 if (p_pyx == 2)
8991 f_pyeval(argvars, rettv);
8992 else
8993 f_py3eval(argvars, rettv);
8994# elif defined(FEAT_PYTHON)
8995 f_pyeval(argvars, rettv);
8996# elif defined(FEAT_PYTHON3)
8997 f_py3eval(argvars, rettv);
8998# endif
8999}
9000#endif
9001
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009002/*
9003 * "range()" function
9004 */
9005 static void
9006f_range(typval_T *argvars, typval_T *rettv)
9007{
9008 varnumber_T start;
9009 varnumber_T end;
9010 varnumber_T stride = 1;
9011 varnumber_T i;
9012 int error = FALSE;
9013
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009014 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009015 if (argvars[1].v_type == VAR_UNKNOWN)
9016 {
9017 end = start - 1;
9018 start = 0;
9019 }
9020 else
9021 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009022 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009023 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009024 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009025 }
9026
9027 if (error)
9028 return; /* type error; errmsg already given */
9029 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009030 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009031 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009032 emsg(_("E727: Start past end"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009033 else
9034 {
9035 if (rettv_list_alloc(rettv) == OK)
9036 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
9037 if (list_append_number(rettv->vval.v_list,
9038 (varnumber_T)i) == FAIL)
9039 break;
9040 }
9041}
9042
9043/*
9044 * "readfile()" function
9045 */
9046 static void
9047f_readfile(typval_T *argvars, typval_T *rettv)
9048{
9049 int binary = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009050 int blob = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009051 int failed = FALSE;
9052 char_u *fname;
9053 FILE *fd;
9054 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
9055 int io_size = sizeof(buf);
9056 int readlen; /* size of last fread() */
9057 char_u *prev = NULL; /* previously read bytes, if any */
9058 long prevlen = 0; /* length of data in prev */
9059 long prevsize = 0; /* size of prev buffer */
9060 long maxline = MAXLNUM;
9061 long cnt = 0;
9062 char_u *p; /* position in buf */
9063 char_u *start; /* start of current line */
9064
9065 if (argvars[1].v_type != VAR_UNKNOWN)
9066 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009067 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009068 binary = TRUE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009069 if (STRCMP(tv_get_string(&argvars[1]), "B") == 0)
9070 blob = TRUE;
9071
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009072 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009073 maxline = (long)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009074 }
9075
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009076 if (blob)
9077 {
9078 if (rettv_blob_alloc(rettv) == FAIL)
9079 return;
9080 }
9081 else
9082 {
9083 if (rettv_list_alloc(rettv) == FAIL)
9084 return;
9085 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009086
9087 /* Always open the file in binary mode, library functions have a mind of
9088 * their own about CR-LF conversion. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009089 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009090 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
9091 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009092 semsg(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009093 return;
9094 }
9095
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009096 if (blob)
9097 {
9098 if (read_blob(fd, rettv->vval.v_blob) == FAIL)
9099 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009100 emsg("cannot read file");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009101 blob_free(rettv->vval.v_blob);
9102 }
9103 fclose(fd);
9104 return;
9105 }
9106
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009107 while (cnt < maxline || maxline < 0)
9108 {
9109 readlen = (int)fread(buf, 1, io_size, fd);
9110
9111 /* This for loop processes what was read, but is also entered at end
9112 * of file so that either:
9113 * - an incomplete line gets written
9114 * - a "binary" file gets an empty line at the end if it ends in a
9115 * newline. */
9116 for (p = buf, start = buf;
9117 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
9118 ++p)
9119 {
9120 if (*p == '\n' || readlen <= 0)
9121 {
9122 listitem_T *li;
9123 char_u *s = NULL;
9124 long_u len = p - start;
9125
9126 /* Finished a line. Remove CRs before NL. */
9127 if (readlen > 0 && !binary)
9128 {
9129 while (len > 0 && start[len - 1] == '\r')
9130 --len;
9131 /* removal may cross back to the "prev" string */
9132 if (len == 0)
9133 while (prevlen > 0 && prev[prevlen - 1] == '\r')
9134 --prevlen;
9135 }
9136 if (prevlen == 0)
9137 s = vim_strnsave(start, (int)len);
9138 else
9139 {
9140 /* Change "prev" buffer to be the right size. This way
9141 * the bytes are only copied once, and very long lines are
9142 * allocated only once. */
9143 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
9144 {
9145 mch_memmove(s + prevlen, start, len);
9146 s[prevlen + len] = NUL;
9147 prev = NULL; /* the list will own the string */
9148 prevlen = prevsize = 0;
9149 }
9150 }
9151 if (s == NULL)
9152 {
9153 do_outofmem_msg((long_u) prevlen + len + 1);
9154 failed = TRUE;
9155 break;
9156 }
9157
9158 if ((li = listitem_alloc()) == NULL)
9159 {
9160 vim_free(s);
9161 failed = TRUE;
9162 break;
9163 }
9164 li->li_tv.v_type = VAR_STRING;
9165 li->li_tv.v_lock = 0;
9166 li->li_tv.vval.v_string = s;
9167 list_append(rettv->vval.v_list, li);
9168
9169 start = p + 1; /* step over newline */
9170 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
9171 break;
9172 }
9173 else if (*p == NUL)
9174 *p = '\n';
9175#ifdef FEAT_MBYTE
9176 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
9177 * when finding the BF and check the previous two bytes. */
9178 else if (*p == 0xbf && enc_utf8 && !binary)
9179 {
9180 /* Find the two bytes before the 0xbf. If p is at buf, or buf
9181 * + 1, these may be in the "prev" string. */
9182 char_u back1 = p >= buf + 1 ? p[-1]
9183 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
9184 char_u back2 = p >= buf + 2 ? p[-2]
9185 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
9186 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
9187
9188 if (back2 == 0xef && back1 == 0xbb)
9189 {
9190 char_u *dest = p - 2;
9191
9192 /* Usually a BOM is at the beginning of a file, and so at
9193 * the beginning of a line; then we can just step over it.
9194 */
9195 if (start == dest)
9196 start = p + 1;
9197 else
9198 {
9199 /* have to shuffle buf to close gap */
9200 int adjust_prevlen = 0;
9201
9202 if (dest < buf)
9203 {
9204 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
9205 dest = buf;
9206 }
9207 if (readlen > p - buf + 1)
9208 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
9209 readlen -= 3 - adjust_prevlen;
9210 prevlen -= adjust_prevlen;
9211 p = dest - 1;
9212 }
9213 }
9214 }
9215#endif
9216 } /* for */
9217
9218 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
9219 break;
9220 if (start < p)
9221 {
9222 /* There's part of a line in buf, store it in "prev". */
9223 if (p - start + prevlen >= prevsize)
9224 {
9225 /* need bigger "prev" buffer */
9226 char_u *newprev;
9227
9228 /* A common use case is ordinary text files and "prev" gets a
9229 * fragment of a line, so the first allocation is made
9230 * small, to avoid repeatedly 'allocing' large and
9231 * 'reallocing' small. */
9232 if (prevsize == 0)
9233 prevsize = (long)(p - start);
9234 else
9235 {
9236 long grow50pc = (prevsize * 3) / 2;
9237 long growmin = (long)((p - start) * 2 + prevlen);
9238 prevsize = grow50pc > growmin ? grow50pc : growmin;
9239 }
9240 newprev = prev == NULL ? alloc(prevsize)
9241 : vim_realloc(prev, prevsize);
9242 if (newprev == NULL)
9243 {
9244 do_outofmem_msg((long_u)prevsize);
9245 failed = TRUE;
9246 break;
9247 }
9248 prev = newprev;
9249 }
9250 /* Add the line part to end of "prev". */
9251 mch_memmove(prev + prevlen, start, p - start);
9252 prevlen += (long)(p - start);
9253 }
9254 } /* while */
9255
9256 /*
9257 * For a negative line count use only the lines at the end of the file,
9258 * free the rest.
9259 */
9260 if (!failed && maxline < 0)
9261 while (cnt > -maxline)
9262 {
9263 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
9264 --cnt;
9265 }
9266
9267 if (failed)
9268 {
9269 list_free(rettv->vval.v_list);
9270 /* readfile doc says an empty list is returned on error */
9271 rettv->vval.v_list = list_alloc();
9272 }
9273
9274 vim_free(prev);
9275 fclose(fd);
9276}
9277
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009278 static void
9279return_register(int regname, typval_T *rettv)
9280{
9281 char_u buf[2] = {0, 0};
9282
9283 buf[0] = (char_u)regname;
9284 rettv->v_type = VAR_STRING;
9285 rettv->vval.v_string = vim_strsave(buf);
9286}
9287
9288/*
9289 * "reg_executing()" function
9290 */
9291 static void
9292f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9293{
9294 return_register(reg_executing, rettv);
9295}
9296
9297/*
9298 * "reg_recording()" function
9299 */
9300 static void
9301f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9302{
9303 return_register(reg_recording, rettv);
9304}
9305
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009306#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009307/*
9308 * Convert a List to proftime_T.
9309 * Return FAIL when there is something wrong.
9310 */
9311 static int
9312list2proftime(typval_T *arg, proftime_T *tm)
9313{
9314 long n1, n2;
9315 int error = FALSE;
9316
9317 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9318 || arg->vval.v_list->lv_len != 2)
9319 return FAIL;
9320 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9321 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
9322# ifdef WIN3264
9323 tm->HighPart = n1;
9324 tm->LowPart = n2;
9325# else
9326 tm->tv_sec = n1;
9327 tm->tv_usec = n2;
9328# endif
9329 return error ? FAIL : OK;
9330}
9331#endif /* FEAT_RELTIME */
9332
9333/*
9334 * "reltime()" function
9335 */
9336 static void
9337f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9338{
9339#ifdef FEAT_RELTIME
9340 proftime_T res;
9341 proftime_T start;
9342
9343 if (argvars[0].v_type == VAR_UNKNOWN)
9344 {
9345 /* No arguments: get current time. */
9346 profile_start(&res);
9347 }
9348 else if (argvars[1].v_type == VAR_UNKNOWN)
9349 {
9350 if (list2proftime(&argvars[0], &res) == FAIL)
9351 return;
9352 profile_end(&res);
9353 }
9354 else
9355 {
9356 /* Two arguments: compute the difference. */
9357 if (list2proftime(&argvars[0], &start) == FAIL
9358 || list2proftime(&argvars[1], &res) == FAIL)
9359 return;
9360 profile_sub(&res, &start);
9361 }
9362
9363 if (rettv_list_alloc(rettv) == OK)
9364 {
9365 long n1, n2;
9366
9367# ifdef WIN3264
9368 n1 = res.HighPart;
9369 n2 = res.LowPart;
9370# else
9371 n1 = res.tv_sec;
9372 n2 = res.tv_usec;
9373# endif
9374 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9375 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9376 }
9377#endif
9378}
9379
9380#ifdef FEAT_FLOAT
9381/*
9382 * "reltimefloat()" function
9383 */
9384 static void
9385f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9386{
9387# ifdef FEAT_RELTIME
9388 proftime_T tm;
9389# endif
9390
9391 rettv->v_type = VAR_FLOAT;
9392 rettv->vval.v_float = 0;
9393# ifdef FEAT_RELTIME
9394 if (list2proftime(&argvars[0], &tm) == OK)
9395 rettv->vval.v_float = profile_float(&tm);
9396# endif
9397}
9398#endif
9399
9400/*
9401 * "reltimestr()" function
9402 */
9403 static void
9404f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9405{
9406#ifdef FEAT_RELTIME
9407 proftime_T tm;
9408#endif
9409
9410 rettv->v_type = VAR_STRING;
9411 rettv->vval.v_string = NULL;
9412#ifdef FEAT_RELTIME
9413 if (list2proftime(&argvars[0], &tm) == OK)
9414 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9415#endif
9416}
9417
9418#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009419 static void
9420make_connection(void)
9421{
9422 if (X_DISPLAY == NULL
9423# ifdef FEAT_GUI
9424 && !gui.in_use
9425# endif
9426 )
9427 {
9428 x_force_connect = TRUE;
9429 setup_term_clip();
9430 x_force_connect = FALSE;
9431 }
9432}
9433
9434 static int
9435check_connection(void)
9436{
9437 make_connection();
9438 if (X_DISPLAY == NULL)
9439 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009440 emsg(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009441 return FAIL;
9442 }
9443 return OK;
9444}
9445#endif
9446
9447#ifdef FEAT_CLIENTSERVER
9448 static void
9449remote_common(typval_T *argvars, typval_T *rettv, int expr)
9450{
9451 char_u *server_name;
9452 char_u *keys;
9453 char_u *r = NULL;
9454 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009455 int timeout = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009456# ifdef WIN32
9457 HWND w;
9458# else
9459 Window w;
9460# endif
9461
9462 if (check_restricted() || check_secure())
9463 return;
9464
9465# ifdef FEAT_X11
9466 if (check_connection() == FAIL)
9467 return;
9468# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009469 if (argvars[2].v_type != VAR_UNKNOWN
9470 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009471 timeout = tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009472
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009473 server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009474 if (server_name == NULL)
9475 return; /* type error; errmsg already given */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009476 keys = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009477# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009478 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009479# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009480 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9481 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009482# endif
9483 {
9484 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009485 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009486 emsg((char *)r); // sending worked but evaluation failed
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009487 vim_free(r);
9488 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009489 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009490 semsg(_("E241: Unable to send to %s"), server_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009491 return;
9492 }
9493
9494 rettv->vval.v_string = r;
9495
9496 if (argvars[2].v_type != VAR_UNKNOWN)
9497 {
9498 dictitem_T v;
9499 char_u str[30];
9500 char_u *idvar;
9501
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009502 idvar = tv_get_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009503 if (idvar != NULL && *idvar != NUL)
9504 {
9505 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9506 v.di_tv.v_type = VAR_STRING;
9507 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009508 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009509 vim_free(v.di_tv.vval.v_string);
9510 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009511 }
9512}
9513#endif
9514
9515/*
9516 * "remote_expr()" function
9517 */
9518 static void
9519f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9520{
9521 rettv->v_type = VAR_STRING;
9522 rettv->vval.v_string = NULL;
9523#ifdef FEAT_CLIENTSERVER
9524 remote_common(argvars, rettv, TRUE);
9525#endif
9526}
9527
9528/*
9529 * "remote_foreground()" function
9530 */
9531 static void
9532f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9533{
9534#ifdef FEAT_CLIENTSERVER
9535# ifdef WIN32
9536 /* On Win32 it's done in this application. */
9537 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009538 char_u *server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009539
9540 if (server_name != NULL)
9541 serverForeground(server_name);
9542 }
9543# else
9544 /* Send a foreground() expression to the server. */
9545 argvars[1].v_type = VAR_STRING;
9546 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9547 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009548 rettv->v_type = VAR_STRING;
9549 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009550 remote_common(argvars, rettv, TRUE);
9551 vim_free(argvars[1].vval.v_string);
9552# endif
9553#endif
9554}
9555
9556 static void
9557f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
9558{
9559#ifdef FEAT_CLIENTSERVER
9560 dictitem_T v;
9561 char_u *s = NULL;
9562# ifdef WIN32
9563 long_u n = 0;
9564# endif
9565 char_u *serverid;
9566
9567 if (check_restricted() || check_secure())
9568 {
9569 rettv->vval.v_number = -1;
9570 return;
9571 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009572 serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009573 if (serverid == NULL)
9574 {
9575 rettv->vval.v_number = -1;
9576 return; /* type error; errmsg already given */
9577 }
9578# ifdef WIN32
9579 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
9580 if (n == 0)
9581 rettv->vval.v_number = -1;
9582 else
9583 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009584 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009585 rettv->vval.v_number = (s != NULL);
9586 }
9587# else
9588 if (check_connection() == FAIL)
9589 return;
9590
9591 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
9592 serverStrToWin(serverid), &s);
9593# endif
9594
9595 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
9596 {
9597 char_u *retvar;
9598
9599 v.di_tv.v_type = VAR_STRING;
9600 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009601 retvar = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009602 if (retvar != NULL)
9603 set_var(retvar, &v.di_tv, FALSE);
9604 vim_free(v.di_tv.vval.v_string);
9605 }
9606#else
9607 rettv->vval.v_number = -1;
9608#endif
9609}
9610
9611 static void
9612f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
9613{
9614 char_u *r = NULL;
9615
9616#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009617 char_u *serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009618
9619 if (serverid != NULL && !check_restricted() && !check_secure())
9620 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009621 int timeout = 0;
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009622# ifdef WIN32
9623 /* The server's HWND is encoded in the 'id' parameter */
9624 long_u n = 0;
9625# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009626
9627 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009628 timeout = tv_get_number(&argvars[1]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009629
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009630# ifdef WIN32
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009631 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
9632 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009633 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009634 if (r == NULL)
9635# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009636 if (check_connection() == FAIL
9637 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
9638 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009639# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009640 emsg(_("E277: Unable to read a server reply"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009641 }
9642#endif
9643 rettv->v_type = VAR_STRING;
9644 rettv->vval.v_string = r;
9645}
9646
9647/*
9648 * "remote_send()" function
9649 */
9650 static void
9651f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9652{
9653 rettv->v_type = VAR_STRING;
9654 rettv->vval.v_string = NULL;
9655#ifdef FEAT_CLIENTSERVER
9656 remote_common(argvars, rettv, FALSE);
9657#endif
9658}
9659
9660/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009661 * "remote_startserver()" function
9662 */
9663 static void
9664f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9665{
9666#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009667 char_u *server = tv_get_string_chk(&argvars[0]);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009668
9669 if (server == NULL)
9670 return; /* type error; errmsg already given */
9671 if (serverName != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009672 emsg(_("E941: already started a server"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009673 else
9674 {
9675# ifdef FEAT_X11
9676 if (check_connection() == OK)
9677 serverRegisterName(X_DISPLAY, server);
9678# else
9679 serverSetName(server);
9680# endif
9681 }
9682#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009683 emsg(_("E942: +clientserver feature not available"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009684#endif
9685}
9686
9687/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009688 * "remove()" function
9689 */
9690 static void
9691f_remove(typval_T *argvars, typval_T *rettv)
9692{
9693 list_T *l;
9694 listitem_T *item, *item2;
9695 listitem_T *li;
9696 long idx;
9697 long end;
9698 char_u *key;
9699 dict_T *d;
9700 dictitem_T *di;
9701 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009702 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009703
9704 if (argvars[0].v_type == VAR_DICT)
9705 {
9706 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009707 semsg(_(e_toomanyarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009708 else if ((d = argvars[0].vval.v_dict) != NULL
9709 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
9710 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009711 key = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009712 if (key != NULL)
9713 {
9714 di = dict_find(d, key, -1);
9715 if (di == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009716 semsg(_(e_dictkey), key);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009717 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
9718 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
9719 {
9720 *rettv = di->di_tv;
9721 init_tv(&di->di_tv);
9722 dictitem_remove(d, di);
9723 }
9724 }
9725 }
9726 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009727 else if (argvars[0].v_type == VAR_BLOB)
9728 {
9729 idx = (long)tv_get_number_chk(&argvars[1], &error);
9730 if (!error)
9731 {
9732 blob_T *b = argvars[0].vval.v_blob;
9733 int len = blob_len(b);
9734 char_u *p;
9735
9736 if (idx < 0)
9737 // count from the end
9738 idx = len + idx;
9739 if (idx < 0 || idx >= len)
9740 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009741 semsg(_(e_blobidx), idx);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009742 return;
9743 }
9744 if (argvars[2].v_type == VAR_UNKNOWN)
9745 {
9746 // Remove one item, return its value.
9747 p = (char_u *)b->bv_ga.ga_data;
9748 rettv->vval.v_number = (varnumber_T) *(p + idx);
9749 mch_memmove(p + idx, p + idx + 1, (size_t)len - idx - 1);
9750 --b->bv_ga.ga_len;
9751 }
9752 else
9753 {
9754 blob_T *blob;
9755
9756 // Remove range of items, return list with values.
9757 end = (long)tv_get_number_chk(&argvars[2], &error);
9758 if (error)
9759 return;
9760 if (end < 0)
9761 // count from the end
9762 end = len + end;
9763 if (end >= len || idx > end)
9764 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009765 semsg(_(e_blobidx), end);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009766 return;
9767 }
9768 blob = blob_alloc();
9769 if (blob == NULL)
9770 return;
9771 blob->bv_ga.ga_len = end - idx + 1;
9772 if (ga_grow(&blob->bv_ga, end - idx + 1) == FAIL)
9773 {
9774 vim_free(blob);
9775 return;
9776 }
9777 p = (char_u *)b->bv_ga.ga_data;
9778 mch_memmove((char_u *)blob->bv_ga.ga_data, p + idx,
9779 (size_t)(end - idx + 1));
9780 ++blob->bv_refcount;
9781 rettv->v_type = VAR_BLOB;
9782 rettv->vval.v_blob = blob;
9783
9784 mch_memmove(p + idx, p + end + 1, (size_t)(len - end));
9785 b->bv_ga.ga_len -= end - idx + 1;
9786 }
9787 }
9788 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009789 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009790 semsg(_(e_listdictarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009791 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009792 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009793 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009794 idx = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009795 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009796 ; // type error: do nothing, errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009797 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009798 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009799 else
9800 {
9801 if (argvars[2].v_type == VAR_UNKNOWN)
9802 {
9803 /* Remove one item, return its value. */
9804 vimlist_remove(l, item, item);
9805 *rettv = item->li_tv;
9806 vim_free(item);
9807 }
9808 else
9809 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009810 // Remove range of items, return list with values.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009811 end = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009812 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009813 ; // type error: do nothing
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009814 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009815 semsg(_(e_listidx), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009816 else
9817 {
9818 int cnt = 0;
9819
9820 for (li = item; li != NULL; li = li->li_next)
9821 {
9822 ++cnt;
9823 if (li == item2)
9824 break;
9825 }
9826 if (li == NULL) /* didn't find "item2" after "item" */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009827 emsg(_(e_invrange));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009828 else
9829 {
9830 vimlist_remove(l, item, item2);
9831 if (rettv_list_alloc(rettv) == OK)
9832 {
9833 l = rettv->vval.v_list;
9834 l->lv_first = item;
9835 l->lv_last = item2;
9836 item->li_prev = NULL;
9837 item2->li_next = NULL;
9838 l->lv_len = cnt;
9839 }
9840 }
9841 }
9842 }
9843 }
9844 }
9845}
9846
9847/*
9848 * "rename({from}, {to})" function
9849 */
9850 static void
9851f_rename(typval_T *argvars, typval_T *rettv)
9852{
9853 char_u buf[NUMBUFLEN];
9854
9855 if (check_restricted() || check_secure())
9856 rettv->vval.v_number = -1;
9857 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009858 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
9859 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009860}
9861
9862/*
9863 * "repeat()" function
9864 */
9865 static void
9866f_repeat(typval_T *argvars, typval_T *rettv)
9867{
9868 char_u *p;
9869 int n;
9870 int slen;
9871 int len;
9872 char_u *r;
9873 int i;
9874
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009875 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009876 if (argvars[0].v_type == VAR_LIST)
9877 {
9878 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9879 while (n-- > 0)
9880 if (list_extend(rettv->vval.v_list,
9881 argvars[0].vval.v_list, NULL) == FAIL)
9882 break;
9883 }
9884 else
9885 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009886 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009887 rettv->v_type = VAR_STRING;
9888 rettv->vval.v_string = NULL;
9889
9890 slen = (int)STRLEN(p);
9891 len = slen * n;
9892 if (len <= 0)
9893 return;
9894
9895 r = alloc(len + 1);
9896 if (r != NULL)
9897 {
9898 for (i = 0; i < n; i++)
9899 mch_memmove(r + i * slen, p, (size_t)slen);
9900 r[len] = NUL;
9901 }
9902
9903 rettv->vval.v_string = r;
9904 }
9905}
9906
9907/*
9908 * "resolve()" function
9909 */
9910 static void
9911f_resolve(typval_T *argvars, typval_T *rettv)
9912{
9913 char_u *p;
9914#ifdef HAVE_READLINK
9915 char_u *buf = NULL;
9916#endif
9917
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009918 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009919#ifdef FEAT_SHORTCUT
9920 {
9921 char_u *v = NULL;
9922
9923 v = mch_resolve_shortcut(p);
9924 if (v != NULL)
9925 rettv->vval.v_string = v;
9926 else
9927 rettv->vval.v_string = vim_strsave(p);
9928 }
9929#else
9930# ifdef HAVE_READLINK
9931 {
9932 char_u *cpy;
9933 int len;
9934 char_u *remain = NULL;
9935 char_u *q;
9936 int is_relative_to_current = FALSE;
9937 int has_trailing_pathsep = FALSE;
9938 int limit = 100;
9939
9940 p = vim_strsave(p);
9941
9942 if (p[0] == '.' && (vim_ispathsep(p[1])
9943 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9944 is_relative_to_current = TRUE;
9945
9946 len = STRLEN(p);
9947 if (len > 0 && after_pathsep(p, p + len))
9948 {
9949 has_trailing_pathsep = TRUE;
9950 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9951 }
9952
9953 q = getnextcomp(p);
9954 if (*q != NUL)
9955 {
9956 /* Separate the first path component in "p", and keep the
9957 * remainder (beginning with the path separator). */
9958 remain = vim_strsave(q - 1);
9959 q[-1] = NUL;
9960 }
9961
9962 buf = alloc(MAXPATHL + 1);
9963 if (buf == NULL)
9964 goto fail;
9965
9966 for (;;)
9967 {
9968 for (;;)
9969 {
9970 len = readlink((char *)p, (char *)buf, MAXPATHL);
9971 if (len <= 0)
9972 break;
9973 buf[len] = NUL;
9974
9975 if (limit-- == 0)
9976 {
9977 vim_free(p);
9978 vim_free(remain);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009979 emsg(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009980 rettv->vval.v_string = NULL;
9981 goto fail;
9982 }
9983
9984 /* Ensure that the result will have a trailing path separator
9985 * if the argument has one. */
9986 if (remain == NULL && has_trailing_pathsep)
9987 add_pathsep(buf);
9988
9989 /* Separate the first path component in the link value and
9990 * concatenate the remainders. */
9991 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9992 if (*q != NUL)
9993 {
9994 if (remain == NULL)
9995 remain = vim_strsave(q - 1);
9996 else
9997 {
9998 cpy = concat_str(q - 1, remain);
9999 if (cpy != NULL)
10000 {
10001 vim_free(remain);
10002 remain = cpy;
10003 }
10004 }
10005 q[-1] = NUL;
10006 }
10007
10008 q = gettail(p);
10009 if (q > p && *q == NUL)
10010 {
10011 /* Ignore trailing path separator. */
10012 q[-1] = NUL;
10013 q = gettail(p);
10014 }
10015 if (q > p && !mch_isFullName(buf))
10016 {
10017 /* symlink is relative to directory of argument */
10018 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
10019 if (cpy != NULL)
10020 {
10021 STRCPY(cpy, p);
10022 STRCPY(gettail(cpy), buf);
10023 vim_free(p);
10024 p = cpy;
10025 }
10026 }
10027 else
10028 {
10029 vim_free(p);
10030 p = vim_strsave(buf);
10031 }
10032 }
10033
10034 if (remain == NULL)
10035 break;
10036
10037 /* Append the first path component of "remain" to "p". */
10038 q = getnextcomp(remain + 1);
10039 len = q - remain - (*q != NUL);
10040 cpy = vim_strnsave(p, STRLEN(p) + len);
10041 if (cpy != NULL)
10042 {
10043 STRNCAT(cpy, remain, len);
10044 vim_free(p);
10045 p = cpy;
10046 }
10047 /* Shorten "remain". */
10048 if (*q != NUL)
10049 STRMOVE(remain, q - 1);
10050 else
Bram Moolenaard23a8232018-02-10 18:45:26 +010010051 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010052 }
10053
10054 /* If the result is a relative path name, make it explicitly relative to
10055 * the current directory if and only if the argument had this form. */
10056 if (!vim_ispathsep(*p))
10057 {
10058 if (is_relative_to_current
10059 && *p != NUL
10060 && !(p[0] == '.'
10061 && (p[1] == NUL
10062 || vim_ispathsep(p[1])
10063 || (p[1] == '.'
10064 && (p[2] == NUL
10065 || vim_ispathsep(p[2]))))))
10066 {
10067 /* Prepend "./". */
10068 cpy = concat_str((char_u *)"./", p);
10069 if (cpy != NULL)
10070 {
10071 vim_free(p);
10072 p = cpy;
10073 }
10074 }
10075 else if (!is_relative_to_current)
10076 {
10077 /* Strip leading "./". */
10078 q = p;
10079 while (q[0] == '.' && vim_ispathsep(q[1]))
10080 q += 2;
10081 if (q > p)
10082 STRMOVE(p, p + 2);
10083 }
10084 }
10085
10086 /* Ensure that the result will have no trailing path separator
10087 * if the argument had none. But keep "/" or "//". */
10088 if (!has_trailing_pathsep)
10089 {
10090 q = p + STRLEN(p);
10091 if (after_pathsep(p, q))
10092 *gettail_sep(p) = NUL;
10093 }
10094
10095 rettv->vval.v_string = p;
10096 }
10097# else
10098 rettv->vval.v_string = vim_strsave(p);
10099# endif
10100#endif
10101
10102 simplify_filename(rettv->vval.v_string);
10103
10104#ifdef HAVE_READLINK
10105fail:
10106 vim_free(buf);
10107#endif
10108 rettv->v_type = VAR_STRING;
10109}
10110
10111/*
10112 * "reverse({list})" function
10113 */
10114 static void
10115f_reverse(typval_T *argvars, typval_T *rettv)
10116{
10117 list_T *l;
10118 listitem_T *li, *ni;
10119
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010120 if (argvars[0].v_type == VAR_BLOB)
10121 {
10122 blob_T *b = argvars[0].vval.v_blob;
10123 int i, len = blob_len(b);
10124
10125 for (i = 0; i < len / 2; i++)
10126 {
10127 int tmp = blob_get(b, i);
10128
10129 blob_set(b, i, blob_get(b, len - i - 1));
10130 blob_set(b, len - i - 1, tmp);
10131 }
10132 rettv_blob_set(rettv, b);
10133 return;
10134 }
10135
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010136 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010137 semsg(_(e_listarg), "reverse()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010138 else if ((l = argvars[0].vval.v_list) != NULL
10139 && !tv_check_lock(l->lv_lock,
10140 (char_u *)N_("reverse() argument"), TRUE))
10141 {
10142 li = l->lv_last;
10143 l->lv_first = l->lv_last = NULL;
10144 l->lv_len = 0;
10145 while (li != NULL)
10146 {
10147 ni = li->li_prev;
10148 list_append(l, li);
10149 li = ni;
10150 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020010151 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010152 l->lv_idx = l->lv_len - l->lv_idx - 1;
10153 }
10154}
10155
10156#define SP_NOMOVE 0x01 /* don't move cursor */
10157#define SP_REPEAT 0x02 /* repeat to find outer pair */
10158#define SP_RETCOUNT 0x04 /* return matchcount */
10159#define SP_SETPCMARK 0x08 /* set previous context mark */
10160#define SP_START 0x10 /* accept match at start position */
10161#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
10162#define SP_END 0x40 /* leave cursor at end of match */
10163#define SP_COLUMN 0x80 /* start at cursor column */
10164
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010165/*
10166 * Get flags for a search function.
10167 * Possibly sets "p_ws".
10168 * Returns BACKWARD, FORWARD or zero (for an error).
10169 */
10170 static int
10171get_search_arg(typval_T *varp, int *flagsp)
10172{
10173 int dir = FORWARD;
10174 char_u *flags;
10175 char_u nbuf[NUMBUFLEN];
10176 int mask;
10177
10178 if (varp->v_type != VAR_UNKNOWN)
10179 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010180 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010181 if (flags == NULL)
10182 return 0; /* type error; errmsg already given */
10183 while (*flags != NUL)
10184 {
10185 switch (*flags)
10186 {
10187 case 'b': dir = BACKWARD; break;
10188 case 'w': p_ws = TRUE; break;
10189 case 'W': p_ws = FALSE; break;
10190 default: mask = 0;
10191 if (flagsp != NULL)
10192 switch (*flags)
10193 {
10194 case 'c': mask = SP_START; break;
10195 case 'e': mask = SP_END; break;
10196 case 'm': mask = SP_RETCOUNT; break;
10197 case 'n': mask = SP_NOMOVE; break;
10198 case 'p': mask = SP_SUBPAT; break;
10199 case 'r': mask = SP_REPEAT; break;
10200 case 's': mask = SP_SETPCMARK; break;
10201 case 'z': mask = SP_COLUMN; break;
10202 }
10203 if (mask == 0)
10204 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010205 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010206 dir = 0;
10207 }
10208 else
10209 *flagsp |= mask;
10210 }
10211 if (dir == 0)
10212 break;
10213 ++flags;
10214 }
10215 }
10216 return dir;
10217}
10218
10219/*
10220 * Shared by search() and searchpos() functions.
10221 */
10222 static int
10223search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
10224{
10225 int flags;
10226 char_u *pat;
10227 pos_T pos;
10228 pos_T save_cursor;
10229 int save_p_ws = p_ws;
10230 int dir;
10231 int retval = 0; /* default: FAIL */
10232 long lnum_stop = 0;
10233 proftime_T tm;
10234#ifdef FEAT_RELTIME
10235 long time_limit = 0;
10236#endif
10237 int options = SEARCH_KEEP;
10238 int subpatnum;
10239
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010240 pat = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010241 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
10242 if (dir == 0)
10243 goto theend;
10244 flags = *flagsp;
10245 if (flags & SP_START)
10246 options |= SEARCH_START;
10247 if (flags & SP_END)
10248 options |= SEARCH_END;
10249 if (flags & SP_COLUMN)
10250 options |= SEARCH_COL;
10251
10252 /* Optional arguments: line number to stop searching and timeout. */
10253 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
10254 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010255 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010256 if (lnum_stop < 0)
10257 goto theend;
10258#ifdef FEAT_RELTIME
10259 if (argvars[3].v_type != VAR_UNKNOWN)
10260 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010261 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010262 if (time_limit < 0)
10263 goto theend;
10264 }
10265#endif
10266 }
10267
10268#ifdef FEAT_RELTIME
10269 /* Set the time limit, if there is one. */
10270 profile_setlimit(time_limit, &tm);
10271#endif
10272
10273 /*
10274 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
10275 * Check to make sure only those flags are set.
10276 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
10277 * flags cannot be set. Check for that condition also.
10278 */
10279 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
10280 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10281 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010282 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010283 goto theend;
10284 }
10285
10286 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010287 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010288 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010289 if (subpatnum != FAIL)
10290 {
10291 if (flags & SP_SUBPAT)
10292 retval = subpatnum;
10293 else
10294 retval = pos.lnum;
10295 if (flags & SP_SETPCMARK)
10296 setpcmark();
10297 curwin->w_cursor = pos;
10298 if (match_pos != NULL)
10299 {
10300 /* Store the match cursor position */
10301 match_pos->lnum = pos.lnum;
10302 match_pos->col = pos.col + 1;
10303 }
10304 /* "/$" will put the cursor after the end of the line, may need to
10305 * correct that here */
10306 check_cursor();
10307 }
10308
10309 /* If 'n' flag is used: restore cursor position. */
10310 if (flags & SP_NOMOVE)
10311 curwin->w_cursor = save_cursor;
10312 else
10313 curwin->w_set_curswant = TRUE;
10314theend:
10315 p_ws = save_p_ws;
10316
10317 return retval;
10318}
10319
10320#ifdef FEAT_FLOAT
10321
10322/*
10323 * round() is not in C90, use ceil() or floor() instead.
10324 */
10325 float_T
10326vim_round(float_T f)
10327{
10328 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
10329}
10330
10331/*
10332 * "round({float})" function
10333 */
10334 static void
10335f_round(typval_T *argvars, typval_T *rettv)
10336{
10337 float_T f = 0.0;
10338
10339 rettv->v_type = VAR_FLOAT;
10340 if (get_float_arg(argvars, &f) == OK)
10341 rettv->vval.v_float = vim_round(f);
10342 else
10343 rettv->vval.v_float = 0.0;
10344}
10345#endif
10346
10347/*
10348 * "screenattr()" function
10349 */
10350 static void
10351f_screenattr(typval_T *argvars, typval_T *rettv)
10352{
10353 int row;
10354 int col;
10355 int c;
10356
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010357 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10358 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010359 if (row < 0 || row >= screen_Rows
10360 || col < 0 || col >= screen_Columns)
10361 c = -1;
10362 else
10363 c = ScreenAttrs[LineOffset[row] + col];
10364 rettv->vval.v_number = c;
10365}
10366
10367/*
10368 * "screenchar()" function
10369 */
10370 static void
10371f_screenchar(typval_T *argvars, typval_T *rettv)
10372{
10373 int row;
10374 int col;
10375 int off;
10376 int c;
10377
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010378 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10379 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010380 if (row < 0 || row >= screen_Rows
10381 || col < 0 || col >= screen_Columns)
10382 c = -1;
10383 else
10384 {
10385 off = LineOffset[row] + col;
10386#ifdef FEAT_MBYTE
10387 if (enc_utf8 && ScreenLinesUC[off] != 0)
10388 c = ScreenLinesUC[off];
10389 else
10390#endif
10391 c = ScreenLines[off];
10392 }
10393 rettv->vval.v_number = c;
10394}
10395
10396/*
10397 * "screencol()" function
10398 *
10399 * First column is 1 to be consistent with virtcol().
10400 */
10401 static void
10402f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10403{
10404 rettv->vval.v_number = screen_screencol() + 1;
10405}
10406
10407/*
10408 * "screenrow()" function
10409 */
10410 static void
10411f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10412{
10413 rettv->vval.v_number = screen_screenrow() + 1;
10414}
10415
10416/*
10417 * "search()" function
10418 */
10419 static void
10420f_search(typval_T *argvars, typval_T *rettv)
10421{
10422 int flags = 0;
10423
10424 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10425}
10426
10427/*
10428 * "searchdecl()" function
10429 */
10430 static void
10431f_searchdecl(typval_T *argvars, typval_T *rettv)
10432{
10433 int locally = 1;
10434 int thisblock = 0;
10435 int error = FALSE;
10436 char_u *name;
10437
10438 rettv->vval.v_number = 1; /* default: FAIL */
10439
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010440 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010441 if (argvars[1].v_type != VAR_UNKNOWN)
10442 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010443 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010444 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010445 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010446 }
10447 if (!error && name != NULL)
10448 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10449 locally, thisblock, SEARCH_KEEP) == FAIL;
10450}
10451
10452/*
10453 * Used by searchpair() and searchpairpos()
10454 */
10455 static int
10456searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10457{
10458 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010459 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010460 int save_p_ws = p_ws;
10461 int dir;
10462 int flags = 0;
10463 char_u nbuf1[NUMBUFLEN];
10464 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010465 int retval = 0; /* default: FAIL */
10466 long lnum_stop = 0;
10467 long time_limit = 0;
10468
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010469 /* Get the three pattern arguments: start, middle, end. Will result in an
10470 * error if not a valid argument. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010471 spat = tv_get_string_chk(&argvars[0]);
10472 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
10473 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010474 if (spat == NULL || mpat == NULL || epat == NULL)
10475 goto theend; /* type error */
10476
10477 /* Handle the optional fourth argument: flags */
10478 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
10479 if (dir == 0)
10480 goto theend;
10481
10482 /* Don't accept SP_END or SP_SUBPAT.
10483 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
10484 */
10485 if ((flags & (SP_END | SP_SUBPAT)) != 0
10486 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10487 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010488 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010489 goto theend;
10490 }
10491
10492 /* Using 'r' implies 'W', otherwise it doesn't work. */
10493 if (flags & SP_REPEAT)
10494 p_ws = FALSE;
10495
10496 /* Optional fifth argument: skip expression */
10497 if (argvars[3].v_type == VAR_UNKNOWN
10498 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010010499 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010500 else
10501 {
Bram Moolenaar48570482017-10-30 21:48:41 +010010502 skip = &argvars[4];
10503 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
10504 && skip->v_type != VAR_STRING)
10505 {
10506 /* Type error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010507 semsg(_(e_invarg2), tv_get_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010010508 goto theend;
10509 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010510 if (argvars[5].v_type != VAR_UNKNOWN)
10511 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010512 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010513 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010514 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010515 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
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#ifdef FEAT_RELTIME
10519 if (argvars[6].v_type != VAR_UNKNOWN)
10520 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010521 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010522 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010523 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010524 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010525 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010526 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010527 }
10528#endif
10529 }
10530 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010531
10532 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
10533 match_pos, lnum_stop, time_limit);
10534
10535theend:
10536 p_ws = save_p_ws;
10537
10538 return retval;
10539}
10540
10541/*
10542 * "searchpair()" function
10543 */
10544 static void
10545f_searchpair(typval_T *argvars, typval_T *rettv)
10546{
10547 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
10548}
10549
10550/*
10551 * "searchpairpos()" function
10552 */
10553 static void
10554f_searchpairpos(typval_T *argvars, typval_T *rettv)
10555{
10556 pos_T match_pos;
10557 int lnum = 0;
10558 int col = 0;
10559
10560 if (rettv_list_alloc(rettv) == FAIL)
10561 return;
10562
10563 if (searchpair_cmn(argvars, &match_pos) > 0)
10564 {
10565 lnum = match_pos.lnum;
10566 col = match_pos.col;
10567 }
10568
10569 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10570 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10571}
10572
10573/*
10574 * Search for a start/middle/end thing.
10575 * Used by searchpair(), see its documentation for the details.
10576 * Returns 0 or -1 for no match,
10577 */
10578 long
10579do_searchpair(
10580 char_u *spat, /* start pattern */
10581 char_u *mpat, /* middle pattern */
10582 char_u *epat, /* end pattern */
10583 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010010584 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010585 int flags, /* SP_SETPCMARK and other SP_ values */
10586 pos_T *match_pos,
10587 linenr_T lnum_stop, /* stop at this line if not zero */
10588 long time_limit UNUSED) /* stop after this many msec */
10589{
10590 char_u *save_cpo;
10591 char_u *pat, *pat2 = NULL, *pat3 = NULL;
10592 long retval = 0;
10593 pos_T pos;
10594 pos_T firstpos;
10595 pos_T foundpos;
10596 pos_T save_cursor;
10597 pos_T save_pos;
10598 int n;
10599 int r;
10600 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010010601 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010602 int err;
10603 int options = SEARCH_KEEP;
10604 proftime_T tm;
10605
10606 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10607 save_cpo = p_cpo;
10608 p_cpo = empty_option;
10609
10610#ifdef FEAT_RELTIME
10611 /* Set the time limit, if there is one. */
10612 profile_setlimit(time_limit, &tm);
10613#endif
10614
10615 /* Make two search patterns: start/end (pat2, for in nested pairs) and
10616 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010617 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
10618 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010619 if (pat2 == NULL || pat3 == NULL)
10620 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010621 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010622 if (*mpat == NUL)
10623 STRCPY(pat3, pat2);
10624 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010625 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010626 spat, epat, mpat);
10627 if (flags & SP_START)
10628 options |= SEARCH_START;
10629
Bram Moolenaar48570482017-10-30 21:48:41 +010010630 if (skip != NULL)
10631 {
10632 /* Empty string means to not use the skip expression. */
10633 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
10634 use_skip = skip->vval.v_string != NULL
10635 && *skip->vval.v_string != NUL;
10636 }
10637
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010638 save_cursor = curwin->w_cursor;
10639 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010640 CLEAR_POS(&firstpos);
10641 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010642 pat = pat3;
10643 for (;;)
10644 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010645 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010646 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010647 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010648 /* didn't find it or found the first match again: FAIL */
10649 break;
10650
10651 if (firstpos.lnum == 0)
10652 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010653 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010654 {
10655 /* Found the same position again. Can happen with a pattern that
10656 * has "\zs" at the end and searching backwards. Advance one
10657 * character and try again. */
10658 if (dir == BACKWARD)
10659 decl(&pos);
10660 else
10661 incl(&pos);
10662 }
10663 foundpos = pos;
10664
10665 /* clear the start flag to avoid getting stuck here */
10666 options &= ~SEARCH_START;
10667
10668 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010010669 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010670 {
10671 save_pos = curwin->w_cursor;
10672 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010010673 err = FALSE;
10674 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010675 curwin->w_cursor = save_pos;
10676 if (err)
10677 {
10678 /* Evaluating {skip} caused an error, break here. */
10679 curwin->w_cursor = save_cursor;
10680 retval = -1;
10681 break;
10682 }
10683 if (r)
10684 continue;
10685 }
10686
10687 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
10688 {
10689 /* Found end when searching backwards or start when searching
10690 * forward: nested pair. */
10691 ++nest;
10692 pat = pat2; /* nested, don't search for middle */
10693 }
10694 else
10695 {
10696 /* Found end when searching forward or start when searching
10697 * backward: end of (nested) pair; or found middle in outer pair. */
10698 if (--nest == 1)
10699 pat = pat3; /* outer level, search for middle */
10700 }
10701
10702 if (nest == 0)
10703 {
10704 /* Found the match: return matchcount or line number. */
10705 if (flags & SP_RETCOUNT)
10706 ++retval;
10707 else
10708 retval = pos.lnum;
10709 if (flags & SP_SETPCMARK)
10710 setpcmark();
10711 curwin->w_cursor = pos;
10712 if (!(flags & SP_REPEAT))
10713 break;
10714 nest = 1; /* search for next unmatched */
10715 }
10716 }
10717
10718 if (match_pos != NULL)
10719 {
10720 /* Store the match cursor position */
10721 match_pos->lnum = curwin->w_cursor.lnum;
10722 match_pos->col = curwin->w_cursor.col + 1;
10723 }
10724
10725 /* If 'n' flag is used or search failed: restore cursor position. */
10726 if ((flags & SP_NOMOVE) || retval == 0)
10727 curwin->w_cursor = save_cursor;
10728
10729theend:
10730 vim_free(pat2);
10731 vim_free(pat3);
10732 if (p_cpo == empty_option)
10733 p_cpo = save_cpo;
10734 else
10735 /* Darn, evaluating the {skip} expression changed the value. */
10736 free_string_option(save_cpo);
10737
10738 return retval;
10739}
10740
10741/*
10742 * "searchpos()" function
10743 */
10744 static void
10745f_searchpos(typval_T *argvars, typval_T *rettv)
10746{
10747 pos_T match_pos;
10748 int lnum = 0;
10749 int col = 0;
10750 int n;
10751 int flags = 0;
10752
10753 if (rettv_list_alloc(rettv) == FAIL)
10754 return;
10755
10756 n = search_cmn(argvars, &match_pos, &flags);
10757 if (n > 0)
10758 {
10759 lnum = match_pos.lnum;
10760 col = match_pos.col;
10761 }
10762
10763 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10764 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10765 if (flags & SP_SUBPAT)
10766 list_append_number(rettv->vval.v_list, (varnumber_T)n);
10767}
10768
10769 static void
10770f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
10771{
10772#ifdef FEAT_CLIENTSERVER
10773 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010774 char_u *server = tv_get_string_chk(&argvars[0]);
10775 char_u *reply = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010776
10777 rettv->vval.v_number = -1;
10778 if (server == NULL || reply == NULL)
10779 return;
10780 if (check_restricted() || check_secure())
10781 return;
10782# ifdef FEAT_X11
10783 if (check_connection() == FAIL)
10784 return;
10785# endif
10786
10787 if (serverSendReply(server, reply) < 0)
10788 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010789 emsg(_("E258: Unable to send to client"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010790 return;
10791 }
10792 rettv->vval.v_number = 0;
10793#else
10794 rettv->vval.v_number = -1;
10795#endif
10796}
10797
10798 static void
10799f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
10800{
10801 char_u *r = NULL;
10802
10803#ifdef FEAT_CLIENTSERVER
10804# ifdef WIN32
10805 r = serverGetVimNames();
10806# else
10807 make_connection();
10808 if (X_DISPLAY != NULL)
10809 r = serverGetVimNames(X_DISPLAY);
10810# endif
10811#endif
10812 rettv->v_type = VAR_STRING;
10813 rettv->vval.v_string = r;
10814}
10815
10816/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010817 * "setbufline()" function
10818 */
10819 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020010820f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010821{
10822 linenr_T lnum;
10823 buf_T *buf;
10824
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010010825 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010826 if (buf == NULL)
10827 rettv->vval.v_number = 1; /* FAIL */
10828 else
10829 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010830 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020010831 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010832 }
10833}
10834
10835/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010836 * "setbufvar()" function
10837 */
10838 static void
10839f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10840{
10841 buf_T *buf;
10842 char_u *varname, *bufvarname;
10843 typval_T *varp;
10844 char_u nbuf[NUMBUFLEN];
10845
10846 if (check_restricted() || check_secure())
10847 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010848 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
10849 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010010850 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010851 varp = &argvars[2];
10852
10853 if (buf != NULL && varname != NULL && varp != NULL)
10854 {
10855 if (*varname == '&')
10856 {
10857 long numval;
10858 char_u *strval;
10859 int error = FALSE;
10860 aco_save_T aco;
10861
10862 /* set curbuf to be our buf, temporarily */
10863 aucmd_prepbuf(&aco, buf);
10864
10865 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010866 numval = (long)tv_get_number_chk(varp, &error);
10867 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010868 if (!error && strval != NULL)
10869 set_option_value(varname, numval, strval, OPT_LOCAL);
10870
10871 /* reset notion of buffer */
10872 aucmd_restbuf(&aco);
10873 }
10874 else
10875 {
10876 buf_T *save_curbuf = curbuf;
10877
10878 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
10879 if (bufvarname != NULL)
10880 {
10881 curbuf = buf;
10882 STRCPY(bufvarname, "b:");
10883 STRCPY(bufvarname + 2, varname);
10884 set_var(bufvarname, varp, TRUE);
10885 vim_free(bufvarname);
10886 curbuf = save_curbuf;
10887 }
10888 }
10889 }
10890}
10891
10892 static void
10893f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10894{
10895 dict_T *d;
10896 dictitem_T *di;
10897 char_u *csearch;
10898
10899 if (argvars[0].v_type != VAR_DICT)
10900 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010901 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010902 return;
10903 }
10904
10905 if ((d = argvars[0].vval.v_dict) != NULL)
10906 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010010907 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010908 if (csearch != NULL)
10909 {
10910#ifdef FEAT_MBYTE
10911 if (enc_utf8)
10912 {
10913 int pcc[MAX_MCO];
10914 int c = utfc_ptr2char(csearch, pcc);
10915
10916 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
10917 }
10918 else
10919#endif
10920 set_last_csearch(PTR2CHAR(csearch),
10921 csearch, MB_PTR2LEN(csearch));
10922 }
10923
10924 di = dict_find(d, (char_u *)"forward", -1);
10925 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010926 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010927 ? FORWARD : BACKWARD);
10928
10929 di = dict_find(d, (char_u *)"until", -1);
10930 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010931 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010932 }
10933}
10934
10935/*
10936 * "setcmdpos()" function
10937 */
10938 static void
10939f_setcmdpos(typval_T *argvars, typval_T *rettv)
10940{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010941 int pos = (int)tv_get_number(&argvars[0]) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010942
10943 if (pos >= 0)
10944 rettv->vval.v_number = set_cmdline_pos(pos);
10945}
10946
10947/*
10948 * "setfperm({fname}, {mode})" function
10949 */
10950 static void
10951f_setfperm(typval_T *argvars, typval_T *rettv)
10952{
10953 char_u *fname;
10954 char_u modebuf[NUMBUFLEN];
10955 char_u *mode_str;
10956 int i;
10957 int mask;
10958 int mode = 0;
10959
10960 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010961 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010962 if (fname == NULL)
10963 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010964 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010965 if (mode_str == NULL)
10966 return;
10967 if (STRLEN(mode_str) != 9)
10968 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010969 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010970 return;
10971 }
10972
10973 mask = 1;
10974 for (i = 8; i >= 0; --i)
10975 {
10976 if (mode_str[i] != '-')
10977 mode |= mask;
10978 mask = mask << 1;
10979 }
10980 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
10981}
10982
10983/*
10984 * "setline()" function
10985 */
10986 static void
10987f_setline(typval_T *argvars, typval_T *rettv)
10988{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010989 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010990
Bram Moolenaarca851592018-06-06 21:04:07 +020010991 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010992}
10993
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010994/*
10995 * Used by "setqflist()" and "setloclist()" functions
10996 */
10997 static void
10998set_qf_ll_list(
10999 win_T *wp UNUSED,
11000 typval_T *list_arg UNUSED,
11001 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020011002 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011003 typval_T *rettv)
11004{
11005#ifdef FEAT_QUICKFIX
11006 static char *e_invact = N_("E927: Invalid action: '%s'");
11007 char_u *act;
11008 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011009 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011010#endif
11011
11012 rettv->vval.v_number = -1;
11013
11014#ifdef FEAT_QUICKFIX
11015 if (list_arg->v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011016 emsg(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011017 else if (recursive != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011018 emsg(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011019 else
11020 {
11021 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011022 dict_T *d = NULL;
11023 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011024
11025 if (action_arg->v_type == VAR_STRING)
11026 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011027 act = tv_get_string_chk(action_arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011028 if (act == NULL)
11029 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020011030 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
11031 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011032 action = *act;
11033 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011034 semsg(_(e_invact), act);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011035 }
11036 else if (action_arg->v_type == VAR_UNKNOWN)
11037 action = ' ';
11038 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011039 emsg(_(e_stringreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011040
Bram Moolenaard823fa92016-08-12 16:29:27 +020011041 if (action_arg->v_type != VAR_UNKNOWN
11042 && what_arg->v_type != VAR_UNKNOWN)
11043 {
11044 if (what_arg->v_type == VAR_DICT)
11045 d = what_arg->vval.v_dict;
11046 else
11047 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011048 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +020011049 valid_dict = FALSE;
11050 }
11051 }
11052
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011053 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011054 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011055 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
11056 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011057 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011058 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011059 }
11060#endif
11061}
11062
11063/*
11064 * "setloclist()" function
11065 */
11066 static void
11067f_setloclist(typval_T *argvars, typval_T *rettv)
11068{
11069 win_T *win;
11070
11071 rettv->vval.v_number = -1;
11072
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020011073 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011074 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020011075 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011076}
11077
11078/*
11079 * "setmatches()" function
11080 */
11081 static void
11082f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
11083{
11084#ifdef FEAT_SEARCH_EXTRA
11085 list_T *l;
11086 listitem_T *li;
11087 dict_T *d;
11088 list_T *s = NULL;
11089
11090 rettv->vval.v_number = -1;
11091 if (argvars[0].v_type != VAR_LIST)
11092 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011093 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011094 return;
11095 }
11096 if ((l = argvars[0].vval.v_list) != NULL)
11097 {
11098
11099 /* To some extent make sure that we are dealing with a list from
11100 * "getmatches()". */
11101 li = l->lv_first;
11102 while (li != NULL)
11103 {
11104 if (li->li_tv.v_type != VAR_DICT
11105 || (d = li->li_tv.vval.v_dict) == NULL)
11106 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011107 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011108 return;
11109 }
11110 if (!(dict_find(d, (char_u *)"group", -1) != NULL
11111 && (dict_find(d, (char_u *)"pattern", -1) != NULL
11112 || dict_find(d, (char_u *)"pos1", -1) != NULL)
11113 && dict_find(d, (char_u *)"priority", -1) != NULL
11114 && dict_find(d, (char_u *)"id", -1) != NULL))
11115 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011116 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011117 return;
11118 }
11119 li = li->li_next;
11120 }
11121
11122 clear_matches(curwin);
11123 li = l->lv_first;
11124 while (li != NULL)
11125 {
11126 int i = 0;
11127 char_u buf[5];
11128 dictitem_T *di;
11129 char_u *group;
11130 int priority;
11131 int id;
11132 char_u *conceal;
11133
11134 d = li->li_tv.vval.v_dict;
11135 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
11136 {
11137 if (s == NULL)
11138 {
11139 s = list_alloc();
11140 if (s == NULL)
11141 return;
11142 }
11143
11144 /* match from matchaddpos() */
11145 for (i = 1; i < 9; i++)
11146 {
11147 sprintf((char *)buf, (char *)"pos%d", i);
11148 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
11149 {
11150 if (di->di_tv.v_type != VAR_LIST)
11151 return;
11152
11153 list_append_tv(s, &di->di_tv);
11154 s->lv_refcount++;
11155 }
11156 else
11157 break;
11158 }
11159 }
11160
Bram Moolenaar8f667172018-12-14 15:38:31 +010011161 group = dict_get_string(d, (char_u *)"group", TRUE);
11162 priority = (int)dict_get_number(d, (char_u *)"priority");
11163 id = (int)dict_get_number(d, (char_u *)"id");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011164 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar8f667172018-12-14 15:38:31 +010011165 ? dict_get_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011166 : NULL;
11167 if (i == 0)
11168 {
11169 match_add(curwin, group,
Bram Moolenaar8f667172018-12-14 15:38:31 +010011170 dict_get_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011171 priority, id, NULL, conceal);
11172 }
11173 else
11174 {
11175 match_add(curwin, group, NULL, priority, id, s, conceal);
11176 list_unref(s);
11177 s = NULL;
11178 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020011179 vim_free(group);
11180 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011181
11182 li = li->li_next;
11183 }
11184 rettv->vval.v_number = 0;
11185 }
11186#endif
11187}
11188
11189/*
11190 * "setpos()" function
11191 */
11192 static void
11193f_setpos(typval_T *argvars, typval_T *rettv)
11194{
11195 pos_T pos;
11196 int fnum;
11197 char_u *name;
11198 colnr_T curswant = -1;
11199
11200 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011201 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011202 if (name != NULL)
11203 {
11204 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
11205 {
11206 if (--pos.col < 0)
11207 pos.col = 0;
11208 if (name[0] == '.' && name[1] == NUL)
11209 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011210 /* set cursor; "fnum" is ignored */
11211 curwin->w_cursor = pos;
11212 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011213 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011214 curwin->w_curswant = curswant - 1;
11215 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011216 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011217 check_cursor();
11218 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011219 }
11220 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
11221 {
11222 /* set mark */
11223 if (setmark_pos(name[1], &pos, fnum) == OK)
11224 rettv->vval.v_number = 0;
11225 }
11226 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011227 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011228 }
11229 }
11230}
11231
11232/*
11233 * "setqflist()" function
11234 */
11235 static void
11236f_setqflist(typval_T *argvars, typval_T *rettv)
11237{
Bram Moolenaard823fa92016-08-12 16:29:27 +020011238 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011239}
11240
11241/*
11242 * "setreg()" function
11243 */
11244 static void
11245f_setreg(typval_T *argvars, typval_T *rettv)
11246{
11247 int regname;
11248 char_u *strregname;
11249 char_u *stropt;
11250 char_u *strval;
11251 int append;
11252 char_u yank_type;
11253 long block_len;
11254
11255 block_len = -1;
11256 yank_type = MAUTO;
11257 append = FALSE;
11258
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011259 strregname = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011260 rettv->vval.v_number = 1; /* FAIL is default */
11261
11262 if (strregname == NULL)
11263 return; /* type error; errmsg already given */
11264 regname = *strregname;
11265 if (regname == 0 || regname == '@')
11266 regname = '"';
11267
11268 if (argvars[2].v_type != VAR_UNKNOWN)
11269 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011270 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011271 if (stropt == NULL)
11272 return; /* type error */
11273 for (; *stropt != NUL; ++stropt)
11274 switch (*stropt)
11275 {
11276 case 'a': case 'A': /* append */
11277 append = TRUE;
11278 break;
11279 case 'v': case 'c': /* character-wise selection */
11280 yank_type = MCHAR;
11281 break;
11282 case 'V': case 'l': /* line-wise selection */
11283 yank_type = MLINE;
11284 break;
11285 case 'b': case Ctrl_V: /* block-wise selection */
11286 yank_type = MBLOCK;
11287 if (VIM_ISDIGIT(stropt[1]))
11288 {
11289 ++stropt;
11290 block_len = getdigits(&stropt) - 1;
11291 --stropt;
11292 }
11293 break;
11294 }
11295 }
11296
11297 if (argvars[1].v_type == VAR_LIST)
11298 {
11299 char_u **lstval;
11300 char_u **allocval;
11301 char_u buf[NUMBUFLEN];
11302 char_u **curval;
11303 char_u **curallocval;
11304 list_T *ll = argvars[1].vval.v_list;
11305 listitem_T *li;
11306 int len;
11307
11308 /* If the list is NULL handle like an empty list. */
11309 len = ll == NULL ? 0 : ll->lv_len;
11310
11311 /* First half: use for pointers to result lines; second half: use for
11312 * pointers to allocated copies. */
11313 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
11314 if (lstval == NULL)
11315 return;
11316 curval = lstval;
11317 allocval = lstval + len + 2;
11318 curallocval = allocval;
11319
11320 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
11321 li = li->li_next)
11322 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011323 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011324 if (strval == NULL)
11325 goto free_lstval;
11326 if (strval == buf)
11327 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011328 /* Need to make a copy, next tv_get_string_buf_chk() will
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011329 * overwrite the string. */
11330 strval = vim_strsave(buf);
11331 if (strval == NULL)
11332 goto free_lstval;
11333 *curallocval++ = strval;
11334 }
11335 *curval++ = strval;
11336 }
11337 *curval++ = NULL;
11338
11339 write_reg_contents_lst(regname, lstval, -1,
11340 append, yank_type, block_len);
11341free_lstval:
11342 while (curallocval > allocval)
11343 vim_free(*--curallocval);
11344 vim_free(lstval);
11345 }
11346 else
11347 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011348 strval = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011349 if (strval == NULL)
11350 return;
11351 write_reg_contents_ex(regname, strval, -1,
11352 append, yank_type, block_len);
11353 }
11354 rettv->vval.v_number = 0;
11355}
11356
11357/*
11358 * "settabvar()" function
11359 */
11360 static void
11361f_settabvar(typval_T *argvars, typval_T *rettv)
11362{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011363 tabpage_T *save_curtab;
11364 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011365 char_u *varname, *tabvarname;
11366 typval_T *varp;
11367
11368 rettv->vval.v_number = 0;
11369
11370 if (check_restricted() || check_secure())
11371 return;
11372
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011373 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
11374 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011375 varp = &argvars[2];
11376
Bram Moolenaar4033c552017-09-16 20:54:51 +020011377 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011378 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011379 save_curtab = curtab;
11380 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011381
11382 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
11383 if (tabvarname != NULL)
11384 {
11385 STRCPY(tabvarname, "t:");
11386 STRCPY(tabvarname + 2, varname);
11387 set_var(tabvarname, varp, TRUE);
11388 vim_free(tabvarname);
11389 }
11390
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011391 /* Restore current tabpage */
11392 if (valid_tabpage(save_curtab))
11393 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011394 }
11395}
11396
11397/*
11398 * "settabwinvar()" function
11399 */
11400 static void
11401f_settabwinvar(typval_T *argvars, typval_T *rettv)
11402{
11403 setwinvar(argvars, rettv, 1);
11404}
11405
11406/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011407 * "settagstack()" function
11408 */
11409 static void
11410f_settagstack(typval_T *argvars, typval_T *rettv)
11411{
11412 static char *e_invact2 = N_("E962: Invalid action: '%s'");
11413 win_T *wp;
11414 dict_T *d;
11415 int action = 'r';
11416
11417 rettv->vval.v_number = -1;
11418
11419 // first argument: window number or id
11420 wp = find_win_by_nr_or_id(&argvars[0]);
11421 if (wp == NULL)
11422 return;
11423
11424 // second argument: dict with items to set in the tag stack
11425 if (argvars[1].v_type != VAR_DICT)
11426 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011427 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011428 return;
11429 }
11430 d = argvars[1].vval.v_dict;
11431 if (d == NULL)
11432 return;
11433
11434 // third argument: action - 'a' for append and 'r' for replace.
11435 // default is to replace the stack.
11436 if (argvars[2].v_type == VAR_UNKNOWN)
11437 action = 'r';
11438 else if (argvars[2].v_type == VAR_STRING)
11439 {
11440 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011441 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011442 if (actstr == NULL)
11443 return;
11444 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
11445 action = *actstr;
11446 else
11447 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011448 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011449 return;
11450 }
11451 }
11452 else
11453 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011454 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011455 return;
11456 }
11457
11458 if (set_tagstack(wp, d, action) == OK)
11459 rettv->vval.v_number = 0;
11460}
11461
11462/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011463 * "setwinvar()" function
11464 */
11465 static void
11466f_setwinvar(typval_T *argvars, typval_T *rettv)
11467{
11468 setwinvar(argvars, rettv, 0);
11469}
11470
11471#ifdef FEAT_CRYPT
11472/*
11473 * "sha256({string})" function
11474 */
11475 static void
11476f_sha256(typval_T *argvars, typval_T *rettv)
11477{
11478 char_u *p;
11479
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011480 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011481 rettv->vval.v_string = vim_strsave(
11482 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
11483 rettv->v_type = VAR_STRING;
11484}
11485#endif /* FEAT_CRYPT */
11486
11487/*
11488 * "shellescape({string})" function
11489 */
11490 static void
11491f_shellescape(typval_T *argvars, typval_T *rettv)
11492{
Bram Moolenaar20615522017-06-05 18:46:26 +020011493 int do_special = non_zero_arg(&argvars[1]);
11494
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011495 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011496 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011497 rettv->v_type = VAR_STRING;
11498}
11499
11500/*
11501 * shiftwidth() function
11502 */
11503 static void
11504f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
11505{
Bram Moolenaarf9514162018-11-22 03:08:29 +010011506 rettv->vval.v_number = 0;
11507
11508 if (argvars[0].v_type != VAR_UNKNOWN)
11509 {
11510 long col;
11511
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011512 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +010011513 if (col < 0)
11514 return; // type error; errmsg already given
11515#ifdef FEAT_VARTABS
11516 rettv->vval.v_number = get_sw_value_col(curbuf, col);
11517 return;
11518#endif
11519 }
11520
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011521 rettv->vval.v_number = get_sw_value(curbuf);
11522}
11523
Bram Moolenaar162b7142018-12-21 15:17:36 +010011524#ifdef FEAT_SIGNS
11525/*
11526 * "sign_define()" function
11527 */
11528 static void
11529f_sign_define(typval_T *argvars, typval_T *rettv)
11530{
11531 char_u *name;
11532 dict_T *dict;
11533 char_u *icon = NULL;
11534 char_u *linehl = NULL;
11535 char_u *text = NULL;
11536 char_u *texthl = NULL;
11537
11538 rettv->vval.v_number = -1;
11539
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011540 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011541 if (name == NULL)
11542 return;
11543
11544 if (argvars[1].v_type != VAR_UNKNOWN)
11545 {
11546 if (argvars[1].v_type != VAR_DICT)
11547 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011548 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011549 return;
11550 }
11551
11552 // sign attributes
11553 dict = argvars[1].vval.v_dict;
11554 if (dict_find(dict, (char_u *)"icon", -1) != NULL)
11555 icon = dict_get_string(dict, (char_u *)"icon", TRUE);
11556 if (dict_find(dict, (char_u *)"linehl", -1) != NULL)
11557 linehl = dict_get_string(dict, (char_u *)"linehl", TRUE);
11558 if (dict_find(dict, (char_u *)"text", -1) != NULL)
11559 text = dict_get_string(dict, (char_u *)"text", TRUE);
11560 if (dict_find(dict, (char_u *)"texthl", -1) != NULL)
11561 texthl = dict_get_string(dict, (char_u *)"texthl", TRUE);
11562 }
11563
11564 if (sign_define_by_name(name, icon, linehl, text, texthl) == OK)
11565 rettv->vval.v_number = 0;
11566
11567 vim_free(icon);
11568 vim_free(linehl);
11569 vim_free(text);
11570 vim_free(texthl);
11571}
11572
11573/*
11574 * "sign_getdefined()" function
11575 */
11576 static void
11577f_sign_getdefined(typval_T *argvars, typval_T *rettv)
11578{
11579 char_u *name = NULL;
11580
11581 if (rettv_list_alloc_id(rettv, aid_sign_getdefined) != OK)
11582 return;
11583
11584 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011585 name = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011586
11587 sign_getlist(name, rettv->vval.v_list);
11588}
11589
11590/*
11591 * "sign_getplaced()" function
11592 */
11593 static void
11594f_sign_getplaced(typval_T *argvars, typval_T *rettv)
11595{
11596 buf_T *buf = NULL;
11597 dict_T *dict;
11598 dictitem_T *di;
11599 linenr_T lnum = 0;
11600 int sign_id = 0;
11601 char_u *group = NULL;
11602 int notanum = FALSE;
11603
11604 if (rettv_list_alloc_id(rettv, aid_sign_getplaced) != OK)
11605 return;
11606
11607 if (argvars[0].v_type != VAR_UNKNOWN)
11608 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010011609 // get signs placed in the specified buffer
11610 buf = get_buf_arg(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011611 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010011612 return;
Bram Moolenaar162b7142018-12-21 15:17:36 +010011613
11614 if (argvars[1].v_type != VAR_UNKNOWN)
11615 {
11616 if (argvars[1].v_type != VAR_DICT ||
11617 ((dict = argvars[1].vval.v_dict) == NULL))
11618 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011619 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011620 return;
11621 }
11622 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
11623 {
11624 // get signs placed at this line
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011625 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011626 if (notanum)
11627 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011628 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011629 }
11630 if ((di = dict_find(dict, (char_u *)"id", -1)) != NULL)
11631 {
11632 // get sign placed with this identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011633 sign_id = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011634 if (notanum)
11635 return;
11636 }
11637 if ((di = dict_find(dict, (char_u *)"group", -1)) != NULL)
11638 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011639 group = tv_get_string_chk(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011640 if (group == NULL)
11641 return;
Bram Moolenaar6436cd82018-12-27 00:28:33 +010011642 if (*group == '\0') // empty string means global group
11643 group = NULL;
Bram Moolenaar162b7142018-12-21 15:17:36 +010011644 }
11645 }
11646 }
11647
11648 sign_get_placed(buf, lnum, sign_id, group, rettv->vval.v_list);
11649}
11650
11651/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010011652 * "sign_jump()" function
11653 */
11654 static void
11655f_sign_jump(typval_T *argvars, typval_T *rettv)
11656{
11657 int sign_id;
11658 char_u *sign_group = NULL;
11659 buf_T *buf;
11660 int notanum = FALSE;
11661
11662 rettv->vval.v_number = -1;
11663
11664 // Sign identifer
11665 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
11666 if (notanum)
11667 return;
11668 if (sign_id <= 0)
11669 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011670 emsg(_(e_invarg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010011671 return;
11672 }
11673
11674 // Sign group
11675 sign_group = tv_get_string_chk(&argvars[1]);
11676 if (sign_group == NULL)
11677 return;
11678 if (sign_group[0] == '\0')
11679 sign_group = NULL; // global sign group
11680 else
11681 {
11682 sign_group = vim_strsave(sign_group);
11683 if (sign_group == NULL)
11684 return;
11685 }
11686
11687 // Buffer to place the sign
11688 buf = get_buf_arg(&argvars[2]);
11689 if (buf == NULL)
11690 goto cleanup;
11691
11692 rettv->vval.v_number = sign_jump(sign_id, sign_group, buf);
11693
11694cleanup:
11695 vim_free(sign_group);
11696}
11697
11698/*
Bram Moolenaar162b7142018-12-21 15:17:36 +010011699 * "sign_place()" function
11700 */
11701 static void
11702f_sign_place(typval_T *argvars, typval_T *rettv)
11703{
11704 int sign_id;
11705 char_u *group = NULL;
11706 char_u *sign_name;
11707 buf_T *buf;
11708 dict_T *dict;
11709 dictitem_T *di;
11710 linenr_T lnum = 0;
11711 int prio = SIGN_DEF_PRIO;
11712 int notanum = FALSE;
11713
11714 rettv->vval.v_number = -1;
11715
11716 // Sign identifer
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011717 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011718 if (notanum)
11719 return;
11720 if (sign_id < 0)
11721 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011722 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011723 return;
11724 }
11725
11726 // Sign group
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011727 group = tv_get_string_chk(&argvars[1]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011728 if (group == NULL)
11729 return;
11730 if (group[0] == '\0')
11731 group = NULL; // global sign group
11732 else
11733 {
11734 group = vim_strsave(group);
11735 if (group == NULL)
11736 return;
11737 }
11738
11739 // Sign name
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011740 sign_name = tv_get_string_chk(&argvars[2]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011741 if (sign_name == NULL)
11742 goto cleanup;
11743
11744 // Buffer to place the sign
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010011745 buf = get_buf_arg(&argvars[3]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011746 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010011747 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010011748
11749 if (argvars[4].v_type != VAR_UNKNOWN)
11750 {
11751 if (argvars[4].v_type != VAR_DICT ||
11752 ((dict = argvars[4].vval.v_dict) == NULL))
11753 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011754 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011755 goto cleanup;
11756 }
11757
11758 // Line number where the sign is to be placed
11759 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
11760 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011761 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011762 if (notanum)
11763 goto cleanup;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011764 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011765 }
11766 if ((di = dict_find(dict, (char_u *)"priority", -1)) != NULL)
11767 {
11768 // Sign priority
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011769 prio = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011770 if (notanum)
11771 goto cleanup;
11772 }
11773 }
11774
11775 if (sign_place(&sign_id, group, sign_name, buf, lnum, prio) == OK)
11776 rettv->vval.v_number = sign_id;
11777
11778cleanup:
11779 vim_free(group);
11780}
11781
11782/*
11783 * "sign_undefine()" function
11784 */
11785 static void
11786f_sign_undefine(typval_T *argvars, typval_T *rettv)
11787{
11788 char_u *name;
11789
11790 rettv->vval.v_number = -1;
11791
11792 if (argvars[0].v_type == VAR_UNKNOWN)
11793 {
11794 // Free all the signs
11795 free_signs();
11796 rettv->vval.v_number = 0;
11797 }
11798 else
11799 {
11800 // Free only the specified sign
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011801 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011802 if (name == NULL)
11803 return;
11804
11805 if (sign_undefine_by_name(name) == OK)
11806 rettv->vval.v_number = 0;
11807 }
11808}
11809
11810/*
11811 * "sign_unplace()" function
11812 */
11813 static void
11814f_sign_unplace(typval_T *argvars, typval_T *rettv)
11815{
11816 dict_T *dict;
11817 dictitem_T *di;
11818 int sign_id = 0;
11819 buf_T *buf = NULL;
11820 char_u *group = NULL;
11821
11822 rettv->vval.v_number = -1;
11823
11824 if (argvars[0].v_type != VAR_STRING)
11825 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011826 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011827 return;
11828 }
11829
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011830 group = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011831 if (group[0] == '\0')
11832 group = NULL; // global sign group
11833 else
11834 {
11835 group = vim_strsave(group);
11836 if (group == NULL)
11837 return;
11838 }
11839
11840 if (argvars[1].v_type != VAR_UNKNOWN)
11841 {
11842 if (argvars[1].v_type != VAR_DICT)
11843 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011844 emsg(_(e_dictreq));
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010011845 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010011846 }
11847 dict = argvars[1].vval.v_dict;
11848
11849 if ((di = dict_find(dict, (char_u *)"buffer", -1)) != NULL)
11850 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010011851 buf = get_buf_arg(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011852 if (buf == NULL)
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010011853 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010011854 }
11855 if (dict_find(dict, (char_u *)"id", -1) != NULL)
11856 sign_id = dict_get_number(dict, (char_u *)"id");
11857 }
11858
11859 if (buf == NULL)
11860 {
11861 // Delete the sign in all the buffers
11862 FOR_ALL_BUFFERS(buf)
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010011863 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010011864 rettv->vval.v_number = 0;
11865 }
11866 else
11867 {
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010011868 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010011869 rettv->vval.v_number = 0;
11870 }
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010011871
11872cleanup:
Bram Moolenaar162b7142018-12-21 15:17:36 +010011873 vim_free(group);
11874}
11875#endif
11876
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011877/*
11878 * "simplify()" function
11879 */
11880 static void
11881f_simplify(typval_T *argvars, typval_T *rettv)
11882{
11883 char_u *p;
11884
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011885 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011886 rettv->vval.v_string = vim_strsave(p);
11887 simplify_filename(rettv->vval.v_string); /* simplify in place */
11888 rettv->v_type = VAR_STRING;
11889}
11890
11891#ifdef FEAT_FLOAT
11892/*
11893 * "sin()" function
11894 */
11895 static void
11896f_sin(typval_T *argvars, typval_T *rettv)
11897{
11898 float_T f = 0.0;
11899
11900 rettv->v_type = VAR_FLOAT;
11901 if (get_float_arg(argvars, &f) == OK)
11902 rettv->vval.v_float = sin(f);
11903 else
11904 rettv->vval.v_float = 0.0;
11905}
11906
11907/*
11908 * "sinh()" function
11909 */
11910 static void
11911f_sinh(typval_T *argvars, typval_T *rettv)
11912{
11913 float_T f = 0.0;
11914
11915 rettv->v_type = VAR_FLOAT;
11916 if (get_float_arg(argvars, &f) == OK)
11917 rettv->vval.v_float = sinh(f);
11918 else
11919 rettv->vval.v_float = 0.0;
11920}
11921#endif
11922
11923static int
11924#ifdef __BORLANDC__
11925 _RTLENTRYF
11926#endif
11927 item_compare(const void *s1, const void *s2);
11928static int
11929#ifdef __BORLANDC__
11930 _RTLENTRYF
11931#endif
11932 item_compare2(const void *s1, const void *s2);
11933
11934/* struct used in the array that's given to qsort() */
11935typedef struct
11936{
11937 listitem_T *item;
11938 int idx;
11939} sortItem_T;
11940
11941/* struct storing information about current sort */
11942typedef struct
11943{
11944 int item_compare_ic;
11945 int item_compare_numeric;
11946 int item_compare_numbers;
11947#ifdef FEAT_FLOAT
11948 int item_compare_float;
11949#endif
11950 char_u *item_compare_func;
11951 partial_T *item_compare_partial;
11952 dict_T *item_compare_selfdict;
11953 int item_compare_func_err;
11954 int item_compare_keep_zero;
11955} sortinfo_T;
11956static sortinfo_T *sortinfo = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011957#define ITEM_COMPARE_FAIL 999
11958
11959/*
11960 * Compare functions for f_sort() and f_uniq() below.
11961 */
11962 static int
11963#ifdef __BORLANDC__
11964_RTLENTRYF
11965#endif
11966item_compare(const void *s1, const void *s2)
11967{
11968 sortItem_T *si1, *si2;
11969 typval_T *tv1, *tv2;
11970 char_u *p1, *p2;
11971 char_u *tofree1 = NULL, *tofree2 = NULL;
11972 int res;
11973 char_u numbuf1[NUMBUFLEN];
11974 char_u numbuf2[NUMBUFLEN];
11975
11976 si1 = (sortItem_T *)s1;
11977 si2 = (sortItem_T *)s2;
11978 tv1 = &si1->item->li_tv;
11979 tv2 = &si2->item->li_tv;
11980
11981 if (sortinfo->item_compare_numbers)
11982 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011983 varnumber_T v1 = tv_get_number(tv1);
11984 varnumber_T v2 = tv_get_number(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011985
11986 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11987 }
11988
11989#ifdef FEAT_FLOAT
11990 if (sortinfo->item_compare_float)
11991 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011992 float_T v1 = tv_get_float(tv1);
11993 float_T v2 = tv_get_float(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011994
11995 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11996 }
11997#endif
11998
11999 /* tv2string() puts quotes around a string and allocates memory. Don't do
12000 * that for string variables. Use a single quote when comparing with a
12001 * non-string to do what the docs promise. */
12002 if (tv1->v_type == VAR_STRING)
12003 {
12004 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12005 p1 = (char_u *)"'";
12006 else
12007 p1 = tv1->vval.v_string;
12008 }
12009 else
12010 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
12011 if (tv2->v_type == VAR_STRING)
12012 {
12013 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12014 p2 = (char_u *)"'";
12015 else
12016 p2 = tv2->vval.v_string;
12017 }
12018 else
12019 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
12020 if (p1 == NULL)
12021 p1 = (char_u *)"";
12022 if (p2 == NULL)
12023 p2 = (char_u *)"";
12024 if (!sortinfo->item_compare_numeric)
12025 {
12026 if (sortinfo->item_compare_ic)
12027 res = STRICMP(p1, p2);
12028 else
12029 res = STRCMP(p1, p2);
12030 }
12031 else
12032 {
12033 double n1, n2;
12034 n1 = strtod((char *)p1, (char **)&p1);
12035 n2 = strtod((char *)p2, (char **)&p2);
12036 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
12037 }
12038
12039 /* When the result would be zero, compare the item indexes. Makes the
12040 * sort stable. */
12041 if (res == 0 && !sortinfo->item_compare_keep_zero)
12042 res = si1->idx > si2->idx ? 1 : -1;
12043
12044 vim_free(tofree1);
12045 vim_free(tofree2);
12046 return res;
12047}
12048
12049 static int
12050#ifdef __BORLANDC__
12051_RTLENTRYF
12052#endif
12053item_compare2(const void *s1, const void *s2)
12054{
12055 sortItem_T *si1, *si2;
12056 int res;
12057 typval_T rettv;
12058 typval_T argv[3];
12059 int dummy;
12060 char_u *func_name;
12061 partial_T *partial = sortinfo->item_compare_partial;
12062
12063 /* shortcut after failure in previous call; compare all items equal */
12064 if (sortinfo->item_compare_func_err)
12065 return 0;
12066
12067 si1 = (sortItem_T *)s1;
12068 si2 = (sortItem_T *)s2;
12069
12070 if (partial == NULL)
12071 func_name = sortinfo->item_compare_func;
12072 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012073 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012074
12075 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
12076 * in the copy without changing the original list items. */
12077 copy_tv(&si1->item->li_tv, &argv[0]);
12078 copy_tv(&si2->item->li_tv, &argv[1]);
12079
12080 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
12081 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020012082 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012083 partial, sortinfo->item_compare_selfdict);
12084 clear_tv(&argv[0]);
12085 clear_tv(&argv[1]);
12086
12087 if (res == FAIL)
12088 res = ITEM_COMPARE_FAIL;
12089 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012090 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012091 if (sortinfo->item_compare_func_err)
12092 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
12093 clear_tv(&rettv);
12094
12095 /* When the result would be zero, compare the pointers themselves. Makes
12096 * the sort stable. */
12097 if (res == 0 && !sortinfo->item_compare_keep_zero)
12098 res = si1->idx > si2->idx ? 1 : -1;
12099
12100 return res;
12101}
12102
12103/*
12104 * "sort({list})" function
12105 */
12106 static void
12107do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
12108{
12109 list_T *l;
12110 listitem_T *li;
12111 sortItem_T *ptrs;
12112 sortinfo_T *old_sortinfo;
12113 sortinfo_T info;
12114 long len;
12115 long i;
12116
12117 /* Pointer to current info struct used in compare function. Save and
12118 * restore the current one for nested calls. */
12119 old_sortinfo = sortinfo;
12120 sortinfo = &info;
12121
12122 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012123 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012124 else
12125 {
12126 l = argvars[0].vval.v_list;
12127 if (l == NULL || tv_check_lock(l->lv_lock,
12128 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
12129 TRUE))
12130 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012131 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012132
12133 len = list_len(l);
12134 if (len <= 1)
12135 goto theend; /* short list sorts pretty quickly */
12136
12137 info.item_compare_ic = FALSE;
12138 info.item_compare_numeric = FALSE;
12139 info.item_compare_numbers = FALSE;
12140#ifdef FEAT_FLOAT
12141 info.item_compare_float = FALSE;
12142#endif
12143 info.item_compare_func = NULL;
12144 info.item_compare_partial = NULL;
12145 info.item_compare_selfdict = NULL;
12146 if (argvars[1].v_type != VAR_UNKNOWN)
12147 {
12148 /* optional second argument: {func} */
12149 if (argvars[1].v_type == VAR_FUNC)
12150 info.item_compare_func = argvars[1].vval.v_string;
12151 else if (argvars[1].v_type == VAR_PARTIAL)
12152 info.item_compare_partial = argvars[1].vval.v_partial;
12153 else
12154 {
12155 int error = FALSE;
12156
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012157 i = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012158 if (error)
12159 goto theend; /* type error; errmsg already given */
12160 if (i == 1)
12161 info.item_compare_ic = TRUE;
12162 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012163 info.item_compare_func = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012164 else if (i != 0)
12165 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012166 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012167 goto theend;
12168 }
12169 if (info.item_compare_func != NULL)
12170 {
12171 if (*info.item_compare_func == NUL)
12172 {
12173 /* empty string means default sort */
12174 info.item_compare_func = NULL;
12175 }
12176 else if (STRCMP(info.item_compare_func, "n") == 0)
12177 {
12178 info.item_compare_func = NULL;
12179 info.item_compare_numeric = TRUE;
12180 }
12181 else if (STRCMP(info.item_compare_func, "N") == 0)
12182 {
12183 info.item_compare_func = NULL;
12184 info.item_compare_numbers = TRUE;
12185 }
12186#ifdef FEAT_FLOAT
12187 else if (STRCMP(info.item_compare_func, "f") == 0)
12188 {
12189 info.item_compare_func = NULL;
12190 info.item_compare_float = TRUE;
12191 }
12192#endif
12193 else if (STRCMP(info.item_compare_func, "i") == 0)
12194 {
12195 info.item_compare_func = NULL;
12196 info.item_compare_ic = TRUE;
12197 }
12198 }
12199 }
12200
12201 if (argvars[2].v_type != VAR_UNKNOWN)
12202 {
12203 /* optional third argument: {dict} */
12204 if (argvars[2].v_type != VAR_DICT)
12205 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012206 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012207 goto theend;
12208 }
12209 info.item_compare_selfdict = argvars[2].vval.v_dict;
12210 }
12211 }
12212
12213 /* Make an array with each entry pointing to an item in the List. */
12214 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
12215 if (ptrs == NULL)
12216 goto theend;
12217
12218 i = 0;
12219 if (sort)
12220 {
12221 /* sort(): ptrs will be the list to sort */
12222 for (li = l->lv_first; li != NULL; li = li->li_next)
12223 {
12224 ptrs[i].item = li;
12225 ptrs[i].idx = i;
12226 ++i;
12227 }
12228
12229 info.item_compare_func_err = FALSE;
12230 info.item_compare_keep_zero = FALSE;
12231 /* test the compare function */
12232 if ((info.item_compare_func != NULL
12233 || info.item_compare_partial != NULL)
12234 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
12235 == ITEM_COMPARE_FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012236 emsg(_("E702: Sort compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012237 else
12238 {
12239 /* Sort the array with item pointers. */
12240 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
12241 info.item_compare_func == NULL
12242 && info.item_compare_partial == NULL
12243 ? item_compare : item_compare2);
12244
12245 if (!info.item_compare_func_err)
12246 {
12247 /* Clear the List and append the items in sorted order. */
12248 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
12249 l->lv_len = 0;
12250 for (i = 0; i < len; ++i)
12251 list_append(l, ptrs[i].item);
12252 }
12253 }
12254 }
12255 else
12256 {
12257 int (*item_compare_func_ptr)(const void *, const void *);
12258
12259 /* f_uniq(): ptrs will be a stack of items to remove */
12260 info.item_compare_func_err = FALSE;
12261 info.item_compare_keep_zero = TRUE;
12262 item_compare_func_ptr = info.item_compare_func != NULL
12263 || info.item_compare_partial != NULL
12264 ? item_compare2 : item_compare;
12265
12266 for (li = l->lv_first; li != NULL && li->li_next != NULL;
12267 li = li->li_next)
12268 {
12269 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
12270 == 0)
12271 ptrs[i++].item = li;
12272 if (info.item_compare_func_err)
12273 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012274 emsg(_("E882: Uniq compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012275 break;
12276 }
12277 }
12278
12279 if (!info.item_compare_func_err)
12280 {
12281 while (--i >= 0)
12282 {
12283 li = ptrs[i].item->li_next;
12284 ptrs[i].item->li_next = li->li_next;
12285 if (li->li_next != NULL)
12286 li->li_next->li_prev = ptrs[i].item;
12287 else
12288 l->lv_last = ptrs[i].item;
12289 list_fix_watch(l, li);
12290 listitem_free(li);
12291 l->lv_len--;
12292 }
12293 }
12294 }
12295
12296 vim_free(ptrs);
12297 }
12298theend:
12299 sortinfo = old_sortinfo;
12300}
12301
12302/*
12303 * "sort({list})" function
12304 */
12305 static void
12306f_sort(typval_T *argvars, typval_T *rettv)
12307{
12308 do_sort_uniq(argvars, rettv, TRUE);
12309}
12310
12311/*
12312 * "uniq({list})" function
12313 */
12314 static void
12315f_uniq(typval_T *argvars, typval_T *rettv)
12316{
12317 do_sort_uniq(argvars, rettv, FALSE);
12318}
12319
12320/*
12321 * "soundfold({word})" function
12322 */
12323 static void
12324f_soundfold(typval_T *argvars, typval_T *rettv)
12325{
12326 char_u *s;
12327
12328 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012329 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012330#ifdef FEAT_SPELL
12331 rettv->vval.v_string = eval_soundfold(s);
12332#else
12333 rettv->vval.v_string = vim_strsave(s);
12334#endif
12335}
12336
12337/*
12338 * "spellbadword()" function
12339 */
12340 static void
12341f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
12342{
12343 char_u *word = (char_u *)"";
12344 hlf_T attr = HLF_COUNT;
12345 int len = 0;
12346
12347 if (rettv_list_alloc(rettv) == FAIL)
12348 return;
12349
12350#ifdef FEAT_SPELL
12351 if (argvars[0].v_type == VAR_UNKNOWN)
12352 {
12353 /* Find the start and length of the badly spelled word. */
12354 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
12355 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012356 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012357 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012358 curwin->w_set_curswant = TRUE;
12359 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012360 }
12361 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
12362 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012363 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012364 int capcol = -1;
12365
12366 if (str != NULL)
12367 {
12368 /* Check the argument for spelling. */
12369 while (*str != NUL)
12370 {
12371 len = spell_check(curwin, str, &attr, &capcol, FALSE);
12372 if (attr != HLF_COUNT)
12373 {
12374 word = str;
12375 break;
12376 }
12377 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020012378 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012379 }
12380 }
12381 }
12382#endif
12383
12384 list_append_string(rettv->vval.v_list, word, len);
12385 list_append_string(rettv->vval.v_list, (char_u *)(
12386 attr == HLF_SPB ? "bad" :
12387 attr == HLF_SPR ? "rare" :
12388 attr == HLF_SPL ? "local" :
12389 attr == HLF_SPC ? "caps" :
12390 ""), -1);
12391}
12392
12393/*
12394 * "spellsuggest()" function
12395 */
12396 static void
12397f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
12398{
12399#ifdef FEAT_SPELL
12400 char_u *str;
12401 int typeerr = FALSE;
12402 int maxcount;
12403 garray_T ga;
12404 int i;
12405 listitem_T *li;
12406 int need_capital = FALSE;
12407#endif
12408
12409 if (rettv_list_alloc(rettv) == FAIL)
12410 return;
12411
12412#ifdef FEAT_SPELL
12413 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
12414 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012415 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012416 if (argvars[1].v_type != VAR_UNKNOWN)
12417 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012418 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012419 if (maxcount <= 0)
12420 return;
12421 if (argvars[2].v_type != VAR_UNKNOWN)
12422 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012423 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012424 if (typeerr)
12425 return;
12426 }
12427 }
12428 else
12429 maxcount = 25;
12430
12431 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
12432
12433 for (i = 0; i < ga.ga_len; ++i)
12434 {
12435 str = ((char_u **)ga.ga_data)[i];
12436
12437 li = listitem_alloc();
12438 if (li == NULL)
12439 vim_free(str);
12440 else
12441 {
12442 li->li_tv.v_type = VAR_STRING;
12443 li->li_tv.v_lock = 0;
12444 li->li_tv.vval.v_string = str;
12445 list_append(rettv->vval.v_list, li);
12446 }
12447 }
12448 ga_clear(&ga);
12449 }
12450#endif
12451}
12452
12453 static void
12454f_split(typval_T *argvars, typval_T *rettv)
12455{
12456 char_u *str;
12457 char_u *end;
12458 char_u *pat = NULL;
12459 regmatch_T regmatch;
12460 char_u patbuf[NUMBUFLEN];
12461 char_u *save_cpo;
12462 int match;
12463 colnr_T col = 0;
12464 int keepempty = FALSE;
12465 int typeerr = FALSE;
12466
12467 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12468 save_cpo = p_cpo;
12469 p_cpo = (char_u *)"";
12470
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012471 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012472 if (argvars[1].v_type != VAR_UNKNOWN)
12473 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012474 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012475 if (pat == NULL)
12476 typeerr = TRUE;
12477 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012478 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012479 }
12480 if (pat == NULL || *pat == NUL)
12481 pat = (char_u *)"[\\x01- ]\\+";
12482
12483 if (rettv_list_alloc(rettv) == FAIL)
12484 return;
12485 if (typeerr)
12486 return;
12487
12488 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12489 if (regmatch.regprog != NULL)
12490 {
12491 regmatch.rm_ic = FALSE;
12492 while (*str != NUL || keepempty)
12493 {
12494 if (*str == NUL)
12495 match = FALSE; /* empty item at the end */
12496 else
12497 match = vim_regexec_nl(&regmatch, str, col);
12498 if (match)
12499 end = regmatch.startp[0];
12500 else
12501 end = str + STRLEN(str);
12502 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
12503 && *str != NUL && match && end < regmatch.endp[0]))
12504 {
12505 if (list_append_string(rettv->vval.v_list, str,
12506 (int)(end - str)) == FAIL)
12507 break;
12508 }
12509 if (!match)
12510 break;
12511 /* Advance to just after the match. */
12512 if (regmatch.endp[0] > str)
12513 col = 0;
12514 else
12515 {
12516 /* Don't get stuck at the same match. */
12517#ifdef FEAT_MBYTE
12518 col = (*mb_ptr2len)(regmatch.endp[0]);
12519#else
12520 col = 1;
12521#endif
12522 }
12523 str = regmatch.endp[0];
12524 }
12525
12526 vim_regfree(regmatch.regprog);
12527 }
12528
12529 p_cpo = save_cpo;
12530}
12531
12532#ifdef FEAT_FLOAT
12533/*
12534 * "sqrt()" function
12535 */
12536 static void
12537f_sqrt(typval_T *argvars, typval_T *rettv)
12538{
12539 float_T f = 0.0;
12540
12541 rettv->v_type = VAR_FLOAT;
12542 if (get_float_arg(argvars, &f) == OK)
12543 rettv->vval.v_float = sqrt(f);
12544 else
12545 rettv->vval.v_float = 0.0;
12546}
12547
12548/*
12549 * "str2float()" function
12550 */
12551 static void
12552f_str2float(typval_T *argvars, typval_T *rettv)
12553{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012554 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010012555 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012556
Bram Moolenaar08243d22017-01-10 16:12:29 +010012557 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012558 p = skipwhite(p + 1);
12559 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010012560 if (isneg)
12561 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012562 rettv->v_type = VAR_FLOAT;
12563}
12564#endif
12565
12566/*
12567 * "str2nr()" function
12568 */
12569 static void
12570f_str2nr(typval_T *argvars, typval_T *rettv)
12571{
12572 int base = 10;
12573 char_u *p;
12574 varnumber_T n;
12575 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010012576 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012577
12578 if (argvars[1].v_type != VAR_UNKNOWN)
12579 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012580 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012581 if (base != 2 && base != 8 && base != 10 && base != 16)
12582 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012583 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012584 return;
12585 }
12586 }
12587
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012588 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010012589 isneg = (*p == '-');
12590 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012591 p = skipwhite(p + 1);
12592 switch (base)
12593 {
12594 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
12595 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
12596 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
12597 default: what = 0;
12598 }
12599 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010012600 if (isneg)
12601 rettv->vval.v_number = -n;
12602 else
12603 rettv->vval.v_number = n;
12604
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012605}
12606
12607#ifdef HAVE_STRFTIME
12608/*
12609 * "strftime({format}[, {time}])" function
12610 */
12611 static void
12612f_strftime(typval_T *argvars, typval_T *rettv)
12613{
12614 char_u result_buf[256];
12615 struct tm *curtime;
12616 time_t seconds;
12617 char_u *p;
12618
12619 rettv->v_type = VAR_STRING;
12620
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012621 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012622 if (argvars[1].v_type == VAR_UNKNOWN)
12623 seconds = time(NULL);
12624 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012625 seconds = (time_t)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012626 curtime = localtime(&seconds);
12627 /* MSVC returns NULL for an invalid value of seconds. */
12628 if (curtime == NULL)
12629 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
12630 else
12631 {
12632# ifdef FEAT_MBYTE
12633 vimconv_T conv;
12634 char_u *enc;
12635
12636 conv.vc_type = CONV_NONE;
12637 enc = enc_locale();
12638 convert_setup(&conv, p_enc, enc);
12639 if (conv.vc_type != CONV_NONE)
12640 p = string_convert(&conv, p, NULL);
12641# endif
12642 if (p != NULL)
12643 (void)strftime((char *)result_buf, sizeof(result_buf),
12644 (char *)p, curtime);
12645 else
12646 result_buf[0] = NUL;
12647
12648# ifdef FEAT_MBYTE
12649 if (conv.vc_type != CONV_NONE)
12650 vim_free(p);
12651 convert_setup(&conv, enc, p_enc);
12652 if (conv.vc_type != CONV_NONE)
12653 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
12654 else
12655# endif
12656 rettv->vval.v_string = vim_strsave(result_buf);
12657
12658# ifdef FEAT_MBYTE
12659 /* Release conversion descriptors */
12660 convert_setup(&conv, NULL, NULL);
12661 vim_free(enc);
12662# endif
12663 }
12664}
12665#endif
12666
12667/*
12668 * "strgetchar()" function
12669 */
12670 static void
12671f_strgetchar(typval_T *argvars, typval_T *rettv)
12672{
12673 char_u *str;
12674 int len;
12675 int error = FALSE;
12676 int charidx;
12677
12678 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012679 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012680 if (str == NULL)
12681 return;
12682 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012683 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012684 if (error)
12685 return;
12686#ifdef FEAT_MBYTE
12687 {
12688 int byteidx = 0;
12689
12690 while (charidx >= 0 && byteidx < len)
12691 {
12692 if (charidx == 0)
12693 {
12694 rettv->vval.v_number = mb_ptr2char(str + byteidx);
12695 break;
12696 }
12697 --charidx;
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012698 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012699 }
12700 }
12701#else
12702 if (charidx < len)
12703 rettv->vval.v_number = str[charidx];
12704#endif
12705}
12706
12707/*
12708 * "stridx()" function
12709 */
12710 static void
12711f_stridx(typval_T *argvars, typval_T *rettv)
12712{
12713 char_u buf[NUMBUFLEN];
12714 char_u *needle;
12715 char_u *haystack;
12716 char_u *save_haystack;
12717 char_u *pos;
12718 int start_idx;
12719
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012720 needle = tv_get_string_chk(&argvars[1]);
12721 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012722 rettv->vval.v_number = -1;
12723 if (needle == NULL || haystack == NULL)
12724 return; /* type error; errmsg already given */
12725
12726 if (argvars[2].v_type != VAR_UNKNOWN)
12727 {
12728 int error = FALSE;
12729
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012730 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012731 if (error || start_idx >= (int)STRLEN(haystack))
12732 return;
12733 if (start_idx >= 0)
12734 haystack += start_idx;
12735 }
12736
12737 pos = (char_u *)strstr((char *)haystack, (char *)needle);
12738 if (pos != NULL)
12739 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
12740}
12741
12742/*
12743 * "string()" function
12744 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010012745 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012746f_string(typval_T *argvars, typval_T *rettv)
12747{
12748 char_u *tofree;
12749 char_u numbuf[NUMBUFLEN];
12750
12751 rettv->v_type = VAR_STRING;
12752 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
12753 get_copyID());
12754 /* Make a copy if we have a value but it's not in allocated memory. */
12755 if (rettv->vval.v_string != NULL && tofree == NULL)
12756 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
12757}
12758
12759/*
12760 * "strlen()" function
12761 */
12762 static void
12763f_strlen(typval_T *argvars, typval_T *rettv)
12764{
12765 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012766 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012767}
12768
12769/*
12770 * "strchars()" function
12771 */
12772 static void
12773f_strchars(typval_T *argvars, typval_T *rettv)
12774{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012775 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012776 int skipcc = 0;
12777#ifdef FEAT_MBYTE
12778 varnumber_T len = 0;
12779 int (*func_mb_ptr2char_adv)(char_u **pp);
12780#endif
12781
12782 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012783 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012784 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012785 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012786 else
12787 {
12788#ifdef FEAT_MBYTE
12789 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
12790 while (*s != NUL)
12791 {
12792 func_mb_ptr2char_adv(&s);
12793 ++len;
12794 }
12795 rettv->vval.v_number = len;
12796#else
12797 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
12798#endif
12799 }
12800}
12801
12802/*
12803 * "strdisplaywidth()" function
12804 */
12805 static void
12806f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
12807{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012808 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012809 int col = 0;
12810
12811 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012812 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012813
12814 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
12815}
12816
12817/*
12818 * "strwidth()" function
12819 */
12820 static void
12821f_strwidth(typval_T *argvars, typval_T *rettv)
12822{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012823 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012824
12825 rettv->vval.v_number = (varnumber_T)(
12826#ifdef FEAT_MBYTE
12827 mb_string2cells(s, -1)
12828#else
12829 STRLEN(s)
12830#endif
12831 );
12832}
12833
12834/*
12835 * "strcharpart()" function
12836 */
12837 static void
12838f_strcharpart(typval_T *argvars, typval_T *rettv)
12839{
12840#ifdef FEAT_MBYTE
12841 char_u *p;
12842 int nchar;
12843 int nbyte = 0;
12844 int charlen;
12845 int len = 0;
12846 int slen;
12847 int error = FALSE;
12848
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012849 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012850 slen = (int)STRLEN(p);
12851
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012852 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012853 if (!error)
12854 {
12855 if (nchar > 0)
12856 while (nchar > 0 && nbyte < slen)
12857 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012858 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012859 --nchar;
12860 }
12861 else
12862 nbyte = nchar;
12863 if (argvars[2].v_type != VAR_UNKNOWN)
12864 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012865 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012866 while (charlen > 0 && nbyte + len < slen)
12867 {
12868 int off = nbyte + len;
12869
12870 if (off < 0)
12871 len += 1;
12872 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012873 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012874 --charlen;
12875 }
12876 }
12877 else
12878 len = slen - nbyte; /* default: all bytes that are available. */
12879 }
12880
12881 /*
12882 * Only return the overlap between the specified part and the actual
12883 * string.
12884 */
12885 if (nbyte < 0)
12886 {
12887 len += nbyte;
12888 nbyte = 0;
12889 }
12890 else if (nbyte > slen)
12891 nbyte = slen;
12892 if (len < 0)
12893 len = 0;
12894 else if (nbyte + len > slen)
12895 len = slen - nbyte;
12896
12897 rettv->v_type = VAR_STRING;
12898 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
12899#else
12900 f_strpart(argvars, rettv);
12901#endif
12902}
12903
12904/*
12905 * "strpart()" function
12906 */
12907 static void
12908f_strpart(typval_T *argvars, typval_T *rettv)
12909{
12910 char_u *p;
12911 int n;
12912 int len;
12913 int slen;
12914 int error = FALSE;
12915
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012916 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012917 slen = (int)STRLEN(p);
12918
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012919 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012920 if (error)
12921 len = 0;
12922 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012923 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012924 else
12925 len = slen - n; /* default len: all bytes that are available. */
12926
12927 /*
12928 * Only return the overlap between the specified part and the actual
12929 * string.
12930 */
12931 if (n < 0)
12932 {
12933 len += n;
12934 n = 0;
12935 }
12936 else if (n > slen)
12937 n = slen;
12938 if (len < 0)
12939 len = 0;
12940 else if (n + len > slen)
12941 len = slen - n;
12942
12943 rettv->v_type = VAR_STRING;
12944 rettv->vval.v_string = vim_strnsave(p + n, len);
12945}
12946
12947/*
12948 * "strridx()" function
12949 */
12950 static void
12951f_strridx(typval_T *argvars, typval_T *rettv)
12952{
12953 char_u buf[NUMBUFLEN];
12954 char_u *needle;
12955 char_u *haystack;
12956 char_u *rest;
12957 char_u *lastmatch = NULL;
12958 int haystack_len, end_idx;
12959
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012960 needle = tv_get_string_chk(&argvars[1]);
12961 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012962
12963 rettv->vval.v_number = -1;
12964 if (needle == NULL || haystack == NULL)
12965 return; /* type error; errmsg already given */
12966
12967 haystack_len = (int)STRLEN(haystack);
12968 if (argvars[2].v_type != VAR_UNKNOWN)
12969 {
12970 /* Third argument: upper limit for index */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012971 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012972 if (end_idx < 0)
12973 return; /* can never find a match */
12974 }
12975 else
12976 end_idx = haystack_len;
12977
12978 if (*needle == NUL)
12979 {
12980 /* Empty string matches past the end. */
12981 lastmatch = haystack + end_idx;
12982 }
12983 else
12984 {
12985 for (rest = haystack; *rest != '\0'; ++rest)
12986 {
12987 rest = (char_u *)strstr((char *)rest, (char *)needle);
12988 if (rest == NULL || rest > haystack + end_idx)
12989 break;
12990 lastmatch = rest;
12991 }
12992 }
12993
12994 if (lastmatch == NULL)
12995 rettv->vval.v_number = -1;
12996 else
12997 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
12998}
12999
13000/*
13001 * "strtrans()" function
13002 */
13003 static void
13004f_strtrans(typval_T *argvars, typval_T *rettv)
13005{
13006 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013007 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013008}
13009
13010/*
13011 * "submatch()" function
13012 */
13013 static void
13014f_submatch(typval_T *argvars, typval_T *rettv)
13015{
13016 int error = FALSE;
13017 int no;
13018 int retList = 0;
13019
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013020 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013021 if (error)
13022 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013023 if (no < 0 || no >= NSUBEXP)
13024 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013025 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +010013026 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013027 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013028 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013029 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013030 if (error)
13031 return;
13032
13033 if (retList == 0)
13034 {
13035 rettv->v_type = VAR_STRING;
13036 rettv->vval.v_string = reg_submatch(no);
13037 }
13038 else
13039 {
13040 rettv->v_type = VAR_LIST;
13041 rettv->vval.v_list = reg_submatch_list(no);
13042 }
13043}
13044
13045/*
13046 * "substitute()" function
13047 */
13048 static void
13049f_substitute(typval_T *argvars, typval_T *rettv)
13050{
13051 char_u patbuf[NUMBUFLEN];
13052 char_u subbuf[NUMBUFLEN];
13053 char_u flagsbuf[NUMBUFLEN];
13054
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013055 char_u *str = tv_get_string_chk(&argvars[0]);
13056 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013057 char_u *sub = NULL;
13058 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013059 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013060
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013061 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
13062 expr = &argvars[2];
13063 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013064 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013065
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013066 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013067 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
13068 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013069 rettv->vval.v_string = NULL;
13070 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013071 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013072}
13073
13074/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013075 * "swapinfo(swap_filename)" function
13076 */
13077 static void
13078f_swapinfo(typval_T *argvars, typval_T *rettv)
13079{
13080 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013081 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013082}
13083
13084/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020013085 * "swapname(expr)" function
13086 */
13087 static void
13088f_swapname(typval_T *argvars, typval_T *rettv)
13089{
13090 buf_T *buf;
13091
13092 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010013093 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +020013094 if (buf == NULL || buf->b_ml.ml_mfp == NULL
13095 || buf->b_ml.ml_mfp->mf_fname == NULL)
13096 rettv->vval.v_string = NULL;
13097 else
13098 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
13099}
13100
13101/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013102 * "synID(lnum, col, trans)" function
13103 */
13104 static void
13105f_synID(typval_T *argvars UNUSED, typval_T *rettv)
13106{
13107 int id = 0;
13108#ifdef FEAT_SYN_HL
13109 linenr_T lnum;
13110 colnr_T col;
13111 int trans;
13112 int transerr = FALSE;
13113
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013114 lnum = tv_get_lnum(argvars); /* -1 on type error */
13115 col = (linenr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
13116 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013117
13118 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13119 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
13120 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
13121#endif
13122
13123 rettv->vval.v_number = id;
13124}
13125
13126/*
13127 * "synIDattr(id, what [, mode])" function
13128 */
13129 static void
13130f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
13131{
13132 char_u *p = NULL;
13133#ifdef FEAT_SYN_HL
13134 int id;
13135 char_u *what;
13136 char_u *mode;
13137 char_u modebuf[NUMBUFLEN];
13138 int modec;
13139
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013140 id = (int)tv_get_number(&argvars[0]);
13141 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013142 if (argvars[2].v_type != VAR_UNKNOWN)
13143 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013144 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013145 modec = TOLOWER_ASC(mode[0]);
13146 if (modec != 't' && modec != 'c' && modec != 'g')
13147 modec = 0; /* replace invalid with current */
13148 }
13149 else
13150 {
13151#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
13152 if (USE_24BIT)
13153 modec = 'g';
13154 else
13155#endif
13156 if (t_colors > 1)
13157 modec = 'c';
13158 else
13159 modec = 't';
13160 }
13161
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013162 switch (TOLOWER_ASC(what[0]))
13163 {
13164 case 'b':
13165 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
13166 p = highlight_color(id, what, modec);
13167 else /* bold */
13168 p = highlight_has_attr(id, HL_BOLD, modec);
13169 break;
13170
13171 case 'f': /* fg[#] or font */
13172 p = highlight_color(id, what, modec);
13173 break;
13174
13175 case 'i':
13176 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
13177 p = highlight_has_attr(id, HL_INVERSE, modec);
13178 else /* italic */
13179 p = highlight_has_attr(id, HL_ITALIC, modec);
13180 break;
13181
13182 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020013183 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013184 break;
13185
13186 case 'r': /* reverse */
13187 p = highlight_has_attr(id, HL_INVERSE, modec);
13188 break;
13189
13190 case 's':
13191 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
13192 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020013193 /* strikeout */
13194 else if (TOLOWER_ASC(what[1]) == 't' &&
13195 TOLOWER_ASC(what[2]) == 'r')
13196 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013197 else /* standout */
13198 p = highlight_has_attr(id, HL_STANDOUT, modec);
13199 break;
13200
13201 case 'u':
13202 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
13203 /* underline */
13204 p = highlight_has_attr(id, HL_UNDERLINE, modec);
13205 else
13206 /* undercurl */
13207 p = highlight_has_attr(id, HL_UNDERCURL, modec);
13208 break;
13209 }
13210
13211 if (p != NULL)
13212 p = vim_strsave(p);
13213#endif
13214 rettv->v_type = VAR_STRING;
13215 rettv->vval.v_string = p;
13216}
13217
13218/*
13219 * "synIDtrans(id)" function
13220 */
13221 static void
13222f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
13223{
13224 int id;
13225
13226#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013227 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013228
13229 if (id > 0)
13230 id = syn_get_final_id(id);
13231 else
13232#endif
13233 id = 0;
13234
13235 rettv->vval.v_number = id;
13236}
13237
13238/*
13239 * "synconcealed(lnum, col)" function
13240 */
13241 static void
13242f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
13243{
13244#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
13245 linenr_T lnum;
13246 colnr_T col;
13247 int syntax_flags = 0;
13248 int cchar;
13249 int matchid = 0;
13250 char_u str[NUMBUFLEN];
13251#endif
13252
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013253 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013254
13255#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013256 lnum = tv_get_lnum(argvars); /* -1 on type error */
13257 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013258
13259 vim_memset(str, NUL, sizeof(str));
13260
13261 if (rettv_list_alloc(rettv) != FAIL)
13262 {
13263 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13264 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13265 && curwin->w_p_cole > 0)
13266 {
13267 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
13268 syntax_flags = get_syntax_info(&matchid);
13269
13270 /* get the conceal character */
13271 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
13272 {
13273 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020013274 if (cchar == NUL && curwin->w_p_cole == 1)
13275 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013276 if (cchar != NUL)
13277 {
13278# ifdef FEAT_MBYTE
13279 if (has_mbyte)
13280 (*mb_char2bytes)(cchar, str);
13281 else
13282# endif
13283 str[0] = cchar;
13284 }
13285 }
13286 }
13287
13288 list_append_number(rettv->vval.v_list,
13289 (syntax_flags & HL_CONCEAL) != 0);
13290 /* -1 to auto-determine strlen */
13291 list_append_string(rettv->vval.v_list, str, -1);
13292 list_append_number(rettv->vval.v_list, matchid);
13293 }
13294#endif
13295}
13296
13297/*
13298 * "synstack(lnum, col)" function
13299 */
13300 static void
13301f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
13302{
13303#ifdef FEAT_SYN_HL
13304 linenr_T lnum;
13305 colnr_T col;
13306 int i;
13307 int id;
13308#endif
13309
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013310 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013311
13312#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013313 lnum = tv_get_lnum(argvars); /* -1 on type error */
13314 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013315
13316 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13317 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13318 && rettv_list_alloc(rettv) != FAIL)
13319 {
13320 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
13321 for (i = 0; ; ++i)
13322 {
13323 id = syn_get_stack_item(i);
13324 if (id < 0)
13325 break;
13326 if (list_append_number(rettv->vval.v_list, id) == FAIL)
13327 break;
13328 }
13329 }
13330#endif
13331}
13332
13333 static void
13334get_cmd_output_as_rettv(
13335 typval_T *argvars,
13336 typval_T *rettv,
13337 int retlist)
13338{
13339 char_u *res = NULL;
13340 char_u *p;
13341 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013342 int err = FALSE;
13343 FILE *fd;
13344 list_T *list = NULL;
13345 int flags = SHELL_SILENT;
13346
13347 rettv->v_type = VAR_STRING;
13348 rettv->vval.v_string = NULL;
13349 if (check_restricted() || check_secure())
13350 goto errret;
13351
13352 if (argvars[1].v_type != VAR_UNKNOWN)
13353 {
13354 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010013355 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013356 * command.
13357 */
13358 if ((infile = vim_tempname('i', TRUE)) == NULL)
13359 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013360 emsg(_(e_notmp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013361 goto errret;
13362 }
13363
13364 fd = mch_fopen((char *)infile, WRITEBIN);
13365 if (fd == NULL)
13366 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013367 semsg(_(e_notopen), infile);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013368 goto errret;
13369 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010013370 if (argvars[1].v_type == VAR_NUMBER)
13371 {
13372 linenr_T lnum;
13373 buf_T *buf;
13374
13375 buf = buflist_findnr(argvars[1].vval.v_number);
13376 if (buf == NULL)
13377 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013378 semsg(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010013379 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010013380 goto errret;
13381 }
13382
13383 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
13384 {
13385 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
13386 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
13387 {
13388 err = TRUE;
13389 break;
13390 }
13391 if (putc(NL, fd) == EOF)
13392 {
13393 err = TRUE;
13394 break;
13395 }
13396 }
13397 }
13398 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013399 {
13400 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
13401 err = TRUE;
13402 }
13403 else
13404 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010013405 size_t len;
13406 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013407
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013408 p = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013409 if (p == NULL)
13410 {
13411 fclose(fd);
13412 goto errret; /* type error; errmsg already given */
13413 }
13414 len = STRLEN(p);
13415 if (len > 0 && fwrite(p, len, 1, fd) != 1)
13416 err = TRUE;
13417 }
13418 if (fclose(fd) != 0)
13419 err = TRUE;
13420 if (err)
13421 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013422 emsg(_("E677: Error writing temp file"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013423 goto errret;
13424 }
13425 }
13426
13427 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
13428 * echoes typeahead, that messes up the display. */
13429 if (!msg_silent)
13430 flags += SHELL_COOKED;
13431
13432 if (retlist)
13433 {
13434 int len;
13435 listitem_T *li;
13436 char_u *s = NULL;
13437 char_u *start;
13438 char_u *end;
13439 int i;
13440
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013441 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013442 if (res == NULL)
13443 goto errret;
13444
13445 list = list_alloc();
13446 if (list == NULL)
13447 goto errret;
13448
13449 for (i = 0; i < len; ++i)
13450 {
13451 start = res + i;
13452 while (i < len && res[i] != NL)
13453 ++i;
13454 end = res + i;
13455
13456 s = alloc((unsigned)(end - start + 1));
13457 if (s == NULL)
13458 goto errret;
13459
13460 for (p = s; start < end; ++p, ++start)
13461 *p = *start == NUL ? NL : *start;
13462 *p = NUL;
13463
13464 li = listitem_alloc();
13465 if (li == NULL)
13466 {
13467 vim_free(s);
13468 goto errret;
13469 }
13470 li->li_tv.v_type = VAR_STRING;
13471 li->li_tv.v_lock = 0;
13472 li->li_tv.vval.v_string = s;
13473 list_append(list, li);
13474 }
13475
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013476 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013477 list = NULL;
13478 }
13479 else
13480 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013481 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013482#ifdef USE_CR
13483 /* translate <CR> into <NL> */
13484 if (res != NULL)
13485 {
13486 char_u *s;
13487
13488 for (s = res; *s; ++s)
13489 {
13490 if (*s == CAR)
13491 *s = NL;
13492 }
13493 }
13494#else
13495# ifdef USE_CRNL
13496 /* translate <CR><NL> into <NL> */
13497 if (res != NULL)
13498 {
13499 char_u *s, *d;
13500
13501 d = res;
13502 for (s = res; *s; ++s)
13503 {
13504 if (s[0] == CAR && s[1] == NL)
13505 ++s;
13506 *d++ = *s;
13507 }
13508 *d = NUL;
13509 }
13510# endif
13511#endif
13512 rettv->vval.v_string = res;
13513 res = NULL;
13514 }
13515
13516errret:
13517 if (infile != NULL)
13518 {
13519 mch_remove(infile);
13520 vim_free(infile);
13521 }
13522 if (res != NULL)
13523 vim_free(res);
13524 if (list != NULL)
13525 list_free(list);
13526}
13527
13528/*
13529 * "system()" function
13530 */
13531 static void
13532f_system(typval_T *argvars, typval_T *rettv)
13533{
13534 get_cmd_output_as_rettv(argvars, rettv, FALSE);
13535}
13536
13537/*
13538 * "systemlist()" function
13539 */
13540 static void
13541f_systemlist(typval_T *argvars, typval_T *rettv)
13542{
13543 get_cmd_output_as_rettv(argvars, rettv, TRUE);
13544}
13545
13546/*
13547 * "tabpagebuflist()" function
13548 */
13549 static void
13550f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13551{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013552 tabpage_T *tp;
13553 win_T *wp = NULL;
13554
13555 if (argvars[0].v_type == VAR_UNKNOWN)
13556 wp = firstwin;
13557 else
13558 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013559 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013560 if (tp != NULL)
13561 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
13562 }
13563 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
13564 {
13565 for (; wp != NULL; wp = wp->w_next)
13566 if (list_append_number(rettv->vval.v_list,
13567 wp->w_buffer->b_fnum) == FAIL)
13568 break;
13569 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013570}
13571
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013572/*
13573 * "tabpagenr()" function
13574 */
13575 static void
13576f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
13577{
13578 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013579 char_u *arg;
13580
13581 if (argvars[0].v_type != VAR_UNKNOWN)
13582 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013583 arg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013584 nr = 0;
13585 if (arg != NULL)
13586 {
13587 if (STRCMP(arg, "$") == 0)
13588 nr = tabpage_index(NULL) - 1;
13589 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013590 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013591 }
13592 }
13593 else
13594 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013595 rettv->vval.v_number = nr;
13596}
13597
13598
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013599/*
13600 * Common code for tabpagewinnr() and winnr().
13601 */
13602 static int
13603get_winnr(tabpage_T *tp, typval_T *argvar)
13604{
13605 win_T *twin;
13606 int nr = 1;
13607 win_T *wp;
13608 char_u *arg;
13609
13610 twin = (tp == curtab) ? curwin : tp->tp_curwin;
13611 if (argvar->v_type != VAR_UNKNOWN)
13612 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013613 arg = tv_get_string_chk(argvar);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013614 if (arg == NULL)
13615 nr = 0; /* type error; errmsg already given */
13616 else if (STRCMP(arg, "$") == 0)
13617 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
13618 else if (STRCMP(arg, "#") == 0)
13619 {
13620 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
13621 if (twin == NULL)
13622 nr = 0;
13623 }
13624 else
13625 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013626 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013627 nr = 0;
13628 }
13629 }
13630
13631 if (nr > 0)
13632 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
13633 wp != twin; wp = wp->w_next)
13634 {
13635 if (wp == NULL)
13636 {
13637 /* didn't find it in this tabpage */
13638 nr = 0;
13639 break;
13640 }
13641 ++nr;
13642 }
13643 return nr;
13644}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013645
13646/*
13647 * "tabpagewinnr()" function
13648 */
13649 static void
13650f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
13651{
13652 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013653 tabpage_T *tp;
13654
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013655 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013656 if (tp == NULL)
13657 nr = 0;
13658 else
13659 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013660 rettv->vval.v_number = nr;
13661}
13662
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013663/*
13664 * "tagfiles()" function
13665 */
13666 static void
13667f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
13668{
13669 char_u *fname;
13670 tagname_T tn;
13671 int first;
13672
13673 if (rettv_list_alloc(rettv) == FAIL)
13674 return;
13675 fname = alloc(MAXPATHL);
13676 if (fname == NULL)
13677 return;
13678
13679 for (first = TRUE; ; first = FALSE)
13680 if (get_tagfname(&tn, first, fname) == FAIL
13681 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
13682 break;
13683 tagname_free(&tn);
13684 vim_free(fname);
13685}
13686
13687/*
13688 * "taglist()" function
13689 */
13690 static void
13691f_taglist(typval_T *argvars, typval_T *rettv)
13692{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013693 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013694 char_u *tag_pattern;
13695
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013696 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013697
13698 rettv->vval.v_number = FALSE;
13699 if (*tag_pattern == NUL)
13700 return;
13701
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013702 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013703 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013704 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013705 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013706}
13707
13708/*
13709 * "tempname()" function
13710 */
13711 static void
13712f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
13713{
13714 static int x = 'A';
13715
13716 rettv->v_type = VAR_STRING;
13717 rettv->vval.v_string = vim_tempname(x, FALSE);
13718
13719 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
13720 * names. Skip 'I' and 'O', they are used for shell redirection. */
13721 do
13722 {
13723 if (x == 'Z')
13724 x = '0';
13725 else if (x == '9')
13726 x = 'A';
13727 else
13728 {
13729#ifdef EBCDIC
13730 if (x == 'I')
13731 x = 'J';
13732 else if (x == 'R')
13733 x = 'S';
13734 else
13735#endif
13736 ++x;
13737 }
13738 } while (x == 'I' || x == 'O');
13739}
13740
13741#ifdef FEAT_FLOAT
13742/*
13743 * "tan()" function
13744 */
13745 static void
13746f_tan(typval_T *argvars, typval_T *rettv)
13747{
13748 float_T f = 0.0;
13749
13750 rettv->v_type = VAR_FLOAT;
13751 if (get_float_arg(argvars, &f) == OK)
13752 rettv->vval.v_float = tan(f);
13753 else
13754 rettv->vval.v_float = 0.0;
13755}
13756
13757/*
13758 * "tanh()" function
13759 */
13760 static void
13761f_tanh(typval_T *argvars, typval_T *rettv)
13762{
13763 float_T f = 0.0;
13764
13765 rettv->v_type = VAR_FLOAT;
13766 if (get_float_arg(argvars, &f) == OK)
13767 rettv->vval.v_float = tanh(f);
13768 else
13769 rettv->vval.v_float = 0.0;
13770}
13771#endif
13772
13773/*
13774 * "test_alloc_fail(id, countdown, repeat)" function
13775 */
13776 static void
13777f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
13778{
13779 if (argvars[0].v_type != VAR_NUMBER
13780 || argvars[0].vval.v_number <= 0
13781 || argvars[1].v_type != VAR_NUMBER
13782 || argvars[1].vval.v_number < 0
13783 || argvars[2].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013784 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013785 else
13786 {
13787 alloc_fail_id = argvars[0].vval.v_number;
13788 if (alloc_fail_id >= aid_last)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013789 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013790 alloc_fail_countdown = argvars[1].vval.v_number;
13791 alloc_fail_repeat = argvars[2].vval.v_number;
13792 did_outofmem_msg = FALSE;
13793 }
13794}
13795
13796/*
13797 * "test_autochdir()"
13798 */
13799 static void
13800f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13801{
13802#if defined(FEAT_AUTOCHDIR)
13803 test_autochdir = TRUE;
13804#endif
13805}
13806
13807/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020013808 * "test_feedinput()"
13809 */
13810 static void
13811f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
13812{
13813#ifdef USE_INPUT_BUF
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013814 char_u *val = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5e80de32017-09-03 15:48:12 +020013815
13816 if (val != NULL)
13817 {
13818 trash_input_buf();
13819 add_to_input_buf_csi(val, (int)STRLEN(val));
13820 }
13821#endif
13822}
13823
13824/*
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013825 * "test_option_not_set({name})" function
13826 */
13827 static void
13828f_test_option_not_set(typval_T *argvars, typval_T *rettv UNUSED)
13829{
13830 char_u *name = (char_u *)"";
13831
13832 if (argvars[0].v_type != VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013833 emsg(_(e_invarg));
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013834 else
13835 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013836 name = tv_get_string(&argvars[0]);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013837 if (reset_option_was_set(name) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013838 semsg(_(e_invarg2), name);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013839 }
13840}
13841
13842/*
13843 * "test_override({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013844 */
13845 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013846f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013847{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013848 char_u *name = (char_u *)"";
13849 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013850 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013851
13852 if (argvars[0].v_type != VAR_STRING
13853 || (argvars[1].v_type) != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013854 emsg(_(e_invarg));
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013855 else
13856 {
Bram Moolenaare38197d2018-12-24 23:35:13 +010013857 name = tv_get_string(&argvars[0]);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013858 val = (int)tv_get_number(&argvars[1]);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013859
13860 if (STRCMP(name, (char_u *)"redraw") == 0)
13861 disable_redraw_for_testing = val;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020013862 else if (STRCMP(name, (char_u *)"redraw_flag") == 0)
13863 ignore_redraw_flag_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013864 else if (STRCMP(name, (char_u *)"char_avail") == 0)
13865 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013866 else if (STRCMP(name, (char_u *)"starting") == 0)
13867 {
13868 if (val)
13869 {
13870 if (save_starting < 0)
13871 save_starting = starting;
13872 starting = 0;
13873 }
13874 else
13875 {
13876 starting = save_starting;
13877 save_starting = -1;
13878 }
13879 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013880 else if (STRCMP(name, (char_u *)"nfa_fail") == 0)
13881 nfa_fail_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013882 else if (STRCMP(name, (char_u *)"ALL") == 0)
13883 {
13884 disable_char_avail_for_testing = FALSE;
13885 disable_redraw_for_testing = FALSE;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020013886 ignore_redraw_flag_for_testing = FALSE;
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013887 nfa_fail_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013888 if (save_starting >= 0)
13889 {
13890 starting = save_starting;
13891 save_starting = -1;
13892 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013893 }
13894 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013895 semsg(_(e_invarg2), name);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013896 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013897}
13898
13899/*
13900 * "test_garbagecollect_now()" function
13901 */
13902 static void
13903f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13904{
13905 /* This is dangerous, any Lists and Dicts used internally may be freed
13906 * while still in use. */
13907 garbage_collect(TRUE);
13908}
13909
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013910/*
13911 * "test_ignore_error()" function
13912 */
13913 static void
13914f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
13915{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013916 ignore_error_for_testing(tv_get_string(&argvars[0]));
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013917}
13918
Bram Moolenaarc0f5a782019-01-13 15:16:13 +010013919 static void
13920f_test_null_blob(typval_T *argvars UNUSED, typval_T *rettv)
13921{
13922 rettv->v_type = VAR_BLOB;
13923 rettv->vval.v_blob = NULL;
13924}
13925
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013926#ifdef FEAT_JOB_CHANNEL
13927 static void
13928f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
13929{
13930 rettv->v_type = VAR_CHANNEL;
13931 rettv->vval.v_channel = NULL;
13932}
13933#endif
13934
13935 static void
13936f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
13937{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013938 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013939}
13940
13941#ifdef FEAT_JOB_CHANNEL
13942 static void
13943f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
13944{
13945 rettv->v_type = VAR_JOB;
13946 rettv->vval.v_job = NULL;
13947}
13948#endif
13949
13950 static void
13951f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
13952{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013953 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013954}
13955
13956 static void
13957f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
13958{
13959 rettv->v_type = VAR_PARTIAL;
13960 rettv->vval.v_partial = NULL;
13961}
13962
13963 static void
13964f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
13965{
13966 rettv->v_type = VAR_STRING;
13967 rettv->vval.v_string = NULL;
13968}
13969
Bram Moolenaarab186732018-09-14 21:27:06 +020013970#ifdef FEAT_GUI
13971 static void
13972f_test_scrollbar(typval_T *argvars, typval_T *rettv UNUSED)
13973{
13974 char_u *which;
13975 long value;
13976 int dragging;
13977 scrollbar_T *sb = NULL;
13978
13979 if (argvars[0].v_type != VAR_STRING
13980 || (argvars[1].v_type) != VAR_NUMBER
13981 || (argvars[2].v_type) != VAR_NUMBER)
13982 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013983 emsg(_(e_invarg));
Bram Moolenaarab186732018-09-14 21:27:06 +020013984 return;
13985 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013986 which = tv_get_string(&argvars[0]);
13987 value = tv_get_number(&argvars[1]);
13988 dragging = tv_get_number(&argvars[2]);
Bram Moolenaarab186732018-09-14 21:27:06 +020013989
13990 if (STRCMP(which, "left") == 0)
13991 sb = &curwin->w_scrollbars[SBAR_LEFT];
13992 else if (STRCMP(which, "right") == 0)
13993 sb = &curwin->w_scrollbars[SBAR_RIGHT];
13994 else if (STRCMP(which, "hor") == 0)
13995 sb = &gui.bottom_sbar;
13996 if (sb == NULL)
13997 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013998 semsg(_(e_invarg2), which);
Bram Moolenaarab186732018-09-14 21:27:06 +020013999 return;
14000 }
14001 gui_drag_scrollbar(sb, value, dragging);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020014002# ifndef USE_ON_FLY_SCROLL
14003 // need to loop through normal_cmd() to handle the scroll events
14004 exec_normal(FALSE, TRUE, FALSE);
14005# endif
Bram Moolenaarab186732018-09-14 21:27:06 +020014006}
14007#endif
14008
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014009 static void
14010f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
14011{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014012 time_for_testing = (time_t)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014013}
14014
14015#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
14016/*
14017 * Get a callback from "arg". It can be a Funcref or a function name.
14018 * When "arg" is zero return an empty string.
14019 * Return NULL for an invalid argument.
14020 */
14021 char_u *
14022get_callback(typval_T *arg, partial_T **pp)
14023{
14024 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
14025 {
14026 *pp = arg->vval.v_partial;
14027 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020014028 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014029 }
14030 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020014031 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014032 {
14033 func_ref(arg->vval.v_string);
14034 return arg->vval.v_string;
14035 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014036 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
14037 return (char_u *)"";
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014038 emsg(_("E921: Invalid callback argument"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014039 return NULL;
14040}
14041
14042/*
Bram Moolenaard5359b22018-04-05 22:44:39 +020014043 * Unref/free "callback" and "partial" returned by get_callback().
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014044 */
14045 void
14046free_callback(char_u *callback, partial_T *partial)
14047{
14048 if (partial != NULL)
14049 partial_unref(partial);
14050 else if (callback != NULL)
14051 {
14052 func_unref(callback);
14053 vim_free(callback);
14054 }
14055}
14056#endif
14057
14058#ifdef FEAT_TIMERS
14059/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014060 * "timer_info([timer])" function
14061 */
14062 static void
14063f_timer_info(typval_T *argvars, typval_T *rettv)
14064{
14065 timer_T *timer = NULL;
14066
14067 if (rettv_list_alloc(rettv) != OK)
14068 return;
14069 if (argvars[0].v_type != VAR_UNKNOWN)
14070 {
14071 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014072 emsg(_(e_number_exp));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014073 else
14074 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014075 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014076 if (timer != NULL)
14077 add_timer_info(rettv, timer);
14078 }
14079 }
14080 else
14081 add_timer_info_all(rettv);
14082}
14083
14084/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014085 * "timer_pause(timer, paused)" function
14086 */
14087 static void
14088f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
14089{
14090 timer_T *timer = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014091 int paused = (int)tv_get_number(&argvars[1]);
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014092
14093 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014094 emsg(_(e_number_exp));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014095 else
14096 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014097 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014098 if (timer != NULL)
14099 timer->tr_paused = paused;
14100 }
14101}
14102
14103/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014104 * "timer_start(time, callback [, options])" function
14105 */
14106 static void
14107f_timer_start(typval_T *argvars, typval_T *rettv)
14108{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014109 long msec = (long)tv_get_number(&argvars[0]);
Bram Moolenaar75537a92016-09-05 22:45:28 +020014110 timer_T *timer;
14111 int repeat = 0;
14112 char_u *callback;
14113 dict_T *dict;
14114 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014115
Bram Moolenaar75537a92016-09-05 22:45:28 +020014116 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014117 if (check_secure())
14118 return;
14119 if (argvars[2].v_type != VAR_UNKNOWN)
14120 {
14121 if (argvars[2].v_type != VAR_DICT
14122 || (dict = argvars[2].vval.v_dict) == NULL)
14123 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014124 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014125 return;
14126 }
14127 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014128 repeat = dict_get_number(dict, (char_u *)"repeat");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014129 }
14130
Bram Moolenaar75537a92016-09-05 22:45:28 +020014131 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014132 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020014133 return;
14134
14135 timer = create_timer(msec, repeat);
14136 if (timer == NULL)
14137 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014138 else
14139 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020014140 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020014141 timer->tr_callback = vim_strsave(callback);
14142 else
14143 /* pointer into the partial */
14144 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020014145 timer->tr_partial = partial;
14146 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014147 }
14148}
14149
14150/*
14151 * "timer_stop(timer)" function
14152 */
14153 static void
14154f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
14155{
14156 timer_T *timer;
14157
14158 if (argvars[0].v_type != VAR_NUMBER)
14159 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014160 emsg(_(e_number_exp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014161 return;
14162 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014163 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014164 if (timer != NULL)
14165 stop_timer(timer);
14166}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014167
14168/*
14169 * "timer_stopall()" function
14170 */
14171 static void
14172f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14173{
14174 stop_all_timers();
14175}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014176#endif
14177
14178/*
14179 * "tolower(string)" function
14180 */
14181 static void
14182f_tolower(typval_T *argvars, typval_T *rettv)
14183{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014184 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014185 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014186}
14187
14188/*
14189 * "toupper(string)" function
14190 */
14191 static void
14192f_toupper(typval_T *argvars, typval_T *rettv)
14193{
14194 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014195 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014196}
14197
14198/*
14199 * "tr(string, fromstr, tostr)" function
14200 */
14201 static void
14202f_tr(typval_T *argvars, typval_T *rettv)
14203{
14204 char_u *in_str;
14205 char_u *fromstr;
14206 char_u *tostr;
14207 char_u *p;
14208#ifdef FEAT_MBYTE
14209 int inlen;
14210 int fromlen;
14211 int tolen;
14212 int idx;
14213 char_u *cpstr;
14214 int cplen;
14215 int first = TRUE;
14216#endif
14217 char_u buf[NUMBUFLEN];
14218 char_u buf2[NUMBUFLEN];
14219 garray_T ga;
14220
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014221 in_str = tv_get_string(&argvars[0]);
14222 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
14223 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014224
14225 /* Default return value: empty string. */
14226 rettv->v_type = VAR_STRING;
14227 rettv->vval.v_string = NULL;
14228 if (fromstr == NULL || tostr == NULL)
14229 return; /* type error; errmsg already given */
14230 ga_init2(&ga, (int)sizeof(char), 80);
14231
14232#ifdef FEAT_MBYTE
14233 if (!has_mbyte)
14234#endif
14235 /* not multi-byte: fromstr and tostr must be the same length */
14236 if (STRLEN(fromstr) != STRLEN(tostr))
14237 {
14238#ifdef FEAT_MBYTE
14239error:
14240#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014241 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014242 ga_clear(&ga);
14243 return;
14244 }
14245
14246 /* fromstr and tostr have to contain the same number of chars */
14247 while (*in_str != NUL)
14248 {
14249#ifdef FEAT_MBYTE
14250 if (has_mbyte)
14251 {
14252 inlen = (*mb_ptr2len)(in_str);
14253 cpstr = in_str;
14254 cplen = inlen;
14255 idx = 0;
14256 for (p = fromstr; *p != NUL; p += fromlen)
14257 {
14258 fromlen = (*mb_ptr2len)(p);
14259 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
14260 {
14261 for (p = tostr; *p != NUL; p += tolen)
14262 {
14263 tolen = (*mb_ptr2len)(p);
14264 if (idx-- == 0)
14265 {
14266 cplen = tolen;
14267 cpstr = p;
14268 break;
14269 }
14270 }
14271 if (*p == NUL) /* tostr is shorter than fromstr */
14272 goto error;
14273 break;
14274 }
14275 ++idx;
14276 }
14277
14278 if (first && cpstr == in_str)
14279 {
14280 /* Check that fromstr and tostr have the same number of
14281 * (multi-byte) characters. Done only once when a character
14282 * of in_str doesn't appear in fromstr. */
14283 first = FALSE;
14284 for (p = tostr; *p != NUL; p += tolen)
14285 {
14286 tolen = (*mb_ptr2len)(p);
14287 --idx;
14288 }
14289 if (idx != 0)
14290 goto error;
14291 }
14292
14293 (void)ga_grow(&ga, cplen);
14294 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
14295 ga.ga_len += cplen;
14296
14297 in_str += inlen;
14298 }
14299 else
14300#endif
14301 {
14302 /* When not using multi-byte chars we can do it faster. */
14303 p = vim_strchr(fromstr, *in_str);
14304 if (p != NULL)
14305 ga_append(&ga, tostr[p - fromstr]);
14306 else
14307 ga_append(&ga, *in_str);
14308 ++in_str;
14309 }
14310 }
14311
14312 /* add a terminating NUL */
14313 (void)ga_grow(&ga, 1);
14314 ga_append(&ga, NUL);
14315
14316 rettv->vval.v_string = ga.ga_data;
14317}
14318
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014319/*
14320 * "trim({expr})" function
14321 */
14322 static void
14323f_trim(typval_T *argvars, typval_T *rettv)
14324{
14325 char_u buf1[NUMBUFLEN];
14326 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014327 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014328 char_u *mask = NULL;
14329 char_u *tail;
14330 char_u *prev;
14331 char_u *p;
14332 int c1;
14333
14334 rettv->v_type = VAR_STRING;
14335 if (head == NULL)
14336 {
14337 rettv->vval.v_string = NULL;
14338 return;
14339 }
14340
14341 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014342 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014343
14344 while (*head != NUL)
14345 {
14346 c1 = PTR2CHAR(head);
14347 if (mask == NULL)
14348 {
14349 if (c1 > ' ' && c1 != 0xa0)
14350 break;
14351 }
14352 else
14353 {
14354 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14355 if (c1 == PTR2CHAR(p))
14356 break;
14357 if (*p == NUL)
14358 break;
14359 }
14360 MB_PTR_ADV(head);
14361 }
14362
14363 for (tail = head + STRLEN(head); tail > head; tail = prev)
14364 {
14365 prev = tail;
14366 MB_PTR_BACK(head, prev);
14367 c1 = PTR2CHAR(prev);
14368 if (mask == NULL)
14369 {
14370 if (c1 > ' ' && c1 != 0xa0)
14371 break;
14372 }
14373 else
14374 {
14375 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14376 if (c1 == PTR2CHAR(p))
14377 break;
14378 if (*p == NUL)
14379 break;
14380 }
14381 }
14382 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
14383}
14384
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014385#ifdef FEAT_FLOAT
14386/*
14387 * "trunc({float})" function
14388 */
14389 static void
14390f_trunc(typval_T *argvars, typval_T *rettv)
14391{
14392 float_T f = 0.0;
14393
14394 rettv->v_type = VAR_FLOAT;
14395 if (get_float_arg(argvars, &f) == OK)
14396 /* trunc() is not in C90, use floor() or ceil() instead. */
14397 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
14398 else
14399 rettv->vval.v_float = 0.0;
14400}
14401#endif
14402
14403/*
14404 * "type(expr)" function
14405 */
14406 static void
14407f_type(typval_T *argvars, typval_T *rettv)
14408{
14409 int n = -1;
14410
14411 switch (argvars[0].v_type)
14412 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020014413 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
14414 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014415 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020014416 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
14417 case VAR_LIST: n = VAR_TYPE_LIST; break;
14418 case VAR_DICT: n = VAR_TYPE_DICT; break;
14419 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014420 case VAR_SPECIAL:
14421 if (argvars[0].vval.v_number == VVAL_FALSE
14422 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020014423 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014424 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020014425 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014426 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020014427 case VAR_JOB: n = VAR_TYPE_JOB; break;
14428 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014429 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014430 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010014431 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014432 n = -1;
14433 break;
14434 }
14435 rettv->vval.v_number = n;
14436}
14437
14438/*
14439 * "undofile(name)" function
14440 */
14441 static void
14442f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
14443{
14444 rettv->v_type = VAR_STRING;
14445#ifdef FEAT_PERSISTENT_UNDO
14446 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014447 char_u *fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014448
14449 if (*fname == NUL)
14450 {
14451 /* If there is no file name there will be no undo file. */
14452 rettv->vval.v_string = NULL;
14453 }
14454 else
14455 {
14456 char_u *ffname = FullName_save(fname, FALSE);
14457
14458 if (ffname != NULL)
14459 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
14460 vim_free(ffname);
14461 }
14462 }
14463#else
14464 rettv->vval.v_string = NULL;
14465#endif
14466}
14467
14468/*
14469 * "undotree()" function
14470 */
14471 static void
14472f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
14473{
14474 if (rettv_dict_alloc(rettv) == OK)
14475 {
14476 dict_T *dict = rettv->vval.v_dict;
14477 list_T *list;
14478
Bram Moolenaare0be1672018-07-08 16:50:37 +020014479 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
14480 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
14481 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
14482 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
14483 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
14484 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014485
14486 list = list_alloc();
14487 if (list != NULL)
14488 {
14489 u_eval_tree(curbuf->b_u_oldhead, list);
14490 dict_add_list(dict, "entries", list);
14491 }
14492 }
14493}
14494
14495/*
14496 * "values(dict)" function
14497 */
14498 static void
14499f_values(typval_T *argvars, typval_T *rettv)
14500{
14501 dict_list(argvars, rettv, 1);
14502}
14503
14504/*
14505 * "virtcol(string)" function
14506 */
14507 static void
14508f_virtcol(typval_T *argvars, typval_T *rettv)
14509{
14510 colnr_T vcol = 0;
14511 pos_T *fp;
14512 int fnum = curbuf->b_fnum;
14513
14514 fp = var2fpos(&argvars[0], FALSE, &fnum);
14515 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
14516 && fnum == curbuf->b_fnum)
14517 {
14518 getvvcol(curwin, fp, NULL, NULL, &vcol);
14519 ++vcol;
14520 }
14521
14522 rettv->vval.v_number = vcol;
14523}
14524
14525/*
14526 * "visualmode()" function
14527 */
14528 static void
14529f_visualmode(typval_T *argvars, typval_T *rettv)
14530{
14531 char_u str[2];
14532
14533 rettv->v_type = VAR_STRING;
14534 str[0] = curbuf->b_visual_mode_eval;
14535 str[1] = NUL;
14536 rettv->vval.v_string = vim_strsave(str);
14537
14538 /* A non-zero number or non-empty string argument: reset mode. */
14539 if (non_zero_arg(&argvars[0]))
14540 curbuf->b_visual_mode_eval = NUL;
14541}
14542
14543/*
14544 * "wildmenumode()" function
14545 */
14546 static void
14547f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14548{
14549#ifdef FEAT_WILDMENU
14550 if (wild_menu_showing)
14551 rettv->vval.v_number = 1;
14552#endif
14553}
14554
14555/*
14556 * "winbufnr(nr)" function
14557 */
14558 static void
14559f_winbufnr(typval_T *argvars, typval_T *rettv)
14560{
14561 win_T *wp;
14562
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014563 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014564 if (wp == NULL)
14565 rettv->vval.v_number = -1;
14566 else
14567 rettv->vval.v_number = wp->w_buffer->b_fnum;
14568}
14569
14570/*
14571 * "wincol()" function
14572 */
14573 static void
14574f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
14575{
14576 validate_cursor();
14577 rettv->vval.v_number = curwin->w_wcol + 1;
14578}
14579
14580/*
14581 * "winheight(nr)" function
14582 */
14583 static void
14584f_winheight(typval_T *argvars, typval_T *rettv)
14585{
14586 win_T *wp;
14587
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014588 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014589 if (wp == NULL)
14590 rettv->vval.v_number = -1;
14591 else
14592 rettv->vval.v_number = wp->w_height;
14593}
14594
14595/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020014596 * "winlayout()" function
14597 */
14598 static void
14599f_winlayout(typval_T *argvars, typval_T *rettv)
14600{
14601 tabpage_T *tp;
14602
14603 if (rettv_list_alloc(rettv) != OK)
14604 return;
14605
14606 if (argvars[0].v_type == VAR_UNKNOWN)
14607 tp = curtab;
14608 else
14609 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014610 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020014611 if (tp == NULL)
14612 return;
14613 }
14614
14615 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
14616}
14617
14618/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014619 * "winline()" function
14620 */
14621 static void
14622f_winline(typval_T *argvars UNUSED, typval_T *rettv)
14623{
14624 validate_cursor();
14625 rettv->vval.v_number = curwin->w_wrow + 1;
14626}
14627
14628/*
14629 * "winnr()" function
14630 */
14631 static void
14632f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
14633{
14634 int nr = 1;
14635
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014636 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014637 rettv->vval.v_number = nr;
14638}
14639
14640/*
14641 * "winrestcmd()" function
14642 */
14643 static void
14644f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
14645{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014646 win_T *wp;
14647 int winnr = 1;
14648 garray_T ga;
14649 char_u buf[50];
14650
14651 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020014652 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014653 {
14654 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
14655 ga_concat(&ga, buf);
14656 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
14657 ga_concat(&ga, buf);
14658 ++winnr;
14659 }
14660 ga_append(&ga, NUL);
14661
14662 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014663 rettv->v_type = VAR_STRING;
14664}
14665
14666/*
14667 * "winrestview()" function
14668 */
14669 static void
14670f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
14671{
14672 dict_T *dict;
14673
14674 if (argvars[0].v_type != VAR_DICT
14675 || (dict = argvars[0].vval.v_dict) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014676 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014677 else
14678 {
14679 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014680 curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014681 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014682 curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014683#ifdef FEAT_VIRTUALEDIT
14684 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014685 curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014686#endif
14687 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
14688 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010014689 curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014690 curwin->w_set_curswant = FALSE;
14691 }
14692
14693 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014694 set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014695#ifdef FEAT_DIFF
14696 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014697 curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014698#endif
14699 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014700 curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014701 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014702 curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014703
14704 check_cursor();
14705 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020014706 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014707 changed_window_setting();
14708
14709 if (curwin->w_topline <= 0)
14710 curwin->w_topline = 1;
14711 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
14712 curwin->w_topline = curbuf->b_ml.ml_line_count;
14713#ifdef FEAT_DIFF
14714 check_topfill(curwin, TRUE);
14715#endif
14716 }
14717}
14718
14719/*
14720 * "winsaveview()" function
14721 */
14722 static void
14723f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
14724{
14725 dict_T *dict;
14726
14727 if (rettv_dict_alloc(rettv) == FAIL)
14728 return;
14729 dict = rettv->vval.v_dict;
14730
Bram Moolenaare0be1672018-07-08 16:50:37 +020014731 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
14732 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014733#ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +020014734 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014735#endif
14736 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020014737 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014738
Bram Moolenaare0be1672018-07-08 16:50:37 +020014739 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014740#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020014741 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014742#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020014743 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
14744 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014745}
14746
14747/*
14748 * "winwidth(nr)" function
14749 */
14750 static void
14751f_winwidth(typval_T *argvars, typval_T *rettv)
14752{
14753 win_T *wp;
14754
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014755 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014756 if (wp == NULL)
14757 rettv->vval.v_number = -1;
14758 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014759 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014760}
14761
14762/*
14763 * "wordcount()" function
14764 */
14765 static void
14766f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
14767{
14768 if (rettv_dict_alloc(rettv) == FAIL)
14769 return;
14770 cursor_pos_info(rettv->vval.v_dict);
14771}
14772
14773/*
14774 * "writefile()" function
14775 */
14776 static void
14777f_writefile(typval_T *argvars, typval_T *rettv)
14778{
14779 int binary = FALSE;
14780 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014781#ifdef HAVE_FSYNC
14782 int do_fsync = p_fs;
14783#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014784 char_u *fname;
14785 FILE *fd;
14786 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014787 listitem_T *li;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014788 list_T *list = NULL;
14789 blob_T *blob = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014790
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014791 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014792 if (check_restricted() || check_secure())
14793 return;
14794
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014795 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014796 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014797 list = argvars[0].vval.v_list;
14798 if (list == NULL)
14799 return;
14800 for (li = list->lv_first; li != NULL; li = li->li_next)
14801 if (tv_get_string_chk(&li->li_tv) == NULL)
14802 return;
14803 }
14804 else if (argvars[0].v_type == VAR_BLOB)
14805 {
14806 blob = argvars[0].vval.v_blob;
14807 if (blob == NULL)
14808 return;
14809 }
14810 else
14811 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014812 semsg(_(e_invarg2), "writefile()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014813 return;
14814 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014815
14816 if (argvars[2].v_type != VAR_UNKNOWN)
14817 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014818 char_u *arg2 = tv_get_string_chk(&argvars[2]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014819
14820 if (arg2 == NULL)
14821 return;
14822 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014823 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014824 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014825 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014826#ifdef HAVE_FSYNC
14827 if (vim_strchr(arg2, 's') != NULL)
14828 do_fsync = TRUE;
14829 else if (vim_strchr(arg2, 'S') != NULL)
14830 do_fsync = FALSE;
14831#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014832 }
14833
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014834 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014835 if (fname == NULL)
14836 return;
14837
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014838 /* Always open the file in binary mode, library functions have a mind of
14839 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014840 if (*fname == NUL || (fd = mch_fopen((char *)fname,
14841 append ? APPENDBIN : WRITEBIN)) == NULL)
14842 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014843 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014844 ret = -1;
14845 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014846 else if (blob)
14847 {
14848 if (write_blob(fd, blob) == FAIL)
14849 ret = -1;
14850#ifdef HAVE_FSYNC
14851 else if (do_fsync)
14852 // Ignore the error, the user wouldn't know what to do about it.
14853 // May happen for a device.
14854 vim_ignored = fsync(fileno(fd));
14855#endif
14856 fclose(fd);
14857 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014858 else
14859 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014860 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014861 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014862#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010014863 else if (do_fsync)
14864 /* Ignore the error, the user wouldn't know what to do about it.
14865 * May happen for a device. */
Bram Moolenaar42335f52018-09-13 15:33:43 +020014866 vim_ignored = fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014867#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014868 fclose(fd);
14869 }
14870
14871 rettv->vval.v_number = ret;
14872}
14873
14874/*
14875 * "xor(expr, expr)" function
14876 */
14877 static void
14878f_xor(typval_T *argvars, typval_T *rettv)
14879{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014880 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
14881 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014882}
14883
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014884#endif /* FEAT_EVAL */