blob: afad8be6f449fee4c2b5ceecc42d377ea4ab0e85 [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 Moolenaarf49cc602018-11-11 15:21:05 +0100204static void f_gettagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200205static void f_getwininfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100206static void f_getwinpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200207static void f_getwinposx(typval_T *argvars, typval_T *rettv);
208static void f_getwinposy(typval_T *argvars, typval_T *rettv);
209static void f_getwinvar(typval_T *argvars, typval_T *rettv);
210static void f_glob(typval_T *argvars, typval_T *rettv);
211static void f_globpath(typval_T *argvars, typval_T *rettv);
212static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
213static void f_has(typval_T *argvars, typval_T *rettv);
214static void f_has_key(typval_T *argvars, typval_T *rettv);
215static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
216static void f_hasmapto(typval_T *argvars, typval_T *rettv);
217static void f_histadd(typval_T *argvars, typval_T *rettv);
218static void f_histdel(typval_T *argvars, typval_T *rettv);
219static void f_histget(typval_T *argvars, typval_T *rettv);
220static void f_histnr(typval_T *argvars, typval_T *rettv);
221static void f_hlID(typval_T *argvars, typval_T *rettv);
222static void f_hlexists(typval_T *argvars, typval_T *rettv);
223static void f_hostname(typval_T *argvars, typval_T *rettv);
224static void f_iconv(typval_T *argvars, typval_T *rettv);
225static void f_indent(typval_T *argvars, typval_T *rettv);
226static void f_index(typval_T *argvars, typval_T *rettv);
227static void f_input(typval_T *argvars, typval_T *rettv);
228static void f_inputdialog(typval_T *argvars, typval_T *rettv);
229static void f_inputlist(typval_T *argvars, typval_T *rettv);
230static void f_inputrestore(typval_T *argvars, typval_T *rettv);
231static void f_inputsave(typval_T *argvars, typval_T *rettv);
232static void f_inputsecret(typval_T *argvars, typval_T *rettv);
233static void f_insert(typval_T *argvars, typval_T *rettv);
234static void f_invert(typval_T *argvars, typval_T *rettv);
235static void f_isdirectory(typval_T *argvars, typval_T *rettv);
236static void f_islocked(typval_T *argvars, typval_T *rettv);
237#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
238static void f_isnan(typval_T *argvars, typval_T *rettv);
239#endif
240static void f_items(typval_T *argvars, typval_T *rettv);
241#ifdef FEAT_JOB_CHANNEL
242static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
243static void f_job_info(typval_T *argvars, typval_T *rettv);
244static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
245static void f_job_start(typval_T *argvars, typval_T *rettv);
246static void f_job_stop(typval_T *argvars, typval_T *rettv);
247static void f_job_status(typval_T *argvars, typval_T *rettv);
248#endif
249static void f_join(typval_T *argvars, typval_T *rettv);
250static void f_js_decode(typval_T *argvars, typval_T *rettv);
251static void f_js_encode(typval_T *argvars, typval_T *rettv);
252static void f_json_decode(typval_T *argvars, typval_T *rettv);
253static void f_json_encode(typval_T *argvars, typval_T *rettv);
254static void f_keys(typval_T *argvars, typval_T *rettv);
255static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
256static void f_len(typval_T *argvars, typval_T *rettv);
257static void f_libcall(typval_T *argvars, typval_T *rettv);
258static void f_libcallnr(typval_T *argvars, typval_T *rettv);
259static void f_line(typval_T *argvars, typval_T *rettv);
260static void f_line2byte(typval_T *argvars, typval_T *rettv);
261static void f_lispindent(typval_T *argvars, typval_T *rettv);
262static void f_localtime(typval_T *argvars, typval_T *rettv);
263#ifdef FEAT_FLOAT
264static void f_log(typval_T *argvars, typval_T *rettv);
265static void f_log10(typval_T *argvars, typval_T *rettv);
266#endif
267#ifdef FEAT_LUA
268static void f_luaeval(typval_T *argvars, typval_T *rettv);
269#endif
270static void f_map(typval_T *argvars, typval_T *rettv);
271static void f_maparg(typval_T *argvars, typval_T *rettv);
272static void f_mapcheck(typval_T *argvars, typval_T *rettv);
273static void f_match(typval_T *argvars, typval_T *rettv);
274static void f_matchadd(typval_T *argvars, typval_T *rettv);
275static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
276static void f_matcharg(typval_T *argvars, typval_T *rettv);
277static void f_matchdelete(typval_T *argvars, typval_T *rettv);
278static void f_matchend(typval_T *argvars, typval_T *rettv);
279static void f_matchlist(typval_T *argvars, typval_T *rettv);
280static void f_matchstr(typval_T *argvars, typval_T *rettv);
281static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
282static void f_max(typval_T *argvars, typval_T *rettv);
283static void f_min(typval_T *argvars, typval_T *rettv);
284#ifdef vim_mkdir
285static void f_mkdir(typval_T *argvars, typval_T *rettv);
286#endif
287static void f_mode(typval_T *argvars, typval_T *rettv);
288#ifdef FEAT_MZSCHEME
289static void f_mzeval(typval_T *argvars, typval_T *rettv);
290#endif
291static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
292static void f_nr2char(typval_T *argvars, typval_T *rettv);
293static void f_or(typval_T *argvars, typval_T *rettv);
294static void f_pathshorten(typval_T *argvars, typval_T *rettv);
295#ifdef FEAT_PERL
296static void f_perleval(typval_T *argvars, typval_T *rettv);
297#endif
298#ifdef FEAT_FLOAT
299static void f_pow(typval_T *argvars, typval_T *rettv);
300#endif
301static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
302static void f_printf(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200303#ifdef FEAT_JOB_CHANNEL
304static void f_prompt_setcallback(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200305static void f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200306static void f_prompt_setprompt(typval_T *argvars, typval_T *rettv);
307#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200308static void f_pumvisible(typval_T *argvars, typval_T *rettv);
309#ifdef FEAT_PYTHON3
310static void f_py3eval(typval_T *argvars, typval_T *rettv);
311#endif
312#ifdef FEAT_PYTHON
313static void f_pyeval(typval_T *argvars, typval_T *rettv);
314#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100315#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
316static void f_pyxeval(typval_T *argvars, typval_T *rettv);
317#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200318static void f_range(typval_T *argvars, typval_T *rettv);
319static void f_readfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200320static void f_reg_executing(typval_T *argvars, typval_T *rettv);
321static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200322static void f_reltime(typval_T *argvars, typval_T *rettv);
323#ifdef FEAT_FLOAT
324static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
325#endif
326static void f_reltimestr(typval_T *argvars, typval_T *rettv);
327static void f_remote_expr(typval_T *argvars, typval_T *rettv);
328static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
329static void f_remote_peek(typval_T *argvars, typval_T *rettv);
330static void f_remote_read(typval_T *argvars, typval_T *rettv);
331static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100332static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200333static void f_remove(typval_T *argvars, typval_T *rettv);
334static void f_rename(typval_T *argvars, typval_T *rettv);
335static void f_repeat(typval_T *argvars, typval_T *rettv);
336static void f_resolve(typval_T *argvars, typval_T *rettv);
337static void f_reverse(typval_T *argvars, typval_T *rettv);
338#ifdef FEAT_FLOAT
339static void f_round(typval_T *argvars, typval_T *rettv);
340#endif
341static void f_screenattr(typval_T *argvars, typval_T *rettv);
342static void f_screenchar(typval_T *argvars, typval_T *rettv);
343static void f_screencol(typval_T *argvars, typval_T *rettv);
344static void f_screenrow(typval_T *argvars, typval_T *rettv);
345static void f_search(typval_T *argvars, typval_T *rettv);
346static void f_searchdecl(typval_T *argvars, typval_T *rettv);
347static void f_searchpair(typval_T *argvars, typval_T *rettv);
348static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
349static void f_searchpos(typval_T *argvars, typval_T *rettv);
350static void f_server2client(typval_T *argvars, typval_T *rettv);
351static void f_serverlist(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200352static void f_setbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200353static void f_setbufvar(typval_T *argvars, typval_T *rettv);
354static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
355static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
356static void f_setfperm(typval_T *argvars, typval_T *rettv);
357static void f_setline(typval_T *argvars, typval_T *rettv);
358static void f_setloclist(typval_T *argvars, typval_T *rettv);
359static void f_setmatches(typval_T *argvars, typval_T *rettv);
360static void f_setpos(typval_T *argvars, typval_T *rettv);
361static void f_setqflist(typval_T *argvars, typval_T *rettv);
362static void f_setreg(typval_T *argvars, typval_T *rettv);
363static void f_settabvar(typval_T *argvars, typval_T *rettv);
364static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100365static void f_settagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200366static void f_setwinvar(typval_T *argvars, typval_T *rettv);
367#ifdef FEAT_CRYPT
368static void f_sha256(typval_T *argvars, typval_T *rettv);
369#endif /* FEAT_CRYPT */
370static void f_shellescape(typval_T *argvars, typval_T *rettv);
371static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
372static void f_simplify(typval_T *argvars, typval_T *rettv);
373#ifdef FEAT_FLOAT
374static void f_sin(typval_T *argvars, typval_T *rettv);
375static void f_sinh(typval_T *argvars, typval_T *rettv);
376#endif
377static void f_sort(typval_T *argvars, typval_T *rettv);
378static void f_soundfold(typval_T *argvars, typval_T *rettv);
379static void f_spellbadword(typval_T *argvars, typval_T *rettv);
380static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
381static void f_split(typval_T *argvars, typval_T *rettv);
382#ifdef FEAT_FLOAT
383static void f_sqrt(typval_T *argvars, typval_T *rettv);
384static void f_str2float(typval_T *argvars, typval_T *rettv);
385#endif
386static void f_str2nr(typval_T *argvars, typval_T *rettv);
387static void f_strchars(typval_T *argvars, typval_T *rettv);
388#ifdef HAVE_STRFTIME
389static void f_strftime(typval_T *argvars, typval_T *rettv);
390#endif
391static void f_strgetchar(typval_T *argvars, typval_T *rettv);
392static void f_stridx(typval_T *argvars, typval_T *rettv);
393static void f_string(typval_T *argvars, typval_T *rettv);
394static void f_strlen(typval_T *argvars, typval_T *rettv);
395static void f_strcharpart(typval_T *argvars, typval_T *rettv);
396static void f_strpart(typval_T *argvars, typval_T *rettv);
397static void f_strridx(typval_T *argvars, typval_T *rettv);
398static void f_strtrans(typval_T *argvars, typval_T *rettv);
399static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
400static void f_strwidth(typval_T *argvars, typval_T *rettv);
401static void f_submatch(typval_T *argvars, typval_T *rettv);
402static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200403static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200404static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200405static void f_synID(typval_T *argvars, typval_T *rettv);
406static void f_synIDattr(typval_T *argvars, typval_T *rettv);
407static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
408static void f_synstack(typval_T *argvars, typval_T *rettv);
409static void f_synconcealed(typval_T *argvars, typval_T *rettv);
410static void f_system(typval_T *argvars, typval_T *rettv);
411static void f_systemlist(typval_T *argvars, typval_T *rettv);
412static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
413static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
414static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
415static void f_taglist(typval_T *argvars, typval_T *rettv);
416static void f_tagfiles(typval_T *argvars, typval_T *rettv);
417static void f_tempname(typval_T *argvars, typval_T *rettv);
418static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
419static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200420static void f_test_feedinput(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200421static void f_test_option_not_set(typval_T *argvars, typval_T *rettv);
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100422static void f_test_override(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200423static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100424static void f_test_ignore_error(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200425#ifdef FEAT_JOB_CHANNEL
426static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
427#endif
428static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
429#ifdef FEAT_JOB_CHANNEL
430static void f_test_null_job(typval_T *argvars, typval_T *rettv);
431#endif
432static void f_test_null_list(typval_T *argvars, typval_T *rettv);
433static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
434static void f_test_null_string(typval_T *argvars, typval_T *rettv);
Bram Moolenaarab186732018-09-14 21:27:06 +0200435#ifdef FEAT_GUI
436static void f_test_scrollbar(typval_T *argvars, typval_T *rettv);
437#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200438static void f_test_settime(typval_T *argvars, typval_T *rettv);
439#ifdef FEAT_FLOAT
440static void f_tan(typval_T *argvars, typval_T *rettv);
441static void f_tanh(typval_T *argvars, typval_T *rettv);
442#endif
443#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200444static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200445static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200446static void f_timer_start(typval_T *argvars, typval_T *rettv);
447static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200448static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200449#endif
450static void f_tolower(typval_T *argvars, typval_T *rettv);
451static void f_toupper(typval_T *argvars, typval_T *rettv);
452static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100453static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200454#ifdef FEAT_FLOAT
455static void f_trunc(typval_T *argvars, typval_T *rettv);
456#endif
457static void f_type(typval_T *argvars, typval_T *rettv);
458static void f_undofile(typval_T *argvars, typval_T *rettv);
459static void f_undotree(typval_T *argvars, typval_T *rettv);
460static void f_uniq(typval_T *argvars, typval_T *rettv);
461static void f_values(typval_T *argvars, typval_T *rettv);
462static void f_virtcol(typval_T *argvars, typval_T *rettv);
463static void f_visualmode(typval_T *argvars, typval_T *rettv);
464static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
465static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
466static void f_win_getid(typval_T *argvars, typval_T *rettv);
467static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
468static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
469static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100470static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200471static void f_winbufnr(typval_T *argvars, typval_T *rettv);
472static void f_wincol(typval_T *argvars, typval_T *rettv);
473static void f_winheight(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200474static void f_winlayout(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200475static void f_winline(typval_T *argvars, typval_T *rettv);
476static void f_winnr(typval_T *argvars, typval_T *rettv);
477static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
478static void f_winrestview(typval_T *argvars, typval_T *rettv);
479static void f_winsaveview(typval_T *argvars, typval_T *rettv);
480static void f_winwidth(typval_T *argvars, typval_T *rettv);
481static void f_writefile(typval_T *argvars, typval_T *rettv);
482static void f_wordcount(typval_T *argvars, typval_T *rettv);
483static void f_xor(typval_T *argvars, typval_T *rettv);
484
485/*
486 * Array with names and number of arguments of all internal functions
487 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
488 */
489static struct fst
490{
491 char *f_name; /* function name */
492 char f_min_argc; /* minimal number of arguments */
493 char f_max_argc; /* maximal number of arguments */
494 void (*f_func)(typval_T *args, typval_T *rvar);
495 /* implementation of function */
496} functions[] =
497{
498#ifdef FEAT_FLOAT
499 {"abs", 1, 1, f_abs},
500 {"acos", 1, 1, f_acos}, /* WJMc */
501#endif
502 {"add", 2, 2, f_add},
503 {"and", 2, 2, f_and},
504 {"append", 2, 2, f_append},
Bram Moolenaarca851592018-06-06 21:04:07 +0200505 {"appendbufline", 3, 3, f_appendbufline},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200506 {"argc", 0, 1, f_argc},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200507 {"argidx", 0, 0, f_argidx},
508 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200509 {"argv", 0, 2, f_argv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200510#ifdef FEAT_FLOAT
511 {"asin", 1, 1, f_asin}, /* WJMc */
512#endif
Bram Moolenaarb48e96f2018-02-13 12:26:14 +0100513 {"assert_beeps", 1, 2, f_assert_beeps},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200514 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaard96ff162018-02-18 22:13:29 +0100515 {"assert_equalfile", 2, 2, f_assert_equalfile},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200516 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaar1307d1c2018-10-07 20:16:49 +0200517 {"assert_fails", 1, 3, f_assert_fails},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200518 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100519 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200520 {"assert_match", 2, 3, f_assert_match},
521 {"assert_notequal", 2, 3, f_assert_notequal},
522 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100523 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200524 {"assert_true", 1, 2, f_assert_true},
525#ifdef FEAT_FLOAT
526 {"atan", 1, 1, f_atan},
527 {"atan2", 2, 2, f_atan2},
528#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100529#ifdef FEAT_BEVAL
530 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100531# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100532 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100533# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100534#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200535 {"browse", 4, 4, f_browse},
536 {"browsedir", 2, 2, f_browsedir},
537 {"bufexists", 1, 1, f_bufexists},
538 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
539 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
540 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
541 {"buflisted", 1, 1, f_buflisted},
542 {"bufloaded", 1, 1, f_bufloaded},
543 {"bufname", 1, 1, f_bufname},
544 {"bufnr", 1, 2, f_bufnr},
545 {"bufwinid", 1, 1, f_bufwinid},
546 {"bufwinnr", 1, 1, f_bufwinnr},
547 {"byte2line", 1, 1, f_byte2line},
548 {"byteidx", 2, 2, f_byteidx},
549 {"byteidxcomp", 2, 2, f_byteidxcomp},
550 {"call", 2, 3, f_call},
551#ifdef FEAT_FLOAT
552 {"ceil", 1, 1, f_ceil},
553#endif
554#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100555 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200556 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200557 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200558 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
559 {"ch_evalraw", 2, 3, f_ch_evalraw},
560 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
561 {"ch_getjob", 1, 1, f_ch_getjob},
562 {"ch_info", 1, 1, f_ch_info},
563 {"ch_log", 1, 2, f_ch_log},
564 {"ch_logfile", 1, 2, f_ch_logfile},
565 {"ch_open", 1, 2, f_ch_open},
566 {"ch_read", 1, 2, f_ch_read},
567 {"ch_readraw", 1, 2, f_ch_readraw},
568 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
569 {"ch_sendraw", 2, 3, f_ch_sendraw},
570 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200571 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200572#endif
573 {"changenr", 0, 0, f_changenr},
574 {"char2nr", 1, 2, f_char2nr},
575 {"cindent", 1, 1, f_cindent},
576 {"clearmatches", 0, 0, f_clearmatches},
577 {"col", 1, 1, f_col},
578#if defined(FEAT_INS_EXPAND)
579 {"complete", 2, 2, f_complete},
580 {"complete_add", 1, 1, f_complete_add},
581 {"complete_check", 0, 0, f_complete_check},
582#endif
583 {"confirm", 1, 4, f_confirm},
584 {"copy", 1, 1, f_copy},
585#ifdef FEAT_FLOAT
586 {"cos", 1, 1, f_cos},
587 {"cosh", 1, 1, f_cosh},
588#endif
589 {"count", 2, 4, f_count},
590 {"cscope_connection",0,3, f_cscope_connection},
591 {"cursor", 1, 3, f_cursor},
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200592#ifdef WIN3264
593 {"debugbreak", 1, 1, f_debugbreak},
594#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200595 {"deepcopy", 1, 2, f_deepcopy},
596 {"delete", 1, 2, f_delete},
Bram Moolenaard79a2622018-06-07 18:17:46 +0200597 {"deletebufline", 2, 3, f_deletebufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200598 {"did_filetype", 0, 0, f_did_filetype},
599 {"diff_filler", 1, 1, f_diff_filler},
600 {"diff_hlID", 2, 2, f_diff_hlID},
601 {"empty", 1, 1, f_empty},
602 {"escape", 2, 2, f_escape},
603 {"eval", 1, 1, f_eval},
604 {"eventhandler", 0, 0, f_eventhandler},
605 {"executable", 1, 1, f_executable},
606 {"execute", 1, 2, f_execute},
607 {"exepath", 1, 1, f_exepath},
608 {"exists", 1, 1, f_exists},
609#ifdef FEAT_FLOAT
610 {"exp", 1, 1, f_exp},
611#endif
612 {"expand", 1, 3, f_expand},
613 {"extend", 2, 3, f_extend},
614 {"feedkeys", 1, 2, f_feedkeys},
615 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
616 {"filereadable", 1, 1, f_filereadable},
617 {"filewritable", 1, 1, f_filewritable},
618 {"filter", 2, 2, f_filter},
619 {"finddir", 1, 3, f_finddir},
620 {"findfile", 1, 3, f_findfile},
621#ifdef FEAT_FLOAT
622 {"float2nr", 1, 1, f_float2nr},
623 {"floor", 1, 1, f_floor},
624 {"fmod", 2, 2, f_fmod},
625#endif
626 {"fnameescape", 1, 1, f_fnameescape},
627 {"fnamemodify", 2, 2, f_fnamemodify},
628 {"foldclosed", 1, 1, f_foldclosed},
629 {"foldclosedend", 1, 1, f_foldclosedend},
630 {"foldlevel", 1, 1, f_foldlevel},
631 {"foldtext", 0, 0, f_foldtext},
632 {"foldtextresult", 1, 1, f_foldtextresult},
633 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200634 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200635 {"function", 1, 3, f_function},
636 {"garbagecollect", 0, 1, f_garbagecollect},
637 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200638 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200639 {"getbufline", 2, 3, f_getbufline},
640 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100641 {"getchangelist", 1, 1, f_getchangelist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200642 {"getchar", 0, 1, f_getchar},
643 {"getcharmod", 0, 0, f_getcharmod},
644 {"getcharsearch", 0, 0, f_getcharsearch},
645 {"getcmdline", 0, 0, f_getcmdline},
646 {"getcmdpos", 0, 0, f_getcmdpos},
647 {"getcmdtype", 0, 0, f_getcmdtype},
648 {"getcmdwintype", 0, 0, f_getcmdwintype},
649#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200650 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200651#endif
652 {"getcurpos", 0, 0, f_getcurpos},
653 {"getcwd", 0, 2, f_getcwd},
654 {"getfontname", 0, 1, f_getfontname},
655 {"getfperm", 1, 1, f_getfperm},
656 {"getfsize", 1, 1, f_getfsize},
657 {"getftime", 1, 1, f_getftime},
658 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100659 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200660 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200661 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200662 {"getmatches", 0, 0, f_getmatches},
663 {"getpid", 0, 0, f_getpid},
664 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200665 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200666 {"getreg", 0, 3, f_getreg},
667 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200668 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200669 {"gettabvar", 2, 3, f_gettabvar},
670 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100671 {"gettagstack", 0, 1, f_gettagstack},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200672 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100673 {"getwinpos", 0, 1, f_getwinpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200674 {"getwinposx", 0, 0, f_getwinposx},
675 {"getwinposy", 0, 0, f_getwinposy},
676 {"getwinvar", 2, 3, f_getwinvar},
677 {"glob", 1, 4, f_glob},
678 {"glob2regpat", 1, 1, f_glob2regpat},
679 {"globpath", 2, 5, f_globpath},
680 {"has", 1, 1, f_has},
681 {"has_key", 2, 2, f_has_key},
682 {"haslocaldir", 0, 2, f_haslocaldir},
683 {"hasmapto", 1, 3, f_hasmapto},
684 {"highlightID", 1, 1, f_hlID}, /* obsolete */
685 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
686 {"histadd", 2, 2, f_histadd},
687 {"histdel", 1, 2, f_histdel},
688 {"histget", 1, 2, f_histget},
689 {"histnr", 1, 1, f_histnr},
690 {"hlID", 1, 1, f_hlID},
691 {"hlexists", 1, 1, f_hlexists},
692 {"hostname", 0, 0, f_hostname},
693 {"iconv", 3, 3, f_iconv},
694 {"indent", 1, 1, f_indent},
695 {"index", 2, 4, f_index},
696 {"input", 1, 3, f_input},
697 {"inputdialog", 1, 3, f_inputdialog},
698 {"inputlist", 1, 1, f_inputlist},
699 {"inputrestore", 0, 0, f_inputrestore},
700 {"inputsave", 0, 0, f_inputsave},
701 {"inputsecret", 1, 2, f_inputsecret},
702 {"insert", 2, 3, f_insert},
703 {"invert", 1, 1, f_invert},
704 {"isdirectory", 1, 1, f_isdirectory},
705 {"islocked", 1, 1, f_islocked},
706#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
707 {"isnan", 1, 1, f_isnan},
708#endif
709 {"items", 1, 1, f_items},
710#ifdef FEAT_JOB_CHANNEL
711 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200712 {"job_info", 0, 1, f_job_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200713 {"job_setoptions", 2, 2, f_job_setoptions},
714 {"job_start", 1, 2, f_job_start},
715 {"job_status", 1, 1, f_job_status},
716 {"job_stop", 1, 2, f_job_stop},
717#endif
718 {"join", 1, 2, f_join},
719 {"js_decode", 1, 1, f_js_decode},
720 {"js_encode", 1, 1, f_js_encode},
721 {"json_decode", 1, 1, f_json_decode},
722 {"json_encode", 1, 1, f_json_encode},
723 {"keys", 1, 1, f_keys},
724 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
725 {"len", 1, 1, f_len},
726 {"libcall", 3, 3, f_libcall},
727 {"libcallnr", 3, 3, f_libcallnr},
728 {"line", 1, 1, f_line},
729 {"line2byte", 1, 1, f_line2byte},
730 {"lispindent", 1, 1, f_lispindent},
731 {"localtime", 0, 0, f_localtime},
732#ifdef FEAT_FLOAT
733 {"log", 1, 1, f_log},
734 {"log10", 1, 1, f_log10},
735#endif
736#ifdef FEAT_LUA
737 {"luaeval", 1, 2, f_luaeval},
738#endif
739 {"map", 2, 2, f_map},
740 {"maparg", 1, 4, f_maparg},
741 {"mapcheck", 1, 3, f_mapcheck},
742 {"match", 2, 4, f_match},
743 {"matchadd", 2, 5, f_matchadd},
744 {"matchaddpos", 2, 5, f_matchaddpos},
745 {"matcharg", 1, 1, f_matcharg},
746 {"matchdelete", 1, 1, f_matchdelete},
747 {"matchend", 2, 4, f_matchend},
748 {"matchlist", 2, 4, f_matchlist},
749 {"matchstr", 2, 4, f_matchstr},
750 {"matchstrpos", 2, 4, f_matchstrpos},
751 {"max", 1, 1, f_max},
752 {"min", 1, 1, f_min},
753#ifdef vim_mkdir
754 {"mkdir", 1, 3, f_mkdir},
755#endif
756 {"mode", 0, 1, f_mode},
757#ifdef FEAT_MZSCHEME
758 {"mzeval", 1, 1, f_mzeval},
759#endif
760 {"nextnonblank", 1, 1, f_nextnonblank},
761 {"nr2char", 1, 2, f_nr2char},
762 {"or", 2, 2, f_or},
763 {"pathshorten", 1, 1, f_pathshorten},
764#ifdef FEAT_PERL
765 {"perleval", 1, 1, f_perleval},
766#endif
767#ifdef FEAT_FLOAT
768 {"pow", 2, 2, f_pow},
769#endif
770 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100771 {"printf", 1, 19, f_printf},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200772#ifdef FEAT_JOB_CHANNEL
773 {"prompt_setcallback", 2, 2, f_prompt_setcallback},
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200774 {"prompt_setinterrupt", 2, 2, f_prompt_setinterrupt},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200775 {"prompt_setprompt", 2, 2, f_prompt_setprompt},
776#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200777 {"pumvisible", 0, 0, f_pumvisible},
778#ifdef FEAT_PYTHON3
779 {"py3eval", 1, 1, f_py3eval},
780#endif
781#ifdef FEAT_PYTHON
782 {"pyeval", 1, 1, f_pyeval},
783#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100784#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
785 {"pyxeval", 1, 1, f_pyxeval},
786#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200787 {"range", 1, 3, f_range},
788 {"readfile", 1, 3, f_readfile},
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200789 {"reg_executing", 0, 0, f_reg_executing},
790 {"reg_recording", 0, 0, f_reg_recording},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200791 {"reltime", 0, 2, f_reltime},
792#ifdef FEAT_FLOAT
793 {"reltimefloat", 1, 1, f_reltimefloat},
794#endif
795 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100796 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200797 {"remote_foreground", 1, 1, f_remote_foreground},
798 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100799 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200800 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100801 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200802 {"remove", 2, 3, f_remove},
803 {"rename", 2, 2, f_rename},
804 {"repeat", 2, 2, f_repeat},
805 {"resolve", 1, 1, f_resolve},
806 {"reverse", 1, 1, f_reverse},
807#ifdef FEAT_FLOAT
808 {"round", 1, 1, f_round},
809#endif
810 {"screenattr", 2, 2, f_screenattr},
811 {"screenchar", 2, 2, f_screenchar},
812 {"screencol", 0, 0, f_screencol},
813 {"screenrow", 0, 0, f_screenrow},
814 {"search", 1, 4, f_search},
815 {"searchdecl", 1, 3, f_searchdecl},
816 {"searchpair", 3, 7, f_searchpair},
817 {"searchpairpos", 3, 7, f_searchpairpos},
818 {"searchpos", 1, 4, f_searchpos},
819 {"server2client", 2, 2, f_server2client},
820 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200821 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200822 {"setbufvar", 3, 3, f_setbufvar},
823 {"setcharsearch", 1, 1, f_setcharsearch},
824 {"setcmdpos", 1, 1, f_setcmdpos},
825 {"setfperm", 2, 2, f_setfperm},
826 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200827 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200828 {"setmatches", 1, 1, f_setmatches},
829 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200830 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200831 {"setreg", 2, 3, f_setreg},
832 {"settabvar", 3, 3, f_settabvar},
833 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100834 {"settagstack", 2, 3, f_settagstack},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200835 {"setwinvar", 3, 3, f_setwinvar},
836#ifdef FEAT_CRYPT
837 {"sha256", 1, 1, f_sha256},
838#endif
839 {"shellescape", 1, 2, f_shellescape},
840 {"shiftwidth", 0, 0, f_shiftwidth},
841 {"simplify", 1, 1, f_simplify},
842#ifdef FEAT_FLOAT
843 {"sin", 1, 1, f_sin},
844 {"sinh", 1, 1, f_sinh},
845#endif
846 {"sort", 1, 3, f_sort},
847 {"soundfold", 1, 1, f_soundfold},
848 {"spellbadword", 0, 1, f_spellbadword},
849 {"spellsuggest", 1, 3, f_spellsuggest},
850 {"split", 1, 3, f_split},
851#ifdef FEAT_FLOAT
852 {"sqrt", 1, 1, f_sqrt},
853 {"str2float", 1, 1, f_str2float},
854#endif
855 {"str2nr", 1, 2, f_str2nr},
856 {"strcharpart", 2, 3, f_strcharpart},
857 {"strchars", 1, 2, f_strchars},
858 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
859#ifdef HAVE_STRFTIME
860 {"strftime", 1, 2, f_strftime},
861#endif
862 {"strgetchar", 2, 2, f_strgetchar},
863 {"stridx", 2, 3, f_stridx},
864 {"string", 1, 1, f_string},
865 {"strlen", 1, 1, f_strlen},
866 {"strpart", 2, 3, f_strpart},
867 {"strridx", 2, 3, f_strridx},
868 {"strtrans", 1, 1, f_strtrans},
869 {"strwidth", 1, 1, f_strwidth},
870 {"submatch", 1, 2, f_submatch},
871 {"substitute", 4, 4, f_substitute},
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200872 {"swapinfo", 1, 1, f_swapinfo},
Bram Moolenaar110bd602018-09-16 18:46:59 +0200873 {"swapname", 1, 1, f_swapname},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200874 {"synID", 3, 3, f_synID},
875 {"synIDattr", 2, 3, f_synIDattr},
876 {"synIDtrans", 1, 1, f_synIDtrans},
877 {"synconcealed", 2, 2, f_synconcealed},
878 {"synstack", 2, 2, f_synstack},
879 {"system", 1, 2, f_system},
880 {"systemlist", 1, 2, f_systemlist},
881 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
882 {"tabpagenr", 0, 1, f_tabpagenr},
883 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
884 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100885 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200886#ifdef FEAT_FLOAT
887 {"tan", 1, 1, f_tan},
888 {"tanh", 1, 1, f_tanh},
889#endif
890 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200891#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100892 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
893 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100894 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200895 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200896# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
897 {"term_getansicolors", 1, 1, f_term_getansicolors},
898# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200899 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200900 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200901 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200902 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200903 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200904 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200905 {"term_getstatus", 1, 1, f_term_getstatus},
906 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200907 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200908 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200909 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200910 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200911# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
912 {"term_setansicolors", 2, 2, f_term_setansicolors},
913# endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100914 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100915 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaara42d3632018-04-14 17:05:38 +0200916 {"term_setsize", 3, 3, f_term_setsize},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200917 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200918 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200919#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200920 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
921 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200922 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200923 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100924 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200925#ifdef FEAT_JOB_CHANNEL
926 {"test_null_channel", 0, 0, f_test_null_channel},
927#endif
928 {"test_null_dict", 0, 0, f_test_null_dict},
929#ifdef FEAT_JOB_CHANNEL
930 {"test_null_job", 0, 0, f_test_null_job},
931#endif
932 {"test_null_list", 0, 0, f_test_null_list},
933 {"test_null_partial", 0, 0, f_test_null_partial},
934 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200935 {"test_option_not_set", 1, 1, f_test_option_not_set},
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100936 {"test_override", 2, 2, f_test_override},
Bram Moolenaarab186732018-09-14 21:27:06 +0200937#ifdef FEAT_GUI
938 {"test_scrollbar", 3, 3, f_test_scrollbar},
939#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200940 {"test_settime", 1, 1, f_test_settime},
941#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200942 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200943 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200944 {"timer_start", 2, 3, f_timer_start},
945 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200946 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200947#endif
948 {"tolower", 1, 1, f_tolower},
949 {"toupper", 1, 1, f_toupper},
950 {"tr", 3, 3, f_tr},
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100951 {"trim", 1, 2, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200952#ifdef FEAT_FLOAT
953 {"trunc", 1, 1, f_trunc},
954#endif
955 {"type", 1, 1, f_type},
956 {"undofile", 1, 1, f_undofile},
957 {"undotree", 0, 0, f_undotree},
958 {"uniq", 1, 3, f_uniq},
959 {"values", 1, 1, f_values},
960 {"virtcol", 1, 1, f_virtcol},
961 {"visualmode", 0, 1, f_visualmode},
962 {"wildmenumode", 0, 0, f_wildmenumode},
963 {"win_findbuf", 1, 1, f_win_findbuf},
964 {"win_getid", 0, 2, f_win_getid},
965 {"win_gotoid", 1, 1, f_win_gotoid},
966 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
967 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100968 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200969 {"winbufnr", 1, 1, f_winbufnr},
970 {"wincol", 0, 0, f_wincol},
971 {"winheight", 1, 1, f_winheight},
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200972 {"winlayout", 0, 1, f_winlayout},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200973 {"winline", 0, 0, f_winline},
974 {"winnr", 0, 1, f_winnr},
975 {"winrestcmd", 0, 0, f_winrestcmd},
976 {"winrestview", 1, 1, f_winrestview},
977 {"winsaveview", 0, 0, f_winsaveview},
978 {"winwidth", 1, 1, f_winwidth},
979 {"wordcount", 0, 0, f_wordcount},
980 {"writefile", 2, 3, f_writefile},
981 {"xor", 2, 2, f_xor},
982};
983
984#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
985
986/*
987 * Function given to ExpandGeneric() to obtain the list of internal
988 * or user defined function names.
989 */
990 char_u *
991get_function_name(expand_T *xp, int idx)
992{
993 static int intidx = -1;
994 char_u *name;
995
996 if (idx == 0)
997 intidx = -1;
998 if (intidx < 0)
999 {
1000 name = get_user_func_name(xp, idx);
1001 if (name != NULL)
1002 return name;
1003 }
1004 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
1005 {
1006 STRCPY(IObuff, functions[intidx].f_name);
1007 STRCAT(IObuff, "(");
1008 if (functions[intidx].f_max_argc == 0)
1009 STRCAT(IObuff, ")");
1010 return IObuff;
1011 }
1012
1013 return NULL;
1014}
1015
1016/*
1017 * Function given to ExpandGeneric() to obtain the list of internal or
1018 * user defined variable or function names.
1019 */
1020 char_u *
1021get_expr_name(expand_T *xp, int idx)
1022{
1023 static int intidx = -1;
1024 char_u *name;
1025
1026 if (idx == 0)
1027 intidx = -1;
1028 if (intidx < 0)
1029 {
1030 name = get_function_name(xp, idx);
1031 if (name != NULL)
1032 return name;
1033 }
1034 return get_user_var_name(xp, ++intidx);
1035}
1036
1037#endif /* FEAT_CMDL_COMPL */
1038
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001039/*
1040 * Find internal function in table above.
1041 * Return index, or -1 if not found
1042 */
1043 int
1044find_internal_func(
1045 char_u *name) /* name of the function */
1046{
1047 int first = 0;
1048 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
1049 int cmp;
1050 int x;
1051
1052 /*
1053 * Find the function name in the table. Binary search.
1054 */
1055 while (first <= last)
1056 {
1057 x = first + ((unsigned)(last - first) >> 1);
1058 cmp = STRCMP(name, functions[x].f_name);
1059 if (cmp < 0)
1060 last = x - 1;
1061 else if (cmp > 0)
1062 first = x + 1;
1063 else
1064 return x;
1065 }
1066 return -1;
1067}
1068
1069 int
1070call_internal_func(
1071 char_u *name,
1072 int argcount,
1073 typval_T *argvars,
1074 typval_T *rettv)
1075{
1076 int i;
1077
1078 i = find_internal_func(name);
1079 if (i < 0)
1080 return ERROR_UNKNOWN;
1081 if (argcount < functions[i].f_min_argc)
1082 return ERROR_TOOFEW;
1083 if (argcount > functions[i].f_max_argc)
1084 return ERROR_TOOMANY;
1085 argvars[argcount].v_type = VAR_UNKNOWN;
1086 functions[i].f_func(argvars, rettv);
1087 return ERROR_NONE;
1088}
1089
1090/*
1091 * Return TRUE for a non-zero Number and a non-empty String.
1092 */
1093 static int
1094non_zero_arg(typval_T *argvars)
1095{
1096 return ((argvars[0].v_type == VAR_NUMBER
1097 && argvars[0].vval.v_number != 0)
1098 || (argvars[0].v_type == VAR_SPECIAL
1099 && argvars[0].vval.v_number == VVAL_TRUE)
1100 || (argvars[0].v_type == VAR_STRING
1101 && argvars[0].vval.v_string != NULL
1102 && *argvars[0].vval.v_string != NUL));
1103}
1104
1105/*
1106 * Get the lnum from the first argument.
1107 * Also accepts ".", "$", etc., but that only works for the current buffer.
1108 * Returns -1 on error.
1109 */
1110 static linenr_T
1111get_tv_lnum(typval_T *argvars)
1112{
1113 typval_T rettv;
1114 linenr_T lnum;
1115
1116 lnum = (linenr_T)get_tv_number_chk(&argvars[0], NULL);
1117 if (lnum == 0) /* no valid number, try using line() */
1118 {
1119 rettv.v_type = VAR_NUMBER;
1120 f_line(argvars, &rettv);
1121 lnum = (linenr_T)rettv.vval.v_number;
1122 clear_tv(&rettv);
1123 }
1124 return lnum;
1125}
1126
1127#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001128/*
1129 * Get the float value of "argvars[0]" into "f".
1130 * Returns FAIL when the argument is not a Number or Float.
1131 */
1132 static int
1133get_float_arg(typval_T *argvars, float_T *f)
1134{
1135 if (argvars[0].v_type == VAR_FLOAT)
1136 {
1137 *f = argvars[0].vval.v_float;
1138 return OK;
1139 }
1140 if (argvars[0].v_type == VAR_NUMBER)
1141 {
1142 *f = (float_T)argvars[0].vval.v_number;
1143 return OK;
1144 }
1145 EMSG(_("E808: Number or Float required"));
1146 return FAIL;
1147}
1148
1149/*
1150 * "abs(expr)" function
1151 */
1152 static void
1153f_abs(typval_T *argvars, typval_T *rettv)
1154{
1155 if (argvars[0].v_type == VAR_FLOAT)
1156 {
1157 rettv->v_type = VAR_FLOAT;
1158 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1159 }
1160 else
1161 {
1162 varnumber_T n;
1163 int error = FALSE;
1164
1165 n = get_tv_number_chk(&argvars[0], &error);
1166 if (error)
1167 rettv->vval.v_number = -1;
1168 else if (n > 0)
1169 rettv->vval.v_number = n;
1170 else
1171 rettv->vval.v_number = -n;
1172 }
1173}
1174
1175/*
1176 * "acos()" function
1177 */
1178 static void
1179f_acos(typval_T *argvars, typval_T *rettv)
1180{
1181 float_T f = 0.0;
1182
1183 rettv->v_type = VAR_FLOAT;
1184 if (get_float_arg(argvars, &f) == OK)
1185 rettv->vval.v_float = acos(f);
1186 else
1187 rettv->vval.v_float = 0.0;
1188}
1189#endif
1190
1191/*
1192 * "add(list, item)" function
1193 */
1194 static void
1195f_add(typval_T *argvars, typval_T *rettv)
1196{
1197 list_T *l;
1198
1199 rettv->vval.v_number = 1; /* Default: Failed */
1200 if (argvars[0].v_type == VAR_LIST)
1201 {
1202 if ((l = argvars[0].vval.v_list) != NULL
1203 && !tv_check_lock(l->lv_lock,
1204 (char_u *)N_("add() argument"), TRUE)
1205 && list_append_tv(l, &argvars[1]) == OK)
1206 copy_tv(&argvars[0], rettv);
1207 }
1208 else
1209 EMSG(_(e_listreq));
1210}
1211
1212/*
1213 * "and(expr, expr)" function
1214 */
1215 static void
1216f_and(typval_T *argvars, typval_T *rettv)
1217{
1218 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
1219 & get_tv_number_chk(&argvars[1], NULL);
1220}
1221
1222/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001223 * Get the lnum from the first argument.
1224 * Also accepts "$", then "buf" is used.
1225 * Returns 0 on error.
1226 */
1227 static linenr_T
1228get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
1229{
1230 if (argvars[0].v_type == VAR_STRING
1231 && argvars[0].vval.v_string != NULL
1232 && argvars[0].vval.v_string[0] == '$'
1233 && buf != NULL)
1234 return buf->b_ml.ml_line_count;
1235 return (linenr_T)get_tv_number_chk(&argvars[0], NULL);
1236}
1237
1238/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02001239 * If there is a window for "curbuf", make it the current window.
1240 */
1241 static void
1242find_win_for_curbuf(void)
1243{
1244 wininfo_T *wip;
1245
1246 for (wip = curbuf->b_wininfo; wip != NULL; wip = wip->wi_next)
1247 {
1248 if (wip->wi_win != NULL)
1249 {
1250 curwin = wip->wi_win;
1251 break;
1252 }
1253 }
1254}
1255
1256/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001257 * Set line or list of lines in buffer "buf".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001258 */
1259 static void
Bram Moolenaarca851592018-06-06 21:04:07 +02001260set_buffer_lines(
1261 buf_T *buf,
1262 linenr_T lnum_arg,
1263 int append,
1264 typval_T *lines,
1265 typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001266{
Bram Moolenaarca851592018-06-06 21:04:07 +02001267 linenr_T lnum = lnum_arg + (append ? 1 : 0);
1268 char_u *line = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001269 list_T *l = NULL;
1270 listitem_T *li = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001271 long added = 0;
Bram Moolenaarca851592018-06-06 21:04:07 +02001272 linenr_T append_lnum;
1273 buf_T *curbuf_save = NULL;
1274 win_T *curwin_save = NULL;
1275 int is_curbuf = buf == curbuf;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001276
Bram Moolenaarca851592018-06-06 21:04:07 +02001277 /* When using the current buffer ml_mfp will be set if needed. Useful when
1278 * setline() is used on startup. For other buffers the buffer must be
1279 * loaded. */
1280 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001281 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001282 rettv->vval.v_number = 1; /* FAIL */
1283 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001284 }
1285
Bram Moolenaarca851592018-06-06 21:04:07 +02001286 if (!is_curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001287 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001288 curbuf_save = curbuf;
1289 curwin_save = curwin;
1290 curbuf = buf;
Bram Moolenaard79a2622018-06-07 18:17:46 +02001291 find_win_for_curbuf();
Bram Moolenaarca851592018-06-06 21:04:07 +02001292 }
1293
1294 if (append)
1295 // appendbufline() uses the line number below which we insert
1296 append_lnum = lnum - 1;
1297 else
1298 // setbufline() uses the line number above which we insert, we only
1299 // append if it's below the last line
1300 append_lnum = curbuf->b_ml.ml_line_count;
1301
1302 if (lines->v_type == VAR_LIST)
1303 {
1304 l = lines->vval.v_list;
1305 li = l->lv_first;
1306 }
1307 else
1308 line = get_tv_string_chk(lines);
1309
1310 /* default result is zero == OK */
1311 for (;;)
1312 {
1313 if (l != NULL)
1314 {
1315 /* list argument, get next string */
1316 if (li == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001317 break;
Bram Moolenaarca851592018-06-06 21:04:07 +02001318 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001319 li = li->li_next;
1320 }
1321
Bram Moolenaarca851592018-06-06 21:04:07 +02001322 rettv->vval.v_number = 1; /* FAIL */
1323 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
1324 break;
1325
1326 /* When coming here from Insert mode, sync undo, so that this can be
1327 * undone separately from what was previously inserted. */
1328 if (u_sync_once == 2)
1329 {
1330 u_sync_once = 1; /* notify that u_sync() was called */
1331 u_sync(TRUE);
1332 }
1333
1334 if (!append && lnum <= curbuf->b_ml.ml_line_count)
1335 {
1336 /* existing line, replace it */
1337 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
1338 {
1339 changed_bytes(lnum, 0);
1340 if (is_curbuf && lnum == curwin->w_cursor.lnum)
1341 check_cursor_col();
1342 rettv->vval.v_number = 0; /* OK */
1343 }
1344 }
1345 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
1346 {
1347 /* append the line */
1348 ++added;
1349 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
1350 rettv->vval.v_number = 0; /* OK */
1351 }
1352
1353 if (l == NULL) /* only one string argument */
1354 break;
1355 ++lnum;
1356 }
1357
1358 if (added > 0)
1359 {
1360 win_T *wp;
1361 tabpage_T *tp;
1362
1363 appended_lines_mark(append_lnum, added);
1364 FOR_ALL_TAB_WINDOWS(tp, wp)
1365 if (wp->w_buffer == buf && wp->w_cursor.lnum > append_lnum)
1366 wp->w_cursor.lnum += added;
1367 check_cursor_col();
1368
Bram Moolenaarf2732452018-06-03 14:47:35 +02001369#ifdef FEAT_JOB_CHANNEL
1370 if (bt_prompt(curbuf) && (State & INSERT))
1371 // show the line with the prompt
1372 update_topline();
1373#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001374 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001375
1376 if (!is_curbuf)
1377 {
1378 curbuf = curbuf_save;
1379 curwin = curwin_save;
1380 }
1381}
1382
1383/*
1384 * "append(lnum, string/list)" function
1385 */
1386 static void
1387f_append(typval_T *argvars, typval_T *rettv)
1388{
1389 linenr_T lnum = get_tv_lnum(&argvars[0]);
1390
1391 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1392}
1393
1394/*
1395 * "appendbufline(buf, lnum, string/list)" function
1396 */
1397 static void
1398f_appendbufline(typval_T *argvars, typval_T *rettv)
1399{
1400 linenr_T lnum;
1401 buf_T *buf;
1402
1403 buf = get_buf_tv(&argvars[0], FALSE);
1404 if (buf == NULL)
1405 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001406 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001407 {
1408 lnum = get_tv_lnum_buf(&argvars[1], buf);
1409 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1410 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001411}
1412
1413/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001414 * "argc([window id])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001415 */
1416 static void
Bram Moolenaare6e39892018-10-25 12:32:11 +02001417f_argc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001418{
Bram Moolenaare6e39892018-10-25 12:32:11 +02001419 win_T *wp;
1420
1421 if (argvars[0].v_type == VAR_UNKNOWN)
1422 // use the current window
1423 rettv->vval.v_number = ARGCOUNT;
1424 else if (argvars[0].v_type == VAR_NUMBER
1425 && get_tv_number(&argvars[0]) == -1)
1426 // use the global argument list
1427 rettv->vval.v_number = GARGCOUNT;
1428 else
1429 {
1430 // use the argument list of the specified window
1431 wp = find_win_by_nr_or_id(&argvars[0]);
1432 if (wp != NULL)
1433 rettv->vval.v_number = WARGCOUNT(wp);
1434 else
1435 rettv->vval.v_number = -1;
1436 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001437}
1438
1439/*
1440 * "argidx()" function
1441 */
1442 static void
1443f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1444{
1445 rettv->vval.v_number = curwin->w_arg_idx;
1446}
1447
1448/*
1449 * "arglistid()" function
1450 */
1451 static void
1452f_arglistid(typval_T *argvars, typval_T *rettv)
1453{
1454 win_T *wp;
1455
1456 rettv->vval.v_number = -1;
1457 wp = find_tabwin(&argvars[0], &argvars[1]);
1458 if (wp != NULL)
1459 rettv->vval.v_number = wp->w_alist->id;
1460}
1461
1462/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001463 * Get the argument list for a given window
1464 */
1465 static void
1466get_arglist_as_rettv(aentry_T *arglist, int argcount, typval_T *rettv)
1467{
1468 int idx;
1469
1470 if (rettv_list_alloc(rettv) == OK && arglist != NULL)
1471 for (idx = 0; idx < argcount; ++idx)
1472 list_append_string(rettv->vval.v_list,
1473 alist_name(&arglist[idx]), -1);
1474}
1475
1476/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001477 * "argv(nr)" function
1478 */
1479 static void
1480f_argv(typval_T *argvars, typval_T *rettv)
1481{
1482 int idx;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001483 aentry_T *arglist = NULL;
1484 int argcount = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001485
1486 if (argvars[0].v_type != VAR_UNKNOWN)
1487 {
Bram Moolenaare6e39892018-10-25 12:32:11 +02001488 if (argvars[1].v_type == VAR_UNKNOWN)
1489 {
1490 arglist = ARGLIST;
1491 argcount = ARGCOUNT;
1492 }
1493 else if (argvars[1].v_type == VAR_NUMBER
1494 && get_tv_number(&argvars[1]) == -1)
1495 {
1496 arglist = GARGLIST;
1497 argcount = GARGCOUNT;
1498 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001499 else
Bram Moolenaare6e39892018-10-25 12:32:11 +02001500 {
1501 win_T *wp = find_win_by_nr_or_id(&argvars[1]);
1502
1503 if (wp != NULL)
1504 {
1505 /* Use the argument list of the specified window */
1506 arglist = WARGLIST(wp);
1507 argcount = WARGCOUNT(wp);
1508 }
1509 }
1510
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001511 rettv->v_type = VAR_STRING;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001512 rettv->vval.v_string = NULL;
1513 idx = get_tv_number_chk(&argvars[0], NULL);
1514 if (arglist != NULL && idx >= 0 && idx < argcount)
1515 rettv->vval.v_string = vim_strsave(alist_name(&arglist[idx]));
1516 else if (idx == -1)
1517 get_arglist_as_rettv(arglist, argcount, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001518 }
Bram Moolenaare6e39892018-10-25 12:32:11 +02001519 else
1520 get_arglist_as_rettv(ARGLIST, ARGCOUNT, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001521}
1522
1523/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001524 * "assert_beeps(cmd [, error])" function
1525 */
1526 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001527f_assert_beeps(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001528{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001529 rettv->vval.v_number = assert_beeps(argvars);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001530}
1531
1532/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001533 * "assert_equal(expected, actual[, msg])" function
1534 */
1535 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001536f_assert_equal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001537{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001538 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_EQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001539}
1540
1541/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01001542 * "assert_equalfile(fname-one, fname-two)" function
1543 */
1544 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001545f_assert_equalfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaard96ff162018-02-18 22:13:29 +01001546{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001547 rettv->vval.v_number = assert_equalfile(argvars);
Bram Moolenaard96ff162018-02-18 22:13:29 +01001548}
1549
1550/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001551 * "assert_notequal(expected, actual[, msg])" function
1552 */
1553 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001554f_assert_notequal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001555{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001556 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001557}
1558
1559/*
1560 * "assert_exception(string[, msg])" function
1561 */
1562 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001563f_assert_exception(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001564{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001565 rettv->vval.v_number = assert_exception(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001566}
1567
1568/*
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02001569 * "assert_fails(cmd [, error[, msg]])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001570 */
1571 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001572f_assert_fails(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001573{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001574 rettv->vval.v_number = assert_fails(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001575}
1576
1577/*
1578 * "assert_false(actual[, msg])" function
1579 */
1580 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001581f_assert_false(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001582{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001583 rettv->vval.v_number = assert_bool(argvars, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001584}
1585
1586/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001587 * "assert_inrange(lower, upper[, msg])" function
1588 */
1589 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001590f_assert_inrange(typval_T *argvars, typval_T *rettv)
Bram Moolenaar61c04492016-07-23 15:35:35 +02001591{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001592 rettv->vval.v_number = assert_inrange(argvars);
Bram Moolenaar61c04492016-07-23 15:35:35 +02001593}
1594
1595/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001596 * "assert_match(pattern, actual[, msg])" function
1597 */
1598 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001599f_assert_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001600{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001601 rettv->vval.v_number = assert_match_common(argvars, ASSERT_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001602}
1603
1604/*
1605 * "assert_notmatch(pattern, actual[, msg])" function
1606 */
1607 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001608f_assert_notmatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001609{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001610 rettv->vval.v_number = assert_match_common(argvars, ASSERT_NOTMATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001611}
1612
1613/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001614 * "assert_report(msg)" function
1615 */
1616 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001617f_assert_report(typval_T *argvars, typval_T *rettv)
Bram Moolenaar42205552017-03-18 19:42:22 +01001618{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001619 rettv->vval.v_number = assert_report(argvars);
Bram Moolenaar42205552017-03-18 19:42:22 +01001620}
1621
1622/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001623 * "assert_true(actual[, msg])" function
1624 */
1625 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001626f_assert_true(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001627{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001628 rettv->vval.v_number = assert_bool(argvars, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001629}
1630
1631#ifdef FEAT_FLOAT
1632/*
1633 * "asin()" function
1634 */
1635 static void
1636f_asin(typval_T *argvars, typval_T *rettv)
1637{
1638 float_T f = 0.0;
1639
1640 rettv->v_type = VAR_FLOAT;
1641 if (get_float_arg(argvars, &f) == OK)
1642 rettv->vval.v_float = asin(f);
1643 else
1644 rettv->vval.v_float = 0.0;
1645}
1646
1647/*
1648 * "atan()" function
1649 */
1650 static void
1651f_atan(typval_T *argvars, typval_T *rettv)
1652{
1653 float_T f = 0.0;
1654
1655 rettv->v_type = VAR_FLOAT;
1656 if (get_float_arg(argvars, &f) == OK)
1657 rettv->vval.v_float = atan(f);
1658 else
1659 rettv->vval.v_float = 0.0;
1660}
1661
1662/*
1663 * "atan2()" function
1664 */
1665 static void
1666f_atan2(typval_T *argvars, typval_T *rettv)
1667{
1668 float_T fx = 0.0, fy = 0.0;
1669
1670 rettv->v_type = VAR_FLOAT;
1671 if (get_float_arg(argvars, &fx) == OK
1672 && get_float_arg(&argvars[1], &fy) == OK)
1673 rettv->vval.v_float = atan2(fx, fy);
1674 else
1675 rettv->vval.v_float = 0.0;
1676}
1677#endif
1678
1679/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001680 * "balloon_show()" function
1681 */
1682#ifdef FEAT_BEVAL
1683 static void
1684f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1685{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001686 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001687 {
1688 if (argvars[0].v_type == VAR_LIST
1689# ifdef FEAT_GUI
1690 && !gui.in_use
1691# endif
1692 )
1693 post_balloon(balloonEval, NULL, argvars[0].vval.v_list);
1694 else
1695 post_balloon(balloonEval, get_tv_string_chk(&argvars[0]), NULL);
1696 }
1697}
1698
Bram Moolenaar669a8282017-11-19 20:13:05 +01001699# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001700 static void
1701f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1702{
1703 if (rettv_list_alloc(rettv) == OK)
1704 {
1705 char_u *msg = get_tv_string_chk(&argvars[0]);
1706
1707 if (msg != NULL)
1708 {
1709 pumitem_T *array;
1710 int size = split_message(msg, &array);
1711 int i;
1712
1713 /* Skip the first and last item, they are always empty. */
1714 for (i = 1; i < size - 1; ++i)
1715 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001716 while (size > 0)
1717 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001718 vim_free(array);
1719 }
1720 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001721}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001722# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001723#endif
1724
1725/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001726 * "browse(save, title, initdir, default)" function
1727 */
1728 static void
1729f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1730{
1731#ifdef FEAT_BROWSE
1732 int save;
1733 char_u *title;
1734 char_u *initdir;
1735 char_u *defname;
1736 char_u buf[NUMBUFLEN];
1737 char_u buf2[NUMBUFLEN];
1738 int error = FALSE;
1739
1740 save = (int)get_tv_number_chk(&argvars[0], &error);
1741 title = get_tv_string_chk(&argvars[1]);
1742 initdir = get_tv_string_buf_chk(&argvars[2], buf);
1743 defname = get_tv_string_buf_chk(&argvars[3], buf2);
1744
1745 if (error || title == NULL || initdir == NULL || defname == NULL)
1746 rettv->vval.v_string = NULL;
1747 else
1748 rettv->vval.v_string =
1749 do_browse(save ? BROWSE_SAVE : 0,
1750 title, defname, NULL, initdir, NULL, curbuf);
1751#else
1752 rettv->vval.v_string = NULL;
1753#endif
1754 rettv->v_type = VAR_STRING;
1755}
1756
1757/*
1758 * "browsedir(title, initdir)" function
1759 */
1760 static void
1761f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1762{
1763#ifdef FEAT_BROWSE
1764 char_u *title;
1765 char_u *initdir;
1766 char_u buf[NUMBUFLEN];
1767
1768 title = get_tv_string_chk(&argvars[0]);
1769 initdir = get_tv_string_buf_chk(&argvars[1], buf);
1770
1771 if (title == NULL || initdir == NULL)
1772 rettv->vval.v_string = NULL;
1773 else
1774 rettv->vval.v_string = do_browse(BROWSE_DIR,
1775 title, NULL, NULL, initdir, NULL, curbuf);
1776#else
1777 rettv->vval.v_string = NULL;
1778#endif
1779 rettv->v_type = VAR_STRING;
1780}
1781
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001782/*
1783 * Find a buffer by number or exact name.
1784 */
1785 static buf_T *
1786find_buffer(typval_T *avar)
1787{
1788 buf_T *buf = NULL;
1789
1790 if (avar->v_type == VAR_NUMBER)
1791 buf = buflist_findnr((int)avar->vval.v_number);
1792 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1793 {
1794 buf = buflist_findname_exp(avar->vval.v_string);
1795 if (buf == NULL)
1796 {
1797 /* No full path name match, try a match with a URL or a "nofile"
1798 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001799 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001800 if (buf->b_fname != NULL
1801 && (path_with_url(buf->b_fname)
1802#ifdef FEAT_QUICKFIX
1803 || bt_nofile(buf)
1804#endif
1805 )
1806 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1807 break;
1808 }
1809 }
1810 return buf;
1811}
1812
1813/*
1814 * "bufexists(expr)" function
1815 */
1816 static void
1817f_bufexists(typval_T *argvars, typval_T *rettv)
1818{
1819 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1820}
1821
1822/*
1823 * "buflisted(expr)" function
1824 */
1825 static void
1826f_buflisted(typval_T *argvars, typval_T *rettv)
1827{
1828 buf_T *buf;
1829
1830 buf = find_buffer(&argvars[0]);
1831 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1832}
1833
1834/*
1835 * "bufloaded(expr)" function
1836 */
1837 static void
1838f_bufloaded(typval_T *argvars, typval_T *rettv)
1839{
1840 buf_T *buf;
1841
1842 buf = find_buffer(&argvars[0]);
1843 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1844}
1845
1846 buf_T *
1847buflist_find_by_name(char_u *name, int curtab_only)
1848{
1849 int save_magic;
1850 char_u *save_cpo;
1851 buf_T *buf;
1852
1853 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1854 save_magic = p_magic;
1855 p_magic = TRUE;
1856 save_cpo = p_cpo;
1857 p_cpo = (char_u *)"";
1858
1859 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1860 TRUE, FALSE, curtab_only));
1861
1862 p_magic = save_magic;
1863 p_cpo = save_cpo;
1864 return buf;
1865}
1866
1867/*
1868 * Get buffer by number or pattern.
1869 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001870 buf_T *
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001871get_buf_tv(typval_T *tv, int curtab_only)
1872{
1873 char_u *name = tv->vval.v_string;
1874 buf_T *buf;
1875
1876 if (tv->v_type == VAR_NUMBER)
1877 return buflist_findnr((int)tv->vval.v_number);
1878 if (tv->v_type != VAR_STRING)
1879 return NULL;
1880 if (name == NULL || *name == NUL)
1881 return curbuf;
1882 if (name[0] == '$' && name[1] == NUL)
1883 return lastbuf;
1884
1885 buf = buflist_find_by_name(name, curtab_only);
1886
1887 /* If not found, try expanding the name, like done for bufexists(). */
1888 if (buf == NULL)
1889 buf = find_buffer(tv);
1890
1891 return buf;
1892}
1893
1894/*
1895 * "bufname(expr)" function
1896 */
1897 static void
1898f_bufname(typval_T *argvars, typval_T *rettv)
1899{
1900 buf_T *buf;
1901
1902 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1903 ++emsg_off;
1904 buf = get_buf_tv(&argvars[0], FALSE);
1905 rettv->v_type = VAR_STRING;
1906 if (buf != NULL && buf->b_fname != NULL)
1907 rettv->vval.v_string = vim_strsave(buf->b_fname);
1908 else
1909 rettv->vval.v_string = NULL;
1910 --emsg_off;
1911}
1912
1913/*
1914 * "bufnr(expr)" function
1915 */
1916 static void
1917f_bufnr(typval_T *argvars, typval_T *rettv)
1918{
1919 buf_T *buf;
1920 int error = FALSE;
1921 char_u *name;
1922
1923 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1924 ++emsg_off;
1925 buf = get_buf_tv(&argvars[0], FALSE);
1926 --emsg_off;
1927
1928 /* If the buffer isn't found and the second argument is not zero create a
1929 * new buffer. */
1930 if (buf == NULL
1931 && argvars[1].v_type != VAR_UNKNOWN
1932 && get_tv_number_chk(&argvars[1], &error) != 0
1933 && !error
1934 && (name = get_tv_string_chk(&argvars[0])) != NULL
1935 && !error)
1936 buf = buflist_new(name, NULL, (linenr_T)1, 0);
1937
1938 if (buf != NULL)
1939 rettv->vval.v_number = buf->b_fnum;
1940 else
1941 rettv->vval.v_number = -1;
1942}
1943
1944 static void
1945buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
1946{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001947 win_T *wp;
1948 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001949 buf_T *buf;
1950
1951 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1952 ++emsg_off;
1953 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02001954 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001955 {
1956 ++winnr;
1957 if (wp->w_buffer == buf)
1958 break;
1959 }
1960 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001961 --emsg_off;
1962}
1963
1964/*
1965 * "bufwinid(nr)" function
1966 */
1967 static void
1968f_bufwinid(typval_T *argvars, typval_T *rettv)
1969{
1970 buf_win_common(argvars, rettv, FALSE);
1971}
1972
1973/*
1974 * "bufwinnr(nr)" function
1975 */
1976 static void
1977f_bufwinnr(typval_T *argvars, typval_T *rettv)
1978{
1979 buf_win_common(argvars, rettv, TRUE);
1980}
1981
1982/*
1983 * "byte2line(byte)" function
1984 */
1985 static void
1986f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
1987{
1988#ifndef FEAT_BYTEOFF
1989 rettv->vval.v_number = -1;
1990#else
1991 long boff = 0;
1992
1993 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
1994 if (boff < 0)
1995 rettv->vval.v_number = -1;
1996 else
1997 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
1998 (linenr_T)0, &boff);
1999#endif
2000}
2001
2002 static void
2003byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2004{
2005#ifdef FEAT_MBYTE
2006 char_u *t;
2007#endif
2008 char_u *str;
2009 varnumber_T idx;
2010
2011 str = get_tv_string_chk(&argvars[0]);
2012 idx = get_tv_number_chk(&argvars[1], NULL);
2013 rettv->vval.v_number = -1;
2014 if (str == NULL || idx < 0)
2015 return;
2016
2017#ifdef FEAT_MBYTE
2018 t = str;
2019 for ( ; idx > 0; idx--)
2020 {
2021 if (*t == NUL) /* EOL reached */
2022 return;
2023 if (enc_utf8 && comp)
2024 t += utf_ptr2len(t);
2025 else
2026 t += (*mb_ptr2len)(t);
2027 }
2028 rettv->vval.v_number = (varnumber_T)(t - str);
2029#else
2030 if ((size_t)idx <= STRLEN(str))
2031 rettv->vval.v_number = idx;
2032#endif
2033}
2034
2035/*
2036 * "byteidx()" function
2037 */
2038 static void
2039f_byteidx(typval_T *argvars, typval_T *rettv)
2040{
2041 byteidx(argvars, rettv, FALSE);
2042}
2043
2044/*
2045 * "byteidxcomp()" function
2046 */
2047 static void
2048f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2049{
2050 byteidx(argvars, rettv, TRUE);
2051}
2052
2053/*
2054 * "call(func, arglist [, dict])" function
2055 */
2056 static void
2057f_call(typval_T *argvars, typval_T *rettv)
2058{
2059 char_u *func;
2060 partial_T *partial = NULL;
2061 dict_T *selfdict = NULL;
2062
2063 if (argvars[1].v_type != VAR_LIST)
2064 {
2065 EMSG(_(e_listreq));
2066 return;
2067 }
2068 if (argvars[1].vval.v_list == NULL)
2069 return;
2070
2071 if (argvars[0].v_type == VAR_FUNC)
2072 func = argvars[0].vval.v_string;
2073 else if (argvars[0].v_type == VAR_PARTIAL)
2074 {
2075 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002076 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002077 }
2078 else
2079 func = get_tv_string(&argvars[0]);
2080 if (*func == NUL)
2081 return; /* type error or empty name */
2082
2083 if (argvars[2].v_type != VAR_UNKNOWN)
2084 {
2085 if (argvars[2].v_type != VAR_DICT)
2086 {
2087 EMSG(_(e_dictreq));
2088 return;
2089 }
2090 selfdict = argvars[2].vval.v_dict;
2091 }
2092
2093 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2094}
2095
2096#ifdef FEAT_FLOAT
2097/*
2098 * "ceil({float})" function
2099 */
2100 static void
2101f_ceil(typval_T *argvars, typval_T *rettv)
2102{
2103 float_T f = 0.0;
2104
2105 rettv->v_type = VAR_FLOAT;
2106 if (get_float_arg(argvars, &f) == OK)
2107 rettv->vval.v_float = ceil(f);
2108 else
2109 rettv->vval.v_float = 0.0;
2110}
2111#endif
2112
2113#ifdef FEAT_JOB_CHANNEL
2114/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002115 * "ch_canread()" function
2116 */
2117 static void
2118f_ch_canread(typval_T *argvars, typval_T *rettv)
2119{
Bram Moolenaar958dc692016-12-01 15:34:12 +01002120 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002121
2122 rettv->vval.v_number = 0;
2123 if (channel != NULL)
2124 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
2125 || channel_has_readahead(channel, PART_OUT)
2126 || channel_has_readahead(channel, PART_ERR);
2127}
2128
2129/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002130 * "ch_close()" function
2131 */
2132 static void
2133f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
2134{
2135 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2136
2137 if (channel != NULL)
2138 {
2139 channel_close(channel, FALSE);
2140 channel_clear(channel);
2141 }
2142}
2143
2144/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002145 * "ch_close()" function
2146 */
2147 static void
2148f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
2149{
2150 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2151
2152 if (channel != NULL)
2153 channel_close_in(channel);
2154}
2155
2156/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002157 * "ch_getbufnr()" function
2158 */
2159 static void
2160f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
2161{
2162 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2163
2164 rettv->vval.v_number = -1;
2165 if (channel != NULL)
2166 {
2167 char_u *what = get_tv_string(&argvars[1]);
2168 int part;
2169
2170 if (STRCMP(what, "err") == 0)
2171 part = PART_ERR;
2172 else if (STRCMP(what, "out") == 0)
2173 part = PART_OUT;
2174 else if (STRCMP(what, "in") == 0)
2175 part = PART_IN;
2176 else
2177 part = PART_SOCK;
2178 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
2179 rettv->vval.v_number =
2180 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
2181 }
2182}
2183
2184/*
2185 * "ch_getjob()" function
2186 */
2187 static void
2188f_ch_getjob(typval_T *argvars, typval_T *rettv)
2189{
2190 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2191
2192 if (channel != NULL)
2193 {
2194 rettv->v_type = VAR_JOB;
2195 rettv->vval.v_job = channel->ch_job;
2196 if (channel->ch_job != NULL)
2197 ++channel->ch_job->jv_refcount;
2198 }
2199}
2200
2201/*
2202 * "ch_info()" function
2203 */
2204 static void
2205f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
2206{
2207 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2208
2209 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
2210 channel_info(channel, rettv->vval.v_dict);
2211}
2212
2213/*
2214 * "ch_log()" function
2215 */
2216 static void
2217f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
2218{
2219 char_u *msg = get_tv_string(&argvars[0]);
2220 channel_T *channel = NULL;
2221
2222 if (argvars[1].v_type != VAR_UNKNOWN)
2223 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
2224
Bram Moolenaard5359b22018-04-05 22:44:39 +02002225 ch_log(channel, "%s", msg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002226}
2227
2228/*
2229 * "ch_logfile()" function
2230 */
2231 static void
2232f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
2233{
2234 char_u *fname;
2235 char_u *opt = (char_u *)"";
2236 char_u buf[NUMBUFLEN];
2237
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02002238 /* Don't open a file in restricted mode. */
2239 if (check_restricted() || check_secure())
2240 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002241 fname = get_tv_string(&argvars[0]);
2242 if (argvars[1].v_type == VAR_STRING)
2243 opt = get_tv_string_buf(&argvars[1], buf);
2244 ch_logfile(fname, opt);
2245}
2246
2247/*
2248 * "ch_open()" function
2249 */
2250 static void
2251f_ch_open(typval_T *argvars, typval_T *rettv)
2252{
2253 rettv->v_type = VAR_CHANNEL;
2254 if (check_restricted() || check_secure())
2255 return;
2256 rettv->vval.v_channel = channel_open_func(argvars);
2257}
2258
2259/*
2260 * "ch_read()" function
2261 */
2262 static void
2263f_ch_read(typval_T *argvars, typval_T *rettv)
2264{
2265 common_channel_read(argvars, rettv, FALSE);
2266}
2267
2268/*
2269 * "ch_readraw()" function
2270 */
2271 static void
2272f_ch_readraw(typval_T *argvars, typval_T *rettv)
2273{
2274 common_channel_read(argvars, rettv, TRUE);
2275}
2276
2277/*
2278 * "ch_evalexpr()" function
2279 */
2280 static void
2281f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2282{
2283 ch_expr_common(argvars, rettv, TRUE);
2284}
2285
2286/*
2287 * "ch_sendexpr()" function
2288 */
2289 static void
2290f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2291{
2292 ch_expr_common(argvars, rettv, FALSE);
2293}
2294
2295/*
2296 * "ch_evalraw()" function
2297 */
2298 static void
2299f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2300{
2301 ch_raw_common(argvars, rettv, TRUE);
2302}
2303
2304/*
2305 * "ch_sendraw()" function
2306 */
2307 static void
2308f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2309{
2310 ch_raw_common(argvars, rettv, FALSE);
2311}
2312
2313/*
2314 * "ch_setoptions()" function
2315 */
2316 static void
2317f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2318{
2319 channel_T *channel;
2320 jobopt_T opt;
2321
2322 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2323 if (channel == NULL)
2324 return;
2325 clear_job_options(&opt);
2326 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002327 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002328 channel_set_options(channel, &opt);
2329 free_job_options(&opt);
2330}
2331
2332/*
2333 * "ch_status()" function
2334 */
2335 static void
2336f_ch_status(typval_T *argvars, typval_T *rettv)
2337{
2338 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002339 jobopt_T opt;
2340 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002341
2342 /* return an empty string by default */
2343 rettv->v_type = VAR_STRING;
2344 rettv->vval.v_string = NULL;
2345
2346 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002347
2348 if (argvars[1].v_type != VAR_UNKNOWN)
2349 {
2350 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002351 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002352 && (opt.jo_set & JO_PART))
2353 part = opt.jo_part;
2354 }
2355
2356 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002357}
2358#endif
2359
2360/*
2361 * "changenr()" function
2362 */
2363 static void
2364f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2365{
2366 rettv->vval.v_number = curbuf->b_u_seq_cur;
2367}
2368
2369/*
2370 * "char2nr(string)" function
2371 */
2372 static void
2373f_char2nr(typval_T *argvars, typval_T *rettv)
2374{
2375#ifdef FEAT_MBYTE
2376 if (has_mbyte)
2377 {
2378 int utf8 = 0;
2379
2380 if (argvars[1].v_type != VAR_UNKNOWN)
2381 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
2382
2383 if (utf8)
2384 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
2385 else
2386 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
2387 }
2388 else
2389#endif
2390 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
2391}
2392
2393/*
2394 * "cindent(lnum)" function
2395 */
2396 static void
2397f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2398{
2399#ifdef FEAT_CINDENT
2400 pos_T pos;
2401 linenr_T lnum;
2402
2403 pos = curwin->w_cursor;
2404 lnum = get_tv_lnum(argvars);
2405 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2406 {
2407 curwin->w_cursor.lnum = lnum;
2408 rettv->vval.v_number = get_c_indent();
2409 curwin->w_cursor = pos;
2410 }
2411 else
2412#endif
2413 rettv->vval.v_number = -1;
2414}
2415
2416/*
2417 * "clearmatches()" function
2418 */
2419 static void
2420f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2421{
2422#ifdef FEAT_SEARCH_EXTRA
2423 clear_matches(curwin);
2424#endif
2425}
2426
2427/*
2428 * "col(string)" function
2429 */
2430 static void
2431f_col(typval_T *argvars, typval_T *rettv)
2432{
2433 colnr_T col = 0;
2434 pos_T *fp;
2435 int fnum = curbuf->b_fnum;
2436
2437 fp = var2fpos(&argvars[0], FALSE, &fnum);
2438 if (fp != NULL && fnum == curbuf->b_fnum)
2439 {
2440 if (fp->col == MAXCOL)
2441 {
2442 /* '> can be MAXCOL, get the length of the line then */
2443 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2444 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2445 else
2446 col = MAXCOL;
2447 }
2448 else
2449 {
2450 col = fp->col + 1;
2451#ifdef FEAT_VIRTUALEDIT
2452 /* col(".") when the cursor is on the NUL at the end of the line
2453 * because of "coladd" can be seen as an extra column. */
2454 if (virtual_active() && fp == &curwin->w_cursor)
2455 {
2456 char_u *p = ml_get_cursor();
2457
2458 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2459 curwin->w_virtcol - curwin->w_cursor.coladd))
2460 {
2461# ifdef FEAT_MBYTE
2462 int l;
2463
2464 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2465 col += l;
2466# else
2467 if (*p != NUL && p[1] == NUL)
2468 ++col;
2469# endif
2470 }
2471 }
2472#endif
2473 }
2474 }
2475 rettv->vval.v_number = col;
2476}
2477
2478#if defined(FEAT_INS_EXPAND)
2479/*
2480 * "complete()" function
2481 */
2482 static void
2483f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2484{
2485 int startcol;
2486
2487 if ((State & INSERT) == 0)
2488 {
2489 EMSG(_("E785: complete() can only be used in Insert mode"));
2490 return;
2491 }
2492
2493 /* Check for undo allowed here, because if something was already inserted
2494 * the line was already saved for undo and this check isn't done. */
2495 if (!undo_allowed())
2496 return;
2497
2498 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2499 {
2500 EMSG(_(e_invarg));
2501 return;
2502 }
2503
2504 startcol = (int)get_tv_number_chk(&argvars[0], NULL);
2505 if (startcol <= 0)
2506 return;
2507
2508 set_completion(startcol - 1, argvars[1].vval.v_list);
2509}
2510
2511/*
2512 * "complete_add()" function
2513 */
2514 static void
2515f_complete_add(typval_T *argvars, typval_T *rettv)
2516{
2517 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2518}
2519
2520/*
2521 * "complete_check()" function
2522 */
2523 static void
2524f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2525{
2526 int saved = RedrawingDisabled;
2527
2528 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002529 ins_compl_check_keys(0, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002530 rettv->vval.v_number = compl_interrupted;
2531 RedrawingDisabled = saved;
2532}
2533#endif
2534
2535/*
2536 * "confirm(message, buttons[, default [, type]])" function
2537 */
2538 static void
2539f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2540{
2541#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2542 char_u *message;
2543 char_u *buttons = NULL;
2544 char_u buf[NUMBUFLEN];
2545 char_u buf2[NUMBUFLEN];
2546 int def = 1;
2547 int type = VIM_GENERIC;
2548 char_u *typestr;
2549 int error = FALSE;
2550
2551 message = get_tv_string_chk(&argvars[0]);
2552 if (message == NULL)
2553 error = TRUE;
2554 if (argvars[1].v_type != VAR_UNKNOWN)
2555 {
2556 buttons = get_tv_string_buf_chk(&argvars[1], buf);
2557 if (buttons == NULL)
2558 error = TRUE;
2559 if (argvars[2].v_type != VAR_UNKNOWN)
2560 {
2561 def = (int)get_tv_number_chk(&argvars[2], &error);
2562 if (argvars[3].v_type != VAR_UNKNOWN)
2563 {
2564 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
2565 if (typestr == NULL)
2566 error = TRUE;
2567 else
2568 {
2569 switch (TOUPPER_ASC(*typestr))
2570 {
2571 case 'E': type = VIM_ERROR; break;
2572 case 'Q': type = VIM_QUESTION; break;
2573 case 'I': type = VIM_INFO; break;
2574 case 'W': type = VIM_WARNING; break;
2575 case 'G': type = VIM_GENERIC; break;
2576 }
2577 }
2578 }
2579 }
2580 }
2581
2582 if (buttons == NULL || *buttons == NUL)
2583 buttons = (char_u *)_("&Ok");
2584
2585 if (!error)
2586 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2587 def, NULL, FALSE);
2588#endif
2589}
2590
2591/*
2592 * "copy()" function
2593 */
2594 static void
2595f_copy(typval_T *argvars, typval_T *rettv)
2596{
2597 item_copy(&argvars[0], rettv, FALSE, 0);
2598}
2599
2600#ifdef FEAT_FLOAT
2601/*
2602 * "cos()" function
2603 */
2604 static void
2605f_cos(typval_T *argvars, typval_T *rettv)
2606{
2607 float_T f = 0.0;
2608
2609 rettv->v_type = VAR_FLOAT;
2610 if (get_float_arg(argvars, &f) == OK)
2611 rettv->vval.v_float = cos(f);
2612 else
2613 rettv->vval.v_float = 0.0;
2614}
2615
2616/*
2617 * "cosh()" function
2618 */
2619 static void
2620f_cosh(typval_T *argvars, typval_T *rettv)
2621{
2622 float_T f = 0.0;
2623
2624 rettv->v_type = VAR_FLOAT;
2625 if (get_float_arg(argvars, &f) == OK)
2626 rettv->vval.v_float = cosh(f);
2627 else
2628 rettv->vval.v_float = 0.0;
2629}
2630#endif
2631
2632/*
2633 * "count()" function
2634 */
2635 static void
2636f_count(typval_T *argvars, typval_T *rettv)
2637{
2638 long n = 0;
2639 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002640 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002641
Bram Moolenaar9966b212017-07-28 16:46:57 +02002642 if (argvars[2].v_type != VAR_UNKNOWN)
2643 ic = (int)get_tv_number_chk(&argvars[2], &error);
2644
2645 if (argvars[0].v_type == VAR_STRING)
2646 {
2647 char_u *expr = get_tv_string_chk(&argvars[1]);
2648 char_u *p = argvars[0].vval.v_string;
2649 char_u *next;
2650
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002651 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002652 {
2653 if (ic)
2654 {
2655 size_t len = STRLEN(expr);
2656
2657 while (*p != NUL)
2658 {
2659 if (MB_STRNICMP(p, expr, len) == 0)
2660 {
2661 ++n;
2662 p += len;
2663 }
2664 else
2665 MB_PTR_ADV(p);
2666 }
2667 }
2668 else
2669 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2670 != NULL)
2671 {
2672 ++n;
2673 p = next + STRLEN(expr);
2674 }
2675 }
2676
2677 }
2678 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002679 {
2680 listitem_T *li;
2681 list_T *l;
2682 long idx;
2683
2684 if ((l = argvars[0].vval.v_list) != NULL)
2685 {
2686 li = l->lv_first;
2687 if (argvars[2].v_type != VAR_UNKNOWN)
2688 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002689 if (argvars[3].v_type != VAR_UNKNOWN)
2690 {
2691 idx = (long)get_tv_number_chk(&argvars[3], &error);
2692 if (!error)
2693 {
2694 li = list_find(l, idx);
2695 if (li == NULL)
2696 EMSGN(_(e_listidx), idx);
2697 }
2698 }
2699 if (error)
2700 li = NULL;
2701 }
2702
2703 for ( ; li != NULL; li = li->li_next)
2704 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2705 ++n;
2706 }
2707 }
2708 else if (argvars[0].v_type == VAR_DICT)
2709 {
2710 int todo;
2711 dict_T *d;
2712 hashitem_T *hi;
2713
2714 if ((d = argvars[0].vval.v_dict) != NULL)
2715 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002716 if (argvars[2].v_type != VAR_UNKNOWN)
2717 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002718 if (argvars[3].v_type != VAR_UNKNOWN)
2719 EMSG(_(e_invarg));
2720 }
2721
2722 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2723 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2724 {
2725 if (!HASHITEM_EMPTY(hi))
2726 {
2727 --todo;
2728 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2729 ++n;
2730 }
2731 }
2732 }
2733 }
2734 else
2735 EMSG2(_(e_listdictarg), "count()");
2736 rettv->vval.v_number = n;
2737}
2738
2739/*
2740 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2741 *
2742 * Checks the existence of a cscope connection.
2743 */
2744 static void
2745f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2746{
2747#ifdef FEAT_CSCOPE
2748 int num = 0;
2749 char_u *dbpath = NULL;
2750 char_u *prepend = NULL;
2751 char_u buf[NUMBUFLEN];
2752
2753 if (argvars[0].v_type != VAR_UNKNOWN
2754 && argvars[1].v_type != VAR_UNKNOWN)
2755 {
2756 num = (int)get_tv_number(&argvars[0]);
2757 dbpath = get_tv_string(&argvars[1]);
2758 if (argvars[2].v_type != VAR_UNKNOWN)
2759 prepend = get_tv_string_buf(&argvars[2], buf);
2760 }
2761
2762 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2763#endif
2764}
2765
2766/*
2767 * "cursor(lnum, col)" function, or
2768 * "cursor(list)"
2769 *
2770 * Moves the cursor to the specified line and column.
2771 * Returns 0 when the position could be set, -1 otherwise.
2772 */
2773 static void
2774f_cursor(typval_T *argvars, typval_T *rettv)
2775{
2776 long line, col;
2777#ifdef FEAT_VIRTUALEDIT
2778 long coladd = 0;
2779#endif
2780 int set_curswant = TRUE;
2781
2782 rettv->vval.v_number = -1;
2783 if (argvars[1].v_type == VAR_UNKNOWN)
2784 {
2785 pos_T pos;
2786 colnr_T curswant = -1;
2787
2788 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2789 {
2790 EMSG(_(e_invarg));
2791 return;
2792 }
2793 line = pos.lnum;
2794 col = pos.col;
2795#ifdef FEAT_VIRTUALEDIT
2796 coladd = pos.coladd;
2797#endif
2798 if (curswant >= 0)
2799 {
2800 curwin->w_curswant = curswant - 1;
2801 set_curswant = FALSE;
2802 }
2803 }
2804 else
2805 {
2806 line = get_tv_lnum(argvars);
2807 col = (long)get_tv_number_chk(&argvars[1], NULL);
2808#ifdef FEAT_VIRTUALEDIT
2809 if (argvars[2].v_type != VAR_UNKNOWN)
2810 coladd = (long)get_tv_number_chk(&argvars[2], NULL);
2811#endif
2812 }
2813 if (line < 0 || col < 0
2814#ifdef FEAT_VIRTUALEDIT
2815 || coladd < 0
2816#endif
2817 )
2818 return; /* type error; errmsg already given */
2819 if (line > 0)
2820 curwin->w_cursor.lnum = line;
2821 if (col > 0)
2822 curwin->w_cursor.col = col - 1;
2823#ifdef FEAT_VIRTUALEDIT
2824 curwin->w_cursor.coladd = coladd;
2825#endif
2826
2827 /* Make sure the cursor is in a valid position. */
2828 check_cursor();
2829#ifdef FEAT_MBYTE
2830 /* Correct cursor for multi-byte character. */
2831 if (has_mbyte)
2832 mb_adjust_cursor();
2833#endif
2834
2835 curwin->w_set_curswant = set_curswant;
2836 rettv->vval.v_number = 0;
2837}
2838
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002839#ifdef WIN3264
2840/*
2841 * "debugbreak()" function
2842 */
2843 static void
2844f_debugbreak(typval_T *argvars, typval_T *rettv)
2845{
2846 int pid;
2847
2848 rettv->vval.v_number = FAIL;
2849 pid = (int)get_tv_number(&argvars[0]);
2850 if (pid == 0)
2851 EMSG(_(e_invarg));
2852 else
2853 {
2854 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2855
2856 if (hProcess != NULL)
2857 {
2858 DebugBreakProcess(hProcess);
2859 CloseHandle(hProcess);
2860 rettv->vval.v_number = OK;
2861 }
2862 }
2863}
2864#endif
2865
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002866/*
2867 * "deepcopy()" function
2868 */
2869 static void
2870f_deepcopy(typval_T *argvars, typval_T *rettv)
2871{
2872 int noref = 0;
2873 int copyID;
2874
2875 if (argvars[1].v_type != VAR_UNKNOWN)
2876 noref = (int)get_tv_number_chk(&argvars[1], NULL);
2877 if (noref < 0 || noref > 1)
2878 EMSG(_(e_invarg));
2879 else
2880 {
2881 copyID = get_copyID();
2882 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2883 }
2884}
2885
2886/*
2887 * "delete()" function
2888 */
2889 static void
2890f_delete(typval_T *argvars, typval_T *rettv)
2891{
2892 char_u nbuf[NUMBUFLEN];
2893 char_u *name;
2894 char_u *flags;
2895
2896 rettv->vval.v_number = -1;
2897 if (check_restricted() || check_secure())
2898 return;
2899
2900 name = get_tv_string(&argvars[0]);
2901 if (name == NULL || *name == NUL)
2902 {
2903 EMSG(_(e_invarg));
2904 return;
2905 }
2906
2907 if (argvars[1].v_type != VAR_UNKNOWN)
2908 flags = get_tv_string_buf(&argvars[1], nbuf);
2909 else
2910 flags = (char_u *)"";
2911
2912 if (*flags == NUL)
2913 /* delete a file */
2914 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2915 else if (STRCMP(flags, "d") == 0)
2916 /* delete an empty directory */
2917 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2918 else if (STRCMP(flags, "rf") == 0)
2919 /* delete a directory recursively */
2920 rettv->vval.v_number = delete_recursive(name);
2921 else
2922 EMSG2(_(e_invexpr2), flags);
2923}
2924
2925/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02002926 * "deletebufline()" function
2927 */
2928 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02002929f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02002930{
2931 buf_T *buf;
2932 linenr_T first, last;
2933 linenr_T lnum;
2934 long count;
2935 int is_curbuf;
2936 buf_T *curbuf_save = NULL;
2937 win_T *curwin_save = NULL;
2938 tabpage_T *tp;
2939 win_T *wp;
2940
2941 buf = get_buf_tv(&argvars[0], FALSE);
2942 if (buf == NULL)
2943 {
2944 rettv->vval.v_number = 1; /* FAIL */
2945 return;
2946 }
2947 is_curbuf = buf == curbuf;
2948
2949 first = get_tv_lnum_buf(&argvars[1], buf);
2950 if (argvars[2].v_type != VAR_UNKNOWN)
2951 last = get_tv_lnum_buf(&argvars[2], buf);
2952 else
2953 last = first;
2954
2955 if (buf->b_ml.ml_mfp == NULL || first < 1
2956 || first > buf->b_ml.ml_line_count || last < first)
2957 {
2958 rettv->vval.v_number = 1; /* FAIL */
2959 return;
2960 }
2961
2962 if (!is_curbuf)
2963 {
2964 curbuf_save = curbuf;
2965 curwin_save = curwin;
2966 curbuf = buf;
2967 find_win_for_curbuf();
2968 }
2969 if (last > curbuf->b_ml.ml_line_count)
2970 last = curbuf->b_ml.ml_line_count;
2971 count = last - first + 1;
2972
2973 // When coming here from Insert mode, sync undo, so that this can be
2974 // undone separately from what was previously inserted.
2975 if (u_sync_once == 2)
2976 {
2977 u_sync_once = 1; // notify that u_sync() was called
2978 u_sync(TRUE);
2979 }
2980
2981 if (u_save(first - 1, last + 1) == FAIL)
2982 {
2983 rettv->vval.v_number = 1; /* FAIL */
2984 return;
2985 }
2986
2987 for (lnum = first; lnum <= last; ++lnum)
2988 ml_delete(first, TRUE);
2989
2990 FOR_ALL_TAB_WINDOWS(tp, wp)
2991 if (wp->w_buffer == buf)
2992 {
2993 if (wp->w_cursor.lnum > last)
2994 wp->w_cursor.lnum -= count;
2995 else if (wp->w_cursor.lnum> first)
2996 wp->w_cursor.lnum = first;
2997 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
2998 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
2999 }
3000 check_cursor_col();
3001 deleted_lines_mark(first, count);
3002
3003 if (!is_curbuf)
3004 {
3005 curbuf = curbuf_save;
3006 curwin = curwin_save;
3007 }
3008}
3009
3010/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003011 * "did_filetype()" function
3012 */
3013 static void
3014f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3015{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003016 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003017}
3018
3019/*
3020 * "diff_filler()" function
3021 */
3022 static void
3023f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3024{
3025#ifdef FEAT_DIFF
3026 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
3027#endif
3028}
3029
3030/*
3031 * "diff_hlID()" function
3032 */
3033 static void
3034f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3035{
3036#ifdef FEAT_DIFF
3037 linenr_T lnum = get_tv_lnum(argvars);
3038 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01003039 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003040 static int fnum = 0;
3041 static int change_start = 0;
3042 static int change_end = 0;
3043 static hlf_T hlID = (hlf_T)0;
3044 int filler_lines;
3045 int col;
3046
3047 if (lnum < 0) /* ignore type error in {lnum} arg */
3048 lnum = 0;
3049 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003050 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003051 || fnum != curbuf->b_fnum)
3052 {
3053 /* New line, buffer, change: need to get the values. */
3054 filler_lines = diff_check(curwin, lnum);
3055 if (filler_lines < 0)
3056 {
3057 if (filler_lines == -1)
3058 {
3059 change_start = MAXCOL;
3060 change_end = -1;
3061 if (diff_find_change(curwin, lnum, &change_start, &change_end))
3062 hlID = HLF_ADD; /* added line */
3063 else
3064 hlID = HLF_CHD; /* changed line */
3065 }
3066 else
3067 hlID = HLF_ADD; /* added line */
3068 }
3069 else
3070 hlID = (hlf_T)0;
3071 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003072 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003073 fnum = curbuf->b_fnum;
3074 }
3075
3076 if (hlID == HLF_CHD || hlID == HLF_TXD)
3077 {
3078 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
3079 if (col >= change_start && col <= change_end)
3080 hlID = HLF_TXD; /* changed text */
3081 else
3082 hlID = HLF_CHD; /* changed line */
3083 }
3084 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
3085#endif
3086}
3087
3088/*
3089 * "empty({expr})" function
3090 */
3091 static void
3092f_empty(typval_T *argvars, typval_T *rettv)
3093{
3094 int n = FALSE;
3095
3096 switch (argvars[0].v_type)
3097 {
3098 case VAR_STRING:
3099 case VAR_FUNC:
3100 n = argvars[0].vval.v_string == NULL
3101 || *argvars[0].vval.v_string == NUL;
3102 break;
3103 case VAR_PARTIAL:
3104 n = FALSE;
3105 break;
3106 case VAR_NUMBER:
3107 n = argvars[0].vval.v_number == 0;
3108 break;
3109 case VAR_FLOAT:
3110#ifdef FEAT_FLOAT
3111 n = argvars[0].vval.v_float == 0.0;
3112 break;
3113#endif
3114 case VAR_LIST:
3115 n = argvars[0].vval.v_list == NULL
3116 || argvars[0].vval.v_list->lv_first == NULL;
3117 break;
3118 case VAR_DICT:
3119 n = argvars[0].vval.v_dict == NULL
3120 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
3121 break;
3122 case VAR_SPECIAL:
3123 n = argvars[0].vval.v_number != VVAL_TRUE;
3124 break;
3125
3126 case VAR_JOB:
3127#ifdef FEAT_JOB_CHANNEL
3128 n = argvars[0].vval.v_job == NULL
3129 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
3130 break;
3131#endif
3132 case VAR_CHANNEL:
3133#ifdef FEAT_JOB_CHANNEL
3134 n = argvars[0].vval.v_channel == NULL
3135 || !channel_is_open(argvars[0].vval.v_channel);
3136 break;
3137#endif
3138 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01003139 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003140 n = TRUE;
3141 break;
3142 }
3143
3144 rettv->vval.v_number = n;
3145}
3146
3147/*
3148 * "escape({string}, {chars})" function
3149 */
3150 static void
3151f_escape(typval_T *argvars, typval_T *rettv)
3152{
3153 char_u buf[NUMBUFLEN];
3154
3155 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
3156 get_tv_string_buf(&argvars[1], buf));
3157 rettv->v_type = VAR_STRING;
3158}
3159
3160/*
3161 * "eval()" function
3162 */
3163 static void
3164f_eval(typval_T *argvars, typval_T *rettv)
3165{
3166 char_u *s, *p;
3167
3168 s = get_tv_string_chk(&argvars[0]);
3169 if (s != NULL)
3170 s = skipwhite(s);
3171
3172 p = s;
3173 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3174 {
3175 if (p != NULL && !aborting())
3176 EMSG2(_(e_invexpr2), p);
3177 need_clr_eos = FALSE;
3178 rettv->v_type = VAR_NUMBER;
3179 rettv->vval.v_number = 0;
3180 }
3181 else if (*s != NUL)
3182 EMSG(_(e_trailing));
3183}
3184
3185/*
3186 * "eventhandler()" function
3187 */
3188 static void
3189f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3190{
3191 rettv->vval.v_number = vgetc_busy;
3192}
3193
3194/*
3195 * "executable()" function
3196 */
3197 static void
3198f_executable(typval_T *argvars, typval_T *rettv)
3199{
3200 char_u *name = get_tv_string(&argvars[0]);
3201
3202 /* Check in $PATH and also check directly if there is a directory name. */
3203 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
3204 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
3205}
3206
3207static garray_T redir_execute_ga;
3208
3209/*
3210 * Append "value[value_len]" to the execute() output.
3211 */
3212 void
3213execute_redir_str(char_u *value, int value_len)
3214{
3215 int len;
3216
3217 if (value_len == -1)
3218 len = (int)STRLEN(value); /* Append the entire string */
3219 else
3220 len = value_len; /* Append only "value_len" characters */
3221 if (ga_grow(&redir_execute_ga, len) == OK)
3222 {
3223 mch_memmove((char *)redir_execute_ga.ga_data
3224 + redir_execute_ga.ga_len, value, len);
3225 redir_execute_ga.ga_len += len;
3226 }
3227}
3228
3229/*
3230 * Get next line from a list.
3231 * Called by do_cmdline() to get the next line.
3232 * Returns allocated string, or NULL for end of function.
3233 */
3234
3235 static char_u *
3236get_list_line(
3237 int c UNUSED,
3238 void *cookie,
3239 int indent UNUSED)
3240{
3241 listitem_T **p = (listitem_T **)cookie;
3242 listitem_T *item = *p;
3243 char_u buf[NUMBUFLEN];
3244 char_u *s;
3245
3246 if (item == NULL)
3247 return NULL;
3248 s = get_tv_string_buf_chk(&item->li_tv, buf);
3249 *p = item->li_next;
3250 return s == NULL ? NULL : vim_strsave(s);
3251}
3252
3253/*
3254 * "execute()" function
3255 */
3256 static void
3257f_execute(typval_T *argvars, typval_T *rettv)
3258{
3259 char_u *cmd = NULL;
3260 list_T *list = NULL;
3261 int save_msg_silent = msg_silent;
3262 int save_emsg_silent = emsg_silent;
3263 int save_emsg_noredir = emsg_noredir;
3264 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003265 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003266 garray_T save_ga;
3267
3268 rettv->vval.v_string = NULL;
3269 rettv->v_type = VAR_STRING;
3270
3271 if (argvars[0].v_type == VAR_LIST)
3272 {
3273 list = argvars[0].vval.v_list;
3274 if (list == NULL || list->lv_first == NULL)
3275 /* empty list, no commands, empty output */
3276 return;
3277 ++list->lv_refcount;
3278 }
3279 else
3280 {
3281 cmd = get_tv_string_chk(&argvars[0]);
3282 if (cmd == NULL)
3283 return;
3284 }
3285
3286 if (argvars[1].v_type != VAR_UNKNOWN)
3287 {
3288 char_u buf[NUMBUFLEN];
3289 char_u *s = get_tv_string_buf_chk(&argvars[1], buf);
3290
3291 if (s == NULL)
3292 return;
3293 if (STRNCMP(s, "silent", 6) == 0)
3294 ++msg_silent;
3295 if (STRCMP(s, "silent!") == 0)
3296 {
3297 emsg_silent = TRUE;
3298 emsg_noredir = TRUE;
3299 }
3300 }
3301 else
3302 ++msg_silent;
3303
3304 if (redir_execute)
3305 save_ga = redir_execute_ga;
3306 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3307 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003308 redir_off = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003309
3310 if (cmd != NULL)
3311 do_cmdline_cmd(cmd);
3312 else
3313 {
3314 listitem_T *item = list->lv_first;
3315
3316 do_cmdline(NULL, get_list_line, (void *)&item,
3317 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3318 --list->lv_refcount;
3319 }
3320
Bram Moolenaard297f352017-01-29 20:31:21 +01003321 /* Need to append a NUL to the result. */
3322 if (ga_grow(&redir_execute_ga, 1) == OK)
3323 {
3324 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3325 rettv->vval.v_string = redir_execute_ga.ga_data;
3326 }
3327 else
3328 {
3329 ga_clear(&redir_execute_ga);
3330 rettv->vval.v_string = NULL;
3331 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003332 msg_silent = save_msg_silent;
3333 emsg_silent = save_emsg_silent;
3334 emsg_noredir = save_emsg_noredir;
3335
3336 redir_execute = save_redir_execute;
3337 if (redir_execute)
3338 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003339 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003340
3341 /* "silent reg" or "silent echo x" leaves msg_col somewhere in the
3342 * line. Put it back in the first column. */
3343 msg_col = 0;
3344}
3345
3346/*
3347 * "exepath()" function
3348 */
3349 static void
3350f_exepath(typval_T *argvars, typval_T *rettv)
3351{
3352 char_u *p = NULL;
3353
3354 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
3355 rettv->v_type = VAR_STRING;
3356 rettv->vval.v_string = p;
3357}
3358
3359/*
3360 * "exists()" function
3361 */
3362 static void
3363f_exists(typval_T *argvars, typval_T *rettv)
3364{
3365 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003366 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003367
3368 p = get_tv_string(&argvars[0]);
3369 if (*p == '$') /* environment variable */
3370 {
3371 /* first try "normal" environment variables (fast) */
3372 if (mch_getenv(p + 1) != NULL)
3373 n = TRUE;
3374 else
3375 {
3376 /* try expanding things like $VIM and ${HOME} */
3377 p = expand_env_save(p);
3378 if (p != NULL && *p != '$')
3379 n = TRUE;
3380 vim_free(p);
3381 }
3382 }
3383 else if (*p == '&' || *p == '+') /* option */
3384 {
3385 n = (get_option_tv(&p, NULL, TRUE) == OK);
3386 if (*skipwhite(p) != NUL)
3387 n = FALSE; /* trailing garbage */
3388 }
3389 else if (*p == '*') /* internal or user defined function */
3390 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003391 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003392 }
3393 else if (*p == ':')
3394 {
3395 n = cmd_exists(p + 1);
3396 }
3397 else if (*p == '#')
3398 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003399 if (p[1] == '#')
3400 n = autocmd_supported(p + 2);
3401 else
3402 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003403 }
3404 else /* internal variable */
3405 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003406 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003407 }
3408
3409 rettv->vval.v_number = n;
3410}
3411
3412#ifdef FEAT_FLOAT
3413/*
3414 * "exp()" function
3415 */
3416 static void
3417f_exp(typval_T *argvars, typval_T *rettv)
3418{
3419 float_T f = 0.0;
3420
3421 rettv->v_type = VAR_FLOAT;
3422 if (get_float_arg(argvars, &f) == OK)
3423 rettv->vval.v_float = exp(f);
3424 else
3425 rettv->vval.v_float = 0.0;
3426}
3427#endif
3428
3429/*
3430 * "expand()" function
3431 */
3432 static void
3433f_expand(typval_T *argvars, typval_T *rettv)
3434{
3435 char_u *s;
3436 int len;
3437 char_u *errormsg;
3438 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3439 expand_T xpc;
3440 int error = FALSE;
3441 char_u *result;
3442
3443 rettv->v_type = VAR_STRING;
3444 if (argvars[1].v_type != VAR_UNKNOWN
3445 && argvars[2].v_type != VAR_UNKNOWN
3446 && get_tv_number_chk(&argvars[2], &error)
3447 && !error)
3448 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003449 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003450 }
3451
3452 s = get_tv_string(&argvars[0]);
3453 if (*s == '%' || *s == '#' || *s == '<')
3454 {
3455 ++emsg_off;
3456 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3457 --emsg_off;
3458 if (rettv->v_type == VAR_LIST)
3459 {
3460 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3461 list_append_string(rettv->vval.v_list, result, -1);
3462 else
3463 vim_free(result);
3464 }
3465 else
3466 rettv->vval.v_string = result;
3467 }
3468 else
3469 {
3470 /* When the optional second argument is non-zero, don't remove matches
3471 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3472 if (argvars[1].v_type != VAR_UNKNOWN
3473 && get_tv_number_chk(&argvars[1], &error))
3474 options |= WILD_KEEP_ALL;
3475 if (!error)
3476 {
3477 ExpandInit(&xpc);
3478 xpc.xp_context = EXPAND_FILES;
3479 if (p_wic)
3480 options += WILD_ICASE;
3481 if (rettv->v_type == VAR_STRING)
3482 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3483 options, WILD_ALL);
3484 else if (rettv_list_alloc(rettv) != FAIL)
3485 {
3486 int i;
3487
3488 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3489 for (i = 0; i < xpc.xp_numfiles; i++)
3490 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3491 ExpandCleanup(&xpc);
3492 }
3493 }
3494 else
3495 rettv->vval.v_string = NULL;
3496 }
3497}
3498
3499/*
3500 * "extend(list, list [, idx])" function
3501 * "extend(dict, dict [, action])" function
3502 */
3503 static void
3504f_extend(typval_T *argvars, typval_T *rettv)
3505{
3506 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3507
3508 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3509 {
3510 list_T *l1, *l2;
3511 listitem_T *item;
3512 long before;
3513 int error = FALSE;
3514
3515 l1 = argvars[0].vval.v_list;
3516 l2 = argvars[1].vval.v_list;
3517 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
3518 && l2 != NULL)
3519 {
3520 if (argvars[2].v_type != VAR_UNKNOWN)
3521 {
3522 before = (long)get_tv_number_chk(&argvars[2], &error);
3523 if (error)
3524 return; /* type error; errmsg already given */
3525
3526 if (before == l1->lv_len)
3527 item = NULL;
3528 else
3529 {
3530 item = list_find(l1, before);
3531 if (item == NULL)
3532 {
3533 EMSGN(_(e_listidx), before);
3534 return;
3535 }
3536 }
3537 }
3538 else
3539 item = NULL;
3540 list_extend(l1, l2, item);
3541
3542 copy_tv(&argvars[0], rettv);
3543 }
3544 }
3545 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3546 {
3547 dict_T *d1, *d2;
3548 char_u *action;
3549 int i;
3550
3551 d1 = argvars[0].vval.v_dict;
3552 d2 = argvars[1].vval.v_dict;
3553 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
3554 && d2 != NULL)
3555 {
3556 /* Check the third argument. */
3557 if (argvars[2].v_type != VAR_UNKNOWN)
3558 {
3559 static char *(av[]) = {"keep", "force", "error"};
3560
3561 action = get_tv_string_chk(&argvars[2]);
3562 if (action == NULL)
3563 return; /* type error; errmsg already given */
3564 for (i = 0; i < 3; ++i)
3565 if (STRCMP(action, av[i]) == 0)
3566 break;
3567 if (i == 3)
3568 {
3569 EMSG2(_(e_invarg2), action);
3570 return;
3571 }
3572 }
3573 else
3574 action = (char_u *)"force";
3575
3576 dict_extend(d1, d2, action);
3577
3578 copy_tv(&argvars[0], rettv);
3579 }
3580 }
3581 else
3582 EMSG2(_(e_listdictarg), "extend()");
3583}
3584
3585/*
3586 * "feedkeys()" function
3587 */
3588 static void
3589f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3590{
3591 int remap = TRUE;
3592 int insert = FALSE;
3593 char_u *keys, *flags;
3594 char_u nbuf[NUMBUFLEN];
3595 int typed = FALSE;
3596 int execute = FALSE;
3597 int dangerous = FALSE;
3598 char_u *keys_esc;
3599
3600 /* This is not allowed in the sandbox. If the commands would still be
3601 * executed in the sandbox it would be OK, but it probably happens later,
3602 * when "sandbox" is no longer set. */
3603 if (check_secure())
3604 return;
3605
3606 keys = get_tv_string(&argvars[0]);
3607
3608 if (argvars[1].v_type != VAR_UNKNOWN)
3609 {
3610 flags = get_tv_string_buf(&argvars[1], nbuf);
3611 for ( ; *flags != NUL; ++flags)
3612 {
3613 switch (*flags)
3614 {
3615 case 'n': remap = FALSE; break;
3616 case 'm': remap = TRUE; break;
3617 case 't': typed = TRUE; break;
3618 case 'i': insert = TRUE; break;
3619 case 'x': execute = TRUE; break;
3620 case '!': dangerous = TRUE; break;
3621 }
3622 }
3623 }
3624
3625 if (*keys != NUL || execute)
3626 {
3627 /* Need to escape K_SPECIAL and CSI before putting the string in the
3628 * typeahead buffer. */
3629 keys_esc = vim_strsave_escape_csi(keys);
3630 if (keys_esc != NULL)
3631 {
3632 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
3633 insert ? 0 : typebuf.tb_len, !typed, FALSE);
3634 vim_free(keys_esc);
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003635 if (vgetc_busy
3636#ifdef FEAT_TIMERS
3637 || timer_busy
3638#endif
3639 )
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003640 typebuf_was_filled = TRUE;
3641 if (execute)
3642 {
3643 int save_msg_scroll = msg_scroll;
3644
3645 /* Avoid a 1 second delay when the keys start Insert mode. */
3646 msg_scroll = FALSE;
3647
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003648 if (!dangerous)
3649 ++ex_normal_busy;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02003650 exec_normal(TRUE, FALSE, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003651 if (!dangerous)
3652 --ex_normal_busy;
3653
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003654 msg_scroll |= save_msg_scroll;
3655 }
3656 }
3657 }
3658}
3659
3660/*
3661 * "filereadable()" function
3662 */
3663 static void
3664f_filereadable(typval_T *argvars, typval_T *rettv)
3665{
3666 int fd;
3667 char_u *p;
3668 int n;
3669
3670#ifndef O_NONBLOCK
3671# define O_NONBLOCK 0
3672#endif
3673 p = get_tv_string(&argvars[0]);
3674 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3675 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3676 {
3677 n = TRUE;
3678 close(fd);
3679 }
3680 else
3681 n = FALSE;
3682
3683 rettv->vval.v_number = n;
3684}
3685
3686/*
3687 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3688 * rights to write into.
3689 */
3690 static void
3691f_filewritable(typval_T *argvars, typval_T *rettv)
3692{
3693 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
3694}
3695
3696 static void
3697findfilendir(
3698 typval_T *argvars UNUSED,
3699 typval_T *rettv,
3700 int find_what UNUSED)
3701{
3702#ifdef FEAT_SEARCHPATH
3703 char_u *fname;
3704 char_u *fresult = NULL;
3705 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3706 char_u *p;
3707 char_u pathbuf[NUMBUFLEN];
3708 int count = 1;
3709 int first = TRUE;
3710 int error = FALSE;
3711#endif
3712
3713 rettv->vval.v_string = NULL;
3714 rettv->v_type = VAR_STRING;
3715
3716#ifdef FEAT_SEARCHPATH
3717 fname = get_tv_string(&argvars[0]);
3718
3719 if (argvars[1].v_type != VAR_UNKNOWN)
3720 {
3721 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
3722 if (p == NULL)
3723 error = TRUE;
3724 else
3725 {
3726 if (*p != NUL)
3727 path = p;
3728
3729 if (argvars[2].v_type != VAR_UNKNOWN)
3730 count = (int)get_tv_number_chk(&argvars[2], &error);
3731 }
3732 }
3733
3734 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3735 error = TRUE;
3736
3737 if (*fname != NUL && !error)
3738 {
3739 do
3740 {
3741 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3742 vim_free(fresult);
3743 fresult = find_file_in_path_option(first ? fname : NULL,
3744 first ? (int)STRLEN(fname) : 0,
3745 0, first, path,
3746 find_what,
3747 curbuf->b_ffname,
3748 find_what == FINDFILE_DIR
3749 ? (char_u *)"" : curbuf->b_p_sua);
3750 first = FALSE;
3751
3752 if (fresult != NULL && rettv->v_type == VAR_LIST)
3753 list_append_string(rettv->vval.v_list, fresult, -1);
3754
3755 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3756 }
3757
3758 if (rettv->v_type == VAR_STRING)
3759 rettv->vval.v_string = fresult;
3760#endif
3761}
3762
3763/*
3764 * "filter()" function
3765 */
3766 static void
3767f_filter(typval_T *argvars, typval_T *rettv)
3768{
3769 filter_map(argvars, rettv, FALSE);
3770}
3771
3772/*
3773 * "finddir({fname}[, {path}[, {count}]])" function
3774 */
3775 static void
3776f_finddir(typval_T *argvars, typval_T *rettv)
3777{
3778 findfilendir(argvars, rettv, FINDFILE_DIR);
3779}
3780
3781/*
3782 * "findfile({fname}[, {path}[, {count}]])" function
3783 */
3784 static void
3785f_findfile(typval_T *argvars, typval_T *rettv)
3786{
3787 findfilendir(argvars, rettv, FINDFILE_FILE);
3788}
3789
3790#ifdef FEAT_FLOAT
3791/*
3792 * "float2nr({float})" function
3793 */
3794 static void
3795f_float2nr(typval_T *argvars, typval_T *rettv)
3796{
3797 float_T f = 0.0;
3798
3799 if (get_float_arg(argvars, &f) == OK)
3800 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003801 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003802 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003803 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003804 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003805 else
3806 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003807 }
3808}
3809
3810/*
3811 * "floor({float})" function
3812 */
3813 static void
3814f_floor(typval_T *argvars, typval_T *rettv)
3815{
3816 float_T f = 0.0;
3817
3818 rettv->v_type = VAR_FLOAT;
3819 if (get_float_arg(argvars, &f) == OK)
3820 rettv->vval.v_float = floor(f);
3821 else
3822 rettv->vval.v_float = 0.0;
3823}
3824
3825/*
3826 * "fmod()" function
3827 */
3828 static void
3829f_fmod(typval_T *argvars, typval_T *rettv)
3830{
3831 float_T fx = 0.0, fy = 0.0;
3832
3833 rettv->v_type = VAR_FLOAT;
3834 if (get_float_arg(argvars, &fx) == OK
3835 && get_float_arg(&argvars[1], &fy) == OK)
3836 rettv->vval.v_float = fmod(fx, fy);
3837 else
3838 rettv->vval.v_float = 0.0;
3839}
3840#endif
3841
3842/*
3843 * "fnameescape({string})" function
3844 */
3845 static void
3846f_fnameescape(typval_T *argvars, typval_T *rettv)
3847{
3848 rettv->vval.v_string = vim_strsave_fnameescape(
3849 get_tv_string(&argvars[0]), FALSE);
3850 rettv->v_type = VAR_STRING;
3851}
3852
3853/*
3854 * "fnamemodify({fname}, {mods})" function
3855 */
3856 static void
3857f_fnamemodify(typval_T *argvars, typval_T *rettv)
3858{
3859 char_u *fname;
3860 char_u *mods;
3861 int usedlen = 0;
3862 int len;
3863 char_u *fbuf = NULL;
3864 char_u buf[NUMBUFLEN];
3865
3866 fname = get_tv_string_chk(&argvars[0]);
3867 mods = get_tv_string_buf_chk(&argvars[1], buf);
3868 if (fname == NULL || mods == NULL)
3869 fname = NULL;
3870 else
3871 {
3872 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02003873 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003874 }
3875
3876 rettv->v_type = VAR_STRING;
3877 if (fname == NULL)
3878 rettv->vval.v_string = NULL;
3879 else
3880 rettv->vval.v_string = vim_strnsave(fname, len);
3881 vim_free(fbuf);
3882}
3883
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003884/*
3885 * "foldclosed()" function
3886 */
3887 static void
3888foldclosed_both(
3889 typval_T *argvars UNUSED,
3890 typval_T *rettv,
3891 int end UNUSED)
3892{
3893#ifdef FEAT_FOLDING
3894 linenr_T lnum;
3895 linenr_T first, last;
3896
3897 lnum = get_tv_lnum(argvars);
3898 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3899 {
3900 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3901 {
3902 if (end)
3903 rettv->vval.v_number = (varnumber_T)last;
3904 else
3905 rettv->vval.v_number = (varnumber_T)first;
3906 return;
3907 }
3908 }
3909#endif
3910 rettv->vval.v_number = -1;
3911}
3912
3913/*
3914 * "foldclosed()" function
3915 */
3916 static void
3917f_foldclosed(typval_T *argvars, typval_T *rettv)
3918{
3919 foldclosed_both(argvars, rettv, FALSE);
3920}
3921
3922/*
3923 * "foldclosedend()" function
3924 */
3925 static void
3926f_foldclosedend(typval_T *argvars, typval_T *rettv)
3927{
3928 foldclosed_both(argvars, rettv, TRUE);
3929}
3930
3931/*
3932 * "foldlevel()" function
3933 */
3934 static void
3935f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3936{
3937#ifdef FEAT_FOLDING
3938 linenr_T lnum;
3939
3940 lnum = get_tv_lnum(argvars);
3941 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3942 rettv->vval.v_number = foldLevel(lnum);
3943#endif
3944}
3945
3946/*
3947 * "foldtext()" function
3948 */
3949 static void
3950f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
3951{
3952#ifdef FEAT_FOLDING
3953 linenr_T foldstart;
3954 linenr_T foldend;
3955 char_u *dashes;
3956 linenr_T lnum;
3957 char_u *s;
3958 char_u *r;
3959 int len;
3960 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003961 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003962#endif
3963
3964 rettv->v_type = VAR_STRING;
3965 rettv->vval.v_string = NULL;
3966#ifdef FEAT_FOLDING
3967 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
3968 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
3969 dashes = get_vim_var_str(VV_FOLDDASHES);
3970 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
3971 && dashes != NULL)
3972 {
3973 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02003974 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003975 if (!linewhite(lnum))
3976 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003977
3978 /* Find interesting text in this line. */
3979 s = skipwhite(ml_get(lnum));
3980 /* skip C comment-start */
3981 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
3982 {
3983 s = skipwhite(s + 2);
3984 if (*skipwhite(s) == NUL
3985 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
3986 {
3987 s = skipwhite(ml_get(lnum + 1));
3988 if (*s == '*')
3989 s = skipwhite(s + 1);
3990 }
3991 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02003992 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003993 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003994 r = alloc((unsigned)(STRLEN(txt)
3995 + STRLEN(dashes) /* for %s */
3996 + 20 /* for %3ld */
3997 + STRLEN(s))); /* concatenated */
3998 if (r != NULL)
3999 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02004000 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004001 len = (int)STRLEN(r);
4002 STRCAT(r, s);
4003 /* remove 'foldmarker' and 'commentstring' */
4004 foldtext_cleanup(r + len);
4005 rettv->vval.v_string = r;
4006 }
4007 }
4008#endif
4009}
4010
4011/*
4012 * "foldtextresult(lnum)" function
4013 */
4014 static void
4015f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
4016{
4017#ifdef FEAT_FOLDING
4018 linenr_T lnum;
4019 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004020 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004021 foldinfo_T foldinfo;
4022 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004023 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004024#endif
4025
4026 rettv->v_type = VAR_STRING;
4027 rettv->vval.v_string = NULL;
4028#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004029 if (entered)
4030 return; /* reject recursive use */
4031 entered = TRUE;
4032
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004033 lnum = get_tv_lnum(argvars);
4034 /* treat illegal types and illegal string values for {lnum} the same */
4035 if (lnum < 0)
4036 lnum = 0;
4037 fold_count = foldedCount(curwin, lnum, &foldinfo);
4038 if (fold_count > 0)
4039 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01004040 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
4041 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004042 if (text == buf)
4043 text = vim_strsave(text);
4044 rettv->vval.v_string = text;
4045 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004046
4047 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004048#endif
4049}
4050
4051/*
4052 * "foreground()" function
4053 */
4054 static void
4055f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4056{
4057#ifdef FEAT_GUI
4058 if (gui.in_use)
4059 gui_mch_set_foreground();
4060#else
4061# ifdef WIN32
4062 win32_set_foreground();
4063# endif
4064#endif
4065}
4066
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004067 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004068common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004069{
4070 char_u *s;
4071 char_u *name;
4072 int use_string = FALSE;
4073 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004074 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004075
4076 if (argvars[0].v_type == VAR_FUNC)
4077 {
4078 /* function(MyFunc, [arg], dict) */
4079 s = argvars[0].vval.v_string;
4080 }
4081 else if (argvars[0].v_type == VAR_PARTIAL
4082 && argvars[0].vval.v_partial != NULL)
4083 {
4084 /* function(dict.MyFunc, [arg]) */
4085 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004086 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004087 }
4088 else
4089 {
4090 /* function('MyFunc', [arg], dict) */
4091 s = get_tv_string(&argvars[0]);
4092 use_string = TRUE;
4093 }
4094
Bram Moolenaar843b8842016-08-21 14:36:15 +02004095 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004096 {
4097 name = s;
4098 trans_name = trans_function_name(&name, FALSE,
4099 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4100 if (*name != NUL)
4101 s = NULL;
4102 }
4103
Bram Moolenaar843b8842016-08-21 14:36:15 +02004104 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4105 || (is_funcref && trans_name == NULL))
Bram Moolenaar5582ef12016-09-14 22:16:13 +02004106 EMSG2(_(e_invarg2), use_string ? get_tv_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004107 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004108 else if (trans_name != NULL && (is_funcref
4109 ? find_func(trans_name) == NULL
4110 : !translated_function_exists(trans_name)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004111 EMSG2(_("E700: Unknown function: %s"), s);
4112 else
4113 {
4114 int dict_idx = 0;
4115 int arg_idx = 0;
4116 list_T *list = NULL;
4117
4118 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4119 {
4120 char sid_buf[25];
4121 int off = *s == 's' ? 2 : 5;
4122
4123 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4124 * also be called from another script. Using trans_function_name()
4125 * would also work, but some plugins depend on the name being
4126 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004127 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004128 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
4129 if (name != NULL)
4130 {
4131 STRCPY(name, sid_buf);
4132 STRCAT(name, s + off);
4133 }
4134 }
4135 else
4136 name = vim_strsave(s);
4137
4138 if (argvars[1].v_type != VAR_UNKNOWN)
4139 {
4140 if (argvars[2].v_type != VAR_UNKNOWN)
4141 {
4142 /* function(name, [args], dict) */
4143 arg_idx = 1;
4144 dict_idx = 2;
4145 }
4146 else if (argvars[1].v_type == VAR_DICT)
4147 /* function(name, dict) */
4148 dict_idx = 1;
4149 else
4150 /* function(name, [args]) */
4151 arg_idx = 1;
4152 if (dict_idx > 0)
4153 {
4154 if (argvars[dict_idx].v_type != VAR_DICT)
4155 {
4156 EMSG(_("E922: expected a dict"));
4157 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004158 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004159 }
4160 if (argvars[dict_idx].vval.v_dict == NULL)
4161 dict_idx = 0;
4162 }
4163 if (arg_idx > 0)
4164 {
4165 if (argvars[arg_idx].v_type != VAR_LIST)
4166 {
4167 EMSG(_("E923: Second argument of function() must be a list or a dict"));
4168 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004169 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004170 }
4171 list = argvars[arg_idx].vval.v_list;
4172 if (list == NULL || list->lv_len == 0)
4173 arg_idx = 0;
4174 }
4175 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004176 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004177 {
4178 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
4179
4180 /* result is a VAR_PARTIAL */
4181 if (pt == NULL)
4182 vim_free(name);
4183 else
4184 {
4185 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4186 {
4187 listitem_T *li;
4188 int i = 0;
4189 int arg_len = 0;
4190 int lv_len = 0;
4191
4192 if (arg_pt != NULL)
4193 arg_len = arg_pt->pt_argc;
4194 if (list != NULL)
4195 lv_len = list->lv_len;
4196 pt->pt_argc = arg_len + lv_len;
4197 pt->pt_argv = (typval_T *)alloc(
4198 sizeof(typval_T) * pt->pt_argc);
4199 if (pt->pt_argv == NULL)
4200 {
4201 vim_free(pt);
4202 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004203 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004204 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004205 for (i = 0; i < arg_len; i++)
4206 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4207 if (lv_len > 0)
4208 for (li = list->lv_first; li != NULL;
4209 li = li->li_next)
4210 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004211 }
4212
4213 /* For "function(dict.func, [], dict)" and "func" is a partial
4214 * use "dict". That is backwards compatible. */
4215 if (dict_idx > 0)
4216 {
4217 /* The dict is bound explicitly, pt_auto is FALSE. */
4218 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4219 ++pt->pt_dict->dv_refcount;
4220 }
4221 else if (arg_pt != NULL)
4222 {
4223 /* If the dict was bound automatically the result is also
4224 * bound automatically. */
4225 pt->pt_dict = arg_pt->pt_dict;
4226 pt->pt_auto = arg_pt->pt_auto;
4227 if (pt->pt_dict != NULL)
4228 ++pt->pt_dict->dv_refcount;
4229 }
4230
4231 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004232 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4233 {
4234 pt->pt_func = arg_pt->pt_func;
4235 func_ptr_ref(pt->pt_func);
4236 vim_free(name);
4237 }
4238 else if (is_funcref)
4239 {
4240 pt->pt_func = find_func(trans_name);
4241 func_ptr_ref(pt->pt_func);
4242 vim_free(name);
4243 }
4244 else
4245 {
4246 pt->pt_name = name;
4247 func_ref(name);
4248 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004249 }
4250 rettv->v_type = VAR_PARTIAL;
4251 rettv->vval.v_partial = pt;
4252 }
4253 else
4254 {
4255 /* result is a VAR_FUNC */
4256 rettv->v_type = VAR_FUNC;
4257 rettv->vval.v_string = name;
4258 func_ref(name);
4259 }
4260 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004261theend:
4262 vim_free(trans_name);
4263}
4264
4265/*
4266 * "funcref()" function
4267 */
4268 static void
4269f_funcref(typval_T *argvars, typval_T *rettv)
4270{
4271 common_function(argvars, rettv, TRUE);
4272}
4273
4274/*
4275 * "function()" function
4276 */
4277 static void
4278f_function(typval_T *argvars, typval_T *rettv)
4279{
4280 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004281}
4282
4283/*
4284 * "garbagecollect()" function
4285 */
4286 static void
4287f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4288{
4289 /* This is postponed until we are back at the toplevel, because we may be
4290 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4291 want_garbage_collect = TRUE;
4292
4293 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
4294 garbage_collect_at_exit = TRUE;
4295}
4296
4297/*
4298 * "get()" function
4299 */
4300 static void
4301f_get(typval_T *argvars, typval_T *rettv)
4302{
4303 listitem_T *li;
4304 list_T *l;
4305 dictitem_T *di;
4306 dict_T *d;
4307 typval_T *tv = NULL;
4308
4309 if (argvars[0].v_type == VAR_LIST)
4310 {
4311 if ((l = argvars[0].vval.v_list) != NULL)
4312 {
4313 int error = FALSE;
4314
4315 li = list_find(l, (long)get_tv_number_chk(&argvars[1], &error));
4316 if (!error && li != NULL)
4317 tv = &li->li_tv;
4318 }
4319 }
4320 else if (argvars[0].v_type == VAR_DICT)
4321 {
4322 if ((d = argvars[0].vval.v_dict) != NULL)
4323 {
4324 di = dict_find(d, get_tv_string(&argvars[1]), -1);
4325 if (di != NULL)
4326 tv = &di->di_tv;
4327 }
4328 }
4329 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4330 {
4331 partial_T *pt;
4332 partial_T fref_pt;
4333
4334 if (argvars[0].v_type == VAR_PARTIAL)
4335 pt = argvars[0].vval.v_partial;
4336 else
4337 {
4338 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4339 fref_pt.pt_name = argvars[0].vval.v_string;
4340 pt = &fref_pt;
4341 }
4342
4343 if (pt != NULL)
4344 {
4345 char_u *what = get_tv_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004346 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004347
4348 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4349 {
4350 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004351 n = partial_name(pt);
4352 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004353 rettv->vval.v_string = NULL;
4354 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004355 {
4356 rettv->vval.v_string = vim_strsave(n);
4357 if (rettv->v_type == VAR_FUNC)
4358 func_ref(rettv->vval.v_string);
4359 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004360 }
4361 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004362 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004363 else if (STRCMP(what, "args") == 0)
4364 {
4365 rettv->v_type = VAR_LIST;
4366 if (rettv_list_alloc(rettv) == OK)
4367 {
4368 int i;
4369
4370 for (i = 0; i < pt->pt_argc; ++i)
4371 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4372 }
4373 }
4374 else
4375 EMSG2(_(e_invarg2), what);
4376 return;
4377 }
4378 }
4379 else
4380 EMSG2(_(e_listdictarg), "get()");
4381
4382 if (tv == NULL)
4383 {
4384 if (argvars[2].v_type != VAR_UNKNOWN)
4385 copy_tv(&argvars[2], rettv);
4386 }
4387 else
4388 copy_tv(tv, rettv);
4389}
4390
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004391#ifdef FEAT_SIGNS
4392/*
4393 * Returns information about signs placed in a buffer as list of dicts.
4394 */
4395 static void
4396get_buffer_signs(buf_T *buf, list_T *l)
4397{
4398 signlist_T *sign;
4399
4400 for (sign = buf->b_signlist; sign; sign = sign->next)
4401 {
4402 dict_T *d = dict_alloc();
4403
4404 if (d != NULL)
4405 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02004406 dict_add_number(d, "id", sign->id);
4407 dict_add_number(d, "lnum", sign->lnum);
4408 dict_add_string(d, "name", sign_typenr2name(sign->typenr));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004409
4410 list_append_dict(l, d);
4411 }
4412 }
4413}
4414#endif
4415
4416/*
4417 * Returns buffer options, variables and other attributes in a dictionary.
4418 */
4419 static dict_T *
4420get_buffer_info(buf_T *buf)
4421{
4422 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004423 tabpage_T *tp;
4424 win_T *wp;
4425 list_T *windows;
4426
4427 dict = dict_alloc();
4428 if (dict == NULL)
4429 return NULL;
4430
Bram Moolenaare0be1672018-07-08 16:50:37 +02004431 dict_add_number(dict, "bufnr", buf->b_fnum);
4432 dict_add_string(dict, "name", buf->b_ffname);
4433 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4434 : buflist_findlnum(buf));
4435 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4436 dict_add_number(dict, "listed", buf->b_p_bl);
4437 dict_add_number(dict, "changed", bufIsChanged(buf));
4438 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4439 dict_add_number(dict, "hidden",
4440 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004441
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004442 /* Get a reference to buffer variables */
4443 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004444
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004445 /* List of windows displaying this buffer */
4446 windows = list_alloc();
4447 if (windows != NULL)
4448 {
4449 FOR_ALL_TAB_WINDOWS(tp, wp)
4450 if (wp->w_buffer == buf)
4451 list_append_number(windows, (varnumber_T)wp->w_id);
4452 dict_add_list(dict, "windows", windows);
4453 }
4454
4455#ifdef FEAT_SIGNS
4456 if (buf->b_signlist != NULL)
4457 {
4458 /* List of signs placed in this buffer */
4459 list_T *signs = list_alloc();
4460 if (signs != NULL)
4461 {
4462 get_buffer_signs(buf, signs);
4463 dict_add_list(dict, "signs", signs);
4464 }
4465 }
4466#endif
4467
4468 return dict;
4469}
4470
4471/*
4472 * "getbufinfo()" function
4473 */
4474 static void
4475f_getbufinfo(typval_T *argvars, typval_T *rettv)
4476{
4477 buf_T *buf = NULL;
4478 buf_T *argbuf = NULL;
4479 dict_T *d;
4480 int filtered = FALSE;
4481 int sel_buflisted = FALSE;
4482 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004483 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004484
4485 if (rettv_list_alloc(rettv) != OK)
4486 return;
4487
4488 /* List of all the buffers or selected buffers */
4489 if (argvars[0].v_type == VAR_DICT)
4490 {
4491 dict_T *sel_d = argvars[0].vval.v_dict;
4492
4493 if (sel_d != NULL)
4494 {
4495 dictitem_T *di;
4496
4497 filtered = TRUE;
4498
4499 di = dict_find(sel_d, (char_u *)"buflisted", -1);
4500 if (di != NULL && get_tv_number(&di->di_tv))
4501 sel_buflisted = TRUE;
4502
4503 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
4504 if (di != NULL && get_tv_number(&di->di_tv))
4505 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004506
4507 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
4508 if (di != NULL && get_tv_number(&di->di_tv))
4509 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004510 }
4511 }
4512 else if (argvars[0].v_type != VAR_UNKNOWN)
4513 {
4514 /* Information about one buffer. Argument specifies the buffer */
4515 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4516 ++emsg_off;
4517 argbuf = get_buf_tv(&argvars[0], FALSE);
4518 --emsg_off;
4519 if (argbuf == NULL)
4520 return;
4521 }
4522
4523 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004524 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004525 {
4526 if (argbuf != NULL && argbuf != buf)
4527 continue;
4528 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004529 || (sel_buflisted && !buf->b_p_bl)
4530 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004531 continue;
4532
4533 d = get_buffer_info(buf);
4534 if (d != NULL)
4535 list_append_dict(rettv->vval.v_list, d);
4536 if (argbuf != NULL)
4537 return;
4538 }
4539}
4540
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004541/*
4542 * Get line or list of lines from buffer "buf" into "rettv".
4543 * Return a range (from start to end) of lines in rettv from the specified
4544 * buffer.
4545 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4546 */
4547 static void
4548get_buffer_lines(
4549 buf_T *buf,
4550 linenr_T start,
4551 linenr_T end,
4552 int retlist,
4553 typval_T *rettv)
4554{
4555 char_u *p;
4556
4557 rettv->v_type = VAR_STRING;
4558 rettv->vval.v_string = NULL;
4559 if (retlist && rettv_list_alloc(rettv) == FAIL)
4560 return;
4561
4562 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4563 return;
4564
4565 if (!retlist)
4566 {
4567 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4568 p = ml_get_buf(buf, start, FALSE);
4569 else
4570 p = (char_u *)"";
4571 rettv->vval.v_string = vim_strsave(p);
4572 }
4573 else
4574 {
4575 if (end < start)
4576 return;
4577
4578 if (start < 1)
4579 start = 1;
4580 if (end > buf->b_ml.ml_line_count)
4581 end = buf->b_ml.ml_line_count;
4582 while (start <= end)
4583 if (list_append_string(rettv->vval.v_list,
4584 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4585 break;
4586 }
4587}
4588
4589/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004590 * "getbufline()" function
4591 */
4592 static void
4593f_getbufline(typval_T *argvars, typval_T *rettv)
4594{
4595 linenr_T lnum;
4596 linenr_T end;
4597 buf_T *buf;
4598
4599 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4600 ++emsg_off;
4601 buf = get_buf_tv(&argvars[0], FALSE);
4602 --emsg_off;
4603
4604 lnum = get_tv_lnum_buf(&argvars[1], buf);
4605 if (argvars[2].v_type == VAR_UNKNOWN)
4606 end = lnum;
4607 else
4608 end = get_tv_lnum_buf(&argvars[2], buf);
4609
4610 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4611}
4612
4613/*
4614 * "getbufvar()" function
4615 */
4616 static void
4617f_getbufvar(typval_T *argvars, typval_T *rettv)
4618{
4619 buf_T *buf;
4620 buf_T *save_curbuf;
4621 char_u *varname;
4622 dictitem_T *v;
4623 int done = FALSE;
4624
4625 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4626 varname = get_tv_string_chk(&argvars[1]);
4627 ++emsg_off;
4628 buf = get_buf_tv(&argvars[0], FALSE);
4629
4630 rettv->v_type = VAR_STRING;
4631 rettv->vval.v_string = NULL;
4632
4633 if (buf != NULL && varname != NULL)
4634 {
4635 /* set curbuf to be our buf, temporarily */
4636 save_curbuf = curbuf;
4637 curbuf = buf;
4638
Bram Moolenaar30567352016-08-27 21:25:44 +02004639 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004640 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004641 if (varname[1] == NUL)
4642 {
4643 /* get all buffer-local options in a dict */
4644 dict_T *opts = get_winbuf_options(TRUE);
4645
4646 if (opts != NULL)
4647 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004648 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004649 done = TRUE;
4650 }
4651 }
4652 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4653 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004654 done = TRUE;
4655 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004656 else
4657 {
4658 /* Look up the variable. */
4659 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4660 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4661 'b', varname, FALSE);
4662 if (v != NULL)
4663 {
4664 copy_tv(&v->di_tv, rettv);
4665 done = TRUE;
4666 }
4667 }
4668
4669 /* restore previous notion of curbuf */
4670 curbuf = save_curbuf;
4671 }
4672
4673 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4674 /* use the default value */
4675 copy_tv(&argvars[2], rettv);
4676
4677 --emsg_off;
4678}
4679
4680/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004681 * "getchangelist()" function
4682 */
4683 static void
4684f_getchangelist(typval_T *argvars, typval_T *rettv)
4685{
4686#ifdef FEAT_JUMPLIST
4687 buf_T *buf;
4688 int i;
4689 list_T *l;
4690 dict_T *d;
4691#endif
4692
4693 if (rettv_list_alloc(rettv) != OK)
4694 return;
4695
4696#ifdef FEAT_JUMPLIST
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004697 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4698 ++emsg_off;
4699 buf = get_buf_tv(&argvars[0], FALSE);
4700 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004701 if (buf == NULL)
4702 return;
4703
4704 l = list_alloc();
4705 if (l == NULL)
4706 return;
4707
4708 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4709 return;
4710 /*
4711 * The current window change list index tracks only the position in the
4712 * current buffer change list. For other buffers, use the change list
4713 * length as the current index.
4714 */
4715 list_append_number(rettv->vval.v_list,
4716 (varnumber_T)((buf == curwin->w_buffer)
4717 ? curwin->w_changelistidx : buf->b_changelistlen));
4718
4719 for (i = 0; i < buf->b_changelistlen; ++i)
4720 {
4721 if (buf->b_changelist[i].lnum == 0)
4722 continue;
4723 if ((d = dict_alloc()) == NULL)
4724 return;
4725 if (list_append_dict(l, d) == FAIL)
4726 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004727 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4728 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004729# ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +02004730 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004731# endif
4732 }
4733#endif
4734}
4735/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004736 * "getchar()" function
4737 */
4738 static void
4739f_getchar(typval_T *argvars, typval_T *rettv)
4740{
4741 varnumber_T n;
4742 int error = FALSE;
4743
Bram Moolenaar84d93902018-09-11 20:10:20 +02004744#ifdef MESSAGE_QUEUE
4745 // vpeekc() used to check for messages, but that caused problems, invoking
4746 // a callback where it was not expected. Some plugins use getchar(1) in a
4747 // loop to await a message, therefore make sure we check for messages here.
4748 parse_queued_messages();
4749#endif
4750
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004751 /* Position the cursor. Needed after a message that ends in a space. */
4752 windgoto(msg_row, msg_col);
4753
4754 ++no_mapping;
4755 ++allow_keys;
4756 for (;;)
4757 {
4758 if (argvars[0].v_type == VAR_UNKNOWN)
4759 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004760 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004761 else if (get_tv_number_chk(&argvars[0], &error) == 1)
4762 /* getchar(1): only check if char avail */
4763 n = vpeekc_any();
4764 else if (error || vpeekc_any() == NUL)
4765 /* illegal argument or getchar(0) and no char avail: return zero */
4766 n = 0;
4767 else
4768 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004769 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004770
4771 if (n == K_IGNORE)
4772 continue;
4773 break;
4774 }
4775 --no_mapping;
4776 --allow_keys;
4777
4778 set_vim_var_nr(VV_MOUSE_WIN, 0);
4779 set_vim_var_nr(VV_MOUSE_WINID, 0);
4780 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4781 set_vim_var_nr(VV_MOUSE_COL, 0);
4782
4783 rettv->vval.v_number = n;
4784 if (IS_SPECIAL(n) || mod_mask != 0)
4785 {
4786 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4787 int i = 0;
4788
4789 /* Turn a special key into three bytes, plus modifier. */
4790 if (mod_mask != 0)
4791 {
4792 temp[i++] = K_SPECIAL;
4793 temp[i++] = KS_MODIFIER;
4794 temp[i++] = mod_mask;
4795 }
4796 if (IS_SPECIAL(n))
4797 {
4798 temp[i++] = K_SPECIAL;
4799 temp[i++] = K_SECOND(n);
4800 temp[i++] = K_THIRD(n);
4801 }
4802#ifdef FEAT_MBYTE
4803 else if (has_mbyte)
4804 i += (*mb_char2bytes)(n, temp + i);
4805#endif
4806 else
4807 temp[i++] = n;
4808 temp[i++] = NUL;
4809 rettv->v_type = VAR_STRING;
4810 rettv->vval.v_string = vim_strsave(temp);
4811
4812#ifdef FEAT_MOUSE
4813 if (is_mouse_key(n))
4814 {
4815 int row = mouse_row;
4816 int col = mouse_col;
4817 win_T *win;
4818 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004819 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004820 int winnr = 1;
4821
4822 if (row >= 0 && col >= 0)
4823 {
4824 /* Find the window at the mouse coordinates and compute the
4825 * text position. */
4826 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004827 if (win == NULL)
4828 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004829 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004830 for (wp = firstwin; wp != win; wp = wp->w_next)
4831 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004832 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4833 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4834 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4835 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4836 }
4837 }
4838#endif
4839 }
4840}
4841
4842/*
4843 * "getcharmod()" function
4844 */
4845 static void
4846f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4847{
4848 rettv->vval.v_number = mod_mask;
4849}
4850
4851/*
4852 * "getcharsearch()" function
4853 */
4854 static void
4855f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4856{
4857 if (rettv_dict_alloc(rettv) != FAIL)
4858 {
4859 dict_T *dict = rettv->vval.v_dict;
4860
Bram Moolenaare0be1672018-07-08 16:50:37 +02004861 dict_add_string(dict, "char", last_csearch());
4862 dict_add_number(dict, "forward", last_csearch_forward());
4863 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004864 }
4865}
4866
4867/*
4868 * "getcmdline()" function
4869 */
4870 static void
4871f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4872{
4873 rettv->v_type = VAR_STRING;
4874 rettv->vval.v_string = get_cmdline_str();
4875}
4876
4877/*
4878 * "getcmdpos()" function
4879 */
4880 static void
4881f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4882{
4883 rettv->vval.v_number = get_cmdline_pos() + 1;
4884}
4885
4886/*
4887 * "getcmdtype()" function
4888 */
4889 static void
4890f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4891{
4892 rettv->v_type = VAR_STRING;
4893 rettv->vval.v_string = alloc(2);
4894 if (rettv->vval.v_string != NULL)
4895 {
4896 rettv->vval.v_string[0] = get_cmdline_type();
4897 rettv->vval.v_string[1] = NUL;
4898 }
4899}
4900
4901/*
4902 * "getcmdwintype()" function
4903 */
4904 static void
4905f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4906{
4907 rettv->v_type = VAR_STRING;
4908 rettv->vval.v_string = NULL;
4909#ifdef FEAT_CMDWIN
4910 rettv->vval.v_string = alloc(2);
4911 if (rettv->vval.v_string != NULL)
4912 {
4913 rettv->vval.v_string[0] = cmdwin_type;
4914 rettv->vval.v_string[1] = NUL;
4915 }
4916#endif
4917}
4918
4919#if defined(FEAT_CMDL_COMPL)
4920/*
4921 * "getcompletion()" function
4922 */
4923 static void
4924f_getcompletion(typval_T *argvars, typval_T *rettv)
4925{
4926 char_u *pat;
4927 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004928 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004929 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
4930 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004931
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004932 if (argvars[2].v_type != VAR_UNKNOWN)
4933 filtered = get_tv_number_chk(&argvars[2], NULL);
4934
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004935 if (p_wic)
4936 options |= WILD_ICASE;
4937
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004938 /* For filtered results, 'wildignore' is used */
4939 if (!filtered)
4940 options |= WILD_KEEP_ALL;
4941
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004942 ExpandInit(&xpc);
4943 xpc.xp_pattern = get_tv_string(&argvars[0]);
4944 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4945 xpc.xp_context = cmdcomplete_str_to_type(get_tv_string(&argvars[1]));
4946 if (xpc.xp_context == EXPAND_NOTHING)
4947 {
4948 if (argvars[1].v_type == VAR_STRING)
4949 EMSG2(_(e_invarg2), argvars[1].vval.v_string);
4950 else
4951 EMSG(_(e_invarg));
4952 return;
4953 }
4954
4955# if defined(FEAT_MENU)
4956 if (xpc.xp_context == EXPAND_MENUS)
4957 {
4958 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4959 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4960 }
4961# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02004962#ifdef FEAT_CSCOPE
4963 if (xpc.xp_context == EXPAND_CSCOPE)
4964 {
4965 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4966 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4967 }
4968#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02004969#ifdef FEAT_SIGNS
4970 if (xpc.xp_context == EXPAND_SIGN)
4971 {
4972 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4973 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4974 }
4975#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004976
4977 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4978 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
4979 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004980 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004981
4982 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4983
4984 for (i = 0; i < xpc.xp_numfiles; i++)
4985 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4986 }
4987 vim_free(pat);
4988 ExpandCleanup(&xpc);
4989}
4990#endif
4991
4992/*
4993 * "getcwd()" function
4994 */
4995 static void
4996f_getcwd(typval_T *argvars, typval_T *rettv)
4997{
4998 win_T *wp = NULL;
4999 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01005000 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005001
5002 rettv->v_type = VAR_STRING;
5003 rettv->vval.v_string = NULL;
5004
Bram Moolenaar54591292018-02-09 20:53:59 +01005005 if (argvars[0].v_type == VAR_NUMBER && argvars[0].vval.v_number == -1)
5006 global = TRUE;
5007 else
5008 wp = find_tabwin(&argvars[0], &argvars[1]);
5009
5010 if (wp != NULL && wp->w_localdir != NULL)
5011 rettv->vval.v_string = vim_strsave(wp->w_localdir);
5012 else if (wp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005013 {
Bram Moolenaar54591292018-02-09 20:53:59 +01005014 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005015 rettv->vval.v_string = vim_strsave(globaldir);
5016 else
5017 {
5018 cwd = alloc(MAXPATHL);
5019 if (cwd != NULL)
5020 {
5021 if (mch_dirname(cwd, MAXPATHL) != FAIL)
5022 rettv->vval.v_string = vim_strsave(cwd);
5023 vim_free(cwd);
5024 }
5025 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005026 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02005027#ifdef BACKSLASH_IN_FILENAME
5028 if (rettv->vval.v_string != NULL)
5029 slash_adjust(rettv->vval.v_string);
5030#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005031}
5032
5033/*
5034 * "getfontname()" function
5035 */
5036 static void
5037f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
5038{
5039 rettv->v_type = VAR_STRING;
5040 rettv->vval.v_string = NULL;
5041#ifdef FEAT_GUI
5042 if (gui.in_use)
5043 {
5044 GuiFont font;
5045 char_u *name = NULL;
5046
5047 if (argvars[0].v_type == VAR_UNKNOWN)
5048 {
5049 /* Get the "Normal" font. Either the name saved by
5050 * hl_set_font_name() or from the font ID. */
5051 font = gui.norm_font;
5052 name = hl_get_font_name();
5053 }
5054 else
5055 {
5056 name = get_tv_string(&argvars[0]);
5057 if (STRCMP(name, "*") == 0) /* don't use font dialog */
5058 return;
5059 font = gui_mch_get_font(name, FALSE);
5060 if (font == NOFONT)
5061 return; /* Invalid font name, return empty string. */
5062 }
5063 rettv->vval.v_string = gui_mch_get_fontname(font, name);
5064 if (argvars[0].v_type != VAR_UNKNOWN)
5065 gui_mch_free_font(font);
5066 }
5067#endif
5068}
5069
5070/*
5071 * "getfperm({fname})" function
5072 */
5073 static void
5074f_getfperm(typval_T *argvars, typval_T *rettv)
5075{
5076 char_u *fname;
5077 stat_T st;
5078 char_u *perm = NULL;
5079 char_u flags[] = "rwx";
5080 int i;
5081
5082 fname = get_tv_string(&argvars[0]);
5083
5084 rettv->v_type = VAR_STRING;
5085 if (mch_stat((char *)fname, &st) >= 0)
5086 {
5087 perm = vim_strsave((char_u *)"---------");
5088 if (perm != NULL)
5089 {
5090 for (i = 0; i < 9; i++)
5091 {
5092 if (st.st_mode & (1 << (8 - i)))
5093 perm[i] = flags[i % 3];
5094 }
5095 }
5096 }
5097 rettv->vval.v_string = perm;
5098}
5099
5100/*
5101 * "getfsize({fname})" function
5102 */
5103 static void
5104f_getfsize(typval_T *argvars, typval_T *rettv)
5105{
5106 char_u *fname;
5107 stat_T st;
5108
5109 fname = get_tv_string(&argvars[0]);
5110
5111 rettv->v_type = VAR_NUMBER;
5112
5113 if (mch_stat((char *)fname, &st) >= 0)
5114 {
5115 if (mch_isdir(fname))
5116 rettv->vval.v_number = 0;
5117 else
5118 {
5119 rettv->vval.v_number = (varnumber_T)st.st_size;
5120
5121 /* non-perfect check for overflow */
5122 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5123 rettv->vval.v_number = -2;
5124 }
5125 }
5126 else
5127 rettv->vval.v_number = -1;
5128}
5129
5130/*
5131 * "getftime({fname})" function
5132 */
5133 static void
5134f_getftime(typval_T *argvars, typval_T *rettv)
5135{
5136 char_u *fname;
5137 stat_T st;
5138
5139 fname = get_tv_string(&argvars[0]);
5140
5141 if (mch_stat((char *)fname, &st) >= 0)
5142 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5143 else
5144 rettv->vval.v_number = -1;
5145}
5146
5147/*
5148 * "getftype({fname})" function
5149 */
5150 static void
5151f_getftype(typval_T *argvars, typval_T *rettv)
5152{
5153 char_u *fname;
5154 stat_T st;
5155 char_u *type = NULL;
5156 char *t;
5157
5158 fname = get_tv_string(&argvars[0]);
5159
5160 rettv->v_type = VAR_STRING;
5161 if (mch_lstat((char *)fname, &st) >= 0)
5162 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005163 if (S_ISREG(st.st_mode))
5164 t = "file";
5165 else if (S_ISDIR(st.st_mode))
5166 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005167 else if (S_ISLNK(st.st_mode))
5168 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005169 else if (S_ISBLK(st.st_mode))
5170 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005171 else if (S_ISCHR(st.st_mode))
5172 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005173 else if (S_ISFIFO(st.st_mode))
5174 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005175 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005176 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005177 else
5178 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005179 type = vim_strsave((char_u *)t);
5180 }
5181 rettv->vval.v_string = type;
5182}
5183
5184/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005185 * "getjumplist()" function
5186 */
5187 static void
5188f_getjumplist(typval_T *argvars, typval_T *rettv)
5189{
5190#ifdef FEAT_JUMPLIST
5191 win_T *wp;
5192 int i;
5193 list_T *l;
5194 dict_T *d;
5195#endif
5196
5197 if (rettv_list_alloc(rettv) != OK)
5198 return;
5199
5200#ifdef FEAT_JUMPLIST
5201 wp = find_tabwin(&argvars[0], &argvars[1]);
5202 if (wp == NULL)
5203 return;
5204
5205 l = list_alloc();
5206 if (l == NULL)
5207 return;
5208
5209 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5210 return;
5211 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5212
Bram Moolenaar48679742018-02-13 13:33:29 +01005213 cleanup_jumplist(wp, TRUE);
5214
Bram Moolenaar4f505882018-02-10 21:06:32 +01005215 for (i = 0; i < wp->w_jumplistlen; ++i)
5216 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005217 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5218 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005219 if ((d = dict_alloc()) == NULL)
5220 return;
5221 if (list_append_dict(l, d) == FAIL)
5222 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005223 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5224 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005225# ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +02005226 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005227# endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005228 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005229 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005230 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005231 }
5232#endif
5233}
5234
5235/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005236 * "getline(lnum, [end])" function
5237 */
5238 static void
5239f_getline(typval_T *argvars, typval_T *rettv)
5240{
5241 linenr_T lnum;
5242 linenr_T end;
5243 int retlist;
5244
5245 lnum = get_tv_lnum(argvars);
5246 if (argvars[1].v_type == VAR_UNKNOWN)
5247 {
5248 end = 0;
5249 retlist = FALSE;
5250 }
5251 else
5252 {
5253 end = get_tv_lnum(&argvars[1]);
5254 retlist = TRUE;
5255 }
5256
5257 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5258}
5259
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005260#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005261 static void
5262get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5263{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005264 if (what_arg->v_type == VAR_UNKNOWN)
5265 {
5266 if (rettv_list_alloc(rettv) == OK)
5267 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005268 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005269 }
5270 else
5271 {
5272 if (rettv_dict_alloc(rettv) == OK)
5273 if (is_qf || (wp != NULL))
5274 {
5275 if (what_arg->v_type == VAR_DICT)
5276 {
5277 dict_T *d = what_arg->vval.v_dict;
5278
5279 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005280 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005281 }
5282 else
5283 EMSG(_(e_dictreq));
5284 }
5285 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005286}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005287#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005288
5289/*
5290 * "getloclist()" function
5291 */
5292 static void
5293f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5294{
5295#ifdef FEAT_QUICKFIX
5296 win_T *wp;
5297
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005298 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005299 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5300#endif
5301}
5302
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005303/*
5304 * "getmatches()" function
5305 */
5306 static void
5307f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5308{
5309#ifdef FEAT_SEARCH_EXTRA
5310 dict_T *dict;
5311 matchitem_T *cur = curwin->w_match_head;
5312 int i;
5313
5314 if (rettv_list_alloc(rettv) == OK)
5315 {
5316 while (cur != NULL)
5317 {
5318 dict = dict_alloc();
5319 if (dict == NULL)
5320 return;
5321 if (cur->match.regprog == NULL)
5322 {
5323 /* match added with matchaddpos() */
5324 for (i = 0; i < MAXPOSMATCH; ++i)
5325 {
5326 llpos_T *llpos;
5327 char buf[6];
5328 list_T *l;
5329
5330 llpos = &cur->pos.pos[i];
5331 if (llpos->lnum == 0)
5332 break;
5333 l = list_alloc();
5334 if (l == NULL)
5335 break;
5336 list_append_number(l, (varnumber_T)llpos->lnum);
5337 if (llpos->col > 0)
5338 {
5339 list_append_number(l, (varnumber_T)llpos->col);
5340 list_append_number(l, (varnumber_T)llpos->len);
5341 }
5342 sprintf(buf, "pos%d", i + 1);
5343 dict_add_list(dict, buf, l);
5344 }
5345 }
5346 else
5347 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02005348 dict_add_string(dict, "pattern", cur->pattern);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005349 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02005350 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
5351 dict_add_number(dict, "priority", (long)cur->priority);
5352 dict_add_number(dict, "id", (long)cur->id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005353# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
5354 if (cur->conceal_char)
5355 {
5356 char_u buf[MB_MAXBYTES + 1];
5357
5358 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005359 dict_add_string(dict, "conceal", (char_u *)&buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005360 }
5361# endif
5362 list_append_dict(rettv->vval.v_list, dict);
5363 cur = cur->next;
5364 }
5365 }
5366#endif
5367}
5368
5369/*
5370 * "getpid()" function
5371 */
5372 static void
5373f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5374{
5375 rettv->vval.v_number = mch_get_pid();
5376}
5377
5378 static void
5379getpos_both(
5380 typval_T *argvars,
5381 typval_T *rettv,
5382 int getcurpos)
5383{
5384 pos_T *fp;
5385 list_T *l;
5386 int fnum = -1;
5387
5388 if (rettv_list_alloc(rettv) == OK)
5389 {
5390 l = rettv->vval.v_list;
5391 if (getcurpos)
5392 fp = &curwin->w_cursor;
5393 else
5394 fp = var2fpos(&argvars[0], TRUE, &fnum);
5395 if (fnum != -1)
5396 list_append_number(l, (varnumber_T)fnum);
5397 else
5398 list_append_number(l, (varnumber_T)0);
5399 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5400 : (varnumber_T)0);
5401 list_append_number(l, (fp != NULL)
5402 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5403 : (varnumber_T)0);
5404 list_append_number(l,
5405#ifdef FEAT_VIRTUALEDIT
5406 (fp != NULL) ? (varnumber_T)fp->coladd :
5407#endif
5408 (varnumber_T)0);
5409 if (getcurpos)
5410 {
5411 update_curswant();
5412 list_append_number(l, curwin->w_curswant == MAXCOL ?
5413 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
5414 }
5415 }
5416 else
5417 rettv->vval.v_number = FALSE;
5418}
5419
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005420/*
5421 * "getcurpos()" function
5422 */
5423 static void
5424f_getcurpos(typval_T *argvars, typval_T *rettv)
5425{
5426 getpos_both(argvars, rettv, TRUE);
5427}
5428
5429/*
5430 * "getpos(string)" function
5431 */
5432 static void
5433f_getpos(typval_T *argvars, typval_T *rettv)
5434{
5435 getpos_both(argvars, rettv, FALSE);
5436}
5437
5438/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005439 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005440 */
5441 static void
5442f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5443{
5444#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005445 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005446#endif
5447}
5448
5449/*
5450 * "getreg()" function
5451 */
5452 static void
5453f_getreg(typval_T *argvars, typval_T *rettv)
5454{
5455 char_u *strregname;
5456 int regname;
5457 int arg2 = FALSE;
5458 int return_list = FALSE;
5459 int error = FALSE;
5460
5461 if (argvars[0].v_type != VAR_UNKNOWN)
5462 {
5463 strregname = get_tv_string_chk(&argvars[0]);
5464 error = strregname == NULL;
5465 if (argvars[1].v_type != VAR_UNKNOWN)
5466 {
5467 arg2 = (int)get_tv_number_chk(&argvars[1], &error);
5468 if (!error && argvars[2].v_type != VAR_UNKNOWN)
5469 return_list = (int)get_tv_number_chk(&argvars[2], &error);
5470 }
5471 }
5472 else
5473 strregname = get_vim_var_str(VV_REG);
5474
5475 if (error)
5476 return;
5477
5478 regname = (strregname == NULL ? '"' : *strregname);
5479 if (regname == 0)
5480 regname = '"';
5481
5482 if (return_list)
5483 {
5484 rettv->v_type = VAR_LIST;
5485 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5486 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5487 if (rettv->vval.v_list == NULL)
5488 (void)rettv_list_alloc(rettv);
5489 else
5490 ++rettv->vval.v_list->lv_refcount;
5491 }
5492 else
5493 {
5494 rettv->v_type = VAR_STRING;
5495 rettv->vval.v_string = get_reg_contents(regname,
5496 arg2 ? GREG_EXPR_SRC : 0);
5497 }
5498}
5499
5500/*
5501 * "getregtype()" function
5502 */
5503 static void
5504f_getregtype(typval_T *argvars, typval_T *rettv)
5505{
5506 char_u *strregname;
5507 int regname;
5508 char_u buf[NUMBUFLEN + 2];
5509 long reglen = 0;
5510
5511 if (argvars[0].v_type != VAR_UNKNOWN)
5512 {
5513 strregname = get_tv_string_chk(&argvars[0]);
5514 if (strregname == NULL) /* type error; errmsg already given */
5515 {
5516 rettv->v_type = VAR_STRING;
5517 rettv->vval.v_string = NULL;
5518 return;
5519 }
5520 }
5521 else
5522 /* Default to v:register */
5523 strregname = get_vim_var_str(VV_REG);
5524
5525 regname = (strregname == NULL ? '"' : *strregname);
5526 if (regname == 0)
5527 regname = '"';
5528
5529 buf[0] = NUL;
5530 buf[1] = NUL;
5531 switch (get_reg_type(regname, &reglen))
5532 {
5533 case MLINE: buf[0] = 'V'; break;
5534 case MCHAR: buf[0] = 'v'; break;
5535 case MBLOCK:
5536 buf[0] = Ctrl_V;
5537 sprintf((char *)buf + 1, "%ld", reglen + 1);
5538 break;
5539 }
5540 rettv->v_type = VAR_STRING;
5541 rettv->vval.v_string = vim_strsave(buf);
5542}
5543
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005544/*
5545 * Returns information (variables, options, etc.) about a tab page
5546 * as a dictionary.
5547 */
5548 static dict_T *
5549get_tabpage_info(tabpage_T *tp, int tp_idx)
5550{
5551 win_T *wp;
5552 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005553 list_T *l;
5554
5555 dict = dict_alloc();
5556 if (dict == NULL)
5557 return NULL;
5558
Bram Moolenaare0be1672018-07-08 16:50:37 +02005559 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005560
5561 l = list_alloc();
5562 if (l != NULL)
5563 {
5564 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5565 wp; wp = wp->w_next)
5566 list_append_number(l, (varnumber_T)wp->w_id);
5567 dict_add_list(dict, "windows", l);
5568 }
5569
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005570 /* Make a reference to tabpage variables */
5571 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005572
5573 return dict;
5574}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005575
5576/*
5577 * "gettabinfo()" function
5578 */
5579 static void
5580f_gettabinfo(typval_T *argvars, typval_T *rettv)
5581{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005582 tabpage_T *tp, *tparg = NULL;
5583 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005584 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005585
5586 if (rettv_list_alloc(rettv) != OK)
5587 return;
5588
5589 if (argvars[0].v_type != VAR_UNKNOWN)
5590 {
5591 /* Information about one tab page */
5592 tparg = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5593 if (tparg == NULL)
5594 return;
5595 }
5596
5597 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005598 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005599 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005600 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005601 if (tparg != NULL && tp != tparg)
5602 continue;
5603 d = get_tabpage_info(tp, tpnr);
5604 if (d != NULL)
5605 list_append_dict(rettv->vval.v_list, d);
5606 if (tparg != NULL)
5607 return;
5608 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005609}
5610
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005611/*
5612 * "gettabvar()" function
5613 */
5614 static void
5615f_gettabvar(typval_T *argvars, typval_T *rettv)
5616{
5617 win_T *oldcurwin;
5618 tabpage_T *tp, *oldtabpage;
5619 dictitem_T *v;
5620 char_u *varname;
5621 int done = FALSE;
5622
5623 rettv->v_type = VAR_STRING;
5624 rettv->vval.v_string = NULL;
5625
5626 varname = get_tv_string_chk(&argvars[1]);
5627 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5628 if (tp != NULL && varname != NULL)
5629 {
5630 /* Set tp to be our tabpage, temporarily. Also set the window to the
5631 * first window in the tabpage, otherwise the window is not valid. */
5632 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005633 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5634 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005635 {
5636 /* look up the variable */
5637 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5638 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5639 if (v != NULL)
5640 {
5641 copy_tv(&v->di_tv, rettv);
5642 done = TRUE;
5643 }
5644 }
5645
5646 /* restore previous notion of curwin */
5647 restore_win(oldcurwin, oldtabpage, TRUE);
5648 }
5649
5650 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5651 copy_tv(&argvars[2], rettv);
5652}
5653
5654/*
5655 * "gettabwinvar()" function
5656 */
5657 static void
5658f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5659{
5660 getwinvar(argvars, rettv, 1);
5661}
5662
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005663/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01005664 * "gettagstack()" function
5665 */
5666 static void
5667f_gettagstack(typval_T *argvars, typval_T *rettv)
5668{
5669 win_T *wp = curwin; // default is current window
5670
5671 if (rettv_dict_alloc(rettv) != OK)
5672 return;
5673
5674 if (argvars[0].v_type != VAR_UNKNOWN)
5675 {
5676 wp = find_win_by_nr_or_id(&argvars[0]);
5677 if (wp == NULL)
5678 return;
5679 }
5680
5681 get_tagstack(wp, rettv->vval.v_dict);
5682}
5683
5684/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005685 * Returns information about a window as a dictionary.
5686 */
5687 static dict_T *
5688get_win_info(win_T *wp, short tpnr, short winnr)
5689{
5690 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005691
5692 dict = dict_alloc();
5693 if (dict == NULL)
5694 return NULL;
5695
Bram Moolenaare0be1672018-07-08 16:50:37 +02005696 dict_add_number(dict, "tabnr", tpnr);
5697 dict_add_number(dict, "winnr", winnr);
5698 dict_add_number(dict, "winid", wp->w_id);
5699 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005700 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005701#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02005702 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005703#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005704 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005705 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005706 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005707
Bram Moolenaar69905d12017-08-13 18:14:47 +02005708#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02005709 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02005710#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005711#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02005712 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
5713 dict_add_number(dict, "loclist",
5714 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02005715#endif
5716
Bram Moolenaar30567352016-08-27 21:25:44 +02005717 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005718 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005719
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005720 return dict;
5721}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005722
5723/*
5724 * "getwininfo()" function
5725 */
5726 static void
5727f_getwininfo(typval_T *argvars, typval_T *rettv)
5728{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005729 tabpage_T *tp;
5730 win_T *wp = NULL, *wparg = NULL;
5731 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005732 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005733
5734 if (rettv_list_alloc(rettv) != OK)
5735 return;
5736
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005737 if (argvars[0].v_type != VAR_UNKNOWN)
5738 {
5739 wparg = win_id2wp(argvars);
5740 if (wparg == NULL)
5741 return;
5742 }
5743
5744 /* Collect information about either all the windows across all the tab
5745 * pages or one particular window.
5746 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005747 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005748 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005749 tabnr++;
5750 winnr = 0;
5751 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005752 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005753 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005754 if (wparg != NULL && wp != wparg)
5755 continue;
5756 d = get_win_info(wp, tabnr, winnr);
5757 if (d != NULL)
5758 list_append_dict(rettv->vval.v_list, d);
5759 if (wparg != NULL)
5760 /* found information about a specific window */
5761 return;
5762 }
5763 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005764}
5765
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005766/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005767 * "win_findbuf()" function
5768 */
5769 static void
5770f_win_findbuf(typval_T *argvars, typval_T *rettv)
5771{
5772 if (rettv_list_alloc(rettv) != FAIL)
5773 win_findbuf(argvars, rettv->vval.v_list);
5774}
5775
5776/*
5777 * "win_getid()" function
5778 */
5779 static void
5780f_win_getid(typval_T *argvars, typval_T *rettv)
5781{
5782 rettv->vval.v_number = win_getid(argvars);
5783}
5784
5785/*
5786 * "win_gotoid()" function
5787 */
5788 static void
5789f_win_gotoid(typval_T *argvars, typval_T *rettv)
5790{
5791 rettv->vval.v_number = win_gotoid(argvars);
5792}
5793
5794/*
5795 * "win_id2tabwin()" function
5796 */
5797 static void
5798f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5799{
5800 if (rettv_list_alloc(rettv) != FAIL)
5801 win_id2tabwin(argvars, rettv->vval.v_list);
5802}
5803
5804/*
5805 * "win_id2win()" function
5806 */
5807 static void
5808f_win_id2win(typval_T *argvars, typval_T *rettv)
5809{
5810 rettv->vval.v_number = win_id2win(argvars);
5811}
5812
5813/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005814 * "win_screenpos()" function
5815 */
5816 static void
5817f_win_screenpos(typval_T *argvars, typval_T *rettv)
5818{
5819 win_T *wp;
5820
5821 if (rettv_list_alloc(rettv) == FAIL)
5822 return;
5823
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005824 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005825 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5826 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5827}
5828
5829/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005830 * "getwinpos({timeout})" function
5831 */
5832 static void
5833f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
5834{
5835 int x = -1;
5836 int y = -1;
5837
5838 if (rettv_list_alloc(rettv) == FAIL)
5839 return;
5840#ifdef FEAT_GUI
5841 if (gui.in_use)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01005842 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005843# if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5844 else
5845# endif
5846#endif
5847#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5848 {
5849 varnumber_T timeout = 100;
5850
5851 if (argvars[0].v_type != VAR_UNKNOWN)
5852 timeout = get_tv_number(&argvars[0]);
5853 term_get_winpos(&x, &y, timeout);
5854 }
5855#endif
5856 list_append_number(rettv->vval.v_list, (varnumber_T)x);
5857 list_append_number(rettv->vval.v_list, (varnumber_T)y);
5858}
5859
5860
5861/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005862 * "getwinposx()" function
5863 */
5864 static void
5865f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5866{
5867 rettv->vval.v_number = -1;
5868#ifdef FEAT_GUI
5869 if (gui.in_use)
5870 {
5871 int x, y;
5872
5873 if (gui_mch_get_winpos(&x, &y) == OK)
5874 rettv->vval.v_number = x;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005875 return;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005876 }
5877#endif
5878#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5879 {
5880 int x, y;
5881
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005882 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005883 rettv->vval.v_number = x;
5884 }
5885#endif
5886}
5887
5888/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005889 * "getwinposy()" function
5890 */
5891 static void
5892f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5893{
5894 rettv->vval.v_number = -1;
5895#ifdef FEAT_GUI
5896 if (gui.in_use)
5897 {
5898 int x, y;
5899
5900 if (gui_mch_get_winpos(&x, &y) == OK)
5901 rettv->vval.v_number = y;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005902 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005903 }
5904#endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005905#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5906 {
5907 int x, y;
5908
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005909 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005910 rettv->vval.v_number = y;
5911 }
5912#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005913}
5914
5915/*
5916 * "getwinvar()" function
5917 */
5918 static void
5919f_getwinvar(typval_T *argvars, typval_T *rettv)
5920{
5921 getwinvar(argvars, rettv, 0);
5922}
5923
5924/*
5925 * "glob()" function
5926 */
5927 static void
5928f_glob(typval_T *argvars, typval_T *rettv)
5929{
5930 int options = WILD_SILENT|WILD_USE_NL;
5931 expand_T xpc;
5932 int error = FALSE;
5933
5934 /* When the optional second argument is non-zero, don't remove matches
5935 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5936 rettv->v_type = VAR_STRING;
5937 if (argvars[1].v_type != VAR_UNKNOWN)
5938 {
5939 if (get_tv_number_chk(&argvars[1], &error))
5940 options |= WILD_KEEP_ALL;
5941 if (argvars[2].v_type != VAR_UNKNOWN)
5942 {
5943 if (get_tv_number_chk(&argvars[2], &error))
5944 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005945 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005946 }
5947 if (argvars[3].v_type != VAR_UNKNOWN
5948 && get_tv_number_chk(&argvars[3], &error))
5949 options |= WILD_ALLLINKS;
5950 }
5951 }
5952 if (!error)
5953 {
5954 ExpandInit(&xpc);
5955 xpc.xp_context = EXPAND_FILES;
5956 if (p_wic)
5957 options += WILD_ICASE;
5958 if (rettv->v_type == VAR_STRING)
5959 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
5960 NULL, options, WILD_ALL);
5961 else if (rettv_list_alloc(rettv) != FAIL)
5962 {
5963 int i;
5964
5965 ExpandOne(&xpc, get_tv_string(&argvars[0]),
5966 NULL, options, WILD_ALL_KEEP);
5967 for (i = 0; i < xpc.xp_numfiles; i++)
5968 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5969
5970 ExpandCleanup(&xpc);
5971 }
5972 }
5973 else
5974 rettv->vval.v_string = NULL;
5975}
5976
5977/*
5978 * "globpath()" function
5979 */
5980 static void
5981f_globpath(typval_T *argvars, typval_T *rettv)
5982{
5983 int flags = 0;
5984 char_u buf1[NUMBUFLEN];
5985 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
5986 int error = FALSE;
5987 garray_T ga;
5988 int i;
5989
5990 /* When the optional second argument is non-zero, don't remove matches
5991 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5992 rettv->v_type = VAR_STRING;
5993 if (argvars[2].v_type != VAR_UNKNOWN)
5994 {
5995 if (get_tv_number_chk(&argvars[2], &error))
5996 flags |= WILD_KEEP_ALL;
5997 if (argvars[3].v_type != VAR_UNKNOWN)
5998 {
5999 if (get_tv_number_chk(&argvars[3], &error))
6000 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006001 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006002 }
6003 if (argvars[4].v_type != VAR_UNKNOWN
6004 && get_tv_number_chk(&argvars[4], &error))
6005 flags |= WILD_ALLLINKS;
6006 }
6007 }
6008 if (file != NULL && !error)
6009 {
6010 ga_init2(&ga, (int)sizeof(char_u *), 10);
6011 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
6012 if (rettv->v_type == VAR_STRING)
6013 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
6014 else if (rettv_list_alloc(rettv) != FAIL)
6015 for (i = 0; i < ga.ga_len; ++i)
6016 list_append_string(rettv->vval.v_list,
6017 ((char_u **)(ga.ga_data))[i], -1);
6018 ga_clear_strings(&ga);
6019 }
6020 else
6021 rettv->vval.v_string = NULL;
6022}
6023
6024/*
6025 * "glob2regpat()" function
6026 */
6027 static void
6028f_glob2regpat(typval_T *argvars, typval_T *rettv)
6029{
6030 char_u *pat = get_tv_string_chk(&argvars[0]);
6031
6032 rettv->v_type = VAR_STRING;
6033 rettv->vval.v_string = (pat == NULL)
6034 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
6035}
6036
6037/* for VIM_VERSION_ defines */
6038#include "version.h"
6039
6040/*
6041 * "has()" function
6042 */
6043 static void
6044f_has(typval_T *argvars, typval_T *rettv)
6045{
6046 int i;
6047 char_u *name;
6048 int n = FALSE;
6049 static char *(has_list[]) =
6050 {
6051#ifdef AMIGA
6052 "amiga",
6053# ifdef FEAT_ARP
6054 "arp",
6055# endif
6056#endif
6057#ifdef __BEOS__
6058 "beos",
6059#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02006060#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01006061 "mac", /* Mac OS X (and, once, Mac OS Classic) */
6062 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02006063# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01006064 "macunix", /* Mac OS X, with the darwin feature */
6065 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02006066# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006067#endif
6068#ifdef __QNX__
6069 "qnx",
6070#endif
6071#ifdef UNIX
6072 "unix",
6073#endif
6074#ifdef VMS
6075 "vms",
6076#endif
6077#ifdef WIN32
6078 "win32",
6079#endif
6080#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
6081 "win32unix",
6082#endif
6083#if defined(WIN64) || defined(_WIN64)
6084 "win64",
6085#endif
6086#ifdef EBCDIC
6087 "ebcdic",
6088#endif
6089#ifndef CASE_INSENSITIVE_FILENAME
6090 "fname_case",
6091#endif
6092#ifdef HAVE_ACL
6093 "acl",
6094#endif
6095#ifdef FEAT_ARABIC
6096 "arabic",
6097#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006098 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006099#ifdef FEAT_AUTOCHDIR
6100 "autochdir",
6101#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006102#ifdef FEAT_AUTOSERVERNAME
6103 "autoservername",
6104#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006105#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006106 "balloon_eval",
6107# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
6108 "balloon_multiline",
6109# endif
6110#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006111#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006112 "balloon_eval_term",
6113#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006114#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6115 "builtin_terms",
6116# ifdef ALL_BUILTIN_TCAPS
6117 "all_builtin_terms",
6118# endif
6119#endif
6120#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
6121 || defined(FEAT_GUI_W32) \
6122 || defined(FEAT_GUI_MOTIF))
6123 "browsefilter",
6124#endif
6125#ifdef FEAT_BYTEOFF
6126 "byte_offset",
6127#endif
6128#ifdef FEAT_JOB_CHANNEL
6129 "channel",
6130#endif
6131#ifdef FEAT_CINDENT
6132 "cindent",
6133#endif
6134#ifdef FEAT_CLIENTSERVER
6135 "clientserver",
6136#endif
6137#ifdef FEAT_CLIPBOARD
6138 "clipboard",
6139#endif
6140#ifdef FEAT_CMDL_COMPL
6141 "cmdline_compl",
6142#endif
6143#ifdef FEAT_CMDHIST
6144 "cmdline_hist",
6145#endif
6146#ifdef FEAT_COMMENTS
6147 "comments",
6148#endif
6149#ifdef FEAT_CONCEAL
6150 "conceal",
6151#endif
6152#ifdef FEAT_CRYPT
6153 "cryptv",
6154 "crypt-blowfish",
6155 "crypt-blowfish2",
6156#endif
6157#ifdef FEAT_CSCOPE
6158 "cscope",
6159#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006160 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006161#ifdef CURSOR_SHAPE
6162 "cursorshape",
6163#endif
6164#ifdef DEBUG
6165 "debug",
6166#endif
6167#ifdef FEAT_CON_DIALOG
6168 "dialog_con",
6169#endif
6170#ifdef FEAT_GUI_DIALOG
6171 "dialog_gui",
6172#endif
6173#ifdef FEAT_DIFF
6174 "diff",
6175#endif
6176#ifdef FEAT_DIGRAPHS
6177 "digraphs",
6178#endif
6179#ifdef FEAT_DIRECTX
6180 "directx",
6181#endif
6182#ifdef FEAT_DND
6183 "dnd",
6184#endif
6185#ifdef FEAT_EMACS_TAGS
6186 "emacs_tags",
6187#endif
6188 "eval", /* always present, of course! */
6189 "ex_extra", /* graduated feature */
6190#ifdef FEAT_SEARCH_EXTRA
6191 "extra_search",
6192#endif
6193#ifdef FEAT_FKMAP
6194 "farsi",
6195#endif
6196#ifdef FEAT_SEARCHPATH
6197 "file_in_path",
6198#endif
6199#ifdef FEAT_FILTERPIPE
6200 "filterpipe",
6201#endif
6202#ifdef FEAT_FIND_ID
6203 "find_in_path",
6204#endif
6205#ifdef FEAT_FLOAT
6206 "float",
6207#endif
6208#ifdef FEAT_FOLDING
6209 "folding",
6210#endif
6211#ifdef FEAT_FOOTER
6212 "footer",
6213#endif
6214#if !defined(USE_SYSTEM) && defined(UNIX)
6215 "fork",
6216#endif
6217#ifdef FEAT_GETTEXT
6218 "gettext",
6219#endif
6220#ifdef FEAT_GUI
6221 "gui",
6222#endif
6223#ifdef FEAT_GUI_ATHENA
6224# ifdef FEAT_GUI_NEXTAW
6225 "gui_neXtaw",
6226# else
6227 "gui_athena",
6228# endif
6229#endif
6230#ifdef FEAT_GUI_GTK
6231 "gui_gtk",
6232# ifdef USE_GTK3
6233 "gui_gtk3",
6234# else
6235 "gui_gtk2",
6236# endif
6237#endif
6238#ifdef FEAT_GUI_GNOME
6239 "gui_gnome",
6240#endif
6241#ifdef FEAT_GUI_MAC
6242 "gui_mac",
6243#endif
6244#ifdef FEAT_GUI_MOTIF
6245 "gui_motif",
6246#endif
6247#ifdef FEAT_GUI_PHOTON
6248 "gui_photon",
6249#endif
6250#ifdef FEAT_GUI_W32
6251 "gui_win32",
6252#endif
6253#ifdef FEAT_HANGULIN
6254 "hangul_input",
6255#endif
6256#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6257 "iconv",
6258#endif
6259#ifdef FEAT_INS_EXPAND
6260 "insert_expand",
6261#endif
6262#ifdef FEAT_JOB_CHANNEL
6263 "job",
6264#endif
6265#ifdef FEAT_JUMPLIST
6266 "jumplist",
6267#endif
6268#ifdef FEAT_KEYMAP
6269 "keymap",
6270#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006271 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006272#ifdef FEAT_LANGMAP
6273 "langmap",
6274#endif
6275#ifdef FEAT_LIBCALL
6276 "libcall",
6277#endif
6278#ifdef FEAT_LINEBREAK
6279 "linebreak",
6280#endif
6281#ifdef FEAT_LISP
6282 "lispindent",
6283#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006284 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006285#ifdef FEAT_LOCALMAP
6286 "localmap",
6287#endif
6288#ifdef FEAT_LUA
6289# ifndef DYNAMIC_LUA
6290 "lua",
6291# endif
6292#endif
6293#ifdef FEAT_MENU
6294 "menu",
6295#endif
6296#ifdef FEAT_SESSION
6297 "mksession",
6298#endif
6299#ifdef FEAT_MODIFY_FNAME
6300 "modify_fname",
6301#endif
6302#ifdef FEAT_MOUSE
6303 "mouse",
6304#endif
6305#ifdef FEAT_MOUSESHAPE
6306 "mouseshape",
6307#endif
6308#if defined(UNIX) || defined(VMS)
6309# ifdef FEAT_MOUSE_DEC
6310 "mouse_dec",
6311# endif
6312# ifdef FEAT_MOUSE_GPM
6313 "mouse_gpm",
6314# endif
6315# ifdef FEAT_MOUSE_JSB
6316 "mouse_jsbterm",
6317# endif
6318# ifdef FEAT_MOUSE_NET
6319 "mouse_netterm",
6320# endif
6321# ifdef FEAT_MOUSE_PTERM
6322 "mouse_pterm",
6323# endif
6324# ifdef FEAT_MOUSE_SGR
6325 "mouse_sgr",
6326# endif
6327# ifdef FEAT_SYSMOUSE
6328 "mouse_sysmouse",
6329# endif
6330# ifdef FEAT_MOUSE_URXVT
6331 "mouse_urxvt",
6332# endif
6333# ifdef FEAT_MOUSE_XTERM
6334 "mouse_xterm",
6335# endif
6336#endif
6337#ifdef FEAT_MBYTE
6338 "multi_byte",
6339#endif
6340#ifdef FEAT_MBYTE_IME
6341 "multi_byte_ime",
6342#endif
6343#ifdef FEAT_MULTI_LANG
6344 "multi_lang",
6345#endif
6346#ifdef FEAT_MZSCHEME
6347#ifndef DYNAMIC_MZSCHEME
6348 "mzscheme",
6349#endif
6350#endif
6351#ifdef FEAT_NUM64
6352 "num64",
6353#endif
6354#ifdef FEAT_OLE
6355 "ole",
6356#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006357#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006358 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006359#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006360#ifdef FEAT_PATH_EXTRA
6361 "path_extra",
6362#endif
6363#ifdef FEAT_PERL
6364#ifndef DYNAMIC_PERL
6365 "perl",
6366#endif
6367#endif
6368#ifdef FEAT_PERSISTENT_UNDO
6369 "persistent_undo",
6370#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006371#if defined(FEAT_PYTHON)
6372 "python_compiled",
6373# if defined(DYNAMIC_PYTHON)
6374 "python_dynamic",
6375# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006376 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006377 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006378# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006379#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006380#if defined(FEAT_PYTHON3)
6381 "python3_compiled",
6382# if defined(DYNAMIC_PYTHON3)
6383 "python3_dynamic",
6384# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006385 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006386 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006387# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006388#endif
6389#ifdef FEAT_POSTSCRIPT
6390 "postscript",
6391#endif
6392#ifdef FEAT_PRINTER
6393 "printer",
6394#endif
6395#ifdef FEAT_PROFILE
6396 "profile",
6397#endif
6398#ifdef FEAT_RELTIME
6399 "reltime",
6400#endif
6401#ifdef FEAT_QUICKFIX
6402 "quickfix",
6403#endif
6404#ifdef FEAT_RIGHTLEFT
6405 "rightleft",
6406#endif
6407#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6408 "ruby",
6409#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006410 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006411#ifdef FEAT_CMDL_INFO
6412 "showcmd",
6413 "cmdline_info",
6414#endif
6415#ifdef FEAT_SIGNS
6416 "signs",
6417#endif
6418#ifdef FEAT_SMARTINDENT
6419 "smartindent",
6420#endif
6421#ifdef STARTUPTIME
6422 "startuptime",
6423#endif
6424#ifdef FEAT_STL_OPT
6425 "statusline",
6426#endif
6427#ifdef FEAT_SUN_WORKSHOP
6428 "sun_workshop",
6429#endif
6430#ifdef FEAT_NETBEANS_INTG
6431 "netbeans_intg",
6432#endif
6433#ifdef FEAT_SPELL
6434 "spell",
6435#endif
6436#ifdef FEAT_SYN_HL
6437 "syntax",
6438#endif
6439#if defined(USE_SYSTEM) || !defined(UNIX)
6440 "system",
6441#endif
6442#ifdef FEAT_TAG_BINS
6443 "tag_binary",
6444#endif
6445#ifdef FEAT_TAG_OLDSTATIC
6446 "tag_old_static",
6447#endif
6448#ifdef FEAT_TAG_ANYWHITE
6449 "tag_any_white",
6450#endif
6451#ifdef FEAT_TCL
6452# ifndef DYNAMIC_TCL
6453 "tcl",
6454# endif
6455#endif
6456#ifdef FEAT_TERMGUICOLORS
6457 "termguicolors",
6458#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006459#if defined(FEAT_TERMINAL) && !defined(WIN3264)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006460 "terminal",
6461#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006462#ifdef TERMINFO
6463 "terminfo",
6464#endif
6465#ifdef FEAT_TERMRESPONSE
6466 "termresponse",
6467#endif
6468#ifdef FEAT_TEXTOBJ
6469 "textobjects",
6470#endif
6471#ifdef HAVE_TGETENT
6472 "tgetent",
6473#endif
6474#ifdef FEAT_TIMERS
6475 "timers",
6476#endif
6477#ifdef FEAT_TITLE
6478 "title",
6479#endif
6480#ifdef FEAT_TOOLBAR
6481 "toolbar",
6482#endif
6483#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6484 "unnamedplus",
6485#endif
6486#ifdef FEAT_USR_CMDS
6487 "user-commands", /* was accidentally included in 5.4 */
6488 "user_commands",
6489#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006490#ifdef FEAT_VARTABS
6491 "vartabs",
6492#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006493#ifdef FEAT_VIMINFO
6494 "viminfo",
6495#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006496 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006497#ifdef FEAT_VIRTUALEDIT
6498 "virtualedit",
6499#endif
6500 "visual",
6501#ifdef FEAT_VISUALEXTRA
6502 "visualextra",
6503#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006504 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006505#ifdef FEAT_VTP
6506 "vtp",
6507#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006508#ifdef FEAT_WILDIGN
6509 "wildignore",
6510#endif
6511#ifdef FEAT_WILDMENU
6512 "wildmenu",
6513#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006514 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006515#ifdef FEAT_WAK
6516 "winaltkeys",
6517#endif
6518#ifdef FEAT_WRITEBACKUP
6519 "writebackup",
6520#endif
6521#ifdef FEAT_XIM
6522 "xim",
6523#endif
6524#ifdef FEAT_XFONTSET
6525 "xfontset",
6526#endif
6527#ifdef FEAT_XPM_W32
6528 "xpm",
6529 "xpm_w32", /* for backward compatibility */
6530#else
6531# if defined(HAVE_XPM)
6532 "xpm",
6533# endif
6534#endif
6535#ifdef USE_XSMP
6536 "xsmp",
6537#endif
6538#ifdef USE_XSMP_INTERACT
6539 "xsmp_interact",
6540#endif
6541#ifdef FEAT_XCLIPBOARD
6542 "xterm_clipboard",
6543#endif
6544#ifdef FEAT_XTERM_SAVE
6545 "xterm_save",
6546#endif
6547#if defined(UNIX) && defined(FEAT_X11)
6548 "X11",
6549#endif
6550 NULL
6551 };
6552
6553 name = get_tv_string(&argvars[0]);
6554 for (i = 0; has_list[i] != NULL; ++i)
6555 if (STRICMP(name, has_list[i]) == 0)
6556 {
6557 n = TRUE;
6558 break;
6559 }
6560
6561 if (n == FALSE)
6562 {
6563 if (STRNICMP(name, "patch", 5) == 0)
6564 {
6565 if (name[5] == '-'
6566 && STRLEN(name) >= 11
6567 && vim_isdigit(name[6])
6568 && vim_isdigit(name[8])
6569 && vim_isdigit(name[10]))
6570 {
6571 int major = atoi((char *)name + 6);
6572 int minor = atoi((char *)name + 8);
6573
6574 /* Expect "patch-9.9.01234". */
6575 n = (major < VIM_VERSION_MAJOR
6576 || (major == VIM_VERSION_MAJOR
6577 && (minor < VIM_VERSION_MINOR
6578 || (minor == VIM_VERSION_MINOR
6579 && has_patch(atoi((char *)name + 10))))));
6580 }
6581 else
6582 n = has_patch(atoi((char *)name + 5));
6583 }
6584 else if (STRICMP(name, "vim_starting") == 0)
6585 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006586 else if (STRICMP(name, "ttyin") == 0)
6587 n = mch_input_isatty();
6588 else if (STRICMP(name, "ttyout") == 0)
6589 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006590#ifdef FEAT_MBYTE
6591 else if (STRICMP(name, "multi_byte_encoding") == 0)
6592 n = has_mbyte;
6593#endif
6594#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
6595 else if (STRICMP(name, "balloon_multiline") == 0)
6596 n = multiline_balloon_available();
6597#endif
6598#ifdef DYNAMIC_TCL
6599 else if (STRICMP(name, "tcl") == 0)
6600 n = tcl_enabled(FALSE);
6601#endif
6602#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6603 else if (STRICMP(name, "iconv") == 0)
6604 n = iconv_enabled(FALSE);
6605#endif
6606#ifdef DYNAMIC_LUA
6607 else if (STRICMP(name, "lua") == 0)
6608 n = lua_enabled(FALSE);
6609#endif
6610#ifdef DYNAMIC_MZSCHEME
6611 else if (STRICMP(name, "mzscheme") == 0)
6612 n = mzscheme_enabled(FALSE);
6613#endif
6614#ifdef DYNAMIC_RUBY
6615 else if (STRICMP(name, "ruby") == 0)
6616 n = ruby_enabled(FALSE);
6617#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006618#ifdef DYNAMIC_PYTHON
6619 else if (STRICMP(name, "python") == 0)
6620 n = python_enabled(FALSE);
6621#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006622#ifdef DYNAMIC_PYTHON3
6623 else if (STRICMP(name, "python3") == 0)
6624 n = python3_enabled(FALSE);
6625#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006626#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6627 else if (STRICMP(name, "pythonx") == 0)
6628 {
6629# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6630 if (p_pyx == 0)
6631 n = python3_enabled(FALSE) || python_enabled(FALSE);
6632 else if (p_pyx == 3)
6633 n = python3_enabled(FALSE);
6634 else if (p_pyx == 2)
6635 n = python_enabled(FALSE);
6636# elif defined(DYNAMIC_PYTHON)
6637 n = python_enabled(FALSE);
6638# elif defined(DYNAMIC_PYTHON3)
6639 n = python3_enabled(FALSE);
6640# endif
6641 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006642#endif
6643#ifdef DYNAMIC_PERL
6644 else if (STRICMP(name, "perl") == 0)
6645 n = perl_enabled(FALSE);
6646#endif
6647#ifdef FEAT_GUI
6648 else if (STRICMP(name, "gui_running") == 0)
6649 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006650# ifdef FEAT_BROWSE
6651 else if (STRICMP(name, "browse") == 0)
6652 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6653# endif
6654#endif
6655#ifdef FEAT_SYN_HL
6656 else if (STRICMP(name, "syntax_items") == 0)
6657 n = syntax_present(curwin);
6658#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006659#ifdef FEAT_VTP
6660 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02006661 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006662#endif
6663#ifdef FEAT_NETBEANS_INTG
6664 else if (STRICMP(name, "netbeans_enabled") == 0)
6665 n = netbeans_active();
6666#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006667#if defined(FEAT_TERMINAL) && defined(WIN3264)
6668 else if (STRICMP(name, "terminal") == 0)
6669 n = terminal_enabled();
6670#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006671 }
6672
6673 rettv->vval.v_number = n;
6674}
6675
6676/*
6677 * "has_key()" function
6678 */
6679 static void
6680f_has_key(typval_T *argvars, typval_T *rettv)
6681{
6682 if (argvars[0].v_type != VAR_DICT)
6683 {
6684 EMSG(_(e_dictreq));
6685 return;
6686 }
6687 if (argvars[0].vval.v_dict == NULL)
6688 return;
6689
6690 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
6691 get_tv_string(&argvars[1]), -1) != NULL;
6692}
6693
6694/*
6695 * "haslocaldir()" function
6696 */
6697 static void
6698f_haslocaldir(typval_T *argvars, typval_T *rettv)
6699{
6700 win_T *wp = NULL;
6701
6702 wp = find_tabwin(&argvars[0], &argvars[1]);
6703 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6704}
6705
6706/*
6707 * "hasmapto()" function
6708 */
6709 static void
6710f_hasmapto(typval_T *argvars, typval_T *rettv)
6711{
6712 char_u *name;
6713 char_u *mode;
6714 char_u buf[NUMBUFLEN];
6715 int abbr = FALSE;
6716
6717 name = get_tv_string(&argvars[0]);
6718 if (argvars[1].v_type == VAR_UNKNOWN)
6719 mode = (char_u *)"nvo";
6720 else
6721 {
6722 mode = get_tv_string_buf(&argvars[1], buf);
6723 if (argvars[2].v_type != VAR_UNKNOWN)
6724 abbr = (int)get_tv_number(&argvars[2]);
6725 }
6726
6727 if (map_to_exists(name, mode, abbr))
6728 rettv->vval.v_number = TRUE;
6729 else
6730 rettv->vval.v_number = FALSE;
6731}
6732
6733/*
6734 * "histadd()" function
6735 */
6736 static void
6737f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6738{
6739#ifdef FEAT_CMDHIST
6740 int histype;
6741 char_u *str;
6742 char_u buf[NUMBUFLEN];
6743#endif
6744
6745 rettv->vval.v_number = FALSE;
6746 if (check_restricted() || check_secure())
6747 return;
6748#ifdef FEAT_CMDHIST
6749 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6750 histype = str != NULL ? get_histtype(str) : -1;
6751 if (histype >= 0)
6752 {
6753 str = get_tv_string_buf(&argvars[1], buf);
6754 if (*str != NUL)
6755 {
6756 init_history();
6757 add_to_history(histype, str, FALSE, NUL);
6758 rettv->vval.v_number = TRUE;
6759 return;
6760 }
6761 }
6762#endif
6763}
6764
6765/*
6766 * "histdel()" function
6767 */
6768 static void
6769f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6770{
6771#ifdef FEAT_CMDHIST
6772 int n;
6773 char_u buf[NUMBUFLEN];
6774 char_u *str;
6775
6776 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6777 if (str == NULL)
6778 n = 0;
6779 else if (argvars[1].v_type == VAR_UNKNOWN)
6780 /* only one argument: clear entire history */
6781 n = clr_history(get_histtype(str));
6782 else if (argvars[1].v_type == VAR_NUMBER)
6783 /* index given: remove that entry */
6784 n = del_history_idx(get_histtype(str),
6785 (int)get_tv_number(&argvars[1]));
6786 else
6787 /* string given: remove all matching entries */
6788 n = del_history_entry(get_histtype(str),
6789 get_tv_string_buf(&argvars[1], buf));
6790 rettv->vval.v_number = n;
6791#endif
6792}
6793
6794/*
6795 * "histget()" function
6796 */
6797 static void
6798f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6799{
6800#ifdef FEAT_CMDHIST
6801 int type;
6802 int idx;
6803 char_u *str;
6804
6805 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6806 if (str == NULL)
6807 rettv->vval.v_string = NULL;
6808 else
6809 {
6810 type = get_histtype(str);
6811 if (argvars[1].v_type == VAR_UNKNOWN)
6812 idx = get_history_idx(type);
6813 else
6814 idx = (int)get_tv_number_chk(&argvars[1], NULL);
6815 /* -1 on type error */
6816 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6817 }
6818#else
6819 rettv->vval.v_string = NULL;
6820#endif
6821 rettv->v_type = VAR_STRING;
6822}
6823
6824/*
6825 * "histnr()" function
6826 */
6827 static void
6828f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6829{
6830 int i;
6831
6832#ifdef FEAT_CMDHIST
6833 char_u *history = get_tv_string_chk(&argvars[0]);
6834
6835 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6836 if (i >= HIST_CMD && i < HIST_COUNT)
6837 i = get_history_idx(i);
6838 else
6839#endif
6840 i = -1;
6841 rettv->vval.v_number = i;
6842}
6843
6844/*
6845 * "highlightID(name)" function
6846 */
6847 static void
6848f_hlID(typval_T *argvars, typval_T *rettv)
6849{
6850 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
6851}
6852
6853/*
6854 * "highlight_exists()" function
6855 */
6856 static void
6857f_hlexists(typval_T *argvars, typval_T *rettv)
6858{
6859 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
6860}
6861
6862/*
6863 * "hostname()" function
6864 */
6865 static void
6866f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6867{
6868 char_u hostname[256];
6869
6870 mch_get_host_name(hostname, 256);
6871 rettv->v_type = VAR_STRING;
6872 rettv->vval.v_string = vim_strsave(hostname);
6873}
6874
6875/*
6876 * iconv() function
6877 */
6878 static void
6879f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6880{
6881#ifdef FEAT_MBYTE
6882 char_u buf1[NUMBUFLEN];
6883 char_u buf2[NUMBUFLEN];
6884 char_u *from, *to, *str;
6885 vimconv_T vimconv;
6886#endif
6887
6888 rettv->v_type = VAR_STRING;
6889 rettv->vval.v_string = NULL;
6890
6891#ifdef FEAT_MBYTE
6892 str = get_tv_string(&argvars[0]);
6893 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
6894 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
6895 vimconv.vc_type = CONV_NONE;
6896 convert_setup(&vimconv, from, to);
6897
6898 /* If the encodings are equal, no conversion needed. */
6899 if (vimconv.vc_type == CONV_NONE)
6900 rettv->vval.v_string = vim_strsave(str);
6901 else
6902 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6903
6904 convert_setup(&vimconv, NULL, NULL);
6905 vim_free(from);
6906 vim_free(to);
6907#endif
6908}
6909
6910/*
6911 * "indent()" function
6912 */
6913 static void
6914f_indent(typval_T *argvars, typval_T *rettv)
6915{
6916 linenr_T lnum;
6917
6918 lnum = get_tv_lnum(argvars);
6919 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6920 rettv->vval.v_number = get_indent_lnum(lnum);
6921 else
6922 rettv->vval.v_number = -1;
6923}
6924
6925/*
6926 * "index()" function
6927 */
6928 static void
6929f_index(typval_T *argvars, typval_T *rettv)
6930{
6931 list_T *l;
6932 listitem_T *item;
6933 long idx = 0;
6934 int ic = FALSE;
6935
6936 rettv->vval.v_number = -1;
6937 if (argvars[0].v_type != VAR_LIST)
6938 {
6939 EMSG(_(e_listreq));
6940 return;
6941 }
6942 l = argvars[0].vval.v_list;
6943 if (l != NULL)
6944 {
6945 item = l->lv_first;
6946 if (argvars[2].v_type != VAR_UNKNOWN)
6947 {
6948 int error = FALSE;
6949
6950 /* Start at specified item. Use the cached index that list_find()
6951 * sets, so that a negative number also works. */
6952 item = list_find(l, (long)get_tv_number_chk(&argvars[2], &error));
6953 idx = l->lv_idx;
6954 if (argvars[3].v_type != VAR_UNKNOWN)
6955 ic = (int)get_tv_number_chk(&argvars[3], &error);
6956 if (error)
6957 item = NULL;
6958 }
6959
6960 for ( ; item != NULL; item = item->li_next, ++idx)
6961 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6962 {
6963 rettv->vval.v_number = idx;
6964 break;
6965 }
6966 }
6967}
6968
6969static int inputsecret_flag = 0;
6970
6971/*
6972 * "input()" function
6973 * Also handles inputsecret() when inputsecret is set.
6974 */
6975 static void
6976f_input(typval_T *argvars, typval_T *rettv)
6977{
6978 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6979}
6980
6981/*
6982 * "inputdialog()" function
6983 */
6984 static void
6985f_inputdialog(typval_T *argvars, typval_T *rettv)
6986{
6987#if defined(FEAT_GUI_TEXTDIALOG)
6988 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
6989 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6990 {
6991 char_u *message;
6992 char_u buf[NUMBUFLEN];
6993 char_u *defstr = (char_u *)"";
6994
6995 message = get_tv_string_chk(&argvars[0]);
6996 if (argvars[1].v_type != VAR_UNKNOWN
6997 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
6998 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6999 else
7000 IObuff[0] = NUL;
7001 if (message != NULL && defstr != NULL
7002 && do_dialog(VIM_QUESTION, NULL, message,
7003 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
7004 rettv->vval.v_string = vim_strsave(IObuff);
7005 else
7006 {
7007 if (message != NULL && defstr != NULL
7008 && argvars[1].v_type != VAR_UNKNOWN
7009 && argvars[2].v_type != VAR_UNKNOWN)
7010 rettv->vval.v_string = vim_strsave(
7011 get_tv_string_buf(&argvars[2], buf));
7012 else
7013 rettv->vval.v_string = NULL;
7014 }
7015 rettv->v_type = VAR_STRING;
7016 }
7017 else
7018#endif
7019 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
7020}
7021
7022/*
7023 * "inputlist()" function
7024 */
7025 static void
7026f_inputlist(typval_T *argvars, typval_T *rettv)
7027{
7028 listitem_T *li;
7029 int selected;
7030 int mouse_used;
7031
7032#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007033 /* While starting up, there is no place to enter text. When running tests
7034 * with --not-a-term we assume feedkeys() will be used. */
7035 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007036 return;
7037#endif
7038 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
7039 {
7040 EMSG2(_(e_listarg), "inputlist()");
7041 return;
7042 }
7043
7044 msg_start();
7045 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
7046 lines_left = Rows; /* avoid more prompt */
7047 msg_scroll = TRUE;
7048 msg_clr_eos();
7049
7050 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
7051 {
7052 msg_puts(get_tv_string(&li->li_tv));
7053 msg_putchar('\n');
7054 }
7055
7056 /* Ask for choice. */
7057 selected = prompt_for_number(&mouse_used);
7058 if (mouse_used)
7059 selected -= lines_left;
7060
7061 rettv->vval.v_number = selected;
7062}
7063
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007064static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7065
7066/*
7067 * "inputrestore()" function
7068 */
7069 static void
7070f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7071{
7072 if (ga_userinput.ga_len > 0)
7073 {
7074 --ga_userinput.ga_len;
7075 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7076 + ga_userinput.ga_len);
7077 /* default return is zero == OK */
7078 }
7079 else if (p_verbose > 1)
7080 {
7081 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
7082 rettv->vval.v_number = 1; /* Failed */
7083 }
7084}
7085
7086/*
7087 * "inputsave()" function
7088 */
7089 static void
7090f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7091{
7092 /* Add an entry to the stack of typeahead storage. */
7093 if (ga_grow(&ga_userinput, 1) == OK)
7094 {
7095 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7096 + ga_userinput.ga_len);
7097 ++ga_userinput.ga_len;
7098 /* default return is zero == OK */
7099 }
7100 else
7101 rettv->vval.v_number = 1; /* Failed */
7102}
7103
7104/*
7105 * "inputsecret()" function
7106 */
7107 static void
7108f_inputsecret(typval_T *argvars, typval_T *rettv)
7109{
7110 ++cmdline_star;
7111 ++inputsecret_flag;
7112 f_input(argvars, rettv);
7113 --cmdline_star;
7114 --inputsecret_flag;
7115}
7116
7117/*
7118 * "insert()" function
7119 */
7120 static void
7121f_insert(typval_T *argvars, typval_T *rettv)
7122{
7123 long before = 0;
7124 listitem_T *item;
7125 list_T *l;
7126 int error = FALSE;
7127
7128 if (argvars[0].v_type != VAR_LIST)
7129 EMSG2(_(e_listarg), "insert()");
7130 else if ((l = argvars[0].vval.v_list) != NULL
7131 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
7132 {
7133 if (argvars[2].v_type != VAR_UNKNOWN)
7134 before = (long)get_tv_number_chk(&argvars[2], &error);
7135 if (error)
7136 return; /* type error; errmsg already given */
7137
7138 if (before == l->lv_len)
7139 item = NULL;
7140 else
7141 {
7142 item = list_find(l, before);
7143 if (item == NULL)
7144 {
7145 EMSGN(_(e_listidx), before);
7146 l = NULL;
7147 }
7148 }
7149 if (l != NULL)
7150 {
7151 list_insert_tv(l, &argvars[1], item);
7152 copy_tv(&argvars[0], rettv);
7153 }
7154 }
7155}
7156
7157/*
7158 * "invert(expr)" function
7159 */
7160 static void
7161f_invert(typval_T *argvars, typval_T *rettv)
7162{
7163 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
7164}
7165
7166/*
7167 * "isdirectory()" function
7168 */
7169 static void
7170f_isdirectory(typval_T *argvars, typval_T *rettv)
7171{
7172 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
7173}
7174
7175/*
7176 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7177 * or it refers to a List or Dictionary that is locked.
7178 */
7179 static int
7180tv_islocked(typval_T *tv)
7181{
7182 return (tv->v_lock & VAR_LOCKED)
7183 || (tv->v_type == VAR_LIST
7184 && tv->vval.v_list != NULL
7185 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7186 || (tv->v_type == VAR_DICT
7187 && tv->vval.v_dict != NULL
7188 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7189}
7190
7191/*
7192 * "islocked()" function
7193 */
7194 static void
7195f_islocked(typval_T *argvars, typval_T *rettv)
7196{
7197 lval_T lv;
7198 char_u *end;
7199 dictitem_T *di;
7200
7201 rettv->vval.v_number = -1;
7202 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007203 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007204 if (end != NULL && lv.ll_name != NULL)
7205 {
7206 if (*end != NUL)
7207 EMSG(_(e_trailing));
7208 else
7209 {
7210 if (lv.ll_tv == NULL)
7211 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007212 di = find_var(lv.ll_name, NULL, TRUE);
7213 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007214 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007215 /* Consider a variable locked when:
7216 * 1. the variable itself is locked
7217 * 2. the value of the variable is locked.
7218 * 3. the List or Dict value is locked.
7219 */
7220 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7221 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007222 }
7223 }
7224 else if (lv.ll_range)
7225 EMSG(_("E786: Range not allowed"));
7226 else if (lv.ll_newkey != NULL)
7227 EMSG2(_(e_dictkey), lv.ll_newkey);
7228 else if (lv.ll_list != NULL)
7229 /* List item. */
7230 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7231 else
7232 /* Dictionary item. */
7233 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7234 }
7235 }
7236
7237 clear_lval(&lv);
7238}
7239
7240#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7241/*
7242 * "isnan()" function
7243 */
7244 static void
7245f_isnan(typval_T *argvars, typval_T *rettv)
7246{
7247 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7248 && isnan(argvars[0].vval.v_float);
7249}
7250#endif
7251
7252/*
7253 * "items(dict)" function
7254 */
7255 static void
7256f_items(typval_T *argvars, typval_T *rettv)
7257{
7258 dict_list(argvars, rettv, 2);
7259}
7260
7261#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
7262/*
7263 * Get the job from the argument.
7264 * Returns NULL if the job is invalid.
7265 */
7266 static job_T *
7267get_job_arg(typval_T *tv)
7268{
7269 job_T *job;
7270
7271 if (tv->v_type != VAR_JOB)
7272 {
7273 EMSG2(_(e_invarg2), get_tv_string(tv));
7274 return NULL;
7275 }
7276 job = tv->vval.v_job;
7277
7278 if (job == NULL)
7279 EMSG(_("E916: not a valid job"));
7280 return job;
7281}
7282
7283/*
7284 * "job_getchannel()" function
7285 */
7286 static void
7287f_job_getchannel(typval_T *argvars, typval_T *rettv)
7288{
7289 job_T *job = get_job_arg(&argvars[0]);
7290
7291 if (job != NULL)
7292 {
7293 rettv->v_type = VAR_CHANNEL;
7294 rettv->vval.v_channel = job->jv_channel;
7295 if (job->jv_channel != NULL)
7296 ++job->jv_channel->ch_refcount;
7297 }
7298}
7299
7300/*
7301 * "job_info()" function
7302 */
7303 static void
7304f_job_info(typval_T *argvars, typval_T *rettv)
7305{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007306 if (argvars[0].v_type != VAR_UNKNOWN)
7307 {
7308 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007309
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007310 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7311 job_info(job, rettv->vval.v_dict);
7312 }
7313 else if (rettv_list_alloc(rettv) == OK)
7314 job_info_all(rettv->vval.v_list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007315}
7316
7317/*
7318 * "job_setoptions()" function
7319 */
7320 static void
7321f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7322{
7323 job_T *job = get_job_arg(&argvars[0]);
7324 jobopt_T opt;
7325
7326 if (job == NULL)
7327 return;
7328 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007329 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007330 job_set_options(job, &opt);
7331 free_job_options(&opt);
7332}
7333
7334/*
7335 * "job_start()" function
7336 */
7337 static void
7338f_job_start(typval_T *argvars, typval_T *rettv)
7339{
7340 rettv->v_type = VAR_JOB;
7341 if (check_restricted() || check_secure())
7342 return;
Bram Moolenaar493359e2018-06-12 20:25:52 +02007343 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007344}
7345
7346/*
7347 * "job_status()" function
7348 */
7349 static void
7350f_job_status(typval_T *argvars, typval_T *rettv)
7351{
7352 job_T *job = get_job_arg(&argvars[0]);
7353
7354 if (job != NULL)
7355 {
7356 rettv->v_type = VAR_STRING;
7357 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7358 }
7359}
7360
7361/*
7362 * "job_stop()" function
7363 */
7364 static void
7365f_job_stop(typval_T *argvars, typval_T *rettv)
7366{
7367 job_T *job = get_job_arg(&argvars[0]);
7368
7369 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007370 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007371}
7372#endif
7373
7374/*
7375 * "join()" function
7376 */
7377 static void
7378f_join(typval_T *argvars, typval_T *rettv)
7379{
7380 garray_T ga;
7381 char_u *sep;
7382
7383 if (argvars[0].v_type != VAR_LIST)
7384 {
7385 EMSG(_(e_listreq));
7386 return;
7387 }
7388 if (argvars[0].vval.v_list == NULL)
7389 return;
7390 if (argvars[1].v_type == VAR_UNKNOWN)
7391 sep = (char_u *)" ";
7392 else
7393 sep = get_tv_string_chk(&argvars[1]);
7394
7395 rettv->v_type = VAR_STRING;
7396
7397 if (sep != NULL)
7398 {
7399 ga_init2(&ga, (int)sizeof(char), 80);
7400 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7401 ga_append(&ga, NUL);
7402 rettv->vval.v_string = (char_u *)ga.ga_data;
7403 }
7404 else
7405 rettv->vval.v_string = NULL;
7406}
7407
7408/*
7409 * "js_decode()" function
7410 */
7411 static void
7412f_js_decode(typval_T *argvars, typval_T *rettv)
7413{
7414 js_read_T reader;
7415
7416 reader.js_buf = get_tv_string(&argvars[0]);
7417 reader.js_fill = NULL;
7418 reader.js_used = 0;
7419 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
7420 EMSG(_(e_invarg));
7421}
7422
7423/*
7424 * "js_encode()" function
7425 */
7426 static void
7427f_js_encode(typval_T *argvars, typval_T *rettv)
7428{
7429 rettv->v_type = VAR_STRING;
7430 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7431}
7432
7433/*
7434 * "json_decode()" function
7435 */
7436 static void
7437f_json_decode(typval_T *argvars, typval_T *rettv)
7438{
7439 js_read_T reader;
7440
7441 reader.js_buf = get_tv_string(&argvars[0]);
7442 reader.js_fill = NULL;
7443 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007444 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007445}
7446
7447/*
7448 * "json_encode()" function
7449 */
7450 static void
7451f_json_encode(typval_T *argvars, typval_T *rettv)
7452{
7453 rettv->v_type = VAR_STRING;
7454 rettv->vval.v_string = json_encode(&argvars[0], 0);
7455}
7456
7457/*
7458 * "keys()" function
7459 */
7460 static void
7461f_keys(typval_T *argvars, typval_T *rettv)
7462{
7463 dict_list(argvars, rettv, 0);
7464}
7465
7466/*
7467 * "last_buffer_nr()" function.
7468 */
7469 static void
7470f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7471{
7472 int n = 0;
7473 buf_T *buf;
7474
Bram Moolenaar29323592016-07-24 22:04:11 +02007475 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007476 if (n < buf->b_fnum)
7477 n = buf->b_fnum;
7478
7479 rettv->vval.v_number = n;
7480}
7481
7482/*
7483 * "len()" function
7484 */
7485 static void
7486f_len(typval_T *argvars, typval_T *rettv)
7487{
7488 switch (argvars[0].v_type)
7489 {
7490 case VAR_STRING:
7491 case VAR_NUMBER:
7492 rettv->vval.v_number = (varnumber_T)STRLEN(
7493 get_tv_string(&argvars[0]));
7494 break;
7495 case VAR_LIST:
7496 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7497 break;
7498 case VAR_DICT:
7499 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7500 break;
7501 case VAR_UNKNOWN:
7502 case VAR_SPECIAL:
7503 case VAR_FLOAT:
7504 case VAR_FUNC:
7505 case VAR_PARTIAL:
7506 case VAR_JOB:
7507 case VAR_CHANNEL:
7508 EMSG(_("E701: Invalid type for len()"));
7509 break;
7510 }
7511}
7512
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007513 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007514libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007515{
7516#ifdef FEAT_LIBCALL
7517 char_u *string_in;
7518 char_u **string_result;
7519 int nr_result;
7520#endif
7521
7522 rettv->v_type = type;
7523 if (type != VAR_NUMBER)
7524 rettv->vval.v_string = NULL;
7525
7526 if (check_restricted() || check_secure())
7527 return;
7528
7529#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007530 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007531 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7532 {
7533 string_in = NULL;
7534 if (argvars[2].v_type == VAR_STRING)
7535 string_in = argvars[2].vval.v_string;
7536 if (type == VAR_NUMBER)
7537 string_result = NULL;
7538 else
7539 string_result = &rettv->vval.v_string;
7540 if (mch_libcall(argvars[0].vval.v_string,
7541 argvars[1].vval.v_string,
7542 string_in,
7543 argvars[2].vval.v_number,
7544 string_result,
7545 &nr_result) == OK
7546 && type == VAR_NUMBER)
7547 rettv->vval.v_number = nr_result;
7548 }
7549#endif
7550}
7551
7552/*
7553 * "libcall()" function
7554 */
7555 static void
7556f_libcall(typval_T *argvars, typval_T *rettv)
7557{
7558 libcall_common(argvars, rettv, VAR_STRING);
7559}
7560
7561/*
7562 * "libcallnr()" function
7563 */
7564 static void
7565f_libcallnr(typval_T *argvars, typval_T *rettv)
7566{
7567 libcall_common(argvars, rettv, VAR_NUMBER);
7568}
7569
7570/*
7571 * "line(string)" function
7572 */
7573 static void
7574f_line(typval_T *argvars, typval_T *rettv)
7575{
7576 linenr_T lnum = 0;
7577 pos_T *fp;
7578 int fnum;
7579
7580 fp = var2fpos(&argvars[0], TRUE, &fnum);
7581 if (fp != NULL)
7582 lnum = fp->lnum;
7583 rettv->vval.v_number = lnum;
7584}
7585
7586/*
7587 * "line2byte(lnum)" function
7588 */
7589 static void
7590f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7591{
7592#ifndef FEAT_BYTEOFF
7593 rettv->vval.v_number = -1;
7594#else
7595 linenr_T lnum;
7596
7597 lnum = get_tv_lnum(argvars);
7598 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7599 rettv->vval.v_number = -1;
7600 else
7601 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7602 if (rettv->vval.v_number >= 0)
7603 ++rettv->vval.v_number;
7604#endif
7605}
7606
7607/*
7608 * "lispindent(lnum)" function
7609 */
7610 static void
7611f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7612{
7613#ifdef FEAT_LISP
7614 pos_T pos;
7615 linenr_T lnum;
7616
7617 pos = curwin->w_cursor;
7618 lnum = get_tv_lnum(argvars);
7619 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7620 {
7621 curwin->w_cursor.lnum = lnum;
7622 rettv->vval.v_number = get_lisp_indent();
7623 curwin->w_cursor = pos;
7624 }
7625 else
7626#endif
7627 rettv->vval.v_number = -1;
7628}
7629
7630/*
7631 * "localtime()" function
7632 */
7633 static void
7634f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7635{
7636 rettv->vval.v_number = (varnumber_T)time(NULL);
7637}
7638
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007639 static void
7640get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7641{
7642 char_u *keys;
7643 char_u *which;
7644 char_u buf[NUMBUFLEN];
7645 char_u *keys_buf = NULL;
7646 char_u *rhs;
7647 int mode;
7648 int abbr = FALSE;
7649 int get_dict = FALSE;
7650 mapblock_T *mp;
7651 int buffer_local;
7652
7653 /* return empty string for failure */
7654 rettv->v_type = VAR_STRING;
7655 rettv->vval.v_string = NULL;
7656
7657 keys = get_tv_string(&argvars[0]);
7658 if (*keys == NUL)
7659 return;
7660
7661 if (argvars[1].v_type != VAR_UNKNOWN)
7662 {
7663 which = get_tv_string_buf_chk(&argvars[1], buf);
7664 if (argvars[2].v_type != VAR_UNKNOWN)
7665 {
7666 abbr = (int)get_tv_number(&argvars[2]);
7667 if (argvars[3].v_type != VAR_UNKNOWN)
7668 get_dict = (int)get_tv_number(&argvars[3]);
7669 }
7670 }
7671 else
7672 which = (char_u *)"";
7673 if (which == NULL)
7674 return;
7675
7676 mode = get_map_mode(&which, 0);
7677
7678 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7679 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7680 vim_free(keys_buf);
7681
7682 if (!get_dict)
7683 {
7684 /* Return a string. */
7685 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02007686 {
7687 if (*rhs == NUL)
7688 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
7689 else
7690 rettv->vval.v_string = str2special_save(rhs, FALSE);
7691 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007692
7693 }
7694 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7695 {
7696 /* Return a dictionary. */
7697 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7698 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7699 dict_T *dict = rettv->vval.v_dict;
7700
Bram Moolenaare0be1672018-07-08 16:50:37 +02007701 dict_add_string(dict, "lhs", lhs);
7702 dict_add_string(dict, "rhs", mp->m_orig_str);
7703 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
7704 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
7705 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007706 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
7707 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02007708 dict_add_number(dict, "buffer", (long)buffer_local);
7709 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
7710 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007711
7712 vim_free(lhs);
7713 vim_free(mapmode);
7714 }
7715}
7716
7717#ifdef FEAT_FLOAT
7718/*
7719 * "log()" function
7720 */
7721 static void
7722f_log(typval_T *argvars, typval_T *rettv)
7723{
7724 float_T f = 0.0;
7725
7726 rettv->v_type = VAR_FLOAT;
7727 if (get_float_arg(argvars, &f) == OK)
7728 rettv->vval.v_float = log(f);
7729 else
7730 rettv->vval.v_float = 0.0;
7731}
7732
7733/*
7734 * "log10()" function
7735 */
7736 static void
7737f_log10(typval_T *argvars, typval_T *rettv)
7738{
7739 float_T f = 0.0;
7740
7741 rettv->v_type = VAR_FLOAT;
7742 if (get_float_arg(argvars, &f) == OK)
7743 rettv->vval.v_float = log10(f);
7744 else
7745 rettv->vval.v_float = 0.0;
7746}
7747#endif
7748
7749#ifdef FEAT_LUA
7750/*
7751 * "luaeval()" function
7752 */
7753 static void
7754f_luaeval(typval_T *argvars, typval_T *rettv)
7755{
7756 char_u *str;
7757 char_u buf[NUMBUFLEN];
7758
7759 str = get_tv_string_buf(&argvars[0], buf);
7760 do_luaeval(str, argvars + 1, rettv);
7761}
7762#endif
7763
7764/*
7765 * "map()" function
7766 */
7767 static void
7768f_map(typval_T *argvars, typval_T *rettv)
7769{
7770 filter_map(argvars, rettv, TRUE);
7771}
7772
7773/*
7774 * "maparg()" function
7775 */
7776 static void
7777f_maparg(typval_T *argvars, typval_T *rettv)
7778{
7779 get_maparg(argvars, rettv, TRUE);
7780}
7781
7782/*
7783 * "mapcheck()" function
7784 */
7785 static void
7786f_mapcheck(typval_T *argvars, typval_T *rettv)
7787{
7788 get_maparg(argvars, rettv, FALSE);
7789}
7790
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007791typedef enum
7792{
7793 MATCH_END, /* matchend() */
7794 MATCH_MATCH, /* match() */
7795 MATCH_STR, /* matchstr() */
7796 MATCH_LIST, /* matchlist() */
7797 MATCH_POS /* matchstrpos() */
7798} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007799
7800 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007801find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007802{
7803 char_u *str = NULL;
7804 long len = 0;
7805 char_u *expr = NULL;
7806 char_u *pat;
7807 regmatch_T regmatch;
7808 char_u patbuf[NUMBUFLEN];
7809 char_u strbuf[NUMBUFLEN];
7810 char_u *save_cpo;
7811 long start = 0;
7812 long nth = 1;
7813 colnr_T startcol = 0;
7814 int match = 0;
7815 list_T *l = NULL;
7816 listitem_T *li = NULL;
7817 long idx = 0;
7818 char_u *tofree = NULL;
7819
7820 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7821 save_cpo = p_cpo;
7822 p_cpo = (char_u *)"";
7823
7824 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007825 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007826 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007827 /* type MATCH_LIST: return empty list when there are no matches.
7828 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007829 if (rettv_list_alloc(rettv) == FAIL)
7830 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007831 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007832 && (list_append_string(rettv->vval.v_list,
7833 (char_u *)"", 0) == FAIL
7834 || list_append_number(rettv->vval.v_list,
7835 (varnumber_T)-1) == FAIL
7836 || list_append_number(rettv->vval.v_list,
7837 (varnumber_T)-1) == FAIL
7838 || list_append_number(rettv->vval.v_list,
7839 (varnumber_T)-1) == FAIL))
7840 {
7841 list_free(rettv->vval.v_list);
7842 rettv->vval.v_list = NULL;
7843 goto theend;
7844 }
7845 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007846 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007847 {
7848 rettv->v_type = VAR_STRING;
7849 rettv->vval.v_string = NULL;
7850 }
7851
7852 if (argvars[0].v_type == VAR_LIST)
7853 {
7854 if ((l = argvars[0].vval.v_list) == NULL)
7855 goto theend;
7856 li = l->lv_first;
7857 }
7858 else
7859 {
7860 expr = str = get_tv_string(&argvars[0]);
7861 len = (long)STRLEN(str);
7862 }
7863
7864 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
7865 if (pat == NULL)
7866 goto theend;
7867
7868 if (argvars[2].v_type != VAR_UNKNOWN)
7869 {
7870 int error = FALSE;
7871
7872 start = (long)get_tv_number_chk(&argvars[2], &error);
7873 if (error)
7874 goto theend;
7875 if (l != NULL)
7876 {
7877 li = list_find(l, start);
7878 if (li == NULL)
7879 goto theend;
7880 idx = l->lv_idx; /* use the cached index */
7881 }
7882 else
7883 {
7884 if (start < 0)
7885 start = 0;
7886 if (start > len)
7887 goto theend;
7888 /* When "count" argument is there ignore matches before "start",
7889 * otherwise skip part of the string. Differs when pattern is "^"
7890 * or "\<". */
7891 if (argvars[3].v_type != VAR_UNKNOWN)
7892 startcol = start;
7893 else
7894 {
7895 str += start;
7896 len -= start;
7897 }
7898 }
7899
7900 if (argvars[3].v_type != VAR_UNKNOWN)
7901 nth = (long)get_tv_number_chk(&argvars[3], &error);
7902 if (error)
7903 goto theend;
7904 }
7905
7906 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7907 if (regmatch.regprog != NULL)
7908 {
7909 regmatch.rm_ic = p_ic;
7910
7911 for (;;)
7912 {
7913 if (l != NULL)
7914 {
7915 if (li == NULL)
7916 {
7917 match = FALSE;
7918 break;
7919 }
7920 vim_free(tofree);
7921 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
7922 if (str == NULL)
7923 break;
7924 }
7925
7926 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
7927
7928 if (match && --nth <= 0)
7929 break;
7930 if (l == NULL && !match)
7931 break;
7932
7933 /* Advance to just after the match. */
7934 if (l != NULL)
7935 {
7936 li = li->li_next;
7937 ++idx;
7938 }
7939 else
7940 {
7941#ifdef FEAT_MBYTE
7942 startcol = (colnr_T)(regmatch.startp[0]
7943 + (*mb_ptr2len)(regmatch.startp[0]) - str);
7944#else
7945 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
7946#endif
7947 if (startcol > (colnr_T)len
7948 || str + startcol <= regmatch.startp[0])
7949 {
7950 match = FALSE;
7951 break;
7952 }
7953 }
7954 }
7955
7956 if (match)
7957 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007958 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007959 {
7960 listitem_T *li1 = rettv->vval.v_list->lv_first;
7961 listitem_T *li2 = li1->li_next;
7962 listitem_T *li3 = li2->li_next;
7963 listitem_T *li4 = li3->li_next;
7964
7965 vim_free(li1->li_tv.vval.v_string);
7966 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
7967 (int)(regmatch.endp[0] - regmatch.startp[0]));
7968 li3->li_tv.vval.v_number =
7969 (varnumber_T)(regmatch.startp[0] - expr);
7970 li4->li_tv.vval.v_number =
7971 (varnumber_T)(regmatch.endp[0] - expr);
7972 if (l != NULL)
7973 li2->li_tv.vval.v_number = (varnumber_T)idx;
7974 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007975 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007976 {
7977 int i;
7978
7979 /* return list with matched string and submatches */
7980 for (i = 0; i < NSUBEXP; ++i)
7981 {
7982 if (regmatch.endp[i] == NULL)
7983 {
7984 if (list_append_string(rettv->vval.v_list,
7985 (char_u *)"", 0) == FAIL)
7986 break;
7987 }
7988 else if (list_append_string(rettv->vval.v_list,
7989 regmatch.startp[i],
7990 (int)(regmatch.endp[i] - regmatch.startp[i]))
7991 == FAIL)
7992 break;
7993 }
7994 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007995 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007996 {
7997 /* return matched string */
7998 if (l != NULL)
7999 copy_tv(&li->li_tv, rettv);
8000 else
8001 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
8002 (int)(regmatch.endp[0] - regmatch.startp[0]));
8003 }
8004 else if (l != NULL)
8005 rettv->vval.v_number = idx;
8006 else
8007 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008008 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008009 rettv->vval.v_number =
8010 (varnumber_T)(regmatch.startp[0] - str);
8011 else
8012 rettv->vval.v_number =
8013 (varnumber_T)(regmatch.endp[0] - str);
8014 rettv->vval.v_number += (varnumber_T)(str - expr);
8015 }
8016 }
8017 vim_regfree(regmatch.regprog);
8018 }
8019
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008020theend:
8021 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008022 /* matchstrpos() without a list: drop the second item. */
8023 listitem_remove(rettv->vval.v_list,
8024 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008025 vim_free(tofree);
8026 p_cpo = save_cpo;
8027}
8028
8029/*
8030 * "match()" function
8031 */
8032 static void
8033f_match(typval_T *argvars, typval_T *rettv)
8034{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008035 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008036}
8037
Bram Moolenaar95e51472018-07-28 16:55:56 +02008038#ifdef FEAT_SEARCH_EXTRA
8039 static int
8040matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
8041{
8042 dictitem_T *di;
8043
8044 if (tv->v_type != VAR_DICT)
8045 {
8046 EMSG(_(e_dictreq));
8047 return FAIL;
8048 }
8049
8050 if (dict_find(tv->vval.v_dict, (char_u *)"conceal", -1) != NULL)
8051 *conceal_char = get_dict_string(tv->vval.v_dict,
8052 (char_u *)"conceal", FALSE);
8053
8054 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
8055 {
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02008056 *win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008057 if (*win == NULL)
8058 {
8059 EMSG(_("E957: Invalid window number"));
8060 return FAIL;
8061 }
8062 }
8063
8064 return OK;
8065}
8066#endif
8067
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008068/*
8069 * "matchadd()" function
8070 */
8071 static void
8072f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8073{
8074#ifdef FEAT_SEARCH_EXTRA
8075 char_u buf[NUMBUFLEN];
8076 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
8077 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
8078 int prio = 10; /* default priority */
8079 int id = -1;
8080 int error = FALSE;
8081 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008082 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008083
8084 rettv->vval.v_number = -1;
8085
8086 if (grp == NULL || pat == NULL)
8087 return;
8088 if (argvars[2].v_type != VAR_UNKNOWN)
8089 {
8090 prio = (int)get_tv_number_chk(&argvars[2], &error);
8091 if (argvars[3].v_type != VAR_UNKNOWN)
8092 {
8093 id = (int)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008094 if (argvars[4].v_type != VAR_UNKNOWN
8095 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8096 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008097 }
8098 }
8099 if (error == TRUE)
8100 return;
8101 if (id >= 1 && id <= 3)
8102 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02008103 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008104 return;
8105 }
8106
Bram Moolenaar95e51472018-07-28 16:55:56 +02008107 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008108 conceal_char);
8109#endif
8110}
8111
8112/*
8113 * "matchaddpos()" function
8114 */
8115 static void
8116f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8117{
8118#ifdef FEAT_SEARCH_EXTRA
8119 char_u buf[NUMBUFLEN];
8120 char_u *group;
8121 int prio = 10;
8122 int id = -1;
8123 int error = FALSE;
8124 list_T *l;
8125 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008126 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008127
8128 rettv->vval.v_number = -1;
8129
8130 group = get_tv_string_buf_chk(&argvars[0], buf);
8131 if (group == NULL)
8132 return;
8133
8134 if (argvars[1].v_type != VAR_LIST)
8135 {
8136 EMSG2(_(e_listarg), "matchaddpos()");
8137 return;
8138 }
8139 l = argvars[1].vval.v_list;
8140 if (l == NULL)
8141 return;
8142
8143 if (argvars[2].v_type != VAR_UNKNOWN)
8144 {
8145 prio = (int)get_tv_number_chk(&argvars[2], &error);
8146 if (argvars[3].v_type != VAR_UNKNOWN)
8147 {
8148 id = (int)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008149
8150 if (argvars[4].v_type != VAR_UNKNOWN
8151 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8152 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008153 }
8154 }
8155 if (error == TRUE)
8156 return;
8157
8158 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8159 if (id == 1 || id == 2)
8160 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02008161 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008162 return;
8163 }
8164
Bram Moolenaar95e51472018-07-28 16:55:56 +02008165 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008166 conceal_char);
8167#endif
8168}
8169
8170/*
8171 * "matcharg()" function
8172 */
8173 static void
8174f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8175{
8176 if (rettv_list_alloc(rettv) == OK)
8177 {
8178#ifdef FEAT_SEARCH_EXTRA
8179 int id = (int)get_tv_number(&argvars[0]);
8180 matchitem_T *m;
8181
8182 if (id >= 1 && id <= 3)
8183 {
8184 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8185 {
8186 list_append_string(rettv->vval.v_list,
8187 syn_id2name(m->hlg_id), -1);
8188 list_append_string(rettv->vval.v_list, m->pattern, -1);
8189 }
8190 else
8191 {
8192 list_append_string(rettv->vval.v_list, NULL, -1);
8193 list_append_string(rettv->vval.v_list, NULL, -1);
8194 }
8195 }
8196#endif
8197 }
8198}
8199
8200/*
8201 * "matchdelete()" function
8202 */
8203 static void
8204f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8205{
8206#ifdef FEAT_SEARCH_EXTRA
8207 rettv->vval.v_number = match_delete(curwin,
8208 (int)get_tv_number(&argvars[0]), TRUE);
8209#endif
8210}
8211
8212/*
8213 * "matchend()" function
8214 */
8215 static void
8216f_matchend(typval_T *argvars, typval_T *rettv)
8217{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008218 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008219}
8220
8221/*
8222 * "matchlist()" function
8223 */
8224 static void
8225f_matchlist(typval_T *argvars, typval_T *rettv)
8226{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008227 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008228}
8229
8230/*
8231 * "matchstr()" function
8232 */
8233 static void
8234f_matchstr(typval_T *argvars, typval_T *rettv)
8235{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008236 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008237}
8238
8239/*
8240 * "matchstrpos()" function
8241 */
8242 static void
8243f_matchstrpos(typval_T *argvars, typval_T *rettv)
8244{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008245 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008246}
8247
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008248 static void
8249max_min(typval_T *argvars, typval_T *rettv, int domax)
8250{
8251 varnumber_T n = 0;
8252 varnumber_T i;
8253 int error = FALSE;
8254
8255 if (argvars[0].v_type == VAR_LIST)
8256 {
8257 list_T *l;
8258 listitem_T *li;
8259
8260 l = argvars[0].vval.v_list;
8261 if (l != NULL)
8262 {
8263 li = l->lv_first;
8264 if (li != NULL)
8265 {
8266 n = get_tv_number_chk(&li->li_tv, &error);
8267 for (;;)
8268 {
8269 li = li->li_next;
8270 if (li == NULL)
8271 break;
8272 i = get_tv_number_chk(&li->li_tv, &error);
8273 if (domax ? i > n : i < n)
8274 n = i;
8275 }
8276 }
8277 }
8278 }
8279 else if (argvars[0].v_type == VAR_DICT)
8280 {
8281 dict_T *d;
8282 int first = TRUE;
8283 hashitem_T *hi;
8284 int todo;
8285
8286 d = argvars[0].vval.v_dict;
8287 if (d != NULL)
8288 {
8289 todo = (int)d->dv_hashtab.ht_used;
8290 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8291 {
8292 if (!HASHITEM_EMPTY(hi))
8293 {
8294 --todo;
8295 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
8296 if (first)
8297 {
8298 n = i;
8299 first = FALSE;
8300 }
8301 else if (domax ? i > n : i < n)
8302 n = i;
8303 }
8304 }
8305 }
8306 }
8307 else
Bram Moolenaar26b84332016-09-04 21:42:36 +02008308 EMSG2(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008309 rettv->vval.v_number = error ? 0 : n;
8310}
8311
8312/*
8313 * "max()" function
8314 */
8315 static void
8316f_max(typval_T *argvars, typval_T *rettv)
8317{
8318 max_min(argvars, rettv, TRUE);
8319}
8320
8321/*
8322 * "min()" function
8323 */
8324 static void
8325f_min(typval_T *argvars, typval_T *rettv)
8326{
8327 max_min(argvars, rettv, FALSE);
8328}
8329
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008330/*
8331 * Create the directory in which "dir" is located, and higher levels when
8332 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008333 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008334 */
8335 static int
8336mkdir_recurse(char_u *dir, int prot)
8337{
8338 char_u *p;
8339 char_u *updir;
8340 int r = FAIL;
8341
8342 /* Get end of directory name in "dir".
8343 * We're done when it's "/" or "c:/". */
8344 p = gettail_sep(dir);
8345 if (p <= get_past_head(dir))
8346 return OK;
8347
8348 /* If the directory exists we're done. Otherwise: create it.*/
8349 updir = vim_strnsave(dir, (int)(p - dir));
8350 if (updir == NULL)
8351 return FAIL;
8352 if (mch_isdir(updir))
8353 r = OK;
8354 else if (mkdir_recurse(updir, prot) == OK)
8355 r = vim_mkdir_emsg(updir, prot);
8356 vim_free(updir);
8357 return r;
8358}
8359
8360#ifdef vim_mkdir
8361/*
8362 * "mkdir()" function
8363 */
8364 static void
8365f_mkdir(typval_T *argvars, typval_T *rettv)
8366{
8367 char_u *dir;
8368 char_u buf[NUMBUFLEN];
8369 int prot = 0755;
8370
8371 rettv->vval.v_number = FAIL;
8372 if (check_restricted() || check_secure())
8373 return;
8374
8375 dir = get_tv_string_buf(&argvars[0], buf);
8376 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008377 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008378
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008379 if (*gettail(dir) == NUL)
8380 /* remove trailing slashes */
8381 *gettail_sep(dir) = NUL;
8382
8383 if (argvars[1].v_type != VAR_UNKNOWN)
8384 {
8385 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008386 {
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008387 prot = (int)get_tv_number_chk(&argvars[2], NULL);
8388 if (prot == -1)
8389 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008390 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008391 if (STRCMP(get_tv_string(&argvars[1]), "p") == 0)
8392 {
8393 if (mch_isdir(dir))
8394 {
8395 /* With the "p" flag it's OK if the dir already exists. */
8396 rettv->vval.v_number = OK;
8397 return;
8398 }
8399 mkdir_recurse(dir, prot);
8400 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008401 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008402 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008403}
8404#endif
8405
8406/*
8407 * "mode()" function
8408 */
8409 static void
8410f_mode(typval_T *argvars, typval_T *rettv)
8411{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008412 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008413
Bram Moolenaar612cc382018-07-29 15:34:26 +02008414 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008415
8416 if (time_for_testing == 93784)
8417 {
8418 /* Testing the two-character code. */
8419 buf[0] = 'x';
8420 buf[1] = '!';
8421 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008422#ifdef FEAT_TERMINAL
8423 else if (term_use_loop())
8424 buf[0] = 't';
8425#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008426 else if (VIsual_active)
8427 {
8428 if (VIsual_select)
8429 buf[0] = VIsual_mode + 's' - 'v';
8430 else
8431 buf[0] = VIsual_mode;
8432 }
8433 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8434 || State == CONFIRM)
8435 {
8436 buf[0] = 'r';
8437 if (State == ASKMORE)
8438 buf[1] = 'm';
8439 else if (State == CONFIRM)
8440 buf[1] = '?';
8441 }
8442 else if (State == EXTERNCMD)
8443 buf[0] = '!';
8444 else if (State & INSERT)
8445 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008446 if (State & VREPLACE_FLAG)
8447 {
8448 buf[0] = 'R';
8449 buf[1] = 'v';
8450 }
8451 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008452 {
8453 if (State & REPLACE_FLAG)
8454 buf[0] = 'R';
8455 else
8456 buf[0] = 'i';
8457#ifdef FEAT_INS_EXPAND
8458 if (ins_compl_active())
8459 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008460 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008461 buf[1] = 'x';
8462#endif
8463 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008464 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008465 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008466 {
8467 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008468 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008469 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008470 else if (exmode_active == EXMODE_NORMAL)
8471 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008472 }
8473 else
8474 {
8475 buf[0] = 'n';
8476 if (finish_op)
8477 buf[1] = 'o';
Bram Moolenaar612cc382018-07-29 15:34:26 +02008478 else if (restart_edit == 'I' || restart_edit == 'R'
8479 || restart_edit == 'V')
8480 {
8481 buf[1] = 'i';
8482 buf[2] = restart_edit;
8483 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008484 }
8485
8486 /* Clear out the minor mode when the argument is not a non-zero number or
8487 * non-empty string. */
8488 if (!non_zero_arg(&argvars[0]))
8489 buf[1] = NUL;
8490
8491 rettv->vval.v_string = vim_strsave(buf);
8492 rettv->v_type = VAR_STRING;
8493}
8494
8495#if defined(FEAT_MZSCHEME) || defined(PROTO)
8496/*
8497 * "mzeval()" function
8498 */
8499 static void
8500f_mzeval(typval_T *argvars, typval_T *rettv)
8501{
8502 char_u *str;
8503 char_u buf[NUMBUFLEN];
8504
8505 str = get_tv_string_buf(&argvars[0], buf);
8506 do_mzeval(str, rettv);
8507}
8508
8509 void
8510mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8511{
8512 typval_T argvars[3];
8513
8514 argvars[0].v_type = VAR_STRING;
8515 argvars[0].vval.v_string = name;
8516 copy_tv(args, &argvars[1]);
8517 argvars[2].v_type = VAR_UNKNOWN;
8518 f_call(argvars, rettv);
8519 clear_tv(&argvars[1]);
8520}
8521#endif
8522
8523/*
8524 * "nextnonblank()" function
8525 */
8526 static void
8527f_nextnonblank(typval_T *argvars, typval_T *rettv)
8528{
8529 linenr_T lnum;
8530
8531 for (lnum = get_tv_lnum(argvars); ; ++lnum)
8532 {
8533 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8534 {
8535 lnum = 0;
8536 break;
8537 }
8538 if (*skipwhite(ml_get(lnum)) != NUL)
8539 break;
8540 }
8541 rettv->vval.v_number = lnum;
8542}
8543
8544/*
8545 * "nr2char()" function
8546 */
8547 static void
8548f_nr2char(typval_T *argvars, typval_T *rettv)
8549{
8550 char_u buf[NUMBUFLEN];
8551
8552#ifdef FEAT_MBYTE
8553 if (has_mbyte)
8554 {
8555 int utf8 = 0;
8556
8557 if (argvars[1].v_type != VAR_UNKNOWN)
8558 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
8559 if (utf8)
8560 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8561 else
8562 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8563 }
8564 else
8565#endif
8566 {
8567 buf[0] = (char_u)get_tv_number(&argvars[0]);
8568 buf[1] = NUL;
8569 }
8570 rettv->v_type = VAR_STRING;
8571 rettv->vval.v_string = vim_strsave(buf);
8572}
8573
8574/*
8575 * "or(expr, expr)" function
8576 */
8577 static void
8578f_or(typval_T *argvars, typval_T *rettv)
8579{
8580 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8581 | get_tv_number_chk(&argvars[1], NULL);
8582}
8583
8584/*
8585 * "pathshorten()" function
8586 */
8587 static void
8588f_pathshorten(typval_T *argvars, typval_T *rettv)
8589{
8590 char_u *p;
8591
8592 rettv->v_type = VAR_STRING;
8593 p = get_tv_string_chk(&argvars[0]);
8594 if (p == NULL)
8595 rettv->vval.v_string = NULL;
8596 else
8597 {
8598 p = vim_strsave(p);
8599 rettv->vval.v_string = p;
8600 if (p != NULL)
8601 shorten_dir(p);
8602 }
8603}
8604
8605#ifdef FEAT_PERL
8606/*
8607 * "perleval()" function
8608 */
8609 static void
8610f_perleval(typval_T *argvars, typval_T *rettv)
8611{
8612 char_u *str;
8613 char_u buf[NUMBUFLEN];
8614
8615 str = get_tv_string_buf(&argvars[0], buf);
8616 do_perleval(str, rettv);
8617}
8618#endif
8619
8620#ifdef FEAT_FLOAT
8621/*
8622 * "pow()" function
8623 */
8624 static void
8625f_pow(typval_T *argvars, typval_T *rettv)
8626{
8627 float_T fx = 0.0, fy = 0.0;
8628
8629 rettv->v_type = VAR_FLOAT;
8630 if (get_float_arg(argvars, &fx) == OK
8631 && get_float_arg(&argvars[1], &fy) == OK)
8632 rettv->vval.v_float = pow(fx, fy);
8633 else
8634 rettv->vval.v_float = 0.0;
8635}
8636#endif
8637
8638/*
8639 * "prevnonblank()" function
8640 */
8641 static void
8642f_prevnonblank(typval_T *argvars, typval_T *rettv)
8643{
8644 linenr_T lnum;
8645
8646 lnum = get_tv_lnum(argvars);
8647 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8648 lnum = 0;
8649 else
8650 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8651 --lnum;
8652 rettv->vval.v_number = lnum;
8653}
8654
8655/* This dummy va_list is here because:
8656 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8657 * - locally in the function results in a "used before set" warning
8658 * - using va_start() to initialize it gives "function with fixed args" error */
8659static va_list ap;
8660
8661/*
8662 * "printf()" function
8663 */
8664 static void
8665f_printf(typval_T *argvars, typval_T *rettv)
8666{
8667 char_u buf[NUMBUFLEN];
8668 int len;
8669 char_u *s;
8670 int saved_did_emsg = did_emsg;
8671 char *fmt;
8672
8673 rettv->v_type = VAR_STRING;
8674 rettv->vval.v_string = NULL;
8675
8676 /* Get the required length, allocate the buffer and do it for real. */
8677 did_emsg = FALSE;
8678 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008679 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008680 if (!did_emsg)
8681 {
8682 s = alloc(len + 1);
8683 if (s != NULL)
8684 {
8685 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008686 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8687 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008688 }
8689 }
8690 did_emsg |= saved_did_emsg;
8691}
8692
Bram Moolenaarf2732452018-06-03 14:47:35 +02008693#ifdef FEAT_JOB_CHANNEL
8694/*
8695 * "prompt_setcallback({buffer}, {callback})" function
8696 */
8697 static void
8698f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
8699{
8700 buf_T *buf;
8701 char_u *callback;
8702 partial_T *partial;
8703
8704 if (check_secure())
8705 return;
8706 buf = get_buf_tv(&argvars[0], FALSE);
8707 if (buf == NULL)
8708 return;
8709
8710 callback = get_callback(&argvars[1], &partial);
8711 if (callback == NULL)
8712 return;
8713
8714 free_callback(buf->b_prompt_callback, buf->b_prompt_partial);
8715 if (partial == NULL)
8716 buf->b_prompt_callback = vim_strsave(callback);
8717 else
8718 /* pointer into the partial */
8719 buf->b_prompt_callback = callback;
8720 buf->b_prompt_partial = partial;
8721}
8722
8723/*
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02008724 * "prompt_setinterrupt({buffer}, {callback})" function
8725 */
8726 static void
8727f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
8728{
8729 buf_T *buf;
8730 char_u *callback;
8731 partial_T *partial;
8732
8733 if (check_secure())
8734 return;
8735 buf = get_buf_tv(&argvars[0], FALSE);
8736 if (buf == NULL)
8737 return;
8738
8739 callback = get_callback(&argvars[1], &partial);
8740 if (callback == NULL)
8741 return;
8742
8743 free_callback(buf->b_prompt_interrupt, buf->b_prompt_int_partial);
8744 if (partial == NULL)
8745 buf->b_prompt_interrupt = vim_strsave(callback);
8746 else
8747 /* pointer into the partial */
8748 buf->b_prompt_interrupt = callback;
8749 buf->b_prompt_int_partial = partial;
8750}
8751
8752/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02008753 * "prompt_setprompt({buffer}, {text})" function
8754 */
8755 static void
8756f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
8757{
8758 buf_T *buf;
8759 char_u *text;
8760
8761 if (check_secure())
8762 return;
8763 buf = get_buf_tv(&argvars[0], FALSE);
8764 if (buf == NULL)
8765 return;
8766
8767 text = get_tv_string(&argvars[1]);
8768 vim_free(buf->b_prompt_text);
8769 buf->b_prompt_text = vim_strsave(text);
8770}
8771#endif
8772
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008773/*
8774 * "pumvisible()" function
8775 */
8776 static void
8777f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8778{
8779#ifdef FEAT_INS_EXPAND
8780 if (pum_visible())
8781 rettv->vval.v_number = 1;
8782#endif
8783}
8784
8785#ifdef FEAT_PYTHON3
8786/*
8787 * "py3eval()" function
8788 */
8789 static void
8790f_py3eval(typval_T *argvars, typval_T *rettv)
8791{
8792 char_u *str;
8793 char_u buf[NUMBUFLEN];
8794
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008795 if (p_pyx == 0)
8796 p_pyx = 3;
8797
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008798 str = get_tv_string_buf(&argvars[0], buf);
8799 do_py3eval(str, rettv);
8800}
8801#endif
8802
8803#ifdef FEAT_PYTHON
8804/*
8805 * "pyeval()" function
8806 */
8807 static void
8808f_pyeval(typval_T *argvars, typval_T *rettv)
8809{
8810 char_u *str;
8811 char_u buf[NUMBUFLEN];
8812
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008813 if (p_pyx == 0)
8814 p_pyx = 2;
8815
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008816 str = get_tv_string_buf(&argvars[0], buf);
8817 do_pyeval(str, rettv);
8818}
8819#endif
8820
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008821#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8822/*
8823 * "pyxeval()" function
8824 */
8825 static void
8826f_pyxeval(typval_T *argvars, typval_T *rettv)
8827{
8828# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8829 init_pyxversion();
8830 if (p_pyx == 2)
8831 f_pyeval(argvars, rettv);
8832 else
8833 f_py3eval(argvars, rettv);
8834# elif defined(FEAT_PYTHON)
8835 f_pyeval(argvars, rettv);
8836# elif defined(FEAT_PYTHON3)
8837 f_py3eval(argvars, rettv);
8838# endif
8839}
8840#endif
8841
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008842/*
8843 * "range()" function
8844 */
8845 static void
8846f_range(typval_T *argvars, typval_T *rettv)
8847{
8848 varnumber_T start;
8849 varnumber_T end;
8850 varnumber_T stride = 1;
8851 varnumber_T i;
8852 int error = FALSE;
8853
8854 start = get_tv_number_chk(&argvars[0], &error);
8855 if (argvars[1].v_type == VAR_UNKNOWN)
8856 {
8857 end = start - 1;
8858 start = 0;
8859 }
8860 else
8861 {
8862 end = get_tv_number_chk(&argvars[1], &error);
8863 if (argvars[2].v_type != VAR_UNKNOWN)
8864 stride = get_tv_number_chk(&argvars[2], &error);
8865 }
8866
8867 if (error)
8868 return; /* type error; errmsg already given */
8869 if (stride == 0)
8870 EMSG(_("E726: Stride is zero"));
8871 else if (stride > 0 ? end + 1 < start : end - 1 > start)
8872 EMSG(_("E727: Start past end"));
8873 else
8874 {
8875 if (rettv_list_alloc(rettv) == OK)
8876 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8877 if (list_append_number(rettv->vval.v_list,
8878 (varnumber_T)i) == FAIL)
8879 break;
8880 }
8881}
8882
8883/*
8884 * "readfile()" function
8885 */
8886 static void
8887f_readfile(typval_T *argvars, typval_T *rettv)
8888{
8889 int binary = FALSE;
8890 int failed = FALSE;
8891 char_u *fname;
8892 FILE *fd;
8893 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
8894 int io_size = sizeof(buf);
8895 int readlen; /* size of last fread() */
8896 char_u *prev = NULL; /* previously read bytes, if any */
8897 long prevlen = 0; /* length of data in prev */
8898 long prevsize = 0; /* size of prev buffer */
8899 long maxline = MAXLNUM;
8900 long cnt = 0;
8901 char_u *p; /* position in buf */
8902 char_u *start; /* start of current line */
8903
8904 if (argvars[1].v_type != VAR_UNKNOWN)
8905 {
8906 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
8907 binary = TRUE;
8908 if (argvars[2].v_type != VAR_UNKNOWN)
8909 maxline = (long)get_tv_number(&argvars[2]);
8910 }
8911
8912 if (rettv_list_alloc(rettv) == FAIL)
8913 return;
8914
8915 /* Always open the file in binary mode, library functions have a mind of
8916 * their own about CR-LF conversion. */
8917 fname = get_tv_string(&argvars[0]);
8918 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
8919 {
8920 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
8921 return;
8922 }
8923
8924 while (cnt < maxline || maxline < 0)
8925 {
8926 readlen = (int)fread(buf, 1, io_size, fd);
8927
8928 /* This for loop processes what was read, but is also entered at end
8929 * of file so that either:
8930 * - an incomplete line gets written
8931 * - a "binary" file gets an empty line at the end if it ends in a
8932 * newline. */
8933 for (p = buf, start = buf;
8934 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
8935 ++p)
8936 {
8937 if (*p == '\n' || readlen <= 0)
8938 {
8939 listitem_T *li;
8940 char_u *s = NULL;
8941 long_u len = p - start;
8942
8943 /* Finished a line. Remove CRs before NL. */
8944 if (readlen > 0 && !binary)
8945 {
8946 while (len > 0 && start[len - 1] == '\r')
8947 --len;
8948 /* removal may cross back to the "prev" string */
8949 if (len == 0)
8950 while (prevlen > 0 && prev[prevlen - 1] == '\r')
8951 --prevlen;
8952 }
8953 if (prevlen == 0)
8954 s = vim_strnsave(start, (int)len);
8955 else
8956 {
8957 /* Change "prev" buffer to be the right size. This way
8958 * the bytes are only copied once, and very long lines are
8959 * allocated only once. */
8960 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
8961 {
8962 mch_memmove(s + prevlen, start, len);
8963 s[prevlen + len] = NUL;
8964 prev = NULL; /* the list will own the string */
8965 prevlen = prevsize = 0;
8966 }
8967 }
8968 if (s == NULL)
8969 {
8970 do_outofmem_msg((long_u) prevlen + len + 1);
8971 failed = TRUE;
8972 break;
8973 }
8974
8975 if ((li = listitem_alloc()) == NULL)
8976 {
8977 vim_free(s);
8978 failed = TRUE;
8979 break;
8980 }
8981 li->li_tv.v_type = VAR_STRING;
8982 li->li_tv.v_lock = 0;
8983 li->li_tv.vval.v_string = s;
8984 list_append(rettv->vval.v_list, li);
8985
8986 start = p + 1; /* step over newline */
8987 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
8988 break;
8989 }
8990 else if (*p == NUL)
8991 *p = '\n';
8992#ifdef FEAT_MBYTE
8993 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
8994 * when finding the BF and check the previous two bytes. */
8995 else if (*p == 0xbf && enc_utf8 && !binary)
8996 {
8997 /* Find the two bytes before the 0xbf. If p is at buf, or buf
8998 * + 1, these may be in the "prev" string. */
8999 char_u back1 = p >= buf + 1 ? p[-1]
9000 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
9001 char_u back2 = p >= buf + 2 ? p[-2]
9002 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
9003 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
9004
9005 if (back2 == 0xef && back1 == 0xbb)
9006 {
9007 char_u *dest = p - 2;
9008
9009 /* Usually a BOM is at the beginning of a file, and so at
9010 * the beginning of a line; then we can just step over it.
9011 */
9012 if (start == dest)
9013 start = p + 1;
9014 else
9015 {
9016 /* have to shuffle buf to close gap */
9017 int adjust_prevlen = 0;
9018
9019 if (dest < buf)
9020 {
9021 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
9022 dest = buf;
9023 }
9024 if (readlen > p - buf + 1)
9025 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
9026 readlen -= 3 - adjust_prevlen;
9027 prevlen -= adjust_prevlen;
9028 p = dest - 1;
9029 }
9030 }
9031 }
9032#endif
9033 } /* for */
9034
9035 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
9036 break;
9037 if (start < p)
9038 {
9039 /* There's part of a line in buf, store it in "prev". */
9040 if (p - start + prevlen >= prevsize)
9041 {
9042 /* need bigger "prev" buffer */
9043 char_u *newprev;
9044
9045 /* A common use case is ordinary text files and "prev" gets a
9046 * fragment of a line, so the first allocation is made
9047 * small, to avoid repeatedly 'allocing' large and
9048 * 'reallocing' small. */
9049 if (prevsize == 0)
9050 prevsize = (long)(p - start);
9051 else
9052 {
9053 long grow50pc = (prevsize * 3) / 2;
9054 long growmin = (long)((p - start) * 2 + prevlen);
9055 prevsize = grow50pc > growmin ? grow50pc : growmin;
9056 }
9057 newprev = prev == NULL ? alloc(prevsize)
9058 : vim_realloc(prev, prevsize);
9059 if (newprev == NULL)
9060 {
9061 do_outofmem_msg((long_u)prevsize);
9062 failed = TRUE;
9063 break;
9064 }
9065 prev = newprev;
9066 }
9067 /* Add the line part to end of "prev". */
9068 mch_memmove(prev + prevlen, start, p - start);
9069 prevlen += (long)(p - start);
9070 }
9071 } /* while */
9072
9073 /*
9074 * For a negative line count use only the lines at the end of the file,
9075 * free the rest.
9076 */
9077 if (!failed && maxline < 0)
9078 while (cnt > -maxline)
9079 {
9080 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
9081 --cnt;
9082 }
9083
9084 if (failed)
9085 {
9086 list_free(rettv->vval.v_list);
9087 /* readfile doc says an empty list is returned on error */
9088 rettv->vval.v_list = list_alloc();
9089 }
9090
9091 vim_free(prev);
9092 fclose(fd);
9093}
9094
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009095 static void
9096return_register(int regname, typval_T *rettv)
9097{
9098 char_u buf[2] = {0, 0};
9099
9100 buf[0] = (char_u)regname;
9101 rettv->v_type = VAR_STRING;
9102 rettv->vval.v_string = vim_strsave(buf);
9103}
9104
9105/*
9106 * "reg_executing()" function
9107 */
9108 static void
9109f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9110{
9111 return_register(reg_executing, rettv);
9112}
9113
9114/*
9115 * "reg_recording()" function
9116 */
9117 static void
9118f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9119{
9120 return_register(reg_recording, rettv);
9121}
9122
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009123#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009124/*
9125 * Convert a List to proftime_T.
9126 * Return FAIL when there is something wrong.
9127 */
9128 static int
9129list2proftime(typval_T *arg, proftime_T *tm)
9130{
9131 long n1, n2;
9132 int error = FALSE;
9133
9134 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9135 || arg->vval.v_list->lv_len != 2)
9136 return FAIL;
9137 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9138 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
9139# ifdef WIN3264
9140 tm->HighPart = n1;
9141 tm->LowPart = n2;
9142# else
9143 tm->tv_sec = n1;
9144 tm->tv_usec = n2;
9145# endif
9146 return error ? FAIL : OK;
9147}
9148#endif /* FEAT_RELTIME */
9149
9150/*
9151 * "reltime()" function
9152 */
9153 static void
9154f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9155{
9156#ifdef FEAT_RELTIME
9157 proftime_T res;
9158 proftime_T start;
9159
9160 if (argvars[0].v_type == VAR_UNKNOWN)
9161 {
9162 /* No arguments: get current time. */
9163 profile_start(&res);
9164 }
9165 else if (argvars[1].v_type == VAR_UNKNOWN)
9166 {
9167 if (list2proftime(&argvars[0], &res) == FAIL)
9168 return;
9169 profile_end(&res);
9170 }
9171 else
9172 {
9173 /* Two arguments: compute the difference. */
9174 if (list2proftime(&argvars[0], &start) == FAIL
9175 || list2proftime(&argvars[1], &res) == FAIL)
9176 return;
9177 profile_sub(&res, &start);
9178 }
9179
9180 if (rettv_list_alloc(rettv) == OK)
9181 {
9182 long n1, n2;
9183
9184# ifdef WIN3264
9185 n1 = res.HighPart;
9186 n2 = res.LowPart;
9187# else
9188 n1 = res.tv_sec;
9189 n2 = res.tv_usec;
9190# endif
9191 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9192 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9193 }
9194#endif
9195}
9196
9197#ifdef FEAT_FLOAT
9198/*
9199 * "reltimefloat()" function
9200 */
9201 static void
9202f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9203{
9204# ifdef FEAT_RELTIME
9205 proftime_T tm;
9206# endif
9207
9208 rettv->v_type = VAR_FLOAT;
9209 rettv->vval.v_float = 0;
9210# ifdef FEAT_RELTIME
9211 if (list2proftime(&argvars[0], &tm) == OK)
9212 rettv->vval.v_float = profile_float(&tm);
9213# endif
9214}
9215#endif
9216
9217/*
9218 * "reltimestr()" function
9219 */
9220 static void
9221f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9222{
9223#ifdef FEAT_RELTIME
9224 proftime_T tm;
9225#endif
9226
9227 rettv->v_type = VAR_STRING;
9228 rettv->vval.v_string = NULL;
9229#ifdef FEAT_RELTIME
9230 if (list2proftime(&argvars[0], &tm) == OK)
9231 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9232#endif
9233}
9234
9235#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009236 static void
9237make_connection(void)
9238{
9239 if (X_DISPLAY == NULL
9240# ifdef FEAT_GUI
9241 && !gui.in_use
9242# endif
9243 )
9244 {
9245 x_force_connect = TRUE;
9246 setup_term_clip();
9247 x_force_connect = FALSE;
9248 }
9249}
9250
9251 static int
9252check_connection(void)
9253{
9254 make_connection();
9255 if (X_DISPLAY == NULL)
9256 {
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009257 EMSG(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009258 return FAIL;
9259 }
9260 return OK;
9261}
9262#endif
9263
9264#ifdef FEAT_CLIENTSERVER
9265 static void
9266remote_common(typval_T *argvars, typval_T *rettv, int expr)
9267{
9268 char_u *server_name;
9269 char_u *keys;
9270 char_u *r = NULL;
9271 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009272 int timeout = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009273# ifdef WIN32
9274 HWND w;
9275# else
9276 Window w;
9277# endif
9278
9279 if (check_restricted() || check_secure())
9280 return;
9281
9282# ifdef FEAT_X11
9283 if (check_connection() == FAIL)
9284 return;
9285# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009286 if (argvars[2].v_type != VAR_UNKNOWN
9287 && argvars[3].v_type != VAR_UNKNOWN)
9288 timeout = get_tv_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009289
9290 server_name = get_tv_string_chk(&argvars[0]);
9291 if (server_name == NULL)
9292 return; /* type error; errmsg already given */
9293 keys = get_tv_string_buf(&argvars[1], buf);
9294# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009295 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009296# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009297 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9298 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009299# endif
9300 {
9301 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009302 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009303 EMSG(r); /* sending worked but evaluation failed */
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009304 vim_free(r);
9305 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009306 else
9307 EMSG2(_("E241: Unable to send to %s"), server_name);
9308 return;
9309 }
9310
9311 rettv->vval.v_string = r;
9312
9313 if (argvars[2].v_type != VAR_UNKNOWN)
9314 {
9315 dictitem_T v;
9316 char_u str[30];
9317 char_u *idvar;
9318
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009319 idvar = get_tv_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009320 if (idvar != NULL && *idvar != NUL)
9321 {
9322 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9323 v.di_tv.v_type = VAR_STRING;
9324 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009325 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009326 vim_free(v.di_tv.vval.v_string);
9327 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009328 }
9329}
9330#endif
9331
9332/*
9333 * "remote_expr()" function
9334 */
9335 static void
9336f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9337{
9338 rettv->v_type = VAR_STRING;
9339 rettv->vval.v_string = NULL;
9340#ifdef FEAT_CLIENTSERVER
9341 remote_common(argvars, rettv, TRUE);
9342#endif
9343}
9344
9345/*
9346 * "remote_foreground()" function
9347 */
9348 static void
9349f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9350{
9351#ifdef FEAT_CLIENTSERVER
9352# ifdef WIN32
9353 /* On Win32 it's done in this application. */
9354 {
9355 char_u *server_name = get_tv_string_chk(&argvars[0]);
9356
9357 if (server_name != NULL)
9358 serverForeground(server_name);
9359 }
9360# else
9361 /* Send a foreground() expression to the server. */
9362 argvars[1].v_type = VAR_STRING;
9363 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9364 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009365 rettv->v_type = VAR_STRING;
9366 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009367 remote_common(argvars, rettv, TRUE);
9368 vim_free(argvars[1].vval.v_string);
9369# endif
9370#endif
9371}
9372
9373 static void
9374f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
9375{
9376#ifdef FEAT_CLIENTSERVER
9377 dictitem_T v;
9378 char_u *s = NULL;
9379# ifdef WIN32
9380 long_u n = 0;
9381# endif
9382 char_u *serverid;
9383
9384 if (check_restricted() || check_secure())
9385 {
9386 rettv->vval.v_number = -1;
9387 return;
9388 }
9389 serverid = get_tv_string_chk(&argvars[0]);
9390 if (serverid == NULL)
9391 {
9392 rettv->vval.v_number = -1;
9393 return; /* type error; errmsg already given */
9394 }
9395# ifdef WIN32
9396 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
9397 if (n == 0)
9398 rettv->vval.v_number = -1;
9399 else
9400 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009401 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009402 rettv->vval.v_number = (s != NULL);
9403 }
9404# else
9405 if (check_connection() == FAIL)
9406 return;
9407
9408 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
9409 serverStrToWin(serverid), &s);
9410# endif
9411
9412 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
9413 {
9414 char_u *retvar;
9415
9416 v.di_tv.v_type = VAR_STRING;
9417 v.di_tv.vval.v_string = vim_strsave(s);
9418 retvar = get_tv_string_chk(&argvars[1]);
9419 if (retvar != NULL)
9420 set_var(retvar, &v.di_tv, FALSE);
9421 vim_free(v.di_tv.vval.v_string);
9422 }
9423#else
9424 rettv->vval.v_number = -1;
9425#endif
9426}
9427
9428 static void
9429f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
9430{
9431 char_u *r = NULL;
9432
9433#ifdef FEAT_CLIENTSERVER
9434 char_u *serverid = get_tv_string_chk(&argvars[0]);
9435
9436 if (serverid != NULL && !check_restricted() && !check_secure())
9437 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009438 int timeout = 0;
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009439# ifdef WIN32
9440 /* The server's HWND is encoded in the 'id' parameter */
9441 long_u n = 0;
9442# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009443
9444 if (argvars[1].v_type != VAR_UNKNOWN)
9445 timeout = get_tv_number(&argvars[1]);
9446
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009447# ifdef WIN32
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009448 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
9449 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009450 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009451 if (r == NULL)
9452# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009453 if (check_connection() == FAIL
9454 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
9455 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009456# endif
9457 EMSG(_("E277: Unable to read a server reply"));
9458 }
9459#endif
9460 rettv->v_type = VAR_STRING;
9461 rettv->vval.v_string = r;
9462}
9463
9464/*
9465 * "remote_send()" function
9466 */
9467 static void
9468f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9469{
9470 rettv->v_type = VAR_STRING;
9471 rettv->vval.v_string = NULL;
9472#ifdef FEAT_CLIENTSERVER
9473 remote_common(argvars, rettv, FALSE);
9474#endif
9475}
9476
9477/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009478 * "remote_startserver()" function
9479 */
9480 static void
9481f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9482{
9483#ifdef FEAT_CLIENTSERVER
9484 char_u *server = get_tv_string_chk(&argvars[0]);
9485
9486 if (server == NULL)
9487 return; /* type error; errmsg already given */
9488 if (serverName != NULL)
9489 EMSG(_("E941: already started a server"));
9490 else
9491 {
9492# ifdef FEAT_X11
9493 if (check_connection() == OK)
9494 serverRegisterName(X_DISPLAY, server);
9495# else
9496 serverSetName(server);
9497# endif
9498 }
9499#else
9500 EMSG(_("E942: +clientserver feature not available"));
9501#endif
9502}
9503
9504/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009505 * "remove()" function
9506 */
9507 static void
9508f_remove(typval_T *argvars, typval_T *rettv)
9509{
9510 list_T *l;
9511 listitem_T *item, *item2;
9512 listitem_T *li;
9513 long idx;
9514 long end;
9515 char_u *key;
9516 dict_T *d;
9517 dictitem_T *di;
9518 char_u *arg_errmsg = (char_u *)N_("remove() argument");
9519
9520 if (argvars[0].v_type == VAR_DICT)
9521 {
9522 if (argvars[2].v_type != VAR_UNKNOWN)
9523 EMSG2(_(e_toomanyarg), "remove()");
9524 else if ((d = argvars[0].vval.v_dict) != NULL
9525 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
9526 {
9527 key = get_tv_string_chk(&argvars[1]);
9528 if (key != NULL)
9529 {
9530 di = dict_find(d, key, -1);
9531 if (di == NULL)
9532 EMSG2(_(e_dictkey), key);
9533 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
9534 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
9535 {
9536 *rettv = di->di_tv;
9537 init_tv(&di->di_tv);
9538 dictitem_remove(d, di);
9539 }
9540 }
9541 }
9542 }
9543 else if (argvars[0].v_type != VAR_LIST)
9544 EMSG2(_(e_listdictarg), "remove()");
9545 else if ((l = argvars[0].vval.v_list) != NULL
9546 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
9547 {
9548 int error = FALSE;
9549
9550 idx = (long)get_tv_number_chk(&argvars[1], &error);
9551 if (error)
9552 ; /* type error: do nothing, errmsg already given */
9553 else if ((item = list_find(l, idx)) == NULL)
9554 EMSGN(_(e_listidx), idx);
9555 else
9556 {
9557 if (argvars[2].v_type == VAR_UNKNOWN)
9558 {
9559 /* Remove one item, return its value. */
9560 vimlist_remove(l, item, item);
9561 *rettv = item->li_tv;
9562 vim_free(item);
9563 }
9564 else
9565 {
9566 /* Remove range of items, return list with values. */
9567 end = (long)get_tv_number_chk(&argvars[2], &error);
9568 if (error)
9569 ; /* type error: do nothing */
9570 else if ((item2 = list_find(l, end)) == NULL)
9571 EMSGN(_(e_listidx), end);
9572 else
9573 {
9574 int cnt = 0;
9575
9576 for (li = item; li != NULL; li = li->li_next)
9577 {
9578 ++cnt;
9579 if (li == item2)
9580 break;
9581 }
9582 if (li == NULL) /* didn't find "item2" after "item" */
9583 EMSG(_(e_invrange));
9584 else
9585 {
9586 vimlist_remove(l, item, item2);
9587 if (rettv_list_alloc(rettv) == OK)
9588 {
9589 l = rettv->vval.v_list;
9590 l->lv_first = item;
9591 l->lv_last = item2;
9592 item->li_prev = NULL;
9593 item2->li_next = NULL;
9594 l->lv_len = cnt;
9595 }
9596 }
9597 }
9598 }
9599 }
9600 }
9601}
9602
9603/*
9604 * "rename({from}, {to})" function
9605 */
9606 static void
9607f_rename(typval_T *argvars, typval_T *rettv)
9608{
9609 char_u buf[NUMBUFLEN];
9610
9611 if (check_restricted() || check_secure())
9612 rettv->vval.v_number = -1;
9613 else
9614 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
9615 get_tv_string_buf(&argvars[1], buf));
9616}
9617
9618/*
9619 * "repeat()" function
9620 */
9621 static void
9622f_repeat(typval_T *argvars, typval_T *rettv)
9623{
9624 char_u *p;
9625 int n;
9626 int slen;
9627 int len;
9628 char_u *r;
9629 int i;
9630
9631 n = (int)get_tv_number(&argvars[1]);
9632 if (argvars[0].v_type == VAR_LIST)
9633 {
9634 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9635 while (n-- > 0)
9636 if (list_extend(rettv->vval.v_list,
9637 argvars[0].vval.v_list, NULL) == FAIL)
9638 break;
9639 }
9640 else
9641 {
9642 p = get_tv_string(&argvars[0]);
9643 rettv->v_type = VAR_STRING;
9644 rettv->vval.v_string = NULL;
9645
9646 slen = (int)STRLEN(p);
9647 len = slen * n;
9648 if (len <= 0)
9649 return;
9650
9651 r = alloc(len + 1);
9652 if (r != NULL)
9653 {
9654 for (i = 0; i < n; i++)
9655 mch_memmove(r + i * slen, p, (size_t)slen);
9656 r[len] = NUL;
9657 }
9658
9659 rettv->vval.v_string = r;
9660 }
9661}
9662
9663/*
9664 * "resolve()" function
9665 */
9666 static void
9667f_resolve(typval_T *argvars, typval_T *rettv)
9668{
9669 char_u *p;
9670#ifdef HAVE_READLINK
9671 char_u *buf = NULL;
9672#endif
9673
9674 p = get_tv_string(&argvars[0]);
9675#ifdef FEAT_SHORTCUT
9676 {
9677 char_u *v = NULL;
9678
9679 v = mch_resolve_shortcut(p);
9680 if (v != NULL)
9681 rettv->vval.v_string = v;
9682 else
9683 rettv->vval.v_string = vim_strsave(p);
9684 }
9685#else
9686# ifdef HAVE_READLINK
9687 {
9688 char_u *cpy;
9689 int len;
9690 char_u *remain = NULL;
9691 char_u *q;
9692 int is_relative_to_current = FALSE;
9693 int has_trailing_pathsep = FALSE;
9694 int limit = 100;
9695
9696 p = vim_strsave(p);
9697
9698 if (p[0] == '.' && (vim_ispathsep(p[1])
9699 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9700 is_relative_to_current = TRUE;
9701
9702 len = STRLEN(p);
9703 if (len > 0 && after_pathsep(p, p + len))
9704 {
9705 has_trailing_pathsep = TRUE;
9706 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9707 }
9708
9709 q = getnextcomp(p);
9710 if (*q != NUL)
9711 {
9712 /* Separate the first path component in "p", and keep the
9713 * remainder (beginning with the path separator). */
9714 remain = vim_strsave(q - 1);
9715 q[-1] = NUL;
9716 }
9717
9718 buf = alloc(MAXPATHL + 1);
9719 if (buf == NULL)
9720 goto fail;
9721
9722 for (;;)
9723 {
9724 for (;;)
9725 {
9726 len = readlink((char *)p, (char *)buf, MAXPATHL);
9727 if (len <= 0)
9728 break;
9729 buf[len] = NUL;
9730
9731 if (limit-- == 0)
9732 {
9733 vim_free(p);
9734 vim_free(remain);
9735 EMSG(_("E655: Too many symbolic links (cycle?)"));
9736 rettv->vval.v_string = NULL;
9737 goto fail;
9738 }
9739
9740 /* Ensure that the result will have a trailing path separator
9741 * if the argument has one. */
9742 if (remain == NULL && has_trailing_pathsep)
9743 add_pathsep(buf);
9744
9745 /* Separate the first path component in the link value and
9746 * concatenate the remainders. */
9747 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9748 if (*q != NUL)
9749 {
9750 if (remain == NULL)
9751 remain = vim_strsave(q - 1);
9752 else
9753 {
9754 cpy = concat_str(q - 1, remain);
9755 if (cpy != NULL)
9756 {
9757 vim_free(remain);
9758 remain = cpy;
9759 }
9760 }
9761 q[-1] = NUL;
9762 }
9763
9764 q = gettail(p);
9765 if (q > p && *q == NUL)
9766 {
9767 /* Ignore trailing path separator. */
9768 q[-1] = NUL;
9769 q = gettail(p);
9770 }
9771 if (q > p && !mch_isFullName(buf))
9772 {
9773 /* symlink is relative to directory of argument */
9774 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
9775 if (cpy != NULL)
9776 {
9777 STRCPY(cpy, p);
9778 STRCPY(gettail(cpy), buf);
9779 vim_free(p);
9780 p = cpy;
9781 }
9782 }
9783 else
9784 {
9785 vim_free(p);
9786 p = vim_strsave(buf);
9787 }
9788 }
9789
9790 if (remain == NULL)
9791 break;
9792
9793 /* Append the first path component of "remain" to "p". */
9794 q = getnextcomp(remain + 1);
9795 len = q - remain - (*q != NUL);
9796 cpy = vim_strnsave(p, STRLEN(p) + len);
9797 if (cpy != NULL)
9798 {
9799 STRNCAT(cpy, remain, len);
9800 vim_free(p);
9801 p = cpy;
9802 }
9803 /* Shorten "remain". */
9804 if (*q != NUL)
9805 STRMOVE(remain, q - 1);
9806 else
Bram Moolenaard23a8232018-02-10 18:45:26 +01009807 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009808 }
9809
9810 /* If the result is a relative path name, make it explicitly relative to
9811 * the current directory if and only if the argument had this form. */
9812 if (!vim_ispathsep(*p))
9813 {
9814 if (is_relative_to_current
9815 && *p != NUL
9816 && !(p[0] == '.'
9817 && (p[1] == NUL
9818 || vim_ispathsep(p[1])
9819 || (p[1] == '.'
9820 && (p[2] == NUL
9821 || vim_ispathsep(p[2]))))))
9822 {
9823 /* Prepend "./". */
9824 cpy = concat_str((char_u *)"./", p);
9825 if (cpy != NULL)
9826 {
9827 vim_free(p);
9828 p = cpy;
9829 }
9830 }
9831 else if (!is_relative_to_current)
9832 {
9833 /* Strip leading "./". */
9834 q = p;
9835 while (q[0] == '.' && vim_ispathsep(q[1]))
9836 q += 2;
9837 if (q > p)
9838 STRMOVE(p, p + 2);
9839 }
9840 }
9841
9842 /* Ensure that the result will have no trailing path separator
9843 * if the argument had none. But keep "/" or "//". */
9844 if (!has_trailing_pathsep)
9845 {
9846 q = p + STRLEN(p);
9847 if (after_pathsep(p, q))
9848 *gettail_sep(p) = NUL;
9849 }
9850
9851 rettv->vval.v_string = p;
9852 }
9853# else
9854 rettv->vval.v_string = vim_strsave(p);
9855# endif
9856#endif
9857
9858 simplify_filename(rettv->vval.v_string);
9859
9860#ifdef HAVE_READLINK
9861fail:
9862 vim_free(buf);
9863#endif
9864 rettv->v_type = VAR_STRING;
9865}
9866
9867/*
9868 * "reverse({list})" function
9869 */
9870 static void
9871f_reverse(typval_T *argvars, typval_T *rettv)
9872{
9873 list_T *l;
9874 listitem_T *li, *ni;
9875
9876 if (argvars[0].v_type != VAR_LIST)
9877 EMSG2(_(e_listarg), "reverse()");
9878 else if ((l = argvars[0].vval.v_list) != NULL
9879 && !tv_check_lock(l->lv_lock,
9880 (char_u *)N_("reverse() argument"), TRUE))
9881 {
9882 li = l->lv_last;
9883 l->lv_first = l->lv_last = NULL;
9884 l->lv_len = 0;
9885 while (li != NULL)
9886 {
9887 ni = li->li_prev;
9888 list_append(l, li);
9889 li = ni;
9890 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009891 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009892 l->lv_idx = l->lv_len - l->lv_idx - 1;
9893 }
9894}
9895
9896#define SP_NOMOVE 0x01 /* don't move cursor */
9897#define SP_REPEAT 0x02 /* repeat to find outer pair */
9898#define SP_RETCOUNT 0x04 /* return matchcount */
9899#define SP_SETPCMARK 0x08 /* set previous context mark */
9900#define SP_START 0x10 /* accept match at start position */
9901#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
9902#define SP_END 0x40 /* leave cursor at end of match */
9903#define SP_COLUMN 0x80 /* start at cursor column */
9904
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009905/*
9906 * Get flags for a search function.
9907 * Possibly sets "p_ws".
9908 * Returns BACKWARD, FORWARD or zero (for an error).
9909 */
9910 static int
9911get_search_arg(typval_T *varp, int *flagsp)
9912{
9913 int dir = FORWARD;
9914 char_u *flags;
9915 char_u nbuf[NUMBUFLEN];
9916 int mask;
9917
9918 if (varp->v_type != VAR_UNKNOWN)
9919 {
9920 flags = get_tv_string_buf_chk(varp, nbuf);
9921 if (flags == NULL)
9922 return 0; /* type error; errmsg already given */
9923 while (*flags != NUL)
9924 {
9925 switch (*flags)
9926 {
9927 case 'b': dir = BACKWARD; break;
9928 case 'w': p_ws = TRUE; break;
9929 case 'W': p_ws = FALSE; break;
9930 default: mask = 0;
9931 if (flagsp != NULL)
9932 switch (*flags)
9933 {
9934 case 'c': mask = SP_START; break;
9935 case 'e': mask = SP_END; break;
9936 case 'm': mask = SP_RETCOUNT; break;
9937 case 'n': mask = SP_NOMOVE; break;
9938 case 'p': mask = SP_SUBPAT; break;
9939 case 'r': mask = SP_REPEAT; break;
9940 case 's': mask = SP_SETPCMARK; break;
9941 case 'z': mask = SP_COLUMN; break;
9942 }
9943 if (mask == 0)
9944 {
9945 EMSG2(_(e_invarg2), flags);
9946 dir = 0;
9947 }
9948 else
9949 *flagsp |= mask;
9950 }
9951 if (dir == 0)
9952 break;
9953 ++flags;
9954 }
9955 }
9956 return dir;
9957}
9958
9959/*
9960 * Shared by search() and searchpos() functions.
9961 */
9962 static int
9963search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
9964{
9965 int flags;
9966 char_u *pat;
9967 pos_T pos;
9968 pos_T save_cursor;
9969 int save_p_ws = p_ws;
9970 int dir;
9971 int retval = 0; /* default: FAIL */
9972 long lnum_stop = 0;
9973 proftime_T tm;
9974#ifdef FEAT_RELTIME
9975 long time_limit = 0;
9976#endif
9977 int options = SEARCH_KEEP;
9978 int subpatnum;
9979
9980 pat = get_tv_string(&argvars[0]);
9981 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
9982 if (dir == 0)
9983 goto theend;
9984 flags = *flagsp;
9985 if (flags & SP_START)
9986 options |= SEARCH_START;
9987 if (flags & SP_END)
9988 options |= SEARCH_END;
9989 if (flags & SP_COLUMN)
9990 options |= SEARCH_COL;
9991
9992 /* Optional arguments: line number to stop searching and timeout. */
9993 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9994 {
9995 lnum_stop = (long)get_tv_number_chk(&argvars[2], NULL);
9996 if (lnum_stop < 0)
9997 goto theend;
9998#ifdef FEAT_RELTIME
9999 if (argvars[3].v_type != VAR_UNKNOWN)
10000 {
10001 time_limit = (long)get_tv_number_chk(&argvars[3], NULL);
10002 if (time_limit < 0)
10003 goto theend;
10004 }
10005#endif
10006 }
10007
10008#ifdef FEAT_RELTIME
10009 /* Set the time limit, if there is one. */
10010 profile_setlimit(time_limit, &tm);
10011#endif
10012
10013 /*
10014 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
10015 * Check to make sure only those flags are set.
10016 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
10017 * flags cannot be set. Check for that condition also.
10018 */
10019 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
10020 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10021 {
10022 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
10023 goto theend;
10024 }
10025
10026 pos = save_cursor = curwin->w_cursor;
10027 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010028 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010029 if (subpatnum != FAIL)
10030 {
10031 if (flags & SP_SUBPAT)
10032 retval = subpatnum;
10033 else
10034 retval = pos.lnum;
10035 if (flags & SP_SETPCMARK)
10036 setpcmark();
10037 curwin->w_cursor = pos;
10038 if (match_pos != NULL)
10039 {
10040 /* Store the match cursor position */
10041 match_pos->lnum = pos.lnum;
10042 match_pos->col = pos.col + 1;
10043 }
10044 /* "/$" will put the cursor after the end of the line, may need to
10045 * correct that here */
10046 check_cursor();
10047 }
10048
10049 /* If 'n' flag is used: restore cursor position. */
10050 if (flags & SP_NOMOVE)
10051 curwin->w_cursor = save_cursor;
10052 else
10053 curwin->w_set_curswant = TRUE;
10054theend:
10055 p_ws = save_p_ws;
10056
10057 return retval;
10058}
10059
10060#ifdef FEAT_FLOAT
10061
10062/*
10063 * round() is not in C90, use ceil() or floor() instead.
10064 */
10065 float_T
10066vim_round(float_T f)
10067{
10068 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
10069}
10070
10071/*
10072 * "round({float})" function
10073 */
10074 static void
10075f_round(typval_T *argvars, typval_T *rettv)
10076{
10077 float_T f = 0.0;
10078
10079 rettv->v_type = VAR_FLOAT;
10080 if (get_float_arg(argvars, &f) == OK)
10081 rettv->vval.v_float = vim_round(f);
10082 else
10083 rettv->vval.v_float = 0.0;
10084}
10085#endif
10086
10087/*
10088 * "screenattr()" function
10089 */
10090 static void
10091f_screenattr(typval_T *argvars, typval_T *rettv)
10092{
10093 int row;
10094 int col;
10095 int c;
10096
10097 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
10098 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
10099 if (row < 0 || row >= screen_Rows
10100 || col < 0 || col >= screen_Columns)
10101 c = -1;
10102 else
10103 c = ScreenAttrs[LineOffset[row] + col];
10104 rettv->vval.v_number = c;
10105}
10106
10107/*
10108 * "screenchar()" function
10109 */
10110 static void
10111f_screenchar(typval_T *argvars, typval_T *rettv)
10112{
10113 int row;
10114 int col;
10115 int off;
10116 int c;
10117
10118 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
10119 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
10120 if (row < 0 || row >= screen_Rows
10121 || col < 0 || col >= screen_Columns)
10122 c = -1;
10123 else
10124 {
10125 off = LineOffset[row] + col;
10126#ifdef FEAT_MBYTE
10127 if (enc_utf8 && ScreenLinesUC[off] != 0)
10128 c = ScreenLinesUC[off];
10129 else
10130#endif
10131 c = ScreenLines[off];
10132 }
10133 rettv->vval.v_number = c;
10134}
10135
10136/*
10137 * "screencol()" function
10138 *
10139 * First column is 1 to be consistent with virtcol().
10140 */
10141 static void
10142f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10143{
10144 rettv->vval.v_number = screen_screencol() + 1;
10145}
10146
10147/*
10148 * "screenrow()" function
10149 */
10150 static void
10151f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10152{
10153 rettv->vval.v_number = screen_screenrow() + 1;
10154}
10155
10156/*
10157 * "search()" function
10158 */
10159 static void
10160f_search(typval_T *argvars, typval_T *rettv)
10161{
10162 int flags = 0;
10163
10164 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10165}
10166
10167/*
10168 * "searchdecl()" function
10169 */
10170 static void
10171f_searchdecl(typval_T *argvars, typval_T *rettv)
10172{
10173 int locally = 1;
10174 int thisblock = 0;
10175 int error = FALSE;
10176 char_u *name;
10177
10178 rettv->vval.v_number = 1; /* default: FAIL */
10179
10180 name = get_tv_string_chk(&argvars[0]);
10181 if (argvars[1].v_type != VAR_UNKNOWN)
10182 {
10183 locally = (int)get_tv_number_chk(&argvars[1], &error) == 0;
10184 if (!error && argvars[2].v_type != VAR_UNKNOWN)
10185 thisblock = (int)get_tv_number_chk(&argvars[2], &error) != 0;
10186 }
10187 if (!error && name != NULL)
10188 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10189 locally, thisblock, SEARCH_KEEP) == FAIL;
10190}
10191
10192/*
10193 * Used by searchpair() and searchpairpos()
10194 */
10195 static int
10196searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10197{
10198 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010199 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010200 int save_p_ws = p_ws;
10201 int dir;
10202 int flags = 0;
10203 char_u nbuf1[NUMBUFLEN];
10204 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010205 int retval = 0; /* default: FAIL */
10206 long lnum_stop = 0;
10207 long time_limit = 0;
10208
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010209 /* Get the three pattern arguments: start, middle, end. Will result in an
10210 * error if not a valid argument. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010211 spat = get_tv_string_chk(&argvars[0]);
10212 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
10213 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
10214 if (spat == NULL || mpat == NULL || epat == NULL)
10215 goto theend; /* type error */
10216
10217 /* Handle the optional fourth argument: flags */
10218 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
10219 if (dir == 0)
10220 goto theend;
10221
10222 /* Don't accept SP_END or SP_SUBPAT.
10223 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
10224 */
10225 if ((flags & (SP_END | SP_SUBPAT)) != 0
10226 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10227 {
10228 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
10229 goto theend;
10230 }
10231
10232 /* Using 'r' implies 'W', otherwise it doesn't work. */
10233 if (flags & SP_REPEAT)
10234 p_ws = FALSE;
10235
10236 /* Optional fifth argument: skip expression */
10237 if (argvars[3].v_type == VAR_UNKNOWN
10238 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010010239 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010240 else
10241 {
Bram Moolenaar48570482017-10-30 21:48:41 +010010242 skip = &argvars[4];
10243 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
10244 && skip->v_type != VAR_STRING)
10245 {
10246 /* Type error */
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010247 EMSG2(_(e_invarg2), get_tv_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010010248 goto theend;
10249 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010250 if (argvars[5].v_type != VAR_UNKNOWN)
10251 {
10252 lnum_stop = (long)get_tv_number_chk(&argvars[5], NULL);
10253 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010254 {
10255 EMSG2(_(e_invarg2), get_tv_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010256 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010257 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010258#ifdef FEAT_RELTIME
10259 if (argvars[6].v_type != VAR_UNKNOWN)
10260 {
10261 time_limit = (long)get_tv_number_chk(&argvars[6], NULL);
10262 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010263 {
10264 EMSG2(_(e_invarg2), get_tv_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010265 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010266 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010267 }
10268#endif
10269 }
10270 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010271
10272 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
10273 match_pos, lnum_stop, time_limit);
10274
10275theend:
10276 p_ws = save_p_ws;
10277
10278 return retval;
10279}
10280
10281/*
10282 * "searchpair()" function
10283 */
10284 static void
10285f_searchpair(typval_T *argvars, typval_T *rettv)
10286{
10287 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
10288}
10289
10290/*
10291 * "searchpairpos()" function
10292 */
10293 static void
10294f_searchpairpos(typval_T *argvars, typval_T *rettv)
10295{
10296 pos_T match_pos;
10297 int lnum = 0;
10298 int col = 0;
10299
10300 if (rettv_list_alloc(rettv) == FAIL)
10301 return;
10302
10303 if (searchpair_cmn(argvars, &match_pos) > 0)
10304 {
10305 lnum = match_pos.lnum;
10306 col = match_pos.col;
10307 }
10308
10309 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10310 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10311}
10312
10313/*
10314 * Search for a start/middle/end thing.
10315 * Used by searchpair(), see its documentation for the details.
10316 * Returns 0 or -1 for no match,
10317 */
10318 long
10319do_searchpair(
10320 char_u *spat, /* start pattern */
10321 char_u *mpat, /* middle pattern */
10322 char_u *epat, /* end pattern */
10323 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010010324 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010325 int flags, /* SP_SETPCMARK and other SP_ values */
10326 pos_T *match_pos,
10327 linenr_T lnum_stop, /* stop at this line if not zero */
10328 long time_limit UNUSED) /* stop after this many msec */
10329{
10330 char_u *save_cpo;
10331 char_u *pat, *pat2 = NULL, *pat3 = NULL;
10332 long retval = 0;
10333 pos_T pos;
10334 pos_T firstpos;
10335 pos_T foundpos;
10336 pos_T save_cursor;
10337 pos_T save_pos;
10338 int n;
10339 int r;
10340 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010010341 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010342 int err;
10343 int options = SEARCH_KEEP;
10344 proftime_T tm;
10345
10346 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10347 save_cpo = p_cpo;
10348 p_cpo = empty_option;
10349
10350#ifdef FEAT_RELTIME
10351 /* Set the time limit, if there is one. */
10352 profile_setlimit(time_limit, &tm);
10353#endif
10354
10355 /* Make two search patterns: start/end (pat2, for in nested pairs) and
10356 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010357 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
10358 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010359 if (pat2 == NULL || pat3 == NULL)
10360 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010361 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010362 if (*mpat == NUL)
10363 STRCPY(pat3, pat2);
10364 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010365 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010366 spat, epat, mpat);
10367 if (flags & SP_START)
10368 options |= SEARCH_START;
10369
Bram Moolenaar48570482017-10-30 21:48:41 +010010370 if (skip != NULL)
10371 {
10372 /* Empty string means to not use the skip expression. */
10373 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
10374 use_skip = skip->vval.v_string != NULL
10375 && *skip->vval.v_string != NUL;
10376 }
10377
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010378 save_cursor = curwin->w_cursor;
10379 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010380 CLEAR_POS(&firstpos);
10381 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010382 pat = pat3;
10383 for (;;)
10384 {
10385 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010386 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010387 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010388 /* didn't find it or found the first match again: FAIL */
10389 break;
10390
10391 if (firstpos.lnum == 0)
10392 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010393 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010394 {
10395 /* Found the same position again. Can happen with a pattern that
10396 * has "\zs" at the end and searching backwards. Advance one
10397 * character and try again. */
10398 if (dir == BACKWARD)
10399 decl(&pos);
10400 else
10401 incl(&pos);
10402 }
10403 foundpos = pos;
10404
10405 /* clear the start flag to avoid getting stuck here */
10406 options &= ~SEARCH_START;
10407
10408 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010010409 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010410 {
10411 save_pos = curwin->w_cursor;
10412 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010010413 err = FALSE;
10414 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010415 curwin->w_cursor = save_pos;
10416 if (err)
10417 {
10418 /* Evaluating {skip} caused an error, break here. */
10419 curwin->w_cursor = save_cursor;
10420 retval = -1;
10421 break;
10422 }
10423 if (r)
10424 continue;
10425 }
10426
10427 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
10428 {
10429 /* Found end when searching backwards or start when searching
10430 * forward: nested pair. */
10431 ++nest;
10432 pat = pat2; /* nested, don't search for middle */
10433 }
10434 else
10435 {
10436 /* Found end when searching forward or start when searching
10437 * backward: end of (nested) pair; or found middle in outer pair. */
10438 if (--nest == 1)
10439 pat = pat3; /* outer level, search for middle */
10440 }
10441
10442 if (nest == 0)
10443 {
10444 /* Found the match: return matchcount or line number. */
10445 if (flags & SP_RETCOUNT)
10446 ++retval;
10447 else
10448 retval = pos.lnum;
10449 if (flags & SP_SETPCMARK)
10450 setpcmark();
10451 curwin->w_cursor = pos;
10452 if (!(flags & SP_REPEAT))
10453 break;
10454 nest = 1; /* search for next unmatched */
10455 }
10456 }
10457
10458 if (match_pos != NULL)
10459 {
10460 /* Store the match cursor position */
10461 match_pos->lnum = curwin->w_cursor.lnum;
10462 match_pos->col = curwin->w_cursor.col + 1;
10463 }
10464
10465 /* If 'n' flag is used or search failed: restore cursor position. */
10466 if ((flags & SP_NOMOVE) || retval == 0)
10467 curwin->w_cursor = save_cursor;
10468
10469theend:
10470 vim_free(pat2);
10471 vim_free(pat3);
10472 if (p_cpo == empty_option)
10473 p_cpo = save_cpo;
10474 else
10475 /* Darn, evaluating the {skip} expression changed the value. */
10476 free_string_option(save_cpo);
10477
10478 return retval;
10479}
10480
10481/*
10482 * "searchpos()" function
10483 */
10484 static void
10485f_searchpos(typval_T *argvars, typval_T *rettv)
10486{
10487 pos_T match_pos;
10488 int lnum = 0;
10489 int col = 0;
10490 int n;
10491 int flags = 0;
10492
10493 if (rettv_list_alloc(rettv) == FAIL)
10494 return;
10495
10496 n = search_cmn(argvars, &match_pos, &flags);
10497 if (n > 0)
10498 {
10499 lnum = match_pos.lnum;
10500 col = match_pos.col;
10501 }
10502
10503 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10504 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10505 if (flags & SP_SUBPAT)
10506 list_append_number(rettv->vval.v_list, (varnumber_T)n);
10507}
10508
10509 static void
10510f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
10511{
10512#ifdef FEAT_CLIENTSERVER
10513 char_u buf[NUMBUFLEN];
10514 char_u *server = get_tv_string_chk(&argvars[0]);
10515 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
10516
10517 rettv->vval.v_number = -1;
10518 if (server == NULL || reply == NULL)
10519 return;
10520 if (check_restricted() || check_secure())
10521 return;
10522# ifdef FEAT_X11
10523 if (check_connection() == FAIL)
10524 return;
10525# endif
10526
10527 if (serverSendReply(server, reply) < 0)
10528 {
10529 EMSG(_("E258: Unable to send to client"));
10530 return;
10531 }
10532 rettv->vval.v_number = 0;
10533#else
10534 rettv->vval.v_number = -1;
10535#endif
10536}
10537
10538 static void
10539f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
10540{
10541 char_u *r = NULL;
10542
10543#ifdef FEAT_CLIENTSERVER
10544# ifdef WIN32
10545 r = serverGetVimNames();
10546# else
10547 make_connection();
10548 if (X_DISPLAY != NULL)
10549 r = serverGetVimNames(X_DISPLAY);
10550# endif
10551#endif
10552 rettv->v_type = VAR_STRING;
10553 rettv->vval.v_string = r;
10554}
10555
10556/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010557 * "setbufline()" function
10558 */
10559 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020010560f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010561{
10562 linenr_T lnum;
10563 buf_T *buf;
10564
10565 buf = get_buf_tv(&argvars[0], FALSE);
10566 if (buf == NULL)
10567 rettv->vval.v_number = 1; /* FAIL */
10568 else
10569 {
10570 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020010571 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010572 }
10573}
10574
10575/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010576 * "setbufvar()" function
10577 */
10578 static void
10579f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10580{
10581 buf_T *buf;
10582 char_u *varname, *bufvarname;
10583 typval_T *varp;
10584 char_u nbuf[NUMBUFLEN];
10585
10586 if (check_restricted() || check_secure())
10587 return;
10588 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10589 varname = get_tv_string_chk(&argvars[1]);
10590 buf = get_buf_tv(&argvars[0], FALSE);
10591 varp = &argvars[2];
10592
10593 if (buf != NULL && varname != NULL && varp != NULL)
10594 {
10595 if (*varname == '&')
10596 {
10597 long numval;
10598 char_u *strval;
10599 int error = FALSE;
10600 aco_save_T aco;
10601
10602 /* set curbuf to be our buf, temporarily */
10603 aucmd_prepbuf(&aco, buf);
10604
10605 ++varname;
10606 numval = (long)get_tv_number_chk(varp, &error);
10607 strval = get_tv_string_buf_chk(varp, nbuf);
10608 if (!error && strval != NULL)
10609 set_option_value(varname, numval, strval, OPT_LOCAL);
10610
10611 /* reset notion of buffer */
10612 aucmd_restbuf(&aco);
10613 }
10614 else
10615 {
10616 buf_T *save_curbuf = curbuf;
10617
10618 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
10619 if (bufvarname != NULL)
10620 {
10621 curbuf = buf;
10622 STRCPY(bufvarname, "b:");
10623 STRCPY(bufvarname + 2, varname);
10624 set_var(bufvarname, varp, TRUE);
10625 vim_free(bufvarname);
10626 curbuf = save_curbuf;
10627 }
10628 }
10629 }
10630}
10631
10632 static void
10633f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10634{
10635 dict_T *d;
10636 dictitem_T *di;
10637 char_u *csearch;
10638
10639 if (argvars[0].v_type != VAR_DICT)
10640 {
10641 EMSG(_(e_dictreq));
10642 return;
10643 }
10644
10645 if ((d = argvars[0].vval.v_dict) != NULL)
10646 {
10647 csearch = get_dict_string(d, (char_u *)"char", FALSE);
10648 if (csearch != NULL)
10649 {
10650#ifdef FEAT_MBYTE
10651 if (enc_utf8)
10652 {
10653 int pcc[MAX_MCO];
10654 int c = utfc_ptr2char(csearch, pcc);
10655
10656 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
10657 }
10658 else
10659#endif
10660 set_last_csearch(PTR2CHAR(csearch),
10661 csearch, MB_PTR2LEN(csearch));
10662 }
10663
10664 di = dict_find(d, (char_u *)"forward", -1);
10665 if (di != NULL)
10666 set_csearch_direction((int)get_tv_number(&di->di_tv)
10667 ? FORWARD : BACKWARD);
10668
10669 di = dict_find(d, (char_u *)"until", -1);
10670 if (di != NULL)
10671 set_csearch_until(!!get_tv_number(&di->di_tv));
10672 }
10673}
10674
10675/*
10676 * "setcmdpos()" function
10677 */
10678 static void
10679f_setcmdpos(typval_T *argvars, typval_T *rettv)
10680{
10681 int pos = (int)get_tv_number(&argvars[0]) - 1;
10682
10683 if (pos >= 0)
10684 rettv->vval.v_number = set_cmdline_pos(pos);
10685}
10686
10687/*
10688 * "setfperm({fname}, {mode})" function
10689 */
10690 static void
10691f_setfperm(typval_T *argvars, typval_T *rettv)
10692{
10693 char_u *fname;
10694 char_u modebuf[NUMBUFLEN];
10695 char_u *mode_str;
10696 int i;
10697 int mask;
10698 int mode = 0;
10699
10700 rettv->vval.v_number = 0;
10701 fname = get_tv_string_chk(&argvars[0]);
10702 if (fname == NULL)
10703 return;
10704 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
10705 if (mode_str == NULL)
10706 return;
10707 if (STRLEN(mode_str) != 9)
10708 {
10709 EMSG2(_(e_invarg2), mode_str);
10710 return;
10711 }
10712
10713 mask = 1;
10714 for (i = 8; i >= 0; --i)
10715 {
10716 if (mode_str[i] != '-')
10717 mode |= mask;
10718 mask = mask << 1;
10719 }
10720 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
10721}
10722
10723/*
10724 * "setline()" function
10725 */
10726 static void
10727f_setline(typval_T *argvars, typval_T *rettv)
10728{
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010729 linenr_T lnum = get_tv_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010730
Bram Moolenaarca851592018-06-06 21:04:07 +020010731 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010732}
10733
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010734/*
10735 * Used by "setqflist()" and "setloclist()" functions
10736 */
10737 static void
10738set_qf_ll_list(
10739 win_T *wp UNUSED,
10740 typval_T *list_arg UNUSED,
10741 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020010742 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010743 typval_T *rettv)
10744{
10745#ifdef FEAT_QUICKFIX
10746 static char *e_invact = N_("E927: Invalid action: '%s'");
10747 char_u *act;
10748 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010749 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010750#endif
10751
10752 rettv->vval.v_number = -1;
10753
10754#ifdef FEAT_QUICKFIX
10755 if (list_arg->v_type != VAR_LIST)
10756 EMSG(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010757 else if (recursive != 0)
10758 EMSG(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010759 else
10760 {
10761 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010762 dict_T *d = NULL;
10763 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010764
10765 if (action_arg->v_type == VAR_STRING)
10766 {
10767 act = get_tv_string_chk(action_arg);
10768 if (act == NULL)
10769 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020010770 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
10771 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010772 action = *act;
10773 else
10774 EMSG2(_(e_invact), act);
10775 }
10776 else if (action_arg->v_type == VAR_UNKNOWN)
10777 action = ' ';
10778 else
10779 EMSG(_(e_stringreq));
10780
Bram Moolenaard823fa92016-08-12 16:29:27 +020010781 if (action_arg->v_type != VAR_UNKNOWN
10782 && what_arg->v_type != VAR_UNKNOWN)
10783 {
10784 if (what_arg->v_type == VAR_DICT)
10785 d = what_arg->vval.v_dict;
10786 else
10787 {
10788 EMSG(_(e_dictreq));
10789 valid_dict = FALSE;
10790 }
10791 }
10792
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010793 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010794 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010795 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
10796 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010797 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010798 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010799 }
10800#endif
10801}
10802
10803/*
10804 * "setloclist()" function
10805 */
10806 static void
10807f_setloclist(typval_T *argvars, typval_T *rettv)
10808{
10809 win_T *win;
10810
10811 rettv->vval.v_number = -1;
10812
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020010813 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010814 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020010815 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010816}
10817
10818/*
10819 * "setmatches()" function
10820 */
10821 static void
10822f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10823{
10824#ifdef FEAT_SEARCH_EXTRA
10825 list_T *l;
10826 listitem_T *li;
10827 dict_T *d;
10828 list_T *s = NULL;
10829
10830 rettv->vval.v_number = -1;
10831 if (argvars[0].v_type != VAR_LIST)
10832 {
10833 EMSG(_(e_listreq));
10834 return;
10835 }
10836 if ((l = argvars[0].vval.v_list) != NULL)
10837 {
10838
10839 /* To some extent make sure that we are dealing with a list from
10840 * "getmatches()". */
10841 li = l->lv_first;
10842 while (li != NULL)
10843 {
10844 if (li->li_tv.v_type != VAR_DICT
10845 || (d = li->li_tv.vval.v_dict) == NULL)
10846 {
10847 EMSG(_(e_invarg));
10848 return;
10849 }
10850 if (!(dict_find(d, (char_u *)"group", -1) != NULL
10851 && (dict_find(d, (char_u *)"pattern", -1) != NULL
10852 || dict_find(d, (char_u *)"pos1", -1) != NULL)
10853 && dict_find(d, (char_u *)"priority", -1) != NULL
10854 && dict_find(d, (char_u *)"id", -1) != NULL))
10855 {
10856 EMSG(_(e_invarg));
10857 return;
10858 }
10859 li = li->li_next;
10860 }
10861
10862 clear_matches(curwin);
10863 li = l->lv_first;
10864 while (li != NULL)
10865 {
10866 int i = 0;
10867 char_u buf[5];
10868 dictitem_T *di;
10869 char_u *group;
10870 int priority;
10871 int id;
10872 char_u *conceal;
10873
10874 d = li->li_tv.vval.v_dict;
10875 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
10876 {
10877 if (s == NULL)
10878 {
10879 s = list_alloc();
10880 if (s == NULL)
10881 return;
10882 }
10883
10884 /* match from matchaddpos() */
10885 for (i = 1; i < 9; i++)
10886 {
10887 sprintf((char *)buf, (char *)"pos%d", i);
10888 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
10889 {
10890 if (di->di_tv.v_type != VAR_LIST)
10891 return;
10892
10893 list_append_tv(s, &di->di_tv);
10894 s->lv_refcount++;
10895 }
10896 else
10897 break;
10898 }
10899 }
10900
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010901 group = get_dict_string(d, (char_u *)"group", TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010902 priority = (int)get_dict_number(d, (char_u *)"priority");
10903 id = (int)get_dict_number(d, (char_u *)"id");
10904 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010905 ? get_dict_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010906 : NULL;
10907 if (i == 0)
10908 {
10909 match_add(curwin, group,
10910 get_dict_string(d, (char_u *)"pattern", FALSE),
10911 priority, id, NULL, conceal);
10912 }
10913 else
10914 {
10915 match_add(curwin, group, NULL, priority, id, s, conceal);
10916 list_unref(s);
10917 s = NULL;
10918 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010919 vim_free(group);
10920 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010921
10922 li = li->li_next;
10923 }
10924 rettv->vval.v_number = 0;
10925 }
10926#endif
10927}
10928
10929/*
10930 * "setpos()" function
10931 */
10932 static void
10933f_setpos(typval_T *argvars, typval_T *rettv)
10934{
10935 pos_T pos;
10936 int fnum;
10937 char_u *name;
10938 colnr_T curswant = -1;
10939
10940 rettv->vval.v_number = -1;
10941 name = get_tv_string_chk(argvars);
10942 if (name != NULL)
10943 {
10944 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
10945 {
10946 if (--pos.col < 0)
10947 pos.col = 0;
10948 if (name[0] == '.' && name[1] == NUL)
10949 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010950 /* set cursor; "fnum" is ignored */
10951 curwin->w_cursor = pos;
10952 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010953 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010954 curwin->w_curswant = curswant - 1;
10955 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010956 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010957 check_cursor();
10958 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010959 }
10960 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
10961 {
10962 /* set mark */
10963 if (setmark_pos(name[1], &pos, fnum) == OK)
10964 rettv->vval.v_number = 0;
10965 }
10966 else
10967 EMSG(_(e_invarg));
10968 }
10969 }
10970}
10971
10972/*
10973 * "setqflist()" function
10974 */
10975 static void
10976f_setqflist(typval_T *argvars, typval_T *rettv)
10977{
Bram Moolenaard823fa92016-08-12 16:29:27 +020010978 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010979}
10980
10981/*
10982 * "setreg()" function
10983 */
10984 static void
10985f_setreg(typval_T *argvars, typval_T *rettv)
10986{
10987 int regname;
10988 char_u *strregname;
10989 char_u *stropt;
10990 char_u *strval;
10991 int append;
10992 char_u yank_type;
10993 long block_len;
10994
10995 block_len = -1;
10996 yank_type = MAUTO;
10997 append = FALSE;
10998
10999 strregname = get_tv_string_chk(argvars);
11000 rettv->vval.v_number = 1; /* FAIL is default */
11001
11002 if (strregname == NULL)
11003 return; /* type error; errmsg already given */
11004 regname = *strregname;
11005 if (regname == 0 || regname == '@')
11006 regname = '"';
11007
11008 if (argvars[2].v_type != VAR_UNKNOWN)
11009 {
11010 stropt = get_tv_string_chk(&argvars[2]);
11011 if (stropt == NULL)
11012 return; /* type error */
11013 for (; *stropt != NUL; ++stropt)
11014 switch (*stropt)
11015 {
11016 case 'a': case 'A': /* append */
11017 append = TRUE;
11018 break;
11019 case 'v': case 'c': /* character-wise selection */
11020 yank_type = MCHAR;
11021 break;
11022 case 'V': case 'l': /* line-wise selection */
11023 yank_type = MLINE;
11024 break;
11025 case 'b': case Ctrl_V: /* block-wise selection */
11026 yank_type = MBLOCK;
11027 if (VIM_ISDIGIT(stropt[1]))
11028 {
11029 ++stropt;
11030 block_len = getdigits(&stropt) - 1;
11031 --stropt;
11032 }
11033 break;
11034 }
11035 }
11036
11037 if (argvars[1].v_type == VAR_LIST)
11038 {
11039 char_u **lstval;
11040 char_u **allocval;
11041 char_u buf[NUMBUFLEN];
11042 char_u **curval;
11043 char_u **curallocval;
11044 list_T *ll = argvars[1].vval.v_list;
11045 listitem_T *li;
11046 int len;
11047
11048 /* If the list is NULL handle like an empty list. */
11049 len = ll == NULL ? 0 : ll->lv_len;
11050
11051 /* First half: use for pointers to result lines; second half: use for
11052 * pointers to allocated copies. */
11053 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
11054 if (lstval == NULL)
11055 return;
11056 curval = lstval;
11057 allocval = lstval + len + 2;
11058 curallocval = allocval;
11059
11060 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
11061 li = li->li_next)
11062 {
11063 strval = get_tv_string_buf_chk(&li->li_tv, buf);
11064 if (strval == NULL)
11065 goto free_lstval;
11066 if (strval == buf)
11067 {
11068 /* Need to make a copy, next get_tv_string_buf_chk() will
11069 * overwrite the string. */
11070 strval = vim_strsave(buf);
11071 if (strval == NULL)
11072 goto free_lstval;
11073 *curallocval++ = strval;
11074 }
11075 *curval++ = strval;
11076 }
11077 *curval++ = NULL;
11078
11079 write_reg_contents_lst(regname, lstval, -1,
11080 append, yank_type, block_len);
11081free_lstval:
11082 while (curallocval > allocval)
11083 vim_free(*--curallocval);
11084 vim_free(lstval);
11085 }
11086 else
11087 {
11088 strval = get_tv_string_chk(&argvars[1]);
11089 if (strval == NULL)
11090 return;
11091 write_reg_contents_ex(regname, strval, -1,
11092 append, yank_type, block_len);
11093 }
11094 rettv->vval.v_number = 0;
11095}
11096
11097/*
11098 * "settabvar()" function
11099 */
11100 static void
11101f_settabvar(typval_T *argvars, typval_T *rettv)
11102{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011103 tabpage_T *save_curtab;
11104 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011105 char_u *varname, *tabvarname;
11106 typval_T *varp;
11107
11108 rettv->vval.v_number = 0;
11109
11110 if (check_restricted() || check_secure())
11111 return;
11112
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011113 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011114 varname = get_tv_string_chk(&argvars[1]);
11115 varp = &argvars[2];
11116
Bram Moolenaar4033c552017-09-16 20:54:51 +020011117 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011118 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011119 save_curtab = curtab;
11120 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011121
11122 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
11123 if (tabvarname != NULL)
11124 {
11125 STRCPY(tabvarname, "t:");
11126 STRCPY(tabvarname + 2, varname);
11127 set_var(tabvarname, varp, TRUE);
11128 vim_free(tabvarname);
11129 }
11130
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011131 /* Restore current tabpage */
11132 if (valid_tabpage(save_curtab))
11133 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011134 }
11135}
11136
11137/*
11138 * "settabwinvar()" function
11139 */
11140 static void
11141f_settabwinvar(typval_T *argvars, typval_T *rettv)
11142{
11143 setwinvar(argvars, rettv, 1);
11144}
11145
11146/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011147 * "settagstack()" function
11148 */
11149 static void
11150f_settagstack(typval_T *argvars, typval_T *rettv)
11151{
11152 static char *e_invact2 = N_("E962: Invalid action: '%s'");
11153 win_T *wp;
11154 dict_T *d;
11155 int action = 'r';
11156
11157 rettv->vval.v_number = -1;
11158
11159 // first argument: window number or id
11160 wp = find_win_by_nr_or_id(&argvars[0]);
11161 if (wp == NULL)
11162 return;
11163
11164 // second argument: dict with items to set in the tag stack
11165 if (argvars[1].v_type != VAR_DICT)
11166 {
11167 EMSG(_(e_dictreq));
11168 return;
11169 }
11170 d = argvars[1].vval.v_dict;
11171 if (d == NULL)
11172 return;
11173
11174 // third argument: action - 'a' for append and 'r' for replace.
11175 // default is to replace the stack.
11176 if (argvars[2].v_type == VAR_UNKNOWN)
11177 action = 'r';
11178 else if (argvars[2].v_type == VAR_STRING)
11179 {
11180 char_u *actstr;
11181 actstr = get_tv_string_chk(&argvars[2]);
11182 if (actstr == NULL)
11183 return;
11184 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
11185 action = *actstr;
11186 else
11187 {
11188 EMSG2(_(e_invact2), actstr);
11189 return;
11190 }
11191 }
11192 else
11193 {
11194 EMSG(_(e_stringreq));
11195 return;
11196 }
11197
11198 if (set_tagstack(wp, d, action) == OK)
11199 rettv->vval.v_number = 0;
11200}
11201
11202/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011203 * "setwinvar()" function
11204 */
11205 static void
11206f_setwinvar(typval_T *argvars, typval_T *rettv)
11207{
11208 setwinvar(argvars, rettv, 0);
11209}
11210
11211#ifdef FEAT_CRYPT
11212/*
11213 * "sha256({string})" function
11214 */
11215 static void
11216f_sha256(typval_T *argvars, typval_T *rettv)
11217{
11218 char_u *p;
11219
11220 p = get_tv_string(&argvars[0]);
11221 rettv->vval.v_string = vim_strsave(
11222 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
11223 rettv->v_type = VAR_STRING;
11224}
11225#endif /* FEAT_CRYPT */
11226
11227/*
11228 * "shellescape({string})" function
11229 */
11230 static void
11231f_shellescape(typval_T *argvars, typval_T *rettv)
11232{
Bram Moolenaar20615522017-06-05 18:46:26 +020011233 int do_special = non_zero_arg(&argvars[1]);
11234
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011235 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar20615522017-06-05 18:46:26 +020011236 get_tv_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011237 rettv->v_type = VAR_STRING;
11238}
11239
11240/*
11241 * shiftwidth() function
11242 */
11243 static void
11244f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
11245{
11246 rettv->vval.v_number = get_sw_value(curbuf);
11247}
11248
11249/*
11250 * "simplify()" function
11251 */
11252 static void
11253f_simplify(typval_T *argvars, typval_T *rettv)
11254{
11255 char_u *p;
11256
11257 p = get_tv_string(&argvars[0]);
11258 rettv->vval.v_string = vim_strsave(p);
11259 simplify_filename(rettv->vval.v_string); /* simplify in place */
11260 rettv->v_type = VAR_STRING;
11261}
11262
11263#ifdef FEAT_FLOAT
11264/*
11265 * "sin()" function
11266 */
11267 static void
11268f_sin(typval_T *argvars, typval_T *rettv)
11269{
11270 float_T f = 0.0;
11271
11272 rettv->v_type = VAR_FLOAT;
11273 if (get_float_arg(argvars, &f) == OK)
11274 rettv->vval.v_float = sin(f);
11275 else
11276 rettv->vval.v_float = 0.0;
11277}
11278
11279/*
11280 * "sinh()" function
11281 */
11282 static void
11283f_sinh(typval_T *argvars, typval_T *rettv)
11284{
11285 float_T f = 0.0;
11286
11287 rettv->v_type = VAR_FLOAT;
11288 if (get_float_arg(argvars, &f) == OK)
11289 rettv->vval.v_float = sinh(f);
11290 else
11291 rettv->vval.v_float = 0.0;
11292}
11293#endif
11294
11295static int
11296#ifdef __BORLANDC__
11297 _RTLENTRYF
11298#endif
11299 item_compare(const void *s1, const void *s2);
11300static int
11301#ifdef __BORLANDC__
11302 _RTLENTRYF
11303#endif
11304 item_compare2(const void *s1, const void *s2);
11305
11306/* struct used in the array that's given to qsort() */
11307typedef struct
11308{
11309 listitem_T *item;
11310 int idx;
11311} sortItem_T;
11312
11313/* struct storing information about current sort */
11314typedef struct
11315{
11316 int item_compare_ic;
11317 int item_compare_numeric;
11318 int item_compare_numbers;
11319#ifdef FEAT_FLOAT
11320 int item_compare_float;
11321#endif
11322 char_u *item_compare_func;
11323 partial_T *item_compare_partial;
11324 dict_T *item_compare_selfdict;
11325 int item_compare_func_err;
11326 int item_compare_keep_zero;
11327} sortinfo_T;
11328static sortinfo_T *sortinfo = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011329#define ITEM_COMPARE_FAIL 999
11330
11331/*
11332 * Compare functions for f_sort() and f_uniq() below.
11333 */
11334 static int
11335#ifdef __BORLANDC__
11336_RTLENTRYF
11337#endif
11338item_compare(const void *s1, const void *s2)
11339{
11340 sortItem_T *si1, *si2;
11341 typval_T *tv1, *tv2;
11342 char_u *p1, *p2;
11343 char_u *tofree1 = NULL, *tofree2 = NULL;
11344 int res;
11345 char_u numbuf1[NUMBUFLEN];
11346 char_u numbuf2[NUMBUFLEN];
11347
11348 si1 = (sortItem_T *)s1;
11349 si2 = (sortItem_T *)s2;
11350 tv1 = &si1->item->li_tv;
11351 tv2 = &si2->item->li_tv;
11352
11353 if (sortinfo->item_compare_numbers)
11354 {
11355 varnumber_T v1 = get_tv_number(tv1);
11356 varnumber_T v2 = get_tv_number(tv2);
11357
11358 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11359 }
11360
11361#ifdef FEAT_FLOAT
11362 if (sortinfo->item_compare_float)
11363 {
11364 float_T v1 = get_tv_float(tv1);
11365 float_T v2 = get_tv_float(tv2);
11366
11367 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11368 }
11369#endif
11370
11371 /* tv2string() puts quotes around a string and allocates memory. Don't do
11372 * that for string variables. Use a single quote when comparing with a
11373 * non-string to do what the docs promise. */
11374 if (tv1->v_type == VAR_STRING)
11375 {
11376 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11377 p1 = (char_u *)"'";
11378 else
11379 p1 = tv1->vval.v_string;
11380 }
11381 else
11382 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
11383 if (tv2->v_type == VAR_STRING)
11384 {
11385 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11386 p2 = (char_u *)"'";
11387 else
11388 p2 = tv2->vval.v_string;
11389 }
11390 else
11391 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
11392 if (p1 == NULL)
11393 p1 = (char_u *)"";
11394 if (p2 == NULL)
11395 p2 = (char_u *)"";
11396 if (!sortinfo->item_compare_numeric)
11397 {
11398 if (sortinfo->item_compare_ic)
11399 res = STRICMP(p1, p2);
11400 else
11401 res = STRCMP(p1, p2);
11402 }
11403 else
11404 {
11405 double n1, n2;
11406 n1 = strtod((char *)p1, (char **)&p1);
11407 n2 = strtod((char *)p2, (char **)&p2);
11408 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
11409 }
11410
11411 /* When the result would be zero, compare the item indexes. Makes the
11412 * sort stable. */
11413 if (res == 0 && !sortinfo->item_compare_keep_zero)
11414 res = si1->idx > si2->idx ? 1 : -1;
11415
11416 vim_free(tofree1);
11417 vim_free(tofree2);
11418 return res;
11419}
11420
11421 static int
11422#ifdef __BORLANDC__
11423_RTLENTRYF
11424#endif
11425item_compare2(const void *s1, const void *s2)
11426{
11427 sortItem_T *si1, *si2;
11428 int res;
11429 typval_T rettv;
11430 typval_T argv[3];
11431 int dummy;
11432 char_u *func_name;
11433 partial_T *partial = sortinfo->item_compare_partial;
11434
11435 /* shortcut after failure in previous call; compare all items equal */
11436 if (sortinfo->item_compare_func_err)
11437 return 0;
11438
11439 si1 = (sortItem_T *)s1;
11440 si2 = (sortItem_T *)s2;
11441
11442 if (partial == NULL)
11443 func_name = sortinfo->item_compare_func;
11444 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020011445 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011446
11447 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
11448 * in the copy without changing the original list items. */
11449 copy_tv(&si1->item->li_tv, &argv[0]);
11450 copy_tv(&si2->item->li_tv, &argv[1]);
11451
11452 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
11453 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020011454 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011455 partial, sortinfo->item_compare_selfdict);
11456 clear_tv(&argv[0]);
11457 clear_tv(&argv[1]);
11458
11459 if (res == FAIL)
11460 res = ITEM_COMPARE_FAIL;
11461 else
11462 res = (int)get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
11463 if (sortinfo->item_compare_func_err)
11464 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
11465 clear_tv(&rettv);
11466
11467 /* When the result would be zero, compare the pointers themselves. Makes
11468 * the sort stable. */
11469 if (res == 0 && !sortinfo->item_compare_keep_zero)
11470 res = si1->idx > si2->idx ? 1 : -1;
11471
11472 return res;
11473}
11474
11475/*
11476 * "sort({list})" function
11477 */
11478 static void
11479do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
11480{
11481 list_T *l;
11482 listitem_T *li;
11483 sortItem_T *ptrs;
11484 sortinfo_T *old_sortinfo;
11485 sortinfo_T info;
11486 long len;
11487 long i;
11488
11489 /* Pointer to current info struct used in compare function. Save and
11490 * restore the current one for nested calls. */
11491 old_sortinfo = sortinfo;
11492 sortinfo = &info;
11493
11494 if (argvars[0].v_type != VAR_LIST)
11495 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
11496 else
11497 {
11498 l = argvars[0].vval.v_list;
11499 if (l == NULL || tv_check_lock(l->lv_lock,
11500 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
11501 TRUE))
11502 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011503 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011504
11505 len = list_len(l);
11506 if (len <= 1)
11507 goto theend; /* short list sorts pretty quickly */
11508
11509 info.item_compare_ic = FALSE;
11510 info.item_compare_numeric = FALSE;
11511 info.item_compare_numbers = FALSE;
11512#ifdef FEAT_FLOAT
11513 info.item_compare_float = FALSE;
11514#endif
11515 info.item_compare_func = NULL;
11516 info.item_compare_partial = NULL;
11517 info.item_compare_selfdict = NULL;
11518 if (argvars[1].v_type != VAR_UNKNOWN)
11519 {
11520 /* optional second argument: {func} */
11521 if (argvars[1].v_type == VAR_FUNC)
11522 info.item_compare_func = argvars[1].vval.v_string;
11523 else if (argvars[1].v_type == VAR_PARTIAL)
11524 info.item_compare_partial = argvars[1].vval.v_partial;
11525 else
11526 {
11527 int error = FALSE;
11528
11529 i = (long)get_tv_number_chk(&argvars[1], &error);
11530 if (error)
11531 goto theend; /* type error; errmsg already given */
11532 if (i == 1)
11533 info.item_compare_ic = TRUE;
11534 else if (argvars[1].v_type != VAR_NUMBER)
11535 info.item_compare_func = get_tv_string(&argvars[1]);
11536 else if (i != 0)
11537 {
11538 EMSG(_(e_invarg));
11539 goto theend;
11540 }
11541 if (info.item_compare_func != NULL)
11542 {
11543 if (*info.item_compare_func == NUL)
11544 {
11545 /* empty string means default sort */
11546 info.item_compare_func = NULL;
11547 }
11548 else if (STRCMP(info.item_compare_func, "n") == 0)
11549 {
11550 info.item_compare_func = NULL;
11551 info.item_compare_numeric = TRUE;
11552 }
11553 else if (STRCMP(info.item_compare_func, "N") == 0)
11554 {
11555 info.item_compare_func = NULL;
11556 info.item_compare_numbers = TRUE;
11557 }
11558#ifdef FEAT_FLOAT
11559 else if (STRCMP(info.item_compare_func, "f") == 0)
11560 {
11561 info.item_compare_func = NULL;
11562 info.item_compare_float = TRUE;
11563 }
11564#endif
11565 else if (STRCMP(info.item_compare_func, "i") == 0)
11566 {
11567 info.item_compare_func = NULL;
11568 info.item_compare_ic = TRUE;
11569 }
11570 }
11571 }
11572
11573 if (argvars[2].v_type != VAR_UNKNOWN)
11574 {
11575 /* optional third argument: {dict} */
11576 if (argvars[2].v_type != VAR_DICT)
11577 {
11578 EMSG(_(e_dictreq));
11579 goto theend;
11580 }
11581 info.item_compare_selfdict = argvars[2].vval.v_dict;
11582 }
11583 }
11584
11585 /* Make an array with each entry pointing to an item in the List. */
11586 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
11587 if (ptrs == NULL)
11588 goto theend;
11589
11590 i = 0;
11591 if (sort)
11592 {
11593 /* sort(): ptrs will be the list to sort */
11594 for (li = l->lv_first; li != NULL; li = li->li_next)
11595 {
11596 ptrs[i].item = li;
11597 ptrs[i].idx = i;
11598 ++i;
11599 }
11600
11601 info.item_compare_func_err = FALSE;
11602 info.item_compare_keep_zero = FALSE;
11603 /* test the compare function */
11604 if ((info.item_compare_func != NULL
11605 || info.item_compare_partial != NULL)
11606 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
11607 == ITEM_COMPARE_FAIL)
11608 EMSG(_("E702: Sort compare function failed"));
11609 else
11610 {
11611 /* Sort the array with item pointers. */
11612 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
11613 info.item_compare_func == NULL
11614 && info.item_compare_partial == NULL
11615 ? item_compare : item_compare2);
11616
11617 if (!info.item_compare_func_err)
11618 {
11619 /* Clear the List and append the items in sorted order. */
11620 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
11621 l->lv_len = 0;
11622 for (i = 0; i < len; ++i)
11623 list_append(l, ptrs[i].item);
11624 }
11625 }
11626 }
11627 else
11628 {
11629 int (*item_compare_func_ptr)(const void *, const void *);
11630
11631 /* f_uniq(): ptrs will be a stack of items to remove */
11632 info.item_compare_func_err = FALSE;
11633 info.item_compare_keep_zero = TRUE;
11634 item_compare_func_ptr = info.item_compare_func != NULL
11635 || info.item_compare_partial != NULL
11636 ? item_compare2 : item_compare;
11637
11638 for (li = l->lv_first; li != NULL && li->li_next != NULL;
11639 li = li->li_next)
11640 {
11641 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
11642 == 0)
11643 ptrs[i++].item = li;
11644 if (info.item_compare_func_err)
11645 {
11646 EMSG(_("E882: Uniq compare function failed"));
11647 break;
11648 }
11649 }
11650
11651 if (!info.item_compare_func_err)
11652 {
11653 while (--i >= 0)
11654 {
11655 li = ptrs[i].item->li_next;
11656 ptrs[i].item->li_next = li->li_next;
11657 if (li->li_next != NULL)
11658 li->li_next->li_prev = ptrs[i].item;
11659 else
11660 l->lv_last = ptrs[i].item;
11661 list_fix_watch(l, li);
11662 listitem_free(li);
11663 l->lv_len--;
11664 }
11665 }
11666 }
11667
11668 vim_free(ptrs);
11669 }
11670theend:
11671 sortinfo = old_sortinfo;
11672}
11673
11674/*
11675 * "sort({list})" function
11676 */
11677 static void
11678f_sort(typval_T *argvars, typval_T *rettv)
11679{
11680 do_sort_uniq(argvars, rettv, TRUE);
11681}
11682
11683/*
11684 * "uniq({list})" function
11685 */
11686 static void
11687f_uniq(typval_T *argvars, typval_T *rettv)
11688{
11689 do_sort_uniq(argvars, rettv, FALSE);
11690}
11691
11692/*
11693 * "soundfold({word})" function
11694 */
11695 static void
11696f_soundfold(typval_T *argvars, typval_T *rettv)
11697{
11698 char_u *s;
11699
11700 rettv->v_type = VAR_STRING;
11701 s = get_tv_string(&argvars[0]);
11702#ifdef FEAT_SPELL
11703 rettv->vval.v_string = eval_soundfold(s);
11704#else
11705 rettv->vval.v_string = vim_strsave(s);
11706#endif
11707}
11708
11709/*
11710 * "spellbadword()" function
11711 */
11712 static void
11713f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
11714{
11715 char_u *word = (char_u *)"";
11716 hlf_T attr = HLF_COUNT;
11717 int len = 0;
11718
11719 if (rettv_list_alloc(rettv) == FAIL)
11720 return;
11721
11722#ifdef FEAT_SPELL
11723 if (argvars[0].v_type == VAR_UNKNOWN)
11724 {
11725 /* Find the start and length of the badly spelled word. */
11726 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
11727 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011728 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011729 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011730 curwin->w_set_curswant = TRUE;
11731 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011732 }
11733 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
11734 {
11735 char_u *str = get_tv_string_chk(&argvars[0]);
11736 int capcol = -1;
11737
11738 if (str != NULL)
11739 {
11740 /* Check the argument for spelling. */
11741 while (*str != NUL)
11742 {
11743 len = spell_check(curwin, str, &attr, &capcol, FALSE);
11744 if (attr != HLF_COUNT)
11745 {
11746 word = str;
11747 break;
11748 }
11749 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020011750 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011751 }
11752 }
11753 }
11754#endif
11755
11756 list_append_string(rettv->vval.v_list, word, len);
11757 list_append_string(rettv->vval.v_list, (char_u *)(
11758 attr == HLF_SPB ? "bad" :
11759 attr == HLF_SPR ? "rare" :
11760 attr == HLF_SPL ? "local" :
11761 attr == HLF_SPC ? "caps" :
11762 ""), -1);
11763}
11764
11765/*
11766 * "spellsuggest()" function
11767 */
11768 static void
11769f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
11770{
11771#ifdef FEAT_SPELL
11772 char_u *str;
11773 int typeerr = FALSE;
11774 int maxcount;
11775 garray_T ga;
11776 int i;
11777 listitem_T *li;
11778 int need_capital = FALSE;
11779#endif
11780
11781 if (rettv_list_alloc(rettv) == FAIL)
11782 return;
11783
11784#ifdef FEAT_SPELL
11785 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
11786 {
11787 str = get_tv_string(&argvars[0]);
11788 if (argvars[1].v_type != VAR_UNKNOWN)
11789 {
11790 maxcount = (int)get_tv_number_chk(&argvars[1], &typeerr);
11791 if (maxcount <= 0)
11792 return;
11793 if (argvars[2].v_type != VAR_UNKNOWN)
11794 {
11795 need_capital = (int)get_tv_number_chk(&argvars[2], &typeerr);
11796 if (typeerr)
11797 return;
11798 }
11799 }
11800 else
11801 maxcount = 25;
11802
11803 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
11804
11805 for (i = 0; i < ga.ga_len; ++i)
11806 {
11807 str = ((char_u **)ga.ga_data)[i];
11808
11809 li = listitem_alloc();
11810 if (li == NULL)
11811 vim_free(str);
11812 else
11813 {
11814 li->li_tv.v_type = VAR_STRING;
11815 li->li_tv.v_lock = 0;
11816 li->li_tv.vval.v_string = str;
11817 list_append(rettv->vval.v_list, li);
11818 }
11819 }
11820 ga_clear(&ga);
11821 }
11822#endif
11823}
11824
11825 static void
11826f_split(typval_T *argvars, typval_T *rettv)
11827{
11828 char_u *str;
11829 char_u *end;
11830 char_u *pat = NULL;
11831 regmatch_T regmatch;
11832 char_u patbuf[NUMBUFLEN];
11833 char_u *save_cpo;
11834 int match;
11835 colnr_T col = 0;
11836 int keepempty = FALSE;
11837 int typeerr = FALSE;
11838
11839 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11840 save_cpo = p_cpo;
11841 p_cpo = (char_u *)"";
11842
11843 str = get_tv_string(&argvars[0]);
11844 if (argvars[1].v_type != VAR_UNKNOWN)
11845 {
11846 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11847 if (pat == NULL)
11848 typeerr = TRUE;
11849 if (argvars[2].v_type != VAR_UNKNOWN)
11850 keepempty = (int)get_tv_number_chk(&argvars[2], &typeerr);
11851 }
11852 if (pat == NULL || *pat == NUL)
11853 pat = (char_u *)"[\\x01- ]\\+";
11854
11855 if (rettv_list_alloc(rettv) == FAIL)
11856 return;
11857 if (typeerr)
11858 return;
11859
11860 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11861 if (regmatch.regprog != NULL)
11862 {
11863 regmatch.rm_ic = FALSE;
11864 while (*str != NUL || keepempty)
11865 {
11866 if (*str == NUL)
11867 match = FALSE; /* empty item at the end */
11868 else
11869 match = vim_regexec_nl(&regmatch, str, col);
11870 if (match)
11871 end = regmatch.startp[0];
11872 else
11873 end = str + STRLEN(str);
11874 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
11875 && *str != NUL && match && end < regmatch.endp[0]))
11876 {
11877 if (list_append_string(rettv->vval.v_list, str,
11878 (int)(end - str)) == FAIL)
11879 break;
11880 }
11881 if (!match)
11882 break;
11883 /* Advance to just after the match. */
11884 if (regmatch.endp[0] > str)
11885 col = 0;
11886 else
11887 {
11888 /* Don't get stuck at the same match. */
11889#ifdef FEAT_MBYTE
11890 col = (*mb_ptr2len)(regmatch.endp[0]);
11891#else
11892 col = 1;
11893#endif
11894 }
11895 str = regmatch.endp[0];
11896 }
11897
11898 vim_regfree(regmatch.regprog);
11899 }
11900
11901 p_cpo = save_cpo;
11902}
11903
11904#ifdef FEAT_FLOAT
11905/*
11906 * "sqrt()" function
11907 */
11908 static void
11909f_sqrt(typval_T *argvars, typval_T *rettv)
11910{
11911 float_T f = 0.0;
11912
11913 rettv->v_type = VAR_FLOAT;
11914 if (get_float_arg(argvars, &f) == OK)
11915 rettv->vval.v_float = sqrt(f);
11916 else
11917 rettv->vval.v_float = 0.0;
11918}
11919
11920/*
11921 * "str2float()" function
11922 */
11923 static void
11924f_str2float(typval_T *argvars, typval_T *rettv)
11925{
11926 char_u *p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011927 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011928
Bram Moolenaar08243d22017-01-10 16:12:29 +010011929 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011930 p = skipwhite(p + 1);
11931 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011932 if (isneg)
11933 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011934 rettv->v_type = VAR_FLOAT;
11935}
11936#endif
11937
11938/*
11939 * "str2nr()" function
11940 */
11941 static void
11942f_str2nr(typval_T *argvars, typval_T *rettv)
11943{
11944 int base = 10;
11945 char_u *p;
11946 varnumber_T n;
11947 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010011948 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011949
11950 if (argvars[1].v_type != VAR_UNKNOWN)
11951 {
11952 base = (int)get_tv_number(&argvars[1]);
11953 if (base != 2 && base != 8 && base != 10 && base != 16)
11954 {
11955 EMSG(_(e_invarg));
11956 return;
11957 }
11958 }
11959
11960 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011961 isneg = (*p == '-');
11962 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011963 p = skipwhite(p + 1);
11964 switch (base)
11965 {
11966 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
11967 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
11968 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
11969 default: what = 0;
11970 }
11971 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011972 if (isneg)
11973 rettv->vval.v_number = -n;
11974 else
11975 rettv->vval.v_number = n;
11976
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011977}
11978
11979#ifdef HAVE_STRFTIME
11980/*
11981 * "strftime({format}[, {time}])" function
11982 */
11983 static void
11984f_strftime(typval_T *argvars, typval_T *rettv)
11985{
11986 char_u result_buf[256];
11987 struct tm *curtime;
11988 time_t seconds;
11989 char_u *p;
11990
11991 rettv->v_type = VAR_STRING;
11992
11993 p = get_tv_string(&argvars[0]);
11994 if (argvars[1].v_type == VAR_UNKNOWN)
11995 seconds = time(NULL);
11996 else
11997 seconds = (time_t)get_tv_number(&argvars[1]);
11998 curtime = localtime(&seconds);
11999 /* MSVC returns NULL for an invalid value of seconds. */
12000 if (curtime == NULL)
12001 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
12002 else
12003 {
12004# ifdef FEAT_MBYTE
12005 vimconv_T conv;
12006 char_u *enc;
12007
12008 conv.vc_type = CONV_NONE;
12009 enc = enc_locale();
12010 convert_setup(&conv, p_enc, enc);
12011 if (conv.vc_type != CONV_NONE)
12012 p = string_convert(&conv, p, NULL);
12013# endif
12014 if (p != NULL)
12015 (void)strftime((char *)result_buf, sizeof(result_buf),
12016 (char *)p, curtime);
12017 else
12018 result_buf[0] = NUL;
12019
12020# ifdef FEAT_MBYTE
12021 if (conv.vc_type != CONV_NONE)
12022 vim_free(p);
12023 convert_setup(&conv, enc, p_enc);
12024 if (conv.vc_type != CONV_NONE)
12025 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
12026 else
12027# endif
12028 rettv->vval.v_string = vim_strsave(result_buf);
12029
12030# ifdef FEAT_MBYTE
12031 /* Release conversion descriptors */
12032 convert_setup(&conv, NULL, NULL);
12033 vim_free(enc);
12034# endif
12035 }
12036}
12037#endif
12038
12039/*
12040 * "strgetchar()" function
12041 */
12042 static void
12043f_strgetchar(typval_T *argvars, typval_T *rettv)
12044{
12045 char_u *str;
12046 int len;
12047 int error = FALSE;
12048 int charidx;
12049
12050 rettv->vval.v_number = -1;
12051 str = get_tv_string_chk(&argvars[0]);
12052 if (str == NULL)
12053 return;
12054 len = (int)STRLEN(str);
12055 charidx = (int)get_tv_number_chk(&argvars[1], &error);
12056 if (error)
12057 return;
12058#ifdef FEAT_MBYTE
12059 {
12060 int byteidx = 0;
12061
12062 while (charidx >= 0 && byteidx < len)
12063 {
12064 if (charidx == 0)
12065 {
12066 rettv->vval.v_number = mb_ptr2char(str + byteidx);
12067 break;
12068 }
12069 --charidx;
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012070 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012071 }
12072 }
12073#else
12074 if (charidx < len)
12075 rettv->vval.v_number = str[charidx];
12076#endif
12077}
12078
12079/*
12080 * "stridx()" function
12081 */
12082 static void
12083f_stridx(typval_T *argvars, typval_T *rettv)
12084{
12085 char_u buf[NUMBUFLEN];
12086 char_u *needle;
12087 char_u *haystack;
12088 char_u *save_haystack;
12089 char_u *pos;
12090 int start_idx;
12091
12092 needle = get_tv_string_chk(&argvars[1]);
12093 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
12094 rettv->vval.v_number = -1;
12095 if (needle == NULL || haystack == NULL)
12096 return; /* type error; errmsg already given */
12097
12098 if (argvars[2].v_type != VAR_UNKNOWN)
12099 {
12100 int error = FALSE;
12101
12102 start_idx = (int)get_tv_number_chk(&argvars[2], &error);
12103 if (error || start_idx >= (int)STRLEN(haystack))
12104 return;
12105 if (start_idx >= 0)
12106 haystack += start_idx;
12107 }
12108
12109 pos = (char_u *)strstr((char *)haystack, (char *)needle);
12110 if (pos != NULL)
12111 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
12112}
12113
12114/*
12115 * "string()" function
12116 */
12117 static void
12118f_string(typval_T *argvars, typval_T *rettv)
12119{
12120 char_u *tofree;
12121 char_u numbuf[NUMBUFLEN];
12122
12123 rettv->v_type = VAR_STRING;
12124 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
12125 get_copyID());
12126 /* Make a copy if we have a value but it's not in allocated memory. */
12127 if (rettv->vval.v_string != NULL && tofree == NULL)
12128 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
12129}
12130
12131/*
12132 * "strlen()" function
12133 */
12134 static void
12135f_strlen(typval_T *argvars, typval_T *rettv)
12136{
12137 rettv->vval.v_number = (varnumber_T)(STRLEN(
12138 get_tv_string(&argvars[0])));
12139}
12140
12141/*
12142 * "strchars()" function
12143 */
12144 static void
12145f_strchars(typval_T *argvars, typval_T *rettv)
12146{
12147 char_u *s = get_tv_string(&argvars[0]);
12148 int skipcc = 0;
12149#ifdef FEAT_MBYTE
12150 varnumber_T len = 0;
12151 int (*func_mb_ptr2char_adv)(char_u **pp);
12152#endif
12153
12154 if (argvars[1].v_type != VAR_UNKNOWN)
12155 skipcc = (int)get_tv_number_chk(&argvars[1], NULL);
12156 if (skipcc < 0 || skipcc > 1)
12157 EMSG(_(e_invarg));
12158 else
12159 {
12160#ifdef FEAT_MBYTE
12161 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
12162 while (*s != NUL)
12163 {
12164 func_mb_ptr2char_adv(&s);
12165 ++len;
12166 }
12167 rettv->vval.v_number = len;
12168#else
12169 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
12170#endif
12171 }
12172}
12173
12174/*
12175 * "strdisplaywidth()" function
12176 */
12177 static void
12178f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
12179{
12180 char_u *s = get_tv_string(&argvars[0]);
12181 int col = 0;
12182
12183 if (argvars[1].v_type != VAR_UNKNOWN)
12184 col = (int)get_tv_number(&argvars[1]);
12185
12186 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
12187}
12188
12189/*
12190 * "strwidth()" function
12191 */
12192 static void
12193f_strwidth(typval_T *argvars, typval_T *rettv)
12194{
12195 char_u *s = get_tv_string(&argvars[0]);
12196
12197 rettv->vval.v_number = (varnumber_T)(
12198#ifdef FEAT_MBYTE
12199 mb_string2cells(s, -1)
12200#else
12201 STRLEN(s)
12202#endif
12203 );
12204}
12205
12206/*
12207 * "strcharpart()" function
12208 */
12209 static void
12210f_strcharpart(typval_T *argvars, typval_T *rettv)
12211{
12212#ifdef FEAT_MBYTE
12213 char_u *p;
12214 int nchar;
12215 int nbyte = 0;
12216 int charlen;
12217 int len = 0;
12218 int slen;
12219 int error = FALSE;
12220
12221 p = get_tv_string(&argvars[0]);
12222 slen = (int)STRLEN(p);
12223
12224 nchar = (int)get_tv_number_chk(&argvars[1], &error);
12225 if (!error)
12226 {
12227 if (nchar > 0)
12228 while (nchar > 0 && nbyte < slen)
12229 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012230 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012231 --nchar;
12232 }
12233 else
12234 nbyte = nchar;
12235 if (argvars[2].v_type != VAR_UNKNOWN)
12236 {
12237 charlen = (int)get_tv_number(&argvars[2]);
12238 while (charlen > 0 && nbyte + len < slen)
12239 {
12240 int off = nbyte + len;
12241
12242 if (off < 0)
12243 len += 1;
12244 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012245 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012246 --charlen;
12247 }
12248 }
12249 else
12250 len = slen - nbyte; /* default: all bytes that are available. */
12251 }
12252
12253 /*
12254 * Only return the overlap between the specified part and the actual
12255 * string.
12256 */
12257 if (nbyte < 0)
12258 {
12259 len += nbyte;
12260 nbyte = 0;
12261 }
12262 else if (nbyte > slen)
12263 nbyte = slen;
12264 if (len < 0)
12265 len = 0;
12266 else if (nbyte + len > slen)
12267 len = slen - nbyte;
12268
12269 rettv->v_type = VAR_STRING;
12270 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
12271#else
12272 f_strpart(argvars, rettv);
12273#endif
12274}
12275
12276/*
12277 * "strpart()" function
12278 */
12279 static void
12280f_strpart(typval_T *argvars, typval_T *rettv)
12281{
12282 char_u *p;
12283 int n;
12284 int len;
12285 int slen;
12286 int error = FALSE;
12287
12288 p = get_tv_string(&argvars[0]);
12289 slen = (int)STRLEN(p);
12290
12291 n = (int)get_tv_number_chk(&argvars[1], &error);
12292 if (error)
12293 len = 0;
12294 else if (argvars[2].v_type != VAR_UNKNOWN)
12295 len = (int)get_tv_number(&argvars[2]);
12296 else
12297 len = slen - n; /* default len: all bytes that are available. */
12298
12299 /*
12300 * Only return the overlap between the specified part and the actual
12301 * string.
12302 */
12303 if (n < 0)
12304 {
12305 len += n;
12306 n = 0;
12307 }
12308 else if (n > slen)
12309 n = slen;
12310 if (len < 0)
12311 len = 0;
12312 else if (n + len > slen)
12313 len = slen - n;
12314
12315 rettv->v_type = VAR_STRING;
12316 rettv->vval.v_string = vim_strnsave(p + n, len);
12317}
12318
12319/*
12320 * "strridx()" function
12321 */
12322 static void
12323f_strridx(typval_T *argvars, typval_T *rettv)
12324{
12325 char_u buf[NUMBUFLEN];
12326 char_u *needle;
12327 char_u *haystack;
12328 char_u *rest;
12329 char_u *lastmatch = NULL;
12330 int haystack_len, end_idx;
12331
12332 needle = get_tv_string_chk(&argvars[1]);
12333 haystack = get_tv_string_buf_chk(&argvars[0], buf);
12334
12335 rettv->vval.v_number = -1;
12336 if (needle == NULL || haystack == NULL)
12337 return; /* type error; errmsg already given */
12338
12339 haystack_len = (int)STRLEN(haystack);
12340 if (argvars[2].v_type != VAR_UNKNOWN)
12341 {
12342 /* Third argument: upper limit for index */
12343 end_idx = (int)get_tv_number_chk(&argvars[2], NULL);
12344 if (end_idx < 0)
12345 return; /* can never find a match */
12346 }
12347 else
12348 end_idx = haystack_len;
12349
12350 if (*needle == NUL)
12351 {
12352 /* Empty string matches past the end. */
12353 lastmatch = haystack + end_idx;
12354 }
12355 else
12356 {
12357 for (rest = haystack; *rest != '\0'; ++rest)
12358 {
12359 rest = (char_u *)strstr((char *)rest, (char *)needle);
12360 if (rest == NULL || rest > haystack + end_idx)
12361 break;
12362 lastmatch = rest;
12363 }
12364 }
12365
12366 if (lastmatch == NULL)
12367 rettv->vval.v_number = -1;
12368 else
12369 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
12370}
12371
12372/*
12373 * "strtrans()" function
12374 */
12375 static void
12376f_strtrans(typval_T *argvars, typval_T *rettv)
12377{
12378 rettv->v_type = VAR_STRING;
12379 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
12380}
12381
12382/*
12383 * "submatch()" function
12384 */
12385 static void
12386f_submatch(typval_T *argvars, typval_T *rettv)
12387{
12388 int error = FALSE;
12389 int no;
12390 int retList = 0;
12391
12392 no = (int)get_tv_number_chk(&argvars[0], &error);
12393 if (error)
12394 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012395 if (no < 0 || no >= NSUBEXP)
12396 {
Bram Moolenaar79518e22017-02-17 16:31:35 +010012397 EMSGN(_("E935: invalid submatch number: %d"), no);
12398 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012399 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012400 if (argvars[1].v_type != VAR_UNKNOWN)
12401 retList = (int)get_tv_number_chk(&argvars[1], &error);
12402 if (error)
12403 return;
12404
12405 if (retList == 0)
12406 {
12407 rettv->v_type = VAR_STRING;
12408 rettv->vval.v_string = reg_submatch(no);
12409 }
12410 else
12411 {
12412 rettv->v_type = VAR_LIST;
12413 rettv->vval.v_list = reg_submatch_list(no);
12414 }
12415}
12416
12417/*
12418 * "substitute()" function
12419 */
12420 static void
12421f_substitute(typval_T *argvars, typval_T *rettv)
12422{
12423 char_u patbuf[NUMBUFLEN];
12424 char_u subbuf[NUMBUFLEN];
12425 char_u flagsbuf[NUMBUFLEN];
12426
12427 char_u *str = get_tv_string_chk(&argvars[0]);
12428 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012429 char_u *sub = NULL;
12430 typval_T *expr = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012431 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
12432
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012433 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
12434 expr = &argvars[2];
12435 else
12436 sub = get_tv_string_buf_chk(&argvars[2], subbuf);
12437
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012438 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012439 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
12440 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012441 rettv->vval.v_string = NULL;
12442 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012443 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012444}
12445
12446/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020012447 * "swapinfo(swap_filename)" function
12448 */
12449 static void
12450f_swapinfo(typval_T *argvars, typval_T *rettv)
12451{
12452 if (rettv_dict_alloc(rettv) == OK)
12453 get_b0_dict(get_tv_string(argvars), rettv->vval.v_dict);
12454}
12455
12456/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020012457 * "swapname(expr)" function
12458 */
12459 static void
12460f_swapname(typval_T *argvars, typval_T *rettv)
12461{
12462 buf_T *buf;
12463
12464 rettv->v_type = VAR_STRING;
12465 buf = get_buf_tv(&argvars[0], FALSE);
12466 if (buf == NULL || buf->b_ml.ml_mfp == NULL
12467 || buf->b_ml.ml_mfp->mf_fname == NULL)
12468 rettv->vval.v_string = NULL;
12469 else
12470 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
12471}
12472
12473/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012474 * "synID(lnum, col, trans)" function
12475 */
12476 static void
12477f_synID(typval_T *argvars UNUSED, typval_T *rettv)
12478{
12479 int id = 0;
12480#ifdef FEAT_SYN_HL
12481 linenr_T lnum;
12482 colnr_T col;
12483 int trans;
12484 int transerr = FALSE;
12485
12486 lnum = get_tv_lnum(argvars); /* -1 on type error */
12487 col = (linenr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12488 trans = (int)get_tv_number_chk(&argvars[2], &transerr);
12489
12490 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12491 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
12492 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
12493#endif
12494
12495 rettv->vval.v_number = id;
12496}
12497
12498/*
12499 * "synIDattr(id, what [, mode])" function
12500 */
12501 static void
12502f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
12503{
12504 char_u *p = NULL;
12505#ifdef FEAT_SYN_HL
12506 int id;
12507 char_u *what;
12508 char_u *mode;
12509 char_u modebuf[NUMBUFLEN];
12510 int modec;
12511
12512 id = (int)get_tv_number(&argvars[0]);
12513 what = get_tv_string(&argvars[1]);
12514 if (argvars[2].v_type != VAR_UNKNOWN)
12515 {
12516 mode = get_tv_string_buf(&argvars[2], modebuf);
12517 modec = TOLOWER_ASC(mode[0]);
12518 if (modec != 't' && modec != 'c' && modec != 'g')
12519 modec = 0; /* replace invalid with current */
12520 }
12521 else
12522 {
12523#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
12524 if (USE_24BIT)
12525 modec = 'g';
12526 else
12527#endif
12528 if (t_colors > 1)
12529 modec = 'c';
12530 else
12531 modec = 't';
12532 }
12533
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012534 switch (TOLOWER_ASC(what[0]))
12535 {
12536 case 'b':
12537 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
12538 p = highlight_color(id, what, modec);
12539 else /* bold */
12540 p = highlight_has_attr(id, HL_BOLD, modec);
12541 break;
12542
12543 case 'f': /* fg[#] or font */
12544 p = highlight_color(id, what, modec);
12545 break;
12546
12547 case 'i':
12548 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
12549 p = highlight_has_attr(id, HL_INVERSE, modec);
12550 else /* italic */
12551 p = highlight_has_attr(id, HL_ITALIC, modec);
12552 break;
12553
12554 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020012555 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012556 break;
12557
12558 case 'r': /* reverse */
12559 p = highlight_has_attr(id, HL_INVERSE, modec);
12560 break;
12561
12562 case 's':
12563 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
12564 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020012565 /* strikeout */
12566 else if (TOLOWER_ASC(what[1]) == 't' &&
12567 TOLOWER_ASC(what[2]) == 'r')
12568 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012569 else /* standout */
12570 p = highlight_has_attr(id, HL_STANDOUT, modec);
12571 break;
12572
12573 case 'u':
12574 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
12575 /* underline */
12576 p = highlight_has_attr(id, HL_UNDERLINE, modec);
12577 else
12578 /* undercurl */
12579 p = highlight_has_attr(id, HL_UNDERCURL, modec);
12580 break;
12581 }
12582
12583 if (p != NULL)
12584 p = vim_strsave(p);
12585#endif
12586 rettv->v_type = VAR_STRING;
12587 rettv->vval.v_string = p;
12588}
12589
12590/*
12591 * "synIDtrans(id)" function
12592 */
12593 static void
12594f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
12595{
12596 int id;
12597
12598#ifdef FEAT_SYN_HL
12599 id = (int)get_tv_number(&argvars[0]);
12600
12601 if (id > 0)
12602 id = syn_get_final_id(id);
12603 else
12604#endif
12605 id = 0;
12606
12607 rettv->vval.v_number = id;
12608}
12609
12610/*
12611 * "synconcealed(lnum, col)" function
12612 */
12613 static void
12614f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
12615{
12616#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12617 linenr_T lnum;
12618 colnr_T col;
12619 int syntax_flags = 0;
12620 int cchar;
12621 int matchid = 0;
12622 char_u str[NUMBUFLEN];
12623#endif
12624
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012625 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012626
12627#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12628 lnum = get_tv_lnum(argvars); /* -1 on type error */
12629 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12630
12631 vim_memset(str, NUL, sizeof(str));
12632
12633 if (rettv_list_alloc(rettv) != FAIL)
12634 {
12635 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12636 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12637 && curwin->w_p_cole > 0)
12638 {
12639 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
12640 syntax_flags = get_syntax_info(&matchid);
12641
12642 /* get the conceal character */
12643 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
12644 {
12645 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020012646 if (cchar == NUL && curwin->w_p_cole == 1)
12647 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012648 if (cchar != NUL)
12649 {
12650# ifdef FEAT_MBYTE
12651 if (has_mbyte)
12652 (*mb_char2bytes)(cchar, str);
12653 else
12654# endif
12655 str[0] = cchar;
12656 }
12657 }
12658 }
12659
12660 list_append_number(rettv->vval.v_list,
12661 (syntax_flags & HL_CONCEAL) != 0);
12662 /* -1 to auto-determine strlen */
12663 list_append_string(rettv->vval.v_list, str, -1);
12664 list_append_number(rettv->vval.v_list, matchid);
12665 }
12666#endif
12667}
12668
12669/*
12670 * "synstack(lnum, col)" function
12671 */
12672 static void
12673f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
12674{
12675#ifdef FEAT_SYN_HL
12676 linenr_T lnum;
12677 colnr_T col;
12678 int i;
12679 int id;
12680#endif
12681
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012682 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012683
12684#ifdef FEAT_SYN_HL
12685 lnum = get_tv_lnum(argvars); /* -1 on type error */
12686 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12687
12688 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12689 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12690 && rettv_list_alloc(rettv) != FAIL)
12691 {
12692 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
12693 for (i = 0; ; ++i)
12694 {
12695 id = syn_get_stack_item(i);
12696 if (id < 0)
12697 break;
12698 if (list_append_number(rettv->vval.v_list, id) == FAIL)
12699 break;
12700 }
12701 }
12702#endif
12703}
12704
12705 static void
12706get_cmd_output_as_rettv(
12707 typval_T *argvars,
12708 typval_T *rettv,
12709 int retlist)
12710{
12711 char_u *res = NULL;
12712 char_u *p;
12713 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012714 int err = FALSE;
12715 FILE *fd;
12716 list_T *list = NULL;
12717 int flags = SHELL_SILENT;
12718
12719 rettv->v_type = VAR_STRING;
12720 rettv->vval.v_string = NULL;
12721 if (check_restricted() || check_secure())
12722 goto errret;
12723
12724 if (argvars[1].v_type != VAR_UNKNOWN)
12725 {
12726 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010012727 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012728 * command.
12729 */
12730 if ((infile = vim_tempname('i', TRUE)) == NULL)
12731 {
12732 EMSG(_(e_notmp));
12733 goto errret;
12734 }
12735
12736 fd = mch_fopen((char *)infile, WRITEBIN);
12737 if (fd == NULL)
12738 {
12739 EMSG2(_(e_notopen), infile);
12740 goto errret;
12741 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010012742 if (argvars[1].v_type == VAR_NUMBER)
12743 {
12744 linenr_T lnum;
12745 buf_T *buf;
12746
12747 buf = buflist_findnr(argvars[1].vval.v_number);
12748 if (buf == NULL)
12749 {
12750 EMSGN(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010012751 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010012752 goto errret;
12753 }
12754
12755 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
12756 {
12757 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
12758 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
12759 {
12760 err = TRUE;
12761 break;
12762 }
12763 if (putc(NL, fd) == EOF)
12764 {
12765 err = TRUE;
12766 break;
12767 }
12768 }
12769 }
12770 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012771 {
12772 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
12773 err = TRUE;
12774 }
12775 else
12776 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010012777 size_t len;
12778 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012779
12780 p = get_tv_string_buf_chk(&argvars[1], buf);
12781 if (p == NULL)
12782 {
12783 fclose(fd);
12784 goto errret; /* type error; errmsg already given */
12785 }
12786 len = STRLEN(p);
12787 if (len > 0 && fwrite(p, len, 1, fd) != 1)
12788 err = TRUE;
12789 }
12790 if (fclose(fd) != 0)
12791 err = TRUE;
12792 if (err)
12793 {
12794 EMSG(_("E677: Error writing temp file"));
12795 goto errret;
12796 }
12797 }
12798
12799 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
12800 * echoes typeahead, that messes up the display. */
12801 if (!msg_silent)
12802 flags += SHELL_COOKED;
12803
12804 if (retlist)
12805 {
12806 int len;
12807 listitem_T *li;
12808 char_u *s = NULL;
12809 char_u *start;
12810 char_u *end;
12811 int i;
12812
12813 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
12814 if (res == NULL)
12815 goto errret;
12816
12817 list = list_alloc();
12818 if (list == NULL)
12819 goto errret;
12820
12821 for (i = 0; i < len; ++i)
12822 {
12823 start = res + i;
12824 while (i < len && res[i] != NL)
12825 ++i;
12826 end = res + i;
12827
12828 s = alloc((unsigned)(end - start + 1));
12829 if (s == NULL)
12830 goto errret;
12831
12832 for (p = s; start < end; ++p, ++start)
12833 *p = *start == NUL ? NL : *start;
12834 *p = NUL;
12835
12836 li = listitem_alloc();
12837 if (li == NULL)
12838 {
12839 vim_free(s);
12840 goto errret;
12841 }
12842 li->li_tv.v_type = VAR_STRING;
12843 li->li_tv.v_lock = 0;
12844 li->li_tv.vval.v_string = s;
12845 list_append(list, li);
12846 }
12847
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012848 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012849 list = NULL;
12850 }
12851 else
12852 {
12853 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
12854#ifdef USE_CR
12855 /* translate <CR> into <NL> */
12856 if (res != NULL)
12857 {
12858 char_u *s;
12859
12860 for (s = res; *s; ++s)
12861 {
12862 if (*s == CAR)
12863 *s = NL;
12864 }
12865 }
12866#else
12867# ifdef USE_CRNL
12868 /* translate <CR><NL> into <NL> */
12869 if (res != NULL)
12870 {
12871 char_u *s, *d;
12872
12873 d = res;
12874 for (s = res; *s; ++s)
12875 {
12876 if (s[0] == CAR && s[1] == NL)
12877 ++s;
12878 *d++ = *s;
12879 }
12880 *d = NUL;
12881 }
12882# endif
12883#endif
12884 rettv->vval.v_string = res;
12885 res = NULL;
12886 }
12887
12888errret:
12889 if (infile != NULL)
12890 {
12891 mch_remove(infile);
12892 vim_free(infile);
12893 }
12894 if (res != NULL)
12895 vim_free(res);
12896 if (list != NULL)
12897 list_free(list);
12898}
12899
12900/*
12901 * "system()" function
12902 */
12903 static void
12904f_system(typval_T *argvars, typval_T *rettv)
12905{
12906 get_cmd_output_as_rettv(argvars, rettv, FALSE);
12907}
12908
12909/*
12910 * "systemlist()" function
12911 */
12912 static void
12913f_systemlist(typval_T *argvars, typval_T *rettv)
12914{
12915 get_cmd_output_as_rettv(argvars, rettv, TRUE);
12916}
12917
12918/*
12919 * "tabpagebuflist()" function
12920 */
12921 static void
12922f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12923{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012924 tabpage_T *tp;
12925 win_T *wp = NULL;
12926
12927 if (argvars[0].v_type == VAR_UNKNOWN)
12928 wp = firstwin;
12929 else
12930 {
12931 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12932 if (tp != NULL)
12933 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12934 }
12935 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
12936 {
12937 for (; wp != NULL; wp = wp->w_next)
12938 if (list_append_number(rettv->vval.v_list,
12939 wp->w_buffer->b_fnum) == FAIL)
12940 break;
12941 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012942}
12943
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012944/*
12945 * "tabpagenr()" function
12946 */
12947 static void
12948f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
12949{
12950 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012951 char_u *arg;
12952
12953 if (argvars[0].v_type != VAR_UNKNOWN)
12954 {
12955 arg = get_tv_string_chk(&argvars[0]);
12956 nr = 0;
12957 if (arg != NULL)
12958 {
12959 if (STRCMP(arg, "$") == 0)
12960 nr = tabpage_index(NULL) - 1;
12961 else
12962 EMSG2(_(e_invexpr2), arg);
12963 }
12964 }
12965 else
12966 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012967 rettv->vval.v_number = nr;
12968}
12969
12970
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012971/*
12972 * Common code for tabpagewinnr() and winnr().
12973 */
12974 static int
12975get_winnr(tabpage_T *tp, typval_T *argvar)
12976{
12977 win_T *twin;
12978 int nr = 1;
12979 win_T *wp;
12980 char_u *arg;
12981
12982 twin = (tp == curtab) ? curwin : tp->tp_curwin;
12983 if (argvar->v_type != VAR_UNKNOWN)
12984 {
12985 arg = get_tv_string_chk(argvar);
12986 if (arg == NULL)
12987 nr = 0; /* type error; errmsg already given */
12988 else if (STRCMP(arg, "$") == 0)
12989 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
12990 else if (STRCMP(arg, "#") == 0)
12991 {
12992 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
12993 if (twin == NULL)
12994 nr = 0;
12995 }
12996 else
12997 {
12998 EMSG2(_(e_invexpr2), arg);
12999 nr = 0;
13000 }
13001 }
13002
13003 if (nr > 0)
13004 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
13005 wp != twin; wp = wp->w_next)
13006 {
13007 if (wp == NULL)
13008 {
13009 /* didn't find it in this tabpage */
13010 nr = 0;
13011 break;
13012 }
13013 ++nr;
13014 }
13015 return nr;
13016}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013017
13018/*
13019 * "tabpagewinnr()" function
13020 */
13021 static void
13022f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
13023{
13024 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013025 tabpage_T *tp;
13026
13027 tp = find_tabpage((int)get_tv_number(&argvars[0]));
13028 if (tp == NULL)
13029 nr = 0;
13030 else
13031 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013032 rettv->vval.v_number = nr;
13033}
13034
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013035/*
13036 * "tagfiles()" function
13037 */
13038 static void
13039f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
13040{
13041 char_u *fname;
13042 tagname_T tn;
13043 int first;
13044
13045 if (rettv_list_alloc(rettv) == FAIL)
13046 return;
13047 fname = alloc(MAXPATHL);
13048 if (fname == NULL)
13049 return;
13050
13051 for (first = TRUE; ; first = FALSE)
13052 if (get_tagfname(&tn, first, fname) == FAIL
13053 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
13054 break;
13055 tagname_free(&tn);
13056 vim_free(fname);
13057}
13058
13059/*
13060 * "taglist()" function
13061 */
13062 static void
13063f_taglist(typval_T *argvars, typval_T *rettv)
13064{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013065 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013066 char_u *tag_pattern;
13067
13068 tag_pattern = get_tv_string(&argvars[0]);
13069
13070 rettv->vval.v_number = FALSE;
13071 if (*tag_pattern == NUL)
13072 return;
13073
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013074 if (argvars[1].v_type != VAR_UNKNOWN)
13075 fname = get_tv_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013076 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013077 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013078}
13079
13080/*
13081 * "tempname()" function
13082 */
13083 static void
13084f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
13085{
13086 static int x = 'A';
13087
13088 rettv->v_type = VAR_STRING;
13089 rettv->vval.v_string = vim_tempname(x, FALSE);
13090
13091 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
13092 * names. Skip 'I' and 'O', they are used for shell redirection. */
13093 do
13094 {
13095 if (x == 'Z')
13096 x = '0';
13097 else if (x == '9')
13098 x = 'A';
13099 else
13100 {
13101#ifdef EBCDIC
13102 if (x == 'I')
13103 x = 'J';
13104 else if (x == 'R')
13105 x = 'S';
13106 else
13107#endif
13108 ++x;
13109 }
13110 } while (x == 'I' || x == 'O');
13111}
13112
13113#ifdef FEAT_FLOAT
13114/*
13115 * "tan()" function
13116 */
13117 static void
13118f_tan(typval_T *argvars, typval_T *rettv)
13119{
13120 float_T f = 0.0;
13121
13122 rettv->v_type = VAR_FLOAT;
13123 if (get_float_arg(argvars, &f) == OK)
13124 rettv->vval.v_float = tan(f);
13125 else
13126 rettv->vval.v_float = 0.0;
13127}
13128
13129/*
13130 * "tanh()" function
13131 */
13132 static void
13133f_tanh(typval_T *argvars, typval_T *rettv)
13134{
13135 float_T f = 0.0;
13136
13137 rettv->v_type = VAR_FLOAT;
13138 if (get_float_arg(argvars, &f) == OK)
13139 rettv->vval.v_float = tanh(f);
13140 else
13141 rettv->vval.v_float = 0.0;
13142}
13143#endif
13144
13145/*
13146 * "test_alloc_fail(id, countdown, repeat)" function
13147 */
13148 static void
13149f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
13150{
13151 if (argvars[0].v_type != VAR_NUMBER
13152 || argvars[0].vval.v_number <= 0
13153 || argvars[1].v_type != VAR_NUMBER
13154 || argvars[1].vval.v_number < 0
13155 || argvars[2].v_type != VAR_NUMBER)
13156 EMSG(_(e_invarg));
13157 else
13158 {
13159 alloc_fail_id = argvars[0].vval.v_number;
13160 if (alloc_fail_id >= aid_last)
13161 EMSG(_(e_invarg));
13162 alloc_fail_countdown = argvars[1].vval.v_number;
13163 alloc_fail_repeat = argvars[2].vval.v_number;
13164 did_outofmem_msg = FALSE;
13165 }
13166}
13167
13168/*
13169 * "test_autochdir()"
13170 */
13171 static void
13172f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13173{
13174#if defined(FEAT_AUTOCHDIR)
13175 test_autochdir = TRUE;
13176#endif
13177}
13178
13179/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020013180 * "test_feedinput()"
13181 */
13182 static void
13183f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
13184{
13185#ifdef USE_INPUT_BUF
13186 char_u *val = get_tv_string_chk(&argvars[0]);
13187
13188 if (val != NULL)
13189 {
13190 trash_input_buf();
13191 add_to_input_buf_csi(val, (int)STRLEN(val));
13192 }
13193#endif
13194}
13195
13196/*
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013197 * "test_option_not_set({name})" function
13198 */
13199 static void
13200f_test_option_not_set(typval_T *argvars, typval_T *rettv UNUSED)
13201{
13202 char_u *name = (char_u *)"";
13203
13204 if (argvars[0].v_type != VAR_STRING)
13205 EMSG(_(e_invarg));
13206 else
13207 {
Bram Moolenaarbf1c1b82018-09-13 21:30:05 +020013208 name = get_tv_string(&argvars[0]);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013209 if (reset_option_was_set(name) == FAIL)
13210 EMSG2(_(e_invarg2), name);
13211 }
13212}
13213
13214/*
13215 * "test_override({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013216 */
13217 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013218f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013219{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013220 char_u *name = (char_u *)"";
13221 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013222 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013223
13224 if (argvars[0].v_type != VAR_STRING
13225 || (argvars[1].v_type) != VAR_NUMBER)
13226 EMSG(_(e_invarg));
13227 else
13228 {
13229 name = get_tv_string_chk(&argvars[0]);
13230 val = (int)get_tv_number(&argvars[1]);
13231
13232 if (STRCMP(name, (char_u *)"redraw") == 0)
13233 disable_redraw_for_testing = val;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020013234 else if (STRCMP(name, (char_u *)"redraw_flag") == 0)
13235 ignore_redraw_flag_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013236 else if (STRCMP(name, (char_u *)"char_avail") == 0)
13237 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013238 else if (STRCMP(name, (char_u *)"starting") == 0)
13239 {
13240 if (val)
13241 {
13242 if (save_starting < 0)
13243 save_starting = starting;
13244 starting = 0;
13245 }
13246 else
13247 {
13248 starting = save_starting;
13249 save_starting = -1;
13250 }
13251 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013252 else if (STRCMP(name, (char_u *)"nfa_fail") == 0)
13253 nfa_fail_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013254 else if (STRCMP(name, (char_u *)"ALL") == 0)
13255 {
13256 disable_char_avail_for_testing = FALSE;
13257 disable_redraw_for_testing = FALSE;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020013258 ignore_redraw_flag_for_testing = FALSE;
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013259 nfa_fail_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013260 if (save_starting >= 0)
13261 {
13262 starting = save_starting;
13263 save_starting = -1;
13264 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013265 }
13266 else
13267 EMSG2(_(e_invarg2), name);
13268 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013269}
13270
13271/*
13272 * "test_garbagecollect_now()" function
13273 */
13274 static void
13275f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13276{
13277 /* This is dangerous, any Lists and Dicts used internally may be freed
13278 * while still in use. */
13279 garbage_collect(TRUE);
13280}
13281
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013282/*
13283 * "test_ignore_error()" function
13284 */
13285 static void
13286f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
13287{
13288 ignore_error_for_testing(get_tv_string(&argvars[0]));
13289}
13290
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013291#ifdef FEAT_JOB_CHANNEL
13292 static void
13293f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
13294{
13295 rettv->v_type = VAR_CHANNEL;
13296 rettv->vval.v_channel = NULL;
13297}
13298#endif
13299
13300 static void
13301f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
13302{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013303 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013304}
13305
13306#ifdef FEAT_JOB_CHANNEL
13307 static void
13308f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
13309{
13310 rettv->v_type = VAR_JOB;
13311 rettv->vval.v_job = NULL;
13312}
13313#endif
13314
13315 static void
13316f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
13317{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013318 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013319}
13320
13321 static void
13322f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
13323{
13324 rettv->v_type = VAR_PARTIAL;
13325 rettv->vval.v_partial = NULL;
13326}
13327
13328 static void
13329f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
13330{
13331 rettv->v_type = VAR_STRING;
13332 rettv->vval.v_string = NULL;
13333}
13334
Bram Moolenaarab186732018-09-14 21:27:06 +020013335#ifdef FEAT_GUI
13336 static void
13337f_test_scrollbar(typval_T *argvars, typval_T *rettv UNUSED)
13338{
13339 char_u *which;
13340 long value;
13341 int dragging;
13342 scrollbar_T *sb = NULL;
13343
13344 if (argvars[0].v_type != VAR_STRING
13345 || (argvars[1].v_type) != VAR_NUMBER
13346 || (argvars[2].v_type) != VAR_NUMBER)
13347 {
13348 EMSG(_(e_invarg));
13349 return;
13350 }
13351 which = get_tv_string(&argvars[0]);
13352 value = get_tv_number(&argvars[1]);
13353 dragging = get_tv_number(&argvars[2]);
13354
13355 if (STRCMP(which, "left") == 0)
13356 sb = &curwin->w_scrollbars[SBAR_LEFT];
13357 else if (STRCMP(which, "right") == 0)
13358 sb = &curwin->w_scrollbars[SBAR_RIGHT];
13359 else if (STRCMP(which, "hor") == 0)
13360 sb = &gui.bottom_sbar;
13361 if (sb == NULL)
13362 {
13363 EMSG2(_(e_invarg2), which);
13364 return;
13365 }
13366 gui_drag_scrollbar(sb, value, dragging);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020013367# ifndef USE_ON_FLY_SCROLL
13368 // need to loop through normal_cmd() to handle the scroll events
13369 exec_normal(FALSE, TRUE, FALSE);
13370# endif
Bram Moolenaarab186732018-09-14 21:27:06 +020013371}
13372#endif
13373
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013374 static void
13375f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
13376{
13377 time_for_testing = (time_t)get_tv_number(&argvars[0]);
13378}
13379
13380#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
13381/*
13382 * Get a callback from "arg". It can be a Funcref or a function name.
13383 * When "arg" is zero return an empty string.
13384 * Return NULL for an invalid argument.
13385 */
13386 char_u *
13387get_callback(typval_T *arg, partial_T **pp)
13388{
13389 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
13390 {
13391 *pp = arg->vval.v_partial;
13392 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020013393 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013394 }
13395 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020013396 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013397 {
13398 func_ref(arg->vval.v_string);
13399 return arg->vval.v_string;
13400 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013401 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
13402 return (char_u *)"";
13403 EMSG(_("E921: Invalid callback argument"));
13404 return NULL;
13405}
13406
13407/*
Bram Moolenaard5359b22018-04-05 22:44:39 +020013408 * Unref/free "callback" and "partial" returned by get_callback().
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013409 */
13410 void
13411free_callback(char_u *callback, partial_T *partial)
13412{
13413 if (partial != NULL)
13414 partial_unref(partial);
13415 else if (callback != NULL)
13416 {
13417 func_unref(callback);
13418 vim_free(callback);
13419 }
13420}
13421#endif
13422
13423#ifdef FEAT_TIMERS
13424/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020013425 * "timer_info([timer])" function
13426 */
13427 static void
13428f_timer_info(typval_T *argvars, typval_T *rettv)
13429{
13430 timer_T *timer = NULL;
13431
13432 if (rettv_list_alloc(rettv) != OK)
13433 return;
13434 if (argvars[0].v_type != VAR_UNKNOWN)
13435 {
13436 if (argvars[0].v_type != VAR_NUMBER)
13437 EMSG(_(e_number_exp));
13438 else
13439 {
13440 timer = find_timer((int)get_tv_number(&argvars[0]));
13441 if (timer != NULL)
13442 add_timer_info(rettv, timer);
13443 }
13444 }
13445 else
13446 add_timer_info_all(rettv);
13447}
13448
13449/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013450 * "timer_pause(timer, paused)" function
13451 */
13452 static void
13453f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
13454{
13455 timer_T *timer = NULL;
13456 int paused = (int)get_tv_number(&argvars[1]);
13457
13458 if (argvars[0].v_type != VAR_NUMBER)
13459 EMSG(_(e_number_exp));
13460 else
13461 {
13462 timer = find_timer((int)get_tv_number(&argvars[0]));
13463 if (timer != NULL)
13464 timer->tr_paused = paused;
13465 }
13466}
13467
13468/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013469 * "timer_start(time, callback [, options])" function
13470 */
13471 static void
13472f_timer_start(typval_T *argvars, typval_T *rettv)
13473{
Bram Moolenaar75537a92016-09-05 22:45:28 +020013474 long msec = (long)get_tv_number(&argvars[0]);
13475 timer_T *timer;
13476 int repeat = 0;
13477 char_u *callback;
13478 dict_T *dict;
13479 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013480
Bram Moolenaar75537a92016-09-05 22:45:28 +020013481 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013482 if (check_secure())
13483 return;
13484 if (argvars[2].v_type != VAR_UNKNOWN)
13485 {
13486 if (argvars[2].v_type != VAR_DICT
13487 || (dict = argvars[2].vval.v_dict) == NULL)
13488 {
13489 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
13490 return;
13491 }
13492 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
13493 repeat = get_dict_number(dict, (char_u *)"repeat");
13494 }
13495
Bram Moolenaar75537a92016-09-05 22:45:28 +020013496 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013497 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020013498 return;
13499
13500 timer = create_timer(msec, repeat);
13501 if (timer == NULL)
13502 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013503 else
13504 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020013505 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020013506 timer->tr_callback = vim_strsave(callback);
13507 else
13508 /* pointer into the partial */
13509 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020013510 timer->tr_partial = partial;
13511 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013512 }
13513}
13514
13515/*
13516 * "timer_stop(timer)" function
13517 */
13518 static void
13519f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
13520{
13521 timer_T *timer;
13522
13523 if (argvars[0].v_type != VAR_NUMBER)
13524 {
13525 EMSG(_(e_number_exp));
13526 return;
13527 }
13528 timer = find_timer((int)get_tv_number(&argvars[0]));
13529 if (timer != NULL)
13530 stop_timer(timer);
13531}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013532
13533/*
13534 * "timer_stopall()" function
13535 */
13536 static void
13537f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13538{
13539 stop_all_timers();
13540}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013541#endif
13542
13543/*
13544 * "tolower(string)" function
13545 */
13546 static void
13547f_tolower(typval_T *argvars, typval_T *rettv)
13548{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013549 rettv->v_type = VAR_STRING;
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +010013550 rettv->vval.v_string = strlow_save(get_tv_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013551}
13552
13553/*
13554 * "toupper(string)" function
13555 */
13556 static void
13557f_toupper(typval_T *argvars, typval_T *rettv)
13558{
13559 rettv->v_type = VAR_STRING;
13560 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
13561}
13562
13563/*
13564 * "tr(string, fromstr, tostr)" function
13565 */
13566 static void
13567f_tr(typval_T *argvars, typval_T *rettv)
13568{
13569 char_u *in_str;
13570 char_u *fromstr;
13571 char_u *tostr;
13572 char_u *p;
13573#ifdef FEAT_MBYTE
13574 int inlen;
13575 int fromlen;
13576 int tolen;
13577 int idx;
13578 char_u *cpstr;
13579 int cplen;
13580 int first = TRUE;
13581#endif
13582 char_u buf[NUMBUFLEN];
13583 char_u buf2[NUMBUFLEN];
13584 garray_T ga;
13585
13586 in_str = get_tv_string(&argvars[0]);
13587 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
13588 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
13589
13590 /* Default return value: empty string. */
13591 rettv->v_type = VAR_STRING;
13592 rettv->vval.v_string = NULL;
13593 if (fromstr == NULL || tostr == NULL)
13594 return; /* type error; errmsg already given */
13595 ga_init2(&ga, (int)sizeof(char), 80);
13596
13597#ifdef FEAT_MBYTE
13598 if (!has_mbyte)
13599#endif
13600 /* not multi-byte: fromstr and tostr must be the same length */
13601 if (STRLEN(fromstr) != STRLEN(tostr))
13602 {
13603#ifdef FEAT_MBYTE
13604error:
13605#endif
13606 EMSG2(_(e_invarg2), fromstr);
13607 ga_clear(&ga);
13608 return;
13609 }
13610
13611 /* fromstr and tostr have to contain the same number of chars */
13612 while (*in_str != NUL)
13613 {
13614#ifdef FEAT_MBYTE
13615 if (has_mbyte)
13616 {
13617 inlen = (*mb_ptr2len)(in_str);
13618 cpstr = in_str;
13619 cplen = inlen;
13620 idx = 0;
13621 for (p = fromstr; *p != NUL; p += fromlen)
13622 {
13623 fromlen = (*mb_ptr2len)(p);
13624 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
13625 {
13626 for (p = tostr; *p != NUL; p += tolen)
13627 {
13628 tolen = (*mb_ptr2len)(p);
13629 if (idx-- == 0)
13630 {
13631 cplen = tolen;
13632 cpstr = p;
13633 break;
13634 }
13635 }
13636 if (*p == NUL) /* tostr is shorter than fromstr */
13637 goto error;
13638 break;
13639 }
13640 ++idx;
13641 }
13642
13643 if (first && cpstr == in_str)
13644 {
13645 /* Check that fromstr and tostr have the same number of
13646 * (multi-byte) characters. Done only once when a character
13647 * of in_str doesn't appear in fromstr. */
13648 first = FALSE;
13649 for (p = tostr; *p != NUL; p += tolen)
13650 {
13651 tolen = (*mb_ptr2len)(p);
13652 --idx;
13653 }
13654 if (idx != 0)
13655 goto error;
13656 }
13657
13658 (void)ga_grow(&ga, cplen);
13659 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
13660 ga.ga_len += cplen;
13661
13662 in_str += inlen;
13663 }
13664 else
13665#endif
13666 {
13667 /* When not using multi-byte chars we can do it faster. */
13668 p = vim_strchr(fromstr, *in_str);
13669 if (p != NULL)
13670 ga_append(&ga, tostr[p - fromstr]);
13671 else
13672 ga_append(&ga, *in_str);
13673 ++in_str;
13674 }
13675 }
13676
13677 /* add a terminating NUL */
13678 (void)ga_grow(&ga, 1);
13679 ga_append(&ga, NUL);
13680
13681 rettv->vval.v_string = ga.ga_data;
13682}
13683
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010013684/*
13685 * "trim({expr})" function
13686 */
13687 static void
13688f_trim(typval_T *argvars, typval_T *rettv)
13689{
13690 char_u buf1[NUMBUFLEN];
13691 char_u buf2[NUMBUFLEN];
13692 char_u *head = get_tv_string_buf_chk(&argvars[0], buf1);
13693 char_u *mask = NULL;
13694 char_u *tail;
13695 char_u *prev;
13696 char_u *p;
13697 int c1;
13698
13699 rettv->v_type = VAR_STRING;
13700 if (head == NULL)
13701 {
13702 rettv->vval.v_string = NULL;
13703 return;
13704 }
13705
13706 if (argvars[1].v_type == VAR_STRING)
13707 mask = get_tv_string_buf_chk(&argvars[1], buf2);
13708
13709 while (*head != NUL)
13710 {
13711 c1 = PTR2CHAR(head);
13712 if (mask == NULL)
13713 {
13714 if (c1 > ' ' && c1 != 0xa0)
13715 break;
13716 }
13717 else
13718 {
13719 for (p = mask; *p != NUL; MB_PTR_ADV(p))
13720 if (c1 == PTR2CHAR(p))
13721 break;
13722 if (*p == NUL)
13723 break;
13724 }
13725 MB_PTR_ADV(head);
13726 }
13727
13728 for (tail = head + STRLEN(head); tail > head; tail = prev)
13729 {
13730 prev = tail;
13731 MB_PTR_BACK(head, prev);
13732 c1 = PTR2CHAR(prev);
13733 if (mask == NULL)
13734 {
13735 if (c1 > ' ' && c1 != 0xa0)
13736 break;
13737 }
13738 else
13739 {
13740 for (p = mask; *p != NUL; MB_PTR_ADV(p))
13741 if (c1 == PTR2CHAR(p))
13742 break;
13743 if (*p == NUL)
13744 break;
13745 }
13746 }
13747 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
13748}
13749
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013750#ifdef FEAT_FLOAT
13751/*
13752 * "trunc({float})" function
13753 */
13754 static void
13755f_trunc(typval_T *argvars, typval_T *rettv)
13756{
13757 float_T f = 0.0;
13758
13759 rettv->v_type = VAR_FLOAT;
13760 if (get_float_arg(argvars, &f) == OK)
13761 /* trunc() is not in C90, use floor() or ceil() instead. */
13762 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
13763 else
13764 rettv->vval.v_float = 0.0;
13765}
13766#endif
13767
13768/*
13769 * "type(expr)" function
13770 */
13771 static void
13772f_type(typval_T *argvars, typval_T *rettv)
13773{
13774 int n = -1;
13775
13776 switch (argvars[0].v_type)
13777 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020013778 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
13779 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013780 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020013781 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
13782 case VAR_LIST: n = VAR_TYPE_LIST; break;
13783 case VAR_DICT: n = VAR_TYPE_DICT; break;
13784 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013785 case VAR_SPECIAL:
13786 if (argvars[0].vval.v_number == VVAL_FALSE
13787 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020013788 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013789 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020013790 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013791 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020013792 case VAR_JOB: n = VAR_TYPE_JOB; break;
13793 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013794 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010013795 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013796 n = -1;
13797 break;
13798 }
13799 rettv->vval.v_number = n;
13800}
13801
13802/*
13803 * "undofile(name)" function
13804 */
13805 static void
13806f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
13807{
13808 rettv->v_type = VAR_STRING;
13809#ifdef FEAT_PERSISTENT_UNDO
13810 {
13811 char_u *fname = get_tv_string(&argvars[0]);
13812
13813 if (*fname == NUL)
13814 {
13815 /* If there is no file name there will be no undo file. */
13816 rettv->vval.v_string = NULL;
13817 }
13818 else
13819 {
13820 char_u *ffname = FullName_save(fname, FALSE);
13821
13822 if (ffname != NULL)
13823 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
13824 vim_free(ffname);
13825 }
13826 }
13827#else
13828 rettv->vval.v_string = NULL;
13829#endif
13830}
13831
13832/*
13833 * "undotree()" function
13834 */
13835 static void
13836f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
13837{
13838 if (rettv_dict_alloc(rettv) == OK)
13839 {
13840 dict_T *dict = rettv->vval.v_dict;
13841 list_T *list;
13842
Bram Moolenaare0be1672018-07-08 16:50:37 +020013843 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
13844 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
13845 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
13846 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
13847 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
13848 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013849
13850 list = list_alloc();
13851 if (list != NULL)
13852 {
13853 u_eval_tree(curbuf->b_u_oldhead, list);
13854 dict_add_list(dict, "entries", list);
13855 }
13856 }
13857}
13858
13859/*
13860 * "values(dict)" function
13861 */
13862 static void
13863f_values(typval_T *argvars, typval_T *rettv)
13864{
13865 dict_list(argvars, rettv, 1);
13866}
13867
13868/*
13869 * "virtcol(string)" function
13870 */
13871 static void
13872f_virtcol(typval_T *argvars, typval_T *rettv)
13873{
13874 colnr_T vcol = 0;
13875 pos_T *fp;
13876 int fnum = curbuf->b_fnum;
13877
13878 fp = var2fpos(&argvars[0], FALSE, &fnum);
13879 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
13880 && fnum == curbuf->b_fnum)
13881 {
13882 getvvcol(curwin, fp, NULL, NULL, &vcol);
13883 ++vcol;
13884 }
13885
13886 rettv->vval.v_number = vcol;
13887}
13888
13889/*
13890 * "visualmode()" function
13891 */
13892 static void
13893f_visualmode(typval_T *argvars, typval_T *rettv)
13894{
13895 char_u str[2];
13896
13897 rettv->v_type = VAR_STRING;
13898 str[0] = curbuf->b_visual_mode_eval;
13899 str[1] = NUL;
13900 rettv->vval.v_string = vim_strsave(str);
13901
13902 /* A non-zero number or non-empty string argument: reset mode. */
13903 if (non_zero_arg(&argvars[0]))
13904 curbuf->b_visual_mode_eval = NUL;
13905}
13906
13907/*
13908 * "wildmenumode()" function
13909 */
13910 static void
13911f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13912{
13913#ifdef FEAT_WILDMENU
13914 if (wild_menu_showing)
13915 rettv->vval.v_number = 1;
13916#endif
13917}
13918
13919/*
13920 * "winbufnr(nr)" function
13921 */
13922 static void
13923f_winbufnr(typval_T *argvars, typval_T *rettv)
13924{
13925 win_T *wp;
13926
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020013927 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013928 if (wp == NULL)
13929 rettv->vval.v_number = -1;
13930 else
13931 rettv->vval.v_number = wp->w_buffer->b_fnum;
13932}
13933
13934/*
13935 * "wincol()" function
13936 */
13937 static void
13938f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
13939{
13940 validate_cursor();
13941 rettv->vval.v_number = curwin->w_wcol + 1;
13942}
13943
13944/*
13945 * "winheight(nr)" function
13946 */
13947 static void
13948f_winheight(typval_T *argvars, typval_T *rettv)
13949{
13950 win_T *wp;
13951
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020013952 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013953 if (wp == NULL)
13954 rettv->vval.v_number = -1;
13955 else
13956 rettv->vval.v_number = wp->w_height;
13957}
13958
13959/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020013960 * "winlayout()" function
13961 */
13962 static void
13963f_winlayout(typval_T *argvars, typval_T *rettv)
13964{
13965 tabpage_T *tp;
13966
13967 if (rettv_list_alloc(rettv) != OK)
13968 return;
13969
13970 if (argvars[0].v_type == VAR_UNKNOWN)
13971 tp = curtab;
13972 else
13973 {
13974 tp = find_tabpage((int)get_tv_number(&argvars[0]));
13975 if (tp == NULL)
13976 return;
13977 }
13978
13979 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
13980}
13981
13982/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013983 * "winline()" function
13984 */
13985 static void
13986f_winline(typval_T *argvars UNUSED, typval_T *rettv)
13987{
13988 validate_cursor();
13989 rettv->vval.v_number = curwin->w_wrow + 1;
13990}
13991
13992/*
13993 * "winnr()" function
13994 */
13995 static void
13996f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
13997{
13998 int nr = 1;
13999
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014000 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014001 rettv->vval.v_number = nr;
14002}
14003
14004/*
14005 * "winrestcmd()" function
14006 */
14007 static void
14008f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
14009{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014010 win_T *wp;
14011 int winnr = 1;
14012 garray_T ga;
14013 char_u buf[50];
14014
14015 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020014016 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014017 {
14018 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
14019 ga_concat(&ga, buf);
14020 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
14021 ga_concat(&ga, buf);
14022 ++winnr;
14023 }
14024 ga_append(&ga, NUL);
14025
14026 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014027 rettv->v_type = VAR_STRING;
14028}
14029
14030/*
14031 * "winrestview()" function
14032 */
14033 static void
14034f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
14035{
14036 dict_T *dict;
14037
14038 if (argvars[0].v_type != VAR_DICT
14039 || (dict = argvars[0].vval.v_dict) == NULL)
14040 EMSG(_(e_invarg));
14041 else
14042 {
14043 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
14044 curwin->w_cursor.lnum = (linenr_T)get_dict_number(dict, (char_u *)"lnum");
14045 if (dict_find(dict, (char_u *)"col", -1) != NULL)
14046 curwin->w_cursor.col = (colnr_T)get_dict_number(dict, (char_u *)"col");
14047#ifdef FEAT_VIRTUALEDIT
14048 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
14049 curwin->w_cursor.coladd = (colnr_T)get_dict_number(dict, (char_u *)"coladd");
14050#endif
14051 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
14052 {
14053 curwin->w_curswant = (colnr_T)get_dict_number(dict, (char_u *)"curswant");
14054 curwin->w_set_curswant = FALSE;
14055 }
14056
14057 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
14058 set_topline(curwin, (linenr_T)get_dict_number(dict, (char_u *)"topline"));
14059#ifdef FEAT_DIFF
14060 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
14061 curwin->w_topfill = (int)get_dict_number(dict, (char_u *)"topfill");
14062#endif
14063 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
14064 curwin->w_leftcol = (colnr_T)get_dict_number(dict, (char_u *)"leftcol");
14065 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
14066 curwin->w_skipcol = (colnr_T)get_dict_number(dict, (char_u *)"skipcol");
14067
14068 check_cursor();
14069 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020014070 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014071 changed_window_setting();
14072
14073 if (curwin->w_topline <= 0)
14074 curwin->w_topline = 1;
14075 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
14076 curwin->w_topline = curbuf->b_ml.ml_line_count;
14077#ifdef FEAT_DIFF
14078 check_topfill(curwin, TRUE);
14079#endif
14080 }
14081}
14082
14083/*
14084 * "winsaveview()" function
14085 */
14086 static void
14087f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
14088{
14089 dict_T *dict;
14090
14091 if (rettv_dict_alloc(rettv) == FAIL)
14092 return;
14093 dict = rettv->vval.v_dict;
14094
Bram Moolenaare0be1672018-07-08 16:50:37 +020014095 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
14096 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014097#ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +020014098 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014099#endif
14100 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020014101 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014102
Bram Moolenaare0be1672018-07-08 16:50:37 +020014103 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014104#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020014105 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014106#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020014107 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
14108 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014109}
14110
14111/*
14112 * "winwidth(nr)" function
14113 */
14114 static void
14115f_winwidth(typval_T *argvars, typval_T *rettv)
14116{
14117 win_T *wp;
14118
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014119 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014120 if (wp == NULL)
14121 rettv->vval.v_number = -1;
14122 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014123 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014124}
14125
14126/*
14127 * "wordcount()" function
14128 */
14129 static void
14130f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
14131{
14132 if (rettv_dict_alloc(rettv) == FAIL)
14133 return;
14134 cursor_pos_info(rettv->vval.v_dict);
14135}
14136
14137/*
14138 * "writefile()" function
14139 */
14140 static void
14141f_writefile(typval_T *argvars, typval_T *rettv)
14142{
14143 int binary = FALSE;
14144 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014145#ifdef HAVE_FSYNC
14146 int do_fsync = p_fs;
14147#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014148 char_u *fname;
14149 FILE *fd;
14150 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014151 listitem_T *li;
14152 list_T *list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014153
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014154 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014155 if (check_restricted() || check_secure())
14156 return;
14157
14158 if (argvars[0].v_type != VAR_LIST)
14159 {
14160 EMSG2(_(e_listarg), "writefile()");
14161 return;
14162 }
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014163 list = argvars[0].vval.v_list;
14164 if (list == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014165 return;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014166 for (li = list->lv_first; li != NULL; li = li->li_next)
14167 if (get_tv_string_chk(&li->li_tv) == NULL)
14168 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014169
14170 if (argvars[2].v_type != VAR_UNKNOWN)
14171 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014172 char_u *arg2 = get_tv_string_chk(&argvars[2]);
14173
14174 if (arg2 == NULL)
14175 return;
14176 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014177 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014178 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014179 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014180#ifdef HAVE_FSYNC
14181 if (vim_strchr(arg2, 's') != NULL)
14182 do_fsync = TRUE;
14183 else if (vim_strchr(arg2, 'S') != NULL)
14184 do_fsync = FALSE;
14185#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014186 }
14187
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014188 fname = get_tv_string_chk(&argvars[1]);
14189 if (fname == NULL)
14190 return;
14191
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014192 /* Always open the file in binary mode, library functions have a mind of
14193 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014194 if (*fname == NUL || (fd = mch_fopen((char *)fname,
14195 append ? APPENDBIN : WRITEBIN)) == NULL)
14196 {
14197 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
14198 ret = -1;
14199 }
14200 else
14201 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014202 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014203 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014204#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010014205 else if (do_fsync)
14206 /* Ignore the error, the user wouldn't know what to do about it.
14207 * May happen for a device. */
Bram Moolenaar42335f52018-09-13 15:33:43 +020014208 vim_ignored = fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014209#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014210 fclose(fd);
14211 }
14212
14213 rettv->vval.v_number = ret;
14214}
14215
14216/*
14217 * "xor(expr, expr)" function
14218 */
14219 static void
14220f_xor(typval_T *argvars, typval_T *rettv)
14221{
14222 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14223 ^ get_tv_number_chk(&argvars[1], NULL);
14224}
14225
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014226#endif /* FEAT_EVAL */