blob: 2bebdd1c62db076235173bfea0f529c866b47402 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalfunc.c: Builtin functions
12 */
13#define USING_FLOAT_STUFF
14
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
23#ifdef VMS
24# include <float.h>
25#endif
26
Bram Moolenaard0573012017-10-28 21:11:06 +020027#ifdef MACOS_X
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020028# include <time.h> /* for time_t */
29#endif
30
31static char *e_listarg = N_("E686: Argument of %s must be a List");
32#ifdef FEAT_QUICKFIX
33static char *e_stringreq = N_("E928: String required");
34#endif
35
36#ifdef FEAT_FLOAT
37static void f_abs(typval_T *argvars, typval_T *rettv);
38static void f_acos(typval_T *argvars, typval_T *rettv);
39#endif
40static void f_add(typval_T *argvars, typval_T *rettv);
41static void f_and(typval_T *argvars, typval_T *rettv);
42static void f_append(typval_T *argvars, typval_T *rettv);
Bram Moolenaarca851592018-06-06 21:04:07 +020043static void f_appendbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020044static void f_argc(typval_T *argvars, typval_T *rettv);
45static void f_argidx(typval_T *argvars, typval_T *rettv);
46static void f_arglistid(typval_T *argvars, typval_T *rettv);
47static void f_argv(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +010048static void f_assert_beeps(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020049static void f_assert_equal(typval_T *argvars, typval_T *rettv);
Bram Moolenaard96ff162018-02-18 22:13:29 +010050static void f_assert_equalfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020051static void f_assert_exception(typval_T *argvars, typval_T *rettv);
52static void f_assert_fails(typval_T *argvars, typval_T *rettv);
53static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaar61c04492016-07-23 15:35:35 +020054static void f_assert_inrange(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020055static void f_assert_match(typval_T *argvars, typval_T *rettv);
56static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
57static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar42205552017-03-18 19:42:22 +010058static void f_assert_report(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020059static void f_assert_true(typval_T *argvars, typval_T *rettv);
60#ifdef FEAT_FLOAT
61static void f_asin(typval_T *argvars, typval_T *rettv);
62static void f_atan(typval_T *argvars, typval_T *rettv);
63static void f_atan2(typval_T *argvars, typval_T *rettv);
64#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010065#ifdef FEAT_BEVAL
66static void f_balloon_show(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010067# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010068static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010069# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010070#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020071static void f_browse(typval_T *argvars, typval_T *rettv);
72static void f_browsedir(typval_T *argvars, typval_T *rettv);
73static void f_bufexists(typval_T *argvars, typval_T *rettv);
74static void f_buflisted(typval_T *argvars, typval_T *rettv);
75static void f_bufloaded(typval_T *argvars, typval_T *rettv);
76static void f_bufname(typval_T *argvars, typval_T *rettv);
77static void f_bufnr(typval_T *argvars, typval_T *rettv);
78static void f_bufwinid(typval_T *argvars, typval_T *rettv);
79static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
80static void f_byte2line(typval_T *argvars, typval_T *rettv);
81static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
82static void f_byteidx(typval_T *argvars, typval_T *rettv);
83static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
84static void f_call(typval_T *argvars, typval_T *rettv);
85#ifdef FEAT_FLOAT
86static void f_ceil(typval_T *argvars, typval_T *rettv);
87#endif
88#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +010089static void f_ch_canread(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020090static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0874a832016-09-01 15:11:51 +020091static void f_ch_close_in(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020092static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
93static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
94static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
95static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
96static void f_ch_info(typval_T *argvars, typval_T *rettv);
97static void f_ch_log(typval_T *argvars, typval_T *rettv);
98static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
99static void f_ch_open(typval_T *argvars, typval_T *rettv);
100static void f_ch_read(typval_T *argvars, typval_T *rettv);
101static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
102static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
103static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
104static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
105static void f_ch_status(typval_T *argvars, typval_T *rettv);
106#endif
107static void f_changenr(typval_T *argvars, typval_T *rettv);
108static void f_char2nr(typval_T *argvars, typval_T *rettv);
109static void f_cindent(typval_T *argvars, typval_T *rettv);
110static void f_clearmatches(typval_T *argvars, typval_T *rettv);
111static void f_col(typval_T *argvars, typval_T *rettv);
112#if defined(FEAT_INS_EXPAND)
113static void f_complete(typval_T *argvars, typval_T *rettv);
114static void f_complete_add(typval_T *argvars, typval_T *rettv);
115static void f_complete_check(typval_T *argvars, typval_T *rettv);
116#endif
117static void f_confirm(typval_T *argvars, typval_T *rettv);
118static void f_copy(typval_T *argvars, typval_T *rettv);
119#ifdef FEAT_FLOAT
120static void f_cos(typval_T *argvars, typval_T *rettv);
121static void f_cosh(typval_T *argvars, typval_T *rettv);
122#endif
123static void f_count(typval_T *argvars, typval_T *rettv);
124static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
125static void f_cursor(typval_T *argsvars, typval_T *rettv);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200126#ifdef WIN3264
127static void f_debugbreak(typval_T *argvars, typval_T *rettv);
128#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200129static void f_deepcopy(typval_T *argvars, typval_T *rettv);
130static void f_delete(typval_T *argvars, typval_T *rettv);
Bram Moolenaard79a2622018-06-07 18:17:46 +0200131static void f_deletebufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200132static void f_did_filetype(typval_T *argvars, typval_T *rettv);
133static void f_diff_filler(typval_T *argvars, typval_T *rettv);
134static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
135static void f_empty(typval_T *argvars, typval_T *rettv);
136static void f_escape(typval_T *argvars, typval_T *rettv);
137static void f_eval(typval_T *argvars, typval_T *rettv);
138static void f_eventhandler(typval_T *argvars, typval_T *rettv);
139static void f_executable(typval_T *argvars, typval_T *rettv);
140static void f_execute(typval_T *argvars, typval_T *rettv);
141static void f_exepath(typval_T *argvars, typval_T *rettv);
142static void f_exists(typval_T *argvars, typval_T *rettv);
143#ifdef FEAT_FLOAT
144static void f_exp(typval_T *argvars, typval_T *rettv);
145#endif
146static void f_expand(typval_T *argvars, typval_T *rettv);
147static void f_extend(typval_T *argvars, typval_T *rettv);
148static void f_feedkeys(typval_T *argvars, typval_T *rettv);
149static void f_filereadable(typval_T *argvars, typval_T *rettv);
150static void f_filewritable(typval_T *argvars, typval_T *rettv);
151static void f_filter(typval_T *argvars, typval_T *rettv);
152static void f_finddir(typval_T *argvars, typval_T *rettv);
153static void f_findfile(typval_T *argvars, typval_T *rettv);
154#ifdef FEAT_FLOAT
155static void f_float2nr(typval_T *argvars, typval_T *rettv);
156static void f_floor(typval_T *argvars, typval_T *rettv);
157static void f_fmod(typval_T *argvars, typval_T *rettv);
158#endif
159static void f_fnameescape(typval_T *argvars, typval_T *rettv);
160static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
161static void f_foldclosed(typval_T *argvars, typval_T *rettv);
162static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
163static void f_foldlevel(typval_T *argvars, typval_T *rettv);
164static void f_foldtext(typval_T *argvars, typval_T *rettv);
165static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
166static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200167static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200168static void f_function(typval_T *argvars, typval_T *rettv);
169static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
170static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200171static void f_getbufinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200172static void f_getbufline(typval_T *argvars, typval_T *rettv);
173static void f_getbufvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100174static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200175static void f_getchar(typval_T *argvars, typval_T *rettv);
176static void f_getcharmod(typval_T *argvars, typval_T *rettv);
177static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
178static void f_getcmdline(typval_T *argvars, typval_T *rettv);
179#if defined(FEAT_CMDL_COMPL)
180static void f_getcompletion(typval_T *argvars, typval_T *rettv);
181#endif
182static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
183static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
184static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
185static void f_getcwd(typval_T *argvars, typval_T *rettv);
186static void f_getfontname(typval_T *argvars, typval_T *rettv);
187static void f_getfperm(typval_T *argvars, typval_T *rettv);
188static void f_getfsize(typval_T *argvars, typval_T *rettv);
189static void f_getftime(typval_T *argvars, typval_T *rettv);
190static void f_getftype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +0100191static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200192static void f_getline(typval_T *argvars, typval_T *rettv);
Bram Moolenaard823fa92016-08-12 16:29:27 +0200193static void f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200194static void f_getmatches(typval_T *argvars, typval_T *rettv);
195static void f_getpid(typval_T *argvars, typval_T *rettv);
196static void f_getcurpos(typval_T *argvars, typval_T *rettv);
197static void f_getpos(typval_T *argvars, typval_T *rettv);
198static void f_getqflist(typval_T *argvars, typval_T *rettv);
199static void f_getreg(typval_T *argvars, typval_T *rettv);
200static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200201static void f_gettabinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200202static void f_gettabvar(typval_T *argvars, typval_T *rettv);
203static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200204static void f_getwininfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100205static void f_getwinpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200206static void f_getwinposx(typval_T *argvars, typval_T *rettv);
207static void f_getwinposy(typval_T *argvars, typval_T *rettv);
208static void f_getwinvar(typval_T *argvars, typval_T *rettv);
209static void f_glob(typval_T *argvars, typval_T *rettv);
210static void f_globpath(typval_T *argvars, typval_T *rettv);
211static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
212static void f_has(typval_T *argvars, typval_T *rettv);
213static void f_has_key(typval_T *argvars, typval_T *rettv);
214static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
215static void f_hasmapto(typval_T *argvars, typval_T *rettv);
216static void f_histadd(typval_T *argvars, typval_T *rettv);
217static void f_histdel(typval_T *argvars, typval_T *rettv);
218static void f_histget(typval_T *argvars, typval_T *rettv);
219static void f_histnr(typval_T *argvars, typval_T *rettv);
220static void f_hlID(typval_T *argvars, typval_T *rettv);
221static void f_hlexists(typval_T *argvars, typval_T *rettv);
222static void f_hostname(typval_T *argvars, typval_T *rettv);
223static void f_iconv(typval_T *argvars, typval_T *rettv);
224static void f_indent(typval_T *argvars, typval_T *rettv);
225static void f_index(typval_T *argvars, typval_T *rettv);
226static void f_input(typval_T *argvars, typval_T *rettv);
227static void f_inputdialog(typval_T *argvars, typval_T *rettv);
228static void f_inputlist(typval_T *argvars, typval_T *rettv);
229static void f_inputrestore(typval_T *argvars, typval_T *rettv);
230static void f_inputsave(typval_T *argvars, typval_T *rettv);
231static void f_inputsecret(typval_T *argvars, typval_T *rettv);
232static void f_insert(typval_T *argvars, typval_T *rettv);
233static void f_invert(typval_T *argvars, typval_T *rettv);
234static void f_isdirectory(typval_T *argvars, typval_T *rettv);
235static void f_islocked(typval_T *argvars, typval_T *rettv);
236#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
237static void f_isnan(typval_T *argvars, typval_T *rettv);
238#endif
239static void f_items(typval_T *argvars, typval_T *rettv);
240#ifdef FEAT_JOB_CHANNEL
241static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
242static void f_job_info(typval_T *argvars, typval_T *rettv);
243static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
244static void f_job_start(typval_T *argvars, typval_T *rettv);
245static void f_job_stop(typval_T *argvars, typval_T *rettv);
246static void f_job_status(typval_T *argvars, typval_T *rettv);
247#endif
248static void f_join(typval_T *argvars, typval_T *rettv);
249static void f_js_decode(typval_T *argvars, typval_T *rettv);
250static void f_js_encode(typval_T *argvars, typval_T *rettv);
251static void f_json_decode(typval_T *argvars, typval_T *rettv);
252static void f_json_encode(typval_T *argvars, typval_T *rettv);
253static void f_keys(typval_T *argvars, typval_T *rettv);
254static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
255static void f_len(typval_T *argvars, typval_T *rettv);
256static void f_libcall(typval_T *argvars, typval_T *rettv);
257static void f_libcallnr(typval_T *argvars, typval_T *rettv);
258static void f_line(typval_T *argvars, typval_T *rettv);
259static void f_line2byte(typval_T *argvars, typval_T *rettv);
260static void f_lispindent(typval_T *argvars, typval_T *rettv);
261static void f_localtime(typval_T *argvars, typval_T *rettv);
262#ifdef FEAT_FLOAT
263static void f_log(typval_T *argvars, typval_T *rettv);
264static void f_log10(typval_T *argvars, typval_T *rettv);
265#endif
266#ifdef FEAT_LUA
267static void f_luaeval(typval_T *argvars, typval_T *rettv);
268#endif
269static void f_map(typval_T *argvars, typval_T *rettv);
270static void f_maparg(typval_T *argvars, typval_T *rettv);
271static void f_mapcheck(typval_T *argvars, typval_T *rettv);
272static void f_match(typval_T *argvars, typval_T *rettv);
273static void f_matchadd(typval_T *argvars, typval_T *rettv);
274static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
275static void f_matcharg(typval_T *argvars, typval_T *rettv);
276static void f_matchdelete(typval_T *argvars, typval_T *rettv);
277static void f_matchend(typval_T *argvars, typval_T *rettv);
278static void f_matchlist(typval_T *argvars, typval_T *rettv);
279static void f_matchstr(typval_T *argvars, typval_T *rettv);
280static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
281static void f_max(typval_T *argvars, typval_T *rettv);
282static void f_min(typval_T *argvars, typval_T *rettv);
283#ifdef vim_mkdir
284static void f_mkdir(typval_T *argvars, typval_T *rettv);
285#endif
286static void f_mode(typval_T *argvars, typval_T *rettv);
287#ifdef FEAT_MZSCHEME
288static void f_mzeval(typval_T *argvars, typval_T *rettv);
289#endif
290static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
291static void f_nr2char(typval_T *argvars, typval_T *rettv);
292static void f_or(typval_T *argvars, typval_T *rettv);
293static void f_pathshorten(typval_T *argvars, typval_T *rettv);
294#ifdef FEAT_PERL
295static void f_perleval(typval_T *argvars, typval_T *rettv);
296#endif
297#ifdef FEAT_FLOAT
298static void f_pow(typval_T *argvars, typval_T *rettv);
299#endif
300static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
301static void f_printf(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200302#ifdef FEAT_JOB_CHANNEL
303static void f_prompt_setcallback(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200304static void f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200305static void f_prompt_setprompt(typval_T *argvars, typval_T *rettv);
306#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200307static void f_pumvisible(typval_T *argvars, typval_T *rettv);
308#ifdef FEAT_PYTHON3
309static void f_py3eval(typval_T *argvars, typval_T *rettv);
310#endif
311#ifdef FEAT_PYTHON
312static void f_pyeval(typval_T *argvars, typval_T *rettv);
313#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100314#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
315static void f_pyxeval(typval_T *argvars, typval_T *rettv);
316#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200317static void f_range(typval_T *argvars, typval_T *rettv);
318static void f_readfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200319static void f_reg_executing(typval_T *argvars, typval_T *rettv);
320static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200321static void f_reltime(typval_T *argvars, typval_T *rettv);
322#ifdef FEAT_FLOAT
323static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
324#endif
325static void f_reltimestr(typval_T *argvars, typval_T *rettv);
326static void f_remote_expr(typval_T *argvars, typval_T *rettv);
327static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
328static void f_remote_peek(typval_T *argvars, typval_T *rettv);
329static void f_remote_read(typval_T *argvars, typval_T *rettv);
330static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100331static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200332static void f_remove(typval_T *argvars, typval_T *rettv);
333static void f_rename(typval_T *argvars, typval_T *rettv);
334static void f_repeat(typval_T *argvars, typval_T *rettv);
335static void f_resolve(typval_T *argvars, typval_T *rettv);
336static void f_reverse(typval_T *argvars, typval_T *rettv);
337#ifdef FEAT_FLOAT
338static void f_round(typval_T *argvars, typval_T *rettv);
339#endif
340static void f_screenattr(typval_T *argvars, typval_T *rettv);
341static void f_screenchar(typval_T *argvars, typval_T *rettv);
342static void f_screencol(typval_T *argvars, typval_T *rettv);
343static void f_screenrow(typval_T *argvars, typval_T *rettv);
344static void f_search(typval_T *argvars, typval_T *rettv);
345static void f_searchdecl(typval_T *argvars, typval_T *rettv);
346static void f_searchpair(typval_T *argvars, typval_T *rettv);
347static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
348static void f_searchpos(typval_T *argvars, typval_T *rettv);
349static void f_server2client(typval_T *argvars, typval_T *rettv);
350static void f_serverlist(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200351static void f_setbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200352static void f_setbufvar(typval_T *argvars, typval_T *rettv);
353static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
354static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
355static void f_setfperm(typval_T *argvars, typval_T *rettv);
356static void f_setline(typval_T *argvars, typval_T *rettv);
357static void f_setloclist(typval_T *argvars, typval_T *rettv);
358static void f_setmatches(typval_T *argvars, typval_T *rettv);
359static void f_setpos(typval_T *argvars, typval_T *rettv);
360static void f_setqflist(typval_T *argvars, typval_T *rettv);
361static void f_setreg(typval_T *argvars, typval_T *rettv);
362static void f_settabvar(typval_T *argvars, typval_T *rettv);
363static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
364static void f_setwinvar(typval_T *argvars, typval_T *rettv);
365#ifdef FEAT_CRYPT
366static void f_sha256(typval_T *argvars, typval_T *rettv);
367#endif /* FEAT_CRYPT */
368static void f_shellescape(typval_T *argvars, typval_T *rettv);
369static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
370static void f_simplify(typval_T *argvars, typval_T *rettv);
371#ifdef FEAT_FLOAT
372static void f_sin(typval_T *argvars, typval_T *rettv);
373static void f_sinh(typval_T *argvars, typval_T *rettv);
374#endif
375static void f_sort(typval_T *argvars, typval_T *rettv);
376static void f_soundfold(typval_T *argvars, typval_T *rettv);
377static void f_spellbadword(typval_T *argvars, typval_T *rettv);
378static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
379static void f_split(typval_T *argvars, typval_T *rettv);
380#ifdef FEAT_FLOAT
381static void f_sqrt(typval_T *argvars, typval_T *rettv);
382static void f_str2float(typval_T *argvars, typval_T *rettv);
383#endif
384static void f_str2nr(typval_T *argvars, typval_T *rettv);
385static void f_strchars(typval_T *argvars, typval_T *rettv);
386#ifdef HAVE_STRFTIME
387static void f_strftime(typval_T *argvars, typval_T *rettv);
388#endif
389static void f_strgetchar(typval_T *argvars, typval_T *rettv);
390static void f_stridx(typval_T *argvars, typval_T *rettv);
391static void f_string(typval_T *argvars, typval_T *rettv);
392static void f_strlen(typval_T *argvars, typval_T *rettv);
393static void f_strcharpart(typval_T *argvars, typval_T *rettv);
394static void f_strpart(typval_T *argvars, typval_T *rettv);
395static void f_strridx(typval_T *argvars, typval_T *rettv);
396static void f_strtrans(typval_T *argvars, typval_T *rettv);
397static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
398static void f_strwidth(typval_T *argvars, typval_T *rettv);
399static void f_submatch(typval_T *argvars, typval_T *rettv);
400static void f_substitute(typval_T *argvars, typval_T *rettv);
401static void f_synID(typval_T *argvars, typval_T *rettv);
402static void f_synIDattr(typval_T *argvars, typval_T *rettv);
403static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
404static void f_synstack(typval_T *argvars, typval_T *rettv);
405static void f_synconcealed(typval_T *argvars, typval_T *rettv);
406static void f_system(typval_T *argvars, typval_T *rettv);
407static void f_systemlist(typval_T *argvars, typval_T *rettv);
408static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
409static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
410static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
411static void f_taglist(typval_T *argvars, typval_T *rettv);
412static void f_tagfiles(typval_T *argvars, typval_T *rettv);
413static void f_tempname(typval_T *argvars, typval_T *rettv);
414static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
415static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200416static void f_test_feedinput(typval_T *argvars, typval_T *rettv);
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100417static void f_test_override(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200418static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100419static void f_test_ignore_error(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200420#ifdef FEAT_JOB_CHANNEL
421static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
422#endif
423static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
424#ifdef FEAT_JOB_CHANNEL
425static void f_test_null_job(typval_T *argvars, typval_T *rettv);
426#endif
427static void f_test_null_list(typval_T *argvars, typval_T *rettv);
428static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
429static void f_test_null_string(typval_T *argvars, typval_T *rettv);
430static void f_test_settime(typval_T *argvars, typval_T *rettv);
431#ifdef FEAT_FLOAT
432static void f_tan(typval_T *argvars, typval_T *rettv);
433static void f_tanh(typval_T *argvars, typval_T *rettv);
434#endif
435#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200436static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200437static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200438static void f_timer_start(typval_T *argvars, typval_T *rettv);
439static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200440static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200441#endif
442static void f_tolower(typval_T *argvars, typval_T *rettv);
443static void f_toupper(typval_T *argvars, typval_T *rettv);
444static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100445static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200446#ifdef FEAT_FLOAT
447static void f_trunc(typval_T *argvars, typval_T *rettv);
448#endif
449static void f_type(typval_T *argvars, typval_T *rettv);
450static void f_undofile(typval_T *argvars, typval_T *rettv);
451static void f_undotree(typval_T *argvars, typval_T *rettv);
452static void f_uniq(typval_T *argvars, typval_T *rettv);
453static void f_values(typval_T *argvars, typval_T *rettv);
454static void f_virtcol(typval_T *argvars, typval_T *rettv);
455static void f_visualmode(typval_T *argvars, typval_T *rettv);
456static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
457static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
458static void f_win_getid(typval_T *argvars, typval_T *rettv);
459static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
460static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
461static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100462static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200463static void f_winbufnr(typval_T *argvars, typval_T *rettv);
464static void f_wincol(typval_T *argvars, typval_T *rettv);
465static void f_winheight(typval_T *argvars, typval_T *rettv);
466static void f_winline(typval_T *argvars, typval_T *rettv);
467static void f_winnr(typval_T *argvars, typval_T *rettv);
468static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
469static void f_winrestview(typval_T *argvars, typval_T *rettv);
470static void f_winsaveview(typval_T *argvars, typval_T *rettv);
471static void f_winwidth(typval_T *argvars, typval_T *rettv);
472static void f_writefile(typval_T *argvars, typval_T *rettv);
473static void f_wordcount(typval_T *argvars, typval_T *rettv);
474static void f_xor(typval_T *argvars, typval_T *rettv);
475
476/*
477 * Array with names and number of arguments of all internal functions
478 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
479 */
480static struct fst
481{
482 char *f_name; /* function name */
483 char f_min_argc; /* minimal number of arguments */
484 char f_max_argc; /* maximal number of arguments */
485 void (*f_func)(typval_T *args, typval_T *rvar);
486 /* implementation of function */
487} functions[] =
488{
489#ifdef FEAT_FLOAT
490 {"abs", 1, 1, f_abs},
491 {"acos", 1, 1, f_acos}, /* WJMc */
492#endif
493 {"add", 2, 2, f_add},
494 {"and", 2, 2, f_and},
495 {"append", 2, 2, f_append},
Bram Moolenaarca851592018-06-06 21:04:07 +0200496 {"appendbufline", 3, 3, f_appendbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200497 {"argc", 0, 0, f_argc},
498 {"argidx", 0, 0, f_argidx},
499 {"arglistid", 0, 2, f_arglistid},
500 {"argv", 0, 1, f_argv},
501#ifdef FEAT_FLOAT
502 {"asin", 1, 1, f_asin}, /* WJMc */
503#endif
Bram Moolenaarb48e96f2018-02-13 12:26:14 +0100504 {"assert_beeps", 1, 2, f_assert_beeps},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200505 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaard96ff162018-02-18 22:13:29 +0100506 {"assert_equalfile", 2, 2, f_assert_equalfile},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200507 {"assert_exception", 1, 2, f_assert_exception},
508 {"assert_fails", 1, 2, f_assert_fails},
509 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100510 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200511 {"assert_match", 2, 3, f_assert_match},
512 {"assert_notequal", 2, 3, f_assert_notequal},
513 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100514 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200515 {"assert_true", 1, 2, f_assert_true},
516#ifdef FEAT_FLOAT
517 {"atan", 1, 1, f_atan},
518 {"atan2", 2, 2, f_atan2},
519#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100520#ifdef FEAT_BEVAL
521 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100522# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100523 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100524# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100525#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200526 {"browse", 4, 4, f_browse},
527 {"browsedir", 2, 2, f_browsedir},
528 {"bufexists", 1, 1, f_bufexists},
529 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
530 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
531 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
532 {"buflisted", 1, 1, f_buflisted},
533 {"bufloaded", 1, 1, f_bufloaded},
534 {"bufname", 1, 1, f_bufname},
535 {"bufnr", 1, 2, f_bufnr},
536 {"bufwinid", 1, 1, f_bufwinid},
537 {"bufwinnr", 1, 1, f_bufwinnr},
538 {"byte2line", 1, 1, f_byte2line},
539 {"byteidx", 2, 2, f_byteidx},
540 {"byteidxcomp", 2, 2, f_byteidxcomp},
541 {"call", 2, 3, f_call},
542#ifdef FEAT_FLOAT
543 {"ceil", 1, 1, f_ceil},
544#endif
545#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100546 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200547 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200548 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200549 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
550 {"ch_evalraw", 2, 3, f_ch_evalraw},
551 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
552 {"ch_getjob", 1, 1, f_ch_getjob},
553 {"ch_info", 1, 1, f_ch_info},
554 {"ch_log", 1, 2, f_ch_log},
555 {"ch_logfile", 1, 2, f_ch_logfile},
556 {"ch_open", 1, 2, f_ch_open},
557 {"ch_read", 1, 2, f_ch_read},
558 {"ch_readraw", 1, 2, f_ch_readraw},
559 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
560 {"ch_sendraw", 2, 3, f_ch_sendraw},
561 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200562 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200563#endif
564 {"changenr", 0, 0, f_changenr},
565 {"char2nr", 1, 2, f_char2nr},
566 {"cindent", 1, 1, f_cindent},
567 {"clearmatches", 0, 0, f_clearmatches},
568 {"col", 1, 1, f_col},
569#if defined(FEAT_INS_EXPAND)
570 {"complete", 2, 2, f_complete},
571 {"complete_add", 1, 1, f_complete_add},
572 {"complete_check", 0, 0, f_complete_check},
573#endif
574 {"confirm", 1, 4, f_confirm},
575 {"copy", 1, 1, f_copy},
576#ifdef FEAT_FLOAT
577 {"cos", 1, 1, f_cos},
578 {"cosh", 1, 1, f_cosh},
579#endif
580 {"count", 2, 4, f_count},
581 {"cscope_connection",0,3, f_cscope_connection},
582 {"cursor", 1, 3, f_cursor},
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200583#ifdef WIN3264
584 {"debugbreak", 1, 1, f_debugbreak},
585#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200586 {"deepcopy", 1, 2, f_deepcopy},
587 {"delete", 1, 2, f_delete},
Bram Moolenaard79a2622018-06-07 18:17:46 +0200588 {"deletebufline", 2, 3, f_deletebufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200589 {"did_filetype", 0, 0, f_did_filetype},
590 {"diff_filler", 1, 1, f_diff_filler},
591 {"diff_hlID", 2, 2, f_diff_hlID},
592 {"empty", 1, 1, f_empty},
593 {"escape", 2, 2, f_escape},
594 {"eval", 1, 1, f_eval},
595 {"eventhandler", 0, 0, f_eventhandler},
596 {"executable", 1, 1, f_executable},
597 {"execute", 1, 2, f_execute},
598 {"exepath", 1, 1, f_exepath},
599 {"exists", 1, 1, f_exists},
600#ifdef FEAT_FLOAT
601 {"exp", 1, 1, f_exp},
602#endif
603 {"expand", 1, 3, f_expand},
604 {"extend", 2, 3, f_extend},
605 {"feedkeys", 1, 2, f_feedkeys},
606 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
607 {"filereadable", 1, 1, f_filereadable},
608 {"filewritable", 1, 1, f_filewritable},
609 {"filter", 2, 2, f_filter},
610 {"finddir", 1, 3, f_finddir},
611 {"findfile", 1, 3, f_findfile},
612#ifdef FEAT_FLOAT
613 {"float2nr", 1, 1, f_float2nr},
614 {"floor", 1, 1, f_floor},
615 {"fmod", 2, 2, f_fmod},
616#endif
617 {"fnameescape", 1, 1, f_fnameescape},
618 {"fnamemodify", 2, 2, f_fnamemodify},
619 {"foldclosed", 1, 1, f_foldclosed},
620 {"foldclosedend", 1, 1, f_foldclosedend},
621 {"foldlevel", 1, 1, f_foldlevel},
622 {"foldtext", 0, 0, f_foldtext},
623 {"foldtextresult", 1, 1, f_foldtextresult},
624 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200625 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200626 {"function", 1, 3, f_function},
627 {"garbagecollect", 0, 1, f_garbagecollect},
628 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200629 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200630 {"getbufline", 2, 3, f_getbufline},
631 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100632 {"getchangelist", 1, 1, f_getchangelist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200633 {"getchar", 0, 1, f_getchar},
634 {"getcharmod", 0, 0, f_getcharmod},
635 {"getcharsearch", 0, 0, f_getcharsearch},
636 {"getcmdline", 0, 0, f_getcmdline},
637 {"getcmdpos", 0, 0, f_getcmdpos},
638 {"getcmdtype", 0, 0, f_getcmdtype},
639 {"getcmdwintype", 0, 0, f_getcmdwintype},
640#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200641 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200642#endif
643 {"getcurpos", 0, 0, f_getcurpos},
644 {"getcwd", 0, 2, f_getcwd},
645 {"getfontname", 0, 1, f_getfontname},
646 {"getfperm", 1, 1, f_getfperm},
647 {"getfsize", 1, 1, f_getfsize},
648 {"getftime", 1, 1, f_getftime},
649 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100650 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200651 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200652 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200653 {"getmatches", 0, 0, f_getmatches},
654 {"getpid", 0, 0, f_getpid},
655 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200656 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200657 {"getreg", 0, 3, f_getreg},
658 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200659 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200660 {"gettabvar", 2, 3, f_gettabvar},
661 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200662 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100663 {"getwinpos", 0, 1, f_getwinpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200664 {"getwinposx", 0, 0, f_getwinposx},
665 {"getwinposy", 0, 0, f_getwinposy},
666 {"getwinvar", 2, 3, f_getwinvar},
667 {"glob", 1, 4, f_glob},
668 {"glob2regpat", 1, 1, f_glob2regpat},
669 {"globpath", 2, 5, f_globpath},
670 {"has", 1, 1, f_has},
671 {"has_key", 2, 2, f_has_key},
672 {"haslocaldir", 0, 2, f_haslocaldir},
673 {"hasmapto", 1, 3, f_hasmapto},
674 {"highlightID", 1, 1, f_hlID}, /* obsolete */
675 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
676 {"histadd", 2, 2, f_histadd},
677 {"histdel", 1, 2, f_histdel},
678 {"histget", 1, 2, f_histget},
679 {"histnr", 1, 1, f_histnr},
680 {"hlID", 1, 1, f_hlID},
681 {"hlexists", 1, 1, f_hlexists},
682 {"hostname", 0, 0, f_hostname},
683 {"iconv", 3, 3, f_iconv},
684 {"indent", 1, 1, f_indent},
685 {"index", 2, 4, f_index},
686 {"input", 1, 3, f_input},
687 {"inputdialog", 1, 3, f_inputdialog},
688 {"inputlist", 1, 1, f_inputlist},
689 {"inputrestore", 0, 0, f_inputrestore},
690 {"inputsave", 0, 0, f_inputsave},
691 {"inputsecret", 1, 2, f_inputsecret},
692 {"insert", 2, 3, f_insert},
693 {"invert", 1, 1, f_invert},
694 {"isdirectory", 1, 1, f_isdirectory},
695 {"islocked", 1, 1, f_islocked},
696#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
697 {"isnan", 1, 1, f_isnan},
698#endif
699 {"items", 1, 1, f_items},
700#ifdef FEAT_JOB_CHANNEL
701 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200702 {"job_info", 0, 1, f_job_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200703 {"job_setoptions", 2, 2, f_job_setoptions},
704 {"job_start", 1, 2, f_job_start},
705 {"job_status", 1, 1, f_job_status},
706 {"job_stop", 1, 2, f_job_stop},
707#endif
708 {"join", 1, 2, f_join},
709 {"js_decode", 1, 1, f_js_decode},
710 {"js_encode", 1, 1, f_js_encode},
711 {"json_decode", 1, 1, f_json_decode},
712 {"json_encode", 1, 1, f_json_encode},
713 {"keys", 1, 1, f_keys},
714 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
715 {"len", 1, 1, f_len},
716 {"libcall", 3, 3, f_libcall},
717 {"libcallnr", 3, 3, f_libcallnr},
718 {"line", 1, 1, f_line},
719 {"line2byte", 1, 1, f_line2byte},
720 {"lispindent", 1, 1, f_lispindent},
721 {"localtime", 0, 0, f_localtime},
722#ifdef FEAT_FLOAT
723 {"log", 1, 1, f_log},
724 {"log10", 1, 1, f_log10},
725#endif
726#ifdef FEAT_LUA
727 {"luaeval", 1, 2, f_luaeval},
728#endif
729 {"map", 2, 2, f_map},
730 {"maparg", 1, 4, f_maparg},
731 {"mapcheck", 1, 3, f_mapcheck},
732 {"match", 2, 4, f_match},
733 {"matchadd", 2, 5, f_matchadd},
734 {"matchaddpos", 2, 5, f_matchaddpos},
735 {"matcharg", 1, 1, f_matcharg},
736 {"matchdelete", 1, 1, f_matchdelete},
737 {"matchend", 2, 4, f_matchend},
738 {"matchlist", 2, 4, f_matchlist},
739 {"matchstr", 2, 4, f_matchstr},
740 {"matchstrpos", 2, 4, f_matchstrpos},
741 {"max", 1, 1, f_max},
742 {"min", 1, 1, f_min},
743#ifdef vim_mkdir
744 {"mkdir", 1, 3, f_mkdir},
745#endif
746 {"mode", 0, 1, f_mode},
747#ifdef FEAT_MZSCHEME
748 {"mzeval", 1, 1, f_mzeval},
749#endif
750 {"nextnonblank", 1, 1, f_nextnonblank},
751 {"nr2char", 1, 2, f_nr2char},
752 {"or", 2, 2, f_or},
753 {"pathshorten", 1, 1, f_pathshorten},
754#ifdef FEAT_PERL
755 {"perleval", 1, 1, f_perleval},
756#endif
757#ifdef FEAT_FLOAT
758 {"pow", 2, 2, f_pow},
759#endif
760 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100761 {"printf", 1, 19, f_printf},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200762#ifdef FEAT_JOB_CHANNEL
763 {"prompt_setcallback", 2, 2, f_prompt_setcallback},
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200764 {"prompt_setinterrupt", 2, 2, f_prompt_setinterrupt},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200765 {"prompt_setprompt", 2, 2, f_prompt_setprompt},
766#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200767 {"pumvisible", 0, 0, f_pumvisible},
768#ifdef FEAT_PYTHON3
769 {"py3eval", 1, 1, f_py3eval},
770#endif
771#ifdef FEAT_PYTHON
772 {"pyeval", 1, 1, f_pyeval},
773#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100774#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
775 {"pyxeval", 1, 1, f_pyxeval},
776#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200777 {"range", 1, 3, f_range},
778 {"readfile", 1, 3, f_readfile},
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200779 {"reg_executing", 0, 0, f_reg_executing},
780 {"reg_recording", 0, 0, f_reg_recording},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200781 {"reltime", 0, 2, f_reltime},
782#ifdef FEAT_FLOAT
783 {"reltimefloat", 1, 1, f_reltimefloat},
784#endif
785 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100786 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200787 {"remote_foreground", 1, 1, f_remote_foreground},
788 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100789 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200790 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100791 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200792 {"remove", 2, 3, f_remove},
793 {"rename", 2, 2, f_rename},
794 {"repeat", 2, 2, f_repeat},
795 {"resolve", 1, 1, f_resolve},
796 {"reverse", 1, 1, f_reverse},
797#ifdef FEAT_FLOAT
798 {"round", 1, 1, f_round},
799#endif
800 {"screenattr", 2, 2, f_screenattr},
801 {"screenchar", 2, 2, f_screenchar},
802 {"screencol", 0, 0, f_screencol},
803 {"screenrow", 0, 0, f_screenrow},
804 {"search", 1, 4, f_search},
805 {"searchdecl", 1, 3, f_searchdecl},
806 {"searchpair", 3, 7, f_searchpair},
807 {"searchpairpos", 3, 7, f_searchpairpos},
808 {"searchpos", 1, 4, f_searchpos},
809 {"server2client", 2, 2, f_server2client},
810 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200811 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200812 {"setbufvar", 3, 3, f_setbufvar},
813 {"setcharsearch", 1, 1, f_setcharsearch},
814 {"setcmdpos", 1, 1, f_setcmdpos},
815 {"setfperm", 2, 2, f_setfperm},
816 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200817 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200818 {"setmatches", 1, 1, f_setmatches},
819 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200820 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200821 {"setreg", 2, 3, f_setreg},
822 {"settabvar", 3, 3, f_settabvar},
823 {"settabwinvar", 4, 4, f_settabwinvar},
824 {"setwinvar", 3, 3, f_setwinvar},
825#ifdef FEAT_CRYPT
826 {"sha256", 1, 1, f_sha256},
827#endif
828 {"shellescape", 1, 2, f_shellescape},
829 {"shiftwidth", 0, 0, f_shiftwidth},
830 {"simplify", 1, 1, f_simplify},
831#ifdef FEAT_FLOAT
832 {"sin", 1, 1, f_sin},
833 {"sinh", 1, 1, f_sinh},
834#endif
835 {"sort", 1, 3, f_sort},
836 {"soundfold", 1, 1, f_soundfold},
837 {"spellbadword", 0, 1, f_spellbadword},
838 {"spellsuggest", 1, 3, f_spellsuggest},
839 {"split", 1, 3, f_split},
840#ifdef FEAT_FLOAT
841 {"sqrt", 1, 1, f_sqrt},
842 {"str2float", 1, 1, f_str2float},
843#endif
844 {"str2nr", 1, 2, f_str2nr},
845 {"strcharpart", 2, 3, f_strcharpart},
846 {"strchars", 1, 2, f_strchars},
847 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
848#ifdef HAVE_STRFTIME
849 {"strftime", 1, 2, f_strftime},
850#endif
851 {"strgetchar", 2, 2, f_strgetchar},
852 {"stridx", 2, 3, f_stridx},
853 {"string", 1, 1, f_string},
854 {"strlen", 1, 1, f_strlen},
855 {"strpart", 2, 3, f_strpart},
856 {"strridx", 2, 3, f_strridx},
857 {"strtrans", 1, 1, f_strtrans},
858 {"strwidth", 1, 1, f_strwidth},
859 {"submatch", 1, 2, f_submatch},
860 {"substitute", 4, 4, f_substitute},
861 {"synID", 3, 3, f_synID},
862 {"synIDattr", 2, 3, f_synIDattr},
863 {"synIDtrans", 1, 1, f_synIDtrans},
864 {"synconcealed", 2, 2, f_synconcealed},
865 {"synstack", 2, 2, f_synstack},
866 {"system", 1, 2, f_system},
867 {"systemlist", 1, 2, f_systemlist},
868 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
869 {"tabpagenr", 0, 1, f_tabpagenr},
870 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
871 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100872 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200873#ifdef FEAT_FLOAT
874 {"tan", 1, 1, f_tan},
875 {"tanh", 1, 1, f_tanh},
876#endif
877 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200878#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100879 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
880 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100881 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200882 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200883# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
884 {"term_getansicolors", 1, 1, f_term_getansicolors},
885# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200886 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200887 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200888 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200889 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200890 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200891 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200892 {"term_getstatus", 1, 1, f_term_getstatus},
893 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200894 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200895 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200896 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200897 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200898# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
899 {"term_setansicolors", 2, 2, f_term_setansicolors},
900# endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100901 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100902 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaara42d3632018-04-14 17:05:38 +0200903 {"term_setsize", 3, 3, f_term_setsize},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200904 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200905 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200906#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200907 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
908 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200909 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200910 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100911 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200912#ifdef FEAT_JOB_CHANNEL
913 {"test_null_channel", 0, 0, f_test_null_channel},
914#endif
915 {"test_null_dict", 0, 0, f_test_null_dict},
916#ifdef FEAT_JOB_CHANNEL
917 {"test_null_job", 0, 0, f_test_null_job},
918#endif
919 {"test_null_list", 0, 0, f_test_null_list},
920 {"test_null_partial", 0, 0, f_test_null_partial},
921 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100922 {"test_override", 2, 2, f_test_override},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200923 {"test_settime", 1, 1, f_test_settime},
924#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200925 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200926 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200927 {"timer_start", 2, 3, f_timer_start},
928 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200929 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200930#endif
931 {"tolower", 1, 1, f_tolower},
932 {"toupper", 1, 1, f_toupper},
933 {"tr", 3, 3, f_tr},
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100934 {"trim", 1, 2, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200935#ifdef FEAT_FLOAT
936 {"trunc", 1, 1, f_trunc},
937#endif
938 {"type", 1, 1, f_type},
939 {"undofile", 1, 1, f_undofile},
940 {"undotree", 0, 0, f_undotree},
941 {"uniq", 1, 3, f_uniq},
942 {"values", 1, 1, f_values},
943 {"virtcol", 1, 1, f_virtcol},
944 {"visualmode", 0, 1, f_visualmode},
945 {"wildmenumode", 0, 0, f_wildmenumode},
946 {"win_findbuf", 1, 1, f_win_findbuf},
947 {"win_getid", 0, 2, f_win_getid},
948 {"win_gotoid", 1, 1, f_win_gotoid},
949 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
950 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100951 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200952 {"winbufnr", 1, 1, f_winbufnr},
953 {"wincol", 0, 0, f_wincol},
954 {"winheight", 1, 1, f_winheight},
955 {"winline", 0, 0, f_winline},
956 {"winnr", 0, 1, f_winnr},
957 {"winrestcmd", 0, 0, f_winrestcmd},
958 {"winrestview", 1, 1, f_winrestview},
959 {"winsaveview", 0, 0, f_winsaveview},
960 {"winwidth", 1, 1, f_winwidth},
961 {"wordcount", 0, 0, f_wordcount},
962 {"writefile", 2, 3, f_writefile},
963 {"xor", 2, 2, f_xor},
964};
965
966#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
967
968/*
969 * Function given to ExpandGeneric() to obtain the list of internal
970 * or user defined function names.
971 */
972 char_u *
973get_function_name(expand_T *xp, int idx)
974{
975 static int intidx = -1;
976 char_u *name;
977
978 if (idx == 0)
979 intidx = -1;
980 if (intidx < 0)
981 {
982 name = get_user_func_name(xp, idx);
983 if (name != NULL)
984 return name;
985 }
986 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
987 {
988 STRCPY(IObuff, functions[intidx].f_name);
989 STRCAT(IObuff, "(");
990 if (functions[intidx].f_max_argc == 0)
991 STRCAT(IObuff, ")");
992 return IObuff;
993 }
994
995 return NULL;
996}
997
998/*
999 * Function given to ExpandGeneric() to obtain the list of internal or
1000 * user defined variable or function names.
1001 */
1002 char_u *
1003get_expr_name(expand_T *xp, int idx)
1004{
1005 static int intidx = -1;
1006 char_u *name;
1007
1008 if (idx == 0)
1009 intidx = -1;
1010 if (intidx < 0)
1011 {
1012 name = get_function_name(xp, idx);
1013 if (name != NULL)
1014 return name;
1015 }
1016 return get_user_var_name(xp, ++intidx);
1017}
1018
1019#endif /* FEAT_CMDL_COMPL */
1020
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001021/*
1022 * Find internal function in table above.
1023 * Return index, or -1 if not found
1024 */
1025 int
1026find_internal_func(
1027 char_u *name) /* name of the function */
1028{
1029 int first = 0;
1030 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
1031 int cmp;
1032 int x;
1033
1034 /*
1035 * Find the function name in the table. Binary search.
1036 */
1037 while (first <= last)
1038 {
1039 x = first + ((unsigned)(last - first) >> 1);
1040 cmp = STRCMP(name, functions[x].f_name);
1041 if (cmp < 0)
1042 last = x - 1;
1043 else if (cmp > 0)
1044 first = x + 1;
1045 else
1046 return x;
1047 }
1048 return -1;
1049}
1050
1051 int
1052call_internal_func(
1053 char_u *name,
1054 int argcount,
1055 typval_T *argvars,
1056 typval_T *rettv)
1057{
1058 int i;
1059
1060 i = find_internal_func(name);
1061 if (i < 0)
1062 return ERROR_UNKNOWN;
1063 if (argcount < functions[i].f_min_argc)
1064 return ERROR_TOOFEW;
1065 if (argcount > functions[i].f_max_argc)
1066 return ERROR_TOOMANY;
1067 argvars[argcount].v_type = VAR_UNKNOWN;
1068 functions[i].f_func(argvars, rettv);
1069 return ERROR_NONE;
1070}
1071
1072/*
1073 * Return TRUE for a non-zero Number and a non-empty String.
1074 */
1075 static int
1076non_zero_arg(typval_T *argvars)
1077{
1078 return ((argvars[0].v_type == VAR_NUMBER
1079 && argvars[0].vval.v_number != 0)
1080 || (argvars[0].v_type == VAR_SPECIAL
1081 && argvars[0].vval.v_number == VVAL_TRUE)
1082 || (argvars[0].v_type == VAR_STRING
1083 && argvars[0].vval.v_string != NULL
1084 && *argvars[0].vval.v_string != NUL));
1085}
1086
1087/*
1088 * Get the lnum from the first argument.
1089 * Also accepts ".", "$", etc., but that only works for the current buffer.
1090 * Returns -1 on error.
1091 */
1092 static linenr_T
1093get_tv_lnum(typval_T *argvars)
1094{
1095 typval_T rettv;
1096 linenr_T lnum;
1097
1098 lnum = (linenr_T)get_tv_number_chk(&argvars[0], NULL);
1099 if (lnum == 0) /* no valid number, try using line() */
1100 {
1101 rettv.v_type = VAR_NUMBER;
1102 f_line(argvars, &rettv);
1103 lnum = (linenr_T)rettv.vval.v_number;
1104 clear_tv(&rettv);
1105 }
1106 return lnum;
1107}
1108
1109#ifdef FEAT_FLOAT
1110static int get_float_arg(typval_T *argvars, float_T *f);
1111
1112/*
1113 * Get the float value of "argvars[0]" into "f".
1114 * Returns FAIL when the argument is not a Number or Float.
1115 */
1116 static int
1117get_float_arg(typval_T *argvars, float_T *f)
1118{
1119 if (argvars[0].v_type == VAR_FLOAT)
1120 {
1121 *f = argvars[0].vval.v_float;
1122 return OK;
1123 }
1124 if (argvars[0].v_type == VAR_NUMBER)
1125 {
1126 *f = (float_T)argvars[0].vval.v_number;
1127 return OK;
1128 }
1129 EMSG(_("E808: Number or Float required"));
1130 return FAIL;
1131}
1132
1133/*
1134 * "abs(expr)" function
1135 */
1136 static void
1137f_abs(typval_T *argvars, typval_T *rettv)
1138{
1139 if (argvars[0].v_type == VAR_FLOAT)
1140 {
1141 rettv->v_type = VAR_FLOAT;
1142 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1143 }
1144 else
1145 {
1146 varnumber_T n;
1147 int error = FALSE;
1148
1149 n = get_tv_number_chk(&argvars[0], &error);
1150 if (error)
1151 rettv->vval.v_number = -1;
1152 else if (n > 0)
1153 rettv->vval.v_number = n;
1154 else
1155 rettv->vval.v_number = -n;
1156 }
1157}
1158
1159/*
1160 * "acos()" function
1161 */
1162 static void
1163f_acos(typval_T *argvars, typval_T *rettv)
1164{
1165 float_T f = 0.0;
1166
1167 rettv->v_type = VAR_FLOAT;
1168 if (get_float_arg(argvars, &f) == OK)
1169 rettv->vval.v_float = acos(f);
1170 else
1171 rettv->vval.v_float = 0.0;
1172}
1173#endif
1174
1175/*
1176 * "add(list, item)" function
1177 */
1178 static void
1179f_add(typval_T *argvars, typval_T *rettv)
1180{
1181 list_T *l;
1182
1183 rettv->vval.v_number = 1; /* Default: Failed */
1184 if (argvars[0].v_type == VAR_LIST)
1185 {
1186 if ((l = argvars[0].vval.v_list) != NULL
1187 && !tv_check_lock(l->lv_lock,
1188 (char_u *)N_("add() argument"), TRUE)
1189 && list_append_tv(l, &argvars[1]) == OK)
1190 copy_tv(&argvars[0], rettv);
1191 }
1192 else
1193 EMSG(_(e_listreq));
1194}
1195
1196/*
1197 * "and(expr, expr)" function
1198 */
1199 static void
1200f_and(typval_T *argvars, typval_T *rettv)
1201{
1202 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
1203 & get_tv_number_chk(&argvars[1], NULL);
1204}
1205
1206/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001207 * Get the lnum from the first argument.
1208 * Also accepts "$", then "buf" is used.
1209 * Returns 0 on error.
1210 */
1211 static linenr_T
1212get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
1213{
1214 if (argvars[0].v_type == VAR_STRING
1215 && argvars[0].vval.v_string != NULL
1216 && argvars[0].vval.v_string[0] == '$'
1217 && buf != NULL)
1218 return buf->b_ml.ml_line_count;
1219 return (linenr_T)get_tv_number_chk(&argvars[0], NULL);
1220}
1221
1222/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02001223 * If there is a window for "curbuf", make it the current window.
1224 */
1225 static void
1226find_win_for_curbuf(void)
1227{
1228 wininfo_T *wip;
1229
1230 for (wip = curbuf->b_wininfo; wip != NULL; wip = wip->wi_next)
1231 {
1232 if (wip->wi_win != NULL)
1233 {
1234 curwin = wip->wi_win;
1235 break;
1236 }
1237 }
1238}
1239
1240/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001241 * Set line or list of lines in buffer "buf".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001242 */
1243 static void
Bram Moolenaarca851592018-06-06 21:04:07 +02001244set_buffer_lines(
1245 buf_T *buf,
1246 linenr_T lnum_arg,
1247 int append,
1248 typval_T *lines,
1249 typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001250{
Bram Moolenaarca851592018-06-06 21:04:07 +02001251 linenr_T lnum = lnum_arg + (append ? 1 : 0);
1252 char_u *line = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001253 list_T *l = NULL;
1254 listitem_T *li = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001255 long added = 0;
Bram Moolenaarca851592018-06-06 21:04:07 +02001256 linenr_T append_lnum;
1257 buf_T *curbuf_save = NULL;
1258 win_T *curwin_save = NULL;
1259 int is_curbuf = buf == curbuf;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001260
Bram Moolenaarca851592018-06-06 21:04:07 +02001261 /* When using the current buffer ml_mfp will be set if needed. Useful when
1262 * setline() is used on startup. For other buffers the buffer must be
1263 * loaded. */
1264 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001265 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001266 rettv->vval.v_number = 1; /* FAIL */
1267 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001268 }
1269
Bram Moolenaarca851592018-06-06 21:04:07 +02001270 if (!is_curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001271 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001272 curbuf_save = curbuf;
1273 curwin_save = curwin;
1274 curbuf = buf;
Bram Moolenaard79a2622018-06-07 18:17:46 +02001275 find_win_for_curbuf();
Bram Moolenaarca851592018-06-06 21:04:07 +02001276 }
1277
1278 if (append)
1279 // appendbufline() uses the line number below which we insert
1280 append_lnum = lnum - 1;
1281 else
1282 // setbufline() uses the line number above which we insert, we only
1283 // append if it's below the last line
1284 append_lnum = curbuf->b_ml.ml_line_count;
1285
1286 if (lines->v_type == VAR_LIST)
1287 {
1288 l = lines->vval.v_list;
1289 li = l->lv_first;
1290 }
1291 else
1292 line = get_tv_string_chk(lines);
1293
1294 /* default result is zero == OK */
1295 for (;;)
1296 {
1297 if (l != NULL)
1298 {
1299 /* list argument, get next string */
1300 if (li == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001301 break;
Bram Moolenaarca851592018-06-06 21:04:07 +02001302 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001303 li = li->li_next;
1304 }
1305
Bram Moolenaarca851592018-06-06 21:04:07 +02001306 rettv->vval.v_number = 1; /* FAIL */
1307 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
1308 break;
1309
1310 /* When coming here from Insert mode, sync undo, so that this can be
1311 * undone separately from what was previously inserted. */
1312 if (u_sync_once == 2)
1313 {
1314 u_sync_once = 1; /* notify that u_sync() was called */
1315 u_sync(TRUE);
1316 }
1317
1318 if (!append && lnum <= curbuf->b_ml.ml_line_count)
1319 {
1320 /* existing line, replace it */
1321 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
1322 {
1323 changed_bytes(lnum, 0);
1324 if (is_curbuf && lnum == curwin->w_cursor.lnum)
1325 check_cursor_col();
1326 rettv->vval.v_number = 0; /* OK */
1327 }
1328 }
1329 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
1330 {
1331 /* append the line */
1332 ++added;
1333 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
1334 rettv->vval.v_number = 0; /* OK */
1335 }
1336
1337 if (l == NULL) /* only one string argument */
1338 break;
1339 ++lnum;
1340 }
1341
1342 if (added > 0)
1343 {
1344 win_T *wp;
1345 tabpage_T *tp;
1346
1347 appended_lines_mark(append_lnum, added);
1348 FOR_ALL_TAB_WINDOWS(tp, wp)
1349 if (wp->w_buffer == buf && wp->w_cursor.lnum > append_lnum)
1350 wp->w_cursor.lnum += added;
1351 check_cursor_col();
1352
Bram Moolenaarf2732452018-06-03 14:47:35 +02001353#ifdef FEAT_JOB_CHANNEL
1354 if (bt_prompt(curbuf) && (State & INSERT))
1355 // show the line with the prompt
1356 update_topline();
1357#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001358 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001359
1360 if (!is_curbuf)
1361 {
1362 curbuf = curbuf_save;
1363 curwin = curwin_save;
1364 }
1365}
1366
1367/*
1368 * "append(lnum, string/list)" function
1369 */
1370 static void
1371f_append(typval_T *argvars, typval_T *rettv)
1372{
1373 linenr_T lnum = get_tv_lnum(&argvars[0]);
1374
1375 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1376}
1377
1378/*
1379 * "appendbufline(buf, lnum, string/list)" function
1380 */
1381 static void
1382f_appendbufline(typval_T *argvars, typval_T *rettv)
1383{
1384 linenr_T lnum;
1385 buf_T *buf;
1386
1387 buf = get_buf_tv(&argvars[0], FALSE);
1388 if (buf == NULL)
1389 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001390 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001391 {
1392 lnum = get_tv_lnum_buf(&argvars[1], buf);
1393 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1394 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001395}
1396
1397/*
1398 * "argc()" function
1399 */
1400 static void
1401f_argc(typval_T *argvars UNUSED, typval_T *rettv)
1402{
1403 rettv->vval.v_number = ARGCOUNT;
1404}
1405
1406/*
1407 * "argidx()" function
1408 */
1409 static void
1410f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1411{
1412 rettv->vval.v_number = curwin->w_arg_idx;
1413}
1414
1415/*
1416 * "arglistid()" function
1417 */
1418 static void
1419f_arglistid(typval_T *argvars, typval_T *rettv)
1420{
1421 win_T *wp;
1422
1423 rettv->vval.v_number = -1;
1424 wp = find_tabwin(&argvars[0], &argvars[1]);
1425 if (wp != NULL)
1426 rettv->vval.v_number = wp->w_alist->id;
1427}
1428
1429/*
1430 * "argv(nr)" function
1431 */
1432 static void
1433f_argv(typval_T *argvars, typval_T *rettv)
1434{
1435 int idx;
1436
1437 if (argvars[0].v_type != VAR_UNKNOWN)
1438 {
1439 idx = (int)get_tv_number_chk(&argvars[0], NULL);
1440 if (idx >= 0 && idx < ARGCOUNT)
1441 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
1442 else
1443 rettv->vval.v_string = NULL;
1444 rettv->v_type = VAR_STRING;
1445 }
1446 else if (rettv_list_alloc(rettv) == OK)
1447 for (idx = 0; idx < ARGCOUNT; ++idx)
1448 list_append_string(rettv->vval.v_list,
1449 alist_name(&ARGLIST[idx]), -1);
1450}
1451
1452/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001453 * "assert_beeps(cmd [, error])" function
1454 */
1455 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001456f_assert_beeps(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001457{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001458 rettv->vval.v_number = assert_beeps(argvars);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001459}
1460
1461/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001462 * "assert_equal(expected, actual[, msg])" function
1463 */
1464 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001465f_assert_equal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001466{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001467 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_EQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001468}
1469
1470/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01001471 * "assert_equalfile(fname-one, fname-two)" function
1472 */
1473 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001474f_assert_equalfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaard96ff162018-02-18 22:13:29 +01001475{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001476 rettv->vval.v_number = assert_equalfile(argvars);
Bram Moolenaard96ff162018-02-18 22:13:29 +01001477}
1478
1479/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001480 * "assert_notequal(expected, actual[, msg])" function
1481 */
1482 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001483f_assert_notequal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001484{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001485 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001486}
1487
1488/*
1489 * "assert_exception(string[, msg])" function
1490 */
1491 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001492f_assert_exception(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001493{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001494 rettv->vval.v_number = assert_exception(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001495}
1496
1497/*
1498 * "assert_fails(cmd [, error])" function
1499 */
1500 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001501f_assert_fails(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001502{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001503 rettv->vval.v_number = assert_fails(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001504}
1505
1506/*
1507 * "assert_false(actual[, msg])" function
1508 */
1509 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001510f_assert_false(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001511{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001512 rettv->vval.v_number = assert_bool(argvars, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001513}
1514
1515/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001516 * "assert_inrange(lower, upper[, msg])" function
1517 */
1518 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001519f_assert_inrange(typval_T *argvars, typval_T *rettv)
Bram Moolenaar61c04492016-07-23 15:35:35 +02001520{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001521 rettv->vval.v_number = assert_inrange(argvars);
Bram Moolenaar61c04492016-07-23 15:35:35 +02001522}
1523
1524/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001525 * "assert_match(pattern, actual[, msg])" function
1526 */
1527 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001528f_assert_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001529{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001530 rettv->vval.v_number = assert_match_common(argvars, ASSERT_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001531}
1532
1533/*
1534 * "assert_notmatch(pattern, actual[, msg])" function
1535 */
1536 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001537f_assert_notmatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001538{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001539 rettv->vval.v_number = assert_match_common(argvars, ASSERT_NOTMATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001540}
1541
1542/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001543 * "assert_report(msg)" function
1544 */
1545 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001546f_assert_report(typval_T *argvars, typval_T *rettv)
Bram Moolenaar42205552017-03-18 19:42:22 +01001547{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001548 rettv->vval.v_number = assert_report(argvars);
Bram Moolenaar42205552017-03-18 19:42:22 +01001549}
1550
1551/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001552 * "assert_true(actual[, msg])" function
1553 */
1554 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001555f_assert_true(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001556{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001557 rettv->vval.v_number = assert_bool(argvars, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001558}
1559
1560#ifdef FEAT_FLOAT
1561/*
1562 * "asin()" function
1563 */
1564 static void
1565f_asin(typval_T *argvars, typval_T *rettv)
1566{
1567 float_T f = 0.0;
1568
1569 rettv->v_type = VAR_FLOAT;
1570 if (get_float_arg(argvars, &f) == OK)
1571 rettv->vval.v_float = asin(f);
1572 else
1573 rettv->vval.v_float = 0.0;
1574}
1575
1576/*
1577 * "atan()" function
1578 */
1579 static void
1580f_atan(typval_T *argvars, typval_T *rettv)
1581{
1582 float_T f = 0.0;
1583
1584 rettv->v_type = VAR_FLOAT;
1585 if (get_float_arg(argvars, &f) == OK)
1586 rettv->vval.v_float = atan(f);
1587 else
1588 rettv->vval.v_float = 0.0;
1589}
1590
1591/*
1592 * "atan2()" function
1593 */
1594 static void
1595f_atan2(typval_T *argvars, typval_T *rettv)
1596{
1597 float_T fx = 0.0, fy = 0.0;
1598
1599 rettv->v_type = VAR_FLOAT;
1600 if (get_float_arg(argvars, &fx) == OK
1601 && get_float_arg(&argvars[1], &fy) == OK)
1602 rettv->vval.v_float = atan2(fx, fy);
1603 else
1604 rettv->vval.v_float = 0.0;
1605}
1606#endif
1607
1608/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001609 * "balloon_show()" function
1610 */
1611#ifdef FEAT_BEVAL
1612 static void
1613f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1614{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001615 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001616 {
1617 if (argvars[0].v_type == VAR_LIST
1618# ifdef FEAT_GUI
1619 && !gui.in_use
1620# endif
1621 )
1622 post_balloon(balloonEval, NULL, argvars[0].vval.v_list);
1623 else
1624 post_balloon(balloonEval, get_tv_string_chk(&argvars[0]), NULL);
1625 }
1626}
1627
Bram Moolenaar669a8282017-11-19 20:13:05 +01001628# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001629 static void
1630f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1631{
1632 if (rettv_list_alloc(rettv) == OK)
1633 {
1634 char_u *msg = get_tv_string_chk(&argvars[0]);
1635
1636 if (msg != NULL)
1637 {
1638 pumitem_T *array;
1639 int size = split_message(msg, &array);
1640 int i;
1641
1642 /* Skip the first and last item, they are always empty. */
1643 for (i = 1; i < size - 1; ++i)
1644 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001645 while (size > 0)
1646 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001647 vim_free(array);
1648 }
1649 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001650}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001651# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001652#endif
1653
1654/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001655 * "browse(save, title, initdir, default)" function
1656 */
1657 static void
1658f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1659{
1660#ifdef FEAT_BROWSE
1661 int save;
1662 char_u *title;
1663 char_u *initdir;
1664 char_u *defname;
1665 char_u buf[NUMBUFLEN];
1666 char_u buf2[NUMBUFLEN];
1667 int error = FALSE;
1668
1669 save = (int)get_tv_number_chk(&argvars[0], &error);
1670 title = get_tv_string_chk(&argvars[1]);
1671 initdir = get_tv_string_buf_chk(&argvars[2], buf);
1672 defname = get_tv_string_buf_chk(&argvars[3], buf2);
1673
1674 if (error || title == NULL || initdir == NULL || defname == NULL)
1675 rettv->vval.v_string = NULL;
1676 else
1677 rettv->vval.v_string =
1678 do_browse(save ? BROWSE_SAVE : 0,
1679 title, defname, NULL, initdir, NULL, curbuf);
1680#else
1681 rettv->vval.v_string = NULL;
1682#endif
1683 rettv->v_type = VAR_STRING;
1684}
1685
1686/*
1687 * "browsedir(title, initdir)" function
1688 */
1689 static void
1690f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1691{
1692#ifdef FEAT_BROWSE
1693 char_u *title;
1694 char_u *initdir;
1695 char_u buf[NUMBUFLEN];
1696
1697 title = get_tv_string_chk(&argvars[0]);
1698 initdir = get_tv_string_buf_chk(&argvars[1], buf);
1699
1700 if (title == NULL || initdir == NULL)
1701 rettv->vval.v_string = NULL;
1702 else
1703 rettv->vval.v_string = do_browse(BROWSE_DIR,
1704 title, NULL, NULL, initdir, NULL, curbuf);
1705#else
1706 rettv->vval.v_string = NULL;
1707#endif
1708 rettv->v_type = VAR_STRING;
1709}
1710
1711static buf_T *find_buffer(typval_T *avar);
1712
1713/*
1714 * Find a buffer by number or exact name.
1715 */
1716 static buf_T *
1717find_buffer(typval_T *avar)
1718{
1719 buf_T *buf = NULL;
1720
1721 if (avar->v_type == VAR_NUMBER)
1722 buf = buflist_findnr((int)avar->vval.v_number);
1723 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1724 {
1725 buf = buflist_findname_exp(avar->vval.v_string);
1726 if (buf == NULL)
1727 {
1728 /* No full path name match, try a match with a URL or a "nofile"
1729 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001730 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001731 if (buf->b_fname != NULL
1732 && (path_with_url(buf->b_fname)
1733#ifdef FEAT_QUICKFIX
1734 || bt_nofile(buf)
1735#endif
1736 )
1737 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1738 break;
1739 }
1740 }
1741 return buf;
1742}
1743
1744/*
1745 * "bufexists(expr)" function
1746 */
1747 static void
1748f_bufexists(typval_T *argvars, typval_T *rettv)
1749{
1750 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1751}
1752
1753/*
1754 * "buflisted(expr)" function
1755 */
1756 static void
1757f_buflisted(typval_T *argvars, typval_T *rettv)
1758{
1759 buf_T *buf;
1760
1761 buf = find_buffer(&argvars[0]);
1762 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1763}
1764
1765/*
1766 * "bufloaded(expr)" function
1767 */
1768 static void
1769f_bufloaded(typval_T *argvars, typval_T *rettv)
1770{
1771 buf_T *buf;
1772
1773 buf = find_buffer(&argvars[0]);
1774 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1775}
1776
1777 buf_T *
1778buflist_find_by_name(char_u *name, int curtab_only)
1779{
1780 int save_magic;
1781 char_u *save_cpo;
1782 buf_T *buf;
1783
1784 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1785 save_magic = p_magic;
1786 p_magic = TRUE;
1787 save_cpo = p_cpo;
1788 p_cpo = (char_u *)"";
1789
1790 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1791 TRUE, FALSE, curtab_only));
1792
1793 p_magic = save_magic;
1794 p_cpo = save_cpo;
1795 return buf;
1796}
1797
1798/*
1799 * Get buffer by number or pattern.
1800 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001801 buf_T *
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001802get_buf_tv(typval_T *tv, int curtab_only)
1803{
1804 char_u *name = tv->vval.v_string;
1805 buf_T *buf;
1806
1807 if (tv->v_type == VAR_NUMBER)
1808 return buflist_findnr((int)tv->vval.v_number);
1809 if (tv->v_type != VAR_STRING)
1810 return NULL;
1811 if (name == NULL || *name == NUL)
1812 return curbuf;
1813 if (name[0] == '$' && name[1] == NUL)
1814 return lastbuf;
1815
1816 buf = buflist_find_by_name(name, curtab_only);
1817
1818 /* If not found, try expanding the name, like done for bufexists(). */
1819 if (buf == NULL)
1820 buf = find_buffer(tv);
1821
1822 return buf;
1823}
1824
1825/*
1826 * "bufname(expr)" function
1827 */
1828 static void
1829f_bufname(typval_T *argvars, typval_T *rettv)
1830{
1831 buf_T *buf;
1832
1833 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1834 ++emsg_off;
1835 buf = get_buf_tv(&argvars[0], FALSE);
1836 rettv->v_type = VAR_STRING;
1837 if (buf != NULL && buf->b_fname != NULL)
1838 rettv->vval.v_string = vim_strsave(buf->b_fname);
1839 else
1840 rettv->vval.v_string = NULL;
1841 --emsg_off;
1842}
1843
1844/*
1845 * "bufnr(expr)" function
1846 */
1847 static void
1848f_bufnr(typval_T *argvars, typval_T *rettv)
1849{
1850 buf_T *buf;
1851 int error = FALSE;
1852 char_u *name;
1853
1854 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1855 ++emsg_off;
1856 buf = get_buf_tv(&argvars[0], FALSE);
1857 --emsg_off;
1858
1859 /* If the buffer isn't found and the second argument is not zero create a
1860 * new buffer. */
1861 if (buf == NULL
1862 && argvars[1].v_type != VAR_UNKNOWN
1863 && get_tv_number_chk(&argvars[1], &error) != 0
1864 && !error
1865 && (name = get_tv_string_chk(&argvars[0])) != NULL
1866 && !error)
1867 buf = buflist_new(name, NULL, (linenr_T)1, 0);
1868
1869 if (buf != NULL)
1870 rettv->vval.v_number = buf->b_fnum;
1871 else
1872 rettv->vval.v_number = -1;
1873}
1874
1875 static void
1876buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
1877{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001878 win_T *wp;
1879 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001880 buf_T *buf;
1881
1882 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1883 ++emsg_off;
1884 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02001885 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001886 {
1887 ++winnr;
1888 if (wp->w_buffer == buf)
1889 break;
1890 }
1891 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001892 --emsg_off;
1893}
1894
1895/*
1896 * "bufwinid(nr)" function
1897 */
1898 static void
1899f_bufwinid(typval_T *argvars, typval_T *rettv)
1900{
1901 buf_win_common(argvars, rettv, FALSE);
1902}
1903
1904/*
1905 * "bufwinnr(nr)" function
1906 */
1907 static void
1908f_bufwinnr(typval_T *argvars, typval_T *rettv)
1909{
1910 buf_win_common(argvars, rettv, TRUE);
1911}
1912
1913/*
1914 * "byte2line(byte)" function
1915 */
1916 static void
1917f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
1918{
1919#ifndef FEAT_BYTEOFF
1920 rettv->vval.v_number = -1;
1921#else
1922 long boff = 0;
1923
1924 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
1925 if (boff < 0)
1926 rettv->vval.v_number = -1;
1927 else
1928 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
1929 (linenr_T)0, &boff);
1930#endif
1931}
1932
1933 static void
1934byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
1935{
1936#ifdef FEAT_MBYTE
1937 char_u *t;
1938#endif
1939 char_u *str;
1940 varnumber_T idx;
1941
1942 str = get_tv_string_chk(&argvars[0]);
1943 idx = get_tv_number_chk(&argvars[1], NULL);
1944 rettv->vval.v_number = -1;
1945 if (str == NULL || idx < 0)
1946 return;
1947
1948#ifdef FEAT_MBYTE
1949 t = str;
1950 for ( ; idx > 0; idx--)
1951 {
1952 if (*t == NUL) /* EOL reached */
1953 return;
1954 if (enc_utf8 && comp)
1955 t += utf_ptr2len(t);
1956 else
1957 t += (*mb_ptr2len)(t);
1958 }
1959 rettv->vval.v_number = (varnumber_T)(t - str);
1960#else
1961 if ((size_t)idx <= STRLEN(str))
1962 rettv->vval.v_number = idx;
1963#endif
1964}
1965
1966/*
1967 * "byteidx()" function
1968 */
1969 static void
1970f_byteidx(typval_T *argvars, typval_T *rettv)
1971{
1972 byteidx(argvars, rettv, FALSE);
1973}
1974
1975/*
1976 * "byteidxcomp()" function
1977 */
1978 static void
1979f_byteidxcomp(typval_T *argvars, typval_T *rettv)
1980{
1981 byteidx(argvars, rettv, TRUE);
1982}
1983
1984/*
1985 * "call(func, arglist [, dict])" function
1986 */
1987 static void
1988f_call(typval_T *argvars, typval_T *rettv)
1989{
1990 char_u *func;
1991 partial_T *partial = NULL;
1992 dict_T *selfdict = NULL;
1993
1994 if (argvars[1].v_type != VAR_LIST)
1995 {
1996 EMSG(_(e_listreq));
1997 return;
1998 }
1999 if (argvars[1].vval.v_list == NULL)
2000 return;
2001
2002 if (argvars[0].v_type == VAR_FUNC)
2003 func = argvars[0].vval.v_string;
2004 else if (argvars[0].v_type == VAR_PARTIAL)
2005 {
2006 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002007 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002008 }
2009 else
2010 func = get_tv_string(&argvars[0]);
2011 if (*func == NUL)
2012 return; /* type error or empty name */
2013
2014 if (argvars[2].v_type != VAR_UNKNOWN)
2015 {
2016 if (argvars[2].v_type != VAR_DICT)
2017 {
2018 EMSG(_(e_dictreq));
2019 return;
2020 }
2021 selfdict = argvars[2].vval.v_dict;
2022 }
2023
2024 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2025}
2026
2027#ifdef FEAT_FLOAT
2028/*
2029 * "ceil({float})" function
2030 */
2031 static void
2032f_ceil(typval_T *argvars, typval_T *rettv)
2033{
2034 float_T f = 0.0;
2035
2036 rettv->v_type = VAR_FLOAT;
2037 if (get_float_arg(argvars, &f) == OK)
2038 rettv->vval.v_float = ceil(f);
2039 else
2040 rettv->vval.v_float = 0.0;
2041}
2042#endif
2043
2044#ifdef FEAT_JOB_CHANNEL
2045/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002046 * "ch_canread()" function
2047 */
2048 static void
2049f_ch_canread(typval_T *argvars, typval_T *rettv)
2050{
Bram Moolenaar958dc692016-12-01 15:34:12 +01002051 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002052
2053 rettv->vval.v_number = 0;
2054 if (channel != NULL)
2055 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
2056 || channel_has_readahead(channel, PART_OUT)
2057 || channel_has_readahead(channel, PART_ERR);
2058}
2059
2060/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002061 * "ch_close()" function
2062 */
2063 static void
2064f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
2065{
2066 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2067
2068 if (channel != NULL)
2069 {
2070 channel_close(channel, FALSE);
2071 channel_clear(channel);
2072 }
2073}
2074
2075/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002076 * "ch_close()" function
2077 */
2078 static void
2079f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
2080{
2081 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2082
2083 if (channel != NULL)
2084 channel_close_in(channel);
2085}
2086
2087/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002088 * "ch_getbufnr()" function
2089 */
2090 static void
2091f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
2092{
2093 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2094
2095 rettv->vval.v_number = -1;
2096 if (channel != NULL)
2097 {
2098 char_u *what = get_tv_string(&argvars[1]);
2099 int part;
2100
2101 if (STRCMP(what, "err") == 0)
2102 part = PART_ERR;
2103 else if (STRCMP(what, "out") == 0)
2104 part = PART_OUT;
2105 else if (STRCMP(what, "in") == 0)
2106 part = PART_IN;
2107 else
2108 part = PART_SOCK;
2109 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
2110 rettv->vval.v_number =
2111 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
2112 }
2113}
2114
2115/*
2116 * "ch_getjob()" function
2117 */
2118 static void
2119f_ch_getjob(typval_T *argvars, typval_T *rettv)
2120{
2121 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2122
2123 if (channel != NULL)
2124 {
2125 rettv->v_type = VAR_JOB;
2126 rettv->vval.v_job = channel->ch_job;
2127 if (channel->ch_job != NULL)
2128 ++channel->ch_job->jv_refcount;
2129 }
2130}
2131
2132/*
2133 * "ch_info()" function
2134 */
2135 static void
2136f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
2137{
2138 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2139
2140 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
2141 channel_info(channel, rettv->vval.v_dict);
2142}
2143
2144/*
2145 * "ch_log()" function
2146 */
2147 static void
2148f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
2149{
2150 char_u *msg = get_tv_string(&argvars[0]);
2151 channel_T *channel = NULL;
2152
2153 if (argvars[1].v_type != VAR_UNKNOWN)
2154 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
2155
Bram Moolenaard5359b22018-04-05 22:44:39 +02002156 ch_log(channel, "%s", msg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002157}
2158
2159/*
2160 * "ch_logfile()" function
2161 */
2162 static void
2163f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
2164{
2165 char_u *fname;
2166 char_u *opt = (char_u *)"";
2167 char_u buf[NUMBUFLEN];
2168
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02002169 /* Don't open a file in restricted mode. */
2170 if (check_restricted() || check_secure())
2171 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002172 fname = get_tv_string(&argvars[0]);
2173 if (argvars[1].v_type == VAR_STRING)
2174 opt = get_tv_string_buf(&argvars[1], buf);
2175 ch_logfile(fname, opt);
2176}
2177
2178/*
2179 * "ch_open()" function
2180 */
2181 static void
2182f_ch_open(typval_T *argvars, typval_T *rettv)
2183{
2184 rettv->v_type = VAR_CHANNEL;
2185 if (check_restricted() || check_secure())
2186 return;
2187 rettv->vval.v_channel = channel_open_func(argvars);
2188}
2189
2190/*
2191 * "ch_read()" function
2192 */
2193 static void
2194f_ch_read(typval_T *argvars, typval_T *rettv)
2195{
2196 common_channel_read(argvars, rettv, FALSE);
2197}
2198
2199/*
2200 * "ch_readraw()" function
2201 */
2202 static void
2203f_ch_readraw(typval_T *argvars, typval_T *rettv)
2204{
2205 common_channel_read(argvars, rettv, TRUE);
2206}
2207
2208/*
2209 * "ch_evalexpr()" function
2210 */
2211 static void
2212f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2213{
2214 ch_expr_common(argvars, rettv, TRUE);
2215}
2216
2217/*
2218 * "ch_sendexpr()" function
2219 */
2220 static void
2221f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2222{
2223 ch_expr_common(argvars, rettv, FALSE);
2224}
2225
2226/*
2227 * "ch_evalraw()" function
2228 */
2229 static void
2230f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2231{
2232 ch_raw_common(argvars, rettv, TRUE);
2233}
2234
2235/*
2236 * "ch_sendraw()" function
2237 */
2238 static void
2239f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2240{
2241 ch_raw_common(argvars, rettv, FALSE);
2242}
2243
2244/*
2245 * "ch_setoptions()" function
2246 */
2247 static void
2248f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2249{
2250 channel_T *channel;
2251 jobopt_T opt;
2252
2253 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2254 if (channel == NULL)
2255 return;
2256 clear_job_options(&opt);
2257 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002258 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002259 channel_set_options(channel, &opt);
2260 free_job_options(&opt);
2261}
2262
2263/*
2264 * "ch_status()" function
2265 */
2266 static void
2267f_ch_status(typval_T *argvars, typval_T *rettv)
2268{
2269 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002270 jobopt_T opt;
2271 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002272
2273 /* return an empty string by default */
2274 rettv->v_type = VAR_STRING;
2275 rettv->vval.v_string = NULL;
2276
2277 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002278
2279 if (argvars[1].v_type != VAR_UNKNOWN)
2280 {
2281 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002282 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002283 && (opt.jo_set & JO_PART))
2284 part = opt.jo_part;
2285 }
2286
2287 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002288}
2289#endif
2290
2291/*
2292 * "changenr()" function
2293 */
2294 static void
2295f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2296{
2297 rettv->vval.v_number = curbuf->b_u_seq_cur;
2298}
2299
2300/*
2301 * "char2nr(string)" function
2302 */
2303 static void
2304f_char2nr(typval_T *argvars, typval_T *rettv)
2305{
2306#ifdef FEAT_MBYTE
2307 if (has_mbyte)
2308 {
2309 int utf8 = 0;
2310
2311 if (argvars[1].v_type != VAR_UNKNOWN)
2312 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
2313
2314 if (utf8)
2315 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
2316 else
2317 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
2318 }
2319 else
2320#endif
2321 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
2322}
2323
2324/*
2325 * "cindent(lnum)" function
2326 */
2327 static void
2328f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2329{
2330#ifdef FEAT_CINDENT
2331 pos_T pos;
2332 linenr_T lnum;
2333
2334 pos = curwin->w_cursor;
2335 lnum = get_tv_lnum(argvars);
2336 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2337 {
2338 curwin->w_cursor.lnum = lnum;
2339 rettv->vval.v_number = get_c_indent();
2340 curwin->w_cursor = pos;
2341 }
2342 else
2343#endif
2344 rettv->vval.v_number = -1;
2345}
2346
2347/*
2348 * "clearmatches()" function
2349 */
2350 static void
2351f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2352{
2353#ifdef FEAT_SEARCH_EXTRA
2354 clear_matches(curwin);
2355#endif
2356}
2357
2358/*
2359 * "col(string)" function
2360 */
2361 static void
2362f_col(typval_T *argvars, typval_T *rettv)
2363{
2364 colnr_T col = 0;
2365 pos_T *fp;
2366 int fnum = curbuf->b_fnum;
2367
2368 fp = var2fpos(&argvars[0], FALSE, &fnum);
2369 if (fp != NULL && fnum == curbuf->b_fnum)
2370 {
2371 if (fp->col == MAXCOL)
2372 {
2373 /* '> can be MAXCOL, get the length of the line then */
2374 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2375 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2376 else
2377 col = MAXCOL;
2378 }
2379 else
2380 {
2381 col = fp->col + 1;
2382#ifdef FEAT_VIRTUALEDIT
2383 /* col(".") when the cursor is on the NUL at the end of the line
2384 * because of "coladd" can be seen as an extra column. */
2385 if (virtual_active() && fp == &curwin->w_cursor)
2386 {
2387 char_u *p = ml_get_cursor();
2388
2389 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2390 curwin->w_virtcol - curwin->w_cursor.coladd))
2391 {
2392# ifdef FEAT_MBYTE
2393 int l;
2394
2395 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2396 col += l;
2397# else
2398 if (*p != NUL && p[1] == NUL)
2399 ++col;
2400# endif
2401 }
2402 }
2403#endif
2404 }
2405 }
2406 rettv->vval.v_number = col;
2407}
2408
2409#if defined(FEAT_INS_EXPAND)
2410/*
2411 * "complete()" function
2412 */
2413 static void
2414f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2415{
2416 int startcol;
2417
2418 if ((State & INSERT) == 0)
2419 {
2420 EMSG(_("E785: complete() can only be used in Insert mode"));
2421 return;
2422 }
2423
2424 /* Check for undo allowed here, because if something was already inserted
2425 * the line was already saved for undo and this check isn't done. */
2426 if (!undo_allowed())
2427 return;
2428
2429 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2430 {
2431 EMSG(_(e_invarg));
2432 return;
2433 }
2434
2435 startcol = (int)get_tv_number_chk(&argvars[0], NULL);
2436 if (startcol <= 0)
2437 return;
2438
2439 set_completion(startcol - 1, argvars[1].vval.v_list);
2440}
2441
2442/*
2443 * "complete_add()" function
2444 */
2445 static void
2446f_complete_add(typval_T *argvars, typval_T *rettv)
2447{
2448 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2449}
2450
2451/*
2452 * "complete_check()" function
2453 */
2454 static void
2455f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2456{
2457 int saved = RedrawingDisabled;
2458
2459 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002460 ins_compl_check_keys(0, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002461 rettv->vval.v_number = compl_interrupted;
2462 RedrawingDisabled = saved;
2463}
2464#endif
2465
2466/*
2467 * "confirm(message, buttons[, default [, type]])" function
2468 */
2469 static void
2470f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2471{
2472#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2473 char_u *message;
2474 char_u *buttons = NULL;
2475 char_u buf[NUMBUFLEN];
2476 char_u buf2[NUMBUFLEN];
2477 int def = 1;
2478 int type = VIM_GENERIC;
2479 char_u *typestr;
2480 int error = FALSE;
2481
2482 message = get_tv_string_chk(&argvars[0]);
2483 if (message == NULL)
2484 error = TRUE;
2485 if (argvars[1].v_type != VAR_UNKNOWN)
2486 {
2487 buttons = get_tv_string_buf_chk(&argvars[1], buf);
2488 if (buttons == NULL)
2489 error = TRUE;
2490 if (argvars[2].v_type != VAR_UNKNOWN)
2491 {
2492 def = (int)get_tv_number_chk(&argvars[2], &error);
2493 if (argvars[3].v_type != VAR_UNKNOWN)
2494 {
2495 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
2496 if (typestr == NULL)
2497 error = TRUE;
2498 else
2499 {
2500 switch (TOUPPER_ASC(*typestr))
2501 {
2502 case 'E': type = VIM_ERROR; break;
2503 case 'Q': type = VIM_QUESTION; break;
2504 case 'I': type = VIM_INFO; break;
2505 case 'W': type = VIM_WARNING; break;
2506 case 'G': type = VIM_GENERIC; break;
2507 }
2508 }
2509 }
2510 }
2511 }
2512
2513 if (buttons == NULL || *buttons == NUL)
2514 buttons = (char_u *)_("&Ok");
2515
2516 if (!error)
2517 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2518 def, NULL, FALSE);
2519#endif
2520}
2521
2522/*
2523 * "copy()" function
2524 */
2525 static void
2526f_copy(typval_T *argvars, typval_T *rettv)
2527{
2528 item_copy(&argvars[0], rettv, FALSE, 0);
2529}
2530
2531#ifdef FEAT_FLOAT
2532/*
2533 * "cos()" function
2534 */
2535 static void
2536f_cos(typval_T *argvars, typval_T *rettv)
2537{
2538 float_T f = 0.0;
2539
2540 rettv->v_type = VAR_FLOAT;
2541 if (get_float_arg(argvars, &f) == OK)
2542 rettv->vval.v_float = cos(f);
2543 else
2544 rettv->vval.v_float = 0.0;
2545}
2546
2547/*
2548 * "cosh()" function
2549 */
2550 static void
2551f_cosh(typval_T *argvars, typval_T *rettv)
2552{
2553 float_T f = 0.0;
2554
2555 rettv->v_type = VAR_FLOAT;
2556 if (get_float_arg(argvars, &f) == OK)
2557 rettv->vval.v_float = cosh(f);
2558 else
2559 rettv->vval.v_float = 0.0;
2560}
2561#endif
2562
2563/*
2564 * "count()" function
2565 */
2566 static void
2567f_count(typval_T *argvars, typval_T *rettv)
2568{
2569 long n = 0;
2570 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002571 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002572
Bram Moolenaar9966b212017-07-28 16:46:57 +02002573 if (argvars[2].v_type != VAR_UNKNOWN)
2574 ic = (int)get_tv_number_chk(&argvars[2], &error);
2575
2576 if (argvars[0].v_type == VAR_STRING)
2577 {
2578 char_u *expr = get_tv_string_chk(&argvars[1]);
2579 char_u *p = argvars[0].vval.v_string;
2580 char_u *next;
2581
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002582 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002583 {
2584 if (ic)
2585 {
2586 size_t len = STRLEN(expr);
2587
2588 while (*p != NUL)
2589 {
2590 if (MB_STRNICMP(p, expr, len) == 0)
2591 {
2592 ++n;
2593 p += len;
2594 }
2595 else
2596 MB_PTR_ADV(p);
2597 }
2598 }
2599 else
2600 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2601 != NULL)
2602 {
2603 ++n;
2604 p = next + STRLEN(expr);
2605 }
2606 }
2607
2608 }
2609 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002610 {
2611 listitem_T *li;
2612 list_T *l;
2613 long idx;
2614
2615 if ((l = argvars[0].vval.v_list) != NULL)
2616 {
2617 li = l->lv_first;
2618 if (argvars[2].v_type != VAR_UNKNOWN)
2619 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002620 if (argvars[3].v_type != VAR_UNKNOWN)
2621 {
2622 idx = (long)get_tv_number_chk(&argvars[3], &error);
2623 if (!error)
2624 {
2625 li = list_find(l, idx);
2626 if (li == NULL)
2627 EMSGN(_(e_listidx), idx);
2628 }
2629 }
2630 if (error)
2631 li = NULL;
2632 }
2633
2634 for ( ; li != NULL; li = li->li_next)
2635 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2636 ++n;
2637 }
2638 }
2639 else if (argvars[0].v_type == VAR_DICT)
2640 {
2641 int todo;
2642 dict_T *d;
2643 hashitem_T *hi;
2644
2645 if ((d = argvars[0].vval.v_dict) != NULL)
2646 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002647 if (argvars[2].v_type != VAR_UNKNOWN)
2648 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002649 if (argvars[3].v_type != VAR_UNKNOWN)
2650 EMSG(_(e_invarg));
2651 }
2652
2653 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2654 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2655 {
2656 if (!HASHITEM_EMPTY(hi))
2657 {
2658 --todo;
2659 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2660 ++n;
2661 }
2662 }
2663 }
2664 }
2665 else
2666 EMSG2(_(e_listdictarg), "count()");
2667 rettv->vval.v_number = n;
2668}
2669
2670/*
2671 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2672 *
2673 * Checks the existence of a cscope connection.
2674 */
2675 static void
2676f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2677{
2678#ifdef FEAT_CSCOPE
2679 int num = 0;
2680 char_u *dbpath = NULL;
2681 char_u *prepend = NULL;
2682 char_u buf[NUMBUFLEN];
2683
2684 if (argvars[0].v_type != VAR_UNKNOWN
2685 && argvars[1].v_type != VAR_UNKNOWN)
2686 {
2687 num = (int)get_tv_number(&argvars[0]);
2688 dbpath = get_tv_string(&argvars[1]);
2689 if (argvars[2].v_type != VAR_UNKNOWN)
2690 prepend = get_tv_string_buf(&argvars[2], buf);
2691 }
2692
2693 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2694#endif
2695}
2696
2697/*
2698 * "cursor(lnum, col)" function, or
2699 * "cursor(list)"
2700 *
2701 * Moves the cursor to the specified line and column.
2702 * Returns 0 when the position could be set, -1 otherwise.
2703 */
2704 static void
2705f_cursor(typval_T *argvars, typval_T *rettv)
2706{
2707 long line, col;
2708#ifdef FEAT_VIRTUALEDIT
2709 long coladd = 0;
2710#endif
2711 int set_curswant = TRUE;
2712
2713 rettv->vval.v_number = -1;
2714 if (argvars[1].v_type == VAR_UNKNOWN)
2715 {
2716 pos_T pos;
2717 colnr_T curswant = -1;
2718
2719 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2720 {
2721 EMSG(_(e_invarg));
2722 return;
2723 }
2724 line = pos.lnum;
2725 col = pos.col;
2726#ifdef FEAT_VIRTUALEDIT
2727 coladd = pos.coladd;
2728#endif
2729 if (curswant >= 0)
2730 {
2731 curwin->w_curswant = curswant - 1;
2732 set_curswant = FALSE;
2733 }
2734 }
2735 else
2736 {
2737 line = get_tv_lnum(argvars);
2738 col = (long)get_tv_number_chk(&argvars[1], NULL);
2739#ifdef FEAT_VIRTUALEDIT
2740 if (argvars[2].v_type != VAR_UNKNOWN)
2741 coladd = (long)get_tv_number_chk(&argvars[2], NULL);
2742#endif
2743 }
2744 if (line < 0 || col < 0
2745#ifdef FEAT_VIRTUALEDIT
2746 || coladd < 0
2747#endif
2748 )
2749 return; /* type error; errmsg already given */
2750 if (line > 0)
2751 curwin->w_cursor.lnum = line;
2752 if (col > 0)
2753 curwin->w_cursor.col = col - 1;
2754#ifdef FEAT_VIRTUALEDIT
2755 curwin->w_cursor.coladd = coladd;
2756#endif
2757
2758 /* Make sure the cursor is in a valid position. */
2759 check_cursor();
2760#ifdef FEAT_MBYTE
2761 /* Correct cursor for multi-byte character. */
2762 if (has_mbyte)
2763 mb_adjust_cursor();
2764#endif
2765
2766 curwin->w_set_curswant = set_curswant;
2767 rettv->vval.v_number = 0;
2768}
2769
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002770#ifdef WIN3264
2771/*
2772 * "debugbreak()" function
2773 */
2774 static void
2775f_debugbreak(typval_T *argvars, typval_T *rettv)
2776{
2777 int pid;
2778
2779 rettv->vval.v_number = FAIL;
2780 pid = (int)get_tv_number(&argvars[0]);
2781 if (pid == 0)
2782 EMSG(_(e_invarg));
2783 else
2784 {
2785 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2786
2787 if (hProcess != NULL)
2788 {
2789 DebugBreakProcess(hProcess);
2790 CloseHandle(hProcess);
2791 rettv->vval.v_number = OK;
2792 }
2793 }
2794}
2795#endif
2796
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002797/*
2798 * "deepcopy()" function
2799 */
2800 static void
2801f_deepcopy(typval_T *argvars, typval_T *rettv)
2802{
2803 int noref = 0;
2804 int copyID;
2805
2806 if (argvars[1].v_type != VAR_UNKNOWN)
2807 noref = (int)get_tv_number_chk(&argvars[1], NULL);
2808 if (noref < 0 || noref > 1)
2809 EMSG(_(e_invarg));
2810 else
2811 {
2812 copyID = get_copyID();
2813 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2814 }
2815}
2816
2817/*
2818 * "delete()" function
2819 */
2820 static void
2821f_delete(typval_T *argvars, typval_T *rettv)
2822{
2823 char_u nbuf[NUMBUFLEN];
2824 char_u *name;
2825 char_u *flags;
2826
2827 rettv->vval.v_number = -1;
2828 if (check_restricted() || check_secure())
2829 return;
2830
2831 name = get_tv_string(&argvars[0]);
2832 if (name == NULL || *name == NUL)
2833 {
2834 EMSG(_(e_invarg));
2835 return;
2836 }
2837
2838 if (argvars[1].v_type != VAR_UNKNOWN)
2839 flags = get_tv_string_buf(&argvars[1], nbuf);
2840 else
2841 flags = (char_u *)"";
2842
2843 if (*flags == NUL)
2844 /* delete a file */
2845 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2846 else if (STRCMP(flags, "d") == 0)
2847 /* delete an empty directory */
2848 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2849 else if (STRCMP(flags, "rf") == 0)
2850 /* delete a directory recursively */
2851 rettv->vval.v_number = delete_recursive(name);
2852 else
2853 EMSG2(_(e_invexpr2), flags);
2854}
2855
2856/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02002857 * "deletebufline()" function
2858 */
2859 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02002860f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02002861{
2862 buf_T *buf;
2863 linenr_T first, last;
2864 linenr_T lnum;
2865 long count;
2866 int is_curbuf;
2867 buf_T *curbuf_save = NULL;
2868 win_T *curwin_save = NULL;
2869 tabpage_T *tp;
2870 win_T *wp;
2871
2872 buf = get_buf_tv(&argvars[0], FALSE);
2873 if (buf == NULL)
2874 {
2875 rettv->vval.v_number = 1; /* FAIL */
2876 return;
2877 }
2878 is_curbuf = buf == curbuf;
2879
2880 first = get_tv_lnum_buf(&argvars[1], buf);
2881 if (argvars[2].v_type != VAR_UNKNOWN)
2882 last = get_tv_lnum_buf(&argvars[2], buf);
2883 else
2884 last = first;
2885
2886 if (buf->b_ml.ml_mfp == NULL || first < 1
2887 || first > buf->b_ml.ml_line_count || last < first)
2888 {
2889 rettv->vval.v_number = 1; /* FAIL */
2890 return;
2891 }
2892
2893 if (!is_curbuf)
2894 {
2895 curbuf_save = curbuf;
2896 curwin_save = curwin;
2897 curbuf = buf;
2898 find_win_for_curbuf();
2899 }
2900 if (last > curbuf->b_ml.ml_line_count)
2901 last = curbuf->b_ml.ml_line_count;
2902 count = last - first + 1;
2903
2904 // When coming here from Insert mode, sync undo, so that this can be
2905 // undone separately from what was previously inserted.
2906 if (u_sync_once == 2)
2907 {
2908 u_sync_once = 1; // notify that u_sync() was called
2909 u_sync(TRUE);
2910 }
2911
2912 if (u_save(first - 1, last + 1) == FAIL)
2913 {
2914 rettv->vval.v_number = 1; /* FAIL */
2915 return;
2916 }
2917
2918 for (lnum = first; lnum <= last; ++lnum)
2919 ml_delete(first, TRUE);
2920
2921 FOR_ALL_TAB_WINDOWS(tp, wp)
2922 if (wp->w_buffer == buf)
2923 {
2924 if (wp->w_cursor.lnum > last)
2925 wp->w_cursor.lnum -= count;
2926 else if (wp->w_cursor.lnum> first)
2927 wp->w_cursor.lnum = first;
2928 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
2929 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
2930 }
2931 check_cursor_col();
2932 deleted_lines_mark(first, count);
2933
2934 if (!is_curbuf)
2935 {
2936 curbuf = curbuf_save;
2937 curwin = curwin_save;
2938 }
2939}
2940
2941/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002942 * "did_filetype()" function
2943 */
2944 static void
2945f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2946{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002947 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002948}
2949
2950/*
2951 * "diff_filler()" function
2952 */
2953 static void
2954f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2955{
2956#ifdef FEAT_DIFF
2957 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
2958#endif
2959}
2960
2961/*
2962 * "diff_hlID()" function
2963 */
2964 static void
2965f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2966{
2967#ifdef FEAT_DIFF
2968 linenr_T lnum = get_tv_lnum(argvars);
2969 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01002970 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002971 static int fnum = 0;
2972 static int change_start = 0;
2973 static int change_end = 0;
2974 static hlf_T hlID = (hlf_T)0;
2975 int filler_lines;
2976 int col;
2977
2978 if (lnum < 0) /* ignore type error in {lnum} arg */
2979 lnum = 0;
2980 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002981 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002982 || fnum != curbuf->b_fnum)
2983 {
2984 /* New line, buffer, change: need to get the values. */
2985 filler_lines = diff_check(curwin, lnum);
2986 if (filler_lines < 0)
2987 {
2988 if (filler_lines == -1)
2989 {
2990 change_start = MAXCOL;
2991 change_end = -1;
2992 if (diff_find_change(curwin, lnum, &change_start, &change_end))
2993 hlID = HLF_ADD; /* added line */
2994 else
2995 hlID = HLF_CHD; /* changed line */
2996 }
2997 else
2998 hlID = HLF_ADD; /* added line */
2999 }
3000 else
3001 hlID = (hlf_T)0;
3002 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003003 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003004 fnum = curbuf->b_fnum;
3005 }
3006
3007 if (hlID == HLF_CHD || hlID == HLF_TXD)
3008 {
3009 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
3010 if (col >= change_start && col <= change_end)
3011 hlID = HLF_TXD; /* changed text */
3012 else
3013 hlID = HLF_CHD; /* changed line */
3014 }
3015 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
3016#endif
3017}
3018
3019/*
3020 * "empty({expr})" function
3021 */
3022 static void
3023f_empty(typval_T *argvars, typval_T *rettv)
3024{
3025 int n = FALSE;
3026
3027 switch (argvars[0].v_type)
3028 {
3029 case VAR_STRING:
3030 case VAR_FUNC:
3031 n = argvars[0].vval.v_string == NULL
3032 || *argvars[0].vval.v_string == NUL;
3033 break;
3034 case VAR_PARTIAL:
3035 n = FALSE;
3036 break;
3037 case VAR_NUMBER:
3038 n = argvars[0].vval.v_number == 0;
3039 break;
3040 case VAR_FLOAT:
3041#ifdef FEAT_FLOAT
3042 n = argvars[0].vval.v_float == 0.0;
3043 break;
3044#endif
3045 case VAR_LIST:
3046 n = argvars[0].vval.v_list == NULL
3047 || argvars[0].vval.v_list->lv_first == NULL;
3048 break;
3049 case VAR_DICT:
3050 n = argvars[0].vval.v_dict == NULL
3051 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
3052 break;
3053 case VAR_SPECIAL:
3054 n = argvars[0].vval.v_number != VVAL_TRUE;
3055 break;
3056
3057 case VAR_JOB:
3058#ifdef FEAT_JOB_CHANNEL
3059 n = argvars[0].vval.v_job == NULL
3060 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
3061 break;
3062#endif
3063 case VAR_CHANNEL:
3064#ifdef FEAT_JOB_CHANNEL
3065 n = argvars[0].vval.v_channel == NULL
3066 || !channel_is_open(argvars[0].vval.v_channel);
3067 break;
3068#endif
3069 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01003070 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003071 n = TRUE;
3072 break;
3073 }
3074
3075 rettv->vval.v_number = n;
3076}
3077
3078/*
3079 * "escape({string}, {chars})" function
3080 */
3081 static void
3082f_escape(typval_T *argvars, typval_T *rettv)
3083{
3084 char_u buf[NUMBUFLEN];
3085
3086 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
3087 get_tv_string_buf(&argvars[1], buf));
3088 rettv->v_type = VAR_STRING;
3089}
3090
3091/*
3092 * "eval()" function
3093 */
3094 static void
3095f_eval(typval_T *argvars, typval_T *rettv)
3096{
3097 char_u *s, *p;
3098
3099 s = get_tv_string_chk(&argvars[0]);
3100 if (s != NULL)
3101 s = skipwhite(s);
3102
3103 p = s;
3104 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3105 {
3106 if (p != NULL && !aborting())
3107 EMSG2(_(e_invexpr2), p);
3108 need_clr_eos = FALSE;
3109 rettv->v_type = VAR_NUMBER;
3110 rettv->vval.v_number = 0;
3111 }
3112 else if (*s != NUL)
3113 EMSG(_(e_trailing));
3114}
3115
3116/*
3117 * "eventhandler()" function
3118 */
3119 static void
3120f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3121{
3122 rettv->vval.v_number = vgetc_busy;
3123}
3124
3125/*
3126 * "executable()" function
3127 */
3128 static void
3129f_executable(typval_T *argvars, typval_T *rettv)
3130{
3131 char_u *name = get_tv_string(&argvars[0]);
3132
3133 /* Check in $PATH and also check directly if there is a directory name. */
3134 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
3135 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
3136}
3137
3138static garray_T redir_execute_ga;
3139
3140/*
3141 * Append "value[value_len]" to the execute() output.
3142 */
3143 void
3144execute_redir_str(char_u *value, int value_len)
3145{
3146 int len;
3147
3148 if (value_len == -1)
3149 len = (int)STRLEN(value); /* Append the entire string */
3150 else
3151 len = value_len; /* Append only "value_len" characters */
3152 if (ga_grow(&redir_execute_ga, len) == OK)
3153 {
3154 mch_memmove((char *)redir_execute_ga.ga_data
3155 + redir_execute_ga.ga_len, value, len);
3156 redir_execute_ga.ga_len += len;
3157 }
3158}
3159
3160/*
3161 * Get next line from a list.
3162 * Called by do_cmdline() to get the next line.
3163 * Returns allocated string, or NULL for end of function.
3164 */
3165
3166 static char_u *
3167get_list_line(
3168 int c UNUSED,
3169 void *cookie,
3170 int indent UNUSED)
3171{
3172 listitem_T **p = (listitem_T **)cookie;
3173 listitem_T *item = *p;
3174 char_u buf[NUMBUFLEN];
3175 char_u *s;
3176
3177 if (item == NULL)
3178 return NULL;
3179 s = get_tv_string_buf_chk(&item->li_tv, buf);
3180 *p = item->li_next;
3181 return s == NULL ? NULL : vim_strsave(s);
3182}
3183
3184/*
3185 * "execute()" function
3186 */
3187 static void
3188f_execute(typval_T *argvars, typval_T *rettv)
3189{
3190 char_u *cmd = NULL;
3191 list_T *list = NULL;
3192 int save_msg_silent = msg_silent;
3193 int save_emsg_silent = emsg_silent;
3194 int save_emsg_noredir = emsg_noredir;
3195 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003196 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003197 garray_T save_ga;
3198
3199 rettv->vval.v_string = NULL;
3200 rettv->v_type = VAR_STRING;
3201
3202 if (argvars[0].v_type == VAR_LIST)
3203 {
3204 list = argvars[0].vval.v_list;
3205 if (list == NULL || list->lv_first == NULL)
3206 /* empty list, no commands, empty output */
3207 return;
3208 ++list->lv_refcount;
3209 }
3210 else
3211 {
3212 cmd = get_tv_string_chk(&argvars[0]);
3213 if (cmd == NULL)
3214 return;
3215 }
3216
3217 if (argvars[1].v_type != VAR_UNKNOWN)
3218 {
3219 char_u buf[NUMBUFLEN];
3220 char_u *s = get_tv_string_buf_chk(&argvars[1], buf);
3221
3222 if (s == NULL)
3223 return;
3224 if (STRNCMP(s, "silent", 6) == 0)
3225 ++msg_silent;
3226 if (STRCMP(s, "silent!") == 0)
3227 {
3228 emsg_silent = TRUE;
3229 emsg_noredir = TRUE;
3230 }
3231 }
3232 else
3233 ++msg_silent;
3234
3235 if (redir_execute)
3236 save_ga = redir_execute_ga;
3237 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3238 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003239 redir_off = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003240
3241 if (cmd != NULL)
3242 do_cmdline_cmd(cmd);
3243 else
3244 {
3245 listitem_T *item = list->lv_first;
3246
3247 do_cmdline(NULL, get_list_line, (void *)&item,
3248 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3249 --list->lv_refcount;
3250 }
3251
Bram Moolenaard297f352017-01-29 20:31:21 +01003252 /* Need to append a NUL to the result. */
3253 if (ga_grow(&redir_execute_ga, 1) == OK)
3254 {
3255 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3256 rettv->vval.v_string = redir_execute_ga.ga_data;
3257 }
3258 else
3259 {
3260 ga_clear(&redir_execute_ga);
3261 rettv->vval.v_string = NULL;
3262 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003263 msg_silent = save_msg_silent;
3264 emsg_silent = save_emsg_silent;
3265 emsg_noredir = save_emsg_noredir;
3266
3267 redir_execute = save_redir_execute;
3268 if (redir_execute)
3269 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003270 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003271
3272 /* "silent reg" or "silent echo x" leaves msg_col somewhere in the
3273 * line. Put it back in the first column. */
3274 msg_col = 0;
3275}
3276
3277/*
3278 * "exepath()" function
3279 */
3280 static void
3281f_exepath(typval_T *argvars, typval_T *rettv)
3282{
3283 char_u *p = NULL;
3284
3285 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
3286 rettv->v_type = VAR_STRING;
3287 rettv->vval.v_string = p;
3288}
3289
3290/*
3291 * "exists()" function
3292 */
3293 static void
3294f_exists(typval_T *argvars, typval_T *rettv)
3295{
3296 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003297 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003298
3299 p = get_tv_string(&argvars[0]);
3300 if (*p == '$') /* environment variable */
3301 {
3302 /* first try "normal" environment variables (fast) */
3303 if (mch_getenv(p + 1) != NULL)
3304 n = TRUE;
3305 else
3306 {
3307 /* try expanding things like $VIM and ${HOME} */
3308 p = expand_env_save(p);
3309 if (p != NULL && *p != '$')
3310 n = TRUE;
3311 vim_free(p);
3312 }
3313 }
3314 else if (*p == '&' || *p == '+') /* option */
3315 {
3316 n = (get_option_tv(&p, NULL, TRUE) == OK);
3317 if (*skipwhite(p) != NUL)
3318 n = FALSE; /* trailing garbage */
3319 }
3320 else if (*p == '*') /* internal or user defined function */
3321 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003322 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003323 }
3324 else if (*p == ':')
3325 {
3326 n = cmd_exists(p + 1);
3327 }
3328 else if (*p == '#')
3329 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003330 if (p[1] == '#')
3331 n = autocmd_supported(p + 2);
3332 else
3333 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003334 }
3335 else /* internal variable */
3336 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003337 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003338 }
3339
3340 rettv->vval.v_number = n;
3341}
3342
3343#ifdef FEAT_FLOAT
3344/*
3345 * "exp()" function
3346 */
3347 static void
3348f_exp(typval_T *argvars, typval_T *rettv)
3349{
3350 float_T f = 0.0;
3351
3352 rettv->v_type = VAR_FLOAT;
3353 if (get_float_arg(argvars, &f) == OK)
3354 rettv->vval.v_float = exp(f);
3355 else
3356 rettv->vval.v_float = 0.0;
3357}
3358#endif
3359
3360/*
3361 * "expand()" function
3362 */
3363 static void
3364f_expand(typval_T *argvars, typval_T *rettv)
3365{
3366 char_u *s;
3367 int len;
3368 char_u *errormsg;
3369 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3370 expand_T xpc;
3371 int error = FALSE;
3372 char_u *result;
3373
3374 rettv->v_type = VAR_STRING;
3375 if (argvars[1].v_type != VAR_UNKNOWN
3376 && argvars[2].v_type != VAR_UNKNOWN
3377 && get_tv_number_chk(&argvars[2], &error)
3378 && !error)
3379 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003380 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003381 }
3382
3383 s = get_tv_string(&argvars[0]);
3384 if (*s == '%' || *s == '#' || *s == '<')
3385 {
3386 ++emsg_off;
3387 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3388 --emsg_off;
3389 if (rettv->v_type == VAR_LIST)
3390 {
3391 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3392 list_append_string(rettv->vval.v_list, result, -1);
3393 else
3394 vim_free(result);
3395 }
3396 else
3397 rettv->vval.v_string = result;
3398 }
3399 else
3400 {
3401 /* When the optional second argument is non-zero, don't remove matches
3402 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3403 if (argvars[1].v_type != VAR_UNKNOWN
3404 && get_tv_number_chk(&argvars[1], &error))
3405 options |= WILD_KEEP_ALL;
3406 if (!error)
3407 {
3408 ExpandInit(&xpc);
3409 xpc.xp_context = EXPAND_FILES;
3410 if (p_wic)
3411 options += WILD_ICASE;
3412 if (rettv->v_type == VAR_STRING)
3413 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3414 options, WILD_ALL);
3415 else if (rettv_list_alloc(rettv) != FAIL)
3416 {
3417 int i;
3418
3419 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3420 for (i = 0; i < xpc.xp_numfiles; i++)
3421 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3422 ExpandCleanup(&xpc);
3423 }
3424 }
3425 else
3426 rettv->vval.v_string = NULL;
3427 }
3428}
3429
3430/*
3431 * "extend(list, list [, idx])" function
3432 * "extend(dict, dict [, action])" function
3433 */
3434 static void
3435f_extend(typval_T *argvars, typval_T *rettv)
3436{
3437 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3438
3439 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3440 {
3441 list_T *l1, *l2;
3442 listitem_T *item;
3443 long before;
3444 int error = FALSE;
3445
3446 l1 = argvars[0].vval.v_list;
3447 l2 = argvars[1].vval.v_list;
3448 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
3449 && l2 != NULL)
3450 {
3451 if (argvars[2].v_type != VAR_UNKNOWN)
3452 {
3453 before = (long)get_tv_number_chk(&argvars[2], &error);
3454 if (error)
3455 return; /* type error; errmsg already given */
3456
3457 if (before == l1->lv_len)
3458 item = NULL;
3459 else
3460 {
3461 item = list_find(l1, before);
3462 if (item == NULL)
3463 {
3464 EMSGN(_(e_listidx), before);
3465 return;
3466 }
3467 }
3468 }
3469 else
3470 item = NULL;
3471 list_extend(l1, l2, item);
3472
3473 copy_tv(&argvars[0], rettv);
3474 }
3475 }
3476 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3477 {
3478 dict_T *d1, *d2;
3479 char_u *action;
3480 int i;
3481
3482 d1 = argvars[0].vval.v_dict;
3483 d2 = argvars[1].vval.v_dict;
3484 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
3485 && d2 != NULL)
3486 {
3487 /* Check the third argument. */
3488 if (argvars[2].v_type != VAR_UNKNOWN)
3489 {
3490 static char *(av[]) = {"keep", "force", "error"};
3491
3492 action = get_tv_string_chk(&argvars[2]);
3493 if (action == NULL)
3494 return; /* type error; errmsg already given */
3495 for (i = 0; i < 3; ++i)
3496 if (STRCMP(action, av[i]) == 0)
3497 break;
3498 if (i == 3)
3499 {
3500 EMSG2(_(e_invarg2), action);
3501 return;
3502 }
3503 }
3504 else
3505 action = (char_u *)"force";
3506
3507 dict_extend(d1, d2, action);
3508
3509 copy_tv(&argvars[0], rettv);
3510 }
3511 }
3512 else
3513 EMSG2(_(e_listdictarg), "extend()");
3514}
3515
3516/*
3517 * "feedkeys()" function
3518 */
3519 static void
3520f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3521{
3522 int remap = TRUE;
3523 int insert = FALSE;
3524 char_u *keys, *flags;
3525 char_u nbuf[NUMBUFLEN];
3526 int typed = FALSE;
3527 int execute = FALSE;
3528 int dangerous = FALSE;
3529 char_u *keys_esc;
3530
3531 /* This is not allowed in the sandbox. If the commands would still be
3532 * executed in the sandbox it would be OK, but it probably happens later,
3533 * when "sandbox" is no longer set. */
3534 if (check_secure())
3535 return;
3536
3537 keys = get_tv_string(&argvars[0]);
3538
3539 if (argvars[1].v_type != VAR_UNKNOWN)
3540 {
3541 flags = get_tv_string_buf(&argvars[1], nbuf);
3542 for ( ; *flags != NUL; ++flags)
3543 {
3544 switch (*flags)
3545 {
3546 case 'n': remap = FALSE; break;
3547 case 'm': remap = TRUE; break;
3548 case 't': typed = TRUE; break;
3549 case 'i': insert = TRUE; break;
3550 case 'x': execute = TRUE; break;
3551 case '!': dangerous = TRUE; break;
3552 }
3553 }
3554 }
3555
3556 if (*keys != NUL || execute)
3557 {
3558 /* Need to escape K_SPECIAL and CSI before putting the string in the
3559 * typeahead buffer. */
3560 keys_esc = vim_strsave_escape_csi(keys);
3561 if (keys_esc != NULL)
3562 {
3563 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
3564 insert ? 0 : typebuf.tb_len, !typed, FALSE);
3565 vim_free(keys_esc);
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003566 if (vgetc_busy
3567#ifdef FEAT_TIMERS
3568 || timer_busy
3569#endif
3570 )
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003571 typebuf_was_filled = TRUE;
3572 if (execute)
3573 {
3574 int save_msg_scroll = msg_scroll;
3575
3576 /* Avoid a 1 second delay when the keys start Insert mode. */
3577 msg_scroll = FALSE;
3578
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003579 if (!dangerous)
3580 ++ex_normal_busy;
Bram Moolenaar655a82a2018-05-08 22:01:07 +02003581 exec_normal(TRUE, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003582 if (!dangerous)
3583 --ex_normal_busy;
3584
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003585 msg_scroll |= save_msg_scroll;
3586 }
3587 }
3588 }
3589}
3590
3591/*
3592 * "filereadable()" function
3593 */
3594 static void
3595f_filereadable(typval_T *argvars, typval_T *rettv)
3596{
3597 int fd;
3598 char_u *p;
3599 int n;
3600
3601#ifndef O_NONBLOCK
3602# define O_NONBLOCK 0
3603#endif
3604 p = get_tv_string(&argvars[0]);
3605 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3606 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3607 {
3608 n = TRUE;
3609 close(fd);
3610 }
3611 else
3612 n = FALSE;
3613
3614 rettv->vval.v_number = n;
3615}
3616
3617/*
3618 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3619 * rights to write into.
3620 */
3621 static void
3622f_filewritable(typval_T *argvars, typval_T *rettv)
3623{
3624 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
3625}
3626
3627 static void
3628findfilendir(
3629 typval_T *argvars UNUSED,
3630 typval_T *rettv,
3631 int find_what UNUSED)
3632{
3633#ifdef FEAT_SEARCHPATH
3634 char_u *fname;
3635 char_u *fresult = NULL;
3636 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3637 char_u *p;
3638 char_u pathbuf[NUMBUFLEN];
3639 int count = 1;
3640 int first = TRUE;
3641 int error = FALSE;
3642#endif
3643
3644 rettv->vval.v_string = NULL;
3645 rettv->v_type = VAR_STRING;
3646
3647#ifdef FEAT_SEARCHPATH
3648 fname = get_tv_string(&argvars[0]);
3649
3650 if (argvars[1].v_type != VAR_UNKNOWN)
3651 {
3652 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
3653 if (p == NULL)
3654 error = TRUE;
3655 else
3656 {
3657 if (*p != NUL)
3658 path = p;
3659
3660 if (argvars[2].v_type != VAR_UNKNOWN)
3661 count = (int)get_tv_number_chk(&argvars[2], &error);
3662 }
3663 }
3664
3665 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3666 error = TRUE;
3667
3668 if (*fname != NUL && !error)
3669 {
3670 do
3671 {
3672 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3673 vim_free(fresult);
3674 fresult = find_file_in_path_option(first ? fname : NULL,
3675 first ? (int)STRLEN(fname) : 0,
3676 0, first, path,
3677 find_what,
3678 curbuf->b_ffname,
3679 find_what == FINDFILE_DIR
3680 ? (char_u *)"" : curbuf->b_p_sua);
3681 first = FALSE;
3682
3683 if (fresult != NULL && rettv->v_type == VAR_LIST)
3684 list_append_string(rettv->vval.v_list, fresult, -1);
3685
3686 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3687 }
3688
3689 if (rettv->v_type == VAR_STRING)
3690 rettv->vval.v_string = fresult;
3691#endif
3692}
3693
3694/*
3695 * "filter()" function
3696 */
3697 static void
3698f_filter(typval_T *argvars, typval_T *rettv)
3699{
3700 filter_map(argvars, rettv, FALSE);
3701}
3702
3703/*
3704 * "finddir({fname}[, {path}[, {count}]])" function
3705 */
3706 static void
3707f_finddir(typval_T *argvars, typval_T *rettv)
3708{
3709 findfilendir(argvars, rettv, FINDFILE_DIR);
3710}
3711
3712/*
3713 * "findfile({fname}[, {path}[, {count}]])" function
3714 */
3715 static void
3716f_findfile(typval_T *argvars, typval_T *rettv)
3717{
3718 findfilendir(argvars, rettv, FINDFILE_FILE);
3719}
3720
3721#ifdef FEAT_FLOAT
3722/*
3723 * "float2nr({float})" function
3724 */
3725 static void
3726f_float2nr(typval_T *argvars, typval_T *rettv)
3727{
3728 float_T f = 0.0;
3729
3730 if (get_float_arg(argvars, &f) == OK)
3731 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003732 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003733 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003734 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003735 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003736 else
3737 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003738 }
3739}
3740
3741/*
3742 * "floor({float})" function
3743 */
3744 static void
3745f_floor(typval_T *argvars, typval_T *rettv)
3746{
3747 float_T f = 0.0;
3748
3749 rettv->v_type = VAR_FLOAT;
3750 if (get_float_arg(argvars, &f) == OK)
3751 rettv->vval.v_float = floor(f);
3752 else
3753 rettv->vval.v_float = 0.0;
3754}
3755
3756/*
3757 * "fmod()" function
3758 */
3759 static void
3760f_fmod(typval_T *argvars, typval_T *rettv)
3761{
3762 float_T fx = 0.0, fy = 0.0;
3763
3764 rettv->v_type = VAR_FLOAT;
3765 if (get_float_arg(argvars, &fx) == OK
3766 && get_float_arg(&argvars[1], &fy) == OK)
3767 rettv->vval.v_float = fmod(fx, fy);
3768 else
3769 rettv->vval.v_float = 0.0;
3770}
3771#endif
3772
3773/*
3774 * "fnameescape({string})" function
3775 */
3776 static void
3777f_fnameescape(typval_T *argvars, typval_T *rettv)
3778{
3779 rettv->vval.v_string = vim_strsave_fnameescape(
3780 get_tv_string(&argvars[0]), FALSE);
3781 rettv->v_type = VAR_STRING;
3782}
3783
3784/*
3785 * "fnamemodify({fname}, {mods})" function
3786 */
3787 static void
3788f_fnamemodify(typval_T *argvars, typval_T *rettv)
3789{
3790 char_u *fname;
3791 char_u *mods;
3792 int usedlen = 0;
3793 int len;
3794 char_u *fbuf = NULL;
3795 char_u buf[NUMBUFLEN];
3796
3797 fname = get_tv_string_chk(&argvars[0]);
3798 mods = get_tv_string_buf_chk(&argvars[1], buf);
3799 if (fname == NULL || mods == NULL)
3800 fname = NULL;
3801 else
3802 {
3803 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02003804 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003805 }
3806
3807 rettv->v_type = VAR_STRING;
3808 if (fname == NULL)
3809 rettv->vval.v_string = NULL;
3810 else
3811 rettv->vval.v_string = vim_strnsave(fname, len);
3812 vim_free(fbuf);
3813}
3814
3815static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
3816
3817/*
3818 * "foldclosed()" function
3819 */
3820 static void
3821foldclosed_both(
3822 typval_T *argvars UNUSED,
3823 typval_T *rettv,
3824 int end UNUSED)
3825{
3826#ifdef FEAT_FOLDING
3827 linenr_T lnum;
3828 linenr_T first, last;
3829
3830 lnum = get_tv_lnum(argvars);
3831 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3832 {
3833 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3834 {
3835 if (end)
3836 rettv->vval.v_number = (varnumber_T)last;
3837 else
3838 rettv->vval.v_number = (varnumber_T)first;
3839 return;
3840 }
3841 }
3842#endif
3843 rettv->vval.v_number = -1;
3844}
3845
3846/*
3847 * "foldclosed()" function
3848 */
3849 static void
3850f_foldclosed(typval_T *argvars, typval_T *rettv)
3851{
3852 foldclosed_both(argvars, rettv, FALSE);
3853}
3854
3855/*
3856 * "foldclosedend()" function
3857 */
3858 static void
3859f_foldclosedend(typval_T *argvars, typval_T *rettv)
3860{
3861 foldclosed_both(argvars, rettv, TRUE);
3862}
3863
3864/*
3865 * "foldlevel()" function
3866 */
3867 static void
3868f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3869{
3870#ifdef FEAT_FOLDING
3871 linenr_T lnum;
3872
3873 lnum = get_tv_lnum(argvars);
3874 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3875 rettv->vval.v_number = foldLevel(lnum);
3876#endif
3877}
3878
3879/*
3880 * "foldtext()" function
3881 */
3882 static void
3883f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
3884{
3885#ifdef FEAT_FOLDING
3886 linenr_T foldstart;
3887 linenr_T foldend;
3888 char_u *dashes;
3889 linenr_T lnum;
3890 char_u *s;
3891 char_u *r;
3892 int len;
3893 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003894 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003895#endif
3896
3897 rettv->v_type = VAR_STRING;
3898 rettv->vval.v_string = NULL;
3899#ifdef FEAT_FOLDING
3900 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
3901 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
3902 dashes = get_vim_var_str(VV_FOLDDASHES);
3903 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
3904 && dashes != NULL)
3905 {
3906 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02003907 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003908 if (!linewhite(lnum))
3909 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003910
3911 /* Find interesting text in this line. */
3912 s = skipwhite(ml_get(lnum));
3913 /* skip C comment-start */
3914 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
3915 {
3916 s = skipwhite(s + 2);
3917 if (*skipwhite(s) == NUL
3918 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
3919 {
3920 s = skipwhite(ml_get(lnum + 1));
3921 if (*s == '*')
3922 s = skipwhite(s + 1);
3923 }
3924 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02003925 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003926 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003927 r = alloc((unsigned)(STRLEN(txt)
3928 + STRLEN(dashes) /* for %s */
3929 + 20 /* for %3ld */
3930 + STRLEN(s))); /* concatenated */
3931 if (r != NULL)
3932 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02003933 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003934 len = (int)STRLEN(r);
3935 STRCAT(r, s);
3936 /* remove 'foldmarker' and 'commentstring' */
3937 foldtext_cleanup(r + len);
3938 rettv->vval.v_string = r;
3939 }
3940 }
3941#endif
3942}
3943
3944/*
3945 * "foldtextresult(lnum)" function
3946 */
3947 static void
3948f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
3949{
3950#ifdef FEAT_FOLDING
3951 linenr_T lnum;
3952 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003953 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003954 foldinfo_T foldinfo;
3955 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003956 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003957#endif
3958
3959 rettv->v_type = VAR_STRING;
3960 rettv->vval.v_string = NULL;
3961#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003962 if (entered)
3963 return; /* reject recursive use */
3964 entered = TRUE;
3965
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003966 lnum = get_tv_lnum(argvars);
3967 /* treat illegal types and illegal string values for {lnum} the same */
3968 if (lnum < 0)
3969 lnum = 0;
3970 fold_count = foldedCount(curwin, lnum, &foldinfo);
3971 if (fold_count > 0)
3972 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01003973 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
3974 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003975 if (text == buf)
3976 text = vim_strsave(text);
3977 rettv->vval.v_string = text;
3978 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003979
3980 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003981#endif
3982}
3983
3984/*
3985 * "foreground()" function
3986 */
3987 static void
3988f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3989{
3990#ifdef FEAT_GUI
3991 if (gui.in_use)
3992 gui_mch_set_foreground();
3993#else
3994# ifdef WIN32
3995 win32_set_foreground();
3996# endif
3997#endif
3998}
3999
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004000 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004001common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004002{
4003 char_u *s;
4004 char_u *name;
4005 int use_string = FALSE;
4006 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004007 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004008
4009 if (argvars[0].v_type == VAR_FUNC)
4010 {
4011 /* function(MyFunc, [arg], dict) */
4012 s = argvars[0].vval.v_string;
4013 }
4014 else if (argvars[0].v_type == VAR_PARTIAL
4015 && argvars[0].vval.v_partial != NULL)
4016 {
4017 /* function(dict.MyFunc, [arg]) */
4018 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004019 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004020 }
4021 else
4022 {
4023 /* function('MyFunc', [arg], dict) */
4024 s = get_tv_string(&argvars[0]);
4025 use_string = TRUE;
4026 }
4027
Bram Moolenaar843b8842016-08-21 14:36:15 +02004028 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004029 {
4030 name = s;
4031 trans_name = trans_function_name(&name, FALSE,
4032 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4033 if (*name != NUL)
4034 s = NULL;
4035 }
4036
Bram Moolenaar843b8842016-08-21 14:36:15 +02004037 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4038 || (is_funcref && trans_name == NULL))
Bram Moolenaar5582ef12016-09-14 22:16:13 +02004039 EMSG2(_(e_invarg2), use_string ? get_tv_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004040 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004041 else if (trans_name != NULL && (is_funcref
4042 ? find_func(trans_name) == NULL
4043 : !translated_function_exists(trans_name)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004044 EMSG2(_("E700: Unknown function: %s"), s);
4045 else
4046 {
4047 int dict_idx = 0;
4048 int arg_idx = 0;
4049 list_T *list = NULL;
4050
4051 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4052 {
4053 char sid_buf[25];
4054 int off = *s == 's' ? 2 : 5;
4055
4056 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4057 * also be called from another script. Using trans_function_name()
4058 * would also work, but some plugins depend on the name being
4059 * printable text. */
4060 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
4061 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
4062 if (name != NULL)
4063 {
4064 STRCPY(name, sid_buf);
4065 STRCAT(name, s + off);
4066 }
4067 }
4068 else
4069 name = vim_strsave(s);
4070
4071 if (argvars[1].v_type != VAR_UNKNOWN)
4072 {
4073 if (argvars[2].v_type != VAR_UNKNOWN)
4074 {
4075 /* function(name, [args], dict) */
4076 arg_idx = 1;
4077 dict_idx = 2;
4078 }
4079 else if (argvars[1].v_type == VAR_DICT)
4080 /* function(name, dict) */
4081 dict_idx = 1;
4082 else
4083 /* function(name, [args]) */
4084 arg_idx = 1;
4085 if (dict_idx > 0)
4086 {
4087 if (argvars[dict_idx].v_type != VAR_DICT)
4088 {
4089 EMSG(_("E922: expected a dict"));
4090 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004091 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004092 }
4093 if (argvars[dict_idx].vval.v_dict == NULL)
4094 dict_idx = 0;
4095 }
4096 if (arg_idx > 0)
4097 {
4098 if (argvars[arg_idx].v_type != VAR_LIST)
4099 {
4100 EMSG(_("E923: Second argument of function() must be a list or a dict"));
4101 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004102 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004103 }
4104 list = argvars[arg_idx].vval.v_list;
4105 if (list == NULL || list->lv_len == 0)
4106 arg_idx = 0;
4107 }
4108 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004109 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004110 {
4111 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
4112
4113 /* result is a VAR_PARTIAL */
4114 if (pt == NULL)
4115 vim_free(name);
4116 else
4117 {
4118 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4119 {
4120 listitem_T *li;
4121 int i = 0;
4122 int arg_len = 0;
4123 int lv_len = 0;
4124
4125 if (arg_pt != NULL)
4126 arg_len = arg_pt->pt_argc;
4127 if (list != NULL)
4128 lv_len = list->lv_len;
4129 pt->pt_argc = arg_len + lv_len;
4130 pt->pt_argv = (typval_T *)alloc(
4131 sizeof(typval_T) * pt->pt_argc);
4132 if (pt->pt_argv == NULL)
4133 {
4134 vim_free(pt);
4135 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004136 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004137 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004138 for (i = 0; i < arg_len; i++)
4139 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4140 if (lv_len > 0)
4141 for (li = list->lv_first; li != NULL;
4142 li = li->li_next)
4143 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004144 }
4145
4146 /* For "function(dict.func, [], dict)" and "func" is a partial
4147 * use "dict". That is backwards compatible. */
4148 if (dict_idx > 0)
4149 {
4150 /* The dict is bound explicitly, pt_auto is FALSE. */
4151 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4152 ++pt->pt_dict->dv_refcount;
4153 }
4154 else if (arg_pt != NULL)
4155 {
4156 /* If the dict was bound automatically the result is also
4157 * bound automatically. */
4158 pt->pt_dict = arg_pt->pt_dict;
4159 pt->pt_auto = arg_pt->pt_auto;
4160 if (pt->pt_dict != NULL)
4161 ++pt->pt_dict->dv_refcount;
4162 }
4163
4164 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004165 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4166 {
4167 pt->pt_func = arg_pt->pt_func;
4168 func_ptr_ref(pt->pt_func);
4169 vim_free(name);
4170 }
4171 else if (is_funcref)
4172 {
4173 pt->pt_func = find_func(trans_name);
4174 func_ptr_ref(pt->pt_func);
4175 vim_free(name);
4176 }
4177 else
4178 {
4179 pt->pt_name = name;
4180 func_ref(name);
4181 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004182 }
4183 rettv->v_type = VAR_PARTIAL;
4184 rettv->vval.v_partial = pt;
4185 }
4186 else
4187 {
4188 /* result is a VAR_FUNC */
4189 rettv->v_type = VAR_FUNC;
4190 rettv->vval.v_string = name;
4191 func_ref(name);
4192 }
4193 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004194theend:
4195 vim_free(trans_name);
4196}
4197
4198/*
4199 * "funcref()" function
4200 */
4201 static void
4202f_funcref(typval_T *argvars, typval_T *rettv)
4203{
4204 common_function(argvars, rettv, TRUE);
4205}
4206
4207/*
4208 * "function()" function
4209 */
4210 static void
4211f_function(typval_T *argvars, typval_T *rettv)
4212{
4213 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004214}
4215
4216/*
4217 * "garbagecollect()" function
4218 */
4219 static void
4220f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4221{
4222 /* This is postponed until we are back at the toplevel, because we may be
4223 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4224 want_garbage_collect = TRUE;
4225
4226 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
4227 garbage_collect_at_exit = TRUE;
4228}
4229
4230/*
4231 * "get()" function
4232 */
4233 static void
4234f_get(typval_T *argvars, typval_T *rettv)
4235{
4236 listitem_T *li;
4237 list_T *l;
4238 dictitem_T *di;
4239 dict_T *d;
4240 typval_T *tv = NULL;
4241
4242 if (argvars[0].v_type == VAR_LIST)
4243 {
4244 if ((l = argvars[0].vval.v_list) != NULL)
4245 {
4246 int error = FALSE;
4247
4248 li = list_find(l, (long)get_tv_number_chk(&argvars[1], &error));
4249 if (!error && li != NULL)
4250 tv = &li->li_tv;
4251 }
4252 }
4253 else if (argvars[0].v_type == VAR_DICT)
4254 {
4255 if ((d = argvars[0].vval.v_dict) != NULL)
4256 {
4257 di = dict_find(d, get_tv_string(&argvars[1]), -1);
4258 if (di != NULL)
4259 tv = &di->di_tv;
4260 }
4261 }
4262 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4263 {
4264 partial_T *pt;
4265 partial_T fref_pt;
4266
4267 if (argvars[0].v_type == VAR_PARTIAL)
4268 pt = argvars[0].vval.v_partial;
4269 else
4270 {
4271 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4272 fref_pt.pt_name = argvars[0].vval.v_string;
4273 pt = &fref_pt;
4274 }
4275
4276 if (pt != NULL)
4277 {
4278 char_u *what = get_tv_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004279 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004280
4281 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4282 {
4283 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004284 n = partial_name(pt);
4285 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004286 rettv->vval.v_string = NULL;
4287 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004288 {
4289 rettv->vval.v_string = vim_strsave(n);
4290 if (rettv->v_type == VAR_FUNC)
4291 func_ref(rettv->vval.v_string);
4292 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004293 }
4294 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004295 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004296 else if (STRCMP(what, "args") == 0)
4297 {
4298 rettv->v_type = VAR_LIST;
4299 if (rettv_list_alloc(rettv) == OK)
4300 {
4301 int i;
4302
4303 for (i = 0; i < pt->pt_argc; ++i)
4304 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4305 }
4306 }
4307 else
4308 EMSG2(_(e_invarg2), what);
4309 return;
4310 }
4311 }
4312 else
4313 EMSG2(_(e_listdictarg), "get()");
4314
4315 if (tv == NULL)
4316 {
4317 if (argvars[2].v_type != VAR_UNKNOWN)
4318 copy_tv(&argvars[2], rettv);
4319 }
4320 else
4321 copy_tv(tv, rettv);
4322}
4323
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004324#ifdef FEAT_SIGNS
4325/*
4326 * Returns information about signs placed in a buffer as list of dicts.
4327 */
4328 static void
4329get_buffer_signs(buf_T *buf, list_T *l)
4330{
4331 signlist_T *sign;
4332
4333 for (sign = buf->b_signlist; sign; sign = sign->next)
4334 {
4335 dict_T *d = dict_alloc();
4336
4337 if (d != NULL)
4338 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02004339 dict_add_number(d, "id", sign->id);
4340 dict_add_number(d, "lnum", sign->lnum);
4341 dict_add_string(d, "name", sign_typenr2name(sign->typenr));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004342
4343 list_append_dict(l, d);
4344 }
4345 }
4346}
4347#endif
4348
4349/*
4350 * Returns buffer options, variables and other attributes in a dictionary.
4351 */
4352 static dict_T *
4353get_buffer_info(buf_T *buf)
4354{
4355 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004356 tabpage_T *tp;
4357 win_T *wp;
4358 list_T *windows;
4359
4360 dict = dict_alloc();
4361 if (dict == NULL)
4362 return NULL;
4363
Bram Moolenaare0be1672018-07-08 16:50:37 +02004364 dict_add_number(dict, "bufnr", buf->b_fnum);
4365 dict_add_string(dict, "name", buf->b_ffname);
4366 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4367 : buflist_findlnum(buf));
4368 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4369 dict_add_number(dict, "listed", buf->b_p_bl);
4370 dict_add_number(dict, "changed", bufIsChanged(buf));
4371 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4372 dict_add_number(dict, "hidden",
4373 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004374
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004375 /* Get a reference to buffer variables */
4376 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004377
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004378 /* List of windows displaying this buffer */
4379 windows = list_alloc();
4380 if (windows != NULL)
4381 {
4382 FOR_ALL_TAB_WINDOWS(tp, wp)
4383 if (wp->w_buffer == buf)
4384 list_append_number(windows, (varnumber_T)wp->w_id);
4385 dict_add_list(dict, "windows", windows);
4386 }
4387
4388#ifdef FEAT_SIGNS
4389 if (buf->b_signlist != NULL)
4390 {
4391 /* List of signs placed in this buffer */
4392 list_T *signs = list_alloc();
4393 if (signs != NULL)
4394 {
4395 get_buffer_signs(buf, signs);
4396 dict_add_list(dict, "signs", signs);
4397 }
4398 }
4399#endif
4400
4401 return dict;
4402}
4403
4404/*
4405 * "getbufinfo()" function
4406 */
4407 static void
4408f_getbufinfo(typval_T *argvars, typval_T *rettv)
4409{
4410 buf_T *buf = NULL;
4411 buf_T *argbuf = NULL;
4412 dict_T *d;
4413 int filtered = FALSE;
4414 int sel_buflisted = FALSE;
4415 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004416 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004417
4418 if (rettv_list_alloc(rettv) != OK)
4419 return;
4420
4421 /* List of all the buffers or selected buffers */
4422 if (argvars[0].v_type == VAR_DICT)
4423 {
4424 dict_T *sel_d = argvars[0].vval.v_dict;
4425
4426 if (sel_d != NULL)
4427 {
4428 dictitem_T *di;
4429
4430 filtered = TRUE;
4431
4432 di = dict_find(sel_d, (char_u *)"buflisted", -1);
4433 if (di != NULL && get_tv_number(&di->di_tv))
4434 sel_buflisted = TRUE;
4435
4436 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
4437 if (di != NULL && get_tv_number(&di->di_tv))
4438 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004439
4440 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
4441 if (di != NULL && get_tv_number(&di->di_tv))
4442 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004443 }
4444 }
4445 else if (argvars[0].v_type != VAR_UNKNOWN)
4446 {
4447 /* Information about one buffer. Argument specifies the buffer */
4448 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4449 ++emsg_off;
4450 argbuf = get_buf_tv(&argvars[0], FALSE);
4451 --emsg_off;
4452 if (argbuf == NULL)
4453 return;
4454 }
4455
4456 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004457 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004458 {
4459 if (argbuf != NULL && argbuf != buf)
4460 continue;
4461 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004462 || (sel_buflisted && !buf->b_p_bl)
4463 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004464 continue;
4465
4466 d = get_buffer_info(buf);
4467 if (d != NULL)
4468 list_append_dict(rettv->vval.v_list, d);
4469 if (argbuf != NULL)
4470 return;
4471 }
4472}
4473
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004474static void get_buffer_lines(buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv);
4475
4476/*
4477 * Get line or list of lines from buffer "buf" into "rettv".
4478 * Return a range (from start to end) of lines in rettv from the specified
4479 * buffer.
4480 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4481 */
4482 static void
4483get_buffer_lines(
4484 buf_T *buf,
4485 linenr_T start,
4486 linenr_T end,
4487 int retlist,
4488 typval_T *rettv)
4489{
4490 char_u *p;
4491
4492 rettv->v_type = VAR_STRING;
4493 rettv->vval.v_string = NULL;
4494 if (retlist && rettv_list_alloc(rettv) == FAIL)
4495 return;
4496
4497 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4498 return;
4499
4500 if (!retlist)
4501 {
4502 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4503 p = ml_get_buf(buf, start, FALSE);
4504 else
4505 p = (char_u *)"";
4506 rettv->vval.v_string = vim_strsave(p);
4507 }
4508 else
4509 {
4510 if (end < start)
4511 return;
4512
4513 if (start < 1)
4514 start = 1;
4515 if (end > buf->b_ml.ml_line_count)
4516 end = buf->b_ml.ml_line_count;
4517 while (start <= end)
4518 if (list_append_string(rettv->vval.v_list,
4519 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4520 break;
4521 }
4522}
4523
4524/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004525 * "getbufline()" function
4526 */
4527 static void
4528f_getbufline(typval_T *argvars, typval_T *rettv)
4529{
4530 linenr_T lnum;
4531 linenr_T end;
4532 buf_T *buf;
4533
4534 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4535 ++emsg_off;
4536 buf = get_buf_tv(&argvars[0], FALSE);
4537 --emsg_off;
4538
4539 lnum = get_tv_lnum_buf(&argvars[1], buf);
4540 if (argvars[2].v_type == VAR_UNKNOWN)
4541 end = lnum;
4542 else
4543 end = get_tv_lnum_buf(&argvars[2], buf);
4544
4545 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4546}
4547
4548/*
4549 * "getbufvar()" function
4550 */
4551 static void
4552f_getbufvar(typval_T *argvars, typval_T *rettv)
4553{
4554 buf_T *buf;
4555 buf_T *save_curbuf;
4556 char_u *varname;
4557 dictitem_T *v;
4558 int done = FALSE;
4559
4560 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4561 varname = get_tv_string_chk(&argvars[1]);
4562 ++emsg_off;
4563 buf = get_buf_tv(&argvars[0], FALSE);
4564
4565 rettv->v_type = VAR_STRING;
4566 rettv->vval.v_string = NULL;
4567
4568 if (buf != NULL && varname != NULL)
4569 {
4570 /* set curbuf to be our buf, temporarily */
4571 save_curbuf = curbuf;
4572 curbuf = buf;
4573
Bram Moolenaar30567352016-08-27 21:25:44 +02004574 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004575 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004576 if (varname[1] == NUL)
4577 {
4578 /* get all buffer-local options in a dict */
4579 dict_T *opts = get_winbuf_options(TRUE);
4580
4581 if (opts != NULL)
4582 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004583 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004584 done = TRUE;
4585 }
4586 }
4587 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4588 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004589 done = TRUE;
4590 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004591 else
4592 {
4593 /* Look up the variable. */
4594 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4595 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4596 'b', varname, FALSE);
4597 if (v != NULL)
4598 {
4599 copy_tv(&v->di_tv, rettv);
4600 done = TRUE;
4601 }
4602 }
4603
4604 /* restore previous notion of curbuf */
4605 curbuf = save_curbuf;
4606 }
4607
4608 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4609 /* use the default value */
4610 copy_tv(&argvars[2], rettv);
4611
4612 --emsg_off;
4613}
4614
4615/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004616 * "getchangelist()" function
4617 */
4618 static void
4619f_getchangelist(typval_T *argvars, typval_T *rettv)
4620{
4621#ifdef FEAT_JUMPLIST
4622 buf_T *buf;
4623 int i;
4624 list_T *l;
4625 dict_T *d;
4626#endif
4627
4628 if (rettv_list_alloc(rettv) != OK)
4629 return;
4630
4631#ifdef FEAT_JUMPLIST
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004632 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4633 ++emsg_off;
4634 buf = get_buf_tv(&argvars[0], FALSE);
4635 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004636 if (buf == NULL)
4637 return;
4638
4639 l = list_alloc();
4640 if (l == NULL)
4641 return;
4642
4643 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4644 return;
4645 /*
4646 * The current window change list index tracks only the position in the
4647 * current buffer change list. For other buffers, use the change list
4648 * length as the current index.
4649 */
4650 list_append_number(rettv->vval.v_list,
4651 (varnumber_T)((buf == curwin->w_buffer)
4652 ? curwin->w_changelistidx : buf->b_changelistlen));
4653
4654 for (i = 0; i < buf->b_changelistlen; ++i)
4655 {
4656 if (buf->b_changelist[i].lnum == 0)
4657 continue;
4658 if ((d = dict_alloc()) == NULL)
4659 return;
4660 if (list_append_dict(l, d) == FAIL)
4661 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004662 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4663 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004664# ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +02004665 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004666# endif
4667 }
4668#endif
4669}
4670/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004671 * "getchar()" function
4672 */
4673 static void
4674f_getchar(typval_T *argvars, typval_T *rettv)
4675{
4676 varnumber_T n;
4677 int error = FALSE;
4678
4679 /* Position the cursor. Needed after a message that ends in a space. */
4680 windgoto(msg_row, msg_col);
4681
4682 ++no_mapping;
4683 ++allow_keys;
4684 for (;;)
4685 {
4686 if (argvars[0].v_type == VAR_UNKNOWN)
4687 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004688 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004689 else if (get_tv_number_chk(&argvars[0], &error) == 1)
4690 /* getchar(1): only check if char avail */
4691 n = vpeekc_any();
4692 else if (error || vpeekc_any() == NUL)
4693 /* illegal argument or getchar(0) and no char avail: return zero */
4694 n = 0;
4695 else
4696 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004697 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004698
4699 if (n == K_IGNORE)
4700 continue;
4701 break;
4702 }
4703 --no_mapping;
4704 --allow_keys;
4705
4706 set_vim_var_nr(VV_MOUSE_WIN, 0);
4707 set_vim_var_nr(VV_MOUSE_WINID, 0);
4708 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4709 set_vim_var_nr(VV_MOUSE_COL, 0);
4710
4711 rettv->vval.v_number = n;
4712 if (IS_SPECIAL(n) || mod_mask != 0)
4713 {
4714 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4715 int i = 0;
4716
4717 /* Turn a special key into three bytes, plus modifier. */
4718 if (mod_mask != 0)
4719 {
4720 temp[i++] = K_SPECIAL;
4721 temp[i++] = KS_MODIFIER;
4722 temp[i++] = mod_mask;
4723 }
4724 if (IS_SPECIAL(n))
4725 {
4726 temp[i++] = K_SPECIAL;
4727 temp[i++] = K_SECOND(n);
4728 temp[i++] = K_THIRD(n);
4729 }
4730#ifdef FEAT_MBYTE
4731 else if (has_mbyte)
4732 i += (*mb_char2bytes)(n, temp + i);
4733#endif
4734 else
4735 temp[i++] = n;
4736 temp[i++] = NUL;
4737 rettv->v_type = VAR_STRING;
4738 rettv->vval.v_string = vim_strsave(temp);
4739
4740#ifdef FEAT_MOUSE
4741 if (is_mouse_key(n))
4742 {
4743 int row = mouse_row;
4744 int col = mouse_col;
4745 win_T *win;
4746 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004747 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004748 int winnr = 1;
4749
4750 if (row >= 0 && col >= 0)
4751 {
4752 /* Find the window at the mouse coordinates and compute the
4753 * text position. */
4754 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004755 if (win == NULL)
4756 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004757 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004758 for (wp = firstwin; wp != win; wp = wp->w_next)
4759 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004760 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4761 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4762 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4763 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4764 }
4765 }
4766#endif
4767 }
4768}
4769
4770/*
4771 * "getcharmod()" function
4772 */
4773 static void
4774f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4775{
4776 rettv->vval.v_number = mod_mask;
4777}
4778
4779/*
4780 * "getcharsearch()" function
4781 */
4782 static void
4783f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4784{
4785 if (rettv_dict_alloc(rettv) != FAIL)
4786 {
4787 dict_T *dict = rettv->vval.v_dict;
4788
Bram Moolenaare0be1672018-07-08 16:50:37 +02004789 dict_add_string(dict, "char", last_csearch());
4790 dict_add_number(dict, "forward", last_csearch_forward());
4791 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004792 }
4793}
4794
4795/*
4796 * "getcmdline()" function
4797 */
4798 static void
4799f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4800{
4801 rettv->v_type = VAR_STRING;
4802 rettv->vval.v_string = get_cmdline_str();
4803}
4804
4805/*
4806 * "getcmdpos()" function
4807 */
4808 static void
4809f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4810{
4811 rettv->vval.v_number = get_cmdline_pos() + 1;
4812}
4813
4814/*
4815 * "getcmdtype()" function
4816 */
4817 static void
4818f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4819{
4820 rettv->v_type = VAR_STRING;
4821 rettv->vval.v_string = alloc(2);
4822 if (rettv->vval.v_string != NULL)
4823 {
4824 rettv->vval.v_string[0] = get_cmdline_type();
4825 rettv->vval.v_string[1] = NUL;
4826 }
4827}
4828
4829/*
4830 * "getcmdwintype()" function
4831 */
4832 static void
4833f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4834{
4835 rettv->v_type = VAR_STRING;
4836 rettv->vval.v_string = NULL;
4837#ifdef FEAT_CMDWIN
4838 rettv->vval.v_string = alloc(2);
4839 if (rettv->vval.v_string != NULL)
4840 {
4841 rettv->vval.v_string[0] = cmdwin_type;
4842 rettv->vval.v_string[1] = NUL;
4843 }
4844#endif
4845}
4846
4847#if defined(FEAT_CMDL_COMPL)
4848/*
4849 * "getcompletion()" function
4850 */
4851 static void
4852f_getcompletion(typval_T *argvars, typval_T *rettv)
4853{
4854 char_u *pat;
4855 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004856 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004857 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
4858 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004859
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004860 if (argvars[2].v_type != VAR_UNKNOWN)
4861 filtered = get_tv_number_chk(&argvars[2], NULL);
4862
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004863 if (p_wic)
4864 options |= WILD_ICASE;
4865
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004866 /* For filtered results, 'wildignore' is used */
4867 if (!filtered)
4868 options |= WILD_KEEP_ALL;
4869
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004870 ExpandInit(&xpc);
4871 xpc.xp_pattern = get_tv_string(&argvars[0]);
4872 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4873 xpc.xp_context = cmdcomplete_str_to_type(get_tv_string(&argvars[1]));
4874 if (xpc.xp_context == EXPAND_NOTHING)
4875 {
4876 if (argvars[1].v_type == VAR_STRING)
4877 EMSG2(_(e_invarg2), argvars[1].vval.v_string);
4878 else
4879 EMSG(_(e_invarg));
4880 return;
4881 }
4882
4883# if defined(FEAT_MENU)
4884 if (xpc.xp_context == EXPAND_MENUS)
4885 {
4886 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4887 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4888 }
4889# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02004890#ifdef FEAT_CSCOPE
4891 if (xpc.xp_context == EXPAND_CSCOPE)
4892 {
4893 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4894 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4895 }
4896#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02004897#ifdef FEAT_SIGNS
4898 if (xpc.xp_context == EXPAND_SIGN)
4899 {
4900 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4901 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4902 }
4903#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004904
4905 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4906 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
4907 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004908 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004909
4910 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4911
4912 for (i = 0; i < xpc.xp_numfiles; i++)
4913 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4914 }
4915 vim_free(pat);
4916 ExpandCleanup(&xpc);
4917}
4918#endif
4919
4920/*
4921 * "getcwd()" function
4922 */
4923 static void
4924f_getcwd(typval_T *argvars, typval_T *rettv)
4925{
4926 win_T *wp = NULL;
4927 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01004928 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004929
4930 rettv->v_type = VAR_STRING;
4931 rettv->vval.v_string = NULL;
4932
Bram Moolenaar54591292018-02-09 20:53:59 +01004933 if (argvars[0].v_type == VAR_NUMBER && argvars[0].vval.v_number == -1)
4934 global = TRUE;
4935 else
4936 wp = find_tabwin(&argvars[0], &argvars[1]);
4937
4938 if (wp != NULL && wp->w_localdir != NULL)
4939 rettv->vval.v_string = vim_strsave(wp->w_localdir);
4940 else if (wp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004941 {
Bram Moolenaar54591292018-02-09 20:53:59 +01004942 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004943 rettv->vval.v_string = vim_strsave(globaldir);
4944 else
4945 {
4946 cwd = alloc(MAXPATHL);
4947 if (cwd != NULL)
4948 {
4949 if (mch_dirname(cwd, MAXPATHL) != FAIL)
4950 rettv->vval.v_string = vim_strsave(cwd);
4951 vim_free(cwd);
4952 }
4953 }
4954#ifdef BACKSLASH_IN_FILENAME
4955 if (rettv->vval.v_string != NULL)
4956 slash_adjust(rettv->vval.v_string);
4957#endif
4958 }
4959}
4960
4961/*
4962 * "getfontname()" function
4963 */
4964 static void
4965f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
4966{
4967 rettv->v_type = VAR_STRING;
4968 rettv->vval.v_string = NULL;
4969#ifdef FEAT_GUI
4970 if (gui.in_use)
4971 {
4972 GuiFont font;
4973 char_u *name = NULL;
4974
4975 if (argvars[0].v_type == VAR_UNKNOWN)
4976 {
4977 /* Get the "Normal" font. Either the name saved by
4978 * hl_set_font_name() or from the font ID. */
4979 font = gui.norm_font;
4980 name = hl_get_font_name();
4981 }
4982 else
4983 {
4984 name = get_tv_string(&argvars[0]);
4985 if (STRCMP(name, "*") == 0) /* don't use font dialog */
4986 return;
4987 font = gui_mch_get_font(name, FALSE);
4988 if (font == NOFONT)
4989 return; /* Invalid font name, return empty string. */
4990 }
4991 rettv->vval.v_string = gui_mch_get_fontname(font, name);
4992 if (argvars[0].v_type != VAR_UNKNOWN)
4993 gui_mch_free_font(font);
4994 }
4995#endif
4996}
4997
4998/*
4999 * "getfperm({fname})" function
5000 */
5001 static void
5002f_getfperm(typval_T *argvars, typval_T *rettv)
5003{
5004 char_u *fname;
5005 stat_T st;
5006 char_u *perm = NULL;
5007 char_u flags[] = "rwx";
5008 int i;
5009
5010 fname = get_tv_string(&argvars[0]);
5011
5012 rettv->v_type = VAR_STRING;
5013 if (mch_stat((char *)fname, &st) >= 0)
5014 {
5015 perm = vim_strsave((char_u *)"---------");
5016 if (perm != NULL)
5017 {
5018 for (i = 0; i < 9; i++)
5019 {
5020 if (st.st_mode & (1 << (8 - i)))
5021 perm[i] = flags[i % 3];
5022 }
5023 }
5024 }
5025 rettv->vval.v_string = perm;
5026}
5027
5028/*
5029 * "getfsize({fname})" function
5030 */
5031 static void
5032f_getfsize(typval_T *argvars, typval_T *rettv)
5033{
5034 char_u *fname;
5035 stat_T st;
5036
5037 fname = get_tv_string(&argvars[0]);
5038
5039 rettv->v_type = VAR_NUMBER;
5040
5041 if (mch_stat((char *)fname, &st) >= 0)
5042 {
5043 if (mch_isdir(fname))
5044 rettv->vval.v_number = 0;
5045 else
5046 {
5047 rettv->vval.v_number = (varnumber_T)st.st_size;
5048
5049 /* non-perfect check for overflow */
5050 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5051 rettv->vval.v_number = -2;
5052 }
5053 }
5054 else
5055 rettv->vval.v_number = -1;
5056}
5057
5058/*
5059 * "getftime({fname})" function
5060 */
5061 static void
5062f_getftime(typval_T *argvars, typval_T *rettv)
5063{
5064 char_u *fname;
5065 stat_T st;
5066
5067 fname = get_tv_string(&argvars[0]);
5068
5069 if (mch_stat((char *)fname, &st) >= 0)
5070 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5071 else
5072 rettv->vval.v_number = -1;
5073}
5074
5075/*
5076 * "getftype({fname})" function
5077 */
5078 static void
5079f_getftype(typval_T *argvars, typval_T *rettv)
5080{
5081 char_u *fname;
5082 stat_T st;
5083 char_u *type = NULL;
5084 char *t;
5085
5086 fname = get_tv_string(&argvars[0]);
5087
5088 rettv->v_type = VAR_STRING;
5089 if (mch_lstat((char *)fname, &st) >= 0)
5090 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005091 if (S_ISREG(st.st_mode))
5092 t = "file";
5093 else if (S_ISDIR(st.st_mode))
5094 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005095 else if (S_ISLNK(st.st_mode))
5096 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005097 else if (S_ISBLK(st.st_mode))
5098 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005099 else if (S_ISCHR(st.st_mode))
5100 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005101 else if (S_ISFIFO(st.st_mode))
5102 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005103 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005104 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005105 else
5106 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005107 type = vim_strsave((char_u *)t);
5108 }
5109 rettv->vval.v_string = type;
5110}
5111
5112/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005113 * "getjumplist()" function
5114 */
5115 static void
5116f_getjumplist(typval_T *argvars, typval_T *rettv)
5117{
5118#ifdef FEAT_JUMPLIST
5119 win_T *wp;
5120 int i;
5121 list_T *l;
5122 dict_T *d;
5123#endif
5124
5125 if (rettv_list_alloc(rettv) != OK)
5126 return;
5127
5128#ifdef FEAT_JUMPLIST
5129 wp = find_tabwin(&argvars[0], &argvars[1]);
5130 if (wp == NULL)
5131 return;
5132
5133 l = list_alloc();
5134 if (l == NULL)
5135 return;
5136
5137 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5138 return;
5139 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5140
Bram Moolenaar48679742018-02-13 13:33:29 +01005141 cleanup_jumplist(wp, TRUE);
5142
Bram Moolenaar4f505882018-02-10 21:06:32 +01005143 for (i = 0; i < wp->w_jumplistlen; ++i)
5144 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005145 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5146 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005147 if ((d = dict_alloc()) == NULL)
5148 return;
5149 if (list_append_dict(l, d) == FAIL)
5150 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005151 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5152 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005153# ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +02005154 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005155# endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005156 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005157 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005158 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005159 }
5160#endif
5161}
5162
5163/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005164 * "getline(lnum, [end])" function
5165 */
5166 static void
5167f_getline(typval_T *argvars, typval_T *rettv)
5168{
5169 linenr_T lnum;
5170 linenr_T end;
5171 int retlist;
5172
5173 lnum = get_tv_lnum(argvars);
5174 if (argvars[1].v_type == VAR_UNKNOWN)
5175 {
5176 end = 0;
5177 retlist = FALSE;
5178 }
5179 else
5180 {
5181 end = get_tv_lnum(&argvars[1]);
5182 retlist = TRUE;
5183 }
5184
5185 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5186}
5187
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005188#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005189 static void
5190get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5191{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005192 if (what_arg->v_type == VAR_UNKNOWN)
5193 {
5194 if (rettv_list_alloc(rettv) == OK)
5195 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005196 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005197 }
5198 else
5199 {
5200 if (rettv_dict_alloc(rettv) == OK)
5201 if (is_qf || (wp != NULL))
5202 {
5203 if (what_arg->v_type == VAR_DICT)
5204 {
5205 dict_T *d = what_arg->vval.v_dict;
5206
5207 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005208 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005209 }
5210 else
5211 EMSG(_(e_dictreq));
5212 }
5213 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005214}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005215#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005216
5217/*
5218 * "getloclist()" function
5219 */
5220 static void
5221f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5222{
5223#ifdef FEAT_QUICKFIX
5224 win_T *wp;
5225
5226 wp = find_win_by_nr(&argvars[0], NULL);
5227 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5228#endif
5229}
5230
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005231/*
5232 * "getmatches()" function
5233 */
5234 static void
5235f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5236{
5237#ifdef FEAT_SEARCH_EXTRA
5238 dict_T *dict;
5239 matchitem_T *cur = curwin->w_match_head;
5240 int i;
5241
5242 if (rettv_list_alloc(rettv) == OK)
5243 {
5244 while (cur != NULL)
5245 {
5246 dict = dict_alloc();
5247 if (dict == NULL)
5248 return;
5249 if (cur->match.regprog == NULL)
5250 {
5251 /* match added with matchaddpos() */
5252 for (i = 0; i < MAXPOSMATCH; ++i)
5253 {
5254 llpos_T *llpos;
5255 char buf[6];
5256 list_T *l;
5257
5258 llpos = &cur->pos.pos[i];
5259 if (llpos->lnum == 0)
5260 break;
5261 l = list_alloc();
5262 if (l == NULL)
5263 break;
5264 list_append_number(l, (varnumber_T)llpos->lnum);
5265 if (llpos->col > 0)
5266 {
5267 list_append_number(l, (varnumber_T)llpos->col);
5268 list_append_number(l, (varnumber_T)llpos->len);
5269 }
5270 sprintf(buf, "pos%d", i + 1);
5271 dict_add_list(dict, buf, l);
5272 }
5273 }
5274 else
5275 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02005276 dict_add_string(dict, "pattern", cur->pattern);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005277 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02005278 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
5279 dict_add_number(dict, "priority", (long)cur->priority);
5280 dict_add_number(dict, "id", (long)cur->id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005281# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
5282 if (cur->conceal_char)
5283 {
5284 char_u buf[MB_MAXBYTES + 1];
5285
5286 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005287 dict_add_string(dict, "conceal", (char_u *)&buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005288 }
5289# endif
5290 list_append_dict(rettv->vval.v_list, dict);
5291 cur = cur->next;
5292 }
5293 }
5294#endif
5295}
5296
5297/*
5298 * "getpid()" function
5299 */
5300 static void
5301f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5302{
5303 rettv->vval.v_number = mch_get_pid();
5304}
5305
5306 static void
5307getpos_both(
5308 typval_T *argvars,
5309 typval_T *rettv,
5310 int getcurpos)
5311{
5312 pos_T *fp;
5313 list_T *l;
5314 int fnum = -1;
5315
5316 if (rettv_list_alloc(rettv) == OK)
5317 {
5318 l = rettv->vval.v_list;
5319 if (getcurpos)
5320 fp = &curwin->w_cursor;
5321 else
5322 fp = var2fpos(&argvars[0], TRUE, &fnum);
5323 if (fnum != -1)
5324 list_append_number(l, (varnumber_T)fnum);
5325 else
5326 list_append_number(l, (varnumber_T)0);
5327 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5328 : (varnumber_T)0);
5329 list_append_number(l, (fp != NULL)
5330 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5331 : (varnumber_T)0);
5332 list_append_number(l,
5333#ifdef FEAT_VIRTUALEDIT
5334 (fp != NULL) ? (varnumber_T)fp->coladd :
5335#endif
5336 (varnumber_T)0);
5337 if (getcurpos)
5338 {
5339 update_curswant();
5340 list_append_number(l, curwin->w_curswant == MAXCOL ?
5341 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
5342 }
5343 }
5344 else
5345 rettv->vval.v_number = FALSE;
5346}
5347
5348
5349/*
5350 * "getcurpos()" function
5351 */
5352 static void
5353f_getcurpos(typval_T *argvars, typval_T *rettv)
5354{
5355 getpos_both(argvars, rettv, TRUE);
5356}
5357
5358/*
5359 * "getpos(string)" function
5360 */
5361 static void
5362f_getpos(typval_T *argvars, typval_T *rettv)
5363{
5364 getpos_both(argvars, rettv, FALSE);
5365}
5366
5367/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005368 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005369 */
5370 static void
5371f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5372{
5373#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005374 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005375#endif
5376}
5377
5378/*
5379 * "getreg()" function
5380 */
5381 static void
5382f_getreg(typval_T *argvars, typval_T *rettv)
5383{
5384 char_u *strregname;
5385 int regname;
5386 int arg2 = FALSE;
5387 int return_list = FALSE;
5388 int error = FALSE;
5389
5390 if (argvars[0].v_type != VAR_UNKNOWN)
5391 {
5392 strregname = get_tv_string_chk(&argvars[0]);
5393 error = strregname == NULL;
5394 if (argvars[1].v_type != VAR_UNKNOWN)
5395 {
5396 arg2 = (int)get_tv_number_chk(&argvars[1], &error);
5397 if (!error && argvars[2].v_type != VAR_UNKNOWN)
5398 return_list = (int)get_tv_number_chk(&argvars[2], &error);
5399 }
5400 }
5401 else
5402 strregname = get_vim_var_str(VV_REG);
5403
5404 if (error)
5405 return;
5406
5407 regname = (strregname == NULL ? '"' : *strregname);
5408 if (regname == 0)
5409 regname = '"';
5410
5411 if (return_list)
5412 {
5413 rettv->v_type = VAR_LIST;
5414 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5415 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5416 if (rettv->vval.v_list == NULL)
5417 (void)rettv_list_alloc(rettv);
5418 else
5419 ++rettv->vval.v_list->lv_refcount;
5420 }
5421 else
5422 {
5423 rettv->v_type = VAR_STRING;
5424 rettv->vval.v_string = get_reg_contents(regname,
5425 arg2 ? GREG_EXPR_SRC : 0);
5426 }
5427}
5428
5429/*
5430 * "getregtype()" function
5431 */
5432 static void
5433f_getregtype(typval_T *argvars, typval_T *rettv)
5434{
5435 char_u *strregname;
5436 int regname;
5437 char_u buf[NUMBUFLEN + 2];
5438 long reglen = 0;
5439
5440 if (argvars[0].v_type != VAR_UNKNOWN)
5441 {
5442 strregname = get_tv_string_chk(&argvars[0]);
5443 if (strregname == NULL) /* type error; errmsg already given */
5444 {
5445 rettv->v_type = VAR_STRING;
5446 rettv->vval.v_string = NULL;
5447 return;
5448 }
5449 }
5450 else
5451 /* Default to v:register */
5452 strregname = get_vim_var_str(VV_REG);
5453
5454 regname = (strregname == NULL ? '"' : *strregname);
5455 if (regname == 0)
5456 regname = '"';
5457
5458 buf[0] = NUL;
5459 buf[1] = NUL;
5460 switch (get_reg_type(regname, &reglen))
5461 {
5462 case MLINE: buf[0] = 'V'; break;
5463 case MCHAR: buf[0] = 'v'; break;
5464 case MBLOCK:
5465 buf[0] = Ctrl_V;
5466 sprintf((char *)buf + 1, "%ld", reglen + 1);
5467 break;
5468 }
5469 rettv->v_type = VAR_STRING;
5470 rettv->vval.v_string = vim_strsave(buf);
5471}
5472
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005473/*
5474 * Returns information (variables, options, etc.) about a tab page
5475 * as a dictionary.
5476 */
5477 static dict_T *
5478get_tabpage_info(tabpage_T *tp, int tp_idx)
5479{
5480 win_T *wp;
5481 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005482 list_T *l;
5483
5484 dict = dict_alloc();
5485 if (dict == NULL)
5486 return NULL;
5487
Bram Moolenaare0be1672018-07-08 16:50:37 +02005488 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005489
5490 l = list_alloc();
5491 if (l != NULL)
5492 {
5493 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5494 wp; wp = wp->w_next)
5495 list_append_number(l, (varnumber_T)wp->w_id);
5496 dict_add_list(dict, "windows", l);
5497 }
5498
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005499 /* Make a reference to tabpage variables */
5500 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005501
5502 return dict;
5503}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005504
5505/*
5506 * "gettabinfo()" function
5507 */
5508 static void
5509f_gettabinfo(typval_T *argvars, typval_T *rettv)
5510{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005511 tabpage_T *tp, *tparg = NULL;
5512 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005513 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005514
5515 if (rettv_list_alloc(rettv) != OK)
5516 return;
5517
5518 if (argvars[0].v_type != VAR_UNKNOWN)
5519 {
5520 /* Information about one tab page */
5521 tparg = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5522 if (tparg == NULL)
5523 return;
5524 }
5525
5526 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005527 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005528 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005529 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005530 if (tparg != NULL && tp != tparg)
5531 continue;
5532 d = get_tabpage_info(tp, tpnr);
5533 if (d != NULL)
5534 list_append_dict(rettv->vval.v_list, d);
5535 if (tparg != NULL)
5536 return;
5537 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005538}
5539
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005540/*
5541 * "gettabvar()" function
5542 */
5543 static void
5544f_gettabvar(typval_T *argvars, typval_T *rettv)
5545{
5546 win_T *oldcurwin;
5547 tabpage_T *tp, *oldtabpage;
5548 dictitem_T *v;
5549 char_u *varname;
5550 int done = FALSE;
5551
5552 rettv->v_type = VAR_STRING;
5553 rettv->vval.v_string = NULL;
5554
5555 varname = get_tv_string_chk(&argvars[1]);
5556 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5557 if (tp != NULL && varname != NULL)
5558 {
5559 /* Set tp to be our tabpage, temporarily. Also set the window to the
5560 * first window in the tabpage, otherwise the window is not valid. */
5561 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005562 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5563 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005564 {
5565 /* look up the variable */
5566 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5567 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5568 if (v != NULL)
5569 {
5570 copy_tv(&v->di_tv, rettv);
5571 done = TRUE;
5572 }
5573 }
5574
5575 /* restore previous notion of curwin */
5576 restore_win(oldcurwin, oldtabpage, TRUE);
5577 }
5578
5579 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5580 copy_tv(&argvars[2], rettv);
5581}
5582
5583/*
5584 * "gettabwinvar()" function
5585 */
5586 static void
5587f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5588{
5589 getwinvar(argvars, rettv, 1);
5590}
5591
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005592/*
5593 * Returns information about a window as a dictionary.
5594 */
5595 static dict_T *
5596get_win_info(win_T *wp, short tpnr, short winnr)
5597{
5598 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005599
5600 dict = dict_alloc();
5601 if (dict == NULL)
5602 return NULL;
5603
Bram Moolenaare0be1672018-07-08 16:50:37 +02005604 dict_add_number(dict, "tabnr", tpnr);
5605 dict_add_number(dict, "winnr", winnr);
5606 dict_add_number(dict, "winid", wp->w_id);
5607 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005608 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005609#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02005610 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005611#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005612 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005613 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005614 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005615
Bram Moolenaar69905d12017-08-13 18:14:47 +02005616#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02005617 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02005618#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005619#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02005620 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
5621 dict_add_number(dict, "loclist",
5622 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02005623#endif
5624
Bram Moolenaar30567352016-08-27 21:25:44 +02005625 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005626 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005627
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005628 return dict;
5629}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005630
5631/*
5632 * "getwininfo()" function
5633 */
5634 static void
5635f_getwininfo(typval_T *argvars, typval_T *rettv)
5636{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005637 tabpage_T *tp;
5638 win_T *wp = NULL, *wparg = NULL;
5639 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005640 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005641
5642 if (rettv_list_alloc(rettv) != OK)
5643 return;
5644
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005645 if (argvars[0].v_type != VAR_UNKNOWN)
5646 {
5647 wparg = win_id2wp(argvars);
5648 if (wparg == NULL)
5649 return;
5650 }
5651
5652 /* Collect information about either all the windows across all the tab
5653 * pages or one particular window.
5654 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005655 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005656 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005657 tabnr++;
5658 winnr = 0;
5659 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005660 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005661 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005662 if (wparg != NULL && wp != wparg)
5663 continue;
5664 d = get_win_info(wp, tabnr, winnr);
5665 if (d != NULL)
5666 list_append_dict(rettv->vval.v_list, d);
5667 if (wparg != NULL)
5668 /* found information about a specific window */
5669 return;
5670 }
5671 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005672}
5673
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005674/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005675 * "win_findbuf()" function
5676 */
5677 static void
5678f_win_findbuf(typval_T *argvars, typval_T *rettv)
5679{
5680 if (rettv_list_alloc(rettv) != FAIL)
5681 win_findbuf(argvars, rettv->vval.v_list);
5682}
5683
5684/*
5685 * "win_getid()" function
5686 */
5687 static void
5688f_win_getid(typval_T *argvars, typval_T *rettv)
5689{
5690 rettv->vval.v_number = win_getid(argvars);
5691}
5692
5693/*
5694 * "win_gotoid()" function
5695 */
5696 static void
5697f_win_gotoid(typval_T *argvars, typval_T *rettv)
5698{
5699 rettv->vval.v_number = win_gotoid(argvars);
5700}
5701
5702/*
5703 * "win_id2tabwin()" function
5704 */
5705 static void
5706f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5707{
5708 if (rettv_list_alloc(rettv) != FAIL)
5709 win_id2tabwin(argvars, rettv->vval.v_list);
5710}
5711
5712/*
5713 * "win_id2win()" function
5714 */
5715 static void
5716f_win_id2win(typval_T *argvars, typval_T *rettv)
5717{
5718 rettv->vval.v_number = win_id2win(argvars);
5719}
5720
5721/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005722 * "win_screenpos()" function
5723 */
5724 static void
5725f_win_screenpos(typval_T *argvars, typval_T *rettv)
5726{
5727 win_T *wp;
5728
5729 if (rettv_list_alloc(rettv) == FAIL)
5730 return;
5731
5732 wp = find_win_by_nr(&argvars[0], NULL);
5733 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5734 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5735}
5736
5737/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005738 * "getwinpos({timeout})" function
5739 */
5740 static void
5741f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
5742{
5743 int x = -1;
5744 int y = -1;
5745
5746 if (rettv_list_alloc(rettv) == FAIL)
5747 return;
5748#ifdef FEAT_GUI
5749 if (gui.in_use)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01005750 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005751# if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5752 else
5753# endif
5754#endif
5755#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5756 {
5757 varnumber_T timeout = 100;
5758
5759 if (argvars[0].v_type != VAR_UNKNOWN)
5760 timeout = get_tv_number(&argvars[0]);
5761 term_get_winpos(&x, &y, timeout);
5762 }
5763#endif
5764 list_append_number(rettv->vval.v_list, (varnumber_T)x);
5765 list_append_number(rettv->vval.v_list, (varnumber_T)y);
5766}
5767
5768
5769/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005770 * "getwinposx()" function
5771 */
5772 static void
5773f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5774{
5775 rettv->vval.v_number = -1;
5776#ifdef FEAT_GUI
5777 if (gui.in_use)
5778 {
5779 int x, y;
5780
5781 if (gui_mch_get_winpos(&x, &y) == OK)
5782 rettv->vval.v_number = x;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005783 return;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005784 }
5785#endif
5786#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5787 {
5788 int x, y;
5789
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005790 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005791 rettv->vval.v_number = x;
5792 }
5793#endif
5794}
5795
5796/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005797 * "getwinposy()" function
5798 */
5799 static void
5800f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5801{
5802 rettv->vval.v_number = -1;
5803#ifdef FEAT_GUI
5804 if (gui.in_use)
5805 {
5806 int x, y;
5807
5808 if (gui_mch_get_winpos(&x, &y) == OK)
5809 rettv->vval.v_number = y;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005810 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005811 }
5812#endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005813#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5814 {
5815 int x, y;
5816
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005817 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005818 rettv->vval.v_number = y;
5819 }
5820#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005821}
5822
5823/*
5824 * "getwinvar()" function
5825 */
5826 static void
5827f_getwinvar(typval_T *argvars, typval_T *rettv)
5828{
5829 getwinvar(argvars, rettv, 0);
5830}
5831
5832/*
5833 * "glob()" function
5834 */
5835 static void
5836f_glob(typval_T *argvars, typval_T *rettv)
5837{
5838 int options = WILD_SILENT|WILD_USE_NL;
5839 expand_T xpc;
5840 int error = FALSE;
5841
5842 /* When the optional second argument is non-zero, don't remove matches
5843 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5844 rettv->v_type = VAR_STRING;
5845 if (argvars[1].v_type != VAR_UNKNOWN)
5846 {
5847 if (get_tv_number_chk(&argvars[1], &error))
5848 options |= WILD_KEEP_ALL;
5849 if (argvars[2].v_type != VAR_UNKNOWN)
5850 {
5851 if (get_tv_number_chk(&argvars[2], &error))
5852 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005853 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005854 }
5855 if (argvars[3].v_type != VAR_UNKNOWN
5856 && get_tv_number_chk(&argvars[3], &error))
5857 options |= WILD_ALLLINKS;
5858 }
5859 }
5860 if (!error)
5861 {
5862 ExpandInit(&xpc);
5863 xpc.xp_context = EXPAND_FILES;
5864 if (p_wic)
5865 options += WILD_ICASE;
5866 if (rettv->v_type == VAR_STRING)
5867 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
5868 NULL, options, WILD_ALL);
5869 else if (rettv_list_alloc(rettv) != FAIL)
5870 {
5871 int i;
5872
5873 ExpandOne(&xpc, get_tv_string(&argvars[0]),
5874 NULL, options, WILD_ALL_KEEP);
5875 for (i = 0; i < xpc.xp_numfiles; i++)
5876 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5877
5878 ExpandCleanup(&xpc);
5879 }
5880 }
5881 else
5882 rettv->vval.v_string = NULL;
5883}
5884
5885/*
5886 * "globpath()" function
5887 */
5888 static void
5889f_globpath(typval_T *argvars, typval_T *rettv)
5890{
5891 int flags = 0;
5892 char_u buf1[NUMBUFLEN];
5893 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
5894 int error = FALSE;
5895 garray_T ga;
5896 int i;
5897
5898 /* When the optional second argument is non-zero, don't remove matches
5899 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5900 rettv->v_type = VAR_STRING;
5901 if (argvars[2].v_type != VAR_UNKNOWN)
5902 {
5903 if (get_tv_number_chk(&argvars[2], &error))
5904 flags |= WILD_KEEP_ALL;
5905 if (argvars[3].v_type != VAR_UNKNOWN)
5906 {
5907 if (get_tv_number_chk(&argvars[3], &error))
5908 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005909 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005910 }
5911 if (argvars[4].v_type != VAR_UNKNOWN
5912 && get_tv_number_chk(&argvars[4], &error))
5913 flags |= WILD_ALLLINKS;
5914 }
5915 }
5916 if (file != NULL && !error)
5917 {
5918 ga_init2(&ga, (int)sizeof(char_u *), 10);
5919 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
5920 if (rettv->v_type == VAR_STRING)
5921 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
5922 else if (rettv_list_alloc(rettv) != FAIL)
5923 for (i = 0; i < ga.ga_len; ++i)
5924 list_append_string(rettv->vval.v_list,
5925 ((char_u **)(ga.ga_data))[i], -1);
5926 ga_clear_strings(&ga);
5927 }
5928 else
5929 rettv->vval.v_string = NULL;
5930}
5931
5932/*
5933 * "glob2regpat()" function
5934 */
5935 static void
5936f_glob2regpat(typval_T *argvars, typval_T *rettv)
5937{
5938 char_u *pat = get_tv_string_chk(&argvars[0]);
5939
5940 rettv->v_type = VAR_STRING;
5941 rettv->vval.v_string = (pat == NULL)
5942 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
5943}
5944
5945/* for VIM_VERSION_ defines */
5946#include "version.h"
5947
5948/*
5949 * "has()" function
5950 */
5951 static void
5952f_has(typval_T *argvars, typval_T *rettv)
5953{
5954 int i;
5955 char_u *name;
5956 int n = FALSE;
5957 static char *(has_list[]) =
5958 {
5959#ifdef AMIGA
5960 "amiga",
5961# ifdef FEAT_ARP
5962 "arp",
5963# endif
5964#endif
5965#ifdef __BEOS__
5966 "beos",
5967#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02005968#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01005969 "mac", /* Mac OS X (and, once, Mac OS Classic) */
5970 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02005971# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01005972 "macunix", /* Mac OS X, with the darwin feature */
5973 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02005974# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005975#endif
5976#ifdef __QNX__
5977 "qnx",
5978#endif
5979#ifdef UNIX
5980 "unix",
5981#endif
5982#ifdef VMS
5983 "vms",
5984#endif
5985#ifdef WIN32
5986 "win32",
5987#endif
5988#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
5989 "win32unix",
5990#endif
5991#if defined(WIN64) || defined(_WIN64)
5992 "win64",
5993#endif
5994#ifdef EBCDIC
5995 "ebcdic",
5996#endif
5997#ifndef CASE_INSENSITIVE_FILENAME
5998 "fname_case",
5999#endif
6000#ifdef HAVE_ACL
6001 "acl",
6002#endif
6003#ifdef FEAT_ARABIC
6004 "arabic",
6005#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006006 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006007#ifdef FEAT_AUTOCHDIR
6008 "autochdir",
6009#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006010#ifdef FEAT_AUTOSERVERNAME
6011 "autoservername",
6012#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006013#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006014 "balloon_eval",
6015# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
6016 "balloon_multiline",
6017# endif
6018#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006019#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006020 "balloon_eval_term",
6021#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006022#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6023 "builtin_terms",
6024# ifdef ALL_BUILTIN_TCAPS
6025 "all_builtin_terms",
6026# endif
6027#endif
6028#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
6029 || defined(FEAT_GUI_W32) \
6030 || defined(FEAT_GUI_MOTIF))
6031 "browsefilter",
6032#endif
6033#ifdef FEAT_BYTEOFF
6034 "byte_offset",
6035#endif
6036#ifdef FEAT_JOB_CHANNEL
6037 "channel",
6038#endif
6039#ifdef FEAT_CINDENT
6040 "cindent",
6041#endif
6042#ifdef FEAT_CLIENTSERVER
6043 "clientserver",
6044#endif
6045#ifdef FEAT_CLIPBOARD
6046 "clipboard",
6047#endif
6048#ifdef FEAT_CMDL_COMPL
6049 "cmdline_compl",
6050#endif
6051#ifdef FEAT_CMDHIST
6052 "cmdline_hist",
6053#endif
6054#ifdef FEAT_COMMENTS
6055 "comments",
6056#endif
6057#ifdef FEAT_CONCEAL
6058 "conceal",
6059#endif
6060#ifdef FEAT_CRYPT
6061 "cryptv",
6062 "crypt-blowfish",
6063 "crypt-blowfish2",
6064#endif
6065#ifdef FEAT_CSCOPE
6066 "cscope",
6067#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006068 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006069#ifdef CURSOR_SHAPE
6070 "cursorshape",
6071#endif
6072#ifdef DEBUG
6073 "debug",
6074#endif
6075#ifdef FEAT_CON_DIALOG
6076 "dialog_con",
6077#endif
6078#ifdef FEAT_GUI_DIALOG
6079 "dialog_gui",
6080#endif
6081#ifdef FEAT_DIFF
6082 "diff",
6083#endif
6084#ifdef FEAT_DIGRAPHS
6085 "digraphs",
6086#endif
6087#ifdef FEAT_DIRECTX
6088 "directx",
6089#endif
6090#ifdef FEAT_DND
6091 "dnd",
6092#endif
6093#ifdef FEAT_EMACS_TAGS
6094 "emacs_tags",
6095#endif
6096 "eval", /* always present, of course! */
6097 "ex_extra", /* graduated feature */
6098#ifdef FEAT_SEARCH_EXTRA
6099 "extra_search",
6100#endif
6101#ifdef FEAT_FKMAP
6102 "farsi",
6103#endif
6104#ifdef FEAT_SEARCHPATH
6105 "file_in_path",
6106#endif
6107#ifdef FEAT_FILTERPIPE
6108 "filterpipe",
6109#endif
6110#ifdef FEAT_FIND_ID
6111 "find_in_path",
6112#endif
6113#ifdef FEAT_FLOAT
6114 "float",
6115#endif
6116#ifdef FEAT_FOLDING
6117 "folding",
6118#endif
6119#ifdef FEAT_FOOTER
6120 "footer",
6121#endif
6122#if !defined(USE_SYSTEM) && defined(UNIX)
6123 "fork",
6124#endif
6125#ifdef FEAT_GETTEXT
6126 "gettext",
6127#endif
6128#ifdef FEAT_GUI
6129 "gui",
6130#endif
6131#ifdef FEAT_GUI_ATHENA
6132# ifdef FEAT_GUI_NEXTAW
6133 "gui_neXtaw",
6134# else
6135 "gui_athena",
6136# endif
6137#endif
6138#ifdef FEAT_GUI_GTK
6139 "gui_gtk",
6140# ifdef USE_GTK3
6141 "gui_gtk3",
6142# else
6143 "gui_gtk2",
6144# endif
6145#endif
6146#ifdef FEAT_GUI_GNOME
6147 "gui_gnome",
6148#endif
6149#ifdef FEAT_GUI_MAC
6150 "gui_mac",
6151#endif
6152#ifdef FEAT_GUI_MOTIF
6153 "gui_motif",
6154#endif
6155#ifdef FEAT_GUI_PHOTON
6156 "gui_photon",
6157#endif
6158#ifdef FEAT_GUI_W32
6159 "gui_win32",
6160#endif
6161#ifdef FEAT_HANGULIN
6162 "hangul_input",
6163#endif
6164#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6165 "iconv",
6166#endif
6167#ifdef FEAT_INS_EXPAND
6168 "insert_expand",
6169#endif
6170#ifdef FEAT_JOB_CHANNEL
6171 "job",
6172#endif
6173#ifdef FEAT_JUMPLIST
6174 "jumplist",
6175#endif
6176#ifdef FEAT_KEYMAP
6177 "keymap",
6178#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006179 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006180#ifdef FEAT_LANGMAP
6181 "langmap",
6182#endif
6183#ifdef FEAT_LIBCALL
6184 "libcall",
6185#endif
6186#ifdef FEAT_LINEBREAK
6187 "linebreak",
6188#endif
6189#ifdef FEAT_LISP
6190 "lispindent",
6191#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006192 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006193#ifdef FEAT_LOCALMAP
6194 "localmap",
6195#endif
6196#ifdef FEAT_LUA
6197# ifndef DYNAMIC_LUA
6198 "lua",
6199# endif
6200#endif
6201#ifdef FEAT_MENU
6202 "menu",
6203#endif
6204#ifdef FEAT_SESSION
6205 "mksession",
6206#endif
6207#ifdef FEAT_MODIFY_FNAME
6208 "modify_fname",
6209#endif
6210#ifdef FEAT_MOUSE
6211 "mouse",
6212#endif
6213#ifdef FEAT_MOUSESHAPE
6214 "mouseshape",
6215#endif
6216#if defined(UNIX) || defined(VMS)
6217# ifdef FEAT_MOUSE_DEC
6218 "mouse_dec",
6219# endif
6220# ifdef FEAT_MOUSE_GPM
6221 "mouse_gpm",
6222# endif
6223# ifdef FEAT_MOUSE_JSB
6224 "mouse_jsbterm",
6225# endif
6226# ifdef FEAT_MOUSE_NET
6227 "mouse_netterm",
6228# endif
6229# ifdef FEAT_MOUSE_PTERM
6230 "mouse_pterm",
6231# endif
6232# ifdef FEAT_MOUSE_SGR
6233 "mouse_sgr",
6234# endif
6235# ifdef FEAT_SYSMOUSE
6236 "mouse_sysmouse",
6237# endif
6238# ifdef FEAT_MOUSE_URXVT
6239 "mouse_urxvt",
6240# endif
6241# ifdef FEAT_MOUSE_XTERM
6242 "mouse_xterm",
6243# endif
6244#endif
6245#ifdef FEAT_MBYTE
6246 "multi_byte",
6247#endif
6248#ifdef FEAT_MBYTE_IME
6249 "multi_byte_ime",
6250#endif
6251#ifdef FEAT_MULTI_LANG
6252 "multi_lang",
6253#endif
6254#ifdef FEAT_MZSCHEME
6255#ifndef DYNAMIC_MZSCHEME
6256 "mzscheme",
6257#endif
6258#endif
6259#ifdef FEAT_NUM64
6260 "num64",
6261#endif
6262#ifdef FEAT_OLE
6263 "ole",
6264#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006265#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006266 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006267#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006268#ifdef FEAT_PATH_EXTRA
6269 "path_extra",
6270#endif
6271#ifdef FEAT_PERL
6272#ifndef DYNAMIC_PERL
6273 "perl",
6274#endif
6275#endif
6276#ifdef FEAT_PERSISTENT_UNDO
6277 "persistent_undo",
6278#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006279#if defined(FEAT_PYTHON)
6280 "python_compiled",
6281# if defined(DYNAMIC_PYTHON)
6282 "python_dynamic",
6283# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006284 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006285 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006286# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006287#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006288#if defined(FEAT_PYTHON3)
6289 "python3_compiled",
6290# if defined(DYNAMIC_PYTHON3)
6291 "python3_dynamic",
6292# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006293 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006294 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006295# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006296#endif
6297#ifdef FEAT_POSTSCRIPT
6298 "postscript",
6299#endif
6300#ifdef FEAT_PRINTER
6301 "printer",
6302#endif
6303#ifdef FEAT_PROFILE
6304 "profile",
6305#endif
6306#ifdef FEAT_RELTIME
6307 "reltime",
6308#endif
6309#ifdef FEAT_QUICKFIX
6310 "quickfix",
6311#endif
6312#ifdef FEAT_RIGHTLEFT
6313 "rightleft",
6314#endif
6315#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6316 "ruby",
6317#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006318 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006319#ifdef FEAT_CMDL_INFO
6320 "showcmd",
6321 "cmdline_info",
6322#endif
6323#ifdef FEAT_SIGNS
6324 "signs",
6325#endif
6326#ifdef FEAT_SMARTINDENT
6327 "smartindent",
6328#endif
6329#ifdef STARTUPTIME
6330 "startuptime",
6331#endif
6332#ifdef FEAT_STL_OPT
6333 "statusline",
6334#endif
6335#ifdef FEAT_SUN_WORKSHOP
6336 "sun_workshop",
6337#endif
6338#ifdef FEAT_NETBEANS_INTG
6339 "netbeans_intg",
6340#endif
6341#ifdef FEAT_SPELL
6342 "spell",
6343#endif
6344#ifdef FEAT_SYN_HL
6345 "syntax",
6346#endif
6347#if defined(USE_SYSTEM) || !defined(UNIX)
6348 "system",
6349#endif
6350#ifdef FEAT_TAG_BINS
6351 "tag_binary",
6352#endif
6353#ifdef FEAT_TAG_OLDSTATIC
6354 "tag_old_static",
6355#endif
6356#ifdef FEAT_TAG_ANYWHITE
6357 "tag_any_white",
6358#endif
6359#ifdef FEAT_TCL
6360# ifndef DYNAMIC_TCL
6361 "tcl",
6362# endif
6363#endif
6364#ifdef FEAT_TERMGUICOLORS
6365 "termguicolors",
6366#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006367#if defined(FEAT_TERMINAL) && !defined(WIN3264)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006368 "terminal",
6369#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006370#ifdef TERMINFO
6371 "terminfo",
6372#endif
6373#ifdef FEAT_TERMRESPONSE
6374 "termresponse",
6375#endif
6376#ifdef FEAT_TEXTOBJ
6377 "textobjects",
6378#endif
6379#ifdef HAVE_TGETENT
6380 "tgetent",
6381#endif
6382#ifdef FEAT_TIMERS
6383 "timers",
6384#endif
6385#ifdef FEAT_TITLE
6386 "title",
6387#endif
6388#ifdef FEAT_TOOLBAR
6389 "toolbar",
6390#endif
6391#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6392 "unnamedplus",
6393#endif
6394#ifdef FEAT_USR_CMDS
6395 "user-commands", /* was accidentally included in 5.4 */
6396 "user_commands",
6397#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006398#ifdef FEAT_VARTABS
6399 "vartabs",
6400#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006401#ifdef FEAT_VIMINFO
6402 "viminfo",
6403#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006404 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006405#ifdef FEAT_VIRTUALEDIT
6406 "virtualedit",
6407#endif
6408 "visual",
6409#ifdef FEAT_VISUALEXTRA
6410 "visualextra",
6411#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006412 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006413#ifdef FEAT_VTP
6414 "vtp",
6415#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006416#ifdef FEAT_WILDIGN
6417 "wildignore",
6418#endif
6419#ifdef FEAT_WILDMENU
6420 "wildmenu",
6421#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006422 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006423#ifdef FEAT_WAK
6424 "winaltkeys",
6425#endif
6426#ifdef FEAT_WRITEBACKUP
6427 "writebackup",
6428#endif
6429#ifdef FEAT_XIM
6430 "xim",
6431#endif
6432#ifdef FEAT_XFONTSET
6433 "xfontset",
6434#endif
6435#ifdef FEAT_XPM_W32
6436 "xpm",
6437 "xpm_w32", /* for backward compatibility */
6438#else
6439# if defined(HAVE_XPM)
6440 "xpm",
6441# endif
6442#endif
6443#ifdef USE_XSMP
6444 "xsmp",
6445#endif
6446#ifdef USE_XSMP_INTERACT
6447 "xsmp_interact",
6448#endif
6449#ifdef FEAT_XCLIPBOARD
6450 "xterm_clipboard",
6451#endif
6452#ifdef FEAT_XTERM_SAVE
6453 "xterm_save",
6454#endif
6455#if defined(UNIX) && defined(FEAT_X11)
6456 "X11",
6457#endif
6458 NULL
6459 };
6460
6461 name = get_tv_string(&argvars[0]);
6462 for (i = 0; has_list[i] != NULL; ++i)
6463 if (STRICMP(name, has_list[i]) == 0)
6464 {
6465 n = TRUE;
6466 break;
6467 }
6468
6469 if (n == FALSE)
6470 {
6471 if (STRNICMP(name, "patch", 5) == 0)
6472 {
6473 if (name[5] == '-'
6474 && STRLEN(name) >= 11
6475 && vim_isdigit(name[6])
6476 && vim_isdigit(name[8])
6477 && vim_isdigit(name[10]))
6478 {
6479 int major = atoi((char *)name + 6);
6480 int minor = atoi((char *)name + 8);
6481
6482 /* Expect "patch-9.9.01234". */
6483 n = (major < VIM_VERSION_MAJOR
6484 || (major == VIM_VERSION_MAJOR
6485 && (minor < VIM_VERSION_MINOR
6486 || (minor == VIM_VERSION_MINOR
6487 && has_patch(atoi((char *)name + 10))))));
6488 }
6489 else
6490 n = has_patch(atoi((char *)name + 5));
6491 }
6492 else if (STRICMP(name, "vim_starting") == 0)
6493 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006494 else if (STRICMP(name, "ttyin") == 0)
6495 n = mch_input_isatty();
6496 else if (STRICMP(name, "ttyout") == 0)
6497 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006498#ifdef FEAT_MBYTE
6499 else if (STRICMP(name, "multi_byte_encoding") == 0)
6500 n = has_mbyte;
6501#endif
6502#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
6503 else if (STRICMP(name, "balloon_multiline") == 0)
6504 n = multiline_balloon_available();
6505#endif
6506#ifdef DYNAMIC_TCL
6507 else if (STRICMP(name, "tcl") == 0)
6508 n = tcl_enabled(FALSE);
6509#endif
6510#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6511 else if (STRICMP(name, "iconv") == 0)
6512 n = iconv_enabled(FALSE);
6513#endif
6514#ifdef DYNAMIC_LUA
6515 else if (STRICMP(name, "lua") == 0)
6516 n = lua_enabled(FALSE);
6517#endif
6518#ifdef DYNAMIC_MZSCHEME
6519 else if (STRICMP(name, "mzscheme") == 0)
6520 n = mzscheme_enabled(FALSE);
6521#endif
6522#ifdef DYNAMIC_RUBY
6523 else if (STRICMP(name, "ruby") == 0)
6524 n = ruby_enabled(FALSE);
6525#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006526#ifdef DYNAMIC_PYTHON
6527 else if (STRICMP(name, "python") == 0)
6528 n = python_enabled(FALSE);
6529#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006530#ifdef DYNAMIC_PYTHON3
6531 else if (STRICMP(name, "python3") == 0)
6532 n = python3_enabled(FALSE);
6533#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006534#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6535 else if (STRICMP(name, "pythonx") == 0)
6536 {
6537# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6538 if (p_pyx == 0)
6539 n = python3_enabled(FALSE) || python_enabled(FALSE);
6540 else if (p_pyx == 3)
6541 n = python3_enabled(FALSE);
6542 else if (p_pyx == 2)
6543 n = python_enabled(FALSE);
6544# elif defined(DYNAMIC_PYTHON)
6545 n = python_enabled(FALSE);
6546# elif defined(DYNAMIC_PYTHON3)
6547 n = python3_enabled(FALSE);
6548# endif
6549 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006550#endif
6551#ifdef DYNAMIC_PERL
6552 else if (STRICMP(name, "perl") == 0)
6553 n = perl_enabled(FALSE);
6554#endif
6555#ifdef FEAT_GUI
6556 else if (STRICMP(name, "gui_running") == 0)
6557 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006558# ifdef FEAT_BROWSE
6559 else if (STRICMP(name, "browse") == 0)
6560 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6561# endif
6562#endif
6563#ifdef FEAT_SYN_HL
6564 else if (STRICMP(name, "syntax_items") == 0)
6565 n = syntax_present(curwin);
6566#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006567#ifdef FEAT_VTP
6568 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02006569 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006570#endif
6571#ifdef FEAT_NETBEANS_INTG
6572 else if (STRICMP(name, "netbeans_enabled") == 0)
6573 n = netbeans_active();
6574#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006575#if defined(FEAT_TERMINAL) && defined(WIN3264)
6576 else if (STRICMP(name, "terminal") == 0)
6577 n = terminal_enabled();
6578#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006579 }
6580
6581 rettv->vval.v_number = n;
6582}
6583
6584/*
6585 * "has_key()" function
6586 */
6587 static void
6588f_has_key(typval_T *argvars, typval_T *rettv)
6589{
6590 if (argvars[0].v_type != VAR_DICT)
6591 {
6592 EMSG(_(e_dictreq));
6593 return;
6594 }
6595 if (argvars[0].vval.v_dict == NULL)
6596 return;
6597
6598 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
6599 get_tv_string(&argvars[1]), -1) != NULL;
6600}
6601
6602/*
6603 * "haslocaldir()" function
6604 */
6605 static void
6606f_haslocaldir(typval_T *argvars, typval_T *rettv)
6607{
6608 win_T *wp = NULL;
6609
6610 wp = find_tabwin(&argvars[0], &argvars[1]);
6611 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6612}
6613
6614/*
6615 * "hasmapto()" function
6616 */
6617 static void
6618f_hasmapto(typval_T *argvars, typval_T *rettv)
6619{
6620 char_u *name;
6621 char_u *mode;
6622 char_u buf[NUMBUFLEN];
6623 int abbr = FALSE;
6624
6625 name = get_tv_string(&argvars[0]);
6626 if (argvars[1].v_type == VAR_UNKNOWN)
6627 mode = (char_u *)"nvo";
6628 else
6629 {
6630 mode = get_tv_string_buf(&argvars[1], buf);
6631 if (argvars[2].v_type != VAR_UNKNOWN)
6632 abbr = (int)get_tv_number(&argvars[2]);
6633 }
6634
6635 if (map_to_exists(name, mode, abbr))
6636 rettv->vval.v_number = TRUE;
6637 else
6638 rettv->vval.v_number = FALSE;
6639}
6640
6641/*
6642 * "histadd()" function
6643 */
6644 static void
6645f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6646{
6647#ifdef FEAT_CMDHIST
6648 int histype;
6649 char_u *str;
6650 char_u buf[NUMBUFLEN];
6651#endif
6652
6653 rettv->vval.v_number = FALSE;
6654 if (check_restricted() || check_secure())
6655 return;
6656#ifdef FEAT_CMDHIST
6657 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6658 histype = str != NULL ? get_histtype(str) : -1;
6659 if (histype >= 0)
6660 {
6661 str = get_tv_string_buf(&argvars[1], buf);
6662 if (*str != NUL)
6663 {
6664 init_history();
6665 add_to_history(histype, str, FALSE, NUL);
6666 rettv->vval.v_number = TRUE;
6667 return;
6668 }
6669 }
6670#endif
6671}
6672
6673/*
6674 * "histdel()" function
6675 */
6676 static void
6677f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6678{
6679#ifdef FEAT_CMDHIST
6680 int n;
6681 char_u buf[NUMBUFLEN];
6682 char_u *str;
6683
6684 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6685 if (str == NULL)
6686 n = 0;
6687 else if (argvars[1].v_type == VAR_UNKNOWN)
6688 /* only one argument: clear entire history */
6689 n = clr_history(get_histtype(str));
6690 else if (argvars[1].v_type == VAR_NUMBER)
6691 /* index given: remove that entry */
6692 n = del_history_idx(get_histtype(str),
6693 (int)get_tv_number(&argvars[1]));
6694 else
6695 /* string given: remove all matching entries */
6696 n = del_history_entry(get_histtype(str),
6697 get_tv_string_buf(&argvars[1], buf));
6698 rettv->vval.v_number = n;
6699#endif
6700}
6701
6702/*
6703 * "histget()" function
6704 */
6705 static void
6706f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6707{
6708#ifdef FEAT_CMDHIST
6709 int type;
6710 int idx;
6711 char_u *str;
6712
6713 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6714 if (str == NULL)
6715 rettv->vval.v_string = NULL;
6716 else
6717 {
6718 type = get_histtype(str);
6719 if (argvars[1].v_type == VAR_UNKNOWN)
6720 idx = get_history_idx(type);
6721 else
6722 idx = (int)get_tv_number_chk(&argvars[1], NULL);
6723 /* -1 on type error */
6724 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6725 }
6726#else
6727 rettv->vval.v_string = NULL;
6728#endif
6729 rettv->v_type = VAR_STRING;
6730}
6731
6732/*
6733 * "histnr()" function
6734 */
6735 static void
6736f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6737{
6738 int i;
6739
6740#ifdef FEAT_CMDHIST
6741 char_u *history = get_tv_string_chk(&argvars[0]);
6742
6743 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6744 if (i >= HIST_CMD && i < HIST_COUNT)
6745 i = get_history_idx(i);
6746 else
6747#endif
6748 i = -1;
6749 rettv->vval.v_number = i;
6750}
6751
6752/*
6753 * "highlightID(name)" function
6754 */
6755 static void
6756f_hlID(typval_T *argvars, typval_T *rettv)
6757{
6758 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
6759}
6760
6761/*
6762 * "highlight_exists()" function
6763 */
6764 static void
6765f_hlexists(typval_T *argvars, typval_T *rettv)
6766{
6767 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
6768}
6769
6770/*
6771 * "hostname()" function
6772 */
6773 static void
6774f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6775{
6776 char_u hostname[256];
6777
6778 mch_get_host_name(hostname, 256);
6779 rettv->v_type = VAR_STRING;
6780 rettv->vval.v_string = vim_strsave(hostname);
6781}
6782
6783/*
6784 * iconv() function
6785 */
6786 static void
6787f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6788{
6789#ifdef FEAT_MBYTE
6790 char_u buf1[NUMBUFLEN];
6791 char_u buf2[NUMBUFLEN];
6792 char_u *from, *to, *str;
6793 vimconv_T vimconv;
6794#endif
6795
6796 rettv->v_type = VAR_STRING;
6797 rettv->vval.v_string = NULL;
6798
6799#ifdef FEAT_MBYTE
6800 str = get_tv_string(&argvars[0]);
6801 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
6802 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
6803 vimconv.vc_type = CONV_NONE;
6804 convert_setup(&vimconv, from, to);
6805
6806 /* If the encodings are equal, no conversion needed. */
6807 if (vimconv.vc_type == CONV_NONE)
6808 rettv->vval.v_string = vim_strsave(str);
6809 else
6810 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6811
6812 convert_setup(&vimconv, NULL, NULL);
6813 vim_free(from);
6814 vim_free(to);
6815#endif
6816}
6817
6818/*
6819 * "indent()" function
6820 */
6821 static void
6822f_indent(typval_T *argvars, typval_T *rettv)
6823{
6824 linenr_T lnum;
6825
6826 lnum = get_tv_lnum(argvars);
6827 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6828 rettv->vval.v_number = get_indent_lnum(lnum);
6829 else
6830 rettv->vval.v_number = -1;
6831}
6832
6833/*
6834 * "index()" function
6835 */
6836 static void
6837f_index(typval_T *argvars, typval_T *rettv)
6838{
6839 list_T *l;
6840 listitem_T *item;
6841 long idx = 0;
6842 int ic = FALSE;
6843
6844 rettv->vval.v_number = -1;
6845 if (argvars[0].v_type != VAR_LIST)
6846 {
6847 EMSG(_(e_listreq));
6848 return;
6849 }
6850 l = argvars[0].vval.v_list;
6851 if (l != NULL)
6852 {
6853 item = l->lv_first;
6854 if (argvars[2].v_type != VAR_UNKNOWN)
6855 {
6856 int error = FALSE;
6857
6858 /* Start at specified item. Use the cached index that list_find()
6859 * sets, so that a negative number also works. */
6860 item = list_find(l, (long)get_tv_number_chk(&argvars[2], &error));
6861 idx = l->lv_idx;
6862 if (argvars[3].v_type != VAR_UNKNOWN)
6863 ic = (int)get_tv_number_chk(&argvars[3], &error);
6864 if (error)
6865 item = NULL;
6866 }
6867
6868 for ( ; item != NULL; item = item->li_next, ++idx)
6869 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6870 {
6871 rettv->vval.v_number = idx;
6872 break;
6873 }
6874 }
6875}
6876
6877static int inputsecret_flag = 0;
6878
6879/*
6880 * "input()" function
6881 * Also handles inputsecret() when inputsecret is set.
6882 */
6883 static void
6884f_input(typval_T *argvars, typval_T *rettv)
6885{
6886 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6887}
6888
6889/*
6890 * "inputdialog()" function
6891 */
6892 static void
6893f_inputdialog(typval_T *argvars, typval_T *rettv)
6894{
6895#if defined(FEAT_GUI_TEXTDIALOG)
6896 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
6897 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6898 {
6899 char_u *message;
6900 char_u buf[NUMBUFLEN];
6901 char_u *defstr = (char_u *)"";
6902
6903 message = get_tv_string_chk(&argvars[0]);
6904 if (argvars[1].v_type != VAR_UNKNOWN
6905 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
6906 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6907 else
6908 IObuff[0] = NUL;
6909 if (message != NULL && defstr != NULL
6910 && do_dialog(VIM_QUESTION, NULL, message,
6911 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6912 rettv->vval.v_string = vim_strsave(IObuff);
6913 else
6914 {
6915 if (message != NULL && defstr != NULL
6916 && argvars[1].v_type != VAR_UNKNOWN
6917 && argvars[2].v_type != VAR_UNKNOWN)
6918 rettv->vval.v_string = vim_strsave(
6919 get_tv_string_buf(&argvars[2], buf));
6920 else
6921 rettv->vval.v_string = NULL;
6922 }
6923 rettv->v_type = VAR_STRING;
6924 }
6925 else
6926#endif
6927 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6928}
6929
6930/*
6931 * "inputlist()" function
6932 */
6933 static void
6934f_inputlist(typval_T *argvars, typval_T *rettv)
6935{
6936 listitem_T *li;
6937 int selected;
6938 int mouse_used;
6939
6940#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02006941 /* While starting up, there is no place to enter text. When running tests
6942 * with --not-a-term we assume feedkeys() will be used. */
6943 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006944 return;
6945#endif
6946 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6947 {
6948 EMSG2(_(e_listarg), "inputlist()");
6949 return;
6950 }
6951
6952 msg_start();
6953 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
6954 lines_left = Rows; /* avoid more prompt */
6955 msg_scroll = TRUE;
6956 msg_clr_eos();
6957
6958 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
6959 {
6960 msg_puts(get_tv_string(&li->li_tv));
6961 msg_putchar('\n');
6962 }
6963
6964 /* Ask for choice. */
6965 selected = prompt_for_number(&mouse_used);
6966 if (mouse_used)
6967 selected -= lines_left;
6968
6969 rettv->vval.v_number = selected;
6970}
6971
6972
6973static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
6974
6975/*
6976 * "inputrestore()" function
6977 */
6978 static void
6979f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
6980{
6981 if (ga_userinput.ga_len > 0)
6982 {
6983 --ga_userinput.ga_len;
6984 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
6985 + ga_userinput.ga_len);
6986 /* default return is zero == OK */
6987 }
6988 else if (p_verbose > 1)
6989 {
6990 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
6991 rettv->vval.v_number = 1; /* Failed */
6992 }
6993}
6994
6995/*
6996 * "inputsave()" function
6997 */
6998 static void
6999f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7000{
7001 /* Add an entry to the stack of typeahead storage. */
7002 if (ga_grow(&ga_userinput, 1) == OK)
7003 {
7004 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7005 + ga_userinput.ga_len);
7006 ++ga_userinput.ga_len;
7007 /* default return is zero == OK */
7008 }
7009 else
7010 rettv->vval.v_number = 1; /* Failed */
7011}
7012
7013/*
7014 * "inputsecret()" function
7015 */
7016 static void
7017f_inputsecret(typval_T *argvars, typval_T *rettv)
7018{
7019 ++cmdline_star;
7020 ++inputsecret_flag;
7021 f_input(argvars, rettv);
7022 --cmdline_star;
7023 --inputsecret_flag;
7024}
7025
7026/*
7027 * "insert()" function
7028 */
7029 static void
7030f_insert(typval_T *argvars, typval_T *rettv)
7031{
7032 long before = 0;
7033 listitem_T *item;
7034 list_T *l;
7035 int error = FALSE;
7036
7037 if (argvars[0].v_type != VAR_LIST)
7038 EMSG2(_(e_listarg), "insert()");
7039 else if ((l = argvars[0].vval.v_list) != NULL
7040 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
7041 {
7042 if (argvars[2].v_type != VAR_UNKNOWN)
7043 before = (long)get_tv_number_chk(&argvars[2], &error);
7044 if (error)
7045 return; /* type error; errmsg already given */
7046
7047 if (before == l->lv_len)
7048 item = NULL;
7049 else
7050 {
7051 item = list_find(l, before);
7052 if (item == NULL)
7053 {
7054 EMSGN(_(e_listidx), before);
7055 l = NULL;
7056 }
7057 }
7058 if (l != NULL)
7059 {
7060 list_insert_tv(l, &argvars[1], item);
7061 copy_tv(&argvars[0], rettv);
7062 }
7063 }
7064}
7065
7066/*
7067 * "invert(expr)" function
7068 */
7069 static void
7070f_invert(typval_T *argvars, typval_T *rettv)
7071{
7072 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
7073}
7074
7075/*
7076 * "isdirectory()" function
7077 */
7078 static void
7079f_isdirectory(typval_T *argvars, typval_T *rettv)
7080{
7081 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
7082}
7083
7084/*
7085 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7086 * or it refers to a List or Dictionary that is locked.
7087 */
7088 static int
7089tv_islocked(typval_T *tv)
7090{
7091 return (tv->v_lock & VAR_LOCKED)
7092 || (tv->v_type == VAR_LIST
7093 && tv->vval.v_list != NULL
7094 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7095 || (tv->v_type == VAR_DICT
7096 && tv->vval.v_dict != NULL
7097 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7098}
7099
7100/*
7101 * "islocked()" function
7102 */
7103 static void
7104f_islocked(typval_T *argvars, typval_T *rettv)
7105{
7106 lval_T lv;
7107 char_u *end;
7108 dictitem_T *di;
7109
7110 rettv->vval.v_number = -1;
7111 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007112 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007113 if (end != NULL && lv.ll_name != NULL)
7114 {
7115 if (*end != NUL)
7116 EMSG(_(e_trailing));
7117 else
7118 {
7119 if (lv.ll_tv == NULL)
7120 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007121 di = find_var(lv.ll_name, NULL, TRUE);
7122 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007123 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007124 /* Consider a variable locked when:
7125 * 1. the variable itself is locked
7126 * 2. the value of the variable is locked.
7127 * 3. the List or Dict value is locked.
7128 */
7129 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7130 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007131 }
7132 }
7133 else if (lv.ll_range)
7134 EMSG(_("E786: Range not allowed"));
7135 else if (lv.ll_newkey != NULL)
7136 EMSG2(_(e_dictkey), lv.ll_newkey);
7137 else if (lv.ll_list != NULL)
7138 /* List item. */
7139 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7140 else
7141 /* Dictionary item. */
7142 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7143 }
7144 }
7145
7146 clear_lval(&lv);
7147}
7148
7149#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7150/*
7151 * "isnan()" function
7152 */
7153 static void
7154f_isnan(typval_T *argvars, typval_T *rettv)
7155{
7156 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7157 && isnan(argvars[0].vval.v_float);
7158}
7159#endif
7160
7161/*
7162 * "items(dict)" function
7163 */
7164 static void
7165f_items(typval_T *argvars, typval_T *rettv)
7166{
7167 dict_list(argvars, rettv, 2);
7168}
7169
7170#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
7171/*
7172 * Get the job from the argument.
7173 * Returns NULL if the job is invalid.
7174 */
7175 static job_T *
7176get_job_arg(typval_T *tv)
7177{
7178 job_T *job;
7179
7180 if (tv->v_type != VAR_JOB)
7181 {
7182 EMSG2(_(e_invarg2), get_tv_string(tv));
7183 return NULL;
7184 }
7185 job = tv->vval.v_job;
7186
7187 if (job == NULL)
7188 EMSG(_("E916: not a valid job"));
7189 return job;
7190}
7191
7192/*
7193 * "job_getchannel()" function
7194 */
7195 static void
7196f_job_getchannel(typval_T *argvars, typval_T *rettv)
7197{
7198 job_T *job = get_job_arg(&argvars[0]);
7199
7200 if (job != NULL)
7201 {
7202 rettv->v_type = VAR_CHANNEL;
7203 rettv->vval.v_channel = job->jv_channel;
7204 if (job->jv_channel != NULL)
7205 ++job->jv_channel->ch_refcount;
7206 }
7207}
7208
7209/*
7210 * "job_info()" function
7211 */
7212 static void
7213f_job_info(typval_T *argvars, typval_T *rettv)
7214{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007215 if (argvars[0].v_type != VAR_UNKNOWN)
7216 {
7217 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007218
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007219 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7220 job_info(job, rettv->vval.v_dict);
7221 }
7222 else if (rettv_list_alloc(rettv) == OK)
7223 job_info_all(rettv->vval.v_list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007224}
7225
7226/*
7227 * "job_setoptions()" function
7228 */
7229 static void
7230f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7231{
7232 job_T *job = get_job_arg(&argvars[0]);
7233 jobopt_T opt;
7234
7235 if (job == NULL)
7236 return;
7237 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007238 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007239 job_set_options(job, &opt);
7240 free_job_options(&opt);
7241}
7242
7243/*
7244 * "job_start()" function
7245 */
7246 static void
7247f_job_start(typval_T *argvars, typval_T *rettv)
7248{
7249 rettv->v_type = VAR_JOB;
7250 if (check_restricted() || check_secure())
7251 return;
Bram Moolenaar493359e2018-06-12 20:25:52 +02007252 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007253}
7254
7255/*
7256 * "job_status()" function
7257 */
7258 static void
7259f_job_status(typval_T *argvars, typval_T *rettv)
7260{
7261 job_T *job = get_job_arg(&argvars[0]);
7262
7263 if (job != NULL)
7264 {
7265 rettv->v_type = VAR_STRING;
7266 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7267 }
7268}
7269
7270/*
7271 * "job_stop()" function
7272 */
7273 static void
7274f_job_stop(typval_T *argvars, typval_T *rettv)
7275{
7276 job_T *job = get_job_arg(&argvars[0]);
7277
7278 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007279 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007280}
7281#endif
7282
7283/*
7284 * "join()" function
7285 */
7286 static void
7287f_join(typval_T *argvars, typval_T *rettv)
7288{
7289 garray_T ga;
7290 char_u *sep;
7291
7292 if (argvars[0].v_type != VAR_LIST)
7293 {
7294 EMSG(_(e_listreq));
7295 return;
7296 }
7297 if (argvars[0].vval.v_list == NULL)
7298 return;
7299 if (argvars[1].v_type == VAR_UNKNOWN)
7300 sep = (char_u *)" ";
7301 else
7302 sep = get_tv_string_chk(&argvars[1]);
7303
7304 rettv->v_type = VAR_STRING;
7305
7306 if (sep != NULL)
7307 {
7308 ga_init2(&ga, (int)sizeof(char), 80);
7309 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7310 ga_append(&ga, NUL);
7311 rettv->vval.v_string = (char_u *)ga.ga_data;
7312 }
7313 else
7314 rettv->vval.v_string = NULL;
7315}
7316
7317/*
7318 * "js_decode()" function
7319 */
7320 static void
7321f_js_decode(typval_T *argvars, typval_T *rettv)
7322{
7323 js_read_T reader;
7324
7325 reader.js_buf = get_tv_string(&argvars[0]);
7326 reader.js_fill = NULL;
7327 reader.js_used = 0;
7328 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
7329 EMSG(_(e_invarg));
7330}
7331
7332/*
7333 * "js_encode()" function
7334 */
7335 static void
7336f_js_encode(typval_T *argvars, typval_T *rettv)
7337{
7338 rettv->v_type = VAR_STRING;
7339 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7340}
7341
7342/*
7343 * "json_decode()" function
7344 */
7345 static void
7346f_json_decode(typval_T *argvars, typval_T *rettv)
7347{
7348 js_read_T reader;
7349
7350 reader.js_buf = get_tv_string(&argvars[0]);
7351 reader.js_fill = NULL;
7352 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007353 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007354}
7355
7356/*
7357 * "json_encode()" function
7358 */
7359 static void
7360f_json_encode(typval_T *argvars, typval_T *rettv)
7361{
7362 rettv->v_type = VAR_STRING;
7363 rettv->vval.v_string = json_encode(&argvars[0], 0);
7364}
7365
7366/*
7367 * "keys()" function
7368 */
7369 static void
7370f_keys(typval_T *argvars, typval_T *rettv)
7371{
7372 dict_list(argvars, rettv, 0);
7373}
7374
7375/*
7376 * "last_buffer_nr()" function.
7377 */
7378 static void
7379f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7380{
7381 int n = 0;
7382 buf_T *buf;
7383
Bram Moolenaar29323592016-07-24 22:04:11 +02007384 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007385 if (n < buf->b_fnum)
7386 n = buf->b_fnum;
7387
7388 rettv->vval.v_number = n;
7389}
7390
7391/*
7392 * "len()" function
7393 */
7394 static void
7395f_len(typval_T *argvars, typval_T *rettv)
7396{
7397 switch (argvars[0].v_type)
7398 {
7399 case VAR_STRING:
7400 case VAR_NUMBER:
7401 rettv->vval.v_number = (varnumber_T)STRLEN(
7402 get_tv_string(&argvars[0]));
7403 break;
7404 case VAR_LIST:
7405 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7406 break;
7407 case VAR_DICT:
7408 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7409 break;
7410 case VAR_UNKNOWN:
7411 case VAR_SPECIAL:
7412 case VAR_FLOAT:
7413 case VAR_FUNC:
7414 case VAR_PARTIAL:
7415 case VAR_JOB:
7416 case VAR_CHANNEL:
7417 EMSG(_("E701: Invalid type for len()"));
7418 break;
7419 }
7420}
7421
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007422 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007423libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007424{
7425#ifdef FEAT_LIBCALL
7426 char_u *string_in;
7427 char_u **string_result;
7428 int nr_result;
7429#endif
7430
7431 rettv->v_type = type;
7432 if (type != VAR_NUMBER)
7433 rettv->vval.v_string = NULL;
7434
7435 if (check_restricted() || check_secure())
7436 return;
7437
7438#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007439 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007440 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7441 {
7442 string_in = NULL;
7443 if (argvars[2].v_type == VAR_STRING)
7444 string_in = argvars[2].vval.v_string;
7445 if (type == VAR_NUMBER)
7446 string_result = NULL;
7447 else
7448 string_result = &rettv->vval.v_string;
7449 if (mch_libcall(argvars[0].vval.v_string,
7450 argvars[1].vval.v_string,
7451 string_in,
7452 argvars[2].vval.v_number,
7453 string_result,
7454 &nr_result) == OK
7455 && type == VAR_NUMBER)
7456 rettv->vval.v_number = nr_result;
7457 }
7458#endif
7459}
7460
7461/*
7462 * "libcall()" function
7463 */
7464 static void
7465f_libcall(typval_T *argvars, typval_T *rettv)
7466{
7467 libcall_common(argvars, rettv, VAR_STRING);
7468}
7469
7470/*
7471 * "libcallnr()" function
7472 */
7473 static void
7474f_libcallnr(typval_T *argvars, typval_T *rettv)
7475{
7476 libcall_common(argvars, rettv, VAR_NUMBER);
7477}
7478
7479/*
7480 * "line(string)" function
7481 */
7482 static void
7483f_line(typval_T *argvars, typval_T *rettv)
7484{
7485 linenr_T lnum = 0;
7486 pos_T *fp;
7487 int fnum;
7488
7489 fp = var2fpos(&argvars[0], TRUE, &fnum);
7490 if (fp != NULL)
7491 lnum = fp->lnum;
7492 rettv->vval.v_number = lnum;
7493}
7494
7495/*
7496 * "line2byte(lnum)" function
7497 */
7498 static void
7499f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7500{
7501#ifndef FEAT_BYTEOFF
7502 rettv->vval.v_number = -1;
7503#else
7504 linenr_T lnum;
7505
7506 lnum = get_tv_lnum(argvars);
7507 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7508 rettv->vval.v_number = -1;
7509 else
7510 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7511 if (rettv->vval.v_number >= 0)
7512 ++rettv->vval.v_number;
7513#endif
7514}
7515
7516/*
7517 * "lispindent(lnum)" function
7518 */
7519 static void
7520f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7521{
7522#ifdef FEAT_LISP
7523 pos_T pos;
7524 linenr_T lnum;
7525
7526 pos = curwin->w_cursor;
7527 lnum = get_tv_lnum(argvars);
7528 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7529 {
7530 curwin->w_cursor.lnum = lnum;
7531 rettv->vval.v_number = get_lisp_indent();
7532 curwin->w_cursor = pos;
7533 }
7534 else
7535#endif
7536 rettv->vval.v_number = -1;
7537}
7538
7539/*
7540 * "localtime()" function
7541 */
7542 static void
7543f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7544{
7545 rettv->vval.v_number = (varnumber_T)time(NULL);
7546}
7547
7548static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
7549
7550 static void
7551get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7552{
7553 char_u *keys;
7554 char_u *which;
7555 char_u buf[NUMBUFLEN];
7556 char_u *keys_buf = NULL;
7557 char_u *rhs;
7558 int mode;
7559 int abbr = FALSE;
7560 int get_dict = FALSE;
7561 mapblock_T *mp;
7562 int buffer_local;
7563
7564 /* return empty string for failure */
7565 rettv->v_type = VAR_STRING;
7566 rettv->vval.v_string = NULL;
7567
7568 keys = get_tv_string(&argvars[0]);
7569 if (*keys == NUL)
7570 return;
7571
7572 if (argvars[1].v_type != VAR_UNKNOWN)
7573 {
7574 which = get_tv_string_buf_chk(&argvars[1], buf);
7575 if (argvars[2].v_type != VAR_UNKNOWN)
7576 {
7577 abbr = (int)get_tv_number(&argvars[2]);
7578 if (argvars[3].v_type != VAR_UNKNOWN)
7579 get_dict = (int)get_tv_number(&argvars[3]);
7580 }
7581 }
7582 else
7583 which = (char_u *)"";
7584 if (which == NULL)
7585 return;
7586
7587 mode = get_map_mode(&which, 0);
7588
7589 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7590 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7591 vim_free(keys_buf);
7592
7593 if (!get_dict)
7594 {
7595 /* Return a string. */
7596 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02007597 {
7598 if (*rhs == NUL)
7599 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
7600 else
7601 rettv->vval.v_string = str2special_save(rhs, FALSE);
7602 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007603
7604 }
7605 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7606 {
7607 /* Return a dictionary. */
7608 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7609 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7610 dict_T *dict = rettv->vval.v_dict;
7611
Bram Moolenaare0be1672018-07-08 16:50:37 +02007612 dict_add_string(dict, "lhs", lhs);
7613 dict_add_string(dict, "rhs", mp->m_orig_str);
7614 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
7615 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
7616 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
7617 dict_add_number(dict, "sid", (long)mp->m_script_ID);
7618 dict_add_number(dict, "buffer", (long)buffer_local);
7619 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
7620 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007621
7622 vim_free(lhs);
7623 vim_free(mapmode);
7624 }
7625}
7626
7627#ifdef FEAT_FLOAT
7628/*
7629 * "log()" function
7630 */
7631 static void
7632f_log(typval_T *argvars, typval_T *rettv)
7633{
7634 float_T f = 0.0;
7635
7636 rettv->v_type = VAR_FLOAT;
7637 if (get_float_arg(argvars, &f) == OK)
7638 rettv->vval.v_float = log(f);
7639 else
7640 rettv->vval.v_float = 0.0;
7641}
7642
7643/*
7644 * "log10()" function
7645 */
7646 static void
7647f_log10(typval_T *argvars, typval_T *rettv)
7648{
7649 float_T f = 0.0;
7650
7651 rettv->v_type = VAR_FLOAT;
7652 if (get_float_arg(argvars, &f) == OK)
7653 rettv->vval.v_float = log10(f);
7654 else
7655 rettv->vval.v_float = 0.0;
7656}
7657#endif
7658
7659#ifdef FEAT_LUA
7660/*
7661 * "luaeval()" function
7662 */
7663 static void
7664f_luaeval(typval_T *argvars, typval_T *rettv)
7665{
7666 char_u *str;
7667 char_u buf[NUMBUFLEN];
7668
7669 str = get_tv_string_buf(&argvars[0], buf);
7670 do_luaeval(str, argvars + 1, rettv);
7671}
7672#endif
7673
7674/*
7675 * "map()" function
7676 */
7677 static void
7678f_map(typval_T *argvars, typval_T *rettv)
7679{
7680 filter_map(argvars, rettv, TRUE);
7681}
7682
7683/*
7684 * "maparg()" function
7685 */
7686 static void
7687f_maparg(typval_T *argvars, typval_T *rettv)
7688{
7689 get_maparg(argvars, rettv, TRUE);
7690}
7691
7692/*
7693 * "mapcheck()" function
7694 */
7695 static void
7696f_mapcheck(typval_T *argvars, typval_T *rettv)
7697{
7698 get_maparg(argvars, rettv, FALSE);
7699}
7700
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007701typedef enum
7702{
7703 MATCH_END, /* matchend() */
7704 MATCH_MATCH, /* match() */
7705 MATCH_STR, /* matchstr() */
7706 MATCH_LIST, /* matchlist() */
7707 MATCH_POS /* matchstrpos() */
7708} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007709
7710 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007711find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007712{
7713 char_u *str = NULL;
7714 long len = 0;
7715 char_u *expr = NULL;
7716 char_u *pat;
7717 regmatch_T regmatch;
7718 char_u patbuf[NUMBUFLEN];
7719 char_u strbuf[NUMBUFLEN];
7720 char_u *save_cpo;
7721 long start = 0;
7722 long nth = 1;
7723 colnr_T startcol = 0;
7724 int match = 0;
7725 list_T *l = NULL;
7726 listitem_T *li = NULL;
7727 long idx = 0;
7728 char_u *tofree = NULL;
7729
7730 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7731 save_cpo = p_cpo;
7732 p_cpo = (char_u *)"";
7733
7734 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007735 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007736 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007737 /* type MATCH_LIST: return empty list when there are no matches.
7738 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007739 if (rettv_list_alloc(rettv) == FAIL)
7740 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007741 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007742 && (list_append_string(rettv->vval.v_list,
7743 (char_u *)"", 0) == FAIL
7744 || list_append_number(rettv->vval.v_list,
7745 (varnumber_T)-1) == FAIL
7746 || list_append_number(rettv->vval.v_list,
7747 (varnumber_T)-1) == FAIL
7748 || list_append_number(rettv->vval.v_list,
7749 (varnumber_T)-1) == FAIL))
7750 {
7751 list_free(rettv->vval.v_list);
7752 rettv->vval.v_list = NULL;
7753 goto theend;
7754 }
7755 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007756 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007757 {
7758 rettv->v_type = VAR_STRING;
7759 rettv->vval.v_string = NULL;
7760 }
7761
7762 if (argvars[0].v_type == VAR_LIST)
7763 {
7764 if ((l = argvars[0].vval.v_list) == NULL)
7765 goto theend;
7766 li = l->lv_first;
7767 }
7768 else
7769 {
7770 expr = str = get_tv_string(&argvars[0]);
7771 len = (long)STRLEN(str);
7772 }
7773
7774 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
7775 if (pat == NULL)
7776 goto theend;
7777
7778 if (argvars[2].v_type != VAR_UNKNOWN)
7779 {
7780 int error = FALSE;
7781
7782 start = (long)get_tv_number_chk(&argvars[2], &error);
7783 if (error)
7784 goto theend;
7785 if (l != NULL)
7786 {
7787 li = list_find(l, start);
7788 if (li == NULL)
7789 goto theend;
7790 idx = l->lv_idx; /* use the cached index */
7791 }
7792 else
7793 {
7794 if (start < 0)
7795 start = 0;
7796 if (start > len)
7797 goto theend;
7798 /* When "count" argument is there ignore matches before "start",
7799 * otherwise skip part of the string. Differs when pattern is "^"
7800 * or "\<". */
7801 if (argvars[3].v_type != VAR_UNKNOWN)
7802 startcol = start;
7803 else
7804 {
7805 str += start;
7806 len -= start;
7807 }
7808 }
7809
7810 if (argvars[3].v_type != VAR_UNKNOWN)
7811 nth = (long)get_tv_number_chk(&argvars[3], &error);
7812 if (error)
7813 goto theend;
7814 }
7815
7816 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7817 if (regmatch.regprog != NULL)
7818 {
7819 regmatch.rm_ic = p_ic;
7820
7821 for (;;)
7822 {
7823 if (l != NULL)
7824 {
7825 if (li == NULL)
7826 {
7827 match = FALSE;
7828 break;
7829 }
7830 vim_free(tofree);
7831 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
7832 if (str == NULL)
7833 break;
7834 }
7835
7836 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
7837
7838 if (match && --nth <= 0)
7839 break;
7840 if (l == NULL && !match)
7841 break;
7842
7843 /* Advance to just after the match. */
7844 if (l != NULL)
7845 {
7846 li = li->li_next;
7847 ++idx;
7848 }
7849 else
7850 {
7851#ifdef FEAT_MBYTE
7852 startcol = (colnr_T)(regmatch.startp[0]
7853 + (*mb_ptr2len)(regmatch.startp[0]) - str);
7854#else
7855 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
7856#endif
7857 if (startcol > (colnr_T)len
7858 || str + startcol <= regmatch.startp[0])
7859 {
7860 match = FALSE;
7861 break;
7862 }
7863 }
7864 }
7865
7866 if (match)
7867 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007868 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007869 {
7870 listitem_T *li1 = rettv->vval.v_list->lv_first;
7871 listitem_T *li2 = li1->li_next;
7872 listitem_T *li3 = li2->li_next;
7873 listitem_T *li4 = li3->li_next;
7874
7875 vim_free(li1->li_tv.vval.v_string);
7876 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
7877 (int)(regmatch.endp[0] - regmatch.startp[0]));
7878 li3->li_tv.vval.v_number =
7879 (varnumber_T)(regmatch.startp[0] - expr);
7880 li4->li_tv.vval.v_number =
7881 (varnumber_T)(regmatch.endp[0] - expr);
7882 if (l != NULL)
7883 li2->li_tv.vval.v_number = (varnumber_T)idx;
7884 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007885 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007886 {
7887 int i;
7888
7889 /* return list with matched string and submatches */
7890 for (i = 0; i < NSUBEXP; ++i)
7891 {
7892 if (regmatch.endp[i] == NULL)
7893 {
7894 if (list_append_string(rettv->vval.v_list,
7895 (char_u *)"", 0) == FAIL)
7896 break;
7897 }
7898 else if (list_append_string(rettv->vval.v_list,
7899 regmatch.startp[i],
7900 (int)(regmatch.endp[i] - regmatch.startp[i]))
7901 == FAIL)
7902 break;
7903 }
7904 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007905 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007906 {
7907 /* return matched string */
7908 if (l != NULL)
7909 copy_tv(&li->li_tv, rettv);
7910 else
7911 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
7912 (int)(regmatch.endp[0] - regmatch.startp[0]));
7913 }
7914 else if (l != NULL)
7915 rettv->vval.v_number = idx;
7916 else
7917 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007918 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007919 rettv->vval.v_number =
7920 (varnumber_T)(regmatch.startp[0] - str);
7921 else
7922 rettv->vval.v_number =
7923 (varnumber_T)(regmatch.endp[0] - str);
7924 rettv->vval.v_number += (varnumber_T)(str - expr);
7925 }
7926 }
7927 vim_regfree(regmatch.regprog);
7928 }
7929
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007930theend:
7931 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007932 /* matchstrpos() without a list: drop the second item. */
7933 listitem_remove(rettv->vval.v_list,
7934 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007935 vim_free(tofree);
7936 p_cpo = save_cpo;
7937}
7938
7939/*
7940 * "match()" function
7941 */
7942 static void
7943f_match(typval_T *argvars, typval_T *rettv)
7944{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007945 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007946}
7947
Bram Moolenaar95e51472018-07-28 16:55:56 +02007948#ifdef FEAT_SEARCH_EXTRA
7949 static int
7950matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
7951{
7952 dictitem_T *di;
7953
7954 if (tv->v_type != VAR_DICT)
7955 {
7956 EMSG(_(e_dictreq));
7957 return FAIL;
7958 }
7959
7960 if (dict_find(tv->vval.v_dict, (char_u *)"conceal", -1) != NULL)
7961 *conceal_char = get_dict_string(tv->vval.v_dict,
7962 (char_u *)"conceal", FALSE);
7963
7964 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
7965 {
7966 *win = find_win_by_nr(&di->di_tv, NULL);
7967 if (*win == NULL)
7968 {
7969 EMSG(_("E957: Invalid window number"));
7970 return FAIL;
7971 }
7972 }
7973
7974 return OK;
7975}
7976#endif
7977
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007978/*
7979 * "matchadd()" function
7980 */
7981 static void
7982f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7983{
7984#ifdef FEAT_SEARCH_EXTRA
7985 char_u buf[NUMBUFLEN];
7986 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
7987 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
7988 int prio = 10; /* default priority */
7989 int id = -1;
7990 int error = FALSE;
7991 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02007992 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007993
7994 rettv->vval.v_number = -1;
7995
7996 if (grp == NULL || pat == NULL)
7997 return;
7998 if (argvars[2].v_type != VAR_UNKNOWN)
7999 {
8000 prio = (int)get_tv_number_chk(&argvars[2], &error);
8001 if (argvars[3].v_type != VAR_UNKNOWN)
8002 {
8003 id = (int)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008004 if (argvars[4].v_type != VAR_UNKNOWN
8005 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8006 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008007 }
8008 }
8009 if (error == TRUE)
8010 return;
8011 if (id >= 1 && id <= 3)
8012 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02008013 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008014 return;
8015 }
8016
Bram Moolenaar95e51472018-07-28 16:55:56 +02008017 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008018 conceal_char);
8019#endif
8020}
8021
8022/*
8023 * "matchaddpos()" function
8024 */
8025 static void
8026f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8027{
8028#ifdef FEAT_SEARCH_EXTRA
8029 char_u buf[NUMBUFLEN];
8030 char_u *group;
8031 int prio = 10;
8032 int id = -1;
8033 int error = FALSE;
8034 list_T *l;
8035 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008036 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008037
8038 rettv->vval.v_number = -1;
8039
8040 group = get_tv_string_buf_chk(&argvars[0], buf);
8041 if (group == NULL)
8042 return;
8043
8044 if (argvars[1].v_type != VAR_LIST)
8045 {
8046 EMSG2(_(e_listarg), "matchaddpos()");
8047 return;
8048 }
8049 l = argvars[1].vval.v_list;
8050 if (l == NULL)
8051 return;
8052
8053 if (argvars[2].v_type != VAR_UNKNOWN)
8054 {
8055 prio = (int)get_tv_number_chk(&argvars[2], &error);
8056 if (argvars[3].v_type != VAR_UNKNOWN)
8057 {
8058 id = (int)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008059
8060 if (argvars[4].v_type != VAR_UNKNOWN
8061 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8062 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008063 }
8064 }
8065 if (error == TRUE)
8066 return;
8067
8068 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8069 if (id == 1 || id == 2)
8070 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02008071 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008072 return;
8073 }
8074
Bram Moolenaar95e51472018-07-28 16:55:56 +02008075 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008076 conceal_char);
8077#endif
8078}
8079
8080/*
8081 * "matcharg()" function
8082 */
8083 static void
8084f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8085{
8086 if (rettv_list_alloc(rettv) == OK)
8087 {
8088#ifdef FEAT_SEARCH_EXTRA
8089 int id = (int)get_tv_number(&argvars[0]);
8090 matchitem_T *m;
8091
8092 if (id >= 1 && id <= 3)
8093 {
8094 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8095 {
8096 list_append_string(rettv->vval.v_list,
8097 syn_id2name(m->hlg_id), -1);
8098 list_append_string(rettv->vval.v_list, m->pattern, -1);
8099 }
8100 else
8101 {
8102 list_append_string(rettv->vval.v_list, NULL, -1);
8103 list_append_string(rettv->vval.v_list, NULL, -1);
8104 }
8105 }
8106#endif
8107 }
8108}
8109
8110/*
8111 * "matchdelete()" function
8112 */
8113 static void
8114f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8115{
8116#ifdef FEAT_SEARCH_EXTRA
8117 rettv->vval.v_number = match_delete(curwin,
8118 (int)get_tv_number(&argvars[0]), TRUE);
8119#endif
8120}
8121
8122/*
8123 * "matchend()" function
8124 */
8125 static void
8126f_matchend(typval_T *argvars, typval_T *rettv)
8127{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008128 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008129}
8130
8131/*
8132 * "matchlist()" function
8133 */
8134 static void
8135f_matchlist(typval_T *argvars, typval_T *rettv)
8136{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008137 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008138}
8139
8140/*
8141 * "matchstr()" function
8142 */
8143 static void
8144f_matchstr(typval_T *argvars, typval_T *rettv)
8145{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008146 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008147}
8148
8149/*
8150 * "matchstrpos()" function
8151 */
8152 static void
8153f_matchstrpos(typval_T *argvars, typval_T *rettv)
8154{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008155 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008156}
8157
8158static void max_min(typval_T *argvars, typval_T *rettv, int domax);
8159
8160 static void
8161max_min(typval_T *argvars, typval_T *rettv, int domax)
8162{
8163 varnumber_T n = 0;
8164 varnumber_T i;
8165 int error = FALSE;
8166
8167 if (argvars[0].v_type == VAR_LIST)
8168 {
8169 list_T *l;
8170 listitem_T *li;
8171
8172 l = argvars[0].vval.v_list;
8173 if (l != NULL)
8174 {
8175 li = l->lv_first;
8176 if (li != NULL)
8177 {
8178 n = get_tv_number_chk(&li->li_tv, &error);
8179 for (;;)
8180 {
8181 li = li->li_next;
8182 if (li == NULL)
8183 break;
8184 i = get_tv_number_chk(&li->li_tv, &error);
8185 if (domax ? i > n : i < n)
8186 n = i;
8187 }
8188 }
8189 }
8190 }
8191 else if (argvars[0].v_type == VAR_DICT)
8192 {
8193 dict_T *d;
8194 int first = TRUE;
8195 hashitem_T *hi;
8196 int todo;
8197
8198 d = argvars[0].vval.v_dict;
8199 if (d != NULL)
8200 {
8201 todo = (int)d->dv_hashtab.ht_used;
8202 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8203 {
8204 if (!HASHITEM_EMPTY(hi))
8205 {
8206 --todo;
8207 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
8208 if (first)
8209 {
8210 n = i;
8211 first = FALSE;
8212 }
8213 else if (domax ? i > n : i < n)
8214 n = i;
8215 }
8216 }
8217 }
8218 }
8219 else
Bram Moolenaar26b84332016-09-04 21:42:36 +02008220 EMSG2(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008221 rettv->vval.v_number = error ? 0 : n;
8222}
8223
8224/*
8225 * "max()" function
8226 */
8227 static void
8228f_max(typval_T *argvars, typval_T *rettv)
8229{
8230 max_min(argvars, rettv, TRUE);
8231}
8232
8233/*
8234 * "min()" function
8235 */
8236 static void
8237f_min(typval_T *argvars, typval_T *rettv)
8238{
8239 max_min(argvars, rettv, FALSE);
8240}
8241
8242static int mkdir_recurse(char_u *dir, int prot);
8243
8244/*
8245 * Create the directory in which "dir" is located, and higher levels when
8246 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008247 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008248 */
8249 static int
8250mkdir_recurse(char_u *dir, int prot)
8251{
8252 char_u *p;
8253 char_u *updir;
8254 int r = FAIL;
8255
8256 /* Get end of directory name in "dir".
8257 * We're done when it's "/" or "c:/". */
8258 p = gettail_sep(dir);
8259 if (p <= get_past_head(dir))
8260 return OK;
8261
8262 /* If the directory exists we're done. Otherwise: create it.*/
8263 updir = vim_strnsave(dir, (int)(p - dir));
8264 if (updir == NULL)
8265 return FAIL;
8266 if (mch_isdir(updir))
8267 r = OK;
8268 else if (mkdir_recurse(updir, prot) == OK)
8269 r = vim_mkdir_emsg(updir, prot);
8270 vim_free(updir);
8271 return r;
8272}
8273
8274#ifdef vim_mkdir
8275/*
8276 * "mkdir()" function
8277 */
8278 static void
8279f_mkdir(typval_T *argvars, typval_T *rettv)
8280{
8281 char_u *dir;
8282 char_u buf[NUMBUFLEN];
8283 int prot = 0755;
8284
8285 rettv->vval.v_number = FAIL;
8286 if (check_restricted() || check_secure())
8287 return;
8288
8289 dir = get_tv_string_buf(&argvars[0], buf);
8290 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008291 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008292
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008293 if (*gettail(dir) == NUL)
8294 /* remove trailing slashes */
8295 *gettail_sep(dir) = NUL;
8296
8297 if (argvars[1].v_type != VAR_UNKNOWN)
8298 {
8299 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008300 {
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008301 prot = (int)get_tv_number_chk(&argvars[2], NULL);
8302 if (prot == -1)
8303 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008304 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008305 if (STRCMP(get_tv_string(&argvars[1]), "p") == 0)
8306 {
8307 if (mch_isdir(dir))
8308 {
8309 /* With the "p" flag it's OK if the dir already exists. */
8310 rettv->vval.v_number = OK;
8311 return;
8312 }
8313 mkdir_recurse(dir, prot);
8314 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008315 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008316 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008317}
8318#endif
8319
8320/*
8321 * "mode()" function
8322 */
8323 static void
8324f_mode(typval_T *argvars, typval_T *rettv)
8325{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008326 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008327
Bram Moolenaar612cc382018-07-29 15:34:26 +02008328 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008329
8330 if (time_for_testing == 93784)
8331 {
8332 /* Testing the two-character code. */
8333 buf[0] = 'x';
8334 buf[1] = '!';
8335 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008336#ifdef FEAT_TERMINAL
8337 else if (term_use_loop())
8338 buf[0] = 't';
8339#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008340 else if (VIsual_active)
8341 {
8342 if (VIsual_select)
8343 buf[0] = VIsual_mode + 's' - 'v';
8344 else
8345 buf[0] = VIsual_mode;
8346 }
8347 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8348 || State == CONFIRM)
8349 {
8350 buf[0] = 'r';
8351 if (State == ASKMORE)
8352 buf[1] = 'm';
8353 else if (State == CONFIRM)
8354 buf[1] = '?';
8355 }
8356 else if (State == EXTERNCMD)
8357 buf[0] = '!';
8358 else if (State & INSERT)
8359 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008360 if (State & VREPLACE_FLAG)
8361 {
8362 buf[0] = 'R';
8363 buf[1] = 'v';
8364 }
8365 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008366 {
8367 if (State & REPLACE_FLAG)
8368 buf[0] = 'R';
8369 else
8370 buf[0] = 'i';
8371#ifdef FEAT_INS_EXPAND
8372 if (ins_compl_active())
8373 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008374 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008375 buf[1] = 'x';
8376#endif
8377 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008378 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008379 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008380 {
8381 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008382 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008383 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008384 else if (exmode_active == EXMODE_NORMAL)
8385 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008386 }
8387 else
8388 {
8389 buf[0] = 'n';
8390 if (finish_op)
8391 buf[1] = 'o';
Bram Moolenaar612cc382018-07-29 15:34:26 +02008392 else if (restart_edit == 'I' || restart_edit == 'R'
8393 || restart_edit == 'V')
8394 {
8395 buf[1] = 'i';
8396 buf[2] = restart_edit;
8397 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008398 }
8399
8400 /* Clear out the minor mode when the argument is not a non-zero number or
8401 * non-empty string. */
8402 if (!non_zero_arg(&argvars[0]))
8403 buf[1] = NUL;
8404
8405 rettv->vval.v_string = vim_strsave(buf);
8406 rettv->v_type = VAR_STRING;
8407}
8408
8409#if defined(FEAT_MZSCHEME) || defined(PROTO)
8410/*
8411 * "mzeval()" function
8412 */
8413 static void
8414f_mzeval(typval_T *argvars, typval_T *rettv)
8415{
8416 char_u *str;
8417 char_u buf[NUMBUFLEN];
8418
8419 str = get_tv_string_buf(&argvars[0], buf);
8420 do_mzeval(str, rettv);
8421}
8422
8423 void
8424mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8425{
8426 typval_T argvars[3];
8427
8428 argvars[0].v_type = VAR_STRING;
8429 argvars[0].vval.v_string = name;
8430 copy_tv(args, &argvars[1]);
8431 argvars[2].v_type = VAR_UNKNOWN;
8432 f_call(argvars, rettv);
8433 clear_tv(&argvars[1]);
8434}
8435#endif
8436
8437/*
8438 * "nextnonblank()" function
8439 */
8440 static void
8441f_nextnonblank(typval_T *argvars, typval_T *rettv)
8442{
8443 linenr_T lnum;
8444
8445 for (lnum = get_tv_lnum(argvars); ; ++lnum)
8446 {
8447 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8448 {
8449 lnum = 0;
8450 break;
8451 }
8452 if (*skipwhite(ml_get(lnum)) != NUL)
8453 break;
8454 }
8455 rettv->vval.v_number = lnum;
8456}
8457
8458/*
8459 * "nr2char()" function
8460 */
8461 static void
8462f_nr2char(typval_T *argvars, typval_T *rettv)
8463{
8464 char_u buf[NUMBUFLEN];
8465
8466#ifdef FEAT_MBYTE
8467 if (has_mbyte)
8468 {
8469 int utf8 = 0;
8470
8471 if (argvars[1].v_type != VAR_UNKNOWN)
8472 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
8473 if (utf8)
8474 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8475 else
8476 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8477 }
8478 else
8479#endif
8480 {
8481 buf[0] = (char_u)get_tv_number(&argvars[0]);
8482 buf[1] = NUL;
8483 }
8484 rettv->v_type = VAR_STRING;
8485 rettv->vval.v_string = vim_strsave(buf);
8486}
8487
8488/*
8489 * "or(expr, expr)" function
8490 */
8491 static void
8492f_or(typval_T *argvars, typval_T *rettv)
8493{
8494 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8495 | get_tv_number_chk(&argvars[1], NULL);
8496}
8497
8498/*
8499 * "pathshorten()" function
8500 */
8501 static void
8502f_pathshorten(typval_T *argvars, typval_T *rettv)
8503{
8504 char_u *p;
8505
8506 rettv->v_type = VAR_STRING;
8507 p = get_tv_string_chk(&argvars[0]);
8508 if (p == NULL)
8509 rettv->vval.v_string = NULL;
8510 else
8511 {
8512 p = vim_strsave(p);
8513 rettv->vval.v_string = p;
8514 if (p != NULL)
8515 shorten_dir(p);
8516 }
8517}
8518
8519#ifdef FEAT_PERL
8520/*
8521 * "perleval()" function
8522 */
8523 static void
8524f_perleval(typval_T *argvars, typval_T *rettv)
8525{
8526 char_u *str;
8527 char_u buf[NUMBUFLEN];
8528
8529 str = get_tv_string_buf(&argvars[0], buf);
8530 do_perleval(str, rettv);
8531}
8532#endif
8533
8534#ifdef FEAT_FLOAT
8535/*
8536 * "pow()" function
8537 */
8538 static void
8539f_pow(typval_T *argvars, typval_T *rettv)
8540{
8541 float_T fx = 0.0, fy = 0.0;
8542
8543 rettv->v_type = VAR_FLOAT;
8544 if (get_float_arg(argvars, &fx) == OK
8545 && get_float_arg(&argvars[1], &fy) == OK)
8546 rettv->vval.v_float = pow(fx, fy);
8547 else
8548 rettv->vval.v_float = 0.0;
8549}
8550#endif
8551
8552/*
8553 * "prevnonblank()" function
8554 */
8555 static void
8556f_prevnonblank(typval_T *argvars, typval_T *rettv)
8557{
8558 linenr_T lnum;
8559
8560 lnum = get_tv_lnum(argvars);
8561 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8562 lnum = 0;
8563 else
8564 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8565 --lnum;
8566 rettv->vval.v_number = lnum;
8567}
8568
8569/* This dummy va_list is here because:
8570 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8571 * - locally in the function results in a "used before set" warning
8572 * - using va_start() to initialize it gives "function with fixed args" error */
8573static va_list ap;
8574
8575/*
8576 * "printf()" function
8577 */
8578 static void
8579f_printf(typval_T *argvars, typval_T *rettv)
8580{
8581 char_u buf[NUMBUFLEN];
8582 int len;
8583 char_u *s;
8584 int saved_did_emsg = did_emsg;
8585 char *fmt;
8586
8587 rettv->v_type = VAR_STRING;
8588 rettv->vval.v_string = NULL;
8589
8590 /* Get the required length, allocate the buffer and do it for real. */
8591 did_emsg = FALSE;
8592 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008593 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008594 if (!did_emsg)
8595 {
8596 s = alloc(len + 1);
8597 if (s != NULL)
8598 {
8599 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008600 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8601 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008602 }
8603 }
8604 did_emsg |= saved_did_emsg;
8605}
8606
Bram Moolenaarf2732452018-06-03 14:47:35 +02008607#ifdef FEAT_JOB_CHANNEL
8608/*
8609 * "prompt_setcallback({buffer}, {callback})" function
8610 */
8611 static void
8612f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
8613{
8614 buf_T *buf;
8615 char_u *callback;
8616 partial_T *partial;
8617
8618 if (check_secure())
8619 return;
8620 buf = get_buf_tv(&argvars[0], FALSE);
8621 if (buf == NULL)
8622 return;
8623
8624 callback = get_callback(&argvars[1], &partial);
8625 if (callback == NULL)
8626 return;
8627
8628 free_callback(buf->b_prompt_callback, buf->b_prompt_partial);
8629 if (partial == NULL)
8630 buf->b_prompt_callback = vim_strsave(callback);
8631 else
8632 /* pointer into the partial */
8633 buf->b_prompt_callback = callback;
8634 buf->b_prompt_partial = partial;
8635}
8636
8637/*
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02008638 * "prompt_setinterrupt({buffer}, {callback})" function
8639 */
8640 static void
8641f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
8642{
8643 buf_T *buf;
8644 char_u *callback;
8645 partial_T *partial;
8646
8647 if (check_secure())
8648 return;
8649 buf = get_buf_tv(&argvars[0], FALSE);
8650 if (buf == NULL)
8651 return;
8652
8653 callback = get_callback(&argvars[1], &partial);
8654 if (callback == NULL)
8655 return;
8656
8657 free_callback(buf->b_prompt_interrupt, buf->b_prompt_int_partial);
8658 if (partial == NULL)
8659 buf->b_prompt_interrupt = vim_strsave(callback);
8660 else
8661 /* pointer into the partial */
8662 buf->b_prompt_interrupt = callback;
8663 buf->b_prompt_int_partial = partial;
8664}
8665
8666/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02008667 * "prompt_setprompt({buffer}, {text})" function
8668 */
8669 static void
8670f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
8671{
8672 buf_T *buf;
8673 char_u *text;
8674
8675 if (check_secure())
8676 return;
8677 buf = get_buf_tv(&argvars[0], FALSE);
8678 if (buf == NULL)
8679 return;
8680
8681 text = get_tv_string(&argvars[1]);
8682 vim_free(buf->b_prompt_text);
8683 buf->b_prompt_text = vim_strsave(text);
8684}
8685#endif
8686
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008687/*
8688 * "pumvisible()" function
8689 */
8690 static void
8691f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8692{
8693#ifdef FEAT_INS_EXPAND
8694 if (pum_visible())
8695 rettv->vval.v_number = 1;
8696#endif
8697}
8698
8699#ifdef FEAT_PYTHON3
8700/*
8701 * "py3eval()" function
8702 */
8703 static void
8704f_py3eval(typval_T *argvars, typval_T *rettv)
8705{
8706 char_u *str;
8707 char_u buf[NUMBUFLEN];
8708
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008709 if (p_pyx == 0)
8710 p_pyx = 3;
8711
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008712 str = get_tv_string_buf(&argvars[0], buf);
8713 do_py3eval(str, rettv);
8714}
8715#endif
8716
8717#ifdef FEAT_PYTHON
8718/*
8719 * "pyeval()" function
8720 */
8721 static void
8722f_pyeval(typval_T *argvars, typval_T *rettv)
8723{
8724 char_u *str;
8725 char_u buf[NUMBUFLEN];
8726
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008727 if (p_pyx == 0)
8728 p_pyx = 2;
8729
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008730 str = get_tv_string_buf(&argvars[0], buf);
8731 do_pyeval(str, rettv);
8732}
8733#endif
8734
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008735#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8736/*
8737 * "pyxeval()" function
8738 */
8739 static void
8740f_pyxeval(typval_T *argvars, typval_T *rettv)
8741{
8742# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8743 init_pyxversion();
8744 if (p_pyx == 2)
8745 f_pyeval(argvars, rettv);
8746 else
8747 f_py3eval(argvars, rettv);
8748# elif defined(FEAT_PYTHON)
8749 f_pyeval(argvars, rettv);
8750# elif defined(FEAT_PYTHON3)
8751 f_py3eval(argvars, rettv);
8752# endif
8753}
8754#endif
8755
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008756/*
8757 * "range()" function
8758 */
8759 static void
8760f_range(typval_T *argvars, typval_T *rettv)
8761{
8762 varnumber_T start;
8763 varnumber_T end;
8764 varnumber_T stride = 1;
8765 varnumber_T i;
8766 int error = FALSE;
8767
8768 start = get_tv_number_chk(&argvars[0], &error);
8769 if (argvars[1].v_type == VAR_UNKNOWN)
8770 {
8771 end = start - 1;
8772 start = 0;
8773 }
8774 else
8775 {
8776 end = get_tv_number_chk(&argvars[1], &error);
8777 if (argvars[2].v_type != VAR_UNKNOWN)
8778 stride = get_tv_number_chk(&argvars[2], &error);
8779 }
8780
8781 if (error)
8782 return; /* type error; errmsg already given */
8783 if (stride == 0)
8784 EMSG(_("E726: Stride is zero"));
8785 else if (stride > 0 ? end + 1 < start : end - 1 > start)
8786 EMSG(_("E727: Start past end"));
8787 else
8788 {
8789 if (rettv_list_alloc(rettv) == OK)
8790 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8791 if (list_append_number(rettv->vval.v_list,
8792 (varnumber_T)i) == FAIL)
8793 break;
8794 }
8795}
8796
8797/*
8798 * "readfile()" function
8799 */
8800 static void
8801f_readfile(typval_T *argvars, typval_T *rettv)
8802{
8803 int binary = FALSE;
8804 int failed = FALSE;
8805 char_u *fname;
8806 FILE *fd;
8807 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
8808 int io_size = sizeof(buf);
8809 int readlen; /* size of last fread() */
8810 char_u *prev = NULL; /* previously read bytes, if any */
8811 long prevlen = 0; /* length of data in prev */
8812 long prevsize = 0; /* size of prev buffer */
8813 long maxline = MAXLNUM;
8814 long cnt = 0;
8815 char_u *p; /* position in buf */
8816 char_u *start; /* start of current line */
8817
8818 if (argvars[1].v_type != VAR_UNKNOWN)
8819 {
8820 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
8821 binary = TRUE;
8822 if (argvars[2].v_type != VAR_UNKNOWN)
8823 maxline = (long)get_tv_number(&argvars[2]);
8824 }
8825
8826 if (rettv_list_alloc(rettv) == FAIL)
8827 return;
8828
8829 /* Always open the file in binary mode, library functions have a mind of
8830 * their own about CR-LF conversion. */
8831 fname = get_tv_string(&argvars[0]);
8832 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
8833 {
8834 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
8835 return;
8836 }
8837
8838 while (cnt < maxline || maxline < 0)
8839 {
8840 readlen = (int)fread(buf, 1, io_size, fd);
8841
8842 /* This for loop processes what was read, but is also entered at end
8843 * of file so that either:
8844 * - an incomplete line gets written
8845 * - a "binary" file gets an empty line at the end if it ends in a
8846 * newline. */
8847 for (p = buf, start = buf;
8848 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
8849 ++p)
8850 {
8851 if (*p == '\n' || readlen <= 0)
8852 {
8853 listitem_T *li;
8854 char_u *s = NULL;
8855 long_u len = p - start;
8856
8857 /* Finished a line. Remove CRs before NL. */
8858 if (readlen > 0 && !binary)
8859 {
8860 while (len > 0 && start[len - 1] == '\r')
8861 --len;
8862 /* removal may cross back to the "prev" string */
8863 if (len == 0)
8864 while (prevlen > 0 && prev[prevlen - 1] == '\r')
8865 --prevlen;
8866 }
8867 if (prevlen == 0)
8868 s = vim_strnsave(start, (int)len);
8869 else
8870 {
8871 /* Change "prev" buffer to be the right size. This way
8872 * the bytes are only copied once, and very long lines are
8873 * allocated only once. */
8874 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
8875 {
8876 mch_memmove(s + prevlen, start, len);
8877 s[prevlen + len] = NUL;
8878 prev = NULL; /* the list will own the string */
8879 prevlen = prevsize = 0;
8880 }
8881 }
8882 if (s == NULL)
8883 {
8884 do_outofmem_msg((long_u) prevlen + len + 1);
8885 failed = TRUE;
8886 break;
8887 }
8888
8889 if ((li = listitem_alloc()) == NULL)
8890 {
8891 vim_free(s);
8892 failed = TRUE;
8893 break;
8894 }
8895 li->li_tv.v_type = VAR_STRING;
8896 li->li_tv.v_lock = 0;
8897 li->li_tv.vval.v_string = s;
8898 list_append(rettv->vval.v_list, li);
8899
8900 start = p + 1; /* step over newline */
8901 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
8902 break;
8903 }
8904 else if (*p == NUL)
8905 *p = '\n';
8906#ifdef FEAT_MBYTE
8907 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
8908 * when finding the BF and check the previous two bytes. */
8909 else if (*p == 0xbf && enc_utf8 && !binary)
8910 {
8911 /* Find the two bytes before the 0xbf. If p is at buf, or buf
8912 * + 1, these may be in the "prev" string. */
8913 char_u back1 = p >= buf + 1 ? p[-1]
8914 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
8915 char_u back2 = p >= buf + 2 ? p[-2]
8916 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
8917 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
8918
8919 if (back2 == 0xef && back1 == 0xbb)
8920 {
8921 char_u *dest = p - 2;
8922
8923 /* Usually a BOM is at the beginning of a file, and so at
8924 * the beginning of a line; then we can just step over it.
8925 */
8926 if (start == dest)
8927 start = p + 1;
8928 else
8929 {
8930 /* have to shuffle buf to close gap */
8931 int adjust_prevlen = 0;
8932
8933 if (dest < buf)
8934 {
8935 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
8936 dest = buf;
8937 }
8938 if (readlen > p - buf + 1)
8939 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
8940 readlen -= 3 - adjust_prevlen;
8941 prevlen -= adjust_prevlen;
8942 p = dest - 1;
8943 }
8944 }
8945 }
8946#endif
8947 } /* for */
8948
8949 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
8950 break;
8951 if (start < p)
8952 {
8953 /* There's part of a line in buf, store it in "prev". */
8954 if (p - start + prevlen >= prevsize)
8955 {
8956 /* need bigger "prev" buffer */
8957 char_u *newprev;
8958
8959 /* A common use case is ordinary text files and "prev" gets a
8960 * fragment of a line, so the first allocation is made
8961 * small, to avoid repeatedly 'allocing' large and
8962 * 'reallocing' small. */
8963 if (prevsize == 0)
8964 prevsize = (long)(p - start);
8965 else
8966 {
8967 long grow50pc = (prevsize * 3) / 2;
8968 long growmin = (long)((p - start) * 2 + prevlen);
8969 prevsize = grow50pc > growmin ? grow50pc : growmin;
8970 }
8971 newprev = prev == NULL ? alloc(prevsize)
8972 : vim_realloc(prev, prevsize);
8973 if (newprev == NULL)
8974 {
8975 do_outofmem_msg((long_u)prevsize);
8976 failed = TRUE;
8977 break;
8978 }
8979 prev = newprev;
8980 }
8981 /* Add the line part to end of "prev". */
8982 mch_memmove(prev + prevlen, start, p - start);
8983 prevlen += (long)(p - start);
8984 }
8985 } /* while */
8986
8987 /*
8988 * For a negative line count use only the lines at the end of the file,
8989 * free the rest.
8990 */
8991 if (!failed && maxline < 0)
8992 while (cnt > -maxline)
8993 {
8994 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
8995 --cnt;
8996 }
8997
8998 if (failed)
8999 {
9000 list_free(rettv->vval.v_list);
9001 /* readfile doc says an empty list is returned on error */
9002 rettv->vval.v_list = list_alloc();
9003 }
9004
9005 vim_free(prev);
9006 fclose(fd);
9007}
9008
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009009 static void
9010return_register(int regname, typval_T *rettv)
9011{
9012 char_u buf[2] = {0, 0};
9013
9014 buf[0] = (char_u)regname;
9015 rettv->v_type = VAR_STRING;
9016 rettv->vval.v_string = vim_strsave(buf);
9017}
9018
9019/*
9020 * "reg_executing()" function
9021 */
9022 static void
9023f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9024{
9025 return_register(reg_executing, rettv);
9026}
9027
9028/*
9029 * "reg_recording()" function
9030 */
9031 static void
9032f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9033{
9034 return_register(reg_recording, rettv);
9035}
9036
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009037#if defined(FEAT_RELTIME)
9038static int list2proftime(typval_T *arg, proftime_T *tm);
9039
9040/*
9041 * Convert a List to proftime_T.
9042 * Return FAIL when there is something wrong.
9043 */
9044 static int
9045list2proftime(typval_T *arg, proftime_T *tm)
9046{
9047 long n1, n2;
9048 int error = FALSE;
9049
9050 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9051 || arg->vval.v_list->lv_len != 2)
9052 return FAIL;
9053 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9054 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
9055# ifdef WIN3264
9056 tm->HighPart = n1;
9057 tm->LowPart = n2;
9058# else
9059 tm->tv_sec = n1;
9060 tm->tv_usec = n2;
9061# endif
9062 return error ? FAIL : OK;
9063}
9064#endif /* FEAT_RELTIME */
9065
9066/*
9067 * "reltime()" function
9068 */
9069 static void
9070f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9071{
9072#ifdef FEAT_RELTIME
9073 proftime_T res;
9074 proftime_T start;
9075
9076 if (argvars[0].v_type == VAR_UNKNOWN)
9077 {
9078 /* No arguments: get current time. */
9079 profile_start(&res);
9080 }
9081 else if (argvars[1].v_type == VAR_UNKNOWN)
9082 {
9083 if (list2proftime(&argvars[0], &res) == FAIL)
9084 return;
9085 profile_end(&res);
9086 }
9087 else
9088 {
9089 /* Two arguments: compute the difference. */
9090 if (list2proftime(&argvars[0], &start) == FAIL
9091 || list2proftime(&argvars[1], &res) == FAIL)
9092 return;
9093 profile_sub(&res, &start);
9094 }
9095
9096 if (rettv_list_alloc(rettv) == OK)
9097 {
9098 long n1, n2;
9099
9100# ifdef WIN3264
9101 n1 = res.HighPart;
9102 n2 = res.LowPart;
9103# else
9104 n1 = res.tv_sec;
9105 n2 = res.tv_usec;
9106# endif
9107 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9108 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9109 }
9110#endif
9111}
9112
9113#ifdef FEAT_FLOAT
9114/*
9115 * "reltimefloat()" function
9116 */
9117 static void
9118f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9119{
9120# ifdef FEAT_RELTIME
9121 proftime_T tm;
9122# endif
9123
9124 rettv->v_type = VAR_FLOAT;
9125 rettv->vval.v_float = 0;
9126# ifdef FEAT_RELTIME
9127 if (list2proftime(&argvars[0], &tm) == OK)
9128 rettv->vval.v_float = profile_float(&tm);
9129# endif
9130}
9131#endif
9132
9133/*
9134 * "reltimestr()" function
9135 */
9136 static void
9137f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9138{
9139#ifdef FEAT_RELTIME
9140 proftime_T tm;
9141#endif
9142
9143 rettv->v_type = VAR_STRING;
9144 rettv->vval.v_string = NULL;
9145#ifdef FEAT_RELTIME
9146 if (list2proftime(&argvars[0], &tm) == OK)
9147 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9148#endif
9149}
9150
9151#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
9152static void make_connection(void);
9153static int check_connection(void);
9154
9155 static void
9156make_connection(void)
9157{
9158 if (X_DISPLAY == NULL
9159# ifdef FEAT_GUI
9160 && !gui.in_use
9161# endif
9162 )
9163 {
9164 x_force_connect = TRUE;
9165 setup_term_clip();
9166 x_force_connect = FALSE;
9167 }
9168}
9169
9170 static int
9171check_connection(void)
9172{
9173 make_connection();
9174 if (X_DISPLAY == NULL)
9175 {
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009176 EMSG(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009177 return FAIL;
9178 }
9179 return OK;
9180}
9181#endif
9182
9183#ifdef FEAT_CLIENTSERVER
9184 static void
9185remote_common(typval_T *argvars, typval_T *rettv, int expr)
9186{
9187 char_u *server_name;
9188 char_u *keys;
9189 char_u *r = NULL;
9190 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009191 int timeout = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009192# ifdef WIN32
9193 HWND w;
9194# else
9195 Window w;
9196# endif
9197
9198 if (check_restricted() || check_secure())
9199 return;
9200
9201# ifdef FEAT_X11
9202 if (check_connection() == FAIL)
9203 return;
9204# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009205 if (argvars[2].v_type != VAR_UNKNOWN
9206 && argvars[3].v_type != VAR_UNKNOWN)
9207 timeout = get_tv_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009208
9209 server_name = get_tv_string_chk(&argvars[0]);
9210 if (server_name == NULL)
9211 return; /* type error; errmsg already given */
9212 keys = get_tv_string_buf(&argvars[1], buf);
9213# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009214 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009215# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009216 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9217 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009218# endif
9219 {
9220 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009221 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009222 EMSG(r); /* sending worked but evaluation failed */
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009223 vim_free(r);
9224 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009225 else
9226 EMSG2(_("E241: Unable to send to %s"), server_name);
9227 return;
9228 }
9229
9230 rettv->vval.v_string = r;
9231
9232 if (argvars[2].v_type != VAR_UNKNOWN)
9233 {
9234 dictitem_T v;
9235 char_u str[30];
9236 char_u *idvar;
9237
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009238 idvar = get_tv_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009239 if (idvar != NULL && *idvar != NUL)
9240 {
9241 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9242 v.di_tv.v_type = VAR_STRING;
9243 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009244 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009245 vim_free(v.di_tv.vval.v_string);
9246 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009247 }
9248}
9249#endif
9250
9251/*
9252 * "remote_expr()" function
9253 */
9254 static void
9255f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9256{
9257 rettv->v_type = VAR_STRING;
9258 rettv->vval.v_string = NULL;
9259#ifdef FEAT_CLIENTSERVER
9260 remote_common(argvars, rettv, TRUE);
9261#endif
9262}
9263
9264/*
9265 * "remote_foreground()" function
9266 */
9267 static void
9268f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9269{
9270#ifdef FEAT_CLIENTSERVER
9271# ifdef WIN32
9272 /* On Win32 it's done in this application. */
9273 {
9274 char_u *server_name = get_tv_string_chk(&argvars[0]);
9275
9276 if (server_name != NULL)
9277 serverForeground(server_name);
9278 }
9279# else
9280 /* Send a foreground() expression to the server. */
9281 argvars[1].v_type = VAR_STRING;
9282 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9283 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009284 rettv->v_type = VAR_STRING;
9285 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009286 remote_common(argvars, rettv, TRUE);
9287 vim_free(argvars[1].vval.v_string);
9288# endif
9289#endif
9290}
9291
9292 static void
9293f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
9294{
9295#ifdef FEAT_CLIENTSERVER
9296 dictitem_T v;
9297 char_u *s = NULL;
9298# ifdef WIN32
9299 long_u n = 0;
9300# endif
9301 char_u *serverid;
9302
9303 if (check_restricted() || check_secure())
9304 {
9305 rettv->vval.v_number = -1;
9306 return;
9307 }
9308 serverid = get_tv_string_chk(&argvars[0]);
9309 if (serverid == NULL)
9310 {
9311 rettv->vval.v_number = -1;
9312 return; /* type error; errmsg already given */
9313 }
9314# ifdef WIN32
9315 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
9316 if (n == 0)
9317 rettv->vval.v_number = -1;
9318 else
9319 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009320 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009321 rettv->vval.v_number = (s != NULL);
9322 }
9323# else
9324 if (check_connection() == FAIL)
9325 return;
9326
9327 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
9328 serverStrToWin(serverid), &s);
9329# endif
9330
9331 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
9332 {
9333 char_u *retvar;
9334
9335 v.di_tv.v_type = VAR_STRING;
9336 v.di_tv.vval.v_string = vim_strsave(s);
9337 retvar = get_tv_string_chk(&argvars[1]);
9338 if (retvar != NULL)
9339 set_var(retvar, &v.di_tv, FALSE);
9340 vim_free(v.di_tv.vval.v_string);
9341 }
9342#else
9343 rettv->vval.v_number = -1;
9344#endif
9345}
9346
9347 static void
9348f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
9349{
9350 char_u *r = NULL;
9351
9352#ifdef FEAT_CLIENTSERVER
9353 char_u *serverid = get_tv_string_chk(&argvars[0]);
9354
9355 if (serverid != NULL && !check_restricted() && !check_secure())
9356 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009357 int timeout = 0;
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009358# ifdef WIN32
9359 /* The server's HWND is encoded in the 'id' parameter */
9360 long_u n = 0;
9361# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009362
9363 if (argvars[1].v_type != VAR_UNKNOWN)
9364 timeout = get_tv_number(&argvars[1]);
9365
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009366# ifdef WIN32
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009367 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
9368 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009369 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009370 if (r == NULL)
9371# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009372 if (check_connection() == FAIL
9373 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
9374 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009375# endif
9376 EMSG(_("E277: Unable to read a server reply"));
9377 }
9378#endif
9379 rettv->v_type = VAR_STRING;
9380 rettv->vval.v_string = r;
9381}
9382
9383/*
9384 * "remote_send()" function
9385 */
9386 static void
9387f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9388{
9389 rettv->v_type = VAR_STRING;
9390 rettv->vval.v_string = NULL;
9391#ifdef FEAT_CLIENTSERVER
9392 remote_common(argvars, rettv, FALSE);
9393#endif
9394}
9395
9396/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009397 * "remote_startserver()" function
9398 */
9399 static void
9400f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9401{
9402#ifdef FEAT_CLIENTSERVER
9403 char_u *server = get_tv_string_chk(&argvars[0]);
9404
9405 if (server == NULL)
9406 return; /* type error; errmsg already given */
9407 if (serverName != NULL)
9408 EMSG(_("E941: already started a server"));
9409 else
9410 {
9411# ifdef FEAT_X11
9412 if (check_connection() == OK)
9413 serverRegisterName(X_DISPLAY, server);
9414# else
9415 serverSetName(server);
9416# endif
9417 }
9418#else
9419 EMSG(_("E942: +clientserver feature not available"));
9420#endif
9421}
9422
9423/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009424 * "remove()" function
9425 */
9426 static void
9427f_remove(typval_T *argvars, typval_T *rettv)
9428{
9429 list_T *l;
9430 listitem_T *item, *item2;
9431 listitem_T *li;
9432 long idx;
9433 long end;
9434 char_u *key;
9435 dict_T *d;
9436 dictitem_T *di;
9437 char_u *arg_errmsg = (char_u *)N_("remove() argument");
9438
9439 if (argvars[0].v_type == VAR_DICT)
9440 {
9441 if (argvars[2].v_type != VAR_UNKNOWN)
9442 EMSG2(_(e_toomanyarg), "remove()");
9443 else if ((d = argvars[0].vval.v_dict) != NULL
9444 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
9445 {
9446 key = get_tv_string_chk(&argvars[1]);
9447 if (key != NULL)
9448 {
9449 di = dict_find(d, key, -1);
9450 if (di == NULL)
9451 EMSG2(_(e_dictkey), key);
9452 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
9453 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
9454 {
9455 *rettv = di->di_tv;
9456 init_tv(&di->di_tv);
9457 dictitem_remove(d, di);
9458 }
9459 }
9460 }
9461 }
9462 else if (argvars[0].v_type != VAR_LIST)
9463 EMSG2(_(e_listdictarg), "remove()");
9464 else if ((l = argvars[0].vval.v_list) != NULL
9465 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
9466 {
9467 int error = FALSE;
9468
9469 idx = (long)get_tv_number_chk(&argvars[1], &error);
9470 if (error)
9471 ; /* type error: do nothing, errmsg already given */
9472 else if ((item = list_find(l, idx)) == NULL)
9473 EMSGN(_(e_listidx), idx);
9474 else
9475 {
9476 if (argvars[2].v_type == VAR_UNKNOWN)
9477 {
9478 /* Remove one item, return its value. */
9479 vimlist_remove(l, item, item);
9480 *rettv = item->li_tv;
9481 vim_free(item);
9482 }
9483 else
9484 {
9485 /* Remove range of items, return list with values. */
9486 end = (long)get_tv_number_chk(&argvars[2], &error);
9487 if (error)
9488 ; /* type error: do nothing */
9489 else if ((item2 = list_find(l, end)) == NULL)
9490 EMSGN(_(e_listidx), end);
9491 else
9492 {
9493 int cnt = 0;
9494
9495 for (li = item; li != NULL; li = li->li_next)
9496 {
9497 ++cnt;
9498 if (li == item2)
9499 break;
9500 }
9501 if (li == NULL) /* didn't find "item2" after "item" */
9502 EMSG(_(e_invrange));
9503 else
9504 {
9505 vimlist_remove(l, item, item2);
9506 if (rettv_list_alloc(rettv) == OK)
9507 {
9508 l = rettv->vval.v_list;
9509 l->lv_first = item;
9510 l->lv_last = item2;
9511 item->li_prev = NULL;
9512 item2->li_next = NULL;
9513 l->lv_len = cnt;
9514 }
9515 }
9516 }
9517 }
9518 }
9519 }
9520}
9521
9522/*
9523 * "rename({from}, {to})" function
9524 */
9525 static void
9526f_rename(typval_T *argvars, typval_T *rettv)
9527{
9528 char_u buf[NUMBUFLEN];
9529
9530 if (check_restricted() || check_secure())
9531 rettv->vval.v_number = -1;
9532 else
9533 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
9534 get_tv_string_buf(&argvars[1], buf));
9535}
9536
9537/*
9538 * "repeat()" function
9539 */
9540 static void
9541f_repeat(typval_T *argvars, typval_T *rettv)
9542{
9543 char_u *p;
9544 int n;
9545 int slen;
9546 int len;
9547 char_u *r;
9548 int i;
9549
9550 n = (int)get_tv_number(&argvars[1]);
9551 if (argvars[0].v_type == VAR_LIST)
9552 {
9553 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9554 while (n-- > 0)
9555 if (list_extend(rettv->vval.v_list,
9556 argvars[0].vval.v_list, NULL) == FAIL)
9557 break;
9558 }
9559 else
9560 {
9561 p = get_tv_string(&argvars[0]);
9562 rettv->v_type = VAR_STRING;
9563 rettv->vval.v_string = NULL;
9564
9565 slen = (int)STRLEN(p);
9566 len = slen * n;
9567 if (len <= 0)
9568 return;
9569
9570 r = alloc(len + 1);
9571 if (r != NULL)
9572 {
9573 for (i = 0; i < n; i++)
9574 mch_memmove(r + i * slen, p, (size_t)slen);
9575 r[len] = NUL;
9576 }
9577
9578 rettv->vval.v_string = r;
9579 }
9580}
9581
9582/*
9583 * "resolve()" function
9584 */
9585 static void
9586f_resolve(typval_T *argvars, typval_T *rettv)
9587{
9588 char_u *p;
9589#ifdef HAVE_READLINK
9590 char_u *buf = NULL;
9591#endif
9592
9593 p = get_tv_string(&argvars[0]);
9594#ifdef FEAT_SHORTCUT
9595 {
9596 char_u *v = NULL;
9597
9598 v = mch_resolve_shortcut(p);
9599 if (v != NULL)
9600 rettv->vval.v_string = v;
9601 else
9602 rettv->vval.v_string = vim_strsave(p);
9603 }
9604#else
9605# ifdef HAVE_READLINK
9606 {
9607 char_u *cpy;
9608 int len;
9609 char_u *remain = NULL;
9610 char_u *q;
9611 int is_relative_to_current = FALSE;
9612 int has_trailing_pathsep = FALSE;
9613 int limit = 100;
9614
9615 p = vim_strsave(p);
9616
9617 if (p[0] == '.' && (vim_ispathsep(p[1])
9618 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9619 is_relative_to_current = TRUE;
9620
9621 len = STRLEN(p);
9622 if (len > 0 && after_pathsep(p, p + len))
9623 {
9624 has_trailing_pathsep = TRUE;
9625 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9626 }
9627
9628 q = getnextcomp(p);
9629 if (*q != NUL)
9630 {
9631 /* Separate the first path component in "p", and keep the
9632 * remainder (beginning with the path separator). */
9633 remain = vim_strsave(q - 1);
9634 q[-1] = NUL;
9635 }
9636
9637 buf = alloc(MAXPATHL + 1);
9638 if (buf == NULL)
9639 goto fail;
9640
9641 for (;;)
9642 {
9643 for (;;)
9644 {
9645 len = readlink((char *)p, (char *)buf, MAXPATHL);
9646 if (len <= 0)
9647 break;
9648 buf[len] = NUL;
9649
9650 if (limit-- == 0)
9651 {
9652 vim_free(p);
9653 vim_free(remain);
9654 EMSG(_("E655: Too many symbolic links (cycle?)"));
9655 rettv->vval.v_string = NULL;
9656 goto fail;
9657 }
9658
9659 /* Ensure that the result will have a trailing path separator
9660 * if the argument has one. */
9661 if (remain == NULL && has_trailing_pathsep)
9662 add_pathsep(buf);
9663
9664 /* Separate the first path component in the link value and
9665 * concatenate the remainders. */
9666 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9667 if (*q != NUL)
9668 {
9669 if (remain == NULL)
9670 remain = vim_strsave(q - 1);
9671 else
9672 {
9673 cpy = concat_str(q - 1, remain);
9674 if (cpy != NULL)
9675 {
9676 vim_free(remain);
9677 remain = cpy;
9678 }
9679 }
9680 q[-1] = NUL;
9681 }
9682
9683 q = gettail(p);
9684 if (q > p && *q == NUL)
9685 {
9686 /* Ignore trailing path separator. */
9687 q[-1] = NUL;
9688 q = gettail(p);
9689 }
9690 if (q > p && !mch_isFullName(buf))
9691 {
9692 /* symlink is relative to directory of argument */
9693 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
9694 if (cpy != NULL)
9695 {
9696 STRCPY(cpy, p);
9697 STRCPY(gettail(cpy), buf);
9698 vim_free(p);
9699 p = cpy;
9700 }
9701 }
9702 else
9703 {
9704 vim_free(p);
9705 p = vim_strsave(buf);
9706 }
9707 }
9708
9709 if (remain == NULL)
9710 break;
9711
9712 /* Append the first path component of "remain" to "p". */
9713 q = getnextcomp(remain + 1);
9714 len = q - remain - (*q != NUL);
9715 cpy = vim_strnsave(p, STRLEN(p) + len);
9716 if (cpy != NULL)
9717 {
9718 STRNCAT(cpy, remain, len);
9719 vim_free(p);
9720 p = cpy;
9721 }
9722 /* Shorten "remain". */
9723 if (*q != NUL)
9724 STRMOVE(remain, q - 1);
9725 else
Bram Moolenaard23a8232018-02-10 18:45:26 +01009726 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009727 }
9728
9729 /* If the result is a relative path name, make it explicitly relative to
9730 * the current directory if and only if the argument had this form. */
9731 if (!vim_ispathsep(*p))
9732 {
9733 if (is_relative_to_current
9734 && *p != NUL
9735 && !(p[0] == '.'
9736 && (p[1] == NUL
9737 || vim_ispathsep(p[1])
9738 || (p[1] == '.'
9739 && (p[2] == NUL
9740 || vim_ispathsep(p[2]))))))
9741 {
9742 /* Prepend "./". */
9743 cpy = concat_str((char_u *)"./", p);
9744 if (cpy != NULL)
9745 {
9746 vim_free(p);
9747 p = cpy;
9748 }
9749 }
9750 else if (!is_relative_to_current)
9751 {
9752 /* Strip leading "./". */
9753 q = p;
9754 while (q[0] == '.' && vim_ispathsep(q[1]))
9755 q += 2;
9756 if (q > p)
9757 STRMOVE(p, p + 2);
9758 }
9759 }
9760
9761 /* Ensure that the result will have no trailing path separator
9762 * if the argument had none. But keep "/" or "//". */
9763 if (!has_trailing_pathsep)
9764 {
9765 q = p + STRLEN(p);
9766 if (after_pathsep(p, q))
9767 *gettail_sep(p) = NUL;
9768 }
9769
9770 rettv->vval.v_string = p;
9771 }
9772# else
9773 rettv->vval.v_string = vim_strsave(p);
9774# endif
9775#endif
9776
9777 simplify_filename(rettv->vval.v_string);
9778
9779#ifdef HAVE_READLINK
9780fail:
9781 vim_free(buf);
9782#endif
9783 rettv->v_type = VAR_STRING;
9784}
9785
9786/*
9787 * "reverse({list})" function
9788 */
9789 static void
9790f_reverse(typval_T *argvars, typval_T *rettv)
9791{
9792 list_T *l;
9793 listitem_T *li, *ni;
9794
9795 if (argvars[0].v_type != VAR_LIST)
9796 EMSG2(_(e_listarg), "reverse()");
9797 else if ((l = argvars[0].vval.v_list) != NULL
9798 && !tv_check_lock(l->lv_lock,
9799 (char_u *)N_("reverse() argument"), TRUE))
9800 {
9801 li = l->lv_last;
9802 l->lv_first = l->lv_last = NULL;
9803 l->lv_len = 0;
9804 while (li != NULL)
9805 {
9806 ni = li->li_prev;
9807 list_append(l, li);
9808 li = ni;
9809 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009810 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009811 l->lv_idx = l->lv_len - l->lv_idx - 1;
9812 }
9813}
9814
9815#define SP_NOMOVE 0x01 /* don't move cursor */
9816#define SP_REPEAT 0x02 /* repeat to find outer pair */
9817#define SP_RETCOUNT 0x04 /* return matchcount */
9818#define SP_SETPCMARK 0x08 /* set previous context mark */
9819#define SP_START 0x10 /* accept match at start position */
9820#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
9821#define SP_END 0x40 /* leave cursor at end of match */
9822#define SP_COLUMN 0x80 /* start at cursor column */
9823
9824static int get_search_arg(typval_T *varp, int *flagsp);
9825
9826/*
9827 * Get flags for a search function.
9828 * Possibly sets "p_ws".
9829 * Returns BACKWARD, FORWARD or zero (for an error).
9830 */
9831 static int
9832get_search_arg(typval_T *varp, int *flagsp)
9833{
9834 int dir = FORWARD;
9835 char_u *flags;
9836 char_u nbuf[NUMBUFLEN];
9837 int mask;
9838
9839 if (varp->v_type != VAR_UNKNOWN)
9840 {
9841 flags = get_tv_string_buf_chk(varp, nbuf);
9842 if (flags == NULL)
9843 return 0; /* type error; errmsg already given */
9844 while (*flags != NUL)
9845 {
9846 switch (*flags)
9847 {
9848 case 'b': dir = BACKWARD; break;
9849 case 'w': p_ws = TRUE; break;
9850 case 'W': p_ws = FALSE; break;
9851 default: mask = 0;
9852 if (flagsp != NULL)
9853 switch (*flags)
9854 {
9855 case 'c': mask = SP_START; break;
9856 case 'e': mask = SP_END; break;
9857 case 'm': mask = SP_RETCOUNT; break;
9858 case 'n': mask = SP_NOMOVE; break;
9859 case 'p': mask = SP_SUBPAT; break;
9860 case 'r': mask = SP_REPEAT; break;
9861 case 's': mask = SP_SETPCMARK; break;
9862 case 'z': mask = SP_COLUMN; break;
9863 }
9864 if (mask == 0)
9865 {
9866 EMSG2(_(e_invarg2), flags);
9867 dir = 0;
9868 }
9869 else
9870 *flagsp |= mask;
9871 }
9872 if (dir == 0)
9873 break;
9874 ++flags;
9875 }
9876 }
9877 return dir;
9878}
9879
9880/*
9881 * Shared by search() and searchpos() functions.
9882 */
9883 static int
9884search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
9885{
9886 int flags;
9887 char_u *pat;
9888 pos_T pos;
9889 pos_T save_cursor;
9890 int save_p_ws = p_ws;
9891 int dir;
9892 int retval = 0; /* default: FAIL */
9893 long lnum_stop = 0;
9894 proftime_T tm;
9895#ifdef FEAT_RELTIME
9896 long time_limit = 0;
9897#endif
9898 int options = SEARCH_KEEP;
9899 int subpatnum;
9900
9901 pat = get_tv_string(&argvars[0]);
9902 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
9903 if (dir == 0)
9904 goto theend;
9905 flags = *flagsp;
9906 if (flags & SP_START)
9907 options |= SEARCH_START;
9908 if (flags & SP_END)
9909 options |= SEARCH_END;
9910 if (flags & SP_COLUMN)
9911 options |= SEARCH_COL;
9912
9913 /* Optional arguments: line number to stop searching and timeout. */
9914 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9915 {
9916 lnum_stop = (long)get_tv_number_chk(&argvars[2], NULL);
9917 if (lnum_stop < 0)
9918 goto theend;
9919#ifdef FEAT_RELTIME
9920 if (argvars[3].v_type != VAR_UNKNOWN)
9921 {
9922 time_limit = (long)get_tv_number_chk(&argvars[3], NULL);
9923 if (time_limit < 0)
9924 goto theend;
9925 }
9926#endif
9927 }
9928
9929#ifdef FEAT_RELTIME
9930 /* Set the time limit, if there is one. */
9931 profile_setlimit(time_limit, &tm);
9932#endif
9933
9934 /*
9935 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
9936 * Check to make sure only those flags are set.
9937 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
9938 * flags cannot be set. Check for that condition also.
9939 */
9940 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
9941 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9942 {
9943 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
9944 goto theend;
9945 }
9946
9947 pos = save_cursor = curwin->w_cursor;
9948 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009949 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009950 if (subpatnum != FAIL)
9951 {
9952 if (flags & SP_SUBPAT)
9953 retval = subpatnum;
9954 else
9955 retval = pos.lnum;
9956 if (flags & SP_SETPCMARK)
9957 setpcmark();
9958 curwin->w_cursor = pos;
9959 if (match_pos != NULL)
9960 {
9961 /* Store the match cursor position */
9962 match_pos->lnum = pos.lnum;
9963 match_pos->col = pos.col + 1;
9964 }
9965 /* "/$" will put the cursor after the end of the line, may need to
9966 * correct that here */
9967 check_cursor();
9968 }
9969
9970 /* If 'n' flag is used: restore cursor position. */
9971 if (flags & SP_NOMOVE)
9972 curwin->w_cursor = save_cursor;
9973 else
9974 curwin->w_set_curswant = TRUE;
9975theend:
9976 p_ws = save_p_ws;
9977
9978 return retval;
9979}
9980
9981#ifdef FEAT_FLOAT
9982
9983/*
9984 * round() is not in C90, use ceil() or floor() instead.
9985 */
9986 float_T
9987vim_round(float_T f)
9988{
9989 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
9990}
9991
9992/*
9993 * "round({float})" function
9994 */
9995 static void
9996f_round(typval_T *argvars, typval_T *rettv)
9997{
9998 float_T f = 0.0;
9999
10000 rettv->v_type = VAR_FLOAT;
10001 if (get_float_arg(argvars, &f) == OK)
10002 rettv->vval.v_float = vim_round(f);
10003 else
10004 rettv->vval.v_float = 0.0;
10005}
10006#endif
10007
10008/*
10009 * "screenattr()" function
10010 */
10011 static void
10012f_screenattr(typval_T *argvars, typval_T *rettv)
10013{
10014 int row;
10015 int col;
10016 int c;
10017
10018 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
10019 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
10020 if (row < 0 || row >= screen_Rows
10021 || col < 0 || col >= screen_Columns)
10022 c = -1;
10023 else
10024 c = ScreenAttrs[LineOffset[row] + col];
10025 rettv->vval.v_number = c;
10026}
10027
10028/*
10029 * "screenchar()" function
10030 */
10031 static void
10032f_screenchar(typval_T *argvars, typval_T *rettv)
10033{
10034 int row;
10035 int col;
10036 int off;
10037 int c;
10038
10039 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
10040 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
10041 if (row < 0 || row >= screen_Rows
10042 || col < 0 || col >= screen_Columns)
10043 c = -1;
10044 else
10045 {
10046 off = LineOffset[row] + col;
10047#ifdef FEAT_MBYTE
10048 if (enc_utf8 && ScreenLinesUC[off] != 0)
10049 c = ScreenLinesUC[off];
10050 else
10051#endif
10052 c = ScreenLines[off];
10053 }
10054 rettv->vval.v_number = c;
10055}
10056
10057/*
10058 * "screencol()" function
10059 *
10060 * First column is 1 to be consistent with virtcol().
10061 */
10062 static void
10063f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10064{
10065 rettv->vval.v_number = screen_screencol() + 1;
10066}
10067
10068/*
10069 * "screenrow()" function
10070 */
10071 static void
10072f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10073{
10074 rettv->vval.v_number = screen_screenrow() + 1;
10075}
10076
10077/*
10078 * "search()" function
10079 */
10080 static void
10081f_search(typval_T *argvars, typval_T *rettv)
10082{
10083 int flags = 0;
10084
10085 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10086}
10087
10088/*
10089 * "searchdecl()" function
10090 */
10091 static void
10092f_searchdecl(typval_T *argvars, typval_T *rettv)
10093{
10094 int locally = 1;
10095 int thisblock = 0;
10096 int error = FALSE;
10097 char_u *name;
10098
10099 rettv->vval.v_number = 1; /* default: FAIL */
10100
10101 name = get_tv_string_chk(&argvars[0]);
10102 if (argvars[1].v_type != VAR_UNKNOWN)
10103 {
10104 locally = (int)get_tv_number_chk(&argvars[1], &error) == 0;
10105 if (!error && argvars[2].v_type != VAR_UNKNOWN)
10106 thisblock = (int)get_tv_number_chk(&argvars[2], &error) != 0;
10107 }
10108 if (!error && name != NULL)
10109 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10110 locally, thisblock, SEARCH_KEEP) == FAIL;
10111}
10112
10113/*
10114 * Used by searchpair() and searchpairpos()
10115 */
10116 static int
10117searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10118{
10119 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010120 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010121 int save_p_ws = p_ws;
10122 int dir;
10123 int flags = 0;
10124 char_u nbuf1[NUMBUFLEN];
10125 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010126 int retval = 0; /* default: FAIL */
10127 long lnum_stop = 0;
10128 long time_limit = 0;
10129
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010130 /* Get the three pattern arguments: start, middle, end. Will result in an
10131 * error if not a valid argument. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010132 spat = get_tv_string_chk(&argvars[0]);
10133 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
10134 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
10135 if (spat == NULL || mpat == NULL || epat == NULL)
10136 goto theend; /* type error */
10137
10138 /* Handle the optional fourth argument: flags */
10139 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
10140 if (dir == 0)
10141 goto theend;
10142
10143 /* Don't accept SP_END or SP_SUBPAT.
10144 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
10145 */
10146 if ((flags & (SP_END | SP_SUBPAT)) != 0
10147 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10148 {
10149 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
10150 goto theend;
10151 }
10152
10153 /* Using 'r' implies 'W', otherwise it doesn't work. */
10154 if (flags & SP_REPEAT)
10155 p_ws = FALSE;
10156
10157 /* Optional fifth argument: skip expression */
10158 if (argvars[3].v_type == VAR_UNKNOWN
10159 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010010160 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010161 else
10162 {
Bram Moolenaar48570482017-10-30 21:48:41 +010010163 skip = &argvars[4];
10164 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
10165 && skip->v_type != VAR_STRING)
10166 {
10167 /* Type error */
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010168 EMSG2(_(e_invarg2), get_tv_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010010169 goto theend;
10170 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010171 if (argvars[5].v_type != VAR_UNKNOWN)
10172 {
10173 lnum_stop = (long)get_tv_number_chk(&argvars[5], NULL);
10174 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010175 {
10176 EMSG2(_(e_invarg2), get_tv_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010177 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010178 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010179#ifdef FEAT_RELTIME
10180 if (argvars[6].v_type != VAR_UNKNOWN)
10181 {
10182 time_limit = (long)get_tv_number_chk(&argvars[6], NULL);
10183 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010184 {
10185 EMSG2(_(e_invarg2), get_tv_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010186 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010187 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010188 }
10189#endif
10190 }
10191 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010192
10193 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
10194 match_pos, lnum_stop, time_limit);
10195
10196theend:
10197 p_ws = save_p_ws;
10198
10199 return retval;
10200}
10201
10202/*
10203 * "searchpair()" function
10204 */
10205 static void
10206f_searchpair(typval_T *argvars, typval_T *rettv)
10207{
10208 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
10209}
10210
10211/*
10212 * "searchpairpos()" function
10213 */
10214 static void
10215f_searchpairpos(typval_T *argvars, typval_T *rettv)
10216{
10217 pos_T match_pos;
10218 int lnum = 0;
10219 int col = 0;
10220
10221 if (rettv_list_alloc(rettv) == FAIL)
10222 return;
10223
10224 if (searchpair_cmn(argvars, &match_pos) > 0)
10225 {
10226 lnum = match_pos.lnum;
10227 col = match_pos.col;
10228 }
10229
10230 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10231 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10232}
10233
10234/*
10235 * Search for a start/middle/end thing.
10236 * Used by searchpair(), see its documentation for the details.
10237 * Returns 0 or -1 for no match,
10238 */
10239 long
10240do_searchpair(
10241 char_u *spat, /* start pattern */
10242 char_u *mpat, /* middle pattern */
10243 char_u *epat, /* end pattern */
10244 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010010245 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010246 int flags, /* SP_SETPCMARK and other SP_ values */
10247 pos_T *match_pos,
10248 linenr_T lnum_stop, /* stop at this line if not zero */
10249 long time_limit UNUSED) /* stop after this many msec */
10250{
10251 char_u *save_cpo;
10252 char_u *pat, *pat2 = NULL, *pat3 = NULL;
10253 long retval = 0;
10254 pos_T pos;
10255 pos_T firstpos;
10256 pos_T foundpos;
10257 pos_T save_cursor;
10258 pos_T save_pos;
10259 int n;
10260 int r;
10261 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010010262 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010263 int err;
10264 int options = SEARCH_KEEP;
10265 proftime_T tm;
10266
10267 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10268 save_cpo = p_cpo;
10269 p_cpo = empty_option;
10270
10271#ifdef FEAT_RELTIME
10272 /* Set the time limit, if there is one. */
10273 profile_setlimit(time_limit, &tm);
10274#endif
10275
10276 /* Make two search patterns: start/end (pat2, for in nested pairs) and
10277 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010278 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
10279 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010280 if (pat2 == NULL || pat3 == NULL)
10281 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010282 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010283 if (*mpat == NUL)
10284 STRCPY(pat3, pat2);
10285 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010286 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010287 spat, epat, mpat);
10288 if (flags & SP_START)
10289 options |= SEARCH_START;
10290
Bram Moolenaar48570482017-10-30 21:48:41 +010010291 if (skip != NULL)
10292 {
10293 /* Empty string means to not use the skip expression. */
10294 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
10295 use_skip = skip->vval.v_string != NULL
10296 && *skip->vval.v_string != NUL;
10297 }
10298
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010299 save_cursor = curwin->w_cursor;
10300 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010301 CLEAR_POS(&firstpos);
10302 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010303 pat = pat3;
10304 for (;;)
10305 {
10306 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010307 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010308 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010309 /* didn't find it or found the first match again: FAIL */
10310 break;
10311
10312 if (firstpos.lnum == 0)
10313 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010314 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010315 {
10316 /* Found the same position again. Can happen with a pattern that
10317 * has "\zs" at the end and searching backwards. Advance one
10318 * character and try again. */
10319 if (dir == BACKWARD)
10320 decl(&pos);
10321 else
10322 incl(&pos);
10323 }
10324 foundpos = pos;
10325
10326 /* clear the start flag to avoid getting stuck here */
10327 options &= ~SEARCH_START;
10328
10329 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010010330 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010331 {
10332 save_pos = curwin->w_cursor;
10333 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010010334 err = FALSE;
10335 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010336 curwin->w_cursor = save_pos;
10337 if (err)
10338 {
10339 /* Evaluating {skip} caused an error, break here. */
10340 curwin->w_cursor = save_cursor;
10341 retval = -1;
10342 break;
10343 }
10344 if (r)
10345 continue;
10346 }
10347
10348 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
10349 {
10350 /* Found end when searching backwards or start when searching
10351 * forward: nested pair. */
10352 ++nest;
10353 pat = pat2; /* nested, don't search for middle */
10354 }
10355 else
10356 {
10357 /* Found end when searching forward or start when searching
10358 * backward: end of (nested) pair; or found middle in outer pair. */
10359 if (--nest == 1)
10360 pat = pat3; /* outer level, search for middle */
10361 }
10362
10363 if (nest == 0)
10364 {
10365 /* Found the match: return matchcount or line number. */
10366 if (flags & SP_RETCOUNT)
10367 ++retval;
10368 else
10369 retval = pos.lnum;
10370 if (flags & SP_SETPCMARK)
10371 setpcmark();
10372 curwin->w_cursor = pos;
10373 if (!(flags & SP_REPEAT))
10374 break;
10375 nest = 1; /* search for next unmatched */
10376 }
10377 }
10378
10379 if (match_pos != NULL)
10380 {
10381 /* Store the match cursor position */
10382 match_pos->lnum = curwin->w_cursor.lnum;
10383 match_pos->col = curwin->w_cursor.col + 1;
10384 }
10385
10386 /* If 'n' flag is used or search failed: restore cursor position. */
10387 if ((flags & SP_NOMOVE) || retval == 0)
10388 curwin->w_cursor = save_cursor;
10389
10390theend:
10391 vim_free(pat2);
10392 vim_free(pat3);
10393 if (p_cpo == empty_option)
10394 p_cpo = save_cpo;
10395 else
10396 /* Darn, evaluating the {skip} expression changed the value. */
10397 free_string_option(save_cpo);
10398
10399 return retval;
10400}
10401
10402/*
10403 * "searchpos()" function
10404 */
10405 static void
10406f_searchpos(typval_T *argvars, typval_T *rettv)
10407{
10408 pos_T match_pos;
10409 int lnum = 0;
10410 int col = 0;
10411 int n;
10412 int flags = 0;
10413
10414 if (rettv_list_alloc(rettv) == FAIL)
10415 return;
10416
10417 n = search_cmn(argvars, &match_pos, &flags);
10418 if (n > 0)
10419 {
10420 lnum = match_pos.lnum;
10421 col = match_pos.col;
10422 }
10423
10424 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10425 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10426 if (flags & SP_SUBPAT)
10427 list_append_number(rettv->vval.v_list, (varnumber_T)n);
10428}
10429
10430 static void
10431f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
10432{
10433#ifdef FEAT_CLIENTSERVER
10434 char_u buf[NUMBUFLEN];
10435 char_u *server = get_tv_string_chk(&argvars[0]);
10436 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
10437
10438 rettv->vval.v_number = -1;
10439 if (server == NULL || reply == NULL)
10440 return;
10441 if (check_restricted() || check_secure())
10442 return;
10443# ifdef FEAT_X11
10444 if (check_connection() == FAIL)
10445 return;
10446# endif
10447
10448 if (serverSendReply(server, reply) < 0)
10449 {
10450 EMSG(_("E258: Unable to send to client"));
10451 return;
10452 }
10453 rettv->vval.v_number = 0;
10454#else
10455 rettv->vval.v_number = -1;
10456#endif
10457}
10458
10459 static void
10460f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
10461{
10462 char_u *r = NULL;
10463
10464#ifdef FEAT_CLIENTSERVER
10465# ifdef WIN32
10466 r = serverGetVimNames();
10467# else
10468 make_connection();
10469 if (X_DISPLAY != NULL)
10470 r = serverGetVimNames(X_DISPLAY);
10471# endif
10472#endif
10473 rettv->v_type = VAR_STRING;
10474 rettv->vval.v_string = r;
10475}
10476
10477/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010478 * "setbufline()" function
10479 */
10480 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020010481f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010482{
10483 linenr_T lnum;
10484 buf_T *buf;
10485
10486 buf = get_buf_tv(&argvars[0], FALSE);
10487 if (buf == NULL)
10488 rettv->vval.v_number = 1; /* FAIL */
10489 else
10490 {
10491 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020010492 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010493 }
10494}
10495
10496/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010497 * "setbufvar()" function
10498 */
10499 static void
10500f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10501{
10502 buf_T *buf;
10503 char_u *varname, *bufvarname;
10504 typval_T *varp;
10505 char_u nbuf[NUMBUFLEN];
10506
10507 if (check_restricted() || check_secure())
10508 return;
10509 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10510 varname = get_tv_string_chk(&argvars[1]);
10511 buf = get_buf_tv(&argvars[0], FALSE);
10512 varp = &argvars[2];
10513
10514 if (buf != NULL && varname != NULL && varp != NULL)
10515 {
10516 if (*varname == '&')
10517 {
10518 long numval;
10519 char_u *strval;
10520 int error = FALSE;
10521 aco_save_T aco;
10522
10523 /* set curbuf to be our buf, temporarily */
10524 aucmd_prepbuf(&aco, buf);
10525
10526 ++varname;
10527 numval = (long)get_tv_number_chk(varp, &error);
10528 strval = get_tv_string_buf_chk(varp, nbuf);
10529 if (!error && strval != NULL)
10530 set_option_value(varname, numval, strval, OPT_LOCAL);
10531
10532 /* reset notion of buffer */
10533 aucmd_restbuf(&aco);
10534 }
10535 else
10536 {
10537 buf_T *save_curbuf = curbuf;
10538
10539 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
10540 if (bufvarname != NULL)
10541 {
10542 curbuf = buf;
10543 STRCPY(bufvarname, "b:");
10544 STRCPY(bufvarname + 2, varname);
10545 set_var(bufvarname, varp, TRUE);
10546 vim_free(bufvarname);
10547 curbuf = save_curbuf;
10548 }
10549 }
10550 }
10551}
10552
10553 static void
10554f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10555{
10556 dict_T *d;
10557 dictitem_T *di;
10558 char_u *csearch;
10559
10560 if (argvars[0].v_type != VAR_DICT)
10561 {
10562 EMSG(_(e_dictreq));
10563 return;
10564 }
10565
10566 if ((d = argvars[0].vval.v_dict) != NULL)
10567 {
10568 csearch = get_dict_string(d, (char_u *)"char", FALSE);
10569 if (csearch != NULL)
10570 {
10571#ifdef FEAT_MBYTE
10572 if (enc_utf8)
10573 {
10574 int pcc[MAX_MCO];
10575 int c = utfc_ptr2char(csearch, pcc);
10576
10577 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
10578 }
10579 else
10580#endif
10581 set_last_csearch(PTR2CHAR(csearch),
10582 csearch, MB_PTR2LEN(csearch));
10583 }
10584
10585 di = dict_find(d, (char_u *)"forward", -1);
10586 if (di != NULL)
10587 set_csearch_direction((int)get_tv_number(&di->di_tv)
10588 ? FORWARD : BACKWARD);
10589
10590 di = dict_find(d, (char_u *)"until", -1);
10591 if (di != NULL)
10592 set_csearch_until(!!get_tv_number(&di->di_tv));
10593 }
10594}
10595
10596/*
10597 * "setcmdpos()" function
10598 */
10599 static void
10600f_setcmdpos(typval_T *argvars, typval_T *rettv)
10601{
10602 int pos = (int)get_tv_number(&argvars[0]) - 1;
10603
10604 if (pos >= 0)
10605 rettv->vval.v_number = set_cmdline_pos(pos);
10606}
10607
10608/*
10609 * "setfperm({fname}, {mode})" function
10610 */
10611 static void
10612f_setfperm(typval_T *argvars, typval_T *rettv)
10613{
10614 char_u *fname;
10615 char_u modebuf[NUMBUFLEN];
10616 char_u *mode_str;
10617 int i;
10618 int mask;
10619 int mode = 0;
10620
10621 rettv->vval.v_number = 0;
10622 fname = get_tv_string_chk(&argvars[0]);
10623 if (fname == NULL)
10624 return;
10625 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
10626 if (mode_str == NULL)
10627 return;
10628 if (STRLEN(mode_str) != 9)
10629 {
10630 EMSG2(_(e_invarg2), mode_str);
10631 return;
10632 }
10633
10634 mask = 1;
10635 for (i = 8; i >= 0; --i)
10636 {
10637 if (mode_str[i] != '-')
10638 mode |= mask;
10639 mask = mask << 1;
10640 }
10641 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
10642}
10643
10644/*
10645 * "setline()" function
10646 */
10647 static void
10648f_setline(typval_T *argvars, typval_T *rettv)
10649{
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010650 linenr_T lnum = get_tv_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010651
Bram Moolenaarca851592018-06-06 21:04:07 +020010652 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010653}
10654
Bram Moolenaard823fa92016-08-12 16:29:27 +020010655static 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 +020010656
10657/*
10658 * Used by "setqflist()" and "setloclist()" functions
10659 */
10660 static void
10661set_qf_ll_list(
10662 win_T *wp UNUSED,
10663 typval_T *list_arg UNUSED,
10664 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020010665 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010666 typval_T *rettv)
10667{
10668#ifdef FEAT_QUICKFIX
10669 static char *e_invact = N_("E927: Invalid action: '%s'");
10670 char_u *act;
10671 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010672 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010673#endif
10674
10675 rettv->vval.v_number = -1;
10676
10677#ifdef FEAT_QUICKFIX
10678 if (list_arg->v_type != VAR_LIST)
10679 EMSG(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010680 else if (recursive != 0)
10681 EMSG(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010682 else
10683 {
10684 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010685 dict_T *d = NULL;
10686 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010687
10688 if (action_arg->v_type == VAR_STRING)
10689 {
10690 act = get_tv_string_chk(action_arg);
10691 if (act == NULL)
10692 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020010693 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
10694 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010695 action = *act;
10696 else
10697 EMSG2(_(e_invact), act);
10698 }
10699 else if (action_arg->v_type == VAR_UNKNOWN)
10700 action = ' ';
10701 else
10702 EMSG(_(e_stringreq));
10703
Bram Moolenaard823fa92016-08-12 16:29:27 +020010704 if (action_arg->v_type != VAR_UNKNOWN
10705 && what_arg->v_type != VAR_UNKNOWN)
10706 {
10707 if (what_arg->v_type == VAR_DICT)
10708 d = what_arg->vval.v_dict;
10709 else
10710 {
10711 EMSG(_(e_dictreq));
10712 valid_dict = FALSE;
10713 }
10714 }
10715
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010716 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010717 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010718 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
10719 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010720 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010721 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010722 }
10723#endif
10724}
10725
10726/*
10727 * "setloclist()" function
10728 */
10729 static void
10730f_setloclist(typval_T *argvars, typval_T *rettv)
10731{
10732 win_T *win;
10733
10734 rettv->vval.v_number = -1;
10735
10736 win = find_win_by_nr(&argvars[0], NULL);
10737 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020010738 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010739}
10740
10741/*
10742 * "setmatches()" function
10743 */
10744 static void
10745f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10746{
10747#ifdef FEAT_SEARCH_EXTRA
10748 list_T *l;
10749 listitem_T *li;
10750 dict_T *d;
10751 list_T *s = NULL;
10752
10753 rettv->vval.v_number = -1;
10754 if (argvars[0].v_type != VAR_LIST)
10755 {
10756 EMSG(_(e_listreq));
10757 return;
10758 }
10759 if ((l = argvars[0].vval.v_list) != NULL)
10760 {
10761
10762 /* To some extent make sure that we are dealing with a list from
10763 * "getmatches()". */
10764 li = l->lv_first;
10765 while (li != NULL)
10766 {
10767 if (li->li_tv.v_type != VAR_DICT
10768 || (d = li->li_tv.vval.v_dict) == NULL)
10769 {
10770 EMSG(_(e_invarg));
10771 return;
10772 }
10773 if (!(dict_find(d, (char_u *)"group", -1) != NULL
10774 && (dict_find(d, (char_u *)"pattern", -1) != NULL
10775 || dict_find(d, (char_u *)"pos1", -1) != NULL)
10776 && dict_find(d, (char_u *)"priority", -1) != NULL
10777 && dict_find(d, (char_u *)"id", -1) != NULL))
10778 {
10779 EMSG(_(e_invarg));
10780 return;
10781 }
10782 li = li->li_next;
10783 }
10784
10785 clear_matches(curwin);
10786 li = l->lv_first;
10787 while (li != NULL)
10788 {
10789 int i = 0;
10790 char_u buf[5];
10791 dictitem_T *di;
10792 char_u *group;
10793 int priority;
10794 int id;
10795 char_u *conceal;
10796
10797 d = li->li_tv.vval.v_dict;
10798 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
10799 {
10800 if (s == NULL)
10801 {
10802 s = list_alloc();
10803 if (s == NULL)
10804 return;
10805 }
10806
10807 /* match from matchaddpos() */
10808 for (i = 1; i < 9; i++)
10809 {
10810 sprintf((char *)buf, (char *)"pos%d", i);
10811 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
10812 {
10813 if (di->di_tv.v_type != VAR_LIST)
10814 return;
10815
10816 list_append_tv(s, &di->di_tv);
10817 s->lv_refcount++;
10818 }
10819 else
10820 break;
10821 }
10822 }
10823
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010824 group = get_dict_string(d, (char_u *)"group", TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010825 priority = (int)get_dict_number(d, (char_u *)"priority");
10826 id = (int)get_dict_number(d, (char_u *)"id");
10827 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010828 ? get_dict_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010829 : NULL;
10830 if (i == 0)
10831 {
10832 match_add(curwin, group,
10833 get_dict_string(d, (char_u *)"pattern", FALSE),
10834 priority, id, NULL, conceal);
10835 }
10836 else
10837 {
10838 match_add(curwin, group, NULL, priority, id, s, conceal);
10839 list_unref(s);
10840 s = NULL;
10841 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010842 vim_free(group);
10843 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010844
10845 li = li->li_next;
10846 }
10847 rettv->vval.v_number = 0;
10848 }
10849#endif
10850}
10851
10852/*
10853 * "setpos()" function
10854 */
10855 static void
10856f_setpos(typval_T *argvars, typval_T *rettv)
10857{
10858 pos_T pos;
10859 int fnum;
10860 char_u *name;
10861 colnr_T curswant = -1;
10862
10863 rettv->vval.v_number = -1;
10864 name = get_tv_string_chk(argvars);
10865 if (name != NULL)
10866 {
10867 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
10868 {
10869 if (--pos.col < 0)
10870 pos.col = 0;
10871 if (name[0] == '.' && name[1] == NUL)
10872 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010873 /* set cursor; "fnum" is ignored */
10874 curwin->w_cursor = pos;
10875 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010876 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010877 curwin->w_curswant = curswant - 1;
10878 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010879 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010880 check_cursor();
10881 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010882 }
10883 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
10884 {
10885 /* set mark */
10886 if (setmark_pos(name[1], &pos, fnum) == OK)
10887 rettv->vval.v_number = 0;
10888 }
10889 else
10890 EMSG(_(e_invarg));
10891 }
10892 }
10893}
10894
10895/*
10896 * "setqflist()" function
10897 */
10898 static void
10899f_setqflist(typval_T *argvars, typval_T *rettv)
10900{
Bram Moolenaard823fa92016-08-12 16:29:27 +020010901 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010902}
10903
10904/*
10905 * "setreg()" function
10906 */
10907 static void
10908f_setreg(typval_T *argvars, typval_T *rettv)
10909{
10910 int regname;
10911 char_u *strregname;
10912 char_u *stropt;
10913 char_u *strval;
10914 int append;
10915 char_u yank_type;
10916 long block_len;
10917
10918 block_len = -1;
10919 yank_type = MAUTO;
10920 append = FALSE;
10921
10922 strregname = get_tv_string_chk(argvars);
10923 rettv->vval.v_number = 1; /* FAIL is default */
10924
10925 if (strregname == NULL)
10926 return; /* type error; errmsg already given */
10927 regname = *strregname;
10928 if (regname == 0 || regname == '@')
10929 regname = '"';
10930
10931 if (argvars[2].v_type != VAR_UNKNOWN)
10932 {
10933 stropt = get_tv_string_chk(&argvars[2]);
10934 if (stropt == NULL)
10935 return; /* type error */
10936 for (; *stropt != NUL; ++stropt)
10937 switch (*stropt)
10938 {
10939 case 'a': case 'A': /* append */
10940 append = TRUE;
10941 break;
10942 case 'v': case 'c': /* character-wise selection */
10943 yank_type = MCHAR;
10944 break;
10945 case 'V': case 'l': /* line-wise selection */
10946 yank_type = MLINE;
10947 break;
10948 case 'b': case Ctrl_V: /* block-wise selection */
10949 yank_type = MBLOCK;
10950 if (VIM_ISDIGIT(stropt[1]))
10951 {
10952 ++stropt;
10953 block_len = getdigits(&stropt) - 1;
10954 --stropt;
10955 }
10956 break;
10957 }
10958 }
10959
10960 if (argvars[1].v_type == VAR_LIST)
10961 {
10962 char_u **lstval;
10963 char_u **allocval;
10964 char_u buf[NUMBUFLEN];
10965 char_u **curval;
10966 char_u **curallocval;
10967 list_T *ll = argvars[1].vval.v_list;
10968 listitem_T *li;
10969 int len;
10970
10971 /* If the list is NULL handle like an empty list. */
10972 len = ll == NULL ? 0 : ll->lv_len;
10973
10974 /* First half: use for pointers to result lines; second half: use for
10975 * pointers to allocated copies. */
10976 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
10977 if (lstval == NULL)
10978 return;
10979 curval = lstval;
10980 allocval = lstval + len + 2;
10981 curallocval = allocval;
10982
10983 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
10984 li = li->li_next)
10985 {
10986 strval = get_tv_string_buf_chk(&li->li_tv, buf);
10987 if (strval == NULL)
10988 goto free_lstval;
10989 if (strval == buf)
10990 {
10991 /* Need to make a copy, next get_tv_string_buf_chk() will
10992 * overwrite the string. */
10993 strval = vim_strsave(buf);
10994 if (strval == NULL)
10995 goto free_lstval;
10996 *curallocval++ = strval;
10997 }
10998 *curval++ = strval;
10999 }
11000 *curval++ = NULL;
11001
11002 write_reg_contents_lst(regname, lstval, -1,
11003 append, yank_type, block_len);
11004free_lstval:
11005 while (curallocval > allocval)
11006 vim_free(*--curallocval);
11007 vim_free(lstval);
11008 }
11009 else
11010 {
11011 strval = get_tv_string_chk(&argvars[1]);
11012 if (strval == NULL)
11013 return;
11014 write_reg_contents_ex(regname, strval, -1,
11015 append, yank_type, block_len);
11016 }
11017 rettv->vval.v_number = 0;
11018}
11019
11020/*
11021 * "settabvar()" function
11022 */
11023 static void
11024f_settabvar(typval_T *argvars, typval_T *rettv)
11025{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011026 tabpage_T *save_curtab;
11027 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011028 char_u *varname, *tabvarname;
11029 typval_T *varp;
11030
11031 rettv->vval.v_number = 0;
11032
11033 if (check_restricted() || check_secure())
11034 return;
11035
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011036 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011037 varname = get_tv_string_chk(&argvars[1]);
11038 varp = &argvars[2];
11039
Bram Moolenaar4033c552017-09-16 20:54:51 +020011040 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011041 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011042 save_curtab = curtab;
11043 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011044
11045 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
11046 if (tabvarname != NULL)
11047 {
11048 STRCPY(tabvarname, "t:");
11049 STRCPY(tabvarname + 2, varname);
11050 set_var(tabvarname, varp, TRUE);
11051 vim_free(tabvarname);
11052 }
11053
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011054 /* Restore current tabpage */
11055 if (valid_tabpage(save_curtab))
11056 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011057 }
11058}
11059
11060/*
11061 * "settabwinvar()" function
11062 */
11063 static void
11064f_settabwinvar(typval_T *argvars, typval_T *rettv)
11065{
11066 setwinvar(argvars, rettv, 1);
11067}
11068
11069/*
11070 * "setwinvar()" function
11071 */
11072 static void
11073f_setwinvar(typval_T *argvars, typval_T *rettv)
11074{
11075 setwinvar(argvars, rettv, 0);
11076}
11077
11078#ifdef FEAT_CRYPT
11079/*
11080 * "sha256({string})" function
11081 */
11082 static void
11083f_sha256(typval_T *argvars, typval_T *rettv)
11084{
11085 char_u *p;
11086
11087 p = get_tv_string(&argvars[0]);
11088 rettv->vval.v_string = vim_strsave(
11089 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
11090 rettv->v_type = VAR_STRING;
11091}
11092#endif /* FEAT_CRYPT */
11093
11094/*
11095 * "shellescape({string})" function
11096 */
11097 static void
11098f_shellescape(typval_T *argvars, typval_T *rettv)
11099{
Bram Moolenaar20615522017-06-05 18:46:26 +020011100 int do_special = non_zero_arg(&argvars[1]);
11101
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011102 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar20615522017-06-05 18:46:26 +020011103 get_tv_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011104 rettv->v_type = VAR_STRING;
11105}
11106
11107/*
11108 * shiftwidth() function
11109 */
11110 static void
11111f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
11112{
11113 rettv->vval.v_number = get_sw_value(curbuf);
11114}
11115
11116/*
11117 * "simplify()" function
11118 */
11119 static void
11120f_simplify(typval_T *argvars, typval_T *rettv)
11121{
11122 char_u *p;
11123
11124 p = get_tv_string(&argvars[0]);
11125 rettv->vval.v_string = vim_strsave(p);
11126 simplify_filename(rettv->vval.v_string); /* simplify in place */
11127 rettv->v_type = VAR_STRING;
11128}
11129
11130#ifdef FEAT_FLOAT
11131/*
11132 * "sin()" function
11133 */
11134 static void
11135f_sin(typval_T *argvars, typval_T *rettv)
11136{
11137 float_T f = 0.0;
11138
11139 rettv->v_type = VAR_FLOAT;
11140 if (get_float_arg(argvars, &f) == OK)
11141 rettv->vval.v_float = sin(f);
11142 else
11143 rettv->vval.v_float = 0.0;
11144}
11145
11146/*
11147 * "sinh()" function
11148 */
11149 static void
11150f_sinh(typval_T *argvars, typval_T *rettv)
11151{
11152 float_T f = 0.0;
11153
11154 rettv->v_type = VAR_FLOAT;
11155 if (get_float_arg(argvars, &f) == OK)
11156 rettv->vval.v_float = sinh(f);
11157 else
11158 rettv->vval.v_float = 0.0;
11159}
11160#endif
11161
11162static int
11163#ifdef __BORLANDC__
11164 _RTLENTRYF
11165#endif
11166 item_compare(const void *s1, const void *s2);
11167static int
11168#ifdef __BORLANDC__
11169 _RTLENTRYF
11170#endif
11171 item_compare2(const void *s1, const void *s2);
11172
11173/* struct used in the array that's given to qsort() */
11174typedef struct
11175{
11176 listitem_T *item;
11177 int idx;
11178} sortItem_T;
11179
11180/* struct storing information about current sort */
11181typedef struct
11182{
11183 int item_compare_ic;
11184 int item_compare_numeric;
11185 int item_compare_numbers;
11186#ifdef FEAT_FLOAT
11187 int item_compare_float;
11188#endif
11189 char_u *item_compare_func;
11190 partial_T *item_compare_partial;
11191 dict_T *item_compare_selfdict;
11192 int item_compare_func_err;
11193 int item_compare_keep_zero;
11194} sortinfo_T;
11195static sortinfo_T *sortinfo = NULL;
11196static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
11197#define ITEM_COMPARE_FAIL 999
11198
11199/*
11200 * Compare functions for f_sort() and f_uniq() below.
11201 */
11202 static int
11203#ifdef __BORLANDC__
11204_RTLENTRYF
11205#endif
11206item_compare(const void *s1, const void *s2)
11207{
11208 sortItem_T *si1, *si2;
11209 typval_T *tv1, *tv2;
11210 char_u *p1, *p2;
11211 char_u *tofree1 = NULL, *tofree2 = NULL;
11212 int res;
11213 char_u numbuf1[NUMBUFLEN];
11214 char_u numbuf2[NUMBUFLEN];
11215
11216 si1 = (sortItem_T *)s1;
11217 si2 = (sortItem_T *)s2;
11218 tv1 = &si1->item->li_tv;
11219 tv2 = &si2->item->li_tv;
11220
11221 if (sortinfo->item_compare_numbers)
11222 {
11223 varnumber_T v1 = get_tv_number(tv1);
11224 varnumber_T v2 = get_tv_number(tv2);
11225
11226 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11227 }
11228
11229#ifdef FEAT_FLOAT
11230 if (sortinfo->item_compare_float)
11231 {
11232 float_T v1 = get_tv_float(tv1);
11233 float_T v2 = get_tv_float(tv2);
11234
11235 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11236 }
11237#endif
11238
11239 /* tv2string() puts quotes around a string and allocates memory. Don't do
11240 * that for string variables. Use a single quote when comparing with a
11241 * non-string to do what the docs promise. */
11242 if (tv1->v_type == VAR_STRING)
11243 {
11244 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11245 p1 = (char_u *)"'";
11246 else
11247 p1 = tv1->vval.v_string;
11248 }
11249 else
11250 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
11251 if (tv2->v_type == VAR_STRING)
11252 {
11253 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11254 p2 = (char_u *)"'";
11255 else
11256 p2 = tv2->vval.v_string;
11257 }
11258 else
11259 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
11260 if (p1 == NULL)
11261 p1 = (char_u *)"";
11262 if (p2 == NULL)
11263 p2 = (char_u *)"";
11264 if (!sortinfo->item_compare_numeric)
11265 {
11266 if (sortinfo->item_compare_ic)
11267 res = STRICMP(p1, p2);
11268 else
11269 res = STRCMP(p1, p2);
11270 }
11271 else
11272 {
11273 double n1, n2;
11274 n1 = strtod((char *)p1, (char **)&p1);
11275 n2 = strtod((char *)p2, (char **)&p2);
11276 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
11277 }
11278
11279 /* When the result would be zero, compare the item indexes. Makes the
11280 * sort stable. */
11281 if (res == 0 && !sortinfo->item_compare_keep_zero)
11282 res = si1->idx > si2->idx ? 1 : -1;
11283
11284 vim_free(tofree1);
11285 vim_free(tofree2);
11286 return res;
11287}
11288
11289 static int
11290#ifdef __BORLANDC__
11291_RTLENTRYF
11292#endif
11293item_compare2(const void *s1, const void *s2)
11294{
11295 sortItem_T *si1, *si2;
11296 int res;
11297 typval_T rettv;
11298 typval_T argv[3];
11299 int dummy;
11300 char_u *func_name;
11301 partial_T *partial = sortinfo->item_compare_partial;
11302
11303 /* shortcut after failure in previous call; compare all items equal */
11304 if (sortinfo->item_compare_func_err)
11305 return 0;
11306
11307 si1 = (sortItem_T *)s1;
11308 si2 = (sortItem_T *)s2;
11309
11310 if (partial == NULL)
11311 func_name = sortinfo->item_compare_func;
11312 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020011313 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011314
11315 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
11316 * in the copy without changing the original list items. */
11317 copy_tv(&si1->item->li_tv, &argv[0]);
11318 copy_tv(&si2->item->li_tv, &argv[1]);
11319
11320 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
11321 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020011322 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011323 partial, sortinfo->item_compare_selfdict);
11324 clear_tv(&argv[0]);
11325 clear_tv(&argv[1]);
11326
11327 if (res == FAIL)
11328 res = ITEM_COMPARE_FAIL;
11329 else
11330 res = (int)get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
11331 if (sortinfo->item_compare_func_err)
11332 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
11333 clear_tv(&rettv);
11334
11335 /* When the result would be zero, compare the pointers themselves. Makes
11336 * the sort stable. */
11337 if (res == 0 && !sortinfo->item_compare_keep_zero)
11338 res = si1->idx > si2->idx ? 1 : -1;
11339
11340 return res;
11341}
11342
11343/*
11344 * "sort({list})" function
11345 */
11346 static void
11347do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
11348{
11349 list_T *l;
11350 listitem_T *li;
11351 sortItem_T *ptrs;
11352 sortinfo_T *old_sortinfo;
11353 sortinfo_T info;
11354 long len;
11355 long i;
11356
11357 /* Pointer to current info struct used in compare function. Save and
11358 * restore the current one for nested calls. */
11359 old_sortinfo = sortinfo;
11360 sortinfo = &info;
11361
11362 if (argvars[0].v_type != VAR_LIST)
11363 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
11364 else
11365 {
11366 l = argvars[0].vval.v_list;
11367 if (l == NULL || tv_check_lock(l->lv_lock,
11368 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
11369 TRUE))
11370 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011371 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011372
11373 len = list_len(l);
11374 if (len <= 1)
11375 goto theend; /* short list sorts pretty quickly */
11376
11377 info.item_compare_ic = FALSE;
11378 info.item_compare_numeric = FALSE;
11379 info.item_compare_numbers = FALSE;
11380#ifdef FEAT_FLOAT
11381 info.item_compare_float = FALSE;
11382#endif
11383 info.item_compare_func = NULL;
11384 info.item_compare_partial = NULL;
11385 info.item_compare_selfdict = NULL;
11386 if (argvars[1].v_type != VAR_UNKNOWN)
11387 {
11388 /* optional second argument: {func} */
11389 if (argvars[1].v_type == VAR_FUNC)
11390 info.item_compare_func = argvars[1].vval.v_string;
11391 else if (argvars[1].v_type == VAR_PARTIAL)
11392 info.item_compare_partial = argvars[1].vval.v_partial;
11393 else
11394 {
11395 int error = FALSE;
11396
11397 i = (long)get_tv_number_chk(&argvars[1], &error);
11398 if (error)
11399 goto theend; /* type error; errmsg already given */
11400 if (i == 1)
11401 info.item_compare_ic = TRUE;
11402 else if (argvars[1].v_type != VAR_NUMBER)
11403 info.item_compare_func = get_tv_string(&argvars[1]);
11404 else if (i != 0)
11405 {
11406 EMSG(_(e_invarg));
11407 goto theend;
11408 }
11409 if (info.item_compare_func != NULL)
11410 {
11411 if (*info.item_compare_func == NUL)
11412 {
11413 /* empty string means default sort */
11414 info.item_compare_func = NULL;
11415 }
11416 else if (STRCMP(info.item_compare_func, "n") == 0)
11417 {
11418 info.item_compare_func = NULL;
11419 info.item_compare_numeric = TRUE;
11420 }
11421 else if (STRCMP(info.item_compare_func, "N") == 0)
11422 {
11423 info.item_compare_func = NULL;
11424 info.item_compare_numbers = TRUE;
11425 }
11426#ifdef FEAT_FLOAT
11427 else if (STRCMP(info.item_compare_func, "f") == 0)
11428 {
11429 info.item_compare_func = NULL;
11430 info.item_compare_float = TRUE;
11431 }
11432#endif
11433 else if (STRCMP(info.item_compare_func, "i") == 0)
11434 {
11435 info.item_compare_func = NULL;
11436 info.item_compare_ic = TRUE;
11437 }
11438 }
11439 }
11440
11441 if (argvars[2].v_type != VAR_UNKNOWN)
11442 {
11443 /* optional third argument: {dict} */
11444 if (argvars[2].v_type != VAR_DICT)
11445 {
11446 EMSG(_(e_dictreq));
11447 goto theend;
11448 }
11449 info.item_compare_selfdict = argvars[2].vval.v_dict;
11450 }
11451 }
11452
11453 /* Make an array with each entry pointing to an item in the List. */
11454 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
11455 if (ptrs == NULL)
11456 goto theend;
11457
11458 i = 0;
11459 if (sort)
11460 {
11461 /* sort(): ptrs will be the list to sort */
11462 for (li = l->lv_first; li != NULL; li = li->li_next)
11463 {
11464 ptrs[i].item = li;
11465 ptrs[i].idx = i;
11466 ++i;
11467 }
11468
11469 info.item_compare_func_err = FALSE;
11470 info.item_compare_keep_zero = FALSE;
11471 /* test the compare function */
11472 if ((info.item_compare_func != NULL
11473 || info.item_compare_partial != NULL)
11474 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
11475 == ITEM_COMPARE_FAIL)
11476 EMSG(_("E702: Sort compare function failed"));
11477 else
11478 {
11479 /* Sort the array with item pointers. */
11480 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
11481 info.item_compare_func == NULL
11482 && info.item_compare_partial == NULL
11483 ? item_compare : item_compare2);
11484
11485 if (!info.item_compare_func_err)
11486 {
11487 /* Clear the List and append the items in sorted order. */
11488 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
11489 l->lv_len = 0;
11490 for (i = 0; i < len; ++i)
11491 list_append(l, ptrs[i].item);
11492 }
11493 }
11494 }
11495 else
11496 {
11497 int (*item_compare_func_ptr)(const void *, const void *);
11498
11499 /* f_uniq(): ptrs will be a stack of items to remove */
11500 info.item_compare_func_err = FALSE;
11501 info.item_compare_keep_zero = TRUE;
11502 item_compare_func_ptr = info.item_compare_func != NULL
11503 || info.item_compare_partial != NULL
11504 ? item_compare2 : item_compare;
11505
11506 for (li = l->lv_first; li != NULL && li->li_next != NULL;
11507 li = li->li_next)
11508 {
11509 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
11510 == 0)
11511 ptrs[i++].item = li;
11512 if (info.item_compare_func_err)
11513 {
11514 EMSG(_("E882: Uniq compare function failed"));
11515 break;
11516 }
11517 }
11518
11519 if (!info.item_compare_func_err)
11520 {
11521 while (--i >= 0)
11522 {
11523 li = ptrs[i].item->li_next;
11524 ptrs[i].item->li_next = li->li_next;
11525 if (li->li_next != NULL)
11526 li->li_next->li_prev = ptrs[i].item;
11527 else
11528 l->lv_last = ptrs[i].item;
11529 list_fix_watch(l, li);
11530 listitem_free(li);
11531 l->lv_len--;
11532 }
11533 }
11534 }
11535
11536 vim_free(ptrs);
11537 }
11538theend:
11539 sortinfo = old_sortinfo;
11540}
11541
11542/*
11543 * "sort({list})" function
11544 */
11545 static void
11546f_sort(typval_T *argvars, typval_T *rettv)
11547{
11548 do_sort_uniq(argvars, rettv, TRUE);
11549}
11550
11551/*
11552 * "uniq({list})" function
11553 */
11554 static void
11555f_uniq(typval_T *argvars, typval_T *rettv)
11556{
11557 do_sort_uniq(argvars, rettv, FALSE);
11558}
11559
11560/*
11561 * "soundfold({word})" function
11562 */
11563 static void
11564f_soundfold(typval_T *argvars, typval_T *rettv)
11565{
11566 char_u *s;
11567
11568 rettv->v_type = VAR_STRING;
11569 s = get_tv_string(&argvars[0]);
11570#ifdef FEAT_SPELL
11571 rettv->vval.v_string = eval_soundfold(s);
11572#else
11573 rettv->vval.v_string = vim_strsave(s);
11574#endif
11575}
11576
11577/*
11578 * "spellbadword()" function
11579 */
11580 static void
11581f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
11582{
11583 char_u *word = (char_u *)"";
11584 hlf_T attr = HLF_COUNT;
11585 int len = 0;
11586
11587 if (rettv_list_alloc(rettv) == FAIL)
11588 return;
11589
11590#ifdef FEAT_SPELL
11591 if (argvars[0].v_type == VAR_UNKNOWN)
11592 {
11593 /* Find the start and length of the badly spelled word. */
11594 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
11595 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011596 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011597 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011598 curwin->w_set_curswant = TRUE;
11599 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011600 }
11601 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
11602 {
11603 char_u *str = get_tv_string_chk(&argvars[0]);
11604 int capcol = -1;
11605
11606 if (str != NULL)
11607 {
11608 /* Check the argument for spelling. */
11609 while (*str != NUL)
11610 {
11611 len = spell_check(curwin, str, &attr, &capcol, FALSE);
11612 if (attr != HLF_COUNT)
11613 {
11614 word = str;
11615 break;
11616 }
11617 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020011618 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011619 }
11620 }
11621 }
11622#endif
11623
11624 list_append_string(rettv->vval.v_list, word, len);
11625 list_append_string(rettv->vval.v_list, (char_u *)(
11626 attr == HLF_SPB ? "bad" :
11627 attr == HLF_SPR ? "rare" :
11628 attr == HLF_SPL ? "local" :
11629 attr == HLF_SPC ? "caps" :
11630 ""), -1);
11631}
11632
11633/*
11634 * "spellsuggest()" function
11635 */
11636 static void
11637f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
11638{
11639#ifdef FEAT_SPELL
11640 char_u *str;
11641 int typeerr = FALSE;
11642 int maxcount;
11643 garray_T ga;
11644 int i;
11645 listitem_T *li;
11646 int need_capital = FALSE;
11647#endif
11648
11649 if (rettv_list_alloc(rettv) == FAIL)
11650 return;
11651
11652#ifdef FEAT_SPELL
11653 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
11654 {
11655 str = get_tv_string(&argvars[0]);
11656 if (argvars[1].v_type != VAR_UNKNOWN)
11657 {
11658 maxcount = (int)get_tv_number_chk(&argvars[1], &typeerr);
11659 if (maxcount <= 0)
11660 return;
11661 if (argvars[2].v_type != VAR_UNKNOWN)
11662 {
11663 need_capital = (int)get_tv_number_chk(&argvars[2], &typeerr);
11664 if (typeerr)
11665 return;
11666 }
11667 }
11668 else
11669 maxcount = 25;
11670
11671 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
11672
11673 for (i = 0; i < ga.ga_len; ++i)
11674 {
11675 str = ((char_u **)ga.ga_data)[i];
11676
11677 li = listitem_alloc();
11678 if (li == NULL)
11679 vim_free(str);
11680 else
11681 {
11682 li->li_tv.v_type = VAR_STRING;
11683 li->li_tv.v_lock = 0;
11684 li->li_tv.vval.v_string = str;
11685 list_append(rettv->vval.v_list, li);
11686 }
11687 }
11688 ga_clear(&ga);
11689 }
11690#endif
11691}
11692
11693 static void
11694f_split(typval_T *argvars, typval_T *rettv)
11695{
11696 char_u *str;
11697 char_u *end;
11698 char_u *pat = NULL;
11699 regmatch_T regmatch;
11700 char_u patbuf[NUMBUFLEN];
11701 char_u *save_cpo;
11702 int match;
11703 colnr_T col = 0;
11704 int keepempty = FALSE;
11705 int typeerr = FALSE;
11706
11707 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11708 save_cpo = p_cpo;
11709 p_cpo = (char_u *)"";
11710
11711 str = get_tv_string(&argvars[0]);
11712 if (argvars[1].v_type != VAR_UNKNOWN)
11713 {
11714 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11715 if (pat == NULL)
11716 typeerr = TRUE;
11717 if (argvars[2].v_type != VAR_UNKNOWN)
11718 keepempty = (int)get_tv_number_chk(&argvars[2], &typeerr);
11719 }
11720 if (pat == NULL || *pat == NUL)
11721 pat = (char_u *)"[\\x01- ]\\+";
11722
11723 if (rettv_list_alloc(rettv) == FAIL)
11724 return;
11725 if (typeerr)
11726 return;
11727
11728 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11729 if (regmatch.regprog != NULL)
11730 {
11731 regmatch.rm_ic = FALSE;
11732 while (*str != NUL || keepempty)
11733 {
11734 if (*str == NUL)
11735 match = FALSE; /* empty item at the end */
11736 else
11737 match = vim_regexec_nl(&regmatch, str, col);
11738 if (match)
11739 end = regmatch.startp[0];
11740 else
11741 end = str + STRLEN(str);
11742 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
11743 && *str != NUL && match && end < regmatch.endp[0]))
11744 {
11745 if (list_append_string(rettv->vval.v_list, str,
11746 (int)(end - str)) == FAIL)
11747 break;
11748 }
11749 if (!match)
11750 break;
11751 /* Advance to just after the match. */
11752 if (regmatch.endp[0] > str)
11753 col = 0;
11754 else
11755 {
11756 /* Don't get stuck at the same match. */
11757#ifdef FEAT_MBYTE
11758 col = (*mb_ptr2len)(regmatch.endp[0]);
11759#else
11760 col = 1;
11761#endif
11762 }
11763 str = regmatch.endp[0];
11764 }
11765
11766 vim_regfree(regmatch.regprog);
11767 }
11768
11769 p_cpo = save_cpo;
11770}
11771
11772#ifdef FEAT_FLOAT
11773/*
11774 * "sqrt()" function
11775 */
11776 static void
11777f_sqrt(typval_T *argvars, typval_T *rettv)
11778{
11779 float_T f = 0.0;
11780
11781 rettv->v_type = VAR_FLOAT;
11782 if (get_float_arg(argvars, &f) == OK)
11783 rettv->vval.v_float = sqrt(f);
11784 else
11785 rettv->vval.v_float = 0.0;
11786}
11787
11788/*
11789 * "str2float()" function
11790 */
11791 static void
11792f_str2float(typval_T *argvars, typval_T *rettv)
11793{
11794 char_u *p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011795 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011796
Bram Moolenaar08243d22017-01-10 16:12:29 +010011797 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011798 p = skipwhite(p + 1);
11799 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011800 if (isneg)
11801 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011802 rettv->v_type = VAR_FLOAT;
11803}
11804#endif
11805
11806/*
11807 * "str2nr()" function
11808 */
11809 static void
11810f_str2nr(typval_T *argvars, typval_T *rettv)
11811{
11812 int base = 10;
11813 char_u *p;
11814 varnumber_T n;
11815 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010011816 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011817
11818 if (argvars[1].v_type != VAR_UNKNOWN)
11819 {
11820 base = (int)get_tv_number(&argvars[1]);
11821 if (base != 2 && base != 8 && base != 10 && base != 16)
11822 {
11823 EMSG(_(e_invarg));
11824 return;
11825 }
11826 }
11827
11828 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011829 isneg = (*p == '-');
11830 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011831 p = skipwhite(p + 1);
11832 switch (base)
11833 {
11834 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
11835 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
11836 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
11837 default: what = 0;
11838 }
11839 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011840 if (isneg)
11841 rettv->vval.v_number = -n;
11842 else
11843 rettv->vval.v_number = n;
11844
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011845}
11846
11847#ifdef HAVE_STRFTIME
11848/*
11849 * "strftime({format}[, {time}])" function
11850 */
11851 static void
11852f_strftime(typval_T *argvars, typval_T *rettv)
11853{
11854 char_u result_buf[256];
11855 struct tm *curtime;
11856 time_t seconds;
11857 char_u *p;
11858
11859 rettv->v_type = VAR_STRING;
11860
11861 p = get_tv_string(&argvars[0]);
11862 if (argvars[1].v_type == VAR_UNKNOWN)
11863 seconds = time(NULL);
11864 else
11865 seconds = (time_t)get_tv_number(&argvars[1]);
11866 curtime = localtime(&seconds);
11867 /* MSVC returns NULL for an invalid value of seconds. */
11868 if (curtime == NULL)
11869 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
11870 else
11871 {
11872# ifdef FEAT_MBYTE
11873 vimconv_T conv;
11874 char_u *enc;
11875
11876 conv.vc_type = CONV_NONE;
11877 enc = enc_locale();
11878 convert_setup(&conv, p_enc, enc);
11879 if (conv.vc_type != CONV_NONE)
11880 p = string_convert(&conv, p, NULL);
11881# endif
11882 if (p != NULL)
11883 (void)strftime((char *)result_buf, sizeof(result_buf),
11884 (char *)p, curtime);
11885 else
11886 result_buf[0] = NUL;
11887
11888# ifdef FEAT_MBYTE
11889 if (conv.vc_type != CONV_NONE)
11890 vim_free(p);
11891 convert_setup(&conv, enc, p_enc);
11892 if (conv.vc_type != CONV_NONE)
11893 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
11894 else
11895# endif
11896 rettv->vval.v_string = vim_strsave(result_buf);
11897
11898# ifdef FEAT_MBYTE
11899 /* Release conversion descriptors */
11900 convert_setup(&conv, NULL, NULL);
11901 vim_free(enc);
11902# endif
11903 }
11904}
11905#endif
11906
11907/*
11908 * "strgetchar()" function
11909 */
11910 static void
11911f_strgetchar(typval_T *argvars, typval_T *rettv)
11912{
11913 char_u *str;
11914 int len;
11915 int error = FALSE;
11916 int charidx;
11917
11918 rettv->vval.v_number = -1;
11919 str = get_tv_string_chk(&argvars[0]);
11920 if (str == NULL)
11921 return;
11922 len = (int)STRLEN(str);
11923 charidx = (int)get_tv_number_chk(&argvars[1], &error);
11924 if (error)
11925 return;
11926#ifdef FEAT_MBYTE
11927 {
11928 int byteidx = 0;
11929
11930 while (charidx >= 0 && byteidx < len)
11931 {
11932 if (charidx == 0)
11933 {
11934 rettv->vval.v_number = mb_ptr2char(str + byteidx);
11935 break;
11936 }
11937 --charidx;
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011938 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011939 }
11940 }
11941#else
11942 if (charidx < len)
11943 rettv->vval.v_number = str[charidx];
11944#endif
11945}
11946
11947/*
11948 * "stridx()" function
11949 */
11950 static void
11951f_stridx(typval_T *argvars, typval_T *rettv)
11952{
11953 char_u buf[NUMBUFLEN];
11954 char_u *needle;
11955 char_u *haystack;
11956 char_u *save_haystack;
11957 char_u *pos;
11958 int start_idx;
11959
11960 needle = get_tv_string_chk(&argvars[1]);
11961 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
11962 rettv->vval.v_number = -1;
11963 if (needle == NULL || haystack == NULL)
11964 return; /* type error; errmsg already given */
11965
11966 if (argvars[2].v_type != VAR_UNKNOWN)
11967 {
11968 int error = FALSE;
11969
11970 start_idx = (int)get_tv_number_chk(&argvars[2], &error);
11971 if (error || start_idx >= (int)STRLEN(haystack))
11972 return;
11973 if (start_idx >= 0)
11974 haystack += start_idx;
11975 }
11976
11977 pos = (char_u *)strstr((char *)haystack, (char *)needle);
11978 if (pos != NULL)
11979 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
11980}
11981
11982/*
11983 * "string()" function
11984 */
11985 static void
11986f_string(typval_T *argvars, typval_T *rettv)
11987{
11988 char_u *tofree;
11989 char_u numbuf[NUMBUFLEN];
11990
11991 rettv->v_type = VAR_STRING;
11992 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
11993 get_copyID());
11994 /* Make a copy if we have a value but it's not in allocated memory. */
11995 if (rettv->vval.v_string != NULL && tofree == NULL)
11996 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
11997}
11998
11999/*
12000 * "strlen()" function
12001 */
12002 static void
12003f_strlen(typval_T *argvars, typval_T *rettv)
12004{
12005 rettv->vval.v_number = (varnumber_T)(STRLEN(
12006 get_tv_string(&argvars[0])));
12007}
12008
12009/*
12010 * "strchars()" function
12011 */
12012 static void
12013f_strchars(typval_T *argvars, typval_T *rettv)
12014{
12015 char_u *s = get_tv_string(&argvars[0]);
12016 int skipcc = 0;
12017#ifdef FEAT_MBYTE
12018 varnumber_T len = 0;
12019 int (*func_mb_ptr2char_adv)(char_u **pp);
12020#endif
12021
12022 if (argvars[1].v_type != VAR_UNKNOWN)
12023 skipcc = (int)get_tv_number_chk(&argvars[1], NULL);
12024 if (skipcc < 0 || skipcc > 1)
12025 EMSG(_(e_invarg));
12026 else
12027 {
12028#ifdef FEAT_MBYTE
12029 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
12030 while (*s != NUL)
12031 {
12032 func_mb_ptr2char_adv(&s);
12033 ++len;
12034 }
12035 rettv->vval.v_number = len;
12036#else
12037 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
12038#endif
12039 }
12040}
12041
12042/*
12043 * "strdisplaywidth()" function
12044 */
12045 static void
12046f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
12047{
12048 char_u *s = get_tv_string(&argvars[0]);
12049 int col = 0;
12050
12051 if (argvars[1].v_type != VAR_UNKNOWN)
12052 col = (int)get_tv_number(&argvars[1]);
12053
12054 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
12055}
12056
12057/*
12058 * "strwidth()" function
12059 */
12060 static void
12061f_strwidth(typval_T *argvars, typval_T *rettv)
12062{
12063 char_u *s = get_tv_string(&argvars[0]);
12064
12065 rettv->vval.v_number = (varnumber_T)(
12066#ifdef FEAT_MBYTE
12067 mb_string2cells(s, -1)
12068#else
12069 STRLEN(s)
12070#endif
12071 );
12072}
12073
12074/*
12075 * "strcharpart()" function
12076 */
12077 static void
12078f_strcharpart(typval_T *argvars, typval_T *rettv)
12079{
12080#ifdef FEAT_MBYTE
12081 char_u *p;
12082 int nchar;
12083 int nbyte = 0;
12084 int charlen;
12085 int len = 0;
12086 int slen;
12087 int error = FALSE;
12088
12089 p = get_tv_string(&argvars[0]);
12090 slen = (int)STRLEN(p);
12091
12092 nchar = (int)get_tv_number_chk(&argvars[1], &error);
12093 if (!error)
12094 {
12095 if (nchar > 0)
12096 while (nchar > 0 && nbyte < slen)
12097 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012098 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012099 --nchar;
12100 }
12101 else
12102 nbyte = nchar;
12103 if (argvars[2].v_type != VAR_UNKNOWN)
12104 {
12105 charlen = (int)get_tv_number(&argvars[2]);
12106 while (charlen > 0 && nbyte + len < slen)
12107 {
12108 int off = nbyte + len;
12109
12110 if (off < 0)
12111 len += 1;
12112 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012113 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012114 --charlen;
12115 }
12116 }
12117 else
12118 len = slen - nbyte; /* default: all bytes that are available. */
12119 }
12120
12121 /*
12122 * Only return the overlap between the specified part and the actual
12123 * string.
12124 */
12125 if (nbyte < 0)
12126 {
12127 len += nbyte;
12128 nbyte = 0;
12129 }
12130 else if (nbyte > slen)
12131 nbyte = slen;
12132 if (len < 0)
12133 len = 0;
12134 else if (nbyte + len > slen)
12135 len = slen - nbyte;
12136
12137 rettv->v_type = VAR_STRING;
12138 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
12139#else
12140 f_strpart(argvars, rettv);
12141#endif
12142}
12143
12144/*
12145 * "strpart()" function
12146 */
12147 static void
12148f_strpart(typval_T *argvars, typval_T *rettv)
12149{
12150 char_u *p;
12151 int n;
12152 int len;
12153 int slen;
12154 int error = FALSE;
12155
12156 p = get_tv_string(&argvars[0]);
12157 slen = (int)STRLEN(p);
12158
12159 n = (int)get_tv_number_chk(&argvars[1], &error);
12160 if (error)
12161 len = 0;
12162 else if (argvars[2].v_type != VAR_UNKNOWN)
12163 len = (int)get_tv_number(&argvars[2]);
12164 else
12165 len = slen - n; /* default len: all bytes that are available. */
12166
12167 /*
12168 * Only return the overlap between the specified part and the actual
12169 * string.
12170 */
12171 if (n < 0)
12172 {
12173 len += n;
12174 n = 0;
12175 }
12176 else if (n > slen)
12177 n = slen;
12178 if (len < 0)
12179 len = 0;
12180 else if (n + len > slen)
12181 len = slen - n;
12182
12183 rettv->v_type = VAR_STRING;
12184 rettv->vval.v_string = vim_strnsave(p + n, len);
12185}
12186
12187/*
12188 * "strridx()" function
12189 */
12190 static void
12191f_strridx(typval_T *argvars, typval_T *rettv)
12192{
12193 char_u buf[NUMBUFLEN];
12194 char_u *needle;
12195 char_u *haystack;
12196 char_u *rest;
12197 char_u *lastmatch = NULL;
12198 int haystack_len, end_idx;
12199
12200 needle = get_tv_string_chk(&argvars[1]);
12201 haystack = get_tv_string_buf_chk(&argvars[0], buf);
12202
12203 rettv->vval.v_number = -1;
12204 if (needle == NULL || haystack == NULL)
12205 return; /* type error; errmsg already given */
12206
12207 haystack_len = (int)STRLEN(haystack);
12208 if (argvars[2].v_type != VAR_UNKNOWN)
12209 {
12210 /* Third argument: upper limit for index */
12211 end_idx = (int)get_tv_number_chk(&argvars[2], NULL);
12212 if (end_idx < 0)
12213 return; /* can never find a match */
12214 }
12215 else
12216 end_idx = haystack_len;
12217
12218 if (*needle == NUL)
12219 {
12220 /* Empty string matches past the end. */
12221 lastmatch = haystack + end_idx;
12222 }
12223 else
12224 {
12225 for (rest = haystack; *rest != '\0'; ++rest)
12226 {
12227 rest = (char_u *)strstr((char *)rest, (char *)needle);
12228 if (rest == NULL || rest > haystack + end_idx)
12229 break;
12230 lastmatch = rest;
12231 }
12232 }
12233
12234 if (lastmatch == NULL)
12235 rettv->vval.v_number = -1;
12236 else
12237 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
12238}
12239
12240/*
12241 * "strtrans()" function
12242 */
12243 static void
12244f_strtrans(typval_T *argvars, typval_T *rettv)
12245{
12246 rettv->v_type = VAR_STRING;
12247 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
12248}
12249
12250/*
12251 * "submatch()" function
12252 */
12253 static void
12254f_submatch(typval_T *argvars, typval_T *rettv)
12255{
12256 int error = FALSE;
12257 int no;
12258 int retList = 0;
12259
12260 no = (int)get_tv_number_chk(&argvars[0], &error);
12261 if (error)
12262 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012263 if (no < 0 || no >= NSUBEXP)
12264 {
Bram Moolenaar79518e22017-02-17 16:31:35 +010012265 EMSGN(_("E935: invalid submatch number: %d"), no);
12266 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012267 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012268 if (argvars[1].v_type != VAR_UNKNOWN)
12269 retList = (int)get_tv_number_chk(&argvars[1], &error);
12270 if (error)
12271 return;
12272
12273 if (retList == 0)
12274 {
12275 rettv->v_type = VAR_STRING;
12276 rettv->vval.v_string = reg_submatch(no);
12277 }
12278 else
12279 {
12280 rettv->v_type = VAR_LIST;
12281 rettv->vval.v_list = reg_submatch_list(no);
12282 }
12283}
12284
12285/*
12286 * "substitute()" function
12287 */
12288 static void
12289f_substitute(typval_T *argvars, typval_T *rettv)
12290{
12291 char_u patbuf[NUMBUFLEN];
12292 char_u subbuf[NUMBUFLEN];
12293 char_u flagsbuf[NUMBUFLEN];
12294
12295 char_u *str = get_tv_string_chk(&argvars[0]);
12296 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012297 char_u *sub = NULL;
12298 typval_T *expr = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012299 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
12300
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012301 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
12302 expr = &argvars[2];
12303 else
12304 sub = get_tv_string_buf_chk(&argvars[2], subbuf);
12305
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012306 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012307 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
12308 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012309 rettv->vval.v_string = NULL;
12310 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012311 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012312}
12313
12314/*
12315 * "synID(lnum, col, trans)" function
12316 */
12317 static void
12318f_synID(typval_T *argvars UNUSED, typval_T *rettv)
12319{
12320 int id = 0;
12321#ifdef FEAT_SYN_HL
12322 linenr_T lnum;
12323 colnr_T col;
12324 int trans;
12325 int transerr = FALSE;
12326
12327 lnum = get_tv_lnum(argvars); /* -1 on type error */
12328 col = (linenr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12329 trans = (int)get_tv_number_chk(&argvars[2], &transerr);
12330
12331 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12332 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
12333 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
12334#endif
12335
12336 rettv->vval.v_number = id;
12337}
12338
12339/*
12340 * "synIDattr(id, what [, mode])" function
12341 */
12342 static void
12343f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
12344{
12345 char_u *p = NULL;
12346#ifdef FEAT_SYN_HL
12347 int id;
12348 char_u *what;
12349 char_u *mode;
12350 char_u modebuf[NUMBUFLEN];
12351 int modec;
12352
12353 id = (int)get_tv_number(&argvars[0]);
12354 what = get_tv_string(&argvars[1]);
12355 if (argvars[2].v_type != VAR_UNKNOWN)
12356 {
12357 mode = get_tv_string_buf(&argvars[2], modebuf);
12358 modec = TOLOWER_ASC(mode[0]);
12359 if (modec != 't' && modec != 'c' && modec != 'g')
12360 modec = 0; /* replace invalid with current */
12361 }
12362 else
12363 {
12364#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
12365 if (USE_24BIT)
12366 modec = 'g';
12367 else
12368#endif
12369 if (t_colors > 1)
12370 modec = 'c';
12371 else
12372 modec = 't';
12373 }
12374
12375
12376 switch (TOLOWER_ASC(what[0]))
12377 {
12378 case 'b':
12379 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
12380 p = highlight_color(id, what, modec);
12381 else /* bold */
12382 p = highlight_has_attr(id, HL_BOLD, modec);
12383 break;
12384
12385 case 'f': /* fg[#] or font */
12386 p = highlight_color(id, what, modec);
12387 break;
12388
12389 case 'i':
12390 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
12391 p = highlight_has_attr(id, HL_INVERSE, modec);
12392 else /* italic */
12393 p = highlight_has_attr(id, HL_ITALIC, modec);
12394 break;
12395
12396 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020012397 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012398 break;
12399
12400 case 'r': /* reverse */
12401 p = highlight_has_attr(id, HL_INVERSE, modec);
12402 break;
12403
12404 case 's':
12405 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
12406 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020012407 /* strikeout */
12408 else if (TOLOWER_ASC(what[1]) == 't' &&
12409 TOLOWER_ASC(what[2]) == 'r')
12410 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012411 else /* standout */
12412 p = highlight_has_attr(id, HL_STANDOUT, modec);
12413 break;
12414
12415 case 'u':
12416 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
12417 /* underline */
12418 p = highlight_has_attr(id, HL_UNDERLINE, modec);
12419 else
12420 /* undercurl */
12421 p = highlight_has_attr(id, HL_UNDERCURL, modec);
12422 break;
12423 }
12424
12425 if (p != NULL)
12426 p = vim_strsave(p);
12427#endif
12428 rettv->v_type = VAR_STRING;
12429 rettv->vval.v_string = p;
12430}
12431
12432/*
12433 * "synIDtrans(id)" function
12434 */
12435 static void
12436f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
12437{
12438 int id;
12439
12440#ifdef FEAT_SYN_HL
12441 id = (int)get_tv_number(&argvars[0]);
12442
12443 if (id > 0)
12444 id = syn_get_final_id(id);
12445 else
12446#endif
12447 id = 0;
12448
12449 rettv->vval.v_number = id;
12450}
12451
12452/*
12453 * "synconcealed(lnum, col)" function
12454 */
12455 static void
12456f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
12457{
12458#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12459 linenr_T lnum;
12460 colnr_T col;
12461 int syntax_flags = 0;
12462 int cchar;
12463 int matchid = 0;
12464 char_u str[NUMBUFLEN];
12465#endif
12466
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012467 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012468
12469#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12470 lnum = get_tv_lnum(argvars); /* -1 on type error */
12471 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12472
12473 vim_memset(str, NUL, sizeof(str));
12474
12475 if (rettv_list_alloc(rettv) != FAIL)
12476 {
12477 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12478 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12479 && curwin->w_p_cole > 0)
12480 {
12481 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
12482 syntax_flags = get_syntax_info(&matchid);
12483
12484 /* get the conceal character */
12485 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
12486 {
12487 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020012488 if (cchar == NUL && curwin->w_p_cole == 1)
12489 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012490 if (cchar != NUL)
12491 {
12492# ifdef FEAT_MBYTE
12493 if (has_mbyte)
12494 (*mb_char2bytes)(cchar, str);
12495 else
12496# endif
12497 str[0] = cchar;
12498 }
12499 }
12500 }
12501
12502 list_append_number(rettv->vval.v_list,
12503 (syntax_flags & HL_CONCEAL) != 0);
12504 /* -1 to auto-determine strlen */
12505 list_append_string(rettv->vval.v_list, str, -1);
12506 list_append_number(rettv->vval.v_list, matchid);
12507 }
12508#endif
12509}
12510
12511/*
12512 * "synstack(lnum, col)" function
12513 */
12514 static void
12515f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
12516{
12517#ifdef FEAT_SYN_HL
12518 linenr_T lnum;
12519 colnr_T col;
12520 int i;
12521 int id;
12522#endif
12523
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012524 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012525
12526#ifdef FEAT_SYN_HL
12527 lnum = get_tv_lnum(argvars); /* -1 on type error */
12528 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12529
12530 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12531 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12532 && rettv_list_alloc(rettv) != FAIL)
12533 {
12534 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
12535 for (i = 0; ; ++i)
12536 {
12537 id = syn_get_stack_item(i);
12538 if (id < 0)
12539 break;
12540 if (list_append_number(rettv->vval.v_list, id) == FAIL)
12541 break;
12542 }
12543 }
12544#endif
12545}
12546
12547 static void
12548get_cmd_output_as_rettv(
12549 typval_T *argvars,
12550 typval_T *rettv,
12551 int retlist)
12552{
12553 char_u *res = NULL;
12554 char_u *p;
12555 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012556 int err = FALSE;
12557 FILE *fd;
12558 list_T *list = NULL;
12559 int flags = SHELL_SILENT;
12560
12561 rettv->v_type = VAR_STRING;
12562 rettv->vval.v_string = NULL;
12563 if (check_restricted() || check_secure())
12564 goto errret;
12565
12566 if (argvars[1].v_type != VAR_UNKNOWN)
12567 {
12568 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010012569 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012570 * command.
12571 */
12572 if ((infile = vim_tempname('i', TRUE)) == NULL)
12573 {
12574 EMSG(_(e_notmp));
12575 goto errret;
12576 }
12577
12578 fd = mch_fopen((char *)infile, WRITEBIN);
12579 if (fd == NULL)
12580 {
12581 EMSG2(_(e_notopen), infile);
12582 goto errret;
12583 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010012584 if (argvars[1].v_type == VAR_NUMBER)
12585 {
12586 linenr_T lnum;
12587 buf_T *buf;
12588
12589 buf = buflist_findnr(argvars[1].vval.v_number);
12590 if (buf == NULL)
12591 {
12592 EMSGN(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010012593 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010012594 goto errret;
12595 }
12596
12597 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
12598 {
12599 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
12600 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
12601 {
12602 err = TRUE;
12603 break;
12604 }
12605 if (putc(NL, fd) == EOF)
12606 {
12607 err = TRUE;
12608 break;
12609 }
12610 }
12611 }
12612 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012613 {
12614 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
12615 err = TRUE;
12616 }
12617 else
12618 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010012619 size_t len;
12620 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012621
12622 p = get_tv_string_buf_chk(&argvars[1], buf);
12623 if (p == NULL)
12624 {
12625 fclose(fd);
12626 goto errret; /* type error; errmsg already given */
12627 }
12628 len = STRLEN(p);
12629 if (len > 0 && fwrite(p, len, 1, fd) != 1)
12630 err = TRUE;
12631 }
12632 if (fclose(fd) != 0)
12633 err = TRUE;
12634 if (err)
12635 {
12636 EMSG(_("E677: Error writing temp file"));
12637 goto errret;
12638 }
12639 }
12640
12641 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
12642 * echoes typeahead, that messes up the display. */
12643 if (!msg_silent)
12644 flags += SHELL_COOKED;
12645
12646 if (retlist)
12647 {
12648 int len;
12649 listitem_T *li;
12650 char_u *s = NULL;
12651 char_u *start;
12652 char_u *end;
12653 int i;
12654
12655 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
12656 if (res == NULL)
12657 goto errret;
12658
12659 list = list_alloc();
12660 if (list == NULL)
12661 goto errret;
12662
12663 for (i = 0; i < len; ++i)
12664 {
12665 start = res + i;
12666 while (i < len && res[i] != NL)
12667 ++i;
12668 end = res + i;
12669
12670 s = alloc((unsigned)(end - start + 1));
12671 if (s == NULL)
12672 goto errret;
12673
12674 for (p = s; start < end; ++p, ++start)
12675 *p = *start == NUL ? NL : *start;
12676 *p = NUL;
12677
12678 li = listitem_alloc();
12679 if (li == NULL)
12680 {
12681 vim_free(s);
12682 goto errret;
12683 }
12684 li->li_tv.v_type = VAR_STRING;
12685 li->li_tv.v_lock = 0;
12686 li->li_tv.vval.v_string = s;
12687 list_append(list, li);
12688 }
12689
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012690 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012691 list = NULL;
12692 }
12693 else
12694 {
12695 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
12696#ifdef USE_CR
12697 /* translate <CR> into <NL> */
12698 if (res != NULL)
12699 {
12700 char_u *s;
12701
12702 for (s = res; *s; ++s)
12703 {
12704 if (*s == CAR)
12705 *s = NL;
12706 }
12707 }
12708#else
12709# ifdef USE_CRNL
12710 /* translate <CR><NL> into <NL> */
12711 if (res != NULL)
12712 {
12713 char_u *s, *d;
12714
12715 d = res;
12716 for (s = res; *s; ++s)
12717 {
12718 if (s[0] == CAR && s[1] == NL)
12719 ++s;
12720 *d++ = *s;
12721 }
12722 *d = NUL;
12723 }
12724# endif
12725#endif
12726 rettv->vval.v_string = res;
12727 res = NULL;
12728 }
12729
12730errret:
12731 if (infile != NULL)
12732 {
12733 mch_remove(infile);
12734 vim_free(infile);
12735 }
12736 if (res != NULL)
12737 vim_free(res);
12738 if (list != NULL)
12739 list_free(list);
12740}
12741
12742/*
12743 * "system()" function
12744 */
12745 static void
12746f_system(typval_T *argvars, typval_T *rettv)
12747{
12748 get_cmd_output_as_rettv(argvars, rettv, FALSE);
12749}
12750
12751/*
12752 * "systemlist()" function
12753 */
12754 static void
12755f_systemlist(typval_T *argvars, typval_T *rettv)
12756{
12757 get_cmd_output_as_rettv(argvars, rettv, TRUE);
12758}
12759
12760/*
12761 * "tabpagebuflist()" function
12762 */
12763 static void
12764f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12765{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012766 tabpage_T *tp;
12767 win_T *wp = NULL;
12768
12769 if (argvars[0].v_type == VAR_UNKNOWN)
12770 wp = firstwin;
12771 else
12772 {
12773 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12774 if (tp != NULL)
12775 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12776 }
12777 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
12778 {
12779 for (; wp != NULL; wp = wp->w_next)
12780 if (list_append_number(rettv->vval.v_list,
12781 wp->w_buffer->b_fnum) == FAIL)
12782 break;
12783 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012784}
12785
12786
12787/*
12788 * "tabpagenr()" function
12789 */
12790 static void
12791f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
12792{
12793 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012794 char_u *arg;
12795
12796 if (argvars[0].v_type != VAR_UNKNOWN)
12797 {
12798 arg = get_tv_string_chk(&argvars[0]);
12799 nr = 0;
12800 if (arg != NULL)
12801 {
12802 if (STRCMP(arg, "$") == 0)
12803 nr = tabpage_index(NULL) - 1;
12804 else
12805 EMSG2(_(e_invexpr2), arg);
12806 }
12807 }
12808 else
12809 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012810 rettv->vval.v_number = nr;
12811}
12812
12813
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012814static int get_winnr(tabpage_T *tp, typval_T *argvar);
12815
12816/*
12817 * Common code for tabpagewinnr() and winnr().
12818 */
12819 static int
12820get_winnr(tabpage_T *tp, typval_T *argvar)
12821{
12822 win_T *twin;
12823 int nr = 1;
12824 win_T *wp;
12825 char_u *arg;
12826
12827 twin = (tp == curtab) ? curwin : tp->tp_curwin;
12828 if (argvar->v_type != VAR_UNKNOWN)
12829 {
12830 arg = get_tv_string_chk(argvar);
12831 if (arg == NULL)
12832 nr = 0; /* type error; errmsg already given */
12833 else if (STRCMP(arg, "$") == 0)
12834 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
12835 else if (STRCMP(arg, "#") == 0)
12836 {
12837 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
12838 if (twin == NULL)
12839 nr = 0;
12840 }
12841 else
12842 {
12843 EMSG2(_(e_invexpr2), arg);
12844 nr = 0;
12845 }
12846 }
12847
12848 if (nr > 0)
12849 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12850 wp != twin; wp = wp->w_next)
12851 {
12852 if (wp == NULL)
12853 {
12854 /* didn't find it in this tabpage */
12855 nr = 0;
12856 break;
12857 }
12858 ++nr;
12859 }
12860 return nr;
12861}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012862
12863/*
12864 * "tabpagewinnr()" function
12865 */
12866 static void
12867f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
12868{
12869 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012870 tabpage_T *tp;
12871
12872 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12873 if (tp == NULL)
12874 nr = 0;
12875 else
12876 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012877 rettv->vval.v_number = nr;
12878}
12879
12880
12881/*
12882 * "tagfiles()" function
12883 */
12884 static void
12885f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
12886{
12887 char_u *fname;
12888 tagname_T tn;
12889 int first;
12890
12891 if (rettv_list_alloc(rettv) == FAIL)
12892 return;
12893 fname = alloc(MAXPATHL);
12894 if (fname == NULL)
12895 return;
12896
12897 for (first = TRUE; ; first = FALSE)
12898 if (get_tagfname(&tn, first, fname) == FAIL
12899 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
12900 break;
12901 tagname_free(&tn);
12902 vim_free(fname);
12903}
12904
12905/*
12906 * "taglist()" function
12907 */
12908 static void
12909f_taglist(typval_T *argvars, typval_T *rettv)
12910{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012911 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012912 char_u *tag_pattern;
12913
12914 tag_pattern = get_tv_string(&argvars[0]);
12915
12916 rettv->vval.v_number = FALSE;
12917 if (*tag_pattern == NUL)
12918 return;
12919
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012920 if (argvars[1].v_type != VAR_UNKNOWN)
12921 fname = get_tv_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012922 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012923 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012924}
12925
12926/*
12927 * "tempname()" function
12928 */
12929 static void
12930f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
12931{
12932 static int x = 'A';
12933
12934 rettv->v_type = VAR_STRING;
12935 rettv->vval.v_string = vim_tempname(x, FALSE);
12936
12937 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
12938 * names. Skip 'I' and 'O', they are used for shell redirection. */
12939 do
12940 {
12941 if (x == 'Z')
12942 x = '0';
12943 else if (x == '9')
12944 x = 'A';
12945 else
12946 {
12947#ifdef EBCDIC
12948 if (x == 'I')
12949 x = 'J';
12950 else if (x == 'R')
12951 x = 'S';
12952 else
12953#endif
12954 ++x;
12955 }
12956 } while (x == 'I' || x == 'O');
12957}
12958
12959#ifdef FEAT_FLOAT
12960/*
12961 * "tan()" function
12962 */
12963 static void
12964f_tan(typval_T *argvars, typval_T *rettv)
12965{
12966 float_T f = 0.0;
12967
12968 rettv->v_type = VAR_FLOAT;
12969 if (get_float_arg(argvars, &f) == OK)
12970 rettv->vval.v_float = tan(f);
12971 else
12972 rettv->vval.v_float = 0.0;
12973}
12974
12975/*
12976 * "tanh()" function
12977 */
12978 static void
12979f_tanh(typval_T *argvars, typval_T *rettv)
12980{
12981 float_T f = 0.0;
12982
12983 rettv->v_type = VAR_FLOAT;
12984 if (get_float_arg(argvars, &f) == OK)
12985 rettv->vval.v_float = tanh(f);
12986 else
12987 rettv->vval.v_float = 0.0;
12988}
12989#endif
12990
12991/*
12992 * "test_alloc_fail(id, countdown, repeat)" function
12993 */
12994 static void
12995f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
12996{
12997 if (argvars[0].v_type != VAR_NUMBER
12998 || argvars[0].vval.v_number <= 0
12999 || argvars[1].v_type != VAR_NUMBER
13000 || argvars[1].vval.v_number < 0
13001 || argvars[2].v_type != VAR_NUMBER)
13002 EMSG(_(e_invarg));
13003 else
13004 {
13005 alloc_fail_id = argvars[0].vval.v_number;
13006 if (alloc_fail_id >= aid_last)
13007 EMSG(_(e_invarg));
13008 alloc_fail_countdown = argvars[1].vval.v_number;
13009 alloc_fail_repeat = argvars[2].vval.v_number;
13010 did_outofmem_msg = FALSE;
13011 }
13012}
13013
13014/*
13015 * "test_autochdir()"
13016 */
13017 static void
13018f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13019{
13020#if defined(FEAT_AUTOCHDIR)
13021 test_autochdir = TRUE;
13022#endif
13023}
13024
13025/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020013026 * "test_feedinput()"
13027 */
13028 static void
13029f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
13030{
13031#ifdef USE_INPUT_BUF
13032 char_u *val = get_tv_string_chk(&argvars[0]);
13033
13034 if (val != NULL)
13035 {
13036 trash_input_buf();
13037 add_to_input_buf_csi(val, (int)STRLEN(val));
13038 }
13039#endif
13040}
13041
13042/*
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013043 * "test_disable({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013044 */
13045 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013046f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013047{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013048 char_u *name = (char_u *)"";
13049 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013050 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013051
13052 if (argvars[0].v_type != VAR_STRING
13053 || (argvars[1].v_type) != VAR_NUMBER)
13054 EMSG(_(e_invarg));
13055 else
13056 {
13057 name = get_tv_string_chk(&argvars[0]);
13058 val = (int)get_tv_number(&argvars[1]);
13059
13060 if (STRCMP(name, (char_u *)"redraw") == 0)
13061 disable_redraw_for_testing = val;
13062 else if (STRCMP(name, (char_u *)"char_avail") == 0)
13063 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013064 else if (STRCMP(name, (char_u *)"starting") == 0)
13065 {
13066 if (val)
13067 {
13068 if (save_starting < 0)
13069 save_starting = starting;
13070 starting = 0;
13071 }
13072 else
13073 {
13074 starting = save_starting;
13075 save_starting = -1;
13076 }
13077 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013078 else if (STRCMP(name, (char_u *)"nfa_fail") == 0)
13079 nfa_fail_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013080 else if (STRCMP(name, (char_u *)"ALL") == 0)
13081 {
13082 disable_char_avail_for_testing = FALSE;
13083 disable_redraw_for_testing = FALSE;
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013084 nfa_fail_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013085 if (save_starting >= 0)
13086 {
13087 starting = save_starting;
13088 save_starting = -1;
13089 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013090 }
13091 else
13092 EMSG2(_(e_invarg2), name);
13093 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013094}
13095
13096/*
13097 * "test_garbagecollect_now()" function
13098 */
13099 static void
13100f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13101{
13102 /* This is dangerous, any Lists and Dicts used internally may be freed
13103 * while still in use. */
13104 garbage_collect(TRUE);
13105}
13106
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013107/*
13108 * "test_ignore_error()" function
13109 */
13110 static void
13111f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
13112{
13113 ignore_error_for_testing(get_tv_string(&argvars[0]));
13114}
13115
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013116#ifdef FEAT_JOB_CHANNEL
13117 static void
13118f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
13119{
13120 rettv->v_type = VAR_CHANNEL;
13121 rettv->vval.v_channel = NULL;
13122}
13123#endif
13124
13125 static void
13126f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
13127{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013128 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013129}
13130
13131#ifdef FEAT_JOB_CHANNEL
13132 static void
13133f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
13134{
13135 rettv->v_type = VAR_JOB;
13136 rettv->vval.v_job = NULL;
13137}
13138#endif
13139
13140 static void
13141f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
13142{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013143 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013144}
13145
13146 static void
13147f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
13148{
13149 rettv->v_type = VAR_PARTIAL;
13150 rettv->vval.v_partial = NULL;
13151}
13152
13153 static void
13154f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
13155{
13156 rettv->v_type = VAR_STRING;
13157 rettv->vval.v_string = NULL;
13158}
13159
13160 static void
13161f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
13162{
13163 time_for_testing = (time_t)get_tv_number(&argvars[0]);
13164}
13165
13166#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
13167/*
13168 * Get a callback from "arg". It can be a Funcref or a function name.
13169 * When "arg" is zero return an empty string.
13170 * Return NULL for an invalid argument.
13171 */
13172 char_u *
13173get_callback(typval_T *arg, partial_T **pp)
13174{
13175 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
13176 {
13177 *pp = arg->vval.v_partial;
13178 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020013179 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013180 }
13181 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020013182 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013183 {
13184 func_ref(arg->vval.v_string);
13185 return arg->vval.v_string;
13186 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013187 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
13188 return (char_u *)"";
13189 EMSG(_("E921: Invalid callback argument"));
13190 return NULL;
13191}
13192
13193/*
Bram Moolenaard5359b22018-04-05 22:44:39 +020013194 * Unref/free "callback" and "partial" returned by get_callback().
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013195 */
13196 void
13197free_callback(char_u *callback, partial_T *partial)
13198{
13199 if (partial != NULL)
13200 partial_unref(partial);
13201 else if (callback != NULL)
13202 {
13203 func_unref(callback);
13204 vim_free(callback);
13205 }
13206}
13207#endif
13208
13209#ifdef FEAT_TIMERS
13210/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020013211 * "timer_info([timer])" function
13212 */
13213 static void
13214f_timer_info(typval_T *argvars, typval_T *rettv)
13215{
13216 timer_T *timer = NULL;
13217
13218 if (rettv_list_alloc(rettv) != OK)
13219 return;
13220 if (argvars[0].v_type != VAR_UNKNOWN)
13221 {
13222 if (argvars[0].v_type != VAR_NUMBER)
13223 EMSG(_(e_number_exp));
13224 else
13225 {
13226 timer = find_timer((int)get_tv_number(&argvars[0]));
13227 if (timer != NULL)
13228 add_timer_info(rettv, timer);
13229 }
13230 }
13231 else
13232 add_timer_info_all(rettv);
13233}
13234
13235/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013236 * "timer_pause(timer, paused)" function
13237 */
13238 static void
13239f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
13240{
13241 timer_T *timer = NULL;
13242 int paused = (int)get_tv_number(&argvars[1]);
13243
13244 if (argvars[0].v_type != VAR_NUMBER)
13245 EMSG(_(e_number_exp));
13246 else
13247 {
13248 timer = find_timer((int)get_tv_number(&argvars[0]));
13249 if (timer != NULL)
13250 timer->tr_paused = paused;
13251 }
13252}
13253
13254/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013255 * "timer_start(time, callback [, options])" function
13256 */
13257 static void
13258f_timer_start(typval_T *argvars, typval_T *rettv)
13259{
Bram Moolenaar75537a92016-09-05 22:45:28 +020013260 long msec = (long)get_tv_number(&argvars[0]);
13261 timer_T *timer;
13262 int repeat = 0;
13263 char_u *callback;
13264 dict_T *dict;
13265 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013266
Bram Moolenaar75537a92016-09-05 22:45:28 +020013267 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013268 if (check_secure())
13269 return;
13270 if (argvars[2].v_type != VAR_UNKNOWN)
13271 {
13272 if (argvars[2].v_type != VAR_DICT
13273 || (dict = argvars[2].vval.v_dict) == NULL)
13274 {
13275 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
13276 return;
13277 }
13278 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
13279 repeat = get_dict_number(dict, (char_u *)"repeat");
13280 }
13281
Bram Moolenaar75537a92016-09-05 22:45:28 +020013282 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013283 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020013284 return;
13285
13286 timer = create_timer(msec, repeat);
13287 if (timer == NULL)
13288 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013289 else
13290 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020013291 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020013292 timer->tr_callback = vim_strsave(callback);
13293 else
13294 /* pointer into the partial */
13295 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020013296 timer->tr_partial = partial;
13297 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013298 }
13299}
13300
13301/*
13302 * "timer_stop(timer)" function
13303 */
13304 static void
13305f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
13306{
13307 timer_T *timer;
13308
13309 if (argvars[0].v_type != VAR_NUMBER)
13310 {
13311 EMSG(_(e_number_exp));
13312 return;
13313 }
13314 timer = find_timer((int)get_tv_number(&argvars[0]));
13315 if (timer != NULL)
13316 stop_timer(timer);
13317}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013318
13319/*
13320 * "timer_stopall()" function
13321 */
13322 static void
13323f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13324{
13325 stop_all_timers();
13326}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013327#endif
13328
13329/*
13330 * "tolower(string)" function
13331 */
13332 static void
13333f_tolower(typval_T *argvars, typval_T *rettv)
13334{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013335 rettv->v_type = VAR_STRING;
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +010013336 rettv->vval.v_string = strlow_save(get_tv_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013337}
13338
13339/*
13340 * "toupper(string)" function
13341 */
13342 static void
13343f_toupper(typval_T *argvars, typval_T *rettv)
13344{
13345 rettv->v_type = VAR_STRING;
13346 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
13347}
13348
13349/*
13350 * "tr(string, fromstr, tostr)" function
13351 */
13352 static void
13353f_tr(typval_T *argvars, typval_T *rettv)
13354{
13355 char_u *in_str;
13356 char_u *fromstr;
13357 char_u *tostr;
13358 char_u *p;
13359#ifdef FEAT_MBYTE
13360 int inlen;
13361 int fromlen;
13362 int tolen;
13363 int idx;
13364 char_u *cpstr;
13365 int cplen;
13366 int first = TRUE;
13367#endif
13368 char_u buf[NUMBUFLEN];
13369 char_u buf2[NUMBUFLEN];
13370 garray_T ga;
13371
13372 in_str = get_tv_string(&argvars[0]);
13373 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
13374 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
13375
13376 /* Default return value: empty string. */
13377 rettv->v_type = VAR_STRING;
13378 rettv->vval.v_string = NULL;
13379 if (fromstr == NULL || tostr == NULL)
13380 return; /* type error; errmsg already given */
13381 ga_init2(&ga, (int)sizeof(char), 80);
13382
13383#ifdef FEAT_MBYTE
13384 if (!has_mbyte)
13385#endif
13386 /* not multi-byte: fromstr and tostr must be the same length */
13387 if (STRLEN(fromstr) != STRLEN(tostr))
13388 {
13389#ifdef FEAT_MBYTE
13390error:
13391#endif
13392 EMSG2(_(e_invarg2), fromstr);
13393 ga_clear(&ga);
13394 return;
13395 }
13396
13397 /* fromstr and tostr have to contain the same number of chars */
13398 while (*in_str != NUL)
13399 {
13400#ifdef FEAT_MBYTE
13401 if (has_mbyte)
13402 {
13403 inlen = (*mb_ptr2len)(in_str);
13404 cpstr = in_str;
13405 cplen = inlen;
13406 idx = 0;
13407 for (p = fromstr; *p != NUL; p += fromlen)
13408 {
13409 fromlen = (*mb_ptr2len)(p);
13410 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
13411 {
13412 for (p = tostr; *p != NUL; p += tolen)
13413 {
13414 tolen = (*mb_ptr2len)(p);
13415 if (idx-- == 0)
13416 {
13417 cplen = tolen;
13418 cpstr = p;
13419 break;
13420 }
13421 }
13422 if (*p == NUL) /* tostr is shorter than fromstr */
13423 goto error;
13424 break;
13425 }
13426 ++idx;
13427 }
13428
13429 if (first && cpstr == in_str)
13430 {
13431 /* Check that fromstr and tostr have the same number of
13432 * (multi-byte) characters. Done only once when a character
13433 * of in_str doesn't appear in fromstr. */
13434 first = FALSE;
13435 for (p = tostr; *p != NUL; p += tolen)
13436 {
13437 tolen = (*mb_ptr2len)(p);
13438 --idx;
13439 }
13440 if (idx != 0)
13441 goto error;
13442 }
13443
13444 (void)ga_grow(&ga, cplen);
13445 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
13446 ga.ga_len += cplen;
13447
13448 in_str += inlen;
13449 }
13450 else
13451#endif
13452 {
13453 /* When not using multi-byte chars we can do it faster. */
13454 p = vim_strchr(fromstr, *in_str);
13455 if (p != NULL)
13456 ga_append(&ga, tostr[p - fromstr]);
13457 else
13458 ga_append(&ga, *in_str);
13459 ++in_str;
13460 }
13461 }
13462
13463 /* add a terminating NUL */
13464 (void)ga_grow(&ga, 1);
13465 ga_append(&ga, NUL);
13466
13467 rettv->vval.v_string = ga.ga_data;
13468}
13469
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010013470/*
13471 * "trim({expr})" function
13472 */
13473 static void
13474f_trim(typval_T *argvars, typval_T *rettv)
13475{
13476 char_u buf1[NUMBUFLEN];
13477 char_u buf2[NUMBUFLEN];
13478 char_u *head = get_tv_string_buf_chk(&argvars[0], buf1);
13479 char_u *mask = NULL;
13480 char_u *tail;
13481 char_u *prev;
13482 char_u *p;
13483 int c1;
13484
13485 rettv->v_type = VAR_STRING;
13486 if (head == NULL)
13487 {
13488 rettv->vval.v_string = NULL;
13489 return;
13490 }
13491
13492 if (argvars[1].v_type == VAR_STRING)
13493 mask = get_tv_string_buf_chk(&argvars[1], buf2);
13494
13495 while (*head != NUL)
13496 {
13497 c1 = PTR2CHAR(head);
13498 if (mask == NULL)
13499 {
13500 if (c1 > ' ' && c1 != 0xa0)
13501 break;
13502 }
13503 else
13504 {
13505 for (p = mask; *p != NUL; MB_PTR_ADV(p))
13506 if (c1 == PTR2CHAR(p))
13507 break;
13508 if (*p == NUL)
13509 break;
13510 }
13511 MB_PTR_ADV(head);
13512 }
13513
13514 for (tail = head + STRLEN(head); tail > head; tail = prev)
13515 {
13516 prev = tail;
13517 MB_PTR_BACK(head, prev);
13518 c1 = PTR2CHAR(prev);
13519 if (mask == NULL)
13520 {
13521 if (c1 > ' ' && c1 != 0xa0)
13522 break;
13523 }
13524 else
13525 {
13526 for (p = mask; *p != NUL; MB_PTR_ADV(p))
13527 if (c1 == PTR2CHAR(p))
13528 break;
13529 if (*p == NUL)
13530 break;
13531 }
13532 }
13533 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
13534}
13535
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013536#ifdef FEAT_FLOAT
13537/*
13538 * "trunc({float})" function
13539 */
13540 static void
13541f_trunc(typval_T *argvars, typval_T *rettv)
13542{
13543 float_T f = 0.0;
13544
13545 rettv->v_type = VAR_FLOAT;
13546 if (get_float_arg(argvars, &f) == OK)
13547 /* trunc() is not in C90, use floor() or ceil() instead. */
13548 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
13549 else
13550 rettv->vval.v_float = 0.0;
13551}
13552#endif
13553
13554/*
13555 * "type(expr)" function
13556 */
13557 static void
13558f_type(typval_T *argvars, typval_T *rettv)
13559{
13560 int n = -1;
13561
13562 switch (argvars[0].v_type)
13563 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020013564 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
13565 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013566 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020013567 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
13568 case VAR_LIST: n = VAR_TYPE_LIST; break;
13569 case VAR_DICT: n = VAR_TYPE_DICT; break;
13570 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013571 case VAR_SPECIAL:
13572 if (argvars[0].vval.v_number == VVAL_FALSE
13573 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020013574 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013575 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020013576 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013577 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020013578 case VAR_JOB: n = VAR_TYPE_JOB; break;
13579 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013580 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010013581 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013582 n = -1;
13583 break;
13584 }
13585 rettv->vval.v_number = n;
13586}
13587
13588/*
13589 * "undofile(name)" function
13590 */
13591 static void
13592f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
13593{
13594 rettv->v_type = VAR_STRING;
13595#ifdef FEAT_PERSISTENT_UNDO
13596 {
13597 char_u *fname = get_tv_string(&argvars[0]);
13598
13599 if (*fname == NUL)
13600 {
13601 /* If there is no file name there will be no undo file. */
13602 rettv->vval.v_string = NULL;
13603 }
13604 else
13605 {
13606 char_u *ffname = FullName_save(fname, FALSE);
13607
13608 if (ffname != NULL)
13609 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
13610 vim_free(ffname);
13611 }
13612 }
13613#else
13614 rettv->vval.v_string = NULL;
13615#endif
13616}
13617
13618/*
13619 * "undotree()" function
13620 */
13621 static void
13622f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
13623{
13624 if (rettv_dict_alloc(rettv) == OK)
13625 {
13626 dict_T *dict = rettv->vval.v_dict;
13627 list_T *list;
13628
Bram Moolenaare0be1672018-07-08 16:50:37 +020013629 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
13630 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
13631 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
13632 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
13633 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
13634 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013635
13636 list = list_alloc();
13637 if (list != NULL)
13638 {
13639 u_eval_tree(curbuf->b_u_oldhead, list);
13640 dict_add_list(dict, "entries", list);
13641 }
13642 }
13643}
13644
13645/*
13646 * "values(dict)" function
13647 */
13648 static void
13649f_values(typval_T *argvars, typval_T *rettv)
13650{
13651 dict_list(argvars, rettv, 1);
13652}
13653
13654/*
13655 * "virtcol(string)" function
13656 */
13657 static void
13658f_virtcol(typval_T *argvars, typval_T *rettv)
13659{
13660 colnr_T vcol = 0;
13661 pos_T *fp;
13662 int fnum = curbuf->b_fnum;
13663
13664 fp = var2fpos(&argvars[0], FALSE, &fnum);
13665 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
13666 && fnum == curbuf->b_fnum)
13667 {
13668 getvvcol(curwin, fp, NULL, NULL, &vcol);
13669 ++vcol;
13670 }
13671
13672 rettv->vval.v_number = vcol;
13673}
13674
13675/*
13676 * "visualmode()" function
13677 */
13678 static void
13679f_visualmode(typval_T *argvars, typval_T *rettv)
13680{
13681 char_u str[2];
13682
13683 rettv->v_type = VAR_STRING;
13684 str[0] = curbuf->b_visual_mode_eval;
13685 str[1] = NUL;
13686 rettv->vval.v_string = vim_strsave(str);
13687
13688 /* A non-zero number or non-empty string argument: reset mode. */
13689 if (non_zero_arg(&argvars[0]))
13690 curbuf->b_visual_mode_eval = NUL;
13691}
13692
13693/*
13694 * "wildmenumode()" function
13695 */
13696 static void
13697f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13698{
13699#ifdef FEAT_WILDMENU
13700 if (wild_menu_showing)
13701 rettv->vval.v_number = 1;
13702#endif
13703}
13704
13705/*
13706 * "winbufnr(nr)" function
13707 */
13708 static void
13709f_winbufnr(typval_T *argvars, typval_T *rettv)
13710{
13711 win_T *wp;
13712
13713 wp = find_win_by_nr(&argvars[0], NULL);
13714 if (wp == NULL)
13715 rettv->vval.v_number = -1;
13716 else
13717 rettv->vval.v_number = wp->w_buffer->b_fnum;
13718}
13719
13720/*
13721 * "wincol()" function
13722 */
13723 static void
13724f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
13725{
13726 validate_cursor();
13727 rettv->vval.v_number = curwin->w_wcol + 1;
13728}
13729
13730/*
13731 * "winheight(nr)" function
13732 */
13733 static void
13734f_winheight(typval_T *argvars, typval_T *rettv)
13735{
13736 win_T *wp;
13737
13738 wp = find_win_by_nr(&argvars[0], NULL);
13739 if (wp == NULL)
13740 rettv->vval.v_number = -1;
13741 else
13742 rettv->vval.v_number = wp->w_height;
13743}
13744
13745/*
13746 * "winline()" function
13747 */
13748 static void
13749f_winline(typval_T *argvars UNUSED, typval_T *rettv)
13750{
13751 validate_cursor();
13752 rettv->vval.v_number = curwin->w_wrow + 1;
13753}
13754
13755/*
13756 * "winnr()" function
13757 */
13758 static void
13759f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
13760{
13761 int nr = 1;
13762
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013763 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013764 rettv->vval.v_number = nr;
13765}
13766
13767/*
13768 * "winrestcmd()" function
13769 */
13770 static void
13771f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
13772{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013773 win_T *wp;
13774 int winnr = 1;
13775 garray_T ga;
13776 char_u buf[50];
13777
13778 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020013779 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013780 {
13781 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
13782 ga_concat(&ga, buf);
13783 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
13784 ga_concat(&ga, buf);
13785 ++winnr;
13786 }
13787 ga_append(&ga, NUL);
13788
13789 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013790 rettv->v_type = VAR_STRING;
13791}
13792
13793/*
13794 * "winrestview()" function
13795 */
13796 static void
13797f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
13798{
13799 dict_T *dict;
13800
13801 if (argvars[0].v_type != VAR_DICT
13802 || (dict = argvars[0].vval.v_dict) == NULL)
13803 EMSG(_(e_invarg));
13804 else
13805 {
13806 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
13807 curwin->w_cursor.lnum = (linenr_T)get_dict_number(dict, (char_u *)"lnum");
13808 if (dict_find(dict, (char_u *)"col", -1) != NULL)
13809 curwin->w_cursor.col = (colnr_T)get_dict_number(dict, (char_u *)"col");
13810#ifdef FEAT_VIRTUALEDIT
13811 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
13812 curwin->w_cursor.coladd = (colnr_T)get_dict_number(dict, (char_u *)"coladd");
13813#endif
13814 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
13815 {
13816 curwin->w_curswant = (colnr_T)get_dict_number(dict, (char_u *)"curswant");
13817 curwin->w_set_curswant = FALSE;
13818 }
13819
13820 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
13821 set_topline(curwin, (linenr_T)get_dict_number(dict, (char_u *)"topline"));
13822#ifdef FEAT_DIFF
13823 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
13824 curwin->w_topfill = (int)get_dict_number(dict, (char_u *)"topfill");
13825#endif
13826 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
13827 curwin->w_leftcol = (colnr_T)get_dict_number(dict, (char_u *)"leftcol");
13828 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
13829 curwin->w_skipcol = (colnr_T)get_dict_number(dict, (char_u *)"skipcol");
13830
13831 check_cursor();
13832 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020013833 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013834 changed_window_setting();
13835
13836 if (curwin->w_topline <= 0)
13837 curwin->w_topline = 1;
13838 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
13839 curwin->w_topline = curbuf->b_ml.ml_line_count;
13840#ifdef FEAT_DIFF
13841 check_topfill(curwin, TRUE);
13842#endif
13843 }
13844}
13845
13846/*
13847 * "winsaveview()" function
13848 */
13849 static void
13850f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
13851{
13852 dict_T *dict;
13853
13854 if (rettv_dict_alloc(rettv) == FAIL)
13855 return;
13856 dict = rettv->vval.v_dict;
13857
Bram Moolenaare0be1672018-07-08 16:50:37 +020013858 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
13859 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013860#ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +020013861 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013862#endif
13863 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020013864 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013865
Bram Moolenaare0be1672018-07-08 16:50:37 +020013866 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013867#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020013868 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013869#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020013870 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
13871 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013872}
13873
13874/*
13875 * "winwidth(nr)" function
13876 */
13877 static void
13878f_winwidth(typval_T *argvars, typval_T *rettv)
13879{
13880 win_T *wp;
13881
13882 wp = find_win_by_nr(&argvars[0], NULL);
13883 if (wp == NULL)
13884 rettv->vval.v_number = -1;
13885 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013886 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013887}
13888
13889/*
13890 * "wordcount()" function
13891 */
13892 static void
13893f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
13894{
13895 if (rettv_dict_alloc(rettv) == FAIL)
13896 return;
13897 cursor_pos_info(rettv->vval.v_dict);
13898}
13899
13900/*
13901 * "writefile()" function
13902 */
13903 static void
13904f_writefile(typval_T *argvars, typval_T *rettv)
13905{
13906 int binary = FALSE;
13907 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013908#ifdef HAVE_FSYNC
13909 int do_fsync = p_fs;
13910#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013911 char_u *fname;
13912 FILE *fd;
13913 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013914 listitem_T *li;
13915 list_T *list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013916
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013917 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013918 if (check_restricted() || check_secure())
13919 return;
13920
13921 if (argvars[0].v_type != VAR_LIST)
13922 {
13923 EMSG2(_(e_listarg), "writefile()");
13924 return;
13925 }
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013926 list = argvars[0].vval.v_list;
13927 if (list == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013928 return;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013929 for (li = list->lv_first; li != NULL; li = li->li_next)
13930 if (get_tv_string_chk(&li->li_tv) == NULL)
13931 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013932
13933 if (argvars[2].v_type != VAR_UNKNOWN)
13934 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013935 char_u *arg2 = get_tv_string_chk(&argvars[2]);
13936
13937 if (arg2 == NULL)
13938 return;
13939 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013940 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013941 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013942 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013943#ifdef HAVE_FSYNC
13944 if (vim_strchr(arg2, 's') != NULL)
13945 do_fsync = TRUE;
13946 else if (vim_strchr(arg2, 'S') != NULL)
13947 do_fsync = FALSE;
13948#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013949 }
13950
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013951 fname = get_tv_string_chk(&argvars[1]);
13952 if (fname == NULL)
13953 return;
13954
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013955 /* Always open the file in binary mode, library functions have a mind of
13956 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013957 if (*fname == NUL || (fd = mch_fopen((char *)fname,
13958 append ? APPENDBIN : WRITEBIN)) == NULL)
13959 {
13960 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
13961 ret = -1;
13962 }
13963 else
13964 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013965 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013966 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013967#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010013968 else if (do_fsync)
13969 /* Ignore the error, the user wouldn't know what to do about it.
13970 * May happen for a device. */
13971 ignored = fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013972#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013973 fclose(fd);
13974 }
13975
13976 rettv->vval.v_number = ret;
13977}
13978
13979/*
13980 * "xor(expr, expr)" function
13981 */
13982 static void
13983f_xor(typval_T *argvars, typval_T *rettv)
13984{
13985 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
13986 ^ get_tv_number_chk(&argvars[1], NULL);
13987}
13988
13989
13990#endif /* FEAT_EVAL */