blob: 7d0f33aaa02ebef595de0262b549ceef3e0f18eb [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 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02004341 dict_add_number(d, "id", sign->id);
4342 dict_add_number(d, "lnum", sign->lnum);
4343 dict_add_string(d, "name", 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 Moolenaare0be1672018-07-08 16:50:37 +02004366 dict_add_number(dict, "bufnr", buf->b_fnum);
4367 dict_add_string(dict, "name", buf->b_ffname);
4368 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4369 : buflist_findlnum(buf));
4370 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4371 dict_add_number(dict, "listed", buf->b_p_bl);
4372 dict_add_number(dict, "changed", bufIsChanged(buf));
4373 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4374 dict_add_number(dict, "hidden",
4375 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004376
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004377 /* Get a reference to buffer variables */
4378 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004379
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004380 /* List of windows displaying this buffer */
4381 windows = list_alloc();
4382 if (windows != NULL)
4383 {
4384 FOR_ALL_TAB_WINDOWS(tp, wp)
4385 if (wp->w_buffer == buf)
4386 list_append_number(windows, (varnumber_T)wp->w_id);
4387 dict_add_list(dict, "windows", windows);
4388 }
4389
4390#ifdef FEAT_SIGNS
4391 if (buf->b_signlist != NULL)
4392 {
4393 /* List of signs placed in this buffer */
4394 list_T *signs = list_alloc();
4395 if (signs != NULL)
4396 {
4397 get_buffer_signs(buf, signs);
4398 dict_add_list(dict, "signs", signs);
4399 }
4400 }
4401#endif
4402
4403 return dict;
4404}
4405
4406/*
4407 * "getbufinfo()" function
4408 */
4409 static void
4410f_getbufinfo(typval_T *argvars, typval_T *rettv)
4411{
4412 buf_T *buf = NULL;
4413 buf_T *argbuf = NULL;
4414 dict_T *d;
4415 int filtered = FALSE;
4416 int sel_buflisted = FALSE;
4417 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004418 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004419
4420 if (rettv_list_alloc(rettv) != OK)
4421 return;
4422
4423 /* List of all the buffers or selected buffers */
4424 if (argvars[0].v_type == VAR_DICT)
4425 {
4426 dict_T *sel_d = argvars[0].vval.v_dict;
4427
4428 if (sel_d != NULL)
4429 {
4430 dictitem_T *di;
4431
4432 filtered = TRUE;
4433
4434 di = dict_find(sel_d, (char_u *)"buflisted", -1);
4435 if (di != NULL && get_tv_number(&di->di_tv))
4436 sel_buflisted = TRUE;
4437
4438 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
4439 if (di != NULL && get_tv_number(&di->di_tv))
4440 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004441
4442 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
4443 if (di != NULL && get_tv_number(&di->di_tv))
4444 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004445 }
4446 }
4447 else if (argvars[0].v_type != VAR_UNKNOWN)
4448 {
4449 /* Information about one buffer. Argument specifies the buffer */
4450 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4451 ++emsg_off;
4452 argbuf = get_buf_tv(&argvars[0], FALSE);
4453 --emsg_off;
4454 if (argbuf == NULL)
4455 return;
4456 }
4457
4458 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004459 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004460 {
4461 if (argbuf != NULL && argbuf != buf)
4462 continue;
4463 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004464 || (sel_buflisted && !buf->b_p_bl)
4465 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004466 continue;
4467
4468 d = get_buffer_info(buf);
4469 if (d != NULL)
4470 list_append_dict(rettv->vval.v_list, d);
4471 if (argbuf != NULL)
4472 return;
4473 }
4474}
4475
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004476static void get_buffer_lines(buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv);
4477
4478/*
4479 * Get line or list of lines from buffer "buf" into "rettv".
4480 * Return a range (from start to end) of lines in rettv from the specified
4481 * buffer.
4482 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4483 */
4484 static void
4485get_buffer_lines(
4486 buf_T *buf,
4487 linenr_T start,
4488 linenr_T end,
4489 int retlist,
4490 typval_T *rettv)
4491{
4492 char_u *p;
4493
4494 rettv->v_type = VAR_STRING;
4495 rettv->vval.v_string = NULL;
4496 if (retlist && rettv_list_alloc(rettv) == FAIL)
4497 return;
4498
4499 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4500 return;
4501
4502 if (!retlist)
4503 {
4504 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4505 p = ml_get_buf(buf, start, FALSE);
4506 else
4507 p = (char_u *)"";
4508 rettv->vval.v_string = vim_strsave(p);
4509 }
4510 else
4511 {
4512 if (end < start)
4513 return;
4514
4515 if (start < 1)
4516 start = 1;
4517 if (end > buf->b_ml.ml_line_count)
4518 end = buf->b_ml.ml_line_count;
4519 while (start <= end)
4520 if (list_append_string(rettv->vval.v_list,
4521 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4522 break;
4523 }
4524}
4525
4526/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004527 * "getbufline()" function
4528 */
4529 static void
4530f_getbufline(typval_T *argvars, typval_T *rettv)
4531{
4532 linenr_T lnum;
4533 linenr_T end;
4534 buf_T *buf;
4535
4536 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4537 ++emsg_off;
4538 buf = get_buf_tv(&argvars[0], FALSE);
4539 --emsg_off;
4540
4541 lnum = get_tv_lnum_buf(&argvars[1], buf);
4542 if (argvars[2].v_type == VAR_UNKNOWN)
4543 end = lnum;
4544 else
4545 end = get_tv_lnum_buf(&argvars[2], buf);
4546
4547 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4548}
4549
4550/*
4551 * "getbufvar()" function
4552 */
4553 static void
4554f_getbufvar(typval_T *argvars, typval_T *rettv)
4555{
4556 buf_T *buf;
4557 buf_T *save_curbuf;
4558 char_u *varname;
4559 dictitem_T *v;
4560 int done = FALSE;
4561
4562 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4563 varname = get_tv_string_chk(&argvars[1]);
4564 ++emsg_off;
4565 buf = get_buf_tv(&argvars[0], FALSE);
4566
4567 rettv->v_type = VAR_STRING;
4568 rettv->vval.v_string = NULL;
4569
4570 if (buf != NULL && varname != NULL)
4571 {
4572 /* set curbuf to be our buf, temporarily */
4573 save_curbuf = curbuf;
4574 curbuf = buf;
4575
Bram Moolenaar30567352016-08-27 21:25:44 +02004576 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004577 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004578 if (varname[1] == NUL)
4579 {
4580 /* get all buffer-local options in a dict */
4581 dict_T *opts = get_winbuf_options(TRUE);
4582
4583 if (opts != NULL)
4584 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004585 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004586 done = TRUE;
4587 }
4588 }
4589 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4590 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004591 done = TRUE;
4592 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004593 else
4594 {
4595 /* Look up the variable. */
4596 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4597 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4598 'b', varname, FALSE);
4599 if (v != NULL)
4600 {
4601 copy_tv(&v->di_tv, rettv);
4602 done = TRUE;
4603 }
4604 }
4605
4606 /* restore previous notion of curbuf */
4607 curbuf = save_curbuf;
4608 }
4609
4610 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4611 /* use the default value */
4612 copy_tv(&argvars[2], rettv);
4613
4614 --emsg_off;
4615}
4616
4617/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004618 * "getchangelist()" function
4619 */
4620 static void
4621f_getchangelist(typval_T *argvars, typval_T *rettv)
4622{
4623#ifdef FEAT_JUMPLIST
4624 buf_T *buf;
4625 int i;
4626 list_T *l;
4627 dict_T *d;
4628#endif
4629
4630 if (rettv_list_alloc(rettv) != OK)
4631 return;
4632
4633#ifdef FEAT_JUMPLIST
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004634 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4635 ++emsg_off;
4636 buf = get_buf_tv(&argvars[0], FALSE);
4637 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004638 if (buf == NULL)
4639 return;
4640
4641 l = list_alloc();
4642 if (l == NULL)
4643 return;
4644
4645 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4646 return;
4647 /*
4648 * The current window change list index tracks only the position in the
4649 * current buffer change list. For other buffers, use the change list
4650 * length as the current index.
4651 */
4652 list_append_number(rettv->vval.v_list,
4653 (varnumber_T)((buf == curwin->w_buffer)
4654 ? curwin->w_changelistidx : buf->b_changelistlen));
4655
4656 for (i = 0; i < buf->b_changelistlen; ++i)
4657 {
4658 if (buf->b_changelist[i].lnum == 0)
4659 continue;
4660 if ((d = dict_alloc()) == NULL)
4661 return;
4662 if (list_append_dict(l, d) == FAIL)
4663 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004664 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4665 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004666# ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +02004667 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004668# endif
4669 }
4670#endif
4671}
4672/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004673 * "getchar()" function
4674 */
4675 static void
4676f_getchar(typval_T *argvars, typval_T *rettv)
4677{
4678 varnumber_T n;
4679 int error = FALSE;
4680
4681 /* Position the cursor. Needed after a message that ends in a space. */
4682 windgoto(msg_row, msg_col);
4683
4684 ++no_mapping;
4685 ++allow_keys;
4686 for (;;)
4687 {
4688 if (argvars[0].v_type == VAR_UNKNOWN)
4689 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004690 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004691 else if (get_tv_number_chk(&argvars[0], &error) == 1)
4692 /* getchar(1): only check if char avail */
4693 n = vpeekc_any();
4694 else if (error || vpeekc_any() == NUL)
4695 /* illegal argument or getchar(0) and no char avail: return zero */
4696 n = 0;
4697 else
4698 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004699 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004700
4701 if (n == K_IGNORE)
4702 continue;
4703 break;
4704 }
4705 --no_mapping;
4706 --allow_keys;
4707
4708 set_vim_var_nr(VV_MOUSE_WIN, 0);
4709 set_vim_var_nr(VV_MOUSE_WINID, 0);
4710 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4711 set_vim_var_nr(VV_MOUSE_COL, 0);
4712
4713 rettv->vval.v_number = n;
4714 if (IS_SPECIAL(n) || mod_mask != 0)
4715 {
4716 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4717 int i = 0;
4718
4719 /* Turn a special key into three bytes, plus modifier. */
4720 if (mod_mask != 0)
4721 {
4722 temp[i++] = K_SPECIAL;
4723 temp[i++] = KS_MODIFIER;
4724 temp[i++] = mod_mask;
4725 }
4726 if (IS_SPECIAL(n))
4727 {
4728 temp[i++] = K_SPECIAL;
4729 temp[i++] = K_SECOND(n);
4730 temp[i++] = K_THIRD(n);
4731 }
4732#ifdef FEAT_MBYTE
4733 else if (has_mbyte)
4734 i += (*mb_char2bytes)(n, temp + i);
4735#endif
4736 else
4737 temp[i++] = n;
4738 temp[i++] = NUL;
4739 rettv->v_type = VAR_STRING;
4740 rettv->vval.v_string = vim_strsave(temp);
4741
4742#ifdef FEAT_MOUSE
4743 if (is_mouse_key(n))
4744 {
4745 int row = mouse_row;
4746 int col = mouse_col;
4747 win_T *win;
4748 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004749 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004750 int winnr = 1;
4751
4752 if (row >= 0 && col >= 0)
4753 {
4754 /* Find the window at the mouse coordinates and compute the
4755 * text position. */
4756 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004757 if (win == NULL)
4758 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004759 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004760 for (wp = firstwin; wp != win; wp = wp->w_next)
4761 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004762 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4763 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4764 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4765 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4766 }
4767 }
4768#endif
4769 }
4770}
4771
4772/*
4773 * "getcharmod()" function
4774 */
4775 static void
4776f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4777{
4778 rettv->vval.v_number = mod_mask;
4779}
4780
4781/*
4782 * "getcharsearch()" function
4783 */
4784 static void
4785f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4786{
4787 if (rettv_dict_alloc(rettv) != FAIL)
4788 {
4789 dict_T *dict = rettv->vval.v_dict;
4790
Bram Moolenaare0be1672018-07-08 16:50:37 +02004791 dict_add_string(dict, "char", last_csearch());
4792 dict_add_number(dict, "forward", last_csearch_forward());
4793 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004794 }
4795}
4796
4797/*
4798 * "getcmdline()" function
4799 */
4800 static void
4801f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4802{
4803 rettv->v_type = VAR_STRING;
4804 rettv->vval.v_string = get_cmdline_str();
4805}
4806
4807/*
4808 * "getcmdpos()" function
4809 */
4810 static void
4811f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4812{
4813 rettv->vval.v_number = get_cmdline_pos() + 1;
4814}
4815
4816/*
4817 * "getcmdtype()" function
4818 */
4819 static void
4820f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4821{
4822 rettv->v_type = VAR_STRING;
4823 rettv->vval.v_string = alloc(2);
4824 if (rettv->vval.v_string != NULL)
4825 {
4826 rettv->vval.v_string[0] = get_cmdline_type();
4827 rettv->vval.v_string[1] = NUL;
4828 }
4829}
4830
4831/*
4832 * "getcmdwintype()" function
4833 */
4834 static void
4835f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4836{
4837 rettv->v_type = VAR_STRING;
4838 rettv->vval.v_string = NULL;
4839#ifdef FEAT_CMDWIN
4840 rettv->vval.v_string = alloc(2);
4841 if (rettv->vval.v_string != NULL)
4842 {
4843 rettv->vval.v_string[0] = cmdwin_type;
4844 rettv->vval.v_string[1] = NUL;
4845 }
4846#endif
4847}
4848
4849#if defined(FEAT_CMDL_COMPL)
4850/*
4851 * "getcompletion()" function
4852 */
4853 static void
4854f_getcompletion(typval_T *argvars, typval_T *rettv)
4855{
4856 char_u *pat;
4857 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004858 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004859 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
4860 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004861
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004862 if (argvars[2].v_type != VAR_UNKNOWN)
4863 filtered = get_tv_number_chk(&argvars[2], NULL);
4864
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004865 if (p_wic)
4866 options |= WILD_ICASE;
4867
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004868 /* For filtered results, 'wildignore' is used */
4869 if (!filtered)
4870 options |= WILD_KEEP_ALL;
4871
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004872 ExpandInit(&xpc);
4873 xpc.xp_pattern = get_tv_string(&argvars[0]);
4874 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4875 xpc.xp_context = cmdcomplete_str_to_type(get_tv_string(&argvars[1]));
4876 if (xpc.xp_context == EXPAND_NOTHING)
4877 {
4878 if (argvars[1].v_type == VAR_STRING)
4879 EMSG2(_(e_invarg2), argvars[1].vval.v_string);
4880 else
4881 EMSG(_(e_invarg));
4882 return;
4883 }
4884
4885# if defined(FEAT_MENU)
4886 if (xpc.xp_context == EXPAND_MENUS)
4887 {
4888 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4889 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4890 }
4891# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02004892#ifdef FEAT_CSCOPE
4893 if (xpc.xp_context == EXPAND_CSCOPE)
4894 {
4895 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4896 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4897 }
4898#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02004899#ifdef FEAT_SIGNS
4900 if (xpc.xp_context == EXPAND_SIGN)
4901 {
4902 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4903 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4904 }
4905#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004906
4907 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4908 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
4909 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004910 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004911
4912 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4913
4914 for (i = 0; i < xpc.xp_numfiles; i++)
4915 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4916 }
4917 vim_free(pat);
4918 ExpandCleanup(&xpc);
4919}
4920#endif
4921
4922/*
4923 * "getcwd()" function
4924 */
4925 static void
4926f_getcwd(typval_T *argvars, typval_T *rettv)
4927{
4928 win_T *wp = NULL;
4929 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01004930 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004931
4932 rettv->v_type = VAR_STRING;
4933 rettv->vval.v_string = NULL;
4934
Bram Moolenaar54591292018-02-09 20:53:59 +01004935 if (argvars[0].v_type == VAR_NUMBER && argvars[0].vval.v_number == -1)
4936 global = TRUE;
4937 else
4938 wp = find_tabwin(&argvars[0], &argvars[1]);
4939
4940 if (wp != NULL && wp->w_localdir != NULL)
4941 rettv->vval.v_string = vim_strsave(wp->w_localdir);
4942 else if (wp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004943 {
Bram Moolenaar54591292018-02-09 20:53:59 +01004944 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004945 rettv->vval.v_string = vim_strsave(globaldir);
4946 else
4947 {
4948 cwd = alloc(MAXPATHL);
4949 if (cwd != NULL)
4950 {
4951 if (mch_dirname(cwd, MAXPATHL) != FAIL)
4952 rettv->vval.v_string = vim_strsave(cwd);
4953 vim_free(cwd);
4954 }
4955 }
4956#ifdef BACKSLASH_IN_FILENAME
4957 if (rettv->vval.v_string != NULL)
4958 slash_adjust(rettv->vval.v_string);
4959#endif
4960 }
4961}
4962
4963/*
4964 * "getfontname()" function
4965 */
4966 static void
4967f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
4968{
4969 rettv->v_type = VAR_STRING;
4970 rettv->vval.v_string = NULL;
4971#ifdef FEAT_GUI
4972 if (gui.in_use)
4973 {
4974 GuiFont font;
4975 char_u *name = NULL;
4976
4977 if (argvars[0].v_type == VAR_UNKNOWN)
4978 {
4979 /* Get the "Normal" font. Either the name saved by
4980 * hl_set_font_name() or from the font ID. */
4981 font = gui.norm_font;
4982 name = hl_get_font_name();
4983 }
4984 else
4985 {
4986 name = get_tv_string(&argvars[0]);
4987 if (STRCMP(name, "*") == 0) /* don't use font dialog */
4988 return;
4989 font = gui_mch_get_font(name, FALSE);
4990 if (font == NOFONT)
4991 return; /* Invalid font name, return empty string. */
4992 }
4993 rettv->vval.v_string = gui_mch_get_fontname(font, name);
4994 if (argvars[0].v_type != VAR_UNKNOWN)
4995 gui_mch_free_font(font);
4996 }
4997#endif
4998}
4999
5000/*
5001 * "getfperm({fname})" function
5002 */
5003 static void
5004f_getfperm(typval_T *argvars, typval_T *rettv)
5005{
5006 char_u *fname;
5007 stat_T st;
5008 char_u *perm = NULL;
5009 char_u flags[] = "rwx";
5010 int i;
5011
5012 fname = get_tv_string(&argvars[0]);
5013
5014 rettv->v_type = VAR_STRING;
5015 if (mch_stat((char *)fname, &st) >= 0)
5016 {
5017 perm = vim_strsave((char_u *)"---------");
5018 if (perm != NULL)
5019 {
5020 for (i = 0; i < 9; i++)
5021 {
5022 if (st.st_mode & (1 << (8 - i)))
5023 perm[i] = flags[i % 3];
5024 }
5025 }
5026 }
5027 rettv->vval.v_string = perm;
5028}
5029
5030/*
5031 * "getfsize({fname})" function
5032 */
5033 static void
5034f_getfsize(typval_T *argvars, typval_T *rettv)
5035{
5036 char_u *fname;
5037 stat_T st;
5038
5039 fname = get_tv_string(&argvars[0]);
5040
5041 rettv->v_type = VAR_NUMBER;
5042
5043 if (mch_stat((char *)fname, &st) >= 0)
5044 {
5045 if (mch_isdir(fname))
5046 rettv->vval.v_number = 0;
5047 else
5048 {
5049 rettv->vval.v_number = (varnumber_T)st.st_size;
5050
5051 /* non-perfect check for overflow */
5052 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5053 rettv->vval.v_number = -2;
5054 }
5055 }
5056 else
5057 rettv->vval.v_number = -1;
5058}
5059
5060/*
5061 * "getftime({fname})" function
5062 */
5063 static void
5064f_getftime(typval_T *argvars, typval_T *rettv)
5065{
5066 char_u *fname;
5067 stat_T st;
5068
5069 fname = get_tv_string(&argvars[0]);
5070
5071 if (mch_stat((char *)fname, &st) >= 0)
5072 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5073 else
5074 rettv->vval.v_number = -1;
5075}
5076
5077/*
5078 * "getftype({fname})" function
5079 */
5080 static void
5081f_getftype(typval_T *argvars, typval_T *rettv)
5082{
5083 char_u *fname;
5084 stat_T st;
5085 char_u *type = NULL;
5086 char *t;
5087
5088 fname = get_tv_string(&argvars[0]);
5089
5090 rettv->v_type = VAR_STRING;
5091 if (mch_lstat((char *)fname, &st) >= 0)
5092 {
5093#ifdef S_ISREG
5094 if (S_ISREG(st.st_mode))
5095 t = "file";
5096 else if (S_ISDIR(st.st_mode))
5097 t = "dir";
5098# ifdef S_ISLNK
5099 else if (S_ISLNK(st.st_mode))
5100 t = "link";
5101# endif
5102# ifdef S_ISBLK
5103 else if (S_ISBLK(st.st_mode))
5104 t = "bdev";
5105# endif
5106# ifdef S_ISCHR
5107 else if (S_ISCHR(st.st_mode))
5108 t = "cdev";
5109# endif
5110# ifdef S_ISFIFO
5111 else if (S_ISFIFO(st.st_mode))
5112 t = "fifo";
5113# endif
5114# ifdef S_ISSOCK
5115 else if (S_ISSOCK(st.st_mode))
5116 t = "fifo";
5117# endif
5118 else
5119 t = "other";
5120#else
5121# ifdef S_IFMT
5122 switch (st.st_mode & S_IFMT)
5123 {
5124 case S_IFREG: t = "file"; break;
5125 case S_IFDIR: t = "dir"; break;
5126# ifdef S_IFLNK
5127 case S_IFLNK: t = "link"; break;
5128# endif
5129# ifdef S_IFBLK
5130 case S_IFBLK: t = "bdev"; break;
5131# endif
5132# ifdef S_IFCHR
5133 case S_IFCHR: t = "cdev"; break;
5134# endif
5135# ifdef S_IFIFO
5136 case S_IFIFO: t = "fifo"; break;
5137# endif
5138# ifdef S_IFSOCK
5139 case S_IFSOCK: t = "socket"; break;
5140# endif
5141 default: t = "other";
5142 }
5143# else
5144 if (mch_isdir(fname))
5145 t = "dir";
5146 else
5147 t = "file";
5148# endif
5149#endif
5150 type = vim_strsave((char_u *)t);
5151 }
5152 rettv->vval.v_string = type;
5153}
5154
5155/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005156 * "getjumplist()" function
5157 */
5158 static void
5159f_getjumplist(typval_T *argvars, typval_T *rettv)
5160{
5161#ifdef FEAT_JUMPLIST
5162 win_T *wp;
5163 int i;
5164 list_T *l;
5165 dict_T *d;
5166#endif
5167
5168 if (rettv_list_alloc(rettv) != OK)
5169 return;
5170
5171#ifdef FEAT_JUMPLIST
5172 wp = find_tabwin(&argvars[0], &argvars[1]);
5173 if (wp == NULL)
5174 return;
5175
5176 l = list_alloc();
5177 if (l == NULL)
5178 return;
5179
5180 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5181 return;
5182 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5183
Bram Moolenaar48679742018-02-13 13:33:29 +01005184 cleanup_jumplist(wp, TRUE);
5185
Bram Moolenaar4f505882018-02-10 21:06:32 +01005186 for (i = 0; i < wp->w_jumplistlen; ++i)
5187 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005188 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5189 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005190 if ((d = dict_alloc()) == NULL)
5191 return;
5192 if (list_append_dict(l, d) == FAIL)
5193 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005194 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5195 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005196# ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +02005197 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005198# endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005199 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005200 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005201 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005202 }
5203#endif
5204}
5205
5206/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005207 * "getline(lnum, [end])" function
5208 */
5209 static void
5210f_getline(typval_T *argvars, typval_T *rettv)
5211{
5212 linenr_T lnum;
5213 linenr_T end;
5214 int retlist;
5215
5216 lnum = get_tv_lnum(argvars);
5217 if (argvars[1].v_type == VAR_UNKNOWN)
5218 {
5219 end = 0;
5220 retlist = FALSE;
5221 }
5222 else
5223 {
5224 end = get_tv_lnum(&argvars[1]);
5225 retlist = TRUE;
5226 }
5227
5228 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5229}
5230
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005231#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005232 static void
5233get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5234{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005235 if (what_arg->v_type == VAR_UNKNOWN)
5236 {
5237 if (rettv_list_alloc(rettv) == OK)
5238 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005239 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005240 }
5241 else
5242 {
5243 if (rettv_dict_alloc(rettv) == OK)
5244 if (is_qf || (wp != NULL))
5245 {
5246 if (what_arg->v_type == VAR_DICT)
5247 {
5248 dict_T *d = what_arg->vval.v_dict;
5249
5250 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005251 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005252 }
5253 else
5254 EMSG(_(e_dictreq));
5255 }
5256 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005257}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005258#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005259
5260/*
5261 * "getloclist()" function
5262 */
5263 static void
5264f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5265{
5266#ifdef FEAT_QUICKFIX
5267 win_T *wp;
5268
5269 wp = find_win_by_nr(&argvars[0], NULL);
5270 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5271#endif
5272}
5273
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005274/*
5275 * "getmatches()" function
5276 */
5277 static void
5278f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5279{
5280#ifdef FEAT_SEARCH_EXTRA
5281 dict_T *dict;
5282 matchitem_T *cur = curwin->w_match_head;
5283 int i;
5284
5285 if (rettv_list_alloc(rettv) == OK)
5286 {
5287 while (cur != NULL)
5288 {
5289 dict = dict_alloc();
5290 if (dict == NULL)
5291 return;
5292 if (cur->match.regprog == NULL)
5293 {
5294 /* match added with matchaddpos() */
5295 for (i = 0; i < MAXPOSMATCH; ++i)
5296 {
5297 llpos_T *llpos;
5298 char buf[6];
5299 list_T *l;
5300
5301 llpos = &cur->pos.pos[i];
5302 if (llpos->lnum == 0)
5303 break;
5304 l = list_alloc();
5305 if (l == NULL)
5306 break;
5307 list_append_number(l, (varnumber_T)llpos->lnum);
5308 if (llpos->col > 0)
5309 {
5310 list_append_number(l, (varnumber_T)llpos->col);
5311 list_append_number(l, (varnumber_T)llpos->len);
5312 }
5313 sprintf(buf, "pos%d", i + 1);
5314 dict_add_list(dict, buf, l);
5315 }
5316 }
5317 else
5318 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02005319 dict_add_string(dict, "pattern", cur->pattern);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005320 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02005321 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
5322 dict_add_number(dict, "priority", (long)cur->priority);
5323 dict_add_number(dict, "id", (long)cur->id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005324# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
5325 if (cur->conceal_char)
5326 {
5327 char_u buf[MB_MAXBYTES + 1];
5328
5329 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005330 dict_add_string(dict, "conceal", (char_u *)&buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005331 }
5332# endif
5333 list_append_dict(rettv->vval.v_list, dict);
5334 cur = cur->next;
5335 }
5336 }
5337#endif
5338}
5339
5340/*
5341 * "getpid()" function
5342 */
5343 static void
5344f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5345{
5346 rettv->vval.v_number = mch_get_pid();
5347}
5348
5349 static void
5350getpos_both(
5351 typval_T *argvars,
5352 typval_T *rettv,
5353 int getcurpos)
5354{
5355 pos_T *fp;
5356 list_T *l;
5357 int fnum = -1;
5358
5359 if (rettv_list_alloc(rettv) == OK)
5360 {
5361 l = rettv->vval.v_list;
5362 if (getcurpos)
5363 fp = &curwin->w_cursor;
5364 else
5365 fp = var2fpos(&argvars[0], TRUE, &fnum);
5366 if (fnum != -1)
5367 list_append_number(l, (varnumber_T)fnum);
5368 else
5369 list_append_number(l, (varnumber_T)0);
5370 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5371 : (varnumber_T)0);
5372 list_append_number(l, (fp != NULL)
5373 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5374 : (varnumber_T)0);
5375 list_append_number(l,
5376#ifdef FEAT_VIRTUALEDIT
5377 (fp != NULL) ? (varnumber_T)fp->coladd :
5378#endif
5379 (varnumber_T)0);
5380 if (getcurpos)
5381 {
5382 update_curswant();
5383 list_append_number(l, curwin->w_curswant == MAXCOL ?
5384 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
5385 }
5386 }
5387 else
5388 rettv->vval.v_number = FALSE;
5389}
5390
5391
5392/*
5393 * "getcurpos()" function
5394 */
5395 static void
5396f_getcurpos(typval_T *argvars, typval_T *rettv)
5397{
5398 getpos_both(argvars, rettv, TRUE);
5399}
5400
5401/*
5402 * "getpos(string)" function
5403 */
5404 static void
5405f_getpos(typval_T *argvars, typval_T *rettv)
5406{
5407 getpos_both(argvars, rettv, FALSE);
5408}
5409
5410/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005411 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005412 */
5413 static void
5414f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5415{
5416#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005417 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005418#endif
5419}
5420
5421/*
5422 * "getreg()" function
5423 */
5424 static void
5425f_getreg(typval_T *argvars, typval_T *rettv)
5426{
5427 char_u *strregname;
5428 int regname;
5429 int arg2 = FALSE;
5430 int return_list = FALSE;
5431 int error = FALSE;
5432
5433 if (argvars[0].v_type != VAR_UNKNOWN)
5434 {
5435 strregname = get_tv_string_chk(&argvars[0]);
5436 error = strregname == NULL;
5437 if (argvars[1].v_type != VAR_UNKNOWN)
5438 {
5439 arg2 = (int)get_tv_number_chk(&argvars[1], &error);
5440 if (!error && argvars[2].v_type != VAR_UNKNOWN)
5441 return_list = (int)get_tv_number_chk(&argvars[2], &error);
5442 }
5443 }
5444 else
5445 strregname = get_vim_var_str(VV_REG);
5446
5447 if (error)
5448 return;
5449
5450 regname = (strregname == NULL ? '"' : *strregname);
5451 if (regname == 0)
5452 regname = '"';
5453
5454 if (return_list)
5455 {
5456 rettv->v_type = VAR_LIST;
5457 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5458 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5459 if (rettv->vval.v_list == NULL)
5460 (void)rettv_list_alloc(rettv);
5461 else
5462 ++rettv->vval.v_list->lv_refcount;
5463 }
5464 else
5465 {
5466 rettv->v_type = VAR_STRING;
5467 rettv->vval.v_string = get_reg_contents(regname,
5468 arg2 ? GREG_EXPR_SRC : 0);
5469 }
5470}
5471
5472/*
5473 * "getregtype()" function
5474 */
5475 static void
5476f_getregtype(typval_T *argvars, typval_T *rettv)
5477{
5478 char_u *strregname;
5479 int regname;
5480 char_u buf[NUMBUFLEN + 2];
5481 long reglen = 0;
5482
5483 if (argvars[0].v_type != VAR_UNKNOWN)
5484 {
5485 strregname = get_tv_string_chk(&argvars[0]);
5486 if (strregname == NULL) /* type error; errmsg already given */
5487 {
5488 rettv->v_type = VAR_STRING;
5489 rettv->vval.v_string = NULL;
5490 return;
5491 }
5492 }
5493 else
5494 /* Default to v:register */
5495 strregname = get_vim_var_str(VV_REG);
5496
5497 regname = (strregname == NULL ? '"' : *strregname);
5498 if (regname == 0)
5499 regname = '"';
5500
5501 buf[0] = NUL;
5502 buf[1] = NUL;
5503 switch (get_reg_type(regname, &reglen))
5504 {
5505 case MLINE: buf[0] = 'V'; break;
5506 case MCHAR: buf[0] = 'v'; break;
5507 case MBLOCK:
5508 buf[0] = Ctrl_V;
5509 sprintf((char *)buf + 1, "%ld", reglen + 1);
5510 break;
5511 }
5512 rettv->v_type = VAR_STRING;
5513 rettv->vval.v_string = vim_strsave(buf);
5514}
5515
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005516/*
5517 * Returns information (variables, options, etc.) about a tab page
5518 * as a dictionary.
5519 */
5520 static dict_T *
5521get_tabpage_info(tabpage_T *tp, int tp_idx)
5522{
5523 win_T *wp;
5524 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005525 list_T *l;
5526
5527 dict = dict_alloc();
5528 if (dict == NULL)
5529 return NULL;
5530
Bram Moolenaare0be1672018-07-08 16:50:37 +02005531 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005532
5533 l = list_alloc();
5534 if (l != NULL)
5535 {
5536 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5537 wp; wp = wp->w_next)
5538 list_append_number(l, (varnumber_T)wp->w_id);
5539 dict_add_list(dict, "windows", l);
5540 }
5541
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005542 /* Make a reference to tabpage variables */
5543 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005544
5545 return dict;
5546}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005547
5548/*
5549 * "gettabinfo()" function
5550 */
5551 static void
5552f_gettabinfo(typval_T *argvars, typval_T *rettv)
5553{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005554 tabpage_T *tp, *tparg = NULL;
5555 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005556 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005557
5558 if (rettv_list_alloc(rettv) != OK)
5559 return;
5560
5561 if (argvars[0].v_type != VAR_UNKNOWN)
5562 {
5563 /* Information about one tab page */
5564 tparg = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5565 if (tparg == NULL)
5566 return;
5567 }
5568
5569 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005570 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005571 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005572 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005573 if (tparg != NULL && tp != tparg)
5574 continue;
5575 d = get_tabpage_info(tp, tpnr);
5576 if (d != NULL)
5577 list_append_dict(rettv->vval.v_list, d);
5578 if (tparg != NULL)
5579 return;
5580 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005581}
5582
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005583/*
5584 * "gettabvar()" function
5585 */
5586 static void
5587f_gettabvar(typval_T *argvars, typval_T *rettv)
5588{
5589 win_T *oldcurwin;
5590 tabpage_T *tp, *oldtabpage;
5591 dictitem_T *v;
5592 char_u *varname;
5593 int done = FALSE;
5594
5595 rettv->v_type = VAR_STRING;
5596 rettv->vval.v_string = NULL;
5597
5598 varname = get_tv_string_chk(&argvars[1]);
5599 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5600 if (tp != NULL && varname != NULL)
5601 {
5602 /* Set tp to be our tabpage, temporarily. Also set the window to the
5603 * first window in the tabpage, otherwise the window is not valid. */
5604 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005605 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5606 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005607 {
5608 /* look up the variable */
5609 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5610 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5611 if (v != NULL)
5612 {
5613 copy_tv(&v->di_tv, rettv);
5614 done = TRUE;
5615 }
5616 }
5617
5618 /* restore previous notion of curwin */
5619 restore_win(oldcurwin, oldtabpage, TRUE);
5620 }
5621
5622 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5623 copy_tv(&argvars[2], rettv);
5624}
5625
5626/*
5627 * "gettabwinvar()" function
5628 */
5629 static void
5630f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5631{
5632 getwinvar(argvars, rettv, 1);
5633}
5634
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005635/*
5636 * Returns information about a window as a dictionary.
5637 */
5638 static dict_T *
5639get_win_info(win_T *wp, short tpnr, short winnr)
5640{
5641 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005642
5643 dict = dict_alloc();
5644 if (dict == NULL)
5645 return NULL;
5646
Bram Moolenaare0be1672018-07-08 16:50:37 +02005647 dict_add_number(dict, "tabnr", tpnr);
5648 dict_add_number(dict, "winnr", winnr);
5649 dict_add_number(dict, "winid", wp->w_id);
5650 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005651 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005652#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02005653 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005654#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005655 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005656 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005657 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005658
Bram Moolenaar69905d12017-08-13 18:14:47 +02005659#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02005660 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02005661#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005662#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02005663 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
5664 dict_add_number(dict, "loclist",
5665 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02005666#endif
5667
Bram Moolenaar30567352016-08-27 21:25:44 +02005668 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005669 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005670
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005671 return dict;
5672}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005673
5674/*
5675 * "getwininfo()" function
5676 */
5677 static void
5678f_getwininfo(typval_T *argvars, typval_T *rettv)
5679{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005680 tabpage_T *tp;
5681 win_T *wp = NULL, *wparg = NULL;
5682 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005683 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005684
5685 if (rettv_list_alloc(rettv) != OK)
5686 return;
5687
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005688 if (argvars[0].v_type != VAR_UNKNOWN)
5689 {
5690 wparg = win_id2wp(argvars);
5691 if (wparg == NULL)
5692 return;
5693 }
5694
5695 /* Collect information about either all the windows across all the tab
5696 * pages or one particular window.
5697 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005698 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005699 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005700 tabnr++;
5701 winnr = 0;
5702 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005703 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005704 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005705 if (wparg != NULL && wp != wparg)
5706 continue;
5707 d = get_win_info(wp, tabnr, winnr);
5708 if (d != NULL)
5709 list_append_dict(rettv->vval.v_list, d);
5710 if (wparg != NULL)
5711 /* found information about a specific window */
5712 return;
5713 }
5714 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005715}
5716
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005717/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005718 * "win_findbuf()" function
5719 */
5720 static void
5721f_win_findbuf(typval_T *argvars, typval_T *rettv)
5722{
5723 if (rettv_list_alloc(rettv) != FAIL)
5724 win_findbuf(argvars, rettv->vval.v_list);
5725}
5726
5727/*
5728 * "win_getid()" function
5729 */
5730 static void
5731f_win_getid(typval_T *argvars, typval_T *rettv)
5732{
5733 rettv->vval.v_number = win_getid(argvars);
5734}
5735
5736/*
5737 * "win_gotoid()" function
5738 */
5739 static void
5740f_win_gotoid(typval_T *argvars, typval_T *rettv)
5741{
5742 rettv->vval.v_number = win_gotoid(argvars);
5743}
5744
5745/*
5746 * "win_id2tabwin()" function
5747 */
5748 static void
5749f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5750{
5751 if (rettv_list_alloc(rettv) != FAIL)
5752 win_id2tabwin(argvars, rettv->vval.v_list);
5753}
5754
5755/*
5756 * "win_id2win()" function
5757 */
5758 static void
5759f_win_id2win(typval_T *argvars, typval_T *rettv)
5760{
5761 rettv->vval.v_number = win_id2win(argvars);
5762}
5763
5764/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005765 * "win_screenpos()" function
5766 */
5767 static void
5768f_win_screenpos(typval_T *argvars, typval_T *rettv)
5769{
5770 win_T *wp;
5771
5772 if (rettv_list_alloc(rettv) == FAIL)
5773 return;
5774
5775 wp = find_win_by_nr(&argvars[0], NULL);
5776 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5777 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5778}
5779
5780/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005781 * "getwinpos({timeout})" function
5782 */
5783 static void
5784f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
5785{
5786 int x = -1;
5787 int y = -1;
5788
5789 if (rettv_list_alloc(rettv) == FAIL)
5790 return;
5791#ifdef FEAT_GUI
5792 if (gui.in_use)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01005793 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005794# if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5795 else
5796# endif
5797#endif
5798#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5799 {
5800 varnumber_T timeout = 100;
5801
5802 if (argvars[0].v_type != VAR_UNKNOWN)
5803 timeout = get_tv_number(&argvars[0]);
5804 term_get_winpos(&x, &y, timeout);
5805 }
5806#endif
5807 list_append_number(rettv->vval.v_list, (varnumber_T)x);
5808 list_append_number(rettv->vval.v_list, (varnumber_T)y);
5809}
5810
5811
5812/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005813 * "getwinposx()" function
5814 */
5815 static void
5816f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5817{
5818 rettv->vval.v_number = -1;
5819#ifdef FEAT_GUI
5820 if (gui.in_use)
5821 {
5822 int x, y;
5823
5824 if (gui_mch_get_winpos(&x, &y) == OK)
5825 rettv->vval.v_number = x;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005826 return;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005827 }
5828#endif
5829#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5830 {
5831 int x, y;
5832
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005833 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005834 rettv->vval.v_number = x;
5835 }
5836#endif
5837}
5838
5839/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005840 * "getwinposy()" function
5841 */
5842 static void
5843f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5844{
5845 rettv->vval.v_number = -1;
5846#ifdef FEAT_GUI
5847 if (gui.in_use)
5848 {
5849 int x, y;
5850
5851 if (gui_mch_get_winpos(&x, &y) == OK)
5852 rettv->vval.v_number = y;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005853 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005854 }
5855#endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005856#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5857 {
5858 int x, y;
5859
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005860 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005861 rettv->vval.v_number = y;
5862 }
5863#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005864}
5865
5866/*
5867 * "getwinvar()" function
5868 */
5869 static void
5870f_getwinvar(typval_T *argvars, typval_T *rettv)
5871{
5872 getwinvar(argvars, rettv, 0);
5873}
5874
5875/*
5876 * "glob()" function
5877 */
5878 static void
5879f_glob(typval_T *argvars, typval_T *rettv)
5880{
5881 int options = WILD_SILENT|WILD_USE_NL;
5882 expand_T xpc;
5883 int error = FALSE;
5884
5885 /* When the optional second argument is non-zero, don't remove matches
5886 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5887 rettv->v_type = VAR_STRING;
5888 if (argvars[1].v_type != VAR_UNKNOWN)
5889 {
5890 if (get_tv_number_chk(&argvars[1], &error))
5891 options |= WILD_KEEP_ALL;
5892 if (argvars[2].v_type != VAR_UNKNOWN)
5893 {
5894 if (get_tv_number_chk(&argvars[2], &error))
5895 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005896 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005897 }
5898 if (argvars[3].v_type != VAR_UNKNOWN
5899 && get_tv_number_chk(&argvars[3], &error))
5900 options |= WILD_ALLLINKS;
5901 }
5902 }
5903 if (!error)
5904 {
5905 ExpandInit(&xpc);
5906 xpc.xp_context = EXPAND_FILES;
5907 if (p_wic)
5908 options += WILD_ICASE;
5909 if (rettv->v_type == VAR_STRING)
5910 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
5911 NULL, options, WILD_ALL);
5912 else if (rettv_list_alloc(rettv) != FAIL)
5913 {
5914 int i;
5915
5916 ExpandOne(&xpc, get_tv_string(&argvars[0]),
5917 NULL, options, WILD_ALL_KEEP);
5918 for (i = 0; i < xpc.xp_numfiles; i++)
5919 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5920
5921 ExpandCleanup(&xpc);
5922 }
5923 }
5924 else
5925 rettv->vval.v_string = NULL;
5926}
5927
5928/*
5929 * "globpath()" function
5930 */
5931 static void
5932f_globpath(typval_T *argvars, typval_T *rettv)
5933{
5934 int flags = 0;
5935 char_u buf1[NUMBUFLEN];
5936 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
5937 int error = FALSE;
5938 garray_T ga;
5939 int i;
5940
5941 /* When the optional second argument is non-zero, don't remove matches
5942 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5943 rettv->v_type = VAR_STRING;
5944 if (argvars[2].v_type != VAR_UNKNOWN)
5945 {
5946 if (get_tv_number_chk(&argvars[2], &error))
5947 flags |= WILD_KEEP_ALL;
5948 if (argvars[3].v_type != VAR_UNKNOWN)
5949 {
5950 if (get_tv_number_chk(&argvars[3], &error))
5951 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005952 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005953 }
5954 if (argvars[4].v_type != VAR_UNKNOWN
5955 && get_tv_number_chk(&argvars[4], &error))
5956 flags |= WILD_ALLLINKS;
5957 }
5958 }
5959 if (file != NULL && !error)
5960 {
5961 ga_init2(&ga, (int)sizeof(char_u *), 10);
5962 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
5963 if (rettv->v_type == VAR_STRING)
5964 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
5965 else if (rettv_list_alloc(rettv) != FAIL)
5966 for (i = 0; i < ga.ga_len; ++i)
5967 list_append_string(rettv->vval.v_list,
5968 ((char_u **)(ga.ga_data))[i], -1);
5969 ga_clear_strings(&ga);
5970 }
5971 else
5972 rettv->vval.v_string = NULL;
5973}
5974
5975/*
5976 * "glob2regpat()" function
5977 */
5978 static void
5979f_glob2regpat(typval_T *argvars, typval_T *rettv)
5980{
5981 char_u *pat = get_tv_string_chk(&argvars[0]);
5982
5983 rettv->v_type = VAR_STRING;
5984 rettv->vval.v_string = (pat == NULL)
5985 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
5986}
5987
5988/* for VIM_VERSION_ defines */
5989#include "version.h"
5990
5991/*
5992 * "has()" function
5993 */
5994 static void
5995f_has(typval_T *argvars, typval_T *rettv)
5996{
5997 int i;
5998 char_u *name;
5999 int n = FALSE;
6000 static char *(has_list[]) =
6001 {
6002#ifdef AMIGA
6003 "amiga",
6004# ifdef FEAT_ARP
6005 "arp",
6006# endif
6007#endif
6008#ifdef __BEOS__
6009 "beos",
6010#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02006011#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01006012 "mac", /* Mac OS X (and, once, Mac OS Classic) */
6013 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02006014# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01006015 "macunix", /* Mac OS X, with the darwin feature */
6016 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02006017# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006018#endif
6019#ifdef __QNX__
6020 "qnx",
6021#endif
6022#ifdef UNIX
6023 "unix",
6024#endif
6025#ifdef VMS
6026 "vms",
6027#endif
6028#ifdef WIN32
6029 "win32",
6030#endif
6031#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
6032 "win32unix",
6033#endif
6034#if defined(WIN64) || defined(_WIN64)
6035 "win64",
6036#endif
6037#ifdef EBCDIC
6038 "ebcdic",
6039#endif
6040#ifndef CASE_INSENSITIVE_FILENAME
6041 "fname_case",
6042#endif
6043#ifdef HAVE_ACL
6044 "acl",
6045#endif
6046#ifdef FEAT_ARABIC
6047 "arabic",
6048#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006049 "autocmd",
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006050#ifdef FEAT_AUTOSERVERNAME
6051 "autoservername",
6052#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006053#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006054 "balloon_eval",
6055# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
6056 "balloon_multiline",
6057# endif
6058#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006059#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006060 "balloon_eval_term",
6061#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006062#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6063 "builtin_terms",
6064# ifdef ALL_BUILTIN_TCAPS
6065 "all_builtin_terms",
6066# endif
6067#endif
6068#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
6069 || defined(FEAT_GUI_W32) \
6070 || defined(FEAT_GUI_MOTIF))
6071 "browsefilter",
6072#endif
6073#ifdef FEAT_BYTEOFF
6074 "byte_offset",
6075#endif
6076#ifdef FEAT_JOB_CHANNEL
6077 "channel",
6078#endif
6079#ifdef FEAT_CINDENT
6080 "cindent",
6081#endif
6082#ifdef FEAT_CLIENTSERVER
6083 "clientserver",
6084#endif
6085#ifdef FEAT_CLIPBOARD
6086 "clipboard",
6087#endif
6088#ifdef FEAT_CMDL_COMPL
6089 "cmdline_compl",
6090#endif
6091#ifdef FEAT_CMDHIST
6092 "cmdline_hist",
6093#endif
6094#ifdef FEAT_COMMENTS
6095 "comments",
6096#endif
6097#ifdef FEAT_CONCEAL
6098 "conceal",
6099#endif
6100#ifdef FEAT_CRYPT
6101 "cryptv",
6102 "crypt-blowfish",
6103 "crypt-blowfish2",
6104#endif
6105#ifdef FEAT_CSCOPE
6106 "cscope",
6107#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006108 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006109#ifdef CURSOR_SHAPE
6110 "cursorshape",
6111#endif
6112#ifdef DEBUG
6113 "debug",
6114#endif
6115#ifdef FEAT_CON_DIALOG
6116 "dialog_con",
6117#endif
6118#ifdef FEAT_GUI_DIALOG
6119 "dialog_gui",
6120#endif
6121#ifdef FEAT_DIFF
6122 "diff",
6123#endif
6124#ifdef FEAT_DIGRAPHS
6125 "digraphs",
6126#endif
6127#ifdef FEAT_DIRECTX
6128 "directx",
6129#endif
6130#ifdef FEAT_DND
6131 "dnd",
6132#endif
6133#ifdef FEAT_EMACS_TAGS
6134 "emacs_tags",
6135#endif
6136 "eval", /* always present, of course! */
6137 "ex_extra", /* graduated feature */
6138#ifdef FEAT_SEARCH_EXTRA
6139 "extra_search",
6140#endif
6141#ifdef FEAT_FKMAP
6142 "farsi",
6143#endif
6144#ifdef FEAT_SEARCHPATH
6145 "file_in_path",
6146#endif
6147#ifdef FEAT_FILTERPIPE
6148 "filterpipe",
6149#endif
6150#ifdef FEAT_FIND_ID
6151 "find_in_path",
6152#endif
6153#ifdef FEAT_FLOAT
6154 "float",
6155#endif
6156#ifdef FEAT_FOLDING
6157 "folding",
6158#endif
6159#ifdef FEAT_FOOTER
6160 "footer",
6161#endif
6162#if !defined(USE_SYSTEM) && defined(UNIX)
6163 "fork",
6164#endif
6165#ifdef FEAT_GETTEXT
6166 "gettext",
6167#endif
6168#ifdef FEAT_GUI
6169 "gui",
6170#endif
6171#ifdef FEAT_GUI_ATHENA
6172# ifdef FEAT_GUI_NEXTAW
6173 "gui_neXtaw",
6174# else
6175 "gui_athena",
6176# endif
6177#endif
6178#ifdef FEAT_GUI_GTK
6179 "gui_gtk",
6180# ifdef USE_GTK3
6181 "gui_gtk3",
6182# else
6183 "gui_gtk2",
6184# endif
6185#endif
6186#ifdef FEAT_GUI_GNOME
6187 "gui_gnome",
6188#endif
6189#ifdef FEAT_GUI_MAC
6190 "gui_mac",
6191#endif
6192#ifdef FEAT_GUI_MOTIF
6193 "gui_motif",
6194#endif
6195#ifdef FEAT_GUI_PHOTON
6196 "gui_photon",
6197#endif
6198#ifdef FEAT_GUI_W32
6199 "gui_win32",
6200#endif
6201#ifdef FEAT_HANGULIN
6202 "hangul_input",
6203#endif
6204#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6205 "iconv",
6206#endif
6207#ifdef FEAT_INS_EXPAND
6208 "insert_expand",
6209#endif
6210#ifdef FEAT_JOB_CHANNEL
6211 "job",
6212#endif
6213#ifdef FEAT_JUMPLIST
6214 "jumplist",
6215#endif
6216#ifdef FEAT_KEYMAP
6217 "keymap",
6218#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006219 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006220#ifdef FEAT_LANGMAP
6221 "langmap",
6222#endif
6223#ifdef FEAT_LIBCALL
6224 "libcall",
6225#endif
6226#ifdef FEAT_LINEBREAK
6227 "linebreak",
6228#endif
6229#ifdef FEAT_LISP
6230 "lispindent",
6231#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006232 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006233#ifdef FEAT_LOCALMAP
6234 "localmap",
6235#endif
6236#ifdef FEAT_LUA
6237# ifndef DYNAMIC_LUA
6238 "lua",
6239# endif
6240#endif
6241#ifdef FEAT_MENU
6242 "menu",
6243#endif
6244#ifdef FEAT_SESSION
6245 "mksession",
6246#endif
6247#ifdef FEAT_MODIFY_FNAME
6248 "modify_fname",
6249#endif
6250#ifdef FEAT_MOUSE
6251 "mouse",
6252#endif
6253#ifdef FEAT_MOUSESHAPE
6254 "mouseshape",
6255#endif
6256#if defined(UNIX) || defined(VMS)
6257# ifdef FEAT_MOUSE_DEC
6258 "mouse_dec",
6259# endif
6260# ifdef FEAT_MOUSE_GPM
6261 "mouse_gpm",
6262# endif
6263# ifdef FEAT_MOUSE_JSB
6264 "mouse_jsbterm",
6265# endif
6266# ifdef FEAT_MOUSE_NET
6267 "mouse_netterm",
6268# endif
6269# ifdef FEAT_MOUSE_PTERM
6270 "mouse_pterm",
6271# endif
6272# ifdef FEAT_MOUSE_SGR
6273 "mouse_sgr",
6274# endif
6275# ifdef FEAT_SYSMOUSE
6276 "mouse_sysmouse",
6277# endif
6278# ifdef FEAT_MOUSE_URXVT
6279 "mouse_urxvt",
6280# endif
6281# ifdef FEAT_MOUSE_XTERM
6282 "mouse_xterm",
6283# endif
6284#endif
6285#ifdef FEAT_MBYTE
6286 "multi_byte",
6287#endif
6288#ifdef FEAT_MBYTE_IME
6289 "multi_byte_ime",
6290#endif
6291#ifdef FEAT_MULTI_LANG
6292 "multi_lang",
6293#endif
6294#ifdef FEAT_MZSCHEME
6295#ifndef DYNAMIC_MZSCHEME
6296 "mzscheme",
6297#endif
6298#endif
6299#ifdef FEAT_NUM64
6300 "num64",
6301#endif
6302#ifdef FEAT_OLE
6303 "ole",
6304#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006305#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006306 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006307#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006308#ifdef FEAT_PATH_EXTRA
6309 "path_extra",
6310#endif
6311#ifdef FEAT_PERL
6312#ifndef DYNAMIC_PERL
6313 "perl",
6314#endif
6315#endif
6316#ifdef FEAT_PERSISTENT_UNDO
6317 "persistent_undo",
6318#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006319#if defined(FEAT_PYTHON)
6320 "python_compiled",
6321# if defined(DYNAMIC_PYTHON)
6322 "python_dynamic",
6323# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006324 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006325 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006326# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006327#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006328#if defined(FEAT_PYTHON3)
6329 "python3_compiled",
6330# if defined(DYNAMIC_PYTHON3)
6331 "python3_dynamic",
6332# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006333 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006334 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006335# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006336#endif
6337#ifdef FEAT_POSTSCRIPT
6338 "postscript",
6339#endif
6340#ifdef FEAT_PRINTER
6341 "printer",
6342#endif
6343#ifdef FEAT_PROFILE
6344 "profile",
6345#endif
6346#ifdef FEAT_RELTIME
6347 "reltime",
6348#endif
6349#ifdef FEAT_QUICKFIX
6350 "quickfix",
6351#endif
6352#ifdef FEAT_RIGHTLEFT
6353 "rightleft",
6354#endif
6355#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6356 "ruby",
6357#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006358 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006359#ifdef FEAT_CMDL_INFO
6360 "showcmd",
6361 "cmdline_info",
6362#endif
6363#ifdef FEAT_SIGNS
6364 "signs",
6365#endif
6366#ifdef FEAT_SMARTINDENT
6367 "smartindent",
6368#endif
6369#ifdef STARTUPTIME
6370 "startuptime",
6371#endif
6372#ifdef FEAT_STL_OPT
6373 "statusline",
6374#endif
6375#ifdef FEAT_SUN_WORKSHOP
6376 "sun_workshop",
6377#endif
6378#ifdef FEAT_NETBEANS_INTG
6379 "netbeans_intg",
6380#endif
6381#ifdef FEAT_SPELL
6382 "spell",
6383#endif
6384#ifdef FEAT_SYN_HL
6385 "syntax",
6386#endif
6387#if defined(USE_SYSTEM) || !defined(UNIX)
6388 "system",
6389#endif
6390#ifdef FEAT_TAG_BINS
6391 "tag_binary",
6392#endif
6393#ifdef FEAT_TAG_OLDSTATIC
6394 "tag_old_static",
6395#endif
6396#ifdef FEAT_TAG_ANYWHITE
6397 "tag_any_white",
6398#endif
6399#ifdef FEAT_TCL
6400# ifndef DYNAMIC_TCL
6401 "tcl",
6402# endif
6403#endif
6404#ifdef FEAT_TERMGUICOLORS
6405 "termguicolors",
6406#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006407#if defined(FEAT_TERMINAL) && !defined(WIN3264)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006408 "terminal",
6409#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006410#ifdef TERMINFO
6411 "terminfo",
6412#endif
6413#ifdef FEAT_TERMRESPONSE
6414 "termresponse",
6415#endif
6416#ifdef FEAT_TEXTOBJ
6417 "textobjects",
6418#endif
6419#ifdef HAVE_TGETENT
6420 "tgetent",
6421#endif
6422#ifdef FEAT_TIMERS
6423 "timers",
6424#endif
6425#ifdef FEAT_TITLE
6426 "title",
6427#endif
6428#ifdef FEAT_TOOLBAR
6429 "toolbar",
6430#endif
6431#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6432 "unnamedplus",
6433#endif
6434#ifdef FEAT_USR_CMDS
6435 "user-commands", /* was accidentally included in 5.4 */
6436 "user_commands",
6437#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006438#ifdef FEAT_VARTABS
6439 "vartabs",
6440#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006441#ifdef FEAT_VIMINFO
6442 "viminfo",
6443#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006444 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006445#ifdef FEAT_VIRTUALEDIT
6446 "virtualedit",
6447#endif
6448 "visual",
6449#ifdef FEAT_VISUALEXTRA
6450 "visualextra",
6451#endif
6452#ifdef FEAT_VREPLACE
6453 "vreplace",
6454#endif
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006455#ifdef FEAT_VTP
6456 "vtp",
6457#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006458#ifdef FEAT_WILDIGN
6459 "wildignore",
6460#endif
6461#ifdef FEAT_WILDMENU
6462 "wildmenu",
6463#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006464 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006465#ifdef FEAT_WAK
6466 "winaltkeys",
6467#endif
6468#ifdef FEAT_WRITEBACKUP
6469 "writebackup",
6470#endif
6471#ifdef FEAT_XIM
6472 "xim",
6473#endif
6474#ifdef FEAT_XFONTSET
6475 "xfontset",
6476#endif
6477#ifdef FEAT_XPM_W32
6478 "xpm",
6479 "xpm_w32", /* for backward compatibility */
6480#else
6481# if defined(HAVE_XPM)
6482 "xpm",
6483# endif
6484#endif
6485#ifdef USE_XSMP
6486 "xsmp",
6487#endif
6488#ifdef USE_XSMP_INTERACT
6489 "xsmp_interact",
6490#endif
6491#ifdef FEAT_XCLIPBOARD
6492 "xterm_clipboard",
6493#endif
6494#ifdef FEAT_XTERM_SAVE
6495 "xterm_save",
6496#endif
6497#if defined(UNIX) && defined(FEAT_X11)
6498 "X11",
6499#endif
6500 NULL
6501 };
6502
6503 name = get_tv_string(&argvars[0]);
6504 for (i = 0; has_list[i] != NULL; ++i)
6505 if (STRICMP(name, has_list[i]) == 0)
6506 {
6507 n = TRUE;
6508 break;
6509 }
6510
6511 if (n == FALSE)
6512 {
6513 if (STRNICMP(name, "patch", 5) == 0)
6514 {
6515 if (name[5] == '-'
6516 && STRLEN(name) >= 11
6517 && vim_isdigit(name[6])
6518 && vim_isdigit(name[8])
6519 && vim_isdigit(name[10]))
6520 {
6521 int major = atoi((char *)name + 6);
6522 int minor = atoi((char *)name + 8);
6523
6524 /* Expect "patch-9.9.01234". */
6525 n = (major < VIM_VERSION_MAJOR
6526 || (major == VIM_VERSION_MAJOR
6527 && (minor < VIM_VERSION_MINOR
6528 || (minor == VIM_VERSION_MINOR
6529 && has_patch(atoi((char *)name + 10))))));
6530 }
6531 else
6532 n = has_patch(atoi((char *)name + 5));
6533 }
6534 else if (STRICMP(name, "vim_starting") == 0)
6535 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006536 else if (STRICMP(name, "ttyin") == 0)
6537 n = mch_input_isatty();
6538 else if (STRICMP(name, "ttyout") == 0)
6539 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006540#ifdef FEAT_MBYTE
6541 else if (STRICMP(name, "multi_byte_encoding") == 0)
6542 n = has_mbyte;
6543#endif
6544#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
6545 else if (STRICMP(name, "balloon_multiline") == 0)
6546 n = multiline_balloon_available();
6547#endif
6548#ifdef DYNAMIC_TCL
6549 else if (STRICMP(name, "tcl") == 0)
6550 n = tcl_enabled(FALSE);
6551#endif
6552#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6553 else if (STRICMP(name, "iconv") == 0)
6554 n = iconv_enabled(FALSE);
6555#endif
6556#ifdef DYNAMIC_LUA
6557 else if (STRICMP(name, "lua") == 0)
6558 n = lua_enabled(FALSE);
6559#endif
6560#ifdef DYNAMIC_MZSCHEME
6561 else if (STRICMP(name, "mzscheme") == 0)
6562 n = mzscheme_enabled(FALSE);
6563#endif
6564#ifdef DYNAMIC_RUBY
6565 else if (STRICMP(name, "ruby") == 0)
6566 n = ruby_enabled(FALSE);
6567#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006568#ifdef DYNAMIC_PYTHON
6569 else if (STRICMP(name, "python") == 0)
6570 n = python_enabled(FALSE);
6571#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006572#ifdef DYNAMIC_PYTHON3
6573 else if (STRICMP(name, "python3") == 0)
6574 n = python3_enabled(FALSE);
6575#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006576#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6577 else if (STRICMP(name, "pythonx") == 0)
6578 {
6579# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6580 if (p_pyx == 0)
6581 n = python3_enabled(FALSE) || python_enabled(FALSE);
6582 else if (p_pyx == 3)
6583 n = python3_enabled(FALSE);
6584 else if (p_pyx == 2)
6585 n = python_enabled(FALSE);
6586# elif defined(DYNAMIC_PYTHON)
6587 n = python_enabled(FALSE);
6588# elif defined(DYNAMIC_PYTHON3)
6589 n = python3_enabled(FALSE);
6590# endif
6591 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006592#endif
6593#ifdef DYNAMIC_PERL
6594 else if (STRICMP(name, "perl") == 0)
6595 n = perl_enabled(FALSE);
6596#endif
6597#ifdef FEAT_GUI
6598 else if (STRICMP(name, "gui_running") == 0)
6599 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006600# ifdef FEAT_BROWSE
6601 else if (STRICMP(name, "browse") == 0)
6602 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6603# endif
6604#endif
6605#ifdef FEAT_SYN_HL
6606 else if (STRICMP(name, "syntax_items") == 0)
6607 n = syntax_present(curwin);
6608#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006609#ifdef FEAT_VTP
6610 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02006611 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006612#endif
6613#ifdef FEAT_NETBEANS_INTG
6614 else if (STRICMP(name, "netbeans_enabled") == 0)
6615 n = netbeans_active();
6616#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006617#if defined(FEAT_TERMINAL) && defined(WIN3264)
6618 else if (STRICMP(name, "terminal") == 0)
6619 n = terminal_enabled();
6620#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006621 }
6622
6623 rettv->vval.v_number = n;
6624}
6625
6626/*
6627 * "has_key()" function
6628 */
6629 static void
6630f_has_key(typval_T *argvars, typval_T *rettv)
6631{
6632 if (argvars[0].v_type != VAR_DICT)
6633 {
6634 EMSG(_(e_dictreq));
6635 return;
6636 }
6637 if (argvars[0].vval.v_dict == NULL)
6638 return;
6639
6640 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
6641 get_tv_string(&argvars[1]), -1) != NULL;
6642}
6643
6644/*
6645 * "haslocaldir()" function
6646 */
6647 static void
6648f_haslocaldir(typval_T *argvars, typval_T *rettv)
6649{
6650 win_T *wp = NULL;
6651
6652 wp = find_tabwin(&argvars[0], &argvars[1]);
6653 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6654}
6655
6656/*
6657 * "hasmapto()" function
6658 */
6659 static void
6660f_hasmapto(typval_T *argvars, typval_T *rettv)
6661{
6662 char_u *name;
6663 char_u *mode;
6664 char_u buf[NUMBUFLEN];
6665 int abbr = FALSE;
6666
6667 name = get_tv_string(&argvars[0]);
6668 if (argvars[1].v_type == VAR_UNKNOWN)
6669 mode = (char_u *)"nvo";
6670 else
6671 {
6672 mode = get_tv_string_buf(&argvars[1], buf);
6673 if (argvars[2].v_type != VAR_UNKNOWN)
6674 abbr = (int)get_tv_number(&argvars[2]);
6675 }
6676
6677 if (map_to_exists(name, mode, abbr))
6678 rettv->vval.v_number = TRUE;
6679 else
6680 rettv->vval.v_number = FALSE;
6681}
6682
6683/*
6684 * "histadd()" function
6685 */
6686 static void
6687f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6688{
6689#ifdef FEAT_CMDHIST
6690 int histype;
6691 char_u *str;
6692 char_u buf[NUMBUFLEN];
6693#endif
6694
6695 rettv->vval.v_number = FALSE;
6696 if (check_restricted() || check_secure())
6697 return;
6698#ifdef FEAT_CMDHIST
6699 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6700 histype = str != NULL ? get_histtype(str) : -1;
6701 if (histype >= 0)
6702 {
6703 str = get_tv_string_buf(&argvars[1], buf);
6704 if (*str != NUL)
6705 {
6706 init_history();
6707 add_to_history(histype, str, FALSE, NUL);
6708 rettv->vval.v_number = TRUE;
6709 return;
6710 }
6711 }
6712#endif
6713}
6714
6715/*
6716 * "histdel()" function
6717 */
6718 static void
6719f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6720{
6721#ifdef FEAT_CMDHIST
6722 int n;
6723 char_u buf[NUMBUFLEN];
6724 char_u *str;
6725
6726 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6727 if (str == NULL)
6728 n = 0;
6729 else if (argvars[1].v_type == VAR_UNKNOWN)
6730 /* only one argument: clear entire history */
6731 n = clr_history(get_histtype(str));
6732 else if (argvars[1].v_type == VAR_NUMBER)
6733 /* index given: remove that entry */
6734 n = del_history_idx(get_histtype(str),
6735 (int)get_tv_number(&argvars[1]));
6736 else
6737 /* string given: remove all matching entries */
6738 n = del_history_entry(get_histtype(str),
6739 get_tv_string_buf(&argvars[1], buf));
6740 rettv->vval.v_number = n;
6741#endif
6742}
6743
6744/*
6745 * "histget()" function
6746 */
6747 static void
6748f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6749{
6750#ifdef FEAT_CMDHIST
6751 int type;
6752 int idx;
6753 char_u *str;
6754
6755 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6756 if (str == NULL)
6757 rettv->vval.v_string = NULL;
6758 else
6759 {
6760 type = get_histtype(str);
6761 if (argvars[1].v_type == VAR_UNKNOWN)
6762 idx = get_history_idx(type);
6763 else
6764 idx = (int)get_tv_number_chk(&argvars[1], NULL);
6765 /* -1 on type error */
6766 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6767 }
6768#else
6769 rettv->vval.v_string = NULL;
6770#endif
6771 rettv->v_type = VAR_STRING;
6772}
6773
6774/*
6775 * "histnr()" function
6776 */
6777 static void
6778f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6779{
6780 int i;
6781
6782#ifdef FEAT_CMDHIST
6783 char_u *history = get_tv_string_chk(&argvars[0]);
6784
6785 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6786 if (i >= HIST_CMD && i < HIST_COUNT)
6787 i = get_history_idx(i);
6788 else
6789#endif
6790 i = -1;
6791 rettv->vval.v_number = i;
6792}
6793
6794/*
6795 * "highlightID(name)" function
6796 */
6797 static void
6798f_hlID(typval_T *argvars, typval_T *rettv)
6799{
6800 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
6801}
6802
6803/*
6804 * "highlight_exists()" function
6805 */
6806 static void
6807f_hlexists(typval_T *argvars, typval_T *rettv)
6808{
6809 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
6810}
6811
6812/*
6813 * "hostname()" function
6814 */
6815 static void
6816f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6817{
6818 char_u hostname[256];
6819
6820 mch_get_host_name(hostname, 256);
6821 rettv->v_type = VAR_STRING;
6822 rettv->vval.v_string = vim_strsave(hostname);
6823}
6824
6825/*
6826 * iconv() function
6827 */
6828 static void
6829f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6830{
6831#ifdef FEAT_MBYTE
6832 char_u buf1[NUMBUFLEN];
6833 char_u buf2[NUMBUFLEN];
6834 char_u *from, *to, *str;
6835 vimconv_T vimconv;
6836#endif
6837
6838 rettv->v_type = VAR_STRING;
6839 rettv->vval.v_string = NULL;
6840
6841#ifdef FEAT_MBYTE
6842 str = get_tv_string(&argvars[0]);
6843 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
6844 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
6845 vimconv.vc_type = CONV_NONE;
6846 convert_setup(&vimconv, from, to);
6847
6848 /* If the encodings are equal, no conversion needed. */
6849 if (vimconv.vc_type == CONV_NONE)
6850 rettv->vval.v_string = vim_strsave(str);
6851 else
6852 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6853
6854 convert_setup(&vimconv, NULL, NULL);
6855 vim_free(from);
6856 vim_free(to);
6857#endif
6858}
6859
6860/*
6861 * "indent()" function
6862 */
6863 static void
6864f_indent(typval_T *argvars, typval_T *rettv)
6865{
6866 linenr_T lnum;
6867
6868 lnum = get_tv_lnum(argvars);
6869 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6870 rettv->vval.v_number = get_indent_lnum(lnum);
6871 else
6872 rettv->vval.v_number = -1;
6873}
6874
6875/*
6876 * "index()" function
6877 */
6878 static void
6879f_index(typval_T *argvars, typval_T *rettv)
6880{
6881 list_T *l;
6882 listitem_T *item;
6883 long idx = 0;
6884 int ic = FALSE;
6885
6886 rettv->vval.v_number = -1;
6887 if (argvars[0].v_type != VAR_LIST)
6888 {
6889 EMSG(_(e_listreq));
6890 return;
6891 }
6892 l = argvars[0].vval.v_list;
6893 if (l != NULL)
6894 {
6895 item = l->lv_first;
6896 if (argvars[2].v_type != VAR_UNKNOWN)
6897 {
6898 int error = FALSE;
6899
6900 /* Start at specified item. Use the cached index that list_find()
6901 * sets, so that a negative number also works. */
6902 item = list_find(l, (long)get_tv_number_chk(&argvars[2], &error));
6903 idx = l->lv_idx;
6904 if (argvars[3].v_type != VAR_UNKNOWN)
6905 ic = (int)get_tv_number_chk(&argvars[3], &error);
6906 if (error)
6907 item = NULL;
6908 }
6909
6910 for ( ; item != NULL; item = item->li_next, ++idx)
6911 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6912 {
6913 rettv->vval.v_number = idx;
6914 break;
6915 }
6916 }
6917}
6918
6919static int inputsecret_flag = 0;
6920
6921/*
6922 * "input()" function
6923 * Also handles inputsecret() when inputsecret is set.
6924 */
6925 static void
6926f_input(typval_T *argvars, typval_T *rettv)
6927{
6928 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6929}
6930
6931/*
6932 * "inputdialog()" function
6933 */
6934 static void
6935f_inputdialog(typval_T *argvars, typval_T *rettv)
6936{
6937#if defined(FEAT_GUI_TEXTDIALOG)
6938 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
6939 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6940 {
6941 char_u *message;
6942 char_u buf[NUMBUFLEN];
6943 char_u *defstr = (char_u *)"";
6944
6945 message = get_tv_string_chk(&argvars[0]);
6946 if (argvars[1].v_type != VAR_UNKNOWN
6947 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
6948 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6949 else
6950 IObuff[0] = NUL;
6951 if (message != NULL && defstr != NULL
6952 && do_dialog(VIM_QUESTION, NULL, message,
6953 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6954 rettv->vval.v_string = vim_strsave(IObuff);
6955 else
6956 {
6957 if (message != NULL && defstr != NULL
6958 && argvars[1].v_type != VAR_UNKNOWN
6959 && argvars[2].v_type != VAR_UNKNOWN)
6960 rettv->vval.v_string = vim_strsave(
6961 get_tv_string_buf(&argvars[2], buf));
6962 else
6963 rettv->vval.v_string = NULL;
6964 }
6965 rettv->v_type = VAR_STRING;
6966 }
6967 else
6968#endif
6969 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6970}
6971
6972/*
6973 * "inputlist()" function
6974 */
6975 static void
6976f_inputlist(typval_T *argvars, typval_T *rettv)
6977{
6978 listitem_T *li;
6979 int selected;
6980 int mouse_used;
6981
6982#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02006983 /* While starting up, there is no place to enter text. When running tests
6984 * with --not-a-term we assume feedkeys() will be used. */
6985 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006986 return;
6987#endif
6988 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6989 {
6990 EMSG2(_(e_listarg), "inputlist()");
6991 return;
6992 }
6993
6994 msg_start();
6995 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
6996 lines_left = Rows; /* avoid more prompt */
6997 msg_scroll = TRUE;
6998 msg_clr_eos();
6999
7000 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
7001 {
7002 msg_puts(get_tv_string(&li->li_tv));
7003 msg_putchar('\n');
7004 }
7005
7006 /* Ask for choice. */
7007 selected = prompt_for_number(&mouse_used);
7008 if (mouse_used)
7009 selected -= lines_left;
7010
7011 rettv->vval.v_number = selected;
7012}
7013
7014
7015static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7016
7017/*
7018 * "inputrestore()" function
7019 */
7020 static void
7021f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7022{
7023 if (ga_userinput.ga_len > 0)
7024 {
7025 --ga_userinput.ga_len;
7026 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7027 + ga_userinput.ga_len);
7028 /* default return is zero == OK */
7029 }
7030 else if (p_verbose > 1)
7031 {
7032 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
7033 rettv->vval.v_number = 1; /* Failed */
7034 }
7035}
7036
7037/*
7038 * "inputsave()" function
7039 */
7040 static void
7041f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7042{
7043 /* Add an entry to the stack of typeahead storage. */
7044 if (ga_grow(&ga_userinput, 1) == OK)
7045 {
7046 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7047 + ga_userinput.ga_len);
7048 ++ga_userinput.ga_len;
7049 /* default return is zero == OK */
7050 }
7051 else
7052 rettv->vval.v_number = 1; /* Failed */
7053}
7054
7055/*
7056 * "inputsecret()" function
7057 */
7058 static void
7059f_inputsecret(typval_T *argvars, typval_T *rettv)
7060{
7061 ++cmdline_star;
7062 ++inputsecret_flag;
7063 f_input(argvars, rettv);
7064 --cmdline_star;
7065 --inputsecret_flag;
7066}
7067
7068/*
7069 * "insert()" function
7070 */
7071 static void
7072f_insert(typval_T *argvars, typval_T *rettv)
7073{
7074 long before = 0;
7075 listitem_T *item;
7076 list_T *l;
7077 int error = FALSE;
7078
7079 if (argvars[0].v_type != VAR_LIST)
7080 EMSG2(_(e_listarg), "insert()");
7081 else if ((l = argvars[0].vval.v_list) != NULL
7082 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
7083 {
7084 if (argvars[2].v_type != VAR_UNKNOWN)
7085 before = (long)get_tv_number_chk(&argvars[2], &error);
7086 if (error)
7087 return; /* type error; errmsg already given */
7088
7089 if (before == l->lv_len)
7090 item = NULL;
7091 else
7092 {
7093 item = list_find(l, before);
7094 if (item == NULL)
7095 {
7096 EMSGN(_(e_listidx), before);
7097 l = NULL;
7098 }
7099 }
7100 if (l != NULL)
7101 {
7102 list_insert_tv(l, &argvars[1], item);
7103 copy_tv(&argvars[0], rettv);
7104 }
7105 }
7106}
7107
7108/*
7109 * "invert(expr)" function
7110 */
7111 static void
7112f_invert(typval_T *argvars, typval_T *rettv)
7113{
7114 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
7115}
7116
7117/*
7118 * "isdirectory()" function
7119 */
7120 static void
7121f_isdirectory(typval_T *argvars, typval_T *rettv)
7122{
7123 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
7124}
7125
7126/*
7127 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7128 * or it refers to a List or Dictionary that is locked.
7129 */
7130 static int
7131tv_islocked(typval_T *tv)
7132{
7133 return (tv->v_lock & VAR_LOCKED)
7134 || (tv->v_type == VAR_LIST
7135 && tv->vval.v_list != NULL
7136 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7137 || (tv->v_type == VAR_DICT
7138 && tv->vval.v_dict != NULL
7139 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7140}
7141
7142/*
7143 * "islocked()" function
7144 */
7145 static void
7146f_islocked(typval_T *argvars, typval_T *rettv)
7147{
7148 lval_T lv;
7149 char_u *end;
7150 dictitem_T *di;
7151
7152 rettv->vval.v_number = -1;
7153 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007154 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007155 if (end != NULL && lv.ll_name != NULL)
7156 {
7157 if (*end != NUL)
7158 EMSG(_(e_trailing));
7159 else
7160 {
7161 if (lv.ll_tv == NULL)
7162 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007163 di = find_var(lv.ll_name, NULL, TRUE);
7164 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007165 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007166 /* Consider a variable locked when:
7167 * 1. the variable itself is locked
7168 * 2. the value of the variable is locked.
7169 * 3. the List or Dict value is locked.
7170 */
7171 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7172 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007173 }
7174 }
7175 else if (lv.ll_range)
7176 EMSG(_("E786: Range not allowed"));
7177 else if (lv.ll_newkey != NULL)
7178 EMSG2(_(e_dictkey), lv.ll_newkey);
7179 else if (lv.ll_list != NULL)
7180 /* List item. */
7181 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7182 else
7183 /* Dictionary item. */
7184 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7185 }
7186 }
7187
7188 clear_lval(&lv);
7189}
7190
7191#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7192/*
7193 * "isnan()" function
7194 */
7195 static void
7196f_isnan(typval_T *argvars, typval_T *rettv)
7197{
7198 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7199 && isnan(argvars[0].vval.v_float);
7200}
7201#endif
7202
7203/*
7204 * "items(dict)" function
7205 */
7206 static void
7207f_items(typval_T *argvars, typval_T *rettv)
7208{
7209 dict_list(argvars, rettv, 2);
7210}
7211
7212#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
7213/*
7214 * Get the job from the argument.
7215 * Returns NULL if the job is invalid.
7216 */
7217 static job_T *
7218get_job_arg(typval_T *tv)
7219{
7220 job_T *job;
7221
7222 if (tv->v_type != VAR_JOB)
7223 {
7224 EMSG2(_(e_invarg2), get_tv_string(tv));
7225 return NULL;
7226 }
7227 job = tv->vval.v_job;
7228
7229 if (job == NULL)
7230 EMSG(_("E916: not a valid job"));
7231 return job;
7232}
7233
7234/*
7235 * "job_getchannel()" function
7236 */
7237 static void
7238f_job_getchannel(typval_T *argvars, typval_T *rettv)
7239{
7240 job_T *job = get_job_arg(&argvars[0]);
7241
7242 if (job != NULL)
7243 {
7244 rettv->v_type = VAR_CHANNEL;
7245 rettv->vval.v_channel = job->jv_channel;
7246 if (job->jv_channel != NULL)
7247 ++job->jv_channel->ch_refcount;
7248 }
7249}
7250
7251/*
7252 * "job_info()" function
7253 */
7254 static void
7255f_job_info(typval_T *argvars, typval_T *rettv)
7256{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007257 if (argvars[0].v_type != VAR_UNKNOWN)
7258 {
7259 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007260
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007261 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7262 job_info(job, rettv->vval.v_dict);
7263 }
7264 else if (rettv_list_alloc(rettv) == OK)
7265 job_info_all(rettv->vval.v_list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007266}
7267
7268/*
7269 * "job_setoptions()" function
7270 */
7271 static void
7272f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7273{
7274 job_T *job = get_job_arg(&argvars[0]);
7275 jobopt_T opt;
7276
7277 if (job == NULL)
7278 return;
7279 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007280 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007281 job_set_options(job, &opt);
7282 free_job_options(&opt);
7283}
7284
7285/*
7286 * "job_start()" function
7287 */
7288 static void
7289f_job_start(typval_T *argvars, typval_T *rettv)
7290{
7291 rettv->v_type = VAR_JOB;
7292 if (check_restricted() || check_secure())
7293 return;
Bram Moolenaar493359e2018-06-12 20:25:52 +02007294 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007295}
7296
7297/*
7298 * "job_status()" function
7299 */
7300 static void
7301f_job_status(typval_T *argvars, typval_T *rettv)
7302{
7303 job_T *job = get_job_arg(&argvars[0]);
7304
7305 if (job != NULL)
7306 {
7307 rettv->v_type = VAR_STRING;
7308 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7309 }
7310}
7311
7312/*
7313 * "job_stop()" function
7314 */
7315 static void
7316f_job_stop(typval_T *argvars, typval_T *rettv)
7317{
7318 job_T *job = get_job_arg(&argvars[0]);
7319
7320 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007321 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007322}
7323#endif
7324
7325/*
7326 * "join()" function
7327 */
7328 static void
7329f_join(typval_T *argvars, typval_T *rettv)
7330{
7331 garray_T ga;
7332 char_u *sep;
7333
7334 if (argvars[0].v_type != VAR_LIST)
7335 {
7336 EMSG(_(e_listreq));
7337 return;
7338 }
7339 if (argvars[0].vval.v_list == NULL)
7340 return;
7341 if (argvars[1].v_type == VAR_UNKNOWN)
7342 sep = (char_u *)" ";
7343 else
7344 sep = get_tv_string_chk(&argvars[1]);
7345
7346 rettv->v_type = VAR_STRING;
7347
7348 if (sep != NULL)
7349 {
7350 ga_init2(&ga, (int)sizeof(char), 80);
7351 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7352 ga_append(&ga, NUL);
7353 rettv->vval.v_string = (char_u *)ga.ga_data;
7354 }
7355 else
7356 rettv->vval.v_string = NULL;
7357}
7358
7359/*
7360 * "js_decode()" function
7361 */
7362 static void
7363f_js_decode(typval_T *argvars, typval_T *rettv)
7364{
7365 js_read_T reader;
7366
7367 reader.js_buf = get_tv_string(&argvars[0]);
7368 reader.js_fill = NULL;
7369 reader.js_used = 0;
7370 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
7371 EMSG(_(e_invarg));
7372}
7373
7374/*
7375 * "js_encode()" function
7376 */
7377 static void
7378f_js_encode(typval_T *argvars, typval_T *rettv)
7379{
7380 rettv->v_type = VAR_STRING;
7381 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7382}
7383
7384/*
7385 * "json_decode()" function
7386 */
7387 static void
7388f_json_decode(typval_T *argvars, typval_T *rettv)
7389{
7390 js_read_T reader;
7391
7392 reader.js_buf = get_tv_string(&argvars[0]);
7393 reader.js_fill = NULL;
7394 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007395 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007396}
7397
7398/*
7399 * "json_encode()" function
7400 */
7401 static void
7402f_json_encode(typval_T *argvars, typval_T *rettv)
7403{
7404 rettv->v_type = VAR_STRING;
7405 rettv->vval.v_string = json_encode(&argvars[0], 0);
7406}
7407
7408/*
7409 * "keys()" function
7410 */
7411 static void
7412f_keys(typval_T *argvars, typval_T *rettv)
7413{
7414 dict_list(argvars, rettv, 0);
7415}
7416
7417/*
7418 * "last_buffer_nr()" function.
7419 */
7420 static void
7421f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7422{
7423 int n = 0;
7424 buf_T *buf;
7425
Bram Moolenaar29323592016-07-24 22:04:11 +02007426 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007427 if (n < buf->b_fnum)
7428 n = buf->b_fnum;
7429
7430 rettv->vval.v_number = n;
7431}
7432
7433/*
7434 * "len()" function
7435 */
7436 static void
7437f_len(typval_T *argvars, typval_T *rettv)
7438{
7439 switch (argvars[0].v_type)
7440 {
7441 case VAR_STRING:
7442 case VAR_NUMBER:
7443 rettv->vval.v_number = (varnumber_T)STRLEN(
7444 get_tv_string(&argvars[0]));
7445 break;
7446 case VAR_LIST:
7447 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7448 break;
7449 case VAR_DICT:
7450 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7451 break;
7452 case VAR_UNKNOWN:
7453 case VAR_SPECIAL:
7454 case VAR_FLOAT:
7455 case VAR_FUNC:
7456 case VAR_PARTIAL:
7457 case VAR_JOB:
7458 case VAR_CHANNEL:
7459 EMSG(_("E701: Invalid type for len()"));
7460 break;
7461 }
7462}
7463
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007464 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007465libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007466{
7467#ifdef FEAT_LIBCALL
7468 char_u *string_in;
7469 char_u **string_result;
7470 int nr_result;
7471#endif
7472
7473 rettv->v_type = type;
7474 if (type != VAR_NUMBER)
7475 rettv->vval.v_string = NULL;
7476
7477 if (check_restricted() || check_secure())
7478 return;
7479
7480#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007481 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007482 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7483 {
7484 string_in = NULL;
7485 if (argvars[2].v_type == VAR_STRING)
7486 string_in = argvars[2].vval.v_string;
7487 if (type == VAR_NUMBER)
7488 string_result = NULL;
7489 else
7490 string_result = &rettv->vval.v_string;
7491 if (mch_libcall(argvars[0].vval.v_string,
7492 argvars[1].vval.v_string,
7493 string_in,
7494 argvars[2].vval.v_number,
7495 string_result,
7496 &nr_result) == OK
7497 && type == VAR_NUMBER)
7498 rettv->vval.v_number = nr_result;
7499 }
7500#endif
7501}
7502
7503/*
7504 * "libcall()" function
7505 */
7506 static void
7507f_libcall(typval_T *argvars, typval_T *rettv)
7508{
7509 libcall_common(argvars, rettv, VAR_STRING);
7510}
7511
7512/*
7513 * "libcallnr()" function
7514 */
7515 static void
7516f_libcallnr(typval_T *argvars, typval_T *rettv)
7517{
7518 libcall_common(argvars, rettv, VAR_NUMBER);
7519}
7520
7521/*
7522 * "line(string)" function
7523 */
7524 static void
7525f_line(typval_T *argvars, typval_T *rettv)
7526{
7527 linenr_T lnum = 0;
7528 pos_T *fp;
7529 int fnum;
7530
7531 fp = var2fpos(&argvars[0], TRUE, &fnum);
7532 if (fp != NULL)
7533 lnum = fp->lnum;
7534 rettv->vval.v_number = lnum;
7535}
7536
7537/*
7538 * "line2byte(lnum)" function
7539 */
7540 static void
7541f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7542{
7543#ifndef FEAT_BYTEOFF
7544 rettv->vval.v_number = -1;
7545#else
7546 linenr_T lnum;
7547
7548 lnum = get_tv_lnum(argvars);
7549 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7550 rettv->vval.v_number = -1;
7551 else
7552 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7553 if (rettv->vval.v_number >= 0)
7554 ++rettv->vval.v_number;
7555#endif
7556}
7557
7558/*
7559 * "lispindent(lnum)" function
7560 */
7561 static void
7562f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7563{
7564#ifdef FEAT_LISP
7565 pos_T pos;
7566 linenr_T lnum;
7567
7568 pos = curwin->w_cursor;
7569 lnum = get_tv_lnum(argvars);
7570 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7571 {
7572 curwin->w_cursor.lnum = lnum;
7573 rettv->vval.v_number = get_lisp_indent();
7574 curwin->w_cursor = pos;
7575 }
7576 else
7577#endif
7578 rettv->vval.v_number = -1;
7579}
7580
7581/*
7582 * "localtime()" function
7583 */
7584 static void
7585f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7586{
7587 rettv->vval.v_number = (varnumber_T)time(NULL);
7588}
7589
7590static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
7591
7592 static void
7593get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7594{
7595 char_u *keys;
7596 char_u *which;
7597 char_u buf[NUMBUFLEN];
7598 char_u *keys_buf = NULL;
7599 char_u *rhs;
7600 int mode;
7601 int abbr = FALSE;
7602 int get_dict = FALSE;
7603 mapblock_T *mp;
7604 int buffer_local;
7605
7606 /* return empty string for failure */
7607 rettv->v_type = VAR_STRING;
7608 rettv->vval.v_string = NULL;
7609
7610 keys = get_tv_string(&argvars[0]);
7611 if (*keys == NUL)
7612 return;
7613
7614 if (argvars[1].v_type != VAR_UNKNOWN)
7615 {
7616 which = get_tv_string_buf_chk(&argvars[1], buf);
7617 if (argvars[2].v_type != VAR_UNKNOWN)
7618 {
7619 abbr = (int)get_tv_number(&argvars[2]);
7620 if (argvars[3].v_type != VAR_UNKNOWN)
7621 get_dict = (int)get_tv_number(&argvars[3]);
7622 }
7623 }
7624 else
7625 which = (char_u *)"";
7626 if (which == NULL)
7627 return;
7628
7629 mode = get_map_mode(&which, 0);
7630
7631 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7632 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7633 vim_free(keys_buf);
7634
7635 if (!get_dict)
7636 {
7637 /* Return a string. */
7638 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02007639 {
7640 if (*rhs == NUL)
7641 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
7642 else
7643 rettv->vval.v_string = str2special_save(rhs, FALSE);
7644 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007645
7646 }
7647 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7648 {
7649 /* Return a dictionary. */
7650 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7651 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7652 dict_T *dict = rettv->vval.v_dict;
7653
Bram Moolenaare0be1672018-07-08 16:50:37 +02007654 dict_add_string(dict, "lhs", lhs);
7655 dict_add_string(dict, "rhs", mp->m_orig_str);
7656 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
7657 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
7658 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
7659 dict_add_number(dict, "sid", (long)mp->m_script_ID);
7660 dict_add_number(dict, "buffer", (long)buffer_local);
7661 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
7662 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007663
7664 vim_free(lhs);
7665 vim_free(mapmode);
7666 }
7667}
7668
7669#ifdef FEAT_FLOAT
7670/*
7671 * "log()" function
7672 */
7673 static void
7674f_log(typval_T *argvars, typval_T *rettv)
7675{
7676 float_T f = 0.0;
7677
7678 rettv->v_type = VAR_FLOAT;
7679 if (get_float_arg(argvars, &f) == OK)
7680 rettv->vval.v_float = log(f);
7681 else
7682 rettv->vval.v_float = 0.0;
7683}
7684
7685/*
7686 * "log10()" function
7687 */
7688 static void
7689f_log10(typval_T *argvars, typval_T *rettv)
7690{
7691 float_T f = 0.0;
7692
7693 rettv->v_type = VAR_FLOAT;
7694 if (get_float_arg(argvars, &f) == OK)
7695 rettv->vval.v_float = log10(f);
7696 else
7697 rettv->vval.v_float = 0.0;
7698}
7699#endif
7700
7701#ifdef FEAT_LUA
7702/*
7703 * "luaeval()" function
7704 */
7705 static void
7706f_luaeval(typval_T *argvars, typval_T *rettv)
7707{
7708 char_u *str;
7709 char_u buf[NUMBUFLEN];
7710
7711 str = get_tv_string_buf(&argvars[0], buf);
7712 do_luaeval(str, argvars + 1, rettv);
7713}
7714#endif
7715
7716/*
7717 * "map()" function
7718 */
7719 static void
7720f_map(typval_T *argvars, typval_T *rettv)
7721{
7722 filter_map(argvars, rettv, TRUE);
7723}
7724
7725/*
7726 * "maparg()" function
7727 */
7728 static void
7729f_maparg(typval_T *argvars, typval_T *rettv)
7730{
7731 get_maparg(argvars, rettv, TRUE);
7732}
7733
7734/*
7735 * "mapcheck()" function
7736 */
7737 static void
7738f_mapcheck(typval_T *argvars, typval_T *rettv)
7739{
7740 get_maparg(argvars, rettv, FALSE);
7741}
7742
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007743typedef enum
7744{
7745 MATCH_END, /* matchend() */
7746 MATCH_MATCH, /* match() */
7747 MATCH_STR, /* matchstr() */
7748 MATCH_LIST, /* matchlist() */
7749 MATCH_POS /* matchstrpos() */
7750} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007751
7752 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007753find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007754{
7755 char_u *str = NULL;
7756 long len = 0;
7757 char_u *expr = NULL;
7758 char_u *pat;
7759 regmatch_T regmatch;
7760 char_u patbuf[NUMBUFLEN];
7761 char_u strbuf[NUMBUFLEN];
7762 char_u *save_cpo;
7763 long start = 0;
7764 long nth = 1;
7765 colnr_T startcol = 0;
7766 int match = 0;
7767 list_T *l = NULL;
7768 listitem_T *li = NULL;
7769 long idx = 0;
7770 char_u *tofree = NULL;
7771
7772 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7773 save_cpo = p_cpo;
7774 p_cpo = (char_u *)"";
7775
7776 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007777 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007778 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007779 /* type MATCH_LIST: return empty list when there are no matches.
7780 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007781 if (rettv_list_alloc(rettv) == FAIL)
7782 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007783 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007784 && (list_append_string(rettv->vval.v_list,
7785 (char_u *)"", 0) == 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 || list_append_number(rettv->vval.v_list,
7791 (varnumber_T)-1) == FAIL))
7792 {
7793 list_free(rettv->vval.v_list);
7794 rettv->vval.v_list = NULL;
7795 goto theend;
7796 }
7797 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007798 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007799 {
7800 rettv->v_type = VAR_STRING;
7801 rettv->vval.v_string = NULL;
7802 }
7803
7804 if (argvars[0].v_type == VAR_LIST)
7805 {
7806 if ((l = argvars[0].vval.v_list) == NULL)
7807 goto theend;
7808 li = l->lv_first;
7809 }
7810 else
7811 {
7812 expr = str = get_tv_string(&argvars[0]);
7813 len = (long)STRLEN(str);
7814 }
7815
7816 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
7817 if (pat == NULL)
7818 goto theend;
7819
7820 if (argvars[2].v_type != VAR_UNKNOWN)
7821 {
7822 int error = FALSE;
7823
7824 start = (long)get_tv_number_chk(&argvars[2], &error);
7825 if (error)
7826 goto theend;
7827 if (l != NULL)
7828 {
7829 li = list_find(l, start);
7830 if (li == NULL)
7831 goto theend;
7832 idx = l->lv_idx; /* use the cached index */
7833 }
7834 else
7835 {
7836 if (start < 0)
7837 start = 0;
7838 if (start > len)
7839 goto theend;
7840 /* When "count" argument is there ignore matches before "start",
7841 * otherwise skip part of the string. Differs when pattern is "^"
7842 * or "\<". */
7843 if (argvars[3].v_type != VAR_UNKNOWN)
7844 startcol = start;
7845 else
7846 {
7847 str += start;
7848 len -= start;
7849 }
7850 }
7851
7852 if (argvars[3].v_type != VAR_UNKNOWN)
7853 nth = (long)get_tv_number_chk(&argvars[3], &error);
7854 if (error)
7855 goto theend;
7856 }
7857
7858 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7859 if (regmatch.regprog != NULL)
7860 {
7861 regmatch.rm_ic = p_ic;
7862
7863 for (;;)
7864 {
7865 if (l != NULL)
7866 {
7867 if (li == NULL)
7868 {
7869 match = FALSE;
7870 break;
7871 }
7872 vim_free(tofree);
7873 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
7874 if (str == NULL)
7875 break;
7876 }
7877
7878 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
7879
7880 if (match && --nth <= 0)
7881 break;
7882 if (l == NULL && !match)
7883 break;
7884
7885 /* Advance to just after the match. */
7886 if (l != NULL)
7887 {
7888 li = li->li_next;
7889 ++idx;
7890 }
7891 else
7892 {
7893#ifdef FEAT_MBYTE
7894 startcol = (colnr_T)(regmatch.startp[0]
7895 + (*mb_ptr2len)(regmatch.startp[0]) - str);
7896#else
7897 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
7898#endif
7899 if (startcol > (colnr_T)len
7900 || str + startcol <= regmatch.startp[0])
7901 {
7902 match = FALSE;
7903 break;
7904 }
7905 }
7906 }
7907
7908 if (match)
7909 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007910 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007911 {
7912 listitem_T *li1 = rettv->vval.v_list->lv_first;
7913 listitem_T *li2 = li1->li_next;
7914 listitem_T *li3 = li2->li_next;
7915 listitem_T *li4 = li3->li_next;
7916
7917 vim_free(li1->li_tv.vval.v_string);
7918 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
7919 (int)(regmatch.endp[0] - regmatch.startp[0]));
7920 li3->li_tv.vval.v_number =
7921 (varnumber_T)(regmatch.startp[0] - expr);
7922 li4->li_tv.vval.v_number =
7923 (varnumber_T)(regmatch.endp[0] - expr);
7924 if (l != NULL)
7925 li2->li_tv.vval.v_number = (varnumber_T)idx;
7926 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007927 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007928 {
7929 int i;
7930
7931 /* return list with matched string and submatches */
7932 for (i = 0; i < NSUBEXP; ++i)
7933 {
7934 if (regmatch.endp[i] == NULL)
7935 {
7936 if (list_append_string(rettv->vval.v_list,
7937 (char_u *)"", 0) == FAIL)
7938 break;
7939 }
7940 else if (list_append_string(rettv->vval.v_list,
7941 regmatch.startp[i],
7942 (int)(regmatch.endp[i] - regmatch.startp[i]))
7943 == FAIL)
7944 break;
7945 }
7946 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007947 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007948 {
7949 /* return matched string */
7950 if (l != NULL)
7951 copy_tv(&li->li_tv, rettv);
7952 else
7953 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
7954 (int)(regmatch.endp[0] - regmatch.startp[0]));
7955 }
7956 else if (l != NULL)
7957 rettv->vval.v_number = idx;
7958 else
7959 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007960 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007961 rettv->vval.v_number =
7962 (varnumber_T)(regmatch.startp[0] - str);
7963 else
7964 rettv->vval.v_number =
7965 (varnumber_T)(regmatch.endp[0] - str);
7966 rettv->vval.v_number += (varnumber_T)(str - expr);
7967 }
7968 }
7969 vim_regfree(regmatch.regprog);
7970 }
7971
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007972theend:
7973 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007974 /* matchstrpos() without a list: drop the second item. */
7975 listitem_remove(rettv->vval.v_list,
7976 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007977 vim_free(tofree);
7978 p_cpo = save_cpo;
7979}
7980
7981/*
7982 * "match()" function
7983 */
7984 static void
7985f_match(typval_T *argvars, typval_T *rettv)
7986{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007987 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007988}
7989
7990/*
7991 * "matchadd()" function
7992 */
7993 static void
7994f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7995{
7996#ifdef FEAT_SEARCH_EXTRA
7997 char_u buf[NUMBUFLEN];
7998 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
7999 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
8000 int prio = 10; /* default priority */
8001 int id = -1;
8002 int error = FALSE;
8003 char_u *conceal_char = NULL;
8004
8005 rettv->vval.v_number = -1;
8006
8007 if (grp == NULL || pat == NULL)
8008 return;
8009 if (argvars[2].v_type != VAR_UNKNOWN)
8010 {
8011 prio = (int)get_tv_number_chk(&argvars[2], &error);
8012 if (argvars[3].v_type != VAR_UNKNOWN)
8013 {
8014 id = (int)get_tv_number_chk(&argvars[3], &error);
8015 if (argvars[4].v_type != VAR_UNKNOWN)
8016 {
8017 if (argvars[4].v_type != VAR_DICT)
8018 {
8019 EMSG(_(e_dictreq));
8020 return;
8021 }
8022 if (dict_find(argvars[4].vval.v_dict,
8023 (char_u *)"conceal", -1) != NULL)
8024 conceal_char = get_dict_string(argvars[4].vval.v_dict,
8025 (char_u *)"conceal", FALSE);
8026 }
8027 }
8028 }
8029 if (error == TRUE)
8030 return;
8031 if (id >= 1 && id <= 3)
8032 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02008033 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008034 return;
8035 }
8036
8037 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
8038 conceal_char);
8039#endif
8040}
8041
8042/*
8043 * "matchaddpos()" function
8044 */
8045 static void
8046f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8047{
8048#ifdef FEAT_SEARCH_EXTRA
8049 char_u buf[NUMBUFLEN];
8050 char_u *group;
8051 int prio = 10;
8052 int id = -1;
8053 int error = FALSE;
8054 list_T *l;
8055 char_u *conceal_char = NULL;
8056
8057 rettv->vval.v_number = -1;
8058
8059 group = get_tv_string_buf_chk(&argvars[0], buf);
8060 if (group == NULL)
8061 return;
8062
8063 if (argvars[1].v_type != VAR_LIST)
8064 {
8065 EMSG2(_(e_listarg), "matchaddpos()");
8066 return;
8067 }
8068 l = argvars[1].vval.v_list;
8069 if (l == NULL)
8070 return;
8071
8072 if (argvars[2].v_type != VAR_UNKNOWN)
8073 {
8074 prio = (int)get_tv_number_chk(&argvars[2], &error);
8075 if (argvars[3].v_type != VAR_UNKNOWN)
8076 {
8077 id = (int)get_tv_number_chk(&argvars[3], &error);
8078 if (argvars[4].v_type != VAR_UNKNOWN)
8079 {
8080 if (argvars[4].v_type != VAR_DICT)
8081 {
8082 EMSG(_(e_dictreq));
8083 return;
8084 }
8085 if (dict_find(argvars[4].vval.v_dict,
8086 (char_u *)"conceal", -1) != NULL)
8087 conceal_char = get_dict_string(argvars[4].vval.v_dict,
8088 (char_u *)"conceal", FALSE);
8089 }
8090 }
8091 }
8092 if (error == TRUE)
8093 return;
8094
8095 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8096 if (id == 1 || id == 2)
8097 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02008098 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008099 return;
8100 }
8101
8102 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
8103 conceal_char);
8104#endif
8105}
8106
8107/*
8108 * "matcharg()" function
8109 */
8110 static void
8111f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8112{
8113 if (rettv_list_alloc(rettv) == OK)
8114 {
8115#ifdef FEAT_SEARCH_EXTRA
8116 int id = (int)get_tv_number(&argvars[0]);
8117 matchitem_T *m;
8118
8119 if (id >= 1 && id <= 3)
8120 {
8121 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8122 {
8123 list_append_string(rettv->vval.v_list,
8124 syn_id2name(m->hlg_id), -1);
8125 list_append_string(rettv->vval.v_list, m->pattern, -1);
8126 }
8127 else
8128 {
8129 list_append_string(rettv->vval.v_list, NULL, -1);
8130 list_append_string(rettv->vval.v_list, NULL, -1);
8131 }
8132 }
8133#endif
8134 }
8135}
8136
8137/*
8138 * "matchdelete()" function
8139 */
8140 static void
8141f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8142{
8143#ifdef FEAT_SEARCH_EXTRA
8144 rettv->vval.v_number = match_delete(curwin,
8145 (int)get_tv_number(&argvars[0]), TRUE);
8146#endif
8147}
8148
8149/*
8150 * "matchend()" function
8151 */
8152 static void
8153f_matchend(typval_T *argvars, typval_T *rettv)
8154{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008155 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008156}
8157
8158/*
8159 * "matchlist()" function
8160 */
8161 static void
8162f_matchlist(typval_T *argvars, typval_T *rettv)
8163{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008164 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008165}
8166
8167/*
8168 * "matchstr()" function
8169 */
8170 static void
8171f_matchstr(typval_T *argvars, typval_T *rettv)
8172{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008173 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008174}
8175
8176/*
8177 * "matchstrpos()" function
8178 */
8179 static void
8180f_matchstrpos(typval_T *argvars, typval_T *rettv)
8181{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008182 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008183}
8184
8185static void max_min(typval_T *argvars, typval_T *rettv, int domax);
8186
8187 static void
8188max_min(typval_T *argvars, typval_T *rettv, int domax)
8189{
8190 varnumber_T n = 0;
8191 varnumber_T i;
8192 int error = FALSE;
8193
8194 if (argvars[0].v_type == VAR_LIST)
8195 {
8196 list_T *l;
8197 listitem_T *li;
8198
8199 l = argvars[0].vval.v_list;
8200 if (l != NULL)
8201 {
8202 li = l->lv_first;
8203 if (li != NULL)
8204 {
8205 n = get_tv_number_chk(&li->li_tv, &error);
8206 for (;;)
8207 {
8208 li = li->li_next;
8209 if (li == NULL)
8210 break;
8211 i = get_tv_number_chk(&li->li_tv, &error);
8212 if (domax ? i > n : i < n)
8213 n = i;
8214 }
8215 }
8216 }
8217 }
8218 else if (argvars[0].v_type == VAR_DICT)
8219 {
8220 dict_T *d;
8221 int first = TRUE;
8222 hashitem_T *hi;
8223 int todo;
8224
8225 d = argvars[0].vval.v_dict;
8226 if (d != NULL)
8227 {
8228 todo = (int)d->dv_hashtab.ht_used;
8229 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8230 {
8231 if (!HASHITEM_EMPTY(hi))
8232 {
8233 --todo;
8234 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
8235 if (first)
8236 {
8237 n = i;
8238 first = FALSE;
8239 }
8240 else if (domax ? i > n : i < n)
8241 n = i;
8242 }
8243 }
8244 }
8245 }
8246 else
Bram Moolenaar26b84332016-09-04 21:42:36 +02008247 EMSG2(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008248 rettv->vval.v_number = error ? 0 : n;
8249}
8250
8251/*
8252 * "max()" function
8253 */
8254 static void
8255f_max(typval_T *argvars, typval_T *rettv)
8256{
8257 max_min(argvars, rettv, TRUE);
8258}
8259
8260/*
8261 * "min()" function
8262 */
8263 static void
8264f_min(typval_T *argvars, typval_T *rettv)
8265{
8266 max_min(argvars, rettv, FALSE);
8267}
8268
8269static int mkdir_recurse(char_u *dir, int prot);
8270
8271/*
8272 * Create the directory in which "dir" is located, and higher levels when
8273 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008274 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008275 */
8276 static int
8277mkdir_recurse(char_u *dir, int prot)
8278{
8279 char_u *p;
8280 char_u *updir;
8281 int r = FAIL;
8282
8283 /* Get end of directory name in "dir".
8284 * We're done when it's "/" or "c:/". */
8285 p = gettail_sep(dir);
8286 if (p <= get_past_head(dir))
8287 return OK;
8288
8289 /* If the directory exists we're done. Otherwise: create it.*/
8290 updir = vim_strnsave(dir, (int)(p - dir));
8291 if (updir == NULL)
8292 return FAIL;
8293 if (mch_isdir(updir))
8294 r = OK;
8295 else if (mkdir_recurse(updir, prot) == OK)
8296 r = vim_mkdir_emsg(updir, prot);
8297 vim_free(updir);
8298 return r;
8299}
8300
8301#ifdef vim_mkdir
8302/*
8303 * "mkdir()" function
8304 */
8305 static void
8306f_mkdir(typval_T *argvars, typval_T *rettv)
8307{
8308 char_u *dir;
8309 char_u buf[NUMBUFLEN];
8310 int prot = 0755;
8311
8312 rettv->vval.v_number = FAIL;
8313 if (check_restricted() || check_secure())
8314 return;
8315
8316 dir = get_tv_string_buf(&argvars[0], buf);
8317 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008318 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008319
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008320 if (*gettail(dir) == NUL)
8321 /* remove trailing slashes */
8322 *gettail_sep(dir) = NUL;
8323
8324 if (argvars[1].v_type != VAR_UNKNOWN)
8325 {
8326 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008327 {
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008328 prot = (int)get_tv_number_chk(&argvars[2], NULL);
8329 if (prot == -1)
8330 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008331 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008332 if (STRCMP(get_tv_string(&argvars[1]), "p") == 0)
8333 {
8334 if (mch_isdir(dir))
8335 {
8336 /* With the "p" flag it's OK if the dir already exists. */
8337 rettv->vval.v_number = OK;
8338 return;
8339 }
8340 mkdir_recurse(dir, prot);
8341 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008342 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008343 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008344}
8345#endif
8346
8347/*
8348 * "mode()" function
8349 */
8350 static void
8351f_mode(typval_T *argvars, typval_T *rettv)
8352{
8353 char_u buf[3];
8354
8355 buf[1] = NUL;
8356 buf[2] = NUL;
8357
8358 if (time_for_testing == 93784)
8359 {
8360 /* Testing the two-character code. */
8361 buf[0] = 'x';
8362 buf[1] = '!';
8363 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008364#ifdef FEAT_TERMINAL
8365 else if (term_use_loop())
8366 buf[0] = 't';
8367#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008368 else if (VIsual_active)
8369 {
8370 if (VIsual_select)
8371 buf[0] = VIsual_mode + 's' - 'v';
8372 else
8373 buf[0] = VIsual_mode;
8374 }
8375 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8376 || State == CONFIRM)
8377 {
8378 buf[0] = 'r';
8379 if (State == ASKMORE)
8380 buf[1] = 'm';
8381 else if (State == CONFIRM)
8382 buf[1] = '?';
8383 }
8384 else if (State == EXTERNCMD)
8385 buf[0] = '!';
8386 else if (State & INSERT)
8387 {
8388#ifdef FEAT_VREPLACE
8389 if (State & VREPLACE_FLAG)
8390 {
8391 buf[0] = 'R';
8392 buf[1] = 'v';
8393 }
8394 else
8395#endif
Bram Moolenaare90858d2017-02-01 17:24:34 +01008396 {
8397 if (State & REPLACE_FLAG)
8398 buf[0] = 'R';
8399 else
8400 buf[0] = 'i';
8401#ifdef FEAT_INS_EXPAND
8402 if (ins_compl_active())
8403 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008404 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008405 buf[1] = 'x';
8406#endif
8407 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008408 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008409 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008410 {
8411 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008412 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008413 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008414 else if (exmode_active == EXMODE_NORMAL)
8415 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008416 }
8417 else
8418 {
8419 buf[0] = 'n';
8420 if (finish_op)
8421 buf[1] = 'o';
8422 }
8423
8424 /* Clear out the minor mode when the argument is not a non-zero number or
8425 * non-empty string. */
8426 if (!non_zero_arg(&argvars[0]))
8427 buf[1] = NUL;
8428
8429 rettv->vval.v_string = vim_strsave(buf);
8430 rettv->v_type = VAR_STRING;
8431}
8432
8433#if defined(FEAT_MZSCHEME) || defined(PROTO)
8434/*
8435 * "mzeval()" function
8436 */
8437 static void
8438f_mzeval(typval_T *argvars, typval_T *rettv)
8439{
8440 char_u *str;
8441 char_u buf[NUMBUFLEN];
8442
8443 str = get_tv_string_buf(&argvars[0], buf);
8444 do_mzeval(str, rettv);
8445}
8446
8447 void
8448mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8449{
8450 typval_T argvars[3];
8451
8452 argvars[0].v_type = VAR_STRING;
8453 argvars[0].vval.v_string = name;
8454 copy_tv(args, &argvars[1]);
8455 argvars[2].v_type = VAR_UNKNOWN;
8456 f_call(argvars, rettv);
8457 clear_tv(&argvars[1]);
8458}
8459#endif
8460
8461/*
8462 * "nextnonblank()" function
8463 */
8464 static void
8465f_nextnonblank(typval_T *argvars, typval_T *rettv)
8466{
8467 linenr_T lnum;
8468
8469 for (lnum = get_tv_lnum(argvars); ; ++lnum)
8470 {
8471 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8472 {
8473 lnum = 0;
8474 break;
8475 }
8476 if (*skipwhite(ml_get(lnum)) != NUL)
8477 break;
8478 }
8479 rettv->vval.v_number = lnum;
8480}
8481
8482/*
8483 * "nr2char()" function
8484 */
8485 static void
8486f_nr2char(typval_T *argvars, typval_T *rettv)
8487{
8488 char_u buf[NUMBUFLEN];
8489
8490#ifdef FEAT_MBYTE
8491 if (has_mbyte)
8492 {
8493 int utf8 = 0;
8494
8495 if (argvars[1].v_type != VAR_UNKNOWN)
8496 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
8497 if (utf8)
8498 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8499 else
8500 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8501 }
8502 else
8503#endif
8504 {
8505 buf[0] = (char_u)get_tv_number(&argvars[0]);
8506 buf[1] = NUL;
8507 }
8508 rettv->v_type = VAR_STRING;
8509 rettv->vval.v_string = vim_strsave(buf);
8510}
8511
8512/*
8513 * "or(expr, expr)" function
8514 */
8515 static void
8516f_or(typval_T *argvars, typval_T *rettv)
8517{
8518 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8519 | get_tv_number_chk(&argvars[1], NULL);
8520}
8521
8522/*
8523 * "pathshorten()" function
8524 */
8525 static void
8526f_pathshorten(typval_T *argvars, typval_T *rettv)
8527{
8528 char_u *p;
8529
8530 rettv->v_type = VAR_STRING;
8531 p = get_tv_string_chk(&argvars[0]);
8532 if (p == NULL)
8533 rettv->vval.v_string = NULL;
8534 else
8535 {
8536 p = vim_strsave(p);
8537 rettv->vval.v_string = p;
8538 if (p != NULL)
8539 shorten_dir(p);
8540 }
8541}
8542
8543#ifdef FEAT_PERL
8544/*
8545 * "perleval()" function
8546 */
8547 static void
8548f_perleval(typval_T *argvars, typval_T *rettv)
8549{
8550 char_u *str;
8551 char_u buf[NUMBUFLEN];
8552
8553 str = get_tv_string_buf(&argvars[0], buf);
8554 do_perleval(str, rettv);
8555}
8556#endif
8557
8558#ifdef FEAT_FLOAT
8559/*
8560 * "pow()" function
8561 */
8562 static void
8563f_pow(typval_T *argvars, typval_T *rettv)
8564{
8565 float_T fx = 0.0, fy = 0.0;
8566
8567 rettv->v_type = VAR_FLOAT;
8568 if (get_float_arg(argvars, &fx) == OK
8569 && get_float_arg(&argvars[1], &fy) == OK)
8570 rettv->vval.v_float = pow(fx, fy);
8571 else
8572 rettv->vval.v_float = 0.0;
8573}
8574#endif
8575
8576/*
8577 * "prevnonblank()" function
8578 */
8579 static void
8580f_prevnonblank(typval_T *argvars, typval_T *rettv)
8581{
8582 linenr_T lnum;
8583
8584 lnum = get_tv_lnum(argvars);
8585 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8586 lnum = 0;
8587 else
8588 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8589 --lnum;
8590 rettv->vval.v_number = lnum;
8591}
8592
8593/* This dummy va_list is here because:
8594 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8595 * - locally in the function results in a "used before set" warning
8596 * - using va_start() to initialize it gives "function with fixed args" error */
8597static va_list ap;
8598
8599/*
8600 * "printf()" function
8601 */
8602 static void
8603f_printf(typval_T *argvars, typval_T *rettv)
8604{
8605 char_u buf[NUMBUFLEN];
8606 int len;
8607 char_u *s;
8608 int saved_did_emsg = did_emsg;
8609 char *fmt;
8610
8611 rettv->v_type = VAR_STRING;
8612 rettv->vval.v_string = NULL;
8613
8614 /* Get the required length, allocate the buffer and do it for real. */
8615 did_emsg = FALSE;
8616 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008617 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008618 if (!did_emsg)
8619 {
8620 s = alloc(len + 1);
8621 if (s != NULL)
8622 {
8623 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008624 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8625 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008626 }
8627 }
8628 did_emsg |= saved_did_emsg;
8629}
8630
Bram Moolenaarf2732452018-06-03 14:47:35 +02008631#ifdef FEAT_JOB_CHANNEL
8632/*
8633 * "prompt_setcallback({buffer}, {callback})" function
8634 */
8635 static void
8636f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
8637{
8638 buf_T *buf;
8639 char_u *callback;
8640 partial_T *partial;
8641
8642 if (check_secure())
8643 return;
8644 buf = get_buf_tv(&argvars[0], FALSE);
8645 if (buf == NULL)
8646 return;
8647
8648 callback = get_callback(&argvars[1], &partial);
8649 if (callback == NULL)
8650 return;
8651
8652 free_callback(buf->b_prompt_callback, buf->b_prompt_partial);
8653 if (partial == NULL)
8654 buf->b_prompt_callback = vim_strsave(callback);
8655 else
8656 /* pointer into the partial */
8657 buf->b_prompt_callback = callback;
8658 buf->b_prompt_partial = partial;
8659}
8660
8661/*
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02008662 * "prompt_setinterrupt({buffer}, {callback})" function
8663 */
8664 static void
8665f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
8666{
8667 buf_T *buf;
8668 char_u *callback;
8669 partial_T *partial;
8670
8671 if (check_secure())
8672 return;
8673 buf = get_buf_tv(&argvars[0], FALSE);
8674 if (buf == NULL)
8675 return;
8676
8677 callback = get_callback(&argvars[1], &partial);
8678 if (callback == NULL)
8679 return;
8680
8681 free_callback(buf->b_prompt_interrupt, buf->b_prompt_int_partial);
8682 if (partial == NULL)
8683 buf->b_prompt_interrupt = vim_strsave(callback);
8684 else
8685 /* pointer into the partial */
8686 buf->b_prompt_interrupt = callback;
8687 buf->b_prompt_int_partial = partial;
8688}
8689
8690/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02008691 * "prompt_setprompt({buffer}, {text})" function
8692 */
8693 static void
8694f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
8695{
8696 buf_T *buf;
8697 char_u *text;
8698
8699 if (check_secure())
8700 return;
8701 buf = get_buf_tv(&argvars[0], FALSE);
8702 if (buf == NULL)
8703 return;
8704
8705 text = get_tv_string(&argvars[1]);
8706 vim_free(buf->b_prompt_text);
8707 buf->b_prompt_text = vim_strsave(text);
8708}
8709#endif
8710
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008711/*
8712 * "pumvisible()" function
8713 */
8714 static void
8715f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8716{
8717#ifdef FEAT_INS_EXPAND
8718 if (pum_visible())
8719 rettv->vval.v_number = 1;
8720#endif
8721}
8722
8723#ifdef FEAT_PYTHON3
8724/*
8725 * "py3eval()" function
8726 */
8727 static void
8728f_py3eval(typval_T *argvars, typval_T *rettv)
8729{
8730 char_u *str;
8731 char_u buf[NUMBUFLEN];
8732
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008733 if (p_pyx == 0)
8734 p_pyx = 3;
8735
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008736 str = get_tv_string_buf(&argvars[0], buf);
8737 do_py3eval(str, rettv);
8738}
8739#endif
8740
8741#ifdef FEAT_PYTHON
8742/*
8743 * "pyeval()" function
8744 */
8745 static void
8746f_pyeval(typval_T *argvars, typval_T *rettv)
8747{
8748 char_u *str;
8749 char_u buf[NUMBUFLEN];
8750
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008751 if (p_pyx == 0)
8752 p_pyx = 2;
8753
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008754 str = get_tv_string_buf(&argvars[0], buf);
8755 do_pyeval(str, rettv);
8756}
8757#endif
8758
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008759#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8760/*
8761 * "pyxeval()" function
8762 */
8763 static void
8764f_pyxeval(typval_T *argvars, typval_T *rettv)
8765{
8766# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8767 init_pyxversion();
8768 if (p_pyx == 2)
8769 f_pyeval(argvars, rettv);
8770 else
8771 f_py3eval(argvars, rettv);
8772# elif defined(FEAT_PYTHON)
8773 f_pyeval(argvars, rettv);
8774# elif defined(FEAT_PYTHON3)
8775 f_py3eval(argvars, rettv);
8776# endif
8777}
8778#endif
8779
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008780/*
8781 * "range()" function
8782 */
8783 static void
8784f_range(typval_T *argvars, typval_T *rettv)
8785{
8786 varnumber_T start;
8787 varnumber_T end;
8788 varnumber_T stride = 1;
8789 varnumber_T i;
8790 int error = FALSE;
8791
8792 start = get_tv_number_chk(&argvars[0], &error);
8793 if (argvars[1].v_type == VAR_UNKNOWN)
8794 {
8795 end = start - 1;
8796 start = 0;
8797 }
8798 else
8799 {
8800 end = get_tv_number_chk(&argvars[1], &error);
8801 if (argvars[2].v_type != VAR_UNKNOWN)
8802 stride = get_tv_number_chk(&argvars[2], &error);
8803 }
8804
8805 if (error)
8806 return; /* type error; errmsg already given */
8807 if (stride == 0)
8808 EMSG(_("E726: Stride is zero"));
8809 else if (stride > 0 ? end + 1 < start : end - 1 > start)
8810 EMSG(_("E727: Start past end"));
8811 else
8812 {
8813 if (rettv_list_alloc(rettv) == OK)
8814 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8815 if (list_append_number(rettv->vval.v_list,
8816 (varnumber_T)i) == FAIL)
8817 break;
8818 }
8819}
8820
8821/*
8822 * "readfile()" function
8823 */
8824 static void
8825f_readfile(typval_T *argvars, typval_T *rettv)
8826{
8827 int binary = FALSE;
8828 int failed = FALSE;
8829 char_u *fname;
8830 FILE *fd;
8831 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
8832 int io_size = sizeof(buf);
8833 int readlen; /* size of last fread() */
8834 char_u *prev = NULL; /* previously read bytes, if any */
8835 long prevlen = 0; /* length of data in prev */
8836 long prevsize = 0; /* size of prev buffer */
8837 long maxline = MAXLNUM;
8838 long cnt = 0;
8839 char_u *p; /* position in buf */
8840 char_u *start; /* start of current line */
8841
8842 if (argvars[1].v_type != VAR_UNKNOWN)
8843 {
8844 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
8845 binary = TRUE;
8846 if (argvars[2].v_type != VAR_UNKNOWN)
8847 maxline = (long)get_tv_number(&argvars[2]);
8848 }
8849
8850 if (rettv_list_alloc(rettv) == FAIL)
8851 return;
8852
8853 /* Always open the file in binary mode, library functions have a mind of
8854 * their own about CR-LF conversion. */
8855 fname = get_tv_string(&argvars[0]);
8856 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
8857 {
8858 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
8859 return;
8860 }
8861
8862 while (cnt < maxline || maxline < 0)
8863 {
8864 readlen = (int)fread(buf, 1, io_size, fd);
8865
8866 /* This for loop processes what was read, but is also entered at end
8867 * of file so that either:
8868 * - an incomplete line gets written
8869 * - a "binary" file gets an empty line at the end if it ends in a
8870 * newline. */
8871 for (p = buf, start = buf;
8872 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
8873 ++p)
8874 {
8875 if (*p == '\n' || readlen <= 0)
8876 {
8877 listitem_T *li;
8878 char_u *s = NULL;
8879 long_u len = p - start;
8880
8881 /* Finished a line. Remove CRs before NL. */
8882 if (readlen > 0 && !binary)
8883 {
8884 while (len > 0 && start[len - 1] == '\r')
8885 --len;
8886 /* removal may cross back to the "prev" string */
8887 if (len == 0)
8888 while (prevlen > 0 && prev[prevlen - 1] == '\r')
8889 --prevlen;
8890 }
8891 if (prevlen == 0)
8892 s = vim_strnsave(start, (int)len);
8893 else
8894 {
8895 /* Change "prev" buffer to be the right size. This way
8896 * the bytes are only copied once, and very long lines are
8897 * allocated only once. */
8898 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
8899 {
8900 mch_memmove(s + prevlen, start, len);
8901 s[prevlen + len] = NUL;
8902 prev = NULL; /* the list will own the string */
8903 prevlen = prevsize = 0;
8904 }
8905 }
8906 if (s == NULL)
8907 {
8908 do_outofmem_msg((long_u) prevlen + len + 1);
8909 failed = TRUE;
8910 break;
8911 }
8912
8913 if ((li = listitem_alloc()) == NULL)
8914 {
8915 vim_free(s);
8916 failed = TRUE;
8917 break;
8918 }
8919 li->li_tv.v_type = VAR_STRING;
8920 li->li_tv.v_lock = 0;
8921 li->li_tv.vval.v_string = s;
8922 list_append(rettv->vval.v_list, li);
8923
8924 start = p + 1; /* step over newline */
8925 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
8926 break;
8927 }
8928 else if (*p == NUL)
8929 *p = '\n';
8930#ifdef FEAT_MBYTE
8931 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
8932 * when finding the BF and check the previous two bytes. */
8933 else if (*p == 0xbf && enc_utf8 && !binary)
8934 {
8935 /* Find the two bytes before the 0xbf. If p is at buf, or buf
8936 * + 1, these may be in the "prev" string. */
8937 char_u back1 = p >= buf + 1 ? p[-1]
8938 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
8939 char_u back2 = p >= buf + 2 ? p[-2]
8940 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
8941 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
8942
8943 if (back2 == 0xef && back1 == 0xbb)
8944 {
8945 char_u *dest = p - 2;
8946
8947 /* Usually a BOM is at the beginning of a file, and so at
8948 * the beginning of a line; then we can just step over it.
8949 */
8950 if (start == dest)
8951 start = p + 1;
8952 else
8953 {
8954 /* have to shuffle buf to close gap */
8955 int adjust_prevlen = 0;
8956
8957 if (dest < buf)
8958 {
8959 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
8960 dest = buf;
8961 }
8962 if (readlen > p - buf + 1)
8963 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
8964 readlen -= 3 - adjust_prevlen;
8965 prevlen -= adjust_prevlen;
8966 p = dest - 1;
8967 }
8968 }
8969 }
8970#endif
8971 } /* for */
8972
8973 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
8974 break;
8975 if (start < p)
8976 {
8977 /* There's part of a line in buf, store it in "prev". */
8978 if (p - start + prevlen >= prevsize)
8979 {
8980 /* need bigger "prev" buffer */
8981 char_u *newprev;
8982
8983 /* A common use case is ordinary text files and "prev" gets a
8984 * fragment of a line, so the first allocation is made
8985 * small, to avoid repeatedly 'allocing' large and
8986 * 'reallocing' small. */
8987 if (prevsize == 0)
8988 prevsize = (long)(p - start);
8989 else
8990 {
8991 long grow50pc = (prevsize * 3) / 2;
8992 long growmin = (long)((p - start) * 2 + prevlen);
8993 prevsize = grow50pc > growmin ? grow50pc : growmin;
8994 }
8995 newprev = prev == NULL ? alloc(prevsize)
8996 : vim_realloc(prev, prevsize);
8997 if (newprev == NULL)
8998 {
8999 do_outofmem_msg((long_u)prevsize);
9000 failed = TRUE;
9001 break;
9002 }
9003 prev = newprev;
9004 }
9005 /* Add the line part to end of "prev". */
9006 mch_memmove(prev + prevlen, start, p - start);
9007 prevlen += (long)(p - start);
9008 }
9009 } /* while */
9010
9011 /*
9012 * For a negative line count use only the lines at the end of the file,
9013 * free the rest.
9014 */
9015 if (!failed && maxline < 0)
9016 while (cnt > -maxline)
9017 {
9018 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
9019 --cnt;
9020 }
9021
9022 if (failed)
9023 {
9024 list_free(rettv->vval.v_list);
9025 /* readfile doc says an empty list is returned on error */
9026 rettv->vval.v_list = list_alloc();
9027 }
9028
9029 vim_free(prev);
9030 fclose(fd);
9031}
9032
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009033 static void
9034return_register(int regname, typval_T *rettv)
9035{
9036 char_u buf[2] = {0, 0};
9037
9038 buf[0] = (char_u)regname;
9039 rettv->v_type = VAR_STRING;
9040 rettv->vval.v_string = vim_strsave(buf);
9041}
9042
9043/*
9044 * "reg_executing()" function
9045 */
9046 static void
9047f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9048{
9049 return_register(reg_executing, rettv);
9050}
9051
9052/*
9053 * "reg_recording()" function
9054 */
9055 static void
9056f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9057{
9058 return_register(reg_recording, rettv);
9059}
9060
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009061#if defined(FEAT_RELTIME)
9062static int list2proftime(typval_T *arg, proftime_T *tm);
9063
9064/*
9065 * Convert a List to proftime_T.
9066 * Return FAIL when there is something wrong.
9067 */
9068 static int
9069list2proftime(typval_T *arg, proftime_T *tm)
9070{
9071 long n1, n2;
9072 int error = FALSE;
9073
9074 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9075 || arg->vval.v_list->lv_len != 2)
9076 return FAIL;
9077 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9078 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
9079# ifdef WIN3264
9080 tm->HighPart = n1;
9081 tm->LowPart = n2;
9082# else
9083 tm->tv_sec = n1;
9084 tm->tv_usec = n2;
9085# endif
9086 return error ? FAIL : OK;
9087}
9088#endif /* FEAT_RELTIME */
9089
9090/*
9091 * "reltime()" function
9092 */
9093 static void
9094f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9095{
9096#ifdef FEAT_RELTIME
9097 proftime_T res;
9098 proftime_T start;
9099
9100 if (argvars[0].v_type == VAR_UNKNOWN)
9101 {
9102 /* No arguments: get current time. */
9103 profile_start(&res);
9104 }
9105 else if (argvars[1].v_type == VAR_UNKNOWN)
9106 {
9107 if (list2proftime(&argvars[0], &res) == FAIL)
9108 return;
9109 profile_end(&res);
9110 }
9111 else
9112 {
9113 /* Two arguments: compute the difference. */
9114 if (list2proftime(&argvars[0], &start) == FAIL
9115 || list2proftime(&argvars[1], &res) == FAIL)
9116 return;
9117 profile_sub(&res, &start);
9118 }
9119
9120 if (rettv_list_alloc(rettv) == OK)
9121 {
9122 long n1, n2;
9123
9124# ifdef WIN3264
9125 n1 = res.HighPart;
9126 n2 = res.LowPart;
9127# else
9128 n1 = res.tv_sec;
9129 n2 = res.tv_usec;
9130# endif
9131 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9132 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9133 }
9134#endif
9135}
9136
9137#ifdef FEAT_FLOAT
9138/*
9139 * "reltimefloat()" function
9140 */
9141 static void
9142f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9143{
9144# ifdef FEAT_RELTIME
9145 proftime_T tm;
9146# endif
9147
9148 rettv->v_type = VAR_FLOAT;
9149 rettv->vval.v_float = 0;
9150# ifdef FEAT_RELTIME
9151 if (list2proftime(&argvars[0], &tm) == OK)
9152 rettv->vval.v_float = profile_float(&tm);
9153# endif
9154}
9155#endif
9156
9157/*
9158 * "reltimestr()" function
9159 */
9160 static void
9161f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9162{
9163#ifdef FEAT_RELTIME
9164 proftime_T tm;
9165#endif
9166
9167 rettv->v_type = VAR_STRING;
9168 rettv->vval.v_string = NULL;
9169#ifdef FEAT_RELTIME
9170 if (list2proftime(&argvars[0], &tm) == OK)
9171 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9172#endif
9173}
9174
9175#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
9176static void make_connection(void);
9177static int check_connection(void);
9178
9179 static void
9180make_connection(void)
9181{
9182 if (X_DISPLAY == NULL
9183# ifdef FEAT_GUI
9184 && !gui.in_use
9185# endif
9186 )
9187 {
9188 x_force_connect = TRUE;
9189 setup_term_clip();
9190 x_force_connect = FALSE;
9191 }
9192}
9193
9194 static int
9195check_connection(void)
9196{
9197 make_connection();
9198 if (X_DISPLAY == NULL)
9199 {
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009200 EMSG(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009201 return FAIL;
9202 }
9203 return OK;
9204}
9205#endif
9206
9207#ifdef FEAT_CLIENTSERVER
9208 static void
9209remote_common(typval_T *argvars, typval_T *rettv, int expr)
9210{
9211 char_u *server_name;
9212 char_u *keys;
9213 char_u *r = NULL;
9214 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009215 int timeout = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009216# ifdef WIN32
9217 HWND w;
9218# else
9219 Window w;
9220# endif
9221
9222 if (check_restricted() || check_secure())
9223 return;
9224
9225# ifdef FEAT_X11
9226 if (check_connection() == FAIL)
9227 return;
9228# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009229 if (argvars[2].v_type != VAR_UNKNOWN
9230 && argvars[3].v_type != VAR_UNKNOWN)
9231 timeout = get_tv_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009232
9233 server_name = get_tv_string_chk(&argvars[0]);
9234 if (server_name == NULL)
9235 return; /* type error; errmsg already given */
9236 keys = get_tv_string_buf(&argvars[1], buf);
9237# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009238 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009239# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009240 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9241 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009242# endif
9243 {
9244 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009245 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009246 EMSG(r); /* sending worked but evaluation failed */
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009247 vim_free(r);
9248 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009249 else
9250 EMSG2(_("E241: Unable to send to %s"), server_name);
9251 return;
9252 }
9253
9254 rettv->vval.v_string = r;
9255
9256 if (argvars[2].v_type != VAR_UNKNOWN)
9257 {
9258 dictitem_T v;
9259 char_u str[30];
9260 char_u *idvar;
9261
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009262 idvar = get_tv_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009263 if (idvar != NULL && *idvar != NUL)
9264 {
9265 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9266 v.di_tv.v_type = VAR_STRING;
9267 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009268 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009269 vim_free(v.di_tv.vval.v_string);
9270 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009271 }
9272}
9273#endif
9274
9275/*
9276 * "remote_expr()" function
9277 */
9278 static void
9279f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9280{
9281 rettv->v_type = VAR_STRING;
9282 rettv->vval.v_string = NULL;
9283#ifdef FEAT_CLIENTSERVER
9284 remote_common(argvars, rettv, TRUE);
9285#endif
9286}
9287
9288/*
9289 * "remote_foreground()" function
9290 */
9291 static void
9292f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9293{
9294#ifdef FEAT_CLIENTSERVER
9295# ifdef WIN32
9296 /* On Win32 it's done in this application. */
9297 {
9298 char_u *server_name = get_tv_string_chk(&argvars[0]);
9299
9300 if (server_name != NULL)
9301 serverForeground(server_name);
9302 }
9303# else
9304 /* Send a foreground() expression to the server. */
9305 argvars[1].v_type = VAR_STRING;
9306 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9307 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009308 rettv->v_type = VAR_STRING;
9309 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009310 remote_common(argvars, rettv, TRUE);
9311 vim_free(argvars[1].vval.v_string);
9312# endif
9313#endif
9314}
9315
9316 static void
9317f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
9318{
9319#ifdef FEAT_CLIENTSERVER
9320 dictitem_T v;
9321 char_u *s = NULL;
9322# ifdef WIN32
9323 long_u n = 0;
9324# endif
9325 char_u *serverid;
9326
9327 if (check_restricted() || check_secure())
9328 {
9329 rettv->vval.v_number = -1;
9330 return;
9331 }
9332 serverid = get_tv_string_chk(&argvars[0]);
9333 if (serverid == NULL)
9334 {
9335 rettv->vval.v_number = -1;
9336 return; /* type error; errmsg already given */
9337 }
9338# ifdef WIN32
9339 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
9340 if (n == 0)
9341 rettv->vval.v_number = -1;
9342 else
9343 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009344 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009345 rettv->vval.v_number = (s != NULL);
9346 }
9347# else
9348 if (check_connection() == FAIL)
9349 return;
9350
9351 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
9352 serverStrToWin(serverid), &s);
9353# endif
9354
9355 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
9356 {
9357 char_u *retvar;
9358
9359 v.di_tv.v_type = VAR_STRING;
9360 v.di_tv.vval.v_string = vim_strsave(s);
9361 retvar = get_tv_string_chk(&argvars[1]);
9362 if (retvar != NULL)
9363 set_var(retvar, &v.di_tv, FALSE);
9364 vim_free(v.di_tv.vval.v_string);
9365 }
9366#else
9367 rettv->vval.v_number = -1;
9368#endif
9369}
9370
9371 static void
9372f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
9373{
9374 char_u *r = NULL;
9375
9376#ifdef FEAT_CLIENTSERVER
9377 char_u *serverid = get_tv_string_chk(&argvars[0]);
9378
9379 if (serverid != NULL && !check_restricted() && !check_secure())
9380 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009381 int timeout = 0;
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009382# ifdef WIN32
9383 /* The server's HWND is encoded in the 'id' parameter */
9384 long_u n = 0;
9385# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009386
9387 if (argvars[1].v_type != VAR_UNKNOWN)
9388 timeout = get_tv_number(&argvars[1]);
9389
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009390# ifdef WIN32
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009391 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
9392 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009393 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009394 if (r == NULL)
9395# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009396 if (check_connection() == FAIL
9397 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
9398 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009399# endif
9400 EMSG(_("E277: Unable to read a server reply"));
9401 }
9402#endif
9403 rettv->v_type = VAR_STRING;
9404 rettv->vval.v_string = r;
9405}
9406
9407/*
9408 * "remote_send()" function
9409 */
9410 static void
9411f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9412{
9413 rettv->v_type = VAR_STRING;
9414 rettv->vval.v_string = NULL;
9415#ifdef FEAT_CLIENTSERVER
9416 remote_common(argvars, rettv, FALSE);
9417#endif
9418}
9419
9420/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009421 * "remote_startserver()" function
9422 */
9423 static void
9424f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9425{
9426#ifdef FEAT_CLIENTSERVER
9427 char_u *server = get_tv_string_chk(&argvars[0]);
9428
9429 if (server == NULL)
9430 return; /* type error; errmsg already given */
9431 if (serverName != NULL)
9432 EMSG(_("E941: already started a server"));
9433 else
9434 {
9435# ifdef FEAT_X11
9436 if (check_connection() == OK)
9437 serverRegisterName(X_DISPLAY, server);
9438# else
9439 serverSetName(server);
9440# endif
9441 }
9442#else
9443 EMSG(_("E942: +clientserver feature not available"));
9444#endif
9445}
9446
9447/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009448 * "remove()" function
9449 */
9450 static void
9451f_remove(typval_T *argvars, typval_T *rettv)
9452{
9453 list_T *l;
9454 listitem_T *item, *item2;
9455 listitem_T *li;
9456 long idx;
9457 long end;
9458 char_u *key;
9459 dict_T *d;
9460 dictitem_T *di;
9461 char_u *arg_errmsg = (char_u *)N_("remove() argument");
9462
9463 if (argvars[0].v_type == VAR_DICT)
9464 {
9465 if (argvars[2].v_type != VAR_UNKNOWN)
9466 EMSG2(_(e_toomanyarg), "remove()");
9467 else if ((d = argvars[0].vval.v_dict) != NULL
9468 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
9469 {
9470 key = get_tv_string_chk(&argvars[1]);
9471 if (key != NULL)
9472 {
9473 di = dict_find(d, key, -1);
9474 if (di == NULL)
9475 EMSG2(_(e_dictkey), key);
9476 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
9477 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
9478 {
9479 *rettv = di->di_tv;
9480 init_tv(&di->di_tv);
9481 dictitem_remove(d, di);
9482 }
9483 }
9484 }
9485 }
9486 else if (argvars[0].v_type != VAR_LIST)
9487 EMSG2(_(e_listdictarg), "remove()");
9488 else if ((l = argvars[0].vval.v_list) != NULL
9489 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
9490 {
9491 int error = FALSE;
9492
9493 idx = (long)get_tv_number_chk(&argvars[1], &error);
9494 if (error)
9495 ; /* type error: do nothing, errmsg already given */
9496 else if ((item = list_find(l, idx)) == NULL)
9497 EMSGN(_(e_listidx), idx);
9498 else
9499 {
9500 if (argvars[2].v_type == VAR_UNKNOWN)
9501 {
9502 /* Remove one item, return its value. */
9503 vimlist_remove(l, item, item);
9504 *rettv = item->li_tv;
9505 vim_free(item);
9506 }
9507 else
9508 {
9509 /* Remove range of items, return list with values. */
9510 end = (long)get_tv_number_chk(&argvars[2], &error);
9511 if (error)
9512 ; /* type error: do nothing */
9513 else if ((item2 = list_find(l, end)) == NULL)
9514 EMSGN(_(e_listidx), end);
9515 else
9516 {
9517 int cnt = 0;
9518
9519 for (li = item; li != NULL; li = li->li_next)
9520 {
9521 ++cnt;
9522 if (li == item2)
9523 break;
9524 }
9525 if (li == NULL) /* didn't find "item2" after "item" */
9526 EMSG(_(e_invrange));
9527 else
9528 {
9529 vimlist_remove(l, item, item2);
9530 if (rettv_list_alloc(rettv) == OK)
9531 {
9532 l = rettv->vval.v_list;
9533 l->lv_first = item;
9534 l->lv_last = item2;
9535 item->li_prev = NULL;
9536 item2->li_next = NULL;
9537 l->lv_len = cnt;
9538 }
9539 }
9540 }
9541 }
9542 }
9543 }
9544}
9545
9546/*
9547 * "rename({from}, {to})" function
9548 */
9549 static void
9550f_rename(typval_T *argvars, typval_T *rettv)
9551{
9552 char_u buf[NUMBUFLEN];
9553
9554 if (check_restricted() || check_secure())
9555 rettv->vval.v_number = -1;
9556 else
9557 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
9558 get_tv_string_buf(&argvars[1], buf));
9559}
9560
9561/*
9562 * "repeat()" function
9563 */
9564 static void
9565f_repeat(typval_T *argvars, typval_T *rettv)
9566{
9567 char_u *p;
9568 int n;
9569 int slen;
9570 int len;
9571 char_u *r;
9572 int i;
9573
9574 n = (int)get_tv_number(&argvars[1]);
9575 if (argvars[0].v_type == VAR_LIST)
9576 {
9577 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9578 while (n-- > 0)
9579 if (list_extend(rettv->vval.v_list,
9580 argvars[0].vval.v_list, NULL) == FAIL)
9581 break;
9582 }
9583 else
9584 {
9585 p = get_tv_string(&argvars[0]);
9586 rettv->v_type = VAR_STRING;
9587 rettv->vval.v_string = NULL;
9588
9589 slen = (int)STRLEN(p);
9590 len = slen * n;
9591 if (len <= 0)
9592 return;
9593
9594 r = alloc(len + 1);
9595 if (r != NULL)
9596 {
9597 for (i = 0; i < n; i++)
9598 mch_memmove(r + i * slen, p, (size_t)slen);
9599 r[len] = NUL;
9600 }
9601
9602 rettv->vval.v_string = r;
9603 }
9604}
9605
9606/*
9607 * "resolve()" function
9608 */
9609 static void
9610f_resolve(typval_T *argvars, typval_T *rettv)
9611{
9612 char_u *p;
9613#ifdef HAVE_READLINK
9614 char_u *buf = NULL;
9615#endif
9616
9617 p = get_tv_string(&argvars[0]);
9618#ifdef FEAT_SHORTCUT
9619 {
9620 char_u *v = NULL;
9621
9622 v = mch_resolve_shortcut(p);
9623 if (v != NULL)
9624 rettv->vval.v_string = v;
9625 else
9626 rettv->vval.v_string = vim_strsave(p);
9627 }
9628#else
9629# ifdef HAVE_READLINK
9630 {
9631 char_u *cpy;
9632 int len;
9633 char_u *remain = NULL;
9634 char_u *q;
9635 int is_relative_to_current = FALSE;
9636 int has_trailing_pathsep = FALSE;
9637 int limit = 100;
9638
9639 p = vim_strsave(p);
9640
9641 if (p[0] == '.' && (vim_ispathsep(p[1])
9642 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9643 is_relative_to_current = TRUE;
9644
9645 len = STRLEN(p);
9646 if (len > 0 && after_pathsep(p, p + len))
9647 {
9648 has_trailing_pathsep = TRUE;
9649 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9650 }
9651
9652 q = getnextcomp(p);
9653 if (*q != NUL)
9654 {
9655 /* Separate the first path component in "p", and keep the
9656 * remainder (beginning with the path separator). */
9657 remain = vim_strsave(q - 1);
9658 q[-1] = NUL;
9659 }
9660
9661 buf = alloc(MAXPATHL + 1);
9662 if (buf == NULL)
9663 goto fail;
9664
9665 for (;;)
9666 {
9667 for (;;)
9668 {
9669 len = readlink((char *)p, (char *)buf, MAXPATHL);
9670 if (len <= 0)
9671 break;
9672 buf[len] = NUL;
9673
9674 if (limit-- == 0)
9675 {
9676 vim_free(p);
9677 vim_free(remain);
9678 EMSG(_("E655: Too many symbolic links (cycle?)"));
9679 rettv->vval.v_string = NULL;
9680 goto fail;
9681 }
9682
9683 /* Ensure that the result will have a trailing path separator
9684 * if the argument has one. */
9685 if (remain == NULL && has_trailing_pathsep)
9686 add_pathsep(buf);
9687
9688 /* Separate the first path component in the link value and
9689 * concatenate the remainders. */
9690 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9691 if (*q != NUL)
9692 {
9693 if (remain == NULL)
9694 remain = vim_strsave(q - 1);
9695 else
9696 {
9697 cpy = concat_str(q - 1, remain);
9698 if (cpy != NULL)
9699 {
9700 vim_free(remain);
9701 remain = cpy;
9702 }
9703 }
9704 q[-1] = NUL;
9705 }
9706
9707 q = gettail(p);
9708 if (q > p && *q == NUL)
9709 {
9710 /* Ignore trailing path separator. */
9711 q[-1] = NUL;
9712 q = gettail(p);
9713 }
9714 if (q > p && !mch_isFullName(buf))
9715 {
9716 /* symlink is relative to directory of argument */
9717 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
9718 if (cpy != NULL)
9719 {
9720 STRCPY(cpy, p);
9721 STRCPY(gettail(cpy), buf);
9722 vim_free(p);
9723 p = cpy;
9724 }
9725 }
9726 else
9727 {
9728 vim_free(p);
9729 p = vim_strsave(buf);
9730 }
9731 }
9732
9733 if (remain == NULL)
9734 break;
9735
9736 /* Append the first path component of "remain" to "p". */
9737 q = getnextcomp(remain + 1);
9738 len = q - remain - (*q != NUL);
9739 cpy = vim_strnsave(p, STRLEN(p) + len);
9740 if (cpy != NULL)
9741 {
9742 STRNCAT(cpy, remain, len);
9743 vim_free(p);
9744 p = cpy;
9745 }
9746 /* Shorten "remain". */
9747 if (*q != NUL)
9748 STRMOVE(remain, q - 1);
9749 else
Bram Moolenaard23a8232018-02-10 18:45:26 +01009750 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009751 }
9752
9753 /* If the result is a relative path name, make it explicitly relative to
9754 * the current directory if and only if the argument had this form. */
9755 if (!vim_ispathsep(*p))
9756 {
9757 if (is_relative_to_current
9758 && *p != NUL
9759 && !(p[0] == '.'
9760 && (p[1] == NUL
9761 || vim_ispathsep(p[1])
9762 || (p[1] == '.'
9763 && (p[2] == NUL
9764 || vim_ispathsep(p[2]))))))
9765 {
9766 /* Prepend "./". */
9767 cpy = concat_str((char_u *)"./", p);
9768 if (cpy != NULL)
9769 {
9770 vim_free(p);
9771 p = cpy;
9772 }
9773 }
9774 else if (!is_relative_to_current)
9775 {
9776 /* Strip leading "./". */
9777 q = p;
9778 while (q[0] == '.' && vim_ispathsep(q[1]))
9779 q += 2;
9780 if (q > p)
9781 STRMOVE(p, p + 2);
9782 }
9783 }
9784
9785 /* Ensure that the result will have no trailing path separator
9786 * if the argument had none. But keep "/" or "//". */
9787 if (!has_trailing_pathsep)
9788 {
9789 q = p + STRLEN(p);
9790 if (after_pathsep(p, q))
9791 *gettail_sep(p) = NUL;
9792 }
9793
9794 rettv->vval.v_string = p;
9795 }
9796# else
9797 rettv->vval.v_string = vim_strsave(p);
9798# endif
9799#endif
9800
9801 simplify_filename(rettv->vval.v_string);
9802
9803#ifdef HAVE_READLINK
9804fail:
9805 vim_free(buf);
9806#endif
9807 rettv->v_type = VAR_STRING;
9808}
9809
9810/*
9811 * "reverse({list})" function
9812 */
9813 static void
9814f_reverse(typval_T *argvars, typval_T *rettv)
9815{
9816 list_T *l;
9817 listitem_T *li, *ni;
9818
9819 if (argvars[0].v_type != VAR_LIST)
9820 EMSG2(_(e_listarg), "reverse()");
9821 else if ((l = argvars[0].vval.v_list) != NULL
9822 && !tv_check_lock(l->lv_lock,
9823 (char_u *)N_("reverse() argument"), TRUE))
9824 {
9825 li = l->lv_last;
9826 l->lv_first = l->lv_last = NULL;
9827 l->lv_len = 0;
9828 while (li != NULL)
9829 {
9830 ni = li->li_prev;
9831 list_append(l, li);
9832 li = ni;
9833 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009834 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009835 l->lv_idx = l->lv_len - l->lv_idx - 1;
9836 }
9837}
9838
9839#define SP_NOMOVE 0x01 /* don't move cursor */
9840#define SP_REPEAT 0x02 /* repeat to find outer pair */
9841#define SP_RETCOUNT 0x04 /* return matchcount */
9842#define SP_SETPCMARK 0x08 /* set previous context mark */
9843#define SP_START 0x10 /* accept match at start position */
9844#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
9845#define SP_END 0x40 /* leave cursor at end of match */
9846#define SP_COLUMN 0x80 /* start at cursor column */
9847
9848static int get_search_arg(typval_T *varp, int *flagsp);
9849
9850/*
9851 * Get flags for a search function.
9852 * Possibly sets "p_ws".
9853 * Returns BACKWARD, FORWARD or zero (for an error).
9854 */
9855 static int
9856get_search_arg(typval_T *varp, int *flagsp)
9857{
9858 int dir = FORWARD;
9859 char_u *flags;
9860 char_u nbuf[NUMBUFLEN];
9861 int mask;
9862
9863 if (varp->v_type != VAR_UNKNOWN)
9864 {
9865 flags = get_tv_string_buf_chk(varp, nbuf);
9866 if (flags == NULL)
9867 return 0; /* type error; errmsg already given */
9868 while (*flags != NUL)
9869 {
9870 switch (*flags)
9871 {
9872 case 'b': dir = BACKWARD; break;
9873 case 'w': p_ws = TRUE; break;
9874 case 'W': p_ws = FALSE; break;
9875 default: mask = 0;
9876 if (flagsp != NULL)
9877 switch (*flags)
9878 {
9879 case 'c': mask = SP_START; break;
9880 case 'e': mask = SP_END; break;
9881 case 'm': mask = SP_RETCOUNT; break;
9882 case 'n': mask = SP_NOMOVE; break;
9883 case 'p': mask = SP_SUBPAT; break;
9884 case 'r': mask = SP_REPEAT; break;
9885 case 's': mask = SP_SETPCMARK; break;
9886 case 'z': mask = SP_COLUMN; break;
9887 }
9888 if (mask == 0)
9889 {
9890 EMSG2(_(e_invarg2), flags);
9891 dir = 0;
9892 }
9893 else
9894 *flagsp |= mask;
9895 }
9896 if (dir == 0)
9897 break;
9898 ++flags;
9899 }
9900 }
9901 return dir;
9902}
9903
9904/*
9905 * Shared by search() and searchpos() functions.
9906 */
9907 static int
9908search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
9909{
9910 int flags;
9911 char_u *pat;
9912 pos_T pos;
9913 pos_T save_cursor;
9914 int save_p_ws = p_ws;
9915 int dir;
9916 int retval = 0; /* default: FAIL */
9917 long lnum_stop = 0;
9918 proftime_T tm;
9919#ifdef FEAT_RELTIME
9920 long time_limit = 0;
9921#endif
9922 int options = SEARCH_KEEP;
9923 int subpatnum;
9924
9925 pat = get_tv_string(&argvars[0]);
9926 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
9927 if (dir == 0)
9928 goto theend;
9929 flags = *flagsp;
9930 if (flags & SP_START)
9931 options |= SEARCH_START;
9932 if (flags & SP_END)
9933 options |= SEARCH_END;
9934 if (flags & SP_COLUMN)
9935 options |= SEARCH_COL;
9936
9937 /* Optional arguments: line number to stop searching and timeout. */
9938 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9939 {
9940 lnum_stop = (long)get_tv_number_chk(&argvars[2], NULL);
9941 if (lnum_stop < 0)
9942 goto theend;
9943#ifdef FEAT_RELTIME
9944 if (argvars[3].v_type != VAR_UNKNOWN)
9945 {
9946 time_limit = (long)get_tv_number_chk(&argvars[3], NULL);
9947 if (time_limit < 0)
9948 goto theend;
9949 }
9950#endif
9951 }
9952
9953#ifdef FEAT_RELTIME
9954 /* Set the time limit, if there is one. */
9955 profile_setlimit(time_limit, &tm);
9956#endif
9957
9958 /*
9959 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
9960 * Check to make sure only those flags are set.
9961 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
9962 * flags cannot be set. Check for that condition also.
9963 */
9964 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
9965 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9966 {
9967 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
9968 goto theend;
9969 }
9970
9971 pos = save_cursor = curwin->w_cursor;
9972 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009973 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009974 if (subpatnum != FAIL)
9975 {
9976 if (flags & SP_SUBPAT)
9977 retval = subpatnum;
9978 else
9979 retval = pos.lnum;
9980 if (flags & SP_SETPCMARK)
9981 setpcmark();
9982 curwin->w_cursor = pos;
9983 if (match_pos != NULL)
9984 {
9985 /* Store the match cursor position */
9986 match_pos->lnum = pos.lnum;
9987 match_pos->col = pos.col + 1;
9988 }
9989 /* "/$" will put the cursor after the end of the line, may need to
9990 * correct that here */
9991 check_cursor();
9992 }
9993
9994 /* If 'n' flag is used: restore cursor position. */
9995 if (flags & SP_NOMOVE)
9996 curwin->w_cursor = save_cursor;
9997 else
9998 curwin->w_set_curswant = TRUE;
9999theend:
10000 p_ws = save_p_ws;
10001
10002 return retval;
10003}
10004
10005#ifdef FEAT_FLOAT
10006
10007/*
10008 * round() is not in C90, use ceil() or floor() instead.
10009 */
10010 float_T
10011vim_round(float_T f)
10012{
10013 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
10014}
10015
10016/*
10017 * "round({float})" function
10018 */
10019 static void
10020f_round(typval_T *argvars, typval_T *rettv)
10021{
10022 float_T f = 0.0;
10023
10024 rettv->v_type = VAR_FLOAT;
10025 if (get_float_arg(argvars, &f) == OK)
10026 rettv->vval.v_float = vim_round(f);
10027 else
10028 rettv->vval.v_float = 0.0;
10029}
10030#endif
10031
10032/*
10033 * "screenattr()" function
10034 */
10035 static void
10036f_screenattr(typval_T *argvars, typval_T *rettv)
10037{
10038 int row;
10039 int col;
10040 int c;
10041
10042 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
10043 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
10044 if (row < 0 || row >= screen_Rows
10045 || col < 0 || col >= screen_Columns)
10046 c = -1;
10047 else
10048 c = ScreenAttrs[LineOffset[row] + col];
10049 rettv->vval.v_number = c;
10050}
10051
10052/*
10053 * "screenchar()" function
10054 */
10055 static void
10056f_screenchar(typval_T *argvars, typval_T *rettv)
10057{
10058 int row;
10059 int col;
10060 int off;
10061 int c;
10062
10063 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
10064 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
10065 if (row < 0 || row >= screen_Rows
10066 || col < 0 || col >= screen_Columns)
10067 c = -1;
10068 else
10069 {
10070 off = LineOffset[row] + col;
10071#ifdef FEAT_MBYTE
10072 if (enc_utf8 && ScreenLinesUC[off] != 0)
10073 c = ScreenLinesUC[off];
10074 else
10075#endif
10076 c = ScreenLines[off];
10077 }
10078 rettv->vval.v_number = c;
10079}
10080
10081/*
10082 * "screencol()" function
10083 *
10084 * First column is 1 to be consistent with virtcol().
10085 */
10086 static void
10087f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10088{
10089 rettv->vval.v_number = screen_screencol() + 1;
10090}
10091
10092/*
10093 * "screenrow()" function
10094 */
10095 static void
10096f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10097{
10098 rettv->vval.v_number = screen_screenrow() + 1;
10099}
10100
10101/*
10102 * "search()" function
10103 */
10104 static void
10105f_search(typval_T *argvars, typval_T *rettv)
10106{
10107 int flags = 0;
10108
10109 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10110}
10111
10112/*
10113 * "searchdecl()" function
10114 */
10115 static void
10116f_searchdecl(typval_T *argvars, typval_T *rettv)
10117{
10118 int locally = 1;
10119 int thisblock = 0;
10120 int error = FALSE;
10121 char_u *name;
10122
10123 rettv->vval.v_number = 1; /* default: FAIL */
10124
10125 name = get_tv_string_chk(&argvars[0]);
10126 if (argvars[1].v_type != VAR_UNKNOWN)
10127 {
10128 locally = (int)get_tv_number_chk(&argvars[1], &error) == 0;
10129 if (!error && argvars[2].v_type != VAR_UNKNOWN)
10130 thisblock = (int)get_tv_number_chk(&argvars[2], &error) != 0;
10131 }
10132 if (!error && name != NULL)
10133 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10134 locally, thisblock, SEARCH_KEEP) == FAIL;
10135}
10136
10137/*
10138 * Used by searchpair() and searchpairpos()
10139 */
10140 static int
10141searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10142{
10143 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010144 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010145 int save_p_ws = p_ws;
10146 int dir;
10147 int flags = 0;
10148 char_u nbuf1[NUMBUFLEN];
10149 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010150 int retval = 0; /* default: FAIL */
10151 long lnum_stop = 0;
10152 long time_limit = 0;
10153
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010154 /* Get the three pattern arguments: start, middle, end. Will result in an
10155 * error if not a valid argument. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010156 spat = get_tv_string_chk(&argvars[0]);
10157 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
10158 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
10159 if (spat == NULL || mpat == NULL || epat == NULL)
10160 goto theend; /* type error */
10161
10162 /* Handle the optional fourth argument: flags */
10163 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
10164 if (dir == 0)
10165 goto theend;
10166
10167 /* Don't accept SP_END or SP_SUBPAT.
10168 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
10169 */
10170 if ((flags & (SP_END | SP_SUBPAT)) != 0
10171 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10172 {
10173 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
10174 goto theend;
10175 }
10176
10177 /* Using 'r' implies 'W', otherwise it doesn't work. */
10178 if (flags & SP_REPEAT)
10179 p_ws = FALSE;
10180
10181 /* Optional fifth argument: skip expression */
10182 if (argvars[3].v_type == VAR_UNKNOWN
10183 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010010184 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010185 else
10186 {
Bram Moolenaar48570482017-10-30 21:48:41 +010010187 skip = &argvars[4];
10188 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
10189 && skip->v_type != VAR_STRING)
10190 {
10191 /* Type error */
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010192 EMSG2(_(e_invarg2), get_tv_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010010193 goto theend;
10194 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010195 if (argvars[5].v_type != VAR_UNKNOWN)
10196 {
10197 lnum_stop = (long)get_tv_number_chk(&argvars[5], NULL);
10198 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010199 {
10200 EMSG2(_(e_invarg2), get_tv_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010201 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010202 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010203#ifdef FEAT_RELTIME
10204 if (argvars[6].v_type != VAR_UNKNOWN)
10205 {
10206 time_limit = (long)get_tv_number_chk(&argvars[6], NULL);
10207 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010208 {
10209 EMSG2(_(e_invarg2), get_tv_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010210 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010211 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010212 }
10213#endif
10214 }
10215 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010216
10217 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
10218 match_pos, lnum_stop, time_limit);
10219
10220theend:
10221 p_ws = save_p_ws;
10222
10223 return retval;
10224}
10225
10226/*
10227 * "searchpair()" function
10228 */
10229 static void
10230f_searchpair(typval_T *argvars, typval_T *rettv)
10231{
10232 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
10233}
10234
10235/*
10236 * "searchpairpos()" function
10237 */
10238 static void
10239f_searchpairpos(typval_T *argvars, typval_T *rettv)
10240{
10241 pos_T match_pos;
10242 int lnum = 0;
10243 int col = 0;
10244
10245 if (rettv_list_alloc(rettv) == FAIL)
10246 return;
10247
10248 if (searchpair_cmn(argvars, &match_pos) > 0)
10249 {
10250 lnum = match_pos.lnum;
10251 col = match_pos.col;
10252 }
10253
10254 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10255 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10256}
10257
10258/*
10259 * Search for a start/middle/end thing.
10260 * Used by searchpair(), see its documentation for the details.
10261 * Returns 0 or -1 for no match,
10262 */
10263 long
10264do_searchpair(
10265 char_u *spat, /* start pattern */
10266 char_u *mpat, /* middle pattern */
10267 char_u *epat, /* end pattern */
10268 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010010269 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010270 int flags, /* SP_SETPCMARK and other SP_ values */
10271 pos_T *match_pos,
10272 linenr_T lnum_stop, /* stop at this line if not zero */
10273 long time_limit UNUSED) /* stop after this many msec */
10274{
10275 char_u *save_cpo;
10276 char_u *pat, *pat2 = NULL, *pat3 = NULL;
10277 long retval = 0;
10278 pos_T pos;
10279 pos_T firstpos;
10280 pos_T foundpos;
10281 pos_T save_cursor;
10282 pos_T save_pos;
10283 int n;
10284 int r;
10285 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010010286 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010287 int err;
10288 int options = SEARCH_KEEP;
10289 proftime_T tm;
10290
10291 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10292 save_cpo = p_cpo;
10293 p_cpo = empty_option;
10294
10295#ifdef FEAT_RELTIME
10296 /* Set the time limit, if there is one. */
10297 profile_setlimit(time_limit, &tm);
10298#endif
10299
10300 /* Make two search patterns: start/end (pat2, for in nested pairs) and
10301 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010302 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
10303 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010304 if (pat2 == NULL || pat3 == NULL)
10305 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010306 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010307 if (*mpat == NUL)
10308 STRCPY(pat3, pat2);
10309 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010310 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010311 spat, epat, mpat);
10312 if (flags & SP_START)
10313 options |= SEARCH_START;
10314
Bram Moolenaar48570482017-10-30 21:48:41 +010010315 if (skip != NULL)
10316 {
10317 /* Empty string means to not use the skip expression. */
10318 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
10319 use_skip = skip->vval.v_string != NULL
10320 && *skip->vval.v_string != NUL;
10321 }
10322
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010323 save_cursor = curwin->w_cursor;
10324 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010325 CLEAR_POS(&firstpos);
10326 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010327 pat = pat3;
10328 for (;;)
10329 {
10330 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010331 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010332 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010333 /* didn't find it or found the first match again: FAIL */
10334 break;
10335
10336 if (firstpos.lnum == 0)
10337 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010338 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010339 {
10340 /* Found the same position again. Can happen with a pattern that
10341 * has "\zs" at the end and searching backwards. Advance one
10342 * character and try again. */
10343 if (dir == BACKWARD)
10344 decl(&pos);
10345 else
10346 incl(&pos);
10347 }
10348 foundpos = pos;
10349
10350 /* clear the start flag to avoid getting stuck here */
10351 options &= ~SEARCH_START;
10352
10353 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010010354 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010355 {
10356 save_pos = curwin->w_cursor;
10357 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010010358 err = FALSE;
10359 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010360 curwin->w_cursor = save_pos;
10361 if (err)
10362 {
10363 /* Evaluating {skip} caused an error, break here. */
10364 curwin->w_cursor = save_cursor;
10365 retval = -1;
10366 break;
10367 }
10368 if (r)
10369 continue;
10370 }
10371
10372 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
10373 {
10374 /* Found end when searching backwards or start when searching
10375 * forward: nested pair. */
10376 ++nest;
10377 pat = pat2; /* nested, don't search for middle */
10378 }
10379 else
10380 {
10381 /* Found end when searching forward or start when searching
10382 * backward: end of (nested) pair; or found middle in outer pair. */
10383 if (--nest == 1)
10384 pat = pat3; /* outer level, search for middle */
10385 }
10386
10387 if (nest == 0)
10388 {
10389 /* Found the match: return matchcount or line number. */
10390 if (flags & SP_RETCOUNT)
10391 ++retval;
10392 else
10393 retval = pos.lnum;
10394 if (flags & SP_SETPCMARK)
10395 setpcmark();
10396 curwin->w_cursor = pos;
10397 if (!(flags & SP_REPEAT))
10398 break;
10399 nest = 1; /* search for next unmatched */
10400 }
10401 }
10402
10403 if (match_pos != NULL)
10404 {
10405 /* Store the match cursor position */
10406 match_pos->lnum = curwin->w_cursor.lnum;
10407 match_pos->col = curwin->w_cursor.col + 1;
10408 }
10409
10410 /* If 'n' flag is used or search failed: restore cursor position. */
10411 if ((flags & SP_NOMOVE) || retval == 0)
10412 curwin->w_cursor = save_cursor;
10413
10414theend:
10415 vim_free(pat2);
10416 vim_free(pat3);
10417 if (p_cpo == empty_option)
10418 p_cpo = save_cpo;
10419 else
10420 /* Darn, evaluating the {skip} expression changed the value. */
10421 free_string_option(save_cpo);
10422
10423 return retval;
10424}
10425
10426/*
10427 * "searchpos()" function
10428 */
10429 static void
10430f_searchpos(typval_T *argvars, typval_T *rettv)
10431{
10432 pos_T match_pos;
10433 int lnum = 0;
10434 int col = 0;
10435 int n;
10436 int flags = 0;
10437
10438 if (rettv_list_alloc(rettv) == FAIL)
10439 return;
10440
10441 n = search_cmn(argvars, &match_pos, &flags);
10442 if (n > 0)
10443 {
10444 lnum = match_pos.lnum;
10445 col = match_pos.col;
10446 }
10447
10448 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10449 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10450 if (flags & SP_SUBPAT)
10451 list_append_number(rettv->vval.v_list, (varnumber_T)n);
10452}
10453
10454 static void
10455f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
10456{
10457#ifdef FEAT_CLIENTSERVER
10458 char_u buf[NUMBUFLEN];
10459 char_u *server = get_tv_string_chk(&argvars[0]);
10460 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
10461
10462 rettv->vval.v_number = -1;
10463 if (server == NULL || reply == NULL)
10464 return;
10465 if (check_restricted() || check_secure())
10466 return;
10467# ifdef FEAT_X11
10468 if (check_connection() == FAIL)
10469 return;
10470# endif
10471
10472 if (serverSendReply(server, reply) < 0)
10473 {
10474 EMSG(_("E258: Unable to send to client"));
10475 return;
10476 }
10477 rettv->vval.v_number = 0;
10478#else
10479 rettv->vval.v_number = -1;
10480#endif
10481}
10482
10483 static void
10484f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
10485{
10486 char_u *r = NULL;
10487
10488#ifdef FEAT_CLIENTSERVER
10489# ifdef WIN32
10490 r = serverGetVimNames();
10491# else
10492 make_connection();
10493 if (X_DISPLAY != NULL)
10494 r = serverGetVimNames(X_DISPLAY);
10495# endif
10496#endif
10497 rettv->v_type = VAR_STRING;
10498 rettv->vval.v_string = r;
10499}
10500
10501/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010502 * "setbufline()" function
10503 */
10504 static void
10505f_setbufline(argvars, rettv)
10506 typval_T *argvars;
10507 typval_T *rettv;
10508{
10509 linenr_T lnum;
10510 buf_T *buf;
10511
10512 buf = get_buf_tv(&argvars[0], FALSE);
10513 if (buf == NULL)
10514 rettv->vval.v_number = 1; /* FAIL */
10515 else
10516 {
10517 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020010518 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010519 }
10520}
10521
10522/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010523 * "setbufvar()" function
10524 */
10525 static void
10526f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10527{
10528 buf_T *buf;
10529 char_u *varname, *bufvarname;
10530 typval_T *varp;
10531 char_u nbuf[NUMBUFLEN];
10532
10533 if (check_restricted() || check_secure())
10534 return;
10535 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10536 varname = get_tv_string_chk(&argvars[1]);
10537 buf = get_buf_tv(&argvars[0], FALSE);
10538 varp = &argvars[2];
10539
10540 if (buf != NULL && varname != NULL && varp != NULL)
10541 {
10542 if (*varname == '&')
10543 {
10544 long numval;
10545 char_u *strval;
10546 int error = FALSE;
10547 aco_save_T aco;
10548
10549 /* set curbuf to be our buf, temporarily */
10550 aucmd_prepbuf(&aco, buf);
10551
10552 ++varname;
10553 numval = (long)get_tv_number_chk(varp, &error);
10554 strval = get_tv_string_buf_chk(varp, nbuf);
10555 if (!error && strval != NULL)
10556 set_option_value(varname, numval, strval, OPT_LOCAL);
10557
10558 /* reset notion of buffer */
10559 aucmd_restbuf(&aco);
10560 }
10561 else
10562 {
10563 buf_T *save_curbuf = curbuf;
10564
10565 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
10566 if (bufvarname != NULL)
10567 {
10568 curbuf = buf;
10569 STRCPY(bufvarname, "b:");
10570 STRCPY(bufvarname + 2, varname);
10571 set_var(bufvarname, varp, TRUE);
10572 vim_free(bufvarname);
10573 curbuf = save_curbuf;
10574 }
10575 }
10576 }
10577}
10578
10579 static void
10580f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10581{
10582 dict_T *d;
10583 dictitem_T *di;
10584 char_u *csearch;
10585
10586 if (argvars[0].v_type != VAR_DICT)
10587 {
10588 EMSG(_(e_dictreq));
10589 return;
10590 }
10591
10592 if ((d = argvars[0].vval.v_dict) != NULL)
10593 {
10594 csearch = get_dict_string(d, (char_u *)"char", FALSE);
10595 if (csearch != NULL)
10596 {
10597#ifdef FEAT_MBYTE
10598 if (enc_utf8)
10599 {
10600 int pcc[MAX_MCO];
10601 int c = utfc_ptr2char(csearch, pcc);
10602
10603 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
10604 }
10605 else
10606#endif
10607 set_last_csearch(PTR2CHAR(csearch),
10608 csearch, MB_PTR2LEN(csearch));
10609 }
10610
10611 di = dict_find(d, (char_u *)"forward", -1);
10612 if (di != NULL)
10613 set_csearch_direction((int)get_tv_number(&di->di_tv)
10614 ? FORWARD : BACKWARD);
10615
10616 di = dict_find(d, (char_u *)"until", -1);
10617 if (di != NULL)
10618 set_csearch_until(!!get_tv_number(&di->di_tv));
10619 }
10620}
10621
10622/*
10623 * "setcmdpos()" function
10624 */
10625 static void
10626f_setcmdpos(typval_T *argvars, typval_T *rettv)
10627{
10628 int pos = (int)get_tv_number(&argvars[0]) - 1;
10629
10630 if (pos >= 0)
10631 rettv->vval.v_number = set_cmdline_pos(pos);
10632}
10633
10634/*
10635 * "setfperm({fname}, {mode})" function
10636 */
10637 static void
10638f_setfperm(typval_T *argvars, typval_T *rettv)
10639{
10640 char_u *fname;
10641 char_u modebuf[NUMBUFLEN];
10642 char_u *mode_str;
10643 int i;
10644 int mask;
10645 int mode = 0;
10646
10647 rettv->vval.v_number = 0;
10648 fname = get_tv_string_chk(&argvars[0]);
10649 if (fname == NULL)
10650 return;
10651 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
10652 if (mode_str == NULL)
10653 return;
10654 if (STRLEN(mode_str) != 9)
10655 {
10656 EMSG2(_(e_invarg2), mode_str);
10657 return;
10658 }
10659
10660 mask = 1;
10661 for (i = 8; i >= 0; --i)
10662 {
10663 if (mode_str[i] != '-')
10664 mode |= mask;
10665 mask = mask << 1;
10666 }
10667 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
10668}
10669
10670/*
10671 * "setline()" function
10672 */
10673 static void
10674f_setline(typval_T *argvars, typval_T *rettv)
10675{
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010676 linenr_T lnum = get_tv_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010677
Bram Moolenaarca851592018-06-06 21:04:07 +020010678 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010679}
10680
Bram Moolenaard823fa92016-08-12 16:29:27 +020010681static 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 +020010682
10683/*
10684 * Used by "setqflist()" and "setloclist()" functions
10685 */
10686 static void
10687set_qf_ll_list(
10688 win_T *wp UNUSED,
10689 typval_T *list_arg UNUSED,
10690 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020010691 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010692 typval_T *rettv)
10693{
10694#ifdef FEAT_QUICKFIX
10695 static char *e_invact = N_("E927: Invalid action: '%s'");
10696 char_u *act;
10697 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010698 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010699#endif
10700
10701 rettv->vval.v_number = -1;
10702
10703#ifdef FEAT_QUICKFIX
10704 if (list_arg->v_type != VAR_LIST)
10705 EMSG(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010706 else if (recursive != 0)
10707 EMSG(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010708 else
10709 {
10710 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010711 dict_T *d = NULL;
10712 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010713
10714 if (action_arg->v_type == VAR_STRING)
10715 {
10716 act = get_tv_string_chk(action_arg);
10717 if (act == NULL)
10718 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020010719 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
10720 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010721 action = *act;
10722 else
10723 EMSG2(_(e_invact), act);
10724 }
10725 else if (action_arg->v_type == VAR_UNKNOWN)
10726 action = ' ';
10727 else
10728 EMSG(_(e_stringreq));
10729
Bram Moolenaard823fa92016-08-12 16:29:27 +020010730 if (action_arg->v_type != VAR_UNKNOWN
10731 && what_arg->v_type != VAR_UNKNOWN)
10732 {
10733 if (what_arg->v_type == VAR_DICT)
10734 d = what_arg->vval.v_dict;
10735 else
10736 {
10737 EMSG(_(e_dictreq));
10738 valid_dict = FALSE;
10739 }
10740 }
10741
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010742 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010743 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010744 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
10745 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010746 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010747 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010748 }
10749#endif
10750}
10751
10752/*
10753 * "setloclist()" function
10754 */
10755 static void
10756f_setloclist(typval_T *argvars, typval_T *rettv)
10757{
10758 win_T *win;
10759
10760 rettv->vval.v_number = -1;
10761
10762 win = find_win_by_nr(&argvars[0], NULL);
10763 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020010764 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010765}
10766
10767/*
10768 * "setmatches()" function
10769 */
10770 static void
10771f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10772{
10773#ifdef FEAT_SEARCH_EXTRA
10774 list_T *l;
10775 listitem_T *li;
10776 dict_T *d;
10777 list_T *s = NULL;
10778
10779 rettv->vval.v_number = -1;
10780 if (argvars[0].v_type != VAR_LIST)
10781 {
10782 EMSG(_(e_listreq));
10783 return;
10784 }
10785 if ((l = argvars[0].vval.v_list) != NULL)
10786 {
10787
10788 /* To some extent make sure that we are dealing with a list from
10789 * "getmatches()". */
10790 li = l->lv_first;
10791 while (li != NULL)
10792 {
10793 if (li->li_tv.v_type != VAR_DICT
10794 || (d = li->li_tv.vval.v_dict) == NULL)
10795 {
10796 EMSG(_(e_invarg));
10797 return;
10798 }
10799 if (!(dict_find(d, (char_u *)"group", -1) != NULL
10800 && (dict_find(d, (char_u *)"pattern", -1) != NULL
10801 || dict_find(d, (char_u *)"pos1", -1) != NULL)
10802 && dict_find(d, (char_u *)"priority", -1) != NULL
10803 && dict_find(d, (char_u *)"id", -1) != NULL))
10804 {
10805 EMSG(_(e_invarg));
10806 return;
10807 }
10808 li = li->li_next;
10809 }
10810
10811 clear_matches(curwin);
10812 li = l->lv_first;
10813 while (li != NULL)
10814 {
10815 int i = 0;
10816 char_u buf[5];
10817 dictitem_T *di;
10818 char_u *group;
10819 int priority;
10820 int id;
10821 char_u *conceal;
10822
10823 d = li->li_tv.vval.v_dict;
10824 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
10825 {
10826 if (s == NULL)
10827 {
10828 s = list_alloc();
10829 if (s == NULL)
10830 return;
10831 }
10832
10833 /* match from matchaddpos() */
10834 for (i = 1; i < 9; i++)
10835 {
10836 sprintf((char *)buf, (char *)"pos%d", i);
10837 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
10838 {
10839 if (di->di_tv.v_type != VAR_LIST)
10840 return;
10841
10842 list_append_tv(s, &di->di_tv);
10843 s->lv_refcount++;
10844 }
10845 else
10846 break;
10847 }
10848 }
10849
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010850 group = get_dict_string(d, (char_u *)"group", TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010851 priority = (int)get_dict_number(d, (char_u *)"priority");
10852 id = (int)get_dict_number(d, (char_u *)"id");
10853 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010854 ? get_dict_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010855 : NULL;
10856 if (i == 0)
10857 {
10858 match_add(curwin, group,
10859 get_dict_string(d, (char_u *)"pattern", FALSE),
10860 priority, id, NULL, conceal);
10861 }
10862 else
10863 {
10864 match_add(curwin, group, NULL, priority, id, s, conceal);
10865 list_unref(s);
10866 s = NULL;
10867 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010868 vim_free(group);
10869 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010870
10871 li = li->li_next;
10872 }
10873 rettv->vval.v_number = 0;
10874 }
10875#endif
10876}
10877
10878/*
10879 * "setpos()" function
10880 */
10881 static void
10882f_setpos(typval_T *argvars, typval_T *rettv)
10883{
10884 pos_T pos;
10885 int fnum;
10886 char_u *name;
10887 colnr_T curswant = -1;
10888
10889 rettv->vval.v_number = -1;
10890 name = get_tv_string_chk(argvars);
10891 if (name != NULL)
10892 {
10893 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
10894 {
10895 if (--pos.col < 0)
10896 pos.col = 0;
10897 if (name[0] == '.' && name[1] == NUL)
10898 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010899 /* set cursor; "fnum" is ignored */
10900 curwin->w_cursor = pos;
10901 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010902 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010903 curwin->w_curswant = curswant - 1;
10904 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010905 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010906 check_cursor();
10907 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010908 }
10909 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
10910 {
10911 /* set mark */
10912 if (setmark_pos(name[1], &pos, fnum) == OK)
10913 rettv->vval.v_number = 0;
10914 }
10915 else
10916 EMSG(_(e_invarg));
10917 }
10918 }
10919}
10920
10921/*
10922 * "setqflist()" function
10923 */
10924 static void
10925f_setqflist(typval_T *argvars, typval_T *rettv)
10926{
Bram Moolenaard823fa92016-08-12 16:29:27 +020010927 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010928}
10929
10930/*
10931 * "setreg()" function
10932 */
10933 static void
10934f_setreg(typval_T *argvars, typval_T *rettv)
10935{
10936 int regname;
10937 char_u *strregname;
10938 char_u *stropt;
10939 char_u *strval;
10940 int append;
10941 char_u yank_type;
10942 long block_len;
10943
10944 block_len = -1;
10945 yank_type = MAUTO;
10946 append = FALSE;
10947
10948 strregname = get_tv_string_chk(argvars);
10949 rettv->vval.v_number = 1; /* FAIL is default */
10950
10951 if (strregname == NULL)
10952 return; /* type error; errmsg already given */
10953 regname = *strregname;
10954 if (regname == 0 || regname == '@')
10955 regname = '"';
10956
10957 if (argvars[2].v_type != VAR_UNKNOWN)
10958 {
10959 stropt = get_tv_string_chk(&argvars[2]);
10960 if (stropt == NULL)
10961 return; /* type error */
10962 for (; *stropt != NUL; ++stropt)
10963 switch (*stropt)
10964 {
10965 case 'a': case 'A': /* append */
10966 append = TRUE;
10967 break;
10968 case 'v': case 'c': /* character-wise selection */
10969 yank_type = MCHAR;
10970 break;
10971 case 'V': case 'l': /* line-wise selection */
10972 yank_type = MLINE;
10973 break;
10974 case 'b': case Ctrl_V: /* block-wise selection */
10975 yank_type = MBLOCK;
10976 if (VIM_ISDIGIT(stropt[1]))
10977 {
10978 ++stropt;
10979 block_len = getdigits(&stropt) - 1;
10980 --stropt;
10981 }
10982 break;
10983 }
10984 }
10985
10986 if (argvars[1].v_type == VAR_LIST)
10987 {
10988 char_u **lstval;
10989 char_u **allocval;
10990 char_u buf[NUMBUFLEN];
10991 char_u **curval;
10992 char_u **curallocval;
10993 list_T *ll = argvars[1].vval.v_list;
10994 listitem_T *li;
10995 int len;
10996
10997 /* If the list is NULL handle like an empty list. */
10998 len = ll == NULL ? 0 : ll->lv_len;
10999
11000 /* First half: use for pointers to result lines; second half: use for
11001 * pointers to allocated copies. */
11002 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
11003 if (lstval == NULL)
11004 return;
11005 curval = lstval;
11006 allocval = lstval + len + 2;
11007 curallocval = allocval;
11008
11009 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
11010 li = li->li_next)
11011 {
11012 strval = get_tv_string_buf_chk(&li->li_tv, buf);
11013 if (strval == NULL)
11014 goto free_lstval;
11015 if (strval == buf)
11016 {
11017 /* Need to make a copy, next get_tv_string_buf_chk() will
11018 * overwrite the string. */
11019 strval = vim_strsave(buf);
11020 if (strval == NULL)
11021 goto free_lstval;
11022 *curallocval++ = strval;
11023 }
11024 *curval++ = strval;
11025 }
11026 *curval++ = NULL;
11027
11028 write_reg_contents_lst(regname, lstval, -1,
11029 append, yank_type, block_len);
11030free_lstval:
11031 while (curallocval > allocval)
11032 vim_free(*--curallocval);
11033 vim_free(lstval);
11034 }
11035 else
11036 {
11037 strval = get_tv_string_chk(&argvars[1]);
11038 if (strval == NULL)
11039 return;
11040 write_reg_contents_ex(regname, strval, -1,
11041 append, yank_type, block_len);
11042 }
11043 rettv->vval.v_number = 0;
11044}
11045
11046/*
11047 * "settabvar()" function
11048 */
11049 static void
11050f_settabvar(typval_T *argvars, typval_T *rettv)
11051{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011052 tabpage_T *save_curtab;
11053 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011054 char_u *varname, *tabvarname;
11055 typval_T *varp;
11056
11057 rettv->vval.v_number = 0;
11058
11059 if (check_restricted() || check_secure())
11060 return;
11061
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011062 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011063 varname = get_tv_string_chk(&argvars[1]);
11064 varp = &argvars[2];
11065
Bram Moolenaar4033c552017-09-16 20:54:51 +020011066 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011067 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011068 save_curtab = curtab;
11069 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011070
11071 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
11072 if (tabvarname != NULL)
11073 {
11074 STRCPY(tabvarname, "t:");
11075 STRCPY(tabvarname + 2, varname);
11076 set_var(tabvarname, varp, TRUE);
11077 vim_free(tabvarname);
11078 }
11079
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011080 /* Restore current tabpage */
11081 if (valid_tabpage(save_curtab))
11082 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011083 }
11084}
11085
11086/*
11087 * "settabwinvar()" function
11088 */
11089 static void
11090f_settabwinvar(typval_T *argvars, typval_T *rettv)
11091{
11092 setwinvar(argvars, rettv, 1);
11093}
11094
11095/*
11096 * "setwinvar()" function
11097 */
11098 static void
11099f_setwinvar(typval_T *argvars, typval_T *rettv)
11100{
11101 setwinvar(argvars, rettv, 0);
11102}
11103
11104#ifdef FEAT_CRYPT
11105/*
11106 * "sha256({string})" function
11107 */
11108 static void
11109f_sha256(typval_T *argvars, typval_T *rettv)
11110{
11111 char_u *p;
11112
11113 p = get_tv_string(&argvars[0]);
11114 rettv->vval.v_string = vim_strsave(
11115 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
11116 rettv->v_type = VAR_STRING;
11117}
11118#endif /* FEAT_CRYPT */
11119
11120/*
11121 * "shellescape({string})" function
11122 */
11123 static void
11124f_shellescape(typval_T *argvars, typval_T *rettv)
11125{
Bram Moolenaar20615522017-06-05 18:46:26 +020011126 int do_special = non_zero_arg(&argvars[1]);
11127
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011128 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar20615522017-06-05 18:46:26 +020011129 get_tv_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011130 rettv->v_type = VAR_STRING;
11131}
11132
11133/*
11134 * shiftwidth() function
11135 */
11136 static void
11137f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
11138{
11139 rettv->vval.v_number = get_sw_value(curbuf);
11140}
11141
11142/*
11143 * "simplify()" function
11144 */
11145 static void
11146f_simplify(typval_T *argvars, typval_T *rettv)
11147{
11148 char_u *p;
11149
11150 p = get_tv_string(&argvars[0]);
11151 rettv->vval.v_string = vim_strsave(p);
11152 simplify_filename(rettv->vval.v_string); /* simplify in place */
11153 rettv->v_type = VAR_STRING;
11154}
11155
11156#ifdef FEAT_FLOAT
11157/*
11158 * "sin()" function
11159 */
11160 static void
11161f_sin(typval_T *argvars, typval_T *rettv)
11162{
11163 float_T f = 0.0;
11164
11165 rettv->v_type = VAR_FLOAT;
11166 if (get_float_arg(argvars, &f) == OK)
11167 rettv->vval.v_float = sin(f);
11168 else
11169 rettv->vval.v_float = 0.0;
11170}
11171
11172/*
11173 * "sinh()" function
11174 */
11175 static void
11176f_sinh(typval_T *argvars, typval_T *rettv)
11177{
11178 float_T f = 0.0;
11179
11180 rettv->v_type = VAR_FLOAT;
11181 if (get_float_arg(argvars, &f) == OK)
11182 rettv->vval.v_float = sinh(f);
11183 else
11184 rettv->vval.v_float = 0.0;
11185}
11186#endif
11187
11188static int
11189#ifdef __BORLANDC__
11190 _RTLENTRYF
11191#endif
11192 item_compare(const void *s1, const void *s2);
11193static int
11194#ifdef __BORLANDC__
11195 _RTLENTRYF
11196#endif
11197 item_compare2(const void *s1, const void *s2);
11198
11199/* struct used in the array that's given to qsort() */
11200typedef struct
11201{
11202 listitem_T *item;
11203 int idx;
11204} sortItem_T;
11205
11206/* struct storing information about current sort */
11207typedef struct
11208{
11209 int item_compare_ic;
11210 int item_compare_numeric;
11211 int item_compare_numbers;
11212#ifdef FEAT_FLOAT
11213 int item_compare_float;
11214#endif
11215 char_u *item_compare_func;
11216 partial_T *item_compare_partial;
11217 dict_T *item_compare_selfdict;
11218 int item_compare_func_err;
11219 int item_compare_keep_zero;
11220} sortinfo_T;
11221static sortinfo_T *sortinfo = NULL;
11222static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
11223#define ITEM_COMPARE_FAIL 999
11224
11225/*
11226 * Compare functions for f_sort() and f_uniq() below.
11227 */
11228 static int
11229#ifdef __BORLANDC__
11230_RTLENTRYF
11231#endif
11232item_compare(const void *s1, const void *s2)
11233{
11234 sortItem_T *si1, *si2;
11235 typval_T *tv1, *tv2;
11236 char_u *p1, *p2;
11237 char_u *tofree1 = NULL, *tofree2 = NULL;
11238 int res;
11239 char_u numbuf1[NUMBUFLEN];
11240 char_u numbuf2[NUMBUFLEN];
11241
11242 si1 = (sortItem_T *)s1;
11243 si2 = (sortItem_T *)s2;
11244 tv1 = &si1->item->li_tv;
11245 tv2 = &si2->item->li_tv;
11246
11247 if (sortinfo->item_compare_numbers)
11248 {
11249 varnumber_T v1 = get_tv_number(tv1);
11250 varnumber_T v2 = get_tv_number(tv2);
11251
11252 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11253 }
11254
11255#ifdef FEAT_FLOAT
11256 if (sortinfo->item_compare_float)
11257 {
11258 float_T v1 = get_tv_float(tv1);
11259 float_T v2 = get_tv_float(tv2);
11260
11261 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11262 }
11263#endif
11264
11265 /* tv2string() puts quotes around a string and allocates memory. Don't do
11266 * that for string variables. Use a single quote when comparing with a
11267 * non-string to do what the docs promise. */
11268 if (tv1->v_type == VAR_STRING)
11269 {
11270 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11271 p1 = (char_u *)"'";
11272 else
11273 p1 = tv1->vval.v_string;
11274 }
11275 else
11276 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
11277 if (tv2->v_type == VAR_STRING)
11278 {
11279 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11280 p2 = (char_u *)"'";
11281 else
11282 p2 = tv2->vval.v_string;
11283 }
11284 else
11285 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
11286 if (p1 == NULL)
11287 p1 = (char_u *)"";
11288 if (p2 == NULL)
11289 p2 = (char_u *)"";
11290 if (!sortinfo->item_compare_numeric)
11291 {
11292 if (sortinfo->item_compare_ic)
11293 res = STRICMP(p1, p2);
11294 else
11295 res = STRCMP(p1, p2);
11296 }
11297 else
11298 {
11299 double n1, n2;
11300 n1 = strtod((char *)p1, (char **)&p1);
11301 n2 = strtod((char *)p2, (char **)&p2);
11302 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
11303 }
11304
11305 /* When the result would be zero, compare the item indexes. Makes the
11306 * sort stable. */
11307 if (res == 0 && !sortinfo->item_compare_keep_zero)
11308 res = si1->idx > si2->idx ? 1 : -1;
11309
11310 vim_free(tofree1);
11311 vim_free(tofree2);
11312 return res;
11313}
11314
11315 static int
11316#ifdef __BORLANDC__
11317_RTLENTRYF
11318#endif
11319item_compare2(const void *s1, const void *s2)
11320{
11321 sortItem_T *si1, *si2;
11322 int res;
11323 typval_T rettv;
11324 typval_T argv[3];
11325 int dummy;
11326 char_u *func_name;
11327 partial_T *partial = sortinfo->item_compare_partial;
11328
11329 /* shortcut after failure in previous call; compare all items equal */
11330 if (sortinfo->item_compare_func_err)
11331 return 0;
11332
11333 si1 = (sortItem_T *)s1;
11334 si2 = (sortItem_T *)s2;
11335
11336 if (partial == NULL)
11337 func_name = sortinfo->item_compare_func;
11338 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020011339 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011340
11341 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
11342 * in the copy without changing the original list items. */
11343 copy_tv(&si1->item->li_tv, &argv[0]);
11344 copy_tv(&si2->item->li_tv, &argv[1]);
11345
11346 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
11347 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020011348 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011349 partial, sortinfo->item_compare_selfdict);
11350 clear_tv(&argv[0]);
11351 clear_tv(&argv[1]);
11352
11353 if (res == FAIL)
11354 res = ITEM_COMPARE_FAIL;
11355 else
11356 res = (int)get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
11357 if (sortinfo->item_compare_func_err)
11358 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
11359 clear_tv(&rettv);
11360
11361 /* When the result would be zero, compare the pointers themselves. Makes
11362 * the sort stable. */
11363 if (res == 0 && !sortinfo->item_compare_keep_zero)
11364 res = si1->idx > si2->idx ? 1 : -1;
11365
11366 return res;
11367}
11368
11369/*
11370 * "sort({list})" function
11371 */
11372 static void
11373do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
11374{
11375 list_T *l;
11376 listitem_T *li;
11377 sortItem_T *ptrs;
11378 sortinfo_T *old_sortinfo;
11379 sortinfo_T info;
11380 long len;
11381 long i;
11382
11383 /* Pointer to current info struct used in compare function. Save and
11384 * restore the current one for nested calls. */
11385 old_sortinfo = sortinfo;
11386 sortinfo = &info;
11387
11388 if (argvars[0].v_type != VAR_LIST)
11389 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
11390 else
11391 {
11392 l = argvars[0].vval.v_list;
11393 if (l == NULL || tv_check_lock(l->lv_lock,
11394 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
11395 TRUE))
11396 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011397 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011398
11399 len = list_len(l);
11400 if (len <= 1)
11401 goto theend; /* short list sorts pretty quickly */
11402
11403 info.item_compare_ic = FALSE;
11404 info.item_compare_numeric = FALSE;
11405 info.item_compare_numbers = FALSE;
11406#ifdef FEAT_FLOAT
11407 info.item_compare_float = FALSE;
11408#endif
11409 info.item_compare_func = NULL;
11410 info.item_compare_partial = NULL;
11411 info.item_compare_selfdict = NULL;
11412 if (argvars[1].v_type != VAR_UNKNOWN)
11413 {
11414 /* optional second argument: {func} */
11415 if (argvars[1].v_type == VAR_FUNC)
11416 info.item_compare_func = argvars[1].vval.v_string;
11417 else if (argvars[1].v_type == VAR_PARTIAL)
11418 info.item_compare_partial = argvars[1].vval.v_partial;
11419 else
11420 {
11421 int error = FALSE;
11422
11423 i = (long)get_tv_number_chk(&argvars[1], &error);
11424 if (error)
11425 goto theend; /* type error; errmsg already given */
11426 if (i == 1)
11427 info.item_compare_ic = TRUE;
11428 else if (argvars[1].v_type != VAR_NUMBER)
11429 info.item_compare_func = get_tv_string(&argvars[1]);
11430 else if (i != 0)
11431 {
11432 EMSG(_(e_invarg));
11433 goto theend;
11434 }
11435 if (info.item_compare_func != NULL)
11436 {
11437 if (*info.item_compare_func == NUL)
11438 {
11439 /* empty string means default sort */
11440 info.item_compare_func = NULL;
11441 }
11442 else if (STRCMP(info.item_compare_func, "n") == 0)
11443 {
11444 info.item_compare_func = NULL;
11445 info.item_compare_numeric = TRUE;
11446 }
11447 else if (STRCMP(info.item_compare_func, "N") == 0)
11448 {
11449 info.item_compare_func = NULL;
11450 info.item_compare_numbers = TRUE;
11451 }
11452#ifdef FEAT_FLOAT
11453 else if (STRCMP(info.item_compare_func, "f") == 0)
11454 {
11455 info.item_compare_func = NULL;
11456 info.item_compare_float = TRUE;
11457 }
11458#endif
11459 else if (STRCMP(info.item_compare_func, "i") == 0)
11460 {
11461 info.item_compare_func = NULL;
11462 info.item_compare_ic = TRUE;
11463 }
11464 }
11465 }
11466
11467 if (argvars[2].v_type != VAR_UNKNOWN)
11468 {
11469 /* optional third argument: {dict} */
11470 if (argvars[2].v_type != VAR_DICT)
11471 {
11472 EMSG(_(e_dictreq));
11473 goto theend;
11474 }
11475 info.item_compare_selfdict = argvars[2].vval.v_dict;
11476 }
11477 }
11478
11479 /* Make an array with each entry pointing to an item in the List. */
11480 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
11481 if (ptrs == NULL)
11482 goto theend;
11483
11484 i = 0;
11485 if (sort)
11486 {
11487 /* sort(): ptrs will be the list to sort */
11488 for (li = l->lv_first; li != NULL; li = li->li_next)
11489 {
11490 ptrs[i].item = li;
11491 ptrs[i].idx = i;
11492 ++i;
11493 }
11494
11495 info.item_compare_func_err = FALSE;
11496 info.item_compare_keep_zero = FALSE;
11497 /* test the compare function */
11498 if ((info.item_compare_func != NULL
11499 || info.item_compare_partial != NULL)
11500 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
11501 == ITEM_COMPARE_FAIL)
11502 EMSG(_("E702: Sort compare function failed"));
11503 else
11504 {
11505 /* Sort the array with item pointers. */
11506 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
11507 info.item_compare_func == NULL
11508 && info.item_compare_partial == NULL
11509 ? item_compare : item_compare2);
11510
11511 if (!info.item_compare_func_err)
11512 {
11513 /* Clear the List and append the items in sorted order. */
11514 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
11515 l->lv_len = 0;
11516 for (i = 0; i < len; ++i)
11517 list_append(l, ptrs[i].item);
11518 }
11519 }
11520 }
11521 else
11522 {
11523 int (*item_compare_func_ptr)(const void *, const void *);
11524
11525 /* f_uniq(): ptrs will be a stack of items to remove */
11526 info.item_compare_func_err = FALSE;
11527 info.item_compare_keep_zero = TRUE;
11528 item_compare_func_ptr = info.item_compare_func != NULL
11529 || info.item_compare_partial != NULL
11530 ? item_compare2 : item_compare;
11531
11532 for (li = l->lv_first; li != NULL && li->li_next != NULL;
11533 li = li->li_next)
11534 {
11535 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
11536 == 0)
11537 ptrs[i++].item = li;
11538 if (info.item_compare_func_err)
11539 {
11540 EMSG(_("E882: Uniq compare function failed"));
11541 break;
11542 }
11543 }
11544
11545 if (!info.item_compare_func_err)
11546 {
11547 while (--i >= 0)
11548 {
11549 li = ptrs[i].item->li_next;
11550 ptrs[i].item->li_next = li->li_next;
11551 if (li->li_next != NULL)
11552 li->li_next->li_prev = ptrs[i].item;
11553 else
11554 l->lv_last = ptrs[i].item;
11555 list_fix_watch(l, li);
11556 listitem_free(li);
11557 l->lv_len--;
11558 }
11559 }
11560 }
11561
11562 vim_free(ptrs);
11563 }
11564theend:
11565 sortinfo = old_sortinfo;
11566}
11567
11568/*
11569 * "sort({list})" function
11570 */
11571 static void
11572f_sort(typval_T *argvars, typval_T *rettv)
11573{
11574 do_sort_uniq(argvars, rettv, TRUE);
11575}
11576
11577/*
11578 * "uniq({list})" function
11579 */
11580 static void
11581f_uniq(typval_T *argvars, typval_T *rettv)
11582{
11583 do_sort_uniq(argvars, rettv, FALSE);
11584}
11585
11586/*
11587 * "soundfold({word})" function
11588 */
11589 static void
11590f_soundfold(typval_T *argvars, typval_T *rettv)
11591{
11592 char_u *s;
11593
11594 rettv->v_type = VAR_STRING;
11595 s = get_tv_string(&argvars[0]);
11596#ifdef FEAT_SPELL
11597 rettv->vval.v_string = eval_soundfold(s);
11598#else
11599 rettv->vval.v_string = vim_strsave(s);
11600#endif
11601}
11602
11603/*
11604 * "spellbadword()" function
11605 */
11606 static void
11607f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
11608{
11609 char_u *word = (char_u *)"";
11610 hlf_T attr = HLF_COUNT;
11611 int len = 0;
11612
11613 if (rettv_list_alloc(rettv) == FAIL)
11614 return;
11615
11616#ifdef FEAT_SPELL
11617 if (argvars[0].v_type == VAR_UNKNOWN)
11618 {
11619 /* Find the start and length of the badly spelled word. */
11620 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
11621 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011622 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011623 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011624 curwin->w_set_curswant = TRUE;
11625 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011626 }
11627 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
11628 {
11629 char_u *str = get_tv_string_chk(&argvars[0]);
11630 int capcol = -1;
11631
11632 if (str != NULL)
11633 {
11634 /* Check the argument for spelling. */
11635 while (*str != NUL)
11636 {
11637 len = spell_check(curwin, str, &attr, &capcol, FALSE);
11638 if (attr != HLF_COUNT)
11639 {
11640 word = str;
11641 break;
11642 }
11643 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020011644 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011645 }
11646 }
11647 }
11648#endif
11649
11650 list_append_string(rettv->vval.v_list, word, len);
11651 list_append_string(rettv->vval.v_list, (char_u *)(
11652 attr == HLF_SPB ? "bad" :
11653 attr == HLF_SPR ? "rare" :
11654 attr == HLF_SPL ? "local" :
11655 attr == HLF_SPC ? "caps" :
11656 ""), -1);
11657}
11658
11659/*
11660 * "spellsuggest()" function
11661 */
11662 static void
11663f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
11664{
11665#ifdef FEAT_SPELL
11666 char_u *str;
11667 int typeerr = FALSE;
11668 int maxcount;
11669 garray_T ga;
11670 int i;
11671 listitem_T *li;
11672 int need_capital = FALSE;
11673#endif
11674
11675 if (rettv_list_alloc(rettv) == FAIL)
11676 return;
11677
11678#ifdef FEAT_SPELL
11679 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
11680 {
11681 str = get_tv_string(&argvars[0]);
11682 if (argvars[1].v_type != VAR_UNKNOWN)
11683 {
11684 maxcount = (int)get_tv_number_chk(&argvars[1], &typeerr);
11685 if (maxcount <= 0)
11686 return;
11687 if (argvars[2].v_type != VAR_UNKNOWN)
11688 {
11689 need_capital = (int)get_tv_number_chk(&argvars[2], &typeerr);
11690 if (typeerr)
11691 return;
11692 }
11693 }
11694 else
11695 maxcount = 25;
11696
11697 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
11698
11699 for (i = 0; i < ga.ga_len; ++i)
11700 {
11701 str = ((char_u **)ga.ga_data)[i];
11702
11703 li = listitem_alloc();
11704 if (li == NULL)
11705 vim_free(str);
11706 else
11707 {
11708 li->li_tv.v_type = VAR_STRING;
11709 li->li_tv.v_lock = 0;
11710 li->li_tv.vval.v_string = str;
11711 list_append(rettv->vval.v_list, li);
11712 }
11713 }
11714 ga_clear(&ga);
11715 }
11716#endif
11717}
11718
11719 static void
11720f_split(typval_T *argvars, typval_T *rettv)
11721{
11722 char_u *str;
11723 char_u *end;
11724 char_u *pat = NULL;
11725 regmatch_T regmatch;
11726 char_u patbuf[NUMBUFLEN];
11727 char_u *save_cpo;
11728 int match;
11729 colnr_T col = 0;
11730 int keepempty = FALSE;
11731 int typeerr = FALSE;
11732
11733 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11734 save_cpo = p_cpo;
11735 p_cpo = (char_u *)"";
11736
11737 str = get_tv_string(&argvars[0]);
11738 if (argvars[1].v_type != VAR_UNKNOWN)
11739 {
11740 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11741 if (pat == NULL)
11742 typeerr = TRUE;
11743 if (argvars[2].v_type != VAR_UNKNOWN)
11744 keepempty = (int)get_tv_number_chk(&argvars[2], &typeerr);
11745 }
11746 if (pat == NULL || *pat == NUL)
11747 pat = (char_u *)"[\\x01- ]\\+";
11748
11749 if (rettv_list_alloc(rettv) == FAIL)
11750 return;
11751 if (typeerr)
11752 return;
11753
11754 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11755 if (regmatch.regprog != NULL)
11756 {
11757 regmatch.rm_ic = FALSE;
11758 while (*str != NUL || keepempty)
11759 {
11760 if (*str == NUL)
11761 match = FALSE; /* empty item at the end */
11762 else
11763 match = vim_regexec_nl(&regmatch, str, col);
11764 if (match)
11765 end = regmatch.startp[0];
11766 else
11767 end = str + STRLEN(str);
11768 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
11769 && *str != NUL && match && end < regmatch.endp[0]))
11770 {
11771 if (list_append_string(rettv->vval.v_list, str,
11772 (int)(end - str)) == FAIL)
11773 break;
11774 }
11775 if (!match)
11776 break;
11777 /* Advance to just after the match. */
11778 if (regmatch.endp[0] > str)
11779 col = 0;
11780 else
11781 {
11782 /* Don't get stuck at the same match. */
11783#ifdef FEAT_MBYTE
11784 col = (*mb_ptr2len)(regmatch.endp[0]);
11785#else
11786 col = 1;
11787#endif
11788 }
11789 str = regmatch.endp[0];
11790 }
11791
11792 vim_regfree(regmatch.regprog);
11793 }
11794
11795 p_cpo = save_cpo;
11796}
11797
11798#ifdef FEAT_FLOAT
11799/*
11800 * "sqrt()" function
11801 */
11802 static void
11803f_sqrt(typval_T *argvars, typval_T *rettv)
11804{
11805 float_T f = 0.0;
11806
11807 rettv->v_type = VAR_FLOAT;
11808 if (get_float_arg(argvars, &f) == OK)
11809 rettv->vval.v_float = sqrt(f);
11810 else
11811 rettv->vval.v_float = 0.0;
11812}
11813
11814/*
11815 * "str2float()" function
11816 */
11817 static void
11818f_str2float(typval_T *argvars, typval_T *rettv)
11819{
11820 char_u *p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011821 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011822
Bram Moolenaar08243d22017-01-10 16:12:29 +010011823 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011824 p = skipwhite(p + 1);
11825 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011826 if (isneg)
11827 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011828 rettv->v_type = VAR_FLOAT;
11829}
11830#endif
11831
11832/*
11833 * "str2nr()" function
11834 */
11835 static void
11836f_str2nr(typval_T *argvars, typval_T *rettv)
11837{
11838 int base = 10;
11839 char_u *p;
11840 varnumber_T n;
11841 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010011842 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011843
11844 if (argvars[1].v_type != VAR_UNKNOWN)
11845 {
11846 base = (int)get_tv_number(&argvars[1]);
11847 if (base != 2 && base != 8 && base != 10 && base != 16)
11848 {
11849 EMSG(_(e_invarg));
11850 return;
11851 }
11852 }
11853
11854 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011855 isneg = (*p == '-');
11856 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011857 p = skipwhite(p + 1);
11858 switch (base)
11859 {
11860 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
11861 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
11862 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
11863 default: what = 0;
11864 }
11865 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011866 if (isneg)
11867 rettv->vval.v_number = -n;
11868 else
11869 rettv->vval.v_number = n;
11870
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011871}
11872
11873#ifdef HAVE_STRFTIME
11874/*
11875 * "strftime({format}[, {time}])" function
11876 */
11877 static void
11878f_strftime(typval_T *argvars, typval_T *rettv)
11879{
11880 char_u result_buf[256];
11881 struct tm *curtime;
11882 time_t seconds;
11883 char_u *p;
11884
11885 rettv->v_type = VAR_STRING;
11886
11887 p = get_tv_string(&argvars[0]);
11888 if (argvars[1].v_type == VAR_UNKNOWN)
11889 seconds = time(NULL);
11890 else
11891 seconds = (time_t)get_tv_number(&argvars[1]);
11892 curtime = localtime(&seconds);
11893 /* MSVC returns NULL for an invalid value of seconds. */
11894 if (curtime == NULL)
11895 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
11896 else
11897 {
11898# ifdef FEAT_MBYTE
11899 vimconv_T conv;
11900 char_u *enc;
11901
11902 conv.vc_type = CONV_NONE;
11903 enc = enc_locale();
11904 convert_setup(&conv, p_enc, enc);
11905 if (conv.vc_type != CONV_NONE)
11906 p = string_convert(&conv, p, NULL);
11907# endif
11908 if (p != NULL)
11909 (void)strftime((char *)result_buf, sizeof(result_buf),
11910 (char *)p, curtime);
11911 else
11912 result_buf[0] = NUL;
11913
11914# ifdef FEAT_MBYTE
11915 if (conv.vc_type != CONV_NONE)
11916 vim_free(p);
11917 convert_setup(&conv, enc, p_enc);
11918 if (conv.vc_type != CONV_NONE)
11919 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
11920 else
11921# endif
11922 rettv->vval.v_string = vim_strsave(result_buf);
11923
11924# ifdef FEAT_MBYTE
11925 /* Release conversion descriptors */
11926 convert_setup(&conv, NULL, NULL);
11927 vim_free(enc);
11928# endif
11929 }
11930}
11931#endif
11932
11933/*
11934 * "strgetchar()" function
11935 */
11936 static void
11937f_strgetchar(typval_T *argvars, typval_T *rettv)
11938{
11939 char_u *str;
11940 int len;
11941 int error = FALSE;
11942 int charidx;
11943
11944 rettv->vval.v_number = -1;
11945 str = get_tv_string_chk(&argvars[0]);
11946 if (str == NULL)
11947 return;
11948 len = (int)STRLEN(str);
11949 charidx = (int)get_tv_number_chk(&argvars[1], &error);
11950 if (error)
11951 return;
11952#ifdef FEAT_MBYTE
11953 {
11954 int byteidx = 0;
11955
11956 while (charidx >= 0 && byteidx < len)
11957 {
11958 if (charidx == 0)
11959 {
11960 rettv->vval.v_number = mb_ptr2char(str + byteidx);
11961 break;
11962 }
11963 --charidx;
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011964 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011965 }
11966 }
11967#else
11968 if (charidx < len)
11969 rettv->vval.v_number = str[charidx];
11970#endif
11971}
11972
11973/*
11974 * "stridx()" function
11975 */
11976 static void
11977f_stridx(typval_T *argvars, typval_T *rettv)
11978{
11979 char_u buf[NUMBUFLEN];
11980 char_u *needle;
11981 char_u *haystack;
11982 char_u *save_haystack;
11983 char_u *pos;
11984 int start_idx;
11985
11986 needle = get_tv_string_chk(&argvars[1]);
11987 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
11988 rettv->vval.v_number = -1;
11989 if (needle == NULL || haystack == NULL)
11990 return; /* type error; errmsg already given */
11991
11992 if (argvars[2].v_type != VAR_UNKNOWN)
11993 {
11994 int error = FALSE;
11995
11996 start_idx = (int)get_tv_number_chk(&argvars[2], &error);
11997 if (error || start_idx >= (int)STRLEN(haystack))
11998 return;
11999 if (start_idx >= 0)
12000 haystack += start_idx;
12001 }
12002
12003 pos = (char_u *)strstr((char *)haystack, (char *)needle);
12004 if (pos != NULL)
12005 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
12006}
12007
12008/*
12009 * "string()" function
12010 */
12011 static void
12012f_string(typval_T *argvars, typval_T *rettv)
12013{
12014 char_u *tofree;
12015 char_u numbuf[NUMBUFLEN];
12016
12017 rettv->v_type = VAR_STRING;
12018 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
12019 get_copyID());
12020 /* Make a copy if we have a value but it's not in allocated memory. */
12021 if (rettv->vval.v_string != NULL && tofree == NULL)
12022 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
12023}
12024
12025/*
12026 * "strlen()" function
12027 */
12028 static void
12029f_strlen(typval_T *argvars, typval_T *rettv)
12030{
12031 rettv->vval.v_number = (varnumber_T)(STRLEN(
12032 get_tv_string(&argvars[0])));
12033}
12034
12035/*
12036 * "strchars()" function
12037 */
12038 static void
12039f_strchars(typval_T *argvars, typval_T *rettv)
12040{
12041 char_u *s = get_tv_string(&argvars[0]);
12042 int skipcc = 0;
12043#ifdef FEAT_MBYTE
12044 varnumber_T len = 0;
12045 int (*func_mb_ptr2char_adv)(char_u **pp);
12046#endif
12047
12048 if (argvars[1].v_type != VAR_UNKNOWN)
12049 skipcc = (int)get_tv_number_chk(&argvars[1], NULL);
12050 if (skipcc < 0 || skipcc > 1)
12051 EMSG(_(e_invarg));
12052 else
12053 {
12054#ifdef FEAT_MBYTE
12055 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
12056 while (*s != NUL)
12057 {
12058 func_mb_ptr2char_adv(&s);
12059 ++len;
12060 }
12061 rettv->vval.v_number = len;
12062#else
12063 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
12064#endif
12065 }
12066}
12067
12068/*
12069 * "strdisplaywidth()" function
12070 */
12071 static void
12072f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
12073{
12074 char_u *s = get_tv_string(&argvars[0]);
12075 int col = 0;
12076
12077 if (argvars[1].v_type != VAR_UNKNOWN)
12078 col = (int)get_tv_number(&argvars[1]);
12079
12080 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
12081}
12082
12083/*
12084 * "strwidth()" function
12085 */
12086 static void
12087f_strwidth(typval_T *argvars, typval_T *rettv)
12088{
12089 char_u *s = get_tv_string(&argvars[0]);
12090
12091 rettv->vval.v_number = (varnumber_T)(
12092#ifdef FEAT_MBYTE
12093 mb_string2cells(s, -1)
12094#else
12095 STRLEN(s)
12096#endif
12097 );
12098}
12099
12100/*
12101 * "strcharpart()" function
12102 */
12103 static void
12104f_strcharpart(typval_T *argvars, typval_T *rettv)
12105{
12106#ifdef FEAT_MBYTE
12107 char_u *p;
12108 int nchar;
12109 int nbyte = 0;
12110 int charlen;
12111 int len = 0;
12112 int slen;
12113 int error = FALSE;
12114
12115 p = get_tv_string(&argvars[0]);
12116 slen = (int)STRLEN(p);
12117
12118 nchar = (int)get_tv_number_chk(&argvars[1], &error);
12119 if (!error)
12120 {
12121 if (nchar > 0)
12122 while (nchar > 0 && nbyte < slen)
12123 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012124 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012125 --nchar;
12126 }
12127 else
12128 nbyte = nchar;
12129 if (argvars[2].v_type != VAR_UNKNOWN)
12130 {
12131 charlen = (int)get_tv_number(&argvars[2]);
12132 while (charlen > 0 && nbyte + len < slen)
12133 {
12134 int off = nbyte + len;
12135
12136 if (off < 0)
12137 len += 1;
12138 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012139 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012140 --charlen;
12141 }
12142 }
12143 else
12144 len = slen - nbyte; /* default: all bytes that are available. */
12145 }
12146
12147 /*
12148 * Only return the overlap between the specified part and the actual
12149 * string.
12150 */
12151 if (nbyte < 0)
12152 {
12153 len += nbyte;
12154 nbyte = 0;
12155 }
12156 else if (nbyte > slen)
12157 nbyte = slen;
12158 if (len < 0)
12159 len = 0;
12160 else if (nbyte + len > slen)
12161 len = slen - nbyte;
12162
12163 rettv->v_type = VAR_STRING;
12164 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
12165#else
12166 f_strpart(argvars, rettv);
12167#endif
12168}
12169
12170/*
12171 * "strpart()" function
12172 */
12173 static void
12174f_strpart(typval_T *argvars, typval_T *rettv)
12175{
12176 char_u *p;
12177 int n;
12178 int len;
12179 int slen;
12180 int error = FALSE;
12181
12182 p = get_tv_string(&argvars[0]);
12183 slen = (int)STRLEN(p);
12184
12185 n = (int)get_tv_number_chk(&argvars[1], &error);
12186 if (error)
12187 len = 0;
12188 else if (argvars[2].v_type != VAR_UNKNOWN)
12189 len = (int)get_tv_number(&argvars[2]);
12190 else
12191 len = slen - n; /* default len: all bytes that are available. */
12192
12193 /*
12194 * Only return the overlap between the specified part and the actual
12195 * string.
12196 */
12197 if (n < 0)
12198 {
12199 len += n;
12200 n = 0;
12201 }
12202 else if (n > slen)
12203 n = slen;
12204 if (len < 0)
12205 len = 0;
12206 else if (n + len > slen)
12207 len = slen - n;
12208
12209 rettv->v_type = VAR_STRING;
12210 rettv->vval.v_string = vim_strnsave(p + n, len);
12211}
12212
12213/*
12214 * "strridx()" function
12215 */
12216 static void
12217f_strridx(typval_T *argvars, typval_T *rettv)
12218{
12219 char_u buf[NUMBUFLEN];
12220 char_u *needle;
12221 char_u *haystack;
12222 char_u *rest;
12223 char_u *lastmatch = NULL;
12224 int haystack_len, end_idx;
12225
12226 needle = get_tv_string_chk(&argvars[1]);
12227 haystack = get_tv_string_buf_chk(&argvars[0], buf);
12228
12229 rettv->vval.v_number = -1;
12230 if (needle == NULL || haystack == NULL)
12231 return; /* type error; errmsg already given */
12232
12233 haystack_len = (int)STRLEN(haystack);
12234 if (argvars[2].v_type != VAR_UNKNOWN)
12235 {
12236 /* Third argument: upper limit for index */
12237 end_idx = (int)get_tv_number_chk(&argvars[2], NULL);
12238 if (end_idx < 0)
12239 return; /* can never find a match */
12240 }
12241 else
12242 end_idx = haystack_len;
12243
12244 if (*needle == NUL)
12245 {
12246 /* Empty string matches past the end. */
12247 lastmatch = haystack + end_idx;
12248 }
12249 else
12250 {
12251 for (rest = haystack; *rest != '\0'; ++rest)
12252 {
12253 rest = (char_u *)strstr((char *)rest, (char *)needle);
12254 if (rest == NULL || rest > haystack + end_idx)
12255 break;
12256 lastmatch = rest;
12257 }
12258 }
12259
12260 if (lastmatch == NULL)
12261 rettv->vval.v_number = -1;
12262 else
12263 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
12264}
12265
12266/*
12267 * "strtrans()" function
12268 */
12269 static void
12270f_strtrans(typval_T *argvars, typval_T *rettv)
12271{
12272 rettv->v_type = VAR_STRING;
12273 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
12274}
12275
12276/*
12277 * "submatch()" function
12278 */
12279 static void
12280f_submatch(typval_T *argvars, typval_T *rettv)
12281{
12282 int error = FALSE;
12283 int no;
12284 int retList = 0;
12285
12286 no = (int)get_tv_number_chk(&argvars[0], &error);
12287 if (error)
12288 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012289 if (no < 0 || no >= NSUBEXP)
12290 {
Bram Moolenaar79518e22017-02-17 16:31:35 +010012291 EMSGN(_("E935: invalid submatch number: %d"), no);
12292 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012293 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012294 if (argvars[1].v_type != VAR_UNKNOWN)
12295 retList = (int)get_tv_number_chk(&argvars[1], &error);
12296 if (error)
12297 return;
12298
12299 if (retList == 0)
12300 {
12301 rettv->v_type = VAR_STRING;
12302 rettv->vval.v_string = reg_submatch(no);
12303 }
12304 else
12305 {
12306 rettv->v_type = VAR_LIST;
12307 rettv->vval.v_list = reg_submatch_list(no);
12308 }
12309}
12310
12311/*
12312 * "substitute()" function
12313 */
12314 static void
12315f_substitute(typval_T *argvars, typval_T *rettv)
12316{
12317 char_u patbuf[NUMBUFLEN];
12318 char_u subbuf[NUMBUFLEN];
12319 char_u flagsbuf[NUMBUFLEN];
12320
12321 char_u *str = get_tv_string_chk(&argvars[0]);
12322 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012323 char_u *sub = NULL;
12324 typval_T *expr = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012325 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
12326
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012327 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
12328 expr = &argvars[2];
12329 else
12330 sub = get_tv_string_buf_chk(&argvars[2], subbuf);
12331
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012332 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012333 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
12334 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012335 rettv->vval.v_string = NULL;
12336 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012337 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012338}
12339
12340/*
12341 * "synID(lnum, col, trans)" function
12342 */
12343 static void
12344f_synID(typval_T *argvars UNUSED, typval_T *rettv)
12345{
12346 int id = 0;
12347#ifdef FEAT_SYN_HL
12348 linenr_T lnum;
12349 colnr_T col;
12350 int trans;
12351 int transerr = FALSE;
12352
12353 lnum = get_tv_lnum(argvars); /* -1 on type error */
12354 col = (linenr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12355 trans = (int)get_tv_number_chk(&argvars[2], &transerr);
12356
12357 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12358 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
12359 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
12360#endif
12361
12362 rettv->vval.v_number = id;
12363}
12364
12365/*
12366 * "synIDattr(id, what [, mode])" function
12367 */
12368 static void
12369f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
12370{
12371 char_u *p = NULL;
12372#ifdef FEAT_SYN_HL
12373 int id;
12374 char_u *what;
12375 char_u *mode;
12376 char_u modebuf[NUMBUFLEN];
12377 int modec;
12378
12379 id = (int)get_tv_number(&argvars[0]);
12380 what = get_tv_string(&argvars[1]);
12381 if (argvars[2].v_type != VAR_UNKNOWN)
12382 {
12383 mode = get_tv_string_buf(&argvars[2], modebuf);
12384 modec = TOLOWER_ASC(mode[0]);
12385 if (modec != 't' && modec != 'c' && modec != 'g')
12386 modec = 0; /* replace invalid with current */
12387 }
12388 else
12389 {
12390#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
12391 if (USE_24BIT)
12392 modec = 'g';
12393 else
12394#endif
12395 if (t_colors > 1)
12396 modec = 'c';
12397 else
12398 modec = 't';
12399 }
12400
12401
12402 switch (TOLOWER_ASC(what[0]))
12403 {
12404 case 'b':
12405 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
12406 p = highlight_color(id, what, modec);
12407 else /* bold */
12408 p = highlight_has_attr(id, HL_BOLD, modec);
12409 break;
12410
12411 case 'f': /* fg[#] or font */
12412 p = highlight_color(id, what, modec);
12413 break;
12414
12415 case 'i':
12416 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
12417 p = highlight_has_attr(id, HL_INVERSE, modec);
12418 else /* italic */
12419 p = highlight_has_attr(id, HL_ITALIC, modec);
12420 break;
12421
12422 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020012423 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012424 break;
12425
12426 case 'r': /* reverse */
12427 p = highlight_has_attr(id, HL_INVERSE, modec);
12428 break;
12429
12430 case 's':
12431 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
12432 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020012433 /* strikeout */
12434 else if (TOLOWER_ASC(what[1]) == 't' &&
12435 TOLOWER_ASC(what[2]) == 'r')
12436 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012437 else /* standout */
12438 p = highlight_has_attr(id, HL_STANDOUT, modec);
12439 break;
12440
12441 case 'u':
12442 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
12443 /* underline */
12444 p = highlight_has_attr(id, HL_UNDERLINE, modec);
12445 else
12446 /* undercurl */
12447 p = highlight_has_attr(id, HL_UNDERCURL, modec);
12448 break;
12449 }
12450
12451 if (p != NULL)
12452 p = vim_strsave(p);
12453#endif
12454 rettv->v_type = VAR_STRING;
12455 rettv->vval.v_string = p;
12456}
12457
12458/*
12459 * "synIDtrans(id)" function
12460 */
12461 static void
12462f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
12463{
12464 int id;
12465
12466#ifdef FEAT_SYN_HL
12467 id = (int)get_tv_number(&argvars[0]);
12468
12469 if (id > 0)
12470 id = syn_get_final_id(id);
12471 else
12472#endif
12473 id = 0;
12474
12475 rettv->vval.v_number = id;
12476}
12477
12478/*
12479 * "synconcealed(lnum, col)" function
12480 */
12481 static void
12482f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
12483{
12484#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12485 linenr_T lnum;
12486 colnr_T col;
12487 int syntax_flags = 0;
12488 int cchar;
12489 int matchid = 0;
12490 char_u str[NUMBUFLEN];
12491#endif
12492
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012493 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012494
12495#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12496 lnum = get_tv_lnum(argvars); /* -1 on type error */
12497 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12498
12499 vim_memset(str, NUL, sizeof(str));
12500
12501 if (rettv_list_alloc(rettv) != FAIL)
12502 {
12503 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12504 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12505 && curwin->w_p_cole > 0)
12506 {
12507 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
12508 syntax_flags = get_syntax_info(&matchid);
12509
12510 /* get the conceal character */
12511 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
12512 {
12513 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020012514 if (cchar == NUL && curwin->w_p_cole == 1)
12515 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012516 if (cchar != NUL)
12517 {
12518# ifdef FEAT_MBYTE
12519 if (has_mbyte)
12520 (*mb_char2bytes)(cchar, str);
12521 else
12522# endif
12523 str[0] = cchar;
12524 }
12525 }
12526 }
12527
12528 list_append_number(rettv->vval.v_list,
12529 (syntax_flags & HL_CONCEAL) != 0);
12530 /* -1 to auto-determine strlen */
12531 list_append_string(rettv->vval.v_list, str, -1);
12532 list_append_number(rettv->vval.v_list, matchid);
12533 }
12534#endif
12535}
12536
12537/*
12538 * "synstack(lnum, col)" function
12539 */
12540 static void
12541f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
12542{
12543#ifdef FEAT_SYN_HL
12544 linenr_T lnum;
12545 colnr_T col;
12546 int i;
12547 int id;
12548#endif
12549
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012550 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012551
12552#ifdef FEAT_SYN_HL
12553 lnum = get_tv_lnum(argvars); /* -1 on type error */
12554 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12555
12556 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12557 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12558 && rettv_list_alloc(rettv) != FAIL)
12559 {
12560 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
12561 for (i = 0; ; ++i)
12562 {
12563 id = syn_get_stack_item(i);
12564 if (id < 0)
12565 break;
12566 if (list_append_number(rettv->vval.v_list, id) == FAIL)
12567 break;
12568 }
12569 }
12570#endif
12571}
12572
12573 static void
12574get_cmd_output_as_rettv(
12575 typval_T *argvars,
12576 typval_T *rettv,
12577 int retlist)
12578{
12579 char_u *res = NULL;
12580 char_u *p;
12581 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012582 int err = FALSE;
12583 FILE *fd;
12584 list_T *list = NULL;
12585 int flags = SHELL_SILENT;
12586
12587 rettv->v_type = VAR_STRING;
12588 rettv->vval.v_string = NULL;
12589 if (check_restricted() || check_secure())
12590 goto errret;
12591
12592 if (argvars[1].v_type != VAR_UNKNOWN)
12593 {
12594 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010012595 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012596 * command.
12597 */
12598 if ((infile = vim_tempname('i', TRUE)) == NULL)
12599 {
12600 EMSG(_(e_notmp));
12601 goto errret;
12602 }
12603
12604 fd = mch_fopen((char *)infile, WRITEBIN);
12605 if (fd == NULL)
12606 {
12607 EMSG2(_(e_notopen), infile);
12608 goto errret;
12609 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010012610 if (argvars[1].v_type == VAR_NUMBER)
12611 {
12612 linenr_T lnum;
12613 buf_T *buf;
12614
12615 buf = buflist_findnr(argvars[1].vval.v_number);
12616 if (buf == NULL)
12617 {
12618 EMSGN(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010012619 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010012620 goto errret;
12621 }
12622
12623 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
12624 {
12625 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
12626 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
12627 {
12628 err = TRUE;
12629 break;
12630 }
12631 if (putc(NL, fd) == EOF)
12632 {
12633 err = TRUE;
12634 break;
12635 }
12636 }
12637 }
12638 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012639 {
12640 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
12641 err = TRUE;
12642 }
12643 else
12644 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010012645 size_t len;
12646 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012647
12648 p = get_tv_string_buf_chk(&argvars[1], buf);
12649 if (p == NULL)
12650 {
12651 fclose(fd);
12652 goto errret; /* type error; errmsg already given */
12653 }
12654 len = STRLEN(p);
12655 if (len > 0 && fwrite(p, len, 1, fd) != 1)
12656 err = TRUE;
12657 }
12658 if (fclose(fd) != 0)
12659 err = TRUE;
12660 if (err)
12661 {
12662 EMSG(_("E677: Error writing temp file"));
12663 goto errret;
12664 }
12665 }
12666
12667 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
12668 * echoes typeahead, that messes up the display. */
12669 if (!msg_silent)
12670 flags += SHELL_COOKED;
12671
12672 if (retlist)
12673 {
12674 int len;
12675 listitem_T *li;
12676 char_u *s = NULL;
12677 char_u *start;
12678 char_u *end;
12679 int i;
12680
12681 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
12682 if (res == NULL)
12683 goto errret;
12684
12685 list = list_alloc();
12686 if (list == NULL)
12687 goto errret;
12688
12689 for (i = 0; i < len; ++i)
12690 {
12691 start = res + i;
12692 while (i < len && res[i] != NL)
12693 ++i;
12694 end = res + i;
12695
12696 s = alloc((unsigned)(end - start + 1));
12697 if (s == NULL)
12698 goto errret;
12699
12700 for (p = s; start < end; ++p, ++start)
12701 *p = *start == NUL ? NL : *start;
12702 *p = NUL;
12703
12704 li = listitem_alloc();
12705 if (li == NULL)
12706 {
12707 vim_free(s);
12708 goto errret;
12709 }
12710 li->li_tv.v_type = VAR_STRING;
12711 li->li_tv.v_lock = 0;
12712 li->li_tv.vval.v_string = s;
12713 list_append(list, li);
12714 }
12715
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012716 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012717 list = NULL;
12718 }
12719 else
12720 {
12721 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
12722#ifdef USE_CR
12723 /* translate <CR> into <NL> */
12724 if (res != NULL)
12725 {
12726 char_u *s;
12727
12728 for (s = res; *s; ++s)
12729 {
12730 if (*s == CAR)
12731 *s = NL;
12732 }
12733 }
12734#else
12735# ifdef USE_CRNL
12736 /* translate <CR><NL> into <NL> */
12737 if (res != NULL)
12738 {
12739 char_u *s, *d;
12740
12741 d = res;
12742 for (s = res; *s; ++s)
12743 {
12744 if (s[0] == CAR && s[1] == NL)
12745 ++s;
12746 *d++ = *s;
12747 }
12748 *d = NUL;
12749 }
12750# endif
12751#endif
12752 rettv->vval.v_string = res;
12753 res = NULL;
12754 }
12755
12756errret:
12757 if (infile != NULL)
12758 {
12759 mch_remove(infile);
12760 vim_free(infile);
12761 }
12762 if (res != NULL)
12763 vim_free(res);
12764 if (list != NULL)
12765 list_free(list);
12766}
12767
12768/*
12769 * "system()" function
12770 */
12771 static void
12772f_system(typval_T *argvars, typval_T *rettv)
12773{
12774 get_cmd_output_as_rettv(argvars, rettv, FALSE);
12775}
12776
12777/*
12778 * "systemlist()" function
12779 */
12780 static void
12781f_systemlist(typval_T *argvars, typval_T *rettv)
12782{
12783 get_cmd_output_as_rettv(argvars, rettv, TRUE);
12784}
12785
12786/*
12787 * "tabpagebuflist()" function
12788 */
12789 static void
12790f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12791{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012792 tabpage_T *tp;
12793 win_T *wp = NULL;
12794
12795 if (argvars[0].v_type == VAR_UNKNOWN)
12796 wp = firstwin;
12797 else
12798 {
12799 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12800 if (tp != NULL)
12801 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12802 }
12803 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
12804 {
12805 for (; wp != NULL; wp = wp->w_next)
12806 if (list_append_number(rettv->vval.v_list,
12807 wp->w_buffer->b_fnum) == FAIL)
12808 break;
12809 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012810}
12811
12812
12813/*
12814 * "tabpagenr()" function
12815 */
12816 static void
12817f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
12818{
12819 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012820 char_u *arg;
12821
12822 if (argvars[0].v_type != VAR_UNKNOWN)
12823 {
12824 arg = get_tv_string_chk(&argvars[0]);
12825 nr = 0;
12826 if (arg != NULL)
12827 {
12828 if (STRCMP(arg, "$") == 0)
12829 nr = tabpage_index(NULL) - 1;
12830 else
12831 EMSG2(_(e_invexpr2), arg);
12832 }
12833 }
12834 else
12835 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012836 rettv->vval.v_number = nr;
12837}
12838
12839
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012840static int get_winnr(tabpage_T *tp, typval_T *argvar);
12841
12842/*
12843 * Common code for tabpagewinnr() and winnr().
12844 */
12845 static int
12846get_winnr(tabpage_T *tp, typval_T *argvar)
12847{
12848 win_T *twin;
12849 int nr = 1;
12850 win_T *wp;
12851 char_u *arg;
12852
12853 twin = (tp == curtab) ? curwin : tp->tp_curwin;
12854 if (argvar->v_type != VAR_UNKNOWN)
12855 {
12856 arg = get_tv_string_chk(argvar);
12857 if (arg == NULL)
12858 nr = 0; /* type error; errmsg already given */
12859 else if (STRCMP(arg, "$") == 0)
12860 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
12861 else if (STRCMP(arg, "#") == 0)
12862 {
12863 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
12864 if (twin == NULL)
12865 nr = 0;
12866 }
12867 else
12868 {
12869 EMSG2(_(e_invexpr2), arg);
12870 nr = 0;
12871 }
12872 }
12873
12874 if (nr > 0)
12875 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12876 wp != twin; wp = wp->w_next)
12877 {
12878 if (wp == NULL)
12879 {
12880 /* didn't find it in this tabpage */
12881 nr = 0;
12882 break;
12883 }
12884 ++nr;
12885 }
12886 return nr;
12887}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012888
12889/*
12890 * "tabpagewinnr()" function
12891 */
12892 static void
12893f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
12894{
12895 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012896 tabpage_T *tp;
12897
12898 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12899 if (tp == NULL)
12900 nr = 0;
12901 else
12902 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012903 rettv->vval.v_number = nr;
12904}
12905
12906
12907/*
12908 * "tagfiles()" function
12909 */
12910 static void
12911f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
12912{
12913 char_u *fname;
12914 tagname_T tn;
12915 int first;
12916
12917 if (rettv_list_alloc(rettv) == FAIL)
12918 return;
12919 fname = alloc(MAXPATHL);
12920 if (fname == NULL)
12921 return;
12922
12923 for (first = TRUE; ; first = FALSE)
12924 if (get_tagfname(&tn, first, fname) == FAIL
12925 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
12926 break;
12927 tagname_free(&tn);
12928 vim_free(fname);
12929}
12930
12931/*
12932 * "taglist()" function
12933 */
12934 static void
12935f_taglist(typval_T *argvars, typval_T *rettv)
12936{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012937 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012938 char_u *tag_pattern;
12939
12940 tag_pattern = get_tv_string(&argvars[0]);
12941
12942 rettv->vval.v_number = FALSE;
12943 if (*tag_pattern == NUL)
12944 return;
12945
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012946 if (argvars[1].v_type != VAR_UNKNOWN)
12947 fname = get_tv_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012948 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012949 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012950}
12951
12952/*
12953 * "tempname()" function
12954 */
12955 static void
12956f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
12957{
12958 static int x = 'A';
12959
12960 rettv->v_type = VAR_STRING;
12961 rettv->vval.v_string = vim_tempname(x, FALSE);
12962
12963 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
12964 * names. Skip 'I' and 'O', they are used for shell redirection. */
12965 do
12966 {
12967 if (x == 'Z')
12968 x = '0';
12969 else if (x == '9')
12970 x = 'A';
12971 else
12972 {
12973#ifdef EBCDIC
12974 if (x == 'I')
12975 x = 'J';
12976 else if (x == 'R')
12977 x = 'S';
12978 else
12979#endif
12980 ++x;
12981 }
12982 } while (x == 'I' || x == 'O');
12983}
12984
12985#ifdef FEAT_FLOAT
12986/*
12987 * "tan()" function
12988 */
12989 static void
12990f_tan(typval_T *argvars, typval_T *rettv)
12991{
12992 float_T f = 0.0;
12993
12994 rettv->v_type = VAR_FLOAT;
12995 if (get_float_arg(argvars, &f) == OK)
12996 rettv->vval.v_float = tan(f);
12997 else
12998 rettv->vval.v_float = 0.0;
12999}
13000
13001/*
13002 * "tanh()" function
13003 */
13004 static void
13005f_tanh(typval_T *argvars, typval_T *rettv)
13006{
13007 float_T f = 0.0;
13008
13009 rettv->v_type = VAR_FLOAT;
13010 if (get_float_arg(argvars, &f) == OK)
13011 rettv->vval.v_float = tanh(f);
13012 else
13013 rettv->vval.v_float = 0.0;
13014}
13015#endif
13016
13017/*
13018 * "test_alloc_fail(id, countdown, repeat)" function
13019 */
13020 static void
13021f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
13022{
13023 if (argvars[0].v_type != VAR_NUMBER
13024 || argvars[0].vval.v_number <= 0
13025 || argvars[1].v_type != VAR_NUMBER
13026 || argvars[1].vval.v_number < 0
13027 || argvars[2].v_type != VAR_NUMBER)
13028 EMSG(_(e_invarg));
13029 else
13030 {
13031 alloc_fail_id = argvars[0].vval.v_number;
13032 if (alloc_fail_id >= aid_last)
13033 EMSG(_(e_invarg));
13034 alloc_fail_countdown = argvars[1].vval.v_number;
13035 alloc_fail_repeat = argvars[2].vval.v_number;
13036 did_outofmem_msg = FALSE;
13037 }
13038}
13039
13040/*
13041 * "test_autochdir()"
13042 */
13043 static void
13044f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13045{
13046#if defined(FEAT_AUTOCHDIR)
13047 test_autochdir = TRUE;
13048#endif
13049}
13050
13051/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020013052 * "test_feedinput()"
13053 */
13054 static void
13055f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
13056{
13057#ifdef USE_INPUT_BUF
13058 char_u *val = get_tv_string_chk(&argvars[0]);
13059
13060 if (val != NULL)
13061 {
13062 trash_input_buf();
13063 add_to_input_buf_csi(val, (int)STRLEN(val));
13064 }
13065#endif
13066}
13067
13068/*
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013069 * "test_disable({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013070 */
13071 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013072f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013073{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013074 char_u *name = (char_u *)"";
13075 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013076 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013077
13078 if (argvars[0].v_type != VAR_STRING
13079 || (argvars[1].v_type) != VAR_NUMBER)
13080 EMSG(_(e_invarg));
13081 else
13082 {
13083 name = get_tv_string_chk(&argvars[0]);
13084 val = (int)get_tv_number(&argvars[1]);
13085
13086 if (STRCMP(name, (char_u *)"redraw") == 0)
13087 disable_redraw_for_testing = val;
13088 else if (STRCMP(name, (char_u *)"char_avail") == 0)
13089 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013090 else if (STRCMP(name, (char_u *)"starting") == 0)
13091 {
13092 if (val)
13093 {
13094 if (save_starting < 0)
13095 save_starting = starting;
13096 starting = 0;
13097 }
13098 else
13099 {
13100 starting = save_starting;
13101 save_starting = -1;
13102 }
13103 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013104 else if (STRCMP(name, (char_u *)"nfa_fail") == 0)
13105 nfa_fail_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013106 else if (STRCMP(name, (char_u *)"ALL") == 0)
13107 {
13108 disable_char_avail_for_testing = FALSE;
13109 disable_redraw_for_testing = FALSE;
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013110 nfa_fail_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013111 if (save_starting >= 0)
13112 {
13113 starting = save_starting;
13114 save_starting = -1;
13115 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013116 }
13117 else
13118 EMSG2(_(e_invarg2), name);
13119 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013120}
13121
13122/*
13123 * "test_garbagecollect_now()" function
13124 */
13125 static void
13126f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13127{
13128 /* This is dangerous, any Lists and Dicts used internally may be freed
13129 * while still in use. */
13130 garbage_collect(TRUE);
13131}
13132
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013133/*
13134 * "test_ignore_error()" function
13135 */
13136 static void
13137f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
13138{
13139 ignore_error_for_testing(get_tv_string(&argvars[0]));
13140}
13141
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013142#ifdef FEAT_JOB_CHANNEL
13143 static void
13144f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
13145{
13146 rettv->v_type = VAR_CHANNEL;
13147 rettv->vval.v_channel = NULL;
13148}
13149#endif
13150
13151 static void
13152f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
13153{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013154 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013155}
13156
13157#ifdef FEAT_JOB_CHANNEL
13158 static void
13159f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
13160{
13161 rettv->v_type = VAR_JOB;
13162 rettv->vval.v_job = NULL;
13163}
13164#endif
13165
13166 static void
13167f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
13168{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013169 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013170}
13171
13172 static void
13173f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
13174{
13175 rettv->v_type = VAR_PARTIAL;
13176 rettv->vval.v_partial = NULL;
13177}
13178
13179 static void
13180f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
13181{
13182 rettv->v_type = VAR_STRING;
13183 rettv->vval.v_string = NULL;
13184}
13185
13186 static void
13187f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
13188{
13189 time_for_testing = (time_t)get_tv_number(&argvars[0]);
13190}
13191
13192#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
13193/*
13194 * Get a callback from "arg". It can be a Funcref or a function name.
13195 * When "arg" is zero return an empty string.
13196 * Return NULL for an invalid argument.
13197 */
13198 char_u *
13199get_callback(typval_T *arg, partial_T **pp)
13200{
13201 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
13202 {
13203 *pp = arg->vval.v_partial;
13204 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020013205 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013206 }
13207 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020013208 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013209 {
13210 func_ref(arg->vval.v_string);
13211 return arg->vval.v_string;
13212 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013213 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
13214 return (char_u *)"";
13215 EMSG(_("E921: Invalid callback argument"));
13216 return NULL;
13217}
13218
13219/*
Bram Moolenaard5359b22018-04-05 22:44:39 +020013220 * Unref/free "callback" and "partial" returned by get_callback().
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013221 */
13222 void
13223free_callback(char_u *callback, partial_T *partial)
13224{
13225 if (partial != NULL)
13226 partial_unref(partial);
13227 else if (callback != NULL)
13228 {
13229 func_unref(callback);
13230 vim_free(callback);
13231 }
13232}
13233#endif
13234
13235#ifdef FEAT_TIMERS
13236/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020013237 * "timer_info([timer])" function
13238 */
13239 static void
13240f_timer_info(typval_T *argvars, typval_T *rettv)
13241{
13242 timer_T *timer = NULL;
13243
13244 if (rettv_list_alloc(rettv) != OK)
13245 return;
13246 if (argvars[0].v_type != VAR_UNKNOWN)
13247 {
13248 if (argvars[0].v_type != VAR_NUMBER)
13249 EMSG(_(e_number_exp));
13250 else
13251 {
13252 timer = find_timer((int)get_tv_number(&argvars[0]));
13253 if (timer != NULL)
13254 add_timer_info(rettv, timer);
13255 }
13256 }
13257 else
13258 add_timer_info_all(rettv);
13259}
13260
13261/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013262 * "timer_pause(timer, paused)" function
13263 */
13264 static void
13265f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
13266{
13267 timer_T *timer = NULL;
13268 int paused = (int)get_tv_number(&argvars[1]);
13269
13270 if (argvars[0].v_type != VAR_NUMBER)
13271 EMSG(_(e_number_exp));
13272 else
13273 {
13274 timer = find_timer((int)get_tv_number(&argvars[0]));
13275 if (timer != NULL)
13276 timer->tr_paused = paused;
13277 }
13278}
13279
13280/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013281 * "timer_start(time, callback [, options])" function
13282 */
13283 static void
13284f_timer_start(typval_T *argvars, typval_T *rettv)
13285{
Bram Moolenaar75537a92016-09-05 22:45:28 +020013286 long msec = (long)get_tv_number(&argvars[0]);
13287 timer_T *timer;
13288 int repeat = 0;
13289 char_u *callback;
13290 dict_T *dict;
13291 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013292
Bram Moolenaar75537a92016-09-05 22:45:28 +020013293 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013294 if (check_secure())
13295 return;
13296 if (argvars[2].v_type != VAR_UNKNOWN)
13297 {
13298 if (argvars[2].v_type != VAR_DICT
13299 || (dict = argvars[2].vval.v_dict) == NULL)
13300 {
13301 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
13302 return;
13303 }
13304 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
13305 repeat = get_dict_number(dict, (char_u *)"repeat");
13306 }
13307
Bram Moolenaar75537a92016-09-05 22:45:28 +020013308 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013309 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020013310 return;
13311
13312 timer = create_timer(msec, repeat);
13313 if (timer == NULL)
13314 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013315 else
13316 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020013317 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020013318 timer->tr_callback = vim_strsave(callback);
13319 else
13320 /* pointer into the partial */
13321 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020013322 timer->tr_partial = partial;
13323 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013324 }
13325}
13326
13327/*
13328 * "timer_stop(timer)" function
13329 */
13330 static void
13331f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
13332{
13333 timer_T *timer;
13334
13335 if (argvars[0].v_type != VAR_NUMBER)
13336 {
13337 EMSG(_(e_number_exp));
13338 return;
13339 }
13340 timer = find_timer((int)get_tv_number(&argvars[0]));
13341 if (timer != NULL)
13342 stop_timer(timer);
13343}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013344
13345/*
13346 * "timer_stopall()" function
13347 */
13348 static void
13349f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13350{
13351 stop_all_timers();
13352}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013353#endif
13354
13355/*
13356 * "tolower(string)" function
13357 */
13358 static void
13359f_tolower(typval_T *argvars, typval_T *rettv)
13360{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013361 rettv->v_type = VAR_STRING;
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +010013362 rettv->vval.v_string = strlow_save(get_tv_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013363}
13364
13365/*
13366 * "toupper(string)" function
13367 */
13368 static void
13369f_toupper(typval_T *argvars, typval_T *rettv)
13370{
13371 rettv->v_type = VAR_STRING;
13372 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
13373}
13374
13375/*
13376 * "tr(string, fromstr, tostr)" function
13377 */
13378 static void
13379f_tr(typval_T *argvars, typval_T *rettv)
13380{
13381 char_u *in_str;
13382 char_u *fromstr;
13383 char_u *tostr;
13384 char_u *p;
13385#ifdef FEAT_MBYTE
13386 int inlen;
13387 int fromlen;
13388 int tolen;
13389 int idx;
13390 char_u *cpstr;
13391 int cplen;
13392 int first = TRUE;
13393#endif
13394 char_u buf[NUMBUFLEN];
13395 char_u buf2[NUMBUFLEN];
13396 garray_T ga;
13397
13398 in_str = get_tv_string(&argvars[0]);
13399 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
13400 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
13401
13402 /* Default return value: empty string. */
13403 rettv->v_type = VAR_STRING;
13404 rettv->vval.v_string = NULL;
13405 if (fromstr == NULL || tostr == NULL)
13406 return; /* type error; errmsg already given */
13407 ga_init2(&ga, (int)sizeof(char), 80);
13408
13409#ifdef FEAT_MBYTE
13410 if (!has_mbyte)
13411#endif
13412 /* not multi-byte: fromstr and tostr must be the same length */
13413 if (STRLEN(fromstr) != STRLEN(tostr))
13414 {
13415#ifdef FEAT_MBYTE
13416error:
13417#endif
13418 EMSG2(_(e_invarg2), fromstr);
13419 ga_clear(&ga);
13420 return;
13421 }
13422
13423 /* fromstr and tostr have to contain the same number of chars */
13424 while (*in_str != NUL)
13425 {
13426#ifdef FEAT_MBYTE
13427 if (has_mbyte)
13428 {
13429 inlen = (*mb_ptr2len)(in_str);
13430 cpstr = in_str;
13431 cplen = inlen;
13432 idx = 0;
13433 for (p = fromstr; *p != NUL; p += fromlen)
13434 {
13435 fromlen = (*mb_ptr2len)(p);
13436 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
13437 {
13438 for (p = tostr; *p != NUL; p += tolen)
13439 {
13440 tolen = (*mb_ptr2len)(p);
13441 if (idx-- == 0)
13442 {
13443 cplen = tolen;
13444 cpstr = p;
13445 break;
13446 }
13447 }
13448 if (*p == NUL) /* tostr is shorter than fromstr */
13449 goto error;
13450 break;
13451 }
13452 ++idx;
13453 }
13454
13455 if (first && cpstr == in_str)
13456 {
13457 /* Check that fromstr and tostr have the same number of
13458 * (multi-byte) characters. Done only once when a character
13459 * of in_str doesn't appear in fromstr. */
13460 first = FALSE;
13461 for (p = tostr; *p != NUL; p += tolen)
13462 {
13463 tolen = (*mb_ptr2len)(p);
13464 --idx;
13465 }
13466 if (idx != 0)
13467 goto error;
13468 }
13469
13470 (void)ga_grow(&ga, cplen);
13471 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
13472 ga.ga_len += cplen;
13473
13474 in_str += inlen;
13475 }
13476 else
13477#endif
13478 {
13479 /* When not using multi-byte chars we can do it faster. */
13480 p = vim_strchr(fromstr, *in_str);
13481 if (p != NULL)
13482 ga_append(&ga, tostr[p - fromstr]);
13483 else
13484 ga_append(&ga, *in_str);
13485 ++in_str;
13486 }
13487 }
13488
13489 /* add a terminating NUL */
13490 (void)ga_grow(&ga, 1);
13491 ga_append(&ga, NUL);
13492
13493 rettv->vval.v_string = ga.ga_data;
13494}
13495
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010013496/*
13497 * "trim({expr})" function
13498 */
13499 static void
13500f_trim(typval_T *argvars, typval_T *rettv)
13501{
13502 char_u buf1[NUMBUFLEN];
13503 char_u buf2[NUMBUFLEN];
13504 char_u *head = get_tv_string_buf_chk(&argvars[0], buf1);
13505 char_u *mask = NULL;
13506 char_u *tail;
13507 char_u *prev;
13508 char_u *p;
13509 int c1;
13510
13511 rettv->v_type = VAR_STRING;
13512 if (head == NULL)
13513 {
13514 rettv->vval.v_string = NULL;
13515 return;
13516 }
13517
13518 if (argvars[1].v_type == VAR_STRING)
13519 mask = get_tv_string_buf_chk(&argvars[1], buf2);
13520
13521 while (*head != NUL)
13522 {
13523 c1 = PTR2CHAR(head);
13524 if (mask == NULL)
13525 {
13526 if (c1 > ' ' && c1 != 0xa0)
13527 break;
13528 }
13529 else
13530 {
13531 for (p = mask; *p != NUL; MB_PTR_ADV(p))
13532 if (c1 == PTR2CHAR(p))
13533 break;
13534 if (*p == NUL)
13535 break;
13536 }
13537 MB_PTR_ADV(head);
13538 }
13539
13540 for (tail = head + STRLEN(head); tail > head; tail = prev)
13541 {
13542 prev = tail;
13543 MB_PTR_BACK(head, prev);
13544 c1 = PTR2CHAR(prev);
13545 if (mask == NULL)
13546 {
13547 if (c1 > ' ' && c1 != 0xa0)
13548 break;
13549 }
13550 else
13551 {
13552 for (p = mask; *p != NUL; MB_PTR_ADV(p))
13553 if (c1 == PTR2CHAR(p))
13554 break;
13555 if (*p == NUL)
13556 break;
13557 }
13558 }
13559 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
13560}
13561
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013562#ifdef FEAT_FLOAT
13563/*
13564 * "trunc({float})" function
13565 */
13566 static void
13567f_trunc(typval_T *argvars, typval_T *rettv)
13568{
13569 float_T f = 0.0;
13570
13571 rettv->v_type = VAR_FLOAT;
13572 if (get_float_arg(argvars, &f) == OK)
13573 /* trunc() is not in C90, use floor() or ceil() instead. */
13574 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
13575 else
13576 rettv->vval.v_float = 0.0;
13577}
13578#endif
13579
13580/*
13581 * "type(expr)" function
13582 */
13583 static void
13584f_type(typval_T *argvars, typval_T *rettv)
13585{
13586 int n = -1;
13587
13588 switch (argvars[0].v_type)
13589 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020013590 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
13591 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013592 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020013593 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
13594 case VAR_LIST: n = VAR_TYPE_LIST; break;
13595 case VAR_DICT: n = VAR_TYPE_DICT; break;
13596 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013597 case VAR_SPECIAL:
13598 if (argvars[0].vval.v_number == VVAL_FALSE
13599 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020013600 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013601 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020013602 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013603 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020013604 case VAR_JOB: n = VAR_TYPE_JOB; break;
13605 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013606 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010013607 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013608 n = -1;
13609 break;
13610 }
13611 rettv->vval.v_number = n;
13612}
13613
13614/*
13615 * "undofile(name)" function
13616 */
13617 static void
13618f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
13619{
13620 rettv->v_type = VAR_STRING;
13621#ifdef FEAT_PERSISTENT_UNDO
13622 {
13623 char_u *fname = get_tv_string(&argvars[0]);
13624
13625 if (*fname == NUL)
13626 {
13627 /* If there is no file name there will be no undo file. */
13628 rettv->vval.v_string = NULL;
13629 }
13630 else
13631 {
13632 char_u *ffname = FullName_save(fname, FALSE);
13633
13634 if (ffname != NULL)
13635 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
13636 vim_free(ffname);
13637 }
13638 }
13639#else
13640 rettv->vval.v_string = NULL;
13641#endif
13642}
13643
13644/*
13645 * "undotree()" function
13646 */
13647 static void
13648f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
13649{
13650 if (rettv_dict_alloc(rettv) == OK)
13651 {
13652 dict_T *dict = rettv->vval.v_dict;
13653 list_T *list;
13654
Bram Moolenaare0be1672018-07-08 16:50:37 +020013655 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
13656 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
13657 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
13658 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
13659 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
13660 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013661
13662 list = list_alloc();
13663 if (list != NULL)
13664 {
13665 u_eval_tree(curbuf->b_u_oldhead, list);
13666 dict_add_list(dict, "entries", list);
13667 }
13668 }
13669}
13670
13671/*
13672 * "values(dict)" function
13673 */
13674 static void
13675f_values(typval_T *argvars, typval_T *rettv)
13676{
13677 dict_list(argvars, rettv, 1);
13678}
13679
13680/*
13681 * "virtcol(string)" function
13682 */
13683 static void
13684f_virtcol(typval_T *argvars, typval_T *rettv)
13685{
13686 colnr_T vcol = 0;
13687 pos_T *fp;
13688 int fnum = curbuf->b_fnum;
13689
13690 fp = var2fpos(&argvars[0], FALSE, &fnum);
13691 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
13692 && fnum == curbuf->b_fnum)
13693 {
13694 getvvcol(curwin, fp, NULL, NULL, &vcol);
13695 ++vcol;
13696 }
13697
13698 rettv->vval.v_number = vcol;
13699}
13700
13701/*
13702 * "visualmode()" function
13703 */
13704 static void
13705f_visualmode(typval_T *argvars, typval_T *rettv)
13706{
13707 char_u str[2];
13708
13709 rettv->v_type = VAR_STRING;
13710 str[0] = curbuf->b_visual_mode_eval;
13711 str[1] = NUL;
13712 rettv->vval.v_string = vim_strsave(str);
13713
13714 /* A non-zero number or non-empty string argument: reset mode. */
13715 if (non_zero_arg(&argvars[0]))
13716 curbuf->b_visual_mode_eval = NUL;
13717}
13718
13719/*
13720 * "wildmenumode()" function
13721 */
13722 static void
13723f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13724{
13725#ifdef FEAT_WILDMENU
13726 if (wild_menu_showing)
13727 rettv->vval.v_number = 1;
13728#endif
13729}
13730
13731/*
13732 * "winbufnr(nr)" function
13733 */
13734 static void
13735f_winbufnr(typval_T *argvars, typval_T *rettv)
13736{
13737 win_T *wp;
13738
13739 wp = find_win_by_nr(&argvars[0], NULL);
13740 if (wp == NULL)
13741 rettv->vval.v_number = -1;
13742 else
13743 rettv->vval.v_number = wp->w_buffer->b_fnum;
13744}
13745
13746/*
13747 * "wincol()" function
13748 */
13749 static void
13750f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
13751{
13752 validate_cursor();
13753 rettv->vval.v_number = curwin->w_wcol + 1;
13754}
13755
13756/*
13757 * "winheight(nr)" function
13758 */
13759 static void
13760f_winheight(typval_T *argvars, typval_T *rettv)
13761{
13762 win_T *wp;
13763
13764 wp = find_win_by_nr(&argvars[0], NULL);
13765 if (wp == NULL)
13766 rettv->vval.v_number = -1;
13767 else
13768 rettv->vval.v_number = wp->w_height;
13769}
13770
13771/*
13772 * "winline()" function
13773 */
13774 static void
13775f_winline(typval_T *argvars UNUSED, typval_T *rettv)
13776{
13777 validate_cursor();
13778 rettv->vval.v_number = curwin->w_wrow + 1;
13779}
13780
13781/*
13782 * "winnr()" function
13783 */
13784 static void
13785f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
13786{
13787 int nr = 1;
13788
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013789 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013790 rettv->vval.v_number = nr;
13791}
13792
13793/*
13794 * "winrestcmd()" function
13795 */
13796 static void
13797f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
13798{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013799 win_T *wp;
13800 int winnr = 1;
13801 garray_T ga;
13802 char_u buf[50];
13803
13804 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020013805 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013806 {
13807 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
13808 ga_concat(&ga, buf);
13809 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
13810 ga_concat(&ga, buf);
13811 ++winnr;
13812 }
13813 ga_append(&ga, NUL);
13814
13815 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013816 rettv->v_type = VAR_STRING;
13817}
13818
13819/*
13820 * "winrestview()" function
13821 */
13822 static void
13823f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
13824{
13825 dict_T *dict;
13826
13827 if (argvars[0].v_type != VAR_DICT
13828 || (dict = argvars[0].vval.v_dict) == NULL)
13829 EMSG(_(e_invarg));
13830 else
13831 {
13832 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
13833 curwin->w_cursor.lnum = (linenr_T)get_dict_number(dict, (char_u *)"lnum");
13834 if (dict_find(dict, (char_u *)"col", -1) != NULL)
13835 curwin->w_cursor.col = (colnr_T)get_dict_number(dict, (char_u *)"col");
13836#ifdef FEAT_VIRTUALEDIT
13837 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
13838 curwin->w_cursor.coladd = (colnr_T)get_dict_number(dict, (char_u *)"coladd");
13839#endif
13840 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
13841 {
13842 curwin->w_curswant = (colnr_T)get_dict_number(dict, (char_u *)"curswant");
13843 curwin->w_set_curswant = FALSE;
13844 }
13845
13846 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
13847 set_topline(curwin, (linenr_T)get_dict_number(dict, (char_u *)"topline"));
13848#ifdef FEAT_DIFF
13849 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
13850 curwin->w_topfill = (int)get_dict_number(dict, (char_u *)"topfill");
13851#endif
13852 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
13853 curwin->w_leftcol = (colnr_T)get_dict_number(dict, (char_u *)"leftcol");
13854 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
13855 curwin->w_skipcol = (colnr_T)get_dict_number(dict, (char_u *)"skipcol");
13856
13857 check_cursor();
13858 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020013859 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013860 changed_window_setting();
13861
13862 if (curwin->w_topline <= 0)
13863 curwin->w_topline = 1;
13864 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
13865 curwin->w_topline = curbuf->b_ml.ml_line_count;
13866#ifdef FEAT_DIFF
13867 check_topfill(curwin, TRUE);
13868#endif
13869 }
13870}
13871
13872/*
13873 * "winsaveview()" function
13874 */
13875 static void
13876f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
13877{
13878 dict_T *dict;
13879
13880 if (rettv_dict_alloc(rettv) == FAIL)
13881 return;
13882 dict = rettv->vval.v_dict;
13883
Bram Moolenaare0be1672018-07-08 16:50:37 +020013884 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
13885 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013886#ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +020013887 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013888#endif
13889 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020013890 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013891
Bram Moolenaare0be1672018-07-08 16:50:37 +020013892 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013893#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020013894 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013895#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020013896 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
13897 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013898}
13899
13900/*
13901 * "winwidth(nr)" function
13902 */
13903 static void
13904f_winwidth(typval_T *argvars, typval_T *rettv)
13905{
13906 win_T *wp;
13907
13908 wp = find_win_by_nr(&argvars[0], NULL);
13909 if (wp == NULL)
13910 rettv->vval.v_number = -1;
13911 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013912 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013913}
13914
13915/*
13916 * "wordcount()" function
13917 */
13918 static void
13919f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
13920{
13921 if (rettv_dict_alloc(rettv) == FAIL)
13922 return;
13923 cursor_pos_info(rettv->vval.v_dict);
13924}
13925
13926/*
13927 * "writefile()" function
13928 */
13929 static void
13930f_writefile(typval_T *argvars, typval_T *rettv)
13931{
13932 int binary = FALSE;
13933 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013934#ifdef HAVE_FSYNC
13935 int do_fsync = p_fs;
13936#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013937 char_u *fname;
13938 FILE *fd;
13939 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013940 listitem_T *li;
13941 list_T *list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013942
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013943 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013944 if (check_restricted() || check_secure())
13945 return;
13946
13947 if (argvars[0].v_type != VAR_LIST)
13948 {
13949 EMSG2(_(e_listarg), "writefile()");
13950 return;
13951 }
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013952 list = argvars[0].vval.v_list;
13953 if (list == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013954 return;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013955 for (li = list->lv_first; li != NULL; li = li->li_next)
13956 if (get_tv_string_chk(&li->li_tv) == NULL)
13957 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013958
13959 if (argvars[2].v_type != VAR_UNKNOWN)
13960 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013961 char_u *arg2 = get_tv_string_chk(&argvars[2]);
13962
13963 if (arg2 == NULL)
13964 return;
13965 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013966 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013967 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013968 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013969#ifdef HAVE_FSYNC
13970 if (vim_strchr(arg2, 's') != NULL)
13971 do_fsync = TRUE;
13972 else if (vim_strchr(arg2, 'S') != NULL)
13973 do_fsync = FALSE;
13974#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013975 }
13976
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013977 fname = get_tv_string_chk(&argvars[1]);
13978 if (fname == NULL)
13979 return;
13980
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013981 /* Always open the file in binary mode, library functions have a mind of
13982 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013983 if (*fname == NUL || (fd = mch_fopen((char *)fname,
13984 append ? APPENDBIN : WRITEBIN)) == NULL)
13985 {
13986 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
13987 ret = -1;
13988 }
13989 else
13990 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013991 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013992 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013993#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010013994 else if (do_fsync)
13995 /* Ignore the error, the user wouldn't know what to do about it.
13996 * May happen for a device. */
13997 ignored = fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013998#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013999 fclose(fd);
14000 }
14001
14002 rettv->vval.v_number = ret;
14003}
14004
14005/*
14006 * "xor(expr, expr)" function
14007 */
14008 static void
14009f_xor(typval_T *argvars, typval_T *rettv)
14010{
14011 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14012 ^ get_tv_number_chk(&argvars[1], NULL);
14013}
14014
14015
14016#endif /* FEAT_EVAL */