blob: 830700eaa473922a93962599e96787d9c63e8cc4 [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
6515#ifdef FEAT_SUN_WORKSHOP
6516 "sun_workshop",
6517#endif
6518#ifdef FEAT_NETBEANS_INTG
6519 "netbeans_intg",
6520#endif
6521#ifdef FEAT_SPELL
6522 "spell",
6523#endif
6524#ifdef FEAT_SYN_HL
6525 "syntax",
6526#endif
6527#if defined(USE_SYSTEM) || !defined(UNIX)
6528 "system",
6529#endif
6530#ifdef FEAT_TAG_BINS
6531 "tag_binary",
6532#endif
6533#ifdef FEAT_TAG_OLDSTATIC
6534 "tag_old_static",
6535#endif
6536#ifdef FEAT_TAG_ANYWHITE
6537 "tag_any_white",
6538#endif
6539#ifdef FEAT_TCL
6540# ifndef DYNAMIC_TCL
6541 "tcl",
6542# endif
6543#endif
6544#ifdef FEAT_TERMGUICOLORS
6545 "termguicolors",
6546#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006547#if defined(FEAT_TERMINAL) && !defined(WIN3264)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006548 "terminal",
6549#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006550#ifdef TERMINFO
6551 "terminfo",
6552#endif
6553#ifdef FEAT_TERMRESPONSE
6554 "termresponse",
6555#endif
6556#ifdef FEAT_TEXTOBJ
6557 "textobjects",
6558#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006559#ifdef FEAT_TEXT_PROP
6560 "textprop",
6561#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006562#ifdef HAVE_TGETENT
6563 "tgetent",
6564#endif
6565#ifdef FEAT_TIMERS
6566 "timers",
6567#endif
6568#ifdef FEAT_TITLE
6569 "title",
6570#endif
6571#ifdef FEAT_TOOLBAR
6572 "toolbar",
6573#endif
6574#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6575 "unnamedplus",
6576#endif
6577#ifdef FEAT_USR_CMDS
6578 "user-commands", /* was accidentally included in 5.4 */
6579 "user_commands",
6580#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006581#ifdef FEAT_VARTABS
6582 "vartabs",
6583#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006584#ifdef FEAT_VIMINFO
6585 "viminfo",
6586#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006587 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006588#ifdef FEAT_VIRTUALEDIT
6589 "virtualedit",
6590#endif
6591 "visual",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006592 "visualextra",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006593 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006594#ifdef FEAT_VTP
6595 "vtp",
6596#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006597#ifdef FEAT_WILDIGN
6598 "wildignore",
6599#endif
6600#ifdef FEAT_WILDMENU
6601 "wildmenu",
6602#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006603 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006604#ifdef FEAT_WAK
6605 "winaltkeys",
6606#endif
6607#ifdef FEAT_WRITEBACKUP
6608 "writebackup",
6609#endif
6610#ifdef FEAT_XIM
6611 "xim",
6612#endif
6613#ifdef FEAT_XFONTSET
6614 "xfontset",
6615#endif
6616#ifdef FEAT_XPM_W32
6617 "xpm",
6618 "xpm_w32", /* for backward compatibility */
6619#else
6620# if defined(HAVE_XPM)
6621 "xpm",
6622# endif
6623#endif
6624#ifdef USE_XSMP
6625 "xsmp",
6626#endif
6627#ifdef USE_XSMP_INTERACT
6628 "xsmp_interact",
6629#endif
6630#ifdef FEAT_XCLIPBOARD
6631 "xterm_clipboard",
6632#endif
6633#ifdef FEAT_XTERM_SAVE
6634 "xterm_save",
6635#endif
6636#if defined(UNIX) && defined(FEAT_X11)
6637 "X11",
6638#endif
6639 NULL
6640 };
6641
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006642 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006643 for (i = 0; has_list[i] != NULL; ++i)
6644 if (STRICMP(name, has_list[i]) == 0)
6645 {
6646 n = TRUE;
6647 break;
6648 }
6649
6650 if (n == FALSE)
6651 {
6652 if (STRNICMP(name, "patch", 5) == 0)
6653 {
6654 if (name[5] == '-'
6655 && STRLEN(name) >= 11
6656 && vim_isdigit(name[6])
6657 && vim_isdigit(name[8])
6658 && vim_isdigit(name[10]))
6659 {
6660 int major = atoi((char *)name + 6);
6661 int minor = atoi((char *)name + 8);
6662
6663 /* Expect "patch-9.9.01234". */
6664 n = (major < VIM_VERSION_MAJOR
6665 || (major == VIM_VERSION_MAJOR
6666 && (minor < VIM_VERSION_MINOR
6667 || (minor == VIM_VERSION_MINOR
6668 && has_patch(atoi((char *)name + 10))))));
6669 }
6670 else
6671 n = has_patch(atoi((char *)name + 5));
6672 }
6673 else if (STRICMP(name, "vim_starting") == 0)
6674 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006675 else if (STRICMP(name, "ttyin") == 0)
6676 n = mch_input_isatty();
6677 else if (STRICMP(name, "ttyout") == 0)
6678 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006679#ifdef FEAT_MBYTE
6680 else if (STRICMP(name, "multi_byte_encoding") == 0)
6681 n = has_mbyte;
6682#endif
6683#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
6684 else if (STRICMP(name, "balloon_multiline") == 0)
6685 n = multiline_balloon_available();
6686#endif
6687#ifdef DYNAMIC_TCL
6688 else if (STRICMP(name, "tcl") == 0)
6689 n = tcl_enabled(FALSE);
6690#endif
6691#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6692 else if (STRICMP(name, "iconv") == 0)
6693 n = iconv_enabled(FALSE);
6694#endif
6695#ifdef DYNAMIC_LUA
6696 else if (STRICMP(name, "lua") == 0)
6697 n = lua_enabled(FALSE);
6698#endif
6699#ifdef DYNAMIC_MZSCHEME
6700 else if (STRICMP(name, "mzscheme") == 0)
6701 n = mzscheme_enabled(FALSE);
6702#endif
6703#ifdef DYNAMIC_RUBY
6704 else if (STRICMP(name, "ruby") == 0)
6705 n = ruby_enabled(FALSE);
6706#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006707#ifdef DYNAMIC_PYTHON
6708 else if (STRICMP(name, "python") == 0)
6709 n = python_enabled(FALSE);
6710#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006711#ifdef DYNAMIC_PYTHON3
6712 else if (STRICMP(name, "python3") == 0)
6713 n = python3_enabled(FALSE);
6714#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006715#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6716 else if (STRICMP(name, "pythonx") == 0)
6717 {
6718# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6719 if (p_pyx == 0)
6720 n = python3_enabled(FALSE) || python_enabled(FALSE);
6721 else if (p_pyx == 3)
6722 n = python3_enabled(FALSE);
6723 else if (p_pyx == 2)
6724 n = python_enabled(FALSE);
6725# elif defined(DYNAMIC_PYTHON)
6726 n = python_enabled(FALSE);
6727# elif defined(DYNAMIC_PYTHON3)
6728 n = python3_enabled(FALSE);
6729# endif
6730 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006731#endif
6732#ifdef DYNAMIC_PERL
6733 else if (STRICMP(name, "perl") == 0)
6734 n = perl_enabled(FALSE);
6735#endif
6736#ifdef FEAT_GUI
6737 else if (STRICMP(name, "gui_running") == 0)
6738 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006739# ifdef FEAT_BROWSE
6740 else if (STRICMP(name, "browse") == 0)
6741 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6742# endif
6743#endif
6744#ifdef FEAT_SYN_HL
6745 else if (STRICMP(name, "syntax_items") == 0)
6746 n = syntax_present(curwin);
6747#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006748#ifdef FEAT_VTP
6749 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02006750 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006751#endif
6752#ifdef FEAT_NETBEANS_INTG
6753 else if (STRICMP(name, "netbeans_enabled") == 0)
6754 n = netbeans_active();
6755#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006756#if defined(FEAT_TERMINAL) && defined(WIN3264)
6757 else if (STRICMP(name, "terminal") == 0)
6758 n = terminal_enabled();
6759#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006760 }
6761
6762 rettv->vval.v_number = n;
6763}
6764
6765/*
6766 * "has_key()" function
6767 */
6768 static void
6769f_has_key(typval_T *argvars, typval_T *rettv)
6770{
6771 if (argvars[0].v_type != VAR_DICT)
6772 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006773 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006774 return;
6775 }
6776 if (argvars[0].vval.v_dict == NULL)
6777 return;
6778
6779 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006780 tv_get_string(&argvars[1]), -1) != NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006781}
6782
6783/*
6784 * "haslocaldir()" function
6785 */
6786 static void
6787f_haslocaldir(typval_T *argvars, typval_T *rettv)
6788{
6789 win_T *wp = NULL;
6790
6791 wp = find_tabwin(&argvars[0], &argvars[1]);
6792 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6793}
6794
6795/*
6796 * "hasmapto()" function
6797 */
6798 static void
6799f_hasmapto(typval_T *argvars, typval_T *rettv)
6800{
6801 char_u *name;
6802 char_u *mode;
6803 char_u buf[NUMBUFLEN];
6804 int abbr = FALSE;
6805
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006806 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006807 if (argvars[1].v_type == VAR_UNKNOWN)
6808 mode = (char_u *)"nvo";
6809 else
6810 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006811 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006812 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006813 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006814 }
6815
6816 if (map_to_exists(name, mode, abbr))
6817 rettv->vval.v_number = TRUE;
6818 else
6819 rettv->vval.v_number = FALSE;
6820}
6821
6822/*
6823 * "histadd()" function
6824 */
6825 static void
6826f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6827{
6828#ifdef FEAT_CMDHIST
6829 int histype;
6830 char_u *str;
6831 char_u buf[NUMBUFLEN];
6832#endif
6833
6834 rettv->vval.v_number = FALSE;
6835 if (check_restricted() || check_secure())
6836 return;
6837#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006838 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006839 histype = str != NULL ? get_histtype(str) : -1;
6840 if (histype >= 0)
6841 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006842 str = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006843 if (*str != NUL)
6844 {
6845 init_history();
6846 add_to_history(histype, str, FALSE, NUL);
6847 rettv->vval.v_number = TRUE;
6848 return;
6849 }
6850 }
6851#endif
6852}
6853
6854/*
6855 * "histdel()" function
6856 */
6857 static void
6858f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6859{
6860#ifdef FEAT_CMDHIST
6861 int n;
6862 char_u buf[NUMBUFLEN];
6863 char_u *str;
6864
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006865 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006866 if (str == NULL)
6867 n = 0;
6868 else if (argvars[1].v_type == VAR_UNKNOWN)
6869 /* only one argument: clear entire history */
6870 n = clr_history(get_histtype(str));
6871 else if (argvars[1].v_type == VAR_NUMBER)
6872 /* index given: remove that entry */
6873 n = del_history_idx(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006874 (int)tv_get_number(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006875 else
6876 /* string given: remove all matching entries */
6877 n = del_history_entry(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006878 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006879 rettv->vval.v_number = n;
6880#endif
6881}
6882
6883/*
6884 * "histget()" function
6885 */
6886 static void
6887f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6888{
6889#ifdef FEAT_CMDHIST
6890 int type;
6891 int idx;
6892 char_u *str;
6893
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006894 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006895 if (str == NULL)
6896 rettv->vval.v_string = NULL;
6897 else
6898 {
6899 type = get_histtype(str);
6900 if (argvars[1].v_type == VAR_UNKNOWN)
6901 idx = get_history_idx(type);
6902 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006903 idx = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006904 /* -1 on type error */
6905 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6906 }
6907#else
6908 rettv->vval.v_string = NULL;
6909#endif
6910 rettv->v_type = VAR_STRING;
6911}
6912
6913/*
6914 * "histnr()" function
6915 */
6916 static void
6917f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6918{
6919 int i;
6920
6921#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006922 char_u *history = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006923
6924 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6925 if (i >= HIST_CMD && i < HIST_COUNT)
6926 i = get_history_idx(i);
6927 else
6928#endif
6929 i = -1;
6930 rettv->vval.v_number = i;
6931}
6932
6933/*
6934 * "highlightID(name)" function
6935 */
6936 static void
6937f_hlID(typval_T *argvars, typval_T *rettv)
6938{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006939 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006940}
6941
6942/*
6943 * "highlight_exists()" function
6944 */
6945 static void
6946f_hlexists(typval_T *argvars, typval_T *rettv)
6947{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006948 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006949}
6950
6951/*
6952 * "hostname()" function
6953 */
6954 static void
6955f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6956{
6957 char_u hostname[256];
6958
6959 mch_get_host_name(hostname, 256);
6960 rettv->v_type = VAR_STRING;
6961 rettv->vval.v_string = vim_strsave(hostname);
6962}
6963
6964/*
6965 * iconv() function
6966 */
6967 static void
6968f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6969{
6970#ifdef FEAT_MBYTE
6971 char_u buf1[NUMBUFLEN];
6972 char_u buf2[NUMBUFLEN];
6973 char_u *from, *to, *str;
6974 vimconv_T vimconv;
6975#endif
6976
6977 rettv->v_type = VAR_STRING;
6978 rettv->vval.v_string = NULL;
6979
6980#ifdef FEAT_MBYTE
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006981 str = tv_get_string(&argvars[0]);
6982 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
6983 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006984 vimconv.vc_type = CONV_NONE;
6985 convert_setup(&vimconv, from, to);
6986
6987 /* If the encodings are equal, no conversion needed. */
6988 if (vimconv.vc_type == CONV_NONE)
6989 rettv->vval.v_string = vim_strsave(str);
6990 else
6991 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6992
6993 convert_setup(&vimconv, NULL, NULL);
6994 vim_free(from);
6995 vim_free(to);
6996#endif
6997}
6998
6999/*
7000 * "indent()" function
7001 */
7002 static void
7003f_indent(typval_T *argvars, typval_T *rettv)
7004{
7005 linenr_T lnum;
7006
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007007 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007008 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7009 rettv->vval.v_number = get_indent_lnum(lnum);
7010 else
7011 rettv->vval.v_number = -1;
7012}
7013
7014/*
7015 * "index()" function
7016 */
7017 static void
7018f_index(typval_T *argvars, typval_T *rettv)
7019{
7020 list_T *l;
7021 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007022 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007023 long idx = 0;
7024 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007025 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007026
7027 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007028 if (argvars[0].v_type == VAR_BLOB)
7029 {
7030 typval_T tv;
7031 int start = 0;
7032
7033 if (argvars[2].v_type != VAR_UNKNOWN)
7034 {
7035 start = tv_get_number_chk(&argvars[2], &error);
7036 if (error)
7037 return;
7038 }
7039 b = argvars[0].vval.v_blob;
7040 if (b == NULL)
7041 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01007042 if (start < 0)
7043 {
7044 start = blob_len(b) + start;
7045 if (start < 0)
7046 start = 0;
7047 }
7048
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007049 for (idx = start; idx < blob_len(b); ++idx)
7050 {
7051 tv.v_type = VAR_NUMBER;
7052 tv.vval.v_number = blob_get(b, idx);
7053 if (tv_equal(&tv, &argvars[1], ic, FALSE))
7054 {
7055 rettv->vval.v_number = idx;
7056 return;
7057 }
7058 }
7059 return;
7060 }
7061 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007062 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007063 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007064 return;
7065 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007066
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007067 l = argvars[0].vval.v_list;
7068 if (l != NULL)
7069 {
7070 item = l->lv_first;
7071 if (argvars[2].v_type != VAR_UNKNOWN)
7072 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007073 /* Start at specified item. Use the cached index that list_find()
7074 * sets, so that a negative number also works. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007075 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007076 idx = l->lv_idx;
7077 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007078 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007079 if (error)
7080 item = NULL;
7081 }
7082
7083 for ( ; item != NULL; item = item->li_next, ++idx)
7084 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
7085 {
7086 rettv->vval.v_number = idx;
7087 break;
7088 }
7089 }
7090}
7091
7092static int inputsecret_flag = 0;
7093
7094/*
7095 * "input()" function
7096 * Also handles inputsecret() when inputsecret is set.
7097 */
7098 static void
7099f_input(typval_T *argvars, typval_T *rettv)
7100{
7101 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
7102}
7103
7104/*
7105 * "inputdialog()" function
7106 */
7107 static void
7108f_inputdialog(typval_T *argvars, typval_T *rettv)
7109{
7110#if defined(FEAT_GUI_TEXTDIALOG)
7111 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
7112 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
7113 {
7114 char_u *message;
7115 char_u buf[NUMBUFLEN];
7116 char_u *defstr = (char_u *)"";
7117
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007118 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007119 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007120 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007121 vim_strncpy(IObuff, defstr, IOSIZE - 1);
7122 else
7123 IObuff[0] = NUL;
7124 if (message != NULL && defstr != NULL
7125 && do_dialog(VIM_QUESTION, NULL, message,
7126 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
7127 rettv->vval.v_string = vim_strsave(IObuff);
7128 else
7129 {
7130 if (message != NULL && defstr != NULL
7131 && argvars[1].v_type != VAR_UNKNOWN
7132 && argvars[2].v_type != VAR_UNKNOWN)
7133 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007134 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007135 else
7136 rettv->vval.v_string = NULL;
7137 }
7138 rettv->v_type = VAR_STRING;
7139 }
7140 else
7141#endif
7142 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
7143}
7144
7145/*
7146 * "inputlist()" function
7147 */
7148 static void
7149f_inputlist(typval_T *argvars, typval_T *rettv)
7150{
7151 listitem_T *li;
7152 int selected;
7153 int mouse_used;
7154
7155#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007156 /* While starting up, there is no place to enter text. When running tests
7157 * with --not-a-term we assume feedkeys() will be used. */
7158 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007159 return;
7160#endif
7161 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
7162 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007163 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007164 return;
7165 }
7166
7167 msg_start();
7168 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
7169 lines_left = Rows; /* avoid more prompt */
7170 msg_scroll = TRUE;
7171 msg_clr_eos();
7172
7173 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
7174 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007175 msg_puts(tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007176 msg_putchar('\n');
7177 }
7178
7179 /* Ask for choice. */
7180 selected = prompt_for_number(&mouse_used);
7181 if (mouse_used)
7182 selected -= lines_left;
7183
7184 rettv->vval.v_number = selected;
7185}
7186
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007187static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7188
7189/*
7190 * "inputrestore()" function
7191 */
7192 static void
7193f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7194{
7195 if (ga_userinput.ga_len > 0)
7196 {
7197 --ga_userinput.ga_len;
7198 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7199 + ga_userinput.ga_len);
7200 /* default return is zero == OK */
7201 }
7202 else if (p_verbose > 1)
7203 {
7204 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
7205 rettv->vval.v_number = 1; /* Failed */
7206 }
7207}
7208
7209/*
7210 * "inputsave()" function
7211 */
7212 static void
7213f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7214{
7215 /* Add an entry to the stack of typeahead storage. */
7216 if (ga_grow(&ga_userinput, 1) == OK)
7217 {
7218 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7219 + ga_userinput.ga_len);
7220 ++ga_userinput.ga_len;
7221 /* default return is zero == OK */
7222 }
7223 else
7224 rettv->vval.v_number = 1; /* Failed */
7225}
7226
7227/*
7228 * "inputsecret()" function
7229 */
7230 static void
7231f_inputsecret(typval_T *argvars, typval_T *rettv)
7232{
7233 ++cmdline_star;
7234 ++inputsecret_flag;
7235 f_input(argvars, rettv);
7236 --cmdline_star;
7237 --inputsecret_flag;
7238}
7239
7240/*
7241 * "insert()" function
7242 */
7243 static void
7244f_insert(typval_T *argvars, typval_T *rettv)
7245{
7246 long before = 0;
7247 listitem_T *item;
7248 list_T *l;
7249 int error = FALSE;
7250
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007251 if (argvars[0].v_type == VAR_BLOB)
7252 {
7253 int val, len;
7254 char_u *p;
7255
7256 len = blob_len(argvars[0].vval.v_blob);
7257 if (argvars[2].v_type != VAR_UNKNOWN)
7258 {
7259 before = (long)tv_get_number_chk(&argvars[2], &error);
7260 if (error)
7261 return; // type error; errmsg already given
7262 if (before < 0 || before > len)
7263 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007264 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007265 return;
7266 }
7267 }
7268 val = tv_get_number_chk(&argvars[1], &error);
7269 if (error)
7270 return;
7271 if (val < 0 || val > 255)
7272 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007273 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007274 return;
7275 }
7276
7277 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
7278 return;
7279 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
7280 mch_memmove(p + before + 1, p + before, (size_t)len - before);
7281 *(p + before) = val;
7282 ++argvars[0].vval.v_blob->bv_ga.ga_len;
7283
7284 copy_tv(&argvars[0], rettv);
7285 }
7286 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007287 semsg(_(e_listarg), "insert()");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007288 else if ((l = argvars[0].vval.v_list) != NULL && !tv_check_lock(l->lv_lock,
7289 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007290 {
7291 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007292 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007293 if (error)
7294 return; /* type error; errmsg already given */
7295
7296 if (before == l->lv_len)
7297 item = NULL;
7298 else
7299 {
7300 item = list_find(l, before);
7301 if (item == NULL)
7302 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007303 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007304 l = NULL;
7305 }
7306 }
7307 if (l != NULL)
7308 {
7309 list_insert_tv(l, &argvars[1], item);
7310 copy_tv(&argvars[0], rettv);
7311 }
7312 }
7313}
7314
7315/*
7316 * "invert(expr)" function
7317 */
7318 static void
7319f_invert(typval_T *argvars, typval_T *rettv)
7320{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007321 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007322}
7323
7324/*
7325 * "isdirectory()" function
7326 */
7327 static void
7328f_isdirectory(typval_T *argvars, typval_T *rettv)
7329{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007330 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007331}
7332
7333/*
7334 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7335 * or it refers to a List or Dictionary that is locked.
7336 */
7337 static int
7338tv_islocked(typval_T *tv)
7339{
7340 return (tv->v_lock & VAR_LOCKED)
7341 || (tv->v_type == VAR_LIST
7342 && tv->vval.v_list != NULL
7343 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7344 || (tv->v_type == VAR_DICT
7345 && tv->vval.v_dict != NULL
7346 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7347}
7348
7349/*
7350 * "islocked()" function
7351 */
7352 static void
7353f_islocked(typval_T *argvars, typval_T *rettv)
7354{
7355 lval_T lv;
7356 char_u *end;
7357 dictitem_T *di;
7358
7359 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007360 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007361 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007362 if (end != NULL && lv.ll_name != NULL)
7363 {
7364 if (*end != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007365 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007366 else
7367 {
7368 if (lv.ll_tv == NULL)
7369 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007370 di = find_var(lv.ll_name, NULL, TRUE);
7371 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007372 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007373 /* Consider a variable locked when:
7374 * 1. the variable itself is locked
7375 * 2. the value of the variable is locked.
7376 * 3. the List or Dict value is locked.
7377 */
7378 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7379 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007380 }
7381 }
7382 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007383 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007384 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007385 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007386 else if (lv.ll_list != NULL)
7387 /* List item. */
7388 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7389 else
7390 /* Dictionary item. */
7391 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7392 }
7393 }
7394
7395 clear_lval(&lv);
7396}
7397
7398#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7399/*
7400 * "isnan()" function
7401 */
7402 static void
7403f_isnan(typval_T *argvars, typval_T *rettv)
7404{
7405 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7406 && isnan(argvars[0].vval.v_float);
7407}
7408#endif
7409
7410/*
7411 * "items(dict)" function
7412 */
7413 static void
7414f_items(typval_T *argvars, typval_T *rettv)
7415{
7416 dict_list(argvars, rettv, 2);
7417}
7418
7419#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
7420/*
7421 * Get the job from the argument.
7422 * Returns NULL if the job is invalid.
7423 */
7424 static job_T *
7425get_job_arg(typval_T *tv)
7426{
7427 job_T *job;
7428
7429 if (tv->v_type != VAR_JOB)
7430 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007431 semsg(_(e_invarg2), tv_get_string(tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007432 return NULL;
7433 }
7434 job = tv->vval.v_job;
7435
7436 if (job == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007437 emsg(_("E916: not a valid job"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007438 return job;
7439}
7440
7441/*
7442 * "job_getchannel()" function
7443 */
7444 static void
7445f_job_getchannel(typval_T *argvars, typval_T *rettv)
7446{
7447 job_T *job = get_job_arg(&argvars[0]);
7448
7449 if (job != NULL)
7450 {
7451 rettv->v_type = VAR_CHANNEL;
7452 rettv->vval.v_channel = job->jv_channel;
7453 if (job->jv_channel != NULL)
7454 ++job->jv_channel->ch_refcount;
7455 }
7456}
7457
7458/*
7459 * "job_info()" function
7460 */
7461 static void
7462f_job_info(typval_T *argvars, typval_T *rettv)
7463{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007464 if (argvars[0].v_type != VAR_UNKNOWN)
7465 {
7466 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007467
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007468 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7469 job_info(job, rettv->vval.v_dict);
7470 }
7471 else if (rettv_list_alloc(rettv) == OK)
7472 job_info_all(rettv->vval.v_list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007473}
7474
7475/*
7476 * "job_setoptions()" function
7477 */
7478 static void
7479f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7480{
7481 job_T *job = get_job_arg(&argvars[0]);
7482 jobopt_T opt;
7483
7484 if (job == NULL)
7485 return;
7486 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007487 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007488 job_set_options(job, &opt);
7489 free_job_options(&opt);
7490}
7491
7492/*
7493 * "job_start()" function
7494 */
7495 static void
7496f_job_start(typval_T *argvars, typval_T *rettv)
7497{
7498 rettv->v_type = VAR_JOB;
7499 if (check_restricted() || check_secure())
7500 return;
Bram Moolenaar493359e2018-06-12 20:25:52 +02007501 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007502}
7503
7504/*
7505 * "job_status()" function
7506 */
7507 static void
7508f_job_status(typval_T *argvars, typval_T *rettv)
7509{
7510 job_T *job = get_job_arg(&argvars[0]);
7511
7512 if (job != NULL)
7513 {
7514 rettv->v_type = VAR_STRING;
7515 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7516 }
7517}
7518
7519/*
7520 * "job_stop()" function
7521 */
7522 static void
7523f_job_stop(typval_T *argvars, typval_T *rettv)
7524{
7525 job_T *job = get_job_arg(&argvars[0]);
7526
7527 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007528 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007529}
7530#endif
7531
7532/*
7533 * "join()" function
7534 */
7535 static void
7536f_join(typval_T *argvars, typval_T *rettv)
7537{
7538 garray_T ga;
7539 char_u *sep;
7540
7541 if (argvars[0].v_type != VAR_LIST)
7542 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007543 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007544 return;
7545 }
7546 if (argvars[0].vval.v_list == NULL)
7547 return;
7548 if (argvars[1].v_type == VAR_UNKNOWN)
7549 sep = (char_u *)" ";
7550 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007551 sep = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007552
7553 rettv->v_type = VAR_STRING;
7554
7555 if (sep != NULL)
7556 {
7557 ga_init2(&ga, (int)sizeof(char), 80);
7558 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7559 ga_append(&ga, NUL);
7560 rettv->vval.v_string = (char_u *)ga.ga_data;
7561 }
7562 else
7563 rettv->vval.v_string = NULL;
7564}
7565
7566/*
7567 * "js_decode()" function
7568 */
7569 static void
7570f_js_decode(typval_T *argvars, typval_T *rettv)
7571{
7572 js_read_T reader;
7573
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007574 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007575 reader.js_fill = NULL;
7576 reader.js_used = 0;
7577 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007578 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007579}
7580
7581/*
7582 * "js_encode()" function
7583 */
7584 static void
7585f_js_encode(typval_T *argvars, typval_T *rettv)
7586{
7587 rettv->v_type = VAR_STRING;
7588 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7589}
7590
7591/*
7592 * "json_decode()" function
7593 */
7594 static void
7595f_json_decode(typval_T *argvars, typval_T *rettv)
7596{
7597 js_read_T reader;
7598
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007599 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007600 reader.js_fill = NULL;
7601 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007602 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007603}
7604
7605/*
7606 * "json_encode()" function
7607 */
7608 static void
7609f_json_encode(typval_T *argvars, typval_T *rettv)
7610{
7611 rettv->v_type = VAR_STRING;
7612 rettv->vval.v_string = json_encode(&argvars[0], 0);
7613}
7614
7615/*
7616 * "keys()" function
7617 */
7618 static void
7619f_keys(typval_T *argvars, typval_T *rettv)
7620{
7621 dict_list(argvars, rettv, 0);
7622}
7623
7624/*
7625 * "last_buffer_nr()" function.
7626 */
7627 static void
7628f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7629{
7630 int n = 0;
7631 buf_T *buf;
7632
Bram Moolenaar29323592016-07-24 22:04:11 +02007633 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007634 if (n < buf->b_fnum)
7635 n = buf->b_fnum;
7636
7637 rettv->vval.v_number = n;
7638}
7639
7640/*
7641 * "len()" function
7642 */
7643 static void
7644f_len(typval_T *argvars, typval_T *rettv)
7645{
7646 switch (argvars[0].v_type)
7647 {
7648 case VAR_STRING:
7649 case VAR_NUMBER:
7650 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007651 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007652 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007653 case VAR_BLOB:
7654 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
7655 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007656 case VAR_LIST:
7657 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7658 break;
7659 case VAR_DICT:
7660 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7661 break;
7662 case VAR_UNKNOWN:
7663 case VAR_SPECIAL:
7664 case VAR_FLOAT:
7665 case VAR_FUNC:
7666 case VAR_PARTIAL:
7667 case VAR_JOB:
7668 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007669 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007670 break;
7671 }
7672}
7673
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007674 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007675libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007676{
7677#ifdef FEAT_LIBCALL
7678 char_u *string_in;
7679 char_u **string_result;
7680 int nr_result;
7681#endif
7682
7683 rettv->v_type = type;
7684 if (type != VAR_NUMBER)
7685 rettv->vval.v_string = NULL;
7686
7687 if (check_restricted() || check_secure())
7688 return;
7689
7690#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007691 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007692 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7693 {
7694 string_in = NULL;
7695 if (argvars[2].v_type == VAR_STRING)
7696 string_in = argvars[2].vval.v_string;
7697 if (type == VAR_NUMBER)
7698 string_result = NULL;
7699 else
7700 string_result = &rettv->vval.v_string;
7701 if (mch_libcall(argvars[0].vval.v_string,
7702 argvars[1].vval.v_string,
7703 string_in,
7704 argvars[2].vval.v_number,
7705 string_result,
7706 &nr_result) == OK
7707 && type == VAR_NUMBER)
7708 rettv->vval.v_number = nr_result;
7709 }
7710#endif
7711}
7712
7713/*
7714 * "libcall()" function
7715 */
7716 static void
7717f_libcall(typval_T *argvars, typval_T *rettv)
7718{
7719 libcall_common(argvars, rettv, VAR_STRING);
7720}
7721
7722/*
7723 * "libcallnr()" function
7724 */
7725 static void
7726f_libcallnr(typval_T *argvars, typval_T *rettv)
7727{
7728 libcall_common(argvars, rettv, VAR_NUMBER);
7729}
7730
7731/*
7732 * "line(string)" function
7733 */
7734 static void
7735f_line(typval_T *argvars, typval_T *rettv)
7736{
7737 linenr_T lnum = 0;
7738 pos_T *fp;
7739 int fnum;
7740
7741 fp = var2fpos(&argvars[0], TRUE, &fnum);
7742 if (fp != NULL)
7743 lnum = fp->lnum;
7744 rettv->vval.v_number = lnum;
7745}
7746
7747/*
7748 * "line2byte(lnum)" function
7749 */
7750 static void
7751f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7752{
7753#ifndef FEAT_BYTEOFF
7754 rettv->vval.v_number = -1;
7755#else
7756 linenr_T lnum;
7757
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007758 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007759 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7760 rettv->vval.v_number = -1;
7761 else
7762 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7763 if (rettv->vval.v_number >= 0)
7764 ++rettv->vval.v_number;
7765#endif
7766}
7767
7768/*
7769 * "lispindent(lnum)" function
7770 */
7771 static void
7772f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7773{
7774#ifdef FEAT_LISP
7775 pos_T pos;
7776 linenr_T lnum;
7777
7778 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007779 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007780 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7781 {
7782 curwin->w_cursor.lnum = lnum;
7783 rettv->vval.v_number = get_lisp_indent();
7784 curwin->w_cursor = pos;
7785 }
7786 else
7787#endif
7788 rettv->vval.v_number = -1;
7789}
7790
7791/*
7792 * "localtime()" function
7793 */
7794 static void
7795f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7796{
7797 rettv->vval.v_number = (varnumber_T)time(NULL);
7798}
7799
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007800 static void
7801get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7802{
7803 char_u *keys;
7804 char_u *which;
7805 char_u buf[NUMBUFLEN];
7806 char_u *keys_buf = NULL;
7807 char_u *rhs;
7808 int mode;
7809 int abbr = FALSE;
7810 int get_dict = FALSE;
7811 mapblock_T *mp;
7812 int buffer_local;
7813
7814 /* return empty string for failure */
7815 rettv->v_type = VAR_STRING;
7816 rettv->vval.v_string = NULL;
7817
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007818 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007819 if (*keys == NUL)
7820 return;
7821
7822 if (argvars[1].v_type != VAR_UNKNOWN)
7823 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007824 which = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007825 if (argvars[2].v_type != VAR_UNKNOWN)
7826 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007827 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007828 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007829 get_dict = (int)tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007830 }
7831 }
7832 else
7833 which = (char_u *)"";
7834 if (which == NULL)
7835 return;
7836
7837 mode = get_map_mode(&which, 0);
7838
7839 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7840 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7841 vim_free(keys_buf);
7842
7843 if (!get_dict)
7844 {
7845 /* Return a string. */
7846 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02007847 {
7848 if (*rhs == NUL)
7849 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
7850 else
7851 rettv->vval.v_string = str2special_save(rhs, FALSE);
7852 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007853
7854 }
7855 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7856 {
7857 /* Return a dictionary. */
7858 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7859 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7860 dict_T *dict = rettv->vval.v_dict;
7861
Bram Moolenaare0be1672018-07-08 16:50:37 +02007862 dict_add_string(dict, "lhs", lhs);
7863 dict_add_string(dict, "rhs", mp->m_orig_str);
7864 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
7865 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
7866 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007867 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
7868 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02007869 dict_add_number(dict, "buffer", (long)buffer_local);
7870 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
7871 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007872
7873 vim_free(lhs);
7874 vim_free(mapmode);
7875 }
7876}
7877
7878#ifdef FEAT_FLOAT
7879/*
7880 * "log()" function
7881 */
7882 static void
7883f_log(typval_T *argvars, typval_T *rettv)
7884{
7885 float_T f = 0.0;
7886
7887 rettv->v_type = VAR_FLOAT;
7888 if (get_float_arg(argvars, &f) == OK)
7889 rettv->vval.v_float = log(f);
7890 else
7891 rettv->vval.v_float = 0.0;
7892}
7893
7894/*
7895 * "log10()" function
7896 */
7897 static void
7898f_log10(typval_T *argvars, typval_T *rettv)
7899{
7900 float_T f = 0.0;
7901
7902 rettv->v_type = VAR_FLOAT;
7903 if (get_float_arg(argvars, &f) == OK)
7904 rettv->vval.v_float = log10(f);
7905 else
7906 rettv->vval.v_float = 0.0;
7907}
7908#endif
7909
7910#ifdef FEAT_LUA
7911/*
7912 * "luaeval()" function
7913 */
7914 static void
7915f_luaeval(typval_T *argvars, typval_T *rettv)
7916{
7917 char_u *str;
7918 char_u buf[NUMBUFLEN];
7919
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007920 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007921 do_luaeval(str, argvars + 1, rettv);
7922}
7923#endif
7924
7925/*
7926 * "map()" function
7927 */
7928 static void
7929f_map(typval_T *argvars, typval_T *rettv)
7930{
7931 filter_map(argvars, rettv, TRUE);
7932}
7933
7934/*
7935 * "maparg()" function
7936 */
7937 static void
7938f_maparg(typval_T *argvars, typval_T *rettv)
7939{
7940 get_maparg(argvars, rettv, TRUE);
7941}
7942
7943/*
7944 * "mapcheck()" function
7945 */
7946 static void
7947f_mapcheck(typval_T *argvars, typval_T *rettv)
7948{
7949 get_maparg(argvars, rettv, FALSE);
7950}
7951
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007952typedef enum
7953{
7954 MATCH_END, /* matchend() */
7955 MATCH_MATCH, /* match() */
7956 MATCH_STR, /* matchstr() */
7957 MATCH_LIST, /* matchlist() */
7958 MATCH_POS /* matchstrpos() */
7959} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007960
7961 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007962find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007963{
7964 char_u *str = NULL;
7965 long len = 0;
7966 char_u *expr = NULL;
7967 char_u *pat;
7968 regmatch_T regmatch;
7969 char_u patbuf[NUMBUFLEN];
7970 char_u strbuf[NUMBUFLEN];
7971 char_u *save_cpo;
7972 long start = 0;
7973 long nth = 1;
7974 colnr_T startcol = 0;
7975 int match = 0;
7976 list_T *l = NULL;
7977 listitem_T *li = NULL;
7978 long idx = 0;
7979 char_u *tofree = NULL;
7980
7981 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7982 save_cpo = p_cpo;
7983 p_cpo = (char_u *)"";
7984
7985 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007986 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007987 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007988 /* type MATCH_LIST: return empty list when there are no matches.
7989 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007990 if (rettv_list_alloc(rettv) == FAIL)
7991 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007992 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007993 && (list_append_string(rettv->vval.v_list,
7994 (char_u *)"", 0) == FAIL
7995 || list_append_number(rettv->vval.v_list,
7996 (varnumber_T)-1) == FAIL
7997 || list_append_number(rettv->vval.v_list,
7998 (varnumber_T)-1) == FAIL
7999 || list_append_number(rettv->vval.v_list,
8000 (varnumber_T)-1) == FAIL))
8001 {
8002 list_free(rettv->vval.v_list);
8003 rettv->vval.v_list = NULL;
8004 goto theend;
8005 }
8006 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008007 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008008 {
8009 rettv->v_type = VAR_STRING;
8010 rettv->vval.v_string = NULL;
8011 }
8012
8013 if (argvars[0].v_type == VAR_LIST)
8014 {
8015 if ((l = argvars[0].vval.v_list) == NULL)
8016 goto theend;
8017 li = l->lv_first;
8018 }
8019 else
8020 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008021 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008022 len = (long)STRLEN(str);
8023 }
8024
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008025 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008026 if (pat == NULL)
8027 goto theend;
8028
8029 if (argvars[2].v_type != VAR_UNKNOWN)
8030 {
8031 int error = FALSE;
8032
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008033 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008034 if (error)
8035 goto theend;
8036 if (l != NULL)
8037 {
8038 li = list_find(l, start);
8039 if (li == NULL)
8040 goto theend;
8041 idx = l->lv_idx; /* use the cached index */
8042 }
8043 else
8044 {
8045 if (start < 0)
8046 start = 0;
8047 if (start > len)
8048 goto theend;
8049 /* When "count" argument is there ignore matches before "start",
8050 * otherwise skip part of the string. Differs when pattern is "^"
8051 * or "\<". */
8052 if (argvars[3].v_type != VAR_UNKNOWN)
8053 startcol = start;
8054 else
8055 {
8056 str += start;
8057 len -= start;
8058 }
8059 }
8060
8061 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008062 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008063 if (error)
8064 goto theend;
8065 }
8066
8067 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8068 if (regmatch.regprog != NULL)
8069 {
8070 regmatch.rm_ic = p_ic;
8071
8072 for (;;)
8073 {
8074 if (l != NULL)
8075 {
8076 if (li == NULL)
8077 {
8078 match = FALSE;
8079 break;
8080 }
8081 vim_free(tofree);
8082 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
8083 if (str == NULL)
8084 break;
8085 }
8086
8087 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
8088
8089 if (match && --nth <= 0)
8090 break;
8091 if (l == NULL && !match)
8092 break;
8093
8094 /* Advance to just after the match. */
8095 if (l != NULL)
8096 {
8097 li = li->li_next;
8098 ++idx;
8099 }
8100 else
8101 {
8102#ifdef FEAT_MBYTE
8103 startcol = (colnr_T)(regmatch.startp[0]
8104 + (*mb_ptr2len)(regmatch.startp[0]) - str);
8105#else
8106 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
8107#endif
8108 if (startcol > (colnr_T)len
8109 || str + startcol <= regmatch.startp[0])
8110 {
8111 match = FALSE;
8112 break;
8113 }
8114 }
8115 }
8116
8117 if (match)
8118 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008119 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008120 {
8121 listitem_T *li1 = rettv->vval.v_list->lv_first;
8122 listitem_T *li2 = li1->li_next;
8123 listitem_T *li3 = li2->li_next;
8124 listitem_T *li4 = li3->li_next;
8125
8126 vim_free(li1->li_tv.vval.v_string);
8127 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
8128 (int)(regmatch.endp[0] - regmatch.startp[0]));
8129 li3->li_tv.vval.v_number =
8130 (varnumber_T)(regmatch.startp[0] - expr);
8131 li4->li_tv.vval.v_number =
8132 (varnumber_T)(regmatch.endp[0] - expr);
8133 if (l != NULL)
8134 li2->li_tv.vval.v_number = (varnumber_T)idx;
8135 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008136 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008137 {
8138 int i;
8139
8140 /* return list with matched string and submatches */
8141 for (i = 0; i < NSUBEXP; ++i)
8142 {
8143 if (regmatch.endp[i] == NULL)
8144 {
8145 if (list_append_string(rettv->vval.v_list,
8146 (char_u *)"", 0) == FAIL)
8147 break;
8148 }
8149 else if (list_append_string(rettv->vval.v_list,
8150 regmatch.startp[i],
8151 (int)(regmatch.endp[i] - regmatch.startp[i]))
8152 == FAIL)
8153 break;
8154 }
8155 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008156 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008157 {
8158 /* return matched string */
8159 if (l != NULL)
8160 copy_tv(&li->li_tv, rettv);
8161 else
8162 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
8163 (int)(regmatch.endp[0] - regmatch.startp[0]));
8164 }
8165 else if (l != NULL)
8166 rettv->vval.v_number = idx;
8167 else
8168 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008169 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008170 rettv->vval.v_number =
8171 (varnumber_T)(regmatch.startp[0] - str);
8172 else
8173 rettv->vval.v_number =
8174 (varnumber_T)(regmatch.endp[0] - str);
8175 rettv->vval.v_number += (varnumber_T)(str - expr);
8176 }
8177 }
8178 vim_regfree(regmatch.regprog);
8179 }
8180
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008181theend:
8182 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008183 /* matchstrpos() without a list: drop the second item. */
8184 listitem_remove(rettv->vval.v_list,
8185 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008186 vim_free(tofree);
8187 p_cpo = save_cpo;
8188}
8189
8190/*
8191 * "match()" function
8192 */
8193 static void
8194f_match(typval_T *argvars, typval_T *rettv)
8195{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008196 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008197}
8198
Bram Moolenaar95e51472018-07-28 16:55:56 +02008199#ifdef FEAT_SEARCH_EXTRA
8200 static int
8201matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
8202{
8203 dictitem_T *di;
8204
8205 if (tv->v_type != VAR_DICT)
8206 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008207 emsg(_(e_dictreq));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008208 return FAIL;
8209 }
8210
8211 if (dict_find(tv->vval.v_dict, (char_u *)"conceal", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01008212 *conceal_char = dict_get_string(tv->vval.v_dict,
Bram Moolenaar95e51472018-07-28 16:55:56 +02008213 (char_u *)"conceal", FALSE);
8214
8215 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
8216 {
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02008217 *win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008218 if (*win == NULL)
8219 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008220 emsg(_("E957: Invalid window number"));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008221 return FAIL;
8222 }
8223 }
8224
8225 return OK;
8226}
8227#endif
8228
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008229/*
8230 * "matchadd()" function
8231 */
8232 static void
8233f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8234{
8235#ifdef FEAT_SEARCH_EXTRA
8236 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008237 char_u *grp = tv_get_string_buf_chk(&argvars[0], buf); /* group */
8238 char_u *pat = tv_get_string_buf_chk(&argvars[1], buf); /* pattern */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008239 int prio = 10; /* default priority */
8240 int id = -1;
8241 int error = FALSE;
8242 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008243 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008244
8245 rettv->vval.v_number = -1;
8246
8247 if (grp == NULL || pat == NULL)
8248 return;
8249 if (argvars[2].v_type != VAR_UNKNOWN)
8250 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008251 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008252 if (argvars[3].v_type != VAR_UNKNOWN)
8253 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008254 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008255 if (argvars[4].v_type != VAR_UNKNOWN
8256 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8257 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008258 }
8259 }
8260 if (error == TRUE)
8261 return;
8262 if (id >= 1 && id <= 3)
8263 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008264 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008265 return;
8266 }
8267
Bram Moolenaar95e51472018-07-28 16:55:56 +02008268 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008269 conceal_char);
8270#endif
8271}
8272
8273/*
8274 * "matchaddpos()" function
8275 */
8276 static void
8277f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8278{
8279#ifdef FEAT_SEARCH_EXTRA
8280 char_u buf[NUMBUFLEN];
8281 char_u *group;
8282 int prio = 10;
8283 int id = -1;
8284 int error = FALSE;
8285 list_T *l;
8286 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008287 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008288
8289 rettv->vval.v_number = -1;
8290
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008291 group = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008292 if (group == NULL)
8293 return;
8294
8295 if (argvars[1].v_type != VAR_LIST)
8296 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008297 semsg(_(e_listarg), "matchaddpos()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008298 return;
8299 }
8300 l = argvars[1].vval.v_list;
8301 if (l == NULL)
8302 return;
8303
8304 if (argvars[2].v_type != VAR_UNKNOWN)
8305 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008306 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008307 if (argvars[3].v_type != VAR_UNKNOWN)
8308 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008309 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008310
8311 if (argvars[4].v_type != VAR_UNKNOWN
8312 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8313 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008314 }
8315 }
8316 if (error == TRUE)
8317 return;
8318
8319 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8320 if (id == 1 || id == 2)
8321 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008322 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008323 return;
8324 }
8325
Bram Moolenaar95e51472018-07-28 16:55:56 +02008326 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008327 conceal_char);
8328#endif
8329}
8330
8331/*
8332 * "matcharg()" function
8333 */
8334 static void
8335f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8336{
8337 if (rettv_list_alloc(rettv) == OK)
8338 {
8339#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008340 int id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008341 matchitem_T *m;
8342
8343 if (id >= 1 && id <= 3)
8344 {
8345 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8346 {
8347 list_append_string(rettv->vval.v_list,
8348 syn_id2name(m->hlg_id), -1);
8349 list_append_string(rettv->vval.v_list, m->pattern, -1);
8350 }
8351 else
8352 {
8353 list_append_string(rettv->vval.v_list, NULL, -1);
8354 list_append_string(rettv->vval.v_list, NULL, -1);
8355 }
8356 }
8357#endif
8358 }
8359}
8360
8361/*
8362 * "matchdelete()" function
8363 */
8364 static void
8365f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8366{
8367#ifdef FEAT_SEARCH_EXTRA
8368 rettv->vval.v_number = match_delete(curwin,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008369 (int)tv_get_number(&argvars[0]), TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008370#endif
8371}
8372
8373/*
8374 * "matchend()" function
8375 */
8376 static void
8377f_matchend(typval_T *argvars, typval_T *rettv)
8378{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008379 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008380}
8381
8382/*
8383 * "matchlist()" function
8384 */
8385 static void
8386f_matchlist(typval_T *argvars, typval_T *rettv)
8387{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008388 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008389}
8390
8391/*
8392 * "matchstr()" function
8393 */
8394 static void
8395f_matchstr(typval_T *argvars, typval_T *rettv)
8396{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008397 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008398}
8399
8400/*
8401 * "matchstrpos()" function
8402 */
8403 static void
8404f_matchstrpos(typval_T *argvars, typval_T *rettv)
8405{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008406 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008407}
8408
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008409 static void
8410max_min(typval_T *argvars, typval_T *rettv, int domax)
8411{
8412 varnumber_T n = 0;
8413 varnumber_T i;
8414 int error = FALSE;
8415
8416 if (argvars[0].v_type == VAR_LIST)
8417 {
8418 list_T *l;
8419 listitem_T *li;
8420
8421 l = argvars[0].vval.v_list;
8422 if (l != NULL)
8423 {
8424 li = l->lv_first;
8425 if (li != NULL)
8426 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008427 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008428 for (;;)
8429 {
8430 li = li->li_next;
8431 if (li == NULL)
8432 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008433 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008434 if (domax ? i > n : i < n)
8435 n = i;
8436 }
8437 }
8438 }
8439 }
8440 else if (argvars[0].v_type == VAR_DICT)
8441 {
8442 dict_T *d;
8443 int first = TRUE;
8444 hashitem_T *hi;
8445 int todo;
8446
8447 d = argvars[0].vval.v_dict;
8448 if (d != NULL)
8449 {
8450 todo = (int)d->dv_hashtab.ht_used;
8451 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8452 {
8453 if (!HASHITEM_EMPTY(hi))
8454 {
8455 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008456 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008457 if (first)
8458 {
8459 n = i;
8460 first = FALSE;
8461 }
8462 else if (domax ? i > n : i < n)
8463 n = i;
8464 }
8465 }
8466 }
8467 }
8468 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008469 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008470 rettv->vval.v_number = error ? 0 : n;
8471}
8472
8473/*
8474 * "max()" function
8475 */
8476 static void
8477f_max(typval_T *argvars, typval_T *rettv)
8478{
8479 max_min(argvars, rettv, TRUE);
8480}
8481
8482/*
8483 * "min()" function
8484 */
8485 static void
8486f_min(typval_T *argvars, typval_T *rettv)
8487{
8488 max_min(argvars, rettv, FALSE);
8489}
8490
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008491/*
8492 * Create the directory in which "dir" is located, and higher levels when
8493 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008494 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008495 */
8496 static int
8497mkdir_recurse(char_u *dir, int prot)
8498{
8499 char_u *p;
8500 char_u *updir;
8501 int r = FAIL;
8502
8503 /* Get end of directory name in "dir".
8504 * We're done when it's "/" or "c:/". */
8505 p = gettail_sep(dir);
8506 if (p <= get_past_head(dir))
8507 return OK;
8508
8509 /* If the directory exists we're done. Otherwise: create it.*/
8510 updir = vim_strnsave(dir, (int)(p - dir));
8511 if (updir == NULL)
8512 return FAIL;
8513 if (mch_isdir(updir))
8514 r = OK;
8515 else if (mkdir_recurse(updir, prot) == OK)
8516 r = vim_mkdir_emsg(updir, prot);
8517 vim_free(updir);
8518 return r;
8519}
8520
8521#ifdef vim_mkdir
8522/*
8523 * "mkdir()" function
8524 */
8525 static void
8526f_mkdir(typval_T *argvars, typval_T *rettv)
8527{
8528 char_u *dir;
8529 char_u buf[NUMBUFLEN];
8530 int prot = 0755;
8531
8532 rettv->vval.v_number = FAIL;
8533 if (check_restricted() || check_secure())
8534 return;
8535
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008536 dir = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008537 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008538 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008539
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008540 if (*gettail(dir) == NUL)
8541 /* remove trailing slashes */
8542 *gettail_sep(dir) = NUL;
8543
8544 if (argvars[1].v_type != VAR_UNKNOWN)
8545 {
8546 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008547 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008548 prot = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008549 if (prot == -1)
8550 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008551 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008552 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008553 {
8554 if (mch_isdir(dir))
8555 {
8556 /* With the "p" flag it's OK if the dir already exists. */
8557 rettv->vval.v_number = OK;
8558 return;
8559 }
8560 mkdir_recurse(dir, prot);
8561 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008562 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008563 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008564}
8565#endif
8566
8567/*
8568 * "mode()" function
8569 */
8570 static void
8571f_mode(typval_T *argvars, typval_T *rettv)
8572{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008573 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008574
Bram Moolenaar612cc382018-07-29 15:34:26 +02008575 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008576
8577 if (time_for_testing == 93784)
8578 {
8579 /* Testing the two-character code. */
8580 buf[0] = 'x';
8581 buf[1] = '!';
8582 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008583#ifdef FEAT_TERMINAL
8584 else if (term_use_loop())
8585 buf[0] = 't';
8586#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008587 else if (VIsual_active)
8588 {
8589 if (VIsual_select)
8590 buf[0] = VIsual_mode + 's' - 'v';
8591 else
8592 buf[0] = VIsual_mode;
8593 }
8594 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8595 || State == CONFIRM)
8596 {
8597 buf[0] = 'r';
8598 if (State == ASKMORE)
8599 buf[1] = 'm';
8600 else if (State == CONFIRM)
8601 buf[1] = '?';
8602 }
8603 else if (State == EXTERNCMD)
8604 buf[0] = '!';
8605 else if (State & INSERT)
8606 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008607 if (State & VREPLACE_FLAG)
8608 {
8609 buf[0] = 'R';
8610 buf[1] = 'v';
8611 }
8612 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008613 {
8614 if (State & REPLACE_FLAG)
8615 buf[0] = 'R';
8616 else
8617 buf[0] = 'i';
8618#ifdef FEAT_INS_EXPAND
8619 if (ins_compl_active())
8620 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008621 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008622 buf[1] = 'x';
8623#endif
8624 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008625 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008626 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008627 {
8628 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008629 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008630 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008631 else if (exmode_active == EXMODE_NORMAL)
8632 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008633 }
8634 else
8635 {
8636 buf[0] = 'n';
8637 if (finish_op)
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008638 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008639 buf[1] = 'o';
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008640 // to be able to detect force-linewise/blockwise/characterwise operations
8641 buf[2] = motion_force;
8642 }
Bram Moolenaar612cc382018-07-29 15:34:26 +02008643 else if (restart_edit == 'I' || restart_edit == 'R'
8644 || restart_edit == 'V')
8645 {
8646 buf[1] = 'i';
8647 buf[2] = restart_edit;
8648 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008649 }
8650
8651 /* Clear out the minor mode when the argument is not a non-zero number or
8652 * non-empty string. */
8653 if (!non_zero_arg(&argvars[0]))
8654 buf[1] = NUL;
8655
8656 rettv->vval.v_string = vim_strsave(buf);
8657 rettv->v_type = VAR_STRING;
8658}
8659
8660#if defined(FEAT_MZSCHEME) || defined(PROTO)
8661/*
8662 * "mzeval()" function
8663 */
8664 static void
8665f_mzeval(typval_T *argvars, typval_T *rettv)
8666{
8667 char_u *str;
8668 char_u buf[NUMBUFLEN];
8669
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008670 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008671 do_mzeval(str, rettv);
8672}
8673
8674 void
8675mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8676{
8677 typval_T argvars[3];
8678
8679 argvars[0].v_type = VAR_STRING;
8680 argvars[0].vval.v_string = name;
8681 copy_tv(args, &argvars[1]);
8682 argvars[2].v_type = VAR_UNKNOWN;
8683 f_call(argvars, rettv);
8684 clear_tv(&argvars[1]);
8685}
8686#endif
8687
8688/*
8689 * "nextnonblank()" function
8690 */
8691 static void
8692f_nextnonblank(typval_T *argvars, typval_T *rettv)
8693{
8694 linenr_T lnum;
8695
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008696 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008697 {
8698 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8699 {
8700 lnum = 0;
8701 break;
8702 }
8703 if (*skipwhite(ml_get(lnum)) != NUL)
8704 break;
8705 }
8706 rettv->vval.v_number = lnum;
8707}
8708
8709/*
8710 * "nr2char()" function
8711 */
8712 static void
8713f_nr2char(typval_T *argvars, typval_T *rettv)
8714{
8715 char_u buf[NUMBUFLEN];
8716
8717#ifdef FEAT_MBYTE
8718 if (has_mbyte)
8719 {
8720 int utf8 = 0;
8721
8722 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008723 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008724 if (utf8)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008725 buf[(*utf_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008726 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008727 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008728 }
8729 else
8730#endif
8731 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008732 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008733 buf[1] = NUL;
8734 }
8735 rettv->v_type = VAR_STRING;
8736 rettv->vval.v_string = vim_strsave(buf);
8737}
8738
8739/*
8740 * "or(expr, expr)" function
8741 */
8742 static void
8743f_or(typval_T *argvars, typval_T *rettv)
8744{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008745 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
8746 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008747}
8748
8749/*
8750 * "pathshorten()" function
8751 */
8752 static void
8753f_pathshorten(typval_T *argvars, typval_T *rettv)
8754{
8755 char_u *p;
8756
8757 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008758 p = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008759 if (p == NULL)
8760 rettv->vval.v_string = NULL;
8761 else
8762 {
8763 p = vim_strsave(p);
8764 rettv->vval.v_string = p;
8765 if (p != NULL)
8766 shorten_dir(p);
8767 }
8768}
8769
8770#ifdef FEAT_PERL
8771/*
8772 * "perleval()" function
8773 */
8774 static void
8775f_perleval(typval_T *argvars, typval_T *rettv)
8776{
8777 char_u *str;
8778 char_u buf[NUMBUFLEN];
8779
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008780 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008781 do_perleval(str, rettv);
8782}
8783#endif
8784
8785#ifdef FEAT_FLOAT
8786/*
8787 * "pow()" function
8788 */
8789 static void
8790f_pow(typval_T *argvars, typval_T *rettv)
8791{
8792 float_T fx = 0.0, fy = 0.0;
8793
8794 rettv->v_type = VAR_FLOAT;
8795 if (get_float_arg(argvars, &fx) == OK
8796 && get_float_arg(&argvars[1], &fy) == OK)
8797 rettv->vval.v_float = pow(fx, fy);
8798 else
8799 rettv->vval.v_float = 0.0;
8800}
8801#endif
8802
8803/*
8804 * "prevnonblank()" function
8805 */
8806 static void
8807f_prevnonblank(typval_T *argvars, typval_T *rettv)
8808{
8809 linenr_T lnum;
8810
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008811 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008812 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8813 lnum = 0;
8814 else
8815 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8816 --lnum;
8817 rettv->vval.v_number = lnum;
8818}
8819
8820/* This dummy va_list is here because:
8821 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8822 * - locally in the function results in a "used before set" warning
8823 * - using va_start() to initialize it gives "function with fixed args" error */
8824static va_list ap;
8825
8826/*
8827 * "printf()" function
8828 */
8829 static void
8830f_printf(typval_T *argvars, typval_T *rettv)
8831{
8832 char_u buf[NUMBUFLEN];
8833 int len;
8834 char_u *s;
8835 int saved_did_emsg = did_emsg;
8836 char *fmt;
8837
8838 rettv->v_type = VAR_STRING;
8839 rettv->vval.v_string = NULL;
8840
8841 /* Get the required length, allocate the buffer and do it for real. */
8842 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008843 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008844 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008845 if (!did_emsg)
8846 {
8847 s = alloc(len + 1);
8848 if (s != NULL)
8849 {
8850 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008851 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8852 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008853 }
8854 }
8855 did_emsg |= saved_did_emsg;
8856}
8857
Bram Moolenaarf2732452018-06-03 14:47:35 +02008858#ifdef FEAT_JOB_CHANNEL
8859/*
8860 * "prompt_setcallback({buffer}, {callback})" function
8861 */
8862 static void
8863f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
8864{
8865 buf_T *buf;
8866 char_u *callback;
8867 partial_T *partial;
8868
8869 if (check_secure())
8870 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01008871 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008872 if (buf == NULL)
8873 return;
8874
8875 callback = get_callback(&argvars[1], &partial);
8876 if (callback == NULL)
8877 return;
8878
8879 free_callback(buf->b_prompt_callback, buf->b_prompt_partial);
8880 if (partial == NULL)
8881 buf->b_prompt_callback = vim_strsave(callback);
8882 else
8883 /* pointer into the partial */
8884 buf->b_prompt_callback = callback;
8885 buf->b_prompt_partial = partial;
8886}
8887
8888/*
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02008889 * "prompt_setinterrupt({buffer}, {callback})" function
8890 */
8891 static void
8892f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
8893{
8894 buf_T *buf;
8895 char_u *callback;
8896 partial_T *partial;
8897
8898 if (check_secure())
8899 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01008900 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02008901 if (buf == NULL)
8902 return;
8903
8904 callback = get_callback(&argvars[1], &partial);
8905 if (callback == NULL)
8906 return;
8907
8908 free_callback(buf->b_prompt_interrupt, buf->b_prompt_int_partial);
8909 if (partial == NULL)
8910 buf->b_prompt_interrupt = vim_strsave(callback);
8911 else
8912 /* pointer into the partial */
8913 buf->b_prompt_interrupt = callback;
8914 buf->b_prompt_int_partial = partial;
8915}
8916
8917/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02008918 * "prompt_setprompt({buffer}, {text})" function
8919 */
8920 static void
8921f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
8922{
8923 buf_T *buf;
8924 char_u *text;
8925
8926 if (check_secure())
8927 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01008928 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008929 if (buf == NULL)
8930 return;
8931
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008932 text = tv_get_string(&argvars[1]);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008933 vim_free(buf->b_prompt_text);
8934 buf->b_prompt_text = vim_strsave(text);
8935}
8936#endif
8937
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008938/*
8939 * "pumvisible()" function
8940 */
8941 static void
8942f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8943{
8944#ifdef FEAT_INS_EXPAND
8945 if (pum_visible())
8946 rettv->vval.v_number = 1;
8947#endif
8948}
8949
8950#ifdef FEAT_PYTHON3
8951/*
8952 * "py3eval()" function
8953 */
8954 static void
8955f_py3eval(typval_T *argvars, typval_T *rettv)
8956{
8957 char_u *str;
8958 char_u buf[NUMBUFLEN];
8959
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008960 if (p_pyx == 0)
8961 p_pyx = 3;
8962
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008963 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008964 do_py3eval(str, rettv);
8965}
8966#endif
8967
8968#ifdef FEAT_PYTHON
8969/*
8970 * "pyeval()" function
8971 */
8972 static void
8973f_pyeval(typval_T *argvars, typval_T *rettv)
8974{
8975 char_u *str;
8976 char_u buf[NUMBUFLEN];
8977
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008978 if (p_pyx == 0)
8979 p_pyx = 2;
8980
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008981 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008982 do_pyeval(str, rettv);
8983}
8984#endif
8985
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008986#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8987/*
8988 * "pyxeval()" function
8989 */
8990 static void
8991f_pyxeval(typval_T *argvars, typval_T *rettv)
8992{
8993# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8994 init_pyxversion();
8995 if (p_pyx == 2)
8996 f_pyeval(argvars, rettv);
8997 else
8998 f_py3eval(argvars, rettv);
8999# elif defined(FEAT_PYTHON)
9000 f_pyeval(argvars, rettv);
9001# elif defined(FEAT_PYTHON3)
9002 f_py3eval(argvars, rettv);
9003# endif
9004}
9005#endif
9006
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009007/*
9008 * "range()" function
9009 */
9010 static void
9011f_range(typval_T *argvars, typval_T *rettv)
9012{
9013 varnumber_T start;
9014 varnumber_T end;
9015 varnumber_T stride = 1;
9016 varnumber_T i;
9017 int error = FALSE;
9018
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009019 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009020 if (argvars[1].v_type == VAR_UNKNOWN)
9021 {
9022 end = start - 1;
9023 start = 0;
9024 }
9025 else
9026 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009027 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009028 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009029 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009030 }
9031
9032 if (error)
9033 return; /* type error; errmsg already given */
9034 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009035 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009036 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009037 emsg(_("E727: Start past end"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009038 else
9039 {
9040 if (rettv_list_alloc(rettv) == OK)
9041 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
9042 if (list_append_number(rettv->vval.v_list,
9043 (varnumber_T)i) == FAIL)
9044 break;
9045 }
9046}
9047
9048/*
9049 * "readfile()" function
9050 */
9051 static void
9052f_readfile(typval_T *argvars, typval_T *rettv)
9053{
9054 int binary = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009055 int blob = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009056 int failed = FALSE;
9057 char_u *fname;
9058 FILE *fd;
9059 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
9060 int io_size = sizeof(buf);
9061 int readlen; /* size of last fread() */
9062 char_u *prev = NULL; /* previously read bytes, if any */
9063 long prevlen = 0; /* length of data in prev */
9064 long prevsize = 0; /* size of prev buffer */
9065 long maxline = MAXLNUM;
9066 long cnt = 0;
9067 char_u *p; /* position in buf */
9068 char_u *start; /* start of current line */
9069
9070 if (argvars[1].v_type != VAR_UNKNOWN)
9071 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009072 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009073 binary = TRUE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009074 if (STRCMP(tv_get_string(&argvars[1]), "B") == 0)
9075 blob = TRUE;
9076
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009077 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009078 maxline = (long)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009079 }
9080
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009081 if (blob)
9082 {
9083 if (rettv_blob_alloc(rettv) == FAIL)
9084 return;
9085 }
9086 else
9087 {
9088 if (rettv_list_alloc(rettv) == FAIL)
9089 return;
9090 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009091
9092 /* Always open the file in binary mode, library functions have a mind of
9093 * their own about CR-LF conversion. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009094 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009095 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
9096 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009097 semsg(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009098 return;
9099 }
9100
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009101 if (blob)
9102 {
9103 if (read_blob(fd, rettv->vval.v_blob) == FAIL)
9104 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009105 emsg("cannot read file");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009106 blob_free(rettv->vval.v_blob);
9107 }
9108 fclose(fd);
9109 return;
9110 }
9111
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009112 while (cnt < maxline || maxline < 0)
9113 {
9114 readlen = (int)fread(buf, 1, io_size, fd);
9115
9116 /* This for loop processes what was read, but is also entered at end
9117 * of file so that either:
9118 * - an incomplete line gets written
9119 * - a "binary" file gets an empty line at the end if it ends in a
9120 * newline. */
9121 for (p = buf, start = buf;
9122 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
9123 ++p)
9124 {
9125 if (*p == '\n' || readlen <= 0)
9126 {
9127 listitem_T *li;
9128 char_u *s = NULL;
9129 long_u len = p - start;
9130
9131 /* Finished a line. Remove CRs before NL. */
9132 if (readlen > 0 && !binary)
9133 {
9134 while (len > 0 && start[len - 1] == '\r')
9135 --len;
9136 /* removal may cross back to the "prev" string */
9137 if (len == 0)
9138 while (prevlen > 0 && prev[prevlen - 1] == '\r')
9139 --prevlen;
9140 }
9141 if (prevlen == 0)
9142 s = vim_strnsave(start, (int)len);
9143 else
9144 {
9145 /* Change "prev" buffer to be the right size. This way
9146 * the bytes are only copied once, and very long lines are
9147 * allocated only once. */
9148 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
9149 {
9150 mch_memmove(s + prevlen, start, len);
9151 s[prevlen + len] = NUL;
9152 prev = NULL; /* the list will own the string */
9153 prevlen = prevsize = 0;
9154 }
9155 }
9156 if (s == NULL)
9157 {
9158 do_outofmem_msg((long_u) prevlen + len + 1);
9159 failed = TRUE;
9160 break;
9161 }
9162
9163 if ((li = listitem_alloc()) == NULL)
9164 {
9165 vim_free(s);
9166 failed = TRUE;
9167 break;
9168 }
9169 li->li_tv.v_type = VAR_STRING;
9170 li->li_tv.v_lock = 0;
9171 li->li_tv.vval.v_string = s;
9172 list_append(rettv->vval.v_list, li);
9173
9174 start = p + 1; /* step over newline */
9175 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
9176 break;
9177 }
9178 else if (*p == NUL)
9179 *p = '\n';
9180#ifdef FEAT_MBYTE
9181 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
9182 * when finding the BF and check the previous two bytes. */
9183 else if (*p == 0xbf && enc_utf8 && !binary)
9184 {
9185 /* Find the two bytes before the 0xbf. If p is at buf, or buf
9186 * + 1, these may be in the "prev" string. */
9187 char_u back1 = p >= buf + 1 ? p[-1]
9188 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
9189 char_u back2 = p >= buf + 2 ? p[-2]
9190 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
9191 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
9192
9193 if (back2 == 0xef && back1 == 0xbb)
9194 {
9195 char_u *dest = p - 2;
9196
9197 /* Usually a BOM is at the beginning of a file, and so at
9198 * the beginning of a line; then we can just step over it.
9199 */
9200 if (start == dest)
9201 start = p + 1;
9202 else
9203 {
9204 /* have to shuffle buf to close gap */
9205 int adjust_prevlen = 0;
9206
9207 if (dest < buf)
9208 {
9209 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
9210 dest = buf;
9211 }
9212 if (readlen > p - buf + 1)
9213 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
9214 readlen -= 3 - adjust_prevlen;
9215 prevlen -= adjust_prevlen;
9216 p = dest - 1;
9217 }
9218 }
9219 }
9220#endif
9221 } /* for */
9222
9223 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
9224 break;
9225 if (start < p)
9226 {
9227 /* There's part of a line in buf, store it in "prev". */
9228 if (p - start + prevlen >= prevsize)
9229 {
9230 /* need bigger "prev" buffer */
9231 char_u *newprev;
9232
9233 /* A common use case is ordinary text files and "prev" gets a
9234 * fragment of a line, so the first allocation is made
9235 * small, to avoid repeatedly 'allocing' large and
9236 * 'reallocing' small. */
9237 if (prevsize == 0)
9238 prevsize = (long)(p - start);
9239 else
9240 {
9241 long grow50pc = (prevsize * 3) / 2;
9242 long growmin = (long)((p - start) * 2 + prevlen);
9243 prevsize = grow50pc > growmin ? grow50pc : growmin;
9244 }
9245 newprev = prev == NULL ? alloc(prevsize)
9246 : vim_realloc(prev, prevsize);
9247 if (newprev == NULL)
9248 {
9249 do_outofmem_msg((long_u)prevsize);
9250 failed = TRUE;
9251 break;
9252 }
9253 prev = newprev;
9254 }
9255 /* Add the line part to end of "prev". */
9256 mch_memmove(prev + prevlen, start, p - start);
9257 prevlen += (long)(p - start);
9258 }
9259 } /* while */
9260
9261 /*
9262 * For a negative line count use only the lines at the end of the file,
9263 * free the rest.
9264 */
9265 if (!failed && maxline < 0)
9266 while (cnt > -maxline)
9267 {
9268 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
9269 --cnt;
9270 }
9271
9272 if (failed)
9273 {
9274 list_free(rettv->vval.v_list);
9275 /* readfile doc says an empty list is returned on error */
9276 rettv->vval.v_list = list_alloc();
9277 }
9278
9279 vim_free(prev);
9280 fclose(fd);
9281}
9282
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009283 static void
9284return_register(int regname, typval_T *rettv)
9285{
9286 char_u buf[2] = {0, 0};
9287
9288 buf[0] = (char_u)regname;
9289 rettv->v_type = VAR_STRING;
9290 rettv->vval.v_string = vim_strsave(buf);
9291}
9292
9293/*
9294 * "reg_executing()" function
9295 */
9296 static void
9297f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9298{
9299 return_register(reg_executing, rettv);
9300}
9301
9302/*
9303 * "reg_recording()" function
9304 */
9305 static void
9306f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9307{
9308 return_register(reg_recording, rettv);
9309}
9310
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009311#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009312/*
9313 * Convert a List to proftime_T.
9314 * Return FAIL when there is something wrong.
9315 */
9316 static int
9317list2proftime(typval_T *arg, proftime_T *tm)
9318{
9319 long n1, n2;
9320 int error = FALSE;
9321
9322 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9323 || arg->vval.v_list->lv_len != 2)
9324 return FAIL;
9325 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9326 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
9327# ifdef WIN3264
9328 tm->HighPart = n1;
9329 tm->LowPart = n2;
9330# else
9331 tm->tv_sec = n1;
9332 tm->tv_usec = n2;
9333# endif
9334 return error ? FAIL : OK;
9335}
9336#endif /* FEAT_RELTIME */
9337
9338/*
9339 * "reltime()" function
9340 */
9341 static void
9342f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9343{
9344#ifdef FEAT_RELTIME
9345 proftime_T res;
9346 proftime_T start;
9347
9348 if (argvars[0].v_type == VAR_UNKNOWN)
9349 {
9350 /* No arguments: get current time. */
9351 profile_start(&res);
9352 }
9353 else if (argvars[1].v_type == VAR_UNKNOWN)
9354 {
9355 if (list2proftime(&argvars[0], &res) == FAIL)
9356 return;
9357 profile_end(&res);
9358 }
9359 else
9360 {
9361 /* Two arguments: compute the difference. */
9362 if (list2proftime(&argvars[0], &start) == FAIL
9363 || list2proftime(&argvars[1], &res) == FAIL)
9364 return;
9365 profile_sub(&res, &start);
9366 }
9367
9368 if (rettv_list_alloc(rettv) == OK)
9369 {
9370 long n1, n2;
9371
9372# ifdef WIN3264
9373 n1 = res.HighPart;
9374 n2 = res.LowPart;
9375# else
9376 n1 = res.tv_sec;
9377 n2 = res.tv_usec;
9378# endif
9379 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9380 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9381 }
9382#endif
9383}
9384
9385#ifdef FEAT_FLOAT
9386/*
9387 * "reltimefloat()" function
9388 */
9389 static void
9390f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9391{
9392# ifdef FEAT_RELTIME
9393 proftime_T tm;
9394# endif
9395
9396 rettv->v_type = VAR_FLOAT;
9397 rettv->vval.v_float = 0;
9398# ifdef FEAT_RELTIME
9399 if (list2proftime(&argvars[0], &tm) == OK)
9400 rettv->vval.v_float = profile_float(&tm);
9401# endif
9402}
9403#endif
9404
9405/*
9406 * "reltimestr()" function
9407 */
9408 static void
9409f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9410{
9411#ifdef FEAT_RELTIME
9412 proftime_T tm;
9413#endif
9414
9415 rettv->v_type = VAR_STRING;
9416 rettv->vval.v_string = NULL;
9417#ifdef FEAT_RELTIME
9418 if (list2proftime(&argvars[0], &tm) == OK)
9419 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9420#endif
9421}
9422
9423#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009424 static void
9425make_connection(void)
9426{
9427 if (X_DISPLAY == NULL
9428# ifdef FEAT_GUI
9429 && !gui.in_use
9430# endif
9431 )
9432 {
9433 x_force_connect = TRUE;
9434 setup_term_clip();
9435 x_force_connect = FALSE;
9436 }
9437}
9438
9439 static int
9440check_connection(void)
9441{
9442 make_connection();
9443 if (X_DISPLAY == NULL)
9444 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009445 emsg(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009446 return FAIL;
9447 }
9448 return OK;
9449}
9450#endif
9451
9452#ifdef FEAT_CLIENTSERVER
9453 static void
9454remote_common(typval_T *argvars, typval_T *rettv, int expr)
9455{
9456 char_u *server_name;
9457 char_u *keys;
9458 char_u *r = NULL;
9459 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009460 int timeout = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009461# ifdef WIN32
9462 HWND w;
9463# else
9464 Window w;
9465# endif
9466
9467 if (check_restricted() || check_secure())
9468 return;
9469
9470# ifdef FEAT_X11
9471 if (check_connection() == FAIL)
9472 return;
9473# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009474 if (argvars[2].v_type != VAR_UNKNOWN
9475 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009476 timeout = tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009477
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009478 server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009479 if (server_name == NULL)
9480 return; /* type error; errmsg already given */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009481 keys = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009482# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009483 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009484# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009485 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9486 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009487# endif
9488 {
9489 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009490 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009491 emsg((char *)r); // sending worked but evaluation failed
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009492 vim_free(r);
9493 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009494 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009495 semsg(_("E241: Unable to send to %s"), server_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009496 return;
9497 }
9498
9499 rettv->vval.v_string = r;
9500
9501 if (argvars[2].v_type != VAR_UNKNOWN)
9502 {
9503 dictitem_T v;
9504 char_u str[30];
9505 char_u *idvar;
9506
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009507 idvar = tv_get_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009508 if (idvar != NULL && *idvar != NUL)
9509 {
9510 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9511 v.di_tv.v_type = VAR_STRING;
9512 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009513 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009514 vim_free(v.di_tv.vval.v_string);
9515 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009516 }
9517}
9518#endif
9519
9520/*
9521 * "remote_expr()" function
9522 */
9523 static void
9524f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9525{
9526 rettv->v_type = VAR_STRING;
9527 rettv->vval.v_string = NULL;
9528#ifdef FEAT_CLIENTSERVER
9529 remote_common(argvars, rettv, TRUE);
9530#endif
9531}
9532
9533/*
9534 * "remote_foreground()" function
9535 */
9536 static void
9537f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9538{
9539#ifdef FEAT_CLIENTSERVER
9540# ifdef WIN32
9541 /* On Win32 it's done in this application. */
9542 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009543 char_u *server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009544
9545 if (server_name != NULL)
9546 serverForeground(server_name);
9547 }
9548# else
9549 /* Send a foreground() expression to the server. */
9550 argvars[1].v_type = VAR_STRING;
9551 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9552 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009553 rettv->v_type = VAR_STRING;
9554 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009555 remote_common(argvars, rettv, TRUE);
9556 vim_free(argvars[1].vval.v_string);
9557# endif
9558#endif
9559}
9560
9561 static void
9562f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
9563{
9564#ifdef FEAT_CLIENTSERVER
9565 dictitem_T v;
9566 char_u *s = NULL;
9567# ifdef WIN32
9568 long_u n = 0;
9569# endif
9570 char_u *serverid;
9571
9572 if (check_restricted() || check_secure())
9573 {
9574 rettv->vval.v_number = -1;
9575 return;
9576 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009577 serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009578 if (serverid == NULL)
9579 {
9580 rettv->vval.v_number = -1;
9581 return; /* type error; errmsg already given */
9582 }
9583# ifdef WIN32
9584 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
9585 if (n == 0)
9586 rettv->vval.v_number = -1;
9587 else
9588 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009589 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009590 rettv->vval.v_number = (s != NULL);
9591 }
9592# else
9593 if (check_connection() == FAIL)
9594 return;
9595
9596 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
9597 serverStrToWin(serverid), &s);
9598# endif
9599
9600 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
9601 {
9602 char_u *retvar;
9603
9604 v.di_tv.v_type = VAR_STRING;
9605 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009606 retvar = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009607 if (retvar != NULL)
9608 set_var(retvar, &v.di_tv, FALSE);
9609 vim_free(v.di_tv.vval.v_string);
9610 }
9611#else
9612 rettv->vval.v_number = -1;
9613#endif
9614}
9615
9616 static void
9617f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
9618{
9619 char_u *r = NULL;
9620
9621#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009622 char_u *serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009623
9624 if (serverid != NULL && !check_restricted() && !check_secure())
9625 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009626 int timeout = 0;
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009627# ifdef WIN32
9628 /* The server's HWND is encoded in the 'id' parameter */
9629 long_u n = 0;
9630# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009631
9632 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009633 timeout = tv_get_number(&argvars[1]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009634
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009635# ifdef WIN32
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009636 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
9637 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009638 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009639 if (r == NULL)
9640# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009641 if (check_connection() == FAIL
9642 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
9643 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009644# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009645 emsg(_("E277: Unable to read a server reply"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009646 }
9647#endif
9648 rettv->v_type = VAR_STRING;
9649 rettv->vval.v_string = r;
9650}
9651
9652/*
9653 * "remote_send()" function
9654 */
9655 static void
9656f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9657{
9658 rettv->v_type = VAR_STRING;
9659 rettv->vval.v_string = NULL;
9660#ifdef FEAT_CLIENTSERVER
9661 remote_common(argvars, rettv, FALSE);
9662#endif
9663}
9664
9665/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009666 * "remote_startserver()" function
9667 */
9668 static void
9669f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9670{
9671#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009672 char_u *server = tv_get_string_chk(&argvars[0]);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009673
9674 if (server == NULL)
9675 return; /* type error; errmsg already given */
9676 if (serverName != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009677 emsg(_("E941: already started a server"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009678 else
9679 {
9680# ifdef FEAT_X11
9681 if (check_connection() == OK)
9682 serverRegisterName(X_DISPLAY, server);
9683# else
9684 serverSetName(server);
9685# endif
9686 }
9687#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009688 emsg(_("E942: +clientserver feature not available"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009689#endif
9690}
9691
9692/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009693 * "remove()" function
9694 */
9695 static void
9696f_remove(typval_T *argvars, typval_T *rettv)
9697{
9698 list_T *l;
9699 listitem_T *item, *item2;
9700 listitem_T *li;
9701 long idx;
9702 long end;
9703 char_u *key;
9704 dict_T *d;
9705 dictitem_T *di;
9706 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009707 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009708
9709 if (argvars[0].v_type == VAR_DICT)
9710 {
9711 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009712 semsg(_(e_toomanyarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009713 else if ((d = argvars[0].vval.v_dict) != NULL
9714 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
9715 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009716 key = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009717 if (key != NULL)
9718 {
9719 di = dict_find(d, key, -1);
9720 if (di == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009721 semsg(_(e_dictkey), key);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009722 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
9723 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
9724 {
9725 *rettv = di->di_tv;
9726 init_tv(&di->di_tv);
9727 dictitem_remove(d, di);
9728 }
9729 }
9730 }
9731 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009732 else if (argvars[0].v_type == VAR_BLOB)
9733 {
9734 idx = (long)tv_get_number_chk(&argvars[1], &error);
9735 if (!error)
9736 {
9737 blob_T *b = argvars[0].vval.v_blob;
9738 int len = blob_len(b);
9739 char_u *p;
9740
9741 if (idx < 0)
9742 // count from the end
9743 idx = len + idx;
9744 if (idx < 0 || idx >= len)
9745 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009746 semsg(_(e_blobidx), idx);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009747 return;
9748 }
9749 if (argvars[2].v_type == VAR_UNKNOWN)
9750 {
9751 // Remove one item, return its value.
9752 p = (char_u *)b->bv_ga.ga_data;
9753 rettv->vval.v_number = (varnumber_T) *(p + idx);
9754 mch_memmove(p + idx, p + idx + 1, (size_t)len - idx - 1);
9755 --b->bv_ga.ga_len;
9756 }
9757 else
9758 {
9759 blob_T *blob;
9760
9761 // Remove range of items, return list with values.
9762 end = (long)tv_get_number_chk(&argvars[2], &error);
9763 if (error)
9764 return;
9765 if (end < 0)
9766 // count from the end
9767 end = len + end;
9768 if (end >= len || idx > end)
9769 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009770 semsg(_(e_blobidx), end);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009771 return;
9772 }
9773 blob = blob_alloc();
9774 if (blob == NULL)
9775 return;
9776 blob->bv_ga.ga_len = end - idx + 1;
9777 if (ga_grow(&blob->bv_ga, end - idx + 1) == FAIL)
9778 {
9779 vim_free(blob);
9780 return;
9781 }
9782 p = (char_u *)b->bv_ga.ga_data;
9783 mch_memmove((char_u *)blob->bv_ga.ga_data, p + idx,
9784 (size_t)(end - idx + 1));
9785 ++blob->bv_refcount;
9786 rettv->v_type = VAR_BLOB;
9787 rettv->vval.v_blob = blob;
9788
9789 mch_memmove(p + idx, p + end + 1, (size_t)(len - end));
9790 b->bv_ga.ga_len -= end - idx + 1;
9791 }
9792 }
9793 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009794 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009795 semsg(_(e_listdictarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009796 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009797 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009798 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009799 idx = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009800 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009801 ; // type error: do nothing, errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009802 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009803 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009804 else
9805 {
9806 if (argvars[2].v_type == VAR_UNKNOWN)
9807 {
9808 /* Remove one item, return its value. */
9809 vimlist_remove(l, item, item);
9810 *rettv = item->li_tv;
9811 vim_free(item);
9812 }
9813 else
9814 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009815 // Remove range of items, return list with values.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009816 end = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009817 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009818 ; // type error: do nothing
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009819 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009820 semsg(_(e_listidx), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009821 else
9822 {
9823 int cnt = 0;
9824
9825 for (li = item; li != NULL; li = li->li_next)
9826 {
9827 ++cnt;
9828 if (li == item2)
9829 break;
9830 }
9831 if (li == NULL) /* didn't find "item2" after "item" */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009832 emsg(_(e_invrange));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009833 else
9834 {
9835 vimlist_remove(l, item, item2);
9836 if (rettv_list_alloc(rettv) == OK)
9837 {
9838 l = rettv->vval.v_list;
9839 l->lv_first = item;
9840 l->lv_last = item2;
9841 item->li_prev = NULL;
9842 item2->li_next = NULL;
9843 l->lv_len = cnt;
9844 }
9845 }
9846 }
9847 }
9848 }
9849 }
9850}
9851
9852/*
9853 * "rename({from}, {to})" function
9854 */
9855 static void
9856f_rename(typval_T *argvars, typval_T *rettv)
9857{
9858 char_u buf[NUMBUFLEN];
9859
9860 if (check_restricted() || check_secure())
9861 rettv->vval.v_number = -1;
9862 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009863 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
9864 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009865}
9866
9867/*
9868 * "repeat()" function
9869 */
9870 static void
9871f_repeat(typval_T *argvars, typval_T *rettv)
9872{
9873 char_u *p;
9874 int n;
9875 int slen;
9876 int len;
9877 char_u *r;
9878 int i;
9879
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009880 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009881 if (argvars[0].v_type == VAR_LIST)
9882 {
9883 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9884 while (n-- > 0)
9885 if (list_extend(rettv->vval.v_list,
9886 argvars[0].vval.v_list, NULL) == FAIL)
9887 break;
9888 }
9889 else
9890 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009891 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009892 rettv->v_type = VAR_STRING;
9893 rettv->vval.v_string = NULL;
9894
9895 slen = (int)STRLEN(p);
9896 len = slen * n;
9897 if (len <= 0)
9898 return;
9899
9900 r = alloc(len + 1);
9901 if (r != NULL)
9902 {
9903 for (i = 0; i < n; i++)
9904 mch_memmove(r + i * slen, p, (size_t)slen);
9905 r[len] = NUL;
9906 }
9907
9908 rettv->vval.v_string = r;
9909 }
9910}
9911
9912/*
9913 * "resolve()" function
9914 */
9915 static void
9916f_resolve(typval_T *argvars, typval_T *rettv)
9917{
9918 char_u *p;
9919#ifdef HAVE_READLINK
9920 char_u *buf = NULL;
9921#endif
9922
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009923 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009924#ifdef FEAT_SHORTCUT
9925 {
9926 char_u *v = NULL;
9927
9928 v = mch_resolve_shortcut(p);
9929 if (v != NULL)
9930 rettv->vval.v_string = v;
9931 else
9932 rettv->vval.v_string = vim_strsave(p);
9933 }
9934#else
9935# ifdef HAVE_READLINK
9936 {
9937 char_u *cpy;
9938 int len;
9939 char_u *remain = NULL;
9940 char_u *q;
9941 int is_relative_to_current = FALSE;
9942 int has_trailing_pathsep = FALSE;
9943 int limit = 100;
9944
9945 p = vim_strsave(p);
9946
9947 if (p[0] == '.' && (vim_ispathsep(p[1])
9948 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9949 is_relative_to_current = TRUE;
9950
9951 len = STRLEN(p);
9952 if (len > 0 && after_pathsep(p, p + len))
9953 {
9954 has_trailing_pathsep = TRUE;
9955 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9956 }
9957
9958 q = getnextcomp(p);
9959 if (*q != NUL)
9960 {
9961 /* Separate the first path component in "p", and keep the
9962 * remainder (beginning with the path separator). */
9963 remain = vim_strsave(q - 1);
9964 q[-1] = NUL;
9965 }
9966
9967 buf = alloc(MAXPATHL + 1);
9968 if (buf == NULL)
9969 goto fail;
9970
9971 for (;;)
9972 {
9973 for (;;)
9974 {
9975 len = readlink((char *)p, (char *)buf, MAXPATHL);
9976 if (len <= 0)
9977 break;
9978 buf[len] = NUL;
9979
9980 if (limit-- == 0)
9981 {
9982 vim_free(p);
9983 vim_free(remain);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009984 emsg(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009985 rettv->vval.v_string = NULL;
9986 goto fail;
9987 }
9988
9989 /* Ensure that the result will have a trailing path separator
9990 * if the argument has one. */
9991 if (remain == NULL && has_trailing_pathsep)
9992 add_pathsep(buf);
9993
9994 /* Separate the first path component in the link value and
9995 * concatenate the remainders. */
9996 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9997 if (*q != NUL)
9998 {
9999 if (remain == NULL)
10000 remain = vim_strsave(q - 1);
10001 else
10002 {
10003 cpy = concat_str(q - 1, remain);
10004 if (cpy != NULL)
10005 {
10006 vim_free(remain);
10007 remain = cpy;
10008 }
10009 }
10010 q[-1] = NUL;
10011 }
10012
10013 q = gettail(p);
10014 if (q > p && *q == NUL)
10015 {
10016 /* Ignore trailing path separator. */
10017 q[-1] = NUL;
10018 q = gettail(p);
10019 }
10020 if (q > p && !mch_isFullName(buf))
10021 {
10022 /* symlink is relative to directory of argument */
10023 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
10024 if (cpy != NULL)
10025 {
10026 STRCPY(cpy, p);
10027 STRCPY(gettail(cpy), buf);
10028 vim_free(p);
10029 p = cpy;
10030 }
10031 }
10032 else
10033 {
10034 vim_free(p);
10035 p = vim_strsave(buf);
10036 }
10037 }
10038
10039 if (remain == NULL)
10040 break;
10041
10042 /* Append the first path component of "remain" to "p". */
10043 q = getnextcomp(remain + 1);
10044 len = q - remain - (*q != NUL);
10045 cpy = vim_strnsave(p, STRLEN(p) + len);
10046 if (cpy != NULL)
10047 {
10048 STRNCAT(cpy, remain, len);
10049 vim_free(p);
10050 p = cpy;
10051 }
10052 /* Shorten "remain". */
10053 if (*q != NUL)
10054 STRMOVE(remain, q - 1);
10055 else
Bram Moolenaard23a8232018-02-10 18:45:26 +010010056 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010057 }
10058
10059 /* If the result is a relative path name, make it explicitly relative to
10060 * the current directory if and only if the argument had this form. */
10061 if (!vim_ispathsep(*p))
10062 {
10063 if (is_relative_to_current
10064 && *p != NUL
10065 && !(p[0] == '.'
10066 && (p[1] == NUL
10067 || vim_ispathsep(p[1])
10068 || (p[1] == '.'
10069 && (p[2] == NUL
10070 || vim_ispathsep(p[2]))))))
10071 {
10072 /* Prepend "./". */
10073 cpy = concat_str((char_u *)"./", p);
10074 if (cpy != NULL)
10075 {
10076 vim_free(p);
10077 p = cpy;
10078 }
10079 }
10080 else if (!is_relative_to_current)
10081 {
10082 /* Strip leading "./". */
10083 q = p;
10084 while (q[0] == '.' && vim_ispathsep(q[1]))
10085 q += 2;
10086 if (q > p)
10087 STRMOVE(p, p + 2);
10088 }
10089 }
10090
10091 /* Ensure that the result will have no trailing path separator
10092 * if the argument had none. But keep "/" or "//". */
10093 if (!has_trailing_pathsep)
10094 {
10095 q = p + STRLEN(p);
10096 if (after_pathsep(p, q))
10097 *gettail_sep(p) = NUL;
10098 }
10099
10100 rettv->vval.v_string = p;
10101 }
10102# else
10103 rettv->vval.v_string = vim_strsave(p);
10104# endif
10105#endif
10106
10107 simplify_filename(rettv->vval.v_string);
10108
10109#ifdef HAVE_READLINK
10110fail:
10111 vim_free(buf);
10112#endif
10113 rettv->v_type = VAR_STRING;
10114}
10115
10116/*
10117 * "reverse({list})" function
10118 */
10119 static void
10120f_reverse(typval_T *argvars, typval_T *rettv)
10121{
10122 list_T *l;
10123 listitem_T *li, *ni;
10124
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010125 if (argvars[0].v_type == VAR_BLOB)
10126 {
10127 blob_T *b = argvars[0].vval.v_blob;
10128 int i, len = blob_len(b);
10129
10130 for (i = 0; i < len / 2; i++)
10131 {
10132 int tmp = blob_get(b, i);
10133
10134 blob_set(b, i, blob_get(b, len - i - 1));
10135 blob_set(b, len - i - 1, tmp);
10136 }
10137 rettv_blob_set(rettv, b);
10138 return;
10139 }
10140
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010141 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010142 semsg(_(e_listarg), "reverse()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010143 else if ((l = argvars[0].vval.v_list) != NULL
10144 && !tv_check_lock(l->lv_lock,
10145 (char_u *)N_("reverse() argument"), TRUE))
10146 {
10147 li = l->lv_last;
10148 l->lv_first = l->lv_last = NULL;
10149 l->lv_len = 0;
10150 while (li != NULL)
10151 {
10152 ni = li->li_prev;
10153 list_append(l, li);
10154 li = ni;
10155 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020010156 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010157 l->lv_idx = l->lv_len - l->lv_idx - 1;
10158 }
10159}
10160
10161#define SP_NOMOVE 0x01 /* don't move cursor */
10162#define SP_REPEAT 0x02 /* repeat to find outer pair */
10163#define SP_RETCOUNT 0x04 /* return matchcount */
10164#define SP_SETPCMARK 0x08 /* set previous context mark */
10165#define SP_START 0x10 /* accept match at start position */
10166#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
10167#define SP_END 0x40 /* leave cursor at end of match */
10168#define SP_COLUMN 0x80 /* start at cursor column */
10169
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010170/*
10171 * Get flags for a search function.
10172 * Possibly sets "p_ws".
10173 * Returns BACKWARD, FORWARD or zero (for an error).
10174 */
10175 static int
10176get_search_arg(typval_T *varp, int *flagsp)
10177{
10178 int dir = FORWARD;
10179 char_u *flags;
10180 char_u nbuf[NUMBUFLEN];
10181 int mask;
10182
10183 if (varp->v_type != VAR_UNKNOWN)
10184 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010185 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010186 if (flags == NULL)
10187 return 0; /* type error; errmsg already given */
10188 while (*flags != NUL)
10189 {
10190 switch (*flags)
10191 {
10192 case 'b': dir = BACKWARD; break;
10193 case 'w': p_ws = TRUE; break;
10194 case 'W': p_ws = FALSE; break;
10195 default: mask = 0;
10196 if (flagsp != NULL)
10197 switch (*flags)
10198 {
10199 case 'c': mask = SP_START; break;
10200 case 'e': mask = SP_END; break;
10201 case 'm': mask = SP_RETCOUNT; break;
10202 case 'n': mask = SP_NOMOVE; break;
10203 case 'p': mask = SP_SUBPAT; break;
10204 case 'r': mask = SP_REPEAT; break;
10205 case 's': mask = SP_SETPCMARK; break;
10206 case 'z': mask = SP_COLUMN; break;
10207 }
10208 if (mask == 0)
10209 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010210 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010211 dir = 0;
10212 }
10213 else
10214 *flagsp |= mask;
10215 }
10216 if (dir == 0)
10217 break;
10218 ++flags;
10219 }
10220 }
10221 return dir;
10222}
10223
10224/*
10225 * Shared by search() and searchpos() functions.
10226 */
10227 static int
10228search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
10229{
10230 int flags;
10231 char_u *pat;
10232 pos_T pos;
10233 pos_T save_cursor;
10234 int save_p_ws = p_ws;
10235 int dir;
10236 int retval = 0; /* default: FAIL */
10237 long lnum_stop = 0;
10238 proftime_T tm;
10239#ifdef FEAT_RELTIME
10240 long time_limit = 0;
10241#endif
10242 int options = SEARCH_KEEP;
10243 int subpatnum;
10244
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010245 pat = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010246 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
10247 if (dir == 0)
10248 goto theend;
10249 flags = *flagsp;
10250 if (flags & SP_START)
10251 options |= SEARCH_START;
10252 if (flags & SP_END)
10253 options |= SEARCH_END;
10254 if (flags & SP_COLUMN)
10255 options |= SEARCH_COL;
10256
10257 /* Optional arguments: line number to stop searching and timeout. */
10258 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
10259 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010260 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010261 if (lnum_stop < 0)
10262 goto theend;
10263#ifdef FEAT_RELTIME
10264 if (argvars[3].v_type != VAR_UNKNOWN)
10265 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010266 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010267 if (time_limit < 0)
10268 goto theend;
10269 }
10270#endif
10271 }
10272
10273#ifdef FEAT_RELTIME
10274 /* Set the time limit, if there is one. */
10275 profile_setlimit(time_limit, &tm);
10276#endif
10277
10278 /*
10279 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
10280 * Check to make sure only those flags are set.
10281 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
10282 * flags cannot be set. Check for that condition also.
10283 */
10284 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
10285 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10286 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010287 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010288 goto theend;
10289 }
10290
10291 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010292 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010293 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010294 if (subpatnum != FAIL)
10295 {
10296 if (flags & SP_SUBPAT)
10297 retval = subpatnum;
10298 else
10299 retval = pos.lnum;
10300 if (flags & SP_SETPCMARK)
10301 setpcmark();
10302 curwin->w_cursor = pos;
10303 if (match_pos != NULL)
10304 {
10305 /* Store the match cursor position */
10306 match_pos->lnum = pos.lnum;
10307 match_pos->col = pos.col + 1;
10308 }
10309 /* "/$" will put the cursor after the end of the line, may need to
10310 * correct that here */
10311 check_cursor();
10312 }
10313
10314 /* If 'n' flag is used: restore cursor position. */
10315 if (flags & SP_NOMOVE)
10316 curwin->w_cursor = save_cursor;
10317 else
10318 curwin->w_set_curswant = TRUE;
10319theend:
10320 p_ws = save_p_ws;
10321
10322 return retval;
10323}
10324
10325#ifdef FEAT_FLOAT
10326
10327/*
10328 * round() is not in C90, use ceil() or floor() instead.
10329 */
10330 float_T
10331vim_round(float_T f)
10332{
10333 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
10334}
10335
10336/*
10337 * "round({float})" function
10338 */
10339 static void
10340f_round(typval_T *argvars, typval_T *rettv)
10341{
10342 float_T f = 0.0;
10343
10344 rettv->v_type = VAR_FLOAT;
10345 if (get_float_arg(argvars, &f) == OK)
10346 rettv->vval.v_float = vim_round(f);
10347 else
10348 rettv->vval.v_float = 0.0;
10349}
10350#endif
10351
10352/*
10353 * "screenattr()" function
10354 */
10355 static void
10356f_screenattr(typval_T *argvars, typval_T *rettv)
10357{
10358 int row;
10359 int col;
10360 int c;
10361
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010362 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10363 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010364 if (row < 0 || row >= screen_Rows
10365 || col < 0 || col >= screen_Columns)
10366 c = -1;
10367 else
10368 c = ScreenAttrs[LineOffset[row] + col];
10369 rettv->vval.v_number = c;
10370}
10371
10372/*
10373 * "screenchar()" function
10374 */
10375 static void
10376f_screenchar(typval_T *argvars, typval_T *rettv)
10377{
10378 int row;
10379 int col;
10380 int off;
10381 int c;
10382
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010383 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10384 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010385 if (row < 0 || row >= screen_Rows
10386 || col < 0 || col >= screen_Columns)
10387 c = -1;
10388 else
10389 {
10390 off = LineOffset[row] + col;
10391#ifdef FEAT_MBYTE
10392 if (enc_utf8 && ScreenLinesUC[off] != 0)
10393 c = ScreenLinesUC[off];
10394 else
10395#endif
10396 c = ScreenLines[off];
10397 }
10398 rettv->vval.v_number = c;
10399}
10400
10401/*
10402 * "screencol()" function
10403 *
10404 * First column is 1 to be consistent with virtcol().
10405 */
10406 static void
10407f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10408{
10409 rettv->vval.v_number = screen_screencol() + 1;
10410}
10411
10412/*
10413 * "screenrow()" function
10414 */
10415 static void
10416f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10417{
10418 rettv->vval.v_number = screen_screenrow() + 1;
10419}
10420
10421/*
10422 * "search()" function
10423 */
10424 static void
10425f_search(typval_T *argvars, typval_T *rettv)
10426{
10427 int flags = 0;
10428
10429 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10430}
10431
10432/*
10433 * "searchdecl()" function
10434 */
10435 static void
10436f_searchdecl(typval_T *argvars, typval_T *rettv)
10437{
10438 int locally = 1;
10439 int thisblock = 0;
10440 int error = FALSE;
10441 char_u *name;
10442
10443 rettv->vval.v_number = 1; /* default: FAIL */
10444
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010445 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010446 if (argvars[1].v_type != VAR_UNKNOWN)
10447 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010448 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010449 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010450 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010451 }
10452 if (!error && name != NULL)
10453 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10454 locally, thisblock, SEARCH_KEEP) == FAIL;
10455}
10456
10457/*
10458 * Used by searchpair() and searchpairpos()
10459 */
10460 static int
10461searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10462{
10463 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010464 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010465 int save_p_ws = p_ws;
10466 int dir;
10467 int flags = 0;
10468 char_u nbuf1[NUMBUFLEN];
10469 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010470 int retval = 0; /* default: FAIL */
10471 long lnum_stop = 0;
10472 long time_limit = 0;
10473
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010474 /* Get the three pattern arguments: start, middle, end. Will result in an
10475 * error if not a valid argument. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010476 spat = tv_get_string_chk(&argvars[0]);
10477 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
10478 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010479 if (spat == NULL || mpat == NULL || epat == NULL)
10480 goto theend; /* type error */
10481
10482 /* Handle the optional fourth argument: flags */
10483 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
10484 if (dir == 0)
10485 goto theend;
10486
10487 /* Don't accept SP_END or SP_SUBPAT.
10488 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
10489 */
10490 if ((flags & (SP_END | SP_SUBPAT)) != 0
10491 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10492 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010493 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010494 goto theend;
10495 }
10496
10497 /* Using 'r' implies 'W', otherwise it doesn't work. */
10498 if (flags & SP_REPEAT)
10499 p_ws = FALSE;
10500
10501 /* Optional fifth argument: skip expression */
10502 if (argvars[3].v_type == VAR_UNKNOWN
10503 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010010504 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010505 else
10506 {
Bram Moolenaar48570482017-10-30 21:48:41 +010010507 skip = &argvars[4];
10508 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
10509 && skip->v_type != VAR_STRING)
10510 {
10511 /* Type error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010512 semsg(_(e_invarg2), tv_get_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010010513 goto theend;
10514 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010515 if (argvars[5].v_type != VAR_UNKNOWN)
10516 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010517 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010518 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010519 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010520 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010521 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010522 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010523#ifdef FEAT_RELTIME
10524 if (argvars[6].v_type != VAR_UNKNOWN)
10525 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010526 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010527 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010528 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010529 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010530 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010531 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010532 }
10533#endif
10534 }
10535 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010536
10537 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
10538 match_pos, lnum_stop, time_limit);
10539
10540theend:
10541 p_ws = save_p_ws;
10542
10543 return retval;
10544}
10545
10546/*
10547 * "searchpair()" function
10548 */
10549 static void
10550f_searchpair(typval_T *argvars, typval_T *rettv)
10551{
10552 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
10553}
10554
10555/*
10556 * "searchpairpos()" function
10557 */
10558 static void
10559f_searchpairpos(typval_T *argvars, typval_T *rettv)
10560{
10561 pos_T match_pos;
10562 int lnum = 0;
10563 int col = 0;
10564
10565 if (rettv_list_alloc(rettv) == FAIL)
10566 return;
10567
10568 if (searchpair_cmn(argvars, &match_pos) > 0)
10569 {
10570 lnum = match_pos.lnum;
10571 col = match_pos.col;
10572 }
10573
10574 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10575 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10576}
10577
10578/*
10579 * Search for a start/middle/end thing.
10580 * Used by searchpair(), see its documentation for the details.
10581 * Returns 0 or -1 for no match,
10582 */
10583 long
10584do_searchpair(
10585 char_u *spat, /* start pattern */
10586 char_u *mpat, /* middle pattern */
10587 char_u *epat, /* end pattern */
10588 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010010589 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010590 int flags, /* SP_SETPCMARK and other SP_ values */
10591 pos_T *match_pos,
10592 linenr_T lnum_stop, /* stop at this line if not zero */
10593 long time_limit UNUSED) /* stop after this many msec */
10594{
10595 char_u *save_cpo;
10596 char_u *pat, *pat2 = NULL, *pat3 = NULL;
10597 long retval = 0;
10598 pos_T pos;
10599 pos_T firstpos;
10600 pos_T foundpos;
10601 pos_T save_cursor;
10602 pos_T save_pos;
10603 int n;
10604 int r;
10605 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010010606 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010607 int err;
10608 int options = SEARCH_KEEP;
10609 proftime_T tm;
10610
10611 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10612 save_cpo = p_cpo;
10613 p_cpo = empty_option;
10614
10615#ifdef FEAT_RELTIME
10616 /* Set the time limit, if there is one. */
10617 profile_setlimit(time_limit, &tm);
10618#endif
10619
10620 /* Make two search patterns: start/end (pat2, for in nested pairs) and
10621 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010622 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
10623 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010624 if (pat2 == NULL || pat3 == NULL)
10625 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010626 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010627 if (*mpat == NUL)
10628 STRCPY(pat3, pat2);
10629 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010630 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010631 spat, epat, mpat);
10632 if (flags & SP_START)
10633 options |= SEARCH_START;
10634
Bram Moolenaar48570482017-10-30 21:48:41 +010010635 if (skip != NULL)
10636 {
10637 /* Empty string means to not use the skip expression. */
10638 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
10639 use_skip = skip->vval.v_string != NULL
10640 && *skip->vval.v_string != NUL;
10641 }
10642
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010643 save_cursor = curwin->w_cursor;
10644 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010645 CLEAR_POS(&firstpos);
10646 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010647 pat = pat3;
10648 for (;;)
10649 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010650 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010651 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010652 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010653 /* didn't find it or found the first match again: FAIL */
10654 break;
10655
10656 if (firstpos.lnum == 0)
10657 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010658 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010659 {
10660 /* Found the same position again. Can happen with a pattern that
10661 * has "\zs" at the end and searching backwards. Advance one
10662 * character and try again. */
10663 if (dir == BACKWARD)
10664 decl(&pos);
10665 else
10666 incl(&pos);
10667 }
10668 foundpos = pos;
10669
10670 /* clear the start flag to avoid getting stuck here */
10671 options &= ~SEARCH_START;
10672
10673 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010010674 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010675 {
10676 save_pos = curwin->w_cursor;
10677 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010010678 err = FALSE;
10679 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010680 curwin->w_cursor = save_pos;
10681 if (err)
10682 {
10683 /* Evaluating {skip} caused an error, break here. */
10684 curwin->w_cursor = save_cursor;
10685 retval = -1;
10686 break;
10687 }
10688 if (r)
10689 continue;
10690 }
10691
10692 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
10693 {
10694 /* Found end when searching backwards or start when searching
10695 * forward: nested pair. */
10696 ++nest;
10697 pat = pat2; /* nested, don't search for middle */
10698 }
10699 else
10700 {
10701 /* Found end when searching forward or start when searching
10702 * backward: end of (nested) pair; or found middle in outer pair. */
10703 if (--nest == 1)
10704 pat = pat3; /* outer level, search for middle */
10705 }
10706
10707 if (nest == 0)
10708 {
10709 /* Found the match: return matchcount or line number. */
10710 if (flags & SP_RETCOUNT)
10711 ++retval;
10712 else
10713 retval = pos.lnum;
10714 if (flags & SP_SETPCMARK)
10715 setpcmark();
10716 curwin->w_cursor = pos;
10717 if (!(flags & SP_REPEAT))
10718 break;
10719 nest = 1; /* search for next unmatched */
10720 }
10721 }
10722
10723 if (match_pos != NULL)
10724 {
10725 /* Store the match cursor position */
10726 match_pos->lnum = curwin->w_cursor.lnum;
10727 match_pos->col = curwin->w_cursor.col + 1;
10728 }
10729
10730 /* If 'n' flag is used or search failed: restore cursor position. */
10731 if ((flags & SP_NOMOVE) || retval == 0)
10732 curwin->w_cursor = save_cursor;
10733
10734theend:
10735 vim_free(pat2);
10736 vim_free(pat3);
10737 if (p_cpo == empty_option)
10738 p_cpo = save_cpo;
10739 else
10740 /* Darn, evaluating the {skip} expression changed the value. */
10741 free_string_option(save_cpo);
10742
10743 return retval;
10744}
10745
10746/*
10747 * "searchpos()" function
10748 */
10749 static void
10750f_searchpos(typval_T *argvars, typval_T *rettv)
10751{
10752 pos_T match_pos;
10753 int lnum = 0;
10754 int col = 0;
10755 int n;
10756 int flags = 0;
10757
10758 if (rettv_list_alloc(rettv) == FAIL)
10759 return;
10760
10761 n = search_cmn(argvars, &match_pos, &flags);
10762 if (n > 0)
10763 {
10764 lnum = match_pos.lnum;
10765 col = match_pos.col;
10766 }
10767
10768 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10769 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10770 if (flags & SP_SUBPAT)
10771 list_append_number(rettv->vval.v_list, (varnumber_T)n);
10772}
10773
10774 static void
10775f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
10776{
10777#ifdef FEAT_CLIENTSERVER
10778 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010779 char_u *server = tv_get_string_chk(&argvars[0]);
10780 char_u *reply = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010781
10782 rettv->vval.v_number = -1;
10783 if (server == NULL || reply == NULL)
10784 return;
10785 if (check_restricted() || check_secure())
10786 return;
10787# ifdef FEAT_X11
10788 if (check_connection() == FAIL)
10789 return;
10790# endif
10791
10792 if (serverSendReply(server, reply) < 0)
10793 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010794 emsg(_("E258: Unable to send to client"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010795 return;
10796 }
10797 rettv->vval.v_number = 0;
10798#else
10799 rettv->vval.v_number = -1;
10800#endif
10801}
10802
10803 static void
10804f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
10805{
10806 char_u *r = NULL;
10807
10808#ifdef FEAT_CLIENTSERVER
10809# ifdef WIN32
10810 r = serverGetVimNames();
10811# else
10812 make_connection();
10813 if (X_DISPLAY != NULL)
10814 r = serverGetVimNames(X_DISPLAY);
10815# endif
10816#endif
10817 rettv->v_type = VAR_STRING;
10818 rettv->vval.v_string = r;
10819}
10820
10821/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010822 * "setbufline()" function
10823 */
10824 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020010825f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010826{
10827 linenr_T lnum;
10828 buf_T *buf;
10829
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010010830 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010831 if (buf == NULL)
10832 rettv->vval.v_number = 1; /* FAIL */
10833 else
10834 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010835 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020010836 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010837 }
10838}
10839
10840/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010841 * "setbufvar()" function
10842 */
10843 static void
10844f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10845{
10846 buf_T *buf;
10847 char_u *varname, *bufvarname;
10848 typval_T *varp;
10849 char_u nbuf[NUMBUFLEN];
10850
10851 if (check_restricted() || check_secure())
10852 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010853 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
10854 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010010855 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010856 varp = &argvars[2];
10857
10858 if (buf != NULL && varname != NULL && varp != NULL)
10859 {
10860 if (*varname == '&')
10861 {
10862 long numval;
10863 char_u *strval;
10864 int error = FALSE;
10865 aco_save_T aco;
10866
10867 /* set curbuf to be our buf, temporarily */
10868 aucmd_prepbuf(&aco, buf);
10869
10870 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010871 numval = (long)tv_get_number_chk(varp, &error);
10872 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010873 if (!error && strval != NULL)
10874 set_option_value(varname, numval, strval, OPT_LOCAL);
10875
10876 /* reset notion of buffer */
10877 aucmd_restbuf(&aco);
10878 }
10879 else
10880 {
10881 buf_T *save_curbuf = curbuf;
10882
10883 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
10884 if (bufvarname != NULL)
10885 {
10886 curbuf = buf;
10887 STRCPY(bufvarname, "b:");
10888 STRCPY(bufvarname + 2, varname);
10889 set_var(bufvarname, varp, TRUE);
10890 vim_free(bufvarname);
10891 curbuf = save_curbuf;
10892 }
10893 }
10894 }
10895}
10896
10897 static void
10898f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10899{
10900 dict_T *d;
10901 dictitem_T *di;
10902 char_u *csearch;
10903
10904 if (argvars[0].v_type != VAR_DICT)
10905 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010906 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010907 return;
10908 }
10909
10910 if ((d = argvars[0].vval.v_dict) != NULL)
10911 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010010912 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010913 if (csearch != NULL)
10914 {
10915#ifdef FEAT_MBYTE
10916 if (enc_utf8)
10917 {
10918 int pcc[MAX_MCO];
10919 int c = utfc_ptr2char(csearch, pcc);
10920
10921 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
10922 }
10923 else
10924#endif
10925 set_last_csearch(PTR2CHAR(csearch),
10926 csearch, MB_PTR2LEN(csearch));
10927 }
10928
10929 di = dict_find(d, (char_u *)"forward", -1);
10930 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010931 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010932 ? FORWARD : BACKWARD);
10933
10934 di = dict_find(d, (char_u *)"until", -1);
10935 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010936 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010937 }
10938}
10939
10940/*
10941 * "setcmdpos()" function
10942 */
10943 static void
10944f_setcmdpos(typval_T *argvars, typval_T *rettv)
10945{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010946 int pos = (int)tv_get_number(&argvars[0]) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010947
10948 if (pos >= 0)
10949 rettv->vval.v_number = set_cmdline_pos(pos);
10950}
10951
10952/*
10953 * "setfperm({fname}, {mode})" function
10954 */
10955 static void
10956f_setfperm(typval_T *argvars, typval_T *rettv)
10957{
10958 char_u *fname;
10959 char_u modebuf[NUMBUFLEN];
10960 char_u *mode_str;
10961 int i;
10962 int mask;
10963 int mode = 0;
10964
10965 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010966 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010967 if (fname == NULL)
10968 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010969 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010970 if (mode_str == NULL)
10971 return;
10972 if (STRLEN(mode_str) != 9)
10973 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010974 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010975 return;
10976 }
10977
10978 mask = 1;
10979 for (i = 8; i >= 0; --i)
10980 {
10981 if (mode_str[i] != '-')
10982 mode |= mask;
10983 mask = mask << 1;
10984 }
10985 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
10986}
10987
10988/*
10989 * "setline()" function
10990 */
10991 static void
10992f_setline(typval_T *argvars, typval_T *rettv)
10993{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010994 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010995
Bram Moolenaarca851592018-06-06 21:04:07 +020010996 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010997}
10998
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010999/*
11000 * Used by "setqflist()" and "setloclist()" functions
11001 */
11002 static void
11003set_qf_ll_list(
11004 win_T *wp UNUSED,
11005 typval_T *list_arg UNUSED,
11006 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020011007 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011008 typval_T *rettv)
11009{
11010#ifdef FEAT_QUICKFIX
11011 static char *e_invact = N_("E927: Invalid action: '%s'");
11012 char_u *act;
11013 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011014 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011015#endif
11016
11017 rettv->vval.v_number = -1;
11018
11019#ifdef FEAT_QUICKFIX
11020 if (list_arg->v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011021 emsg(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011022 else if (recursive != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011023 emsg(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011024 else
11025 {
11026 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011027 dict_T *d = NULL;
11028 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011029
11030 if (action_arg->v_type == VAR_STRING)
11031 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011032 act = tv_get_string_chk(action_arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011033 if (act == NULL)
11034 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020011035 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
11036 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011037 action = *act;
11038 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011039 semsg(_(e_invact), act);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011040 }
11041 else if (action_arg->v_type == VAR_UNKNOWN)
11042 action = ' ';
11043 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011044 emsg(_(e_stringreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011045
Bram Moolenaard823fa92016-08-12 16:29:27 +020011046 if (action_arg->v_type != VAR_UNKNOWN
11047 && what_arg->v_type != VAR_UNKNOWN)
11048 {
11049 if (what_arg->v_type == VAR_DICT)
11050 d = what_arg->vval.v_dict;
11051 else
11052 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011053 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +020011054 valid_dict = FALSE;
11055 }
11056 }
11057
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011058 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011059 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011060 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
11061 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011062 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011063 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011064 }
11065#endif
11066}
11067
11068/*
11069 * "setloclist()" function
11070 */
11071 static void
11072f_setloclist(typval_T *argvars, typval_T *rettv)
11073{
11074 win_T *win;
11075
11076 rettv->vval.v_number = -1;
11077
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020011078 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011079 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020011080 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011081}
11082
11083/*
11084 * "setmatches()" function
11085 */
11086 static void
11087f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
11088{
11089#ifdef FEAT_SEARCH_EXTRA
11090 list_T *l;
11091 listitem_T *li;
11092 dict_T *d;
11093 list_T *s = NULL;
11094
11095 rettv->vval.v_number = -1;
11096 if (argvars[0].v_type != VAR_LIST)
11097 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011098 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011099 return;
11100 }
11101 if ((l = argvars[0].vval.v_list) != NULL)
11102 {
11103
11104 /* To some extent make sure that we are dealing with a list from
11105 * "getmatches()". */
11106 li = l->lv_first;
11107 while (li != NULL)
11108 {
11109 if (li->li_tv.v_type != VAR_DICT
11110 || (d = li->li_tv.vval.v_dict) == NULL)
11111 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011112 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011113 return;
11114 }
11115 if (!(dict_find(d, (char_u *)"group", -1) != NULL
11116 && (dict_find(d, (char_u *)"pattern", -1) != NULL
11117 || dict_find(d, (char_u *)"pos1", -1) != NULL)
11118 && dict_find(d, (char_u *)"priority", -1) != NULL
11119 && dict_find(d, (char_u *)"id", -1) != NULL))
11120 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011121 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011122 return;
11123 }
11124 li = li->li_next;
11125 }
11126
11127 clear_matches(curwin);
11128 li = l->lv_first;
11129 while (li != NULL)
11130 {
11131 int i = 0;
11132 char_u buf[5];
11133 dictitem_T *di;
11134 char_u *group;
11135 int priority;
11136 int id;
11137 char_u *conceal;
11138
11139 d = li->li_tv.vval.v_dict;
11140 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
11141 {
11142 if (s == NULL)
11143 {
11144 s = list_alloc();
11145 if (s == NULL)
11146 return;
11147 }
11148
11149 /* match from matchaddpos() */
11150 for (i = 1; i < 9; i++)
11151 {
11152 sprintf((char *)buf, (char *)"pos%d", i);
11153 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
11154 {
11155 if (di->di_tv.v_type != VAR_LIST)
11156 return;
11157
11158 list_append_tv(s, &di->di_tv);
11159 s->lv_refcount++;
11160 }
11161 else
11162 break;
11163 }
11164 }
11165
Bram Moolenaar8f667172018-12-14 15:38:31 +010011166 group = dict_get_string(d, (char_u *)"group", TRUE);
11167 priority = (int)dict_get_number(d, (char_u *)"priority");
11168 id = (int)dict_get_number(d, (char_u *)"id");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011169 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar8f667172018-12-14 15:38:31 +010011170 ? dict_get_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011171 : NULL;
11172 if (i == 0)
11173 {
11174 match_add(curwin, group,
Bram Moolenaar8f667172018-12-14 15:38:31 +010011175 dict_get_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011176 priority, id, NULL, conceal);
11177 }
11178 else
11179 {
11180 match_add(curwin, group, NULL, priority, id, s, conceal);
11181 list_unref(s);
11182 s = NULL;
11183 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020011184 vim_free(group);
11185 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011186
11187 li = li->li_next;
11188 }
11189 rettv->vval.v_number = 0;
11190 }
11191#endif
11192}
11193
11194/*
11195 * "setpos()" function
11196 */
11197 static void
11198f_setpos(typval_T *argvars, typval_T *rettv)
11199{
11200 pos_T pos;
11201 int fnum;
11202 char_u *name;
11203 colnr_T curswant = -1;
11204
11205 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011206 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011207 if (name != NULL)
11208 {
11209 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
11210 {
11211 if (--pos.col < 0)
11212 pos.col = 0;
11213 if (name[0] == '.' && name[1] == NUL)
11214 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011215 /* set cursor; "fnum" is ignored */
11216 curwin->w_cursor = pos;
11217 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011218 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011219 curwin->w_curswant = curswant - 1;
11220 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011221 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011222 check_cursor();
11223 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011224 }
11225 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
11226 {
11227 /* set mark */
11228 if (setmark_pos(name[1], &pos, fnum) == OK)
11229 rettv->vval.v_number = 0;
11230 }
11231 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011232 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011233 }
11234 }
11235}
11236
11237/*
11238 * "setqflist()" function
11239 */
11240 static void
11241f_setqflist(typval_T *argvars, typval_T *rettv)
11242{
Bram Moolenaard823fa92016-08-12 16:29:27 +020011243 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011244}
11245
11246/*
11247 * "setreg()" function
11248 */
11249 static void
11250f_setreg(typval_T *argvars, typval_T *rettv)
11251{
11252 int regname;
11253 char_u *strregname;
11254 char_u *stropt;
11255 char_u *strval;
11256 int append;
11257 char_u yank_type;
11258 long block_len;
11259
11260 block_len = -1;
11261 yank_type = MAUTO;
11262 append = FALSE;
11263
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011264 strregname = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011265 rettv->vval.v_number = 1; /* FAIL is default */
11266
11267 if (strregname == NULL)
11268 return; /* type error; errmsg already given */
11269 regname = *strregname;
11270 if (regname == 0 || regname == '@')
11271 regname = '"';
11272
11273 if (argvars[2].v_type != VAR_UNKNOWN)
11274 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011275 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011276 if (stropt == NULL)
11277 return; /* type error */
11278 for (; *stropt != NUL; ++stropt)
11279 switch (*stropt)
11280 {
11281 case 'a': case 'A': /* append */
11282 append = TRUE;
11283 break;
11284 case 'v': case 'c': /* character-wise selection */
11285 yank_type = MCHAR;
11286 break;
11287 case 'V': case 'l': /* line-wise selection */
11288 yank_type = MLINE;
11289 break;
11290 case 'b': case Ctrl_V: /* block-wise selection */
11291 yank_type = MBLOCK;
11292 if (VIM_ISDIGIT(stropt[1]))
11293 {
11294 ++stropt;
11295 block_len = getdigits(&stropt) - 1;
11296 --stropt;
11297 }
11298 break;
11299 }
11300 }
11301
11302 if (argvars[1].v_type == VAR_LIST)
11303 {
11304 char_u **lstval;
11305 char_u **allocval;
11306 char_u buf[NUMBUFLEN];
11307 char_u **curval;
11308 char_u **curallocval;
11309 list_T *ll = argvars[1].vval.v_list;
11310 listitem_T *li;
11311 int len;
11312
11313 /* If the list is NULL handle like an empty list. */
11314 len = ll == NULL ? 0 : ll->lv_len;
11315
11316 /* First half: use for pointers to result lines; second half: use for
11317 * pointers to allocated copies. */
11318 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
11319 if (lstval == NULL)
11320 return;
11321 curval = lstval;
11322 allocval = lstval + len + 2;
11323 curallocval = allocval;
11324
11325 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
11326 li = li->li_next)
11327 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011328 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011329 if (strval == NULL)
11330 goto free_lstval;
11331 if (strval == buf)
11332 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011333 /* Need to make a copy, next tv_get_string_buf_chk() will
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011334 * overwrite the string. */
11335 strval = vim_strsave(buf);
11336 if (strval == NULL)
11337 goto free_lstval;
11338 *curallocval++ = strval;
11339 }
11340 *curval++ = strval;
11341 }
11342 *curval++ = NULL;
11343
11344 write_reg_contents_lst(regname, lstval, -1,
11345 append, yank_type, block_len);
11346free_lstval:
11347 while (curallocval > allocval)
11348 vim_free(*--curallocval);
11349 vim_free(lstval);
11350 }
11351 else
11352 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011353 strval = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011354 if (strval == NULL)
11355 return;
11356 write_reg_contents_ex(regname, strval, -1,
11357 append, yank_type, block_len);
11358 }
11359 rettv->vval.v_number = 0;
11360}
11361
11362/*
11363 * "settabvar()" function
11364 */
11365 static void
11366f_settabvar(typval_T *argvars, typval_T *rettv)
11367{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011368 tabpage_T *save_curtab;
11369 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011370 char_u *varname, *tabvarname;
11371 typval_T *varp;
11372
11373 rettv->vval.v_number = 0;
11374
11375 if (check_restricted() || check_secure())
11376 return;
11377
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011378 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
11379 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011380 varp = &argvars[2];
11381
Bram Moolenaar4033c552017-09-16 20:54:51 +020011382 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011383 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011384 save_curtab = curtab;
11385 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011386
11387 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
11388 if (tabvarname != NULL)
11389 {
11390 STRCPY(tabvarname, "t:");
11391 STRCPY(tabvarname + 2, varname);
11392 set_var(tabvarname, varp, TRUE);
11393 vim_free(tabvarname);
11394 }
11395
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011396 /* Restore current tabpage */
11397 if (valid_tabpage(save_curtab))
11398 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011399 }
11400}
11401
11402/*
11403 * "settabwinvar()" function
11404 */
11405 static void
11406f_settabwinvar(typval_T *argvars, typval_T *rettv)
11407{
11408 setwinvar(argvars, rettv, 1);
11409}
11410
11411/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011412 * "settagstack()" function
11413 */
11414 static void
11415f_settagstack(typval_T *argvars, typval_T *rettv)
11416{
11417 static char *e_invact2 = N_("E962: Invalid action: '%s'");
11418 win_T *wp;
11419 dict_T *d;
11420 int action = 'r';
11421
11422 rettv->vval.v_number = -1;
11423
11424 // first argument: window number or id
11425 wp = find_win_by_nr_or_id(&argvars[0]);
11426 if (wp == NULL)
11427 return;
11428
11429 // second argument: dict with items to set in the tag stack
11430 if (argvars[1].v_type != VAR_DICT)
11431 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011432 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011433 return;
11434 }
11435 d = argvars[1].vval.v_dict;
11436 if (d == NULL)
11437 return;
11438
11439 // third argument: action - 'a' for append and 'r' for replace.
11440 // default is to replace the stack.
11441 if (argvars[2].v_type == VAR_UNKNOWN)
11442 action = 'r';
11443 else if (argvars[2].v_type == VAR_STRING)
11444 {
11445 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011446 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011447 if (actstr == NULL)
11448 return;
11449 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
11450 action = *actstr;
11451 else
11452 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011453 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011454 return;
11455 }
11456 }
11457 else
11458 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011459 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011460 return;
11461 }
11462
11463 if (set_tagstack(wp, d, action) == OK)
11464 rettv->vval.v_number = 0;
11465}
11466
11467/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011468 * "setwinvar()" function
11469 */
11470 static void
11471f_setwinvar(typval_T *argvars, typval_T *rettv)
11472{
11473 setwinvar(argvars, rettv, 0);
11474}
11475
11476#ifdef FEAT_CRYPT
11477/*
11478 * "sha256({string})" function
11479 */
11480 static void
11481f_sha256(typval_T *argvars, typval_T *rettv)
11482{
11483 char_u *p;
11484
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011485 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011486 rettv->vval.v_string = vim_strsave(
11487 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
11488 rettv->v_type = VAR_STRING;
11489}
11490#endif /* FEAT_CRYPT */
11491
11492/*
11493 * "shellescape({string})" function
11494 */
11495 static void
11496f_shellescape(typval_T *argvars, typval_T *rettv)
11497{
Bram Moolenaar20615522017-06-05 18:46:26 +020011498 int do_special = non_zero_arg(&argvars[1]);
11499
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011500 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011501 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011502 rettv->v_type = VAR_STRING;
11503}
11504
11505/*
11506 * shiftwidth() function
11507 */
11508 static void
11509f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
11510{
Bram Moolenaarf9514162018-11-22 03:08:29 +010011511 rettv->vval.v_number = 0;
11512
11513 if (argvars[0].v_type != VAR_UNKNOWN)
11514 {
11515 long col;
11516
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011517 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +010011518 if (col < 0)
11519 return; // type error; errmsg already given
11520#ifdef FEAT_VARTABS
11521 rettv->vval.v_number = get_sw_value_col(curbuf, col);
11522 return;
11523#endif
11524 }
11525
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011526 rettv->vval.v_number = get_sw_value(curbuf);
11527}
11528
Bram Moolenaar162b7142018-12-21 15:17:36 +010011529#ifdef FEAT_SIGNS
11530/*
11531 * "sign_define()" function
11532 */
11533 static void
11534f_sign_define(typval_T *argvars, typval_T *rettv)
11535{
11536 char_u *name;
11537 dict_T *dict;
11538 char_u *icon = NULL;
11539 char_u *linehl = NULL;
11540 char_u *text = NULL;
11541 char_u *texthl = NULL;
11542
11543 rettv->vval.v_number = -1;
11544
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011545 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011546 if (name == NULL)
11547 return;
11548
11549 if (argvars[1].v_type != VAR_UNKNOWN)
11550 {
11551 if (argvars[1].v_type != VAR_DICT)
11552 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011553 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011554 return;
11555 }
11556
11557 // sign attributes
11558 dict = argvars[1].vval.v_dict;
11559 if (dict_find(dict, (char_u *)"icon", -1) != NULL)
11560 icon = dict_get_string(dict, (char_u *)"icon", TRUE);
11561 if (dict_find(dict, (char_u *)"linehl", -1) != NULL)
11562 linehl = dict_get_string(dict, (char_u *)"linehl", TRUE);
11563 if (dict_find(dict, (char_u *)"text", -1) != NULL)
11564 text = dict_get_string(dict, (char_u *)"text", TRUE);
11565 if (dict_find(dict, (char_u *)"texthl", -1) != NULL)
11566 texthl = dict_get_string(dict, (char_u *)"texthl", TRUE);
11567 }
11568
11569 if (sign_define_by_name(name, icon, linehl, text, texthl) == OK)
11570 rettv->vval.v_number = 0;
11571
11572 vim_free(icon);
11573 vim_free(linehl);
11574 vim_free(text);
11575 vim_free(texthl);
11576}
11577
11578/*
11579 * "sign_getdefined()" function
11580 */
11581 static void
11582f_sign_getdefined(typval_T *argvars, typval_T *rettv)
11583{
11584 char_u *name = NULL;
11585
11586 if (rettv_list_alloc_id(rettv, aid_sign_getdefined) != OK)
11587 return;
11588
11589 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011590 name = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011591
11592 sign_getlist(name, rettv->vval.v_list);
11593}
11594
11595/*
11596 * "sign_getplaced()" function
11597 */
11598 static void
11599f_sign_getplaced(typval_T *argvars, typval_T *rettv)
11600{
11601 buf_T *buf = NULL;
11602 dict_T *dict;
11603 dictitem_T *di;
11604 linenr_T lnum = 0;
11605 int sign_id = 0;
11606 char_u *group = NULL;
11607 int notanum = FALSE;
11608
11609 if (rettv_list_alloc_id(rettv, aid_sign_getplaced) != OK)
11610 return;
11611
11612 if (argvars[0].v_type != VAR_UNKNOWN)
11613 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010011614 // get signs placed in the specified buffer
11615 buf = get_buf_arg(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011616 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010011617 return;
Bram Moolenaar162b7142018-12-21 15:17:36 +010011618
11619 if (argvars[1].v_type != VAR_UNKNOWN)
11620 {
11621 if (argvars[1].v_type != VAR_DICT ||
11622 ((dict = argvars[1].vval.v_dict) == NULL))
11623 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011624 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011625 return;
11626 }
11627 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
11628 {
11629 // get signs placed at this line
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011630 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011631 if (notanum)
11632 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011633 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011634 }
11635 if ((di = dict_find(dict, (char_u *)"id", -1)) != NULL)
11636 {
11637 // get sign placed with this identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011638 sign_id = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011639 if (notanum)
11640 return;
11641 }
11642 if ((di = dict_find(dict, (char_u *)"group", -1)) != NULL)
11643 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011644 group = tv_get_string_chk(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011645 if (group == NULL)
11646 return;
Bram Moolenaar6436cd82018-12-27 00:28:33 +010011647 if (*group == '\0') // empty string means global group
11648 group = NULL;
Bram Moolenaar162b7142018-12-21 15:17:36 +010011649 }
11650 }
11651 }
11652
11653 sign_get_placed(buf, lnum, sign_id, group, rettv->vval.v_list);
11654}
11655
11656/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010011657 * "sign_jump()" function
11658 */
11659 static void
11660f_sign_jump(typval_T *argvars, typval_T *rettv)
11661{
11662 int sign_id;
11663 char_u *sign_group = NULL;
11664 buf_T *buf;
11665 int notanum = FALSE;
11666
11667 rettv->vval.v_number = -1;
11668
11669 // Sign identifer
11670 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
11671 if (notanum)
11672 return;
11673 if (sign_id <= 0)
11674 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011675 emsg(_(e_invarg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010011676 return;
11677 }
11678
11679 // Sign group
11680 sign_group = tv_get_string_chk(&argvars[1]);
11681 if (sign_group == NULL)
11682 return;
11683 if (sign_group[0] == '\0')
11684 sign_group = NULL; // global sign group
11685 else
11686 {
11687 sign_group = vim_strsave(sign_group);
11688 if (sign_group == NULL)
11689 return;
11690 }
11691
11692 // Buffer to place the sign
11693 buf = get_buf_arg(&argvars[2]);
11694 if (buf == NULL)
11695 goto cleanup;
11696
11697 rettv->vval.v_number = sign_jump(sign_id, sign_group, buf);
11698
11699cleanup:
11700 vim_free(sign_group);
11701}
11702
11703/*
Bram Moolenaar162b7142018-12-21 15:17:36 +010011704 * "sign_place()" function
11705 */
11706 static void
11707f_sign_place(typval_T *argvars, typval_T *rettv)
11708{
11709 int sign_id;
11710 char_u *group = NULL;
11711 char_u *sign_name;
11712 buf_T *buf;
11713 dict_T *dict;
11714 dictitem_T *di;
11715 linenr_T lnum = 0;
11716 int prio = SIGN_DEF_PRIO;
11717 int notanum = FALSE;
11718
11719 rettv->vval.v_number = -1;
11720
11721 // Sign identifer
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011722 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011723 if (notanum)
11724 return;
11725 if (sign_id < 0)
11726 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011727 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011728 return;
11729 }
11730
11731 // Sign group
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011732 group = tv_get_string_chk(&argvars[1]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011733 if (group == NULL)
11734 return;
11735 if (group[0] == '\0')
11736 group = NULL; // global sign group
11737 else
11738 {
11739 group = vim_strsave(group);
11740 if (group == NULL)
11741 return;
11742 }
11743
11744 // Sign name
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011745 sign_name = tv_get_string_chk(&argvars[2]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011746 if (sign_name == NULL)
11747 goto cleanup;
11748
11749 // Buffer to place the sign
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010011750 buf = get_buf_arg(&argvars[3]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011751 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010011752 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010011753
11754 if (argvars[4].v_type != VAR_UNKNOWN)
11755 {
11756 if (argvars[4].v_type != VAR_DICT ||
11757 ((dict = argvars[4].vval.v_dict) == NULL))
11758 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011759 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011760 goto cleanup;
11761 }
11762
11763 // Line number where the sign is to be placed
11764 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
11765 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011766 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011767 if (notanum)
11768 goto cleanup;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011769 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011770 }
11771 if ((di = dict_find(dict, (char_u *)"priority", -1)) != NULL)
11772 {
11773 // Sign priority
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011774 prio = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011775 if (notanum)
11776 goto cleanup;
11777 }
11778 }
11779
11780 if (sign_place(&sign_id, group, sign_name, buf, lnum, prio) == OK)
11781 rettv->vval.v_number = sign_id;
11782
11783cleanup:
11784 vim_free(group);
11785}
11786
11787/*
11788 * "sign_undefine()" function
11789 */
11790 static void
11791f_sign_undefine(typval_T *argvars, typval_T *rettv)
11792{
11793 char_u *name;
11794
11795 rettv->vval.v_number = -1;
11796
11797 if (argvars[0].v_type == VAR_UNKNOWN)
11798 {
11799 // Free all the signs
11800 free_signs();
11801 rettv->vval.v_number = 0;
11802 }
11803 else
11804 {
11805 // Free only the specified sign
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011806 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011807 if (name == NULL)
11808 return;
11809
11810 if (sign_undefine_by_name(name) == OK)
11811 rettv->vval.v_number = 0;
11812 }
11813}
11814
11815/*
11816 * "sign_unplace()" function
11817 */
11818 static void
11819f_sign_unplace(typval_T *argvars, typval_T *rettv)
11820{
11821 dict_T *dict;
11822 dictitem_T *di;
11823 int sign_id = 0;
11824 buf_T *buf = NULL;
11825 char_u *group = NULL;
11826
11827 rettv->vval.v_number = -1;
11828
11829 if (argvars[0].v_type != VAR_STRING)
11830 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011831 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011832 return;
11833 }
11834
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011835 group = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011836 if (group[0] == '\0')
11837 group = NULL; // global sign group
11838 else
11839 {
11840 group = vim_strsave(group);
11841 if (group == NULL)
11842 return;
11843 }
11844
11845 if (argvars[1].v_type != VAR_UNKNOWN)
11846 {
11847 if (argvars[1].v_type != VAR_DICT)
11848 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011849 emsg(_(e_dictreq));
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010011850 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010011851 }
11852 dict = argvars[1].vval.v_dict;
11853
11854 if ((di = dict_find(dict, (char_u *)"buffer", -1)) != NULL)
11855 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010011856 buf = get_buf_arg(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011857 if (buf == NULL)
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010011858 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010011859 }
11860 if (dict_find(dict, (char_u *)"id", -1) != NULL)
11861 sign_id = dict_get_number(dict, (char_u *)"id");
11862 }
11863
11864 if (buf == NULL)
11865 {
11866 // Delete the sign in all the buffers
11867 FOR_ALL_BUFFERS(buf)
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010011868 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010011869 rettv->vval.v_number = 0;
11870 }
11871 else
11872 {
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010011873 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010011874 rettv->vval.v_number = 0;
11875 }
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010011876
11877cleanup:
Bram Moolenaar162b7142018-12-21 15:17:36 +010011878 vim_free(group);
11879}
11880#endif
11881
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011882/*
11883 * "simplify()" function
11884 */
11885 static void
11886f_simplify(typval_T *argvars, typval_T *rettv)
11887{
11888 char_u *p;
11889
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011890 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011891 rettv->vval.v_string = vim_strsave(p);
11892 simplify_filename(rettv->vval.v_string); /* simplify in place */
11893 rettv->v_type = VAR_STRING;
11894}
11895
11896#ifdef FEAT_FLOAT
11897/*
11898 * "sin()" function
11899 */
11900 static void
11901f_sin(typval_T *argvars, typval_T *rettv)
11902{
11903 float_T f = 0.0;
11904
11905 rettv->v_type = VAR_FLOAT;
11906 if (get_float_arg(argvars, &f) == OK)
11907 rettv->vval.v_float = sin(f);
11908 else
11909 rettv->vval.v_float = 0.0;
11910}
11911
11912/*
11913 * "sinh()" function
11914 */
11915 static void
11916f_sinh(typval_T *argvars, typval_T *rettv)
11917{
11918 float_T f = 0.0;
11919
11920 rettv->v_type = VAR_FLOAT;
11921 if (get_float_arg(argvars, &f) == OK)
11922 rettv->vval.v_float = sinh(f);
11923 else
11924 rettv->vval.v_float = 0.0;
11925}
11926#endif
11927
11928static int
11929#ifdef __BORLANDC__
11930 _RTLENTRYF
11931#endif
11932 item_compare(const void *s1, const void *s2);
11933static int
11934#ifdef __BORLANDC__
11935 _RTLENTRYF
11936#endif
11937 item_compare2(const void *s1, const void *s2);
11938
11939/* struct used in the array that's given to qsort() */
11940typedef struct
11941{
11942 listitem_T *item;
11943 int idx;
11944} sortItem_T;
11945
11946/* struct storing information about current sort */
11947typedef struct
11948{
11949 int item_compare_ic;
11950 int item_compare_numeric;
11951 int item_compare_numbers;
11952#ifdef FEAT_FLOAT
11953 int item_compare_float;
11954#endif
11955 char_u *item_compare_func;
11956 partial_T *item_compare_partial;
11957 dict_T *item_compare_selfdict;
11958 int item_compare_func_err;
11959 int item_compare_keep_zero;
11960} sortinfo_T;
11961static sortinfo_T *sortinfo = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011962#define ITEM_COMPARE_FAIL 999
11963
11964/*
11965 * Compare functions for f_sort() and f_uniq() below.
11966 */
11967 static int
11968#ifdef __BORLANDC__
11969_RTLENTRYF
11970#endif
11971item_compare(const void *s1, const void *s2)
11972{
11973 sortItem_T *si1, *si2;
11974 typval_T *tv1, *tv2;
11975 char_u *p1, *p2;
11976 char_u *tofree1 = NULL, *tofree2 = NULL;
11977 int res;
11978 char_u numbuf1[NUMBUFLEN];
11979 char_u numbuf2[NUMBUFLEN];
11980
11981 si1 = (sortItem_T *)s1;
11982 si2 = (sortItem_T *)s2;
11983 tv1 = &si1->item->li_tv;
11984 tv2 = &si2->item->li_tv;
11985
11986 if (sortinfo->item_compare_numbers)
11987 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011988 varnumber_T v1 = tv_get_number(tv1);
11989 varnumber_T v2 = tv_get_number(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011990
11991 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11992 }
11993
11994#ifdef FEAT_FLOAT
11995 if (sortinfo->item_compare_float)
11996 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011997 float_T v1 = tv_get_float(tv1);
11998 float_T v2 = tv_get_float(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011999
12000 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
12001 }
12002#endif
12003
12004 /* tv2string() puts quotes around a string and allocates memory. Don't do
12005 * that for string variables. Use a single quote when comparing with a
12006 * non-string to do what the docs promise. */
12007 if (tv1->v_type == VAR_STRING)
12008 {
12009 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12010 p1 = (char_u *)"'";
12011 else
12012 p1 = tv1->vval.v_string;
12013 }
12014 else
12015 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
12016 if (tv2->v_type == VAR_STRING)
12017 {
12018 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12019 p2 = (char_u *)"'";
12020 else
12021 p2 = tv2->vval.v_string;
12022 }
12023 else
12024 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
12025 if (p1 == NULL)
12026 p1 = (char_u *)"";
12027 if (p2 == NULL)
12028 p2 = (char_u *)"";
12029 if (!sortinfo->item_compare_numeric)
12030 {
12031 if (sortinfo->item_compare_ic)
12032 res = STRICMP(p1, p2);
12033 else
12034 res = STRCMP(p1, p2);
12035 }
12036 else
12037 {
12038 double n1, n2;
12039 n1 = strtod((char *)p1, (char **)&p1);
12040 n2 = strtod((char *)p2, (char **)&p2);
12041 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
12042 }
12043
12044 /* When the result would be zero, compare the item indexes. Makes the
12045 * sort stable. */
12046 if (res == 0 && !sortinfo->item_compare_keep_zero)
12047 res = si1->idx > si2->idx ? 1 : -1;
12048
12049 vim_free(tofree1);
12050 vim_free(tofree2);
12051 return res;
12052}
12053
12054 static int
12055#ifdef __BORLANDC__
12056_RTLENTRYF
12057#endif
12058item_compare2(const void *s1, const void *s2)
12059{
12060 sortItem_T *si1, *si2;
12061 int res;
12062 typval_T rettv;
12063 typval_T argv[3];
12064 int dummy;
12065 char_u *func_name;
12066 partial_T *partial = sortinfo->item_compare_partial;
12067
12068 /* shortcut after failure in previous call; compare all items equal */
12069 if (sortinfo->item_compare_func_err)
12070 return 0;
12071
12072 si1 = (sortItem_T *)s1;
12073 si2 = (sortItem_T *)s2;
12074
12075 if (partial == NULL)
12076 func_name = sortinfo->item_compare_func;
12077 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012078 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012079
12080 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
12081 * in the copy without changing the original list items. */
12082 copy_tv(&si1->item->li_tv, &argv[0]);
12083 copy_tv(&si2->item->li_tv, &argv[1]);
12084
12085 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
12086 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020012087 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012088 partial, sortinfo->item_compare_selfdict);
12089 clear_tv(&argv[0]);
12090 clear_tv(&argv[1]);
12091
12092 if (res == FAIL)
12093 res = ITEM_COMPARE_FAIL;
12094 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012095 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012096 if (sortinfo->item_compare_func_err)
12097 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
12098 clear_tv(&rettv);
12099
12100 /* When the result would be zero, compare the pointers themselves. Makes
12101 * the sort stable. */
12102 if (res == 0 && !sortinfo->item_compare_keep_zero)
12103 res = si1->idx > si2->idx ? 1 : -1;
12104
12105 return res;
12106}
12107
12108/*
12109 * "sort({list})" function
12110 */
12111 static void
12112do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
12113{
12114 list_T *l;
12115 listitem_T *li;
12116 sortItem_T *ptrs;
12117 sortinfo_T *old_sortinfo;
12118 sortinfo_T info;
12119 long len;
12120 long i;
12121
12122 /* Pointer to current info struct used in compare function. Save and
12123 * restore the current one for nested calls. */
12124 old_sortinfo = sortinfo;
12125 sortinfo = &info;
12126
12127 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012128 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012129 else
12130 {
12131 l = argvars[0].vval.v_list;
12132 if (l == NULL || tv_check_lock(l->lv_lock,
12133 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
12134 TRUE))
12135 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012136 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012137
12138 len = list_len(l);
12139 if (len <= 1)
12140 goto theend; /* short list sorts pretty quickly */
12141
12142 info.item_compare_ic = FALSE;
12143 info.item_compare_numeric = FALSE;
12144 info.item_compare_numbers = FALSE;
12145#ifdef FEAT_FLOAT
12146 info.item_compare_float = FALSE;
12147#endif
12148 info.item_compare_func = NULL;
12149 info.item_compare_partial = NULL;
12150 info.item_compare_selfdict = NULL;
12151 if (argvars[1].v_type != VAR_UNKNOWN)
12152 {
12153 /* optional second argument: {func} */
12154 if (argvars[1].v_type == VAR_FUNC)
12155 info.item_compare_func = argvars[1].vval.v_string;
12156 else if (argvars[1].v_type == VAR_PARTIAL)
12157 info.item_compare_partial = argvars[1].vval.v_partial;
12158 else
12159 {
12160 int error = FALSE;
12161
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012162 i = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012163 if (error)
12164 goto theend; /* type error; errmsg already given */
12165 if (i == 1)
12166 info.item_compare_ic = TRUE;
12167 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012168 info.item_compare_func = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012169 else if (i != 0)
12170 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012171 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012172 goto theend;
12173 }
12174 if (info.item_compare_func != NULL)
12175 {
12176 if (*info.item_compare_func == NUL)
12177 {
12178 /* empty string means default sort */
12179 info.item_compare_func = NULL;
12180 }
12181 else if (STRCMP(info.item_compare_func, "n") == 0)
12182 {
12183 info.item_compare_func = NULL;
12184 info.item_compare_numeric = TRUE;
12185 }
12186 else if (STRCMP(info.item_compare_func, "N") == 0)
12187 {
12188 info.item_compare_func = NULL;
12189 info.item_compare_numbers = TRUE;
12190 }
12191#ifdef FEAT_FLOAT
12192 else if (STRCMP(info.item_compare_func, "f") == 0)
12193 {
12194 info.item_compare_func = NULL;
12195 info.item_compare_float = TRUE;
12196 }
12197#endif
12198 else if (STRCMP(info.item_compare_func, "i") == 0)
12199 {
12200 info.item_compare_func = NULL;
12201 info.item_compare_ic = TRUE;
12202 }
12203 }
12204 }
12205
12206 if (argvars[2].v_type != VAR_UNKNOWN)
12207 {
12208 /* optional third argument: {dict} */
12209 if (argvars[2].v_type != VAR_DICT)
12210 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012211 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012212 goto theend;
12213 }
12214 info.item_compare_selfdict = argvars[2].vval.v_dict;
12215 }
12216 }
12217
12218 /* Make an array with each entry pointing to an item in the List. */
12219 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
12220 if (ptrs == NULL)
12221 goto theend;
12222
12223 i = 0;
12224 if (sort)
12225 {
12226 /* sort(): ptrs will be the list to sort */
12227 for (li = l->lv_first; li != NULL; li = li->li_next)
12228 {
12229 ptrs[i].item = li;
12230 ptrs[i].idx = i;
12231 ++i;
12232 }
12233
12234 info.item_compare_func_err = FALSE;
12235 info.item_compare_keep_zero = FALSE;
12236 /* test the compare function */
12237 if ((info.item_compare_func != NULL
12238 || info.item_compare_partial != NULL)
12239 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
12240 == ITEM_COMPARE_FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012241 emsg(_("E702: Sort compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012242 else
12243 {
12244 /* Sort the array with item pointers. */
12245 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
12246 info.item_compare_func == NULL
12247 && info.item_compare_partial == NULL
12248 ? item_compare : item_compare2);
12249
12250 if (!info.item_compare_func_err)
12251 {
12252 /* Clear the List and append the items in sorted order. */
12253 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
12254 l->lv_len = 0;
12255 for (i = 0; i < len; ++i)
12256 list_append(l, ptrs[i].item);
12257 }
12258 }
12259 }
12260 else
12261 {
12262 int (*item_compare_func_ptr)(const void *, const void *);
12263
12264 /* f_uniq(): ptrs will be a stack of items to remove */
12265 info.item_compare_func_err = FALSE;
12266 info.item_compare_keep_zero = TRUE;
12267 item_compare_func_ptr = info.item_compare_func != NULL
12268 || info.item_compare_partial != NULL
12269 ? item_compare2 : item_compare;
12270
12271 for (li = l->lv_first; li != NULL && li->li_next != NULL;
12272 li = li->li_next)
12273 {
12274 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
12275 == 0)
12276 ptrs[i++].item = li;
12277 if (info.item_compare_func_err)
12278 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012279 emsg(_("E882: Uniq compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012280 break;
12281 }
12282 }
12283
12284 if (!info.item_compare_func_err)
12285 {
12286 while (--i >= 0)
12287 {
12288 li = ptrs[i].item->li_next;
12289 ptrs[i].item->li_next = li->li_next;
12290 if (li->li_next != NULL)
12291 li->li_next->li_prev = ptrs[i].item;
12292 else
12293 l->lv_last = ptrs[i].item;
12294 list_fix_watch(l, li);
12295 listitem_free(li);
12296 l->lv_len--;
12297 }
12298 }
12299 }
12300
12301 vim_free(ptrs);
12302 }
12303theend:
12304 sortinfo = old_sortinfo;
12305}
12306
12307/*
12308 * "sort({list})" function
12309 */
12310 static void
12311f_sort(typval_T *argvars, typval_T *rettv)
12312{
12313 do_sort_uniq(argvars, rettv, TRUE);
12314}
12315
12316/*
12317 * "uniq({list})" function
12318 */
12319 static void
12320f_uniq(typval_T *argvars, typval_T *rettv)
12321{
12322 do_sort_uniq(argvars, rettv, FALSE);
12323}
12324
12325/*
12326 * "soundfold({word})" function
12327 */
12328 static void
12329f_soundfold(typval_T *argvars, typval_T *rettv)
12330{
12331 char_u *s;
12332
12333 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012334 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012335#ifdef FEAT_SPELL
12336 rettv->vval.v_string = eval_soundfold(s);
12337#else
12338 rettv->vval.v_string = vim_strsave(s);
12339#endif
12340}
12341
12342/*
12343 * "spellbadword()" function
12344 */
12345 static void
12346f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
12347{
12348 char_u *word = (char_u *)"";
12349 hlf_T attr = HLF_COUNT;
12350 int len = 0;
12351
12352 if (rettv_list_alloc(rettv) == FAIL)
12353 return;
12354
12355#ifdef FEAT_SPELL
12356 if (argvars[0].v_type == VAR_UNKNOWN)
12357 {
12358 /* Find the start and length of the badly spelled word. */
12359 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
12360 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012361 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012362 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012363 curwin->w_set_curswant = TRUE;
12364 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012365 }
12366 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
12367 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012368 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012369 int capcol = -1;
12370
12371 if (str != NULL)
12372 {
12373 /* Check the argument for spelling. */
12374 while (*str != NUL)
12375 {
12376 len = spell_check(curwin, str, &attr, &capcol, FALSE);
12377 if (attr != HLF_COUNT)
12378 {
12379 word = str;
12380 break;
12381 }
12382 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020012383 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012384 }
12385 }
12386 }
12387#endif
12388
12389 list_append_string(rettv->vval.v_list, word, len);
12390 list_append_string(rettv->vval.v_list, (char_u *)(
12391 attr == HLF_SPB ? "bad" :
12392 attr == HLF_SPR ? "rare" :
12393 attr == HLF_SPL ? "local" :
12394 attr == HLF_SPC ? "caps" :
12395 ""), -1);
12396}
12397
12398/*
12399 * "spellsuggest()" function
12400 */
12401 static void
12402f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
12403{
12404#ifdef FEAT_SPELL
12405 char_u *str;
12406 int typeerr = FALSE;
12407 int maxcount;
12408 garray_T ga;
12409 int i;
12410 listitem_T *li;
12411 int need_capital = FALSE;
12412#endif
12413
12414 if (rettv_list_alloc(rettv) == FAIL)
12415 return;
12416
12417#ifdef FEAT_SPELL
12418 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
12419 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012420 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012421 if (argvars[1].v_type != VAR_UNKNOWN)
12422 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012423 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012424 if (maxcount <= 0)
12425 return;
12426 if (argvars[2].v_type != VAR_UNKNOWN)
12427 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012428 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012429 if (typeerr)
12430 return;
12431 }
12432 }
12433 else
12434 maxcount = 25;
12435
12436 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
12437
12438 for (i = 0; i < ga.ga_len; ++i)
12439 {
12440 str = ((char_u **)ga.ga_data)[i];
12441
12442 li = listitem_alloc();
12443 if (li == NULL)
12444 vim_free(str);
12445 else
12446 {
12447 li->li_tv.v_type = VAR_STRING;
12448 li->li_tv.v_lock = 0;
12449 li->li_tv.vval.v_string = str;
12450 list_append(rettv->vval.v_list, li);
12451 }
12452 }
12453 ga_clear(&ga);
12454 }
12455#endif
12456}
12457
12458 static void
12459f_split(typval_T *argvars, typval_T *rettv)
12460{
12461 char_u *str;
12462 char_u *end;
12463 char_u *pat = NULL;
12464 regmatch_T regmatch;
12465 char_u patbuf[NUMBUFLEN];
12466 char_u *save_cpo;
12467 int match;
12468 colnr_T col = 0;
12469 int keepempty = FALSE;
12470 int typeerr = FALSE;
12471
12472 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12473 save_cpo = p_cpo;
12474 p_cpo = (char_u *)"";
12475
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012476 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012477 if (argvars[1].v_type != VAR_UNKNOWN)
12478 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012479 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012480 if (pat == NULL)
12481 typeerr = TRUE;
12482 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012483 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012484 }
12485 if (pat == NULL || *pat == NUL)
12486 pat = (char_u *)"[\\x01- ]\\+";
12487
12488 if (rettv_list_alloc(rettv) == FAIL)
12489 return;
12490 if (typeerr)
12491 return;
12492
12493 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12494 if (regmatch.regprog != NULL)
12495 {
12496 regmatch.rm_ic = FALSE;
12497 while (*str != NUL || keepempty)
12498 {
12499 if (*str == NUL)
12500 match = FALSE; /* empty item at the end */
12501 else
12502 match = vim_regexec_nl(&regmatch, str, col);
12503 if (match)
12504 end = regmatch.startp[0];
12505 else
12506 end = str + STRLEN(str);
12507 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
12508 && *str != NUL && match && end < regmatch.endp[0]))
12509 {
12510 if (list_append_string(rettv->vval.v_list, str,
12511 (int)(end - str)) == FAIL)
12512 break;
12513 }
12514 if (!match)
12515 break;
12516 /* Advance to just after the match. */
12517 if (regmatch.endp[0] > str)
12518 col = 0;
12519 else
12520 {
12521 /* Don't get stuck at the same match. */
12522#ifdef FEAT_MBYTE
12523 col = (*mb_ptr2len)(regmatch.endp[0]);
12524#else
12525 col = 1;
12526#endif
12527 }
12528 str = regmatch.endp[0];
12529 }
12530
12531 vim_regfree(regmatch.regprog);
12532 }
12533
12534 p_cpo = save_cpo;
12535}
12536
12537#ifdef FEAT_FLOAT
12538/*
12539 * "sqrt()" function
12540 */
12541 static void
12542f_sqrt(typval_T *argvars, typval_T *rettv)
12543{
12544 float_T f = 0.0;
12545
12546 rettv->v_type = VAR_FLOAT;
12547 if (get_float_arg(argvars, &f) == OK)
12548 rettv->vval.v_float = sqrt(f);
12549 else
12550 rettv->vval.v_float = 0.0;
12551}
12552
12553/*
12554 * "str2float()" function
12555 */
12556 static void
12557f_str2float(typval_T *argvars, typval_T *rettv)
12558{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012559 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010012560 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012561
Bram Moolenaar08243d22017-01-10 16:12:29 +010012562 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012563 p = skipwhite(p + 1);
12564 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010012565 if (isneg)
12566 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012567 rettv->v_type = VAR_FLOAT;
12568}
12569#endif
12570
12571/*
12572 * "str2nr()" function
12573 */
12574 static void
12575f_str2nr(typval_T *argvars, typval_T *rettv)
12576{
12577 int base = 10;
12578 char_u *p;
12579 varnumber_T n;
12580 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010012581 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012582
12583 if (argvars[1].v_type != VAR_UNKNOWN)
12584 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012585 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012586 if (base != 2 && base != 8 && base != 10 && base != 16)
12587 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012588 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012589 return;
12590 }
12591 }
12592
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012593 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010012594 isneg = (*p == '-');
12595 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012596 p = skipwhite(p + 1);
12597 switch (base)
12598 {
12599 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
12600 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
12601 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
12602 default: what = 0;
12603 }
12604 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010012605 if (isneg)
12606 rettv->vval.v_number = -n;
12607 else
12608 rettv->vval.v_number = n;
12609
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012610}
12611
12612#ifdef HAVE_STRFTIME
12613/*
12614 * "strftime({format}[, {time}])" function
12615 */
12616 static void
12617f_strftime(typval_T *argvars, typval_T *rettv)
12618{
12619 char_u result_buf[256];
12620 struct tm *curtime;
12621 time_t seconds;
12622 char_u *p;
12623
12624 rettv->v_type = VAR_STRING;
12625
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012626 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012627 if (argvars[1].v_type == VAR_UNKNOWN)
12628 seconds = time(NULL);
12629 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012630 seconds = (time_t)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012631 curtime = localtime(&seconds);
12632 /* MSVC returns NULL for an invalid value of seconds. */
12633 if (curtime == NULL)
12634 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
12635 else
12636 {
12637# ifdef FEAT_MBYTE
12638 vimconv_T conv;
12639 char_u *enc;
12640
12641 conv.vc_type = CONV_NONE;
12642 enc = enc_locale();
12643 convert_setup(&conv, p_enc, enc);
12644 if (conv.vc_type != CONV_NONE)
12645 p = string_convert(&conv, p, NULL);
12646# endif
12647 if (p != NULL)
12648 (void)strftime((char *)result_buf, sizeof(result_buf),
12649 (char *)p, curtime);
12650 else
12651 result_buf[0] = NUL;
12652
12653# ifdef FEAT_MBYTE
12654 if (conv.vc_type != CONV_NONE)
12655 vim_free(p);
12656 convert_setup(&conv, enc, p_enc);
12657 if (conv.vc_type != CONV_NONE)
12658 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
12659 else
12660# endif
12661 rettv->vval.v_string = vim_strsave(result_buf);
12662
12663# ifdef FEAT_MBYTE
12664 /* Release conversion descriptors */
12665 convert_setup(&conv, NULL, NULL);
12666 vim_free(enc);
12667# endif
12668 }
12669}
12670#endif
12671
12672/*
12673 * "strgetchar()" function
12674 */
12675 static void
12676f_strgetchar(typval_T *argvars, typval_T *rettv)
12677{
12678 char_u *str;
12679 int len;
12680 int error = FALSE;
12681 int charidx;
12682
12683 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012684 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012685 if (str == NULL)
12686 return;
12687 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012688 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012689 if (error)
12690 return;
12691#ifdef FEAT_MBYTE
12692 {
12693 int byteidx = 0;
12694
12695 while (charidx >= 0 && byteidx < len)
12696 {
12697 if (charidx == 0)
12698 {
12699 rettv->vval.v_number = mb_ptr2char(str + byteidx);
12700 break;
12701 }
12702 --charidx;
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012703 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012704 }
12705 }
12706#else
12707 if (charidx < len)
12708 rettv->vval.v_number = str[charidx];
12709#endif
12710}
12711
12712/*
12713 * "stridx()" function
12714 */
12715 static void
12716f_stridx(typval_T *argvars, typval_T *rettv)
12717{
12718 char_u buf[NUMBUFLEN];
12719 char_u *needle;
12720 char_u *haystack;
12721 char_u *save_haystack;
12722 char_u *pos;
12723 int start_idx;
12724
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012725 needle = tv_get_string_chk(&argvars[1]);
12726 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012727 rettv->vval.v_number = -1;
12728 if (needle == NULL || haystack == NULL)
12729 return; /* type error; errmsg already given */
12730
12731 if (argvars[2].v_type != VAR_UNKNOWN)
12732 {
12733 int error = FALSE;
12734
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012735 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012736 if (error || start_idx >= (int)STRLEN(haystack))
12737 return;
12738 if (start_idx >= 0)
12739 haystack += start_idx;
12740 }
12741
12742 pos = (char_u *)strstr((char *)haystack, (char *)needle);
12743 if (pos != NULL)
12744 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
12745}
12746
12747/*
12748 * "string()" function
12749 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010012750 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012751f_string(typval_T *argvars, typval_T *rettv)
12752{
12753 char_u *tofree;
12754 char_u numbuf[NUMBUFLEN];
12755
12756 rettv->v_type = VAR_STRING;
12757 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
12758 get_copyID());
12759 /* Make a copy if we have a value but it's not in allocated memory. */
12760 if (rettv->vval.v_string != NULL && tofree == NULL)
12761 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
12762}
12763
12764/*
12765 * "strlen()" function
12766 */
12767 static void
12768f_strlen(typval_T *argvars, typval_T *rettv)
12769{
12770 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012771 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012772}
12773
12774/*
12775 * "strchars()" function
12776 */
12777 static void
12778f_strchars(typval_T *argvars, typval_T *rettv)
12779{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012780 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012781 int skipcc = 0;
12782#ifdef FEAT_MBYTE
12783 varnumber_T len = 0;
12784 int (*func_mb_ptr2char_adv)(char_u **pp);
12785#endif
12786
12787 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012788 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012789 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012790 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012791 else
12792 {
12793#ifdef FEAT_MBYTE
12794 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
12795 while (*s != NUL)
12796 {
12797 func_mb_ptr2char_adv(&s);
12798 ++len;
12799 }
12800 rettv->vval.v_number = len;
12801#else
12802 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
12803#endif
12804 }
12805}
12806
12807/*
12808 * "strdisplaywidth()" function
12809 */
12810 static void
12811f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
12812{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012813 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012814 int col = 0;
12815
12816 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012817 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012818
12819 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
12820}
12821
12822/*
12823 * "strwidth()" function
12824 */
12825 static void
12826f_strwidth(typval_T *argvars, typval_T *rettv)
12827{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012828 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012829
12830 rettv->vval.v_number = (varnumber_T)(
12831#ifdef FEAT_MBYTE
12832 mb_string2cells(s, -1)
12833#else
12834 STRLEN(s)
12835#endif
12836 );
12837}
12838
12839/*
12840 * "strcharpart()" function
12841 */
12842 static void
12843f_strcharpart(typval_T *argvars, typval_T *rettv)
12844{
12845#ifdef FEAT_MBYTE
12846 char_u *p;
12847 int nchar;
12848 int nbyte = 0;
12849 int charlen;
12850 int len = 0;
12851 int slen;
12852 int error = FALSE;
12853
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012854 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012855 slen = (int)STRLEN(p);
12856
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012857 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012858 if (!error)
12859 {
12860 if (nchar > 0)
12861 while (nchar > 0 && nbyte < slen)
12862 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012863 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012864 --nchar;
12865 }
12866 else
12867 nbyte = nchar;
12868 if (argvars[2].v_type != VAR_UNKNOWN)
12869 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012870 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012871 while (charlen > 0 && nbyte + len < slen)
12872 {
12873 int off = nbyte + len;
12874
12875 if (off < 0)
12876 len += 1;
12877 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012878 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012879 --charlen;
12880 }
12881 }
12882 else
12883 len = slen - nbyte; /* default: all bytes that are available. */
12884 }
12885
12886 /*
12887 * Only return the overlap between the specified part and the actual
12888 * string.
12889 */
12890 if (nbyte < 0)
12891 {
12892 len += nbyte;
12893 nbyte = 0;
12894 }
12895 else if (nbyte > slen)
12896 nbyte = slen;
12897 if (len < 0)
12898 len = 0;
12899 else if (nbyte + len > slen)
12900 len = slen - nbyte;
12901
12902 rettv->v_type = VAR_STRING;
12903 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
12904#else
12905 f_strpart(argvars, rettv);
12906#endif
12907}
12908
12909/*
12910 * "strpart()" function
12911 */
12912 static void
12913f_strpart(typval_T *argvars, typval_T *rettv)
12914{
12915 char_u *p;
12916 int n;
12917 int len;
12918 int slen;
12919 int error = FALSE;
12920
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012921 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012922 slen = (int)STRLEN(p);
12923
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012924 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012925 if (error)
12926 len = 0;
12927 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012928 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012929 else
12930 len = slen - n; /* default len: all bytes that are available. */
12931
12932 /*
12933 * Only return the overlap between the specified part and the actual
12934 * string.
12935 */
12936 if (n < 0)
12937 {
12938 len += n;
12939 n = 0;
12940 }
12941 else if (n > slen)
12942 n = slen;
12943 if (len < 0)
12944 len = 0;
12945 else if (n + len > slen)
12946 len = slen - n;
12947
12948 rettv->v_type = VAR_STRING;
12949 rettv->vval.v_string = vim_strnsave(p + n, len);
12950}
12951
12952/*
12953 * "strridx()" function
12954 */
12955 static void
12956f_strridx(typval_T *argvars, typval_T *rettv)
12957{
12958 char_u buf[NUMBUFLEN];
12959 char_u *needle;
12960 char_u *haystack;
12961 char_u *rest;
12962 char_u *lastmatch = NULL;
12963 int haystack_len, end_idx;
12964
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012965 needle = tv_get_string_chk(&argvars[1]);
12966 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012967
12968 rettv->vval.v_number = -1;
12969 if (needle == NULL || haystack == NULL)
12970 return; /* type error; errmsg already given */
12971
12972 haystack_len = (int)STRLEN(haystack);
12973 if (argvars[2].v_type != VAR_UNKNOWN)
12974 {
12975 /* Third argument: upper limit for index */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012976 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012977 if (end_idx < 0)
12978 return; /* can never find a match */
12979 }
12980 else
12981 end_idx = haystack_len;
12982
12983 if (*needle == NUL)
12984 {
12985 /* Empty string matches past the end. */
12986 lastmatch = haystack + end_idx;
12987 }
12988 else
12989 {
12990 for (rest = haystack; *rest != '\0'; ++rest)
12991 {
12992 rest = (char_u *)strstr((char *)rest, (char *)needle);
12993 if (rest == NULL || rest > haystack + end_idx)
12994 break;
12995 lastmatch = rest;
12996 }
12997 }
12998
12999 if (lastmatch == NULL)
13000 rettv->vval.v_number = -1;
13001 else
13002 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
13003}
13004
13005/*
13006 * "strtrans()" function
13007 */
13008 static void
13009f_strtrans(typval_T *argvars, typval_T *rettv)
13010{
13011 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013012 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013013}
13014
13015/*
13016 * "submatch()" function
13017 */
13018 static void
13019f_submatch(typval_T *argvars, typval_T *rettv)
13020{
13021 int error = FALSE;
13022 int no;
13023 int retList = 0;
13024
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013025 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013026 if (error)
13027 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013028 if (no < 0 || no >= NSUBEXP)
13029 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013030 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +010013031 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013032 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013033 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013034 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013035 if (error)
13036 return;
13037
13038 if (retList == 0)
13039 {
13040 rettv->v_type = VAR_STRING;
13041 rettv->vval.v_string = reg_submatch(no);
13042 }
13043 else
13044 {
13045 rettv->v_type = VAR_LIST;
13046 rettv->vval.v_list = reg_submatch_list(no);
13047 }
13048}
13049
13050/*
13051 * "substitute()" function
13052 */
13053 static void
13054f_substitute(typval_T *argvars, typval_T *rettv)
13055{
13056 char_u patbuf[NUMBUFLEN];
13057 char_u subbuf[NUMBUFLEN];
13058 char_u flagsbuf[NUMBUFLEN];
13059
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013060 char_u *str = tv_get_string_chk(&argvars[0]);
13061 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013062 char_u *sub = NULL;
13063 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013064 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013065
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013066 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
13067 expr = &argvars[2];
13068 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013069 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013070
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013071 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013072 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
13073 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013074 rettv->vval.v_string = NULL;
13075 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013076 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013077}
13078
13079/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013080 * "swapinfo(swap_filename)" function
13081 */
13082 static void
13083f_swapinfo(typval_T *argvars, typval_T *rettv)
13084{
13085 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013086 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013087}
13088
13089/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020013090 * "swapname(expr)" function
13091 */
13092 static void
13093f_swapname(typval_T *argvars, typval_T *rettv)
13094{
13095 buf_T *buf;
13096
13097 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010013098 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +020013099 if (buf == NULL || buf->b_ml.ml_mfp == NULL
13100 || buf->b_ml.ml_mfp->mf_fname == NULL)
13101 rettv->vval.v_string = NULL;
13102 else
13103 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
13104}
13105
13106/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013107 * "synID(lnum, col, trans)" function
13108 */
13109 static void
13110f_synID(typval_T *argvars UNUSED, typval_T *rettv)
13111{
13112 int id = 0;
13113#ifdef FEAT_SYN_HL
13114 linenr_T lnum;
13115 colnr_T col;
13116 int trans;
13117 int transerr = FALSE;
13118
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013119 lnum = tv_get_lnum(argvars); /* -1 on type error */
13120 col = (linenr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
13121 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013122
13123 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13124 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
13125 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
13126#endif
13127
13128 rettv->vval.v_number = id;
13129}
13130
13131/*
13132 * "synIDattr(id, what [, mode])" function
13133 */
13134 static void
13135f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
13136{
13137 char_u *p = NULL;
13138#ifdef FEAT_SYN_HL
13139 int id;
13140 char_u *what;
13141 char_u *mode;
13142 char_u modebuf[NUMBUFLEN];
13143 int modec;
13144
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013145 id = (int)tv_get_number(&argvars[0]);
13146 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013147 if (argvars[2].v_type != VAR_UNKNOWN)
13148 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013149 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013150 modec = TOLOWER_ASC(mode[0]);
13151 if (modec != 't' && modec != 'c' && modec != 'g')
13152 modec = 0; /* replace invalid with current */
13153 }
13154 else
13155 {
13156#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
13157 if (USE_24BIT)
13158 modec = 'g';
13159 else
13160#endif
13161 if (t_colors > 1)
13162 modec = 'c';
13163 else
13164 modec = 't';
13165 }
13166
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013167 switch (TOLOWER_ASC(what[0]))
13168 {
13169 case 'b':
13170 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
13171 p = highlight_color(id, what, modec);
13172 else /* bold */
13173 p = highlight_has_attr(id, HL_BOLD, modec);
13174 break;
13175
13176 case 'f': /* fg[#] or font */
13177 p = highlight_color(id, what, modec);
13178 break;
13179
13180 case 'i':
13181 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
13182 p = highlight_has_attr(id, HL_INVERSE, modec);
13183 else /* italic */
13184 p = highlight_has_attr(id, HL_ITALIC, modec);
13185 break;
13186
13187 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020013188 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013189 break;
13190
13191 case 'r': /* reverse */
13192 p = highlight_has_attr(id, HL_INVERSE, modec);
13193 break;
13194
13195 case 's':
13196 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
13197 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020013198 /* strikeout */
13199 else if (TOLOWER_ASC(what[1]) == 't' &&
13200 TOLOWER_ASC(what[2]) == 'r')
13201 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013202 else /* standout */
13203 p = highlight_has_attr(id, HL_STANDOUT, modec);
13204 break;
13205
13206 case 'u':
13207 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
13208 /* underline */
13209 p = highlight_has_attr(id, HL_UNDERLINE, modec);
13210 else
13211 /* undercurl */
13212 p = highlight_has_attr(id, HL_UNDERCURL, modec);
13213 break;
13214 }
13215
13216 if (p != NULL)
13217 p = vim_strsave(p);
13218#endif
13219 rettv->v_type = VAR_STRING;
13220 rettv->vval.v_string = p;
13221}
13222
13223/*
13224 * "synIDtrans(id)" function
13225 */
13226 static void
13227f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
13228{
13229 int id;
13230
13231#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013232 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013233
13234 if (id > 0)
13235 id = syn_get_final_id(id);
13236 else
13237#endif
13238 id = 0;
13239
13240 rettv->vval.v_number = id;
13241}
13242
13243/*
13244 * "synconcealed(lnum, col)" function
13245 */
13246 static void
13247f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
13248{
13249#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
13250 linenr_T lnum;
13251 colnr_T col;
13252 int syntax_flags = 0;
13253 int cchar;
13254 int matchid = 0;
13255 char_u str[NUMBUFLEN];
13256#endif
13257
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013258 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013259
13260#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013261 lnum = tv_get_lnum(argvars); /* -1 on type error */
13262 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013263
13264 vim_memset(str, NUL, sizeof(str));
13265
13266 if (rettv_list_alloc(rettv) != FAIL)
13267 {
13268 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13269 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13270 && curwin->w_p_cole > 0)
13271 {
13272 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
13273 syntax_flags = get_syntax_info(&matchid);
13274
13275 /* get the conceal character */
13276 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
13277 {
13278 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020013279 if (cchar == NUL && curwin->w_p_cole == 1)
13280 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013281 if (cchar != NUL)
13282 {
13283# ifdef FEAT_MBYTE
13284 if (has_mbyte)
13285 (*mb_char2bytes)(cchar, str);
13286 else
13287# endif
13288 str[0] = cchar;
13289 }
13290 }
13291 }
13292
13293 list_append_number(rettv->vval.v_list,
13294 (syntax_flags & HL_CONCEAL) != 0);
13295 /* -1 to auto-determine strlen */
13296 list_append_string(rettv->vval.v_list, str, -1);
13297 list_append_number(rettv->vval.v_list, matchid);
13298 }
13299#endif
13300}
13301
13302/*
13303 * "synstack(lnum, col)" function
13304 */
13305 static void
13306f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
13307{
13308#ifdef FEAT_SYN_HL
13309 linenr_T lnum;
13310 colnr_T col;
13311 int i;
13312 int id;
13313#endif
13314
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013315 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013316
13317#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013318 lnum = tv_get_lnum(argvars); /* -1 on type error */
13319 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013320
13321 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13322 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13323 && rettv_list_alloc(rettv) != FAIL)
13324 {
13325 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
13326 for (i = 0; ; ++i)
13327 {
13328 id = syn_get_stack_item(i);
13329 if (id < 0)
13330 break;
13331 if (list_append_number(rettv->vval.v_list, id) == FAIL)
13332 break;
13333 }
13334 }
13335#endif
13336}
13337
13338 static void
13339get_cmd_output_as_rettv(
13340 typval_T *argvars,
13341 typval_T *rettv,
13342 int retlist)
13343{
13344 char_u *res = NULL;
13345 char_u *p;
13346 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013347 int err = FALSE;
13348 FILE *fd;
13349 list_T *list = NULL;
13350 int flags = SHELL_SILENT;
13351
13352 rettv->v_type = VAR_STRING;
13353 rettv->vval.v_string = NULL;
13354 if (check_restricted() || check_secure())
13355 goto errret;
13356
13357 if (argvars[1].v_type != VAR_UNKNOWN)
13358 {
13359 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010013360 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013361 * command.
13362 */
13363 if ((infile = vim_tempname('i', TRUE)) == NULL)
13364 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013365 emsg(_(e_notmp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013366 goto errret;
13367 }
13368
13369 fd = mch_fopen((char *)infile, WRITEBIN);
13370 if (fd == NULL)
13371 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013372 semsg(_(e_notopen), infile);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013373 goto errret;
13374 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010013375 if (argvars[1].v_type == VAR_NUMBER)
13376 {
13377 linenr_T lnum;
13378 buf_T *buf;
13379
13380 buf = buflist_findnr(argvars[1].vval.v_number);
13381 if (buf == NULL)
13382 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013383 semsg(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010013384 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010013385 goto errret;
13386 }
13387
13388 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
13389 {
13390 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
13391 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
13392 {
13393 err = TRUE;
13394 break;
13395 }
13396 if (putc(NL, fd) == EOF)
13397 {
13398 err = TRUE;
13399 break;
13400 }
13401 }
13402 }
13403 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013404 {
13405 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
13406 err = TRUE;
13407 }
13408 else
13409 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010013410 size_t len;
13411 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013412
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013413 p = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013414 if (p == NULL)
13415 {
13416 fclose(fd);
13417 goto errret; /* type error; errmsg already given */
13418 }
13419 len = STRLEN(p);
13420 if (len > 0 && fwrite(p, len, 1, fd) != 1)
13421 err = TRUE;
13422 }
13423 if (fclose(fd) != 0)
13424 err = TRUE;
13425 if (err)
13426 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013427 emsg(_("E677: Error writing temp file"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013428 goto errret;
13429 }
13430 }
13431
13432 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
13433 * echoes typeahead, that messes up the display. */
13434 if (!msg_silent)
13435 flags += SHELL_COOKED;
13436
13437 if (retlist)
13438 {
13439 int len;
13440 listitem_T *li;
13441 char_u *s = NULL;
13442 char_u *start;
13443 char_u *end;
13444 int i;
13445
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013446 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013447 if (res == NULL)
13448 goto errret;
13449
13450 list = list_alloc();
13451 if (list == NULL)
13452 goto errret;
13453
13454 for (i = 0; i < len; ++i)
13455 {
13456 start = res + i;
13457 while (i < len && res[i] != NL)
13458 ++i;
13459 end = res + i;
13460
13461 s = alloc((unsigned)(end - start + 1));
13462 if (s == NULL)
13463 goto errret;
13464
13465 for (p = s; start < end; ++p, ++start)
13466 *p = *start == NUL ? NL : *start;
13467 *p = NUL;
13468
13469 li = listitem_alloc();
13470 if (li == NULL)
13471 {
13472 vim_free(s);
13473 goto errret;
13474 }
13475 li->li_tv.v_type = VAR_STRING;
13476 li->li_tv.v_lock = 0;
13477 li->li_tv.vval.v_string = s;
13478 list_append(list, li);
13479 }
13480
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013481 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013482 list = NULL;
13483 }
13484 else
13485 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013486 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013487#ifdef USE_CR
13488 /* translate <CR> into <NL> */
13489 if (res != NULL)
13490 {
13491 char_u *s;
13492
13493 for (s = res; *s; ++s)
13494 {
13495 if (*s == CAR)
13496 *s = NL;
13497 }
13498 }
13499#else
13500# ifdef USE_CRNL
13501 /* translate <CR><NL> into <NL> */
13502 if (res != NULL)
13503 {
13504 char_u *s, *d;
13505
13506 d = res;
13507 for (s = res; *s; ++s)
13508 {
13509 if (s[0] == CAR && s[1] == NL)
13510 ++s;
13511 *d++ = *s;
13512 }
13513 *d = NUL;
13514 }
13515# endif
13516#endif
13517 rettv->vval.v_string = res;
13518 res = NULL;
13519 }
13520
13521errret:
13522 if (infile != NULL)
13523 {
13524 mch_remove(infile);
13525 vim_free(infile);
13526 }
13527 if (res != NULL)
13528 vim_free(res);
13529 if (list != NULL)
13530 list_free(list);
13531}
13532
13533/*
13534 * "system()" function
13535 */
13536 static void
13537f_system(typval_T *argvars, typval_T *rettv)
13538{
13539 get_cmd_output_as_rettv(argvars, rettv, FALSE);
13540}
13541
13542/*
13543 * "systemlist()" function
13544 */
13545 static void
13546f_systemlist(typval_T *argvars, typval_T *rettv)
13547{
13548 get_cmd_output_as_rettv(argvars, rettv, TRUE);
13549}
13550
13551/*
13552 * "tabpagebuflist()" function
13553 */
13554 static void
13555f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13556{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013557 tabpage_T *tp;
13558 win_T *wp = NULL;
13559
13560 if (argvars[0].v_type == VAR_UNKNOWN)
13561 wp = firstwin;
13562 else
13563 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013564 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013565 if (tp != NULL)
13566 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
13567 }
13568 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
13569 {
13570 for (; wp != NULL; wp = wp->w_next)
13571 if (list_append_number(rettv->vval.v_list,
13572 wp->w_buffer->b_fnum) == FAIL)
13573 break;
13574 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013575}
13576
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013577/*
13578 * "tabpagenr()" function
13579 */
13580 static void
13581f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
13582{
13583 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013584 char_u *arg;
13585
13586 if (argvars[0].v_type != VAR_UNKNOWN)
13587 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013588 arg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013589 nr = 0;
13590 if (arg != NULL)
13591 {
13592 if (STRCMP(arg, "$") == 0)
13593 nr = tabpage_index(NULL) - 1;
13594 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013595 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013596 }
13597 }
13598 else
13599 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013600 rettv->vval.v_number = nr;
13601}
13602
13603
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013604/*
13605 * Common code for tabpagewinnr() and winnr().
13606 */
13607 static int
13608get_winnr(tabpage_T *tp, typval_T *argvar)
13609{
13610 win_T *twin;
13611 int nr = 1;
13612 win_T *wp;
13613 char_u *arg;
13614
13615 twin = (tp == curtab) ? curwin : tp->tp_curwin;
13616 if (argvar->v_type != VAR_UNKNOWN)
13617 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013618 arg = tv_get_string_chk(argvar);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013619 if (arg == NULL)
13620 nr = 0; /* type error; errmsg already given */
13621 else if (STRCMP(arg, "$") == 0)
13622 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
13623 else if (STRCMP(arg, "#") == 0)
13624 {
13625 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
13626 if (twin == NULL)
13627 nr = 0;
13628 }
13629 else
13630 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013631 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013632 nr = 0;
13633 }
13634 }
13635
13636 if (nr > 0)
13637 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
13638 wp != twin; wp = wp->w_next)
13639 {
13640 if (wp == NULL)
13641 {
13642 /* didn't find it in this tabpage */
13643 nr = 0;
13644 break;
13645 }
13646 ++nr;
13647 }
13648 return nr;
13649}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013650
13651/*
13652 * "tabpagewinnr()" function
13653 */
13654 static void
13655f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
13656{
13657 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013658 tabpage_T *tp;
13659
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013660 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013661 if (tp == NULL)
13662 nr = 0;
13663 else
13664 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013665 rettv->vval.v_number = nr;
13666}
13667
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013668/*
13669 * "tagfiles()" function
13670 */
13671 static void
13672f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
13673{
13674 char_u *fname;
13675 tagname_T tn;
13676 int first;
13677
13678 if (rettv_list_alloc(rettv) == FAIL)
13679 return;
13680 fname = alloc(MAXPATHL);
13681 if (fname == NULL)
13682 return;
13683
13684 for (first = TRUE; ; first = FALSE)
13685 if (get_tagfname(&tn, first, fname) == FAIL
13686 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
13687 break;
13688 tagname_free(&tn);
13689 vim_free(fname);
13690}
13691
13692/*
13693 * "taglist()" function
13694 */
13695 static void
13696f_taglist(typval_T *argvars, typval_T *rettv)
13697{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013698 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013699 char_u *tag_pattern;
13700
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013701 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013702
13703 rettv->vval.v_number = FALSE;
13704 if (*tag_pattern == NUL)
13705 return;
13706
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013707 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013708 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013709 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013710 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013711}
13712
13713/*
13714 * "tempname()" function
13715 */
13716 static void
13717f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
13718{
13719 static int x = 'A';
13720
13721 rettv->v_type = VAR_STRING;
13722 rettv->vval.v_string = vim_tempname(x, FALSE);
13723
13724 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
13725 * names. Skip 'I' and 'O', they are used for shell redirection. */
13726 do
13727 {
13728 if (x == 'Z')
13729 x = '0';
13730 else if (x == '9')
13731 x = 'A';
13732 else
13733 {
13734#ifdef EBCDIC
13735 if (x == 'I')
13736 x = 'J';
13737 else if (x == 'R')
13738 x = 'S';
13739 else
13740#endif
13741 ++x;
13742 }
13743 } while (x == 'I' || x == 'O');
13744}
13745
13746#ifdef FEAT_FLOAT
13747/*
13748 * "tan()" function
13749 */
13750 static void
13751f_tan(typval_T *argvars, typval_T *rettv)
13752{
13753 float_T f = 0.0;
13754
13755 rettv->v_type = VAR_FLOAT;
13756 if (get_float_arg(argvars, &f) == OK)
13757 rettv->vval.v_float = tan(f);
13758 else
13759 rettv->vval.v_float = 0.0;
13760}
13761
13762/*
13763 * "tanh()" function
13764 */
13765 static void
13766f_tanh(typval_T *argvars, typval_T *rettv)
13767{
13768 float_T f = 0.0;
13769
13770 rettv->v_type = VAR_FLOAT;
13771 if (get_float_arg(argvars, &f) == OK)
13772 rettv->vval.v_float = tanh(f);
13773 else
13774 rettv->vval.v_float = 0.0;
13775}
13776#endif
13777
13778/*
13779 * "test_alloc_fail(id, countdown, repeat)" function
13780 */
13781 static void
13782f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
13783{
13784 if (argvars[0].v_type != VAR_NUMBER
13785 || argvars[0].vval.v_number <= 0
13786 || argvars[1].v_type != VAR_NUMBER
13787 || argvars[1].vval.v_number < 0
13788 || argvars[2].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013789 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013790 else
13791 {
13792 alloc_fail_id = argvars[0].vval.v_number;
13793 if (alloc_fail_id >= aid_last)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013794 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013795 alloc_fail_countdown = argvars[1].vval.v_number;
13796 alloc_fail_repeat = argvars[2].vval.v_number;
13797 did_outofmem_msg = FALSE;
13798 }
13799}
13800
13801/*
13802 * "test_autochdir()"
13803 */
13804 static void
13805f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13806{
13807#if defined(FEAT_AUTOCHDIR)
13808 test_autochdir = TRUE;
13809#endif
13810}
13811
13812/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020013813 * "test_feedinput()"
13814 */
13815 static void
13816f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
13817{
13818#ifdef USE_INPUT_BUF
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013819 char_u *val = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5e80de32017-09-03 15:48:12 +020013820
13821 if (val != NULL)
13822 {
13823 trash_input_buf();
13824 add_to_input_buf_csi(val, (int)STRLEN(val));
13825 }
13826#endif
13827}
13828
13829/*
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013830 * "test_option_not_set({name})" function
13831 */
13832 static void
13833f_test_option_not_set(typval_T *argvars, typval_T *rettv UNUSED)
13834{
13835 char_u *name = (char_u *)"";
13836
13837 if (argvars[0].v_type != VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013838 emsg(_(e_invarg));
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013839 else
13840 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013841 name = tv_get_string(&argvars[0]);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013842 if (reset_option_was_set(name) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013843 semsg(_(e_invarg2), name);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013844 }
13845}
13846
13847/*
13848 * "test_override({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013849 */
13850 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013851f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013852{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013853 char_u *name = (char_u *)"";
13854 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013855 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013856
13857 if (argvars[0].v_type != VAR_STRING
13858 || (argvars[1].v_type) != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013859 emsg(_(e_invarg));
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013860 else
13861 {
Bram Moolenaare38197d2018-12-24 23:35:13 +010013862 name = tv_get_string(&argvars[0]);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013863 val = (int)tv_get_number(&argvars[1]);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013864
13865 if (STRCMP(name, (char_u *)"redraw") == 0)
13866 disable_redraw_for_testing = val;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020013867 else if (STRCMP(name, (char_u *)"redraw_flag") == 0)
13868 ignore_redraw_flag_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013869 else if (STRCMP(name, (char_u *)"char_avail") == 0)
13870 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013871 else if (STRCMP(name, (char_u *)"starting") == 0)
13872 {
13873 if (val)
13874 {
13875 if (save_starting < 0)
13876 save_starting = starting;
13877 starting = 0;
13878 }
13879 else
13880 {
13881 starting = save_starting;
13882 save_starting = -1;
13883 }
13884 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013885 else if (STRCMP(name, (char_u *)"nfa_fail") == 0)
13886 nfa_fail_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013887 else if (STRCMP(name, (char_u *)"ALL") == 0)
13888 {
13889 disable_char_avail_for_testing = FALSE;
13890 disable_redraw_for_testing = FALSE;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020013891 ignore_redraw_flag_for_testing = FALSE;
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013892 nfa_fail_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013893 if (save_starting >= 0)
13894 {
13895 starting = save_starting;
13896 save_starting = -1;
13897 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013898 }
13899 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013900 semsg(_(e_invarg2), name);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013901 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013902}
13903
13904/*
13905 * "test_garbagecollect_now()" function
13906 */
13907 static void
13908f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13909{
13910 /* This is dangerous, any Lists and Dicts used internally may be freed
13911 * while still in use. */
13912 garbage_collect(TRUE);
13913}
13914
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013915/*
13916 * "test_ignore_error()" function
13917 */
13918 static void
13919f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
13920{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013921 ignore_error_for_testing(tv_get_string(&argvars[0]));
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013922}
13923
Bram Moolenaarc0f5a782019-01-13 15:16:13 +010013924 static void
13925f_test_null_blob(typval_T *argvars UNUSED, typval_T *rettv)
13926{
13927 rettv->v_type = VAR_BLOB;
13928 rettv->vval.v_blob = NULL;
13929}
13930
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013931#ifdef FEAT_JOB_CHANNEL
13932 static void
13933f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
13934{
13935 rettv->v_type = VAR_CHANNEL;
13936 rettv->vval.v_channel = NULL;
13937}
13938#endif
13939
13940 static void
13941f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
13942{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013943 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013944}
13945
13946#ifdef FEAT_JOB_CHANNEL
13947 static void
13948f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
13949{
13950 rettv->v_type = VAR_JOB;
13951 rettv->vval.v_job = NULL;
13952}
13953#endif
13954
13955 static void
13956f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
13957{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013958 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013959}
13960
13961 static void
13962f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
13963{
13964 rettv->v_type = VAR_PARTIAL;
13965 rettv->vval.v_partial = NULL;
13966}
13967
13968 static void
13969f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
13970{
13971 rettv->v_type = VAR_STRING;
13972 rettv->vval.v_string = NULL;
13973}
13974
Bram Moolenaarab186732018-09-14 21:27:06 +020013975#ifdef FEAT_GUI
13976 static void
13977f_test_scrollbar(typval_T *argvars, typval_T *rettv UNUSED)
13978{
13979 char_u *which;
13980 long value;
13981 int dragging;
13982 scrollbar_T *sb = NULL;
13983
13984 if (argvars[0].v_type != VAR_STRING
13985 || (argvars[1].v_type) != VAR_NUMBER
13986 || (argvars[2].v_type) != VAR_NUMBER)
13987 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013988 emsg(_(e_invarg));
Bram Moolenaarab186732018-09-14 21:27:06 +020013989 return;
13990 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013991 which = tv_get_string(&argvars[0]);
13992 value = tv_get_number(&argvars[1]);
13993 dragging = tv_get_number(&argvars[2]);
Bram Moolenaarab186732018-09-14 21:27:06 +020013994
13995 if (STRCMP(which, "left") == 0)
13996 sb = &curwin->w_scrollbars[SBAR_LEFT];
13997 else if (STRCMP(which, "right") == 0)
13998 sb = &curwin->w_scrollbars[SBAR_RIGHT];
13999 else if (STRCMP(which, "hor") == 0)
14000 sb = &gui.bottom_sbar;
14001 if (sb == NULL)
14002 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014003 semsg(_(e_invarg2), which);
Bram Moolenaarab186732018-09-14 21:27:06 +020014004 return;
14005 }
14006 gui_drag_scrollbar(sb, value, dragging);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020014007# ifndef USE_ON_FLY_SCROLL
14008 // need to loop through normal_cmd() to handle the scroll events
14009 exec_normal(FALSE, TRUE, FALSE);
14010# endif
Bram Moolenaarab186732018-09-14 21:27:06 +020014011}
14012#endif
14013
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014014 static void
14015f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
14016{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014017 time_for_testing = (time_t)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014018}
14019
14020#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
14021/*
14022 * Get a callback from "arg". It can be a Funcref or a function name.
14023 * When "arg" is zero return an empty string.
14024 * Return NULL for an invalid argument.
14025 */
14026 char_u *
14027get_callback(typval_T *arg, partial_T **pp)
14028{
14029 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
14030 {
14031 *pp = arg->vval.v_partial;
14032 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020014033 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014034 }
14035 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020014036 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014037 {
14038 func_ref(arg->vval.v_string);
14039 return arg->vval.v_string;
14040 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014041 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
14042 return (char_u *)"";
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014043 emsg(_("E921: Invalid callback argument"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014044 return NULL;
14045}
14046
14047/*
Bram Moolenaard5359b22018-04-05 22:44:39 +020014048 * Unref/free "callback" and "partial" returned by get_callback().
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014049 */
14050 void
14051free_callback(char_u *callback, partial_T *partial)
14052{
14053 if (partial != NULL)
14054 partial_unref(partial);
14055 else if (callback != NULL)
14056 {
14057 func_unref(callback);
14058 vim_free(callback);
14059 }
14060}
14061#endif
14062
14063#ifdef FEAT_TIMERS
14064/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014065 * "timer_info([timer])" function
14066 */
14067 static void
14068f_timer_info(typval_T *argvars, typval_T *rettv)
14069{
14070 timer_T *timer = NULL;
14071
14072 if (rettv_list_alloc(rettv) != OK)
14073 return;
14074 if (argvars[0].v_type != VAR_UNKNOWN)
14075 {
14076 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014077 emsg(_(e_number_exp));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014078 else
14079 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014080 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014081 if (timer != NULL)
14082 add_timer_info(rettv, timer);
14083 }
14084 }
14085 else
14086 add_timer_info_all(rettv);
14087}
14088
14089/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014090 * "timer_pause(timer, paused)" function
14091 */
14092 static void
14093f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
14094{
14095 timer_T *timer = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014096 int paused = (int)tv_get_number(&argvars[1]);
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014097
14098 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014099 emsg(_(e_number_exp));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014100 else
14101 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014102 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014103 if (timer != NULL)
14104 timer->tr_paused = paused;
14105 }
14106}
14107
14108/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014109 * "timer_start(time, callback [, options])" function
14110 */
14111 static void
14112f_timer_start(typval_T *argvars, typval_T *rettv)
14113{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014114 long msec = (long)tv_get_number(&argvars[0]);
Bram Moolenaar75537a92016-09-05 22:45:28 +020014115 timer_T *timer;
14116 int repeat = 0;
14117 char_u *callback;
14118 dict_T *dict;
14119 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014120
Bram Moolenaar75537a92016-09-05 22:45:28 +020014121 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014122 if (check_secure())
14123 return;
14124 if (argvars[2].v_type != VAR_UNKNOWN)
14125 {
14126 if (argvars[2].v_type != VAR_DICT
14127 || (dict = argvars[2].vval.v_dict) == NULL)
14128 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014129 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014130 return;
14131 }
14132 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014133 repeat = dict_get_number(dict, (char_u *)"repeat");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014134 }
14135
Bram Moolenaar75537a92016-09-05 22:45:28 +020014136 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014137 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020014138 return;
14139
14140 timer = create_timer(msec, repeat);
14141 if (timer == NULL)
14142 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014143 else
14144 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020014145 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020014146 timer->tr_callback = vim_strsave(callback);
14147 else
14148 /* pointer into the partial */
14149 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020014150 timer->tr_partial = partial;
14151 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014152 }
14153}
14154
14155/*
14156 * "timer_stop(timer)" function
14157 */
14158 static void
14159f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
14160{
14161 timer_T *timer;
14162
14163 if (argvars[0].v_type != VAR_NUMBER)
14164 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014165 emsg(_(e_number_exp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014166 return;
14167 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014168 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014169 if (timer != NULL)
14170 stop_timer(timer);
14171}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014172
14173/*
14174 * "timer_stopall()" function
14175 */
14176 static void
14177f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14178{
14179 stop_all_timers();
14180}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014181#endif
14182
14183/*
14184 * "tolower(string)" function
14185 */
14186 static void
14187f_tolower(typval_T *argvars, typval_T *rettv)
14188{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014189 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014190 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014191}
14192
14193/*
14194 * "toupper(string)" function
14195 */
14196 static void
14197f_toupper(typval_T *argvars, typval_T *rettv)
14198{
14199 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014200 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014201}
14202
14203/*
14204 * "tr(string, fromstr, tostr)" function
14205 */
14206 static void
14207f_tr(typval_T *argvars, typval_T *rettv)
14208{
14209 char_u *in_str;
14210 char_u *fromstr;
14211 char_u *tostr;
14212 char_u *p;
14213#ifdef FEAT_MBYTE
14214 int inlen;
14215 int fromlen;
14216 int tolen;
14217 int idx;
14218 char_u *cpstr;
14219 int cplen;
14220 int first = TRUE;
14221#endif
14222 char_u buf[NUMBUFLEN];
14223 char_u buf2[NUMBUFLEN];
14224 garray_T ga;
14225
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014226 in_str = tv_get_string(&argvars[0]);
14227 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
14228 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014229
14230 /* Default return value: empty string. */
14231 rettv->v_type = VAR_STRING;
14232 rettv->vval.v_string = NULL;
14233 if (fromstr == NULL || tostr == NULL)
14234 return; /* type error; errmsg already given */
14235 ga_init2(&ga, (int)sizeof(char), 80);
14236
14237#ifdef FEAT_MBYTE
14238 if (!has_mbyte)
14239#endif
14240 /* not multi-byte: fromstr and tostr must be the same length */
14241 if (STRLEN(fromstr) != STRLEN(tostr))
14242 {
14243#ifdef FEAT_MBYTE
14244error:
14245#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014246 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014247 ga_clear(&ga);
14248 return;
14249 }
14250
14251 /* fromstr and tostr have to contain the same number of chars */
14252 while (*in_str != NUL)
14253 {
14254#ifdef FEAT_MBYTE
14255 if (has_mbyte)
14256 {
14257 inlen = (*mb_ptr2len)(in_str);
14258 cpstr = in_str;
14259 cplen = inlen;
14260 idx = 0;
14261 for (p = fromstr; *p != NUL; p += fromlen)
14262 {
14263 fromlen = (*mb_ptr2len)(p);
14264 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
14265 {
14266 for (p = tostr; *p != NUL; p += tolen)
14267 {
14268 tolen = (*mb_ptr2len)(p);
14269 if (idx-- == 0)
14270 {
14271 cplen = tolen;
14272 cpstr = p;
14273 break;
14274 }
14275 }
14276 if (*p == NUL) /* tostr is shorter than fromstr */
14277 goto error;
14278 break;
14279 }
14280 ++idx;
14281 }
14282
14283 if (first && cpstr == in_str)
14284 {
14285 /* Check that fromstr and tostr have the same number of
14286 * (multi-byte) characters. Done only once when a character
14287 * of in_str doesn't appear in fromstr. */
14288 first = FALSE;
14289 for (p = tostr; *p != NUL; p += tolen)
14290 {
14291 tolen = (*mb_ptr2len)(p);
14292 --idx;
14293 }
14294 if (idx != 0)
14295 goto error;
14296 }
14297
14298 (void)ga_grow(&ga, cplen);
14299 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
14300 ga.ga_len += cplen;
14301
14302 in_str += inlen;
14303 }
14304 else
14305#endif
14306 {
14307 /* When not using multi-byte chars we can do it faster. */
14308 p = vim_strchr(fromstr, *in_str);
14309 if (p != NULL)
14310 ga_append(&ga, tostr[p - fromstr]);
14311 else
14312 ga_append(&ga, *in_str);
14313 ++in_str;
14314 }
14315 }
14316
14317 /* add a terminating NUL */
14318 (void)ga_grow(&ga, 1);
14319 ga_append(&ga, NUL);
14320
14321 rettv->vval.v_string = ga.ga_data;
14322}
14323
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014324/*
14325 * "trim({expr})" function
14326 */
14327 static void
14328f_trim(typval_T *argvars, typval_T *rettv)
14329{
14330 char_u buf1[NUMBUFLEN];
14331 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014332 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014333 char_u *mask = NULL;
14334 char_u *tail;
14335 char_u *prev;
14336 char_u *p;
14337 int c1;
14338
14339 rettv->v_type = VAR_STRING;
14340 if (head == NULL)
14341 {
14342 rettv->vval.v_string = NULL;
14343 return;
14344 }
14345
14346 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014347 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014348
14349 while (*head != NUL)
14350 {
14351 c1 = PTR2CHAR(head);
14352 if (mask == NULL)
14353 {
14354 if (c1 > ' ' && c1 != 0xa0)
14355 break;
14356 }
14357 else
14358 {
14359 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14360 if (c1 == PTR2CHAR(p))
14361 break;
14362 if (*p == NUL)
14363 break;
14364 }
14365 MB_PTR_ADV(head);
14366 }
14367
14368 for (tail = head + STRLEN(head); tail > head; tail = prev)
14369 {
14370 prev = tail;
14371 MB_PTR_BACK(head, prev);
14372 c1 = PTR2CHAR(prev);
14373 if (mask == NULL)
14374 {
14375 if (c1 > ' ' && c1 != 0xa0)
14376 break;
14377 }
14378 else
14379 {
14380 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14381 if (c1 == PTR2CHAR(p))
14382 break;
14383 if (*p == NUL)
14384 break;
14385 }
14386 }
14387 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
14388}
14389
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014390#ifdef FEAT_FLOAT
14391/*
14392 * "trunc({float})" function
14393 */
14394 static void
14395f_trunc(typval_T *argvars, typval_T *rettv)
14396{
14397 float_T f = 0.0;
14398
14399 rettv->v_type = VAR_FLOAT;
14400 if (get_float_arg(argvars, &f) == OK)
14401 /* trunc() is not in C90, use floor() or ceil() instead. */
14402 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
14403 else
14404 rettv->vval.v_float = 0.0;
14405}
14406#endif
14407
14408/*
14409 * "type(expr)" function
14410 */
14411 static void
14412f_type(typval_T *argvars, typval_T *rettv)
14413{
14414 int n = -1;
14415
14416 switch (argvars[0].v_type)
14417 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020014418 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
14419 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014420 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020014421 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
14422 case VAR_LIST: n = VAR_TYPE_LIST; break;
14423 case VAR_DICT: n = VAR_TYPE_DICT; break;
14424 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014425 case VAR_SPECIAL:
14426 if (argvars[0].vval.v_number == VVAL_FALSE
14427 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020014428 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014429 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020014430 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014431 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020014432 case VAR_JOB: n = VAR_TYPE_JOB; break;
14433 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014434 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014435 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010014436 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014437 n = -1;
14438 break;
14439 }
14440 rettv->vval.v_number = n;
14441}
14442
14443/*
14444 * "undofile(name)" function
14445 */
14446 static void
14447f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
14448{
14449 rettv->v_type = VAR_STRING;
14450#ifdef FEAT_PERSISTENT_UNDO
14451 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014452 char_u *fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014453
14454 if (*fname == NUL)
14455 {
14456 /* If there is no file name there will be no undo file. */
14457 rettv->vval.v_string = NULL;
14458 }
14459 else
14460 {
14461 char_u *ffname = FullName_save(fname, FALSE);
14462
14463 if (ffname != NULL)
14464 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
14465 vim_free(ffname);
14466 }
14467 }
14468#else
14469 rettv->vval.v_string = NULL;
14470#endif
14471}
14472
14473/*
14474 * "undotree()" function
14475 */
14476 static void
14477f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
14478{
14479 if (rettv_dict_alloc(rettv) == OK)
14480 {
14481 dict_T *dict = rettv->vval.v_dict;
14482 list_T *list;
14483
Bram Moolenaare0be1672018-07-08 16:50:37 +020014484 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
14485 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
14486 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
14487 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
14488 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
14489 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014490
14491 list = list_alloc();
14492 if (list != NULL)
14493 {
14494 u_eval_tree(curbuf->b_u_oldhead, list);
14495 dict_add_list(dict, "entries", list);
14496 }
14497 }
14498}
14499
14500/*
14501 * "values(dict)" function
14502 */
14503 static void
14504f_values(typval_T *argvars, typval_T *rettv)
14505{
14506 dict_list(argvars, rettv, 1);
14507}
14508
14509/*
14510 * "virtcol(string)" function
14511 */
14512 static void
14513f_virtcol(typval_T *argvars, typval_T *rettv)
14514{
14515 colnr_T vcol = 0;
14516 pos_T *fp;
14517 int fnum = curbuf->b_fnum;
14518
14519 fp = var2fpos(&argvars[0], FALSE, &fnum);
14520 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
14521 && fnum == curbuf->b_fnum)
14522 {
14523 getvvcol(curwin, fp, NULL, NULL, &vcol);
14524 ++vcol;
14525 }
14526
14527 rettv->vval.v_number = vcol;
14528}
14529
14530/*
14531 * "visualmode()" function
14532 */
14533 static void
14534f_visualmode(typval_T *argvars, typval_T *rettv)
14535{
14536 char_u str[2];
14537
14538 rettv->v_type = VAR_STRING;
14539 str[0] = curbuf->b_visual_mode_eval;
14540 str[1] = NUL;
14541 rettv->vval.v_string = vim_strsave(str);
14542
14543 /* A non-zero number or non-empty string argument: reset mode. */
14544 if (non_zero_arg(&argvars[0]))
14545 curbuf->b_visual_mode_eval = NUL;
14546}
14547
14548/*
14549 * "wildmenumode()" function
14550 */
14551 static void
14552f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14553{
14554#ifdef FEAT_WILDMENU
14555 if (wild_menu_showing)
14556 rettv->vval.v_number = 1;
14557#endif
14558}
14559
14560/*
14561 * "winbufnr(nr)" function
14562 */
14563 static void
14564f_winbufnr(typval_T *argvars, typval_T *rettv)
14565{
14566 win_T *wp;
14567
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014568 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014569 if (wp == NULL)
14570 rettv->vval.v_number = -1;
14571 else
14572 rettv->vval.v_number = wp->w_buffer->b_fnum;
14573}
14574
14575/*
14576 * "wincol()" function
14577 */
14578 static void
14579f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
14580{
14581 validate_cursor();
14582 rettv->vval.v_number = curwin->w_wcol + 1;
14583}
14584
14585/*
14586 * "winheight(nr)" function
14587 */
14588 static void
14589f_winheight(typval_T *argvars, typval_T *rettv)
14590{
14591 win_T *wp;
14592
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014593 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014594 if (wp == NULL)
14595 rettv->vval.v_number = -1;
14596 else
14597 rettv->vval.v_number = wp->w_height;
14598}
14599
14600/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020014601 * "winlayout()" function
14602 */
14603 static void
14604f_winlayout(typval_T *argvars, typval_T *rettv)
14605{
14606 tabpage_T *tp;
14607
14608 if (rettv_list_alloc(rettv) != OK)
14609 return;
14610
14611 if (argvars[0].v_type == VAR_UNKNOWN)
14612 tp = curtab;
14613 else
14614 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014615 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020014616 if (tp == NULL)
14617 return;
14618 }
14619
14620 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
14621}
14622
14623/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014624 * "winline()" function
14625 */
14626 static void
14627f_winline(typval_T *argvars UNUSED, typval_T *rettv)
14628{
14629 validate_cursor();
14630 rettv->vval.v_number = curwin->w_wrow + 1;
14631}
14632
14633/*
14634 * "winnr()" function
14635 */
14636 static void
14637f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
14638{
14639 int nr = 1;
14640
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014641 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014642 rettv->vval.v_number = nr;
14643}
14644
14645/*
14646 * "winrestcmd()" function
14647 */
14648 static void
14649f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
14650{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014651 win_T *wp;
14652 int winnr = 1;
14653 garray_T ga;
14654 char_u buf[50];
14655
14656 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020014657 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014658 {
14659 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
14660 ga_concat(&ga, buf);
14661 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
14662 ga_concat(&ga, buf);
14663 ++winnr;
14664 }
14665 ga_append(&ga, NUL);
14666
14667 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014668 rettv->v_type = VAR_STRING;
14669}
14670
14671/*
14672 * "winrestview()" function
14673 */
14674 static void
14675f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
14676{
14677 dict_T *dict;
14678
14679 if (argvars[0].v_type != VAR_DICT
14680 || (dict = argvars[0].vval.v_dict) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014681 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014682 else
14683 {
14684 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014685 curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014686 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014687 curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014688#ifdef FEAT_VIRTUALEDIT
14689 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014690 curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014691#endif
14692 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
14693 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010014694 curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014695 curwin->w_set_curswant = FALSE;
14696 }
14697
14698 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014699 set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014700#ifdef FEAT_DIFF
14701 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014702 curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014703#endif
14704 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014705 curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014706 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014707 curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014708
14709 check_cursor();
14710 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020014711 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014712 changed_window_setting();
14713
14714 if (curwin->w_topline <= 0)
14715 curwin->w_topline = 1;
14716 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
14717 curwin->w_topline = curbuf->b_ml.ml_line_count;
14718#ifdef FEAT_DIFF
14719 check_topfill(curwin, TRUE);
14720#endif
14721 }
14722}
14723
14724/*
14725 * "winsaveview()" function
14726 */
14727 static void
14728f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
14729{
14730 dict_T *dict;
14731
14732 if (rettv_dict_alloc(rettv) == FAIL)
14733 return;
14734 dict = rettv->vval.v_dict;
14735
Bram Moolenaare0be1672018-07-08 16:50:37 +020014736 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
14737 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014738#ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +020014739 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014740#endif
14741 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020014742 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014743
Bram Moolenaare0be1672018-07-08 16:50:37 +020014744 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014745#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020014746 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014747#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020014748 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
14749 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014750}
14751
14752/*
14753 * "winwidth(nr)" function
14754 */
14755 static void
14756f_winwidth(typval_T *argvars, typval_T *rettv)
14757{
14758 win_T *wp;
14759
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014760 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014761 if (wp == NULL)
14762 rettv->vval.v_number = -1;
14763 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014764 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014765}
14766
14767/*
14768 * "wordcount()" function
14769 */
14770 static void
14771f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
14772{
14773 if (rettv_dict_alloc(rettv) == FAIL)
14774 return;
14775 cursor_pos_info(rettv->vval.v_dict);
14776}
14777
14778/*
14779 * "writefile()" function
14780 */
14781 static void
14782f_writefile(typval_T *argvars, typval_T *rettv)
14783{
14784 int binary = FALSE;
14785 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014786#ifdef HAVE_FSYNC
14787 int do_fsync = p_fs;
14788#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014789 char_u *fname;
14790 FILE *fd;
14791 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014792 listitem_T *li;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014793 list_T *list = NULL;
14794 blob_T *blob = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014795
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014796 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014797 if (check_restricted() || check_secure())
14798 return;
14799
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014800 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014801 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014802 list = argvars[0].vval.v_list;
14803 if (list == NULL)
14804 return;
14805 for (li = list->lv_first; li != NULL; li = li->li_next)
14806 if (tv_get_string_chk(&li->li_tv) == NULL)
14807 return;
14808 }
14809 else if (argvars[0].v_type == VAR_BLOB)
14810 {
14811 blob = argvars[0].vval.v_blob;
14812 if (blob == NULL)
14813 return;
14814 }
14815 else
14816 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014817 semsg(_(e_invarg2), "writefile()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014818 return;
14819 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014820
14821 if (argvars[2].v_type != VAR_UNKNOWN)
14822 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014823 char_u *arg2 = tv_get_string_chk(&argvars[2]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014824
14825 if (arg2 == NULL)
14826 return;
14827 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014828 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014829 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014830 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014831#ifdef HAVE_FSYNC
14832 if (vim_strchr(arg2, 's') != NULL)
14833 do_fsync = TRUE;
14834 else if (vim_strchr(arg2, 'S') != NULL)
14835 do_fsync = FALSE;
14836#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014837 }
14838
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014839 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014840 if (fname == NULL)
14841 return;
14842
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014843 /* Always open the file in binary mode, library functions have a mind of
14844 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014845 if (*fname == NUL || (fd = mch_fopen((char *)fname,
14846 append ? APPENDBIN : WRITEBIN)) == NULL)
14847 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014848 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014849 ret = -1;
14850 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014851 else if (blob)
14852 {
14853 if (write_blob(fd, blob) == FAIL)
14854 ret = -1;
14855#ifdef HAVE_FSYNC
14856 else if (do_fsync)
14857 // Ignore the error, the user wouldn't know what to do about it.
14858 // May happen for a device.
14859 vim_ignored = fsync(fileno(fd));
14860#endif
14861 fclose(fd);
14862 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014863 else
14864 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014865 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014866 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014867#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010014868 else if (do_fsync)
14869 /* Ignore the error, the user wouldn't know what to do about it.
14870 * May happen for a device. */
Bram Moolenaar42335f52018-09-13 15:33:43 +020014871 vim_ignored = fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014872#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014873 fclose(fd);
14874 }
14875
14876 rettv->vval.v_number = ret;
14877}
14878
14879/*
14880 * "xor(expr, expr)" function
14881 */
14882 static void
14883f_xor(typval_T *argvars, typval_T *rettv)
14884{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014885 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
14886 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014887}
14888
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014889#endif /* FEAT_EVAL */