blob: 69a674d2c4f51fb325b57c09309069b883534b73 [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);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200401static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200402static void f_synID(typval_T *argvars, typval_T *rettv);
403static void f_synIDattr(typval_T *argvars, typval_T *rettv);
404static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
405static void f_synstack(typval_T *argvars, typval_T *rettv);
406static void f_synconcealed(typval_T *argvars, typval_T *rettv);
407static void f_system(typval_T *argvars, typval_T *rettv);
408static void f_systemlist(typval_T *argvars, typval_T *rettv);
409static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
410static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
411static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
412static void f_taglist(typval_T *argvars, typval_T *rettv);
413static void f_tagfiles(typval_T *argvars, typval_T *rettv);
414static void f_tempname(typval_T *argvars, typval_T *rettv);
415static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
416static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200417static void f_test_feedinput(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200418static void f_test_option_not_set(typval_T *argvars, typval_T *rettv);
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100419static void f_test_override(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200420static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100421static void f_test_ignore_error(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200422#ifdef FEAT_JOB_CHANNEL
423static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
424#endif
425static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
426#ifdef FEAT_JOB_CHANNEL
427static void f_test_null_job(typval_T *argvars, typval_T *rettv);
428#endif
429static void f_test_null_list(typval_T *argvars, typval_T *rettv);
430static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
431static void f_test_null_string(typval_T *argvars, typval_T *rettv);
432static void f_test_settime(typval_T *argvars, typval_T *rettv);
433#ifdef FEAT_FLOAT
434static void f_tan(typval_T *argvars, typval_T *rettv);
435static void f_tanh(typval_T *argvars, typval_T *rettv);
436#endif
437#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200438static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200439static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200440static void f_timer_start(typval_T *argvars, typval_T *rettv);
441static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200442static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200443#endif
444static void f_tolower(typval_T *argvars, typval_T *rettv);
445static void f_toupper(typval_T *argvars, typval_T *rettv);
446static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100447static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200448#ifdef FEAT_FLOAT
449static void f_trunc(typval_T *argvars, typval_T *rettv);
450#endif
451static void f_type(typval_T *argvars, typval_T *rettv);
452static void f_undofile(typval_T *argvars, typval_T *rettv);
453static void f_undotree(typval_T *argvars, typval_T *rettv);
454static void f_uniq(typval_T *argvars, typval_T *rettv);
455static void f_values(typval_T *argvars, typval_T *rettv);
456static void f_virtcol(typval_T *argvars, typval_T *rettv);
457static void f_visualmode(typval_T *argvars, typval_T *rettv);
458static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
459static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
460static void f_win_getid(typval_T *argvars, typval_T *rettv);
461static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
462static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
463static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100464static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200465static void f_winbufnr(typval_T *argvars, typval_T *rettv);
466static void f_wincol(typval_T *argvars, typval_T *rettv);
467static void f_winheight(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200468static void f_winlayout(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200469static void f_winline(typval_T *argvars, typval_T *rettv);
470static void f_winnr(typval_T *argvars, typval_T *rettv);
471static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
472static void f_winrestview(typval_T *argvars, typval_T *rettv);
473static void f_winsaveview(typval_T *argvars, typval_T *rettv);
474static void f_winwidth(typval_T *argvars, typval_T *rettv);
475static void f_writefile(typval_T *argvars, typval_T *rettv);
476static void f_wordcount(typval_T *argvars, typval_T *rettv);
477static void f_xor(typval_T *argvars, typval_T *rettv);
478
479/*
480 * Array with names and number of arguments of all internal functions
481 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
482 */
483static struct fst
484{
485 char *f_name; /* function name */
486 char f_min_argc; /* minimal number of arguments */
487 char f_max_argc; /* maximal number of arguments */
488 void (*f_func)(typval_T *args, typval_T *rvar);
489 /* implementation of function */
490} functions[] =
491{
492#ifdef FEAT_FLOAT
493 {"abs", 1, 1, f_abs},
494 {"acos", 1, 1, f_acos}, /* WJMc */
495#endif
496 {"add", 2, 2, f_add},
497 {"and", 2, 2, f_and},
498 {"append", 2, 2, f_append},
Bram Moolenaarca851592018-06-06 21:04:07 +0200499 {"appendbufline", 3, 3, f_appendbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200500 {"argc", 0, 0, f_argc},
501 {"argidx", 0, 0, f_argidx},
502 {"arglistid", 0, 2, f_arglistid},
503 {"argv", 0, 1, f_argv},
504#ifdef FEAT_FLOAT
505 {"asin", 1, 1, f_asin}, /* WJMc */
506#endif
Bram Moolenaarb48e96f2018-02-13 12:26:14 +0100507 {"assert_beeps", 1, 2, f_assert_beeps},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200508 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaard96ff162018-02-18 22:13:29 +0100509 {"assert_equalfile", 2, 2, f_assert_equalfile},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200510 {"assert_exception", 1, 2, f_assert_exception},
511 {"assert_fails", 1, 2, f_assert_fails},
512 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100513 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200514 {"assert_match", 2, 3, f_assert_match},
515 {"assert_notequal", 2, 3, f_assert_notequal},
516 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100517 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200518 {"assert_true", 1, 2, f_assert_true},
519#ifdef FEAT_FLOAT
520 {"atan", 1, 1, f_atan},
521 {"atan2", 2, 2, f_atan2},
522#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100523#ifdef FEAT_BEVAL
524 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100525# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100526 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100527# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100528#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200529 {"browse", 4, 4, f_browse},
530 {"browsedir", 2, 2, f_browsedir},
531 {"bufexists", 1, 1, f_bufexists},
532 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
533 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
534 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
535 {"buflisted", 1, 1, f_buflisted},
536 {"bufloaded", 1, 1, f_bufloaded},
537 {"bufname", 1, 1, f_bufname},
538 {"bufnr", 1, 2, f_bufnr},
539 {"bufwinid", 1, 1, f_bufwinid},
540 {"bufwinnr", 1, 1, f_bufwinnr},
541 {"byte2line", 1, 1, f_byte2line},
542 {"byteidx", 2, 2, f_byteidx},
543 {"byteidxcomp", 2, 2, f_byteidxcomp},
544 {"call", 2, 3, f_call},
545#ifdef FEAT_FLOAT
546 {"ceil", 1, 1, f_ceil},
547#endif
548#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100549 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200550 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200551 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200552 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
553 {"ch_evalraw", 2, 3, f_ch_evalraw},
554 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
555 {"ch_getjob", 1, 1, f_ch_getjob},
556 {"ch_info", 1, 1, f_ch_info},
557 {"ch_log", 1, 2, f_ch_log},
558 {"ch_logfile", 1, 2, f_ch_logfile},
559 {"ch_open", 1, 2, f_ch_open},
560 {"ch_read", 1, 2, f_ch_read},
561 {"ch_readraw", 1, 2, f_ch_readraw},
562 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
563 {"ch_sendraw", 2, 3, f_ch_sendraw},
564 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200565 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200566#endif
567 {"changenr", 0, 0, f_changenr},
568 {"char2nr", 1, 2, f_char2nr},
569 {"cindent", 1, 1, f_cindent},
570 {"clearmatches", 0, 0, f_clearmatches},
571 {"col", 1, 1, f_col},
572#if defined(FEAT_INS_EXPAND)
573 {"complete", 2, 2, f_complete},
574 {"complete_add", 1, 1, f_complete_add},
575 {"complete_check", 0, 0, f_complete_check},
576#endif
577 {"confirm", 1, 4, f_confirm},
578 {"copy", 1, 1, f_copy},
579#ifdef FEAT_FLOAT
580 {"cos", 1, 1, f_cos},
581 {"cosh", 1, 1, f_cosh},
582#endif
583 {"count", 2, 4, f_count},
584 {"cscope_connection",0,3, f_cscope_connection},
585 {"cursor", 1, 3, f_cursor},
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200586#ifdef WIN3264
587 {"debugbreak", 1, 1, f_debugbreak},
588#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200589 {"deepcopy", 1, 2, f_deepcopy},
590 {"delete", 1, 2, f_delete},
Bram Moolenaard79a2622018-06-07 18:17:46 +0200591 {"deletebufline", 2, 3, f_deletebufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200592 {"did_filetype", 0, 0, f_did_filetype},
593 {"diff_filler", 1, 1, f_diff_filler},
594 {"diff_hlID", 2, 2, f_diff_hlID},
595 {"empty", 1, 1, f_empty},
596 {"escape", 2, 2, f_escape},
597 {"eval", 1, 1, f_eval},
598 {"eventhandler", 0, 0, f_eventhandler},
599 {"executable", 1, 1, f_executable},
600 {"execute", 1, 2, f_execute},
601 {"exepath", 1, 1, f_exepath},
602 {"exists", 1, 1, f_exists},
603#ifdef FEAT_FLOAT
604 {"exp", 1, 1, f_exp},
605#endif
606 {"expand", 1, 3, f_expand},
607 {"extend", 2, 3, f_extend},
608 {"feedkeys", 1, 2, f_feedkeys},
609 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
610 {"filereadable", 1, 1, f_filereadable},
611 {"filewritable", 1, 1, f_filewritable},
612 {"filter", 2, 2, f_filter},
613 {"finddir", 1, 3, f_finddir},
614 {"findfile", 1, 3, f_findfile},
615#ifdef FEAT_FLOAT
616 {"float2nr", 1, 1, f_float2nr},
617 {"floor", 1, 1, f_floor},
618 {"fmod", 2, 2, f_fmod},
619#endif
620 {"fnameescape", 1, 1, f_fnameescape},
621 {"fnamemodify", 2, 2, f_fnamemodify},
622 {"foldclosed", 1, 1, f_foldclosed},
623 {"foldclosedend", 1, 1, f_foldclosedend},
624 {"foldlevel", 1, 1, f_foldlevel},
625 {"foldtext", 0, 0, f_foldtext},
626 {"foldtextresult", 1, 1, f_foldtextresult},
627 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200628 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200629 {"function", 1, 3, f_function},
630 {"garbagecollect", 0, 1, f_garbagecollect},
631 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200632 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200633 {"getbufline", 2, 3, f_getbufline},
634 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100635 {"getchangelist", 1, 1, f_getchangelist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200636 {"getchar", 0, 1, f_getchar},
637 {"getcharmod", 0, 0, f_getcharmod},
638 {"getcharsearch", 0, 0, f_getcharsearch},
639 {"getcmdline", 0, 0, f_getcmdline},
640 {"getcmdpos", 0, 0, f_getcmdpos},
641 {"getcmdtype", 0, 0, f_getcmdtype},
642 {"getcmdwintype", 0, 0, f_getcmdwintype},
643#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200644 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200645#endif
646 {"getcurpos", 0, 0, f_getcurpos},
647 {"getcwd", 0, 2, f_getcwd},
648 {"getfontname", 0, 1, f_getfontname},
649 {"getfperm", 1, 1, f_getfperm},
650 {"getfsize", 1, 1, f_getfsize},
651 {"getftime", 1, 1, f_getftime},
652 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100653 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200654 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200655 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200656 {"getmatches", 0, 0, f_getmatches},
657 {"getpid", 0, 0, f_getpid},
658 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200659 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200660 {"getreg", 0, 3, f_getreg},
661 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200662 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200663 {"gettabvar", 2, 3, f_gettabvar},
664 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200665 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100666 {"getwinpos", 0, 1, f_getwinpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200667 {"getwinposx", 0, 0, f_getwinposx},
668 {"getwinposy", 0, 0, f_getwinposy},
669 {"getwinvar", 2, 3, f_getwinvar},
670 {"glob", 1, 4, f_glob},
671 {"glob2regpat", 1, 1, f_glob2regpat},
672 {"globpath", 2, 5, f_globpath},
673 {"has", 1, 1, f_has},
674 {"has_key", 2, 2, f_has_key},
675 {"haslocaldir", 0, 2, f_haslocaldir},
676 {"hasmapto", 1, 3, f_hasmapto},
677 {"highlightID", 1, 1, f_hlID}, /* obsolete */
678 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
679 {"histadd", 2, 2, f_histadd},
680 {"histdel", 1, 2, f_histdel},
681 {"histget", 1, 2, f_histget},
682 {"histnr", 1, 1, f_histnr},
683 {"hlID", 1, 1, f_hlID},
684 {"hlexists", 1, 1, f_hlexists},
685 {"hostname", 0, 0, f_hostname},
686 {"iconv", 3, 3, f_iconv},
687 {"indent", 1, 1, f_indent},
688 {"index", 2, 4, f_index},
689 {"input", 1, 3, f_input},
690 {"inputdialog", 1, 3, f_inputdialog},
691 {"inputlist", 1, 1, f_inputlist},
692 {"inputrestore", 0, 0, f_inputrestore},
693 {"inputsave", 0, 0, f_inputsave},
694 {"inputsecret", 1, 2, f_inputsecret},
695 {"insert", 2, 3, f_insert},
696 {"invert", 1, 1, f_invert},
697 {"isdirectory", 1, 1, f_isdirectory},
698 {"islocked", 1, 1, f_islocked},
699#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
700 {"isnan", 1, 1, f_isnan},
701#endif
702 {"items", 1, 1, f_items},
703#ifdef FEAT_JOB_CHANNEL
704 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200705 {"job_info", 0, 1, f_job_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200706 {"job_setoptions", 2, 2, f_job_setoptions},
707 {"job_start", 1, 2, f_job_start},
708 {"job_status", 1, 1, f_job_status},
709 {"job_stop", 1, 2, f_job_stop},
710#endif
711 {"join", 1, 2, f_join},
712 {"js_decode", 1, 1, f_js_decode},
713 {"js_encode", 1, 1, f_js_encode},
714 {"json_decode", 1, 1, f_json_decode},
715 {"json_encode", 1, 1, f_json_encode},
716 {"keys", 1, 1, f_keys},
717 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
718 {"len", 1, 1, f_len},
719 {"libcall", 3, 3, f_libcall},
720 {"libcallnr", 3, 3, f_libcallnr},
721 {"line", 1, 1, f_line},
722 {"line2byte", 1, 1, f_line2byte},
723 {"lispindent", 1, 1, f_lispindent},
724 {"localtime", 0, 0, f_localtime},
725#ifdef FEAT_FLOAT
726 {"log", 1, 1, f_log},
727 {"log10", 1, 1, f_log10},
728#endif
729#ifdef FEAT_LUA
730 {"luaeval", 1, 2, f_luaeval},
731#endif
732 {"map", 2, 2, f_map},
733 {"maparg", 1, 4, f_maparg},
734 {"mapcheck", 1, 3, f_mapcheck},
735 {"match", 2, 4, f_match},
736 {"matchadd", 2, 5, f_matchadd},
737 {"matchaddpos", 2, 5, f_matchaddpos},
738 {"matcharg", 1, 1, f_matcharg},
739 {"matchdelete", 1, 1, f_matchdelete},
740 {"matchend", 2, 4, f_matchend},
741 {"matchlist", 2, 4, f_matchlist},
742 {"matchstr", 2, 4, f_matchstr},
743 {"matchstrpos", 2, 4, f_matchstrpos},
744 {"max", 1, 1, f_max},
745 {"min", 1, 1, f_min},
746#ifdef vim_mkdir
747 {"mkdir", 1, 3, f_mkdir},
748#endif
749 {"mode", 0, 1, f_mode},
750#ifdef FEAT_MZSCHEME
751 {"mzeval", 1, 1, f_mzeval},
752#endif
753 {"nextnonblank", 1, 1, f_nextnonblank},
754 {"nr2char", 1, 2, f_nr2char},
755 {"or", 2, 2, f_or},
756 {"pathshorten", 1, 1, f_pathshorten},
757#ifdef FEAT_PERL
758 {"perleval", 1, 1, f_perleval},
759#endif
760#ifdef FEAT_FLOAT
761 {"pow", 2, 2, f_pow},
762#endif
763 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100764 {"printf", 1, 19, f_printf},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200765#ifdef FEAT_JOB_CHANNEL
766 {"prompt_setcallback", 2, 2, f_prompt_setcallback},
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200767 {"prompt_setinterrupt", 2, 2, f_prompt_setinterrupt},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200768 {"prompt_setprompt", 2, 2, f_prompt_setprompt},
769#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200770 {"pumvisible", 0, 0, f_pumvisible},
771#ifdef FEAT_PYTHON3
772 {"py3eval", 1, 1, f_py3eval},
773#endif
774#ifdef FEAT_PYTHON
775 {"pyeval", 1, 1, f_pyeval},
776#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100777#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
778 {"pyxeval", 1, 1, f_pyxeval},
779#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200780 {"range", 1, 3, f_range},
781 {"readfile", 1, 3, f_readfile},
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200782 {"reg_executing", 0, 0, f_reg_executing},
783 {"reg_recording", 0, 0, f_reg_recording},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200784 {"reltime", 0, 2, f_reltime},
785#ifdef FEAT_FLOAT
786 {"reltimefloat", 1, 1, f_reltimefloat},
787#endif
788 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100789 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200790 {"remote_foreground", 1, 1, f_remote_foreground},
791 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100792 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200793 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100794 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200795 {"remove", 2, 3, f_remove},
796 {"rename", 2, 2, f_rename},
797 {"repeat", 2, 2, f_repeat},
798 {"resolve", 1, 1, f_resolve},
799 {"reverse", 1, 1, f_reverse},
800#ifdef FEAT_FLOAT
801 {"round", 1, 1, f_round},
802#endif
803 {"screenattr", 2, 2, f_screenattr},
804 {"screenchar", 2, 2, f_screenchar},
805 {"screencol", 0, 0, f_screencol},
806 {"screenrow", 0, 0, f_screenrow},
807 {"search", 1, 4, f_search},
808 {"searchdecl", 1, 3, f_searchdecl},
809 {"searchpair", 3, 7, f_searchpair},
810 {"searchpairpos", 3, 7, f_searchpairpos},
811 {"searchpos", 1, 4, f_searchpos},
812 {"server2client", 2, 2, f_server2client},
813 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200814 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200815 {"setbufvar", 3, 3, f_setbufvar},
816 {"setcharsearch", 1, 1, f_setcharsearch},
817 {"setcmdpos", 1, 1, f_setcmdpos},
818 {"setfperm", 2, 2, f_setfperm},
819 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200820 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200821 {"setmatches", 1, 1, f_setmatches},
822 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200823 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200824 {"setreg", 2, 3, f_setreg},
825 {"settabvar", 3, 3, f_settabvar},
826 {"settabwinvar", 4, 4, f_settabwinvar},
827 {"setwinvar", 3, 3, f_setwinvar},
828#ifdef FEAT_CRYPT
829 {"sha256", 1, 1, f_sha256},
830#endif
831 {"shellescape", 1, 2, f_shellescape},
832 {"shiftwidth", 0, 0, f_shiftwidth},
833 {"simplify", 1, 1, f_simplify},
834#ifdef FEAT_FLOAT
835 {"sin", 1, 1, f_sin},
836 {"sinh", 1, 1, f_sinh},
837#endif
838 {"sort", 1, 3, f_sort},
839 {"soundfold", 1, 1, f_soundfold},
840 {"spellbadword", 0, 1, f_spellbadword},
841 {"spellsuggest", 1, 3, f_spellsuggest},
842 {"split", 1, 3, f_split},
843#ifdef FEAT_FLOAT
844 {"sqrt", 1, 1, f_sqrt},
845 {"str2float", 1, 1, f_str2float},
846#endif
847 {"str2nr", 1, 2, f_str2nr},
848 {"strcharpart", 2, 3, f_strcharpart},
849 {"strchars", 1, 2, f_strchars},
850 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
851#ifdef HAVE_STRFTIME
852 {"strftime", 1, 2, f_strftime},
853#endif
854 {"strgetchar", 2, 2, f_strgetchar},
855 {"stridx", 2, 3, f_stridx},
856 {"string", 1, 1, f_string},
857 {"strlen", 1, 1, f_strlen},
858 {"strpart", 2, 3, f_strpart},
859 {"strridx", 2, 3, f_strridx},
860 {"strtrans", 1, 1, f_strtrans},
861 {"strwidth", 1, 1, f_strwidth},
862 {"submatch", 1, 2, f_submatch},
863 {"substitute", 4, 4, f_substitute},
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200864 {"swapinfo", 1, 1, f_swapinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200865 {"synID", 3, 3, f_synID},
866 {"synIDattr", 2, 3, f_synIDattr},
867 {"synIDtrans", 1, 1, f_synIDtrans},
868 {"synconcealed", 2, 2, f_synconcealed},
869 {"synstack", 2, 2, f_synstack},
870 {"system", 1, 2, f_system},
871 {"systemlist", 1, 2, f_systemlist},
872 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
873 {"tabpagenr", 0, 1, f_tabpagenr},
874 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
875 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100876 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200877#ifdef FEAT_FLOAT
878 {"tan", 1, 1, f_tan},
879 {"tanh", 1, 1, f_tanh},
880#endif
881 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200882#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100883 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
884 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100885 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200886 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200887# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
888 {"term_getansicolors", 1, 1, f_term_getansicolors},
889# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200890 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200891 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200892 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200893 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200894 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200895 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200896 {"term_getstatus", 1, 1, f_term_getstatus},
897 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200898 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200899 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200900 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200901 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200902# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
903 {"term_setansicolors", 2, 2, f_term_setansicolors},
904# endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100905 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100906 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaara42d3632018-04-14 17:05:38 +0200907 {"term_setsize", 3, 3, f_term_setsize},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200908 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200909 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200910#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200911 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
912 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200913 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200914 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100915 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200916#ifdef FEAT_JOB_CHANNEL
917 {"test_null_channel", 0, 0, f_test_null_channel},
918#endif
919 {"test_null_dict", 0, 0, f_test_null_dict},
920#ifdef FEAT_JOB_CHANNEL
921 {"test_null_job", 0, 0, f_test_null_job},
922#endif
923 {"test_null_list", 0, 0, f_test_null_list},
924 {"test_null_partial", 0, 0, f_test_null_partial},
925 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200926 {"test_option_not_set", 1, 1, f_test_option_not_set},
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100927 {"test_override", 2, 2, f_test_override},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200928 {"test_settime", 1, 1, f_test_settime},
929#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200930 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200931 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200932 {"timer_start", 2, 3, f_timer_start},
933 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200934 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200935#endif
936 {"tolower", 1, 1, f_tolower},
937 {"toupper", 1, 1, f_toupper},
938 {"tr", 3, 3, f_tr},
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100939 {"trim", 1, 2, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200940#ifdef FEAT_FLOAT
941 {"trunc", 1, 1, f_trunc},
942#endif
943 {"type", 1, 1, f_type},
944 {"undofile", 1, 1, f_undofile},
945 {"undotree", 0, 0, f_undotree},
946 {"uniq", 1, 3, f_uniq},
947 {"values", 1, 1, f_values},
948 {"virtcol", 1, 1, f_virtcol},
949 {"visualmode", 0, 1, f_visualmode},
950 {"wildmenumode", 0, 0, f_wildmenumode},
951 {"win_findbuf", 1, 1, f_win_findbuf},
952 {"win_getid", 0, 2, f_win_getid},
953 {"win_gotoid", 1, 1, f_win_gotoid},
954 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
955 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100956 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200957 {"winbufnr", 1, 1, f_winbufnr},
958 {"wincol", 0, 0, f_wincol},
959 {"winheight", 1, 1, f_winheight},
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200960 {"winlayout", 0, 1, f_winlayout},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200961 {"winline", 0, 0, f_winline},
962 {"winnr", 0, 1, f_winnr},
963 {"winrestcmd", 0, 0, f_winrestcmd},
964 {"winrestview", 1, 1, f_winrestview},
965 {"winsaveview", 0, 0, f_winsaveview},
966 {"winwidth", 1, 1, f_winwidth},
967 {"wordcount", 0, 0, f_wordcount},
968 {"writefile", 2, 3, f_writefile},
969 {"xor", 2, 2, f_xor},
970};
971
972#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
973
974/*
975 * Function given to ExpandGeneric() to obtain the list of internal
976 * or user defined function names.
977 */
978 char_u *
979get_function_name(expand_T *xp, int idx)
980{
981 static int intidx = -1;
982 char_u *name;
983
984 if (idx == 0)
985 intidx = -1;
986 if (intidx < 0)
987 {
988 name = get_user_func_name(xp, idx);
989 if (name != NULL)
990 return name;
991 }
992 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
993 {
994 STRCPY(IObuff, functions[intidx].f_name);
995 STRCAT(IObuff, "(");
996 if (functions[intidx].f_max_argc == 0)
997 STRCAT(IObuff, ")");
998 return IObuff;
999 }
1000
1001 return NULL;
1002}
1003
1004/*
1005 * Function given to ExpandGeneric() to obtain the list of internal or
1006 * user defined variable or function names.
1007 */
1008 char_u *
1009get_expr_name(expand_T *xp, int idx)
1010{
1011 static int intidx = -1;
1012 char_u *name;
1013
1014 if (idx == 0)
1015 intidx = -1;
1016 if (intidx < 0)
1017 {
1018 name = get_function_name(xp, idx);
1019 if (name != NULL)
1020 return name;
1021 }
1022 return get_user_var_name(xp, ++intidx);
1023}
1024
1025#endif /* FEAT_CMDL_COMPL */
1026
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001027/*
1028 * Find internal function in table above.
1029 * Return index, or -1 if not found
1030 */
1031 int
1032find_internal_func(
1033 char_u *name) /* name of the function */
1034{
1035 int first = 0;
1036 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
1037 int cmp;
1038 int x;
1039
1040 /*
1041 * Find the function name in the table. Binary search.
1042 */
1043 while (first <= last)
1044 {
1045 x = first + ((unsigned)(last - first) >> 1);
1046 cmp = STRCMP(name, functions[x].f_name);
1047 if (cmp < 0)
1048 last = x - 1;
1049 else if (cmp > 0)
1050 first = x + 1;
1051 else
1052 return x;
1053 }
1054 return -1;
1055}
1056
1057 int
1058call_internal_func(
1059 char_u *name,
1060 int argcount,
1061 typval_T *argvars,
1062 typval_T *rettv)
1063{
1064 int i;
1065
1066 i = find_internal_func(name);
1067 if (i < 0)
1068 return ERROR_UNKNOWN;
1069 if (argcount < functions[i].f_min_argc)
1070 return ERROR_TOOFEW;
1071 if (argcount > functions[i].f_max_argc)
1072 return ERROR_TOOMANY;
1073 argvars[argcount].v_type = VAR_UNKNOWN;
1074 functions[i].f_func(argvars, rettv);
1075 return ERROR_NONE;
1076}
1077
1078/*
1079 * Return TRUE for a non-zero Number and a non-empty String.
1080 */
1081 static int
1082non_zero_arg(typval_T *argvars)
1083{
1084 return ((argvars[0].v_type == VAR_NUMBER
1085 && argvars[0].vval.v_number != 0)
1086 || (argvars[0].v_type == VAR_SPECIAL
1087 && argvars[0].vval.v_number == VVAL_TRUE)
1088 || (argvars[0].v_type == VAR_STRING
1089 && argvars[0].vval.v_string != NULL
1090 && *argvars[0].vval.v_string != NUL));
1091}
1092
1093/*
1094 * Get the lnum from the first argument.
1095 * Also accepts ".", "$", etc., but that only works for the current buffer.
1096 * Returns -1 on error.
1097 */
1098 static linenr_T
1099get_tv_lnum(typval_T *argvars)
1100{
1101 typval_T rettv;
1102 linenr_T lnum;
1103
1104 lnum = (linenr_T)get_tv_number_chk(&argvars[0], NULL);
1105 if (lnum == 0) /* no valid number, try using line() */
1106 {
1107 rettv.v_type = VAR_NUMBER;
1108 f_line(argvars, &rettv);
1109 lnum = (linenr_T)rettv.vval.v_number;
1110 clear_tv(&rettv);
1111 }
1112 return lnum;
1113}
1114
1115#ifdef FEAT_FLOAT
1116static int get_float_arg(typval_T *argvars, float_T *f);
1117
1118/*
1119 * Get the float value of "argvars[0]" into "f".
1120 * Returns FAIL when the argument is not a Number or Float.
1121 */
1122 static int
1123get_float_arg(typval_T *argvars, float_T *f)
1124{
1125 if (argvars[0].v_type == VAR_FLOAT)
1126 {
1127 *f = argvars[0].vval.v_float;
1128 return OK;
1129 }
1130 if (argvars[0].v_type == VAR_NUMBER)
1131 {
1132 *f = (float_T)argvars[0].vval.v_number;
1133 return OK;
1134 }
1135 EMSG(_("E808: Number or Float required"));
1136 return FAIL;
1137}
1138
1139/*
1140 * "abs(expr)" function
1141 */
1142 static void
1143f_abs(typval_T *argvars, typval_T *rettv)
1144{
1145 if (argvars[0].v_type == VAR_FLOAT)
1146 {
1147 rettv->v_type = VAR_FLOAT;
1148 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1149 }
1150 else
1151 {
1152 varnumber_T n;
1153 int error = FALSE;
1154
1155 n = get_tv_number_chk(&argvars[0], &error);
1156 if (error)
1157 rettv->vval.v_number = -1;
1158 else if (n > 0)
1159 rettv->vval.v_number = n;
1160 else
1161 rettv->vval.v_number = -n;
1162 }
1163}
1164
1165/*
1166 * "acos()" function
1167 */
1168 static void
1169f_acos(typval_T *argvars, typval_T *rettv)
1170{
1171 float_T f = 0.0;
1172
1173 rettv->v_type = VAR_FLOAT;
1174 if (get_float_arg(argvars, &f) == OK)
1175 rettv->vval.v_float = acos(f);
1176 else
1177 rettv->vval.v_float = 0.0;
1178}
1179#endif
1180
1181/*
1182 * "add(list, item)" function
1183 */
1184 static void
1185f_add(typval_T *argvars, typval_T *rettv)
1186{
1187 list_T *l;
1188
1189 rettv->vval.v_number = 1; /* Default: Failed */
1190 if (argvars[0].v_type == VAR_LIST)
1191 {
1192 if ((l = argvars[0].vval.v_list) != NULL
1193 && !tv_check_lock(l->lv_lock,
1194 (char_u *)N_("add() argument"), TRUE)
1195 && list_append_tv(l, &argvars[1]) == OK)
1196 copy_tv(&argvars[0], rettv);
1197 }
1198 else
1199 EMSG(_(e_listreq));
1200}
1201
1202/*
1203 * "and(expr, expr)" function
1204 */
1205 static void
1206f_and(typval_T *argvars, typval_T *rettv)
1207{
1208 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
1209 & get_tv_number_chk(&argvars[1], NULL);
1210}
1211
1212/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001213 * Get the lnum from the first argument.
1214 * Also accepts "$", then "buf" is used.
1215 * Returns 0 on error.
1216 */
1217 static linenr_T
1218get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
1219{
1220 if (argvars[0].v_type == VAR_STRING
1221 && argvars[0].vval.v_string != NULL
1222 && argvars[0].vval.v_string[0] == '$'
1223 && buf != NULL)
1224 return buf->b_ml.ml_line_count;
1225 return (linenr_T)get_tv_number_chk(&argvars[0], NULL);
1226}
1227
1228/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02001229 * If there is a window for "curbuf", make it the current window.
1230 */
1231 static void
1232find_win_for_curbuf(void)
1233{
1234 wininfo_T *wip;
1235
1236 for (wip = curbuf->b_wininfo; wip != NULL; wip = wip->wi_next)
1237 {
1238 if (wip->wi_win != NULL)
1239 {
1240 curwin = wip->wi_win;
1241 break;
1242 }
1243 }
1244}
1245
1246/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001247 * Set line or list of lines in buffer "buf".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001248 */
1249 static void
Bram Moolenaarca851592018-06-06 21:04:07 +02001250set_buffer_lines(
1251 buf_T *buf,
1252 linenr_T lnum_arg,
1253 int append,
1254 typval_T *lines,
1255 typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001256{
Bram Moolenaarca851592018-06-06 21:04:07 +02001257 linenr_T lnum = lnum_arg + (append ? 1 : 0);
1258 char_u *line = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001259 list_T *l = NULL;
1260 listitem_T *li = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001261 long added = 0;
Bram Moolenaarca851592018-06-06 21:04:07 +02001262 linenr_T append_lnum;
1263 buf_T *curbuf_save = NULL;
1264 win_T *curwin_save = NULL;
1265 int is_curbuf = buf == curbuf;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001266
Bram Moolenaarca851592018-06-06 21:04:07 +02001267 /* When using the current buffer ml_mfp will be set if needed. Useful when
1268 * setline() is used on startup. For other buffers the buffer must be
1269 * loaded. */
1270 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001271 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001272 rettv->vval.v_number = 1; /* FAIL */
1273 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001274 }
1275
Bram Moolenaarca851592018-06-06 21:04:07 +02001276 if (!is_curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001277 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001278 curbuf_save = curbuf;
1279 curwin_save = curwin;
1280 curbuf = buf;
Bram Moolenaard79a2622018-06-07 18:17:46 +02001281 find_win_for_curbuf();
Bram Moolenaarca851592018-06-06 21:04:07 +02001282 }
1283
1284 if (append)
1285 // appendbufline() uses the line number below which we insert
1286 append_lnum = lnum - 1;
1287 else
1288 // setbufline() uses the line number above which we insert, we only
1289 // append if it's below the last line
1290 append_lnum = curbuf->b_ml.ml_line_count;
1291
1292 if (lines->v_type == VAR_LIST)
1293 {
1294 l = lines->vval.v_list;
1295 li = l->lv_first;
1296 }
1297 else
1298 line = get_tv_string_chk(lines);
1299
1300 /* default result is zero == OK */
1301 for (;;)
1302 {
1303 if (l != NULL)
1304 {
1305 /* list argument, get next string */
1306 if (li == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001307 break;
Bram Moolenaarca851592018-06-06 21:04:07 +02001308 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001309 li = li->li_next;
1310 }
1311
Bram Moolenaarca851592018-06-06 21:04:07 +02001312 rettv->vval.v_number = 1; /* FAIL */
1313 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
1314 break;
1315
1316 /* When coming here from Insert mode, sync undo, so that this can be
1317 * undone separately from what was previously inserted. */
1318 if (u_sync_once == 2)
1319 {
1320 u_sync_once = 1; /* notify that u_sync() was called */
1321 u_sync(TRUE);
1322 }
1323
1324 if (!append && lnum <= curbuf->b_ml.ml_line_count)
1325 {
1326 /* existing line, replace it */
1327 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
1328 {
1329 changed_bytes(lnum, 0);
1330 if (is_curbuf && lnum == curwin->w_cursor.lnum)
1331 check_cursor_col();
1332 rettv->vval.v_number = 0; /* OK */
1333 }
1334 }
1335 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
1336 {
1337 /* append the line */
1338 ++added;
1339 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
1340 rettv->vval.v_number = 0; /* OK */
1341 }
1342
1343 if (l == NULL) /* only one string argument */
1344 break;
1345 ++lnum;
1346 }
1347
1348 if (added > 0)
1349 {
1350 win_T *wp;
1351 tabpage_T *tp;
1352
1353 appended_lines_mark(append_lnum, added);
1354 FOR_ALL_TAB_WINDOWS(tp, wp)
1355 if (wp->w_buffer == buf && wp->w_cursor.lnum > append_lnum)
1356 wp->w_cursor.lnum += added;
1357 check_cursor_col();
1358
Bram Moolenaarf2732452018-06-03 14:47:35 +02001359#ifdef FEAT_JOB_CHANNEL
1360 if (bt_prompt(curbuf) && (State & INSERT))
1361 // show the line with the prompt
1362 update_topline();
1363#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001364 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001365
1366 if (!is_curbuf)
1367 {
1368 curbuf = curbuf_save;
1369 curwin = curwin_save;
1370 }
1371}
1372
1373/*
1374 * "append(lnum, string/list)" function
1375 */
1376 static void
1377f_append(typval_T *argvars, typval_T *rettv)
1378{
1379 linenr_T lnum = get_tv_lnum(&argvars[0]);
1380
1381 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1382}
1383
1384/*
1385 * "appendbufline(buf, lnum, string/list)" function
1386 */
1387 static void
1388f_appendbufline(typval_T *argvars, typval_T *rettv)
1389{
1390 linenr_T lnum;
1391 buf_T *buf;
1392
1393 buf = get_buf_tv(&argvars[0], FALSE);
1394 if (buf == NULL)
1395 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001396 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001397 {
1398 lnum = get_tv_lnum_buf(&argvars[1], buf);
1399 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1400 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001401}
1402
1403/*
1404 * "argc()" function
1405 */
1406 static void
1407f_argc(typval_T *argvars UNUSED, typval_T *rettv)
1408{
1409 rettv->vval.v_number = ARGCOUNT;
1410}
1411
1412/*
1413 * "argidx()" function
1414 */
1415 static void
1416f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1417{
1418 rettv->vval.v_number = curwin->w_arg_idx;
1419}
1420
1421/*
1422 * "arglistid()" function
1423 */
1424 static void
1425f_arglistid(typval_T *argvars, typval_T *rettv)
1426{
1427 win_T *wp;
1428
1429 rettv->vval.v_number = -1;
1430 wp = find_tabwin(&argvars[0], &argvars[1]);
1431 if (wp != NULL)
1432 rettv->vval.v_number = wp->w_alist->id;
1433}
1434
1435/*
1436 * "argv(nr)" function
1437 */
1438 static void
1439f_argv(typval_T *argvars, typval_T *rettv)
1440{
1441 int idx;
1442
1443 if (argvars[0].v_type != VAR_UNKNOWN)
1444 {
1445 idx = (int)get_tv_number_chk(&argvars[0], NULL);
1446 if (idx >= 0 && idx < ARGCOUNT)
1447 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
1448 else
1449 rettv->vval.v_string = NULL;
1450 rettv->v_type = VAR_STRING;
1451 }
1452 else if (rettv_list_alloc(rettv) == OK)
1453 for (idx = 0; idx < ARGCOUNT; ++idx)
1454 list_append_string(rettv->vval.v_list,
1455 alist_name(&ARGLIST[idx]), -1);
1456}
1457
1458/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001459 * "assert_beeps(cmd [, error])" function
1460 */
1461 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001462f_assert_beeps(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001463{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001464 rettv->vval.v_number = assert_beeps(argvars);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001465}
1466
1467/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001468 * "assert_equal(expected, actual[, msg])" function
1469 */
1470 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001471f_assert_equal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001472{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001473 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_EQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001474}
1475
1476/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01001477 * "assert_equalfile(fname-one, fname-two)" function
1478 */
1479 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001480f_assert_equalfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaard96ff162018-02-18 22:13:29 +01001481{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001482 rettv->vval.v_number = assert_equalfile(argvars);
Bram Moolenaard96ff162018-02-18 22:13:29 +01001483}
1484
1485/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001486 * "assert_notequal(expected, actual[, msg])" function
1487 */
1488 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001489f_assert_notequal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001490{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001491 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001492}
1493
1494/*
1495 * "assert_exception(string[, msg])" function
1496 */
1497 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001498f_assert_exception(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001499{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001500 rettv->vval.v_number = assert_exception(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001501}
1502
1503/*
1504 * "assert_fails(cmd [, error])" function
1505 */
1506 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001507f_assert_fails(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001508{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001509 rettv->vval.v_number = assert_fails(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001510}
1511
1512/*
1513 * "assert_false(actual[, msg])" function
1514 */
1515 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001516f_assert_false(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001517{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001518 rettv->vval.v_number = assert_bool(argvars, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001519}
1520
1521/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001522 * "assert_inrange(lower, upper[, msg])" function
1523 */
1524 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001525f_assert_inrange(typval_T *argvars, typval_T *rettv)
Bram Moolenaar61c04492016-07-23 15:35:35 +02001526{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001527 rettv->vval.v_number = assert_inrange(argvars);
Bram Moolenaar61c04492016-07-23 15:35:35 +02001528}
1529
1530/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001531 * "assert_match(pattern, actual[, msg])" function
1532 */
1533 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001534f_assert_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001535{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001536 rettv->vval.v_number = assert_match_common(argvars, ASSERT_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001537}
1538
1539/*
1540 * "assert_notmatch(pattern, actual[, msg])" function
1541 */
1542 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001543f_assert_notmatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001544{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001545 rettv->vval.v_number = assert_match_common(argvars, ASSERT_NOTMATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001546}
1547
1548/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001549 * "assert_report(msg)" function
1550 */
1551 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001552f_assert_report(typval_T *argvars, typval_T *rettv)
Bram Moolenaar42205552017-03-18 19:42:22 +01001553{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001554 rettv->vval.v_number = assert_report(argvars);
Bram Moolenaar42205552017-03-18 19:42:22 +01001555}
1556
1557/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001558 * "assert_true(actual[, msg])" function
1559 */
1560 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001561f_assert_true(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001562{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001563 rettv->vval.v_number = assert_bool(argvars, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001564}
1565
1566#ifdef FEAT_FLOAT
1567/*
1568 * "asin()" function
1569 */
1570 static void
1571f_asin(typval_T *argvars, typval_T *rettv)
1572{
1573 float_T f = 0.0;
1574
1575 rettv->v_type = VAR_FLOAT;
1576 if (get_float_arg(argvars, &f) == OK)
1577 rettv->vval.v_float = asin(f);
1578 else
1579 rettv->vval.v_float = 0.0;
1580}
1581
1582/*
1583 * "atan()" function
1584 */
1585 static void
1586f_atan(typval_T *argvars, typval_T *rettv)
1587{
1588 float_T f = 0.0;
1589
1590 rettv->v_type = VAR_FLOAT;
1591 if (get_float_arg(argvars, &f) == OK)
1592 rettv->vval.v_float = atan(f);
1593 else
1594 rettv->vval.v_float = 0.0;
1595}
1596
1597/*
1598 * "atan2()" function
1599 */
1600 static void
1601f_atan2(typval_T *argvars, typval_T *rettv)
1602{
1603 float_T fx = 0.0, fy = 0.0;
1604
1605 rettv->v_type = VAR_FLOAT;
1606 if (get_float_arg(argvars, &fx) == OK
1607 && get_float_arg(&argvars[1], &fy) == OK)
1608 rettv->vval.v_float = atan2(fx, fy);
1609 else
1610 rettv->vval.v_float = 0.0;
1611}
1612#endif
1613
1614/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001615 * "balloon_show()" function
1616 */
1617#ifdef FEAT_BEVAL
1618 static void
1619f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1620{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001621 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001622 {
1623 if (argvars[0].v_type == VAR_LIST
1624# ifdef FEAT_GUI
1625 && !gui.in_use
1626# endif
1627 )
1628 post_balloon(balloonEval, NULL, argvars[0].vval.v_list);
1629 else
1630 post_balloon(balloonEval, get_tv_string_chk(&argvars[0]), NULL);
1631 }
1632}
1633
Bram Moolenaar669a8282017-11-19 20:13:05 +01001634# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001635 static void
1636f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1637{
1638 if (rettv_list_alloc(rettv) == OK)
1639 {
1640 char_u *msg = get_tv_string_chk(&argvars[0]);
1641
1642 if (msg != NULL)
1643 {
1644 pumitem_T *array;
1645 int size = split_message(msg, &array);
1646 int i;
1647
1648 /* Skip the first and last item, they are always empty. */
1649 for (i = 1; i < size - 1; ++i)
1650 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001651 while (size > 0)
1652 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001653 vim_free(array);
1654 }
1655 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001656}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001657# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001658#endif
1659
1660/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001661 * "browse(save, title, initdir, default)" function
1662 */
1663 static void
1664f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1665{
1666#ifdef FEAT_BROWSE
1667 int save;
1668 char_u *title;
1669 char_u *initdir;
1670 char_u *defname;
1671 char_u buf[NUMBUFLEN];
1672 char_u buf2[NUMBUFLEN];
1673 int error = FALSE;
1674
1675 save = (int)get_tv_number_chk(&argvars[0], &error);
1676 title = get_tv_string_chk(&argvars[1]);
1677 initdir = get_tv_string_buf_chk(&argvars[2], buf);
1678 defname = get_tv_string_buf_chk(&argvars[3], buf2);
1679
1680 if (error || title == NULL || initdir == NULL || defname == NULL)
1681 rettv->vval.v_string = NULL;
1682 else
1683 rettv->vval.v_string =
1684 do_browse(save ? BROWSE_SAVE : 0,
1685 title, defname, NULL, initdir, NULL, curbuf);
1686#else
1687 rettv->vval.v_string = NULL;
1688#endif
1689 rettv->v_type = VAR_STRING;
1690}
1691
1692/*
1693 * "browsedir(title, initdir)" function
1694 */
1695 static void
1696f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1697{
1698#ifdef FEAT_BROWSE
1699 char_u *title;
1700 char_u *initdir;
1701 char_u buf[NUMBUFLEN];
1702
1703 title = get_tv_string_chk(&argvars[0]);
1704 initdir = get_tv_string_buf_chk(&argvars[1], buf);
1705
1706 if (title == NULL || initdir == NULL)
1707 rettv->vval.v_string = NULL;
1708 else
1709 rettv->vval.v_string = do_browse(BROWSE_DIR,
1710 title, NULL, NULL, initdir, NULL, curbuf);
1711#else
1712 rettv->vval.v_string = NULL;
1713#endif
1714 rettv->v_type = VAR_STRING;
1715}
1716
1717static buf_T *find_buffer(typval_T *avar);
1718
1719/*
1720 * Find a buffer by number or exact name.
1721 */
1722 static buf_T *
1723find_buffer(typval_T *avar)
1724{
1725 buf_T *buf = NULL;
1726
1727 if (avar->v_type == VAR_NUMBER)
1728 buf = buflist_findnr((int)avar->vval.v_number);
1729 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1730 {
1731 buf = buflist_findname_exp(avar->vval.v_string);
1732 if (buf == NULL)
1733 {
1734 /* No full path name match, try a match with a URL or a "nofile"
1735 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001736 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001737 if (buf->b_fname != NULL
1738 && (path_with_url(buf->b_fname)
1739#ifdef FEAT_QUICKFIX
1740 || bt_nofile(buf)
1741#endif
1742 )
1743 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1744 break;
1745 }
1746 }
1747 return buf;
1748}
1749
1750/*
1751 * "bufexists(expr)" function
1752 */
1753 static void
1754f_bufexists(typval_T *argvars, typval_T *rettv)
1755{
1756 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1757}
1758
1759/*
1760 * "buflisted(expr)" function
1761 */
1762 static void
1763f_buflisted(typval_T *argvars, typval_T *rettv)
1764{
1765 buf_T *buf;
1766
1767 buf = find_buffer(&argvars[0]);
1768 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1769}
1770
1771/*
1772 * "bufloaded(expr)" function
1773 */
1774 static void
1775f_bufloaded(typval_T *argvars, typval_T *rettv)
1776{
1777 buf_T *buf;
1778
1779 buf = find_buffer(&argvars[0]);
1780 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1781}
1782
1783 buf_T *
1784buflist_find_by_name(char_u *name, int curtab_only)
1785{
1786 int save_magic;
1787 char_u *save_cpo;
1788 buf_T *buf;
1789
1790 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1791 save_magic = p_magic;
1792 p_magic = TRUE;
1793 save_cpo = p_cpo;
1794 p_cpo = (char_u *)"";
1795
1796 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1797 TRUE, FALSE, curtab_only));
1798
1799 p_magic = save_magic;
1800 p_cpo = save_cpo;
1801 return buf;
1802}
1803
1804/*
1805 * Get buffer by number or pattern.
1806 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001807 buf_T *
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001808get_buf_tv(typval_T *tv, int curtab_only)
1809{
1810 char_u *name = tv->vval.v_string;
1811 buf_T *buf;
1812
1813 if (tv->v_type == VAR_NUMBER)
1814 return buflist_findnr((int)tv->vval.v_number);
1815 if (tv->v_type != VAR_STRING)
1816 return NULL;
1817 if (name == NULL || *name == NUL)
1818 return curbuf;
1819 if (name[0] == '$' && name[1] == NUL)
1820 return lastbuf;
1821
1822 buf = buflist_find_by_name(name, curtab_only);
1823
1824 /* If not found, try expanding the name, like done for bufexists(). */
1825 if (buf == NULL)
1826 buf = find_buffer(tv);
1827
1828 return buf;
1829}
1830
1831/*
1832 * "bufname(expr)" function
1833 */
1834 static void
1835f_bufname(typval_T *argvars, typval_T *rettv)
1836{
1837 buf_T *buf;
1838
1839 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1840 ++emsg_off;
1841 buf = get_buf_tv(&argvars[0], FALSE);
1842 rettv->v_type = VAR_STRING;
1843 if (buf != NULL && buf->b_fname != NULL)
1844 rettv->vval.v_string = vim_strsave(buf->b_fname);
1845 else
1846 rettv->vval.v_string = NULL;
1847 --emsg_off;
1848}
1849
1850/*
1851 * "bufnr(expr)" function
1852 */
1853 static void
1854f_bufnr(typval_T *argvars, typval_T *rettv)
1855{
1856 buf_T *buf;
1857 int error = FALSE;
1858 char_u *name;
1859
1860 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1861 ++emsg_off;
1862 buf = get_buf_tv(&argvars[0], FALSE);
1863 --emsg_off;
1864
1865 /* If the buffer isn't found and the second argument is not zero create a
1866 * new buffer. */
1867 if (buf == NULL
1868 && argvars[1].v_type != VAR_UNKNOWN
1869 && get_tv_number_chk(&argvars[1], &error) != 0
1870 && !error
1871 && (name = get_tv_string_chk(&argvars[0])) != NULL
1872 && !error)
1873 buf = buflist_new(name, NULL, (linenr_T)1, 0);
1874
1875 if (buf != NULL)
1876 rettv->vval.v_number = buf->b_fnum;
1877 else
1878 rettv->vval.v_number = -1;
1879}
1880
1881 static void
1882buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
1883{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001884 win_T *wp;
1885 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001886 buf_T *buf;
1887
1888 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1889 ++emsg_off;
1890 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02001891 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001892 {
1893 ++winnr;
1894 if (wp->w_buffer == buf)
1895 break;
1896 }
1897 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001898 --emsg_off;
1899}
1900
1901/*
1902 * "bufwinid(nr)" function
1903 */
1904 static void
1905f_bufwinid(typval_T *argvars, typval_T *rettv)
1906{
1907 buf_win_common(argvars, rettv, FALSE);
1908}
1909
1910/*
1911 * "bufwinnr(nr)" function
1912 */
1913 static void
1914f_bufwinnr(typval_T *argvars, typval_T *rettv)
1915{
1916 buf_win_common(argvars, rettv, TRUE);
1917}
1918
1919/*
1920 * "byte2line(byte)" function
1921 */
1922 static void
1923f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
1924{
1925#ifndef FEAT_BYTEOFF
1926 rettv->vval.v_number = -1;
1927#else
1928 long boff = 0;
1929
1930 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
1931 if (boff < 0)
1932 rettv->vval.v_number = -1;
1933 else
1934 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
1935 (linenr_T)0, &boff);
1936#endif
1937}
1938
1939 static void
1940byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
1941{
1942#ifdef FEAT_MBYTE
1943 char_u *t;
1944#endif
1945 char_u *str;
1946 varnumber_T idx;
1947
1948 str = get_tv_string_chk(&argvars[0]);
1949 idx = get_tv_number_chk(&argvars[1], NULL);
1950 rettv->vval.v_number = -1;
1951 if (str == NULL || idx < 0)
1952 return;
1953
1954#ifdef FEAT_MBYTE
1955 t = str;
1956 for ( ; idx > 0; idx--)
1957 {
1958 if (*t == NUL) /* EOL reached */
1959 return;
1960 if (enc_utf8 && comp)
1961 t += utf_ptr2len(t);
1962 else
1963 t += (*mb_ptr2len)(t);
1964 }
1965 rettv->vval.v_number = (varnumber_T)(t - str);
1966#else
1967 if ((size_t)idx <= STRLEN(str))
1968 rettv->vval.v_number = idx;
1969#endif
1970}
1971
1972/*
1973 * "byteidx()" function
1974 */
1975 static void
1976f_byteidx(typval_T *argvars, typval_T *rettv)
1977{
1978 byteidx(argvars, rettv, FALSE);
1979}
1980
1981/*
1982 * "byteidxcomp()" function
1983 */
1984 static void
1985f_byteidxcomp(typval_T *argvars, typval_T *rettv)
1986{
1987 byteidx(argvars, rettv, TRUE);
1988}
1989
1990/*
1991 * "call(func, arglist [, dict])" function
1992 */
1993 static void
1994f_call(typval_T *argvars, typval_T *rettv)
1995{
1996 char_u *func;
1997 partial_T *partial = NULL;
1998 dict_T *selfdict = NULL;
1999
2000 if (argvars[1].v_type != VAR_LIST)
2001 {
2002 EMSG(_(e_listreq));
2003 return;
2004 }
2005 if (argvars[1].vval.v_list == NULL)
2006 return;
2007
2008 if (argvars[0].v_type == VAR_FUNC)
2009 func = argvars[0].vval.v_string;
2010 else if (argvars[0].v_type == VAR_PARTIAL)
2011 {
2012 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002013 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002014 }
2015 else
2016 func = get_tv_string(&argvars[0]);
2017 if (*func == NUL)
2018 return; /* type error or empty name */
2019
2020 if (argvars[2].v_type != VAR_UNKNOWN)
2021 {
2022 if (argvars[2].v_type != VAR_DICT)
2023 {
2024 EMSG(_(e_dictreq));
2025 return;
2026 }
2027 selfdict = argvars[2].vval.v_dict;
2028 }
2029
2030 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2031}
2032
2033#ifdef FEAT_FLOAT
2034/*
2035 * "ceil({float})" function
2036 */
2037 static void
2038f_ceil(typval_T *argvars, typval_T *rettv)
2039{
2040 float_T f = 0.0;
2041
2042 rettv->v_type = VAR_FLOAT;
2043 if (get_float_arg(argvars, &f) == OK)
2044 rettv->vval.v_float = ceil(f);
2045 else
2046 rettv->vval.v_float = 0.0;
2047}
2048#endif
2049
2050#ifdef FEAT_JOB_CHANNEL
2051/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002052 * "ch_canread()" function
2053 */
2054 static void
2055f_ch_canread(typval_T *argvars, typval_T *rettv)
2056{
Bram Moolenaar958dc692016-12-01 15:34:12 +01002057 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002058
2059 rettv->vval.v_number = 0;
2060 if (channel != NULL)
2061 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
2062 || channel_has_readahead(channel, PART_OUT)
2063 || channel_has_readahead(channel, PART_ERR);
2064}
2065
2066/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002067 * "ch_close()" function
2068 */
2069 static void
2070f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
2071{
2072 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2073
2074 if (channel != NULL)
2075 {
2076 channel_close(channel, FALSE);
2077 channel_clear(channel);
2078 }
2079}
2080
2081/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002082 * "ch_close()" function
2083 */
2084 static void
2085f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
2086{
2087 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2088
2089 if (channel != NULL)
2090 channel_close_in(channel);
2091}
2092
2093/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002094 * "ch_getbufnr()" function
2095 */
2096 static void
2097f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
2098{
2099 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2100
2101 rettv->vval.v_number = -1;
2102 if (channel != NULL)
2103 {
2104 char_u *what = get_tv_string(&argvars[1]);
2105 int part;
2106
2107 if (STRCMP(what, "err") == 0)
2108 part = PART_ERR;
2109 else if (STRCMP(what, "out") == 0)
2110 part = PART_OUT;
2111 else if (STRCMP(what, "in") == 0)
2112 part = PART_IN;
2113 else
2114 part = PART_SOCK;
2115 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
2116 rettv->vval.v_number =
2117 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
2118 }
2119}
2120
2121/*
2122 * "ch_getjob()" function
2123 */
2124 static void
2125f_ch_getjob(typval_T *argvars, typval_T *rettv)
2126{
2127 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2128
2129 if (channel != NULL)
2130 {
2131 rettv->v_type = VAR_JOB;
2132 rettv->vval.v_job = channel->ch_job;
2133 if (channel->ch_job != NULL)
2134 ++channel->ch_job->jv_refcount;
2135 }
2136}
2137
2138/*
2139 * "ch_info()" function
2140 */
2141 static void
2142f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
2143{
2144 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2145
2146 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
2147 channel_info(channel, rettv->vval.v_dict);
2148}
2149
2150/*
2151 * "ch_log()" function
2152 */
2153 static void
2154f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
2155{
2156 char_u *msg = get_tv_string(&argvars[0]);
2157 channel_T *channel = NULL;
2158
2159 if (argvars[1].v_type != VAR_UNKNOWN)
2160 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
2161
Bram Moolenaard5359b22018-04-05 22:44:39 +02002162 ch_log(channel, "%s", msg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002163}
2164
2165/*
2166 * "ch_logfile()" function
2167 */
2168 static void
2169f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
2170{
2171 char_u *fname;
2172 char_u *opt = (char_u *)"";
2173 char_u buf[NUMBUFLEN];
2174
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02002175 /* Don't open a file in restricted mode. */
2176 if (check_restricted() || check_secure())
2177 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002178 fname = get_tv_string(&argvars[0]);
2179 if (argvars[1].v_type == VAR_STRING)
2180 opt = get_tv_string_buf(&argvars[1], buf);
2181 ch_logfile(fname, opt);
2182}
2183
2184/*
2185 * "ch_open()" function
2186 */
2187 static void
2188f_ch_open(typval_T *argvars, typval_T *rettv)
2189{
2190 rettv->v_type = VAR_CHANNEL;
2191 if (check_restricted() || check_secure())
2192 return;
2193 rettv->vval.v_channel = channel_open_func(argvars);
2194}
2195
2196/*
2197 * "ch_read()" function
2198 */
2199 static void
2200f_ch_read(typval_T *argvars, typval_T *rettv)
2201{
2202 common_channel_read(argvars, rettv, FALSE);
2203}
2204
2205/*
2206 * "ch_readraw()" function
2207 */
2208 static void
2209f_ch_readraw(typval_T *argvars, typval_T *rettv)
2210{
2211 common_channel_read(argvars, rettv, TRUE);
2212}
2213
2214/*
2215 * "ch_evalexpr()" function
2216 */
2217 static void
2218f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2219{
2220 ch_expr_common(argvars, rettv, TRUE);
2221}
2222
2223/*
2224 * "ch_sendexpr()" function
2225 */
2226 static void
2227f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2228{
2229 ch_expr_common(argvars, rettv, FALSE);
2230}
2231
2232/*
2233 * "ch_evalraw()" function
2234 */
2235 static void
2236f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2237{
2238 ch_raw_common(argvars, rettv, TRUE);
2239}
2240
2241/*
2242 * "ch_sendraw()" function
2243 */
2244 static void
2245f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2246{
2247 ch_raw_common(argvars, rettv, FALSE);
2248}
2249
2250/*
2251 * "ch_setoptions()" function
2252 */
2253 static void
2254f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2255{
2256 channel_T *channel;
2257 jobopt_T opt;
2258
2259 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2260 if (channel == NULL)
2261 return;
2262 clear_job_options(&opt);
2263 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002264 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002265 channel_set_options(channel, &opt);
2266 free_job_options(&opt);
2267}
2268
2269/*
2270 * "ch_status()" function
2271 */
2272 static void
2273f_ch_status(typval_T *argvars, typval_T *rettv)
2274{
2275 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002276 jobopt_T opt;
2277 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002278
2279 /* return an empty string by default */
2280 rettv->v_type = VAR_STRING;
2281 rettv->vval.v_string = NULL;
2282
2283 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002284
2285 if (argvars[1].v_type != VAR_UNKNOWN)
2286 {
2287 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002288 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002289 && (opt.jo_set & JO_PART))
2290 part = opt.jo_part;
2291 }
2292
2293 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002294}
2295#endif
2296
2297/*
2298 * "changenr()" function
2299 */
2300 static void
2301f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2302{
2303 rettv->vval.v_number = curbuf->b_u_seq_cur;
2304}
2305
2306/*
2307 * "char2nr(string)" function
2308 */
2309 static void
2310f_char2nr(typval_T *argvars, typval_T *rettv)
2311{
2312#ifdef FEAT_MBYTE
2313 if (has_mbyte)
2314 {
2315 int utf8 = 0;
2316
2317 if (argvars[1].v_type != VAR_UNKNOWN)
2318 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
2319
2320 if (utf8)
2321 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
2322 else
2323 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
2324 }
2325 else
2326#endif
2327 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
2328}
2329
2330/*
2331 * "cindent(lnum)" function
2332 */
2333 static void
2334f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2335{
2336#ifdef FEAT_CINDENT
2337 pos_T pos;
2338 linenr_T lnum;
2339
2340 pos = curwin->w_cursor;
2341 lnum = get_tv_lnum(argvars);
2342 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2343 {
2344 curwin->w_cursor.lnum = lnum;
2345 rettv->vval.v_number = get_c_indent();
2346 curwin->w_cursor = pos;
2347 }
2348 else
2349#endif
2350 rettv->vval.v_number = -1;
2351}
2352
2353/*
2354 * "clearmatches()" function
2355 */
2356 static void
2357f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2358{
2359#ifdef FEAT_SEARCH_EXTRA
2360 clear_matches(curwin);
2361#endif
2362}
2363
2364/*
2365 * "col(string)" function
2366 */
2367 static void
2368f_col(typval_T *argvars, typval_T *rettv)
2369{
2370 colnr_T col = 0;
2371 pos_T *fp;
2372 int fnum = curbuf->b_fnum;
2373
2374 fp = var2fpos(&argvars[0], FALSE, &fnum);
2375 if (fp != NULL && fnum == curbuf->b_fnum)
2376 {
2377 if (fp->col == MAXCOL)
2378 {
2379 /* '> can be MAXCOL, get the length of the line then */
2380 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2381 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2382 else
2383 col = MAXCOL;
2384 }
2385 else
2386 {
2387 col = fp->col + 1;
2388#ifdef FEAT_VIRTUALEDIT
2389 /* col(".") when the cursor is on the NUL at the end of the line
2390 * because of "coladd" can be seen as an extra column. */
2391 if (virtual_active() && fp == &curwin->w_cursor)
2392 {
2393 char_u *p = ml_get_cursor();
2394
2395 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2396 curwin->w_virtcol - curwin->w_cursor.coladd))
2397 {
2398# ifdef FEAT_MBYTE
2399 int l;
2400
2401 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2402 col += l;
2403# else
2404 if (*p != NUL && p[1] == NUL)
2405 ++col;
2406# endif
2407 }
2408 }
2409#endif
2410 }
2411 }
2412 rettv->vval.v_number = col;
2413}
2414
2415#if defined(FEAT_INS_EXPAND)
2416/*
2417 * "complete()" function
2418 */
2419 static void
2420f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2421{
2422 int startcol;
2423
2424 if ((State & INSERT) == 0)
2425 {
2426 EMSG(_("E785: complete() can only be used in Insert mode"));
2427 return;
2428 }
2429
2430 /* Check for undo allowed here, because if something was already inserted
2431 * the line was already saved for undo and this check isn't done. */
2432 if (!undo_allowed())
2433 return;
2434
2435 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2436 {
2437 EMSG(_(e_invarg));
2438 return;
2439 }
2440
2441 startcol = (int)get_tv_number_chk(&argvars[0], NULL);
2442 if (startcol <= 0)
2443 return;
2444
2445 set_completion(startcol - 1, argvars[1].vval.v_list);
2446}
2447
2448/*
2449 * "complete_add()" function
2450 */
2451 static void
2452f_complete_add(typval_T *argvars, typval_T *rettv)
2453{
2454 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2455}
2456
2457/*
2458 * "complete_check()" function
2459 */
2460 static void
2461f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2462{
2463 int saved = RedrawingDisabled;
2464
2465 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002466 ins_compl_check_keys(0, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002467 rettv->vval.v_number = compl_interrupted;
2468 RedrawingDisabled = saved;
2469}
2470#endif
2471
2472/*
2473 * "confirm(message, buttons[, default [, type]])" function
2474 */
2475 static void
2476f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2477{
2478#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2479 char_u *message;
2480 char_u *buttons = NULL;
2481 char_u buf[NUMBUFLEN];
2482 char_u buf2[NUMBUFLEN];
2483 int def = 1;
2484 int type = VIM_GENERIC;
2485 char_u *typestr;
2486 int error = FALSE;
2487
2488 message = get_tv_string_chk(&argvars[0]);
2489 if (message == NULL)
2490 error = TRUE;
2491 if (argvars[1].v_type != VAR_UNKNOWN)
2492 {
2493 buttons = get_tv_string_buf_chk(&argvars[1], buf);
2494 if (buttons == NULL)
2495 error = TRUE;
2496 if (argvars[2].v_type != VAR_UNKNOWN)
2497 {
2498 def = (int)get_tv_number_chk(&argvars[2], &error);
2499 if (argvars[3].v_type != VAR_UNKNOWN)
2500 {
2501 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
2502 if (typestr == NULL)
2503 error = TRUE;
2504 else
2505 {
2506 switch (TOUPPER_ASC(*typestr))
2507 {
2508 case 'E': type = VIM_ERROR; break;
2509 case 'Q': type = VIM_QUESTION; break;
2510 case 'I': type = VIM_INFO; break;
2511 case 'W': type = VIM_WARNING; break;
2512 case 'G': type = VIM_GENERIC; break;
2513 }
2514 }
2515 }
2516 }
2517 }
2518
2519 if (buttons == NULL || *buttons == NUL)
2520 buttons = (char_u *)_("&Ok");
2521
2522 if (!error)
2523 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2524 def, NULL, FALSE);
2525#endif
2526}
2527
2528/*
2529 * "copy()" function
2530 */
2531 static void
2532f_copy(typval_T *argvars, typval_T *rettv)
2533{
2534 item_copy(&argvars[0], rettv, FALSE, 0);
2535}
2536
2537#ifdef FEAT_FLOAT
2538/*
2539 * "cos()" function
2540 */
2541 static void
2542f_cos(typval_T *argvars, typval_T *rettv)
2543{
2544 float_T f = 0.0;
2545
2546 rettv->v_type = VAR_FLOAT;
2547 if (get_float_arg(argvars, &f) == OK)
2548 rettv->vval.v_float = cos(f);
2549 else
2550 rettv->vval.v_float = 0.0;
2551}
2552
2553/*
2554 * "cosh()" function
2555 */
2556 static void
2557f_cosh(typval_T *argvars, typval_T *rettv)
2558{
2559 float_T f = 0.0;
2560
2561 rettv->v_type = VAR_FLOAT;
2562 if (get_float_arg(argvars, &f) == OK)
2563 rettv->vval.v_float = cosh(f);
2564 else
2565 rettv->vval.v_float = 0.0;
2566}
2567#endif
2568
2569/*
2570 * "count()" function
2571 */
2572 static void
2573f_count(typval_T *argvars, typval_T *rettv)
2574{
2575 long n = 0;
2576 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002577 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002578
Bram Moolenaar9966b212017-07-28 16:46:57 +02002579 if (argvars[2].v_type != VAR_UNKNOWN)
2580 ic = (int)get_tv_number_chk(&argvars[2], &error);
2581
2582 if (argvars[0].v_type == VAR_STRING)
2583 {
2584 char_u *expr = get_tv_string_chk(&argvars[1]);
2585 char_u *p = argvars[0].vval.v_string;
2586 char_u *next;
2587
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002588 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002589 {
2590 if (ic)
2591 {
2592 size_t len = STRLEN(expr);
2593
2594 while (*p != NUL)
2595 {
2596 if (MB_STRNICMP(p, expr, len) == 0)
2597 {
2598 ++n;
2599 p += len;
2600 }
2601 else
2602 MB_PTR_ADV(p);
2603 }
2604 }
2605 else
2606 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2607 != NULL)
2608 {
2609 ++n;
2610 p = next + STRLEN(expr);
2611 }
2612 }
2613
2614 }
2615 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002616 {
2617 listitem_T *li;
2618 list_T *l;
2619 long idx;
2620
2621 if ((l = argvars[0].vval.v_list) != NULL)
2622 {
2623 li = l->lv_first;
2624 if (argvars[2].v_type != VAR_UNKNOWN)
2625 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002626 if (argvars[3].v_type != VAR_UNKNOWN)
2627 {
2628 idx = (long)get_tv_number_chk(&argvars[3], &error);
2629 if (!error)
2630 {
2631 li = list_find(l, idx);
2632 if (li == NULL)
2633 EMSGN(_(e_listidx), idx);
2634 }
2635 }
2636 if (error)
2637 li = NULL;
2638 }
2639
2640 for ( ; li != NULL; li = li->li_next)
2641 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2642 ++n;
2643 }
2644 }
2645 else if (argvars[0].v_type == VAR_DICT)
2646 {
2647 int todo;
2648 dict_T *d;
2649 hashitem_T *hi;
2650
2651 if ((d = argvars[0].vval.v_dict) != NULL)
2652 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002653 if (argvars[2].v_type != VAR_UNKNOWN)
2654 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002655 if (argvars[3].v_type != VAR_UNKNOWN)
2656 EMSG(_(e_invarg));
2657 }
2658
2659 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2660 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2661 {
2662 if (!HASHITEM_EMPTY(hi))
2663 {
2664 --todo;
2665 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2666 ++n;
2667 }
2668 }
2669 }
2670 }
2671 else
2672 EMSG2(_(e_listdictarg), "count()");
2673 rettv->vval.v_number = n;
2674}
2675
2676/*
2677 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2678 *
2679 * Checks the existence of a cscope connection.
2680 */
2681 static void
2682f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2683{
2684#ifdef FEAT_CSCOPE
2685 int num = 0;
2686 char_u *dbpath = NULL;
2687 char_u *prepend = NULL;
2688 char_u buf[NUMBUFLEN];
2689
2690 if (argvars[0].v_type != VAR_UNKNOWN
2691 && argvars[1].v_type != VAR_UNKNOWN)
2692 {
2693 num = (int)get_tv_number(&argvars[0]);
2694 dbpath = get_tv_string(&argvars[1]);
2695 if (argvars[2].v_type != VAR_UNKNOWN)
2696 prepend = get_tv_string_buf(&argvars[2], buf);
2697 }
2698
2699 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2700#endif
2701}
2702
2703/*
2704 * "cursor(lnum, col)" function, or
2705 * "cursor(list)"
2706 *
2707 * Moves the cursor to the specified line and column.
2708 * Returns 0 when the position could be set, -1 otherwise.
2709 */
2710 static void
2711f_cursor(typval_T *argvars, typval_T *rettv)
2712{
2713 long line, col;
2714#ifdef FEAT_VIRTUALEDIT
2715 long coladd = 0;
2716#endif
2717 int set_curswant = TRUE;
2718
2719 rettv->vval.v_number = -1;
2720 if (argvars[1].v_type == VAR_UNKNOWN)
2721 {
2722 pos_T pos;
2723 colnr_T curswant = -1;
2724
2725 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2726 {
2727 EMSG(_(e_invarg));
2728 return;
2729 }
2730 line = pos.lnum;
2731 col = pos.col;
2732#ifdef FEAT_VIRTUALEDIT
2733 coladd = pos.coladd;
2734#endif
2735 if (curswant >= 0)
2736 {
2737 curwin->w_curswant = curswant - 1;
2738 set_curswant = FALSE;
2739 }
2740 }
2741 else
2742 {
2743 line = get_tv_lnum(argvars);
2744 col = (long)get_tv_number_chk(&argvars[1], NULL);
2745#ifdef FEAT_VIRTUALEDIT
2746 if (argvars[2].v_type != VAR_UNKNOWN)
2747 coladd = (long)get_tv_number_chk(&argvars[2], NULL);
2748#endif
2749 }
2750 if (line < 0 || col < 0
2751#ifdef FEAT_VIRTUALEDIT
2752 || coladd < 0
2753#endif
2754 )
2755 return; /* type error; errmsg already given */
2756 if (line > 0)
2757 curwin->w_cursor.lnum = line;
2758 if (col > 0)
2759 curwin->w_cursor.col = col - 1;
2760#ifdef FEAT_VIRTUALEDIT
2761 curwin->w_cursor.coladd = coladd;
2762#endif
2763
2764 /* Make sure the cursor is in a valid position. */
2765 check_cursor();
2766#ifdef FEAT_MBYTE
2767 /* Correct cursor for multi-byte character. */
2768 if (has_mbyte)
2769 mb_adjust_cursor();
2770#endif
2771
2772 curwin->w_set_curswant = set_curswant;
2773 rettv->vval.v_number = 0;
2774}
2775
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002776#ifdef WIN3264
2777/*
2778 * "debugbreak()" function
2779 */
2780 static void
2781f_debugbreak(typval_T *argvars, typval_T *rettv)
2782{
2783 int pid;
2784
2785 rettv->vval.v_number = FAIL;
2786 pid = (int)get_tv_number(&argvars[0]);
2787 if (pid == 0)
2788 EMSG(_(e_invarg));
2789 else
2790 {
2791 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2792
2793 if (hProcess != NULL)
2794 {
2795 DebugBreakProcess(hProcess);
2796 CloseHandle(hProcess);
2797 rettv->vval.v_number = OK;
2798 }
2799 }
2800}
2801#endif
2802
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002803/*
2804 * "deepcopy()" function
2805 */
2806 static void
2807f_deepcopy(typval_T *argvars, typval_T *rettv)
2808{
2809 int noref = 0;
2810 int copyID;
2811
2812 if (argvars[1].v_type != VAR_UNKNOWN)
2813 noref = (int)get_tv_number_chk(&argvars[1], NULL);
2814 if (noref < 0 || noref > 1)
2815 EMSG(_(e_invarg));
2816 else
2817 {
2818 copyID = get_copyID();
2819 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2820 }
2821}
2822
2823/*
2824 * "delete()" function
2825 */
2826 static void
2827f_delete(typval_T *argvars, typval_T *rettv)
2828{
2829 char_u nbuf[NUMBUFLEN];
2830 char_u *name;
2831 char_u *flags;
2832
2833 rettv->vval.v_number = -1;
2834 if (check_restricted() || check_secure())
2835 return;
2836
2837 name = get_tv_string(&argvars[0]);
2838 if (name == NULL || *name == NUL)
2839 {
2840 EMSG(_(e_invarg));
2841 return;
2842 }
2843
2844 if (argvars[1].v_type != VAR_UNKNOWN)
2845 flags = get_tv_string_buf(&argvars[1], nbuf);
2846 else
2847 flags = (char_u *)"";
2848
2849 if (*flags == NUL)
2850 /* delete a file */
2851 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2852 else if (STRCMP(flags, "d") == 0)
2853 /* delete an empty directory */
2854 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2855 else if (STRCMP(flags, "rf") == 0)
2856 /* delete a directory recursively */
2857 rettv->vval.v_number = delete_recursive(name);
2858 else
2859 EMSG2(_(e_invexpr2), flags);
2860}
2861
2862/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02002863 * "deletebufline()" function
2864 */
2865 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02002866f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02002867{
2868 buf_T *buf;
2869 linenr_T first, last;
2870 linenr_T lnum;
2871 long count;
2872 int is_curbuf;
2873 buf_T *curbuf_save = NULL;
2874 win_T *curwin_save = NULL;
2875 tabpage_T *tp;
2876 win_T *wp;
2877
2878 buf = get_buf_tv(&argvars[0], FALSE);
2879 if (buf == NULL)
2880 {
2881 rettv->vval.v_number = 1; /* FAIL */
2882 return;
2883 }
2884 is_curbuf = buf == curbuf;
2885
2886 first = get_tv_lnum_buf(&argvars[1], buf);
2887 if (argvars[2].v_type != VAR_UNKNOWN)
2888 last = get_tv_lnum_buf(&argvars[2], buf);
2889 else
2890 last = first;
2891
2892 if (buf->b_ml.ml_mfp == NULL || first < 1
2893 || first > buf->b_ml.ml_line_count || last < first)
2894 {
2895 rettv->vval.v_number = 1; /* FAIL */
2896 return;
2897 }
2898
2899 if (!is_curbuf)
2900 {
2901 curbuf_save = curbuf;
2902 curwin_save = curwin;
2903 curbuf = buf;
2904 find_win_for_curbuf();
2905 }
2906 if (last > curbuf->b_ml.ml_line_count)
2907 last = curbuf->b_ml.ml_line_count;
2908 count = last - first + 1;
2909
2910 // When coming here from Insert mode, sync undo, so that this can be
2911 // undone separately from what was previously inserted.
2912 if (u_sync_once == 2)
2913 {
2914 u_sync_once = 1; // notify that u_sync() was called
2915 u_sync(TRUE);
2916 }
2917
2918 if (u_save(first - 1, last + 1) == FAIL)
2919 {
2920 rettv->vval.v_number = 1; /* FAIL */
2921 return;
2922 }
2923
2924 for (lnum = first; lnum <= last; ++lnum)
2925 ml_delete(first, TRUE);
2926
2927 FOR_ALL_TAB_WINDOWS(tp, wp)
2928 if (wp->w_buffer == buf)
2929 {
2930 if (wp->w_cursor.lnum > last)
2931 wp->w_cursor.lnum -= count;
2932 else if (wp->w_cursor.lnum> first)
2933 wp->w_cursor.lnum = first;
2934 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
2935 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
2936 }
2937 check_cursor_col();
2938 deleted_lines_mark(first, count);
2939
2940 if (!is_curbuf)
2941 {
2942 curbuf = curbuf_save;
2943 curwin = curwin_save;
2944 }
2945}
2946
2947/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002948 * "did_filetype()" function
2949 */
2950 static void
2951f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2952{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002953 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002954}
2955
2956/*
2957 * "diff_filler()" function
2958 */
2959 static void
2960f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2961{
2962#ifdef FEAT_DIFF
2963 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
2964#endif
2965}
2966
2967/*
2968 * "diff_hlID()" function
2969 */
2970 static void
2971f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2972{
2973#ifdef FEAT_DIFF
2974 linenr_T lnum = get_tv_lnum(argvars);
2975 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01002976 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002977 static int fnum = 0;
2978 static int change_start = 0;
2979 static int change_end = 0;
2980 static hlf_T hlID = (hlf_T)0;
2981 int filler_lines;
2982 int col;
2983
2984 if (lnum < 0) /* ignore type error in {lnum} arg */
2985 lnum = 0;
2986 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002987 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002988 || fnum != curbuf->b_fnum)
2989 {
2990 /* New line, buffer, change: need to get the values. */
2991 filler_lines = diff_check(curwin, lnum);
2992 if (filler_lines < 0)
2993 {
2994 if (filler_lines == -1)
2995 {
2996 change_start = MAXCOL;
2997 change_end = -1;
2998 if (diff_find_change(curwin, lnum, &change_start, &change_end))
2999 hlID = HLF_ADD; /* added line */
3000 else
3001 hlID = HLF_CHD; /* changed line */
3002 }
3003 else
3004 hlID = HLF_ADD; /* added line */
3005 }
3006 else
3007 hlID = (hlf_T)0;
3008 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003009 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003010 fnum = curbuf->b_fnum;
3011 }
3012
3013 if (hlID == HLF_CHD || hlID == HLF_TXD)
3014 {
3015 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
3016 if (col >= change_start && col <= change_end)
3017 hlID = HLF_TXD; /* changed text */
3018 else
3019 hlID = HLF_CHD; /* changed line */
3020 }
3021 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
3022#endif
3023}
3024
3025/*
3026 * "empty({expr})" function
3027 */
3028 static void
3029f_empty(typval_T *argvars, typval_T *rettv)
3030{
3031 int n = FALSE;
3032
3033 switch (argvars[0].v_type)
3034 {
3035 case VAR_STRING:
3036 case VAR_FUNC:
3037 n = argvars[0].vval.v_string == NULL
3038 || *argvars[0].vval.v_string == NUL;
3039 break;
3040 case VAR_PARTIAL:
3041 n = FALSE;
3042 break;
3043 case VAR_NUMBER:
3044 n = argvars[0].vval.v_number == 0;
3045 break;
3046 case VAR_FLOAT:
3047#ifdef FEAT_FLOAT
3048 n = argvars[0].vval.v_float == 0.0;
3049 break;
3050#endif
3051 case VAR_LIST:
3052 n = argvars[0].vval.v_list == NULL
3053 || argvars[0].vval.v_list->lv_first == NULL;
3054 break;
3055 case VAR_DICT:
3056 n = argvars[0].vval.v_dict == NULL
3057 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
3058 break;
3059 case VAR_SPECIAL:
3060 n = argvars[0].vval.v_number != VVAL_TRUE;
3061 break;
3062
3063 case VAR_JOB:
3064#ifdef FEAT_JOB_CHANNEL
3065 n = argvars[0].vval.v_job == NULL
3066 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
3067 break;
3068#endif
3069 case VAR_CHANNEL:
3070#ifdef FEAT_JOB_CHANNEL
3071 n = argvars[0].vval.v_channel == NULL
3072 || !channel_is_open(argvars[0].vval.v_channel);
3073 break;
3074#endif
3075 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01003076 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003077 n = TRUE;
3078 break;
3079 }
3080
3081 rettv->vval.v_number = n;
3082}
3083
3084/*
3085 * "escape({string}, {chars})" function
3086 */
3087 static void
3088f_escape(typval_T *argvars, typval_T *rettv)
3089{
3090 char_u buf[NUMBUFLEN];
3091
3092 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
3093 get_tv_string_buf(&argvars[1], buf));
3094 rettv->v_type = VAR_STRING;
3095}
3096
3097/*
3098 * "eval()" function
3099 */
3100 static void
3101f_eval(typval_T *argvars, typval_T *rettv)
3102{
3103 char_u *s, *p;
3104
3105 s = get_tv_string_chk(&argvars[0]);
3106 if (s != NULL)
3107 s = skipwhite(s);
3108
3109 p = s;
3110 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3111 {
3112 if (p != NULL && !aborting())
3113 EMSG2(_(e_invexpr2), p);
3114 need_clr_eos = FALSE;
3115 rettv->v_type = VAR_NUMBER;
3116 rettv->vval.v_number = 0;
3117 }
3118 else if (*s != NUL)
3119 EMSG(_(e_trailing));
3120}
3121
3122/*
3123 * "eventhandler()" function
3124 */
3125 static void
3126f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3127{
3128 rettv->vval.v_number = vgetc_busy;
3129}
3130
3131/*
3132 * "executable()" function
3133 */
3134 static void
3135f_executable(typval_T *argvars, typval_T *rettv)
3136{
3137 char_u *name = get_tv_string(&argvars[0]);
3138
3139 /* Check in $PATH and also check directly if there is a directory name. */
3140 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
3141 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
3142}
3143
3144static garray_T redir_execute_ga;
3145
3146/*
3147 * Append "value[value_len]" to the execute() output.
3148 */
3149 void
3150execute_redir_str(char_u *value, int value_len)
3151{
3152 int len;
3153
3154 if (value_len == -1)
3155 len = (int)STRLEN(value); /* Append the entire string */
3156 else
3157 len = value_len; /* Append only "value_len" characters */
3158 if (ga_grow(&redir_execute_ga, len) == OK)
3159 {
3160 mch_memmove((char *)redir_execute_ga.ga_data
3161 + redir_execute_ga.ga_len, value, len);
3162 redir_execute_ga.ga_len += len;
3163 }
3164}
3165
3166/*
3167 * Get next line from a list.
3168 * Called by do_cmdline() to get the next line.
3169 * Returns allocated string, or NULL for end of function.
3170 */
3171
3172 static char_u *
3173get_list_line(
3174 int c UNUSED,
3175 void *cookie,
3176 int indent UNUSED)
3177{
3178 listitem_T **p = (listitem_T **)cookie;
3179 listitem_T *item = *p;
3180 char_u buf[NUMBUFLEN];
3181 char_u *s;
3182
3183 if (item == NULL)
3184 return NULL;
3185 s = get_tv_string_buf_chk(&item->li_tv, buf);
3186 *p = item->li_next;
3187 return s == NULL ? NULL : vim_strsave(s);
3188}
3189
3190/*
3191 * "execute()" function
3192 */
3193 static void
3194f_execute(typval_T *argvars, typval_T *rettv)
3195{
3196 char_u *cmd = NULL;
3197 list_T *list = NULL;
3198 int save_msg_silent = msg_silent;
3199 int save_emsg_silent = emsg_silent;
3200 int save_emsg_noredir = emsg_noredir;
3201 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003202 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003203 garray_T save_ga;
3204
3205 rettv->vval.v_string = NULL;
3206 rettv->v_type = VAR_STRING;
3207
3208 if (argvars[0].v_type == VAR_LIST)
3209 {
3210 list = argvars[0].vval.v_list;
3211 if (list == NULL || list->lv_first == NULL)
3212 /* empty list, no commands, empty output */
3213 return;
3214 ++list->lv_refcount;
3215 }
3216 else
3217 {
3218 cmd = get_tv_string_chk(&argvars[0]);
3219 if (cmd == NULL)
3220 return;
3221 }
3222
3223 if (argvars[1].v_type != VAR_UNKNOWN)
3224 {
3225 char_u buf[NUMBUFLEN];
3226 char_u *s = get_tv_string_buf_chk(&argvars[1], buf);
3227
3228 if (s == NULL)
3229 return;
3230 if (STRNCMP(s, "silent", 6) == 0)
3231 ++msg_silent;
3232 if (STRCMP(s, "silent!") == 0)
3233 {
3234 emsg_silent = TRUE;
3235 emsg_noredir = TRUE;
3236 }
3237 }
3238 else
3239 ++msg_silent;
3240
3241 if (redir_execute)
3242 save_ga = redir_execute_ga;
3243 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3244 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003245 redir_off = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003246
3247 if (cmd != NULL)
3248 do_cmdline_cmd(cmd);
3249 else
3250 {
3251 listitem_T *item = list->lv_first;
3252
3253 do_cmdline(NULL, get_list_line, (void *)&item,
3254 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3255 --list->lv_refcount;
3256 }
3257
Bram Moolenaard297f352017-01-29 20:31:21 +01003258 /* Need to append a NUL to the result. */
3259 if (ga_grow(&redir_execute_ga, 1) == OK)
3260 {
3261 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3262 rettv->vval.v_string = redir_execute_ga.ga_data;
3263 }
3264 else
3265 {
3266 ga_clear(&redir_execute_ga);
3267 rettv->vval.v_string = NULL;
3268 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003269 msg_silent = save_msg_silent;
3270 emsg_silent = save_emsg_silent;
3271 emsg_noredir = save_emsg_noredir;
3272
3273 redir_execute = save_redir_execute;
3274 if (redir_execute)
3275 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003276 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003277
3278 /* "silent reg" or "silent echo x" leaves msg_col somewhere in the
3279 * line. Put it back in the first column. */
3280 msg_col = 0;
3281}
3282
3283/*
3284 * "exepath()" function
3285 */
3286 static void
3287f_exepath(typval_T *argvars, typval_T *rettv)
3288{
3289 char_u *p = NULL;
3290
3291 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
3292 rettv->v_type = VAR_STRING;
3293 rettv->vval.v_string = p;
3294}
3295
3296/*
3297 * "exists()" function
3298 */
3299 static void
3300f_exists(typval_T *argvars, typval_T *rettv)
3301{
3302 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003303 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003304
3305 p = get_tv_string(&argvars[0]);
3306 if (*p == '$') /* environment variable */
3307 {
3308 /* first try "normal" environment variables (fast) */
3309 if (mch_getenv(p + 1) != NULL)
3310 n = TRUE;
3311 else
3312 {
3313 /* try expanding things like $VIM and ${HOME} */
3314 p = expand_env_save(p);
3315 if (p != NULL && *p != '$')
3316 n = TRUE;
3317 vim_free(p);
3318 }
3319 }
3320 else if (*p == '&' || *p == '+') /* option */
3321 {
3322 n = (get_option_tv(&p, NULL, TRUE) == OK);
3323 if (*skipwhite(p) != NUL)
3324 n = FALSE; /* trailing garbage */
3325 }
3326 else if (*p == '*') /* internal or user defined function */
3327 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003328 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003329 }
3330 else if (*p == ':')
3331 {
3332 n = cmd_exists(p + 1);
3333 }
3334 else if (*p == '#')
3335 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003336 if (p[1] == '#')
3337 n = autocmd_supported(p + 2);
3338 else
3339 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003340 }
3341 else /* internal variable */
3342 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003343 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003344 }
3345
3346 rettv->vval.v_number = n;
3347}
3348
3349#ifdef FEAT_FLOAT
3350/*
3351 * "exp()" function
3352 */
3353 static void
3354f_exp(typval_T *argvars, typval_T *rettv)
3355{
3356 float_T f = 0.0;
3357
3358 rettv->v_type = VAR_FLOAT;
3359 if (get_float_arg(argvars, &f) == OK)
3360 rettv->vval.v_float = exp(f);
3361 else
3362 rettv->vval.v_float = 0.0;
3363}
3364#endif
3365
3366/*
3367 * "expand()" function
3368 */
3369 static void
3370f_expand(typval_T *argvars, typval_T *rettv)
3371{
3372 char_u *s;
3373 int len;
3374 char_u *errormsg;
3375 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3376 expand_T xpc;
3377 int error = FALSE;
3378 char_u *result;
3379
3380 rettv->v_type = VAR_STRING;
3381 if (argvars[1].v_type != VAR_UNKNOWN
3382 && argvars[2].v_type != VAR_UNKNOWN
3383 && get_tv_number_chk(&argvars[2], &error)
3384 && !error)
3385 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003386 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003387 }
3388
3389 s = get_tv_string(&argvars[0]);
3390 if (*s == '%' || *s == '#' || *s == '<')
3391 {
3392 ++emsg_off;
3393 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3394 --emsg_off;
3395 if (rettv->v_type == VAR_LIST)
3396 {
3397 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3398 list_append_string(rettv->vval.v_list, result, -1);
3399 else
3400 vim_free(result);
3401 }
3402 else
3403 rettv->vval.v_string = result;
3404 }
3405 else
3406 {
3407 /* When the optional second argument is non-zero, don't remove matches
3408 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3409 if (argvars[1].v_type != VAR_UNKNOWN
3410 && get_tv_number_chk(&argvars[1], &error))
3411 options |= WILD_KEEP_ALL;
3412 if (!error)
3413 {
3414 ExpandInit(&xpc);
3415 xpc.xp_context = EXPAND_FILES;
3416 if (p_wic)
3417 options += WILD_ICASE;
3418 if (rettv->v_type == VAR_STRING)
3419 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3420 options, WILD_ALL);
3421 else if (rettv_list_alloc(rettv) != FAIL)
3422 {
3423 int i;
3424
3425 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3426 for (i = 0; i < xpc.xp_numfiles; i++)
3427 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3428 ExpandCleanup(&xpc);
3429 }
3430 }
3431 else
3432 rettv->vval.v_string = NULL;
3433 }
3434}
3435
3436/*
3437 * "extend(list, list [, idx])" function
3438 * "extend(dict, dict [, action])" function
3439 */
3440 static void
3441f_extend(typval_T *argvars, typval_T *rettv)
3442{
3443 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3444
3445 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3446 {
3447 list_T *l1, *l2;
3448 listitem_T *item;
3449 long before;
3450 int error = FALSE;
3451
3452 l1 = argvars[0].vval.v_list;
3453 l2 = argvars[1].vval.v_list;
3454 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
3455 && l2 != NULL)
3456 {
3457 if (argvars[2].v_type != VAR_UNKNOWN)
3458 {
3459 before = (long)get_tv_number_chk(&argvars[2], &error);
3460 if (error)
3461 return; /* type error; errmsg already given */
3462
3463 if (before == l1->lv_len)
3464 item = NULL;
3465 else
3466 {
3467 item = list_find(l1, before);
3468 if (item == NULL)
3469 {
3470 EMSGN(_(e_listidx), before);
3471 return;
3472 }
3473 }
3474 }
3475 else
3476 item = NULL;
3477 list_extend(l1, l2, item);
3478
3479 copy_tv(&argvars[0], rettv);
3480 }
3481 }
3482 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3483 {
3484 dict_T *d1, *d2;
3485 char_u *action;
3486 int i;
3487
3488 d1 = argvars[0].vval.v_dict;
3489 d2 = argvars[1].vval.v_dict;
3490 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
3491 && d2 != NULL)
3492 {
3493 /* Check the third argument. */
3494 if (argvars[2].v_type != VAR_UNKNOWN)
3495 {
3496 static char *(av[]) = {"keep", "force", "error"};
3497
3498 action = get_tv_string_chk(&argvars[2]);
3499 if (action == NULL)
3500 return; /* type error; errmsg already given */
3501 for (i = 0; i < 3; ++i)
3502 if (STRCMP(action, av[i]) == 0)
3503 break;
3504 if (i == 3)
3505 {
3506 EMSG2(_(e_invarg2), action);
3507 return;
3508 }
3509 }
3510 else
3511 action = (char_u *)"force";
3512
3513 dict_extend(d1, d2, action);
3514
3515 copy_tv(&argvars[0], rettv);
3516 }
3517 }
3518 else
3519 EMSG2(_(e_listdictarg), "extend()");
3520}
3521
3522/*
3523 * "feedkeys()" function
3524 */
3525 static void
3526f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3527{
3528 int remap = TRUE;
3529 int insert = FALSE;
3530 char_u *keys, *flags;
3531 char_u nbuf[NUMBUFLEN];
3532 int typed = FALSE;
3533 int execute = FALSE;
3534 int dangerous = FALSE;
3535 char_u *keys_esc;
3536
3537 /* This is not allowed in the sandbox. If the commands would still be
3538 * executed in the sandbox it would be OK, but it probably happens later,
3539 * when "sandbox" is no longer set. */
3540 if (check_secure())
3541 return;
3542
3543 keys = get_tv_string(&argvars[0]);
3544
3545 if (argvars[1].v_type != VAR_UNKNOWN)
3546 {
3547 flags = get_tv_string_buf(&argvars[1], nbuf);
3548 for ( ; *flags != NUL; ++flags)
3549 {
3550 switch (*flags)
3551 {
3552 case 'n': remap = FALSE; break;
3553 case 'm': remap = TRUE; break;
3554 case 't': typed = TRUE; break;
3555 case 'i': insert = TRUE; break;
3556 case 'x': execute = TRUE; break;
3557 case '!': dangerous = TRUE; break;
3558 }
3559 }
3560 }
3561
3562 if (*keys != NUL || execute)
3563 {
3564 /* Need to escape K_SPECIAL and CSI before putting the string in the
3565 * typeahead buffer. */
3566 keys_esc = vim_strsave_escape_csi(keys);
3567 if (keys_esc != NULL)
3568 {
3569 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
3570 insert ? 0 : typebuf.tb_len, !typed, FALSE);
3571 vim_free(keys_esc);
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003572 if (vgetc_busy
3573#ifdef FEAT_TIMERS
3574 || timer_busy
3575#endif
3576 )
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003577 typebuf_was_filled = TRUE;
3578 if (execute)
3579 {
3580 int save_msg_scroll = msg_scroll;
3581
3582 /* Avoid a 1 second delay when the keys start Insert mode. */
3583 msg_scroll = FALSE;
3584
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003585 if (!dangerous)
3586 ++ex_normal_busy;
Bram Moolenaar655a82a2018-05-08 22:01:07 +02003587 exec_normal(TRUE, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003588 if (!dangerous)
3589 --ex_normal_busy;
3590
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003591 msg_scroll |= save_msg_scroll;
3592 }
3593 }
3594 }
3595}
3596
3597/*
3598 * "filereadable()" function
3599 */
3600 static void
3601f_filereadable(typval_T *argvars, typval_T *rettv)
3602{
3603 int fd;
3604 char_u *p;
3605 int n;
3606
3607#ifndef O_NONBLOCK
3608# define O_NONBLOCK 0
3609#endif
3610 p = get_tv_string(&argvars[0]);
3611 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3612 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3613 {
3614 n = TRUE;
3615 close(fd);
3616 }
3617 else
3618 n = FALSE;
3619
3620 rettv->vval.v_number = n;
3621}
3622
3623/*
3624 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3625 * rights to write into.
3626 */
3627 static void
3628f_filewritable(typval_T *argvars, typval_T *rettv)
3629{
3630 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
3631}
3632
3633 static void
3634findfilendir(
3635 typval_T *argvars UNUSED,
3636 typval_T *rettv,
3637 int find_what UNUSED)
3638{
3639#ifdef FEAT_SEARCHPATH
3640 char_u *fname;
3641 char_u *fresult = NULL;
3642 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3643 char_u *p;
3644 char_u pathbuf[NUMBUFLEN];
3645 int count = 1;
3646 int first = TRUE;
3647 int error = FALSE;
3648#endif
3649
3650 rettv->vval.v_string = NULL;
3651 rettv->v_type = VAR_STRING;
3652
3653#ifdef FEAT_SEARCHPATH
3654 fname = get_tv_string(&argvars[0]);
3655
3656 if (argvars[1].v_type != VAR_UNKNOWN)
3657 {
3658 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
3659 if (p == NULL)
3660 error = TRUE;
3661 else
3662 {
3663 if (*p != NUL)
3664 path = p;
3665
3666 if (argvars[2].v_type != VAR_UNKNOWN)
3667 count = (int)get_tv_number_chk(&argvars[2], &error);
3668 }
3669 }
3670
3671 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3672 error = TRUE;
3673
3674 if (*fname != NUL && !error)
3675 {
3676 do
3677 {
3678 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3679 vim_free(fresult);
3680 fresult = find_file_in_path_option(first ? fname : NULL,
3681 first ? (int)STRLEN(fname) : 0,
3682 0, first, path,
3683 find_what,
3684 curbuf->b_ffname,
3685 find_what == FINDFILE_DIR
3686 ? (char_u *)"" : curbuf->b_p_sua);
3687 first = FALSE;
3688
3689 if (fresult != NULL && rettv->v_type == VAR_LIST)
3690 list_append_string(rettv->vval.v_list, fresult, -1);
3691
3692 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3693 }
3694
3695 if (rettv->v_type == VAR_STRING)
3696 rettv->vval.v_string = fresult;
3697#endif
3698}
3699
3700/*
3701 * "filter()" function
3702 */
3703 static void
3704f_filter(typval_T *argvars, typval_T *rettv)
3705{
3706 filter_map(argvars, rettv, FALSE);
3707}
3708
3709/*
3710 * "finddir({fname}[, {path}[, {count}]])" function
3711 */
3712 static void
3713f_finddir(typval_T *argvars, typval_T *rettv)
3714{
3715 findfilendir(argvars, rettv, FINDFILE_DIR);
3716}
3717
3718/*
3719 * "findfile({fname}[, {path}[, {count}]])" function
3720 */
3721 static void
3722f_findfile(typval_T *argvars, typval_T *rettv)
3723{
3724 findfilendir(argvars, rettv, FINDFILE_FILE);
3725}
3726
3727#ifdef FEAT_FLOAT
3728/*
3729 * "float2nr({float})" function
3730 */
3731 static void
3732f_float2nr(typval_T *argvars, typval_T *rettv)
3733{
3734 float_T f = 0.0;
3735
3736 if (get_float_arg(argvars, &f) == OK)
3737 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003738 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003739 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003740 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003741 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003742 else
3743 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003744 }
3745}
3746
3747/*
3748 * "floor({float})" function
3749 */
3750 static void
3751f_floor(typval_T *argvars, typval_T *rettv)
3752{
3753 float_T f = 0.0;
3754
3755 rettv->v_type = VAR_FLOAT;
3756 if (get_float_arg(argvars, &f) == OK)
3757 rettv->vval.v_float = floor(f);
3758 else
3759 rettv->vval.v_float = 0.0;
3760}
3761
3762/*
3763 * "fmod()" function
3764 */
3765 static void
3766f_fmod(typval_T *argvars, typval_T *rettv)
3767{
3768 float_T fx = 0.0, fy = 0.0;
3769
3770 rettv->v_type = VAR_FLOAT;
3771 if (get_float_arg(argvars, &fx) == OK
3772 && get_float_arg(&argvars[1], &fy) == OK)
3773 rettv->vval.v_float = fmod(fx, fy);
3774 else
3775 rettv->vval.v_float = 0.0;
3776}
3777#endif
3778
3779/*
3780 * "fnameescape({string})" function
3781 */
3782 static void
3783f_fnameescape(typval_T *argvars, typval_T *rettv)
3784{
3785 rettv->vval.v_string = vim_strsave_fnameescape(
3786 get_tv_string(&argvars[0]), FALSE);
3787 rettv->v_type = VAR_STRING;
3788}
3789
3790/*
3791 * "fnamemodify({fname}, {mods})" function
3792 */
3793 static void
3794f_fnamemodify(typval_T *argvars, typval_T *rettv)
3795{
3796 char_u *fname;
3797 char_u *mods;
3798 int usedlen = 0;
3799 int len;
3800 char_u *fbuf = NULL;
3801 char_u buf[NUMBUFLEN];
3802
3803 fname = get_tv_string_chk(&argvars[0]);
3804 mods = get_tv_string_buf_chk(&argvars[1], buf);
3805 if (fname == NULL || mods == NULL)
3806 fname = NULL;
3807 else
3808 {
3809 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02003810 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003811 }
3812
3813 rettv->v_type = VAR_STRING;
3814 if (fname == NULL)
3815 rettv->vval.v_string = NULL;
3816 else
3817 rettv->vval.v_string = vim_strnsave(fname, len);
3818 vim_free(fbuf);
3819}
3820
3821static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
3822
3823/*
3824 * "foldclosed()" function
3825 */
3826 static void
3827foldclosed_both(
3828 typval_T *argvars UNUSED,
3829 typval_T *rettv,
3830 int end UNUSED)
3831{
3832#ifdef FEAT_FOLDING
3833 linenr_T lnum;
3834 linenr_T first, last;
3835
3836 lnum = get_tv_lnum(argvars);
3837 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3838 {
3839 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3840 {
3841 if (end)
3842 rettv->vval.v_number = (varnumber_T)last;
3843 else
3844 rettv->vval.v_number = (varnumber_T)first;
3845 return;
3846 }
3847 }
3848#endif
3849 rettv->vval.v_number = -1;
3850}
3851
3852/*
3853 * "foldclosed()" function
3854 */
3855 static void
3856f_foldclosed(typval_T *argvars, typval_T *rettv)
3857{
3858 foldclosed_both(argvars, rettv, FALSE);
3859}
3860
3861/*
3862 * "foldclosedend()" function
3863 */
3864 static void
3865f_foldclosedend(typval_T *argvars, typval_T *rettv)
3866{
3867 foldclosed_both(argvars, rettv, TRUE);
3868}
3869
3870/*
3871 * "foldlevel()" function
3872 */
3873 static void
3874f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3875{
3876#ifdef FEAT_FOLDING
3877 linenr_T lnum;
3878
3879 lnum = get_tv_lnum(argvars);
3880 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3881 rettv->vval.v_number = foldLevel(lnum);
3882#endif
3883}
3884
3885/*
3886 * "foldtext()" function
3887 */
3888 static void
3889f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
3890{
3891#ifdef FEAT_FOLDING
3892 linenr_T foldstart;
3893 linenr_T foldend;
3894 char_u *dashes;
3895 linenr_T lnum;
3896 char_u *s;
3897 char_u *r;
3898 int len;
3899 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003900 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003901#endif
3902
3903 rettv->v_type = VAR_STRING;
3904 rettv->vval.v_string = NULL;
3905#ifdef FEAT_FOLDING
3906 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
3907 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
3908 dashes = get_vim_var_str(VV_FOLDDASHES);
3909 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
3910 && dashes != NULL)
3911 {
3912 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02003913 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003914 if (!linewhite(lnum))
3915 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003916
3917 /* Find interesting text in this line. */
3918 s = skipwhite(ml_get(lnum));
3919 /* skip C comment-start */
3920 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
3921 {
3922 s = skipwhite(s + 2);
3923 if (*skipwhite(s) == NUL
3924 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
3925 {
3926 s = skipwhite(ml_get(lnum + 1));
3927 if (*s == '*')
3928 s = skipwhite(s + 1);
3929 }
3930 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02003931 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003932 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003933 r = alloc((unsigned)(STRLEN(txt)
3934 + STRLEN(dashes) /* for %s */
3935 + 20 /* for %3ld */
3936 + STRLEN(s))); /* concatenated */
3937 if (r != NULL)
3938 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02003939 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003940 len = (int)STRLEN(r);
3941 STRCAT(r, s);
3942 /* remove 'foldmarker' and 'commentstring' */
3943 foldtext_cleanup(r + len);
3944 rettv->vval.v_string = r;
3945 }
3946 }
3947#endif
3948}
3949
3950/*
3951 * "foldtextresult(lnum)" function
3952 */
3953 static void
3954f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
3955{
3956#ifdef FEAT_FOLDING
3957 linenr_T lnum;
3958 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003959 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003960 foldinfo_T foldinfo;
3961 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003962 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003963#endif
3964
3965 rettv->v_type = VAR_STRING;
3966 rettv->vval.v_string = NULL;
3967#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003968 if (entered)
3969 return; /* reject recursive use */
3970 entered = TRUE;
3971
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003972 lnum = get_tv_lnum(argvars);
3973 /* treat illegal types and illegal string values for {lnum} the same */
3974 if (lnum < 0)
3975 lnum = 0;
3976 fold_count = foldedCount(curwin, lnum, &foldinfo);
3977 if (fold_count > 0)
3978 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01003979 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
3980 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003981 if (text == buf)
3982 text = vim_strsave(text);
3983 rettv->vval.v_string = text;
3984 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003985
3986 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003987#endif
3988}
3989
3990/*
3991 * "foreground()" function
3992 */
3993 static void
3994f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3995{
3996#ifdef FEAT_GUI
3997 if (gui.in_use)
3998 gui_mch_set_foreground();
3999#else
4000# ifdef WIN32
4001 win32_set_foreground();
4002# endif
4003#endif
4004}
4005
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004006 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004007common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004008{
4009 char_u *s;
4010 char_u *name;
4011 int use_string = FALSE;
4012 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004013 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004014
4015 if (argvars[0].v_type == VAR_FUNC)
4016 {
4017 /* function(MyFunc, [arg], dict) */
4018 s = argvars[0].vval.v_string;
4019 }
4020 else if (argvars[0].v_type == VAR_PARTIAL
4021 && argvars[0].vval.v_partial != NULL)
4022 {
4023 /* function(dict.MyFunc, [arg]) */
4024 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004025 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004026 }
4027 else
4028 {
4029 /* function('MyFunc', [arg], dict) */
4030 s = get_tv_string(&argvars[0]);
4031 use_string = TRUE;
4032 }
4033
Bram Moolenaar843b8842016-08-21 14:36:15 +02004034 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004035 {
4036 name = s;
4037 trans_name = trans_function_name(&name, FALSE,
4038 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4039 if (*name != NUL)
4040 s = NULL;
4041 }
4042
Bram Moolenaar843b8842016-08-21 14:36:15 +02004043 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4044 || (is_funcref && trans_name == NULL))
Bram Moolenaar5582ef12016-09-14 22:16:13 +02004045 EMSG2(_(e_invarg2), use_string ? get_tv_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004046 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004047 else if (trans_name != NULL && (is_funcref
4048 ? find_func(trans_name) == NULL
4049 : !translated_function_exists(trans_name)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004050 EMSG2(_("E700: Unknown function: %s"), s);
4051 else
4052 {
4053 int dict_idx = 0;
4054 int arg_idx = 0;
4055 list_T *list = NULL;
4056
4057 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4058 {
4059 char sid_buf[25];
4060 int off = *s == 's' ? 2 : 5;
4061
4062 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4063 * also be called from another script. Using trans_function_name()
4064 * would also work, but some plugins depend on the name being
4065 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004066 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004067 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
4068 if (name != NULL)
4069 {
4070 STRCPY(name, sid_buf);
4071 STRCAT(name, s + off);
4072 }
4073 }
4074 else
4075 name = vim_strsave(s);
4076
4077 if (argvars[1].v_type != VAR_UNKNOWN)
4078 {
4079 if (argvars[2].v_type != VAR_UNKNOWN)
4080 {
4081 /* function(name, [args], dict) */
4082 arg_idx = 1;
4083 dict_idx = 2;
4084 }
4085 else if (argvars[1].v_type == VAR_DICT)
4086 /* function(name, dict) */
4087 dict_idx = 1;
4088 else
4089 /* function(name, [args]) */
4090 arg_idx = 1;
4091 if (dict_idx > 0)
4092 {
4093 if (argvars[dict_idx].v_type != VAR_DICT)
4094 {
4095 EMSG(_("E922: expected a dict"));
4096 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004097 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004098 }
4099 if (argvars[dict_idx].vval.v_dict == NULL)
4100 dict_idx = 0;
4101 }
4102 if (arg_idx > 0)
4103 {
4104 if (argvars[arg_idx].v_type != VAR_LIST)
4105 {
4106 EMSG(_("E923: Second argument of function() must be a list or a dict"));
4107 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004108 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004109 }
4110 list = argvars[arg_idx].vval.v_list;
4111 if (list == NULL || list->lv_len == 0)
4112 arg_idx = 0;
4113 }
4114 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004115 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004116 {
4117 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
4118
4119 /* result is a VAR_PARTIAL */
4120 if (pt == NULL)
4121 vim_free(name);
4122 else
4123 {
4124 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4125 {
4126 listitem_T *li;
4127 int i = 0;
4128 int arg_len = 0;
4129 int lv_len = 0;
4130
4131 if (arg_pt != NULL)
4132 arg_len = arg_pt->pt_argc;
4133 if (list != NULL)
4134 lv_len = list->lv_len;
4135 pt->pt_argc = arg_len + lv_len;
4136 pt->pt_argv = (typval_T *)alloc(
4137 sizeof(typval_T) * pt->pt_argc);
4138 if (pt->pt_argv == NULL)
4139 {
4140 vim_free(pt);
4141 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004142 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004143 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004144 for (i = 0; i < arg_len; i++)
4145 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4146 if (lv_len > 0)
4147 for (li = list->lv_first; li != NULL;
4148 li = li->li_next)
4149 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004150 }
4151
4152 /* For "function(dict.func, [], dict)" and "func" is a partial
4153 * use "dict". That is backwards compatible. */
4154 if (dict_idx > 0)
4155 {
4156 /* The dict is bound explicitly, pt_auto is FALSE. */
4157 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4158 ++pt->pt_dict->dv_refcount;
4159 }
4160 else if (arg_pt != NULL)
4161 {
4162 /* If the dict was bound automatically the result is also
4163 * bound automatically. */
4164 pt->pt_dict = arg_pt->pt_dict;
4165 pt->pt_auto = arg_pt->pt_auto;
4166 if (pt->pt_dict != NULL)
4167 ++pt->pt_dict->dv_refcount;
4168 }
4169
4170 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004171 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4172 {
4173 pt->pt_func = arg_pt->pt_func;
4174 func_ptr_ref(pt->pt_func);
4175 vim_free(name);
4176 }
4177 else if (is_funcref)
4178 {
4179 pt->pt_func = find_func(trans_name);
4180 func_ptr_ref(pt->pt_func);
4181 vim_free(name);
4182 }
4183 else
4184 {
4185 pt->pt_name = name;
4186 func_ref(name);
4187 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004188 }
4189 rettv->v_type = VAR_PARTIAL;
4190 rettv->vval.v_partial = pt;
4191 }
4192 else
4193 {
4194 /* result is a VAR_FUNC */
4195 rettv->v_type = VAR_FUNC;
4196 rettv->vval.v_string = name;
4197 func_ref(name);
4198 }
4199 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004200theend:
4201 vim_free(trans_name);
4202}
4203
4204/*
4205 * "funcref()" function
4206 */
4207 static void
4208f_funcref(typval_T *argvars, typval_T *rettv)
4209{
4210 common_function(argvars, rettv, TRUE);
4211}
4212
4213/*
4214 * "function()" function
4215 */
4216 static void
4217f_function(typval_T *argvars, typval_T *rettv)
4218{
4219 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004220}
4221
4222/*
4223 * "garbagecollect()" function
4224 */
4225 static void
4226f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4227{
4228 /* This is postponed until we are back at the toplevel, because we may be
4229 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4230 want_garbage_collect = TRUE;
4231
4232 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
4233 garbage_collect_at_exit = TRUE;
4234}
4235
4236/*
4237 * "get()" function
4238 */
4239 static void
4240f_get(typval_T *argvars, typval_T *rettv)
4241{
4242 listitem_T *li;
4243 list_T *l;
4244 dictitem_T *di;
4245 dict_T *d;
4246 typval_T *tv = NULL;
4247
4248 if (argvars[0].v_type == VAR_LIST)
4249 {
4250 if ((l = argvars[0].vval.v_list) != NULL)
4251 {
4252 int error = FALSE;
4253
4254 li = list_find(l, (long)get_tv_number_chk(&argvars[1], &error));
4255 if (!error && li != NULL)
4256 tv = &li->li_tv;
4257 }
4258 }
4259 else if (argvars[0].v_type == VAR_DICT)
4260 {
4261 if ((d = argvars[0].vval.v_dict) != NULL)
4262 {
4263 di = dict_find(d, get_tv_string(&argvars[1]), -1);
4264 if (di != NULL)
4265 tv = &di->di_tv;
4266 }
4267 }
4268 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4269 {
4270 partial_T *pt;
4271 partial_T fref_pt;
4272
4273 if (argvars[0].v_type == VAR_PARTIAL)
4274 pt = argvars[0].vval.v_partial;
4275 else
4276 {
4277 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4278 fref_pt.pt_name = argvars[0].vval.v_string;
4279 pt = &fref_pt;
4280 }
4281
4282 if (pt != NULL)
4283 {
4284 char_u *what = get_tv_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004285 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004286
4287 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4288 {
4289 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004290 n = partial_name(pt);
4291 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004292 rettv->vval.v_string = NULL;
4293 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004294 {
4295 rettv->vval.v_string = vim_strsave(n);
4296 if (rettv->v_type == VAR_FUNC)
4297 func_ref(rettv->vval.v_string);
4298 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004299 }
4300 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004301 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004302 else if (STRCMP(what, "args") == 0)
4303 {
4304 rettv->v_type = VAR_LIST;
4305 if (rettv_list_alloc(rettv) == OK)
4306 {
4307 int i;
4308
4309 for (i = 0; i < pt->pt_argc; ++i)
4310 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4311 }
4312 }
4313 else
4314 EMSG2(_(e_invarg2), what);
4315 return;
4316 }
4317 }
4318 else
4319 EMSG2(_(e_listdictarg), "get()");
4320
4321 if (tv == NULL)
4322 {
4323 if (argvars[2].v_type != VAR_UNKNOWN)
4324 copy_tv(&argvars[2], rettv);
4325 }
4326 else
4327 copy_tv(tv, rettv);
4328}
4329
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004330#ifdef FEAT_SIGNS
4331/*
4332 * Returns information about signs placed in a buffer as list of dicts.
4333 */
4334 static void
4335get_buffer_signs(buf_T *buf, list_T *l)
4336{
4337 signlist_T *sign;
4338
4339 for (sign = buf->b_signlist; sign; sign = sign->next)
4340 {
4341 dict_T *d = dict_alloc();
4342
4343 if (d != NULL)
4344 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02004345 dict_add_number(d, "id", sign->id);
4346 dict_add_number(d, "lnum", sign->lnum);
4347 dict_add_string(d, "name", sign_typenr2name(sign->typenr));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004348
4349 list_append_dict(l, d);
4350 }
4351 }
4352}
4353#endif
4354
4355/*
4356 * Returns buffer options, variables and other attributes in a dictionary.
4357 */
4358 static dict_T *
4359get_buffer_info(buf_T *buf)
4360{
4361 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004362 tabpage_T *tp;
4363 win_T *wp;
4364 list_T *windows;
4365
4366 dict = dict_alloc();
4367 if (dict == NULL)
4368 return NULL;
4369
Bram Moolenaare0be1672018-07-08 16:50:37 +02004370 dict_add_number(dict, "bufnr", buf->b_fnum);
4371 dict_add_string(dict, "name", buf->b_ffname);
4372 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4373 : buflist_findlnum(buf));
4374 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4375 dict_add_number(dict, "listed", buf->b_p_bl);
4376 dict_add_number(dict, "changed", bufIsChanged(buf));
4377 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4378 dict_add_number(dict, "hidden",
4379 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004380
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004381 /* Get a reference to buffer variables */
4382 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004383
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004384 /* List of windows displaying this buffer */
4385 windows = list_alloc();
4386 if (windows != NULL)
4387 {
4388 FOR_ALL_TAB_WINDOWS(tp, wp)
4389 if (wp->w_buffer == buf)
4390 list_append_number(windows, (varnumber_T)wp->w_id);
4391 dict_add_list(dict, "windows", windows);
4392 }
4393
4394#ifdef FEAT_SIGNS
4395 if (buf->b_signlist != NULL)
4396 {
4397 /* List of signs placed in this buffer */
4398 list_T *signs = list_alloc();
4399 if (signs != NULL)
4400 {
4401 get_buffer_signs(buf, signs);
4402 dict_add_list(dict, "signs", signs);
4403 }
4404 }
4405#endif
4406
4407 return dict;
4408}
4409
4410/*
4411 * "getbufinfo()" function
4412 */
4413 static void
4414f_getbufinfo(typval_T *argvars, typval_T *rettv)
4415{
4416 buf_T *buf = NULL;
4417 buf_T *argbuf = NULL;
4418 dict_T *d;
4419 int filtered = FALSE;
4420 int sel_buflisted = FALSE;
4421 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004422 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004423
4424 if (rettv_list_alloc(rettv) != OK)
4425 return;
4426
4427 /* List of all the buffers or selected buffers */
4428 if (argvars[0].v_type == VAR_DICT)
4429 {
4430 dict_T *sel_d = argvars[0].vval.v_dict;
4431
4432 if (sel_d != NULL)
4433 {
4434 dictitem_T *di;
4435
4436 filtered = TRUE;
4437
4438 di = dict_find(sel_d, (char_u *)"buflisted", -1);
4439 if (di != NULL && get_tv_number(&di->di_tv))
4440 sel_buflisted = TRUE;
4441
4442 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
4443 if (di != NULL && get_tv_number(&di->di_tv))
4444 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004445
4446 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
4447 if (di != NULL && get_tv_number(&di->di_tv))
4448 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004449 }
4450 }
4451 else if (argvars[0].v_type != VAR_UNKNOWN)
4452 {
4453 /* Information about one buffer. Argument specifies the buffer */
4454 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4455 ++emsg_off;
4456 argbuf = get_buf_tv(&argvars[0], FALSE);
4457 --emsg_off;
4458 if (argbuf == NULL)
4459 return;
4460 }
4461
4462 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004463 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004464 {
4465 if (argbuf != NULL && argbuf != buf)
4466 continue;
4467 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004468 || (sel_buflisted && !buf->b_p_bl)
4469 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004470 continue;
4471
4472 d = get_buffer_info(buf);
4473 if (d != NULL)
4474 list_append_dict(rettv->vval.v_list, d);
4475 if (argbuf != NULL)
4476 return;
4477 }
4478}
4479
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004480static void get_buffer_lines(buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv);
4481
4482/*
4483 * Get line or list of lines from buffer "buf" into "rettv".
4484 * Return a range (from start to end) of lines in rettv from the specified
4485 * buffer.
4486 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4487 */
4488 static void
4489get_buffer_lines(
4490 buf_T *buf,
4491 linenr_T start,
4492 linenr_T end,
4493 int retlist,
4494 typval_T *rettv)
4495{
4496 char_u *p;
4497
4498 rettv->v_type = VAR_STRING;
4499 rettv->vval.v_string = NULL;
4500 if (retlist && rettv_list_alloc(rettv) == FAIL)
4501 return;
4502
4503 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4504 return;
4505
4506 if (!retlist)
4507 {
4508 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4509 p = ml_get_buf(buf, start, FALSE);
4510 else
4511 p = (char_u *)"";
4512 rettv->vval.v_string = vim_strsave(p);
4513 }
4514 else
4515 {
4516 if (end < start)
4517 return;
4518
4519 if (start < 1)
4520 start = 1;
4521 if (end > buf->b_ml.ml_line_count)
4522 end = buf->b_ml.ml_line_count;
4523 while (start <= end)
4524 if (list_append_string(rettv->vval.v_list,
4525 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4526 break;
4527 }
4528}
4529
4530/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004531 * "getbufline()" function
4532 */
4533 static void
4534f_getbufline(typval_T *argvars, typval_T *rettv)
4535{
4536 linenr_T lnum;
4537 linenr_T end;
4538 buf_T *buf;
4539
4540 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4541 ++emsg_off;
4542 buf = get_buf_tv(&argvars[0], FALSE);
4543 --emsg_off;
4544
4545 lnum = get_tv_lnum_buf(&argvars[1], buf);
4546 if (argvars[2].v_type == VAR_UNKNOWN)
4547 end = lnum;
4548 else
4549 end = get_tv_lnum_buf(&argvars[2], buf);
4550
4551 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4552}
4553
4554/*
4555 * "getbufvar()" function
4556 */
4557 static void
4558f_getbufvar(typval_T *argvars, typval_T *rettv)
4559{
4560 buf_T *buf;
4561 buf_T *save_curbuf;
4562 char_u *varname;
4563 dictitem_T *v;
4564 int done = FALSE;
4565
4566 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4567 varname = get_tv_string_chk(&argvars[1]);
4568 ++emsg_off;
4569 buf = get_buf_tv(&argvars[0], FALSE);
4570
4571 rettv->v_type = VAR_STRING;
4572 rettv->vval.v_string = NULL;
4573
4574 if (buf != NULL && varname != NULL)
4575 {
4576 /* set curbuf to be our buf, temporarily */
4577 save_curbuf = curbuf;
4578 curbuf = buf;
4579
Bram Moolenaar30567352016-08-27 21:25:44 +02004580 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004581 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004582 if (varname[1] == NUL)
4583 {
4584 /* get all buffer-local options in a dict */
4585 dict_T *opts = get_winbuf_options(TRUE);
4586
4587 if (opts != NULL)
4588 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004589 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004590 done = TRUE;
4591 }
4592 }
4593 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4594 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004595 done = TRUE;
4596 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004597 else
4598 {
4599 /* Look up the variable. */
4600 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4601 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4602 'b', varname, FALSE);
4603 if (v != NULL)
4604 {
4605 copy_tv(&v->di_tv, rettv);
4606 done = TRUE;
4607 }
4608 }
4609
4610 /* restore previous notion of curbuf */
4611 curbuf = save_curbuf;
4612 }
4613
4614 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4615 /* use the default value */
4616 copy_tv(&argvars[2], rettv);
4617
4618 --emsg_off;
4619}
4620
4621/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004622 * "getchangelist()" function
4623 */
4624 static void
4625f_getchangelist(typval_T *argvars, typval_T *rettv)
4626{
4627#ifdef FEAT_JUMPLIST
4628 buf_T *buf;
4629 int i;
4630 list_T *l;
4631 dict_T *d;
4632#endif
4633
4634 if (rettv_list_alloc(rettv) != OK)
4635 return;
4636
4637#ifdef FEAT_JUMPLIST
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004638 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4639 ++emsg_off;
4640 buf = get_buf_tv(&argvars[0], FALSE);
4641 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004642 if (buf == NULL)
4643 return;
4644
4645 l = list_alloc();
4646 if (l == NULL)
4647 return;
4648
4649 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4650 return;
4651 /*
4652 * The current window change list index tracks only the position in the
4653 * current buffer change list. For other buffers, use the change list
4654 * length as the current index.
4655 */
4656 list_append_number(rettv->vval.v_list,
4657 (varnumber_T)((buf == curwin->w_buffer)
4658 ? curwin->w_changelistidx : buf->b_changelistlen));
4659
4660 for (i = 0; i < buf->b_changelistlen; ++i)
4661 {
4662 if (buf->b_changelist[i].lnum == 0)
4663 continue;
4664 if ((d = dict_alloc()) == NULL)
4665 return;
4666 if (list_append_dict(l, d) == FAIL)
4667 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004668 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4669 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004670# ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +02004671 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004672# endif
4673 }
4674#endif
4675}
4676/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004677 * "getchar()" function
4678 */
4679 static void
4680f_getchar(typval_T *argvars, typval_T *rettv)
4681{
4682 varnumber_T n;
4683 int error = FALSE;
4684
Bram Moolenaar84d93902018-09-11 20:10:20 +02004685#ifdef MESSAGE_QUEUE
4686 // vpeekc() used to check for messages, but that caused problems, invoking
4687 // a callback where it was not expected. Some plugins use getchar(1) in a
4688 // loop to await a message, therefore make sure we check for messages here.
4689 parse_queued_messages();
4690#endif
4691
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004692 /* Position the cursor. Needed after a message that ends in a space. */
4693 windgoto(msg_row, msg_col);
4694
4695 ++no_mapping;
4696 ++allow_keys;
4697 for (;;)
4698 {
4699 if (argvars[0].v_type == VAR_UNKNOWN)
4700 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004701 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004702 else if (get_tv_number_chk(&argvars[0], &error) == 1)
4703 /* getchar(1): only check if char avail */
4704 n = vpeekc_any();
4705 else if (error || vpeekc_any() == NUL)
4706 /* illegal argument or getchar(0) and no char avail: return zero */
4707 n = 0;
4708 else
4709 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004710 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004711
4712 if (n == K_IGNORE)
4713 continue;
4714 break;
4715 }
4716 --no_mapping;
4717 --allow_keys;
4718
4719 set_vim_var_nr(VV_MOUSE_WIN, 0);
4720 set_vim_var_nr(VV_MOUSE_WINID, 0);
4721 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4722 set_vim_var_nr(VV_MOUSE_COL, 0);
4723
4724 rettv->vval.v_number = n;
4725 if (IS_SPECIAL(n) || mod_mask != 0)
4726 {
4727 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4728 int i = 0;
4729
4730 /* Turn a special key into three bytes, plus modifier. */
4731 if (mod_mask != 0)
4732 {
4733 temp[i++] = K_SPECIAL;
4734 temp[i++] = KS_MODIFIER;
4735 temp[i++] = mod_mask;
4736 }
4737 if (IS_SPECIAL(n))
4738 {
4739 temp[i++] = K_SPECIAL;
4740 temp[i++] = K_SECOND(n);
4741 temp[i++] = K_THIRD(n);
4742 }
4743#ifdef FEAT_MBYTE
4744 else if (has_mbyte)
4745 i += (*mb_char2bytes)(n, temp + i);
4746#endif
4747 else
4748 temp[i++] = n;
4749 temp[i++] = NUL;
4750 rettv->v_type = VAR_STRING;
4751 rettv->vval.v_string = vim_strsave(temp);
4752
4753#ifdef FEAT_MOUSE
4754 if (is_mouse_key(n))
4755 {
4756 int row = mouse_row;
4757 int col = mouse_col;
4758 win_T *win;
4759 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004760 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004761 int winnr = 1;
4762
4763 if (row >= 0 && col >= 0)
4764 {
4765 /* Find the window at the mouse coordinates and compute the
4766 * text position. */
4767 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004768 if (win == NULL)
4769 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004770 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004771 for (wp = firstwin; wp != win; wp = wp->w_next)
4772 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004773 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4774 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4775 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4776 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4777 }
4778 }
4779#endif
4780 }
4781}
4782
4783/*
4784 * "getcharmod()" function
4785 */
4786 static void
4787f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4788{
4789 rettv->vval.v_number = mod_mask;
4790}
4791
4792/*
4793 * "getcharsearch()" function
4794 */
4795 static void
4796f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4797{
4798 if (rettv_dict_alloc(rettv) != FAIL)
4799 {
4800 dict_T *dict = rettv->vval.v_dict;
4801
Bram Moolenaare0be1672018-07-08 16:50:37 +02004802 dict_add_string(dict, "char", last_csearch());
4803 dict_add_number(dict, "forward", last_csearch_forward());
4804 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004805 }
4806}
4807
4808/*
4809 * "getcmdline()" function
4810 */
4811 static void
4812f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4813{
4814 rettv->v_type = VAR_STRING;
4815 rettv->vval.v_string = get_cmdline_str();
4816}
4817
4818/*
4819 * "getcmdpos()" function
4820 */
4821 static void
4822f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4823{
4824 rettv->vval.v_number = get_cmdline_pos() + 1;
4825}
4826
4827/*
4828 * "getcmdtype()" function
4829 */
4830 static void
4831f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4832{
4833 rettv->v_type = VAR_STRING;
4834 rettv->vval.v_string = alloc(2);
4835 if (rettv->vval.v_string != NULL)
4836 {
4837 rettv->vval.v_string[0] = get_cmdline_type();
4838 rettv->vval.v_string[1] = NUL;
4839 }
4840}
4841
4842/*
4843 * "getcmdwintype()" function
4844 */
4845 static void
4846f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4847{
4848 rettv->v_type = VAR_STRING;
4849 rettv->vval.v_string = NULL;
4850#ifdef FEAT_CMDWIN
4851 rettv->vval.v_string = alloc(2);
4852 if (rettv->vval.v_string != NULL)
4853 {
4854 rettv->vval.v_string[0] = cmdwin_type;
4855 rettv->vval.v_string[1] = NUL;
4856 }
4857#endif
4858}
4859
4860#if defined(FEAT_CMDL_COMPL)
4861/*
4862 * "getcompletion()" function
4863 */
4864 static void
4865f_getcompletion(typval_T *argvars, typval_T *rettv)
4866{
4867 char_u *pat;
4868 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004869 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004870 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
4871 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004872
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004873 if (argvars[2].v_type != VAR_UNKNOWN)
4874 filtered = get_tv_number_chk(&argvars[2], NULL);
4875
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004876 if (p_wic)
4877 options |= WILD_ICASE;
4878
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004879 /* For filtered results, 'wildignore' is used */
4880 if (!filtered)
4881 options |= WILD_KEEP_ALL;
4882
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004883 ExpandInit(&xpc);
4884 xpc.xp_pattern = get_tv_string(&argvars[0]);
4885 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4886 xpc.xp_context = cmdcomplete_str_to_type(get_tv_string(&argvars[1]));
4887 if (xpc.xp_context == EXPAND_NOTHING)
4888 {
4889 if (argvars[1].v_type == VAR_STRING)
4890 EMSG2(_(e_invarg2), argvars[1].vval.v_string);
4891 else
4892 EMSG(_(e_invarg));
4893 return;
4894 }
4895
4896# if defined(FEAT_MENU)
4897 if (xpc.xp_context == EXPAND_MENUS)
4898 {
4899 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4900 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4901 }
4902# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02004903#ifdef FEAT_CSCOPE
4904 if (xpc.xp_context == EXPAND_CSCOPE)
4905 {
4906 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4907 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4908 }
4909#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02004910#ifdef FEAT_SIGNS
4911 if (xpc.xp_context == EXPAND_SIGN)
4912 {
4913 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4914 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4915 }
4916#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004917
4918 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4919 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
4920 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004921 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004922
4923 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4924
4925 for (i = 0; i < xpc.xp_numfiles; i++)
4926 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4927 }
4928 vim_free(pat);
4929 ExpandCleanup(&xpc);
4930}
4931#endif
4932
4933/*
4934 * "getcwd()" function
4935 */
4936 static void
4937f_getcwd(typval_T *argvars, typval_T *rettv)
4938{
4939 win_T *wp = NULL;
4940 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01004941 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004942
4943 rettv->v_type = VAR_STRING;
4944 rettv->vval.v_string = NULL;
4945
Bram Moolenaar54591292018-02-09 20:53:59 +01004946 if (argvars[0].v_type == VAR_NUMBER && argvars[0].vval.v_number == -1)
4947 global = TRUE;
4948 else
4949 wp = find_tabwin(&argvars[0], &argvars[1]);
4950
4951 if (wp != NULL && wp->w_localdir != NULL)
4952 rettv->vval.v_string = vim_strsave(wp->w_localdir);
4953 else if (wp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004954 {
Bram Moolenaar54591292018-02-09 20:53:59 +01004955 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004956 rettv->vval.v_string = vim_strsave(globaldir);
4957 else
4958 {
4959 cwd = alloc(MAXPATHL);
4960 if (cwd != NULL)
4961 {
4962 if (mch_dirname(cwd, MAXPATHL) != FAIL)
4963 rettv->vval.v_string = vim_strsave(cwd);
4964 vim_free(cwd);
4965 }
4966 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004967 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02004968#ifdef BACKSLASH_IN_FILENAME
4969 if (rettv->vval.v_string != NULL)
4970 slash_adjust(rettv->vval.v_string);
4971#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004972}
4973
4974/*
4975 * "getfontname()" function
4976 */
4977 static void
4978f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
4979{
4980 rettv->v_type = VAR_STRING;
4981 rettv->vval.v_string = NULL;
4982#ifdef FEAT_GUI
4983 if (gui.in_use)
4984 {
4985 GuiFont font;
4986 char_u *name = NULL;
4987
4988 if (argvars[0].v_type == VAR_UNKNOWN)
4989 {
4990 /* Get the "Normal" font. Either the name saved by
4991 * hl_set_font_name() or from the font ID. */
4992 font = gui.norm_font;
4993 name = hl_get_font_name();
4994 }
4995 else
4996 {
4997 name = get_tv_string(&argvars[0]);
4998 if (STRCMP(name, "*") == 0) /* don't use font dialog */
4999 return;
5000 font = gui_mch_get_font(name, FALSE);
5001 if (font == NOFONT)
5002 return; /* Invalid font name, return empty string. */
5003 }
5004 rettv->vval.v_string = gui_mch_get_fontname(font, name);
5005 if (argvars[0].v_type != VAR_UNKNOWN)
5006 gui_mch_free_font(font);
5007 }
5008#endif
5009}
5010
5011/*
5012 * "getfperm({fname})" function
5013 */
5014 static void
5015f_getfperm(typval_T *argvars, typval_T *rettv)
5016{
5017 char_u *fname;
5018 stat_T st;
5019 char_u *perm = NULL;
5020 char_u flags[] = "rwx";
5021 int i;
5022
5023 fname = get_tv_string(&argvars[0]);
5024
5025 rettv->v_type = VAR_STRING;
5026 if (mch_stat((char *)fname, &st) >= 0)
5027 {
5028 perm = vim_strsave((char_u *)"---------");
5029 if (perm != NULL)
5030 {
5031 for (i = 0; i < 9; i++)
5032 {
5033 if (st.st_mode & (1 << (8 - i)))
5034 perm[i] = flags[i % 3];
5035 }
5036 }
5037 }
5038 rettv->vval.v_string = perm;
5039}
5040
5041/*
5042 * "getfsize({fname})" function
5043 */
5044 static void
5045f_getfsize(typval_T *argvars, typval_T *rettv)
5046{
5047 char_u *fname;
5048 stat_T st;
5049
5050 fname = get_tv_string(&argvars[0]);
5051
5052 rettv->v_type = VAR_NUMBER;
5053
5054 if (mch_stat((char *)fname, &st) >= 0)
5055 {
5056 if (mch_isdir(fname))
5057 rettv->vval.v_number = 0;
5058 else
5059 {
5060 rettv->vval.v_number = (varnumber_T)st.st_size;
5061
5062 /* non-perfect check for overflow */
5063 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5064 rettv->vval.v_number = -2;
5065 }
5066 }
5067 else
5068 rettv->vval.v_number = -1;
5069}
5070
5071/*
5072 * "getftime({fname})" function
5073 */
5074 static void
5075f_getftime(typval_T *argvars, typval_T *rettv)
5076{
5077 char_u *fname;
5078 stat_T st;
5079
5080 fname = get_tv_string(&argvars[0]);
5081
5082 if (mch_stat((char *)fname, &st) >= 0)
5083 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5084 else
5085 rettv->vval.v_number = -1;
5086}
5087
5088/*
5089 * "getftype({fname})" function
5090 */
5091 static void
5092f_getftype(typval_T *argvars, typval_T *rettv)
5093{
5094 char_u *fname;
5095 stat_T st;
5096 char_u *type = NULL;
5097 char *t;
5098
5099 fname = get_tv_string(&argvars[0]);
5100
5101 rettv->v_type = VAR_STRING;
5102 if (mch_lstat((char *)fname, &st) >= 0)
5103 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005104 if (S_ISREG(st.st_mode))
5105 t = "file";
5106 else if (S_ISDIR(st.st_mode))
5107 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005108 else if (S_ISLNK(st.st_mode))
5109 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005110 else if (S_ISBLK(st.st_mode))
5111 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005112 else if (S_ISCHR(st.st_mode))
5113 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005114 else if (S_ISFIFO(st.st_mode))
5115 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005116 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005117 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005118 else
5119 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005120 type = vim_strsave((char_u *)t);
5121 }
5122 rettv->vval.v_string = type;
5123}
5124
5125/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005126 * "getjumplist()" function
5127 */
5128 static void
5129f_getjumplist(typval_T *argvars, typval_T *rettv)
5130{
5131#ifdef FEAT_JUMPLIST
5132 win_T *wp;
5133 int i;
5134 list_T *l;
5135 dict_T *d;
5136#endif
5137
5138 if (rettv_list_alloc(rettv) != OK)
5139 return;
5140
5141#ifdef FEAT_JUMPLIST
5142 wp = find_tabwin(&argvars[0], &argvars[1]);
5143 if (wp == NULL)
5144 return;
5145
5146 l = list_alloc();
5147 if (l == NULL)
5148 return;
5149
5150 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5151 return;
5152 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5153
Bram Moolenaar48679742018-02-13 13:33:29 +01005154 cleanup_jumplist(wp, TRUE);
5155
Bram Moolenaar4f505882018-02-10 21:06:32 +01005156 for (i = 0; i < wp->w_jumplistlen; ++i)
5157 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005158 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5159 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005160 if ((d = dict_alloc()) == NULL)
5161 return;
5162 if (list_append_dict(l, d) == FAIL)
5163 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005164 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5165 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005166# ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +02005167 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005168# endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005169 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005170 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005171 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005172 }
5173#endif
5174}
5175
5176/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005177 * "getline(lnum, [end])" function
5178 */
5179 static void
5180f_getline(typval_T *argvars, typval_T *rettv)
5181{
5182 linenr_T lnum;
5183 linenr_T end;
5184 int retlist;
5185
5186 lnum = get_tv_lnum(argvars);
5187 if (argvars[1].v_type == VAR_UNKNOWN)
5188 {
5189 end = 0;
5190 retlist = FALSE;
5191 }
5192 else
5193 {
5194 end = get_tv_lnum(&argvars[1]);
5195 retlist = TRUE;
5196 }
5197
5198 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5199}
5200
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005201#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005202 static void
5203get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5204{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005205 if (what_arg->v_type == VAR_UNKNOWN)
5206 {
5207 if (rettv_list_alloc(rettv) == OK)
5208 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005209 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005210 }
5211 else
5212 {
5213 if (rettv_dict_alloc(rettv) == OK)
5214 if (is_qf || (wp != NULL))
5215 {
5216 if (what_arg->v_type == VAR_DICT)
5217 {
5218 dict_T *d = what_arg->vval.v_dict;
5219
5220 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005221 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005222 }
5223 else
5224 EMSG(_(e_dictreq));
5225 }
5226 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005227}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005228#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005229
5230/*
5231 * "getloclist()" function
5232 */
5233 static void
5234f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5235{
5236#ifdef FEAT_QUICKFIX
5237 win_T *wp;
5238
5239 wp = find_win_by_nr(&argvars[0], NULL);
5240 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5241#endif
5242}
5243
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005244/*
5245 * "getmatches()" function
5246 */
5247 static void
5248f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5249{
5250#ifdef FEAT_SEARCH_EXTRA
5251 dict_T *dict;
5252 matchitem_T *cur = curwin->w_match_head;
5253 int i;
5254
5255 if (rettv_list_alloc(rettv) == OK)
5256 {
5257 while (cur != NULL)
5258 {
5259 dict = dict_alloc();
5260 if (dict == NULL)
5261 return;
5262 if (cur->match.regprog == NULL)
5263 {
5264 /* match added with matchaddpos() */
5265 for (i = 0; i < MAXPOSMATCH; ++i)
5266 {
5267 llpos_T *llpos;
5268 char buf[6];
5269 list_T *l;
5270
5271 llpos = &cur->pos.pos[i];
5272 if (llpos->lnum == 0)
5273 break;
5274 l = list_alloc();
5275 if (l == NULL)
5276 break;
5277 list_append_number(l, (varnumber_T)llpos->lnum);
5278 if (llpos->col > 0)
5279 {
5280 list_append_number(l, (varnumber_T)llpos->col);
5281 list_append_number(l, (varnumber_T)llpos->len);
5282 }
5283 sprintf(buf, "pos%d", i + 1);
5284 dict_add_list(dict, buf, l);
5285 }
5286 }
5287 else
5288 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02005289 dict_add_string(dict, "pattern", cur->pattern);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005290 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02005291 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
5292 dict_add_number(dict, "priority", (long)cur->priority);
5293 dict_add_number(dict, "id", (long)cur->id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005294# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
5295 if (cur->conceal_char)
5296 {
5297 char_u buf[MB_MAXBYTES + 1];
5298
5299 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005300 dict_add_string(dict, "conceal", (char_u *)&buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005301 }
5302# endif
5303 list_append_dict(rettv->vval.v_list, dict);
5304 cur = cur->next;
5305 }
5306 }
5307#endif
5308}
5309
5310/*
5311 * "getpid()" function
5312 */
5313 static void
5314f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5315{
5316 rettv->vval.v_number = mch_get_pid();
5317}
5318
5319 static void
5320getpos_both(
5321 typval_T *argvars,
5322 typval_T *rettv,
5323 int getcurpos)
5324{
5325 pos_T *fp;
5326 list_T *l;
5327 int fnum = -1;
5328
5329 if (rettv_list_alloc(rettv) == OK)
5330 {
5331 l = rettv->vval.v_list;
5332 if (getcurpos)
5333 fp = &curwin->w_cursor;
5334 else
5335 fp = var2fpos(&argvars[0], TRUE, &fnum);
5336 if (fnum != -1)
5337 list_append_number(l, (varnumber_T)fnum);
5338 else
5339 list_append_number(l, (varnumber_T)0);
5340 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5341 : (varnumber_T)0);
5342 list_append_number(l, (fp != NULL)
5343 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5344 : (varnumber_T)0);
5345 list_append_number(l,
5346#ifdef FEAT_VIRTUALEDIT
5347 (fp != NULL) ? (varnumber_T)fp->coladd :
5348#endif
5349 (varnumber_T)0);
5350 if (getcurpos)
5351 {
5352 update_curswant();
5353 list_append_number(l, curwin->w_curswant == MAXCOL ?
5354 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
5355 }
5356 }
5357 else
5358 rettv->vval.v_number = FALSE;
5359}
5360
5361
5362/*
5363 * "getcurpos()" function
5364 */
5365 static void
5366f_getcurpos(typval_T *argvars, typval_T *rettv)
5367{
5368 getpos_both(argvars, rettv, TRUE);
5369}
5370
5371/*
5372 * "getpos(string)" function
5373 */
5374 static void
5375f_getpos(typval_T *argvars, typval_T *rettv)
5376{
5377 getpos_both(argvars, rettv, FALSE);
5378}
5379
5380/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005381 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005382 */
5383 static void
5384f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5385{
5386#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005387 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005388#endif
5389}
5390
5391/*
5392 * "getreg()" function
5393 */
5394 static void
5395f_getreg(typval_T *argvars, typval_T *rettv)
5396{
5397 char_u *strregname;
5398 int regname;
5399 int arg2 = FALSE;
5400 int return_list = FALSE;
5401 int error = FALSE;
5402
5403 if (argvars[0].v_type != VAR_UNKNOWN)
5404 {
5405 strregname = get_tv_string_chk(&argvars[0]);
5406 error = strregname == NULL;
5407 if (argvars[1].v_type != VAR_UNKNOWN)
5408 {
5409 arg2 = (int)get_tv_number_chk(&argvars[1], &error);
5410 if (!error && argvars[2].v_type != VAR_UNKNOWN)
5411 return_list = (int)get_tv_number_chk(&argvars[2], &error);
5412 }
5413 }
5414 else
5415 strregname = get_vim_var_str(VV_REG);
5416
5417 if (error)
5418 return;
5419
5420 regname = (strregname == NULL ? '"' : *strregname);
5421 if (regname == 0)
5422 regname = '"';
5423
5424 if (return_list)
5425 {
5426 rettv->v_type = VAR_LIST;
5427 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5428 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5429 if (rettv->vval.v_list == NULL)
5430 (void)rettv_list_alloc(rettv);
5431 else
5432 ++rettv->vval.v_list->lv_refcount;
5433 }
5434 else
5435 {
5436 rettv->v_type = VAR_STRING;
5437 rettv->vval.v_string = get_reg_contents(regname,
5438 arg2 ? GREG_EXPR_SRC : 0);
5439 }
5440}
5441
5442/*
5443 * "getregtype()" function
5444 */
5445 static void
5446f_getregtype(typval_T *argvars, typval_T *rettv)
5447{
5448 char_u *strregname;
5449 int regname;
5450 char_u buf[NUMBUFLEN + 2];
5451 long reglen = 0;
5452
5453 if (argvars[0].v_type != VAR_UNKNOWN)
5454 {
5455 strregname = get_tv_string_chk(&argvars[0]);
5456 if (strregname == NULL) /* type error; errmsg already given */
5457 {
5458 rettv->v_type = VAR_STRING;
5459 rettv->vval.v_string = NULL;
5460 return;
5461 }
5462 }
5463 else
5464 /* Default to v:register */
5465 strregname = get_vim_var_str(VV_REG);
5466
5467 regname = (strregname == NULL ? '"' : *strregname);
5468 if (regname == 0)
5469 regname = '"';
5470
5471 buf[0] = NUL;
5472 buf[1] = NUL;
5473 switch (get_reg_type(regname, &reglen))
5474 {
5475 case MLINE: buf[0] = 'V'; break;
5476 case MCHAR: buf[0] = 'v'; break;
5477 case MBLOCK:
5478 buf[0] = Ctrl_V;
5479 sprintf((char *)buf + 1, "%ld", reglen + 1);
5480 break;
5481 }
5482 rettv->v_type = VAR_STRING;
5483 rettv->vval.v_string = vim_strsave(buf);
5484}
5485
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005486/*
5487 * Returns information (variables, options, etc.) about a tab page
5488 * as a dictionary.
5489 */
5490 static dict_T *
5491get_tabpage_info(tabpage_T *tp, int tp_idx)
5492{
5493 win_T *wp;
5494 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005495 list_T *l;
5496
5497 dict = dict_alloc();
5498 if (dict == NULL)
5499 return NULL;
5500
Bram Moolenaare0be1672018-07-08 16:50:37 +02005501 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005502
5503 l = list_alloc();
5504 if (l != NULL)
5505 {
5506 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5507 wp; wp = wp->w_next)
5508 list_append_number(l, (varnumber_T)wp->w_id);
5509 dict_add_list(dict, "windows", l);
5510 }
5511
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005512 /* Make a reference to tabpage variables */
5513 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005514
5515 return dict;
5516}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005517
5518/*
5519 * "gettabinfo()" function
5520 */
5521 static void
5522f_gettabinfo(typval_T *argvars, typval_T *rettv)
5523{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005524 tabpage_T *tp, *tparg = NULL;
5525 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005526 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005527
5528 if (rettv_list_alloc(rettv) != OK)
5529 return;
5530
5531 if (argvars[0].v_type != VAR_UNKNOWN)
5532 {
5533 /* Information about one tab page */
5534 tparg = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5535 if (tparg == NULL)
5536 return;
5537 }
5538
5539 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005540 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005541 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005542 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005543 if (tparg != NULL && tp != tparg)
5544 continue;
5545 d = get_tabpage_info(tp, tpnr);
5546 if (d != NULL)
5547 list_append_dict(rettv->vval.v_list, d);
5548 if (tparg != NULL)
5549 return;
5550 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005551}
5552
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005553/*
5554 * "gettabvar()" function
5555 */
5556 static void
5557f_gettabvar(typval_T *argvars, typval_T *rettv)
5558{
5559 win_T *oldcurwin;
5560 tabpage_T *tp, *oldtabpage;
5561 dictitem_T *v;
5562 char_u *varname;
5563 int done = FALSE;
5564
5565 rettv->v_type = VAR_STRING;
5566 rettv->vval.v_string = NULL;
5567
5568 varname = get_tv_string_chk(&argvars[1]);
5569 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5570 if (tp != NULL && varname != NULL)
5571 {
5572 /* Set tp to be our tabpage, temporarily. Also set the window to the
5573 * first window in the tabpage, otherwise the window is not valid. */
5574 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005575 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5576 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005577 {
5578 /* look up the variable */
5579 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5580 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5581 if (v != NULL)
5582 {
5583 copy_tv(&v->di_tv, rettv);
5584 done = TRUE;
5585 }
5586 }
5587
5588 /* restore previous notion of curwin */
5589 restore_win(oldcurwin, oldtabpage, TRUE);
5590 }
5591
5592 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5593 copy_tv(&argvars[2], rettv);
5594}
5595
5596/*
5597 * "gettabwinvar()" function
5598 */
5599 static void
5600f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5601{
5602 getwinvar(argvars, rettv, 1);
5603}
5604
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005605/*
5606 * Returns information about a window as a dictionary.
5607 */
5608 static dict_T *
5609get_win_info(win_T *wp, short tpnr, short winnr)
5610{
5611 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005612
5613 dict = dict_alloc();
5614 if (dict == NULL)
5615 return NULL;
5616
Bram Moolenaare0be1672018-07-08 16:50:37 +02005617 dict_add_number(dict, "tabnr", tpnr);
5618 dict_add_number(dict, "winnr", winnr);
5619 dict_add_number(dict, "winid", wp->w_id);
5620 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005621 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005622#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02005623 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005624#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005625 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005626 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005627 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005628
Bram Moolenaar69905d12017-08-13 18:14:47 +02005629#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02005630 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02005631#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005632#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02005633 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
5634 dict_add_number(dict, "loclist",
5635 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02005636#endif
5637
Bram Moolenaar30567352016-08-27 21:25:44 +02005638 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005639 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005640
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005641 return dict;
5642}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005643
5644/*
5645 * "getwininfo()" function
5646 */
5647 static void
5648f_getwininfo(typval_T *argvars, typval_T *rettv)
5649{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005650 tabpage_T *tp;
5651 win_T *wp = NULL, *wparg = NULL;
5652 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005653 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005654
5655 if (rettv_list_alloc(rettv) != OK)
5656 return;
5657
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005658 if (argvars[0].v_type != VAR_UNKNOWN)
5659 {
5660 wparg = win_id2wp(argvars);
5661 if (wparg == NULL)
5662 return;
5663 }
5664
5665 /* Collect information about either all the windows across all the tab
5666 * pages or one particular window.
5667 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005668 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005669 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005670 tabnr++;
5671 winnr = 0;
5672 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005673 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005674 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005675 if (wparg != NULL && wp != wparg)
5676 continue;
5677 d = get_win_info(wp, tabnr, winnr);
5678 if (d != NULL)
5679 list_append_dict(rettv->vval.v_list, d);
5680 if (wparg != NULL)
5681 /* found information about a specific window */
5682 return;
5683 }
5684 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005685}
5686
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005687/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005688 * "win_findbuf()" function
5689 */
5690 static void
5691f_win_findbuf(typval_T *argvars, typval_T *rettv)
5692{
5693 if (rettv_list_alloc(rettv) != FAIL)
5694 win_findbuf(argvars, rettv->vval.v_list);
5695}
5696
5697/*
5698 * "win_getid()" function
5699 */
5700 static void
5701f_win_getid(typval_T *argvars, typval_T *rettv)
5702{
5703 rettv->vval.v_number = win_getid(argvars);
5704}
5705
5706/*
5707 * "win_gotoid()" function
5708 */
5709 static void
5710f_win_gotoid(typval_T *argvars, typval_T *rettv)
5711{
5712 rettv->vval.v_number = win_gotoid(argvars);
5713}
5714
5715/*
5716 * "win_id2tabwin()" function
5717 */
5718 static void
5719f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5720{
5721 if (rettv_list_alloc(rettv) != FAIL)
5722 win_id2tabwin(argvars, rettv->vval.v_list);
5723}
5724
5725/*
5726 * "win_id2win()" function
5727 */
5728 static void
5729f_win_id2win(typval_T *argvars, typval_T *rettv)
5730{
5731 rettv->vval.v_number = win_id2win(argvars);
5732}
5733
5734/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005735 * "win_screenpos()" function
5736 */
5737 static void
5738f_win_screenpos(typval_T *argvars, typval_T *rettv)
5739{
5740 win_T *wp;
5741
5742 if (rettv_list_alloc(rettv) == FAIL)
5743 return;
5744
5745 wp = find_win_by_nr(&argvars[0], NULL);
5746 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5747 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5748}
5749
5750/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005751 * "getwinpos({timeout})" function
5752 */
5753 static void
5754f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
5755{
5756 int x = -1;
5757 int y = -1;
5758
5759 if (rettv_list_alloc(rettv) == FAIL)
5760 return;
5761#ifdef FEAT_GUI
5762 if (gui.in_use)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01005763 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005764# if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5765 else
5766# endif
5767#endif
5768#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5769 {
5770 varnumber_T timeout = 100;
5771
5772 if (argvars[0].v_type != VAR_UNKNOWN)
5773 timeout = get_tv_number(&argvars[0]);
5774 term_get_winpos(&x, &y, timeout);
5775 }
5776#endif
5777 list_append_number(rettv->vval.v_list, (varnumber_T)x);
5778 list_append_number(rettv->vval.v_list, (varnumber_T)y);
5779}
5780
5781
5782/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005783 * "getwinposx()" function
5784 */
5785 static void
5786f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5787{
5788 rettv->vval.v_number = -1;
5789#ifdef FEAT_GUI
5790 if (gui.in_use)
5791 {
5792 int x, y;
5793
5794 if (gui_mch_get_winpos(&x, &y) == OK)
5795 rettv->vval.v_number = x;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005796 return;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005797 }
5798#endif
5799#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5800 {
5801 int x, y;
5802
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005803 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005804 rettv->vval.v_number = x;
5805 }
5806#endif
5807}
5808
5809/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005810 * "getwinposy()" function
5811 */
5812 static void
5813f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5814{
5815 rettv->vval.v_number = -1;
5816#ifdef FEAT_GUI
5817 if (gui.in_use)
5818 {
5819 int x, y;
5820
5821 if (gui_mch_get_winpos(&x, &y) == OK)
5822 rettv->vval.v_number = y;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005823 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005824 }
5825#endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005826#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5827 {
5828 int x, y;
5829
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005830 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005831 rettv->vval.v_number = y;
5832 }
5833#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005834}
5835
5836/*
5837 * "getwinvar()" function
5838 */
5839 static void
5840f_getwinvar(typval_T *argvars, typval_T *rettv)
5841{
5842 getwinvar(argvars, rettv, 0);
5843}
5844
5845/*
5846 * "glob()" function
5847 */
5848 static void
5849f_glob(typval_T *argvars, typval_T *rettv)
5850{
5851 int options = WILD_SILENT|WILD_USE_NL;
5852 expand_T xpc;
5853 int error = FALSE;
5854
5855 /* When the optional second argument is non-zero, don't remove matches
5856 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5857 rettv->v_type = VAR_STRING;
5858 if (argvars[1].v_type != VAR_UNKNOWN)
5859 {
5860 if (get_tv_number_chk(&argvars[1], &error))
5861 options |= WILD_KEEP_ALL;
5862 if (argvars[2].v_type != VAR_UNKNOWN)
5863 {
5864 if (get_tv_number_chk(&argvars[2], &error))
5865 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005866 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005867 }
5868 if (argvars[3].v_type != VAR_UNKNOWN
5869 && get_tv_number_chk(&argvars[3], &error))
5870 options |= WILD_ALLLINKS;
5871 }
5872 }
5873 if (!error)
5874 {
5875 ExpandInit(&xpc);
5876 xpc.xp_context = EXPAND_FILES;
5877 if (p_wic)
5878 options += WILD_ICASE;
5879 if (rettv->v_type == VAR_STRING)
5880 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
5881 NULL, options, WILD_ALL);
5882 else if (rettv_list_alloc(rettv) != FAIL)
5883 {
5884 int i;
5885
5886 ExpandOne(&xpc, get_tv_string(&argvars[0]),
5887 NULL, options, WILD_ALL_KEEP);
5888 for (i = 0; i < xpc.xp_numfiles; i++)
5889 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5890
5891 ExpandCleanup(&xpc);
5892 }
5893 }
5894 else
5895 rettv->vval.v_string = NULL;
5896}
5897
5898/*
5899 * "globpath()" function
5900 */
5901 static void
5902f_globpath(typval_T *argvars, typval_T *rettv)
5903{
5904 int flags = 0;
5905 char_u buf1[NUMBUFLEN];
5906 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
5907 int error = FALSE;
5908 garray_T ga;
5909 int i;
5910
5911 /* When the optional second argument is non-zero, don't remove matches
5912 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5913 rettv->v_type = VAR_STRING;
5914 if (argvars[2].v_type != VAR_UNKNOWN)
5915 {
5916 if (get_tv_number_chk(&argvars[2], &error))
5917 flags |= WILD_KEEP_ALL;
5918 if (argvars[3].v_type != VAR_UNKNOWN)
5919 {
5920 if (get_tv_number_chk(&argvars[3], &error))
5921 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005922 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005923 }
5924 if (argvars[4].v_type != VAR_UNKNOWN
5925 && get_tv_number_chk(&argvars[4], &error))
5926 flags |= WILD_ALLLINKS;
5927 }
5928 }
5929 if (file != NULL && !error)
5930 {
5931 ga_init2(&ga, (int)sizeof(char_u *), 10);
5932 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
5933 if (rettv->v_type == VAR_STRING)
5934 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
5935 else if (rettv_list_alloc(rettv) != FAIL)
5936 for (i = 0; i < ga.ga_len; ++i)
5937 list_append_string(rettv->vval.v_list,
5938 ((char_u **)(ga.ga_data))[i], -1);
5939 ga_clear_strings(&ga);
5940 }
5941 else
5942 rettv->vval.v_string = NULL;
5943}
5944
5945/*
5946 * "glob2regpat()" function
5947 */
5948 static void
5949f_glob2regpat(typval_T *argvars, typval_T *rettv)
5950{
5951 char_u *pat = get_tv_string_chk(&argvars[0]);
5952
5953 rettv->v_type = VAR_STRING;
5954 rettv->vval.v_string = (pat == NULL)
5955 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
5956}
5957
5958/* for VIM_VERSION_ defines */
5959#include "version.h"
5960
5961/*
5962 * "has()" function
5963 */
5964 static void
5965f_has(typval_T *argvars, typval_T *rettv)
5966{
5967 int i;
5968 char_u *name;
5969 int n = FALSE;
5970 static char *(has_list[]) =
5971 {
5972#ifdef AMIGA
5973 "amiga",
5974# ifdef FEAT_ARP
5975 "arp",
5976# endif
5977#endif
5978#ifdef __BEOS__
5979 "beos",
5980#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02005981#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01005982 "mac", /* Mac OS X (and, once, Mac OS Classic) */
5983 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02005984# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01005985 "macunix", /* Mac OS X, with the darwin feature */
5986 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02005987# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005988#endif
5989#ifdef __QNX__
5990 "qnx",
5991#endif
5992#ifdef UNIX
5993 "unix",
5994#endif
5995#ifdef VMS
5996 "vms",
5997#endif
5998#ifdef WIN32
5999 "win32",
6000#endif
6001#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
6002 "win32unix",
6003#endif
6004#if defined(WIN64) || defined(_WIN64)
6005 "win64",
6006#endif
6007#ifdef EBCDIC
6008 "ebcdic",
6009#endif
6010#ifndef CASE_INSENSITIVE_FILENAME
6011 "fname_case",
6012#endif
6013#ifdef HAVE_ACL
6014 "acl",
6015#endif
6016#ifdef FEAT_ARABIC
6017 "arabic",
6018#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006019 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006020#ifdef FEAT_AUTOCHDIR
6021 "autochdir",
6022#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006023#ifdef FEAT_AUTOSERVERNAME
6024 "autoservername",
6025#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006026#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006027 "balloon_eval",
6028# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
6029 "balloon_multiline",
6030# endif
6031#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006032#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006033 "balloon_eval_term",
6034#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006035#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6036 "builtin_terms",
6037# ifdef ALL_BUILTIN_TCAPS
6038 "all_builtin_terms",
6039# endif
6040#endif
6041#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
6042 || defined(FEAT_GUI_W32) \
6043 || defined(FEAT_GUI_MOTIF))
6044 "browsefilter",
6045#endif
6046#ifdef FEAT_BYTEOFF
6047 "byte_offset",
6048#endif
6049#ifdef FEAT_JOB_CHANNEL
6050 "channel",
6051#endif
6052#ifdef FEAT_CINDENT
6053 "cindent",
6054#endif
6055#ifdef FEAT_CLIENTSERVER
6056 "clientserver",
6057#endif
6058#ifdef FEAT_CLIPBOARD
6059 "clipboard",
6060#endif
6061#ifdef FEAT_CMDL_COMPL
6062 "cmdline_compl",
6063#endif
6064#ifdef FEAT_CMDHIST
6065 "cmdline_hist",
6066#endif
6067#ifdef FEAT_COMMENTS
6068 "comments",
6069#endif
6070#ifdef FEAT_CONCEAL
6071 "conceal",
6072#endif
6073#ifdef FEAT_CRYPT
6074 "cryptv",
6075 "crypt-blowfish",
6076 "crypt-blowfish2",
6077#endif
6078#ifdef FEAT_CSCOPE
6079 "cscope",
6080#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006081 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006082#ifdef CURSOR_SHAPE
6083 "cursorshape",
6084#endif
6085#ifdef DEBUG
6086 "debug",
6087#endif
6088#ifdef FEAT_CON_DIALOG
6089 "dialog_con",
6090#endif
6091#ifdef FEAT_GUI_DIALOG
6092 "dialog_gui",
6093#endif
6094#ifdef FEAT_DIFF
6095 "diff",
6096#endif
6097#ifdef FEAT_DIGRAPHS
6098 "digraphs",
6099#endif
6100#ifdef FEAT_DIRECTX
6101 "directx",
6102#endif
6103#ifdef FEAT_DND
6104 "dnd",
6105#endif
6106#ifdef FEAT_EMACS_TAGS
6107 "emacs_tags",
6108#endif
6109 "eval", /* always present, of course! */
6110 "ex_extra", /* graduated feature */
6111#ifdef FEAT_SEARCH_EXTRA
6112 "extra_search",
6113#endif
6114#ifdef FEAT_FKMAP
6115 "farsi",
6116#endif
6117#ifdef FEAT_SEARCHPATH
6118 "file_in_path",
6119#endif
6120#ifdef FEAT_FILTERPIPE
6121 "filterpipe",
6122#endif
6123#ifdef FEAT_FIND_ID
6124 "find_in_path",
6125#endif
6126#ifdef FEAT_FLOAT
6127 "float",
6128#endif
6129#ifdef FEAT_FOLDING
6130 "folding",
6131#endif
6132#ifdef FEAT_FOOTER
6133 "footer",
6134#endif
6135#if !defined(USE_SYSTEM) && defined(UNIX)
6136 "fork",
6137#endif
6138#ifdef FEAT_GETTEXT
6139 "gettext",
6140#endif
6141#ifdef FEAT_GUI
6142 "gui",
6143#endif
6144#ifdef FEAT_GUI_ATHENA
6145# ifdef FEAT_GUI_NEXTAW
6146 "gui_neXtaw",
6147# else
6148 "gui_athena",
6149# endif
6150#endif
6151#ifdef FEAT_GUI_GTK
6152 "gui_gtk",
6153# ifdef USE_GTK3
6154 "gui_gtk3",
6155# else
6156 "gui_gtk2",
6157# endif
6158#endif
6159#ifdef FEAT_GUI_GNOME
6160 "gui_gnome",
6161#endif
6162#ifdef FEAT_GUI_MAC
6163 "gui_mac",
6164#endif
6165#ifdef FEAT_GUI_MOTIF
6166 "gui_motif",
6167#endif
6168#ifdef FEAT_GUI_PHOTON
6169 "gui_photon",
6170#endif
6171#ifdef FEAT_GUI_W32
6172 "gui_win32",
6173#endif
6174#ifdef FEAT_HANGULIN
6175 "hangul_input",
6176#endif
6177#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6178 "iconv",
6179#endif
6180#ifdef FEAT_INS_EXPAND
6181 "insert_expand",
6182#endif
6183#ifdef FEAT_JOB_CHANNEL
6184 "job",
6185#endif
6186#ifdef FEAT_JUMPLIST
6187 "jumplist",
6188#endif
6189#ifdef FEAT_KEYMAP
6190 "keymap",
6191#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006192 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006193#ifdef FEAT_LANGMAP
6194 "langmap",
6195#endif
6196#ifdef FEAT_LIBCALL
6197 "libcall",
6198#endif
6199#ifdef FEAT_LINEBREAK
6200 "linebreak",
6201#endif
6202#ifdef FEAT_LISP
6203 "lispindent",
6204#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006205 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006206#ifdef FEAT_LOCALMAP
6207 "localmap",
6208#endif
6209#ifdef FEAT_LUA
6210# ifndef DYNAMIC_LUA
6211 "lua",
6212# endif
6213#endif
6214#ifdef FEAT_MENU
6215 "menu",
6216#endif
6217#ifdef FEAT_SESSION
6218 "mksession",
6219#endif
6220#ifdef FEAT_MODIFY_FNAME
6221 "modify_fname",
6222#endif
6223#ifdef FEAT_MOUSE
6224 "mouse",
6225#endif
6226#ifdef FEAT_MOUSESHAPE
6227 "mouseshape",
6228#endif
6229#if defined(UNIX) || defined(VMS)
6230# ifdef FEAT_MOUSE_DEC
6231 "mouse_dec",
6232# endif
6233# ifdef FEAT_MOUSE_GPM
6234 "mouse_gpm",
6235# endif
6236# ifdef FEAT_MOUSE_JSB
6237 "mouse_jsbterm",
6238# endif
6239# ifdef FEAT_MOUSE_NET
6240 "mouse_netterm",
6241# endif
6242# ifdef FEAT_MOUSE_PTERM
6243 "mouse_pterm",
6244# endif
6245# ifdef FEAT_MOUSE_SGR
6246 "mouse_sgr",
6247# endif
6248# ifdef FEAT_SYSMOUSE
6249 "mouse_sysmouse",
6250# endif
6251# ifdef FEAT_MOUSE_URXVT
6252 "mouse_urxvt",
6253# endif
6254# ifdef FEAT_MOUSE_XTERM
6255 "mouse_xterm",
6256# endif
6257#endif
6258#ifdef FEAT_MBYTE
6259 "multi_byte",
6260#endif
6261#ifdef FEAT_MBYTE_IME
6262 "multi_byte_ime",
6263#endif
6264#ifdef FEAT_MULTI_LANG
6265 "multi_lang",
6266#endif
6267#ifdef FEAT_MZSCHEME
6268#ifndef DYNAMIC_MZSCHEME
6269 "mzscheme",
6270#endif
6271#endif
6272#ifdef FEAT_NUM64
6273 "num64",
6274#endif
6275#ifdef FEAT_OLE
6276 "ole",
6277#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006278#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006279 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006280#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006281#ifdef FEAT_PATH_EXTRA
6282 "path_extra",
6283#endif
6284#ifdef FEAT_PERL
6285#ifndef DYNAMIC_PERL
6286 "perl",
6287#endif
6288#endif
6289#ifdef FEAT_PERSISTENT_UNDO
6290 "persistent_undo",
6291#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006292#if defined(FEAT_PYTHON)
6293 "python_compiled",
6294# if defined(DYNAMIC_PYTHON)
6295 "python_dynamic",
6296# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006297 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006298 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006299# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006300#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006301#if defined(FEAT_PYTHON3)
6302 "python3_compiled",
6303# if defined(DYNAMIC_PYTHON3)
6304 "python3_dynamic",
6305# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006306 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006307 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006308# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006309#endif
6310#ifdef FEAT_POSTSCRIPT
6311 "postscript",
6312#endif
6313#ifdef FEAT_PRINTER
6314 "printer",
6315#endif
6316#ifdef FEAT_PROFILE
6317 "profile",
6318#endif
6319#ifdef FEAT_RELTIME
6320 "reltime",
6321#endif
6322#ifdef FEAT_QUICKFIX
6323 "quickfix",
6324#endif
6325#ifdef FEAT_RIGHTLEFT
6326 "rightleft",
6327#endif
6328#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6329 "ruby",
6330#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006331 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006332#ifdef FEAT_CMDL_INFO
6333 "showcmd",
6334 "cmdline_info",
6335#endif
6336#ifdef FEAT_SIGNS
6337 "signs",
6338#endif
6339#ifdef FEAT_SMARTINDENT
6340 "smartindent",
6341#endif
6342#ifdef STARTUPTIME
6343 "startuptime",
6344#endif
6345#ifdef FEAT_STL_OPT
6346 "statusline",
6347#endif
6348#ifdef FEAT_SUN_WORKSHOP
6349 "sun_workshop",
6350#endif
6351#ifdef FEAT_NETBEANS_INTG
6352 "netbeans_intg",
6353#endif
6354#ifdef FEAT_SPELL
6355 "spell",
6356#endif
6357#ifdef FEAT_SYN_HL
6358 "syntax",
6359#endif
6360#if defined(USE_SYSTEM) || !defined(UNIX)
6361 "system",
6362#endif
6363#ifdef FEAT_TAG_BINS
6364 "tag_binary",
6365#endif
6366#ifdef FEAT_TAG_OLDSTATIC
6367 "tag_old_static",
6368#endif
6369#ifdef FEAT_TAG_ANYWHITE
6370 "tag_any_white",
6371#endif
6372#ifdef FEAT_TCL
6373# ifndef DYNAMIC_TCL
6374 "tcl",
6375# endif
6376#endif
6377#ifdef FEAT_TERMGUICOLORS
6378 "termguicolors",
6379#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006380#if defined(FEAT_TERMINAL) && !defined(WIN3264)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006381 "terminal",
6382#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006383#ifdef TERMINFO
6384 "terminfo",
6385#endif
6386#ifdef FEAT_TERMRESPONSE
6387 "termresponse",
6388#endif
6389#ifdef FEAT_TEXTOBJ
6390 "textobjects",
6391#endif
6392#ifdef HAVE_TGETENT
6393 "tgetent",
6394#endif
6395#ifdef FEAT_TIMERS
6396 "timers",
6397#endif
6398#ifdef FEAT_TITLE
6399 "title",
6400#endif
6401#ifdef FEAT_TOOLBAR
6402 "toolbar",
6403#endif
6404#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6405 "unnamedplus",
6406#endif
6407#ifdef FEAT_USR_CMDS
6408 "user-commands", /* was accidentally included in 5.4 */
6409 "user_commands",
6410#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006411#ifdef FEAT_VARTABS
6412 "vartabs",
6413#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006414#ifdef FEAT_VIMINFO
6415 "viminfo",
6416#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006417 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006418#ifdef FEAT_VIRTUALEDIT
6419 "virtualedit",
6420#endif
6421 "visual",
6422#ifdef FEAT_VISUALEXTRA
6423 "visualextra",
6424#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006425 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006426#ifdef FEAT_VTP
6427 "vtp",
6428#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006429#ifdef FEAT_WILDIGN
6430 "wildignore",
6431#endif
6432#ifdef FEAT_WILDMENU
6433 "wildmenu",
6434#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006435 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006436#ifdef FEAT_WAK
6437 "winaltkeys",
6438#endif
6439#ifdef FEAT_WRITEBACKUP
6440 "writebackup",
6441#endif
6442#ifdef FEAT_XIM
6443 "xim",
6444#endif
6445#ifdef FEAT_XFONTSET
6446 "xfontset",
6447#endif
6448#ifdef FEAT_XPM_W32
6449 "xpm",
6450 "xpm_w32", /* for backward compatibility */
6451#else
6452# if defined(HAVE_XPM)
6453 "xpm",
6454# endif
6455#endif
6456#ifdef USE_XSMP
6457 "xsmp",
6458#endif
6459#ifdef USE_XSMP_INTERACT
6460 "xsmp_interact",
6461#endif
6462#ifdef FEAT_XCLIPBOARD
6463 "xterm_clipboard",
6464#endif
6465#ifdef FEAT_XTERM_SAVE
6466 "xterm_save",
6467#endif
6468#if defined(UNIX) && defined(FEAT_X11)
6469 "X11",
6470#endif
6471 NULL
6472 };
6473
6474 name = get_tv_string(&argvars[0]);
6475 for (i = 0; has_list[i] != NULL; ++i)
6476 if (STRICMP(name, has_list[i]) == 0)
6477 {
6478 n = TRUE;
6479 break;
6480 }
6481
6482 if (n == FALSE)
6483 {
6484 if (STRNICMP(name, "patch", 5) == 0)
6485 {
6486 if (name[5] == '-'
6487 && STRLEN(name) >= 11
6488 && vim_isdigit(name[6])
6489 && vim_isdigit(name[8])
6490 && vim_isdigit(name[10]))
6491 {
6492 int major = atoi((char *)name + 6);
6493 int minor = atoi((char *)name + 8);
6494
6495 /* Expect "patch-9.9.01234". */
6496 n = (major < VIM_VERSION_MAJOR
6497 || (major == VIM_VERSION_MAJOR
6498 && (minor < VIM_VERSION_MINOR
6499 || (minor == VIM_VERSION_MINOR
6500 && has_patch(atoi((char *)name + 10))))));
6501 }
6502 else
6503 n = has_patch(atoi((char *)name + 5));
6504 }
6505 else if (STRICMP(name, "vim_starting") == 0)
6506 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006507 else if (STRICMP(name, "ttyin") == 0)
6508 n = mch_input_isatty();
6509 else if (STRICMP(name, "ttyout") == 0)
6510 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006511#ifdef FEAT_MBYTE
6512 else if (STRICMP(name, "multi_byte_encoding") == 0)
6513 n = has_mbyte;
6514#endif
6515#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
6516 else if (STRICMP(name, "balloon_multiline") == 0)
6517 n = multiline_balloon_available();
6518#endif
6519#ifdef DYNAMIC_TCL
6520 else if (STRICMP(name, "tcl") == 0)
6521 n = tcl_enabled(FALSE);
6522#endif
6523#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6524 else if (STRICMP(name, "iconv") == 0)
6525 n = iconv_enabled(FALSE);
6526#endif
6527#ifdef DYNAMIC_LUA
6528 else if (STRICMP(name, "lua") == 0)
6529 n = lua_enabled(FALSE);
6530#endif
6531#ifdef DYNAMIC_MZSCHEME
6532 else if (STRICMP(name, "mzscheme") == 0)
6533 n = mzscheme_enabled(FALSE);
6534#endif
6535#ifdef DYNAMIC_RUBY
6536 else if (STRICMP(name, "ruby") == 0)
6537 n = ruby_enabled(FALSE);
6538#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006539#ifdef DYNAMIC_PYTHON
6540 else if (STRICMP(name, "python") == 0)
6541 n = python_enabled(FALSE);
6542#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006543#ifdef DYNAMIC_PYTHON3
6544 else if (STRICMP(name, "python3") == 0)
6545 n = python3_enabled(FALSE);
6546#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006547#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6548 else if (STRICMP(name, "pythonx") == 0)
6549 {
6550# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6551 if (p_pyx == 0)
6552 n = python3_enabled(FALSE) || python_enabled(FALSE);
6553 else if (p_pyx == 3)
6554 n = python3_enabled(FALSE);
6555 else if (p_pyx == 2)
6556 n = python_enabled(FALSE);
6557# elif defined(DYNAMIC_PYTHON)
6558 n = python_enabled(FALSE);
6559# elif defined(DYNAMIC_PYTHON3)
6560 n = python3_enabled(FALSE);
6561# endif
6562 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006563#endif
6564#ifdef DYNAMIC_PERL
6565 else if (STRICMP(name, "perl") == 0)
6566 n = perl_enabled(FALSE);
6567#endif
6568#ifdef FEAT_GUI
6569 else if (STRICMP(name, "gui_running") == 0)
6570 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006571# ifdef FEAT_BROWSE
6572 else if (STRICMP(name, "browse") == 0)
6573 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6574# endif
6575#endif
6576#ifdef FEAT_SYN_HL
6577 else if (STRICMP(name, "syntax_items") == 0)
6578 n = syntax_present(curwin);
6579#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006580#ifdef FEAT_VTP
6581 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02006582 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006583#endif
6584#ifdef FEAT_NETBEANS_INTG
6585 else if (STRICMP(name, "netbeans_enabled") == 0)
6586 n = netbeans_active();
6587#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006588#if defined(FEAT_TERMINAL) && defined(WIN3264)
6589 else if (STRICMP(name, "terminal") == 0)
6590 n = terminal_enabled();
6591#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006592 }
6593
6594 rettv->vval.v_number = n;
6595}
6596
6597/*
6598 * "has_key()" function
6599 */
6600 static void
6601f_has_key(typval_T *argvars, typval_T *rettv)
6602{
6603 if (argvars[0].v_type != VAR_DICT)
6604 {
6605 EMSG(_(e_dictreq));
6606 return;
6607 }
6608 if (argvars[0].vval.v_dict == NULL)
6609 return;
6610
6611 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
6612 get_tv_string(&argvars[1]), -1) != NULL;
6613}
6614
6615/*
6616 * "haslocaldir()" function
6617 */
6618 static void
6619f_haslocaldir(typval_T *argvars, typval_T *rettv)
6620{
6621 win_T *wp = NULL;
6622
6623 wp = find_tabwin(&argvars[0], &argvars[1]);
6624 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6625}
6626
6627/*
6628 * "hasmapto()" function
6629 */
6630 static void
6631f_hasmapto(typval_T *argvars, typval_T *rettv)
6632{
6633 char_u *name;
6634 char_u *mode;
6635 char_u buf[NUMBUFLEN];
6636 int abbr = FALSE;
6637
6638 name = get_tv_string(&argvars[0]);
6639 if (argvars[1].v_type == VAR_UNKNOWN)
6640 mode = (char_u *)"nvo";
6641 else
6642 {
6643 mode = get_tv_string_buf(&argvars[1], buf);
6644 if (argvars[2].v_type != VAR_UNKNOWN)
6645 abbr = (int)get_tv_number(&argvars[2]);
6646 }
6647
6648 if (map_to_exists(name, mode, abbr))
6649 rettv->vval.v_number = TRUE;
6650 else
6651 rettv->vval.v_number = FALSE;
6652}
6653
6654/*
6655 * "histadd()" function
6656 */
6657 static void
6658f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6659{
6660#ifdef FEAT_CMDHIST
6661 int histype;
6662 char_u *str;
6663 char_u buf[NUMBUFLEN];
6664#endif
6665
6666 rettv->vval.v_number = FALSE;
6667 if (check_restricted() || check_secure())
6668 return;
6669#ifdef FEAT_CMDHIST
6670 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6671 histype = str != NULL ? get_histtype(str) : -1;
6672 if (histype >= 0)
6673 {
6674 str = get_tv_string_buf(&argvars[1], buf);
6675 if (*str != NUL)
6676 {
6677 init_history();
6678 add_to_history(histype, str, FALSE, NUL);
6679 rettv->vval.v_number = TRUE;
6680 return;
6681 }
6682 }
6683#endif
6684}
6685
6686/*
6687 * "histdel()" function
6688 */
6689 static void
6690f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6691{
6692#ifdef FEAT_CMDHIST
6693 int n;
6694 char_u buf[NUMBUFLEN];
6695 char_u *str;
6696
6697 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6698 if (str == NULL)
6699 n = 0;
6700 else if (argvars[1].v_type == VAR_UNKNOWN)
6701 /* only one argument: clear entire history */
6702 n = clr_history(get_histtype(str));
6703 else if (argvars[1].v_type == VAR_NUMBER)
6704 /* index given: remove that entry */
6705 n = del_history_idx(get_histtype(str),
6706 (int)get_tv_number(&argvars[1]));
6707 else
6708 /* string given: remove all matching entries */
6709 n = del_history_entry(get_histtype(str),
6710 get_tv_string_buf(&argvars[1], buf));
6711 rettv->vval.v_number = n;
6712#endif
6713}
6714
6715/*
6716 * "histget()" function
6717 */
6718 static void
6719f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6720{
6721#ifdef FEAT_CMDHIST
6722 int type;
6723 int idx;
6724 char_u *str;
6725
6726 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6727 if (str == NULL)
6728 rettv->vval.v_string = NULL;
6729 else
6730 {
6731 type = get_histtype(str);
6732 if (argvars[1].v_type == VAR_UNKNOWN)
6733 idx = get_history_idx(type);
6734 else
6735 idx = (int)get_tv_number_chk(&argvars[1], NULL);
6736 /* -1 on type error */
6737 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6738 }
6739#else
6740 rettv->vval.v_string = NULL;
6741#endif
6742 rettv->v_type = VAR_STRING;
6743}
6744
6745/*
6746 * "histnr()" function
6747 */
6748 static void
6749f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6750{
6751 int i;
6752
6753#ifdef FEAT_CMDHIST
6754 char_u *history = get_tv_string_chk(&argvars[0]);
6755
6756 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6757 if (i >= HIST_CMD && i < HIST_COUNT)
6758 i = get_history_idx(i);
6759 else
6760#endif
6761 i = -1;
6762 rettv->vval.v_number = i;
6763}
6764
6765/*
6766 * "highlightID(name)" function
6767 */
6768 static void
6769f_hlID(typval_T *argvars, typval_T *rettv)
6770{
6771 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
6772}
6773
6774/*
6775 * "highlight_exists()" function
6776 */
6777 static void
6778f_hlexists(typval_T *argvars, typval_T *rettv)
6779{
6780 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
6781}
6782
6783/*
6784 * "hostname()" function
6785 */
6786 static void
6787f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6788{
6789 char_u hostname[256];
6790
6791 mch_get_host_name(hostname, 256);
6792 rettv->v_type = VAR_STRING;
6793 rettv->vval.v_string = vim_strsave(hostname);
6794}
6795
6796/*
6797 * iconv() function
6798 */
6799 static void
6800f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6801{
6802#ifdef FEAT_MBYTE
6803 char_u buf1[NUMBUFLEN];
6804 char_u buf2[NUMBUFLEN];
6805 char_u *from, *to, *str;
6806 vimconv_T vimconv;
6807#endif
6808
6809 rettv->v_type = VAR_STRING;
6810 rettv->vval.v_string = NULL;
6811
6812#ifdef FEAT_MBYTE
6813 str = get_tv_string(&argvars[0]);
6814 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
6815 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
6816 vimconv.vc_type = CONV_NONE;
6817 convert_setup(&vimconv, from, to);
6818
6819 /* If the encodings are equal, no conversion needed. */
6820 if (vimconv.vc_type == CONV_NONE)
6821 rettv->vval.v_string = vim_strsave(str);
6822 else
6823 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6824
6825 convert_setup(&vimconv, NULL, NULL);
6826 vim_free(from);
6827 vim_free(to);
6828#endif
6829}
6830
6831/*
6832 * "indent()" function
6833 */
6834 static void
6835f_indent(typval_T *argvars, typval_T *rettv)
6836{
6837 linenr_T lnum;
6838
6839 lnum = get_tv_lnum(argvars);
6840 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6841 rettv->vval.v_number = get_indent_lnum(lnum);
6842 else
6843 rettv->vval.v_number = -1;
6844}
6845
6846/*
6847 * "index()" function
6848 */
6849 static void
6850f_index(typval_T *argvars, typval_T *rettv)
6851{
6852 list_T *l;
6853 listitem_T *item;
6854 long idx = 0;
6855 int ic = FALSE;
6856
6857 rettv->vval.v_number = -1;
6858 if (argvars[0].v_type != VAR_LIST)
6859 {
6860 EMSG(_(e_listreq));
6861 return;
6862 }
6863 l = argvars[0].vval.v_list;
6864 if (l != NULL)
6865 {
6866 item = l->lv_first;
6867 if (argvars[2].v_type != VAR_UNKNOWN)
6868 {
6869 int error = FALSE;
6870
6871 /* Start at specified item. Use the cached index that list_find()
6872 * sets, so that a negative number also works. */
6873 item = list_find(l, (long)get_tv_number_chk(&argvars[2], &error));
6874 idx = l->lv_idx;
6875 if (argvars[3].v_type != VAR_UNKNOWN)
6876 ic = (int)get_tv_number_chk(&argvars[3], &error);
6877 if (error)
6878 item = NULL;
6879 }
6880
6881 for ( ; item != NULL; item = item->li_next, ++idx)
6882 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6883 {
6884 rettv->vval.v_number = idx;
6885 break;
6886 }
6887 }
6888}
6889
6890static int inputsecret_flag = 0;
6891
6892/*
6893 * "input()" function
6894 * Also handles inputsecret() when inputsecret is set.
6895 */
6896 static void
6897f_input(typval_T *argvars, typval_T *rettv)
6898{
6899 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6900}
6901
6902/*
6903 * "inputdialog()" function
6904 */
6905 static void
6906f_inputdialog(typval_T *argvars, typval_T *rettv)
6907{
6908#if defined(FEAT_GUI_TEXTDIALOG)
6909 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
6910 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6911 {
6912 char_u *message;
6913 char_u buf[NUMBUFLEN];
6914 char_u *defstr = (char_u *)"";
6915
6916 message = get_tv_string_chk(&argvars[0]);
6917 if (argvars[1].v_type != VAR_UNKNOWN
6918 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
6919 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6920 else
6921 IObuff[0] = NUL;
6922 if (message != NULL && defstr != NULL
6923 && do_dialog(VIM_QUESTION, NULL, message,
6924 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6925 rettv->vval.v_string = vim_strsave(IObuff);
6926 else
6927 {
6928 if (message != NULL && defstr != NULL
6929 && argvars[1].v_type != VAR_UNKNOWN
6930 && argvars[2].v_type != VAR_UNKNOWN)
6931 rettv->vval.v_string = vim_strsave(
6932 get_tv_string_buf(&argvars[2], buf));
6933 else
6934 rettv->vval.v_string = NULL;
6935 }
6936 rettv->v_type = VAR_STRING;
6937 }
6938 else
6939#endif
6940 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6941}
6942
6943/*
6944 * "inputlist()" function
6945 */
6946 static void
6947f_inputlist(typval_T *argvars, typval_T *rettv)
6948{
6949 listitem_T *li;
6950 int selected;
6951 int mouse_used;
6952
6953#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02006954 /* While starting up, there is no place to enter text. When running tests
6955 * with --not-a-term we assume feedkeys() will be used. */
6956 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006957 return;
6958#endif
6959 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6960 {
6961 EMSG2(_(e_listarg), "inputlist()");
6962 return;
6963 }
6964
6965 msg_start();
6966 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
6967 lines_left = Rows; /* avoid more prompt */
6968 msg_scroll = TRUE;
6969 msg_clr_eos();
6970
6971 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
6972 {
6973 msg_puts(get_tv_string(&li->li_tv));
6974 msg_putchar('\n');
6975 }
6976
6977 /* Ask for choice. */
6978 selected = prompt_for_number(&mouse_used);
6979 if (mouse_used)
6980 selected -= lines_left;
6981
6982 rettv->vval.v_number = selected;
6983}
6984
6985
6986static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
6987
6988/*
6989 * "inputrestore()" function
6990 */
6991 static void
6992f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
6993{
6994 if (ga_userinput.ga_len > 0)
6995 {
6996 --ga_userinput.ga_len;
6997 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
6998 + ga_userinput.ga_len);
6999 /* default return is zero == OK */
7000 }
7001 else if (p_verbose > 1)
7002 {
7003 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
7004 rettv->vval.v_number = 1; /* Failed */
7005 }
7006}
7007
7008/*
7009 * "inputsave()" function
7010 */
7011 static void
7012f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7013{
7014 /* Add an entry to the stack of typeahead storage. */
7015 if (ga_grow(&ga_userinput, 1) == OK)
7016 {
7017 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7018 + ga_userinput.ga_len);
7019 ++ga_userinput.ga_len;
7020 /* default return is zero == OK */
7021 }
7022 else
7023 rettv->vval.v_number = 1; /* Failed */
7024}
7025
7026/*
7027 * "inputsecret()" function
7028 */
7029 static void
7030f_inputsecret(typval_T *argvars, typval_T *rettv)
7031{
7032 ++cmdline_star;
7033 ++inputsecret_flag;
7034 f_input(argvars, rettv);
7035 --cmdline_star;
7036 --inputsecret_flag;
7037}
7038
7039/*
7040 * "insert()" function
7041 */
7042 static void
7043f_insert(typval_T *argvars, typval_T *rettv)
7044{
7045 long before = 0;
7046 listitem_T *item;
7047 list_T *l;
7048 int error = FALSE;
7049
7050 if (argvars[0].v_type != VAR_LIST)
7051 EMSG2(_(e_listarg), "insert()");
7052 else if ((l = argvars[0].vval.v_list) != NULL
7053 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
7054 {
7055 if (argvars[2].v_type != VAR_UNKNOWN)
7056 before = (long)get_tv_number_chk(&argvars[2], &error);
7057 if (error)
7058 return; /* type error; errmsg already given */
7059
7060 if (before == l->lv_len)
7061 item = NULL;
7062 else
7063 {
7064 item = list_find(l, before);
7065 if (item == NULL)
7066 {
7067 EMSGN(_(e_listidx), before);
7068 l = NULL;
7069 }
7070 }
7071 if (l != NULL)
7072 {
7073 list_insert_tv(l, &argvars[1], item);
7074 copy_tv(&argvars[0], rettv);
7075 }
7076 }
7077}
7078
7079/*
7080 * "invert(expr)" function
7081 */
7082 static void
7083f_invert(typval_T *argvars, typval_T *rettv)
7084{
7085 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
7086}
7087
7088/*
7089 * "isdirectory()" function
7090 */
7091 static void
7092f_isdirectory(typval_T *argvars, typval_T *rettv)
7093{
7094 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
7095}
7096
7097/*
7098 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7099 * or it refers to a List or Dictionary that is locked.
7100 */
7101 static int
7102tv_islocked(typval_T *tv)
7103{
7104 return (tv->v_lock & VAR_LOCKED)
7105 || (tv->v_type == VAR_LIST
7106 && tv->vval.v_list != NULL
7107 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7108 || (tv->v_type == VAR_DICT
7109 && tv->vval.v_dict != NULL
7110 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7111}
7112
7113/*
7114 * "islocked()" function
7115 */
7116 static void
7117f_islocked(typval_T *argvars, typval_T *rettv)
7118{
7119 lval_T lv;
7120 char_u *end;
7121 dictitem_T *di;
7122
7123 rettv->vval.v_number = -1;
7124 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007125 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007126 if (end != NULL && lv.ll_name != NULL)
7127 {
7128 if (*end != NUL)
7129 EMSG(_(e_trailing));
7130 else
7131 {
7132 if (lv.ll_tv == NULL)
7133 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007134 di = find_var(lv.ll_name, NULL, TRUE);
7135 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007136 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007137 /* Consider a variable locked when:
7138 * 1. the variable itself is locked
7139 * 2. the value of the variable is locked.
7140 * 3. the List or Dict value is locked.
7141 */
7142 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7143 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007144 }
7145 }
7146 else if (lv.ll_range)
7147 EMSG(_("E786: Range not allowed"));
7148 else if (lv.ll_newkey != NULL)
7149 EMSG2(_(e_dictkey), lv.ll_newkey);
7150 else if (lv.ll_list != NULL)
7151 /* List item. */
7152 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7153 else
7154 /* Dictionary item. */
7155 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7156 }
7157 }
7158
7159 clear_lval(&lv);
7160}
7161
7162#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7163/*
7164 * "isnan()" function
7165 */
7166 static void
7167f_isnan(typval_T *argvars, typval_T *rettv)
7168{
7169 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7170 && isnan(argvars[0].vval.v_float);
7171}
7172#endif
7173
7174/*
7175 * "items(dict)" function
7176 */
7177 static void
7178f_items(typval_T *argvars, typval_T *rettv)
7179{
7180 dict_list(argvars, rettv, 2);
7181}
7182
7183#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
7184/*
7185 * Get the job from the argument.
7186 * Returns NULL if the job is invalid.
7187 */
7188 static job_T *
7189get_job_arg(typval_T *tv)
7190{
7191 job_T *job;
7192
7193 if (tv->v_type != VAR_JOB)
7194 {
7195 EMSG2(_(e_invarg2), get_tv_string(tv));
7196 return NULL;
7197 }
7198 job = tv->vval.v_job;
7199
7200 if (job == NULL)
7201 EMSG(_("E916: not a valid job"));
7202 return job;
7203}
7204
7205/*
7206 * "job_getchannel()" function
7207 */
7208 static void
7209f_job_getchannel(typval_T *argvars, typval_T *rettv)
7210{
7211 job_T *job = get_job_arg(&argvars[0]);
7212
7213 if (job != NULL)
7214 {
7215 rettv->v_type = VAR_CHANNEL;
7216 rettv->vval.v_channel = job->jv_channel;
7217 if (job->jv_channel != NULL)
7218 ++job->jv_channel->ch_refcount;
7219 }
7220}
7221
7222/*
7223 * "job_info()" function
7224 */
7225 static void
7226f_job_info(typval_T *argvars, typval_T *rettv)
7227{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007228 if (argvars[0].v_type != VAR_UNKNOWN)
7229 {
7230 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007231
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007232 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7233 job_info(job, rettv->vval.v_dict);
7234 }
7235 else if (rettv_list_alloc(rettv) == OK)
7236 job_info_all(rettv->vval.v_list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007237}
7238
7239/*
7240 * "job_setoptions()" function
7241 */
7242 static void
7243f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7244{
7245 job_T *job = get_job_arg(&argvars[0]);
7246 jobopt_T opt;
7247
7248 if (job == NULL)
7249 return;
7250 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007251 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007252 job_set_options(job, &opt);
7253 free_job_options(&opt);
7254}
7255
7256/*
7257 * "job_start()" function
7258 */
7259 static void
7260f_job_start(typval_T *argvars, typval_T *rettv)
7261{
7262 rettv->v_type = VAR_JOB;
7263 if (check_restricted() || check_secure())
7264 return;
Bram Moolenaar493359e2018-06-12 20:25:52 +02007265 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007266}
7267
7268/*
7269 * "job_status()" function
7270 */
7271 static void
7272f_job_status(typval_T *argvars, typval_T *rettv)
7273{
7274 job_T *job = get_job_arg(&argvars[0]);
7275
7276 if (job != NULL)
7277 {
7278 rettv->v_type = VAR_STRING;
7279 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7280 }
7281}
7282
7283/*
7284 * "job_stop()" function
7285 */
7286 static void
7287f_job_stop(typval_T *argvars, typval_T *rettv)
7288{
7289 job_T *job = get_job_arg(&argvars[0]);
7290
7291 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007292 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007293}
7294#endif
7295
7296/*
7297 * "join()" function
7298 */
7299 static void
7300f_join(typval_T *argvars, typval_T *rettv)
7301{
7302 garray_T ga;
7303 char_u *sep;
7304
7305 if (argvars[0].v_type != VAR_LIST)
7306 {
7307 EMSG(_(e_listreq));
7308 return;
7309 }
7310 if (argvars[0].vval.v_list == NULL)
7311 return;
7312 if (argvars[1].v_type == VAR_UNKNOWN)
7313 sep = (char_u *)" ";
7314 else
7315 sep = get_tv_string_chk(&argvars[1]);
7316
7317 rettv->v_type = VAR_STRING;
7318
7319 if (sep != NULL)
7320 {
7321 ga_init2(&ga, (int)sizeof(char), 80);
7322 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7323 ga_append(&ga, NUL);
7324 rettv->vval.v_string = (char_u *)ga.ga_data;
7325 }
7326 else
7327 rettv->vval.v_string = NULL;
7328}
7329
7330/*
7331 * "js_decode()" function
7332 */
7333 static void
7334f_js_decode(typval_T *argvars, typval_T *rettv)
7335{
7336 js_read_T reader;
7337
7338 reader.js_buf = get_tv_string(&argvars[0]);
7339 reader.js_fill = NULL;
7340 reader.js_used = 0;
7341 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
7342 EMSG(_(e_invarg));
7343}
7344
7345/*
7346 * "js_encode()" function
7347 */
7348 static void
7349f_js_encode(typval_T *argvars, typval_T *rettv)
7350{
7351 rettv->v_type = VAR_STRING;
7352 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7353}
7354
7355/*
7356 * "json_decode()" function
7357 */
7358 static void
7359f_json_decode(typval_T *argvars, typval_T *rettv)
7360{
7361 js_read_T reader;
7362
7363 reader.js_buf = get_tv_string(&argvars[0]);
7364 reader.js_fill = NULL;
7365 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007366 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007367}
7368
7369/*
7370 * "json_encode()" function
7371 */
7372 static void
7373f_json_encode(typval_T *argvars, typval_T *rettv)
7374{
7375 rettv->v_type = VAR_STRING;
7376 rettv->vval.v_string = json_encode(&argvars[0], 0);
7377}
7378
7379/*
7380 * "keys()" function
7381 */
7382 static void
7383f_keys(typval_T *argvars, typval_T *rettv)
7384{
7385 dict_list(argvars, rettv, 0);
7386}
7387
7388/*
7389 * "last_buffer_nr()" function.
7390 */
7391 static void
7392f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7393{
7394 int n = 0;
7395 buf_T *buf;
7396
Bram Moolenaar29323592016-07-24 22:04:11 +02007397 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007398 if (n < buf->b_fnum)
7399 n = buf->b_fnum;
7400
7401 rettv->vval.v_number = n;
7402}
7403
7404/*
7405 * "len()" function
7406 */
7407 static void
7408f_len(typval_T *argvars, typval_T *rettv)
7409{
7410 switch (argvars[0].v_type)
7411 {
7412 case VAR_STRING:
7413 case VAR_NUMBER:
7414 rettv->vval.v_number = (varnumber_T)STRLEN(
7415 get_tv_string(&argvars[0]));
7416 break;
7417 case VAR_LIST:
7418 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7419 break;
7420 case VAR_DICT:
7421 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7422 break;
7423 case VAR_UNKNOWN:
7424 case VAR_SPECIAL:
7425 case VAR_FLOAT:
7426 case VAR_FUNC:
7427 case VAR_PARTIAL:
7428 case VAR_JOB:
7429 case VAR_CHANNEL:
7430 EMSG(_("E701: Invalid type for len()"));
7431 break;
7432 }
7433}
7434
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007435 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007436libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007437{
7438#ifdef FEAT_LIBCALL
7439 char_u *string_in;
7440 char_u **string_result;
7441 int nr_result;
7442#endif
7443
7444 rettv->v_type = type;
7445 if (type != VAR_NUMBER)
7446 rettv->vval.v_string = NULL;
7447
7448 if (check_restricted() || check_secure())
7449 return;
7450
7451#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007452 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007453 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7454 {
7455 string_in = NULL;
7456 if (argvars[2].v_type == VAR_STRING)
7457 string_in = argvars[2].vval.v_string;
7458 if (type == VAR_NUMBER)
7459 string_result = NULL;
7460 else
7461 string_result = &rettv->vval.v_string;
7462 if (mch_libcall(argvars[0].vval.v_string,
7463 argvars[1].vval.v_string,
7464 string_in,
7465 argvars[2].vval.v_number,
7466 string_result,
7467 &nr_result) == OK
7468 && type == VAR_NUMBER)
7469 rettv->vval.v_number = nr_result;
7470 }
7471#endif
7472}
7473
7474/*
7475 * "libcall()" function
7476 */
7477 static void
7478f_libcall(typval_T *argvars, typval_T *rettv)
7479{
7480 libcall_common(argvars, rettv, VAR_STRING);
7481}
7482
7483/*
7484 * "libcallnr()" function
7485 */
7486 static void
7487f_libcallnr(typval_T *argvars, typval_T *rettv)
7488{
7489 libcall_common(argvars, rettv, VAR_NUMBER);
7490}
7491
7492/*
7493 * "line(string)" function
7494 */
7495 static void
7496f_line(typval_T *argvars, typval_T *rettv)
7497{
7498 linenr_T lnum = 0;
7499 pos_T *fp;
7500 int fnum;
7501
7502 fp = var2fpos(&argvars[0], TRUE, &fnum);
7503 if (fp != NULL)
7504 lnum = fp->lnum;
7505 rettv->vval.v_number = lnum;
7506}
7507
7508/*
7509 * "line2byte(lnum)" function
7510 */
7511 static void
7512f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7513{
7514#ifndef FEAT_BYTEOFF
7515 rettv->vval.v_number = -1;
7516#else
7517 linenr_T lnum;
7518
7519 lnum = get_tv_lnum(argvars);
7520 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7521 rettv->vval.v_number = -1;
7522 else
7523 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7524 if (rettv->vval.v_number >= 0)
7525 ++rettv->vval.v_number;
7526#endif
7527}
7528
7529/*
7530 * "lispindent(lnum)" function
7531 */
7532 static void
7533f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7534{
7535#ifdef FEAT_LISP
7536 pos_T pos;
7537 linenr_T lnum;
7538
7539 pos = curwin->w_cursor;
7540 lnum = get_tv_lnum(argvars);
7541 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7542 {
7543 curwin->w_cursor.lnum = lnum;
7544 rettv->vval.v_number = get_lisp_indent();
7545 curwin->w_cursor = pos;
7546 }
7547 else
7548#endif
7549 rettv->vval.v_number = -1;
7550}
7551
7552/*
7553 * "localtime()" function
7554 */
7555 static void
7556f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7557{
7558 rettv->vval.v_number = (varnumber_T)time(NULL);
7559}
7560
7561static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
7562
7563 static void
7564get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7565{
7566 char_u *keys;
7567 char_u *which;
7568 char_u buf[NUMBUFLEN];
7569 char_u *keys_buf = NULL;
7570 char_u *rhs;
7571 int mode;
7572 int abbr = FALSE;
7573 int get_dict = FALSE;
7574 mapblock_T *mp;
7575 int buffer_local;
7576
7577 /* return empty string for failure */
7578 rettv->v_type = VAR_STRING;
7579 rettv->vval.v_string = NULL;
7580
7581 keys = get_tv_string(&argvars[0]);
7582 if (*keys == NUL)
7583 return;
7584
7585 if (argvars[1].v_type != VAR_UNKNOWN)
7586 {
7587 which = get_tv_string_buf_chk(&argvars[1], buf);
7588 if (argvars[2].v_type != VAR_UNKNOWN)
7589 {
7590 abbr = (int)get_tv_number(&argvars[2]);
7591 if (argvars[3].v_type != VAR_UNKNOWN)
7592 get_dict = (int)get_tv_number(&argvars[3]);
7593 }
7594 }
7595 else
7596 which = (char_u *)"";
7597 if (which == NULL)
7598 return;
7599
7600 mode = get_map_mode(&which, 0);
7601
7602 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7603 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7604 vim_free(keys_buf);
7605
7606 if (!get_dict)
7607 {
7608 /* Return a string. */
7609 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02007610 {
7611 if (*rhs == NUL)
7612 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
7613 else
7614 rettv->vval.v_string = str2special_save(rhs, FALSE);
7615 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007616
7617 }
7618 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7619 {
7620 /* Return a dictionary. */
7621 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7622 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7623 dict_T *dict = rettv->vval.v_dict;
7624
Bram Moolenaare0be1672018-07-08 16:50:37 +02007625 dict_add_string(dict, "lhs", lhs);
7626 dict_add_string(dict, "rhs", mp->m_orig_str);
7627 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
7628 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
7629 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007630 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
7631 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02007632 dict_add_number(dict, "buffer", (long)buffer_local);
7633 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
7634 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007635
7636 vim_free(lhs);
7637 vim_free(mapmode);
7638 }
7639}
7640
7641#ifdef FEAT_FLOAT
7642/*
7643 * "log()" function
7644 */
7645 static void
7646f_log(typval_T *argvars, typval_T *rettv)
7647{
7648 float_T f = 0.0;
7649
7650 rettv->v_type = VAR_FLOAT;
7651 if (get_float_arg(argvars, &f) == OK)
7652 rettv->vval.v_float = log(f);
7653 else
7654 rettv->vval.v_float = 0.0;
7655}
7656
7657/*
7658 * "log10()" function
7659 */
7660 static void
7661f_log10(typval_T *argvars, typval_T *rettv)
7662{
7663 float_T f = 0.0;
7664
7665 rettv->v_type = VAR_FLOAT;
7666 if (get_float_arg(argvars, &f) == OK)
7667 rettv->vval.v_float = log10(f);
7668 else
7669 rettv->vval.v_float = 0.0;
7670}
7671#endif
7672
7673#ifdef FEAT_LUA
7674/*
7675 * "luaeval()" function
7676 */
7677 static void
7678f_luaeval(typval_T *argvars, typval_T *rettv)
7679{
7680 char_u *str;
7681 char_u buf[NUMBUFLEN];
7682
7683 str = get_tv_string_buf(&argvars[0], buf);
7684 do_luaeval(str, argvars + 1, rettv);
7685}
7686#endif
7687
7688/*
7689 * "map()" function
7690 */
7691 static void
7692f_map(typval_T *argvars, typval_T *rettv)
7693{
7694 filter_map(argvars, rettv, TRUE);
7695}
7696
7697/*
7698 * "maparg()" function
7699 */
7700 static void
7701f_maparg(typval_T *argvars, typval_T *rettv)
7702{
7703 get_maparg(argvars, rettv, TRUE);
7704}
7705
7706/*
7707 * "mapcheck()" function
7708 */
7709 static void
7710f_mapcheck(typval_T *argvars, typval_T *rettv)
7711{
7712 get_maparg(argvars, rettv, FALSE);
7713}
7714
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007715typedef enum
7716{
7717 MATCH_END, /* matchend() */
7718 MATCH_MATCH, /* match() */
7719 MATCH_STR, /* matchstr() */
7720 MATCH_LIST, /* matchlist() */
7721 MATCH_POS /* matchstrpos() */
7722} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007723
7724 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007725find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007726{
7727 char_u *str = NULL;
7728 long len = 0;
7729 char_u *expr = NULL;
7730 char_u *pat;
7731 regmatch_T regmatch;
7732 char_u patbuf[NUMBUFLEN];
7733 char_u strbuf[NUMBUFLEN];
7734 char_u *save_cpo;
7735 long start = 0;
7736 long nth = 1;
7737 colnr_T startcol = 0;
7738 int match = 0;
7739 list_T *l = NULL;
7740 listitem_T *li = NULL;
7741 long idx = 0;
7742 char_u *tofree = NULL;
7743
7744 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7745 save_cpo = p_cpo;
7746 p_cpo = (char_u *)"";
7747
7748 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007749 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007750 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007751 /* type MATCH_LIST: return empty list when there are no matches.
7752 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007753 if (rettv_list_alloc(rettv) == FAIL)
7754 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007755 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007756 && (list_append_string(rettv->vval.v_list,
7757 (char_u *)"", 0) == FAIL
7758 || list_append_number(rettv->vval.v_list,
7759 (varnumber_T)-1) == FAIL
7760 || list_append_number(rettv->vval.v_list,
7761 (varnumber_T)-1) == FAIL
7762 || list_append_number(rettv->vval.v_list,
7763 (varnumber_T)-1) == FAIL))
7764 {
7765 list_free(rettv->vval.v_list);
7766 rettv->vval.v_list = NULL;
7767 goto theend;
7768 }
7769 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007770 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007771 {
7772 rettv->v_type = VAR_STRING;
7773 rettv->vval.v_string = NULL;
7774 }
7775
7776 if (argvars[0].v_type == VAR_LIST)
7777 {
7778 if ((l = argvars[0].vval.v_list) == NULL)
7779 goto theend;
7780 li = l->lv_first;
7781 }
7782 else
7783 {
7784 expr = str = get_tv_string(&argvars[0]);
7785 len = (long)STRLEN(str);
7786 }
7787
7788 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
7789 if (pat == NULL)
7790 goto theend;
7791
7792 if (argvars[2].v_type != VAR_UNKNOWN)
7793 {
7794 int error = FALSE;
7795
7796 start = (long)get_tv_number_chk(&argvars[2], &error);
7797 if (error)
7798 goto theend;
7799 if (l != NULL)
7800 {
7801 li = list_find(l, start);
7802 if (li == NULL)
7803 goto theend;
7804 idx = l->lv_idx; /* use the cached index */
7805 }
7806 else
7807 {
7808 if (start < 0)
7809 start = 0;
7810 if (start > len)
7811 goto theend;
7812 /* When "count" argument is there ignore matches before "start",
7813 * otherwise skip part of the string. Differs when pattern is "^"
7814 * or "\<". */
7815 if (argvars[3].v_type != VAR_UNKNOWN)
7816 startcol = start;
7817 else
7818 {
7819 str += start;
7820 len -= start;
7821 }
7822 }
7823
7824 if (argvars[3].v_type != VAR_UNKNOWN)
7825 nth = (long)get_tv_number_chk(&argvars[3], &error);
7826 if (error)
7827 goto theend;
7828 }
7829
7830 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7831 if (regmatch.regprog != NULL)
7832 {
7833 regmatch.rm_ic = p_ic;
7834
7835 for (;;)
7836 {
7837 if (l != NULL)
7838 {
7839 if (li == NULL)
7840 {
7841 match = FALSE;
7842 break;
7843 }
7844 vim_free(tofree);
7845 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
7846 if (str == NULL)
7847 break;
7848 }
7849
7850 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
7851
7852 if (match && --nth <= 0)
7853 break;
7854 if (l == NULL && !match)
7855 break;
7856
7857 /* Advance to just after the match. */
7858 if (l != NULL)
7859 {
7860 li = li->li_next;
7861 ++idx;
7862 }
7863 else
7864 {
7865#ifdef FEAT_MBYTE
7866 startcol = (colnr_T)(regmatch.startp[0]
7867 + (*mb_ptr2len)(regmatch.startp[0]) - str);
7868#else
7869 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
7870#endif
7871 if (startcol > (colnr_T)len
7872 || str + startcol <= regmatch.startp[0])
7873 {
7874 match = FALSE;
7875 break;
7876 }
7877 }
7878 }
7879
7880 if (match)
7881 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007882 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007883 {
7884 listitem_T *li1 = rettv->vval.v_list->lv_first;
7885 listitem_T *li2 = li1->li_next;
7886 listitem_T *li3 = li2->li_next;
7887 listitem_T *li4 = li3->li_next;
7888
7889 vim_free(li1->li_tv.vval.v_string);
7890 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
7891 (int)(regmatch.endp[0] - regmatch.startp[0]));
7892 li3->li_tv.vval.v_number =
7893 (varnumber_T)(regmatch.startp[0] - expr);
7894 li4->li_tv.vval.v_number =
7895 (varnumber_T)(regmatch.endp[0] - expr);
7896 if (l != NULL)
7897 li2->li_tv.vval.v_number = (varnumber_T)idx;
7898 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007899 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007900 {
7901 int i;
7902
7903 /* return list with matched string and submatches */
7904 for (i = 0; i < NSUBEXP; ++i)
7905 {
7906 if (regmatch.endp[i] == NULL)
7907 {
7908 if (list_append_string(rettv->vval.v_list,
7909 (char_u *)"", 0) == FAIL)
7910 break;
7911 }
7912 else if (list_append_string(rettv->vval.v_list,
7913 regmatch.startp[i],
7914 (int)(regmatch.endp[i] - regmatch.startp[i]))
7915 == FAIL)
7916 break;
7917 }
7918 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007919 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007920 {
7921 /* return matched string */
7922 if (l != NULL)
7923 copy_tv(&li->li_tv, rettv);
7924 else
7925 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
7926 (int)(regmatch.endp[0] - regmatch.startp[0]));
7927 }
7928 else if (l != NULL)
7929 rettv->vval.v_number = idx;
7930 else
7931 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007932 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007933 rettv->vval.v_number =
7934 (varnumber_T)(regmatch.startp[0] - str);
7935 else
7936 rettv->vval.v_number =
7937 (varnumber_T)(regmatch.endp[0] - str);
7938 rettv->vval.v_number += (varnumber_T)(str - expr);
7939 }
7940 }
7941 vim_regfree(regmatch.regprog);
7942 }
7943
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007944theend:
7945 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007946 /* matchstrpos() without a list: drop the second item. */
7947 listitem_remove(rettv->vval.v_list,
7948 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007949 vim_free(tofree);
7950 p_cpo = save_cpo;
7951}
7952
7953/*
7954 * "match()" function
7955 */
7956 static void
7957f_match(typval_T *argvars, typval_T *rettv)
7958{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007959 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007960}
7961
Bram Moolenaar95e51472018-07-28 16:55:56 +02007962#ifdef FEAT_SEARCH_EXTRA
7963 static int
7964matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
7965{
7966 dictitem_T *di;
7967
7968 if (tv->v_type != VAR_DICT)
7969 {
7970 EMSG(_(e_dictreq));
7971 return FAIL;
7972 }
7973
7974 if (dict_find(tv->vval.v_dict, (char_u *)"conceal", -1) != NULL)
7975 *conceal_char = get_dict_string(tv->vval.v_dict,
7976 (char_u *)"conceal", FALSE);
7977
7978 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
7979 {
7980 *win = find_win_by_nr(&di->di_tv, NULL);
7981 if (*win == NULL)
7982 {
7983 EMSG(_("E957: Invalid window number"));
7984 return FAIL;
7985 }
7986 }
7987
7988 return OK;
7989}
7990#endif
7991
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007992/*
7993 * "matchadd()" function
7994 */
7995 static void
7996f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7997{
7998#ifdef FEAT_SEARCH_EXTRA
7999 char_u buf[NUMBUFLEN];
8000 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
8001 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
8002 int prio = 10; /* default priority */
8003 int id = -1;
8004 int error = FALSE;
8005 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008006 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008007
8008 rettv->vval.v_number = -1;
8009
8010 if (grp == NULL || pat == NULL)
8011 return;
8012 if (argvars[2].v_type != VAR_UNKNOWN)
8013 {
8014 prio = (int)get_tv_number_chk(&argvars[2], &error);
8015 if (argvars[3].v_type != VAR_UNKNOWN)
8016 {
8017 id = (int)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008018 if (argvars[4].v_type != VAR_UNKNOWN
8019 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8020 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008021 }
8022 }
8023 if (error == TRUE)
8024 return;
8025 if (id >= 1 && id <= 3)
8026 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02008027 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008028 return;
8029 }
8030
Bram Moolenaar95e51472018-07-28 16:55:56 +02008031 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008032 conceal_char);
8033#endif
8034}
8035
8036/*
8037 * "matchaddpos()" function
8038 */
8039 static void
8040f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8041{
8042#ifdef FEAT_SEARCH_EXTRA
8043 char_u buf[NUMBUFLEN];
8044 char_u *group;
8045 int prio = 10;
8046 int id = -1;
8047 int error = FALSE;
8048 list_T *l;
8049 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008050 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008051
8052 rettv->vval.v_number = -1;
8053
8054 group = get_tv_string_buf_chk(&argvars[0], buf);
8055 if (group == NULL)
8056 return;
8057
8058 if (argvars[1].v_type != VAR_LIST)
8059 {
8060 EMSG2(_(e_listarg), "matchaddpos()");
8061 return;
8062 }
8063 l = argvars[1].vval.v_list;
8064 if (l == NULL)
8065 return;
8066
8067 if (argvars[2].v_type != VAR_UNKNOWN)
8068 {
8069 prio = (int)get_tv_number_chk(&argvars[2], &error);
8070 if (argvars[3].v_type != VAR_UNKNOWN)
8071 {
8072 id = (int)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008073
8074 if (argvars[4].v_type != VAR_UNKNOWN
8075 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8076 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008077 }
8078 }
8079 if (error == TRUE)
8080 return;
8081
8082 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8083 if (id == 1 || id == 2)
8084 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02008085 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008086 return;
8087 }
8088
Bram Moolenaar95e51472018-07-28 16:55:56 +02008089 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008090 conceal_char);
8091#endif
8092}
8093
8094/*
8095 * "matcharg()" function
8096 */
8097 static void
8098f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8099{
8100 if (rettv_list_alloc(rettv) == OK)
8101 {
8102#ifdef FEAT_SEARCH_EXTRA
8103 int id = (int)get_tv_number(&argvars[0]);
8104 matchitem_T *m;
8105
8106 if (id >= 1 && id <= 3)
8107 {
8108 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8109 {
8110 list_append_string(rettv->vval.v_list,
8111 syn_id2name(m->hlg_id), -1);
8112 list_append_string(rettv->vval.v_list, m->pattern, -1);
8113 }
8114 else
8115 {
8116 list_append_string(rettv->vval.v_list, NULL, -1);
8117 list_append_string(rettv->vval.v_list, NULL, -1);
8118 }
8119 }
8120#endif
8121 }
8122}
8123
8124/*
8125 * "matchdelete()" function
8126 */
8127 static void
8128f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8129{
8130#ifdef FEAT_SEARCH_EXTRA
8131 rettv->vval.v_number = match_delete(curwin,
8132 (int)get_tv_number(&argvars[0]), TRUE);
8133#endif
8134}
8135
8136/*
8137 * "matchend()" function
8138 */
8139 static void
8140f_matchend(typval_T *argvars, typval_T *rettv)
8141{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008142 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008143}
8144
8145/*
8146 * "matchlist()" function
8147 */
8148 static void
8149f_matchlist(typval_T *argvars, typval_T *rettv)
8150{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008151 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008152}
8153
8154/*
8155 * "matchstr()" function
8156 */
8157 static void
8158f_matchstr(typval_T *argvars, typval_T *rettv)
8159{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008160 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008161}
8162
8163/*
8164 * "matchstrpos()" function
8165 */
8166 static void
8167f_matchstrpos(typval_T *argvars, typval_T *rettv)
8168{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008169 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008170}
8171
8172static void max_min(typval_T *argvars, typval_T *rettv, int domax);
8173
8174 static void
8175max_min(typval_T *argvars, typval_T *rettv, int domax)
8176{
8177 varnumber_T n = 0;
8178 varnumber_T i;
8179 int error = FALSE;
8180
8181 if (argvars[0].v_type == VAR_LIST)
8182 {
8183 list_T *l;
8184 listitem_T *li;
8185
8186 l = argvars[0].vval.v_list;
8187 if (l != NULL)
8188 {
8189 li = l->lv_first;
8190 if (li != NULL)
8191 {
8192 n = get_tv_number_chk(&li->li_tv, &error);
8193 for (;;)
8194 {
8195 li = li->li_next;
8196 if (li == NULL)
8197 break;
8198 i = get_tv_number_chk(&li->li_tv, &error);
8199 if (domax ? i > n : i < n)
8200 n = i;
8201 }
8202 }
8203 }
8204 }
8205 else if (argvars[0].v_type == VAR_DICT)
8206 {
8207 dict_T *d;
8208 int first = TRUE;
8209 hashitem_T *hi;
8210 int todo;
8211
8212 d = argvars[0].vval.v_dict;
8213 if (d != NULL)
8214 {
8215 todo = (int)d->dv_hashtab.ht_used;
8216 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8217 {
8218 if (!HASHITEM_EMPTY(hi))
8219 {
8220 --todo;
8221 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
8222 if (first)
8223 {
8224 n = i;
8225 first = FALSE;
8226 }
8227 else if (domax ? i > n : i < n)
8228 n = i;
8229 }
8230 }
8231 }
8232 }
8233 else
Bram Moolenaar26b84332016-09-04 21:42:36 +02008234 EMSG2(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008235 rettv->vval.v_number = error ? 0 : n;
8236}
8237
8238/*
8239 * "max()" function
8240 */
8241 static void
8242f_max(typval_T *argvars, typval_T *rettv)
8243{
8244 max_min(argvars, rettv, TRUE);
8245}
8246
8247/*
8248 * "min()" function
8249 */
8250 static void
8251f_min(typval_T *argvars, typval_T *rettv)
8252{
8253 max_min(argvars, rettv, FALSE);
8254}
8255
8256static int mkdir_recurse(char_u *dir, int prot);
8257
8258/*
8259 * Create the directory in which "dir" is located, and higher levels when
8260 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008261 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008262 */
8263 static int
8264mkdir_recurse(char_u *dir, int prot)
8265{
8266 char_u *p;
8267 char_u *updir;
8268 int r = FAIL;
8269
8270 /* Get end of directory name in "dir".
8271 * We're done when it's "/" or "c:/". */
8272 p = gettail_sep(dir);
8273 if (p <= get_past_head(dir))
8274 return OK;
8275
8276 /* If the directory exists we're done. Otherwise: create it.*/
8277 updir = vim_strnsave(dir, (int)(p - dir));
8278 if (updir == NULL)
8279 return FAIL;
8280 if (mch_isdir(updir))
8281 r = OK;
8282 else if (mkdir_recurse(updir, prot) == OK)
8283 r = vim_mkdir_emsg(updir, prot);
8284 vim_free(updir);
8285 return r;
8286}
8287
8288#ifdef vim_mkdir
8289/*
8290 * "mkdir()" function
8291 */
8292 static void
8293f_mkdir(typval_T *argvars, typval_T *rettv)
8294{
8295 char_u *dir;
8296 char_u buf[NUMBUFLEN];
8297 int prot = 0755;
8298
8299 rettv->vval.v_number = FAIL;
8300 if (check_restricted() || check_secure())
8301 return;
8302
8303 dir = get_tv_string_buf(&argvars[0], buf);
8304 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008305 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008306
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008307 if (*gettail(dir) == NUL)
8308 /* remove trailing slashes */
8309 *gettail_sep(dir) = NUL;
8310
8311 if (argvars[1].v_type != VAR_UNKNOWN)
8312 {
8313 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008314 {
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008315 prot = (int)get_tv_number_chk(&argvars[2], NULL);
8316 if (prot == -1)
8317 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008318 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008319 if (STRCMP(get_tv_string(&argvars[1]), "p") == 0)
8320 {
8321 if (mch_isdir(dir))
8322 {
8323 /* With the "p" flag it's OK if the dir already exists. */
8324 rettv->vval.v_number = OK;
8325 return;
8326 }
8327 mkdir_recurse(dir, prot);
8328 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008329 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008330 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008331}
8332#endif
8333
8334/*
8335 * "mode()" function
8336 */
8337 static void
8338f_mode(typval_T *argvars, typval_T *rettv)
8339{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008340 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008341
Bram Moolenaar612cc382018-07-29 15:34:26 +02008342 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008343
8344 if (time_for_testing == 93784)
8345 {
8346 /* Testing the two-character code. */
8347 buf[0] = 'x';
8348 buf[1] = '!';
8349 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008350#ifdef FEAT_TERMINAL
8351 else if (term_use_loop())
8352 buf[0] = 't';
8353#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008354 else if (VIsual_active)
8355 {
8356 if (VIsual_select)
8357 buf[0] = VIsual_mode + 's' - 'v';
8358 else
8359 buf[0] = VIsual_mode;
8360 }
8361 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8362 || State == CONFIRM)
8363 {
8364 buf[0] = 'r';
8365 if (State == ASKMORE)
8366 buf[1] = 'm';
8367 else if (State == CONFIRM)
8368 buf[1] = '?';
8369 }
8370 else if (State == EXTERNCMD)
8371 buf[0] = '!';
8372 else if (State & INSERT)
8373 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008374 if (State & VREPLACE_FLAG)
8375 {
8376 buf[0] = 'R';
8377 buf[1] = 'v';
8378 }
8379 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008380 {
8381 if (State & REPLACE_FLAG)
8382 buf[0] = 'R';
8383 else
8384 buf[0] = 'i';
8385#ifdef FEAT_INS_EXPAND
8386 if (ins_compl_active())
8387 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008388 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008389 buf[1] = 'x';
8390#endif
8391 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008392 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008393 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008394 {
8395 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008396 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008397 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008398 else if (exmode_active == EXMODE_NORMAL)
8399 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008400 }
8401 else
8402 {
8403 buf[0] = 'n';
8404 if (finish_op)
8405 buf[1] = 'o';
Bram Moolenaar612cc382018-07-29 15:34:26 +02008406 else if (restart_edit == 'I' || restart_edit == 'R'
8407 || restart_edit == 'V')
8408 {
8409 buf[1] = 'i';
8410 buf[2] = restart_edit;
8411 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008412 }
8413
8414 /* Clear out the minor mode when the argument is not a non-zero number or
8415 * non-empty string. */
8416 if (!non_zero_arg(&argvars[0]))
8417 buf[1] = NUL;
8418
8419 rettv->vval.v_string = vim_strsave(buf);
8420 rettv->v_type = VAR_STRING;
8421}
8422
8423#if defined(FEAT_MZSCHEME) || defined(PROTO)
8424/*
8425 * "mzeval()" function
8426 */
8427 static void
8428f_mzeval(typval_T *argvars, typval_T *rettv)
8429{
8430 char_u *str;
8431 char_u buf[NUMBUFLEN];
8432
8433 str = get_tv_string_buf(&argvars[0], buf);
8434 do_mzeval(str, rettv);
8435}
8436
8437 void
8438mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8439{
8440 typval_T argvars[3];
8441
8442 argvars[0].v_type = VAR_STRING;
8443 argvars[0].vval.v_string = name;
8444 copy_tv(args, &argvars[1]);
8445 argvars[2].v_type = VAR_UNKNOWN;
8446 f_call(argvars, rettv);
8447 clear_tv(&argvars[1]);
8448}
8449#endif
8450
8451/*
8452 * "nextnonblank()" function
8453 */
8454 static void
8455f_nextnonblank(typval_T *argvars, typval_T *rettv)
8456{
8457 linenr_T lnum;
8458
8459 for (lnum = get_tv_lnum(argvars); ; ++lnum)
8460 {
8461 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8462 {
8463 lnum = 0;
8464 break;
8465 }
8466 if (*skipwhite(ml_get(lnum)) != NUL)
8467 break;
8468 }
8469 rettv->vval.v_number = lnum;
8470}
8471
8472/*
8473 * "nr2char()" function
8474 */
8475 static void
8476f_nr2char(typval_T *argvars, typval_T *rettv)
8477{
8478 char_u buf[NUMBUFLEN];
8479
8480#ifdef FEAT_MBYTE
8481 if (has_mbyte)
8482 {
8483 int utf8 = 0;
8484
8485 if (argvars[1].v_type != VAR_UNKNOWN)
8486 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
8487 if (utf8)
8488 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8489 else
8490 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8491 }
8492 else
8493#endif
8494 {
8495 buf[0] = (char_u)get_tv_number(&argvars[0]);
8496 buf[1] = NUL;
8497 }
8498 rettv->v_type = VAR_STRING;
8499 rettv->vval.v_string = vim_strsave(buf);
8500}
8501
8502/*
8503 * "or(expr, expr)" function
8504 */
8505 static void
8506f_or(typval_T *argvars, typval_T *rettv)
8507{
8508 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8509 | get_tv_number_chk(&argvars[1], NULL);
8510}
8511
8512/*
8513 * "pathshorten()" function
8514 */
8515 static void
8516f_pathshorten(typval_T *argvars, typval_T *rettv)
8517{
8518 char_u *p;
8519
8520 rettv->v_type = VAR_STRING;
8521 p = get_tv_string_chk(&argvars[0]);
8522 if (p == NULL)
8523 rettv->vval.v_string = NULL;
8524 else
8525 {
8526 p = vim_strsave(p);
8527 rettv->vval.v_string = p;
8528 if (p != NULL)
8529 shorten_dir(p);
8530 }
8531}
8532
8533#ifdef FEAT_PERL
8534/*
8535 * "perleval()" function
8536 */
8537 static void
8538f_perleval(typval_T *argvars, typval_T *rettv)
8539{
8540 char_u *str;
8541 char_u buf[NUMBUFLEN];
8542
8543 str = get_tv_string_buf(&argvars[0], buf);
8544 do_perleval(str, rettv);
8545}
8546#endif
8547
8548#ifdef FEAT_FLOAT
8549/*
8550 * "pow()" function
8551 */
8552 static void
8553f_pow(typval_T *argvars, typval_T *rettv)
8554{
8555 float_T fx = 0.0, fy = 0.0;
8556
8557 rettv->v_type = VAR_FLOAT;
8558 if (get_float_arg(argvars, &fx) == OK
8559 && get_float_arg(&argvars[1], &fy) == OK)
8560 rettv->vval.v_float = pow(fx, fy);
8561 else
8562 rettv->vval.v_float = 0.0;
8563}
8564#endif
8565
8566/*
8567 * "prevnonblank()" function
8568 */
8569 static void
8570f_prevnonblank(typval_T *argvars, typval_T *rettv)
8571{
8572 linenr_T lnum;
8573
8574 lnum = get_tv_lnum(argvars);
8575 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8576 lnum = 0;
8577 else
8578 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8579 --lnum;
8580 rettv->vval.v_number = lnum;
8581}
8582
8583/* This dummy va_list is here because:
8584 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8585 * - locally in the function results in a "used before set" warning
8586 * - using va_start() to initialize it gives "function with fixed args" error */
8587static va_list ap;
8588
8589/*
8590 * "printf()" function
8591 */
8592 static void
8593f_printf(typval_T *argvars, typval_T *rettv)
8594{
8595 char_u buf[NUMBUFLEN];
8596 int len;
8597 char_u *s;
8598 int saved_did_emsg = did_emsg;
8599 char *fmt;
8600
8601 rettv->v_type = VAR_STRING;
8602 rettv->vval.v_string = NULL;
8603
8604 /* Get the required length, allocate the buffer and do it for real. */
8605 did_emsg = FALSE;
8606 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008607 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008608 if (!did_emsg)
8609 {
8610 s = alloc(len + 1);
8611 if (s != NULL)
8612 {
8613 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008614 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8615 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008616 }
8617 }
8618 did_emsg |= saved_did_emsg;
8619}
8620
Bram Moolenaarf2732452018-06-03 14:47:35 +02008621#ifdef FEAT_JOB_CHANNEL
8622/*
8623 * "prompt_setcallback({buffer}, {callback})" function
8624 */
8625 static void
8626f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
8627{
8628 buf_T *buf;
8629 char_u *callback;
8630 partial_T *partial;
8631
8632 if (check_secure())
8633 return;
8634 buf = get_buf_tv(&argvars[0], FALSE);
8635 if (buf == NULL)
8636 return;
8637
8638 callback = get_callback(&argvars[1], &partial);
8639 if (callback == NULL)
8640 return;
8641
8642 free_callback(buf->b_prompt_callback, buf->b_prompt_partial);
8643 if (partial == NULL)
8644 buf->b_prompt_callback = vim_strsave(callback);
8645 else
8646 /* pointer into the partial */
8647 buf->b_prompt_callback = callback;
8648 buf->b_prompt_partial = partial;
8649}
8650
8651/*
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02008652 * "prompt_setinterrupt({buffer}, {callback})" function
8653 */
8654 static void
8655f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
8656{
8657 buf_T *buf;
8658 char_u *callback;
8659 partial_T *partial;
8660
8661 if (check_secure())
8662 return;
8663 buf = get_buf_tv(&argvars[0], FALSE);
8664 if (buf == NULL)
8665 return;
8666
8667 callback = get_callback(&argvars[1], &partial);
8668 if (callback == NULL)
8669 return;
8670
8671 free_callback(buf->b_prompt_interrupt, buf->b_prompt_int_partial);
8672 if (partial == NULL)
8673 buf->b_prompt_interrupt = vim_strsave(callback);
8674 else
8675 /* pointer into the partial */
8676 buf->b_prompt_interrupt = callback;
8677 buf->b_prompt_int_partial = partial;
8678}
8679
8680/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02008681 * "prompt_setprompt({buffer}, {text})" function
8682 */
8683 static void
8684f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
8685{
8686 buf_T *buf;
8687 char_u *text;
8688
8689 if (check_secure())
8690 return;
8691 buf = get_buf_tv(&argvars[0], FALSE);
8692 if (buf == NULL)
8693 return;
8694
8695 text = get_tv_string(&argvars[1]);
8696 vim_free(buf->b_prompt_text);
8697 buf->b_prompt_text = vim_strsave(text);
8698}
8699#endif
8700
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008701/*
8702 * "pumvisible()" function
8703 */
8704 static void
8705f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8706{
8707#ifdef FEAT_INS_EXPAND
8708 if (pum_visible())
8709 rettv->vval.v_number = 1;
8710#endif
8711}
8712
8713#ifdef FEAT_PYTHON3
8714/*
8715 * "py3eval()" function
8716 */
8717 static void
8718f_py3eval(typval_T *argvars, typval_T *rettv)
8719{
8720 char_u *str;
8721 char_u buf[NUMBUFLEN];
8722
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008723 if (p_pyx == 0)
8724 p_pyx = 3;
8725
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008726 str = get_tv_string_buf(&argvars[0], buf);
8727 do_py3eval(str, rettv);
8728}
8729#endif
8730
8731#ifdef FEAT_PYTHON
8732/*
8733 * "pyeval()" function
8734 */
8735 static void
8736f_pyeval(typval_T *argvars, typval_T *rettv)
8737{
8738 char_u *str;
8739 char_u buf[NUMBUFLEN];
8740
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008741 if (p_pyx == 0)
8742 p_pyx = 2;
8743
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008744 str = get_tv_string_buf(&argvars[0], buf);
8745 do_pyeval(str, rettv);
8746}
8747#endif
8748
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008749#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8750/*
8751 * "pyxeval()" function
8752 */
8753 static void
8754f_pyxeval(typval_T *argvars, typval_T *rettv)
8755{
8756# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8757 init_pyxversion();
8758 if (p_pyx == 2)
8759 f_pyeval(argvars, rettv);
8760 else
8761 f_py3eval(argvars, rettv);
8762# elif defined(FEAT_PYTHON)
8763 f_pyeval(argvars, rettv);
8764# elif defined(FEAT_PYTHON3)
8765 f_py3eval(argvars, rettv);
8766# endif
8767}
8768#endif
8769
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008770/*
8771 * "range()" function
8772 */
8773 static void
8774f_range(typval_T *argvars, typval_T *rettv)
8775{
8776 varnumber_T start;
8777 varnumber_T end;
8778 varnumber_T stride = 1;
8779 varnumber_T i;
8780 int error = FALSE;
8781
8782 start = get_tv_number_chk(&argvars[0], &error);
8783 if (argvars[1].v_type == VAR_UNKNOWN)
8784 {
8785 end = start - 1;
8786 start = 0;
8787 }
8788 else
8789 {
8790 end = get_tv_number_chk(&argvars[1], &error);
8791 if (argvars[2].v_type != VAR_UNKNOWN)
8792 stride = get_tv_number_chk(&argvars[2], &error);
8793 }
8794
8795 if (error)
8796 return; /* type error; errmsg already given */
8797 if (stride == 0)
8798 EMSG(_("E726: Stride is zero"));
8799 else if (stride > 0 ? end + 1 < start : end - 1 > start)
8800 EMSG(_("E727: Start past end"));
8801 else
8802 {
8803 if (rettv_list_alloc(rettv) == OK)
8804 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8805 if (list_append_number(rettv->vval.v_list,
8806 (varnumber_T)i) == FAIL)
8807 break;
8808 }
8809}
8810
8811/*
8812 * "readfile()" function
8813 */
8814 static void
8815f_readfile(typval_T *argvars, typval_T *rettv)
8816{
8817 int binary = FALSE;
8818 int failed = FALSE;
8819 char_u *fname;
8820 FILE *fd;
8821 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
8822 int io_size = sizeof(buf);
8823 int readlen; /* size of last fread() */
8824 char_u *prev = NULL; /* previously read bytes, if any */
8825 long prevlen = 0; /* length of data in prev */
8826 long prevsize = 0; /* size of prev buffer */
8827 long maxline = MAXLNUM;
8828 long cnt = 0;
8829 char_u *p; /* position in buf */
8830 char_u *start; /* start of current line */
8831
8832 if (argvars[1].v_type != VAR_UNKNOWN)
8833 {
8834 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
8835 binary = TRUE;
8836 if (argvars[2].v_type != VAR_UNKNOWN)
8837 maxline = (long)get_tv_number(&argvars[2]);
8838 }
8839
8840 if (rettv_list_alloc(rettv) == FAIL)
8841 return;
8842
8843 /* Always open the file in binary mode, library functions have a mind of
8844 * their own about CR-LF conversion. */
8845 fname = get_tv_string(&argvars[0]);
8846 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
8847 {
8848 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
8849 return;
8850 }
8851
8852 while (cnt < maxline || maxline < 0)
8853 {
8854 readlen = (int)fread(buf, 1, io_size, fd);
8855
8856 /* This for loop processes what was read, but is also entered at end
8857 * of file so that either:
8858 * - an incomplete line gets written
8859 * - a "binary" file gets an empty line at the end if it ends in a
8860 * newline. */
8861 for (p = buf, start = buf;
8862 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
8863 ++p)
8864 {
8865 if (*p == '\n' || readlen <= 0)
8866 {
8867 listitem_T *li;
8868 char_u *s = NULL;
8869 long_u len = p - start;
8870
8871 /* Finished a line. Remove CRs before NL. */
8872 if (readlen > 0 && !binary)
8873 {
8874 while (len > 0 && start[len - 1] == '\r')
8875 --len;
8876 /* removal may cross back to the "prev" string */
8877 if (len == 0)
8878 while (prevlen > 0 && prev[prevlen - 1] == '\r')
8879 --prevlen;
8880 }
8881 if (prevlen == 0)
8882 s = vim_strnsave(start, (int)len);
8883 else
8884 {
8885 /* Change "prev" buffer to be the right size. This way
8886 * the bytes are only copied once, and very long lines are
8887 * allocated only once. */
8888 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
8889 {
8890 mch_memmove(s + prevlen, start, len);
8891 s[prevlen + len] = NUL;
8892 prev = NULL; /* the list will own the string */
8893 prevlen = prevsize = 0;
8894 }
8895 }
8896 if (s == NULL)
8897 {
8898 do_outofmem_msg((long_u) prevlen + len + 1);
8899 failed = TRUE;
8900 break;
8901 }
8902
8903 if ((li = listitem_alloc()) == NULL)
8904 {
8905 vim_free(s);
8906 failed = TRUE;
8907 break;
8908 }
8909 li->li_tv.v_type = VAR_STRING;
8910 li->li_tv.v_lock = 0;
8911 li->li_tv.vval.v_string = s;
8912 list_append(rettv->vval.v_list, li);
8913
8914 start = p + 1; /* step over newline */
8915 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
8916 break;
8917 }
8918 else if (*p == NUL)
8919 *p = '\n';
8920#ifdef FEAT_MBYTE
8921 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
8922 * when finding the BF and check the previous two bytes. */
8923 else if (*p == 0xbf && enc_utf8 && !binary)
8924 {
8925 /* Find the two bytes before the 0xbf. If p is at buf, or buf
8926 * + 1, these may be in the "prev" string. */
8927 char_u back1 = p >= buf + 1 ? p[-1]
8928 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
8929 char_u back2 = p >= buf + 2 ? p[-2]
8930 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
8931 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
8932
8933 if (back2 == 0xef && back1 == 0xbb)
8934 {
8935 char_u *dest = p - 2;
8936
8937 /* Usually a BOM is at the beginning of a file, and so at
8938 * the beginning of a line; then we can just step over it.
8939 */
8940 if (start == dest)
8941 start = p + 1;
8942 else
8943 {
8944 /* have to shuffle buf to close gap */
8945 int adjust_prevlen = 0;
8946
8947 if (dest < buf)
8948 {
8949 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
8950 dest = buf;
8951 }
8952 if (readlen > p - buf + 1)
8953 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
8954 readlen -= 3 - adjust_prevlen;
8955 prevlen -= adjust_prevlen;
8956 p = dest - 1;
8957 }
8958 }
8959 }
8960#endif
8961 } /* for */
8962
8963 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
8964 break;
8965 if (start < p)
8966 {
8967 /* There's part of a line in buf, store it in "prev". */
8968 if (p - start + prevlen >= prevsize)
8969 {
8970 /* need bigger "prev" buffer */
8971 char_u *newprev;
8972
8973 /* A common use case is ordinary text files and "prev" gets a
8974 * fragment of a line, so the first allocation is made
8975 * small, to avoid repeatedly 'allocing' large and
8976 * 'reallocing' small. */
8977 if (prevsize == 0)
8978 prevsize = (long)(p - start);
8979 else
8980 {
8981 long grow50pc = (prevsize * 3) / 2;
8982 long growmin = (long)((p - start) * 2 + prevlen);
8983 prevsize = grow50pc > growmin ? grow50pc : growmin;
8984 }
8985 newprev = prev == NULL ? alloc(prevsize)
8986 : vim_realloc(prev, prevsize);
8987 if (newprev == NULL)
8988 {
8989 do_outofmem_msg((long_u)prevsize);
8990 failed = TRUE;
8991 break;
8992 }
8993 prev = newprev;
8994 }
8995 /* Add the line part to end of "prev". */
8996 mch_memmove(prev + prevlen, start, p - start);
8997 prevlen += (long)(p - start);
8998 }
8999 } /* while */
9000
9001 /*
9002 * For a negative line count use only the lines at the end of the file,
9003 * free the rest.
9004 */
9005 if (!failed && maxline < 0)
9006 while (cnt > -maxline)
9007 {
9008 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
9009 --cnt;
9010 }
9011
9012 if (failed)
9013 {
9014 list_free(rettv->vval.v_list);
9015 /* readfile doc says an empty list is returned on error */
9016 rettv->vval.v_list = list_alloc();
9017 }
9018
9019 vim_free(prev);
9020 fclose(fd);
9021}
9022
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009023 static void
9024return_register(int regname, typval_T *rettv)
9025{
9026 char_u buf[2] = {0, 0};
9027
9028 buf[0] = (char_u)regname;
9029 rettv->v_type = VAR_STRING;
9030 rettv->vval.v_string = vim_strsave(buf);
9031}
9032
9033/*
9034 * "reg_executing()" function
9035 */
9036 static void
9037f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9038{
9039 return_register(reg_executing, rettv);
9040}
9041
9042/*
9043 * "reg_recording()" function
9044 */
9045 static void
9046f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9047{
9048 return_register(reg_recording, rettv);
9049}
9050
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009051#if defined(FEAT_RELTIME)
9052static int list2proftime(typval_T *arg, proftime_T *tm);
9053
9054/*
9055 * Convert a List to proftime_T.
9056 * Return FAIL when there is something wrong.
9057 */
9058 static int
9059list2proftime(typval_T *arg, proftime_T *tm)
9060{
9061 long n1, n2;
9062 int error = FALSE;
9063
9064 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9065 || arg->vval.v_list->lv_len != 2)
9066 return FAIL;
9067 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9068 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
9069# ifdef WIN3264
9070 tm->HighPart = n1;
9071 tm->LowPart = n2;
9072# else
9073 tm->tv_sec = n1;
9074 tm->tv_usec = n2;
9075# endif
9076 return error ? FAIL : OK;
9077}
9078#endif /* FEAT_RELTIME */
9079
9080/*
9081 * "reltime()" function
9082 */
9083 static void
9084f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9085{
9086#ifdef FEAT_RELTIME
9087 proftime_T res;
9088 proftime_T start;
9089
9090 if (argvars[0].v_type == VAR_UNKNOWN)
9091 {
9092 /* No arguments: get current time. */
9093 profile_start(&res);
9094 }
9095 else if (argvars[1].v_type == VAR_UNKNOWN)
9096 {
9097 if (list2proftime(&argvars[0], &res) == FAIL)
9098 return;
9099 profile_end(&res);
9100 }
9101 else
9102 {
9103 /* Two arguments: compute the difference. */
9104 if (list2proftime(&argvars[0], &start) == FAIL
9105 || list2proftime(&argvars[1], &res) == FAIL)
9106 return;
9107 profile_sub(&res, &start);
9108 }
9109
9110 if (rettv_list_alloc(rettv) == OK)
9111 {
9112 long n1, n2;
9113
9114# ifdef WIN3264
9115 n1 = res.HighPart;
9116 n2 = res.LowPart;
9117# else
9118 n1 = res.tv_sec;
9119 n2 = res.tv_usec;
9120# endif
9121 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9122 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9123 }
9124#endif
9125}
9126
9127#ifdef FEAT_FLOAT
9128/*
9129 * "reltimefloat()" function
9130 */
9131 static void
9132f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9133{
9134# ifdef FEAT_RELTIME
9135 proftime_T tm;
9136# endif
9137
9138 rettv->v_type = VAR_FLOAT;
9139 rettv->vval.v_float = 0;
9140# ifdef FEAT_RELTIME
9141 if (list2proftime(&argvars[0], &tm) == OK)
9142 rettv->vval.v_float = profile_float(&tm);
9143# endif
9144}
9145#endif
9146
9147/*
9148 * "reltimestr()" function
9149 */
9150 static void
9151f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9152{
9153#ifdef FEAT_RELTIME
9154 proftime_T tm;
9155#endif
9156
9157 rettv->v_type = VAR_STRING;
9158 rettv->vval.v_string = NULL;
9159#ifdef FEAT_RELTIME
9160 if (list2proftime(&argvars[0], &tm) == OK)
9161 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9162#endif
9163}
9164
9165#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
9166static void make_connection(void);
9167static int check_connection(void);
9168
9169 static void
9170make_connection(void)
9171{
9172 if (X_DISPLAY == NULL
9173# ifdef FEAT_GUI
9174 && !gui.in_use
9175# endif
9176 )
9177 {
9178 x_force_connect = TRUE;
9179 setup_term_clip();
9180 x_force_connect = FALSE;
9181 }
9182}
9183
9184 static int
9185check_connection(void)
9186{
9187 make_connection();
9188 if (X_DISPLAY == NULL)
9189 {
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009190 EMSG(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009191 return FAIL;
9192 }
9193 return OK;
9194}
9195#endif
9196
9197#ifdef FEAT_CLIENTSERVER
9198 static void
9199remote_common(typval_T *argvars, typval_T *rettv, int expr)
9200{
9201 char_u *server_name;
9202 char_u *keys;
9203 char_u *r = NULL;
9204 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009205 int timeout = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009206# ifdef WIN32
9207 HWND w;
9208# else
9209 Window w;
9210# endif
9211
9212 if (check_restricted() || check_secure())
9213 return;
9214
9215# ifdef FEAT_X11
9216 if (check_connection() == FAIL)
9217 return;
9218# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009219 if (argvars[2].v_type != VAR_UNKNOWN
9220 && argvars[3].v_type != VAR_UNKNOWN)
9221 timeout = get_tv_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009222
9223 server_name = get_tv_string_chk(&argvars[0]);
9224 if (server_name == NULL)
9225 return; /* type error; errmsg already given */
9226 keys = get_tv_string_buf(&argvars[1], buf);
9227# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009228 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009229# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009230 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9231 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009232# endif
9233 {
9234 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009235 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009236 EMSG(r); /* sending worked but evaluation failed */
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009237 vim_free(r);
9238 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009239 else
9240 EMSG2(_("E241: Unable to send to %s"), server_name);
9241 return;
9242 }
9243
9244 rettv->vval.v_string = r;
9245
9246 if (argvars[2].v_type != VAR_UNKNOWN)
9247 {
9248 dictitem_T v;
9249 char_u str[30];
9250 char_u *idvar;
9251
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009252 idvar = get_tv_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009253 if (idvar != NULL && *idvar != NUL)
9254 {
9255 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9256 v.di_tv.v_type = VAR_STRING;
9257 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009258 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009259 vim_free(v.di_tv.vval.v_string);
9260 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009261 }
9262}
9263#endif
9264
9265/*
9266 * "remote_expr()" function
9267 */
9268 static void
9269f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9270{
9271 rettv->v_type = VAR_STRING;
9272 rettv->vval.v_string = NULL;
9273#ifdef FEAT_CLIENTSERVER
9274 remote_common(argvars, rettv, TRUE);
9275#endif
9276}
9277
9278/*
9279 * "remote_foreground()" function
9280 */
9281 static void
9282f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9283{
9284#ifdef FEAT_CLIENTSERVER
9285# ifdef WIN32
9286 /* On Win32 it's done in this application. */
9287 {
9288 char_u *server_name = get_tv_string_chk(&argvars[0]);
9289
9290 if (server_name != NULL)
9291 serverForeground(server_name);
9292 }
9293# else
9294 /* Send a foreground() expression to the server. */
9295 argvars[1].v_type = VAR_STRING;
9296 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9297 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009298 rettv->v_type = VAR_STRING;
9299 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009300 remote_common(argvars, rettv, TRUE);
9301 vim_free(argvars[1].vval.v_string);
9302# endif
9303#endif
9304}
9305
9306 static void
9307f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
9308{
9309#ifdef FEAT_CLIENTSERVER
9310 dictitem_T v;
9311 char_u *s = NULL;
9312# ifdef WIN32
9313 long_u n = 0;
9314# endif
9315 char_u *serverid;
9316
9317 if (check_restricted() || check_secure())
9318 {
9319 rettv->vval.v_number = -1;
9320 return;
9321 }
9322 serverid = get_tv_string_chk(&argvars[0]);
9323 if (serverid == NULL)
9324 {
9325 rettv->vval.v_number = -1;
9326 return; /* type error; errmsg already given */
9327 }
9328# ifdef WIN32
9329 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
9330 if (n == 0)
9331 rettv->vval.v_number = -1;
9332 else
9333 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009334 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009335 rettv->vval.v_number = (s != NULL);
9336 }
9337# else
9338 if (check_connection() == FAIL)
9339 return;
9340
9341 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
9342 serverStrToWin(serverid), &s);
9343# endif
9344
9345 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
9346 {
9347 char_u *retvar;
9348
9349 v.di_tv.v_type = VAR_STRING;
9350 v.di_tv.vval.v_string = vim_strsave(s);
9351 retvar = get_tv_string_chk(&argvars[1]);
9352 if (retvar != NULL)
9353 set_var(retvar, &v.di_tv, FALSE);
9354 vim_free(v.di_tv.vval.v_string);
9355 }
9356#else
9357 rettv->vval.v_number = -1;
9358#endif
9359}
9360
9361 static void
9362f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
9363{
9364 char_u *r = NULL;
9365
9366#ifdef FEAT_CLIENTSERVER
9367 char_u *serverid = get_tv_string_chk(&argvars[0]);
9368
9369 if (serverid != NULL && !check_restricted() && !check_secure())
9370 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009371 int timeout = 0;
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009372# ifdef WIN32
9373 /* The server's HWND is encoded in the 'id' parameter */
9374 long_u n = 0;
9375# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009376
9377 if (argvars[1].v_type != VAR_UNKNOWN)
9378 timeout = get_tv_number(&argvars[1]);
9379
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009380# ifdef WIN32
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009381 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
9382 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009383 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009384 if (r == NULL)
9385# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009386 if (check_connection() == FAIL
9387 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
9388 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009389# endif
9390 EMSG(_("E277: Unable to read a server reply"));
9391 }
9392#endif
9393 rettv->v_type = VAR_STRING;
9394 rettv->vval.v_string = r;
9395}
9396
9397/*
9398 * "remote_send()" function
9399 */
9400 static void
9401f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9402{
9403 rettv->v_type = VAR_STRING;
9404 rettv->vval.v_string = NULL;
9405#ifdef FEAT_CLIENTSERVER
9406 remote_common(argvars, rettv, FALSE);
9407#endif
9408}
9409
9410/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009411 * "remote_startserver()" function
9412 */
9413 static void
9414f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9415{
9416#ifdef FEAT_CLIENTSERVER
9417 char_u *server = get_tv_string_chk(&argvars[0]);
9418
9419 if (server == NULL)
9420 return; /* type error; errmsg already given */
9421 if (serverName != NULL)
9422 EMSG(_("E941: already started a server"));
9423 else
9424 {
9425# ifdef FEAT_X11
9426 if (check_connection() == OK)
9427 serverRegisterName(X_DISPLAY, server);
9428# else
9429 serverSetName(server);
9430# endif
9431 }
9432#else
9433 EMSG(_("E942: +clientserver feature not available"));
9434#endif
9435}
9436
9437/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009438 * "remove()" function
9439 */
9440 static void
9441f_remove(typval_T *argvars, typval_T *rettv)
9442{
9443 list_T *l;
9444 listitem_T *item, *item2;
9445 listitem_T *li;
9446 long idx;
9447 long end;
9448 char_u *key;
9449 dict_T *d;
9450 dictitem_T *di;
9451 char_u *arg_errmsg = (char_u *)N_("remove() argument");
9452
9453 if (argvars[0].v_type == VAR_DICT)
9454 {
9455 if (argvars[2].v_type != VAR_UNKNOWN)
9456 EMSG2(_(e_toomanyarg), "remove()");
9457 else if ((d = argvars[0].vval.v_dict) != NULL
9458 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
9459 {
9460 key = get_tv_string_chk(&argvars[1]);
9461 if (key != NULL)
9462 {
9463 di = dict_find(d, key, -1);
9464 if (di == NULL)
9465 EMSG2(_(e_dictkey), key);
9466 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
9467 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
9468 {
9469 *rettv = di->di_tv;
9470 init_tv(&di->di_tv);
9471 dictitem_remove(d, di);
9472 }
9473 }
9474 }
9475 }
9476 else if (argvars[0].v_type != VAR_LIST)
9477 EMSG2(_(e_listdictarg), "remove()");
9478 else if ((l = argvars[0].vval.v_list) != NULL
9479 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
9480 {
9481 int error = FALSE;
9482
9483 idx = (long)get_tv_number_chk(&argvars[1], &error);
9484 if (error)
9485 ; /* type error: do nothing, errmsg already given */
9486 else if ((item = list_find(l, idx)) == NULL)
9487 EMSGN(_(e_listidx), idx);
9488 else
9489 {
9490 if (argvars[2].v_type == VAR_UNKNOWN)
9491 {
9492 /* Remove one item, return its value. */
9493 vimlist_remove(l, item, item);
9494 *rettv = item->li_tv;
9495 vim_free(item);
9496 }
9497 else
9498 {
9499 /* Remove range of items, return list with values. */
9500 end = (long)get_tv_number_chk(&argvars[2], &error);
9501 if (error)
9502 ; /* type error: do nothing */
9503 else if ((item2 = list_find(l, end)) == NULL)
9504 EMSGN(_(e_listidx), end);
9505 else
9506 {
9507 int cnt = 0;
9508
9509 for (li = item; li != NULL; li = li->li_next)
9510 {
9511 ++cnt;
9512 if (li == item2)
9513 break;
9514 }
9515 if (li == NULL) /* didn't find "item2" after "item" */
9516 EMSG(_(e_invrange));
9517 else
9518 {
9519 vimlist_remove(l, item, item2);
9520 if (rettv_list_alloc(rettv) == OK)
9521 {
9522 l = rettv->vval.v_list;
9523 l->lv_first = item;
9524 l->lv_last = item2;
9525 item->li_prev = NULL;
9526 item2->li_next = NULL;
9527 l->lv_len = cnt;
9528 }
9529 }
9530 }
9531 }
9532 }
9533 }
9534}
9535
9536/*
9537 * "rename({from}, {to})" function
9538 */
9539 static void
9540f_rename(typval_T *argvars, typval_T *rettv)
9541{
9542 char_u buf[NUMBUFLEN];
9543
9544 if (check_restricted() || check_secure())
9545 rettv->vval.v_number = -1;
9546 else
9547 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
9548 get_tv_string_buf(&argvars[1], buf));
9549}
9550
9551/*
9552 * "repeat()" function
9553 */
9554 static void
9555f_repeat(typval_T *argvars, typval_T *rettv)
9556{
9557 char_u *p;
9558 int n;
9559 int slen;
9560 int len;
9561 char_u *r;
9562 int i;
9563
9564 n = (int)get_tv_number(&argvars[1]);
9565 if (argvars[0].v_type == VAR_LIST)
9566 {
9567 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9568 while (n-- > 0)
9569 if (list_extend(rettv->vval.v_list,
9570 argvars[0].vval.v_list, NULL) == FAIL)
9571 break;
9572 }
9573 else
9574 {
9575 p = get_tv_string(&argvars[0]);
9576 rettv->v_type = VAR_STRING;
9577 rettv->vval.v_string = NULL;
9578
9579 slen = (int)STRLEN(p);
9580 len = slen * n;
9581 if (len <= 0)
9582 return;
9583
9584 r = alloc(len + 1);
9585 if (r != NULL)
9586 {
9587 for (i = 0; i < n; i++)
9588 mch_memmove(r + i * slen, p, (size_t)slen);
9589 r[len] = NUL;
9590 }
9591
9592 rettv->vval.v_string = r;
9593 }
9594}
9595
9596/*
9597 * "resolve()" function
9598 */
9599 static void
9600f_resolve(typval_T *argvars, typval_T *rettv)
9601{
9602 char_u *p;
9603#ifdef HAVE_READLINK
9604 char_u *buf = NULL;
9605#endif
9606
9607 p = get_tv_string(&argvars[0]);
9608#ifdef FEAT_SHORTCUT
9609 {
9610 char_u *v = NULL;
9611
9612 v = mch_resolve_shortcut(p);
9613 if (v != NULL)
9614 rettv->vval.v_string = v;
9615 else
9616 rettv->vval.v_string = vim_strsave(p);
9617 }
9618#else
9619# ifdef HAVE_READLINK
9620 {
9621 char_u *cpy;
9622 int len;
9623 char_u *remain = NULL;
9624 char_u *q;
9625 int is_relative_to_current = FALSE;
9626 int has_trailing_pathsep = FALSE;
9627 int limit = 100;
9628
9629 p = vim_strsave(p);
9630
9631 if (p[0] == '.' && (vim_ispathsep(p[1])
9632 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9633 is_relative_to_current = TRUE;
9634
9635 len = STRLEN(p);
9636 if (len > 0 && after_pathsep(p, p + len))
9637 {
9638 has_trailing_pathsep = TRUE;
9639 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9640 }
9641
9642 q = getnextcomp(p);
9643 if (*q != NUL)
9644 {
9645 /* Separate the first path component in "p", and keep the
9646 * remainder (beginning with the path separator). */
9647 remain = vim_strsave(q - 1);
9648 q[-1] = NUL;
9649 }
9650
9651 buf = alloc(MAXPATHL + 1);
9652 if (buf == NULL)
9653 goto fail;
9654
9655 for (;;)
9656 {
9657 for (;;)
9658 {
9659 len = readlink((char *)p, (char *)buf, MAXPATHL);
9660 if (len <= 0)
9661 break;
9662 buf[len] = NUL;
9663
9664 if (limit-- == 0)
9665 {
9666 vim_free(p);
9667 vim_free(remain);
9668 EMSG(_("E655: Too many symbolic links (cycle?)"));
9669 rettv->vval.v_string = NULL;
9670 goto fail;
9671 }
9672
9673 /* Ensure that the result will have a trailing path separator
9674 * if the argument has one. */
9675 if (remain == NULL && has_trailing_pathsep)
9676 add_pathsep(buf);
9677
9678 /* Separate the first path component in the link value and
9679 * concatenate the remainders. */
9680 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9681 if (*q != NUL)
9682 {
9683 if (remain == NULL)
9684 remain = vim_strsave(q - 1);
9685 else
9686 {
9687 cpy = concat_str(q - 1, remain);
9688 if (cpy != NULL)
9689 {
9690 vim_free(remain);
9691 remain = cpy;
9692 }
9693 }
9694 q[-1] = NUL;
9695 }
9696
9697 q = gettail(p);
9698 if (q > p && *q == NUL)
9699 {
9700 /* Ignore trailing path separator. */
9701 q[-1] = NUL;
9702 q = gettail(p);
9703 }
9704 if (q > p && !mch_isFullName(buf))
9705 {
9706 /* symlink is relative to directory of argument */
9707 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
9708 if (cpy != NULL)
9709 {
9710 STRCPY(cpy, p);
9711 STRCPY(gettail(cpy), buf);
9712 vim_free(p);
9713 p = cpy;
9714 }
9715 }
9716 else
9717 {
9718 vim_free(p);
9719 p = vim_strsave(buf);
9720 }
9721 }
9722
9723 if (remain == NULL)
9724 break;
9725
9726 /* Append the first path component of "remain" to "p". */
9727 q = getnextcomp(remain + 1);
9728 len = q - remain - (*q != NUL);
9729 cpy = vim_strnsave(p, STRLEN(p) + len);
9730 if (cpy != NULL)
9731 {
9732 STRNCAT(cpy, remain, len);
9733 vim_free(p);
9734 p = cpy;
9735 }
9736 /* Shorten "remain". */
9737 if (*q != NUL)
9738 STRMOVE(remain, q - 1);
9739 else
Bram Moolenaard23a8232018-02-10 18:45:26 +01009740 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009741 }
9742
9743 /* If the result is a relative path name, make it explicitly relative to
9744 * the current directory if and only if the argument had this form. */
9745 if (!vim_ispathsep(*p))
9746 {
9747 if (is_relative_to_current
9748 && *p != NUL
9749 && !(p[0] == '.'
9750 && (p[1] == NUL
9751 || vim_ispathsep(p[1])
9752 || (p[1] == '.'
9753 && (p[2] == NUL
9754 || vim_ispathsep(p[2]))))))
9755 {
9756 /* Prepend "./". */
9757 cpy = concat_str((char_u *)"./", p);
9758 if (cpy != NULL)
9759 {
9760 vim_free(p);
9761 p = cpy;
9762 }
9763 }
9764 else if (!is_relative_to_current)
9765 {
9766 /* Strip leading "./". */
9767 q = p;
9768 while (q[0] == '.' && vim_ispathsep(q[1]))
9769 q += 2;
9770 if (q > p)
9771 STRMOVE(p, p + 2);
9772 }
9773 }
9774
9775 /* Ensure that the result will have no trailing path separator
9776 * if the argument had none. But keep "/" or "//". */
9777 if (!has_trailing_pathsep)
9778 {
9779 q = p + STRLEN(p);
9780 if (after_pathsep(p, q))
9781 *gettail_sep(p) = NUL;
9782 }
9783
9784 rettv->vval.v_string = p;
9785 }
9786# else
9787 rettv->vval.v_string = vim_strsave(p);
9788# endif
9789#endif
9790
9791 simplify_filename(rettv->vval.v_string);
9792
9793#ifdef HAVE_READLINK
9794fail:
9795 vim_free(buf);
9796#endif
9797 rettv->v_type = VAR_STRING;
9798}
9799
9800/*
9801 * "reverse({list})" function
9802 */
9803 static void
9804f_reverse(typval_T *argvars, typval_T *rettv)
9805{
9806 list_T *l;
9807 listitem_T *li, *ni;
9808
9809 if (argvars[0].v_type != VAR_LIST)
9810 EMSG2(_(e_listarg), "reverse()");
9811 else if ((l = argvars[0].vval.v_list) != NULL
9812 && !tv_check_lock(l->lv_lock,
9813 (char_u *)N_("reverse() argument"), TRUE))
9814 {
9815 li = l->lv_last;
9816 l->lv_first = l->lv_last = NULL;
9817 l->lv_len = 0;
9818 while (li != NULL)
9819 {
9820 ni = li->li_prev;
9821 list_append(l, li);
9822 li = ni;
9823 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009824 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009825 l->lv_idx = l->lv_len - l->lv_idx - 1;
9826 }
9827}
9828
9829#define SP_NOMOVE 0x01 /* don't move cursor */
9830#define SP_REPEAT 0x02 /* repeat to find outer pair */
9831#define SP_RETCOUNT 0x04 /* return matchcount */
9832#define SP_SETPCMARK 0x08 /* set previous context mark */
9833#define SP_START 0x10 /* accept match at start position */
9834#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
9835#define SP_END 0x40 /* leave cursor at end of match */
9836#define SP_COLUMN 0x80 /* start at cursor column */
9837
9838static int get_search_arg(typval_T *varp, int *flagsp);
9839
9840/*
9841 * Get flags for a search function.
9842 * Possibly sets "p_ws".
9843 * Returns BACKWARD, FORWARD or zero (for an error).
9844 */
9845 static int
9846get_search_arg(typval_T *varp, int *flagsp)
9847{
9848 int dir = FORWARD;
9849 char_u *flags;
9850 char_u nbuf[NUMBUFLEN];
9851 int mask;
9852
9853 if (varp->v_type != VAR_UNKNOWN)
9854 {
9855 flags = get_tv_string_buf_chk(varp, nbuf);
9856 if (flags == NULL)
9857 return 0; /* type error; errmsg already given */
9858 while (*flags != NUL)
9859 {
9860 switch (*flags)
9861 {
9862 case 'b': dir = BACKWARD; break;
9863 case 'w': p_ws = TRUE; break;
9864 case 'W': p_ws = FALSE; break;
9865 default: mask = 0;
9866 if (flagsp != NULL)
9867 switch (*flags)
9868 {
9869 case 'c': mask = SP_START; break;
9870 case 'e': mask = SP_END; break;
9871 case 'm': mask = SP_RETCOUNT; break;
9872 case 'n': mask = SP_NOMOVE; break;
9873 case 'p': mask = SP_SUBPAT; break;
9874 case 'r': mask = SP_REPEAT; break;
9875 case 's': mask = SP_SETPCMARK; break;
9876 case 'z': mask = SP_COLUMN; break;
9877 }
9878 if (mask == 0)
9879 {
9880 EMSG2(_(e_invarg2), flags);
9881 dir = 0;
9882 }
9883 else
9884 *flagsp |= mask;
9885 }
9886 if (dir == 0)
9887 break;
9888 ++flags;
9889 }
9890 }
9891 return dir;
9892}
9893
9894/*
9895 * Shared by search() and searchpos() functions.
9896 */
9897 static int
9898search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
9899{
9900 int flags;
9901 char_u *pat;
9902 pos_T pos;
9903 pos_T save_cursor;
9904 int save_p_ws = p_ws;
9905 int dir;
9906 int retval = 0; /* default: FAIL */
9907 long lnum_stop = 0;
9908 proftime_T tm;
9909#ifdef FEAT_RELTIME
9910 long time_limit = 0;
9911#endif
9912 int options = SEARCH_KEEP;
9913 int subpatnum;
9914
9915 pat = get_tv_string(&argvars[0]);
9916 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
9917 if (dir == 0)
9918 goto theend;
9919 flags = *flagsp;
9920 if (flags & SP_START)
9921 options |= SEARCH_START;
9922 if (flags & SP_END)
9923 options |= SEARCH_END;
9924 if (flags & SP_COLUMN)
9925 options |= SEARCH_COL;
9926
9927 /* Optional arguments: line number to stop searching and timeout. */
9928 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9929 {
9930 lnum_stop = (long)get_tv_number_chk(&argvars[2], NULL);
9931 if (lnum_stop < 0)
9932 goto theend;
9933#ifdef FEAT_RELTIME
9934 if (argvars[3].v_type != VAR_UNKNOWN)
9935 {
9936 time_limit = (long)get_tv_number_chk(&argvars[3], NULL);
9937 if (time_limit < 0)
9938 goto theend;
9939 }
9940#endif
9941 }
9942
9943#ifdef FEAT_RELTIME
9944 /* Set the time limit, if there is one. */
9945 profile_setlimit(time_limit, &tm);
9946#endif
9947
9948 /*
9949 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
9950 * Check to make sure only those flags are set.
9951 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
9952 * flags cannot be set. Check for that condition also.
9953 */
9954 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
9955 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9956 {
9957 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
9958 goto theend;
9959 }
9960
9961 pos = save_cursor = curwin->w_cursor;
9962 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009963 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009964 if (subpatnum != FAIL)
9965 {
9966 if (flags & SP_SUBPAT)
9967 retval = subpatnum;
9968 else
9969 retval = pos.lnum;
9970 if (flags & SP_SETPCMARK)
9971 setpcmark();
9972 curwin->w_cursor = pos;
9973 if (match_pos != NULL)
9974 {
9975 /* Store the match cursor position */
9976 match_pos->lnum = pos.lnum;
9977 match_pos->col = pos.col + 1;
9978 }
9979 /* "/$" will put the cursor after the end of the line, may need to
9980 * correct that here */
9981 check_cursor();
9982 }
9983
9984 /* If 'n' flag is used: restore cursor position. */
9985 if (flags & SP_NOMOVE)
9986 curwin->w_cursor = save_cursor;
9987 else
9988 curwin->w_set_curswant = TRUE;
9989theend:
9990 p_ws = save_p_ws;
9991
9992 return retval;
9993}
9994
9995#ifdef FEAT_FLOAT
9996
9997/*
9998 * round() is not in C90, use ceil() or floor() instead.
9999 */
10000 float_T
10001vim_round(float_T f)
10002{
10003 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
10004}
10005
10006/*
10007 * "round({float})" function
10008 */
10009 static void
10010f_round(typval_T *argvars, typval_T *rettv)
10011{
10012 float_T f = 0.0;
10013
10014 rettv->v_type = VAR_FLOAT;
10015 if (get_float_arg(argvars, &f) == OK)
10016 rettv->vval.v_float = vim_round(f);
10017 else
10018 rettv->vval.v_float = 0.0;
10019}
10020#endif
10021
10022/*
10023 * "screenattr()" function
10024 */
10025 static void
10026f_screenattr(typval_T *argvars, typval_T *rettv)
10027{
10028 int row;
10029 int col;
10030 int c;
10031
10032 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
10033 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
10034 if (row < 0 || row >= screen_Rows
10035 || col < 0 || col >= screen_Columns)
10036 c = -1;
10037 else
10038 c = ScreenAttrs[LineOffset[row] + col];
10039 rettv->vval.v_number = c;
10040}
10041
10042/*
10043 * "screenchar()" function
10044 */
10045 static void
10046f_screenchar(typval_T *argvars, typval_T *rettv)
10047{
10048 int row;
10049 int col;
10050 int off;
10051 int c;
10052
10053 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
10054 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
10055 if (row < 0 || row >= screen_Rows
10056 || col < 0 || col >= screen_Columns)
10057 c = -1;
10058 else
10059 {
10060 off = LineOffset[row] + col;
10061#ifdef FEAT_MBYTE
10062 if (enc_utf8 && ScreenLinesUC[off] != 0)
10063 c = ScreenLinesUC[off];
10064 else
10065#endif
10066 c = ScreenLines[off];
10067 }
10068 rettv->vval.v_number = c;
10069}
10070
10071/*
10072 * "screencol()" function
10073 *
10074 * First column is 1 to be consistent with virtcol().
10075 */
10076 static void
10077f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10078{
10079 rettv->vval.v_number = screen_screencol() + 1;
10080}
10081
10082/*
10083 * "screenrow()" function
10084 */
10085 static void
10086f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10087{
10088 rettv->vval.v_number = screen_screenrow() + 1;
10089}
10090
10091/*
10092 * "search()" function
10093 */
10094 static void
10095f_search(typval_T *argvars, typval_T *rettv)
10096{
10097 int flags = 0;
10098
10099 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10100}
10101
10102/*
10103 * "searchdecl()" function
10104 */
10105 static void
10106f_searchdecl(typval_T *argvars, typval_T *rettv)
10107{
10108 int locally = 1;
10109 int thisblock = 0;
10110 int error = FALSE;
10111 char_u *name;
10112
10113 rettv->vval.v_number = 1; /* default: FAIL */
10114
10115 name = get_tv_string_chk(&argvars[0]);
10116 if (argvars[1].v_type != VAR_UNKNOWN)
10117 {
10118 locally = (int)get_tv_number_chk(&argvars[1], &error) == 0;
10119 if (!error && argvars[2].v_type != VAR_UNKNOWN)
10120 thisblock = (int)get_tv_number_chk(&argvars[2], &error) != 0;
10121 }
10122 if (!error && name != NULL)
10123 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10124 locally, thisblock, SEARCH_KEEP) == FAIL;
10125}
10126
10127/*
10128 * Used by searchpair() and searchpairpos()
10129 */
10130 static int
10131searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10132{
10133 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010134 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010135 int save_p_ws = p_ws;
10136 int dir;
10137 int flags = 0;
10138 char_u nbuf1[NUMBUFLEN];
10139 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010140 int retval = 0; /* default: FAIL */
10141 long lnum_stop = 0;
10142 long time_limit = 0;
10143
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010144 /* Get the three pattern arguments: start, middle, end. Will result in an
10145 * error if not a valid argument. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010146 spat = get_tv_string_chk(&argvars[0]);
10147 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
10148 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
10149 if (spat == NULL || mpat == NULL || epat == NULL)
10150 goto theend; /* type error */
10151
10152 /* Handle the optional fourth argument: flags */
10153 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
10154 if (dir == 0)
10155 goto theend;
10156
10157 /* Don't accept SP_END or SP_SUBPAT.
10158 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
10159 */
10160 if ((flags & (SP_END | SP_SUBPAT)) != 0
10161 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10162 {
10163 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
10164 goto theend;
10165 }
10166
10167 /* Using 'r' implies 'W', otherwise it doesn't work. */
10168 if (flags & SP_REPEAT)
10169 p_ws = FALSE;
10170
10171 /* Optional fifth argument: skip expression */
10172 if (argvars[3].v_type == VAR_UNKNOWN
10173 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010010174 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010175 else
10176 {
Bram Moolenaar48570482017-10-30 21:48:41 +010010177 skip = &argvars[4];
10178 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
10179 && skip->v_type != VAR_STRING)
10180 {
10181 /* Type error */
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010182 EMSG2(_(e_invarg2), get_tv_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010010183 goto theend;
10184 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010185 if (argvars[5].v_type != VAR_UNKNOWN)
10186 {
10187 lnum_stop = (long)get_tv_number_chk(&argvars[5], NULL);
10188 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010189 {
10190 EMSG2(_(e_invarg2), get_tv_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010191 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010192 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010193#ifdef FEAT_RELTIME
10194 if (argvars[6].v_type != VAR_UNKNOWN)
10195 {
10196 time_limit = (long)get_tv_number_chk(&argvars[6], NULL);
10197 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010198 {
10199 EMSG2(_(e_invarg2), get_tv_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010200 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010201 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010202 }
10203#endif
10204 }
10205 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010206
10207 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
10208 match_pos, lnum_stop, time_limit);
10209
10210theend:
10211 p_ws = save_p_ws;
10212
10213 return retval;
10214}
10215
10216/*
10217 * "searchpair()" function
10218 */
10219 static void
10220f_searchpair(typval_T *argvars, typval_T *rettv)
10221{
10222 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
10223}
10224
10225/*
10226 * "searchpairpos()" function
10227 */
10228 static void
10229f_searchpairpos(typval_T *argvars, typval_T *rettv)
10230{
10231 pos_T match_pos;
10232 int lnum = 0;
10233 int col = 0;
10234
10235 if (rettv_list_alloc(rettv) == FAIL)
10236 return;
10237
10238 if (searchpair_cmn(argvars, &match_pos) > 0)
10239 {
10240 lnum = match_pos.lnum;
10241 col = match_pos.col;
10242 }
10243
10244 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10245 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10246}
10247
10248/*
10249 * Search for a start/middle/end thing.
10250 * Used by searchpair(), see its documentation for the details.
10251 * Returns 0 or -1 for no match,
10252 */
10253 long
10254do_searchpair(
10255 char_u *spat, /* start pattern */
10256 char_u *mpat, /* middle pattern */
10257 char_u *epat, /* end pattern */
10258 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010010259 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010260 int flags, /* SP_SETPCMARK and other SP_ values */
10261 pos_T *match_pos,
10262 linenr_T lnum_stop, /* stop at this line if not zero */
10263 long time_limit UNUSED) /* stop after this many msec */
10264{
10265 char_u *save_cpo;
10266 char_u *pat, *pat2 = NULL, *pat3 = NULL;
10267 long retval = 0;
10268 pos_T pos;
10269 pos_T firstpos;
10270 pos_T foundpos;
10271 pos_T save_cursor;
10272 pos_T save_pos;
10273 int n;
10274 int r;
10275 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010010276 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010277 int err;
10278 int options = SEARCH_KEEP;
10279 proftime_T tm;
10280
10281 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10282 save_cpo = p_cpo;
10283 p_cpo = empty_option;
10284
10285#ifdef FEAT_RELTIME
10286 /* Set the time limit, if there is one. */
10287 profile_setlimit(time_limit, &tm);
10288#endif
10289
10290 /* Make two search patterns: start/end (pat2, for in nested pairs) and
10291 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010292 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
10293 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010294 if (pat2 == NULL || pat3 == NULL)
10295 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010296 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010297 if (*mpat == NUL)
10298 STRCPY(pat3, pat2);
10299 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010300 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010301 spat, epat, mpat);
10302 if (flags & SP_START)
10303 options |= SEARCH_START;
10304
Bram Moolenaar48570482017-10-30 21:48:41 +010010305 if (skip != NULL)
10306 {
10307 /* Empty string means to not use the skip expression. */
10308 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
10309 use_skip = skip->vval.v_string != NULL
10310 && *skip->vval.v_string != NUL;
10311 }
10312
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010313 save_cursor = curwin->w_cursor;
10314 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010315 CLEAR_POS(&firstpos);
10316 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010317 pat = pat3;
10318 for (;;)
10319 {
10320 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010321 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010322 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010323 /* didn't find it or found the first match again: FAIL */
10324 break;
10325
10326 if (firstpos.lnum == 0)
10327 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010328 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010329 {
10330 /* Found the same position again. Can happen with a pattern that
10331 * has "\zs" at the end and searching backwards. Advance one
10332 * character and try again. */
10333 if (dir == BACKWARD)
10334 decl(&pos);
10335 else
10336 incl(&pos);
10337 }
10338 foundpos = pos;
10339
10340 /* clear the start flag to avoid getting stuck here */
10341 options &= ~SEARCH_START;
10342
10343 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010010344 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010345 {
10346 save_pos = curwin->w_cursor;
10347 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010010348 err = FALSE;
10349 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010350 curwin->w_cursor = save_pos;
10351 if (err)
10352 {
10353 /* Evaluating {skip} caused an error, break here. */
10354 curwin->w_cursor = save_cursor;
10355 retval = -1;
10356 break;
10357 }
10358 if (r)
10359 continue;
10360 }
10361
10362 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
10363 {
10364 /* Found end when searching backwards or start when searching
10365 * forward: nested pair. */
10366 ++nest;
10367 pat = pat2; /* nested, don't search for middle */
10368 }
10369 else
10370 {
10371 /* Found end when searching forward or start when searching
10372 * backward: end of (nested) pair; or found middle in outer pair. */
10373 if (--nest == 1)
10374 pat = pat3; /* outer level, search for middle */
10375 }
10376
10377 if (nest == 0)
10378 {
10379 /* Found the match: return matchcount or line number. */
10380 if (flags & SP_RETCOUNT)
10381 ++retval;
10382 else
10383 retval = pos.lnum;
10384 if (flags & SP_SETPCMARK)
10385 setpcmark();
10386 curwin->w_cursor = pos;
10387 if (!(flags & SP_REPEAT))
10388 break;
10389 nest = 1; /* search for next unmatched */
10390 }
10391 }
10392
10393 if (match_pos != NULL)
10394 {
10395 /* Store the match cursor position */
10396 match_pos->lnum = curwin->w_cursor.lnum;
10397 match_pos->col = curwin->w_cursor.col + 1;
10398 }
10399
10400 /* If 'n' flag is used or search failed: restore cursor position. */
10401 if ((flags & SP_NOMOVE) || retval == 0)
10402 curwin->w_cursor = save_cursor;
10403
10404theend:
10405 vim_free(pat2);
10406 vim_free(pat3);
10407 if (p_cpo == empty_option)
10408 p_cpo = save_cpo;
10409 else
10410 /* Darn, evaluating the {skip} expression changed the value. */
10411 free_string_option(save_cpo);
10412
10413 return retval;
10414}
10415
10416/*
10417 * "searchpos()" function
10418 */
10419 static void
10420f_searchpos(typval_T *argvars, typval_T *rettv)
10421{
10422 pos_T match_pos;
10423 int lnum = 0;
10424 int col = 0;
10425 int n;
10426 int flags = 0;
10427
10428 if (rettv_list_alloc(rettv) == FAIL)
10429 return;
10430
10431 n = search_cmn(argvars, &match_pos, &flags);
10432 if (n > 0)
10433 {
10434 lnum = match_pos.lnum;
10435 col = match_pos.col;
10436 }
10437
10438 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10439 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10440 if (flags & SP_SUBPAT)
10441 list_append_number(rettv->vval.v_list, (varnumber_T)n);
10442}
10443
10444 static void
10445f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
10446{
10447#ifdef FEAT_CLIENTSERVER
10448 char_u buf[NUMBUFLEN];
10449 char_u *server = get_tv_string_chk(&argvars[0]);
10450 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
10451
10452 rettv->vval.v_number = -1;
10453 if (server == NULL || reply == NULL)
10454 return;
10455 if (check_restricted() || check_secure())
10456 return;
10457# ifdef FEAT_X11
10458 if (check_connection() == FAIL)
10459 return;
10460# endif
10461
10462 if (serverSendReply(server, reply) < 0)
10463 {
10464 EMSG(_("E258: Unable to send to client"));
10465 return;
10466 }
10467 rettv->vval.v_number = 0;
10468#else
10469 rettv->vval.v_number = -1;
10470#endif
10471}
10472
10473 static void
10474f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
10475{
10476 char_u *r = NULL;
10477
10478#ifdef FEAT_CLIENTSERVER
10479# ifdef WIN32
10480 r = serverGetVimNames();
10481# else
10482 make_connection();
10483 if (X_DISPLAY != NULL)
10484 r = serverGetVimNames(X_DISPLAY);
10485# endif
10486#endif
10487 rettv->v_type = VAR_STRING;
10488 rettv->vval.v_string = r;
10489}
10490
10491/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010492 * "setbufline()" function
10493 */
10494 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020010495f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010496{
10497 linenr_T lnum;
10498 buf_T *buf;
10499
10500 buf = get_buf_tv(&argvars[0], FALSE);
10501 if (buf == NULL)
10502 rettv->vval.v_number = 1; /* FAIL */
10503 else
10504 {
10505 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020010506 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010507 }
10508}
10509
10510/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010511 * "setbufvar()" function
10512 */
10513 static void
10514f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10515{
10516 buf_T *buf;
10517 char_u *varname, *bufvarname;
10518 typval_T *varp;
10519 char_u nbuf[NUMBUFLEN];
10520
10521 if (check_restricted() || check_secure())
10522 return;
10523 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10524 varname = get_tv_string_chk(&argvars[1]);
10525 buf = get_buf_tv(&argvars[0], FALSE);
10526 varp = &argvars[2];
10527
10528 if (buf != NULL && varname != NULL && varp != NULL)
10529 {
10530 if (*varname == '&')
10531 {
10532 long numval;
10533 char_u *strval;
10534 int error = FALSE;
10535 aco_save_T aco;
10536
10537 /* set curbuf to be our buf, temporarily */
10538 aucmd_prepbuf(&aco, buf);
10539
10540 ++varname;
10541 numval = (long)get_tv_number_chk(varp, &error);
10542 strval = get_tv_string_buf_chk(varp, nbuf);
10543 if (!error && strval != NULL)
10544 set_option_value(varname, numval, strval, OPT_LOCAL);
10545
10546 /* reset notion of buffer */
10547 aucmd_restbuf(&aco);
10548 }
10549 else
10550 {
10551 buf_T *save_curbuf = curbuf;
10552
10553 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
10554 if (bufvarname != NULL)
10555 {
10556 curbuf = buf;
10557 STRCPY(bufvarname, "b:");
10558 STRCPY(bufvarname + 2, varname);
10559 set_var(bufvarname, varp, TRUE);
10560 vim_free(bufvarname);
10561 curbuf = save_curbuf;
10562 }
10563 }
10564 }
10565}
10566
10567 static void
10568f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10569{
10570 dict_T *d;
10571 dictitem_T *di;
10572 char_u *csearch;
10573
10574 if (argvars[0].v_type != VAR_DICT)
10575 {
10576 EMSG(_(e_dictreq));
10577 return;
10578 }
10579
10580 if ((d = argvars[0].vval.v_dict) != NULL)
10581 {
10582 csearch = get_dict_string(d, (char_u *)"char", FALSE);
10583 if (csearch != NULL)
10584 {
10585#ifdef FEAT_MBYTE
10586 if (enc_utf8)
10587 {
10588 int pcc[MAX_MCO];
10589 int c = utfc_ptr2char(csearch, pcc);
10590
10591 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
10592 }
10593 else
10594#endif
10595 set_last_csearch(PTR2CHAR(csearch),
10596 csearch, MB_PTR2LEN(csearch));
10597 }
10598
10599 di = dict_find(d, (char_u *)"forward", -1);
10600 if (di != NULL)
10601 set_csearch_direction((int)get_tv_number(&di->di_tv)
10602 ? FORWARD : BACKWARD);
10603
10604 di = dict_find(d, (char_u *)"until", -1);
10605 if (di != NULL)
10606 set_csearch_until(!!get_tv_number(&di->di_tv));
10607 }
10608}
10609
10610/*
10611 * "setcmdpos()" function
10612 */
10613 static void
10614f_setcmdpos(typval_T *argvars, typval_T *rettv)
10615{
10616 int pos = (int)get_tv_number(&argvars[0]) - 1;
10617
10618 if (pos >= 0)
10619 rettv->vval.v_number = set_cmdline_pos(pos);
10620}
10621
10622/*
10623 * "setfperm({fname}, {mode})" function
10624 */
10625 static void
10626f_setfperm(typval_T *argvars, typval_T *rettv)
10627{
10628 char_u *fname;
10629 char_u modebuf[NUMBUFLEN];
10630 char_u *mode_str;
10631 int i;
10632 int mask;
10633 int mode = 0;
10634
10635 rettv->vval.v_number = 0;
10636 fname = get_tv_string_chk(&argvars[0]);
10637 if (fname == NULL)
10638 return;
10639 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
10640 if (mode_str == NULL)
10641 return;
10642 if (STRLEN(mode_str) != 9)
10643 {
10644 EMSG2(_(e_invarg2), mode_str);
10645 return;
10646 }
10647
10648 mask = 1;
10649 for (i = 8; i >= 0; --i)
10650 {
10651 if (mode_str[i] != '-')
10652 mode |= mask;
10653 mask = mask << 1;
10654 }
10655 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
10656}
10657
10658/*
10659 * "setline()" function
10660 */
10661 static void
10662f_setline(typval_T *argvars, typval_T *rettv)
10663{
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010664 linenr_T lnum = get_tv_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010665
Bram Moolenaarca851592018-06-06 21:04:07 +020010666 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010667}
10668
Bram Moolenaard823fa92016-08-12 16:29:27 +020010669static 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 +020010670
10671/*
10672 * Used by "setqflist()" and "setloclist()" functions
10673 */
10674 static void
10675set_qf_ll_list(
10676 win_T *wp UNUSED,
10677 typval_T *list_arg UNUSED,
10678 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020010679 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010680 typval_T *rettv)
10681{
10682#ifdef FEAT_QUICKFIX
10683 static char *e_invact = N_("E927: Invalid action: '%s'");
10684 char_u *act;
10685 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010686 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010687#endif
10688
10689 rettv->vval.v_number = -1;
10690
10691#ifdef FEAT_QUICKFIX
10692 if (list_arg->v_type != VAR_LIST)
10693 EMSG(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010694 else if (recursive != 0)
10695 EMSG(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010696 else
10697 {
10698 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010699 dict_T *d = NULL;
10700 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010701
10702 if (action_arg->v_type == VAR_STRING)
10703 {
10704 act = get_tv_string_chk(action_arg);
10705 if (act == NULL)
10706 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020010707 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
10708 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010709 action = *act;
10710 else
10711 EMSG2(_(e_invact), act);
10712 }
10713 else if (action_arg->v_type == VAR_UNKNOWN)
10714 action = ' ';
10715 else
10716 EMSG(_(e_stringreq));
10717
Bram Moolenaard823fa92016-08-12 16:29:27 +020010718 if (action_arg->v_type != VAR_UNKNOWN
10719 && what_arg->v_type != VAR_UNKNOWN)
10720 {
10721 if (what_arg->v_type == VAR_DICT)
10722 d = what_arg->vval.v_dict;
10723 else
10724 {
10725 EMSG(_(e_dictreq));
10726 valid_dict = FALSE;
10727 }
10728 }
10729
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010730 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010731 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010732 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
10733 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010734 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010735 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010736 }
10737#endif
10738}
10739
10740/*
10741 * "setloclist()" function
10742 */
10743 static void
10744f_setloclist(typval_T *argvars, typval_T *rettv)
10745{
10746 win_T *win;
10747
10748 rettv->vval.v_number = -1;
10749
10750 win = find_win_by_nr(&argvars[0], NULL);
10751 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020010752 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010753}
10754
10755/*
10756 * "setmatches()" function
10757 */
10758 static void
10759f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10760{
10761#ifdef FEAT_SEARCH_EXTRA
10762 list_T *l;
10763 listitem_T *li;
10764 dict_T *d;
10765 list_T *s = NULL;
10766
10767 rettv->vval.v_number = -1;
10768 if (argvars[0].v_type != VAR_LIST)
10769 {
10770 EMSG(_(e_listreq));
10771 return;
10772 }
10773 if ((l = argvars[0].vval.v_list) != NULL)
10774 {
10775
10776 /* To some extent make sure that we are dealing with a list from
10777 * "getmatches()". */
10778 li = l->lv_first;
10779 while (li != NULL)
10780 {
10781 if (li->li_tv.v_type != VAR_DICT
10782 || (d = li->li_tv.vval.v_dict) == NULL)
10783 {
10784 EMSG(_(e_invarg));
10785 return;
10786 }
10787 if (!(dict_find(d, (char_u *)"group", -1) != NULL
10788 && (dict_find(d, (char_u *)"pattern", -1) != NULL
10789 || dict_find(d, (char_u *)"pos1", -1) != NULL)
10790 && dict_find(d, (char_u *)"priority", -1) != NULL
10791 && dict_find(d, (char_u *)"id", -1) != NULL))
10792 {
10793 EMSG(_(e_invarg));
10794 return;
10795 }
10796 li = li->li_next;
10797 }
10798
10799 clear_matches(curwin);
10800 li = l->lv_first;
10801 while (li != NULL)
10802 {
10803 int i = 0;
10804 char_u buf[5];
10805 dictitem_T *di;
10806 char_u *group;
10807 int priority;
10808 int id;
10809 char_u *conceal;
10810
10811 d = li->li_tv.vval.v_dict;
10812 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
10813 {
10814 if (s == NULL)
10815 {
10816 s = list_alloc();
10817 if (s == NULL)
10818 return;
10819 }
10820
10821 /* match from matchaddpos() */
10822 for (i = 1; i < 9; i++)
10823 {
10824 sprintf((char *)buf, (char *)"pos%d", i);
10825 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
10826 {
10827 if (di->di_tv.v_type != VAR_LIST)
10828 return;
10829
10830 list_append_tv(s, &di->di_tv);
10831 s->lv_refcount++;
10832 }
10833 else
10834 break;
10835 }
10836 }
10837
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010838 group = get_dict_string(d, (char_u *)"group", TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010839 priority = (int)get_dict_number(d, (char_u *)"priority");
10840 id = (int)get_dict_number(d, (char_u *)"id");
10841 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010842 ? get_dict_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010843 : NULL;
10844 if (i == 0)
10845 {
10846 match_add(curwin, group,
10847 get_dict_string(d, (char_u *)"pattern", FALSE),
10848 priority, id, NULL, conceal);
10849 }
10850 else
10851 {
10852 match_add(curwin, group, NULL, priority, id, s, conceal);
10853 list_unref(s);
10854 s = NULL;
10855 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010856 vim_free(group);
10857 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010858
10859 li = li->li_next;
10860 }
10861 rettv->vval.v_number = 0;
10862 }
10863#endif
10864}
10865
10866/*
10867 * "setpos()" function
10868 */
10869 static void
10870f_setpos(typval_T *argvars, typval_T *rettv)
10871{
10872 pos_T pos;
10873 int fnum;
10874 char_u *name;
10875 colnr_T curswant = -1;
10876
10877 rettv->vval.v_number = -1;
10878 name = get_tv_string_chk(argvars);
10879 if (name != NULL)
10880 {
10881 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
10882 {
10883 if (--pos.col < 0)
10884 pos.col = 0;
10885 if (name[0] == '.' && name[1] == NUL)
10886 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010887 /* set cursor; "fnum" is ignored */
10888 curwin->w_cursor = pos;
10889 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010890 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010891 curwin->w_curswant = curswant - 1;
10892 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010893 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010894 check_cursor();
10895 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010896 }
10897 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
10898 {
10899 /* set mark */
10900 if (setmark_pos(name[1], &pos, fnum) == OK)
10901 rettv->vval.v_number = 0;
10902 }
10903 else
10904 EMSG(_(e_invarg));
10905 }
10906 }
10907}
10908
10909/*
10910 * "setqflist()" function
10911 */
10912 static void
10913f_setqflist(typval_T *argvars, typval_T *rettv)
10914{
Bram Moolenaard823fa92016-08-12 16:29:27 +020010915 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010916}
10917
10918/*
10919 * "setreg()" function
10920 */
10921 static void
10922f_setreg(typval_T *argvars, typval_T *rettv)
10923{
10924 int regname;
10925 char_u *strregname;
10926 char_u *stropt;
10927 char_u *strval;
10928 int append;
10929 char_u yank_type;
10930 long block_len;
10931
10932 block_len = -1;
10933 yank_type = MAUTO;
10934 append = FALSE;
10935
10936 strregname = get_tv_string_chk(argvars);
10937 rettv->vval.v_number = 1; /* FAIL is default */
10938
10939 if (strregname == NULL)
10940 return; /* type error; errmsg already given */
10941 regname = *strregname;
10942 if (regname == 0 || regname == '@')
10943 regname = '"';
10944
10945 if (argvars[2].v_type != VAR_UNKNOWN)
10946 {
10947 stropt = get_tv_string_chk(&argvars[2]);
10948 if (stropt == NULL)
10949 return; /* type error */
10950 for (; *stropt != NUL; ++stropt)
10951 switch (*stropt)
10952 {
10953 case 'a': case 'A': /* append */
10954 append = TRUE;
10955 break;
10956 case 'v': case 'c': /* character-wise selection */
10957 yank_type = MCHAR;
10958 break;
10959 case 'V': case 'l': /* line-wise selection */
10960 yank_type = MLINE;
10961 break;
10962 case 'b': case Ctrl_V: /* block-wise selection */
10963 yank_type = MBLOCK;
10964 if (VIM_ISDIGIT(stropt[1]))
10965 {
10966 ++stropt;
10967 block_len = getdigits(&stropt) - 1;
10968 --stropt;
10969 }
10970 break;
10971 }
10972 }
10973
10974 if (argvars[1].v_type == VAR_LIST)
10975 {
10976 char_u **lstval;
10977 char_u **allocval;
10978 char_u buf[NUMBUFLEN];
10979 char_u **curval;
10980 char_u **curallocval;
10981 list_T *ll = argvars[1].vval.v_list;
10982 listitem_T *li;
10983 int len;
10984
10985 /* If the list is NULL handle like an empty list. */
10986 len = ll == NULL ? 0 : ll->lv_len;
10987
10988 /* First half: use for pointers to result lines; second half: use for
10989 * pointers to allocated copies. */
10990 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
10991 if (lstval == NULL)
10992 return;
10993 curval = lstval;
10994 allocval = lstval + len + 2;
10995 curallocval = allocval;
10996
10997 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
10998 li = li->li_next)
10999 {
11000 strval = get_tv_string_buf_chk(&li->li_tv, buf);
11001 if (strval == NULL)
11002 goto free_lstval;
11003 if (strval == buf)
11004 {
11005 /* Need to make a copy, next get_tv_string_buf_chk() will
11006 * overwrite the string. */
11007 strval = vim_strsave(buf);
11008 if (strval == NULL)
11009 goto free_lstval;
11010 *curallocval++ = strval;
11011 }
11012 *curval++ = strval;
11013 }
11014 *curval++ = NULL;
11015
11016 write_reg_contents_lst(regname, lstval, -1,
11017 append, yank_type, block_len);
11018free_lstval:
11019 while (curallocval > allocval)
11020 vim_free(*--curallocval);
11021 vim_free(lstval);
11022 }
11023 else
11024 {
11025 strval = get_tv_string_chk(&argvars[1]);
11026 if (strval == NULL)
11027 return;
11028 write_reg_contents_ex(regname, strval, -1,
11029 append, yank_type, block_len);
11030 }
11031 rettv->vval.v_number = 0;
11032}
11033
11034/*
11035 * "settabvar()" function
11036 */
11037 static void
11038f_settabvar(typval_T *argvars, typval_T *rettv)
11039{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011040 tabpage_T *save_curtab;
11041 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011042 char_u *varname, *tabvarname;
11043 typval_T *varp;
11044
11045 rettv->vval.v_number = 0;
11046
11047 if (check_restricted() || check_secure())
11048 return;
11049
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011050 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011051 varname = get_tv_string_chk(&argvars[1]);
11052 varp = &argvars[2];
11053
Bram Moolenaar4033c552017-09-16 20:54:51 +020011054 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011055 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011056 save_curtab = curtab;
11057 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011058
11059 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
11060 if (tabvarname != NULL)
11061 {
11062 STRCPY(tabvarname, "t:");
11063 STRCPY(tabvarname + 2, varname);
11064 set_var(tabvarname, varp, TRUE);
11065 vim_free(tabvarname);
11066 }
11067
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011068 /* Restore current tabpage */
11069 if (valid_tabpage(save_curtab))
11070 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011071 }
11072}
11073
11074/*
11075 * "settabwinvar()" function
11076 */
11077 static void
11078f_settabwinvar(typval_T *argvars, typval_T *rettv)
11079{
11080 setwinvar(argvars, rettv, 1);
11081}
11082
11083/*
11084 * "setwinvar()" function
11085 */
11086 static void
11087f_setwinvar(typval_T *argvars, typval_T *rettv)
11088{
11089 setwinvar(argvars, rettv, 0);
11090}
11091
11092#ifdef FEAT_CRYPT
11093/*
11094 * "sha256({string})" function
11095 */
11096 static void
11097f_sha256(typval_T *argvars, typval_T *rettv)
11098{
11099 char_u *p;
11100
11101 p = get_tv_string(&argvars[0]);
11102 rettv->vval.v_string = vim_strsave(
11103 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
11104 rettv->v_type = VAR_STRING;
11105}
11106#endif /* FEAT_CRYPT */
11107
11108/*
11109 * "shellescape({string})" function
11110 */
11111 static void
11112f_shellescape(typval_T *argvars, typval_T *rettv)
11113{
Bram Moolenaar20615522017-06-05 18:46:26 +020011114 int do_special = non_zero_arg(&argvars[1]);
11115
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011116 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar20615522017-06-05 18:46:26 +020011117 get_tv_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011118 rettv->v_type = VAR_STRING;
11119}
11120
11121/*
11122 * shiftwidth() function
11123 */
11124 static void
11125f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
11126{
11127 rettv->vval.v_number = get_sw_value(curbuf);
11128}
11129
11130/*
11131 * "simplify()" function
11132 */
11133 static void
11134f_simplify(typval_T *argvars, typval_T *rettv)
11135{
11136 char_u *p;
11137
11138 p = get_tv_string(&argvars[0]);
11139 rettv->vval.v_string = vim_strsave(p);
11140 simplify_filename(rettv->vval.v_string); /* simplify in place */
11141 rettv->v_type = VAR_STRING;
11142}
11143
11144#ifdef FEAT_FLOAT
11145/*
11146 * "sin()" function
11147 */
11148 static void
11149f_sin(typval_T *argvars, typval_T *rettv)
11150{
11151 float_T f = 0.0;
11152
11153 rettv->v_type = VAR_FLOAT;
11154 if (get_float_arg(argvars, &f) == OK)
11155 rettv->vval.v_float = sin(f);
11156 else
11157 rettv->vval.v_float = 0.0;
11158}
11159
11160/*
11161 * "sinh()" function
11162 */
11163 static void
11164f_sinh(typval_T *argvars, typval_T *rettv)
11165{
11166 float_T f = 0.0;
11167
11168 rettv->v_type = VAR_FLOAT;
11169 if (get_float_arg(argvars, &f) == OK)
11170 rettv->vval.v_float = sinh(f);
11171 else
11172 rettv->vval.v_float = 0.0;
11173}
11174#endif
11175
11176static int
11177#ifdef __BORLANDC__
11178 _RTLENTRYF
11179#endif
11180 item_compare(const void *s1, const void *s2);
11181static int
11182#ifdef __BORLANDC__
11183 _RTLENTRYF
11184#endif
11185 item_compare2(const void *s1, const void *s2);
11186
11187/* struct used in the array that's given to qsort() */
11188typedef struct
11189{
11190 listitem_T *item;
11191 int idx;
11192} sortItem_T;
11193
11194/* struct storing information about current sort */
11195typedef struct
11196{
11197 int item_compare_ic;
11198 int item_compare_numeric;
11199 int item_compare_numbers;
11200#ifdef FEAT_FLOAT
11201 int item_compare_float;
11202#endif
11203 char_u *item_compare_func;
11204 partial_T *item_compare_partial;
11205 dict_T *item_compare_selfdict;
11206 int item_compare_func_err;
11207 int item_compare_keep_zero;
11208} sortinfo_T;
11209static sortinfo_T *sortinfo = NULL;
11210static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
11211#define ITEM_COMPARE_FAIL 999
11212
11213/*
11214 * Compare functions for f_sort() and f_uniq() below.
11215 */
11216 static int
11217#ifdef __BORLANDC__
11218_RTLENTRYF
11219#endif
11220item_compare(const void *s1, const void *s2)
11221{
11222 sortItem_T *si1, *si2;
11223 typval_T *tv1, *tv2;
11224 char_u *p1, *p2;
11225 char_u *tofree1 = NULL, *tofree2 = NULL;
11226 int res;
11227 char_u numbuf1[NUMBUFLEN];
11228 char_u numbuf2[NUMBUFLEN];
11229
11230 si1 = (sortItem_T *)s1;
11231 si2 = (sortItem_T *)s2;
11232 tv1 = &si1->item->li_tv;
11233 tv2 = &si2->item->li_tv;
11234
11235 if (sortinfo->item_compare_numbers)
11236 {
11237 varnumber_T v1 = get_tv_number(tv1);
11238 varnumber_T v2 = get_tv_number(tv2);
11239
11240 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11241 }
11242
11243#ifdef FEAT_FLOAT
11244 if (sortinfo->item_compare_float)
11245 {
11246 float_T v1 = get_tv_float(tv1);
11247 float_T v2 = get_tv_float(tv2);
11248
11249 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11250 }
11251#endif
11252
11253 /* tv2string() puts quotes around a string and allocates memory. Don't do
11254 * that for string variables. Use a single quote when comparing with a
11255 * non-string to do what the docs promise. */
11256 if (tv1->v_type == VAR_STRING)
11257 {
11258 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11259 p1 = (char_u *)"'";
11260 else
11261 p1 = tv1->vval.v_string;
11262 }
11263 else
11264 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
11265 if (tv2->v_type == VAR_STRING)
11266 {
11267 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11268 p2 = (char_u *)"'";
11269 else
11270 p2 = tv2->vval.v_string;
11271 }
11272 else
11273 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
11274 if (p1 == NULL)
11275 p1 = (char_u *)"";
11276 if (p2 == NULL)
11277 p2 = (char_u *)"";
11278 if (!sortinfo->item_compare_numeric)
11279 {
11280 if (sortinfo->item_compare_ic)
11281 res = STRICMP(p1, p2);
11282 else
11283 res = STRCMP(p1, p2);
11284 }
11285 else
11286 {
11287 double n1, n2;
11288 n1 = strtod((char *)p1, (char **)&p1);
11289 n2 = strtod((char *)p2, (char **)&p2);
11290 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
11291 }
11292
11293 /* When the result would be zero, compare the item indexes. Makes the
11294 * sort stable. */
11295 if (res == 0 && !sortinfo->item_compare_keep_zero)
11296 res = si1->idx > si2->idx ? 1 : -1;
11297
11298 vim_free(tofree1);
11299 vim_free(tofree2);
11300 return res;
11301}
11302
11303 static int
11304#ifdef __BORLANDC__
11305_RTLENTRYF
11306#endif
11307item_compare2(const void *s1, const void *s2)
11308{
11309 sortItem_T *si1, *si2;
11310 int res;
11311 typval_T rettv;
11312 typval_T argv[3];
11313 int dummy;
11314 char_u *func_name;
11315 partial_T *partial = sortinfo->item_compare_partial;
11316
11317 /* shortcut after failure in previous call; compare all items equal */
11318 if (sortinfo->item_compare_func_err)
11319 return 0;
11320
11321 si1 = (sortItem_T *)s1;
11322 si2 = (sortItem_T *)s2;
11323
11324 if (partial == NULL)
11325 func_name = sortinfo->item_compare_func;
11326 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020011327 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011328
11329 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
11330 * in the copy without changing the original list items. */
11331 copy_tv(&si1->item->li_tv, &argv[0]);
11332 copy_tv(&si2->item->li_tv, &argv[1]);
11333
11334 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
11335 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020011336 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011337 partial, sortinfo->item_compare_selfdict);
11338 clear_tv(&argv[0]);
11339 clear_tv(&argv[1]);
11340
11341 if (res == FAIL)
11342 res = ITEM_COMPARE_FAIL;
11343 else
11344 res = (int)get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
11345 if (sortinfo->item_compare_func_err)
11346 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
11347 clear_tv(&rettv);
11348
11349 /* When the result would be zero, compare the pointers themselves. Makes
11350 * the sort stable. */
11351 if (res == 0 && !sortinfo->item_compare_keep_zero)
11352 res = si1->idx > si2->idx ? 1 : -1;
11353
11354 return res;
11355}
11356
11357/*
11358 * "sort({list})" function
11359 */
11360 static void
11361do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
11362{
11363 list_T *l;
11364 listitem_T *li;
11365 sortItem_T *ptrs;
11366 sortinfo_T *old_sortinfo;
11367 sortinfo_T info;
11368 long len;
11369 long i;
11370
11371 /* Pointer to current info struct used in compare function. Save and
11372 * restore the current one for nested calls. */
11373 old_sortinfo = sortinfo;
11374 sortinfo = &info;
11375
11376 if (argvars[0].v_type != VAR_LIST)
11377 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
11378 else
11379 {
11380 l = argvars[0].vval.v_list;
11381 if (l == NULL || tv_check_lock(l->lv_lock,
11382 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
11383 TRUE))
11384 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011385 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011386
11387 len = list_len(l);
11388 if (len <= 1)
11389 goto theend; /* short list sorts pretty quickly */
11390
11391 info.item_compare_ic = FALSE;
11392 info.item_compare_numeric = FALSE;
11393 info.item_compare_numbers = FALSE;
11394#ifdef FEAT_FLOAT
11395 info.item_compare_float = FALSE;
11396#endif
11397 info.item_compare_func = NULL;
11398 info.item_compare_partial = NULL;
11399 info.item_compare_selfdict = NULL;
11400 if (argvars[1].v_type != VAR_UNKNOWN)
11401 {
11402 /* optional second argument: {func} */
11403 if (argvars[1].v_type == VAR_FUNC)
11404 info.item_compare_func = argvars[1].vval.v_string;
11405 else if (argvars[1].v_type == VAR_PARTIAL)
11406 info.item_compare_partial = argvars[1].vval.v_partial;
11407 else
11408 {
11409 int error = FALSE;
11410
11411 i = (long)get_tv_number_chk(&argvars[1], &error);
11412 if (error)
11413 goto theend; /* type error; errmsg already given */
11414 if (i == 1)
11415 info.item_compare_ic = TRUE;
11416 else if (argvars[1].v_type != VAR_NUMBER)
11417 info.item_compare_func = get_tv_string(&argvars[1]);
11418 else if (i != 0)
11419 {
11420 EMSG(_(e_invarg));
11421 goto theend;
11422 }
11423 if (info.item_compare_func != NULL)
11424 {
11425 if (*info.item_compare_func == NUL)
11426 {
11427 /* empty string means default sort */
11428 info.item_compare_func = NULL;
11429 }
11430 else if (STRCMP(info.item_compare_func, "n") == 0)
11431 {
11432 info.item_compare_func = NULL;
11433 info.item_compare_numeric = TRUE;
11434 }
11435 else if (STRCMP(info.item_compare_func, "N") == 0)
11436 {
11437 info.item_compare_func = NULL;
11438 info.item_compare_numbers = TRUE;
11439 }
11440#ifdef FEAT_FLOAT
11441 else if (STRCMP(info.item_compare_func, "f") == 0)
11442 {
11443 info.item_compare_func = NULL;
11444 info.item_compare_float = TRUE;
11445 }
11446#endif
11447 else if (STRCMP(info.item_compare_func, "i") == 0)
11448 {
11449 info.item_compare_func = NULL;
11450 info.item_compare_ic = TRUE;
11451 }
11452 }
11453 }
11454
11455 if (argvars[2].v_type != VAR_UNKNOWN)
11456 {
11457 /* optional third argument: {dict} */
11458 if (argvars[2].v_type != VAR_DICT)
11459 {
11460 EMSG(_(e_dictreq));
11461 goto theend;
11462 }
11463 info.item_compare_selfdict = argvars[2].vval.v_dict;
11464 }
11465 }
11466
11467 /* Make an array with each entry pointing to an item in the List. */
11468 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
11469 if (ptrs == NULL)
11470 goto theend;
11471
11472 i = 0;
11473 if (sort)
11474 {
11475 /* sort(): ptrs will be the list to sort */
11476 for (li = l->lv_first; li != NULL; li = li->li_next)
11477 {
11478 ptrs[i].item = li;
11479 ptrs[i].idx = i;
11480 ++i;
11481 }
11482
11483 info.item_compare_func_err = FALSE;
11484 info.item_compare_keep_zero = FALSE;
11485 /* test the compare function */
11486 if ((info.item_compare_func != NULL
11487 || info.item_compare_partial != NULL)
11488 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
11489 == ITEM_COMPARE_FAIL)
11490 EMSG(_("E702: Sort compare function failed"));
11491 else
11492 {
11493 /* Sort the array with item pointers. */
11494 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
11495 info.item_compare_func == NULL
11496 && info.item_compare_partial == NULL
11497 ? item_compare : item_compare2);
11498
11499 if (!info.item_compare_func_err)
11500 {
11501 /* Clear the List and append the items in sorted order. */
11502 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
11503 l->lv_len = 0;
11504 for (i = 0; i < len; ++i)
11505 list_append(l, ptrs[i].item);
11506 }
11507 }
11508 }
11509 else
11510 {
11511 int (*item_compare_func_ptr)(const void *, const void *);
11512
11513 /* f_uniq(): ptrs will be a stack of items to remove */
11514 info.item_compare_func_err = FALSE;
11515 info.item_compare_keep_zero = TRUE;
11516 item_compare_func_ptr = info.item_compare_func != NULL
11517 || info.item_compare_partial != NULL
11518 ? item_compare2 : item_compare;
11519
11520 for (li = l->lv_first; li != NULL && li->li_next != NULL;
11521 li = li->li_next)
11522 {
11523 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
11524 == 0)
11525 ptrs[i++].item = li;
11526 if (info.item_compare_func_err)
11527 {
11528 EMSG(_("E882: Uniq compare function failed"));
11529 break;
11530 }
11531 }
11532
11533 if (!info.item_compare_func_err)
11534 {
11535 while (--i >= 0)
11536 {
11537 li = ptrs[i].item->li_next;
11538 ptrs[i].item->li_next = li->li_next;
11539 if (li->li_next != NULL)
11540 li->li_next->li_prev = ptrs[i].item;
11541 else
11542 l->lv_last = ptrs[i].item;
11543 list_fix_watch(l, li);
11544 listitem_free(li);
11545 l->lv_len--;
11546 }
11547 }
11548 }
11549
11550 vim_free(ptrs);
11551 }
11552theend:
11553 sortinfo = old_sortinfo;
11554}
11555
11556/*
11557 * "sort({list})" function
11558 */
11559 static void
11560f_sort(typval_T *argvars, typval_T *rettv)
11561{
11562 do_sort_uniq(argvars, rettv, TRUE);
11563}
11564
11565/*
11566 * "uniq({list})" function
11567 */
11568 static void
11569f_uniq(typval_T *argvars, typval_T *rettv)
11570{
11571 do_sort_uniq(argvars, rettv, FALSE);
11572}
11573
11574/*
11575 * "soundfold({word})" function
11576 */
11577 static void
11578f_soundfold(typval_T *argvars, typval_T *rettv)
11579{
11580 char_u *s;
11581
11582 rettv->v_type = VAR_STRING;
11583 s = get_tv_string(&argvars[0]);
11584#ifdef FEAT_SPELL
11585 rettv->vval.v_string = eval_soundfold(s);
11586#else
11587 rettv->vval.v_string = vim_strsave(s);
11588#endif
11589}
11590
11591/*
11592 * "spellbadword()" function
11593 */
11594 static void
11595f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
11596{
11597 char_u *word = (char_u *)"";
11598 hlf_T attr = HLF_COUNT;
11599 int len = 0;
11600
11601 if (rettv_list_alloc(rettv) == FAIL)
11602 return;
11603
11604#ifdef FEAT_SPELL
11605 if (argvars[0].v_type == VAR_UNKNOWN)
11606 {
11607 /* Find the start and length of the badly spelled word. */
11608 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
11609 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011610 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011611 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011612 curwin->w_set_curswant = TRUE;
11613 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011614 }
11615 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
11616 {
11617 char_u *str = get_tv_string_chk(&argvars[0]);
11618 int capcol = -1;
11619
11620 if (str != NULL)
11621 {
11622 /* Check the argument for spelling. */
11623 while (*str != NUL)
11624 {
11625 len = spell_check(curwin, str, &attr, &capcol, FALSE);
11626 if (attr != HLF_COUNT)
11627 {
11628 word = str;
11629 break;
11630 }
11631 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020011632 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011633 }
11634 }
11635 }
11636#endif
11637
11638 list_append_string(rettv->vval.v_list, word, len);
11639 list_append_string(rettv->vval.v_list, (char_u *)(
11640 attr == HLF_SPB ? "bad" :
11641 attr == HLF_SPR ? "rare" :
11642 attr == HLF_SPL ? "local" :
11643 attr == HLF_SPC ? "caps" :
11644 ""), -1);
11645}
11646
11647/*
11648 * "spellsuggest()" function
11649 */
11650 static void
11651f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
11652{
11653#ifdef FEAT_SPELL
11654 char_u *str;
11655 int typeerr = FALSE;
11656 int maxcount;
11657 garray_T ga;
11658 int i;
11659 listitem_T *li;
11660 int need_capital = FALSE;
11661#endif
11662
11663 if (rettv_list_alloc(rettv) == FAIL)
11664 return;
11665
11666#ifdef FEAT_SPELL
11667 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
11668 {
11669 str = get_tv_string(&argvars[0]);
11670 if (argvars[1].v_type != VAR_UNKNOWN)
11671 {
11672 maxcount = (int)get_tv_number_chk(&argvars[1], &typeerr);
11673 if (maxcount <= 0)
11674 return;
11675 if (argvars[2].v_type != VAR_UNKNOWN)
11676 {
11677 need_capital = (int)get_tv_number_chk(&argvars[2], &typeerr);
11678 if (typeerr)
11679 return;
11680 }
11681 }
11682 else
11683 maxcount = 25;
11684
11685 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
11686
11687 for (i = 0; i < ga.ga_len; ++i)
11688 {
11689 str = ((char_u **)ga.ga_data)[i];
11690
11691 li = listitem_alloc();
11692 if (li == NULL)
11693 vim_free(str);
11694 else
11695 {
11696 li->li_tv.v_type = VAR_STRING;
11697 li->li_tv.v_lock = 0;
11698 li->li_tv.vval.v_string = str;
11699 list_append(rettv->vval.v_list, li);
11700 }
11701 }
11702 ga_clear(&ga);
11703 }
11704#endif
11705}
11706
11707 static void
11708f_split(typval_T *argvars, typval_T *rettv)
11709{
11710 char_u *str;
11711 char_u *end;
11712 char_u *pat = NULL;
11713 regmatch_T regmatch;
11714 char_u patbuf[NUMBUFLEN];
11715 char_u *save_cpo;
11716 int match;
11717 colnr_T col = 0;
11718 int keepempty = FALSE;
11719 int typeerr = FALSE;
11720
11721 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11722 save_cpo = p_cpo;
11723 p_cpo = (char_u *)"";
11724
11725 str = get_tv_string(&argvars[0]);
11726 if (argvars[1].v_type != VAR_UNKNOWN)
11727 {
11728 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11729 if (pat == NULL)
11730 typeerr = TRUE;
11731 if (argvars[2].v_type != VAR_UNKNOWN)
11732 keepempty = (int)get_tv_number_chk(&argvars[2], &typeerr);
11733 }
11734 if (pat == NULL || *pat == NUL)
11735 pat = (char_u *)"[\\x01- ]\\+";
11736
11737 if (rettv_list_alloc(rettv) == FAIL)
11738 return;
11739 if (typeerr)
11740 return;
11741
11742 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11743 if (regmatch.regprog != NULL)
11744 {
11745 regmatch.rm_ic = FALSE;
11746 while (*str != NUL || keepempty)
11747 {
11748 if (*str == NUL)
11749 match = FALSE; /* empty item at the end */
11750 else
11751 match = vim_regexec_nl(&regmatch, str, col);
11752 if (match)
11753 end = regmatch.startp[0];
11754 else
11755 end = str + STRLEN(str);
11756 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
11757 && *str != NUL && match && end < regmatch.endp[0]))
11758 {
11759 if (list_append_string(rettv->vval.v_list, str,
11760 (int)(end - str)) == FAIL)
11761 break;
11762 }
11763 if (!match)
11764 break;
11765 /* Advance to just after the match. */
11766 if (regmatch.endp[0] > str)
11767 col = 0;
11768 else
11769 {
11770 /* Don't get stuck at the same match. */
11771#ifdef FEAT_MBYTE
11772 col = (*mb_ptr2len)(regmatch.endp[0]);
11773#else
11774 col = 1;
11775#endif
11776 }
11777 str = regmatch.endp[0];
11778 }
11779
11780 vim_regfree(regmatch.regprog);
11781 }
11782
11783 p_cpo = save_cpo;
11784}
11785
11786#ifdef FEAT_FLOAT
11787/*
11788 * "sqrt()" function
11789 */
11790 static void
11791f_sqrt(typval_T *argvars, typval_T *rettv)
11792{
11793 float_T f = 0.0;
11794
11795 rettv->v_type = VAR_FLOAT;
11796 if (get_float_arg(argvars, &f) == OK)
11797 rettv->vval.v_float = sqrt(f);
11798 else
11799 rettv->vval.v_float = 0.0;
11800}
11801
11802/*
11803 * "str2float()" function
11804 */
11805 static void
11806f_str2float(typval_T *argvars, typval_T *rettv)
11807{
11808 char_u *p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011809 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011810
Bram Moolenaar08243d22017-01-10 16:12:29 +010011811 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011812 p = skipwhite(p + 1);
11813 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011814 if (isneg)
11815 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011816 rettv->v_type = VAR_FLOAT;
11817}
11818#endif
11819
11820/*
11821 * "str2nr()" function
11822 */
11823 static void
11824f_str2nr(typval_T *argvars, typval_T *rettv)
11825{
11826 int base = 10;
11827 char_u *p;
11828 varnumber_T n;
11829 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010011830 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011831
11832 if (argvars[1].v_type != VAR_UNKNOWN)
11833 {
11834 base = (int)get_tv_number(&argvars[1]);
11835 if (base != 2 && base != 8 && base != 10 && base != 16)
11836 {
11837 EMSG(_(e_invarg));
11838 return;
11839 }
11840 }
11841
11842 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011843 isneg = (*p == '-');
11844 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011845 p = skipwhite(p + 1);
11846 switch (base)
11847 {
11848 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
11849 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
11850 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
11851 default: what = 0;
11852 }
11853 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011854 if (isneg)
11855 rettv->vval.v_number = -n;
11856 else
11857 rettv->vval.v_number = n;
11858
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011859}
11860
11861#ifdef HAVE_STRFTIME
11862/*
11863 * "strftime({format}[, {time}])" function
11864 */
11865 static void
11866f_strftime(typval_T *argvars, typval_T *rettv)
11867{
11868 char_u result_buf[256];
11869 struct tm *curtime;
11870 time_t seconds;
11871 char_u *p;
11872
11873 rettv->v_type = VAR_STRING;
11874
11875 p = get_tv_string(&argvars[0]);
11876 if (argvars[1].v_type == VAR_UNKNOWN)
11877 seconds = time(NULL);
11878 else
11879 seconds = (time_t)get_tv_number(&argvars[1]);
11880 curtime = localtime(&seconds);
11881 /* MSVC returns NULL for an invalid value of seconds. */
11882 if (curtime == NULL)
11883 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
11884 else
11885 {
11886# ifdef FEAT_MBYTE
11887 vimconv_T conv;
11888 char_u *enc;
11889
11890 conv.vc_type = CONV_NONE;
11891 enc = enc_locale();
11892 convert_setup(&conv, p_enc, enc);
11893 if (conv.vc_type != CONV_NONE)
11894 p = string_convert(&conv, p, NULL);
11895# endif
11896 if (p != NULL)
11897 (void)strftime((char *)result_buf, sizeof(result_buf),
11898 (char *)p, curtime);
11899 else
11900 result_buf[0] = NUL;
11901
11902# ifdef FEAT_MBYTE
11903 if (conv.vc_type != CONV_NONE)
11904 vim_free(p);
11905 convert_setup(&conv, enc, p_enc);
11906 if (conv.vc_type != CONV_NONE)
11907 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
11908 else
11909# endif
11910 rettv->vval.v_string = vim_strsave(result_buf);
11911
11912# ifdef FEAT_MBYTE
11913 /* Release conversion descriptors */
11914 convert_setup(&conv, NULL, NULL);
11915 vim_free(enc);
11916# endif
11917 }
11918}
11919#endif
11920
11921/*
11922 * "strgetchar()" function
11923 */
11924 static void
11925f_strgetchar(typval_T *argvars, typval_T *rettv)
11926{
11927 char_u *str;
11928 int len;
11929 int error = FALSE;
11930 int charidx;
11931
11932 rettv->vval.v_number = -1;
11933 str = get_tv_string_chk(&argvars[0]);
11934 if (str == NULL)
11935 return;
11936 len = (int)STRLEN(str);
11937 charidx = (int)get_tv_number_chk(&argvars[1], &error);
11938 if (error)
11939 return;
11940#ifdef FEAT_MBYTE
11941 {
11942 int byteidx = 0;
11943
11944 while (charidx >= 0 && byteidx < len)
11945 {
11946 if (charidx == 0)
11947 {
11948 rettv->vval.v_number = mb_ptr2char(str + byteidx);
11949 break;
11950 }
11951 --charidx;
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011952 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011953 }
11954 }
11955#else
11956 if (charidx < len)
11957 rettv->vval.v_number = str[charidx];
11958#endif
11959}
11960
11961/*
11962 * "stridx()" function
11963 */
11964 static void
11965f_stridx(typval_T *argvars, typval_T *rettv)
11966{
11967 char_u buf[NUMBUFLEN];
11968 char_u *needle;
11969 char_u *haystack;
11970 char_u *save_haystack;
11971 char_u *pos;
11972 int start_idx;
11973
11974 needle = get_tv_string_chk(&argvars[1]);
11975 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
11976 rettv->vval.v_number = -1;
11977 if (needle == NULL || haystack == NULL)
11978 return; /* type error; errmsg already given */
11979
11980 if (argvars[2].v_type != VAR_UNKNOWN)
11981 {
11982 int error = FALSE;
11983
11984 start_idx = (int)get_tv_number_chk(&argvars[2], &error);
11985 if (error || start_idx >= (int)STRLEN(haystack))
11986 return;
11987 if (start_idx >= 0)
11988 haystack += start_idx;
11989 }
11990
11991 pos = (char_u *)strstr((char *)haystack, (char *)needle);
11992 if (pos != NULL)
11993 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
11994}
11995
11996/*
11997 * "string()" function
11998 */
11999 static void
12000f_string(typval_T *argvars, typval_T *rettv)
12001{
12002 char_u *tofree;
12003 char_u numbuf[NUMBUFLEN];
12004
12005 rettv->v_type = VAR_STRING;
12006 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
12007 get_copyID());
12008 /* Make a copy if we have a value but it's not in allocated memory. */
12009 if (rettv->vval.v_string != NULL && tofree == NULL)
12010 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
12011}
12012
12013/*
12014 * "strlen()" function
12015 */
12016 static void
12017f_strlen(typval_T *argvars, typval_T *rettv)
12018{
12019 rettv->vval.v_number = (varnumber_T)(STRLEN(
12020 get_tv_string(&argvars[0])));
12021}
12022
12023/*
12024 * "strchars()" function
12025 */
12026 static void
12027f_strchars(typval_T *argvars, typval_T *rettv)
12028{
12029 char_u *s = get_tv_string(&argvars[0]);
12030 int skipcc = 0;
12031#ifdef FEAT_MBYTE
12032 varnumber_T len = 0;
12033 int (*func_mb_ptr2char_adv)(char_u **pp);
12034#endif
12035
12036 if (argvars[1].v_type != VAR_UNKNOWN)
12037 skipcc = (int)get_tv_number_chk(&argvars[1], NULL);
12038 if (skipcc < 0 || skipcc > 1)
12039 EMSG(_(e_invarg));
12040 else
12041 {
12042#ifdef FEAT_MBYTE
12043 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
12044 while (*s != NUL)
12045 {
12046 func_mb_ptr2char_adv(&s);
12047 ++len;
12048 }
12049 rettv->vval.v_number = len;
12050#else
12051 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
12052#endif
12053 }
12054}
12055
12056/*
12057 * "strdisplaywidth()" function
12058 */
12059 static void
12060f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
12061{
12062 char_u *s = get_tv_string(&argvars[0]);
12063 int col = 0;
12064
12065 if (argvars[1].v_type != VAR_UNKNOWN)
12066 col = (int)get_tv_number(&argvars[1]);
12067
12068 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
12069}
12070
12071/*
12072 * "strwidth()" function
12073 */
12074 static void
12075f_strwidth(typval_T *argvars, typval_T *rettv)
12076{
12077 char_u *s = get_tv_string(&argvars[0]);
12078
12079 rettv->vval.v_number = (varnumber_T)(
12080#ifdef FEAT_MBYTE
12081 mb_string2cells(s, -1)
12082#else
12083 STRLEN(s)
12084#endif
12085 );
12086}
12087
12088/*
12089 * "strcharpart()" function
12090 */
12091 static void
12092f_strcharpart(typval_T *argvars, typval_T *rettv)
12093{
12094#ifdef FEAT_MBYTE
12095 char_u *p;
12096 int nchar;
12097 int nbyte = 0;
12098 int charlen;
12099 int len = 0;
12100 int slen;
12101 int error = FALSE;
12102
12103 p = get_tv_string(&argvars[0]);
12104 slen = (int)STRLEN(p);
12105
12106 nchar = (int)get_tv_number_chk(&argvars[1], &error);
12107 if (!error)
12108 {
12109 if (nchar > 0)
12110 while (nchar > 0 && nbyte < slen)
12111 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012112 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012113 --nchar;
12114 }
12115 else
12116 nbyte = nchar;
12117 if (argvars[2].v_type != VAR_UNKNOWN)
12118 {
12119 charlen = (int)get_tv_number(&argvars[2]);
12120 while (charlen > 0 && nbyte + len < slen)
12121 {
12122 int off = nbyte + len;
12123
12124 if (off < 0)
12125 len += 1;
12126 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012127 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012128 --charlen;
12129 }
12130 }
12131 else
12132 len = slen - nbyte; /* default: all bytes that are available. */
12133 }
12134
12135 /*
12136 * Only return the overlap between the specified part and the actual
12137 * string.
12138 */
12139 if (nbyte < 0)
12140 {
12141 len += nbyte;
12142 nbyte = 0;
12143 }
12144 else if (nbyte > slen)
12145 nbyte = slen;
12146 if (len < 0)
12147 len = 0;
12148 else if (nbyte + len > slen)
12149 len = slen - nbyte;
12150
12151 rettv->v_type = VAR_STRING;
12152 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
12153#else
12154 f_strpart(argvars, rettv);
12155#endif
12156}
12157
12158/*
12159 * "strpart()" function
12160 */
12161 static void
12162f_strpart(typval_T *argvars, typval_T *rettv)
12163{
12164 char_u *p;
12165 int n;
12166 int len;
12167 int slen;
12168 int error = FALSE;
12169
12170 p = get_tv_string(&argvars[0]);
12171 slen = (int)STRLEN(p);
12172
12173 n = (int)get_tv_number_chk(&argvars[1], &error);
12174 if (error)
12175 len = 0;
12176 else if (argvars[2].v_type != VAR_UNKNOWN)
12177 len = (int)get_tv_number(&argvars[2]);
12178 else
12179 len = slen - n; /* default len: all bytes that are available. */
12180
12181 /*
12182 * Only return the overlap between the specified part and the actual
12183 * string.
12184 */
12185 if (n < 0)
12186 {
12187 len += n;
12188 n = 0;
12189 }
12190 else if (n > slen)
12191 n = slen;
12192 if (len < 0)
12193 len = 0;
12194 else if (n + len > slen)
12195 len = slen - n;
12196
12197 rettv->v_type = VAR_STRING;
12198 rettv->vval.v_string = vim_strnsave(p + n, len);
12199}
12200
12201/*
12202 * "strridx()" function
12203 */
12204 static void
12205f_strridx(typval_T *argvars, typval_T *rettv)
12206{
12207 char_u buf[NUMBUFLEN];
12208 char_u *needle;
12209 char_u *haystack;
12210 char_u *rest;
12211 char_u *lastmatch = NULL;
12212 int haystack_len, end_idx;
12213
12214 needle = get_tv_string_chk(&argvars[1]);
12215 haystack = get_tv_string_buf_chk(&argvars[0], buf);
12216
12217 rettv->vval.v_number = -1;
12218 if (needle == NULL || haystack == NULL)
12219 return; /* type error; errmsg already given */
12220
12221 haystack_len = (int)STRLEN(haystack);
12222 if (argvars[2].v_type != VAR_UNKNOWN)
12223 {
12224 /* Third argument: upper limit for index */
12225 end_idx = (int)get_tv_number_chk(&argvars[2], NULL);
12226 if (end_idx < 0)
12227 return; /* can never find a match */
12228 }
12229 else
12230 end_idx = haystack_len;
12231
12232 if (*needle == NUL)
12233 {
12234 /* Empty string matches past the end. */
12235 lastmatch = haystack + end_idx;
12236 }
12237 else
12238 {
12239 for (rest = haystack; *rest != '\0'; ++rest)
12240 {
12241 rest = (char_u *)strstr((char *)rest, (char *)needle);
12242 if (rest == NULL || rest > haystack + end_idx)
12243 break;
12244 lastmatch = rest;
12245 }
12246 }
12247
12248 if (lastmatch == NULL)
12249 rettv->vval.v_number = -1;
12250 else
12251 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
12252}
12253
12254/*
12255 * "strtrans()" function
12256 */
12257 static void
12258f_strtrans(typval_T *argvars, typval_T *rettv)
12259{
12260 rettv->v_type = VAR_STRING;
12261 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
12262}
12263
12264/*
12265 * "submatch()" function
12266 */
12267 static void
12268f_submatch(typval_T *argvars, typval_T *rettv)
12269{
12270 int error = FALSE;
12271 int no;
12272 int retList = 0;
12273
12274 no = (int)get_tv_number_chk(&argvars[0], &error);
12275 if (error)
12276 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012277 if (no < 0 || no >= NSUBEXP)
12278 {
Bram Moolenaar79518e22017-02-17 16:31:35 +010012279 EMSGN(_("E935: invalid submatch number: %d"), no);
12280 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012281 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012282 if (argvars[1].v_type != VAR_UNKNOWN)
12283 retList = (int)get_tv_number_chk(&argvars[1], &error);
12284 if (error)
12285 return;
12286
12287 if (retList == 0)
12288 {
12289 rettv->v_type = VAR_STRING;
12290 rettv->vval.v_string = reg_submatch(no);
12291 }
12292 else
12293 {
12294 rettv->v_type = VAR_LIST;
12295 rettv->vval.v_list = reg_submatch_list(no);
12296 }
12297}
12298
12299/*
12300 * "substitute()" function
12301 */
12302 static void
12303f_substitute(typval_T *argvars, typval_T *rettv)
12304{
12305 char_u patbuf[NUMBUFLEN];
12306 char_u subbuf[NUMBUFLEN];
12307 char_u flagsbuf[NUMBUFLEN];
12308
12309 char_u *str = get_tv_string_chk(&argvars[0]);
12310 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012311 char_u *sub = NULL;
12312 typval_T *expr = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012313 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
12314
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012315 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
12316 expr = &argvars[2];
12317 else
12318 sub = get_tv_string_buf_chk(&argvars[2], subbuf);
12319
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012320 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012321 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
12322 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012323 rettv->vval.v_string = NULL;
12324 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012325 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012326}
12327
12328/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020012329 * "swapinfo(swap_filename)" function
12330 */
12331 static void
12332f_swapinfo(typval_T *argvars, typval_T *rettv)
12333{
12334 if (rettv_dict_alloc(rettv) == OK)
12335 get_b0_dict(get_tv_string(argvars), rettv->vval.v_dict);
12336}
12337
12338/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012339 * "synID(lnum, col, trans)" function
12340 */
12341 static void
12342f_synID(typval_T *argvars UNUSED, typval_T *rettv)
12343{
12344 int id = 0;
12345#ifdef FEAT_SYN_HL
12346 linenr_T lnum;
12347 colnr_T col;
12348 int trans;
12349 int transerr = FALSE;
12350
12351 lnum = get_tv_lnum(argvars); /* -1 on type error */
12352 col = (linenr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12353 trans = (int)get_tv_number_chk(&argvars[2], &transerr);
12354
12355 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12356 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
12357 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
12358#endif
12359
12360 rettv->vval.v_number = id;
12361}
12362
12363/*
12364 * "synIDattr(id, what [, mode])" function
12365 */
12366 static void
12367f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
12368{
12369 char_u *p = NULL;
12370#ifdef FEAT_SYN_HL
12371 int id;
12372 char_u *what;
12373 char_u *mode;
12374 char_u modebuf[NUMBUFLEN];
12375 int modec;
12376
12377 id = (int)get_tv_number(&argvars[0]);
12378 what = get_tv_string(&argvars[1]);
12379 if (argvars[2].v_type != VAR_UNKNOWN)
12380 {
12381 mode = get_tv_string_buf(&argvars[2], modebuf);
12382 modec = TOLOWER_ASC(mode[0]);
12383 if (modec != 't' && modec != 'c' && modec != 'g')
12384 modec = 0; /* replace invalid with current */
12385 }
12386 else
12387 {
12388#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
12389 if (USE_24BIT)
12390 modec = 'g';
12391 else
12392#endif
12393 if (t_colors > 1)
12394 modec = 'c';
12395 else
12396 modec = 't';
12397 }
12398
12399
12400 switch (TOLOWER_ASC(what[0]))
12401 {
12402 case 'b':
12403 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
12404 p = highlight_color(id, what, modec);
12405 else /* bold */
12406 p = highlight_has_attr(id, HL_BOLD, modec);
12407 break;
12408
12409 case 'f': /* fg[#] or font */
12410 p = highlight_color(id, what, modec);
12411 break;
12412
12413 case 'i':
12414 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
12415 p = highlight_has_attr(id, HL_INVERSE, modec);
12416 else /* italic */
12417 p = highlight_has_attr(id, HL_ITALIC, modec);
12418 break;
12419
12420 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020012421 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012422 break;
12423
12424 case 'r': /* reverse */
12425 p = highlight_has_attr(id, HL_INVERSE, modec);
12426 break;
12427
12428 case 's':
12429 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
12430 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020012431 /* strikeout */
12432 else if (TOLOWER_ASC(what[1]) == 't' &&
12433 TOLOWER_ASC(what[2]) == 'r')
12434 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012435 else /* standout */
12436 p = highlight_has_attr(id, HL_STANDOUT, modec);
12437 break;
12438
12439 case 'u':
12440 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
12441 /* underline */
12442 p = highlight_has_attr(id, HL_UNDERLINE, modec);
12443 else
12444 /* undercurl */
12445 p = highlight_has_attr(id, HL_UNDERCURL, modec);
12446 break;
12447 }
12448
12449 if (p != NULL)
12450 p = vim_strsave(p);
12451#endif
12452 rettv->v_type = VAR_STRING;
12453 rettv->vval.v_string = p;
12454}
12455
12456/*
12457 * "synIDtrans(id)" function
12458 */
12459 static void
12460f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
12461{
12462 int id;
12463
12464#ifdef FEAT_SYN_HL
12465 id = (int)get_tv_number(&argvars[0]);
12466
12467 if (id > 0)
12468 id = syn_get_final_id(id);
12469 else
12470#endif
12471 id = 0;
12472
12473 rettv->vval.v_number = id;
12474}
12475
12476/*
12477 * "synconcealed(lnum, col)" function
12478 */
12479 static void
12480f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
12481{
12482#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12483 linenr_T lnum;
12484 colnr_T col;
12485 int syntax_flags = 0;
12486 int cchar;
12487 int matchid = 0;
12488 char_u str[NUMBUFLEN];
12489#endif
12490
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012491 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012492
12493#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12494 lnum = get_tv_lnum(argvars); /* -1 on type error */
12495 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12496
12497 vim_memset(str, NUL, sizeof(str));
12498
12499 if (rettv_list_alloc(rettv) != FAIL)
12500 {
12501 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12502 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12503 && curwin->w_p_cole > 0)
12504 {
12505 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
12506 syntax_flags = get_syntax_info(&matchid);
12507
12508 /* get the conceal character */
12509 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
12510 {
12511 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020012512 if (cchar == NUL && curwin->w_p_cole == 1)
12513 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012514 if (cchar != NUL)
12515 {
12516# ifdef FEAT_MBYTE
12517 if (has_mbyte)
12518 (*mb_char2bytes)(cchar, str);
12519 else
12520# endif
12521 str[0] = cchar;
12522 }
12523 }
12524 }
12525
12526 list_append_number(rettv->vval.v_list,
12527 (syntax_flags & HL_CONCEAL) != 0);
12528 /* -1 to auto-determine strlen */
12529 list_append_string(rettv->vval.v_list, str, -1);
12530 list_append_number(rettv->vval.v_list, matchid);
12531 }
12532#endif
12533}
12534
12535/*
12536 * "synstack(lnum, col)" function
12537 */
12538 static void
12539f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
12540{
12541#ifdef FEAT_SYN_HL
12542 linenr_T lnum;
12543 colnr_T col;
12544 int i;
12545 int id;
12546#endif
12547
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012548 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012549
12550#ifdef FEAT_SYN_HL
12551 lnum = get_tv_lnum(argvars); /* -1 on type error */
12552 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12553
12554 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12555 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12556 && rettv_list_alloc(rettv) != FAIL)
12557 {
12558 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
12559 for (i = 0; ; ++i)
12560 {
12561 id = syn_get_stack_item(i);
12562 if (id < 0)
12563 break;
12564 if (list_append_number(rettv->vval.v_list, id) == FAIL)
12565 break;
12566 }
12567 }
12568#endif
12569}
12570
12571 static void
12572get_cmd_output_as_rettv(
12573 typval_T *argvars,
12574 typval_T *rettv,
12575 int retlist)
12576{
12577 char_u *res = NULL;
12578 char_u *p;
12579 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012580 int err = FALSE;
12581 FILE *fd;
12582 list_T *list = NULL;
12583 int flags = SHELL_SILENT;
12584
12585 rettv->v_type = VAR_STRING;
12586 rettv->vval.v_string = NULL;
12587 if (check_restricted() || check_secure())
12588 goto errret;
12589
12590 if (argvars[1].v_type != VAR_UNKNOWN)
12591 {
12592 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010012593 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012594 * command.
12595 */
12596 if ((infile = vim_tempname('i', TRUE)) == NULL)
12597 {
12598 EMSG(_(e_notmp));
12599 goto errret;
12600 }
12601
12602 fd = mch_fopen((char *)infile, WRITEBIN);
12603 if (fd == NULL)
12604 {
12605 EMSG2(_(e_notopen), infile);
12606 goto errret;
12607 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010012608 if (argvars[1].v_type == VAR_NUMBER)
12609 {
12610 linenr_T lnum;
12611 buf_T *buf;
12612
12613 buf = buflist_findnr(argvars[1].vval.v_number);
12614 if (buf == NULL)
12615 {
12616 EMSGN(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010012617 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010012618 goto errret;
12619 }
12620
12621 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
12622 {
12623 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
12624 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
12625 {
12626 err = TRUE;
12627 break;
12628 }
12629 if (putc(NL, fd) == EOF)
12630 {
12631 err = TRUE;
12632 break;
12633 }
12634 }
12635 }
12636 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012637 {
12638 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
12639 err = TRUE;
12640 }
12641 else
12642 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010012643 size_t len;
12644 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012645
12646 p = get_tv_string_buf_chk(&argvars[1], buf);
12647 if (p == NULL)
12648 {
12649 fclose(fd);
12650 goto errret; /* type error; errmsg already given */
12651 }
12652 len = STRLEN(p);
12653 if (len > 0 && fwrite(p, len, 1, fd) != 1)
12654 err = TRUE;
12655 }
12656 if (fclose(fd) != 0)
12657 err = TRUE;
12658 if (err)
12659 {
12660 EMSG(_("E677: Error writing temp file"));
12661 goto errret;
12662 }
12663 }
12664
12665 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
12666 * echoes typeahead, that messes up the display. */
12667 if (!msg_silent)
12668 flags += SHELL_COOKED;
12669
12670 if (retlist)
12671 {
12672 int len;
12673 listitem_T *li;
12674 char_u *s = NULL;
12675 char_u *start;
12676 char_u *end;
12677 int i;
12678
12679 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
12680 if (res == NULL)
12681 goto errret;
12682
12683 list = list_alloc();
12684 if (list == NULL)
12685 goto errret;
12686
12687 for (i = 0; i < len; ++i)
12688 {
12689 start = res + i;
12690 while (i < len && res[i] != NL)
12691 ++i;
12692 end = res + i;
12693
12694 s = alloc((unsigned)(end - start + 1));
12695 if (s == NULL)
12696 goto errret;
12697
12698 for (p = s; start < end; ++p, ++start)
12699 *p = *start == NUL ? NL : *start;
12700 *p = NUL;
12701
12702 li = listitem_alloc();
12703 if (li == NULL)
12704 {
12705 vim_free(s);
12706 goto errret;
12707 }
12708 li->li_tv.v_type = VAR_STRING;
12709 li->li_tv.v_lock = 0;
12710 li->li_tv.vval.v_string = s;
12711 list_append(list, li);
12712 }
12713
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012714 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012715 list = NULL;
12716 }
12717 else
12718 {
12719 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
12720#ifdef USE_CR
12721 /* translate <CR> into <NL> */
12722 if (res != NULL)
12723 {
12724 char_u *s;
12725
12726 for (s = res; *s; ++s)
12727 {
12728 if (*s == CAR)
12729 *s = NL;
12730 }
12731 }
12732#else
12733# ifdef USE_CRNL
12734 /* translate <CR><NL> into <NL> */
12735 if (res != NULL)
12736 {
12737 char_u *s, *d;
12738
12739 d = res;
12740 for (s = res; *s; ++s)
12741 {
12742 if (s[0] == CAR && s[1] == NL)
12743 ++s;
12744 *d++ = *s;
12745 }
12746 *d = NUL;
12747 }
12748# endif
12749#endif
12750 rettv->vval.v_string = res;
12751 res = NULL;
12752 }
12753
12754errret:
12755 if (infile != NULL)
12756 {
12757 mch_remove(infile);
12758 vim_free(infile);
12759 }
12760 if (res != NULL)
12761 vim_free(res);
12762 if (list != NULL)
12763 list_free(list);
12764}
12765
12766/*
12767 * "system()" function
12768 */
12769 static void
12770f_system(typval_T *argvars, typval_T *rettv)
12771{
12772 get_cmd_output_as_rettv(argvars, rettv, FALSE);
12773}
12774
12775/*
12776 * "systemlist()" function
12777 */
12778 static void
12779f_systemlist(typval_T *argvars, typval_T *rettv)
12780{
12781 get_cmd_output_as_rettv(argvars, rettv, TRUE);
12782}
12783
12784/*
12785 * "tabpagebuflist()" function
12786 */
12787 static void
12788f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12789{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012790 tabpage_T *tp;
12791 win_T *wp = NULL;
12792
12793 if (argvars[0].v_type == VAR_UNKNOWN)
12794 wp = firstwin;
12795 else
12796 {
12797 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12798 if (tp != NULL)
12799 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12800 }
12801 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
12802 {
12803 for (; wp != NULL; wp = wp->w_next)
12804 if (list_append_number(rettv->vval.v_list,
12805 wp->w_buffer->b_fnum) == FAIL)
12806 break;
12807 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012808}
12809
12810
12811/*
12812 * "tabpagenr()" function
12813 */
12814 static void
12815f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
12816{
12817 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012818 char_u *arg;
12819
12820 if (argvars[0].v_type != VAR_UNKNOWN)
12821 {
12822 arg = get_tv_string_chk(&argvars[0]);
12823 nr = 0;
12824 if (arg != NULL)
12825 {
12826 if (STRCMP(arg, "$") == 0)
12827 nr = tabpage_index(NULL) - 1;
12828 else
12829 EMSG2(_(e_invexpr2), arg);
12830 }
12831 }
12832 else
12833 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012834 rettv->vval.v_number = nr;
12835}
12836
12837
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012838static int get_winnr(tabpage_T *tp, typval_T *argvar);
12839
12840/*
12841 * Common code for tabpagewinnr() and winnr().
12842 */
12843 static int
12844get_winnr(tabpage_T *tp, typval_T *argvar)
12845{
12846 win_T *twin;
12847 int nr = 1;
12848 win_T *wp;
12849 char_u *arg;
12850
12851 twin = (tp == curtab) ? curwin : tp->tp_curwin;
12852 if (argvar->v_type != VAR_UNKNOWN)
12853 {
12854 arg = get_tv_string_chk(argvar);
12855 if (arg == NULL)
12856 nr = 0; /* type error; errmsg already given */
12857 else if (STRCMP(arg, "$") == 0)
12858 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
12859 else if (STRCMP(arg, "#") == 0)
12860 {
12861 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
12862 if (twin == NULL)
12863 nr = 0;
12864 }
12865 else
12866 {
12867 EMSG2(_(e_invexpr2), arg);
12868 nr = 0;
12869 }
12870 }
12871
12872 if (nr > 0)
12873 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12874 wp != twin; wp = wp->w_next)
12875 {
12876 if (wp == NULL)
12877 {
12878 /* didn't find it in this tabpage */
12879 nr = 0;
12880 break;
12881 }
12882 ++nr;
12883 }
12884 return nr;
12885}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012886
12887/*
12888 * "tabpagewinnr()" function
12889 */
12890 static void
12891f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
12892{
12893 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012894 tabpage_T *tp;
12895
12896 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12897 if (tp == NULL)
12898 nr = 0;
12899 else
12900 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012901 rettv->vval.v_number = nr;
12902}
12903
12904
12905/*
12906 * "tagfiles()" function
12907 */
12908 static void
12909f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
12910{
12911 char_u *fname;
12912 tagname_T tn;
12913 int first;
12914
12915 if (rettv_list_alloc(rettv) == FAIL)
12916 return;
12917 fname = alloc(MAXPATHL);
12918 if (fname == NULL)
12919 return;
12920
12921 for (first = TRUE; ; first = FALSE)
12922 if (get_tagfname(&tn, first, fname) == FAIL
12923 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
12924 break;
12925 tagname_free(&tn);
12926 vim_free(fname);
12927}
12928
12929/*
12930 * "taglist()" function
12931 */
12932 static void
12933f_taglist(typval_T *argvars, typval_T *rettv)
12934{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012935 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012936 char_u *tag_pattern;
12937
12938 tag_pattern = get_tv_string(&argvars[0]);
12939
12940 rettv->vval.v_number = FALSE;
12941 if (*tag_pattern == NUL)
12942 return;
12943
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012944 if (argvars[1].v_type != VAR_UNKNOWN)
12945 fname = get_tv_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012946 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012947 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012948}
12949
12950/*
12951 * "tempname()" function
12952 */
12953 static void
12954f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
12955{
12956 static int x = 'A';
12957
12958 rettv->v_type = VAR_STRING;
12959 rettv->vval.v_string = vim_tempname(x, FALSE);
12960
12961 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
12962 * names. Skip 'I' and 'O', they are used for shell redirection. */
12963 do
12964 {
12965 if (x == 'Z')
12966 x = '0';
12967 else if (x == '9')
12968 x = 'A';
12969 else
12970 {
12971#ifdef EBCDIC
12972 if (x == 'I')
12973 x = 'J';
12974 else if (x == 'R')
12975 x = 'S';
12976 else
12977#endif
12978 ++x;
12979 }
12980 } while (x == 'I' || x == 'O');
12981}
12982
12983#ifdef FEAT_FLOAT
12984/*
12985 * "tan()" function
12986 */
12987 static void
12988f_tan(typval_T *argvars, typval_T *rettv)
12989{
12990 float_T f = 0.0;
12991
12992 rettv->v_type = VAR_FLOAT;
12993 if (get_float_arg(argvars, &f) == OK)
12994 rettv->vval.v_float = tan(f);
12995 else
12996 rettv->vval.v_float = 0.0;
12997}
12998
12999/*
13000 * "tanh()" function
13001 */
13002 static void
13003f_tanh(typval_T *argvars, typval_T *rettv)
13004{
13005 float_T f = 0.0;
13006
13007 rettv->v_type = VAR_FLOAT;
13008 if (get_float_arg(argvars, &f) == OK)
13009 rettv->vval.v_float = tanh(f);
13010 else
13011 rettv->vval.v_float = 0.0;
13012}
13013#endif
13014
13015/*
13016 * "test_alloc_fail(id, countdown, repeat)" function
13017 */
13018 static void
13019f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
13020{
13021 if (argvars[0].v_type != VAR_NUMBER
13022 || argvars[0].vval.v_number <= 0
13023 || argvars[1].v_type != VAR_NUMBER
13024 || argvars[1].vval.v_number < 0
13025 || argvars[2].v_type != VAR_NUMBER)
13026 EMSG(_(e_invarg));
13027 else
13028 {
13029 alloc_fail_id = argvars[0].vval.v_number;
13030 if (alloc_fail_id >= aid_last)
13031 EMSG(_(e_invarg));
13032 alloc_fail_countdown = argvars[1].vval.v_number;
13033 alloc_fail_repeat = argvars[2].vval.v_number;
13034 did_outofmem_msg = FALSE;
13035 }
13036}
13037
13038/*
13039 * "test_autochdir()"
13040 */
13041 static void
13042f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13043{
13044#if defined(FEAT_AUTOCHDIR)
13045 test_autochdir = TRUE;
13046#endif
13047}
13048
13049/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020013050 * "test_feedinput()"
13051 */
13052 static void
13053f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
13054{
13055#ifdef USE_INPUT_BUF
13056 char_u *val = get_tv_string_chk(&argvars[0]);
13057
13058 if (val != NULL)
13059 {
13060 trash_input_buf();
13061 add_to_input_buf_csi(val, (int)STRLEN(val));
13062 }
13063#endif
13064}
13065
13066/*
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013067 * "test_option_not_set({name})" function
13068 */
13069 static void
13070f_test_option_not_set(typval_T *argvars, typval_T *rettv UNUSED)
13071{
13072 char_u *name = (char_u *)"";
13073
13074 if (argvars[0].v_type != VAR_STRING)
13075 EMSG(_(e_invarg));
13076 else
13077 {
13078 name = get_tv_string_chk(&argvars[0]);
13079 if (reset_option_was_set(name) == FAIL)
13080 EMSG2(_(e_invarg2), name);
13081 }
13082}
13083
13084/*
13085 * "test_override({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013086 */
13087 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013088f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013089{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013090 char_u *name = (char_u *)"";
13091 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013092 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013093
13094 if (argvars[0].v_type != VAR_STRING
13095 || (argvars[1].v_type) != VAR_NUMBER)
13096 EMSG(_(e_invarg));
13097 else
13098 {
13099 name = get_tv_string_chk(&argvars[0]);
13100 val = (int)get_tv_number(&argvars[1]);
13101
13102 if (STRCMP(name, (char_u *)"redraw") == 0)
13103 disable_redraw_for_testing = val;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020013104 else if (STRCMP(name, (char_u *)"redraw_flag") == 0)
13105 ignore_redraw_flag_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013106 else if (STRCMP(name, (char_u *)"char_avail") == 0)
13107 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013108 else if (STRCMP(name, (char_u *)"starting") == 0)
13109 {
13110 if (val)
13111 {
13112 if (save_starting < 0)
13113 save_starting = starting;
13114 starting = 0;
13115 }
13116 else
13117 {
13118 starting = save_starting;
13119 save_starting = -1;
13120 }
13121 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013122 else if (STRCMP(name, (char_u *)"nfa_fail") == 0)
13123 nfa_fail_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013124 else if (STRCMP(name, (char_u *)"ALL") == 0)
13125 {
13126 disable_char_avail_for_testing = FALSE;
13127 disable_redraw_for_testing = FALSE;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020013128 ignore_redraw_flag_for_testing = FALSE;
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013129 nfa_fail_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013130 if (save_starting >= 0)
13131 {
13132 starting = save_starting;
13133 save_starting = -1;
13134 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013135 }
13136 else
13137 EMSG2(_(e_invarg2), name);
13138 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013139}
13140
13141/*
13142 * "test_garbagecollect_now()" function
13143 */
13144 static void
13145f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13146{
13147 /* This is dangerous, any Lists and Dicts used internally may be freed
13148 * while still in use. */
13149 garbage_collect(TRUE);
13150}
13151
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013152/*
13153 * "test_ignore_error()" function
13154 */
13155 static void
13156f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
13157{
13158 ignore_error_for_testing(get_tv_string(&argvars[0]));
13159}
13160
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013161#ifdef FEAT_JOB_CHANNEL
13162 static void
13163f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
13164{
13165 rettv->v_type = VAR_CHANNEL;
13166 rettv->vval.v_channel = NULL;
13167}
13168#endif
13169
13170 static void
13171f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
13172{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013173 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013174}
13175
13176#ifdef FEAT_JOB_CHANNEL
13177 static void
13178f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
13179{
13180 rettv->v_type = VAR_JOB;
13181 rettv->vval.v_job = NULL;
13182}
13183#endif
13184
13185 static void
13186f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
13187{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013188 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013189}
13190
13191 static void
13192f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
13193{
13194 rettv->v_type = VAR_PARTIAL;
13195 rettv->vval.v_partial = NULL;
13196}
13197
13198 static void
13199f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
13200{
13201 rettv->v_type = VAR_STRING;
13202 rettv->vval.v_string = NULL;
13203}
13204
13205 static void
13206f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
13207{
13208 time_for_testing = (time_t)get_tv_number(&argvars[0]);
13209}
13210
13211#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
13212/*
13213 * Get a callback from "arg". It can be a Funcref or a function name.
13214 * When "arg" is zero return an empty string.
13215 * Return NULL for an invalid argument.
13216 */
13217 char_u *
13218get_callback(typval_T *arg, partial_T **pp)
13219{
13220 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
13221 {
13222 *pp = arg->vval.v_partial;
13223 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020013224 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013225 }
13226 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020013227 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013228 {
13229 func_ref(arg->vval.v_string);
13230 return arg->vval.v_string;
13231 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013232 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
13233 return (char_u *)"";
13234 EMSG(_("E921: Invalid callback argument"));
13235 return NULL;
13236}
13237
13238/*
Bram Moolenaard5359b22018-04-05 22:44:39 +020013239 * Unref/free "callback" and "partial" returned by get_callback().
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013240 */
13241 void
13242free_callback(char_u *callback, partial_T *partial)
13243{
13244 if (partial != NULL)
13245 partial_unref(partial);
13246 else if (callback != NULL)
13247 {
13248 func_unref(callback);
13249 vim_free(callback);
13250 }
13251}
13252#endif
13253
13254#ifdef FEAT_TIMERS
13255/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020013256 * "timer_info([timer])" function
13257 */
13258 static void
13259f_timer_info(typval_T *argvars, typval_T *rettv)
13260{
13261 timer_T *timer = NULL;
13262
13263 if (rettv_list_alloc(rettv) != OK)
13264 return;
13265 if (argvars[0].v_type != VAR_UNKNOWN)
13266 {
13267 if (argvars[0].v_type != VAR_NUMBER)
13268 EMSG(_(e_number_exp));
13269 else
13270 {
13271 timer = find_timer((int)get_tv_number(&argvars[0]));
13272 if (timer != NULL)
13273 add_timer_info(rettv, timer);
13274 }
13275 }
13276 else
13277 add_timer_info_all(rettv);
13278}
13279
13280/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013281 * "timer_pause(timer, paused)" function
13282 */
13283 static void
13284f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
13285{
13286 timer_T *timer = NULL;
13287 int paused = (int)get_tv_number(&argvars[1]);
13288
13289 if (argvars[0].v_type != VAR_NUMBER)
13290 EMSG(_(e_number_exp));
13291 else
13292 {
13293 timer = find_timer((int)get_tv_number(&argvars[0]));
13294 if (timer != NULL)
13295 timer->tr_paused = paused;
13296 }
13297}
13298
13299/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013300 * "timer_start(time, callback [, options])" function
13301 */
13302 static void
13303f_timer_start(typval_T *argvars, typval_T *rettv)
13304{
Bram Moolenaar75537a92016-09-05 22:45:28 +020013305 long msec = (long)get_tv_number(&argvars[0]);
13306 timer_T *timer;
13307 int repeat = 0;
13308 char_u *callback;
13309 dict_T *dict;
13310 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013311
Bram Moolenaar75537a92016-09-05 22:45:28 +020013312 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013313 if (check_secure())
13314 return;
13315 if (argvars[2].v_type != VAR_UNKNOWN)
13316 {
13317 if (argvars[2].v_type != VAR_DICT
13318 || (dict = argvars[2].vval.v_dict) == NULL)
13319 {
13320 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
13321 return;
13322 }
13323 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
13324 repeat = get_dict_number(dict, (char_u *)"repeat");
13325 }
13326
Bram Moolenaar75537a92016-09-05 22:45:28 +020013327 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013328 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020013329 return;
13330
13331 timer = create_timer(msec, repeat);
13332 if (timer == NULL)
13333 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013334 else
13335 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020013336 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020013337 timer->tr_callback = vim_strsave(callback);
13338 else
13339 /* pointer into the partial */
13340 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020013341 timer->tr_partial = partial;
13342 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013343 }
13344}
13345
13346/*
13347 * "timer_stop(timer)" function
13348 */
13349 static void
13350f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
13351{
13352 timer_T *timer;
13353
13354 if (argvars[0].v_type != VAR_NUMBER)
13355 {
13356 EMSG(_(e_number_exp));
13357 return;
13358 }
13359 timer = find_timer((int)get_tv_number(&argvars[0]));
13360 if (timer != NULL)
13361 stop_timer(timer);
13362}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013363
13364/*
13365 * "timer_stopall()" function
13366 */
13367 static void
13368f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13369{
13370 stop_all_timers();
13371}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013372#endif
13373
13374/*
13375 * "tolower(string)" function
13376 */
13377 static void
13378f_tolower(typval_T *argvars, typval_T *rettv)
13379{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013380 rettv->v_type = VAR_STRING;
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +010013381 rettv->vval.v_string = strlow_save(get_tv_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013382}
13383
13384/*
13385 * "toupper(string)" function
13386 */
13387 static void
13388f_toupper(typval_T *argvars, typval_T *rettv)
13389{
13390 rettv->v_type = VAR_STRING;
13391 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
13392}
13393
13394/*
13395 * "tr(string, fromstr, tostr)" function
13396 */
13397 static void
13398f_tr(typval_T *argvars, typval_T *rettv)
13399{
13400 char_u *in_str;
13401 char_u *fromstr;
13402 char_u *tostr;
13403 char_u *p;
13404#ifdef FEAT_MBYTE
13405 int inlen;
13406 int fromlen;
13407 int tolen;
13408 int idx;
13409 char_u *cpstr;
13410 int cplen;
13411 int first = TRUE;
13412#endif
13413 char_u buf[NUMBUFLEN];
13414 char_u buf2[NUMBUFLEN];
13415 garray_T ga;
13416
13417 in_str = get_tv_string(&argvars[0]);
13418 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
13419 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
13420
13421 /* Default return value: empty string. */
13422 rettv->v_type = VAR_STRING;
13423 rettv->vval.v_string = NULL;
13424 if (fromstr == NULL || tostr == NULL)
13425 return; /* type error; errmsg already given */
13426 ga_init2(&ga, (int)sizeof(char), 80);
13427
13428#ifdef FEAT_MBYTE
13429 if (!has_mbyte)
13430#endif
13431 /* not multi-byte: fromstr and tostr must be the same length */
13432 if (STRLEN(fromstr) != STRLEN(tostr))
13433 {
13434#ifdef FEAT_MBYTE
13435error:
13436#endif
13437 EMSG2(_(e_invarg2), fromstr);
13438 ga_clear(&ga);
13439 return;
13440 }
13441
13442 /* fromstr and tostr have to contain the same number of chars */
13443 while (*in_str != NUL)
13444 {
13445#ifdef FEAT_MBYTE
13446 if (has_mbyte)
13447 {
13448 inlen = (*mb_ptr2len)(in_str);
13449 cpstr = in_str;
13450 cplen = inlen;
13451 idx = 0;
13452 for (p = fromstr; *p != NUL; p += fromlen)
13453 {
13454 fromlen = (*mb_ptr2len)(p);
13455 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
13456 {
13457 for (p = tostr; *p != NUL; p += tolen)
13458 {
13459 tolen = (*mb_ptr2len)(p);
13460 if (idx-- == 0)
13461 {
13462 cplen = tolen;
13463 cpstr = p;
13464 break;
13465 }
13466 }
13467 if (*p == NUL) /* tostr is shorter than fromstr */
13468 goto error;
13469 break;
13470 }
13471 ++idx;
13472 }
13473
13474 if (first && cpstr == in_str)
13475 {
13476 /* Check that fromstr and tostr have the same number of
13477 * (multi-byte) characters. Done only once when a character
13478 * of in_str doesn't appear in fromstr. */
13479 first = FALSE;
13480 for (p = tostr; *p != NUL; p += tolen)
13481 {
13482 tolen = (*mb_ptr2len)(p);
13483 --idx;
13484 }
13485 if (idx != 0)
13486 goto error;
13487 }
13488
13489 (void)ga_grow(&ga, cplen);
13490 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
13491 ga.ga_len += cplen;
13492
13493 in_str += inlen;
13494 }
13495 else
13496#endif
13497 {
13498 /* When not using multi-byte chars we can do it faster. */
13499 p = vim_strchr(fromstr, *in_str);
13500 if (p != NULL)
13501 ga_append(&ga, tostr[p - fromstr]);
13502 else
13503 ga_append(&ga, *in_str);
13504 ++in_str;
13505 }
13506 }
13507
13508 /* add a terminating NUL */
13509 (void)ga_grow(&ga, 1);
13510 ga_append(&ga, NUL);
13511
13512 rettv->vval.v_string = ga.ga_data;
13513}
13514
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010013515/*
13516 * "trim({expr})" function
13517 */
13518 static void
13519f_trim(typval_T *argvars, typval_T *rettv)
13520{
13521 char_u buf1[NUMBUFLEN];
13522 char_u buf2[NUMBUFLEN];
13523 char_u *head = get_tv_string_buf_chk(&argvars[0], buf1);
13524 char_u *mask = NULL;
13525 char_u *tail;
13526 char_u *prev;
13527 char_u *p;
13528 int c1;
13529
13530 rettv->v_type = VAR_STRING;
13531 if (head == NULL)
13532 {
13533 rettv->vval.v_string = NULL;
13534 return;
13535 }
13536
13537 if (argvars[1].v_type == VAR_STRING)
13538 mask = get_tv_string_buf_chk(&argvars[1], buf2);
13539
13540 while (*head != NUL)
13541 {
13542 c1 = PTR2CHAR(head);
13543 if (mask == NULL)
13544 {
13545 if (c1 > ' ' && c1 != 0xa0)
13546 break;
13547 }
13548 else
13549 {
13550 for (p = mask; *p != NUL; MB_PTR_ADV(p))
13551 if (c1 == PTR2CHAR(p))
13552 break;
13553 if (*p == NUL)
13554 break;
13555 }
13556 MB_PTR_ADV(head);
13557 }
13558
13559 for (tail = head + STRLEN(head); tail > head; tail = prev)
13560 {
13561 prev = tail;
13562 MB_PTR_BACK(head, prev);
13563 c1 = PTR2CHAR(prev);
13564 if (mask == NULL)
13565 {
13566 if (c1 > ' ' && c1 != 0xa0)
13567 break;
13568 }
13569 else
13570 {
13571 for (p = mask; *p != NUL; MB_PTR_ADV(p))
13572 if (c1 == PTR2CHAR(p))
13573 break;
13574 if (*p == NUL)
13575 break;
13576 }
13577 }
13578 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
13579}
13580
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013581#ifdef FEAT_FLOAT
13582/*
13583 * "trunc({float})" function
13584 */
13585 static void
13586f_trunc(typval_T *argvars, typval_T *rettv)
13587{
13588 float_T f = 0.0;
13589
13590 rettv->v_type = VAR_FLOAT;
13591 if (get_float_arg(argvars, &f) == OK)
13592 /* trunc() is not in C90, use floor() or ceil() instead. */
13593 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
13594 else
13595 rettv->vval.v_float = 0.0;
13596}
13597#endif
13598
13599/*
13600 * "type(expr)" function
13601 */
13602 static void
13603f_type(typval_T *argvars, typval_T *rettv)
13604{
13605 int n = -1;
13606
13607 switch (argvars[0].v_type)
13608 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020013609 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
13610 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013611 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020013612 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
13613 case VAR_LIST: n = VAR_TYPE_LIST; break;
13614 case VAR_DICT: n = VAR_TYPE_DICT; break;
13615 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013616 case VAR_SPECIAL:
13617 if (argvars[0].vval.v_number == VVAL_FALSE
13618 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020013619 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013620 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020013621 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013622 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020013623 case VAR_JOB: n = VAR_TYPE_JOB; break;
13624 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013625 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010013626 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013627 n = -1;
13628 break;
13629 }
13630 rettv->vval.v_number = n;
13631}
13632
13633/*
13634 * "undofile(name)" function
13635 */
13636 static void
13637f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
13638{
13639 rettv->v_type = VAR_STRING;
13640#ifdef FEAT_PERSISTENT_UNDO
13641 {
13642 char_u *fname = get_tv_string(&argvars[0]);
13643
13644 if (*fname == NUL)
13645 {
13646 /* If there is no file name there will be no undo file. */
13647 rettv->vval.v_string = NULL;
13648 }
13649 else
13650 {
13651 char_u *ffname = FullName_save(fname, FALSE);
13652
13653 if (ffname != NULL)
13654 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
13655 vim_free(ffname);
13656 }
13657 }
13658#else
13659 rettv->vval.v_string = NULL;
13660#endif
13661}
13662
13663/*
13664 * "undotree()" function
13665 */
13666 static void
13667f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
13668{
13669 if (rettv_dict_alloc(rettv) == OK)
13670 {
13671 dict_T *dict = rettv->vval.v_dict;
13672 list_T *list;
13673
Bram Moolenaare0be1672018-07-08 16:50:37 +020013674 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
13675 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
13676 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
13677 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
13678 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
13679 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013680
13681 list = list_alloc();
13682 if (list != NULL)
13683 {
13684 u_eval_tree(curbuf->b_u_oldhead, list);
13685 dict_add_list(dict, "entries", list);
13686 }
13687 }
13688}
13689
13690/*
13691 * "values(dict)" function
13692 */
13693 static void
13694f_values(typval_T *argvars, typval_T *rettv)
13695{
13696 dict_list(argvars, rettv, 1);
13697}
13698
13699/*
13700 * "virtcol(string)" function
13701 */
13702 static void
13703f_virtcol(typval_T *argvars, typval_T *rettv)
13704{
13705 colnr_T vcol = 0;
13706 pos_T *fp;
13707 int fnum = curbuf->b_fnum;
13708
13709 fp = var2fpos(&argvars[0], FALSE, &fnum);
13710 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
13711 && fnum == curbuf->b_fnum)
13712 {
13713 getvvcol(curwin, fp, NULL, NULL, &vcol);
13714 ++vcol;
13715 }
13716
13717 rettv->vval.v_number = vcol;
13718}
13719
13720/*
13721 * "visualmode()" function
13722 */
13723 static void
13724f_visualmode(typval_T *argvars, typval_T *rettv)
13725{
13726 char_u str[2];
13727
13728 rettv->v_type = VAR_STRING;
13729 str[0] = curbuf->b_visual_mode_eval;
13730 str[1] = NUL;
13731 rettv->vval.v_string = vim_strsave(str);
13732
13733 /* A non-zero number or non-empty string argument: reset mode. */
13734 if (non_zero_arg(&argvars[0]))
13735 curbuf->b_visual_mode_eval = NUL;
13736}
13737
13738/*
13739 * "wildmenumode()" function
13740 */
13741 static void
13742f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13743{
13744#ifdef FEAT_WILDMENU
13745 if (wild_menu_showing)
13746 rettv->vval.v_number = 1;
13747#endif
13748}
13749
13750/*
13751 * "winbufnr(nr)" function
13752 */
13753 static void
13754f_winbufnr(typval_T *argvars, typval_T *rettv)
13755{
13756 win_T *wp;
13757
13758 wp = find_win_by_nr(&argvars[0], NULL);
13759 if (wp == NULL)
13760 rettv->vval.v_number = -1;
13761 else
13762 rettv->vval.v_number = wp->w_buffer->b_fnum;
13763}
13764
13765/*
13766 * "wincol()" function
13767 */
13768 static void
13769f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
13770{
13771 validate_cursor();
13772 rettv->vval.v_number = curwin->w_wcol + 1;
13773}
13774
13775/*
13776 * "winheight(nr)" function
13777 */
13778 static void
13779f_winheight(typval_T *argvars, typval_T *rettv)
13780{
13781 win_T *wp;
13782
13783 wp = find_win_by_nr(&argvars[0], NULL);
13784 if (wp == NULL)
13785 rettv->vval.v_number = -1;
13786 else
13787 rettv->vval.v_number = wp->w_height;
13788}
13789
13790/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020013791 * "winlayout()" function
13792 */
13793 static void
13794f_winlayout(typval_T *argvars, typval_T *rettv)
13795{
13796 tabpage_T *tp;
13797
13798 if (rettv_list_alloc(rettv) != OK)
13799 return;
13800
13801 if (argvars[0].v_type == VAR_UNKNOWN)
13802 tp = curtab;
13803 else
13804 {
13805 tp = find_tabpage((int)get_tv_number(&argvars[0]));
13806 if (tp == NULL)
13807 return;
13808 }
13809
13810 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
13811}
13812
13813/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013814 * "winline()" function
13815 */
13816 static void
13817f_winline(typval_T *argvars UNUSED, typval_T *rettv)
13818{
13819 validate_cursor();
13820 rettv->vval.v_number = curwin->w_wrow + 1;
13821}
13822
13823/*
13824 * "winnr()" function
13825 */
13826 static void
13827f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
13828{
13829 int nr = 1;
13830
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013831 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013832 rettv->vval.v_number = nr;
13833}
13834
13835/*
13836 * "winrestcmd()" function
13837 */
13838 static void
13839f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
13840{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013841 win_T *wp;
13842 int winnr = 1;
13843 garray_T ga;
13844 char_u buf[50];
13845
13846 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020013847 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013848 {
13849 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
13850 ga_concat(&ga, buf);
13851 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
13852 ga_concat(&ga, buf);
13853 ++winnr;
13854 }
13855 ga_append(&ga, NUL);
13856
13857 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013858 rettv->v_type = VAR_STRING;
13859}
13860
13861/*
13862 * "winrestview()" function
13863 */
13864 static void
13865f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
13866{
13867 dict_T *dict;
13868
13869 if (argvars[0].v_type != VAR_DICT
13870 || (dict = argvars[0].vval.v_dict) == NULL)
13871 EMSG(_(e_invarg));
13872 else
13873 {
13874 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
13875 curwin->w_cursor.lnum = (linenr_T)get_dict_number(dict, (char_u *)"lnum");
13876 if (dict_find(dict, (char_u *)"col", -1) != NULL)
13877 curwin->w_cursor.col = (colnr_T)get_dict_number(dict, (char_u *)"col");
13878#ifdef FEAT_VIRTUALEDIT
13879 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
13880 curwin->w_cursor.coladd = (colnr_T)get_dict_number(dict, (char_u *)"coladd");
13881#endif
13882 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
13883 {
13884 curwin->w_curswant = (colnr_T)get_dict_number(dict, (char_u *)"curswant");
13885 curwin->w_set_curswant = FALSE;
13886 }
13887
13888 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
13889 set_topline(curwin, (linenr_T)get_dict_number(dict, (char_u *)"topline"));
13890#ifdef FEAT_DIFF
13891 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
13892 curwin->w_topfill = (int)get_dict_number(dict, (char_u *)"topfill");
13893#endif
13894 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
13895 curwin->w_leftcol = (colnr_T)get_dict_number(dict, (char_u *)"leftcol");
13896 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
13897 curwin->w_skipcol = (colnr_T)get_dict_number(dict, (char_u *)"skipcol");
13898
13899 check_cursor();
13900 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020013901 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013902 changed_window_setting();
13903
13904 if (curwin->w_topline <= 0)
13905 curwin->w_topline = 1;
13906 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
13907 curwin->w_topline = curbuf->b_ml.ml_line_count;
13908#ifdef FEAT_DIFF
13909 check_topfill(curwin, TRUE);
13910#endif
13911 }
13912}
13913
13914/*
13915 * "winsaveview()" function
13916 */
13917 static void
13918f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
13919{
13920 dict_T *dict;
13921
13922 if (rettv_dict_alloc(rettv) == FAIL)
13923 return;
13924 dict = rettv->vval.v_dict;
13925
Bram Moolenaare0be1672018-07-08 16:50:37 +020013926 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
13927 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013928#ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +020013929 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013930#endif
13931 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020013932 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013933
Bram Moolenaare0be1672018-07-08 16:50:37 +020013934 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013935#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020013936 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013937#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020013938 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
13939 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013940}
13941
13942/*
13943 * "winwidth(nr)" function
13944 */
13945 static void
13946f_winwidth(typval_T *argvars, typval_T *rettv)
13947{
13948 win_T *wp;
13949
13950 wp = find_win_by_nr(&argvars[0], NULL);
13951 if (wp == NULL)
13952 rettv->vval.v_number = -1;
13953 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013954 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013955}
13956
13957/*
13958 * "wordcount()" function
13959 */
13960 static void
13961f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
13962{
13963 if (rettv_dict_alloc(rettv) == FAIL)
13964 return;
13965 cursor_pos_info(rettv->vval.v_dict);
13966}
13967
13968/*
13969 * "writefile()" function
13970 */
13971 static void
13972f_writefile(typval_T *argvars, typval_T *rettv)
13973{
13974 int binary = FALSE;
13975 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013976#ifdef HAVE_FSYNC
13977 int do_fsync = p_fs;
13978#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013979 char_u *fname;
13980 FILE *fd;
13981 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013982 listitem_T *li;
13983 list_T *list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013984
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013985 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013986 if (check_restricted() || check_secure())
13987 return;
13988
13989 if (argvars[0].v_type != VAR_LIST)
13990 {
13991 EMSG2(_(e_listarg), "writefile()");
13992 return;
13993 }
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013994 list = argvars[0].vval.v_list;
13995 if (list == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013996 return;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013997 for (li = list->lv_first; li != NULL; li = li->li_next)
13998 if (get_tv_string_chk(&li->li_tv) == NULL)
13999 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014000
14001 if (argvars[2].v_type != VAR_UNKNOWN)
14002 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014003 char_u *arg2 = get_tv_string_chk(&argvars[2]);
14004
14005 if (arg2 == NULL)
14006 return;
14007 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014008 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014009 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014010 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014011#ifdef HAVE_FSYNC
14012 if (vim_strchr(arg2, 's') != NULL)
14013 do_fsync = TRUE;
14014 else if (vim_strchr(arg2, 'S') != NULL)
14015 do_fsync = FALSE;
14016#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014017 }
14018
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014019 fname = get_tv_string_chk(&argvars[1]);
14020 if (fname == NULL)
14021 return;
14022
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014023 /* Always open the file in binary mode, library functions have a mind of
14024 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014025 if (*fname == NUL || (fd = mch_fopen((char *)fname,
14026 append ? APPENDBIN : WRITEBIN)) == NULL)
14027 {
14028 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
14029 ret = -1;
14030 }
14031 else
14032 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014033 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014034 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014035#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010014036 else if (do_fsync)
14037 /* Ignore the error, the user wouldn't know what to do about it.
14038 * May happen for a device. */
Bram Moolenaar42335f52018-09-13 15:33:43 +020014039 vim_ignored = fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014040#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014041 fclose(fd);
14042 }
14043
14044 rettv->vval.v_number = ret;
14045}
14046
14047/*
14048 * "xor(expr, expr)" function
14049 */
14050 static void
14051f_xor(typval_T *argvars, typval_T *rettv)
14052{
14053 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14054 ^ get_tv_number_chk(&argvars[1], NULL);
14055}
14056
14057
14058#endif /* FEAT_EVAL */