blob: f30dd9b54c0c7c5a85f4686543855220ddfee8cf [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");
32#ifdef FEAT_QUICKFIX
33static char *e_stringreq = N_("E928: String required");
34#endif
35
36#ifdef FEAT_FLOAT
37static void f_abs(typval_T *argvars, typval_T *rettv);
38static void f_acos(typval_T *argvars, typval_T *rettv);
39#endif
40static void f_add(typval_T *argvars, typval_T *rettv);
41static void f_and(typval_T *argvars, typval_T *rettv);
42static void f_append(typval_T *argvars, typval_T *rettv);
43static void f_argc(typval_T *argvars, typval_T *rettv);
44static void f_argidx(typval_T *argvars, typval_T *rettv);
45static void f_arglistid(typval_T *argvars, typval_T *rettv);
46static void f_argv(typval_T *argvars, typval_T *rettv);
47static void f_assert_equal(typval_T *argvars, typval_T *rettv);
48static void f_assert_exception(typval_T *argvars, typval_T *rettv);
49static void f_assert_fails(typval_T *argvars, typval_T *rettv);
50static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaar61c04492016-07-23 15:35:35 +020051static void f_assert_inrange(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020052static void f_assert_match(typval_T *argvars, typval_T *rettv);
53static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
54static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar42205552017-03-18 19:42:22 +010055static void f_assert_report(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020056static void f_assert_true(typval_T *argvars, typval_T *rettv);
57#ifdef FEAT_FLOAT
58static void f_asin(typval_T *argvars, typval_T *rettv);
59static void f_atan(typval_T *argvars, typval_T *rettv);
60static void f_atan2(typval_T *argvars, typval_T *rettv);
61#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010062#ifdef FEAT_BEVAL
63static void f_balloon_show(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010064# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010065static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010066# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010067#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020068static void f_browse(typval_T *argvars, typval_T *rettv);
69static void f_browsedir(typval_T *argvars, typval_T *rettv);
70static void f_bufexists(typval_T *argvars, typval_T *rettv);
71static void f_buflisted(typval_T *argvars, typval_T *rettv);
72static void f_bufloaded(typval_T *argvars, typval_T *rettv);
73static void f_bufname(typval_T *argvars, typval_T *rettv);
74static void f_bufnr(typval_T *argvars, typval_T *rettv);
75static void f_bufwinid(typval_T *argvars, typval_T *rettv);
76static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
77static void f_byte2line(typval_T *argvars, typval_T *rettv);
78static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
79static void f_byteidx(typval_T *argvars, typval_T *rettv);
80static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
81static void f_call(typval_T *argvars, typval_T *rettv);
82#ifdef FEAT_FLOAT
83static void f_ceil(typval_T *argvars, typval_T *rettv);
84#endif
85#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +010086static void f_ch_canread(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020087static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0874a832016-09-01 15:11:51 +020088static void f_ch_close_in(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020089static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
90static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
91static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
92static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
93static void f_ch_info(typval_T *argvars, typval_T *rettv);
94static void f_ch_log(typval_T *argvars, typval_T *rettv);
95static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
96static void f_ch_open(typval_T *argvars, typval_T *rettv);
97static void f_ch_read(typval_T *argvars, typval_T *rettv);
98static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
99static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
100static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
101static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
102static void f_ch_status(typval_T *argvars, typval_T *rettv);
103#endif
104static void f_changenr(typval_T *argvars, typval_T *rettv);
105static void f_char2nr(typval_T *argvars, typval_T *rettv);
106static void f_cindent(typval_T *argvars, typval_T *rettv);
107static void f_clearmatches(typval_T *argvars, typval_T *rettv);
108static void f_col(typval_T *argvars, typval_T *rettv);
109#if defined(FEAT_INS_EXPAND)
110static void f_complete(typval_T *argvars, typval_T *rettv);
111static void f_complete_add(typval_T *argvars, typval_T *rettv);
112static void f_complete_check(typval_T *argvars, typval_T *rettv);
113#endif
114static void f_confirm(typval_T *argvars, typval_T *rettv);
115static void f_copy(typval_T *argvars, typval_T *rettv);
116#ifdef FEAT_FLOAT
117static void f_cos(typval_T *argvars, typval_T *rettv);
118static void f_cosh(typval_T *argvars, typval_T *rettv);
119#endif
120static void f_count(typval_T *argvars, typval_T *rettv);
121static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
122static void f_cursor(typval_T *argsvars, typval_T *rettv);
123static void f_deepcopy(typval_T *argvars, typval_T *rettv);
124static void f_delete(typval_T *argvars, typval_T *rettv);
125static void f_did_filetype(typval_T *argvars, typval_T *rettv);
126static void f_diff_filler(typval_T *argvars, typval_T *rettv);
127static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
128static void f_empty(typval_T *argvars, typval_T *rettv);
129static void f_escape(typval_T *argvars, typval_T *rettv);
130static void f_eval(typval_T *argvars, typval_T *rettv);
131static void f_eventhandler(typval_T *argvars, typval_T *rettv);
132static void f_executable(typval_T *argvars, typval_T *rettv);
133static void f_execute(typval_T *argvars, typval_T *rettv);
134static void f_exepath(typval_T *argvars, typval_T *rettv);
135static void f_exists(typval_T *argvars, typval_T *rettv);
136#ifdef FEAT_FLOAT
137static void f_exp(typval_T *argvars, typval_T *rettv);
138#endif
139static void f_expand(typval_T *argvars, typval_T *rettv);
140static void f_extend(typval_T *argvars, typval_T *rettv);
141static void f_feedkeys(typval_T *argvars, typval_T *rettv);
142static void f_filereadable(typval_T *argvars, typval_T *rettv);
143static void f_filewritable(typval_T *argvars, typval_T *rettv);
144static void f_filter(typval_T *argvars, typval_T *rettv);
145static void f_finddir(typval_T *argvars, typval_T *rettv);
146static void f_findfile(typval_T *argvars, typval_T *rettv);
147#ifdef FEAT_FLOAT
148static void f_float2nr(typval_T *argvars, typval_T *rettv);
149static void f_floor(typval_T *argvars, typval_T *rettv);
150static void f_fmod(typval_T *argvars, typval_T *rettv);
151#endif
152static void f_fnameescape(typval_T *argvars, typval_T *rettv);
153static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
154static void f_foldclosed(typval_T *argvars, typval_T *rettv);
155static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
156static void f_foldlevel(typval_T *argvars, typval_T *rettv);
157static void f_foldtext(typval_T *argvars, typval_T *rettv);
158static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
159static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200160static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200161static void f_function(typval_T *argvars, typval_T *rettv);
162static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
163static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200164static void f_getbufinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200165static void f_getbufline(typval_T *argvars, typval_T *rettv);
166static void f_getbufvar(typval_T *argvars, typval_T *rettv);
167static void f_getchar(typval_T *argvars, typval_T *rettv);
168static void f_getcharmod(typval_T *argvars, typval_T *rettv);
169static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
170static void f_getcmdline(typval_T *argvars, typval_T *rettv);
171#if defined(FEAT_CMDL_COMPL)
172static void f_getcompletion(typval_T *argvars, typval_T *rettv);
173#endif
174static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
175static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
176static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
177static void f_getcwd(typval_T *argvars, typval_T *rettv);
178static void f_getfontname(typval_T *argvars, typval_T *rettv);
179static void f_getfperm(typval_T *argvars, typval_T *rettv);
180static void f_getfsize(typval_T *argvars, typval_T *rettv);
181static void f_getftime(typval_T *argvars, typval_T *rettv);
182static void f_getftype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +0100183static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200184static void f_getline(typval_T *argvars, typval_T *rettv);
Bram Moolenaard823fa92016-08-12 16:29:27 +0200185static void f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200186static void f_getmatches(typval_T *argvars, typval_T *rettv);
187static void f_getpid(typval_T *argvars, typval_T *rettv);
188static void f_getcurpos(typval_T *argvars, typval_T *rettv);
189static void f_getpos(typval_T *argvars, typval_T *rettv);
190static void f_getqflist(typval_T *argvars, typval_T *rettv);
191static void f_getreg(typval_T *argvars, typval_T *rettv);
192static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200193static void f_gettabinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200194static void f_gettabvar(typval_T *argvars, typval_T *rettv);
195static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200196static void f_getwininfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200197static void f_getwinposx(typval_T *argvars, typval_T *rettv);
198static void f_getwinposy(typval_T *argvars, typval_T *rettv);
199static void f_getwinvar(typval_T *argvars, typval_T *rettv);
200static void f_glob(typval_T *argvars, typval_T *rettv);
201static void f_globpath(typval_T *argvars, typval_T *rettv);
202static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
203static void f_has(typval_T *argvars, typval_T *rettv);
204static void f_has_key(typval_T *argvars, typval_T *rettv);
205static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
206static void f_hasmapto(typval_T *argvars, typval_T *rettv);
207static void f_histadd(typval_T *argvars, typval_T *rettv);
208static void f_histdel(typval_T *argvars, typval_T *rettv);
209static void f_histget(typval_T *argvars, typval_T *rettv);
210static void f_histnr(typval_T *argvars, typval_T *rettv);
211static void f_hlID(typval_T *argvars, typval_T *rettv);
212static void f_hlexists(typval_T *argvars, typval_T *rettv);
213static void f_hostname(typval_T *argvars, typval_T *rettv);
214static void f_iconv(typval_T *argvars, typval_T *rettv);
215static void f_indent(typval_T *argvars, typval_T *rettv);
216static void f_index(typval_T *argvars, typval_T *rettv);
217static void f_input(typval_T *argvars, typval_T *rettv);
218static void f_inputdialog(typval_T *argvars, typval_T *rettv);
219static void f_inputlist(typval_T *argvars, typval_T *rettv);
220static void f_inputrestore(typval_T *argvars, typval_T *rettv);
221static void f_inputsave(typval_T *argvars, typval_T *rettv);
222static void f_inputsecret(typval_T *argvars, typval_T *rettv);
223static void f_insert(typval_T *argvars, typval_T *rettv);
224static void f_invert(typval_T *argvars, typval_T *rettv);
225static void f_isdirectory(typval_T *argvars, typval_T *rettv);
226static void f_islocked(typval_T *argvars, typval_T *rettv);
227#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
228static void f_isnan(typval_T *argvars, typval_T *rettv);
229#endif
230static void f_items(typval_T *argvars, typval_T *rettv);
231#ifdef FEAT_JOB_CHANNEL
232static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
233static void f_job_info(typval_T *argvars, typval_T *rettv);
234static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
235static void f_job_start(typval_T *argvars, typval_T *rettv);
236static void f_job_stop(typval_T *argvars, typval_T *rettv);
237static void f_job_status(typval_T *argvars, typval_T *rettv);
238#endif
239static void f_join(typval_T *argvars, typval_T *rettv);
240static void f_js_decode(typval_T *argvars, typval_T *rettv);
241static void f_js_encode(typval_T *argvars, typval_T *rettv);
242static void f_json_decode(typval_T *argvars, typval_T *rettv);
243static void f_json_encode(typval_T *argvars, typval_T *rettv);
244static void f_keys(typval_T *argvars, typval_T *rettv);
245static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
246static void f_len(typval_T *argvars, typval_T *rettv);
247static void f_libcall(typval_T *argvars, typval_T *rettv);
248static void f_libcallnr(typval_T *argvars, typval_T *rettv);
249static void f_line(typval_T *argvars, typval_T *rettv);
250static void f_line2byte(typval_T *argvars, typval_T *rettv);
251static void f_lispindent(typval_T *argvars, typval_T *rettv);
252static void f_localtime(typval_T *argvars, typval_T *rettv);
253#ifdef FEAT_FLOAT
254static void f_log(typval_T *argvars, typval_T *rettv);
255static void f_log10(typval_T *argvars, typval_T *rettv);
256#endif
257#ifdef FEAT_LUA
258static void f_luaeval(typval_T *argvars, typval_T *rettv);
259#endif
260static void f_map(typval_T *argvars, typval_T *rettv);
261static void f_maparg(typval_T *argvars, typval_T *rettv);
262static void f_mapcheck(typval_T *argvars, typval_T *rettv);
263static void f_match(typval_T *argvars, typval_T *rettv);
264static void f_matchadd(typval_T *argvars, typval_T *rettv);
265static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
266static void f_matcharg(typval_T *argvars, typval_T *rettv);
267static void f_matchdelete(typval_T *argvars, typval_T *rettv);
268static void f_matchend(typval_T *argvars, typval_T *rettv);
269static void f_matchlist(typval_T *argvars, typval_T *rettv);
270static void f_matchstr(typval_T *argvars, typval_T *rettv);
271static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
272static void f_max(typval_T *argvars, typval_T *rettv);
273static void f_min(typval_T *argvars, typval_T *rettv);
274#ifdef vim_mkdir
275static void f_mkdir(typval_T *argvars, typval_T *rettv);
276#endif
277static void f_mode(typval_T *argvars, typval_T *rettv);
278#ifdef FEAT_MZSCHEME
279static void f_mzeval(typval_T *argvars, typval_T *rettv);
280#endif
281static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
282static void f_nr2char(typval_T *argvars, typval_T *rettv);
283static void f_or(typval_T *argvars, typval_T *rettv);
284static void f_pathshorten(typval_T *argvars, typval_T *rettv);
285#ifdef FEAT_PERL
286static void f_perleval(typval_T *argvars, typval_T *rettv);
287#endif
288#ifdef FEAT_FLOAT
289static void f_pow(typval_T *argvars, typval_T *rettv);
290#endif
291static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
292static void f_printf(typval_T *argvars, typval_T *rettv);
293static void f_pumvisible(typval_T *argvars, typval_T *rettv);
294#ifdef FEAT_PYTHON3
295static void f_py3eval(typval_T *argvars, typval_T *rettv);
296#endif
297#ifdef FEAT_PYTHON
298static void f_pyeval(typval_T *argvars, typval_T *rettv);
299#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100300#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
301static void f_pyxeval(typval_T *argvars, typval_T *rettv);
302#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200303static void f_range(typval_T *argvars, typval_T *rettv);
304static void f_readfile(typval_T *argvars, typval_T *rettv);
305static void f_reltime(typval_T *argvars, typval_T *rettv);
306#ifdef FEAT_FLOAT
307static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
308#endif
309static void f_reltimestr(typval_T *argvars, typval_T *rettv);
310static void f_remote_expr(typval_T *argvars, typval_T *rettv);
311static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
312static void f_remote_peek(typval_T *argvars, typval_T *rettv);
313static void f_remote_read(typval_T *argvars, typval_T *rettv);
314static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100315static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200316static void f_remove(typval_T *argvars, typval_T *rettv);
317static void f_rename(typval_T *argvars, typval_T *rettv);
318static void f_repeat(typval_T *argvars, typval_T *rettv);
319static void f_resolve(typval_T *argvars, typval_T *rettv);
320static void f_reverse(typval_T *argvars, typval_T *rettv);
321#ifdef FEAT_FLOAT
322static void f_round(typval_T *argvars, typval_T *rettv);
323#endif
324static void f_screenattr(typval_T *argvars, typval_T *rettv);
325static void f_screenchar(typval_T *argvars, typval_T *rettv);
326static void f_screencol(typval_T *argvars, typval_T *rettv);
327static void f_screenrow(typval_T *argvars, typval_T *rettv);
328static void f_search(typval_T *argvars, typval_T *rettv);
329static void f_searchdecl(typval_T *argvars, typval_T *rettv);
330static void f_searchpair(typval_T *argvars, typval_T *rettv);
331static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
332static void f_searchpos(typval_T *argvars, typval_T *rettv);
333static void f_server2client(typval_T *argvars, typval_T *rettv);
334static void f_serverlist(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200335static void f_setbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200336static void f_setbufvar(typval_T *argvars, typval_T *rettv);
337static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
338static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
339static void f_setfperm(typval_T *argvars, typval_T *rettv);
340static void f_setline(typval_T *argvars, typval_T *rettv);
341static void f_setloclist(typval_T *argvars, typval_T *rettv);
342static void f_setmatches(typval_T *argvars, typval_T *rettv);
343static void f_setpos(typval_T *argvars, typval_T *rettv);
344static void f_setqflist(typval_T *argvars, typval_T *rettv);
345static void f_setreg(typval_T *argvars, typval_T *rettv);
346static void f_settabvar(typval_T *argvars, typval_T *rettv);
347static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
348static void f_setwinvar(typval_T *argvars, typval_T *rettv);
349#ifdef FEAT_CRYPT
350static void f_sha256(typval_T *argvars, typval_T *rettv);
351#endif /* FEAT_CRYPT */
352static void f_shellescape(typval_T *argvars, typval_T *rettv);
353static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
354static void f_simplify(typval_T *argvars, typval_T *rettv);
355#ifdef FEAT_FLOAT
356static void f_sin(typval_T *argvars, typval_T *rettv);
357static void f_sinh(typval_T *argvars, typval_T *rettv);
358#endif
359static void f_sort(typval_T *argvars, typval_T *rettv);
360static void f_soundfold(typval_T *argvars, typval_T *rettv);
361static void f_spellbadword(typval_T *argvars, typval_T *rettv);
362static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
363static void f_split(typval_T *argvars, typval_T *rettv);
364#ifdef FEAT_FLOAT
365static void f_sqrt(typval_T *argvars, typval_T *rettv);
366static void f_str2float(typval_T *argvars, typval_T *rettv);
367#endif
368static void f_str2nr(typval_T *argvars, typval_T *rettv);
369static void f_strchars(typval_T *argvars, typval_T *rettv);
370#ifdef HAVE_STRFTIME
371static void f_strftime(typval_T *argvars, typval_T *rettv);
372#endif
373static void f_strgetchar(typval_T *argvars, typval_T *rettv);
374static void f_stridx(typval_T *argvars, typval_T *rettv);
375static void f_string(typval_T *argvars, typval_T *rettv);
376static void f_strlen(typval_T *argvars, typval_T *rettv);
377static void f_strcharpart(typval_T *argvars, typval_T *rettv);
378static void f_strpart(typval_T *argvars, typval_T *rettv);
379static void f_strridx(typval_T *argvars, typval_T *rettv);
380static void f_strtrans(typval_T *argvars, typval_T *rettv);
381static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
382static void f_strwidth(typval_T *argvars, typval_T *rettv);
383static void f_submatch(typval_T *argvars, typval_T *rettv);
384static void f_substitute(typval_T *argvars, typval_T *rettv);
385static void f_synID(typval_T *argvars, typval_T *rettv);
386static void f_synIDattr(typval_T *argvars, typval_T *rettv);
387static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
388static void f_synstack(typval_T *argvars, typval_T *rettv);
389static void f_synconcealed(typval_T *argvars, typval_T *rettv);
390static void f_system(typval_T *argvars, typval_T *rettv);
391static void f_systemlist(typval_T *argvars, typval_T *rettv);
392static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
393static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
394static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
395static void f_taglist(typval_T *argvars, typval_T *rettv);
396static void f_tagfiles(typval_T *argvars, typval_T *rettv);
397static void f_tempname(typval_T *argvars, typval_T *rettv);
398static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
399static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200400static void f_test_feedinput(typval_T *argvars, typval_T *rettv);
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100401static void f_test_override(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200402static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100403static void f_test_ignore_error(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200404#ifdef FEAT_JOB_CHANNEL
405static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
406#endif
407static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
408#ifdef FEAT_JOB_CHANNEL
409static void f_test_null_job(typval_T *argvars, typval_T *rettv);
410#endif
411static void f_test_null_list(typval_T *argvars, typval_T *rettv);
412static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
413static void f_test_null_string(typval_T *argvars, typval_T *rettv);
414static void f_test_settime(typval_T *argvars, typval_T *rettv);
415#ifdef FEAT_FLOAT
416static void f_tan(typval_T *argvars, typval_T *rettv);
417static void f_tanh(typval_T *argvars, typval_T *rettv);
418#endif
419#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200420static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200421static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200422static void f_timer_start(typval_T *argvars, typval_T *rettv);
423static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200424static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200425#endif
426static void f_tolower(typval_T *argvars, typval_T *rettv);
427static void f_toupper(typval_T *argvars, typval_T *rettv);
428static void f_tr(typval_T *argvars, typval_T *rettv);
429#ifdef FEAT_FLOAT
430static void f_trunc(typval_T *argvars, typval_T *rettv);
431#endif
432static void f_type(typval_T *argvars, typval_T *rettv);
433static void f_undofile(typval_T *argvars, typval_T *rettv);
434static void f_undotree(typval_T *argvars, typval_T *rettv);
435static void f_uniq(typval_T *argvars, typval_T *rettv);
436static void f_values(typval_T *argvars, typval_T *rettv);
437static void f_virtcol(typval_T *argvars, typval_T *rettv);
438static void f_visualmode(typval_T *argvars, typval_T *rettv);
439static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
440static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
441static void f_win_getid(typval_T *argvars, typval_T *rettv);
442static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
443static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
444static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100445static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200446static void f_winbufnr(typval_T *argvars, typval_T *rettv);
447static void f_wincol(typval_T *argvars, typval_T *rettv);
448static void f_winheight(typval_T *argvars, typval_T *rettv);
449static void f_winline(typval_T *argvars, typval_T *rettv);
450static void f_winnr(typval_T *argvars, typval_T *rettv);
451static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
452static void f_winrestview(typval_T *argvars, typval_T *rettv);
453static void f_winsaveview(typval_T *argvars, typval_T *rettv);
454static void f_winwidth(typval_T *argvars, typval_T *rettv);
455static void f_writefile(typval_T *argvars, typval_T *rettv);
456static void f_wordcount(typval_T *argvars, typval_T *rettv);
457static void f_xor(typval_T *argvars, typval_T *rettv);
458
459/*
460 * Array with names and number of arguments of all internal functions
461 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
462 */
463static struct fst
464{
465 char *f_name; /* function name */
466 char f_min_argc; /* minimal number of arguments */
467 char f_max_argc; /* maximal number of arguments */
468 void (*f_func)(typval_T *args, typval_T *rvar);
469 /* implementation of function */
470} functions[] =
471{
472#ifdef FEAT_FLOAT
473 {"abs", 1, 1, f_abs},
474 {"acos", 1, 1, f_acos}, /* WJMc */
475#endif
476 {"add", 2, 2, f_add},
477 {"and", 2, 2, f_and},
478 {"append", 2, 2, f_append},
479 {"argc", 0, 0, f_argc},
480 {"argidx", 0, 0, f_argidx},
481 {"arglistid", 0, 2, f_arglistid},
482 {"argv", 0, 1, f_argv},
483#ifdef FEAT_FLOAT
484 {"asin", 1, 1, f_asin}, /* WJMc */
485#endif
486 {"assert_equal", 2, 3, f_assert_equal},
487 {"assert_exception", 1, 2, f_assert_exception},
488 {"assert_fails", 1, 2, f_assert_fails},
489 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100490 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200491 {"assert_match", 2, 3, f_assert_match},
492 {"assert_notequal", 2, 3, f_assert_notequal},
493 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100494 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200495 {"assert_true", 1, 2, f_assert_true},
496#ifdef FEAT_FLOAT
497 {"atan", 1, 1, f_atan},
498 {"atan2", 2, 2, f_atan2},
499#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100500#ifdef FEAT_BEVAL
501 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100502# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100503 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100504# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100505#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200506 {"browse", 4, 4, f_browse},
507 {"browsedir", 2, 2, f_browsedir},
508 {"bufexists", 1, 1, f_bufexists},
509 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
510 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
511 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
512 {"buflisted", 1, 1, f_buflisted},
513 {"bufloaded", 1, 1, f_bufloaded},
514 {"bufname", 1, 1, f_bufname},
515 {"bufnr", 1, 2, f_bufnr},
516 {"bufwinid", 1, 1, f_bufwinid},
517 {"bufwinnr", 1, 1, f_bufwinnr},
518 {"byte2line", 1, 1, f_byte2line},
519 {"byteidx", 2, 2, f_byteidx},
520 {"byteidxcomp", 2, 2, f_byteidxcomp},
521 {"call", 2, 3, f_call},
522#ifdef FEAT_FLOAT
523 {"ceil", 1, 1, f_ceil},
524#endif
525#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100526 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200527 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200528 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200529 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
530 {"ch_evalraw", 2, 3, f_ch_evalraw},
531 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
532 {"ch_getjob", 1, 1, f_ch_getjob},
533 {"ch_info", 1, 1, f_ch_info},
534 {"ch_log", 1, 2, f_ch_log},
535 {"ch_logfile", 1, 2, f_ch_logfile},
536 {"ch_open", 1, 2, f_ch_open},
537 {"ch_read", 1, 2, f_ch_read},
538 {"ch_readraw", 1, 2, f_ch_readraw},
539 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
540 {"ch_sendraw", 2, 3, f_ch_sendraw},
541 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200542 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200543#endif
544 {"changenr", 0, 0, f_changenr},
545 {"char2nr", 1, 2, f_char2nr},
546 {"cindent", 1, 1, f_cindent},
547 {"clearmatches", 0, 0, f_clearmatches},
548 {"col", 1, 1, f_col},
549#if defined(FEAT_INS_EXPAND)
550 {"complete", 2, 2, f_complete},
551 {"complete_add", 1, 1, f_complete_add},
552 {"complete_check", 0, 0, f_complete_check},
553#endif
554 {"confirm", 1, 4, f_confirm},
555 {"copy", 1, 1, f_copy},
556#ifdef FEAT_FLOAT
557 {"cos", 1, 1, f_cos},
558 {"cosh", 1, 1, f_cosh},
559#endif
560 {"count", 2, 4, f_count},
561 {"cscope_connection",0,3, f_cscope_connection},
562 {"cursor", 1, 3, f_cursor},
563 {"deepcopy", 1, 2, f_deepcopy},
564 {"delete", 1, 2, f_delete},
565 {"did_filetype", 0, 0, f_did_filetype},
566 {"diff_filler", 1, 1, f_diff_filler},
567 {"diff_hlID", 2, 2, f_diff_hlID},
568 {"empty", 1, 1, f_empty},
569 {"escape", 2, 2, f_escape},
570 {"eval", 1, 1, f_eval},
571 {"eventhandler", 0, 0, f_eventhandler},
572 {"executable", 1, 1, f_executable},
573 {"execute", 1, 2, f_execute},
574 {"exepath", 1, 1, f_exepath},
575 {"exists", 1, 1, f_exists},
576#ifdef FEAT_FLOAT
577 {"exp", 1, 1, f_exp},
578#endif
579 {"expand", 1, 3, f_expand},
580 {"extend", 2, 3, f_extend},
581 {"feedkeys", 1, 2, f_feedkeys},
582 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
583 {"filereadable", 1, 1, f_filereadable},
584 {"filewritable", 1, 1, f_filewritable},
585 {"filter", 2, 2, f_filter},
586 {"finddir", 1, 3, f_finddir},
587 {"findfile", 1, 3, f_findfile},
588#ifdef FEAT_FLOAT
589 {"float2nr", 1, 1, f_float2nr},
590 {"floor", 1, 1, f_floor},
591 {"fmod", 2, 2, f_fmod},
592#endif
593 {"fnameescape", 1, 1, f_fnameescape},
594 {"fnamemodify", 2, 2, f_fnamemodify},
595 {"foldclosed", 1, 1, f_foldclosed},
596 {"foldclosedend", 1, 1, f_foldclosedend},
597 {"foldlevel", 1, 1, f_foldlevel},
598 {"foldtext", 0, 0, f_foldtext},
599 {"foldtextresult", 1, 1, f_foldtextresult},
600 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200601 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200602 {"function", 1, 3, f_function},
603 {"garbagecollect", 0, 1, f_garbagecollect},
604 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200605 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200606 {"getbufline", 2, 3, f_getbufline},
607 {"getbufvar", 2, 3, f_getbufvar},
608 {"getchar", 0, 1, f_getchar},
609 {"getcharmod", 0, 0, f_getcharmod},
610 {"getcharsearch", 0, 0, f_getcharsearch},
611 {"getcmdline", 0, 0, f_getcmdline},
612 {"getcmdpos", 0, 0, f_getcmdpos},
613 {"getcmdtype", 0, 0, f_getcmdtype},
614 {"getcmdwintype", 0, 0, f_getcmdwintype},
615#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200616 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200617#endif
618 {"getcurpos", 0, 0, f_getcurpos},
619 {"getcwd", 0, 2, f_getcwd},
620 {"getfontname", 0, 1, f_getfontname},
621 {"getfperm", 1, 1, f_getfperm},
622 {"getfsize", 1, 1, f_getfsize},
623 {"getftime", 1, 1, f_getftime},
624 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100625 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200626 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200627 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200628 {"getmatches", 0, 0, f_getmatches},
629 {"getpid", 0, 0, f_getpid},
630 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200631 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200632 {"getreg", 0, 3, f_getreg},
633 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200634 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200635 {"gettabvar", 2, 3, f_gettabvar},
636 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200637 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200638 {"getwinposx", 0, 0, f_getwinposx},
639 {"getwinposy", 0, 0, f_getwinposy},
640 {"getwinvar", 2, 3, f_getwinvar},
641 {"glob", 1, 4, f_glob},
642 {"glob2regpat", 1, 1, f_glob2regpat},
643 {"globpath", 2, 5, f_globpath},
644 {"has", 1, 1, f_has},
645 {"has_key", 2, 2, f_has_key},
646 {"haslocaldir", 0, 2, f_haslocaldir},
647 {"hasmapto", 1, 3, f_hasmapto},
648 {"highlightID", 1, 1, f_hlID}, /* obsolete */
649 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
650 {"histadd", 2, 2, f_histadd},
651 {"histdel", 1, 2, f_histdel},
652 {"histget", 1, 2, f_histget},
653 {"histnr", 1, 1, f_histnr},
654 {"hlID", 1, 1, f_hlID},
655 {"hlexists", 1, 1, f_hlexists},
656 {"hostname", 0, 0, f_hostname},
657 {"iconv", 3, 3, f_iconv},
658 {"indent", 1, 1, f_indent},
659 {"index", 2, 4, f_index},
660 {"input", 1, 3, f_input},
661 {"inputdialog", 1, 3, f_inputdialog},
662 {"inputlist", 1, 1, f_inputlist},
663 {"inputrestore", 0, 0, f_inputrestore},
664 {"inputsave", 0, 0, f_inputsave},
665 {"inputsecret", 1, 2, f_inputsecret},
666 {"insert", 2, 3, f_insert},
667 {"invert", 1, 1, f_invert},
668 {"isdirectory", 1, 1, f_isdirectory},
669 {"islocked", 1, 1, f_islocked},
670#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
671 {"isnan", 1, 1, f_isnan},
672#endif
673 {"items", 1, 1, f_items},
674#ifdef FEAT_JOB_CHANNEL
675 {"job_getchannel", 1, 1, f_job_getchannel},
676 {"job_info", 1, 1, f_job_info},
677 {"job_setoptions", 2, 2, f_job_setoptions},
678 {"job_start", 1, 2, f_job_start},
679 {"job_status", 1, 1, f_job_status},
680 {"job_stop", 1, 2, f_job_stop},
681#endif
682 {"join", 1, 2, f_join},
683 {"js_decode", 1, 1, f_js_decode},
684 {"js_encode", 1, 1, f_js_encode},
685 {"json_decode", 1, 1, f_json_decode},
686 {"json_encode", 1, 1, f_json_encode},
687 {"keys", 1, 1, f_keys},
688 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
689 {"len", 1, 1, f_len},
690 {"libcall", 3, 3, f_libcall},
691 {"libcallnr", 3, 3, f_libcallnr},
692 {"line", 1, 1, f_line},
693 {"line2byte", 1, 1, f_line2byte},
694 {"lispindent", 1, 1, f_lispindent},
695 {"localtime", 0, 0, f_localtime},
696#ifdef FEAT_FLOAT
697 {"log", 1, 1, f_log},
698 {"log10", 1, 1, f_log10},
699#endif
700#ifdef FEAT_LUA
701 {"luaeval", 1, 2, f_luaeval},
702#endif
703 {"map", 2, 2, f_map},
704 {"maparg", 1, 4, f_maparg},
705 {"mapcheck", 1, 3, f_mapcheck},
706 {"match", 2, 4, f_match},
707 {"matchadd", 2, 5, f_matchadd},
708 {"matchaddpos", 2, 5, f_matchaddpos},
709 {"matcharg", 1, 1, f_matcharg},
710 {"matchdelete", 1, 1, f_matchdelete},
711 {"matchend", 2, 4, f_matchend},
712 {"matchlist", 2, 4, f_matchlist},
713 {"matchstr", 2, 4, f_matchstr},
714 {"matchstrpos", 2, 4, f_matchstrpos},
715 {"max", 1, 1, f_max},
716 {"min", 1, 1, f_min},
717#ifdef vim_mkdir
718 {"mkdir", 1, 3, f_mkdir},
719#endif
720 {"mode", 0, 1, f_mode},
721#ifdef FEAT_MZSCHEME
722 {"mzeval", 1, 1, f_mzeval},
723#endif
724 {"nextnonblank", 1, 1, f_nextnonblank},
725 {"nr2char", 1, 2, f_nr2char},
726 {"or", 2, 2, f_or},
727 {"pathshorten", 1, 1, f_pathshorten},
728#ifdef FEAT_PERL
729 {"perleval", 1, 1, f_perleval},
730#endif
731#ifdef FEAT_FLOAT
732 {"pow", 2, 2, f_pow},
733#endif
734 {"prevnonblank", 1, 1, f_prevnonblank},
735 {"printf", 2, 19, f_printf},
736 {"pumvisible", 0, 0, f_pumvisible},
737#ifdef FEAT_PYTHON3
738 {"py3eval", 1, 1, f_py3eval},
739#endif
740#ifdef FEAT_PYTHON
741 {"pyeval", 1, 1, f_pyeval},
742#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100743#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
744 {"pyxeval", 1, 1, f_pyxeval},
745#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200746 {"range", 1, 3, f_range},
747 {"readfile", 1, 3, f_readfile},
748 {"reltime", 0, 2, f_reltime},
749#ifdef FEAT_FLOAT
750 {"reltimefloat", 1, 1, f_reltimefloat},
751#endif
752 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100753 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200754 {"remote_foreground", 1, 1, f_remote_foreground},
755 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100756 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200757 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100758 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200759 {"remove", 2, 3, f_remove},
760 {"rename", 2, 2, f_rename},
761 {"repeat", 2, 2, f_repeat},
762 {"resolve", 1, 1, f_resolve},
763 {"reverse", 1, 1, f_reverse},
764#ifdef FEAT_FLOAT
765 {"round", 1, 1, f_round},
766#endif
767 {"screenattr", 2, 2, f_screenattr},
768 {"screenchar", 2, 2, f_screenchar},
769 {"screencol", 0, 0, f_screencol},
770 {"screenrow", 0, 0, f_screenrow},
771 {"search", 1, 4, f_search},
772 {"searchdecl", 1, 3, f_searchdecl},
773 {"searchpair", 3, 7, f_searchpair},
774 {"searchpairpos", 3, 7, f_searchpairpos},
775 {"searchpos", 1, 4, f_searchpos},
776 {"server2client", 2, 2, f_server2client},
777 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200778 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200779 {"setbufvar", 3, 3, f_setbufvar},
780 {"setcharsearch", 1, 1, f_setcharsearch},
781 {"setcmdpos", 1, 1, f_setcmdpos},
782 {"setfperm", 2, 2, f_setfperm},
783 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200784 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200785 {"setmatches", 1, 1, f_setmatches},
786 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200787 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200788 {"setreg", 2, 3, f_setreg},
789 {"settabvar", 3, 3, f_settabvar},
790 {"settabwinvar", 4, 4, f_settabwinvar},
791 {"setwinvar", 3, 3, f_setwinvar},
792#ifdef FEAT_CRYPT
793 {"sha256", 1, 1, f_sha256},
794#endif
795 {"shellescape", 1, 2, f_shellescape},
796 {"shiftwidth", 0, 0, f_shiftwidth},
797 {"simplify", 1, 1, f_simplify},
798#ifdef FEAT_FLOAT
799 {"sin", 1, 1, f_sin},
800 {"sinh", 1, 1, f_sinh},
801#endif
802 {"sort", 1, 3, f_sort},
803 {"soundfold", 1, 1, f_soundfold},
804 {"spellbadword", 0, 1, f_spellbadword},
805 {"spellsuggest", 1, 3, f_spellsuggest},
806 {"split", 1, 3, f_split},
807#ifdef FEAT_FLOAT
808 {"sqrt", 1, 1, f_sqrt},
809 {"str2float", 1, 1, f_str2float},
810#endif
811 {"str2nr", 1, 2, f_str2nr},
812 {"strcharpart", 2, 3, f_strcharpart},
813 {"strchars", 1, 2, f_strchars},
814 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
815#ifdef HAVE_STRFTIME
816 {"strftime", 1, 2, f_strftime},
817#endif
818 {"strgetchar", 2, 2, f_strgetchar},
819 {"stridx", 2, 3, f_stridx},
820 {"string", 1, 1, f_string},
821 {"strlen", 1, 1, f_strlen},
822 {"strpart", 2, 3, f_strpart},
823 {"strridx", 2, 3, f_strridx},
824 {"strtrans", 1, 1, f_strtrans},
825 {"strwidth", 1, 1, f_strwidth},
826 {"submatch", 1, 2, f_submatch},
827 {"substitute", 4, 4, f_substitute},
828 {"synID", 3, 3, f_synID},
829 {"synIDattr", 2, 3, f_synIDattr},
830 {"synIDtrans", 1, 1, f_synIDtrans},
831 {"synconcealed", 2, 2, f_synconcealed},
832 {"synstack", 2, 2, f_synstack},
833 {"system", 1, 2, f_system},
834 {"systemlist", 1, 2, f_systemlist},
835 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
836 {"tabpagenr", 0, 1, f_tabpagenr},
837 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
838 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100839 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200840#ifdef FEAT_FLOAT
841 {"tan", 1, 1, f_tan},
842 {"tanh", 1, 1, f_tanh},
843#endif
844 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200845#ifdef FEAT_TERMINAL
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200846 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200847 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200848 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200849 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200850 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200851 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200852 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200853 {"term_getstatus", 1, 1, f_term_getstatus},
854 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200855 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200856 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200857 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200858 {"term_sendkeys", 2, 2, f_term_sendkeys},
859 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200860 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200861#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200862 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
863 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200864 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200865 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100866 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200867#ifdef FEAT_JOB_CHANNEL
868 {"test_null_channel", 0, 0, f_test_null_channel},
869#endif
870 {"test_null_dict", 0, 0, f_test_null_dict},
871#ifdef FEAT_JOB_CHANNEL
872 {"test_null_job", 0, 0, f_test_null_job},
873#endif
874 {"test_null_list", 0, 0, f_test_null_list},
875 {"test_null_partial", 0, 0, f_test_null_partial},
876 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100877 {"test_override", 2, 2, f_test_override},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200878 {"test_settime", 1, 1, f_test_settime},
879#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200880 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200881 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200882 {"timer_start", 2, 3, f_timer_start},
883 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200884 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200885#endif
886 {"tolower", 1, 1, f_tolower},
887 {"toupper", 1, 1, f_toupper},
888 {"tr", 3, 3, f_tr},
889#ifdef FEAT_FLOAT
890 {"trunc", 1, 1, f_trunc},
891#endif
892 {"type", 1, 1, f_type},
893 {"undofile", 1, 1, f_undofile},
894 {"undotree", 0, 0, f_undotree},
895 {"uniq", 1, 3, f_uniq},
896 {"values", 1, 1, f_values},
897 {"virtcol", 1, 1, f_virtcol},
898 {"visualmode", 0, 1, f_visualmode},
899 {"wildmenumode", 0, 0, f_wildmenumode},
900 {"win_findbuf", 1, 1, f_win_findbuf},
901 {"win_getid", 0, 2, f_win_getid},
902 {"win_gotoid", 1, 1, f_win_gotoid},
903 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
904 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100905 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200906 {"winbufnr", 1, 1, f_winbufnr},
907 {"wincol", 0, 0, f_wincol},
908 {"winheight", 1, 1, f_winheight},
909 {"winline", 0, 0, f_winline},
910 {"winnr", 0, 1, f_winnr},
911 {"winrestcmd", 0, 0, f_winrestcmd},
912 {"winrestview", 1, 1, f_winrestview},
913 {"winsaveview", 0, 0, f_winsaveview},
914 {"winwidth", 1, 1, f_winwidth},
915 {"wordcount", 0, 0, f_wordcount},
916 {"writefile", 2, 3, f_writefile},
917 {"xor", 2, 2, f_xor},
918};
919
920#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
921
922/*
923 * Function given to ExpandGeneric() to obtain the list of internal
924 * or user defined function names.
925 */
926 char_u *
927get_function_name(expand_T *xp, int idx)
928{
929 static int intidx = -1;
930 char_u *name;
931
932 if (idx == 0)
933 intidx = -1;
934 if (intidx < 0)
935 {
936 name = get_user_func_name(xp, idx);
937 if (name != NULL)
938 return name;
939 }
940 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
941 {
942 STRCPY(IObuff, functions[intidx].f_name);
943 STRCAT(IObuff, "(");
944 if (functions[intidx].f_max_argc == 0)
945 STRCAT(IObuff, ")");
946 return IObuff;
947 }
948
949 return NULL;
950}
951
952/*
953 * Function given to ExpandGeneric() to obtain the list of internal or
954 * user defined variable or function names.
955 */
956 char_u *
957get_expr_name(expand_T *xp, int idx)
958{
959 static int intidx = -1;
960 char_u *name;
961
962 if (idx == 0)
963 intidx = -1;
964 if (intidx < 0)
965 {
966 name = get_function_name(xp, idx);
967 if (name != NULL)
968 return name;
969 }
970 return get_user_var_name(xp, ++intidx);
971}
972
973#endif /* FEAT_CMDL_COMPL */
974
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200975/*
976 * Find internal function in table above.
977 * Return index, or -1 if not found
978 */
979 int
980find_internal_func(
981 char_u *name) /* name of the function */
982{
983 int first = 0;
984 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
985 int cmp;
986 int x;
987
988 /*
989 * Find the function name in the table. Binary search.
990 */
991 while (first <= last)
992 {
993 x = first + ((unsigned)(last - first) >> 1);
994 cmp = STRCMP(name, functions[x].f_name);
995 if (cmp < 0)
996 last = x - 1;
997 else if (cmp > 0)
998 first = x + 1;
999 else
1000 return x;
1001 }
1002 return -1;
1003}
1004
1005 int
1006call_internal_func(
1007 char_u *name,
1008 int argcount,
1009 typval_T *argvars,
1010 typval_T *rettv)
1011{
1012 int i;
1013
1014 i = find_internal_func(name);
1015 if (i < 0)
1016 return ERROR_UNKNOWN;
1017 if (argcount < functions[i].f_min_argc)
1018 return ERROR_TOOFEW;
1019 if (argcount > functions[i].f_max_argc)
1020 return ERROR_TOOMANY;
1021 argvars[argcount].v_type = VAR_UNKNOWN;
1022 functions[i].f_func(argvars, rettv);
1023 return ERROR_NONE;
1024}
1025
1026/*
1027 * Return TRUE for a non-zero Number and a non-empty String.
1028 */
1029 static int
1030non_zero_arg(typval_T *argvars)
1031{
1032 return ((argvars[0].v_type == VAR_NUMBER
1033 && argvars[0].vval.v_number != 0)
1034 || (argvars[0].v_type == VAR_SPECIAL
1035 && argvars[0].vval.v_number == VVAL_TRUE)
1036 || (argvars[0].v_type == VAR_STRING
1037 && argvars[0].vval.v_string != NULL
1038 && *argvars[0].vval.v_string != NUL));
1039}
1040
1041/*
1042 * Get the lnum from the first argument.
1043 * Also accepts ".", "$", etc., but that only works for the current buffer.
1044 * Returns -1 on error.
1045 */
1046 static linenr_T
1047get_tv_lnum(typval_T *argvars)
1048{
1049 typval_T rettv;
1050 linenr_T lnum;
1051
1052 lnum = (linenr_T)get_tv_number_chk(&argvars[0], NULL);
1053 if (lnum == 0) /* no valid number, try using line() */
1054 {
1055 rettv.v_type = VAR_NUMBER;
1056 f_line(argvars, &rettv);
1057 lnum = (linenr_T)rettv.vval.v_number;
1058 clear_tv(&rettv);
1059 }
1060 return lnum;
1061}
1062
1063#ifdef FEAT_FLOAT
1064static int get_float_arg(typval_T *argvars, float_T *f);
1065
1066/*
1067 * Get the float value of "argvars[0]" into "f".
1068 * Returns FAIL when the argument is not a Number or Float.
1069 */
1070 static int
1071get_float_arg(typval_T *argvars, float_T *f)
1072{
1073 if (argvars[0].v_type == VAR_FLOAT)
1074 {
1075 *f = argvars[0].vval.v_float;
1076 return OK;
1077 }
1078 if (argvars[0].v_type == VAR_NUMBER)
1079 {
1080 *f = (float_T)argvars[0].vval.v_number;
1081 return OK;
1082 }
1083 EMSG(_("E808: Number or Float required"));
1084 return FAIL;
1085}
1086
1087/*
1088 * "abs(expr)" function
1089 */
1090 static void
1091f_abs(typval_T *argvars, typval_T *rettv)
1092{
1093 if (argvars[0].v_type == VAR_FLOAT)
1094 {
1095 rettv->v_type = VAR_FLOAT;
1096 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1097 }
1098 else
1099 {
1100 varnumber_T n;
1101 int error = FALSE;
1102
1103 n = get_tv_number_chk(&argvars[0], &error);
1104 if (error)
1105 rettv->vval.v_number = -1;
1106 else if (n > 0)
1107 rettv->vval.v_number = n;
1108 else
1109 rettv->vval.v_number = -n;
1110 }
1111}
1112
1113/*
1114 * "acos()" function
1115 */
1116 static void
1117f_acos(typval_T *argvars, typval_T *rettv)
1118{
1119 float_T f = 0.0;
1120
1121 rettv->v_type = VAR_FLOAT;
1122 if (get_float_arg(argvars, &f) == OK)
1123 rettv->vval.v_float = acos(f);
1124 else
1125 rettv->vval.v_float = 0.0;
1126}
1127#endif
1128
1129/*
1130 * "add(list, item)" function
1131 */
1132 static void
1133f_add(typval_T *argvars, typval_T *rettv)
1134{
1135 list_T *l;
1136
1137 rettv->vval.v_number = 1; /* Default: Failed */
1138 if (argvars[0].v_type == VAR_LIST)
1139 {
1140 if ((l = argvars[0].vval.v_list) != NULL
1141 && !tv_check_lock(l->lv_lock,
1142 (char_u *)N_("add() argument"), TRUE)
1143 && list_append_tv(l, &argvars[1]) == OK)
1144 copy_tv(&argvars[0], rettv);
1145 }
1146 else
1147 EMSG(_(e_listreq));
1148}
1149
1150/*
1151 * "and(expr, expr)" function
1152 */
1153 static void
1154f_and(typval_T *argvars, typval_T *rettv)
1155{
1156 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
1157 & get_tv_number_chk(&argvars[1], NULL);
1158}
1159
1160/*
1161 * "append(lnum, string/list)" function
1162 */
1163 static void
1164f_append(typval_T *argvars, typval_T *rettv)
1165{
1166 long lnum;
1167 char_u *line;
1168 list_T *l = NULL;
1169 listitem_T *li = NULL;
1170 typval_T *tv;
1171 long added = 0;
1172
1173 /* When coming here from Insert mode, sync undo, so that this can be
1174 * undone separately from what was previously inserted. */
1175 if (u_sync_once == 2)
1176 {
1177 u_sync_once = 1; /* notify that u_sync() was called */
1178 u_sync(TRUE);
1179 }
1180
1181 lnum = get_tv_lnum(argvars);
1182 if (lnum >= 0
1183 && lnum <= curbuf->b_ml.ml_line_count
1184 && u_save(lnum, lnum + 1) == OK)
1185 {
1186 if (argvars[1].v_type == VAR_LIST)
1187 {
1188 l = argvars[1].vval.v_list;
1189 if (l == NULL)
1190 return;
1191 li = l->lv_first;
1192 }
1193 for (;;)
1194 {
1195 if (l == NULL)
1196 tv = &argvars[1]; /* append a string */
1197 else if (li == NULL)
1198 break; /* end of list */
1199 else
1200 tv = &li->li_tv; /* append item from list */
1201 line = get_tv_string_chk(tv);
1202 if (line == NULL) /* type error */
1203 {
1204 rettv->vval.v_number = 1; /* Failed */
1205 break;
1206 }
1207 ml_append(lnum + added, line, (colnr_T)0, FALSE);
1208 ++added;
1209 if (l == NULL)
1210 break;
1211 li = li->li_next;
1212 }
1213
1214 appended_lines_mark(lnum, added);
1215 if (curwin->w_cursor.lnum > lnum)
1216 curwin->w_cursor.lnum += added;
1217 }
1218 else
1219 rettv->vval.v_number = 1; /* Failed */
1220}
1221
1222/*
1223 * "argc()" function
1224 */
1225 static void
1226f_argc(typval_T *argvars UNUSED, typval_T *rettv)
1227{
1228 rettv->vval.v_number = ARGCOUNT;
1229}
1230
1231/*
1232 * "argidx()" function
1233 */
1234 static void
1235f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1236{
1237 rettv->vval.v_number = curwin->w_arg_idx;
1238}
1239
1240/*
1241 * "arglistid()" function
1242 */
1243 static void
1244f_arglistid(typval_T *argvars, typval_T *rettv)
1245{
1246 win_T *wp;
1247
1248 rettv->vval.v_number = -1;
1249 wp = find_tabwin(&argvars[0], &argvars[1]);
1250 if (wp != NULL)
1251 rettv->vval.v_number = wp->w_alist->id;
1252}
1253
1254/*
1255 * "argv(nr)" function
1256 */
1257 static void
1258f_argv(typval_T *argvars, typval_T *rettv)
1259{
1260 int idx;
1261
1262 if (argvars[0].v_type != VAR_UNKNOWN)
1263 {
1264 idx = (int)get_tv_number_chk(&argvars[0], NULL);
1265 if (idx >= 0 && idx < ARGCOUNT)
1266 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
1267 else
1268 rettv->vval.v_string = NULL;
1269 rettv->v_type = VAR_STRING;
1270 }
1271 else if (rettv_list_alloc(rettv) == OK)
1272 for (idx = 0; idx < ARGCOUNT; ++idx)
1273 list_append_string(rettv->vval.v_list,
1274 alist_name(&ARGLIST[idx]), -1);
1275}
1276
1277/*
1278 * "assert_equal(expected, actual[, msg])" function
1279 */
1280 static void
1281f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
1282{
1283 assert_equal_common(argvars, ASSERT_EQUAL);
1284}
1285
1286/*
1287 * "assert_notequal(expected, actual[, msg])" function
1288 */
1289 static void
1290f_assert_notequal(typval_T *argvars, typval_T *rettv UNUSED)
1291{
1292 assert_equal_common(argvars, ASSERT_NOTEQUAL);
1293}
1294
1295/*
1296 * "assert_exception(string[, msg])" function
1297 */
1298 static void
1299f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
1300{
1301 assert_exception(argvars);
1302}
1303
1304/*
1305 * "assert_fails(cmd [, error])" function
1306 */
1307 static void
1308f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
1309{
1310 assert_fails(argvars);
1311}
1312
1313/*
1314 * "assert_false(actual[, msg])" function
1315 */
1316 static void
1317f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
1318{
1319 assert_bool(argvars, FALSE);
1320}
1321
1322/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001323 * "assert_inrange(lower, upper[, msg])" function
1324 */
1325 static void
1326f_assert_inrange(typval_T *argvars, typval_T *rettv UNUSED)
1327{
1328 assert_inrange(argvars);
1329}
1330
1331/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001332 * "assert_match(pattern, actual[, msg])" function
1333 */
1334 static void
1335f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
1336{
1337 assert_match_common(argvars, ASSERT_MATCH);
1338}
1339
1340/*
1341 * "assert_notmatch(pattern, actual[, msg])" function
1342 */
1343 static void
1344f_assert_notmatch(typval_T *argvars, typval_T *rettv UNUSED)
1345{
1346 assert_match_common(argvars, ASSERT_NOTMATCH);
1347}
1348
1349/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001350 * "assert_report(msg)" function
1351 */
1352 static void
1353f_assert_report(typval_T *argvars, typval_T *rettv UNUSED)
1354{
1355 assert_report(argvars);
1356}
1357
1358/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001359 * "assert_true(actual[, msg])" function
1360 */
1361 static void
1362f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
1363{
1364 assert_bool(argvars, TRUE);
1365}
1366
1367#ifdef FEAT_FLOAT
1368/*
1369 * "asin()" function
1370 */
1371 static void
1372f_asin(typval_T *argvars, typval_T *rettv)
1373{
1374 float_T f = 0.0;
1375
1376 rettv->v_type = VAR_FLOAT;
1377 if (get_float_arg(argvars, &f) == OK)
1378 rettv->vval.v_float = asin(f);
1379 else
1380 rettv->vval.v_float = 0.0;
1381}
1382
1383/*
1384 * "atan()" function
1385 */
1386 static void
1387f_atan(typval_T *argvars, typval_T *rettv)
1388{
1389 float_T f = 0.0;
1390
1391 rettv->v_type = VAR_FLOAT;
1392 if (get_float_arg(argvars, &f) == OK)
1393 rettv->vval.v_float = atan(f);
1394 else
1395 rettv->vval.v_float = 0.0;
1396}
1397
1398/*
1399 * "atan2()" function
1400 */
1401 static void
1402f_atan2(typval_T *argvars, typval_T *rettv)
1403{
1404 float_T fx = 0.0, fy = 0.0;
1405
1406 rettv->v_type = VAR_FLOAT;
1407 if (get_float_arg(argvars, &fx) == OK
1408 && get_float_arg(&argvars[1], &fy) == OK)
1409 rettv->vval.v_float = atan2(fx, fy);
1410 else
1411 rettv->vval.v_float = 0.0;
1412}
1413#endif
1414
1415/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001416 * "balloon_show()" function
1417 */
1418#ifdef FEAT_BEVAL
1419 static void
1420f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1421{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001422 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001423 {
1424 if (argvars[0].v_type == VAR_LIST
1425# ifdef FEAT_GUI
1426 && !gui.in_use
1427# endif
1428 )
1429 post_balloon(balloonEval, NULL, argvars[0].vval.v_list);
1430 else
1431 post_balloon(balloonEval, get_tv_string_chk(&argvars[0]), NULL);
1432 }
1433}
1434
Bram Moolenaar669a8282017-11-19 20:13:05 +01001435# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001436 static void
1437f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1438{
1439 if (rettv_list_alloc(rettv) == OK)
1440 {
1441 char_u *msg = get_tv_string_chk(&argvars[0]);
1442
1443 if (msg != NULL)
1444 {
1445 pumitem_T *array;
1446 int size = split_message(msg, &array);
1447 int i;
1448
1449 /* Skip the first and last item, they are always empty. */
1450 for (i = 1; i < size - 1; ++i)
1451 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001452 while (size > 0)
1453 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001454 vim_free(array);
1455 }
1456 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001457}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001458# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001459#endif
1460
1461/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001462 * "browse(save, title, initdir, default)" function
1463 */
1464 static void
1465f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1466{
1467#ifdef FEAT_BROWSE
1468 int save;
1469 char_u *title;
1470 char_u *initdir;
1471 char_u *defname;
1472 char_u buf[NUMBUFLEN];
1473 char_u buf2[NUMBUFLEN];
1474 int error = FALSE;
1475
1476 save = (int)get_tv_number_chk(&argvars[0], &error);
1477 title = get_tv_string_chk(&argvars[1]);
1478 initdir = get_tv_string_buf_chk(&argvars[2], buf);
1479 defname = get_tv_string_buf_chk(&argvars[3], buf2);
1480
1481 if (error || title == NULL || initdir == NULL || defname == NULL)
1482 rettv->vval.v_string = NULL;
1483 else
1484 rettv->vval.v_string =
1485 do_browse(save ? BROWSE_SAVE : 0,
1486 title, defname, NULL, initdir, NULL, curbuf);
1487#else
1488 rettv->vval.v_string = NULL;
1489#endif
1490 rettv->v_type = VAR_STRING;
1491}
1492
1493/*
1494 * "browsedir(title, initdir)" function
1495 */
1496 static void
1497f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1498{
1499#ifdef FEAT_BROWSE
1500 char_u *title;
1501 char_u *initdir;
1502 char_u buf[NUMBUFLEN];
1503
1504 title = get_tv_string_chk(&argvars[0]);
1505 initdir = get_tv_string_buf_chk(&argvars[1], buf);
1506
1507 if (title == NULL || initdir == NULL)
1508 rettv->vval.v_string = NULL;
1509 else
1510 rettv->vval.v_string = do_browse(BROWSE_DIR,
1511 title, NULL, NULL, initdir, NULL, curbuf);
1512#else
1513 rettv->vval.v_string = NULL;
1514#endif
1515 rettv->v_type = VAR_STRING;
1516}
1517
1518static buf_T *find_buffer(typval_T *avar);
1519
1520/*
1521 * Find a buffer by number or exact name.
1522 */
1523 static buf_T *
1524find_buffer(typval_T *avar)
1525{
1526 buf_T *buf = NULL;
1527
1528 if (avar->v_type == VAR_NUMBER)
1529 buf = buflist_findnr((int)avar->vval.v_number);
1530 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1531 {
1532 buf = buflist_findname_exp(avar->vval.v_string);
1533 if (buf == NULL)
1534 {
1535 /* No full path name match, try a match with a URL or a "nofile"
1536 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001537 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001538 if (buf->b_fname != NULL
1539 && (path_with_url(buf->b_fname)
1540#ifdef FEAT_QUICKFIX
1541 || bt_nofile(buf)
1542#endif
1543 )
1544 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1545 break;
1546 }
1547 }
1548 return buf;
1549}
1550
1551/*
1552 * "bufexists(expr)" function
1553 */
1554 static void
1555f_bufexists(typval_T *argvars, typval_T *rettv)
1556{
1557 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1558}
1559
1560/*
1561 * "buflisted(expr)" function
1562 */
1563 static void
1564f_buflisted(typval_T *argvars, typval_T *rettv)
1565{
1566 buf_T *buf;
1567
1568 buf = find_buffer(&argvars[0]);
1569 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1570}
1571
1572/*
1573 * "bufloaded(expr)" function
1574 */
1575 static void
1576f_bufloaded(typval_T *argvars, typval_T *rettv)
1577{
1578 buf_T *buf;
1579
1580 buf = find_buffer(&argvars[0]);
1581 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1582}
1583
1584 buf_T *
1585buflist_find_by_name(char_u *name, int curtab_only)
1586{
1587 int save_magic;
1588 char_u *save_cpo;
1589 buf_T *buf;
1590
1591 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1592 save_magic = p_magic;
1593 p_magic = TRUE;
1594 save_cpo = p_cpo;
1595 p_cpo = (char_u *)"";
1596
1597 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1598 TRUE, FALSE, curtab_only));
1599
1600 p_magic = save_magic;
1601 p_cpo = save_cpo;
1602 return buf;
1603}
1604
1605/*
1606 * Get buffer by number or pattern.
1607 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001608 buf_T *
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001609get_buf_tv(typval_T *tv, int curtab_only)
1610{
1611 char_u *name = tv->vval.v_string;
1612 buf_T *buf;
1613
1614 if (tv->v_type == VAR_NUMBER)
1615 return buflist_findnr((int)tv->vval.v_number);
1616 if (tv->v_type != VAR_STRING)
1617 return NULL;
1618 if (name == NULL || *name == NUL)
1619 return curbuf;
1620 if (name[0] == '$' && name[1] == NUL)
1621 return lastbuf;
1622
1623 buf = buflist_find_by_name(name, curtab_only);
1624
1625 /* If not found, try expanding the name, like done for bufexists(). */
1626 if (buf == NULL)
1627 buf = find_buffer(tv);
1628
1629 return buf;
1630}
1631
1632/*
1633 * "bufname(expr)" function
1634 */
1635 static void
1636f_bufname(typval_T *argvars, typval_T *rettv)
1637{
1638 buf_T *buf;
1639
1640 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1641 ++emsg_off;
1642 buf = get_buf_tv(&argvars[0], FALSE);
1643 rettv->v_type = VAR_STRING;
1644 if (buf != NULL && buf->b_fname != NULL)
1645 rettv->vval.v_string = vim_strsave(buf->b_fname);
1646 else
1647 rettv->vval.v_string = NULL;
1648 --emsg_off;
1649}
1650
1651/*
1652 * "bufnr(expr)" function
1653 */
1654 static void
1655f_bufnr(typval_T *argvars, typval_T *rettv)
1656{
1657 buf_T *buf;
1658 int error = FALSE;
1659 char_u *name;
1660
1661 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1662 ++emsg_off;
1663 buf = get_buf_tv(&argvars[0], FALSE);
1664 --emsg_off;
1665
1666 /* If the buffer isn't found and the second argument is not zero create a
1667 * new buffer. */
1668 if (buf == NULL
1669 && argvars[1].v_type != VAR_UNKNOWN
1670 && get_tv_number_chk(&argvars[1], &error) != 0
1671 && !error
1672 && (name = get_tv_string_chk(&argvars[0])) != NULL
1673 && !error)
1674 buf = buflist_new(name, NULL, (linenr_T)1, 0);
1675
1676 if (buf != NULL)
1677 rettv->vval.v_number = buf->b_fnum;
1678 else
1679 rettv->vval.v_number = -1;
1680}
1681
1682 static void
1683buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
1684{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001685 win_T *wp;
1686 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001687 buf_T *buf;
1688
1689 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1690 ++emsg_off;
1691 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02001692 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001693 {
1694 ++winnr;
1695 if (wp->w_buffer == buf)
1696 break;
1697 }
1698 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001699 --emsg_off;
1700}
1701
1702/*
1703 * "bufwinid(nr)" function
1704 */
1705 static void
1706f_bufwinid(typval_T *argvars, typval_T *rettv)
1707{
1708 buf_win_common(argvars, rettv, FALSE);
1709}
1710
1711/*
1712 * "bufwinnr(nr)" function
1713 */
1714 static void
1715f_bufwinnr(typval_T *argvars, typval_T *rettv)
1716{
1717 buf_win_common(argvars, rettv, TRUE);
1718}
1719
1720/*
1721 * "byte2line(byte)" function
1722 */
1723 static void
1724f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
1725{
1726#ifndef FEAT_BYTEOFF
1727 rettv->vval.v_number = -1;
1728#else
1729 long boff = 0;
1730
1731 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
1732 if (boff < 0)
1733 rettv->vval.v_number = -1;
1734 else
1735 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
1736 (linenr_T)0, &boff);
1737#endif
1738}
1739
1740 static void
1741byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
1742{
1743#ifdef FEAT_MBYTE
1744 char_u *t;
1745#endif
1746 char_u *str;
1747 varnumber_T idx;
1748
1749 str = get_tv_string_chk(&argvars[0]);
1750 idx = get_tv_number_chk(&argvars[1], NULL);
1751 rettv->vval.v_number = -1;
1752 if (str == NULL || idx < 0)
1753 return;
1754
1755#ifdef FEAT_MBYTE
1756 t = str;
1757 for ( ; idx > 0; idx--)
1758 {
1759 if (*t == NUL) /* EOL reached */
1760 return;
1761 if (enc_utf8 && comp)
1762 t += utf_ptr2len(t);
1763 else
1764 t += (*mb_ptr2len)(t);
1765 }
1766 rettv->vval.v_number = (varnumber_T)(t - str);
1767#else
1768 if ((size_t)idx <= STRLEN(str))
1769 rettv->vval.v_number = idx;
1770#endif
1771}
1772
1773/*
1774 * "byteidx()" function
1775 */
1776 static void
1777f_byteidx(typval_T *argvars, typval_T *rettv)
1778{
1779 byteidx(argvars, rettv, FALSE);
1780}
1781
1782/*
1783 * "byteidxcomp()" function
1784 */
1785 static void
1786f_byteidxcomp(typval_T *argvars, typval_T *rettv)
1787{
1788 byteidx(argvars, rettv, TRUE);
1789}
1790
1791/*
1792 * "call(func, arglist [, dict])" function
1793 */
1794 static void
1795f_call(typval_T *argvars, typval_T *rettv)
1796{
1797 char_u *func;
1798 partial_T *partial = NULL;
1799 dict_T *selfdict = NULL;
1800
1801 if (argvars[1].v_type != VAR_LIST)
1802 {
1803 EMSG(_(e_listreq));
1804 return;
1805 }
1806 if (argvars[1].vval.v_list == NULL)
1807 return;
1808
1809 if (argvars[0].v_type == VAR_FUNC)
1810 func = argvars[0].vval.v_string;
1811 else if (argvars[0].v_type == VAR_PARTIAL)
1812 {
1813 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001814 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001815 }
1816 else
1817 func = get_tv_string(&argvars[0]);
1818 if (*func == NUL)
1819 return; /* type error or empty name */
1820
1821 if (argvars[2].v_type != VAR_UNKNOWN)
1822 {
1823 if (argvars[2].v_type != VAR_DICT)
1824 {
1825 EMSG(_(e_dictreq));
1826 return;
1827 }
1828 selfdict = argvars[2].vval.v_dict;
1829 }
1830
1831 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
1832}
1833
1834#ifdef FEAT_FLOAT
1835/*
1836 * "ceil({float})" function
1837 */
1838 static void
1839f_ceil(typval_T *argvars, typval_T *rettv)
1840{
1841 float_T f = 0.0;
1842
1843 rettv->v_type = VAR_FLOAT;
1844 if (get_float_arg(argvars, &f) == OK)
1845 rettv->vval.v_float = ceil(f);
1846 else
1847 rettv->vval.v_float = 0.0;
1848}
1849#endif
1850
1851#ifdef FEAT_JOB_CHANNEL
1852/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01001853 * "ch_canread()" function
1854 */
1855 static void
1856f_ch_canread(typval_T *argvars, typval_T *rettv)
1857{
Bram Moolenaar958dc692016-12-01 15:34:12 +01001858 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01001859
1860 rettv->vval.v_number = 0;
1861 if (channel != NULL)
1862 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
1863 || channel_has_readahead(channel, PART_OUT)
1864 || channel_has_readahead(channel, PART_ERR);
1865}
1866
1867/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001868 * "ch_close()" function
1869 */
1870 static void
1871f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
1872{
1873 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
1874
1875 if (channel != NULL)
1876 {
1877 channel_close(channel, FALSE);
1878 channel_clear(channel);
1879 }
1880}
1881
1882/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02001883 * "ch_close()" function
1884 */
1885 static void
1886f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
1887{
1888 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
1889
1890 if (channel != NULL)
1891 channel_close_in(channel);
1892}
1893
1894/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001895 * "ch_getbufnr()" function
1896 */
1897 static void
1898f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
1899{
1900 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1901
1902 rettv->vval.v_number = -1;
1903 if (channel != NULL)
1904 {
1905 char_u *what = get_tv_string(&argvars[1]);
1906 int part;
1907
1908 if (STRCMP(what, "err") == 0)
1909 part = PART_ERR;
1910 else if (STRCMP(what, "out") == 0)
1911 part = PART_OUT;
1912 else if (STRCMP(what, "in") == 0)
1913 part = PART_IN;
1914 else
1915 part = PART_SOCK;
1916 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
1917 rettv->vval.v_number =
1918 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
1919 }
1920}
1921
1922/*
1923 * "ch_getjob()" function
1924 */
1925 static void
1926f_ch_getjob(typval_T *argvars, typval_T *rettv)
1927{
1928 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1929
1930 if (channel != NULL)
1931 {
1932 rettv->v_type = VAR_JOB;
1933 rettv->vval.v_job = channel->ch_job;
1934 if (channel->ch_job != NULL)
1935 ++channel->ch_job->jv_refcount;
1936 }
1937}
1938
1939/*
1940 * "ch_info()" function
1941 */
1942 static void
1943f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
1944{
1945 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1946
1947 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
1948 channel_info(channel, rettv->vval.v_dict);
1949}
1950
1951/*
1952 * "ch_log()" function
1953 */
1954 static void
1955f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
1956{
1957 char_u *msg = get_tv_string(&argvars[0]);
1958 channel_T *channel = NULL;
1959
1960 if (argvars[1].v_type != VAR_UNKNOWN)
1961 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
1962
1963 ch_log(channel, (char *)msg);
1964}
1965
1966/*
1967 * "ch_logfile()" function
1968 */
1969 static void
1970f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
1971{
1972 char_u *fname;
1973 char_u *opt = (char_u *)"";
1974 char_u buf[NUMBUFLEN];
1975
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02001976 /* Don't open a file in restricted mode. */
1977 if (check_restricted() || check_secure())
1978 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001979 fname = get_tv_string(&argvars[0]);
1980 if (argvars[1].v_type == VAR_STRING)
1981 opt = get_tv_string_buf(&argvars[1], buf);
1982 ch_logfile(fname, opt);
1983}
1984
1985/*
1986 * "ch_open()" function
1987 */
1988 static void
1989f_ch_open(typval_T *argvars, typval_T *rettv)
1990{
1991 rettv->v_type = VAR_CHANNEL;
1992 if (check_restricted() || check_secure())
1993 return;
1994 rettv->vval.v_channel = channel_open_func(argvars);
1995}
1996
1997/*
1998 * "ch_read()" function
1999 */
2000 static void
2001f_ch_read(typval_T *argvars, typval_T *rettv)
2002{
2003 common_channel_read(argvars, rettv, FALSE);
2004}
2005
2006/*
2007 * "ch_readraw()" function
2008 */
2009 static void
2010f_ch_readraw(typval_T *argvars, typval_T *rettv)
2011{
2012 common_channel_read(argvars, rettv, TRUE);
2013}
2014
2015/*
2016 * "ch_evalexpr()" function
2017 */
2018 static void
2019f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2020{
2021 ch_expr_common(argvars, rettv, TRUE);
2022}
2023
2024/*
2025 * "ch_sendexpr()" function
2026 */
2027 static void
2028f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2029{
2030 ch_expr_common(argvars, rettv, FALSE);
2031}
2032
2033/*
2034 * "ch_evalraw()" function
2035 */
2036 static void
2037f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2038{
2039 ch_raw_common(argvars, rettv, TRUE);
2040}
2041
2042/*
2043 * "ch_sendraw()" function
2044 */
2045 static void
2046f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2047{
2048 ch_raw_common(argvars, rettv, FALSE);
2049}
2050
2051/*
2052 * "ch_setoptions()" function
2053 */
2054 static void
2055f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2056{
2057 channel_T *channel;
2058 jobopt_T opt;
2059
2060 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2061 if (channel == NULL)
2062 return;
2063 clear_job_options(&opt);
2064 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002065 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002066 channel_set_options(channel, &opt);
2067 free_job_options(&opt);
2068}
2069
2070/*
2071 * "ch_status()" function
2072 */
2073 static void
2074f_ch_status(typval_T *argvars, typval_T *rettv)
2075{
2076 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002077 jobopt_T opt;
2078 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002079
2080 /* return an empty string by default */
2081 rettv->v_type = VAR_STRING;
2082 rettv->vval.v_string = NULL;
2083
2084 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002085
2086 if (argvars[1].v_type != VAR_UNKNOWN)
2087 {
2088 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002089 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002090 && (opt.jo_set & JO_PART))
2091 part = opt.jo_part;
2092 }
2093
2094 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002095}
2096#endif
2097
2098/*
2099 * "changenr()" function
2100 */
2101 static void
2102f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2103{
2104 rettv->vval.v_number = curbuf->b_u_seq_cur;
2105}
2106
2107/*
2108 * "char2nr(string)" function
2109 */
2110 static void
2111f_char2nr(typval_T *argvars, typval_T *rettv)
2112{
2113#ifdef FEAT_MBYTE
2114 if (has_mbyte)
2115 {
2116 int utf8 = 0;
2117
2118 if (argvars[1].v_type != VAR_UNKNOWN)
2119 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
2120
2121 if (utf8)
2122 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
2123 else
2124 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
2125 }
2126 else
2127#endif
2128 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
2129}
2130
2131/*
2132 * "cindent(lnum)" function
2133 */
2134 static void
2135f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2136{
2137#ifdef FEAT_CINDENT
2138 pos_T pos;
2139 linenr_T lnum;
2140
2141 pos = curwin->w_cursor;
2142 lnum = get_tv_lnum(argvars);
2143 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2144 {
2145 curwin->w_cursor.lnum = lnum;
2146 rettv->vval.v_number = get_c_indent();
2147 curwin->w_cursor = pos;
2148 }
2149 else
2150#endif
2151 rettv->vval.v_number = -1;
2152}
2153
2154/*
2155 * "clearmatches()" function
2156 */
2157 static void
2158f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2159{
2160#ifdef FEAT_SEARCH_EXTRA
2161 clear_matches(curwin);
2162#endif
2163}
2164
2165/*
2166 * "col(string)" function
2167 */
2168 static void
2169f_col(typval_T *argvars, typval_T *rettv)
2170{
2171 colnr_T col = 0;
2172 pos_T *fp;
2173 int fnum = curbuf->b_fnum;
2174
2175 fp = var2fpos(&argvars[0], FALSE, &fnum);
2176 if (fp != NULL && fnum == curbuf->b_fnum)
2177 {
2178 if (fp->col == MAXCOL)
2179 {
2180 /* '> can be MAXCOL, get the length of the line then */
2181 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2182 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2183 else
2184 col = MAXCOL;
2185 }
2186 else
2187 {
2188 col = fp->col + 1;
2189#ifdef FEAT_VIRTUALEDIT
2190 /* col(".") when the cursor is on the NUL at the end of the line
2191 * because of "coladd" can be seen as an extra column. */
2192 if (virtual_active() && fp == &curwin->w_cursor)
2193 {
2194 char_u *p = ml_get_cursor();
2195
2196 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2197 curwin->w_virtcol - curwin->w_cursor.coladd))
2198 {
2199# ifdef FEAT_MBYTE
2200 int l;
2201
2202 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2203 col += l;
2204# else
2205 if (*p != NUL && p[1] == NUL)
2206 ++col;
2207# endif
2208 }
2209 }
2210#endif
2211 }
2212 }
2213 rettv->vval.v_number = col;
2214}
2215
2216#if defined(FEAT_INS_EXPAND)
2217/*
2218 * "complete()" function
2219 */
2220 static void
2221f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2222{
2223 int startcol;
2224
2225 if ((State & INSERT) == 0)
2226 {
2227 EMSG(_("E785: complete() can only be used in Insert mode"));
2228 return;
2229 }
2230
2231 /* Check for undo allowed here, because if something was already inserted
2232 * the line was already saved for undo and this check isn't done. */
2233 if (!undo_allowed())
2234 return;
2235
2236 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2237 {
2238 EMSG(_(e_invarg));
2239 return;
2240 }
2241
2242 startcol = (int)get_tv_number_chk(&argvars[0], NULL);
2243 if (startcol <= 0)
2244 return;
2245
2246 set_completion(startcol - 1, argvars[1].vval.v_list);
2247}
2248
2249/*
2250 * "complete_add()" function
2251 */
2252 static void
2253f_complete_add(typval_T *argvars, typval_T *rettv)
2254{
2255 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2256}
2257
2258/*
2259 * "complete_check()" function
2260 */
2261 static void
2262f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2263{
2264 int saved = RedrawingDisabled;
2265
2266 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002267 ins_compl_check_keys(0, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002268 rettv->vval.v_number = compl_interrupted;
2269 RedrawingDisabled = saved;
2270}
2271#endif
2272
2273/*
2274 * "confirm(message, buttons[, default [, type]])" function
2275 */
2276 static void
2277f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2278{
2279#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2280 char_u *message;
2281 char_u *buttons = NULL;
2282 char_u buf[NUMBUFLEN];
2283 char_u buf2[NUMBUFLEN];
2284 int def = 1;
2285 int type = VIM_GENERIC;
2286 char_u *typestr;
2287 int error = FALSE;
2288
2289 message = get_tv_string_chk(&argvars[0]);
2290 if (message == NULL)
2291 error = TRUE;
2292 if (argvars[1].v_type != VAR_UNKNOWN)
2293 {
2294 buttons = get_tv_string_buf_chk(&argvars[1], buf);
2295 if (buttons == NULL)
2296 error = TRUE;
2297 if (argvars[2].v_type != VAR_UNKNOWN)
2298 {
2299 def = (int)get_tv_number_chk(&argvars[2], &error);
2300 if (argvars[3].v_type != VAR_UNKNOWN)
2301 {
2302 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
2303 if (typestr == NULL)
2304 error = TRUE;
2305 else
2306 {
2307 switch (TOUPPER_ASC(*typestr))
2308 {
2309 case 'E': type = VIM_ERROR; break;
2310 case 'Q': type = VIM_QUESTION; break;
2311 case 'I': type = VIM_INFO; break;
2312 case 'W': type = VIM_WARNING; break;
2313 case 'G': type = VIM_GENERIC; break;
2314 }
2315 }
2316 }
2317 }
2318 }
2319
2320 if (buttons == NULL || *buttons == NUL)
2321 buttons = (char_u *)_("&Ok");
2322
2323 if (!error)
2324 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2325 def, NULL, FALSE);
2326#endif
2327}
2328
2329/*
2330 * "copy()" function
2331 */
2332 static void
2333f_copy(typval_T *argvars, typval_T *rettv)
2334{
2335 item_copy(&argvars[0], rettv, FALSE, 0);
2336}
2337
2338#ifdef FEAT_FLOAT
2339/*
2340 * "cos()" function
2341 */
2342 static void
2343f_cos(typval_T *argvars, typval_T *rettv)
2344{
2345 float_T f = 0.0;
2346
2347 rettv->v_type = VAR_FLOAT;
2348 if (get_float_arg(argvars, &f) == OK)
2349 rettv->vval.v_float = cos(f);
2350 else
2351 rettv->vval.v_float = 0.0;
2352}
2353
2354/*
2355 * "cosh()" function
2356 */
2357 static void
2358f_cosh(typval_T *argvars, typval_T *rettv)
2359{
2360 float_T f = 0.0;
2361
2362 rettv->v_type = VAR_FLOAT;
2363 if (get_float_arg(argvars, &f) == OK)
2364 rettv->vval.v_float = cosh(f);
2365 else
2366 rettv->vval.v_float = 0.0;
2367}
2368#endif
2369
2370/*
2371 * "count()" function
2372 */
2373 static void
2374f_count(typval_T *argvars, typval_T *rettv)
2375{
2376 long n = 0;
2377 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002378 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002379
Bram Moolenaar9966b212017-07-28 16:46:57 +02002380 if (argvars[2].v_type != VAR_UNKNOWN)
2381 ic = (int)get_tv_number_chk(&argvars[2], &error);
2382
2383 if (argvars[0].v_type == VAR_STRING)
2384 {
2385 char_u *expr = get_tv_string_chk(&argvars[1]);
2386 char_u *p = argvars[0].vval.v_string;
2387 char_u *next;
2388
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002389 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002390 {
2391 if (ic)
2392 {
2393 size_t len = STRLEN(expr);
2394
2395 while (*p != NUL)
2396 {
2397 if (MB_STRNICMP(p, expr, len) == 0)
2398 {
2399 ++n;
2400 p += len;
2401 }
2402 else
2403 MB_PTR_ADV(p);
2404 }
2405 }
2406 else
2407 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2408 != NULL)
2409 {
2410 ++n;
2411 p = next + STRLEN(expr);
2412 }
2413 }
2414
2415 }
2416 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002417 {
2418 listitem_T *li;
2419 list_T *l;
2420 long idx;
2421
2422 if ((l = argvars[0].vval.v_list) != NULL)
2423 {
2424 li = l->lv_first;
2425 if (argvars[2].v_type != VAR_UNKNOWN)
2426 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002427 if (argvars[3].v_type != VAR_UNKNOWN)
2428 {
2429 idx = (long)get_tv_number_chk(&argvars[3], &error);
2430 if (!error)
2431 {
2432 li = list_find(l, idx);
2433 if (li == NULL)
2434 EMSGN(_(e_listidx), idx);
2435 }
2436 }
2437 if (error)
2438 li = NULL;
2439 }
2440
2441 for ( ; li != NULL; li = li->li_next)
2442 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2443 ++n;
2444 }
2445 }
2446 else if (argvars[0].v_type == VAR_DICT)
2447 {
2448 int todo;
2449 dict_T *d;
2450 hashitem_T *hi;
2451
2452 if ((d = argvars[0].vval.v_dict) != NULL)
2453 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002454 if (argvars[2].v_type != VAR_UNKNOWN)
2455 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002456 if (argvars[3].v_type != VAR_UNKNOWN)
2457 EMSG(_(e_invarg));
2458 }
2459
2460 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2461 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2462 {
2463 if (!HASHITEM_EMPTY(hi))
2464 {
2465 --todo;
2466 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2467 ++n;
2468 }
2469 }
2470 }
2471 }
2472 else
2473 EMSG2(_(e_listdictarg), "count()");
2474 rettv->vval.v_number = n;
2475}
2476
2477/*
2478 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2479 *
2480 * Checks the existence of a cscope connection.
2481 */
2482 static void
2483f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2484{
2485#ifdef FEAT_CSCOPE
2486 int num = 0;
2487 char_u *dbpath = NULL;
2488 char_u *prepend = NULL;
2489 char_u buf[NUMBUFLEN];
2490
2491 if (argvars[0].v_type != VAR_UNKNOWN
2492 && argvars[1].v_type != VAR_UNKNOWN)
2493 {
2494 num = (int)get_tv_number(&argvars[0]);
2495 dbpath = get_tv_string(&argvars[1]);
2496 if (argvars[2].v_type != VAR_UNKNOWN)
2497 prepend = get_tv_string_buf(&argvars[2], buf);
2498 }
2499
2500 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2501#endif
2502}
2503
2504/*
2505 * "cursor(lnum, col)" function, or
2506 * "cursor(list)"
2507 *
2508 * Moves the cursor to the specified line and column.
2509 * Returns 0 when the position could be set, -1 otherwise.
2510 */
2511 static void
2512f_cursor(typval_T *argvars, typval_T *rettv)
2513{
2514 long line, col;
2515#ifdef FEAT_VIRTUALEDIT
2516 long coladd = 0;
2517#endif
2518 int set_curswant = TRUE;
2519
2520 rettv->vval.v_number = -1;
2521 if (argvars[1].v_type == VAR_UNKNOWN)
2522 {
2523 pos_T pos;
2524 colnr_T curswant = -1;
2525
2526 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2527 {
2528 EMSG(_(e_invarg));
2529 return;
2530 }
2531 line = pos.lnum;
2532 col = pos.col;
2533#ifdef FEAT_VIRTUALEDIT
2534 coladd = pos.coladd;
2535#endif
2536 if (curswant >= 0)
2537 {
2538 curwin->w_curswant = curswant - 1;
2539 set_curswant = FALSE;
2540 }
2541 }
2542 else
2543 {
2544 line = get_tv_lnum(argvars);
2545 col = (long)get_tv_number_chk(&argvars[1], NULL);
2546#ifdef FEAT_VIRTUALEDIT
2547 if (argvars[2].v_type != VAR_UNKNOWN)
2548 coladd = (long)get_tv_number_chk(&argvars[2], NULL);
2549#endif
2550 }
2551 if (line < 0 || col < 0
2552#ifdef FEAT_VIRTUALEDIT
2553 || coladd < 0
2554#endif
2555 )
2556 return; /* type error; errmsg already given */
2557 if (line > 0)
2558 curwin->w_cursor.lnum = line;
2559 if (col > 0)
2560 curwin->w_cursor.col = col - 1;
2561#ifdef FEAT_VIRTUALEDIT
2562 curwin->w_cursor.coladd = coladd;
2563#endif
2564
2565 /* Make sure the cursor is in a valid position. */
2566 check_cursor();
2567#ifdef FEAT_MBYTE
2568 /* Correct cursor for multi-byte character. */
2569 if (has_mbyte)
2570 mb_adjust_cursor();
2571#endif
2572
2573 curwin->w_set_curswant = set_curswant;
2574 rettv->vval.v_number = 0;
2575}
2576
2577/*
2578 * "deepcopy()" function
2579 */
2580 static void
2581f_deepcopy(typval_T *argvars, typval_T *rettv)
2582{
2583 int noref = 0;
2584 int copyID;
2585
2586 if (argvars[1].v_type != VAR_UNKNOWN)
2587 noref = (int)get_tv_number_chk(&argvars[1], NULL);
2588 if (noref < 0 || noref > 1)
2589 EMSG(_(e_invarg));
2590 else
2591 {
2592 copyID = get_copyID();
2593 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2594 }
2595}
2596
2597/*
2598 * "delete()" function
2599 */
2600 static void
2601f_delete(typval_T *argvars, typval_T *rettv)
2602{
2603 char_u nbuf[NUMBUFLEN];
2604 char_u *name;
2605 char_u *flags;
2606
2607 rettv->vval.v_number = -1;
2608 if (check_restricted() || check_secure())
2609 return;
2610
2611 name = get_tv_string(&argvars[0]);
2612 if (name == NULL || *name == NUL)
2613 {
2614 EMSG(_(e_invarg));
2615 return;
2616 }
2617
2618 if (argvars[1].v_type != VAR_UNKNOWN)
2619 flags = get_tv_string_buf(&argvars[1], nbuf);
2620 else
2621 flags = (char_u *)"";
2622
2623 if (*flags == NUL)
2624 /* delete a file */
2625 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2626 else if (STRCMP(flags, "d") == 0)
2627 /* delete an empty directory */
2628 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2629 else if (STRCMP(flags, "rf") == 0)
2630 /* delete a directory recursively */
2631 rettv->vval.v_number = delete_recursive(name);
2632 else
2633 EMSG2(_(e_invexpr2), flags);
2634}
2635
2636/*
2637 * "did_filetype()" function
2638 */
2639 static void
2640f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2641{
2642#ifdef FEAT_AUTOCMD
2643 rettv->vval.v_number = did_filetype;
2644#endif
2645}
2646
2647/*
2648 * "diff_filler()" function
2649 */
2650 static void
2651f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2652{
2653#ifdef FEAT_DIFF
2654 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
2655#endif
2656}
2657
2658/*
2659 * "diff_hlID()" function
2660 */
2661 static void
2662f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2663{
2664#ifdef FEAT_DIFF
2665 linenr_T lnum = get_tv_lnum(argvars);
2666 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01002667 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002668 static int fnum = 0;
2669 static int change_start = 0;
2670 static int change_end = 0;
2671 static hlf_T hlID = (hlf_T)0;
2672 int filler_lines;
2673 int col;
2674
2675 if (lnum < 0) /* ignore type error in {lnum} arg */
2676 lnum = 0;
2677 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002678 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002679 || fnum != curbuf->b_fnum)
2680 {
2681 /* New line, buffer, change: need to get the values. */
2682 filler_lines = diff_check(curwin, lnum);
2683 if (filler_lines < 0)
2684 {
2685 if (filler_lines == -1)
2686 {
2687 change_start = MAXCOL;
2688 change_end = -1;
2689 if (diff_find_change(curwin, lnum, &change_start, &change_end))
2690 hlID = HLF_ADD; /* added line */
2691 else
2692 hlID = HLF_CHD; /* changed line */
2693 }
2694 else
2695 hlID = HLF_ADD; /* added line */
2696 }
2697 else
2698 hlID = (hlf_T)0;
2699 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002700 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002701 fnum = curbuf->b_fnum;
2702 }
2703
2704 if (hlID == HLF_CHD || hlID == HLF_TXD)
2705 {
2706 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
2707 if (col >= change_start && col <= change_end)
2708 hlID = HLF_TXD; /* changed text */
2709 else
2710 hlID = HLF_CHD; /* changed line */
2711 }
2712 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
2713#endif
2714}
2715
2716/*
2717 * "empty({expr})" function
2718 */
2719 static void
2720f_empty(typval_T *argvars, typval_T *rettv)
2721{
2722 int n = FALSE;
2723
2724 switch (argvars[0].v_type)
2725 {
2726 case VAR_STRING:
2727 case VAR_FUNC:
2728 n = argvars[0].vval.v_string == NULL
2729 || *argvars[0].vval.v_string == NUL;
2730 break;
2731 case VAR_PARTIAL:
2732 n = FALSE;
2733 break;
2734 case VAR_NUMBER:
2735 n = argvars[0].vval.v_number == 0;
2736 break;
2737 case VAR_FLOAT:
2738#ifdef FEAT_FLOAT
2739 n = argvars[0].vval.v_float == 0.0;
2740 break;
2741#endif
2742 case VAR_LIST:
2743 n = argvars[0].vval.v_list == NULL
2744 || argvars[0].vval.v_list->lv_first == NULL;
2745 break;
2746 case VAR_DICT:
2747 n = argvars[0].vval.v_dict == NULL
2748 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2749 break;
2750 case VAR_SPECIAL:
2751 n = argvars[0].vval.v_number != VVAL_TRUE;
2752 break;
2753
2754 case VAR_JOB:
2755#ifdef FEAT_JOB_CHANNEL
2756 n = argvars[0].vval.v_job == NULL
2757 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2758 break;
2759#endif
2760 case VAR_CHANNEL:
2761#ifdef FEAT_JOB_CHANNEL
2762 n = argvars[0].vval.v_channel == NULL
2763 || !channel_is_open(argvars[0].vval.v_channel);
2764 break;
2765#endif
2766 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01002767 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002768 n = TRUE;
2769 break;
2770 }
2771
2772 rettv->vval.v_number = n;
2773}
2774
2775/*
2776 * "escape({string}, {chars})" function
2777 */
2778 static void
2779f_escape(typval_T *argvars, typval_T *rettv)
2780{
2781 char_u buf[NUMBUFLEN];
2782
2783 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
2784 get_tv_string_buf(&argvars[1], buf));
2785 rettv->v_type = VAR_STRING;
2786}
2787
2788/*
2789 * "eval()" function
2790 */
2791 static void
2792f_eval(typval_T *argvars, typval_T *rettv)
2793{
2794 char_u *s, *p;
2795
2796 s = get_tv_string_chk(&argvars[0]);
2797 if (s != NULL)
2798 s = skipwhite(s);
2799
2800 p = s;
2801 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
2802 {
2803 if (p != NULL && !aborting())
2804 EMSG2(_(e_invexpr2), p);
2805 need_clr_eos = FALSE;
2806 rettv->v_type = VAR_NUMBER;
2807 rettv->vval.v_number = 0;
2808 }
2809 else if (*s != NUL)
2810 EMSG(_(e_trailing));
2811}
2812
2813/*
2814 * "eventhandler()" function
2815 */
2816 static void
2817f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
2818{
2819 rettv->vval.v_number = vgetc_busy;
2820}
2821
2822/*
2823 * "executable()" function
2824 */
2825 static void
2826f_executable(typval_T *argvars, typval_T *rettv)
2827{
2828 char_u *name = get_tv_string(&argvars[0]);
2829
2830 /* Check in $PATH and also check directly if there is a directory name. */
2831 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
2832 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
2833}
2834
2835static garray_T redir_execute_ga;
2836
2837/*
2838 * Append "value[value_len]" to the execute() output.
2839 */
2840 void
2841execute_redir_str(char_u *value, int value_len)
2842{
2843 int len;
2844
2845 if (value_len == -1)
2846 len = (int)STRLEN(value); /* Append the entire string */
2847 else
2848 len = value_len; /* Append only "value_len" characters */
2849 if (ga_grow(&redir_execute_ga, len) == OK)
2850 {
2851 mch_memmove((char *)redir_execute_ga.ga_data
2852 + redir_execute_ga.ga_len, value, len);
2853 redir_execute_ga.ga_len += len;
2854 }
2855}
2856
2857/*
2858 * Get next line from a list.
2859 * Called by do_cmdline() to get the next line.
2860 * Returns allocated string, or NULL for end of function.
2861 */
2862
2863 static char_u *
2864get_list_line(
2865 int c UNUSED,
2866 void *cookie,
2867 int indent UNUSED)
2868{
2869 listitem_T **p = (listitem_T **)cookie;
2870 listitem_T *item = *p;
2871 char_u buf[NUMBUFLEN];
2872 char_u *s;
2873
2874 if (item == NULL)
2875 return NULL;
2876 s = get_tv_string_buf_chk(&item->li_tv, buf);
2877 *p = item->li_next;
2878 return s == NULL ? NULL : vim_strsave(s);
2879}
2880
2881/*
2882 * "execute()" function
2883 */
2884 static void
2885f_execute(typval_T *argvars, typval_T *rettv)
2886{
2887 char_u *cmd = NULL;
2888 list_T *list = NULL;
2889 int save_msg_silent = msg_silent;
2890 int save_emsg_silent = emsg_silent;
2891 int save_emsg_noredir = emsg_noredir;
2892 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01002893 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002894 garray_T save_ga;
2895
2896 rettv->vval.v_string = NULL;
2897 rettv->v_type = VAR_STRING;
2898
2899 if (argvars[0].v_type == VAR_LIST)
2900 {
2901 list = argvars[0].vval.v_list;
2902 if (list == NULL || list->lv_first == NULL)
2903 /* empty list, no commands, empty output */
2904 return;
2905 ++list->lv_refcount;
2906 }
2907 else
2908 {
2909 cmd = get_tv_string_chk(&argvars[0]);
2910 if (cmd == NULL)
2911 return;
2912 }
2913
2914 if (argvars[1].v_type != VAR_UNKNOWN)
2915 {
2916 char_u buf[NUMBUFLEN];
2917 char_u *s = get_tv_string_buf_chk(&argvars[1], buf);
2918
2919 if (s == NULL)
2920 return;
2921 if (STRNCMP(s, "silent", 6) == 0)
2922 ++msg_silent;
2923 if (STRCMP(s, "silent!") == 0)
2924 {
2925 emsg_silent = TRUE;
2926 emsg_noredir = TRUE;
2927 }
2928 }
2929 else
2930 ++msg_silent;
2931
2932 if (redir_execute)
2933 save_ga = redir_execute_ga;
2934 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
2935 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01002936 redir_off = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002937
2938 if (cmd != NULL)
2939 do_cmdline_cmd(cmd);
2940 else
2941 {
2942 listitem_T *item = list->lv_first;
2943
2944 do_cmdline(NULL, get_list_line, (void *)&item,
2945 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
2946 --list->lv_refcount;
2947 }
2948
Bram Moolenaard297f352017-01-29 20:31:21 +01002949 /* Need to append a NUL to the result. */
2950 if (ga_grow(&redir_execute_ga, 1) == OK)
2951 {
2952 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
2953 rettv->vval.v_string = redir_execute_ga.ga_data;
2954 }
2955 else
2956 {
2957 ga_clear(&redir_execute_ga);
2958 rettv->vval.v_string = NULL;
2959 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002960 msg_silent = save_msg_silent;
2961 emsg_silent = save_emsg_silent;
2962 emsg_noredir = save_emsg_noredir;
2963
2964 redir_execute = save_redir_execute;
2965 if (redir_execute)
2966 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01002967 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002968
2969 /* "silent reg" or "silent echo x" leaves msg_col somewhere in the
2970 * line. Put it back in the first column. */
2971 msg_col = 0;
2972}
2973
2974/*
2975 * "exepath()" function
2976 */
2977 static void
2978f_exepath(typval_T *argvars, typval_T *rettv)
2979{
2980 char_u *p = NULL;
2981
2982 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
2983 rettv->v_type = VAR_STRING;
2984 rettv->vval.v_string = p;
2985}
2986
2987/*
2988 * "exists()" function
2989 */
2990 static void
2991f_exists(typval_T *argvars, typval_T *rettv)
2992{
2993 char_u *p;
2994 char_u *name;
2995 int n = FALSE;
2996 int len = 0;
2997
2998 p = get_tv_string(&argvars[0]);
2999 if (*p == '$') /* environment variable */
3000 {
3001 /* first try "normal" environment variables (fast) */
3002 if (mch_getenv(p + 1) != NULL)
3003 n = TRUE;
3004 else
3005 {
3006 /* try expanding things like $VIM and ${HOME} */
3007 p = expand_env_save(p);
3008 if (p != NULL && *p != '$')
3009 n = TRUE;
3010 vim_free(p);
3011 }
3012 }
3013 else if (*p == '&' || *p == '+') /* option */
3014 {
3015 n = (get_option_tv(&p, NULL, TRUE) == OK);
3016 if (*skipwhite(p) != NUL)
3017 n = FALSE; /* trailing garbage */
3018 }
3019 else if (*p == '*') /* internal or user defined function */
3020 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003021 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003022 }
3023 else if (*p == ':')
3024 {
3025 n = cmd_exists(p + 1);
3026 }
3027 else if (*p == '#')
3028 {
3029#ifdef FEAT_AUTOCMD
3030 if (p[1] == '#')
3031 n = autocmd_supported(p + 2);
3032 else
3033 n = au_exists(p + 1);
3034#endif
3035 }
3036 else /* internal variable */
3037 {
3038 char_u *tofree;
3039 typval_T tv;
3040
3041 /* get_name_len() takes care of expanding curly braces */
3042 name = p;
3043 len = get_name_len(&p, &tofree, TRUE, FALSE);
3044 if (len > 0)
3045 {
3046 if (tofree != NULL)
3047 name = tofree;
3048 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
3049 if (n)
3050 {
3051 /* handle d.key, l[idx], f(expr) */
3052 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
3053 if (n)
3054 clear_tv(&tv);
3055 }
3056 }
3057 if (*p != NUL)
3058 n = FALSE;
3059
3060 vim_free(tofree);
3061 }
3062
3063 rettv->vval.v_number = n;
3064}
3065
3066#ifdef FEAT_FLOAT
3067/*
3068 * "exp()" function
3069 */
3070 static void
3071f_exp(typval_T *argvars, typval_T *rettv)
3072{
3073 float_T f = 0.0;
3074
3075 rettv->v_type = VAR_FLOAT;
3076 if (get_float_arg(argvars, &f) == OK)
3077 rettv->vval.v_float = exp(f);
3078 else
3079 rettv->vval.v_float = 0.0;
3080}
3081#endif
3082
3083/*
3084 * "expand()" function
3085 */
3086 static void
3087f_expand(typval_T *argvars, typval_T *rettv)
3088{
3089 char_u *s;
3090 int len;
3091 char_u *errormsg;
3092 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3093 expand_T xpc;
3094 int error = FALSE;
3095 char_u *result;
3096
3097 rettv->v_type = VAR_STRING;
3098 if (argvars[1].v_type != VAR_UNKNOWN
3099 && argvars[2].v_type != VAR_UNKNOWN
3100 && get_tv_number_chk(&argvars[2], &error)
3101 && !error)
3102 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003103 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003104 }
3105
3106 s = get_tv_string(&argvars[0]);
3107 if (*s == '%' || *s == '#' || *s == '<')
3108 {
3109 ++emsg_off;
3110 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3111 --emsg_off;
3112 if (rettv->v_type == VAR_LIST)
3113 {
3114 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3115 list_append_string(rettv->vval.v_list, result, -1);
3116 else
3117 vim_free(result);
3118 }
3119 else
3120 rettv->vval.v_string = result;
3121 }
3122 else
3123 {
3124 /* When the optional second argument is non-zero, don't remove matches
3125 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3126 if (argvars[1].v_type != VAR_UNKNOWN
3127 && get_tv_number_chk(&argvars[1], &error))
3128 options |= WILD_KEEP_ALL;
3129 if (!error)
3130 {
3131 ExpandInit(&xpc);
3132 xpc.xp_context = EXPAND_FILES;
3133 if (p_wic)
3134 options += WILD_ICASE;
3135 if (rettv->v_type == VAR_STRING)
3136 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3137 options, WILD_ALL);
3138 else if (rettv_list_alloc(rettv) != FAIL)
3139 {
3140 int i;
3141
3142 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3143 for (i = 0; i < xpc.xp_numfiles; i++)
3144 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3145 ExpandCleanup(&xpc);
3146 }
3147 }
3148 else
3149 rettv->vval.v_string = NULL;
3150 }
3151}
3152
3153/*
3154 * "extend(list, list [, idx])" function
3155 * "extend(dict, dict [, action])" function
3156 */
3157 static void
3158f_extend(typval_T *argvars, typval_T *rettv)
3159{
3160 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3161
3162 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3163 {
3164 list_T *l1, *l2;
3165 listitem_T *item;
3166 long before;
3167 int error = FALSE;
3168
3169 l1 = argvars[0].vval.v_list;
3170 l2 = argvars[1].vval.v_list;
3171 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
3172 && l2 != NULL)
3173 {
3174 if (argvars[2].v_type != VAR_UNKNOWN)
3175 {
3176 before = (long)get_tv_number_chk(&argvars[2], &error);
3177 if (error)
3178 return; /* type error; errmsg already given */
3179
3180 if (before == l1->lv_len)
3181 item = NULL;
3182 else
3183 {
3184 item = list_find(l1, before);
3185 if (item == NULL)
3186 {
3187 EMSGN(_(e_listidx), before);
3188 return;
3189 }
3190 }
3191 }
3192 else
3193 item = NULL;
3194 list_extend(l1, l2, item);
3195
3196 copy_tv(&argvars[0], rettv);
3197 }
3198 }
3199 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3200 {
3201 dict_T *d1, *d2;
3202 char_u *action;
3203 int i;
3204
3205 d1 = argvars[0].vval.v_dict;
3206 d2 = argvars[1].vval.v_dict;
3207 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
3208 && d2 != NULL)
3209 {
3210 /* Check the third argument. */
3211 if (argvars[2].v_type != VAR_UNKNOWN)
3212 {
3213 static char *(av[]) = {"keep", "force", "error"};
3214
3215 action = get_tv_string_chk(&argvars[2]);
3216 if (action == NULL)
3217 return; /* type error; errmsg already given */
3218 for (i = 0; i < 3; ++i)
3219 if (STRCMP(action, av[i]) == 0)
3220 break;
3221 if (i == 3)
3222 {
3223 EMSG2(_(e_invarg2), action);
3224 return;
3225 }
3226 }
3227 else
3228 action = (char_u *)"force";
3229
3230 dict_extend(d1, d2, action);
3231
3232 copy_tv(&argvars[0], rettv);
3233 }
3234 }
3235 else
3236 EMSG2(_(e_listdictarg), "extend()");
3237}
3238
3239/*
3240 * "feedkeys()" function
3241 */
3242 static void
3243f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3244{
3245 int remap = TRUE;
3246 int insert = FALSE;
3247 char_u *keys, *flags;
3248 char_u nbuf[NUMBUFLEN];
3249 int typed = FALSE;
3250 int execute = FALSE;
3251 int dangerous = FALSE;
3252 char_u *keys_esc;
3253
3254 /* This is not allowed in the sandbox. If the commands would still be
3255 * executed in the sandbox it would be OK, but it probably happens later,
3256 * when "sandbox" is no longer set. */
3257 if (check_secure())
3258 return;
3259
3260 keys = get_tv_string(&argvars[0]);
3261
3262 if (argvars[1].v_type != VAR_UNKNOWN)
3263 {
3264 flags = get_tv_string_buf(&argvars[1], nbuf);
3265 for ( ; *flags != NUL; ++flags)
3266 {
3267 switch (*flags)
3268 {
3269 case 'n': remap = FALSE; break;
3270 case 'm': remap = TRUE; break;
3271 case 't': typed = TRUE; break;
3272 case 'i': insert = TRUE; break;
3273 case 'x': execute = TRUE; break;
3274 case '!': dangerous = TRUE; break;
3275 }
3276 }
3277 }
3278
3279 if (*keys != NUL || execute)
3280 {
3281 /* Need to escape K_SPECIAL and CSI before putting the string in the
3282 * typeahead buffer. */
3283 keys_esc = vim_strsave_escape_csi(keys);
3284 if (keys_esc != NULL)
3285 {
3286 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
3287 insert ? 0 : typebuf.tb_len, !typed, FALSE);
3288 vim_free(keys_esc);
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003289 if (vgetc_busy
3290#ifdef FEAT_TIMERS
3291 || timer_busy
3292#endif
3293 )
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003294 typebuf_was_filled = TRUE;
3295 if (execute)
3296 {
3297 int save_msg_scroll = msg_scroll;
3298
3299 /* Avoid a 1 second delay when the keys start Insert mode. */
3300 msg_scroll = FALSE;
3301
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02003302#ifdef FEAT_TERMINAL
3303 if (term_use_loop())
3304 terminal_loop(FALSE);
3305 else
3306#endif
3307 {
3308 if (!dangerous)
3309 ++ex_normal_busy;
3310 exec_normal(TRUE);
3311 if (!dangerous)
3312 --ex_normal_busy;
3313 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003314 msg_scroll |= save_msg_scroll;
3315 }
3316 }
3317 }
3318}
3319
3320/*
3321 * "filereadable()" function
3322 */
3323 static void
3324f_filereadable(typval_T *argvars, typval_T *rettv)
3325{
3326 int fd;
3327 char_u *p;
3328 int n;
3329
3330#ifndef O_NONBLOCK
3331# define O_NONBLOCK 0
3332#endif
3333 p = get_tv_string(&argvars[0]);
3334 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3335 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3336 {
3337 n = TRUE;
3338 close(fd);
3339 }
3340 else
3341 n = FALSE;
3342
3343 rettv->vval.v_number = n;
3344}
3345
3346/*
3347 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3348 * rights to write into.
3349 */
3350 static void
3351f_filewritable(typval_T *argvars, typval_T *rettv)
3352{
3353 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
3354}
3355
3356 static void
3357findfilendir(
3358 typval_T *argvars UNUSED,
3359 typval_T *rettv,
3360 int find_what UNUSED)
3361{
3362#ifdef FEAT_SEARCHPATH
3363 char_u *fname;
3364 char_u *fresult = NULL;
3365 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3366 char_u *p;
3367 char_u pathbuf[NUMBUFLEN];
3368 int count = 1;
3369 int first = TRUE;
3370 int error = FALSE;
3371#endif
3372
3373 rettv->vval.v_string = NULL;
3374 rettv->v_type = VAR_STRING;
3375
3376#ifdef FEAT_SEARCHPATH
3377 fname = get_tv_string(&argvars[0]);
3378
3379 if (argvars[1].v_type != VAR_UNKNOWN)
3380 {
3381 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
3382 if (p == NULL)
3383 error = TRUE;
3384 else
3385 {
3386 if (*p != NUL)
3387 path = p;
3388
3389 if (argvars[2].v_type != VAR_UNKNOWN)
3390 count = (int)get_tv_number_chk(&argvars[2], &error);
3391 }
3392 }
3393
3394 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3395 error = TRUE;
3396
3397 if (*fname != NUL && !error)
3398 {
3399 do
3400 {
3401 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3402 vim_free(fresult);
3403 fresult = find_file_in_path_option(first ? fname : NULL,
3404 first ? (int)STRLEN(fname) : 0,
3405 0, first, path,
3406 find_what,
3407 curbuf->b_ffname,
3408 find_what == FINDFILE_DIR
3409 ? (char_u *)"" : curbuf->b_p_sua);
3410 first = FALSE;
3411
3412 if (fresult != NULL && rettv->v_type == VAR_LIST)
3413 list_append_string(rettv->vval.v_list, fresult, -1);
3414
3415 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3416 }
3417
3418 if (rettv->v_type == VAR_STRING)
3419 rettv->vval.v_string = fresult;
3420#endif
3421}
3422
3423/*
3424 * "filter()" function
3425 */
3426 static void
3427f_filter(typval_T *argvars, typval_T *rettv)
3428{
3429 filter_map(argvars, rettv, FALSE);
3430}
3431
3432/*
3433 * "finddir({fname}[, {path}[, {count}]])" function
3434 */
3435 static void
3436f_finddir(typval_T *argvars, typval_T *rettv)
3437{
3438 findfilendir(argvars, rettv, FINDFILE_DIR);
3439}
3440
3441/*
3442 * "findfile({fname}[, {path}[, {count}]])" function
3443 */
3444 static void
3445f_findfile(typval_T *argvars, typval_T *rettv)
3446{
3447 findfilendir(argvars, rettv, FINDFILE_FILE);
3448}
3449
3450#ifdef FEAT_FLOAT
3451/*
3452 * "float2nr({float})" function
3453 */
3454 static void
3455f_float2nr(typval_T *argvars, typval_T *rettv)
3456{
3457 float_T f = 0.0;
3458
3459 if (get_float_arg(argvars, &f) == OK)
3460 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003461 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003462 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003463 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003464 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003465 else
3466 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003467 }
3468}
3469
3470/*
3471 * "floor({float})" function
3472 */
3473 static void
3474f_floor(typval_T *argvars, typval_T *rettv)
3475{
3476 float_T f = 0.0;
3477
3478 rettv->v_type = VAR_FLOAT;
3479 if (get_float_arg(argvars, &f) == OK)
3480 rettv->vval.v_float = floor(f);
3481 else
3482 rettv->vval.v_float = 0.0;
3483}
3484
3485/*
3486 * "fmod()" function
3487 */
3488 static void
3489f_fmod(typval_T *argvars, typval_T *rettv)
3490{
3491 float_T fx = 0.0, fy = 0.0;
3492
3493 rettv->v_type = VAR_FLOAT;
3494 if (get_float_arg(argvars, &fx) == OK
3495 && get_float_arg(&argvars[1], &fy) == OK)
3496 rettv->vval.v_float = fmod(fx, fy);
3497 else
3498 rettv->vval.v_float = 0.0;
3499}
3500#endif
3501
3502/*
3503 * "fnameescape({string})" function
3504 */
3505 static void
3506f_fnameescape(typval_T *argvars, typval_T *rettv)
3507{
3508 rettv->vval.v_string = vim_strsave_fnameescape(
3509 get_tv_string(&argvars[0]), FALSE);
3510 rettv->v_type = VAR_STRING;
3511}
3512
3513/*
3514 * "fnamemodify({fname}, {mods})" function
3515 */
3516 static void
3517f_fnamemodify(typval_T *argvars, typval_T *rettv)
3518{
3519 char_u *fname;
3520 char_u *mods;
3521 int usedlen = 0;
3522 int len;
3523 char_u *fbuf = NULL;
3524 char_u buf[NUMBUFLEN];
3525
3526 fname = get_tv_string_chk(&argvars[0]);
3527 mods = get_tv_string_buf_chk(&argvars[1], buf);
3528 if (fname == NULL || mods == NULL)
3529 fname = NULL;
3530 else
3531 {
3532 len = (int)STRLEN(fname);
3533 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
3534 }
3535
3536 rettv->v_type = VAR_STRING;
3537 if (fname == NULL)
3538 rettv->vval.v_string = NULL;
3539 else
3540 rettv->vval.v_string = vim_strnsave(fname, len);
3541 vim_free(fbuf);
3542}
3543
3544static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
3545
3546/*
3547 * "foldclosed()" function
3548 */
3549 static void
3550foldclosed_both(
3551 typval_T *argvars UNUSED,
3552 typval_T *rettv,
3553 int end UNUSED)
3554{
3555#ifdef FEAT_FOLDING
3556 linenr_T lnum;
3557 linenr_T first, last;
3558
3559 lnum = get_tv_lnum(argvars);
3560 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3561 {
3562 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3563 {
3564 if (end)
3565 rettv->vval.v_number = (varnumber_T)last;
3566 else
3567 rettv->vval.v_number = (varnumber_T)first;
3568 return;
3569 }
3570 }
3571#endif
3572 rettv->vval.v_number = -1;
3573}
3574
3575/*
3576 * "foldclosed()" function
3577 */
3578 static void
3579f_foldclosed(typval_T *argvars, typval_T *rettv)
3580{
3581 foldclosed_both(argvars, rettv, FALSE);
3582}
3583
3584/*
3585 * "foldclosedend()" function
3586 */
3587 static void
3588f_foldclosedend(typval_T *argvars, typval_T *rettv)
3589{
3590 foldclosed_both(argvars, rettv, TRUE);
3591}
3592
3593/*
3594 * "foldlevel()" function
3595 */
3596 static void
3597f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3598{
3599#ifdef FEAT_FOLDING
3600 linenr_T lnum;
3601
3602 lnum = get_tv_lnum(argvars);
3603 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3604 rettv->vval.v_number = foldLevel(lnum);
3605#endif
3606}
3607
3608/*
3609 * "foldtext()" function
3610 */
3611 static void
3612f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
3613{
3614#ifdef FEAT_FOLDING
3615 linenr_T foldstart;
3616 linenr_T foldend;
3617 char_u *dashes;
3618 linenr_T lnum;
3619 char_u *s;
3620 char_u *r;
3621 int len;
3622 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003623 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003624#endif
3625
3626 rettv->v_type = VAR_STRING;
3627 rettv->vval.v_string = NULL;
3628#ifdef FEAT_FOLDING
3629 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
3630 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
3631 dashes = get_vim_var_str(VV_FOLDDASHES);
3632 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
3633 && dashes != NULL)
3634 {
3635 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02003636 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003637 if (!linewhite(lnum))
3638 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003639
3640 /* Find interesting text in this line. */
3641 s = skipwhite(ml_get(lnum));
3642 /* skip C comment-start */
3643 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
3644 {
3645 s = skipwhite(s + 2);
3646 if (*skipwhite(s) == NUL
3647 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
3648 {
3649 s = skipwhite(ml_get(lnum + 1));
3650 if (*s == '*')
3651 s = skipwhite(s + 1);
3652 }
3653 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02003654 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003655 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003656 r = alloc((unsigned)(STRLEN(txt)
3657 + STRLEN(dashes) /* for %s */
3658 + 20 /* for %3ld */
3659 + STRLEN(s))); /* concatenated */
3660 if (r != NULL)
3661 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02003662 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003663 len = (int)STRLEN(r);
3664 STRCAT(r, s);
3665 /* remove 'foldmarker' and 'commentstring' */
3666 foldtext_cleanup(r + len);
3667 rettv->vval.v_string = r;
3668 }
3669 }
3670#endif
3671}
3672
3673/*
3674 * "foldtextresult(lnum)" function
3675 */
3676 static void
3677f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
3678{
3679#ifdef FEAT_FOLDING
3680 linenr_T lnum;
3681 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003682 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003683 foldinfo_T foldinfo;
3684 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003685 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003686#endif
3687
3688 rettv->v_type = VAR_STRING;
3689 rettv->vval.v_string = NULL;
3690#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003691 if (entered)
3692 return; /* reject recursive use */
3693 entered = TRUE;
3694
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003695 lnum = get_tv_lnum(argvars);
3696 /* treat illegal types and illegal string values for {lnum} the same */
3697 if (lnum < 0)
3698 lnum = 0;
3699 fold_count = foldedCount(curwin, lnum, &foldinfo);
3700 if (fold_count > 0)
3701 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01003702 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
3703 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003704 if (text == buf)
3705 text = vim_strsave(text);
3706 rettv->vval.v_string = text;
3707 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003708
3709 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003710#endif
3711}
3712
3713/*
3714 * "foreground()" function
3715 */
3716 static void
3717f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3718{
3719#ifdef FEAT_GUI
3720 if (gui.in_use)
3721 gui_mch_set_foreground();
3722#else
3723# ifdef WIN32
3724 win32_set_foreground();
3725# endif
3726#endif
3727}
3728
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003729 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003730common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003731{
3732 char_u *s;
3733 char_u *name;
3734 int use_string = FALSE;
3735 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003736 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003737
3738 if (argvars[0].v_type == VAR_FUNC)
3739 {
3740 /* function(MyFunc, [arg], dict) */
3741 s = argvars[0].vval.v_string;
3742 }
3743 else if (argvars[0].v_type == VAR_PARTIAL
3744 && argvars[0].vval.v_partial != NULL)
3745 {
3746 /* function(dict.MyFunc, [arg]) */
3747 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003748 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003749 }
3750 else
3751 {
3752 /* function('MyFunc', [arg], dict) */
3753 s = get_tv_string(&argvars[0]);
3754 use_string = TRUE;
3755 }
3756
Bram Moolenaar843b8842016-08-21 14:36:15 +02003757 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003758 {
3759 name = s;
3760 trans_name = trans_function_name(&name, FALSE,
3761 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
3762 if (*name != NUL)
3763 s = NULL;
3764 }
3765
Bram Moolenaar843b8842016-08-21 14:36:15 +02003766 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
3767 || (is_funcref && trans_name == NULL))
Bram Moolenaar5582ef12016-09-14 22:16:13 +02003768 EMSG2(_(e_invarg2), use_string ? get_tv_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003769 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003770 else if (trans_name != NULL && (is_funcref
3771 ? find_func(trans_name) == NULL
3772 : !translated_function_exists(trans_name)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003773 EMSG2(_("E700: Unknown function: %s"), s);
3774 else
3775 {
3776 int dict_idx = 0;
3777 int arg_idx = 0;
3778 list_T *list = NULL;
3779
3780 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
3781 {
3782 char sid_buf[25];
3783 int off = *s == 's' ? 2 : 5;
3784
3785 /* Expand s: and <SID> into <SNR>nr_, so that the function can
3786 * also be called from another script. Using trans_function_name()
3787 * would also work, but some plugins depend on the name being
3788 * printable text. */
3789 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
3790 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
3791 if (name != NULL)
3792 {
3793 STRCPY(name, sid_buf);
3794 STRCAT(name, s + off);
3795 }
3796 }
3797 else
3798 name = vim_strsave(s);
3799
3800 if (argvars[1].v_type != VAR_UNKNOWN)
3801 {
3802 if (argvars[2].v_type != VAR_UNKNOWN)
3803 {
3804 /* function(name, [args], dict) */
3805 arg_idx = 1;
3806 dict_idx = 2;
3807 }
3808 else if (argvars[1].v_type == VAR_DICT)
3809 /* function(name, dict) */
3810 dict_idx = 1;
3811 else
3812 /* function(name, [args]) */
3813 arg_idx = 1;
3814 if (dict_idx > 0)
3815 {
3816 if (argvars[dict_idx].v_type != VAR_DICT)
3817 {
3818 EMSG(_("E922: expected a dict"));
3819 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003820 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003821 }
3822 if (argvars[dict_idx].vval.v_dict == NULL)
3823 dict_idx = 0;
3824 }
3825 if (arg_idx > 0)
3826 {
3827 if (argvars[arg_idx].v_type != VAR_LIST)
3828 {
3829 EMSG(_("E923: Second argument of function() must be a list or a dict"));
3830 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003831 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003832 }
3833 list = argvars[arg_idx].vval.v_list;
3834 if (list == NULL || list->lv_len == 0)
3835 arg_idx = 0;
3836 }
3837 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003838 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003839 {
3840 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
3841
3842 /* result is a VAR_PARTIAL */
3843 if (pt == NULL)
3844 vim_free(name);
3845 else
3846 {
3847 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
3848 {
3849 listitem_T *li;
3850 int i = 0;
3851 int arg_len = 0;
3852 int lv_len = 0;
3853
3854 if (arg_pt != NULL)
3855 arg_len = arg_pt->pt_argc;
3856 if (list != NULL)
3857 lv_len = list->lv_len;
3858 pt->pt_argc = arg_len + lv_len;
3859 pt->pt_argv = (typval_T *)alloc(
3860 sizeof(typval_T) * pt->pt_argc);
3861 if (pt->pt_argv == NULL)
3862 {
3863 vim_free(pt);
3864 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003865 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003866 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003867 for (i = 0; i < arg_len; i++)
3868 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
3869 if (lv_len > 0)
3870 for (li = list->lv_first; li != NULL;
3871 li = li->li_next)
3872 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003873 }
3874
3875 /* For "function(dict.func, [], dict)" and "func" is a partial
3876 * use "dict". That is backwards compatible. */
3877 if (dict_idx > 0)
3878 {
3879 /* The dict is bound explicitly, pt_auto is FALSE. */
3880 pt->pt_dict = argvars[dict_idx].vval.v_dict;
3881 ++pt->pt_dict->dv_refcount;
3882 }
3883 else if (arg_pt != NULL)
3884 {
3885 /* If the dict was bound automatically the result is also
3886 * bound automatically. */
3887 pt->pt_dict = arg_pt->pt_dict;
3888 pt->pt_auto = arg_pt->pt_auto;
3889 if (pt->pt_dict != NULL)
3890 ++pt->pt_dict->dv_refcount;
3891 }
3892
3893 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003894 if (arg_pt != NULL && arg_pt->pt_func != NULL)
3895 {
3896 pt->pt_func = arg_pt->pt_func;
3897 func_ptr_ref(pt->pt_func);
3898 vim_free(name);
3899 }
3900 else if (is_funcref)
3901 {
3902 pt->pt_func = find_func(trans_name);
3903 func_ptr_ref(pt->pt_func);
3904 vim_free(name);
3905 }
3906 else
3907 {
3908 pt->pt_name = name;
3909 func_ref(name);
3910 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003911 }
3912 rettv->v_type = VAR_PARTIAL;
3913 rettv->vval.v_partial = pt;
3914 }
3915 else
3916 {
3917 /* result is a VAR_FUNC */
3918 rettv->v_type = VAR_FUNC;
3919 rettv->vval.v_string = name;
3920 func_ref(name);
3921 }
3922 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003923theend:
3924 vim_free(trans_name);
3925}
3926
3927/*
3928 * "funcref()" function
3929 */
3930 static void
3931f_funcref(typval_T *argvars, typval_T *rettv)
3932{
3933 common_function(argvars, rettv, TRUE);
3934}
3935
3936/*
3937 * "function()" function
3938 */
3939 static void
3940f_function(typval_T *argvars, typval_T *rettv)
3941{
3942 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003943}
3944
3945/*
3946 * "garbagecollect()" function
3947 */
3948 static void
3949f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
3950{
3951 /* This is postponed until we are back at the toplevel, because we may be
3952 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
3953 want_garbage_collect = TRUE;
3954
3955 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
3956 garbage_collect_at_exit = TRUE;
3957}
3958
3959/*
3960 * "get()" function
3961 */
3962 static void
3963f_get(typval_T *argvars, typval_T *rettv)
3964{
3965 listitem_T *li;
3966 list_T *l;
3967 dictitem_T *di;
3968 dict_T *d;
3969 typval_T *tv = NULL;
3970
3971 if (argvars[0].v_type == VAR_LIST)
3972 {
3973 if ((l = argvars[0].vval.v_list) != NULL)
3974 {
3975 int error = FALSE;
3976
3977 li = list_find(l, (long)get_tv_number_chk(&argvars[1], &error));
3978 if (!error && li != NULL)
3979 tv = &li->li_tv;
3980 }
3981 }
3982 else if (argvars[0].v_type == VAR_DICT)
3983 {
3984 if ((d = argvars[0].vval.v_dict) != NULL)
3985 {
3986 di = dict_find(d, get_tv_string(&argvars[1]), -1);
3987 if (di != NULL)
3988 tv = &di->di_tv;
3989 }
3990 }
3991 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
3992 {
3993 partial_T *pt;
3994 partial_T fref_pt;
3995
3996 if (argvars[0].v_type == VAR_PARTIAL)
3997 pt = argvars[0].vval.v_partial;
3998 else
3999 {
4000 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4001 fref_pt.pt_name = argvars[0].vval.v_string;
4002 pt = &fref_pt;
4003 }
4004
4005 if (pt != NULL)
4006 {
4007 char_u *what = get_tv_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004008 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004009
4010 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4011 {
4012 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004013 n = partial_name(pt);
4014 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004015 rettv->vval.v_string = NULL;
4016 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004017 {
4018 rettv->vval.v_string = vim_strsave(n);
4019 if (rettv->v_type == VAR_FUNC)
4020 func_ref(rettv->vval.v_string);
4021 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004022 }
4023 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004024 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004025 else if (STRCMP(what, "args") == 0)
4026 {
4027 rettv->v_type = VAR_LIST;
4028 if (rettv_list_alloc(rettv) == OK)
4029 {
4030 int i;
4031
4032 for (i = 0; i < pt->pt_argc; ++i)
4033 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4034 }
4035 }
4036 else
4037 EMSG2(_(e_invarg2), what);
4038 return;
4039 }
4040 }
4041 else
4042 EMSG2(_(e_listdictarg), "get()");
4043
4044 if (tv == NULL)
4045 {
4046 if (argvars[2].v_type != VAR_UNKNOWN)
4047 copy_tv(&argvars[2], rettv);
4048 }
4049 else
4050 copy_tv(tv, rettv);
4051}
4052
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004053#ifdef FEAT_SIGNS
4054/*
4055 * Returns information about signs placed in a buffer as list of dicts.
4056 */
4057 static void
4058get_buffer_signs(buf_T *buf, list_T *l)
4059{
4060 signlist_T *sign;
4061
4062 for (sign = buf->b_signlist; sign; sign = sign->next)
4063 {
4064 dict_T *d = dict_alloc();
4065
4066 if (d != NULL)
4067 {
4068 dict_add_nr_str(d, "id", sign->id, NULL);
4069 dict_add_nr_str(d, "lnum", sign->lnum, NULL);
Bram Moolenaar6a402ed2016-08-28 14:11:24 +02004070 dict_add_nr_str(d, "name", 0L, sign_typenr2name(sign->typenr));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004071
4072 list_append_dict(l, d);
4073 }
4074 }
4075}
4076#endif
4077
4078/*
4079 * Returns buffer options, variables and other attributes in a dictionary.
4080 */
4081 static dict_T *
4082get_buffer_info(buf_T *buf)
4083{
4084 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004085 tabpage_T *tp;
4086 win_T *wp;
4087 list_T *windows;
4088
4089 dict = dict_alloc();
4090 if (dict == NULL)
4091 return NULL;
4092
Bram Moolenaar33928832016-08-18 21:22:04 +02004093 dict_add_nr_str(dict, "bufnr", buf->b_fnum, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004094 dict_add_nr_str(dict, "name", 0L,
4095 buf->b_ffname != NULL ? buf->b_ffname : (char_u *)"");
Bram Moolenaarf845b872017-01-06 14:04:54 +01004096 dict_add_nr_str(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4097 : buflist_findlnum(buf), NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004098 dict_add_nr_str(dict, "loaded", buf->b_ml.ml_mfp != NULL, NULL);
4099 dict_add_nr_str(dict, "listed", buf->b_p_bl, NULL);
4100 dict_add_nr_str(dict, "changed", bufIsChanged(buf), NULL);
Bram Moolenaar95c526e2017-02-25 14:59:34 +01004101 dict_add_nr_str(dict, "changedtick", CHANGEDTICK(buf), NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004102 dict_add_nr_str(dict, "hidden",
4103 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0,
4104 NULL);
4105
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004106 /* Get a reference to buffer variables */
4107 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004108
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004109 /* List of windows displaying this buffer */
4110 windows = list_alloc();
4111 if (windows != NULL)
4112 {
4113 FOR_ALL_TAB_WINDOWS(tp, wp)
4114 if (wp->w_buffer == buf)
4115 list_append_number(windows, (varnumber_T)wp->w_id);
4116 dict_add_list(dict, "windows", windows);
4117 }
4118
4119#ifdef FEAT_SIGNS
4120 if (buf->b_signlist != NULL)
4121 {
4122 /* List of signs placed in this buffer */
4123 list_T *signs = list_alloc();
4124 if (signs != NULL)
4125 {
4126 get_buffer_signs(buf, signs);
4127 dict_add_list(dict, "signs", signs);
4128 }
4129 }
4130#endif
4131
4132 return dict;
4133}
4134
4135/*
4136 * "getbufinfo()" function
4137 */
4138 static void
4139f_getbufinfo(typval_T *argvars, typval_T *rettv)
4140{
4141 buf_T *buf = NULL;
4142 buf_T *argbuf = NULL;
4143 dict_T *d;
4144 int filtered = FALSE;
4145 int sel_buflisted = FALSE;
4146 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004147 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004148
4149 if (rettv_list_alloc(rettv) != OK)
4150 return;
4151
4152 /* List of all the buffers or selected buffers */
4153 if (argvars[0].v_type == VAR_DICT)
4154 {
4155 dict_T *sel_d = argvars[0].vval.v_dict;
4156
4157 if (sel_d != NULL)
4158 {
4159 dictitem_T *di;
4160
4161 filtered = TRUE;
4162
4163 di = dict_find(sel_d, (char_u *)"buflisted", -1);
4164 if (di != NULL && get_tv_number(&di->di_tv))
4165 sel_buflisted = TRUE;
4166
4167 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
4168 if (di != NULL && get_tv_number(&di->di_tv))
4169 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004170
4171 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
4172 if (di != NULL && get_tv_number(&di->di_tv))
4173 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004174 }
4175 }
4176 else if (argvars[0].v_type != VAR_UNKNOWN)
4177 {
4178 /* Information about one buffer. Argument specifies the buffer */
4179 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4180 ++emsg_off;
4181 argbuf = get_buf_tv(&argvars[0], FALSE);
4182 --emsg_off;
4183 if (argbuf == NULL)
4184 return;
4185 }
4186
4187 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004188 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004189 {
4190 if (argbuf != NULL && argbuf != buf)
4191 continue;
4192 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004193 || (sel_buflisted && !buf->b_p_bl)
4194 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004195 continue;
4196
4197 d = get_buffer_info(buf);
4198 if (d != NULL)
4199 list_append_dict(rettv->vval.v_list, d);
4200 if (argbuf != NULL)
4201 return;
4202 }
4203}
4204
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004205static void get_buffer_lines(buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv);
4206
4207/*
4208 * Get line or list of lines from buffer "buf" into "rettv".
4209 * Return a range (from start to end) of lines in rettv from the specified
4210 * buffer.
4211 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4212 */
4213 static void
4214get_buffer_lines(
4215 buf_T *buf,
4216 linenr_T start,
4217 linenr_T end,
4218 int retlist,
4219 typval_T *rettv)
4220{
4221 char_u *p;
4222
4223 rettv->v_type = VAR_STRING;
4224 rettv->vval.v_string = NULL;
4225 if (retlist && rettv_list_alloc(rettv) == FAIL)
4226 return;
4227
4228 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4229 return;
4230
4231 if (!retlist)
4232 {
4233 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4234 p = ml_get_buf(buf, start, FALSE);
4235 else
4236 p = (char_u *)"";
4237 rettv->vval.v_string = vim_strsave(p);
4238 }
4239 else
4240 {
4241 if (end < start)
4242 return;
4243
4244 if (start < 1)
4245 start = 1;
4246 if (end > buf->b_ml.ml_line_count)
4247 end = buf->b_ml.ml_line_count;
4248 while (start <= end)
4249 if (list_append_string(rettv->vval.v_list,
4250 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4251 break;
4252 }
4253}
4254
4255/*
4256 * Get the lnum from the first argument.
4257 * Also accepts "$", then "buf" is used.
4258 * Returns 0 on error.
4259 */
4260 static linenr_T
4261get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
4262{
4263 if (argvars[0].v_type == VAR_STRING
4264 && argvars[0].vval.v_string != NULL
4265 && argvars[0].vval.v_string[0] == '$'
4266 && buf != NULL)
4267 return buf->b_ml.ml_line_count;
4268 return (linenr_T)get_tv_number_chk(&argvars[0], NULL);
4269}
4270
4271/*
4272 * "getbufline()" function
4273 */
4274 static void
4275f_getbufline(typval_T *argvars, typval_T *rettv)
4276{
4277 linenr_T lnum;
4278 linenr_T end;
4279 buf_T *buf;
4280
4281 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4282 ++emsg_off;
4283 buf = get_buf_tv(&argvars[0], FALSE);
4284 --emsg_off;
4285
4286 lnum = get_tv_lnum_buf(&argvars[1], buf);
4287 if (argvars[2].v_type == VAR_UNKNOWN)
4288 end = lnum;
4289 else
4290 end = get_tv_lnum_buf(&argvars[2], buf);
4291
4292 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4293}
4294
4295/*
4296 * "getbufvar()" function
4297 */
4298 static void
4299f_getbufvar(typval_T *argvars, typval_T *rettv)
4300{
4301 buf_T *buf;
4302 buf_T *save_curbuf;
4303 char_u *varname;
4304 dictitem_T *v;
4305 int done = FALSE;
4306
4307 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4308 varname = get_tv_string_chk(&argvars[1]);
4309 ++emsg_off;
4310 buf = get_buf_tv(&argvars[0], FALSE);
4311
4312 rettv->v_type = VAR_STRING;
4313 rettv->vval.v_string = NULL;
4314
4315 if (buf != NULL && varname != NULL)
4316 {
4317 /* set curbuf to be our buf, temporarily */
4318 save_curbuf = curbuf;
4319 curbuf = buf;
4320
Bram Moolenaar30567352016-08-27 21:25:44 +02004321 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004322 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004323 if (varname[1] == NUL)
4324 {
4325 /* get all buffer-local options in a dict */
4326 dict_T *opts = get_winbuf_options(TRUE);
4327
4328 if (opts != NULL)
4329 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004330 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004331 done = TRUE;
4332 }
4333 }
4334 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4335 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004336 done = TRUE;
4337 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004338 else
4339 {
4340 /* Look up the variable. */
4341 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4342 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4343 'b', varname, FALSE);
4344 if (v != NULL)
4345 {
4346 copy_tv(&v->di_tv, rettv);
4347 done = TRUE;
4348 }
4349 }
4350
4351 /* restore previous notion of curbuf */
4352 curbuf = save_curbuf;
4353 }
4354
4355 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4356 /* use the default value */
4357 copy_tv(&argvars[2], rettv);
4358
4359 --emsg_off;
4360}
4361
4362/*
4363 * "getchar()" function
4364 */
4365 static void
4366f_getchar(typval_T *argvars, typval_T *rettv)
4367{
4368 varnumber_T n;
4369 int error = FALSE;
4370
4371 /* Position the cursor. Needed after a message that ends in a space. */
4372 windgoto(msg_row, msg_col);
4373
4374 ++no_mapping;
4375 ++allow_keys;
4376 for (;;)
4377 {
4378 if (argvars[0].v_type == VAR_UNKNOWN)
4379 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004380 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004381 else if (get_tv_number_chk(&argvars[0], &error) == 1)
4382 /* getchar(1): only check if char avail */
4383 n = vpeekc_any();
4384 else if (error || vpeekc_any() == NUL)
4385 /* illegal argument or getchar(0) and no char avail: return zero */
4386 n = 0;
4387 else
4388 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004389 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004390
4391 if (n == K_IGNORE)
4392 continue;
4393 break;
4394 }
4395 --no_mapping;
4396 --allow_keys;
4397
4398 set_vim_var_nr(VV_MOUSE_WIN, 0);
4399 set_vim_var_nr(VV_MOUSE_WINID, 0);
4400 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4401 set_vim_var_nr(VV_MOUSE_COL, 0);
4402
4403 rettv->vval.v_number = n;
4404 if (IS_SPECIAL(n) || mod_mask != 0)
4405 {
4406 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4407 int i = 0;
4408
4409 /* Turn a special key into three bytes, plus modifier. */
4410 if (mod_mask != 0)
4411 {
4412 temp[i++] = K_SPECIAL;
4413 temp[i++] = KS_MODIFIER;
4414 temp[i++] = mod_mask;
4415 }
4416 if (IS_SPECIAL(n))
4417 {
4418 temp[i++] = K_SPECIAL;
4419 temp[i++] = K_SECOND(n);
4420 temp[i++] = K_THIRD(n);
4421 }
4422#ifdef FEAT_MBYTE
4423 else if (has_mbyte)
4424 i += (*mb_char2bytes)(n, temp + i);
4425#endif
4426 else
4427 temp[i++] = n;
4428 temp[i++] = NUL;
4429 rettv->v_type = VAR_STRING;
4430 rettv->vval.v_string = vim_strsave(temp);
4431
4432#ifdef FEAT_MOUSE
4433 if (is_mouse_key(n))
4434 {
4435 int row = mouse_row;
4436 int col = mouse_col;
4437 win_T *win;
4438 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004439 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004440 int winnr = 1;
4441
4442 if (row >= 0 && col >= 0)
4443 {
4444 /* Find the window at the mouse coordinates and compute the
4445 * text position. */
4446 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004447 if (win == NULL)
4448 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004449 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004450 for (wp = firstwin; wp != win; wp = wp->w_next)
4451 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004452 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4453 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4454 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4455 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4456 }
4457 }
4458#endif
4459 }
4460}
4461
4462/*
4463 * "getcharmod()" function
4464 */
4465 static void
4466f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4467{
4468 rettv->vval.v_number = mod_mask;
4469}
4470
4471/*
4472 * "getcharsearch()" function
4473 */
4474 static void
4475f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4476{
4477 if (rettv_dict_alloc(rettv) != FAIL)
4478 {
4479 dict_T *dict = rettv->vval.v_dict;
4480
4481 dict_add_nr_str(dict, "char", 0L, last_csearch());
4482 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
4483 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
4484 }
4485}
4486
4487/*
4488 * "getcmdline()" function
4489 */
4490 static void
4491f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4492{
4493 rettv->v_type = VAR_STRING;
4494 rettv->vval.v_string = get_cmdline_str();
4495}
4496
4497/*
4498 * "getcmdpos()" function
4499 */
4500 static void
4501f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4502{
4503 rettv->vval.v_number = get_cmdline_pos() + 1;
4504}
4505
4506/*
4507 * "getcmdtype()" function
4508 */
4509 static void
4510f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4511{
4512 rettv->v_type = VAR_STRING;
4513 rettv->vval.v_string = alloc(2);
4514 if (rettv->vval.v_string != NULL)
4515 {
4516 rettv->vval.v_string[0] = get_cmdline_type();
4517 rettv->vval.v_string[1] = NUL;
4518 }
4519}
4520
4521/*
4522 * "getcmdwintype()" function
4523 */
4524 static void
4525f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4526{
4527 rettv->v_type = VAR_STRING;
4528 rettv->vval.v_string = NULL;
4529#ifdef FEAT_CMDWIN
4530 rettv->vval.v_string = alloc(2);
4531 if (rettv->vval.v_string != NULL)
4532 {
4533 rettv->vval.v_string[0] = cmdwin_type;
4534 rettv->vval.v_string[1] = NUL;
4535 }
4536#endif
4537}
4538
4539#if defined(FEAT_CMDL_COMPL)
4540/*
4541 * "getcompletion()" function
4542 */
4543 static void
4544f_getcompletion(typval_T *argvars, typval_T *rettv)
4545{
4546 char_u *pat;
4547 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004548 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004549 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
4550 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004551
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004552 if (argvars[2].v_type != VAR_UNKNOWN)
4553 filtered = get_tv_number_chk(&argvars[2], NULL);
4554
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004555 if (p_wic)
4556 options |= WILD_ICASE;
4557
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004558 /* For filtered results, 'wildignore' is used */
4559 if (!filtered)
4560 options |= WILD_KEEP_ALL;
4561
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004562 ExpandInit(&xpc);
4563 xpc.xp_pattern = get_tv_string(&argvars[0]);
4564 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4565 xpc.xp_context = cmdcomplete_str_to_type(get_tv_string(&argvars[1]));
4566 if (xpc.xp_context == EXPAND_NOTHING)
4567 {
4568 if (argvars[1].v_type == VAR_STRING)
4569 EMSG2(_(e_invarg2), argvars[1].vval.v_string);
4570 else
4571 EMSG(_(e_invarg));
4572 return;
4573 }
4574
4575# if defined(FEAT_MENU)
4576 if (xpc.xp_context == EXPAND_MENUS)
4577 {
4578 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4579 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4580 }
4581# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02004582#ifdef FEAT_CSCOPE
4583 if (xpc.xp_context == EXPAND_CSCOPE)
4584 {
4585 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4586 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4587 }
4588#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02004589#ifdef FEAT_SIGNS
4590 if (xpc.xp_context == EXPAND_SIGN)
4591 {
4592 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4593 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4594 }
4595#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004596
4597 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4598 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
4599 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004600 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004601
4602 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4603
4604 for (i = 0; i < xpc.xp_numfiles; i++)
4605 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4606 }
4607 vim_free(pat);
4608 ExpandCleanup(&xpc);
4609}
4610#endif
4611
4612/*
4613 * "getcwd()" function
4614 */
4615 static void
4616f_getcwd(typval_T *argvars, typval_T *rettv)
4617{
4618 win_T *wp = NULL;
4619 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01004620 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004621
4622 rettv->v_type = VAR_STRING;
4623 rettv->vval.v_string = NULL;
4624
Bram Moolenaar54591292018-02-09 20:53:59 +01004625 if (argvars[0].v_type == VAR_NUMBER && argvars[0].vval.v_number == -1)
4626 global = TRUE;
4627 else
4628 wp = find_tabwin(&argvars[0], &argvars[1]);
4629
4630 if (wp != NULL && wp->w_localdir != NULL)
4631 rettv->vval.v_string = vim_strsave(wp->w_localdir);
4632 else if (wp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004633 {
Bram Moolenaar54591292018-02-09 20:53:59 +01004634 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004635 rettv->vval.v_string = vim_strsave(globaldir);
4636 else
4637 {
4638 cwd = alloc(MAXPATHL);
4639 if (cwd != NULL)
4640 {
4641 if (mch_dirname(cwd, MAXPATHL) != FAIL)
4642 rettv->vval.v_string = vim_strsave(cwd);
4643 vim_free(cwd);
4644 }
4645 }
4646#ifdef BACKSLASH_IN_FILENAME
4647 if (rettv->vval.v_string != NULL)
4648 slash_adjust(rettv->vval.v_string);
4649#endif
4650 }
4651}
4652
4653/*
4654 * "getfontname()" function
4655 */
4656 static void
4657f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
4658{
4659 rettv->v_type = VAR_STRING;
4660 rettv->vval.v_string = NULL;
4661#ifdef FEAT_GUI
4662 if (gui.in_use)
4663 {
4664 GuiFont font;
4665 char_u *name = NULL;
4666
4667 if (argvars[0].v_type == VAR_UNKNOWN)
4668 {
4669 /* Get the "Normal" font. Either the name saved by
4670 * hl_set_font_name() or from the font ID. */
4671 font = gui.norm_font;
4672 name = hl_get_font_name();
4673 }
4674 else
4675 {
4676 name = get_tv_string(&argvars[0]);
4677 if (STRCMP(name, "*") == 0) /* don't use font dialog */
4678 return;
4679 font = gui_mch_get_font(name, FALSE);
4680 if (font == NOFONT)
4681 return; /* Invalid font name, return empty string. */
4682 }
4683 rettv->vval.v_string = gui_mch_get_fontname(font, name);
4684 if (argvars[0].v_type != VAR_UNKNOWN)
4685 gui_mch_free_font(font);
4686 }
4687#endif
4688}
4689
4690/*
4691 * "getfperm({fname})" function
4692 */
4693 static void
4694f_getfperm(typval_T *argvars, typval_T *rettv)
4695{
4696 char_u *fname;
4697 stat_T st;
4698 char_u *perm = NULL;
4699 char_u flags[] = "rwx";
4700 int i;
4701
4702 fname = get_tv_string(&argvars[0]);
4703
4704 rettv->v_type = VAR_STRING;
4705 if (mch_stat((char *)fname, &st) >= 0)
4706 {
4707 perm = vim_strsave((char_u *)"---------");
4708 if (perm != NULL)
4709 {
4710 for (i = 0; i < 9; i++)
4711 {
4712 if (st.st_mode & (1 << (8 - i)))
4713 perm[i] = flags[i % 3];
4714 }
4715 }
4716 }
4717 rettv->vval.v_string = perm;
4718}
4719
4720/*
4721 * "getfsize({fname})" function
4722 */
4723 static void
4724f_getfsize(typval_T *argvars, typval_T *rettv)
4725{
4726 char_u *fname;
4727 stat_T st;
4728
4729 fname = get_tv_string(&argvars[0]);
4730
4731 rettv->v_type = VAR_NUMBER;
4732
4733 if (mch_stat((char *)fname, &st) >= 0)
4734 {
4735 if (mch_isdir(fname))
4736 rettv->vval.v_number = 0;
4737 else
4738 {
4739 rettv->vval.v_number = (varnumber_T)st.st_size;
4740
4741 /* non-perfect check for overflow */
4742 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
4743 rettv->vval.v_number = -2;
4744 }
4745 }
4746 else
4747 rettv->vval.v_number = -1;
4748}
4749
4750/*
4751 * "getftime({fname})" function
4752 */
4753 static void
4754f_getftime(typval_T *argvars, typval_T *rettv)
4755{
4756 char_u *fname;
4757 stat_T st;
4758
4759 fname = get_tv_string(&argvars[0]);
4760
4761 if (mch_stat((char *)fname, &st) >= 0)
4762 rettv->vval.v_number = (varnumber_T)st.st_mtime;
4763 else
4764 rettv->vval.v_number = -1;
4765}
4766
4767/*
4768 * "getftype({fname})" function
4769 */
4770 static void
4771f_getftype(typval_T *argvars, typval_T *rettv)
4772{
4773 char_u *fname;
4774 stat_T st;
4775 char_u *type = NULL;
4776 char *t;
4777
4778 fname = get_tv_string(&argvars[0]);
4779
4780 rettv->v_type = VAR_STRING;
4781 if (mch_lstat((char *)fname, &st) >= 0)
4782 {
4783#ifdef S_ISREG
4784 if (S_ISREG(st.st_mode))
4785 t = "file";
4786 else if (S_ISDIR(st.st_mode))
4787 t = "dir";
4788# ifdef S_ISLNK
4789 else if (S_ISLNK(st.st_mode))
4790 t = "link";
4791# endif
4792# ifdef S_ISBLK
4793 else if (S_ISBLK(st.st_mode))
4794 t = "bdev";
4795# endif
4796# ifdef S_ISCHR
4797 else if (S_ISCHR(st.st_mode))
4798 t = "cdev";
4799# endif
4800# ifdef S_ISFIFO
4801 else if (S_ISFIFO(st.st_mode))
4802 t = "fifo";
4803# endif
4804# ifdef S_ISSOCK
4805 else if (S_ISSOCK(st.st_mode))
4806 t = "fifo";
4807# endif
4808 else
4809 t = "other";
4810#else
4811# ifdef S_IFMT
4812 switch (st.st_mode & S_IFMT)
4813 {
4814 case S_IFREG: t = "file"; break;
4815 case S_IFDIR: t = "dir"; break;
4816# ifdef S_IFLNK
4817 case S_IFLNK: t = "link"; break;
4818# endif
4819# ifdef S_IFBLK
4820 case S_IFBLK: t = "bdev"; break;
4821# endif
4822# ifdef S_IFCHR
4823 case S_IFCHR: t = "cdev"; break;
4824# endif
4825# ifdef S_IFIFO
4826 case S_IFIFO: t = "fifo"; break;
4827# endif
4828# ifdef S_IFSOCK
4829 case S_IFSOCK: t = "socket"; break;
4830# endif
4831 default: t = "other";
4832 }
4833# else
4834 if (mch_isdir(fname))
4835 t = "dir";
4836 else
4837 t = "file";
4838# endif
4839#endif
4840 type = vim_strsave((char_u *)t);
4841 }
4842 rettv->vval.v_string = type;
4843}
4844
4845/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01004846 * "getjumplist()" function
4847 */
4848 static void
4849f_getjumplist(typval_T *argvars, typval_T *rettv)
4850{
4851#ifdef FEAT_JUMPLIST
4852 win_T *wp;
4853 int i;
4854 list_T *l;
4855 dict_T *d;
4856#endif
4857
4858 if (rettv_list_alloc(rettv) != OK)
4859 return;
4860
4861#ifdef FEAT_JUMPLIST
4862 wp = find_tabwin(&argvars[0], &argvars[1]);
4863 if (wp == NULL)
4864 return;
4865
4866 l = list_alloc();
4867 if (l == NULL)
4868 return;
4869
4870 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4871 return;
4872 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
4873
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004874 cleanup_jumplist(wp);
Bram Moolenaar4f505882018-02-10 21:06:32 +01004875 for (i = 0; i < wp->w_jumplistlen; ++i)
4876 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004877 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
4878 continue;
4879 if (wp->w_jumplist[i].fmark.fnum == 0)
4880 fname2fnum(&wp->w_jumplist[i]);
Bram Moolenaar4f505882018-02-10 21:06:32 +01004881 if ((d = dict_alloc()) == NULL)
4882 return;
4883 if (list_append_dict(l, d) == FAIL)
4884 return;
4885 dict_add_nr_str(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum,
4886 NULL);
4887 dict_add_nr_str(d, "col", (long)wp->w_jumplist[i].fmark.mark.col,
4888 NULL);
4889# ifdef FEAT_VIRTUALEDIT
4890 dict_add_nr_str(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd,
4891 NULL);
4892# endif
4893 dict_add_nr_str(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum, NULL);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004894 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaar4f505882018-02-10 21:06:32 +01004895 dict_add_nr_str(d, "filename", 0L, wp->w_jumplist[i].fname);
4896 }
4897#endif
4898}
4899
4900/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004901 * "getline(lnum, [end])" function
4902 */
4903 static void
4904f_getline(typval_T *argvars, typval_T *rettv)
4905{
4906 linenr_T lnum;
4907 linenr_T end;
4908 int retlist;
4909
4910 lnum = get_tv_lnum(argvars);
4911 if (argvars[1].v_type == VAR_UNKNOWN)
4912 {
4913 end = 0;
4914 retlist = FALSE;
4915 }
4916 else
4917 {
4918 end = get_tv_lnum(&argvars[1]);
4919 retlist = TRUE;
4920 }
4921
4922 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
4923}
4924
Bram Moolenaar4ae20952016-08-13 15:29:14 +02004925#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02004926 static void
4927get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
4928{
Bram Moolenaard823fa92016-08-12 16:29:27 +02004929 if (what_arg->v_type == VAR_UNKNOWN)
4930 {
4931 if (rettv_list_alloc(rettv) == OK)
4932 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004933 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02004934 }
4935 else
4936 {
4937 if (rettv_dict_alloc(rettv) == OK)
4938 if (is_qf || (wp != NULL))
4939 {
4940 if (what_arg->v_type == VAR_DICT)
4941 {
4942 dict_T *d = what_arg->vval.v_dict;
4943
4944 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02004945 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02004946 }
4947 else
4948 EMSG(_(e_dictreq));
4949 }
4950 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004951}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02004952#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02004953
4954/*
4955 * "getloclist()" function
4956 */
4957 static void
4958f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4959{
4960#ifdef FEAT_QUICKFIX
4961 win_T *wp;
4962
4963 wp = find_win_by_nr(&argvars[0], NULL);
4964 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
4965#endif
4966}
4967
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004968/*
4969 * "getmatches()" function
4970 */
4971 static void
4972f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4973{
4974#ifdef FEAT_SEARCH_EXTRA
4975 dict_T *dict;
4976 matchitem_T *cur = curwin->w_match_head;
4977 int i;
4978
4979 if (rettv_list_alloc(rettv) == OK)
4980 {
4981 while (cur != NULL)
4982 {
4983 dict = dict_alloc();
4984 if (dict == NULL)
4985 return;
4986 if (cur->match.regprog == NULL)
4987 {
4988 /* match added with matchaddpos() */
4989 for (i = 0; i < MAXPOSMATCH; ++i)
4990 {
4991 llpos_T *llpos;
4992 char buf[6];
4993 list_T *l;
4994
4995 llpos = &cur->pos.pos[i];
4996 if (llpos->lnum == 0)
4997 break;
4998 l = list_alloc();
4999 if (l == NULL)
5000 break;
5001 list_append_number(l, (varnumber_T)llpos->lnum);
5002 if (llpos->col > 0)
5003 {
5004 list_append_number(l, (varnumber_T)llpos->col);
5005 list_append_number(l, (varnumber_T)llpos->len);
5006 }
5007 sprintf(buf, "pos%d", i + 1);
5008 dict_add_list(dict, buf, l);
5009 }
5010 }
5011 else
5012 {
5013 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
5014 }
5015 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
5016 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
5017 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
5018# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
5019 if (cur->conceal_char)
5020 {
5021 char_u buf[MB_MAXBYTES + 1];
5022
5023 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
5024 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
5025 }
5026# endif
5027 list_append_dict(rettv->vval.v_list, dict);
5028 cur = cur->next;
5029 }
5030 }
5031#endif
5032}
5033
5034/*
5035 * "getpid()" function
5036 */
5037 static void
5038f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5039{
5040 rettv->vval.v_number = mch_get_pid();
5041}
5042
5043 static void
5044getpos_both(
5045 typval_T *argvars,
5046 typval_T *rettv,
5047 int getcurpos)
5048{
5049 pos_T *fp;
5050 list_T *l;
5051 int fnum = -1;
5052
5053 if (rettv_list_alloc(rettv) == OK)
5054 {
5055 l = rettv->vval.v_list;
5056 if (getcurpos)
5057 fp = &curwin->w_cursor;
5058 else
5059 fp = var2fpos(&argvars[0], TRUE, &fnum);
5060 if (fnum != -1)
5061 list_append_number(l, (varnumber_T)fnum);
5062 else
5063 list_append_number(l, (varnumber_T)0);
5064 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5065 : (varnumber_T)0);
5066 list_append_number(l, (fp != NULL)
5067 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5068 : (varnumber_T)0);
5069 list_append_number(l,
5070#ifdef FEAT_VIRTUALEDIT
5071 (fp != NULL) ? (varnumber_T)fp->coladd :
5072#endif
5073 (varnumber_T)0);
5074 if (getcurpos)
5075 {
5076 update_curswant();
5077 list_append_number(l, curwin->w_curswant == MAXCOL ?
5078 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
5079 }
5080 }
5081 else
5082 rettv->vval.v_number = FALSE;
5083}
5084
5085
5086/*
5087 * "getcurpos()" function
5088 */
5089 static void
5090f_getcurpos(typval_T *argvars, typval_T *rettv)
5091{
5092 getpos_both(argvars, rettv, TRUE);
5093}
5094
5095/*
5096 * "getpos(string)" function
5097 */
5098 static void
5099f_getpos(typval_T *argvars, typval_T *rettv)
5100{
5101 getpos_both(argvars, rettv, FALSE);
5102}
5103
5104/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005105 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005106 */
5107 static void
5108f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5109{
5110#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005111 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005112#endif
5113}
5114
5115/*
5116 * "getreg()" function
5117 */
5118 static void
5119f_getreg(typval_T *argvars, typval_T *rettv)
5120{
5121 char_u *strregname;
5122 int regname;
5123 int arg2 = FALSE;
5124 int return_list = FALSE;
5125 int error = FALSE;
5126
5127 if (argvars[0].v_type != VAR_UNKNOWN)
5128 {
5129 strregname = get_tv_string_chk(&argvars[0]);
5130 error = strregname == NULL;
5131 if (argvars[1].v_type != VAR_UNKNOWN)
5132 {
5133 arg2 = (int)get_tv_number_chk(&argvars[1], &error);
5134 if (!error && argvars[2].v_type != VAR_UNKNOWN)
5135 return_list = (int)get_tv_number_chk(&argvars[2], &error);
5136 }
5137 }
5138 else
5139 strregname = get_vim_var_str(VV_REG);
5140
5141 if (error)
5142 return;
5143
5144 regname = (strregname == NULL ? '"' : *strregname);
5145 if (regname == 0)
5146 regname = '"';
5147
5148 if (return_list)
5149 {
5150 rettv->v_type = VAR_LIST;
5151 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5152 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5153 if (rettv->vval.v_list == NULL)
5154 (void)rettv_list_alloc(rettv);
5155 else
5156 ++rettv->vval.v_list->lv_refcount;
5157 }
5158 else
5159 {
5160 rettv->v_type = VAR_STRING;
5161 rettv->vval.v_string = get_reg_contents(regname,
5162 arg2 ? GREG_EXPR_SRC : 0);
5163 }
5164}
5165
5166/*
5167 * "getregtype()" function
5168 */
5169 static void
5170f_getregtype(typval_T *argvars, typval_T *rettv)
5171{
5172 char_u *strregname;
5173 int regname;
5174 char_u buf[NUMBUFLEN + 2];
5175 long reglen = 0;
5176
5177 if (argvars[0].v_type != VAR_UNKNOWN)
5178 {
5179 strregname = get_tv_string_chk(&argvars[0]);
5180 if (strregname == NULL) /* type error; errmsg already given */
5181 {
5182 rettv->v_type = VAR_STRING;
5183 rettv->vval.v_string = NULL;
5184 return;
5185 }
5186 }
5187 else
5188 /* Default to v:register */
5189 strregname = get_vim_var_str(VV_REG);
5190
5191 regname = (strregname == NULL ? '"' : *strregname);
5192 if (regname == 0)
5193 regname = '"';
5194
5195 buf[0] = NUL;
5196 buf[1] = NUL;
5197 switch (get_reg_type(regname, &reglen))
5198 {
5199 case MLINE: buf[0] = 'V'; break;
5200 case MCHAR: buf[0] = 'v'; break;
5201 case MBLOCK:
5202 buf[0] = Ctrl_V;
5203 sprintf((char *)buf + 1, "%ld", reglen + 1);
5204 break;
5205 }
5206 rettv->v_type = VAR_STRING;
5207 rettv->vval.v_string = vim_strsave(buf);
5208}
5209
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005210/*
5211 * Returns information (variables, options, etc.) about a tab page
5212 * as a dictionary.
5213 */
5214 static dict_T *
5215get_tabpage_info(tabpage_T *tp, int tp_idx)
5216{
5217 win_T *wp;
5218 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005219 list_T *l;
5220
5221 dict = dict_alloc();
5222 if (dict == NULL)
5223 return NULL;
5224
Bram Moolenaar33928832016-08-18 21:22:04 +02005225 dict_add_nr_str(dict, "tabnr", tp_idx, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005226
5227 l = list_alloc();
5228 if (l != NULL)
5229 {
5230 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5231 wp; wp = wp->w_next)
5232 list_append_number(l, (varnumber_T)wp->w_id);
5233 dict_add_list(dict, "windows", l);
5234 }
5235
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005236 /* Make a reference to tabpage variables */
5237 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005238
5239 return dict;
5240}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005241
5242/*
5243 * "gettabinfo()" function
5244 */
5245 static void
5246f_gettabinfo(typval_T *argvars, typval_T *rettv)
5247{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005248 tabpage_T *tp, *tparg = NULL;
5249 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005250 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005251
5252 if (rettv_list_alloc(rettv) != OK)
5253 return;
5254
5255 if (argvars[0].v_type != VAR_UNKNOWN)
5256 {
5257 /* Information about one tab page */
5258 tparg = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5259 if (tparg == NULL)
5260 return;
5261 }
5262
5263 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005264 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005265 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005266 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005267 if (tparg != NULL && tp != tparg)
5268 continue;
5269 d = get_tabpage_info(tp, tpnr);
5270 if (d != NULL)
5271 list_append_dict(rettv->vval.v_list, d);
5272 if (tparg != NULL)
5273 return;
5274 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005275}
5276
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005277/*
5278 * "gettabvar()" function
5279 */
5280 static void
5281f_gettabvar(typval_T *argvars, typval_T *rettv)
5282{
5283 win_T *oldcurwin;
5284 tabpage_T *tp, *oldtabpage;
5285 dictitem_T *v;
5286 char_u *varname;
5287 int done = FALSE;
5288
5289 rettv->v_type = VAR_STRING;
5290 rettv->vval.v_string = NULL;
5291
5292 varname = get_tv_string_chk(&argvars[1]);
5293 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5294 if (tp != NULL && varname != NULL)
5295 {
5296 /* Set tp to be our tabpage, temporarily. Also set the window to the
5297 * first window in the tabpage, otherwise the window is not valid. */
5298 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005299 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5300 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005301 {
5302 /* look up the variable */
5303 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5304 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5305 if (v != NULL)
5306 {
5307 copy_tv(&v->di_tv, rettv);
5308 done = TRUE;
5309 }
5310 }
5311
5312 /* restore previous notion of curwin */
5313 restore_win(oldcurwin, oldtabpage, TRUE);
5314 }
5315
5316 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5317 copy_tv(&argvars[2], rettv);
5318}
5319
5320/*
5321 * "gettabwinvar()" function
5322 */
5323 static void
5324f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5325{
5326 getwinvar(argvars, rettv, 1);
5327}
5328
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005329/*
5330 * Returns information about a window as a dictionary.
5331 */
5332 static dict_T *
5333get_win_info(win_T *wp, short tpnr, short winnr)
5334{
5335 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005336
5337 dict = dict_alloc();
5338 if (dict == NULL)
5339 return NULL;
5340
Bram Moolenaar33928832016-08-18 21:22:04 +02005341 dict_add_nr_str(dict, "tabnr", tpnr, NULL);
5342 dict_add_nr_str(dict, "winnr", winnr, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005343 dict_add_nr_str(dict, "winid", wp->w_id, NULL);
5344 dict_add_nr_str(dict, "height", wp->w_height, NULL);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005345#ifdef FEAT_MENU
5346 dict_add_nr_str(dict, "winbar", wp->w_winbar_height, NULL);
5347#endif
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005348 dict_add_nr_str(dict, "width", wp->w_width, NULL);
Bram Moolenaar33928832016-08-18 21:22:04 +02005349 dict_add_nr_str(dict, "bufnr", wp->w_buffer->b_fnum, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005350
Bram Moolenaar69905d12017-08-13 18:14:47 +02005351#ifdef FEAT_TERMINAL
5352 dict_add_nr_str(dict, "terminal", bt_terminal(wp->w_buffer), NULL);
5353#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005354#ifdef FEAT_QUICKFIX
5355 dict_add_nr_str(dict, "quickfix", bt_quickfix(wp->w_buffer), NULL);
5356 dict_add_nr_str(dict, "loclist",
5357 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL), NULL);
5358#endif
5359
Bram Moolenaar30567352016-08-27 21:25:44 +02005360 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005361 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005362
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005363 return dict;
5364}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005365
5366/*
5367 * "getwininfo()" function
5368 */
5369 static void
5370f_getwininfo(typval_T *argvars, typval_T *rettv)
5371{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005372 tabpage_T *tp;
5373 win_T *wp = NULL, *wparg = NULL;
5374 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005375 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005376
5377 if (rettv_list_alloc(rettv) != OK)
5378 return;
5379
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005380 if (argvars[0].v_type != VAR_UNKNOWN)
5381 {
5382 wparg = win_id2wp(argvars);
5383 if (wparg == NULL)
5384 return;
5385 }
5386
5387 /* Collect information about either all the windows across all the tab
5388 * pages or one particular window.
5389 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005390 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005391 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005392 tabnr++;
5393 winnr = 0;
5394 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005395 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005396 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005397 if (wparg != NULL && wp != wparg)
5398 continue;
5399 d = get_win_info(wp, tabnr, winnr);
5400 if (d != NULL)
5401 list_append_dict(rettv->vval.v_list, d);
5402 if (wparg != NULL)
5403 /* found information about a specific window */
5404 return;
5405 }
5406 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005407}
5408
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005409/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005410 * "win_findbuf()" function
5411 */
5412 static void
5413f_win_findbuf(typval_T *argvars, typval_T *rettv)
5414{
5415 if (rettv_list_alloc(rettv) != FAIL)
5416 win_findbuf(argvars, rettv->vval.v_list);
5417}
5418
5419/*
5420 * "win_getid()" function
5421 */
5422 static void
5423f_win_getid(typval_T *argvars, typval_T *rettv)
5424{
5425 rettv->vval.v_number = win_getid(argvars);
5426}
5427
5428/*
5429 * "win_gotoid()" function
5430 */
5431 static void
5432f_win_gotoid(typval_T *argvars, typval_T *rettv)
5433{
5434 rettv->vval.v_number = win_gotoid(argvars);
5435}
5436
5437/*
5438 * "win_id2tabwin()" function
5439 */
5440 static void
5441f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5442{
5443 if (rettv_list_alloc(rettv) != FAIL)
5444 win_id2tabwin(argvars, rettv->vval.v_list);
5445}
5446
5447/*
5448 * "win_id2win()" function
5449 */
5450 static void
5451f_win_id2win(typval_T *argvars, typval_T *rettv)
5452{
5453 rettv->vval.v_number = win_id2win(argvars);
5454}
5455
5456/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005457 * "win_screenpos()" function
5458 */
5459 static void
5460f_win_screenpos(typval_T *argvars, typval_T *rettv)
5461{
5462 win_T *wp;
5463
5464 if (rettv_list_alloc(rettv) == FAIL)
5465 return;
5466
5467 wp = find_win_by_nr(&argvars[0], NULL);
5468 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5469 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5470}
5471
5472/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005473 * "getwinposx()" function
5474 */
5475 static void
5476f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5477{
5478 rettv->vval.v_number = -1;
5479#ifdef FEAT_GUI
5480 if (gui.in_use)
5481 {
5482 int x, y;
5483
5484 if (gui_mch_get_winpos(&x, &y) == OK)
5485 rettv->vval.v_number = x;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005486 return;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005487 }
5488#endif
5489#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5490 {
5491 int x, y;
5492
5493 if (term_get_winpos(&x, &y) == OK)
5494 rettv->vval.v_number = x;
5495 }
5496#endif
5497}
5498
5499/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005500 * "getwinposy()" function
5501 */
5502 static void
5503f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5504{
5505 rettv->vval.v_number = -1;
5506#ifdef FEAT_GUI
5507 if (gui.in_use)
5508 {
5509 int x, y;
5510
5511 if (gui_mch_get_winpos(&x, &y) == OK)
5512 rettv->vval.v_number = y;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005513 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005514 }
5515#endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005516#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5517 {
5518 int x, y;
5519
5520 if (term_get_winpos(&x, &y) == OK)
5521 rettv->vval.v_number = y;
5522 }
5523#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005524}
5525
5526/*
5527 * "getwinvar()" function
5528 */
5529 static void
5530f_getwinvar(typval_T *argvars, typval_T *rettv)
5531{
5532 getwinvar(argvars, rettv, 0);
5533}
5534
5535/*
5536 * "glob()" function
5537 */
5538 static void
5539f_glob(typval_T *argvars, typval_T *rettv)
5540{
5541 int options = WILD_SILENT|WILD_USE_NL;
5542 expand_T xpc;
5543 int error = FALSE;
5544
5545 /* When the optional second argument is non-zero, don't remove matches
5546 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5547 rettv->v_type = VAR_STRING;
5548 if (argvars[1].v_type != VAR_UNKNOWN)
5549 {
5550 if (get_tv_number_chk(&argvars[1], &error))
5551 options |= WILD_KEEP_ALL;
5552 if (argvars[2].v_type != VAR_UNKNOWN)
5553 {
5554 if (get_tv_number_chk(&argvars[2], &error))
5555 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005556 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005557 }
5558 if (argvars[3].v_type != VAR_UNKNOWN
5559 && get_tv_number_chk(&argvars[3], &error))
5560 options |= WILD_ALLLINKS;
5561 }
5562 }
5563 if (!error)
5564 {
5565 ExpandInit(&xpc);
5566 xpc.xp_context = EXPAND_FILES;
5567 if (p_wic)
5568 options += WILD_ICASE;
5569 if (rettv->v_type == VAR_STRING)
5570 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
5571 NULL, options, WILD_ALL);
5572 else if (rettv_list_alloc(rettv) != FAIL)
5573 {
5574 int i;
5575
5576 ExpandOne(&xpc, get_tv_string(&argvars[0]),
5577 NULL, options, WILD_ALL_KEEP);
5578 for (i = 0; i < xpc.xp_numfiles; i++)
5579 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5580
5581 ExpandCleanup(&xpc);
5582 }
5583 }
5584 else
5585 rettv->vval.v_string = NULL;
5586}
5587
5588/*
5589 * "globpath()" function
5590 */
5591 static void
5592f_globpath(typval_T *argvars, typval_T *rettv)
5593{
5594 int flags = 0;
5595 char_u buf1[NUMBUFLEN];
5596 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
5597 int error = FALSE;
5598 garray_T ga;
5599 int i;
5600
5601 /* When the optional second argument is non-zero, don't remove matches
5602 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5603 rettv->v_type = VAR_STRING;
5604 if (argvars[2].v_type != VAR_UNKNOWN)
5605 {
5606 if (get_tv_number_chk(&argvars[2], &error))
5607 flags |= WILD_KEEP_ALL;
5608 if (argvars[3].v_type != VAR_UNKNOWN)
5609 {
5610 if (get_tv_number_chk(&argvars[3], &error))
5611 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005612 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005613 }
5614 if (argvars[4].v_type != VAR_UNKNOWN
5615 && get_tv_number_chk(&argvars[4], &error))
5616 flags |= WILD_ALLLINKS;
5617 }
5618 }
5619 if (file != NULL && !error)
5620 {
5621 ga_init2(&ga, (int)sizeof(char_u *), 10);
5622 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
5623 if (rettv->v_type == VAR_STRING)
5624 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
5625 else if (rettv_list_alloc(rettv) != FAIL)
5626 for (i = 0; i < ga.ga_len; ++i)
5627 list_append_string(rettv->vval.v_list,
5628 ((char_u **)(ga.ga_data))[i], -1);
5629 ga_clear_strings(&ga);
5630 }
5631 else
5632 rettv->vval.v_string = NULL;
5633}
5634
5635/*
5636 * "glob2regpat()" function
5637 */
5638 static void
5639f_glob2regpat(typval_T *argvars, typval_T *rettv)
5640{
5641 char_u *pat = get_tv_string_chk(&argvars[0]);
5642
5643 rettv->v_type = VAR_STRING;
5644 rettv->vval.v_string = (pat == NULL)
5645 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
5646}
5647
5648/* for VIM_VERSION_ defines */
5649#include "version.h"
5650
5651/*
5652 * "has()" function
5653 */
5654 static void
5655f_has(typval_T *argvars, typval_T *rettv)
5656{
5657 int i;
5658 char_u *name;
5659 int n = FALSE;
5660 static char *(has_list[]) =
5661 {
5662#ifdef AMIGA
5663 "amiga",
5664# ifdef FEAT_ARP
5665 "arp",
5666# endif
5667#endif
5668#ifdef __BEOS__
5669 "beos",
5670#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02005671#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01005672 "mac", /* Mac OS X (and, once, Mac OS Classic) */
5673 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02005674# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01005675 "macunix", /* Mac OS X, with the darwin feature */
5676 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02005677# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005678#endif
5679#ifdef __QNX__
5680 "qnx",
5681#endif
5682#ifdef UNIX
5683 "unix",
5684#endif
5685#ifdef VMS
5686 "vms",
5687#endif
5688#ifdef WIN32
5689 "win32",
5690#endif
5691#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
5692 "win32unix",
5693#endif
5694#if defined(WIN64) || defined(_WIN64)
5695 "win64",
5696#endif
5697#ifdef EBCDIC
5698 "ebcdic",
5699#endif
5700#ifndef CASE_INSENSITIVE_FILENAME
5701 "fname_case",
5702#endif
5703#ifdef HAVE_ACL
5704 "acl",
5705#endif
5706#ifdef FEAT_ARABIC
5707 "arabic",
5708#endif
5709#ifdef FEAT_AUTOCMD
5710 "autocmd",
5711#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01005712#ifdef FEAT_AUTOSERVERNAME
5713 "autoservername",
5714#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01005715#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005716 "balloon_eval",
5717# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
5718 "balloon_multiline",
5719# endif
5720#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01005721#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01005722 "balloon_eval_term",
5723#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005724#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
5725 "builtin_terms",
5726# ifdef ALL_BUILTIN_TCAPS
5727 "all_builtin_terms",
5728# endif
5729#endif
5730#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
5731 || defined(FEAT_GUI_W32) \
5732 || defined(FEAT_GUI_MOTIF))
5733 "browsefilter",
5734#endif
5735#ifdef FEAT_BYTEOFF
5736 "byte_offset",
5737#endif
5738#ifdef FEAT_JOB_CHANNEL
5739 "channel",
5740#endif
5741#ifdef FEAT_CINDENT
5742 "cindent",
5743#endif
5744#ifdef FEAT_CLIENTSERVER
5745 "clientserver",
5746#endif
5747#ifdef FEAT_CLIPBOARD
5748 "clipboard",
5749#endif
5750#ifdef FEAT_CMDL_COMPL
5751 "cmdline_compl",
5752#endif
5753#ifdef FEAT_CMDHIST
5754 "cmdline_hist",
5755#endif
5756#ifdef FEAT_COMMENTS
5757 "comments",
5758#endif
5759#ifdef FEAT_CONCEAL
5760 "conceal",
5761#endif
5762#ifdef FEAT_CRYPT
5763 "cryptv",
5764 "crypt-blowfish",
5765 "crypt-blowfish2",
5766#endif
5767#ifdef FEAT_CSCOPE
5768 "cscope",
5769#endif
5770#ifdef FEAT_CURSORBIND
5771 "cursorbind",
5772#endif
5773#ifdef CURSOR_SHAPE
5774 "cursorshape",
5775#endif
5776#ifdef DEBUG
5777 "debug",
5778#endif
5779#ifdef FEAT_CON_DIALOG
5780 "dialog_con",
5781#endif
5782#ifdef FEAT_GUI_DIALOG
5783 "dialog_gui",
5784#endif
5785#ifdef FEAT_DIFF
5786 "diff",
5787#endif
5788#ifdef FEAT_DIGRAPHS
5789 "digraphs",
5790#endif
5791#ifdef FEAT_DIRECTX
5792 "directx",
5793#endif
5794#ifdef FEAT_DND
5795 "dnd",
5796#endif
5797#ifdef FEAT_EMACS_TAGS
5798 "emacs_tags",
5799#endif
5800 "eval", /* always present, of course! */
5801 "ex_extra", /* graduated feature */
5802#ifdef FEAT_SEARCH_EXTRA
5803 "extra_search",
5804#endif
5805#ifdef FEAT_FKMAP
5806 "farsi",
5807#endif
5808#ifdef FEAT_SEARCHPATH
5809 "file_in_path",
5810#endif
5811#ifdef FEAT_FILTERPIPE
5812 "filterpipe",
5813#endif
5814#ifdef FEAT_FIND_ID
5815 "find_in_path",
5816#endif
5817#ifdef FEAT_FLOAT
5818 "float",
5819#endif
5820#ifdef FEAT_FOLDING
5821 "folding",
5822#endif
5823#ifdef FEAT_FOOTER
5824 "footer",
5825#endif
5826#if !defined(USE_SYSTEM) && defined(UNIX)
5827 "fork",
5828#endif
5829#ifdef FEAT_GETTEXT
5830 "gettext",
5831#endif
5832#ifdef FEAT_GUI
5833 "gui",
5834#endif
5835#ifdef FEAT_GUI_ATHENA
5836# ifdef FEAT_GUI_NEXTAW
5837 "gui_neXtaw",
5838# else
5839 "gui_athena",
5840# endif
5841#endif
5842#ifdef FEAT_GUI_GTK
5843 "gui_gtk",
5844# ifdef USE_GTK3
5845 "gui_gtk3",
5846# else
5847 "gui_gtk2",
5848# endif
5849#endif
5850#ifdef FEAT_GUI_GNOME
5851 "gui_gnome",
5852#endif
5853#ifdef FEAT_GUI_MAC
5854 "gui_mac",
5855#endif
5856#ifdef FEAT_GUI_MOTIF
5857 "gui_motif",
5858#endif
5859#ifdef FEAT_GUI_PHOTON
5860 "gui_photon",
5861#endif
5862#ifdef FEAT_GUI_W32
5863 "gui_win32",
5864#endif
5865#ifdef FEAT_HANGULIN
5866 "hangul_input",
5867#endif
5868#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
5869 "iconv",
5870#endif
5871#ifdef FEAT_INS_EXPAND
5872 "insert_expand",
5873#endif
5874#ifdef FEAT_JOB_CHANNEL
5875 "job",
5876#endif
5877#ifdef FEAT_JUMPLIST
5878 "jumplist",
5879#endif
5880#ifdef FEAT_KEYMAP
5881 "keymap",
5882#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02005883 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005884#ifdef FEAT_LANGMAP
5885 "langmap",
5886#endif
5887#ifdef FEAT_LIBCALL
5888 "libcall",
5889#endif
5890#ifdef FEAT_LINEBREAK
5891 "linebreak",
5892#endif
5893#ifdef FEAT_LISP
5894 "lispindent",
5895#endif
5896#ifdef FEAT_LISTCMDS
5897 "listcmds",
5898#endif
5899#ifdef FEAT_LOCALMAP
5900 "localmap",
5901#endif
5902#ifdef FEAT_LUA
5903# ifndef DYNAMIC_LUA
5904 "lua",
5905# endif
5906#endif
5907#ifdef FEAT_MENU
5908 "menu",
5909#endif
5910#ifdef FEAT_SESSION
5911 "mksession",
5912#endif
5913#ifdef FEAT_MODIFY_FNAME
5914 "modify_fname",
5915#endif
5916#ifdef FEAT_MOUSE
5917 "mouse",
5918#endif
5919#ifdef FEAT_MOUSESHAPE
5920 "mouseshape",
5921#endif
5922#if defined(UNIX) || defined(VMS)
5923# ifdef FEAT_MOUSE_DEC
5924 "mouse_dec",
5925# endif
5926# ifdef FEAT_MOUSE_GPM
5927 "mouse_gpm",
5928# endif
5929# ifdef FEAT_MOUSE_JSB
5930 "mouse_jsbterm",
5931# endif
5932# ifdef FEAT_MOUSE_NET
5933 "mouse_netterm",
5934# endif
5935# ifdef FEAT_MOUSE_PTERM
5936 "mouse_pterm",
5937# endif
5938# ifdef FEAT_MOUSE_SGR
5939 "mouse_sgr",
5940# endif
5941# ifdef FEAT_SYSMOUSE
5942 "mouse_sysmouse",
5943# endif
5944# ifdef FEAT_MOUSE_URXVT
5945 "mouse_urxvt",
5946# endif
5947# ifdef FEAT_MOUSE_XTERM
5948 "mouse_xterm",
5949# endif
5950#endif
5951#ifdef FEAT_MBYTE
5952 "multi_byte",
5953#endif
5954#ifdef FEAT_MBYTE_IME
5955 "multi_byte_ime",
5956#endif
5957#ifdef FEAT_MULTI_LANG
5958 "multi_lang",
5959#endif
5960#ifdef FEAT_MZSCHEME
5961#ifndef DYNAMIC_MZSCHEME
5962 "mzscheme",
5963#endif
5964#endif
5965#ifdef FEAT_NUM64
5966 "num64",
5967#endif
5968#ifdef FEAT_OLE
5969 "ole",
5970#endif
5971 "packages",
5972#ifdef FEAT_PATH_EXTRA
5973 "path_extra",
5974#endif
5975#ifdef FEAT_PERL
5976#ifndef DYNAMIC_PERL
5977 "perl",
5978#endif
5979#endif
5980#ifdef FEAT_PERSISTENT_UNDO
5981 "persistent_undo",
5982#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01005983#if defined(FEAT_PYTHON)
5984 "python_compiled",
5985# if defined(DYNAMIC_PYTHON)
5986 "python_dynamic",
5987# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005988 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005989 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01005990# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005991#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01005992#if defined(FEAT_PYTHON3)
5993 "python3_compiled",
5994# if defined(DYNAMIC_PYTHON3)
5995 "python3_dynamic",
5996# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005997 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005998 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01005999# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006000#endif
6001#ifdef FEAT_POSTSCRIPT
6002 "postscript",
6003#endif
6004#ifdef FEAT_PRINTER
6005 "printer",
6006#endif
6007#ifdef FEAT_PROFILE
6008 "profile",
6009#endif
6010#ifdef FEAT_RELTIME
6011 "reltime",
6012#endif
6013#ifdef FEAT_QUICKFIX
6014 "quickfix",
6015#endif
6016#ifdef FEAT_RIGHTLEFT
6017 "rightleft",
6018#endif
6019#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6020 "ruby",
6021#endif
6022#ifdef FEAT_SCROLLBIND
6023 "scrollbind",
6024#endif
6025#ifdef FEAT_CMDL_INFO
6026 "showcmd",
6027 "cmdline_info",
6028#endif
6029#ifdef FEAT_SIGNS
6030 "signs",
6031#endif
6032#ifdef FEAT_SMARTINDENT
6033 "smartindent",
6034#endif
6035#ifdef STARTUPTIME
6036 "startuptime",
6037#endif
6038#ifdef FEAT_STL_OPT
6039 "statusline",
6040#endif
6041#ifdef FEAT_SUN_WORKSHOP
6042 "sun_workshop",
6043#endif
6044#ifdef FEAT_NETBEANS_INTG
6045 "netbeans_intg",
6046#endif
6047#ifdef FEAT_SPELL
6048 "spell",
6049#endif
6050#ifdef FEAT_SYN_HL
6051 "syntax",
6052#endif
6053#if defined(USE_SYSTEM) || !defined(UNIX)
6054 "system",
6055#endif
6056#ifdef FEAT_TAG_BINS
6057 "tag_binary",
6058#endif
6059#ifdef FEAT_TAG_OLDSTATIC
6060 "tag_old_static",
6061#endif
6062#ifdef FEAT_TAG_ANYWHITE
6063 "tag_any_white",
6064#endif
6065#ifdef FEAT_TCL
6066# ifndef DYNAMIC_TCL
6067 "tcl",
6068# endif
6069#endif
6070#ifdef FEAT_TERMGUICOLORS
6071 "termguicolors",
6072#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006073#if defined(FEAT_TERMINAL) && !defined(WIN3264)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006074 "terminal",
6075#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006076#ifdef TERMINFO
6077 "terminfo",
6078#endif
6079#ifdef FEAT_TERMRESPONSE
6080 "termresponse",
6081#endif
6082#ifdef FEAT_TEXTOBJ
6083 "textobjects",
6084#endif
6085#ifdef HAVE_TGETENT
6086 "tgetent",
6087#endif
6088#ifdef FEAT_TIMERS
6089 "timers",
6090#endif
6091#ifdef FEAT_TITLE
6092 "title",
6093#endif
6094#ifdef FEAT_TOOLBAR
6095 "toolbar",
6096#endif
6097#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6098 "unnamedplus",
6099#endif
6100#ifdef FEAT_USR_CMDS
6101 "user-commands", /* was accidentally included in 5.4 */
6102 "user_commands",
6103#endif
6104#ifdef FEAT_VIMINFO
6105 "viminfo",
6106#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006107 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006108#ifdef FEAT_VIRTUALEDIT
6109 "virtualedit",
6110#endif
6111 "visual",
6112#ifdef FEAT_VISUALEXTRA
6113 "visualextra",
6114#endif
6115#ifdef FEAT_VREPLACE
6116 "vreplace",
6117#endif
6118#ifdef FEAT_WILDIGN
6119 "wildignore",
6120#endif
6121#ifdef FEAT_WILDMENU
6122 "wildmenu",
6123#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006124 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006125#ifdef FEAT_WAK
6126 "winaltkeys",
6127#endif
6128#ifdef FEAT_WRITEBACKUP
6129 "writebackup",
6130#endif
6131#ifdef FEAT_XIM
6132 "xim",
6133#endif
6134#ifdef FEAT_XFONTSET
6135 "xfontset",
6136#endif
6137#ifdef FEAT_XPM_W32
6138 "xpm",
6139 "xpm_w32", /* for backward compatibility */
6140#else
6141# if defined(HAVE_XPM)
6142 "xpm",
6143# endif
6144#endif
6145#ifdef USE_XSMP
6146 "xsmp",
6147#endif
6148#ifdef USE_XSMP_INTERACT
6149 "xsmp_interact",
6150#endif
6151#ifdef FEAT_XCLIPBOARD
6152 "xterm_clipboard",
6153#endif
6154#ifdef FEAT_XTERM_SAVE
6155 "xterm_save",
6156#endif
6157#if defined(UNIX) && defined(FEAT_X11)
6158 "X11",
6159#endif
6160 NULL
6161 };
6162
6163 name = get_tv_string(&argvars[0]);
6164 for (i = 0; has_list[i] != NULL; ++i)
6165 if (STRICMP(name, has_list[i]) == 0)
6166 {
6167 n = TRUE;
6168 break;
6169 }
6170
6171 if (n == FALSE)
6172 {
6173 if (STRNICMP(name, "patch", 5) == 0)
6174 {
6175 if (name[5] == '-'
6176 && STRLEN(name) >= 11
6177 && vim_isdigit(name[6])
6178 && vim_isdigit(name[8])
6179 && vim_isdigit(name[10]))
6180 {
6181 int major = atoi((char *)name + 6);
6182 int minor = atoi((char *)name + 8);
6183
6184 /* Expect "patch-9.9.01234". */
6185 n = (major < VIM_VERSION_MAJOR
6186 || (major == VIM_VERSION_MAJOR
6187 && (minor < VIM_VERSION_MINOR
6188 || (minor == VIM_VERSION_MINOR
6189 && has_patch(atoi((char *)name + 10))))));
6190 }
6191 else
6192 n = has_patch(atoi((char *)name + 5));
6193 }
6194 else if (STRICMP(name, "vim_starting") == 0)
6195 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006196 else if (STRICMP(name, "ttyin") == 0)
6197 n = mch_input_isatty();
6198 else if (STRICMP(name, "ttyout") == 0)
6199 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006200#ifdef FEAT_MBYTE
6201 else if (STRICMP(name, "multi_byte_encoding") == 0)
6202 n = has_mbyte;
6203#endif
6204#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
6205 else if (STRICMP(name, "balloon_multiline") == 0)
6206 n = multiline_balloon_available();
6207#endif
6208#ifdef DYNAMIC_TCL
6209 else if (STRICMP(name, "tcl") == 0)
6210 n = tcl_enabled(FALSE);
6211#endif
6212#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6213 else if (STRICMP(name, "iconv") == 0)
6214 n = iconv_enabled(FALSE);
6215#endif
6216#ifdef DYNAMIC_LUA
6217 else if (STRICMP(name, "lua") == 0)
6218 n = lua_enabled(FALSE);
6219#endif
6220#ifdef DYNAMIC_MZSCHEME
6221 else if (STRICMP(name, "mzscheme") == 0)
6222 n = mzscheme_enabled(FALSE);
6223#endif
6224#ifdef DYNAMIC_RUBY
6225 else if (STRICMP(name, "ruby") == 0)
6226 n = ruby_enabled(FALSE);
6227#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006228#ifdef DYNAMIC_PYTHON
6229 else if (STRICMP(name, "python") == 0)
6230 n = python_enabled(FALSE);
6231#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006232#ifdef DYNAMIC_PYTHON3
6233 else if (STRICMP(name, "python3") == 0)
6234 n = python3_enabled(FALSE);
6235#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006236#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6237 else if (STRICMP(name, "pythonx") == 0)
6238 {
6239# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6240 if (p_pyx == 0)
6241 n = python3_enabled(FALSE) || python_enabled(FALSE);
6242 else if (p_pyx == 3)
6243 n = python3_enabled(FALSE);
6244 else if (p_pyx == 2)
6245 n = python_enabled(FALSE);
6246# elif defined(DYNAMIC_PYTHON)
6247 n = python_enabled(FALSE);
6248# elif defined(DYNAMIC_PYTHON3)
6249 n = python3_enabled(FALSE);
6250# endif
6251 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006252#endif
6253#ifdef DYNAMIC_PERL
6254 else if (STRICMP(name, "perl") == 0)
6255 n = perl_enabled(FALSE);
6256#endif
6257#ifdef FEAT_GUI
6258 else if (STRICMP(name, "gui_running") == 0)
6259 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006260# ifdef FEAT_BROWSE
6261 else if (STRICMP(name, "browse") == 0)
6262 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6263# endif
6264#endif
6265#ifdef FEAT_SYN_HL
6266 else if (STRICMP(name, "syntax_items") == 0)
6267 n = syntax_present(curwin);
6268#endif
6269#if defined(WIN3264)
6270 else if (STRICMP(name, "win95") == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006271 n = FALSE; /* Win9x is no more supported. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006272#endif
6273#ifdef FEAT_NETBEANS_INTG
6274 else if (STRICMP(name, "netbeans_enabled") == 0)
6275 n = netbeans_active();
6276#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006277#if defined(FEAT_TERMINAL) && defined(WIN3264)
6278 else if (STRICMP(name, "terminal") == 0)
6279 n = terminal_enabled();
6280#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006281 }
6282
6283 rettv->vval.v_number = n;
6284}
6285
6286/*
6287 * "has_key()" function
6288 */
6289 static void
6290f_has_key(typval_T *argvars, typval_T *rettv)
6291{
6292 if (argvars[0].v_type != VAR_DICT)
6293 {
6294 EMSG(_(e_dictreq));
6295 return;
6296 }
6297 if (argvars[0].vval.v_dict == NULL)
6298 return;
6299
6300 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
6301 get_tv_string(&argvars[1]), -1) != NULL;
6302}
6303
6304/*
6305 * "haslocaldir()" function
6306 */
6307 static void
6308f_haslocaldir(typval_T *argvars, typval_T *rettv)
6309{
6310 win_T *wp = NULL;
6311
6312 wp = find_tabwin(&argvars[0], &argvars[1]);
6313 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6314}
6315
6316/*
6317 * "hasmapto()" function
6318 */
6319 static void
6320f_hasmapto(typval_T *argvars, typval_T *rettv)
6321{
6322 char_u *name;
6323 char_u *mode;
6324 char_u buf[NUMBUFLEN];
6325 int abbr = FALSE;
6326
6327 name = get_tv_string(&argvars[0]);
6328 if (argvars[1].v_type == VAR_UNKNOWN)
6329 mode = (char_u *)"nvo";
6330 else
6331 {
6332 mode = get_tv_string_buf(&argvars[1], buf);
6333 if (argvars[2].v_type != VAR_UNKNOWN)
6334 abbr = (int)get_tv_number(&argvars[2]);
6335 }
6336
6337 if (map_to_exists(name, mode, abbr))
6338 rettv->vval.v_number = TRUE;
6339 else
6340 rettv->vval.v_number = FALSE;
6341}
6342
6343/*
6344 * "histadd()" function
6345 */
6346 static void
6347f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6348{
6349#ifdef FEAT_CMDHIST
6350 int histype;
6351 char_u *str;
6352 char_u buf[NUMBUFLEN];
6353#endif
6354
6355 rettv->vval.v_number = FALSE;
6356 if (check_restricted() || check_secure())
6357 return;
6358#ifdef FEAT_CMDHIST
6359 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6360 histype = str != NULL ? get_histtype(str) : -1;
6361 if (histype >= 0)
6362 {
6363 str = get_tv_string_buf(&argvars[1], buf);
6364 if (*str != NUL)
6365 {
6366 init_history();
6367 add_to_history(histype, str, FALSE, NUL);
6368 rettv->vval.v_number = TRUE;
6369 return;
6370 }
6371 }
6372#endif
6373}
6374
6375/*
6376 * "histdel()" function
6377 */
6378 static void
6379f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6380{
6381#ifdef FEAT_CMDHIST
6382 int n;
6383 char_u buf[NUMBUFLEN];
6384 char_u *str;
6385
6386 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6387 if (str == NULL)
6388 n = 0;
6389 else if (argvars[1].v_type == VAR_UNKNOWN)
6390 /* only one argument: clear entire history */
6391 n = clr_history(get_histtype(str));
6392 else if (argvars[1].v_type == VAR_NUMBER)
6393 /* index given: remove that entry */
6394 n = del_history_idx(get_histtype(str),
6395 (int)get_tv_number(&argvars[1]));
6396 else
6397 /* string given: remove all matching entries */
6398 n = del_history_entry(get_histtype(str),
6399 get_tv_string_buf(&argvars[1], buf));
6400 rettv->vval.v_number = n;
6401#endif
6402}
6403
6404/*
6405 * "histget()" function
6406 */
6407 static void
6408f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6409{
6410#ifdef FEAT_CMDHIST
6411 int type;
6412 int idx;
6413 char_u *str;
6414
6415 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6416 if (str == NULL)
6417 rettv->vval.v_string = NULL;
6418 else
6419 {
6420 type = get_histtype(str);
6421 if (argvars[1].v_type == VAR_UNKNOWN)
6422 idx = get_history_idx(type);
6423 else
6424 idx = (int)get_tv_number_chk(&argvars[1], NULL);
6425 /* -1 on type error */
6426 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6427 }
6428#else
6429 rettv->vval.v_string = NULL;
6430#endif
6431 rettv->v_type = VAR_STRING;
6432}
6433
6434/*
6435 * "histnr()" function
6436 */
6437 static void
6438f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6439{
6440 int i;
6441
6442#ifdef FEAT_CMDHIST
6443 char_u *history = get_tv_string_chk(&argvars[0]);
6444
6445 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6446 if (i >= HIST_CMD && i < HIST_COUNT)
6447 i = get_history_idx(i);
6448 else
6449#endif
6450 i = -1;
6451 rettv->vval.v_number = i;
6452}
6453
6454/*
6455 * "highlightID(name)" function
6456 */
6457 static void
6458f_hlID(typval_T *argvars, typval_T *rettv)
6459{
6460 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
6461}
6462
6463/*
6464 * "highlight_exists()" function
6465 */
6466 static void
6467f_hlexists(typval_T *argvars, typval_T *rettv)
6468{
6469 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
6470}
6471
6472/*
6473 * "hostname()" function
6474 */
6475 static void
6476f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6477{
6478 char_u hostname[256];
6479
6480 mch_get_host_name(hostname, 256);
6481 rettv->v_type = VAR_STRING;
6482 rettv->vval.v_string = vim_strsave(hostname);
6483}
6484
6485/*
6486 * iconv() function
6487 */
6488 static void
6489f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6490{
6491#ifdef FEAT_MBYTE
6492 char_u buf1[NUMBUFLEN];
6493 char_u buf2[NUMBUFLEN];
6494 char_u *from, *to, *str;
6495 vimconv_T vimconv;
6496#endif
6497
6498 rettv->v_type = VAR_STRING;
6499 rettv->vval.v_string = NULL;
6500
6501#ifdef FEAT_MBYTE
6502 str = get_tv_string(&argvars[0]);
6503 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
6504 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
6505 vimconv.vc_type = CONV_NONE;
6506 convert_setup(&vimconv, from, to);
6507
6508 /* If the encodings are equal, no conversion needed. */
6509 if (vimconv.vc_type == CONV_NONE)
6510 rettv->vval.v_string = vim_strsave(str);
6511 else
6512 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6513
6514 convert_setup(&vimconv, NULL, NULL);
6515 vim_free(from);
6516 vim_free(to);
6517#endif
6518}
6519
6520/*
6521 * "indent()" function
6522 */
6523 static void
6524f_indent(typval_T *argvars, typval_T *rettv)
6525{
6526 linenr_T lnum;
6527
6528 lnum = get_tv_lnum(argvars);
6529 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6530 rettv->vval.v_number = get_indent_lnum(lnum);
6531 else
6532 rettv->vval.v_number = -1;
6533}
6534
6535/*
6536 * "index()" function
6537 */
6538 static void
6539f_index(typval_T *argvars, typval_T *rettv)
6540{
6541 list_T *l;
6542 listitem_T *item;
6543 long idx = 0;
6544 int ic = FALSE;
6545
6546 rettv->vval.v_number = -1;
6547 if (argvars[0].v_type != VAR_LIST)
6548 {
6549 EMSG(_(e_listreq));
6550 return;
6551 }
6552 l = argvars[0].vval.v_list;
6553 if (l != NULL)
6554 {
6555 item = l->lv_first;
6556 if (argvars[2].v_type != VAR_UNKNOWN)
6557 {
6558 int error = FALSE;
6559
6560 /* Start at specified item. Use the cached index that list_find()
6561 * sets, so that a negative number also works. */
6562 item = list_find(l, (long)get_tv_number_chk(&argvars[2], &error));
6563 idx = l->lv_idx;
6564 if (argvars[3].v_type != VAR_UNKNOWN)
6565 ic = (int)get_tv_number_chk(&argvars[3], &error);
6566 if (error)
6567 item = NULL;
6568 }
6569
6570 for ( ; item != NULL; item = item->li_next, ++idx)
6571 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6572 {
6573 rettv->vval.v_number = idx;
6574 break;
6575 }
6576 }
6577}
6578
6579static int inputsecret_flag = 0;
6580
6581/*
6582 * "input()" function
6583 * Also handles inputsecret() when inputsecret is set.
6584 */
6585 static void
6586f_input(typval_T *argvars, typval_T *rettv)
6587{
6588 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6589}
6590
6591/*
6592 * "inputdialog()" function
6593 */
6594 static void
6595f_inputdialog(typval_T *argvars, typval_T *rettv)
6596{
6597#if defined(FEAT_GUI_TEXTDIALOG)
6598 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
6599 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6600 {
6601 char_u *message;
6602 char_u buf[NUMBUFLEN];
6603 char_u *defstr = (char_u *)"";
6604
6605 message = get_tv_string_chk(&argvars[0]);
6606 if (argvars[1].v_type != VAR_UNKNOWN
6607 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
6608 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6609 else
6610 IObuff[0] = NUL;
6611 if (message != NULL && defstr != NULL
6612 && do_dialog(VIM_QUESTION, NULL, message,
6613 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6614 rettv->vval.v_string = vim_strsave(IObuff);
6615 else
6616 {
6617 if (message != NULL && defstr != NULL
6618 && argvars[1].v_type != VAR_UNKNOWN
6619 && argvars[2].v_type != VAR_UNKNOWN)
6620 rettv->vval.v_string = vim_strsave(
6621 get_tv_string_buf(&argvars[2], buf));
6622 else
6623 rettv->vval.v_string = NULL;
6624 }
6625 rettv->v_type = VAR_STRING;
6626 }
6627 else
6628#endif
6629 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6630}
6631
6632/*
6633 * "inputlist()" function
6634 */
6635 static void
6636f_inputlist(typval_T *argvars, typval_T *rettv)
6637{
6638 listitem_T *li;
6639 int selected;
6640 int mouse_used;
6641
6642#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02006643 /* While starting up, there is no place to enter text. When running tests
6644 * with --not-a-term we assume feedkeys() will be used. */
6645 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006646 return;
6647#endif
6648 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6649 {
6650 EMSG2(_(e_listarg), "inputlist()");
6651 return;
6652 }
6653
6654 msg_start();
6655 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
6656 lines_left = Rows; /* avoid more prompt */
6657 msg_scroll = TRUE;
6658 msg_clr_eos();
6659
6660 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
6661 {
6662 msg_puts(get_tv_string(&li->li_tv));
6663 msg_putchar('\n');
6664 }
6665
6666 /* Ask for choice. */
6667 selected = prompt_for_number(&mouse_used);
6668 if (mouse_used)
6669 selected -= lines_left;
6670
6671 rettv->vval.v_number = selected;
6672}
6673
6674
6675static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
6676
6677/*
6678 * "inputrestore()" function
6679 */
6680 static void
6681f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
6682{
6683 if (ga_userinput.ga_len > 0)
6684 {
6685 --ga_userinput.ga_len;
6686 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
6687 + ga_userinput.ga_len);
6688 /* default return is zero == OK */
6689 }
6690 else if (p_verbose > 1)
6691 {
6692 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
6693 rettv->vval.v_number = 1; /* Failed */
6694 }
6695}
6696
6697/*
6698 * "inputsave()" function
6699 */
6700 static void
6701f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
6702{
6703 /* Add an entry to the stack of typeahead storage. */
6704 if (ga_grow(&ga_userinput, 1) == OK)
6705 {
6706 save_typeahead((tasave_T *)(ga_userinput.ga_data)
6707 + ga_userinput.ga_len);
6708 ++ga_userinput.ga_len;
6709 /* default return is zero == OK */
6710 }
6711 else
6712 rettv->vval.v_number = 1; /* Failed */
6713}
6714
6715/*
6716 * "inputsecret()" function
6717 */
6718 static void
6719f_inputsecret(typval_T *argvars, typval_T *rettv)
6720{
6721 ++cmdline_star;
6722 ++inputsecret_flag;
6723 f_input(argvars, rettv);
6724 --cmdline_star;
6725 --inputsecret_flag;
6726}
6727
6728/*
6729 * "insert()" function
6730 */
6731 static void
6732f_insert(typval_T *argvars, typval_T *rettv)
6733{
6734 long before = 0;
6735 listitem_T *item;
6736 list_T *l;
6737 int error = FALSE;
6738
6739 if (argvars[0].v_type != VAR_LIST)
6740 EMSG2(_(e_listarg), "insert()");
6741 else if ((l = argvars[0].vval.v_list) != NULL
6742 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
6743 {
6744 if (argvars[2].v_type != VAR_UNKNOWN)
6745 before = (long)get_tv_number_chk(&argvars[2], &error);
6746 if (error)
6747 return; /* type error; errmsg already given */
6748
6749 if (before == l->lv_len)
6750 item = NULL;
6751 else
6752 {
6753 item = list_find(l, before);
6754 if (item == NULL)
6755 {
6756 EMSGN(_(e_listidx), before);
6757 l = NULL;
6758 }
6759 }
6760 if (l != NULL)
6761 {
6762 list_insert_tv(l, &argvars[1], item);
6763 copy_tv(&argvars[0], rettv);
6764 }
6765 }
6766}
6767
6768/*
6769 * "invert(expr)" function
6770 */
6771 static void
6772f_invert(typval_T *argvars, typval_T *rettv)
6773{
6774 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
6775}
6776
6777/*
6778 * "isdirectory()" function
6779 */
6780 static void
6781f_isdirectory(typval_T *argvars, typval_T *rettv)
6782{
6783 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
6784}
6785
6786/*
6787 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
6788 * or it refers to a List or Dictionary that is locked.
6789 */
6790 static int
6791tv_islocked(typval_T *tv)
6792{
6793 return (tv->v_lock & VAR_LOCKED)
6794 || (tv->v_type == VAR_LIST
6795 && tv->vval.v_list != NULL
6796 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
6797 || (tv->v_type == VAR_DICT
6798 && tv->vval.v_dict != NULL
6799 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
6800}
6801
6802/*
6803 * "islocked()" function
6804 */
6805 static void
6806f_islocked(typval_T *argvars, typval_T *rettv)
6807{
6808 lval_T lv;
6809 char_u *end;
6810 dictitem_T *di;
6811
6812 rettv->vval.v_number = -1;
6813 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01006814 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006815 if (end != NULL && lv.ll_name != NULL)
6816 {
6817 if (*end != NUL)
6818 EMSG(_(e_trailing));
6819 else
6820 {
6821 if (lv.ll_tv == NULL)
6822 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006823 di = find_var(lv.ll_name, NULL, TRUE);
6824 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006825 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006826 /* Consider a variable locked when:
6827 * 1. the variable itself is locked
6828 * 2. the value of the variable is locked.
6829 * 3. the List or Dict value is locked.
6830 */
6831 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
6832 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006833 }
6834 }
6835 else if (lv.ll_range)
6836 EMSG(_("E786: Range not allowed"));
6837 else if (lv.ll_newkey != NULL)
6838 EMSG2(_(e_dictkey), lv.ll_newkey);
6839 else if (lv.ll_list != NULL)
6840 /* List item. */
6841 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
6842 else
6843 /* Dictionary item. */
6844 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
6845 }
6846 }
6847
6848 clear_lval(&lv);
6849}
6850
6851#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
6852/*
6853 * "isnan()" function
6854 */
6855 static void
6856f_isnan(typval_T *argvars, typval_T *rettv)
6857{
6858 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
6859 && isnan(argvars[0].vval.v_float);
6860}
6861#endif
6862
6863/*
6864 * "items(dict)" function
6865 */
6866 static void
6867f_items(typval_T *argvars, typval_T *rettv)
6868{
6869 dict_list(argvars, rettv, 2);
6870}
6871
6872#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
6873/*
6874 * Get the job from the argument.
6875 * Returns NULL if the job is invalid.
6876 */
6877 static job_T *
6878get_job_arg(typval_T *tv)
6879{
6880 job_T *job;
6881
6882 if (tv->v_type != VAR_JOB)
6883 {
6884 EMSG2(_(e_invarg2), get_tv_string(tv));
6885 return NULL;
6886 }
6887 job = tv->vval.v_job;
6888
6889 if (job == NULL)
6890 EMSG(_("E916: not a valid job"));
6891 return job;
6892}
6893
6894/*
6895 * "job_getchannel()" function
6896 */
6897 static void
6898f_job_getchannel(typval_T *argvars, typval_T *rettv)
6899{
6900 job_T *job = get_job_arg(&argvars[0]);
6901
6902 if (job != NULL)
6903 {
6904 rettv->v_type = VAR_CHANNEL;
6905 rettv->vval.v_channel = job->jv_channel;
6906 if (job->jv_channel != NULL)
6907 ++job->jv_channel->ch_refcount;
6908 }
6909}
6910
6911/*
6912 * "job_info()" function
6913 */
6914 static void
6915f_job_info(typval_T *argvars, typval_T *rettv)
6916{
6917 job_T *job = get_job_arg(&argvars[0]);
6918
6919 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
6920 job_info(job, rettv->vval.v_dict);
6921}
6922
6923/*
6924 * "job_setoptions()" function
6925 */
6926 static void
6927f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
6928{
6929 job_T *job = get_job_arg(&argvars[0]);
6930 jobopt_T opt;
6931
6932 if (job == NULL)
6933 return;
6934 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02006935 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006936 job_set_options(job, &opt);
6937 free_job_options(&opt);
6938}
6939
6940/*
6941 * "job_start()" function
6942 */
6943 static void
6944f_job_start(typval_T *argvars, typval_T *rettv)
6945{
6946 rettv->v_type = VAR_JOB;
6947 if (check_restricted() || check_secure())
6948 return;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006949 rettv->vval.v_job = job_start(argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006950}
6951
6952/*
6953 * "job_status()" function
6954 */
6955 static void
6956f_job_status(typval_T *argvars, typval_T *rettv)
6957{
6958 job_T *job = get_job_arg(&argvars[0]);
6959
6960 if (job != NULL)
6961 {
6962 rettv->v_type = VAR_STRING;
6963 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
6964 }
6965}
6966
6967/*
6968 * "job_stop()" function
6969 */
6970 static void
6971f_job_stop(typval_T *argvars, typval_T *rettv)
6972{
6973 job_T *job = get_job_arg(&argvars[0]);
6974
6975 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02006976 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006977}
6978#endif
6979
6980/*
6981 * "join()" function
6982 */
6983 static void
6984f_join(typval_T *argvars, typval_T *rettv)
6985{
6986 garray_T ga;
6987 char_u *sep;
6988
6989 if (argvars[0].v_type != VAR_LIST)
6990 {
6991 EMSG(_(e_listreq));
6992 return;
6993 }
6994 if (argvars[0].vval.v_list == NULL)
6995 return;
6996 if (argvars[1].v_type == VAR_UNKNOWN)
6997 sep = (char_u *)" ";
6998 else
6999 sep = get_tv_string_chk(&argvars[1]);
7000
7001 rettv->v_type = VAR_STRING;
7002
7003 if (sep != NULL)
7004 {
7005 ga_init2(&ga, (int)sizeof(char), 80);
7006 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7007 ga_append(&ga, NUL);
7008 rettv->vval.v_string = (char_u *)ga.ga_data;
7009 }
7010 else
7011 rettv->vval.v_string = NULL;
7012}
7013
7014/*
7015 * "js_decode()" function
7016 */
7017 static void
7018f_js_decode(typval_T *argvars, typval_T *rettv)
7019{
7020 js_read_T reader;
7021
7022 reader.js_buf = get_tv_string(&argvars[0]);
7023 reader.js_fill = NULL;
7024 reader.js_used = 0;
7025 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
7026 EMSG(_(e_invarg));
7027}
7028
7029/*
7030 * "js_encode()" function
7031 */
7032 static void
7033f_js_encode(typval_T *argvars, typval_T *rettv)
7034{
7035 rettv->v_type = VAR_STRING;
7036 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7037}
7038
7039/*
7040 * "json_decode()" function
7041 */
7042 static void
7043f_json_decode(typval_T *argvars, typval_T *rettv)
7044{
7045 js_read_T reader;
7046
7047 reader.js_buf = get_tv_string(&argvars[0]);
7048 reader.js_fill = NULL;
7049 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007050 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007051}
7052
7053/*
7054 * "json_encode()" function
7055 */
7056 static void
7057f_json_encode(typval_T *argvars, typval_T *rettv)
7058{
7059 rettv->v_type = VAR_STRING;
7060 rettv->vval.v_string = json_encode(&argvars[0], 0);
7061}
7062
7063/*
7064 * "keys()" function
7065 */
7066 static void
7067f_keys(typval_T *argvars, typval_T *rettv)
7068{
7069 dict_list(argvars, rettv, 0);
7070}
7071
7072/*
7073 * "last_buffer_nr()" function.
7074 */
7075 static void
7076f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7077{
7078 int n = 0;
7079 buf_T *buf;
7080
Bram Moolenaar29323592016-07-24 22:04:11 +02007081 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007082 if (n < buf->b_fnum)
7083 n = buf->b_fnum;
7084
7085 rettv->vval.v_number = n;
7086}
7087
7088/*
7089 * "len()" function
7090 */
7091 static void
7092f_len(typval_T *argvars, typval_T *rettv)
7093{
7094 switch (argvars[0].v_type)
7095 {
7096 case VAR_STRING:
7097 case VAR_NUMBER:
7098 rettv->vval.v_number = (varnumber_T)STRLEN(
7099 get_tv_string(&argvars[0]));
7100 break;
7101 case VAR_LIST:
7102 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7103 break;
7104 case VAR_DICT:
7105 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7106 break;
7107 case VAR_UNKNOWN:
7108 case VAR_SPECIAL:
7109 case VAR_FLOAT:
7110 case VAR_FUNC:
7111 case VAR_PARTIAL:
7112 case VAR_JOB:
7113 case VAR_CHANNEL:
7114 EMSG(_("E701: Invalid type for len()"));
7115 break;
7116 }
7117}
7118
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007119 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007120libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007121{
7122#ifdef FEAT_LIBCALL
7123 char_u *string_in;
7124 char_u **string_result;
7125 int nr_result;
7126#endif
7127
7128 rettv->v_type = type;
7129 if (type != VAR_NUMBER)
7130 rettv->vval.v_string = NULL;
7131
7132 if (check_restricted() || check_secure())
7133 return;
7134
7135#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007136 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007137 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7138 {
7139 string_in = NULL;
7140 if (argvars[2].v_type == VAR_STRING)
7141 string_in = argvars[2].vval.v_string;
7142 if (type == VAR_NUMBER)
7143 string_result = NULL;
7144 else
7145 string_result = &rettv->vval.v_string;
7146 if (mch_libcall(argvars[0].vval.v_string,
7147 argvars[1].vval.v_string,
7148 string_in,
7149 argvars[2].vval.v_number,
7150 string_result,
7151 &nr_result) == OK
7152 && type == VAR_NUMBER)
7153 rettv->vval.v_number = nr_result;
7154 }
7155#endif
7156}
7157
7158/*
7159 * "libcall()" function
7160 */
7161 static void
7162f_libcall(typval_T *argvars, typval_T *rettv)
7163{
7164 libcall_common(argvars, rettv, VAR_STRING);
7165}
7166
7167/*
7168 * "libcallnr()" function
7169 */
7170 static void
7171f_libcallnr(typval_T *argvars, typval_T *rettv)
7172{
7173 libcall_common(argvars, rettv, VAR_NUMBER);
7174}
7175
7176/*
7177 * "line(string)" function
7178 */
7179 static void
7180f_line(typval_T *argvars, typval_T *rettv)
7181{
7182 linenr_T lnum = 0;
7183 pos_T *fp;
7184 int fnum;
7185
7186 fp = var2fpos(&argvars[0], TRUE, &fnum);
7187 if (fp != NULL)
7188 lnum = fp->lnum;
7189 rettv->vval.v_number = lnum;
7190}
7191
7192/*
7193 * "line2byte(lnum)" function
7194 */
7195 static void
7196f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7197{
7198#ifndef FEAT_BYTEOFF
7199 rettv->vval.v_number = -1;
7200#else
7201 linenr_T lnum;
7202
7203 lnum = get_tv_lnum(argvars);
7204 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7205 rettv->vval.v_number = -1;
7206 else
7207 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7208 if (rettv->vval.v_number >= 0)
7209 ++rettv->vval.v_number;
7210#endif
7211}
7212
7213/*
7214 * "lispindent(lnum)" function
7215 */
7216 static void
7217f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7218{
7219#ifdef FEAT_LISP
7220 pos_T pos;
7221 linenr_T lnum;
7222
7223 pos = curwin->w_cursor;
7224 lnum = get_tv_lnum(argvars);
7225 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7226 {
7227 curwin->w_cursor.lnum = lnum;
7228 rettv->vval.v_number = get_lisp_indent();
7229 curwin->w_cursor = pos;
7230 }
7231 else
7232#endif
7233 rettv->vval.v_number = -1;
7234}
7235
7236/*
7237 * "localtime()" function
7238 */
7239 static void
7240f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7241{
7242 rettv->vval.v_number = (varnumber_T)time(NULL);
7243}
7244
7245static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
7246
7247 static void
7248get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7249{
7250 char_u *keys;
7251 char_u *which;
7252 char_u buf[NUMBUFLEN];
7253 char_u *keys_buf = NULL;
7254 char_u *rhs;
7255 int mode;
7256 int abbr = FALSE;
7257 int get_dict = FALSE;
7258 mapblock_T *mp;
7259 int buffer_local;
7260
7261 /* return empty string for failure */
7262 rettv->v_type = VAR_STRING;
7263 rettv->vval.v_string = NULL;
7264
7265 keys = get_tv_string(&argvars[0]);
7266 if (*keys == NUL)
7267 return;
7268
7269 if (argvars[1].v_type != VAR_UNKNOWN)
7270 {
7271 which = get_tv_string_buf_chk(&argvars[1], buf);
7272 if (argvars[2].v_type != VAR_UNKNOWN)
7273 {
7274 abbr = (int)get_tv_number(&argvars[2]);
7275 if (argvars[3].v_type != VAR_UNKNOWN)
7276 get_dict = (int)get_tv_number(&argvars[3]);
7277 }
7278 }
7279 else
7280 which = (char_u *)"";
7281 if (which == NULL)
7282 return;
7283
7284 mode = get_map_mode(&which, 0);
7285
7286 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7287 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7288 vim_free(keys_buf);
7289
7290 if (!get_dict)
7291 {
7292 /* Return a string. */
7293 if (rhs != NULL)
7294 rettv->vval.v_string = str2special_save(rhs, FALSE);
7295
7296 }
7297 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7298 {
7299 /* Return a dictionary. */
7300 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7301 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7302 dict_T *dict = rettv->vval.v_dict;
7303
7304 dict_add_nr_str(dict, "lhs", 0L, lhs);
7305 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
7306 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
7307 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
7308 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
7309 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
7310 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
7311 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
7312 dict_add_nr_str(dict, "mode", 0L, mapmode);
7313
7314 vim_free(lhs);
7315 vim_free(mapmode);
7316 }
7317}
7318
7319#ifdef FEAT_FLOAT
7320/*
7321 * "log()" function
7322 */
7323 static void
7324f_log(typval_T *argvars, typval_T *rettv)
7325{
7326 float_T f = 0.0;
7327
7328 rettv->v_type = VAR_FLOAT;
7329 if (get_float_arg(argvars, &f) == OK)
7330 rettv->vval.v_float = log(f);
7331 else
7332 rettv->vval.v_float = 0.0;
7333}
7334
7335/*
7336 * "log10()" function
7337 */
7338 static void
7339f_log10(typval_T *argvars, typval_T *rettv)
7340{
7341 float_T f = 0.0;
7342
7343 rettv->v_type = VAR_FLOAT;
7344 if (get_float_arg(argvars, &f) == OK)
7345 rettv->vval.v_float = log10(f);
7346 else
7347 rettv->vval.v_float = 0.0;
7348}
7349#endif
7350
7351#ifdef FEAT_LUA
7352/*
7353 * "luaeval()" function
7354 */
7355 static void
7356f_luaeval(typval_T *argvars, typval_T *rettv)
7357{
7358 char_u *str;
7359 char_u buf[NUMBUFLEN];
7360
7361 str = get_tv_string_buf(&argvars[0], buf);
7362 do_luaeval(str, argvars + 1, rettv);
7363}
7364#endif
7365
7366/*
7367 * "map()" function
7368 */
7369 static void
7370f_map(typval_T *argvars, typval_T *rettv)
7371{
7372 filter_map(argvars, rettv, TRUE);
7373}
7374
7375/*
7376 * "maparg()" function
7377 */
7378 static void
7379f_maparg(typval_T *argvars, typval_T *rettv)
7380{
7381 get_maparg(argvars, rettv, TRUE);
7382}
7383
7384/*
7385 * "mapcheck()" function
7386 */
7387 static void
7388f_mapcheck(typval_T *argvars, typval_T *rettv)
7389{
7390 get_maparg(argvars, rettv, FALSE);
7391}
7392
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007393typedef enum
7394{
7395 MATCH_END, /* matchend() */
7396 MATCH_MATCH, /* match() */
7397 MATCH_STR, /* matchstr() */
7398 MATCH_LIST, /* matchlist() */
7399 MATCH_POS /* matchstrpos() */
7400} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007401
7402 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007403find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007404{
7405 char_u *str = NULL;
7406 long len = 0;
7407 char_u *expr = NULL;
7408 char_u *pat;
7409 regmatch_T regmatch;
7410 char_u patbuf[NUMBUFLEN];
7411 char_u strbuf[NUMBUFLEN];
7412 char_u *save_cpo;
7413 long start = 0;
7414 long nth = 1;
7415 colnr_T startcol = 0;
7416 int match = 0;
7417 list_T *l = NULL;
7418 listitem_T *li = NULL;
7419 long idx = 0;
7420 char_u *tofree = NULL;
7421
7422 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7423 save_cpo = p_cpo;
7424 p_cpo = (char_u *)"";
7425
7426 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007427 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007428 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007429 /* type MATCH_LIST: return empty list when there are no matches.
7430 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007431 if (rettv_list_alloc(rettv) == FAIL)
7432 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007433 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007434 && (list_append_string(rettv->vval.v_list,
7435 (char_u *)"", 0) == FAIL
7436 || list_append_number(rettv->vval.v_list,
7437 (varnumber_T)-1) == FAIL
7438 || list_append_number(rettv->vval.v_list,
7439 (varnumber_T)-1) == FAIL
7440 || list_append_number(rettv->vval.v_list,
7441 (varnumber_T)-1) == FAIL))
7442 {
7443 list_free(rettv->vval.v_list);
7444 rettv->vval.v_list = NULL;
7445 goto theend;
7446 }
7447 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007448 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007449 {
7450 rettv->v_type = VAR_STRING;
7451 rettv->vval.v_string = NULL;
7452 }
7453
7454 if (argvars[0].v_type == VAR_LIST)
7455 {
7456 if ((l = argvars[0].vval.v_list) == NULL)
7457 goto theend;
7458 li = l->lv_first;
7459 }
7460 else
7461 {
7462 expr = str = get_tv_string(&argvars[0]);
7463 len = (long)STRLEN(str);
7464 }
7465
7466 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
7467 if (pat == NULL)
7468 goto theend;
7469
7470 if (argvars[2].v_type != VAR_UNKNOWN)
7471 {
7472 int error = FALSE;
7473
7474 start = (long)get_tv_number_chk(&argvars[2], &error);
7475 if (error)
7476 goto theend;
7477 if (l != NULL)
7478 {
7479 li = list_find(l, start);
7480 if (li == NULL)
7481 goto theend;
7482 idx = l->lv_idx; /* use the cached index */
7483 }
7484 else
7485 {
7486 if (start < 0)
7487 start = 0;
7488 if (start > len)
7489 goto theend;
7490 /* When "count" argument is there ignore matches before "start",
7491 * otherwise skip part of the string. Differs when pattern is "^"
7492 * or "\<". */
7493 if (argvars[3].v_type != VAR_UNKNOWN)
7494 startcol = start;
7495 else
7496 {
7497 str += start;
7498 len -= start;
7499 }
7500 }
7501
7502 if (argvars[3].v_type != VAR_UNKNOWN)
7503 nth = (long)get_tv_number_chk(&argvars[3], &error);
7504 if (error)
7505 goto theend;
7506 }
7507
7508 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7509 if (regmatch.regprog != NULL)
7510 {
7511 regmatch.rm_ic = p_ic;
7512
7513 for (;;)
7514 {
7515 if (l != NULL)
7516 {
7517 if (li == NULL)
7518 {
7519 match = FALSE;
7520 break;
7521 }
7522 vim_free(tofree);
7523 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
7524 if (str == NULL)
7525 break;
7526 }
7527
7528 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
7529
7530 if (match && --nth <= 0)
7531 break;
7532 if (l == NULL && !match)
7533 break;
7534
7535 /* Advance to just after the match. */
7536 if (l != NULL)
7537 {
7538 li = li->li_next;
7539 ++idx;
7540 }
7541 else
7542 {
7543#ifdef FEAT_MBYTE
7544 startcol = (colnr_T)(regmatch.startp[0]
7545 + (*mb_ptr2len)(regmatch.startp[0]) - str);
7546#else
7547 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
7548#endif
7549 if (startcol > (colnr_T)len
7550 || str + startcol <= regmatch.startp[0])
7551 {
7552 match = FALSE;
7553 break;
7554 }
7555 }
7556 }
7557
7558 if (match)
7559 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007560 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007561 {
7562 listitem_T *li1 = rettv->vval.v_list->lv_first;
7563 listitem_T *li2 = li1->li_next;
7564 listitem_T *li3 = li2->li_next;
7565 listitem_T *li4 = li3->li_next;
7566
7567 vim_free(li1->li_tv.vval.v_string);
7568 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
7569 (int)(regmatch.endp[0] - regmatch.startp[0]));
7570 li3->li_tv.vval.v_number =
7571 (varnumber_T)(regmatch.startp[0] - expr);
7572 li4->li_tv.vval.v_number =
7573 (varnumber_T)(regmatch.endp[0] - expr);
7574 if (l != NULL)
7575 li2->li_tv.vval.v_number = (varnumber_T)idx;
7576 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007577 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007578 {
7579 int i;
7580
7581 /* return list with matched string and submatches */
7582 for (i = 0; i < NSUBEXP; ++i)
7583 {
7584 if (regmatch.endp[i] == NULL)
7585 {
7586 if (list_append_string(rettv->vval.v_list,
7587 (char_u *)"", 0) == FAIL)
7588 break;
7589 }
7590 else if (list_append_string(rettv->vval.v_list,
7591 regmatch.startp[i],
7592 (int)(regmatch.endp[i] - regmatch.startp[i]))
7593 == FAIL)
7594 break;
7595 }
7596 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007597 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007598 {
7599 /* return matched string */
7600 if (l != NULL)
7601 copy_tv(&li->li_tv, rettv);
7602 else
7603 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
7604 (int)(regmatch.endp[0] - regmatch.startp[0]));
7605 }
7606 else if (l != NULL)
7607 rettv->vval.v_number = idx;
7608 else
7609 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007610 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007611 rettv->vval.v_number =
7612 (varnumber_T)(regmatch.startp[0] - str);
7613 else
7614 rettv->vval.v_number =
7615 (varnumber_T)(regmatch.endp[0] - str);
7616 rettv->vval.v_number += (varnumber_T)(str - expr);
7617 }
7618 }
7619 vim_regfree(regmatch.regprog);
7620 }
7621
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007622theend:
7623 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007624 /* matchstrpos() without a list: drop the second item. */
7625 listitem_remove(rettv->vval.v_list,
7626 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007627 vim_free(tofree);
7628 p_cpo = save_cpo;
7629}
7630
7631/*
7632 * "match()" function
7633 */
7634 static void
7635f_match(typval_T *argvars, typval_T *rettv)
7636{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007637 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007638}
7639
7640/*
7641 * "matchadd()" function
7642 */
7643 static void
7644f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7645{
7646#ifdef FEAT_SEARCH_EXTRA
7647 char_u buf[NUMBUFLEN];
7648 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
7649 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
7650 int prio = 10; /* default priority */
7651 int id = -1;
7652 int error = FALSE;
7653 char_u *conceal_char = NULL;
7654
7655 rettv->vval.v_number = -1;
7656
7657 if (grp == NULL || pat == NULL)
7658 return;
7659 if (argvars[2].v_type != VAR_UNKNOWN)
7660 {
7661 prio = (int)get_tv_number_chk(&argvars[2], &error);
7662 if (argvars[3].v_type != VAR_UNKNOWN)
7663 {
7664 id = (int)get_tv_number_chk(&argvars[3], &error);
7665 if (argvars[4].v_type != VAR_UNKNOWN)
7666 {
7667 if (argvars[4].v_type != VAR_DICT)
7668 {
7669 EMSG(_(e_dictreq));
7670 return;
7671 }
7672 if (dict_find(argvars[4].vval.v_dict,
7673 (char_u *)"conceal", -1) != NULL)
7674 conceal_char = get_dict_string(argvars[4].vval.v_dict,
7675 (char_u *)"conceal", FALSE);
7676 }
7677 }
7678 }
7679 if (error == TRUE)
7680 return;
7681 if (id >= 1 && id <= 3)
7682 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02007683 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007684 return;
7685 }
7686
7687 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
7688 conceal_char);
7689#endif
7690}
7691
7692/*
7693 * "matchaddpos()" function
7694 */
7695 static void
7696f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7697{
7698#ifdef FEAT_SEARCH_EXTRA
7699 char_u buf[NUMBUFLEN];
7700 char_u *group;
7701 int prio = 10;
7702 int id = -1;
7703 int error = FALSE;
7704 list_T *l;
7705 char_u *conceal_char = NULL;
7706
7707 rettv->vval.v_number = -1;
7708
7709 group = get_tv_string_buf_chk(&argvars[0], buf);
7710 if (group == NULL)
7711 return;
7712
7713 if (argvars[1].v_type != VAR_LIST)
7714 {
7715 EMSG2(_(e_listarg), "matchaddpos()");
7716 return;
7717 }
7718 l = argvars[1].vval.v_list;
7719 if (l == NULL)
7720 return;
7721
7722 if (argvars[2].v_type != VAR_UNKNOWN)
7723 {
7724 prio = (int)get_tv_number_chk(&argvars[2], &error);
7725 if (argvars[3].v_type != VAR_UNKNOWN)
7726 {
7727 id = (int)get_tv_number_chk(&argvars[3], &error);
7728 if (argvars[4].v_type != VAR_UNKNOWN)
7729 {
7730 if (argvars[4].v_type != VAR_DICT)
7731 {
7732 EMSG(_(e_dictreq));
7733 return;
7734 }
7735 if (dict_find(argvars[4].vval.v_dict,
7736 (char_u *)"conceal", -1) != NULL)
7737 conceal_char = get_dict_string(argvars[4].vval.v_dict,
7738 (char_u *)"conceal", FALSE);
7739 }
7740 }
7741 }
7742 if (error == TRUE)
7743 return;
7744
7745 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
7746 if (id == 1 || id == 2)
7747 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02007748 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007749 return;
7750 }
7751
7752 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
7753 conceal_char);
7754#endif
7755}
7756
7757/*
7758 * "matcharg()" function
7759 */
7760 static void
7761f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
7762{
7763 if (rettv_list_alloc(rettv) == OK)
7764 {
7765#ifdef FEAT_SEARCH_EXTRA
7766 int id = (int)get_tv_number(&argvars[0]);
7767 matchitem_T *m;
7768
7769 if (id >= 1 && id <= 3)
7770 {
7771 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
7772 {
7773 list_append_string(rettv->vval.v_list,
7774 syn_id2name(m->hlg_id), -1);
7775 list_append_string(rettv->vval.v_list, m->pattern, -1);
7776 }
7777 else
7778 {
7779 list_append_string(rettv->vval.v_list, NULL, -1);
7780 list_append_string(rettv->vval.v_list, NULL, -1);
7781 }
7782 }
7783#endif
7784 }
7785}
7786
7787/*
7788 * "matchdelete()" function
7789 */
7790 static void
7791f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7792{
7793#ifdef FEAT_SEARCH_EXTRA
7794 rettv->vval.v_number = match_delete(curwin,
7795 (int)get_tv_number(&argvars[0]), TRUE);
7796#endif
7797}
7798
7799/*
7800 * "matchend()" function
7801 */
7802 static void
7803f_matchend(typval_T *argvars, typval_T *rettv)
7804{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007805 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007806}
7807
7808/*
7809 * "matchlist()" function
7810 */
7811 static void
7812f_matchlist(typval_T *argvars, typval_T *rettv)
7813{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007814 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007815}
7816
7817/*
7818 * "matchstr()" function
7819 */
7820 static void
7821f_matchstr(typval_T *argvars, typval_T *rettv)
7822{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007823 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007824}
7825
7826/*
7827 * "matchstrpos()" function
7828 */
7829 static void
7830f_matchstrpos(typval_T *argvars, typval_T *rettv)
7831{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007832 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007833}
7834
7835static void max_min(typval_T *argvars, typval_T *rettv, int domax);
7836
7837 static void
7838max_min(typval_T *argvars, typval_T *rettv, int domax)
7839{
7840 varnumber_T n = 0;
7841 varnumber_T i;
7842 int error = FALSE;
7843
7844 if (argvars[0].v_type == VAR_LIST)
7845 {
7846 list_T *l;
7847 listitem_T *li;
7848
7849 l = argvars[0].vval.v_list;
7850 if (l != NULL)
7851 {
7852 li = l->lv_first;
7853 if (li != NULL)
7854 {
7855 n = get_tv_number_chk(&li->li_tv, &error);
7856 for (;;)
7857 {
7858 li = li->li_next;
7859 if (li == NULL)
7860 break;
7861 i = get_tv_number_chk(&li->li_tv, &error);
7862 if (domax ? i > n : i < n)
7863 n = i;
7864 }
7865 }
7866 }
7867 }
7868 else if (argvars[0].v_type == VAR_DICT)
7869 {
7870 dict_T *d;
7871 int first = TRUE;
7872 hashitem_T *hi;
7873 int todo;
7874
7875 d = argvars[0].vval.v_dict;
7876 if (d != NULL)
7877 {
7878 todo = (int)d->dv_hashtab.ht_used;
7879 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
7880 {
7881 if (!HASHITEM_EMPTY(hi))
7882 {
7883 --todo;
7884 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
7885 if (first)
7886 {
7887 n = i;
7888 first = FALSE;
7889 }
7890 else if (domax ? i > n : i < n)
7891 n = i;
7892 }
7893 }
7894 }
7895 }
7896 else
Bram Moolenaar26b84332016-09-04 21:42:36 +02007897 EMSG2(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007898 rettv->vval.v_number = error ? 0 : n;
7899}
7900
7901/*
7902 * "max()" function
7903 */
7904 static void
7905f_max(typval_T *argvars, typval_T *rettv)
7906{
7907 max_min(argvars, rettv, TRUE);
7908}
7909
7910/*
7911 * "min()" function
7912 */
7913 static void
7914f_min(typval_T *argvars, typval_T *rettv)
7915{
7916 max_min(argvars, rettv, FALSE);
7917}
7918
7919static int mkdir_recurse(char_u *dir, int prot);
7920
7921/*
7922 * Create the directory in which "dir" is located, and higher levels when
7923 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02007924 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007925 */
7926 static int
7927mkdir_recurse(char_u *dir, int prot)
7928{
7929 char_u *p;
7930 char_u *updir;
7931 int r = FAIL;
7932
7933 /* Get end of directory name in "dir".
7934 * We're done when it's "/" or "c:/". */
7935 p = gettail_sep(dir);
7936 if (p <= get_past_head(dir))
7937 return OK;
7938
7939 /* If the directory exists we're done. Otherwise: create it.*/
7940 updir = vim_strnsave(dir, (int)(p - dir));
7941 if (updir == NULL)
7942 return FAIL;
7943 if (mch_isdir(updir))
7944 r = OK;
7945 else if (mkdir_recurse(updir, prot) == OK)
7946 r = vim_mkdir_emsg(updir, prot);
7947 vim_free(updir);
7948 return r;
7949}
7950
7951#ifdef vim_mkdir
7952/*
7953 * "mkdir()" function
7954 */
7955 static void
7956f_mkdir(typval_T *argvars, typval_T *rettv)
7957{
7958 char_u *dir;
7959 char_u buf[NUMBUFLEN];
7960 int prot = 0755;
7961
7962 rettv->vval.v_number = FAIL;
7963 if (check_restricted() || check_secure())
7964 return;
7965
7966 dir = get_tv_string_buf(&argvars[0], buf);
7967 if (*dir == NUL)
7968 rettv->vval.v_number = FAIL;
7969 else
7970 {
7971 if (*gettail(dir) == NUL)
7972 /* remove trailing slashes */
7973 *gettail_sep(dir) = NUL;
7974
7975 if (argvars[1].v_type != VAR_UNKNOWN)
7976 {
7977 if (argvars[2].v_type != VAR_UNKNOWN)
7978 prot = (int)get_tv_number_chk(&argvars[2], NULL);
7979 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
7980 mkdir_recurse(dir, prot);
7981 }
7982 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
7983 }
7984}
7985#endif
7986
7987/*
7988 * "mode()" function
7989 */
7990 static void
7991f_mode(typval_T *argvars, typval_T *rettv)
7992{
7993 char_u buf[3];
7994
7995 buf[1] = NUL;
7996 buf[2] = NUL;
7997
7998 if (time_for_testing == 93784)
7999 {
8000 /* Testing the two-character code. */
8001 buf[0] = 'x';
8002 buf[1] = '!';
8003 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008004#ifdef FEAT_TERMINAL
8005 else if (term_use_loop())
8006 buf[0] = 't';
8007#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008008 else if (VIsual_active)
8009 {
8010 if (VIsual_select)
8011 buf[0] = VIsual_mode + 's' - 'v';
8012 else
8013 buf[0] = VIsual_mode;
8014 }
8015 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8016 || State == CONFIRM)
8017 {
8018 buf[0] = 'r';
8019 if (State == ASKMORE)
8020 buf[1] = 'm';
8021 else if (State == CONFIRM)
8022 buf[1] = '?';
8023 }
8024 else if (State == EXTERNCMD)
8025 buf[0] = '!';
8026 else if (State & INSERT)
8027 {
8028#ifdef FEAT_VREPLACE
8029 if (State & VREPLACE_FLAG)
8030 {
8031 buf[0] = 'R';
8032 buf[1] = 'v';
8033 }
8034 else
8035#endif
Bram Moolenaare90858d2017-02-01 17:24:34 +01008036 {
8037 if (State & REPLACE_FLAG)
8038 buf[0] = 'R';
8039 else
8040 buf[0] = 'i';
8041#ifdef FEAT_INS_EXPAND
8042 if (ins_compl_active())
8043 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008044 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008045 buf[1] = 'x';
8046#endif
8047 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008048 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008049 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008050 {
8051 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008052 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008053 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008054 else if (exmode_active == EXMODE_NORMAL)
8055 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008056 }
8057 else
8058 {
8059 buf[0] = 'n';
8060 if (finish_op)
8061 buf[1] = 'o';
8062 }
8063
8064 /* Clear out the minor mode when the argument is not a non-zero number or
8065 * non-empty string. */
8066 if (!non_zero_arg(&argvars[0]))
8067 buf[1] = NUL;
8068
8069 rettv->vval.v_string = vim_strsave(buf);
8070 rettv->v_type = VAR_STRING;
8071}
8072
8073#if defined(FEAT_MZSCHEME) || defined(PROTO)
8074/*
8075 * "mzeval()" function
8076 */
8077 static void
8078f_mzeval(typval_T *argvars, typval_T *rettv)
8079{
8080 char_u *str;
8081 char_u buf[NUMBUFLEN];
8082
8083 str = get_tv_string_buf(&argvars[0], buf);
8084 do_mzeval(str, rettv);
8085}
8086
8087 void
8088mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8089{
8090 typval_T argvars[3];
8091
8092 argvars[0].v_type = VAR_STRING;
8093 argvars[0].vval.v_string = name;
8094 copy_tv(args, &argvars[1]);
8095 argvars[2].v_type = VAR_UNKNOWN;
8096 f_call(argvars, rettv);
8097 clear_tv(&argvars[1]);
8098}
8099#endif
8100
8101/*
8102 * "nextnonblank()" function
8103 */
8104 static void
8105f_nextnonblank(typval_T *argvars, typval_T *rettv)
8106{
8107 linenr_T lnum;
8108
8109 for (lnum = get_tv_lnum(argvars); ; ++lnum)
8110 {
8111 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8112 {
8113 lnum = 0;
8114 break;
8115 }
8116 if (*skipwhite(ml_get(lnum)) != NUL)
8117 break;
8118 }
8119 rettv->vval.v_number = lnum;
8120}
8121
8122/*
8123 * "nr2char()" function
8124 */
8125 static void
8126f_nr2char(typval_T *argvars, typval_T *rettv)
8127{
8128 char_u buf[NUMBUFLEN];
8129
8130#ifdef FEAT_MBYTE
8131 if (has_mbyte)
8132 {
8133 int utf8 = 0;
8134
8135 if (argvars[1].v_type != VAR_UNKNOWN)
8136 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
8137 if (utf8)
8138 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8139 else
8140 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8141 }
8142 else
8143#endif
8144 {
8145 buf[0] = (char_u)get_tv_number(&argvars[0]);
8146 buf[1] = NUL;
8147 }
8148 rettv->v_type = VAR_STRING;
8149 rettv->vval.v_string = vim_strsave(buf);
8150}
8151
8152/*
8153 * "or(expr, expr)" function
8154 */
8155 static void
8156f_or(typval_T *argvars, typval_T *rettv)
8157{
8158 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8159 | get_tv_number_chk(&argvars[1], NULL);
8160}
8161
8162/*
8163 * "pathshorten()" function
8164 */
8165 static void
8166f_pathshorten(typval_T *argvars, typval_T *rettv)
8167{
8168 char_u *p;
8169
8170 rettv->v_type = VAR_STRING;
8171 p = get_tv_string_chk(&argvars[0]);
8172 if (p == NULL)
8173 rettv->vval.v_string = NULL;
8174 else
8175 {
8176 p = vim_strsave(p);
8177 rettv->vval.v_string = p;
8178 if (p != NULL)
8179 shorten_dir(p);
8180 }
8181}
8182
8183#ifdef FEAT_PERL
8184/*
8185 * "perleval()" function
8186 */
8187 static void
8188f_perleval(typval_T *argvars, typval_T *rettv)
8189{
8190 char_u *str;
8191 char_u buf[NUMBUFLEN];
8192
8193 str = get_tv_string_buf(&argvars[0], buf);
8194 do_perleval(str, rettv);
8195}
8196#endif
8197
8198#ifdef FEAT_FLOAT
8199/*
8200 * "pow()" function
8201 */
8202 static void
8203f_pow(typval_T *argvars, typval_T *rettv)
8204{
8205 float_T fx = 0.0, fy = 0.0;
8206
8207 rettv->v_type = VAR_FLOAT;
8208 if (get_float_arg(argvars, &fx) == OK
8209 && get_float_arg(&argvars[1], &fy) == OK)
8210 rettv->vval.v_float = pow(fx, fy);
8211 else
8212 rettv->vval.v_float = 0.0;
8213}
8214#endif
8215
8216/*
8217 * "prevnonblank()" function
8218 */
8219 static void
8220f_prevnonblank(typval_T *argvars, typval_T *rettv)
8221{
8222 linenr_T lnum;
8223
8224 lnum = get_tv_lnum(argvars);
8225 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8226 lnum = 0;
8227 else
8228 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8229 --lnum;
8230 rettv->vval.v_number = lnum;
8231}
8232
8233/* This dummy va_list is here because:
8234 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8235 * - locally in the function results in a "used before set" warning
8236 * - using va_start() to initialize it gives "function with fixed args" error */
8237static va_list ap;
8238
8239/*
8240 * "printf()" function
8241 */
8242 static void
8243f_printf(typval_T *argvars, typval_T *rettv)
8244{
8245 char_u buf[NUMBUFLEN];
8246 int len;
8247 char_u *s;
8248 int saved_did_emsg = did_emsg;
8249 char *fmt;
8250
8251 rettv->v_type = VAR_STRING;
8252 rettv->vval.v_string = NULL;
8253
8254 /* Get the required length, allocate the buffer and do it for real. */
8255 did_emsg = FALSE;
8256 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008257 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008258 if (!did_emsg)
8259 {
8260 s = alloc(len + 1);
8261 if (s != NULL)
8262 {
8263 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008264 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8265 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008266 }
8267 }
8268 did_emsg |= saved_did_emsg;
8269}
8270
8271/*
8272 * "pumvisible()" function
8273 */
8274 static void
8275f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8276{
8277#ifdef FEAT_INS_EXPAND
8278 if (pum_visible())
8279 rettv->vval.v_number = 1;
8280#endif
8281}
8282
8283#ifdef FEAT_PYTHON3
8284/*
8285 * "py3eval()" function
8286 */
8287 static void
8288f_py3eval(typval_T *argvars, typval_T *rettv)
8289{
8290 char_u *str;
8291 char_u buf[NUMBUFLEN];
8292
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008293 if (p_pyx == 0)
8294 p_pyx = 3;
8295
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008296 str = get_tv_string_buf(&argvars[0], buf);
8297 do_py3eval(str, rettv);
8298}
8299#endif
8300
8301#ifdef FEAT_PYTHON
8302/*
8303 * "pyeval()" function
8304 */
8305 static void
8306f_pyeval(typval_T *argvars, typval_T *rettv)
8307{
8308 char_u *str;
8309 char_u buf[NUMBUFLEN];
8310
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008311 if (p_pyx == 0)
8312 p_pyx = 2;
8313
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008314 str = get_tv_string_buf(&argvars[0], buf);
8315 do_pyeval(str, rettv);
8316}
8317#endif
8318
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008319#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8320/*
8321 * "pyxeval()" function
8322 */
8323 static void
8324f_pyxeval(typval_T *argvars, typval_T *rettv)
8325{
8326# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8327 init_pyxversion();
8328 if (p_pyx == 2)
8329 f_pyeval(argvars, rettv);
8330 else
8331 f_py3eval(argvars, rettv);
8332# elif defined(FEAT_PYTHON)
8333 f_pyeval(argvars, rettv);
8334# elif defined(FEAT_PYTHON3)
8335 f_py3eval(argvars, rettv);
8336# endif
8337}
8338#endif
8339
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008340/*
8341 * "range()" function
8342 */
8343 static void
8344f_range(typval_T *argvars, typval_T *rettv)
8345{
8346 varnumber_T start;
8347 varnumber_T end;
8348 varnumber_T stride = 1;
8349 varnumber_T i;
8350 int error = FALSE;
8351
8352 start = get_tv_number_chk(&argvars[0], &error);
8353 if (argvars[1].v_type == VAR_UNKNOWN)
8354 {
8355 end = start - 1;
8356 start = 0;
8357 }
8358 else
8359 {
8360 end = get_tv_number_chk(&argvars[1], &error);
8361 if (argvars[2].v_type != VAR_UNKNOWN)
8362 stride = get_tv_number_chk(&argvars[2], &error);
8363 }
8364
8365 if (error)
8366 return; /* type error; errmsg already given */
8367 if (stride == 0)
8368 EMSG(_("E726: Stride is zero"));
8369 else if (stride > 0 ? end + 1 < start : end - 1 > start)
8370 EMSG(_("E727: Start past end"));
8371 else
8372 {
8373 if (rettv_list_alloc(rettv) == OK)
8374 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8375 if (list_append_number(rettv->vval.v_list,
8376 (varnumber_T)i) == FAIL)
8377 break;
8378 }
8379}
8380
8381/*
8382 * "readfile()" function
8383 */
8384 static void
8385f_readfile(typval_T *argvars, typval_T *rettv)
8386{
8387 int binary = FALSE;
8388 int failed = FALSE;
8389 char_u *fname;
8390 FILE *fd;
8391 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
8392 int io_size = sizeof(buf);
8393 int readlen; /* size of last fread() */
8394 char_u *prev = NULL; /* previously read bytes, if any */
8395 long prevlen = 0; /* length of data in prev */
8396 long prevsize = 0; /* size of prev buffer */
8397 long maxline = MAXLNUM;
8398 long cnt = 0;
8399 char_u *p; /* position in buf */
8400 char_u *start; /* start of current line */
8401
8402 if (argvars[1].v_type != VAR_UNKNOWN)
8403 {
8404 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
8405 binary = TRUE;
8406 if (argvars[2].v_type != VAR_UNKNOWN)
8407 maxline = (long)get_tv_number(&argvars[2]);
8408 }
8409
8410 if (rettv_list_alloc(rettv) == FAIL)
8411 return;
8412
8413 /* Always open the file in binary mode, library functions have a mind of
8414 * their own about CR-LF conversion. */
8415 fname = get_tv_string(&argvars[0]);
8416 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
8417 {
8418 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
8419 return;
8420 }
8421
8422 while (cnt < maxline || maxline < 0)
8423 {
8424 readlen = (int)fread(buf, 1, io_size, fd);
8425
8426 /* This for loop processes what was read, but is also entered at end
8427 * of file so that either:
8428 * - an incomplete line gets written
8429 * - a "binary" file gets an empty line at the end if it ends in a
8430 * newline. */
8431 for (p = buf, start = buf;
8432 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
8433 ++p)
8434 {
8435 if (*p == '\n' || readlen <= 0)
8436 {
8437 listitem_T *li;
8438 char_u *s = NULL;
8439 long_u len = p - start;
8440
8441 /* Finished a line. Remove CRs before NL. */
8442 if (readlen > 0 && !binary)
8443 {
8444 while (len > 0 && start[len - 1] == '\r')
8445 --len;
8446 /* removal may cross back to the "prev" string */
8447 if (len == 0)
8448 while (prevlen > 0 && prev[prevlen - 1] == '\r')
8449 --prevlen;
8450 }
8451 if (prevlen == 0)
8452 s = vim_strnsave(start, (int)len);
8453 else
8454 {
8455 /* Change "prev" buffer to be the right size. This way
8456 * the bytes are only copied once, and very long lines are
8457 * allocated only once. */
8458 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
8459 {
8460 mch_memmove(s + prevlen, start, len);
8461 s[prevlen + len] = NUL;
8462 prev = NULL; /* the list will own the string */
8463 prevlen = prevsize = 0;
8464 }
8465 }
8466 if (s == NULL)
8467 {
8468 do_outofmem_msg((long_u) prevlen + len + 1);
8469 failed = TRUE;
8470 break;
8471 }
8472
8473 if ((li = listitem_alloc()) == NULL)
8474 {
8475 vim_free(s);
8476 failed = TRUE;
8477 break;
8478 }
8479 li->li_tv.v_type = VAR_STRING;
8480 li->li_tv.v_lock = 0;
8481 li->li_tv.vval.v_string = s;
8482 list_append(rettv->vval.v_list, li);
8483
8484 start = p + 1; /* step over newline */
8485 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
8486 break;
8487 }
8488 else if (*p == NUL)
8489 *p = '\n';
8490#ifdef FEAT_MBYTE
8491 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
8492 * when finding the BF and check the previous two bytes. */
8493 else if (*p == 0xbf && enc_utf8 && !binary)
8494 {
8495 /* Find the two bytes before the 0xbf. If p is at buf, or buf
8496 * + 1, these may be in the "prev" string. */
8497 char_u back1 = p >= buf + 1 ? p[-1]
8498 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
8499 char_u back2 = p >= buf + 2 ? p[-2]
8500 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
8501 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
8502
8503 if (back2 == 0xef && back1 == 0xbb)
8504 {
8505 char_u *dest = p - 2;
8506
8507 /* Usually a BOM is at the beginning of a file, and so at
8508 * the beginning of a line; then we can just step over it.
8509 */
8510 if (start == dest)
8511 start = p + 1;
8512 else
8513 {
8514 /* have to shuffle buf to close gap */
8515 int adjust_prevlen = 0;
8516
8517 if (dest < buf)
8518 {
8519 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
8520 dest = buf;
8521 }
8522 if (readlen > p - buf + 1)
8523 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
8524 readlen -= 3 - adjust_prevlen;
8525 prevlen -= adjust_prevlen;
8526 p = dest - 1;
8527 }
8528 }
8529 }
8530#endif
8531 } /* for */
8532
8533 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
8534 break;
8535 if (start < p)
8536 {
8537 /* There's part of a line in buf, store it in "prev". */
8538 if (p - start + prevlen >= prevsize)
8539 {
8540 /* need bigger "prev" buffer */
8541 char_u *newprev;
8542
8543 /* A common use case is ordinary text files and "prev" gets a
8544 * fragment of a line, so the first allocation is made
8545 * small, to avoid repeatedly 'allocing' large and
8546 * 'reallocing' small. */
8547 if (prevsize == 0)
8548 prevsize = (long)(p - start);
8549 else
8550 {
8551 long grow50pc = (prevsize * 3) / 2;
8552 long growmin = (long)((p - start) * 2 + prevlen);
8553 prevsize = grow50pc > growmin ? grow50pc : growmin;
8554 }
8555 newprev = prev == NULL ? alloc(prevsize)
8556 : vim_realloc(prev, prevsize);
8557 if (newprev == NULL)
8558 {
8559 do_outofmem_msg((long_u)prevsize);
8560 failed = TRUE;
8561 break;
8562 }
8563 prev = newprev;
8564 }
8565 /* Add the line part to end of "prev". */
8566 mch_memmove(prev + prevlen, start, p - start);
8567 prevlen += (long)(p - start);
8568 }
8569 } /* while */
8570
8571 /*
8572 * For a negative line count use only the lines at the end of the file,
8573 * free the rest.
8574 */
8575 if (!failed && maxline < 0)
8576 while (cnt > -maxline)
8577 {
8578 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
8579 --cnt;
8580 }
8581
8582 if (failed)
8583 {
8584 list_free(rettv->vval.v_list);
8585 /* readfile doc says an empty list is returned on error */
8586 rettv->vval.v_list = list_alloc();
8587 }
8588
8589 vim_free(prev);
8590 fclose(fd);
8591}
8592
8593#if defined(FEAT_RELTIME)
8594static int list2proftime(typval_T *arg, proftime_T *tm);
8595
8596/*
8597 * Convert a List to proftime_T.
8598 * Return FAIL when there is something wrong.
8599 */
8600 static int
8601list2proftime(typval_T *arg, proftime_T *tm)
8602{
8603 long n1, n2;
8604 int error = FALSE;
8605
8606 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
8607 || arg->vval.v_list->lv_len != 2)
8608 return FAIL;
8609 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
8610 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
8611# ifdef WIN3264
8612 tm->HighPart = n1;
8613 tm->LowPart = n2;
8614# else
8615 tm->tv_sec = n1;
8616 tm->tv_usec = n2;
8617# endif
8618 return error ? FAIL : OK;
8619}
8620#endif /* FEAT_RELTIME */
8621
8622/*
8623 * "reltime()" function
8624 */
8625 static void
8626f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8627{
8628#ifdef FEAT_RELTIME
8629 proftime_T res;
8630 proftime_T start;
8631
8632 if (argvars[0].v_type == VAR_UNKNOWN)
8633 {
8634 /* No arguments: get current time. */
8635 profile_start(&res);
8636 }
8637 else if (argvars[1].v_type == VAR_UNKNOWN)
8638 {
8639 if (list2proftime(&argvars[0], &res) == FAIL)
8640 return;
8641 profile_end(&res);
8642 }
8643 else
8644 {
8645 /* Two arguments: compute the difference. */
8646 if (list2proftime(&argvars[0], &start) == FAIL
8647 || list2proftime(&argvars[1], &res) == FAIL)
8648 return;
8649 profile_sub(&res, &start);
8650 }
8651
8652 if (rettv_list_alloc(rettv) == OK)
8653 {
8654 long n1, n2;
8655
8656# ifdef WIN3264
8657 n1 = res.HighPart;
8658 n2 = res.LowPart;
8659# else
8660 n1 = res.tv_sec;
8661 n2 = res.tv_usec;
8662# endif
8663 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
8664 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
8665 }
8666#endif
8667}
8668
8669#ifdef FEAT_FLOAT
8670/*
8671 * "reltimefloat()" function
8672 */
8673 static void
8674f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
8675{
8676# ifdef FEAT_RELTIME
8677 proftime_T tm;
8678# endif
8679
8680 rettv->v_type = VAR_FLOAT;
8681 rettv->vval.v_float = 0;
8682# ifdef FEAT_RELTIME
8683 if (list2proftime(&argvars[0], &tm) == OK)
8684 rettv->vval.v_float = profile_float(&tm);
8685# endif
8686}
8687#endif
8688
8689/*
8690 * "reltimestr()" function
8691 */
8692 static void
8693f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
8694{
8695#ifdef FEAT_RELTIME
8696 proftime_T tm;
8697#endif
8698
8699 rettv->v_type = VAR_STRING;
8700 rettv->vval.v_string = NULL;
8701#ifdef FEAT_RELTIME
8702 if (list2proftime(&argvars[0], &tm) == OK)
8703 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
8704#endif
8705}
8706
8707#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
8708static void make_connection(void);
8709static int check_connection(void);
8710
8711 static void
8712make_connection(void)
8713{
8714 if (X_DISPLAY == NULL
8715# ifdef FEAT_GUI
8716 && !gui.in_use
8717# endif
8718 )
8719 {
8720 x_force_connect = TRUE;
8721 setup_term_clip();
8722 x_force_connect = FALSE;
8723 }
8724}
8725
8726 static int
8727check_connection(void)
8728{
8729 make_connection();
8730 if (X_DISPLAY == NULL)
8731 {
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01008732 EMSG(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008733 return FAIL;
8734 }
8735 return OK;
8736}
8737#endif
8738
8739#ifdef FEAT_CLIENTSERVER
8740 static void
8741remote_common(typval_T *argvars, typval_T *rettv, int expr)
8742{
8743 char_u *server_name;
8744 char_u *keys;
8745 char_u *r = NULL;
8746 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008747 int timeout = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008748# ifdef WIN32
8749 HWND w;
8750# else
8751 Window w;
8752# endif
8753
8754 if (check_restricted() || check_secure())
8755 return;
8756
8757# ifdef FEAT_X11
8758 if (check_connection() == FAIL)
8759 return;
8760# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008761 if (argvars[2].v_type != VAR_UNKNOWN
8762 && argvars[3].v_type != VAR_UNKNOWN)
8763 timeout = get_tv_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008764
8765 server_name = get_tv_string_chk(&argvars[0]);
8766 if (server_name == NULL)
8767 return; /* type error; errmsg already given */
8768 keys = get_tv_string_buf(&argvars[1], buf);
8769# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008770 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008771# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008772 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
8773 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008774# endif
8775 {
8776 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008777 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008778 EMSG(r); /* sending worked but evaluation failed */
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008779 vim_free(r);
8780 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008781 else
8782 EMSG2(_("E241: Unable to send to %s"), server_name);
8783 return;
8784 }
8785
8786 rettv->vval.v_string = r;
8787
8788 if (argvars[2].v_type != VAR_UNKNOWN)
8789 {
8790 dictitem_T v;
8791 char_u str[30];
8792 char_u *idvar;
8793
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008794 idvar = get_tv_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008795 if (idvar != NULL && *idvar != NUL)
8796 {
8797 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
8798 v.di_tv.v_type = VAR_STRING;
8799 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008800 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008801 vim_free(v.di_tv.vval.v_string);
8802 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008803 }
8804}
8805#endif
8806
8807/*
8808 * "remote_expr()" function
8809 */
8810 static void
8811f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
8812{
8813 rettv->v_type = VAR_STRING;
8814 rettv->vval.v_string = NULL;
8815#ifdef FEAT_CLIENTSERVER
8816 remote_common(argvars, rettv, TRUE);
8817#endif
8818}
8819
8820/*
8821 * "remote_foreground()" function
8822 */
8823 static void
8824f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8825{
8826#ifdef FEAT_CLIENTSERVER
8827# ifdef WIN32
8828 /* On Win32 it's done in this application. */
8829 {
8830 char_u *server_name = get_tv_string_chk(&argvars[0]);
8831
8832 if (server_name != NULL)
8833 serverForeground(server_name);
8834 }
8835# else
8836 /* Send a foreground() expression to the server. */
8837 argvars[1].v_type = VAR_STRING;
8838 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
8839 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008840 rettv->v_type = VAR_STRING;
8841 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008842 remote_common(argvars, rettv, TRUE);
8843 vim_free(argvars[1].vval.v_string);
8844# endif
8845#endif
8846}
8847
8848 static void
8849f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
8850{
8851#ifdef FEAT_CLIENTSERVER
8852 dictitem_T v;
8853 char_u *s = NULL;
8854# ifdef WIN32
8855 long_u n = 0;
8856# endif
8857 char_u *serverid;
8858
8859 if (check_restricted() || check_secure())
8860 {
8861 rettv->vval.v_number = -1;
8862 return;
8863 }
8864 serverid = get_tv_string_chk(&argvars[0]);
8865 if (serverid == NULL)
8866 {
8867 rettv->vval.v_number = -1;
8868 return; /* type error; errmsg already given */
8869 }
8870# ifdef WIN32
8871 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
8872 if (n == 0)
8873 rettv->vval.v_number = -1;
8874 else
8875 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008876 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008877 rettv->vval.v_number = (s != NULL);
8878 }
8879# else
8880 if (check_connection() == FAIL)
8881 return;
8882
8883 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
8884 serverStrToWin(serverid), &s);
8885# endif
8886
8887 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
8888 {
8889 char_u *retvar;
8890
8891 v.di_tv.v_type = VAR_STRING;
8892 v.di_tv.vval.v_string = vim_strsave(s);
8893 retvar = get_tv_string_chk(&argvars[1]);
8894 if (retvar != NULL)
8895 set_var(retvar, &v.di_tv, FALSE);
8896 vim_free(v.di_tv.vval.v_string);
8897 }
8898#else
8899 rettv->vval.v_number = -1;
8900#endif
8901}
8902
8903 static void
8904f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
8905{
8906 char_u *r = NULL;
8907
8908#ifdef FEAT_CLIENTSERVER
8909 char_u *serverid = get_tv_string_chk(&argvars[0]);
8910
8911 if (serverid != NULL && !check_restricted() && !check_secure())
8912 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008913 int timeout = 0;
Bram Moolenaar1662ce12017-03-19 21:47:50 +01008914# ifdef WIN32
8915 /* The server's HWND is encoded in the 'id' parameter */
8916 long_u n = 0;
8917# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008918
8919 if (argvars[1].v_type != VAR_UNKNOWN)
8920 timeout = get_tv_number(&argvars[1]);
8921
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008922# ifdef WIN32
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008923 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
8924 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008925 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008926 if (r == NULL)
8927# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008928 if (check_connection() == FAIL
8929 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
8930 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008931# endif
8932 EMSG(_("E277: Unable to read a server reply"));
8933 }
8934#endif
8935 rettv->v_type = VAR_STRING;
8936 rettv->vval.v_string = r;
8937}
8938
8939/*
8940 * "remote_send()" function
8941 */
8942 static void
8943f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
8944{
8945 rettv->v_type = VAR_STRING;
8946 rettv->vval.v_string = NULL;
8947#ifdef FEAT_CLIENTSERVER
8948 remote_common(argvars, rettv, FALSE);
8949#endif
8950}
8951
8952/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01008953 * "remote_startserver()" function
8954 */
8955 static void
8956f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8957{
8958#ifdef FEAT_CLIENTSERVER
8959 char_u *server = get_tv_string_chk(&argvars[0]);
8960
8961 if (server == NULL)
8962 return; /* type error; errmsg already given */
8963 if (serverName != NULL)
8964 EMSG(_("E941: already started a server"));
8965 else
8966 {
8967# ifdef FEAT_X11
8968 if (check_connection() == OK)
8969 serverRegisterName(X_DISPLAY, server);
8970# else
8971 serverSetName(server);
8972# endif
8973 }
8974#else
8975 EMSG(_("E942: +clientserver feature not available"));
8976#endif
8977}
8978
8979/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008980 * "remove()" function
8981 */
8982 static void
8983f_remove(typval_T *argvars, typval_T *rettv)
8984{
8985 list_T *l;
8986 listitem_T *item, *item2;
8987 listitem_T *li;
8988 long idx;
8989 long end;
8990 char_u *key;
8991 dict_T *d;
8992 dictitem_T *di;
8993 char_u *arg_errmsg = (char_u *)N_("remove() argument");
8994
8995 if (argvars[0].v_type == VAR_DICT)
8996 {
8997 if (argvars[2].v_type != VAR_UNKNOWN)
8998 EMSG2(_(e_toomanyarg), "remove()");
8999 else if ((d = argvars[0].vval.v_dict) != NULL
9000 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
9001 {
9002 key = get_tv_string_chk(&argvars[1]);
9003 if (key != NULL)
9004 {
9005 di = dict_find(d, key, -1);
9006 if (di == NULL)
9007 EMSG2(_(e_dictkey), key);
9008 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
9009 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
9010 {
9011 *rettv = di->di_tv;
9012 init_tv(&di->di_tv);
9013 dictitem_remove(d, di);
9014 }
9015 }
9016 }
9017 }
9018 else if (argvars[0].v_type != VAR_LIST)
9019 EMSG2(_(e_listdictarg), "remove()");
9020 else if ((l = argvars[0].vval.v_list) != NULL
9021 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
9022 {
9023 int error = FALSE;
9024
9025 idx = (long)get_tv_number_chk(&argvars[1], &error);
9026 if (error)
9027 ; /* type error: do nothing, errmsg already given */
9028 else if ((item = list_find(l, idx)) == NULL)
9029 EMSGN(_(e_listidx), idx);
9030 else
9031 {
9032 if (argvars[2].v_type == VAR_UNKNOWN)
9033 {
9034 /* Remove one item, return its value. */
9035 vimlist_remove(l, item, item);
9036 *rettv = item->li_tv;
9037 vim_free(item);
9038 }
9039 else
9040 {
9041 /* Remove range of items, return list with values. */
9042 end = (long)get_tv_number_chk(&argvars[2], &error);
9043 if (error)
9044 ; /* type error: do nothing */
9045 else if ((item2 = list_find(l, end)) == NULL)
9046 EMSGN(_(e_listidx), end);
9047 else
9048 {
9049 int cnt = 0;
9050
9051 for (li = item; li != NULL; li = li->li_next)
9052 {
9053 ++cnt;
9054 if (li == item2)
9055 break;
9056 }
9057 if (li == NULL) /* didn't find "item2" after "item" */
9058 EMSG(_(e_invrange));
9059 else
9060 {
9061 vimlist_remove(l, item, item2);
9062 if (rettv_list_alloc(rettv) == OK)
9063 {
9064 l = rettv->vval.v_list;
9065 l->lv_first = item;
9066 l->lv_last = item2;
9067 item->li_prev = NULL;
9068 item2->li_next = NULL;
9069 l->lv_len = cnt;
9070 }
9071 }
9072 }
9073 }
9074 }
9075 }
9076}
9077
9078/*
9079 * "rename({from}, {to})" function
9080 */
9081 static void
9082f_rename(typval_T *argvars, typval_T *rettv)
9083{
9084 char_u buf[NUMBUFLEN];
9085
9086 if (check_restricted() || check_secure())
9087 rettv->vval.v_number = -1;
9088 else
9089 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
9090 get_tv_string_buf(&argvars[1], buf));
9091}
9092
9093/*
9094 * "repeat()" function
9095 */
9096 static void
9097f_repeat(typval_T *argvars, typval_T *rettv)
9098{
9099 char_u *p;
9100 int n;
9101 int slen;
9102 int len;
9103 char_u *r;
9104 int i;
9105
9106 n = (int)get_tv_number(&argvars[1]);
9107 if (argvars[0].v_type == VAR_LIST)
9108 {
9109 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9110 while (n-- > 0)
9111 if (list_extend(rettv->vval.v_list,
9112 argvars[0].vval.v_list, NULL) == FAIL)
9113 break;
9114 }
9115 else
9116 {
9117 p = get_tv_string(&argvars[0]);
9118 rettv->v_type = VAR_STRING;
9119 rettv->vval.v_string = NULL;
9120
9121 slen = (int)STRLEN(p);
9122 len = slen * n;
9123 if (len <= 0)
9124 return;
9125
9126 r = alloc(len + 1);
9127 if (r != NULL)
9128 {
9129 for (i = 0; i < n; i++)
9130 mch_memmove(r + i * slen, p, (size_t)slen);
9131 r[len] = NUL;
9132 }
9133
9134 rettv->vval.v_string = r;
9135 }
9136}
9137
9138/*
9139 * "resolve()" function
9140 */
9141 static void
9142f_resolve(typval_T *argvars, typval_T *rettv)
9143{
9144 char_u *p;
9145#ifdef HAVE_READLINK
9146 char_u *buf = NULL;
9147#endif
9148
9149 p = get_tv_string(&argvars[0]);
9150#ifdef FEAT_SHORTCUT
9151 {
9152 char_u *v = NULL;
9153
9154 v = mch_resolve_shortcut(p);
9155 if (v != NULL)
9156 rettv->vval.v_string = v;
9157 else
9158 rettv->vval.v_string = vim_strsave(p);
9159 }
9160#else
9161# ifdef HAVE_READLINK
9162 {
9163 char_u *cpy;
9164 int len;
9165 char_u *remain = NULL;
9166 char_u *q;
9167 int is_relative_to_current = FALSE;
9168 int has_trailing_pathsep = FALSE;
9169 int limit = 100;
9170
9171 p = vim_strsave(p);
9172
9173 if (p[0] == '.' && (vim_ispathsep(p[1])
9174 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9175 is_relative_to_current = TRUE;
9176
9177 len = STRLEN(p);
9178 if (len > 0 && after_pathsep(p, p + len))
9179 {
9180 has_trailing_pathsep = TRUE;
9181 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9182 }
9183
9184 q = getnextcomp(p);
9185 if (*q != NUL)
9186 {
9187 /* Separate the first path component in "p", and keep the
9188 * remainder (beginning with the path separator). */
9189 remain = vim_strsave(q - 1);
9190 q[-1] = NUL;
9191 }
9192
9193 buf = alloc(MAXPATHL + 1);
9194 if (buf == NULL)
9195 goto fail;
9196
9197 for (;;)
9198 {
9199 for (;;)
9200 {
9201 len = readlink((char *)p, (char *)buf, MAXPATHL);
9202 if (len <= 0)
9203 break;
9204 buf[len] = NUL;
9205
9206 if (limit-- == 0)
9207 {
9208 vim_free(p);
9209 vim_free(remain);
9210 EMSG(_("E655: Too many symbolic links (cycle?)"));
9211 rettv->vval.v_string = NULL;
9212 goto fail;
9213 }
9214
9215 /* Ensure that the result will have a trailing path separator
9216 * if the argument has one. */
9217 if (remain == NULL && has_trailing_pathsep)
9218 add_pathsep(buf);
9219
9220 /* Separate the first path component in the link value and
9221 * concatenate the remainders. */
9222 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9223 if (*q != NUL)
9224 {
9225 if (remain == NULL)
9226 remain = vim_strsave(q - 1);
9227 else
9228 {
9229 cpy = concat_str(q - 1, remain);
9230 if (cpy != NULL)
9231 {
9232 vim_free(remain);
9233 remain = cpy;
9234 }
9235 }
9236 q[-1] = NUL;
9237 }
9238
9239 q = gettail(p);
9240 if (q > p && *q == NUL)
9241 {
9242 /* Ignore trailing path separator. */
9243 q[-1] = NUL;
9244 q = gettail(p);
9245 }
9246 if (q > p && !mch_isFullName(buf))
9247 {
9248 /* symlink is relative to directory of argument */
9249 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
9250 if (cpy != NULL)
9251 {
9252 STRCPY(cpy, p);
9253 STRCPY(gettail(cpy), buf);
9254 vim_free(p);
9255 p = cpy;
9256 }
9257 }
9258 else
9259 {
9260 vim_free(p);
9261 p = vim_strsave(buf);
9262 }
9263 }
9264
9265 if (remain == NULL)
9266 break;
9267
9268 /* Append the first path component of "remain" to "p". */
9269 q = getnextcomp(remain + 1);
9270 len = q - remain - (*q != NUL);
9271 cpy = vim_strnsave(p, STRLEN(p) + len);
9272 if (cpy != NULL)
9273 {
9274 STRNCAT(cpy, remain, len);
9275 vim_free(p);
9276 p = cpy;
9277 }
9278 /* Shorten "remain". */
9279 if (*q != NUL)
9280 STRMOVE(remain, q - 1);
9281 else
Bram Moolenaard23a8232018-02-10 18:45:26 +01009282 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009283 }
9284
9285 /* If the result is a relative path name, make it explicitly relative to
9286 * the current directory if and only if the argument had this form. */
9287 if (!vim_ispathsep(*p))
9288 {
9289 if (is_relative_to_current
9290 && *p != NUL
9291 && !(p[0] == '.'
9292 && (p[1] == NUL
9293 || vim_ispathsep(p[1])
9294 || (p[1] == '.'
9295 && (p[2] == NUL
9296 || vim_ispathsep(p[2]))))))
9297 {
9298 /* Prepend "./". */
9299 cpy = concat_str((char_u *)"./", p);
9300 if (cpy != NULL)
9301 {
9302 vim_free(p);
9303 p = cpy;
9304 }
9305 }
9306 else if (!is_relative_to_current)
9307 {
9308 /* Strip leading "./". */
9309 q = p;
9310 while (q[0] == '.' && vim_ispathsep(q[1]))
9311 q += 2;
9312 if (q > p)
9313 STRMOVE(p, p + 2);
9314 }
9315 }
9316
9317 /* Ensure that the result will have no trailing path separator
9318 * if the argument had none. But keep "/" or "//". */
9319 if (!has_trailing_pathsep)
9320 {
9321 q = p + STRLEN(p);
9322 if (after_pathsep(p, q))
9323 *gettail_sep(p) = NUL;
9324 }
9325
9326 rettv->vval.v_string = p;
9327 }
9328# else
9329 rettv->vval.v_string = vim_strsave(p);
9330# endif
9331#endif
9332
9333 simplify_filename(rettv->vval.v_string);
9334
9335#ifdef HAVE_READLINK
9336fail:
9337 vim_free(buf);
9338#endif
9339 rettv->v_type = VAR_STRING;
9340}
9341
9342/*
9343 * "reverse({list})" function
9344 */
9345 static void
9346f_reverse(typval_T *argvars, typval_T *rettv)
9347{
9348 list_T *l;
9349 listitem_T *li, *ni;
9350
9351 if (argvars[0].v_type != VAR_LIST)
9352 EMSG2(_(e_listarg), "reverse()");
9353 else if ((l = argvars[0].vval.v_list) != NULL
9354 && !tv_check_lock(l->lv_lock,
9355 (char_u *)N_("reverse() argument"), TRUE))
9356 {
9357 li = l->lv_last;
9358 l->lv_first = l->lv_last = NULL;
9359 l->lv_len = 0;
9360 while (li != NULL)
9361 {
9362 ni = li->li_prev;
9363 list_append(l, li);
9364 li = ni;
9365 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009366 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009367 l->lv_idx = l->lv_len - l->lv_idx - 1;
9368 }
9369}
9370
9371#define SP_NOMOVE 0x01 /* don't move cursor */
9372#define SP_REPEAT 0x02 /* repeat to find outer pair */
9373#define SP_RETCOUNT 0x04 /* return matchcount */
9374#define SP_SETPCMARK 0x08 /* set previous context mark */
9375#define SP_START 0x10 /* accept match at start position */
9376#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
9377#define SP_END 0x40 /* leave cursor at end of match */
9378#define SP_COLUMN 0x80 /* start at cursor column */
9379
9380static int get_search_arg(typval_T *varp, int *flagsp);
9381
9382/*
9383 * Get flags for a search function.
9384 * Possibly sets "p_ws".
9385 * Returns BACKWARD, FORWARD or zero (for an error).
9386 */
9387 static int
9388get_search_arg(typval_T *varp, int *flagsp)
9389{
9390 int dir = FORWARD;
9391 char_u *flags;
9392 char_u nbuf[NUMBUFLEN];
9393 int mask;
9394
9395 if (varp->v_type != VAR_UNKNOWN)
9396 {
9397 flags = get_tv_string_buf_chk(varp, nbuf);
9398 if (flags == NULL)
9399 return 0; /* type error; errmsg already given */
9400 while (*flags != NUL)
9401 {
9402 switch (*flags)
9403 {
9404 case 'b': dir = BACKWARD; break;
9405 case 'w': p_ws = TRUE; break;
9406 case 'W': p_ws = FALSE; break;
9407 default: mask = 0;
9408 if (flagsp != NULL)
9409 switch (*flags)
9410 {
9411 case 'c': mask = SP_START; break;
9412 case 'e': mask = SP_END; break;
9413 case 'm': mask = SP_RETCOUNT; break;
9414 case 'n': mask = SP_NOMOVE; break;
9415 case 'p': mask = SP_SUBPAT; break;
9416 case 'r': mask = SP_REPEAT; break;
9417 case 's': mask = SP_SETPCMARK; break;
9418 case 'z': mask = SP_COLUMN; break;
9419 }
9420 if (mask == 0)
9421 {
9422 EMSG2(_(e_invarg2), flags);
9423 dir = 0;
9424 }
9425 else
9426 *flagsp |= mask;
9427 }
9428 if (dir == 0)
9429 break;
9430 ++flags;
9431 }
9432 }
9433 return dir;
9434}
9435
9436/*
9437 * Shared by search() and searchpos() functions.
9438 */
9439 static int
9440search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
9441{
9442 int flags;
9443 char_u *pat;
9444 pos_T pos;
9445 pos_T save_cursor;
9446 int save_p_ws = p_ws;
9447 int dir;
9448 int retval = 0; /* default: FAIL */
9449 long lnum_stop = 0;
9450 proftime_T tm;
9451#ifdef FEAT_RELTIME
9452 long time_limit = 0;
9453#endif
9454 int options = SEARCH_KEEP;
9455 int subpatnum;
9456
9457 pat = get_tv_string(&argvars[0]);
9458 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
9459 if (dir == 0)
9460 goto theend;
9461 flags = *flagsp;
9462 if (flags & SP_START)
9463 options |= SEARCH_START;
9464 if (flags & SP_END)
9465 options |= SEARCH_END;
9466 if (flags & SP_COLUMN)
9467 options |= SEARCH_COL;
9468
9469 /* Optional arguments: line number to stop searching and timeout. */
9470 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9471 {
9472 lnum_stop = (long)get_tv_number_chk(&argvars[2], NULL);
9473 if (lnum_stop < 0)
9474 goto theend;
9475#ifdef FEAT_RELTIME
9476 if (argvars[3].v_type != VAR_UNKNOWN)
9477 {
9478 time_limit = (long)get_tv_number_chk(&argvars[3], NULL);
9479 if (time_limit < 0)
9480 goto theend;
9481 }
9482#endif
9483 }
9484
9485#ifdef FEAT_RELTIME
9486 /* Set the time limit, if there is one. */
9487 profile_setlimit(time_limit, &tm);
9488#endif
9489
9490 /*
9491 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
9492 * Check to make sure only those flags are set.
9493 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
9494 * flags cannot be set. Check for that condition also.
9495 */
9496 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
9497 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9498 {
9499 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
9500 goto theend;
9501 }
9502
9503 pos = save_cursor = curwin->w_cursor;
9504 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009505 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009506 if (subpatnum != FAIL)
9507 {
9508 if (flags & SP_SUBPAT)
9509 retval = subpatnum;
9510 else
9511 retval = pos.lnum;
9512 if (flags & SP_SETPCMARK)
9513 setpcmark();
9514 curwin->w_cursor = pos;
9515 if (match_pos != NULL)
9516 {
9517 /* Store the match cursor position */
9518 match_pos->lnum = pos.lnum;
9519 match_pos->col = pos.col + 1;
9520 }
9521 /* "/$" will put the cursor after the end of the line, may need to
9522 * correct that here */
9523 check_cursor();
9524 }
9525
9526 /* If 'n' flag is used: restore cursor position. */
9527 if (flags & SP_NOMOVE)
9528 curwin->w_cursor = save_cursor;
9529 else
9530 curwin->w_set_curswant = TRUE;
9531theend:
9532 p_ws = save_p_ws;
9533
9534 return retval;
9535}
9536
9537#ifdef FEAT_FLOAT
9538
9539/*
9540 * round() is not in C90, use ceil() or floor() instead.
9541 */
9542 float_T
9543vim_round(float_T f)
9544{
9545 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
9546}
9547
9548/*
9549 * "round({float})" function
9550 */
9551 static void
9552f_round(typval_T *argvars, typval_T *rettv)
9553{
9554 float_T f = 0.0;
9555
9556 rettv->v_type = VAR_FLOAT;
9557 if (get_float_arg(argvars, &f) == OK)
9558 rettv->vval.v_float = vim_round(f);
9559 else
9560 rettv->vval.v_float = 0.0;
9561}
9562#endif
9563
9564/*
9565 * "screenattr()" function
9566 */
9567 static void
9568f_screenattr(typval_T *argvars, typval_T *rettv)
9569{
9570 int row;
9571 int col;
9572 int c;
9573
9574 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
9575 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
9576 if (row < 0 || row >= screen_Rows
9577 || col < 0 || col >= screen_Columns)
9578 c = -1;
9579 else
9580 c = ScreenAttrs[LineOffset[row] + col];
9581 rettv->vval.v_number = c;
9582}
9583
9584/*
9585 * "screenchar()" function
9586 */
9587 static void
9588f_screenchar(typval_T *argvars, typval_T *rettv)
9589{
9590 int row;
9591 int col;
9592 int off;
9593 int c;
9594
9595 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
9596 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
9597 if (row < 0 || row >= screen_Rows
9598 || col < 0 || col >= screen_Columns)
9599 c = -1;
9600 else
9601 {
9602 off = LineOffset[row] + col;
9603#ifdef FEAT_MBYTE
9604 if (enc_utf8 && ScreenLinesUC[off] != 0)
9605 c = ScreenLinesUC[off];
9606 else
9607#endif
9608 c = ScreenLines[off];
9609 }
9610 rettv->vval.v_number = c;
9611}
9612
9613/*
9614 * "screencol()" function
9615 *
9616 * First column is 1 to be consistent with virtcol().
9617 */
9618 static void
9619f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
9620{
9621 rettv->vval.v_number = screen_screencol() + 1;
9622}
9623
9624/*
9625 * "screenrow()" function
9626 */
9627 static void
9628f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
9629{
9630 rettv->vval.v_number = screen_screenrow() + 1;
9631}
9632
9633/*
9634 * "search()" function
9635 */
9636 static void
9637f_search(typval_T *argvars, typval_T *rettv)
9638{
9639 int flags = 0;
9640
9641 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
9642}
9643
9644/*
9645 * "searchdecl()" function
9646 */
9647 static void
9648f_searchdecl(typval_T *argvars, typval_T *rettv)
9649{
9650 int locally = 1;
9651 int thisblock = 0;
9652 int error = FALSE;
9653 char_u *name;
9654
9655 rettv->vval.v_number = 1; /* default: FAIL */
9656
9657 name = get_tv_string_chk(&argvars[0]);
9658 if (argvars[1].v_type != VAR_UNKNOWN)
9659 {
9660 locally = (int)get_tv_number_chk(&argvars[1], &error) == 0;
9661 if (!error && argvars[2].v_type != VAR_UNKNOWN)
9662 thisblock = (int)get_tv_number_chk(&argvars[2], &error) != 0;
9663 }
9664 if (!error && name != NULL)
9665 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
9666 locally, thisblock, SEARCH_KEEP) == FAIL;
9667}
9668
9669/*
9670 * Used by searchpair() and searchpairpos()
9671 */
9672 static int
9673searchpair_cmn(typval_T *argvars, pos_T *match_pos)
9674{
9675 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01009676 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009677 int save_p_ws = p_ws;
9678 int dir;
9679 int flags = 0;
9680 char_u nbuf1[NUMBUFLEN];
9681 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009682 int retval = 0; /* default: FAIL */
9683 long lnum_stop = 0;
9684 long time_limit = 0;
9685
9686 /* Get the three pattern arguments: start, middle, end. */
9687 spat = get_tv_string_chk(&argvars[0]);
9688 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
9689 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
9690 if (spat == NULL || mpat == NULL || epat == NULL)
9691 goto theend; /* type error */
9692
9693 /* Handle the optional fourth argument: flags */
9694 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
9695 if (dir == 0)
9696 goto theend;
9697
9698 /* Don't accept SP_END or SP_SUBPAT.
9699 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
9700 */
9701 if ((flags & (SP_END | SP_SUBPAT)) != 0
9702 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9703 {
9704 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
9705 goto theend;
9706 }
9707
9708 /* Using 'r' implies 'W', otherwise it doesn't work. */
9709 if (flags & SP_REPEAT)
9710 p_ws = FALSE;
9711
9712 /* Optional fifth argument: skip expression */
9713 if (argvars[3].v_type == VAR_UNKNOWN
9714 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01009715 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009716 else
9717 {
Bram Moolenaar48570482017-10-30 21:48:41 +01009718 skip = &argvars[4];
9719 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
9720 && skip->v_type != VAR_STRING)
9721 {
9722 /* Type error */
9723 goto theend;
9724 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009725 if (argvars[5].v_type != VAR_UNKNOWN)
9726 {
9727 lnum_stop = (long)get_tv_number_chk(&argvars[5], NULL);
9728 if (lnum_stop < 0)
9729 goto theend;
9730#ifdef FEAT_RELTIME
9731 if (argvars[6].v_type != VAR_UNKNOWN)
9732 {
9733 time_limit = (long)get_tv_number_chk(&argvars[6], NULL);
9734 if (time_limit < 0)
9735 goto theend;
9736 }
9737#endif
9738 }
9739 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009740
9741 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
9742 match_pos, lnum_stop, time_limit);
9743
9744theend:
9745 p_ws = save_p_ws;
9746
9747 return retval;
9748}
9749
9750/*
9751 * "searchpair()" function
9752 */
9753 static void
9754f_searchpair(typval_T *argvars, typval_T *rettv)
9755{
9756 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
9757}
9758
9759/*
9760 * "searchpairpos()" function
9761 */
9762 static void
9763f_searchpairpos(typval_T *argvars, typval_T *rettv)
9764{
9765 pos_T match_pos;
9766 int lnum = 0;
9767 int col = 0;
9768
9769 if (rettv_list_alloc(rettv) == FAIL)
9770 return;
9771
9772 if (searchpair_cmn(argvars, &match_pos) > 0)
9773 {
9774 lnum = match_pos.lnum;
9775 col = match_pos.col;
9776 }
9777
9778 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
9779 list_append_number(rettv->vval.v_list, (varnumber_T)col);
9780}
9781
9782/*
9783 * Search for a start/middle/end thing.
9784 * Used by searchpair(), see its documentation for the details.
9785 * Returns 0 or -1 for no match,
9786 */
9787 long
9788do_searchpair(
9789 char_u *spat, /* start pattern */
9790 char_u *mpat, /* middle pattern */
9791 char_u *epat, /* end pattern */
9792 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +01009793 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009794 int flags, /* SP_SETPCMARK and other SP_ values */
9795 pos_T *match_pos,
9796 linenr_T lnum_stop, /* stop at this line if not zero */
9797 long time_limit UNUSED) /* stop after this many msec */
9798{
9799 char_u *save_cpo;
9800 char_u *pat, *pat2 = NULL, *pat3 = NULL;
9801 long retval = 0;
9802 pos_T pos;
9803 pos_T firstpos;
9804 pos_T foundpos;
9805 pos_T save_cursor;
9806 pos_T save_pos;
9807 int n;
9808 int r;
9809 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01009810 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009811 int err;
9812 int options = SEARCH_KEEP;
9813 proftime_T tm;
9814
9815 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
9816 save_cpo = p_cpo;
9817 p_cpo = empty_option;
9818
9819#ifdef FEAT_RELTIME
9820 /* Set the time limit, if there is one. */
9821 profile_setlimit(time_limit, &tm);
9822#endif
9823
9824 /* Make two search patterns: start/end (pat2, for in nested pairs) and
9825 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009826 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
9827 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009828 if (pat2 == NULL || pat3 == NULL)
9829 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009830 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009831 if (*mpat == NUL)
9832 STRCPY(pat3, pat2);
9833 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009834 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009835 spat, epat, mpat);
9836 if (flags & SP_START)
9837 options |= SEARCH_START;
9838
Bram Moolenaar48570482017-10-30 21:48:41 +01009839 if (skip != NULL)
9840 {
9841 /* Empty string means to not use the skip expression. */
9842 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
9843 use_skip = skip->vval.v_string != NULL
9844 && *skip->vval.v_string != NUL;
9845 }
9846
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009847 save_cursor = curwin->w_cursor;
9848 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009849 CLEAR_POS(&firstpos);
9850 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009851 pat = pat3;
9852 for (;;)
9853 {
9854 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009855 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009856 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009857 /* didn't find it or found the first match again: FAIL */
9858 break;
9859
9860 if (firstpos.lnum == 0)
9861 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009862 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009863 {
9864 /* Found the same position again. Can happen with a pattern that
9865 * has "\zs" at the end and searching backwards. Advance one
9866 * character and try again. */
9867 if (dir == BACKWARD)
9868 decl(&pos);
9869 else
9870 incl(&pos);
9871 }
9872 foundpos = pos;
9873
9874 /* clear the start flag to avoid getting stuck here */
9875 options &= ~SEARCH_START;
9876
9877 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +01009878 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009879 {
9880 save_pos = curwin->w_cursor;
9881 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01009882 err = FALSE;
9883 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009884 curwin->w_cursor = save_pos;
9885 if (err)
9886 {
9887 /* Evaluating {skip} caused an error, break here. */
9888 curwin->w_cursor = save_cursor;
9889 retval = -1;
9890 break;
9891 }
9892 if (r)
9893 continue;
9894 }
9895
9896 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
9897 {
9898 /* Found end when searching backwards or start when searching
9899 * forward: nested pair. */
9900 ++nest;
9901 pat = pat2; /* nested, don't search for middle */
9902 }
9903 else
9904 {
9905 /* Found end when searching forward or start when searching
9906 * backward: end of (nested) pair; or found middle in outer pair. */
9907 if (--nest == 1)
9908 pat = pat3; /* outer level, search for middle */
9909 }
9910
9911 if (nest == 0)
9912 {
9913 /* Found the match: return matchcount or line number. */
9914 if (flags & SP_RETCOUNT)
9915 ++retval;
9916 else
9917 retval = pos.lnum;
9918 if (flags & SP_SETPCMARK)
9919 setpcmark();
9920 curwin->w_cursor = pos;
9921 if (!(flags & SP_REPEAT))
9922 break;
9923 nest = 1; /* search for next unmatched */
9924 }
9925 }
9926
9927 if (match_pos != NULL)
9928 {
9929 /* Store the match cursor position */
9930 match_pos->lnum = curwin->w_cursor.lnum;
9931 match_pos->col = curwin->w_cursor.col + 1;
9932 }
9933
9934 /* If 'n' flag is used or search failed: restore cursor position. */
9935 if ((flags & SP_NOMOVE) || retval == 0)
9936 curwin->w_cursor = save_cursor;
9937
9938theend:
9939 vim_free(pat2);
9940 vim_free(pat3);
9941 if (p_cpo == empty_option)
9942 p_cpo = save_cpo;
9943 else
9944 /* Darn, evaluating the {skip} expression changed the value. */
9945 free_string_option(save_cpo);
9946
9947 return retval;
9948}
9949
9950/*
9951 * "searchpos()" function
9952 */
9953 static void
9954f_searchpos(typval_T *argvars, typval_T *rettv)
9955{
9956 pos_T match_pos;
9957 int lnum = 0;
9958 int col = 0;
9959 int n;
9960 int flags = 0;
9961
9962 if (rettv_list_alloc(rettv) == FAIL)
9963 return;
9964
9965 n = search_cmn(argvars, &match_pos, &flags);
9966 if (n > 0)
9967 {
9968 lnum = match_pos.lnum;
9969 col = match_pos.col;
9970 }
9971
9972 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
9973 list_append_number(rettv->vval.v_list, (varnumber_T)col);
9974 if (flags & SP_SUBPAT)
9975 list_append_number(rettv->vval.v_list, (varnumber_T)n);
9976}
9977
9978 static void
9979f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
9980{
9981#ifdef FEAT_CLIENTSERVER
9982 char_u buf[NUMBUFLEN];
9983 char_u *server = get_tv_string_chk(&argvars[0]);
9984 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
9985
9986 rettv->vval.v_number = -1;
9987 if (server == NULL || reply == NULL)
9988 return;
9989 if (check_restricted() || check_secure())
9990 return;
9991# ifdef FEAT_X11
9992 if (check_connection() == FAIL)
9993 return;
9994# endif
9995
9996 if (serverSendReply(server, reply) < 0)
9997 {
9998 EMSG(_("E258: Unable to send to client"));
9999 return;
10000 }
10001 rettv->vval.v_number = 0;
10002#else
10003 rettv->vval.v_number = -1;
10004#endif
10005}
10006
10007 static void
10008f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
10009{
10010 char_u *r = NULL;
10011
10012#ifdef FEAT_CLIENTSERVER
10013# ifdef WIN32
10014 r = serverGetVimNames();
10015# else
10016 make_connection();
10017 if (X_DISPLAY != NULL)
10018 r = serverGetVimNames(X_DISPLAY);
10019# endif
10020#endif
10021 rettv->v_type = VAR_STRING;
10022 rettv->vval.v_string = r;
10023}
10024
10025/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010026 * Set line or list of lines in buffer "buf".
10027 */
10028 static void
10029set_buffer_lines(buf_T *buf, linenr_T lnum, typval_T *lines, typval_T *rettv)
10030{
10031 char_u *line = NULL;
10032 list_T *l = NULL;
10033 listitem_T *li = NULL;
10034 long added = 0;
10035 linenr_T lcount;
Bram Moolenaar0c4dc882017-11-06 21:32:54 +010010036 buf_T *curbuf_save = NULL;
10037 win_T *curwin_save = NULL;
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010038 int is_curbuf = buf == curbuf;
10039
Bram Moolenaar9d954202017-09-04 20:34:19 +020010040 /* When using the current buffer ml_mfp will be set if needed. Useful when
10041 * setline() is used on startup. For other buffers the buffer must be
10042 * loaded. */
10043 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010044 {
10045 rettv->vval.v_number = 1; /* FAIL */
10046 return;
10047 }
10048
Bram Moolenaar0c4dc882017-11-06 21:32:54 +010010049 if (!is_curbuf)
10050 {
10051 wininfo_T *wip;
10052
10053 curbuf_save = curbuf;
10054 curwin_save = curwin;
10055 curbuf = buf;
10056 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
10057 {
10058 if (wip->wi_win != NULL)
10059 {
10060 curwin = wip->wi_win;
10061 break;
10062 }
10063 }
10064 }
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010065
10066 lcount = curbuf->b_ml.ml_line_count;
10067
10068 if (lines->v_type == VAR_LIST)
10069 {
10070 l = lines->vval.v_list;
10071 li = l->lv_first;
10072 }
10073 else
10074 line = get_tv_string_chk(lines);
10075
10076 /* default result is zero == OK */
10077 for (;;)
10078 {
10079 if (l != NULL)
10080 {
10081 /* list argument, get next string */
10082 if (li == NULL)
10083 break;
10084 line = get_tv_string_chk(&li->li_tv);
10085 li = li->li_next;
10086 }
10087
10088 rettv->vval.v_number = 1; /* FAIL */
10089 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
10090 break;
10091
10092 /* When coming here from Insert mode, sync undo, so that this can be
10093 * undone separately from what was previously inserted. */
10094 if (u_sync_once == 2)
10095 {
10096 u_sync_once = 1; /* notify that u_sync() was called */
10097 u_sync(TRUE);
10098 }
10099
10100 if (lnum <= curbuf->b_ml.ml_line_count)
10101 {
10102 /* existing line, replace it */
10103 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
10104 {
10105 changed_bytes(lnum, 0);
10106 if (is_curbuf && lnum == curwin->w_cursor.lnum)
10107 check_cursor_col();
10108 rettv->vval.v_number = 0; /* OK */
10109 }
10110 }
10111 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
10112 {
10113 /* lnum is one past the last line, append the line */
10114 ++added;
10115 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
10116 rettv->vval.v_number = 0; /* OK */
10117 }
10118
10119 if (l == NULL) /* only one string argument */
10120 break;
10121 ++lnum;
10122 }
10123
10124 if (added > 0)
10125 appended_lines_mark(lcount, added);
10126
Bram Moolenaar0c4dc882017-11-06 21:32:54 +010010127 if (!is_curbuf)
10128 {
10129 curbuf = curbuf_save;
10130 curwin = curwin_save;
10131 }
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010132}
10133
10134/*
10135 * "setbufline()" function
10136 */
10137 static void
10138f_setbufline(argvars, rettv)
10139 typval_T *argvars;
10140 typval_T *rettv;
10141{
10142 linenr_T lnum;
10143 buf_T *buf;
10144
10145 buf = get_buf_tv(&argvars[0], FALSE);
10146 if (buf == NULL)
10147 rettv->vval.v_number = 1; /* FAIL */
10148 else
10149 {
10150 lnum = get_tv_lnum_buf(&argvars[1], buf);
10151
10152 set_buffer_lines(buf, lnum, &argvars[2], rettv);
10153 }
10154}
10155
10156/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010157 * "setbufvar()" function
10158 */
10159 static void
10160f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10161{
10162 buf_T *buf;
10163 char_u *varname, *bufvarname;
10164 typval_T *varp;
10165 char_u nbuf[NUMBUFLEN];
10166
10167 if (check_restricted() || check_secure())
10168 return;
10169 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10170 varname = get_tv_string_chk(&argvars[1]);
10171 buf = get_buf_tv(&argvars[0], FALSE);
10172 varp = &argvars[2];
10173
10174 if (buf != NULL && varname != NULL && varp != NULL)
10175 {
10176 if (*varname == '&')
10177 {
10178 long numval;
10179 char_u *strval;
10180 int error = FALSE;
10181 aco_save_T aco;
10182
10183 /* set curbuf to be our buf, temporarily */
10184 aucmd_prepbuf(&aco, buf);
10185
10186 ++varname;
10187 numval = (long)get_tv_number_chk(varp, &error);
10188 strval = get_tv_string_buf_chk(varp, nbuf);
10189 if (!error && strval != NULL)
10190 set_option_value(varname, numval, strval, OPT_LOCAL);
10191
10192 /* reset notion of buffer */
10193 aucmd_restbuf(&aco);
10194 }
10195 else
10196 {
10197 buf_T *save_curbuf = curbuf;
10198
10199 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
10200 if (bufvarname != NULL)
10201 {
10202 curbuf = buf;
10203 STRCPY(bufvarname, "b:");
10204 STRCPY(bufvarname + 2, varname);
10205 set_var(bufvarname, varp, TRUE);
10206 vim_free(bufvarname);
10207 curbuf = save_curbuf;
10208 }
10209 }
10210 }
10211}
10212
10213 static void
10214f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10215{
10216 dict_T *d;
10217 dictitem_T *di;
10218 char_u *csearch;
10219
10220 if (argvars[0].v_type != VAR_DICT)
10221 {
10222 EMSG(_(e_dictreq));
10223 return;
10224 }
10225
10226 if ((d = argvars[0].vval.v_dict) != NULL)
10227 {
10228 csearch = get_dict_string(d, (char_u *)"char", FALSE);
10229 if (csearch != NULL)
10230 {
10231#ifdef FEAT_MBYTE
10232 if (enc_utf8)
10233 {
10234 int pcc[MAX_MCO];
10235 int c = utfc_ptr2char(csearch, pcc);
10236
10237 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
10238 }
10239 else
10240#endif
10241 set_last_csearch(PTR2CHAR(csearch),
10242 csearch, MB_PTR2LEN(csearch));
10243 }
10244
10245 di = dict_find(d, (char_u *)"forward", -1);
10246 if (di != NULL)
10247 set_csearch_direction((int)get_tv_number(&di->di_tv)
10248 ? FORWARD : BACKWARD);
10249
10250 di = dict_find(d, (char_u *)"until", -1);
10251 if (di != NULL)
10252 set_csearch_until(!!get_tv_number(&di->di_tv));
10253 }
10254}
10255
10256/*
10257 * "setcmdpos()" function
10258 */
10259 static void
10260f_setcmdpos(typval_T *argvars, typval_T *rettv)
10261{
10262 int pos = (int)get_tv_number(&argvars[0]) - 1;
10263
10264 if (pos >= 0)
10265 rettv->vval.v_number = set_cmdline_pos(pos);
10266}
10267
10268/*
10269 * "setfperm({fname}, {mode})" function
10270 */
10271 static void
10272f_setfperm(typval_T *argvars, typval_T *rettv)
10273{
10274 char_u *fname;
10275 char_u modebuf[NUMBUFLEN];
10276 char_u *mode_str;
10277 int i;
10278 int mask;
10279 int mode = 0;
10280
10281 rettv->vval.v_number = 0;
10282 fname = get_tv_string_chk(&argvars[0]);
10283 if (fname == NULL)
10284 return;
10285 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
10286 if (mode_str == NULL)
10287 return;
10288 if (STRLEN(mode_str) != 9)
10289 {
10290 EMSG2(_(e_invarg2), mode_str);
10291 return;
10292 }
10293
10294 mask = 1;
10295 for (i = 8; i >= 0; --i)
10296 {
10297 if (mode_str[i] != '-')
10298 mode |= mask;
10299 mask = mask << 1;
10300 }
10301 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
10302}
10303
10304/*
10305 * "setline()" function
10306 */
10307 static void
10308f_setline(typval_T *argvars, typval_T *rettv)
10309{
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010310 linenr_T lnum = get_tv_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010311
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010312 set_buffer_lines(curbuf, lnum, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010313}
10314
Bram Moolenaard823fa92016-08-12 16:29:27 +020010315static void set_qf_ll_list(win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *what_arg, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010316
10317/*
10318 * Used by "setqflist()" and "setloclist()" functions
10319 */
10320 static void
10321set_qf_ll_list(
10322 win_T *wp UNUSED,
10323 typval_T *list_arg UNUSED,
10324 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020010325 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010326 typval_T *rettv)
10327{
10328#ifdef FEAT_QUICKFIX
10329 static char *e_invact = N_("E927: Invalid action: '%s'");
10330 char_u *act;
10331 int action = 0;
10332#endif
10333
10334 rettv->vval.v_number = -1;
10335
10336#ifdef FEAT_QUICKFIX
10337 if (list_arg->v_type != VAR_LIST)
10338 EMSG(_(e_listreq));
10339 else
10340 {
10341 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010342 dict_T *d = NULL;
10343 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010344
10345 if (action_arg->v_type == VAR_STRING)
10346 {
10347 act = get_tv_string_chk(action_arg);
10348 if (act == NULL)
10349 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020010350 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
10351 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010352 action = *act;
10353 else
10354 EMSG2(_(e_invact), act);
10355 }
10356 else if (action_arg->v_type == VAR_UNKNOWN)
10357 action = ' ';
10358 else
10359 EMSG(_(e_stringreq));
10360
Bram Moolenaard823fa92016-08-12 16:29:27 +020010361 if (action_arg->v_type != VAR_UNKNOWN
10362 && what_arg->v_type != VAR_UNKNOWN)
10363 {
10364 if (what_arg->v_type == VAR_DICT)
10365 d = what_arg->vval.v_dict;
10366 else
10367 {
10368 EMSG(_(e_dictreq));
10369 valid_dict = FALSE;
10370 }
10371 }
10372
10373 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
10374 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()"), d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010375 rettv->vval.v_number = 0;
10376 }
10377#endif
10378}
10379
10380/*
10381 * "setloclist()" function
10382 */
10383 static void
10384f_setloclist(typval_T *argvars, typval_T *rettv)
10385{
10386 win_T *win;
10387
10388 rettv->vval.v_number = -1;
10389
10390 win = find_win_by_nr(&argvars[0], NULL);
10391 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020010392 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010393}
10394
10395/*
10396 * "setmatches()" function
10397 */
10398 static void
10399f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10400{
10401#ifdef FEAT_SEARCH_EXTRA
10402 list_T *l;
10403 listitem_T *li;
10404 dict_T *d;
10405 list_T *s = NULL;
10406
10407 rettv->vval.v_number = -1;
10408 if (argvars[0].v_type != VAR_LIST)
10409 {
10410 EMSG(_(e_listreq));
10411 return;
10412 }
10413 if ((l = argvars[0].vval.v_list) != NULL)
10414 {
10415
10416 /* To some extent make sure that we are dealing with a list from
10417 * "getmatches()". */
10418 li = l->lv_first;
10419 while (li != NULL)
10420 {
10421 if (li->li_tv.v_type != VAR_DICT
10422 || (d = li->li_tv.vval.v_dict) == NULL)
10423 {
10424 EMSG(_(e_invarg));
10425 return;
10426 }
10427 if (!(dict_find(d, (char_u *)"group", -1) != NULL
10428 && (dict_find(d, (char_u *)"pattern", -1) != NULL
10429 || dict_find(d, (char_u *)"pos1", -1) != NULL)
10430 && dict_find(d, (char_u *)"priority", -1) != NULL
10431 && dict_find(d, (char_u *)"id", -1) != NULL))
10432 {
10433 EMSG(_(e_invarg));
10434 return;
10435 }
10436 li = li->li_next;
10437 }
10438
10439 clear_matches(curwin);
10440 li = l->lv_first;
10441 while (li != NULL)
10442 {
10443 int i = 0;
10444 char_u buf[5];
10445 dictitem_T *di;
10446 char_u *group;
10447 int priority;
10448 int id;
10449 char_u *conceal;
10450
10451 d = li->li_tv.vval.v_dict;
10452 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
10453 {
10454 if (s == NULL)
10455 {
10456 s = list_alloc();
10457 if (s == NULL)
10458 return;
10459 }
10460
10461 /* match from matchaddpos() */
10462 for (i = 1; i < 9; i++)
10463 {
10464 sprintf((char *)buf, (char *)"pos%d", i);
10465 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
10466 {
10467 if (di->di_tv.v_type != VAR_LIST)
10468 return;
10469
10470 list_append_tv(s, &di->di_tv);
10471 s->lv_refcount++;
10472 }
10473 else
10474 break;
10475 }
10476 }
10477
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010478 group = get_dict_string(d, (char_u *)"group", TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010479 priority = (int)get_dict_number(d, (char_u *)"priority");
10480 id = (int)get_dict_number(d, (char_u *)"id");
10481 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010482 ? get_dict_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010483 : NULL;
10484 if (i == 0)
10485 {
10486 match_add(curwin, group,
10487 get_dict_string(d, (char_u *)"pattern", FALSE),
10488 priority, id, NULL, conceal);
10489 }
10490 else
10491 {
10492 match_add(curwin, group, NULL, priority, id, s, conceal);
10493 list_unref(s);
10494 s = NULL;
10495 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010496 vim_free(group);
10497 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010498
10499 li = li->li_next;
10500 }
10501 rettv->vval.v_number = 0;
10502 }
10503#endif
10504}
10505
10506/*
10507 * "setpos()" function
10508 */
10509 static void
10510f_setpos(typval_T *argvars, typval_T *rettv)
10511{
10512 pos_T pos;
10513 int fnum;
10514 char_u *name;
10515 colnr_T curswant = -1;
10516
10517 rettv->vval.v_number = -1;
10518 name = get_tv_string_chk(argvars);
10519 if (name != NULL)
10520 {
10521 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
10522 {
10523 if (--pos.col < 0)
10524 pos.col = 0;
10525 if (name[0] == '.' && name[1] == NUL)
10526 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010527 /* set cursor; "fnum" is ignored */
10528 curwin->w_cursor = pos;
10529 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010530 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010531 curwin->w_curswant = curswant - 1;
10532 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010533 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010534 check_cursor();
10535 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010536 }
10537 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
10538 {
10539 /* set mark */
10540 if (setmark_pos(name[1], &pos, fnum) == OK)
10541 rettv->vval.v_number = 0;
10542 }
10543 else
10544 EMSG(_(e_invarg));
10545 }
10546 }
10547}
10548
10549/*
10550 * "setqflist()" function
10551 */
10552 static void
10553f_setqflist(typval_T *argvars, typval_T *rettv)
10554{
Bram Moolenaard823fa92016-08-12 16:29:27 +020010555 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010556}
10557
10558/*
10559 * "setreg()" function
10560 */
10561 static void
10562f_setreg(typval_T *argvars, typval_T *rettv)
10563{
10564 int regname;
10565 char_u *strregname;
10566 char_u *stropt;
10567 char_u *strval;
10568 int append;
10569 char_u yank_type;
10570 long block_len;
10571
10572 block_len = -1;
10573 yank_type = MAUTO;
10574 append = FALSE;
10575
10576 strregname = get_tv_string_chk(argvars);
10577 rettv->vval.v_number = 1; /* FAIL is default */
10578
10579 if (strregname == NULL)
10580 return; /* type error; errmsg already given */
10581 regname = *strregname;
10582 if (regname == 0 || regname == '@')
10583 regname = '"';
10584
10585 if (argvars[2].v_type != VAR_UNKNOWN)
10586 {
10587 stropt = get_tv_string_chk(&argvars[2]);
10588 if (stropt == NULL)
10589 return; /* type error */
10590 for (; *stropt != NUL; ++stropt)
10591 switch (*stropt)
10592 {
10593 case 'a': case 'A': /* append */
10594 append = TRUE;
10595 break;
10596 case 'v': case 'c': /* character-wise selection */
10597 yank_type = MCHAR;
10598 break;
10599 case 'V': case 'l': /* line-wise selection */
10600 yank_type = MLINE;
10601 break;
10602 case 'b': case Ctrl_V: /* block-wise selection */
10603 yank_type = MBLOCK;
10604 if (VIM_ISDIGIT(stropt[1]))
10605 {
10606 ++stropt;
10607 block_len = getdigits(&stropt) - 1;
10608 --stropt;
10609 }
10610 break;
10611 }
10612 }
10613
10614 if (argvars[1].v_type == VAR_LIST)
10615 {
10616 char_u **lstval;
10617 char_u **allocval;
10618 char_u buf[NUMBUFLEN];
10619 char_u **curval;
10620 char_u **curallocval;
10621 list_T *ll = argvars[1].vval.v_list;
10622 listitem_T *li;
10623 int len;
10624
10625 /* If the list is NULL handle like an empty list. */
10626 len = ll == NULL ? 0 : ll->lv_len;
10627
10628 /* First half: use for pointers to result lines; second half: use for
10629 * pointers to allocated copies. */
10630 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
10631 if (lstval == NULL)
10632 return;
10633 curval = lstval;
10634 allocval = lstval + len + 2;
10635 curallocval = allocval;
10636
10637 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
10638 li = li->li_next)
10639 {
10640 strval = get_tv_string_buf_chk(&li->li_tv, buf);
10641 if (strval == NULL)
10642 goto free_lstval;
10643 if (strval == buf)
10644 {
10645 /* Need to make a copy, next get_tv_string_buf_chk() will
10646 * overwrite the string. */
10647 strval = vim_strsave(buf);
10648 if (strval == NULL)
10649 goto free_lstval;
10650 *curallocval++ = strval;
10651 }
10652 *curval++ = strval;
10653 }
10654 *curval++ = NULL;
10655
10656 write_reg_contents_lst(regname, lstval, -1,
10657 append, yank_type, block_len);
10658free_lstval:
10659 while (curallocval > allocval)
10660 vim_free(*--curallocval);
10661 vim_free(lstval);
10662 }
10663 else
10664 {
10665 strval = get_tv_string_chk(&argvars[1]);
10666 if (strval == NULL)
10667 return;
10668 write_reg_contents_ex(regname, strval, -1,
10669 append, yank_type, block_len);
10670 }
10671 rettv->vval.v_number = 0;
10672}
10673
10674/*
10675 * "settabvar()" function
10676 */
10677 static void
10678f_settabvar(typval_T *argvars, typval_T *rettv)
10679{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010680 tabpage_T *save_curtab;
10681 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010682 char_u *varname, *tabvarname;
10683 typval_T *varp;
10684
10685 rettv->vval.v_number = 0;
10686
10687 if (check_restricted() || check_secure())
10688 return;
10689
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010690 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010691 varname = get_tv_string_chk(&argvars[1]);
10692 varp = &argvars[2];
10693
Bram Moolenaar4033c552017-09-16 20:54:51 +020010694 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010695 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010696 save_curtab = curtab;
10697 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010698
10699 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
10700 if (tabvarname != NULL)
10701 {
10702 STRCPY(tabvarname, "t:");
10703 STRCPY(tabvarname + 2, varname);
10704 set_var(tabvarname, varp, TRUE);
10705 vim_free(tabvarname);
10706 }
10707
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010708 /* Restore current tabpage */
10709 if (valid_tabpage(save_curtab))
10710 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010711 }
10712}
10713
10714/*
10715 * "settabwinvar()" function
10716 */
10717 static void
10718f_settabwinvar(typval_T *argvars, typval_T *rettv)
10719{
10720 setwinvar(argvars, rettv, 1);
10721}
10722
10723/*
10724 * "setwinvar()" function
10725 */
10726 static void
10727f_setwinvar(typval_T *argvars, typval_T *rettv)
10728{
10729 setwinvar(argvars, rettv, 0);
10730}
10731
10732#ifdef FEAT_CRYPT
10733/*
10734 * "sha256({string})" function
10735 */
10736 static void
10737f_sha256(typval_T *argvars, typval_T *rettv)
10738{
10739 char_u *p;
10740
10741 p = get_tv_string(&argvars[0]);
10742 rettv->vval.v_string = vim_strsave(
10743 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
10744 rettv->v_type = VAR_STRING;
10745}
10746#endif /* FEAT_CRYPT */
10747
10748/*
10749 * "shellescape({string})" function
10750 */
10751 static void
10752f_shellescape(typval_T *argvars, typval_T *rettv)
10753{
Bram Moolenaar20615522017-06-05 18:46:26 +020010754 int do_special = non_zero_arg(&argvars[1]);
10755
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010756 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar20615522017-06-05 18:46:26 +020010757 get_tv_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010758 rettv->v_type = VAR_STRING;
10759}
10760
10761/*
10762 * shiftwidth() function
10763 */
10764 static void
10765f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
10766{
10767 rettv->vval.v_number = get_sw_value(curbuf);
10768}
10769
10770/*
10771 * "simplify()" function
10772 */
10773 static void
10774f_simplify(typval_T *argvars, typval_T *rettv)
10775{
10776 char_u *p;
10777
10778 p = get_tv_string(&argvars[0]);
10779 rettv->vval.v_string = vim_strsave(p);
10780 simplify_filename(rettv->vval.v_string); /* simplify in place */
10781 rettv->v_type = VAR_STRING;
10782}
10783
10784#ifdef FEAT_FLOAT
10785/*
10786 * "sin()" function
10787 */
10788 static void
10789f_sin(typval_T *argvars, typval_T *rettv)
10790{
10791 float_T f = 0.0;
10792
10793 rettv->v_type = VAR_FLOAT;
10794 if (get_float_arg(argvars, &f) == OK)
10795 rettv->vval.v_float = sin(f);
10796 else
10797 rettv->vval.v_float = 0.0;
10798}
10799
10800/*
10801 * "sinh()" function
10802 */
10803 static void
10804f_sinh(typval_T *argvars, typval_T *rettv)
10805{
10806 float_T f = 0.0;
10807
10808 rettv->v_type = VAR_FLOAT;
10809 if (get_float_arg(argvars, &f) == OK)
10810 rettv->vval.v_float = sinh(f);
10811 else
10812 rettv->vval.v_float = 0.0;
10813}
10814#endif
10815
10816static int
10817#ifdef __BORLANDC__
10818 _RTLENTRYF
10819#endif
10820 item_compare(const void *s1, const void *s2);
10821static int
10822#ifdef __BORLANDC__
10823 _RTLENTRYF
10824#endif
10825 item_compare2(const void *s1, const void *s2);
10826
10827/* struct used in the array that's given to qsort() */
10828typedef struct
10829{
10830 listitem_T *item;
10831 int idx;
10832} sortItem_T;
10833
10834/* struct storing information about current sort */
10835typedef struct
10836{
10837 int item_compare_ic;
10838 int item_compare_numeric;
10839 int item_compare_numbers;
10840#ifdef FEAT_FLOAT
10841 int item_compare_float;
10842#endif
10843 char_u *item_compare_func;
10844 partial_T *item_compare_partial;
10845 dict_T *item_compare_selfdict;
10846 int item_compare_func_err;
10847 int item_compare_keep_zero;
10848} sortinfo_T;
10849static sortinfo_T *sortinfo = NULL;
10850static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
10851#define ITEM_COMPARE_FAIL 999
10852
10853/*
10854 * Compare functions for f_sort() and f_uniq() below.
10855 */
10856 static int
10857#ifdef __BORLANDC__
10858_RTLENTRYF
10859#endif
10860item_compare(const void *s1, const void *s2)
10861{
10862 sortItem_T *si1, *si2;
10863 typval_T *tv1, *tv2;
10864 char_u *p1, *p2;
10865 char_u *tofree1 = NULL, *tofree2 = NULL;
10866 int res;
10867 char_u numbuf1[NUMBUFLEN];
10868 char_u numbuf2[NUMBUFLEN];
10869
10870 si1 = (sortItem_T *)s1;
10871 si2 = (sortItem_T *)s2;
10872 tv1 = &si1->item->li_tv;
10873 tv2 = &si2->item->li_tv;
10874
10875 if (sortinfo->item_compare_numbers)
10876 {
10877 varnumber_T v1 = get_tv_number(tv1);
10878 varnumber_T v2 = get_tv_number(tv2);
10879
10880 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
10881 }
10882
10883#ifdef FEAT_FLOAT
10884 if (sortinfo->item_compare_float)
10885 {
10886 float_T v1 = get_tv_float(tv1);
10887 float_T v2 = get_tv_float(tv2);
10888
10889 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
10890 }
10891#endif
10892
10893 /* tv2string() puts quotes around a string and allocates memory. Don't do
10894 * that for string variables. Use a single quote when comparing with a
10895 * non-string to do what the docs promise. */
10896 if (tv1->v_type == VAR_STRING)
10897 {
10898 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
10899 p1 = (char_u *)"'";
10900 else
10901 p1 = tv1->vval.v_string;
10902 }
10903 else
10904 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
10905 if (tv2->v_type == VAR_STRING)
10906 {
10907 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
10908 p2 = (char_u *)"'";
10909 else
10910 p2 = tv2->vval.v_string;
10911 }
10912 else
10913 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
10914 if (p1 == NULL)
10915 p1 = (char_u *)"";
10916 if (p2 == NULL)
10917 p2 = (char_u *)"";
10918 if (!sortinfo->item_compare_numeric)
10919 {
10920 if (sortinfo->item_compare_ic)
10921 res = STRICMP(p1, p2);
10922 else
10923 res = STRCMP(p1, p2);
10924 }
10925 else
10926 {
10927 double n1, n2;
10928 n1 = strtod((char *)p1, (char **)&p1);
10929 n2 = strtod((char *)p2, (char **)&p2);
10930 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
10931 }
10932
10933 /* When the result would be zero, compare the item indexes. Makes the
10934 * sort stable. */
10935 if (res == 0 && !sortinfo->item_compare_keep_zero)
10936 res = si1->idx > si2->idx ? 1 : -1;
10937
10938 vim_free(tofree1);
10939 vim_free(tofree2);
10940 return res;
10941}
10942
10943 static int
10944#ifdef __BORLANDC__
10945_RTLENTRYF
10946#endif
10947item_compare2(const void *s1, const void *s2)
10948{
10949 sortItem_T *si1, *si2;
10950 int res;
10951 typval_T rettv;
10952 typval_T argv[3];
10953 int dummy;
10954 char_u *func_name;
10955 partial_T *partial = sortinfo->item_compare_partial;
10956
10957 /* shortcut after failure in previous call; compare all items equal */
10958 if (sortinfo->item_compare_func_err)
10959 return 0;
10960
10961 si1 = (sortItem_T *)s1;
10962 si2 = (sortItem_T *)s2;
10963
10964 if (partial == NULL)
10965 func_name = sortinfo->item_compare_func;
10966 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020010967 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010968
10969 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
10970 * in the copy without changing the original list items. */
10971 copy_tv(&si1->item->li_tv, &argv[0]);
10972 copy_tv(&si2->item->li_tv, &argv[1]);
10973
10974 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
10975 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020010976 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010977 partial, sortinfo->item_compare_selfdict);
10978 clear_tv(&argv[0]);
10979 clear_tv(&argv[1]);
10980
10981 if (res == FAIL)
10982 res = ITEM_COMPARE_FAIL;
10983 else
10984 res = (int)get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
10985 if (sortinfo->item_compare_func_err)
10986 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
10987 clear_tv(&rettv);
10988
10989 /* When the result would be zero, compare the pointers themselves. Makes
10990 * the sort stable. */
10991 if (res == 0 && !sortinfo->item_compare_keep_zero)
10992 res = si1->idx > si2->idx ? 1 : -1;
10993
10994 return res;
10995}
10996
10997/*
10998 * "sort({list})" function
10999 */
11000 static void
11001do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
11002{
11003 list_T *l;
11004 listitem_T *li;
11005 sortItem_T *ptrs;
11006 sortinfo_T *old_sortinfo;
11007 sortinfo_T info;
11008 long len;
11009 long i;
11010
11011 /* Pointer to current info struct used in compare function. Save and
11012 * restore the current one for nested calls. */
11013 old_sortinfo = sortinfo;
11014 sortinfo = &info;
11015
11016 if (argvars[0].v_type != VAR_LIST)
11017 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
11018 else
11019 {
11020 l = argvars[0].vval.v_list;
11021 if (l == NULL || tv_check_lock(l->lv_lock,
11022 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
11023 TRUE))
11024 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011025 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011026
11027 len = list_len(l);
11028 if (len <= 1)
11029 goto theend; /* short list sorts pretty quickly */
11030
11031 info.item_compare_ic = FALSE;
11032 info.item_compare_numeric = FALSE;
11033 info.item_compare_numbers = FALSE;
11034#ifdef FEAT_FLOAT
11035 info.item_compare_float = FALSE;
11036#endif
11037 info.item_compare_func = NULL;
11038 info.item_compare_partial = NULL;
11039 info.item_compare_selfdict = NULL;
11040 if (argvars[1].v_type != VAR_UNKNOWN)
11041 {
11042 /* optional second argument: {func} */
11043 if (argvars[1].v_type == VAR_FUNC)
11044 info.item_compare_func = argvars[1].vval.v_string;
11045 else if (argvars[1].v_type == VAR_PARTIAL)
11046 info.item_compare_partial = argvars[1].vval.v_partial;
11047 else
11048 {
11049 int error = FALSE;
11050
11051 i = (long)get_tv_number_chk(&argvars[1], &error);
11052 if (error)
11053 goto theend; /* type error; errmsg already given */
11054 if (i == 1)
11055 info.item_compare_ic = TRUE;
11056 else if (argvars[1].v_type != VAR_NUMBER)
11057 info.item_compare_func = get_tv_string(&argvars[1]);
11058 else if (i != 0)
11059 {
11060 EMSG(_(e_invarg));
11061 goto theend;
11062 }
11063 if (info.item_compare_func != NULL)
11064 {
11065 if (*info.item_compare_func == NUL)
11066 {
11067 /* empty string means default sort */
11068 info.item_compare_func = NULL;
11069 }
11070 else if (STRCMP(info.item_compare_func, "n") == 0)
11071 {
11072 info.item_compare_func = NULL;
11073 info.item_compare_numeric = TRUE;
11074 }
11075 else if (STRCMP(info.item_compare_func, "N") == 0)
11076 {
11077 info.item_compare_func = NULL;
11078 info.item_compare_numbers = TRUE;
11079 }
11080#ifdef FEAT_FLOAT
11081 else if (STRCMP(info.item_compare_func, "f") == 0)
11082 {
11083 info.item_compare_func = NULL;
11084 info.item_compare_float = TRUE;
11085 }
11086#endif
11087 else if (STRCMP(info.item_compare_func, "i") == 0)
11088 {
11089 info.item_compare_func = NULL;
11090 info.item_compare_ic = TRUE;
11091 }
11092 }
11093 }
11094
11095 if (argvars[2].v_type != VAR_UNKNOWN)
11096 {
11097 /* optional third argument: {dict} */
11098 if (argvars[2].v_type != VAR_DICT)
11099 {
11100 EMSG(_(e_dictreq));
11101 goto theend;
11102 }
11103 info.item_compare_selfdict = argvars[2].vval.v_dict;
11104 }
11105 }
11106
11107 /* Make an array with each entry pointing to an item in the List. */
11108 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
11109 if (ptrs == NULL)
11110 goto theend;
11111
11112 i = 0;
11113 if (sort)
11114 {
11115 /* sort(): ptrs will be the list to sort */
11116 for (li = l->lv_first; li != NULL; li = li->li_next)
11117 {
11118 ptrs[i].item = li;
11119 ptrs[i].idx = i;
11120 ++i;
11121 }
11122
11123 info.item_compare_func_err = FALSE;
11124 info.item_compare_keep_zero = FALSE;
11125 /* test the compare function */
11126 if ((info.item_compare_func != NULL
11127 || info.item_compare_partial != NULL)
11128 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
11129 == ITEM_COMPARE_FAIL)
11130 EMSG(_("E702: Sort compare function failed"));
11131 else
11132 {
11133 /* Sort the array with item pointers. */
11134 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
11135 info.item_compare_func == NULL
11136 && info.item_compare_partial == NULL
11137 ? item_compare : item_compare2);
11138
11139 if (!info.item_compare_func_err)
11140 {
11141 /* Clear the List and append the items in sorted order. */
11142 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
11143 l->lv_len = 0;
11144 for (i = 0; i < len; ++i)
11145 list_append(l, ptrs[i].item);
11146 }
11147 }
11148 }
11149 else
11150 {
11151 int (*item_compare_func_ptr)(const void *, const void *);
11152
11153 /* f_uniq(): ptrs will be a stack of items to remove */
11154 info.item_compare_func_err = FALSE;
11155 info.item_compare_keep_zero = TRUE;
11156 item_compare_func_ptr = info.item_compare_func != NULL
11157 || info.item_compare_partial != NULL
11158 ? item_compare2 : item_compare;
11159
11160 for (li = l->lv_first; li != NULL && li->li_next != NULL;
11161 li = li->li_next)
11162 {
11163 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
11164 == 0)
11165 ptrs[i++].item = li;
11166 if (info.item_compare_func_err)
11167 {
11168 EMSG(_("E882: Uniq compare function failed"));
11169 break;
11170 }
11171 }
11172
11173 if (!info.item_compare_func_err)
11174 {
11175 while (--i >= 0)
11176 {
11177 li = ptrs[i].item->li_next;
11178 ptrs[i].item->li_next = li->li_next;
11179 if (li->li_next != NULL)
11180 li->li_next->li_prev = ptrs[i].item;
11181 else
11182 l->lv_last = ptrs[i].item;
11183 list_fix_watch(l, li);
11184 listitem_free(li);
11185 l->lv_len--;
11186 }
11187 }
11188 }
11189
11190 vim_free(ptrs);
11191 }
11192theend:
11193 sortinfo = old_sortinfo;
11194}
11195
11196/*
11197 * "sort({list})" function
11198 */
11199 static void
11200f_sort(typval_T *argvars, typval_T *rettv)
11201{
11202 do_sort_uniq(argvars, rettv, TRUE);
11203}
11204
11205/*
11206 * "uniq({list})" function
11207 */
11208 static void
11209f_uniq(typval_T *argvars, typval_T *rettv)
11210{
11211 do_sort_uniq(argvars, rettv, FALSE);
11212}
11213
11214/*
11215 * "soundfold({word})" function
11216 */
11217 static void
11218f_soundfold(typval_T *argvars, typval_T *rettv)
11219{
11220 char_u *s;
11221
11222 rettv->v_type = VAR_STRING;
11223 s = get_tv_string(&argvars[0]);
11224#ifdef FEAT_SPELL
11225 rettv->vval.v_string = eval_soundfold(s);
11226#else
11227 rettv->vval.v_string = vim_strsave(s);
11228#endif
11229}
11230
11231/*
11232 * "spellbadword()" function
11233 */
11234 static void
11235f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
11236{
11237 char_u *word = (char_u *)"";
11238 hlf_T attr = HLF_COUNT;
11239 int len = 0;
11240
11241 if (rettv_list_alloc(rettv) == FAIL)
11242 return;
11243
11244#ifdef FEAT_SPELL
11245 if (argvars[0].v_type == VAR_UNKNOWN)
11246 {
11247 /* Find the start and length of the badly spelled word. */
11248 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
11249 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011250 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011251 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011252 curwin->w_set_curswant = TRUE;
11253 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011254 }
11255 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
11256 {
11257 char_u *str = get_tv_string_chk(&argvars[0]);
11258 int capcol = -1;
11259
11260 if (str != NULL)
11261 {
11262 /* Check the argument for spelling. */
11263 while (*str != NUL)
11264 {
11265 len = spell_check(curwin, str, &attr, &capcol, FALSE);
11266 if (attr != HLF_COUNT)
11267 {
11268 word = str;
11269 break;
11270 }
11271 str += len;
11272 }
11273 }
11274 }
11275#endif
11276
11277 list_append_string(rettv->vval.v_list, word, len);
11278 list_append_string(rettv->vval.v_list, (char_u *)(
11279 attr == HLF_SPB ? "bad" :
11280 attr == HLF_SPR ? "rare" :
11281 attr == HLF_SPL ? "local" :
11282 attr == HLF_SPC ? "caps" :
11283 ""), -1);
11284}
11285
11286/*
11287 * "spellsuggest()" function
11288 */
11289 static void
11290f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
11291{
11292#ifdef FEAT_SPELL
11293 char_u *str;
11294 int typeerr = FALSE;
11295 int maxcount;
11296 garray_T ga;
11297 int i;
11298 listitem_T *li;
11299 int need_capital = FALSE;
11300#endif
11301
11302 if (rettv_list_alloc(rettv) == FAIL)
11303 return;
11304
11305#ifdef FEAT_SPELL
11306 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
11307 {
11308 str = get_tv_string(&argvars[0]);
11309 if (argvars[1].v_type != VAR_UNKNOWN)
11310 {
11311 maxcount = (int)get_tv_number_chk(&argvars[1], &typeerr);
11312 if (maxcount <= 0)
11313 return;
11314 if (argvars[2].v_type != VAR_UNKNOWN)
11315 {
11316 need_capital = (int)get_tv_number_chk(&argvars[2], &typeerr);
11317 if (typeerr)
11318 return;
11319 }
11320 }
11321 else
11322 maxcount = 25;
11323
11324 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
11325
11326 for (i = 0; i < ga.ga_len; ++i)
11327 {
11328 str = ((char_u **)ga.ga_data)[i];
11329
11330 li = listitem_alloc();
11331 if (li == NULL)
11332 vim_free(str);
11333 else
11334 {
11335 li->li_tv.v_type = VAR_STRING;
11336 li->li_tv.v_lock = 0;
11337 li->li_tv.vval.v_string = str;
11338 list_append(rettv->vval.v_list, li);
11339 }
11340 }
11341 ga_clear(&ga);
11342 }
11343#endif
11344}
11345
11346 static void
11347f_split(typval_T *argvars, typval_T *rettv)
11348{
11349 char_u *str;
11350 char_u *end;
11351 char_u *pat = NULL;
11352 regmatch_T regmatch;
11353 char_u patbuf[NUMBUFLEN];
11354 char_u *save_cpo;
11355 int match;
11356 colnr_T col = 0;
11357 int keepempty = FALSE;
11358 int typeerr = FALSE;
11359
11360 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11361 save_cpo = p_cpo;
11362 p_cpo = (char_u *)"";
11363
11364 str = get_tv_string(&argvars[0]);
11365 if (argvars[1].v_type != VAR_UNKNOWN)
11366 {
11367 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11368 if (pat == NULL)
11369 typeerr = TRUE;
11370 if (argvars[2].v_type != VAR_UNKNOWN)
11371 keepempty = (int)get_tv_number_chk(&argvars[2], &typeerr);
11372 }
11373 if (pat == NULL || *pat == NUL)
11374 pat = (char_u *)"[\\x01- ]\\+";
11375
11376 if (rettv_list_alloc(rettv) == FAIL)
11377 return;
11378 if (typeerr)
11379 return;
11380
11381 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11382 if (regmatch.regprog != NULL)
11383 {
11384 regmatch.rm_ic = FALSE;
11385 while (*str != NUL || keepempty)
11386 {
11387 if (*str == NUL)
11388 match = FALSE; /* empty item at the end */
11389 else
11390 match = vim_regexec_nl(&regmatch, str, col);
11391 if (match)
11392 end = regmatch.startp[0];
11393 else
11394 end = str + STRLEN(str);
11395 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
11396 && *str != NUL && match && end < regmatch.endp[0]))
11397 {
11398 if (list_append_string(rettv->vval.v_list, str,
11399 (int)(end - str)) == FAIL)
11400 break;
11401 }
11402 if (!match)
11403 break;
11404 /* Advance to just after the match. */
11405 if (regmatch.endp[0] > str)
11406 col = 0;
11407 else
11408 {
11409 /* Don't get stuck at the same match. */
11410#ifdef FEAT_MBYTE
11411 col = (*mb_ptr2len)(regmatch.endp[0]);
11412#else
11413 col = 1;
11414#endif
11415 }
11416 str = regmatch.endp[0];
11417 }
11418
11419 vim_regfree(regmatch.regprog);
11420 }
11421
11422 p_cpo = save_cpo;
11423}
11424
11425#ifdef FEAT_FLOAT
11426/*
11427 * "sqrt()" function
11428 */
11429 static void
11430f_sqrt(typval_T *argvars, typval_T *rettv)
11431{
11432 float_T f = 0.0;
11433
11434 rettv->v_type = VAR_FLOAT;
11435 if (get_float_arg(argvars, &f) == OK)
11436 rettv->vval.v_float = sqrt(f);
11437 else
11438 rettv->vval.v_float = 0.0;
11439}
11440
11441/*
11442 * "str2float()" function
11443 */
11444 static void
11445f_str2float(typval_T *argvars, typval_T *rettv)
11446{
11447 char_u *p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011448 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011449
Bram Moolenaar08243d22017-01-10 16:12:29 +010011450 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011451 p = skipwhite(p + 1);
11452 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011453 if (isneg)
11454 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011455 rettv->v_type = VAR_FLOAT;
11456}
11457#endif
11458
11459/*
11460 * "str2nr()" function
11461 */
11462 static void
11463f_str2nr(typval_T *argvars, typval_T *rettv)
11464{
11465 int base = 10;
11466 char_u *p;
11467 varnumber_T n;
11468 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010011469 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011470
11471 if (argvars[1].v_type != VAR_UNKNOWN)
11472 {
11473 base = (int)get_tv_number(&argvars[1]);
11474 if (base != 2 && base != 8 && base != 10 && base != 16)
11475 {
11476 EMSG(_(e_invarg));
11477 return;
11478 }
11479 }
11480
11481 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011482 isneg = (*p == '-');
11483 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011484 p = skipwhite(p + 1);
11485 switch (base)
11486 {
11487 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
11488 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
11489 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
11490 default: what = 0;
11491 }
11492 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011493 if (isneg)
11494 rettv->vval.v_number = -n;
11495 else
11496 rettv->vval.v_number = n;
11497
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011498}
11499
11500#ifdef HAVE_STRFTIME
11501/*
11502 * "strftime({format}[, {time}])" function
11503 */
11504 static void
11505f_strftime(typval_T *argvars, typval_T *rettv)
11506{
11507 char_u result_buf[256];
11508 struct tm *curtime;
11509 time_t seconds;
11510 char_u *p;
11511
11512 rettv->v_type = VAR_STRING;
11513
11514 p = get_tv_string(&argvars[0]);
11515 if (argvars[1].v_type == VAR_UNKNOWN)
11516 seconds = time(NULL);
11517 else
11518 seconds = (time_t)get_tv_number(&argvars[1]);
11519 curtime = localtime(&seconds);
11520 /* MSVC returns NULL for an invalid value of seconds. */
11521 if (curtime == NULL)
11522 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
11523 else
11524 {
11525# ifdef FEAT_MBYTE
11526 vimconv_T conv;
11527 char_u *enc;
11528
11529 conv.vc_type = CONV_NONE;
11530 enc = enc_locale();
11531 convert_setup(&conv, p_enc, enc);
11532 if (conv.vc_type != CONV_NONE)
11533 p = string_convert(&conv, p, NULL);
11534# endif
11535 if (p != NULL)
11536 (void)strftime((char *)result_buf, sizeof(result_buf),
11537 (char *)p, curtime);
11538 else
11539 result_buf[0] = NUL;
11540
11541# ifdef FEAT_MBYTE
11542 if (conv.vc_type != CONV_NONE)
11543 vim_free(p);
11544 convert_setup(&conv, enc, p_enc);
11545 if (conv.vc_type != CONV_NONE)
11546 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
11547 else
11548# endif
11549 rettv->vval.v_string = vim_strsave(result_buf);
11550
11551# ifdef FEAT_MBYTE
11552 /* Release conversion descriptors */
11553 convert_setup(&conv, NULL, NULL);
11554 vim_free(enc);
11555# endif
11556 }
11557}
11558#endif
11559
11560/*
11561 * "strgetchar()" function
11562 */
11563 static void
11564f_strgetchar(typval_T *argvars, typval_T *rettv)
11565{
11566 char_u *str;
11567 int len;
11568 int error = FALSE;
11569 int charidx;
11570
11571 rettv->vval.v_number = -1;
11572 str = get_tv_string_chk(&argvars[0]);
11573 if (str == NULL)
11574 return;
11575 len = (int)STRLEN(str);
11576 charidx = (int)get_tv_number_chk(&argvars[1], &error);
11577 if (error)
11578 return;
11579#ifdef FEAT_MBYTE
11580 {
11581 int byteidx = 0;
11582
11583 while (charidx >= 0 && byteidx < len)
11584 {
11585 if (charidx == 0)
11586 {
11587 rettv->vval.v_number = mb_ptr2char(str + byteidx);
11588 break;
11589 }
11590 --charidx;
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011591 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011592 }
11593 }
11594#else
11595 if (charidx < len)
11596 rettv->vval.v_number = str[charidx];
11597#endif
11598}
11599
11600/*
11601 * "stridx()" function
11602 */
11603 static void
11604f_stridx(typval_T *argvars, typval_T *rettv)
11605{
11606 char_u buf[NUMBUFLEN];
11607 char_u *needle;
11608 char_u *haystack;
11609 char_u *save_haystack;
11610 char_u *pos;
11611 int start_idx;
11612
11613 needle = get_tv_string_chk(&argvars[1]);
11614 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
11615 rettv->vval.v_number = -1;
11616 if (needle == NULL || haystack == NULL)
11617 return; /* type error; errmsg already given */
11618
11619 if (argvars[2].v_type != VAR_UNKNOWN)
11620 {
11621 int error = FALSE;
11622
11623 start_idx = (int)get_tv_number_chk(&argvars[2], &error);
11624 if (error || start_idx >= (int)STRLEN(haystack))
11625 return;
11626 if (start_idx >= 0)
11627 haystack += start_idx;
11628 }
11629
11630 pos = (char_u *)strstr((char *)haystack, (char *)needle);
11631 if (pos != NULL)
11632 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
11633}
11634
11635/*
11636 * "string()" function
11637 */
11638 static void
11639f_string(typval_T *argvars, typval_T *rettv)
11640{
11641 char_u *tofree;
11642 char_u numbuf[NUMBUFLEN];
11643
11644 rettv->v_type = VAR_STRING;
11645 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
11646 get_copyID());
11647 /* Make a copy if we have a value but it's not in allocated memory. */
11648 if (rettv->vval.v_string != NULL && tofree == NULL)
11649 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
11650}
11651
11652/*
11653 * "strlen()" function
11654 */
11655 static void
11656f_strlen(typval_T *argvars, typval_T *rettv)
11657{
11658 rettv->vval.v_number = (varnumber_T)(STRLEN(
11659 get_tv_string(&argvars[0])));
11660}
11661
11662/*
11663 * "strchars()" function
11664 */
11665 static void
11666f_strchars(typval_T *argvars, typval_T *rettv)
11667{
11668 char_u *s = get_tv_string(&argvars[0]);
11669 int skipcc = 0;
11670#ifdef FEAT_MBYTE
11671 varnumber_T len = 0;
11672 int (*func_mb_ptr2char_adv)(char_u **pp);
11673#endif
11674
11675 if (argvars[1].v_type != VAR_UNKNOWN)
11676 skipcc = (int)get_tv_number_chk(&argvars[1], NULL);
11677 if (skipcc < 0 || skipcc > 1)
11678 EMSG(_(e_invarg));
11679 else
11680 {
11681#ifdef FEAT_MBYTE
11682 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
11683 while (*s != NUL)
11684 {
11685 func_mb_ptr2char_adv(&s);
11686 ++len;
11687 }
11688 rettv->vval.v_number = len;
11689#else
11690 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
11691#endif
11692 }
11693}
11694
11695/*
11696 * "strdisplaywidth()" function
11697 */
11698 static void
11699f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
11700{
11701 char_u *s = get_tv_string(&argvars[0]);
11702 int col = 0;
11703
11704 if (argvars[1].v_type != VAR_UNKNOWN)
11705 col = (int)get_tv_number(&argvars[1]);
11706
11707 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
11708}
11709
11710/*
11711 * "strwidth()" function
11712 */
11713 static void
11714f_strwidth(typval_T *argvars, typval_T *rettv)
11715{
11716 char_u *s = get_tv_string(&argvars[0]);
11717
11718 rettv->vval.v_number = (varnumber_T)(
11719#ifdef FEAT_MBYTE
11720 mb_string2cells(s, -1)
11721#else
11722 STRLEN(s)
11723#endif
11724 );
11725}
11726
11727/*
11728 * "strcharpart()" function
11729 */
11730 static void
11731f_strcharpart(typval_T *argvars, typval_T *rettv)
11732{
11733#ifdef FEAT_MBYTE
11734 char_u *p;
11735 int nchar;
11736 int nbyte = 0;
11737 int charlen;
11738 int len = 0;
11739 int slen;
11740 int error = FALSE;
11741
11742 p = get_tv_string(&argvars[0]);
11743 slen = (int)STRLEN(p);
11744
11745 nchar = (int)get_tv_number_chk(&argvars[1], &error);
11746 if (!error)
11747 {
11748 if (nchar > 0)
11749 while (nchar > 0 && nbyte < slen)
11750 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011751 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011752 --nchar;
11753 }
11754 else
11755 nbyte = nchar;
11756 if (argvars[2].v_type != VAR_UNKNOWN)
11757 {
11758 charlen = (int)get_tv_number(&argvars[2]);
11759 while (charlen > 0 && nbyte + len < slen)
11760 {
11761 int off = nbyte + len;
11762
11763 if (off < 0)
11764 len += 1;
11765 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011766 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011767 --charlen;
11768 }
11769 }
11770 else
11771 len = slen - nbyte; /* default: all bytes that are available. */
11772 }
11773
11774 /*
11775 * Only return the overlap between the specified part and the actual
11776 * string.
11777 */
11778 if (nbyte < 0)
11779 {
11780 len += nbyte;
11781 nbyte = 0;
11782 }
11783 else if (nbyte > slen)
11784 nbyte = slen;
11785 if (len < 0)
11786 len = 0;
11787 else if (nbyte + len > slen)
11788 len = slen - nbyte;
11789
11790 rettv->v_type = VAR_STRING;
11791 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
11792#else
11793 f_strpart(argvars, rettv);
11794#endif
11795}
11796
11797/*
11798 * "strpart()" function
11799 */
11800 static void
11801f_strpart(typval_T *argvars, typval_T *rettv)
11802{
11803 char_u *p;
11804 int n;
11805 int len;
11806 int slen;
11807 int error = FALSE;
11808
11809 p = get_tv_string(&argvars[0]);
11810 slen = (int)STRLEN(p);
11811
11812 n = (int)get_tv_number_chk(&argvars[1], &error);
11813 if (error)
11814 len = 0;
11815 else if (argvars[2].v_type != VAR_UNKNOWN)
11816 len = (int)get_tv_number(&argvars[2]);
11817 else
11818 len = slen - n; /* default len: all bytes that are available. */
11819
11820 /*
11821 * Only return the overlap between the specified part and the actual
11822 * string.
11823 */
11824 if (n < 0)
11825 {
11826 len += n;
11827 n = 0;
11828 }
11829 else if (n > slen)
11830 n = slen;
11831 if (len < 0)
11832 len = 0;
11833 else if (n + len > slen)
11834 len = slen - n;
11835
11836 rettv->v_type = VAR_STRING;
11837 rettv->vval.v_string = vim_strnsave(p + n, len);
11838}
11839
11840/*
11841 * "strridx()" function
11842 */
11843 static void
11844f_strridx(typval_T *argvars, typval_T *rettv)
11845{
11846 char_u buf[NUMBUFLEN];
11847 char_u *needle;
11848 char_u *haystack;
11849 char_u *rest;
11850 char_u *lastmatch = NULL;
11851 int haystack_len, end_idx;
11852
11853 needle = get_tv_string_chk(&argvars[1]);
11854 haystack = get_tv_string_buf_chk(&argvars[0], buf);
11855
11856 rettv->vval.v_number = -1;
11857 if (needle == NULL || haystack == NULL)
11858 return; /* type error; errmsg already given */
11859
11860 haystack_len = (int)STRLEN(haystack);
11861 if (argvars[2].v_type != VAR_UNKNOWN)
11862 {
11863 /* Third argument: upper limit for index */
11864 end_idx = (int)get_tv_number_chk(&argvars[2], NULL);
11865 if (end_idx < 0)
11866 return; /* can never find a match */
11867 }
11868 else
11869 end_idx = haystack_len;
11870
11871 if (*needle == NUL)
11872 {
11873 /* Empty string matches past the end. */
11874 lastmatch = haystack + end_idx;
11875 }
11876 else
11877 {
11878 for (rest = haystack; *rest != '\0'; ++rest)
11879 {
11880 rest = (char_u *)strstr((char *)rest, (char *)needle);
11881 if (rest == NULL || rest > haystack + end_idx)
11882 break;
11883 lastmatch = rest;
11884 }
11885 }
11886
11887 if (lastmatch == NULL)
11888 rettv->vval.v_number = -1;
11889 else
11890 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
11891}
11892
11893/*
11894 * "strtrans()" function
11895 */
11896 static void
11897f_strtrans(typval_T *argvars, typval_T *rettv)
11898{
11899 rettv->v_type = VAR_STRING;
11900 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
11901}
11902
11903/*
11904 * "submatch()" function
11905 */
11906 static void
11907f_submatch(typval_T *argvars, typval_T *rettv)
11908{
11909 int error = FALSE;
11910 int no;
11911 int retList = 0;
11912
11913 no = (int)get_tv_number_chk(&argvars[0], &error);
11914 if (error)
11915 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020011916 if (no < 0 || no >= NSUBEXP)
11917 {
Bram Moolenaar79518e22017-02-17 16:31:35 +010011918 EMSGN(_("E935: invalid submatch number: %d"), no);
11919 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020011920 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011921 if (argvars[1].v_type != VAR_UNKNOWN)
11922 retList = (int)get_tv_number_chk(&argvars[1], &error);
11923 if (error)
11924 return;
11925
11926 if (retList == 0)
11927 {
11928 rettv->v_type = VAR_STRING;
11929 rettv->vval.v_string = reg_submatch(no);
11930 }
11931 else
11932 {
11933 rettv->v_type = VAR_LIST;
11934 rettv->vval.v_list = reg_submatch_list(no);
11935 }
11936}
11937
11938/*
11939 * "substitute()" function
11940 */
11941 static void
11942f_substitute(typval_T *argvars, typval_T *rettv)
11943{
11944 char_u patbuf[NUMBUFLEN];
11945 char_u subbuf[NUMBUFLEN];
11946 char_u flagsbuf[NUMBUFLEN];
11947
11948 char_u *str = get_tv_string_chk(&argvars[0]);
11949 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011950 char_u *sub = NULL;
11951 typval_T *expr = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011952 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
11953
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011954 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
11955 expr = &argvars[2];
11956 else
11957 sub = get_tv_string_buf_chk(&argvars[2], subbuf);
11958
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011959 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011960 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
11961 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011962 rettv->vval.v_string = NULL;
11963 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011964 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011965}
11966
11967/*
11968 * "synID(lnum, col, trans)" function
11969 */
11970 static void
11971f_synID(typval_T *argvars UNUSED, typval_T *rettv)
11972{
11973 int id = 0;
11974#ifdef FEAT_SYN_HL
11975 linenr_T lnum;
11976 colnr_T col;
11977 int trans;
11978 int transerr = FALSE;
11979
11980 lnum = get_tv_lnum(argvars); /* -1 on type error */
11981 col = (linenr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
11982 trans = (int)get_tv_number_chk(&argvars[2], &transerr);
11983
11984 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
11985 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
11986 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
11987#endif
11988
11989 rettv->vval.v_number = id;
11990}
11991
11992/*
11993 * "synIDattr(id, what [, mode])" function
11994 */
11995 static void
11996f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
11997{
11998 char_u *p = NULL;
11999#ifdef FEAT_SYN_HL
12000 int id;
12001 char_u *what;
12002 char_u *mode;
12003 char_u modebuf[NUMBUFLEN];
12004 int modec;
12005
12006 id = (int)get_tv_number(&argvars[0]);
12007 what = get_tv_string(&argvars[1]);
12008 if (argvars[2].v_type != VAR_UNKNOWN)
12009 {
12010 mode = get_tv_string_buf(&argvars[2], modebuf);
12011 modec = TOLOWER_ASC(mode[0]);
12012 if (modec != 't' && modec != 'c' && modec != 'g')
12013 modec = 0; /* replace invalid with current */
12014 }
12015 else
12016 {
12017#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
12018 if (USE_24BIT)
12019 modec = 'g';
12020 else
12021#endif
12022 if (t_colors > 1)
12023 modec = 'c';
12024 else
12025 modec = 't';
12026 }
12027
12028
12029 switch (TOLOWER_ASC(what[0]))
12030 {
12031 case 'b':
12032 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
12033 p = highlight_color(id, what, modec);
12034 else /* bold */
12035 p = highlight_has_attr(id, HL_BOLD, modec);
12036 break;
12037
12038 case 'f': /* fg[#] or font */
12039 p = highlight_color(id, what, modec);
12040 break;
12041
12042 case 'i':
12043 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
12044 p = highlight_has_attr(id, HL_INVERSE, modec);
12045 else /* italic */
12046 p = highlight_has_attr(id, HL_ITALIC, modec);
12047 break;
12048
12049 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020012050 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012051 break;
12052
12053 case 'r': /* reverse */
12054 p = highlight_has_attr(id, HL_INVERSE, modec);
12055 break;
12056
12057 case 's':
12058 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
12059 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020012060 /* strikeout */
12061 else if (TOLOWER_ASC(what[1]) == 't' &&
12062 TOLOWER_ASC(what[2]) == 'r')
12063 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012064 else /* standout */
12065 p = highlight_has_attr(id, HL_STANDOUT, modec);
12066 break;
12067
12068 case 'u':
12069 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
12070 /* underline */
12071 p = highlight_has_attr(id, HL_UNDERLINE, modec);
12072 else
12073 /* undercurl */
12074 p = highlight_has_attr(id, HL_UNDERCURL, modec);
12075 break;
12076 }
12077
12078 if (p != NULL)
12079 p = vim_strsave(p);
12080#endif
12081 rettv->v_type = VAR_STRING;
12082 rettv->vval.v_string = p;
12083}
12084
12085/*
12086 * "synIDtrans(id)" function
12087 */
12088 static void
12089f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
12090{
12091 int id;
12092
12093#ifdef FEAT_SYN_HL
12094 id = (int)get_tv_number(&argvars[0]);
12095
12096 if (id > 0)
12097 id = syn_get_final_id(id);
12098 else
12099#endif
12100 id = 0;
12101
12102 rettv->vval.v_number = id;
12103}
12104
12105/*
12106 * "synconcealed(lnum, col)" function
12107 */
12108 static void
12109f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
12110{
12111#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12112 linenr_T lnum;
12113 colnr_T col;
12114 int syntax_flags = 0;
12115 int cchar;
12116 int matchid = 0;
12117 char_u str[NUMBUFLEN];
12118#endif
12119
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012120 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012121
12122#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12123 lnum = get_tv_lnum(argvars); /* -1 on type error */
12124 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12125
12126 vim_memset(str, NUL, sizeof(str));
12127
12128 if (rettv_list_alloc(rettv) != FAIL)
12129 {
12130 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12131 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12132 && curwin->w_p_cole > 0)
12133 {
12134 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
12135 syntax_flags = get_syntax_info(&matchid);
12136
12137 /* get the conceal character */
12138 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
12139 {
12140 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020012141 if (cchar == NUL && curwin->w_p_cole == 1)
12142 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012143 if (cchar != NUL)
12144 {
12145# ifdef FEAT_MBYTE
12146 if (has_mbyte)
12147 (*mb_char2bytes)(cchar, str);
12148 else
12149# endif
12150 str[0] = cchar;
12151 }
12152 }
12153 }
12154
12155 list_append_number(rettv->vval.v_list,
12156 (syntax_flags & HL_CONCEAL) != 0);
12157 /* -1 to auto-determine strlen */
12158 list_append_string(rettv->vval.v_list, str, -1);
12159 list_append_number(rettv->vval.v_list, matchid);
12160 }
12161#endif
12162}
12163
12164/*
12165 * "synstack(lnum, col)" function
12166 */
12167 static void
12168f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
12169{
12170#ifdef FEAT_SYN_HL
12171 linenr_T lnum;
12172 colnr_T col;
12173 int i;
12174 int id;
12175#endif
12176
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012177 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012178
12179#ifdef FEAT_SYN_HL
12180 lnum = get_tv_lnum(argvars); /* -1 on type error */
12181 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12182
12183 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12184 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12185 && rettv_list_alloc(rettv) != FAIL)
12186 {
12187 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
12188 for (i = 0; ; ++i)
12189 {
12190 id = syn_get_stack_item(i);
12191 if (id < 0)
12192 break;
12193 if (list_append_number(rettv->vval.v_list, id) == FAIL)
12194 break;
12195 }
12196 }
12197#endif
12198}
12199
12200 static void
12201get_cmd_output_as_rettv(
12202 typval_T *argvars,
12203 typval_T *rettv,
12204 int retlist)
12205{
12206 char_u *res = NULL;
12207 char_u *p;
12208 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012209 int err = FALSE;
12210 FILE *fd;
12211 list_T *list = NULL;
12212 int flags = SHELL_SILENT;
12213
12214 rettv->v_type = VAR_STRING;
12215 rettv->vval.v_string = NULL;
12216 if (check_restricted() || check_secure())
12217 goto errret;
12218
12219 if (argvars[1].v_type != VAR_UNKNOWN)
12220 {
12221 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010012222 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012223 * command.
12224 */
12225 if ((infile = vim_tempname('i', TRUE)) == NULL)
12226 {
12227 EMSG(_(e_notmp));
12228 goto errret;
12229 }
12230
12231 fd = mch_fopen((char *)infile, WRITEBIN);
12232 if (fd == NULL)
12233 {
12234 EMSG2(_(e_notopen), infile);
12235 goto errret;
12236 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010012237 if (argvars[1].v_type == VAR_NUMBER)
12238 {
12239 linenr_T lnum;
12240 buf_T *buf;
12241
12242 buf = buflist_findnr(argvars[1].vval.v_number);
12243 if (buf == NULL)
12244 {
12245 EMSGN(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010012246 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010012247 goto errret;
12248 }
12249
12250 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
12251 {
12252 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
12253 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
12254 {
12255 err = TRUE;
12256 break;
12257 }
12258 if (putc(NL, fd) == EOF)
12259 {
12260 err = TRUE;
12261 break;
12262 }
12263 }
12264 }
12265 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012266 {
12267 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
12268 err = TRUE;
12269 }
12270 else
12271 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010012272 size_t len;
12273 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012274
12275 p = get_tv_string_buf_chk(&argvars[1], buf);
12276 if (p == NULL)
12277 {
12278 fclose(fd);
12279 goto errret; /* type error; errmsg already given */
12280 }
12281 len = STRLEN(p);
12282 if (len > 0 && fwrite(p, len, 1, fd) != 1)
12283 err = TRUE;
12284 }
12285 if (fclose(fd) != 0)
12286 err = TRUE;
12287 if (err)
12288 {
12289 EMSG(_("E677: Error writing temp file"));
12290 goto errret;
12291 }
12292 }
12293
12294 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
12295 * echoes typeahead, that messes up the display. */
12296 if (!msg_silent)
12297 flags += SHELL_COOKED;
12298
12299 if (retlist)
12300 {
12301 int len;
12302 listitem_T *li;
12303 char_u *s = NULL;
12304 char_u *start;
12305 char_u *end;
12306 int i;
12307
12308 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
12309 if (res == NULL)
12310 goto errret;
12311
12312 list = list_alloc();
12313 if (list == NULL)
12314 goto errret;
12315
12316 for (i = 0; i < len; ++i)
12317 {
12318 start = res + i;
12319 while (i < len && res[i] != NL)
12320 ++i;
12321 end = res + i;
12322
12323 s = alloc((unsigned)(end - start + 1));
12324 if (s == NULL)
12325 goto errret;
12326
12327 for (p = s; start < end; ++p, ++start)
12328 *p = *start == NUL ? NL : *start;
12329 *p = NUL;
12330
12331 li = listitem_alloc();
12332 if (li == NULL)
12333 {
12334 vim_free(s);
12335 goto errret;
12336 }
12337 li->li_tv.v_type = VAR_STRING;
12338 li->li_tv.v_lock = 0;
12339 li->li_tv.vval.v_string = s;
12340 list_append(list, li);
12341 }
12342
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012343 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012344 list = NULL;
12345 }
12346 else
12347 {
12348 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
12349#ifdef USE_CR
12350 /* translate <CR> into <NL> */
12351 if (res != NULL)
12352 {
12353 char_u *s;
12354
12355 for (s = res; *s; ++s)
12356 {
12357 if (*s == CAR)
12358 *s = NL;
12359 }
12360 }
12361#else
12362# ifdef USE_CRNL
12363 /* translate <CR><NL> into <NL> */
12364 if (res != NULL)
12365 {
12366 char_u *s, *d;
12367
12368 d = res;
12369 for (s = res; *s; ++s)
12370 {
12371 if (s[0] == CAR && s[1] == NL)
12372 ++s;
12373 *d++ = *s;
12374 }
12375 *d = NUL;
12376 }
12377# endif
12378#endif
12379 rettv->vval.v_string = res;
12380 res = NULL;
12381 }
12382
12383errret:
12384 if (infile != NULL)
12385 {
12386 mch_remove(infile);
12387 vim_free(infile);
12388 }
12389 if (res != NULL)
12390 vim_free(res);
12391 if (list != NULL)
12392 list_free(list);
12393}
12394
12395/*
12396 * "system()" function
12397 */
12398 static void
12399f_system(typval_T *argvars, typval_T *rettv)
12400{
12401 get_cmd_output_as_rettv(argvars, rettv, FALSE);
12402}
12403
12404/*
12405 * "systemlist()" function
12406 */
12407 static void
12408f_systemlist(typval_T *argvars, typval_T *rettv)
12409{
12410 get_cmd_output_as_rettv(argvars, rettv, TRUE);
12411}
12412
12413/*
12414 * "tabpagebuflist()" function
12415 */
12416 static void
12417f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12418{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012419 tabpage_T *tp;
12420 win_T *wp = NULL;
12421
12422 if (argvars[0].v_type == VAR_UNKNOWN)
12423 wp = firstwin;
12424 else
12425 {
12426 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12427 if (tp != NULL)
12428 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12429 }
12430 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
12431 {
12432 for (; wp != NULL; wp = wp->w_next)
12433 if (list_append_number(rettv->vval.v_list,
12434 wp->w_buffer->b_fnum) == FAIL)
12435 break;
12436 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012437}
12438
12439
12440/*
12441 * "tabpagenr()" function
12442 */
12443 static void
12444f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
12445{
12446 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012447 char_u *arg;
12448
12449 if (argvars[0].v_type != VAR_UNKNOWN)
12450 {
12451 arg = get_tv_string_chk(&argvars[0]);
12452 nr = 0;
12453 if (arg != NULL)
12454 {
12455 if (STRCMP(arg, "$") == 0)
12456 nr = tabpage_index(NULL) - 1;
12457 else
12458 EMSG2(_(e_invexpr2), arg);
12459 }
12460 }
12461 else
12462 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012463 rettv->vval.v_number = nr;
12464}
12465
12466
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012467static int get_winnr(tabpage_T *tp, typval_T *argvar);
12468
12469/*
12470 * Common code for tabpagewinnr() and winnr().
12471 */
12472 static int
12473get_winnr(tabpage_T *tp, typval_T *argvar)
12474{
12475 win_T *twin;
12476 int nr = 1;
12477 win_T *wp;
12478 char_u *arg;
12479
12480 twin = (tp == curtab) ? curwin : tp->tp_curwin;
12481 if (argvar->v_type != VAR_UNKNOWN)
12482 {
12483 arg = get_tv_string_chk(argvar);
12484 if (arg == NULL)
12485 nr = 0; /* type error; errmsg already given */
12486 else if (STRCMP(arg, "$") == 0)
12487 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
12488 else if (STRCMP(arg, "#") == 0)
12489 {
12490 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
12491 if (twin == NULL)
12492 nr = 0;
12493 }
12494 else
12495 {
12496 EMSG2(_(e_invexpr2), arg);
12497 nr = 0;
12498 }
12499 }
12500
12501 if (nr > 0)
12502 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12503 wp != twin; wp = wp->w_next)
12504 {
12505 if (wp == NULL)
12506 {
12507 /* didn't find it in this tabpage */
12508 nr = 0;
12509 break;
12510 }
12511 ++nr;
12512 }
12513 return nr;
12514}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012515
12516/*
12517 * "tabpagewinnr()" function
12518 */
12519 static void
12520f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
12521{
12522 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012523 tabpage_T *tp;
12524
12525 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12526 if (tp == NULL)
12527 nr = 0;
12528 else
12529 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012530 rettv->vval.v_number = nr;
12531}
12532
12533
12534/*
12535 * "tagfiles()" function
12536 */
12537 static void
12538f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
12539{
12540 char_u *fname;
12541 tagname_T tn;
12542 int first;
12543
12544 if (rettv_list_alloc(rettv) == FAIL)
12545 return;
12546 fname = alloc(MAXPATHL);
12547 if (fname == NULL)
12548 return;
12549
12550 for (first = TRUE; ; first = FALSE)
12551 if (get_tagfname(&tn, first, fname) == FAIL
12552 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
12553 break;
12554 tagname_free(&tn);
12555 vim_free(fname);
12556}
12557
12558/*
12559 * "taglist()" function
12560 */
12561 static void
12562f_taglist(typval_T *argvars, typval_T *rettv)
12563{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012564 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012565 char_u *tag_pattern;
12566
12567 tag_pattern = get_tv_string(&argvars[0]);
12568
12569 rettv->vval.v_number = FALSE;
12570 if (*tag_pattern == NUL)
12571 return;
12572
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012573 if (argvars[1].v_type != VAR_UNKNOWN)
12574 fname = get_tv_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012575 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012576 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012577}
12578
12579/*
12580 * "tempname()" function
12581 */
12582 static void
12583f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
12584{
12585 static int x = 'A';
12586
12587 rettv->v_type = VAR_STRING;
12588 rettv->vval.v_string = vim_tempname(x, FALSE);
12589
12590 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
12591 * names. Skip 'I' and 'O', they are used for shell redirection. */
12592 do
12593 {
12594 if (x == 'Z')
12595 x = '0';
12596 else if (x == '9')
12597 x = 'A';
12598 else
12599 {
12600#ifdef EBCDIC
12601 if (x == 'I')
12602 x = 'J';
12603 else if (x == 'R')
12604 x = 'S';
12605 else
12606#endif
12607 ++x;
12608 }
12609 } while (x == 'I' || x == 'O');
12610}
12611
12612#ifdef FEAT_FLOAT
12613/*
12614 * "tan()" function
12615 */
12616 static void
12617f_tan(typval_T *argvars, typval_T *rettv)
12618{
12619 float_T f = 0.0;
12620
12621 rettv->v_type = VAR_FLOAT;
12622 if (get_float_arg(argvars, &f) == OK)
12623 rettv->vval.v_float = tan(f);
12624 else
12625 rettv->vval.v_float = 0.0;
12626}
12627
12628/*
12629 * "tanh()" function
12630 */
12631 static void
12632f_tanh(typval_T *argvars, typval_T *rettv)
12633{
12634 float_T f = 0.0;
12635
12636 rettv->v_type = VAR_FLOAT;
12637 if (get_float_arg(argvars, &f) == OK)
12638 rettv->vval.v_float = tanh(f);
12639 else
12640 rettv->vval.v_float = 0.0;
12641}
12642#endif
12643
12644/*
12645 * "test_alloc_fail(id, countdown, repeat)" function
12646 */
12647 static void
12648f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
12649{
12650 if (argvars[0].v_type != VAR_NUMBER
12651 || argvars[0].vval.v_number <= 0
12652 || argvars[1].v_type != VAR_NUMBER
12653 || argvars[1].vval.v_number < 0
12654 || argvars[2].v_type != VAR_NUMBER)
12655 EMSG(_(e_invarg));
12656 else
12657 {
12658 alloc_fail_id = argvars[0].vval.v_number;
12659 if (alloc_fail_id >= aid_last)
12660 EMSG(_(e_invarg));
12661 alloc_fail_countdown = argvars[1].vval.v_number;
12662 alloc_fail_repeat = argvars[2].vval.v_number;
12663 did_outofmem_msg = FALSE;
12664 }
12665}
12666
12667/*
12668 * "test_autochdir()"
12669 */
12670 static void
12671f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12672{
12673#if defined(FEAT_AUTOCHDIR)
12674 test_autochdir = TRUE;
12675#endif
12676}
12677
12678/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020012679 * "test_feedinput()"
12680 */
12681 static void
12682f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
12683{
12684#ifdef USE_INPUT_BUF
12685 char_u *val = get_tv_string_chk(&argvars[0]);
12686
12687 if (val != NULL)
12688 {
12689 trash_input_buf();
12690 add_to_input_buf_csi(val, (int)STRLEN(val));
12691 }
12692#endif
12693}
12694
12695/*
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012696 * "test_disable({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012697 */
12698 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012699f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012700{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012701 char_u *name = (char_u *)"";
12702 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012703 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012704
12705 if (argvars[0].v_type != VAR_STRING
12706 || (argvars[1].v_type) != VAR_NUMBER)
12707 EMSG(_(e_invarg));
12708 else
12709 {
12710 name = get_tv_string_chk(&argvars[0]);
12711 val = (int)get_tv_number(&argvars[1]);
12712
12713 if (STRCMP(name, (char_u *)"redraw") == 0)
12714 disable_redraw_for_testing = val;
12715 else if (STRCMP(name, (char_u *)"char_avail") == 0)
12716 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012717 else if (STRCMP(name, (char_u *)"starting") == 0)
12718 {
12719 if (val)
12720 {
12721 if (save_starting < 0)
12722 save_starting = starting;
12723 starting = 0;
12724 }
12725 else
12726 {
12727 starting = save_starting;
12728 save_starting = -1;
12729 }
12730 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012731 else if (STRCMP(name, (char_u *)"ALL") == 0)
12732 {
12733 disable_char_avail_for_testing = FALSE;
12734 disable_redraw_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012735 if (save_starting >= 0)
12736 {
12737 starting = save_starting;
12738 save_starting = -1;
12739 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012740 }
12741 else
12742 EMSG2(_(e_invarg2), name);
12743 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012744}
12745
12746/*
12747 * "test_garbagecollect_now()" function
12748 */
12749 static void
12750f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12751{
12752 /* This is dangerous, any Lists and Dicts used internally may be freed
12753 * while still in use. */
12754 garbage_collect(TRUE);
12755}
12756
Bram Moolenaare0c31f62017-03-01 15:07:05 +010012757/*
12758 * "test_ignore_error()" function
12759 */
12760 static void
12761f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
12762{
12763 ignore_error_for_testing(get_tv_string(&argvars[0]));
12764}
12765
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012766#ifdef FEAT_JOB_CHANNEL
12767 static void
12768f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
12769{
12770 rettv->v_type = VAR_CHANNEL;
12771 rettv->vval.v_channel = NULL;
12772}
12773#endif
12774
12775 static void
12776f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
12777{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012778 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012779}
12780
12781#ifdef FEAT_JOB_CHANNEL
12782 static void
12783f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
12784{
12785 rettv->v_type = VAR_JOB;
12786 rettv->vval.v_job = NULL;
12787}
12788#endif
12789
12790 static void
12791f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
12792{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012793 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012794}
12795
12796 static void
12797f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
12798{
12799 rettv->v_type = VAR_PARTIAL;
12800 rettv->vval.v_partial = NULL;
12801}
12802
12803 static void
12804f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
12805{
12806 rettv->v_type = VAR_STRING;
12807 rettv->vval.v_string = NULL;
12808}
12809
12810 static void
12811f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
12812{
12813 time_for_testing = (time_t)get_tv_number(&argvars[0]);
12814}
12815
12816#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
12817/*
12818 * Get a callback from "arg". It can be a Funcref or a function name.
12819 * When "arg" is zero return an empty string.
12820 * Return NULL for an invalid argument.
12821 */
12822 char_u *
12823get_callback(typval_T *arg, partial_T **pp)
12824{
12825 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
12826 {
12827 *pp = arg->vval.v_partial;
12828 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012829 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012830 }
12831 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012832 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012833 {
12834 func_ref(arg->vval.v_string);
12835 return arg->vval.v_string;
12836 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012837 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
12838 return (char_u *)"";
12839 EMSG(_("E921: Invalid callback argument"));
12840 return NULL;
12841}
12842
12843/*
12844 * Unref/free "callback" and "partial" retured by get_callback().
12845 */
12846 void
12847free_callback(char_u *callback, partial_T *partial)
12848{
12849 if (partial != NULL)
12850 partial_unref(partial);
12851 else if (callback != NULL)
12852 {
12853 func_unref(callback);
12854 vim_free(callback);
12855 }
12856}
12857#endif
12858
12859#ifdef FEAT_TIMERS
12860/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020012861 * "timer_info([timer])" function
12862 */
12863 static void
12864f_timer_info(typval_T *argvars, typval_T *rettv)
12865{
12866 timer_T *timer = NULL;
12867
12868 if (rettv_list_alloc(rettv) != OK)
12869 return;
12870 if (argvars[0].v_type != VAR_UNKNOWN)
12871 {
12872 if (argvars[0].v_type != VAR_NUMBER)
12873 EMSG(_(e_number_exp));
12874 else
12875 {
12876 timer = find_timer((int)get_tv_number(&argvars[0]));
12877 if (timer != NULL)
12878 add_timer_info(rettv, timer);
12879 }
12880 }
12881 else
12882 add_timer_info_all(rettv);
12883}
12884
12885/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012886 * "timer_pause(timer, paused)" function
12887 */
12888 static void
12889f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
12890{
12891 timer_T *timer = NULL;
12892 int paused = (int)get_tv_number(&argvars[1]);
12893
12894 if (argvars[0].v_type != VAR_NUMBER)
12895 EMSG(_(e_number_exp));
12896 else
12897 {
12898 timer = find_timer((int)get_tv_number(&argvars[0]));
12899 if (timer != NULL)
12900 timer->tr_paused = paused;
12901 }
12902}
12903
12904/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012905 * "timer_start(time, callback [, options])" function
12906 */
12907 static void
12908f_timer_start(typval_T *argvars, typval_T *rettv)
12909{
Bram Moolenaar75537a92016-09-05 22:45:28 +020012910 long msec = (long)get_tv_number(&argvars[0]);
12911 timer_T *timer;
12912 int repeat = 0;
12913 char_u *callback;
12914 dict_T *dict;
12915 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012916
Bram Moolenaar75537a92016-09-05 22:45:28 +020012917 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012918 if (check_secure())
12919 return;
12920 if (argvars[2].v_type != VAR_UNKNOWN)
12921 {
12922 if (argvars[2].v_type != VAR_DICT
12923 || (dict = argvars[2].vval.v_dict) == NULL)
12924 {
12925 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
12926 return;
12927 }
12928 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
12929 repeat = get_dict_number(dict, (char_u *)"repeat");
12930 }
12931
Bram Moolenaar75537a92016-09-05 22:45:28 +020012932 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012933 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020012934 return;
12935
12936 timer = create_timer(msec, repeat);
12937 if (timer == NULL)
12938 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012939 else
12940 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020012941 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020012942 timer->tr_callback = vim_strsave(callback);
12943 else
12944 /* pointer into the partial */
12945 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020012946 timer->tr_partial = partial;
12947 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012948 }
12949}
12950
12951/*
12952 * "timer_stop(timer)" function
12953 */
12954 static void
12955f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
12956{
12957 timer_T *timer;
12958
12959 if (argvars[0].v_type != VAR_NUMBER)
12960 {
12961 EMSG(_(e_number_exp));
12962 return;
12963 }
12964 timer = find_timer((int)get_tv_number(&argvars[0]));
12965 if (timer != NULL)
12966 stop_timer(timer);
12967}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012968
12969/*
12970 * "timer_stopall()" function
12971 */
12972 static void
12973f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12974{
12975 stop_all_timers();
12976}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012977#endif
12978
12979/*
12980 * "tolower(string)" function
12981 */
12982 static void
12983f_tolower(typval_T *argvars, typval_T *rettv)
12984{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012985 rettv->v_type = VAR_STRING;
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +010012986 rettv->vval.v_string = strlow_save(get_tv_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012987}
12988
12989/*
12990 * "toupper(string)" function
12991 */
12992 static void
12993f_toupper(typval_T *argvars, typval_T *rettv)
12994{
12995 rettv->v_type = VAR_STRING;
12996 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
12997}
12998
12999/*
13000 * "tr(string, fromstr, tostr)" function
13001 */
13002 static void
13003f_tr(typval_T *argvars, typval_T *rettv)
13004{
13005 char_u *in_str;
13006 char_u *fromstr;
13007 char_u *tostr;
13008 char_u *p;
13009#ifdef FEAT_MBYTE
13010 int inlen;
13011 int fromlen;
13012 int tolen;
13013 int idx;
13014 char_u *cpstr;
13015 int cplen;
13016 int first = TRUE;
13017#endif
13018 char_u buf[NUMBUFLEN];
13019 char_u buf2[NUMBUFLEN];
13020 garray_T ga;
13021
13022 in_str = get_tv_string(&argvars[0]);
13023 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
13024 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
13025
13026 /* Default return value: empty string. */
13027 rettv->v_type = VAR_STRING;
13028 rettv->vval.v_string = NULL;
13029 if (fromstr == NULL || tostr == NULL)
13030 return; /* type error; errmsg already given */
13031 ga_init2(&ga, (int)sizeof(char), 80);
13032
13033#ifdef FEAT_MBYTE
13034 if (!has_mbyte)
13035#endif
13036 /* not multi-byte: fromstr and tostr must be the same length */
13037 if (STRLEN(fromstr) != STRLEN(tostr))
13038 {
13039#ifdef FEAT_MBYTE
13040error:
13041#endif
13042 EMSG2(_(e_invarg2), fromstr);
13043 ga_clear(&ga);
13044 return;
13045 }
13046
13047 /* fromstr and tostr have to contain the same number of chars */
13048 while (*in_str != NUL)
13049 {
13050#ifdef FEAT_MBYTE
13051 if (has_mbyte)
13052 {
13053 inlen = (*mb_ptr2len)(in_str);
13054 cpstr = in_str;
13055 cplen = inlen;
13056 idx = 0;
13057 for (p = fromstr; *p != NUL; p += fromlen)
13058 {
13059 fromlen = (*mb_ptr2len)(p);
13060 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
13061 {
13062 for (p = tostr; *p != NUL; p += tolen)
13063 {
13064 tolen = (*mb_ptr2len)(p);
13065 if (idx-- == 0)
13066 {
13067 cplen = tolen;
13068 cpstr = p;
13069 break;
13070 }
13071 }
13072 if (*p == NUL) /* tostr is shorter than fromstr */
13073 goto error;
13074 break;
13075 }
13076 ++idx;
13077 }
13078
13079 if (first && cpstr == in_str)
13080 {
13081 /* Check that fromstr and tostr have the same number of
13082 * (multi-byte) characters. Done only once when a character
13083 * of in_str doesn't appear in fromstr. */
13084 first = FALSE;
13085 for (p = tostr; *p != NUL; p += tolen)
13086 {
13087 tolen = (*mb_ptr2len)(p);
13088 --idx;
13089 }
13090 if (idx != 0)
13091 goto error;
13092 }
13093
13094 (void)ga_grow(&ga, cplen);
13095 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
13096 ga.ga_len += cplen;
13097
13098 in_str += inlen;
13099 }
13100 else
13101#endif
13102 {
13103 /* When not using multi-byte chars we can do it faster. */
13104 p = vim_strchr(fromstr, *in_str);
13105 if (p != NULL)
13106 ga_append(&ga, tostr[p - fromstr]);
13107 else
13108 ga_append(&ga, *in_str);
13109 ++in_str;
13110 }
13111 }
13112
13113 /* add a terminating NUL */
13114 (void)ga_grow(&ga, 1);
13115 ga_append(&ga, NUL);
13116
13117 rettv->vval.v_string = ga.ga_data;
13118}
13119
13120#ifdef FEAT_FLOAT
13121/*
13122 * "trunc({float})" function
13123 */
13124 static void
13125f_trunc(typval_T *argvars, typval_T *rettv)
13126{
13127 float_T f = 0.0;
13128
13129 rettv->v_type = VAR_FLOAT;
13130 if (get_float_arg(argvars, &f) == OK)
13131 /* trunc() is not in C90, use floor() or ceil() instead. */
13132 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
13133 else
13134 rettv->vval.v_float = 0.0;
13135}
13136#endif
13137
13138/*
13139 * "type(expr)" function
13140 */
13141 static void
13142f_type(typval_T *argvars, typval_T *rettv)
13143{
13144 int n = -1;
13145
13146 switch (argvars[0].v_type)
13147 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020013148 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
13149 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013150 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020013151 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
13152 case VAR_LIST: n = VAR_TYPE_LIST; break;
13153 case VAR_DICT: n = VAR_TYPE_DICT; break;
13154 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013155 case VAR_SPECIAL:
13156 if (argvars[0].vval.v_number == VVAL_FALSE
13157 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020013158 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013159 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020013160 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013161 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020013162 case VAR_JOB: n = VAR_TYPE_JOB; break;
13163 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013164 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010013165 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013166 n = -1;
13167 break;
13168 }
13169 rettv->vval.v_number = n;
13170}
13171
13172/*
13173 * "undofile(name)" function
13174 */
13175 static void
13176f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
13177{
13178 rettv->v_type = VAR_STRING;
13179#ifdef FEAT_PERSISTENT_UNDO
13180 {
13181 char_u *fname = get_tv_string(&argvars[0]);
13182
13183 if (*fname == NUL)
13184 {
13185 /* If there is no file name there will be no undo file. */
13186 rettv->vval.v_string = NULL;
13187 }
13188 else
13189 {
13190 char_u *ffname = FullName_save(fname, FALSE);
13191
13192 if (ffname != NULL)
13193 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
13194 vim_free(ffname);
13195 }
13196 }
13197#else
13198 rettv->vval.v_string = NULL;
13199#endif
13200}
13201
13202/*
13203 * "undotree()" function
13204 */
13205 static void
13206f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
13207{
13208 if (rettv_dict_alloc(rettv) == OK)
13209 {
13210 dict_T *dict = rettv->vval.v_dict;
13211 list_T *list;
13212
13213 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
13214 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
13215 dict_add_nr_str(dict, "save_last",
13216 (long)curbuf->b_u_save_nr_last, NULL);
13217 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
13218 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
13219 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
13220
13221 list = list_alloc();
13222 if (list != NULL)
13223 {
13224 u_eval_tree(curbuf->b_u_oldhead, list);
13225 dict_add_list(dict, "entries", list);
13226 }
13227 }
13228}
13229
13230/*
13231 * "values(dict)" function
13232 */
13233 static void
13234f_values(typval_T *argvars, typval_T *rettv)
13235{
13236 dict_list(argvars, rettv, 1);
13237}
13238
13239/*
13240 * "virtcol(string)" function
13241 */
13242 static void
13243f_virtcol(typval_T *argvars, typval_T *rettv)
13244{
13245 colnr_T vcol = 0;
13246 pos_T *fp;
13247 int fnum = curbuf->b_fnum;
13248
13249 fp = var2fpos(&argvars[0], FALSE, &fnum);
13250 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
13251 && fnum == curbuf->b_fnum)
13252 {
13253 getvvcol(curwin, fp, NULL, NULL, &vcol);
13254 ++vcol;
13255 }
13256
13257 rettv->vval.v_number = vcol;
13258}
13259
13260/*
13261 * "visualmode()" function
13262 */
13263 static void
13264f_visualmode(typval_T *argvars, typval_T *rettv)
13265{
13266 char_u str[2];
13267
13268 rettv->v_type = VAR_STRING;
13269 str[0] = curbuf->b_visual_mode_eval;
13270 str[1] = NUL;
13271 rettv->vval.v_string = vim_strsave(str);
13272
13273 /* A non-zero number or non-empty string argument: reset mode. */
13274 if (non_zero_arg(&argvars[0]))
13275 curbuf->b_visual_mode_eval = NUL;
13276}
13277
13278/*
13279 * "wildmenumode()" function
13280 */
13281 static void
13282f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13283{
13284#ifdef FEAT_WILDMENU
13285 if (wild_menu_showing)
13286 rettv->vval.v_number = 1;
13287#endif
13288}
13289
13290/*
13291 * "winbufnr(nr)" function
13292 */
13293 static void
13294f_winbufnr(typval_T *argvars, typval_T *rettv)
13295{
13296 win_T *wp;
13297
13298 wp = find_win_by_nr(&argvars[0], NULL);
13299 if (wp == NULL)
13300 rettv->vval.v_number = -1;
13301 else
13302 rettv->vval.v_number = wp->w_buffer->b_fnum;
13303}
13304
13305/*
13306 * "wincol()" function
13307 */
13308 static void
13309f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
13310{
13311 validate_cursor();
13312 rettv->vval.v_number = curwin->w_wcol + 1;
13313}
13314
13315/*
13316 * "winheight(nr)" function
13317 */
13318 static void
13319f_winheight(typval_T *argvars, typval_T *rettv)
13320{
13321 win_T *wp;
13322
13323 wp = find_win_by_nr(&argvars[0], NULL);
13324 if (wp == NULL)
13325 rettv->vval.v_number = -1;
13326 else
13327 rettv->vval.v_number = wp->w_height;
13328}
13329
13330/*
13331 * "winline()" function
13332 */
13333 static void
13334f_winline(typval_T *argvars UNUSED, typval_T *rettv)
13335{
13336 validate_cursor();
13337 rettv->vval.v_number = curwin->w_wrow + 1;
13338}
13339
13340/*
13341 * "winnr()" function
13342 */
13343 static void
13344f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
13345{
13346 int nr = 1;
13347
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013348 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013349 rettv->vval.v_number = nr;
13350}
13351
13352/*
13353 * "winrestcmd()" function
13354 */
13355 static void
13356f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
13357{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013358 win_T *wp;
13359 int winnr = 1;
13360 garray_T ga;
13361 char_u buf[50];
13362
13363 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020013364 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013365 {
13366 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
13367 ga_concat(&ga, buf);
13368 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
13369 ga_concat(&ga, buf);
13370 ++winnr;
13371 }
13372 ga_append(&ga, NUL);
13373
13374 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013375 rettv->v_type = VAR_STRING;
13376}
13377
13378/*
13379 * "winrestview()" function
13380 */
13381 static void
13382f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
13383{
13384 dict_T *dict;
13385
13386 if (argvars[0].v_type != VAR_DICT
13387 || (dict = argvars[0].vval.v_dict) == NULL)
13388 EMSG(_(e_invarg));
13389 else
13390 {
13391 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
13392 curwin->w_cursor.lnum = (linenr_T)get_dict_number(dict, (char_u *)"lnum");
13393 if (dict_find(dict, (char_u *)"col", -1) != NULL)
13394 curwin->w_cursor.col = (colnr_T)get_dict_number(dict, (char_u *)"col");
13395#ifdef FEAT_VIRTUALEDIT
13396 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
13397 curwin->w_cursor.coladd = (colnr_T)get_dict_number(dict, (char_u *)"coladd");
13398#endif
13399 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
13400 {
13401 curwin->w_curswant = (colnr_T)get_dict_number(dict, (char_u *)"curswant");
13402 curwin->w_set_curswant = FALSE;
13403 }
13404
13405 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
13406 set_topline(curwin, (linenr_T)get_dict_number(dict, (char_u *)"topline"));
13407#ifdef FEAT_DIFF
13408 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
13409 curwin->w_topfill = (int)get_dict_number(dict, (char_u *)"topfill");
13410#endif
13411 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
13412 curwin->w_leftcol = (colnr_T)get_dict_number(dict, (char_u *)"leftcol");
13413 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
13414 curwin->w_skipcol = (colnr_T)get_dict_number(dict, (char_u *)"skipcol");
13415
13416 check_cursor();
13417 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020013418 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013419 changed_window_setting();
13420
13421 if (curwin->w_topline <= 0)
13422 curwin->w_topline = 1;
13423 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
13424 curwin->w_topline = curbuf->b_ml.ml_line_count;
13425#ifdef FEAT_DIFF
13426 check_topfill(curwin, TRUE);
13427#endif
13428 }
13429}
13430
13431/*
13432 * "winsaveview()" function
13433 */
13434 static void
13435f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
13436{
13437 dict_T *dict;
13438
13439 if (rettv_dict_alloc(rettv) == FAIL)
13440 return;
13441 dict = rettv->vval.v_dict;
13442
13443 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
13444 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
13445#ifdef FEAT_VIRTUALEDIT
13446 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
13447#endif
13448 update_curswant();
13449 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
13450
13451 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
13452#ifdef FEAT_DIFF
13453 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
13454#endif
13455 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
13456 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
13457}
13458
13459/*
13460 * "winwidth(nr)" function
13461 */
13462 static void
13463f_winwidth(typval_T *argvars, typval_T *rettv)
13464{
13465 win_T *wp;
13466
13467 wp = find_win_by_nr(&argvars[0], NULL);
13468 if (wp == NULL)
13469 rettv->vval.v_number = -1;
13470 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013471 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013472}
13473
13474/*
13475 * "wordcount()" function
13476 */
13477 static void
13478f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
13479{
13480 if (rettv_dict_alloc(rettv) == FAIL)
13481 return;
13482 cursor_pos_info(rettv->vval.v_dict);
13483}
13484
13485/*
13486 * "writefile()" function
13487 */
13488 static void
13489f_writefile(typval_T *argvars, typval_T *rettv)
13490{
13491 int binary = FALSE;
13492 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013493#ifdef HAVE_FSYNC
13494 int do_fsync = p_fs;
13495#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013496 char_u *fname;
13497 FILE *fd;
13498 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013499 listitem_T *li;
13500 list_T *list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013501
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013502 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013503 if (check_restricted() || check_secure())
13504 return;
13505
13506 if (argvars[0].v_type != VAR_LIST)
13507 {
13508 EMSG2(_(e_listarg), "writefile()");
13509 return;
13510 }
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013511 list = argvars[0].vval.v_list;
13512 if (list == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013513 return;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013514 for (li = list->lv_first; li != NULL; li = li->li_next)
13515 if (get_tv_string_chk(&li->li_tv) == NULL)
13516 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013517
13518 if (argvars[2].v_type != VAR_UNKNOWN)
13519 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013520 char_u *arg2 = get_tv_string_chk(&argvars[2]);
13521
13522 if (arg2 == NULL)
13523 return;
13524 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013525 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013526 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013527 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013528#ifdef HAVE_FSYNC
13529 if (vim_strchr(arg2, 's') != NULL)
13530 do_fsync = TRUE;
13531 else if (vim_strchr(arg2, 'S') != NULL)
13532 do_fsync = FALSE;
13533#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013534 }
13535
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013536 fname = get_tv_string_chk(&argvars[1]);
13537 if (fname == NULL)
13538 return;
13539
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013540 /* Always open the file in binary mode, library functions have a mind of
13541 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013542 if (*fname == NUL || (fd = mch_fopen((char *)fname,
13543 append ? APPENDBIN : WRITEBIN)) == NULL)
13544 {
13545 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
13546 ret = -1;
13547 }
13548 else
13549 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013550 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013551 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013552#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010013553 else if (do_fsync)
13554 /* Ignore the error, the user wouldn't know what to do about it.
13555 * May happen for a device. */
13556 ignored = fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013557#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013558 fclose(fd);
13559 }
13560
13561 rettv->vval.v_number = ret;
13562}
13563
13564/*
13565 * "xor(expr, expr)" function
13566 */
13567 static void
13568f_xor(typval_T *argvars, typval_T *rettv)
13569{
13570 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
13571 ^ get_tv_number_chk(&argvars[1], NULL);
13572}
13573
13574
13575#endif /* FEAT_EVAL */