blob: 23cdeb49ee930a7937faba340e6653c624f81a8e [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);
Bram Moolenaarca851592018-06-06 21:04:07 +020043static void f_appendbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020044static void f_argc(typval_T *argvars, typval_T *rettv);
45static void f_argidx(typval_T *argvars, typval_T *rettv);
46static void f_arglistid(typval_T *argvars, typval_T *rettv);
47static void f_argv(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +010048static void f_assert_beeps(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020049static void f_assert_equal(typval_T *argvars, typval_T *rettv);
Bram Moolenaard96ff162018-02-18 22:13:29 +010050static void f_assert_equalfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020051static void f_assert_exception(typval_T *argvars, typval_T *rettv);
52static void f_assert_fails(typval_T *argvars, typval_T *rettv);
53static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaar61c04492016-07-23 15:35:35 +020054static void f_assert_inrange(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020055static void f_assert_match(typval_T *argvars, typval_T *rettv);
56static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
57static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar42205552017-03-18 19:42:22 +010058static void f_assert_report(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020059static void f_assert_true(typval_T *argvars, typval_T *rettv);
60#ifdef FEAT_FLOAT
61static void f_asin(typval_T *argvars, typval_T *rettv);
62static void f_atan(typval_T *argvars, typval_T *rettv);
63static void f_atan2(typval_T *argvars, typval_T *rettv);
64#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010065#ifdef FEAT_BEVAL
66static void f_balloon_show(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010067# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010068static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010069# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010070#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020071static void f_browse(typval_T *argvars, typval_T *rettv);
72static void f_browsedir(typval_T *argvars, typval_T *rettv);
73static void f_bufexists(typval_T *argvars, typval_T *rettv);
74static void f_buflisted(typval_T *argvars, typval_T *rettv);
75static void f_bufloaded(typval_T *argvars, typval_T *rettv);
76static void f_bufname(typval_T *argvars, typval_T *rettv);
77static void f_bufnr(typval_T *argvars, typval_T *rettv);
78static void f_bufwinid(typval_T *argvars, typval_T *rettv);
79static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
80static void f_byte2line(typval_T *argvars, typval_T *rettv);
81static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
82static void f_byteidx(typval_T *argvars, typval_T *rettv);
83static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
84static void f_call(typval_T *argvars, typval_T *rettv);
85#ifdef FEAT_FLOAT
86static void f_ceil(typval_T *argvars, typval_T *rettv);
87#endif
88#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +010089static void f_ch_canread(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020090static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0874a832016-09-01 15:11:51 +020091static void f_ch_close_in(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020092static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
93static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
94static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
95static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
96static void f_ch_info(typval_T *argvars, typval_T *rettv);
97static void f_ch_log(typval_T *argvars, typval_T *rettv);
98static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
99static void f_ch_open(typval_T *argvars, typval_T *rettv);
100static void f_ch_read(typval_T *argvars, typval_T *rettv);
101static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
102static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
103static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
104static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
105static void f_ch_status(typval_T *argvars, typval_T *rettv);
106#endif
107static void f_changenr(typval_T *argvars, typval_T *rettv);
108static void f_char2nr(typval_T *argvars, typval_T *rettv);
109static void f_cindent(typval_T *argvars, typval_T *rettv);
110static void f_clearmatches(typval_T *argvars, typval_T *rettv);
111static void f_col(typval_T *argvars, typval_T *rettv);
112#if defined(FEAT_INS_EXPAND)
113static void f_complete(typval_T *argvars, typval_T *rettv);
114static void f_complete_add(typval_T *argvars, typval_T *rettv);
115static void f_complete_check(typval_T *argvars, typval_T *rettv);
116#endif
117static void f_confirm(typval_T *argvars, typval_T *rettv);
118static void f_copy(typval_T *argvars, typval_T *rettv);
119#ifdef FEAT_FLOAT
120static void f_cos(typval_T *argvars, typval_T *rettv);
121static void f_cosh(typval_T *argvars, typval_T *rettv);
122#endif
123static void f_count(typval_T *argvars, typval_T *rettv);
124static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
125static void f_cursor(typval_T *argsvars, typval_T *rettv);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200126#ifdef WIN3264
127static void f_debugbreak(typval_T *argvars, typval_T *rettv);
128#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200129static void f_deepcopy(typval_T *argvars, typval_T *rettv);
130static void f_delete(typval_T *argvars, typval_T *rettv);
Bram Moolenaard79a2622018-06-07 18:17:46 +0200131static void f_deletebufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200132static void f_did_filetype(typval_T *argvars, typval_T *rettv);
133static void f_diff_filler(typval_T *argvars, typval_T *rettv);
134static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
135static void f_empty(typval_T *argvars, typval_T *rettv);
136static void f_escape(typval_T *argvars, typval_T *rettv);
137static void f_eval(typval_T *argvars, typval_T *rettv);
138static void f_eventhandler(typval_T *argvars, typval_T *rettv);
139static void f_executable(typval_T *argvars, typval_T *rettv);
140static void f_execute(typval_T *argvars, typval_T *rettv);
141static void f_exepath(typval_T *argvars, typval_T *rettv);
142static void f_exists(typval_T *argvars, typval_T *rettv);
143#ifdef FEAT_FLOAT
144static void f_exp(typval_T *argvars, typval_T *rettv);
145#endif
146static void f_expand(typval_T *argvars, typval_T *rettv);
147static void f_extend(typval_T *argvars, typval_T *rettv);
148static void f_feedkeys(typval_T *argvars, typval_T *rettv);
149static void f_filereadable(typval_T *argvars, typval_T *rettv);
150static void f_filewritable(typval_T *argvars, typval_T *rettv);
151static void f_filter(typval_T *argvars, typval_T *rettv);
152static void f_finddir(typval_T *argvars, typval_T *rettv);
153static void f_findfile(typval_T *argvars, typval_T *rettv);
154#ifdef FEAT_FLOAT
155static void f_float2nr(typval_T *argvars, typval_T *rettv);
156static void f_floor(typval_T *argvars, typval_T *rettv);
157static void f_fmod(typval_T *argvars, typval_T *rettv);
158#endif
159static void f_fnameescape(typval_T *argvars, typval_T *rettv);
160static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
161static void f_foldclosed(typval_T *argvars, typval_T *rettv);
162static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
163static void f_foldlevel(typval_T *argvars, typval_T *rettv);
164static void f_foldtext(typval_T *argvars, typval_T *rettv);
165static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
166static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200167static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200168static void f_function(typval_T *argvars, typval_T *rettv);
169static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
170static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200171static void f_getbufinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200172static void f_getbufline(typval_T *argvars, typval_T *rettv);
173static void f_getbufvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100174static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200175static void f_getchar(typval_T *argvars, typval_T *rettv);
176static void f_getcharmod(typval_T *argvars, typval_T *rettv);
177static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
178static void f_getcmdline(typval_T *argvars, typval_T *rettv);
179#if defined(FEAT_CMDL_COMPL)
180static void f_getcompletion(typval_T *argvars, typval_T *rettv);
181#endif
182static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
183static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
184static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
185static void f_getcwd(typval_T *argvars, typval_T *rettv);
186static void f_getfontname(typval_T *argvars, typval_T *rettv);
187static void f_getfperm(typval_T *argvars, typval_T *rettv);
188static void f_getfsize(typval_T *argvars, typval_T *rettv);
189static void f_getftime(typval_T *argvars, typval_T *rettv);
190static void f_getftype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +0100191static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200192static void f_getline(typval_T *argvars, typval_T *rettv);
Bram Moolenaard823fa92016-08-12 16:29:27 +0200193static void f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200194static void f_getmatches(typval_T *argvars, typval_T *rettv);
195static void f_getpid(typval_T *argvars, typval_T *rettv);
196static void f_getcurpos(typval_T *argvars, typval_T *rettv);
197static void f_getpos(typval_T *argvars, typval_T *rettv);
198static void f_getqflist(typval_T *argvars, typval_T *rettv);
199static void f_getreg(typval_T *argvars, typval_T *rettv);
200static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200201static void f_gettabinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200202static void f_gettabvar(typval_T *argvars, typval_T *rettv);
203static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200204static void f_getwininfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100205static void f_getwinpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200206static void f_getwinposx(typval_T *argvars, typval_T *rettv);
207static void f_getwinposy(typval_T *argvars, typval_T *rettv);
208static void f_getwinvar(typval_T *argvars, typval_T *rettv);
209static void f_glob(typval_T *argvars, typval_T *rettv);
210static void f_globpath(typval_T *argvars, typval_T *rettv);
211static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
212static void f_has(typval_T *argvars, typval_T *rettv);
213static void f_has_key(typval_T *argvars, typval_T *rettv);
214static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
215static void f_hasmapto(typval_T *argvars, typval_T *rettv);
216static void f_histadd(typval_T *argvars, typval_T *rettv);
217static void f_histdel(typval_T *argvars, typval_T *rettv);
218static void f_histget(typval_T *argvars, typval_T *rettv);
219static void f_histnr(typval_T *argvars, typval_T *rettv);
220static void f_hlID(typval_T *argvars, typval_T *rettv);
221static void f_hlexists(typval_T *argvars, typval_T *rettv);
222static void f_hostname(typval_T *argvars, typval_T *rettv);
223static void f_iconv(typval_T *argvars, typval_T *rettv);
224static void f_indent(typval_T *argvars, typval_T *rettv);
225static void f_index(typval_T *argvars, typval_T *rettv);
226static void f_input(typval_T *argvars, typval_T *rettv);
227static void f_inputdialog(typval_T *argvars, typval_T *rettv);
228static void f_inputlist(typval_T *argvars, typval_T *rettv);
229static void f_inputrestore(typval_T *argvars, typval_T *rettv);
230static void f_inputsave(typval_T *argvars, typval_T *rettv);
231static void f_inputsecret(typval_T *argvars, typval_T *rettv);
232static void f_insert(typval_T *argvars, typval_T *rettv);
233static void f_invert(typval_T *argvars, typval_T *rettv);
234static void f_isdirectory(typval_T *argvars, typval_T *rettv);
235static void f_islocked(typval_T *argvars, typval_T *rettv);
236#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
237static void f_isnan(typval_T *argvars, typval_T *rettv);
238#endif
239static void f_items(typval_T *argvars, typval_T *rettv);
240#ifdef FEAT_JOB_CHANNEL
241static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
242static void f_job_info(typval_T *argvars, typval_T *rettv);
243static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
244static void f_job_start(typval_T *argvars, typval_T *rettv);
245static void f_job_stop(typval_T *argvars, typval_T *rettv);
246static void f_job_status(typval_T *argvars, typval_T *rettv);
247#endif
248static void f_join(typval_T *argvars, typval_T *rettv);
249static void f_js_decode(typval_T *argvars, typval_T *rettv);
250static void f_js_encode(typval_T *argvars, typval_T *rettv);
251static void f_json_decode(typval_T *argvars, typval_T *rettv);
252static void f_json_encode(typval_T *argvars, typval_T *rettv);
253static void f_keys(typval_T *argvars, typval_T *rettv);
254static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
255static void f_len(typval_T *argvars, typval_T *rettv);
256static void f_libcall(typval_T *argvars, typval_T *rettv);
257static void f_libcallnr(typval_T *argvars, typval_T *rettv);
258static void f_line(typval_T *argvars, typval_T *rettv);
259static void f_line2byte(typval_T *argvars, typval_T *rettv);
260static void f_lispindent(typval_T *argvars, typval_T *rettv);
261static void f_localtime(typval_T *argvars, typval_T *rettv);
262#ifdef FEAT_FLOAT
263static void f_log(typval_T *argvars, typval_T *rettv);
264static void f_log10(typval_T *argvars, typval_T *rettv);
265#endif
266#ifdef FEAT_LUA
267static void f_luaeval(typval_T *argvars, typval_T *rettv);
268#endif
269static void f_map(typval_T *argvars, typval_T *rettv);
270static void f_maparg(typval_T *argvars, typval_T *rettv);
271static void f_mapcheck(typval_T *argvars, typval_T *rettv);
272static void f_match(typval_T *argvars, typval_T *rettv);
273static void f_matchadd(typval_T *argvars, typval_T *rettv);
274static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
275static void f_matcharg(typval_T *argvars, typval_T *rettv);
276static void f_matchdelete(typval_T *argvars, typval_T *rettv);
277static void f_matchend(typval_T *argvars, typval_T *rettv);
278static void f_matchlist(typval_T *argvars, typval_T *rettv);
279static void f_matchstr(typval_T *argvars, typval_T *rettv);
280static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
281static void f_max(typval_T *argvars, typval_T *rettv);
282static void f_min(typval_T *argvars, typval_T *rettv);
283#ifdef vim_mkdir
284static void f_mkdir(typval_T *argvars, typval_T *rettv);
285#endif
286static void f_mode(typval_T *argvars, typval_T *rettv);
287#ifdef FEAT_MZSCHEME
288static void f_mzeval(typval_T *argvars, typval_T *rettv);
289#endif
290static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
291static void f_nr2char(typval_T *argvars, typval_T *rettv);
292static void f_or(typval_T *argvars, typval_T *rettv);
293static void f_pathshorten(typval_T *argvars, typval_T *rettv);
294#ifdef FEAT_PERL
295static void f_perleval(typval_T *argvars, typval_T *rettv);
296#endif
297#ifdef FEAT_FLOAT
298static void f_pow(typval_T *argvars, typval_T *rettv);
299#endif
300static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
301static void f_printf(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200302#ifdef FEAT_JOB_CHANNEL
303static void f_prompt_setcallback(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200304static void f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200305static void f_prompt_setprompt(typval_T *argvars, typval_T *rettv);
306#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200307static void f_pumvisible(typval_T *argvars, typval_T *rettv);
308#ifdef FEAT_PYTHON3
309static void f_py3eval(typval_T *argvars, typval_T *rettv);
310#endif
311#ifdef FEAT_PYTHON
312static void f_pyeval(typval_T *argvars, typval_T *rettv);
313#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100314#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
315static void f_pyxeval(typval_T *argvars, typval_T *rettv);
316#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200317static void f_range(typval_T *argvars, typval_T *rettv);
318static void f_readfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200319static void f_reg_executing(typval_T *argvars, typval_T *rettv);
320static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200321static void f_reltime(typval_T *argvars, typval_T *rettv);
322#ifdef FEAT_FLOAT
323static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
324#endif
325static void f_reltimestr(typval_T *argvars, typval_T *rettv);
326static void f_remote_expr(typval_T *argvars, typval_T *rettv);
327static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
328static void f_remote_peek(typval_T *argvars, typval_T *rettv);
329static void f_remote_read(typval_T *argvars, typval_T *rettv);
330static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100331static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200332static void f_remove(typval_T *argvars, typval_T *rettv);
333static void f_rename(typval_T *argvars, typval_T *rettv);
334static void f_repeat(typval_T *argvars, typval_T *rettv);
335static void f_resolve(typval_T *argvars, typval_T *rettv);
336static void f_reverse(typval_T *argvars, typval_T *rettv);
337#ifdef FEAT_FLOAT
338static void f_round(typval_T *argvars, typval_T *rettv);
339#endif
340static void f_screenattr(typval_T *argvars, typval_T *rettv);
341static void f_screenchar(typval_T *argvars, typval_T *rettv);
342static void f_screencol(typval_T *argvars, typval_T *rettv);
343static void f_screenrow(typval_T *argvars, typval_T *rettv);
344static void f_search(typval_T *argvars, typval_T *rettv);
345static void f_searchdecl(typval_T *argvars, typval_T *rettv);
346static void f_searchpair(typval_T *argvars, typval_T *rettv);
347static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
348static void f_searchpos(typval_T *argvars, typval_T *rettv);
349static void f_server2client(typval_T *argvars, typval_T *rettv);
350static void f_serverlist(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200351static void f_setbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200352static void f_setbufvar(typval_T *argvars, typval_T *rettv);
353static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
354static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
355static void f_setfperm(typval_T *argvars, typval_T *rettv);
356static void f_setline(typval_T *argvars, typval_T *rettv);
357static void f_setloclist(typval_T *argvars, typval_T *rettv);
358static void f_setmatches(typval_T *argvars, typval_T *rettv);
359static void f_setpos(typval_T *argvars, typval_T *rettv);
360static void f_setqflist(typval_T *argvars, typval_T *rettv);
361static void f_setreg(typval_T *argvars, typval_T *rettv);
362static void f_settabvar(typval_T *argvars, typval_T *rettv);
363static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
364static void f_setwinvar(typval_T *argvars, typval_T *rettv);
365#ifdef FEAT_CRYPT
366static void f_sha256(typval_T *argvars, typval_T *rettv);
367#endif /* FEAT_CRYPT */
368static void f_shellescape(typval_T *argvars, typval_T *rettv);
369static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
370static void f_simplify(typval_T *argvars, typval_T *rettv);
371#ifdef FEAT_FLOAT
372static void f_sin(typval_T *argvars, typval_T *rettv);
373static void f_sinh(typval_T *argvars, typval_T *rettv);
374#endif
375static void f_sort(typval_T *argvars, typval_T *rettv);
376static void f_soundfold(typval_T *argvars, typval_T *rettv);
377static void f_spellbadword(typval_T *argvars, typval_T *rettv);
378static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
379static void f_split(typval_T *argvars, typval_T *rettv);
380#ifdef FEAT_FLOAT
381static void f_sqrt(typval_T *argvars, typval_T *rettv);
382static void f_str2float(typval_T *argvars, typval_T *rettv);
383#endif
384static void f_str2nr(typval_T *argvars, typval_T *rettv);
385static void f_strchars(typval_T *argvars, typval_T *rettv);
386#ifdef HAVE_STRFTIME
387static void f_strftime(typval_T *argvars, typval_T *rettv);
388#endif
389static void f_strgetchar(typval_T *argvars, typval_T *rettv);
390static void f_stridx(typval_T *argvars, typval_T *rettv);
391static void f_string(typval_T *argvars, typval_T *rettv);
392static void f_strlen(typval_T *argvars, typval_T *rettv);
393static void f_strcharpart(typval_T *argvars, typval_T *rettv);
394static void f_strpart(typval_T *argvars, typval_T *rettv);
395static void f_strridx(typval_T *argvars, typval_T *rettv);
396static void f_strtrans(typval_T *argvars, typval_T *rettv);
397static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
398static void f_strwidth(typval_T *argvars, typval_T *rettv);
399static void f_submatch(typval_T *argvars, typval_T *rettv);
400static void f_substitute(typval_T *argvars, typval_T *rettv);
401static void f_synID(typval_T *argvars, typval_T *rettv);
402static void f_synIDattr(typval_T *argvars, typval_T *rettv);
403static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
404static void f_synstack(typval_T *argvars, typval_T *rettv);
405static void f_synconcealed(typval_T *argvars, typval_T *rettv);
406static void f_system(typval_T *argvars, typval_T *rettv);
407static void f_systemlist(typval_T *argvars, typval_T *rettv);
408static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
409static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
410static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
411static void f_taglist(typval_T *argvars, typval_T *rettv);
412static void f_tagfiles(typval_T *argvars, typval_T *rettv);
413static void f_tempname(typval_T *argvars, typval_T *rettv);
414static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
415static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200416static void f_test_feedinput(typval_T *argvars, typval_T *rettv);
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100417static void f_test_override(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200418static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100419static void f_test_ignore_error(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200420#ifdef FEAT_JOB_CHANNEL
421static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
422#endif
423static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
424#ifdef FEAT_JOB_CHANNEL
425static void f_test_null_job(typval_T *argvars, typval_T *rettv);
426#endif
427static void f_test_null_list(typval_T *argvars, typval_T *rettv);
428static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
429static void f_test_null_string(typval_T *argvars, typval_T *rettv);
430static void f_test_settime(typval_T *argvars, typval_T *rettv);
431#ifdef FEAT_FLOAT
432static void f_tan(typval_T *argvars, typval_T *rettv);
433static void f_tanh(typval_T *argvars, typval_T *rettv);
434#endif
435#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200436static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200437static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200438static void f_timer_start(typval_T *argvars, typval_T *rettv);
439static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200440static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200441#endif
442static void f_tolower(typval_T *argvars, typval_T *rettv);
443static void f_toupper(typval_T *argvars, typval_T *rettv);
444static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100445static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200446#ifdef FEAT_FLOAT
447static void f_trunc(typval_T *argvars, typval_T *rettv);
448#endif
449static void f_type(typval_T *argvars, typval_T *rettv);
450static void f_undofile(typval_T *argvars, typval_T *rettv);
451static void f_undotree(typval_T *argvars, typval_T *rettv);
452static void f_uniq(typval_T *argvars, typval_T *rettv);
453static void f_values(typval_T *argvars, typval_T *rettv);
454static void f_virtcol(typval_T *argvars, typval_T *rettv);
455static void f_visualmode(typval_T *argvars, typval_T *rettv);
456static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
457static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
458static void f_win_getid(typval_T *argvars, typval_T *rettv);
459static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
460static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
461static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100462static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200463static void f_winbufnr(typval_T *argvars, typval_T *rettv);
464static void f_wincol(typval_T *argvars, typval_T *rettv);
465static void f_winheight(typval_T *argvars, typval_T *rettv);
466static void f_winline(typval_T *argvars, typval_T *rettv);
467static void f_winnr(typval_T *argvars, typval_T *rettv);
468static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
469static void f_winrestview(typval_T *argvars, typval_T *rettv);
470static void f_winsaveview(typval_T *argvars, typval_T *rettv);
471static void f_winwidth(typval_T *argvars, typval_T *rettv);
472static void f_writefile(typval_T *argvars, typval_T *rettv);
473static void f_wordcount(typval_T *argvars, typval_T *rettv);
474static void f_xor(typval_T *argvars, typval_T *rettv);
475
476/*
477 * Array with names and number of arguments of all internal functions
478 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
479 */
480static struct fst
481{
482 char *f_name; /* function name */
483 char f_min_argc; /* minimal number of arguments */
484 char f_max_argc; /* maximal number of arguments */
485 void (*f_func)(typval_T *args, typval_T *rvar);
486 /* implementation of function */
487} functions[] =
488{
489#ifdef FEAT_FLOAT
490 {"abs", 1, 1, f_abs},
491 {"acos", 1, 1, f_acos}, /* WJMc */
492#endif
493 {"add", 2, 2, f_add},
494 {"and", 2, 2, f_and},
495 {"append", 2, 2, f_append},
Bram Moolenaarca851592018-06-06 21:04:07 +0200496 {"appendbufline", 3, 3, f_appendbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200497 {"argc", 0, 0, f_argc},
498 {"argidx", 0, 0, f_argidx},
499 {"arglistid", 0, 2, f_arglistid},
500 {"argv", 0, 1, f_argv},
501#ifdef FEAT_FLOAT
502 {"asin", 1, 1, f_asin}, /* WJMc */
503#endif
Bram Moolenaarb48e96f2018-02-13 12:26:14 +0100504 {"assert_beeps", 1, 2, f_assert_beeps},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200505 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaard96ff162018-02-18 22:13:29 +0100506 {"assert_equalfile", 2, 2, f_assert_equalfile},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200507 {"assert_exception", 1, 2, f_assert_exception},
508 {"assert_fails", 1, 2, f_assert_fails},
509 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100510 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200511 {"assert_match", 2, 3, f_assert_match},
512 {"assert_notequal", 2, 3, f_assert_notequal},
513 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100514 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200515 {"assert_true", 1, 2, f_assert_true},
516#ifdef FEAT_FLOAT
517 {"atan", 1, 1, f_atan},
518 {"atan2", 2, 2, f_atan2},
519#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100520#ifdef FEAT_BEVAL
521 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100522# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100523 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100524# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100525#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200526 {"browse", 4, 4, f_browse},
527 {"browsedir", 2, 2, f_browsedir},
528 {"bufexists", 1, 1, f_bufexists},
529 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
530 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
531 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
532 {"buflisted", 1, 1, f_buflisted},
533 {"bufloaded", 1, 1, f_bufloaded},
534 {"bufname", 1, 1, f_bufname},
535 {"bufnr", 1, 2, f_bufnr},
536 {"bufwinid", 1, 1, f_bufwinid},
537 {"bufwinnr", 1, 1, f_bufwinnr},
538 {"byte2line", 1, 1, f_byte2line},
539 {"byteidx", 2, 2, f_byteidx},
540 {"byteidxcomp", 2, 2, f_byteidxcomp},
541 {"call", 2, 3, f_call},
542#ifdef FEAT_FLOAT
543 {"ceil", 1, 1, f_ceil},
544#endif
545#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100546 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200547 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200548 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200549 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
550 {"ch_evalraw", 2, 3, f_ch_evalraw},
551 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
552 {"ch_getjob", 1, 1, f_ch_getjob},
553 {"ch_info", 1, 1, f_ch_info},
554 {"ch_log", 1, 2, f_ch_log},
555 {"ch_logfile", 1, 2, f_ch_logfile},
556 {"ch_open", 1, 2, f_ch_open},
557 {"ch_read", 1, 2, f_ch_read},
558 {"ch_readraw", 1, 2, f_ch_readraw},
559 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
560 {"ch_sendraw", 2, 3, f_ch_sendraw},
561 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200562 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200563#endif
564 {"changenr", 0, 0, f_changenr},
565 {"char2nr", 1, 2, f_char2nr},
566 {"cindent", 1, 1, f_cindent},
567 {"clearmatches", 0, 0, f_clearmatches},
568 {"col", 1, 1, f_col},
569#if defined(FEAT_INS_EXPAND)
570 {"complete", 2, 2, f_complete},
571 {"complete_add", 1, 1, f_complete_add},
572 {"complete_check", 0, 0, f_complete_check},
573#endif
574 {"confirm", 1, 4, f_confirm},
575 {"copy", 1, 1, f_copy},
576#ifdef FEAT_FLOAT
577 {"cos", 1, 1, f_cos},
578 {"cosh", 1, 1, f_cosh},
579#endif
580 {"count", 2, 4, f_count},
581 {"cscope_connection",0,3, f_cscope_connection},
582 {"cursor", 1, 3, f_cursor},
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200583#ifdef WIN3264
584 {"debugbreak", 1, 1, f_debugbreak},
585#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200586 {"deepcopy", 1, 2, f_deepcopy},
587 {"delete", 1, 2, f_delete},
Bram Moolenaard79a2622018-06-07 18:17:46 +0200588 {"deletebufline", 2, 3, f_deletebufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200589 {"did_filetype", 0, 0, f_did_filetype},
590 {"diff_filler", 1, 1, f_diff_filler},
591 {"diff_hlID", 2, 2, f_diff_hlID},
592 {"empty", 1, 1, f_empty},
593 {"escape", 2, 2, f_escape},
594 {"eval", 1, 1, f_eval},
595 {"eventhandler", 0, 0, f_eventhandler},
596 {"executable", 1, 1, f_executable},
597 {"execute", 1, 2, f_execute},
598 {"exepath", 1, 1, f_exepath},
599 {"exists", 1, 1, f_exists},
600#ifdef FEAT_FLOAT
601 {"exp", 1, 1, f_exp},
602#endif
603 {"expand", 1, 3, f_expand},
604 {"extend", 2, 3, f_extend},
605 {"feedkeys", 1, 2, f_feedkeys},
606 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
607 {"filereadable", 1, 1, f_filereadable},
608 {"filewritable", 1, 1, f_filewritable},
609 {"filter", 2, 2, f_filter},
610 {"finddir", 1, 3, f_finddir},
611 {"findfile", 1, 3, f_findfile},
612#ifdef FEAT_FLOAT
613 {"float2nr", 1, 1, f_float2nr},
614 {"floor", 1, 1, f_floor},
615 {"fmod", 2, 2, f_fmod},
616#endif
617 {"fnameescape", 1, 1, f_fnameescape},
618 {"fnamemodify", 2, 2, f_fnamemodify},
619 {"foldclosed", 1, 1, f_foldclosed},
620 {"foldclosedend", 1, 1, f_foldclosedend},
621 {"foldlevel", 1, 1, f_foldlevel},
622 {"foldtext", 0, 0, f_foldtext},
623 {"foldtextresult", 1, 1, f_foldtextresult},
624 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200625 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200626 {"function", 1, 3, f_function},
627 {"garbagecollect", 0, 1, f_garbagecollect},
628 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200629 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200630 {"getbufline", 2, 3, f_getbufline},
631 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100632 {"getchangelist", 1, 1, f_getchangelist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200633 {"getchar", 0, 1, f_getchar},
634 {"getcharmod", 0, 0, f_getcharmod},
635 {"getcharsearch", 0, 0, f_getcharsearch},
636 {"getcmdline", 0, 0, f_getcmdline},
637 {"getcmdpos", 0, 0, f_getcmdpos},
638 {"getcmdtype", 0, 0, f_getcmdtype},
639 {"getcmdwintype", 0, 0, f_getcmdwintype},
640#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200641 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200642#endif
643 {"getcurpos", 0, 0, f_getcurpos},
644 {"getcwd", 0, 2, f_getcwd},
645 {"getfontname", 0, 1, f_getfontname},
646 {"getfperm", 1, 1, f_getfperm},
647 {"getfsize", 1, 1, f_getfsize},
648 {"getftime", 1, 1, f_getftime},
649 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100650 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200651 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200652 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200653 {"getmatches", 0, 0, f_getmatches},
654 {"getpid", 0, 0, f_getpid},
655 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200656 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200657 {"getreg", 0, 3, f_getreg},
658 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200659 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200660 {"gettabvar", 2, 3, f_gettabvar},
661 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200662 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100663 {"getwinpos", 0, 1, f_getwinpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200664 {"getwinposx", 0, 0, f_getwinposx},
665 {"getwinposy", 0, 0, f_getwinposy},
666 {"getwinvar", 2, 3, f_getwinvar},
667 {"glob", 1, 4, f_glob},
668 {"glob2regpat", 1, 1, f_glob2regpat},
669 {"globpath", 2, 5, f_globpath},
670 {"has", 1, 1, f_has},
671 {"has_key", 2, 2, f_has_key},
672 {"haslocaldir", 0, 2, f_haslocaldir},
673 {"hasmapto", 1, 3, f_hasmapto},
674 {"highlightID", 1, 1, f_hlID}, /* obsolete */
675 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
676 {"histadd", 2, 2, f_histadd},
677 {"histdel", 1, 2, f_histdel},
678 {"histget", 1, 2, f_histget},
679 {"histnr", 1, 1, f_histnr},
680 {"hlID", 1, 1, f_hlID},
681 {"hlexists", 1, 1, f_hlexists},
682 {"hostname", 0, 0, f_hostname},
683 {"iconv", 3, 3, f_iconv},
684 {"indent", 1, 1, f_indent},
685 {"index", 2, 4, f_index},
686 {"input", 1, 3, f_input},
687 {"inputdialog", 1, 3, f_inputdialog},
688 {"inputlist", 1, 1, f_inputlist},
689 {"inputrestore", 0, 0, f_inputrestore},
690 {"inputsave", 0, 0, f_inputsave},
691 {"inputsecret", 1, 2, f_inputsecret},
692 {"insert", 2, 3, f_insert},
693 {"invert", 1, 1, f_invert},
694 {"isdirectory", 1, 1, f_isdirectory},
695 {"islocked", 1, 1, f_islocked},
696#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
697 {"isnan", 1, 1, f_isnan},
698#endif
699 {"items", 1, 1, f_items},
700#ifdef FEAT_JOB_CHANNEL
701 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200702 {"job_info", 0, 1, f_job_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200703 {"job_setoptions", 2, 2, f_job_setoptions},
704 {"job_start", 1, 2, f_job_start},
705 {"job_status", 1, 1, f_job_status},
706 {"job_stop", 1, 2, f_job_stop},
707#endif
708 {"join", 1, 2, f_join},
709 {"js_decode", 1, 1, f_js_decode},
710 {"js_encode", 1, 1, f_js_encode},
711 {"json_decode", 1, 1, f_json_decode},
712 {"json_encode", 1, 1, f_json_encode},
713 {"keys", 1, 1, f_keys},
714 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
715 {"len", 1, 1, f_len},
716 {"libcall", 3, 3, f_libcall},
717 {"libcallnr", 3, 3, f_libcallnr},
718 {"line", 1, 1, f_line},
719 {"line2byte", 1, 1, f_line2byte},
720 {"lispindent", 1, 1, f_lispindent},
721 {"localtime", 0, 0, f_localtime},
722#ifdef FEAT_FLOAT
723 {"log", 1, 1, f_log},
724 {"log10", 1, 1, f_log10},
725#endif
726#ifdef FEAT_LUA
727 {"luaeval", 1, 2, f_luaeval},
728#endif
729 {"map", 2, 2, f_map},
730 {"maparg", 1, 4, f_maparg},
731 {"mapcheck", 1, 3, f_mapcheck},
732 {"match", 2, 4, f_match},
733 {"matchadd", 2, 5, f_matchadd},
734 {"matchaddpos", 2, 5, f_matchaddpos},
735 {"matcharg", 1, 1, f_matcharg},
736 {"matchdelete", 1, 1, f_matchdelete},
737 {"matchend", 2, 4, f_matchend},
738 {"matchlist", 2, 4, f_matchlist},
739 {"matchstr", 2, 4, f_matchstr},
740 {"matchstrpos", 2, 4, f_matchstrpos},
741 {"max", 1, 1, f_max},
742 {"min", 1, 1, f_min},
743#ifdef vim_mkdir
744 {"mkdir", 1, 3, f_mkdir},
745#endif
746 {"mode", 0, 1, f_mode},
747#ifdef FEAT_MZSCHEME
748 {"mzeval", 1, 1, f_mzeval},
749#endif
750 {"nextnonblank", 1, 1, f_nextnonblank},
751 {"nr2char", 1, 2, f_nr2char},
752 {"or", 2, 2, f_or},
753 {"pathshorten", 1, 1, f_pathshorten},
754#ifdef FEAT_PERL
755 {"perleval", 1, 1, f_perleval},
756#endif
757#ifdef FEAT_FLOAT
758 {"pow", 2, 2, f_pow},
759#endif
760 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100761 {"printf", 1, 19, f_printf},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200762#ifdef FEAT_JOB_CHANNEL
763 {"prompt_setcallback", 2, 2, f_prompt_setcallback},
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200764 {"prompt_setinterrupt", 2, 2, f_prompt_setinterrupt},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200765 {"prompt_setprompt", 2, 2, f_prompt_setprompt},
766#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200767 {"pumvisible", 0, 0, f_pumvisible},
768#ifdef FEAT_PYTHON3
769 {"py3eval", 1, 1, f_py3eval},
770#endif
771#ifdef FEAT_PYTHON
772 {"pyeval", 1, 1, f_pyeval},
773#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100774#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
775 {"pyxeval", 1, 1, f_pyxeval},
776#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200777 {"range", 1, 3, f_range},
778 {"readfile", 1, 3, f_readfile},
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200779 {"reg_executing", 0, 0, f_reg_executing},
780 {"reg_recording", 0, 0, f_reg_recording},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200781 {"reltime", 0, 2, f_reltime},
782#ifdef FEAT_FLOAT
783 {"reltimefloat", 1, 1, f_reltimefloat},
784#endif
785 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100786 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200787 {"remote_foreground", 1, 1, f_remote_foreground},
788 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100789 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200790 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100791 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200792 {"remove", 2, 3, f_remove},
793 {"rename", 2, 2, f_rename},
794 {"repeat", 2, 2, f_repeat},
795 {"resolve", 1, 1, f_resolve},
796 {"reverse", 1, 1, f_reverse},
797#ifdef FEAT_FLOAT
798 {"round", 1, 1, f_round},
799#endif
800 {"screenattr", 2, 2, f_screenattr},
801 {"screenchar", 2, 2, f_screenchar},
802 {"screencol", 0, 0, f_screencol},
803 {"screenrow", 0, 0, f_screenrow},
804 {"search", 1, 4, f_search},
805 {"searchdecl", 1, 3, f_searchdecl},
806 {"searchpair", 3, 7, f_searchpair},
807 {"searchpairpos", 3, 7, f_searchpairpos},
808 {"searchpos", 1, 4, f_searchpos},
809 {"server2client", 2, 2, f_server2client},
810 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200811 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200812 {"setbufvar", 3, 3, f_setbufvar},
813 {"setcharsearch", 1, 1, f_setcharsearch},
814 {"setcmdpos", 1, 1, f_setcmdpos},
815 {"setfperm", 2, 2, f_setfperm},
816 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200817 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200818 {"setmatches", 1, 1, f_setmatches},
819 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200820 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200821 {"setreg", 2, 3, f_setreg},
822 {"settabvar", 3, 3, f_settabvar},
823 {"settabwinvar", 4, 4, f_settabwinvar},
824 {"setwinvar", 3, 3, f_setwinvar},
825#ifdef FEAT_CRYPT
826 {"sha256", 1, 1, f_sha256},
827#endif
828 {"shellescape", 1, 2, f_shellescape},
829 {"shiftwidth", 0, 0, f_shiftwidth},
830 {"simplify", 1, 1, f_simplify},
831#ifdef FEAT_FLOAT
832 {"sin", 1, 1, f_sin},
833 {"sinh", 1, 1, f_sinh},
834#endif
835 {"sort", 1, 3, f_sort},
836 {"soundfold", 1, 1, f_soundfold},
837 {"spellbadword", 0, 1, f_spellbadword},
838 {"spellsuggest", 1, 3, f_spellsuggest},
839 {"split", 1, 3, f_split},
840#ifdef FEAT_FLOAT
841 {"sqrt", 1, 1, f_sqrt},
842 {"str2float", 1, 1, f_str2float},
843#endif
844 {"str2nr", 1, 2, f_str2nr},
845 {"strcharpart", 2, 3, f_strcharpart},
846 {"strchars", 1, 2, f_strchars},
847 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
848#ifdef HAVE_STRFTIME
849 {"strftime", 1, 2, f_strftime},
850#endif
851 {"strgetchar", 2, 2, f_strgetchar},
852 {"stridx", 2, 3, f_stridx},
853 {"string", 1, 1, f_string},
854 {"strlen", 1, 1, f_strlen},
855 {"strpart", 2, 3, f_strpart},
856 {"strridx", 2, 3, f_strridx},
857 {"strtrans", 1, 1, f_strtrans},
858 {"strwidth", 1, 1, f_strwidth},
859 {"submatch", 1, 2, f_submatch},
860 {"substitute", 4, 4, f_substitute},
861 {"synID", 3, 3, f_synID},
862 {"synIDattr", 2, 3, f_synIDattr},
863 {"synIDtrans", 1, 1, f_synIDtrans},
864 {"synconcealed", 2, 2, f_synconcealed},
865 {"synstack", 2, 2, f_synstack},
866 {"system", 1, 2, f_system},
867 {"systemlist", 1, 2, f_systemlist},
868 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
869 {"tabpagenr", 0, 1, f_tabpagenr},
870 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
871 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100872 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200873#ifdef FEAT_FLOAT
874 {"tan", 1, 1, f_tan},
875 {"tanh", 1, 1, f_tanh},
876#endif
877 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200878#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100879 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
880 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100881 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200882 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200883# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
884 {"term_getansicolors", 1, 1, f_term_getansicolors},
885# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200886 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200887 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200888 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200889 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200890 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200891 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200892 {"term_getstatus", 1, 1, f_term_getstatus},
893 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200894 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200895 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200896 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200897 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200898# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
899 {"term_setansicolors", 2, 2, f_term_setansicolors},
900# endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100901 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100902 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaara42d3632018-04-14 17:05:38 +0200903 {"term_setsize", 3, 3, f_term_setsize},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200904 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200905 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200906#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200907 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
908 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200909 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200910 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100911 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200912#ifdef FEAT_JOB_CHANNEL
913 {"test_null_channel", 0, 0, f_test_null_channel},
914#endif
915 {"test_null_dict", 0, 0, f_test_null_dict},
916#ifdef FEAT_JOB_CHANNEL
917 {"test_null_job", 0, 0, f_test_null_job},
918#endif
919 {"test_null_list", 0, 0, f_test_null_list},
920 {"test_null_partial", 0, 0, f_test_null_partial},
921 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100922 {"test_override", 2, 2, f_test_override},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200923 {"test_settime", 1, 1, f_test_settime},
924#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200925 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200926 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200927 {"timer_start", 2, 3, f_timer_start},
928 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200929 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200930#endif
931 {"tolower", 1, 1, f_tolower},
932 {"toupper", 1, 1, f_toupper},
933 {"tr", 3, 3, f_tr},
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100934 {"trim", 1, 2, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200935#ifdef FEAT_FLOAT
936 {"trunc", 1, 1, f_trunc},
937#endif
938 {"type", 1, 1, f_type},
939 {"undofile", 1, 1, f_undofile},
940 {"undotree", 0, 0, f_undotree},
941 {"uniq", 1, 3, f_uniq},
942 {"values", 1, 1, f_values},
943 {"virtcol", 1, 1, f_virtcol},
944 {"visualmode", 0, 1, f_visualmode},
945 {"wildmenumode", 0, 0, f_wildmenumode},
946 {"win_findbuf", 1, 1, f_win_findbuf},
947 {"win_getid", 0, 2, f_win_getid},
948 {"win_gotoid", 1, 1, f_win_gotoid},
949 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
950 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100951 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200952 {"winbufnr", 1, 1, f_winbufnr},
953 {"wincol", 0, 0, f_wincol},
954 {"winheight", 1, 1, f_winheight},
955 {"winline", 0, 0, f_winline},
956 {"winnr", 0, 1, f_winnr},
957 {"winrestcmd", 0, 0, f_winrestcmd},
958 {"winrestview", 1, 1, f_winrestview},
959 {"winsaveview", 0, 0, f_winsaveview},
960 {"winwidth", 1, 1, f_winwidth},
961 {"wordcount", 0, 0, f_wordcount},
962 {"writefile", 2, 3, f_writefile},
963 {"xor", 2, 2, f_xor},
964};
965
966#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
967
968/*
969 * Function given to ExpandGeneric() to obtain the list of internal
970 * or user defined function names.
971 */
972 char_u *
973get_function_name(expand_T *xp, int idx)
974{
975 static int intidx = -1;
976 char_u *name;
977
978 if (idx == 0)
979 intidx = -1;
980 if (intidx < 0)
981 {
982 name = get_user_func_name(xp, idx);
983 if (name != NULL)
984 return name;
985 }
986 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
987 {
988 STRCPY(IObuff, functions[intidx].f_name);
989 STRCAT(IObuff, "(");
990 if (functions[intidx].f_max_argc == 0)
991 STRCAT(IObuff, ")");
992 return IObuff;
993 }
994
995 return NULL;
996}
997
998/*
999 * Function given to ExpandGeneric() to obtain the list of internal or
1000 * user defined variable or function names.
1001 */
1002 char_u *
1003get_expr_name(expand_T *xp, int idx)
1004{
1005 static int intidx = -1;
1006 char_u *name;
1007
1008 if (idx == 0)
1009 intidx = -1;
1010 if (intidx < 0)
1011 {
1012 name = get_function_name(xp, idx);
1013 if (name != NULL)
1014 return name;
1015 }
1016 return get_user_var_name(xp, ++intidx);
1017}
1018
1019#endif /* FEAT_CMDL_COMPL */
1020
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001021/*
1022 * Find internal function in table above.
1023 * Return index, or -1 if not found
1024 */
1025 int
1026find_internal_func(
1027 char_u *name) /* name of the function */
1028{
1029 int first = 0;
1030 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
1031 int cmp;
1032 int x;
1033
1034 /*
1035 * Find the function name in the table. Binary search.
1036 */
1037 while (first <= last)
1038 {
1039 x = first + ((unsigned)(last - first) >> 1);
1040 cmp = STRCMP(name, functions[x].f_name);
1041 if (cmp < 0)
1042 last = x - 1;
1043 else if (cmp > 0)
1044 first = x + 1;
1045 else
1046 return x;
1047 }
1048 return -1;
1049}
1050
1051 int
1052call_internal_func(
1053 char_u *name,
1054 int argcount,
1055 typval_T *argvars,
1056 typval_T *rettv)
1057{
1058 int i;
1059
1060 i = find_internal_func(name);
1061 if (i < 0)
1062 return ERROR_UNKNOWN;
1063 if (argcount < functions[i].f_min_argc)
1064 return ERROR_TOOFEW;
1065 if (argcount > functions[i].f_max_argc)
1066 return ERROR_TOOMANY;
1067 argvars[argcount].v_type = VAR_UNKNOWN;
1068 functions[i].f_func(argvars, rettv);
1069 return ERROR_NONE;
1070}
1071
1072/*
1073 * Return TRUE for a non-zero Number and a non-empty String.
1074 */
1075 static int
1076non_zero_arg(typval_T *argvars)
1077{
1078 return ((argvars[0].v_type == VAR_NUMBER
1079 && argvars[0].vval.v_number != 0)
1080 || (argvars[0].v_type == VAR_SPECIAL
1081 && argvars[0].vval.v_number == VVAL_TRUE)
1082 || (argvars[0].v_type == VAR_STRING
1083 && argvars[0].vval.v_string != NULL
1084 && *argvars[0].vval.v_string != NUL));
1085}
1086
1087/*
1088 * Get the lnum from the first argument.
1089 * Also accepts ".", "$", etc., but that only works for the current buffer.
1090 * Returns -1 on error.
1091 */
1092 static linenr_T
1093get_tv_lnum(typval_T *argvars)
1094{
1095 typval_T rettv;
1096 linenr_T lnum;
1097
1098 lnum = (linenr_T)get_tv_number_chk(&argvars[0], NULL);
1099 if (lnum == 0) /* no valid number, try using line() */
1100 {
1101 rettv.v_type = VAR_NUMBER;
1102 f_line(argvars, &rettv);
1103 lnum = (linenr_T)rettv.vval.v_number;
1104 clear_tv(&rettv);
1105 }
1106 return lnum;
1107}
1108
1109#ifdef FEAT_FLOAT
1110static int get_float_arg(typval_T *argvars, float_T *f);
1111
1112/*
1113 * Get the float value of "argvars[0]" into "f".
1114 * Returns FAIL when the argument is not a Number or Float.
1115 */
1116 static int
1117get_float_arg(typval_T *argvars, float_T *f)
1118{
1119 if (argvars[0].v_type == VAR_FLOAT)
1120 {
1121 *f = argvars[0].vval.v_float;
1122 return OK;
1123 }
1124 if (argvars[0].v_type == VAR_NUMBER)
1125 {
1126 *f = (float_T)argvars[0].vval.v_number;
1127 return OK;
1128 }
1129 EMSG(_("E808: Number or Float required"));
1130 return FAIL;
1131}
1132
1133/*
1134 * "abs(expr)" function
1135 */
1136 static void
1137f_abs(typval_T *argvars, typval_T *rettv)
1138{
1139 if (argvars[0].v_type == VAR_FLOAT)
1140 {
1141 rettv->v_type = VAR_FLOAT;
1142 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1143 }
1144 else
1145 {
1146 varnumber_T n;
1147 int error = FALSE;
1148
1149 n = get_tv_number_chk(&argvars[0], &error);
1150 if (error)
1151 rettv->vval.v_number = -1;
1152 else if (n > 0)
1153 rettv->vval.v_number = n;
1154 else
1155 rettv->vval.v_number = -n;
1156 }
1157}
1158
1159/*
1160 * "acos()" function
1161 */
1162 static void
1163f_acos(typval_T *argvars, typval_T *rettv)
1164{
1165 float_T f = 0.0;
1166
1167 rettv->v_type = VAR_FLOAT;
1168 if (get_float_arg(argvars, &f) == OK)
1169 rettv->vval.v_float = acos(f);
1170 else
1171 rettv->vval.v_float = 0.0;
1172}
1173#endif
1174
1175/*
1176 * "add(list, item)" function
1177 */
1178 static void
1179f_add(typval_T *argvars, typval_T *rettv)
1180{
1181 list_T *l;
1182
1183 rettv->vval.v_number = 1; /* Default: Failed */
1184 if (argvars[0].v_type == VAR_LIST)
1185 {
1186 if ((l = argvars[0].vval.v_list) != NULL
1187 && !tv_check_lock(l->lv_lock,
1188 (char_u *)N_("add() argument"), TRUE)
1189 && list_append_tv(l, &argvars[1]) == OK)
1190 copy_tv(&argvars[0], rettv);
1191 }
1192 else
1193 EMSG(_(e_listreq));
1194}
1195
1196/*
1197 * "and(expr, expr)" function
1198 */
1199 static void
1200f_and(typval_T *argvars, typval_T *rettv)
1201{
1202 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
1203 & get_tv_number_chk(&argvars[1], NULL);
1204}
1205
1206/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001207 * Get the lnum from the first argument.
1208 * Also accepts "$", then "buf" is used.
1209 * Returns 0 on error.
1210 */
1211 static linenr_T
1212get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
1213{
1214 if (argvars[0].v_type == VAR_STRING
1215 && argvars[0].vval.v_string != NULL
1216 && argvars[0].vval.v_string[0] == '$'
1217 && buf != NULL)
1218 return buf->b_ml.ml_line_count;
1219 return (linenr_T)get_tv_number_chk(&argvars[0], NULL);
1220}
1221
1222/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02001223 * If there is a window for "curbuf", make it the current window.
1224 */
1225 static void
1226find_win_for_curbuf(void)
1227{
1228 wininfo_T *wip;
1229
1230 for (wip = curbuf->b_wininfo; wip != NULL; wip = wip->wi_next)
1231 {
1232 if (wip->wi_win != NULL)
1233 {
1234 curwin = wip->wi_win;
1235 break;
1236 }
1237 }
1238}
1239
1240/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001241 * Set line or list of lines in buffer "buf".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001242 */
1243 static void
Bram Moolenaarca851592018-06-06 21:04:07 +02001244set_buffer_lines(
1245 buf_T *buf,
1246 linenr_T lnum_arg,
1247 int append,
1248 typval_T *lines,
1249 typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001250{
Bram Moolenaarca851592018-06-06 21:04:07 +02001251 linenr_T lnum = lnum_arg + (append ? 1 : 0);
1252 char_u *line = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001253 list_T *l = NULL;
1254 listitem_T *li = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001255 long added = 0;
Bram Moolenaarca851592018-06-06 21:04:07 +02001256 linenr_T append_lnum;
1257 buf_T *curbuf_save = NULL;
1258 win_T *curwin_save = NULL;
1259 int is_curbuf = buf == curbuf;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001260
Bram Moolenaarca851592018-06-06 21:04:07 +02001261 /* When using the current buffer ml_mfp will be set if needed. Useful when
1262 * setline() is used on startup. For other buffers the buffer must be
1263 * loaded. */
1264 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001265 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001266 rettv->vval.v_number = 1; /* FAIL */
1267 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001268 }
1269
Bram Moolenaarca851592018-06-06 21:04:07 +02001270 if (!is_curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001271 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001272 curbuf_save = curbuf;
1273 curwin_save = curwin;
1274 curbuf = buf;
Bram Moolenaard79a2622018-06-07 18:17:46 +02001275 find_win_for_curbuf();
Bram Moolenaarca851592018-06-06 21:04:07 +02001276 }
1277
1278 if (append)
1279 // appendbufline() uses the line number below which we insert
1280 append_lnum = lnum - 1;
1281 else
1282 // setbufline() uses the line number above which we insert, we only
1283 // append if it's below the last line
1284 append_lnum = curbuf->b_ml.ml_line_count;
1285
1286 if (lines->v_type == VAR_LIST)
1287 {
1288 l = lines->vval.v_list;
1289 li = l->lv_first;
1290 }
1291 else
1292 line = get_tv_string_chk(lines);
1293
1294 /* default result is zero == OK */
1295 for (;;)
1296 {
1297 if (l != NULL)
1298 {
1299 /* list argument, get next string */
1300 if (li == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001301 break;
Bram Moolenaarca851592018-06-06 21:04:07 +02001302 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001303 li = li->li_next;
1304 }
1305
Bram Moolenaarca851592018-06-06 21:04:07 +02001306 rettv->vval.v_number = 1; /* FAIL */
1307 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
1308 break;
1309
1310 /* When coming here from Insert mode, sync undo, so that this can be
1311 * undone separately from what was previously inserted. */
1312 if (u_sync_once == 2)
1313 {
1314 u_sync_once = 1; /* notify that u_sync() was called */
1315 u_sync(TRUE);
1316 }
1317
1318 if (!append && lnum <= curbuf->b_ml.ml_line_count)
1319 {
1320 /* existing line, replace it */
1321 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
1322 {
1323 changed_bytes(lnum, 0);
1324 if (is_curbuf && lnum == curwin->w_cursor.lnum)
1325 check_cursor_col();
1326 rettv->vval.v_number = 0; /* OK */
1327 }
1328 }
1329 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
1330 {
1331 /* append the line */
1332 ++added;
1333 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
1334 rettv->vval.v_number = 0; /* OK */
1335 }
1336
1337 if (l == NULL) /* only one string argument */
1338 break;
1339 ++lnum;
1340 }
1341
1342 if (added > 0)
1343 {
1344 win_T *wp;
1345 tabpage_T *tp;
1346
1347 appended_lines_mark(append_lnum, added);
1348 FOR_ALL_TAB_WINDOWS(tp, wp)
1349 if (wp->w_buffer == buf && wp->w_cursor.lnum > append_lnum)
1350 wp->w_cursor.lnum += added;
1351 check_cursor_col();
1352
Bram Moolenaarf2732452018-06-03 14:47:35 +02001353#ifdef FEAT_JOB_CHANNEL
1354 if (bt_prompt(curbuf) && (State & INSERT))
1355 // show the line with the prompt
1356 update_topline();
1357#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001358 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001359
1360 if (!is_curbuf)
1361 {
1362 curbuf = curbuf_save;
1363 curwin = curwin_save;
1364 }
1365}
1366
1367/*
1368 * "append(lnum, string/list)" function
1369 */
1370 static void
1371f_append(typval_T *argvars, typval_T *rettv)
1372{
1373 linenr_T lnum = get_tv_lnum(&argvars[0]);
1374
1375 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1376}
1377
1378/*
1379 * "appendbufline(buf, lnum, string/list)" function
1380 */
1381 static void
1382f_appendbufline(typval_T *argvars, typval_T *rettv)
1383{
1384 linenr_T lnum;
1385 buf_T *buf;
1386
1387 buf = get_buf_tv(&argvars[0], FALSE);
1388 if (buf == NULL)
1389 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001390 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001391 {
1392 lnum = get_tv_lnum_buf(&argvars[1], buf);
1393 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1394 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001395}
1396
1397/*
1398 * "argc()" function
1399 */
1400 static void
1401f_argc(typval_T *argvars UNUSED, typval_T *rettv)
1402{
1403 rettv->vval.v_number = ARGCOUNT;
1404}
1405
1406/*
1407 * "argidx()" function
1408 */
1409 static void
1410f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1411{
1412 rettv->vval.v_number = curwin->w_arg_idx;
1413}
1414
1415/*
1416 * "arglistid()" function
1417 */
1418 static void
1419f_arglistid(typval_T *argvars, typval_T *rettv)
1420{
1421 win_T *wp;
1422
1423 rettv->vval.v_number = -1;
1424 wp = find_tabwin(&argvars[0], &argvars[1]);
1425 if (wp != NULL)
1426 rettv->vval.v_number = wp->w_alist->id;
1427}
1428
1429/*
1430 * "argv(nr)" function
1431 */
1432 static void
1433f_argv(typval_T *argvars, typval_T *rettv)
1434{
1435 int idx;
1436
1437 if (argvars[0].v_type != VAR_UNKNOWN)
1438 {
1439 idx = (int)get_tv_number_chk(&argvars[0], NULL);
1440 if (idx >= 0 && idx < ARGCOUNT)
1441 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
1442 else
1443 rettv->vval.v_string = NULL;
1444 rettv->v_type = VAR_STRING;
1445 }
1446 else if (rettv_list_alloc(rettv) == OK)
1447 for (idx = 0; idx < ARGCOUNT; ++idx)
1448 list_append_string(rettv->vval.v_list,
1449 alist_name(&ARGLIST[idx]), -1);
1450}
1451
1452/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001453 * "assert_beeps(cmd [, error])" function
1454 */
1455 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001456f_assert_beeps(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001457{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001458 rettv->vval.v_number = assert_beeps(argvars);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001459}
1460
1461/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001462 * "assert_equal(expected, actual[, msg])" function
1463 */
1464 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001465f_assert_equal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001466{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001467 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_EQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001468}
1469
1470/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01001471 * "assert_equalfile(fname-one, fname-two)" function
1472 */
1473 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001474f_assert_equalfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaard96ff162018-02-18 22:13:29 +01001475{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001476 rettv->vval.v_number = assert_equalfile(argvars);
Bram Moolenaard96ff162018-02-18 22:13:29 +01001477}
1478
1479/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001480 * "assert_notequal(expected, actual[, msg])" function
1481 */
1482 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001483f_assert_notequal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001484{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001485 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001486}
1487
1488/*
1489 * "assert_exception(string[, msg])" function
1490 */
1491 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001492f_assert_exception(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001493{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001494 rettv->vval.v_number = assert_exception(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001495}
1496
1497/*
1498 * "assert_fails(cmd [, error])" function
1499 */
1500 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001501f_assert_fails(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001502{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001503 rettv->vval.v_number = assert_fails(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001504}
1505
1506/*
1507 * "assert_false(actual[, msg])" function
1508 */
1509 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001510f_assert_false(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001511{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001512 rettv->vval.v_number = assert_bool(argvars, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001513}
1514
1515/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001516 * "assert_inrange(lower, upper[, msg])" function
1517 */
1518 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001519f_assert_inrange(typval_T *argvars, typval_T *rettv)
Bram Moolenaar61c04492016-07-23 15:35:35 +02001520{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001521 rettv->vval.v_number = assert_inrange(argvars);
Bram Moolenaar61c04492016-07-23 15:35:35 +02001522}
1523
1524/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001525 * "assert_match(pattern, actual[, msg])" function
1526 */
1527 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001528f_assert_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001529{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001530 rettv->vval.v_number = assert_match_common(argvars, ASSERT_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001531}
1532
1533/*
1534 * "assert_notmatch(pattern, actual[, msg])" function
1535 */
1536 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001537f_assert_notmatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001538{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001539 rettv->vval.v_number = assert_match_common(argvars, ASSERT_NOTMATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001540}
1541
1542/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001543 * "assert_report(msg)" function
1544 */
1545 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001546f_assert_report(typval_T *argvars, typval_T *rettv)
Bram Moolenaar42205552017-03-18 19:42:22 +01001547{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001548 rettv->vval.v_number = assert_report(argvars);
Bram Moolenaar42205552017-03-18 19:42:22 +01001549}
1550
1551/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001552 * "assert_true(actual[, msg])" function
1553 */
1554 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001555f_assert_true(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001556{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001557 rettv->vval.v_number = assert_bool(argvars, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001558}
1559
1560#ifdef FEAT_FLOAT
1561/*
1562 * "asin()" function
1563 */
1564 static void
1565f_asin(typval_T *argvars, typval_T *rettv)
1566{
1567 float_T f = 0.0;
1568
1569 rettv->v_type = VAR_FLOAT;
1570 if (get_float_arg(argvars, &f) == OK)
1571 rettv->vval.v_float = asin(f);
1572 else
1573 rettv->vval.v_float = 0.0;
1574}
1575
1576/*
1577 * "atan()" function
1578 */
1579 static void
1580f_atan(typval_T *argvars, typval_T *rettv)
1581{
1582 float_T f = 0.0;
1583
1584 rettv->v_type = VAR_FLOAT;
1585 if (get_float_arg(argvars, &f) == OK)
1586 rettv->vval.v_float = atan(f);
1587 else
1588 rettv->vval.v_float = 0.0;
1589}
1590
1591/*
1592 * "atan2()" function
1593 */
1594 static void
1595f_atan2(typval_T *argvars, typval_T *rettv)
1596{
1597 float_T fx = 0.0, fy = 0.0;
1598
1599 rettv->v_type = VAR_FLOAT;
1600 if (get_float_arg(argvars, &fx) == OK
1601 && get_float_arg(&argvars[1], &fy) == OK)
1602 rettv->vval.v_float = atan2(fx, fy);
1603 else
1604 rettv->vval.v_float = 0.0;
1605}
1606#endif
1607
1608/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001609 * "balloon_show()" function
1610 */
1611#ifdef FEAT_BEVAL
1612 static void
1613f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1614{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001615 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001616 {
1617 if (argvars[0].v_type == VAR_LIST
1618# ifdef FEAT_GUI
1619 && !gui.in_use
1620# endif
1621 )
1622 post_balloon(balloonEval, NULL, argvars[0].vval.v_list);
1623 else
1624 post_balloon(balloonEval, get_tv_string_chk(&argvars[0]), NULL);
1625 }
1626}
1627
Bram Moolenaar669a8282017-11-19 20:13:05 +01001628# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001629 static void
1630f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1631{
1632 if (rettv_list_alloc(rettv) == OK)
1633 {
1634 char_u *msg = get_tv_string_chk(&argvars[0]);
1635
1636 if (msg != NULL)
1637 {
1638 pumitem_T *array;
1639 int size = split_message(msg, &array);
1640 int i;
1641
1642 /* Skip the first and last item, they are always empty. */
1643 for (i = 1; i < size - 1; ++i)
1644 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001645 while (size > 0)
1646 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001647 vim_free(array);
1648 }
1649 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001650}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001651# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001652#endif
1653
1654/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001655 * "browse(save, title, initdir, default)" function
1656 */
1657 static void
1658f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1659{
1660#ifdef FEAT_BROWSE
1661 int save;
1662 char_u *title;
1663 char_u *initdir;
1664 char_u *defname;
1665 char_u buf[NUMBUFLEN];
1666 char_u buf2[NUMBUFLEN];
1667 int error = FALSE;
1668
1669 save = (int)get_tv_number_chk(&argvars[0], &error);
1670 title = get_tv_string_chk(&argvars[1]);
1671 initdir = get_tv_string_buf_chk(&argvars[2], buf);
1672 defname = get_tv_string_buf_chk(&argvars[3], buf2);
1673
1674 if (error || title == NULL || initdir == NULL || defname == NULL)
1675 rettv->vval.v_string = NULL;
1676 else
1677 rettv->vval.v_string =
1678 do_browse(save ? BROWSE_SAVE : 0,
1679 title, defname, NULL, initdir, NULL, curbuf);
1680#else
1681 rettv->vval.v_string = NULL;
1682#endif
1683 rettv->v_type = VAR_STRING;
1684}
1685
1686/*
1687 * "browsedir(title, initdir)" function
1688 */
1689 static void
1690f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1691{
1692#ifdef FEAT_BROWSE
1693 char_u *title;
1694 char_u *initdir;
1695 char_u buf[NUMBUFLEN];
1696
1697 title = get_tv_string_chk(&argvars[0]);
1698 initdir = get_tv_string_buf_chk(&argvars[1], buf);
1699
1700 if (title == NULL || initdir == NULL)
1701 rettv->vval.v_string = NULL;
1702 else
1703 rettv->vval.v_string = do_browse(BROWSE_DIR,
1704 title, NULL, NULL, initdir, NULL, curbuf);
1705#else
1706 rettv->vval.v_string = NULL;
1707#endif
1708 rettv->v_type = VAR_STRING;
1709}
1710
1711static buf_T *find_buffer(typval_T *avar);
1712
1713/*
1714 * Find a buffer by number or exact name.
1715 */
1716 static buf_T *
1717find_buffer(typval_T *avar)
1718{
1719 buf_T *buf = NULL;
1720
1721 if (avar->v_type == VAR_NUMBER)
1722 buf = buflist_findnr((int)avar->vval.v_number);
1723 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1724 {
1725 buf = buflist_findname_exp(avar->vval.v_string);
1726 if (buf == NULL)
1727 {
1728 /* No full path name match, try a match with a URL or a "nofile"
1729 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001730 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001731 if (buf->b_fname != NULL
1732 && (path_with_url(buf->b_fname)
1733#ifdef FEAT_QUICKFIX
1734 || bt_nofile(buf)
1735#endif
1736 )
1737 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1738 break;
1739 }
1740 }
1741 return buf;
1742}
1743
1744/*
1745 * "bufexists(expr)" function
1746 */
1747 static void
1748f_bufexists(typval_T *argvars, typval_T *rettv)
1749{
1750 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1751}
1752
1753/*
1754 * "buflisted(expr)" function
1755 */
1756 static void
1757f_buflisted(typval_T *argvars, typval_T *rettv)
1758{
1759 buf_T *buf;
1760
1761 buf = find_buffer(&argvars[0]);
1762 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1763}
1764
1765/*
1766 * "bufloaded(expr)" function
1767 */
1768 static void
1769f_bufloaded(typval_T *argvars, typval_T *rettv)
1770{
1771 buf_T *buf;
1772
1773 buf = find_buffer(&argvars[0]);
1774 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1775}
1776
1777 buf_T *
1778buflist_find_by_name(char_u *name, int curtab_only)
1779{
1780 int save_magic;
1781 char_u *save_cpo;
1782 buf_T *buf;
1783
1784 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1785 save_magic = p_magic;
1786 p_magic = TRUE;
1787 save_cpo = p_cpo;
1788 p_cpo = (char_u *)"";
1789
1790 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1791 TRUE, FALSE, curtab_only));
1792
1793 p_magic = save_magic;
1794 p_cpo = save_cpo;
1795 return buf;
1796}
1797
1798/*
1799 * Get buffer by number or pattern.
1800 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001801 buf_T *
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001802get_buf_tv(typval_T *tv, int curtab_only)
1803{
1804 char_u *name = tv->vval.v_string;
1805 buf_T *buf;
1806
1807 if (tv->v_type == VAR_NUMBER)
1808 return buflist_findnr((int)tv->vval.v_number);
1809 if (tv->v_type != VAR_STRING)
1810 return NULL;
1811 if (name == NULL || *name == NUL)
1812 return curbuf;
1813 if (name[0] == '$' && name[1] == NUL)
1814 return lastbuf;
1815
1816 buf = buflist_find_by_name(name, curtab_only);
1817
1818 /* If not found, try expanding the name, like done for bufexists(). */
1819 if (buf == NULL)
1820 buf = find_buffer(tv);
1821
1822 return buf;
1823}
1824
1825/*
1826 * "bufname(expr)" function
1827 */
1828 static void
1829f_bufname(typval_T *argvars, typval_T *rettv)
1830{
1831 buf_T *buf;
1832
1833 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1834 ++emsg_off;
1835 buf = get_buf_tv(&argvars[0], FALSE);
1836 rettv->v_type = VAR_STRING;
1837 if (buf != NULL && buf->b_fname != NULL)
1838 rettv->vval.v_string = vim_strsave(buf->b_fname);
1839 else
1840 rettv->vval.v_string = NULL;
1841 --emsg_off;
1842}
1843
1844/*
1845 * "bufnr(expr)" function
1846 */
1847 static void
1848f_bufnr(typval_T *argvars, typval_T *rettv)
1849{
1850 buf_T *buf;
1851 int error = FALSE;
1852 char_u *name;
1853
1854 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1855 ++emsg_off;
1856 buf = get_buf_tv(&argvars[0], FALSE);
1857 --emsg_off;
1858
1859 /* If the buffer isn't found and the second argument is not zero create a
1860 * new buffer. */
1861 if (buf == NULL
1862 && argvars[1].v_type != VAR_UNKNOWN
1863 && get_tv_number_chk(&argvars[1], &error) != 0
1864 && !error
1865 && (name = get_tv_string_chk(&argvars[0])) != NULL
1866 && !error)
1867 buf = buflist_new(name, NULL, (linenr_T)1, 0);
1868
1869 if (buf != NULL)
1870 rettv->vval.v_number = buf->b_fnum;
1871 else
1872 rettv->vval.v_number = -1;
1873}
1874
1875 static void
1876buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
1877{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001878 win_T *wp;
1879 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001880 buf_T *buf;
1881
1882 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1883 ++emsg_off;
1884 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02001885 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001886 {
1887 ++winnr;
1888 if (wp->w_buffer == buf)
1889 break;
1890 }
1891 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001892 --emsg_off;
1893}
1894
1895/*
1896 * "bufwinid(nr)" function
1897 */
1898 static void
1899f_bufwinid(typval_T *argvars, typval_T *rettv)
1900{
1901 buf_win_common(argvars, rettv, FALSE);
1902}
1903
1904/*
1905 * "bufwinnr(nr)" function
1906 */
1907 static void
1908f_bufwinnr(typval_T *argvars, typval_T *rettv)
1909{
1910 buf_win_common(argvars, rettv, TRUE);
1911}
1912
1913/*
1914 * "byte2line(byte)" function
1915 */
1916 static void
1917f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
1918{
1919#ifndef FEAT_BYTEOFF
1920 rettv->vval.v_number = -1;
1921#else
1922 long boff = 0;
1923
1924 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
1925 if (boff < 0)
1926 rettv->vval.v_number = -1;
1927 else
1928 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
1929 (linenr_T)0, &boff);
1930#endif
1931}
1932
1933 static void
1934byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
1935{
1936#ifdef FEAT_MBYTE
1937 char_u *t;
1938#endif
1939 char_u *str;
1940 varnumber_T idx;
1941
1942 str = get_tv_string_chk(&argvars[0]);
1943 idx = get_tv_number_chk(&argvars[1], NULL);
1944 rettv->vval.v_number = -1;
1945 if (str == NULL || idx < 0)
1946 return;
1947
1948#ifdef FEAT_MBYTE
1949 t = str;
1950 for ( ; idx > 0; idx--)
1951 {
1952 if (*t == NUL) /* EOL reached */
1953 return;
1954 if (enc_utf8 && comp)
1955 t += utf_ptr2len(t);
1956 else
1957 t += (*mb_ptr2len)(t);
1958 }
1959 rettv->vval.v_number = (varnumber_T)(t - str);
1960#else
1961 if ((size_t)idx <= STRLEN(str))
1962 rettv->vval.v_number = idx;
1963#endif
1964}
1965
1966/*
1967 * "byteidx()" function
1968 */
1969 static void
1970f_byteidx(typval_T *argvars, typval_T *rettv)
1971{
1972 byteidx(argvars, rettv, FALSE);
1973}
1974
1975/*
1976 * "byteidxcomp()" function
1977 */
1978 static void
1979f_byteidxcomp(typval_T *argvars, typval_T *rettv)
1980{
1981 byteidx(argvars, rettv, TRUE);
1982}
1983
1984/*
1985 * "call(func, arglist [, dict])" function
1986 */
1987 static void
1988f_call(typval_T *argvars, typval_T *rettv)
1989{
1990 char_u *func;
1991 partial_T *partial = NULL;
1992 dict_T *selfdict = NULL;
1993
1994 if (argvars[1].v_type != VAR_LIST)
1995 {
1996 EMSG(_(e_listreq));
1997 return;
1998 }
1999 if (argvars[1].vval.v_list == NULL)
2000 return;
2001
2002 if (argvars[0].v_type == VAR_FUNC)
2003 func = argvars[0].vval.v_string;
2004 else if (argvars[0].v_type == VAR_PARTIAL)
2005 {
2006 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002007 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002008 }
2009 else
2010 func = get_tv_string(&argvars[0]);
2011 if (*func == NUL)
2012 return; /* type error or empty name */
2013
2014 if (argvars[2].v_type != VAR_UNKNOWN)
2015 {
2016 if (argvars[2].v_type != VAR_DICT)
2017 {
2018 EMSG(_(e_dictreq));
2019 return;
2020 }
2021 selfdict = argvars[2].vval.v_dict;
2022 }
2023
2024 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2025}
2026
2027#ifdef FEAT_FLOAT
2028/*
2029 * "ceil({float})" function
2030 */
2031 static void
2032f_ceil(typval_T *argvars, typval_T *rettv)
2033{
2034 float_T f = 0.0;
2035
2036 rettv->v_type = VAR_FLOAT;
2037 if (get_float_arg(argvars, &f) == OK)
2038 rettv->vval.v_float = ceil(f);
2039 else
2040 rettv->vval.v_float = 0.0;
2041}
2042#endif
2043
2044#ifdef FEAT_JOB_CHANNEL
2045/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002046 * "ch_canread()" function
2047 */
2048 static void
2049f_ch_canread(typval_T *argvars, typval_T *rettv)
2050{
Bram Moolenaar958dc692016-12-01 15:34:12 +01002051 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002052
2053 rettv->vval.v_number = 0;
2054 if (channel != NULL)
2055 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
2056 || channel_has_readahead(channel, PART_OUT)
2057 || channel_has_readahead(channel, PART_ERR);
2058}
2059
2060/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002061 * "ch_close()" function
2062 */
2063 static void
2064f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
2065{
2066 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2067
2068 if (channel != NULL)
2069 {
2070 channel_close(channel, FALSE);
2071 channel_clear(channel);
2072 }
2073}
2074
2075/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002076 * "ch_close()" function
2077 */
2078 static void
2079f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
2080{
2081 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2082
2083 if (channel != NULL)
2084 channel_close_in(channel);
2085}
2086
2087/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002088 * "ch_getbufnr()" function
2089 */
2090 static void
2091f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
2092{
2093 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2094
2095 rettv->vval.v_number = -1;
2096 if (channel != NULL)
2097 {
2098 char_u *what = get_tv_string(&argvars[1]);
2099 int part;
2100
2101 if (STRCMP(what, "err") == 0)
2102 part = PART_ERR;
2103 else if (STRCMP(what, "out") == 0)
2104 part = PART_OUT;
2105 else if (STRCMP(what, "in") == 0)
2106 part = PART_IN;
2107 else
2108 part = PART_SOCK;
2109 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
2110 rettv->vval.v_number =
2111 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
2112 }
2113}
2114
2115/*
2116 * "ch_getjob()" function
2117 */
2118 static void
2119f_ch_getjob(typval_T *argvars, typval_T *rettv)
2120{
2121 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2122
2123 if (channel != NULL)
2124 {
2125 rettv->v_type = VAR_JOB;
2126 rettv->vval.v_job = channel->ch_job;
2127 if (channel->ch_job != NULL)
2128 ++channel->ch_job->jv_refcount;
2129 }
2130}
2131
2132/*
2133 * "ch_info()" function
2134 */
2135 static void
2136f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
2137{
2138 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2139
2140 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
2141 channel_info(channel, rettv->vval.v_dict);
2142}
2143
2144/*
2145 * "ch_log()" function
2146 */
2147 static void
2148f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
2149{
2150 char_u *msg = get_tv_string(&argvars[0]);
2151 channel_T *channel = NULL;
2152
2153 if (argvars[1].v_type != VAR_UNKNOWN)
2154 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
2155
Bram Moolenaard5359b22018-04-05 22:44:39 +02002156 ch_log(channel, "%s", msg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002157}
2158
2159/*
2160 * "ch_logfile()" function
2161 */
2162 static void
2163f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
2164{
2165 char_u *fname;
2166 char_u *opt = (char_u *)"";
2167 char_u buf[NUMBUFLEN];
2168
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02002169 /* Don't open a file in restricted mode. */
2170 if (check_restricted() || check_secure())
2171 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002172 fname = get_tv_string(&argvars[0]);
2173 if (argvars[1].v_type == VAR_STRING)
2174 opt = get_tv_string_buf(&argvars[1], buf);
2175 ch_logfile(fname, opt);
2176}
2177
2178/*
2179 * "ch_open()" function
2180 */
2181 static void
2182f_ch_open(typval_T *argvars, typval_T *rettv)
2183{
2184 rettv->v_type = VAR_CHANNEL;
2185 if (check_restricted() || check_secure())
2186 return;
2187 rettv->vval.v_channel = channel_open_func(argvars);
2188}
2189
2190/*
2191 * "ch_read()" function
2192 */
2193 static void
2194f_ch_read(typval_T *argvars, typval_T *rettv)
2195{
2196 common_channel_read(argvars, rettv, FALSE);
2197}
2198
2199/*
2200 * "ch_readraw()" function
2201 */
2202 static void
2203f_ch_readraw(typval_T *argvars, typval_T *rettv)
2204{
2205 common_channel_read(argvars, rettv, TRUE);
2206}
2207
2208/*
2209 * "ch_evalexpr()" function
2210 */
2211 static void
2212f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2213{
2214 ch_expr_common(argvars, rettv, TRUE);
2215}
2216
2217/*
2218 * "ch_sendexpr()" function
2219 */
2220 static void
2221f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2222{
2223 ch_expr_common(argvars, rettv, FALSE);
2224}
2225
2226/*
2227 * "ch_evalraw()" function
2228 */
2229 static void
2230f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2231{
2232 ch_raw_common(argvars, rettv, TRUE);
2233}
2234
2235/*
2236 * "ch_sendraw()" function
2237 */
2238 static void
2239f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2240{
2241 ch_raw_common(argvars, rettv, FALSE);
2242}
2243
2244/*
2245 * "ch_setoptions()" function
2246 */
2247 static void
2248f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2249{
2250 channel_T *channel;
2251 jobopt_T opt;
2252
2253 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2254 if (channel == NULL)
2255 return;
2256 clear_job_options(&opt);
2257 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002258 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002259 channel_set_options(channel, &opt);
2260 free_job_options(&opt);
2261}
2262
2263/*
2264 * "ch_status()" function
2265 */
2266 static void
2267f_ch_status(typval_T *argvars, typval_T *rettv)
2268{
2269 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002270 jobopt_T opt;
2271 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002272
2273 /* return an empty string by default */
2274 rettv->v_type = VAR_STRING;
2275 rettv->vval.v_string = NULL;
2276
2277 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002278
2279 if (argvars[1].v_type != VAR_UNKNOWN)
2280 {
2281 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002282 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002283 && (opt.jo_set & JO_PART))
2284 part = opt.jo_part;
2285 }
2286
2287 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002288}
2289#endif
2290
2291/*
2292 * "changenr()" function
2293 */
2294 static void
2295f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2296{
2297 rettv->vval.v_number = curbuf->b_u_seq_cur;
2298}
2299
2300/*
2301 * "char2nr(string)" function
2302 */
2303 static void
2304f_char2nr(typval_T *argvars, typval_T *rettv)
2305{
2306#ifdef FEAT_MBYTE
2307 if (has_mbyte)
2308 {
2309 int utf8 = 0;
2310
2311 if (argvars[1].v_type != VAR_UNKNOWN)
2312 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
2313
2314 if (utf8)
2315 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
2316 else
2317 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
2318 }
2319 else
2320#endif
2321 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
2322}
2323
2324/*
2325 * "cindent(lnum)" function
2326 */
2327 static void
2328f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2329{
2330#ifdef FEAT_CINDENT
2331 pos_T pos;
2332 linenr_T lnum;
2333
2334 pos = curwin->w_cursor;
2335 lnum = get_tv_lnum(argvars);
2336 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2337 {
2338 curwin->w_cursor.lnum = lnum;
2339 rettv->vval.v_number = get_c_indent();
2340 curwin->w_cursor = pos;
2341 }
2342 else
2343#endif
2344 rettv->vval.v_number = -1;
2345}
2346
2347/*
2348 * "clearmatches()" function
2349 */
2350 static void
2351f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2352{
2353#ifdef FEAT_SEARCH_EXTRA
2354 clear_matches(curwin);
2355#endif
2356}
2357
2358/*
2359 * "col(string)" function
2360 */
2361 static void
2362f_col(typval_T *argvars, typval_T *rettv)
2363{
2364 colnr_T col = 0;
2365 pos_T *fp;
2366 int fnum = curbuf->b_fnum;
2367
2368 fp = var2fpos(&argvars[0], FALSE, &fnum);
2369 if (fp != NULL && fnum == curbuf->b_fnum)
2370 {
2371 if (fp->col == MAXCOL)
2372 {
2373 /* '> can be MAXCOL, get the length of the line then */
2374 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2375 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2376 else
2377 col = MAXCOL;
2378 }
2379 else
2380 {
2381 col = fp->col + 1;
2382#ifdef FEAT_VIRTUALEDIT
2383 /* col(".") when the cursor is on the NUL at the end of the line
2384 * because of "coladd" can be seen as an extra column. */
2385 if (virtual_active() && fp == &curwin->w_cursor)
2386 {
2387 char_u *p = ml_get_cursor();
2388
2389 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2390 curwin->w_virtcol - curwin->w_cursor.coladd))
2391 {
2392# ifdef FEAT_MBYTE
2393 int l;
2394
2395 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2396 col += l;
2397# else
2398 if (*p != NUL && p[1] == NUL)
2399 ++col;
2400# endif
2401 }
2402 }
2403#endif
2404 }
2405 }
2406 rettv->vval.v_number = col;
2407}
2408
2409#if defined(FEAT_INS_EXPAND)
2410/*
2411 * "complete()" function
2412 */
2413 static void
2414f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2415{
2416 int startcol;
2417
2418 if ((State & INSERT) == 0)
2419 {
2420 EMSG(_("E785: complete() can only be used in Insert mode"));
2421 return;
2422 }
2423
2424 /* Check for undo allowed here, because if something was already inserted
2425 * the line was already saved for undo and this check isn't done. */
2426 if (!undo_allowed())
2427 return;
2428
2429 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2430 {
2431 EMSG(_(e_invarg));
2432 return;
2433 }
2434
2435 startcol = (int)get_tv_number_chk(&argvars[0], NULL);
2436 if (startcol <= 0)
2437 return;
2438
2439 set_completion(startcol - 1, argvars[1].vval.v_list);
2440}
2441
2442/*
2443 * "complete_add()" function
2444 */
2445 static void
2446f_complete_add(typval_T *argvars, typval_T *rettv)
2447{
2448 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2449}
2450
2451/*
2452 * "complete_check()" function
2453 */
2454 static void
2455f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2456{
2457 int saved = RedrawingDisabled;
2458
2459 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002460 ins_compl_check_keys(0, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002461 rettv->vval.v_number = compl_interrupted;
2462 RedrawingDisabled = saved;
2463}
2464#endif
2465
2466/*
2467 * "confirm(message, buttons[, default [, type]])" function
2468 */
2469 static void
2470f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2471{
2472#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2473 char_u *message;
2474 char_u *buttons = NULL;
2475 char_u buf[NUMBUFLEN];
2476 char_u buf2[NUMBUFLEN];
2477 int def = 1;
2478 int type = VIM_GENERIC;
2479 char_u *typestr;
2480 int error = FALSE;
2481
2482 message = get_tv_string_chk(&argvars[0]);
2483 if (message == NULL)
2484 error = TRUE;
2485 if (argvars[1].v_type != VAR_UNKNOWN)
2486 {
2487 buttons = get_tv_string_buf_chk(&argvars[1], buf);
2488 if (buttons == NULL)
2489 error = TRUE;
2490 if (argvars[2].v_type != VAR_UNKNOWN)
2491 {
2492 def = (int)get_tv_number_chk(&argvars[2], &error);
2493 if (argvars[3].v_type != VAR_UNKNOWN)
2494 {
2495 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
2496 if (typestr == NULL)
2497 error = TRUE;
2498 else
2499 {
2500 switch (TOUPPER_ASC(*typestr))
2501 {
2502 case 'E': type = VIM_ERROR; break;
2503 case 'Q': type = VIM_QUESTION; break;
2504 case 'I': type = VIM_INFO; break;
2505 case 'W': type = VIM_WARNING; break;
2506 case 'G': type = VIM_GENERIC; break;
2507 }
2508 }
2509 }
2510 }
2511 }
2512
2513 if (buttons == NULL || *buttons == NUL)
2514 buttons = (char_u *)_("&Ok");
2515
2516 if (!error)
2517 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2518 def, NULL, FALSE);
2519#endif
2520}
2521
2522/*
2523 * "copy()" function
2524 */
2525 static void
2526f_copy(typval_T *argvars, typval_T *rettv)
2527{
2528 item_copy(&argvars[0], rettv, FALSE, 0);
2529}
2530
2531#ifdef FEAT_FLOAT
2532/*
2533 * "cos()" function
2534 */
2535 static void
2536f_cos(typval_T *argvars, typval_T *rettv)
2537{
2538 float_T f = 0.0;
2539
2540 rettv->v_type = VAR_FLOAT;
2541 if (get_float_arg(argvars, &f) == OK)
2542 rettv->vval.v_float = cos(f);
2543 else
2544 rettv->vval.v_float = 0.0;
2545}
2546
2547/*
2548 * "cosh()" function
2549 */
2550 static void
2551f_cosh(typval_T *argvars, typval_T *rettv)
2552{
2553 float_T f = 0.0;
2554
2555 rettv->v_type = VAR_FLOAT;
2556 if (get_float_arg(argvars, &f) == OK)
2557 rettv->vval.v_float = cosh(f);
2558 else
2559 rettv->vval.v_float = 0.0;
2560}
2561#endif
2562
2563/*
2564 * "count()" function
2565 */
2566 static void
2567f_count(typval_T *argvars, typval_T *rettv)
2568{
2569 long n = 0;
2570 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002571 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002572
Bram Moolenaar9966b212017-07-28 16:46:57 +02002573 if (argvars[2].v_type != VAR_UNKNOWN)
2574 ic = (int)get_tv_number_chk(&argvars[2], &error);
2575
2576 if (argvars[0].v_type == VAR_STRING)
2577 {
2578 char_u *expr = get_tv_string_chk(&argvars[1]);
2579 char_u *p = argvars[0].vval.v_string;
2580 char_u *next;
2581
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002582 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002583 {
2584 if (ic)
2585 {
2586 size_t len = STRLEN(expr);
2587
2588 while (*p != NUL)
2589 {
2590 if (MB_STRNICMP(p, expr, len) == 0)
2591 {
2592 ++n;
2593 p += len;
2594 }
2595 else
2596 MB_PTR_ADV(p);
2597 }
2598 }
2599 else
2600 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2601 != NULL)
2602 {
2603 ++n;
2604 p = next + STRLEN(expr);
2605 }
2606 }
2607
2608 }
2609 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002610 {
2611 listitem_T *li;
2612 list_T *l;
2613 long idx;
2614
2615 if ((l = argvars[0].vval.v_list) != NULL)
2616 {
2617 li = l->lv_first;
2618 if (argvars[2].v_type != VAR_UNKNOWN)
2619 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002620 if (argvars[3].v_type != VAR_UNKNOWN)
2621 {
2622 idx = (long)get_tv_number_chk(&argvars[3], &error);
2623 if (!error)
2624 {
2625 li = list_find(l, idx);
2626 if (li == NULL)
2627 EMSGN(_(e_listidx), idx);
2628 }
2629 }
2630 if (error)
2631 li = NULL;
2632 }
2633
2634 for ( ; li != NULL; li = li->li_next)
2635 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2636 ++n;
2637 }
2638 }
2639 else if (argvars[0].v_type == VAR_DICT)
2640 {
2641 int todo;
2642 dict_T *d;
2643 hashitem_T *hi;
2644
2645 if ((d = argvars[0].vval.v_dict) != NULL)
2646 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002647 if (argvars[2].v_type != VAR_UNKNOWN)
2648 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002649 if (argvars[3].v_type != VAR_UNKNOWN)
2650 EMSG(_(e_invarg));
2651 }
2652
2653 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2654 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2655 {
2656 if (!HASHITEM_EMPTY(hi))
2657 {
2658 --todo;
2659 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2660 ++n;
2661 }
2662 }
2663 }
2664 }
2665 else
2666 EMSG2(_(e_listdictarg), "count()");
2667 rettv->vval.v_number = n;
2668}
2669
2670/*
2671 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2672 *
2673 * Checks the existence of a cscope connection.
2674 */
2675 static void
2676f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2677{
2678#ifdef FEAT_CSCOPE
2679 int num = 0;
2680 char_u *dbpath = NULL;
2681 char_u *prepend = NULL;
2682 char_u buf[NUMBUFLEN];
2683
2684 if (argvars[0].v_type != VAR_UNKNOWN
2685 && argvars[1].v_type != VAR_UNKNOWN)
2686 {
2687 num = (int)get_tv_number(&argvars[0]);
2688 dbpath = get_tv_string(&argvars[1]);
2689 if (argvars[2].v_type != VAR_UNKNOWN)
2690 prepend = get_tv_string_buf(&argvars[2], buf);
2691 }
2692
2693 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2694#endif
2695}
2696
2697/*
2698 * "cursor(lnum, col)" function, or
2699 * "cursor(list)"
2700 *
2701 * Moves the cursor to the specified line and column.
2702 * Returns 0 when the position could be set, -1 otherwise.
2703 */
2704 static void
2705f_cursor(typval_T *argvars, typval_T *rettv)
2706{
2707 long line, col;
2708#ifdef FEAT_VIRTUALEDIT
2709 long coladd = 0;
2710#endif
2711 int set_curswant = TRUE;
2712
2713 rettv->vval.v_number = -1;
2714 if (argvars[1].v_type == VAR_UNKNOWN)
2715 {
2716 pos_T pos;
2717 colnr_T curswant = -1;
2718
2719 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2720 {
2721 EMSG(_(e_invarg));
2722 return;
2723 }
2724 line = pos.lnum;
2725 col = pos.col;
2726#ifdef FEAT_VIRTUALEDIT
2727 coladd = pos.coladd;
2728#endif
2729 if (curswant >= 0)
2730 {
2731 curwin->w_curswant = curswant - 1;
2732 set_curswant = FALSE;
2733 }
2734 }
2735 else
2736 {
2737 line = get_tv_lnum(argvars);
2738 col = (long)get_tv_number_chk(&argvars[1], NULL);
2739#ifdef FEAT_VIRTUALEDIT
2740 if (argvars[2].v_type != VAR_UNKNOWN)
2741 coladd = (long)get_tv_number_chk(&argvars[2], NULL);
2742#endif
2743 }
2744 if (line < 0 || col < 0
2745#ifdef FEAT_VIRTUALEDIT
2746 || coladd < 0
2747#endif
2748 )
2749 return; /* type error; errmsg already given */
2750 if (line > 0)
2751 curwin->w_cursor.lnum = line;
2752 if (col > 0)
2753 curwin->w_cursor.col = col - 1;
2754#ifdef FEAT_VIRTUALEDIT
2755 curwin->w_cursor.coladd = coladd;
2756#endif
2757
2758 /* Make sure the cursor is in a valid position. */
2759 check_cursor();
2760#ifdef FEAT_MBYTE
2761 /* Correct cursor for multi-byte character. */
2762 if (has_mbyte)
2763 mb_adjust_cursor();
2764#endif
2765
2766 curwin->w_set_curswant = set_curswant;
2767 rettv->vval.v_number = 0;
2768}
2769
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002770#ifdef WIN3264
2771/*
2772 * "debugbreak()" function
2773 */
2774 static void
2775f_debugbreak(typval_T *argvars, typval_T *rettv)
2776{
2777 int pid;
2778
2779 rettv->vval.v_number = FAIL;
2780 pid = (int)get_tv_number(&argvars[0]);
2781 if (pid == 0)
2782 EMSG(_(e_invarg));
2783 else
2784 {
2785 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2786
2787 if (hProcess != NULL)
2788 {
2789 DebugBreakProcess(hProcess);
2790 CloseHandle(hProcess);
2791 rettv->vval.v_number = OK;
2792 }
2793 }
2794}
2795#endif
2796
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002797/*
2798 * "deepcopy()" function
2799 */
2800 static void
2801f_deepcopy(typval_T *argvars, typval_T *rettv)
2802{
2803 int noref = 0;
2804 int copyID;
2805
2806 if (argvars[1].v_type != VAR_UNKNOWN)
2807 noref = (int)get_tv_number_chk(&argvars[1], NULL);
2808 if (noref < 0 || noref > 1)
2809 EMSG(_(e_invarg));
2810 else
2811 {
2812 copyID = get_copyID();
2813 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2814 }
2815}
2816
2817/*
2818 * "delete()" function
2819 */
2820 static void
2821f_delete(typval_T *argvars, typval_T *rettv)
2822{
2823 char_u nbuf[NUMBUFLEN];
2824 char_u *name;
2825 char_u *flags;
2826
2827 rettv->vval.v_number = -1;
2828 if (check_restricted() || check_secure())
2829 return;
2830
2831 name = get_tv_string(&argvars[0]);
2832 if (name == NULL || *name == NUL)
2833 {
2834 EMSG(_(e_invarg));
2835 return;
2836 }
2837
2838 if (argvars[1].v_type != VAR_UNKNOWN)
2839 flags = get_tv_string_buf(&argvars[1], nbuf);
2840 else
2841 flags = (char_u *)"";
2842
2843 if (*flags == NUL)
2844 /* delete a file */
2845 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2846 else if (STRCMP(flags, "d") == 0)
2847 /* delete an empty directory */
2848 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2849 else if (STRCMP(flags, "rf") == 0)
2850 /* delete a directory recursively */
2851 rettv->vval.v_number = delete_recursive(name);
2852 else
2853 EMSG2(_(e_invexpr2), flags);
2854}
2855
2856/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02002857 * "deletebufline()" function
2858 */
2859 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02002860f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02002861{
2862 buf_T *buf;
2863 linenr_T first, last;
2864 linenr_T lnum;
2865 long count;
2866 int is_curbuf;
2867 buf_T *curbuf_save = NULL;
2868 win_T *curwin_save = NULL;
2869 tabpage_T *tp;
2870 win_T *wp;
2871
2872 buf = get_buf_tv(&argvars[0], FALSE);
2873 if (buf == NULL)
2874 {
2875 rettv->vval.v_number = 1; /* FAIL */
2876 return;
2877 }
2878 is_curbuf = buf == curbuf;
2879
2880 first = get_tv_lnum_buf(&argvars[1], buf);
2881 if (argvars[2].v_type != VAR_UNKNOWN)
2882 last = get_tv_lnum_buf(&argvars[2], buf);
2883 else
2884 last = first;
2885
2886 if (buf->b_ml.ml_mfp == NULL || first < 1
2887 || first > buf->b_ml.ml_line_count || last < first)
2888 {
2889 rettv->vval.v_number = 1; /* FAIL */
2890 return;
2891 }
2892
2893 if (!is_curbuf)
2894 {
2895 curbuf_save = curbuf;
2896 curwin_save = curwin;
2897 curbuf = buf;
2898 find_win_for_curbuf();
2899 }
2900 if (last > curbuf->b_ml.ml_line_count)
2901 last = curbuf->b_ml.ml_line_count;
2902 count = last - first + 1;
2903
2904 // When coming here from Insert mode, sync undo, so that this can be
2905 // undone separately from what was previously inserted.
2906 if (u_sync_once == 2)
2907 {
2908 u_sync_once = 1; // notify that u_sync() was called
2909 u_sync(TRUE);
2910 }
2911
2912 if (u_save(first - 1, last + 1) == FAIL)
2913 {
2914 rettv->vval.v_number = 1; /* FAIL */
2915 return;
2916 }
2917
2918 for (lnum = first; lnum <= last; ++lnum)
2919 ml_delete(first, TRUE);
2920
2921 FOR_ALL_TAB_WINDOWS(tp, wp)
2922 if (wp->w_buffer == buf)
2923 {
2924 if (wp->w_cursor.lnum > last)
2925 wp->w_cursor.lnum -= count;
2926 else if (wp->w_cursor.lnum> first)
2927 wp->w_cursor.lnum = first;
2928 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
2929 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
2930 }
2931 check_cursor_col();
2932 deleted_lines_mark(first, count);
2933
2934 if (!is_curbuf)
2935 {
2936 curbuf = curbuf_save;
2937 curwin = curwin_save;
2938 }
2939}
2940
2941/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002942 * "did_filetype()" function
2943 */
2944 static void
2945f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2946{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002947 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002948}
2949
2950/*
2951 * "diff_filler()" function
2952 */
2953 static void
2954f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2955{
2956#ifdef FEAT_DIFF
2957 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
2958#endif
2959}
2960
2961/*
2962 * "diff_hlID()" function
2963 */
2964 static void
2965f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2966{
2967#ifdef FEAT_DIFF
2968 linenr_T lnum = get_tv_lnum(argvars);
2969 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01002970 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002971 static int fnum = 0;
2972 static int change_start = 0;
2973 static int change_end = 0;
2974 static hlf_T hlID = (hlf_T)0;
2975 int filler_lines;
2976 int col;
2977
2978 if (lnum < 0) /* ignore type error in {lnum} arg */
2979 lnum = 0;
2980 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002981 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002982 || fnum != curbuf->b_fnum)
2983 {
2984 /* New line, buffer, change: need to get the values. */
2985 filler_lines = diff_check(curwin, lnum);
2986 if (filler_lines < 0)
2987 {
2988 if (filler_lines == -1)
2989 {
2990 change_start = MAXCOL;
2991 change_end = -1;
2992 if (diff_find_change(curwin, lnum, &change_start, &change_end))
2993 hlID = HLF_ADD; /* added line */
2994 else
2995 hlID = HLF_CHD; /* changed line */
2996 }
2997 else
2998 hlID = HLF_ADD; /* added line */
2999 }
3000 else
3001 hlID = (hlf_T)0;
3002 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003003 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003004 fnum = curbuf->b_fnum;
3005 }
3006
3007 if (hlID == HLF_CHD || hlID == HLF_TXD)
3008 {
3009 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
3010 if (col >= change_start && col <= change_end)
3011 hlID = HLF_TXD; /* changed text */
3012 else
3013 hlID = HLF_CHD; /* changed line */
3014 }
3015 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
3016#endif
3017}
3018
3019/*
3020 * "empty({expr})" function
3021 */
3022 static void
3023f_empty(typval_T *argvars, typval_T *rettv)
3024{
3025 int n = FALSE;
3026
3027 switch (argvars[0].v_type)
3028 {
3029 case VAR_STRING:
3030 case VAR_FUNC:
3031 n = argvars[0].vval.v_string == NULL
3032 || *argvars[0].vval.v_string == NUL;
3033 break;
3034 case VAR_PARTIAL:
3035 n = FALSE;
3036 break;
3037 case VAR_NUMBER:
3038 n = argvars[0].vval.v_number == 0;
3039 break;
3040 case VAR_FLOAT:
3041#ifdef FEAT_FLOAT
3042 n = argvars[0].vval.v_float == 0.0;
3043 break;
3044#endif
3045 case VAR_LIST:
3046 n = argvars[0].vval.v_list == NULL
3047 || argvars[0].vval.v_list->lv_first == NULL;
3048 break;
3049 case VAR_DICT:
3050 n = argvars[0].vval.v_dict == NULL
3051 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
3052 break;
3053 case VAR_SPECIAL:
3054 n = argvars[0].vval.v_number != VVAL_TRUE;
3055 break;
3056
3057 case VAR_JOB:
3058#ifdef FEAT_JOB_CHANNEL
3059 n = argvars[0].vval.v_job == NULL
3060 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
3061 break;
3062#endif
3063 case VAR_CHANNEL:
3064#ifdef FEAT_JOB_CHANNEL
3065 n = argvars[0].vval.v_channel == NULL
3066 || !channel_is_open(argvars[0].vval.v_channel);
3067 break;
3068#endif
3069 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01003070 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003071 n = TRUE;
3072 break;
3073 }
3074
3075 rettv->vval.v_number = n;
3076}
3077
3078/*
3079 * "escape({string}, {chars})" function
3080 */
3081 static void
3082f_escape(typval_T *argvars, typval_T *rettv)
3083{
3084 char_u buf[NUMBUFLEN];
3085
3086 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
3087 get_tv_string_buf(&argvars[1], buf));
3088 rettv->v_type = VAR_STRING;
3089}
3090
3091/*
3092 * "eval()" function
3093 */
3094 static void
3095f_eval(typval_T *argvars, typval_T *rettv)
3096{
3097 char_u *s, *p;
3098
3099 s = get_tv_string_chk(&argvars[0]);
3100 if (s != NULL)
3101 s = skipwhite(s);
3102
3103 p = s;
3104 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3105 {
3106 if (p != NULL && !aborting())
3107 EMSG2(_(e_invexpr2), p);
3108 need_clr_eos = FALSE;
3109 rettv->v_type = VAR_NUMBER;
3110 rettv->vval.v_number = 0;
3111 }
3112 else if (*s != NUL)
3113 EMSG(_(e_trailing));
3114}
3115
3116/*
3117 * "eventhandler()" function
3118 */
3119 static void
3120f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3121{
3122 rettv->vval.v_number = vgetc_busy;
3123}
3124
3125/*
3126 * "executable()" function
3127 */
3128 static void
3129f_executable(typval_T *argvars, typval_T *rettv)
3130{
3131 char_u *name = get_tv_string(&argvars[0]);
3132
3133 /* Check in $PATH and also check directly if there is a directory name. */
3134 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
3135 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
3136}
3137
3138static garray_T redir_execute_ga;
3139
3140/*
3141 * Append "value[value_len]" to the execute() output.
3142 */
3143 void
3144execute_redir_str(char_u *value, int value_len)
3145{
3146 int len;
3147
3148 if (value_len == -1)
3149 len = (int)STRLEN(value); /* Append the entire string */
3150 else
3151 len = value_len; /* Append only "value_len" characters */
3152 if (ga_grow(&redir_execute_ga, len) == OK)
3153 {
3154 mch_memmove((char *)redir_execute_ga.ga_data
3155 + redir_execute_ga.ga_len, value, len);
3156 redir_execute_ga.ga_len += len;
3157 }
3158}
3159
3160/*
3161 * Get next line from a list.
3162 * Called by do_cmdline() to get the next line.
3163 * Returns allocated string, or NULL for end of function.
3164 */
3165
3166 static char_u *
3167get_list_line(
3168 int c UNUSED,
3169 void *cookie,
3170 int indent UNUSED)
3171{
3172 listitem_T **p = (listitem_T **)cookie;
3173 listitem_T *item = *p;
3174 char_u buf[NUMBUFLEN];
3175 char_u *s;
3176
3177 if (item == NULL)
3178 return NULL;
3179 s = get_tv_string_buf_chk(&item->li_tv, buf);
3180 *p = item->li_next;
3181 return s == NULL ? NULL : vim_strsave(s);
3182}
3183
3184/*
3185 * "execute()" function
3186 */
3187 static void
3188f_execute(typval_T *argvars, typval_T *rettv)
3189{
3190 char_u *cmd = NULL;
3191 list_T *list = NULL;
3192 int save_msg_silent = msg_silent;
3193 int save_emsg_silent = emsg_silent;
3194 int save_emsg_noredir = emsg_noredir;
3195 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003196 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003197 garray_T save_ga;
3198
3199 rettv->vval.v_string = NULL;
3200 rettv->v_type = VAR_STRING;
3201
3202 if (argvars[0].v_type == VAR_LIST)
3203 {
3204 list = argvars[0].vval.v_list;
3205 if (list == NULL || list->lv_first == NULL)
3206 /* empty list, no commands, empty output */
3207 return;
3208 ++list->lv_refcount;
3209 }
3210 else
3211 {
3212 cmd = get_tv_string_chk(&argvars[0]);
3213 if (cmd == NULL)
3214 return;
3215 }
3216
3217 if (argvars[1].v_type != VAR_UNKNOWN)
3218 {
3219 char_u buf[NUMBUFLEN];
3220 char_u *s = get_tv_string_buf_chk(&argvars[1], buf);
3221
3222 if (s == NULL)
3223 return;
3224 if (STRNCMP(s, "silent", 6) == 0)
3225 ++msg_silent;
3226 if (STRCMP(s, "silent!") == 0)
3227 {
3228 emsg_silent = TRUE;
3229 emsg_noredir = TRUE;
3230 }
3231 }
3232 else
3233 ++msg_silent;
3234
3235 if (redir_execute)
3236 save_ga = redir_execute_ga;
3237 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3238 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003239 redir_off = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003240
3241 if (cmd != NULL)
3242 do_cmdline_cmd(cmd);
3243 else
3244 {
3245 listitem_T *item = list->lv_first;
3246
3247 do_cmdline(NULL, get_list_line, (void *)&item,
3248 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3249 --list->lv_refcount;
3250 }
3251
Bram Moolenaard297f352017-01-29 20:31:21 +01003252 /* Need to append a NUL to the result. */
3253 if (ga_grow(&redir_execute_ga, 1) == OK)
3254 {
3255 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3256 rettv->vval.v_string = redir_execute_ga.ga_data;
3257 }
3258 else
3259 {
3260 ga_clear(&redir_execute_ga);
3261 rettv->vval.v_string = NULL;
3262 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003263 msg_silent = save_msg_silent;
3264 emsg_silent = save_emsg_silent;
3265 emsg_noredir = save_emsg_noredir;
3266
3267 redir_execute = save_redir_execute;
3268 if (redir_execute)
3269 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003270 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003271
3272 /* "silent reg" or "silent echo x" leaves msg_col somewhere in the
3273 * line. Put it back in the first column. */
3274 msg_col = 0;
3275}
3276
3277/*
3278 * "exepath()" function
3279 */
3280 static void
3281f_exepath(typval_T *argvars, typval_T *rettv)
3282{
3283 char_u *p = NULL;
3284
3285 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
3286 rettv->v_type = VAR_STRING;
3287 rettv->vval.v_string = p;
3288}
3289
3290/*
3291 * "exists()" function
3292 */
3293 static void
3294f_exists(typval_T *argvars, typval_T *rettv)
3295{
3296 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003297 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003298
3299 p = get_tv_string(&argvars[0]);
3300 if (*p == '$') /* environment variable */
3301 {
3302 /* first try "normal" environment variables (fast) */
3303 if (mch_getenv(p + 1) != NULL)
3304 n = TRUE;
3305 else
3306 {
3307 /* try expanding things like $VIM and ${HOME} */
3308 p = expand_env_save(p);
3309 if (p != NULL && *p != '$')
3310 n = TRUE;
3311 vim_free(p);
3312 }
3313 }
3314 else if (*p == '&' || *p == '+') /* option */
3315 {
3316 n = (get_option_tv(&p, NULL, TRUE) == OK);
3317 if (*skipwhite(p) != NUL)
3318 n = FALSE; /* trailing garbage */
3319 }
3320 else if (*p == '*') /* internal or user defined function */
3321 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003322 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003323 }
3324 else if (*p == ':')
3325 {
3326 n = cmd_exists(p + 1);
3327 }
3328 else if (*p == '#')
3329 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003330 if (p[1] == '#')
3331 n = autocmd_supported(p + 2);
3332 else
3333 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003334 }
3335 else /* internal variable */
3336 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003337 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003338 }
3339
3340 rettv->vval.v_number = n;
3341}
3342
3343#ifdef FEAT_FLOAT
3344/*
3345 * "exp()" function
3346 */
3347 static void
3348f_exp(typval_T *argvars, typval_T *rettv)
3349{
3350 float_T f = 0.0;
3351
3352 rettv->v_type = VAR_FLOAT;
3353 if (get_float_arg(argvars, &f) == OK)
3354 rettv->vval.v_float = exp(f);
3355 else
3356 rettv->vval.v_float = 0.0;
3357}
3358#endif
3359
3360/*
3361 * "expand()" function
3362 */
3363 static void
3364f_expand(typval_T *argvars, typval_T *rettv)
3365{
3366 char_u *s;
3367 int len;
3368 char_u *errormsg;
3369 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3370 expand_T xpc;
3371 int error = FALSE;
3372 char_u *result;
3373
3374 rettv->v_type = VAR_STRING;
3375 if (argvars[1].v_type != VAR_UNKNOWN
3376 && argvars[2].v_type != VAR_UNKNOWN
3377 && get_tv_number_chk(&argvars[2], &error)
3378 && !error)
3379 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003380 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003381 }
3382
3383 s = get_tv_string(&argvars[0]);
3384 if (*s == '%' || *s == '#' || *s == '<')
3385 {
3386 ++emsg_off;
3387 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3388 --emsg_off;
3389 if (rettv->v_type == VAR_LIST)
3390 {
3391 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3392 list_append_string(rettv->vval.v_list, result, -1);
3393 else
3394 vim_free(result);
3395 }
3396 else
3397 rettv->vval.v_string = result;
3398 }
3399 else
3400 {
3401 /* When the optional second argument is non-zero, don't remove matches
3402 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3403 if (argvars[1].v_type != VAR_UNKNOWN
3404 && get_tv_number_chk(&argvars[1], &error))
3405 options |= WILD_KEEP_ALL;
3406 if (!error)
3407 {
3408 ExpandInit(&xpc);
3409 xpc.xp_context = EXPAND_FILES;
3410 if (p_wic)
3411 options += WILD_ICASE;
3412 if (rettv->v_type == VAR_STRING)
3413 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3414 options, WILD_ALL);
3415 else if (rettv_list_alloc(rettv) != FAIL)
3416 {
3417 int i;
3418
3419 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3420 for (i = 0; i < xpc.xp_numfiles; i++)
3421 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3422 ExpandCleanup(&xpc);
3423 }
3424 }
3425 else
3426 rettv->vval.v_string = NULL;
3427 }
3428}
3429
3430/*
3431 * "extend(list, list [, idx])" function
3432 * "extend(dict, dict [, action])" function
3433 */
3434 static void
3435f_extend(typval_T *argvars, typval_T *rettv)
3436{
3437 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3438
3439 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3440 {
3441 list_T *l1, *l2;
3442 listitem_T *item;
3443 long before;
3444 int error = FALSE;
3445
3446 l1 = argvars[0].vval.v_list;
3447 l2 = argvars[1].vval.v_list;
3448 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
3449 && l2 != NULL)
3450 {
3451 if (argvars[2].v_type != VAR_UNKNOWN)
3452 {
3453 before = (long)get_tv_number_chk(&argvars[2], &error);
3454 if (error)
3455 return; /* type error; errmsg already given */
3456
3457 if (before == l1->lv_len)
3458 item = NULL;
3459 else
3460 {
3461 item = list_find(l1, before);
3462 if (item == NULL)
3463 {
3464 EMSGN(_(e_listidx), before);
3465 return;
3466 }
3467 }
3468 }
3469 else
3470 item = NULL;
3471 list_extend(l1, l2, item);
3472
3473 copy_tv(&argvars[0], rettv);
3474 }
3475 }
3476 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3477 {
3478 dict_T *d1, *d2;
3479 char_u *action;
3480 int i;
3481
3482 d1 = argvars[0].vval.v_dict;
3483 d2 = argvars[1].vval.v_dict;
3484 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
3485 && d2 != NULL)
3486 {
3487 /* Check the third argument. */
3488 if (argvars[2].v_type != VAR_UNKNOWN)
3489 {
3490 static char *(av[]) = {"keep", "force", "error"};
3491
3492 action = get_tv_string_chk(&argvars[2]);
3493 if (action == NULL)
3494 return; /* type error; errmsg already given */
3495 for (i = 0; i < 3; ++i)
3496 if (STRCMP(action, av[i]) == 0)
3497 break;
3498 if (i == 3)
3499 {
3500 EMSG2(_(e_invarg2), action);
3501 return;
3502 }
3503 }
3504 else
3505 action = (char_u *)"force";
3506
3507 dict_extend(d1, d2, action);
3508
3509 copy_tv(&argvars[0], rettv);
3510 }
3511 }
3512 else
3513 EMSG2(_(e_listdictarg), "extend()");
3514}
3515
3516/*
3517 * "feedkeys()" function
3518 */
3519 static void
3520f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3521{
3522 int remap = TRUE;
3523 int insert = FALSE;
3524 char_u *keys, *flags;
3525 char_u nbuf[NUMBUFLEN];
3526 int typed = FALSE;
3527 int execute = FALSE;
3528 int dangerous = FALSE;
3529 char_u *keys_esc;
3530
3531 /* This is not allowed in the sandbox. If the commands would still be
3532 * executed in the sandbox it would be OK, but it probably happens later,
3533 * when "sandbox" is no longer set. */
3534 if (check_secure())
3535 return;
3536
3537 keys = get_tv_string(&argvars[0]);
3538
3539 if (argvars[1].v_type != VAR_UNKNOWN)
3540 {
3541 flags = get_tv_string_buf(&argvars[1], nbuf);
3542 for ( ; *flags != NUL; ++flags)
3543 {
3544 switch (*flags)
3545 {
3546 case 'n': remap = FALSE; break;
3547 case 'm': remap = TRUE; break;
3548 case 't': typed = TRUE; break;
3549 case 'i': insert = TRUE; break;
3550 case 'x': execute = TRUE; break;
3551 case '!': dangerous = TRUE; break;
3552 }
3553 }
3554 }
3555
3556 if (*keys != NUL || execute)
3557 {
3558 /* Need to escape K_SPECIAL and CSI before putting the string in the
3559 * typeahead buffer. */
3560 keys_esc = vim_strsave_escape_csi(keys);
3561 if (keys_esc != NULL)
3562 {
3563 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
3564 insert ? 0 : typebuf.tb_len, !typed, FALSE);
3565 vim_free(keys_esc);
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003566 if (vgetc_busy
3567#ifdef FEAT_TIMERS
3568 || timer_busy
3569#endif
3570 )
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003571 typebuf_was_filled = TRUE;
3572 if (execute)
3573 {
3574 int save_msg_scroll = msg_scroll;
3575
3576 /* Avoid a 1 second delay when the keys start Insert mode. */
3577 msg_scroll = FALSE;
3578
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003579 if (!dangerous)
3580 ++ex_normal_busy;
Bram Moolenaar655a82a2018-05-08 22:01:07 +02003581 exec_normal(TRUE, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003582 if (!dangerous)
3583 --ex_normal_busy;
3584
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003585 msg_scroll |= save_msg_scroll;
3586 }
3587 }
3588 }
3589}
3590
3591/*
3592 * "filereadable()" function
3593 */
3594 static void
3595f_filereadable(typval_T *argvars, typval_T *rettv)
3596{
3597 int fd;
3598 char_u *p;
3599 int n;
3600
3601#ifndef O_NONBLOCK
3602# define O_NONBLOCK 0
3603#endif
3604 p = get_tv_string(&argvars[0]);
3605 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3606 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3607 {
3608 n = TRUE;
3609 close(fd);
3610 }
3611 else
3612 n = FALSE;
3613
3614 rettv->vval.v_number = n;
3615}
3616
3617/*
3618 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3619 * rights to write into.
3620 */
3621 static void
3622f_filewritable(typval_T *argvars, typval_T *rettv)
3623{
3624 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
3625}
3626
3627 static void
3628findfilendir(
3629 typval_T *argvars UNUSED,
3630 typval_T *rettv,
3631 int find_what UNUSED)
3632{
3633#ifdef FEAT_SEARCHPATH
3634 char_u *fname;
3635 char_u *fresult = NULL;
3636 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3637 char_u *p;
3638 char_u pathbuf[NUMBUFLEN];
3639 int count = 1;
3640 int first = TRUE;
3641 int error = FALSE;
3642#endif
3643
3644 rettv->vval.v_string = NULL;
3645 rettv->v_type = VAR_STRING;
3646
3647#ifdef FEAT_SEARCHPATH
3648 fname = get_tv_string(&argvars[0]);
3649
3650 if (argvars[1].v_type != VAR_UNKNOWN)
3651 {
3652 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
3653 if (p == NULL)
3654 error = TRUE;
3655 else
3656 {
3657 if (*p != NUL)
3658 path = p;
3659
3660 if (argvars[2].v_type != VAR_UNKNOWN)
3661 count = (int)get_tv_number_chk(&argvars[2], &error);
3662 }
3663 }
3664
3665 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3666 error = TRUE;
3667
3668 if (*fname != NUL && !error)
3669 {
3670 do
3671 {
3672 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3673 vim_free(fresult);
3674 fresult = find_file_in_path_option(first ? fname : NULL,
3675 first ? (int)STRLEN(fname) : 0,
3676 0, first, path,
3677 find_what,
3678 curbuf->b_ffname,
3679 find_what == FINDFILE_DIR
3680 ? (char_u *)"" : curbuf->b_p_sua);
3681 first = FALSE;
3682
3683 if (fresult != NULL && rettv->v_type == VAR_LIST)
3684 list_append_string(rettv->vval.v_list, fresult, -1);
3685
3686 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3687 }
3688
3689 if (rettv->v_type == VAR_STRING)
3690 rettv->vval.v_string = fresult;
3691#endif
3692}
3693
3694/*
3695 * "filter()" function
3696 */
3697 static void
3698f_filter(typval_T *argvars, typval_T *rettv)
3699{
3700 filter_map(argvars, rettv, FALSE);
3701}
3702
3703/*
3704 * "finddir({fname}[, {path}[, {count}]])" function
3705 */
3706 static void
3707f_finddir(typval_T *argvars, typval_T *rettv)
3708{
3709 findfilendir(argvars, rettv, FINDFILE_DIR);
3710}
3711
3712/*
3713 * "findfile({fname}[, {path}[, {count}]])" function
3714 */
3715 static void
3716f_findfile(typval_T *argvars, typval_T *rettv)
3717{
3718 findfilendir(argvars, rettv, FINDFILE_FILE);
3719}
3720
3721#ifdef FEAT_FLOAT
3722/*
3723 * "float2nr({float})" function
3724 */
3725 static void
3726f_float2nr(typval_T *argvars, typval_T *rettv)
3727{
3728 float_T f = 0.0;
3729
3730 if (get_float_arg(argvars, &f) == OK)
3731 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003732 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003733 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003734 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003735 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003736 else
3737 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003738 }
3739}
3740
3741/*
3742 * "floor({float})" function
3743 */
3744 static void
3745f_floor(typval_T *argvars, typval_T *rettv)
3746{
3747 float_T f = 0.0;
3748
3749 rettv->v_type = VAR_FLOAT;
3750 if (get_float_arg(argvars, &f) == OK)
3751 rettv->vval.v_float = floor(f);
3752 else
3753 rettv->vval.v_float = 0.0;
3754}
3755
3756/*
3757 * "fmod()" function
3758 */
3759 static void
3760f_fmod(typval_T *argvars, typval_T *rettv)
3761{
3762 float_T fx = 0.0, fy = 0.0;
3763
3764 rettv->v_type = VAR_FLOAT;
3765 if (get_float_arg(argvars, &fx) == OK
3766 && get_float_arg(&argvars[1], &fy) == OK)
3767 rettv->vval.v_float = fmod(fx, fy);
3768 else
3769 rettv->vval.v_float = 0.0;
3770}
3771#endif
3772
3773/*
3774 * "fnameescape({string})" function
3775 */
3776 static void
3777f_fnameescape(typval_T *argvars, typval_T *rettv)
3778{
3779 rettv->vval.v_string = vim_strsave_fnameescape(
3780 get_tv_string(&argvars[0]), FALSE);
3781 rettv->v_type = VAR_STRING;
3782}
3783
3784/*
3785 * "fnamemodify({fname}, {mods})" function
3786 */
3787 static void
3788f_fnamemodify(typval_T *argvars, typval_T *rettv)
3789{
3790 char_u *fname;
3791 char_u *mods;
3792 int usedlen = 0;
3793 int len;
3794 char_u *fbuf = NULL;
3795 char_u buf[NUMBUFLEN];
3796
3797 fname = get_tv_string_chk(&argvars[0]);
3798 mods = get_tv_string_buf_chk(&argvars[1], buf);
3799 if (fname == NULL || mods == NULL)
3800 fname = NULL;
3801 else
3802 {
3803 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02003804 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003805 }
3806
3807 rettv->v_type = VAR_STRING;
3808 if (fname == NULL)
3809 rettv->vval.v_string = NULL;
3810 else
3811 rettv->vval.v_string = vim_strnsave(fname, len);
3812 vim_free(fbuf);
3813}
3814
3815static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
3816
3817/*
3818 * "foldclosed()" function
3819 */
3820 static void
3821foldclosed_both(
3822 typval_T *argvars UNUSED,
3823 typval_T *rettv,
3824 int end UNUSED)
3825{
3826#ifdef FEAT_FOLDING
3827 linenr_T lnum;
3828 linenr_T first, last;
3829
3830 lnum = get_tv_lnum(argvars);
3831 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3832 {
3833 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3834 {
3835 if (end)
3836 rettv->vval.v_number = (varnumber_T)last;
3837 else
3838 rettv->vval.v_number = (varnumber_T)first;
3839 return;
3840 }
3841 }
3842#endif
3843 rettv->vval.v_number = -1;
3844}
3845
3846/*
3847 * "foldclosed()" function
3848 */
3849 static void
3850f_foldclosed(typval_T *argvars, typval_T *rettv)
3851{
3852 foldclosed_both(argvars, rettv, FALSE);
3853}
3854
3855/*
3856 * "foldclosedend()" function
3857 */
3858 static void
3859f_foldclosedend(typval_T *argvars, typval_T *rettv)
3860{
3861 foldclosed_both(argvars, rettv, TRUE);
3862}
3863
3864/*
3865 * "foldlevel()" function
3866 */
3867 static void
3868f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3869{
3870#ifdef FEAT_FOLDING
3871 linenr_T lnum;
3872
3873 lnum = get_tv_lnum(argvars);
3874 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3875 rettv->vval.v_number = foldLevel(lnum);
3876#endif
3877}
3878
3879/*
3880 * "foldtext()" function
3881 */
3882 static void
3883f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
3884{
3885#ifdef FEAT_FOLDING
3886 linenr_T foldstart;
3887 linenr_T foldend;
3888 char_u *dashes;
3889 linenr_T lnum;
3890 char_u *s;
3891 char_u *r;
3892 int len;
3893 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003894 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003895#endif
3896
3897 rettv->v_type = VAR_STRING;
3898 rettv->vval.v_string = NULL;
3899#ifdef FEAT_FOLDING
3900 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
3901 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
3902 dashes = get_vim_var_str(VV_FOLDDASHES);
3903 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
3904 && dashes != NULL)
3905 {
3906 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02003907 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003908 if (!linewhite(lnum))
3909 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003910
3911 /* Find interesting text in this line. */
3912 s = skipwhite(ml_get(lnum));
3913 /* skip C comment-start */
3914 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
3915 {
3916 s = skipwhite(s + 2);
3917 if (*skipwhite(s) == NUL
3918 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
3919 {
3920 s = skipwhite(ml_get(lnum + 1));
3921 if (*s == '*')
3922 s = skipwhite(s + 1);
3923 }
3924 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02003925 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003926 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003927 r = alloc((unsigned)(STRLEN(txt)
3928 + STRLEN(dashes) /* for %s */
3929 + 20 /* for %3ld */
3930 + STRLEN(s))); /* concatenated */
3931 if (r != NULL)
3932 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02003933 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003934 len = (int)STRLEN(r);
3935 STRCAT(r, s);
3936 /* remove 'foldmarker' and 'commentstring' */
3937 foldtext_cleanup(r + len);
3938 rettv->vval.v_string = r;
3939 }
3940 }
3941#endif
3942}
3943
3944/*
3945 * "foldtextresult(lnum)" function
3946 */
3947 static void
3948f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
3949{
3950#ifdef FEAT_FOLDING
3951 linenr_T lnum;
3952 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003953 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003954 foldinfo_T foldinfo;
3955 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003956 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003957#endif
3958
3959 rettv->v_type = VAR_STRING;
3960 rettv->vval.v_string = NULL;
3961#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003962 if (entered)
3963 return; /* reject recursive use */
3964 entered = TRUE;
3965
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003966 lnum = get_tv_lnum(argvars);
3967 /* treat illegal types and illegal string values for {lnum} the same */
3968 if (lnum < 0)
3969 lnum = 0;
3970 fold_count = foldedCount(curwin, lnum, &foldinfo);
3971 if (fold_count > 0)
3972 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01003973 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
3974 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003975 if (text == buf)
3976 text = vim_strsave(text);
3977 rettv->vval.v_string = text;
3978 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003979
3980 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003981#endif
3982}
3983
3984/*
3985 * "foreground()" function
3986 */
3987 static void
3988f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3989{
3990#ifdef FEAT_GUI
3991 if (gui.in_use)
3992 gui_mch_set_foreground();
3993#else
3994# ifdef WIN32
3995 win32_set_foreground();
3996# endif
3997#endif
3998}
3999
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004000 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004001common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004002{
4003 char_u *s;
4004 char_u *name;
4005 int use_string = FALSE;
4006 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004007 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004008
4009 if (argvars[0].v_type == VAR_FUNC)
4010 {
4011 /* function(MyFunc, [arg], dict) */
4012 s = argvars[0].vval.v_string;
4013 }
4014 else if (argvars[0].v_type == VAR_PARTIAL
4015 && argvars[0].vval.v_partial != NULL)
4016 {
4017 /* function(dict.MyFunc, [arg]) */
4018 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004019 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004020 }
4021 else
4022 {
4023 /* function('MyFunc', [arg], dict) */
4024 s = get_tv_string(&argvars[0]);
4025 use_string = TRUE;
4026 }
4027
Bram Moolenaar843b8842016-08-21 14:36:15 +02004028 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004029 {
4030 name = s;
4031 trans_name = trans_function_name(&name, FALSE,
4032 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4033 if (*name != NUL)
4034 s = NULL;
4035 }
4036
Bram Moolenaar843b8842016-08-21 14:36:15 +02004037 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4038 || (is_funcref && trans_name == NULL))
Bram Moolenaar5582ef12016-09-14 22:16:13 +02004039 EMSG2(_(e_invarg2), use_string ? get_tv_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004040 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004041 else if (trans_name != NULL && (is_funcref
4042 ? find_func(trans_name) == NULL
4043 : !translated_function_exists(trans_name)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004044 EMSG2(_("E700: Unknown function: %s"), s);
4045 else
4046 {
4047 int dict_idx = 0;
4048 int arg_idx = 0;
4049 list_T *list = NULL;
4050
4051 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4052 {
4053 char sid_buf[25];
4054 int off = *s == 's' ? 2 : 5;
4055
4056 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4057 * also be called from another script. Using trans_function_name()
4058 * would also work, but some plugins depend on the name being
4059 * printable text. */
4060 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
4061 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
4062 if (name != NULL)
4063 {
4064 STRCPY(name, sid_buf);
4065 STRCAT(name, s + off);
4066 }
4067 }
4068 else
4069 name = vim_strsave(s);
4070
4071 if (argvars[1].v_type != VAR_UNKNOWN)
4072 {
4073 if (argvars[2].v_type != VAR_UNKNOWN)
4074 {
4075 /* function(name, [args], dict) */
4076 arg_idx = 1;
4077 dict_idx = 2;
4078 }
4079 else if (argvars[1].v_type == VAR_DICT)
4080 /* function(name, dict) */
4081 dict_idx = 1;
4082 else
4083 /* function(name, [args]) */
4084 arg_idx = 1;
4085 if (dict_idx > 0)
4086 {
4087 if (argvars[dict_idx].v_type != VAR_DICT)
4088 {
4089 EMSG(_("E922: expected a dict"));
4090 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004091 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004092 }
4093 if (argvars[dict_idx].vval.v_dict == NULL)
4094 dict_idx = 0;
4095 }
4096 if (arg_idx > 0)
4097 {
4098 if (argvars[arg_idx].v_type != VAR_LIST)
4099 {
4100 EMSG(_("E923: Second argument of function() must be a list or a dict"));
4101 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004102 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004103 }
4104 list = argvars[arg_idx].vval.v_list;
4105 if (list == NULL || list->lv_len == 0)
4106 arg_idx = 0;
4107 }
4108 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004109 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004110 {
4111 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
4112
4113 /* result is a VAR_PARTIAL */
4114 if (pt == NULL)
4115 vim_free(name);
4116 else
4117 {
4118 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4119 {
4120 listitem_T *li;
4121 int i = 0;
4122 int arg_len = 0;
4123 int lv_len = 0;
4124
4125 if (arg_pt != NULL)
4126 arg_len = arg_pt->pt_argc;
4127 if (list != NULL)
4128 lv_len = list->lv_len;
4129 pt->pt_argc = arg_len + lv_len;
4130 pt->pt_argv = (typval_T *)alloc(
4131 sizeof(typval_T) * pt->pt_argc);
4132 if (pt->pt_argv == NULL)
4133 {
4134 vim_free(pt);
4135 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004136 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004137 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004138 for (i = 0; i < arg_len; i++)
4139 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4140 if (lv_len > 0)
4141 for (li = list->lv_first; li != NULL;
4142 li = li->li_next)
4143 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004144 }
4145
4146 /* For "function(dict.func, [], dict)" and "func" is a partial
4147 * use "dict". That is backwards compatible. */
4148 if (dict_idx > 0)
4149 {
4150 /* The dict is bound explicitly, pt_auto is FALSE. */
4151 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4152 ++pt->pt_dict->dv_refcount;
4153 }
4154 else if (arg_pt != NULL)
4155 {
4156 /* If the dict was bound automatically the result is also
4157 * bound automatically. */
4158 pt->pt_dict = arg_pt->pt_dict;
4159 pt->pt_auto = arg_pt->pt_auto;
4160 if (pt->pt_dict != NULL)
4161 ++pt->pt_dict->dv_refcount;
4162 }
4163
4164 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004165 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4166 {
4167 pt->pt_func = arg_pt->pt_func;
4168 func_ptr_ref(pt->pt_func);
4169 vim_free(name);
4170 }
4171 else if (is_funcref)
4172 {
4173 pt->pt_func = find_func(trans_name);
4174 func_ptr_ref(pt->pt_func);
4175 vim_free(name);
4176 }
4177 else
4178 {
4179 pt->pt_name = name;
4180 func_ref(name);
4181 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004182 }
4183 rettv->v_type = VAR_PARTIAL;
4184 rettv->vval.v_partial = pt;
4185 }
4186 else
4187 {
4188 /* result is a VAR_FUNC */
4189 rettv->v_type = VAR_FUNC;
4190 rettv->vval.v_string = name;
4191 func_ref(name);
4192 }
4193 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004194theend:
4195 vim_free(trans_name);
4196}
4197
4198/*
4199 * "funcref()" function
4200 */
4201 static void
4202f_funcref(typval_T *argvars, typval_T *rettv)
4203{
4204 common_function(argvars, rettv, TRUE);
4205}
4206
4207/*
4208 * "function()" function
4209 */
4210 static void
4211f_function(typval_T *argvars, typval_T *rettv)
4212{
4213 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004214}
4215
4216/*
4217 * "garbagecollect()" function
4218 */
4219 static void
4220f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4221{
4222 /* This is postponed until we are back at the toplevel, because we may be
4223 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4224 want_garbage_collect = TRUE;
4225
4226 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
4227 garbage_collect_at_exit = TRUE;
4228}
4229
4230/*
4231 * "get()" function
4232 */
4233 static void
4234f_get(typval_T *argvars, typval_T *rettv)
4235{
4236 listitem_T *li;
4237 list_T *l;
4238 dictitem_T *di;
4239 dict_T *d;
4240 typval_T *tv = NULL;
4241
4242 if (argvars[0].v_type == VAR_LIST)
4243 {
4244 if ((l = argvars[0].vval.v_list) != NULL)
4245 {
4246 int error = FALSE;
4247
4248 li = list_find(l, (long)get_tv_number_chk(&argvars[1], &error));
4249 if (!error && li != NULL)
4250 tv = &li->li_tv;
4251 }
4252 }
4253 else if (argvars[0].v_type == VAR_DICT)
4254 {
4255 if ((d = argvars[0].vval.v_dict) != NULL)
4256 {
4257 di = dict_find(d, get_tv_string(&argvars[1]), -1);
4258 if (di != NULL)
4259 tv = &di->di_tv;
4260 }
4261 }
4262 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4263 {
4264 partial_T *pt;
4265 partial_T fref_pt;
4266
4267 if (argvars[0].v_type == VAR_PARTIAL)
4268 pt = argvars[0].vval.v_partial;
4269 else
4270 {
4271 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4272 fref_pt.pt_name = argvars[0].vval.v_string;
4273 pt = &fref_pt;
4274 }
4275
4276 if (pt != NULL)
4277 {
4278 char_u *what = get_tv_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004279 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004280
4281 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4282 {
4283 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004284 n = partial_name(pt);
4285 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004286 rettv->vval.v_string = NULL;
4287 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004288 {
4289 rettv->vval.v_string = vim_strsave(n);
4290 if (rettv->v_type == VAR_FUNC)
4291 func_ref(rettv->vval.v_string);
4292 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004293 }
4294 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004295 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004296 else if (STRCMP(what, "args") == 0)
4297 {
4298 rettv->v_type = VAR_LIST;
4299 if (rettv_list_alloc(rettv) == OK)
4300 {
4301 int i;
4302
4303 for (i = 0; i < pt->pt_argc; ++i)
4304 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4305 }
4306 }
4307 else
4308 EMSG2(_(e_invarg2), what);
4309 return;
4310 }
4311 }
4312 else
4313 EMSG2(_(e_listdictarg), "get()");
4314
4315 if (tv == NULL)
4316 {
4317 if (argvars[2].v_type != VAR_UNKNOWN)
4318 copy_tv(&argvars[2], rettv);
4319 }
4320 else
4321 copy_tv(tv, rettv);
4322}
4323
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004324#ifdef FEAT_SIGNS
4325/*
4326 * Returns information about signs placed in a buffer as list of dicts.
4327 */
4328 static void
4329get_buffer_signs(buf_T *buf, list_T *l)
4330{
4331 signlist_T *sign;
4332
4333 for (sign = buf->b_signlist; sign; sign = sign->next)
4334 {
4335 dict_T *d = dict_alloc();
4336
4337 if (d != NULL)
4338 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02004339 dict_add_number(d, "id", sign->id);
4340 dict_add_number(d, "lnum", sign->lnum);
4341 dict_add_string(d, "name", sign_typenr2name(sign->typenr));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004342
4343 list_append_dict(l, d);
4344 }
4345 }
4346}
4347#endif
4348
4349/*
4350 * Returns buffer options, variables and other attributes in a dictionary.
4351 */
4352 static dict_T *
4353get_buffer_info(buf_T *buf)
4354{
4355 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004356 tabpage_T *tp;
4357 win_T *wp;
4358 list_T *windows;
4359
4360 dict = dict_alloc();
4361 if (dict == NULL)
4362 return NULL;
4363
Bram Moolenaare0be1672018-07-08 16:50:37 +02004364 dict_add_number(dict, "bufnr", buf->b_fnum);
4365 dict_add_string(dict, "name", buf->b_ffname);
4366 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4367 : buflist_findlnum(buf));
4368 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4369 dict_add_number(dict, "listed", buf->b_p_bl);
4370 dict_add_number(dict, "changed", bufIsChanged(buf));
4371 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4372 dict_add_number(dict, "hidden",
4373 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004374
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004375 /* Get a reference to buffer variables */
4376 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004377
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004378 /* List of windows displaying this buffer */
4379 windows = list_alloc();
4380 if (windows != NULL)
4381 {
4382 FOR_ALL_TAB_WINDOWS(tp, wp)
4383 if (wp->w_buffer == buf)
4384 list_append_number(windows, (varnumber_T)wp->w_id);
4385 dict_add_list(dict, "windows", windows);
4386 }
4387
4388#ifdef FEAT_SIGNS
4389 if (buf->b_signlist != NULL)
4390 {
4391 /* List of signs placed in this buffer */
4392 list_T *signs = list_alloc();
4393 if (signs != NULL)
4394 {
4395 get_buffer_signs(buf, signs);
4396 dict_add_list(dict, "signs", signs);
4397 }
4398 }
4399#endif
4400
4401 return dict;
4402}
4403
4404/*
4405 * "getbufinfo()" function
4406 */
4407 static void
4408f_getbufinfo(typval_T *argvars, typval_T *rettv)
4409{
4410 buf_T *buf = NULL;
4411 buf_T *argbuf = NULL;
4412 dict_T *d;
4413 int filtered = FALSE;
4414 int sel_buflisted = FALSE;
4415 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004416 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004417
4418 if (rettv_list_alloc(rettv) != OK)
4419 return;
4420
4421 /* List of all the buffers or selected buffers */
4422 if (argvars[0].v_type == VAR_DICT)
4423 {
4424 dict_T *sel_d = argvars[0].vval.v_dict;
4425
4426 if (sel_d != NULL)
4427 {
4428 dictitem_T *di;
4429
4430 filtered = TRUE;
4431
4432 di = dict_find(sel_d, (char_u *)"buflisted", -1);
4433 if (di != NULL && get_tv_number(&di->di_tv))
4434 sel_buflisted = TRUE;
4435
4436 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
4437 if (di != NULL && get_tv_number(&di->di_tv))
4438 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004439
4440 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
4441 if (di != NULL && get_tv_number(&di->di_tv))
4442 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004443 }
4444 }
4445 else if (argvars[0].v_type != VAR_UNKNOWN)
4446 {
4447 /* Information about one buffer. Argument specifies the buffer */
4448 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4449 ++emsg_off;
4450 argbuf = get_buf_tv(&argvars[0], FALSE);
4451 --emsg_off;
4452 if (argbuf == NULL)
4453 return;
4454 }
4455
4456 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004457 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004458 {
4459 if (argbuf != NULL && argbuf != buf)
4460 continue;
4461 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004462 || (sel_buflisted && !buf->b_p_bl)
4463 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004464 continue;
4465
4466 d = get_buffer_info(buf);
4467 if (d != NULL)
4468 list_append_dict(rettv->vval.v_list, d);
4469 if (argbuf != NULL)
4470 return;
4471 }
4472}
4473
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004474static void get_buffer_lines(buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv);
4475
4476/*
4477 * Get line or list of lines from buffer "buf" into "rettv".
4478 * Return a range (from start to end) of lines in rettv from the specified
4479 * buffer.
4480 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4481 */
4482 static void
4483get_buffer_lines(
4484 buf_T *buf,
4485 linenr_T start,
4486 linenr_T end,
4487 int retlist,
4488 typval_T *rettv)
4489{
4490 char_u *p;
4491
4492 rettv->v_type = VAR_STRING;
4493 rettv->vval.v_string = NULL;
4494 if (retlist && rettv_list_alloc(rettv) == FAIL)
4495 return;
4496
4497 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4498 return;
4499
4500 if (!retlist)
4501 {
4502 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4503 p = ml_get_buf(buf, start, FALSE);
4504 else
4505 p = (char_u *)"";
4506 rettv->vval.v_string = vim_strsave(p);
4507 }
4508 else
4509 {
4510 if (end < start)
4511 return;
4512
4513 if (start < 1)
4514 start = 1;
4515 if (end > buf->b_ml.ml_line_count)
4516 end = buf->b_ml.ml_line_count;
4517 while (start <= end)
4518 if (list_append_string(rettv->vval.v_list,
4519 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4520 break;
4521 }
4522}
4523
4524/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004525 * "getbufline()" function
4526 */
4527 static void
4528f_getbufline(typval_T *argvars, typval_T *rettv)
4529{
4530 linenr_T lnum;
4531 linenr_T end;
4532 buf_T *buf;
4533
4534 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4535 ++emsg_off;
4536 buf = get_buf_tv(&argvars[0], FALSE);
4537 --emsg_off;
4538
4539 lnum = get_tv_lnum_buf(&argvars[1], buf);
4540 if (argvars[2].v_type == VAR_UNKNOWN)
4541 end = lnum;
4542 else
4543 end = get_tv_lnum_buf(&argvars[2], buf);
4544
4545 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4546}
4547
4548/*
4549 * "getbufvar()" function
4550 */
4551 static void
4552f_getbufvar(typval_T *argvars, typval_T *rettv)
4553{
4554 buf_T *buf;
4555 buf_T *save_curbuf;
4556 char_u *varname;
4557 dictitem_T *v;
4558 int done = FALSE;
4559
4560 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4561 varname = get_tv_string_chk(&argvars[1]);
4562 ++emsg_off;
4563 buf = get_buf_tv(&argvars[0], FALSE);
4564
4565 rettv->v_type = VAR_STRING;
4566 rettv->vval.v_string = NULL;
4567
4568 if (buf != NULL && varname != NULL)
4569 {
4570 /* set curbuf to be our buf, temporarily */
4571 save_curbuf = curbuf;
4572 curbuf = buf;
4573
Bram Moolenaar30567352016-08-27 21:25:44 +02004574 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004575 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004576 if (varname[1] == NUL)
4577 {
4578 /* get all buffer-local options in a dict */
4579 dict_T *opts = get_winbuf_options(TRUE);
4580
4581 if (opts != NULL)
4582 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004583 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004584 done = TRUE;
4585 }
4586 }
4587 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4588 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004589 done = TRUE;
4590 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004591 else
4592 {
4593 /* Look up the variable. */
4594 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4595 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4596 'b', varname, FALSE);
4597 if (v != NULL)
4598 {
4599 copy_tv(&v->di_tv, rettv);
4600 done = TRUE;
4601 }
4602 }
4603
4604 /* restore previous notion of curbuf */
4605 curbuf = save_curbuf;
4606 }
4607
4608 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4609 /* use the default value */
4610 copy_tv(&argvars[2], rettv);
4611
4612 --emsg_off;
4613}
4614
4615/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004616 * "getchangelist()" function
4617 */
4618 static void
4619f_getchangelist(typval_T *argvars, typval_T *rettv)
4620{
4621#ifdef FEAT_JUMPLIST
4622 buf_T *buf;
4623 int i;
4624 list_T *l;
4625 dict_T *d;
4626#endif
4627
4628 if (rettv_list_alloc(rettv) != OK)
4629 return;
4630
4631#ifdef FEAT_JUMPLIST
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004632 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4633 ++emsg_off;
4634 buf = get_buf_tv(&argvars[0], FALSE);
4635 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004636 if (buf == NULL)
4637 return;
4638
4639 l = list_alloc();
4640 if (l == NULL)
4641 return;
4642
4643 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4644 return;
4645 /*
4646 * The current window change list index tracks only the position in the
4647 * current buffer change list. For other buffers, use the change list
4648 * length as the current index.
4649 */
4650 list_append_number(rettv->vval.v_list,
4651 (varnumber_T)((buf == curwin->w_buffer)
4652 ? curwin->w_changelistidx : buf->b_changelistlen));
4653
4654 for (i = 0; i < buf->b_changelistlen; ++i)
4655 {
4656 if (buf->b_changelist[i].lnum == 0)
4657 continue;
4658 if ((d = dict_alloc()) == NULL)
4659 return;
4660 if (list_append_dict(l, d) == FAIL)
4661 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004662 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4663 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004664# ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +02004665 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004666# endif
4667 }
4668#endif
4669}
4670/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004671 * "getchar()" function
4672 */
4673 static void
4674f_getchar(typval_T *argvars, typval_T *rettv)
4675{
4676 varnumber_T n;
4677 int error = FALSE;
4678
4679 /* Position the cursor. Needed after a message that ends in a space. */
4680 windgoto(msg_row, msg_col);
4681
4682 ++no_mapping;
4683 ++allow_keys;
4684 for (;;)
4685 {
4686 if (argvars[0].v_type == VAR_UNKNOWN)
4687 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004688 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004689 else if (get_tv_number_chk(&argvars[0], &error) == 1)
4690 /* getchar(1): only check if char avail */
4691 n = vpeekc_any();
4692 else if (error || vpeekc_any() == NUL)
4693 /* illegal argument or getchar(0) and no char avail: return zero */
4694 n = 0;
4695 else
4696 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004697 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004698
4699 if (n == K_IGNORE)
4700 continue;
4701 break;
4702 }
4703 --no_mapping;
4704 --allow_keys;
4705
4706 set_vim_var_nr(VV_MOUSE_WIN, 0);
4707 set_vim_var_nr(VV_MOUSE_WINID, 0);
4708 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4709 set_vim_var_nr(VV_MOUSE_COL, 0);
4710
4711 rettv->vval.v_number = n;
4712 if (IS_SPECIAL(n) || mod_mask != 0)
4713 {
4714 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4715 int i = 0;
4716
4717 /* Turn a special key into three bytes, plus modifier. */
4718 if (mod_mask != 0)
4719 {
4720 temp[i++] = K_SPECIAL;
4721 temp[i++] = KS_MODIFIER;
4722 temp[i++] = mod_mask;
4723 }
4724 if (IS_SPECIAL(n))
4725 {
4726 temp[i++] = K_SPECIAL;
4727 temp[i++] = K_SECOND(n);
4728 temp[i++] = K_THIRD(n);
4729 }
4730#ifdef FEAT_MBYTE
4731 else if (has_mbyte)
4732 i += (*mb_char2bytes)(n, temp + i);
4733#endif
4734 else
4735 temp[i++] = n;
4736 temp[i++] = NUL;
4737 rettv->v_type = VAR_STRING;
4738 rettv->vval.v_string = vim_strsave(temp);
4739
4740#ifdef FEAT_MOUSE
4741 if (is_mouse_key(n))
4742 {
4743 int row = mouse_row;
4744 int col = mouse_col;
4745 win_T *win;
4746 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004747 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004748 int winnr = 1;
4749
4750 if (row >= 0 && col >= 0)
4751 {
4752 /* Find the window at the mouse coordinates and compute the
4753 * text position. */
4754 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004755 if (win == NULL)
4756 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004757 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004758 for (wp = firstwin; wp != win; wp = wp->w_next)
4759 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004760 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4761 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4762 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4763 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4764 }
4765 }
4766#endif
4767 }
4768}
4769
4770/*
4771 * "getcharmod()" function
4772 */
4773 static void
4774f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4775{
4776 rettv->vval.v_number = mod_mask;
4777}
4778
4779/*
4780 * "getcharsearch()" function
4781 */
4782 static void
4783f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4784{
4785 if (rettv_dict_alloc(rettv) != FAIL)
4786 {
4787 dict_T *dict = rettv->vval.v_dict;
4788
Bram Moolenaare0be1672018-07-08 16:50:37 +02004789 dict_add_string(dict, "char", last_csearch());
4790 dict_add_number(dict, "forward", last_csearch_forward());
4791 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004792 }
4793}
4794
4795/*
4796 * "getcmdline()" function
4797 */
4798 static void
4799f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4800{
4801 rettv->v_type = VAR_STRING;
4802 rettv->vval.v_string = get_cmdline_str();
4803}
4804
4805/*
4806 * "getcmdpos()" function
4807 */
4808 static void
4809f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4810{
4811 rettv->vval.v_number = get_cmdline_pos() + 1;
4812}
4813
4814/*
4815 * "getcmdtype()" function
4816 */
4817 static void
4818f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4819{
4820 rettv->v_type = VAR_STRING;
4821 rettv->vval.v_string = alloc(2);
4822 if (rettv->vval.v_string != NULL)
4823 {
4824 rettv->vval.v_string[0] = get_cmdline_type();
4825 rettv->vval.v_string[1] = NUL;
4826 }
4827}
4828
4829/*
4830 * "getcmdwintype()" function
4831 */
4832 static void
4833f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4834{
4835 rettv->v_type = VAR_STRING;
4836 rettv->vval.v_string = NULL;
4837#ifdef FEAT_CMDWIN
4838 rettv->vval.v_string = alloc(2);
4839 if (rettv->vval.v_string != NULL)
4840 {
4841 rettv->vval.v_string[0] = cmdwin_type;
4842 rettv->vval.v_string[1] = NUL;
4843 }
4844#endif
4845}
4846
4847#if defined(FEAT_CMDL_COMPL)
4848/*
4849 * "getcompletion()" function
4850 */
4851 static void
4852f_getcompletion(typval_T *argvars, typval_T *rettv)
4853{
4854 char_u *pat;
4855 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004856 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004857 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
4858 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004859
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004860 if (argvars[2].v_type != VAR_UNKNOWN)
4861 filtered = get_tv_number_chk(&argvars[2], NULL);
4862
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004863 if (p_wic)
4864 options |= WILD_ICASE;
4865
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004866 /* For filtered results, 'wildignore' is used */
4867 if (!filtered)
4868 options |= WILD_KEEP_ALL;
4869
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004870 ExpandInit(&xpc);
4871 xpc.xp_pattern = get_tv_string(&argvars[0]);
4872 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4873 xpc.xp_context = cmdcomplete_str_to_type(get_tv_string(&argvars[1]));
4874 if (xpc.xp_context == EXPAND_NOTHING)
4875 {
4876 if (argvars[1].v_type == VAR_STRING)
4877 EMSG2(_(e_invarg2), argvars[1].vval.v_string);
4878 else
4879 EMSG(_(e_invarg));
4880 return;
4881 }
4882
4883# if defined(FEAT_MENU)
4884 if (xpc.xp_context == EXPAND_MENUS)
4885 {
4886 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4887 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4888 }
4889# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02004890#ifdef FEAT_CSCOPE
4891 if (xpc.xp_context == EXPAND_CSCOPE)
4892 {
4893 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4894 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4895 }
4896#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02004897#ifdef FEAT_SIGNS
4898 if (xpc.xp_context == EXPAND_SIGN)
4899 {
4900 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4901 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4902 }
4903#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004904
4905 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4906 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
4907 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004908 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004909
4910 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4911
4912 for (i = 0; i < xpc.xp_numfiles; i++)
4913 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4914 }
4915 vim_free(pat);
4916 ExpandCleanup(&xpc);
4917}
4918#endif
4919
4920/*
4921 * "getcwd()" function
4922 */
4923 static void
4924f_getcwd(typval_T *argvars, typval_T *rettv)
4925{
4926 win_T *wp = NULL;
4927 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01004928 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004929
4930 rettv->v_type = VAR_STRING;
4931 rettv->vval.v_string = NULL;
4932
Bram Moolenaar54591292018-02-09 20:53:59 +01004933 if (argvars[0].v_type == VAR_NUMBER && argvars[0].vval.v_number == -1)
4934 global = TRUE;
4935 else
4936 wp = find_tabwin(&argvars[0], &argvars[1]);
4937
4938 if (wp != NULL && wp->w_localdir != NULL)
4939 rettv->vval.v_string = vim_strsave(wp->w_localdir);
4940 else if (wp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004941 {
Bram Moolenaar54591292018-02-09 20:53:59 +01004942 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004943 rettv->vval.v_string = vim_strsave(globaldir);
4944 else
4945 {
4946 cwd = alloc(MAXPATHL);
4947 if (cwd != NULL)
4948 {
4949 if (mch_dirname(cwd, MAXPATHL) != FAIL)
4950 rettv->vval.v_string = vim_strsave(cwd);
4951 vim_free(cwd);
4952 }
4953 }
4954#ifdef BACKSLASH_IN_FILENAME
4955 if (rettv->vval.v_string != NULL)
4956 slash_adjust(rettv->vval.v_string);
4957#endif
4958 }
4959}
4960
4961/*
4962 * "getfontname()" function
4963 */
4964 static void
4965f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
4966{
4967 rettv->v_type = VAR_STRING;
4968 rettv->vval.v_string = NULL;
4969#ifdef FEAT_GUI
4970 if (gui.in_use)
4971 {
4972 GuiFont font;
4973 char_u *name = NULL;
4974
4975 if (argvars[0].v_type == VAR_UNKNOWN)
4976 {
4977 /* Get the "Normal" font. Either the name saved by
4978 * hl_set_font_name() or from the font ID. */
4979 font = gui.norm_font;
4980 name = hl_get_font_name();
4981 }
4982 else
4983 {
4984 name = get_tv_string(&argvars[0]);
4985 if (STRCMP(name, "*") == 0) /* don't use font dialog */
4986 return;
4987 font = gui_mch_get_font(name, FALSE);
4988 if (font == NOFONT)
4989 return; /* Invalid font name, return empty string. */
4990 }
4991 rettv->vval.v_string = gui_mch_get_fontname(font, name);
4992 if (argvars[0].v_type != VAR_UNKNOWN)
4993 gui_mch_free_font(font);
4994 }
4995#endif
4996}
4997
4998/*
4999 * "getfperm({fname})" function
5000 */
5001 static void
5002f_getfperm(typval_T *argvars, typval_T *rettv)
5003{
5004 char_u *fname;
5005 stat_T st;
5006 char_u *perm = NULL;
5007 char_u flags[] = "rwx";
5008 int i;
5009
5010 fname = get_tv_string(&argvars[0]);
5011
5012 rettv->v_type = VAR_STRING;
5013 if (mch_stat((char *)fname, &st) >= 0)
5014 {
5015 perm = vim_strsave((char_u *)"---------");
5016 if (perm != NULL)
5017 {
5018 for (i = 0; i < 9; i++)
5019 {
5020 if (st.st_mode & (1 << (8 - i)))
5021 perm[i] = flags[i % 3];
5022 }
5023 }
5024 }
5025 rettv->vval.v_string = perm;
5026}
5027
5028/*
5029 * "getfsize({fname})" function
5030 */
5031 static void
5032f_getfsize(typval_T *argvars, typval_T *rettv)
5033{
5034 char_u *fname;
5035 stat_T st;
5036
5037 fname = get_tv_string(&argvars[0]);
5038
5039 rettv->v_type = VAR_NUMBER;
5040
5041 if (mch_stat((char *)fname, &st) >= 0)
5042 {
5043 if (mch_isdir(fname))
5044 rettv->vval.v_number = 0;
5045 else
5046 {
5047 rettv->vval.v_number = (varnumber_T)st.st_size;
5048
5049 /* non-perfect check for overflow */
5050 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5051 rettv->vval.v_number = -2;
5052 }
5053 }
5054 else
5055 rettv->vval.v_number = -1;
5056}
5057
5058/*
5059 * "getftime({fname})" function
5060 */
5061 static void
5062f_getftime(typval_T *argvars, typval_T *rettv)
5063{
5064 char_u *fname;
5065 stat_T st;
5066
5067 fname = get_tv_string(&argvars[0]);
5068
5069 if (mch_stat((char *)fname, &st) >= 0)
5070 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5071 else
5072 rettv->vval.v_number = -1;
5073}
5074
5075/*
5076 * "getftype({fname})" function
5077 */
5078 static void
5079f_getftype(typval_T *argvars, typval_T *rettv)
5080{
5081 char_u *fname;
5082 stat_T st;
5083 char_u *type = NULL;
5084 char *t;
5085
5086 fname = get_tv_string(&argvars[0]);
5087
5088 rettv->v_type = VAR_STRING;
5089 if (mch_lstat((char *)fname, &st) >= 0)
5090 {
5091#ifdef S_ISREG
5092 if (S_ISREG(st.st_mode))
5093 t = "file";
5094 else if (S_ISDIR(st.st_mode))
5095 t = "dir";
5096# ifdef S_ISLNK
5097 else if (S_ISLNK(st.st_mode))
5098 t = "link";
5099# endif
5100# ifdef S_ISBLK
5101 else if (S_ISBLK(st.st_mode))
5102 t = "bdev";
5103# endif
5104# ifdef S_ISCHR
5105 else if (S_ISCHR(st.st_mode))
5106 t = "cdev";
5107# endif
5108# ifdef S_ISFIFO
5109 else if (S_ISFIFO(st.st_mode))
5110 t = "fifo";
5111# endif
5112# ifdef S_ISSOCK
5113 else if (S_ISSOCK(st.st_mode))
5114 t = "fifo";
5115# endif
5116 else
5117 t = "other";
5118#else
5119# ifdef S_IFMT
5120 switch (st.st_mode & S_IFMT)
5121 {
5122 case S_IFREG: t = "file"; break;
5123 case S_IFDIR: t = "dir"; break;
5124# ifdef S_IFLNK
5125 case S_IFLNK: t = "link"; break;
5126# endif
5127# ifdef S_IFBLK
5128 case S_IFBLK: t = "bdev"; break;
5129# endif
5130# ifdef S_IFCHR
5131 case S_IFCHR: t = "cdev"; break;
5132# endif
5133# ifdef S_IFIFO
5134 case S_IFIFO: t = "fifo"; break;
5135# endif
5136# ifdef S_IFSOCK
5137 case S_IFSOCK: t = "socket"; break;
5138# endif
5139 default: t = "other";
5140 }
5141# else
5142 if (mch_isdir(fname))
5143 t = "dir";
5144 else
5145 t = "file";
5146# endif
5147#endif
5148 type = vim_strsave((char_u *)t);
5149 }
5150 rettv->vval.v_string = type;
5151}
5152
5153/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005154 * "getjumplist()" function
5155 */
5156 static void
5157f_getjumplist(typval_T *argvars, typval_T *rettv)
5158{
5159#ifdef FEAT_JUMPLIST
5160 win_T *wp;
5161 int i;
5162 list_T *l;
5163 dict_T *d;
5164#endif
5165
5166 if (rettv_list_alloc(rettv) != OK)
5167 return;
5168
5169#ifdef FEAT_JUMPLIST
5170 wp = find_tabwin(&argvars[0], &argvars[1]);
5171 if (wp == NULL)
5172 return;
5173
5174 l = list_alloc();
5175 if (l == NULL)
5176 return;
5177
5178 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5179 return;
5180 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5181
Bram Moolenaar48679742018-02-13 13:33:29 +01005182 cleanup_jumplist(wp, TRUE);
5183
Bram Moolenaar4f505882018-02-10 21:06:32 +01005184 for (i = 0; i < wp->w_jumplistlen; ++i)
5185 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005186 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5187 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005188 if ((d = dict_alloc()) == NULL)
5189 return;
5190 if (list_append_dict(l, d) == FAIL)
5191 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005192 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5193 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005194# ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +02005195 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005196# endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005197 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005198 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005199 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005200 }
5201#endif
5202}
5203
5204/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005205 * "getline(lnum, [end])" function
5206 */
5207 static void
5208f_getline(typval_T *argvars, typval_T *rettv)
5209{
5210 linenr_T lnum;
5211 linenr_T end;
5212 int retlist;
5213
5214 lnum = get_tv_lnum(argvars);
5215 if (argvars[1].v_type == VAR_UNKNOWN)
5216 {
5217 end = 0;
5218 retlist = FALSE;
5219 }
5220 else
5221 {
5222 end = get_tv_lnum(&argvars[1]);
5223 retlist = TRUE;
5224 }
5225
5226 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5227}
5228
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005229#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005230 static void
5231get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5232{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005233 if (what_arg->v_type == VAR_UNKNOWN)
5234 {
5235 if (rettv_list_alloc(rettv) == OK)
5236 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005237 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005238 }
5239 else
5240 {
5241 if (rettv_dict_alloc(rettv) == OK)
5242 if (is_qf || (wp != NULL))
5243 {
5244 if (what_arg->v_type == VAR_DICT)
5245 {
5246 dict_T *d = what_arg->vval.v_dict;
5247
5248 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005249 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005250 }
5251 else
5252 EMSG(_(e_dictreq));
5253 }
5254 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005255}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005256#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005257
5258/*
5259 * "getloclist()" function
5260 */
5261 static void
5262f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5263{
5264#ifdef FEAT_QUICKFIX
5265 win_T *wp;
5266
5267 wp = find_win_by_nr(&argvars[0], NULL);
5268 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5269#endif
5270}
5271
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005272/*
5273 * "getmatches()" function
5274 */
5275 static void
5276f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5277{
5278#ifdef FEAT_SEARCH_EXTRA
5279 dict_T *dict;
5280 matchitem_T *cur = curwin->w_match_head;
5281 int i;
5282
5283 if (rettv_list_alloc(rettv) == OK)
5284 {
5285 while (cur != NULL)
5286 {
5287 dict = dict_alloc();
5288 if (dict == NULL)
5289 return;
5290 if (cur->match.regprog == NULL)
5291 {
5292 /* match added with matchaddpos() */
5293 for (i = 0; i < MAXPOSMATCH; ++i)
5294 {
5295 llpos_T *llpos;
5296 char buf[6];
5297 list_T *l;
5298
5299 llpos = &cur->pos.pos[i];
5300 if (llpos->lnum == 0)
5301 break;
5302 l = list_alloc();
5303 if (l == NULL)
5304 break;
5305 list_append_number(l, (varnumber_T)llpos->lnum);
5306 if (llpos->col > 0)
5307 {
5308 list_append_number(l, (varnumber_T)llpos->col);
5309 list_append_number(l, (varnumber_T)llpos->len);
5310 }
5311 sprintf(buf, "pos%d", i + 1);
5312 dict_add_list(dict, buf, l);
5313 }
5314 }
5315 else
5316 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02005317 dict_add_string(dict, "pattern", cur->pattern);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005318 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02005319 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
5320 dict_add_number(dict, "priority", (long)cur->priority);
5321 dict_add_number(dict, "id", (long)cur->id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005322# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
5323 if (cur->conceal_char)
5324 {
5325 char_u buf[MB_MAXBYTES + 1];
5326
5327 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005328 dict_add_string(dict, "conceal", (char_u *)&buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005329 }
5330# endif
5331 list_append_dict(rettv->vval.v_list, dict);
5332 cur = cur->next;
5333 }
5334 }
5335#endif
5336}
5337
5338/*
5339 * "getpid()" function
5340 */
5341 static void
5342f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5343{
5344 rettv->vval.v_number = mch_get_pid();
5345}
5346
5347 static void
5348getpos_both(
5349 typval_T *argvars,
5350 typval_T *rettv,
5351 int getcurpos)
5352{
5353 pos_T *fp;
5354 list_T *l;
5355 int fnum = -1;
5356
5357 if (rettv_list_alloc(rettv) == OK)
5358 {
5359 l = rettv->vval.v_list;
5360 if (getcurpos)
5361 fp = &curwin->w_cursor;
5362 else
5363 fp = var2fpos(&argvars[0], TRUE, &fnum);
5364 if (fnum != -1)
5365 list_append_number(l, (varnumber_T)fnum);
5366 else
5367 list_append_number(l, (varnumber_T)0);
5368 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5369 : (varnumber_T)0);
5370 list_append_number(l, (fp != NULL)
5371 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5372 : (varnumber_T)0);
5373 list_append_number(l,
5374#ifdef FEAT_VIRTUALEDIT
5375 (fp != NULL) ? (varnumber_T)fp->coladd :
5376#endif
5377 (varnumber_T)0);
5378 if (getcurpos)
5379 {
5380 update_curswant();
5381 list_append_number(l, curwin->w_curswant == MAXCOL ?
5382 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
5383 }
5384 }
5385 else
5386 rettv->vval.v_number = FALSE;
5387}
5388
5389
5390/*
5391 * "getcurpos()" function
5392 */
5393 static void
5394f_getcurpos(typval_T *argvars, typval_T *rettv)
5395{
5396 getpos_both(argvars, rettv, TRUE);
5397}
5398
5399/*
5400 * "getpos(string)" function
5401 */
5402 static void
5403f_getpos(typval_T *argvars, typval_T *rettv)
5404{
5405 getpos_both(argvars, rettv, FALSE);
5406}
5407
5408/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005409 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005410 */
5411 static void
5412f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5413{
5414#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005415 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005416#endif
5417}
5418
5419/*
5420 * "getreg()" function
5421 */
5422 static void
5423f_getreg(typval_T *argvars, typval_T *rettv)
5424{
5425 char_u *strregname;
5426 int regname;
5427 int arg2 = FALSE;
5428 int return_list = FALSE;
5429 int error = FALSE;
5430
5431 if (argvars[0].v_type != VAR_UNKNOWN)
5432 {
5433 strregname = get_tv_string_chk(&argvars[0]);
5434 error = strregname == NULL;
5435 if (argvars[1].v_type != VAR_UNKNOWN)
5436 {
5437 arg2 = (int)get_tv_number_chk(&argvars[1], &error);
5438 if (!error && argvars[2].v_type != VAR_UNKNOWN)
5439 return_list = (int)get_tv_number_chk(&argvars[2], &error);
5440 }
5441 }
5442 else
5443 strregname = get_vim_var_str(VV_REG);
5444
5445 if (error)
5446 return;
5447
5448 regname = (strregname == NULL ? '"' : *strregname);
5449 if (regname == 0)
5450 regname = '"';
5451
5452 if (return_list)
5453 {
5454 rettv->v_type = VAR_LIST;
5455 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5456 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5457 if (rettv->vval.v_list == NULL)
5458 (void)rettv_list_alloc(rettv);
5459 else
5460 ++rettv->vval.v_list->lv_refcount;
5461 }
5462 else
5463 {
5464 rettv->v_type = VAR_STRING;
5465 rettv->vval.v_string = get_reg_contents(regname,
5466 arg2 ? GREG_EXPR_SRC : 0);
5467 }
5468}
5469
5470/*
5471 * "getregtype()" function
5472 */
5473 static void
5474f_getregtype(typval_T *argvars, typval_T *rettv)
5475{
5476 char_u *strregname;
5477 int regname;
5478 char_u buf[NUMBUFLEN + 2];
5479 long reglen = 0;
5480
5481 if (argvars[0].v_type != VAR_UNKNOWN)
5482 {
5483 strregname = get_tv_string_chk(&argvars[0]);
5484 if (strregname == NULL) /* type error; errmsg already given */
5485 {
5486 rettv->v_type = VAR_STRING;
5487 rettv->vval.v_string = NULL;
5488 return;
5489 }
5490 }
5491 else
5492 /* Default to v:register */
5493 strregname = get_vim_var_str(VV_REG);
5494
5495 regname = (strregname == NULL ? '"' : *strregname);
5496 if (regname == 0)
5497 regname = '"';
5498
5499 buf[0] = NUL;
5500 buf[1] = NUL;
5501 switch (get_reg_type(regname, &reglen))
5502 {
5503 case MLINE: buf[0] = 'V'; break;
5504 case MCHAR: buf[0] = 'v'; break;
5505 case MBLOCK:
5506 buf[0] = Ctrl_V;
5507 sprintf((char *)buf + 1, "%ld", reglen + 1);
5508 break;
5509 }
5510 rettv->v_type = VAR_STRING;
5511 rettv->vval.v_string = vim_strsave(buf);
5512}
5513
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005514/*
5515 * Returns information (variables, options, etc.) about a tab page
5516 * as a dictionary.
5517 */
5518 static dict_T *
5519get_tabpage_info(tabpage_T *tp, int tp_idx)
5520{
5521 win_T *wp;
5522 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005523 list_T *l;
5524
5525 dict = dict_alloc();
5526 if (dict == NULL)
5527 return NULL;
5528
Bram Moolenaare0be1672018-07-08 16:50:37 +02005529 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005530
5531 l = list_alloc();
5532 if (l != NULL)
5533 {
5534 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5535 wp; wp = wp->w_next)
5536 list_append_number(l, (varnumber_T)wp->w_id);
5537 dict_add_list(dict, "windows", l);
5538 }
5539
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005540 /* Make a reference to tabpage variables */
5541 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005542
5543 return dict;
5544}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005545
5546/*
5547 * "gettabinfo()" function
5548 */
5549 static void
5550f_gettabinfo(typval_T *argvars, typval_T *rettv)
5551{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005552 tabpage_T *tp, *tparg = NULL;
5553 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005554 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005555
5556 if (rettv_list_alloc(rettv) != OK)
5557 return;
5558
5559 if (argvars[0].v_type != VAR_UNKNOWN)
5560 {
5561 /* Information about one tab page */
5562 tparg = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5563 if (tparg == NULL)
5564 return;
5565 }
5566
5567 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005568 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005569 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005570 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005571 if (tparg != NULL && tp != tparg)
5572 continue;
5573 d = get_tabpage_info(tp, tpnr);
5574 if (d != NULL)
5575 list_append_dict(rettv->vval.v_list, d);
5576 if (tparg != NULL)
5577 return;
5578 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005579}
5580
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005581/*
5582 * "gettabvar()" function
5583 */
5584 static void
5585f_gettabvar(typval_T *argvars, typval_T *rettv)
5586{
5587 win_T *oldcurwin;
5588 tabpage_T *tp, *oldtabpage;
5589 dictitem_T *v;
5590 char_u *varname;
5591 int done = FALSE;
5592
5593 rettv->v_type = VAR_STRING;
5594 rettv->vval.v_string = NULL;
5595
5596 varname = get_tv_string_chk(&argvars[1]);
5597 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5598 if (tp != NULL && varname != NULL)
5599 {
5600 /* Set tp to be our tabpage, temporarily. Also set the window to the
5601 * first window in the tabpage, otherwise the window is not valid. */
5602 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005603 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5604 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005605 {
5606 /* look up the variable */
5607 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5608 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5609 if (v != NULL)
5610 {
5611 copy_tv(&v->di_tv, rettv);
5612 done = TRUE;
5613 }
5614 }
5615
5616 /* restore previous notion of curwin */
5617 restore_win(oldcurwin, oldtabpage, TRUE);
5618 }
5619
5620 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5621 copy_tv(&argvars[2], rettv);
5622}
5623
5624/*
5625 * "gettabwinvar()" function
5626 */
5627 static void
5628f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5629{
5630 getwinvar(argvars, rettv, 1);
5631}
5632
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005633/*
5634 * Returns information about a window as a dictionary.
5635 */
5636 static dict_T *
5637get_win_info(win_T *wp, short tpnr, short winnr)
5638{
5639 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005640
5641 dict = dict_alloc();
5642 if (dict == NULL)
5643 return NULL;
5644
Bram Moolenaare0be1672018-07-08 16:50:37 +02005645 dict_add_number(dict, "tabnr", tpnr);
5646 dict_add_number(dict, "winnr", winnr);
5647 dict_add_number(dict, "winid", wp->w_id);
5648 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005649 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005650#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02005651 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005652#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005653 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005654 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005655 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005656
Bram Moolenaar69905d12017-08-13 18:14:47 +02005657#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02005658 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02005659#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005660#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02005661 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
5662 dict_add_number(dict, "loclist",
5663 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02005664#endif
5665
Bram Moolenaar30567352016-08-27 21:25:44 +02005666 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005667 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005668
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005669 return dict;
5670}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005671
5672/*
5673 * "getwininfo()" function
5674 */
5675 static void
5676f_getwininfo(typval_T *argvars, typval_T *rettv)
5677{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005678 tabpage_T *tp;
5679 win_T *wp = NULL, *wparg = NULL;
5680 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005681 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005682
5683 if (rettv_list_alloc(rettv) != OK)
5684 return;
5685
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005686 if (argvars[0].v_type != VAR_UNKNOWN)
5687 {
5688 wparg = win_id2wp(argvars);
5689 if (wparg == NULL)
5690 return;
5691 }
5692
5693 /* Collect information about either all the windows across all the tab
5694 * pages or one particular window.
5695 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005696 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005697 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005698 tabnr++;
5699 winnr = 0;
5700 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005701 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005702 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005703 if (wparg != NULL && wp != wparg)
5704 continue;
5705 d = get_win_info(wp, tabnr, winnr);
5706 if (d != NULL)
5707 list_append_dict(rettv->vval.v_list, d);
5708 if (wparg != NULL)
5709 /* found information about a specific window */
5710 return;
5711 }
5712 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005713}
5714
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005715/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005716 * "win_findbuf()" function
5717 */
5718 static void
5719f_win_findbuf(typval_T *argvars, typval_T *rettv)
5720{
5721 if (rettv_list_alloc(rettv) != FAIL)
5722 win_findbuf(argvars, rettv->vval.v_list);
5723}
5724
5725/*
5726 * "win_getid()" function
5727 */
5728 static void
5729f_win_getid(typval_T *argvars, typval_T *rettv)
5730{
5731 rettv->vval.v_number = win_getid(argvars);
5732}
5733
5734/*
5735 * "win_gotoid()" function
5736 */
5737 static void
5738f_win_gotoid(typval_T *argvars, typval_T *rettv)
5739{
5740 rettv->vval.v_number = win_gotoid(argvars);
5741}
5742
5743/*
5744 * "win_id2tabwin()" function
5745 */
5746 static void
5747f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5748{
5749 if (rettv_list_alloc(rettv) != FAIL)
5750 win_id2tabwin(argvars, rettv->vval.v_list);
5751}
5752
5753/*
5754 * "win_id2win()" function
5755 */
5756 static void
5757f_win_id2win(typval_T *argvars, typval_T *rettv)
5758{
5759 rettv->vval.v_number = win_id2win(argvars);
5760}
5761
5762/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005763 * "win_screenpos()" function
5764 */
5765 static void
5766f_win_screenpos(typval_T *argvars, typval_T *rettv)
5767{
5768 win_T *wp;
5769
5770 if (rettv_list_alloc(rettv) == FAIL)
5771 return;
5772
5773 wp = find_win_by_nr(&argvars[0], NULL);
5774 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5775 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5776}
5777
5778/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005779 * "getwinpos({timeout})" function
5780 */
5781 static void
5782f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
5783{
5784 int x = -1;
5785 int y = -1;
5786
5787 if (rettv_list_alloc(rettv) == FAIL)
5788 return;
5789#ifdef FEAT_GUI
5790 if (gui.in_use)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01005791 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005792# if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5793 else
5794# endif
5795#endif
5796#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5797 {
5798 varnumber_T timeout = 100;
5799
5800 if (argvars[0].v_type != VAR_UNKNOWN)
5801 timeout = get_tv_number(&argvars[0]);
5802 term_get_winpos(&x, &y, timeout);
5803 }
5804#endif
5805 list_append_number(rettv->vval.v_list, (varnumber_T)x);
5806 list_append_number(rettv->vval.v_list, (varnumber_T)y);
5807}
5808
5809
5810/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005811 * "getwinposx()" function
5812 */
5813 static void
5814f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5815{
5816 rettv->vval.v_number = -1;
5817#ifdef FEAT_GUI
5818 if (gui.in_use)
5819 {
5820 int x, y;
5821
5822 if (gui_mch_get_winpos(&x, &y) == OK)
5823 rettv->vval.v_number = x;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005824 return;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005825 }
5826#endif
5827#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5828 {
5829 int x, y;
5830
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005831 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005832 rettv->vval.v_number = x;
5833 }
5834#endif
5835}
5836
5837/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005838 * "getwinposy()" function
5839 */
5840 static void
5841f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5842{
5843 rettv->vval.v_number = -1;
5844#ifdef FEAT_GUI
5845 if (gui.in_use)
5846 {
5847 int x, y;
5848
5849 if (gui_mch_get_winpos(&x, &y) == OK)
5850 rettv->vval.v_number = y;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005851 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005852 }
5853#endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005854#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5855 {
5856 int x, y;
5857
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005858 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005859 rettv->vval.v_number = y;
5860 }
5861#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005862}
5863
5864/*
5865 * "getwinvar()" function
5866 */
5867 static void
5868f_getwinvar(typval_T *argvars, typval_T *rettv)
5869{
5870 getwinvar(argvars, rettv, 0);
5871}
5872
5873/*
5874 * "glob()" function
5875 */
5876 static void
5877f_glob(typval_T *argvars, typval_T *rettv)
5878{
5879 int options = WILD_SILENT|WILD_USE_NL;
5880 expand_T xpc;
5881 int error = FALSE;
5882
5883 /* When the optional second argument is non-zero, don't remove matches
5884 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5885 rettv->v_type = VAR_STRING;
5886 if (argvars[1].v_type != VAR_UNKNOWN)
5887 {
5888 if (get_tv_number_chk(&argvars[1], &error))
5889 options |= WILD_KEEP_ALL;
5890 if (argvars[2].v_type != VAR_UNKNOWN)
5891 {
5892 if (get_tv_number_chk(&argvars[2], &error))
5893 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005894 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005895 }
5896 if (argvars[3].v_type != VAR_UNKNOWN
5897 && get_tv_number_chk(&argvars[3], &error))
5898 options |= WILD_ALLLINKS;
5899 }
5900 }
5901 if (!error)
5902 {
5903 ExpandInit(&xpc);
5904 xpc.xp_context = EXPAND_FILES;
5905 if (p_wic)
5906 options += WILD_ICASE;
5907 if (rettv->v_type == VAR_STRING)
5908 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
5909 NULL, options, WILD_ALL);
5910 else if (rettv_list_alloc(rettv) != FAIL)
5911 {
5912 int i;
5913
5914 ExpandOne(&xpc, get_tv_string(&argvars[0]),
5915 NULL, options, WILD_ALL_KEEP);
5916 for (i = 0; i < xpc.xp_numfiles; i++)
5917 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5918
5919 ExpandCleanup(&xpc);
5920 }
5921 }
5922 else
5923 rettv->vval.v_string = NULL;
5924}
5925
5926/*
5927 * "globpath()" function
5928 */
5929 static void
5930f_globpath(typval_T *argvars, typval_T *rettv)
5931{
5932 int flags = 0;
5933 char_u buf1[NUMBUFLEN];
5934 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
5935 int error = FALSE;
5936 garray_T ga;
5937 int i;
5938
5939 /* When the optional second argument is non-zero, don't remove matches
5940 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5941 rettv->v_type = VAR_STRING;
5942 if (argvars[2].v_type != VAR_UNKNOWN)
5943 {
5944 if (get_tv_number_chk(&argvars[2], &error))
5945 flags |= WILD_KEEP_ALL;
5946 if (argvars[3].v_type != VAR_UNKNOWN)
5947 {
5948 if (get_tv_number_chk(&argvars[3], &error))
5949 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005950 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005951 }
5952 if (argvars[4].v_type != VAR_UNKNOWN
5953 && get_tv_number_chk(&argvars[4], &error))
5954 flags |= WILD_ALLLINKS;
5955 }
5956 }
5957 if (file != NULL && !error)
5958 {
5959 ga_init2(&ga, (int)sizeof(char_u *), 10);
5960 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
5961 if (rettv->v_type == VAR_STRING)
5962 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
5963 else if (rettv_list_alloc(rettv) != FAIL)
5964 for (i = 0; i < ga.ga_len; ++i)
5965 list_append_string(rettv->vval.v_list,
5966 ((char_u **)(ga.ga_data))[i], -1);
5967 ga_clear_strings(&ga);
5968 }
5969 else
5970 rettv->vval.v_string = NULL;
5971}
5972
5973/*
5974 * "glob2regpat()" function
5975 */
5976 static void
5977f_glob2regpat(typval_T *argvars, typval_T *rettv)
5978{
5979 char_u *pat = get_tv_string_chk(&argvars[0]);
5980
5981 rettv->v_type = VAR_STRING;
5982 rettv->vval.v_string = (pat == NULL)
5983 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
5984}
5985
5986/* for VIM_VERSION_ defines */
5987#include "version.h"
5988
5989/*
5990 * "has()" function
5991 */
5992 static void
5993f_has(typval_T *argvars, typval_T *rettv)
5994{
5995 int i;
5996 char_u *name;
5997 int n = FALSE;
5998 static char *(has_list[]) =
5999 {
6000#ifdef AMIGA
6001 "amiga",
6002# ifdef FEAT_ARP
6003 "arp",
6004# endif
6005#endif
6006#ifdef __BEOS__
6007 "beos",
6008#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02006009#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01006010 "mac", /* Mac OS X (and, once, Mac OS Classic) */
6011 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02006012# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01006013 "macunix", /* Mac OS X, with the darwin feature */
6014 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02006015# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006016#endif
6017#ifdef __QNX__
6018 "qnx",
6019#endif
6020#ifdef UNIX
6021 "unix",
6022#endif
6023#ifdef VMS
6024 "vms",
6025#endif
6026#ifdef WIN32
6027 "win32",
6028#endif
6029#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
6030 "win32unix",
6031#endif
6032#if defined(WIN64) || defined(_WIN64)
6033 "win64",
6034#endif
6035#ifdef EBCDIC
6036 "ebcdic",
6037#endif
6038#ifndef CASE_INSENSITIVE_FILENAME
6039 "fname_case",
6040#endif
6041#ifdef HAVE_ACL
6042 "acl",
6043#endif
6044#ifdef FEAT_ARABIC
6045 "arabic",
6046#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006047 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006048#ifdef FEAT_AUTOCHDIR
6049 "autochdir",
6050#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006051#ifdef FEAT_AUTOSERVERNAME
6052 "autoservername",
6053#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006054#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006055 "balloon_eval",
6056# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
6057 "balloon_multiline",
6058# endif
6059#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006060#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006061 "balloon_eval_term",
6062#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006063#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6064 "builtin_terms",
6065# ifdef ALL_BUILTIN_TCAPS
6066 "all_builtin_terms",
6067# endif
6068#endif
6069#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
6070 || defined(FEAT_GUI_W32) \
6071 || defined(FEAT_GUI_MOTIF))
6072 "browsefilter",
6073#endif
6074#ifdef FEAT_BYTEOFF
6075 "byte_offset",
6076#endif
6077#ifdef FEAT_JOB_CHANNEL
6078 "channel",
6079#endif
6080#ifdef FEAT_CINDENT
6081 "cindent",
6082#endif
6083#ifdef FEAT_CLIENTSERVER
6084 "clientserver",
6085#endif
6086#ifdef FEAT_CLIPBOARD
6087 "clipboard",
6088#endif
6089#ifdef FEAT_CMDL_COMPL
6090 "cmdline_compl",
6091#endif
6092#ifdef FEAT_CMDHIST
6093 "cmdline_hist",
6094#endif
6095#ifdef FEAT_COMMENTS
6096 "comments",
6097#endif
6098#ifdef FEAT_CONCEAL
6099 "conceal",
6100#endif
6101#ifdef FEAT_CRYPT
6102 "cryptv",
6103 "crypt-blowfish",
6104 "crypt-blowfish2",
6105#endif
6106#ifdef FEAT_CSCOPE
6107 "cscope",
6108#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006109 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006110#ifdef CURSOR_SHAPE
6111 "cursorshape",
6112#endif
6113#ifdef DEBUG
6114 "debug",
6115#endif
6116#ifdef FEAT_CON_DIALOG
6117 "dialog_con",
6118#endif
6119#ifdef FEAT_GUI_DIALOG
6120 "dialog_gui",
6121#endif
6122#ifdef FEAT_DIFF
6123 "diff",
6124#endif
6125#ifdef FEAT_DIGRAPHS
6126 "digraphs",
6127#endif
6128#ifdef FEAT_DIRECTX
6129 "directx",
6130#endif
6131#ifdef FEAT_DND
6132 "dnd",
6133#endif
6134#ifdef FEAT_EMACS_TAGS
6135 "emacs_tags",
6136#endif
6137 "eval", /* always present, of course! */
6138 "ex_extra", /* graduated feature */
6139#ifdef FEAT_SEARCH_EXTRA
6140 "extra_search",
6141#endif
6142#ifdef FEAT_FKMAP
6143 "farsi",
6144#endif
6145#ifdef FEAT_SEARCHPATH
6146 "file_in_path",
6147#endif
6148#ifdef FEAT_FILTERPIPE
6149 "filterpipe",
6150#endif
6151#ifdef FEAT_FIND_ID
6152 "find_in_path",
6153#endif
6154#ifdef FEAT_FLOAT
6155 "float",
6156#endif
6157#ifdef FEAT_FOLDING
6158 "folding",
6159#endif
6160#ifdef FEAT_FOOTER
6161 "footer",
6162#endif
6163#if !defined(USE_SYSTEM) && defined(UNIX)
6164 "fork",
6165#endif
6166#ifdef FEAT_GETTEXT
6167 "gettext",
6168#endif
6169#ifdef FEAT_GUI
6170 "gui",
6171#endif
6172#ifdef FEAT_GUI_ATHENA
6173# ifdef FEAT_GUI_NEXTAW
6174 "gui_neXtaw",
6175# else
6176 "gui_athena",
6177# endif
6178#endif
6179#ifdef FEAT_GUI_GTK
6180 "gui_gtk",
6181# ifdef USE_GTK3
6182 "gui_gtk3",
6183# else
6184 "gui_gtk2",
6185# endif
6186#endif
6187#ifdef FEAT_GUI_GNOME
6188 "gui_gnome",
6189#endif
6190#ifdef FEAT_GUI_MAC
6191 "gui_mac",
6192#endif
6193#ifdef FEAT_GUI_MOTIF
6194 "gui_motif",
6195#endif
6196#ifdef FEAT_GUI_PHOTON
6197 "gui_photon",
6198#endif
6199#ifdef FEAT_GUI_W32
6200 "gui_win32",
6201#endif
6202#ifdef FEAT_HANGULIN
6203 "hangul_input",
6204#endif
6205#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6206 "iconv",
6207#endif
6208#ifdef FEAT_INS_EXPAND
6209 "insert_expand",
6210#endif
6211#ifdef FEAT_JOB_CHANNEL
6212 "job",
6213#endif
6214#ifdef FEAT_JUMPLIST
6215 "jumplist",
6216#endif
6217#ifdef FEAT_KEYMAP
6218 "keymap",
6219#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006220 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006221#ifdef FEAT_LANGMAP
6222 "langmap",
6223#endif
6224#ifdef FEAT_LIBCALL
6225 "libcall",
6226#endif
6227#ifdef FEAT_LINEBREAK
6228 "linebreak",
6229#endif
6230#ifdef FEAT_LISP
6231 "lispindent",
6232#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006233 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006234#ifdef FEAT_LOCALMAP
6235 "localmap",
6236#endif
6237#ifdef FEAT_LUA
6238# ifndef DYNAMIC_LUA
6239 "lua",
6240# endif
6241#endif
6242#ifdef FEAT_MENU
6243 "menu",
6244#endif
6245#ifdef FEAT_SESSION
6246 "mksession",
6247#endif
6248#ifdef FEAT_MODIFY_FNAME
6249 "modify_fname",
6250#endif
6251#ifdef FEAT_MOUSE
6252 "mouse",
6253#endif
6254#ifdef FEAT_MOUSESHAPE
6255 "mouseshape",
6256#endif
6257#if defined(UNIX) || defined(VMS)
6258# ifdef FEAT_MOUSE_DEC
6259 "mouse_dec",
6260# endif
6261# ifdef FEAT_MOUSE_GPM
6262 "mouse_gpm",
6263# endif
6264# ifdef FEAT_MOUSE_JSB
6265 "mouse_jsbterm",
6266# endif
6267# ifdef FEAT_MOUSE_NET
6268 "mouse_netterm",
6269# endif
6270# ifdef FEAT_MOUSE_PTERM
6271 "mouse_pterm",
6272# endif
6273# ifdef FEAT_MOUSE_SGR
6274 "mouse_sgr",
6275# endif
6276# ifdef FEAT_SYSMOUSE
6277 "mouse_sysmouse",
6278# endif
6279# ifdef FEAT_MOUSE_URXVT
6280 "mouse_urxvt",
6281# endif
6282# ifdef FEAT_MOUSE_XTERM
6283 "mouse_xterm",
6284# endif
6285#endif
6286#ifdef FEAT_MBYTE
6287 "multi_byte",
6288#endif
6289#ifdef FEAT_MBYTE_IME
6290 "multi_byte_ime",
6291#endif
6292#ifdef FEAT_MULTI_LANG
6293 "multi_lang",
6294#endif
6295#ifdef FEAT_MZSCHEME
6296#ifndef DYNAMIC_MZSCHEME
6297 "mzscheme",
6298#endif
6299#endif
6300#ifdef FEAT_NUM64
6301 "num64",
6302#endif
6303#ifdef FEAT_OLE
6304 "ole",
6305#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006306#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006307 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006308#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006309#ifdef FEAT_PATH_EXTRA
6310 "path_extra",
6311#endif
6312#ifdef FEAT_PERL
6313#ifndef DYNAMIC_PERL
6314 "perl",
6315#endif
6316#endif
6317#ifdef FEAT_PERSISTENT_UNDO
6318 "persistent_undo",
6319#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006320#if defined(FEAT_PYTHON)
6321 "python_compiled",
6322# if defined(DYNAMIC_PYTHON)
6323 "python_dynamic",
6324# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006325 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006326 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006327# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006328#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006329#if defined(FEAT_PYTHON3)
6330 "python3_compiled",
6331# if defined(DYNAMIC_PYTHON3)
6332 "python3_dynamic",
6333# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006334 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006335 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006336# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006337#endif
6338#ifdef FEAT_POSTSCRIPT
6339 "postscript",
6340#endif
6341#ifdef FEAT_PRINTER
6342 "printer",
6343#endif
6344#ifdef FEAT_PROFILE
6345 "profile",
6346#endif
6347#ifdef FEAT_RELTIME
6348 "reltime",
6349#endif
6350#ifdef FEAT_QUICKFIX
6351 "quickfix",
6352#endif
6353#ifdef FEAT_RIGHTLEFT
6354 "rightleft",
6355#endif
6356#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6357 "ruby",
6358#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006359 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006360#ifdef FEAT_CMDL_INFO
6361 "showcmd",
6362 "cmdline_info",
6363#endif
6364#ifdef FEAT_SIGNS
6365 "signs",
6366#endif
6367#ifdef FEAT_SMARTINDENT
6368 "smartindent",
6369#endif
6370#ifdef STARTUPTIME
6371 "startuptime",
6372#endif
6373#ifdef FEAT_STL_OPT
6374 "statusline",
6375#endif
6376#ifdef FEAT_SUN_WORKSHOP
6377 "sun_workshop",
6378#endif
6379#ifdef FEAT_NETBEANS_INTG
6380 "netbeans_intg",
6381#endif
6382#ifdef FEAT_SPELL
6383 "spell",
6384#endif
6385#ifdef FEAT_SYN_HL
6386 "syntax",
6387#endif
6388#if defined(USE_SYSTEM) || !defined(UNIX)
6389 "system",
6390#endif
6391#ifdef FEAT_TAG_BINS
6392 "tag_binary",
6393#endif
6394#ifdef FEAT_TAG_OLDSTATIC
6395 "tag_old_static",
6396#endif
6397#ifdef FEAT_TAG_ANYWHITE
6398 "tag_any_white",
6399#endif
6400#ifdef FEAT_TCL
6401# ifndef DYNAMIC_TCL
6402 "tcl",
6403# endif
6404#endif
6405#ifdef FEAT_TERMGUICOLORS
6406 "termguicolors",
6407#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006408#if defined(FEAT_TERMINAL) && !defined(WIN3264)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006409 "terminal",
6410#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006411#ifdef TERMINFO
6412 "terminfo",
6413#endif
6414#ifdef FEAT_TERMRESPONSE
6415 "termresponse",
6416#endif
6417#ifdef FEAT_TEXTOBJ
6418 "textobjects",
6419#endif
6420#ifdef HAVE_TGETENT
6421 "tgetent",
6422#endif
6423#ifdef FEAT_TIMERS
6424 "timers",
6425#endif
6426#ifdef FEAT_TITLE
6427 "title",
6428#endif
6429#ifdef FEAT_TOOLBAR
6430 "toolbar",
6431#endif
6432#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6433 "unnamedplus",
6434#endif
6435#ifdef FEAT_USR_CMDS
6436 "user-commands", /* was accidentally included in 5.4 */
6437 "user_commands",
6438#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006439#ifdef FEAT_VARTABS
6440 "vartabs",
6441#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006442#ifdef FEAT_VIMINFO
6443 "viminfo",
6444#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006445 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006446#ifdef FEAT_VIRTUALEDIT
6447 "virtualedit",
6448#endif
6449 "visual",
6450#ifdef FEAT_VISUALEXTRA
6451 "visualextra",
6452#endif
6453#ifdef FEAT_VREPLACE
6454 "vreplace",
6455#endif
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006456#ifdef FEAT_VTP
6457 "vtp",
6458#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006459#ifdef FEAT_WILDIGN
6460 "wildignore",
6461#endif
6462#ifdef FEAT_WILDMENU
6463 "wildmenu",
6464#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006465 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006466#ifdef FEAT_WAK
6467 "winaltkeys",
6468#endif
6469#ifdef FEAT_WRITEBACKUP
6470 "writebackup",
6471#endif
6472#ifdef FEAT_XIM
6473 "xim",
6474#endif
6475#ifdef FEAT_XFONTSET
6476 "xfontset",
6477#endif
6478#ifdef FEAT_XPM_W32
6479 "xpm",
6480 "xpm_w32", /* for backward compatibility */
6481#else
6482# if defined(HAVE_XPM)
6483 "xpm",
6484# endif
6485#endif
6486#ifdef USE_XSMP
6487 "xsmp",
6488#endif
6489#ifdef USE_XSMP_INTERACT
6490 "xsmp_interact",
6491#endif
6492#ifdef FEAT_XCLIPBOARD
6493 "xterm_clipboard",
6494#endif
6495#ifdef FEAT_XTERM_SAVE
6496 "xterm_save",
6497#endif
6498#if defined(UNIX) && defined(FEAT_X11)
6499 "X11",
6500#endif
6501 NULL
6502 };
6503
6504 name = get_tv_string(&argvars[0]);
6505 for (i = 0; has_list[i] != NULL; ++i)
6506 if (STRICMP(name, has_list[i]) == 0)
6507 {
6508 n = TRUE;
6509 break;
6510 }
6511
6512 if (n == FALSE)
6513 {
6514 if (STRNICMP(name, "patch", 5) == 0)
6515 {
6516 if (name[5] == '-'
6517 && STRLEN(name) >= 11
6518 && vim_isdigit(name[6])
6519 && vim_isdigit(name[8])
6520 && vim_isdigit(name[10]))
6521 {
6522 int major = atoi((char *)name + 6);
6523 int minor = atoi((char *)name + 8);
6524
6525 /* Expect "patch-9.9.01234". */
6526 n = (major < VIM_VERSION_MAJOR
6527 || (major == VIM_VERSION_MAJOR
6528 && (minor < VIM_VERSION_MINOR
6529 || (minor == VIM_VERSION_MINOR
6530 && has_patch(atoi((char *)name + 10))))));
6531 }
6532 else
6533 n = has_patch(atoi((char *)name + 5));
6534 }
6535 else if (STRICMP(name, "vim_starting") == 0)
6536 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006537 else if (STRICMP(name, "ttyin") == 0)
6538 n = mch_input_isatty();
6539 else if (STRICMP(name, "ttyout") == 0)
6540 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006541#ifdef FEAT_MBYTE
6542 else if (STRICMP(name, "multi_byte_encoding") == 0)
6543 n = has_mbyte;
6544#endif
6545#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
6546 else if (STRICMP(name, "balloon_multiline") == 0)
6547 n = multiline_balloon_available();
6548#endif
6549#ifdef DYNAMIC_TCL
6550 else if (STRICMP(name, "tcl") == 0)
6551 n = tcl_enabled(FALSE);
6552#endif
6553#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6554 else if (STRICMP(name, "iconv") == 0)
6555 n = iconv_enabled(FALSE);
6556#endif
6557#ifdef DYNAMIC_LUA
6558 else if (STRICMP(name, "lua") == 0)
6559 n = lua_enabled(FALSE);
6560#endif
6561#ifdef DYNAMIC_MZSCHEME
6562 else if (STRICMP(name, "mzscheme") == 0)
6563 n = mzscheme_enabled(FALSE);
6564#endif
6565#ifdef DYNAMIC_RUBY
6566 else if (STRICMP(name, "ruby") == 0)
6567 n = ruby_enabled(FALSE);
6568#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006569#ifdef DYNAMIC_PYTHON
6570 else if (STRICMP(name, "python") == 0)
6571 n = python_enabled(FALSE);
6572#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006573#ifdef DYNAMIC_PYTHON3
6574 else if (STRICMP(name, "python3") == 0)
6575 n = python3_enabled(FALSE);
6576#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006577#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6578 else if (STRICMP(name, "pythonx") == 0)
6579 {
6580# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6581 if (p_pyx == 0)
6582 n = python3_enabled(FALSE) || python_enabled(FALSE);
6583 else if (p_pyx == 3)
6584 n = python3_enabled(FALSE);
6585 else if (p_pyx == 2)
6586 n = python_enabled(FALSE);
6587# elif defined(DYNAMIC_PYTHON)
6588 n = python_enabled(FALSE);
6589# elif defined(DYNAMIC_PYTHON3)
6590 n = python3_enabled(FALSE);
6591# endif
6592 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006593#endif
6594#ifdef DYNAMIC_PERL
6595 else if (STRICMP(name, "perl") == 0)
6596 n = perl_enabled(FALSE);
6597#endif
6598#ifdef FEAT_GUI
6599 else if (STRICMP(name, "gui_running") == 0)
6600 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006601# ifdef FEAT_BROWSE
6602 else if (STRICMP(name, "browse") == 0)
6603 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6604# endif
6605#endif
6606#ifdef FEAT_SYN_HL
6607 else if (STRICMP(name, "syntax_items") == 0)
6608 n = syntax_present(curwin);
6609#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006610#ifdef FEAT_VTP
6611 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02006612 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006613#endif
6614#ifdef FEAT_NETBEANS_INTG
6615 else if (STRICMP(name, "netbeans_enabled") == 0)
6616 n = netbeans_active();
6617#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006618#if defined(FEAT_TERMINAL) && defined(WIN3264)
6619 else if (STRICMP(name, "terminal") == 0)
6620 n = terminal_enabled();
6621#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006622 }
6623
6624 rettv->vval.v_number = n;
6625}
6626
6627/*
6628 * "has_key()" function
6629 */
6630 static void
6631f_has_key(typval_T *argvars, typval_T *rettv)
6632{
6633 if (argvars[0].v_type != VAR_DICT)
6634 {
6635 EMSG(_(e_dictreq));
6636 return;
6637 }
6638 if (argvars[0].vval.v_dict == NULL)
6639 return;
6640
6641 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
6642 get_tv_string(&argvars[1]), -1) != NULL;
6643}
6644
6645/*
6646 * "haslocaldir()" function
6647 */
6648 static void
6649f_haslocaldir(typval_T *argvars, typval_T *rettv)
6650{
6651 win_T *wp = NULL;
6652
6653 wp = find_tabwin(&argvars[0], &argvars[1]);
6654 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6655}
6656
6657/*
6658 * "hasmapto()" function
6659 */
6660 static void
6661f_hasmapto(typval_T *argvars, typval_T *rettv)
6662{
6663 char_u *name;
6664 char_u *mode;
6665 char_u buf[NUMBUFLEN];
6666 int abbr = FALSE;
6667
6668 name = get_tv_string(&argvars[0]);
6669 if (argvars[1].v_type == VAR_UNKNOWN)
6670 mode = (char_u *)"nvo";
6671 else
6672 {
6673 mode = get_tv_string_buf(&argvars[1], buf);
6674 if (argvars[2].v_type != VAR_UNKNOWN)
6675 abbr = (int)get_tv_number(&argvars[2]);
6676 }
6677
6678 if (map_to_exists(name, mode, abbr))
6679 rettv->vval.v_number = TRUE;
6680 else
6681 rettv->vval.v_number = FALSE;
6682}
6683
6684/*
6685 * "histadd()" function
6686 */
6687 static void
6688f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6689{
6690#ifdef FEAT_CMDHIST
6691 int histype;
6692 char_u *str;
6693 char_u buf[NUMBUFLEN];
6694#endif
6695
6696 rettv->vval.v_number = FALSE;
6697 if (check_restricted() || check_secure())
6698 return;
6699#ifdef FEAT_CMDHIST
6700 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6701 histype = str != NULL ? get_histtype(str) : -1;
6702 if (histype >= 0)
6703 {
6704 str = get_tv_string_buf(&argvars[1], buf);
6705 if (*str != NUL)
6706 {
6707 init_history();
6708 add_to_history(histype, str, FALSE, NUL);
6709 rettv->vval.v_number = TRUE;
6710 return;
6711 }
6712 }
6713#endif
6714}
6715
6716/*
6717 * "histdel()" function
6718 */
6719 static void
6720f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6721{
6722#ifdef FEAT_CMDHIST
6723 int n;
6724 char_u buf[NUMBUFLEN];
6725 char_u *str;
6726
6727 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6728 if (str == NULL)
6729 n = 0;
6730 else if (argvars[1].v_type == VAR_UNKNOWN)
6731 /* only one argument: clear entire history */
6732 n = clr_history(get_histtype(str));
6733 else if (argvars[1].v_type == VAR_NUMBER)
6734 /* index given: remove that entry */
6735 n = del_history_idx(get_histtype(str),
6736 (int)get_tv_number(&argvars[1]));
6737 else
6738 /* string given: remove all matching entries */
6739 n = del_history_entry(get_histtype(str),
6740 get_tv_string_buf(&argvars[1], buf));
6741 rettv->vval.v_number = n;
6742#endif
6743}
6744
6745/*
6746 * "histget()" function
6747 */
6748 static void
6749f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6750{
6751#ifdef FEAT_CMDHIST
6752 int type;
6753 int idx;
6754 char_u *str;
6755
6756 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6757 if (str == NULL)
6758 rettv->vval.v_string = NULL;
6759 else
6760 {
6761 type = get_histtype(str);
6762 if (argvars[1].v_type == VAR_UNKNOWN)
6763 idx = get_history_idx(type);
6764 else
6765 idx = (int)get_tv_number_chk(&argvars[1], NULL);
6766 /* -1 on type error */
6767 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6768 }
6769#else
6770 rettv->vval.v_string = NULL;
6771#endif
6772 rettv->v_type = VAR_STRING;
6773}
6774
6775/*
6776 * "histnr()" function
6777 */
6778 static void
6779f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6780{
6781 int i;
6782
6783#ifdef FEAT_CMDHIST
6784 char_u *history = get_tv_string_chk(&argvars[0]);
6785
6786 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6787 if (i >= HIST_CMD && i < HIST_COUNT)
6788 i = get_history_idx(i);
6789 else
6790#endif
6791 i = -1;
6792 rettv->vval.v_number = i;
6793}
6794
6795/*
6796 * "highlightID(name)" function
6797 */
6798 static void
6799f_hlID(typval_T *argvars, typval_T *rettv)
6800{
6801 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
6802}
6803
6804/*
6805 * "highlight_exists()" function
6806 */
6807 static void
6808f_hlexists(typval_T *argvars, typval_T *rettv)
6809{
6810 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
6811}
6812
6813/*
6814 * "hostname()" function
6815 */
6816 static void
6817f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6818{
6819 char_u hostname[256];
6820
6821 mch_get_host_name(hostname, 256);
6822 rettv->v_type = VAR_STRING;
6823 rettv->vval.v_string = vim_strsave(hostname);
6824}
6825
6826/*
6827 * iconv() function
6828 */
6829 static void
6830f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6831{
6832#ifdef FEAT_MBYTE
6833 char_u buf1[NUMBUFLEN];
6834 char_u buf2[NUMBUFLEN];
6835 char_u *from, *to, *str;
6836 vimconv_T vimconv;
6837#endif
6838
6839 rettv->v_type = VAR_STRING;
6840 rettv->vval.v_string = NULL;
6841
6842#ifdef FEAT_MBYTE
6843 str = get_tv_string(&argvars[0]);
6844 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
6845 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
6846 vimconv.vc_type = CONV_NONE;
6847 convert_setup(&vimconv, from, to);
6848
6849 /* If the encodings are equal, no conversion needed. */
6850 if (vimconv.vc_type == CONV_NONE)
6851 rettv->vval.v_string = vim_strsave(str);
6852 else
6853 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6854
6855 convert_setup(&vimconv, NULL, NULL);
6856 vim_free(from);
6857 vim_free(to);
6858#endif
6859}
6860
6861/*
6862 * "indent()" function
6863 */
6864 static void
6865f_indent(typval_T *argvars, typval_T *rettv)
6866{
6867 linenr_T lnum;
6868
6869 lnum = get_tv_lnum(argvars);
6870 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6871 rettv->vval.v_number = get_indent_lnum(lnum);
6872 else
6873 rettv->vval.v_number = -1;
6874}
6875
6876/*
6877 * "index()" function
6878 */
6879 static void
6880f_index(typval_T *argvars, typval_T *rettv)
6881{
6882 list_T *l;
6883 listitem_T *item;
6884 long idx = 0;
6885 int ic = FALSE;
6886
6887 rettv->vval.v_number = -1;
6888 if (argvars[0].v_type != VAR_LIST)
6889 {
6890 EMSG(_(e_listreq));
6891 return;
6892 }
6893 l = argvars[0].vval.v_list;
6894 if (l != NULL)
6895 {
6896 item = l->lv_first;
6897 if (argvars[2].v_type != VAR_UNKNOWN)
6898 {
6899 int error = FALSE;
6900
6901 /* Start at specified item. Use the cached index that list_find()
6902 * sets, so that a negative number also works. */
6903 item = list_find(l, (long)get_tv_number_chk(&argvars[2], &error));
6904 idx = l->lv_idx;
6905 if (argvars[3].v_type != VAR_UNKNOWN)
6906 ic = (int)get_tv_number_chk(&argvars[3], &error);
6907 if (error)
6908 item = NULL;
6909 }
6910
6911 for ( ; item != NULL; item = item->li_next, ++idx)
6912 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6913 {
6914 rettv->vval.v_number = idx;
6915 break;
6916 }
6917 }
6918}
6919
6920static int inputsecret_flag = 0;
6921
6922/*
6923 * "input()" function
6924 * Also handles inputsecret() when inputsecret is set.
6925 */
6926 static void
6927f_input(typval_T *argvars, typval_T *rettv)
6928{
6929 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6930}
6931
6932/*
6933 * "inputdialog()" function
6934 */
6935 static void
6936f_inputdialog(typval_T *argvars, typval_T *rettv)
6937{
6938#if defined(FEAT_GUI_TEXTDIALOG)
6939 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
6940 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6941 {
6942 char_u *message;
6943 char_u buf[NUMBUFLEN];
6944 char_u *defstr = (char_u *)"";
6945
6946 message = get_tv_string_chk(&argvars[0]);
6947 if (argvars[1].v_type != VAR_UNKNOWN
6948 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
6949 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6950 else
6951 IObuff[0] = NUL;
6952 if (message != NULL && defstr != NULL
6953 && do_dialog(VIM_QUESTION, NULL, message,
6954 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6955 rettv->vval.v_string = vim_strsave(IObuff);
6956 else
6957 {
6958 if (message != NULL && defstr != NULL
6959 && argvars[1].v_type != VAR_UNKNOWN
6960 && argvars[2].v_type != VAR_UNKNOWN)
6961 rettv->vval.v_string = vim_strsave(
6962 get_tv_string_buf(&argvars[2], buf));
6963 else
6964 rettv->vval.v_string = NULL;
6965 }
6966 rettv->v_type = VAR_STRING;
6967 }
6968 else
6969#endif
6970 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6971}
6972
6973/*
6974 * "inputlist()" function
6975 */
6976 static void
6977f_inputlist(typval_T *argvars, typval_T *rettv)
6978{
6979 listitem_T *li;
6980 int selected;
6981 int mouse_used;
6982
6983#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02006984 /* While starting up, there is no place to enter text. When running tests
6985 * with --not-a-term we assume feedkeys() will be used. */
6986 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006987 return;
6988#endif
6989 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6990 {
6991 EMSG2(_(e_listarg), "inputlist()");
6992 return;
6993 }
6994
6995 msg_start();
6996 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
6997 lines_left = Rows; /* avoid more prompt */
6998 msg_scroll = TRUE;
6999 msg_clr_eos();
7000
7001 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
7002 {
7003 msg_puts(get_tv_string(&li->li_tv));
7004 msg_putchar('\n');
7005 }
7006
7007 /* Ask for choice. */
7008 selected = prompt_for_number(&mouse_used);
7009 if (mouse_used)
7010 selected -= lines_left;
7011
7012 rettv->vval.v_number = selected;
7013}
7014
7015
7016static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7017
7018/*
7019 * "inputrestore()" function
7020 */
7021 static void
7022f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7023{
7024 if (ga_userinput.ga_len > 0)
7025 {
7026 --ga_userinput.ga_len;
7027 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7028 + ga_userinput.ga_len);
7029 /* default return is zero == OK */
7030 }
7031 else if (p_verbose > 1)
7032 {
7033 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
7034 rettv->vval.v_number = 1; /* Failed */
7035 }
7036}
7037
7038/*
7039 * "inputsave()" function
7040 */
7041 static void
7042f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7043{
7044 /* Add an entry to the stack of typeahead storage. */
7045 if (ga_grow(&ga_userinput, 1) == OK)
7046 {
7047 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7048 + ga_userinput.ga_len);
7049 ++ga_userinput.ga_len;
7050 /* default return is zero == OK */
7051 }
7052 else
7053 rettv->vval.v_number = 1; /* Failed */
7054}
7055
7056/*
7057 * "inputsecret()" function
7058 */
7059 static void
7060f_inputsecret(typval_T *argvars, typval_T *rettv)
7061{
7062 ++cmdline_star;
7063 ++inputsecret_flag;
7064 f_input(argvars, rettv);
7065 --cmdline_star;
7066 --inputsecret_flag;
7067}
7068
7069/*
7070 * "insert()" function
7071 */
7072 static void
7073f_insert(typval_T *argvars, typval_T *rettv)
7074{
7075 long before = 0;
7076 listitem_T *item;
7077 list_T *l;
7078 int error = FALSE;
7079
7080 if (argvars[0].v_type != VAR_LIST)
7081 EMSG2(_(e_listarg), "insert()");
7082 else if ((l = argvars[0].vval.v_list) != NULL
7083 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
7084 {
7085 if (argvars[2].v_type != VAR_UNKNOWN)
7086 before = (long)get_tv_number_chk(&argvars[2], &error);
7087 if (error)
7088 return; /* type error; errmsg already given */
7089
7090 if (before == l->lv_len)
7091 item = NULL;
7092 else
7093 {
7094 item = list_find(l, before);
7095 if (item == NULL)
7096 {
7097 EMSGN(_(e_listidx), before);
7098 l = NULL;
7099 }
7100 }
7101 if (l != NULL)
7102 {
7103 list_insert_tv(l, &argvars[1], item);
7104 copy_tv(&argvars[0], rettv);
7105 }
7106 }
7107}
7108
7109/*
7110 * "invert(expr)" function
7111 */
7112 static void
7113f_invert(typval_T *argvars, typval_T *rettv)
7114{
7115 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
7116}
7117
7118/*
7119 * "isdirectory()" function
7120 */
7121 static void
7122f_isdirectory(typval_T *argvars, typval_T *rettv)
7123{
7124 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
7125}
7126
7127/*
7128 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7129 * or it refers to a List or Dictionary that is locked.
7130 */
7131 static int
7132tv_islocked(typval_T *tv)
7133{
7134 return (tv->v_lock & VAR_LOCKED)
7135 || (tv->v_type == VAR_LIST
7136 && tv->vval.v_list != NULL
7137 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7138 || (tv->v_type == VAR_DICT
7139 && tv->vval.v_dict != NULL
7140 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7141}
7142
7143/*
7144 * "islocked()" function
7145 */
7146 static void
7147f_islocked(typval_T *argvars, typval_T *rettv)
7148{
7149 lval_T lv;
7150 char_u *end;
7151 dictitem_T *di;
7152
7153 rettv->vval.v_number = -1;
7154 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007155 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007156 if (end != NULL && lv.ll_name != NULL)
7157 {
7158 if (*end != NUL)
7159 EMSG(_(e_trailing));
7160 else
7161 {
7162 if (lv.ll_tv == NULL)
7163 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007164 di = find_var(lv.ll_name, NULL, TRUE);
7165 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007166 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007167 /* Consider a variable locked when:
7168 * 1. the variable itself is locked
7169 * 2. the value of the variable is locked.
7170 * 3. the List or Dict value is locked.
7171 */
7172 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7173 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007174 }
7175 }
7176 else if (lv.ll_range)
7177 EMSG(_("E786: Range not allowed"));
7178 else if (lv.ll_newkey != NULL)
7179 EMSG2(_(e_dictkey), lv.ll_newkey);
7180 else if (lv.ll_list != NULL)
7181 /* List item. */
7182 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7183 else
7184 /* Dictionary item. */
7185 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7186 }
7187 }
7188
7189 clear_lval(&lv);
7190}
7191
7192#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7193/*
7194 * "isnan()" function
7195 */
7196 static void
7197f_isnan(typval_T *argvars, typval_T *rettv)
7198{
7199 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7200 && isnan(argvars[0].vval.v_float);
7201}
7202#endif
7203
7204/*
7205 * "items(dict)" function
7206 */
7207 static void
7208f_items(typval_T *argvars, typval_T *rettv)
7209{
7210 dict_list(argvars, rettv, 2);
7211}
7212
7213#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
7214/*
7215 * Get the job from the argument.
7216 * Returns NULL if the job is invalid.
7217 */
7218 static job_T *
7219get_job_arg(typval_T *tv)
7220{
7221 job_T *job;
7222
7223 if (tv->v_type != VAR_JOB)
7224 {
7225 EMSG2(_(e_invarg2), get_tv_string(tv));
7226 return NULL;
7227 }
7228 job = tv->vval.v_job;
7229
7230 if (job == NULL)
7231 EMSG(_("E916: not a valid job"));
7232 return job;
7233}
7234
7235/*
7236 * "job_getchannel()" function
7237 */
7238 static void
7239f_job_getchannel(typval_T *argvars, typval_T *rettv)
7240{
7241 job_T *job = get_job_arg(&argvars[0]);
7242
7243 if (job != NULL)
7244 {
7245 rettv->v_type = VAR_CHANNEL;
7246 rettv->vval.v_channel = job->jv_channel;
7247 if (job->jv_channel != NULL)
7248 ++job->jv_channel->ch_refcount;
7249 }
7250}
7251
7252/*
7253 * "job_info()" function
7254 */
7255 static void
7256f_job_info(typval_T *argvars, typval_T *rettv)
7257{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007258 if (argvars[0].v_type != VAR_UNKNOWN)
7259 {
7260 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007261
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007262 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7263 job_info(job, rettv->vval.v_dict);
7264 }
7265 else if (rettv_list_alloc(rettv) == OK)
7266 job_info_all(rettv->vval.v_list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007267}
7268
7269/*
7270 * "job_setoptions()" function
7271 */
7272 static void
7273f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7274{
7275 job_T *job = get_job_arg(&argvars[0]);
7276 jobopt_T opt;
7277
7278 if (job == NULL)
7279 return;
7280 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007281 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007282 job_set_options(job, &opt);
7283 free_job_options(&opt);
7284}
7285
7286/*
7287 * "job_start()" function
7288 */
7289 static void
7290f_job_start(typval_T *argvars, typval_T *rettv)
7291{
7292 rettv->v_type = VAR_JOB;
7293 if (check_restricted() || check_secure())
7294 return;
Bram Moolenaar493359e2018-06-12 20:25:52 +02007295 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007296}
7297
7298/*
7299 * "job_status()" function
7300 */
7301 static void
7302f_job_status(typval_T *argvars, typval_T *rettv)
7303{
7304 job_T *job = get_job_arg(&argvars[0]);
7305
7306 if (job != NULL)
7307 {
7308 rettv->v_type = VAR_STRING;
7309 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7310 }
7311}
7312
7313/*
7314 * "job_stop()" function
7315 */
7316 static void
7317f_job_stop(typval_T *argvars, typval_T *rettv)
7318{
7319 job_T *job = get_job_arg(&argvars[0]);
7320
7321 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007322 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007323}
7324#endif
7325
7326/*
7327 * "join()" function
7328 */
7329 static void
7330f_join(typval_T *argvars, typval_T *rettv)
7331{
7332 garray_T ga;
7333 char_u *sep;
7334
7335 if (argvars[0].v_type != VAR_LIST)
7336 {
7337 EMSG(_(e_listreq));
7338 return;
7339 }
7340 if (argvars[0].vval.v_list == NULL)
7341 return;
7342 if (argvars[1].v_type == VAR_UNKNOWN)
7343 sep = (char_u *)" ";
7344 else
7345 sep = get_tv_string_chk(&argvars[1]);
7346
7347 rettv->v_type = VAR_STRING;
7348
7349 if (sep != NULL)
7350 {
7351 ga_init2(&ga, (int)sizeof(char), 80);
7352 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7353 ga_append(&ga, NUL);
7354 rettv->vval.v_string = (char_u *)ga.ga_data;
7355 }
7356 else
7357 rettv->vval.v_string = NULL;
7358}
7359
7360/*
7361 * "js_decode()" function
7362 */
7363 static void
7364f_js_decode(typval_T *argvars, typval_T *rettv)
7365{
7366 js_read_T reader;
7367
7368 reader.js_buf = get_tv_string(&argvars[0]);
7369 reader.js_fill = NULL;
7370 reader.js_used = 0;
7371 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
7372 EMSG(_(e_invarg));
7373}
7374
7375/*
7376 * "js_encode()" function
7377 */
7378 static void
7379f_js_encode(typval_T *argvars, typval_T *rettv)
7380{
7381 rettv->v_type = VAR_STRING;
7382 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7383}
7384
7385/*
7386 * "json_decode()" function
7387 */
7388 static void
7389f_json_decode(typval_T *argvars, typval_T *rettv)
7390{
7391 js_read_T reader;
7392
7393 reader.js_buf = get_tv_string(&argvars[0]);
7394 reader.js_fill = NULL;
7395 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007396 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007397}
7398
7399/*
7400 * "json_encode()" function
7401 */
7402 static void
7403f_json_encode(typval_T *argvars, typval_T *rettv)
7404{
7405 rettv->v_type = VAR_STRING;
7406 rettv->vval.v_string = json_encode(&argvars[0], 0);
7407}
7408
7409/*
7410 * "keys()" function
7411 */
7412 static void
7413f_keys(typval_T *argvars, typval_T *rettv)
7414{
7415 dict_list(argvars, rettv, 0);
7416}
7417
7418/*
7419 * "last_buffer_nr()" function.
7420 */
7421 static void
7422f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7423{
7424 int n = 0;
7425 buf_T *buf;
7426
Bram Moolenaar29323592016-07-24 22:04:11 +02007427 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007428 if (n < buf->b_fnum)
7429 n = buf->b_fnum;
7430
7431 rettv->vval.v_number = n;
7432}
7433
7434/*
7435 * "len()" function
7436 */
7437 static void
7438f_len(typval_T *argvars, typval_T *rettv)
7439{
7440 switch (argvars[0].v_type)
7441 {
7442 case VAR_STRING:
7443 case VAR_NUMBER:
7444 rettv->vval.v_number = (varnumber_T)STRLEN(
7445 get_tv_string(&argvars[0]));
7446 break;
7447 case VAR_LIST:
7448 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7449 break;
7450 case VAR_DICT:
7451 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7452 break;
7453 case VAR_UNKNOWN:
7454 case VAR_SPECIAL:
7455 case VAR_FLOAT:
7456 case VAR_FUNC:
7457 case VAR_PARTIAL:
7458 case VAR_JOB:
7459 case VAR_CHANNEL:
7460 EMSG(_("E701: Invalid type for len()"));
7461 break;
7462 }
7463}
7464
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007465 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007466libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007467{
7468#ifdef FEAT_LIBCALL
7469 char_u *string_in;
7470 char_u **string_result;
7471 int nr_result;
7472#endif
7473
7474 rettv->v_type = type;
7475 if (type != VAR_NUMBER)
7476 rettv->vval.v_string = NULL;
7477
7478 if (check_restricted() || check_secure())
7479 return;
7480
7481#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007482 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007483 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7484 {
7485 string_in = NULL;
7486 if (argvars[2].v_type == VAR_STRING)
7487 string_in = argvars[2].vval.v_string;
7488 if (type == VAR_NUMBER)
7489 string_result = NULL;
7490 else
7491 string_result = &rettv->vval.v_string;
7492 if (mch_libcall(argvars[0].vval.v_string,
7493 argvars[1].vval.v_string,
7494 string_in,
7495 argvars[2].vval.v_number,
7496 string_result,
7497 &nr_result) == OK
7498 && type == VAR_NUMBER)
7499 rettv->vval.v_number = nr_result;
7500 }
7501#endif
7502}
7503
7504/*
7505 * "libcall()" function
7506 */
7507 static void
7508f_libcall(typval_T *argvars, typval_T *rettv)
7509{
7510 libcall_common(argvars, rettv, VAR_STRING);
7511}
7512
7513/*
7514 * "libcallnr()" function
7515 */
7516 static void
7517f_libcallnr(typval_T *argvars, typval_T *rettv)
7518{
7519 libcall_common(argvars, rettv, VAR_NUMBER);
7520}
7521
7522/*
7523 * "line(string)" function
7524 */
7525 static void
7526f_line(typval_T *argvars, typval_T *rettv)
7527{
7528 linenr_T lnum = 0;
7529 pos_T *fp;
7530 int fnum;
7531
7532 fp = var2fpos(&argvars[0], TRUE, &fnum);
7533 if (fp != NULL)
7534 lnum = fp->lnum;
7535 rettv->vval.v_number = lnum;
7536}
7537
7538/*
7539 * "line2byte(lnum)" function
7540 */
7541 static void
7542f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7543{
7544#ifndef FEAT_BYTEOFF
7545 rettv->vval.v_number = -1;
7546#else
7547 linenr_T lnum;
7548
7549 lnum = get_tv_lnum(argvars);
7550 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7551 rettv->vval.v_number = -1;
7552 else
7553 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7554 if (rettv->vval.v_number >= 0)
7555 ++rettv->vval.v_number;
7556#endif
7557}
7558
7559/*
7560 * "lispindent(lnum)" function
7561 */
7562 static void
7563f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7564{
7565#ifdef FEAT_LISP
7566 pos_T pos;
7567 linenr_T lnum;
7568
7569 pos = curwin->w_cursor;
7570 lnum = get_tv_lnum(argvars);
7571 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7572 {
7573 curwin->w_cursor.lnum = lnum;
7574 rettv->vval.v_number = get_lisp_indent();
7575 curwin->w_cursor = pos;
7576 }
7577 else
7578#endif
7579 rettv->vval.v_number = -1;
7580}
7581
7582/*
7583 * "localtime()" function
7584 */
7585 static void
7586f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7587{
7588 rettv->vval.v_number = (varnumber_T)time(NULL);
7589}
7590
7591static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
7592
7593 static void
7594get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7595{
7596 char_u *keys;
7597 char_u *which;
7598 char_u buf[NUMBUFLEN];
7599 char_u *keys_buf = NULL;
7600 char_u *rhs;
7601 int mode;
7602 int abbr = FALSE;
7603 int get_dict = FALSE;
7604 mapblock_T *mp;
7605 int buffer_local;
7606
7607 /* return empty string for failure */
7608 rettv->v_type = VAR_STRING;
7609 rettv->vval.v_string = NULL;
7610
7611 keys = get_tv_string(&argvars[0]);
7612 if (*keys == NUL)
7613 return;
7614
7615 if (argvars[1].v_type != VAR_UNKNOWN)
7616 {
7617 which = get_tv_string_buf_chk(&argvars[1], buf);
7618 if (argvars[2].v_type != VAR_UNKNOWN)
7619 {
7620 abbr = (int)get_tv_number(&argvars[2]);
7621 if (argvars[3].v_type != VAR_UNKNOWN)
7622 get_dict = (int)get_tv_number(&argvars[3]);
7623 }
7624 }
7625 else
7626 which = (char_u *)"";
7627 if (which == NULL)
7628 return;
7629
7630 mode = get_map_mode(&which, 0);
7631
7632 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7633 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7634 vim_free(keys_buf);
7635
7636 if (!get_dict)
7637 {
7638 /* Return a string. */
7639 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02007640 {
7641 if (*rhs == NUL)
7642 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
7643 else
7644 rettv->vval.v_string = str2special_save(rhs, FALSE);
7645 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007646
7647 }
7648 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7649 {
7650 /* Return a dictionary. */
7651 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7652 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7653 dict_T *dict = rettv->vval.v_dict;
7654
Bram Moolenaare0be1672018-07-08 16:50:37 +02007655 dict_add_string(dict, "lhs", lhs);
7656 dict_add_string(dict, "rhs", mp->m_orig_str);
7657 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
7658 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
7659 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
7660 dict_add_number(dict, "sid", (long)mp->m_script_ID);
7661 dict_add_number(dict, "buffer", (long)buffer_local);
7662 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
7663 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007664
7665 vim_free(lhs);
7666 vim_free(mapmode);
7667 }
7668}
7669
7670#ifdef FEAT_FLOAT
7671/*
7672 * "log()" function
7673 */
7674 static void
7675f_log(typval_T *argvars, typval_T *rettv)
7676{
7677 float_T f = 0.0;
7678
7679 rettv->v_type = VAR_FLOAT;
7680 if (get_float_arg(argvars, &f) == OK)
7681 rettv->vval.v_float = log(f);
7682 else
7683 rettv->vval.v_float = 0.0;
7684}
7685
7686/*
7687 * "log10()" function
7688 */
7689 static void
7690f_log10(typval_T *argvars, typval_T *rettv)
7691{
7692 float_T f = 0.0;
7693
7694 rettv->v_type = VAR_FLOAT;
7695 if (get_float_arg(argvars, &f) == OK)
7696 rettv->vval.v_float = log10(f);
7697 else
7698 rettv->vval.v_float = 0.0;
7699}
7700#endif
7701
7702#ifdef FEAT_LUA
7703/*
7704 * "luaeval()" function
7705 */
7706 static void
7707f_luaeval(typval_T *argvars, typval_T *rettv)
7708{
7709 char_u *str;
7710 char_u buf[NUMBUFLEN];
7711
7712 str = get_tv_string_buf(&argvars[0], buf);
7713 do_luaeval(str, argvars + 1, rettv);
7714}
7715#endif
7716
7717/*
7718 * "map()" function
7719 */
7720 static void
7721f_map(typval_T *argvars, typval_T *rettv)
7722{
7723 filter_map(argvars, rettv, TRUE);
7724}
7725
7726/*
7727 * "maparg()" function
7728 */
7729 static void
7730f_maparg(typval_T *argvars, typval_T *rettv)
7731{
7732 get_maparg(argvars, rettv, TRUE);
7733}
7734
7735/*
7736 * "mapcheck()" function
7737 */
7738 static void
7739f_mapcheck(typval_T *argvars, typval_T *rettv)
7740{
7741 get_maparg(argvars, rettv, FALSE);
7742}
7743
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007744typedef enum
7745{
7746 MATCH_END, /* matchend() */
7747 MATCH_MATCH, /* match() */
7748 MATCH_STR, /* matchstr() */
7749 MATCH_LIST, /* matchlist() */
7750 MATCH_POS /* matchstrpos() */
7751} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007752
7753 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007754find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007755{
7756 char_u *str = NULL;
7757 long len = 0;
7758 char_u *expr = NULL;
7759 char_u *pat;
7760 regmatch_T regmatch;
7761 char_u patbuf[NUMBUFLEN];
7762 char_u strbuf[NUMBUFLEN];
7763 char_u *save_cpo;
7764 long start = 0;
7765 long nth = 1;
7766 colnr_T startcol = 0;
7767 int match = 0;
7768 list_T *l = NULL;
7769 listitem_T *li = NULL;
7770 long idx = 0;
7771 char_u *tofree = NULL;
7772
7773 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7774 save_cpo = p_cpo;
7775 p_cpo = (char_u *)"";
7776
7777 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007778 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007779 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007780 /* type MATCH_LIST: return empty list when there are no matches.
7781 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007782 if (rettv_list_alloc(rettv) == FAIL)
7783 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007784 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007785 && (list_append_string(rettv->vval.v_list,
7786 (char_u *)"", 0) == FAIL
7787 || list_append_number(rettv->vval.v_list,
7788 (varnumber_T)-1) == FAIL
7789 || list_append_number(rettv->vval.v_list,
7790 (varnumber_T)-1) == FAIL
7791 || list_append_number(rettv->vval.v_list,
7792 (varnumber_T)-1) == FAIL))
7793 {
7794 list_free(rettv->vval.v_list);
7795 rettv->vval.v_list = NULL;
7796 goto theend;
7797 }
7798 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007799 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007800 {
7801 rettv->v_type = VAR_STRING;
7802 rettv->vval.v_string = NULL;
7803 }
7804
7805 if (argvars[0].v_type == VAR_LIST)
7806 {
7807 if ((l = argvars[0].vval.v_list) == NULL)
7808 goto theend;
7809 li = l->lv_first;
7810 }
7811 else
7812 {
7813 expr = str = get_tv_string(&argvars[0]);
7814 len = (long)STRLEN(str);
7815 }
7816
7817 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
7818 if (pat == NULL)
7819 goto theend;
7820
7821 if (argvars[2].v_type != VAR_UNKNOWN)
7822 {
7823 int error = FALSE;
7824
7825 start = (long)get_tv_number_chk(&argvars[2], &error);
7826 if (error)
7827 goto theend;
7828 if (l != NULL)
7829 {
7830 li = list_find(l, start);
7831 if (li == NULL)
7832 goto theend;
7833 idx = l->lv_idx; /* use the cached index */
7834 }
7835 else
7836 {
7837 if (start < 0)
7838 start = 0;
7839 if (start > len)
7840 goto theend;
7841 /* When "count" argument is there ignore matches before "start",
7842 * otherwise skip part of the string. Differs when pattern is "^"
7843 * or "\<". */
7844 if (argvars[3].v_type != VAR_UNKNOWN)
7845 startcol = start;
7846 else
7847 {
7848 str += start;
7849 len -= start;
7850 }
7851 }
7852
7853 if (argvars[3].v_type != VAR_UNKNOWN)
7854 nth = (long)get_tv_number_chk(&argvars[3], &error);
7855 if (error)
7856 goto theend;
7857 }
7858
7859 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7860 if (regmatch.regprog != NULL)
7861 {
7862 regmatch.rm_ic = p_ic;
7863
7864 for (;;)
7865 {
7866 if (l != NULL)
7867 {
7868 if (li == NULL)
7869 {
7870 match = FALSE;
7871 break;
7872 }
7873 vim_free(tofree);
7874 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
7875 if (str == NULL)
7876 break;
7877 }
7878
7879 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
7880
7881 if (match && --nth <= 0)
7882 break;
7883 if (l == NULL && !match)
7884 break;
7885
7886 /* Advance to just after the match. */
7887 if (l != NULL)
7888 {
7889 li = li->li_next;
7890 ++idx;
7891 }
7892 else
7893 {
7894#ifdef FEAT_MBYTE
7895 startcol = (colnr_T)(regmatch.startp[0]
7896 + (*mb_ptr2len)(regmatch.startp[0]) - str);
7897#else
7898 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
7899#endif
7900 if (startcol > (colnr_T)len
7901 || str + startcol <= regmatch.startp[0])
7902 {
7903 match = FALSE;
7904 break;
7905 }
7906 }
7907 }
7908
7909 if (match)
7910 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007911 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007912 {
7913 listitem_T *li1 = rettv->vval.v_list->lv_first;
7914 listitem_T *li2 = li1->li_next;
7915 listitem_T *li3 = li2->li_next;
7916 listitem_T *li4 = li3->li_next;
7917
7918 vim_free(li1->li_tv.vval.v_string);
7919 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
7920 (int)(regmatch.endp[0] - regmatch.startp[0]));
7921 li3->li_tv.vval.v_number =
7922 (varnumber_T)(regmatch.startp[0] - expr);
7923 li4->li_tv.vval.v_number =
7924 (varnumber_T)(regmatch.endp[0] - expr);
7925 if (l != NULL)
7926 li2->li_tv.vval.v_number = (varnumber_T)idx;
7927 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007928 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007929 {
7930 int i;
7931
7932 /* return list with matched string and submatches */
7933 for (i = 0; i < NSUBEXP; ++i)
7934 {
7935 if (regmatch.endp[i] == NULL)
7936 {
7937 if (list_append_string(rettv->vval.v_list,
7938 (char_u *)"", 0) == FAIL)
7939 break;
7940 }
7941 else if (list_append_string(rettv->vval.v_list,
7942 regmatch.startp[i],
7943 (int)(regmatch.endp[i] - regmatch.startp[i]))
7944 == FAIL)
7945 break;
7946 }
7947 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007948 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007949 {
7950 /* return matched string */
7951 if (l != NULL)
7952 copy_tv(&li->li_tv, rettv);
7953 else
7954 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
7955 (int)(regmatch.endp[0] - regmatch.startp[0]));
7956 }
7957 else if (l != NULL)
7958 rettv->vval.v_number = idx;
7959 else
7960 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007961 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007962 rettv->vval.v_number =
7963 (varnumber_T)(regmatch.startp[0] - str);
7964 else
7965 rettv->vval.v_number =
7966 (varnumber_T)(regmatch.endp[0] - str);
7967 rettv->vval.v_number += (varnumber_T)(str - expr);
7968 }
7969 }
7970 vim_regfree(regmatch.regprog);
7971 }
7972
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007973theend:
7974 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007975 /* matchstrpos() without a list: drop the second item. */
7976 listitem_remove(rettv->vval.v_list,
7977 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007978 vim_free(tofree);
7979 p_cpo = save_cpo;
7980}
7981
7982/*
7983 * "match()" function
7984 */
7985 static void
7986f_match(typval_T *argvars, typval_T *rettv)
7987{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007988 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007989}
7990
7991/*
7992 * "matchadd()" function
7993 */
7994 static void
7995f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7996{
7997#ifdef FEAT_SEARCH_EXTRA
7998 char_u buf[NUMBUFLEN];
7999 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
8000 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
8001 int prio = 10; /* default priority */
8002 int id = -1;
8003 int error = FALSE;
8004 char_u *conceal_char = NULL;
8005
8006 rettv->vval.v_number = -1;
8007
8008 if (grp == NULL || pat == NULL)
8009 return;
8010 if (argvars[2].v_type != VAR_UNKNOWN)
8011 {
8012 prio = (int)get_tv_number_chk(&argvars[2], &error);
8013 if (argvars[3].v_type != VAR_UNKNOWN)
8014 {
8015 id = (int)get_tv_number_chk(&argvars[3], &error);
8016 if (argvars[4].v_type != VAR_UNKNOWN)
8017 {
8018 if (argvars[4].v_type != VAR_DICT)
8019 {
8020 EMSG(_(e_dictreq));
8021 return;
8022 }
8023 if (dict_find(argvars[4].vval.v_dict,
8024 (char_u *)"conceal", -1) != NULL)
8025 conceal_char = get_dict_string(argvars[4].vval.v_dict,
8026 (char_u *)"conceal", FALSE);
8027 }
8028 }
8029 }
8030 if (error == TRUE)
8031 return;
8032 if (id >= 1 && id <= 3)
8033 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02008034 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008035 return;
8036 }
8037
8038 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
8039 conceal_char);
8040#endif
8041}
8042
8043/*
8044 * "matchaddpos()" function
8045 */
8046 static void
8047f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8048{
8049#ifdef FEAT_SEARCH_EXTRA
8050 char_u buf[NUMBUFLEN];
8051 char_u *group;
8052 int prio = 10;
8053 int id = -1;
8054 int error = FALSE;
8055 list_T *l;
8056 char_u *conceal_char = NULL;
8057
8058 rettv->vval.v_number = -1;
8059
8060 group = get_tv_string_buf_chk(&argvars[0], buf);
8061 if (group == NULL)
8062 return;
8063
8064 if (argvars[1].v_type != VAR_LIST)
8065 {
8066 EMSG2(_(e_listarg), "matchaddpos()");
8067 return;
8068 }
8069 l = argvars[1].vval.v_list;
8070 if (l == NULL)
8071 return;
8072
8073 if (argvars[2].v_type != VAR_UNKNOWN)
8074 {
8075 prio = (int)get_tv_number_chk(&argvars[2], &error);
8076 if (argvars[3].v_type != VAR_UNKNOWN)
8077 {
8078 id = (int)get_tv_number_chk(&argvars[3], &error);
8079 if (argvars[4].v_type != VAR_UNKNOWN)
8080 {
8081 if (argvars[4].v_type != VAR_DICT)
8082 {
8083 EMSG(_(e_dictreq));
8084 return;
8085 }
8086 if (dict_find(argvars[4].vval.v_dict,
8087 (char_u *)"conceal", -1) != NULL)
8088 conceal_char = get_dict_string(argvars[4].vval.v_dict,
8089 (char_u *)"conceal", FALSE);
8090 }
8091 }
8092 }
8093 if (error == TRUE)
8094 return;
8095
8096 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8097 if (id == 1 || id == 2)
8098 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02008099 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008100 return;
8101 }
8102
8103 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
8104 conceal_char);
8105#endif
8106}
8107
8108/*
8109 * "matcharg()" function
8110 */
8111 static void
8112f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8113{
8114 if (rettv_list_alloc(rettv) == OK)
8115 {
8116#ifdef FEAT_SEARCH_EXTRA
8117 int id = (int)get_tv_number(&argvars[0]);
8118 matchitem_T *m;
8119
8120 if (id >= 1 && id <= 3)
8121 {
8122 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8123 {
8124 list_append_string(rettv->vval.v_list,
8125 syn_id2name(m->hlg_id), -1);
8126 list_append_string(rettv->vval.v_list, m->pattern, -1);
8127 }
8128 else
8129 {
8130 list_append_string(rettv->vval.v_list, NULL, -1);
8131 list_append_string(rettv->vval.v_list, NULL, -1);
8132 }
8133 }
8134#endif
8135 }
8136}
8137
8138/*
8139 * "matchdelete()" function
8140 */
8141 static void
8142f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8143{
8144#ifdef FEAT_SEARCH_EXTRA
8145 rettv->vval.v_number = match_delete(curwin,
8146 (int)get_tv_number(&argvars[0]), TRUE);
8147#endif
8148}
8149
8150/*
8151 * "matchend()" function
8152 */
8153 static void
8154f_matchend(typval_T *argvars, typval_T *rettv)
8155{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008156 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008157}
8158
8159/*
8160 * "matchlist()" function
8161 */
8162 static void
8163f_matchlist(typval_T *argvars, typval_T *rettv)
8164{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008165 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008166}
8167
8168/*
8169 * "matchstr()" function
8170 */
8171 static void
8172f_matchstr(typval_T *argvars, typval_T *rettv)
8173{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008174 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008175}
8176
8177/*
8178 * "matchstrpos()" function
8179 */
8180 static void
8181f_matchstrpos(typval_T *argvars, typval_T *rettv)
8182{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008183 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008184}
8185
8186static void max_min(typval_T *argvars, typval_T *rettv, int domax);
8187
8188 static void
8189max_min(typval_T *argvars, typval_T *rettv, int domax)
8190{
8191 varnumber_T n = 0;
8192 varnumber_T i;
8193 int error = FALSE;
8194
8195 if (argvars[0].v_type == VAR_LIST)
8196 {
8197 list_T *l;
8198 listitem_T *li;
8199
8200 l = argvars[0].vval.v_list;
8201 if (l != NULL)
8202 {
8203 li = l->lv_first;
8204 if (li != NULL)
8205 {
8206 n = get_tv_number_chk(&li->li_tv, &error);
8207 for (;;)
8208 {
8209 li = li->li_next;
8210 if (li == NULL)
8211 break;
8212 i = get_tv_number_chk(&li->li_tv, &error);
8213 if (domax ? i > n : i < n)
8214 n = i;
8215 }
8216 }
8217 }
8218 }
8219 else if (argvars[0].v_type == VAR_DICT)
8220 {
8221 dict_T *d;
8222 int first = TRUE;
8223 hashitem_T *hi;
8224 int todo;
8225
8226 d = argvars[0].vval.v_dict;
8227 if (d != NULL)
8228 {
8229 todo = (int)d->dv_hashtab.ht_used;
8230 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8231 {
8232 if (!HASHITEM_EMPTY(hi))
8233 {
8234 --todo;
8235 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
8236 if (first)
8237 {
8238 n = i;
8239 first = FALSE;
8240 }
8241 else if (domax ? i > n : i < n)
8242 n = i;
8243 }
8244 }
8245 }
8246 }
8247 else
Bram Moolenaar26b84332016-09-04 21:42:36 +02008248 EMSG2(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008249 rettv->vval.v_number = error ? 0 : n;
8250}
8251
8252/*
8253 * "max()" function
8254 */
8255 static void
8256f_max(typval_T *argvars, typval_T *rettv)
8257{
8258 max_min(argvars, rettv, TRUE);
8259}
8260
8261/*
8262 * "min()" function
8263 */
8264 static void
8265f_min(typval_T *argvars, typval_T *rettv)
8266{
8267 max_min(argvars, rettv, FALSE);
8268}
8269
8270static int mkdir_recurse(char_u *dir, int prot);
8271
8272/*
8273 * Create the directory in which "dir" is located, and higher levels when
8274 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008275 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008276 */
8277 static int
8278mkdir_recurse(char_u *dir, int prot)
8279{
8280 char_u *p;
8281 char_u *updir;
8282 int r = FAIL;
8283
8284 /* Get end of directory name in "dir".
8285 * We're done when it's "/" or "c:/". */
8286 p = gettail_sep(dir);
8287 if (p <= get_past_head(dir))
8288 return OK;
8289
8290 /* If the directory exists we're done. Otherwise: create it.*/
8291 updir = vim_strnsave(dir, (int)(p - dir));
8292 if (updir == NULL)
8293 return FAIL;
8294 if (mch_isdir(updir))
8295 r = OK;
8296 else if (mkdir_recurse(updir, prot) == OK)
8297 r = vim_mkdir_emsg(updir, prot);
8298 vim_free(updir);
8299 return r;
8300}
8301
8302#ifdef vim_mkdir
8303/*
8304 * "mkdir()" function
8305 */
8306 static void
8307f_mkdir(typval_T *argvars, typval_T *rettv)
8308{
8309 char_u *dir;
8310 char_u buf[NUMBUFLEN];
8311 int prot = 0755;
8312
8313 rettv->vval.v_number = FAIL;
8314 if (check_restricted() || check_secure())
8315 return;
8316
8317 dir = get_tv_string_buf(&argvars[0], buf);
8318 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008319 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008320
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008321 if (*gettail(dir) == NUL)
8322 /* remove trailing slashes */
8323 *gettail_sep(dir) = NUL;
8324
8325 if (argvars[1].v_type != VAR_UNKNOWN)
8326 {
8327 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008328 {
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008329 prot = (int)get_tv_number_chk(&argvars[2], NULL);
8330 if (prot == -1)
8331 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008332 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008333 if (STRCMP(get_tv_string(&argvars[1]), "p") == 0)
8334 {
8335 if (mch_isdir(dir))
8336 {
8337 /* With the "p" flag it's OK if the dir already exists. */
8338 rettv->vval.v_number = OK;
8339 return;
8340 }
8341 mkdir_recurse(dir, prot);
8342 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008343 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008344 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008345}
8346#endif
8347
8348/*
8349 * "mode()" function
8350 */
8351 static void
8352f_mode(typval_T *argvars, typval_T *rettv)
8353{
8354 char_u buf[3];
8355
8356 buf[1] = NUL;
8357 buf[2] = NUL;
8358
8359 if (time_for_testing == 93784)
8360 {
8361 /* Testing the two-character code. */
8362 buf[0] = 'x';
8363 buf[1] = '!';
8364 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008365#ifdef FEAT_TERMINAL
8366 else if (term_use_loop())
8367 buf[0] = 't';
8368#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008369 else if (VIsual_active)
8370 {
8371 if (VIsual_select)
8372 buf[0] = VIsual_mode + 's' - 'v';
8373 else
8374 buf[0] = VIsual_mode;
8375 }
8376 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8377 || State == CONFIRM)
8378 {
8379 buf[0] = 'r';
8380 if (State == ASKMORE)
8381 buf[1] = 'm';
8382 else if (State == CONFIRM)
8383 buf[1] = '?';
8384 }
8385 else if (State == EXTERNCMD)
8386 buf[0] = '!';
8387 else if (State & INSERT)
8388 {
8389#ifdef FEAT_VREPLACE
8390 if (State & VREPLACE_FLAG)
8391 {
8392 buf[0] = 'R';
8393 buf[1] = 'v';
8394 }
8395 else
8396#endif
Bram Moolenaare90858d2017-02-01 17:24:34 +01008397 {
8398 if (State & REPLACE_FLAG)
8399 buf[0] = 'R';
8400 else
8401 buf[0] = 'i';
8402#ifdef FEAT_INS_EXPAND
8403 if (ins_compl_active())
8404 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008405 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008406 buf[1] = 'x';
8407#endif
8408 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008409 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008410 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008411 {
8412 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008413 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008414 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008415 else if (exmode_active == EXMODE_NORMAL)
8416 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008417 }
8418 else
8419 {
8420 buf[0] = 'n';
8421 if (finish_op)
8422 buf[1] = 'o';
8423 }
8424
8425 /* Clear out the minor mode when the argument is not a non-zero number or
8426 * non-empty string. */
8427 if (!non_zero_arg(&argvars[0]))
8428 buf[1] = NUL;
8429
8430 rettv->vval.v_string = vim_strsave(buf);
8431 rettv->v_type = VAR_STRING;
8432}
8433
8434#if defined(FEAT_MZSCHEME) || defined(PROTO)
8435/*
8436 * "mzeval()" function
8437 */
8438 static void
8439f_mzeval(typval_T *argvars, typval_T *rettv)
8440{
8441 char_u *str;
8442 char_u buf[NUMBUFLEN];
8443
8444 str = get_tv_string_buf(&argvars[0], buf);
8445 do_mzeval(str, rettv);
8446}
8447
8448 void
8449mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8450{
8451 typval_T argvars[3];
8452
8453 argvars[0].v_type = VAR_STRING;
8454 argvars[0].vval.v_string = name;
8455 copy_tv(args, &argvars[1]);
8456 argvars[2].v_type = VAR_UNKNOWN;
8457 f_call(argvars, rettv);
8458 clear_tv(&argvars[1]);
8459}
8460#endif
8461
8462/*
8463 * "nextnonblank()" function
8464 */
8465 static void
8466f_nextnonblank(typval_T *argvars, typval_T *rettv)
8467{
8468 linenr_T lnum;
8469
8470 for (lnum = get_tv_lnum(argvars); ; ++lnum)
8471 {
8472 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8473 {
8474 lnum = 0;
8475 break;
8476 }
8477 if (*skipwhite(ml_get(lnum)) != NUL)
8478 break;
8479 }
8480 rettv->vval.v_number = lnum;
8481}
8482
8483/*
8484 * "nr2char()" function
8485 */
8486 static void
8487f_nr2char(typval_T *argvars, typval_T *rettv)
8488{
8489 char_u buf[NUMBUFLEN];
8490
8491#ifdef FEAT_MBYTE
8492 if (has_mbyte)
8493 {
8494 int utf8 = 0;
8495
8496 if (argvars[1].v_type != VAR_UNKNOWN)
8497 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
8498 if (utf8)
8499 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8500 else
8501 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8502 }
8503 else
8504#endif
8505 {
8506 buf[0] = (char_u)get_tv_number(&argvars[0]);
8507 buf[1] = NUL;
8508 }
8509 rettv->v_type = VAR_STRING;
8510 rettv->vval.v_string = vim_strsave(buf);
8511}
8512
8513/*
8514 * "or(expr, expr)" function
8515 */
8516 static void
8517f_or(typval_T *argvars, typval_T *rettv)
8518{
8519 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8520 | get_tv_number_chk(&argvars[1], NULL);
8521}
8522
8523/*
8524 * "pathshorten()" function
8525 */
8526 static void
8527f_pathshorten(typval_T *argvars, typval_T *rettv)
8528{
8529 char_u *p;
8530
8531 rettv->v_type = VAR_STRING;
8532 p = get_tv_string_chk(&argvars[0]);
8533 if (p == NULL)
8534 rettv->vval.v_string = NULL;
8535 else
8536 {
8537 p = vim_strsave(p);
8538 rettv->vval.v_string = p;
8539 if (p != NULL)
8540 shorten_dir(p);
8541 }
8542}
8543
8544#ifdef FEAT_PERL
8545/*
8546 * "perleval()" function
8547 */
8548 static void
8549f_perleval(typval_T *argvars, typval_T *rettv)
8550{
8551 char_u *str;
8552 char_u buf[NUMBUFLEN];
8553
8554 str = get_tv_string_buf(&argvars[0], buf);
8555 do_perleval(str, rettv);
8556}
8557#endif
8558
8559#ifdef FEAT_FLOAT
8560/*
8561 * "pow()" function
8562 */
8563 static void
8564f_pow(typval_T *argvars, typval_T *rettv)
8565{
8566 float_T fx = 0.0, fy = 0.0;
8567
8568 rettv->v_type = VAR_FLOAT;
8569 if (get_float_arg(argvars, &fx) == OK
8570 && get_float_arg(&argvars[1], &fy) == OK)
8571 rettv->vval.v_float = pow(fx, fy);
8572 else
8573 rettv->vval.v_float = 0.0;
8574}
8575#endif
8576
8577/*
8578 * "prevnonblank()" function
8579 */
8580 static void
8581f_prevnonblank(typval_T *argvars, typval_T *rettv)
8582{
8583 linenr_T lnum;
8584
8585 lnum = get_tv_lnum(argvars);
8586 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8587 lnum = 0;
8588 else
8589 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8590 --lnum;
8591 rettv->vval.v_number = lnum;
8592}
8593
8594/* This dummy va_list is here because:
8595 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8596 * - locally in the function results in a "used before set" warning
8597 * - using va_start() to initialize it gives "function with fixed args" error */
8598static va_list ap;
8599
8600/*
8601 * "printf()" function
8602 */
8603 static void
8604f_printf(typval_T *argvars, typval_T *rettv)
8605{
8606 char_u buf[NUMBUFLEN];
8607 int len;
8608 char_u *s;
8609 int saved_did_emsg = did_emsg;
8610 char *fmt;
8611
8612 rettv->v_type = VAR_STRING;
8613 rettv->vval.v_string = NULL;
8614
8615 /* Get the required length, allocate the buffer and do it for real. */
8616 did_emsg = FALSE;
8617 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008618 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008619 if (!did_emsg)
8620 {
8621 s = alloc(len + 1);
8622 if (s != NULL)
8623 {
8624 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008625 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8626 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008627 }
8628 }
8629 did_emsg |= saved_did_emsg;
8630}
8631
Bram Moolenaarf2732452018-06-03 14:47:35 +02008632#ifdef FEAT_JOB_CHANNEL
8633/*
8634 * "prompt_setcallback({buffer}, {callback})" function
8635 */
8636 static void
8637f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
8638{
8639 buf_T *buf;
8640 char_u *callback;
8641 partial_T *partial;
8642
8643 if (check_secure())
8644 return;
8645 buf = get_buf_tv(&argvars[0], FALSE);
8646 if (buf == NULL)
8647 return;
8648
8649 callback = get_callback(&argvars[1], &partial);
8650 if (callback == NULL)
8651 return;
8652
8653 free_callback(buf->b_prompt_callback, buf->b_prompt_partial);
8654 if (partial == NULL)
8655 buf->b_prompt_callback = vim_strsave(callback);
8656 else
8657 /* pointer into the partial */
8658 buf->b_prompt_callback = callback;
8659 buf->b_prompt_partial = partial;
8660}
8661
8662/*
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02008663 * "prompt_setinterrupt({buffer}, {callback})" function
8664 */
8665 static void
8666f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
8667{
8668 buf_T *buf;
8669 char_u *callback;
8670 partial_T *partial;
8671
8672 if (check_secure())
8673 return;
8674 buf = get_buf_tv(&argvars[0], FALSE);
8675 if (buf == NULL)
8676 return;
8677
8678 callback = get_callback(&argvars[1], &partial);
8679 if (callback == NULL)
8680 return;
8681
8682 free_callback(buf->b_prompt_interrupt, buf->b_prompt_int_partial);
8683 if (partial == NULL)
8684 buf->b_prompt_interrupt = vim_strsave(callback);
8685 else
8686 /* pointer into the partial */
8687 buf->b_prompt_interrupt = callback;
8688 buf->b_prompt_int_partial = partial;
8689}
8690
8691/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02008692 * "prompt_setprompt({buffer}, {text})" function
8693 */
8694 static void
8695f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
8696{
8697 buf_T *buf;
8698 char_u *text;
8699
8700 if (check_secure())
8701 return;
8702 buf = get_buf_tv(&argvars[0], FALSE);
8703 if (buf == NULL)
8704 return;
8705
8706 text = get_tv_string(&argvars[1]);
8707 vim_free(buf->b_prompt_text);
8708 buf->b_prompt_text = vim_strsave(text);
8709}
8710#endif
8711
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008712/*
8713 * "pumvisible()" function
8714 */
8715 static void
8716f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8717{
8718#ifdef FEAT_INS_EXPAND
8719 if (pum_visible())
8720 rettv->vval.v_number = 1;
8721#endif
8722}
8723
8724#ifdef FEAT_PYTHON3
8725/*
8726 * "py3eval()" function
8727 */
8728 static void
8729f_py3eval(typval_T *argvars, typval_T *rettv)
8730{
8731 char_u *str;
8732 char_u buf[NUMBUFLEN];
8733
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008734 if (p_pyx == 0)
8735 p_pyx = 3;
8736
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008737 str = get_tv_string_buf(&argvars[0], buf);
8738 do_py3eval(str, rettv);
8739}
8740#endif
8741
8742#ifdef FEAT_PYTHON
8743/*
8744 * "pyeval()" function
8745 */
8746 static void
8747f_pyeval(typval_T *argvars, typval_T *rettv)
8748{
8749 char_u *str;
8750 char_u buf[NUMBUFLEN];
8751
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008752 if (p_pyx == 0)
8753 p_pyx = 2;
8754
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008755 str = get_tv_string_buf(&argvars[0], buf);
8756 do_pyeval(str, rettv);
8757}
8758#endif
8759
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008760#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8761/*
8762 * "pyxeval()" function
8763 */
8764 static void
8765f_pyxeval(typval_T *argvars, typval_T *rettv)
8766{
8767# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8768 init_pyxversion();
8769 if (p_pyx == 2)
8770 f_pyeval(argvars, rettv);
8771 else
8772 f_py3eval(argvars, rettv);
8773# elif defined(FEAT_PYTHON)
8774 f_pyeval(argvars, rettv);
8775# elif defined(FEAT_PYTHON3)
8776 f_py3eval(argvars, rettv);
8777# endif
8778}
8779#endif
8780
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008781/*
8782 * "range()" function
8783 */
8784 static void
8785f_range(typval_T *argvars, typval_T *rettv)
8786{
8787 varnumber_T start;
8788 varnumber_T end;
8789 varnumber_T stride = 1;
8790 varnumber_T i;
8791 int error = FALSE;
8792
8793 start = get_tv_number_chk(&argvars[0], &error);
8794 if (argvars[1].v_type == VAR_UNKNOWN)
8795 {
8796 end = start - 1;
8797 start = 0;
8798 }
8799 else
8800 {
8801 end = get_tv_number_chk(&argvars[1], &error);
8802 if (argvars[2].v_type != VAR_UNKNOWN)
8803 stride = get_tv_number_chk(&argvars[2], &error);
8804 }
8805
8806 if (error)
8807 return; /* type error; errmsg already given */
8808 if (stride == 0)
8809 EMSG(_("E726: Stride is zero"));
8810 else if (stride > 0 ? end + 1 < start : end - 1 > start)
8811 EMSG(_("E727: Start past end"));
8812 else
8813 {
8814 if (rettv_list_alloc(rettv) == OK)
8815 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8816 if (list_append_number(rettv->vval.v_list,
8817 (varnumber_T)i) == FAIL)
8818 break;
8819 }
8820}
8821
8822/*
8823 * "readfile()" function
8824 */
8825 static void
8826f_readfile(typval_T *argvars, typval_T *rettv)
8827{
8828 int binary = FALSE;
8829 int failed = FALSE;
8830 char_u *fname;
8831 FILE *fd;
8832 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
8833 int io_size = sizeof(buf);
8834 int readlen; /* size of last fread() */
8835 char_u *prev = NULL; /* previously read bytes, if any */
8836 long prevlen = 0; /* length of data in prev */
8837 long prevsize = 0; /* size of prev buffer */
8838 long maxline = MAXLNUM;
8839 long cnt = 0;
8840 char_u *p; /* position in buf */
8841 char_u *start; /* start of current line */
8842
8843 if (argvars[1].v_type != VAR_UNKNOWN)
8844 {
8845 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
8846 binary = TRUE;
8847 if (argvars[2].v_type != VAR_UNKNOWN)
8848 maxline = (long)get_tv_number(&argvars[2]);
8849 }
8850
8851 if (rettv_list_alloc(rettv) == FAIL)
8852 return;
8853
8854 /* Always open the file in binary mode, library functions have a mind of
8855 * their own about CR-LF conversion. */
8856 fname = get_tv_string(&argvars[0]);
8857 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
8858 {
8859 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
8860 return;
8861 }
8862
8863 while (cnt < maxline || maxline < 0)
8864 {
8865 readlen = (int)fread(buf, 1, io_size, fd);
8866
8867 /* This for loop processes what was read, but is also entered at end
8868 * of file so that either:
8869 * - an incomplete line gets written
8870 * - a "binary" file gets an empty line at the end if it ends in a
8871 * newline. */
8872 for (p = buf, start = buf;
8873 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
8874 ++p)
8875 {
8876 if (*p == '\n' || readlen <= 0)
8877 {
8878 listitem_T *li;
8879 char_u *s = NULL;
8880 long_u len = p - start;
8881
8882 /* Finished a line. Remove CRs before NL. */
8883 if (readlen > 0 && !binary)
8884 {
8885 while (len > 0 && start[len - 1] == '\r')
8886 --len;
8887 /* removal may cross back to the "prev" string */
8888 if (len == 0)
8889 while (prevlen > 0 && prev[prevlen - 1] == '\r')
8890 --prevlen;
8891 }
8892 if (prevlen == 0)
8893 s = vim_strnsave(start, (int)len);
8894 else
8895 {
8896 /* Change "prev" buffer to be the right size. This way
8897 * the bytes are only copied once, and very long lines are
8898 * allocated only once. */
8899 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
8900 {
8901 mch_memmove(s + prevlen, start, len);
8902 s[prevlen + len] = NUL;
8903 prev = NULL; /* the list will own the string */
8904 prevlen = prevsize = 0;
8905 }
8906 }
8907 if (s == NULL)
8908 {
8909 do_outofmem_msg((long_u) prevlen + len + 1);
8910 failed = TRUE;
8911 break;
8912 }
8913
8914 if ((li = listitem_alloc()) == NULL)
8915 {
8916 vim_free(s);
8917 failed = TRUE;
8918 break;
8919 }
8920 li->li_tv.v_type = VAR_STRING;
8921 li->li_tv.v_lock = 0;
8922 li->li_tv.vval.v_string = s;
8923 list_append(rettv->vval.v_list, li);
8924
8925 start = p + 1; /* step over newline */
8926 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
8927 break;
8928 }
8929 else if (*p == NUL)
8930 *p = '\n';
8931#ifdef FEAT_MBYTE
8932 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
8933 * when finding the BF and check the previous two bytes. */
8934 else if (*p == 0xbf && enc_utf8 && !binary)
8935 {
8936 /* Find the two bytes before the 0xbf. If p is at buf, or buf
8937 * + 1, these may be in the "prev" string. */
8938 char_u back1 = p >= buf + 1 ? p[-1]
8939 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
8940 char_u back2 = p >= buf + 2 ? p[-2]
8941 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
8942 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
8943
8944 if (back2 == 0xef && back1 == 0xbb)
8945 {
8946 char_u *dest = p - 2;
8947
8948 /* Usually a BOM is at the beginning of a file, and so at
8949 * the beginning of a line; then we can just step over it.
8950 */
8951 if (start == dest)
8952 start = p + 1;
8953 else
8954 {
8955 /* have to shuffle buf to close gap */
8956 int adjust_prevlen = 0;
8957
8958 if (dest < buf)
8959 {
8960 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
8961 dest = buf;
8962 }
8963 if (readlen > p - buf + 1)
8964 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
8965 readlen -= 3 - adjust_prevlen;
8966 prevlen -= adjust_prevlen;
8967 p = dest - 1;
8968 }
8969 }
8970 }
8971#endif
8972 } /* for */
8973
8974 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
8975 break;
8976 if (start < p)
8977 {
8978 /* There's part of a line in buf, store it in "prev". */
8979 if (p - start + prevlen >= prevsize)
8980 {
8981 /* need bigger "prev" buffer */
8982 char_u *newprev;
8983
8984 /* A common use case is ordinary text files and "prev" gets a
8985 * fragment of a line, so the first allocation is made
8986 * small, to avoid repeatedly 'allocing' large and
8987 * 'reallocing' small. */
8988 if (prevsize == 0)
8989 prevsize = (long)(p - start);
8990 else
8991 {
8992 long grow50pc = (prevsize * 3) / 2;
8993 long growmin = (long)((p - start) * 2 + prevlen);
8994 prevsize = grow50pc > growmin ? grow50pc : growmin;
8995 }
8996 newprev = prev == NULL ? alloc(prevsize)
8997 : vim_realloc(prev, prevsize);
8998 if (newprev == NULL)
8999 {
9000 do_outofmem_msg((long_u)prevsize);
9001 failed = TRUE;
9002 break;
9003 }
9004 prev = newprev;
9005 }
9006 /* Add the line part to end of "prev". */
9007 mch_memmove(prev + prevlen, start, p - start);
9008 prevlen += (long)(p - start);
9009 }
9010 } /* while */
9011
9012 /*
9013 * For a negative line count use only the lines at the end of the file,
9014 * free the rest.
9015 */
9016 if (!failed && maxline < 0)
9017 while (cnt > -maxline)
9018 {
9019 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
9020 --cnt;
9021 }
9022
9023 if (failed)
9024 {
9025 list_free(rettv->vval.v_list);
9026 /* readfile doc says an empty list is returned on error */
9027 rettv->vval.v_list = list_alloc();
9028 }
9029
9030 vim_free(prev);
9031 fclose(fd);
9032}
9033
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009034 static void
9035return_register(int regname, typval_T *rettv)
9036{
9037 char_u buf[2] = {0, 0};
9038
9039 buf[0] = (char_u)regname;
9040 rettv->v_type = VAR_STRING;
9041 rettv->vval.v_string = vim_strsave(buf);
9042}
9043
9044/*
9045 * "reg_executing()" function
9046 */
9047 static void
9048f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9049{
9050 return_register(reg_executing, rettv);
9051}
9052
9053/*
9054 * "reg_recording()" function
9055 */
9056 static void
9057f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9058{
9059 return_register(reg_recording, rettv);
9060}
9061
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009062#if defined(FEAT_RELTIME)
9063static int list2proftime(typval_T *arg, proftime_T *tm);
9064
9065/*
9066 * Convert a List to proftime_T.
9067 * Return FAIL when there is something wrong.
9068 */
9069 static int
9070list2proftime(typval_T *arg, proftime_T *tm)
9071{
9072 long n1, n2;
9073 int error = FALSE;
9074
9075 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9076 || arg->vval.v_list->lv_len != 2)
9077 return FAIL;
9078 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9079 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
9080# ifdef WIN3264
9081 tm->HighPart = n1;
9082 tm->LowPart = n2;
9083# else
9084 tm->tv_sec = n1;
9085 tm->tv_usec = n2;
9086# endif
9087 return error ? FAIL : OK;
9088}
9089#endif /* FEAT_RELTIME */
9090
9091/*
9092 * "reltime()" function
9093 */
9094 static void
9095f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9096{
9097#ifdef FEAT_RELTIME
9098 proftime_T res;
9099 proftime_T start;
9100
9101 if (argvars[0].v_type == VAR_UNKNOWN)
9102 {
9103 /* No arguments: get current time. */
9104 profile_start(&res);
9105 }
9106 else if (argvars[1].v_type == VAR_UNKNOWN)
9107 {
9108 if (list2proftime(&argvars[0], &res) == FAIL)
9109 return;
9110 profile_end(&res);
9111 }
9112 else
9113 {
9114 /* Two arguments: compute the difference. */
9115 if (list2proftime(&argvars[0], &start) == FAIL
9116 || list2proftime(&argvars[1], &res) == FAIL)
9117 return;
9118 profile_sub(&res, &start);
9119 }
9120
9121 if (rettv_list_alloc(rettv) == OK)
9122 {
9123 long n1, n2;
9124
9125# ifdef WIN3264
9126 n1 = res.HighPart;
9127 n2 = res.LowPart;
9128# else
9129 n1 = res.tv_sec;
9130 n2 = res.tv_usec;
9131# endif
9132 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9133 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9134 }
9135#endif
9136}
9137
9138#ifdef FEAT_FLOAT
9139/*
9140 * "reltimefloat()" function
9141 */
9142 static void
9143f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9144{
9145# ifdef FEAT_RELTIME
9146 proftime_T tm;
9147# endif
9148
9149 rettv->v_type = VAR_FLOAT;
9150 rettv->vval.v_float = 0;
9151# ifdef FEAT_RELTIME
9152 if (list2proftime(&argvars[0], &tm) == OK)
9153 rettv->vval.v_float = profile_float(&tm);
9154# endif
9155}
9156#endif
9157
9158/*
9159 * "reltimestr()" function
9160 */
9161 static void
9162f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9163{
9164#ifdef FEAT_RELTIME
9165 proftime_T tm;
9166#endif
9167
9168 rettv->v_type = VAR_STRING;
9169 rettv->vval.v_string = NULL;
9170#ifdef FEAT_RELTIME
9171 if (list2proftime(&argvars[0], &tm) == OK)
9172 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9173#endif
9174}
9175
9176#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
9177static void make_connection(void);
9178static int check_connection(void);
9179
9180 static void
9181make_connection(void)
9182{
9183 if (X_DISPLAY == NULL
9184# ifdef FEAT_GUI
9185 && !gui.in_use
9186# endif
9187 )
9188 {
9189 x_force_connect = TRUE;
9190 setup_term_clip();
9191 x_force_connect = FALSE;
9192 }
9193}
9194
9195 static int
9196check_connection(void)
9197{
9198 make_connection();
9199 if (X_DISPLAY == NULL)
9200 {
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009201 EMSG(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009202 return FAIL;
9203 }
9204 return OK;
9205}
9206#endif
9207
9208#ifdef FEAT_CLIENTSERVER
9209 static void
9210remote_common(typval_T *argvars, typval_T *rettv, int expr)
9211{
9212 char_u *server_name;
9213 char_u *keys;
9214 char_u *r = NULL;
9215 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009216 int timeout = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009217# ifdef WIN32
9218 HWND w;
9219# else
9220 Window w;
9221# endif
9222
9223 if (check_restricted() || check_secure())
9224 return;
9225
9226# ifdef FEAT_X11
9227 if (check_connection() == FAIL)
9228 return;
9229# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009230 if (argvars[2].v_type != VAR_UNKNOWN
9231 && argvars[3].v_type != VAR_UNKNOWN)
9232 timeout = get_tv_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009233
9234 server_name = get_tv_string_chk(&argvars[0]);
9235 if (server_name == NULL)
9236 return; /* type error; errmsg already given */
9237 keys = get_tv_string_buf(&argvars[1], buf);
9238# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009239 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009240# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009241 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9242 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009243# endif
9244 {
9245 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009246 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009247 EMSG(r); /* sending worked but evaluation failed */
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009248 vim_free(r);
9249 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009250 else
9251 EMSG2(_("E241: Unable to send to %s"), server_name);
9252 return;
9253 }
9254
9255 rettv->vval.v_string = r;
9256
9257 if (argvars[2].v_type != VAR_UNKNOWN)
9258 {
9259 dictitem_T v;
9260 char_u str[30];
9261 char_u *idvar;
9262
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009263 idvar = get_tv_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009264 if (idvar != NULL && *idvar != NUL)
9265 {
9266 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9267 v.di_tv.v_type = VAR_STRING;
9268 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009269 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009270 vim_free(v.di_tv.vval.v_string);
9271 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009272 }
9273}
9274#endif
9275
9276/*
9277 * "remote_expr()" function
9278 */
9279 static void
9280f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9281{
9282 rettv->v_type = VAR_STRING;
9283 rettv->vval.v_string = NULL;
9284#ifdef FEAT_CLIENTSERVER
9285 remote_common(argvars, rettv, TRUE);
9286#endif
9287}
9288
9289/*
9290 * "remote_foreground()" function
9291 */
9292 static void
9293f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9294{
9295#ifdef FEAT_CLIENTSERVER
9296# ifdef WIN32
9297 /* On Win32 it's done in this application. */
9298 {
9299 char_u *server_name = get_tv_string_chk(&argvars[0]);
9300
9301 if (server_name != NULL)
9302 serverForeground(server_name);
9303 }
9304# else
9305 /* Send a foreground() expression to the server. */
9306 argvars[1].v_type = VAR_STRING;
9307 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9308 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009309 rettv->v_type = VAR_STRING;
9310 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009311 remote_common(argvars, rettv, TRUE);
9312 vim_free(argvars[1].vval.v_string);
9313# endif
9314#endif
9315}
9316
9317 static void
9318f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
9319{
9320#ifdef FEAT_CLIENTSERVER
9321 dictitem_T v;
9322 char_u *s = NULL;
9323# ifdef WIN32
9324 long_u n = 0;
9325# endif
9326 char_u *serverid;
9327
9328 if (check_restricted() || check_secure())
9329 {
9330 rettv->vval.v_number = -1;
9331 return;
9332 }
9333 serverid = get_tv_string_chk(&argvars[0]);
9334 if (serverid == NULL)
9335 {
9336 rettv->vval.v_number = -1;
9337 return; /* type error; errmsg already given */
9338 }
9339# ifdef WIN32
9340 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
9341 if (n == 0)
9342 rettv->vval.v_number = -1;
9343 else
9344 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009345 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009346 rettv->vval.v_number = (s != NULL);
9347 }
9348# else
9349 if (check_connection() == FAIL)
9350 return;
9351
9352 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
9353 serverStrToWin(serverid), &s);
9354# endif
9355
9356 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
9357 {
9358 char_u *retvar;
9359
9360 v.di_tv.v_type = VAR_STRING;
9361 v.di_tv.vval.v_string = vim_strsave(s);
9362 retvar = get_tv_string_chk(&argvars[1]);
9363 if (retvar != NULL)
9364 set_var(retvar, &v.di_tv, FALSE);
9365 vim_free(v.di_tv.vval.v_string);
9366 }
9367#else
9368 rettv->vval.v_number = -1;
9369#endif
9370}
9371
9372 static void
9373f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
9374{
9375 char_u *r = NULL;
9376
9377#ifdef FEAT_CLIENTSERVER
9378 char_u *serverid = get_tv_string_chk(&argvars[0]);
9379
9380 if (serverid != NULL && !check_restricted() && !check_secure())
9381 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009382 int timeout = 0;
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009383# ifdef WIN32
9384 /* The server's HWND is encoded in the 'id' parameter */
9385 long_u n = 0;
9386# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009387
9388 if (argvars[1].v_type != VAR_UNKNOWN)
9389 timeout = get_tv_number(&argvars[1]);
9390
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009391# ifdef WIN32
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009392 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
9393 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009394 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009395 if (r == NULL)
9396# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009397 if (check_connection() == FAIL
9398 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
9399 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009400# endif
9401 EMSG(_("E277: Unable to read a server reply"));
9402 }
9403#endif
9404 rettv->v_type = VAR_STRING;
9405 rettv->vval.v_string = r;
9406}
9407
9408/*
9409 * "remote_send()" function
9410 */
9411 static void
9412f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9413{
9414 rettv->v_type = VAR_STRING;
9415 rettv->vval.v_string = NULL;
9416#ifdef FEAT_CLIENTSERVER
9417 remote_common(argvars, rettv, FALSE);
9418#endif
9419}
9420
9421/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009422 * "remote_startserver()" function
9423 */
9424 static void
9425f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9426{
9427#ifdef FEAT_CLIENTSERVER
9428 char_u *server = get_tv_string_chk(&argvars[0]);
9429
9430 if (server == NULL)
9431 return; /* type error; errmsg already given */
9432 if (serverName != NULL)
9433 EMSG(_("E941: already started a server"));
9434 else
9435 {
9436# ifdef FEAT_X11
9437 if (check_connection() == OK)
9438 serverRegisterName(X_DISPLAY, server);
9439# else
9440 serverSetName(server);
9441# endif
9442 }
9443#else
9444 EMSG(_("E942: +clientserver feature not available"));
9445#endif
9446}
9447
9448/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009449 * "remove()" function
9450 */
9451 static void
9452f_remove(typval_T *argvars, typval_T *rettv)
9453{
9454 list_T *l;
9455 listitem_T *item, *item2;
9456 listitem_T *li;
9457 long idx;
9458 long end;
9459 char_u *key;
9460 dict_T *d;
9461 dictitem_T *di;
9462 char_u *arg_errmsg = (char_u *)N_("remove() argument");
9463
9464 if (argvars[0].v_type == VAR_DICT)
9465 {
9466 if (argvars[2].v_type != VAR_UNKNOWN)
9467 EMSG2(_(e_toomanyarg), "remove()");
9468 else if ((d = argvars[0].vval.v_dict) != NULL
9469 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
9470 {
9471 key = get_tv_string_chk(&argvars[1]);
9472 if (key != NULL)
9473 {
9474 di = dict_find(d, key, -1);
9475 if (di == NULL)
9476 EMSG2(_(e_dictkey), key);
9477 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
9478 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
9479 {
9480 *rettv = di->di_tv;
9481 init_tv(&di->di_tv);
9482 dictitem_remove(d, di);
9483 }
9484 }
9485 }
9486 }
9487 else if (argvars[0].v_type != VAR_LIST)
9488 EMSG2(_(e_listdictarg), "remove()");
9489 else if ((l = argvars[0].vval.v_list) != NULL
9490 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
9491 {
9492 int error = FALSE;
9493
9494 idx = (long)get_tv_number_chk(&argvars[1], &error);
9495 if (error)
9496 ; /* type error: do nothing, errmsg already given */
9497 else if ((item = list_find(l, idx)) == NULL)
9498 EMSGN(_(e_listidx), idx);
9499 else
9500 {
9501 if (argvars[2].v_type == VAR_UNKNOWN)
9502 {
9503 /* Remove one item, return its value. */
9504 vimlist_remove(l, item, item);
9505 *rettv = item->li_tv;
9506 vim_free(item);
9507 }
9508 else
9509 {
9510 /* Remove range of items, return list with values. */
9511 end = (long)get_tv_number_chk(&argvars[2], &error);
9512 if (error)
9513 ; /* type error: do nothing */
9514 else if ((item2 = list_find(l, end)) == NULL)
9515 EMSGN(_(e_listidx), end);
9516 else
9517 {
9518 int cnt = 0;
9519
9520 for (li = item; li != NULL; li = li->li_next)
9521 {
9522 ++cnt;
9523 if (li == item2)
9524 break;
9525 }
9526 if (li == NULL) /* didn't find "item2" after "item" */
9527 EMSG(_(e_invrange));
9528 else
9529 {
9530 vimlist_remove(l, item, item2);
9531 if (rettv_list_alloc(rettv) == OK)
9532 {
9533 l = rettv->vval.v_list;
9534 l->lv_first = item;
9535 l->lv_last = item2;
9536 item->li_prev = NULL;
9537 item2->li_next = NULL;
9538 l->lv_len = cnt;
9539 }
9540 }
9541 }
9542 }
9543 }
9544 }
9545}
9546
9547/*
9548 * "rename({from}, {to})" function
9549 */
9550 static void
9551f_rename(typval_T *argvars, typval_T *rettv)
9552{
9553 char_u buf[NUMBUFLEN];
9554
9555 if (check_restricted() || check_secure())
9556 rettv->vval.v_number = -1;
9557 else
9558 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
9559 get_tv_string_buf(&argvars[1], buf));
9560}
9561
9562/*
9563 * "repeat()" function
9564 */
9565 static void
9566f_repeat(typval_T *argvars, typval_T *rettv)
9567{
9568 char_u *p;
9569 int n;
9570 int slen;
9571 int len;
9572 char_u *r;
9573 int i;
9574
9575 n = (int)get_tv_number(&argvars[1]);
9576 if (argvars[0].v_type == VAR_LIST)
9577 {
9578 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9579 while (n-- > 0)
9580 if (list_extend(rettv->vval.v_list,
9581 argvars[0].vval.v_list, NULL) == FAIL)
9582 break;
9583 }
9584 else
9585 {
9586 p = get_tv_string(&argvars[0]);
9587 rettv->v_type = VAR_STRING;
9588 rettv->vval.v_string = NULL;
9589
9590 slen = (int)STRLEN(p);
9591 len = slen * n;
9592 if (len <= 0)
9593 return;
9594
9595 r = alloc(len + 1);
9596 if (r != NULL)
9597 {
9598 for (i = 0; i < n; i++)
9599 mch_memmove(r + i * slen, p, (size_t)slen);
9600 r[len] = NUL;
9601 }
9602
9603 rettv->vval.v_string = r;
9604 }
9605}
9606
9607/*
9608 * "resolve()" function
9609 */
9610 static void
9611f_resolve(typval_T *argvars, typval_T *rettv)
9612{
9613 char_u *p;
9614#ifdef HAVE_READLINK
9615 char_u *buf = NULL;
9616#endif
9617
9618 p = get_tv_string(&argvars[0]);
9619#ifdef FEAT_SHORTCUT
9620 {
9621 char_u *v = NULL;
9622
9623 v = mch_resolve_shortcut(p);
9624 if (v != NULL)
9625 rettv->vval.v_string = v;
9626 else
9627 rettv->vval.v_string = vim_strsave(p);
9628 }
9629#else
9630# ifdef HAVE_READLINK
9631 {
9632 char_u *cpy;
9633 int len;
9634 char_u *remain = NULL;
9635 char_u *q;
9636 int is_relative_to_current = FALSE;
9637 int has_trailing_pathsep = FALSE;
9638 int limit = 100;
9639
9640 p = vim_strsave(p);
9641
9642 if (p[0] == '.' && (vim_ispathsep(p[1])
9643 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9644 is_relative_to_current = TRUE;
9645
9646 len = STRLEN(p);
9647 if (len > 0 && after_pathsep(p, p + len))
9648 {
9649 has_trailing_pathsep = TRUE;
9650 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9651 }
9652
9653 q = getnextcomp(p);
9654 if (*q != NUL)
9655 {
9656 /* Separate the first path component in "p", and keep the
9657 * remainder (beginning with the path separator). */
9658 remain = vim_strsave(q - 1);
9659 q[-1] = NUL;
9660 }
9661
9662 buf = alloc(MAXPATHL + 1);
9663 if (buf == NULL)
9664 goto fail;
9665
9666 for (;;)
9667 {
9668 for (;;)
9669 {
9670 len = readlink((char *)p, (char *)buf, MAXPATHL);
9671 if (len <= 0)
9672 break;
9673 buf[len] = NUL;
9674
9675 if (limit-- == 0)
9676 {
9677 vim_free(p);
9678 vim_free(remain);
9679 EMSG(_("E655: Too many symbolic links (cycle?)"));
9680 rettv->vval.v_string = NULL;
9681 goto fail;
9682 }
9683
9684 /* Ensure that the result will have a trailing path separator
9685 * if the argument has one. */
9686 if (remain == NULL && has_trailing_pathsep)
9687 add_pathsep(buf);
9688
9689 /* Separate the first path component in the link value and
9690 * concatenate the remainders. */
9691 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9692 if (*q != NUL)
9693 {
9694 if (remain == NULL)
9695 remain = vim_strsave(q - 1);
9696 else
9697 {
9698 cpy = concat_str(q - 1, remain);
9699 if (cpy != NULL)
9700 {
9701 vim_free(remain);
9702 remain = cpy;
9703 }
9704 }
9705 q[-1] = NUL;
9706 }
9707
9708 q = gettail(p);
9709 if (q > p && *q == NUL)
9710 {
9711 /* Ignore trailing path separator. */
9712 q[-1] = NUL;
9713 q = gettail(p);
9714 }
9715 if (q > p && !mch_isFullName(buf))
9716 {
9717 /* symlink is relative to directory of argument */
9718 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
9719 if (cpy != NULL)
9720 {
9721 STRCPY(cpy, p);
9722 STRCPY(gettail(cpy), buf);
9723 vim_free(p);
9724 p = cpy;
9725 }
9726 }
9727 else
9728 {
9729 vim_free(p);
9730 p = vim_strsave(buf);
9731 }
9732 }
9733
9734 if (remain == NULL)
9735 break;
9736
9737 /* Append the first path component of "remain" to "p". */
9738 q = getnextcomp(remain + 1);
9739 len = q - remain - (*q != NUL);
9740 cpy = vim_strnsave(p, STRLEN(p) + len);
9741 if (cpy != NULL)
9742 {
9743 STRNCAT(cpy, remain, len);
9744 vim_free(p);
9745 p = cpy;
9746 }
9747 /* Shorten "remain". */
9748 if (*q != NUL)
9749 STRMOVE(remain, q - 1);
9750 else
Bram Moolenaard23a8232018-02-10 18:45:26 +01009751 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009752 }
9753
9754 /* If the result is a relative path name, make it explicitly relative to
9755 * the current directory if and only if the argument had this form. */
9756 if (!vim_ispathsep(*p))
9757 {
9758 if (is_relative_to_current
9759 && *p != NUL
9760 && !(p[0] == '.'
9761 && (p[1] == NUL
9762 || vim_ispathsep(p[1])
9763 || (p[1] == '.'
9764 && (p[2] == NUL
9765 || vim_ispathsep(p[2]))))))
9766 {
9767 /* Prepend "./". */
9768 cpy = concat_str((char_u *)"./", p);
9769 if (cpy != NULL)
9770 {
9771 vim_free(p);
9772 p = cpy;
9773 }
9774 }
9775 else if (!is_relative_to_current)
9776 {
9777 /* Strip leading "./". */
9778 q = p;
9779 while (q[0] == '.' && vim_ispathsep(q[1]))
9780 q += 2;
9781 if (q > p)
9782 STRMOVE(p, p + 2);
9783 }
9784 }
9785
9786 /* Ensure that the result will have no trailing path separator
9787 * if the argument had none. But keep "/" or "//". */
9788 if (!has_trailing_pathsep)
9789 {
9790 q = p + STRLEN(p);
9791 if (after_pathsep(p, q))
9792 *gettail_sep(p) = NUL;
9793 }
9794
9795 rettv->vval.v_string = p;
9796 }
9797# else
9798 rettv->vval.v_string = vim_strsave(p);
9799# endif
9800#endif
9801
9802 simplify_filename(rettv->vval.v_string);
9803
9804#ifdef HAVE_READLINK
9805fail:
9806 vim_free(buf);
9807#endif
9808 rettv->v_type = VAR_STRING;
9809}
9810
9811/*
9812 * "reverse({list})" function
9813 */
9814 static void
9815f_reverse(typval_T *argvars, typval_T *rettv)
9816{
9817 list_T *l;
9818 listitem_T *li, *ni;
9819
9820 if (argvars[0].v_type != VAR_LIST)
9821 EMSG2(_(e_listarg), "reverse()");
9822 else if ((l = argvars[0].vval.v_list) != NULL
9823 && !tv_check_lock(l->lv_lock,
9824 (char_u *)N_("reverse() argument"), TRUE))
9825 {
9826 li = l->lv_last;
9827 l->lv_first = l->lv_last = NULL;
9828 l->lv_len = 0;
9829 while (li != NULL)
9830 {
9831 ni = li->li_prev;
9832 list_append(l, li);
9833 li = ni;
9834 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009835 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009836 l->lv_idx = l->lv_len - l->lv_idx - 1;
9837 }
9838}
9839
9840#define SP_NOMOVE 0x01 /* don't move cursor */
9841#define SP_REPEAT 0x02 /* repeat to find outer pair */
9842#define SP_RETCOUNT 0x04 /* return matchcount */
9843#define SP_SETPCMARK 0x08 /* set previous context mark */
9844#define SP_START 0x10 /* accept match at start position */
9845#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
9846#define SP_END 0x40 /* leave cursor at end of match */
9847#define SP_COLUMN 0x80 /* start at cursor column */
9848
9849static int get_search_arg(typval_T *varp, int *flagsp);
9850
9851/*
9852 * Get flags for a search function.
9853 * Possibly sets "p_ws".
9854 * Returns BACKWARD, FORWARD or zero (for an error).
9855 */
9856 static int
9857get_search_arg(typval_T *varp, int *flagsp)
9858{
9859 int dir = FORWARD;
9860 char_u *flags;
9861 char_u nbuf[NUMBUFLEN];
9862 int mask;
9863
9864 if (varp->v_type != VAR_UNKNOWN)
9865 {
9866 flags = get_tv_string_buf_chk(varp, nbuf);
9867 if (flags == NULL)
9868 return 0; /* type error; errmsg already given */
9869 while (*flags != NUL)
9870 {
9871 switch (*flags)
9872 {
9873 case 'b': dir = BACKWARD; break;
9874 case 'w': p_ws = TRUE; break;
9875 case 'W': p_ws = FALSE; break;
9876 default: mask = 0;
9877 if (flagsp != NULL)
9878 switch (*flags)
9879 {
9880 case 'c': mask = SP_START; break;
9881 case 'e': mask = SP_END; break;
9882 case 'm': mask = SP_RETCOUNT; break;
9883 case 'n': mask = SP_NOMOVE; break;
9884 case 'p': mask = SP_SUBPAT; break;
9885 case 'r': mask = SP_REPEAT; break;
9886 case 's': mask = SP_SETPCMARK; break;
9887 case 'z': mask = SP_COLUMN; break;
9888 }
9889 if (mask == 0)
9890 {
9891 EMSG2(_(e_invarg2), flags);
9892 dir = 0;
9893 }
9894 else
9895 *flagsp |= mask;
9896 }
9897 if (dir == 0)
9898 break;
9899 ++flags;
9900 }
9901 }
9902 return dir;
9903}
9904
9905/*
9906 * Shared by search() and searchpos() functions.
9907 */
9908 static int
9909search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
9910{
9911 int flags;
9912 char_u *pat;
9913 pos_T pos;
9914 pos_T save_cursor;
9915 int save_p_ws = p_ws;
9916 int dir;
9917 int retval = 0; /* default: FAIL */
9918 long lnum_stop = 0;
9919 proftime_T tm;
9920#ifdef FEAT_RELTIME
9921 long time_limit = 0;
9922#endif
9923 int options = SEARCH_KEEP;
9924 int subpatnum;
9925
9926 pat = get_tv_string(&argvars[0]);
9927 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
9928 if (dir == 0)
9929 goto theend;
9930 flags = *flagsp;
9931 if (flags & SP_START)
9932 options |= SEARCH_START;
9933 if (flags & SP_END)
9934 options |= SEARCH_END;
9935 if (flags & SP_COLUMN)
9936 options |= SEARCH_COL;
9937
9938 /* Optional arguments: line number to stop searching and timeout. */
9939 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9940 {
9941 lnum_stop = (long)get_tv_number_chk(&argvars[2], NULL);
9942 if (lnum_stop < 0)
9943 goto theend;
9944#ifdef FEAT_RELTIME
9945 if (argvars[3].v_type != VAR_UNKNOWN)
9946 {
9947 time_limit = (long)get_tv_number_chk(&argvars[3], NULL);
9948 if (time_limit < 0)
9949 goto theend;
9950 }
9951#endif
9952 }
9953
9954#ifdef FEAT_RELTIME
9955 /* Set the time limit, if there is one. */
9956 profile_setlimit(time_limit, &tm);
9957#endif
9958
9959 /*
9960 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
9961 * Check to make sure only those flags are set.
9962 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
9963 * flags cannot be set. Check for that condition also.
9964 */
9965 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
9966 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9967 {
9968 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
9969 goto theend;
9970 }
9971
9972 pos = save_cursor = curwin->w_cursor;
9973 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009974 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009975 if (subpatnum != FAIL)
9976 {
9977 if (flags & SP_SUBPAT)
9978 retval = subpatnum;
9979 else
9980 retval = pos.lnum;
9981 if (flags & SP_SETPCMARK)
9982 setpcmark();
9983 curwin->w_cursor = pos;
9984 if (match_pos != NULL)
9985 {
9986 /* Store the match cursor position */
9987 match_pos->lnum = pos.lnum;
9988 match_pos->col = pos.col + 1;
9989 }
9990 /* "/$" will put the cursor after the end of the line, may need to
9991 * correct that here */
9992 check_cursor();
9993 }
9994
9995 /* If 'n' flag is used: restore cursor position. */
9996 if (flags & SP_NOMOVE)
9997 curwin->w_cursor = save_cursor;
9998 else
9999 curwin->w_set_curswant = TRUE;
10000theend:
10001 p_ws = save_p_ws;
10002
10003 return retval;
10004}
10005
10006#ifdef FEAT_FLOAT
10007
10008/*
10009 * round() is not in C90, use ceil() or floor() instead.
10010 */
10011 float_T
10012vim_round(float_T f)
10013{
10014 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
10015}
10016
10017/*
10018 * "round({float})" function
10019 */
10020 static void
10021f_round(typval_T *argvars, typval_T *rettv)
10022{
10023 float_T f = 0.0;
10024
10025 rettv->v_type = VAR_FLOAT;
10026 if (get_float_arg(argvars, &f) == OK)
10027 rettv->vval.v_float = vim_round(f);
10028 else
10029 rettv->vval.v_float = 0.0;
10030}
10031#endif
10032
10033/*
10034 * "screenattr()" function
10035 */
10036 static void
10037f_screenattr(typval_T *argvars, typval_T *rettv)
10038{
10039 int row;
10040 int col;
10041 int c;
10042
10043 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
10044 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
10045 if (row < 0 || row >= screen_Rows
10046 || col < 0 || col >= screen_Columns)
10047 c = -1;
10048 else
10049 c = ScreenAttrs[LineOffset[row] + col];
10050 rettv->vval.v_number = c;
10051}
10052
10053/*
10054 * "screenchar()" function
10055 */
10056 static void
10057f_screenchar(typval_T *argvars, typval_T *rettv)
10058{
10059 int row;
10060 int col;
10061 int off;
10062 int c;
10063
10064 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
10065 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
10066 if (row < 0 || row >= screen_Rows
10067 || col < 0 || col >= screen_Columns)
10068 c = -1;
10069 else
10070 {
10071 off = LineOffset[row] + col;
10072#ifdef FEAT_MBYTE
10073 if (enc_utf8 && ScreenLinesUC[off] != 0)
10074 c = ScreenLinesUC[off];
10075 else
10076#endif
10077 c = ScreenLines[off];
10078 }
10079 rettv->vval.v_number = c;
10080}
10081
10082/*
10083 * "screencol()" function
10084 *
10085 * First column is 1 to be consistent with virtcol().
10086 */
10087 static void
10088f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10089{
10090 rettv->vval.v_number = screen_screencol() + 1;
10091}
10092
10093/*
10094 * "screenrow()" function
10095 */
10096 static void
10097f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10098{
10099 rettv->vval.v_number = screen_screenrow() + 1;
10100}
10101
10102/*
10103 * "search()" function
10104 */
10105 static void
10106f_search(typval_T *argvars, typval_T *rettv)
10107{
10108 int flags = 0;
10109
10110 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10111}
10112
10113/*
10114 * "searchdecl()" function
10115 */
10116 static void
10117f_searchdecl(typval_T *argvars, typval_T *rettv)
10118{
10119 int locally = 1;
10120 int thisblock = 0;
10121 int error = FALSE;
10122 char_u *name;
10123
10124 rettv->vval.v_number = 1; /* default: FAIL */
10125
10126 name = get_tv_string_chk(&argvars[0]);
10127 if (argvars[1].v_type != VAR_UNKNOWN)
10128 {
10129 locally = (int)get_tv_number_chk(&argvars[1], &error) == 0;
10130 if (!error && argvars[2].v_type != VAR_UNKNOWN)
10131 thisblock = (int)get_tv_number_chk(&argvars[2], &error) != 0;
10132 }
10133 if (!error && name != NULL)
10134 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10135 locally, thisblock, SEARCH_KEEP) == FAIL;
10136}
10137
10138/*
10139 * Used by searchpair() and searchpairpos()
10140 */
10141 static int
10142searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10143{
10144 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010145 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010146 int save_p_ws = p_ws;
10147 int dir;
10148 int flags = 0;
10149 char_u nbuf1[NUMBUFLEN];
10150 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010151 int retval = 0; /* default: FAIL */
10152 long lnum_stop = 0;
10153 long time_limit = 0;
10154
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010155 /* Get the three pattern arguments: start, middle, end. Will result in an
10156 * error if not a valid argument. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010157 spat = get_tv_string_chk(&argvars[0]);
10158 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
10159 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
10160 if (spat == NULL || mpat == NULL || epat == NULL)
10161 goto theend; /* type error */
10162
10163 /* Handle the optional fourth argument: flags */
10164 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
10165 if (dir == 0)
10166 goto theend;
10167
10168 /* Don't accept SP_END or SP_SUBPAT.
10169 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
10170 */
10171 if ((flags & (SP_END | SP_SUBPAT)) != 0
10172 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10173 {
10174 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
10175 goto theend;
10176 }
10177
10178 /* Using 'r' implies 'W', otherwise it doesn't work. */
10179 if (flags & SP_REPEAT)
10180 p_ws = FALSE;
10181
10182 /* Optional fifth argument: skip expression */
10183 if (argvars[3].v_type == VAR_UNKNOWN
10184 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010010185 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010186 else
10187 {
Bram Moolenaar48570482017-10-30 21:48:41 +010010188 skip = &argvars[4];
10189 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
10190 && skip->v_type != VAR_STRING)
10191 {
10192 /* Type error */
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010193 EMSG2(_(e_invarg2), get_tv_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010010194 goto theend;
10195 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010196 if (argvars[5].v_type != VAR_UNKNOWN)
10197 {
10198 lnum_stop = (long)get_tv_number_chk(&argvars[5], NULL);
10199 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010200 {
10201 EMSG2(_(e_invarg2), get_tv_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010202 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010203 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010204#ifdef FEAT_RELTIME
10205 if (argvars[6].v_type != VAR_UNKNOWN)
10206 {
10207 time_limit = (long)get_tv_number_chk(&argvars[6], NULL);
10208 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010209 {
10210 EMSG2(_(e_invarg2), get_tv_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010211 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010212 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010213 }
10214#endif
10215 }
10216 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010217
10218 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
10219 match_pos, lnum_stop, time_limit);
10220
10221theend:
10222 p_ws = save_p_ws;
10223
10224 return retval;
10225}
10226
10227/*
10228 * "searchpair()" function
10229 */
10230 static void
10231f_searchpair(typval_T *argvars, typval_T *rettv)
10232{
10233 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
10234}
10235
10236/*
10237 * "searchpairpos()" function
10238 */
10239 static void
10240f_searchpairpos(typval_T *argvars, typval_T *rettv)
10241{
10242 pos_T match_pos;
10243 int lnum = 0;
10244 int col = 0;
10245
10246 if (rettv_list_alloc(rettv) == FAIL)
10247 return;
10248
10249 if (searchpair_cmn(argvars, &match_pos) > 0)
10250 {
10251 lnum = match_pos.lnum;
10252 col = match_pos.col;
10253 }
10254
10255 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10256 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10257}
10258
10259/*
10260 * Search for a start/middle/end thing.
10261 * Used by searchpair(), see its documentation for the details.
10262 * Returns 0 or -1 for no match,
10263 */
10264 long
10265do_searchpair(
10266 char_u *spat, /* start pattern */
10267 char_u *mpat, /* middle pattern */
10268 char_u *epat, /* end pattern */
10269 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010010270 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010271 int flags, /* SP_SETPCMARK and other SP_ values */
10272 pos_T *match_pos,
10273 linenr_T lnum_stop, /* stop at this line if not zero */
10274 long time_limit UNUSED) /* stop after this many msec */
10275{
10276 char_u *save_cpo;
10277 char_u *pat, *pat2 = NULL, *pat3 = NULL;
10278 long retval = 0;
10279 pos_T pos;
10280 pos_T firstpos;
10281 pos_T foundpos;
10282 pos_T save_cursor;
10283 pos_T save_pos;
10284 int n;
10285 int r;
10286 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010010287 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010288 int err;
10289 int options = SEARCH_KEEP;
10290 proftime_T tm;
10291
10292 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10293 save_cpo = p_cpo;
10294 p_cpo = empty_option;
10295
10296#ifdef FEAT_RELTIME
10297 /* Set the time limit, if there is one. */
10298 profile_setlimit(time_limit, &tm);
10299#endif
10300
10301 /* Make two search patterns: start/end (pat2, for in nested pairs) and
10302 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010303 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
10304 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010305 if (pat2 == NULL || pat3 == NULL)
10306 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010307 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010308 if (*mpat == NUL)
10309 STRCPY(pat3, pat2);
10310 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010311 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010312 spat, epat, mpat);
10313 if (flags & SP_START)
10314 options |= SEARCH_START;
10315
Bram Moolenaar48570482017-10-30 21:48:41 +010010316 if (skip != NULL)
10317 {
10318 /* Empty string means to not use the skip expression. */
10319 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
10320 use_skip = skip->vval.v_string != NULL
10321 && *skip->vval.v_string != NUL;
10322 }
10323
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010324 save_cursor = curwin->w_cursor;
10325 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010326 CLEAR_POS(&firstpos);
10327 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010328 pat = pat3;
10329 for (;;)
10330 {
10331 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010332 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010333 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010334 /* didn't find it or found the first match again: FAIL */
10335 break;
10336
10337 if (firstpos.lnum == 0)
10338 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010339 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010340 {
10341 /* Found the same position again. Can happen with a pattern that
10342 * has "\zs" at the end and searching backwards. Advance one
10343 * character and try again. */
10344 if (dir == BACKWARD)
10345 decl(&pos);
10346 else
10347 incl(&pos);
10348 }
10349 foundpos = pos;
10350
10351 /* clear the start flag to avoid getting stuck here */
10352 options &= ~SEARCH_START;
10353
10354 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010010355 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010356 {
10357 save_pos = curwin->w_cursor;
10358 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010010359 err = FALSE;
10360 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010361 curwin->w_cursor = save_pos;
10362 if (err)
10363 {
10364 /* Evaluating {skip} caused an error, break here. */
10365 curwin->w_cursor = save_cursor;
10366 retval = -1;
10367 break;
10368 }
10369 if (r)
10370 continue;
10371 }
10372
10373 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
10374 {
10375 /* Found end when searching backwards or start when searching
10376 * forward: nested pair. */
10377 ++nest;
10378 pat = pat2; /* nested, don't search for middle */
10379 }
10380 else
10381 {
10382 /* Found end when searching forward or start when searching
10383 * backward: end of (nested) pair; or found middle in outer pair. */
10384 if (--nest == 1)
10385 pat = pat3; /* outer level, search for middle */
10386 }
10387
10388 if (nest == 0)
10389 {
10390 /* Found the match: return matchcount or line number. */
10391 if (flags & SP_RETCOUNT)
10392 ++retval;
10393 else
10394 retval = pos.lnum;
10395 if (flags & SP_SETPCMARK)
10396 setpcmark();
10397 curwin->w_cursor = pos;
10398 if (!(flags & SP_REPEAT))
10399 break;
10400 nest = 1; /* search for next unmatched */
10401 }
10402 }
10403
10404 if (match_pos != NULL)
10405 {
10406 /* Store the match cursor position */
10407 match_pos->lnum = curwin->w_cursor.lnum;
10408 match_pos->col = curwin->w_cursor.col + 1;
10409 }
10410
10411 /* If 'n' flag is used or search failed: restore cursor position. */
10412 if ((flags & SP_NOMOVE) || retval == 0)
10413 curwin->w_cursor = save_cursor;
10414
10415theend:
10416 vim_free(pat2);
10417 vim_free(pat3);
10418 if (p_cpo == empty_option)
10419 p_cpo = save_cpo;
10420 else
10421 /* Darn, evaluating the {skip} expression changed the value. */
10422 free_string_option(save_cpo);
10423
10424 return retval;
10425}
10426
10427/*
10428 * "searchpos()" function
10429 */
10430 static void
10431f_searchpos(typval_T *argvars, typval_T *rettv)
10432{
10433 pos_T match_pos;
10434 int lnum = 0;
10435 int col = 0;
10436 int n;
10437 int flags = 0;
10438
10439 if (rettv_list_alloc(rettv) == FAIL)
10440 return;
10441
10442 n = search_cmn(argvars, &match_pos, &flags);
10443 if (n > 0)
10444 {
10445 lnum = match_pos.lnum;
10446 col = match_pos.col;
10447 }
10448
10449 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10450 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10451 if (flags & SP_SUBPAT)
10452 list_append_number(rettv->vval.v_list, (varnumber_T)n);
10453}
10454
10455 static void
10456f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
10457{
10458#ifdef FEAT_CLIENTSERVER
10459 char_u buf[NUMBUFLEN];
10460 char_u *server = get_tv_string_chk(&argvars[0]);
10461 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
10462
10463 rettv->vval.v_number = -1;
10464 if (server == NULL || reply == NULL)
10465 return;
10466 if (check_restricted() || check_secure())
10467 return;
10468# ifdef FEAT_X11
10469 if (check_connection() == FAIL)
10470 return;
10471# endif
10472
10473 if (serverSendReply(server, reply) < 0)
10474 {
10475 EMSG(_("E258: Unable to send to client"));
10476 return;
10477 }
10478 rettv->vval.v_number = 0;
10479#else
10480 rettv->vval.v_number = -1;
10481#endif
10482}
10483
10484 static void
10485f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
10486{
10487 char_u *r = NULL;
10488
10489#ifdef FEAT_CLIENTSERVER
10490# ifdef WIN32
10491 r = serverGetVimNames();
10492# else
10493 make_connection();
10494 if (X_DISPLAY != NULL)
10495 r = serverGetVimNames(X_DISPLAY);
10496# endif
10497#endif
10498 rettv->v_type = VAR_STRING;
10499 rettv->vval.v_string = r;
10500}
10501
10502/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010503 * "setbufline()" function
10504 */
10505 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020010506f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010507{
10508 linenr_T lnum;
10509 buf_T *buf;
10510
10511 buf = get_buf_tv(&argvars[0], FALSE);
10512 if (buf == NULL)
10513 rettv->vval.v_number = 1; /* FAIL */
10514 else
10515 {
10516 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020010517 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010518 }
10519}
10520
10521/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010522 * "setbufvar()" function
10523 */
10524 static void
10525f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10526{
10527 buf_T *buf;
10528 char_u *varname, *bufvarname;
10529 typval_T *varp;
10530 char_u nbuf[NUMBUFLEN];
10531
10532 if (check_restricted() || check_secure())
10533 return;
10534 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10535 varname = get_tv_string_chk(&argvars[1]);
10536 buf = get_buf_tv(&argvars[0], FALSE);
10537 varp = &argvars[2];
10538
10539 if (buf != NULL && varname != NULL && varp != NULL)
10540 {
10541 if (*varname == '&')
10542 {
10543 long numval;
10544 char_u *strval;
10545 int error = FALSE;
10546 aco_save_T aco;
10547
10548 /* set curbuf to be our buf, temporarily */
10549 aucmd_prepbuf(&aco, buf);
10550
10551 ++varname;
10552 numval = (long)get_tv_number_chk(varp, &error);
10553 strval = get_tv_string_buf_chk(varp, nbuf);
10554 if (!error && strval != NULL)
10555 set_option_value(varname, numval, strval, OPT_LOCAL);
10556
10557 /* reset notion of buffer */
10558 aucmd_restbuf(&aco);
10559 }
10560 else
10561 {
10562 buf_T *save_curbuf = curbuf;
10563
10564 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
10565 if (bufvarname != NULL)
10566 {
10567 curbuf = buf;
10568 STRCPY(bufvarname, "b:");
10569 STRCPY(bufvarname + 2, varname);
10570 set_var(bufvarname, varp, TRUE);
10571 vim_free(bufvarname);
10572 curbuf = save_curbuf;
10573 }
10574 }
10575 }
10576}
10577
10578 static void
10579f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10580{
10581 dict_T *d;
10582 dictitem_T *di;
10583 char_u *csearch;
10584
10585 if (argvars[0].v_type != VAR_DICT)
10586 {
10587 EMSG(_(e_dictreq));
10588 return;
10589 }
10590
10591 if ((d = argvars[0].vval.v_dict) != NULL)
10592 {
10593 csearch = get_dict_string(d, (char_u *)"char", FALSE);
10594 if (csearch != NULL)
10595 {
10596#ifdef FEAT_MBYTE
10597 if (enc_utf8)
10598 {
10599 int pcc[MAX_MCO];
10600 int c = utfc_ptr2char(csearch, pcc);
10601
10602 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
10603 }
10604 else
10605#endif
10606 set_last_csearch(PTR2CHAR(csearch),
10607 csearch, MB_PTR2LEN(csearch));
10608 }
10609
10610 di = dict_find(d, (char_u *)"forward", -1);
10611 if (di != NULL)
10612 set_csearch_direction((int)get_tv_number(&di->di_tv)
10613 ? FORWARD : BACKWARD);
10614
10615 di = dict_find(d, (char_u *)"until", -1);
10616 if (di != NULL)
10617 set_csearch_until(!!get_tv_number(&di->di_tv));
10618 }
10619}
10620
10621/*
10622 * "setcmdpos()" function
10623 */
10624 static void
10625f_setcmdpos(typval_T *argvars, typval_T *rettv)
10626{
10627 int pos = (int)get_tv_number(&argvars[0]) - 1;
10628
10629 if (pos >= 0)
10630 rettv->vval.v_number = set_cmdline_pos(pos);
10631}
10632
10633/*
10634 * "setfperm({fname}, {mode})" function
10635 */
10636 static void
10637f_setfperm(typval_T *argvars, typval_T *rettv)
10638{
10639 char_u *fname;
10640 char_u modebuf[NUMBUFLEN];
10641 char_u *mode_str;
10642 int i;
10643 int mask;
10644 int mode = 0;
10645
10646 rettv->vval.v_number = 0;
10647 fname = get_tv_string_chk(&argvars[0]);
10648 if (fname == NULL)
10649 return;
10650 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
10651 if (mode_str == NULL)
10652 return;
10653 if (STRLEN(mode_str) != 9)
10654 {
10655 EMSG2(_(e_invarg2), mode_str);
10656 return;
10657 }
10658
10659 mask = 1;
10660 for (i = 8; i >= 0; --i)
10661 {
10662 if (mode_str[i] != '-')
10663 mode |= mask;
10664 mask = mask << 1;
10665 }
10666 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
10667}
10668
10669/*
10670 * "setline()" function
10671 */
10672 static void
10673f_setline(typval_T *argvars, typval_T *rettv)
10674{
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010675 linenr_T lnum = get_tv_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010676
Bram Moolenaarca851592018-06-06 21:04:07 +020010677 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010678}
10679
Bram Moolenaard823fa92016-08-12 16:29:27 +020010680static 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 +020010681
10682/*
10683 * Used by "setqflist()" and "setloclist()" functions
10684 */
10685 static void
10686set_qf_ll_list(
10687 win_T *wp UNUSED,
10688 typval_T *list_arg UNUSED,
10689 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020010690 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010691 typval_T *rettv)
10692{
10693#ifdef FEAT_QUICKFIX
10694 static char *e_invact = N_("E927: Invalid action: '%s'");
10695 char_u *act;
10696 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010697 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010698#endif
10699
10700 rettv->vval.v_number = -1;
10701
10702#ifdef FEAT_QUICKFIX
10703 if (list_arg->v_type != VAR_LIST)
10704 EMSG(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010705 else if (recursive != 0)
10706 EMSG(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010707 else
10708 {
10709 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010710 dict_T *d = NULL;
10711 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010712
10713 if (action_arg->v_type == VAR_STRING)
10714 {
10715 act = get_tv_string_chk(action_arg);
10716 if (act == NULL)
10717 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020010718 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
10719 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010720 action = *act;
10721 else
10722 EMSG2(_(e_invact), act);
10723 }
10724 else if (action_arg->v_type == VAR_UNKNOWN)
10725 action = ' ';
10726 else
10727 EMSG(_(e_stringreq));
10728
Bram Moolenaard823fa92016-08-12 16:29:27 +020010729 if (action_arg->v_type != VAR_UNKNOWN
10730 && what_arg->v_type != VAR_UNKNOWN)
10731 {
10732 if (what_arg->v_type == VAR_DICT)
10733 d = what_arg->vval.v_dict;
10734 else
10735 {
10736 EMSG(_(e_dictreq));
10737 valid_dict = FALSE;
10738 }
10739 }
10740
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010741 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010742 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010743 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
10744 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010745 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010746 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010747 }
10748#endif
10749}
10750
10751/*
10752 * "setloclist()" function
10753 */
10754 static void
10755f_setloclist(typval_T *argvars, typval_T *rettv)
10756{
10757 win_T *win;
10758
10759 rettv->vval.v_number = -1;
10760
10761 win = find_win_by_nr(&argvars[0], NULL);
10762 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020010763 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010764}
10765
10766/*
10767 * "setmatches()" function
10768 */
10769 static void
10770f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10771{
10772#ifdef FEAT_SEARCH_EXTRA
10773 list_T *l;
10774 listitem_T *li;
10775 dict_T *d;
10776 list_T *s = NULL;
10777
10778 rettv->vval.v_number = -1;
10779 if (argvars[0].v_type != VAR_LIST)
10780 {
10781 EMSG(_(e_listreq));
10782 return;
10783 }
10784 if ((l = argvars[0].vval.v_list) != NULL)
10785 {
10786
10787 /* To some extent make sure that we are dealing with a list from
10788 * "getmatches()". */
10789 li = l->lv_first;
10790 while (li != NULL)
10791 {
10792 if (li->li_tv.v_type != VAR_DICT
10793 || (d = li->li_tv.vval.v_dict) == NULL)
10794 {
10795 EMSG(_(e_invarg));
10796 return;
10797 }
10798 if (!(dict_find(d, (char_u *)"group", -1) != NULL
10799 && (dict_find(d, (char_u *)"pattern", -1) != NULL
10800 || dict_find(d, (char_u *)"pos1", -1) != NULL)
10801 && dict_find(d, (char_u *)"priority", -1) != NULL
10802 && dict_find(d, (char_u *)"id", -1) != NULL))
10803 {
10804 EMSG(_(e_invarg));
10805 return;
10806 }
10807 li = li->li_next;
10808 }
10809
10810 clear_matches(curwin);
10811 li = l->lv_first;
10812 while (li != NULL)
10813 {
10814 int i = 0;
10815 char_u buf[5];
10816 dictitem_T *di;
10817 char_u *group;
10818 int priority;
10819 int id;
10820 char_u *conceal;
10821
10822 d = li->li_tv.vval.v_dict;
10823 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
10824 {
10825 if (s == NULL)
10826 {
10827 s = list_alloc();
10828 if (s == NULL)
10829 return;
10830 }
10831
10832 /* match from matchaddpos() */
10833 for (i = 1; i < 9; i++)
10834 {
10835 sprintf((char *)buf, (char *)"pos%d", i);
10836 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
10837 {
10838 if (di->di_tv.v_type != VAR_LIST)
10839 return;
10840
10841 list_append_tv(s, &di->di_tv);
10842 s->lv_refcount++;
10843 }
10844 else
10845 break;
10846 }
10847 }
10848
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010849 group = get_dict_string(d, (char_u *)"group", TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010850 priority = (int)get_dict_number(d, (char_u *)"priority");
10851 id = (int)get_dict_number(d, (char_u *)"id");
10852 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010853 ? get_dict_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010854 : NULL;
10855 if (i == 0)
10856 {
10857 match_add(curwin, group,
10858 get_dict_string(d, (char_u *)"pattern", FALSE),
10859 priority, id, NULL, conceal);
10860 }
10861 else
10862 {
10863 match_add(curwin, group, NULL, priority, id, s, conceal);
10864 list_unref(s);
10865 s = NULL;
10866 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010867 vim_free(group);
10868 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010869
10870 li = li->li_next;
10871 }
10872 rettv->vval.v_number = 0;
10873 }
10874#endif
10875}
10876
10877/*
10878 * "setpos()" function
10879 */
10880 static void
10881f_setpos(typval_T *argvars, typval_T *rettv)
10882{
10883 pos_T pos;
10884 int fnum;
10885 char_u *name;
10886 colnr_T curswant = -1;
10887
10888 rettv->vval.v_number = -1;
10889 name = get_tv_string_chk(argvars);
10890 if (name != NULL)
10891 {
10892 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
10893 {
10894 if (--pos.col < 0)
10895 pos.col = 0;
10896 if (name[0] == '.' && name[1] == NUL)
10897 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010898 /* set cursor; "fnum" is ignored */
10899 curwin->w_cursor = pos;
10900 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010901 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010902 curwin->w_curswant = curswant - 1;
10903 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010904 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010905 check_cursor();
10906 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010907 }
10908 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
10909 {
10910 /* set mark */
10911 if (setmark_pos(name[1], &pos, fnum) == OK)
10912 rettv->vval.v_number = 0;
10913 }
10914 else
10915 EMSG(_(e_invarg));
10916 }
10917 }
10918}
10919
10920/*
10921 * "setqflist()" function
10922 */
10923 static void
10924f_setqflist(typval_T *argvars, typval_T *rettv)
10925{
Bram Moolenaard823fa92016-08-12 16:29:27 +020010926 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010927}
10928
10929/*
10930 * "setreg()" function
10931 */
10932 static void
10933f_setreg(typval_T *argvars, typval_T *rettv)
10934{
10935 int regname;
10936 char_u *strregname;
10937 char_u *stropt;
10938 char_u *strval;
10939 int append;
10940 char_u yank_type;
10941 long block_len;
10942
10943 block_len = -1;
10944 yank_type = MAUTO;
10945 append = FALSE;
10946
10947 strregname = get_tv_string_chk(argvars);
10948 rettv->vval.v_number = 1; /* FAIL is default */
10949
10950 if (strregname == NULL)
10951 return; /* type error; errmsg already given */
10952 regname = *strregname;
10953 if (regname == 0 || regname == '@')
10954 regname = '"';
10955
10956 if (argvars[2].v_type != VAR_UNKNOWN)
10957 {
10958 stropt = get_tv_string_chk(&argvars[2]);
10959 if (stropt == NULL)
10960 return; /* type error */
10961 for (; *stropt != NUL; ++stropt)
10962 switch (*stropt)
10963 {
10964 case 'a': case 'A': /* append */
10965 append = TRUE;
10966 break;
10967 case 'v': case 'c': /* character-wise selection */
10968 yank_type = MCHAR;
10969 break;
10970 case 'V': case 'l': /* line-wise selection */
10971 yank_type = MLINE;
10972 break;
10973 case 'b': case Ctrl_V: /* block-wise selection */
10974 yank_type = MBLOCK;
10975 if (VIM_ISDIGIT(stropt[1]))
10976 {
10977 ++stropt;
10978 block_len = getdigits(&stropt) - 1;
10979 --stropt;
10980 }
10981 break;
10982 }
10983 }
10984
10985 if (argvars[1].v_type == VAR_LIST)
10986 {
10987 char_u **lstval;
10988 char_u **allocval;
10989 char_u buf[NUMBUFLEN];
10990 char_u **curval;
10991 char_u **curallocval;
10992 list_T *ll = argvars[1].vval.v_list;
10993 listitem_T *li;
10994 int len;
10995
10996 /* If the list is NULL handle like an empty list. */
10997 len = ll == NULL ? 0 : ll->lv_len;
10998
10999 /* First half: use for pointers to result lines; second half: use for
11000 * pointers to allocated copies. */
11001 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
11002 if (lstval == NULL)
11003 return;
11004 curval = lstval;
11005 allocval = lstval + len + 2;
11006 curallocval = allocval;
11007
11008 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
11009 li = li->li_next)
11010 {
11011 strval = get_tv_string_buf_chk(&li->li_tv, buf);
11012 if (strval == NULL)
11013 goto free_lstval;
11014 if (strval == buf)
11015 {
11016 /* Need to make a copy, next get_tv_string_buf_chk() will
11017 * overwrite the string. */
11018 strval = vim_strsave(buf);
11019 if (strval == NULL)
11020 goto free_lstval;
11021 *curallocval++ = strval;
11022 }
11023 *curval++ = strval;
11024 }
11025 *curval++ = NULL;
11026
11027 write_reg_contents_lst(regname, lstval, -1,
11028 append, yank_type, block_len);
11029free_lstval:
11030 while (curallocval > allocval)
11031 vim_free(*--curallocval);
11032 vim_free(lstval);
11033 }
11034 else
11035 {
11036 strval = get_tv_string_chk(&argvars[1]);
11037 if (strval == NULL)
11038 return;
11039 write_reg_contents_ex(regname, strval, -1,
11040 append, yank_type, block_len);
11041 }
11042 rettv->vval.v_number = 0;
11043}
11044
11045/*
11046 * "settabvar()" function
11047 */
11048 static void
11049f_settabvar(typval_T *argvars, typval_T *rettv)
11050{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011051 tabpage_T *save_curtab;
11052 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011053 char_u *varname, *tabvarname;
11054 typval_T *varp;
11055
11056 rettv->vval.v_number = 0;
11057
11058 if (check_restricted() || check_secure())
11059 return;
11060
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011061 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011062 varname = get_tv_string_chk(&argvars[1]);
11063 varp = &argvars[2];
11064
Bram Moolenaar4033c552017-09-16 20:54:51 +020011065 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011066 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011067 save_curtab = curtab;
11068 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011069
11070 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
11071 if (tabvarname != NULL)
11072 {
11073 STRCPY(tabvarname, "t:");
11074 STRCPY(tabvarname + 2, varname);
11075 set_var(tabvarname, varp, TRUE);
11076 vim_free(tabvarname);
11077 }
11078
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011079 /* Restore current tabpage */
11080 if (valid_tabpage(save_curtab))
11081 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011082 }
11083}
11084
11085/*
11086 * "settabwinvar()" function
11087 */
11088 static void
11089f_settabwinvar(typval_T *argvars, typval_T *rettv)
11090{
11091 setwinvar(argvars, rettv, 1);
11092}
11093
11094/*
11095 * "setwinvar()" function
11096 */
11097 static void
11098f_setwinvar(typval_T *argvars, typval_T *rettv)
11099{
11100 setwinvar(argvars, rettv, 0);
11101}
11102
11103#ifdef FEAT_CRYPT
11104/*
11105 * "sha256({string})" function
11106 */
11107 static void
11108f_sha256(typval_T *argvars, typval_T *rettv)
11109{
11110 char_u *p;
11111
11112 p = get_tv_string(&argvars[0]);
11113 rettv->vval.v_string = vim_strsave(
11114 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
11115 rettv->v_type = VAR_STRING;
11116}
11117#endif /* FEAT_CRYPT */
11118
11119/*
11120 * "shellescape({string})" function
11121 */
11122 static void
11123f_shellescape(typval_T *argvars, typval_T *rettv)
11124{
Bram Moolenaar20615522017-06-05 18:46:26 +020011125 int do_special = non_zero_arg(&argvars[1]);
11126
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011127 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar20615522017-06-05 18:46:26 +020011128 get_tv_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011129 rettv->v_type = VAR_STRING;
11130}
11131
11132/*
11133 * shiftwidth() function
11134 */
11135 static void
11136f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
11137{
11138 rettv->vval.v_number = get_sw_value(curbuf);
11139}
11140
11141/*
11142 * "simplify()" function
11143 */
11144 static void
11145f_simplify(typval_T *argvars, typval_T *rettv)
11146{
11147 char_u *p;
11148
11149 p = get_tv_string(&argvars[0]);
11150 rettv->vval.v_string = vim_strsave(p);
11151 simplify_filename(rettv->vval.v_string); /* simplify in place */
11152 rettv->v_type = VAR_STRING;
11153}
11154
11155#ifdef FEAT_FLOAT
11156/*
11157 * "sin()" function
11158 */
11159 static void
11160f_sin(typval_T *argvars, typval_T *rettv)
11161{
11162 float_T f = 0.0;
11163
11164 rettv->v_type = VAR_FLOAT;
11165 if (get_float_arg(argvars, &f) == OK)
11166 rettv->vval.v_float = sin(f);
11167 else
11168 rettv->vval.v_float = 0.0;
11169}
11170
11171/*
11172 * "sinh()" function
11173 */
11174 static void
11175f_sinh(typval_T *argvars, typval_T *rettv)
11176{
11177 float_T f = 0.0;
11178
11179 rettv->v_type = VAR_FLOAT;
11180 if (get_float_arg(argvars, &f) == OK)
11181 rettv->vval.v_float = sinh(f);
11182 else
11183 rettv->vval.v_float = 0.0;
11184}
11185#endif
11186
11187static int
11188#ifdef __BORLANDC__
11189 _RTLENTRYF
11190#endif
11191 item_compare(const void *s1, const void *s2);
11192static int
11193#ifdef __BORLANDC__
11194 _RTLENTRYF
11195#endif
11196 item_compare2(const void *s1, const void *s2);
11197
11198/* struct used in the array that's given to qsort() */
11199typedef struct
11200{
11201 listitem_T *item;
11202 int idx;
11203} sortItem_T;
11204
11205/* struct storing information about current sort */
11206typedef struct
11207{
11208 int item_compare_ic;
11209 int item_compare_numeric;
11210 int item_compare_numbers;
11211#ifdef FEAT_FLOAT
11212 int item_compare_float;
11213#endif
11214 char_u *item_compare_func;
11215 partial_T *item_compare_partial;
11216 dict_T *item_compare_selfdict;
11217 int item_compare_func_err;
11218 int item_compare_keep_zero;
11219} sortinfo_T;
11220static sortinfo_T *sortinfo = NULL;
11221static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
11222#define ITEM_COMPARE_FAIL 999
11223
11224/*
11225 * Compare functions for f_sort() and f_uniq() below.
11226 */
11227 static int
11228#ifdef __BORLANDC__
11229_RTLENTRYF
11230#endif
11231item_compare(const void *s1, const void *s2)
11232{
11233 sortItem_T *si1, *si2;
11234 typval_T *tv1, *tv2;
11235 char_u *p1, *p2;
11236 char_u *tofree1 = NULL, *tofree2 = NULL;
11237 int res;
11238 char_u numbuf1[NUMBUFLEN];
11239 char_u numbuf2[NUMBUFLEN];
11240
11241 si1 = (sortItem_T *)s1;
11242 si2 = (sortItem_T *)s2;
11243 tv1 = &si1->item->li_tv;
11244 tv2 = &si2->item->li_tv;
11245
11246 if (sortinfo->item_compare_numbers)
11247 {
11248 varnumber_T v1 = get_tv_number(tv1);
11249 varnumber_T v2 = get_tv_number(tv2);
11250
11251 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11252 }
11253
11254#ifdef FEAT_FLOAT
11255 if (sortinfo->item_compare_float)
11256 {
11257 float_T v1 = get_tv_float(tv1);
11258 float_T v2 = get_tv_float(tv2);
11259
11260 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11261 }
11262#endif
11263
11264 /* tv2string() puts quotes around a string and allocates memory. Don't do
11265 * that for string variables. Use a single quote when comparing with a
11266 * non-string to do what the docs promise. */
11267 if (tv1->v_type == VAR_STRING)
11268 {
11269 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11270 p1 = (char_u *)"'";
11271 else
11272 p1 = tv1->vval.v_string;
11273 }
11274 else
11275 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
11276 if (tv2->v_type == VAR_STRING)
11277 {
11278 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11279 p2 = (char_u *)"'";
11280 else
11281 p2 = tv2->vval.v_string;
11282 }
11283 else
11284 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
11285 if (p1 == NULL)
11286 p1 = (char_u *)"";
11287 if (p2 == NULL)
11288 p2 = (char_u *)"";
11289 if (!sortinfo->item_compare_numeric)
11290 {
11291 if (sortinfo->item_compare_ic)
11292 res = STRICMP(p1, p2);
11293 else
11294 res = STRCMP(p1, p2);
11295 }
11296 else
11297 {
11298 double n1, n2;
11299 n1 = strtod((char *)p1, (char **)&p1);
11300 n2 = strtod((char *)p2, (char **)&p2);
11301 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
11302 }
11303
11304 /* When the result would be zero, compare the item indexes. Makes the
11305 * sort stable. */
11306 if (res == 0 && !sortinfo->item_compare_keep_zero)
11307 res = si1->idx > si2->idx ? 1 : -1;
11308
11309 vim_free(tofree1);
11310 vim_free(tofree2);
11311 return res;
11312}
11313
11314 static int
11315#ifdef __BORLANDC__
11316_RTLENTRYF
11317#endif
11318item_compare2(const void *s1, const void *s2)
11319{
11320 sortItem_T *si1, *si2;
11321 int res;
11322 typval_T rettv;
11323 typval_T argv[3];
11324 int dummy;
11325 char_u *func_name;
11326 partial_T *partial = sortinfo->item_compare_partial;
11327
11328 /* shortcut after failure in previous call; compare all items equal */
11329 if (sortinfo->item_compare_func_err)
11330 return 0;
11331
11332 si1 = (sortItem_T *)s1;
11333 si2 = (sortItem_T *)s2;
11334
11335 if (partial == NULL)
11336 func_name = sortinfo->item_compare_func;
11337 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020011338 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011339
11340 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
11341 * in the copy without changing the original list items. */
11342 copy_tv(&si1->item->li_tv, &argv[0]);
11343 copy_tv(&si2->item->li_tv, &argv[1]);
11344
11345 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
11346 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020011347 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011348 partial, sortinfo->item_compare_selfdict);
11349 clear_tv(&argv[0]);
11350 clear_tv(&argv[1]);
11351
11352 if (res == FAIL)
11353 res = ITEM_COMPARE_FAIL;
11354 else
11355 res = (int)get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
11356 if (sortinfo->item_compare_func_err)
11357 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
11358 clear_tv(&rettv);
11359
11360 /* When the result would be zero, compare the pointers themselves. Makes
11361 * the sort stable. */
11362 if (res == 0 && !sortinfo->item_compare_keep_zero)
11363 res = si1->idx > si2->idx ? 1 : -1;
11364
11365 return res;
11366}
11367
11368/*
11369 * "sort({list})" function
11370 */
11371 static void
11372do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
11373{
11374 list_T *l;
11375 listitem_T *li;
11376 sortItem_T *ptrs;
11377 sortinfo_T *old_sortinfo;
11378 sortinfo_T info;
11379 long len;
11380 long i;
11381
11382 /* Pointer to current info struct used in compare function. Save and
11383 * restore the current one for nested calls. */
11384 old_sortinfo = sortinfo;
11385 sortinfo = &info;
11386
11387 if (argvars[0].v_type != VAR_LIST)
11388 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
11389 else
11390 {
11391 l = argvars[0].vval.v_list;
11392 if (l == NULL || tv_check_lock(l->lv_lock,
11393 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
11394 TRUE))
11395 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011396 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011397
11398 len = list_len(l);
11399 if (len <= 1)
11400 goto theend; /* short list sorts pretty quickly */
11401
11402 info.item_compare_ic = FALSE;
11403 info.item_compare_numeric = FALSE;
11404 info.item_compare_numbers = FALSE;
11405#ifdef FEAT_FLOAT
11406 info.item_compare_float = FALSE;
11407#endif
11408 info.item_compare_func = NULL;
11409 info.item_compare_partial = NULL;
11410 info.item_compare_selfdict = NULL;
11411 if (argvars[1].v_type != VAR_UNKNOWN)
11412 {
11413 /* optional second argument: {func} */
11414 if (argvars[1].v_type == VAR_FUNC)
11415 info.item_compare_func = argvars[1].vval.v_string;
11416 else if (argvars[1].v_type == VAR_PARTIAL)
11417 info.item_compare_partial = argvars[1].vval.v_partial;
11418 else
11419 {
11420 int error = FALSE;
11421
11422 i = (long)get_tv_number_chk(&argvars[1], &error);
11423 if (error)
11424 goto theend; /* type error; errmsg already given */
11425 if (i == 1)
11426 info.item_compare_ic = TRUE;
11427 else if (argvars[1].v_type != VAR_NUMBER)
11428 info.item_compare_func = get_tv_string(&argvars[1]);
11429 else if (i != 0)
11430 {
11431 EMSG(_(e_invarg));
11432 goto theend;
11433 }
11434 if (info.item_compare_func != NULL)
11435 {
11436 if (*info.item_compare_func == NUL)
11437 {
11438 /* empty string means default sort */
11439 info.item_compare_func = NULL;
11440 }
11441 else if (STRCMP(info.item_compare_func, "n") == 0)
11442 {
11443 info.item_compare_func = NULL;
11444 info.item_compare_numeric = TRUE;
11445 }
11446 else if (STRCMP(info.item_compare_func, "N") == 0)
11447 {
11448 info.item_compare_func = NULL;
11449 info.item_compare_numbers = TRUE;
11450 }
11451#ifdef FEAT_FLOAT
11452 else if (STRCMP(info.item_compare_func, "f") == 0)
11453 {
11454 info.item_compare_func = NULL;
11455 info.item_compare_float = TRUE;
11456 }
11457#endif
11458 else if (STRCMP(info.item_compare_func, "i") == 0)
11459 {
11460 info.item_compare_func = NULL;
11461 info.item_compare_ic = TRUE;
11462 }
11463 }
11464 }
11465
11466 if (argvars[2].v_type != VAR_UNKNOWN)
11467 {
11468 /* optional third argument: {dict} */
11469 if (argvars[2].v_type != VAR_DICT)
11470 {
11471 EMSG(_(e_dictreq));
11472 goto theend;
11473 }
11474 info.item_compare_selfdict = argvars[2].vval.v_dict;
11475 }
11476 }
11477
11478 /* Make an array with each entry pointing to an item in the List. */
11479 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
11480 if (ptrs == NULL)
11481 goto theend;
11482
11483 i = 0;
11484 if (sort)
11485 {
11486 /* sort(): ptrs will be the list to sort */
11487 for (li = l->lv_first; li != NULL; li = li->li_next)
11488 {
11489 ptrs[i].item = li;
11490 ptrs[i].idx = i;
11491 ++i;
11492 }
11493
11494 info.item_compare_func_err = FALSE;
11495 info.item_compare_keep_zero = FALSE;
11496 /* test the compare function */
11497 if ((info.item_compare_func != NULL
11498 || info.item_compare_partial != NULL)
11499 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
11500 == ITEM_COMPARE_FAIL)
11501 EMSG(_("E702: Sort compare function failed"));
11502 else
11503 {
11504 /* Sort the array with item pointers. */
11505 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
11506 info.item_compare_func == NULL
11507 && info.item_compare_partial == NULL
11508 ? item_compare : item_compare2);
11509
11510 if (!info.item_compare_func_err)
11511 {
11512 /* Clear the List and append the items in sorted order. */
11513 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
11514 l->lv_len = 0;
11515 for (i = 0; i < len; ++i)
11516 list_append(l, ptrs[i].item);
11517 }
11518 }
11519 }
11520 else
11521 {
11522 int (*item_compare_func_ptr)(const void *, const void *);
11523
11524 /* f_uniq(): ptrs will be a stack of items to remove */
11525 info.item_compare_func_err = FALSE;
11526 info.item_compare_keep_zero = TRUE;
11527 item_compare_func_ptr = info.item_compare_func != NULL
11528 || info.item_compare_partial != NULL
11529 ? item_compare2 : item_compare;
11530
11531 for (li = l->lv_first; li != NULL && li->li_next != NULL;
11532 li = li->li_next)
11533 {
11534 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
11535 == 0)
11536 ptrs[i++].item = li;
11537 if (info.item_compare_func_err)
11538 {
11539 EMSG(_("E882: Uniq compare function failed"));
11540 break;
11541 }
11542 }
11543
11544 if (!info.item_compare_func_err)
11545 {
11546 while (--i >= 0)
11547 {
11548 li = ptrs[i].item->li_next;
11549 ptrs[i].item->li_next = li->li_next;
11550 if (li->li_next != NULL)
11551 li->li_next->li_prev = ptrs[i].item;
11552 else
11553 l->lv_last = ptrs[i].item;
11554 list_fix_watch(l, li);
11555 listitem_free(li);
11556 l->lv_len--;
11557 }
11558 }
11559 }
11560
11561 vim_free(ptrs);
11562 }
11563theend:
11564 sortinfo = old_sortinfo;
11565}
11566
11567/*
11568 * "sort({list})" function
11569 */
11570 static void
11571f_sort(typval_T *argvars, typval_T *rettv)
11572{
11573 do_sort_uniq(argvars, rettv, TRUE);
11574}
11575
11576/*
11577 * "uniq({list})" function
11578 */
11579 static void
11580f_uniq(typval_T *argvars, typval_T *rettv)
11581{
11582 do_sort_uniq(argvars, rettv, FALSE);
11583}
11584
11585/*
11586 * "soundfold({word})" function
11587 */
11588 static void
11589f_soundfold(typval_T *argvars, typval_T *rettv)
11590{
11591 char_u *s;
11592
11593 rettv->v_type = VAR_STRING;
11594 s = get_tv_string(&argvars[0]);
11595#ifdef FEAT_SPELL
11596 rettv->vval.v_string = eval_soundfold(s);
11597#else
11598 rettv->vval.v_string = vim_strsave(s);
11599#endif
11600}
11601
11602/*
11603 * "spellbadword()" function
11604 */
11605 static void
11606f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
11607{
11608 char_u *word = (char_u *)"";
11609 hlf_T attr = HLF_COUNT;
11610 int len = 0;
11611
11612 if (rettv_list_alloc(rettv) == FAIL)
11613 return;
11614
11615#ifdef FEAT_SPELL
11616 if (argvars[0].v_type == VAR_UNKNOWN)
11617 {
11618 /* Find the start and length of the badly spelled word. */
11619 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
11620 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011621 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011622 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011623 curwin->w_set_curswant = TRUE;
11624 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011625 }
11626 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
11627 {
11628 char_u *str = get_tv_string_chk(&argvars[0]);
11629 int capcol = -1;
11630
11631 if (str != NULL)
11632 {
11633 /* Check the argument for spelling. */
11634 while (*str != NUL)
11635 {
11636 len = spell_check(curwin, str, &attr, &capcol, FALSE);
11637 if (attr != HLF_COUNT)
11638 {
11639 word = str;
11640 break;
11641 }
11642 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020011643 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011644 }
11645 }
11646 }
11647#endif
11648
11649 list_append_string(rettv->vval.v_list, word, len);
11650 list_append_string(rettv->vval.v_list, (char_u *)(
11651 attr == HLF_SPB ? "bad" :
11652 attr == HLF_SPR ? "rare" :
11653 attr == HLF_SPL ? "local" :
11654 attr == HLF_SPC ? "caps" :
11655 ""), -1);
11656}
11657
11658/*
11659 * "spellsuggest()" function
11660 */
11661 static void
11662f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
11663{
11664#ifdef FEAT_SPELL
11665 char_u *str;
11666 int typeerr = FALSE;
11667 int maxcount;
11668 garray_T ga;
11669 int i;
11670 listitem_T *li;
11671 int need_capital = FALSE;
11672#endif
11673
11674 if (rettv_list_alloc(rettv) == FAIL)
11675 return;
11676
11677#ifdef FEAT_SPELL
11678 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
11679 {
11680 str = get_tv_string(&argvars[0]);
11681 if (argvars[1].v_type != VAR_UNKNOWN)
11682 {
11683 maxcount = (int)get_tv_number_chk(&argvars[1], &typeerr);
11684 if (maxcount <= 0)
11685 return;
11686 if (argvars[2].v_type != VAR_UNKNOWN)
11687 {
11688 need_capital = (int)get_tv_number_chk(&argvars[2], &typeerr);
11689 if (typeerr)
11690 return;
11691 }
11692 }
11693 else
11694 maxcount = 25;
11695
11696 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
11697
11698 for (i = 0; i < ga.ga_len; ++i)
11699 {
11700 str = ((char_u **)ga.ga_data)[i];
11701
11702 li = listitem_alloc();
11703 if (li == NULL)
11704 vim_free(str);
11705 else
11706 {
11707 li->li_tv.v_type = VAR_STRING;
11708 li->li_tv.v_lock = 0;
11709 li->li_tv.vval.v_string = str;
11710 list_append(rettv->vval.v_list, li);
11711 }
11712 }
11713 ga_clear(&ga);
11714 }
11715#endif
11716}
11717
11718 static void
11719f_split(typval_T *argvars, typval_T *rettv)
11720{
11721 char_u *str;
11722 char_u *end;
11723 char_u *pat = NULL;
11724 regmatch_T regmatch;
11725 char_u patbuf[NUMBUFLEN];
11726 char_u *save_cpo;
11727 int match;
11728 colnr_T col = 0;
11729 int keepempty = FALSE;
11730 int typeerr = FALSE;
11731
11732 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11733 save_cpo = p_cpo;
11734 p_cpo = (char_u *)"";
11735
11736 str = get_tv_string(&argvars[0]);
11737 if (argvars[1].v_type != VAR_UNKNOWN)
11738 {
11739 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11740 if (pat == NULL)
11741 typeerr = TRUE;
11742 if (argvars[2].v_type != VAR_UNKNOWN)
11743 keepempty = (int)get_tv_number_chk(&argvars[2], &typeerr);
11744 }
11745 if (pat == NULL || *pat == NUL)
11746 pat = (char_u *)"[\\x01- ]\\+";
11747
11748 if (rettv_list_alloc(rettv) == FAIL)
11749 return;
11750 if (typeerr)
11751 return;
11752
11753 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11754 if (regmatch.regprog != NULL)
11755 {
11756 regmatch.rm_ic = FALSE;
11757 while (*str != NUL || keepempty)
11758 {
11759 if (*str == NUL)
11760 match = FALSE; /* empty item at the end */
11761 else
11762 match = vim_regexec_nl(&regmatch, str, col);
11763 if (match)
11764 end = regmatch.startp[0];
11765 else
11766 end = str + STRLEN(str);
11767 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
11768 && *str != NUL && match && end < regmatch.endp[0]))
11769 {
11770 if (list_append_string(rettv->vval.v_list, str,
11771 (int)(end - str)) == FAIL)
11772 break;
11773 }
11774 if (!match)
11775 break;
11776 /* Advance to just after the match. */
11777 if (regmatch.endp[0] > str)
11778 col = 0;
11779 else
11780 {
11781 /* Don't get stuck at the same match. */
11782#ifdef FEAT_MBYTE
11783 col = (*mb_ptr2len)(regmatch.endp[0]);
11784#else
11785 col = 1;
11786#endif
11787 }
11788 str = regmatch.endp[0];
11789 }
11790
11791 vim_regfree(regmatch.regprog);
11792 }
11793
11794 p_cpo = save_cpo;
11795}
11796
11797#ifdef FEAT_FLOAT
11798/*
11799 * "sqrt()" function
11800 */
11801 static void
11802f_sqrt(typval_T *argvars, typval_T *rettv)
11803{
11804 float_T f = 0.0;
11805
11806 rettv->v_type = VAR_FLOAT;
11807 if (get_float_arg(argvars, &f) == OK)
11808 rettv->vval.v_float = sqrt(f);
11809 else
11810 rettv->vval.v_float = 0.0;
11811}
11812
11813/*
11814 * "str2float()" function
11815 */
11816 static void
11817f_str2float(typval_T *argvars, typval_T *rettv)
11818{
11819 char_u *p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011820 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011821
Bram Moolenaar08243d22017-01-10 16:12:29 +010011822 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011823 p = skipwhite(p + 1);
11824 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011825 if (isneg)
11826 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011827 rettv->v_type = VAR_FLOAT;
11828}
11829#endif
11830
11831/*
11832 * "str2nr()" function
11833 */
11834 static void
11835f_str2nr(typval_T *argvars, typval_T *rettv)
11836{
11837 int base = 10;
11838 char_u *p;
11839 varnumber_T n;
11840 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010011841 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011842
11843 if (argvars[1].v_type != VAR_UNKNOWN)
11844 {
11845 base = (int)get_tv_number(&argvars[1]);
11846 if (base != 2 && base != 8 && base != 10 && base != 16)
11847 {
11848 EMSG(_(e_invarg));
11849 return;
11850 }
11851 }
11852
11853 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011854 isneg = (*p == '-');
11855 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011856 p = skipwhite(p + 1);
11857 switch (base)
11858 {
11859 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
11860 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
11861 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
11862 default: what = 0;
11863 }
11864 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011865 if (isneg)
11866 rettv->vval.v_number = -n;
11867 else
11868 rettv->vval.v_number = n;
11869
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011870}
11871
11872#ifdef HAVE_STRFTIME
11873/*
11874 * "strftime({format}[, {time}])" function
11875 */
11876 static void
11877f_strftime(typval_T *argvars, typval_T *rettv)
11878{
11879 char_u result_buf[256];
11880 struct tm *curtime;
11881 time_t seconds;
11882 char_u *p;
11883
11884 rettv->v_type = VAR_STRING;
11885
11886 p = get_tv_string(&argvars[0]);
11887 if (argvars[1].v_type == VAR_UNKNOWN)
11888 seconds = time(NULL);
11889 else
11890 seconds = (time_t)get_tv_number(&argvars[1]);
11891 curtime = localtime(&seconds);
11892 /* MSVC returns NULL for an invalid value of seconds. */
11893 if (curtime == NULL)
11894 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
11895 else
11896 {
11897# ifdef FEAT_MBYTE
11898 vimconv_T conv;
11899 char_u *enc;
11900
11901 conv.vc_type = CONV_NONE;
11902 enc = enc_locale();
11903 convert_setup(&conv, p_enc, enc);
11904 if (conv.vc_type != CONV_NONE)
11905 p = string_convert(&conv, p, NULL);
11906# endif
11907 if (p != NULL)
11908 (void)strftime((char *)result_buf, sizeof(result_buf),
11909 (char *)p, curtime);
11910 else
11911 result_buf[0] = NUL;
11912
11913# ifdef FEAT_MBYTE
11914 if (conv.vc_type != CONV_NONE)
11915 vim_free(p);
11916 convert_setup(&conv, enc, p_enc);
11917 if (conv.vc_type != CONV_NONE)
11918 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
11919 else
11920# endif
11921 rettv->vval.v_string = vim_strsave(result_buf);
11922
11923# ifdef FEAT_MBYTE
11924 /* Release conversion descriptors */
11925 convert_setup(&conv, NULL, NULL);
11926 vim_free(enc);
11927# endif
11928 }
11929}
11930#endif
11931
11932/*
11933 * "strgetchar()" function
11934 */
11935 static void
11936f_strgetchar(typval_T *argvars, typval_T *rettv)
11937{
11938 char_u *str;
11939 int len;
11940 int error = FALSE;
11941 int charidx;
11942
11943 rettv->vval.v_number = -1;
11944 str = get_tv_string_chk(&argvars[0]);
11945 if (str == NULL)
11946 return;
11947 len = (int)STRLEN(str);
11948 charidx = (int)get_tv_number_chk(&argvars[1], &error);
11949 if (error)
11950 return;
11951#ifdef FEAT_MBYTE
11952 {
11953 int byteidx = 0;
11954
11955 while (charidx >= 0 && byteidx < len)
11956 {
11957 if (charidx == 0)
11958 {
11959 rettv->vval.v_number = mb_ptr2char(str + byteidx);
11960 break;
11961 }
11962 --charidx;
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011963 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011964 }
11965 }
11966#else
11967 if (charidx < len)
11968 rettv->vval.v_number = str[charidx];
11969#endif
11970}
11971
11972/*
11973 * "stridx()" function
11974 */
11975 static void
11976f_stridx(typval_T *argvars, typval_T *rettv)
11977{
11978 char_u buf[NUMBUFLEN];
11979 char_u *needle;
11980 char_u *haystack;
11981 char_u *save_haystack;
11982 char_u *pos;
11983 int start_idx;
11984
11985 needle = get_tv_string_chk(&argvars[1]);
11986 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
11987 rettv->vval.v_number = -1;
11988 if (needle == NULL || haystack == NULL)
11989 return; /* type error; errmsg already given */
11990
11991 if (argvars[2].v_type != VAR_UNKNOWN)
11992 {
11993 int error = FALSE;
11994
11995 start_idx = (int)get_tv_number_chk(&argvars[2], &error);
11996 if (error || start_idx >= (int)STRLEN(haystack))
11997 return;
11998 if (start_idx >= 0)
11999 haystack += start_idx;
12000 }
12001
12002 pos = (char_u *)strstr((char *)haystack, (char *)needle);
12003 if (pos != NULL)
12004 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
12005}
12006
12007/*
12008 * "string()" function
12009 */
12010 static void
12011f_string(typval_T *argvars, typval_T *rettv)
12012{
12013 char_u *tofree;
12014 char_u numbuf[NUMBUFLEN];
12015
12016 rettv->v_type = VAR_STRING;
12017 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
12018 get_copyID());
12019 /* Make a copy if we have a value but it's not in allocated memory. */
12020 if (rettv->vval.v_string != NULL && tofree == NULL)
12021 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
12022}
12023
12024/*
12025 * "strlen()" function
12026 */
12027 static void
12028f_strlen(typval_T *argvars, typval_T *rettv)
12029{
12030 rettv->vval.v_number = (varnumber_T)(STRLEN(
12031 get_tv_string(&argvars[0])));
12032}
12033
12034/*
12035 * "strchars()" function
12036 */
12037 static void
12038f_strchars(typval_T *argvars, typval_T *rettv)
12039{
12040 char_u *s = get_tv_string(&argvars[0]);
12041 int skipcc = 0;
12042#ifdef FEAT_MBYTE
12043 varnumber_T len = 0;
12044 int (*func_mb_ptr2char_adv)(char_u **pp);
12045#endif
12046
12047 if (argvars[1].v_type != VAR_UNKNOWN)
12048 skipcc = (int)get_tv_number_chk(&argvars[1], NULL);
12049 if (skipcc < 0 || skipcc > 1)
12050 EMSG(_(e_invarg));
12051 else
12052 {
12053#ifdef FEAT_MBYTE
12054 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
12055 while (*s != NUL)
12056 {
12057 func_mb_ptr2char_adv(&s);
12058 ++len;
12059 }
12060 rettv->vval.v_number = len;
12061#else
12062 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
12063#endif
12064 }
12065}
12066
12067/*
12068 * "strdisplaywidth()" function
12069 */
12070 static void
12071f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
12072{
12073 char_u *s = get_tv_string(&argvars[0]);
12074 int col = 0;
12075
12076 if (argvars[1].v_type != VAR_UNKNOWN)
12077 col = (int)get_tv_number(&argvars[1]);
12078
12079 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
12080}
12081
12082/*
12083 * "strwidth()" function
12084 */
12085 static void
12086f_strwidth(typval_T *argvars, typval_T *rettv)
12087{
12088 char_u *s = get_tv_string(&argvars[0]);
12089
12090 rettv->vval.v_number = (varnumber_T)(
12091#ifdef FEAT_MBYTE
12092 mb_string2cells(s, -1)
12093#else
12094 STRLEN(s)
12095#endif
12096 );
12097}
12098
12099/*
12100 * "strcharpart()" function
12101 */
12102 static void
12103f_strcharpart(typval_T *argvars, typval_T *rettv)
12104{
12105#ifdef FEAT_MBYTE
12106 char_u *p;
12107 int nchar;
12108 int nbyte = 0;
12109 int charlen;
12110 int len = 0;
12111 int slen;
12112 int error = FALSE;
12113
12114 p = get_tv_string(&argvars[0]);
12115 slen = (int)STRLEN(p);
12116
12117 nchar = (int)get_tv_number_chk(&argvars[1], &error);
12118 if (!error)
12119 {
12120 if (nchar > 0)
12121 while (nchar > 0 && nbyte < slen)
12122 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012123 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012124 --nchar;
12125 }
12126 else
12127 nbyte = nchar;
12128 if (argvars[2].v_type != VAR_UNKNOWN)
12129 {
12130 charlen = (int)get_tv_number(&argvars[2]);
12131 while (charlen > 0 && nbyte + len < slen)
12132 {
12133 int off = nbyte + len;
12134
12135 if (off < 0)
12136 len += 1;
12137 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012138 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012139 --charlen;
12140 }
12141 }
12142 else
12143 len = slen - nbyte; /* default: all bytes that are available. */
12144 }
12145
12146 /*
12147 * Only return the overlap between the specified part and the actual
12148 * string.
12149 */
12150 if (nbyte < 0)
12151 {
12152 len += nbyte;
12153 nbyte = 0;
12154 }
12155 else if (nbyte > slen)
12156 nbyte = slen;
12157 if (len < 0)
12158 len = 0;
12159 else if (nbyte + len > slen)
12160 len = slen - nbyte;
12161
12162 rettv->v_type = VAR_STRING;
12163 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
12164#else
12165 f_strpart(argvars, rettv);
12166#endif
12167}
12168
12169/*
12170 * "strpart()" function
12171 */
12172 static void
12173f_strpart(typval_T *argvars, typval_T *rettv)
12174{
12175 char_u *p;
12176 int n;
12177 int len;
12178 int slen;
12179 int error = FALSE;
12180
12181 p = get_tv_string(&argvars[0]);
12182 slen = (int)STRLEN(p);
12183
12184 n = (int)get_tv_number_chk(&argvars[1], &error);
12185 if (error)
12186 len = 0;
12187 else if (argvars[2].v_type != VAR_UNKNOWN)
12188 len = (int)get_tv_number(&argvars[2]);
12189 else
12190 len = slen - n; /* default len: all bytes that are available. */
12191
12192 /*
12193 * Only return the overlap between the specified part and the actual
12194 * string.
12195 */
12196 if (n < 0)
12197 {
12198 len += n;
12199 n = 0;
12200 }
12201 else if (n > slen)
12202 n = slen;
12203 if (len < 0)
12204 len = 0;
12205 else if (n + len > slen)
12206 len = slen - n;
12207
12208 rettv->v_type = VAR_STRING;
12209 rettv->vval.v_string = vim_strnsave(p + n, len);
12210}
12211
12212/*
12213 * "strridx()" function
12214 */
12215 static void
12216f_strridx(typval_T *argvars, typval_T *rettv)
12217{
12218 char_u buf[NUMBUFLEN];
12219 char_u *needle;
12220 char_u *haystack;
12221 char_u *rest;
12222 char_u *lastmatch = NULL;
12223 int haystack_len, end_idx;
12224
12225 needle = get_tv_string_chk(&argvars[1]);
12226 haystack = get_tv_string_buf_chk(&argvars[0], buf);
12227
12228 rettv->vval.v_number = -1;
12229 if (needle == NULL || haystack == NULL)
12230 return; /* type error; errmsg already given */
12231
12232 haystack_len = (int)STRLEN(haystack);
12233 if (argvars[2].v_type != VAR_UNKNOWN)
12234 {
12235 /* Third argument: upper limit for index */
12236 end_idx = (int)get_tv_number_chk(&argvars[2], NULL);
12237 if (end_idx < 0)
12238 return; /* can never find a match */
12239 }
12240 else
12241 end_idx = haystack_len;
12242
12243 if (*needle == NUL)
12244 {
12245 /* Empty string matches past the end. */
12246 lastmatch = haystack + end_idx;
12247 }
12248 else
12249 {
12250 for (rest = haystack; *rest != '\0'; ++rest)
12251 {
12252 rest = (char_u *)strstr((char *)rest, (char *)needle);
12253 if (rest == NULL || rest > haystack + end_idx)
12254 break;
12255 lastmatch = rest;
12256 }
12257 }
12258
12259 if (lastmatch == NULL)
12260 rettv->vval.v_number = -1;
12261 else
12262 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
12263}
12264
12265/*
12266 * "strtrans()" function
12267 */
12268 static void
12269f_strtrans(typval_T *argvars, typval_T *rettv)
12270{
12271 rettv->v_type = VAR_STRING;
12272 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
12273}
12274
12275/*
12276 * "submatch()" function
12277 */
12278 static void
12279f_submatch(typval_T *argvars, typval_T *rettv)
12280{
12281 int error = FALSE;
12282 int no;
12283 int retList = 0;
12284
12285 no = (int)get_tv_number_chk(&argvars[0], &error);
12286 if (error)
12287 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012288 if (no < 0 || no >= NSUBEXP)
12289 {
Bram Moolenaar79518e22017-02-17 16:31:35 +010012290 EMSGN(_("E935: invalid submatch number: %d"), no);
12291 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012292 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012293 if (argvars[1].v_type != VAR_UNKNOWN)
12294 retList = (int)get_tv_number_chk(&argvars[1], &error);
12295 if (error)
12296 return;
12297
12298 if (retList == 0)
12299 {
12300 rettv->v_type = VAR_STRING;
12301 rettv->vval.v_string = reg_submatch(no);
12302 }
12303 else
12304 {
12305 rettv->v_type = VAR_LIST;
12306 rettv->vval.v_list = reg_submatch_list(no);
12307 }
12308}
12309
12310/*
12311 * "substitute()" function
12312 */
12313 static void
12314f_substitute(typval_T *argvars, typval_T *rettv)
12315{
12316 char_u patbuf[NUMBUFLEN];
12317 char_u subbuf[NUMBUFLEN];
12318 char_u flagsbuf[NUMBUFLEN];
12319
12320 char_u *str = get_tv_string_chk(&argvars[0]);
12321 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012322 char_u *sub = NULL;
12323 typval_T *expr = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012324 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
12325
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012326 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
12327 expr = &argvars[2];
12328 else
12329 sub = get_tv_string_buf_chk(&argvars[2], subbuf);
12330
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012331 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012332 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
12333 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012334 rettv->vval.v_string = NULL;
12335 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012336 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012337}
12338
12339/*
12340 * "synID(lnum, col, trans)" function
12341 */
12342 static void
12343f_synID(typval_T *argvars UNUSED, typval_T *rettv)
12344{
12345 int id = 0;
12346#ifdef FEAT_SYN_HL
12347 linenr_T lnum;
12348 colnr_T col;
12349 int trans;
12350 int transerr = FALSE;
12351
12352 lnum = get_tv_lnum(argvars); /* -1 on type error */
12353 col = (linenr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12354 trans = (int)get_tv_number_chk(&argvars[2], &transerr);
12355
12356 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12357 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
12358 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
12359#endif
12360
12361 rettv->vval.v_number = id;
12362}
12363
12364/*
12365 * "synIDattr(id, what [, mode])" function
12366 */
12367 static void
12368f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
12369{
12370 char_u *p = NULL;
12371#ifdef FEAT_SYN_HL
12372 int id;
12373 char_u *what;
12374 char_u *mode;
12375 char_u modebuf[NUMBUFLEN];
12376 int modec;
12377
12378 id = (int)get_tv_number(&argvars[0]);
12379 what = get_tv_string(&argvars[1]);
12380 if (argvars[2].v_type != VAR_UNKNOWN)
12381 {
12382 mode = get_tv_string_buf(&argvars[2], modebuf);
12383 modec = TOLOWER_ASC(mode[0]);
12384 if (modec != 't' && modec != 'c' && modec != 'g')
12385 modec = 0; /* replace invalid with current */
12386 }
12387 else
12388 {
12389#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
12390 if (USE_24BIT)
12391 modec = 'g';
12392 else
12393#endif
12394 if (t_colors > 1)
12395 modec = 'c';
12396 else
12397 modec = 't';
12398 }
12399
12400
12401 switch (TOLOWER_ASC(what[0]))
12402 {
12403 case 'b':
12404 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
12405 p = highlight_color(id, what, modec);
12406 else /* bold */
12407 p = highlight_has_attr(id, HL_BOLD, modec);
12408 break;
12409
12410 case 'f': /* fg[#] or font */
12411 p = highlight_color(id, what, modec);
12412 break;
12413
12414 case 'i':
12415 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
12416 p = highlight_has_attr(id, HL_INVERSE, modec);
12417 else /* italic */
12418 p = highlight_has_attr(id, HL_ITALIC, modec);
12419 break;
12420
12421 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020012422 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012423 break;
12424
12425 case 'r': /* reverse */
12426 p = highlight_has_attr(id, HL_INVERSE, modec);
12427 break;
12428
12429 case 's':
12430 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
12431 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020012432 /* strikeout */
12433 else if (TOLOWER_ASC(what[1]) == 't' &&
12434 TOLOWER_ASC(what[2]) == 'r')
12435 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012436 else /* standout */
12437 p = highlight_has_attr(id, HL_STANDOUT, modec);
12438 break;
12439
12440 case 'u':
12441 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
12442 /* underline */
12443 p = highlight_has_attr(id, HL_UNDERLINE, modec);
12444 else
12445 /* undercurl */
12446 p = highlight_has_attr(id, HL_UNDERCURL, modec);
12447 break;
12448 }
12449
12450 if (p != NULL)
12451 p = vim_strsave(p);
12452#endif
12453 rettv->v_type = VAR_STRING;
12454 rettv->vval.v_string = p;
12455}
12456
12457/*
12458 * "synIDtrans(id)" function
12459 */
12460 static void
12461f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
12462{
12463 int id;
12464
12465#ifdef FEAT_SYN_HL
12466 id = (int)get_tv_number(&argvars[0]);
12467
12468 if (id > 0)
12469 id = syn_get_final_id(id);
12470 else
12471#endif
12472 id = 0;
12473
12474 rettv->vval.v_number = id;
12475}
12476
12477/*
12478 * "synconcealed(lnum, col)" function
12479 */
12480 static void
12481f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
12482{
12483#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12484 linenr_T lnum;
12485 colnr_T col;
12486 int syntax_flags = 0;
12487 int cchar;
12488 int matchid = 0;
12489 char_u str[NUMBUFLEN];
12490#endif
12491
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012492 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012493
12494#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12495 lnum = get_tv_lnum(argvars); /* -1 on type error */
12496 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12497
12498 vim_memset(str, NUL, sizeof(str));
12499
12500 if (rettv_list_alloc(rettv) != FAIL)
12501 {
12502 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12503 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12504 && curwin->w_p_cole > 0)
12505 {
12506 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
12507 syntax_flags = get_syntax_info(&matchid);
12508
12509 /* get the conceal character */
12510 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
12511 {
12512 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020012513 if (cchar == NUL && curwin->w_p_cole == 1)
12514 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012515 if (cchar != NUL)
12516 {
12517# ifdef FEAT_MBYTE
12518 if (has_mbyte)
12519 (*mb_char2bytes)(cchar, str);
12520 else
12521# endif
12522 str[0] = cchar;
12523 }
12524 }
12525 }
12526
12527 list_append_number(rettv->vval.v_list,
12528 (syntax_flags & HL_CONCEAL) != 0);
12529 /* -1 to auto-determine strlen */
12530 list_append_string(rettv->vval.v_list, str, -1);
12531 list_append_number(rettv->vval.v_list, matchid);
12532 }
12533#endif
12534}
12535
12536/*
12537 * "synstack(lnum, col)" function
12538 */
12539 static void
12540f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
12541{
12542#ifdef FEAT_SYN_HL
12543 linenr_T lnum;
12544 colnr_T col;
12545 int i;
12546 int id;
12547#endif
12548
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012549 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012550
12551#ifdef FEAT_SYN_HL
12552 lnum = get_tv_lnum(argvars); /* -1 on type error */
12553 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12554
12555 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12556 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12557 && rettv_list_alloc(rettv) != FAIL)
12558 {
12559 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
12560 for (i = 0; ; ++i)
12561 {
12562 id = syn_get_stack_item(i);
12563 if (id < 0)
12564 break;
12565 if (list_append_number(rettv->vval.v_list, id) == FAIL)
12566 break;
12567 }
12568 }
12569#endif
12570}
12571
12572 static void
12573get_cmd_output_as_rettv(
12574 typval_T *argvars,
12575 typval_T *rettv,
12576 int retlist)
12577{
12578 char_u *res = NULL;
12579 char_u *p;
12580 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012581 int err = FALSE;
12582 FILE *fd;
12583 list_T *list = NULL;
12584 int flags = SHELL_SILENT;
12585
12586 rettv->v_type = VAR_STRING;
12587 rettv->vval.v_string = NULL;
12588 if (check_restricted() || check_secure())
12589 goto errret;
12590
12591 if (argvars[1].v_type != VAR_UNKNOWN)
12592 {
12593 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010012594 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012595 * command.
12596 */
12597 if ((infile = vim_tempname('i', TRUE)) == NULL)
12598 {
12599 EMSG(_(e_notmp));
12600 goto errret;
12601 }
12602
12603 fd = mch_fopen((char *)infile, WRITEBIN);
12604 if (fd == NULL)
12605 {
12606 EMSG2(_(e_notopen), infile);
12607 goto errret;
12608 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010012609 if (argvars[1].v_type == VAR_NUMBER)
12610 {
12611 linenr_T lnum;
12612 buf_T *buf;
12613
12614 buf = buflist_findnr(argvars[1].vval.v_number);
12615 if (buf == NULL)
12616 {
12617 EMSGN(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010012618 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010012619 goto errret;
12620 }
12621
12622 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
12623 {
12624 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
12625 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
12626 {
12627 err = TRUE;
12628 break;
12629 }
12630 if (putc(NL, fd) == EOF)
12631 {
12632 err = TRUE;
12633 break;
12634 }
12635 }
12636 }
12637 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012638 {
12639 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
12640 err = TRUE;
12641 }
12642 else
12643 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010012644 size_t len;
12645 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012646
12647 p = get_tv_string_buf_chk(&argvars[1], buf);
12648 if (p == NULL)
12649 {
12650 fclose(fd);
12651 goto errret; /* type error; errmsg already given */
12652 }
12653 len = STRLEN(p);
12654 if (len > 0 && fwrite(p, len, 1, fd) != 1)
12655 err = TRUE;
12656 }
12657 if (fclose(fd) != 0)
12658 err = TRUE;
12659 if (err)
12660 {
12661 EMSG(_("E677: Error writing temp file"));
12662 goto errret;
12663 }
12664 }
12665
12666 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
12667 * echoes typeahead, that messes up the display. */
12668 if (!msg_silent)
12669 flags += SHELL_COOKED;
12670
12671 if (retlist)
12672 {
12673 int len;
12674 listitem_T *li;
12675 char_u *s = NULL;
12676 char_u *start;
12677 char_u *end;
12678 int i;
12679
12680 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
12681 if (res == NULL)
12682 goto errret;
12683
12684 list = list_alloc();
12685 if (list == NULL)
12686 goto errret;
12687
12688 for (i = 0; i < len; ++i)
12689 {
12690 start = res + i;
12691 while (i < len && res[i] != NL)
12692 ++i;
12693 end = res + i;
12694
12695 s = alloc((unsigned)(end - start + 1));
12696 if (s == NULL)
12697 goto errret;
12698
12699 for (p = s; start < end; ++p, ++start)
12700 *p = *start == NUL ? NL : *start;
12701 *p = NUL;
12702
12703 li = listitem_alloc();
12704 if (li == NULL)
12705 {
12706 vim_free(s);
12707 goto errret;
12708 }
12709 li->li_tv.v_type = VAR_STRING;
12710 li->li_tv.v_lock = 0;
12711 li->li_tv.vval.v_string = s;
12712 list_append(list, li);
12713 }
12714
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012715 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012716 list = NULL;
12717 }
12718 else
12719 {
12720 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
12721#ifdef USE_CR
12722 /* translate <CR> into <NL> */
12723 if (res != NULL)
12724 {
12725 char_u *s;
12726
12727 for (s = res; *s; ++s)
12728 {
12729 if (*s == CAR)
12730 *s = NL;
12731 }
12732 }
12733#else
12734# ifdef USE_CRNL
12735 /* translate <CR><NL> into <NL> */
12736 if (res != NULL)
12737 {
12738 char_u *s, *d;
12739
12740 d = res;
12741 for (s = res; *s; ++s)
12742 {
12743 if (s[0] == CAR && s[1] == NL)
12744 ++s;
12745 *d++ = *s;
12746 }
12747 *d = NUL;
12748 }
12749# endif
12750#endif
12751 rettv->vval.v_string = res;
12752 res = NULL;
12753 }
12754
12755errret:
12756 if (infile != NULL)
12757 {
12758 mch_remove(infile);
12759 vim_free(infile);
12760 }
12761 if (res != NULL)
12762 vim_free(res);
12763 if (list != NULL)
12764 list_free(list);
12765}
12766
12767/*
12768 * "system()" function
12769 */
12770 static void
12771f_system(typval_T *argvars, typval_T *rettv)
12772{
12773 get_cmd_output_as_rettv(argvars, rettv, FALSE);
12774}
12775
12776/*
12777 * "systemlist()" function
12778 */
12779 static void
12780f_systemlist(typval_T *argvars, typval_T *rettv)
12781{
12782 get_cmd_output_as_rettv(argvars, rettv, TRUE);
12783}
12784
12785/*
12786 * "tabpagebuflist()" function
12787 */
12788 static void
12789f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12790{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012791 tabpage_T *tp;
12792 win_T *wp = NULL;
12793
12794 if (argvars[0].v_type == VAR_UNKNOWN)
12795 wp = firstwin;
12796 else
12797 {
12798 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12799 if (tp != NULL)
12800 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12801 }
12802 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
12803 {
12804 for (; wp != NULL; wp = wp->w_next)
12805 if (list_append_number(rettv->vval.v_list,
12806 wp->w_buffer->b_fnum) == FAIL)
12807 break;
12808 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012809}
12810
12811
12812/*
12813 * "tabpagenr()" function
12814 */
12815 static void
12816f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
12817{
12818 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012819 char_u *arg;
12820
12821 if (argvars[0].v_type != VAR_UNKNOWN)
12822 {
12823 arg = get_tv_string_chk(&argvars[0]);
12824 nr = 0;
12825 if (arg != NULL)
12826 {
12827 if (STRCMP(arg, "$") == 0)
12828 nr = tabpage_index(NULL) - 1;
12829 else
12830 EMSG2(_(e_invexpr2), arg);
12831 }
12832 }
12833 else
12834 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012835 rettv->vval.v_number = nr;
12836}
12837
12838
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012839static int get_winnr(tabpage_T *tp, typval_T *argvar);
12840
12841/*
12842 * Common code for tabpagewinnr() and winnr().
12843 */
12844 static int
12845get_winnr(tabpage_T *tp, typval_T *argvar)
12846{
12847 win_T *twin;
12848 int nr = 1;
12849 win_T *wp;
12850 char_u *arg;
12851
12852 twin = (tp == curtab) ? curwin : tp->tp_curwin;
12853 if (argvar->v_type != VAR_UNKNOWN)
12854 {
12855 arg = get_tv_string_chk(argvar);
12856 if (arg == NULL)
12857 nr = 0; /* type error; errmsg already given */
12858 else if (STRCMP(arg, "$") == 0)
12859 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
12860 else if (STRCMP(arg, "#") == 0)
12861 {
12862 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
12863 if (twin == NULL)
12864 nr = 0;
12865 }
12866 else
12867 {
12868 EMSG2(_(e_invexpr2), arg);
12869 nr = 0;
12870 }
12871 }
12872
12873 if (nr > 0)
12874 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12875 wp != twin; wp = wp->w_next)
12876 {
12877 if (wp == NULL)
12878 {
12879 /* didn't find it in this tabpage */
12880 nr = 0;
12881 break;
12882 }
12883 ++nr;
12884 }
12885 return nr;
12886}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012887
12888/*
12889 * "tabpagewinnr()" function
12890 */
12891 static void
12892f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
12893{
12894 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012895 tabpage_T *tp;
12896
12897 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12898 if (tp == NULL)
12899 nr = 0;
12900 else
12901 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012902 rettv->vval.v_number = nr;
12903}
12904
12905
12906/*
12907 * "tagfiles()" function
12908 */
12909 static void
12910f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
12911{
12912 char_u *fname;
12913 tagname_T tn;
12914 int first;
12915
12916 if (rettv_list_alloc(rettv) == FAIL)
12917 return;
12918 fname = alloc(MAXPATHL);
12919 if (fname == NULL)
12920 return;
12921
12922 for (first = TRUE; ; first = FALSE)
12923 if (get_tagfname(&tn, first, fname) == FAIL
12924 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
12925 break;
12926 tagname_free(&tn);
12927 vim_free(fname);
12928}
12929
12930/*
12931 * "taglist()" function
12932 */
12933 static void
12934f_taglist(typval_T *argvars, typval_T *rettv)
12935{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012936 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012937 char_u *tag_pattern;
12938
12939 tag_pattern = get_tv_string(&argvars[0]);
12940
12941 rettv->vval.v_number = FALSE;
12942 if (*tag_pattern == NUL)
12943 return;
12944
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012945 if (argvars[1].v_type != VAR_UNKNOWN)
12946 fname = get_tv_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012947 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012948 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012949}
12950
12951/*
12952 * "tempname()" function
12953 */
12954 static void
12955f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
12956{
12957 static int x = 'A';
12958
12959 rettv->v_type = VAR_STRING;
12960 rettv->vval.v_string = vim_tempname(x, FALSE);
12961
12962 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
12963 * names. Skip 'I' and 'O', they are used for shell redirection. */
12964 do
12965 {
12966 if (x == 'Z')
12967 x = '0';
12968 else if (x == '9')
12969 x = 'A';
12970 else
12971 {
12972#ifdef EBCDIC
12973 if (x == 'I')
12974 x = 'J';
12975 else if (x == 'R')
12976 x = 'S';
12977 else
12978#endif
12979 ++x;
12980 }
12981 } while (x == 'I' || x == 'O');
12982}
12983
12984#ifdef FEAT_FLOAT
12985/*
12986 * "tan()" function
12987 */
12988 static void
12989f_tan(typval_T *argvars, typval_T *rettv)
12990{
12991 float_T f = 0.0;
12992
12993 rettv->v_type = VAR_FLOAT;
12994 if (get_float_arg(argvars, &f) == OK)
12995 rettv->vval.v_float = tan(f);
12996 else
12997 rettv->vval.v_float = 0.0;
12998}
12999
13000/*
13001 * "tanh()" function
13002 */
13003 static void
13004f_tanh(typval_T *argvars, typval_T *rettv)
13005{
13006 float_T f = 0.0;
13007
13008 rettv->v_type = VAR_FLOAT;
13009 if (get_float_arg(argvars, &f) == OK)
13010 rettv->vval.v_float = tanh(f);
13011 else
13012 rettv->vval.v_float = 0.0;
13013}
13014#endif
13015
13016/*
13017 * "test_alloc_fail(id, countdown, repeat)" function
13018 */
13019 static void
13020f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
13021{
13022 if (argvars[0].v_type != VAR_NUMBER
13023 || argvars[0].vval.v_number <= 0
13024 || argvars[1].v_type != VAR_NUMBER
13025 || argvars[1].vval.v_number < 0
13026 || argvars[2].v_type != VAR_NUMBER)
13027 EMSG(_(e_invarg));
13028 else
13029 {
13030 alloc_fail_id = argvars[0].vval.v_number;
13031 if (alloc_fail_id >= aid_last)
13032 EMSG(_(e_invarg));
13033 alloc_fail_countdown = argvars[1].vval.v_number;
13034 alloc_fail_repeat = argvars[2].vval.v_number;
13035 did_outofmem_msg = FALSE;
13036 }
13037}
13038
13039/*
13040 * "test_autochdir()"
13041 */
13042 static void
13043f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13044{
13045#if defined(FEAT_AUTOCHDIR)
13046 test_autochdir = TRUE;
13047#endif
13048}
13049
13050/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020013051 * "test_feedinput()"
13052 */
13053 static void
13054f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
13055{
13056#ifdef USE_INPUT_BUF
13057 char_u *val = get_tv_string_chk(&argvars[0]);
13058
13059 if (val != NULL)
13060 {
13061 trash_input_buf();
13062 add_to_input_buf_csi(val, (int)STRLEN(val));
13063 }
13064#endif
13065}
13066
13067/*
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013068 * "test_disable({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013069 */
13070 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013071f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013072{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013073 char_u *name = (char_u *)"";
13074 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013075 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013076
13077 if (argvars[0].v_type != VAR_STRING
13078 || (argvars[1].v_type) != VAR_NUMBER)
13079 EMSG(_(e_invarg));
13080 else
13081 {
13082 name = get_tv_string_chk(&argvars[0]);
13083 val = (int)get_tv_number(&argvars[1]);
13084
13085 if (STRCMP(name, (char_u *)"redraw") == 0)
13086 disable_redraw_for_testing = val;
13087 else if (STRCMP(name, (char_u *)"char_avail") == 0)
13088 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013089 else if (STRCMP(name, (char_u *)"starting") == 0)
13090 {
13091 if (val)
13092 {
13093 if (save_starting < 0)
13094 save_starting = starting;
13095 starting = 0;
13096 }
13097 else
13098 {
13099 starting = save_starting;
13100 save_starting = -1;
13101 }
13102 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013103 else if (STRCMP(name, (char_u *)"nfa_fail") == 0)
13104 nfa_fail_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013105 else if (STRCMP(name, (char_u *)"ALL") == 0)
13106 {
13107 disable_char_avail_for_testing = FALSE;
13108 disable_redraw_for_testing = FALSE;
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013109 nfa_fail_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013110 if (save_starting >= 0)
13111 {
13112 starting = save_starting;
13113 save_starting = -1;
13114 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013115 }
13116 else
13117 EMSG2(_(e_invarg2), name);
13118 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013119}
13120
13121/*
13122 * "test_garbagecollect_now()" function
13123 */
13124 static void
13125f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13126{
13127 /* This is dangerous, any Lists and Dicts used internally may be freed
13128 * while still in use. */
13129 garbage_collect(TRUE);
13130}
13131
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013132/*
13133 * "test_ignore_error()" function
13134 */
13135 static void
13136f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
13137{
13138 ignore_error_for_testing(get_tv_string(&argvars[0]));
13139}
13140
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013141#ifdef FEAT_JOB_CHANNEL
13142 static void
13143f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
13144{
13145 rettv->v_type = VAR_CHANNEL;
13146 rettv->vval.v_channel = NULL;
13147}
13148#endif
13149
13150 static void
13151f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
13152{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013153 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013154}
13155
13156#ifdef FEAT_JOB_CHANNEL
13157 static void
13158f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
13159{
13160 rettv->v_type = VAR_JOB;
13161 rettv->vval.v_job = NULL;
13162}
13163#endif
13164
13165 static void
13166f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
13167{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013168 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013169}
13170
13171 static void
13172f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
13173{
13174 rettv->v_type = VAR_PARTIAL;
13175 rettv->vval.v_partial = NULL;
13176}
13177
13178 static void
13179f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
13180{
13181 rettv->v_type = VAR_STRING;
13182 rettv->vval.v_string = NULL;
13183}
13184
13185 static void
13186f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
13187{
13188 time_for_testing = (time_t)get_tv_number(&argvars[0]);
13189}
13190
13191#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
13192/*
13193 * Get a callback from "arg". It can be a Funcref or a function name.
13194 * When "arg" is zero return an empty string.
13195 * Return NULL for an invalid argument.
13196 */
13197 char_u *
13198get_callback(typval_T *arg, partial_T **pp)
13199{
13200 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
13201 {
13202 *pp = arg->vval.v_partial;
13203 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020013204 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013205 }
13206 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020013207 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013208 {
13209 func_ref(arg->vval.v_string);
13210 return arg->vval.v_string;
13211 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013212 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
13213 return (char_u *)"";
13214 EMSG(_("E921: Invalid callback argument"));
13215 return NULL;
13216}
13217
13218/*
Bram Moolenaard5359b22018-04-05 22:44:39 +020013219 * Unref/free "callback" and "partial" returned by get_callback().
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013220 */
13221 void
13222free_callback(char_u *callback, partial_T *partial)
13223{
13224 if (partial != NULL)
13225 partial_unref(partial);
13226 else if (callback != NULL)
13227 {
13228 func_unref(callback);
13229 vim_free(callback);
13230 }
13231}
13232#endif
13233
13234#ifdef FEAT_TIMERS
13235/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020013236 * "timer_info([timer])" function
13237 */
13238 static void
13239f_timer_info(typval_T *argvars, typval_T *rettv)
13240{
13241 timer_T *timer = NULL;
13242
13243 if (rettv_list_alloc(rettv) != OK)
13244 return;
13245 if (argvars[0].v_type != VAR_UNKNOWN)
13246 {
13247 if (argvars[0].v_type != VAR_NUMBER)
13248 EMSG(_(e_number_exp));
13249 else
13250 {
13251 timer = find_timer((int)get_tv_number(&argvars[0]));
13252 if (timer != NULL)
13253 add_timer_info(rettv, timer);
13254 }
13255 }
13256 else
13257 add_timer_info_all(rettv);
13258}
13259
13260/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013261 * "timer_pause(timer, paused)" function
13262 */
13263 static void
13264f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
13265{
13266 timer_T *timer = NULL;
13267 int paused = (int)get_tv_number(&argvars[1]);
13268
13269 if (argvars[0].v_type != VAR_NUMBER)
13270 EMSG(_(e_number_exp));
13271 else
13272 {
13273 timer = find_timer((int)get_tv_number(&argvars[0]));
13274 if (timer != NULL)
13275 timer->tr_paused = paused;
13276 }
13277}
13278
13279/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013280 * "timer_start(time, callback [, options])" function
13281 */
13282 static void
13283f_timer_start(typval_T *argvars, typval_T *rettv)
13284{
Bram Moolenaar75537a92016-09-05 22:45:28 +020013285 long msec = (long)get_tv_number(&argvars[0]);
13286 timer_T *timer;
13287 int repeat = 0;
13288 char_u *callback;
13289 dict_T *dict;
13290 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013291
Bram Moolenaar75537a92016-09-05 22:45:28 +020013292 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013293 if (check_secure())
13294 return;
13295 if (argvars[2].v_type != VAR_UNKNOWN)
13296 {
13297 if (argvars[2].v_type != VAR_DICT
13298 || (dict = argvars[2].vval.v_dict) == NULL)
13299 {
13300 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
13301 return;
13302 }
13303 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
13304 repeat = get_dict_number(dict, (char_u *)"repeat");
13305 }
13306
Bram Moolenaar75537a92016-09-05 22:45:28 +020013307 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013308 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020013309 return;
13310
13311 timer = create_timer(msec, repeat);
13312 if (timer == NULL)
13313 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013314 else
13315 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020013316 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020013317 timer->tr_callback = vim_strsave(callback);
13318 else
13319 /* pointer into the partial */
13320 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020013321 timer->tr_partial = partial;
13322 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013323 }
13324}
13325
13326/*
13327 * "timer_stop(timer)" function
13328 */
13329 static void
13330f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
13331{
13332 timer_T *timer;
13333
13334 if (argvars[0].v_type != VAR_NUMBER)
13335 {
13336 EMSG(_(e_number_exp));
13337 return;
13338 }
13339 timer = find_timer((int)get_tv_number(&argvars[0]));
13340 if (timer != NULL)
13341 stop_timer(timer);
13342}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013343
13344/*
13345 * "timer_stopall()" function
13346 */
13347 static void
13348f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13349{
13350 stop_all_timers();
13351}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013352#endif
13353
13354/*
13355 * "tolower(string)" function
13356 */
13357 static void
13358f_tolower(typval_T *argvars, typval_T *rettv)
13359{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013360 rettv->v_type = VAR_STRING;
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +010013361 rettv->vval.v_string = strlow_save(get_tv_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013362}
13363
13364/*
13365 * "toupper(string)" function
13366 */
13367 static void
13368f_toupper(typval_T *argvars, typval_T *rettv)
13369{
13370 rettv->v_type = VAR_STRING;
13371 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
13372}
13373
13374/*
13375 * "tr(string, fromstr, tostr)" function
13376 */
13377 static void
13378f_tr(typval_T *argvars, typval_T *rettv)
13379{
13380 char_u *in_str;
13381 char_u *fromstr;
13382 char_u *tostr;
13383 char_u *p;
13384#ifdef FEAT_MBYTE
13385 int inlen;
13386 int fromlen;
13387 int tolen;
13388 int idx;
13389 char_u *cpstr;
13390 int cplen;
13391 int first = TRUE;
13392#endif
13393 char_u buf[NUMBUFLEN];
13394 char_u buf2[NUMBUFLEN];
13395 garray_T ga;
13396
13397 in_str = get_tv_string(&argvars[0]);
13398 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
13399 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
13400
13401 /* Default return value: empty string. */
13402 rettv->v_type = VAR_STRING;
13403 rettv->vval.v_string = NULL;
13404 if (fromstr == NULL || tostr == NULL)
13405 return; /* type error; errmsg already given */
13406 ga_init2(&ga, (int)sizeof(char), 80);
13407
13408#ifdef FEAT_MBYTE
13409 if (!has_mbyte)
13410#endif
13411 /* not multi-byte: fromstr and tostr must be the same length */
13412 if (STRLEN(fromstr) != STRLEN(tostr))
13413 {
13414#ifdef FEAT_MBYTE
13415error:
13416#endif
13417 EMSG2(_(e_invarg2), fromstr);
13418 ga_clear(&ga);
13419 return;
13420 }
13421
13422 /* fromstr and tostr have to contain the same number of chars */
13423 while (*in_str != NUL)
13424 {
13425#ifdef FEAT_MBYTE
13426 if (has_mbyte)
13427 {
13428 inlen = (*mb_ptr2len)(in_str);
13429 cpstr = in_str;
13430 cplen = inlen;
13431 idx = 0;
13432 for (p = fromstr; *p != NUL; p += fromlen)
13433 {
13434 fromlen = (*mb_ptr2len)(p);
13435 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
13436 {
13437 for (p = tostr; *p != NUL; p += tolen)
13438 {
13439 tolen = (*mb_ptr2len)(p);
13440 if (idx-- == 0)
13441 {
13442 cplen = tolen;
13443 cpstr = p;
13444 break;
13445 }
13446 }
13447 if (*p == NUL) /* tostr is shorter than fromstr */
13448 goto error;
13449 break;
13450 }
13451 ++idx;
13452 }
13453
13454 if (first && cpstr == in_str)
13455 {
13456 /* Check that fromstr and tostr have the same number of
13457 * (multi-byte) characters. Done only once when a character
13458 * of in_str doesn't appear in fromstr. */
13459 first = FALSE;
13460 for (p = tostr; *p != NUL; p += tolen)
13461 {
13462 tolen = (*mb_ptr2len)(p);
13463 --idx;
13464 }
13465 if (idx != 0)
13466 goto error;
13467 }
13468
13469 (void)ga_grow(&ga, cplen);
13470 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
13471 ga.ga_len += cplen;
13472
13473 in_str += inlen;
13474 }
13475 else
13476#endif
13477 {
13478 /* When not using multi-byte chars we can do it faster. */
13479 p = vim_strchr(fromstr, *in_str);
13480 if (p != NULL)
13481 ga_append(&ga, tostr[p - fromstr]);
13482 else
13483 ga_append(&ga, *in_str);
13484 ++in_str;
13485 }
13486 }
13487
13488 /* add a terminating NUL */
13489 (void)ga_grow(&ga, 1);
13490 ga_append(&ga, NUL);
13491
13492 rettv->vval.v_string = ga.ga_data;
13493}
13494
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010013495/*
13496 * "trim({expr})" function
13497 */
13498 static void
13499f_trim(typval_T *argvars, typval_T *rettv)
13500{
13501 char_u buf1[NUMBUFLEN];
13502 char_u buf2[NUMBUFLEN];
13503 char_u *head = get_tv_string_buf_chk(&argvars[0], buf1);
13504 char_u *mask = NULL;
13505 char_u *tail;
13506 char_u *prev;
13507 char_u *p;
13508 int c1;
13509
13510 rettv->v_type = VAR_STRING;
13511 if (head == NULL)
13512 {
13513 rettv->vval.v_string = NULL;
13514 return;
13515 }
13516
13517 if (argvars[1].v_type == VAR_STRING)
13518 mask = get_tv_string_buf_chk(&argvars[1], buf2);
13519
13520 while (*head != NUL)
13521 {
13522 c1 = PTR2CHAR(head);
13523 if (mask == NULL)
13524 {
13525 if (c1 > ' ' && c1 != 0xa0)
13526 break;
13527 }
13528 else
13529 {
13530 for (p = mask; *p != NUL; MB_PTR_ADV(p))
13531 if (c1 == PTR2CHAR(p))
13532 break;
13533 if (*p == NUL)
13534 break;
13535 }
13536 MB_PTR_ADV(head);
13537 }
13538
13539 for (tail = head + STRLEN(head); tail > head; tail = prev)
13540 {
13541 prev = tail;
13542 MB_PTR_BACK(head, prev);
13543 c1 = PTR2CHAR(prev);
13544 if (mask == NULL)
13545 {
13546 if (c1 > ' ' && c1 != 0xa0)
13547 break;
13548 }
13549 else
13550 {
13551 for (p = mask; *p != NUL; MB_PTR_ADV(p))
13552 if (c1 == PTR2CHAR(p))
13553 break;
13554 if (*p == NUL)
13555 break;
13556 }
13557 }
13558 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
13559}
13560
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013561#ifdef FEAT_FLOAT
13562/*
13563 * "trunc({float})" function
13564 */
13565 static void
13566f_trunc(typval_T *argvars, typval_T *rettv)
13567{
13568 float_T f = 0.0;
13569
13570 rettv->v_type = VAR_FLOAT;
13571 if (get_float_arg(argvars, &f) == OK)
13572 /* trunc() is not in C90, use floor() or ceil() instead. */
13573 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
13574 else
13575 rettv->vval.v_float = 0.0;
13576}
13577#endif
13578
13579/*
13580 * "type(expr)" function
13581 */
13582 static void
13583f_type(typval_T *argvars, typval_T *rettv)
13584{
13585 int n = -1;
13586
13587 switch (argvars[0].v_type)
13588 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020013589 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
13590 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013591 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020013592 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
13593 case VAR_LIST: n = VAR_TYPE_LIST; break;
13594 case VAR_DICT: n = VAR_TYPE_DICT; break;
13595 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013596 case VAR_SPECIAL:
13597 if (argvars[0].vval.v_number == VVAL_FALSE
13598 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020013599 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013600 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020013601 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013602 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020013603 case VAR_JOB: n = VAR_TYPE_JOB; break;
13604 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013605 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010013606 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013607 n = -1;
13608 break;
13609 }
13610 rettv->vval.v_number = n;
13611}
13612
13613/*
13614 * "undofile(name)" function
13615 */
13616 static void
13617f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
13618{
13619 rettv->v_type = VAR_STRING;
13620#ifdef FEAT_PERSISTENT_UNDO
13621 {
13622 char_u *fname = get_tv_string(&argvars[0]);
13623
13624 if (*fname == NUL)
13625 {
13626 /* If there is no file name there will be no undo file. */
13627 rettv->vval.v_string = NULL;
13628 }
13629 else
13630 {
13631 char_u *ffname = FullName_save(fname, FALSE);
13632
13633 if (ffname != NULL)
13634 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
13635 vim_free(ffname);
13636 }
13637 }
13638#else
13639 rettv->vval.v_string = NULL;
13640#endif
13641}
13642
13643/*
13644 * "undotree()" function
13645 */
13646 static void
13647f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
13648{
13649 if (rettv_dict_alloc(rettv) == OK)
13650 {
13651 dict_T *dict = rettv->vval.v_dict;
13652 list_T *list;
13653
Bram Moolenaare0be1672018-07-08 16:50:37 +020013654 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
13655 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
13656 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
13657 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
13658 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
13659 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013660
13661 list = list_alloc();
13662 if (list != NULL)
13663 {
13664 u_eval_tree(curbuf->b_u_oldhead, list);
13665 dict_add_list(dict, "entries", list);
13666 }
13667 }
13668}
13669
13670/*
13671 * "values(dict)" function
13672 */
13673 static void
13674f_values(typval_T *argvars, typval_T *rettv)
13675{
13676 dict_list(argvars, rettv, 1);
13677}
13678
13679/*
13680 * "virtcol(string)" function
13681 */
13682 static void
13683f_virtcol(typval_T *argvars, typval_T *rettv)
13684{
13685 colnr_T vcol = 0;
13686 pos_T *fp;
13687 int fnum = curbuf->b_fnum;
13688
13689 fp = var2fpos(&argvars[0], FALSE, &fnum);
13690 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
13691 && fnum == curbuf->b_fnum)
13692 {
13693 getvvcol(curwin, fp, NULL, NULL, &vcol);
13694 ++vcol;
13695 }
13696
13697 rettv->vval.v_number = vcol;
13698}
13699
13700/*
13701 * "visualmode()" function
13702 */
13703 static void
13704f_visualmode(typval_T *argvars, typval_T *rettv)
13705{
13706 char_u str[2];
13707
13708 rettv->v_type = VAR_STRING;
13709 str[0] = curbuf->b_visual_mode_eval;
13710 str[1] = NUL;
13711 rettv->vval.v_string = vim_strsave(str);
13712
13713 /* A non-zero number or non-empty string argument: reset mode. */
13714 if (non_zero_arg(&argvars[0]))
13715 curbuf->b_visual_mode_eval = NUL;
13716}
13717
13718/*
13719 * "wildmenumode()" function
13720 */
13721 static void
13722f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13723{
13724#ifdef FEAT_WILDMENU
13725 if (wild_menu_showing)
13726 rettv->vval.v_number = 1;
13727#endif
13728}
13729
13730/*
13731 * "winbufnr(nr)" function
13732 */
13733 static void
13734f_winbufnr(typval_T *argvars, typval_T *rettv)
13735{
13736 win_T *wp;
13737
13738 wp = find_win_by_nr(&argvars[0], NULL);
13739 if (wp == NULL)
13740 rettv->vval.v_number = -1;
13741 else
13742 rettv->vval.v_number = wp->w_buffer->b_fnum;
13743}
13744
13745/*
13746 * "wincol()" function
13747 */
13748 static void
13749f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
13750{
13751 validate_cursor();
13752 rettv->vval.v_number = curwin->w_wcol + 1;
13753}
13754
13755/*
13756 * "winheight(nr)" function
13757 */
13758 static void
13759f_winheight(typval_T *argvars, typval_T *rettv)
13760{
13761 win_T *wp;
13762
13763 wp = find_win_by_nr(&argvars[0], NULL);
13764 if (wp == NULL)
13765 rettv->vval.v_number = -1;
13766 else
13767 rettv->vval.v_number = wp->w_height;
13768}
13769
13770/*
13771 * "winline()" function
13772 */
13773 static void
13774f_winline(typval_T *argvars UNUSED, typval_T *rettv)
13775{
13776 validate_cursor();
13777 rettv->vval.v_number = curwin->w_wrow + 1;
13778}
13779
13780/*
13781 * "winnr()" function
13782 */
13783 static void
13784f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
13785{
13786 int nr = 1;
13787
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013788 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013789 rettv->vval.v_number = nr;
13790}
13791
13792/*
13793 * "winrestcmd()" function
13794 */
13795 static void
13796f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
13797{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013798 win_T *wp;
13799 int winnr = 1;
13800 garray_T ga;
13801 char_u buf[50];
13802
13803 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020013804 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013805 {
13806 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
13807 ga_concat(&ga, buf);
13808 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
13809 ga_concat(&ga, buf);
13810 ++winnr;
13811 }
13812 ga_append(&ga, NUL);
13813
13814 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013815 rettv->v_type = VAR_STRING;
13816}
13817
13818/*
13819 * "winrestview()" function
13820 */
13821 static void
13822f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
13823{
13824 dict_T *dict;
13825
13826 if (argvars[0].v_type != VAR_DICT
13827 || (dict = argvars[0].vval.v_dict) == NULL)
13828 EMSG(_(e_invarg));
13829 else
13830 {
13831 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
13832 curwin->w_cursor.lnum = (linenr_T)get_dict_number(dict, (char_u *)"lnum");
13833 if (dict_find(dict, (char_u *)"col", -1) != NULL)
13834 curwin->w_cursor.col = (colnr_T)get_dict_number(dict, (char_u *)"col");
13835#ifdef FEAT_VIRTUALEDIT
13836 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
13837 curwin->w_cursor.coladd = (colnr_T)get_dict_number(dict, (char_u *)"coladd");
13838#endif
13839 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
13840 {
13841 curwin->w_curswant = (colnr_T)get_dict_number(dict, (char_u *)"curswant");
13842 curwin->w_set_curswant = FALSE;
13843 }
13844
13845 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
13846 set_topline(curwin, (linenr_T)get_dict_number(dict, (char_u *)"topline"));
13847#ifdef FEAT_DIFF
13848 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
13849 curwin->w_topfill = (int)get_dict_number(dict, (char_u *)"topfill");
13850#endif
13851 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
13852 curwin->w_leftcol = (colnr_T)get_dict_number(dict, (char_u *)"leftcol");
13853 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
13854 curwin->w_skipcol = (colnr_T)get_dict_number(dict, (char_u *)"skipcol");
13855
13856 check_cursor();
13857 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020013858 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013859 changed_window_setting();
13860
13861 if (curwin->w_topline <= 0)
13862 curwin->w_topline = 1;
13863 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
13864 curwin->w_topline = curbuf->b_ml.ml_line_count;
13865#ifdef FEAT_DIFF
13866 check_topfill(curwin, TRUE);
13867#endif
13868 }
13869}
13870
13871/*
13872 * "winsaveview()" function
13873 */
13874 static void
13875f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
13876{
13877 dict_T *dict;
13878
13879 if (rettv_dict_alloc(rettv) == FAIL)
13880 return;
13881 dict = rettv->vval.v_dict;
13882
Bram Moolenaare0be1672018-07-08 16:50:37 +020013883 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
13884 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013885#ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +020013886 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013887#endif
13888 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020013889 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013890
Bram Moolenaare0be1672018-07-08 16:50:37 +020013891 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013892#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020013893 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013894#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020013895 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
13896 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013897}
13898
13899/*
13900 * "winwidth(nr)" function
13901 */
13902 static void
13903f_winwidth(typval_T *argvars, typval_T *rettv)
13904{
13905 win_T *wp;
13906
13907 wp = find_win_by_nr(&argvars[0], NULL);
13908 if (wp == NULL)
13909 rettv->vval.v_number = -1;
13910 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013911 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013912}
13913
13914/*
13915 * "wordcount()" function
13916 */
13917 static void
13918f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
13919{
13920 if (rettv_dict_alloc(rettv) == FAIL)
13921 return;
13922 cursor_pos_info(rettv->vval.v_dict);
13923}
13924
13925/*
13926 * "writefile()" function
13927 */
13928 static void
13929f_writefile(typval_T *argvars, typval_T *rettv)
13930{
13931 int binary = FALSE;
13932 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013933#ifdef HAVE_FSYNC
13934 int do_fsync = p_fs;
13935#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013936 char_u *fname;
13937 FILE *fd;
13938 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013939 listitem_T *li;
13940 list_T *list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013941
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013942 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013943 if (check_restricted() || check_secure())
13944 return;
13945
13946 if (argvars[0].v_type != VAR_LIST)
13947 {
13948 EMSG2(_(e_listarg), "writefile()");
13949 return;
13950 }
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013951 list = argvars[0].vval.v_list;
13952 if (list == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013953 return;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013954 for (li = list->lv_first; li != NULL; li = li->li_next)
13955 if (get_tv_string_chk(&li->li_tv) == NULL)
13956 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013957
13958 if (argvars[2].v_type != VAR_UNKNOWN)
13959 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013960 char_u *arg2 = get_tv_string_chk(&argvars[2]);
13961
13962 if (arg2 == NULL)
13963 return;
13964 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013965 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013966 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013967 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013968#ifdef HAVE_FSYNC
13969 if (vim_strchr(arg2, 's') != NULL)
13970 do_fsync = TRUE;
13971 else if (vim_strchr(arg2, 'S') != NULL)
13972 do_fsync = FALSE;
13973#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013974 }
13975
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013976 fname = get_tv_string_chk(&argvars[1]);
13977 if (fname == NULL)
13978 return;
13979
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013980 /* Always open the file in binary mode, library functions have a mind of
13981 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013982 if (*fname == NUL || (fd = mch_fopen((char *)fname,
13983 append ? APPENDBIN : WRITEBIN)) == NULL)
13984 {
13985 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
13986 ret = -1;
13987 }
13988 else
13989 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013990 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013991 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013992#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010013993 else if (do_fsync)
13994 /* Ignore the error, the user wouldn't know what to do about it.
13995 * May happen for a device. */
13996 ignored = fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013997#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013998 fclose(fd);
13999 }
14000
14001 rettv->vval.v_number = ret;
14002}
14003
14004/*
14005 * "xor(expr, expr)" function
14006 */
14007 static void
14008f_xor(typval_T *argvars, typval_T *rettv)
14009{
14010 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14011 ^ get_tv_number_chk(&argvars[1], NULL);
14012}
14013
14014
14015#endif /* FEAT_EVAL */