blob: 7325f5dfc5c6ce91c14a5b21b4146fd9940e94bd [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalfunc.c: Builtin functions
12 */
13#define USING_FLOAT_STUFF
14
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
23#ifdef VMS
24# include <float.h>
25#endif
26
Bram Moolenaard0573012017-10-28 21:11:06 +020027#ifdef MACOS_X
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020028# include <time.h> /* for time_t */
29#endif
30
31static char *e_listarg = N_("E686: Argument of %s must be a List");
32#ifdef FEAT_QUICKFIX
33static char *e_stringreq = N_("E928: String required");
34#endif
35
36#ifdef FEAT_FLOAT
37static void f_abs(typval_T *argvars, typval_T *rettv);
38static void f_acos(typval_T *argvars, typval_T *rettv);
39#endif
40static void f_add(typval_T *argvars, typval_T *rettv);
41static void f_and(typval_T *argvars, typval_T *rettv);
42static void f_append(typval_T *argvars, typval_T *rettv);
Bram Moolenaarca851592018-06-06 21:04:07 +020043static void f_appendbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020044static void f_argc(typval_T *argvars, typval_T *rettv);
45static void f_argidx(typval_T *argvars, typval_T *rettv);
46static void f_arglistid(typval_T *argvars, typval_T *rettv);
47static void f_argv(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +010048static void f_assert_beeps(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020049static void f_assert_equal(typval_T *argvars, typval_T *rettv);
Bram Moolenaard96ff162018-02-18 22:13:29 +010050static void f_assert_equalfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020051static void f_assert_exception(typval_T *argvars, typval_T *rettv);
52static void f_assert_fails(typval_T *argvars, typval_T *rettv);
53static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaar61c04492016-07-23 15:35:35 +020054static void f_assert_inrange(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020055static void f_assert_match(typval_T *argvars, typval_T *rettv);
56static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
57static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar42205552017-03-18 19:42:22 +010058static void f_assert_report(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020059static void f_assert_true(typval_T *argvars, typval_T *rettv);
60#ifdef FEAT_FLOAT
61static void f_asin(typval_T *argvars, typval_T *rettv);
62static void f_atan(typval_T *argvars, typval_T *rettv);
63static void f_atan2(typval_T *argvars, typval_T *rettv);
64#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010065#ifdef FEAT_BEVAL
66static void f_balloon_show(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010067# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010068static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010069# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010070#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020071static void f_browse(typval_T *argvars, typval_T *rettv);
72static void f_browsedir(typval_T *argvars, typval_T *rettv);
73static void f_bufexists(typval_T *argvars, typval_T *rettv);
74static void f_buflisted(typval_T *argvars, typval_T *rettv);
75static void f_bufloaded(typval_T *argvars, typval_T *rettv);
76static void f_bufname(typval_T *argvars, typval_T *rettv);
77static void f_bufnr(typval_T *argvars, typval_T *rettv);
78static void f_bufwinid(typval_T *argvars, typval_T *rettv);
79static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
80static void f_byte2line(typval_T *argvars, typval_T *rettv);
81static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
82static void f_byteidx(typval_T *argvars, typval_T *rettv);
83static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
84static void f_call(typval_T *argvars, typval_T *rettv);
85#ifdef FEAT_FLOAT
86static void f_ceil(typval_T *argvars, typval_T *rettv);
87#endif
88#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +010089static void f_ch_canread(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020090static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0874a832016-09-01 15:11:51 +020091static void f_ch_close_in(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020092static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
93static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
94static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
95static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
96static void f_ch_info(typval_T *argvars, typval_T *rettv);
97static void f_ch_log(typval_T *argvars, typval_T *rettv);
98static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
99static void f_ch_open(typval_T *argvars, typval_T *rettv);
100static void f_ch_read(typval_T *argvars, typval_T *rettv);
101static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
102static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
103static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
104static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
105static void f_ch_status(typval_T *argvars, typval_T *rettv);
106#endif
107static void f_changenr(typval_T *argvars, typval_T *rettv);
108static void f_char2nr(typval_T *argvars, typval_T *rettv);
109static void f_cindent(typval_T *argvars, typval_T *rettv);
110static void f_clearmatches(typval_T *argvars, typval_T *rettv);
111static void f_col(typval_T *argvars, typval_T *rettv);
112#if defined(FEAT_INS_EXPAND)
113static void f_complete(typval_T *argvars, typval_T *rettv);
114static void f_complete_add(typval_T *argvars, typval_T *rettv);
115static void f_complete_check(typval_T *argvars, typval_T *rettv);
116#endif
117static void f_confirm(typval_T *argvars, typval_T *rettv);
118static void f_copy(typval_T *argvars, typval_T *rettv);
119#ifdef FEAT_FLOAT
120static void f_cos(typval_T *argvars, typval_T *rettv);
121static void f_cosh(typval_T *argvars, typval_T *rettv);
122#endif
123static void f_count(typval_T *argvars, typval_T *rettv);
124static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
125static void f_cursor(typval_T *argsvars, typval_T *rettv);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200126#ifdef WIN3264
127static void f_debugbreak(typval_T *argvars, typval_T *rettv);
128#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200129static void f_deepcopy(typval_T *argvars, typval_T *rettv);
130static void f_delete(typval_T *argvars, typval_T *rettv);
Bram Moolenaard79a2622018-06-07 18:17:46 +0200131static void f_deletebufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200132static void f_did_filetype(typval_T *argvars, typval_T *rettv);
133static void f_diff_filler(typval_T *argvars, typval_T *rettv);
134static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
135static void f_empty(typval_T *argvars, typval_T *rettv);
136static void f_escape(typval_T *argvars, typval_T *rettv);
137static void f_eval(typval_T *argvars, typval_T *rettv);
138static void f_eventhandler(typval_T *argvars, typval_T *rettv);
139static void f_executable(typval_T *argvars, typval_T *rettv);
140static void f_execute(typval_T *argvars, typval_T *rettv);
141static void f_exepath(typval_T *argvars, typval_T *rettv);
142static void f_exists(typval_T *argvars, typval_T *rettv);
143#ifdef FEAT_FLOAT
144static void f_exp(typval_T *argvars, typval_T *rettv);
145#endif
146static void f_expand(typval_T *argvars, typval_T *rettv);
147static void f_extend(typval_T *argvars, typval_T *rettv);
148static void f_feedkeys(typval_T *argvars, typval_T *rettv);
149static void f_filereadable(typval_T *argvars, typval_T *rettv);
150static void f_filewritable(typval_T *argvars, typval_T *rettv);
151static void f_filter(typval_T *argvars, typval_T *rettv);
152static void f_finddir(typval_T *argvars, typval_T *rettv);
153static void f_findfile(typval_T *argvars, typval_T *rettv);
154#ifdef FEAT_FLOAT
155static void f_float2nr(typval_T *argvars, typval_T *rettv);
156static void f_floor(typval_T *argvars, typval_T *rettv);
157static void f_fmod(typval_T *argvars, typval_T *rettv);
158#endif
159static void f_fnameescape(typval_T *argvars, typval_T *rettv);
160static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
161static void f_foldclosed(typval_T *argvars, typval_T *rettv);
162static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
163static void f_foldlevel(typval_T *argvars, typval_T *rettv);
164static void f_foldtext(typval_T *argvars, typval_T *rettv);
165static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
166static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200167static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200168static void f_function(typval_T *argvars, typval_T *rettv);
169static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
170static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200171static void f_getbufinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200172static void f_getbufline(typval_T *argvars, typval_T *rettv);
173static void f_getbufvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100174static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200175static void f_getchar(typval_T *argvars, typval_T *rettv);
176static void f_getcharmod(typval_T *argvars, typval_T *rettv);
177static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
178static void f_getcmdline(typval_T *argvars, typval_T *rettv);
179#if defined(FEAT_CMDL_COMPL)
180static void f_getcompletion(typval_T *argvars, typval_T *rettv);
181#endif
182static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
183static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
184static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
185static void f_getcwd(typval_T *argvars, typval_T *rettv);
186static void f_getfontname(typval_T *argvars, typval_T *rettv);
187static void f_getfperm(typval_T *argvars, typval_T *rettv);
188static void f_getfsize(typval_T *argvars, typval_T *rettv);
189static void f_getftime(typval_T *argvars, typval_T *rettv);
190static void f_getftype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +0100191static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200192static void f_getline(typval_T *argvars, typval_T *rettv);
Bram Moolenaard823fa92016-08-12 16:29:27 +0200193static void f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200194static void f_getmatches(typval_T *argvars, typval_T *rettv);
195static void f_getpid(typval_T *argvars, typval_T *rettv);
196static void f_getcurpos(typval_T *argvars, typval_T *rettv);
197static void f_getpos(typval_T *argvars, typval_T *rettv);
198static void f_getqflist(typval_T *argvars, typval_T *rettv);
199static void f_getreg(typval_T *argvars, typval_T *rettv);
200static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200201static void f_gettabinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200202static void f_gettabvar(typval_T *argvars, typval_T *rettv);
203static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200204static void f_getwininfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100205static void f_getwinpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200206static void f_getwinposx(typval_T *argvars, typval_T *rettv);
207static void f_getwinposy(typval_T *argvars, typval_T *rettv);
208static void f_getwinvar(typval_T *argvars, typval_T *rettv);
209static void f_glob(typval_T *argvars, typval_T *rettv);
210static void f_globpath(typval_T *argvars, typval_T *rettv);
211static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
212static void f_has(typval_T *argvars, typval_T *rettv);
213static void f_has_key(typval_T *argvars, typval_T *rettv);
214static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
215static void f_hasmapto(typval_T *argvars, typval_T *rettv);
216static void f_histadd(typval_T *argvars, typval_T *rettv);
217static void f_histdel(typval_T *argvars, typval_T *rettv);
218static void f_histget(typval_T *argvars, typval_T *rettv);
219static void f_histnr(typval_T *argvars, typval_T *rettv);
220static void f_hlID(typval_T *argvars, typval_T *rettv);
221static void f_hlexists(typval_T *argvars, typval_T *rettv);
222static void f_hostname(typval_T *argvars, typval_T *rettv);
223static void f_iconv(typval_T *argvars, typval_T *rettv);
224static void f_indent(typval_T *argvars, typval_T *rettv);
225static void f_index(typval_T *argvars, typval_T *rettv);
226static void f_input(typval_T *argvars, typval_T *rettv);
227static void f_inputdialog(typval_T *argvars, typval_T *rettv);
228static void f_inputlist(typval_T *argvars, typval_T *rettv);
229static void f_inputrestore(typval_T *argvars, typval_T *rettv);
230static void f_inputsave(typval_T *argvars, typval_T *rettv);
231static void f_inputsecret(typval_T *argvars, typval_T *rettv);
232static void f_insert(typval_T *argvars, typval_T *rettv);
233static void f_invert(typval_T *argvars, typval_T *rettv);
234static void f_isdirectory(typval_T *argvars, typval_T *rettv);
235static void f_islocked(typval_T *argvars, typval_T *rettv);
236#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
237static void f_isnan(typval_T *argvars, typval_T *rettv);
238#endif
239static void f_items(typval_T *argvars, typval_T *rettv);
240#ifdef FEAT_JOB_CHANNEL
241static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
242static void f_job_info(typval_T *argvars, typval_T *rettv);
243static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
244static void f_job_start(typval_T *argvars, typval_T *rettv);
245static void f_job_stop(typval_T *argvars, typval_T *rettv);
246static void f_job_status(typval_T *argvars, typval_T *rettv);
247#endif
248static void f_join(typval_T *argvars, typval_T *rettv);
249static void f_js_decode(typval_T *argvars, typval_T *rettv);
250static void f_js_encode(typval_T *argvars, typval_T *rettv);
251static void f_json_decode(typval_T *argvars, typval_T *rettv);
252static void f_json_encode(typval_T *argvars, typval_T *rettv);
253static void f_keys(typval_T *argvars, typval_T *rettv);
254static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
255static void f_len(typval_T *argvars, typval_T *rettv);
256static void f_libcall(typval_T *argvars, typval_T *rettv);
257static void f_libcallnr(typval_T *argvars, typval_T *rettv);
258static void f_line(typval_T *argvars, typval_T *rettv);
259static void f_line2byte(typval_T *argvars, typval_T *rettv);
260static void f_lispindent(typval_T *argvars, typval_T *rettv);
261static void f_localtime(typval_T *argvars, typval_T *rettv);
262#ifdef FEAT_FLOAT
263static void f_log(typval_T *argvars, typval_T *rettv);
264static void f_log10(typval_T *argvars, typval_T *rettv);
265#endif
266#ifdef FEAT_LUA
267static void f_luaeval(typval_T *argvars, typval_T *rettv);
268#endif
269static void f_map(typval_T *argvars, typval_T *rettv);
270static void f_maparg(typval_T *argvars, typval_T *rettv);
271static void f_mapcheck(typval_T *argvars, typval_T *rettv);
272static void f_match(typval_T *argvars, typval_T *rettv);
273static void f_matchadd(typval_T *argvars, typval_T *rettv);
274static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
275static void f_matcharg(typval_T *argvars, typval_T *rettv);
276static void f_matchdelete(typval_T *argvars, typval_T *rettv);
277static void f_matchend(typval_T *argvars, typval_T *rettv);
278static void f_matchlist(typval_T *argvars, typval_T *rettv);
279static void f_matchstr(typval_T *argvars, typval_T *rettv);
280static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
281static void f_max(typval_T *argvars, typval_T *rettv);
282static void f_min(typval_T *argvars, typval_T *rettv);
283#ifdef vim_mkdir
284static void f_mkdir(typval_T *argvars, typval_T *rettv);
285#endif
286static void f_mode(typval_T *argvars, typval_T *rettv);
287#ifdef FEAT_MZSCHEME
288static void f_mzeval(typval_T *argvars, typval_T *rettv);
289#endif
290static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
291static void f_nr2char(typval_T *argvars, typval_T *rettv);
292static void f_or(typval_T *argvars, typval_T *rettv);
293static void f_pathshorten(typval_T *argvars, typval_T *rettv);
294#ifdef FEAT_PERL
295static void f_perleval(typval_T *argvars, typval_T *rettv);
296#endif
297#ifdef FEAT_FLOAT
298static void f_pow(typval_T *argvars, typval_T *rettv);
299#endif
300static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
301static void f_printf(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200302#ifdef FEAT_JOB_CHANNEL
303static void f_prompt_setcallback(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200304static void f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200305static void f_prompt_setprompt(typval_T *argvars, typval_T *rettv);
306#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200307static void f_pumvisible(typval_T *argvars, typval_T *rettv);
308#ifdef FEAT_PYTHON3
309static void f_py3eval(typval_T *argvars, typval_T *rettv);
310#endif
311#ifdef FEAT_PYTHON
312static void f_pyeval(typval_T *argvars, typval_T *rettv);
313#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100314#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
315static void f_pyxeval(typval_T *argvars, typval_T *rettv);
316#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200317static void f_range(typval_T *argvars, typval_T *rettv);
318static void f_readfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200319static void f_reg_executing(typval_T *argvars, typval_T *rettv);
320static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200321static void f_reltime(typval_T *argvars, typval_T *rettv);
322#ifdef FEAT_FLOAT
323static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
324#endif
325static void f_reltimestr(typval_T *argvars, typval_T *rettv);
326static void f_remote_expr(typval_T *argvars, typval_T *rettv);
327static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
328static void f_remote_peek(typval_T *argvars, typval_T *rettv);
329static void f_remote_read(typval_T *argvars, typval_T *rettv);
330static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100331static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200332static void f_remove(typval_T *argvars, typval_T *rettv);
333static void f_rename(typval_T *argvars, typval_T *rettv);
334static void f_repeat(typval_T *argvars, typval_T *rettv);
335static void f_resolve(typval_T *argvars, typval_T *rettv);
336static void f_reverse(typval_T *argvars, typval_T *rettv);
337#ifdef FEAT_FLOAT
338static void f_round(typval_T *argvars, typval_T *rettv);
339#endif
340static void f_screenattr(typval_T *argvars, typval_T *rettv);
341static void f_screenchar(typval_T *argvars, typval_T *rettv);
342static void f_screencol(typval_T *argvars, typval_T *rettv);
343static void f_screenrow(typval_T *argvars, typval_T *rettv);
344static void f_search(typval_T *argvars, typval_T *rettv);
345static void f_searchdecl(typval_T *argvars, typval_T *rettv);
346static void f_searchpair(typval_T *argvars, typval_T *rettv);
347static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
348static void f_searchpos(typval_T *argvars, typval_T *rettv);
349static void f_server2client(typval_T *argvars, typval_T *rettv);
350static void f_serverlist(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200351static void f_setbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200352static void f_setbufvar(typval_T *argvars, typval_T *rettv);
353static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
354static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
355static void f_setfperm(typval_T *argvars, typval_T *rettv);
356static void f_setline(typval_T *argvars, typval_T *rettv);
357static void f_setloclist(typval_T *argvars, typval_T *rettv);
358static void f_setmatches(typval_T *argvars, typval_T *rettv);
359static void f_setpos(typval_T *argvars, typval_T *rettv);
360static void f_setqflist(typval_T *argvars, typval_T *rettv);
361static void f_setreg(typval_T *argvars, typval_T *rettv);
362static void f_settabvar(typval_T *argvars, typval_T *rettv);
363static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
364static void f_setwinvar(typval_T *argvars, typval_T *rettv);
365#ifdef FEAT_CRYPT
366static void f_sha256(typval_T *argvars, typval_T *rettv);
367#endif /* FEAT_CRYPT */
368static void f_shellescape(typval_T *argvars, typval_T *rettv);
369static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
370static void f_simplify(typval_T *argvars, typval_T *rettv);
371#ifdef FEAT_FLOAT
372static void f_sin(typval_T *argvars, typval_T *rettv);
373static void f_sinh(typval_T *argvars, typval_T *rettv);
374#endif
375static void f_sort(typval_T *argvars, typval_T *rettv);
376static void f_soundfold(typval_T *argvars, typval_T *rettv);
377static void f_spellbadword(typval_T *argvars, typval_T *rettv);
378static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
379static void f_split(typval_T *argvars, typval_T *rettv);
380#ifdef FEAT_FLOAT
381static void f_sqrt(typval_T *argvars, typval_T *rettv);
382static void f_str2float(typval_T *argvars, typval_T *rettv);
383#endif
384static void f_str2nr(typval_T *argvars, typval_T *rettv);
385static void f_strchars(typval_T *argvars, typval_T *rettv);
386#ifdef HAVE_STRFTIME
387static void f_strftime(typval_T *argvars, typval_T *rettv);
388#endif
389static void f_strgetchar(typval_T *argvars, typval_T *rettv);
390static void f_stridx(typval_T *argvars, typval_T *rettv);
391static void f_string(typval_T *argvars, typval_T *rettv);
392static void f_strlen(typval_T *argvars, typval_T *rettv);
393static void f_strcharpart(typval_T *argvars, typval_T *rettv);
394static void f_strpart(typval_T *argvars, typval_T *rettv);
395static void f_strridx(typval_T *argvars, typval_T *rettv);
396static void f_strtrans(typval_T *argvars, typval_T *rettv);
397static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
398static void f_strwidth(typval_T *argvars, typval_T *rettv);
399static void f_submatch(typval_T *argvars, typval_T *rettv);
400static void f_substitute(typval_T *argvars, typval_T *rettv);
401static void f_synID(typval_T *argvars, typval_T *rettv);
402static void f_synIDattr(typval_T *argvars, typval_T *rettv);
403static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
404static void f_synstack(typval_T *argvars, typval_T *rettv);
405static void f_synconcealed(typval_T *argvars, typval_T *rettv);
406static void f_system(typval_T *argvars, typval_T *rettv);
407static void f_systemlist(typval_T *argvars, typval_T *rettv);
408static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
409static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
410static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
411static void f_taglist(typval_T *argvars, typval_T *rettv);
412static void f_tagfiles(typval_T *argvars, typval_T *rettv);
413static void f_tempname(typval_T *argvars, typval_T *rettv);
414static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
415static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200416static void f_test_feedinput(typval_T *argvars, typval_T *rettv);
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100417static void f_test_override(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200418static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100419static void f_test_ignore_error(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200420#ifdef FEAT_JOB_CHANNEL
421static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
422#endif
423static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
424#ifdef FEAT_JOB_CHANNEL
425static void f_test_null_job(typval_T *argvars, typval_T *rettv);
426#endif
427static void f_test_null_list(typval_T *argvars, typval_T *rettv);
428static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
429static void f_test_null_string(typval_T *argvars, typval_T *rettv);
430static void f_test_settime(typval_T *argvars, typval_T *rettv);
431#ifdef FEAT_FLOAT
432static void f_tan(typval_T *argvars, typval_T *rettv);
433static void f_tanh(typval_T *argvars, typval_T *rettv);
434#endif
435#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200436static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200437static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200438static void f_timer_start(typval_T *argvars, typval_T *rettv);
439static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200440static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200441#endif
442static void f_tolower(typval_T *argvars, typval_T *rettv);
443static void f_toupper(typval_T *argvars, typval_T *rettv);
444static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100445static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200446#ifdef FEAT_FLOAT
447static void f_trunc(typval_T *argvars, typval_T *rettv);
448#endif
449static void f_type(typval_T *argvars, typval_T *rettv);
450static void f_undofile(typval_T *argvars, typval_T *rettv);
451static void f_undotree(typval_T *argvars, typval_T *rettv);
452static void f_uniq(typval_T *argvars, typval_T *rettv);
453static void f_values(typval_T *argvars, typval_T *rettv);
454static void f_virtcol(typval_T *argvars, typval_T *rettv);
455static void f_visualmode(typval_T *argvars, typval_T *rettv);
456static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
457static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
458static void f_win_getid(typval_T *argvars, typval_T *rettv);
459static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
460static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
461static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100462static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200463static void f_winbufnr(typval_T *argvars, typval_T *rettv);
464static void f_wincol(typval_T *argvars, typval_T *rettv);
465static void f_winheight(typval_T *argvars, typval_T *rettv);
466static void f_winline(typval_T *argvars, typval_T *rettv);
467static void f_winnr(typval_T *argvars, typval_T *rettv);
468static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
469static void f_winrestview(typval_T *argvars, typval_T *rettv);
470static void f_winsaveview(typval_T *argvars, typval_T *rettv);
471static void f_winwidth(typval_T *argvars, typval_T *rettv);
472static void f_writefile(typval_T *argvars, typval_T *rettv);
473static void f_wordcount(typval_T *argvars, typval_T *rettv);
474static void f_xor(typval_T *argvars, typval_T *rettv);
475
476/*
477 * Array with names and number of arguments of all internal functions
478 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
479 */
480static struct fst
481{
482 char *f_name; /* function name */
483 char f_min_argc; /* minimal number of arguments */
484 char f_max_argc; /* maximal number of arguments */
485 void (*f_func)(typval_T *args, typval_T *rvar);
486 /* implementation of function */
487} functions[] =
488{
489#ifdef FEAT_FLOAT
490 {"abs", 1, 1, f_abs},
491 {"acos", 1, 1, f_acos}, /* WJMc */
492#endif
493 {"add", 2, 2, f_add},
494 {"and", 2, 2, f_and},
495 {"append", 2, 2, f_append},
Bram Moolenaarca851592018-06-06 21:04:07 +0200496 {"appendbufline", 3, 3, f_appendbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200497 {"argc", 0, 0, f_argc},
498 {"argidx", 0, 0, f_argidx},
499 {"arglistid", 0, 2, f_arglistid},
500 {"argv", 0, 1, f_argv},
501#ifdef FEAT_FLOAT
502 {"asin", 1, 1, f_asin}, /* WJMc */
503#endif
Bram Moolenaarb48e96f2018-02-13 12:26:14 +0100504 {"assert_beeps", 1, 2, f_assert_beeps},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200505 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaard96ff162018-02-18 22:13:29 +0100506 {"assert_equalfile", 2, 2, f_assert_equalfile},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200507 {"assert_exception", 1, 2, f_assert_exception},
508 {"assert_fails", 1, 2, f_assert_fails},
509 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100510 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200511 {"assert_match", 2, 3, f_assert_match},
512 {"assert_notequal", 2, 3, f_assert_notequal},
513 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100514 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200515 {"assert_true", 1, 2, f_assert_true},
516#ifdef FEAT_FLOAT
517 {"atan", 1, 1, f_atan},
518 {"atan2", 2, 2, f_atan2},
519#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100520#ifdef FEAT_BEVAL
521 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100522# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100523 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100524# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100525#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200526 {"browse", 4, 4, f_browse},
527 {"browsedir", 2, 2, f_browsedir},
528 {"bufexists", 1, 1, f_bufexists},
529 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
530 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
531 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
532 {"buflisted", 1, 1, f_buflisted},
533 {"bufloaded", 1, 1, f_bufloaded},
534 {"bufname", 1, 1, f_bufname},
535 {"bufnr", 1, 2, f_bufnr},
536 {"bufwinid", 1, 1, f_bufwinid},
537 {"bufwinnr", 1, 1, f_bufwinnr},
538 {"byte2line", 1, 1, f_byte2line},
539 {"byteidx", 2, 2, f_byteidx},
540 {"byteidxcomp", 2, 2, f_byteidxcomp},
541 {"call", 2, 3, f_call},
542#ifdef FEAT_FLOAT
543 {"ceil", 1, 1, f_ceil},
544#endif
545#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100546 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200547 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200548 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200549 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
550 {"ch_evalraw", 2, 3, f_ch_evalraw},
551 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
552 {"ch_getjob", 1, 1, f_ch_getjob},
553 {"ch_info", 1, 1, f_ch_info},
554 {"ch_log", 1, 2, f_ch_log},
555 {"ch_logfile", 1, 2, f_ch_logfile},
556 {"ch_open", 1, 2, f_ch_open},
557 {"ch_read", 1, 2, f_ch_read},
558 {"ch_readraw", 1, 2, f_ch_readraw},
559 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
560 {"ch_sendraw", 2, 3, f_ch_sendraw},
561 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200562 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200563#endif
564 {"changenr", 0, 0, f_changenr},
565 {"char2nr", 1, 2, f_char2nr},
566 {"cindent", 1, 1, f_cindent},
567 {"clearmatches", 0, 0, f_clearmatches},
568 {"col", 1, 1, f_col},
569#if defined(FEAT_INS_EXPAND)
570 {"complete", 2, 2, f_complete},
571 {"complete_add", 1, 1, f_complete_add},
572 {"complete_check", 0, 0, f_complete_check},
573#endif
574 {"confirm", 1, 4, f_confirm},
575 {"copy", 1, 1, f_copy},
576#ifdef FEAT_FLOAT
577 {"cos", 1, 1, f_cos},
578 {"cosh", 1, 1, f_cosh},
579#endif
580 {"count", 2, 4, f_count},
581 {"cscope_connection",0,3, f_cscope_connection},
582 {"cursor", 1, 3, f_cursor},
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200583#ifdef WIN3264
584 {"debugbreak", 1, 1, f_debugbreak},
585#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200586 {"deepcopy", 1, 2, f_deepcopy},
587 {"delete", 1, 2, f_delete},
Bram Moolenaard79a2622018-06-07 18:17:46 +0200588 {"deletebufline", 2, 3, f_deletebufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200589 {"did_filetype", 0, 0, f_did_filetype},
590 {"diff_filler", 1, 1, f_diff_filler},
591 {"diff_hlID", 2, 2, f_diff_hlID},
592 {"empty", 1, 1, f_empty},
593 {"escape", 2, 2, f_escape},
594 {"eval", 1, 1, f_eval},
595 {"eventhandler", 0, 0, f_eventhandler},
596 {"executable", 1, 1, f_executable},
597 {"execute", 1, 2, f_execute},
598 {"exepath", 1, 1, f_exepath},
599 {"exists", 1, 1, f_exists},
600#ifdef FEAT_FLOAT
601 {"exp", 1, 1, f_exp},
602#endif
603 {"expand", 1, 3, f_expand},
604 {"extend", 2, 3, f_extend},
605 {"feedkeys", 1, 2, f_feedkeys},
606 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
607 {"filereadable", 1, 1, f_filereadable},
608 {"filewritable", 1, 1, f_filewritable},
609 {"filter", 2, 2, f_filter},
610 {"finddir", 1, 3, f_finddir},
611 {"findfile", 1, 3, f_findfile},
612#ifdef FEAT_FLOAT
613 {"float2nr", 1, 1, f_float2nr},
614 {"floor", 1, 1, f_floor},
615 {"fmod", 2, 2, f_fmod},
616#endif
617 {"fnameescape", 1, 1, f_fnameescape},
618 {"fnamemodify", 2, 2, f_fnamemodify},
619 {"foldclosed", 1, 1, f_foldclosed},
620 {"foldclosedend", 1, 1, f_foldclosedend},
621 {"foldlevel", 1, 1, f_foldlevel},
622 {"foldtext", 0, 0, f_foldtext},
623 {"foldtextresult", 1, 1, f_foldtextresult},
624 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200625 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200626 {"function", 1, 3, f_function},
627 {"garbagecollect", 0, 1, f_garbagecollect},
628 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200629 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200630 {"getbufline", 2, 3, f_getbufline},
631 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100632 {"getchangelist", 1, 1, f_getchangelist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200633 {"getchar", 0, 1, f_getchar},
634 {"getcharmod", 0, 0, f_getcharmod},
635 {"getcharsearch", 0, 0, f_getcharsearch},
636 {"getcmdline", 0, 0, f_getcmdline},
637 {"getcmdpos", 0, 0, f_getcmdpos},
638 {"getcmdtype", 0, 0, f_getcmdtype},
639 {"getcmdwintype", 0, 0, f_getcmdwintype},
640#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200641 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200642#endif
643 {"getcurpos", 0, 0, f_getcurpos},
644 {"getcwd", 0, 2, f_getcwd},
645 {"getfontname", 0, 1, f_getfontname},
646 {"getfperm", 1, 1, f_getfperm},
647 {"getfsize", 1, 1, f_getfsize},
648 {"getftime", 1, 1, f_getftime},
649 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100650 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200651 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200652 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200653 {"getmatches", 0, 0, f_getmatches},
654 {"getpid", 0, 0, f_getpid},
655 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200656 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200657 {"getreg", 0, 3, f_getreg},
658 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200659 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200660 {"gettabvar", 2, 3, f_gettabvar},
661 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200662 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100663 {"getwinpos", 0, 1, f_getwinpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200664 {"getwinposx", 0, 0, f_getwinposx},
665 {"getwinposy", 0, 0, f_getwinposy},
666 {"getwinvar", 2, 3, f_getwinvar},
667 {"glob", 1, 4, f_glob},
668 {"glob2regpat", 1, 1, f_glob2regpat},
669 {"globpath", 2, 5, f_globpath},
670 {"has", 1, 1, f_has},
671 {"has_key", 2, 2, f_has_key},
672 {"haslocaldir", 0, 2, f_haslocaldir},
673 {"hasmapto", 1, 3, f_hasmapto},
674 {"highlightID", 1, 1, f_hlID}, /* obsolete */
675 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
676 {"histadd", 2, 2, f_histadd},
677 {"histdel", 1, 2, f_histdel},
678 {"histget", 1, 2, f_histget},
679 {"histnr", 1, 1, f_histnr},
680 {"hlID", 1, 1, f_hlID},
681 {"hlexists", 1, 1, f_hlexists},
682 {"hostname", 0, 0, f_hostname},
683 {"iconv", 3, 3, f_iconv},
684 {"indent", 1, 1, f_indent},
685 {"index", 2, 4, f_index},
686 {"input", 1, 3, f_input},
687 {"inputdialog", 1, 3, f_inputdialog},
688 {"inputlist", 1, 1, f_inputlist},
689 {"inputrestore", 0, 0, f_inputrestore},
690 {"inputsave", 0, 0, f_inputsave},
691 {"inputsecret", 1, 2, f_inputsecret},
692 {"insert", 2, 3, f_insert},
693 {"invert", 1, 1, f_invert},
694 {"isdirectory", 1, 1, f_isdirectory},
695 {"islocked", 1, 1, f_islocked},
696#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
697 {"isnan", 1, 1, f_isnan},
698#endif
699 {"items", 1, 1, f_items},
700#ifdef FEAT_JOB_CHANNEL
701 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200702 {"job_info", 0, 1, f_job_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200703 {"job_setoptions", 2, 2, f_job_setoptions},
704 {"job_start", 1, 2, f_job_start},
705 {"job_status", 1, 1, f_job_status},
706 {"job_stop", 1, 2, f_job_stop},
707#endif
708 {"join", 1, 2, f_join},
709 {"js_decode", 1, 1, f_js_decode},
710 {"js_encode", 1, 1, f_js_encode},
711 {"json_decode", 1, 1, f_json_decode},
712 {"json_encode", 1, 1, f_json_encode},
713 {"keys", 1, 1, f_keys},
714 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
715 {"len", 1, 1, f_len},
716 {"libcall", 3, 3, f_libcall},
717 {"libcallnr", 3, 3, f_libcallnr},
718 {"line", 1, 1, f_line},
719 {"line2byte", 1, 1, f_line2byte},
720 {"lispindent", 1, 1, f_lispindent},
721 {"localtime", 0, 0, f_localtime},
722#ifdef FEAT_FLOAT
723 {"log", 1, 1, f_log},
724 {"log10", 1, 1, f_log10},
725#endif
726#ifdef FEAT_LUA
727 {"luaeval", 1, 2, f_luaeval},
728#endif
729 {"map", 2, 2, f_map},
730 {"maparg", 1, 4, f_maparg},
731 {"mapcheck", 1, 3, f_mapcheck},
732 {"match", 2, 4, f_match},
733 {"matchadd", 2, 5, f_matchadd},
734 {"matchaddpos", 2, 5, f_matchaddpos},
735 {"matcharg", 1, 1, f_matcharg},
736 {"matchdelete", 1, 1, f_matchdelete},
737 {"matchend", 2, 4, f_matchend},
738 {"matchlist", 2, 4, f_matchlist},
739 {"matchstr", 2, 4, f_matchstr},
740 {"matchstrpos", 2, 4, f_matchstrpos},
741 {"max", 1, 1, f_max},
742 {"min", 1, 1, f_min},
743#ifdef vim_mkdir
744 {"mkdir", 1, 3, f_mkdir},
745#endif
746 {"mode", 0, 1, f_mode},
747#ifdef FEAT_MZSCHEME
748 {"mzeval", 1, 1, f_mzeval},
749#endif
750 {"nextnonblank", 1, 1, f_nextnonblank},
751 {"nr2char", 1, 2, f_nr2char},
752 {"or", 2, 2, f_or},
753 {"pathshorten", 1, 1, f_pathshorten},
754#ifdef FEAT_PERL
755 {"perleval", 1, 1, f_perleval},
756#endif
757#ifdef FEAT_FLOAT
758 {"pow", 2, 2, f_pow},
759#endif
760 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100761 {"printf", 1, 19, f_printf},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200762#ifdef FEAT_JOB_CHANNEL
763 {"prompt_setcallback", 2, 2, f_prompt_setcallback},
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200764 {"prompt_setinterrupt", 2, 2, f_prompt_setinterrupt},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200765 {"prompt_setprompt", 2, 2, f_prompt_setprompt},
766#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200767 {"pumvisible", 0, 0, f_pumvisible},
768#ifdef FEAT_PYTHON3
769 {"py3eval", 1, 1, f_py3eval},
770#endif
771#ifdef FEAT_PYTHON
772 {"pyeval", 1, 1, f_pyeval},
773#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100774#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
775 {"pyxeval", 1, 1, f_pyxeval},
776#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200777 {"range", 1, 3, f_range},
778 {"readfile", 1, 3, f_readfile},
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200779 {"reg_executing", 0, 0, f_reg_executing},
780 {"reg_recording", 0, 0, f_reg_recording},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200781 {"reltime", 0, 2, f_reltime},
782#ifdef FEAT_FLOAT
783 {"reltimefloat", 1, 1, f_reltimefloat},
784#endif
785 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100786 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200787 {"remote_foreground", 1, 1, f_remote_foreground},
788 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100789 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200790 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100791 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200792 {"remove", 2, 3, f_remove},
793 {"rename", 2, 2, f_rename},
794 {"repeat", 2, 2, f_repeat},
795 {"resolve", 1, 1, f_resolve},
796 {"reverse", 1, 1, f_reverse},
797#ifdef FEAT_FLOAT
798 {"round", 1, 1, f_round},
799#endif
800 {"screenattr", 2, 2, f_screenattr},
801 {"screenchar", 2, 2, f_screenchar},
802 {"screencol", 0, 0, f_screencol},
803 {"screenrow", 0, 0, f_screenrow},
804 {"search", 1, 4, f_search},
805 {"searchdecl", 1, 3, f_searchdecl},
806 {"searchpair", 3, 7, f_searchpair},
807 {"searchpairpos", 3, 7, f_searchpairpos},
808 {"searchpos", 1, 4, f_searchpos},
809 {"server2client", 2, 2, f_server2client},
810 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200811 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200812 {"setbufvar", 3, 3, f_setbufvar},
813 {"setcharsearch", 1, 1, f_setcharsearch},
814 {"setcmdpos", 1, 1, f_setcmdpos},
815 {"setfperm", 2, 2, f_setfperm},
816 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200817 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200818 {"setmatches", 1, 1, f_setmatches},
819 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200820 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200821 {"setreg", 2, 3, f_setreg},
822 {"settabvar", 3, 3, f_settabvar},
823 {"settabwinvar", 4, 4, f_settabwinvar},
824 {"setwinvar", 3, 3, f_setwinvar},
825#ifdef FEAT_CRYPT
826 {"sha256", 1, 1, f_sha256},
827#endif
828 {"shellescape", 1, 2, f_shellescape},
829 {"shiftwidth", 0, 0, f_shiftwidth},
830 {"simplify", 1, 1, f_simplify},
831#ifdef FEAT_FLOAT
832 {"sin", 1, 1, f_sin},
833 {"sinh", 1, 1, f_sinh},
834#endif
835 {"sort", 1, 3, f_sort},
836 {"soundfold", 1, 1, f_soundfold},
837 {"spellbadword", 0, 1, f_spellbadword},
838 {"spellsuggest", 1, 3, f_spellsuggest},
839 {"split", 1, 3, f_split},
840#ifdef FEAT_FLOAT
841 {"sqrt", 1, 1, f_sqrt},
842 {"str2float", 1, 1, f_str2float},
843#endif
844 {"str2nr", 1, 2, f_str2nr},
845 {"strcharpart", 2, 3, f_strcharpart},
846 {"strchars", 1, 2, f_strchars},
847 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
848#ifdef HAVE_STRFTIME
849 {"strftime", 1, 2, f_strftime},
850#endif
851 {"strgetchar", 2, 2, f_strgetchar},
852 {"stridx", 2, 3, f_stridx},
853 {"string", 1, 1, f_string},
854 {"strlen", 1, 1, f_strlen},
855 {"strpart", 2, 3, f_strpart},
856 {"strridx", 2, 3, f_strridx},
857 {"strtrans", 1, 1, f_strtrans},
858 {"strwidth", 1, 1, f_strwidth},
859 {"submatch", 1, 2, f_submatch},
860 {"substitute", 4, 4, f_substitute},
861 {"synID", 3, 3, f_synID},
862 {"synIDattr", 2, 3, f_synIDattr},
863 {"synIDtrans", 1, 1, f_synIDtrans},
864 {"synconcealed", 2, 2, f_synconcealed},
865 {"synstack", 2, 2, f_synstack},
866 {"system", 1, 2, f_system},
867 {"systemlist", 1, 2, f_systemlist},
868 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
869 {"tabpagenr", 0, 1, f_tabpagenr},
870 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
871 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100872 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200873#ifdef FEAT_FLOAT
874 {"tan", 1, 1, f_tan},
875 {"tanh", 1, 1, f_tanh},
876#endif
877 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200878#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100879 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
880 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100881 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200882 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200883# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
884 {"term_getansicolors", 1, 1, f_term_getansicolors},
885# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200886 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200887 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200888 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200889 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200890 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200891 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200892 {"term_getstatus", 1, 1, f_term_getstatus},
893 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200894 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200895 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200896 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200897 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200898# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
899 {"term_setansicolors", 2, 2, f_term_setansicolors},
900# endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100901 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100902 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaara42d3632018-04-14 17:05:38 +0200903 {"term_setsize", 3, 3, f_term_setsize},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200904 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200905 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200906#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200907 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
908 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200909 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200910 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100911 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200912#ifdef FEAT_JOB_CHANNEL
913 {"test_null_channel", 0, 0, f_test_null_channel},
914#endif
915 {"test_null_dict", 0, 0, f_test_null_dict},
916#ifdef FEAT_JOB_CHANNEL
917 {"test_null_job", 0, 0, f_test_null_job},
918#endif
919 {"test_null_list", 0, 0, f_test_null_list},
920 {"test_null_partial", 0, 0, f_test_null_partial},
921 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100922 {"test_override", 2, 2, f_test_override},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200923 {"test_settime", 1, 1, f_test_settime},
924#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200925 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200926 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200927 {"timer_start", 2, 3, f_timer_start},
928 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200929 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200930#endif
931 {"tolower", 1, 1, f_tolower},
932 {"toupper", 1, 1, f_toupper},
933 {"tr", 3, 3, f_tr},
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100934 {"trim", 1, 2, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200935#ifdef FEAT_FLOAT
936 {"trunc", 1, 1, f_trunc},
937#endif
938 {"type", 1, 1, f_type},
939 {"undofile", 1, 1, f_undofile},
940 {"undotree", 0, 0, f_undotree},
941 {"uniq", 1, 3, f_uniq},
942 {"values", 1, 1, f_values},
943 {"virtcol", 1, 1, f_virtcol},
944 {"visualmode", 0, 1, f_visualmode},
945 {"wildmenumode", 0, 0, f_wildmenumode},
946 {"win_findbuf", 1, 1, f_win_findbuf},
947 {"win_getid", 0, 2, f_win_getid},
948 {"win_gotoid", 1, 1, f_win_gotoid},
949 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
950 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100951 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200952 {"winbufnr", 1, 1, f_winbufnr},
953 {"wincol", 0, 0, f_wincol},
954 {"winheight", 1, 1, f_winheight},
955 {"winline", 0, 0, f_winline},
956 {"winnr", 0, 1, f_winnr},
957 {"winrestcmd", 0, 0, f_winrestcmd},
958 {"winrestview", 1, 1, f_winrestview},
959 {"winsaveview", 0, 0, f_winsaveview},
960 {"winwidth", 1, 1, f_winwidth},
961 {"wordcount", 0, 0, f_wordcount},
962 {"writefile", 2, 3, f_writefile},
963 {"xor", 2, 2, f_xor},
964};
965
966#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
967
968/*
969 * Function given to ExpandGeneric() to obtain the list of internal
970 * or user defined function names.
971 */
972 char_u *
973get_function_name(expand_T *xp, int idx)
974{
975 static int intidx = -1;
976 char_u *name;
977
978 if (idx == 0)
979 intidx = -1;
980 if (intidx < 0)
981 {
982 name = get_user_func_name(xp, idx);
983 if (name != NULL)
984 return name;
985 }
986 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
987 {
988 STRCPY(IObuff, functions[intidx].f_name);
989 STRCAT(IObuff, "(");
990 if (functions[intidx].f_max_argc == 0)
991 STRCAT(IObuff, ")");
992 return IObuff;
993 }
994
995 return NULL;
996}
997
998/*
999 * Function given to ExpandGeneric() to obtain the list of internal or
1000 * user defined variable or function names.
1001 */
1002 char_u *
1003get_expr_name(expand_T *xp, int idx)
1004{
1005 static int intidx = -1;
1006 char_u *name;
1007
1008 if (idx == 0)
1009 intidx = -1;
1010 if (intidx < 0)
1011 {
1012 name = get_function_name(xp, idx);
1013 if (name != NULL)
1014 return name;
1015 }
1016 return get_user_var_name(xp, ++intidx);
1017}
1018
1019#endif /* FEAT_CMDL_COMPL */
1020
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001021/*
1022 * Find internal function in table above.
1023 * Return index, or -1 if not found
1024 */
1025 int
1026find_internal_func(
1027 char_u *name) /* name of the function */
1028{
1029 int first = 0;
1030 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
1031 int cmp;
1032 int x;
1033
1034 /*
1035 * Find the function name in the table. Binary search.
1036 */
1037 while (first <= last)
1038 {
1039 x = first + ((unsigned)(last - first) >> 1);
1040 cmp = STRCMP(name, functions[x].f_name);
1041 if (cmp < 0)
1042 last = x - 1;
1043 else if (cmp > 0)
1044 first = x + 1;
1045 else
1046 return x;
1047 }
1048 return -1;
1049}
1050
1051 int
1052call_internal_func(
1053 char_u *name,
1054 int argcount,
1055 typval_T *argvars,
1056 typval_T *rettv)
1057{
1058 int i;
1059
1060 i = find_internal_func(name);
1061 if (i < 0)
1062 return ERROR_UNKNOWN;
1063 if (argcount < functions[i].f_min_argc)
1064 return ERROR_TOOFEW;
1065 if (argcount > functions[i].f_max_argc)
1066 return ERROR_TOOMANY;
1067 argvars[argcount].v_type = VAR_UNKNOWN;
1068 functions[i].f_func(argvars, rettv);
1069 return ERROR_NONE;
1070}
1071
1072/*
1073 * Return TRUE for a non-zero Number and a non-empty String.
1074 */
1075 static int
1076non_zero_arg(typval_T *argvars)
1077{
1078 return ((argvars[0].v_type == VAR_NUMBER
1079 && argvars[0].vval.v_number != 0)
1080 || (argvars[0].v_type == VAR_SPECIAL
1081 && argvars[0].vval.v_number == VVAL_TRUE)
1082 || (argvars[0].v_type == VAR_STRING
1083 && argvars[0].vval.v_string != NULL
1084 && *argvars[0].vval.v_string != NUL));
1085}
1086
1087/*
1088 * Get the lnum from the first argument.
1089 * Also accepts ".", "$", etc., but that only works for the current buffer.
1090 * Returns -1 on error.
1091 */
1092 static linenr_T
1093get_tv_lnum(typval_T *argvars)
1094{
1095 typval_T rettv;
1096 linenr_T lnum;
1097
1098 lnum = (linenr_T)get_tv_number_chk(&argvars[0], NULL);
1099 if (lnum == 0) /* no valid number, try using line() */
1100 {
1101 rettv.v_type = VAR_NUMBER;
1102 f_line(argvars, &rettv);
1103 lnum = (linenr_T)rettv.vval.v_number;
1104 clear_tv(&rettv);
1105 }
1106 return lnum;
1107}
1108
1109#ifdef FEAT_FLOAT
1110static int get_float_arg(typval_T *argvars, float_T *f);
1111
1112/*
1113 * Get the float value of "argvars[0]" into "f".
1114 * Returns FAIL when the argument is not a Number or Float.
1115 */
1116 static int
1117get_float_arg(typval_T *argvars, float_T *f)
1118{
1119 if (argvars[0].v_type == VAR_FLOAT)
1120 {
1121 *f = argvars[0].vval.v_float;
1122 return OK;
1123 }
1124 if (argvars[0].v_type == VAR_NUMBER)
1125 {
1126 *f = (float_T)argvars[0].vval.v_number;
1127 return OK;
1128 }
1129 EMSG(_("E808: Number or Float required"));
1130 return FAIL;
1131}
1132
1133/*
1134 * "abs(expr)" function
1135 */
1136 static void
1137f_abs(typval_T *argvars, typval_T *rettv)
1138{
1139 if (argvars[0].v_type == VAR_FLOAT)
1140 {
1141 rettv->v_type = VAR_FLOAT;
1142 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1143 }
1144 else
1145 {
1146 varnumber_T n;
1147 int error = FALSE;
1148
1149 n = get_tv_number_chk(&argvars[0], &error);
1150 if (error)
1151 rettv->vval.v_number = -1;
1152 else if (n > 0)
1153 rettv->vval.v_number = n;
1154 else
1155 rettv->vval.v_number = -n;
1156 }
1157}
1158
1159/*
1160 * "acos()" function
1161 */
1162 static void
1163f_acos(typval_T *argvars, typval_T *rettv)
1164{
1165 float_T f = 0.0;
1166
1167 rettv->v_type = VAR_FLOAT;
1168 if (get_float_arg(argvars, &f) == OK)
1169 rettv->vval.v_float = acos(f);
1170 else
1171 rettv->vval.v_float = 0.0;
1172}
1173#endif
1174
1175/*
1176 * "add(list, item)" function
1177 */
1178 static void
1179f_add(typval_T *argvars, typval_T *rettv)
1180{
1181 list_T *l;
1182
1183 rettv->vval.v_number = 1; /* Default: Failed */
1184 if (argvars[0].v_type == VAR_LIST)
1185 {
1186 if ((l = argvars[0].vval.v_list) != NULL
1187 && !tv_check_lock(l->lv_lock,
1188 (char_u *)N_("add() argument"), TRUE)
1189 && list_append_tv(l, &argvars[1]) == OK)
1190 copy_tv(&argvars[0], rettv);
1191 }
1192 else
1193 EMSG(_(e_listreq));
1194}
1195
1196/*
1197 * "and(expr, expr)" function
1198 */
1199 static void
1200f_and(typval_T *argvars, typval_T *rettv)
1201{
1202 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
1203 & get_tv_number_chk(&argvars[1], NULL);
1204}
1205
1206/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001207 * Get the lnum from the first argument.
1208 * Also accepts "$", then "buf" is used.
1209 * Returns 0 on error.
1210 */
1211 static linenr_T
1212get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
1213{
1214 if (argvars[0].v_type == VAR_STRING
1215 && argvars[0].vval.v_string != NULL
1216 && argvars[0].vval.v_string[0] == '$'
1217 && buf != NULL)
1218 return buf->b_ml.ml_line_count;
1219 return (linenr_T)get_tv_number_chk(&argvars[0], NULL);
1220}
1221
1222/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02001223 * If there is a window for "curbuf", make it the current window.
1224 */
1225 static void
1226find_win_for_curbuf(void)
1227{
1228 wininfo_T *wip;
1229
1230 for (wip = curbuf->b_wininfo; wip != NULL; wip = wip->wi_next)
1231 {
1232 if (wip->wi_win != NULL)
1233 {
1234 curwin = wip->wi_win;
1235 break;
1236 }
1237 }
1238}
1239
1240/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001241 * Set line or list of lines in buffer "buf".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001242 */
1243 static void
Bram Moolenaarca851592018-06-06 21:04:07 +02001244set_buffer_lines(
1245 buf_T *buf,
1246 linenr_T lnum_arg,
1247 int append,
1248 typval_T *lines,
1249 typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001250{
Bram Moolenaarca851592018-06-06 21:04:07 +02001251 linenr_T lnum = lnum_arg + (append ? 1 : 0);
1252 char_u *line = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001253 list_T *l = NULL;
1254 listitem_T *li = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001255 long added = 0;
Bram Moolenaarca851592018-06-06 21:04:07 +02001256 linenr_T append_lnum;
1257 buf_T *curbuf_save = NULL;
1258 win_T *curwin_save = NULL;
1259 int is_curbuf = buf == curbuf;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001260
Bram Moolenaarca851592018-06-06 21:04:07 +02001261 /* When using the current buffer ml_mfp will be set if needed. Useful when
1262 * setline() is used on startup. For other buffers the buffer must be
1263 * loaded. */
1264 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001265 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001266 rettv->vval.v_number = 1; /* FAIL */
1267 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001268 }
1269
Bram Moolenaarca851592018-06-06 21:04:07 +02001270 if (!is_curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001271 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001272 curbuf_save = curbuf;
1273 curwin_save = curwin;
1274 curbuf = buf;
Bram Moolenaard79a2622018-06-07 18:17:46 +02001275 find_win_for_curbuf();
Bram Moolenaarca851592018-06-06 21:04:07 +02001276 }
1277
1278 if (append)
1279 // appendbufline() uses the line number below which we insert
1280 append_lnum = lnum - 1;
1281 else
1282 // setbufline() uses the line number above which we insert, we only
1283 // append if it's below the last line
1284 append_lnum = curbuf->b_ml.ml_line_count;
1285
1286 if (lines->v_type == VAR_LIST)
1287 {
1288 l = lines->vval.v_list;
1289 li = l->lv_first;
1290 }
1291 else
1292 line = get_tv_string_chk(lines);
1293
1294 /* default result is zero == OK */
1295 for (;;)
1296 {
1297 if (l != NULL)
1298 {
1299 /* list argument, get next string */
1300 if (li == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001301 break;
Bram Moolenaarca851592018-06-06 21:04:07 +02001302 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001303 li = li->li_next;
1304 }
1305
Bram Moolenaarca851592018-06-06 21:04:07 +02001306 rettv->vval.v_number = 1; /* FAIL */
1307 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
1308 break;
1309
1310 /* When coming here from Insert mode, sync undo, so that this can be
1311 * undone separately from what was previously inserted. */
1312 if (u_sync_once == 2)
1313 {
1314 u_sync_once = 1; /* notify that u_sync() was called */
1315 u_sync(TRUE);
1316 }
1317
1318 if (!append && lnum <= curbuf->b_ml.ml_line_count)
1319 {
1320 /* existing line, replace it */
1321 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
1322 {
1323 changed_bytes(lnum, 0);
1324 if (is_curbuf && lnum == curwin->w_cursor.lnum)
1325 check_cursor_col();
1326 rettv->vval.v_number = 0; /* OK */
1327 }
1328 }
1329 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
1330 {
1331 /* append the line */
1332 ++added;
1333 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
1334 rettv->vval.v_number = 0; /* OK */
1335 }
1336
1337 if (l == NULL) /* only one string argument */
1338 break;
1339 ++lnum;
1340 }
1341
1342 if (added > 0)
1343 {
1344 win_T *wp;
1345 tabpage_T *tp;
1346
1347 appended_lines_mark(append_lnum, added);
1348 FOR_ALL_TAB_WINDOWS(tp, wp)
1349 if (wp->w_buffer == buf && wp->w_cursor.lnum > append_lnum)
1350 wp->w_cursor.lnum += added;
1351 check_cursor_col();
1352
Bram Moolenaarf2732452018-06-03 14:47:35 +02001353#ifdef FEAT_JOB_CHANNEL
1354 if (bt_prompt(curbuf) && (State & INSERT))
1355 // show the line with the prompt
1356 update_topline();
1357#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001358 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001359
1360 if (!is_curbuf)
1361 {
1362 curbuf = curbuf_save;
1363 curwin = curwin_save;
1364 }
1365}
1366
1367/*
1368 * "append(lnum, string/list)" function
1369 */
1370 static void
1371f_append(typval_T *argvars, typval_T *rettv)
1372{
1373 linenr_T lnum = get_tv_lnum(&argvars[0]);
1374
1375 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1376}
1377
1378/*
1379 * "appendbufline(buf, lnum, string/list)" function
1380 */
1381 static void
1382f_appendbufline(typval_T *argvars, typval_T *rettv)
1383{
1384 linenr_T lnum;
1385 buf_T *buf;
1386
1387 buf = get_buf_tv(&argvars[0], FALSE);
1388 if (buf == NULL)
1389 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001390 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001391 {
1392 lnum = get_tv_lnum_buf(&argvars[1], buf);
1393 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1394 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001395}
1396
1397/*
1398 * "argc()" function
1399 */
1400 static void
1401f_argc(typval_T *argvars UNUSED, typval_T *rettv)
1402{
1403 rettv->vval.v_number = ARGCOUNT;
1404}
1405
1406/*
1407 * "argidx()" function
1408 */
1409 static void
1410f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1411{
1412 rettv->vval.v_number = curwin->w_arg_idx;
1413}
1414
1415/*
1416 * "arglistid()" function
1417 */
1418 static void
1419f_arglistid(typval_T *argvars, typval_T *rettv)
1420{
1421 win_T *wp;
1422
1423 rettv->vval.v_number = -1;
1424 wp = find_tabwin(&argvars[0], &argvars[1]);
1425 if (wp != NULL)
1426 rettv->vval.v_number = wp->w_alist->id;
1427}
1428
1429/*
1430 * "argv(nr)" function
1431 */
1432 static void
1433f_argv(typval_T *argvars, typval_T *rettv)
1434{
1435 int idx;
1436
1437 if (argvars[0].v_type != VAR_UNKNOWN)
1438 {
1439 idx = (int)get_tv_number_chk(&argvars[0], NULL);
1440 if (idx >= 0 && idx < ARGCOUNT)
1441 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
1442 else
1443 rettv->vval.v_string = NULL;
1444 rettv->v_type = VAR_STRING;
1445 }
1446 else if (rettv_list_alloc(rettv) == OK)
1447 for (idx = 0; idx < ARGCOUNT; ++idx)
1448 list_append_string(rettv->vval.v_list,
1449 alist_name(&ARGLIST[idx]), -1);
1450}
1451
1452/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001453 * "assert_beeps(cmd [, error])" function
1454 */
1455 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001456f_assert_beeps(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001457{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001458 rettv->vval.v_number = assert_beeps(argvars);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001459}
1460
1461/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001462 * "assert_equal(expected, actual[, msg])" function
1463 */
1464 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001465f_assert_equal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001466{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001467 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_EQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001468}
1469
1470/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01001471 * "assert_equalfile(fname-one, fname-two)" function
1472 */
1473 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001474f_assert_equalfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaard96ff162018-02-18 22:13:29 +01001475{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001476 rettv->vval.v_number = assert_equalfile(argvars);
Bram Moolenaard96ff162018-02-18 22:13:29 +01001477}
1478
1479/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001480 * "assert_notequal(expected, actual[, msg])" function
1481 */
1482 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001483f_assert_notequal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001484{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001485 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001486}
1487
1488/*
1489 * "assert_exception(string[, msg])" function
1490 */
1491 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001492f_assert_exception(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001493{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001494 rettv->vval.v_number = assert_exception(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001495}
1496
1497/*
1498 * "assert_fails(cmd [, error])" function
1499 */
1500 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001501f_assert_fails(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001502{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001503 rettv->vval.v_number = assert_fails(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001504}
1505
1506/*
1507 * "assert_false(actual[, msg])" function
1508 */
1509 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001510f_assert_false(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001511{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001512 rettv->vval.v_number = assert_bool(argvars, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001513}
1514
1515/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001516 * "assert_inrange(lower, upper[, msg])" function
1517 */
1518 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001519f_assert_inrange(typval_T *argvars, typval_T *rettv)
Bram Moolenaar61c04492016-07-23 15:35:35 +02001520{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001521 rettv->vval.v_number = assert_inrange(argvars);
Bram Moolenaar61c04492016-07-23 15:35:35 +02001522}
1523
1524/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001525 * "assert_match(pattern, actual[, msg])" function
1526 */
1527 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001528f_assert_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001529{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001530 rettv->vval.v_number = assert_match_common(argvars, ASSERT_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001531}
1532
1533/*
1534 * "assert_notmatch(pattern, actual[, msg])" function
1535 */
1536 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001537f_assert_notmatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001538{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001539 rettv->vval.v_number = assert_match_common(argvars, ASSERT_NOTMATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001540}
1541
1542/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001543 * "assert_report(msg)" function
1544 */
1545 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001546f_assert_report(typval_T *argvars, typval_T *rettv)
Bram Moolenaar42205552017-03-18 19:42:22 +01001547{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001548 rettv->vval.v_number = assert_report(argvars);
Bram Moolenaar42205552017-03-18 19:42:22 +01001549}
1550
1551/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001552 * "assert_true(actual[, msg])" function
1553 */
1554 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001555f_assert_true(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001556{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001557 rettv->vval.v_number = assert_bool(argvars, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001558}
1559
1560#ifdef FEAT_FLOAT
1561/*
1562 * "asin()" function
1563 */
1564 static void
1565f_asin(typval_T *argvars, typval_T *rettv)
1566{
1567 float_T f = 0.0;
1568
1569 rettv->v_type = VAR_FLOAT;
1570 if (get_float_arg(argvars, &f) == OK)
1571 rettv->vval.v_float = asin(f);
1572 else
1573 rettv->vval.v_float = 0.0;
1574}
1575
1576/*
1577 * "atan()" function
1578 */
1579 static void
1580f_atan(typval_T *argvars, typval_T *rettv)
1581{
1582 float_T f = 0.0;
1583
1584 rettv->v_type = VAR_FLOAT;
1585 if (get_float_arg(argvars, &f) == OK)
1586 rettv->vval.v_float = atan(f);
1587 else
1588 rettv->vval.v_float = 0.0;
1589}
1590
1591/*
1592 * "atan2()" function
1593 */
1594 static void
1595f_atan2(typval_T *argvars, typval_T *rettv)
1596{
1597 float_T fx = 0.0, fy = 0.0;
1598
1599 rettv->v_type = VAR_FLOAT;
1600 if (get_float_arg(argvars, &fx) == OK
1601 && get_float_arg(&argvars[1], &fy) == OK)
1602 rettv->vval.v_float = atan2(fx, fy);
1603 else
1604 rettv->vval.v_float = 0.0;
1605}
1606#endif
1607
1608/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001609 * "balloon_show()" function
1610 */
1611#ifdef FEAT_BEVAL
1612 static void
1613f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1614{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001615 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001616 {
1617 if (argvars[0].v_type == VAR_LIST
1618# ifdef FEAT_GUI
1619 && !gui.in_use
1620# endif
1621 )
1622 post_balloon(balloonEval, NULL, argvars[0].vval.v_list);
1623 else
1624 post_balloon(balloonEval, get_tv_string_chk(&argvars[0]), NULL);
1625 }
1626}
1627
Bram Moolenaar669a8282017-11-19 20:13:05 +01001628# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001629 static void
1630f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1631{
1632 if (rettv_list_alloc(rettv) == OK)
1633 {
1634 char_u *msg = get_tv_string_chk(&argvars[0]);
1635
1636 if (msg != NULL)
1637 {
1638 pumitem_T *array;
1639 int size = split_message(msg, &array);
1640 int i;
1641
1642 /* Skip the first and last item, they are always empty. */
1643 for (i = 1; i < size - 1; ++i)
1644 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001645 while (size > 0)
1646 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001647 vim_free(array);
1648 }
1649 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001650}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001651# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001652#endif
1653
1654/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001655 * "browse(save, title, initdir, default)" function
1656 */
1657 static void
1658f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1659{
1660#ifdef FEAT_BROWSE
1661 int save;
1662 char_u *title;
1663 char_u *initdir;
1664 char_u *defname;
1665 char_u buf[NUMBUFLEN];
1666 char_u buf2[NUMBUFLEN];
1667 int error = FALSE;
1668
1669 save = (int)get_tv_number_chk(&argvars[0], &error);
1670 title = get_tv_string_chk(&argvars[1]);
1671 initdir = get_tv_string_buf_chk(&argvars[2], buf);
1672 defname = get_tv_string_buf_chk(&argvars[3], buf2);
1673
1674 if (error || title == NULL || initdir == NULL || defname == NULL)
1675 rettv->vval.v_string = NULL;
1676 else
1677 rettv->vval.v_string =
1678 do_browse(save ? BROWSE_SAVE : 0,
1679 title, defname, NULL, initdir, NULL, curbuf);
1680#else
1681 rettv->vval.v_string = NULL;
1682#endif
1683 rettv->v_type = VAR_STRING;
1684}
1685
1686/*
1687 * "browsedir(title, initdir)" function
1688 */
1689 static void
1690f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1691{
1692#ifdef FEAT_BROWSE
1693 char_u *title;
1694 char_u *initdir;
1695 char_u buf[NUMBUFLEN];
1696
1697 title = get_tv_string_chk(&argvars[0]);
1698 initdir = get_tv_string_buf_chk(&argvars[1], buf);
1699
1700 if (title == NULL || initdir == NULL)
1701 rettv->vval.v_string = NULL;
1702 else
1703 rettv->vval.v_string = do_browse(BROWSE_DIR,
1704 title, NULL, NULL, initdir, NULL, curbuf);
1705#else
1706 rettv->vval.v_string = NULL;
1707#endif
1708 rettv->v_type = VAR_STRING;
1709}
1710
1711static buf_T *find_buffer(typval_T *avar);
1712
1713/*
1714 * Find a buffer by number or exact name.
1715 */
1716 static buf_T *
1717find_buffer(typval_T *avar)
1718{
1719 buf_T *buf = NULL;
1720
1721 if (avar->v_type == VAR_NUMBER)
1722 buf = buflist_findnr((int)avar->vval.v_number);
1723 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1724 {
1725 buf = buflist_findname_exp(avar->vval.v_string);
1726 if (buf == NULL)
1727 {
1728 /* No full path name match, try a match with a URL or a "nofile"
1729 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001730 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001731 if (buf->b_fname != NULL
1732 && (path_with_url(buf->b_fname)
1733#ifdef FEAT_QUICKFIX
1734 || bt_nofile(buf)
1735#endif
1736 )
1737 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1738 break;
1739 }
1740 }
1741 return buf;
1742}
1743
1744/*
1745 * "bufexists(expr)" function
1746 */
1747 static void
1748f_bufexists(typval_T *argvars, typval_T *rettv)
1749{
1750 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1751}
1752
1753/*
1754 * "buflisted(expr)" function
1755 */
1756 static void
1757f_buflisted(typval_T *argvars, typval_T *rettv)
1758{
1759 buf_T *buf;
1760
1761 buf = find_buffer(&argvars[0]);
1762 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1763}
1764
1765/*
1766 * "bufloaded(expr)" function
1767 */
1768 static void
1769f_bufloaded(typval_T *argvars, typval_T *rettv)
1770{
1771 buf_T *buf;
1772
1773 buf = find_buffer(&argvars[0]);
1774 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1775}
1776
1777 buf_T *
1778buflist_find_by_name(char_u *name, int curtab_only)
1779{
1780 int save_magic;
1781 char_u *save_cpo;
1782 buf_T *buf;
1783
1784 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1785 save_magic = p_magic;
1786 p_magic = TRUE;
1787 save_cpo = p_cpo;
1788 p_cpo = (char_u *)"";
1789
1790 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1791 TRUE, FALSE, curtab_only));
1792
1793 p_magic = save_magic;
1794 p_cpo = save_cpo;
1795 return buf;
1796}
1797
1798/*
1799 * Get buffer by number or pattern.
1800 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001801 buf_T *
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001802get_buf_tv(typval_T *tv, int curtab_only)
1803{
1804 char_u *name = tv->vval.v_string;
1805 buf_T *buf;
1806
1807 if (tv->v_type == VAR_NUMBER)
1808 return buflist_findnr((int)tv->vval.v_number);
1809 if (tv->v_type != VAR_STRING)
1810 return NULL;
1811 if (name == NULL || *name == NUL)
1812 return curbuf;
1813 if (name[0] == '$' && name[1] == NUL)
1814 return lastbuf;
1815
1816 buf = buflist_find_by_name(name, curtab_only);
1817
1818 /* If not found, try expanding the name, like done for bufexists(). */
1819 if (buf == NULL)
1820 buf = find_buffer(tv);
1821
1822 return buf;
1823}
1824
1825/*
1826 * "bufname(expr)" function
1827 */
1828 static void
1829f_bufname(typval_T *argvars, typval_T *rettv)
1830{
1831 buf_T *buf;
1832
1833 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1834 ++emsg_off;
1835 buf = get_buf_tv(&argvars[0], FALSE);
1836 rettv->v_type = VAR_STRING;
1837 if (buf != NULL && buf->b_fname != NULL)
1838 rettv->vval.v_string = vim_strsave(buf->b_fname);
1839 else
1840 rettv->vval.v_string = NULL;
1841 --emsg_off;
1842}
1843
1844/*
1845 * "bufnr(expr)" function
1846 */
1847 static void
1848f_bufnr(typval_T *argvars, typval_T *rettv)
1849{
1850 buf_T *buf;
1851 int error = FALSE;
1852 char_u *name;
1853
1854 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1855 ++emsg_off;
1856 buf = get_buf_tv(&argvars[0], FALSE);
1857 --emsg_off;
1858
1859 /* If the buffer isn't found and the second argument is not zero create a
1860 * new buffer. */
1861 if (buf == NULL
1862 && argvars[1].v_type != VAR_UNKNOWN
1863 && get_tv_number_chk(&argvars[1], &error) != 0
1864 && !error
1865 && (name = get_tv_string_chk(&argvars[0])) != NULL
1866 && !error)
1867 buf = buflist_new(name, NULL, (linenr_T)1, 0);
1868
1869 if (buf != NULL)
1870 rettv->vval.v_number = buf->b_fnum;
1871 else
1872 rettv->vval.v_number = -1;
1873}
1874
1875 static void
1876buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
1877{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001878 win_T *wp;
1879 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001880 buf_T *buf;
1881
1882 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1883 ++emsg_off;
1884 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02001885 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001886 {
1887 ++winnr;
1888 if (wp->w_buffer == buf)
1889 break;
1890 }
1891 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001892 --emsg_off;
1893}
1894
1895/*
1896 * "bufwinid(nr)" function
1897 */
1898 static void
1899f_bufwinid(typval_T *argvars, typval_T *rettv)
1900{
1901 buf_win_common(argvars, rettv, FALSE);
1902}
1903
1904/*
1905 * "bufwinnr(nr)" function
1906 */
1907 static void
1908f_bufwinnr(typval_T *argvars, typval_T *rettv)
1909{
1910 buf_win_common(argvars, rettv, TRUE);
1911}
1912
1913/*
1914 * "byte2line(byte)" function
1915 */
1916 static void
1917f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
1918{
1919#ifndef FEAT_BYTEOFF
1920 rettv->vval.v_number = -1;
1921#else
1922 long boff = 0;
1923
1924 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
1925 if (boff < 0)
1926 rettv->vval.v_number = -1;
1927 else
1928 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
1929 (linenr_T)0, &boff);
1930#endif
1931}
1932
1933 static void
1934byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
1935{
1936#ifdef FEAT_MBYTE
1937 char_u *t;
1938#endif
1939 char_u *str;
1940 varnumber_T idx;
1941
1942 str = get_tv_string_chk(&argvars[0]);
1943 idx = get_tv_number_chk(&argvars[1], NULL);
1944 rettv->vval.v_number = -1;
1945 if (str == NULL || idx < 0)
1946 return;
1947
1948#ifdef FEAT_MBYTE
1949 t = str;
1950 for ( ; idx > 0; idx--)
1951 {
1952 if (*t == NUL) /* EOL reached */
1953 return;
1954 if (enc_utf8 && comp)
1955 t += utf_ptr2len(t);
1956 else
1957 t += (*mb_ptr2len)(t);
1958 }
1959 rettv->vval.v_number = (varnumber_T)(t - str);
1960#else
1961 if ((size_t)idx <= STRLEN(str))
1962 rettv->vval.v_number = idx;
1963#endif
1964}
1965
1966/*
1967 * "byteidx()" function
1968 */
1969 static void
1970f_byteidx(typval_T *argvars, typval_T *rettv)
1971{
1972 byteidx(argvars, rettv, FALSE);
1973}
1974
1975/*
1976 * "byteidxcomp()" function
1977 */
1978 static void
1979f_byteidxcomp(typval_T *argvars, typval_T *rettv)
1980{
1981 byteidx(argvars, rettv, TRUE);
1982}
1983
1984/*
1985 * "call(func, arglist [, dict])" function
1986 */
1987 static void
1988f_call(typval_T *argvars, typval_T *rettv)
1989{
1990 char_u *func;
1991 partial_T *partial = NULL;
1992 dict_T *selfdict = NULL;
1993
1994 if (argvars[1].v_type != VAR_LIST)
1995 {
1996 EMSG(_(e_listreq));
1997 return;
1998 }
1999 if (argvars[1].vval.v_list == NULL)
2000 return;
2001
2002 if (argvars[0].v_type == VAR_FUNC)
2003 func = argvars[0].vval.v_string;
2004 else if (argvars[0].v_type == VAR_PARTIAL)
2005 {
2006 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002007 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002008 }
2009 else
2010 func = get_tv_string(&argvars[0]);
2011 if (*func == NUL)
2012 return; /* type error or empty name */
2013
2014 if (argvars[2].v_type != VAR_UNKNOWN)
2015 {
2016 if (argvars[2].v_type != VAR_DICT)
2017 {
2018 EMSG(_(e_dictreq));
2019 return;
2020 }
2021 selfdict = argvars[2].vval.v_dict;
2022 }
2023
2024 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2025}
2026
2027#ifdef FEAT_FLOAT
2028/*
2029 * "ceil({float})" function
2030 */
2031 static void
2032f_ceil(typval_T *argvars, typval_T *rettv)
2033{
2034 float_T f = 0.0;
2035
2036 rettv->v_type = VAR_FLOAT;
2037 if (get_float_arg(argvars, &f) == OK)
2038 rettv->vval.v_float = ceil(f);
2039 else
2040 rettv->vval.v_float = 0.0;
2041}
2042#endif
2043
2044#ifdef FEAT_JOB_CHANNEL
2045/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002046 * "ch_canread()" function
2047 */
2048 static void
2049f_ch_canread(typval_T *argvars, typval_T *rettv)
2050{
Bram Moolenaar958dc692016-12-01 15:34:12 +01002051 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002052
2053 rettv->vval.v_number = 0;
2054 if (channel != NULL)
2055 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
2056 || channel_has_readahead(channel, PART_OUT)
2057 || channel_has_readahead(channel, PART_ERR);
2058}
2059
2060/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002061 * "ch_close()" function
2062 */
2063 static void
2064f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
2065{
2066 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2067
2068 if (channel != NULL)
2069 {
2070 channel_close(channel, FALSE);
2071 channel_clear(channel);
2072 }
2073}
2074
2075/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002076 * "ch_close()" function
2077 */
2078 static void
2079f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
2080{
2081 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2082
2083 if (channel != NULL)
2084 channel_close_in(channel);
2085}
2086
2087/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002088 * "ch_getbufnr()" function
2089 */
2090 static void
2091f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
2092{
2093 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2094
2095 rettv->vval.v_number = -1;
2096 if (channel != NULL)
2097 {
2098 char_u *what = get_tv_string(&argvars[1]);
2099 int part;
2100
2101 if (STRCMP(what, "err") == 0)
2102 part = PART_ERR;
2103 else if (STRCMP(what, "out") == 0)
2104 part = PART_OUT;
2105 else if (STRCMP(what, "in") == 0)
2106 part = PART_IN;
2107 else
2108 part = PART_SOCK;
2109 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
2110 rettv->vval.v_number =
2111 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
2112 }
2113}
2114
2115/*
2116 * "ch_getjob()" function
2117 */
2118 static void
2119f_ch_getjob(typval_T *argvars, typval_T *rettv)
2120{
2121 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2122
2123 if (channel != NULL)
2124 {
2125 rettv->v_type = VAR_JOB;
2126 rettv->vval.v_job = channel->ch_job;
2127 if (channel->ch_job != NULL)
2128 ++channel->ch_job->jv_refcount;
2129 }
2130}
2131
2132/*
2133 * "ch_info()" function
2134 */
2135 static void
2136f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
2137{
2138 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2139
2140 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
2141 channel_info(channel, rettv->vval.v_dict);
2142}
2143
2144/*
2145 * "ch_log()" function
2146 */
2147 static void
2148f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
2149{
2150 char_u *msg = get_tv_string(&argvars[0]);
2151 channel_T *channel = NULL;
2152
2153 if (argvars[1].v_type != VAR_UNKNOWN)
2154 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
2155
Bram Moolenaard5359b22018-04-05 22:44:39 +02002156 ch_log(channel, "%s", msg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002157}
2158
2159/*
2160 * "ch_logfile()" function
2161 */
2162 static void
2163f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
2164{
2165 char_u *fname;
2166 char_u *opt = (char_u *)"";
2167 char_u buf[NUMBUFLEN];
2168
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02002169 /* Don't open a file in restricted mode. */
2170 if (check_restricted() || check_secure())
2171 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002172 fname = get_tv_string(&argvars[0]);
2173 if (argvars[1].v_type == VAR_STRING)
2174 opt = get_tv_string_buf(&argvars[1], buf);
2175 ch_logfile(fname, opt);
2176}
2177
2178/*
2179 * "ch_open()" function
2180 */
2181 static void
2182f_ch_open(typval_T *argvars, typval_T *rettv)
2183{
2184 rettv->v_type = VAR_CHANNEL;
2185 if (check_restricted() || check_secure())
2186 return;
2187 rettv->vval.v_channel = channel_open_func(argvars);
2188}
2189
2190/*
2191 * "ch_read()" function
2192 */
2193 static void
2194f_ch_read(typval_T *argvars, typval_T *rettv)
2195{
2196 common_channel_read(argvars, rettv, FALSE);
2197}
2198
2199/*
2200 * "ch_readraw()" function
2201 */
2202 static void
2203f_ch_readraw(typval_T *argvars, typval_T *rettv)
2204{
2205 common_channel_read(argvars, rettv, TRUE);
2206}
2207
2208/*
2209 * "ch_evalexpr()" function
2210 */
2211 static void
2212f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2213{
2214 ch_expr_common(argvars, rettv, TRUE);
2215}
2216
2217/*
2218 * "ch_sendexpr()" function
2219 */
2220 static void
2221f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2222{
2223 ch_expr_common(argvars, rettv, FALSE);
2224}
2225
2226/*
2227 * "ch_evalraw()" function
2228 */
2229 static void
2230f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2231{
2232 ch_raw_common(argvars, rettv, TRUE);
2233}
2234
2235/*
2236 * "ch_sendraw()" function
2237 */
2238 static void
2239f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2240{
2241 ch_raw_common(argvars, rettv, FALSE);
2242}
2243
2244/*
2245 * "ch_setoptions()" function
2246 */
2247 static void
2248f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2249{
2250 channel_T *channel;
2251 jobopt_T opt;
2252
2253 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2254 if (channel == NULL)
2255 return;
2256 clear_job_options(&opt);
2257 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002258 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002259 channel_set_options(channel, &opt);
2260 free_job_options(&opt);
2261}
2262
2263/*
2264 * "ch_status()" function
2265 */
2266 static void
2267f_ch_status(typval_T *argvars, typval_T *rettv)
2268{
2269 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002270 jobopt_T opt;
2271 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002272
2273 /* return an empty string by default */
2274 rettv->v_type = VAR_STRING;
2275 rettv->vval.v_string = NULL;
2276
2277 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002278
2279 if (argvars[1].v_type != VAR_UNKNOWN)
2280 {
2281 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002282 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002283 && (opt.jo_set & JO_PART))
2284 part = opt.jo_part;
2285 }
2286
2287 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002288}
2289#endif
2290
2291/*
2292 * "changenr()" function
2293 */
2294 static void
2295f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2296{
2297 rettv->vval.v_number = curbuf->b_u_seq_cur;
2298}
2299
2300/*
2301 * "char2nr(string)" function
2302 */
2303 static void
2304f_char2nr(typval_T *argvars, typval_T *rettv)
2305{
2306#ifdef FEAT_MBYTE
2307 if (has_mbyte)
2308 {
2309 int utf8 = 0;
2310
2311 if (argvars[1].v_type != VAR_UNKNOWN)
2312 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
2313
2314 if (utf8)
2315 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
2316 else
2317 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
2318 }
2319 else
2320#endif
2321 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
2322}
2323
2324/*
2325 * "cindent(lnum)" function
2326 */
2327 static void
2328f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2329{
2330#ifdef FEAT_CINDENT
2331 pos_T pos;
2332 linenr_T lnum;
2333
2334 pos = curwin->w_cursor;
2335 lnum = get_tv_lnum(argvars);
2336 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2337 {
2338 curwin->w_cursor.lnum = lnum;
2339 rettv->vval.v_number = get_c_indent();
2340 curwin->w_cursor = pos;
2341 }
2342 else
2343#endif
2344 rettv->vval.v_number = -1;
2345}
2346
2347/*
2348 * "clearmatches()" function
2349 */
2350 static void
2351f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2352{
2353#ifdef FEAT_SEARCH_EXTRA
2354 clear_matches(curwin);
2355#endif
2356}
2357
2358/*
2359 * "col(string)" function
2360 */
2361 static void
2362f_col(typval_T *argvars, typval_T *rettv)
2363{
2364 colnr_T col = 0;
2365 pos_T *fp;
2366 int fnum = curbuf->b_fnum;
2367
2368 fp = var2fpos(&argvars[0], FALSE, &fnum);
2369 if (fp != NULL && fnum == curbuf->b_fnum)
2370 {
2371 if (fp->col == MAXCOL)
2372 {
2373 /* '> can be MAXCOL, get the length of the line then */
2374 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2375 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2376 else
2377 col = MAXCOL;
2378 }
2379 else
2380 {
2381 col = fp->col + 1;
2382#ifdef FEAT_VIRTUALEDIT
2383 /* col(".") when the cursor is on the NUL at the end of the line
2384 * because of "coladd" can be seen as an extra column. */
2385 if (virtual_active() && fp == &curwin->w_cursor)
2386 {
2387 char_u *p = ml_get_cursor();
2388
2389 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2390 curwin->w_virtcol - curwin->w_cursor.coladd))
2391 {
2392# ifdef FEAT_MBYTE
2393 int l;
2394
2395 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2396 col += l;
2397# else
2398 if (*p != NUL && p[1] == NUL)
2399 ++col;
2400# endif
2401 }
2402 }
2403#endif
2404 }
2405 }
2406 rettv->vval.v_number = col;
2407}
2408
2409#if defined(FEAT_INS_EXPAND)
2410/*
2411 * "complete()" function
2412 */
2413 static void
2414f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2415{
2416 int startcol;
2417
2418 if ((State & INSERT) == 0)
2419 {
2420 EMSG(_("E785: complete() can only be used in Insert mode"));
2421 return;
2422 }
2423
2424 /* Check for undo allowed here, because if something was already inserted
2425 * the line was already saved for undo and this check isn't done. */
2426 if (!undo_allowed())
2427 return;
2428
2429 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2430 {
2431 EMSG(_(e_invarg));
2432 return;
2433 }
2434
2435 startcol = (int)get_tv_number_chk(&argvars[0], NULL);
2436 if (startcol <= 0)
2437 return;
2438
2439 set_completion(startcol - 1, argvars[1].vval.v_list);
2440}
2441
2442/*
2443 * "complete_add()" function
2444 */
2445 static void
2446f_complete_add(typval_T *argvars, typval_T *rettv)
2447{
2448 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2449}
2450
2451/*
2452 * "complete_check()" function
2453 */
2454 static void
2455f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2456{
2457 int saved = RedrawingDisabled;
2458
2459 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002460 ins_compl_check_keys(0, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002461 rettv->vval.v_number = compl_interrupted;
2462 RedrawingDisabled = saved;
2463}
2464#endif
2465
2466/*
2467 * "confirm(message, buttons[, default [, type]])" function
2468 */
2469 static void
2470f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2471{
2472#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2473 char_u *message;
2474 char_u *buttons = NULL;
2475 char_u buf[NUMBUFLEN];
2476 char_u buf2[NUMBUFLEN];
2477 int def = 1;
2478 int type = VIM_GENERIC;
2479 char_u *typestr;
2480 int error = FALSE;
2481
2482 message = get_tv_string_chk(&argvars[0]);
2483 if (message == NULL)
2484 error = TRUE;
2485 if (argvars[1].v_type != VAR_UNKNOWN)
2486 {
2487 buttons = get_tv_string_buf_chk(&argvars[1], buf);
2488 if (buttons == NULL)
2489 error = TRUE;
2490 if (argvars[2].v_type != VAR_UNKNOWN)
2491 {
2492 def = (int)get_tv_number_chk(&argvars[2], &error);
2493 if (argvars[3].v_type != VAR_UNKNOWN)
2494 {
2495 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
2496 if (typestr == NULL)
2497 error = TRUE;
2498 else
2499 {
2500 switch (TOUPPER_ASC(*typestr))
2501 {
2502 case 'E': type = VIM_ERROR; break;
2503 case 'Q': type = VIM_QUESTION; break;
2504 case 'I': type = VIM_INFO; break;
2505 case 'W': type = VIM_WARNING; break;
2506 case 'G': type = VIM_GENERIC; break;
2507 }
2508 }
2509 }
2510 }
2511 }
2512
2513 if (buttons == NULL || *buttons == NUL)
2514 buttons = (char_u *)_("&Ok");
2515
2516 if (!error)
2517 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2518 def, NULL, FALSE);
2519#endif
2520}
2521
2522/*
2523 * "copy()" function
2524 */
2525 static void
2526f_copy(typval_T *argvars, typval_T *rettv)
2527{
2528 item_copy(&argvars[0], rettv, FALSE, 0);
2529}
2530
2531#ifdef FEAT_FLOAT
2532/*
2533 * "cos()" function
2534 */
2535 static void
2536f_cos(typval_T *argvars, typval_T *rettv)
2537{
2538 float_T f = 0.0;
2539
2540 rettv->v_type = VAR_FLOAT;
2541 if (get_float_arg(argvars, &f) == OK)
2542 rettv->vval.v_float = cos(f);
2543 else
2544 rettv->vval.v_float = 0.0;
2545}
2546
2547/*
2548 * "cosh()" function
2549 */
2550 static void
2551f_cosh(typval_T *argvars, typval_T *rettv)
2552{
2553 float_T f = 0.0;
2554
2555 rettv->v_type = VAR_FLOAT;
2556 if (get_float_arg(argvars, &f) == OK)
2557 rettv->vval.v_float = cosh(f);
2558 else
2559 rettv->vval.v_float = 0.0;
2560}
2561#endif
2562
2563/*
2564 * "count()" function
2565 */
2566 static void
2567f_count(typval_T *argvars, typval_T *rettv)
2568{
2569 long n = 0;
2570 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002571 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002572
Bram Moolenaar9966b212017-07-28 16:46:57 +02002573 if (argvars[2].v_type != VAR_UNKNOWN)
2574 ic = (int)get_tv_number_chk(&argvars[2], &error);
2575
2576 if (argvars[0].v_type == VAR_STRING)
2577 {
2578 char_u *expr = get_tv_string_chk(&argvars[1]);
2579 char_u *p = argvars[0].vval.v_string;
2580 char_u *next;
2581
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002582 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002583 {
2584 if (ic)
2585 {
2586 size_t len = STRLEN(expr);
2587
2588 while (*p != NUL)
2589 {
2590 if (MB_STRNICMP(p, expr, len) == 0)
2591 {
2592 ++n;
2593 p += len;
2594 }
2595 else
2596 MB_PTR_ADV(p);
2597 }
2598 }
2599 else
2600 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2601 != NULL)
2602 {
2603 ++n;
2604 p = next + STRLEN(expr);
2605 }
2606 }
2607
2608 }
2609 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002610 {
2611 listitem_T *li;
2612 list_T *l;
2613 long idx;
2614
2615 if ((l = argvars[0].vval.v_list) != NULL)
2616 {
2617 li = l->lv_first;
2618 if (argvars[2].v_type != VAR_UNKNOWN)
2619 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002620 if (argvars[3].v_type != VAR_UNKNOWN)
2621 {
2622 idx = (long)get_tv_number_chk(&argvars[3], &error);
2623 if (!error)
2624 {
2625 li = list_find(l, idx);
2626 if (li == NULL)
2627 EMSGN(_(e_listidx), idx);
2628 }
2629 }
2630 if (error)
2631 li = NULL;
2632 }
2633
2634 for ( ; li != NULL; li = li->li_next)
2635 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2636 ++n;
2637 }
2638 }
2639 else if (argvars[0].v_type == VAR_DICT)
2640 {
2641 int todo;
2642 dict_T *d;
2643 hashitem_T *hi;
2644
2645 if ((d = argvars[0].vval.v_dict) != NULL)
2646 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002647 if (argvars[2].v_type != VAR_UNKNOWN)
2648 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002649 if (argvars[3].v_type != VAR_UNKNOWN)
2650 EMSG(_(e_invarg));
2651 }
2652
2653 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2654 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2655 {
2656 if (!HASHITEM_EMPTY(hi))
2657 {
2658 --todo;
2659 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2660 ++n;
2661 }
2662 }
2663 }
2664 }
2665 else
2666 EMSG2(_(e_listdictarg), "count()");
2667 rettv->vval.v_number = n;
2668}
2669
2670/*
2671 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2672 *
2673 * Checks the existence of a cscope connection.
2674 */
2675 static void
2676f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2677{
2678#ifdef FEAT_CSCOPE
2679 int num = 0;
2680 char_u *dbpath = NULL;
2681 char_u *prepend = NULL;
2682 char_u buf[NUMBUFLEN];
2683
2684 if (argvars[0].v_type != VAR_UNKNOWN
2685 && argvars[1].v_type != VAR_UNKNOWN)
2686 {
2687 num = (int)get_tv_number(&argvars[0]);
2688 dbpath = get_tv_string(&argvars[1]);
2689 if (argvars[2].v_type != VAR_UNKNOWN)
2690 prepend = get_tv_string_buf(&argvars[2], buf);
2691 }
2692
2693 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2694#endif
2695}
2696
2697/*
2698 * "cursor(lnum, col)" function, or
2699 * "cursor(list)"
2700 *
2701 * Moves the cursor to the specified line and column.
2702 * Returns 0 when the position could be set, -1 otherwise.
2703 */
2704 static void
2705f_cursor(typval_T *argvars, typval_T *rettv)
2706{
2707 long line, col;
2708#ifdef FEAT_VIRTUALEDIT
2709 long coladd = 0;
2710#endif
2711 int set_curswant = TRUE;
2712
2713 rettv->vval.v_number = -1;
2714 if (argvars[1].v_type == VAR_UNKNOWN)
2715 {
2716 pos_T pos;
2717 colnr_T curswant = -1;
2718
2719 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2720 {
2721 EMSG(_(e_invarg));
2722 return;
2723 }
2724 line = pos.lnum;
2725 col = pos.col;
2726#ifdef FEAT_VIRTUALEDIT
2727 coladd = pos.coladd;
2728#endif
2729 if (curswant >= 0)
2730 {
2731 curwin->w_curswant = curswant - 1;
2732 set_curswant = FALSE;
2733 }
2734 }
2735 else
2736 {
2737 line = get_tv_lnum(argvars);
2738 col = (long)get_tv_number_chk(&argvars[1], NULL);
2739#ifdef FEAT_VIRTUALEDIT
2740 if (argvars[2].v_type != VAR_UNKNOWN)
2741 coladd = (long)get_tv_number_chk(&argvars[2], NULL);
2742#endif
2743 }
2744 if (line < 0 || col < 0
2745#ifdef FEAT_VIRTUALEDIT
2746 || coladd < 0
2747#endif
2748 )
2749 return; /* type error; errmsg already given */
2750 if (line > 0)
2751 curwin->w_cursor.lnum = line;
2752 if (col > 0)
2753 curwin->w_cursor.col = col - 1;
2754#ifdef FEAT_VIRTUALEDIT
2755 curwin->w_cursor.coladd = coladd;
2756#endif
2757
2758 /* Make sure the cursor is in a valid position. */
2759 check_cursor();
2760#ifdef FEAT_MBYTE
2761 /* Correct cursor for multi-byte character. */
2762 if (has_mbyte)
2763 mb_adjust_cursor();
2764#endif
2765
2766 curwin->w_set_curswant = set_curswant;
2767 rettv->vval.v_number = 0;
2768}
2769
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002770#ifdef WIN3264
2771/*
2772 * "debugbreak()" function
2773 */
2774 static void
2775f_debugbreak(typval_T *argvars, typval_T *rettv)
2776{
2777 int pid;
2778
2779 rettv->vval.v_number = FAIL;
2780 pid = (int)get_tv_number(&argvars[0]);
2781 if (pid == 0)
2782 EMSG(_(e_invarg));
2783 else
2784 {
2785 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2786
2787 if (hProcess != NULL)
2788 {
2789 DebugBreakProcess(hProcess);
2790 CloseHandle(hProcess);
2791 rettv->vval.v_number = OK;
2792 }
2793 }
2794}
2795#endif
2796
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002797/*
2798 * "deepcopy()" function
2799 */
2800 static void
2801f_deepcopy(typval_T *argvars, typval_T *rettv)
2802{
2803 int noref = 0;
2804 int copyID;
2805
2806 if (argvars[1].v_type != VAR_UNKNOWN)
2807 noref = (int)get_tv_number_chk(&argvars[1], NULL);
2808 if (noref < 0 || noref > 1)
2809 EMSG(_(e_invarg));
2810 else
2811 {
2812 copyID = get_copyID();
2813 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2814 }
2815}
2816
2817/*
2818 * "delete()" function
2819 */
2820 static void
2821f_delete(typval_T *argvars, typval_T *rettv)
2822{
2823 char_u nbuf[NUMBUFLEN];
2824 char_u *name;
2825 char_u *flags;
2826
2827 rettv->vval.v_number = -1;
2828 if (check_restricted() || check_secure())
2829 return;
2830
2831 name = get_tv_string(&argvars[0]);
2832 if (name == NULL || *name == NUL)
2833 {
2834 EMSG(_(e_invarg));
2835 return;
2836 }
2837
2838 if (argvars[1].v_type != VAR_UNKNOWN)
2839 flags = get_tv_string_buf(&argvars[1], nbuf);
2840 else
2841 flags = (char_u *)"";
2842
2843 if (*flags == NUL)
2844 /* delete a file */
2845 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2846 else if (STRCMP(flags, "d") == 0)
2847 /* delete an empty directory */
2848 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2849 else if (STRCMP(flags, "rf") == 0)
2850 /* delete a directory recursively */
2851 rettv->vval.v_number = delete_recursive(name);
2852 else
2853 EMSG2(_(e_invexpr2), flags);
2854}
2855
2856/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02002857 * "deletebufline()" function
2858 */
2859 static void
2860f_deletebufline(argvars, rettv)
2861 typval_T *argvars;
2862 typval_T *rettv;
2863{
2864 buf_T *buf;
2865 linenr_T first, last;
2866 linenr_T lnum;
2867 long count;
2868 int is_curbuf;
2869 buf_T *curbuf_save = NULL;
2870 win_T *curwin_save = NULL;
2871 tabpage_T *tp;
2872 win_T *wp;
2873
2874 buf = get_buf_tv(&argvars[0], FALSE);
2875 if (buf == NULL)
2876 {
2877 rettv->vval.v_number = 1; /* FAIL */
2878 return;
2879 }
2880 is_curbuf = buf == curbuf;
2881
2882 first = get_tv_lnum_buf(&argvars[1], buf);
2883 if (argvars[2].v_type != VAR_UNKNOWN)
2884 last = get_tv_lnum_buf(&argvars[2], buf);
2885 else
2886 last = first;
2887
2888 if (buf->b_ml.ml_mfp == NULL || first < 1
2889 || first > buf->b_ml.ml_line_count || last < first)
2890 {
2891 rettv->vval.v_number = 1; /* FAIL */
2892 return;
2893 }
2894
2895 if (!is_curbuf)
2896 {
2897 curbuf_save = curbuf;
2898 curwin_save = curwin;
2899 curbuf = buf;
2900 find_win_for_curbuf();
2901 }
2902 if (last > curbuf->b_ml.ml_line_count)
2903 last = curbuf->b_ml.ml_line_count;
2904 count = last - first + 1;
2905
2906 // When coming here from Insert mode, sync undo, so that this can be
2907 // undone separately from what was previously inserted.
2908 if (u_sync_once == 2)
2909 {
2910 u_sync_once = 1; // notify that u_sync() was called
2911 u_sync(TRUE);
2912 }
2913
2914 if (u_save(first - 1, last + 1) == FAIL)
2915 {
2916 rettv->vval.v_number = 1; /* FAIL */
2917 return;
2918 }
2919
2920 for (lnum = first; lnum <= last; ++lnum)
2921 ml_delete(first, TRUE);
2922
2923 FOR_ALL_TAB_WINDOWS(tp, wp)
2924 if (wp->w_buffer == buf)
2925 {
2926 if (wp->w_cursor.lnum > last)
2927 wp->w_cursor.lnum -= count;
2928 else if (wp->w_cursor.lnum> first)
2929 wp->w_cursor.lnum = first;
2930 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
2931 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
2932 }
2933 check_cursor_col();
2934 deleted_lines_mark(first, count);
2935
2936 if (!is_curbuf)
2937 {
2938 curbuf = curbuf_save;
2939 curwin = curwin_save;
2940 }
2941}
2942
2943/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002944 * "did_filetype()" function
2945 */
2946 static void
2947f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2948{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002949 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002950}
2951
2952/*
2953 * "diff_filler()" function
2954 */
2955 static void
2956f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2957{
2958#ifdef FEAT_DIFF
2959 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
2960#endif
2961}
2962
2963/*
2964 * "diff_hlID()" function
2965 */
2966 static void
2967f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2968{
2969#ifdef FEAT_DIFF
2970 linenr_T lnum = get_tv_lnum(argvars);
2971 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01002972 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002973 static int fnum = 0;
2974 static int change_start = 0;
2975 static int change_end = 0;
2976 static hlf_T hlID = (hlf_T)0;
2977 int filler_lines;
2978 int col;
2979
2980 if (lnum < 0) /* ignore type error in {lnum} arg */
2981 lnum = 0;
2982 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002983 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002984 || fnum != curbuf->b_fnum)
2985 {
2986 /* New line, buffer, change: need to get the values. */
2987 filler_lines = diff_check(curwin, lnum);
2988 if (filler_lines < 0)
2989 {
2990 if (filler_lines == -1)
2991 {
2992 change_start = MAXCOL;
2993 change_end = -1;
2994 if (diff_find_change(curwin, lnum, &change_start, &change_end))
2995 hlID = HLF_ADD; /* added line */
2996 else
2997 hlID = HLF_CHD; /* changed line */
2998 }
2999 else
3000 hlID = HLF_ADD; /* added line */
3001 }
3002 else
3003 hlID = (hlf_T)0;
3004 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003005 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003006 fnum = curbuf->b_fnum;
3007 }
3008
3009 if (hlID == HLF_CHD || hlID == HLF_TXD)
3010 {
3011 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
3012 if (col >= change_start && col <= change_end)
3013 hlID = HLF_TXD; /* changed text */
3014 else
3015 hlID = HLF_CHD; /* changed line */
3016 }
3017 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
3018#endif
3019}
3020
3021/*
3022 * "empty({expr})" function
3023 */
3024 static void
3025f_empty(typval_T *argvars, typval_T *rettv)
3026{
3027 int n = FALSE;
3028
3029 switch (argvars[0].v_type)
3030 {
3031 case VAR_STRING:
3032 case VAR_FUNC:
3033 n = argvars[0].vval.v_string == NULL
3034 || *argvars[0].vval.v_string == NUL;
3035 break;
3036 case VAR_PARTIAL:
3037 n = FALSE;
3038 break;
3039 case VAR_NUMBER:
3040 n = argvars[0].vval.v_number == 0;
3041 break;
3042 case VAR_FLOAT:
3043#ifdef FEAT_FLOAT
3044 n = argvars[0].vval.v_float == 0.0;
3045 break;
3046#endif
3047 case VAR_LIST:
3048 n = argvars[0].vval.v_list == NULL
3049 || argvars[0].vval.v_list->lv_first == NULL;
3050 break;
3051 case VAR_DICT:
3052 n = argvars[0].vval.v_dict == NULL
3053 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
3054 break;
3055 case VAR_SPECIAL:
3056 n = argvars[0].vval.v_number != VVAL_TRUE;
3057 break;
3058
3059 case VAR_JOB:
3060#ifdef FEAT_JOB_CHANNEL
3061 n = argvars[0].vval.v_job == NULL
3062 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
3063 break;
3064#endif
3065 case VAR_CHANNEL:
3066#ifdef FEAT_JOB_CHANNEL
3067 n = argvars[0].vval.v_channel == NULL
3068 || !channel_is_open(argvars[0].vval.v_channel);
3069 break;
3070#endif
3071 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01003072 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003073 n = TRUE;
3074 break;
3075 }
3076
3077 rettv->vval.v_number = n;
3078}
3079
3080/*
3081 * "escape({string}, {chars})" function
3082 */
3083 static void
3084f_escape(typval_T *argvars, typval_T *rettv)
3085{
3086 char_u buf[NUMBUFLEN];
3087
3088 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
3089 get_tv_string_buf(&argvars[1], buf));
3090 rettv->v_type = VAR_STRING;
3091}
3092
3093/*
3094 * "eval()" function
3095 */
3096 static void
3097f_eval(typval_T *argvars, typval_T *rettv)
3098{
3099 char_u *s, *p;
3100
3101 s = get_tv_string_chk(&argvars[0]);
3102 if (s != NULL)
3103 s = skipwhite(s);
3104
3105 p = s;
3106 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3107 {
3108 if (p != NULL && !aborting())
3109 EMSG2(_(e_invexpr2), p);
3110 need_clr_eos = FALSE;
3111 rettv->v_type = VAR_NUMBER;
3112 rettv->vval.v_number = 0;
3113 }
3114 else if (*s != NUL)
3115 EMSG(_(e_trailing));
3116}
3117
3118/*
3119 * "eventhandler()" function
3120 */
3121 static void
3122f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3123{
3124 rettv->vval.v_number = vgetc_busy;
3125}
3126
3127/*
3128 * "executable()" function
3129 */
3130 static void
3131f_executable(typval_T *argvars, typval_T *rettv)
3132{
3133 char_u *name = get_tv_string(&argvars[0]);
3134
3135 /* Check in $PATH and also check directly if there is a directory name. */
3136 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
3137 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
3138}
3139
3140static garray_T redir_execute_ga;
3141
3142/*
3143 * Append "value[value_len]" to the execute() output.
3144 */
3145 void
3146execute_redir_str(char_u *value, int value_len)
3147{
3148 int len;
3149
3150 if (value_len == -1)
3151 len = (int)STRLEN(value); /* Append the entire string */
3152 else
3153 len = value_len; /* Append only "value_len" characters */
3154 if (ga_grow(&redir_execute_ga, len) == OK)
3155 {
3156 mch_memmove((char *)redir_execute_ga.ga_data
3157 + redir_execute_ga.ga_len, value, len);
3158 redir_execute_ga.ga_len += len;
3159 }
3160}
3161
3162/*
3163 * Get next line from a list.
3164 * Called by do_cmdline() to get the next line.
3165 * Returns allocated string, or NULL for end of function.
3166 */
3167
3168 static char_u *
3169get_list_line(
3170 int c UNUSED,
3171 void *cookie,
3172 int indent UNUSED)
3173{
3174 listitem_T **p = (listitem_T **)cookie;
3175 listitem_T *item = *p;
3176 char_u buf[NUMBUFLEN];
3177 char_u *s;
3178
3179 if (item == NULL)
3180 return NULL;
3181 s = get_tv_string_buf_chk(&item->li_tv, buf);
3182 *p = item->li_next;
3183 return s == NULL ? NULL : vim_strsave(s);
3184}
3185
3186/*
3187 * "execute()" function
3188 */
3189 static void
3190f_execute(typval_T *argvars, typval_T *rettv)
3191{
3192 char_u *cmd = NULL;
3193 list_T *list = NULL;
3194 int save_msg_silent = msg_silent;
3195 int save_emsg_silent = emsg_silent;
3196 int save_emsg_noredir = emsg_noredir;
3197 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003198 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003199 garray_T save_ga;
3200
3201 rettv->vval.v_string = NULL;
3202 rettv->v_type = VAR_STRING;
3203
3204 if (argvars[0].v_type == VAR_LIST)
3205 {
3206 list = argvars[0].vval.v_list;
3207 if (list == NULL || list->lv_first == NULL)
3208 /* empty list, no commands, empty output */
3209 return;
3210 ++list->lv_refcount;
3211 }
3212 else
3213 {
3214 cmd = get_tv_string_chk(&argvars[0]);
3215 if (cmd == NULL)
3216 return;
3217 }
3218
3219 if (argvars[1].v_type != VAR_UNKNOWN)
3220 {
3221 char_u buf[NUMBUFLEN];
3222 char_u *s = get_tv_string_buf_chk(&argvars[1], buf);
3223
3224 if (s == NULL)
3225 return;
3226 if (STRNCMP(s, "silent", 6) == 0)
3227 ++msg_silent;
3228 if (STRCMP(s, "silent!") == 0)
3229 {
3230 emsg_silent = TRUE;
3231 emsg_noredir = TRUE;
3232 }
3233 }
3234 else
3235 ++msg_silent;
3236
3237 if (redir_execute)
3238 save_ga = redir_execute_ga;
3239 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3240 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003241 redir_off = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003242
3243 if (cmd != NULL)
3244 do_cmdline_cmd(cmd);
3245 else
3246 {
3247 listitem_T *item = list->lv_first;
3248
3249 do_cmdline(NULL, get_list_line, (void *)&item,
3250 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3251 --list->lv_refcount;
3252 }
3253
Bram Moolenaard297f352017-01-29 20:31:21 +01003254 /* Need to append a NUL to the result. */
3255 if (ga_grow(&redir_execute_ga, 1) == OK)
3256 {
3257 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3258 rettv->vval.v_string = redir_execute_ga.ga_data;
3259 }
3260 else
3261 {
3262 ga_clear(&redir_execute_ga);
3263 rettv->vval.v_string = NULL;
3264 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003265 msg_silent = save_msg_silent;
3266 emsg_silent = save_emsg_silent;
3267 emsg_noredir = save_emsg_noredir;
3268
3269 redir_execute = save_redir_execute;
3270 if (redir_execute)
3271 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003272 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003273
3274 /* "silent reg" or "silent echo x" leaves msg_col somewhere in the
3275 * line. Put it back in the first column. */
3276 msg_col = 0;
3277}
3278
3279/*
3280 * "exepath()" function
3281 */
3282 static void
3283f_exepath(typval_T *argvars, typval_T *rettv)
3284{
3285 char_u *p = NULL;
3286
3287 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
3288 rettv->v_type = VAR_STRING;
3289 rettv->vval.v_string = p;
3290}
3291
3292/*
3293 * "exists()" function
3294 */
3295 static void
3296f_exists(typval_T *argvars, typval_T *rettv)
3297{
3298 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003299 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003300
3301 p = get_tv_string(&argvars[0]);
3302 if (*p == '$') /* environment variable */
3303 {
3304 /* first try "normal" environment variables (fast) */
3305 if (mch_getenv(p + 1) != NULL)
3306 n = TRUE;
3307 else
3308 {
3309 /* try expanding things like $VIM and ${HOME} */
3310 p = expand_env_save(p);
3311 if (p != NULL && *p != '$')
3312 n = TRUE;
3313 vim_free(p);
3314 }
3315 }
3316 else if (*p == '&' || *p == '+') /* option */
3317 {
3318 n = (get_option_tv(&p, NULL, TRUE) == OK);
3319 if (*skipwhite(p) != NUL)
3320 n = FALSE; /* trailing garbage */
3321 }
3322 else if (*p == '*') /* internal or user defined function */
3323 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003324 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003325 }
3326 else if (*p == ':')
3327 {
3328 n = cmd_exists(p + 1);
3329 }
3330 else if (*p == '#')
3331 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003332 if (p[1] == '#')
3333 n = autocmd_supported(p + 2);
3334 else
3335 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003336 }
3337 else /* internal variable */
3338 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003339 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003340 }
3341
3342 rettv->vval.v_number = n;
3343}
3344
3345#ifdef FEAT_FLOAT
3346/*
3347 * "exp()" function
3348 */
3349 static void
3350f_exp(typval_T *argvars, typval_T *rettv)
3351{
3352 float_T f = 0.0;
3353
3354 rettv->v_type = VAR_FLOAT;
3355 if (get_float_arg(argvars, &f) == OK)
3356 rettv->vval.v_float = exp(f);
3357 else
3358 rettv->vval.v_float = 0.0;
3359}
3360#endif
3361
3362/*
3363 * "expand()" function
3364 */
3365 static void
3366f_expand(typval_T *argvars, typval_T *rettv)
3367{
3368 char_u *s;
3369 int len;
3370 char_u *errormsg;
3371 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3372 expand_T xpc;
3373 int error = FALSE;
3374 char_u *result;
3375
3376 rettv->v_type = VAR_STRING;
3377 if (argvars[1].v_type != VAR_UNKNOWN
3378 && argvars[2].v_type != VAR_UNKNOWN
3379 && get_tv_number_chk(&argvars[2], &error)
3380 && !error)
3381 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003382 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003383 }
3384
3385 s = get_tv_string(&argvars[0]);
3386 if (*s == '%' || *s == '#' || *s == '<')
3387 {
3388 ++emsg_off;
3389 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3390 --emsg_off;
3391 if (rettv->v_type == VAR_LIST)
3392 {
3393 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3394 list_append_string(rettv->vval.v_list, result, -1);
3395 else
3396 vim_free(result);
3397 }
3398 else
3399 rettv->vval.v_string = result;
3400 }
3401 else
3402 {
3403 /* When the optional second argument is non-zero, don't remove matches
3404 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3405 if (argvars[1].v_type != VAR_UNKNOWN
3406 && get_tv_number_chk(&argvars[1], &error))
3407 options |= WILD_KEEP_ALL;
3408 if (!error)
3409 {
3410 ExpandInit(&xpc);
3411 xpc.xp_context = EXPAND_FILES;
3412 if (p_wic)
3413 options += WILD_ICASE;
3414 if (rettv->v_type == VAR_STRING)
3415 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3416 options, WILD_ALL);
3417 else if (rettv_list_alloc(rettv) != FAIL)
3418 {
3419 int i;
3420
3421 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3422 for (i = 0; i < xpc.xp_numfiles; i++)
3423 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3424 ExpandCleanup(&xpc);
3425 }
3426 }
3427 else
3428 rettv->vval.v_string = NULL;
3429 }
3430}
3431
3432/*
3433 * "extend(list, list [, idx])" function
3434 * "extend(dict, dict [, action])" function
3435 */
3436 static void
3437f_extend(typval_T *argvars, typval_T *rettv)
3438{
3439 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3440
3441 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3442 {
3443 list_T *l1, *l2;
3444 listitem_T *item;
3445 long before;
3446 int error = FALSE;
3447
3448 l1 = argvars[0].vval.v_list;
3449 l2 = argvars[1].vval.v_list;
3450 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
3451 && l2 != NULL)
3452 {
3453 if (argvars[2].v_type != VAR_UNKNOWN)
3454 {
3455 before = (long)get_tv_number_chk(&argvars[2], &error);
3456 if (error)
3457 return; /* type error; errmsg already given */
3458
3459 if (before == l1->lv_len)
3460 item = NULL;
3461 else
3462 {
3463 item = list_find(l1, before);
3464 if (item == NULL)
3465 {
3466 EMSGN(_(e_listidx), before);
3467 return;
3468 }
3469 }
3470 }
3471 else
3472 item = NULL;
3473 list_extend(l1, l2, item);
3474
3475 copy_tv(&argvars[0], rettv);
3476 }
3477 }
3478 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3479 {
3480 dict_T *d1, *d2;
3481 char_u *action;
3482 int i;
3483
3484 d1 = argvars[0].vval.v_dict;
3485 d2 = argvars[1].vval.v_dict;
3486 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
3487 && d2 != NULL)
3488 {
3489 /* Check the third argument. */
3490 if (argvars[2].v_type != VAR_UNKNOWN)
3491 {
3492 static char *(av[]) = {"keep", "force", "error"};
3493
3494 action = get_tv_string_chk(&argvars[2]);
3495 if (action == NULL)
3496 return; /* type error; errmsg already given */
3497 for (i = 0; i < 3; ++i)
3498 if (STRCMP(action, av[i]) == 0)
3499 break;
3500 if (i == 3)
3501 {
3502 EMSG2(_(e_invarg2), action);
3503 return;
3504 }
3505 }
3506 else
3507 action = (char_u *)"force";
3508
3509 dict_extend(d1, d2, action);
3510
3511 copy_tv(&argvars[0], rettv);
3512 }
3513 }
3514 else
3515 EMSG2(_(e_listdictarg), "extend()");
3516}
3517
3518/*
3519 * "feedkeys()" function
3520 */
3521 static void
3522f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3523{
3524 int remap = TRUE;
3525 int insert = FALSE;
3526 char_u *keys, *flags;
3527 char_u nbuf[NUMBUFLEN];
3528 int typed = FALSE;
3529 int execute = FALSE;
3530 int dangerous = FALSE;
3531 char_u *keys_esc;
3532
3533 /* This is not allowed in the sandbox. If the commands would still be
3534 * executed in the sandbox it would be OK, but it probably happens later,
3535 * when "sandbox" is no longer set. */
3536 if (check_secure())
3537 return;
3538
3539 keys = get_tv_string(&argvars[0]);
3540
3541 if (argvars[1].v_type != VAR_UNKNOWN)
3542 {
3543 flags = get_tv_string_buf(&argvars[1], nbuf);
3544 for ( ; *flags != NUL; ++flags)
3545 {
3546 switch (*flags)
3547 {
3548 case 'n': remap = FALSE; break;
3549 case 'm': remap = TRUE; break;
3550 case 't': typed = TRUE; break;
3551 case 'i': insert = TRUE; break;
3552 case 'x': execute = TRUE; break;
3553 case '!': dangerous = TRUE; break;
3554 }
3555 }
3556 }
3557
3558 if (*keys != NUL || execute)
3559 {
3560 /* Need to escape K_SPECIAL and CSI before putting the string in the
3561 * typeahead buffer. */
3562 keys_esc = vim_strsave_escape_csi(keys);
3563 if (keys_esc != NULL)
3564 {
3565 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
3566 insert ? 0 : typebuf.tb_len, !typed, FALSE);
3567 vim_free(keys_esc);
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003568 if (vgetc_busy
3569#ifdef FEAT_TIMERS
3570 || timer_busy
3571#endif
3572 )
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003573 typebuf_was_filled = TRUE;
3574 if (execute)
3575 {
3576 int save_msg_scroll = msg_scroll;
3577
3578 /* Avoid a 1 second delay when the keys start Insert mode. */
3579 msg_scroll = FALSE;
3580
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003581 if (!dangerous)
3582 ++ex_normal_busy;
Bram Moolenaar655a82a2018-05-08 22:01:07 +02003583 exec_normal(TRUE, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003584 if (!dangerous)
3585 --ex_normal_busy;
3586
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003587 msg_scroll |= save_msg_scroll;
3588 }
3589 }
3590 }
3591}
3592
3593/*
3594 * "filereadable()" function
3595 */
3596 static void
3597f_filereadable(typval_T *argvars, typval_T *rettv)
3598{
3599 int fd;
3600 char_u *p;
3601 int n;
3602
3603#ifndef O_NONBLOCK
3604# define O_NONBLOCK 0
3605#endif
3606 p = get_tv_string(&argvars[0]);
3607 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3608 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3609 {
3610 n = TRUE;
3611 close(fd);
3612 }
3613 else
3614 n = FALSE;
3615
3616 rettv->vval.v_number = n;
3617}
3618
3619/*
3620 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3621 * rights to write into.
3622 */
3623 static void
3624f_filewritable(typval_T *argvars, typval_T *rettv)
3625{
3626 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
3627}
3628
3629 static void
3630findfilendir(
3631 typval_T *argvars UNUSED,
3632 typval_T *rettv,
3633 int find_what UNUSED)
3634{
3635#ifdef FEAT_SEARCHPATH
3636 char_u *fname;
3637 char_u *fresult = NULL;
3638 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3639 char_u *p;
3640 char_u pathbuf[NUMBUFLEN];
3641 int count = 1;
3642 int first = TRUE;
3643 int error = FALSE;
3644#endif
3645
3646 rettv->vval.v_string = NULL;
3647 rettv->v_type = VAR_STRING;
3648
3649#ifdef FEAT_SEARCHPATH
3650 fname = get_tv_string(&argvars[0]);
3651
3652 if (argvars[1].v_type != VAR_UNKNOWN)
3653 {
3654 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
3655 if (p == NULL)
3656 error = TRUE;
3657 else
3658 {
3659 if (*p != NUL)
3660 path = p;
3661
3662 if (argvars[2].v_type != VAR_UNKNOWN)
3663 count = (int)get_tv_number_chk(&argvars[2], &error);
3664 }
3665 }
3666
3667 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3668 error = TRUE;
3669
3670 if (*fname != NUL && !error)
3671 {
3672 do
3673 {
3674 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3675 vim_free(fresult);
3676 fresult = find_file_in_path_option(first ? fname : NULL,
3677 first ? (int)STRLEN(fname) : 0,
3678 0, first, path,
3679 find_what,
3680 curbuf->b_ffname,
3681 find_what == FINDFILE_DIR
3682 ? (char_u *)"" : curbuf->b_p_sua);
3683 first = FALSE;
3684
3685 if (fresult != NULL && rettv->v_type == VAR_LIST)
3686 list_append_string(rettv->vval.v_list, fresult, -1);
3687
3688 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3689 }
3690
3691 if (rettv->v_type == VAR_STRING)
3692 rettv->vval.v_string = fresult;
3693#endif
3694}
3695
3696/*
3697 * "filter()" function
3698 */
3699 static void
3700f_filter(typval_T *argvars, typval_T *rettv)
3701{
3702 filter_map(argvars, rettv, FALSE);
3703}
3704
3705/*
3706 * "finddir({fname}[, {path}[, {count}]])" function
3707 */
3708 static void
3709f_finddir(typval_T *argvars, typval_T *rettv)
3710{
3711 findfilendir(argvars, rettv, FINDFILE_DIR);
3712}
3713
3714/*
3715 * "findfile({fname}[, {path}[, {count}]])" function
3716 */
3717 static void
3718f_findfile(typval_T *argvars, typval_T *rettv)
3719{
3720 findfilendir(argvars, rettv, FINDFILE_FILE);
3721}
3722
3723#ifdef FEAT_FLOAT
3724/*
3725 * "float2nr({float})" function
3726 */
3727 static void
3728f_float2nr(typval_T *argvars, typval_T *rettv)
3729{
3730 float_T f = 0.0;
3731
3732 if (get_float_arg(argvars, &f) == OK)
3733 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003734 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003735 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003736 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003737 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003738 else
3739 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003740 }
3741}
3742
3743/*
3744 * "floor({float})" function
3745 */
3746 static void
3747f_floor(typval_T *argvars, typval_T *rettv)
3748{
3749 float_T f = 0.0;
3750
3751 rettv->v_type = VAR_FLOAT;
3752 if (get_float_arg(argvars, &f) == OK)
3753 rettv->vval.v_float = floor(f);
3754 else
3755 rettv->vval.v_float = 0.0;
3756}
3757
3758/*
3759 * "fmod()" function
3760 */
3761 static void
3762f_fmod(typval_T *argvars, typval_T *rettv)
3763{
3764 float_T fx = 0.0, fy = 0.0;
3765
3766 rettv->v_type = VAR_FLOAT;
3767 if (get_float_arg(argvars, &fx) == OK
3768 && get_float_arg(&argvars[1], &fy) == OK)
3769 rettv->vval.v_float = fmod(fx, fy);
3770 else
3771 rettv->vval.v_float = 0.0;
3772}
3773#endif
3774
3775/*
3776 * "fnameescape({string})" function
3777 */
3778 static void
3779f_fnameescape(typval_T *argvars, typval_T *rettv)
3780{
3781 rettv->vval.v_string = vim_strsave_fnameescape(
3782 get_tv_string(&argvars[0]), FALSE);
3783 rettv->v_type = VAR_STRING;
3784}
3785
3786/*
3787 * "fnamemodify({fname}, {mods})" function
3788 */
3789 static void
3790f_fnamemodify(typval_T *argvars, typval_T *rettv)
3791{
3792 char_u *fname;
3793 char_u *mods;
3794 int usedlen = 0;
3795 int len;
3796 char_u *fbuf = NULL;
3797 char_u buf[NUMBUFLEN];
3798
3799 fname = get_tv_string_chk(&argvars[0]);
3800 mods = get_tv_string_buf_chk(&argvars[1], buf);
3801 if (fname == NULL || mods == NULL)
3802 fname = NULL;
3803 else
3804 {
3805 len = (int)STRLEN(fname);
3806 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
3807 }
3808
3809 rettv->v_type = VAR_STRING;
3810 if (fname == NULL)
3811 rettv->vval.v_string = NULL;
3812 else
3813 rettv->vval.v_string = vim_strnsave(fname, len);
3814 vim_free(fbuf);
3815}
3816
3817static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
3818
3819/*
3820 * "foldclosed()" function
3821 */
3822 static void
3823foldclosed_both(
3824 typval_T *argvars UNUSED,
3825 typval_T *rettv,
3826 int end UNUSED)
3827{
3828#ifdef FEAT_FOLDING
3829 linenr_T lnum;
3830 linenr_T first, last;
3831
3832 lnum = get_tv_lnum(argvars);
3833 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3834 {
3835 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3836 {
3837 if (end)
3838 rettv->vval.v_number = (varnumber_T)last;
3839 else
3840 rettv->vval.v_number = (varnumber_T)first;
3841 return;
3842 }
3843 }
3844#endif
3845 rettv->vval.v_number = -1;
3846}
3847
3848/*
3849 * "foldclosed()" function
3850 */
3851 static void
3852f_foldclosed(typval_T *argvars, typval_T *rettv)
3853{
3854 foldclosed_both(argvars, rettv, FALSE);
3855}
3856
3857/*
3858 * "foldclosedend()" function
3859 */
3860 static void
3861f_foldclosedend(typval_T *argvars, typval_T *rettv)
3862{
3863 foldclosed_both(argvars, rettv, TRUE);
3864}
3865
3866/*
3867 * "foldlevel()" function
3868 */
3869 static void
3870f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3871{
3872#ifdef FEAT_FOLDING
3873 linenr_T lnum;
3874
3875 lnum = get_tv_lnum(argvars);
3876 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3877 rettv->vval.v_number = foldLevel(lnum);
3878#endif
3879}
3880
3881/*
3882 * "foldtext()" function
3883 */
3884 static void
3885f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
3886{
3887#ifdef FEAT_FOLDING
3888 linenr_T foldstart;
3889 linenr_T foldend;
3890 char_u *dashes;
3891 linenr_T lnum;
3892 char_u *s;
3893 char_u *r;
3894 int len;
3895 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003896 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003897#endif
3898
3899 rettv->v_type = VAR_STRING;
3900 rettv->vval.v_string = NULL;
3901#ifdef FEAT_FOLDING
3902 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
3903 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
3904 dashes = get_vim_var_str(VV_FOLDDASHES);
3905 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
3906 && dashes != NULL)
3907 {
3908 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02003909 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003910 if (!linewhite(lnum))
3911 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003912
3913 /* Find interesting text in this line. */
3914 s = skipwhite(ml_get(lnum));
3915 /* skip C comment-start */
3916 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
3917 {
3918 s = skipwhite(s + 2);
3919 if (*skipwhite(s) == NUL
3920 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
3921 {
3922 s = skipwhite(ml_get(lnum + 1));
3923 if (*s == '*')
3924 s = skipwhite(s + 1);
3925 }
3926 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02003927 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003928 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003929 r = alloc((unsigned)(STRLEN(txt)
3930 + STRLEN(dashes) /* for %s */
3931 + 20 /* for %3ld */
3932 + STRLEN(s))); /* concatenated */
3933 if (r != NULL)
3934 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02003935 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003936 len = (int)STRLEN(r);
3937 STRCAT(r, s);
3938 /* remove 'foldmarker' and 'commentstring' */
3939 foldtext_cleanup(r + len);
3940 rettv->vval.v_string = r;
3941 }
3942 }
3943#endif
3944}
3945
3946/*
3947 * "foldtextresult(lnum)" function
3948 */
3949 static void
3950f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
3951{
3952#ifdef FEAT_FOLDING
3953 linenr_T lnum;
3954 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003955 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003956 foldinfo_T foldinfo;
3957 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003958 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003959#endif
3960
3961 rettv->v_type = VAR_STRING;
3962 rettv->vval.v_string = NULL;
3963#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003964 if (entered)
3965 return; /* reject recursive use */
3966 entered = TRUE;
3967
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003968 lnum = get_tv_lnum(argvars);
3969 /* treat illegal types and illegal string values for {lnum} the same */
3970 if (lnum < 0)
3971 lnum = 0;
3972 fold_count = foldedCount(curwin, lnum, &foldinfo);
3973 if (fold_count > 0)
3974 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01003975 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
3976 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003977 if (text == buf)
3978 text = vim_strsave(text);
3979 rettv->vval.v_string = text;
3980 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003981
3982 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003983#endif
3984}
3985
3986/*
3987 * "foreground()" function
3988 */
3989 static void
3990f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3991{
3992#ifdef FEAT_GUI
3993 if (gui.in_use)
3994 gui_mch_set_foreground();
3995#else
3996# ifdef WIN32
3997 win32_set_foreground();
3998# endif
3999#endif
4000}
4001
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004002 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004003common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004004{
4005 char_u *s;
4006 char_u *name;
4007 int use_string = FALSE;
4008 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004009 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004010
4011 if (argvars[0].v_type == VAR_FUNC)
4012 {
4013 /* function(MyFunc, [arg], dict) */
4014 s = argvars[0].vval.v_string;
4015 }
4016 else if (argvars[0].v_type == VAR_PARTIAL
4017 && argvars[0].vval.v_partial != NULL)
4018 {
4019 /* function(dict.MyFunc, [arg]) */
4020 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004021 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004022 }
4023 else
4024 {
4025 /* function('MyFunc', [arg], dict) */
4026 s = get_tv_string(&argvars[0]);
4027 use_string = TRUE;
4028 }
4029
Bram Moolenaar843b8842016-08-21 14:36:15 +02004030 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004031 {
4032 name = s;
4033 trans_name = trans_function_name(&name, FALSE,
4034 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4035 if (*name != NUL)
4036 s = NULL;
4037 }
4038
Bram Moolenaar843b8842016-08-21 14:36:15 +02004039 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4040 || (is_funcref && trans_name == NULL))
Bram Moolenaar5582ef12016-09-14 22:16:13 +02004041 EMSG2(_(e_invarg2), use_string ? get_tv_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004042 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004043 else if (trans_name != NULL && (is_funcref
4044 ? find_func(trans_name) == NULL
4045 : !translated_function_exists(trans_name)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004046 EMSG2(_("E700: Unknown function: %s"), s);
4047 else
4048 {
4049 int dict_idx = 0;
4050 int arg_idx = 0;
4051 list_T *list = NULL;
4052
4053 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4054 {
4055 char sid_buf[25];
4056 int off = *s == 's' ? 2 : 5;
4057
4058 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4059 * also be called from another script. Using trans_function_name()
4060 * would also work, but some plugins depend on the name being
4061 * printable text. */
4062 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
4063 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
4064 if (name != NULL)
4065 {
4066 STRCPY(name, sid_buf);
4067 STRCAT(name, s + off);
4068 }
4069 }
4070 else
4071 name = vim_strsave(s);
4072
4073 if (argvars[1].v_type != VAR_UNKNOWN)
4074 {
4075 if (argvars[2].v_type != VAR_UNKNOWN)
4076 {
4077 /* function(name, [args], dict) */
4078 arg_idx = 1;
4079 dict_idx = 2;
4080 }
4081 else if (argvars[1].v_type == VAR_DICT)
4082 /* function(name, dict) */
4083 dict_idx = 1;
4084 else
4085 /* function(name, [args]) */
4086 arg_idx = 1;
4087 if (dict_idx > 0)
4088 {
4089 if (argvars[dict_idx].v_type != VAR_DICT)
4090 {
4091 EMSG(_("E922: expected a dict"));
4092 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004093 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004094 }
4095 if (argvars[dict_idx].vval.v_dict == NULL)
4096 dict_idx = 0;
4097 }
4098 if (arg_idx > 0)
4099 {
4100 if (argvars[arg_idx].v_type != VAR_LIST)
4101 {
4102 EMSG(_("E923: Second argument of function() must be a list or a dict"));
4103 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004104 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004105 }
4106 list = argvars[arg_idx].vval.v_list;
4107 if (list == NULL || list->lv_len == 0)
4108 arg_idx = 0;
4109 }
4110 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004111 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004112 {
4113 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
4114
4115 /* result is a VAR_PARTIAL */
4116 if (pt == NULL)
4117 vim_free(name);
4118 else
4119 {
4120 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4121 {
4122 listitem_T *li;
4123 int i = 0;
4124 int arg_len = 0;
4125 int lv_len = 0;
4126
4127 if (arg_pt != NULL)
4128 arg_len = arg_pt->pt_argc;
4129 if (list != NULL)
4130 lv_len = list->lv_len;
4131 pt->pt_argc = arg_len + lv_len;
4132 pt->pt_argv = (typval_T *)alloc(
4133 sizeof(typval_T) * pt->pt_argc);
4134 if (pt->pt_argv == NULL)
4135 {
4136 vim_free(pt);
4137 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004138 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004139 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004140 for (i = 0; i < arg_len; i++)
4141 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4142 if (lv_len > 0)
4143 for (li = list->lv_first; li != NULL;
4144 li = li->li_next)
4145 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004146 }
4147
4148 /* For "function(dict.func, [], dict)" and "func" is a partial
4149 * use "dict". That is backwards compatible. */
4150 if (dict_idx > 0)
4151 {
4152 /* The dict is bound explicitly, pt_auto is FALSE. */
4153 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4154 ++pt->pt_dict->dv_refcount;
4155 }
4156 else if (arg_pt != NULL)
4157 {
4158 /* If the dict was bound automatically the result is also
4159 * bound automatically. */
4160 pt->pt_dict = arg_pt->pt_dict;
4161 pt->pt_auto = arg_pt->pt_auto;
4162 if (pt->pt_dict != NULL)
4163 ++pt->pt_dict->dv_refcount;
4164 }
4165
4166 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004167 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4168 {
4169 pt->pt_func = arg_pt->pt_func;
4170 func_ptr_ref(pt->pt_func);
4171 vim_free(name);
4172 }
4173 else if (is_funcref)
4174 {
4175 pt->pt_func = find_func(trans_name);
4176 func_ptr_ref(pt->pt_func);
4177 vim_free(name);
4178 }
4179 else
4180 {
4181 pt->pt_name = name;
4182 func_ref(name);
4183 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004184 }
4185 rettv->v_type = VAR_PARTIAL;
4186 rettv->vval.v_partial = pt;
4187 }
4188 else
4189 {
4190 /* result is a VAR_FUNC */
4191 rettv->v_type = VAR_FUNC;
4192 rettv->vval.v_string = name;
4193 func_ref(name);
4194 }
4195 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004196theend:
4197 vim_free(trans_name);
4198}
4199
4200/*
4201 * "funcref()" function
4202 */
4203 static void
4204f_funcref(typval_T *argvars, typval_T *rettv)
4205{
4206 common_function(argvars, rettv, TRUE);
4207}
4208
4209/*
4210 * "function()" function
4211 */
4212 static void
4213f_function(typval_T *argvars, typval_T *rettv)
4214{
4215 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004216}
4217
4218/*
4219 * "garbagecollect()" function
4220 */
4221 static void
4222f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4223{
4224 /* This is postponed until we are back at the toplevel, because we may be
4225 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4226 want_garbage_collect = TRUE;
4227
4228 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
4229 garbage_collect_at_exit = TRUE;
4230}
4231
4232/*
4233 * "get()" function
4234 */
4235 static void
4236f_get(typval_T *argvars, typval_T *rettv)
4237{
4238 listitem_T *li;
4239 list_T *l;
4240 dictitem_T *di;
4241 dict_T *d;
4242 typval_T *tv = NULL;
4243
4244 if (argvars[0].v_type == VAR_LIST)
4245 {
4246 if ((l = argvars[0].vval.v_list) != NULL)
4247 {
4248 int error = FALSE;
4249
4250 li = list_find(l, (long)get_tv_number_chk(&argvars[1], &error));
4251 if (!error && li != NULL)
4252 tv = &li->li_tv;
4253 }
4254 }
4255 else if (argvars[0].v_type == VAR_DICT)
4256 {
4257 if ((d = argvars[0].vval.v_dict) != NULL)
4258 {
4259 di = dict_find(d, get_tv_string(&argvars[1]), -1);
4260 if (di != NULL)
4261 tv = &di->di_tv;
4262 }
4263 }
4264 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4265 {
4266 partial_T *pt;
4267 partial_T fref_pt;
4268
4269 if (argvars[0].v_type == VAR_PARTIAL)
4270 pt = argvars[0].vval.v_partial;
4271 else
4272 {
4273 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4274 fref_pt.pt_name = argvars[0].vval.v_string;
4275 pt = &fref_pt;
4276 }
4277
4278 if (pt != NULL)
4279 {
4280 char_u *what = get_tv_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004281 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004282
4283 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4284 {
4285 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004286 n = partial_name(pt);
4287 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004288 rettv->vval.v_string = NULL;
4289 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004290 {
4291 rettv->vval.v_string = vim_strsave(n);
4292 if (rettv->v_type == VAR_FUNC)
4293 func_ref(rettv->vval.v_string);
4294 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004295 }
4296 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004297 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004298 else if (STRCMP(what, "args") == 0)
4299 {
4300 rettv->v_type = VAR_LIST;
4301 if (rettv_list_alloc(rettv) == OK)
4302 {
4303 int i;
4304
4305 for (i = 0; i < pt->pt_argc; ++i)
4306 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4307 }
4308 }
4309 else
4310 EMSG2(_(e_invarg2), what);
4311 return;
4312 }
4313 }
4314 else
4315 EMSG2(_(e_listdictarg), "get()");
4316
4317 if (tv == NULL)
4318 {
4319 if (argvars[2].v_type != VAR_UNKNOWN)
4320 copy_tv(&argvars[2], rettv);
4321 }
4322 else
4323 copy_tv(tv, rettv);
4324}
4325
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004326#ifdef FEAT_SIGNS
4327/*
4328 * Returns information about signs placed in a buffer as list of dicts.
4329 */
4330 static void
4331get_buffer_signs(buf_T *buf, list_T *l)
4332{
4333 signlist_T *sign;
4334
4335 for (sign = buf->b_signlist; sign; sign = sign->next)
4336 {
4337 dict_T *d = dict_alloc();
4338
4339 if (d != NULL)
4340 {
4341 dict_add_nr_str(d, "id", sign->id, NULL);
4342 dict_add_nr_str(d, "lnum", sign->lnum, NULL);
Bram Moolenaar6a402ed2016-08-28 14:11:24 +02004343 dict_add_nr_str(d, "name", 0L, sign_typenr2name(sign->typenr));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004344
4345 list_append_dict(l, d);
4346 }
4347 }
4348}
4349#endif
4350
4351/*
4352 * Returns buffer options, variables and other attributes in a dictionary.
4353 */
4354 static dict_T *
4355get_buffer_info(buf_T *buf)
4356{
4357 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004358 tabpage_T *tp;
4359 win_T *wp;
4360 list_T *windows;
4361
4362 dict = dict_alloc();
4363 if (dict == NULL)
4364 return NULL;
4365
Bram Moolenaar33928832016-08-18 21:22:04 +02004366 dict_add_nr_str(dict, "bufnr", buf->b_fnum, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004367 dict_add_nr_str(dict, "name", 0L,
4368 buf->b_ffname != NULL ? buf->b_ffname : (char_u *)"");
Bram Moolenaarf845b872017-01-06 14:04:54 +01004369 dict_add_nr_str(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4370 : buflist_findlnum(buf), NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004371 dict_add_nr_str(dict, "loaded", buf->b_ml.ml_mfp != NULL, NULL);
4372 dict_add_nr_str(dict, "listed", buf->b_p_bl, NULL);
4373 dict_add_nr_str(dict, "changed", bufIsChanged(buf), NULL);
Bram Moolenaar95c526e2017-02-25 14:59:34 +01004374 dict_add_nr_str(dict, "changedtick", CHANGEDTICK(buf), NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004375 dict_add_nr_str(dict, "hidden",
4376 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0,
4377 NULL);
4378
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004379 /* Get a reference to buffer variables */
4380 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004381
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004382 /* List of windows displaying this buffer */
4383 windows = list_alloc();
4384 if (windows != NULL)
4385 {
4386 FOR_ALL_TAB_WINDOWS(tp, wp)
4387 if (wp->w_buffer == buf)
4388 list_append_number(windows, (varnumber_T)wp->w_id);
4389 dict_add_list(dict, "windows", windows);
4390 }
4391
4392#ifdef FEAT_SIGNS
4393 if (buf->b_signlist != NULL)
4394 {
4395 /* List of signs placed in this buffer */
4396 list_T *signs = list_alloc();
4397 if (signs != NULL)
4398 {
4399 get_buffer_signs(buf, signs);
4400 dict_add_list(dict, "signs", signs);
4401 }
4402 }
4403#endif
4404
4405 return dict;
4406}
4407
4408/*
4409 * "getbufinfo()" function
4410 */
4411 static void
4412f_getbufinfo(typval_T *argvars, typval_T *rettv)
4413{
4414 buf_T *buf = NULL;
4415 buf_T *argbuf = NULL;
4416 dict_T *d;
4417 int filtered = FALSE;
4418 int sel_buflisted = FALSE;
4419 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004420 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004421
4422 if (rettv_list_alloc(rettv) != OK)
4423 return;
4424
4425 /* List of all the buffers or selected buffers */
4426 if (argvars[0].v_type == VAR_DICT)
4427 {
4428 dict_T *sel_d = argvars[0].vval.v_dict;
4429
4430 if (sel_d != NULL)
4431 {
4432 dictitem_T *di;
4433
4434 filtered = TRUE;
4435
4436 di = dict_find(sel_d, (char_u *)"buflisted", -1);
4437 if (di != NULL && get_tv_number(&di->di_tv))
4438 sel_buflisted = TRUE;
4439
4440 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
4441 if (di != NULL && get_tv_number(&di->di_tv))
4442 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004443
4444 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
4445 if (di != NULL && get_tv_number(&di->di_tv))
4446 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004447 }
4448 }
4449 else if (argvars[0].v_type != VAR_UNKNOWN)
4450 {
4451 /* Information about one buffer. Argument specifies the buffer */
4452 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4453 ++emsg_off;
4454 argbuf = get_buf_tv(&argvars[0], FALSE);
4455 --emsg_off;
4456 if (argbuf == NULL)
4457 return;
4458 }
4459
4460 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004461 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004462 {
4463 if (argbuf != NULL && argbuf != buf)
4464 continue;
4465 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004466 || (sel_buflisted && !buf->b_p_bl)
4467 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004468 continue;
4469
4470 d = get_buffer_info(buf);
4471 if (d != NULL)
4472 list_append_dict(rettv->vval.v_list, d);
4473 if (argbuf != NULL)
4474 return;
4475 }
4476}
4477
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004478static void get_buffer_lines(buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv);
4479
4480/*
4481 * Get line or list of lines from buffer "buf" into "rettv".
4482 * Return a range (from start to end) of lines in rettv from the specified
4483 * buffer.
4484 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4485 */
4486 static void
4487get_buffer_lines(
4488 buf_T *buf,
4489 linenr_T start,
4490 linenr_T end,
4491 int retlist,
4492 typval_T *rettv)
4493{
4494 char_u *p;
4495
4496 rettv->v_type = VAR_STRING;
4497 rettv->vval.v_string = NULL;
4498 if (retlist && rettv_list_alloc(rettv) == FAIL)
4499 return;
4500
4501 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4502 return;
4503
4504 if (!retlist)
4505 {
4506 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4507 p = ml_get_buf(buf, start, FALSE);
4508 else
4509 p = (char_u *)"";
4510 rettv->vval.v_string = vim_strsave(p);
4511 }
4512 else
4513 {
4514 if (end < start)
4515 return;
4516
4517 if (start < 1)
4518 start = 1;
4519 if (end > buf->b_ml.ml_line_count)
4520 end = buf->b_ml.ml_line_count;
4521 while (start <= end)
4522 if (list_append_string(rettv->vval.v_list,
4523 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4524 break;
4525 }
4526}
4527
4528/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004529 * "getbufline()" function
4530 */
4531 static void
4532f_getbufline(typval_T *argvars, typval_T *rettv)
4533{
4534 linenr_T lnum;
4535 linenr_T end;
4536 buf_T *buf;
4537
4538 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4539 ++emsg_off;
4540 buf = get_buf_tv(&argvars[0], FALSE);
4541 --emsg_off;
4542
4543 lnum = get_tv_lnum_buf(&argvars[1], buf);
4544 if (argvars[2].v_type == VAR_UNKNOWN)
4545 end = lnum;
4546 else
4547 end = get_tv_lnum_buf(&argvars[2], buf);
4548
4549 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4550}
4551
4552/*
4553 * "getbufvar()" function
4554 */
4555 static void
4556f_getbufvar(typval_T *argvars, typval_T *rettv)
4557{
4558 buf_T *buf;
4559 buf_T *save_curbuf;
4560 char_u *varname;
4561 dictitem_T *v;
4562 int done = FALSE;
4563
4564 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4565 varname = get_tv_string_chk(&argvars[1]);
4566 ++emsg_off;
4567 buf = get_buf_tv(&argvars[0], FALSE);
4568
4569 rettv->v_type = VAR_STRING;
4570 rettv->vval.v_string = NULL;
4571
4572 if (buf != NULL && varname != NULL)
4573 {
4574 /* set curbuf to be our buf, temporarily */
4575 save_curbuf = curbuf;
4576 curbuf = buf;
4577
Bram Moolenaar30567352016-08-27 21:25:44 +02004578 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004579 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004580 if (varname[1] == NUL)
4581 {
4582 /* get all buffer-local options in a dict */
4583 dict_T *opts = get_winbuf_options(TRUE);
4584
4585 if (opts != NULL)
4586 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004587 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004588 done = TRUE;
4589 }
4590 }
4591 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4592 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004593 done = TRUE;
4594 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004595 else
4596 {
4597 /* Look up the variable. */
4598 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4599 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4600 'b', varname, FALSE);
4601 if (v != NULL)
4602 {
4603 copy_tv(&v->di_tv, rettv);
4604 done = TRUE;
4605 }
4606 }
4607
4608 /* restore previous notion of curbuf */
4609 curbuf = save_curbuf;
4610 }
4611
4612 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4613 /* use the default value */
4614 copy_tv(&argvars[2], rettv);
4615
4616 --emsg_off;
4617}
4618
4619/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004620 * "getchangelist()" function
4621 */
4622 static void
4623f_getchangelist(typval_T *argvars, typval_T *rettv)
4624{
4625#ifdef FEAT_JUMPLIST
4626 buf_T *buf;
4627 int i;
4628 list_T *l;
4629 dict_T *d;
4630#endif
4631
4632 if (rettv_list_alloc(rettv) != OK)
4633 return;
4634
4635#ifdef FEAT_JUMPLIST
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004636 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4637 ++emsg_off;
4638 buf = get_buf_tv(&argvars[0], FALSE);
4639 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004640 if (buf == NULL)
4641 return;
4642
4643 l = list_alloc();
4644 if (l == NULL)
4645 return;
4646
4647 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4648 return;
4649 /*
4650 * The current window change list index tracks only the position in the
4651 * current buffer change list. For other buffers, use the change list
4652 * length as the current index.
4653 */
4654 list_append_number(rettv->vval.v_list,
4655 (varnumber_T)((buf == curwin->w_buffer)
4656 ? curwin->w_changelistidx : buf->b_changelistlen));
4657
4658 for (i = 0; i < buf->b_changelistlen; ++i)
4659 {
4660 if (buf->b_changelist[i].lnum == 0)
4661 continue;
4662 if ((d = dict_alloc()) == NULL)
4663 return;
4664 if (list_append_dict(l, d) == FAIL)
4665 return;
4666 dict_add_nr_str(d, "lnum", (long)buf->b_changelist[i].lnum, NULL);
4667 dict_add_nr_str(d, "col", (long)buf->b_changelist[i].col, NULL);
4668# ifdef FEAT_VIRTUALEDIT
4669 dict_add_nr_str(d, "coladd", (long)buf->b_changelist[i].coladd, NULL);
4670# endif
4671 }
4672#endif
4673}
4674/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004675 * "getchar()" function
4676 */
4677 static void
4678f_getchar(typval_T *argvars, typval_T *rettv)
4679{
4680 varnumber_T n;
4681 int error = FALSE;
4682
4683 /* Position the cursor. Needed after a message that ends in a space. */
4684 windgoto(msg_row, msg_col);
4685
4686 ++no_mapping;
4687 ++allow_keys;
4688 for (;;)
4689 {
4690 if (argvars[0].v_type == VAR_UNKNOWN)
4691 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004692 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004693 else if (get_tv_number_chk(&argvars[0], &error) == 1)
4694 /* getchar(1): only check if char avail */
4695 n = vpeekc_any();
4696 else if (error || vpeekc_any() == NUL)
4697 /* illegal argument or getchar(0) and no char avail: return zero */
4698 n = 0;
4699 else
4700 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004701 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004702
4703 if (n == K_IGNORE)
4704 continue;
4705 break;
4706 }
4707 --no_mapping;
4708 --allow_keys;
4709
4710 set_vim_var_nr(VV_MOUSE_WIN, 0);
4711 set_vim_var_nr(VV_MOUSE_WINID, 0);
4712 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4713 set_vim_var_nr(VV_MOUSE_COL, 0);
4714
4715 rettv->vval.v_number = n;
4716 if (IS_SPECIAL(n) || mod_mask != 0)
4717 {
4718 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4719 int i = 0;
4720
4721 /* Turn a special key into three bytes, plus modifier. */
4722 if (mod_mask != 0)
4723 {
4724 temp[i++] = K_SPECIAL;
4725 temp[i++] = KS_MODIFIER;
4726 temp[i++] = mod_mask;
4727 }
4728 if (IS_SPECIAL(n))
4729 {
4730 temp[i++] = K_SPECIAL;
4731 temp[i++] = K_SECOND(n);
4732 temp[i++] = K_THIRD(n);
4733 }
4734#ifdef FEAT_MBYTE
4735 else if (has_mbyte)
4736 i += (*mb_char2bytes)(n, temp + i);
4737#endif
4738 else
4739 temp[i++] = n;
4740 temp[i++] = NUL;
4741 rettv->v_type = VAR_STRING;
4742 rettv->vval.v_string = vim_strsave(temp);
4743
4744#ifdef FEAT_MOUSE
4745 if (is_mouse_key(n))
4746 {
4747 int row = mouse_row;
4748 int col = mouse_col;
4749 win_T *win;
4750 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004751 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004752 int winnr = 1;
4753
4754 if (row >= 0 && col >= 0)
4755 {
4756 /* Find the window at the mouse coordinates and compute the
4757 * text position. */
4758 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004759 if (win == NULL)
4760 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004761 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004762 for (wp = firstwin; wp != win; wp = wp->w_next)
4763 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004764 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4765 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4766 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4767 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4768 }
4769 }
4770#endif
4771 }
4772}
4773
4774/*
4775 * "getcharmod()" function
4776 */
4777 static void
4778f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4779{
4780 rettv->vval.v_number = mod_mask;
4781}
4782
4783/*
4784 * "getcharsearch()" function
4785 */
4786 static void
4787f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4788{
4789 if (rettv_dict_alloc(rettv) != FAIL)
4790 {
4791 dict_T *dict = rettv->vval.v_dict;
4792
4793 dict_add_nr_str(dict, "char", 0L, last_csearch());
4794 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
4795 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
4796 }
4797}
4798
4799/*
4800 * "getcmdline()" function
4801 */
4802 static void
4803f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4804{
4805 rettv->v_type = VAR_STRING;
4806 rettv->vval.v_string = get_cmdline_str();
4807}
4808
4809/*
4810 * "getcmdpos()" function
4811 */
4812 static void
4813f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4814{
4815 rettv->vval.v_number = get_cmdline_pos() + 1;
4816}
4817
4818/*
4819 * "getcmdtype()" function
4820 */
4821 static void
4822f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4823{
4824 rettv->v_type = VAR_STRING;
4825 rettv->vval.v_string = alloc(2);
4826 if (rettv->vval.v_string != NULL)
4827 {
4828 rettv->vval.v_string[0] = get_cmdline_type();
4829 rettv->vval.v_string[1] = NUL;
4830 }
4831}
4832
4833/*
4834 * "getcmdwintype()" function
4835 */
4836 static void
4837f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4838{
4839 rettv->v_type = VAR_STRING;
4840 rettv->vval.v_string = NULL;
4841#ifdef FEAT_CMDWIN
4842 rettv->vval.v_string = alloc(2);
4843 if (rettv->vval.v_string != NULL)
4844 {
4845 rettv->vval.v_string[0] = cmdwin_type;
4846 rettv->vval.v_string[1] = NUL;
4847 }
4848#endif
4849}
4850
4851#if defined(FEAT_CMDL_COMPL)
4852/*
4853 * "getcompletion()" function
4854 */
4855 static void
4856f_getcompletion(typval_T *argvars, typval_T *rettv)
4857{
4858 char_u *pat;
4859 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004860 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004861 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
4862 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004863
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004864 if (argvars[2].v_type != VAR_UNKNOWN)
4865 filtered = get_tv_number_chk(&argvars[2], NULL);
4866
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004867 if (p_wic)
4868 options |= WILD_ICASE;
4869
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004870 /* For filtered results, 'wildignore' is used */
4871 if (!filtered)
4872 options |= WILD_KEEP_ALL;
4873
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004874 ExpandInit(&xpc);
4875 xpc.xp_pattern = get_tv_string(&argvars[0]);
4876 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4877 xpc.xp_context = cmdcomplete_str_to_type(get_tv_string(&argvars[1]));
4878 if (xpc.xp_context == EXPAND_NOTHING)
4879 {
4880 if (argvars[1].v_type == VAR_STRING)
4881 EMSG2(_(e_invarg2), argvars[1].vval.v_string);
4882 else
4883 EMSG(_(e_invarg));
4884 return;
4885 }
4886
4887# if defined(FEAT_MENU)
4888 if (xpc.xp_context == EXPAND_MENUS)
4889 {
4890 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4891 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4892 }
4893# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02004894#ifdef FEAT_CSCOPE
4895 if (xpc.xp_context == EXPAND_CSCOPE)
4896 {
4897 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4898 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4899 }
4900#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02004901#ifdef FEAT_SIGNS
4902 if (xpc.xp_context == EXPAND_SIGN)
4903 {
4904 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4905 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4906 }
4907#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004908
4909 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4910 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
4911 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004912 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004913
4914 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4915
4916 for (i = 0; i < xpc.xp_numfiles; i++)
4917 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4918 }
4919 vim_free(pat);
4920 ExpandCleanup(&xpc);
4921}
4922#endif
4923
4924/*
4925 * "getcwd()" function
4926 */
4927 static void
4928f_getcwd(typval_T *argvars, typval_T *rettv)
4929{
4930 win_T *wp = NULL;
4931 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01004932 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004933
4934 rettv->v_type = VAR_STRING;
4935 rettv->vval.v_string = NULL;
4936
Bram Moolenaar54591292018-02-09 20:53:59 +01004937 if (argvars[0].v_type == VAR_NUMBER && argvars[0].vval.v_number == -1)
4938 global = TRUE;
4939 else
4940 wp = find_tabwin(&argvars[0], &argvars[1]);
4941
4942 if (wp != NULL && wp->w_localdir != NULL)
4943 rettv->vval.v_string = vim_strsave(wp->w_localdir);
4944 else if (wp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004945 {
Bram Moolenaar54591292018-02-09 20:53:59 +01004946 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004947 rettv->vval.v_string = vim_strsave(globaldir);
4948 else
4949 {
4950 cwd = alloc(MAXPATHL);
4951 if (cwd != NULL)
4952 {
4953 if (mch_dirname(cwd, MAXPATHL) != FAIL)
4954 rettv->vval.v_string = vim_strsave(cwd);
4955 vim_free(cwd);
4956 }
4957 }
4958#ifdef BACKSLASH_IN_FILENAME
4959 if (rettv->vval.v_string != NULL)
4960 slash_adjust(rettv->vval.v_string);
4961#endif
4962 }
4963}
4964
4965/*
4966 * "getfontname()" function
4967 */
4968 static void
4969f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
4970{
4971 rettv->v_type = VAR_STRING;
4972 rettv->vval.v_string = NULL;
4973#ifdef FEAT_GUI
4974 if (gui.in_use)
4975 {
4976 GuiFont font;
4977 char_u *name = NULL;
4978
4979 if (argvars[0].v_type == VAR_UNKNOWN)
4980 {
4981 /* Get the "Normal" font. Either the name saved by
4982 * hl_set_font_name() or from the font ID. */
4983 font = gui.norm_font;
4984 name = hl_get_font_name();
4985 }
4986 else
4987 {
4988 name = get_tv_string(&argvars[0]);
4989 if (STRCMP(name, "*") == 0) /* don't use font dialog */
4990 return;
4991 font = gui_mch_get_font(name, FALSE);
4992 if (font == NOFONT)
4993 return; /* Invalid font name, return empty string. */
4994 }
4995 rettv->vval.v_string = gui_mch_get_fontname(font, name);
4996 if (argvars[0].v_type != VAR_UNKNOWN)
4997 gui_mch_free_font(font);
4998 }
4999#endif
5000}
5001
5002/*
5003 * "getfperm({fname})" function
5004 */
5005 static void
5006f_getfperm(typval_T *argvars, typval_T *rettv)
5007{
5008 char_u *fname;
5009 stat_T st;
5010 char_u *perm = NULL;
5011 char_u flags[] = "rwx";
5012 int i;
5013
5014 fname = get_tv_string(&argvars[0]);
5015
5016 rettv->v_type = VAR_STRING;
5017 if (mch_stat((char *)fname, &st) >= 0)
5018 {
5019 perm = vim_strsave((char_u *)"---------");
5020 if (perm != NULL)
5021 {
5022 for (i = 0; i < 9; i++)
5023 {
5024 if (st.st_mode & (1 << (8 - i)))
5025 perm[i] = flags[i % 3];
5026 }
5027 }
5028 }
5029 rettv->vval.v_string = perm;
5030}
5031
5032/*
5033 * "getfsize({fname})" function
5034 */
5035 static void
5036f_getfsize(typval_T *argvars, typval_T *rettv)
5037{
5038 char_u *fname;
5039 stat_T st;
5040
5041 fname = get_tv_string(&argvars[0]);
5042
5043 rettv->v_type = VAR_NUMBER;
5044
5045 if (mch_stat((char *)fname, &st) >= 0)
5046 {
5047 if (mch_isdir(fname))
5048 rettv->vval.v_number = 0;
5049 else
5050 {
5051 rettv->vval.v_number = (varnumber_T)st.st_size;
5052
5053 /* non-perfect check for overflow */
5054 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5055 rettv->vval.v_number = -2;
5056 }
5057 }
5058 else
5059 rettv->vval.v_number = -1;
5060}
5061
5062/*
5063 * "getftime({fname})" function
5064 */
5065 static void
5066f_getftime(typval_T *argvars, typval_T *rettv)
5067{
5068 char_u *fname;
5069 stat_T st;
5070
5071 fname = get_tv_string(&argvars[0]);
5072
5073 if (mch_stat((char *)fname, &st) >= 0)
5074 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5075 else
5076 rettv->vval.v_number = -1;
5077}
5078
5079/*
5080 * "getftype({fname})" function
5081 */
5082 static void
5083f_getftype(typval_T *argvars, typval_T *rettv)
5084{
5085 char_u *fname;
5086 stat_T st;
5087 char_u *type = NULL;
5088 char *t;
5089
5090 fname = get_tv_string(&argvars[0]);
5091
5092 rettv->v_type = VAR_STRING;
5093 if (mch_lstat((char *)fname, &st) >= 0)
5094 {
5095#ifdef S_ISREG
5096 if (S_ISREG(st.st_mode))
5097 t = "file";
5098 else if (S_ISDIR(st.st_mode))
5099 t = "dir";
5100# ifdef S_ISLNK
5101 else if (S_ISLNK(st.st_mode))
5102 t = "link";
5103# endif
5104# ifdef S_ISBLK
5105 else if (S_ISBLK(st.st_mode))
5106 t = "bdev";
5107# endif
5108# ifdef S_ISCHR
5109 else if (S_ISCHR(st.st_mode))
5110 t = "cdev";
5111# endif
5112# ifdef S_ISFIFO
5113 else if (S_ISFIFO(st.st_mode))
5114 t = "fifo";
5115# endif
5116# ifdef S_ISSOCK
5117 else if (S_ISSOCK(st.st_mode))
5118 t = "fifo";
5119# endif
5120 else
5121 t = "other";
5122#else
5123# ifdef S_IFMT
5124 switch (st.st_mode & S_IFMT)
5125 {
5126 case S_IFREG: t = "file"; break;
5127 case S_IFDIR: t = "dir"; break;
5128# ifdef S_IFLNK
5129 case S_IFLNK: t = "link"; break;
5130# endif
5131# ifdef S_IFBLK
5132 case S_IFBLK: t = "bdev"; break;
5133# endif
5134# ifdef S_IFCHR
5135 case S_IFCHR: t = "cdev"; break;
5136# endif
5137# ifdef S_IFIFO
5138 case S_IFIFO: t = "fifo"; break;
5139# endif
5140# ifdef S_IFSOCK
5141 case S_IFSOCK: t = "socket"; break;
5142# endif
5143 default: t = "other";
5144 }
5145# else
5146 if (mch_isdir(fname))
5147 t = "dir";
5148 else
5149 t = "file";
5150# endif
5151#endif
5152 type = vim_strsave((char_u *)t);
5153 }
5154 rettv->vval.v_string = type;
5155}
5156
5157/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005158 * "getjumplist()" function
5159 */
5160 static void
5161f_getjumplist(typval_T *argvars, typval_T *rettv)
5162{
5163#ifdef FEAT_JUMPLIST
5164 win_T *wp;
5165 int i;
5166 list_T *l;
5167 dict_T *d;
5168#endif
5169
5170 if (rettv_list_alloc(rettv) != OK)
5171 return;
5172
5173#ifdef FEAT_JUMPLIST
5174 wp = find_tabwin(&argvars[0], &argvars[1]);
5175 if (wp == NULL)
5176 return;
5177
5178 l = list_alloc();
5179 if (l == NULL)
5180 return;
5181
5182 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5183 return;
5184 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5185
Bram Moolenaar48679742018-02-13 13:33:29 +01005186 cleanup_jumplist(wp, TRUE);
5187
Bram Moolenaar4f505882018-02-10 21:06:32 +01005188 for (i = 0; i < wp->w_jumplistlen; ++i)
5189 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005190 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5191 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005192 if ((d = dict_alloc()) == NULL)
5193 return;
5194 if (list_append_dict(l, d) == FAIL)
5195 return;
5196 dict_add_nr_str(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum,
5197 NULL);
5198 dict_add_nr_str(d, "col", (long)wp->w_jumplist[i].fmark.mark.col,
5199 NULL);
5200# ifdef FEAT_VIRTUALEDIT
5201 dict_add_nr_str(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd,
5202 NULL);
5203# endif
5204 dict_add_nr_str(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum, NULL);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005205 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaar4f505882018-02-10 21:06:32 +01005206 dict_add_nr_str(d, "filename", 0L, wp->w_jumplist[i].fname);
5207 }
5208#endif
5209}
5210
5211/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005212 * "getline(lnum, [end])" function
5213 */
5214 static void
5215f_getline(typval_T *argvars, typval_T *rettv)
5216{
5217 linenr_T lnum;
5218 linenr_T end;
5219 int retlist;
5220
5221 lnum = get_tv_lnum(argvars);
5222 if (argvars[1].v_type == VAR_UNKNOWN)
5223 {
5224 end = 0;
5225 retlist = FALSE;
5226 }
5227 else
5228 {
5229 end = get_tv_lnum(&argvars[1]);
5230 retlist = TRUE;
5231 }
5232
5233 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5234}
5235
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005236#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005237 static void
5238get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5239{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005240 if (what_arg->v_type == VAR_UNKNOWN)
5241 {
5242 if (rettv_list_alloc(rettv) == OK)
5243 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005244 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005245 }
5246 else
5247 {
5248 if (rettv_dict_alloc(rettv) == OK)
5249 if (is_qf || (wp != NULL))
5250 {
5251 if (what_arg->v_type == VAR_DICT)
5252 {
5253 dict_T *d = what_arg->vval.v_dict;
5254
5255 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005256 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005257 }
5258 else
5259 EMSG(_(e_dictreq));
5260 }
5261 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005262}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005263#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005264
5265/*
5266 * "getloclist()" function
5267 */
5268 static void
5269f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5270{
5271#ifdef FEAT_QUICKFIX
5272 win_T *wp;
5273
5274 wp = find_win_by_nr(&argvars[0], NULL);
5275 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5276#endif
5277}
5278
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005279/*
5280 * "getmatches()" function
5281 */
5282 static void
5283f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5284{
5285#ifdef FEAT_SEARCH_EXTRA
5286 dict_T *dict;
5287 matchitem_T *cur = curwin->w_match_head;
5288 int i;
5289
5290 if (rettv_list_alloc(rettv) == OK)
5291 {
5292 while (cur != NULL)
5293 {
5294 dict = dict_alloc();
5295 if (dict == NULL)
5296 return;
5297 if (cur->match.regprog == NULL)
5298 {
5299 /* match added with matchaddpos() */
5300 for (i = 0; i < MAXPOSMATCH; ++i)
5301 {
5302 llpos_T *llpos;
5303 char buf[6];
5304 list_T *l;
5305
5306 llpos = &cur->pos.pos[i];
5307 if (llpos->lnum == 0)
5308 break;
5309 l = list_alloc();
5310 if (l == NULL)
5311 break;
5312 list_append_number(l, (varnumber_T)llpos->lnum);
5313 if (llpos->col > 0)
5314 {
5315 list_append_number(l, (varnumber_T)llpos->col);
5316 list_append_number(l, (varnumber_T)llpos->len);
5317 }
5318 sprintf(buf, "pos%d", i + 1);
5319 dict_add_list(dict, buf, l);
5320 }
5321 }
5322 else
5323 {
5324 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
5325 }
5326 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
5327 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
5328 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
5329# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
5330 if (cur->conceal_char)
5331 {
5332 char_u buf[MB_MAXBYTES + 1];
5333
5334 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
5335 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
5336 }
5337# endif
5338 list_append_dict(rettv->vval.v_list, dict);
5339 cur = cur->next;
5340 }
5341 }
5342#endif
5343}
5344
5345/*
5346 * "getpid()" function
5347 */
5348 static void
5349f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5350{
5351 rettv->vval.v_number = mch_get_pid();
5352}
5353
5354 static void
5355getpos_both(
5356 typval_T *argvars,
5357 typval_T *rettv,
5358 int getcurpos)
5359{
5360 pos_T *fp;
5361 list_T *l;
5362 int fnum = -1;
5363
5364 if (rettv_list_alloc(rettv) == OK)
5365 {
5366 l = rettv->vval.v_list;
5367 if (getcurpos)
5368 fp = &curwin->w_cursor;
5369 else
5370 fp = var2fpos(&argvars[0], TRUE, &fnum);
5371 if (fnum != -1)
5372 list_append_number(l, (varnumber_T)fnum);
5373 else
5374 list_append_number(l, (varnumber_T)0);
5375 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5376 : (varnumber_T)0);
5377 list_append_number(l, (fp != NULL)
5378 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5379 : (varnumber_T)0);
5380 list_append_number(l,
5381#ifdef FEAT_VIRTUALEDIT
5382 (fp != NULL) ? (varnumber_T)fp->coladd :
5383#endif
5384 (varnumber_T)0);
5385 if (getcurpos)
5386 {
5387 update_curswant();
5388 list_append_number(l, curwin->w_curswant == MAXCOL ?
5389 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
5390 }
5391 }
5392 else
5393 rettv->vval.v_number = FALSE;
5394}
5395
5396
5397/*
5398 * "getcurpos()" function
5399 */
5400 static void
5401f_getcurpos(typval_T *argvars, typval_T *rettv)
5402{
5403 getpos_both(argvars, rettv, TRUE);
5404}
5405
5406/*
5407 * "getpos(string)" function
5408 */
5409 static void
5410f_getpos(typval_T *argvars, typval_T *rettv)
5411{
5412 getpos_both(argvars, rettv, FALSE);
5413}
5414
5415/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005416 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005417 */
5418 static void
5419f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5420{
5421#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005422 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005423#endif
5424}
5425
5426/*
5427 * "getreg()" function
5428 */
5429 static void
5430f_getreg(typval_T *argvars, typval_T *rettv)
5431{
5432 char_u *strregname;
5433 int regname;
5434 int arg2 = FALSE;
5435 int return_list = FALSE;
5436 int error = FALSE;
5437
5438 if (argvars[0].v_type != VAR_UNKNOWN)
5439 {
5440 strregname = get_tv_string_chk(&argvars[0]);
5441 error = strregname == NULL;
5442 if (argvars[1].v_type != VAR_UNKNOWN)
5443 {
5444 arg2 = (int)get_tv_number_chk(&argvars[1], &error);
5445 if (!error && argvars[2].v_type != VAR_UNKNOWN)
5446 return_list = (int)get_tv_number_chk(&argvars[2], &error);
5447 }
5448 }
5449 else
5450 strregname = get_vim_var_str(VV_REG);
5451
5452 if (error)
5453 return;
5454
5455 regname = (strregname == NULL ? '"' : *strregname);
5456 if (regname == 0)
5457 regname = '"';
5458
5459 if (return_list)
5460 {
5461 rettv->v_type = VAR_LIST;
5462 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5463 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5464 if (rettv->vval.v_list == NULL)
5465 (void)rettv_list_alloc(rettv);
5466 else
5467 ++rettv->vval.v_list->lv_refcount;
5468 }
5469 else
5470 {
5471 rettv->v_type = VAR_STRING;
5472 rettv->vval.v_string = get_reg_contents(regname,
5473 arg2 ? GREG_EXPR_SRC : 0);
5474 }
5475}
5476
5477/*
5478 * "getregtype()" function
5479 */
5480 static void
5481f_getregtype(typval_T *argvars, typval_T *rettv)
5482{
5483 char_u *strregname;
5484 int regname;
5485 char_u buf[NUMBUFLEN + 2];
5486 long reglen = 0;
5487
5488 if (argvars[0].v_type != VAR_UNKNOWN)
5489 {
5490 strregname = get_tv_string_chk(&argvars[0]);
5491 if (strregname == NULL) /* type error; errmsg already given */
5492 {
5493 rettv->v_type = VAR_STRING;
5494 rettv->vval.v_string = NULL;
5495 return;
5496 }
5497 }
5498 else
5499 /* Default to v:register */
5500 strregname = get_vim_var_str(VV_REG);
5501
5502 regname = (strregname == NULL ? '"' : *strregname);
5503 if (regname == 0)
5504 regname = '"';
5505
5506 buf[0] = NUL;
5507 buf[1] = NUL;
5508 switch (get_reg_type(regname, &reglen))
5509 {
5510 case MLINE: buf[0] = 'V'; break;
5511 case MCHAR: buf[0] = 'v'; break;
5512 case MBLOCK:
5513 buf[0] = Ctrl_V;
5514 sprintf((char *)buf + 1, "%ld", reglen + 1);
5515 break;
5516 }
5517 rettv->v_type = VAR_STRING;
5518 rettv->vval.v_string = vim_strsave(buf);
5519}
5520
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005521/*
5522 * Returns information (variables, options, etc.) about a tab page
5523 * as a dictionary.
5524 */
5525 static dict_T *
5526get_tabpage_info(tabpage_T *tp, int tp_idx)
5527{
5528 win_T *wp;
5529 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005530 list_T *l;
5531
5532 dict = dict_alloc();
5533 if (dict == NULL)
5534 return NULL;
5535
Bram Moolenaar33928832016-08-18 21:22:04 +02005536 dict_add_nr_str(dict, "tabnr", tp_idx, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005537
5538 l = list_alloc();
5539 if (l != NULL)
5540 {
5541 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5542 wp; wp = wp->w_next)
5543 list_append_number(l, (varnumber_T)wp->w_id);
5544 dict_add_list(dict, "windows", l);
5545 }
5546
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005547 /* Make a reference to tabpage variables */
5548 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005549
5550 return dict;
5551}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005552
5553/*
5554 * "gettabinfo()" function
5555 */
5556 static void
5557f_gettabinfo(typval_T *argvars, typval_T *rettv)
5558{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005559 tabpage_T *tp, *tparg = NULL;
5560 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005561 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005562
5563 if (rettv_list_alloc(rettv) != OK)
5564 return;
5565
5566 if (argvars[0].v_type != VAR_UNKNOWN)
5567 {
5568 /* Information about one tab page */
5569 tparg = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5570 if (tparg == NULL)
5571 return;
5572 }
5573
5574 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005575 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005576 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005577 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005578 if (tparg != NULL && tp != tparg)
5579 continue;
5580 d = get_tabpage_info(tp, tpnr);
5581 if (d != NULL)
5582 list_append_dict(rettv->vval.v_list, d);
5583 if (tparg != NULL)
5584 return;
5585 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005586}
5587
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005588/*
5589 * "gettabvar()" function
5590 */
5591 static void
5592f_gettabvar(typval_T *argvars, typval_T *rettv)
5593{
5594 win_T *oldcurwin;
5595 tabpage_T *tp, *oldtabpage;
5596 dictitem_T *v;
5597 char_u *varname;
5598 int done = FALSE;
5599
5600 rettv->v_type = VAR_STRING;
5601 rettv->vval.v_string = NULL;
5602
5603 varname = get_tv_string_chk(&argvars[1]);
5604 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5605 if (tp != NULL && varname != NULL)
5606 {
5607 /* Set tp to be our tabpage, temporarily. Also set the window to the
5608 * first window in the tabpage, otherwise the window is not valid. */
5609 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005610 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5611 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005612 {
5613 /* look up the variable */
5614 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5615 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5616 if (v != NULL)
5617 {
5618 copy_tv(&v->di_tv, rettv);
5619 done = TRUE;
5620 }
5621 }
5622
5623 /* restore previous notion of curwin */
5624 restore_win(oldcurwin, oldtabpage, TRUE);
5625 }
5626
5627 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5628 copy_tv(&argvars[2], rettv);
5629}
5630
5631/*
5632 * "gettabwinvar()" function
5633 */
5634 static void
5635f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5636{
5637 getwinvar(argvars, rettv, 1);
5638}
5639
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005640/*
5641 * Returns information about a window as a dictionary.
5642 */
5643 static dict_T *
5644get_win_info(win_T *wp, short tpnr, short winnr)
5645{
5646 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005647
5648 dict = dict_alloc();
5649 if (dict == NULL)
5650 return NULL;
5651
Bram Moolenaar33928832016-08-18 21:22:04 +02005652 dict_add_nr_str(dict, "tabnr", tpnr, NULL);
5653 dict_add_nr_str(dict, "winnr", winnr, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005654 dict_add_nr_str(dict, "winid", wp->w_id, NULL);
5655 dict_add_nr_str(dict, "height", wp->w_height, NULL);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005656#ifdef FEAT_MENU
5657 dict_add_nr_str(dict, "winbar", wp->w_winbar_height, NULL);
5658#endif
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005659 dict_add_nr_str(dict, "width", wp->w_width, NULL);
Bram Moolenaar33928832016-08-18 21:22:04 +02005660 dict_add_nr_str(dict, "bufnr", wp->w_buffer->b_fnum, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005661
Bram Moolenaar69905d12017-08-13 18:14:47 +02005662#ifdef FEAT_TERMINAL
5663 dict_add_nr_str(dict, "terminal", bt_terminal(wp->w_buffer), NULL);
5664#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005665#ifdef FEAT_QUICKFIX
5666 dict_add_nr_str(dict, "quickfix", bt_quickfix(wp->w_buffer), NULL);
5667 dict_add_nr_str(dict, "loclist",
5668 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL), NULL);
5669#endif
5670
Bram Moolenaar30567352016-08-27 21:25:44 +02005671 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005672 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005673
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005674 return dict;
5675}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005676
5677/*
5678 * "getwininfo()" function
5679 */
5680 static void
5681f_getwininfo(typval_T *argvars, typval_T *rettv)
5682{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005683 tabpage_T *tp;
5684 win_T *wp = NULL, *wparg = NULL;
5685 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005686 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005687
5688 if (rettv_list_alloc(rettv) != OK)
5689 return;
5690
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005691 if (argvars[0].v_type != VAR_UNKNOWN)
5692 {
5693 wparg = win_id2wp(argvars);
5694 if (wparg == NULL)
5695 return;
5696 }
5697
5698 /* Collect information about either all the windows across all the tab
5699 * pages or one particular window.
5700 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005701 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005702 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005703 tabnr++;
5704 winnr = 0;
5705 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005706 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005707 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005708 if (wparg != NULL && wp != wparg)
5709 continue;
5710 d = get_win_info(wp, tabnr, winnr);
5711 if (d != NULL)
5712 list_append_dict(rettv->vval.v_list, d);
5713 if (wparg != NULL)
5714 /* found information about a specific window */
5715 return;
5716 }
5717 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005718}
5719
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005720/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005721 * "win_findbuf()" function
5722 */
5723 static void
5724f_win_findbuf(typval_T *argvars, typval_T *rettv)
5725{
5726 if (rettv_list_alloc(rettv) != FAIL)
5727 win_findbuf(argvars, rettv->vval.v_list);
5728}
5729
5730/*
5731 * "win_getid()" function
5732 */
5733 static void
5734f_win_getid(typval_T *argvars, typval_T *rettv)
5735{
5736 rettv->vval.v_number = win_getid(argvars);
5737}
5738
5739/*
5740 * "win_gotoid()" function
5741 */
5742 static void
5743f_win_gotoid(typval_T *argvars, typval_T *rettv)
5744{
5745 rettv->vval.v_number = win_gotoid(argvars);
5746}
5747
5748/*
5749 * "win_id2tabwin()" function
5750 */
5751 static void
5752f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5753{
5754 if (rettv_list_alloc(rettv) != FAIL)
5755 win_id2tabwin(argvars, rettv->vval.v_list);
5756}
5757
5758/*
5759 * "win_id2win()" function
5760 */
5761 static void
5762f_win_id2win(typval_T *argvars, typval_T *rettv)
5763{
5764 rettv->vval.v_number = win_id2win(argvars);
5765}
5766
5767/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005768 * "win_screenpos()" function
5769 */
5770 static void
5771f_win_screenpos(typval_T *argvars, typval_T *rettv)
5772{
5773 win_T *wp;
5774
5775 if (rettv_list_alloc(rettv) == FAIL)
5776 return;
5777
5778 wp = find_win_by_nr(&argvars[0], NULL);
5779 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5780 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5781}
5782
5783/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005784 * "getwinpos({timeout})" function
5785 */
5786 static void
5787f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
5788{
5789 int x = -1;
5790 int y = -1;
5791
5792 if (rettv_list_alloc(rettv) == FAIL)
5793 return;
5794#ifdef FEAT_GUI
5795 if (gui.in_use)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01005796 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005797# if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5798 else
5799# endif
5800#endif
5801#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5802 {
5803 varnumber_T timeout = 100;
5804
5805 if (argvars[0].v_type != VAR_UNKNOWN)
5806 timeout = get_tv_number(&argvars[0]);
5807 term_get_winpos(&x, &y, timeout);
5808 }
5809#endif
5810 list_append_number(rettv->vval.v_list, (varnumber_T)x);
5811 list_append_number(rettv->vval.v_list, (varnumber_T)y);
5812}
5813
5814
5815/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005816 * "getwinposx()" function
5817 */
5818 static void
5819f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5820{
5821 rettv->vval.v_number = -1;
5822#ifdef FEAT_GUI
5823 if (gui.in_use)
5824 {
5825 int x, y;
5826
5827 if (gui_mch_get_winpos(&x, &y) == OK)
5828 rettv->vval.v_number = x;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005829 return;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005830 }
5831#endif
5832#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5833 {
5834 int x, y;
5835
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005836 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005837 rettv->vval.v_number = x;
5838 }
5839#endif
5840}
5841
5842/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005843 * "getwinposy()" function
5844 */
5845 static void
5846f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5847{
5848 rettv->vval.v_number = -1;
5849#ifdef FEAT_GUI
5850 if (gui.in_use)
5851 {
5852 int x, y;
5853
5854 if (gui_mch_get_winpos(&x, &y) == OK)
5855 rettv->vval.v_number = y;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005856 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005857 }
5858#endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005859#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5860 {
5861 int x, y;
5862
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005863 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005864 rettv->vval.v_number = y;
5865 }
5866#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005867}
5868
5869/*
5870 * "getwinvar()" function
5871 */
5872 static void
5873f_getwinvar(typval_T *argvars, typval_T *rettv)
5874{
5875 getwinvar(argvars, rettv, 0);
5876}
5877
5878/*
5879 * "glob()" function
5880 */
5881 static void
5882f_glob(typval_T *argvars, typval_T *rettv)
5883{
5884 int options = WILD_SILENT|WILD_USE_NL;
5885 expand_T xpc;
5886 int error = FALSE;
5887
5888 /* When the optional second argument is non-zero, don't remove matches
5889 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5890 rettv->v_type = VAR_STRING;
5891 if (argvars[1].v_type != VAR_UNKNOWN)
5892 {
5893 if (get_tv_number_chk(&argvars[1], &error))
5894 options |= WILD_KEEP_ALL;
5895 if (argvars[2].v_type != VAR_UNKNOWN)
5896 {
5897 if (get_tv_number_chk(&argvars[2], &error))
5898 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005899 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005900 }
5901 if (argvars[3].v_type != VAR_UNKNOWN
5902 && get_tv_number_chk(&argvars[3], &error))
5903 options |= WILD_ALLLINKS;
5904 }
5905 }
5906 if (!error)
5907 {
5908 ExpandInit(&xpc);
5909 xpc.xp_context = EXPAND_FILES;
5910 if (p_wic)
5911 options += WILD_ICASE;
5912 if (rettv->v_type == VAR_STRING)
5913 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
5914 NULL, options, WILD_ALL);
5915 else if (rettv_list_alloc(rettv) != FAIL)
5916 {
5917 int i;
5918
5919 ExpandOne(&xpc, get_tv_string(&argvars[0]),
5920 NULL, options, WILD_ALL_KEEP);
5921 for (i = 0; i < xpc.xp_numfiles; i++)
5922 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5923
5924 ExpandCleanup(&xpc);
5925 }
5926 }
5927 else
5928 rettv->vval.v_string = NULL;
5929}
5930
5931/*
5932 * "globpath()" function
5933 */
5934 static void
5935f_globpath(typval_T *argvars, typval_T *rettv)
5936{
5937 int flags = 0;
5938 char_u buf1[NUMBUFLEN];
5939 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
5940 int error = FALSE;
5941 garray_T ga;
5942 int i;
5943
5944 /* When the optional second argument is non-zero, don't remove matches
5945 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5946 rettv->v_type = VAR_STRING;
5947 if (argvars[2].v_type != VAR_UNKNOWN)
5948 {
5949 if (get_tv_number_chk(&argvars[2], &error))
5950 flags |= WILD_KEEP_ALL;
5951 if (argvars[3].v_type != VAR_UNKNOWN)
5952 {
5953 if (get_tv_number_chk(&argvars[3], &error))
5954 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005955 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005956 }
5957 if (argvars[4].v_type != VAR_UNKNOWN
5958 && get_tv_number_chk(&argvars[4], &error))
5959 flags |= WILD_ALLLINKS;
5960 }
5961 }
5962 if (file != NULL && !error)
5963 {
5964 ga_init2(&ga, (int)sizeof(char_u *), 10);
5965 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
5966 if (rettv->v_type == VAR_STRING)
5967 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
5968 else if (rettv_list_alloc(rettv) != FAIL)
5969 for (i = 0; i < ga.ga_len; ++i)
5970 list_append_string(rettv->vval.v_list,
5971 ((char_u **)(ga.ga_data))[i], -1);
5972 ga_clear_strings(&ga);
5973 }
5974 else
5975 rettv->vval.v_string = NULL;
5976}
5977
5978/*
5979 * "glob2regpat()" function
5980 */
5981 static void
5982f_glob2regpat(typval_T *argvars, typval_T *rettv)
5983{
5984 char_u *pat = get_tv_string_chk(&argvars[0]);
5985
5986 rettv->v_type = VAR_STRING;
5987 rettv->vval.v_string = (pat == NULL)
5988 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
5989}
5990
5991/* for VIM_VERSION_ defines */
5992#include "version.h"
5993
5994/*
5995 * "has()" function
5996 */
5997 static void
5998f_has(typval_T *argvars, typval_T *rettv)
5999{
6000 int i;
6001 char_u *name;
6002 int n = FALSE;
6003 static char *(has_list[]) =
6004 {
6005#ifdef AMIGA
6006 "amiga",
6007# ifdef FEAT_ARP
6008 "arp",
6009# endif
6010#endif
6011#ifdef __BEOS__
6012 "beos",
6013#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02006014#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01006015 "mac", /* Mac OS X (and, once, Mac OS Classic) */
6016 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02006017# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01006018 "macunix", /* Mac OS X, with the darwin feature */
6019 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02006020# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006021#endif
6022#ifdef __QNX__
6023 "qnx",
6024#endif
6025#ifdef UNIX
6026 "unix",
6027#endif
6028#ifdef VMS
6029 "vms",
6030#endif
6031#ifdef WIN32
6032 "win32",
6033#endif
6034#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
6035 "win32unix",
6036#endif
6037#if defined(WIN64) || defined(_WIN64)
6038 "win64",
6039#endif
6040#ifdef EBCDIC
6041 "ebcdic",
6042#endif
6043#ifndef CASE_INSENSITIVE_FILENAME
6044 "fname_case",
6045#endif
6046#ifdef HAVE_ACL
6047 "acl",
6048#endif
6049#ifdef FEAT_ARABIC
6050 "arabic",
6051#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006052 "autocmd",
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006053#ifdef FEAT_AUTOSERVERNAME
6054 "autoservername",
6055#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006056#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006057 "balloon_eval",
6058# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
6059 "balloon_multiline",
6060# endif
6061#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006062#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006063 "balloon_eval_term",
6064#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006065#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6066 "builtin_terms",
6067# ifdef ALL_BUILTIN_TCAPS
6068 "all_builtin_terms",
6069# endif
6070#endif
6071#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
6072 || defined(FEAT_GUI_W32) \
6073 || defined(FEAT_GUI_MOTIF))
6074 "browsefilter",
6075#endif
6076#ifdef FEAT_BYTEOFF
6077 "byte_offset",
6078#endif
6079#ifdef FEAT_JOB_CHANNEL
6080 "channel",
6081#endif
6082#ifdef FEAT_CINDENT
6083 "cindent",
6084#endif
6085#ifdef FEAT_CLIENTSERVER
6086 "clientserver",
6087#endif
6088#ifdef FEAT_CLIPBOARD
6089 "clipboard",
6090#endif
6091#ifdef FEAT_CMDL_COMPL
6092 "cmdline_compl",
6093#endif
6094#ifdef FEAT_CMDHIST
6095 "cmdline_hist",
6096#endif
6097#ifdef FEAT_COMMENTS
6098 "comments",
6099#endif
6100#ifdef FEAT_CONCEAL
6101 "conceal",
6102#endif
6103#ifdef FEAT_CRYPT
6104 "cryptv",
6105 "crypt-blowfish",
6106 "crypt-blowfish2",
6107#endif
6108#ifdef FEAT_CSCOPE
6109 "cscope",
6110#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006111 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006112#ifdef CURSOR_SHAPE
6113 "cursorshape",
6114#endif
6115#ifdef DEBUG
6116 "debug",
6117#endif
6118#ifdef FEAT_CON_DIALOG
6119 "dialog_con",
6120#endif
6121#ifdef FEAT_GUI_DIALOG
6122 "dialog_gui",
6123#endif
6124#ifdef FEAT_DIFF
6125 "diff",
6126#endif
6127#ifdef FEAT_DIGRAPHS
6128 "digraphs",
6129#endif
6130#ifdef FEAT_DIRECTX
6131 "directx",
6132#endif
6133#ifdef FEAT_DND
6134 "dnd",
6135#endif
6136#ifdef FEAT_EMACS_TAGS
6137 "emacs_tags",
6138#endif
6139 "eval", /* always present, of course! */
6140 "ex_extra", /* graduated feature */
6141#ifdef FEAT_SEARCH_EXTRA
6142 "extra_search",
6143#endif
6144#ifdef FEAT_FKMAP
6145 "farsi",
6146#endif
6147#ifdef FEAT_SEARCHPATH
6148 "file_in_path",
6149#endif
6150#ifdef FEAT_FILTERPIPE
6151 "filterpipe",
6152#endif
6153#ifdef FEAT_FIND_ID
6154 "find_in_path",
6155#endif
6156#ifdef FEAT_FLOAT
6157 "float",
6158#endif
6159#ifdef FEAT_FOLDING
6160 "folding",
6161#endif
6162#ifdef FEAT_FOOTER
6163 "footer",
6164#endif
6165#if !defined(USE_SYSTEM) && defined(UNIX)
6166 "fork",
6167#endif
6168#ifdef FEAT_GETTEXT
6169 "gettext",
6170#endif
6171#ifdef FEAT_GUI
6172 "gui",
6173#endif
6174#ifdef FEAT_GUI_ATHENA
6175# ifdef FEAT_GUI_NEXTAW
6176 "gui_neXtaw",
6177# else
6178 "gui_athena",
6179# endif
6180#endif
6181#ifdef FEAT_GUI_GTK
6182 "gui_gtk",
6183# ifdef USE_GTK3
6184 "gui_gtk3",
6185# else
6186 "gui_gtk2",
6187# endif
6188#endif
6189#ifdef FEAT_GUI_GNOME
6190 "gui_gnome",
6191#endif
6192#ifdef FEAT_GUI_MAC
6193 "gui_mac",
6194#endif
6195#ifdef FEAT_GUI_MOTIF
6196 "gui_motif",
6197#endif
6198#ifdef FEAT_GUI_PHOTON
6199 "gui_photon",
6200#endif
6201#ifdef FEAT_GUI_W32
6202 "gui_win32",
6203#endif
6204#ifdef FEAT_HANGULIN
6205 "hangul_input",
6206#endif
6207#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6208 "iconv",
6209#endif
6210#ifdef FEAT_INS_EXPAND
6211 "insert_expand",
6212#endif
6213#ifdef FEAT_JOB_CHANNEL
6214 "job",
6215#endif
6216#ifdef FEAT_JUMPLIST
6217 "jumplist",
6218#endif
6219#ifdef FEAT_KEYMAP
6220 "keymap",
6221#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006222 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006223#ifdef FEAT_LANGMAP
6224 "langmap",
6225#endif
6226#ifdef FEAT_LIBCALL
6227 "libcall",
6228#endif
6229#ifdef FEAT_LINEBREAK
6230 "linebreak",
6231#endif
6232#ifdef FEAT_LISP
6233 "lispindent",
6234#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006235 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006236#ifdef FEAT_LOCALMAP
6237 "localmap",
6238#endif
6239#ifdef FEAT_LUA
6240# ifndef DYNAMIC_LUA
6241 "lua",
6242# endif
6243#endif
6244#ifdef FEAT_MENU
6245 "menu",
6246#endif
6247#ifdef FEAT_SESSION
6248 "mksession",
6249#endif
6250#ifdef FEAT_MODIFY_FNAME
6251 "modify_fname",
6252#endif
6253#ifdef FEAT_MOUSE
6254 "mouse",
6255#endif
6256#ifdef FEAT_MOUSESHAPE
6257 "mouseshape",
6258#endif
6259#if defined(UNIX) || defined(VMS)
6260# ifdef FEAT_MOUSE_DEC
6261 "mouse_dec",
6262# endif
6263# ifdef FEAT_MOUSE_GPM
6264 "mouse_gpm",
6265# endif
6266# ifdef FEAT_MOUSE_JSB
6267 "mouse_jsbterm",
6268# endif
6269# ifdef FEAT_MOUSE_NET
6270 "mouse_netterm",
6271# endif
6272# ifdef FEAT_MOUSE_PTERM
6273 "mouse_pterm",
6274# endif
6275# ifdef FEAT_MOUSE_SGR
6276 "mouse_sgr",
6277# endif
6278# ifdef FEAT_SYSMOUSE
6279 "mouse_sysmouse",
6280# endif
6281# ifdef FEAT_MOUSE_URXVT
6282 "mouse_urxvt",
6283# endif
6284# ifdef FEAT_MOUSE_XTERM
6285 "mouse_xterm",
6286# endif
6287#endif
6288#ifdef FEAT_MBYTE
6289 "multi_byte",
6290#endif
6291#ifdef FEAT_MBYTE_IME
6292 "multi_byte_ime",
6293#endif
6294#ifdef FEAT_MULTI_LANG
6295 "multi_lang",
6296#endif
6297#ifdef FEAT_MZSCHEME
6298#ifndef DYNAMIC_MZSCHEME
6299 "mzscheme",
6300#endif
6301#endif
6302#ifdef FEAT_NUM64
6303 "num64",
6304#endif
6305#ifdef FEAT_OLE
6306 "ole",
6307#endif
6308 "packages",
6309#ifdef FEAT_PATH_EXTRA
6310 "path_extra",
6311#endif
6312#ifdef FEAT_PERL
6313#ifndef DYNAMIC_PERL
6314 "perl",
6315#endif
6316#endif
6317#ifdef FEAT_PERSISTENT_UNDO
6318 "persistent_undo",
6319#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006320#if defined(FEAT_PYTHON)
6321 "python_compiled",
6322# if defined(DYNAMIC_PYTHON)
6323 "python_dynamic",
6324# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006325 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006326 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006327# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006328#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006329#if defined(FEAT_PYTHON3)
6330 "python3_compiled",
6331# if defined(DYNAMIC_PYTHON3)
6332 "python3_dynamic",
6333# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006334 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006335 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006336# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006337#endif
6338#ifdef FEAT_POSTSCRIPT
6339 "postscript",
6340#endif
6341#ifdef FEAT_PRINTER
6342 "printer",
6343#endif
6344#ifdef FEAT_PROFILE
6345 "profile",
6346#endif
6347#ifdef FEAT_RELTIME
6348 "reltime",
6349#endif
6350#ifdef FEAT_QUICKFIX
6351 "quickfix",
6352#endif
6353#ifdef FEAT_RIGHTLEFT
6354 "rightleft",
6355#endif
6356#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6357 "ruby",
6358#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006359 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006360#ifdef FEAT_CMDL_INFO
6361 "showcmd",
6362 "cmdline_info",
6363#endif
6364#ifdef FEAT_SIGNS
6365 "signs",
6366#endif
6367#ifdef FEAT_SMARTINDENT
6368 "smartindent",
6369#endif
6370#ifdef STARTUPTIME
6371 "startuptime",
6372#endif
6373#ifdef FEAT_STL_OPT
6374 "statusline",
6375#endif
6376#ifdef FEAT_SUN_WORKSHOP
6377 "sun_workshop",
6378#endif
6379#ifdef FEAT_NETBEANS_INTG
6380 "netbeans_intg",
6381#endif
6382#ifdef FEAT_SPELL
6383 "spell",
6384#endif
6385#ifdef FEAT_SYN_HL
6386 "syntax",
6387#endif
6388#if defined(USE_SYSTEM) || !defined(UNIX)
6389 "system",
6390#endif
6391#ifdef FEAT_TAG_BINS
6392 "tag_binary",
6393#endif
6394#ifdef FEAT_TAG_OLDSTATIC
6395 "tag_old_static",
6396#endif
6397#ifdef FEAT_TAG_ANYWHITE
6398 "tag_any_white",
6399#endif
6400#ifdef FEAT_TCL
6401# ifndef DYNAMIC_TCL
6402 "tcl",
6403# endif
6404#endif
6405#ifdef FEAT_TERMGUICOLORS
6406 "termguicolors",
6407#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006408#if defined(FEAT_TERMINAL) && !defined(WIN3264)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006409 "terminal",
6410#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006411#ifdef TERMINFO
6412 "terminfo",
6413#endif
6414#ifdef FEAT_TERMRESPONSE
6415 "termresponse",
6416#endif
6417#ifdef FEAT_TEXTOBJ
6418 "textobjects",
6419#endif
6420#ifdef HAVE_TGETENT
6421 "tgetent",
6422#endif
6423#ifdef FEAT_TIMERS
6424 "timers",
6425#endif
6426#ifdef FEAT_TITLE
6427 "title",
6428#endif
6429#ifdef FEAT_TOOLBAR
6430 "toolbar",
6431#endif
6432#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6433 "unnamedplus",
6434#endif
6435#ifdef FEAT_USR_CMDS
6436 "user-commands", /* was accidentally included in 5.4 */
6437 "user_commands",
6438#endif
6439#ifdef FEAT_VIMINFO
6440 "viminfo",
6441#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006442 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006443#ifdef FEAT_VIRTUALEDIT
6444 "virtualedit",
6445#endif
6446 "visual",
6447#ifdef FEAT_VISUALEXTRA
6448 "visualextra",
6449#endif
6450#ifdef FEAT_VREPLACE
6451 "vreplace",
6452#endif
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006453#ifdef FEAT_VTP
6454 "vtp",
6455#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006456#ifdef FEAT_WILDIGN
6457 "wildignore",
6458#endif
6459#ifdef FEAT_WILDMENU
6460 "wildmenu",
6461#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006462 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006463#ifdef FEAT_WAK
6464 "winaltkeys",
6465#endif
6466#ifdef FEAT_WRITEBACKUP
6467 "writebackup",
6468#endif
6469#ifdef FEAT_XIM
6470 "xim",
6471#endif
6472#ifdef FEAT_XFONTSET
6473 "xfontset",
6474#endif
6475#ifdef FEAT_XPM_W32
6476 "xpm",
6477 "xpm_w32", /* for backward compatibility */
6478#else
6479# if defined(HAVE_XPM)
6480 "xpm",
6481# endif
6482#endif
6483#ifdef USE_XSMP
6484 "xsmp",
6485#endif
6486#ifdef USE_XSMP_INTERACT
6487 "xsmp_interact",
6488#endif
6489#ifdef FEAT_XCLIPBOARD
6490 "xterm_clipboard",
6491#endif
6492#ifdef FEAT_XTERM_SAVE
6493 "xterm_save",
6494#endif
6495#if defined(UNIX) && defined(FEAT_X11)
6496 "X11",
6497#endif
6498 NULL
6499 };
6500
6501 name = get_tv_string(&argvars[0]);
6502 for (i = 0; has_list[i] != NULL; ++i)
6503 if (STRICMP(name, has_list[i]) == 0)
6504 {
6505 n = TRUE;
6506 break;
6507 }
6508
6509 if (n == FALSE)
6510 {
6511 if (STRNICMP(name, "patch", 5) == 0)
6512 {
6513 if (name[5] == '-'
6514 && STRLEN(name) >= 11
6515 && vim_isdigit(name[6])
6516 && vim_isdigit(name[8])
6517 && vim_isdigit(name[10]))
6518 {
6519 int major = atoi((char *)name + 6);
6520 int minor = atoi((char *)name + 8);
6521
6522 /* Expect "patch-9.9.01234". */
6523 n = (major < VIM_VERSION_MAJOR
6524 || (major == VIM_VERSION_MAJOR
6525 && (minor < VIM_VERSION_MINOR
6526 || (minor == VIM_VERSION_MINOR
6527 && has_patch(atoi((char *)name + 10))))));
6528 }
6529 else
6530 n = has_patch(atoi((char *)name + 5));
6531 }
6532 else if (STRICMP(name, "vim_starting") == 0)
6533 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006534 else if (STRICMP(name, "ttyin") == 0)
6535 n = mch_input_isatty();
6536 else if (STRICMP(name, "ttyout") == 0)
6537 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006538#ifdef FEAT_MBYTE
6539 else if (STRICMP(name, "multi_byte_encoding") == 0)
6540 n = has_mbyte;
6541#endif
6542#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
6543 else if (STRICMP(name, "balloon_multiline") == 0)
6544 n = multiline_balloon_available();
6545#endif
6546#ifdef DYNAMIC_TCL
6547 else if (STRICMP(name, "tcl") == 0)
6548 n = tcl_enabled(FALSE);
6549#endif
6550#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6551 else if (STRICMP(name, "iconv") == 0)
6552 n = iconv_enabled(FALSE);
6553#endif
6554#ifdef DYNAMIC_LUA
6555 else if (STRICMP(name, "lua") == 0)
6556 n = lua_enabled(FALSE);
6557#endif
6558#ifdef DYNAMIC_MZSCHEME
6559 else if (STRICMP(name, "mzscheme") == 0)
6560 n = mzscheme_enabled(FALSE);
6561#endif
6562#ifdef DYNAMIC_RUBY
6563 else if (STRICMP(name, "ruby") == 0)
6564 n = ruby_enabled(FALSE);
6565#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006566#ifdef DYNAMIC_PYTHON
6567 else if (STRICMP(name, "python") == 0)
6568 n = python_enabled(FALSE);
6569#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006570#ifdef DYNAMIC_PYTHON3
6571 else if (STRICMP(name, "python3") == 0)
6572 n = python3_enabled(FALSE);
6573#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006574#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6575 else if (STRICMP(name, "pythonx") == 0)
6576 {
6577# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6578 if (p_pyx == 0)
6579 n = python3_enabled(FALSE) || python_enabled(FALSE);
6580 else if (p_pyx == 3)
6581 n = python3_enabled(FALSE);
6582 else if (p_pyx == 2)
6583 n = python_enabled(FALSE);
6584# elif defined(DYNAMIC_PYTHON)
6585 n = python_enabled(FALSE);
6586# elif defined(DYNAMIC_PYTHON3)
6587 n = python3_enabled(FALSE);
6588# endif
6589 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006590#endif
6591#ifdef DYNAMIC_PERL
6592 else if (STRICMP(name, "perl") == 0)
6593 n = perl_enabled(FALSE);
6594#endif
6595#ifdef FEAT_GUI
6596 else if (STRICMP(name, "gui_running") == 0)
6597 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006598# ifdef FEAT_BROWSE
6599 else if (STRICMP(name, "browse") == 0)
6600 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6601# endif
6602#endif
6603#ifdef FEAT_SYN_HL
6604 else if (STRICMP(name, "syntax_items") == 0)
6605 n = syntax_present(curwin);
6606#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006607#ifdef FEAT_VTP
6608 else if (STRICMP(name, "vcon") == 0)
6609 n = has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006610#endif
6611#ifdef FEAT_NETBEANS_INTG
6612 else if (STRICMP(name, "netbeans_enabled") == 0)
6613 n = netbeans_active();
6614#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006615#if defined(FEAT_TERMINAL) && defined(WIN3264)
6616 else if (STRICMP(name, "terminal") == 0)
6617 n = terminal_enabled();
6618#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006619 }
6620
6621 rettv->vval.v_number = n;
6622}
6623
6624/*
6625 * "has_key()" function
6626 */
6627 static void
6628f_has_key(typval_T *argvars, typval_T *rettv)
6629{
6630 if (argvars[0].v_type != VAR_DICT)
6631 {
6632 EMSG(_(e_dictreq));
6633 return;
6634 }
6635 if (argvars[0].vval.v_dict == NULL)
6636 return;
6637
6638 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
6639 get_tv_string(&argvars[1]), -1) != NULL;
6640}
6641
6642/*
6643 * "haslocaldir()" function
6644 */
6645 static void
6646f_haslocaldir(typval_T *argvars, typval_T *rettv)
6647{
6648 win_T *wp = NULL;
6649
6650 wp = find_tabwin(&argvars[0], &argvars[1]);
6651 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6652}
6653
6654/*
6655 * "hasmapto()" function
6656 */
6657 static void
6658f_hasmapto(typval_T *argvars, typval_T *rettv)
6659{
6660 char_u *name;
6661 char_u *mode;
6662 char_u buf[NUMBUFLEN];
6663 int abbr = FALSE;
6664
6665 name = get_tv_string(&argvars[0]);
6666 if (argvars[1].v_type == VAR_UNKNOWN)
6667 mode = (char_u *)"nvo";
6668 else
6669 {
6670 mode = get_tv_string_buf(&argvars[1], buf);
6671 if (argvars[2].v_type != VAR_UNKNOWN)
6672 abbr = (int)get_tv_number(&argvars[2]);
6673 }
6674
6675 if (map_to_exists(name, mode, abbr))
6676 rettv->vval.v_number = TRUE;
6677 else
6678 rettv->vval.v_number = FALSE;
6679}
6680
6681/*
6682 * "histadd()" function
6683 */
6684 static void
6685f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6686{
6687#ifdef FEAT_CMDHIST
6688 int histype;
6689 char_u *str;
6690 char_u buf[NUMBUFLEN];
6691#endif
6692
6693 rettv->vval.v_number = FALSE;
6694 if (check_restricted() || check_secure())
6695 return;
6696#ifdef FEAT_CMDHIST
6697 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6698 histype = str != NULL ? get_histtype(str) : -1;
6699 if (histype >= 0)
6700 {
6701 str = get_tv_string_buf(&argvars[1], buf);
6702 if (*str != NUL)
6703 {
6704 init_history();
6705 add_to_history(histype, str, FALSE, NUL);
6706 rettv->vval.v_number = TRUE;
6707 return;
6708 }
6709 }
6710#endif
6711}
6712
6713/*
6714 * "histdel()" function
6715 */
6716 static void
6717f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6718{
6719#ifdef FEAT_CMDHIST
6720 int n;
6721 char_u buf[NUMBUFLEN];
6722 char_u *str;
6723
6724 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6725 if (str == NULL)
6726 n = 0;
6727 else if (argvars[1].v_type == VAR_UNKNOWN)
6728 /* only one argument: clear entire history */
6729 n = clr_history(get_histtype(str));
6730 else if (argvars[1].v_type == VAR_NUMBER)
6731 /* index given: remove that entry */
6732 n = del_history_idx(get_histtype(str),
6733 (int)get_tv_number(&argvars[1]));
6734 else
6735 /* string given: remove all matching entries */
6736 n = del_history_entry(get_histtype(str),
6737 get_tv_string_buf(&argvars[1], buf));
6738 rettv->vval.v_number = n;
6739#endif
6740}
6741
6742/*
6743 * "histget()" function
6744 */
6745 static void
6746f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6747{
6748#ifdef FEAT_CMDHIST
6749 int type;
6750 int idx;
6751 char_u *str;
6752
6753 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6754 if (str == NULL)
6755 rettv->vval.v_string = NULL;
6756 else
6757 {
6758 type = get_histtype(str);
6759 if (argvars[1].v_type == VAR_UNKNOWN)
6760 idx = get_history_idx(type);
6761 else
6762 idx = (int)get_tv_number_chk(&argvars[1], NULL);
6763 /* -1 on type error */
6764 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6765 }
6766#else
6767 rettv->vval.v_string = NULL;
6768#endif
6769 rettv->v_type = VAR_STRING;
6770}
6771
6772/*
6773 * "histnr()" function
6774 */
6775 static void
6776f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6777{
6778 int i;
6779
6780#ifdef FEAT_CMDHIST
6781 char_u *history = get_tv_string_chk(&argvars[0]);
6782
6783 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6784 if (i >= HIST_CMD && i < HIST_COUNT)
6785 i = get_history_idx(i);
6786 else
6787#endif
6788 i = -1;
6789 rettv->vval.v_number = i;
6790}
6791
6792/*
6793 * "highlightID(name)" function
6794 */
6795 static void
6796f_hlID(typval_T *argvars, typval_T *rettv)
6797{
6798 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
6799}
6800
6801/*
6802 * "highlight_exists()" function
6803 */
6804 static void
6805f_hlexists(typval_T *argvars, typval_T *rettv)
6806{
6807 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
6808}
6809
6810/*
6811 * "hostname()" function
6812 */
6813 static void
6814f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6815{
6816 char_u hostname[256];
6817
6818 mch_get_host_name(hostname, 256);
6819 rettv->v_type = VAR_STRING;
6820 rettv->vval.v_string = vim_strsave(hostname);
6821}
6822
6823/*
6824 * iconv() function
6825 */
6826 static void
6827f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6828{
6829#ifdef FEAT_MBYTE
6830 char_u buf1[NUMBUFLEN];
6831 char_u buf2[NUMBUFLEN];
6832 char_u *from, *to, *str;
6833 vimconv_T vimconv;
6834#endif
6835
6836 rettv->v_type = VAR_STRING;
6837 rettv->vval.v_string = NULL;
6838
6839#ifdef FEAT_MBYTE
6840 str = get_tv_string(&argvars[0]);
6841 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
6842 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
6843 vimconv.vc_type = CONV_NONE;
6844 convert_setup(&vimconv, from, to);
6845
6846 /* If the encodings are equal, no conversion needed. */
6847 if (vimconv.vc_type == CONV_NONE)
6848 rettv->vval.v_string = vim_strsave(str);
6849 else
6850 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6851
6852 convert_setup(&vimconv, NULL, NULL);
6853 vim_free(from);
6854 vim_free(to);
6855#endif
6856}
6857
6858/*
6859 * "indent()" function
6860 */
6861 static void
6862f_indent(typval_T *argvars, typval_T *rettv)
6863{
6864 linenr_T lnum;
6865
6866 lnum = get_tv_lnum(argvars);
6867 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6868 rettv->vval.v_number = get_indent_lnum(lnum);
6869 else
6870 rettv->vval.v_number = -1;
6871}
6872
6873/*
6874 * "index()" function
6875 */
6876 static void
6877f_index(typval_T *argvars, typval_T *rettv)
6878{
6879 list_T *l;
6880 listitem_T *item;
6881 long idx = 0;
6882 int ic = FALSE;
6883
6884 rettv->vval.v_number = -1;
6885 if (argvars[0].v_type != VAR_LIST)
6886 {
6887 EMSG(_(e_listreq));
6888 return;
6889 }
6890 l = argvars[0].vval.v_list;
6891 if (l != NULL)
6892 {
6893 item = l->lv_first;
6894 if (argvars[2].v_type != VAR_UNKNOWN)
6895 {
6896 int error = FALSE;
6897
6898 /* Start at specified item. Use the cached index that list_find()
6899 * sets, so that a negative number also works. */
6900 item = list_find(l, (long)get_tv_number_chk(&argvars[2], &error));
6901 idx = l->lv_idx;
6902 if (argvars[3].v_type != VAR_UNKNOWN)
6903 ic = (int)get_tv_number_chk(&argvars[3], &error);
6904 if (error)
6905 item = NULL;
6906 }
6907
6908 for ( ; item != NULL; item = item->li_next, ++idx)
6909 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6910 {
6911 rettv->vval.v_number = idx;
6912 break;
6913 }
6914 }
6915}
6916
6917static int inputsecret_flag = 0;
6918
6919/*
6920 * "input()" function
6921 * Also handles inputsecret() when inputsecret is set.
6922 */
6923 static void
6924f_input(typval_T *argvars, typval_T *rettv)
6925{
6926 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6927}
6928
6929/*
6930 * "inputdialog()" function
6931 */
6932 static void
6933f_inputdialog(typval_T *argvars, typval_T *rettv)
6934{
6935#if defined(FEAT_GUI_TEXTDIALOG)
6936 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
6937 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6938 {
6939 char_u *message;
6940 char_u buf[NUMBUFLEN];
6941 char_u *defstr = (char_u *)"";
6942
6943 message = get_tv_string_chk(&argvars[0]);
6944 if (argvars[1].v_type != VAR_UNKNOWN
6945 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
6946 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6947 else
6948 IObuff[0] = NUL;
6949 if (message != NULL && defstr != NULL
6950 && do_dialog(VIM_QUESTION, NULL, message,
6951 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6952 rettv->vval.v_string = vim_strsave(IObuff);
6953 else
6954 {
6955 if (message != NULL && defstr != NULL
6956 && argvars[1].v_type != VAR_UNKNOWN
6957 && argvars[2].v_type != VAR_UNKNOWN)
6958 rettv->vval.v_string = vim_strsave(
6959 get_tv_string_buf(&argvars[2], buf));
6960 else
6961 rettv->vval.v_string = NULL;
6962 }
6963 rettv->v_type = VAR_STRING;
6964 }
6965 else
6966#endif
6967 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6968}
6969
6970/*
6971 * "inputlist()" function
6972 */
6973 static void
6974f_inputlist(typval_T *argvars, typval_T *rettv)
6975{
6976 listitem_T *li;
6977 int selected;
6978 int mouse_used;
6979
6980#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02006981 /* While starting up, there is no place to enter text. When running tests
6982 * with --not-a-term we assume feedkeys() will be used. */
6983 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006984 return;
6985#endif
6986 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6987 {
6988 EMSG2(_(e_listarg), "inputlist()");
6989 return;
6990 }
6991
6992 msg_start();
6993 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
6994 lines_left = Rows; /* avoid more prompt */
6995 msg_scroll = TRUE;
6996 msg_clr_eos();
6997
6998 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
6999 {
7000 msg_puts(get_tv_string(&li->li_tv));
7001 msg_putchar('\n');
7002 }
7003
7004 /* Ask for choice. */
7005 selected = prompt_for_number(&mouse_used);
7006 if (mouse_used)
7007 selected -= lines_left;
7008
7009 rettv->vval.v_number = selected;
7010}
7011
7012
7013static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7014
7015/*
7016 * "inputrestore()" function
7017 */
7018 static void
7019f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7020{
7021 if (ga_userinput.ga_len > 0)
7022 {
7023 --ga_userinput.ga_len;
7024 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7025 + ga_userinput.ga_len);
7026 /* default return is zero == OK */
7027 }
7028 else if (p_verbose > 1)
7029 {
7030 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
7031 rettv->vval.v_number = 1; /* Failed */
7032 }
7033}
7034
7035/*
7036 * "inputsave()" function
7037 */
7038 static void
7039f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7040{
7041 /* Add an entry to the stack of typeahead storage. */
7042 if (ga_grow(&ga_userinput, 1) == OK)
7043 {
7044 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7045 + ga_userinput.ga_len);
7046 ++ga_userinput.ga_len;
7047 /* default return is zero == OK */
7048 }
7049 else
7050 rettv->vval.v_number = 1; /* Failed */
7051}
7052
7053/*
7054 * "inputsecret()" function
7055 */
7056 static void
7057f_inputsecret(typval_T *argvars, typval_T *rettv)
7058{
7059 ++cmdline_star;
7060 ++inputsecret_flag;
7061 f_input(argvars, rettv);
7062 --cmdline_star;
7063 --inputsecret_flag;
7064}
7065
7066/*
7067 * "insert()" function
7068 */
7069 static void
7070f_insert(typval_T *argvars, typval_T *rettv)
7071{
7072 long before = 0;
7073 listitem_T *item;
7074 list_T *l;
7075 int error = FALSE;
7076
7077 if (argvars[0].v_type != VAR_LIST)
7078 EMSG2(_(e_listarg), "insert()");
7079 else if ((l = argvars[0].vval.v_list) != NULL
7080 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
7081 {
7082 if (argvars[2].v_type != VAR_UNKNOWN)
7083 before = (long)get_tv_number_chk(&argvars[2], &error);
7084 if (error)
7085 return; /* type error; errmsg already given */
7086
7087 if (before == l->lv_len)
7088 item = NULL;
7089 else
7090 {
7091 item = list_find(l, before);
7092 if (item == NULL)
7093 {
7094 EMSGN(_(e_listidx), before);
7095 l = NULL;
7096 }
7097 }
7098 if (l != NULL)
7099 {
7100 list_insert_tv(l, &argvars[1], item);
7101 copy_tv(&argvars[0], rettv);
7102 }
7103 }
7104}
7105
7106/*
7107 * "invert(expr)" function
7108 */
7109 static void
7110f_invert(typval_T *argvars, typval_T *rettv)
7111{
7112 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
7113}
7114
7115/*
7116 * "isdirectory()" function
7117 */
7118 static void
7119f_isdirectory(typval_T *argvars, typval_T *rettv)
7120{
7121 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
7122}
7123
7124/*
7125 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7126 * or it refers to a List or Dictionary that is locked.
7127 */
7128 static int
7129tv_islocked(typval_T *tv)
7130{
7131 return (tv->v_lock & VAR_LOCKED)
7132 || (tv->v_type == VAR_LIST
7133 && tv->vval.v_list != NULL
7134 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7135 || (tv->v_type == VAR_DICT
7136 && tv->vval.v_dict != NULL
7137 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7138}
7139
7140/*
7141 * "islocked()" function
7142 */
7143 static void
7144f_islocked(typval_T *argvars, typval_T *rettv)
7145{
7146 lval_T lv;
7147 char_u *end;
7148 dictitem_T *di;
7149
7150 rettv->vval.v_number = -1;
7151 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007152 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007153 if (end != NULL && lv.ll_name != NULL)
7154 {
7155 if (*end != NUL)
7156 EMSG(_(e_trailing));
7157 else
7158 {
7159 if (lv.ll_tv == NULL)
7160 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007161 di = find_var(lv.ll_name, NULL, TRUE);
7162 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007163 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007164 /* Consider a variable locked when:
7165 * 1. the variable itself is locked
7166 * 2. the value of the variable is locked.
7167 * 3. the List or Dict value is locked.
7168 */
7169 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7170 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007171 }
7172 }
7173 else if (lv.ll_range)
7174 EMSG(_("E786: Range not allowed"));
7175 else if (lv.ll_newkey != NULL)
7176 EMSG2(_(e_dictkey), lv.ll_newkey);
7177 else if (lv.ll_list != NULL)
7178 /* List item. */
7179 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7180 else
7181 /* Dictionary item. */
7182 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7183 }
7184 }
7185
7186 clear_lval(&lv);
7187}
7188
7189#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7190/*
7191 * "isnan()" function
7192 */
7193 static void
7194f_isnan(typval_T *argvars, typval_T *rettv)
7195{
7196 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7197 && isnan(argvars[0].vval.v_float);
7198}
7199#endif
7200
7201/*
7202 * "items(dict)" function
7203 */
7204 static void
7205f_items(typval_T *argvars, typval_T *rettv)
7206{
7207 dict_list(argvars, rettv, 2);
7208}
7209
7210#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
7211/*
7212 * Get the job from the argument.
7213 * Returns NULL if the job is invalid.
7214 */
7215 static job_T *
7216get_job_arg(typval_T *tv)
7217{
7218 job_T *job;
7219
7220 if (tv->v_type != VAR_JOB)
7221 {
7222 EMSG2(_(e_invarg2), get_tv_string(tv));
7223 return NULL;
7224 }
7225 job = tv->vval.v_job;
7226
7227 if (job == NULL)
7228 EMSG(_("E916: not a valid job"));
7229 return job;
7230}
7231
7232/*
7233 * "job_getchannel()" function
7234 */
7235 static void
7236f_job_getchannel(typval_T *argvars, typval_T *rettv)
7237{
7238 job_T *job = get_job_arg(&argvars[0]);
7239
7240 if (job != NULL)
7241 {
7242 rettv->v_type = VAR_CHANNEL;
7243 rettv->vval.v_channel = job->jv_channel;
7244 if (job->jv_channel != NULL)
7245 ++job->jv_channel->ch_refcount;
7246 }
7247}
7248
7249/*
7250 * "job_info()" function
7251 */
7252 static void
7253f_job_info(typval_T *argvars, typval_T *rettv)
7254{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007255 if (argvars[0].v_type != VAR_UNKNOWN)
7256 {
7257 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007258
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007259 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7260 job_info(job, rettv->vval.v_dict);
7261 }
7262 else if (rettv_list_alloc(rettv) == OK)
7263 job_info_all(rettv->vval.v_list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007264}
7265
7266/*
7267 * "job_setoptions()" function
7268 */
7269 static void
7270f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7271{
7272 job_T *job = get_job_arg(&argvars[0]);
7273 jobopt_T opt;
7274
7275 if (job == NULL)
7276 return;
7277 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007278 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007279 job_set_options(job, &opt);
7280 free_job_options(&opt);
7281}
7282
7283/*
7284 * "job_start()" function
7285 */
7286 static void
7287f_job_start(typval_T *argvars, typval_T *rettv)
7288{
7289 rettv->v_type = VAR_JOB;
7290 if (check_restricted() || check_secure())
7291 return;
Bram Moolenaar493359e2018-06-12 20:25:52 +02007292 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007293}
7294
7295/*
7296 * "job_status()" function
7297 */
7298 static void
7299f_job_status(typval_T *argvars, typval_T *rettv)
7300{
7301 job_T *job = get_job_arg(&argvars[0]);
7302
7303 if (job != NULL)
7304 {
7305 rettv->v_type = VAR_STRING;
7306 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7307 }
7308}
7309
7310/*
7311 * "job_stop()" function
7312 */
7313 static void
7314f_job_stop(typval_T *argvars, typval_T *rettv)
7315{
7316 job_T *job = get_job_arg(&argvars[0]);
7317
7318 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007319 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007320}
7321#endif
7322
7323/*
7324 * "join()" function
7325 */
7326 static void
7327f_join(typval_T *argvars, typval_T *rettv)
7328{
7329 garray_T ga;
7330 char_u *sep;
7331
7332 if (argvars[0].v_type != VAR_LIST)
7333 {
7334 EMSG(_(e_listreq));
7335 return;
7336 }
7337 if (argvars[0].vval.v_list == NULL)
7338 return;
7339 if (argvars[1].v_type == VAR_UNKNOWN)
7340 sep = (char_u *)" ";
7341 else
7342 sep = get_tv_string_chk(&argvars[1]);
7343
7344 rettv->v_type = VAR_STRING;
7345
7346 if (sep != NULL)
7347 {
7348 ga_init2(&ga, (int)sizeof(char), 80);
7349 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7350 ga_append(&ga, NUL);
7351 rettv->vval.v_string = (char_u *)ga.ga_data;
7352 }
7353 else
7354 rettv->vval.v_string = NULL;
7355}
7356
7357/*
7358 * "js_decode()" function
7359 */
7360 static void
7361f_js_decode(typval_T *argvars, typval_T *rettv)
7362{
7363 js_read_T reader;
7364
7365 reader.js_buf = get_tv_string(&argvars[0]);
7366 reader.js_fill = NULL;
7367 reader.js_used = 0;
7368 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
7369 EMSG(_(e_invarg));
7370}
7371
7372/*
7373 * "js_encode()" function
7374 */
7375 static void
7376f_js_encode(typval_T *argvars, typval_T *rettv)
7377{
7378 rettv->v_type = VAR_STRING;
7379 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7380}
7381
7382/*
7383 * "json_decode()" function
7384 */
7385 static void
7386f_json_decode(typval_T *argvars, typval_T *rettv)
7387{
7388 js_read_T reader;
7389
7390 reader.js_buf = get_tv_string(&argvars[0]);
7391 reader.js_fill = NULL;
7392 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007393 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007394}
7395
7396/*
7397 * "json_encode()" function
7398 */
7399 static void
7400f_json_encode(typval_T *argvars, typval_T *rettv)
7401{
7402 rettv->v_type = VAR_STRING;
7403 rettv->vval.v_string = json_encode(&argvars[0], 0);
7404}
7405
7406/*
7407 * "keys()" function
7408 */
7409 static void
7410f_keys(typval_T *argvars, typval_T *rettv)
7411{
7412 dict_list(argvars, rettv, 0);
7413}
7414
7415/*
7416 * "last_buffer_nr()" function.
7417 */
7418 static void
7419f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7420{
7421 int n = 0;
7422 buf_T *buf;
7423
Bram Moolenaar29323592016-07-24 22:04:11 +02007424 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007425 if (n < buf->b_fnum)
7426 n = buf->b_fnum;
7427
7428 rettv->vval.v_number = n;
7429}
7430
7431/*
7432 * "len()" function
7433 */
7434 static void
7435f_len(typval_T *argvars, typval_T *rettv)
7436{
7437 switch (argvars[0].v_type)
7438 {
7439 case VAR_STRING:
7440 case VAR_NUMBER:
7441 rettv->vval.v_number = (varnumber_T)STRLEN(
7442 get_tv_string(&argvars[0]));
7443 break;
7444 case VAR_LIST:
7445 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7446 break;
7447 case VAR_DICT:
7448 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7449 break;
7450 case VAR_UNKNOWN:
7451 case VAR_SPECIAL:
7452 case VAR_FLOAT:
7453 case VAR_FUNC:
7454 case VAR_PARTIAL:
7455 case VAR_JOB:
7456 case VAR_CHANNEL:
7457 EMSG(_("E701: Invalid type for len()"));
7458 break;
7459 }
7460}
7461
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007462 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007463libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007464{
7465#ifdef FEAT_LIBCALL
7466 char_u *string_in;
7467 char_u **string_result;
7468 int nr_result;
7469#endif
7470
7471 rettv->v_type = type;
7472 if (type != VAR_NUMBER)
7473 rettv->vval.v_string = NULL;
7474
7475 if (check_restricted() || check_secure())
7476 return;
7477
7478#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007479 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007480 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7481 {
7482 string_in = NULL;
7483 if (argvars[2].v_type == VAR_STRING)
7484 string_in = argvars[2].vval.v_string;
7485 if (type == VAR_NUMBER)
7486 string_result = NULL;
7487 else
7488 string_result = &rettv->vval.v_string;
7489 if (mch_libcall(argvars[0].vval.v_string,
7490 argvars[1].vval.v_string,
7491 string_in,
7492 argvars[2].vval.v_number,
7493 string_result,
7494 &nr_result) == OK
7495 && type == VAR_NUMBER)
7496 rettv->vval.v_number = nr_result;
7497 }
7498#endif
7499}
7500
7501/*
7502 * "libcall()" function
7503 */
7504 static void
7505f_libcall(typval_T *argvars, typval_T *rettv)
7506{
7507 libcall_common(argvars, rettv, VAR_STRING);
7508}
7509
7510/*
7511 * "libcallnr()" function
7512 */
7513 static void
7514f_libcallnr(typval_T *argvars, typval_T *rettv)
7515{
7516 libcall_common(argvars, rettv, VAR_NUMBER);
7517}
7518
7519/*
7520 * "line(string)" function
7521 */
7522 static void
7523f_line(typval_T *argvars, typval_T *rettv)
7524{
7525 linenr_T lnum = 0;
7526 pos_T *fp;
7527 int fnum;
7528
7529 fp = var2fpos(&argvars[0], TRUE, &fnum);
7530 if (fp != NULL)
7531 lnum = fp->lnum;
7532 rettv->vval.v_number = lnum;
7533}
7534
7535/*
7536 * "line2byte(lnum)" function
7537 */
7538 static void
7539f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7540{
7541#ifndef FEAT_BYTEOFF
7542 rettv->vval.v_number = -1;
7543#else
7544 linenr_T lnum;
7545
7546 lnum = get_tv_lnum(argvars);
7547 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7548 rettv->vval.v_number = -1;
7549 else
7550 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7551 if (rettv->vval.v_number >= 0)
7552 ++rettv->vval.v_number;
7553#endif
7554}
7555
7556/*
7557 * "lispindent(lnum)" function
7558 */
7559 static void
7560f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7561{
7562#ifdef FEAT_LISP
7563 pos_T pos;
7564 linenr_T lnum;
7565
7566 pos = curwin->w_cursor;
7567 lnum = get_tv_lnum(argvars);
7568 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7569 {
7570 curwin->w_cursor.lnum = lnum;
7571 rettv->vval.v_number = get_lisp_indent();
7572 curwin->w_cursor = pos;
7573 }
7574 else
7575#endif
7576 rettv->vval.v_number = -1;
7577}
7578
7579/*
7580 * "localtime()" function
7581 */
7582 static void
7583f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7584{
7585 rettv->vval.v_number = (varnumber_T)time(NULL);
7586}
7587
7588static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
7589
7590 static void
7591get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7592{
7593 char_u *keys;
7594 char_u *which;
7595 char_u buf[NUMBUFLEN];
7596 char_u *keys_buf = NULL;
7597 char_u *rhs;
7598 int mode;
7599 int abbr = FALSE;
7600 int get_dict = FALSE;
7601 mapblock_T *mp;
7602 int buffer_local;
7603
7604 /* return empty string for failure */
7605 rettv->v_type = VAR_STRING;
7606 rettv->vval.v_string = NULL;
7607
7608 keys = get_tv_string(&argvars[0]);
7609 if (*keys == NUL)
7610 return;
7611
7612 if (argvars[1].v_type != VAR_UNKNOWN)
7613 {
7614 which = get_tv_string_buf_chk(&argvars[1], buf);
7615 if (argvars[2].v_type != VAR_UNKNOWN)
7616 {
7617 abbr = (int)get_tv_number(&argvars[2]);
7618 if (argvars[3].v_type != VAR_UNKNOWN)
7619 get_dict = (int)get_tv_number(&argvars[3]);
7620 }
7621 }
7622 else
7623 which = (char_u *)"";
7624 if (which == NULL)
7625 return;
7626
7627 mode = get_map_mode(&which, 0);
7628
7629 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7630 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7631 vim_free(keys_buf);
7632
7633 if (!get_dict)
7634 {
7635 /* Return a string. */
7636 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02007637 {
7638 if (*rhs == NUL)
7639 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
7640 else
7641 rettv->vval.v_string = str2special_save(rhs, FALSE);
7642 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007643
7644 }
7645 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7646 {
7647 /* Return a dictionary. */
7648 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7649 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7650 dict_T *dict = rettv->vval.v_dict;
7651
7652 dict_add_nr_str(dict, "lhs", 0L, lhs);
7653 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
7654 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
7655 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
7656 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
7657 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
7658 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
7659 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
7660 dict_add_nr_str(dict, "mode", 0L, mapmode);
7661
7662 vim_free(lhs);
7663 vim_free(mapmode);
7664 }
7665}
7666
7667#ifdef FEAT_FLOAT
7668/*
7669 * "log()" function
7670 */
7671 static void
7672f_log(typval_T *argvars, typval_T *rettv)
7673{
7674 float_T f = 0.0;
7675
7676 rettv->v_type = VAR_FLOAT;
7677 if (get_float_arg(argvars, &f) == OK)
7678 rettv->vval.v_float = log(f);
7679 else
7680 rettv->vval.v_float = 0.0;
7681}
7682
7683/*
7684 * "log10()" function
7685 */
7686 static void
7687f_log10(typval_T *argvars, typval_T *rettv)
7688{
7689 float_T f = 0.0;
7690
7691 rettv->v_type = VAR_FLOAT;
7692 if (get_float_arg(argvars, &f) == OK)
7693 rettv->vval.v_float = log10(f);
7694 else
7695 rettv->vval.v_float = 0.0;
7696}
7697#endif
7698
7699#ifdef FEAT_LUA
7700/*
7701 * "luaeval()" function
7702 */
7703 static void
7704f_luaeval(typval_T *argvars, typval_T *rettv)
7705{
7706 char_u *str;
7707 char_u buf[NUMBUFLEN];
7708
7709 str = get_tv_string_buf(&argvars[0], buf);
7710 do_luaeval(str, argvars + 1, rettv);
7711}
7712#endif
7713
7714/*
7715 * "map()" function
7716 */
7717 static void
7718f_map(typval_T *argvars, typval_T *rettv)
7719{
7720 filter_map(argvars, rettv, TRUE);
7721}
7722
7723/*
7724 * "maparg()" function
7725 */
7726 static void
7727f_maparg(typval_T *argvars, typval_T *rettv)
7728{
7729 get_maparg(argvars, rettv, TRUE);
7730}
7731
7732/*
7733 * "mapcheck()" function
7734 */
7735 static void
7736f_mapcheck(typval_T *argvars, typval_T *rettv)
7737{
7738 get_maparg(argvars, rettv, FALSE);
7739}
7740
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007741typedef enum
7742{
7743 MATCH_END, /* matchend() */
7744 MATCH_MATCH, /* match() */
7745 MATCH_STR, /* matchstr() */
7746 MATCH_LIST, /* matchlist() */
7747 MATCH_POS /* matchstrpos() */
7748} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007749
7750 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007751find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007752{
7753 char_u *str = NULL;
7754 long len = 0;
7755 char_u *expr = NULL;
7756 char_u *pat;
7757 regmatch_T regmatch;
7758 char_u patbuf[NUMBUFLEN];
7759 char_u strbuf[NUMBUFLEN];
7760 char_u *save_cpo;
7761 long start = 0;
7762 long nth = 1;
7763 colnr_T startcol = 0;
7764 int match = 0;
7765 list_T *l = NULL;
7766 listitem_T *li = NULL;
7767 long idx = 0;
7768 char_u *tofree = NULL;
7769
7770 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7771 save_cpo = p_cpo;
7772 p_cpo = (char_u *)"";
7773
7774 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007775 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007776 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007777 /* type MATCH_LIST: return empty list when there are no matches.
7778 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007779 if (rettv_list_alloc(rettv) == FAIL)
7780 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007781 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007782 && (list_append_string(rettv->vval.v_list,
7783 (char_u *)"", 0) == FAIL
7784 || list_append_number(rettv->vval.v_list,
7785 (varnumber_T)-1) == FAIL
7786 || list_append_number(rettv->vval.v_list,
7787 (varnumber_T)-1) == FAIL
7788 || list_append_number(rettv->vval.v_list,
7789 (varnumber_T)-1) == FAIL))
7790 {
7791 list_free(rettv->vval.v_list);
7792 rettv->vval.v_list = NULL;
7793 goto theend;
7794 }
7795 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007796 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007797 {
7798 rettv->v_type = VAR_STRING;
7799 rettv->vval.v_string = NULL;
7800 }
7801
7802 if (argvars[0].v_type == VAR_LIST)
7803 {
7804 if ((l = argvars[0].vval.v_list) == NULL)
7805 goto theend;
7806 li = l->lv_first;
7807 }
7808 else
7809 {
7810 expr = str = get_tv_string(&argvars[0]);
7811 len = (long)STRLEN(str);
7812 }
7813
7814 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
7815 if (pat == NULL)
7816 goto theend;
7817
7818 if (argvars[2].v_type != VAR_UNKNOWN)
7819 {
7820 int error = FALSE;
7821
7822 start = (long)get_tv_number_chk(&argvars[2], &error);
7823 if (error)
7824 goto theend;
7825 if (l != NULL)
7826 {
7827 li = list_find(l, start);
7828 if (li == NULL)
7829 goto theend;
7830 idx = l->lv_idx; /* use the cached index */
7831 }
7832 else
7833 {
7834 if (start < 0)
7835 start = 0;
7836 if (start > len)
7837 goto theend;
7838 /* When "count" argument is there ignore matches before "start",
7839 * otherwise skip part of the string. Differs when pattern is "^"
7840 * or "\<". */
7841 if (argvars[3].v_type != VAR_UNKNOWN)
7842 startcol = start;
7843 else
7844 {
7845 str += start;
7846 len -= start;
7847 }
7848 }
7849
7850 if (argvars[3].v_type != VAR_UNKNOWN)
7851 nth = (long)get_tv_number_chk(&argvars[3], &error);
7852 if (error)
7853 goto theend;
7854 }
7855
7856 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7857 if (regmatch.regprog != NULL)
7858 {
7859 regmatch.rm_ic = p_ic;
7860
7861 for (;;)
7862 {
7863 if (l != NULL)
7864 {
7865 if (li == NULL)
7866 {
7867 match = FALSE;
7868 break;
7869 }
7870 vim_free(tofree);
7871 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
7872 if (str == NULL)
7873 break;
7874 }
7875
7876 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
7877
7878 if (match && --nth <= 0)
7879 break;
7880 if (l == NULL && !match)
7881 break;
7882
7883 /* Advance to just after the match. */
7884 if (l != NULL)
7885 {
7886 li = li->li_next;
7887 ++idx;
7888 }
7889 else
7890 {
7891#ifdef FEAT_MBYTE
7892 startcol = (colnr_T)(regmatch.startp[0]
7893 + (*mb_ptr2len)(regmatch.startp[0]) - str);
7894#else
7895 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
7896#endif
7897 if (startcol > (colnr_T)len
7898 || str + startcol <= regmatch.startp[0])
7899 {
7900 match = FALSE;
7901 break;
7902 }
7903 }
7904 }
7905
7906 if (match)
7907 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007908 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007909 {
7910 listitem_T *li1 = rettv->vval.v_list->lv_first;
7911 listitem_T *li2 = li1->li_next;
7912 listitem_T *li3 = li2->li_next;
7913 listitem_T *li4 = li3->li_next;
7914
7915 vim_free(li1->li_tv.vval.v_string);
7916 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
7917 (int)(regmatch.endp[0] - regmatch.startp[0]));
7918 li3->li_tv.vval.v_number =
7919 (varnumber_T)(regmatch.startp[0] - expr);
7920 li4->li_tv.vval.v_number =
7921 (varnumber_T)(regmatch.endp[0] - expr);
7922 if (l != NULL)
7923 li2->li_tv.vval.v_number = (varnumber_T)idx;
7924 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007925 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007926 {
7927 int i;
7928
7929 /* return list with matched string and submatches */
7930 for (i = 0; i < NSUBEXP; ++i)
7931 {
7932 if (regmatch.endp[i] == NULL)
7933 {
7934 if (list_append_string(rettv->vval.v_list,
7935 (char_u *)"", 0) == FAIL)
7936 break;
7937 }
7938 else if (list_append_string(rettv->vval.v_list,
7939 regmatch.startp[i],
7940 (int)(regmatch.endp[i] - regmatch.startp[i]))
7941 == FAIL)
7942 break;
7943 }
7944 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007945 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007946 {
7947 /* return matched string */
7948 if (l != NULL)
7949 copy_tv(&li->li_tv, rettv);
7950 else
7951 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
7952 (int)(regmatch.endp[0] - regmatch.startp[0]));
7953 }
7954 else if (l != NULL)
7955 rettv->vval.v_number = idx;
7956 else
7957 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007958 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007959 rettv->vval.v_number =
7960 (varnumber_T)(regmatch.startp[0] - str);
7961 else
7962 rettv->vval.v_number =
7963 (varnumber_T)(regmatch.endp[0] - str);
7964 rettv->vval.v_number += (varnumber_T)(str - expr);
7965 }
7966 }
7967 vim_regfree(regmatch.regprog);
7968 }
7969
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007970theend:
7971 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007972 /* matchstrpos() without a list: drop the second item. */
7973 listitem_remove(rettv->vval.v_list,
7974 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007975 vim_free(tofree);
7976 p_cpo = save_cpo;
7977}
7978
7979/*
7980 * "match()" function
7981 */
7982 static void
7983f_match(typval_T *argvars, typval_T *rettv)
7984{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007985 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007986}
7987
7988/*
7989 * "matchadd()" function
7990 */
7991 static void
7992f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7993{
7994#ifdef FEAT_SEARCH_EXTRA
7995 char_u buf[NUMBUFLEN];
7996 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
7997 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
7998 int prio = 10; /* default priority */
7999 int id = -1;
8000 int error = FALSE;
8001 char_u *conceal_char = NULL;
8002
8003 rettv->vval.v_number = -1;
8004
8005 if (grp == NULL || pat == NULL)
8006 return;
8007 if (argvars[2].v_type != VAR_UNKNOWN)
8008 {
8009 prio = (int)get_tv_number_chk(&argvars[2], &error);
8010 if (argvars[3].v_type != VAR_UNKNOWN)
8011 {
8012 id = (int)get_tv_number_chk(&argvars[3], &error);
8013 if (argvars[4].v_type != VAR_UNKNOWN)
8014 {
8015 if (argvars[4].v_type != VAR_DICT)
8016 {
8017 EMSG(_(e_dictreq));
8018 return;
8019 }
8020 if (dict_find(argvars[4].vval.v_dict,
8021 (char_u *)"conceal", -1) != NULL)
8022 conceal_char = get_dict_string(argvars[4].vval.v_dict,
8023 (char_u *)"conceal", FALSE);
8024 }
8025 }
8026 }
8027 if (error == TRUE)
8028 return;
8029 if (id >= 1 && id <= 3)
8030 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02008031 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008032 return;
8033 }
8034
8035 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
8036 conceal_char);
8037#endif
8038}
8039
8040/*
8041 * "matchaddpos()" function
8042 */
8043 static void
8044f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8045{
8046#ifdef FEAT_SEARCH_EXTRA
8047 char_u buf[NUMBUFLEN];
8048 char_u *group;
8049 int prio = 10;
8050 int id = -1;
8051 int error = FALSE;
8052 list_T *l;
8053 char_u *conceal_char = NULL;
8054
8055 rettv->vval.v_number = -1;
8056
8057 group = get_tv_string_buf_chk(&argvars[0], buf);
8058 if (group == NULL)
8059 return;
8060
8061 if (argvars[1].v_type != VAR_LIST)
8062 {
8063 EMSG2(_(e_listarg), "matchaddpos()");
8064 return;
8065 }
8066 l = argvars[1].vval.v_list;
8067 if (l == NULL)
8068 return;
8069
8070 if (argvars[2].v_type != VAR_UNKNOWN)
8071 {
8072 prio = (int)get_tv_number_chk(&argvars[2], &error);
8073 if (argvars[3].v_type != VAR_UNKNOWN)
8074 {
8075 id = (int)get_tv_number_chk(&argvars[3], &error);
8076 if (argvars[4].v_type != VAR_UNKNOWN)
8077 {
8078 if (argvars[4].v_type != VAR_DICT)
8079 {
8080 EMSG(_(e_dictreq));
8081 return;
8082 }
8083 if (dict_find(argvars[4].vval.v_dict,
8084 (char_u *)"conceal", -1) != NULL)
8085 conceal_char = get_dict_string(argvars[4].vval.v_dict,
8086 (char_u *)"conceal", FALSE);
8087 }
8088 }
8089 }
8090 if (error == TRUE)
8091 return;
8092
8093 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8094 if (id == 1 || id == 2)
8095 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02008096 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008097 return;
8098 }
8099
8100 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
8101 conceal_char);
8102#endif
8103}
8104
8105/*
8106 * "matcharg()" function
8107 */
8108 static void
8109f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8110{
8111 if (rettv_list_alloc(rettv) == OK)
8112 {
8113#ifdef FEAT_SEARCH_EXTRA
8114 int id = (int)get_tv_number(&argvars[0]);
8115 matchitem_T *m;
8116
8117 if (id >= 1 && id <= 3)
8118 {
8119 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8120 {
8121 list_append_string(rettv->vval.v_list,
8122 syn_id2name(m->hlg_id), -1);
8123 list_append_string(rettv->vval.v_list, m->pattern, -1);
8124 }
8125 else
8126 {
8127 list_append_string(rettv->vval.v_list, NULL, -1);
8128 list_append_string(rettv->vval.v_list, NULL, -1);
8129 }
8130 }
8131#endif
8132 }
8133}
8134
8135/*
8136 * "matchdelete()" function
8137 */
8138 static void
8139f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8140{
8141#ifdef FEAT_SEARCH_EXTRA
8142 rettv->vval.v_number = match_delete(curwin,
8143 (int)get_tv_number(&argvars[0]), TRUE);
8144#endif
8145}
8146
8147/*
8148 * "matchend()" function
8149 */
8150 static void
8151f_matchend(typval_T *argvars, typval_T *rettv)
8152{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008153 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008154}
8155
8156/*
8157 * "matchlist()" function
8158 */
8159 static void
8160f_matchlist(typval_T *argvars, typval_T *rettv)
8161{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008162 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008163}
8164
8165/*
8166 * "matchstr()" function
8167 */
8168 static void
8169f_matchstr(typval_T *argvars, typval_T *rettv)
8170{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008171 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008172}
8173
8174/*
8175 * "matchstrpos()" function
8176 */
8177 static void
8178f_matchstrpos(typval_T *argvars, typval_T *rettv)
8179{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008180 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008181}
8182
8183static void max_min(typval_T *argvars, typval_T *rettv, int domax);
8184
8185 static void
8186max_min(typval_T *argvars, typval_T *rettv, int domax)
8187{
8188 varnumber_T n = 0;
8189 varnumber_T i;
8190 int error = FALSE;
8191
8192 if (argvars[0].v_type == VAR_LIST)
8193 {
8194 list_T *l;
8195 listitem_T *li;
8196
8197 l = argvars[0].vval.v_list;
8198 if (l != NULL)
8199 {
8200 li = l->lv_first;
8201 if (li != NULL)
8202 {
8203 n = get_tv_number_chk(&li->li_tv, &error);
8204 for (;;)
8205 {
8206 li = li->li_next;
8207 if (li == NULL)
8208 break;
8209 i = get_tv_number_chk(&li->li_tv, &error);
8210 if (domax ? i > n : i < n)
8211 n = i;
8212 }
8213 }
8214 }
8215 }
8216 else if (argvars[0].v_type == VAR_DICT)
8217 {
8218 dict_T *d;
8219 int first = TRUE;
8220 hashitem_T *hi;
8221 int todo;
8222
8223 d = argvars[0].vval.v_dict;
8224 if (d != NULL)
8225 {
8226 todo = (int)d->dv_hashtab.ht_used;
8227 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8228 {
8229 if (!HASHITEM_EMPTY(hi))
8230 {
8231 --todo;
8232 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
8233 if (first)
8234 {
8235 n = i;
8236 first = FALSE;
8237 }
8238 else if (domax ? i > n : i < n)
8239 n = i;
8240 }
8241 }
8242 }
8243 }
8244 else
Bram Moolenaar26b84332016-09-04 21:42:36 +02008245 EMSG2(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008246 rettv->vval.v_number = error ? 0 : n;
8247}
8248
8249/*
8250 * "max()" function
8251 */
8252 static void
8253f_max(typval_T *argvars, typval_T *rettv)
8254{
8255 max_min(argvars, rettv, TRUE);
8256}
8257
8258/*
8259 * "min()" function
8260 */
8261 static void
8262f_min(typval_T *argvars, typval_T *rettv)
8263{
8264 max_min(argvars, rettv, FALSE);
8265}
8266
8267static int mkdir_recurse(char_u *dir, int prot);
8268
8269/*
8270 * Create the directory in which "dir" is located, and higher levels when
8271 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008272 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008273 */
8274 static int
8275mkdir_recurse(char_u *dir, int prot)
8276{
8277 char_u *p;
8278 char_u *updir;
8279 int r = FAIL;
8280
8281 /* Get end of directory name in "dir".
8282 * We're done when it's "/" or "c:/". */
8283 p = gettail_sep(dir);
8284 if (p <= get_past_head(dir))
8285 return OK;
8286
8287 /* If the directory exists we're done. Otherwise: create it.*/
8288 updir = vim_strnsave(dir, (int)(p - dir));
8289 if (updir == NULL)
8290 return FAIL;
8291 if (mch_isdir(updir))
8292 r = OK;
8293 else if (mkdir_recurse(updir, prot) == OK)
8294 r = vim_mkdir_emsg(updir, prot);
8295 vim_free(updir);
8296 return r;
8297}
8298
8299#ifdef vim_mkdir
8300/*
8301 * "mkdir()" function
8302 */
8303 static void
8304f_mkdir(typval_T *argvars, typval_T *rettv)
8305{
8306 char_u *dir;
8307 char_u buf[NUMBUFLEN];
8308 int prot = 0755;
8309
8310 rettv->vval.v_number = FAIL;
8311 if (check_restricted() || check_secure())
8312 return;
8313
8314 dir = get_tv_string_buf(&argvars[0], buf);
8315 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008316 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008317
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008318 if (*gettail(dir) == NUL)
8319 /* remove trailing slashes */
8320 *gettail_sep(dir) = NUL;
8321
8322 if (argvars[1].v_type != VAR_UNKNOWN)
8323 {
8324 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008325 {
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008326 prot = (int)get_tv_number_chk(&argvars[2], NULL);
8327 if (prot == -1)
8328 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008329 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008330 if (STRCMP(get_tv_string(&argvars[1]), "p") == 0)
8331 {
8332 if (mch_isdir(dir))
8333 {
8334 /* With the "p" flag it's OK if the dir already exists. */
8335 rettv->vval.v_number = OK;
8336 return;
8337 }
8338 mkdir_recurse(dir, prot);
8339 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008340 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008341 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008342}
8343#endif
8344
8345/*
8346 * "mode()" function
8347 */
8348 static void
8349f_mode(typval_T *argvars, typval_T *rettv)
8350{
8351 char_u buf[3];
8352
8353 buf[1] = NUL;
8354 buf[2] = NUL;
8355
8356 if (time_for_testing == 93784)
8357 {
8358 /* Testing the two-character code. */
8359 buf[0] = 'x';
8360 buf[1] = '!';
8361 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008362#ifdef FEAT_TERMINAL
8363 else if (term_use_loop())
8364 buf[0] = 't';
8365#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008366 else if (VIsual_active)
8367 {
8368 if (VIsual_select)
8369 buf[0] = VIsual_mode + 's' - 'v';
8370 else
8371 buf[0] = VIsual_mode;
8372 }
8373 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8374 || State == CONFIRM)
8375 {
8376 buf[0] = 'r';
8377 if (State == ASKMORE)
8378 buf[1] = 'm';
8379 else if (State == CONFIRM)
8380 buf[1] = '?';
8381 }
8382 else if (State == EXTERNCMD)
8383 buf[0] = '!';
8384 else if (State & INSERT)
8385 {
8386#ifdef FEAT_VREPLACE
8387 if (State & VREPLACE_FLAG)
8388 {
8389 buf[0] = 'R';
8390 buf[1] = 'v';
8391 }
8392 else
8393#endif
Bram Moolenaare90858d2017-02-01 17:24:34 +01008394 {
8395 if (State & REPLACE_FLAG)
8396 buf[0] = 'R';
8397 else
8398 buf[0] = 'i';
8399#ifdef FEAT_INS_EXPAND
8400 if (ins_compl_active())
8401 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008402 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008403 buf[1] = 'x';
8404#endif
8405 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008406 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008407 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008408 {
8409 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008410 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008411 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008412 else if (exmode_active == EXMODE_NORMAL)
8413 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008414 }
8415 else
8416 {
8417 buf[0] = 'n';
8418 if (finish_op)
8419 buf[1] = 'o';
8420 }
8421
8422 /* Clear out the minor mode when the argument is not a non-zero number or
8423 * non-empty string. */
8424 if (!non_zero_arg(&argvars[0]))
8425 buf[1] = NUL;
8426
8427 rettv->vval.v_string = vim_strsave(buf);
8428 rettv->v_type = VAR_STRING;
8429}
8430
8431#if defined(FEAT_MZSCHEME) || defined(PROTO)
8432/*
8433 * "mzeval()" function
8434 */
8435 static void
8436f_mzeval(typval_T *argvars, typval_T *rettv)
8437{
8438 char_u *str;
8439 char_u buf[NUMBUFLEN];
8440
8441 str = get_tv_string_buf(&argvars[0], buf);
8442 do_mzeval(str, rettv);
8443}
8444
8445 void
8446mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8447{
8448 typval_T argvars[3];
8449
8450 argvars[0].v_type = VAR_STRING;
8451 argvars[0].vval.v_string = name;
8452 copy_tv(args, &argvars[1]);
8453 argvars[2].v_type = VAR_UNKNOWN;
8454 f_call(argvars, rettv);
8455 clear_tv(&argvars[1]);
8456}
8457#endif
8458
8459/*
8460 * "nextnonblank()" function
8461 */
8462 static void
8463f_nextnonblank(typval_T *argvars, typval_T *rettv)
8464{
8465 linenr_T lnum;
8466
8467 for (lnum = get_tv_lnum(argvars); ; ++lnum)
8468 {
8469 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8470 {
8471 lnum = 0;
8472 break;
8473 }
8474 if (*skipwhite(ml_get(lnum)) != NUL)
8475 break;
8476 }
8477 rettv->vval.v_number = lnum;
8478}
8479
8480/*
8481 * "nr2char()" function
8482 */
8483 static void
8484f_nr2char(typval_T *argvars, typval_T *rettv)
8485{
8486 char_u buf[NUMBUFLEN];
8487
8488#ifdef FEAT_MBYTE
8489 if (has_mbyte)
8490 {
8491 int utf8 = 0;
8492
8493 if (argvars[1].v_type != VAR_UNKNOWN)
8494 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
8495 if (utf8)
8496 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8497 else
8498 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8499 }
8500 else
8501#endif
8502 {
8503 buf[0] = (char_u)get_tv_number(&argvars[0]);
8504 buf[1] = NUL;
8505 }
8506 rettv->v_type = VAR_STRING;
8507 rettv->vval.v_string = vim_strsave(buf);
8508}
8509
8510/*
8511 * "or(expr, expr)" function
8512 */
8513 static void
8514f_or(typval_T *argvars, typval_T *rettv)
8515{
8516 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8517 | get_tv_number_chk(&argvars[1], NULL);
8518}
8519
8520/*
8521 * "pathshorten()" function
8522 */
8523 static void
8524f_pathshorten(typval_T *argvars, typval_T *rettv)
8525{
8526 char_u *p;
8527
8528 rettv->v_type = VAR_STRING;
8529 p = get_tv_string_chk(&argvars[0]);
8530 if (p == NULL)
8531 rettv->vval.v_string = NULL;
8532 else
8533 {
8534 p = vim_strsave(p);
8535 rettv->vval.v_string = p;
8536 if (p != NULL)
8537 shorten_dir(p);
8538 }
8539}
8540
8541#ifdef FEAT_PERL
8542/*
8543 * "perleval()" function
8544 */
8545 static void
8546f_perleval(typval_T *argvars, typval_T *rettv)
8547{
8548 char_u *str;
8549 char_u buf[NUMBUFLEN];
8550
8551 str = get_tv_string_buf(&argvars[0], buf);
8552 do_perleval(str, rettv);
8553}
8554#endif
8555
8556#ifdef FEAT_FLOAT
8557/*
8558 * "pow()" function
8559 */
8560 static void
8561f_pow(typval_T *argvars, typval_T *rettv)
8562{
8563 float_T fx = 0.0, fy = 0.0;
8564
8565 rettv->v_type = VAR_FLOAT;
8566 if (get_float_arg(argvars, &fx) == OK
8567 && get_float_arg(&argvars[1], &fy) == OK)
8568 rettv->vval.v_float = pow(fx, fy);
8569 else
8570 rettv->vval.v_float = 0.0;
8571}
8572#endif
8573
8574/*
8575 * "prevnonblank()" function
8576 */
8577 static void
8578f_prevnonblank(typval_T *argvars, typval_T *rettv)
8579{
8580 linenr_T lnum;
8581
8582 lnum = get_tv_lnum(argvars);
8583 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8584 lnum = 0;
8585 else
8586 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8587 --lnum;
8588 rettv->vval.v_number = lnum;
8589}
8590
8591/* This dummy va_list is here because:
8592 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8593 * - locally in the function results in a "used before set" warning
8594 * - using va_start() to initialize it gives "function with fixed args" error */
8595static va_list ap;
8596
8597/*
8598 * "printf()" function
8599 */
8600 static void
8601f_printf(typval_T *argvars, typval_T *rettv)
8602{
8603 char_u buf[NUMBUFLEN];
8604 int len;
8605 char_u *s;
8606 int saved_did_emsg = did_emsg;
8607 char *fmt;
8608
8609 rettv->v_type = VAR_STRING;
8610 rettv->vval.v_string = NULL;
8611
8612 /* Get the required length, allocate the buffer and do it for real. */
8613 did_emsg = FALSE;
8614 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008615 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008616 if (!did_emsg)
8617 {
8618 s = alloc(len + 1);
8619 if (s != NULL)
8620 {
8621 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008622 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8623 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008624 }
8625 }
8626 did_emsg |= saved_did_emsg;
8627}
8628
Bram Moolenaarf2732452018-06-03 14:47:35 +02008629#ifdef FEAT_JOB_CHANNEL
8630/*
8631 * "prompt_setcallback({buffer}, {callback})" function
8632 */
8633 static void
8634f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
8635{
8636 buf_T *buf;
8637 char_u *callback;
8638 partial_T *partial;
8639
8640 if (check_secure())
8641 return;
8642 buf = get_buf_tv(&argvars[0], FALSE);
8643 if (buf == NULL)
8644 return;
8645
8646 callback = get_callback(&argvars[1], &partial);
8647 if (callback == NULL)
8648 return;
8649
8650 free_callback(buf->b_prompt_callback, buf->b_prompt_partial);
8651 if (partial == NULL)
8652 buf->b_prompt_callback = vim_strsave(callback);
8653 else
8654 /* pointer into the partial */
8655 buf->b_prompt_callback = callback;
8656 buf->b_prompt_partial = partial;
8657}
8658
8659/*
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02008660 * "prompt_setinterrupt({buffer}, {callback})" function
8661 */
8662 static void
8663f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
8664{
8665 buf_T *buf;
8666 char_u *callback;
8667 partial_T *partial;
8668
8669 if (check_secure())
8670 return;
8671 buf = get_buf_tv(&argvars[0], FALSE);
8672 if (buf == NULL)
8673 return;
8674
8675 callback = get_callback(&argvars[1], &partial);
8676 if (callback == NULL)
8677 return;
8678
8679 free_callback(buf->b_prompt_interrupt, buf->b_prompt_int_partial);
8680 if (partial == NULL)
8681 buf->b_prompt_interrupt = vim_strsave(callback);
8682 else
8683 /* pointer into the partial */
8684 buf->b_prompt_interrupt = callback;
8685 buf->b_prompt_int_partial = partial;
8686}
8687
8688/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02008689 * "prompt_setprompt({buffer}, {text})" function
8690 */
8691 static void
8692f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
8693{
8694 buf_T *buf;
8695 char_u *text;
8696
8697 if (check_secure())
8698 return;
8699 buf = get_buf_tv(&argvars[0], FALSE);
8700 if (buf == NULL)
8701 return;
8702
8703 text = get_tv_string(&argvars[1]);
8704 vim_free(buf->b_prompt_text);
8705 buf->b_prompt_text = vim_strsave(text);
8706}
8707#endif
8708
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008709/*
8710 * "pumvisible()" function
8711 */
8712 static void
8713f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8714{
8715#ifdef FEAT_INS_EXPAND
8716 if (pum_visible())
8717 rettv->vval.v_number = 1;
8718#endif
8719}
8720
8721#ifdef FEAT_PYTHON3
8722/*
8723 * "py3eval()" function
8724 */
8725 static void
8726f_py3eval(typval_T *argvars, typval_T *rettv)
8727{
8728 char_u *str;
8729 char_u buf[NUMBUFLEN];
8730
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008731 if (p_pyx == 0)
8732 p_pyx = 3;
8733
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008734 str = get_tv_string_buf(&argvars[0], buf);
8735 do_py3eval(str, rettv);
8736}
8737#endif
8738
8739#ifdef FEAT_PYTHON
8740/*
8741 * "pyeval()" function
8742 */
8743 static void
8744f_pyeval(typval_T *argvars, typval_T *rettv)
8745{
8746 char_u *str;
8747 char_u buf[NUMBUFLEN];
8748
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008749 if (p_pyx == 0)
8750 p_pyx = 2;
8751
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008752 str = get_tv_string_buf(&argvars[0], buf);
8753 do_pyeval(str, rettv);
8754}
8755#endif
8756
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008757#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8758/*
8759 * "pyxeval()" function
8760 */
8761 static void
8762f_pyxeval(typval_T *argvars, typval_T *rettv)
8763{
8764# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8765 init_pyxversion();
8766 if (p_pyx == 2)
8767 f_pyeval(argvars, rettv);
8768 else
8769 f_py3eval(argvars, rettv);
8770# elif defined(FEAT_PYTHON)
8771 f_pyeval(argvars, rettv);
8772# elif defined(FEAT_PYTHON3)
8773 f_py3eval(argvars, rettv);
8774# endif
8775}
8776#endif
8777
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008778/*
8779 * "range()" function
8780 */
8781 static void
8782f_range(typval_T *argvars, typval_T *rettv)
8783{
8784 varnumber_T start;
8785 varnumber_T end;
8786 varnumber_T stride = 1;
8787 varnumber_T i;
8788 int error = FALSE;
8789
8790 start = get_tv_number_chk(&argvars[0], &error);
8791 if (argvars[1].v_type == VAR_UNKNOWN)
8792 {
8793 end = start - 1;
8794 start = 0;
8795 }
8796 else
8797 {
8798 end = get_tv_number_chk(&argvars[1], &error);
8799 if (argvars[2].v_type != VAR_UNKNOWN)
8800 stride = get_tv_number_chk(&argvars[2], &error);
8801 }
8802
8803 if (error)
8804 return; /* type error; errmsg already given */
8805 if (stride == 0)
8806 EMSG(_("E726: Stride is zero"));
8807 else if (stride > 0 ? end + 1 < start : end - 1 > start)
8808 EMSG(_("E727: Start past end"));
8809 else
8810 {
8811 if (rettv_list_alloc(rettv) == OK)
8812 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8813 if (list_append_number(rettv->vval.v_list,
8814 (varnumber_T)i) == FAIL)
8815 break;
8816 }
8817}
8818
8819/*
8820 * "readfile()" function
8821 */
8822 static void
8823f_readfile(typval_T *argvars, typval_T *rettv)
8824{
8825 int binary = FALSE;
8826 int failed = FALSE;
8827 char_u *fname;
8828 FILE *fd;
8829 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
8830 int io_size = sizeof(buf);
8831 int readlen; /* size of last fread() */
8832 char_u *prev = NULL; /* previously read bytes, if any */
8833 long prevlen = 0; /* length of data in prev */
8834 long prevsize = 0; /* size of prev buffer */
8835 long maxline = MAXLNUM;
8836 long cnt = 0;
8837 char_u *p; /* position in buf */
8838 char_u *start; /* start of current line */
8839
8840 if (argvars[1].v_type != VAR_UNKNOWN)
8841 {
8842 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
8843 binary = TRUE;
8844 if (argvars[2].v_type != VAR_UNKNOWN)
8845 maxline = (long)get_tv_number(&argvars[2]);
8846 }
8847
8848 if (rettv_list_alloc(rettv) == FAIL)
8849 return;
8850
8851 /* Always open the file in binary mode, library functions have a mind of
8852 * their own about CR-LF conversion. */
8853 fname = get_tv_string(&argvars[0]);
8854 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
8855 {
8856 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
8857 return;
8858 }
8859
8860 while (cnt < maxline || maxline < 0)
8861 {
8862 readlen = (int)fread(buf, 1, io_size, fd);
8863
8864 /* This for loop processes what was read, but is also entered at end
8865 * of file so that either:
8866 * - an incomplete line gets written
8867 * - a "binary" file gets an empty line at the end if it ends in a
8868 * newline. */
8869 for (p = buf, start = buf;
8870 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
8871 ++p)
8872 {
8873 if (*p == '\n' || readlen <= 0)
8874 {
8875 listitem_T *li;
8876 char_u *s = NULL;
8877 long_u len = p - start;
8878
8879 /* Finished a line. Remove CRs before NL. */
8880 if (readlen > 0 && !binary)
8881 {
8882 while (len > 0 && start[len - 1] == '\r')
8883 --len;
8884 /* removal may cross back to the "prev" string */
8885 if (len == 0)
8886 while (prevlen > 0 && prev[prevlen - 1] == '\r')
8887 --prevlen;
8888 }
8889 if (prevlen == 0)
8890 s = vim_strnsave(start, (int)len);
8891 else
8892 {
8893 /* Change "prev" buffer to be the right size. This way
8894 * the bytes are only copied once, and very long lines are
8895 * allocated only once. */
8896 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
8897 {
8898 mch_memmove(s + prevlen, start, len);
8899 s[prevlen + len] = NUL;
8900 prev = NULL; /* the list will own the string */
8901 prevlen = prevsize = 0;
8902 }
8903 }
8904 if (s == NULL)
8905 {
8906 do_outofmem_msg((long_u) prevlen + len + 1);
8907 failed = TRUE;
8908 break;
8909 }
8910
8911 if ((li = listitem_alloc()) == NULL)
8912 {
8913 vim_free(s);
8914 failed = TRUE;
8915 break;
8916 }
8917 li->li_tv.v_type = VAR_STRING;
8918 li->li_tv.v_lock = 0;
8919 li->li_tv.vval.v_string = s;
8920 list_append(rettv->vval.v_list, li);
8921
8922 start = p + 1; /* step over newline */
8923 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
8924 break;
8925 }
8926 else if (*p == NUL)
8927 *p = '\n';
8928#ifdef FEAT_MBYTE
8929 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
8930 * when finding the BF and check the previous two bytes. */
8931 else if (*p == 0xbf && enc_utf8 && !binary)
8932 {
8933 /* Find the two bytes before the 0xbf. If p is at buf, or buf
8934 * + 1, these may be in the "prev" string. */
8935 char_u back1 = p >= buf + 1 ? p[-1]
8936 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
8937 char_u back2 = p >= buf + 2 ? p[-2]
8938 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
8939 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
8940
8941 if (back2 == 0xef && back1 == 0xbb)
8942 {
8943 char_u *dest = p - 2;
8944
8945 /* Usually a BOM is at the beginning of a file, and so at
8946 * the beginning of a line; then we can just step over it.
8947 */
8948 if (start == dest)
8949 start = p + 1;
8950 else
8951 {
8952 /* have to shuffle buf to close gap */
8953 int adjust_prevlen = 0;
8954
8955 if (dest < buf)
8956 {
8957 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
8958 dest = buf;
8959 }
8960 if (readlen > p - buf + 1)
8961 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
8962 readlen -= 3 - adjust_prevlen;
8963 prevlen -= adjust_prevlen;
8964 p = dest - 1;
8965 }
8966 }
8967 }
8968#endif
8969 } /* for */
8970
8971 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
8972 break;
8973 if (start < p)
8974 {
8975 /* There's part of a line in buf, store it in "prev". */
8976 if (p - start + prevlen >= prevsize)
8977 {
8978 /* need bigger "prev" buffer */
8979 char_u *newprev;
8980
8981 /* A common use case is ordinary text files and "prev" gets a
8982 * fragment of a line, so the first allocation is made
8983 * small, to avoid repeatedly 'allocing' large and
8984 * 'reallocing' small. */
8985 if (prevsize == 0)
8986 prevsize = (long)(p - start);
8987 else
8988 {
8989 long grow50pc = (prevsize * 3) / 2;
8990 long growmin = (long)((p - start) * 2 + prevlen);
8991 prevsize = grow50pc > growmin ? grow50pc : growmin;
8992 }
8993 newprev = prev == NULL ? alloc(prevsize)
8994 : vim_realloc(prev, prevsize);
8995 if (newprev == NULL)
8996 {
8997 do_outofmem_msg((long_u)prevsize);
8998 failed = TRUE;
8999 break;
9000 }
9001 prev = newprev;
9002 }
9003 /* Add the line part to end of "prev". */
9004 mch_memmove(prev + prevlen, start, p - start);
9005 prevlen += (long)(p - start);
9006 }
9007 } /* while */
9008
9009 /*
9010 * For a negative line count use only the lines at the end of the file,
9011 * free the rest.
9012 */
9013 if (!failed && maxline < 0)
9014 while (cnt > -maxline)
9015 {
9016 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
9017 --cnt;
9018 }
9019
9020 if (failed)
9021 {
9022 list_free(rettv->vval.v_list);
9023 /* readfile doc says an empty list is returned on error */
9024 rettv->vval.v_list = list_alloc();
9025 }
9026
9027 vim_free(prev);
9028 fclose(fd);
9029}
9030
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009031 static void
9032return_register(int regname, typval_T *rettv)
9033{
9034 char_u buf[2] = {0, 0};
9035
9036 buf[0] = (char_u)regname;
9037 rettv->v_type = VAR_STRING;
9038 rettv->vval.v_string = vim_strsave(buf);
9039}
9040
9041/*
9042 * "reg_executing()" function
9043 */
9044 static void
9045f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9046{
9047 return_register(reg_executing, rettv);
9048}
9049
9050/*
9051 * "reg_recording()" function
9052 */
9053 static void
9054f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9055{
9056 return_register(reg_recording, rettv);
9057}
9058
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009059#if defined(FEAT_RELTIME)
9060static int list2proftime(typval_T *arg, proftime_T *tm);
9061
9062/*
9063 * Convert a List to proftime_T.
9064 * Return FAIL when there is something wrong.
9065 */
9066 static int
9067list2proftime(typval_T *arg, proftime_T *tm)
9068{
9069 long n1, n2;
9070 int error = FALSE;
9071
9072 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9073 || arg->vval.v_list->lv_len != 2)
9074 return FAIL;
9075 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9076 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
9077# ifdef WIN3264
9078 tm->HighPart = n1;
9079 tm->LowPart = n2;
9080# else
9081 tm->tv_sec = n1;
9082 tm->tv_usec = n2;
9083# endif
9084 return error ? FAIL : OK;
9085}
9086#endif /* FEAT_RELTIME */
9087
9088/*
9089 * "reltime()" function
9090 */
9091 static void
9092f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9093{
9094#ifdef FEAT_RELTIME
9095 proftime_T res;
9096 proftime_T start;
9097
9098 if (argvars[0].v_type == VAR_UNKNOWN)
9099 {
9100 /* No arguments: get current time. */
9101 profile_start(&res);
9102 }
9103 else if (argvars[1].v_type == VAR_UNKNOWN)
9104 {
9105 if (list2proftime(&argvars[0], &res) == FAIL)
9106 return;
9107 profile_end(&res);
9108 }
9109 else
9110 {
9111 /* Two arguments: compute the difference. */
9112 if (list2proftime(&argvars[0], &start) == FAIL
9113 || list2proftime(&argvars[1], &res) == FAIL)
9114 return;
9115 profile_sub(&res, &start);
9116 }
9117
9118 if (rettv_list_alloc(rettv) == OK)
9119 {
9120 long n1, n2;
9121
9122# ifdef WIN3264
9123 n1 = res.HighPart;
9124 n2 = res.LowPart;
9125# else
9126 n1 = res.tv_sec;
9127 n2 = res.tv_usec;
9128# endif
9129 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9130 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9131 }
9132#endif
9133}
9134
9135#ifdef FEAT_FLOAT
9136/*
9137 * "reltimefloat()" function
9138 */
9139 static void
9140f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9141{
9142# ifdef FEAT_RELTIME
9143 proftime_T tm;
9144# endif
9145
9146 rettv->v_type = VAR_FLOAT;
9147 rettv->vval.v_float = 0;
9148# ifdef FEAT_RELTIME
9149 if (list2proftime(&argvars[0], &tm) == OK)
9150 rettv->vval.v_float = profile_float(&tm);
9151# endif
9152}
9153#endif
9154
9155/*
9156 * "reltimestr()" function
9157 */
9158 static void
9159f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9160{
9161#ifdef FEAT_RELTIME
9162 proftime_T tm;
9163#endif
9164
9165 rettv->v_type = VAR_STRING;
9166 rettv->vval.v_string = NULL;
9167#ifdef FEAT_RELTIME
9168 if (list2proftime(&argvars[0], &tm) == OK)
9169 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9170#endif
9171}
9172
9173#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
9174static void make_connection(void);
9175static int check_connection(void);
9176
9177 static void
9178make_connection(void)
9179{
9180 if (X_DISPLAY == NULL
9181# ifdef FEAT_GUI
9182 && !gui.in_use
9183# endif
9184 )
9185 {
9186 x_force_connect = TRUE;
9187 setup_term_clip();
9188 x_force_connect = FALSE;
9189 }
9190}
9191
9192 static int
9193check_connection(void)
9194{
9195 make_connection();
9196 if (X_DISPLAY == NULL)
9197 {
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009198 EMSG(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009199 return FAIL;
9200 }
9201 return OK;
9202}
9203#endif
9204
9205#ifdef FEAT_CLIENTSERVER
9206 static void
9207remote_common(typval_T *argvars, typval_T *rettv, int expr)
9208{
9209 char_u *server_name;
9210 char_u *keys;
9211 char_u *r = NULL;
9212 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009213 int timeout = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009214# ifdef WIN32
9215 HWND w;
9216# else
9217 Window w;
9218# endif
9219
9220 if (check_restricted() || check_secure())
9221 return;
9222
9223# ifdef FEAT_X11
9224 if (check_connection() == FAIL)
9225 return;
9226# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009227 if (argvars[2].v_type != VAR_UNKNOWN
9228 && argvars[3].v_type != VAR_UNKNOWN)
9229 timeout = get_tv_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009230
9231 server_name = get_tv_string_chk(&argvars[0]);
9232 if (server_name == NULL)
9233 return; /* type error; errmsg already given */
9234 keys = get_tv_string_buf(&argvars[1], buf);
9235# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009236 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009237# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009238 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9239 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009240# endif
9241 {
9242 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009243 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009244 EMSG(r); /* sending worked but evaluation failed */
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009245 vim_free(r);
9246 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009247 else
9248 EMSG2(_("E241: Unable to send to %s"), server_name);
9249 return;
9250 }
9251
9252 rettv->vval.v_string = r;
9253
9254 if (argvars[2].v_type != VAR_UNKNOWN)
9255 {
9256 dictitem_T v;
9257 char_u str[30];
9258 char_u *idvar;
9259
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009260 idvar = get_tv_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009261 if (idvar != NULL && *idvar != NUL)
9262 {
9263 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9264 v.di_tv.v_type = VAR_STRING;
9265 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009266 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009267 vim_free(v.di_tv.vval.v_string);
9268 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009269 }
9270}
9271#endif
9272
9273/*
9274 * "remote_expr()" function
9275 */
9276 static void
9277f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9278{
9279 rettv->v_type = VAR_STRING;
9280 rettv->vval.v_string = NULL;
9281#ifdef FEAT_CLIENTSERVER
9282 remote_common(argvars, rettv, TRUE);
9283#endif
9284}
9285
9286/*
9287 * "remote_foreground()" function
9288 */
9289 static void
9290f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9291{
9292#ifdef FEAT_CLIENTSERVER
9293# ifdef WIN32
9294 /* On Win32 it's done in this application. */
9295 {
9296 char_u *server_name = get_tv_string_chk(&argvars[0]);
9297
9298 if (server_name != NULL)
9299 serverForeground(server_name);
9300 }
9301# else
9302 /* Send a foreground() expression to the server. */
9303 argvars[1].v_type = VAR_STRING;
9304 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9305 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009306 rettv->v_type = VAR_STRING;
9307 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009308 remote_common(argvars, rettv, TRUE);
9309 vim_free(argvars[1].vval.v_string);
9310# endif
9311#endif
9312}
9313
9314 static void
9315f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
9316{
9317#ifdef FEAT_CLIENTSERVER
9318 dictitem_T v;
9319 char_u *s = NULL;
9320# ifdef WIN32
9321 long_u n = 0;
9322# endif
9323 char_u *serverid;
9324
9325 if (check_restricted() || check_secure())
9326 {
9327 rettv->vval.v_number = -1;
9328 return;
9329 }
9330 serverid = get_tv_string_chk(&argvars[0]);
9331 if (serverid == NULL)
9332 {
9333 rettv->vval.v_number = -1;
9334 return; /* type error; errmsg already given */
9335 }
9336# ifdef WIN32
9337 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
9338 if (n == 0)
9339 rettv->vval.v_number = -1;
9340 else
9341 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009342 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009343 rettv->vval.v_number = (s != NULL);
9344 }
9345# else
9346 if (check_connection() == FAIL)
9347 return;
9348
9349 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
9350 serverStrToWin(serverid), &s);
9351# endif
9352
9353 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
9354 {
9355 char_u *retvar;
9356
9357 v.di_tv.v_type = VAR_STRING;
9358 v.di_tv.vval.v_string = vim_strsave(s);
9359 retvar = get_tv_string_chk(&argvars[1]);
9360 if (retvar != NULL)
9361 set_var(retvar, &v.di_tv, FALSE);
9362 vim_free(v.di_tv.vval.v_string);
9363 }
9364#else
9365 rettv->vval.v_number = -1;
9366#endif
9367}
9368
9369 static void
9370f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
9371{
9372 char_u *r = NULL;
9373
9374#ifdef FEAT_CLIENTSERVER
9375 char_u *serverid = get_tv_string_chk(&argvars[0]);
9376
9377 if (serverid != NULL && !check_restricted() && !check_secure())
9378 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009379 int timeout = 0;
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009380# ifdef WIN32
9381 /* The server's HWND is encoded in the 'id' parameter */
9382 long_u n = 0;
9383# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009384
9385 if (argvars[1].v_type != VAR_UNKNOWN)
9386 timeout = get_tv_number(&argvars[1]);
9387
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009388# ifdef WIN32
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009389 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
9390 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009391 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009392 if (r == NULL)
9393# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009394 if (check_connection() == FAIL
9395 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
9396 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009397# endif
9398 EMSG(_("E277: Unable to read a server reply"));
9399 }
9400#endif
9401 rettv->v_type = VAR_STRING;
9402 rettv->vval.v_string = r;
9403}
9404
9405/*
9406 * "remote_send()" function
9407 */
9408 static void
9409f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9410{
9411 rettv->v_type = VAR_STRING;
9412 rettv->vval.v_string = NULL;
9413#ifdef FEAT_CLIENTSERVER
9414 remote_common(argvars, rettv, FALSE);
9415#endif
9416}
9417
9418/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009419 * "remote_startserver()" function
9420 */
9421 static void
9422f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9423{
9424#ifdef FEAT_CLIENTSERVER
9425 char_u *server = get_tv_string_chk(&argvars[0]);
9426
9427 if (server == NULL)
9428 return; /* type error; errmsg already given */
9429 if (serverName != NULL)
9430 EMSG(_("E941: already started a server"));
9431 else
9432 {
9433# ifdef FEAT_X11
9434 if (check_connection() == OK)
9435 serverRegisterName(X_DISPLAY, server);
9436# else
9437 serverSetName(server);
9438# endif
9439 }
9440#else
9441 EMSG(_("E942: +clientserver feature not available"));
9442#endif
9443}
9444
9445/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009446 * "remove()" function
9447 */
9448 static void
9449f_remove(typval_T *argvars, typval_T *rettv)
9450{
9451 list_T *l;
9452 listitem_T *item, *item2;
9453 listitem_T *li;
9454 long idx;
9455 long end;
9456 char_u *key;
9457 dict_T *d;
9458 dictitem_T *di;
9459 char_u *arg_errmsg = (char_u *)N_("remove() argument");
9460
9461 if (argvars[0].v_type == VAR_DICT)
9462 {
9463 if (argvars[2].v_type != VAR_UNKNOWN)
9464 EMSG2(_(e_toomanyarg), "remove()");
9465 else if ((d = argvars[0].vval.v_dict) != NULL
9466 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
9467 {
9468 key = get_tv_string_chk(&argvars[1]);
9469 if (key != NULL)
9470 {
9471 di = dict_find(d, key, -1);
9472 if (di == NULL)
9473 EMSG2(_(e_dictkey), key);
9474 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
9475 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
9476 {
9477 *rettv = di->di_tv;
9478 init_tv(&di->di_tv);
9479 dictitem_remove(d, di);
9480 }
9481 }
9482 }
9483 }
9484 else if (argvars[0].v_type != VAR_LIST)
9485 EMSG2(_(e_listdictarg), "remove()");
9486 else if ((l = argvars[0].vval.v_list) != NULL
9487 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
9488 {
9489 int error = FALSE;
9490
9491 idx = (long)get_tv_number_chk(&argvars[1], &error);
9492 if (error)
9493 ; /* type error: do nothing, errmsg already given */
9494 else if ((item = list_find(l, idx)) == NULL)
9495 EMSGN(_(e_listidx), idx);
9496 else
9497 {
9498 if (argvars[2].v_type == VAR_UNKNOWN)
9499 {
9500 /* Remove one item, return its value. */
9501 vimlist_remove(l, item, item);
9502 *rettv = item->li_tv;
9503 vim_free(item);
9504 }
9505 else
9506 {
9507 /* Remove range of items, return list with values. */
9508 end = (long)get_tv_number_chk(&argvars[2], &error);
9509 if (error)
9510 ; /* type error: do nothing */
9511 else if ((item2 = list_find(l, end)) == NULL)
9512 EMSGN(_(e_listidx), end);
9513 else
9514 {
9515 int cnt = 0;
9516
9517 for (li = item; li != NULL; li = li->li_next)
9518 {
9519 ++cnt;
9520 if (li == item2)
9521 break;
9522 }
9523 if (li == NULL) /* didn't find "item2" after "item" */
9524 EMSG(_(e_invrange));
9525 else
9526 {
9527 vimlist_remove(l, item, item2);
9528 if (rettv_list_alloc(rettv) == OK)
9529 {
9530 l = rettv->vval.v_list;
9531 l->lv_first = item;
9532 l->lv_last = item2;
9533 item->li_prev = NULL;
9534 item2->li_next = NULL;
9535 l->lv_len = cnt;
9536 }
9537 }
9538 }
9539 }
9540 }
9541 }
9542}
9543
9544/*
9545 * "rename({from}, {to})" function
9546 */
9547 static void
9548f_rename(typval_T *argvars, typval_T *rettv)
9549{
9550 char_u buf[NUMBUFLEN];
9551
9552 if (check_restricted() || check_secure())
9553 rettv->vval.v_number = -1;
9554 else
9555 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
9556 get_tv_string_buf(&argvars[1], buf));
9557}
9558
9559/*
9560 * "repeat()" function
9561 */
9562 static void
9563f_repeat(typval_T *argvars, typval_T *rettv)
9564{
9565 char_u *p;
9566 int n;
9567 int slen;
9568 int len;
9569 char_u *r;
9570 int i;
9571
9572 n = (int)get_tv_number(&argvars[1]);
9573 if (argvars[0].v_type == VAR_LIST)
9574 {
9575 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9576 while (n-- > 0)
9577 if (list_extend(rettv->vval.v_list,
9578 argvars[0].vval.v_list, NULL) == FAIL)
9579 break;
9580 }
9581 else
9582 {
9583 p = get_tv_string(&argvars[0]);
9584 rettv->v_type = VAR_STRING;
9585 rettv->vval.v_string = NULL;
9586
9587 slen = (int)STRLEN(p);
9588 len = slen * n;
9589 if (len <= 0)
9590 return;
9591
9592 r = alloc(len + 1);
9593 if (r != NULL)
9594 {
9595 for (i = 0; i < n; i++)
9596 mch_memmove(r + i * slen, p, (size_t)slen);
9597 r[len] = NUL;
9598 }
9599
9600 rettv->vval.v_string = r;
9601 }
9602}
9603
9604/*
9605 * "resolve()" function
9606 */
9607 static void
9608f_resolve(typval_T *argvars, typval_T *rettv)
9609{
9610 char_u *p;
9611#ifdef HAVE_READLINK
9612 char_u *buf = NULL;
9613#endif
9614
9615 p = get_tv_string(&argvars[0]);
9616#ifdef FEAT_SHORTCUT
9617 {
9618 char_u *v = NULL;
9619
9620 v = mch_resolve_shortcut(p);
9621 if (v != NULL)
9622 rettv->vval.v_string = v;
9623 else
9624 rettv->vval.v_string = vim_strsave(p);
9625 }
9626#else
9627# ifdef HAVE_READLINK
9628 {
9629 char_u *cpy;
9630 int len;
9631 char_u *remain = NULL;
9632 char_u *q;
9633 int is_relative_to_current = FALSE;
9634 int has_trailing_pathsep = FALSE;
9635 int limit = 100;
9636
9637 p = vim_strsave(p);
9638
9639 if (p[0] == '.' && (vim_ispathsep(p[1])
9640 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9641 is_relative_to_current = TRUE;
9642
9643 len = STRLEN(p);
9644 if (len > 0 && after_pathsep(p, p + len))
9645 {
9646 has_trailing_pathsep = TRUE;
9647 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9648 }
9649
9650 q = getnextcomp(p);
9651 if (*q != NUL)
9652 {
9653 /* Separate the first path component in "p", and keep the
9654 * remainder (beginning with the path separator). */
9655 remain = vim_strsave(q - 1);
9656 q[-1] = NUL;
9657 }
9658
9659 buf = alloc(MAXPATHL + 1);
9660 if (buf == NULL)
9661 goto fail;
9662
9663 for (;;)
9664 {
9665 for (;;)
9666 {
9667 len = readlink((char *)p, (char *)buf, MAXPATHL);
9668 if (len <= 0)
9669 break;
9670 buf[len] = NUL;
9671
9672 if (limit-- == 0)
9673 {
9674 vim_free(p);
9675 vim_free(remain);
9676 EMSG(_("E655: Too many symbolic links (cycle?)"));
9677 rettv->vval.v_string = NULL;
9678 goto fail;
9679 }
9680
9681 /* Ensure that the result will have a trailing path separator
9682 * if the argument has one. */
9683 if (remain == NULL && has_trailing_pathsep)
9684 add_pathsep(buf);
9685
9686 /* Separate the first path component in the link value and
9687 * concatenate the remainders. */
9688 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9689 if (*q != NUL)
9690 {
9691 if (remain == NULL)
9692 remain = vim_strsave(q - 1);
9693 else
9694 {
9695 cpy = concat_str(q - 1, remain);
9696 if (cpy != NULL)
9697 {
9698 vim_free(remain);
9699 remain = cpy;
9700 }
9701 }
9702 q[-1] = NUL;
9703 }
9704
9705 q = gettail(p);
9706 if (q > p && *q == NUL)
9707 {
9708 /* Ignore trailing path separator. */
9709 q[-1] = NUL;
9710 q = gettail(p);
9711 }
9712 if (q > p && !mch_isFullName(buf))
9713 {
9714 /* symlink is relative to directory of argument */
9715 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
9716 if (cpy != NULL)
9717 {
9718 STRCPY(cpy, p);
9719 STRCPY(gettail(cpy), buf);
9720 vim_free(p);
9721 p = cpy;
9722 }
9723 }
9724 else
9725 {
9726 vim_free(p);
9727 p = vim_strsave(buf);
9728 }
9729 }
9730
9731 if (remain == NULL)
9732 break;
9733
9734 /* Append the first path component of "remain" to "p". */
9735 q = getnextcomp(remain + 1);
9736 len = q - remain - (*q != NUL);
9737 cpy = vim_strnsave(p, STRLEN(p) + len);
9738 if (cpy != NULL)
9739 {
9740 STRNCAT(cpy, remain, len);
9741 vim_free(p);
9742 p = cpy;
9743 }
9744 /* Shorten "remain". */
9745 if (*q != NUL)
9746 STRMOVE(remain, q - 1);
9747 else
Bram Moolenaard23a8232018-02-10 18:45:26 +01009748 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009749 }
9750
9751 /* If the result is a relative path name, make it explicitly relative to
9752 * the current directory if and only if the argument had this form. */
9753 if (!vim_ispathsep(*p))
9754 {
9755 if (is_relative_to_current
9756 && *p != NUL
9757 && !(p[0] == '.'
9758 && (p[1] == NUL
9759 || vim_ispathsep(p[1])
9760 || (p[1] == '.'
9761 && (p[2] == NUL
9762 || vim_ispathsep(p[2]))))))
9763 {
9764 /* Prepend "./". */
9765 cpy = concat_str((char_u *)"./", p);
9766 if (cpy != NULL)
9767 {
9768 vim_free(p);
9769 p = cpy;
9770 }
9771 }
9772 else if (!is_relative_to_current)
9773 {
9774 /* Strip leading "./". */
9775 q = p;
9776 while (q[0] == '.' && vim_ispathsep(q[1]))
9777 q += 2;
9778 if (q > p)
9779 STRMOVE(p, p + 2);
9780 }
9781 }
9782
9783 /* Ensure that the result will have no trailing path separator
9784 * if the argument had none. But keep "/" or "//". */
9785 if (!has_trailing_pathsep)
9786 {
9787 q = p + STRLEN(p);
9788 if (after_pathsep(p, q))
9789 *gettail_sep(p) = NUL;
9790 }
9791
9792 rettv->vval.v_string = p;
9793 }
9794# else
9795 rettv->vval.v_string = vim_strsave(p);
9796# endif
9797#endif
9798
9799 simplify_filename(rettv->vval.v_string);
9800
9801#ifdef HAVE_READLINK
9802fail:
9803 vim_free(buf);
9804#endif
9805 rettv->v_type = VAR_STRING;
9806}
9807
9808/*
9809 * "reverse({list})" function
9810 */
9811 static void
9812f_reverse(typval_T *argvars, typval_T *rettv)
9813{
9814 list_T *l;
9815 listitem_T *li, *ni;
9816
9817 if (argvars[0].v_type != VAR_LIST)
9818 EMSG2(_(e_listarg), "reverse()");
9819 else if ((l = argvars[0].vval.v_list) != NULL
9820 && !tv_check_lock(l->lv_lock,
9821 (char_u *)N_("reverse() argument"), TRUE))
9822 {
9823 li = l->lv_last;
9824 l->lv_first = l->lv_last = NULL;
9825 l->lv_len = 0;
9826 while (li != NULL)
9827 {
9828 ni = li->li_prev;
9829 list_append(l, li);
9830 li = ni;
9831 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009832 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009833 l->lv_idx = l->lv_len - l->lv_idx - 1;
9834 }
9835}
9836
9837#define SP_NOMOVE 0x01 /* don't move cursor */
9838#define SP_REPEAT 0x02 /* repeat to find outer pair */
9839#define SP_RETCOUNT 0x04 /* return matchcount */
9840#define SP_SETPCMARK 0x08 /* set previous context mark */
9841#define SP_START 0x10 /* accept match at start position */
9842#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
9843#define SP_END 0x40 /* leave cursor at end of match */
9844#define SP_COLUMN 0x80 /* start at cursor column */
9845
9846static int get_search_arg(typval_T *varp, int *flagsp);
9847
9848/*
9849 * Get flags for a search function.
9850 * Possibly sets "p_ws".
9851 * Returns BACKWARD, FORWARD or zero (for an error).
9852 */
9853 static int
9854get_search_arg(typval_T *varp, int *flagsp)
9855{
9856 int dir = FORWARD;
9857 char_u *flags;
9858 char_u nbuf[NUMBUFLEN];
9859 int mask;
9860
9861 if (varp->v_type != VAR_UNKNOWN)
9862 {
9863 flags = get_tv_string_buf_chk(varp, nbuf);
9864 if (flags == NULL)
9865 return 0; /* type error; errmsg already given */
9866 while (*flags != NUL)
9867 {
9868 switch (*flags)
9869 {
9870 case 'b': dir = BACKWARD; break;
9871 case 'w': p_ws = TRUE; break;
9872 case 'W': p_ws = FALSE; break;
9873 default: mask = 0;
9874 if (flagsp != NULL)
9875 switch (*flags)
9876 {
9877 case 'c': mask = SP_START; break;
9878 case 'e': mask = SP_END; break;
9879 case 'm': mask = SP_RETCOUNT; break;
9880 case 'n': mask = SP_NOMOVE; break;
9881 case 'p': mask = SP_SUBPAT; break;
9882 case 'r': mask = SP_REPEAT; break;
9883 case 's': mask = SP_SETPCMARK; break;
9884 case 'z': mask = SP_COLUMN; break;
9885 }
9886 if (mask == 0)
9887 {
9888 EMSG2(_(e_invarg2), flags);
9889 dir = 0;
9890 }
9891 else
9892 *flagsp |= mask;
9893 }
9894 if (dir == 0)
9895 break;
9896 ++flags;
9897 }
9898 }
9899 return dir;
9900}
9901
9902/*
9903 * Shared by search() and searchpos() functions.
9904 */
9905 static int
9906search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
9907{
9908 int flags;
9909 char_u *pat;
9910 pos_T pos;
9911 pos_T save_cursor;
9912 int save_p_ws = p_ws;
9913 int dir;
9914 int retval = 0; /* default: FAIL */
9915 long lnum_stop = 0;
9916 proftime_T tm;
9917#ifdef FEAT_RELTIME
9918 long time_limit = 0;
9919#endif
9920 int options = SEARCH_KEEP;
9921 int subpatnum;
9922
9923 pat = get_tv_string(&argvars[0]);
9924 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
9925 if (dir == 0)
9926 goto theend;
9927 flags = *flagsp;
9928 if (flags & SP_START)
9929 options |= SEARCH_START;
9930 if (flags & SP_END)
9931 options |= SEARCH_END;
9932 if (flags & SP_COLUMN)
9933 options |= SEARCH_COL;
9934
9935 /* Optional arguments: line number to stop searching and timeout. */
9936 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9937 {
9938 lnum_stop = (long)get_tv_number_chk(&argvars[2], NULL);
9939 if (lnum_stop < 0)
9940 goto theend;
9941#ifdef FEAT_RELTIME
9942 if (argvars[3].v_type != VAR_UNKNOWN)
9943 {
9944 time_limit = (long)get_tv_number_chk(&argvars[3], NULL);
9945 if (time_limit < 0)
9946 goto theend;
9947 }
9948#endif
9949 }
9950
9951#ifdef FEAT_RELTIME
9952 /* Set the time limit, if there is one. */
9953 profile_setlimit(time_limit, &tm);
9954#endif
9955
9956 /*
9957 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
9958 * Check to make sure only those flags are set.
9959 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
9960 * flags cannot be set. Check for that condition also.
9961 */
9962 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
9963 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9964 {
9965 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
9966 goto theend;
9967 }
9968
9969 pos = save_cursor = curwin->w_cursor;
9970 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009971 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009972 if (subpatnum != FAIL)
9973 {
9974 if (flags & SP_SUBPAT)
9975 retval = subpatnum;
9976 else
9977 retval = pos.lnum;
9978 if (flags & SP_SETPCMARK)
9979 setpcmark();
9980 curwin->w_cursor = pos;
9981 if (match_pos != NULL)
9982 {
9983 /* Store the match cursor position */
9984 match_pos->lnum = pos.lnum;
9985 match_pos->col = pos.col + 1;
9986 }
9987 /* "/$" will put the cursor after the end of the line, may need to
9988 * correct that here */
9989 check_cursor();
9990 }
9991
9992 /* If 'n' flag is used: restore cursor position. */
9993 if (flags & SP_NOMOVE)
9994 curwin->w_cursor = save_cursor;
9995 else
9996 curwin->w_set_curswant = TRUE;
9997theend:
9998 p_ws = save_p_ws;
9999
10000 return retval;
10001}
10002
10003#ifdef FEAT_FLOAT
10004
10005/*
10006 * round() is not in C90, use ceil() or floor() instead.
10007 */
10008 float_T
10009vim_round(float_T f)
10010{
10011 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
10012}
10013
10014/*
10015 * "round({float})" function
10016 */
10017 static void
10018f_round(typval_T *argvars, typval_T *rettv)
10019{
10020 float_T f = 0.0;
10021
10022 rettv->v_type = VAR_FLOAT;
10023 if (get_float_arg(argvars, &f) == OK)
10024 rettv->vval.v_float = vim_round(f);
10025 else
10026 rettv->vval.v_float = 0.0;
10027}
10028#endif
10029
10030/*
10031 * "screenattr()" function
10032 */
10033 static void
10034f_screenattr(typval_T *argvars, typval_T *rettv)
10035{
10036 int row;
10037 int col;
10038 int c;
10039
10040 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
10041 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
10042 if (row < 0 || row >= screen_Rows
10043 || col < 0 || col >= screen_Columns)
10044 c = -1;
10045 else
10046 c = ScreenAttrs[LineOffset[row] + col];
10047 rettv->vval.v_number = c;
10048}
10049
10050/*
10051 * "screenchar()" function
10052 */
10053 static void
10054f_screenchar(typval_T *argvars, typval_T *rettv)
10055{
10056 int row;
10057 int col;
10058 int off;
10059 int c;
10060
10061 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
10062 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
10063 if (row < 0 || row >= screen_Rows
10064 || col < 0 || col >= screen_Columns)
10065 c = -1;
10066 else
10067 {
10068 off = LineOffset[row] + col;
10069#ifdef FEAT_MBYTE
10070 if (enc_utf8 && ScreenLinesUC[off] != 0)
10071 c = ScreenLinesUC[off];
10072 else
10073#endif
10074 c = ScreenLines[off];
10075 }
10076 rettv->vval.v_number = c;
10077}
10078
10079/*
10080 * "screencol()" function
10081 *
10082 * First column is 1 to be consistent with virtcol().
10083 */
10084 static void
10085f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10086{
10087 rettv->vval.v_number = screen_screencol() + 1;
10088}
10089
10090/*
10091 * "screenrow()" function
10092 */
10093 static void
10094f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10095{
10096 rettv->vval.v_number = screen_screenrow() + 1;
10097}
10098
10099/*
10100 * "search()" function
10101 */
10102 static void
10103f_search(typval_T *argvars, typval_T *rettv)
10104{
10105 int flags = 0;
10106
10107 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10108}
10109
10110/*
10111 * "searchdecl()" function
10112 */
10113 static void
10114f_searchdecl(typval_T *argvars, typval_T *rettv)
10115{
10116 int locally = 1;
10117 int thisblock = 0;
10118 int error = FALSE;
10119 char_u *name;
10120
10121 rettv->vval.v_number = 1; /* default: FAIL */
10122
10123 name = get_tv_string_chk(&argvars[0]);
10124 if (argvars[1].v_type != VAR_UNKNOWN)
10125 {
10126 locally = (int)get_tv_number_chk(&argvars[1], &error) == 0;
10127 if (!error && argvars[2].v_type != VAR_UNKNOWN)
10128 thisblock = (int)get_tv_number_chk(&argvars[2], &error) != 0;
10129 }
10130 if (!error && name != NULL)
10131 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10132 locally, thisblock, SEARCH_KEEP) == FAIL;
10133}
10134
10135/*
10136 * Used by searchpair() and searchpairpos()
10137 */
10138 static int
10139searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10140{
10141 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010142 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010143 int save_p_ws = p_ws;
10144 int dir;
10145 int flags = 0;
10146 char_u nbuf1[NUMBUFLEN];
10147 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010148 int retval = 0; /* default: FAIL */
10149 long lnum_stop = 0;
10150 long time_limit = 0;
10151
10152 /* Get the three pattern arguments: start, middle, end. */
10153 spat = get_tv_string_chk(&argvars[0]);
10154 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
10155 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
10156 if (spat == NULL || mpat == NULL || epat == NULL)
10157 goto theend; /* type error */
10158
10159 /* Handle the optional fourth argument: flags */
10160 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
10161 if (dir == 0)
10162 goto theend;
10163
10164 /* Don't accept SP_END or SP_SUBPAT.
10165 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
10166 */
10167 if ((flags & (SP_END | SP_SUBPAT)) != 0
10168 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10169 {
10170 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
10171 goto theend;
10172 }
10173
10174 /* Using 'r' implies 'W', otherwise it doesn't work. */
10175 if (flags & SP_REPEAT)
10176 p_ws = FALSE;
10177
10178 /* Optional fifth argument: skip expression */
10179 if (argvars[3].v_type == VAR_UNKNOWN
10180 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010010181 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010182 else
10183 {
Bram Moolenaar48570482017-10-30 21:48:41 +010010184 skip = &argvars[4];
10185 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
10186 && skip->v_type != VAR_STRING)
10187 {
10188 /* Type error */
10189 goto theend;
10190 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010191 if (argvars[5].v_type != VAR_UNKNOWN)
10192 {
10193 lnum_stop = (long)get_tv_number_chk(&argvars[5], NULL);
10194 if (lnum_stop < 0)
10195 goto theend;
10196#ifdef FEAT_RELTIME
10197 if (argvars[6].v_type != VAR_UNKNOWN)
10198 {
10199 time_limit = (long)get_tv_number_chk(&argvars[6], NULL);
10200 if (time_limit < 0)
10201 goto theend;
10202 }
10203#endif
10204 }
10205 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010206
10207 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
10208 match_pos, lnum_stop, time_limit);
10209
10210theend:
10211 p_ws = save_p_ws;
10212
10213 return retval;
10214}
10215
10216/*
10217 * "searchpair()" function
10218 */
10219 static void
10220f_searchpair(typval_T *argvars, typval_T *rettv)
10221{
10222 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
10223}
10224
10225/*
10226 * "searchpairpos()" function
10227 */
10228 static void
10229f_searchpairpos(typval_T *argvars, typval_T *rettv)
10230{
10231 pos_T match_pos;
10232 int lnum = 0;
10233 int col = 0;
10234
10235 if (rettv_list_alloc(rettv) == FAIL)
10236 return;
10237
10238 if (searchpair_cmn(argvars, &match_pos) > 0)
10239 {
10240 lnum = match_pos.lnum;
10241 col = match_pos.col;
10242 }
10243
10244 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10245 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10246}
10247
10248/*
10249 * Search for a start/middle/end thing.
10250 * Used by searchpair(), see its documentation for the details.
10251 * Returns 0 or -1 for no match,
10252 */
10253 long
10254do_searchpair(
10255 char_u *spat, /* start pattern */
10256 char_u *mpat, /* middle pattern */
10257 char_u *epat, /* end pattern */
10258 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010010259 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010260 int flags, /* SP_SETPCMARK and other SP_ values */
10261 pos_T *match_pos,
10262 linenr_T lnum_stop, /* stop at this line if not zero */
10263 long time_limit UNUSED) /* stop after this many msec */
10264{
10265 char_u *save_cpo;
10266 char_u *pat, *pat2 = NULL, *pat3 = NULL;
10267 long retval = 0;
10268 pos_T pos;
10269 pos_T firstpos;
10270 pos_T foundpos;
10271 pos_T save_cursor;
10272 pos_T save_pos;
10273 int n;
10274 int r;
10275 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010010276 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010277 int err;
10278 int options = SEARCH_KEEP;
10279 proftime_T tm;
10280
10281 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10282 save_cpo = p_cpo;
10283 p_cpo = empty_option;
10284
10285#ifdef FEAT_RELTIME
10286 /* Set the time limit, if there is one. */
10287 profile_setlimit(time_limit, &tm);
10288#endif
10289
10290 /* Make two search patterns: start/end (pat2, for in nested pairs) and
10291 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010292 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
10293 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010294 if (pat2 == NULL || pat3 == NULL)
10295 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010296 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010297 if (*mpat == NUL)
10298 STRCPY(pat3, pat2);
10299 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010300 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010301 spat, epat, mpat);
10302 if (flags & SP_START)
10303 options |= SEARCH_START;
10304
Bram Moolenaar48570482017-10-30 21:48:41 +010010305 if (skip != NULL)
10306 {
10307 /* Empty string means to not use the skip expression. */
10308 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
10309 use_skip = skip->vval.v_string != NULL
10310 && *skip->vval.v_string != NUL;
10311 }
10312
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010313 save_cursor = curwin->w_cursor;
10314 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010315 CLEAR_POS(&firstpos);
10316 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010317 pat = pat3;
10318 for (;;)
10319 {
10320 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010321 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010322 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010323 /* didn't find it or found the first match again: FAIL */
10324 break;
10325
10326 if (firstpos.lnum == 0)
10327 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010328 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010329 {
10330 /* Found the same position again. Can happen with a pattern that
10331 * has "\zs" at the end and searching backwards. Advance one
10332 * character and try again. */
10333 if (dir == BACKWARD)
10334 decl(&pos);
10335 else
10336 incl(&pos);
10337 }
10338 foundpos = pos;
10339
10340 /* clear the start flag to avoid getting stuck here */
10341 options &= ~SEARCH_START;
10342
10343 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010010344 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010345 {
10346 save_pos = curwin->w_cursor;
10347 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010010348 err = FALSE;
10349 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010350 curwin->w_cursor = save_pos;
10351 if (err)
10352 {
10353 /* Evaluating {skip} caused an error, break here. */
10354 curwin->w_cursor = save_cursor;
10355 retval = -1;
10356 break;
10357 }
10358 if (r)
10359 continue;
10360 }
10361
10362 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
10363 {
10364 /* Found end when searching backwards or start when searching
10365 * forward: nested pair. */
10366 ++nest;
10367 pat = pat2; /* nested, don't search for middle */
10368 }
10369 else
10370 {
10371 /* Found end when searching forward or start when searching
10372 * backward: end of (nested) pair; or found middle in outer pair. */
10373 if (--nest == 1)
10374 pat = pat3; /* outer level, search for middle */
10375 }
10376
10377 if (nest == 0)
10378 {
10379 /* Found the match: return matchcount or line number. */
10380 if (flags & SP_RETCOUNT)
10381 ++retval;
10382 else
10383 retval = pos.lnum;
10384 if (flags & SP_SETPCMARK)
10385 setpcmark();
10386 curwin->w_cursor = pos;
10387 if (!(flags & SP_REPEAT))
10388 break;
10389 nest = 1; /* search for next unmatched */
10390 }
10391 }
10392
10393 if (match_pos != NULL)
10394 {
10395 /* Store the match cursor position */
10396 match_pos->lnum = curwin->w_cursor.lnum;
10397 match_pos->col = curwin->w_cursor.col + 1;
10398 }
10399
10400 /* If 'n' flag is used or search failed: restore cursor position. */
10401 if ((flags & SP_NOMOVE) || retval == 0)
10402 curwin->w_cursor = save_cursor;
10403
10404theend:
10405 vim_free(pat2);
10406 vim_free(pat3);
10407 if (p_cpo == empty_option)
10408 p_cpo = save_cpo;
10409 else
10410 /* Darn, evaluating the {skip} expression changed the value. */
10411 free_string_option(save_cpo);
10412
10413 return retval;
10414}
10415
10416/*
10417 * "searchpos()" function
10418 */
10419 static void
10420f_searchpos(typval_T *argvars, typval_T *rettv)
10421{
10422 pos_T match_pos;
10423 int lnum = 0;
10424 int col = 0;
10425 int n;
10426 int flags = 0;
10427
10428 if (rettv_list_alloc(rettv) == FAIL)
10429 return;
10430
10431 n = search_cmn(argvars, &match_pos, &flags);
10432 if (n > 0)
10433 {
10434 lnum = match_pos.lnum;
10435 col = match_pos.col;
10436 }
10437
10438 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10439 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10440 if (flags & SP_SUBPAT)
10441 list_append_number(rettv->vval.v_list, (varnumber_T)n);
10442}
10443
10444 static void
10445f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
10446{
10447#ifdef FEAT_CLIENTSERVER
10448 char_u buf[NUMBUFLEN];
10449 char_u *server = get_tv_string_chk(&argvars[0]);
10450 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
10451
10452 rettv->vval.v_number = -1;
10453 if (server == NULL || reply == NULL)
10454 return;
10455 if (check_restricted() || check_secure())
10456 return;
10457# ifdef FEAT_X11
10458 if (check_connection() == FAIL)
10459 return;
10460# endif
10461
10462 if (serverSendReply(server, reply) < 0)
10463 {
10464 EMSG(_("E258: Unable to send to client"));
10465 return;
10466 }
10467 rettv->vval.v_number = 0;
10468#else
10469 rettv->vval.v_number = -1;
10470#endif
10471}
10472
10473 static void
10474f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
10475{
10476 char_u *r = NULL;
10477
10478#ifdef FEAT_CLIENTSERVER
10479# ifdef WIN32
10480 r = serverGetVimNames();
10481# else
10482 make_connection();
10483 if (X_DISPLAY != NULL)
10484 r = serverGetVimNames(X_DISPLAY);
10485# endif
10486#endif
10487 rettv->v_type = VAR_STRING;
10488 rettv->vval.v_string = r;
10489}
10490
10491/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010492 * "setbufline()" function
10493 */
10494 static void
10495f_setbufline(argvars, rettv)
10496 typval_T *argvars;
10497 typval_T *rettv;
10498{
10499 linenr_T lnum;
10500 buf_T *buf;
10501
10502 buf = get_buf_tv(&argvars[0], FALSE);
10503 if (buf == NULL)
10504 rettv->vval.v_number = 1; /* FAIL */
10505 else
10506 {
10507 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020010508 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010509 }
10510}
10511
10512/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010513 * "setbufvar()" function
10514 */
10515 static void
10516f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10517{
10518 buf_T *buf;
10519 char_u *varname, *bufvarname;
10520 typval_T *varp;
10521 char_u nbuf[NUMBUFLEN];
10522
10523 if (check_restricted() || check_secure())
10524 return;
10525 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10526 varname = get_tv_string_chk(&argvars[1]);
10527 buf = get_buf_tv(&argvars[0], FALSE);
10528 varp = &argvars[2];
10529
10530 if (buf != NULL && varname != NULL && varp != NULL)
10531 {
10532 if (*varname == '&')
10533 {
10534 long numval;
10535 char_u *strval;
10536 int error = FALSE;
10537 aco_save_T aco;
10538
10539 /* set curbuf to be our buf, temporarily */
10540 aucmd_prepbuf(&aco, buf);
10541
10542 ++varname;
10543 numval = (long)get_tv_number_chk(varp, &error);
10544 strval = get_tv_string_buf_chk(varp, nbuf);
10545 if (!error && strval != NULL)
10546 set_option_value(varname, numval, strval, OPT_LOCAL);
10547
10548 /* reset notion of buffer */
10549 aucmd_restbuf(&aco);
10550 }
10551 else
10552 {
10553 buf_T *save_curbuf = curbuf;
10554
10555 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
10556 if (bufvarname != NULL)
10557 {
10558 curbuf = buf;
10559 STRCPY(bufvarname, "b:");
10560 STRCPY(bufvarname + 2, varname);
10561 set_var(bufvarname, varp, TRUE);
10562 vim_free(bufvarname);
10563 curbuf = save_curbuf;
10564 }
10565 }
10566 }
10567}
10568
10569 static void
10570f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10571{
10572 dict_T *d;
10573 dictitem_T *di;
10574 char_u *csearch;
10575
10576 if (argvars[0].v_type != VAR_DICT)
10577 {
10578 EMSG(_(e_dictreq));
10579 return;
10580 }
10581
10582 if ((d = argvars[0].vval.v_dict) != NULL)
10583 {
10584 csearch = get_dict_string(d, (char_u *)"char", FALSE);
10585 if (csearch != NULL)
10586 {
10587#ifdef FEAT_MBYTE
10588 if (enc_utf8)
10589 {
10590 int pcc[MAX_MCO];
10591 int c = utfc_ptr2char(csearch, pcc);
10592
10593 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
10594 }
10595 else
10596#endif
10597 set_last_csearch(PTR2CHAR(csearch),
10598 csearch, MB_PTR2LEN(csearch));
10599 }
10600
10601 di = dict_find(d, (char_u *)"forward", -1);
10602 if (di != NULL)
10603 set_csearch_direction((int)get_tv_number(&di->di_tv)
10604 ? FORWARD : BACKWARD);
10605
10606 di = dict_find(d, (char_u *)"until", -1);
10607 if (di != NULL)
10608 set_csearch_until(!!get_tv_number(&di->di_tv));
10609 }
10610}
10611
10612/*
10613 * "setcmdpos()" function
10614 */
10615 static void
10616f_setcmdpos(typval_T *argvars, typval_T *rettv)
10617{
10618 int pos = (int)get_tv_number(&argvars[0]) - 1;
10619
10620 if (pos >= 0)
10621 rettv->vval.v_number = set_cmdline_pos(pos);
10622}
10623
10624/*
10625 * "setfperm({fname}, {mode})" function
10626 */
10627 static void
10628f_setfperm(typval_T *argvars, typval_T *rettv)
10629{
10630 char_u *fname;
10631 char_u modebuf[NUMBUFLEN];
10632 char_u *mode_str;
10633 int i;
10634 int mask;
10635 int mode = 0;
10636
10637 rettv->vval.v_number = 0;
10638 fname = get_tv_string_chk(&argvars[0]);
10639 if (fname == NULL)
10640 return;
10641 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
10642 if (mode_str == NULL)
10643 return;
10644 if (STRLEN(mode_str) != 9)
10645 {
10646 EMSG2(_(e_invarg2), mode_str);
10647 return;
10648 }
10649
10650 mask = 1;
10651 for (i = 8; i >= 0; --i)
10652 {
10653 if (mode_str[i] != '-')
10654 mode |= mask;
10655 mask = mask << 1;
10656 }
10657 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
10658}
10659
10660/*
10661 * "setline()" function
10662 */
10663 static void
10664f_setline(typval_T *argvars, typval_T *rettv)
10665{
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010666 linenr_T lnum = get_tv_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010667
Bram Moolenaarca851592018-06-06 21:04:07 +020010668 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010669}
10670
Bram Moolenaard823fa92016-08-12 16:29:27 +020010671static void set_qf_ll_list(win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *what_arg, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010672
10673/*
10674 * Used by "setqflist()" and "setloclist()" functions
10675 */
10676 static void
10677set_qf_ll_list(
10678 win_T *wp UNUSED,
10679 typval_T *list_arg UNUSED,
10680 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020010681 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010682 typval_T *rettv)
10683{
10684#ifdef FEAT_QUICKFIX
10685 static char *e_invact = N_("E927: Invalid action: '%s'");
10686 char_u *act;
10687 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010688 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010689#endif
10690
10691 rettv->vval.v_number = -1;
10692
10693#ifdef FEAT_QUICKFIX
10694 if (list_arg->v_type != VAR_LIST)
10695 EMSG(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010696 else if (recursive != 0)
10697 EMSG(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010698 else
10699 {
10700 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010701 dict_T *d = NULL;
10702 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010703
10704 if (action_arg->v_type == VAR_STRING)
10705 {
10706 act = get_tv_string_chk(action_arg);
10707 if (act == NULL)
10708 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020010709 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
10710 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010711 action = *act;
10712 else
10713 EMSG2(_(e_invact), act);
10714 }
10715 else if (action_arg->v_type == VAR_UNKNOWN)
10716 action = ' ';
10717 else
10718 EMSG(_(e_stringreq));
10719
Bram Moolenaard823fa92016-08-12 16:29:27 +020010720 if (action_arg->v_type != VAR_UNKNOWN
10721 && what_arg->v_type != VAR_UNKNOWN)
10722 {
10723 if (what_arg->v_type == VAR_DICT)
10724 d = what_arg->vval.v_dict;
10725 else
10726 {
10727 EMSG(_(e_dictreq));
10728 valid_dict = FALSE;
10729 }
10730 }
10731
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010732 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010733 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010734 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
10735 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010736 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010737 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010738 }
10739#endif
10740}
10741
10742/*
10743 * "setloclist()" function
10744 */
10745 static void
10746f_setloclist(typval_T *argvars, typval_T *rettv)
10747{
10748 win_T *win;
10749
10750 rettv->vval.v_number = -1;
10751
10752 win = find_win_by_nr(&argvars[0], NULL);
10753 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020010754 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010755}
10756
10757/*
10758 * "setmatches()" function
10759 */
10760 static void
10761f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10762{
10763#ifdef FEAT_SEARCH_EXTRA
10764 list_T *l;
10765 listitem_T *li;
10766 dict_T *d;
10767 list_T *s = NULL;
10768
10769 rettv->vval.v_number = -1;
10770 if (argvars[0].v_type != VAR_LIST)
10771 {
10772 EMSG(_(e_listreq));
10773 return;
10774 }
10775 if ((l = argvars[0].vval.v_list) != NULL)
10776 {
10777
10778 /* To some extent make sure that we are dealing with a list from
10779 * "getmatches()". */
10780 li = l->lv_first;
10781 while (li != NULL)
10782 {
10783 if (li->li_tv.v_type != VAR_DICT
10784 || (d = li->li_tv.vval.v_dict) == NULL)
10785 {
10786 EMSG(_(e_invarg));
10787 return;
10788 }
10789 if (!(dict_find(d, (char_u *)"group", -1) != NULL
10790 && (dict_find(d, (char_u *)"pattern", -1) != NULL
10791 || dict_find(d, (char_u *)"pos1", -1) != NULL)
10792 && dict_find(d, (char_u *)"priority", -1) != NULL
10793 && dict_find(d, (char_u *)"id", -1) != NULL))
10794 {
10795 EMSG(_(e_invarg));
10796 return;
10797 }
10798 li = li->li_next;
10799 }
10800
10801 clear_matches(curwin);
10802 li = l->lv_first;
10803 while (li != NULL)
10804 {
10805 int i = 0;
10806 char_u buf[5];
10807 dictitem_T *di;
10808 char_u *group;
10809 int priority;
10810 int id;
10811 char_u *conceal;
10812
10813 d = li->li_tv.vval.v_dict;
10814 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
10815 {
10816 if (s == NULL)
10817 {
10818 s = list_alloc();
10819 if (s == NULL)
10820 return;
10821 }
10822
10823 /* match from matchaddpos() */
10824 for (i = 1; i < 9; i++)
10825 {
10826 sprintf((char *)buf, (char *)"pos%d", i);
10827 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
10828 {
10829 if (di->di_tv.v_type != VAR_LIST)
10830 return;
10831
10832 list_append_tv(s, &di->di_tv);
10833 s->lv_refcount++;
10834 }
10835 else
10836 break;
10837 }
10838 }
10839
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010840 group = get_dict_string(d, (char_u *)"group", TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010841 priority = (int)get_dict_number(d, (char_u *)"priority");
10842 id = (int)get_dict_number(d, (char_u *)"id");
10843 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010844 ? get_dict_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010845 : NULL;
10846 if (i == 0)
10847 {
10848 match_add(curwin, group,
10849 get_dict_string(d, (char_u *)"pattern", FALSE),
10850 priority, id, NULL, conceal);
10851 }
10852 else
10853 {
10854 match_add(curwin, group, NULL, priority, id, s, conceal);
10855 list_unref(s);
10856 s = NULL;
10857 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010858 vim_free(group);
10859 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010860
10861 li = li->li_next;
10862 }
10863 rettv->vval.v_number = 0;
10864 }
10865#endif
10866}
10867
10868/*
10869 * "setpos()" function
10870 */
10871 static void
10872f_setpos(typval_T *argvars, typval_T *rettv)
10873{
10874 pos_T pos;
10875 int fnum;
10876 char_u *name;
10877 colnr_T curswant = -1;
10878
10879 rettv->vval.v_number = -1;
10880 name = get_tv_string_chk(argvars);
10881 if (name != NULL)
10882 {
10883 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
10884 {
10885 if (--pos.col < 0)
10886 pos.col = 0;
10887 if (name[0] == '.' && name[1] == NUL)
10888 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010889 /* set cursor; "fnum" is ignored */
10890 curwin->w_cursor = pos;
10891 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010892 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010893 curwin->w_curswant = curswant - 1;
10894 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010895 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010896 check_cursor();
10897 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010898 }
10899 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
10900 {
10901 /* set mark */
10902 if (setmark_pos(name[1], &pos, fnum) == OK)
10903 rettv->vval.v_number = 0;
10904 }
10905 else
10906 EMSG(_(e_invarg));
10907 }
10908 }
10909}
10910
10911/*
10912 * "setqflist()" function
10913 */
10914 static void
10915f_setqflist(typval_T *argvars, typval_T *rettv)
10916{
Bram Moolenaard823fa92016-08-12 16:29:27 +020010917 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010918}
10919
10920/*
10921 * "setreg()" function
10922 */
10923 static void
10924f_setreg(typval_T *argvars, typval_T *rettv)
10925{
10926 int regname;
10927 char_u *strregname;
10928 char_u *stropt;
10929 char_u *strval;
10930 int append;
10931 char_u yank_type;
10932 long block_len;
10933
10934 block_len = -1;
10935 yank_type = MAUTO;
10936 append = FALSE;
10937
10938 strregname = get_tv_string_chk(argvars);
10939 rettv->vval.v_number = 1; /* FAIL is default */
10940
10941 if (strregname == NULL)
10942 return; /* type error; errmsg already given */
10943 regname = *strregname;
10944 if (regname == 0 || regname == '@')
10945 regname = '"';
10946
10947 if (argvars[2].v_type != VAR_UNKNOWN)
10948 {
10949 stropt = get_tv_string_chk(&argvars[2]);
10950 if (stropt == NULL)
10951 return; /* type error */
10952 for (; *stropt != NUL; ++stropt)
10953 switch (*stropt)
10954 {
10955 case 'a': case 'A': /* append */
10956 append = TRUE;
10957 break;
10958 case 'v': case 'c': /* character-wise selection */
10959 yank_type = MCHAR;
10960 break;
10961 case 'V': case 'l': /* line-wise selection */
10962 yank_type = MLINE;
10963 break;
10964 case 'b': case Ctrl_V: /* block-wise selection */
10965 yank_type = MBLOCK;
10966 if (VIM_ISDIGIT(stropt[1]))
10967 {
10968 ++stropt;
10969 block_len = getdigits(&stropt) - 1;
10970 --stropt;
10971 }
10972 break;
10973 }
10974 }
10975
10976 if (argvars[1].v_type == VAR_LIST)
10977 {
10978 char_u **lstval;
10979 char_u **allocval;
10980 char_u buf[NUMBUFLEN];
10981 char_u **curval;
10982 char_u **curallocval;
10983 list_T *ll = argvars[1].vval.v_list;
10984 listitem_T *li;
10985 int len;
10986
10987 /* If the list is NULL handle like an empty list. */
10988 len = ll == NULL ? 0 : ll->lv_len;
10989
10990 /* First half: use for pointers to result lines; second half: use for
10991 * pointers to allocated copies. */
10992 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
10993 if (lstval == NULL)
10994 return;
10995 curval = lstval;
10996 allocval = lstval + len + 2;
10997 curallocval = allocval;
10998
10999 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
11000 li = li->li_next)
11001 {
11002 strval = get_tv_string_buf_chk(&li->li_tv, buf);
11003 if (strval == NULL)
11004 goto free_lstval;
11005 if (strval == buf)
11006 {
11007 /* Need to make a copy, next get_tv_string_buf_chk() will
11008 * overwrite the string. */
11009 strval = vim_strsave(buf);
11010 if (strval == NULL)
11011 goto free_lstval;
11012 *curallocval++ = strval;
11013 }
11014 *curval++ = strval;
11015 }
11016 *curval++ = NULL;
11017
11018 write_reg_contents_lst(regname, lstval, -1,
11019 append, yank_type, block_len);
11020free_lstval:
11021 while (curallocval > allocval)
11022 vim_free(*--curallocval);
11023 vim_free(lstval);
11024 }
11025 else
11026 {
11027 strval = get_tv_string_chk(&argvars[1]);
11028 if (strval == NULL)
11029 return;
11030 write_reg_contents_ex(regname, strval, -1,
11031 append, yank_type, block_len);
11032 }
11033 rettv->vval.v_number = 0;
11034}
11035
11036/*
11037 * "settabvar()" function
11038 */
11039 static void
11040f_settabvar(typval_T *argvars, typval_T *rettv)
11041{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011042 tabpage_T *save_curtab;
11043 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011044 char_u *varname, *tabvarname;
11045 typval_T *varp;
11046
11047 rettv->vval.v_number = 0;
11048
11049 if (check_restricted() || check_secure())
11050 return;
11051
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011052 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011053 varname = get_tv_string_chk(&argvars[1]);
11054 varp = &argvars[2];
11055
Bram Moolenaar4033c552017-09-16 20:54:51 +020011056 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011057 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011058 save_curtab = curtab;
11059 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011060
11061 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
11062 if (tabvarname != NULL)
11063 {
11064 STRCPY(tabvarname, "t:");
11065 STRCPY(tabvarname + 2, varname);
11066 set_var(tabvarname, varp, TRUE);
11067 vim_free(tabvarname);
11068 }
11069
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011070 /* Restore current tabpage */
11071 if (valid_tabpage(save_curtab))
11072 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011073 }
11074}
11075
11076/*
11077 * "settabwinvar()" function
11078 */
11079 static void
11080f_settabwinvar(typval_T *argvars, typval_T *rettv)
11081{
11082 setwinvar(argvars, rettv, 1);
11083}
11084
11085/*
11086 * "setwinvar()" function
11087 */
11088 static void
11089f_setwinvar(typval_T *argvars, typval_T *rettv)
11090{
11091 setwinvar(argvars, rettv, 0);
11092}
11093
11094#ifdef FEAT_CRYPT
11095/*
11096 * "sha256({string})" function
11097 */
11098 static void
11099f_sha256(typval_T *argvars, typval_T *rettv)
11100{
11101 char_u *p;
11102
11103 p = get_tv_string(&argvars[0]);
11104 rettv->vval.v_string = vim_strsave(
11105 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
11106 rettv->v_type = VAR_STRING;
11107}
11108#endif /* FEAT_CRYPT */
11109
11110/*
11111 * "shellescape({string})" function
11112 */
11113 static void
11114f_shellescape(typval_T *argvars, typval_T *rettv)
11115{
Bram Moolenaar20615522017-06-05 18:46:26 +020011116 int do_special = non_zero_arg(&argvars[1]);
11117
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011118 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar20615522017-06-05 18:46:26 +020011119 get_tv_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011120 rettv->v_type = VAR_STRING;
11121}
11122
11123/*
11124 * shiftwidth() function
11125 */
11126 static void
11127f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
11128{
11129 rettv->vval.v_number = get_sw_value(curbuf);
11130}
11131
11132/*
11133 * "simplify()" function
11134 */
11135 static void
11136f_simplify(typval_T *argvars, typval_T *rettv)
11137{
11138 char_u *p;
11139
11140 p = get_tv_string(&argvars[0]);
11141 rettv->vval.v_string = vim_strsave(p);
11142 simplify_filename(rettv->vval.v_string); /* simplify in place */
11143 rettv->v_type = VAR_STRING;
11144}
11145
11146#ifdef FEAT_FLOAT
11147/*
11148 * "sin()" function
11149 */
11150 static void
11151f_sin(typval_T *argvars, typval_T *rettv)
11152{
11153 float_T f = 0.0;
11154
11155 rettv->v_type = VAR_FLOAT;
11156 if (get_float_arg(argvars, &f) == OK)
11157 rettv->vval.v_float = sin(f);
11158 else
11159 rettv->vval.v_float = 0.0;
11160}
11161
11162/*
11163 * "sinh()" function
11164 */
11165 static void
11166f_sinh(typval_T *argvars, typval_T *rettv)
11167{
11168 float_T f = 0.0;
11169
11170 rettv->v_type = VAR_FLOAT;
11171 if (get_float_arg(argvars, &f) == OK)
11172 rettv->vval.v_float = sinh(f);
11173 else
11174 rettv->vval.v_float = 0.0;
11175}
11176#endif
11177
11178static int
11179#ifdef __BORLANDC__
11180 _RTLENTRYF
11181#endif
11182 item_compare(const void *s1, const void *s2);
11183static int
11184#ifdef __BORLANDC__
11185 _RTLENTRYF
11186#endif
11187 item_compare2(const void *s1, const void *s2);
11188
11189/* struct used in the array that's given to qsort() */
11190typedef struct
11191{
11192 listitem_T *item;
11193 int idx;
11194} sortItem_T;
11195
11196/* struct storing information about current sort */
11197typedef struct
11198{
11199 int item_compare_ic;
11200 int item_compare_numeric;
11201 int item_compare_numbers;
11202#ifdef FEAT_FLOAT
11203 int item_compare_float;
11204#endif
11205 char_u *item_compare_func;
11206 partial_T *item_compare_partial;
11207 dict_T *item_compare_selfdict;
11208 int item_compare_func_err;
11209 int item_compare_keep_zero;
11210} sortinfo_T;
11211static sortinfo_T *sortinfo = NULL;
11212static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
11213#define ITEM_COMPARE_FAIL 999
11214
11215/*
11216 * Compare functions for f_sort() and f_uniq() below.
11217 */
11218 static int
11219#ifdef __BORLANDC__
11220_RTLENTRYF
11221#endif
11222item_compare(const void *s1, const void *s2)
11223{
11224 sortItem_T *si1, *si2;
11225 typval_T *tv1, *tv2;
11226 char_u *p1, *p2;
11227 char_u *tofree1 = NULL, *tofree2 = NULL;
11228 int res;
11229 char_u numbuf1[NUMBUFLEN];
11230 char_u numbuf2[NUMBUFLEN];
11231
11232 si1 = (sortItem_T *)s1;
11233 si2 = (sortItem_T *)s2;
11234 tv1 = &si1->item->li_tv;
11235 tv2 = &si2->item->li_tv;
11236
11237 if (sortinfo->item_compare_numbers)
11238 {
11239 varnumber_T v1 = get_tv_number(tv1);
11240 varnumber_T v2 = get_tv_number(tv2);
11241
11242 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11243 }
11244
11245#ifdef FEAT_FLOAT
11246 if (sortinfo->item_compare_float)
11247 {
11248 float_T v1 = get_tv_float(tv1);
11249 float_T v2 = get_tv_float(tv2);
11250
11251 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11252 }
11253#endif
11254
11255 /* tv2string() puts quotes around a string and allocates memory. Don't do
11256 * that for string variables. Use a single quote when comparing with a
11257 * non-string to do what the docs promise. */
11258 if (tv1->v_type == VAR_STRING)
11259 {
11260 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11261 p1 = (char_u *)"'";
11262 else
11263 p1 = tv1->vval.v_string;
11264 }
11265 else
11266 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
11267 if (tv2->v_type == VAR_STRING)
11268 {
11269 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11270 p2 = (char_u *)"'";
11271 else
11272 p2 = tv2->vval.v_string;
11273 }
11274 else
11275 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
11276 if (p1 == NULL)
11277 p1 = (char_u *)"";
11278 if (p2 == NULL)
11279 p2 = (char_u *)"";
11280 if (!sortinfo->item_compare_numeric)
11281 {
11282 if (sortinfo->item_compare_ic)
11283 res = STRICMP(p1, p2);
11284 else
11285 res = STRCMP(p1, p2);
11286 }
11287 else
11288 {
11289 double n1, n2;
11290 n1 = strtod((char *)p1, (char **)&p1);
11291 n2 = strtod((char *)p2, (char **)&p2);
11292 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
11293 }
11294
11295 /* When the result would be zero, compare the item indexes. Makes the
11296 * sort stable. */
11297 if (res == 0 && !sortinfo->item_compare_keep_zero)
11298 res = si1->idx > si2->idx ? 1 : -1;
11299
11300 vim_free(tofree1);
11301 vim_free(tofree2);
11302 return res;
11303}
11304
11305 static int
11306#ifdef __BORLANDC__
11307_RTLENTRYF
11308#endif
11309item_compare2(const void *s1, const void *s2)
11310{
11311 sortItem_T *si1, *si2;
11312 int res;
11313 typval_T rettv;
11314 typval_T argv[3];
11315 int dummy;
11316 char_u *func_name;
11317 partial_T *partial = sortinfo->item_compare_partial;
11318
11319 /* shortcut after failure in previous call; compare all items equal */
11320 if (sortinfo->item_compare_func_err)
11321 return 0;
11322
11323 si1 = (sortItem_T *)s1;
11324 si2 = (sortItem_T *)s2;
11325
11326 if (partial == NULL)
11327 func_name = sortinfo->item_compare_func;
11328 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020011329 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011330
11331 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
11332 * in the copy without changing the original list items. */
11333 copy_tv(&si1->item->li_tv, &argv[0]);
11334 copy_tv(&si2->item->li_tv, &argv[1]);
11335
11336 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
11337 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020011338 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011339 partial, sortinfo->item_compare_selfdict);
11340 clear_tv(&argv[0]);
11341 clear_tv(&argv[1]);
11342
11343 if (res == FAIL)
11344 res = ITEM_COMPARE_FAIL;
11345 else
11346 res = (int)get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
11347 if (sortinfo->item_compare_func_err)
11348 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
11349 clear_tv(&rettv);
11350
11351 /* When the result would be zero, compare the pointers themselves. Makes
11352 * the sort stable. */
11353 if (res == 0 && !sortinfo->item_compare_keep_zero)
11354 res = si1->idx > si2->idx ? 1 : -1;
11355
11356 return res;
11357}
11358
11359/*
11360 * "sort({list})" function
11361 */
11362 static void
11363do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
11364{
11365 list_T *l;
11366 listitem_T *li;
11367 sortItem_T *ptrs;
11368 sortinfo_T *old_sortinfo;
11369 sortinfo_T info;
11370 long len;
11371 long i;
11372
11373 /* Pointer to current info struct used in compare function. Save and
11374 * restore the current one for nested calls. */
11375 old_sortinfo = sortinfo;
11376 sortinfo = &info;
11377
11378 if (argvars[0].v_type != VAR_LIST)
11379 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
11380 else
11381 {
11382 l = argvars[0].vval.v_list;
11383 if (l == NULL || tv_check_lock(l->lv_lock,
11384 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
11385 TRUE))
11386 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011387 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011388
11389 len = list_len(l);
11390 if (len <= 1)
11391 goto theend; /* short list sorts pretty quickly */
11392
11393 info.item_compare_ic = FALSE;
11394 info.item_compare_numeric = FALSE;
11395 info.item_compare_numbers = FALSE;
11396#ifdef FEAT_FLOAT
11397 info.item_compare_float = FALSE;
11398#endif
11399 info.item_compare_func = NULL;
11400 info.item_compare_partial = NULL;
11401 info.item_compare_selfdict = NULL;
11402 if (argvars[1].v_type != VAR_UNKNOWN)
11403 {
11404 /* optional second argument: {func} */
11405 if (argvars[1].v_type == VAR_FUNC)
11406 info.item_compare_func = argvars[1].vval.v_string;
11407 else if (argvars[1].v_type == VAR_PARTIAL)
11408 info.item_compare_partial = argvars[1].vval.v_partial;
11409 else
11410 {
11411 int error = FALSE;
11412
11413 i = (long)get_tv_number_chk(&argvars[1], &error);
11414 if (error)
11415 goto theend; /* type error; errmsg already given */
11416 if (i == 1)
11417 info.item_compare_ic = TRUE;
11418 else if (argvars[1].v_type != VAR_NUMBER)
11419 info.item_compare_func = get_tv_string(&argvars[1]);
11420 else if (i != 0)
11421 {
11422 EMSG(_(e_invarg));
11423 goto theend;
11424 }
11425 if (info.item_compare_func != NULL)
11426 {
11427 if (*info.item_compare_func == NUL)
11428 {
11429 /* empty string means default sort */
11430 info.item_compare_func = NULL;
11431 }
11432 else if (STRCMP(info.item_compare_func, "n") == 0)
11433 {
11434 info.item_compare_func = NULL;
11435 info.item_compare_numeric = TRUE;
11436 }
11437 else if (STRCMP(info.item_compare_func, "N") == 0)
11438 {
11439 info.item_compare_func = NULL;
11440 info.item_compare_numbers = TRUE;
11441 }
11442#ifdef FEAT_FLOAT
11443 else if (STRCMP(info.item_compare_func, "f") == 0)
11444 {
11445 info.item_compare_func = NULL;
11446 info.item_compare_float = TRUE;
11447 }
11448#endif
11449 else if (STRCMP(info.item_compare_func, "i") == 0)
11450 {
11451 info.item_compare_func = NULL;
11452 info.item_compare_ic = TRUE;
11453 }
11454 }
11455 }
11456
11457 if (argvars[2].v_type != VAR_UNKNOWN)
11458 {
11459 /* optional third argument: {dict} */
11460 if (argvars[2].v_type != VAR_DICT)
11461 {
11462 EMSG(_(e_dictreq));
11463 goto theend;
11464 }
11465 info.item_compare_selfdict = argvars[2].vval.v_dict;
11466 }
11467 }
11468
11469 /* Make an array with each entry pointing to an item in the List. */
11470 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
11471 if (ptrs == NULL)
11472 goto theend;
11473
11474 i = 0;
11475 if (sort)
11476 {
11477 /* sort(): ptrs will be the list to sort */
11478 for (li = l->lv_first; li != NULL; li = li->li_next)
11479 {
11480 ptrs[i].item = li;
11481 ptrs[i].idx = i;
11482 ++i;
11483 }
11484
11485 info.item_compare_func_err = FALSE;
11486 info.item_compare_keep_zero = FALSE;
11487 /* test the compare function */
11488 if ((info.item_compare_func != NULL
11489 || info.item_compare_partial != NULL)
11490 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
11491 == ITEM_COMPARE_FAIL)
11492 EMSG(_("E702: Sort compare function failed"));
11493 else
11494 {
11495 /* Sort the array with item pointers. */
11496 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
11497 info.item_compare_func == NULL
11498 && info.item_compare_partial == NULL
11499 ? item_compare : item_compare2);
11500
11501 if (!info.item_compare_func_err)
11502 {
11503 /* Clear the List and append the items in sorted order. */
11504 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
11505 l->lv_len = 0;
11506 for (i = 0; i < len; ++i)
11507 list_append(l, ptrs[i].item);
11508 }
11509 }
11510 }
11511 else
11512 {
11513 int (*item_compare_func_ptr)(const void *, const void *);
11514
11515 /* f_uniq(): ptrs will be a stack of items to remove */
11516 info.item_compare_func_err = FALSE;
11517 info.item_compare_keep_zero = TRUE;
11518 item_compare_func_ptr = info.item_compare_func != NULL
11519 || info.item_compare_partial != NULL
11520 ? item_compare2 : item_compare;
11521
11522 for (li = l->lv_first; li != NULL && li->li_next != NULL;
11523 li = li->li_next)
11524 {
11525 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
11526 == 0)
11527 ptrs[i++].item = li;
11528 if (info.item_compare_func_err)
11529 {
11530 EMSG(_("E882: Uniq compare function failed"));
11531 break;
11532 }
11533 }
11534
11535 if (!info.item_compare_func_err)
11536 {
11537 while (--i >= 0)
11538 {
11539 li = ptrs[i].item->li_next;
11540 ptrs[i].item->li_next = li->li_next;
11541 if (li->li_next != NULL)
11542 li->li_next->li_prev = ptrs[i].item;
11543 else
11544 l->lv_last = ptrs[i].item;
11545 list_fix_watch(l, li);
11546 listitem_free(li);
11547 l->lv_len--;
11548 }
11549 }
11550 }
11551
11552 vim_free(ptrs);
11553 }
11554theend:
11555 sortinfo = old_sortinfo;
11556}
11557
11558/*
11559 * "sort({list})" function
11560 */
11561 static void
11562f_sort(typval_T *argvars, typval_T *rettv)
11563{
11564 do_sort_uniq(argvars, rettv, TRUE);
11565}
11566
11567/*
11568 * "uniq({list})" function
11569 */
11570 static void
11571f_uniq(typval_T *argvars, typval_T *rettv)
11572{
11573 do_sort_uniq(argvars, rettv, FALSE);
11574}
11575
11576/*
11577 * "soundfold({word})" function
11578 */
11579 static void
11580f_soundfold(typval_T *argvars, typval_T *rettv)
11581{
11582 char_u *s;
11583
11584 rettv->v_type = VAR_STRING;
11585 s = get_tv_string(&argvars[0]);
11586#ifdef FEAT_SPELL
11587 rettv->vval.v_string = eval_soundfold(s);
11588#else
11589 rettv->vval.v_string = vim_strsave(s);
11590#endif
11591}
11592
11593/*
11594 * "spellbadword()" function
11595 */
11596 static void
11597f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
11598{
11599 char_u *word = (char_u *)"";
11600 hlf_T attr = HLF_COUNT;
11601 int len = 0;
11602
11603 if (rettv_list_alloc(rettv) == FAIL)
11604 return;
11605
11606#ifdef FEAT_SPELL
11607 if (argvars[0].v_type == VAR_UNKNOWN)
11608 {
11609 /* Find the start and length of the badly spelled word. */
11610 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
11611 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011612 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011613 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011614 curwin->w_set_curswant = TRUE;
11615 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011616 }
11617 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
11618 {
11619 char_u *str = get_tv_string_chk(&argvars[0]);
11620 int capcol = -1;
11621
11622 if (str != NULL)
11623 {
11624 /* Check the argument for spelling. */
11625 while (*str != NUL)
11626 {
11627 len = spell_check(curwin, str, &attr, &capcol, FALSE);
11628 if (attr != HLF_COUNT)
11629 {
11630 word = str;
11631 break;
11632 }
11633 str += len;
11634 }
11635 }
11636 }
11637#endif
11638
11639 list_append_string(rettv->vval.v_list, word, len);
11640 list_append_string(rettv->vval.v_list, (char_u *)(
11641 attr == HLF_SPB ? "bad" :
11642 attr == HLF_SPR ? "rare" :
11643 attr == HLF_SPL ? "local" :
11644 attr == HLF_SPC ? "caps" :
11645 ""), -1);
11646}
11647
11648/*
11649 * "spellsuggest()" function
11650 */
11651 static void
11652f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
11653{
11654#ifdef FEAT_SPELL
11655 char_u *str;
11656 int typeerr = FALSE;
11657 int maxcount;
11658 garray_T ga;
11659 int i;
11660 listitem_T *li;
11661 int need_capital = FALSE;
11662#endif
11663
11664 if (rettv_list_alloc(rettv) == FAIL)
11665 return;
11666
11667#ifdef FEAT_SPELL
11668 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
11669 {
11670 str = get_tv_string(&argvars[0]);
11671 if (argvars[1].v_type != VAR_UNKNOWN)
11672 {
11673 maxcount = (int)get_tv_number_chk(&argvars[1], &typeerr);
11674 if (maxcount <= 0)
11675 return;
11676 if (argvars[2].v_type != VAR_UNKNOWN)
11677 {
11678 need_capital = (int)get_tv_number_chk(&argvars[2], &typeerr);
11679 if (typeerr)
11680 return;
11681 }
11682 }
11683 else
11684 maxcount = 25;
11685
11686 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
11687
11688 for (i = 0; i < ga.ga_len; ++i)
11689 {
11690 str = ((char_u **)ga.ga_data)[i];
11691
11692 li = listitem_alloc();
11693 if (li == NULL)
11694 vim_free(str);
11695 else
11696 {
11697 li->li_tv.v_type = VAR_STRING;
11698 li->li_tv.v_lock = 0;
11699 li->li_tv.vval.v_string = str;
11700 list_append(rettv->vval.v_list, li);
11701 }
11702 }
11703 ga_clear(&ga);
11704 }
11705#endif
11706}
11707
11708 static void
11709f_split(typval_T *argvars, typval_T *rettv)
11710{
11711 char_u *str;
11712 char_u *end;
11713 char_u *pat = NULL;
11714 regmatch_T regmatch;
11715 char_u patbuf[NUMBUFLEN];
11716 char_u *save_cpo;
11717 int match;
11718 colnr_T col = 0;
11719 int keepempty = FALSE;
11720 int typeerr = FALSE;
11721
11722 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11723 save_cpo = p_cpo;
11724 p_cpo = (char_u *)"";
11725
11726 str = get_tv_string(&argvars[0]);
11727 if (argvars[1].v_type != VAR_UNKNOWN)
11728 {
11729 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11730 if (pat == NULL)
11731 typeerr = TRUE;
11732 if (argvars[2].v_type != VAR_UNKNOWN)
11733 keepempty = (int)get_tv_number_chk(&argvars[2], &typeerr);
11734 }
11735 if (pat == NULL || *pat == NUL)
11736 pat = (char_u *)"[\\x01- ]\\+";
11737
11738 if (rettv_list_alloc(rettv) == FAIL)
11739 return;
11740 if (typeerr)
11741 return;
11742
11743 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11744 if (regmatch.regprog != NULL)
11745 {
11746 regmatch.rm_ic = FALSE;
11747 while (*str != NUL || keepempty)
11748 {
11749 if (*str == NUL)
11750 match = FALSE; /* empty item at the end */
11751 else
11752 match = vim_regexec_nl(&regmatch, str, col);
11753 if (match)
11754 end = regmatch.startp[0];
11755 else
11756 end = str + STRLEN(str);
11757 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
11758 && *str != NUL && match && end < regmatch.endp[0]))
11759 {
11760 if (list_append_string(rettv->vval.v_list, str,
11761 (int)(end - str)) == FAIL)
11762 break;
11763 }
11764 if (!match)
11765 break;
11766 /* Advance to just after the match. */
11767 if (regmatch.endp[0] > str)
11768 col = 0;
11769 else
11770 {
11771 /* Don't get stuck at the same match. */
11772#ifdef FEAT_MBYTE
11773 col = (*mb_ptr2len)(regmatch.endp[0]);
11774#else
11775 col = 1;
11776#endif
11777 }
11778 str = regmatch.endp[0];
11779 }
11780
11781 vim_regfree(regmatch.regprog);
11782 }
11783
11784 p_cpo = save_cpo;
11785}
11786
11787#ifdef FEAT_FLOAT
11788/*
11789 * "sqrt()" function
11790 */
11791 static void
11792f_sqrt(typval_T *argvars, typval_T *rettv)
11793{
11794 float_T f = 0.0;
11795
11796 rettv->v_type = VAR_FLOAT;
11797 if (get_float_arg(argvars, &f) == OK)
11798 rettv->vval.v_float = sqrt(f);
11799 else
11800 rettv->vval.v_float = 0.0;
11801}
11802
11803/*
11804 * "str2float()" function
11805 */
11806 static void
11807f_str2float(typval_T *argvars, typval_T *rettv)
11808{
11809 char_u *p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011810 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011811
Bram Moolenaar08243d22017-01-10 16:12:29 +010011812 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011813 p = skipwhite(p + 1);
11814 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011815 if (isneg)
11816 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011817 rettv->v_type = VAR_FLOAT;
11818}
11819#endif
11820
11821/*
11822 * "str2nr()" function
11823 */
11824 static void
11825f_str2nr(typval_T *argvars, typval_T *rettv)
11826{
11827 int base = 10;
11828 char_u *p;
11829 varnumber_T n;
11830 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010011831 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011832
11833 if (argvars[1].v_type != VAR_UNKNOWN)
11834 {
11835 base = (int)get_tv_number(&argvars[1]);
11836 if (base != 2 && base != 8 && base != 10 && base != 16)
11837 {
11838 EMSG(_(e_invarg));
11839 return;
11840 }
11841 }
11842
11843 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011844 isneg = (*p == '-');
11845 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011846 p = skipwhite(p + 1);
11847 switch (base)
11848 {
11849 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
11850 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
11851 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
11852 default: what = 0;
11853 }
11854 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011855 if (isneg)
11856 rettv->vval.v_number = -n;
11857 else
11858 rettv->vval.v_number = n;
11859
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011860}
11861
11862#ifdef HAVE_STRFTIME
11863/*
11864 * "strftime({format}[, {time}])" function
11865 */
11866 static void
11867f_strftime(typval_T *argvars, typval_T *rettv)
11868{
11869 char_u result_buf[256];
11870 struct tm *curtime;
11871 time_t seconds;
11872 char_u *p;
11873
11874 rettv->v_type = VAR_STRING;
11875
11876 p = get_tv_string(&argvars[0]);
11877 if (argvars[1].v_type == VAR_UNKNOWN)
11878 seconds = time(NULL);
11879 else
11880 seconds = (time_t)get_tv_number(&argvars[1]);
11881 curtime = localtime(&seconds);
11882 /* MSVC returns NULL for an invalid value of seconds. */
11883 if (curtime == NULL)
11884 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
11885 else
11886 {
11887# ifdef FEAT_MBYTE
11888 vimconv_T conv;
11889 char_u *enc;
11890
11891 conv.vc_type = CONV_NONE;
11892 enc = enc_locale();
11893 convert_setup(&conv, p_enc, enc);
11894 if (conv.vc_type != CONV_NONE)
11895 p = string_convert(&conv, p, NULL);
11896# endif
11897 if (p != NULL)
11898 (void)strftime((char *)result_buf, sizeof(result_buf),
11899 (char *)p, curtime);
11900 else
11901 result_buf[0] = NUL;
11902
11903# ifdef FEAT_MBYTE
11904 if (conv.vc_type != CONV_NONE)
11905 vim_free(p);
11906 convert_setup(&conv, enc, p_enc);
11907 if (conv.vc_type != CONV_NONE)
11908 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
11909 else
11910# endif
11911 rettv->vval.v_string = vim_strsave(result_buf);
11912
11913# ifdef FEAT_MBYTE
11914 /* Release conversion descriptors */
11915 convert_setup(&conv, NULL, NULL);
11916 vim_free(enc);
11917# endif
11918 }
11919}
11920#endif
11921
11922/*
11923 * "strgetchar()" function
11924 */
11925 static void
11926f_strgetchar(typval_T *argvars, typval_T *rettv)
11927{
11928 char_u *str;
11929 int len;
11930 int error = FALSE;
11931 int charidx;
11932
11933 rettv->vval.v_number = -1;
11934 str = get_tv_string_chk(&argvars[0]);
11935 if (str == NULL)
11936 return;
11937 len = (int)STRLEN(str);
11938 charidx = (int)get_tv_number_chk(&argvars[1], &error);
11939 if (error)
11940 return;
11941#ifdef FEAT_MBYTE
11942 {
11943 int byteidx = 0;
11944
11945 while (charidx >= 0 && byteidx < len)
11946 {
11947 if (charidx == 0)
11948 {
11949 rettv->vval.v_number = mb_ptr2char(str + byteidx);
11950 break;
11951 }
11952 --charidx;
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011953 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011954 }
11955 }
11956#else
11957 if (charidx < len)
11958 rettv->vval.v_number = str[charidx];
11959#endif
11960}
11961
11962/*
11963 * "stridx()" function
11964 */
11965 static void
11966f_stridx(typval_T *argvars, typval_T *rettv)
11967{
11968 char_u buf[NUMBUFLEN];
11969 char_u *needle;
11970 char_u *haystack;
11971 char_u *save_haystack;
11972 char_u *pos;
11973 int start_idx;
11974
11975 needle = get_tv_string_chk(&argvars[1]);
11976 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
11977 rettv->vval.v_number = -1;
11978 if (needle == NULL || haystack == NULL)
11979 return; /* type error; errmsg already given */
11980
11981 if (argvars[2].v_type != VAR_UNKNOWN)
11982 {
11983 int error = FALSE;
11984
11985 start_idx = (int)get_tv_number_chk(&argvars[2], &error);
11986 if (error || start_idx >= (int)STRLEN(haystack))
11987 return;
11988 if (start_idx >= 0)
11989 haystack += start_idx;
11990 }
11991
11992 pos = (char_u *)strstr((char *)haystack, (char *)needle);
11993 if (pos != NULL)
11994 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
11995}
11996
11997/*
11998 * "string()" function
11999 */
12000 static void
12001f_string(typval_T *argvars, typval_T *rettv)
12002{
12003 char_u *tofree;
12004 char_u numbuf[NUMBUFLEN];
12005
12006 rettv->v_type = VAR_STRING;
12007 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
12008 get_copyID());
12009 /* Make a copy if we have a value but it's not in allocated memory. */
12010 if (rettv->vval.v_string != NULL && tofree == NULL)
12011 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
12012}
12013
12014/*
12015 * "strlen()" function
12016 */
12017 static void
12018f_strlen(typval_T *argvars, typval_T *rettv)
12019{
12020 rettv->vval.v_number = (varnumber_T)(STRLEN(
12021 get_tv_string(&argvars[0])));
12022}
12023
12024/*
12025 * "strchars()" function
12026 */
12027 static void
12028f_strchars(typval_T *argvars, typval_T *rettv)
12029{
12030 char_u *s = get_tv_string(&argvars[0]);
12031 int skipcc = 0;
12032#ifdef FEAT_MBYTE
12033 varnumber_T len = 0;
12034 int (*func_mb_ptr2char_adv)(char_u **pp);
12035#endif
12036
12037 if (argvars[1].v_type != VAR_UNKNOWN)
12038 skipcc = (int)get_tv_number_chk(&argvars[1], NULL);
12039 if (skipcc < 0 || skipcc > 1)
12040 EMSG(_(e_invarg));
12041 else
12042 {
12043#ifdef FEAT_MBYTE
12044 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
12045 while (*s != NUL)
12046 {
12047 func_mb_ptr2char_adv(&s);
12048 ++len;
12049 }
12050 rettv->vval.v_number = len;
12051#else
12052 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
12053#endif
12054 }
12055}
12056
12057/*
12058 * "strdisplaywidth()" function
12059 */
12060 static void
12061f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
12062{
12063 char_u *s = get_tv_string(&argvars[0]);
12064 int col = 0;
12065
12066 if (argvars[1].v_type != VAR_UNKNOWN)
12067 col = (int)get_tv_number(&argvars[1]);
12068
12069 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
12070}
12071
12072/*
12073 * "strwidth()" function
12074 */
12075 static void
12076f_strwidth(typval_T *argvars, typval_T *rettv)
12077{
12078 char_u *s = get_tv_string(&argvars[0]);
12079
12080 rettv->vval.v_number = (varnumber_T)(
12081#ifdef FEAT_MBYTE
12082 mb_string2cells(s, -1)
12083#else
12084 STRLEN(s)
12085#endif
12086 );
12087}
12088
12089/*
12090 * "strcharpart()" function
12091 */
12092 static void
12093f_strcharpart(typval_T *argvars, typval_T *rettv)
12094{
12095#ifdef FEAT_MBYTE
12096 char_u *p;
12097 int nchar;
12098 int nbyte = 0;
12099 int charlen;
12100 int len = 0;
12101 int slen;
12102 int error = FALSE;
12103
12104 p = get_tv_string(&argvars[0]);
12105 slen = (int)STRLEN(p);
12106
12107 nchar = (int)get_tv_number_chk(&argvars[1], &error);
12108 if (!error)
12109 {
12110 if (nchar > 0)
12111 while (nchar > 0 && nbyte < slen)
12112 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012113 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012114 --nchar;
12115 }
12116 else
12117 nbyte = nchar;
12118 if (argvars[2].v_type != VAR_UNKNOWN)
12119 {
12120 charlen = (int)get_tv_number(&argvars[2]);
12121 while (charlen > 0 && nbyte + len < slen)
12122 {
12123 int off = nbyte + len;
12124
12125 if (off < 0)
12126 len += 1;
12127 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012128 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012129 --charlen;
12130 }
12131 }
12132 else
12133 len = slen - nbyte; /* default: all bytes that are available. */
12134 }
12135
12136 /*
12137 * Only return the overlap between the specified part and the actual
12138 * string.
12139 */
12140 if (nbyte < 0)
12141 {
12142 len += nbyte;
12143 nbyte = 0;
12144 }
12145 else if (nbyte > slen)
12146 nbyte = slen;
12147 if (len < 0)
12148 len = 0;
12149 else if (nbyte + len > slen)
12150 len = slen - nbyte;
12151
12152 rettv->v_type = VAR_STRING;
12153 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
12154#else
12155 f_strpart(argvars, rettv);
12156#endif
12157}
12158
12159/*
12160 * "strpart()" function
12161 */
12162 static void
12163f_strpart(typval_T *argvars, typval_T *rettv)
12164{
12165 char_u *p;
12166 int n;
12167 int len;
12168 int slen;
12169 int error = FALSE;
12170
12171 p = get_tv_string(&argvars[0]);
12172 slen = (int)STRLEN(p);
12173
12174 n = (int)get_tv_number_chk(&argvars[1], &error);
12175 if (error)
12176 len = 0;
12177 else if (argvars[2].v_type != VAR_UNKNOWN)
12178 len = (int)get_tv_number(&argvars[2]);
12179 else
12180 len = slen - n; /* default len: all bytes that are available. */
12181
12182 /*
12183 * Only return the overlap between the specified part and the actual
12184 * string.
12185 */
12186 if (n < 0)
12187 {
12188 len += n;
12189 n = 0;
12190 }
12191 else if (n > slen)
12192 n = slen;
12193 if (len < 0)
12194 len = 0;
12195 else if (n + len > slen)
12196 len = slen - n;
12197
12198 rettv->v_type = VAR_STRING;
12199 rettv->vval.v_string = vim_strnsave(p + n, len);
12200}
12201
12202/*
12203 * "strridx()" function
12204 */
12205 static void
12206f_strridx(typval_T *argvars, typval_T *rettv)
12207{
12208 char_u buf[NUMBUFLEN];
12209 char_u *needle;
12210 char_u *haystack;
12211 char_u *rest;
12212 char_u *lastmatch = NULL;
12213 int haystack_len, end_idx;
12214
12215 needle = get_tv_string_chk(&argvars[1]);
12216 haystack = get_tv_string_buf_chk(&argvars[0], buf);
12217
12218 rettv->vval.v_number = -1;
12219 if (needle == NULL || haystack == NULL)
12220 return; /* type error; errmsg already given */
12221
12222 haystack_len = (int)STRLEN(haystack);
12223 if (argvars[2].v_type != VAR_UNKNOWN)
12224 {
12225 /* Third argument: upper limit for index */
12226 end_idx = (int)get_tv_number_chk(&argvars[2], NULL);
12227 if (end_idx < 0)
12228 return; /* can never find a match */
12229 }
12230 else
12231 end_idx = haystack_len;
12232
12233 if (*needle == NUL)
12234 {
12235 /* Empty string matches past the end. */
12236 lastmatch = haystack + end_idx;
12237 }
12238 else
12239 {
12240 for (rest = haystack; *rest != '\0'; ++rest)
12241 {
12242 rest = (char_u *)strstr((char *)rest, (char *)needle);
12243 if (rest == NULL || rest > haystack + end_idx)
12244 break;
12245 lastmatch = rest;
12246 }
12247 }
12248
12249 if (lastmatch == NULL)
12250 rettv->vval.v_number = -1;
12251 else
12252 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
12253}
12254
12255/*
12256 * "strtrans()" function
12257 */
12258 static void
12259f_strtrans(typval_T *argvars, typval_T *rettv)
12260{
12261 rettv->v_type = VAR_STRING;
12262 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
12263}
12264
12265/*
12266 * "submatch()" function
12267 */
12268 static void
12269f_submatch(typval_T *argvars, typval_T *rettv)
12270{
12271 int error = FALSE;
12272 int no;
12273 int retList = 0;
12274
12275 no = (int)get_tv_number_chk(&argvars[0], &error);
12276 if (error)
12277 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012278 if (no < 0 || no >= NSUBEXP)
12279 {
Bram Moolenaar79518e22017-02-17 16:31:35 +010012280 EMSGN(_("E935: invalid submatch number: %d"), no);
12281 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012282 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012283 if (argvars[1].v_type != VAR_UNKNOWN)
12284 retList = (int)get_tv_number_chk(&argvars[1], &error);
12285 if (error)
12286 return;
12287
12288 if (retList == 0)
12289 {
12290 rettv->v_type = VAR_STRING;
12291 rettv->vval.v_string = reg_submatch(no);
12292 }
12293 else
12294 {
12295 rettv->v_type = VAR_LIST;
12296 rettv->vval.v_list = reg_submatch_list(no);
12297 }
12298}
12299
12300/*
12301 * "substitute()" function
12302 */
12303 static void
12304f_substitute(typval_T *argvars, typval_T *rettv)
12305{
12306 char_u patbuf[NUMBUFLEN];
12307 char_u subbuf[NUMBUFLEN];
12308 char_u flagsbuf[NUMBUFLEN];
12309
12310 char_u *str = get_tv_string_chk(&argvars[0]);
12311 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012312 char_u *sub = NULL;
12313 typval_T *expr = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012314 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
12315
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012316 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
12317 expr = &argvars[2];
12318 else
12319 sub = get_tv_string_buf_chk(&argvars[2], subbuf);
12320
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012321 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012322 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
12323 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012324 rettv->vval.v_string = NULL;
12325 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012326 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012327}
12328
12329/*
12330 * "synID(lnum, col, trans)" function
12331 */
12332 static void
12333f_synID(typval_T *argvars UNUSED, typval_T *rettv)
12334{
12335 int id = 0;
12336#ifdef FEAT_SYN_HL
12337 linenr_T lnum;
12338 colnr_T col;
12339 int trans;
12340 int transerr = FALSE;
12341
12342 lnum = get_tv_lnum(argvars); /* -1 on type error */
12343 col = (linenr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12344 trans = (int)get_tv_number_chk(&argvars[2], &transerr);
12345
12346 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12347 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
12348 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
12349#endif
12350
12351 rettv->vval.v_number = id;
12352}
12353
12354/*
12355 * "synIDattr(id, what [, mode])" function
12356 */
12357 static void
12358f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
12359{
12360 char_u *p = NULL;
12361#ifdef FEAT_SYN_HL
12362 int id;
12363 char_u *what;
12364 char_u *mode;
12365 char_u modebuf[NUMBUFLEN];
12366 int modec;
12367
12368 id = (int)get_tv_number(&argvars[0]);
12369 what = get_tv_string(&argvars[1]);
12370 if (argvars[2].v_type != VAR_UNKNOWN)
12371 {
12372 mode = get_tv_string_buf(&argvars[2], modebuf);
12373 modec = TOLOWER_ASC(mode[0]);
12374 if (modec != 't' && modec != 'c' && modec != 'g')
12375 modec = 0; /* replace invalid with current */
12376 }
12377 else
12378 {
12379#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
12380 if (USE_24BIT)
12381 modec = 'g';
12382 else
12383#endif
12384 if (t_colors > 1)
12385 modec = 'c';
12386 else
12387 modec = 't';
12388 }
12389
12390
12391 switch (TOLOWER_ASC(what[0]))
12392 {
12393 case 'b':
12394 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
12395 p = highlight_color(id, what, modec);
12396 else /* bold */
12397 p = highlight_has_attr(id, HL_BOLD, modec);
12398 break;
12399
12400 case 'f': /* fg[#] or font */
12401 p = highlight_color(id, what, modec);
12402 break;
12403
12404 case 'i':
12405 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
12406 p = highlight_has_attr(id, HL_INVERSE, modec);
12407 else /* italic */
12408 p = highlight_has_attr(id, HL_ITALIC, modec);
12409 break;
12410
12411 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020012412 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012413 break;
12414
12415 case 'r': /* reverse */
12416 p = highlight_has_attr(id, HL_INVERSE, modec);
12417 break;
12418
12419 case 's':
12420 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
12421 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020012422 /* strikeout */
12423 else if (TOLOWER_ASC(what[1]) == 't' &&
12424 TOLOWER_ASC(what[2]) == 'r')
12425 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012426 else /* standout */
12427 p = highlight_has_attr(id, HL_STANDOUT, modec);
12428 break;
12429
12430 case 'u':
12431 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
12432 /* underline */
12433 p = highlight_has_attr(id, HL_UNDERLINE, modec);
12434 else
12435 /* undercurl */
12436 p = highlight_has_attr(id, HL_UNDERCURL, modec);
12437 break;
12438 }
12439
12440 if (p != NULL)
12441 p = vim_strsave(p);
12442#endif
12443 rettv->v_type = VAR_STRING;
12444 rettv->vval.v_string = p;
12445}
12446
12447/*
12448 * "synIDtrans(id)" function
12449 */
12450 static void
12451f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
12452{
12453 int id;
12454
12455#ifdef FEAT_SYN_HL
12456 id = (int)get_tv_number(&argvars[0]);
12457
12458 if (id > 0)
12459 id = syn_get_final_id(id);
12460 else
12461#endif
12462 id = 0;
12463
12464 rettv->vval.v_number = id;
12465}
12466
12467/*
12468 * "synconcealed(lnum, col)" function
12469 */
12470 static void
12471f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
12472{
12473#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12474 linenr_T lnum;
12475 colnr_T col;
12476 int syntax_flags = 0;
12477 int cchar;
12478 int matchid = 0;
12479 char_u str[NUMBUFLEN];
12480#endif
12481
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012482 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012483
12484#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12485 lnum = get_tv_lnum(argvars); /* -1 on type error */
12486 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12487
12488 vim_memset(str, NUL, sizeof(str));
12489
12490 if (rettv_list_alloc(rettv) != FAIL)
12491 {
12492 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12493 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12494 && curwin->w_p_cole > 0)
12495 {
12496 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
12497 syntax_flags = get_syntax_info(&matchid);
12498
12499 /* get the conceal character */
12500 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
12501 {
12502 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020012503 if (cchar == NUL && curwin->w_p_cole == 1)
12504 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012505 if (cchar != NUL)
12506 {
12507# ifdef FEAT_MBYTE
12508 if (has_mbyte)
12509 (*mb_char2bytes)(cchar, str);
12510 else
12511# endif
12512 str[0] = cchar;
12513 }
12514 }
12515 }
12516
12517 list_append_number(rettv->vval.v_list,
12518 (syntax_flags & HL_CONCEAL) != 0);
12519 /* -1 to auto-determine strlen */
12520 list_append_string(rettv->vval.v_list, str, -1);
12521 list_append_number(rettv->vval.v_list, matchid);
12522 }
12523#endif
12524}
12525
12526/*
12527 * "synstack(lnum, col)" function
12528 */
12529 static void
12530f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
12531{
12532#ifdef FEAT_SYN_HL
12533 linenr_T lnum;
12534 colnr_T col;
12535 int i;
12536 int id;
12537#endif
12538
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012539 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012540
12541#ifdef FEAT_SYN_HL
12542 lnum = get_tv_lnum(argvars); /* -1 on type error */
12543 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12544
12545 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12546 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12547 && rettv_list_alloc(rettv) != FAIL)
12548 {
12549 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
12550 for (i = 0; ; ++i)
12551 {
12552 id = syn_get_stack_item(i);
12553 if (id < 0)
12554 break;
12555 if (list_append_number(rettv->vval.v_list, id) == FAIL)
12556 break;
12557 }
12558 }
12559#endif
12560}
12561
12562 static void
12563get_cmd_output_as_rettv(
12564 typval_T *argvars,
12565 typval_T *rettv,
12566 int retlist)
12567{
12568 char_u *res = NULL;
12569 char_u *p;
12570 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012571 int err = FALSE;
12572 FILE *fd;
12573 list_T *list = NULL;
12574 int flags = SHELL_SILENT;
12575
12576 rettv->v_type = VAR_STRING;
12577 rettv->vval.v_string = NULL;
12578 if (check_restricted() || check_secure())
12579 goto errret;
12580
12581 if (argvars[1].v_type != VAR_UNKNOWN)
12582 {
12583 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010012584 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012585 * command.
12586 */
12587 if ((infile = vim_tempname('i', TRUE)) == NULL)
12588 {
12589 EMSG(_(e_notmp));
12590 goto errret;
12591 }
12592
12593 fd = mch_fopen((char *)infile, WRITEBIN);
12594 if (fd == NULL)
12595 {
12596 EMSG2(_(e_notopen), infile);
12597 goto errret;
12598 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010012599 if (argvars[1].v_type == VAR_NUMBER)
12600 {
12601 linenr_T lnum;
12602 buf_T *buf;
12603
12604 buf = buflist_findnr(argvars[1].vval.v_number);
12605 if (buf == NULL)
12606 {
12607 EMSGN(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010012608 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010012609 goto errret;
12610 }
12611
12612 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
12613 {
12614 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
12615 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
12616 {
12617 err = TRUE;
12618 break;
12619 }
12620 if (putc(NL, fd) == EOF)
12621 {
12622 err = TRUE;
12623 break;
12624 }
12625 }
12626 }
12627 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012628 {
12629 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
12630 err = TRUE;
12631 }
12632 else
12633 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010012634 size_t len;
12635 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012636
12637 p = get_tv_string_buf_chk(&argvars[1], buf);
12638 if (p == NULL)
12639 {
12640 fclose(fd);
12641 goto errret; /* type error; errmsg already given */
12642 }
12643 len = STRLEN(p);
12644 if (len > 0 && fwrite(p, len, 1, fd) != 1)
12645 err = TRUE;
12646 }
12647 if (fclose(fd) != 0)
12648 err = TRUE;
12649 if (err)
12650 {
12651 EMSG(_("E677: Error writing temp file"));
12652 goto errret;
12653 }
12654 }
12655
12656 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
12657 * echoes typeahead, that messes up the display. */
12658 if (!msg_silent)
12659 flags += SHELL_COOKED;
12660
12661 if (retlist)
12662 {
12663 int len;
12664 listitem_T *li;
12665 char_u *s = NULL;
12666 char_u *start;
12667 char_u *end;
12668 int i;
12669
12670 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
12671 if (res == NULL)
12672 goto errret;
12673
12674 list = list_alloc();
12675 if (list == NULL)
12676 goto errret;
12677
12678 for (i = 0; i < len; ++i)
12679 {
12680 start = res + i;
12681 while (i < len && res[i] != NL)
12682 ++i;
12683 end = res + i;
12684
12685 s = alloc((unsigned)(end - start + 1));
12686 if (s == NULL)
12687 goto errret;
12688
12689 for (p = s; start < end; ++p, ++start)
12690 *p = *start == NUL ? NL : *start;
12691 *p = NUL;
12692
12693 li = listitem_alloc();
12694 if (li == NULL)
12695 {
12696 vim_free(s);
12697 goto errret;
12698 }
12699 li->li_tv.v_type = VAR_STRING;
12700 li->li_tv.v_lock = 0;
12701 li->li_tv.vval.v_string = s;
12702 list_append(list, li);
12703 }
12704
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012705 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012706 list = NULL;
12707 }
12708 else
12709 {
12710 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
12711#ifdef USE_CR
12712 /* translate <CR> into <NL> */
12713 if (res != NULL)
12714 {
12715 char_u *s;
12716
12717 for (s = res; *s; ++s)
12718 {
12719 if (*s == CAR)
12720 *s = NL;
12721 }
12722 }
12723#else
12724# ifdef USE_CRNL
12725 /* translate <CR><NL> into <NL> */
12726 if (res != NULL)
12727 {
12728 char_u *s, *d;
12729
12730 d = res;
12731 for (s = res; *s; ++s)
12732 {
12733 if (s[0] == CAR && s[1] == NL)
12734 ++s;
12735 *d++ = *s;
12736 }
12737 *d = NUL;
12738 }
12739# endif
12740#endif
12741 rettv->vval.v_string = res;
12742 res = NULL;
12743 }
12744
12745errret:
12746 if (infile != NULL)
12747 {
12748 mch_remove(infile);
12749 vim_free(infile);
12750 }
12751 if (res != NULL)
12752 vim_free(res);
12753 if (list != NULL)
12754 list_free(list);
12755}
12756
12757/*
12758 * "system()" function
12759 */
12760 static void
12761f_system(typval_T *argvars, typval_T *rettv)
12762{
12763 get_cmd_output_as_rettv(argvars, rettv, FALSE);
12764}
12765
12766/*
12767 * "systemlist()" function
12768 */
12769 static void
12770f_systemlist(typval_T *argvars, typval_T *rettv)
12771{
12772 get_cmd_output_as_rettv(argvars, rettv, TRUE);
12773}
12774
12775/*
12776 * "tabpagebuflist()" function
12777 */
12778 static void
12779f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12780{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012781 tabpage_T *tp;
12782 win_T *wp = NULL;
12783
12784 if (argvars[0].v_type == VAR_UNKNOWN)
12785 wp = firstwin;
12786 else
12787 {
12788 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12789 if (tp != NULL)
12790 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12791 }
12792 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
12793 {
12794 for (; wp != NULL; wp = wp->w_next)
12795 if (list_append_number(rettv->vval.v_list,
12796 wp->w_buffer->b_fnum) == FAIL)
12797 break;
12798 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012799}
12800
12801
12802/*
12803 * "tabpagenr()" function
12804 */
12805 static void
12806f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
12807{
12808 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012809 char_u *arg;
12810
12811 if (argvars[0].v_type != VAR_UNKNOWN)
12812 {
12813 arg = get_tv_string_chk(&argvars[0]);
12814 nr = 0;
12815 if (arg != NULL)
12816 {
12817 if (STRCMP(arg, "$") == 0)
12818 nr = tabpage_index(NULL) - 1;
12819 else
12820 EMSG2(_(e_invexpr2), arg);
12821 }
12822 }
12823 else
12824 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012825 rettv->vval.v_number = nr;
12826}
12827
12828
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012829static int get_winnr(tabpage_T *tp, typval_T *argvar);
12830
12831/*
12832 * Common code for tabpagewinnr() and winnr().
12833 */
12834 static int
12835get_winnr(tabpage_T *tp, typval_T *argvar)
12836{
12837 win_T *twin;
12838 int nr = 1;
12839 win_T *wp;
12840 char_u *arg;
12841
12842 twin = (tp == curtab) ? curwin : tp->tp_curwin;
12843 if (argvar->v_type != VAR_UNKNOWN)
12844 {
12845 arg = get_tv_string_chk(argvar);
12846 if (arg == NULL)
12847 nr = 0; /* type error; errmsg already given */
12848 else if (STRCMP(arg, "$") == 0)
12849 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
12850 else if (STRCMP(arg, "#") == 0)
12851 {
12852 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
12853 if (twin == NULL)
12854 nr = 0;
12855 }
12856 else
12857 {
12858 EMSG2(_(e_invexpr2), arg);
12859 nr = 0;
12860 }
12861 }
12862
12863 if (nr > 0)
12864 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12865 wp != twin; wp = wp->w_next)
12866 {
12867 if (wp == NULL)
12868 {
12869 /* didn't find it in this tabpage */
12870 nr = 0;
12871 break;
12872 }
12873 ++nr;
12874 }
12875 return nr;
12876}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012877
12878/*
12879 * "tabpagewinnr()" function
12880 */
12881 static void
12882f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
12883{
12884 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012885 tabpage_T *tp;
12886
12887 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12888 if (tp == NULL)
12889 nr = 0;
12890 else
12891 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012892 rettv->vval.v_number = nr;
12893}
12894
12895
12896/*
12897 * "tagfiles()" function
12898 */
12899 static void
12900f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
12901{
12902 char_u *fname;
12903 tagname_T tn;
12904 int first;
12905
12906 if (rettv_list_alloc(rettv) == FAIL)
12907 return;
12908 fname = alloc(MAXPATHL);
12909 if (fname == NULL)
12910 return;
12911
12912 for (first = TRUE; ; first = FALSE)
12913 if (get_tagfname(&tn, first, fname) == FAIL
12914 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
12915 break;
12916 tagname_free(&tn);
12917 vim_free(fname);
12918}
12919
12920/*
12921 * "taglist()" function
12922 */
12923 static void
12924f_taglist(typval_T *argvars, typval_T *rettv)
12925{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012926 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012927 char_u *tag_pattern;
12928
12929 tag_pattern = get_tv_string(&argvars[0]);
12930
12931 rettv->vval.v_number = FALSE;
12932 if (*tag_pattern == NUL)
12933 return;
12934
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012935 if (argvars[1].v_type != VAR_UNKNOWN)
12936 fname = get_tv_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012937 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012938 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012939}
12940
12941/*
12942 * "tempname()" function
12943 */
12944 static void
12945f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
12946{
12947 static int x = 'A';
12948
12949 rettv->v_type = VAR_STRING;
12950 rettv->vval.v_string = vim_tempname(x, FALSE);
12951
12952 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
12953 * names. Skip 'I' and 'O', they are used for shell redirection. */
12954 do
12955 {
12956 if (x == 'Z')
12957 x = '0';
12958 else if (x == '9')
12959 x = 'A';
12960 else
12961 {
12962#ifdef EBCDIC
12963 if (x == 'I')
12964 x = 'J';
12965 else if (x == 'R')
12966 x = 'S';
12967 else
12968#endif
12969 ++x;
12970 }
12971 } while (x == 'I' || x == 'O');
12972}
12973
12974#ifdef FEAT_FLOAT
12975/*
12976 * "tan()" function
12977 */
12978 static void
12979f_tan(typval_T *argvars, typval_T *rettv)
12980{
12981 float_T f = 0.0;
12982
12983 rettv->v_type = VAR_FLOAT;
12984 if (get_float_arg(argvars, &f) == OK)
12985 rettv->vval.v_float = tan(f);
12986 else
12987 rettv->vval.v_float = 0.0;
12988}
12989
12990/*
12991 * "tanh()" function
12992 */
12993 static void
12994f_tanh(typval_T *argvars, typval_T *rettv)
12995{
12996 float_T f = 0.0;
12997
12998 rettv->v_type = VAR_FLOAT;
12999 if (get_float_arg(argvars, &f) == OK)
13000 rettv->vval.v_float = tanh(f);
13001 else
13002 rettv->vval.v_float = 0.0;
13003}
13004#endif
13005
13006/*
13007 * "test_alloc_fail(id, countdown, repeat)" function
13008 */
13009 static void
13010f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
13011{
13012 if (argvars[0].v_type != VAR_NUMBER
13013 || argvars[0].vval.v_number <= 0
13014 || argvars[1].v_type != VAR_NUMBER
13015 || argvars[1].vval.v_number < 0
13016 || argvars[2].v_type != VAR_NUMBER)
13017 EMSG(_(e_invarg));
13018 else
13019 {
13020 alloc_fail_id = argvars[0].vval.v_number;
13021 if (alloc_fail_id >= aid_last)
13022 EMSG(_(e_invarg));
13023 alloc_fail_countdown = argvars[1].vval.v_number;
13024 alloc_fail_repeat = argvars[2].vval.v_number;
13025 did_outofmem_msg = FALSE;
13026 }
13027}
13028
13029/*
13030 * "test_autochdir()"
13031 */
13032 static void
13033f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13034{
13035#if defined(FEAT_AUTOCHDIR)
13036 test_autochdir = TRUE;
13037#endif
13038}
13039
13040/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020013041 * "test_feedinput()"
13042 */
13043 static void
13044f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
13045{
13046#ifdef USE_INPUT_BUF
13047 char_u *val = get_tv_string_chk(&argvars[0]);
13048
13049 if (val != NULL)
13050 {
13051 trash_input_buf();
13052 add_to_input_buf_csi(val, (int)STRLEN(val));
13053 }
13054#endif
13055}
13056
13057/*
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013058 * "test_disable({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013059 */
13060 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013061f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013062{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013063 char_u *name = (char_u *)"";
13064 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013065 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013066
13067 if (argvars[0].v_type != VAR_STRING
13068 || (argvars[1].v_type) != VAR_NUMBER)
13069 EMSG(_(e_invarg));
13070 else
13071 {
13072 name = get_tv_string_chk(&argvars[0]);
13073 val = (int)get_tv_number(&argvars[1]);
13074
13075 if (STRCMP(name, (char_u *)"redraw") == 0)
13076 disable_redraw_for_testing = val;
13077 else if (STRCMP(name, (char_u *)"char_avail") == 0)
13078 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013079 else if (STRCMP(name, (char_u *)"starting") == 0)
13080 {
13081 if (val)
13082 {
13083 if (save_starting < 0)
13084 save_starting = starting;
13085 starting = 0;
13086 }
13087 else
13088 {
13089 starting = save_starting;
13090 save_starting = -1;
13091 }
13092 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013093 else if (STRCMP(name, (char_u *)"nfa_fail") == 0)
13094 nfa_fail_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013095 else if (STRCMP(name, (char_u *)"ALL") == 0)
13096 {
13097 disable_char_avail_for_testing = FALSE;
13098 disable_redraw_for_testing = FALSE;
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013099 nfa_fail_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013100 if (save_starting >= 0)
13101 {
13102 starting = save_starting;
13103 save_starting = -1;
13104 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013105 }
13106 else
13107 EMSG2(_(e_invarg2), name);
13108 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013109}
13110
13111/*
13112 * "test_garbagecollect_now()" function
13113 */
13114 static void
13115f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13116{
13117 /* This is dangerous, any Lists and Dicts used internally may be freed
13118 * while still in use. */
13119 garbage_collect(TRUE);
13120}
13121
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013122/*
13123 * "test_ignore_error()" function
13124 */
13125 static void
13126f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
13127{
13128 ignore_error_for_testing(get_tv_string(&argvars[0]));
13129}
13130
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013131#ifdef FEAT_JOB_CHANNEL
13132 static void
13133f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
13134{
13135 rettv->v_type = VAR_CHANNEL;
13136 rettv->vval.v_channel = NULL;
13137}
13138#endif
13139
13140 static void
13141f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
13142{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013143 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013144}
13145
13146#ifdef FEAT_JOB_CHANNEL
13147 static void
13148f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
13149{
13150 rettv->v_type = VAR_JOB;
13151 rettv->vval.v_job = NULL;
13152}
13153#endif
13154
13155 static void
13156f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
13157{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013158 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013159}
13160
13161 static void
13162f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
13163{
13164 rettv->v_type = VAR_PARTIAL;
13165 rettv->vval.v_partial = NULL;
13166}
13167
13168 static void
13169f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
13170{
13171 rettv->v_type = VAR_STRING;
13172 rettv->vval.v_string = NULL;
13173}
13174
13175 static void
13176f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
13177{
13178 time_for_testing = (time_t)get_tv_number(&argvars[0]);
13179}
13180
13181#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
13182/*
13183 * Get a callback from "arg". It can be a Funcref or a function name.
13184 * When "arg" is zero return an empty string.
13185 * Return NULL for an invalid argument.
13186 */
13187 char_u *
13188get_callback(typval_T *arg, partial_T **pp)
13189{
13190 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
13191 {
13192 *pp = arg->vval.v_partial;
13193 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020013194 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013195 }
13196 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020013197 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013198 {
13199 func_ref(arg->vval.v_string);
13200 return arg->vval.v_string;
13201 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013202 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
13203 return (char_u *)"";
13204 EMSG(_("E921: Invalid callback argument"));
13205 return NULL;
13206}
13207
13208/*
Bram Moolenaard5359b22018-04-05 22:44:39 +020013209 * Unref/free "callback" and "partial" returned by get_callback().
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013210 */
13211 void
13212free_callback(char_u *callback, partial_T *partial)
13213{
13214 if (partial != NULL)
13215 partial_unref(partial);
13216 else if (callback != NULL)
13217 {
13218 func_unref(callback);
13219 vim_free(callback);
13220 }
13221}
13222#endif
13223
13224#ifdef FEAT_TIMERS
13225/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020013226 * "timer_info([timer])" function
13227 */
13228 static void
13229f_timer_info(typval_T *argvars, typval_T *rettv)
13230{
13231 timer_T *timer = NULL;
13232
13233 if (rettv_list_alloc(rettv) != OK)
13234 return;
13235 if (argvars[0].v_type != VAR_UNKNOWN)
13236 {
13237 if (argvars[0].v_type != VAR_NUMBER)
13238 EMSG(_(e_number_exp));
13239 else
13240 {
13241 timer = find_timer((int)get_tv_number(&argvars[0]));
13242 if (timer != NULL)
13243 add_timer_info(rettv, timer);
13244 }
13245 }
13246 else
13247 add_timer_info_all(rettv);
13248}
13249
13250/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013251 * "timer_pause(timer, paused)" function
13252 */
13253 static void
13254f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
13255{
13256 timer_T *timer = NULL;
13257 int paused = (int)get_tv_number(&argvars[1]);
13258
13259 if (argvars[0].v_type != VAR_NUMBER)
13260 EMSG(_(e_number_exp));
13261 else
13262 {
13263 timer = find_timer((int)get_tv_number(&argvars[0]));
13264 if (timer != NULL)
13265 timer->tr_paused = paused;
13266 }
13267}
13268
13269/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013270 * "timer_start(time, callback [, options])" function
13271 */
13272 static void
13273f_timer_start(typval_T *argvars, typval_T *rettv)
13274{
Bram Moolenaar75537a92016-09-05 22:45:28 +020013275 long msec = (long)get_tv_number(&argvars[0]);
13276 timer_T *timer;
13277 int repeat = 0;
13278 char_u *callback;
13279 dict_T *dict;
13280 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013281
Bram Moolenaar75537a92016-09-05 22:45:28 +020013282 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013283 if (check_secure())
13284 return;
13285 if (argvars[2].v_type != VAR_UNKNOWN)
13286 {
13287 if (argvars[2].v_type != VAR_DICT
13288 || (dict = argvars[2].vval.v_dict) == NULL)
13289 {
13290 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
13291 return;
13292 }
13293 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
13294 repeat = get_dict_number(dict, (char_u *)"repeat");
13295 }
13296
Bram Moolenaar75537a92016-09-05 22:45:28 +020013297 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013298 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020013299 return;
13300
13301 timer = create_timer(msec, repeat);
13302 if (timer == NULL)
13303 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013304 else
13305 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020013306 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020013307 timer->tr_callback = vim_strsave(callback);
13308 else
13309 /* pointer into the partial */
13310 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020013311 timer->tr_partial = partial;
13312 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013313 }
13314}
13315
13316/*
13317 * "timer_stop(timer)" function
13318 */
13319 static void
13320f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
13321{
13322 timer_T *timer;
13323
13324 if (argvars[0].v_type != VAR_NUMBER)
13325 {
13326 EMSG(_(e_number_exp));
13327 return;
13328 }
13329 timer = find_timer((int)get_tv_number(&argvars[0]));
13330 if (timer != NULL)
13331 stop_timer(timer);
13332}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013333
13334/*
13335 * "timer_stopall()" function
13336 */
13337 static void
13338f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13339{
13340 stop_all_timers();
13341}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013342#endif
13343
13344/*
13345 * "tolower(string)" function
13346 */
13347 static void
13348f_tolower(typval_T *argvars, typval_T *rettv)
13349{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013350 rettv->v_type = VAR_STRING;
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +010013351 rettv->vval.v_string = strlow_save(get_tv_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013352}
13353
13354/*
13355 * "toupper(string)" function
13356 */
13357 static void
13358f_toupper(typval_T *argvars, typval_T *rettv)
13359{
13360 rettv->v_type = VAR_STRING;
13361 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
13362}
13363
13364/*
13365 * "tr(string, fromstr, tostr)" function
13366 */
13367 static void
13368f_tr(typval_T *argvars, typval_T *rettv)
13369{
13370 char_u *in_str;
13371 char_u *fromstr;
13372 char_u *tostr;
13373 char_u *p;
13374#ifdef FEAT_MBYTE
13375 int inlen;
13376 int fromlen;
13377 int tolen;
13378 int idx;
13379 char_u *cpstr;
13380 int cplen;
13381 int first = TRUE;
13382#endif
13383 char_u buf[NUMBUFLEN];
13384 char_u buf2[NUMBUFLEN];
13385 garray_T ga;
13386
13387 in_str = get_tv_string(&argvars[0]);
13388 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
13389 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
13390
13391 /* Default return value: empty string. */
13392 rettv->v_type = VAR_STRING;
13393 rettv->vval.v_string = NULL;
13394 if (fromstr == NULL || tostr == NULL)
13395 return; /* type error; errmsg already given */
13396 ga_init2(&ga, (int)sizeof(char), 80);
13397
13398#ifdef FEAT_MBYTE
13399 if (!has_mbyte)
13400#endif
13401 /* not multi-byte: fromstr and tostr must be the same length */
13402 if (STRLEN(fromstr) != STRLEN(tostr))
13403 {
13404#ifdef FEAT_MBYTE
13405error:
13406#endif
13407 EMSG2(_(e_invarg2), fromstr);
13408 ga_clear(&ga);
13409 return;
13410 }
13411
13412 /* fromstr and tostr have to contain the same number of chars */
13413 while (*in_str != NUL)
13414 {
13415#ifdef FEAT_MBYTE
13416 if (has_mbyte)
13417 {
13418 inlen = (*mb_ptr2len)(in_str);
13419 cpstr = in_str;
13420 cplen = inlen;
13421 idx = 0;
13422 for (p = fromstr; *p != NUL; p += fromlen)
13423 {
13424 fromlen = (*mb_ptr2len)(p);
13425 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
13426 {
13427 for (p = tostr; *p != NUL; p += tolen)
13428 {
13429 tolen = (*mb_ptr2len)(p);
13430 if (idx-- == 0)
13431 {
13432 cplen = tolen;
13433 cpstr = p;
13434 break;
13435 }
13436 }
13437 if (*p == NUL) /* tostr is shorter than fromstr */
13438 goto error;
13439 break;
13440 }
13441 ++idx;
13442 }
13443
13444 if (first && cpstr == in_str)
13445 {
13446 /* Check that fromstr and tostr have the same number of
13447 * (multi-byte) characters. Done only once when a character
13448 * of in_str doesn't appear in fromstr. */
13449 first = FALSE;
13450 for (p = tostr; *p != NUL; p += tolen)
13451 {
13452 tolen = (*mb_ptr2len)(p);
13453 --idx;
13454 }
13455 if (idx != 0)
13456 goto error;
13457 }
13458
13459 (void)ga_grow(&ga, cplen);
13460 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
13461 ga.ga_len += cplen;
13462
13463 in_str += inlen;
13464 }
13465 else
13466#endif
13467 {
13468 /* When not using multi-byte chars we can do it faster. */
13469 p = vim_strchr(fromstr, *in_str);
13470 if (p != NULL)
13471 ga_append(&ga, tostr[p - fromstr]);
13472 else
13473 ga_append(&ga, *in_str);
13474 ++in_str;
13475 }
13476 }
13477
13478 /* add a terminating NUL */
13479 (void)ga_grow(&ga, 1);
13480 ga_append(&ga, NUL);
13481
13482 rettv->vval.v_string = ga.ga_data;
13483}
13484
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010013485/*
13486 * "trim({expr})" function
13487 */
13488 static void
13489f_trim(typval_T *argvars, typval_T *rettv)
13490{
13491 char_u buf1[NUMBUFLEN];
13492 char_u buf2[NUMBUFLEN];
13493 char_u *head = get_tv_string_buf_chk(&argvars[0], buf1);
13494 char_u *mask = NULL;
13495 char_u *tail;
13496 char_u *prev;
13497 char_u *p;
13498 int c1;
13499
13500 rettv->v_type = VAR_STRING;
13501 if (head == NULL)
13502 {
13503 rettv->vval.v_string = NULL;
13504 return;
13505 }
13506
13507 if (argvars[1].v_type == VAR_STRING)
13508 mask = get_tv_string_buf_chk(&argvars[1], buf2);
13509
13510 while (*head != NUL)
13511 {
13512 c1 = PTR2CHAR(head);
13513 if (mask == NULL)
13514 {
13515 if (c1 > ' ' && c1 != 0xa0)
13516 break;
13517 }
13518 else
13519 {
13520 for (p = mask; *p != NUL; MB_PTR_ADV(p))
13521 if (c1 == PTR2CHAR(p))
13522 break;
13523 if (*p == NUL)
13524 break;
13525 }
13526 MB_PTR_ADV(head);
13527 }
13528
13529 for (tail = head + STRLEN(head); tail > head; tail = prev)
13530 {
13531 prev = tail;
13532 MB_PTR_BACK(head, prev);
13533 c1 = PTR2CHAR(prev);
13534 if (mask == NULL)
13535 {
13536 if (c1 > ' ' && c1 != 0xa0)
13537 break;
13538 }
13539 else
13540 {
13541 for (p = mask; *p != NUL; MB_PTR_ADV(p))
13542 if (c1 == PTR2CHAR(p))
13543 break;
13544 if (*p == NUL)
13545 break;
13546 }
13547 }
13548 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
13549}
13550
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013551#ifdef FEAT_FLOAT
13552/*
13553 * "trunc({float})" function
13554 */
13555 static void
13556f_trunc(typval_T *argvars, typval_T *rettv)
13557{
13558 float_T f = 0.0;
13559
13560 rettv->v_type = VAR_FLOAT;
13561 if (get_float_arg(argvars, &f) == OK)
13562 /* trunc() is not in C90, use floor() or ceil() instead. */
13563 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
13564 else
13565 rettv->vval.v_float = 0.0;
13566}
13567#endif
13568
13569/*
13570 * "type(expr)" function
13571 */
13572 static void
13573f_type(typval_T *argvars, typval_T *rettv)
13574{
13575 int n = -1;
13576
13577 switch (argvars[0].v_type)
13578 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020013579 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
13580 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013581 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020013582 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
13583 case VAR_LIST: n = VAR_TYPE_LIST; break;
13584 case VAR_DICT: n = VAR_TYPE_DICT; break;
13585 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013586 case VAR_SPECIAL:
13587 if (argvars[0].vval.v_number == VVAL_FALSE
13588 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020013589 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013590 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020013591 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013592 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020013593 case VAR_JOB: n = VAR_TYPE_JOB; break;
13594 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013595 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010013596 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013597 n = -1;
13598 break;
13599 }
13600 rettv->vval.v_number = n;
13601}
13602
13603/*
13604 * "undofile(name)" function
13605 */
13606 static void
13607f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
13608{
13609 rettv->v_type = VAR_STRING;
13610#ifdef FEAT_PERSISTENT_UNDO
13611 {
13612 char_u *fname = get_tv_string(&argvars[0]);
13613
13614 if (*fname == NUL)
13615 {
13616 /* If there is no file name there will be no undo file. */
13617 rettv->vval.v_string = NULL;
13618 }
13619 else
13620 {
13621 char_u *ffname = FullName_save(fname, FALSE);
13622
13623 if (ffname != NULL)
13624 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
13625 vim_free(ffname);
13626 }
13627 }
13628#else
13629 rettv->vval.v_string = NULL;
13630#endif
13631}
13632
13633/*
13634 * "undotree()" function
13635 */
13636 static void
13637f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
13638{
13639 if (rettv_dict_alloc(rettv) == OK)
13640 {
13641 dict_T *dict = rettv->vval.v_dict;
13642 list_T *list;
13643
13644 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
13645 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
13646 dict_add_nr_str(dict, "save_last",
13647 (long)curbuf->b_u_save_nr_last, NULL);
13648 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
13649 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
13650 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
13651
13652 list = list_alloc();
13653 if (list != NULL)
13654 {
13655 u_eval_tree(curbuf->b_u_oldhead, list);
13656 dict_add_list(dict, "entries", list);
13657 }
13658 }
13659}
13660
13661/*
13662 * "values(dict)" function
13663 */
13664 static void
13665f_values(typval_T *argvars, typval_T *rettv)
13666{
13667 dict_list(argvars, rettv, 1);
13668}
13669
13670/*
13671 * "virtcol(string)" function
13672 */
13673 static void
13674f_virtcol(typval_T *argvars, typval_T *rettv)
13675{
13676 colnr_T vcol = 0;
13677 pos_T *fp;
13678 int fnum = curbuf->b_fnum;
13679
13680 fp = var2fpos(&argvars[0], FALSE, &fnum);
13681 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
13682 && fnum == curbuf->b_fnum)
13683 {
13684 getvvcol(curwin, fp, NULL, NULL, &vcol);
13685 ++vcol;
13686 }
13687
13688 rettv->vval.v_number = vcol;
13689}
13690
13691/*
13692 * "visualmode()" function
13693 */
13694 static void
13695f_visualmode(typval_T *argvars, typval_T *rettv)
13696{
13697 char_u str[2];
13698
13699 rettv->v_type = VAR_STRING;
13700 str[0] = curbuf->b_visual_mode_eval;
13701 str[1] = NUL;
13702 rettv->vval.v_string = vim_strsave(str);
13703
13704 /* A non-zero number or non-empty string argument: reset mode. */
13705 if (non_zero_arg(&argvars[0]))
13706 curbuf->b_visual_mode_eval = NUL;
13707}
13708
13709/*
13710 * "wildmenumode()" function
13711 */
13712 static void
13713f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13714{
13715#ifdef FEAT_WILDMENU
13716 if (wild_menu_showing)
13717 rettv->vval.v_number = 1;
13718#endif
13719}
13720
13721/*
13722 * "winbufnr(nr)" function
13723 */
13724 static void
13725f_winbufnr(typval_T *argvars, typval_T *rettv)
13726{
13727 win_T *wp;
13728
13729 wp = find_win_by_nr(&argvars[0], NULL);
13730 if (wp == NULL)
13731 rettv->vval.v_number = -1;
13732 else
13733 rettv->vval.v_number = wp->w_buffer->b_fnum;
13734}
13735
13736/*
13737 * "wincol()" function
13738 */
13739 static void
13740f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
13741{
13742 validate_cursor();
13743 rettv->vval.v_number = curwin->w_wcol + 1;
13744}
13745
13746/*
13747 * "winheight(nr)" function
13748 */
13749 static void
13750f_winheight(typval_T *argvars, typval_T *rettv)
13751{
13752 win_T *wp;
13753
13754 wp = find_win_by_nr(&argvars[0], NULL);
13755 if (wp == NULL)
13756 rettv->vval.v_number = -1;
13757 else
13758 rettv->vval.v_number = wp->w_height;
13759}
13760
13761/*
13762 * "winline()" function
13763 */
13764 static void
13765f_winline(typval_T *argvars UNUSED, typval_T *rettv)
13766{
13767 validate_cursor();
13768 rettv->vval.v_number = curwin->w_wrow + 1;
13769}
13770
13771/*
13772 * "winnr()" function
13773 */
13774 static void
13775f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
13776{
13777 int nr = 1;
13778
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013779 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013780 rettv->vval.v_number = nr;
13781}
13782
13783/*
13784 * "winrestcmd()" function
13785 */
13786 static void
13787f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
13788{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013789 win_T *wp;
13790 int winnr = 1;
13791 garray_T ga;
13792 char_u buf[50];
13793
13794 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020013795 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013796 {
13797 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
13798 ga_concat(&ga, buf);
13799 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
13800 ga_concat(&ga, buf);
13801 ++winnr;
13802 }
13803 ga_append(&ga, NUL);
13804
13805 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013806 rettv->v_type = VAR_STRING;
13807}
13808
13809/*
13810 * "winrestview()" function
13811 */
13812 static void
13813f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
13814{
13815 dict_T *dict;
13816
13817 if (argvars[0].v_type != VAR_DICT
13818 || (dict = argvars[0].vval.v_dict) == NULL)
13819 EMSG(_(e_invarg));
13820 else
13821 {
13822 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
13823 curwin->w_cursor.lnum = (linenr_T)get_dict_number(dict, (char_u *)"lnum");
13824 if (dict_find(dict, (char_u *)"col", -1) != NULL)
13825 curwin->w_cursor.col = (colnr_T)get_dict_number(dict, (char_u *)"col");
13826#ifdef FEAT_VIRTUALEDIT
13827 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
13828 curwin->w_cursor.coladd = (colnr_T)get_dict_number(dict, (char_u *)"coladd");
13829#endif
13830 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
13831 {
13832 curwin->w_curswant = (colnr_T)get_dict_number(dict, (char_u *)"curswant");
13833 curwin->w_set_curswant = FALSE;
13834 }
13835
13836 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
13837 set_topline(curwin, (linenr_T)get_dict_number(dict, (char_u *)"topline"));
13838#ifdef FEAT_DIFF
13839 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
13840 curwin->w_topfill = (int)get_dict_number(dict, (char_u *)"topfill");
13841#endif
13842 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
13843 curwin->w_leftcol = (colnr_T)get_dict_number(dict, (char_u *)"leftcol");
13844 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
13845 curwin->w_skipcol = (colnr_T)get_dict_number(dict, (char_u *)"skipcol");
13846
13847 check_cursor();
13848 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020013849 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013850 changed_window_setting();
13851
13852 if (curwin->w_topline <= 0)
13853 curwin->w_topline = 1;
13854 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
13855 curwin->w_topline = curbuf->b_ml.ml_line_count;
13856#ifdef FEAT_DIFF
13857 check_topfill(curwin, TRUE);
13858#endif
13859 }
13860}
13861
13862/*
13863 * "winsaveview()" function
13864 */
13865 static void
13866f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
13867{
13868 dict_T *dict;
13869
13870 if (rettv_dict_alloc(rettv) == FAIL)
13871 return;
13872 dict = rettv->vval.v_dict;
13873
13874 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
13875 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
13876#ifdef FEAT_VIRTUALEDIT
13877 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
13878#endif
13879 update_curswant();
13880 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
13881
13882 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
13883#ifdef FEAT_DIFF
13884 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
13885#endif
13886 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
13887 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
13888}
13889
13890/*
13891 * "winwidth(nr)" function
13892 */
13893 static void
13894f_winwidth(typval_T *argvars, typval_T *rettv)
13895{
13896 win_T *wp;
13897
13898 wp = find_win_by_nr(&argvars[0], NULL);
13899 if (wp == NULL)
13900 rettv->vval.v_number = -1;
13901 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013902 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013903}
13904
13905/*
13906 * "wordcount()" function
13907 */
13908 static void
13909f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
13910{
13911 if (rettv_dict_alloc(rettv) == FAIL)
13912 return;
13913 cursor_pos_info(rettv->vval.v_dict);
13914}
13915
13916/*
13917 * "writefile()" function
13918 */
13919 static void
13920f_writefile(typval_T *argvars, typval_T *rettv)
13921{
13922 int binary = FALSE;
13923 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013924#ifdef HAVE_FSYNC
13925 int do_fsync = p_fs;
13926#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013927 char_u *fname;
13928 FILE *fd;
13929 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013930 listitem_T *li;
13931 list_T *list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013932
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013933 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013934 if (check_restricted() || check_secure())
13935 return;
13936
13937 if (argvars[0].v_type != VAR_LIST)
13938 {
13939 EMSG2(_(e_listarg), "writefile()");
13940 return;
13941 }
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013942 list = argvars[0].vval.v_list;
13943 if (list == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013944 return;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013945 for (li = list->lv_first; li != NULL; li = li->li_next)
13946 if (get_tv_string_chk(&li->li_tv) == NULL)
13947 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013948
13949 if (argvars[2].v_type != VAR_UNKNOWN)
13950 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013951 char_u *arg2 = get_tv_string_chk(&argvars[2]);
13952
13953 if (arg2 == NULL)
13954 return;
13955 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013956 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013957 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013958 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013959#ifdef HAVE_FSYNC
13960 if (vim_strchr(arg2, 's') != NULL)
13961 do_fsync = TRUE;
13962 else if (vim_strchr(arg2, 'S') != NULL)
13963 do_fsync = FALSE;
13964#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013965 }
13966
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013967 fname = get_tv_string_chk(&argvars[1]);
13968 if (fname == NULL)
13969 return;
13970
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013971 /* Always open the file in binary mode, library functions have a mind of
13972 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013973 if (*fname == NUL || (fd = mch_fopen((char *)fname,
13974 append ? APPENDBIN : WRITEBIN)) == NULL)
13975 {
13976 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
13977 ret = -1;
13978 }
13979 else
13980 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013981 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013982 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013983#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010013984 else if (do_fsync)
13985 /* Ignore the error, the user wouldn't know what to do about it.
13986 * May happen for a device. */
13987 ignored = fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013988#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013989 fclose(fd);
13990 }
13991
13992 rettv->vval.v_number = ret;
13993}
13994
13995/*
13996 * "xor(expr, expr)" function
13997 */
13998 static void
13999f_xor(typval_T *argvars, typval_T *rettv)
14000{
14001 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14002 ^ get_tv_number_chk(&argvars[1], NULL);
14003}
14004
14005
14006#endif /* FEAT_EVAL */