blob: 1bdb72263528c89fdff49df18432a6e43ae79b29 [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;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004411 if (idx < 0)
4412 idx = blob_len(argvars[0].vval.v_blob) + idx;
4413 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
4414 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004415 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004416 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004417 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004418 tv = rettv;
4419 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004420 }
4421 }
4422 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004423 {
4424 if ((l = argvars[0].vval.v_list) != NULL)
4425 {
4426 int error = FALSE;
4427
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004428 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004429 if (!error && li != NULL)
4430 tv = &li->li_tv;
4431 }
4432 }
4433 else if (argvars[0].v_type == VAR_DICT)
4434 {
4435 if ((d = argvars[0].vval.v_dict) != NULL)
4436 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004437 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004438 if (di != NULL)
4439 tv = &di->di_tv;
4440 }
4441 }
4442 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4443 {
4444 partial_T *pt;
4445 partial_T fref_pt;
4446
4447 if (argvars[0].v_type == VAR_PARTIAL)
4448 pt = argvars[0].vval.v_partial;
4449 else
4450 {
4451 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4452 fref_pt.pt_name = argvars[0].vval.v_string;
4453 pt = &fref_pt;
4454 }
4455
4456 if (pt != NULL)
4457 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004458 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004459 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004460
4461 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4462 {
4463 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004464 n = partial_name(pt);
4465 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004466 rettv->vval.v_string = NULL;
4467 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004468 {
4469 rettv->vval.v_string = vim_strsave(n);
4470 if (rettv->v_type == VAR_FUNC)
4471 func_ref(rettv->vval.v_string);
4472 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004473 }
4474 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004475 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004476 else if (STRCMP(what, "args") == 0)
4477 {
4478 rettv->v_type = VAR_LIST;
4479 if (rettv_list_alloc(rettv) == OK)
4480 {
4481 int i;
4482
4483 for (i = 0; i < pt->pt_argc; ++i)
4484 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4485 }
4486 }
4487 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004488 semsg(_(e_invarg2), what);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004489 return;
4490 }
4491 }
4492 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004493 semsg(_(e_listdictarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004494
4495 if (tv == NULL)
4496 {
4497 if (argvars[2].v_type != VAR_UNKNOWN)
4498 copy_tv(&argvars[2], rettv);
4499 }
4500 else
4501 copy_tv(tv, rettv);
4502}
4503
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004504/*
4505 * Returns buffer options, variables and other attributes in a dictionary.
4506 */
4507 static dict_T *
4508get_buffer_info(buf_T *buf)
4509{
4510 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004511 tabpage_T *tp;
4512 win_T *wp;
4513 list_T *windows;
4514
4515 dict = dict_alloc();
4516 if (dict == NULL)
4517 return NULL;
4518
Bram Moolenaare0be1672018-07-08 16:50:37 +02004519 dict_add_number(dict, "bufnr", buf->b_fnum);
4520 dict_add_string(dict, "name", buf->b_ffname);
4521 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4522 : buflist_findlnum(buf));
4523 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4524 dict_add_number(dict, "listed", buf->b_p_bl);
4525 dict_add_number(dict, "changed", bufIsChanged(buf));
4526 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4527 dict_add_number(dict, "hidden",
4528 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004529
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004530 /* Get a reference to buffer variables */
4531 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004532
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004533 /* List of windows displaying this buffer */
4534 windows = list_alloc();
4535 if (windows != NULL)
4536 {
4537 FOR_ALL_TAB_WINDOWS(tp, wp)
4538 if (wp->w_buffer == buf)
4539 list_append_number(windows, (varnumber_T)wp->w_id);
4540 dict_add_list(dict, "windows", windows);
4541 }
4542
4543#ifdef FEAT_SIGNS
4544 if (buf->b_signlist != NULL)
4545 {
4546 /* List of signs placed in this buffer */
4547 list_T *signs = list_alloc();
4548 if (signs != NULL)
4549 {
4550 get_buffer_signs(buf, signs);
4551 dict_add_list(dict, "signs", signs);
4552 }
4553 }
4554#endif
4555
4556 return dict;
4557}
4558
4559/*
4560 * "getbufinfo()" function
4561 */
4562 static void
4563f_getbufinfo(typval_T *argvars, typval_T *rettv)
4564{
4565 buf_T *buf = NULL;
4566 buf_T *argbuf = NULL;
4567 dict_T *d;
4568 int filtered = FALSE;
4569 int sel_buflisted = FALSE;
4570 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004571 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004572
4573 if (rettv_list_alloc(rettv) != OK)
4574 return;
4575
4576 /* List of all the buffers or selected buffers */
4577 if (argvars[0].v_type == VAR_DICT)
4578 {
4579 dict_T *sel_d = argvars[0].vval.v_dict;
4580
4581 if (sel_d != NULL)
4582 {
4583 dictitem_T *di;
4584
4585 filtered = TRUE;
4586
4587 di = dict_find(sel_d, (char_u *)"buflisted", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004588 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004589 sel_buflisted = TRUE;
4590
4591 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004592 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004593 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004594
4595 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004596 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004597 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004598 }
4599 }
4600 else if (argvars[0].v_type != VAR_UNKNOWN)
4601 {
4602 /* Information about one buffer. Argument specifies the buffer */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004603 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004604 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004605 argbuf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004606 --emsg_off;
4607 if (argbuf == NULL)
4608 return;
4609 }
4610
4611 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004612 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004613 {
4614 if (argbuf != NULL && argbuf != buf)
4615 continue;
4616 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004617 || (sel_buflisted && !buf->b_p_bl)
4618 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004619 continue;
4620
4621 d = get_buffer_info(buf);
4622 if (d != NULL)
4623 list_append_dict(rettv->vval.v_list, d);
4624 if (argbuf != NULL)
4625 return;
4626 }
4627}
4628
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004629/*
4630 * Get line or list of lines from buffer "buf" into "rettv".
4631 * Return a range (from start to end) of lines in rettv from the specified
4632 * buffer.
4633 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4634 */
4635 static void
4636get_buffer_lines(
4637 buf_T *buf,
4638 linenr_T start,
4639 linenr_T end,
4640 int retlist,
4641 typval_T *rettv)
4642{
4643 char_u *p;
4644
4645 rettv->v_type = VAR_STRING;
4646 rettv->vval.v_string = NULL;
4647 if (retlist && rettv_list_alloc(rettv) == FAIL)
4648 return;
4649
4650 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4651 return;
4652
4653 if (!retlist)
4654 {
4655 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4656 p = ml_get_buf(buf, start, FALSE);
4657 else
4658 p = (char_u *)"";
4659 rettv->vval.v_string = vim_strsave(p);
4660 }
4661 else
4662 {
4663 if (end < start)
4664 return;
4665
4666 if (start < 1)
4667 start = 1;
4668 if (end > buf->b_ml.ml_line_count)
4669 end = buf->b_ml.ml_line_count;
4670 while (start <= end)
4671 if (list_append_string(rettv->vval.v_list,
4672 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4673 break;
4674 }
4675}
4676
4677/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004678 * "getbufline()" function
4679 */
4680 static void
4681f_getbufline(typval_T *argvars, typval_T *rettv)
4682{
4683 linenr_T lnum;
4684 linenr_T end;
4685 buf_T *buf;
4686
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004687 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004688 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004689 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004690 --emsg_off;
4691
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004692 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004693 if (argvars[2].v_type == VAR_UNKNOWN)
4694 end = lnum;
4695 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004696 end = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004697
4698 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4699}
4700
4701/*
4702 * "getbufvar()" function
4703 */
4704 static void
4705f_getbufvar(typval_T *argvars, typval_T *rettv)
4706{
4707 buf_T *buf;
4708 buf_T *save_curbuf;
4709 char_u *varname;
4710 dictitem_T *v;
4711 int done = FALSE;
4712
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004713 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
4714 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004715 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004716 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004717
4718 rettv->v_type = VAR_STRING;
4719 rettv->vval.v_string = NULL;
4720
4721 if (buf != NULL && varname != NULL)
4722 {
4723 /* set curbuf to be our buf, temporarily */
4724 save_curbuf = curbuf;
4725 curbuf = buf;
4726
Bram Moolenaar30567352016-08-27 21:25:44 +02004727 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004728 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004729 if (varname[1] == NUL)
4730 {
4731 /* get all buffer-local options in a dict */
4732 dict_T *opts = get_winbuf_options(TRUE);
4733
4734 if (opts != NULL)
4735 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004736 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004737 done = TRUE;
4738 }
4739 }
4740 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4741 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004742 done = TRUE;
4743 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004744 else
4745 {
4746 /* Look up the variable. */
4747 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4748 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4749 'b', varname, FALSE);
4750 if (v != NULL)
4751 {
4752 copy_tv(&v->di_tv, rettv);
4753 done = TRUE;
4754 }
4755 }
4756
4757 /* restore previous notion of curbuf */
4758 curbuf = save_curbuf;
4759 }
4760
4761 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4762 /* use the default value */
4763 copy_tv(&argvars[2], rettv);
4764
4765 --emsg_off;
4766}
4767
4768/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004769 * "getchangelist()" function
4770 */
4771 static void
4772f_getchangelist(typval_T *argvars, typval_T *rettv)
4773{
4774#ifdef FEAT_JUMPLIST
4775 buf_T *buf;
4776 int i;
4777 list_T *l;
4778 dict_T *d;
4779#endif
4780
4781 if (rettv_list_alloc(rettv) != OK)
4782 return;
4783
4784#ifdef FEAT_JUMPLIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004785 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004786 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004787 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004788 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004789 if (buf == NULL)
4790 return;
4791
4792 l = list_alloc();
4793 if (l == NULL)
4794 return;
4795
4796 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4797 return;
4798 /*
4799 * The current window change list index tracks only the position in the
4800 * current buffer change list. For other buffers, use the change list
4801 * length as the current index.
4802 */
4803 list_append_number(rettv->vval.v_list,
4804 (varnumber_T)((buf == curwin->w_buffer)
4805 ? curwin->w_changelistidx : buf->b_changelistlen));
4806
4807 for (i = 0; i < buf->b_changelistlen; ++i)
4808 {
4809 if (buf->b_changelist[i].lnum == 0)
4810 continue;
4811 if ((d = dict_alloc()) == NULL)
4812 return;
4813 if (list_append_dict(l, d) == FAIL)
4814 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004815 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4816 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004817# ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +02004818 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004819# endif
4820 }
4821#endif
4822}
4823/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004824 * "getchar()" function
4825 */
4826 static void
4827f_getchar(typval_T *argvars, typval_T *rettv)
4828{
4829 varnumber_T n;
4830 int error = FALSE;
4831
Bram Moolenaar84d93902018-09-11 20:10:20 +02004832#ifdef MESSAGE_QUEUE
4833 // vpeekc() used to check for messages, but that caused problems, invoking
4834 // a callback where it was not expected. Some plugins use getchar(1) in a
4835 // loop to await a message, therefore make sure we check for messages here.
4836 parse_queued_messages();
4837#endif
4838
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004839 /* Position the cursor. Needed after a message that ends in a space. */
4840 windgoto(msg_row, msg_col);
4841
4842 ++no_mapping;
4843 ++allow_keys;
4844 for (;;)
4845 {
4846 if (argvars[0].v_type == VAR_UNKNOWN)
4847 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004848 n = plain_vgetc();
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004849 else if (tv_get_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004850 /* getchar(1): only check if char avail */
4851 n = vpeekc_any();
4852 else if (error || vpeekc_any() == NUL)
4853 /* illegal argument or getchar(0) and no char avail: return zero */
4854 n = 0;
4855 else
4856 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004857 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004858
4859 if (n == K_IGNORE)
4860 continue;
4861 break;
4862 }
4863 --no_mapping;
4864 --allow_keys;
4865
4866 set_vim_var_nr(VV_MOUSE_WIN, 0);
4867 set_vim_var_nr(VV_MOUSE_WINID, 0);
4868 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4869 set_vim_var_nr(VV_MOUSE_COL, 0);
4870
4871 rettv->vval.v_number = n;
4872 if (IS_SPECIAL(n) || mod_mask != 0)
4873 {
4874 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4875 int i = 0;
4876
4877 /* Turn a special key into three bytes, plus modifier. */
4878 if (mod_mask != 0)
4879 {
4880 temp[i++] = K_SPECIAL;
4881 temp[i++] = KS_MODIFIER;
4882 temp[i++] = mod_mask;
4883 }
4884 if (IS_SPECIAL(n))
4885 {
4886 temp[i++] = K_SPECIAL;
4887 temp[i++] = K_SECOND(n);
4888 temp[i++] = K_THIRD(n);
4889 }
4890#ifdef FEAT_MBYTE
4891 else if (has_mbyte)
4892 i += (*mb_char2bytes)(n, temp + i);
4893#endif
4894 else
4895 temp[i++] = n;
4896 temp[i++] = NUL;
4897 rettv->v_type = VAR_STRING;
4898 rettv->vval.v_string = vim_strsave(temp);
4899
4900#ifdef FEAT_MOUSE
4901 if (is_mouse_key(n))
4902 {
4903 int row = mouse_row;
4904 int col = mouse_col;
4905 win_T *win;
4906 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004907 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004908 int winnr = 1;
4909
4910 if (row >= 0 && col >= 0)
4911 {
4912 /* Find the window at the mouse coordinates and compute the
4913 * text position. */
4914 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004915 if (win == NULL)
4916 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004917 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004918 for (wp = firstwin; wp != win; wp = wp->w_next)
4919 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004920 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4921 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4922 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4923 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4924 }
4925 }
4926#endif
4927 }
4928}
4929
4930/*
4931 * "getcharmod()" function
4932 */
4933 static void
4934f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4935{
4936 rettv->vval.v_number = mod_mask;
4937}
4938
4939/*
4940 * "getcharsearch()" function
4941 */
4942 static void
4943f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4944{
4945 if (rettv_dict_alloc(rettv) != FAIL)
4946 {
4947 dict_T *dict = rettv->vval.v_dict;
4948
Bram Moolenaare0be1672018-07-08 16:50:37 +02004949 dict_add_string(dict, "char", last_csearch());
4950 dict_add_number(dict, "forward", last_csearch_forward());
4951 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004952 }
4953}
4954
4955/*
4956 * "getcmdline()" function
4957 */
4958 static void
4959f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4960{
4961 rettv->v_type = VAR_STRING;
4962 rettv->vval.v_string = get_cmdline_str();
4963}
4964
4965/*
4966 * "getcmdpos()" function
4967 */
4968 static void
4969f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4970{
4971 rettv->vval.v_number = get_cmdline_pos() + 1;
4972}
4973
4974/*
4975 * "getcmdtype()" function
4976 */
4977 static void
4978f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4979{
4980 rettv->v_type = VAR_STRING;
4981 rettv->vval.v_string = alloc(2);
4982 if (rettv->vval.v_string != NULL)
4983 {
4984 rettv->vval.v_string[0] = get_cmdline_type();
4985 rettv->vval.v_string[1] = NUL;
4986 }
4987}
4988
4989/*
4990 * "getcmdwintype()" function
4991 */
4992 static void
4993f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4994{
4995 rettv->v_type = VAR_STRING;
4996 rettv->vval.v_string = NULL;
4997#ifdef FEAT_CMDWIN
4998 rettv->vval.v_string = alloc(2);
4999 if (rettv->vval.v_string != NULL)
5000 {
5001 rettv->vval.v_string[0] = cmdwin_type;
5002 rettv->vval.v_string[1] = NUL;
5003 }
5004#endif
5005}
5006
5007#if defined(FEAT_CMDL_COMPL)
5008/*
5009 * "getcompletion()" function
5010 */
5011 static void
5012f_getcompletion(typval_T *argvars, typval_T *rettv)
5013{
5014 char_u *pat;
5015 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005016 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005017 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
5018 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005019
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005020 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005021 filtered = tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005022
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005023 if (p_wic)
5024 options |= WILD_ICASE;
5025
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005026 /* For filtered results, 'wildignore' is used */
5027 if (!filtered)
5028 options |= WILD_KEEP_ALL;
5029
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005030 ExpandInit(&xpc);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005031 xpc.xp_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005032 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005033 xpc.xp_context = cmdcomplete_str_to_type(tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005034 if (xpc.xp_context == EXPAND_NOTHING)
5035 {
5036 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005037 semsg(_(e_invarg2), argvars[1].vval.v_string);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005038 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005039 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005040 return;
5041 }
5042
5043# if defined(FEAT_MENU)
5044 if (xpc.xp_context == EXPAND_MENUS)
5045 {
5046 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
5047 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5048 }
5049# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02005050#ifdef FEAT_CSCOPE
5051 if (xpc.xp_context == EXPAND_CSCOPE)
5052 {
5053 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
5054 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5055 }
5056#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02005057#ifdef FEAT_SIGNS
5058 if (xpc.xp_context == EXPAND_SIGN)
5059 {
5060 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
5061 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5062 }
5063#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005064
5065 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
5066 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
5067 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005068 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005069
5070 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
5071
5072 for (i = 0; i < xpc.xp_numfiles; i++)
5073 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5074 }
5075 vim_free(pat);
5076 ExpandCleanup(&xpc);
5077}
5078#endif
5079
5080/*
5081 * "getcwd()" function
5082 */
5083 static void
5084f_getcwd(typval_T *argvars, typval_T *rettv)
5085{
5086 win_T *wp = NULL;
5087 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01005088 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005089
5090 rettv->v_type = VAR_STRING;
5091 rettv->vval.v_string = NULL;
5092
Bram Moolenaar54591292018-02-09 20:53:59 +01005093 if (argvars[0].v_type == VAR_NUMBER && argvars[0].vval.v_number == -1)
5094 global = TRUE;
5095 else
5096 wp = find_tabwin(&argvars[0], &argvars[1]);
5097
5098 if (wp != NULL && wp->w_localdir != NULL)
5099 rettv->vval.v_string = vim_strsave(wp->w_localdir);
5100 else if (wp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005101 {
Bram Moolenaar54591292018-02-09 20:53:59 +01005102 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005103 rettv->vval.v_string = vim_strsave(globaldir);
5104 else
5105 {
5106 cwd = alloc(MAXPATHL);
5107 if (cwd != NULL)
5108 {
5109 if (mch_dirname(cwd, MAXPATHL) != FAIL)
5110 rettv->vval.v_string = vim_strsave(cwd);
5111 vim_free(cwd);
5112 }
5113 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005114 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02005115#ifdef BACKSLASH_IN_FILENAME
5116 if (rettv->vval.v_string != NULL)
5117 slash_adjust(rettv->vval.v_string);
5118#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005119}
5120
5121/*
5122 * "getfontname()" function
5123 */
5124 static void
5125f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
5126{
5127 rettv->v_type = VAR_STRING;
5128 rettv->vval.v_string = NULL;
5129#ifdef FEAT_GUI
5130 if (gui.in_use)
5131 {
5132 GuiFont font;
5133 char_u *name = NULL;
5134
5135 if (argvars[0].v_type == VAR_UNKNOWN)
5136 {
5137 /* Get the "Normal" font. Either the name saved by
5138 * hl_set_font_name() or from the font ID. */
5139 font = gui.norm_font;
5140 name = hl_get_font_name();
5141 }
5142 else
5143 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005144 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005145 if (STRCMP(name, "*") == 0) /* don't use font dialog */
5146 return;
5147 font = gui_mch_get_font(name, FALSE);
5148 if (font == NOFONT)
5149 return; /* Invalid font name, return empty string. */
5150 }
5151 rettv->vval.v_string = gui_mch_get_fontname(font, name);
5152 if (argvars[0].v_type != VAR_UNKNOWN)
5153 gui_mch_free_font(font);
5154 }
5155#endif
5156}
5157
5158/*
5159 * "getfperm({fname})" function
5160 */
5161 static void
5162f_getfperm(typval_T *argvars, typval_T *rettv)
5163{
5164 char_u *fname;
5165 stat_T st;
5166 char_u *perm = NULL;
5167 char_u flags[] = "rwx";
5168 int i;
5169
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005170 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005171
5172 rettv->v_type = VAR_STRING;
5173 if (mch_stat((char *)fname, &st) >= 0)
5174 {
5175 perm = vim_strsave((char_u *)"---------");
5176 if (perm != NULL)
5177 {
5178 for (i = 0; i < 9; i++)
5179 {
5180 if (st.st_mode & (1 << (8 - i)))
5181 perm[i] = flags[i % 3];
5182 }
5183 }
5184 }
5185 rettv->vval.v_string = perm;
5186}
5187
5188/*
5189 * "getfsize({fname})" function
5190 */
5191 static void
5192f_getfsize(typval_T *argvars, typval_T *rettv)
5193{
5194 char_u *fname;
5195 stat_T st;
5196
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005197 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005198
5199 rettv->v_type = VAR_NUMBER;
5200
5201 if (mch_stat((char *)fname, &st) >= 0)
5202 {
5203 if (mch_isdir(fname))
5204 rettv->vval.v_number = 0;
5205 else
5206 {
5207 rettv->vval.v_number = (varnumber_T)st.st_size;
5208
5209 /* non-perfect check for overflow */
5210 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5211 rettv->vval.v_number = -2;
5212 }
5213 }
5214 else
5215 rettv->vval.v_number = -1;
5216}
5217
5218/*
5219 * "getftime({fname})" function
5220 */
5221 static void
5222f_getftime(typval_T *argvars, typval_T *rettv)
5223{
5224 char_u *fname;
5225 stat_T st;
5226
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005227 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005228
5229 if (mch_stat((char *)fname, &st) >= 0)
5230 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5231 else
5232 rettv->vval.v_number = -1;
5233}
5234
5235/*
5236 * "getftype({fname})" function
5237 */
5238 static void
5239f_getftype(typval_T *argvars, typval_T *rettv)
5240{
5241 char_u *fname;
5242 stat_T st;
5243 char_u *type = NULL;
5244 char *t;
5245
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005246 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005247
5248 rettv->v_type = VAR_STRING;
5249 if (mch_lstat((char *)fname, &st) >= 0)
5250 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005251 if (S_ISREG(st.st_mode))
5252 t = "file";
5253 else if (S_ISDIR(st.st_mode))
5254 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005255 else if (S_ISLNK(st.st_mode))
5256 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005257 else if (S_ISBLK(st.st_mode))
5258 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005259 else if (S_ISCHR(st.st_mode))
5260 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005261 else if (S_ISFIFO(st.st_mode))
5262 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005263 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005264 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005265 else
5266 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005267 type = vim_strsave((char_u *)t);
5268 }
5269 rettv->vval.v_string = type;
5270}
5271
5272/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005273 * "getjumplist()" function
5274 */
5275 static void
5276f_getjumplist(typval_T *argvars, typval_T *rettv)
5277{
5278#ifdef FEAT_JUMPLIST
5279 win_T *wp;
5280 int i;
5281 list_T *l;
5282 dict_T *d;
5283#endif
5284
5285 if (rettv_list_alloc(rettv) != OK)
5286 return;
5287
5288#ifdef FEAT_JUMPLIST
5289 wp = find_tabwin(&argvars[0], &argvars[1]);
5290 if (wp == NULL)
5291 return;
5292
5293 l = list_alloc();
5294 if (l == NULL)
5295 return;
5296
5297 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5298 return;
5299 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5300
Bram Moolenaar48679742018-02-13 13:33:29 +01005301 cleanup_jumplist(wp, TRUE);
5302
Bram Moolenaar4f505882018-02-10 21:06:32 +01005303 for (i = 0; i < wp->w_jumplistlen; ++i)
5304 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005305 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5306 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005307 if ((d = dict_alloc()) == NULL)
5308 return;
5309 if (list_append_dict(l, d) == FAIL)
5310 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005311 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5312 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005313# ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +02005314 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005315# endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005316 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005317 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005318 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005319 }
5320#endif
5321}
5322
5323/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005324 * "getline(lnum, [end])" function
5325 */
5326 static void
5327f_getline(typval_T *argvars, typval_T *rettv)
5328{
5329 linenr_T lnum;
5330 linenr_T end;
5331 int retlist;
5332
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005333 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005334 if (argvars[1].v_type == VAR_UNKNOWN)
5335 {
5336 end = 0;
5337 retlist = FALSE;
5338 }
5339 else
5340 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005341 end = tv_get_lnum(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005342 retlist = TRUE;
5343 }
5344
5345 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5346}
5347
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005348#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005349 static void
5350get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5351{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005352 if (what_arg->v_type == VAR_UNKNOWN)
5353 {
5354 if (rettv_list_alloc(rettv) == OK)
5355 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005356 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005357 }
5358 else
5359 {
5360 if (rettv_dict_alloc(rettv) == OK)
5361 if (is_qf || (wp != NULL))
5362 {
5363 if (what_arg->v_type == VAR_DICT)
5364 {
5365 dict_T *d = what_arg->vval.v_dict;
5366
5367 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005368 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005369 }
5370 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005371 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +02005372 }
5373 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005374}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005375#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005376
5377/*
5378 * "getloclist()" function
5379 */
5380 static void
5381f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5382{
5383#ifdef FEAT_QUICKFIX
5384 win_T *wp;
5385
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005386 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005387 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5388#endif
5389}
5390
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005391/*
5392 * "getmatches()" function
5393 */
5394 static void
5395f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5396{
5397#ifdef FEAT_SEARCH_EXTRA
5398 dict_T *dict;
5399 matchitem_T *cur = curwin->w_match_head;
5400 int i;
5401
5402 if (rettv_list_alloc(rettv) == OK)
5403 {
5404 while (cur != NULL)
5405 {
5406 dict = dict_alloc();
5407 if (dict == NULL)
5408 return;
5409 if (cur->match.regprog == NULL)
5410 {
5411 /* match added with matchaddpos() */
5412 for (i = 0; i < MAXPOSMATCH; ++i)
5413 {
5414 llpos_T *llpos;
5415 char buf[6];
5416 list_T *l;
5417
5418 llpos = &cur->pos.pos[i];
5419 if (llpos->lnum == 0)
5420 break;
5421 l = list_alloc();
5422 if (l == NULL)
5423 break;
5424 list_append_number(l, (varnumber_T)llpos->lnum);
5425 if (llpos->col > 0)
5426 {
5427 list_append_number(l, (varnumber_T)llpos->col);
5428 list_append_number(l, (varnumber_T)llpos->len);
5429 }
5430 sprintf(buf, "pos%d", i + 1);
5431 dict_add_list(dict, buf, l);
5432 }
5433 }
5434 else
5435 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02005436 dict_add_string(dict, "pattern", cur->pattern);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005437 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02005438 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
5439 dict_add_number(dict, "priority", (long)cur->priority);
5440 dict_add_number(dict, "id", (long)cur->id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005441# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
5442 if (cur->conceal_char)
5443 {
5444 char_u buf[MB_MAXBYTES + 1];
5445
5446 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005447 dict_add_string(dict, "conceal", (char_u *)&buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005448 }
5449# endif
5450 list_append_dict(rettv->vval.v_list, dict);
5451 cur = cur->next;
5452 }
5453 }
5454#endif
5455}
5456
5457/*
5458 * "getpid()" function
5459 */
5460 static void
5461f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5462{
5463 rettv->vval.v_number = mch_get_pid();
5464}
5465
5466 static void
5467getpos_both(
5468 typval_T *argvars,
5469 typval_T *rettv,
5470 int getcurpos)
5471{
5472 pos_T *fp;
5473 list_T *l;
5474 int fnum = -1;
5475
5476 if (rettv_list_alloc(rettv) == OK)
5477 {
5478 l = rettv->vval.v_list;
5479 if (getcurpos)
5480 fp = &curwin->w_cursor;
5481 else
5482 fp = var2fpos(&argvars[0], TRUE, &fnum);
5483 if (fnum != -1)
5484 list_append_number(l, (varnumber_T)fnum);
5485 else
5486 list_append_number(l, (varnumber_T)0);
5487 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5488 : (varnumber_T)0);
5489 list_append_number(l, (fp != NULL)
5490 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5491 : (varnumber_T)0);
5492 list_append_number(l,
5493#ifdef FEAT_VIRTUALEDIT
5494 (fp != NULL) ? (varnumber_T)fp->coladd :
5495#endif
5496 (varnumber_T)0);
5497 if (getcurpos)
5498 {
5499 update_curswant();
5500 list_append_number(l, curwin->w_curswant == MAXCOL ?
5501 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
5502 }
5503 }
5504 else
5505 rettv->vval.v_number = FALSE;
5506}
5507
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005508/*
5509 * "getcurpos()" function
5510 */
5511 static void
5512f_getcurpos(typval_T *argvars, typval_T *rettv)
5513{
5514 getpos_both(argvars, rettv, TRUE);
5515}
5516
5517/*
5518 * "getpos(string)" function
5519 */
5520 static void
5521f_getpos(typval_T *argvars, typval_T *rettv)
5522{
5523 getpos_both(argvars, rettv, FALSE);
5524}
5525
5526/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005527 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005528 */
5529 static void
5530f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5531{
5532#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005533 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005534#endif
5535}
5536
5537/*
5538 * "getreg()" function
5539 */
5540 static void
5541f_getreg(typval_T *argvars, typval_T *rettv)
5542{
5543 char_u *strregname;
5544 int regname;
5545 int arg2 = FALSE;
5546 int return_list = FALSE;
5547 int error = FALSE;
5548
5549 if (argvars[0].v_type != VAR_UNKNOWN)
5550 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005551 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005552 error = strregname == NULL;
5553 if (argvars[1].v_type != VAR_UNKNOWN)
5554 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005555 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005556 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005557 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005558 }
5559 }
5560 else
5561 strregname = get_vim_var_str(VV_REG);
5562
5563 if (error)
5564 return;
5565
5566 regname = (strregname == NULL ? '"' : *strregname);
5567 if (regname == 0)
5568 regname = '"';
5569
5570 if (return_list)
5571 {
5572 rettv->v_type = VAR_LIST;
5573 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5574 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5575 if (rettv->vval.v_list == NULL)
5576 (void)rettv_list_alloc(rettv);
5577 else
5578 ++rettv->vval.v_list->lv_refcount;
5579 }
5580 else
5581 {
5582 rettv->v_type = VAR_STRING;
5583 rettv->vval.v_string = get_reg_contents(regname,
5584 arg2 ? GREG_EXPR_SRC : 0);
5585 }
5586}
5587
5588/*
5589 * "getregtype()" function
5590 */
5591 static void
5592f_getregtype(typval_T *argvars, typval_T *rettv)
5593{
5594 char_u *strregname;
5595 int regname;
5596 char_u buf[NUMBUFLEN + 2];
5597 long reglen = 0;
5598
5599 if (argvars[0].v_type != VAR_UNKNOWN)
5600 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005601 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005602 if (strregname == NULL) /* type error; errmsg already given */
5603 {
5604 rettv->v_type = VAR_STRING;
5605 rettv->vval.v_string = NULL;
5606 return;
5607 }
5608 }
5609 else
5610 /* Default to v:register */
5611 strregname = get_vim_var_str(VV_REG);
5612
5613 regname = (strregname == NULL ? '"' : *strregname);
5614 if (regname == 0)
5615 regname = '"';
5616
5617 buf[0] = NUL;
5618 buf[1] = NUL;
5619 switch (get_reg_type(regname, &reglen))
5620 {
5621 case MLINE: buf[0] = 'V'; break;
5622 case MCHAR: buf[0] = 'v'; break;
5623 case MBLOCK:
5624 buf[0] = Ctrl_V;
5625 sprintf((char *)buf + 1, "%ld", reglen + 1);
5626 break;
5627 }
5628 rettv->v_type = VAR_STRING;
5629 rettv->vval.v_string = vim_strsave(buf);
5630}
5631
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005632/*
5633 * Returns information (variables, options, etc.) about a tab page
5634 * as a dictionary.
5635 */
5636 static dict_T *
5637get_tabpage_info(tabpage_T *tp, int tp_idx)
5638{
5639 win_T *wp;
5640 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005641 list_T *l;
5642
5643 dict = dict_alloc();
5644 if (dict == NULL)
5645 return NULL;
5646
Bram Moolenaare0be1672018-07-08 16:50:37 +02005647 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005648
5649 l = list_alloc();
5650 if (l != NULL)
5651 {
5652 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5653 wp; wp = wp->w_next)
5654 list_append_number(l, (varnumber_T)wp->w_id);
5655 dict_add_list(dict, "windows", l);
5656 }
5657
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005658 /* Make a reference to tabpage variables */
5659 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005660
5661 return dict;
5662}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005663
5664/*
5665 * "gettabinfo()" function
5666 */
5667 static void
5668f_gettabinfo(typval_T *argvars, typval_T *rettv)
5669{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005670 tabpage_T *tp, *tparg = NULL;
5671 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005672 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005673
5674 if (rettv_list_alloc(rettv) != OK)
5675 return;
5676
5677 if (argvars[0].v_type != VAR_UNKNOWN)
5678 {
5679 /* Information about one tab page */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005680 tparg = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005681 if (tparg == NULL)
5682 return;
5683 }
5684
5685 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005686 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005687 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005688 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005689 if (tparg != NULL && tp != tparg)
5690 continue;
5691 d = get_tabpage_info(tp, tpnr);
5692 if (d != NULL)
5693 list_append_dict(rettv->vval.v_list, d);
5694 if (tparg != NULL)
5695 return;
5696 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005697}
5698
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005699/*
5700 * "gettabvar()" function
5701 */
5702 static void
5703f_gettabvar(typval_T *argvars, typval_T *rettv)
5704{
5705 win_T *oldcurwin;
5706 tabpage_T *tp, *oldtabpage;
5707 dictitem_T *v;
5708 char_u *varname;
5709 int done = FALSE;
5710
5711 rettv->v_type = VAR_STRING;
5712 rettv->vval.v_string = NULL;
5713
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005714 varname = tv_get_string_chk(&argvars[1]);
5715 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005716 if (tp != NULL && varname != NULL)
5717 {
5718 /* Set tp to be our tabpage, temporarily. Also set the window to the
5719 * first window in the tabpage, otherwise the window is not valid. */
5720 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005721 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5722 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005723 {
5724 /* look up the variable */
5725 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5726 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5727 if (v != NULL)
5728 {
5729 copy_tv(&v->di_tv, rettv);
5730 done = TRUE;
5731 }
5732 }
5733
5734 /* restore previous notion of curwin */
5735 restore_win(oldcurwin, oldtabpage, TRUE);
5736 }
5737
5738 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5739 copy_tv(&argvars[2], rettv);
5740}
5741
5742/*
5743 * "gettabwinvar()" function
5744 */
5745 static void
5746f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5747{
5748 getwinvar(argvars, rettv, 1);
5749}
5750
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005751/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01005752 * "gettagstack()" function
5753 */
5754 static void
5755f_gettagstack(typval_T *argvars, typval_T *rettv)
5756{
5757 win_T *wp = curwin; // default is current window
5758
5759 if (rettv_dict_alloc(rettv) != OK)
5760 return;
5761
5762 if (argvars[0].v_type != VAR_UNKNOWN)
5763 {
5764 wp = find_win_by_nr_or_id(&argvars[0]);
5765 if (wp == NULL)
5766 return;
5767 }
5768
5769 get_tagstack(wp, rettv->vval.v_dict);
5770}
5771
5772/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005773 * Returns information about a window as a dictionary.
5774 */
5775 static dict_T *
5776get_win_info(win_T *wp, short tpnr, short winnr)
5777{
5778 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005779
5780 dict = dict_alloc();
5781 if (dict == NULL)
5782 return NULL;
5783
Bram Moolenaare0be1672018-07-08 16:50:37 +02005784 dict_add_number(dict, "tabnr", tpnr);
5785 dict_add_number(dict, "winnr", winnr);
5786 dict_add_number(dict, "winid", wp->w_id);
5787 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005788 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005789#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02005790 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005791#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005792 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005793 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005794 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005795
Bram Moolenaar69905d12017-08-13 18:14:47 +02005796#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02005797 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02005798#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005799#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02005800 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
5801 dict_add_number(dict, "loclist",
5802 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02005803#endif
5804
Bram Moolenaar30567352016-08-27 21:25:44 +02005805 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005806 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005807
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005808 return dict;
5809}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005810
5811/*
5812 * "getwininfo()" function
5813 */
5814 static void
5815f_getwininfo(typval_T *argvars, typval_T *rettv)
5816{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005817 tabpage_T *tp;
5818 win_T *wp = NULL, *wparg = NULL;
5819 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005820 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005821
5822 if (rettv_list_alloc(rettv) != OK)
5823 return;
5824
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005825 if (argvars[0].v_type != VAR_UNKNOWN)
5826 {
5827 wparg = win_id2wp(argvars);
5828 if (wparg == NULL)
5829 return;
5830 }
5831
5832 /* Collect information about either all the windows across all the tab
5833 * pages or one particular window.
5834 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005835 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005836 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005837 tabnr++;
5838 winnr = 0;
5839 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005840 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005841 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005842 if (wparg != NULL && wp != wparg)
5843 continue;
5844 d = get_win_info(wp, tabnr, winnr);
5845 if (d != NULL)
5846 list_append_dict(rettv->vval.v_list, d);
5847 if (wparg != NULL)
5848 /* found information about a specific window */
5849 return;
5850 }
5851 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005852}
5853
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005854/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005855 * "win_findbuf()" function
5856 */
5857 static void
5858f_win_findbuf(typval_T *argvars, typval_T *rettv)
5859{
5860 if (rettv_list_alloc(rettv) != FAIL)
5861 win_findbuf(argvars, rettv->vval.v_list);
5862}
5863
5864/*
5865 * "win_getid()" function
5866 */
5867 static void
5868f_win_getid(typval_T *argvars, typval_T *rettv)
5869{
5870 rettv->vval.v_number = win_getid(argvars);
5871}
5872
5873/*
5874 * "win_gotoid()" function
5875 */
5876 static void
5877f_win_gotoid(typval_T *argvars, typval_T *rettv)
5878{
5879 rettv->vval.v_number = win_gotoid(argvars);
5880}
5881
5882/*
5883 * "win_id2tabwin()" function
5884 */
5885 static void
5886f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5887{
5888 if (rettv_list_alloc(rettv) != FAIL)
5889 win_id2tabwin(argvars, rettv->vval.v_list);
5890}
5891
5892/*
5893 * "win_id2win()" function
5894 */
5895 static void
5896f_win_id2win(typval_T *argvars, typval_T *rettv)
5897{
5898 rettv->vval.v_number = win_id2win(argvars);
5899}
5900
5901/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005902 * "win_screenpos()" function
5903 */
5904 static void
5905f_win_screenpos(typval_T *argvars, typval_T *rettv)
5906{
5907 win_T *wp;
5908
5909 if (rettv_list_alloc(rettv) == FAIL)
5910 return;
5911
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005912 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005913 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5914 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5915}
5916
5917/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005918 * "getwinpos({timeout})" function
5919 */
5920 static void
5921f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
5922{
5923 int x = -1;
5924 int y = -1;
5925
5926 if (rettv_list_alloc(rettv) == FAIL)
5927 return;
5928#ifdef FEAT_GUI
5929 if (gui.in_use)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01005930 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005931# if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5932 else
5933# endif
5934#endif
5935#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5936 {
5937 varnumber_T timeout = 100;
5938
5939 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005940 timeout = tv_get_number(&argvars[0]);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005941 term_get_winpos(&x, &y, timeout);
5942 }
5943#endif
5944 list_append_number(rettv->vval.v_list, (varnumber_T)x);
5945 list_append_number(rettv->vval.v_list, (varnumber_T)y);
5946}
5947
5948
5949/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005950 * "getwinposx()" function
5951 */
5952 static void
5953f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5954{
5955 rettv->vval.v_number = -1;
5956#ifdef FEAT_GUI
5957 if (gui.in_use)
5958 {
5959 int x, y;
5960
5961 if (gui_mch_get_winpos(&x, &y) == OK)
5962 rettv->vval.v_number = x;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005963 return;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005964 }
5965#endif
5966#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5967 {
5968 int x, y;
5969
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005970 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005971 rettv->vval.v_number = x;
5972 }
5973#endif
5974}
5975
5976/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005977 * "getwinposy()" function
5978 */
5979 static void
5980f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5981{
5982 rettv->vval.v_number = -1;
5983#ifdef FEAT_GUI
5984 if (gui.in_use)
5985 {
5986 int x, y;
5987
5988 if (gui_mch_get_winpos(&x, &y) == OK)
5989 rettv->vval.v_number = y;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005990 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005991 }
5992#endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005993#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5994 {
5995 int x, y;
5996
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005997 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005998 rettv->vval.v_number = y;
5999 }
6000#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006001}
6002
6003/*
6004 * "getwinvar()" function
6005 */
6006 static void
6007f_getwinvar(typval_T *argvars, typval_T *rettv)
6008{
6009 getwinvar(argvars, rettv, 0);
6010}
6011
6012/*
6013 * "glob()" function
6014 */
6015 static void
6016f_glob(typval_T *argvars, typval_T *rettv)
6017{
6018 int options = WILD_SILENT|WILD_USE_NL;
6019 expand_T xpc;
6020 int error = FALSE;
6021
6022 /* When the optional second argument is non-zero, don't remove matches
6023 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6024 rettv->v_type = VAR_STRING;
6025 if (argvars[1].v_type != VAR_UNKNOWN)
6026 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006027 if (tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006028 options |= WILD_KEEP_ALL;
6029 if (argvars[2].v_type != VAR_UNKNOWN)
6030 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006031 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006032 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006033 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006034 }
6035 if (argvars[3].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006036 && tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006037 options |= WILD_ALLLINKS;
6038 }
6039 }
6040 if (!error)
6041 {
6042 ExpandInit(&xpc);
6043 xpc.xp_context = EXPAND_FILES;
6044 if (p_wic)
6045 options += WILD_ICASE;
6046 if (rettv->v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006047 rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006048 NULL, options, WILD_ALL);
6049 else if (rettv_list_alloc(rettv) != FAIL)
6050 {
6051 int i;
6052
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006053 ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006054 NULL, options, WILD_ALL_KEEP);
6055 for (i = 0; i < xpc.xp_numfiles; i++)
6056 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
6057
6058 ExpandCleanup(&xpc);
6059 }
6060 }
6061 else
6062 rettv->vval.v_string = NULL;
6063}
6064
6065/*
6066 * "globpath()" function
6067 */
6068 static void
6069f_globpath(typval_T *argvars, typval_T *rettv)
6070{
6071 int flags = 0;
6072 char_u buf1[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006073 char_u *file = tv_get_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006074 int error = FALSE;
6075 garray_T ga;
6076 int i;
6077
6078 /* When the optional second argument is non-zero, don't remove matches
6079 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6080 rettv->v_type = VAR_STRING;
6081 if (argvars[2].v_type != VAR_UNKNOWN)
6082 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006083 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006084 flags |= WILD_KEEP_ALL;
6085 if (argvars[3].v_type != VAR_UNKNOWN)
6086 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006087 if (tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006088 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006089 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006090 }
6091 if (argvars[4].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006092 && tv_get_number_chk(&argvars[4], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006093 flags |= WILD_ALLLINKS;
6094 }
6095 }
6096 if (file != NULL && !error)
6097 {
6098 ga_init2(&ga, (int)sizeof(char_u *), 10);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006099 globpath(tv_get_string(&argvars[0]), file, &ga, flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006100 if (rettv->v_type == VAR_STRING)
6101 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
6102 else if (rettv_list_alloc(rettv) != FAIL)
6103 for (i = 0; i < ga.ga_len; ++i)
6104 list_append_string(rettv->vval.v_list,
6105 ((char_u **)(ga.ga_data))[i], -1);
6106 ga_clear_strings(&ga);
6107 }
6108 else
6109 rettv->vval.v_string = NULL;
6110}
6111
6112/*
6113 * "glob2regpat()" function
6114 */
6115 static void
6116f_glob2regpat(typval_T *argvars, typval_T *rettv)
6117{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006118 char_u *pat = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006119
6120 rettv->v_type = VAR_STRING;
6121 rettv->vval.v_string = (pat == NULL)
6122 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
6123}
6124
6125/* for VIM_VERSION_ defines */
6126#include "version.h"
6127
6128/*
6129 * "has()" function
6130 */
6131 static void
6132f_has(typval_T *argvars, typval_T *rettv)
6133{
6134 int i;
6135 char_u *name;
6136 int n = FALSE;
6137 static char *(has_list[]) =
6138 {
6139#ifdef AMIGA
6140 "amiga",
6141# ifdef FEAT_ARP
6142 "arp",
6143# endif
6144#endif
6145#ifdef __BEOS__
6146 "beos",
6147#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02006148#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01006149 "mac", /* Mac OS X (and, once, Mac OS Classic) */
6150 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02006151# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01006152 "macunix", /* Mac OS X, with the darwin feature */
6153 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02006154# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006155#endif
6156#ifdef __QNX__
6157 "qnx",
6158#endif
6159#ifdef UNIX
6160 "unix",
6161#endif
6162#ifdef VMS
6163 "vms",
6164#endif
6165#ifdef WIN32
6166 "win32",
6167#endif
6168#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
6169 "win32unix",
6170#endif
6171#if defined(WIN64) || defined(_WIN64)
6172 "win64",
6173#endif
6174#ifdef EBCDIC
6175 "ebcdic",
6176#endif
6177#ifndef CASE_INSENSITIVE_FILENAME
6178 "fname_case",
6179#endif
6180#ifdef HAVE_ACL
6181 "acl",
6182#endif
6183#ifdef FEAT_ARABIC
6184 "arabic",
6185#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006186 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006187#ifdef FEAT_AUTOCHDIR
6188 "autochdir",
6189#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006190#ifdef FEAT_AUTOSERVERNAME
6191 "autoservername",
6192#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006193#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006194 "balloon_eval",
6195# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
6196 "balloon_multiline",
6197# endif
6198#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006199#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006200 "balloon_eval_term",
6201#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006202#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6203 "builtin_terms",
6204# ifdef ALL_BUILTIN_TCAPS
6205 "all_builtin_terms",
6206# endif
6207#endif
6208#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
6209 || defined(FEAT_GUI_W32) \
6210 || defined(FEAT_GUI_MOTIF))
6211 "browsefilter",
6212#endif
6213#ifdef FEAT_BYTEOFF
6214 "byte_offset",
6215#endif
6216#ifdef FEAT_JOB_CHANNEL
6217 "channel",
6218#endif
6219#ifdef FEAT_CINDENT
6220 "cindent",
6221#endif
6222#ifdef FEAT_CLIENTSERVER
6223 "clientserver",
6224#endif
6225#ifdef FEAT_CLIPBOARD
6226 "clipboard",
6227#endif
6228#ifdef FEAT_CMDL_COMPL
6229 "cmdline_compl",
6230#endif
6231#ifdef FEAT_CMDHIST
6232 "cmdline_hist",
6233#endif
6234#ifdef FEAT_COMMENTS
6235 "comments",
6236#endif
6237#ifdef FEAT_CONCEAL
6238 "conceal",
6239#endif
6240#ifdef FEAT_CRYPT
6241 "cryptv",
6242 "crypt-blowfish",
6243 "crypt-blowfish2",
6244#endif
6245#ifdef FEAT_CSCOPE
6246 "cscope",
6247#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006248 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006249#ifdef CURSOR_SHAPE
6250 "cursorshape",
6251#endif
6252#ifdef DEBUG
6253 "debug",
6254#endif
6255#ifdef FEAT_CON_DIALOG
6256 "dialog_con",
6257#endif
6258#ifdef FEAT_GUI_DIALOG
6259 "dialog_gui",
6260#endif
6261#ifdef FEAT_DIFF
6262 "diff",
6263#endif
6264#ifdef FEAT_DIGRAPHS
6265 "digraphs",
6266#endif
6267#ifdef FEAT_DIRECTX
6268 "directx",
6269#endif
6270#ifdef FEAT_DND
6271 "dnd",
6272#endif
6273#ifdef FEAT_EMACS_TAGS
6274 "emacs_tags",
6275#endif
6276 "eval", /* always present, of course! */
6277 "ex_extra", /* graduated feature */
6278#ifdef FEAT_SEARCH_EXTRA
6279 "extra_search",
6280#endif
6281#ifdef FEAT_FKMAP
6282 "farsi",
6283#endif
6284#ifdef FEAT_SEARCHPATH
6285 "file_in_path",
6286#endif
6287#ifdef FEAT_FILTERPIPE
6288 "filterpipe",
6289#endif
6290#ifdef FEAT_FIND_ID
6291 "find_in_path",
6292#endif
6293#ifdef FEAT_FLOAT
6294 "float",
6295#endif
6296#ifdef FEAT_FOLDING
6297 "folding",
6298#endif
6299#ifdef FEAT_FOOTER
6300 "footer",
6301#endif
6302#if !defined(USE_SYSTEM) && defined(UNIX)
6303 "fork",
6304#endif
6305#ifdef FEAT_GETTEXT
6306 "gettext",
6307#endif
6308#ifdef FEAT_GUI
6309 "gui",
6310#endif
6311#ifdef FEAT_GUI_ATHENA
6312# ifdef FEAT_GUI_NEXTAW
6313 "gui_neXtaw",
6314# else
6315 "gui_athena",
6316# endif
6317#endif
6318#ifdef FEAT_GUI_GTK
6319 "gui_gtk",
6320# ifdef USE_GTK3
6321 "gui_gtk3",
6322# else
6323 "gui_gtk2",
6324# endif
6325#endif
6326#ifdef FEAT_GUI_GNOME
6327 "gui_gnome",
6328#endif
6329#ifdef FEAT_GUI_MAC
6330 "gui_mac",
6331#endif
6332#ifdef FEAT_GUI_MOTIF
6333 "gui_motif",
6334#endif
6335#ifdef FEAT_GUI_PHOTON
6336 "gui_photon",
6337#endif
6338#ifdef FEAT_GUI_W32
6339 "gui_win32",
6340#endif
6341#ifdef FEAT_HANGULIN
6342 "hangul_input",
6343#endif
6344#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6345 "iconv",
6346#endif
6347#ifdef FEAT_INS_EXPAND
6348 "insert_expand",
6349#endif
6350#ifdef FEAT_JOB_CHANNEL
6351 "job",
6352#endif
6353#ifdef FEAT_JUMPLIST
6354 "jumplist",
6355#endif
6356#ifdef FEAT_KEYMAP
6357 "keymap",
6358#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006359 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006360#ifdef FEAT_LANGMAP
6361 "langmap",
6362#endif
6363#ifdef FEAT_LIBCALL
6364 "libcall",
6365#endif
6366#ifdef FEAT_LINEBREAK
6367 "linebreak",
6368#endif
6369#ifdef FEAT_LISP
6370 "lispindent",
6371#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006372 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006373#ifdef FEAT_LOCALMAP
6374 "localmap",
6375#endif
6376#ifdef FEAT_LUA
6377# ifndef DYNAMIC_LUA
6378 "lua",
6379# endif
6380#endif
6381#ifdef FEAT_MENU
6382 "menu",
6383#endif
6384#ifdef FEAT_SESSION
6385 "mksession",
6386#endif
6387#ifdef FEAT_MODIFY_FNAME
6388 "modify_fname",
6389#endif
6390#ifdef FEAT_MOUSE
6391 "mouse",
6392#endif
6393#ifdef FEAT_MOUSESHAPE
6394 "mouseshape",
6395#endif
6396#if defined(UNIX) || defined(VMS)
6397# ifdef FEAT_MOUSE_DEC
6398 "mouse_dec",
6399# endif
6400# ifdef FEAT_MOUSE_GPM
6401 "mouse_gpm",
6402# endif
6403# ifdef FEAT_MOUSE_JSB
6404 "mouse_jsbterm",
6405# endif
6406# ifdef FEAT_MOUSE_NET
6407 "mouse_netterm",
6408# endif
6409# ifdef FEAT_MOUSE_PTERM
6410 "mouse_pterm",
6411# endif
6412# ifdef FEAT_MOUSE_SGR
6413 "mouse_sgr",
6414# endif
6415# ifdef FEAT_SYSMOUSE
6416 "mouse_sysmouse",
6417# endif
6418# ifdef FEAT_MOUSE_URXVT
6419 "mouse_urxvt",
6420# endif
6421# ifdef FEAT_MOUSE_XTERM
6422 "mouse_xterm",
6423# endif
6424#endif
6425#ifdef FEAT_MBYTE
6426 "multi_byte",
6427#endif
6428#ifdef FEAT_MBYTE_IME
6429 "multi_byte_ime",
6430#endif
6431#ifdef FEAT_MULTI_LANG
6432 "multi_lang",
6433#endif
6434#ifdef FEAT_MZSCHEME
6435#ifndef DYNAMIC_MZSCHEME
6436 "mzscheme",
6437#endif
6438#endif
6439#ifdef FEAT_NUM64
6440 "num64",
6441#endif
6442#ifdef FEAT_OLE
6443 "ole",
6444#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006445#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006446 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006447#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006448#ifdef FEAT_PATH_EXTRA
6449 "path_extra",
6450#endif
6451#ifdef FEAT_PERL
6452#ifndef DYNAMIC_PERL
6453 "perl",
6454#endif
6455#endif
6456#ifdef FEAT_PERSISTENT_UNDO
6457 "persistent_undo",
6458#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006459#if defined(FEAT_PYTHON)
6460 "python_compiled",
6461# if defined(DYNAMIC_PYTHON)
6462 "python_dynamic",
6463# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006464 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006465 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006466# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006467#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006468#if defined(FEAT_PYTHON3)
6469 "python3_compiled",
6470# if defined(DYNAMIC_PYTHON3)
6471 "python3_dynamic",
6472# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006473 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006474 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006475# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006476#endif
6477#ifdef FEAT_POSTSCRIPT
6478 "postscript",
6479#endif
6480#ifdef FEAT_PRINTER
6481 "printer",
6482#endif
6483#ifdef FEAT_PROFILE
6484 "profile",
6485#endif
6486#ifdef FEAT_RELTIME
6487 "reltime",
6488#endif
6489#ifdef FEAT_QUICKFIX
6490 "quickfix",
6491#endif
6492#ifdef FEAT_RIGHTLEFT
6493 "rightleft",
6494#endif
6495#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6496 "ruby",
6497#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006498 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006499#ifdef FEAT_CMDL_INFO
6500 "showcmd",
6501 "cmdline_info",
6502#endif
6503#ifdef FEAT_SIGNS
6504 "signs",
6505#endif
6506#ifdef FEAT_SMARTINDENT
6507 "smartindent",
6508#endif
6509#ifdef STARTUPTIME
6510 "startuptime",
6511#endif
6512#ifdef FEAT_STL_OPT
6513 "statusline",
6514#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006515#ifdef FEAT_NETBEANS_INTG
6516 "netbeans_intg",
6517#endif
6518#ifdef FEAT_SPELL
6519 "spell",
6520#endif
6521#ifdef FEAT_SYN_HL
6522 "syntax",
6523#endif
6524#if defined(USE_SYSTEM) || !defined(UNIX)
6525 "system",
6526#endif
6527#ifdef FEAT_TAG_BINS
6528 "tag_binary",
6529#endif
6530#ifdef FEAT_TAG_OLDSTATIC
6531 "tag_old_static",
6532#endif
6533#ifdef FEAT_TAG_ANYWHITE
6534 "tag_any_white",
6535#endif
6536#ifdef FEAT_TCL
6537# ifndef DYNAMIC_TCL
6538 "tcl",
6539# endif
6540#endif
6541#ifdef FEAT_TERMGUICOLORS
6542 "termguicolors",
6543#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006544#if defined(FEAT_TERMINAL) && !defined(WIN3264)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006545 "terminal",
6546#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006547#ifdef TERMINFO
6548 "terminfo",
6549#endif
6550#ifdef FEAT_TERMRESPONSE
6551 "termresponse",
6552#endif
6553#ifdef FEAT_TEXTOBJ
6554 "textobjects",
6555#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006556#ifdef FEAT_TEXT_PROP
6557 "textprop",
6558#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006559#ifdef HAVE_TGETENT
6560 "tgetent",
6561#endif
6562#ifdef FEAT_TIMERS
6563 "timers",
6564#endif
6565#ifdef FEAT_TITLE
6566 "title",
6567#endif
6568#ifdef FEAT_TOOLBAR
6569 "toolbar",
6570#endif
6571#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6572 "unnamedplus",
6573#endif
6574#ifdef FEAT_USR_CMDS
6575 "user-commands", /* was accidentally included in 5.4 */
6576 "user_commands",
6577#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006578#ifdef FEAT_VARTABS
6579 "vartabs",
6580#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006581#ifdef FEAT_VIMINFO
6582 "viminfo",
6583#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006584 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006585#ifdef FEAT_VIRTUALEDIT
6586 "virtualedit",
6587#endif
6588 "visual",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006589 "visualextra",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006590 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006591#ifdef FEAT_VTP
6592 "vtp",
6593#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006594#ifdef FEAT_WILDIGN
6595 "wildignore",
6596#endif
6597#ifdef FEAT_WILDMENU
6598 "wildmenu",
6599#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006600 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006601#ifdef FEAT_WAK
6602 "winaltkeys",
6603#endif
6604#ifdef FEAT_WRITEBACKUP
6605 "writebackup",
6606#endif
6607#ifdef FEAT_XIM
6608 "xim",
6609#endif
6610#ifdef FEAT_XFONTSET
6611 "xfontset",
6612#endif
6613#ifdef FEAT_XPM_W32
6614 "xpm",
6615 "xpm_w32", /* for backward compatibility */
6616#else
6617# if defined(HAVE_XPM)
6618 "xpm",
6619# endif
6620#endif
6621#ifdef USE_XSMP
6622 "xsmp",
6623#endif
6624#ifdef USE_XSMP_INTERACT
6625 "xsmp_interact",
6626#endif
6627#ifdef FEAT_XCLIPBOARD
6628 "xterm_clipboard",
6629#endif
6630#ifdef FEAT_XTERM_SAVE
6631 "xterm_save",
6632#endif
6633#if defined(UNIX) && defined(FEAT_X11)
6634 "X11",
6635#endif
6636 NULL
6637 };
6638
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006639 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006640 for (i = 0; has_list[i] != NULL; ++i)
6641 if (STRICMP(name, has_list[i]) == 0)
6642 {
6643 n = TRUE;
6644 break;
6645 }
6646
6647 if (n == FALSE)
6648 {
6649 if (STRNICMP(name, "patch", 5) == 0)
6650 {
6651 if (name[5] == '-'
6652 && STRLEN(name) >= 11
6653 && vim_isdigit(name[6])
6654 && vim_isdigit(name[8])
6655 && vim_isdigit(name[10]))
6656 {
6657 int major = atoi((char *)name + 6);
6658 int minor = atoi((char *)name + 8);
6659
6660 /* Expect "patch-9.9.01234". */
6661 n = (major < VIM_VERSION_MAJOR
6662 || (major == VIM_VERSION_MAJOR
6663 && (minor < VIM_VERSION_MINOR
6664 || (minor == VIM_VERSION_MINOR
6665 && has_patch(atoi((char *)name + 10))))));
6666 }
6667 else
6668 n = has_patch(atoi((char *)name + 5));
6669 }
6670 else if (STRICMP(name, "vim_starting") == 0)
6671 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006672 else if (STRICMP(name, "ttyin") == 0)
6673 n = mch_input_isatty();
6674 else if (STRICMP(name, "ttyout") == 0)
6675 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006676#ifdef FEAT_MBYTE
6677 else if (STRICMP(name, "multi_byte_encoding") == 0)
6678 n = has_mbyte;
6679#endif
6680#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
6681 else if (STRICMP(name, "balloon_multiline") == 0)
6682 n = multiline_balloon_available();
6683#endif
6684#ifdef DYNAMIC_TCL
6685 else if (STRICMP(name, "tcl") == 0)
6686 n = tcl_enabled(FALSE);
6687#endif
6688#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6689 else if (STRICMP(name, "iconv") == 0)
6690 n = iconv_enabled(FALSE);
6691#endif
6692#ifdef DYNAMIC_LUA
6693 else if (STRICMP(name, "lua") == 0)
6694 n = lua_enabled(FALSE);
6695#endif
6696#ifdef DYNAMIC_MZSCHEME
6697 else if (STRICMP(name, "mzscheme") == 0)
6698 n = mzscheme_enabled(FALSE);
6699#endif
6700#ifdef DYNAMIC_RUBY
6701 else if (STRICMP(name, "ruby") == 0)
6702 n = ruby_enabled(FALSE);
6703#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006704#ifdef DYNAMIC_PYTHON
6705 else if (STRICMP(name, "python") == 0)
6706 n = python_enabled(FALSE);
6707#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006708#ifdef DYNAMIC_PYTHON3
6709 else if (STRICMP(name, "python3") == 0)
6710 n = python3_enabled(FALSE);
6711#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006712#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6713 else if (STRICMP(name, "pythonx") == 0)
6714 {
6715# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6716 if (p_pyx == 0)
6717 n = python3_enabled(FALSE) || python_enabled(FALSE);
6718 else if (p_pyx == 3)
6719 n = python3_enabled(FALSE);
6720 else if (p_pyx == 2)
6721 n = python_enabled(FALSE);
6722# elif defined(DYNAMIC_PYTHON)
6723 n = python_enabled(FALSE);
6724# elif defined(DYNAMIC_PYTHON3)
6725 n = python3_enabled(FALSE);
6726# endif
6727 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006728#endif
6729#ifdef DYNAMIC_PERL
6730 else if (STRICMP(name, "perl") == 0)
6731 n = perl_enabled(FALSE);
6732#endif
6733#ifdef FEAT_GUI
6734 else if (STRICMP(name, "gui_running") == 0)
6735 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006736# ifdef FEAT_BROWSE
6737 else if (STRICMP(name, "browse") == 0)
6738 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6739# endif
6740#endif
6741#ifdef FEAT_SYN_HL
6742 else if (STRICMP(name, "syntax_items") == 0)
6743 n = syntax_present(curwin);
6744#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006745#ifdef FEAT_VTP
6746 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02006747 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006748#endif
6749#ifdef FEAT_NETBEANS_INTG
6750 else if (STRICMP(name, "netbeans_enabled") == 0)
6751 n = netbeans_active();
6752#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006753#if defined(FEAT_TERMINAL) && defined(WIN3264)
6754 else if (STRICMP(name, "terminal") == 0)
6755 n = terminal_enabled();
6756#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006757 }
6758
6759 rettv->vval.v_number = n;
6760}
6761
6762/*
6763 * "has_key()" function
6764 */
6765 static void
6766f_has_key(typval_T *argvars, typval_T *rettv)
6767{
6768 if (argvars[0].v_type != VAR_DICT)
6769 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006770 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006771 return;
6772 }
6773 if (argvars[0].vval.v_dict == NULL)
6774 return;
6775
6776 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006777 tv_get_string(&argvars[1]), -1) != NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006778}
6779
6780/*
6781 * "haslocaldir()" function
6782 */
6783 static void
6784f_haslocaldir(typval_T *argvars, typval_T *rettv)
6785{
6786 win_T *wp = NULL;
6787
6788 wp = find_tabwin(&argvars[0], &argvars[1]);
6789 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6790}
6791
6792/*
6793 * "hasmapto()" function
6794 */
6795 static void
6796f_hasmapto(typval_T *argvars, typval_T *rettv)
6797{
6798 char_u *name;
6799 char_u *mode;
6800 char_u buf[NUMBUFLEN];
6801 int abbr = FALSE;
6802
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006803 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006804 if (argvars[1].v_type == VAR_UNKNOWN)
6805 mode = (char_u *)"nvo";
6806 else
6807 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006808 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006809 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006810 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006811 }
6812
6813 if (map_to_exists(name, mode, abbr))
6814 rettv->vval.v_number = TRUE;
6815 else
6816 rettv->vval.v_number = FALSE;
6817}
6818
6819/*
6820 * "histadd()" function
6821 */
6822 static void
6823f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6824{
6825#ifdef FEAT_CMDHIST
6826 int histype;
6827 char_u *str;
6828 char_u buf[NUMBUFLEN];
6829#endif
6830
6831 rettv->vval.v_number = FALSE;
6832 if (check_restricted() || check_secure())
6833 return;
6834#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006835 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006836 histype = str != NULL ? get_histtype(str) : -1;
6837 if (histype >= 0)
6838 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006839 str = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006840 if (*str != NUL)
6841 {
6842 init_history();
6843 add_to_history(histype, str, FALSE, NUL);
6844 rettv->vval.v_number = TRUE;
6845 return;
6846 }
6847 }
6848#endif
6849}
6850
6851/*
6852 * "histdel()" function
6853 */
6854 static void
6855f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6856{
6857#ifdef FEAT_CMDHIST
6858 int n;
6859 char_u buf[NUMBUFLEN];
6860 char_u *str;
6861
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006862 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006863 if (str == NULL)
6864 n = 0;
6865 else if (argvars[1].v_type == VAR_UNKNOWN)
6866 /* only one argument: clear entire history */
6867 n = clr_history(get_histtype(str));
6868 else if (argvars[1].v_type == VAR_NUMBER)
6869 /* index given: remove that entry */
6870 n = del_history_idx(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006871 (int)tv_get_number(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006872 else
6873 /* string given: remove all matching entries */
6874 n = del_history_entry(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006875 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006876 rettv->vval.v_number = n;
6877#endif
6878}
6879
6880/*
6881 * "histget()" function
6882 */
6883 static void
6884f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6885{
6886#ifdef FEAT_CMDHIST
6887 int type;
6888 int idx;
6889 char_u *str;
6890
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006891 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006892 if (str == NULL)
6893 rettv->vval.v_string = NULL;
6894 else
6895 {
6896 type = get_histtype(str);
6897 if (argvars[1].v_type == VAR_UNKNOWN)
6898 idx = get_history_idx(type);
6899 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006900 idx = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006901 /* -1 on type error */
6902 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6903 }
6904#else
6905 rettv->vval.v_string = NULL;
6906#endif
6907 rettv->v_type = VAR_STRING;
6908}
6909
6910/*
6911 * "histnr()" function
6912 */
6913 static void
6914f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6915{
6916 int i;
6917
6918#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006919 char_u *history = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006920
6921 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6922 if (i >= HIST_CMD && i < HIST_COUNT)
6923 i = get_history_idx(i);
6924 else
6925#endif
6926 i = -1;
6927 rettv->vval.v_number = i;
6928}
6929
6930/*
6931 * "highlightID(name)" function
6932 */
6933 static void
6934f_hlID(typval_T *argvars, typval_T *rettv)
6935{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006936 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006937}
6938
6939/*
6940 * "highlight_exists()" function
6941 */
6942 static void
6943f_hlexists(typval_T *argvars, typval_T *rettv)
6944{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006945 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006946}
6947
6948/*
6949 * "hostname()" function
6950 */
6951 static void
6952f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6953{
6954 char_u hostname[256];
6955
6956 mch_get_host_name(hostname, 256);
6957 rettv->v_type = VAR_STRING;
6958 rettv->vval.v_string = vim_strsave(hostname);
6959}
6960
6961/*
6962 * iconv() function
6963 */
6964 static void
6965f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6966{
6967#ifdef FEAT_MBYTE
6968 char_u buf1[NUMBUFLEN];
6969 char_u buf2[NUMBUFLEN];
6970 char_u *from, *to, *str;
6971 vimconv_T vimconv;
6972#endif
6973
6974 rettv->v_type = VAR_STRING;
6975 rettv->vval.v_string = NULL;
6976
6977#ifdef FEAT_MBYTE
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006978 str = tv_get_string(&argvars[0]);
6979 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
6980 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006981 vimconv.vc_type = CONV_NONE;
6982 convert_setup(&vimconv, from, to);
6983
6984 /* If the encodings are equal, no conversion needed. */
6985 if (vimconv.vc_type == CONV_NONE)
6986 rettv->vval.v_string = vim_strsave(str);
6987 else
6988 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6989
6990 convert_setup(&vimconv, NULL, NULL);
6991 vim_free(from);
6992 vim_free(to);
6993#endif
6994}
6995
6996/*
6997 * "indent()" function
6998 */
6999 static void
7000f_indent(typval_T *argvars, typval_T *rettv)
7001{
7002 linenr_T lnum;
7003
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007004 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007005 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7006 rettv->vval.v_number = get_indent_lnum(lnum);
7007 else
7008 rettv->vval.v_number = -1;
7009}
7010
7011/*
7012 * "index()" function
7013 */
7014 static void
7015f_index(typval_T *argvars, typval_T *rettv)
7016{
7017 list_T *l;
7018 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007019 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007020 long idx = 0;
7021 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007022 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007023
7024 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007025 if (argvars[0].v_type == VAR_BLOB)
7026 {
7027 typval_T tv;
7028 int start = 0;
7029
7030 if (argvars[2].v_type != VAR_UNKNOWN)
7031 {
7032 start = tv_get_number_chk(&argvars[2], &error);
7033 if (error)
7034 return;
7035 }
7036 b = argvars[0].vval.v_blob;
7037 if (b == NULL)
7038 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01007039 if (start < 0)
7040 {
7041 start = blob_len(b) + start;
7042 if (start < 0)
7043 start = 0;
7044 }
7045
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007046 for (idx = start; idx < blob_len(b); ++idx)
7047 {
7048 tv.v_type = VAR_NUMBER;
7049 tv.vval.v_number = blob_get(b, idx);
7050 if (tv_equal(&tv, &argvars[1], ic, FALSE))
7051 {
7052 rettv->vval.v_number = idx;
7053 return;
7054 }
7055 }
7056 return;
7057 }
7058 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007059 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007060 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007061 return;
7062 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007063
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007064 l = argvars[0].vval.v_list;
7065 if (l != NULL)
7066 {
7067 item = l->lv_first;
7068 if (argvars[2].v_type != VAR_UNKNOWN)
7069 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007070 /* Start at specified item. Use the cached index that list_find()
7071 * sets, so that a negative number also works. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007072 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007073 idx = l->lv_idx;
7074 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007075 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007076 if (error)
7077 item = NULL;
7078 }
7079
7080 for ( ; item != NULL; item = item->li_next, ++idx)
7081 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
7082 {
7083 rettv->vval.v_number = idx;
7084 break;
7085 }
7086 }
7087}
7088
7089static int inputsecret_flag = 0;
7090
7091/*
7092 * "input()" function
7093 * Also handles inputsecret() when inputsecret is set.
7094 */
7095 static void
7096f_input(typval_T *argvars, typval_T *rettv)
7097{
7098 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
7099}
7100
7101/*
7102 * "inputdialog()" function
7103 */
7104 static void
7105f_inputdialog(typval_T *argvars, typval_T *rettv)
7106{
7107#if defined(FEAT_GUI_TEXTDIALOG)
7108 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
7109 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
7110 {
7111 char_u *message;
7112 char_u buf[NUMBUFLEN];
7113 char_u *defstr = (char_u *)"";
7114
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007115 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007116 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007117 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007118 vim_strncpy(IObuff, defstr, IOSIZE - 1);
7119 else
7120 IObuff[0] = NUL;
7121 if (message != NULL && defstr != NULL
7122 && do_dialog(VIM_QUESTION, NULL, message,
7123 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
7124 rettv->vval.v_string = vim_strsave(IObuff);
7125 else
7126 {
7127 if (message != NULL && defstr != NULL
7128 && argvars[1].v_type != VAR_UNKNOWN
7129 && argvars[2].v_type != VAR_UNKNOWN)
7130 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007131 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007132 else
7133 rettv->vval.v_string = NULL;
7134 }
7135 rettv->v_type = VAR_STRING;
7136 }
7137 else
7138#endif
7139 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
7140}
7141
7142/*
7143 * "inputlist()" function
7144 */
7145 static void
7146f_inputlist(typval_T *argvars, typval_T *rettv)
7147{
7148 listitem_T *li;
7149 int selected;
7150 int mouse_used;
7151
7152#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007153 /* While starting up, there is no place to enter text. When running tests
7154 * with --not-a-term we assume feedkeys() will be used. */
7155 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007156 return;
7157#endif
7158 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
7159 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007160 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007161 return;
7162 }
7163
7164 msg_start();
7165 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
7166 lines_left = Rows; /* avoid more prompt */
7167 msg_scroll = TRUE;
7168 msg_clr_eos();
7169
7170 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
7171 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007172 msg_puts(tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007173 msg_putchar('\n');
7174 }
7175
7176 /* Ask for choice. */
7177 selected = prompt_for_number(&mouse_used);
7178 if (mouse_used)
7179 selected -= lines_left;
7180
7181 rettv->vval.v_number = selected;
7182}
7183
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007184static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7185
7186/*
7187 * "inputrestore()" function
7188 */
7189 static void
7190f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7191{
7192 if (ga_userinput.ga_len > 0)
7193 {
7194 --ga_userinput.ga_len;
7195 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7196 + ga_userinput.ga_len);
7197 /* default return is zero == OK */
7198 }
7199 else if (p_verbose > 1)
7200 {
7201 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
7202 rettv->vval.v_number = 1; /* Failed */
7203 }
7204}
7205
7206/*
7207 * "inputsave()" function
7208 */
7209 static void
7210f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7211{
7212 /* Add an entry to the stack of typeahead storage. */
7213 if (ga_grow(&ga_userinput, 1) == OK)
7214 {
7215 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7216 + ga_userinput.ga_len);
7217 ++ga_userinput.ga_len;
7218 /* default return is zero == OK */
7219 }
7220 else
7221 rettv->vval.v_number = 1; /* Failed */
7222}
7223
7224/*
7225 * "inputsecret()" function
7226 */
7227 static void
7228f_inputsecret(typval_T *argvars, typval_T *rettv)
7229{
7230 ++cmdline_star;
7231 ++inputsecret_flag;
7232 f_input(argvars, rettv);
7233 --cmdline_star;
7234 --inputsecret_flag;
7235}
7236
7237/*
7238 * "insert()" function
7239 */
7240 static void
7241f_insert(typval_T *argvars, typval_T *rettv)
7242{
7243 long before = 0;
7244 listitem_T *item;
7245 list_T *l;
7246 int error = FALSE;
7247
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007248 if (argvars[0].v_type == VAR_BLOB)
7249 {
7250 int val, len;
7251 char_u *p;
7252
7253 len = blob_len(argvars[0].vval.v_blob);
7254 if (argvars[2].v_type != VAR_UNKNOWN)
7255 {
7256 before = (long)tv_get_number_chk(&argvars[2], &error);
7257 if (error)
7258 return; // type error; errmsg already given
7259 if (before < 0 || before > len)
7260 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007261 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007262 return;
7263 }
7264 }
7265 val = tv_get_number_chk(&argvars[1], &error);
7266 if (error)
7267 return;
7268 if (val < 0 || val > 255)
7269 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007270 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007271 return;
7272 }
7273
7274 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
7275 return;
7276 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
7277 mch_memmove(p + before + 1, p + before, (size_t)len - before);
7278 *(p + before) = val;
7279 ++argvars[0].vval.v_blob->bv_ga.ga_len;
7280
7281 copy_tv(&argvars[0], rettv);
7282 }
7283 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007284 semsg(_(e_listarg), "insert()");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007285 else if ((l = argvars[0].vval.v_list) != NULL && !tv_check_lock(l->lv_lock,
7286 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007287 {
7288 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007289 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007290 if (error)
7291 return; /* type error; errmsg already given */
7292
7293 if (before == l->lv_len)
7294 item = NULL;
7295 else
7296 {
7297 item = list_find(l, before);
7298 if (item == NULL)
7299 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007300 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007301 l = NULL;
7302 }
7303 }
7304 if (l != NULL)
7305 {
7306 list_insert_tv(l, &argvars[1], item);
7307 copy_tv(&argvars[0], rettv);
7308 }
7309 }
7310}
7311
7312/*
7313 * "invert(expr)" function
7314 */
7315 static void
7316f_invert(typval_T *argvars, typval_T *rettv)
7317{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007318 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007319}
7320
7321/*
7322 * "isdirectory()" function
7323 */
7324 static void
7325f_isdirectory(typval_T *argvars, typval_T *rettv)
7326{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007327 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007328}
7329
7330/*
7331 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7332 * or it refers to a List or Dictionary that is locked.
7333 */
7334 static int
7335tv_islocked(typval_T *tv)
7336{
7337 return (tv->v_lock & VAR_LOCKED)
7338 || (tv->v_type == VAR_LIST
7339 && tv->vval.v_list != NULL
7340 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7341 || (tv->v_type == VAR_DICT
7342 && tv->vval.v_dict != NULL
7343 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7344}
7345
7346/*
7347 * "islocked()" function
7348 */
7349 static void
7350f_islocked(typval_T *argvars, typval_T *rettv)
7351{
7352 lval_T lv;
7353 char_u *end;
7354 dictitem_T *di;
7355
7356 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007357 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007358 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007359 if (end != NULL && lv.ll_name != NULL)
7360 {
7361 if (*end != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007362 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007363 else
7364 {
7365 if (lv.ll_tv == NULL)
7366 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007367 di = find_var(lv.ll_name, NULL, TRUE);
7368 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007369 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007370 /* Consider a variable locked when:
7371 * 1. the variable itself is locked
7372 * 2. the value of the variable is locked.
7373 * 3. the List or Dict value is locked.
7374 */
7375 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7376 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007377 }
7378 }
7379 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007380 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007381 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007382 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007383 else if (lv.ll_list != NULL)
7384 /* List item. */
7385 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7386 else
7387 /* Dictionary item. */
7388 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7389 }
7390 }
7391
7392 clear_lval(&lv);
7393}
7394
7395#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7396/*
7397 * "isnan()" function
7398 */
7399 static void
7400f_isnan(typval_T *argvars, typval_T *rettv)
7401{
7402 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7403 && isnan(argvars[0].vval.v_float);
7404}
7405#endif
7406
7407/*
7408 * "items(dict)" function
7409 */
7410 static void
7411f_items(typval_T *argvars, typval_T *rettv)
7412{
7413 dict_list(argvars, rettv, 2);
7414}
7415
7416#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
7417/*
7418 * Get the job from the argument.
7419 * Returns NULL if the job is invalid.
7420 */
7421 static job_T *
7422get_job_arg(typval_T *tv)
7423{
7424 job_T *job;
7425
7426 if (tv->v_type != VAR_JOB)
7427 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007428 semsg(_(e_invarg2), tv_get_string(tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007429 return NULL;
7430 }
7431 job = tv->vval.v_job;
7432
7433 if (job == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007434 emsg(_("E916: not a valid job"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007435 return job;
7436}
7437
7438/*
7439 * "job_getchannel()" function
7440 */
7441 static void
7442f_job_getchannel(typval_T *argvars, typval_T *rettv)
7443{
7444 job_T *job = get_job_arg(&argvars[0]);
7445
7446 if (job != NULL)
7447 {
7448 rettv->v_type = VAR_CHANNEL;
7449 rettv->vval.v_channel = job->jv_channel;
7450 if (job->jv_channel != NULL)
7451 ++job->jv_channel->ch_refcount;
7452 }
7453}
7454
7455/*
7456 * "job_info()" function
7457 */
7458 static void
7459f_job_info(typval_T *argvars, typval_T *rettv)
7460{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007461 if (argvars[0].v_type != VAR_UNKNOWN)
7462 {
7463 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007464
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007465 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7466 job_info(job, rettv->vval.v_dict);
7467 }
7468 else if (rettv_list_alloc(rettv) == OK)
7469 job_info_all(rettv->vval.v_list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007470}
7471
7472/*
7473 * "job_setoptions()" function
7474 */
7475 static void
7476f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7477{
7478 job_T *job = get_job_arg(&argvars[0]);
7479 jobopt_T opt;
7480
7481 if (job == NULL)
7482 return;
7483 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007484 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007485 job_set_options(job, &opt);
7486 free_job_options(&opt);
7487}
7488
7489/*
7490 * "job_start()" function
7491 */
7492 static void
7493f_job_start(typval_T *argvars, typval_T *rettv)
7494{
7495 rettv->v_type = VAR_JOB;
7496 if (check_restricted() || check_secure())
7497 return;
Bram Moolenaar493359e2018-06-12 20:25:52 +02007498 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007499}
7500
7501/*
7502 * "job_status()" function
7503 */
7504 static void
7505f_job_status(typval_T *argvars, typval_T *rettv)
7506{
7507 job_T *job = get_job_arg(&argvars[0]);
7508
7509 if (job != NULL)
7510 {
7511 rettv->v_type = VAR_STRING;
7512 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7513 }
7514}
7515
7516/*
7517 * "job_stop()" function
7518 */
7519 static void
7520f_job_stop(typval_T *argvars, typval_T *rettv)
7521{
7522 job_T *job = get_job_arg(&argvars[0]);
7523
7524 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007525 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007526}
7527#endif
7528
7529/*
7530 * "join()" function
7531 */
7532 static void
7533f_join(typval_T *argvars, typval_T *rettv)
7534{
7535 garray_T ga;
7536 char_u *sep;
7537
7538 if (argvars[0].v_type != VAR_LIST)
7539 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007540 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007541 return;
7542 }
7543 if (argvars[0].vval.v_list == NULL)
7544 return;
7545 if (argvars[1].v_type == VAR_UNKNOWN)
7546 sep = (char_u *)" ";
7547 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007548 sep = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007549
7550 rettv->v_type = VAR_STRING;
7551
7552 if (sep != NULL)
7553 {
7554 ga_init2(&ga, (int)sizeof(char), 80);
7555 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7556 ga_append(&ga, NUL);
7557 rettv->vval.v_string = (char_u *)ga.ga_data;
7558 }
7559 else
7560 rettv->vval.v_string = NULL;
7561}
7562
7563/*
7564 * "js_decode()" function
7565 */
7566 static void
7567f_js_decode(typval_T *argvars, typval_T *rettv)
7568{
7569 js_read_T reader;
7570
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007571 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007572 reader.js_fill = NULL;
7573 reader.js_used = 0;
7574 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007575 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007576}
7577
7578/*
7579 * "js_encode()" function
7580 */
7581 static void
7582f_js_encode(typval_T *argvars, typval_T *rettv)
7583{
7584 rettv->v_type = VAR_STRING;
7585 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7586}
7587
7588/*
7589 * "json_decode()" function
7590 */
7591 static void
7592f_json_decode(typval_T *argvars, typval_T *rettv)
7593{
7594 js_read_T reader;
7595
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007596 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007597 reader.js_fill = NULL;
7598 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007599 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007600}
7601
7602/*
7603 * "json_encode()" function
7604 */
7605 static void
7606f_json_encode(typval_T *argvars, typval_T *rettv)
7607{
7608 rettv->v_type = VAR_STRING;
7609 rettv->vval.v_string = json_encode(&argvars[0], 0);
7610}
7611
7612/*
7613 * "keys()" function
7614 */
7615 static void
7616f_keys(typval_T *argvars, typval_T *rettv)
7617{
7618 dict_list(argvars, rettv, 0);
7619}
7620
7621/*
7622 * "last_buffer_nr()" function.
7623 */
7624 static void
7625f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7626{
7627 int n = 0;
7628 buf_T *buf;
7629
Bram Moolenaar29323592016-07-24 22:04:11 +02007630 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007631 if (n < buf->b_fnum)
7632 n = buf->b_fnum;
7633
7634 rettv->vval.v_number = n;
7635}
7636
7637/*
7638 * "len()" function
7639 */
7640 static void
7641f_len(typval_T *argvars, typval_T *rettv)
7642{
7643 switch (argvars[0].v_type)
7644 {
7645 case VAR_STRING:
7646 case VAR_NUMBER:
7647 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007648 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007649 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007650 case VAR_BLOB:
7651 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
7652 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007653 case VAR_LIST:
7654 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7655 break;
7656 case VAR_DICT:
7657 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7658 break;
7659 case VAR_UNKNOWN:
7660 case VAR_SPECIAL:
7661 case VAR_FLOAT:
7662 case VAR_FUNC:
7663 case VAR_PARTIAL:
7664 case VAR_JOB:
7665 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007666 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007667 break;
7668 }
7669}
7670
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007671 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007672libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007673{
7674#ifdef FEAT_LIBCALL
7675 char_u *string_in;
7676 char_u **string_result;
7677 int nr_result;
7678#endif
7679
7680 rettv->v_type = type;
7681 if (type != VAR_NUMBER)
7682 rettv->vval.v_string = NULL;
7683
7684 if (check_restricted() || check_secure())
7685 return;
7686
7687#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007688 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007689 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7690 {
7691 string_in = NULL;
7692 if (argvars[2].v_type == VAR_STRING)
7693 string_in = argvars[2].vval.v_string;
7694 if (type == VAR_NUMBER)
7695 string_result = NULL;
7696 else
7697 string_result = &rettv->vval.v_string;
7698 if (mch_libcall(argvars[0].vval.v_string,
7699 argvars[1].vval.v_string,
7700 string_in,
7701 argvars[2].vval.v_number,
7702 string_result,
7703 &nr_result) == OK
7704 && type == VAR_NUMBER)
7705 rettv->vval.v_number = nr_result;
7706 }
7707#endif
7708}
7709
7710/*
7711 * "libcall()" function
7712 */
7713 static void
7714f_libcall(typval_T *argvars, typval_T *rettv)
7715{
7716 libcall_common(argvars, rettv, VAR_STRING);
7717}
7718
7719/*
7720 * "libcallnr()" function
7721 */
7722 static void
7723f_libcallnr(typval_T *argvars, typval_T *rettv)
7724{
7725 libcall_common(argvars, rettv, VAR_NUMBER);
7726}
7727
7728/*
7729 * "line(string)" function
7730 */
7731 static void
7732f_line(typval_T *argvars, typval_T *rettv)
7733{
7734 linenr_T lnum = 0;
7735 pos_T *fp;
7736 int fnum;
7737
7738 fp = var2fpos(&argvars[0], TRUE, &fnum);
7739 if (fp != NULL)
7740 lnum = fp->lnum;
7741 rettv->vval.v_number = lnum;
7742}
7743
7744/*
7745 * "line2byte(lnum)" function
7746 */
7747 static void
7748f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7749{
7750#ifndef FEAT_BYTEOFF
7751 rettv->vval.v_number = -1;
7752#else
7753 linenr_T lnum;
7754
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007755 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007756 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7757 rettv->vval.v_number = -1;
7758 else
7759 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7760 if (rettv->vval.v_number >= 0)
7761 ++rettv->vval.v_number;
7762#endif
7763}
7764
7765/*
7766 * "lispindent(lnum)" function
7767 */
7768 static void
7769f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7770{
7771#ifdef FEAT_LISP
7772 pos_T pos;
7773 linenr_T lnum;
7774
7775 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007776 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007777 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7778 {
7779 curwin->w_cursor.lnum = lnum;
7780 rettv->vval.v_number = get_lisp_indent();
7781 curwin->w_cursor = pos;
7782 }
7783 else
7784#endif
7785 rettv->vval.v_number = -1;
7786}
7787
7788/*
7789 * "localtime()" function
7790 */
7791 static void
7792f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7793{
7794 rettv->vval.v_number = (varnumber_T)time(NULL);
7795}
7796
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007797 static void
7798get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7799{
7800 char_u *keys;
7801 char_u *which;
7802 char_u buf[NUMBUFLEN];
7803 char_u *keys_buf = NULL;
7804 char_u *rhs;
7805 int mode;
7806 int abbr = FALSE;
7807 int get_dict = FALSE;
7808 mapblock_T *mp;
7809 int buffer_local;
7810
7811 /* return empty string for failure */
7812 rettv->v_type = VAR_STRING;
7813 rettv->vval.v_string = NULL;
7814
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007815 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007816 if (*keys == NUL)
7817 return;
7818
7819 if (argvars[1].v_type != VAR_UNKNOWN)
7820 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007821 which = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007822 if (argvars[2].v_type != VAR_UNKNOWN)
7823 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007824 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007825 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007826 get_dict = (int)tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007827 }
7828 }
7829 else
7830 which = (char_u *)"";
7831 if (which == NULL)
7832 return;
7833
7834 mode = get_map_mode(&which, 0);
7835
7836 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7837 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7838 vim_free(keys_buf);
7839
7840 if (!get_dict)
7841 {
7842 /* Return a string. */
7843 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02007844 {
7845 if (*rhs == NUL)
7846 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
7847 else
7848 rettv->vval.v_string = str2special_save(rhs, FALSE);
7849 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007850
7851 }
7852 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7853 {
7854 /* Return a dictionary. */
7855 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7856 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7857 dict_T *dict = rettv->vval.v_dict;
7858
Bram Moolenaare0be1672018-07-08 16:50:37 +02007859 dict_add_string(dict, "lhs", lhs);
7860 dict_add_string(dict, "rhs", mp->m_orig_str);
7861 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
7862 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
7863 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007864 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
7865 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02007866 dict_add_number(dict, "buffer", (long)buffer_local);
7867 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
7868 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007869
7870 vim_free(lhs);
7871 vim_free(mapmode);
7872 }
7873}
7874
7875#ifdef FEAT_FLOAT
7876/*
7877 * "log()" function
7878 */
7879 static void
7880f_log(typval_T *argvars, typval_T *rettv)
7881{
7882 float_T f = 0.0;
7883
7884 rettv->v_type = VAR_FLOAT;
7885 if (get_float_arg(argvars, &f) == OK)
7886 rettv->vval.v_float = log(f);
7887 else
7888 rettv->vval.v_float = 0.0;
7889}
7890
7891/*
7892 * "log10()" function
7893 */
7894 static void
7895f_log10(typval_T *argvars, typval_T *rettv)
7896{
7897 float_T f = 0.0;
7898
7899 rettv->v_type = VAR_FLOAT;
7900 if (get_float_arg(argvars, &f) == OK)
7901 rettv->vval.v_float = log10(f);
7902 else
7903 rettv->vval.v_float = 0.0;
7904}
7905#endif
7906
7907#ifdef FEAT_LUA
7908/*
7909 * "luaeval()" function
7910 */
7911 static void
7912f_luaeval(typval_T *argvars, typval_T *rettv)
7913{
7914 char_u *str;
7915 char_u buf[NUMBUFLEN];
7916
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007917 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007918 do_luaeval(str, argvars + 1, rettv);
7919}
7920#endif
7921
7922/*
7923 * "map()" function
7924 */
7925 static void
7926f_map(typval_T *argvars, typval_T *rettv)
7927{
7928 filter_map(argvars, rettv, TRUE);
7929}
7930
7931/*
7932 * "maparg()" function
7933 */
7934 static void
7935f_maparg(typval_T *argvars, typval_T *rettv)
7936{
7937 get_maparg(argvars, rettv, TRUE);
7938}
7939
7940/*
7941 * "mapcheck()" function
7942 */
7943 static void
7944f_mapcheck(typval_T *argvars, typval_T *rettv)
7945{
7946 get_maparg(argvars, rettv, FALSE);
7947}
7948
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007949typedef enum
7950{
7951 MATCH_END, /* matchend() */
7952 MATCH_MATCH, /* match() */
7953 MATCH_STR, /* matchstr() */
7954 MATCH_LIST, /* matchlist() */
7955 MATCH_POS /* matchstrpos() */
7956} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007957
7958 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007959find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007960{
7961 char_u *str = NULL;
7962 long len = 0;
7963 char_u *expr = NULL;
7964 char_u *pat;
7965 regmatch_T regmatch;
7966 char_u patbuf[NUMBUFLEN];
7967 char_u strbuf[NUMBUFLEN];
7968 char_u *save_cpo;
7969 long start = 0;
7970 long nth = 1;
7971 colnr_T startcol = 0;
7972 int match = 0;
7973 list_T *l = NULL;
7974 listitem_T *li = NULL;
7975 long idx = 0;
7976 char_u *tofree = NULL;
7977
7978 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7979 save_cpo = p_cpo;
7980 p_cpo = (char_u *)"";
7981
7982 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007983 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007984 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007985 /* type MATCH_LIST: return empty list when there are no matches.
7986 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007987 if (rettv_list_alloc(rettv) == FAIL)
7988 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007989 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007990 && (list_append_string(rettv->vval.v_list,
7991 (char_u *)"", 0) == 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 || list_append_number(rettv->vval.v_list,
7997 (varnumber_T)-1) == FAIL))
7998 {
7999 list_free(rettv->vval.v_list);
8000 rettv->vval.v_list = NULL;
8001 goto theend;
8002 }
8003 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008004 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008005 {
8006 rettv->v_type = VAR_STRING;
8007 rettv->vval.v_string = NULL;
8008 }
8009
8010 if (argvars[0].v_type == VAR_LIST)
8011 {
8012 if ((l = argvars[0].vval.v_list) == NULL)
8013 goto theend;
8014 li = l->lv_first;
8015 }
8016 else
8017 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008018 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008019 len = (long)STRLEN(str);
8020 }
8021
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008022 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008023 if (pat == NULL)
8024 goto theend;
8025
8026 if (argvars[2].v_type != VAR_UNKNOWN)
8027 {
8028 int error = FALSE;
8029
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008030 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008031 if (error)
8032 goto theend;
8033 if (l != NULL)
8034 {
8035 li = list_find(l, start);
8036 if (li == NULL)
8037 goto theend;
8038 idx = l->lv_idx; /* use the cached index */
8039 }
8040 else
8041 {
8042 if (start < 0)
8043 start = 0;
8044 if (start > len)
8045 goto theend;
8046 /* When "count" argument is there ignore matches before "start",
8047 * otherwise skip part of the string. Differs when pattern is "^"
8048 * or "\<". */
8049 if (argvars[3].v_type != VAR_UNKNOWN)
8050 startcol = start;
8051 else
8052 {
8053 str += start;
8054 len -= start;
8055 }
8056 }
8057
8058 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008059 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008060 if (error)
8061 goto theend;
8062 }
8063
8064 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8065 if (regmatch.regprog != NULL)
8066 {
8067 regmatch.rm_ic = p_ic;
8068
8069 for (;;)
8070 {
8071 if (l != NULL)
8072 {
8073 if (li == NULL)
8074 {
8075 match = FALSE;
8076 break;
8077 }
8078 vim_free(tofree);
8079 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
8080 if (str == NULL)
8081 break;
8082 }
8083
8084 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
8085
8086 if (match && --nth <= 0)
8087 break;
8088 if (l == NULL && !match)
8089 break;
8090
8091 /* Advance to just after the match. */
8092 if (l != NULL)
8093 {
8094 li = li->li_next;
8095 ++idx;
8096 }
8097 else
8098 {
8099#ifdef FEAT_MBYTE
8100 startcol = (colnr_T)(regmatch.startp[0]
8101 + (*mb_ptr2len)(regmatch.startp[0]) - str);
8102#else
8103 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
8104#endif
8105 if (startcol > (colnr_T)len
8106 || str + startcol <= regmatch.startp[0])
8107 {
8108 match = FALSE;
8109 break;
8110 }
8111 }
8112 }
8113
8114 if (match)
8115 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008116 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008117 {
8118 listitem_T *li1 = rettv->vval.v_list->lv_first;
8119 listitem_T *li2 = li1->li_next;
8120 listitem_T *li3 = li2->li_next;
8121 listitem_T *li4 = li3->li_next;
8122
8123 vim_free(li1->li_tv.vval.v_string);
8124 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
8125 (int)(regmatch.endp[0] - regmatch.startp[0]));
8126 li3->li_tv.vval.v_number =
8127 (varnumber_T)(regmatch.startp[0] - expr);
8128 li4->li_tv.vval.v_number =
8129 (varnumber_T)(regmatch.endp[0] - expr);
8130 if (l != NULL)
8131 li2->li_tv.vval.v_number = (varnumber_T)idx;
8132 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008133 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008134 {
8135 int i;
8136
8137 /* return list with matched string and submatches */
8138 for (i = 0; i < NSUBEXP; ++i)
8139 {
8140 if (regmatch.endp[i] == NULL)
8141 {
8142 if (list_append_string(rettv->vval.v_list,
8143 (char_u *)"", 0) == FAIL)
8144 break;
8145 }
8146 else if (list_append_string(rettv->vval.v_list,
8147 regmatch.startp[i],
8148 (int)(regmatch.endp[i] - regmatch.startp[i]))
8149 == FAIL)
8150 break;
8151 }
8152 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008153 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008154 {
8155 /* return matched string */
8156 if (l != NULL)
8157 copy_tv(&li->li_tv, rettv);
8158 else
8159 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
8160 (int)(regmatch.endp[0] - regmatch.startp[0]));
8161 }
8162 else if (l != NULL)
8163 rettv->vval.v_number = idx;
8164 else
8165 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008166 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008167 rettv->vval.v_number =
8168 (varnumber_T)(regmatch.startp[0] - str);
8169 else
8170 rettv->vval.v_number =
8171 (varnumber_T)(regmatch.endp[0] - str);
8172 rettv->vval.v_number += (varnumber_T)(str - expr);
8173 }
8174 }
8175 vim_regfree(regmatch.regprog);
8176 }
8177
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008178theend:
8179 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008180 /* matchstrpos() without a list: drop the second item. */
8181 listitem_remove(rettv->vval.v_list,
8182 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008183 vim_free(tofree);
8184 p_cpo = save_cpo;
8185}
8186
8187/*
8188 * "match()" function
8189 */
8190 static void
8191f_match(typval_T *argvars, typval_T *rettv)
8192{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008193 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008194}
8195
Bram Moolenaar95e51472018-07-28 16:55:56 +02008196#ifdef FEAT_SEARCH_EXTRA
8197 static int
8198matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
8199{
8200 dictitem_T *di;
8201
8202 if (tv->v_type != VAR_DICT)
8203 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008204 emsg(_(e_dictreq));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008205 return FAIL;
8206 }
8207
8208 if (dict_find(tv->vval.v_dict, (char_u *)"conceal", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01008209 *conceal_char = dict_get_string(tv->vval.v_dict,
Bram Moolenaar95e51472018-07-28 16:55:56 +02008210 (char_u *)"conceal", FALSE);
8211
8212 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
8213 {
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02008214 *win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008215 if (*win == NULL)
8216 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008217 emsg(_("E957: Invalid window number"));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008218 return FAIL;
8219 }
8220 }
8221
8222 return OK;
8223}
8224#endif
8225
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008226/*
8227 * "matchadd()" function
8228 */
8229 static void
8230f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8231{
8232#ifdef FEAT_SEARCH_EXTRA
8233 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008234 char_u *grp = tv_get_string_buf_chk(&argvars[0], buf); /* group */
8235 char_u *pat = tv_get_string_buf_chk(&argvars[1], buf); /* pattern */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008236 int prio = 10; /* default priority */
8237 int id = -1;
8238 int error = FALSE;
8239 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008240 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008241
8242 rettv->vval.v_number = -1;
8243
8244 if (grp == NULL || pat == NULL)
8245 return;
8246 if (argvars[2].v_type != VAR_UNKNOWN)
8247 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008248 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008249 if (argvars[3].v_type != VAR_UNKNOWN)
8250 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008251 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008252 if (argvars[4].v_type != VAR_UNKNOWN
8253 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8254 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008255 }
8256 }
8257 if (error == TRUE)
8258 return;
8259 if (id >= 1 && id <= 3)
8260 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008261 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008262 return;
8263 }
8264
Bram Moolenaar95e51472018-07-28 16:55:56 +02008265 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008266 conceal_char);
8267#endif
8268}
8269
8270/*
8271 * "matchaddpos()" function
8272 */
8273 static void
8274f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8275{
8276#ifdef FEAT_SEARCH_EXTRA
8277 char_u buf[NUMBUFLEN];
8278 char_u *group;
8279 int prio = 10;
8280 int id = -1;
8281 int error = FALSE;
8282 list_T *l;
8283 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008284 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008285
8286 rettv->vval.v_number = -1;
8287
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008288 group = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008289 if (group == NULL)
8290 return;
8291
8292 if (argvars[1].v_type != VAR_LIST)
8293 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008294 semsg(_(e_listarg), "matchaddpos()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008295 return;
8296 }
8297 l = argvars[1].vval.v_list;
8298 if (l == NULL)
8299 return;
8300
8301 if (argvars[2].v_type != VAR_UNKNOWN)
8302 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008303 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008304 if (argvars[3].v_type != VAR_UNKNOWN)
8305 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008306 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008307
8308 if (argvars[4].v_type != VAR_UNKNOWN
8309 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8310 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008311 }
8312 }
8313 if (error == TRUE)
8314 return;
8315
8316 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8317 if (id == 1 || id == 2)
8318 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008319 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008320 return;
8321 }
8322
Bram Moolenaar95e51472018-07-28 16:55:56 +02008323 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008324 conceal_char);
8325#endif
8326}
8327
8328/*
8329 * "matcharg()" function
8330 */
8331 static void
8332f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8333{
8334 if (rettv_list_alloc(rettv) == OK)
8335 {
8336#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008337 int id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008338 matchitem_T *m;
8339
8340 if (id >= 1 && id <= 3)
8341 {
8342 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8343 {
8344 list_append_string(rettv->vval.v_list,
8345 syn_id2name(m->hlg_id), -1);
8346 list_append_string(rettv->vval.v_list, m->pattern, -1);
8347 }
8348 else
8349 {
8350 list_append_string(rettv->vval.v_list, NULL, -1);
8351 list_append_string(rettv->vval.v_list, NULL, -1);
8352 }
8353 }
8354#endif
8355 }
8356}
8357
8358/*
8359 * "matchdelete()" function
8360 */
8361 static void
8362f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8363{
8364#ifdef FEAT_SEARCH_EXTRA
8365 rettv->vval.v_number = match_delete(curwin,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008366 (int)tv_get_number(&argvars[0]), TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008367#endif
8368}
8369
8370/*
8371 * "matchend()" function
8372 */
8373 static void
8374f_matchend(typval_T *argvars, typval_T *rettv)
8375{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008376 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008377}
8378
8379/*
8380 * "matchlist()" function
8381 */
8382 static void
8383f_matchlist(typval_T *argvars, typval_T *rettv)
8384{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008385 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008386}
8387
8388/*
8389 * "matchstr()" function
8390 */
8391 static void
8392f_matchstr(typval_T *argvars, typval_T *rettv)
8393{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008394 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008395}
8396
8397/*
8398 * "matchstrpos()" function
8399 */
8400 static void
8401f_matchstrpos(typval_T *argvars, typval_T *rettv)
8402{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008403 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008404}
8405
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008406 static void
8407max_min(typval_T *argvars, typval_T *rettv, int domax)
8408{
8409 varnumber_T n = 0;
8410 varnumber_T i;
8411 int error = FALSE;
8412
8413 if (argvars[0].v_type == VAR_LIST)
8414 {
8415 list_T *l;
8416 listitem_T *li;
8417
8418 l = argvars[0].vval.v_list;
8419 if (l != NULL)
8420 {
8421 li = l->lv_first;
8422 if (li != NULL)
8423 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008424 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008425 for (;;)
8426 {
8427 li = li->li_next;
8428 if (li == NULL)
8429 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008430 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008431 if (domax ? i > n : i < n)
8432 n = i;
8433 }
8434 }
8435 }
8436 }
8437 else if (argvars[0].v_type == VAR_DICT)
8438 {
8439 dict_T *d;
8440 int first = TRUE;
8441 hashitem_T *hi;
8442 int todo;
8443
8444 d = argvars[0].vval.v_dict;
8445 if (d != NULL)
8446 {
8447 todo = (int)d->dv_hashtab.ht_used;
8448 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8449 {
8450 if (!HASHITEM_EMPTY(hi))
8451 {
8452 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008453 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008454 if (first)
8455 {
8456 n = i;
8457 first = FALSE;
8458 }
8459 else if (domax ? i > n : i < n)
8460 n = i;
8461 }
8462 }
8463 }
8464 }
8465 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008466 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008467 rettv->vval.v_number = error ? 0 : n;
8468}
8469
8470/*
8471 * "max()" function
8472 */
8473 static void
8474f_max(typval_T *argvars, typval_T *rettv)
8475{
8476 max_min(argvars, rettv, TRUE);
8477}
8478
8479/*
8480 * "min()" function
8481 */
8482 static void
8483f_min(typval_T *argvars, typval_T *rettv)
8484{
8485 max_min(argvars, rettv, FALSE);
8486}
8487
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008488/*
8489 * Create the directory in which "dir" is located, and higher levels when
8490 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008491 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008492 */
8493 static int
8494mkdir_recurse(char_u *dir, int prot)
8495{
8496 char_u *p;
8497 char_u *updir;
8498 int r = FAIL;
8499
8500 /* Get end of directory name in "dir".
8501 * We're done when it's "/" or "c:/". */
8502 p = gettail_sep(dir);
8503 if (p <= get_past_head(dir))
8504 return OK;
8505
8506 /* If the directory exists we're done. Otherwise: create it.*/
8507 updir = vim_strnsave(dir, (int)(p - dir));
8508 if (updir == NULL)
8509 return FAIL;
8510 if (mch_isdir(updir))
8511 r = OK;
8512 else if (mkdir_recurse(updir, prot) == OK)
8513 r = vim_mkdir_emsg(updir, prot);
8514 vim_free(updir);
8515 return r;
8516}
8517
8518#ifdef vim_mkdir
8519/*
8520 * "mkdir()" function
8521 */
8522 static void
8523f_mkdir(typval_T *argvars, typval_T *rettv)
8524{
8525 char_u *dir;
8526 char_u buf[NUMBUFLEN];
8527 int prot = 0755;
8528
8529 rettv->vval.v_number = FAIL;
8530 if (check_restricted() || check_secure())
8531 return;
8532
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008533 dir = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008534 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008535 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008536
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008537 if (*gettail(dir) == NUL)
8538 /* remove trailing slashes */
8539 *gettail_sep(dir) = NUL;
8540
8541 if (argvars[1].v_type != VAR_UNKNOWN)
8542 {
8543 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008544 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008545 prot = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008546 if (prot == -1)
8547 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008548 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008549 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008550 {
8551 if (mch_isdir(dir))
8552 {
8553 /* With the "p" flag it's OK if the dir already exists. */
8554 rettv->vval.v_number = OK;
8555 return;
8556 }
8557 mkdir_recurse(dir, prot);
8558 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008559 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008560 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008561}
8562#endif
8563
8564/*
8565 * "mode()" function
8566 */
8567 static void
8568f_mode(typval_T *argvars, typval_T *rettv)
8569{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008570 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008571
Bram Moolenaar612cc382018-07-29 15:34:26 +02008572 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008573
8574 if (time_for_testing == 93784)
8575 {
8576 /* Testing the two-character code. */
8577 buf[0] = 'x';
8578 buf[1] = '!';
8579 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008580#ifdef FEAT_TERMINAL
8581 else if (term_use_loop())
8582 buf[0] = 't';
8583#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008584 else if (VIsual_active)
8585 {
8586 if (VIsual_select)
8587 buf[0] = VIsual_mode + 's' - 'v';
8588 else
8589 buf[0] = VIsual_mode;
8590 }
8591 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8592 || State == CONFIRM)
8593 {
8594 buf[0] = 'r';
8595 if (State == ASKMORE)
8596 buf[1] = 'm';
8597 else if (State == CONFIRM)
8598 buf[1] = '?';
8599 }
8600 else if (State == EXTERNCMD)
8601 buf[0] = '!';
8602 else if (State & INSERT)
8603 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008604 if (State & VREPLACE_FLAG)
8605 {
8606 buf[0] = 'R';
8607 buf[1] = 'v';
8608 }
8609 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008610 {
8611 if (State & REPLACE_FLAG)
8612 buf[0] = 'R';
8613 else
8614 buf[0] = 'i';
8615#ifdef FEAT_INS_EXPAND
8616 if (ins_compl_active())
8617 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008618 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008619 buf[1] = 'x';
8620#endif
8621 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008622 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008623 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008624 {
8625 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008626 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008627 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008628 else if (exmode_active == EXMODE_NORMAL)
8629 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008630 }
8631 else
8632 {
8633 buf[0] = 'n';
8634 if (finish_op)
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008635 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008636 buf[1] = 'o';
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008637 // to be able to detect force-linewise/blockwise/characterwise operations
8638 buf[2] = motion_force;
8639 }
Bram Moolenaar612cc382018-07-29 15:34:26 +02008640 else if (restart_edit == 'I' || restart_edit == 'R'
8641 || restart_edit == 'V')
8642 {
8643 buf[1] = 'i';
8644 buf[2] = restart_edit;
8645 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008646 }
8647
8648 /* Clear out the minor mode when the argument is not a non-zero number or
8649 * non-empty string. */
8650 if (!non_zero_arg(&argvars[0]))
8651 buf[1] = NUL;
8652
8653 rettv->vval.v_string = vim_strsave(buf);
8654 rettv->v_type = VAR_STRING;
8655}
8656
8657#if defined(FEAT_MZSCHEME) || defined(PROTO)
8658/*
8659 * "mzeval()" function
8660 */
8661 static void
8662f_mzeval(typval_T *argvars, typval_T *rettv)
8663{
8664 char_u *str;
8665 char_u buf[NUMBUFLEN];
8666
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008667 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008668 do_mzeval(str, rettv);
8669}
8670
8671 void
8672mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8673{
8674 typval_T argvars[3];
8675
8676 argvars[0].v_type = VAR_STRING;
8677 argvars[0].vval.v_string = name;
8678 copy_tv(args, &argvars[1]);
8679 argvars[2].v_type = VAR_UNKNOWN;
8680 f_call(argvars, rettv);
8681 clear_tv(&argvars[1]);
8682}
8683#endif
8684
8685/*
8686 * "nextnonblank()" function
8687 */
8688 static void
8689f_nextnonblank(typval_T *argvars, typval_T *rettv)
8690{
8691 linenr_T lnum;
8692
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008693 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008694 {
8695 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8696 {
8697 lnum = 0;
8698 break;
8699 }
8700 if (*skipwhite(ml_get(lnum)) != NUL)
8701 break;
8702 }
8703 rettv->vval.v_number = lnum;
8704}
8705
8706/*
8707 * "nr2char()" function
8708 */
8709 static void
8710f_nr2char(typval_T *argvars, typval_T *rettv)
8711{
8712 char_u buf[NUMBUFLEN];
8713
8714#ifdef FEAT_MBYTE
8715 if (has_mbyte)
8716 {
8717 int utf8 = 0;
8718
8719 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008720 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008721 if (utf8)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008722 buf[(*utf_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008723 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008724 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008725 }
8726 else
8727#endif
8728 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008729 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008730 buf[1] = NUL;
8731 }
8732 rettv->v_type = VAR_STRING;
8733 rettv->vval.v_string = vim_strsave(buf);
8734}
8735
8736/*
8737 * "or(expr, expr)" function
8738 */
8739 static void
8740f_or(typval_T *argvars, typval_T *rettv)
8741{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008742 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
8743 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008744}
8745
8746/*
8747 * "pathshorten()" function
8748 */
8749 static void
8750f_pathshorten(typval_T *argvars, typval_T *rettv)
8751{
8752 char_u *p;
8753
8754 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008755 p = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008756 if (p == NULL)
8757 rettv->vval.v_string = NULL;
8758 else
8759 {
8760 p = vim_strsave(p);
8761 rettv->vval.v_string = p;
8762 if (p != NULL)
8763 shorten_dir(p);
8764 }
8765}
8766
8767#ifdef FEAT_PERL
8768/*
8769 * "perleval()" function
8770 */
8771 static void
8772f_perleval(typval_T *argvars, typval_T *rettv)
8773{
8774 char_u *str;
8775 char_u buf[NUMBUFLEN];
8776
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008777 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008778 do_perleval(str, rettv);
8779}
8780#endif
8781
8782#ifdef FEAT_FLOAT
8783/*
8784 * "pow()" function
8785 */
8786 static void
8787f_pow(typval_T *argvars, typval_T *rettv)
8788{
8789 float_T fx = 0.0, fy = 0.0;
8790
8791 rettv->v_type = VAR_FLOAT;
8792 if (get_float_arg(argvars, &fx) == OK
8793 && get_float_arg(&argvars[1], &fy) == OK)
8794 rettv->vval.v_float = pow(fx, fy);
8795 else
8796 rettv->vval.v_float = 0.0;
8797}
8798#endif
8799
8800/*
8801 * "prevnonblank()" function
8802 */
8803 static void
8804f_prevnonblank(typval_T *argvars, typval_T *rettv)
8805{
8806 linenr_T lnum;
8807
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008808 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008809 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8810 lnum = 0;
8811 else
8812 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8813 --lnum;
8814 rettv->vval.v_number = lnum;
8815}
8816
8817/* This dummy va_list is here because:
8818 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8819 * - locally in the function results in a "used before set" warning
8820 * - using va_start() to initialize it gives "function with fixed args" error */
8821static va_list ap;
8822
8823/*
8824 * "printf()" function
8825 */
8826 static void
8827f_printf(typval_T *argvars, typval_T *rettv)
8828{
8829 char_u buf[NUMBUFLEN];
8830 int len;
8831 char_u *s;
8832 int saved_did_emsg = did_emsg;
8833 char *fmt;
8834
8835 rettv->v_type = VAR_STRING;
8836 rettv->vval.v_string = NULL;
8837
8838 /* Get the required length, allocate the buffer and do it for real. */
8839 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008840 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008841 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008842 if (!did_emsg)
8843 {
8844 s = alloc(len + 1);
8845 if (s != NULL)
8846 {
8847 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008848 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8849 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008850 }
8851 }
8852 did_emsg |= saved_did_emsg;
8853}
8854
Bram Moolenaarf2732452018-06-03 14:47:35 +02008855#ifdef FEAT_JOB_CHANNEL
8856/*
8857 * "prompt_setcallback({buffer}, {callback})" function
8858 */
8859 static void
8860f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
8861{
8862 buf_T *buf;
8863 char_u *callback;
8864 partial_T *partial;
8865
8866 if (check_secure())
8867 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01008868 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008869 if (buf == NULL)
8870 return;
8871
8872 callback = get_callback(&argvars[1], &partial);
8873 if (callback == NULL)
8874 return;
8875
8876 free_callback(buf->b_prompt_callback, buf->b_prompt_partial);
8877 if (partial == NULL)
8878 buf->b_prompt_callback = vim_strsave(callback);
8879 else
8880 /* pointer into the partial */
8881 buf->b_prompt_callback = callback;
8882 buf->b_prompt_partial = partial;
8883}
8884
8885/*
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02008886 * "prompt_setinterrupt({buffer}, {callback})" function
8887 */
8888 static void
8889f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
8890{
8891 buf_T *buf;
8892 char_u *callback;
8893 partial_T *partial;
8894
8895 if (check_secure())
8896 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01008897 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02008898 if (buf == NULL)
8899 return;
8900
8901 callback = get_callback(&argvars[1], &partial);
8902 if (callback == NULL)
8903 return;
8904
8905 free_callback(buf->b_prompt_interrupt, buf->b_prompt_int_partial);
8906 if (partial == NULL)
8907 buf->b_prompt_interrupt = vim_strsave(callback);
8908 else
8909 /* pointer into the partial */
8910 buf->b_prompt_interrupt = callback;
8911 buf->b_prompt_int_partial = partial;
8912}
8913
8914/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02008915 * "prompt_setprompt({buffer}, {text})" function
8916 */
8917 static void
8918f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
8919{
8920 buf_T *buf;
8921 char_u *text;
8922
8923 if (check_secure())
8924 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01008925 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008926 if (buf == NULL)
8927 return;
8928
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008929 text = tv_get_string(&argvars[1]);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008930 vim_free(buf->b_prompt_text);
8931 buf->b_prompt_text = vim_strsave(text);
8932}
8933#endif
8934
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008935/*
8936 * "pumvisible()" function
8937 */
8938 static void
8939f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8940{
8941#ifdef FEAT_INS_EXPAND
8942 if (pum_visible())
8943 rettv->vval.v_number = 1;
8944#endif
8945}
8946
8947#ifdef FEAT_PYTHON3
8948/*
8949 * "py3eval()" function
8950 */
8951 static void
8952f_py3eval(typval_T *argvars, typval_T *rettv)
8953{
8954 char_u *str;
8955 char_u buf[NUMBUFLEN];
8956
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008957 if (p_pyx == 0)
8958 p_pyx = 3;
8959
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008960 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008961 do_py3eval(str, rettv);
8962}
8963#endif
8964
8965#ifdef FEAT_PYTHON
8966/*
8967 * "pyeval()" function
8968 */
8969 static void
8970f_pyeval(typval_T *argvars, typval_T *rettv)
8971{
8972 char_u *str;
8973 char_u buf[NUMBUFLEN];
8974
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008975 if (p_pyx == 0)
8976 p_pyx = 2;
8977
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008978 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008979 do_pyeval(str, rettv);
8980}
8981#endif
8982
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008983#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8984/*
8985 * "pyxeval()" function
8986 */
8987 static void
8988f_pyxeval(typval_T *argvars, typval_T *rettv)
8989{
8990# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8991 init_pyxversion();
8992 if (p_pyx == 2)
8993 f_pyeval(argvars, rettv);
8994 else
8995 f_py3eval(argvars, rettv);
8996# elif defined(FEAT_PYTHON)
8997 f_pyeval(argvars, rettv);
8998# elif defined(FEAT_PYTHON3)
8999 f_py3eval(argvars, rettv);
9000# endif
9001}
9002#endif
9003
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009004/*
9005 * "range()" function
9006 */
9007 static void
9008f_range(typval_T *argvars, typval_T *rettv)
9009{
9010 varnumber_T start;
9011 varnumber_T end;
9012 varnumber_T stride = 1;
9013 varnumber_T i;
9014 int error = FALSE;
9015
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009016 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009017 if (argvars[1].v_type == VAR_UNKNOWN)
9018 {
9019 end = start - 1;
9020 start = 0;
9021 }
9022 else
9023 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009024 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009025 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009026 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009027 }
9028
9029 if (error)
9030 return; /* type error; errmsg already given */
9031 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009032 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009033 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009034 emsg(_("E727: Start past end"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009035 else
9036 {
9037 if (rettv_list_alloc(rettv) == OK)
9038 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
9039 if (list_append_number(rettv->vval.v_list,
9040 (varnumber_T)i) == FAIL)
9041 break;
9042 }
9043}
9044
9045/*
9046 * "readfile()" function
9047 */
9048 static void
9049f_readfile(typval_T *argvars, typval_T *rettv)
9050{
9051 int binary = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009052 int blob = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009053 int failed = FALSE;
9054 char_u *fname;
9055 FILE *fd;
9056 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
9057 int io_size = sizeof(buf);
9058 int readlen; /* size of last fread() */
9059 char_u *prev = NULL; /* previously read bytes, if any */
9060 long prevlen = 0; /* length of data in prev */
9061 long prevsize = 0; /* size of prev buffer */
9062 long maxline = MAXLNUM;
9063 long cnt = 0;
9064 char_u *p; /* position in buf */
9065 char_u *start; /* start of current line */
9066
9067 if (argvars[1].v_type != VAR_UNKNOWN)
9068 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009069 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009070 binary = TRUE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009071 if (STRCMP(tv_get_string(&argvars[1]), "B") == 0)
9072 blob = TRUE;
9073
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009074 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009075 maxline = (long)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009076 }
9077
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009078 if (blob)
9079 {
9080 if (rettv_blob_alloc(rettv) == FAIL)
9081 return;
9082 }
9083 else
9084 {
9085 if (rettv_list_alloc(rettv) == FAIL)
9086 return;
9087 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009088
9089 /* Always open the file in binary mode, library functions have a mind of
9090 * their own about CR-LF conversion. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009091 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009092 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
9093 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009094 semsg(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009095 return;
9096 }
9097
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009098 if (blob)
9099 {
9100 if (read_blob(fd, rettv->vval.v_blob) == FAIL)
9101 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009102 emsg("cannot read file");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009103 blob_free(rettv->vval.v_blob);
9104 }
9105 fclose(fd);
9106 return;
9107 }
9108
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009109 while (cnt < maxline || maxline < 0)
9110 {
9111 readlen = (int)fread(buf, 1, io_size, fd);
9112
9113 /* This for loop processes what was read, but is also entered at end
9114 * of file so that either:
9115 * - an incomplete line gets written
9116 * - a "binary" file gets an empty line at the end if it ends in a
9117 * newline. */
9118 for (p = buf, start = buf;
9119 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
9120 ++p)
9121 {
9122 if (*p == '\n' || readlen <= 0)
9123 {
9124 listitem_T *li;
9125 char_u *s = NULL;
9126 long_u len = p - start;
9127
9128 /* Finished a line. Remove CRs before NL. */
9129 if (readlen > 0 && !binary)
9130 {
9131 while (len > 0 && start[len - 1] == '\r')
9132 --len;
9133 /* removal may cross back to the "prev" string */
9134 if (len == 0)
9135 while (prevlen > 0 && prev[prevlen - 1] == '\r')
9136 --prevlen;
9137 }
9138 if (prevlen == 0)
9139 s = vim_strnsave(start, (int)len);
9140 else
9141 {
9142 /* Change "prev" buffer to be the right size. This way
9143 * the bytes are only copied once, and very long lines are
9144 * allocated only once. */
9145 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
9146 {
9147 mch_memmove(s + prevlen, start, len);
9148 s[prevlen + len] = NUL;
9149 prev = NULL; /* the list will own the string */
9150 prevlen = prevsize = 0;
9151 }
9152 }
9153 if (s == NULL)
9154 {
9155 do_outofmem_msg((long_u) prevlen + len + 1);
9156 failed = TRUE;
9157 break;
9158 }
9159
9160 if ((li = listitem_alloc()) == NULL)
9161 {
9162 vim_free(s);
9163 failed = TRUE;
9164 break;
9165 }
9166 li->li_tv.v_type = VAR_STRING;
9167 li->li_tv.v_lock = 0;
9168 li->li_tv.vval.v_string = s;
9169 list_append(rettv->vval.v_list, li);
9170
9171 start = p + 1; /* step over newline */
9172 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
9173 break;
9174 }
9175 else if (*p == NUL)
9176 *p = '\n';
9177#ifdef FEAT_MBYTE
9178 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
9179 * when finding the BF and check the previous two bytes. */
9180 else if (*p == 0xbf && enc_utf8 && !binary)
9181 {
9182 /* Find the two bytes before the 0xbf. If p is at buf, or buf
9183 * + 1, these may be in the "prev" string. */
9184 char_u back1 = p >= buf + 1 ? p[-1]
9185 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
9186 char_u back2 = p >= buf + 2 ? p[-2]
9187 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
9188 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
9189
9190 if (back2 == 0xef && back1 == 0xbb)
9191 {
9192 char_u *dest = p - 2;
9193
9194 /* Usually a BOM is at the beginning of a file, and so at
9195 * the beginning of a line; then we can just step over it.
9196 */
9197 if (start == dest)
9198 start = p + 1;
9199 else
9200 {
9201 /* have to shuffle buf to close gap */
9202 int adjust_prevlen = 0;
9203
9204 if (dest < buf)
9205 {
9206 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
9207 dest = buf;
9208 }
9209 if (readlen > p - buf + 1)
9210 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
9211 readlen -= 3 - adjust_prevlen;
9212 prevlen -= adjust_prevlen;
9213 p = dest - 1;
9214 }
9215 }
9216 }
9217#endif
9218 } /* for */
9219
9220 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
9221 break;
9222 if (start < p)
9223 {
9224 /* There's part of a line in buf, store it in "prev". */
9225 if (p - start + prevlen >= prevsize)
9226 {
9227 /* need bigger "prev" buffer */
9228 char_u *newprev;
9229
9230 /* A common use case is ordinary text files and "prev" gets a
9231 * fragment of a line, so the first allocation is made
9232 * small, to avoid repeatedly 'allocing' large and
9233 * 'reallocing' small. */
9234 if (prevsize == 0)
9235 prevsize = (long)(p - start);
9236 else
9237 {
9238 long grow50pc = (prevsize * 3) / 2;
9239 long growmin = (long)((p - start) * 2 + prevlen);
9240 prevsize = grow50pc > growmin ? grow50pc : growmin;
9241 }
9242 newprev = prev == NULL ? alloc(prevsize)
9243 : vim_realloc(prev, prevsize);
9244 if (newprev == NULL)
9245 {
9246 do_outofmem_msg((long_u)prevsize);
9247 failed = TRUE;
9248 break;
9249 }
9250 prev = newprev;
9251 }
9252 /* Add the line part to end of "prev". */
9253 mch_memmove(prev + prevlen, start, p - start);
9254 prevlen += (long)(p - start);
9255 }
9256 } /* while */
9257
9258 /*
9259 * For a negative line count use only the lines at the end of the file,
9260 * free the rest.
9261 */
9262 if (!failed && maxline < 0)
9263 while (cnt > -maxline)
9264 {
9265 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
9266 --cnt;
9267 }
9268
9269 if (failed)
9270 {
9271 list_free(rettv->vval.v_list);
9272 /* readfile doc says an empty list is returned on error */
9273 rettv->vval.v_list = list_alloc();
9274 }
9275
9276 vim_free(prev);
9277 fclose(fd);
9278}
9279
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009280 static void
9281return_register(int regname, typval_T *rettv)
9282{
9283 char_u buf[2] = {0, 0};
9284
9285 buf[0] = (char_u)regname;
9286 rettv->v_type = VAR_STRING;
9287 rettv->vval.v_string = vim_strsave(buf);
9288}
9289
9290/*
9291 * "reg_executing()" function
9292 */
9293 static void
9294f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9295{
9296 return_register(reg_executing, rettv);
9297}
9298
9299/*
9300 * "reg_recording()" function
9301 */
9302 static void
9303f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9304{
9305 return_register(reg_recording, rettv);
9306}
9307
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009308#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009309/*
9310 * Convert a List to proftime_T.
9311 * Return FAIL when there is something wrong.
9312 */
9313 static int
9314list2proftime(typval_T *arg, proftime_T *tm)
9315{
9316 long n1, n2;
9317 int error = FALSE;
9318
9319 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9320 || arg->vval.v_list->lv_len != 2)
9321 return FAIL;
9322 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9323 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
9324# ifdef WIN3264
9325 tm->HighPart = n1;
9326 tm->LowPart = n2;
9327# else
9328 tm->tv_sec = n1;
9329 tm->tv_usec = n2;
9330# endif
9331 return error ? FAIL : OK;
9332}
9333#endif /* FEAT_RELTIME */
9334
9335/*
9336 * "reltime()" function
9337 */
9338 static void
9339f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9340{
9341#ifdef FEAT_RELTIME
9342 proftime_T res;
9343 proftime_T start;
9344
9345 if (argvars[0].v_type == VAR_UNKNOWN)
9346 {
9347 /* No arguments: get current time. */
9348 profile_start(&res);
9349 }
9350 else if (argvars[1].v_type == VAR_UNKNOWN)
9351 {
9352 if (list2proftime(&argvars[0], &res) == FAIL)
9353 return;
9354 profile_end(&res);
9355 }
9356 else
9357 {
9358 /* Two arguments: compute the difference. */
9359 if (list2proftime(&argvars[0], &start) == FAIL
9360 || list2proftime(&argvars[1], &res) == FAIL)
9361 return;
9362 profile_sub(&res, &start);
9363 }
9364
9365 if (rettv_list_alloc(rettv) == OK)
9366 {
9367 long n1, n2;
9368
9369# ifdef WIN3264
9370 n1 = res.HighPart;
9371 n2 = res.LowPart;
9372# else
9373 n1 = res.tv_sec;
9374 n2 = res.tv_usec;
9375# endif
9376 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9377 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9378 }
9379#endif
9380}
9381
9382#ifdef FEAT_FLOAT
9383/*
9384 * "reltimefloat()" function
9385 */
9386 static void
9387f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9388{
9389# ifdef FEAT_RELTIME
9390 proftime_T tm;
9391# endif
9392
9393 rettv->v_type = VAR_FLOAT;
9394 rettv->vval.v_float = 0;
9395# ifdef FEAT_RELTIME
9396 if (list2proftime(&argvars[0], &tm) == OK)
9397 rettv->vval.v_float = profile_float(&tm);
9398# endif
9399}
9400#endif
9401
9402/*
9403 * "reltimestr()" function
9404 */
9405 static void
9406f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9407{
9408#ifdef FEAT_RELTIME
9409 proftime_T tm;
9410#endif
9411
9412 rettv->v_type = VAR_STRING;
9413 rettv->vval.v_string = NULL;
9414#ifdef FEAT_RELTIME
9415 if (list2proftime(&argvars[0], &tm) == OK)
9416 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9417#endif
9418}
9419
9420#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009421 static void
9422make_connection(void)
9423{
9424 if (X_DISPLAY == NULL
9425# ifdef FEAT_GUI
9426 && !gui.in_use
9427# endif
9428 )
9429 {
9430 x_force_connect = TRUE;
9431 setup_term_clip();
9432 x_force_connect = FALSE;
9433 }
9434}
9435
9436 static int
9437check_connection(void)
9438{
9439 make_connection();
9440 if (X_DISPLAY == NULL)
9441 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009442 emsg(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009443 return FAIL;
9444 }
9445 return OK;
9446}
9447#endif
9448
9449#ifdef FEAT_CLIENTSERVER
9450 static void
9451remote_common(typval_T *argvars, typval_T *rettv, int expr)
9452{
9453 char_u *server_name;
9454 char_u *keys;
9455 char_u *r = NULL;
9456 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009457 int timeout = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009458# ifdef WIN32
9459 HWND w;
9460# else
9461 Window w;
9462# endif
9463
9464 if (check_restricted() || check_secure())
9465 return;
9466
9467# ifdef FEAT_X11
9468 if (check_connection() == FAIL)
9469 return;
9470# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009471 if (argvars[2].v_type != VAR_UNKNOWN
9472 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009473 timeout = tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009474
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009475 server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009476 if (server_name == NULL)
9477 return; /* type error; errmsg already given */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009478 keys = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009479# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009480 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009481# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009482 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9483 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009484# endif
9485 {
9486 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009487 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009488 emsg((char *)r); // sending worked but evaluation failed
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009489 vim_free(r);
9490 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009491 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009492 semsg(_("E241: Unable to send to %s"), server_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009493 return;
9494 }
9495
9496 rettv->vval.v_string = r;
9497
9498 if (argvars[2].v_type != VAR_UNKNOWN)
9499 {
9500 dictitem_T v;
9501 char_u str[30];
9502 char_u *idvar;
9503
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009504 idvar = tv_get_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009505 if (idvar != NULL && *idvar != NUL)
9506 {
9507 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9508 v.di_tv.v_type = VAR_STRING;
9509 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009510 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009511 vim_free(v.di_tv.vval.v_string);
9512 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009513 }
9514}
9515#endif
9516
9517/*
9518 * "remote_expr()" function
9519 */
9520 static void
9521f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9522{
9523 rettv->v_type = VAR_STRING;
9524 rettv->vval.v_string = NULL;
9525#ifdef FEAT_CLIENTSERVER
9526 remote_common(argvars, rettv, TRUE);
9527#endif
9528}
9529
9530/*
9531 * "remote_foreground()" function
9532 */
9533 static void
9534f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9535{
9536#ifdef FEAT_CLIENTSERVER
9537# ifdef WIN32
9538 /* On Win32 it's done in this application. */
9539 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009540 char_u *server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009541
9542 if (server_name != NULL)
9543 serverForeground(server_name);
9544 }
9545# else
9546 /* Send a foreground() expression to the server. */
9547 argvars[1].v_type = VAR_STRING;
9548 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9549 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009550 rettv->v_type = VAR_STRING;
9551 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009552 remote_common(argvars, rettv, TRUE);
9553 vim_free(argvars[1].vval.v_string);
9554# endif
9555#endif
9556}
9557
9558 static void
9559f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
9560{
9561#ifdef FEAT_CLIENTSERVER
9562 dictitem_T v;
9563 char_u *s = NULL;
9564# ifdef WIN32
9565 long_u n = 0;
9566# endif
9567 char_u *serverid;
9568
9569 if (check_restricted() || check_secure())
9570 {
9571 rettv->vval.v_number = -1;
9572 return;
9573 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009574 serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009575 if (serverid == NULL)
9576 {
9577 rettv->vval.v_number = -1;
9578 return; /* type error; errmsg already given */
9579 }
9580# ifdef WIN32
9581 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
9582 if (n == 0)
9583 rettv->vval.v_number = -1;
9584 else
9585 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009586 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009587 rettv->vval.v_number = (s != NULL);
9588 }
9589# else
9590 if (check_connection() == FAIL)
9591 return;
9592
9593 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
9594 serverStrToWin(serverid), &s);
9595# endif
9596
9597 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
9598 {
9599 char_u *retvar;
9600
9601 v.di_tv.v_type = VAR_STRING;
9602 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009603 retvar = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009604 if (retvar != NULL)
9605 set_var(retvar, &v.di_tv, FALSE);
9606 vim_free(v.di_tv.vval.v_string);
9607 }
9608#else
9609 rettv->vval.v_number = -1;
9610#endif
9611}
9612
9613 static void
9614f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
9615{
9616 char_u *r = NULL;
9617
9618#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009619 char_u *serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009620
9621 if (serverid != NULL && !check_restricted() && !check_secure())
9622 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009623 int timeout = 0;
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009624# ifdef WIN32
9625 /* The server's HWND is encoded in the 'id' parameter */
9626 long_u n = 0;
9627# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009628
9629 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009630 timeout = tv_get_number(&argvars[1]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009631
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009632# ifdef WIN32
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009633 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
9634 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009635 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009636 if (r == NULL)
9637# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009638 if (check_connection() == FAIL
9639 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
9640 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009641# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009642 emsg(_("E277: Unable to read a server reply"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009643 }
9644#endif
9645 rettv->v_type = VAR_STRING;
9646 rettv->vval.v_string = r;
9647}
9648
9649/*
9650 * "remote_send()" function
9651 */
9652 static void
9653f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9654{
9655 rettv->v_type = VAR_STRING;
9656 rettv->vval.v_string = NULL;
9657#ifdef FEAT_CLIENTSERVER
9658 remote_common(argvars, rettv, FALSE);
9659#endif
9660}
9661
9662/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009663 * "remote_startserver()" function
9664 */
9665 static void
9666f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9667{
9668#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009669 char_u *server = tv_get_string_chk(&argvars[0]);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009670
9671 if (server == NULL)
9672 return; /* type error; errmsg already given */
9673 if (serverName != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009674 emsg(_("E941: already started a server"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009675 else
9676 {
9677# ifdef FEAT_X11
9678 if (check_connection() == OK)
9679 serverRegisterName(X_DISPLAY, server);
9680# else
9681 serverSetName(server);
9682# endif
9683 }
9684#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009685 emsg(_("E942: +clientserver feature not available"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009686#endif
9687}
9688
9689/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009690 * "remove()" function
9691 */
9692 static void
9693f_remove(typval_T *argvars, typval_T *rettv)
9694{
9695 list_T *l;
9696 listitem_T *item, *item2;
9697 listitem_T *li;
9698 long idx;
9699 long end;
9700 char_u *key;
9701 dict_T *d;
9702 dictitem_T *di;
9703 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009704 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009705
9706 if (argvars[0].v_type == VAR_DICT)
9707 {
9708 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009709 semsg(_(e_toomanyarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009710 else if ((d = argvars[0].vval.v_dict) != NULL
9711 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
9712 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009713 key = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009714 if (key != NULL)
9715 {
9716 di = dict_find(d, key, -1);
9717 if (di == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009718 semsg(_(e_dictkey), key);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009719 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
9720 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
9721 {
9722 *rettv = di->di_tv;
9723 init_tv(&di->di_tv);
9724 dictitem_remove(d, di);
9725 }
9726 }
9727 }
9728 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009729 else if (argvars[0].v_type == VAR_BLOB)
9730 {
9731 idx = (long)tv_get_number_chk(&argvars[1], &error);
9732 if (!error)
9733 {
9734 blob_T *b = argvars[0].vval.v_blob;
9735 int len = blob_len(b);
9736 char_u *p;
9737
9738 if (idx < 0)
9739 // count from the end
9740 idx = len + idx;
9741 if (idx < 0 || idx >= len)
9742 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009743 semsg(_(e_blobidx), idx);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009744 return;
9745 }
9746 if (argvars[2].v_type == VAR_UNKNOWN)
9747 {
9748 // Remove one item, return its value.
9749 p = (char_u *)b->bv_ga.ga_data;
9750 rettv->vval.v_number = (varnumber_T) *(p + idx);
9751 mch_memmove(p + idx, p + idx + 1, (size_t)len - idx - 1);
9752 --b->bv_ga.ga_len;
9753 }
9754 else
9755 {
9756 blob_T *blob;
9757
9758 // Remove range of items, return list with values.
9759 end = (long)tv_get_number_chk(&argvars[2], &error);
9760 if (error)
9761 return;
9762 if (end < 0)
9763 // count from the end
9764 end = len + end;
9765 if (end >= len || idx > end)
9766 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009767 semsg(_(e_blobidx), end);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009768 return;
9769 }
9770 blob = blob_alloc();
9771 if (blob == NULL)
9772 return;
9773 blob->bv_ga.ga_len = end - idx + 1;
9774 if (ga_grow(&blob->bv_ga, end - idx + 1) == FAIL)
9775 {
9776 vim_free(blob);
9777 return;
9778 }
9779 p = (char_u *)b->bv_ga.ga_data;
9780 mch_memmove((char_u *)blob->bv_ga.ga_data, p + idx,
9781 (size_t)(end - idx + 1));
9782 ++blob->bv_refcount;
9783 rettv->v_type = VAR_BLOB;
9784 rettv->vval.v_blob = blob;
9785
9786 mch_memmove(p + idx, p + end + 1, (size_t)(len - end));
9787 b->bv_ga.ga_len -= end - idx + 1;
9788 }
9789 }
9790 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009791 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009792 semsg(_(e_listdictarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009793 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009794 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009795 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009796 idx = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009797 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009798 ; // type error: do nothing, errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009799 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009800 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009801 else
9802 {
9803 if (argvars[2].v_type == VAR_UNKNOWN)
9804 {
9805 /* Remove one item, return its value. */
9806 vimlist_remove(l, item, item);
9807 *rettv = item->li_tv;
9808 vim_free(item);
9809 }
9810 else
9811 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009812 // Remove range of items, return list with values.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009813 end = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009814 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009815 ; // type error: do nothing
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009816 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009817 semsg(_(e_listidx), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009818 else
9819 {
9820 int cnt = 0;
9821
9822 for (li = item; li != NULL; li = li->li_next)
9823 {
9824 ++cnt;
9825 if (li == item2)
9826 break;
9827 }
9828 if (li == NULL) /* didn't find "item2" after "item" */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009829 emsg(_(e_invrange));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009830 else
9831 {
9832 vimlist_remove(l, item, item2);
9833 if (rettv_list_alloc(rettv) == OK)
9834 {
9835 l = rettv->vval.v_list;
9836 l->lv_first = item;
9837 l->lv_last = item2;
9838 item->li_prev = NULL;
9839 item2->li_next = NULL;
9840 l->lv_len = cnt;
9841 }
9842 }
9843 }
9844 }
9845 }
9846 }
9847}
9848
9849/*
9850 * "rename({from}, {to})" function
9851 */
9852 static void
9853f_rename(typval_T *argvars, typval_T *rettv)
9854{
9855 char_u buf[NUMBUFLEN];
9856
9857 if (check_restricted() || check_secure())
9858 rettv->vval.v_number = -1;
9859 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009860 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
9861 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009862}
9863
9864/*
9865 * "repeat()" function
9866 */
9867 static void
9868f_repeat(typval_T *argvars, typval_T *rettv)
9869{
9870 char_u *p;
9871 int n;
9872 int slen;
9873 int len;
9874 char_u *r;
9875 int i;
9876
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009877 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009878 if (argvars[0].v_type == VAR_LIST)
9879 {
9880 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9881 while (n-- > 0)
9882 if (list_extend(rettv->vval.v_list,
9883 argvars[0].vval.v_list, NULL) == FAIL)
9884 break;
9885 }
9886 else
9887 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009888 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009889 rettv->v_type = VAR_STRING;
9890 rettv->vval.v_string = NULL;
9891
9892 slen = (int)STRLEN(p);
9893 len = slen * n;
9894 if (len <= 0)
9895 return;
9896
9897 r = alloc(len + 1);
9898 if (r != NULL)
9899 {
9900 for (i = 0; i < n; i++)
9901 mch_memmove(r + i * slen, p, (size_t)slen);
9902 r[len] = NUL;
9903 }
9904
9905 rettv->vval.v_string = r;
9906 }
9907}
9908
9909/*
9910 * "resolve()" function
9911 */
9912 static void
9913f_resolve(typval_T *argvars, typval_T *rettv)
9914{
9915 char_u *p;
9916#ifdef HAVE_READLINK
9917 char_u *buf = NULL;
9918#endif
9919
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009920 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009921#ifdef FEAT_SHORTCUT
9922 {
9923 char_u *v = NULL;
9924
9925 v = mch_resolve_shortcut(p);
9926 if (v != NULL)
9927 rettv->vval.v_string = v;
9928 else
9929 rettv->vval.v_string = vim_strsave(p);
9930 }
9931#else
9932# ifdef HAVE_READLINK
9933 {
9934 char_u *cpy;
9935 int len;
9936 char_u *remain = NULL;
9937 char_u *q;
9938 int is_relative_to_current = FALSE;
9939 int has_trailing_pathsep = FALSE;
9940 int limit = 100;
9941
9942 p = vim_strsave(p);
9943
9944 if (p[0] == '.' && (vim_ispathsep(p[1])
9945 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9946 is_relative_to_current = TRUE;
9947
9948 len = STRLEN(p);
9949 if (len > 0 && after_pathsep(p, p + len))
9950 {
9951 has_trailing_pathsep = TRUE;
9952 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9953 }
9954
9955 q = getnextcomp(p);
9956 if (*q != NUL)
9957 {
9958 /* Separate the first path component in "p", and keep the
9959 * remainder (beginning with the path separator). */
9960 remain = vim_strsave(q - 1);
9961 q[-1] = NUL;
9962 }
9963
9964 buf = alloc(MAXPATHL + 1);
9965 if (buf == NULL)
9966 goto fail;
9967
9968 for (;;)
9969 {
9970 for (;;)
9971 {
9972 len = readlink((char *)p, (char *)buf, MAXPATHL);
9973 if (len <= 0)
9974 break;
9975 buf[len] = NUL;
9976
9977 if (limit-- == 0)
9978 {
9979 vim_free(p);
9980 vim_free(remain);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009981 emsg(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009982 rettv->vval.v_string = NULL;
9983 goto fail;
9984 }
9985
9986 /* Ensure that the result will have a trailing path separator
9987 * if the argument has one. */
9988 if (remain == NULL && has_trailing_pathsep)
9989 add_pathsep(buf);
9990
9991 /* Separate the first path component in the link value and
9992 * concatenate the remainders. */
9993 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9994 if (*q != NUL)
9995 {
9996 if (remain == NULL)
9997 remain = vim_strsave(q - 1);
9998 else
9999 {
10000 cpy = concat_str(q - 1, remain);
10001 if (cpy != NULL)
10002 {
10003 vim_free(remain);
10004 remain = cpy;
10005 }
10006 }
10007 q[-1] = NUL;
10008 }
10009
10010 q = gettail(p);
10011 if (q > p && *q == NUL)
10012 {
10013 /* Ignore trailing path separator. */
10014 q[-1] = NUL;
10015 q = gettail(p);
10016 }
10017 if (q > p && !mch_isFullName(buf))
10018 {
10019 /* symlink is relative to directory of argument */
10020 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
10021 if (cpy != NULL)
10022 {
10023 STRCPY(cpy, p);
10024 STRCPY(gettail(cpy), buf);
10025 vim_free(p);
10026 p = cpy;
10027 }
10028 }
10029 else
10030 {
10031 vim_free(p);
10032 p = vim_strsave(buf);
10033 }
10034 }
10035
10036 if (remain == NULL)
10037 break;
10038
10039 /* Append the first path component of "remain" to "p". */
10040 q = getnextcomp(remain + 1);
10041 len = q - remain - (*q != NUL);
10042 cpy = vim_strnsave(p, STRLEN(p) + len);
10043 if (cpy != NULL)
10044 {
10045 STRNCAT(cpy, remain, len);
10046 vim_free(p);
10047 p = cpy;
10048 }
10049 /* Shorten "remain". */
10050 if (*q != NUL)
10051 STRMOVE(remain, q - 1);
10052 else
Bram Moolenaard23a8232018-02-10 18:45:26 +010010053 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010054 }
10055
10056 /* If the result is a relative path name, make it explicitly relative to
10057 * the current directory if and only if the argument had this form. */
10058 if (!vim_ispathsep(*p))
10059 {
10060 if (is_relative_to_current
10061 && *p != NUL
10062 && !(p[0] == '.'
10063 && (p[1] == NUL
10064 || vim_ispathsep(p[1])
10065 || (p[1] == '.'
10066 && (p[2] == NUL
10067 || vim_ispathsep(p[2]))))))
10068 {
10069 /* Prepend "./". */
10070 cpy = concat_str((char_u *)"./", p);
10071 if (cpy != NULL)
10072 {
10073 vim_free(p);
10074 p = cpy;
10075 }
10076 }
10077 else if (!is_relative_to_current)
10078 {
10079 /* Strip leading "./". */
10080 q = p;
10081 while (q[0] == '.' && vim_ispathsep(q[1]))
10082 q += 2;
10083 if (q > p)
10084 STRMOVE(p, p + 2);
10085 }
10086 }
10087
10088 /* Ensure that the result will have no trailing path separator
10089 * if the argument had none. But keep "/" or "//". */
10090 if (!has_trailing_pathsep)
10091 {
10092 q = p + STRLEN(p);
10093 if (after_pathsep(p, q))
10094 *gettail_sep(p) = NUL;
10095 }
10096
10097 rettv->vval.v_string = p;
10098 }
10099# else
10100 rettv->vval.v_string = vim_strsave(p);
10101# endif
10102#endif
10103
10104 simplify_filename(rettv->vval.v_string);
10105
10106#ifdef HAVE_READLINK
10107fail:
10108 vim_free(buf);
10109#endif
10110 rettv->v_type = VAR_STRING;
10111}
10112
10113/*
10114 * "reverse({list})" function
10115 */
10116 static void
10117f_reverse(typval_T *argvars, typval_T *rettv)
10118{
10119 list_T *l;
10120 listitem_T *li, *ni;
10121
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010122 if (argvars[0].v_type == VAR_BLOB)
10123 {
10124 blob_T *b = argvars[0].vval.v_blob;
10125 int i, len = blob_len(b);
10126
10127 for (i = 0; i < len / 2; i++)
10128 {
10129 int tmp = blob_get(b, i);
10130
10131 blob_set(b, i, blob_get(b, len - i - 1));
10132 blob_set(b, len - i - 1, tmp);
10133 }
10134 rettv_blob_set(rettv, b);
10135 return;
10136 }
10137
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010138 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010139 semsg(_(e_listarg), "reverse()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010140 else if ((l = argvars[0].vval.v_list) != NULL
10141 && !tv_check_lock(l->lv_lock,
10142 (char_u *)N_("reverse() argument"), TRUE))
10143 {
10144 li = l->lv_last;
10145 l->lv_first = l->lv_last = NULL;
10146 l->lv_len = 0;
10147 while (li != NULL)
10148 {
10149 ni = li->li_prev;
10150 list_append(l, li);
10151 li = ni;
10152 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020010153 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010154 l->lv_idx = l->lv_len - l->lv_idx - 1;
10155 }
10156}
10157
10158#define SP_NOMOVE 0x01 /* don't move cursor */
10159#define SP_REPEAT 0x02 /* repeat to find outer pair */
10160#define SP_RETCOUNT 0x04 /* return matchcount */
10161#define SP_SETPCMARK 0x08 /* set previous context mark */
10162#define SP_START 0x10 /* accept match at start position */
10163#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
10164#define SP_END 0x40 /* leave cursor at end of match */
10165#define SP_COLUMN 0x80 /* start at cursor column */
10166
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010167/*
10168 * Get flags for a search function.
10169 * Possibly sets "p_ws".
10170 * Returns BACKWARD, FORWARD or zero (for an error).
10171 */
10172 static int
10173get_search_arg(typval_T *varp, int *flagsp)
10174{
10175 int dir = FORWARD;
10176 char_u *flags;
10177 char_u nbuf[NUMBUFLEN];
10178 int mask;
10179
10180 if (varp->v_type != VAR_UNKNOWN)
10181 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010182 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010183 if (flags == NULL)
10184 return 0; /* type error; errmsg already given */
10185 while (*flags != NUL)
10186 {
10187 switch (*flags)
10188 {
10189 case 'b': dir = BACKWARD; break;
10190 case 'w': p_ws = TRUE; break;
10191 case 'W': p_ws = FALSE; break;
10192 default: mask = 0;
10193 if (flagsp != NULL)
10194 switch (*flags)
10195 {
10196 case 'c': mask = SP_START; break;
10197 case 'e': mask = SP_END; break;
10198 case 'm': mask = SP_RETCOUNT; break;
10199 case 'n': mask = SP_NOMOVE; break;
10200 case 'p': mask = SP_SUBPAT; break;
10201 case 'r': mask = SP_REPEAT; break;
10202 case 's': mask = SP_SETPCMARK; break;
10203 case 'z': mask = SP_COLUMN; break;
10204 }
10205 if (mask == 0)
10206 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010207 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010208 dir = 0;
10209 }
10210 else
10211 *flagsp |= mask;
10212 }
10213 if (dir == 0)
10214 break;
10215 ++flags;
10216 }
10217 }
10218 return dir;
10219}
10220
10221/*
10222 * Shared by search() and searchpos() functions.
10223 */
10224 static int
10225search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
10226{
10227 int flags;
10228 char_u *pat;
10229 pos_T pos;
10230 pos_T save_cursor;
10231 int save_p_ws = p_ws;
10232 int dir;
10233 int retval = 0; /* default: FAIL */
10234 long lnum_stop = 0;
10235 proftime_T tm;
10236#ifdef FEAT_RELTIME
10237 long time_limit = 0;
10238#endif
10239 int options = SEARCH_KEEP;
10240 int subpatnum;
10241
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010242 pat = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010243 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
10244 if (dir == 0)
10245 goto theend;
10246 flags = *flagsp;
10247 if (flags & SP_START)
10248 options |= SEARCH_START;
10249 if (flags & SP_END)
10250 options |= SEARCH_END;
10251 if (flags & SP_COLUMN)
10252 options |= SEARCH_COL;
10253
10254 /* Optional arguments: line number to stop searching and timeout. */
10255 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
10256 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010257 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010258 if (lnum_stop < 0)
10259 goto theend;
10260#ifdef FEAT_RELTIME
10261 if (argvars[3].v_type != VAR_UNKNOWN)
10262 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010263 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010264 if (time_limit < 0)
10265 goto theend;
10266 }
10267#endif
10268 }
10269
10270#ifdef FEAT_RELTIME
10271 /* Set the time limit, if there is one. */
10272 profile_setlimit(time_limit, &tm);
10273#endif
10274
10275 /*
10276 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
10277 * Check to make sure only those flags are set.
10278 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
10279 * flags cannot be set. Check for that condition also.
10280 */
10281 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
10282 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10283 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010284 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010285 goto theend;
10286 }
10287
10288 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010289 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010290 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010291 if (subpatnum != FAIL)
10292 {
10293 if (flags & SP_SUBPAT)
10294 retval = subpatnum;
10295 else
10296 retval = pos.lnum;
10297 if (flags & SP_SETPCMARK)
10298 setpcmark();
10299 curwin->w_cursor = pos;
10300 if (match_pos != NULL)
10301 {
10302 /* Store the match cursor position */
10303 match_pos->lnum = pos.lnum;
10304 match_pos->col = pos.col + 1;
10305 }
10306 /* "/$" will put the cursor after the end of the line, may need to
10307 * correct that here */
10308 check_cursor();
10309 }
10310
10311 /* If 'n' flag is used: restore cursor position. */
10312 if (flags & SP_NOMOVE)
10313 curwin->w_cursor = save_cursor;
10314 else
10315 curwin->w_set_curswant = TRUE;
10316theend:
10317 p_ws = save_p_ws;
10318
10319 return retval;
10320}
10321
10322#ifdef FEAT_FLOAT
10323
10324/*
10325 * round() is not in C90, use ceil() or floor() instead.
10326 */
10327 float_T
10328vim_round(float_T f)
10329{
10330 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
10331}
10332
10333/*
10334 * "round({float})" function
10335 */
10336 static void
10337f_round(typval_T *argvars, typval_T *rettv)
10338{
10339 float_T f = 0.0;
10340
10341 rettv->v_type = VAR_FLOAT;
10342 if (get_float_arg(argvars, &f) == OK)
10343 rettv->vval.v_float = vim_round(f);
10344 else
10345 rettv->vval.v_float = 0.0;
10346}
10347#endif
10348
10349/*
10350 * "screenattr()" function
10351 */
10352 static void
10353f_screenattr(typval_T *argvars, typval_T *rettv)
10354{
10355 int row;
10356 int col;
10357 int c;
10358
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010359 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10360 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010361 if (row < 0 || row >= screen_Rows
10362 || col < 0 || col >= screen_Columns)
10363 c = -1;
10364 else
10365 c = ScreenAttrs[LineOffset[row] + col];
10366 rettv->vval.v_number = c;
10367}
10368
10369/*
10370 * "screenchar()" function
10371 */
10372 static void
10373f_screenchar(typval_T *argvars, typval_T *rettv)
10374{
10375 int row;
10376 int col;
10377 int off;
10378 int c;
10379
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010380 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10381 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010382 if (row < 0 || row >= screen_Rows
10383 || col < 0 || col >= screen_Columns)
10384 c = -1;
10385 else
10386 {
10387 off = LineOffset[row] + col;
10388#ifdef FEAT_MBYTE
10389 if (enc_utf8 && ScreenLinesUC[off] != 0)
10390 c = ScreenLinesUC[off];
10391 else
10392#endif
10393 c = ScreenLines[off];
10394 }
10395 rettv->vval.v_number = c;
10396}
10397
10398/*
10399 * "screencol()" function
10400 *
10401 * First column is 1 to be consistent with virtcol().
10402 */
10403 static void
10404f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10405{
10406 rettv->vval.v_number = screen_screencol() + 1;
10407}
10408
10409/*
10410 * "screenrow()" function
10411 */
10412 static void
10413f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10414{
10415 rettv->vval.v_number = screen_screenrow() + 1;
10416}
10417
10418/*
10419 * "search()" function
10420 */
10421 static void
10422f_search(typval_T *argvars, typval_T *rettv)
10423{
10424 int flags = 0;
10425
10426 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10427}
10428
10429/*
10430 * "searchdecl()" function
10431 */
10432 static void
10433f_searchdecl(typval_T *argvars, typval_T *rettv)
10434{
10435 int locally = 1;
10436 int thisblock = 0;
10437 int error = FALSE;
10438 char_u *name;
10439
10440 rettv->vval.v_number = 1; /* default: FAIL */
10441
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010442 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010443 if (argvars[1].v_type != VAR_UNKNOWN)
10444 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010445 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010446 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010447 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010448 }
10449 if (!error && name != NULL)
10450 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10451 locally, thisblock, SEARCH_KEEP) == FAIL;
10452}
10453
10454/*
10455 * Used by searchpair() and searchpairpos()
10456 */
10457 static int
10458searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10459{
10460 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010461 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010462 int save_p_ws = p_ws;
10463 int dir;
10464 int flags = 0;
10465 char_u nbuf1[NUMBUFLEN];
10466 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010467 int retval = 0; /* default: FAIL */
10468 long lnum_stop = 0;
10469 long time_limit = 0;
10470
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010471 /* Get the three pattern arguments: start, middle, end. Will result in an
10472 * error if not a valid argument. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010473 spat = tv_get_string_chk(&argvars[0]);
10474 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
10475 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010476 if (spat == NULL || mpat == NULL || epat == NULL)
10477 goto theend; /* type error */
10478
10479 /* Handle the optional fourth argument: flags */
10480 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
10481 if (dir == 0)
10482 goto theend;
10483
10484 /* Don't accept SP_END or SP_SUBPAT.
10485 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
10486 */
10487 if ((flags & (SP_END | SP_SUBPAT)) != 0
10488 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10489 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010490 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010491 goto theend;
10492 }
10493
10494 /* Using 'r' implies 'W', otherwise it doesn't work. */
10495 if (flags & SP_REPEAT)
10496 p_ws = FALSE;
10497
10498 /* Optional fifth argument: skip expression */
10499 if (argvars[3].v_type == VAR_UNKNOWN
10500 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010010501 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010502 else
10503 {
Bram Moolenaar48570482017-10-30 21:48:41 +010010504 skip = &argvars[4];
10505 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
10506 && skip->v_type != VAR_STRING)
10507 {
10508 /* Type error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010509 semsg(_(e_invarg2), tv_get_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010010510 goto theend;
10511 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010512 if (argvars[5].v_type != VAR_UNKNOWN)
10513 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010514 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010515 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010516 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010517 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010518 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010519 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010520#ifdef FEAT_RELTIME
10521 if (argvars[6].v_type != VAR_UNKNOWN)
10522 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010523 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010524 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010525 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010526 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010527 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010528 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010529 }
10530#endif
10531 }
10532 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010533
10534 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
10535 match_pos, lnum_stop, time_limit);
10536
10537theend:
10538 p_ws = save_p_ws;
10539
10540 return retval;
10541}
10542
10543/*
10544 * "searchpair()" function
10545 */
10546 static void
10547f_searchpair(typval_T *argvars, typval_T *rettv)
10548{
10549 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
10550}
10551
10552/*
10553 * "searchpairpos()" function
10554 */
10555 static void
10556f_searchpairpos(typval_T *argvars, typval_T *rettv)
10557{
10558 pos_T match_pos;
10559 int lnum = 0;
10560 int col = 0;
10561
10562 if (rettv_list_alloc(rettv) == FAIL)
10563 return;
10564
10565 if (searchpair_cmn(argvars, &match_pos) > 0)
10566 {
10567 lnum = match_pos.lnum;
10568 col = match_pos.col;
10569 }
10570
10571 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10572 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10573}
10574
10575/*
10576 * Search for a start/middle/end thing.
10577 * Used by searchpair(), see its documentation for the details.
10578 * Returns 0 or -1 for no match,
10579 */
10580 long
10581do_searchpair(
10582 char_u *spat, /* start pattern */
10583 char_u *mpat, /* middle pattern */
10584 char_u *epat, /* end pattern */
10585 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010010586 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010587 int flags, /* SP_SETPCMARK and other SP_ values */
10588 pos_T *match_pos,
10589 linenr_T lnum_stop, /* stop at this line if not zero */
10590 long time_limit UNUSED) /* stop after this many msec */
10591{
10592 char_u *save_cpo;
10593 char_u *pat, *pat2 = NULL, *pat3 = NULL;
10594 long retval = 0;
10595 pos_T pos;
10596 pos_T firstpos;
10597 pos_T foundpos;
10598 pos_T save_cursor;
10599 pos_T save_pos;
10600 int n;
10601 int r;
10602 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010010603 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010604 int err;
10605 int options = SEARCH_KEEP;
10606 proftime_T tm;
10607
10608 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10609 save_cpo = p_cpo;
10610 p_cpo = empty_option;
10611
10612#ifdef FEAT_RELTIME
10613 /* Set the time limit, if there is one. */
10614 profile_setlimit(time_limit, &tm);
10615#endif
10616
10617 /* Make two search patterns: start/end (pat2, for in nested pairs) and
10618 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010619 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
10620 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010621 if (pat2 == NULL || pat3 == NULL)
10622 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010623 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010624 if (*mpat == NUL)
10625 STRCPY(pat3, pat2);
10626 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010627 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010628 spat, epat, mpat);
10629 if (flags & SP_START)
10630 options |= SEARCH_START;
10631
Bram Moolenaar48570482017-10-30 21:48:41 +010010632 if (skip != NULL)
10633 {
10634 /* Empty string means to not use the skip expression. */
10635 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
10636 use_skip = skip->vval.v_string != NULL
10637 && *skip->vval.v_string != NUL;
10638 }
10639
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010640 save_cursor = curwin->w_cursor;
10641 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010642 CLEAR_POS(&firstpos);
10643 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010644 pat = pat3;
10645 for (;;)
10646 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010647 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010648 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010649 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010650 /* didn't find it or found the first match again: FAIL */
10651 break;
10652
10653 if (firstpos.lnum == 0)
10654 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010655 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010656 {
10657 /* Found the same position again. Can happen with a pattern that
10658 * has "\zs" at the end and searching backwards. Advance one
10659 * character and try again. */
10660 if (dir == BACKWARD)
10661 decl(&pos);
10662 else
10663 incl(&pos);
10664 }
10665 foundpos = pos;
10666
10667 /* clear the start flag to avoid getting stuck here */
10668 options &= ~SEARCH_START;
10669
10670 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010010671 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010672 {
10673 save_pos = curwin->w_cursor;
10674 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010010675 err = FALSE;
10676 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010677 curwin->w_cursor = save_pos;
10678 if (err)
10679 {
10680 /* Evaluating {skip} caused an error, break here. */
10681 curwin->w_cursor = save_cursor;
10682 retval = -1;
10683 break;
10684 }
10685 if (r)
10686 continue;
10687 }
10688
10689 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
10690 {
10691 /* Found end when searching backwards or start when searching
10692 * forward: nested pair. */
10693 ++nest;
10694 pat = pat2; /* nested, don't search for middle */
10695 }
10696 else
10697 {
10698 /* Found end when searching forward or start when searching
10699 * backward: end of (nested) pair; or found middle in outer pair. */
10700 if (--nest == 1)
10701 pat = pat3; /* outer level, search for middle */
10702 }
10703
10704 if (nest == 0)
10705 {
10706 /* Found the match: return matchcount or line number. */
10707 if (flags & SP_RETCOUNT)
10708 ++retval;
10709 else
10710 retval = pos.lnum;
10711 if (flags & SP_SETPCMARK)
10712 setpcmark();
10713 curwin->w_cursor = pos;
10714 if (!(flags & SP_REPEAT))
10715 break;
10716 nest = 1; /* search for next unmatched */
10717 }
10718 }
10719
10720 if (match_pos != NULL)
10721 {
10722 /* Store the match cursor position */
10723 match_pos->lnum = curwin->w_cursor.lnum;
10724 match_pos->col = curwin->w_cursor.col + 1;
10725 }
10726
10727 /* If 'n' flag is used or search failed: restore cursor position. */
10728 if ((flags & SP_NOMOVE) || retval == 0)
10729 curwin->w_cursor = save_cursor;
10730
10731theend:
10732 vim_free(pat2);
10733 vim_free(pat3);
10734 if (p_cpo == empty_option)
10735 p_cpo = save_cpo;
10736 else
10737 /* Darn, evaluating the {skip} expression changed the value. */
10738 free_string_option(save_cpo);
10739
10740 return retval;
10741}
10742
10743/*
10744 * "searchpos()" function
10745 */
10746 static void
10747f_searchpos(typval_T *argvars, typval_T *rettv)
10748{
10749 pos_T match_pos;
10750 int lnum = 0;
10751 int col = 0;
10752 int n;
10753 int flags = 0;
10754
10755 if (rettv_list_alloc(rettv) == FAIL)
10756 return;
10757
10758 n = search_cmn(argvars, &match_pos, &flags);
10759 if (n > 0)
10760 {
10761 lnum = match_pos.lnum;
10762 col = match_pos.col;
10763 }
10764
10765 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10766 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10767 if (flags & SP_SUBPAT)
10768 list_append_number(rettv->vval.v_list, (varnumber_T)n);
10769}
10770
10771 static void
10772f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
10773{
10774#ifdef FEAT_CLIENTSERVER
10775 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010776 char_u *server = tv_get_string_chk(&argvars[0]);
10777 char_u *reply = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010778
10779 rettv->vval.v_number = -1;
10780 if (server == NULL || reply == NULL)
10781 return;
10782 if (check_restricted() || check_secure())
10783 return;
10784# ifdef FEAT_X11
10785 if (check_connection() == FAIL)
10786 return;
10787# endif
10788
10789 if (serverSendReply(server, reply) < 0)
10790 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010791 emsg(_("E258: Unable to send to client"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010792 return;
10793 }
10794 rettv->vval.v_number = 0;
10795#else
10796 rettv->vval.v_number = -1;
10797#endif
10798}
10799
10800 static void
10801f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
10802{
10803 char_u *r = NULL;
10804
10805#ifdef FEAT_CLIENTSERVER
10806# ifdef WIN32
10807 r = serverGetVimNames();
10808# else
10809 make_connection();
10810 if (X_DISPLAY != NULL)
10811 r = serverGetVimNames(X_DISPLAY);
10812# endif
10813#endif
10814 rettv->v_type = VAR_STRING;
10815 rettv->vval.v_string = r;
10816}
10817
10818/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010819 * "setbufline()" function
10820 */
10821 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020010822f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010823{
10824 linenr_T lnum;
10825 buf_T *buf;
10826
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010010827 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010828 if (buf == NULL)
10829 rettv->vval.v_number = 1; /* FAIL */
10830 else
10831 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010832 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020010833 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010834 }
10835}
10836
10837/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010838 * "setbufvar()" function
10839 */
10840 static void
10841f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10842{
10843 buf_T *buf;
10844 char_u *varname, *bufvarname;
10845 typval_T *varp;
10846 char_u nbuf[NUMBUFLEN];
10847
10848 if (check_restricted() || check_secure())
10849 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010850 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
10851 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010010852 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010853 varp = &argvars[2];
10854
10855 if (buf != NULL && varname != NULL && varp != NULL)
10856 {
10857 if (*varname == '&')
10858 {
10859 long numval;
10860 char_u *strval;
10861 int error = FALSE;
10862 aco_save_T aco;
10863
10864 /* set curbuf to be our buf, temporarily */
10865 aucmd_prepbuf(&aco, buf);
10866
10867 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010868 numval = (long)tv_get_number_chk(varp, &error);
10869 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010870 if (!error && strval != NULL)
10871 set_option_value(varname, numval, strval, OPT_LOCAL);
10872
10873 /* reset notion of buffer */
10874 aucmd_restbuf(&aco);
10875 }
10876 else
10877 {
10878 buf_T *save_curbuf = curbuf;
10879
10880 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
10881 if (bufvarname != NULL)
10882 {
10883 curbuf = buf;
10884 STRCPY(bufvarname, "b:");
10885 STRCPY(bufvarname + 2, varname);
10886 set_var(bufvarname, varp, TRUE);
10887 vim_free(bufvarname);
10888 curbuf = save_curbuf;
10889 }
10890 }
10891 }
10892}
10893
10894 static void
10895f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10896{
10897 dict_T *d;
10898 dictitem_T *di;
10899 char_u *csearch;
10900
10901 if (argvars[0].v_type != VAR_DICT)
10902 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010903 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010904 return;
10905 }
10906
10907 if ((d = argvars[0].vval.v_dict) != NULL)
10908 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010010909 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010910 if (csearch != NULL)
10911 {
10912#ifdef FEAT_MBYTE
10913 if (enc_utf8)
10914 {
10915 int pcc[MAX_MCO];
10916 int c = utfc_ptr2char(csearch, pcc);
10917
10918 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
10919 }
10920 else
10921#endif
10922 set_last_csearch(PTR2CHAR(csearch),
10923 csearch, MB_PTR2LEN(csearch));
10924 }
10925
10926 di = dict_find(d, (char_u *)"forward", -1);
10927 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010928 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010929 ? FORWARD : BACKWARD);
10930
10931 di = dict_find(d, (char_u *)"until", -1);
10932 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010933 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010934 }
10935}
10936
10937/*
10938 * "setcmdpos()" function
10939 */
10940 static void
10941f_setcmdpos(typval_T *argvars, typval_T *rettv)
10942{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010943 int pos = (int)tv_get_number(&argvars[0]) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010944
10945 if (pos >= 0)
10946 rettv->vval.v_number = set_cmdline_pos(pos);
10947}
10948
10949/*
10950 * "setfperm({fname}, {mode})" function
10951 */
10952 static void
10953f_setfperm(typval_T *argvars, typval_T *rettv)
10954{
10955 char_u *fname;
10956 char_u modebuf[NUMBUFLEN];
10957 char_u *mode_str;
10958 int i;
10959 int mask;
10960 int mode = 0;
10961
10962 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010963 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010964 if (fname == NULL)
10965 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010966 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010967 if (mode_str == NULL)
10968 return;
10969 if (STRLEN(mode_str) != 9)
10970 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010971 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010972 return;
10973 }
10974
10975 mask = 1;
10976 for (i = 8; i >= 0; --i)
10977 {
10978 if (mode_str[i] != '-')
10979 mode |= mask;
10980 mask = mask << 1;
10981 }
10982 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
10983}
10984
10985/*
10986 * "setline()" function
10987 */
10988 static void
10989f_setline(typval_T *argvars, typval_T *rettv)
10990{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010991 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010992
Bram Moolenaarca851592018-06-06 21:04:07 +020010993 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010994}
10995
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010996/*
10997 * Used by "setqflist()" and "setloclist()" functions
10998 */
10999 static void
11000set_qf_ll_list(
11001 win_T *wp UNUSED,
11002 typval_T *list_arg UNUSED,
11003 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020011004 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011005 typval_T *rettv)
11006{
11007#ifdef FEAT_QUICKFIX
11008 static char *e_invact = N_("E927: Invalid action: '%s'");
11009 char_u *act;
11010 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011011 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011012#endif
11013
11014 rettv->vval.v_number = -1;
11015
11016#ifdef FEAT_QUICKFIX
11017 if (list_arg->v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011018 emsg(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011019 else if (recursive != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011020 emsg(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011021 else
11022 {
11023 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011024 dict_T *d = NULL;
11025 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011026
11027 if (action_arg->v_type == VAR_STRING)
11028 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011029 act = tv_get_string_chk(action_arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011030 if (act == NULL)
11031 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020011032 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
11033 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011034 action = *act;
11035 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011036 semsg(_(e_invact), act);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011037 }
11038 else if (action_arg->v_type == VAR_UNKNOWN)
11039 action = ' ';
11040 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011041 emsg(_(e_stringreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011042
Bram Moolenaard823fa92016-08-12 16:29:27 +020011043 if (action_arg->v_type != VAR_UNKNOWN
11044 && what_arg->v_type != VAR_UNKNOWN)
11045 {
11046 if (what_arg->v_type == VAR_DICT)
11047 d = what_arg->vval.v_dict;
11048 else
11049 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011050 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +020011051 valid_dict = FALSE;
11052 }
11053 }
11054
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011055 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011056 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011057 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
11058 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011059 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011060 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011061 }
11062#endif
11063}
11064
11065/*
11066 * "setloclist()" function
11067 */
11068 static void
11069f_setloclist(typval_T *argvars, typval_T *rettv)
11070{
11071 win_T *win;
11072
11073 rettv->vval.v_number = -1;
11074
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020011075 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011076 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020011077 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011078}
11079
11080/*
11081 * "setmatches()" function
11082 */
11083 static void
11084f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
11085{
11086#ifdef FEAT_SEARCH_EXTRA
11087 list_T *l;
11088 listitem_T *li;
11089 dict_T *d;
11090 list_T *s = NULL;
11091
11092 rettv->vval.v_number = -1;
11093 if (argvars[0].v_type != VAR_LIST)
11094 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011095 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011096 return;
11097 }
11098 if ((l = argvars[0].vval.v_list) != NULL)
11099 {
11100
11101 /* To some extent make sure that we are dealing with a list from
11102 * "getmatches()". */
11103 li = l->lv_first;
11104 while (li != NULL)
11105 {
11106 if (li->li_tv.v_type != VAR_DICT
11107 || (d = li->li_tv.vval.v_dict) == NULL)
11108 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011109 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011110 return;
11111 }
11112 if (!(dict_find(d, (char_u *)"group", -1) != NULL
11113 && (dict_find(d, (char_u *)"pattern", -1) != NULL
11114 || dict_find(d, (char_u *)"pos1", -1) != NULL)
11115 && dict_find(d, (char_u *)"priority", -1) != NULL
11116 && dict_find(d, (char_u *)"id", -1) != NULL))
11117 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011118 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011119 return;
11120 }
11121 li = li->li_next;
11122 }
11123
11124 clear_matches(curwin);
11125 li = l->lv_first;
11126 while (li != NULL)
11127 {
11128 int i = 0;
11129 char_u buf[5];
11130 dictitem_T *di;
11131 char_u *group;
11132 int priority;
11133 int id;
11134 char_u *conceal;
11135
11136 d = li->li_tv.vval.v_dict;
11137 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
11138 {
11139 if (s == NULL)
11140 {
11141 s = list_alloc();
11142 if (s == NULL)
11143 return;
11144 }
11145
11146 /* match from matchaddpos() */
11147 for (i = 1; i < 9; i++)
11148 {
11149 sprintf((char *)buf, (char *)"pos%d", i);
11150 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
11151 {
11152 if (di->di_tv.v_type != VAR_LIST)
11153 return;
11154
11155 list_append_tv(s, &di->di_tv);
11156 s->lv_refcount++;
11157 }
11158 else
11159 break;
11160 }
11161 }
11162
Bram Moolenaar8f667172018-12-14 15:38:31 +010011163 group = dict_get_string(d, (char_u *)"group", TRUE);
11164 priority = (int)dict_get_number(d, (char_u *)"priority");
11165 id = (int)dict_get_number(d, (char_u *)"id");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011166 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar8f667172018-12-14 15:38:31 +010011167 ? dict_get_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011168 : NULL;
11169 if (i == 0)
11170 {
11171 match_add(curwin, group,
Bram Moolenaar8f667172018-12-14 15:38:31 +010011172 dict_get_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011173 priority, id, NULL, conceal);
11174 }
11175 else
11176 {
11177 match_add(curwin, group, NULL, priority, id, s, conceal);
11178 list_unref(s);
11179 s = NULL;
11180 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020011181 vim_free(group);
11182 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011183
11184 li = li->li_next;
11185 }
11186 rettv->vval.v_number = 0;
11187 }
11188#endif
11189}
11190
11191/*
11192 * "setpos()" function
11193 */
11194 static void
11195f_setpos(typval_T *argvars, typval_T *rettv)
11196{
11197 pos_T pos;
11198 int fnum;
11199 char_u *name;
11200 colnr_T curswant = -1;
11201
11202 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011203 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011204 if (name != NULL)
11205 {
11206 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
11207 {
11208 if (--pos.col < 0)
11209 pos.col = 0;
11210 if (name[0] == '.' && name[1] == NUL)
11211 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011212 /* set cursor; "fnum" is ignored */
11213 curwin->w_cursor = pos;
11214 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011215 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011216 curwin->w_curswant = curswant - 1;
11217 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011218 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011219 check_cursor();
11220 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011221 }
11222 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
11223 {
11224 /* set mark */
11225 if (setmark_pos(name[1], &pos, fnum) == OK)
11226 rettv->vval.v_number = 0;
11227 }
11228 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011229 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011230 }
11231 }
11232}
11233
11234/*
11235 * "setqflist()" function
11236 */
11237 static void
11238f_setqflist(typval_T *argvars, typval_T *rettv)
11239{
Bram Moolenaard823fa92016-08-12 16:29:27 +020011240 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011241}
11242
11243/*
11244 * "setreg()" function
11245 */
11246 static void
11247f_setreg(typval_T *argvars, typval_T *rettv)
11248{
11249 int regname;
11250 char_u *strregname;
11251 char_u *stropt;
11252 char_u *strval;
11253 int append;
11254 char_u yank_type;
11255 long block_len;
11256
11257 block_len = -1;
11258 yank_type = MAUTO;
11259 append = FALSE;
11260
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011261 strregname = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011262 rettv->vval.v_number = 1; /* FAIL is default */
11263
11264 if (strregname == NULL)
11265 return; /* type error; errmsg already given */
11266 regname = *strregname;
11267 if (regname == 0 || regname == '@')
11268 regname = '"';
11269
11270 if (argvars[2].v_type != VAR_UNKNOWN)
11271 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011272 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011273 if (stropt == NULL)
11274 return; /* type error */
11275 for (; *stropt != NUL; ++stropt)
11276 switch (*stropt)
11277 {
11278 case 'a': case 'A': /* append */
11279 append = TRUE;
11280 break;
11281 case 'v': case 'c': /* character-wise selection */
11282 yank_type = MCHAR;
11283 break;
11284 case 'V': case 'l': /* line-wise selection */
11285 yank_type = MLINE;
11286 break;
11287 case 'b': case Ctrl_V: /* block-wise selection */
11288 yank_type = MBLOCK;
11289 if (VIM_ISDIGIT(stropt[1]))
11290 {
11291 ++stropt;
11292 block_len = getdigits(&stropt) - 1;
11293 --stropt;
11294 }
11295 break;
11296 }
11297 }
11298
11299 if (argvars[1].v_type == VAR_LIST)
11300 {
11301 char_u **lstval;
11302 char_u **allocval;
11303 char_u buf[NUMBUFLEN];
11304 char_u **curval;
11305 char_u **curallocval;
11306 list_T *ll = argvars[1].vval.v_list;
11307 listitem_T *li;
11308 int len;
11309
11310 /* If the list is NULL handle like an empty list. */
11311 len = ll == NULL ? 0 : ll->lv_len;
11312
11313 /* First half: use for pointers to result lines; second half: use for
11314 * pointers to allocated copies. */
11315 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
11316 if (lstval == NULL)
11317 return;
11318 curval = lstval;
11319 allocval = lstval + len + 2;
11320 curallocval = allocval;
11321
11322 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
11323 li = li->li_next)
11324 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011325 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011326 if (strval == NULL)
11327 goto free_lstval;
11328 if (strval == buf)
11329 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011330 /* Need to make a copy, next tv_get_string_buf_chk() will
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011331 * overwrite the string. */
11332 strval = vim_strsave(buf);
11333 if (strval == NULL)
11334 goto free_lstval;
11335 *curallocval++ = strval;
11336 }
11337 *curval++ = strval;
11338 }
11339 *curval++ = NULL;
11340
11341 write_reg_contents_lst(regname, lstval, -1,
11342 append, yank_type, block_len);
11343free_lstval:
11344 while (curallocval > allocval)
11345 vim_free(*--curallocval);
11346 vim_free(lstval);
11347 }
11348 else
11349 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011350 strval = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011351 if (strval == NULL)
11352 return;
11353 write_reg_contents_ex(regname, strval, -1,
11354 append, yank_type, block_len);
11355 }
11356 rettv->vval.v_number = 0;
11357}
11358
11359/*
11360 * "settabvar()" function
11361 */
11362 static void
11363f_settabvar(typval_T *argvars, typval_T *rettv)
11364{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011365 tabpage_T *save_curtab;
11366 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011367 char_u *varname, *tabvarname;
11368 typval_T *varp;
11369
11370 rettv->vval.v_number = 0;
11371
11372 if (check_restricted() || check_secure())
11373 return;
11374
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011375 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
11376 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011377 varp = &argvars[2];
11378
Bram Moolenaar4033c552017-09-16 20:54:51 +020011379 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011380 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011381 save_curtab = curtab;
11382 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011383
11384 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
11385 if (tabvarname != NULL)
11386 {
11387 STRCPY(tabvarname, "t:");
11388 STRCPY(tabvarname + 2, varname);
11389 set_var(tabvarname, varp, TRUE);
11390 vim_free(tabvarname);
11391 }
11392
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011393 /* Restore current tabpage */
11394 if (valid_tabpage(save_curtab))
11395 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011396 }
11397}
11398
11399/*
11400 * "settabwinvar()" function
11401 */
11402 static void
11403f_settabwinvar(typval_T *argvars, typval_T *rettv)
11404{
11405 setwinvar(argvars, rettv, 1);
11406}
11407
11408/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011409 * "settagstack()" function
11410 */
11411 static void
11412f_settagstack(typval_T *argvars, typval_T *rettv)
11413{
11414 static char *e_invact2 = N_("E962: Invalid action: '%s'");
11415 win_T *wp;
11416 dict_T *d;
11417 int action = 'r';
11418
11419 rettv->vval.v_number = -1;
11420
11421 // first argument: window number or id
11422 wp = find_win_by_nr_or_id(&argvars[0]);
11423 if (wp == NULL)
11424 return;
11425
11426 // second argument: dict with items to set in the tag stack
11427 if (argvars[1].v_type != VAR_DICT)
11428 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011429 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011430 return;
11431 }
11432 d = argvars[1].vval.v_dict;
11433 if (d == NULL)
11434 return;
11435
11436 // third argument: action - 'a' for append and 'r' for replace.
11437 // default is to replace the stack.
11438 if (argvars[2].v_type == VAR_UNKNOWN)
11439 action = 'r';
11440 else if (argvars[2].v_type == VAR_STRING)
11441 {
11442 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011443 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011444 if (actstr == NULL)
11445 return;
11446 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
11447 action = *actstr;
11448 else
11449 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011450 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011451 return;
11452 }
11453 }
11454 else
11455 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011456 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011457 return;
11458 }
11459
11460 if (set_tagstack(wp, d, action) == OK)
11461 rettv->vval.v_number = 0;
11462}
11463
11464/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011465 * "setwinvar()" function
11466 */
11467 static void
11468f_setwinvar(typval_T *argvars, typval_T *rettv)
11469{
11470 setwinvar(argvars, rettv, 0);
11471}
11472
11473#ifdef FEAT_CRYPT
11474/*
11475 * "sha256({string})" function
11476 */
11477 static void
11478f_sha256(typval_T *argvars, typval_T *rettv)
11479{
11480 char_u *p;
11481
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011482 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011483 rettv->vval.v_string = vim_strsave(
11484 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
11485 rettv->v_type = VAR_STRING;
11486}
11487#endif /* FEAT_CRYPT */
11488
11489/*
11490 * "shellescape({string})" function
11491 */
11492 static void
11493f_shellescape(typval_T *argvars, typval_T *rettv)
11494{
Bram Moolenaar20615522017-06-05 18:46:26 +020011495 int do_special = non_zero_arg(&argvars[1]);
11496
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011497 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011498 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011499 rettv->v_type = VAR_STRING;
11500}
11501
11502/*
11503 * shiftwidth() function
11504 */
11505 static void
11506f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
11507{
Bram Moolenaarf9514162018-11-22 03:08:29 +010011508 rettv->vval.v_number = 0;
11509
11510 if (argvars[0].v_type != VAR_UNKNOWN)
11511 {
11512 long col;
11513
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011514 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +010011515 if (col < 0)
11516 return; // type error; errmsg already given
11517#ifdef FEAT_VARTABS
11518 rettv->vval.v_number = get_sw_value_col(curbuf, col);
11519 return;
11520#endif
11521 }
11522
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011523 rettv->vval.v_number = get_sw_value(curbuf);
11524}
11525
Bram Moolenaar162b7142018-12-21 15:17:36 +010011526#ifdef FEAT_SIGNS
11527/*
11528 * "sign_define()" function
11529 */
11530 static void
11531f_sign_define(typval_T *argvars, typval_T *rettv)
11532{
11533 char_u *name;
11534 dict_T *dict;
11535 char_u *icon = NULL;
11536 char_u *linehl = NULL;
11537 char_u *text = NULL;
11538 char_u *texthl = NULL;
11539
11540 rettv->vval.v_number = -1;
11541
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011542 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011543 if (name == NULL)
11544 return;
11545
11546 if (argvars[1].v_type != VAR_UNKNOWN)
11547 {
11548 if (argvars[1].v_type != VAR_DICT)
11549 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011550 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011551 return;
11552 }
11553
11554 // sign attributes
11555 dict = argvars[1].vval.v_dict;
11556 if (dict_find(dict, (char_u *)"icon", -1) != NULL)
11557 icon = dict_get_string(dict, (char_u *)"icon", TRUE);
11558 if (dict_find(dict, (char_u *)"linehl", -1) != NULL)
11559 linehl = dict_get_string(dict, (char_u *)"linehl", TRUE);
11560 if (dict_find(dict, (char_u *)"text", -1) != NULL)
11561 text = dict_get_string(dict, (char_u *)"text", TRUE);
11562 if (dict_find(dict, (char_u *)"texthl", -1) != NULL)
11563 texthl = dict_get_string(dict, (char_u *)"texthl", TRUE);
11564 }
11565
11566 if (sign_define_by_name(name, icon, linehl, text, texthl) == OK)
11567 rettv->vval.v_number = 0;
11568
11569 vim_free(icon);
11570 vim_free(linehl);
11571 vim_free(text);
11572 vim_free(texthl);
11573}
11574
11575/*
11576 * "sign_getdefined()" function
11577 */
11578 static void
11579f_sign_getdefined(typval_T *argvars, typval_T *rettv)
11580{
11581 char_u *name = NULL;
11582
11583 if (rettv_list_alloc_id(rettv, aid_sign_getdefined) != OK)
11584 return;
11585
11586 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011587 name = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011588
11589 sign_getlist(name, rettv->vval.v_list);
11590}
11591
11592/*
11593 * "sign_getplaced()" function
11594 */
11595 static void
11596f_sign_getplaced(typval_T *argvars, typval_T *rettv)
11597{
11598 buf_T *buf = NULL;
11599 dict_T *dict;
11600 dictitem_T *di;
11601 linenr_T lnum = 0;
11602 int sign_id = 0;
11603 char_u *group = NULL;
11604 int notanum = FALSE;
11605
11606 if (rettv_list_alloc_id(rettv, aid_sign_getplaced) != OK)
11607 return;
11608
11609 if (argvars[0].v_type != VAR_UNKNOWN)
11610 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010011611 // get signs placed in the specified buffer
11612 buf = get_buf_arg(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011613 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010011614 return;
Bram Moolenaar162b7142018-12-21 15:17:36 +010011615
11616 if (argvars[1].v_type != VAR_UNKNOWN)
11617 {
11618 if (argvars[1].v_type != VAR_DICT ||
11619 ((dict = argvars[1].vval.v_dict) == NULL))
11620 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011621 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011622 return;
11623 }
11624 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
11625 {
11626 // get signs placed at this line
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011627 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011628 if (notanum)
11629 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011630 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011631 }
11632 if ((di = dict_find(dict, (char_u *)"id", -1)) != NULL)
11633 {
11634 // get sign placed with this identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011635 sign_id = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011636 if (notanum)
11637 return;
11638 }
11639 if ((di = dict_find(dict, (char_u *)"group", -1)) != NULL)
11640 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011641 group = tv_get_string_chk(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011642 if (group == NULL)
11643 return;
Bram Moolenaar6436cd82018-12-27 00:28:33 +010011644 if (*group == '\0') // empty string means global group
11645 group = NULL;
Bram Moolenaar162b7142018-12-21 15:17:36 +010011646 }
11647 }
11648 }
11649
11650 sign_get_placed(buf, lnum, sign_id, group, rettv->vval.v_list);
11651}
11652
11653/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010011654 * "sign_jump()" function
11655 */
11656 static void
11657f_sign_jump(typval_T *argvars, typval_T *rettv)
11658{
11659 int sign_id;
11660 char_u *sign_group = NULL;
11661 buf_T *buf;
11662 int notanum = FALSE;
11663
11664 rettv->vval.v_number = -1;
11665
11666 // Sign identifer
11667 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
11668 if (notanum)
11669 return;
11670 if (sign_id <= 0)
11671 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011672 emsg(_(e_invarg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010011673 return;
11674 }
11675
11676 // Sign group
11677 sign_group = tv_get_string_chk(&argvars[1]);
11678 if (sign_group == NULL)
11679 return;
11680 if (sign_group[0] == '\0')
11681 sign_group = NULL; // global sign group
11682 else
11683 {
11684 sign_group = vim_strsave(sign_group);
11685 if (sign_group == NULL)
11686 return;
11687 }
11688
11689 // Buffer to place the sign
11690 buf = get_buf_arg(&argvars[2]);
11691 if (buf == NULL)
11692 goto cleanup;
11693
11694 rettv->vval.v_number = sign_jump(sign_id, sign_group, buf);
11695
11696cleanup:
11697 vim_free(sign_group);
11698}
11699
11700/*
Bram Moolenaar162b7142018-12-21 15:17:36 +010011701 * "sign_place()" function
11702 */
11703 static void
11704f_sign_place(typval_T *argvars, typval_T *rettv)
11705{
11706 int sign_id;
11707 char_u *group = NULL;
11708 char_u *sign_name;
11709 buf_T *buf;
11710 dict_T *dict;
11711 dictitem_T *di;
11712 linenr_T lnum = 0;
11713 int prio = SIGN_DEF_PRIO;
11714 int notanum = FALSE;
11715
11716 rettv->vval.v_number = -1;
11717
11718 // Sign identifer
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011719 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011720 if (notanum)
11721 return;
11722 if (sign_id < 0)
11723 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011724 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011725 return;
11726 }
11727
11728 // Sign group
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011729 group = tv_get_string_chk(&argvars[1]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011730 if (group == NULL)
11731 return;
11732 if (group[0] == '\0')
11733 group = NULL; // global sign group
11734 else
11735 {
11736 group = vim_strsave(group);
11737 if (group == NULL)
11738 return;
11739 }
11740
11741 // Sign name
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011742 sign_name = tv_get_string_chk(&argvars[2]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011743 if (sign_name == NULL)
11744 goto cleanup;
11745
11746 // Buffer to place the sign
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010011747 buf = get_buf_arg(&argvars[3]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011748 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010011749 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010011750
11751 if (argvars[4].v_type != VAR_UNKNOWN)
11752 {
11753 if (argvars[4].v_type != VAR_DICT ||
11754 ((dict = argvars[4].vval.v_dict) == NULL))
11755 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011756 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011757 goto cleanup;
11758 }
11759
11760 // Line number where the sign is to be placed
11761 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
11762 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011763 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011764 if (notanum)
11765 goto cleanup;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011766 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011767 }
11768 if ((di = dict_find(dict, (char_u *)"priority", -1)) != NULL)
11769 {
11770 // Sign priority
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011771 prio = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011772 if (notanum)
11773 goto cleanup;
11774 }
11775 }
11776
11777 if (sign_place(&sign_id, group, sign_name, buf, lnum, prio) == OK)
11778 rettv->vval.v_number = sign_id;
11779
11780cleanup:
11781 vim_free(group);
11782}
11783
11784/*
11785 * "sign_undefine()" function
11786 */
11787 static void
11788f_sign_undefine(typval_T *argvars, typval_T *rettv)
11789{
11790 char_u *name;
11791
11792 rettv->vval.v_number = -1;
11793
11794 if (argvars[0].v_type == VAR_UNKNOWN)
11795 {
11796 // Free all the signs
11797 free_signs();
11798 rettv->vval.v_number = 0;
11799 }
11800 else
11801 {
11802 // Free only the specified sign
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011803 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011804 if (name == NULL)
11805 return;
11806
11807 if (sign_undefine_by_name(name) == OK)
11808 rettv->vval.v_number = 0;
11809 }
11810}
11811
11812/*
11813 * "sign_unplace()" function
11814 */
11815 static void
11816f_sign_unplace(typval_T *argvars, typval_T *rettv)
11817{
11818 dict_T *dict;
11819 dictitem_T *di;
11820 int sign_id = 0;
11821 buf_T *buf = NULL;
11822 char_u *group = NULL;
11823
11824 rettv->vval.v_number = -1;
11825
11826 if (argvars[0].v_type != VAR_STRING)
11827 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011828 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011829 return;
11830 }
11831
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011832 group = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011833 if (group[0] == '\0')
11834 group = NULL; // global sign group
11835 else
11836 {
11837 group = vim_strsave(group);
11838 if (group == NULL)
11839 return;
11840 }
11841
11842 if (argvars[1].v_type != VAR_UNKNOWN)
11843 {
11844 if (argvars[1].v_type != VAR_DICT)
11845 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011846 emsg(_(e_dictreq));
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010011847 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010011848 }
11849 dict = argvars[1].vval.v_dict;
11850
11851 if ((di = dict_find(dict, (char_u *)"buffer", -1)) != NULL)
11852 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010011853 buf = get_buf_arg(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011854 if (buf == NULL)
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010011855 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010011856 }
11857 if (dict_find(dict, (char_u *)"id", -1) != NULL)
11858 sign_id = dict_get_number(dict, (char_u *)"id");
11859 }
11860
11861 if (buf == NULL)
11862 {
11863 // Delete the sign in all the buffers
11864 FOR_ALL_BUFFERS(buf)
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010011865 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010011866 rettv->vval.v_number = 0;
11867 }
11868 else
11869 {
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010011870 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010011871 rettv->vval.v_number = 0;
11872 }
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010011873
11874cleanup:
Bram Moolenaar162b7142018-12-21 15:17:36 +010011875 vim_free(group);
11876}
11877#endif
11878
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011879/*
11880 * "simplify()" function
11881 */
11882 static void
11883f_simplify(typval_T *argvars, typval_T *rettv)
11884{
11885 char_u *p;
11886
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011887 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011888 rettv->vval.v_string = vim_strsave(p);
11889 simplify_filename(rettv->vval.v_string); /* simplify in place */
11890 rettv->v_type = VAR_STRING;
11891}
11892
11893#ifdef FEAT_FLOAT
11894/*
11895 * "sin()" function
11896 */
11897 static void
11898f_sin(typval_T *argvars, typval_T *rettv)
11899{
11900 float_T f = 0.0;
11901
11902 rettv->v_type = VAR_FLOAT;
11903 if (get_float_arg(argvars, &f) == OK)
11904 rettv->vval.v_float = sin(f);
11905 else
11906 rettv->vval.v_float = 0.0;
11907}
11908
11909/*
11910 * "sinh()" function
11911 */
11912 static void
11913f_sinh(typval_T *argvars, typval_T *rettv)
11914{
11915 float_T f = 0.0;
11916
11917 rettv->v_type = VAR_FLOAT;
11918 if (get_float_arg(argvars, &f) == OK)
11919 rettv->vval.v_float = sinh(f);
11920 else
11921 rettv->vval.v_float = 0.0;
11922}
11923#endif
11924
11925static int
11926#ifdef __BORLANDC__
11927 _RTLENTRYF
11928#endif
11929 item_compare(const void *s1, const void *s2);
11930static int
11931#ifdef __BORLANDC__
11932 _RTLENTRYF
11933#endif
11934 item_compare2(const void *s1, const void *s2);
11935
11936/* struct used in the array that's given to qsort() */
11937typedef struct
11938{
11939 listitem_T *item;
11940 int idx;
11941} sortItem_T;
11942
11943/* struct storing information about current sort */
11944typedef struct
11945{
11946 int item_compare_ic;
11947 int item_compare_numeric;
11948 int item_compare_numbers;
11949#ifdef FEAT_FLOAT
11950 int item_compare_float;
11951#endif
11952 char_u *item_compare_func;
11953 partial_T *item_compare_partial;
11954 dict_T *item_compare_selfdict;
11955 int item_compare_func_err;
11956 int item_compare_keep_zero;
11957} sortinfo_T;
11958static sortinfo_T *sortinfo = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011959#define ITEM_COMPARE_FAIL 999
11960
11961/*
11962 * Compare functions for f_sort() and f_uniq() below.
11963 */
11964 static int
11965#ifdef __BORLANDC__
11966_RTLENTRYF
11967#endif
11968item_compare(const void *s1, const void *s2)
11969{
11970 sortItem_T *si1, *si2;
11971 typval_T *tv1, *tv2;
11972 char_u *p1, *p2;
11973 char_u *tofree1 = NULL, *tofree2 = NULL;
11974 int res;
11975 char_u numbuf1[NUMBUFLEN];
11976 char_u numbuf2[NUMBUFLEN];
11977
11978 si1 = (sortItem_T *)s1;
11979 si2 = (sortItem_T *)s2;
11980 tv1 = &si1->item->li_tv;
11981 tv2 = &si2->item->li_tv;
11982
11983 if (sortinfo->item_compare_numbers)
11984 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011985 varnumber_T v1 = tv_get_number(tv1);
11986 varnumber_T v2 = tv_get_number(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011987
11988 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11989 }
11990
11991#ifdef FEAT_FLOAT
11992 if (sortinfo->item_compare_float)
11993 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011994 float_T v1 = tv_get_float(tv1);
11995 float_T v2 = tv_get_float(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011996
11997 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11998 }
11999#endif
12000
12001 /* tv2string() puts quotes around a string and allocates memory. Don't do
12002 * that for string variables. Use a single quote when comparing with a
12003 * non-string to do what the docs promise. */
12004 if (tv1->v_type == VAR_STRING)
12005 {
12006 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12007 p1 = (char_u *)"'";
12008 else
12009 p1 = tv1->vval.v_string;
12010 }
12011 else
12012 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
12013 if (tv2->v_type == VAR_STRING)
12014 {
12015 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12016 p2 = (char_u *)"'";
12017 else
12018 p2 = tv2->vval.v_string;
12019 }
12020 else
12021 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
12022 if (p1 == NULL)
12023 p1 = (char_u *)"";
12024 if (p2 == NULL)
12025 p2 = (char_u *)"";
12026 if (!sortinfo->item_compare_numeric)
12027 {
12028 if (sortinfo->item_compare_ic)
12029 res = STRICMP(p1, p2);
12030 else
12031 res = STRCMP(p1, p2);
12032 }
12033 else
12034 {
12035 double n1, n2;
12036 n1 = strtod((char *)p1, (char **)&p1);
12037 n2 = strtod((char *)p2, (char **)&p2);
12038 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
12039 }
12040
12041 /* When the result would be zero, compare the item indexes. Makes the
12042 * sort stable. */
12043 if (res == 0 && !sortinfo->item_compare_keep_zero)
12044 res = si1->idx > si2->idx ? 1 : -1;
12045
12046 vim_free(tofree1);
12047 vim_free(tofree2);
12048 return res;
12049}
12050
12051 static int
12052#ifdef __BORLANDC__
12053_RTLENTRYF
12054#endif
12055item_compare2(const void *s1, const void *s2)
12056{
12057 sortItem_T *si1, *si2;
12058 int res;
12059 typval_T rettv;
12060 typval_T argv[3];
12061 int dummy;
12062 char_u *func_name;
12063 partial_T *partial = sortinfo->item_compare_partial;
12064
12065 /* shortcut after failure in previous call; compare all items equal */
12066 if (sortinfo->item_compare_func_err)
12067 return 0;
12068
12069 si1 = (sortItem_T *)s1;
12070 si2 = (sortItem_T *)s2;
12071
12072 if (partial == NULL)
12073 func_name = sortinfo->item_compare_func;
12074 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012075 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012076
12077 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
12078 * in the copy without changing the original list items. */
12079 copy_tv(&si1->item->li_tv, &argv[0]);
12080 copy_tv(&si2->item->li_tv, &argv[1]);
12081
12082 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
12083 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020012084 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012085 partial, sortinfo->item_compare_selfdict);
12086 clear_tv(&argv[0]);
12087 clear_tv(&argv[1]);
12088
12089 if (res == FAIL)
12090 res = ITEM_COMPARE_FAIL;
12091 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012092 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012093 if (sortinfo->item_compare_func_err)
12094 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
12095 clear_tv(&rettv);
12096
12097 /* When the result would be zero, compare the pointers themselves. Makes
12098 * the sort stable. */
12099 if (res == 0 && !sortinfo->item_compare_keep_zero)
12100 res = si1->idx > si2->idx ? 1 : -1;
12101
12102 return res;
12103}
12104
12105/*
12106 * "sort({list})" function
12107 */
12108 static void
12109do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
12110{
12111 list_T *l;
12112 listitem_T *li;
12113 sortItem_T *ptrs;
12114 sortinfo_T *old_sortinfo;
12115 sortinfo_T info;
12116 long len;
12117 long i;
12118
12119 /* Pointer to current info struct used in compare function. Save and
12120 * restore the current one for nested calls. */
12121 old_sortinfo = sortinfo;
12122 sortinfo = &info;
12123
12124 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012125 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012126 else
12127 {
12128 l = argvars[0].vval.v_list;
12129 if (l == NULL || tv_check_lock(l->lv_lock,
12130 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
12131 TRUE))
12132 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012133 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012134
12135 len = list_len(l);
12136 if (len <= 1)
12137 goto theend; /* short list sorts pretty quickly */
12138
12139 info.item_compare_ic = FALSE;
12140 info.item_compare_numeric = FALSE;
12141 info.item_compare_numbers = FALSE;
12142#ifdef FEAT_FLOAT
12143 info.item_compare_float = FALSE;
12144#endif
12145 info.item_compare_func = NULL;
12146 info.item_compare_partial = NULL;
12147 info.item_compare_selfdict = NULL;
12148 if (argvars[1].v_type != VAR_UNKNOWN)
12149 {
12150 /* optional second argument: {func} */
12151 if (argvars[1].v_type == VAR_FUNC)
12152 info.item_compare_func = argvars[1].vval.v_string;
12153 else if (argvars[1].v_type == VAR_PARTIAL)
12154 info.item_compare_partial = argvars[1].vval.v_partial;
12155 else
12156 {
12157 int error = FALSE;
12158
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012159 i = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012160 if (error)
12161 goto theend; /* type error; errmsg already given */
12162 if (i == 1)
12163 info.item_compare_ic = TRUE;
12164 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012165 info.item_compare_func = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012166 else if (i != 0)
12167 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012168 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012169 goto theend;
12170 }
12171 if (info.item_compare_func != NULL)
12172 {
12173 if (*info.item_compare_func == NUL)
12174 {
12175 /* empty string means default sort */
12176 info.item_compare_func = NULL;
12177 }
12178 else if (STRCMP(info.item_compare_func, "n") == 0)
12179 {
12180 info.item_compare_func = NULL;
12181 info.item_compare_numeric = TRUE;
12182 }
12183 else if (STRCMP(info.item_compare_func, "N") == 0)
12184 {
12185 info.item_compare_func = NULL;
12186 info.item_compare_numbers = TRUE;
12187 }
12188#ifdef FEAT_FLOAT
12189 else if (STRCMP(info.item_compare_func, "f") == 0)
12190 {
12191 info.item_compare_func = NULL;
12192 info.item_compare_float = TRUE;
12193 }
12194#endif
12195 else if (STRCMP(info.item_compare_func, "i") == 0)
12196 {
12197 info.item_compare_func = NULL;
12198 info.item_compare_ic = TRUE;
12199 }
12200 }
12201 }
12202
12203 if (argvars[2].v_type != VAR_UNKNOWN)
12204 {
12205 /* optional third argument: {dict} */
12206 if (argvars[2].v_type != VAR_DICT)
12207 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012208 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012209 goto theend;
12210 }
12211 info.item_compare_selfdict = argvars[2].vval.v_dict;
12212 }
12213 }
12214
12215 /* Make an array with each entry pointing to an item in the List. */
12216 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
12217 if (ptrs == NULL)
12218 goto theend;
12219
12220 i = 0;
12221 if (sort)
12222 {
12223 /* sort(): ptrs will be the list to sort */
12224 for (li = l->lv_first; li != NULL; li = li->li_next)
12225 {
12226 ptrs[i].item = li;
12227 ptrs[i].idx = i;
12228 ++i;
12229 }
12230
12231 info.item_compare_func_err = FALSE;
12232 info.item_compare_keep_zero = FALSE;
12233 /* test the compare function */
12234 if ((info.item_compare_func != NULL
12235 || info.item_compare_partial != NULL)
12236 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
12237 == ITEM_COMPARE_FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012238 emsg(_("E702: Sort compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012239 else
12240 {
12241 /* Sort the array with item pointers. */
12242 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
12243 info.item_compare_func == NULL
12244 && info.item_compare_partial == NULL
12245 ? item_compare : item_compare2);
12246
12247 if (!info.item_compare_func_err)
12248 {
12249 /* Clear the List and append the items in sorted order. */
12250 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
12251 l->lv_len = 0;
12252 for (i = 0; i < len; ++i)
12253 list_append(l, ptrs[i].item);
12254 }
12255 }
12256 }
12257 else
12258 {
12259 int (*item_compare_func_ptr)(const void *, const void *);
12260
12261 /* f_uniq(): ptrs will be a stack of items to remove */
12262 info.item_compare_func_err = FALSE;
12263 info.item_compare_keep_zero = TRUE;
12264 item_compare_func_ptr = info.item_compare_func != NULL
12265 || info.item_compare_partial != NULL
12266 ? item_compare2 : item_compare;
12267
12268 for (li = l->lv_first; li != NULL && li->li_next != NULL;
12269 li = li->li_next)
12270 {
12271 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
12272 == 0)
12273 ptrs[i++].item = li;
12274 if (info.item_compare_func_err)
12275 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012276 emsg(_("E882: Uniq compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012277 break;
12278 }
12279 }
12280
12281 if (!info.item_compare_func_err)
12282 {
12283 while (--i >= 0)
12284 {
12285 li = ptrs[i].item->li_next;
12286 ptrs[i].item->li_next = li->li_next;
12287 if (li->li_next != NULL)
12288 li->li_next->li_prev = ptrs[i].item;
12289 else
12290 l->lv_last = ptrs[i].item;
12291 list_fix_watch(l, li);
12292 listitem_free(li);
12293 l->lv_len--;
12294 }
12295 }
12296 }
12297
12298 vim_free(ptrs);
12299 }
12300theend:
12301 sortinfo = old_sortinfo;
12302}
12303
12304/*
12305 * "sort({list})" function
12306 */
12307 static void
12308f_sort(typval_T *argvars, typval_T *rettv)
12309{
12310 do_sort_uniq(argvars, rettv, TRUE);
12311}
12312
12313/*
12314 * "uniq({list})" function
12315 */
12316 static void
12317f_uniq(typval_T *argvars, typval_T *rettv)
12318{
12319 do_sort_uniq(argvars, rettv, FALSE);
12320}
12321
12322/*
12323 * "soundfold({word})" function
12324 */
12325 static void
12326f_soundfold(typval_T *argvars, typval_T *rettv)
12327{
12328 char_u *s;
12329
12330 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012331 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012332#ifdef FEAT_SPELL
12333 rettv->vval.v_string = eval_soundfold(s);
12334#else
12335 rettv->vval.v_string = vim_strsave(s);
12336#endif
12337}
12338
12339/*
12340 * "spellbadword()" function
12341 */
12342 static void
12343f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
12344{
12345 char_u *word = (char_u *)"";
12346 hlf_T attr = HLF_COUNT;
12347 int len = 0;
12348
12349 if (rettv_list_alloc(rettv) == FAIL)
12350 return;
12351
12352#ifdef FEAT_SPELL
12353 if (argvars[0].v_type == VAR_UNKNOWN)
12354 {
12355 /* Find the start and length of the badly spelled word. */
12356 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
12357 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012358 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012359 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012360 curwin->w_set_curswant = TRUE;
12361 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012362 }
12363 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
12364 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012365 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012366 int capcol = -1;
12367
12368 if (str != NULL)
12369 {
12370 /* Check the argument for spelling. */
12371 while (*str != NUL)
12372 {
12373 len = spell_check(curwin, str, &attr, &capcol, FALSE);
12374 if (attr != HLF_COUNT)
12375 {
12376 word = str;
12377 break;
12378 }
12379 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020012380 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012381 }
12382 }
12383 }
12384#endif
12385
12386 list_append_string(rettv->vval.v_list, word, len);
12387 list_append_string(rettv->vval.v_list, (char_u *)(
12388 attr == HLF_SPB ? "bad" :
12389 attr == HLF_SPR ? "rare" :
12390 attr == HLF_SPL ? "local" :
12391 attr == HLF_SPC ? "caps" :
12392 ""), -1);
12393}
12394
12395/*
12396 * "spellsuggest()" function
12397 */
12398 static void
12399f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
12400{
12401#ifdef FEAT_SPELL
12402 char_u *str;
12403 int typeerr = FALSE;
12404 int maxcount;
12405 garray_T ga;
12406 int i;
12407 listitem_T *li;
12408 int need_capital = FALSE;
12409#endif
12410
12411 if (rettv_list_alloc(rettv) == FAIL)
12412 return;
12413
12414#ifdef FEAT_SPELL
12415 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
12416 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012417 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012418 if (argvars[1].v_type != VAR_UNKNOWN)
12419 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012420 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012421 if (maxcount <= 0)
12422 return;
12423 if (argvars[2].v_type != VAR_UNKNOWN)
12424 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012425 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012426 if (typeerr)
12427 return;
12428 }
12429 }
12430 else
12431 maxcount = 25;
12432
12433 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
12434
12435 for (i = 0; i < ga.ga_len; ++i)
12436 {
12437 str = ((char_u **)ga.ga_data)[i];
12438
12439 li = listitem_alloc();
12440 if (li == NULL)
12441 vim_free(str);
12442 else
12443 {
12444 li->li_tv.v_type = VAR_STRING;
12445 li->li_tv.v_lock = 0;
12446 li->li_tv.vval.v_string = str;
12447 list_append(rettv->vval.v_list, li);
12448 }
12449 }
12450 ga_clear(&ga);
12451 }
12452#endif
12453}
12454
12455 static void
12456f_split(typval_T *argvars, typval_T *rettv)
12457{
12458 char_u *str;
12459 char_u *end;
12460 char_u *pat = NULL;
12461 regmatch_T regmatch;
12462 char_u patbuf[NUMBUFLEN];
12463 char_u *save_cpo;
12464 int match;
12465 colnr_T col = 0;
12466 int keepempty = FALSE;
12467 int typeerr = FALSE;
12468
12469 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12470 save_cpo = p_cpo;
12471 p_cpo = (char_u *)"";
12472
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012473 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012474 if (argvars[1].v_type != VAR_UNKNOWN)
12475 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012476 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012477 if (pat == NULL)
12478 typeerr = TRUE;
12479 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012480 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012481 }
12482 if (pat == NULL || *pat == NUL)
12483 pat = (char_u *)"[\\x01- ]\\+";
12484
12485 if (rettv_list_alloc(rettv) == FAIL)
12486 return;
12487 if (typeerr)
12488 return;
12489
12490 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12491 if (regmatch.regprog != NULL)
12492 {
12493 regmatch.rm_ic = FALSE;
12494 while (*str != NUL || keepempty)
12495 {
12496 if (*str == NUL)
12497 match = FALSE; /* empty item at the end */
12498 else
12499 match = vim_regexec_nl(&regmatch, str, col);
12500 if (match)
12501 end = regmatch.startp[0];
12502 else
12503 end = str + STRLEN(str);
12504 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
12505 && *str != NUL && match && end < regmatch.endp[0]))
12506 {
12507 if (list_append_string(rettv->vval.v_list, str,
12508 (int)(end - str)) == FAIL)
12509 break;
12510 }
12511 if (!match)
12512 break;
12513 /* Advance to just after the match. */
12514 if (regmatch.endp[0] > str)
12515 col = 0;
12516 else
12517 {
12518 /* Don't get stuck at the same match. */
12519#ifdef FEAT_MBYTE
12520 col = (*mb_ptr2len)(regmatch.endp[0]);
12521#else
12522 col = 1;
12523#endif
12524 }
12525 str = regmatch.endp[0];
12526 }
12527
12528 vim_regfree(regmatch.regprog);
12529 }
12530
12531 p_cpo = save_cpo;
12532}
12533
12534#ifdef FEAT_FLOAT
12535/*
12536 * "sqrt()" function
12537 */
12538 static void
12539f_sqrt(typval_T *argvars, typval_T *rettv)
12540{
12541 float_T f = 0.0;
12542
12543 rettv->v_type = VAR_FLOAT;
12544 if (get_float_arg(argvars, &f) == OK)
12545 rettv->vval.v_float = sqrt(f);
12546 else
12547 rettv->vval.v_float = 0.0;
12548}
12549
12550/*
12551 * "str2float()" function
12552 */
12553 static void
12554f_str2float(typval_T *argvars, typval_T *rettv)
12555{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012556 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010012557 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012558
Bram Moolenaar08243d22017-01-10 16:12:29 +010012559 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012560 p = skipwhite(p + 1);
12561 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010012562 if (isneg)
12563 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012564 rettv->v_type = VAR_FLOAT;
12565}
12566#endif
12567
12568/*
12569 * "str2nr()" function
12570 */
12571 static void
12572f_str2nr(typval_T *argvars, typval_T *rettv)
12573{
12574 int base = 10;
12575 char_u *p;
12576 varnumber_T n;
12577 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010012578 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012579
12580 if (argvars[1].v_type != VAR_UNKNOWN)
12581 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012582 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012583 if (base != 2 && base != 8 && base != 10 && base != 16)
12584 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012585 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012586 return;
12587 }
12588 }
12589
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012590 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010012591 isneg = (*p == '-');
12592 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012593 p = skipwhite(p + 1);
12594 switch (base)
12595 {
12596 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
12597 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
12598 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
12599 default: what = 0;
12600 }
12601 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010012602 if (isneg)
12603 rettv->vval.v_number = -n;
12604 else
12605 rettv->vval.v_number = n;
12606
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012607}
12608
12609#ifdef HAVE_STRFTIME
12610/*
12611 * "strftime({format}[, {time}])" function
12612 */
12613 static void
12614f_strftime(typval_T *argvars, typval_T *rettv)
12615{
12616 char_u result_buf[256];
12617 struct tm *curtime;
12618 time_t seconds;
12619 char_u *p;
12620
12621 rettv->v_type = VAR_STRING;
12622
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012623 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012624 if (argvars[1].v_type == VAR_UNKNOWN)
12625 seconds = time(NULL);
12626 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012627 seconds = (time_t)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012628 curtime = localtime(&seconds);
12629 /* MSVC returns NULL for an invalid value of seconds. */
12630 if (curtime == NULL)
12631 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
12632 else
12633 {
12634# ifdef FEAT_MBYTE
12635 vimconv_T conv;
12636 char_u *enc;
12637
12638 conv.vc_type = CONV_NONE;
12639 enc = enc_locale();
12640 convert_setup(&conv, p_enc, enc);
12641 if (conv.vc_type != CONV_NONE)
12642 p = string_convert(&conv, p, NULL);
12643# endif
12644 if (p != NULL)
12645 (void)strftime((char *)result_buf, sizeof(result_buf),
12646 (char *)p, curtime);
12647 else
12648 result_buf[0] = NUL;
12649
12650# ifdef FEAT_MBYTE
12651 if (conv.vc_type != CONV_NONE)
12652 vim_free(p);
12653 convert_setup(&conv, enc, p_enc);
12654 if (conv.vc_type != CONV_NONE)
12655 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
12656 else
12657# endif
12658 rettv->vval.v_string = vim_strsave(result_buf);
12659
12660# ifdef FEAT_MBYTE
12661 /* Release conversion descriptors */
12662 convert_setup(&conv, NULL, NULL);
12663 vim_free(enc);
12664# endif
12665 }
12666}
12667#endif
12668
12669/*
12670 * "strgetchar()" function
12671 */
12672 static void
12673f_strgetchar(typval_T *argvars, typval_T *rettv)
12674{
12675 char_u *str;
12676 int len;
12677 int error = FALSE;
12678 int charidx;
12679
12680 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012681 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012682 if (str == NULL)
12683 return;
12684 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012685 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012686 if (error)
12687 return;
12688#ifdef FEAT_MBYTE
12689 {
12690 int byteidx = 0;
12691
12692 while (charidx >= 0 && byteidx < len)
12693 {
12694 if (charidx == 0)
12695 {
12696 rettv->vval.v_number = mb_ptr2char(str + byteidx);
12697 break;
12698 }
12699 --charidx;
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012700 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012701 }
12702 }
12703#else
12704 if (charidx < len)
12705 rettv->vval.v_number = str[charidx];
12706#endif
12707}
12708
12709/*
12710 * "stridx()" function
12711 */
12712 static void
12713f_stridx(typval_T *argvars, typval_T *rettv)
12714{
12715 char_u buf[NUMBUFLEN];
12716 char_u *needle;
12717 char_u *haystack;
12718 char_u *save_haystack;
12719 char_u *pos;
12720 int start_idx;
12721
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012722 needle = tv_get_string_chk(&argvars[1]);
12723 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012724 rettv->vval.v_number = -1;
12725 if (needle == NULL || haystack == NULL)
12726 return; /* type error; errmsg already given */
12727
12728 if (argvars[2].v_type != VAR_UNKNOWN)
12729 {
12730 int error = FALSE;
12731
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012732 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012733 if (error || start_idx >= (int)STRLEN(haystack))
12734 return;
12735 if (start_idx >= 0)
12736 haystack += start_idx;
12737 }
12738
12739 pos = (char_u *)strstr((char *)haystack, (char *)needle);
12740 if (pos != NULL)
12741 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
12742}
12743
12744/*
12745 * "string()" function
12746 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010012747 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012748f_string(typval_T *argvars, typval_T *rettv)
12749{
12750 char_u *tofree;
12751 char_u numbuf[NUMBUFLEN];
12752
12753 rettv->v_type = VAR_STRING;
12754 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
12755 get_copyID());
12756 /* Make a copy if we have a value but it's not in allocated memory. */
12757 if (rettv->vval.v_string != NULL && tofree == NULL)
12758 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
12759}
12760
12761/*
12762 * "strlen()" function
12763 */
12764 static void
12765f_strlen(typval_T *argvars, typval_T *rettv)
12766{
12767 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012768 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012769}
12770
12771/*
12772 * "strchars()" function
12773 */
12774 static void
12775f_strchars(typval_T *argvars, typval_T *rettv)
12776{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012777 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012778 int skipcc = 0;
12779#ifdef FEAT_MBYTE
12780 varnumber_T len = 0;
12781 int (*func_mb_ptr2char_adv)(char_u **pp);
12782#endif
12783
12784 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012785 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012786 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012787 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012788 else
12789 {
12790#ifdef FEAT_MBYTE
12791 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
12792 while (*s != NUL)
12793 {
12794 func_mb_ptr2char_adv(&s);
12795 ++len;
12796 }
12797 rettv->vval.v_number = len;
12798#else
12799 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
12800#endif
12801 }
12802}
12803
12804/*
12805 * "strdisplaywidth()" function
12806 */
12807 static void
12808f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
12809{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012810 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012811 int col = 0;
12812
12813 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012814 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012815
12816 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
12817}
12818
12819/*
12820 * "strwidth()" function
12821 */
12822 static void
12823f_strwidth(typval_T *argvars, typval_T *rettv)
12824{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012825 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012826
12827 rettv->vval.v_number = (varnumber_T)(
12828#ifdef FEAT_MBYTE
12829 mb_string2cells(s, -1)
12830#else
12831 STRLEN(s)
12832#endif
12833 );
12834}
12835
12836/*
12837 * "strcharpart()" function
12838 */
12839 static void
12840f_strcharpart(typval_T *argvars, typval_T *rettv)
12841{
12842#ifdef FEAT_MBYTE
12843 char_u *p;
12844 int nchar;
12845 int nbyte = 0;
12846 int charlen;
12847 int len = 0;
12848 int slen;
12849 int error = FALSE;
12850
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012851 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012852 slen = (int)STRLEN(p);
12853
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012854 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012855 if (!error)
12856 {
12857 if (nchar > 0)
12858 while (nchar > 0 && nbyte < slen)
12859 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012860 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012861 --nchar;
12862 }
12863 else
12864 nbyte = nchar;
12865 if (argvars[2].v_type != VAR_UNKNOWN)
12866 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012867 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012868 while (charlen > 0 && nbyte + len < slen)
12869 {
12870 int off = nbyte + len;
12871
12872 if (off < 0)
12873 len += 1;
12874 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012875 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012876 --charlen;
12877 }
12878 }
12879 else
12880 len = slen - nbyte; /* default: all bytes that are available. */
12881 }
12882
12883 /*
12884 * Only return the overlap between the specified part and the actual
12885 * string.
12886 */
12887 if (nbyte < 0)
12888 {
12889 len += nbyte;
12890 nbyte = 0;
12891 }
12892 else if (nbyte > slen)
12893 nbyte = slen;
12894 if (len < 0)
12895 len = 0;
12896 else if (nbyte + len > slen)
12897 len = slen - nbyte;
12898
12899 rettv->v_type = VAR_STRING;
12900 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
12901#else
12902 f_strpart(argvars, rettv);
12903#endif
12904}
12905
12906/*
12907 * "strpart()" function
12908 */
12909 static void
12910f_strpart(typval_T *argvars, typval_T *rettv)
12911{
12912 char_u *p;
12913 int n;
12914 int len;
12915 int slen;
12916 int error = FALSE;
12917
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012918 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012919 slen = (int)STRLEN(p);
12920
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012921 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012922 if (error)
12923 len = 0;
12924 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012925 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012926 else
12927 len = slen - n; /* default len: all bytes that are available. */
12928
12929 /*
12930 * Only return the overlap between the specified part and the actual
12931 * string.
12932 */
12933 if (n < 0)
12934 {
12935 len += n;
12936 n = 0;
12937 }
12938 else if (n > slen)
12939 n = slen;
12940 if (len < 0)
12941 len = 0;
12942 else if (n + len > slen)
12943 len = slen - n;
12944
12945 rettv->v_type = VAR_STRING;
12946 rettv->vval.v_string = vim_strnsave(p + n, len);
12947}
12948
12949/*
12950 * "strridx()" function
12951 */
12952 static void
12953f_strridx(typval_T *argvars, typval_T *rettv)
12954{
12955 char_u buf[NUMBUFLEN];
12956 char_u *needle;
12957 char_u *haystack;
12958 char_u *rest;
12959 char_u *lastmatch = NULL;
12960 int haystack_len, end_idx;
12961
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012962 needle = tv_get_string_chk(&argvars[1]);
12963 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012964
12965 rettv->vval.v_number = -1;
12966 if (needle == NULL || haystack == NULL)
12967 return; /* type error; errmsg already given */
12968
12969 haystack_len = (int)STRLEN(haystack);
12970 if (argvars[2].v_type != VAR_UNKNOWN)
12971 {
12972 /* Third argument: upper limit for index */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012973 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012974 if (end_idx < 0)
12975 return; /* can never find a match */
12976 }
12977 else
12978 end_idx = haystack_len;
12979
12980 if (*needle == NUL)
12981 {
12982 /* Empty string matches past the end. */
12983 lastmatch = haystack + end_idx;
12984 }
12985 else
12986 {
12987 for (rest = haystack; *rest != '\0'; ++rest)
12988 {
12989 rest = (char_u *)strstr((char *)rest, (char *)needle);
12990 if (rest == NULL || rest > haystack + end_idx)
12991 break;
12992 lastmatch = rest;
12993 }
12994 }
12995
12996 if (lastmatch == NULL)
12997 rettv->vval.v_number = -1;
12998 else
12999 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
13000}
13001
13002/*
13003 * "strtrans()" function
13004 */
13005 static void
13006f_strtrans(typval_T *argvars, typval_T *rettv)
13007{
13008 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013009 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013010}
13011
13012/*
13013 * "submatch()" function
13014 */
13015 static void
13016f_submatch(typval_T *argvars, typval_T *rettv)
13017{
13018 int error = FALSE;
13019 int no;
13020 int retList = 0;
13021
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013022 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013023 if (error)
13024 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013025 if (no < 0 || no >= NSUBEXP)
13026 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013027 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +010013028 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013029 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013030 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013031 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013032 if (error)
13033 return;
13034
13035 if (retList == 0)
13036 {
13037 rettv->v_type = VAR_STRING;
13038 rettv->vval.v_string = reg_submatch(no);
13039 }
13040 else
13041 {
13042 rettv->v_type = VAR_LIST;
13043 rettv->vval.v_list = reg_submatch_list(no);
13044 }
13045}
13046
13047/*
13048 * "substitute()" function
13049 */
13050 static void
13051f_substitute(typval_T *argvars, typval_T *rettv)
13052{
13053 char_u patbuf[NUMBUFLEN];
13054 char_u subbuf[NUMBUFLEN];
13055 char_u flagsbuf[NUMBUFLEN];
13056
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013057 char_u *str = tv_get_string_chk(&argvars[0]);
13058 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013059 char_u *sub = NULL;
13060 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013061 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013062
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013063 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
13064 expr = &argvars[2];
13065 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013066 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013067
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013068 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013069 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
13070 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013071 rettv->vval.v_string = NULL;
13072 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013073 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013074}
13075
13076/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013077 * "swapinfo(swap_filename)" function
13078 */
13079 static void
13080f_swapinfo(typval_T *argvars, typval_T *rettv)
13081{
13082 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013083 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013084}
13085
13086/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020013087 * "swapname(expr)" function
13088 */
13089 static void
13090f_swapname(typval_T *argvars, typval_T *rettv)
13091{
13092 buf_T *buf;
13093
13094 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010013095 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +020013096 if (buf == NULL || buf->b_ml.ml_mfp == NULL
13097 || buf->b_ml.ml_mfp->mf_fname == NULL)
13098 rettv->vval.v_string = NULL;
13099 else
13100 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
13101}
13102
13103/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013104 * "synID(lnum, col, trans)" function
13105 */
13106 static void
13107f_synID(typval_T *argvars UNUSED, typval_T *rettv)
13108{
13109 int id = 0;
13110#ifdef FEAT_SYN_HL
13111 linenr_T lnum;
13112 colnr_T col;
13113 int trans;
13114 int transerr = FALSE;
13115
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013116 lnum = tv_get_lnum(argvars); /* -1 on type error */
13117 col = (linenr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
13118 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013119
13120 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13121 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
13122 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
13123#endif
13124
13125 rettv->vval.v_number = id;
13126}
13127
13128/*
13129 * "synIDattr(id, what [, mode])" function
13130 */
13131 static void
13132f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
13133{
13134 char_u *p = NULL;
13135#ifdef FEAT_SYN_HL
13136 int id;
13137 char_u *what;
13138 char_u *mode;
13139 char_u modebuf[NUMBUFLEN];
13140 int modec;
13141
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013142 id = (int)tv_get_number(&argvars[0]);
13143 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013144 if (argvars[2].v_type != VAR_UNKNOWN)
13145 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013146 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013147 modec = TOLOWER_ASC(mode[0]);
13148 if (modec != 't' && modec != 'c' && modec != 'g')
13149 modec = 0; /* replace invalid with current */
13150 }
13151 else
13152 {
13153#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
13154 if (USE_24BIT)
13155 modec = 'g';
13156 else
13157#endif
13158 if (t_colors > 1)
13159 modec = 'c';
13160 else
13161 modec = 't';
13162 }
13163
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013164 switch (TOLOWER_ASC(what[0]))
13165 {
13166 case 'b':
13167 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
13168 p = highlight_color(id, what, modec);
13169 else /* bold */
13170 p = highlight_has_attr(id, HL_BOLD, modec);
13171 break;
13172
13173 case 'f': /* fg[#] or font */
13174 p = highlight_color(id, what, modec);
13175 break;
13176
13177 case 'i':
13178 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
13179 p = highlight_has_attr(id, HL_INVERSE, modec);
13180 else /* italic */
13181 p = highlight_has_attr(id, HL_ITALIC, modec);
13182 break;
13183
13184 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020013185 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013186 break;
13187
13188 case 'r': /* reverse */
13189 p = highlight_has_attr(id, HL_INVERSE, modec);
13190 break;
13191
13192 case 's':
13193 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
13194 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020013195 /* strikeout */
13196 else if (TOLOWER_ASC(what[1]) == 't' &&
13197 TOLOWER_ASC(what[2]) == 'r')
13198 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013199 else /* standout */
13200 p = highlight_has_attr(id, HL_STANDOUT, modec);
13201 break;
13202
13203 case 'u':
13204 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
13205 /* underline */
13206 p = highlight_has_attr(id, HL_UNDERLINE, modec);
13207 else
13208 /* undercurl */
13209 p = highlight_has_attr(id, HL_UNDERCURL, modec);
13210 break;
13211 }
13212
13213 if (p != NULL)
13214 p = vim_strsave(p);
13215#endif
13216 rettv->v_type = VAR_STRING;
13217 rettv->vval.v_string = p;
13218}
13219
13220/*
13221 * "synIDtrans(id)" function
13222 */
13223 static void
13224f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
13225{
13226 int id;
13227
13228#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013229 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013230
13231 if (id > 0)
13232 id = syn_get_final_id(id);
13233 else
13234#endif
13235 id = 0;
13236
13237 rettv->vval.v_number = id;
13238}
13239
13240/*
13241 * "synconcealed(lnum, col)" function
13242 */
13243 static void
13244f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
13245{
13246#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
13247 linenr_T lnum;
13248 colnr_T col;
13249 int syntax_flags = 0;
13250 int cchar;
13251 int matchid = 0;
13252 char_u str[NUMBUFLEN];
13253#endif
13254
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013255 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013256
13257#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013258 lnum = tv_get_lnum(argvars); /* -1 on type error */
13259 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013260
13261 vim_memset(str, NUL, sizeof(str));
13262
13263 if (rettv_list_alloc(rettv) != FAIL)
13264 {
13265 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13266 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13267 && curwin->w_p_cole > 0)
13268 {
13269 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
13270 syntax_flags = get_syntax_info(&matchid);
13271
13272 /* get the conceal character */
13273 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
13274 {
13275 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020013276 if (cchar == NUL && curwin->w_p_cole == 1)
13277 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013278 if (cchar != NUL)
13279 {
13280# ifdef FEAT_MBYTE
13281 if (has_mbyte)
13282 (*mb_char2bytes)(cchar, str);
13283 else
13284# endif
13285 str[0] = cchar;
13286 }
13287 }
13288 }
13289
13290 list_append_number(rettv->vval.v_list,
13291 (syntax_flags & HL_CONCEAL) != 0);
13292 /* -1 to auto-determine strlen */
13293 list_append_string(rettv->vval.v_list, str, -1);
13294 list_append_number(rettv->vval.v_list, matchid);
13295 }
13296#endif
13297}
13298
13299/*
13300 * "synstack(lnum, col)" function
13301 */
13302 static void
13303f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
13304{
13305#ifdef FEAT_SYN_HL
13306 linenr_T lnum;
13307 colnr_T col;
13308 int i;
13309 int id;
13310#endif
13311
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013312 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013313
13314#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013315 lnum = tv_get_lnum(argvars); /* -1 on type error */
13316 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013317
13318 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13319 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13320 && rettv_list_alloc(rettv) != FAIL)
13321 {
13322 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
13323 for (i = 0; ; ++i)
13324 {
13325 id = syn_get_stack_item(i);
13326 if (id < 0)
13327 break;
13328 if (list_append_number(rettv->vval.v_list, id) == FAIL)
13329 break;
13330 }
13331 }
13332#endif
13333}
13334
13335 static void
13336get_cmd_output_as_rettv(
13337 typval_T *argvars,
13338 typval_T *rettv,
13339 int retlist)
13340{
13341 char_u *res = NULL;
13342 char_u *p;
13343 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013344 int err = FALSE;
13345 FILE *fd;
13346 list_T *list = NULL;
13347 int flags = SHELL_SILENT;
13348
13349 rettv->v_type = VAR_STRING;
13350 rettv->vval.v_string = NULL;
13351 if (check_restricted() || check_secure())
13352 goto errret;
13353
13354 if (argvars[1].v_type != VAR_UNKNOWN)
13355 {
13356 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010013357 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013358 * command.
13359 */
13360 if ((infile = vim_tempname('i', TRUE)) == NULL)
13361 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013362 emsg(_(e_notmp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013363 goto errret;
13364 }
13365
13366 fd = mch_fopen((char *)infile, WRITEBIN);
13367 if (fd == NULL)
13368 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013369 semsg(_(e_notopen), infile);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013370 goto errret;
13371 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010013372 if (argvars[1].v_type == VAR_NUMBER)
13373 {
13374 linenr_T lnum;
13375 buf_T *buf;
13376
13377 buf = buflist_findnr(argvars[1].vval.v_number);
13378 if (buf == NULL)
13379 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013380 semsg(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010013381 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010013382 goto errret;
13383 }
13384
13385 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
13386 {
13387 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
13388 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
13389 {
13390 err = TRUE;
13391 break;
13392 }
13393 if (putc(NL, fd) == EOF)
13394 {
13395 err = TRUE;
13396 break;
13397 }
13398 }
13399 }
13400 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013401 {
13402 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
13403 err = TRUE;
13404 }
13405 else
13406 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010013407 size_t len;
13408 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013409
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013410 p = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013411 if (p == NULL)
13412 {
13413 fclose(fd);
13414 goto errret; /* type error; errmsg already given */
13415 }
13416 len = STRLEN(p);
13417 if (len > 0 && fwrite(p, len, 1, fd) != 1)
13418 err = TRUE;
13419 }
13420 if (fclose(fd) != 0)
13421 err = TRUE;
13422 if (err)
13423 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013424 emsg(_("E677: Error writing temp file"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013425 goto errret;
13426 }
13427 }
13428
13429 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
13430 * echoes typeahead, that messes up the display. */
13431 if (!msg_silent)
13432 flags += SHELL_COOKED;
13433
13434 if (retlist)
13435 {
13436 int len;
13437 listitem_T *li;
13438 char_u *s = NULL;
13439 char_u *start;
13440 char_u *end;
13441 int i;
13442
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013443 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013444 if (res == NULL)
13445 goto errret;
13446
13447 list = list_alloc();
13448 if (list == NULL)
13449 goto errret;
13450
13451 for (i = 0; i < len; ++i)
13452 {
13453 start = res + i;
13454 while (i < len && res[i] != NL)
13455 ++i;
13456 end = res + i;
13457
13458 s = alloc((unsigned)(end - start + 1));
13459 if (s == NULL)
13460 goto errret;
13461
13462 for (p = s; start < end; ++p, ++start)
13463 *p = *start == NUL ? NL : *start;
13464 *p = NUL;
13465
13466 li = listitem_alloc();
13467 if (li == NULL)
13468 {
13469 vim_free(s);
13470 goto errret;
13471 }
13472 li->li_tv.v_type = VAR_STRING;
13473 li->li_tv.v_lock = 0;
13474 li->li_tv.vval.v_string = s;
13475 list_append(list, li);
13476 }
13477
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013478 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013479 list = NULL;
13480 }
13481 else
13482 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013483 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013484#ifdef USE_CR
13485 /* translate <CR> into <NL> */
13486 if (res != NULL)
13487 {
13488 char_u *s;
13489
13490 for (s = res; *s; ++s)
13491 {
13492 if (*s == CAR)
13493 *s = NL;
13494 }
13495 }
13496#else
13497# ifdef USE_CRNL
13498 /* translate <CR><NL> into <NL> */
13499 if (res != NULL)
13500 {
13501 char_u *s, *d;
13502
13503 d = res;
13504 for (s = res; *s; ++s)
13505 {
13506 if (s[0] == CAR && s[1] == NL)
13507 ++s;
13508 *d++ = *s;
13509 }
13510 *d = NUL;
13511 }
13512# endif
13513#endif
13514 rettv->vval.v_string = res;
13515 res = NULL;
13516 }
13517
13518errret:
13519 if (infile != NULL)
13520 {
13521 mch_remove(infile);
13522 vim_free(infile);
13523 }
13524 if (res != NULL)
13525 vim_free(res);
13526 if (list != NULL)
13527 list_free(list);
13528}
13529
13530/*
13531 * "system()" function
13532 */
13533 static void
13534f_system(typval_T *argvars, typval_T *rettv)
13535{
13536 get_cmd_output_as_rettv(argvars, rettv, FALSE);
13537}
13538
13539/*
13540 * "systemlist()" function
13541 */
13542 static void
13543f_systemlist(typval_T *argvars, typval_T *rettv)
13544{
13545 get_cmd_output_as_rettv(argvars, rettv, TRUE);
13546}
13547
13548/*
13549 * "tabpagebuflist()" function
13550 */
13551 static void
13552f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13553{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013554 tabpage_T *tp;
13555 win_T *wp = NULL;
13556
13557 if (argvars[0].v_type == VAR_UNKNOWN)
13558 wp = firstwin;
13559 else
13560 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013561 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013562 if (tp != NULL)
13563 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
13564 }
13565 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
13566 {
13567 for (; wp != NULL; wp = wp->w_next)
13568 if (list_append_number(rettv->vval.v_list,
13569 wp->w_buffer->b_fnum) == FAIL)
13570 break;
13571 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013572}
13573
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013574/*
13575 * "tabpagenr()" function
13576 */
13577 static void
13578f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
13579{
13580 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013581 char_u *arg;
13582
13583 if (argvars[0].v_type != VAR_UNKNOWN)
13584 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013585 arg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013586 nr = 0;
13587 if (arg != NULL)
13588 {
13589 if (STRCMP(arg, "$") == 0)
13590 nr = tabpage_index(NULL) - 1;
13591 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013592 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013593 }
13594 }
13595 else
13596 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013597 rettv->vval.v_number = nr;
13598}
13599
13600
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013601/*
13602 * Common code for tabpagewinnr() and winnr().
13603 */
13604 static int
13605get_winnr(tabpage_T *tp, typval_T *argvar)
13606{
13607 win_T *twin;
13608 int nr = 1;
13609 win_T *wp;
13610 char_u *arg;
13611
13612 twin = (tp == curtab) ? curwin : tp->tp_curwin;
13613 if (argvar->v_type != VAR_UNKNOWN)
13614 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013615 arg = tv_get_string_chk(argvar);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013616 if (arg == NULL)
13617 nr = 0; /* type error; errmsg already given */
13618 else if (STRCMP(arg, "$") == 0)
13619 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
13620 else if (STRCMP(arg, "#") == 0)
13621 {
13622 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
13623 if (twin == NULL)
13624 nr = 0;
13625 }
13626 else
13627 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013628 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013629 nr = 0;
13630 }
13631 }
13632
13633 if (nr > 0)
13634 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
13635 wp != twin; wp = wp->w_next)
13636 {
13637 if (wp == NULL)
13638 {
13639 /* didn't find it in this tabpage */
13640 nr = 0;
13641 break;
13642 }
13643 ++nr;
13644 }
13645 return nr;
13646}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013647
13648/*
13649 * "tabpagewinnr()" function
13650 */
13651 static void
13652f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
13653{
13654 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013655 tabpage_T *tp;
13656
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013657 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013658 if (tp == NULL)
13659 nr = 0;
13660 else
13661 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013662 rettv->vval.v_number = nr;
13663}
13664
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013665/*
13666 * "tagfiles()" function
13667 */
13668 static void
13669f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
13670{
13671 char_u *fname;
13672 tagname_T tn;
13673 int first;
13674
13675 if (rettv_list_alloc(rettv) == FAIL)
13676 return;
13677 fname = alloc(MAXPATHL);
13678 if (fname == NULL)
13679 return;
13680
13681 for (first = TRUE; ; first = FALSE)
13682 if (get_tagfname(&tn, first, fname) == FAIL
13683 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
13684 break;
13685 tagname_free(&tn);
13686 vim_free(fname);
13687}
13688
13689/*
13690 * "taglist()" function
13691 */
13692 static void
13693f_taglist(typval_T *argvars, typval_T *rettv)
13694{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013695 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013696 char_u *tag_pattern;
13697
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013698 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013699
13700 rettv->vval.v_number = FALSE;
13701 if (*tag_pattern == NUL)
13702 return;
13703
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013704 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013705 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013706 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013707 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013708}
13709
13710/*
13711 * "tempname()" function
13712 */
13713 static void
13714f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
13715{
13716 static int x = 'A';
13717
13718 rettv->v_type = VAR_STRING;
13719 rettv->vval.v_string = vim_tempname(x, FALSE);
13720
13721 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
13722 * names. Skip 'I' and 'O', they are used for shell redirection. */
13723 do
13724 {
13725 if (x == 'Z')
13726 x = '0';
13727 else if (x == '9')
13728 x = 'A';
13729 else
13730 {
13731#ifdef EBCDIC
13732 if (x == 'I')
13733 x = 'J';
13734 else if (x == 'R')
13735 x = 'S';
13736 else
13737#endif
13738 ++x;
13739 }
13740 } while (x == 'I' || x == 'O');
13741}
13742
13743#ifdef FEAT_FLOAT
13744/*
13745 * "tan()" function
13746 */
13747 static void
13748f_tan(typval_T *argvars, typval_T *rettv)
13749{
13750 float_T f = 0.0;
13751
13752 rettv->v_type = VAR_FLOAT;
13753 if (get_float_arg(argvars, &f) == OK)
13754 rettv->vval.v_float = tan(f);
13755 else
13756 rettv->vval.v_float = 0.0;
13757}
13758
13759/*
13760 * "tanh()" function
13761 */
13762 static void
13763f_tanh(typval_T *argvars, typval_T *rettv)
13764{
13765 float_T f = 0.0;
13766
13767 rettv->v_type = VAR_FLOAT;
13768 if (get_float_arg(argvars, &f) == OK)
13769 rettv->vval.v_float = tanh(f);
13770 else
13771 rettv->vval.v_float = 0.0;
13772}
13773#endif
13774
13775/*
13776 * "test_alloc_fail(id, countdown, repeat)" function
13777 */
13778 static void
13779f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
13780{
13781 if (argvars[0].v_type != VAR_NUMBER
13782 || argvars[0].vval.v_number <= 0
13783 || argvars[1].v_type != VAR_NUMBER
13784 || argvars[1].vval.v_number < 0
13785 || argvars[2].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013786 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013787 else
13788 {
13789 alloc_fail_id = argvars[0].vval.v_number;
13790 if (alloc_fail_id >= aid_last)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013791 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013792 alloc_fail_countdown = argvars[1].vval.v_number;
13793 alloc_fail_repeat = argvars[2].vval.v_number;
13794 did_outofmem_msg = FALSE;
13795 }
13796}
13797
13798/*
13799 * "test_autochdir()"
13800 */
13801 static void
13802f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13803{
13804#if defined(FEAT_AUTOCHDIR)
13805 test_autochdir = TRUE;
13806#endif
13807}
13808
13809/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020013810 * "test_feedinput()"
13811 */
13812 static void
13813f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
13814{
13815#ifdef USE_INPUT_BUF
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013816 char_u *val = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5e80de32017-09-03 15:48:12 +020013817
13818 if (val != NULL)
13819 {
13820 trash_input_buf();
13821 add_to_input_buf_csi(val, (int)STRLEN(val));
13822 }
13823#endif
13824}
13825
13826/*
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013827 * "test_option_not_set({name})" function
13828 */
13829 static void
13830f_test_option_not_set(typval_T *argvars, typval_T *rettv UNUSED)
13831{
13832 char_u *name = (char_u *)"";
13833
13834 if (argvars[0].v_type != VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013835 emsg(_(e_invarg));
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013836 else
13837 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013838 name = tv_get_string(&argvars[0]);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013839 if (reset_option_was_set(name) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013840 semsg(_(e_invarg2), name);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013841 }
13842}
13843
13844/*
13845 * "test_override({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013846 */
13847 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013848f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013849{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013850 char_u *name = (char_u *)"";
13851 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013852 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013853
13854 if (argvars[0].v_type != VAR_STRING
13855 || (argvars[1].v_type) != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013856 emsg(_(e_invarg));
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013857 else
13858 {
Bram Moolenaare38197d2018-12-24 23:35:13 +010013859 name = tv_get_string(&argvars[0]);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013860 val = (int)tv_get_number(&argvars[1]);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013861
13862 if (STRCMP(name, (char_u *)"redraw") == 0)
13863 disable_redraw_for_testing = val;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020013864 else if (STRCMP(name, (char_u *)"redraw_flag") == 0)
13865 ignore_redraw_flag_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013866 else if (STRCMP(name, (char_u *)"char_avail") == 0)
13867 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013868 else if (STRCMP(name, (char_u *)"starting") == 0)
13869 {
13870 if (val)
13871 {
13872 if (save_starting < 0)
13873 save_starting = starting;
13874 starting = 0;
13875 }
13876 else
13877 {
13878 starting = save_starting;
13879 save_starting = -1;
13880 }
13881 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013882 else if (STRCMP(name, (char_u *)"nfa_fail") == 0)
13883 nfa_fail_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013884 else if (STRCMP(name, (char_u *)"ALL") == 0)
13885 {
13886 disable_char_avail_for_testing = FALSE;
13887 disable_redraw_for_testing = FALSE;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020013888 ignore_redraw_flag_for_testing = FALSE;
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013889 nfa_fail_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013890 if (save_starting >= 0)
13891 {
13892 starting = save_starting;
13893 save_starting = -1;
13894 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013895 }
13896 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013897 semsg(_(e_invarg2), name);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013898 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013899}
13900
13901/*
13902 * "test_garbagecollect_now()" function
13903 */
13904 static void
13905f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13906{
13907 /* This is dangerous, any Lists and Dicts used internally may be freed
13908 * while still in use. */
13909 garbage_collect(TRUE);
13910}
13911
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013912/*
13913 * "test_ignore_error()" function
13914 */
13915 static void
13916f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
13917{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013918 ignore_error_for_testing(tv_get_string(&argvars[0]));
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013919}
13920
Bram Moolenaarc0f5a782019-01-13 15:16:13 +010013921 static void
13922f_test_null_blob(typval_T *argvars UNUSED, typval_T *rettv)
13923{
13924 rettv->v_type = VAR_BLOB;
13925 rettv->vval.v_blob = NULL;
13926}
13927
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013928#ifdef FEAT_JOB_CHANNEL
13929 static void
13930f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
13931{
13932 rettv->v_type = VAR_CHANNEL;
13933 rettv->vval.v_channel = NULL;
13934}
13935#endif
13936
13937 static void
13938f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
13939{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013940 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013941}
13942
13943#ifdef FEAT_JOB_CHANNEL
13944 static void
13945f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
13946{
13947 rettv->v_type = VAR_JOB;
13948 rettv->vval.v_job = NULL;
13949}
13950#endif
13951
13952 static void
13953f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
13954{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013955 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013956}
13957
13958 static void
13959f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
13960{
13961 rettv->v_type = VAR_PARTIAL;
13962 rettv->vval.v_partial = NULL;
13963}
13964
13965 static void
13966f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
13967{
13968 rettv->v_type = VAR_STRING;
13969 rettv->vval.v_string = NULL;
13970}
13971
Bram Moolenaarab186732018-09-14 21:27:06 +020013972#ifdef FEAT_GUI
13973 static void
13974f_test_scrollbar(typval_T *argvars, typval_T *rettv UNUSED)
13975{
13976 char_u *which;
13977 long value;
13978 int dragging;
13979 scrollbar_T *sb = NULL;
13980
13981 if (argvars[0].v_type != VAR_STRING
13982 || (argvars[1].v_type) != VAR_NUMBER
13983 || (argvars[2].v_type) != VAR_NUMBER)
13984 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013985 emsg(_(e_invarg));
Bram Moolenaarab186732018-09-14 21:27:06 +020013986 return;
13987 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013988 which = tv_get_string(&argvars[0]);
13989 value = tv_get_number(&argvars[1]);
13990 dragging = tv_get_number(&argvars[2]);
Bram Moolenaarab186732018-09-14 21:27:06 +020013991
13992 if (STRCMP(which, "left") == 0)
13993 sb = &curwin->w_scrollbars[SBAR_LEFT];
13994 else if (STRCMP(which, "right") == 0)
13995 sb = &curwin->w_scrollbars[SBAR_RIGHT];
13996 else if (STRCMP(which, "hor") == 0)
13997 sb = &gui.bottom_sbar;
13998 if (sb == NULL)
13999 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014000 semsg(_(e_invarg2), which);
Bram Moolenaarab186732018-09-14 21:27:06 +020014001 return;
14002 }
14003 gui_drag_scrollbar(sb, value, dragging);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020014004# ifndef USE_ON_FLY_SCROLL
14005 // need to loop through normal_cmd() to handle the scroll events
14006 exec_normal(FALSE, TRUE, FALSE);
14007# endif
Bram Moolenaarab186732018-09-14 21:27:06 +020014008}
14009#endif
14010
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014011 static void
14012f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
14013{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014014 time_for_testing = (time_t)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014015}
14016
14017#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
14018/*
14019 * Get a callback from "arg". It can be a Funcref or a function name.
14020 * When "arg" is zero return an empty string.
14021 * Return NULL for an invalid argument.
14022 */
14023 char_u *
14024get_callback(typval_T *arg, partial_T **pp)
14025{
14026 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
14027 {
14028 *pp = arg->vval.v_partial;
14029 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020014030 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014031 }
14032 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020014033 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014034 {
14035 func_ref(arg->vval.v_string);
14036 return arg->vval.v_string;
14037 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014038 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
14039 return (char_u *)"";
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014040 emsg(_("E921: Invalid callback argument"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014041 return NULL;
14042}
14043
14044/*
Bram Moolenaard5359b22018-04-05 22:44:39 +020014045 * Unref/free "callback" and "partial" returned by get_callback().
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014046 */
14047 void
14048free_callback(char_u *callback, partial_T *partial)
14049{
14050 if (partial != NULL)
14051 partial_unref(partial);
14052 else if (callback != NULL)
14053 {
14054 func_unref(callback);
14055 vim_free(callback);
14056 }
14057}
14058#endif
14059
14060#ifdef FEAT_TIMERS
14061/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014062 * "timer_info([timer])" function
14063 */
14064 static void
14065f_timer_info(typval_T *argvars, typval_T *rettv)
14066{
14067 timer_T *timer = NULL;
14068
14069 if (rettv_list_alloc(rettv) != OK)
14070 return;
14071 if (argvars[0].v_type != VAR_UNKNOWN)
14072 {
14073 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014074 emsg(_(e_number_exp));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014075 else
14076 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014077 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014078 if (timer != NULL)
14079 add_timer_info(rettv, timer);
14080 }
14081 }
14082 else
14083 add_timer_info_all(rettv);
14084}
14085
14086/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014087 * "timer_pause(timer, paused)" function
14088 */
14089 static void
14090f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
14091{
14092 timer_T *timer = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014093 int paused = (int)tv_get_number(&argvars[1]);
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014094
14095 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014096 emsg(_(e_number_exp));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014097 else
14098 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014099 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014100 if (timer != NULL)
14101 timer->tr_paused = paused;
14102 }
14103}
14104
14105/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014106 * "timer_start(time, callback [, options])" function
14107 */
14108 static void
14109f_timer_start(typval_T *argvars, typval_T *rettv)
14110{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014111 long msec = (long)tv_get_number(&argvars[0]);
Bram Moolenaar75537a92016-09-05 22:45:28 +020014112 timer_T *timer;
14113 int repeat = 0;
14114 char_u *callback;
14115 dict_T *dict;
14116 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014117
Bram Moolenaar75537a92016-09-05 22:45:28 +020014118 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014119 if (check_secure())
14120 return;
14121 if (argvars[2].v_type != VAR_UNKNOWN)
14122 {
14123 if (argvars[2].v_type != VAR_DICT
14124 || (dict = argvars[2].vval.v_dict) == NULL)
14125 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014126 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014127 return;
14128 }
14129 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014130 repeat = dict_get_number(dict, (char_u *)"repeat");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014131 }
14132
Bram Moolenaar75537a92016-09-05 22:45:28 +020014133 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014134 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020014135 return;
14136
14137 timer = create_timer(msec, repeat);
14138 if (timer == NULL)
14139 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014140 else
14141 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020014142 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020014143 timer->tr_callback = vim_strsave(callback);
14144 else
14145 /* pointer into the partial */
14146 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020014147 timer->tr_partial = partial;
14148 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014149 }
14150}
14151
14152/*
14153 * "timer_stop(timer)" function
14154 */
14155 static void
14156f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
14157{
14158 timer_T *timer;
14159
14160 if (argvars[0].v_type != VAR_NUMBER)
14161 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014162 emsg(_(e_number_exp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014163 return;
14164 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014165 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014166 if (timer != NULL)
14167 stop_timer(timer);
14168}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014169
14170/*
14171 * "timer_stopall()" function
14172 */
14173 static void
14174f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14175{
14176 stop_all_timers();
14177}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014178#endif
14179
14180/*
14181 * "tolower(string)" function
14182 */
14183 static void
14184f_tolower(typval_T *argvars, typval_T *rettv)
14185{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014186 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014187 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014188}
14189
14190/*
14191 * "toupper(string)" function
14192 */
14193 static void
14194f_toupper(typval_T *argvars, typval_T *rettv)
14195{
14196 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014197 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014198}
14199
14200/*
14201 * "tr(string, fromstr, tostr)" function
14202 */
14203 static void
14204f_tr(typval_T *argvars, typval_T *rettv)
14205{
14206 char_u *in_str;
14207 char_u *fromstr;
14208 char_u *tostr;
14209 char_u *p;
14210#ifdef FEAT_MBYTE
14211 int inlen;
14212 int fromlen;
14213 int tolen;
14214 int idx;
14215 char_u *cpstr;
14216 int cplen;
14217 int first = TRUE;
14218#endif
14219 char_u buf[NUMBUFLEN];
14220 char_u buf2[NUMBUFLEN];
14221 garray_T ga;
14222
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014223 in_str = tv_get_string(&argvars[0]);
14224 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
14225 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014226
14227 /* Default return value: empty string. */
14228 rettv->v_type = VAR_STRING;
14229 rettv->vval.v_string = NULL;
14230 if (fromstr == NULL || tostr == NULL)
14231 return; /* type error; errmsg already given */
14232 ga_init2(&ga, (int)sizeof(char), 80);
14233
14234#ifdef FEAT_MBYTE
14235 if (!has_mbyte)
14236#endif
14237 /* not multi-byte: fromstr and tostr must be the same length */
14238 if (STRLEN(fromstr) != STRLEN(tostr))
14239 {
14240#ifdef FEAT_MBYTE
14241error:
14242#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014243 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014244 ga_clear(&ga);
14245 return;
14246 }
14247
14248 /* fromstr and tostr have to contain the same number of chars */
14249 while (*in_str != NUL)
14250 {
14251#ifdef FEAT_MBYTE
14252 if (has_mbyte)
14253 {
14254 inlen = (*mb_ptr2len)(in_str);
14255 cpstr = in_str;
14256 cplen = inlen;
14257 idx = 0;
14258 for (p = fromstr; *p != NUL; p += fromlen)
14259 {
14260 fromlen = (*mb_ptr2len)(p);
14261 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
14262 {
14263 for (p = tostr; *p != NUL; p += tolen)
14264 {
14265 tolen = (*mb_ptr2len)(p);
14266 if (idx-- == 0)
14267 {
14268 cplen = tolen;
14269 cpstr = p;
14270 break;
14271 }
14272 }
14273 if (*p == NUL) /* tostr is shorter than fromstr */
14274 goto error;
14275 break;
14276 }
14277 ++idx;
14278 }
14279
14280 if (first && cpstr == in_str)
14281 {
14282 /* Check that fromstr and tostr have the same number of
14283 * (multi-byte) characters. Done only once when a character
14284 * of in_str doesn't appear in fromstr. */
14285 first = FALSE;
14286 for (p = tostr; *p != NUL; p += tolen)
14287 {
14288 tolen = (*mb_ptr2len)(p);
14289 --idx;
14290 }
14291 if (idx != 0)
14292 goto error;
14293 }
14294
14295 (void)ga_grow(&ga, cplen);
14296 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
14297 ga.ga_len += cplen;
14298
14299 in_str += inlen;
14300 }
14301 else
14302#endif
14303 {
14304 /* When not using multi-byte chars we can do it faster. */
14305 p = vim_strchr(fromstr, *in_str);
14306 if (p != NULL)
14307 ga_append(&ga, tostr[p - fromstr]);
14308 else
14309 ga_append(&ga, *in_str);
14310 ++in_str;
14311 }
14312 }
14313
14314 /* add a terminating NUL */
14315 (void)ga_grow(&ga, 1);
14316 ga_append(&ga, NUL);
14317
14318 rettv->vval.v_string = ga.ga_data;
14319}
14320
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014321/*
14322 * "trim({expr})" function
14323 */
14324 static void
14325f_trim(typval_T *argvars, typval_T *rettv)
14326{
14327 char_u buf1[NUMBUFLEN];
14328 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014329 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014330 char_u *mask = NULL;
14331 char_u *tail;
14332 char_u *prev;
14333 char_u *p;
14334 int c1;
14335
14336 rettv->v_type = VAR_STRING;
14337 if (head == NULL)
14338 {
14339 rettv->vval.v_string = NULL;
14340 return;
14341 }
14342
14343 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014344 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014345
14346 while (*head != NUL)
14347 {
14348 c1 = PTR2CHAR(head);
14349 if (mask == NULL)
14350 {
14351 if (c1 > ' ' && c1 != 0xa0)
14352 break;
14353 }
14354 else
14355 {
14356 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14357 if (c1 == PTR2CHAR(p))
14358 break;
14359 if (*p == NUL)
14360 break;
14361 }
14362 MB_PTR_ADV(head);
14363 }
14364
14365 for (tail = head + STRLEN(head); tail > head; tail = prev)
14366 {
14367 prev = tail;
14368 MB_PTR_BACK(head, prev);
14369 c1 = PTR2CHAR(prev);
14370 if (mask == NULL)
14371 {
14372 if (c1 > ' ' && c1 != 0xa0)
14373 break;
14374 }
14375 else
14376 {
14377 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14378 if (c1 == PTR2CHAR(p))
14379 break;
14380 if (*p == NUL)
14381 break;
14382 }
14383 }
14384 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
14385}
14386
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014387#ifdef FEAT_FLOAT
14388/*
14389 * "trunc({float})" function
14390 */
14391 static void
14392f_trunc(typval_T *argvars, typval_T *rettv)
14393{
14394 float_T f = 0.0;
14395
14396 rettv->v_type = VAR_FLOAT;
14397 if (get_float_arg(argvars, &f) == OK)
14398 /* trunc() is not in C90, use floor() or ceil() instead. */
14399 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
14400 else
14401 rettv->vval.v_float = 0.0;
14402}
14403#endif
14404
14405/*
14406 * "type(expr)" function
14407 */
14408 static void
14409f_type(typval_T *argvars, typval_T *rettv)
14410{
14411 int n = -1;
14412
14413 switch (argvars[0].v_type)
14414 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020014415 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
14416 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014417 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020014418 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
14419 case VAR_LIST: n = VAR_TYPE_LIST; break;
14420 case VAR_DICT: n = VAR_TYPE_DICT; break;
14421 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014422 case VAR_SPECIAL:
14423 if (argvars[0].vval.v_number == VVAL_FALSE
14424 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020014425 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014426 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020014427 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014428 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020014429 case VAR_JOB: n = VAR_TYPE_JOB; break;
14430 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014431 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014432 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010014433 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014434 n = -1;
14435 break;
14436 }
14437 rettv->vval.v_number = n;
14438}
14439
14440/*
14441 * "undofile(name)" function
14442 */
14443 static void
14444f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
14445{
14446 rettv->v_type = VAR_STRING;
14447#ifdef FEAT_PERSISTENT_UNDO
14448 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014449 char_u *fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014450
14451 if (*fname == NUL)
14452 {
14453 /* If there is no file name there will be no undo file. */
14454 rettv->vval.v_string = NULL;
14455 }
14456 else
14457 {
14458 char_u *ffname = FullName_save(fname, FALSE);
14459
14460 if (ffname != NULL)
14461 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
14462 vim_free(ffname);
14463 }
14464 }
14465#else
14466 rettv->vval.v_string = NULL;
14467#endif
14468}
14469
14470/*
14471 * "undotree()" function
14472 */
14473 static void
14474f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
14475{
14476 if (rettv_dict_alloc(rettv) == OK)
14477 {
14478 dict_T *dict = rettv->vval.v_dict;
14479 list_T *list;
14480
Bram Moolenaare0be1672018-07-08 16:50:37 +020014481 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
14482 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
14483 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
14484 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
14485 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
14486 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014487
14488 list = list_alloc();
14489 if (list != NULL)
14490 {
14491 u_eval_tree(curbuf->b_u_oldhead, list);
14492 dict_add_list(dict, "entries", list);
14493 }
14494 }
14495}
14496
14497/*
14498 * "values(dict)" function
14499 */
14500 static void
14501f_values(typval_T *argvars, typval_T *rettv)
14502{
14503 dict_list(argvars, rettv, 1);
14504}
14505
14506/*
14507 * "virtcol(string)" function
14508 */
14509 static void
14510f_virtcol(typval_T *argvars, typval_T *rettv)
14511{
14512 colnr_T vcol = 0;
14513 pos_T *fp;
14514 int fnum = curbuf->b_fnum;
14515
14516 fp = var2fpos(&argvars[0], FALSE, &fnum);
14517 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
14518 && fnum == curbuf->b_fnum)
14519 {
14520 getvvcol(curwin, fp, NULL, NULL, &vcol);
14521 ++vcol;
14522 }
14523
14524 rettv->vval.v_number = vcol;
14525}
14526
14527/*
14528 * "visualmode()" function
14529 */
14530 static void
14531f_visualmode(typval_T *argvars, typval_T *rettv)
14532{
14533 char_u str[2];
14534
14535 rettv->v_type = VAR_STRING;
14536 str[0] = curbuf->b_visual_mode_eval;
14537 str[1] = NUL;
14538 rettv->vval.v_string = vim_strsave(str);
14539
14540 /* A non-zero number or non-empty string argument: reset mode. */
14541 if (non_zero_arg(&argvars[0]))
14542 curbuf->b_visual_mode_eval = NUL;
14543}
14544
14545/*
14546 * "wildmenumode()" function
14547 */
14548 static void
14549f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14550{
14551#ifdef FEAT_WILDMENU
14552 if (wild_menu_showing)
14553 rettv->vval.v_number = 1;
14554#endif
14555}
14556
14557/*
14558 * "winbufnr(nr)" function
14559 */
14560 static void
14561f_winbufnr(typval_T *argvars, typval_T *rettv)
14562{
14563 win_T *wp;
14564
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014565 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014566 if (wp == NULL)
14567 rettv->vval.v_number = -1;
14568 else
14569 rettv->vval.v_number = wp->w_buffer->b_fnum;
14570}
14571
14572/*
14573 * "wincol()" function
14574 */
14575 static void
14576f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
14577{
14578 validate_cursor();
14579 rettv->vval.v_number = curwin->w_wcol + 1;
14580}
14581
14582/*
14583 * "winheight(nr)" function
14584 */
14585 static void
14586f_winheight(typval_T *argvars, typval_T *rettv)
14587{
14588 win_T *wp;
14589
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014590 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014591 if (wp == NULL)
14592 rettv->vval.v_number = -1;
14593 else
14594 rettv->vval.v_number = wp->w_height;
14595}
14596
14597/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020014598 * "winlayout()" function
14599 */
14600 static void
14601f_winlayout(typval_T *argvars, typval_T *rettv)
14602{
14603 tabpage_T *tp;
14604
14605 if (rettv_list_alloc(rettv) != OK)
14606 return;
14607
14608 if (argvars[0].v_type == VAR_UNKNOWN)
14609 tp = curtab;
14610 else
14611 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014612 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020014613 if (tp == NULL)
14614 return;
14615 }
14616
14617 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
14618}
14619
14620/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014621 * "winline()" function
14622 */
14623 static void
14624f_winline(typval_T *argvars UNUSED, typval_T *rettv)
14625{
14626 validate_cursor();
14627 rettv->vval.v_number = curwin->w_wrow + 1;
14628}
14629
14630/*
14631 * "winnr()" function
14632 */
14633 static void
14634f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
14635{
14636 int nr = 1;
14637
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014638 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014639 rettv->vval.v_number = nr;
14640}
14641
14642/*
14643 * "winrestcmd()" function
14644 */
14645 static void
14646f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
14647{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014648 win_T *wp;
14649 int winnr = 1;
14650 garray_T ga;
14651 char_u buf[50];
14652
14653 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020014654 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014655 {
14656 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
14657 ga_concat(&ga, buf);
14658 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
14659 ga_concat(&ga, buf);
14660 ++winnr;
14661 }
14662 ga_append(&ga, NUL);
14663
14664 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014665 rettv->v_type = VAR_STRING;
14666}
14667
14668/*
14669 * "winrestview()" function
14670 */
14671 static void
14672f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
14673{
14674 dict_T *dict;
14675
14676 if (argvars[0].v_type != VAR_DICT
14677 || (dict = argvars[0].vval.v_dict) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014678 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014679 else
14680 {
14681 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014682 curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014683 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014684 curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014685#ifdef FEAT_VIRTUALEDIT
14686 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014687 curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014688#endif
14689 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
14690 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010014691 curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014692 curwin->w_set_curswant = FALSE;
14693 }
14694
14695 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014696 set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014697#ifdef FEAT_DIFF
14698 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014699 curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014700#endif
14701 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014702 curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014703 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014704 curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014705
14706 check_cursor();
14707 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020014708 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014709 changed_window_setting();
14710
14711 if (curwin->w_topline <= 0)
14712 curwin->w_topline = 1;
14713 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
14714 curwin->w_topline = curbuf->b_ml.ml_line_count;
14715#ifdef FEAT_DIFF
14716 check_topfill(curwin, TRUE);
14717#endif
14718 }
14719}
14720
14721/*
14722 * "winsaveview()" function
14723 */
14724 static void
14725f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
14726{
14727 dict_T *dict;
14728
14729 if (rettv_dict_alloc(rettv) == FAIL)
14730 return;
14731 dict = rettv->vval.v_dict;
14732
Bram Moolenaare0be1672018-07-08 16:50:37 +020014733 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
14734 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014735#ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +020014736 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014737#endif
14738 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020014739 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014740
Bram Moolenaare0be1672018-07-08 16:50:37 +020014741 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014742#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020014743 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014744#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020014745 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
14746 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014747}
14748
14749/*
14750 * "winwidth(nr)" function
14751 */
14752 static void
14753f_winwidth(typval_T *argvars, typval_T *rettv)
14754{
14755 win_T *wp;
14756
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014757 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014758 if (wp == NULL)
14759 rettv->vval.v_number = -1;
14760 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014761 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014762}
14763
14764/*
14765 * "wordcount()" function
14766 */
14767 static void
14768f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
14769{
14770 if (rettv_dict_alloc(rettv) == FAIL)
14771 return;
14772 cursor_pos_info(rettv->vval.v_dict);
14773}
14774
14775/*
14776 * "writefile()" function
14777 */
14778 static void
14779f_writefile(typval_T *argvars, typval_T *rettv)
14780{
14781 int binary = FALSE;
14782 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014783#ifdef HAVE_FSYNC
14784 int do_fsync = p_fs;
14785#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014786 char_u *fname;
14787 FILE *fd;
14788 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014789 listitem_T *li;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014790 list_T *list = NULL;
14791 blob_T *blob = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014792
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014793 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014794 if (check_restricted() || check_secure())
14795 return;
14796
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014797 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014798 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014799 list = argvars[0].vval.v_list;
14800 if (list == NULL)
14801 return;
14802 for (li = list->lv_first; li != NULL; li = li->li_next)
14803 if (tv_get_string_chk(&li->li_tv) == NULL)
14804 return;
14805 }
14806 else if (argvars[0].v_type == VAR_BLOB)
14807 {
14808 blob = argvars[0].vval.v_blob;
14809 if (blob == NULL)
14810 return;
14811 }
14812 else
14813 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014814 semsg(_(e_invarg2), "writefile()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014815 return;
14816 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014817
14818 if (argvars[2].v_type != VAR_UNKNOWN)
14819 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014820 char_u *arg2 = tv_get_string_chk(&argvars[2]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014821
14822 if (arg2 == NULL)
14823 return;
14824 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014825 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014826 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014827 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014828#ifdef HAVE_FSYNC
14829 if (vim_strchr(arg2, 's') != NULL)
14830 do_fsync = TRUE;
14831 else if (vim_strchr(arg2, 'S') != NULL)
14832 do_fsync = FALSE;
14833#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014834 }
14835
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014836 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014837 if (fname == NULL)
14838 return;
14839
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014840 /* Always open the file in binary mode, library functions have a mind of
14841 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014842 if (*fname == NUL || (fd = mch_fopen((char *)fname,
14843 append ? APPENDBIN : WRITEBIN)) == NULL)
14844 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014845 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014846 ret = -1;
14847 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014848 else if (blob)
14849 {
14850 if (write_blob(fd, blob) == FAIL)
14851 ret = -1;
14852#ifdef HAVE_FSYNC
14853 else if (do_fsync)
14854 // Ignore the error, the user wouldn't know what to do about it.
14855 // May happen for a device.
14856 vim_ignored = fsync(fileno(fd));
14857#endif
14858 fclose(fd);
14859 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014860 else
14861 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014862 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014863 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014864#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010014865 else if (do_fsync)
14866 /* Ignore the error, the user wouldn't know what to do about it.
14867 * May happen for a device. */
Bram Moolenaar42335f52018-09-13 15:33:43 +020014868 vim_ignored = fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014869#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014870 fclose(fd);
14871 }
14872
14873 rettv->vval.v_number = ret;
14874}
14875
14876/*
14877 * "xor(expr, expr)" function
14878 */
14879 static void
14880f_xor(typval_T *argvars, typval_T *rettv)
14881{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014882 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
14883 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014884}
14885
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014886#endif /* FEAT_EVAL */