blob: cd0888b626acd143bd465cec885cf5286df75f04 [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");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020032static char *e_stringreq = N_("E928: String required");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020033
34#ifdef FEAT_FLOAT
35static void f_abs(typval_T *argvars, typval_T *rettv);
36static void f_acos(typval_T *argvars, typval_T *rettv);
37#endif
38static void f_add(typval_T *argvars, typval_T *rettv);
39static void f_and(typval_T *argvars, typval_T *rettv);
40static void f_append(typval_T *argvars, typval_T *rettv);
Bram Moolenaarca851592018-06-06 21:04:07 +020041static void f_appendbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020042static void f_argc(typval_T *argvars, typval_T *rettv);
43static void f_argidx(typval_T *argvars, typval_T *rettv);
44static void f_arglistid(typval_T *argvars, typval_T *rettv);
45static void f_argv(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +010046static void f_assert_beeps(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020047static void f_assert_equal(typval_T *argvars, typval_T *rettv);
Bram Moolenaard96ff162018-02-18 22:13:29 +010048static void f_assert_equalfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020049static void f_assert_exception(typval_T *argvars, typval_T *rettv);
50static void f_assert_fails(typval_T *argvars, typval_T *rettv);
51static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaar61c04492016-07-23 15:35:35 +020052static void f_assert_inrange(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020053static void f_assert_match(typval_T *argvars, typval_T *rettv);
54static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
55static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar42205552017-03-18 19:42:22 +010056static void f_assert_report(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020057static void f_assert_true(typval_T *argvars, typval_T *rettv);
58#ifdef FEAT_FLOAT
59static void f_asin(typval_T *argvars, typval_T *rettv);
60static void f_atan(typval_T *argvars, typval_T *rettv);
61static void f_atan2(typval_T *argvars, typval_T *rettv);
62#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010063#ifdef FEAT_BEVAL
64static void f_balloon_show(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010065# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010066static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010067# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010068#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020069static void f_browse(typval_T *argvars, typval_T *rettv);
70static void f_browsedir(typval_T *argvars, typval_T *rettv);
71static void f_bufexists(typval_T *argvars, typval_T *rettv);
72static void f_buflisted(typval_T *argvars, typval_T *rettv);
73static void f_bufloaded(typval_T *argvars, typval_T *rettv);
74static void f_bufname(typval_T *argvars, typval_T *rettv);
75static void f_bufnr(typval_T *argvars, typval_T *rettv);
76static void f_bufwinid(typval_T *argvars, typval_T *rettv);
77static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
78static void f_byte2line(typval_T *argvars, typval_T *rettv);
79static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
80static void f_byteidx(typval_T *argvars, typval_T *rettv);
81static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
82static void f_call(typval_T *argvars, typval_T *rettv);
83#ifdef FEAT_FLOAT
84static void f_ceil(typval_T *argvars, typval_T *rettv);
85#endif
86#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +010087static void f_ch_canread(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020088static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0874a832016-09-01 15:11:51 +020089static void f_ch_close_in(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020090static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
91static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
92static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
93static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
94static void f_ch_info(typval_T *argvars, typval_T *rettv);
95static void f_ch_log(typval_T *argvars, typval_T *rettv);
96static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
97static void f_ch_open(typval_T *argvars, typval_T *rettv);
98static void f_ch_read(typval_T *argvars, typval_T *rettv);
99static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
100static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
101static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
102static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
103static void f_ch_status(typval_T *argvars, typval_T *rettv);
104#endif
105static void f_changenr(typval_T *argvars, typval_T *rettv);
106static void f_char2nr(typval_T *argvars, typval_T *rettv);
107static void f_cindent(typval_T *argvars, typval_T *rettv);
108static void f_clearmatches(typval_T *argvars, typval_T *rettv);
109static void f_col(typval_T *argvars, typval_T *rettv);
110#if defined(FEAT_INS_EXPAND)
111static void f_complete(typval_T *argvars, typval_T *rettv);
112static void f_complete_add(typval_T *argvars, typval_T *rettv);
113static void f_complete_check(typval_T *argvars, typval_T *rettv);
114#endif
115static void f_confirm(typval_T *argvars, typval_T *rettv);
116static void f_copy(typval_T *argvars, typval_T *rettv);
117#ifdef FEAT_FLOAT
118static void f_cos(typval_T *argvars, typval_T *rettv);
119static void f_cosh(typval_T *argvars, typval_T *rettv);
120#endif
121static void f_count(typval_T *argvars, typval_T *rettv);
122static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
123static void f_cursor(typval_T *argsvars, typval_T *rettv);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200124#ifdef WIN3264
125static void f_debugbreak(typval_T *argvars, typval_T *rettv);
126#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200127static void f_deepcopy(typval_T *argvars, typval_T *rettv);
128static void f_delete(typval_T *argvars, typval_T *rettv);
Bram Moolenaard79a2622018-06-07 18:17:46 +0200129static void f_deletebufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200130static void f_did_filetype(typval_T *argvars, typval_T *rettv);
131static void f_diff_filler(typval_T *argvars, typval_T *rettv);
132static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
133static void f_empty(typval_T *argvars, typval_T *rettv);
134static void f_escape(typval_T *argvars, typval_T *rettv);
135static void f_eval(typval_T *argvars, typval_T *rettv);
136static void f_eventhandler(typval_T *argvars, typval_T *rettv);
137static void f_executable(typval_T *argvars, typval_T *rettv);
138static void f_execute(typval_T *argvars, typval_T *rettv);
139static void f_exepath(typval_T *argvars, typval_T *rettv);
140static void f_exists(typval_T *argvars, typval_T *rettv);
141#ifdef FEAT_FLOAT
142static void f_exp(typval_T *argvars, typval_T *rettv);
143#endif
144static void f_expand(typval_T *argvars, typval_T *rettv);
145static void f_extend(typval_T *argvars, typval_T *rettv);
146static void f_feedkeys(typval_T *argvars, typval_T *rettv);
147static void f_filereadable(typval_T *argvars, typval_T *rettv);
148static void f_filewritable(typval_T *argvars, typval_T *rettv);
149static void f_filter(typval_T *argvars, typval_T *rettv);
150static void f_finddir(typval_T *argvars, typval_T *rettv);
151static void f_findfile(typval_T *argvars, typval_T *rettv);
152#ifdef FEAT_FLOAT
153static void f_float2nr(typval_T *argvars, typval_T *rettv);
154static void f_floor(typval_T *argvars, typval_T *rettv);
155static void f_fmod(typval_T *argvars, typval_T *rettv);
156#endif
157static void f_fnameescape(typval_T *argvars, typval_T *rettv);
158static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
159static void f_foldclosed(typval_T *argvars, typval_T *rettv);
160static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
161static void f_foldlevel(typval_T *argvars, typval_T *rettv);
162static void f_foldtext(typval_T *argvars, typval_T *rettv);
163static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
164static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200165static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200166static void f_function(typval_T *argvars, typval_T *rettv);
167static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
168static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200169static void f_getbufinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200170static void f_getbufline(typval_T *argvars, typval_T *rettv);
171static void f_getbufvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100172static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200173static void f_getchar(typval_T *argvars, typval_T *rettv);
174static void f_getcharmod(typval_T *argvars, typval_T *rettv);
175static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
176static void f_getcmdline(typval_T *argvars, typval_T *rettv);
177#if defined(FEAT_CMDL_COMPL)
178static void f_getcompletion(typval_T *argvars, typval_T *rettv);
179#endif
180static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
181static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
182static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
183static void f_getcwd(typval_T *argvars, typval_T *rettv);
184static void f_getfontname(typval_T *argvars, typval_T *rettv);
185static void f_getfperm(typval_T *argvars, typval_T *rettv);
186static void f_getfsize(typval_T *argvars, typval_T *rettv);
187static void f_getftime(typval_T *argvars, typval_T *rettv);
188static void f_getftype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +0100189static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200190static void f_getline(typval_T *argvars, typval_T *rettv);
Bram Moolenaard823fa92016-08-12 16:29:27 +0200191static void f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200192static void f_getmatches(typval_T *argvars, typval_T *rettv);
193static void f_getpid(typval_T *argvars, typval_T *rettv);
194static void f_getcurpos(typval_T *argvars, typval_T *rettv);
195static void f_getpos(typval_T *argvars, typval_T *rettv);
196static void f_getqflist(typval_T *argvars, typval_T *rettv);
197static void f_getreg(typval_T *argvars, typval_T *rettv);
198static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200199static void f_gettabinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200200static void f_gettabvar(typval_T *argvars, typval_T *rettv);
201static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100202static void f_gettagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200203static void f_getwininfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100204static void f_getwinpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200205static void f_getwinposx(typval_T *argvars, typval_T *rettv);
206static void f_getwinposy(typval_T *argvars, typval_T *rettv);
207static void f_getwinvar(typval_T *argvars, typval_T *rettv);
208static void f_glob(typval_T *argvars, typval_T *rettv);
209static void f_globpath(typval_T *argvars, typval_T *rettv);
210static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
211static void f_has(typval_T *argvars, typval_T *rettv);
212static void f_has_key(typval_T *argvars, typval_T *rettv);
213static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
214static void f_hasmapto(typval_T *argvars, typval_T *rettv);
215static void f_histadd(typval_T *argvars, typval_T *rettv);
216static void f_histdel(typval_T *argvars, typval_T *rettv);
217static void f_histget(typval_T *argvars, typval_T *rettv);
218static void f_histnr(typval_T *argvars, typval_T *rettv);
219static void f_hlID(typval_T *argvars, typval_T *rettv);
220static void f_hlexists(typval_T *argvars, typval_T *rettv);
221static void f_hostname(typval_T *argvars, typval_T *rettv);
222static void f_iconv(typval_T *argvars, typval_T *rettv);
223static void f_indent(typval_T *argvars, typval_T *rettv);
224static void f_index(typval_T *argvars, typval_T *rettv);
225static void f_input(typval_T *argvars, typval_T *rettv);
226static void f_inputdialog(typval_T *argvars, typval_T *rettv);
227static void f_inputlist(typval_T *argvars, typval_T *rettv);
228static void f_inputrestore(typval_T *argvars, typval_T *rettv);
229static void f_inputsave(typval_T *argvars, typval_T *rettv);
230static void f_inputsecret(typval_T *argvars, typval_T *rettv);
231static void f_insert(typval_T *argvars, typval_T *rettv);
232static void f_invert(typval_T *argvars, typval_T *rettv);
233static void f_isdirectory(typval_T *argvars, typval_T *rettv);
234static void f_islocked(typval_T *argvars, typval_T *rettv);
235#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
236static void f_isnan(typval_T *argvars, typval_T *rettv);
237#endif
238static void f_items(typval_T *argvars, typval_T *rettv);
239#ifdef FEAT_JOB_CHANNEL
240static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
241static void f_job_info(typval_T *argvars, typval_T *rettv);
242static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
243static void f_job_start(typval_T *argvars, typval_T *rettv);
244static void f_job_stop(typval_T *argvars, typval_T *rettv);
245static void f_job_status(typval_T *argvars, typval_T *rettv);
246#endif
247static void f_join(typval_T *argvars, typval_T *rettv);
248static void f_js_decode(typval_T *argvars, typval_T *rettv);
249static void f_js_encode(typval_T *argvars, typval_T *rettv);
250static void f_json_decode(typval_T *argvars, typval_T *rettv);
251static void f_json_encode(typval_T *argvars, typval_T *rettv);
252static void f_keys(typval_T *argvars, typval_T *rettv);
253static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
254static void f_len(typval_T *argvars, typval_T *rettv);
255static void f_libcall(typval_T *argvars, typval_T *rettv);
256static void f_libcallnr(typval_T *argvars, typval_T *rettv);
257static void f_line(typval_T *argvars, typval_T *rettv);
258static void f_line2byte(typval_T *argvars, typval_T *rettv);
259static void f_lispindent(typval_T *argvars, typval_T *rettv);
260static void f_localtime(typval_T *argvars, typval_T *rettv);
261#ifdef FEAT_FLOAT
262static void f_log(typval_T *argvars, typval_T *rettv);
263static void f_log10(typval_T *argvars, typval_T *rettv);
264#endif
265#ifdef FEAT_LUA
266static void f_luaeval(typval_T *argvars, typval_T *rettv);
267#endif
268static void f_map(typval_T *argvars, typval_T *rettv);
269static void f_maparg(typval_T *argvars, typval_T *rettv);
270static void f_mapcheck(typval_T *argvars, typval_T *rettv);
271static void f_match(typval_T *argvars, typval_T *rettv);
272static void f_matchadd(typval_T *argvars, typval_T *rettv);
273static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
274static void f_matcharg(typval_T *argvars, typval_T *rettv);
275static void f_matchdelete(typval_T *argvars, typval_T *rettv);
276static void f_matchend(typval_T *argvars, typval_T *rettv);
277static void f_matchlist(typval_T *argvars, typval_T *rettv);
278static void f_matchstr(typval_T *argvars, typval_T *rettv);
279static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
280static void f_max(typval_T *argvars, typval_T *rettv);
281static void f_min(typval_T *argvars, typval_T *rettv);
282#ifdef vim_mkdir
283static void f_mkdir(typval_T *argvars, typval_T *rettv);
284#endif
285static void f_mode(typval_T *argvars, typval_T *rettv);
286#ifdef FEAT_MZSCHEME
287static void f_mzeval(typval_T *argvars, typval_T *rettv);
288#endif
289static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
290static void f_nr2char(typval_T *argvars, typval_T *rettv);
291static void f_or(typval_T *argvars, typval_T *rettv);
292static void f_pathshorten(typval_T *argvars, typval_T *rettv);
293#ifdef FEAT_PERL
294static void f_perleval(typval_T *argvars, typval_T *rettv);
295#endif
296#ifdef FEAT_FLOAT
297static void f_pow(typval_T *argvars, typval_T *rettv);
298#endif
299static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
300static void f_printf(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200301#ifdef FEAT_JOB_CHANNEL
302static void f_prompt_setcallback(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200303static void f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200304static void f_prompt_setprompt(typval_T *argvars, typval_T *rettv);
305#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200306static void f_pumvisible(typval_T *argvars, typval_T *rettv);
307#ifdef FEAT_PYTHON3
308static void f_py3eval(typval_T *argvars, typval_T *rettv);
309#endif
310#ifdef FEAT_PYTHON
311static void f_pyeval(typval_T *argvars, typval_T *rettv);
312#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100313#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
314static void f_pyxeval(typval_T *argvars, typval_T *rettv);
315#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200316static void f_range(typval_T *argvars, typval_T *rettv);
317static void f_readfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200318static void f_reg_executing(typval_T *argvars, typval_T *rettv);
319static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200320static void f_reltime(typval_T *argvars, typval_T *rettv);
321#ifdef FEAT_FLOAT
322static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
323#endif
324static void f_reltimestr(typval_T *argvars, typval_T *rettv);
325static void f_remote_expr(typval_T *argvars, typval_T *rettv);
326static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
327static void f_remote_peek(typval_T *argvars, typval_T *rettv);
328static void f_remote_read(typval_T *argvars, typval_T *rettv);
329static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100330static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200331static void f_remove(typval_T *argvars, typval_T *rettv);
332static void f_rename(typval_T *argvars, typval_T *rettv);
333static void f_repeat(typval_T *argvars, typval_T *rettv);
334static void f_resolve(typval_T *argvars, typval_T *rettv);
335static void f_reverse(typval_T *argvars, typval_T *rettv);
336#ifdef FEAT_FLOAT
337static void f_round(typval_T *argvars, typval_T *rettv);
338#endif
339static void f_screenattr(typval_T *argvars, typval_T *rettv);
340static void f_screenchar(typval_T *argvars, typval_T *rettv);
341static void f_screencol(typval_T *argvars, typval_T *rettv);
342static void f_screenrow(typval_T *argvars, typval_T *rettv);
343static void f_search(typval_T *argvars, typval_T *rettv);
344static void f_searchdecl(typval_T *argvars, typval_T *rettv);
345static void f_searchpair(typval_T *argvars, typval_T *rettv);
346static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
347static void f_searchpos(typval_T *argvars, typval_T *rettv);
348static void f_server2client(typval_T *argvars, typval_T *rettv);
349static void f_serverlist(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200350static void f_setbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200351static void f_setbufvar(typval_T *argvars, typval_T *rettv);
352static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
353static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
354static void f_setfperm(typval_T *argvars, typval_T *rettv);
355static void f_setline(typval_T *argvars, typval_T *rettv);
356static void f_setloclist(typval_T *argvars, typval_T *rettv);
357static void f_setmatches(typval_T *argvars, typval_T *rettv);
358static void f_setpos(typval_T *argvars, typval_T *rettv);
359static void f_setqflist(typval_T *argvars, typval_T *rettv);
360static void f_setreg(typval_T *argvars, typval_T *rettv);
361static void f_settabvar(typval_T *argvars, typval_T *rettv);
362static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100363static void f_settagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200364static 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);
Bram Moolenaar162b7142018-12-21 15:17:36 +0100370#ifdef FEAT_SIGNS
371static void f_sign_define(typval_T *argvars, typval_T *rettv);
372static void f_sign_getdefined(typval_T *argvars, typval_T *rettv);
373static void f_sign_getplaced(typval_T *argvars, typval_T *rettv);
374static void f_sign_place(typval_T *argvars, typval_T *rettv);
375static void f_sign_undefine(typval_T *argvars, typval_T *rettv);
376static void f_sign_unplace(typval_T *argvars, typval_T *rettv);
377#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200378static void f_simplify(typval_T *argvars, typval_T *rettv);
379#ifdef FEAT_FLOAT
380static void f_sin(typval_T *argvars, typval_T *rettv);
381static void f_sinh(typval_T *argvars, typval_T *rettv);
382#endif
383static void f_sort(typval_T *argvars, typval_T *rettv);
384static void f_soundfold(typval_T *argvars, typval_T *rettv);
385static void f_spellbadword(typval_T *argvars, typval_T *rettv);
386static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
387static void f_split(typval_T *argvars, typval_T *rettv);
388#ifdef FEAT_FLOAT
389static void f_sqrt(typval_T *argvars, typval_T *rettv);
390static void f_str2float(typval_T *argvars, typval_T *rettv);
391#endif
392static void f_str2nr(typval_T *argvars, typval_T *rettv);
393static void f_strchars(typval_T *argvars, typval_T *rettv);
394#ifdef HAVE_STRFTIME
395static void f_strftime(typval_T *argvars, typval_T *rettv);
396#endif
397static void f_strgetchar(typval_T *argvars, typval_T *rettv);
398static void f_stridx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200399static void f_strlen(typval_T *argvars, typval_T *rettv);
400static void f_strcharpart(typval_T *argvars, typval_T *rettv);
401static void f_strpart(typval_T *argvars, typval_T *rettv);
402static void f_strridx(typval_T *argvars, typval_T *rettv);
403static void f_strtrans(typval_T *argvars, typval_T *rettv);
404static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
405static void f_strwidth(typval_T *argvars, typval_T *rettv);
406static void f_submatch(typval_T *argvars, typval_T *rettv);
407static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200408static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200409static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200410static void f_synID(typval_T *argvars, typval_T *rettv);
411static void f_synIDattr(typval_T *argvars, typval_T *rettv);
412static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
413static void f_synstack(typval_T *argvars, typval_T *rettv);
414static void f_synconcealed(typval_T *argvars, typval_T *rettv);
415static void f_system(typval_T *argvars, typval_T *rettv);
416static void f_systemlist(typval_T *argvars, typval_T *rettv);
417static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
418static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
419static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
420static void f_taglist(typval_T *argvars, typval_T *rettv);
421static void f_tagfiles(typval_T *argvars, typval_T *rettv);
422static void f_tempname(typval_T *argvars, typval_T *rettv);
423static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
424static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200425static void f_test_feedinput(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200426static void f_test_option_not_set(typval_T *argvars, typval_T *rettv);
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100427static void f_test_override(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200428static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100429static void f_test_ignore_error(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200430#ifdef FEAT_JOB_CHANNEL
431static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
432#endif
433static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
434#ifdef FEAT_JOB_CHANNEL
435static void f_test_null_job(typval_T *argvars, typval_T *rettv);
436#endif
437static void f_test_null_list(typval_T *argvars, typval_T *rettv);
438static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
439static void f_test_null_string(typval_T *argvars, typval_T *rettv);
Bram Moolenaarab186732018-09-14 21:27:06 +0200440#ifdef FEAT_GUI
441static void f_test_scrollbar(typval_T *argvars, typval_T *rettv);
442#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200443static void f_test_settime(typval_T *argvars, typval_T *rettv);
444#ifdef FEAT_FLOAT
445static void f_tan(typval_T *argvars, typval_T *rettv);
446static void f_tanh(typval_T *argvars, typval_T *rettv);
447#endif
448#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200449static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200450static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200451static void f_timer_start(typval_T *argvars, typval_T *rettv);
452static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200453static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200454#endif
455static void f_tolower(typval_T *argvars, typval_T *rettv);
456static void f_toupper(typval_T *argvars, typval_T *rettv);
457static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100458static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200459#ifdef FEAT_FLOAT
460static void f_trunc(typval_T *argvars, typval_T *rettv);
461#endif
462static void f_type(typval_T *argvars, typval_T *rettv);
463static void f_undofile(typval_T *argvars, typval_T *rettv);
464static void f_undotree(typval_T *argvars, typval_T *rettv);
465static void f_uniq(typval_T *argvars, typval_T *rettv);
466static void f_values(typval_T *argvars, typval_T *rettv);
467static void f_virtcol(typval_T *argvars, typval_T *rettv);
468static void f_visualmode(typval_T *argvars, typval_T *rettv);
469static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
470static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
471static void f_win_getid(typval_T *argvars, typval_T *rettv);
472static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
473static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
474static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100475static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200476static void f_winbufnr(typval_T *argvars, typval_T *rettv);
477static void f_wincol(typval_T *argvars, typval_T *rettv);
478static void f_winheight(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200479static void f_winlayout(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200480static void f_winline(typval_T *argvars, typval_T *rettv);
481static void f_winnr(typval_T *argvars, typval_T *rettv);
482static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
483static void f_winrestview(typval_T *argvars, typval_T *rettv);
484static void f_winsaveview(typval_T *argvars, typval_T *rettv);
485static void f_winwidth(typval_T *argvars, typval_T *rettv);
486static void f_writefile(typval_T *argvars, typval_T *rettv);
487static void f_wordcount(typval_T *argvars, typval_T *rettv);
488static void f_xor(typval_T *argvars, typval_T *rettv);
489
490/*
491 * Array with names and number of arguments of all internal functions
492 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
493 */
494static struct fst
495{
496 char *f_name; /* function name */
497 char f_min_argc; /* minimal number of arguments */
498 char f_max_argc; /* maximal number of arguments */
499 void (*f_func)(typval_T *args, typval_T *rvar);
500 /* implementation of function */
501} functions[] =
502{
503#ifdef FEAT_FLOAT
504 {"abs", 1, 1, f_abs},
505 {"acos", 1, 1, f_acos}, /* WJMc */
506#endif
507 {"add", 2, 2, f_add},
508 {"and", 2, 2, f_and},
509 {"append", 2, 2, f_append},
Bram Moolenaarca851592018-06-06 21:04:07 +0200510 {"appendbufline", 3, 3, f_appendbufline},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200511 {"argc", 0, 1, f_argc},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200512 {"argidx", 0, 0, f_argidx},
513 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200514 {"argv", 0, 2, f_argv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200515#ifdef FEAT_FLOAT
516 {"asin", 1, 1, f_asin}, /* WJMc */
517#endif
Bram Moolenaarb48e96f2018-02-13 12:26:14 +0100518 {"assert_beeps", 1, 2, f_assert_beeps},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200519 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaard96ff162018-02-18 22:13:29 +0100520 {"assert_equalfile", 2, 2, f_assert_equalfile},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200521 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaar1307d1c2018-10-07 20:16:49 +0200522 {"assert_fails", 1, 3, f_assert_fails},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200523 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100524 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200525 {"assert_match", 2, 3, f_assert_match},
526 {"assert_notequal", 2, 3, f_assert_notequal},
527 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100528 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200529 {"assert_true", 1, 2, f_assert_true},
530#ifdef FEAT_FLOAT
531 {"atan", 1, 1, f_atan},
532 {"atan2", 2, 2, f_atan2},
533#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100534#ifdef FEAT_BEVAL
535 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100536# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100537 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100538# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100539#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200540 {"browse", 4, 4, f_browse},
541 {"browsedir", 2, 2, f_browsedir},
542 {"bufexists", 1, 1, f_bufexists},
543 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
544 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
545 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
546 {"buflisted", 1, 1, f_buflisted},
547 {"bufloaded", 1, 1, f_bufloaded},
548 {"bufname", 1, 1, f_bufname},
549 {"bufnr", 1, 2, f_bufnr},
550 {"bufwinid", 1, 1, f_bufwinid},
551 {"bufwinnr", 1, 1, f_bufwinnr},
552 {"byte2line", 1, 1, f_byte2line},
553 {"byteidx", 2, 2, f_byteidx},
554 {"byteidxcomp", 2, 2, f_byteidxcomp},
555 {"call", 2, 3, f_call},
556#ifdef FEAT_FLOAT
557 {"ceil", 1, 1, f_ceil},
558#endif
559#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100560 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200561 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200562 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200563 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
564 {"ch_evalraw", 2, 3, f_ch_evalraw},
565 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
566 {"ch_getjob", 1, 1, f_ch_getjob},
567 {"ch_info", 1, 1, f_ch_info},
568 {"ch_log", 1, 2, f_ch_log},
569 {"ch_logfile", 1, 2, f_ch_logfile},
570 {"ch_open", 1, 2, f_ch_open},
571 {"ch_read", 1, 2, f_ch_read},
572 {"ch_readraw", 1, 2, f_ch_readraw},
573 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
574 {"ch_sendraw", 2, 3, f_ch_sendraw},
575 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200576 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200577#endif
578 {"changenr", 0, 0, f_changenr},
579 {"char2nr", 1, 2, f_char2nr},
580 {"cindent", 1, 1, f_cindent},
581 {"clearmatches", 0, 0, f_clearmatches},
582 {"col", 1, 1, f_col},
583#if defined(FEAT_INS_EXPAND)
584 {"complete", 2, 2, f_complete},
585 {"complete_add", 1, 1, f_complete_add},
586 {"complete_check", 0, 0, f_complete_check},
587#endif
588 {"confirm", 1, 4, f_confirm},
589 {"copy", 1, 1, f_copy},
590#ifdef FEAT_FLOAT
591 {"cos", 1, 1, f_cos},
592 {"cosh", 1, 1, f_cosh},
593#endif
594 {"count", 2, 4, f_count},
595 {"cscope_connection",0,3, f_cscope_connection},
596 {"cursor", 1, 3, f_cursor},
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200597#ifdef WIN3264
598 {"debugbreak", 1, 1, f_debugbreak},
599#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200600 {"deepcopy", 1, 2, f_deepcopy},
601 {"delete", 1, 2, f_delete},
Bram Moolenaard79a2622018-06-07 18:17:46 +0200602 {"deletebufline", 2, 3, f_deletebufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200603 {"did_filetype", 0, 0, f_did_filetype},
604 {"diff_filler", 1, 1, f_diff_filler},
605 {"diff_hlID", 2, 2, f_diff_hlID},
606 {"empty", 1, 1, f_empty},
607 {"escape", 2, 2, f_escape},
608 {"eval", 1, 1, f_eval},
609 {"eventhandler", 0, 0, f_eventhandler},
610 {"executable", 1, 1, f_executable},
611 {"execute", 1, 2, f_execute},
612 {"exepath", 1, 1, f_exepath},
613 {"exists", 1, 1, f_exists},
614#ifdef FEAT_FLOAT
615 {"exp", 1, 1, f_exp},
616#endif
617 {"expand", 1, 3, f_expand},
618 {"extend", 2, 3, f_extend},
619 {"feedkeys", 1, 2, f_feedkeys},
620 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
621 {"filereadable", 1, 1, f_filereadable},
622 {"filewritable", 1, 1, f_filewritable},
623 {"filter", 2, 2, f_filter},
624 {"finddir", 1, 3, f_finddir},
625 {"findfile", 1, 3, f_findfile},
626#ifdef FEAT_FLOAT
627 {"float2nr", 1, 1, f_float2nr},
628 {"floor", 1, 1, f_floor},
629 {"fmod", 2, 2, f_fmod},
630#endif
631 {"fnameescape", 1, 1, f_fnameescape},
632 {"fnamemodify", 2, 2, f_fnamemodify},
633 {"foldclosed", 1, 1, f_foldclosed},
634 {"foldclosedend", 1, 1, f_foldclosedend},
635 {"foldlevel", 1, 1, f_foldlevel},
636 {"foldtext", 0, 0, f_foldtext},
637 {"foldtextresult", 1, 1, f_foldtextresult},
638 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200639 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200640 {"function", 1, 3, f_function},
641 {"garbagecollect", 0, 1, f_garbagecollect},
642 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200643 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200644 {"getbufline", 2, 3, f_getbufline},
645 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100646 {"getchangelist", 1, 1, f_getchangelist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200647 {"getchar", 0, 1, f_getchar},
648 {"getcharmod", 0, 0, f_getcharmod},
649 {"getcharsearch", 0, 0, f_getcharsearch},
650 {"getcmdline", 0, 0, f_getcmdline},
651 {"getcmdpos", 0, 0, f_getcmdpos},
652 {"getcmdtype", 0, 0, f_getcmdtype},
653 {"getcmdwintype", 0, 0, f_getcmdwintype},
654#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200655 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200656#endif
657 {"getcurpos", 0, 0, f_getcurpos},
658 {"getcwd", 0, 2, f_getcwd},
659 {"getfontname", 0, 1, f_getfontname},
660 {"getfperm", 1, 1, f_getfperm},
661 {"getfsize", 1, 1, f_getfsize},
662 {"getftime", 1, 1, f_getftime},
663 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100664 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200665 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200666 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200667 {"getmatches", 0, 0, f_getmatches},
668 {"getpid", 0, 0, f_getpid},
669 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200670 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200671 {"getreg", 0, 3, f_getreg},
672 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200673 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200674 {"gettabvar", 2, 3, f_gettabvar},
675 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100676 {"gettagstack", 0, 1, f_gettagstack},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200677 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100678 {"getwinpos", 0, 1, f_getwinpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200679 {"getwinposx", 0, 0, f_getwinposx},
680 {"getwinposy", 0, 0, f_getwinposy},
681 {"getwinvar", 2, 3, f_getwinvar},
682 {"glob", 1, 4, f_glob},
683 {"glob2regpat", 1, 1, f_glob2regpat},
684 {"globpath", 2, 5, f_globpath},
685 {"has", 1, 1, f_has},
686 {"has_key", 2, 2, f_has_key},
687 {"haslocaldir", 0, 2, f_haslocaldir},
688 {"hasmapto", 1, 3, f_hasmapto},
689 {"highlightID", 1, 1, f_hlID}, /* obsolete */
690 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
691 {"histadd", 2, 2, f_histadd},
692 {"histdel", 1, 2, f_histdel},
693 {"histget", 1, 2, f_histget},
694 {"histnr", 1, 1, f_histnr},
695 {"hlID", 1, 1, f_hlID},
696 {"hlexists", 1, 1, f_hlexists},
697 {"hostname", 0, 0, f_hostname},
698 {"iconv", 3, 3, f_iconv},
699 {"indent", 1, 1, f_indent},
700 {"index", 2, 4, f_index},
701 {"input", 1, 3, f_input},
702 {"inputdialog", 1, 3, f_inputdialog},
703 {"inputlist", 1, 1, f_inputlist},
704 {"inputrestore", 0, 0, f_inputrestore},
705 {"inputsave", 0, 0, f_inputsave},
706 {"inputsecret", 1, 2, f_inputsecret},
707 {"insert", 2, 3, f_insert},
708 {"invert", 1, 1, f_invert},
709 {"isdirectory", 1, 1, f_isdirectory},
710 {"islocked", 1, 1, f_islocked},
711#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
712 {"isnan", 1, 1, f_isnan},
713#endif
714 {"items", 1, 1, f_items},
715#ifdef FEAT_JOB_CHANNEL
716 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200717 {"job_info", 0, 1, f_job_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200718 {"job_setoptions", 2, 2, f_job_setoptions},
719 {"job_start", 1, 2, f_job_start},
720 {"job_status", 1, 1, f_job_status},
721 {"job_stop", 1, 2, f_job_stop},
722#endif
723 {"join", 1, 2, f_join},
724 {"js_decode", 1, 1, f_js_decode},
725 {"js_encode", 1, 1, f_js_encode},
726 {"json_decode", 1, 1, f_json_decode},
727 {"json_encode", 1, 1, f_json_encode},
728 {"keys", 1, 1, f_keys},
729 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
730 {"len", 1, 1, f_len},
731 {"libcall", 3, 3, f_libcall},
732 {"libcallnr", 3, 3, f_libcallnr},
733 {"line", 1, 1, f_line},
734 {"line2byte", 1, 1, f_line2byte},
735 {"lispindent", 1, 1, f_lispindent},
736 {"localtime", 0, 0, f_localtime},
737#ifdef FEAT_FLOAT
738 {"log", 1, 1, f_log},
739 {"log10", 1, 1, f_log10},
740#endif
741#ifdef FEAT_LUA
742 {"luaeval", 1, 2, f_luaeval},
743#endif
744 {"map", 2, 2, f_map},
745 {"maparg", 1, 4, f_maparg},
746 {"mapcheck", 1, 3, f_mapcheck},
747 {"match", 2, 4, f_match},
748 {"matchadd", 2, 5, f_matchadd},
749 {"matchaddpos", 2, 5, f_matchaddpos},
750 {"matcharg", 1, 1, f_matcharg},
751 {"matchdelete", 1, 1, f_matchdelete},
752 {"matchend", 2, 4, f_matchend},
753 {"matchlist", 2, 4, f_matchlist},
754 {"matchstr", 2, 4, f_matchstr},
755 {"matchstrpos", 2, 4, f_matchstrpos},
756 {"max", 1, 1, f_max},
757 {"min", 1, 1, f_min},
758#ifdef vim_mkdir
759 {"mkdir", 1, 3, f_mkdir},
760#endif
761 {"mode", 0, 1, f_mode},
762#ifdef FEAT_MZSCHEME
763 {"mzeval", 1, 1, f_mzeval},
764#endif
765 {"nextnonblank", 1, 1, f_nextnonblank},
766 {"nr2char", 1, 2, f_nr2char},
767 {"or", 2, 2, f_or},
768 {"pathshorten", 1, 1, f_pathshorten},
769#ifdef FEAT_PERL
770 {"perleval", 1, 1, f_perleval},
771#endif
772#ifdef FEAT_FLOAT
773 {"pow", 2, 2, f_pow},
774#endif
775 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100776 {"printf", 1, 19, f_printf},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200777#ifdef FEAT_JOB_CHANNEL
778 {"prompt_setcallback", 2, 2, f_prompt_setcallback},
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200779 {"prompt_setinterrupt", 2, 2, f_prompt_setinterrupt},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200780 {"prompt_setprompt", 2, 2, f_prompt_setprompt},
781#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100782#ifdef FEAT_TEXT_PROP
783 {"prop_add", 3, 3, f_prop_add},
784 {"prop_clear", 1, 3, f_prop_clear},
785 {"prop_list", 1, 2, f_prop_list},
786 {"prop_remove", 2, 3, f_prop_remove},
787 {"prop_type_add", 2, 2, f_prop_type_add},
788 {"prop_type_change", 2, 2, f_prop_type_change},
789 {"prop_type_delete", 1, 2, f_prop_type_delete},
790 {"prop_type_get", 1, 2, f_prop_type_get},
791 {"prop_type_list", 0, 1, f_prop_type_list},
792#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200793 {"pumvisible", 0, 0, f_pumvisible},
794#ifdef FEAT_PYTHON3
795 {"py3eval", 1, 1, f_py3eval},
796#endif
797#ifdef FEAT_PYTHON
798 {"pyeval", 1, 1, f_pyeval},
799#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100800#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
801 {"pyxeval", 1, 1, f_pyxeval},
802#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200803 {"range", 1, 3, f_range},
804 {"readfile", 1, 3, f_readfile},
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200805 {"reg_executing", 0, 0, f_reg_executing},
806 {"reg_recording", 0, 0, f_reg_recording},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200807 {"reltime", 0, 2, f_reltime},
808#ifdef FEAT_FLOAT
809 {"reltimefloat", 1, 1, f_reltimefloat},
810#endif
811 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100812 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200813 {"remote_foreground", 1, 1, f_remote_foreground},
814 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100815 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200816 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100817 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200818 {"remove", 2, 3, f_remove},
819 {"rename", 2, 2, f_rename},
820 {"repeat", 2, 2, f_repeat},
821 {"resolve", 1, 1, f_resolve},
822 {"reverse", 1, 1, f_reverse},
823#ifdef FEAT_FLOAT
824 {"round", 1, 1, f_round},
825#endif
826 {"screenattr", 2, 2, f_screenattr},
827 {"screenchar", 2, 2, f_screenchar},
828 {"screencol", 0, 0, f_screencol},
829 {"screenrow", 0, 0, f_screenrow},
830 {"search", 1, 4, f_search},
831 {"searchdecl", 1, 3, f_searchdecl},
832 {"searchpair", 3, 7, f_searchpair},
833 {"searchpairpos", 3, 7, f_searchpairpos},
834 {"searchpos", 1, 4, f_searchpos},
835 {"server2client", 2, 2, f_server2client},
836 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200837 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200838 {"setbufvar", 3, 3, f_setbufvar},
839 {"setcharsearch", 1, 1, f_setcharsearch},
840 {"setcmdpos", 1, 1, f_setcmdpos},
841 {"setfperm", 2, 2, f_setfperm},
842 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200843 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200844 {"setmatches", 1, 1, f_setmatches},
845 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200846 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200847 {"setreg", 2, 3, f_setreg},
848 {"settabvar", 3, 3, f_settabvar},
849 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100850 {"settagstack", 2, 3, f_settagstack},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200851 {"setwinvar", 3, 3, f_setwinvar},
852#ifdef FEAT_CRYPT
853 {"sha256", 1, 1, f_sha256},
854#endif
855 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaarf9514162018-11-22 03:08:29 +0100856 {"shiftwidth", 0, 1, f_shiftwidth},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100857#ifdef FEAT_SIGNS
858 {"sign_define", 1, 2, f_sign_define},
859 {"sign_getdefined", 0, 1, f_sign_getdefined},
860 {"sign_getplaced", 0, 2, f_sign_getplaced},
861 {"sign_place", 4, 5, f_sign_place},
862 {"sign_undefine", 0, 1, f_sign_undefine},
863 {"sign_unplace", 1, 2, f_sign_unplace},
864#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200865 {"simplify", 1, 1, f_simplify},
866#ifdef FEAT_FLOAT
867 {"sin", 1, 1, f_sin},
868 {"sinh", 1, 1, f_sinh},
869#endif
870 {"sort", 1, 3, f_sort},
871 {"soundfold", 1, 1, f_soundfold},
872 {"spellbadword", 0, 1, f_spellbadword},
873 {"spellsuggest", 1, 3, f_spellsuggest},
874 {"split", 1, 3, f_split},
875#ifdef FEAT_FLOAT
876 {"sqrt", 1, 1, f_sqrt},
877 {"str2float", 1, 1, f_str2float},
878#endif
879 {"str2nr", 1, 2, f_str2nr},
880 {"strcharpart", 2, 3, f_strcharpart},
881 {"strchars", 1, 2, f_strchars},
882 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
883#ifdef HAVE_STRFTIME
884 {"strftime", 1, 2, f_strftime},
885#endif
886 {"strgetchar", 2, 2, f_strgetchar},
887 {"stridx", 2, 3, f_stridx},
888 {"string", 1, 1, f_string},
889 {"strlen", 1, 1, f_strlen},
890 {"strpart", 2, 3, f_strpart},
891 {"strridx", 2, 3, f_strridx},
892 {"strtrans", 1, 1, f_strtrans},
893 {"strwidth", 1, 1, f_strwidth},
894 {"submatch", 1, 2, f_submatch},
895 {"substitute", 4, 4, f_substitute},
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200896 {"swapinfo", 1, 1, f_swapinfo},
Bram Moolenaar110bd602018-09-16 18:46:59 +0200897 {"swapname", 1, 1, f_swapname},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200898 {"synID", 3, 3, f_synID},
899 {"synIDattr", 2, 3, f_synIDattr},
900 {"synIDtrans", 1, 1, f_synIDtrans},
901 {"synconcealed", 2, 2, f_synconcealed},
902 {"synstack", 2, 2, f_synstack},
903 {"system", 1, 2, f_system},
904 {"systemlist", 1, 2, f_systemlist},
905 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
906 {"tabpagenr", 0, 1, f_tabpagenr},
907 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
908 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100909 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200910#ifdef FEAT_FLOAT
911 {"tan", 1, 1, f_tan},
912 {"tanh", 1, 1, f_tanh},
913#endif
914 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200915#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100916 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
917 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100918 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200919 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200920# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
921 {"term_getansicolors", 1, 1, f_term_getansicolors},
922# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200923 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200924 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200925 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200926 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200927 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200928 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200929 {"term_getstatus", 1, 1, f_term_getstatus},
930 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200931 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200932 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200933 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200934 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200935# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
936 {"term_setansicolors", 2, 2, f_term_setansicolors},
937# endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100938 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100939 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaara42d3632018-04-14 17:05:38 +0200940 {"term_setsize", 3, 3, f_term_setsize},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200941 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200942 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200943#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200944 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
945 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200946 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200947 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100948 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200949#ifdef FEAT_JOB_CHANNEL
950 {"test_null_channel", 0, 0, f_test_null_channel},
951#endif
952 {"test_null_dict", 0, 0, f_test_null_dict},
953#ifdef FEAT_JOB_CHANNEL
954 {"test_null_job", 0, 0, f_test_null_job},
955#endif
956 {"test_null_list", 0, 0, f_test_null_list},
957 {"test_null_partial", 0, 0, f_test_null_partial},
958 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200959 {"test_option_not_set", 1, 1, f_test_option_not_set},
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100960 {"test_override", 2, 2, f_test_override},
Bram Moolenaarab186732018-09-14 21:27:06 +0200961#ifdef FEAT_GUI
962 {"test_scrollbar", 3, 3, f_test_scrollbar},
963#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200964 {"test_settime", 1, 1, f_test_settime},
965#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200966 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200967 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200968 {"timer_start", 2, 3, f_timer_start},
969 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200970 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200971#endif
972 {"tolower", 1, 1, f_tolower},
973 {"toupper", 1, 1, f_toupper},
974 {"tr", 3, 3, f_tr},
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100975 {"trim", 1, 2, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200976#ifdef FEAT_FLOAT
977 {"trunc", 1, 1, f_trunc},
978#endif
979 {"type", 1, 1, f_type},
980 {"undofile", 1, 1, f_undofile},
981 {"undotree", 0, 0, f_undotree},
982 {"uniq", 1, 3, f_uniq},
983 {"values", 1, 1, f_values},
984 {"virtcol", 1, 1, f_virtcol},
985 {"visualmode", 0, 1, f_visualmode},
986 {"wildmenumode", 0, 0, f_wildmenumode},
987 {"win_findbuf", 1, 1, f_win_findbuf},
988 {"win_getid", 0, 2, f_win_getid},
989 {"win_gotoid", 1, 1, f_win_gotoid},
990 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
991 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100992 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200993 {"winbufnr", 1, 1, f_winbufnr},
994 {"wincol", 0, 0, f_wincol},
995 {"winheight", 1, 1, f_winheight},
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200996 {"winlayout", 0, 1, f_winlayout},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200997 {"winline", 0, 0, f_winline},
998 {"winnr", 0, 1, f_winnr},
999 {"winrestcmd", 0, 0, f_winrestcmd},
1000 {"winrestview", 1, 1, f_winrestview},
1001 {"winsaveview", 0, 0, f_winsaveview},
1002 {"winwidth", 1, 1, f_winwidth},
1003 {"wordcount", 0, 0, f_wordcount},
1004 {"writefile", 2, 3, f_writefile},
1005 {"xor", 2, 2, f_xor},
1006};
1007
1008#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
1009
1010/*
1011 * Function given to ExpandGeneric() to obtain the list of internal
1012 * or user defined function names.
1013 */
1014 char_u *
1015get_function_name(expand_T *xp, int idx)
1016{
1017 static int intidx = -1;
1018 char_u *name;
1019
1020 if (idx == 0)
1021 intidx = -1;
1022 if (intidx < 0)
1023 {
1024 name = get_user_func_name(xp, idx);
1025 if (name != NULL)
1026 return name;
1027 }
1028 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
1029 {
1030 STRCPY(IObuff, functions[intidx].f_name);
1031 STRCAT(IObuff, "(");
1032 if (functions[intidx].f_max_argc == 0)
1033 STRCAT(IObuff, ")");
1034 return IObuff;
1035 }
1036
1037 return NULL;
1038}
1039
1040/*
1041 * Function given to ExpandGeneric() to obtain the list of internal or
1042 * user defined variable or function names.
1043 */
1044 char_u *
1045get_expr_name(expand_T *xp, int idx)
1046{
1047 static int intidx = -1;
1048 char_u *name;
1049
1050 if (idx == 0)
1051 intidx = -1;
1052 if (intidx < 0)
1053 {
1054 name = get_function_name(xp, idx);
1055 if (name != NULL)
1056 return name;
1057 }
1058 return get_user_var_name(xp, ++intidx);
1059}
1060
1061#endif /* FEAT_CMDL_COMPL */
1062
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001063/*
1064 * Find internal function in table above.
1065 * Return index, or -1 if not found
1066 */
1067 int
1068find_internal_func(
1069 char_u *name) /* name of the function */
1070{
1071 int first = 0;
1072 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
1073 int cmp;
1074 int x;
1075
1076 /*
1077 * Find the function name in the table. Binary search.
1078 */
1079 while (first <= last)
1080 {
1081 x = first + ((unsigned)(last - first) >> 1);
1082 cmp = STRCMP(name, functions[x].f_name);
1083 if (cmp < 0)
1084 last = x - 1;
1085 else if (cmp > 0)
1086 first = x + 1;
1087 else
1088 return x;
1089 }
1090 return -1;
1091}
1092
1093 int
1094call_internal_func(
1095 char_u *name,
1096 int argcount,
1097 typval_T *argvars,
1098 typval_T *rettv)
1099{
1100 int i;
1101
1102 i = find_internal_func(name);
1103 if (i < 0)
1104 return ERROR_UNKNOWN;
1105 if (argcount < functions[i].f_min_argc)
1106 return ERROR_TOOFEW;
1107 if (argcount > functions[i].f_max_argc)
1108 return ERROR_TOOMANY;
1109 argvars[argcount].v_type = VAR_UNKNOWN;
1110 functions[i].f_func(argvars, rettv);
1111 return ERROR_NONE;
1112}
1113
1114/*
1115 * Return TRUE for a non-zero Number and a non-empty String.
1116 */
1117 static int
1118non_zero_arg(typval_T *argvars)
1119{
1120 return ((argvars[0].v_type == VAR_NUMBER
1121 && argvars[0].vval.v_number != 0)
1122 || (argvars[0].v_type == VAR_SPECIAL
1123 && argvars[0].vval.v_number == VVAL_TRUE)
1124 || (argvars[0].v_type == VAR_STRING
1125 && argvars[0].vval.v_string != NULL
1126 && *argvars[0].vval.v_string != NUL));
1127}
1128
1129/*
1130 * Get the lnum from the first argument.
1131 * Also accepts ".", "$", etc., but that only works for the current buffer.
1132 * Returns -1 on error.
1133 */
1134 static linenr_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001135tv_get_lnum(typval_T *argvars)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001136{
1137 typval_T rettv;
1138 linenr_T lnum;
1139
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001140 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001141 if (lnum == 0) /* no valid number, try using line() */
1142 {
1143 rettv.v_type = VAR_NUMBER;
1144 f_line(argvars, &rettv);
1145 lnum = (linenr_T)rettv.vval.v_number;
1146 clear_tv(&rettv);
1147 }
1148 return lnum;
1149}
1150
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001151/*
1152 * Get the lnum from the first argument.
1153 * Also accepts "$", then "buf" is used.
1154 * Returns 0 on error.
1155 */
1156 static linenr_T
1157tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
1158{
1159 if (argvars[0].v_type == VAR_STRING
1160 && argvars[0].vval.v_string != NULL
1161 && argvars[0].vval.v_string[0] == '$'
1162 && buf != NULL)
1163 return buf->b_ml.ml_line_count;
1164 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1165}
1166
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001167#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001168/*
1169 * Get the float value of "argvars[0]" into "f".
1170 * Returns FAIL when the argument is not a Number or Float.
1171 */
1172 static int
1173get_float_arg(typval_T *argvars, float_T *f)
1174{
1175 if (argvars[0].v_type == VAR_FLOAT)
1176 {
1177 *f = argvars[0].vval.v_float;
1178 return OK;
1179 }
1180 if (argvars[0].v_type == VAR_NUMBER)
1181 {
1182 *f = (float_T)argvars[0].vval.v_number;
1183 return OK;
1184 }
1185 EMSG(_("E808: Number or Float required"));
1186 return FAIL;
1187}
1188
1189/*
1190 * "abs(expr)" function
1191 */
1192 static void
1193f_abs(typval_T *argvars, typval_T *rettv)
1194{
1195 if (argvars[0].v_type == VAR_FLOAT)
1196 {
1197 rettv->v_type = VAR_FLOAT;
1198 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1199 }
1200 else
1201 {
1202 varnumber_T n;
1203 int error = FALSE;
1204
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001205 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001206 if (error)
1207 rettv->vval.v_number = -1;
1208 else if (n > 0)
1209 rettv->vval.v_number = n;
1210 else
1211 rettv->vval.v_number = -n;
1212 }
1213}
1214
1215/*
1216 * "acos()" function
1217 */
1218 static void
1219f_acos(typval_T *argvars, typval_T *rettv)
1220{
1221 float_T f = 0.0;
1222
1223 rettv->v_type = VAR_FLOAT;
1224 if (get_float_arg(argvars, &f) == OK)
1225 rettv->vval.v_float = acos(f);
1226 else
1227 rettv->vval.v_float = 0.0;
1228}
1229#endif
1230
1231/*
1232 * "add(list, item)" function
1233 */
1234 static void
1235f_add(typval_T *argvars, typval_T *rettv)
1236{
1237 list_T *l;
1238
1239 rettv->vval.v_number = 1; /* Default: Failed */
1240 if (argvars[0].v_type == VAR_LIST)
1241 {
1242 if ((l = argvars[0].vval.v_list) != NULL
1243 && !tv_check_lock(l->lv_lock,
1244 (char_u *)N_("add() argument"), TRUE)
1245 && list_append_tv(l, &argvars[1]) == OK)
1246 copy_tv(&argvars[0], rettv);
1247 }
1248 else
1249 EMSG(_(e_listreq));
1250}
1251
1252/*
1253 * "and(expr, expr)" function
1254 */
1255 static void
1256f_and(typval_T *argvars, typval_T *rettv)
1257{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001258 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1259 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001260}
1261
1262/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02001263 * If there is a window for "curbuf", make it the current window.
1264 */
1265 static void
1266find_win_for_curbuf(void)
1267{
1268 wininfo_T *wip;
1269
1270 for (wip = curbuf->b_wininfo; wip != NULL; wip = wip->wi_next)
1271 {
1272 if (wip->wi_win != NULL)
1273 {
1274 curwin = wip->wi_win;
1275 break;
1276 }
1277 }
1278}
1279
1280/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001281 * Set line or list of lines in buffer "buf".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001282 */
1283 static void
Bram Moolenaarca851592018-06-06 21:04:07 +02001284set_buffer_lines(
1285 buf_T *buf,
1286 linenr_T lnum_arg,
1287 int append,
1288 typval_T *lines,
1289 typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001290{
Bram Moolenaarca851592018-06-06 21:04:07 +02001291 linenr_T lnum = lnum_arg + (append ? 1 : 0);
1292 char_u *line = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001293 list_T *l = NULL;
1294 listitem_T *li = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001295 long added = 0;
Bram Moolenaarca851592018-06-06 21:04:07 +02001296 linenr_T append_lnum;
1297 buf_T *curbuf_save = NULL;
1298 win_T *curwin_save = NULL;
1299 int is_curbuf = buf == curbuf;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001300
Bram Moolenaarca851592018-06-06 21:04:07 +02001301 /* When using the current buffer ml_mfp will be set if needed. Useful when
1302 * setline() is used on startup. For other buffers the buffer must be
1303 * loaded. */
1304 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001305 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001306 rettv->vval.v_number = 1; /* FAIL */
1307 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001308 }
1309
Bram Moolenaarca851592018-06-06 21:04:07 +02001310 if (!is_curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001311 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001312 curbuf_save = curbuf;
1313 curwin_save = curwin;
1314 curbuf = buf;
Bram Moolenaard79a2622018-06-07 18:17:46 +02001315 find_win_for_curbuf();
Bram Moolenaarca851592018-06-06 21:04:07 +02001316 }
1317
1318 if (append)
1319 // appendbufline() uses the line number below which we insert
1320 append_lnum = lnum - 1;
1321 else
1322 // setbufline() uses the line number above which we insert, we only
1323 // append if it's below the last line
1324 append_lnum = curbuf->b_ml.ml_line_count;
1325
1326 if (lines->v_type == VAR_LIST)
1327 {
1328 l = lines->vval.v_list;
1329 li = l->lv_first;
1330 }
1331 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001332 line = tv_get_string_chk(lines);
Bram Moolenaarca851592018-06-06 21:04:07 +02001333
1334 /* default result is zero == OK */
1335 for (;;)
1336 {
1337 if (l != NULL)
1338 {
1339 /* list argument, get next string */
1340 if (li == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001341 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001342 line = tv_get_string_chk(&li->li_tv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001343 li = li->li_next;
1344 }
1345
Bram Moolenaarca851592018-06-06 21:04:07 +02001346 rettv->vval.v_number = 1; /* FAIL */
1347 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
1348 break;
1349
1350 /* When coming here from Insert mode, sync undo, so that this can be
1351 * undone separately from what was previously inserted. */
1352 if (u_sync_once == 2)
1353 {
1354 u_sync_once = 1; /* notify that u_sync() was called */
1355 u_sync(TRUE);
1356 }
1357
1358 if (!append && lnum <= curbuf->b_ml.ml_line_count)
1359 {
Bram Moolenaar21b50382019-01-04 18:07:24 +01001360 // Existing line, replace it.
1361 // Removes any existing text properties.
1362 if (u_savesub(lnum) == OK && ml_replace_len(
1363 lnum, line, (colnr_T)STRLEN(line) + 1, TRUE, TRUE) == OK)
Bram Moolenaarca851592018-06-06 21:04:07 +02001364 {
1365 changed_bytes(lnum, 0);
1366 if (is_curbuf && lnum == curwin->w_cursor.lnum)
1367 check_cursor_col();
1368 rettv->vval.v_number = 0; /* OK */
1369 }
1370 }
1371 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
1372 {
1373 /* append the line */
1374 ++added;
1375 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
1376 rettv->vval.v_number = 0; /* OK */
1377 }
1378
1379 if (l == NULL) /* only one string argument */
1380 break;
1381 ++lnum;
1382 }
1383
1384 if (added > 0)
1385 {
1386 win_T *wp;
1387 tabpage_T *tp;
1388
1389 appended_lines_mark(append_lnum, added);
1390 FOR_ALL_TAB_WINDOWS(tp, wp)
1391 if (wp->w_buffer == buf && wp->w_cursor.lnum > append_lnum)
1392 wp->w_cursor.lnum += added;
1393 check_cursor_col();
1394
Bram Moolenaarf2732452018-06-03 14:47:35 +02001395#ifdef FEAT_JOB_CHANNEL
1396 if (bt_prompt(curbuf) && (State & INSERT))
1397 // show the line with the prompt
1398 update_topline();
1399#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001400 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001401
1402 if (!is_curbuf)
1403 {
1404 curbuf = curbuf_save;
1405 curwin = curwin_save;
1406 }
1407}
1408
1409/*
1410 * "append(lnum, string/list)" function
1411 */
1412 static void
1413f_append(typval_T *argvars, typval_T *rettv)
1414{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001415 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaarca851592018-06-06 21:04:07 +02001416
1417 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1418}
1419
1420/*
1421 * "appendbufline(buf, lnum, string/list)" function
1422 */
1423 static void
1424f_appendbufline(typval_T *argvars, typval_T *rettv)
1425{
1426 linenr_T lnum;
1427 buf_T *buf;
1428
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001429 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarca851592018-06-06 21:04:07 +02001430 if (buf == NULL)
1431 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001432 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001433 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001434 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +02001435 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1436 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001437}
1438
1439/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001440 * "argc([window id])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001441 */
1442 static void
Bram Moolenaare6e39892018-10-25 12:32:11 +02001443f_argc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001444{
Bram Moolenaare6e39892018-10-25 12:32:11 +02001445 win_T *wp;
1446
1447 if (argvars[0].v_type == VAR_UNKNOWN)
1448 // use the current window
1449 rettv->vval.v_number = ARGCOUNT;
1450 else if (argvars[0].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001451 && tv_get_number(&argvars[0]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001452 // use the global argument list
1453 rettv->vval.v_number = GARGCOUNT;
1454 else
1455 {
1456 // use the argument list of the specified window
1457 wp = find_win_by_nr_or_id(&argvars[0]);
1458 if (wp != NULL)
1459 rettv->vval.v_number = WARGCOUNT(wp);
1460 else
1461 rettv->vval.v_number = -1;
1462 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001463}
1464
1465/*
1466 * "argidx()" function
1467 */
1468 static void
1469f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1470{
1471 rettv->vval.v_number = curwin->w_arg_idx;
1472}
1473
1474/*
1475 * "arglistid()" function
1476 */
1477 static void
1478f_arglistid(typval_T *argvars, typval_T *rettv)
1479{
1480 win_T *wp;
1481
1482 rettv->vval.v_number = -1;
1483 wp = find_tabwin(&argvars[0], &argvars[1]);
1484 if (wp != NULL)
1485 rettv->vval.v_number = wp->w_alist->id;
1486}
1487
1488/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001489 * Get the argument list for a given window
1490 */
1491 static void
1492get_arglist_as_rettv(aentry_T *arglist, int argcount, typval_T *rettv)
1493{
1494 int idx;
1495
1496 if (rettv_list_alloc(rettv) == OK && arglist != NULL)
1497 for (idx = 0; idx < argcount; ++idx)
1498 list_append_string(rettv->vval.v_list,
1499 alist_name(&arglist[idx]), -1);
1500}
1501
1502/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001503 * "argv(nr)" function
1504 */
1505 static void
1506f_argv(typval_T *argvars, typval_T *rettv)
1507{
1508 int idx;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001509 aentry_T *arglist = NULL;
1510 int argcount = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001511
1512 if (argvars[0].v_type != VAR_UNKNOWN)
1513 {
Bram Moolenaare6e39892018-10-25 12:32:11 +02001514 if (argvars[1].v_type == VAR_UNKNOWN)
1515 {
1516 arglist = ARGLIST;
1517 argcount = ARGCOUNT;
1518 }
1519 else if (argvars[1].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001520 && tv_get_number(&argvars[1]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001521 {
1522 arglist = GARGLIST;
1523 argcount = GARGCOUNT;
1524 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001525 else
Bram Moolenaare6e39892018-10-25 12:32:11 +02001526 {
1527 win_T *wp = find_win_by_nr_or_id(&argvars[1]);
1528
1529 if (wp != NULL)
1530 {
1531 /* Use the argument list of the specified window */
1532 arglist = WARGLIST(wp);
1533 argcount = WARGCOUNT(wp);
1534 }
1535 }
1536
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001537 rettv->v_type = VAR_STRING;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001538 rettv->vval.v_string = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001539 idx = tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02001540 if (arglist != NULL && idx >= 0 && idx < argcount)
1541 rettv->vval.v_string = vim_strsave(alist_name(&arglist[idx]));
1542 else if (idx == -1)
1543 get_arglist_as_rettv(arglist, argcount, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001544 }
Bram Moolenaare6e39892018-10-25 12:32:11 +02001545 else
1546 get_arglist_as_rettv(ARGLIST, ARGCOUNT, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001547}
1548
1549/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001550 * "assert_beeps(cmd [, error])" function
1551 */
1552 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001553f_assert_beeps(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001554{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001555 rettv->vval.v_number = assert_beeps(argvars);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001556}
1557
1558/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001559 * "assert_equal(expected, actual[, msg])" function
1560 */
1561 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001562f_assert_equal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001563{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001564 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_EQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001565}
1566
1567/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01001568 * "assert_equalfile(fname-one, fname-two)" function
1569 */
1570 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001571f_assert_equalfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaard96ff162018-02-18 22:13:29 +01001572{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001573 rettv->vval.v_number = assert_equalfile(argvars);
Bram Moolenaard96ff162018-02-18 22:13:29 +01001574}
1575
1576/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001577 * "assert_notequal(expected, actual[, msg])" function
1578 */
1579 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001580f_assert_notequal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001581{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001582 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001583}
1584
1585/*
1586 * "assert_exception(string[, msg])" function
1587 */
1588 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001589f_assert_exception(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001590{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001591 rettv->vval.v_number = assert_exception(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001592}
1593
1594/*
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02001595 * "assert_fails(cmd [, error[, msg]])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001596 */
1597 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001598f_assert_fails(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001599{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001600 rettv->vval.v_number = assert_fails(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001601}
1602
1603/*
1604 * "assert_false(actual[, msg])" function
1605 */
1606 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001607f_assert_false(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001608{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001609 rettv->vval.v_number = assert_bool(argvars, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001610}
1611
1612/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001613 * "assert_inrange(lower, upper[, msg])" function
1614 */
1615 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001616f_assert_inrange(typval_T *argvars, typval_T *rettv)
Bram Moolenaar61c04492016-07-23 15:35:35 +02001617{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001618 rettv->vval.v_number = assert_inrange(argvars);
Bram Moolenaar61c04492016-07-23 15:35:35 +02001619}
1620
1621/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001622 * "assert_match(pattern, actual[, msg])" function
1623 */
1624 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001625f_assert_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001626{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001627 rettv->vval.v_number = assert_match_common(argvars, ASSERT_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001628}
1629
1630/*
1631 * "assert_notmatch(pattern, actual[, msg])" function
1632 */
1633 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001634f_assert_notmatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001635{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001636 rettv->vval.v_number = assert_match_common(argvars, ASSERT_NOTMATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001637}
1638
1639/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001640 * "assert_report(msg)" function
1641 */
1642 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001643f_assert_report(typval_T *argvars, typval_T *rettv)
Bram Moolenaar42205552017-03-18 19:42:22 +01001644{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001645 rettv->vval.v_number = assert_report(argvars);
Bram Moolenaar42205552017-03-18 19:42:22 +01001646}
1647
1648/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001649 * "assert_true(actual[, msg])" function
1650 */
1651 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001652f_assert_true(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001653{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001654 rettv->vval.v_number = assert_bool(argvars, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001655}
1656
1657#ifdef FEAT_FLOAT
1658/*
1659 * "asin()" function
1660 */
1661 static void
1662f_asin(typval_T *argvars, typval_T *rettv)
1663{
1664 float_T f = 0.0;
1665
1666 rettv->v_type = VAR_FLOAT;
1667 if (get_float_arg(argvars, &f) == OK)
1668 rettv->vval.v_float = asin(f);
1669 else
1670 rettv->vval.v_float = 0.0;
1671}
1672
1673/*
1674 * "atan()" function
1675 */
1676 static void
1677f_atan(typval_T *argvars, typval_T *rettv)
1678{
1679 float_T f = 0.0;
1680
1681 rettv->v_type = VAR_FLOAT;
1682 if (get_float_arg(argvars, &f) == OK)
1683 rettv->vval.v_float = atan(f);
1684 else
1685 rettv->vval.v_float = 0.0;
1686}
1687
1688/*
1689 * "atan2()" function
1690 */
1691 static void
1692f_atan2(typval_T *argvars, typval_T *rettv)
1693{
1694 float_T fx = 0.0, fy = 0.0;
1695
1696 rettv->v_type = VAR_FLOAT;
1697 if (get_float_arg(argvars, &fx) == OK
1698 && get_float_arg(&argvars[1], &fy) == OK)
1699 rettv->vval.v_float = atan2(fx, fy);
1700 else
1701 rettv->vval.v_float = 0.0;
1702}
1703#endif
1704
1705/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001706 * "balloon_show()" function
1707 */
1708#ifdef FEAT_BEVAL
1709 static void
1710f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1711{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001712 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001713 {
1714 if (argvars[0].v_type == VAR_LIST
1715# ifdef FEAT_GUI
1716 && !gui.in_use
1717# endif
1718 )
1719 post_balloon(balloonEval, NULL, argvars[0].vval.v_list);
1720 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001721 post_balloon(balloonEval, tv_get_string_chk(&argvars[0]), NULL);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001722 }
1723}
1724
Bram Moolenaar669a8282017-11-19 20:13:05 +01001725# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001726 static void
1727f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1728{
1729 if (rettv_list_alloc(rettv) == OK)
1730 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001731 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001732
1733 if (msg != NULL)
1734 {
1735 pumitem_T *array;
1736 int size = split_message(msg, &array);
1737 int i;
1738
1739 /* Skip the first and last item, they are always empty. */
1740 for (i = 1; i < size - 1; ++i)
1741 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001742 while (size > 0)
1743 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001744 vim_free(array);
1745 }
1746 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001747}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001748# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001749#endif
1750
1751/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001752 * "browse(save, title, initdir, default)" function
1753 */
1754 static void
1755f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1756{
1757#ifdef FEAT_BROWSE
1758 int save;
1759 char_u *title;
1760 char_u *initdir;
1761 char_u *defname;
1762 char_u buf[NUMBUFLEN];
1763 char_u buf2[NUMBUFLEN];
1764 int error = FALSE;
1765
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001766 save = (int)tv_get_number_chk(&argvars[0], &error);
1767 title = tv_get_string_chk(&argvars[1]);
1768 initdir = tv_get_string_buf_chk(&argvars[2], buf);
1769 defname = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001770
1771 if (error || title == NULL || initdir == NULL || defname == NULL)
1772 rettv->vval.v_string = NULL;
1773 else
1774 rettv->vval.v_string =
1775 do_browse(save ? BROWSE_SAVE : 0,
1776 title, defname, NULL, initdir, NULL, curbuf);
1777#else
1778 rettv->vval.v_string = NULL;
1779#endif
1780 rettv->v_type = VAR_STRING;
1781}
1782
1783/*
1784 * "browsedir(title, initdir)" function
1785 */
1786 static void
1787f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1788{
1789#ifdef FEAT_BROWSE
1790 char_u *title;
1791 char_u *initdir;
1792 char_u buf[NUMBUFLEN];
1793
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001794 title = tv_get_string_chk(&argvars[0]);
1795 initdir = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001796
1797 if (title == NULL || initdir == NULL)
1798 rettv->vval.v_string = NULL;
1799 else
1800 rettv->vval.v_string = do_browse(BROWSE_DIR,
1801 title, NULL, NULL, initdir, NULL, curbuf);
1802#else
1803 rettv->vval.v_string = NULL;
1804#endif
1805 rettv->v_type = VAR_STRING;
1806}
1807
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001808/*
1809 * Find a buffer by number or exact name.
1810 */
1811 static buf_T *
1812find_buffer(typval_T *avar)
1813{
1814 buf_T *buf = NULL;
1815
1816 if (avar->v_type == VAR_NUMBER)
1817 buf = buflist_findnr((int)avar->vval.v_number);
1818 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1819 {
1820 buf = buflist_findname_exp(avar->vval.v_string);
1821 if (buf == NULL)
1822 {
1823 /* No full path name match, try a match with a URL or a "nofile"
1824 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001825 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001826 if (buf->b_fname != NULL
1827 && (path_with_url(buf->b_fname)
1828#ifdef FEAT_QUICKFIX
1829 || bt_nofile(buf)
1830#endif
1831 )
1832 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1833 break;
1834 }
1835 }
1836 return buf;
1837}
1838
1839/*
1840 * "bufexists(expr)" function
1841 */
1842 static void
1843f_bufexists(typval_T *argvars, typval_T *rettv)
1844{
1845 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1846}
1847
1848/*
1849 * "buflisted(expr)" function
1850 */
1851 static void
1852f_buflisted(typval_T *argvars, typval_T *rettv)
1853{
1854 buf_T *buf;
1855
1856 buf = find_buffer(&argvars[0]);
1857 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1858}
1859
1860/*
1861 * "bufloaded(expr)" function
1862 */
1863 static void
1864f_bufloaded(typval_T *argvars, typval_T *rettv)
1865{
1866 buf_T *buf;
1867
1868 buf = find_buffer(&argvars[0]);
1869 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1870}
1871
1872 buf_T *
1873buflist_find_by_name(char_u *name, int curtab_only)
1874{
1875 int save_magic;
1876 char_u *save_cpo;
1877 buf_T *buf;
1878
1879 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1880 save_magic = p_magic;
1881 p_magic = TRUE;
1882 save_cpo = p_cpo;
1883 p_cpo = (char_u *)"";
1884
1885 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1886 TRUE, FALSE, curtab_only));
1887
1888 p_magic = save_magic;
1889 p_cpo = save_cpo;
1890 return buf;
1891}
1892
1893/*
1894 * Get buffer by number or pattern.
1895 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001896 buf_T *
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001897tv_get_buf(typval_T *tv, int curtab_only)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001898{
1899 char_u *name = tv->vval.v_string;
1900 buf_T *buf;
1901
1902 if (tv->v_type == VAR_NUMBER)
1903 return buflist_findnr((int)tv->vval.v_number);
1904 if (tv->v_type != VAR_STRING)
1905 return NULL;
1906 if (name == NULL || *name == NUL)
1907 return curbuf;
1908 if (name[0] == '$' && name[1] == NUL)
1909 return lastbuf;
1910
1911 buf = buflist_find_by_name(name, curtab_only);
1912
1913 /* If not found, try expanding the name, like done for bufexists(). */
1914 if (buf == NULL)
1915 buf = find_buffer(tv);
1916
1917 return buf;
1918}
1919
1920/*
1921 * "bufname(expr)" function
1922 */
1923 static void
1924f_bufname(typval_T *argvars, typval_T *rettv)
1925{
1926 buf_T *buf;
1927
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001928 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001929 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001930 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001931 rettv->v_type = VAR_STRING;
1932 if (buf != NULL && buf->b_fname != NULL)
1933 rettv->vval.v_string = vim_strsave(buf->b_fname);
1934 else
1935 rettv->vval.v_string = NULL;
1936 --emsg_off;
1937}
1938
1939/*
1940 * "bufnr(expr)" function
1941 */
1942 static void
1943f_bufnr(typval_T *argvars, typval_T *rettv)
1944{
1945 buf_T *buf;
1946 int error = FALSE;
1947 char_u *name;
1948
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001949 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001950 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001951 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001952 --emsg_off;
1953
1954 /* If the buffer isn't found and the second argument is not zero create a
1955 * new buffer. */
1956 if (buf == NULL
1957 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001958 && tv_get_number_chk(&argvars[1], &error) != 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001959 && !error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001960 && (name = tv_get_string_chk(&argvars[0])) != NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001961 && !error)
1962 buf = buflist_new(name, NULL, (linenr_T)1, 0);
1963
1964 if (buf != NULL)
1965 rettv->vval.v_number = buf->b_fnum;
1966 else
1967 rettv->vval.v_number = -1;
1968}
1969
1970 static void
1971buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
1972{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001973 win_T *wp;
1974 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001975 buf_T *buf;
1976
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001977 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001978 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001979 buf = tv_get_buf(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02001980 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001981 {
1982 ++winnr;
1983 if (wp->w_buffer == buf)
1984 break;
1985 }
1986 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001987 --emsg_off;
1988}
1989
1990/*
1991 * "bufwinid(nr)" function
1992 */
1993 static void
1994f_bufwinid(typval_T *argvars, typval_T *rettv)
1995{
1996 buf_win_common(argvars, rettv, FALSE);
1997}
1998
1999/*
2000 * "bufwinnr(nr)" function
2001 */
2002 static void
2003f_bufwinnr(typval_T *argvars, typval_T *rettv)
2004{
2005 buf_win_common(argvars, rettv, TRUE);
2006}
2007
2008/*
2009 * "byte2line(byte)" function
2010 */
2011 static void
2012f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2013{
2014#ifndef FEAT_BYTEOFF
2015 rettv->vval.v_number = -1;
2016#else
2017 long boff = 0;
2018
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002019 boff = tv_get_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002020 if (boff < 0)
2021 rettv->vval.v_number = -1;
2022 else
2023 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2024 (linenr_T)0, &boff);
2025#endif
2026}
2027
2028 static void
2029byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2030{
2031#ifdef FEAT_MBYTE
2032 char_u *t;
2033#endif
2034 char_u *str;
2035 varnumber_T idx;
2036
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002037 str = tv_get_string_chk(&argvars[0]);
2038 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002039 rettv->vval.v_number = -1;
2040 if (str == NULL || idx < 0)
2041 return;
2042
2043#ifdef FEAT_MBYTE
2044 t = str;
2045 for ( ; idx > 0; idx--)
2046 {
2047 if (*t == NUL) /* EOL reached */
2048 return;
2049 if (enc_utf8 && comp)
2050 t += utf_ptr2len(t);
2051 else
2052 t += (*mb_ptr2len)(t);
2053 }
2054 rettv->vval.v_number = (varnumber_T)(t - str);
2055#else
2056 if ((size_t)idx <= STRLEN(str))
2057 rettv->vval.v_number = idx;
2058#endif
2059}
2060
2061/*
2062 * "byteidx()" function
2063 */
2064 static void
2065f_byteidx(typval_T *argvars, typval_T *rettv)
2066{
2067 byteidx(argvars, rettv, FALSE);
2068}
2069
2070/*
2071 * "byteidxcomp()" function
2072 */
2073 static void
2074f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2075{
2076 byteidx(argvars, rettv, TRUE);
2077}
2078
2079/*
2080 * "call(func, arglist [, dict])" function
2081 */
2082 static void
2083f_call(typval_T *argvars, typval_T *rettv)
2084{
2085 char_u *func;
2086 partial_T *partial = NULL;
2087 dict_T *selfdict = NULL;
2088
2089 if (argvars[1].v_type != VAR_LIST)
2090 {
2091 EMSG(_(e_listreq));
2092 return;
2093 }
2094 if (argvars[1].vval.v_list == NULL)
2095 return;
2096
2097 if (argvars[0].v_type == VAR_FUNC)
2098 func = argvars[0].vval.v_string;
2099 else if (argvars[0].v_type == VAR_PARTIAL)
2100 {
2101 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002102 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002103 }
2104 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002105 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002106 if (*func == NUL)
2107 return; /* type error or empty name */
2108
2109 if (argvars[2].v_type != VAR_UNKNOWN)
2110 {
2111 if (argvars[2].v_type != VAR_DICT)
2112 {
2113 EMSG(_(e_dictreq));
2114 return;
2115 }
2116 selfdict = argvars[2].vval.v_dict;
2117 }
2118
2119 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2120}
2121
2122#ifdef FEAT_FLOAT
2123/*
2124 * "ceil({float})" function
2125 */
2126 static void
2127f_ceil(typval_T *argvars, typval_T *rettv)
2128{
2129 float_T f = 0.0;
2130
2131 rettv->v_type = VAR_FLOAT;
2132 if (get_float_arg(argvars, &f) == OK)
2133 rettv->vval.v_float = ceil(f);
2134 else
2135 rettv->vval.v_float = 0.0;
2136}
2137#endif
2138
2139#ifdef FEAT_JOB_CHANNEL
2140/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002141 * "ch_canread()" function
2142 */
2143 static void
2144f_ch_canread(typval_T *argvars, typval_T *rettv)
2145{
Bram Moolenaar958dc692016-12-01 15:34:12 +01002146 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002147
2148 rettv->vval.v_number = 0;
2149 if (channel != NULL)
2150 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
2151 || channel_has_readahead(channel, PART_OUT)
2152 || channel_has_readahead(channel, PART_ERR);
2153}
2154
2155/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002156 * "ch_close()" function
2157 */
2158 static void
2159f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
2160{
2161 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2162
2163 if (channel != NULL)
2164 {
2165 channel_close(channel, FALSE);
2166 channel_clear(channel);
2167 }
2168}
2169
2170/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002171 * "ch_close()" function
2172 */
2173 static void
2174f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
2175{
2176 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2177
2178 if (channel != NULL)
2179 channel_close_in(channel);
2180}
2181
2182/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002183 * "ch_getbufnr()" function
2184 */
2185 static void
2186f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
2187{
2188 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2189
2190 rettv->vval.v_number = -1;
2191 if (channel != NULL)
2192 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002193 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002194 int part;
2195
2196 if (STRCMP(what, "err") == 0)
2197 part = PART_ERR;
2198 else if (STRCMP(what, "out") == 0)
2199 part = PART_OUT;
2200 else if (STRCMP(what, "in") == 0)
2201 part = PART_IN;
2202 else
2203 part = PART_SOCK;
2204 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
2205 rettv->vval.v_number =
2206 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
2207 }
2208}
2209
2210/*
2211 * "ch_getjob()" function
2212 */
2213 static void
2214f_ch_getjob(typval_T *argvars, typval_T *rettv)
2215{
2216 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2217
2218 if (channel != NULL)
2219 {
2220 rettv->v_type = VAR_JOB;
2221 rettv->vval.v_job = channel->ch_job;
2222 if (channel->ch_job != NULL)
2223 ++channel->ch_job->jv_refcount;
2224 }
2225}
2226
2227/*
2228 * "ch_info()" function
2229 */
2230 static void
2231f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
2232{
2233 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2234
2235 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
2236 channel_info(channel, rettv->vval.v_dict);
2237}
2238
2239/*
2240 * "ch_log()" function
2241 */
2242 static void
2243f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
2244{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002245 char_u *msg = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002246 channel_T *channel = NULL;
2247
2248 if (argvars[1].v_type != VAR_UNKNOWN)
2249 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
2250
Bram Moolenaard5359b22018-04-05 22:44:39 +02002251 ch_log(channel, "%s", msg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002252}
2253
2254/*
2255 * "ch_logfile()" function
2256 */
2257 static void
2258f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
2259{
2260 char_u *fname;
2261 char_u *opt = (char_u *)"";
2262 char_u buf[NUMBUFLEN];
2263
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02002264 /* Don't open a file in restricted mode. */
2265 if (check_restricted() || check_secure())
2266 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002267 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002268 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002269 opt = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002270 ch_logfile(fname, opt);
2271}
2272
2273/*
2274 * "ch_open()" function
2275 */
2276 static void
2277f_ch_open(typval_T *argvars, typval_T *rettv)
2278{
2279 rettv->v_type = VAR_CHANNEL;
2280 if (check_restricted() || check_secure())
2281 return;
2282 rettv->vval.v_channel = channel_open_func(argvars);
2283}
2284
2285/*
2286 * "ch_read()" function
2287 */
2288 static void
2289f_ch_read(typval_T *argvars, typval_T *rettv)
2290{
2291 common_channel_read(argvars, rettv, FALSE);
2292}
2293
2294/*
2295 * "ch_readraw()" function
2296 */
2297 static void
2298f_ch_readraw(typval_T *argvars, typval_T *rettv)
2299{
2300 common_channel_read(argvars, rettv, TRUE);
2301}
2302
2303/*
2304 * "ch_evalexpr()" function
2305 */
2306 static void
2307f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2308{
2309 ch_expr_common(argvars, rettv, TRUE);
2310}
2311
2312/*
2313 * "ch_sendexpr()" function
2314 */
2315 static void
2316f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2317{
2318 ch_expr_common(argvars, rettv, FALSE);
2319}
2320
2321/*
2322 * "ch_evalraw()" function
2323 */
2324 static void
2325f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2326{
2327 ch_raw_common(argvars, rettv, TRUE);
2328}
2329
2330/*
2331 * "ch_sendraw()" function
2332 */
2333 static void
2334f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2335{
2336 ch_raw_common(argvars, rettv, FALSE);
2337}
2338
2339/*
2340 * "ch_setoptions()" function
2341 */
2342 static void
2343f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2344{
2345 channel_T *channel;
2346 jobopt_T opt;
2347
2348 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2349 if (channel == NULL)
2350 return;
2351 clear_job_options(&opt);
2352 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002353 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002354 channel_set_options(channel, &opt);
2355 free_job_options(&opt);
2356}
2357
2358/*
2359 * "ch_status()" function
2360 */
2361 static void
2362f_ch_status(typval_T *argvars, typval_T *rettv)
2363{
2364 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002365 jobopt_T opt;
2366 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002367
2368 /* return an empty string by default */
2369 rettv->v_type = VAR_STRING;
2370 rettv->vval.v_string = NULL;
2371
2372 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002373
2374 if (argvars[1].v_type != VAR_UNKNOWN)
2375 {
2376 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002377 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002378 && (opt.jo_set & JO_PART))
2379 part = opt.jo_part;
2380 }
2381
2382 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002383}
2384#endif
2385
2386/*
2387 * "changenr()" function
2388 */
2389 static void
2390f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2391{
2392 rettv->vval.v_number = curbuf->b_u_seq_cur;
2393}
2394
2395/*
2396 * "char2nr(string)" function
2397 */
2398 static void
2399f_char2nr(typval_T *argvars, typval_T *rettv)
2400{
2401#ifdef FEAT_MBYTE
2402 if (has_mbyte)
2403 {
2404 int utf8 = 0;
2405
2406 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002407 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002408
2409 if (utf8)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002410 rettv->vval.v_number = (*utf_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002411 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002412 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002413 }
2414 else
2415#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002416 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002417}
2418
2419/*
2420 * "cindent(lnum)" function
2421 */
2422 static void
2423f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2424{
2425#ifdef FEAT_CINDENT
2426 pos_T pos;
2427 linenr_T lnum;
2428
2429 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002430 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002431 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2432 {
2433 curwin->w_cursor.lnum = lnum;
2434 rettv->vval.v_number = get_c_indent();
2435 curwin->w_cursor = pos;
2436 }
2437 else
2438#endif
2439 rettv->vval.v_number = -1;
2440}
2441
2442/*
2443 * "clearmatches()" function
2444 */
2445 static void
2446f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2447{
2448#ifdef FEAT_SEARCH_EXTRA
2449 clear_matches(curwin);
2450#endif
2451}
2452
2453/*
2454 * "col(string)" function
2455 */
2456 static void
2457f_col(typval_T *argvars, typval_T *rettv)
2458{
2459 colnr_T col = 0;
2460 pos_T *fp;
2461 int fnum = curbuf->b_fnum;
2462
2463 fp = var2fpos(&argvars[0], FALSE, &fnum);
2464 if (fp != NULL && fnum == curbuf->b_fnum)
2465 {
2466 if (fp->col == MAXCOL)
2467 {
2468 /* '> can be MAXCOL, get the length of the line then */
2469 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2470 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2471 else
2472 col = MAXCOL;
2473 }
2474 else
2475 {
2476 col = fp->col + 1;
2477#ifdef FEAT_VIRTUALEDIT
2478 /* col(".") when the cursor is on the NUL at the end of the line
2479 * because of "coladd" can be seen as an extra column. */
2480 if (virtual_active() && fp == &curwin->w_cursor)
2481 {
2482 char_u *p = ml_get_cursor();
2483
2484 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2485 curwin->w_virtcol - curwin->w_cursor.coladd))
2486 {
2487# ifdef FEAT_MBYTE
2488 int l;
2489
2490 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2491 col += l;
2492# else
2493 if (*p != NUL && p[1] == NUL)
2494 ++col;
2495# endif
2496 }
2497 }
2498#endif
2499 }
2500 }
2501 rettv->vval.v_number = col;
2502}
2503
2504#if defined(FEAT_INS_EXPAND)
2505/*
2506 * "complete()" function
2507 */
2508 static void
2509f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2510{
2511 int startcol;
2512
2513 if ((State & INSERT) == 0)
2514 {
2515 EMSG(_("E785: complete() can only be used in Insert mode"));
2516 return;
2517 }
2518
2519 /* Check for undo allowed here, because if something was already inserted
2520 * the line was already saved for undo and this check isn't done. */
2521 if (!undo_allowed())
2522 return;
2523
2524 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2525 {
2526 EMSG(_(e_invarg));
2527 return;
2528 }
2529
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002530 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002531 if (startcol <= 0)
2532 return;
2533
2534 set_completion(startcol - 1, argvars[1].vval.v_list);
2535}
2536
2537/*
2538 * "complete_add()" function
2539 */
2540 static void
2541f_complete_add(typval_T *argvars, typval_T *rettv)
2542{
2543 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2544}
2545
2546/*
2547 * "complete_check()" function
2548 */
2549 static void
2550f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2551{
2552 int saved = RedrawingDisabled;
2553
2554 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002555 ins_compl_check_keys(0, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002556 rettv->vval.v_number = compl_interrupted;
2557 RedrawingDisabled = saved;
2558}
2559#endif
2560
2561/*
2562 * "confirm(message, buttons[, default [, type]])" function
2563 */
2564 static void
2565f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2566{
2567#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2568 char_u *message;
2569 char_u *buttons = NULL;
2570 char_u buf[NUMBUFLEN];
2571 char_u buf2[NUMBUFLEN];
2572 int def = 1;
2573 int type = VIM_GENERIC;
2574 char_u *typestr;
2575 int error = FALSE;
2576
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002577 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002578 if (message == NULL)
2579 error = TRUE;
2580 if (argvars[1].v_type != VAR_UNKNOWN)
2581 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002582 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002583 if (buttons == NULL)
2584 error = TRUE;
2585 if (argvars[2].v_type != VAR_UNKNOWN)
2586 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002587 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002588 if (argvars[3].v_type != VAR_UNKNOWN)
2589 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002590 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002591 if (typestr == NULL)
2592 error = TRUE;
2593 else
2594 {
2595 switch (TOUPPER_ASC(*typestr))
2596 {
2597 case 'E': type = VIM_ERROR; break;
2598 case 'Q': type = VIM_QUESTION; break;
2599 case 'I': type = VIM_INFO; break;
2600 case 'W': type = VIM_WARNING; break;
2601 case 'G': type = VIM_GENERIC; break;
2602 }
2603 }
2604 }
2605 }
2606 }
2607
2608 if (buttons == NULL || *buttons == NUL)
2609 buttons = (char_u *)_("&Ok");
2610
2611 if (!error)
2612 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2613 def, NULL, FALSE);
2614#endif
2615}
2616
2617/*
2618 * "copy()" function
2619 */
2620 static void
2621f_copy(typval_T *argvars, typval_T *rettv)
2622{
2623 item_copy(&argvars[0], rettv, FALSE, 0);
2624}
2625
2626#ifdef FEAT_FLOAT
2627/*
2628 * "cos()" function
2629 */
2630 static void
2631f_cos(typval_T *argvars, typval_T *rettv)
2632{
2633 float_T f = 0.0;
2634
2635 rettv->v_type = VAR_FLOAT;
2636 if (get_float_arg(argvars, &f) == OK)
2637 rettv->vval.v_float = cos(f);
2638 else
2639 rettv->vval.v_float = 0.0;
2640}
2641
2642/*
2643 * "cosh()" function
2644 */
2645 static void
2646f_cosh(typval_T *argvars, typval_T *rettv)
2647{
2648 float_T f = 0.0;
2649
2650 rettv->v_type = VAR_FLOAT;
2651 if (get_float_arg(argvars, &f) == OK)
2652 rettv->vval.v_float = cosh(f);
2653 else
2654 rettv->vval.v_float = 0.0;
2655}
2656#endif
2657
2658/*
2659 * "count()" function
2660 */
2661 static void
2662f_count(typval_T *argvars, typval_T *rettv)
2663{
2664 long n = 0;
2665 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002666 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002667
Bram Moolenaar9966b212017-07-28 16:46:57 +02002668 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002669 ic = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002670
2671 if (argvars[0].v_type == VAR_STRING)
2672 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002673 char_u *expr = tv_get_string_chk(&argvars[1]);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002674 char_u *p = argvars[0].vval.v_string;
2675 char_u *next;
2676
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002677 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002678 {
2679 if (ic)
2680 {
2681 size_t len = STRLEN(expr);
2682
2683 while (*p != NUL)
2684 {
2685 if (MB_STRNICMP(p, expr, len) == 0)
2686 {
2687 ++n;
2688 p += len;
2689 }
2690 else
2691 MB_PTR_ADV(p);
2692 }
2693 }
2694 else
2695 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2696 != NULL)
2697 {
2698 ++n;
2699 p = next + STRLEN(expr);
2700 }
2701 }
2702
2703 }
2704 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002705 {
2706 listitem_T *li;
2707 list_T *l;
2708 long idx;
2709
2710 if ((l = argvars[0].vval.v_list) != NULL)
2711 {
2712 li = l->lv_first;
2713 if (argvars[2].v_type != VAR_UNKNOWN)
2714 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002715 if (argvars[3].v_type != VAR_UNKNOWN)
2716 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002717 idx = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002718 if (!error)
2719 {
2720 li = list_find(l, idx);
2721 if (li == NULL)
2722 EMSGN(_(e_listidx), idx);
2723 }
2724 }
2725 if (error)
2726 li = NULL;
2727 }
2728
2729 for ( ; li != NULL; li = li->li_next)
2730 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2731 ++n;
2732 }
2733 }
2734 else if (argvars[0].v_type == VAR_DICT)
2735 {
2736 int todo;
2737 dict_T *d;
2738 hashitem_T *hi;
2739
2740 if ((d = argvars[0].vval.v_dict) != NULL)
2741 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002742 if (argvars[2].v_type != VAR_UNKNOWN)
2743 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002744 if (argvars[3].v_type != VAR_UNKNOWN)
2745 EMSG(_(e_invarg));
2746 }
2747
2748 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2749 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2750 {
2751 if (!HASHITEM_EMPTY(hi))
2752 {
2753 --todo;
2754 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2755 ++n;
2756 }
2757 }
2758 }
2759 }
2760 else
2761 EMSG2(_(e_listdictarg), "count()");
2762 rettv->vval.v_number = n;
2763}
2764
2765/*
2766 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2767 *
2768 * Checks the existence of a cscope connection.
2769 */
2770 static void
2771f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2772{
2773#ifdef FEAT_CSCOPE
2774 int num = 0;
2775 char_u *dbpath = NULL;
2776 char_u *prepend = NULL;
2777 char_u buf[NUMBUFLEN];
2778
2779 if (argvars[0].v_type != VAR_UNKNOWN
2780 && argvars[1].v_type != VAR_UNKNOWN)
2781 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002782 num = (int)tv_get_number(&argvars[0]);
2783 dbpath = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002784 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002785 prepend = tv_get_string_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002786 }
2787
2788 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2789#endif
2790}
2791
2792/*
2793 * "cursor(lnum, col)" function, or
2794 * "cursor(list)"
2795 *
2796 * Moves the cursor to the specified line and column.
2797 * Returns 0 when the position could be set, -1 otherwise.
2798 */
2799 static void
2800f_cursor(typval_T *argvars, typval_T *rettv)
2801{
2802 long line, col;
2803#ifdef FEAT_VIRTUALEDIT
2804 long coladd = 0;
2805#endif
2806 int set_curswant = TRUE;
2807
2808 rettv->vval.v_number = -1;
2809 if (argvars[1].v_type == VAR_UNKNOWN)
2810 {
2811 pos_T pos;
2812 colnr_T curswant = -1;
2813
2814 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2815 {
2816 EMSG(_(e_invarg));
2817 return;
2818 }
2819 line = pos.lnum;
2820 col = pos.col;
2821#ifdef FEAT_VIRTUALEDIT
2822 coladd = pos.coladd;
2823#endif
2824 if (curswant >= 0)
2825 {
2826 curwin->w_curswant = curswant - 1;
2827 set_curswant = FALSE;
2828 }
2829 }
2830 else
2831 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002832 line = tv_get_lnum(argvars);
2833 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002834#ifdef FEAT_VIRTUALEDIT
2835 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002836 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002837#endif
2838 }
2839 if (line < 0 || col < 0
2840#ifdef FEAT_VIRTUALEDIT
2841 || coladd < 0
2842#endif
2843 )
2844 return; /* type error; errmsg already given */
2845 if (line > 0)
2846 curwin->w_cursor.lnum = line;
2847 if (col > 0)
2848 curwin->w_cursor.col = col - 1;
2849#ifdef FEAT_VIRTUALEDIT
2850 curwin->w_cursor.coladd = coladd;
2851#endif
2852
2853 /* Make sure the cursor is in a valid position. */
2854 check_cursor();
2855#ifdef FEAT_MBYTE
2856 /* Correct cursor for multi-byte character. */
2857 if (has_mbyte)
2858 mb_adjust_cursor();
2859#endif
2860
2861 curwin->w_set_curswant = set_curswant;
2862 rettv->vval.v_number = 0;
2863}
2864
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002865#ifdef WIN3264
2866/*
2867 * "debugbreak()" function
2868 */
2869 static void
2870f_debugbreak(typval_T *argvars, typval_T *rettv)
2871{
2872 int pid;
2873
2874 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002875 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002876 if (pid == 0)
2877 EMSG(_(e_invarg));
2878 else
2879 {
2880 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2881
2882 if (hProcess != NULL)
2883 {
2884 DebugBreakProcess(hProcess);
2885 CloseHandle(hProcess);
2886 rettv->vval.v_number = OK;
2887 }
2888 }
2889}
2890#endif
2891
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002892/*
2893 * "deepcopy()" function
2894 */
2895 static void
2896f_deepcopy(typval_T *argvars, typval_T *rettv)
2897{
2898 int noref = 0;
2899 int copyID;
2900
2901 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002902 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002903 if (noref < 0 || noref > 1)
2904 EMSG(_(e_invarg));
2905 else
2906 {
2907 copyID = get_copyID();
2908 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2909 }
2910}
2911
2912/*
2913 * "delete()" function
2914 */
2915 static void
2916f_delete(typval_T *argvars, typval_T *rettv)
2917{
2918 char_u nbuf[NUMBUFLEN];
2919 char_u *name;
2920 char_u *flags;
2921
2922 rettv->vval.v_number = -1;
2923 if (check_restricted() || check_secure())
2924 return;
2925
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002926 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002927 if (name == NULL || *name == NUL)
2928 {
2929 EMSG(_(e_invarg));
2930 return;
2931 }
2932
2933 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002934 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002935 else
2936 flags = (char_u *)"";
2937
2938 if (*flags == NUL)
2939 /* delete a file */
2940 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2941 else if (STRCMP(flags, "d") == 0)
2942 /* delete an empty directory */
2943 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2944 else if (STRCMP(flags, "rf") == 0)
2945 /* delete a directory recursively */
2946 rettv->vval.v_number = delete_recursive(name);
2947 else
2948 EMSG2(_(e_invexpr2), flags);
2949}
2950
2951/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02002952 * "deletebufline()" function
2953 */
2954 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02002955f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02002956{
2957 buf_T *buf;
2958 linenr_T first, last;
2959 linenr_T lnum;
2960 long count;
2961 int is_curbuf;
2962 buf_T *curbuf_save = NULL;
2963 win_T *curwin_save = NULL;
2964 tabpage_T *tp;
2965 win_T *wp;
2966
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002967 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002968 if (buf == NULL)
2969 {
2970 rettv->vval.v_number = 1; /* FAIL */
2971 return;
2972 }
2973 is_curbuf = buf == curbuf;
2974
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002975 first = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002976 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002977 last = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002978 else
2979 last = first;
2980
2981 if (buf->b_ml.ml_mfp == NULL || first < 1
2982 || first > buf->b_ml.ml_line_count || last < first)
2983 {
2984 rettv->vval.v_number = 1; /* FAIL */
2985 return;
2986 }
2987
2988 if (!is_curbuf)
2989 {
2990 curbuf_save = curbuf;
2991 curwin_save = curwin;
2992 curbuf = buf;
2993 find_win_for_curbuf();
2994 }
2995 if (last > curbuf->b_ml.ml_line_count)
2996 last = curbuf->b_ml.ml_line_count;
2997 count = last - first + 1;
2998
2999 // When coming here from Insert mode, sync undo, so that this can be
3000 // undone separately from what was previously inserted.
3001 if (u_sync_once == 2)
3002 {
3003 u_sync_once = 1; // notify that u_sync() was called
3004 u_sync(TRUE);
3005 }
3006
3007 if (u_save(first - 1, last + 1) == FAIL)
3008 {
3009 rettv->vval.v_number = 1; /* FAIL */
3010 return;
3011 }
3012
3013 for (lnum = first; lnum <= last; ++lnum)
3014 ml_delete(first, TRUE);
3015
3016 FOR_ALL_TAB_WINDOWS(tp, wp)
3017 if (wp->w_buffer == buf)
3018 {
3019 if (wp->w_cursor.lnum > last)
3020 wp->w_cursor.lnum -= count;
3021 else if (wp->w_cursor.lnum> first)
3022 wp->w_cursor.lnum = first;
3023 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
3024 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
3025 }
3026 check_cursor_col();
3027 deleted_lines_mark(first, count);
3028
3029 if (!is_curbuf)
3030 {
3031 curbuf = curbuf_save;
3032 curwin = curwin_save;
3033 }
3034}
3035
3036/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003037 * "did_filetype()" function
3038 */
3039 static void
3040f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3041{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003042 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003043}
3044
3045/*
3046 * "diff_filler()" function
3047 */
3048 static void
3049f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3050{
3051#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003052 rettv->vval.v_number = diff_check_fill(curwin, tv_get_lnum(argvars));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003053#endif
3054}
3055
3056/*
3057 * "diff_hlID()" function
3058 */
3059 static void
3060f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3061{
3062#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003063 linenr_T lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003064 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01003065 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003066 static int fnum = 0;
3067 static int change_start = 0;
3068 static int change_end = 0;
3069 static hlf_T hlID = (hlf_T)0;
3070 int filler_lines;
3071 int col;
3072
3073 if (lnum < 0) /* ignore type error in {lnum} arg */
3074 lnum = 0;
3075 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003076 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003077 || fnum != curbuf->b_fnum)
3078 {
3079 /* New line, buffer, change: need to get the values. */
3080 filler_lines = diff_check(curwin, lnum);
3081 if (filler_lines < 0)
3082 {
3083 if (filler_lines == -1)
3084 {
3085 change_start = MAXCOL;
3086 change_end = -1;
3087 if (diff_find_change(curwin, lnum, &change_start, &change_end))
3088 hlID = HLF_ADD; /* added line */
3089 else
3090 hlID = HLF_CHD; /* changed line */
3091 }
3092 else
3093 hlID = HLF_ADD; /* added line */
3094 }
3095 else
3096 hlID = (hlf_T)0;
3097 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003098 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003099 fnum = curbuf->b_fnum;
3100 }
3101
3102 if (hlID == HLF_CHD || hlID == HLF_TXD)
3103 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003104 col = tv_get_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003105 if (col >= change_start && col <= change_end)
3106 hlID = HLF_TXD; /* changed text */
3107 else
3108 hlID = HLF_CHD; /* changed line */
3109 }
3110 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
3111#endif
3112}
3113
3114/*
3115 * "empty({expr})" function
3116 */
3117 static void
3118f_empty(typval_T *argvars, typval_T *rettv)
3119{
3120 int n = FALSE;
3121
3122 switch (argvars[0].v_type)
3123 {
3124 case VAR_STRING:
3125 case VAR_FUNC:
3126 n = argvars[0].vval.v_string == NULL
3127 || *argvars[0].vval.v_string == NUL;
3128 break;
3129 case VAR_PARTIAL:
3130 n = FALSE;
3131 break;
3132 case VAR_NUMBER:
3133 n = argvars[0].vval.v_number == 0;
3134 break;
3135 case VAR_FLOAT:
3136#ifdef FEAT_FLOAT
3137 n = argvars[0].vval.v_float == 0.0;
3138 break;
3139#endif
3140 case VAR_LIST:
3141 n = argvars[0].vval.v_list == NULL
3142 || argvars[0].vval.v_list->lv_first == NULL;
3143 break;
3144 case VAR_DICT:
3145 n = argvars[0].vval.v_dict == NULL
3146 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
3147 break;
3148 case VAR_SPECIAL:
3149 n = argvars[0].vval.v_number != VVAL_TRUE;
3150 break;
3151
3152 case VAR_JOB:
3153#ifdef FEAT_JOB_CHANNEL
3154 n = argvars[0].vval.v_job == NULL
3155 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
3156 break;
3157#endif
3158 case VAR_CHANNEL:
3159#ifdef FEAT_JOB_CHANNEL
3160 n = argvars[0].vval.v_channel == NULL
3161 || !channel_is_open(argvars[0].vval.v_channel);
3162 break;
3163#endif
3164 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01003165 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003166 n = TRUE;
3167 break;
3168 }
3169
3170 rettv->vval.v_number = n;
3171}
3172
3173/*
3174 * "escape({string}, {chars})" function
3175 */
3176 static void
3177f_escape(typval_T *argvars, typval_T *rettv)
3178{
3179 char_u buf[NUMBUFLEN];
3180
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003181 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
3182 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003183 rettv->v_type = VAR_STRING;
3184}
3185
3186/*
3187 * "eval()" function
3188 */
3189 static void
3190f_eval(typval_T *argvars, typval_T *rettv)
3191{
3192 char_u *s, *p;
3193
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003194 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003195 if (s != NULL)
3196 s = skipwhite(s);
3197
3198 p = s;
3199 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3200 {
3201 if (p != NULL && !aborting())
3202 EMSG2(_(e_invexpr2), p);
3203 need_clr_eos = FALSE;
3204 rettv->v_type = VAR_NUMBER;
3205 rettv->vval.v_number = 0;
3206 }
3207 else if (*s != NUL)
3208 EMSG(_(e_trailing));
3209}
3210
3211/*
3212 * "eventhandler()" function
3213 */
3214 static void
3215f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3216{
3217 rettv->vval.v_number = vgetc_busy;
3218}
3219
3220/*
3221 * "executable()" function
3222 */
3223 static void
3224f_executable(typval_T *argvars, typval_T *rettv)
3225{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003226 char_u *name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003227
3228 /* Check in $PATH and also check directly if there is a directory name. */
3229 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
3230 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
3231}
3232
3233static garray_T redir_execute_ga;
3234
3235/*
3236 * Append "value[value_len]" to the execute() output.
3237 */
3238 void
3239execute_redir_str(char_u *value, int value_len)
3240{
3241 int len;
3242
3243 if (value_len == -1)
3244 len = (int)STRLEN(value); /* Append the entire string */
3245 else
3246 len = value_len; /* Append only "value_len" characters */
3247 if (ga_grow(&redir_execute_ga, len) == OK)
3248 {
3249 mch_memmove((char *)redir_execute_ga.ga_data
3250 + redir_execute_ga.ga_len, value, len);
3251 redir_execute_ga.ga_len += len;
3252 }
3253}
3254
3255/*
3256 * Get next line from a list.
3257 * Called by do_cmdline() to get the next line.
3258 * Returns allocated string, or NULL for end of function.
3259 */
3260
3261 static char_u *
3262get_list_line(
3263 int c UNUSED,
3264 void *cookie,
3265 int indent UNUSED)
3266{
3267 listitem_T **p = (listitem_T **)cookie;
3268 listitem_T *item = *p;
3269 char_u buf[NUMBUFLEN];
3270 char_u *s;
3271
3272 if (item == NULL)
3273 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003274 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003275 *p = item->li_next;
3276 return s == NULL ? NULL : vim_strsave(s);
3277}
3278
3279/*
3280 * "execute()" function
3281 */
3282 static void
3283f_execute(typval_T *argvars, typval_T *rettv)
3284{
3285 char_u *cmd = NULL;
3286 list_T *list = NULL;
3287 int save_msg_silent = msg_silent;
3288 int save_emsg_silent = emsg_silent;
3289 int save_emsg_noredir = emsg_noredir;
3290 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003291 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003292 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003293 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003294 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003295
3296 rettv->vval.v_string = NULL;
3297 rettv->v_type = VAR_STRING;
3298
3299 if (argvars[0].v_type == VAR_LIST)
3300 {
3301 list = argvars[0].vval.v_list;
3302 if (list == NULL || list->lv_first == NULL)
3303 /* empty list, no commands, empty output */
3304 return;
3305 ++list->lv_refcount;
3306 }
3307 else
3308 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003309 cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003310 if (cmd == NULL)
3311 return;
3312 }
3313
3314 if (argvars[1].v_type != VAR_UNKNOWN)
3315 {
3316 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003317 char_u *s = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003318
3319 if (s == NULL)
3320 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003321 if (*s == NUL)
3322 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003323 if (STRNCMP(s, "silent", 6) == 0)
3324 ++msg_silent;
3325 if (STRCMP(s, "silent!") == 0)
3326 {
3327 emsg_silent = TRUE;
3328 emsg_noredir = TRUE;
3329 }
3330 }
3331 else
3332 ++msg_silent;
3333
3334 if (redir_execute)
3335 save_ga = redir_execute_ga;
3336 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3337 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003338 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003339 if (!echo_output)
3340 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003341
3342 if (cmd != NULL)
3343 do_cmdline_cmd(cmd);
3344 else
3345 {
3346 listitem_T *item = list->lv_first;
3347
3348 do_cmdline(NULL, get_list_line, (void *)&item,
3349 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3350 --list->lv_refcount;
3351 }
3352
Bram Moolenaard297f352017-01-29 20:31:21 +01003353 /* Need to append a NUL to the result. */
3354 if (ga_grow(&redir_execute_ga, 1) == OK)
3355 {
3356 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3357 rettv->vval.v_string = redir_execute_ga.ga_data;
3358 }
3359 else
3360 {
3361 ga_clear(&redir_execute_ga);
3362 rettv->vval.v_string = NULL;
3363 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003364 msg_silent = save_msg_silent;
3365 emsg_silent = save_emsg_silent;
3366 emsg_noredir = save_emsg_noredir;
3367
3368 redir_execute = save_redir_execute;
3369 if (redir_execute)
3370 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003371 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003372
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003373 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003374 if (echo_output)
3375 // When not working silently: put it in column zero. A following
3376 // "echon" will overwrite the message, unavoidably.
3377 msg_col = 0;
3378 else
3379 // When working silently: Put it back where it was, since nothing
3380 // should have been written.
3381 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003382}
3383
3384/*
3385 * "exepath()" function
3386 */
3387 static void
3388f_exepath(typval_T *argvars, typval_T *rettv)
3389{
3390 char_u *p = NULL;
3391
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003392 (void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003393 rettv->v_type = VAR_STRING;
3394 rettv->vval.v_string = p;
3395}
3396
3397/*
3398 * "exists()" function
3399 */
3400 static void
3401f_exists(typval_T *argvars, typval_T *rettv)
3402{
3403 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003404 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003405
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003406 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003407 if (*p == '$') /* environment variable */
3408 {
3409 /* first try "normal" environment variables (fast) */
3410 if (mch_getenv(p + 1) != NULL)
3411 n = TRUE;
3412 else
3413 {
3414 /* try expanding things like $VIM and ${HOME} */
3415 p = expand_env_save(p);
3416 if (p != NULL && *p != '$')
3417 n = TRUE;
3418 vim_free(p);
3419 }
3420 }
3421 else if (*p == '&' || *p == '+') /* option */
3422 {
3423 n = (get_option_tv(&p, NULL, TRUE) == OK);
3424 if (*skipwhite(p) != NUL)
3425 n = FALSE; /* trailing garbage */
3426 }
3427 else if (*p == '*') /* internal or user defined function */
3428 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003429 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003430 }
3431 else if (*p == ':')
3432 {
3433 n = cmd_exists(p + 1);
3434 }
3435 else if (*p == '#')
3436 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003437 if (p[1] == '#')
3438 n = autocmd_supported(p + 2);
3439 else
3440 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003441 }
3442 else /* internal variable */
3443 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003444 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003445 }
3446
3447 rettv->vval.v_number = n;
3448}
3449
3450#ifdef FEAT_FLOAT
3451/*
3452 * "exp()" function
3453 */
3454 static void
3455f_exp(typval_T *argvars, typval_T *rettv)
3456{
3457 float_T f = 0.0;
3458
3459 rettv->v_type = VAR_FLOAT;
3460 if (get_float_arg(argvars, &f) == OK)
3461 rettv->vval.v_float = exp(f);
3462 else
3463 rettv->vval.v_float = 0.0;
3464}
3465#endif
3466
3467/*
3468 * "expand()" function
3469 */
3470 static void
3471f_expand(typval_T *argvars, typval_T *rettv)
3472{
3473 char_u *s;
3474 int len;
3475 char_u *errormsg;
3476 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3477 expand_T xpc;
3478 int error = FALSE;
3479 char_u *result;
3480
3481 rettv->v_type = VAR_STRING;
3482 if (argvars[1].v_type != VAR_UNKNOWN
3483 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003484 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003485 && !error)
3486 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003487 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003488 }
3489
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003490 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003491 if (*s == '%' || *s == '#' || *s == '<')
3492 {
3493 ++emsg_off;
3494 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3495 --emsg_off;
3496 if (rettv->v_type == VAR_LIST)
3497 {
3498 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3499 list_append_string(rettv->vval.v_list, result, -1);
3500 else
3501 vim_free(result);
3502 }
3503 else
3504 rettv->vval.v_string = result;
3505 }
3506 else
3507 {
3508 /* When the optional second argument is non-zero, don't remove matches
3509 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3510 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003511 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003512 options |= WILD_KEEP_ALL;
3513 if (!error)
3514 {
3515 ExpandInit(&xpc);
3516 xpc.xp_context = EXPAND_FILES;
3517 if (p_wic)
3518 options += WILD_ICASE;
3519 if (rettv->v_type == VAR_STRING)
3520 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3521 options, WILD_ALL);
3522 else if (rettv_list_alloc(rettv) != FAIL)
3523 {
3524 int i;
3525
3526 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3527 for (i = 0; i < xpc.xp_numfiles; i++)
3528 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3529 ExpandCleanup(&xpc);
3530 }
3531 }
3532 else
3533 rettv->vval.v_string = NULL;
3534 }
3535}
3536
3537/*
3538 * "extend(list, list [, idx])" function
3539 * "extend(dict, dict [, action])" function
3540 */
3541 static void
3542f_extend(typval_T *argvars, typval_T *rettv)
3543{
3544 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3545
3546 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3547 {
3548 list_T *l1, *l2;
3549 listitem_T *item;
3550 long before;
3551 int error = FALSE;
3552
3553 l1 = argvars[0].vval.v_list;
3554 l2 = argvars[1].vval.v_list;
3555 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
3556 && l2 != NULL)
3557 {
3558 if (argvars[2].v_type != VAR_UNKNOWN)
3559 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003560 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003561 if (error)
3562 return; /* type error; errmsg already given */
3563
3564 if (before == l1->lv_len)
3565 item = NULL;
3566 else
3567 {
3568 item = list_find(l1, before);
3569 if (item == NULL)
3570 {
3571 EMSGN(_(e_listidx), before);
3572 return;
3573 }
3574 }
3575 }
3576 else
3577 item = NULL;
3578 list_extend(l1, l2, item);
3579
3580 copy_tv(&argvars[0], rettv);
3581 }
3582 }
3583 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3584 {
3585 dict_T *d1, *d2;
3586 char_u *action;
3587 int i;
3588
3589 d1 = argvars[0].vval.v_dict;
3590 d2 = argvars[1].vval.v_dict;
3591 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
3592 && d2 != NULL)
3593 {
3594 /* Check the third argument. */
3595 if (argvars[2].v_type != VAR_UNKNOWN)
3596 {
3597 static char *(av[]) = {"keep", "force", "error"};
3598
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003599 action = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003600 if (action == NULL)
3601 return; /* type error; errmsg already given */
3602 for (i = 0; i < 3; ++i)
3603 if (STRCMP(action, av[i]) == 0)
3604 break;
3605 if (i == 3)
3606 {
3607 EMSG2(_(e_invarg2), action);
3608 return;
3609 }
3610 }
3611 else
3612 action = (char_u *)"force";
3613
3614 dict_extend(d1, d2, action);
3615
3616 copy_tv(&argvars[0], rettv);
3617 }
3618 }
3619 else
3620 EMSG2(_(e_listdictarg), "extend()");
3621}
3622
3623/*
3624 * "feedkeys()" function
3625 */
3626 static void
3627f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3628{
3629 int remap = TRUE;
3630 int insert = FALSE;
3631 char_u *keys, *flags;
3632 char_u nbuf[NUMBUFLEN];
3633 int typed = FALSE;
3634 int execute = FALSE;
3635 int dangerous = FALSE;
3636 char_u *keys_esc;
3637
3638 /* This is not allowed in the sandbox. If the commands would still be
3639 * executed in the sandbox it would be OK, but it probably happens later,
3640 * when "sandbox" is no longer set. */
3641 if (check_secure())
3642 return;
3643
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003644 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003645
3646 if (argvars[1].v_type != VAR_UNKNOWN)
3647 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003648 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003649 for ( ; *flags != NUL; ++flags)
3650 {
3651 switch (*flags)
3652 {
3653 case 'n': remap = FALSE; break;
3654 case 'm': remap = TRUE; break;
3655 case 't': typed = TRUE; break;
3656 case 'i': insert = TRUE; break;
3657 case 'x': execute = TRUE; break;
3658 case '!': dangerous = TRUE; break;
3659 }
3660 }
3661 }
3662
3663 if (*keys != NUL || execute)
3664 {
3665 /* Need to escape K_SPECIAL and CSI before putting the string in the
3666 * typeahead buffer. */
3667 keys_esc = vim_strsave_escape_csi(keys);
3668 if (keys_esc != NULL)
3669 {
3670 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
3671 insert ? 0 : typebuf.tb_len, !typed, FALSE);
3672 vim_free(keys_esc);
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003673 if (vgetc_busy
3674#ifdef FEAT_TIMERS
3675 || timer_busy
3676#endif
3677 )
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003678 typebuf_was_filled = TRUE;
3679 if (execute)
3680 {
3681 int save_msg_scroll = msg_scroll;
3682
3683 /* Avoid a 1 second delay when the keys start Insert mode. */
3684 msg_scroll = FALSE;
3685
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003686 if (!dangerous)
3687 ++ex_normal_busy;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02003688 exec_normal(TRUE, FALSE, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003689 if (!dangerous)
3690 --ex_normal_busy;
3691
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003692 msg_scroll |= save_msg_scroll;
3693 }
3694 }
3695 }
3696}
3697
3698/*
3699 * "filereadable()" function
3700 */
3701 static void
3702f_filereadable(typval_T *argvars, typval_T *rettv)
3703{
3704 int fd;
3705 char_u *p;
3706 int n;
3707
3708#ifndef O_NONBLOCK
3709# define O_NONBLOCK 0
3710#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003711 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003712 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3713 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3714 {
3715 n = TRUE;
3716 close(fd);
3717 }
3718 else
3719 n = FALSE;
3720
3721 rettv->vval.v_number = n;
3722}
3723
3724/*
3725 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3726 * rights to write into.
3727 */
3728 static void
3729f_filewritable(typval_T *argvars, typval_T *rettv)
3730{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003731 rettv->vval.v_number = filewritable(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003732}
3733
3734 static void
3735findfilendir(
3736 typval_T *argvars UNUSED,
3737 typval_T *rettv,
3738 int find_what UNUSED)
3739{
3740#ifdef FEAT_SEARCHPATH
3741 char_u *fname;
3742 char_u *fresult = NULL;
3743 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3744 char_u *p;
3745 char_u pathbuf[NUMBUFLEN];
3746 int count = 1;
3747 int first = TRUE;
3748 int error = FALSE;
3749#endif
3750
3751 rettv->vval.v_string = NULL;
3752 rettv->v_type = VAR_STRING;
3753
3754#ifdef FEAT_SEARCHPATH
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003755 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003756
3757 if (argvars[1].v_type != VAR_UNKNOWN)
3758 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003759 p = tv_get_string_buf_chk(&argvars[1], pathbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003760 if (p == NULL)
3761 error = TRUE;
3762 else
3763 {
3764 if (*p != NUL)
3765 path = p;
3766
3767 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003768 count = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003769 }
3770 }
3771
3772 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3773 error = TRUE;
3774
3775 if (*fname != NUL && !error)
3776 {
3777 do
3778 {
3779 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3780 vim_free(fresult);
3781 fresult = find_file_in_path_option(first ? fname : NULL,
3782 first ? (int)STRLEN(fname) : 0,
3783 0, first, path,
3784 find_what,
3785 curbuf->b_ffname,
3786 find_what == FINDFILE_DIR
3787 ? (char_u *)"" : curbuf->b_p_sua);
3788 first = FALSE;
3789
3790 if (fresult != NULL && rettv->v_type == VAR_LIST)
3791 list_append_string(rettv->vval.v_list, fresult, -1);
3792
3793 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3794 }
3795
3796 if (rettv->v_type == VAR_STRING)
3797 rettv->vval.v_string = fresult;
3798#endif
3799}
3800
3801/*
3802 * "filter()" function
3803 */
3804 static void
3805f_filter(typval_T *argvars, typval_T *rettv)
3806{
3807 filter_map(argvars, rettv, FALSE);
3808}
3809
3810/*
3811 * "finddir({fname}[, {path}[, {count}]])" function
3812 */
3813 static void
3814f_finddir(typval_T *argvars, typval_T *rettv)
3815{
3816 findfilendir(argvars, rettv, FINDFILE_DIR);
3817}
3818
3819/*
3820 * "findfile({fname}[, {path}[, {count}]])" function
3821 */
3822 static void
3823f_findfile(typval_T *argvars, typval_T *rettv)
3824{
3825 findfilendir(argvars, rettv, FINDFILE_FILE);
3826}
3827
3828#ifdef FEAT_FLOAT
3829/*
3830 * "float2nr({float})" function
3831 */
3832 static void
3833f_float2nr(typval_T *argvars, typval_T *rettv)
3834{
3835 float_T f = 0.0;
3836
3837 if (get_float_arg(argvars, &f) == OK)
3838 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003839 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003840 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003841 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003842 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003843 else
3844 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003845 }
3846}
3847
3848/*
3849 * "floor({float})" function
3850 */
3851 static void
3852f_floor(typval_T *argvars, typval_T *rettv)
3853{
3854 float_T f = 0.0;
3855
3856 rettv->v_type = VAR_FLOAT;
3857 if (get_float_arg(argvars, &f) == OK)
3858 rettv->vval.v_float = floor(f);
3859 else
3860 rettv->vval.v_float = 0.0;
3861}
3862
3863/*
3864 * "fmod()" function
3865 */
3866 static void
3867f_fmod(typval_T *argvars, typval_T *rettv)
3868{
3869 float_T fx = 0.0, fy = 0.0;
3870
3871 rettv->v_type = VAR_FLOAT;
3872 if (get_float_arg(argvars, &fx) == OK
3873 && get_float_arg(&argvars[1], &fy) == OK)
3874 rettv->vval.v_float = fmod(fx, fy);
3875 else
3876 rettv->vval.v_float = 0.0;
3877}
3878#endif
3879
3880/*
3881 * "fnameescape({string})" function
3882 */
3883 static void
3884f_fnameescape(typval_T *argvars, typval_T *rettv)
3885{
3886 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003887 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003888 rettv->v_type = VAR_STRING;
3889}
3890
3891/*
3892 * "fnamemodify({fname}, {mods})" function
3893 */
3894 static void
3895f_fnamemodify(typval_T *argvars, typval_T *rettv)
3896{
3897 char_u *fname;
3898 char_u *mods;
3899 int usedlen = 0;
3900 int len;
3901 char_u *fbuf = NULL;
3902 char_u buf[NUMBUFLEN];
3903
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003904 fname = tv_get_string_chk(&argvars[0]);
3905 mods = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003906 if (fname == NULL || mods == NULL)
3907 fname = NULL;
3908 else
3909 {
3910 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02003911 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003912 }
3913
3914 rettv->v_type = VAR_STRING;
3915 if (fname == NULL)
3916 rettv->vval.v_string = NULL;
3917 else
3918 rettv->vval.v_string = vim_strnsave(fname, len);
3919 vim_free(fbuf);
3920}
3921
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003922/*
3923 * "foldclosed()" function
3924 */
3925 static void
3926foldclosed_both(
3927 typval_T *argvars UNUSED,
3928 typval_T *rettv,
3929 int end UNUSED)
3930{
3931#ifdef FEAT_FOLDING
3932 linenr_T lnum;
3933 linenr_T first, last;
3934
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003935 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003936 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3937 {
3938 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3939 {
3940 if (end)
3941 rettv->vval.v_number = (varnumber_T)last;
3942 else
3943 rettv->vval.v_number = (varnumber_T)first;
3944 return;
3945 }
3946 }
3947#endif
3948 rettv->vval.v_number = -1;
3949}
3950
3951/*
3952 * "foldclosed()" function
3953 */
3954 static void
3955f_foldclosed(typval_T *argvars, typval_T *rettv)
3956{
3957 foldclosed_both(argvars, rettv, FALSE);
3958}
3959
3960/*
3961 * "foldclosedend()" function
3962 */
3963 static void
3964f_foldclosedend(typval_T *argvars, typval_T *rettv)
3965{
3966 foldclosed_both(argvars, rettv, TRUE);
3967}
3968
3969/*
3970 * "foldlevel()" function
3971 */
3972 static void
3973f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3974{
3975#ifdef FEAT_FOLDING
3976 linenr_T lnum;
3977
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003978 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003979 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3980 rettv->vval.v_number = foldLevel(lnum);
3981#endif
3982}
3983
3984/*
3985 * "foldtext()" function
3986 */
3987 static void
3988f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
3989{
3990#ifdef FEAT_FOLDING
3991 linenr_T foldstart;
3992 linenr_T foldend;
3993 char_u *dashes;
3994 linenr_T lnum;
3995 char_u *s;
3996 char_u *r;
3997 int len;
3998 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003999 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004000#endif
4001
4002 rettv->v_type = VAR_STRING;
4003 rettv->vval.v_string = NULL;
4004#ifdef FEAT_FOLDING
4005 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
4006 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
4007 dashes = get_vim_var_str(VV_FOLDDASHES);
4008 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
4009 && dashes != NULL)
4010 {
4011 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02004012 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004013 if (!linewhite(lnum))
4014 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004015
4016 /* Find interesting text in this line. */
4017 s = skipwhite(ml_get(lnum));
4018 /* skip C comment-start */
4019 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
4020 {
4021 s = skipwhite(s + 2);
4022 if (*skipwhite(s) == NUL
4023 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
4024 {
4025 s = skipwhite(ml_get(lnum + 1));
4026 if (*s == '*')
4027 s = skipwhite(s + 1);
4028 }
4029 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02004030 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01004031 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004032 r = alloc((unsigned)(STRLEN(txt)
4033 + STRLEN(dashes) /* for %s */
4034 + 20 /* for %3ld */
4035 + STRLEN(s))); /* concatenated */
4036 if (r != NULL)
4037 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02004038 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004039 len = (int)STRLEN(r);
4040 STRCAT(r, s);
4041 /* remove 'foldmarker' and 'commentstring' */
4042 foldtext_cleanup(r + len);
4043 rettv->vval.v_string = r;
4044 }
4045 }
4046#endif
4047}
4048
4049/*
4050 * "foldtextresult(lnum)" function
4051 */
4052 static void
4053f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
4054{
4055#ifdef FEAT_FOLDING
4056 linenr_T lnum;
4057 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004058 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004059 foldinfo_T foldinfo;
4060 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004061 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004062#endif
4063
4064 rettv->v_type = VAR_STRING;
4065 rettv->vval.v_string = NULL;
4066#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004067 if (entered)
4068 return; /* reject recursive use */
4069 entered = TRUE;
4070
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004071 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004072 /* treat illegal types and illegal string values for {lnum} the same */
4073 if (lnum < 0)
4074 lnum = 0;
4075 fold_count = foldedCount(curwin, lnum, &foldinfo);
4076 if (fold_count > 0)
4077 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01004078 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
4079 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004080 if (text == buf)
4081 text = vim_strsave(text);
4082 rettv->vval.v_string = text;
4083 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004084
4085 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004086#endif
4087}
4088
4089/*
4090 * "foreground()" function
4091 */
4092 static void
4093f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4094{
4095#ifdef FEAT_GUI
4096 if (gui.in_use)
4097 gui_mch_set_foreground();
4098#else
4099# ifdef WIN32
4100 win32_set_foreground();
4101# endif
4102#endif
4103}
4104
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004105 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004106common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004107{
4108 char_u *s;
4109 char_u *name;
4110 int use_string = FALSE;
4111 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004112 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004113
4114 if (argvars[0].v_type == VAR_FUNC)
4115 {
4116 /* function(MyFunc, [arg], dict) */
4117 s = argvars[0].vval.v_string;
4118 }
4119 else if (argvars[0].v_type == VAR_PARTIAL
4120 && argvars[0].vval.v_partial != NULL)
4121 {
4122 /* function(dict.MyFunc, [arg]) */
4123 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004124 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004125 }
4126 else
4127 {
4128 /* function('MyFunc', [arg], dict) */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004129 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004130 use_string = TRUE;
4131 }
4132
Bram Moolenaar843b8842016-08-21 14:36:15 +02004133 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004134 {
4135 name = s;
4136 trans_name = trans_function_name(&name, FALSE,
4137 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4138 if (*name != NUL)
4139 s = NULL;
4140 }
4141
Bram Moolenaar843b8842016-08-21 14:36:15 +02004142 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4143 || (is_funcref && trans_name == NULL))
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004144 EMSG2(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004145 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004146 else if (trans_name != NULL && (is_funcref
4147 ? find_func(trans_name) == NULL
4148 : !translated_function_exists(trans_name)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004149 EMSG2(_("E700: Unknown function: %s"), s);
4150 else
4151 {
4152 int dict_idx = 0;
4153 int arg_idx = 0;
4154 list_T *list = NULL;
4155
4156 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4157 {
4158 char sid_buf[25];
4159 int off = *s == 's' ? 2 : 5;
4160
4161 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4162 * also be called from another script. Using trans_function_name()
4163 * would also work, but some plugins depend on the name being
4164 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004165 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004166 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
4167 if (name != NULL)
4168 {
4169 STRCPY(name, sid_buf);
4170 STRCAT(name, s + off);
4171 }
4172 }
4173 else
4174 name = vim_strsave(s);
4175
4176 if (argvars[1].v_type != VAR_UNKNOWN)
4177 {
4178 if (argvars[2].v_type != VAR_UNKNOWN)
4179 {
4180 /* function(name, [args], dict) */
4181 arg_idx = 1;
4182 dict_idx = 2;
4183 }
4184 else if (argvars[1].v_type == VAR_DICT)
4185 /* function(name, dict) */
4186 dict_idx = 1;
4187 else
4188 /* function(name, [args]) */
4189 arg_idx = 1;
4190 if (dict_idx > 0)
4191 {
4192 if (argvars[dict_idx].v_type != VAR_DICT)
4193 {
4194 EMSG(_("E922: expected a dict"));
4195 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004196 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004197 }
4198 if (argvars[dict_idx].vval.v_dict == NULL)
4199 dict_idx = 0;
4200 }
4201 if (arg_idx > 0)
4202 {
4203 if (argvars[arg_idx].v_type != VAR_LIST)
4204 {
4205 EMSG(_("E923: Second argument of function() must be a list or a dict"));
4206 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004207 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004208 }
4209 list = argvars[arg_idx].vval.v_list;
4210 if (list == NULL || list->lv_len == 0)
4211 arg_idx = 0;
4212 }
4213 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004214 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004215 {
4216 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
4217
4218 /* result is a VAR_PARTIAL */
4219 if (pt == NULL)
4220 vim_free(name);
4221 else
4222 {
4223 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4224 {
4225 listitem_T *li;
4226 int i = 0;
4227 int arg_len = 0;
4228 int lv_len = 0;
4229
4230 if (arg_pt != NULL)
4231 arg_len = arg_pt->pt_argc;
4232 if (list != NULL)
4233 lv_len = list->lv_len;
4234 pt->pt_argc = arg_len + lv_len;
4235 pt->pt_argv = (typval_T *)alloc(
4236 sizeof(typval_T) * pt->pt_argc);
4237 if (pt->pt_argv == NULL)
4238 {
4239 vim_free(pt);
4240 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004241 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004242 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004243 for (i = 0; i < arg_len; i++)
4244 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4245 if (lv_len > 0)
4246 for (li = list->lv_first; li != NULL;
4247 li = li->li_next)
4248 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004249 }
4250
4251 /* For "function(dict.func, [], dict)" and "func" is a partial
4252 * use "dict". That is backwards compatible. */
4253 if (dict_idx > 0)
4254 {
4255 /* The dict is bound explicitly, pt_auto is FALSE. */
4256 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4257 ++pt->pt_dict->dv_refcount;
4258 }
4259 else if (arg_pt != NULL)
4260 {
4261 /* If the dict was bound automatically the result is also
4262 * bound automatically. */
4263 pt->pt_dict = arg_pt->pt_dict;
4264 pt->pt_auto = arg_pt->pt_auto;
4265 if (pt->pt_dict != NULL)
4266 ++pt->pt_dict->dv_refcount;
4267 }
4268
4269 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004270 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4271 {
4272 pt->pt_func = arg_pt->pt_func;
4273 func_ptr_ref(pt->pt_func);
4274 vim_free(name);
4275 }
4276 else if (is_funcref)
4277 {
4278 pt->pt_func = find_func(trans_name);
4279 func_ptr_ref(pt->pt_func);
4280 vim_free(name);
4281 }
4282 else
4283 {
4284 pt->pt_name = name;
4285 func_ref(name);
4286 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004287 }
4288 rettv->v_type = VAR_PARTIAL;
4289 rettv->vval.v_partial = pt;
4290 }
4291 else
4292 {
4293 /* result is a VAR_FUNC */
4294 rettv->v_type = VAR_FUNC;
4295 rettv->vval.v_string = name;
4296 func_ref(name);
4297 }
4298 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004299theend:
4300 vim_free(trans_name);
4301}
4302
4303/*
4304 * "funcref()" function
4305 */
4306 static void
4307f_funcref(typval_T *argvars, typval_T *rettv)
4308{
4309 common_function(argvars, rettv, TRUE);
4310}
4311
4312/*
4313 * "function()" function
4314 */
4315 static void
4316f_function(typval_T *argvars, typval_T *rettv)
4317{
4318 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004319}
4320
4321/*
4322 * "garbagecollect()" function
4323 */
4324 static void
4325f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4326{
4327 /* This is postponed until we are back at the toplevel, because we may be
4328 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4329 want_garbage_collect = TRUE;
4330
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004331 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004332 garbage_collect_at_exit = TRUE;
4333}
4334
4335/*
4336 * "get()" function
4337 */
4338 static void
4339f_get(typval_T *argvars, typval_T *rettv)
4340{
4341 listitem_T *li;
4342 list_T *l;
4343 dictitem_T *di;
4344 dict_T *d;
4345 typval_T *tv = NULL;
4346
4347 if (argvars[0].v_type == VAR_LIST)
4348 {
4349 if ((l = argvars[0].vval.v_list) != NULL)
4350 {
4351 int error = FALSE;
4352
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004353 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004354 if (!error && li != NULL)
4355 tv = &li->li_tv;
4356 }
4357 }
4358 else if (argvars[0].v_type == VAR_DICT)
4359 {
4360 if ((d = argvars[0].vval.v_dict) != NULL)
4361 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004362 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004363 if (di != NULL)
4364 tv = &di->di_tv;
4365 }
4366 }
4367 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4368 {
4369 partial_T *pt;
4370 partial_T fref_pt;
4371
4372 if (argvars[0].v_type == VAR_PARTIAL)
4373 pt = argvars[0].vval.v_partial;
4374 else
4375 {
4376 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4377 fref_pt.pt_name = argvars[0].vval.v_string;
4378 pt = &fref_pt;
4379 }
4380
4381 if (pt != NULL)
4382 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004383 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004384 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004385
4386 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4387 {
4388 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004389 n = partial_name(pt);
4390 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004391 rettv->vval.v_string = NULL;
4392 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004393 {
4394 rettv->vval.v_string = vim_strsave(n);
4395 if (rettv->v_type == VAR_FUNC)
4396 func_ref(rettv->vval.v_string);
4397 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004398 }
4399 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004400 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004401 else if (STRCMP(what, "args") == 0)
4402 {
4403 rettv->v_type = VAR_LIST;
4404 if (rettv_list_alloc(rettv) == OK)
4405 {
4406 int i;
4407
4408 for (i = 0; i < pt->pt_argc; ++i)
4409 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4410 }
4411 }
4412 else
4413 EMSG2(_(e_invarg2), what);
4414 return;
4415 }
4416 }
4417 else
4418 EMSG2(_(e_listdictarg), "get()");
4419
4420 if (tv == NULL)
4421 {
4422 if (argvars[2].v_type != VAR_UNKNOWN)
4423 copy_tv(&argvars[2], rettv);
4424 }
4425 else
4426 copy_tv(tv, rettv);
4427}
4428
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004429/*
4430 * Returns buffer options, variables and other attributes in a dictionary.
4431 */
4432 static dict_T *
4433get_buffer_info(buf_T *buf)
4434{
4435 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004436 tabpage_T *tp;
4437 win_T *wp;
4438 list_T *windows;
4439
4440 dict = dict_alloc();
4441 if (dict == NULL)
4442 return NULL;
4443
Bram Moolenaare0be1672018-07-08 16:50:37 +02004444 dict_add_number(dict, "bufnr", buf->b_fnum);
4445 dict_add_string(dict, "name", buf->b_ffname);
4446 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4447 : buflist_findlnum(buf));
4448 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4449 dict_add_number(dict, "listed", buf->b_p_bl);
4450 dict_add_number(dict, "changed", bufIsChanged(buf));
4451 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4452 dict_add_number(dict, "hidden",
4453 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004454
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004455 /* Get a reference to buffer variables */
4456 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004457
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004458 /* List of windows displaying this buffer */
4459 windows = list_alloc();
4460 if (windows != NULL)
4461 {
4462 FOR_ALL_TAB_WINDOWS(tp, wp)
4463 if (wp->w_buffer == buf)
4464 list_append_number(windows, (varnumber_T)wp->w_id);
4465 dict_add_list(dict, "windows", windows);
4466 }
4467
4468#ifdef FEAT_SIGNS
4469 if (buf->b_signlist != NULL)
4470 {
4471 /* List of signs placed in this buffer */
4472 list_T *signs = list_alloc();
4473 if (signs != NULL)
4474 {
4475 get_buffer_signs(buf, signs);
4476 dict_add_list(dict, "signs", signs);
4477 }
4478 }
4479#endif
4480
4481 return dict;
4482}
4483
4484/*
4485 * "getbufinfo()" function
4486 */
4487 static void
4488f_getbufinfo(typval_T *argvars, typval_T *rettv)
4489{
4490 buf_T *buf = NULL;
4491 buf_T *argbuf = NULL;
4492 dict_T *d;
4493 int filtered = FALSE;
4494 int sel_buflisted = FALSE;
4495 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004496 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004497
4498 if (rettv_list_alloc(rettv) != OK)
4499 return;
4500
4501 /* List of all the buffers or selected buffers */
4502 if (argvars[0].v_type == VAR_DICT)
4503 {
4504 dict_T *sel_d = argvars[0].vval.v_dict;
4505
4506 if (sel_d != NULL)
4507 {
4508 dictitem_T *di;
4509
4510 filtered = TRUE;
4511
4512 di = dict_find(sel_d, (char_u *)"buflisted", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004513 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004514 sel_buflisted = TRUE;
4515
4516 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004517 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004518 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004519
4520 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004521 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004522 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004523 }
4524 }
4525 else if (argvars[0].v_type != VAR_UNKNOWN)
4526 {
4527 /* Information about one buffer. Argument specifies the buffer */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004528 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004529 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004530 argbuf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004531 --emsg_off;
4532 if (argbuf == NULL)
4533 return;
4534 }
4535
4536 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004537 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004538 {
4539 if (argbuf != NULL && argbuf != buf)
4540 continue;
4541 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004542 || (sel_buflisted && !buf->b_p_bl)
4543 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004544 continue;
4545
4546 d = get_buffer_info(buf);
4547 if (d != NULL)
4548 list_append_dict(rettv->vval.v_list, d);
4549 if (argbuf != NULL)
4550 return;
4551 }
4552}
4553
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004554/*
4555 * Get line or list of lines from buffer "buf" into "rettv".
4556 * Return a range (from start to end) of lines in rettv from the specified
4557 * buffer.
4558 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4559 */
4560 static void
4561get_buffer_lines(
4562 buf_T *buf,
4563 linenr_T start,
4564 linenr_T end,
4565 int retlist,
4566 typval_T *rettv)
4567{
4568 char_u *p;
4569
4570 rettv->v_type = VAR_STRING;
4571 rettv->vval.v_string = NULL;
4572 if (retlist && rettv_list_alloc(rettv) == FAIL)
4573 return;
4574
4575 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4576 return;
4577
4578 if (!retlist)
4579 {
4580 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4581 p = ml_get_buf(buf, start, FALSE);
4582 else
4583 p = (char_u *)"";
4584 rettv->vval.v_string = vim_strsave(p);
4585 }
4586 else
4587 {
4588 if (end < start)
4589 return;
4590
4591 if (start < 1)
4592 start = 1;
4593 if (end > buf->b_ml.ml_line_count)
4594 end = buf->b_ml.ml_line_count;
4595 while (start <= end)
4596 if (list_append_string(rettv->vval.v_list,
4597 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4598 break;
4599 }
4600}
4601
4602/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004603 * "getbufline()" function
4604 */
4605 static void
4606f_getbufline(typval_T *argvars, typval_T *rettv)
4607{
4608 linenr_T lnum;
4609 linenr_T end;
4610 buf_T *buf;
4611
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004612 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004613 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004614 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004615 --emsg_off;
4616
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004617 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004618 if (argvars[2].v_type == VAR_UNKNOWN)
4619 end = lnum;
4620 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004621 end = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004622
4623 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4624}
4625
4626/*
4627 * "getbufvar()" function
4628 */
4629 static void
4630f_getbufvar(typval_T *argvars, typval_T *rettv)
4631{
4632 buf_T *buf;
4633 buf_T *save_curbuf;
4634 char_u *varname;
4635 dictitem_T *v;
4636 int done = FALSE;
4637
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004638 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
4639 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004640 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004641 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004642
4643 rettv->v_type = VAR_STRING;
4644 rettv->vval.v_string = NULL;
4645
4646 if (buf != NULL && varname != NULL)
4647 {
4648 /* set curbuf to be our buf, temporarily */
4649 save_curbuf = curbuf;
4650 curbuf = buf;
4651
Bram Moolenaar30567352016-08-27 21:25:44 +02004652 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004653 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004654 if (varname[1] == NUL)
4655 {
4656 /* get all buffer-local options in a dict */
4657 dict_T *opts = get_winbuf_options(TRUE);
4658
4659 if (opts != NULL)
4660 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004661 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004662 done = TRUE;
4663 }
4664 }
4665 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4666 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004667 done = TRUE;
4668 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004669 else
4670 {
4671 /* Look up the variable. */
4672 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4673 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4674 'b', varname, FALSE);
4675 if (v != NULL)
4676 {
4677 copy_tv(&v->di_tv, rettv);
4678 done = TRUE;
4679 }
4680 }
4681
4682 /* restore previous notion of curbuf */
4683 curbuf = save_curbuf;
4684 }
4685
4686 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4687 /* use the default value */
4688 copy_tv(&argvars[2], rettv);
4689
4690 --emsg_off;
4691}
4692
4693/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004694 * "getchangelist()" function
4695 */
4696 static void
4697f_getchangelist(typval_T *argvars, typval_T *rettv)
4698{
4699#ifdef FEAT_JUMPLIST
4700 buf_T *buf;
4701 int i;
4702 list_T *l;
4703 dict_T *d;
4704#endif
4705
4706 if (rettv_list_alloc(rettv) != OK)
4707 return;
4708
4709#ifdef FEAT_JUMPLIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004710 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004711 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004712 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004713 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004714 if (buf == NULL)
4715 return;
4716
4717 l = list_alloc();
4718 if (l == NULL)
4719 return;
4720
4721 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4722 return;
4723 /*
4724 * The current window change list index tracks only the position in the
4725 * current buffer change list. For other buffers, use the change list
4726 * length as the current index.
4727 */
4728 list_append_number(rettv->vval.v_list,
4729 (varnumber_T)((buf == curwin->w_buffer)
4730 ? curwin->w_changelistidx : buf->b_changelistlen));
4731
4732 for (i = 0; i < buf->b_changelistlen; ++i)
4733 {
4734 if (buf->b_changelist[i].lnum == 0)
4735 continue;
4736 if ((d = dict_alloc()) == NULL)
4737 return;
4738 if (list_append_dict(l, d) == FAIL)
4739 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004740 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4741 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004742# ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +02004743 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004744# endif
4745 }
4746#endif
4747}
4748/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004749 * "getchar()" function
4750 */
4751 static void
4752f_getchar(typval_T *argvars, typval_T *rettv)
4753{
4754 varnumber_T n;
4755 int error = FALSE;
4756
Bram Moolenaar84d93902018-09-11 20:10:20 +02004757#ifdef MESSAGE_QUEUE
4758 // vpeekc() used to check for messages, but that caused problems, invoking
4759 // a callback where it was not expected. Some plugins use getchar(1) in a
4760 // loop to await a message, therefore make sure we check for messages here.
4761 parse_queued_messages();
4762#endif
4763
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004764 /* Position the cursor. Needed after a message that ends in a space. */
4765 windgoto(msg_row, msg_col);
4766
4767 ++no_mapping;
4768 ++allow_keys;
4769 for (;;)
4770 {
4771 if (argvars[0].v_type == VAR_UNKNOWN)
4772 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004773 n = plain_vgetc();
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004774 else if (tv_get_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004775 /* getchar(1): only check if char avail */
4776 n = vpeekc_any();
4777 else if (error || vpeekc_any() == NUL)
4778 /* illegal argument or getchar(0) and no char avail: return zero */
4779 n = 0;
4780 else
4781 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004782 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004783
4784 if (n == K_IGNORE)
4785 continue;
4786 break;
4787 }
4788 --no_mapping;
4789 --allow_keys;
4790
4791 set_vim_var_nr(VV_MOUSE_WIN, 0);
4792 set_vim_var_nr(VV_MOUSE_WINID, 0);
4793 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4794 set_vim_var_nr(VV_MOUSE_COL, 0);
4795
4796 rettv->vval.v_number = n;
4797 if (IS_SPECIAL(n) || mod_mask != 0)
4798 {
4799 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4800 int i = 0;
4801
4802 /* Turn a special key into three bytes, plus modifier. */
4803 if (mod_mask != 0)
4804 {
4805 temp[i++] = K_SPECIAL;
4806 temp[i++] = KS_MODIFIER;
4807 temp[i++] = mod_mask;
4808 }
4809 if (IS_SPECIAL(n))
4810 {
4811 temp[i++] = K_SPECIAL;
4812 temp[i++] = K_SECOND(n);
4813 temp[i++] = K_THIRD(n);
4814 }
4815#ifdef FEAT_MBYTE
4816 else if (has_mbyte)
4817 i += (*mb_char2bytes)(n, temp + i);
4818#endif
4819 else
4820 temp[i++] = n;
4821 temp[i++] = NUL;
4822 rettv->v_type = VAR_STRING;
4823 rettv->vval.v_string = vim_strsave(temp);
4824
4825#ifdef FEAT_MOUSE
4826 if (is_mouse_key(n))
4827 {
4828 int row = mouse_row;
4829 int col = mouse_col;
4830 win_T *win;
4831 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004832 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004833 int winnr = 1;
4834
4835 if (row >= 0 && col >= 0)
4836 {
4837 /* Find the window at the mouse coordinates and compute the
4838 * text position. */
4839 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004840 if (win == NULL)
4841 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004842 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004843 for (wp = firstwin; wp != win; wp = wp->w_next)
4844 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004845 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4846 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4847 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4848 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4849 }
4850 }
4851#endif
4852 }
4853}
4854
4855/*
4856 * "getcharmod()" function
4857 */
4858 static void
4859f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4860{
4861 rettv->vval.v_number = mod_mask;
4862}
4863
4864/*
4865 * "getcharsearch()" function
4866 */
4867 static void
4868f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4869{
4870 if (rettv_dict_alloc(rettv) != FAIL)
4871 {
4872 dict_T *dict = rettv->vval.v_dict;
4873
Bram Moolenaare0be1672018-07-08 16:50:37 +02004874 dict_add_string(dict, "char", last_csearch());
4875 dict_add_number(dict, "forward", last_csearch_forward());
4876 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004877 }
4878}
4879
4880/*
4881 * "getcmdline()" function
4882 */
4883 static void
4884f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4885{
4886 rettv->v_type = VAR_STRING;
4887 rettv->vval.v_string = get_cmdline_str();
4888}
4889
4890/*
4891 * "getcmdpos()" function
4892 */
4893 static void
4894f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4895{
4896 rettv->vval.v_number = get_cmdline_pos() + 1;
4897}
4898
4899/*
4900 * "getcmdtype()" function
4901 */
4902 static void
4903f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4904{
4905 rettv->v_type = VAR_STRING;
4906 rettv->vval.v_string = alloc(2);
4907 if (rettv->vval.v_string != NULL)
4908 {
4909 rettv->vval.v_string[0] = get_cmdline_type();
4910 rettv->vval.v_string[1] = NUL;
4911 }
4912}
4913
4914/*
4915 * "getcmdwintype()" function
4916 */
4917 static void
4918f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4919{
4920 rettv->v_type = VAR_STRING;
4921 rettv->vval.v_string = NULL;
4922#ifdef FEAT_CMDWIN
4923 rettv->vval.v_string = alloc(2);
4924 if (rettv->vval.v_string != NULL)
4925 {
4926 rettv->vval.v_string[0] = cmdwin_type;
4927 rettv->vval.v_string[1] = NUL;
4928 }
4929#endif
4930}
4931
4932#if defined(FEAT_CMDL_COMPL)
4933/*
4934 * "getcompletion()" function
4935 */
4936 static void
4937f_getcompletion(typval_T *argvars, typval_T *rettv)
4938{
4939 char_u *pat;
4940 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004941 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004942 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
4943 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004944
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004945 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004946 filtered = tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004947
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004948 if (p_wic)
4949 options |= WILD_ICASE;
4950
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004951 /* For filtered results, 'wildignore' is used */
4952 if (!filtered)
4953 options |= WILD_KEEP_ALL;
4954
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004955 ExpandInit(&xpc);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004956 xpc.xp_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004957 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004958 xpc.xp_context = cmdcomplete_str_to_type(tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004959 if (xpc.xp_context == EXPAND_NOTHING)
4960 {
4961 if (argvars[1].v_type == VAR_STRING)
4962 EMSG2(_(e_invarg2), argvars[1].vval.v_string);
4963 else
4964 EMSG(_(e_invarg));
4965 return;
4966 }
4967
4968# if defined(FEAT_MENU)
4969 if (xpc.xp_context == EXPAND_MENUS)
4970 {
4971 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4972 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4973 }
4974# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02004975#ifdef FEAT_CSCOPE
4976 if (xpc.xp_context == EXPAND_CSCOPE)
4977 {
4978 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4979 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4980 }
4981#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02004982#ifdef FEAT_SIGNS
4983 if (xpc.xp_context == EXPAND_SIGN)
4984 {
4985 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4986 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4987 }
4988#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004989
4990 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4991 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
4992 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004993 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004994
4995 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4996
4997 for (i = 0; i < xpc.xp_numfiles; i++)
4998 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4999 }
5000 vim_free(pat);
5001 ExpandCleanup(&xpc);
5002}
5003#endif
5004
5005/*
5006 * "getcwd()" function
5007 */
5008 static void
5009f_getcwd(typval_T *argvars, typval_T *rettv)
5010{
5011 win_T *wp = NULL;
5012 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01005013 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005014
5015 rettv->v_type = VAR_STRING;
5016 rettv->vval.v_string = NULL;
5017
Bram Moolenaar54591292018-02-09 20:53:59 +01005018 if (argvars[0].v_type == VAR_NUMBER && argvars[0].vval.v_number == -1)
5019 global = TRUE;
5020 else
5021 wp = find_tabwin(&argvars[0], &argvars[1]);
5022
5023 if (wp != NULL && wp->w_localdir != NULL)
5024 rettv->vval.v_string = vim_strsave(wp->w_localdir);
5025 else if (wp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005026 {
Bram Moolenaar54591292018-02-09 20:53:59 +01005027 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005028 rettv->vval.v_string = vim_strsave(globaldir);
5029 else
5030 {
5031 cwd = alloc(MAXPATHL);
5032 if (cwd != NULL)
5033 {
5034 if (mch_dirname(cwd, MAXPATHL) != FAIL)
5035 rettv->vval.v_string = vim_strsave(cwd);
5036 vim_free(cwd);
5037 }
5038 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005039 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02005040#ifdef BACKSLASH_IN_FILENAME
5041 if (rettv->vval.v_string != NULL)
5042 slash_adjust(rettv->vval.v_string);
5043#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005044}
5045
5046/*
5047 * "getfontname()" function
5048 */
5049 static void
5050f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
5051{
5052 rettv->v_type = VAR_STRING;
5053 rettv->vval.v_string = NULL;
5054#ifdef FEAT_GUI
5055 if (gui.in_use)
5056 {
5057 GuiFont font;
5058 char_u *name = NULL;
5059
5060 if (argvars[0].v_type == VAR_UNKNOWN)
5061 {
5062 /* Get the "Normal" font. Either the name saved by
5063 * hl_set_font_name() or from the font ID. */
5064 font = gui.norm_font;
5065 name = hl_get_font_name();
5066 }
5067 else
5068 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005069 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005070 if (STRCMP(name, "*") == 0) /* don't use font dialog */
5071 return;
5072 font = gui_mch_get_font(name, FALSE);
5073 if (font == NOFONT)
5074 return; /* Invalid font name, return empty string. */
5075 }
5076 rettv->vval.v_string = gui_mch_get_fontname(font, name);
5077 if (argvars[0].v_type != VAR_UNKNOWN)
5078 gui_mch_free_font(font);
5079 }
5080#endif
5081}
5082
5083/*
5084 * "getfperm({fname})" function
5085 */
5086 static void
5087f_getfperm(typval_T *argvars, typval_T *rettv)
5088{
5089 char_u *fname;
5090 stat_T st;
5091 char_u *perm = NULL;
5092 char_u flags[] = "rwx";
5093 int i;
5094
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005095 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005096
5097 rettv->v_type = VAR_STRING;
5098 if (mch_stat((char *)fname, &st) >= 0)
5099 {
5100 perm = vim_strsave((char_u *)"---------");
5101 if (perm != NULL)
5102 {
5103 for (i = 0; i < 9; i++)
5104 {
5105 if (st.st_mode & (1 << (8 - i)))
5106 perm[i] = flags[i % 3];
5107 }
5108 }
5109 }
5110 rettv->vval.v_string = perm;
5111}
5112
5113/*
5114 * "getfsize({fname})" function
5115 */
5116 static void
5117f_getfsize(typval_T *argvars, typval_T *rettv)
5118{
5119 char_u *fname;
5120 stat_T st;
5121
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005122 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005123
5124 rettv->v_type = VAR_NUMBER;
5125
5126 if (mch_stat((char *)fname, &st) >= 0)
5127 {
5128 if (mch_isdir(fname))
5129 rettv->vval.v_number = 0;
5130 else
5131 {
5132 rettv->vval.v_number = (varnumber_T)st.st_size;
5133
5134 /* non-perfect check for overflow */
5135 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5136 rettv->vval.v_number = -2;
5137 }
5138 }
5139 else
5140 rettv->vval.v_number = -1;
5141}
5142
5143/*
5144 * "getftime({fname})" function
5145 */
5146 static void
5147f_getftime(typval_T *argvars, typval_T *rettv)
5148{
5149 char_u *fname;
5150 stat_T st;
5151
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005152 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005153
5154 if (mch_stat((char *)fname, &st) >= 0)
5155 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5156 else
5157 rettv->vval.v_number = -1;
5158}
5159
5160/*
5161 * "getftype({fname})" function
5162 */
5163 static void
5164f_getftype(typval_T *argvars, typval_T *rettv)
5165{
5166 char_u *fname;
5167 stat_T st;
5168 char_u *type = NULL;
5169 char *t;
5170
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005171 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005172
5173 rettv->v_type = VAR_STRING;
5174 if (mch_lstat((char *)fname, &st) >= 0)
5175 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005176 if (S_ISREG(st.st_mode))
5177 t = "file";
5178 else if (S_ISDIR(st.st_mode))
5179 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005180 else if (S_ISLNK(st.st_mode))
5181 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005182 else if (S_ISBLK(st.st_mode))
5183 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005184 else if (S_ISCHR(st.st_mode))
5185 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005186 else if (S_ISFIFO(st.st_mode))
5187 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005188 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005189 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005190 else
5191 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005192 type = vim_strsave((char_u *)t);
5193 }
5194 rettv->vval.v_string = type;
5195}
5196
5197/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005198 * "getjumplist()" function
5199 */
5200 static void
5201f_getjumplist(typval_T *argvars, typval_T *rettv)
5202{
5203#ifdef FEAT_JUMPLIST
5204 win_T *wp;
5205 int i;
5206 list_T *l;
5207 dict_T *d;
5208#endif
5209
5210 if (rettv_list_alloc(rettv) != OK)
5211 return;
5212
5213#ifdef FEAT_JUMPLIST
5214 wp = find_tabwin(&argvars[0], &argvars[1]);
5215 if (wp == NULL)
5216 return;
5217
5218 l = list_alloc();
5219 if (l == NULL)
5220 return;
5221
5222 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5223 return;
5224 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5225
Bram Moolenaar48679742018-02-13 13:33:29 +01005226 cleanup_jumplist(wp, TRUE);
5227
Bram Moolenaar4f505882018-02-10 21:06:32 +01005228 for (i = 0; i < wp->w_jumplistlen; ++i)
5229 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005230 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5231 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005232 if ((d = dict_alloc()) == NULL)
5233 return;
5234 if (list_append_dict(l, d) == FAIL)
5235 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005236 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5237 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005238# ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +02005239 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005240# endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005241 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005242 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005243 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005244 }
5245#endif
5246}
5247
5248/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005249 * "getline(lnum, [end])" function
5250 */
5251 static void
5252f_getline(typval_T *argvars, typval_T *rettv)
5253{
5254 linenr_T lnum;
5255 linenr_T end;
5256 int retlist;
5257
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005258 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005259 if (argvars[1].v_type == VAR_UNKNOWN)
5260 {
5261 end = 0;
5262 retlist = FALSE;
5263 }
5264 else
5265 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005266 end = tv_get_lnum(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005267 retlist = TRUE;
5268 }
5269
5270 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5271}
5272
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005273#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005274 static void
5275get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5276{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005277 if (what_arg->v_type == VAR_UNKNOWN)
5278 {
5279 if (rettv_list_alloc(rettv) == OK)
5280 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005281 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005282 }
5283 else
5284 {
5285 if (rettv_dict_alloc(rettv) == OK)
5286 if (is_qf || (wp != NULL))
5287 {
5288 if (what_arg->v_type == VAR_DICT)
5289 {
5290 dict_T *d = what_arg->vval.v_dict;
5291
5292 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005293 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005294 }
5295 else
5296 EMSG(_(e_dictreq));
5297 }
5298 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005299}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005300#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005301
5302/*
5303 * "getloclist()" function
5304 */
5305 static void
5306f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5307{
5308#ifdef FEAT_QUICKFIX
5309 win_T *wp;
5310
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005311 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005312 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5313#endif
5314}
5315
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005316/*
5317 * "getmatches()" function
5318 */
5319 static void
5320f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5321{
5322#ifdef FEAT_SEARCH_EXTRA
5323 dict_T *dict;
5324 matchitem_T *cur = curwin->w_match_head;
5325 int i;
5326
5327 if (rettv_list_alloc(rettv) == OK)
5328 {
5329 while (cur != NULL)
5330 {
5331 dict = dict_alloc();
5332 if (dict == NULL)
5333 return;
5334 if (cur->match.regprog == NULL)
5335 {
5336 /* match added with matchaddpos() */
5337 for (i = 0; i < MAXPOSMATCH; ++i)
5338 {
5339 llpos_T *llpos;
5340 char buf[6];
5341 list_T *l;
5342
5343 llpos = &cur->pos.pos[i];
5344 if (llpos->lnum == 0)
5345 break;
5346 l = list_alloc();
5347 if (l == NULL)
5348 break;
5349 list_append_number(l, (varnumber_T)llpos->lnum);
5350 if (llpos->col > 0)
5351 {
5352 list_append_number(l, (varnumber_T)llpos->col);
5353 list_append_number(l, (varnumber_T)llpos->len);
5354 }
5355 sprintf(buf, "pos%d", i + 1);
5356 dict_add_list(dict, buf, l);
5357 }
5358 }
5359 else
5360 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02005361 dict_add_string(dict, "pattern", cur->pattern);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005362 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02005363 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
5364 dict_add_number(dict, "priority", (long)cur->priority);
5365 dict_add_number(dict, "id", (long)cur->id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005366# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
5367 if (cur->conceal_char)
5368 {
5369 char_u buf[MB_MAXBYTES + 1];
5370
5371 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005372 dict_add_string(dict, "conceal", (char_u *)&buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005373 }
5374# endif
5375 list_append_dict(rettv->vval.v_list, dict);
5376 cur = cur->next;
5377 }
5378 }
5379#endif
5380}
5381
5382/*
5383 * "getpid()" function
5384 */
5385 static void
5386f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5387{
5388 rettv->vval.v_number = mch_get_pid();
5389}
5390
5391 static void
5392getpos_both(
5393 typval_T *argvars,
5394 typval_T *rettv,
5395 int getcurpos)
5396{
5397 pos_T *fp;
5398 list_T *l;
5399 int fnum = -1;
5400
5401 if (rettv_list_alloc(rettv) == OK)
5402 {
5403 l = rettv->vval.v_list;
5404 if (getcurpos)
5405 fp = &curwin->w_cursor;
5406 else
5407 fp = var2fpos(&argvars[0], TRUE, &fnum);
5408 if (fnum != -1)
5409 list_append_number(l, (varnumber_T)fnum);
5410 else
5411 list_append_number(l, (varnumber_T)0);
5412 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5413 : (varnumber_T)0);
5414 list_append_number(l, (fp != NULL)
5415 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5416 : (varnumber_T)0);
5417 list_append_number(l,
5418#ifdef FEAT_VIRTUALEDIT
5419 (fp != NULL) ? (varnumber_T)fp->coladd :
5420#endif
5421 (varnumber_T)0);
5422 if (getcurpos)
5423 {
5424 update_curswant();
5425 list_append_number(l, curwin->w_curswant == MAXCOL ?
5426 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
5427 }
5428 }
5429 else
5430 rettv->vval.v_number = FALSE;
5431}
5432
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005433/*
5434 * "getcurpos()" function
5435 */
5436 static void
5437f_getcurpos(typval_T *argvars, typval_T *rettv)
5438{
5439 getpos_both(argvars, rettv, TRUE);
5440}
5441
5442/*
5443 * "getpos(string)" function
5444 */
5445 static void
5446f_getpos(typval_T *argvars, typval_T *rettv)
5447{
5448 getpos_both(argvars, rettv, FALSE);
5449}
5450
5451/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005452 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005453 */
5454 static void
5455f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5456{
5457#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005458 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005459#endif
5460}
5461
5462/*
5463 * "getreg()" function
5464 */
5465 static void
5466f_getreg(typval_T *argvars, typval_T *rettv)
5467{
5468 char_u *strregname;
5469 int regname;
5470 int arg2 = FALSE;
5471 int return_list = FALSE;
5472 int error = FALSE;
5473
5474 if (argvars[0].v_type != VAR_UNKNOWN)
5475 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005476 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005477 error = strregname == NULL;
5478 if (argvars[1].v_type != VAR_UNKNOWN)
5479 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005480 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005481 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005482 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005483 }
5484 }
5485 else
5486 strregname = get_vim_var_str(VV_REG);
5487
5488 if (error)
5489 return;
5490
5491 regname = (strregname == NULL ? '"' : *strregname);
5492 if (regname == 0)
5493 regname = '"';
5494
5495 if (return_list)
5496 {
5497 rettv->v_type = VAR_LIST;
5498 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5499 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5500 if (rettv->vval.v_list == NULL)
5501 (void)rettv_list_alloc(rettv);
5502 else
5503 ++rettv->vval.v_list->lv_refcount;
5504 }
5505 else
5506 {
5507 rettv->v_type = VAR_STRING;
5508 rettv->vval.v_string = get_reg_contents(regname,
5509 arg2 ? GREG_EXPR_SRC : 0);
5510 }
5511}
5512
5513/*
5514 * "getregtype()" function
5515 */
5516 static void
5517f_getregtype(typval_T *argvars, typval_T *rettv)
5518{
5519 char_u *strregname;
5520 int regname;
5521 char_u buf[NUMBUFLEN + 2];
5522 long reglen = 0;
5523
5524 if (argvars[0].v_type != VAR_UNKNOWN)
5525 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005526 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005527 if (strregname == NULL) /* type error; errmsg already given */
5528 {
5529 rettv->v_type = VAR_STRING;
5530 rettv->vval.v_string = NULL;
5531 return;
5532 }
5533 }
5534 else
5535 /* Default to v:register */
5536 strregname = get_vim_var_str(VV_REG);
5537
5538 regname = (strregname == NULL ? '"' : *strregname);
5539 if (regname == 0)
5540 regname = '"';
5541
5542 buf[0] = NUL;
5543 buf[1] = NUL;
5544 switch (get_reg_type(regname, &reglen))
5545 {
5546 case MLINE: buf[0] = 'V'; break;
5547 case MCHAR: buf[0] = 'v'; break;
5548 case MBLOCK:
5549 buf[0] = Ctrl_V;
5550 sprintf((char *)buf + 1, "%ld", reglen + 1);
5551 break;
5552 }
5553 rettv->v_type = VAR_STRING;
5554 rettv->vval.v_string = vim_strsave(buf);
5555}
5556
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005557/*
5558 * Returns information (variables, options, etc.) about a tab page
5559 * as a dictionary.
5560 */
5561 static dict_T *
5562get_tabpage_info(tabpage_T *tp, int tp_idx)
5563{
5564 win_T *wp;
5565 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005566 list_T *l;
5567
5568 dict = dict_alloc();
5569 if (dict == NULL)
5570 return NULL;
5571
Bram Moolenaare0be1672018-07-08 16:50:37 +02005572 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005573
5574 l = list_alloc();
5575 if (l != NULL)
5576 {
5577 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5578 wp; wp = wp->w_next)
5579 list_append_number(l, (varnumber_T)wp->w_id);
5580 dict_add_list(dict, "windows", l);
5581 }
5582
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005583 /* Make a reference to tabpage variables */
5584 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005585
5586 return dict;
5587}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005588
5589/*
5590 * "gettabinfo()" function
5591 */
5592 static void
5593f_gettabinfo(typval_T *argvars, typval_T *rettv)
5594{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005595 tabpage_T *tp, *tparg = NULL;
5596 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005597 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005598
5599 if (rettv_list_alloc(rettv) != OK)
5600 return;
5601
5602 if (argvars[0].v_type != VAR_UNKNOWN)
5603 {
5604 /* Information about one tab page */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005605 tparg = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005606 if (tparg == NULL)
5607 return;
5608 }
5609
5610 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005611 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005612 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005613 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005614 if (tparg != NULL && tp != tparg)
5615 continue;
5616 d = get_tabpage_info(tp, tpnr);
5617 if (d != NULL)
5618 list_append_dict(rettv->vval.v_list, d);
5619 if (tparg != NULL)
5620 return;
5621 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005622}
5623
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005624/*
5625 * "gettabvar()" function
5626 */
5627 static void
5628f_gettabvar(typval_T *argvars, typval_T *rettv)
5629{
5630 win_T *oldcurwin;
5631 tabpage_T *tp, *oldtabpage;
5632 dictitem_T *v;
5633 char_u *varname;
5634 int done = FALSE;
5635
5636 rettv->v_type = VAR_STRING;
5637 rettv->vval.v_string = NULL;
5638
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005639 varname = tv_get_string_chk(&argvars[1]);
5640 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005641 if (tp != NULL && varname != NULL)
5642 {
5643 /* Set tp to be our tabpage, temporarily. Also set the window to the
5644 * first window in the tabpage, otherwise the window is not valid. */
5645 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005646 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5647 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005648 {
5649 /* look up the variable */
5650 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5651 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5652 if (v != NULL)
5653 {
5654 copy_tv(&v->di_tv, rettv);
5655 done = TRUE;
5656 }
5657 }
5658
5659 /* restore previous notion of curwin */
5660 restore_win(oldcurwin, oldtabpage, TRUE);
5661 }
5662
5663 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5664 copy_tv(&argvars[2], rettv);
5665}
5666
5667/*
5668 * "gettabwinvar()" function
5669 */
5670 static void
5671f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5672{
5673 getwinvar(argvars, rettv, 1);
5674}
5675
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005676/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01005677 * "gettagstack()" function
5678 */
5679 static void
5680f_gettagstack(typval_T *argvars, typval_T *rettv)
5681{
5682 win_T *wp = curwin; // default is current window
5683
5684 if (rettv_dict_alloc(rettv) != OK)
5685 return;
5686
5687 if (argvars[0].v_type != VAR_UNKNOWN)
5688 {
5689 wp = find_win_by_nr_or_id(&argvars[0]);
5690 if (wp == NULL)
5691 return;
5692 }
5693
5694 get_tagstack(wp, rettv->vval.v_dict);
5695}
5696
5697/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005698 * Returns information about a window as a dictionary.
5699 */
5700 static dict_T *
5701get_win_info(win_T *wp, short tpnr, short winnr)
5702{
5703 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005704
5705 dict = dict_alloc();
5706 if (dict == NULL)
5707 return NULL;
5708
Bram Moolenaare0be1672018-07-08 16:50:37 +02005709 dict_add_number(dict, "tabnr", tpnr);
5710 dict_add_number(dict, "winnr", winnr);
5711 dict_add_number(dict, "winid", wp->w_id);
5712 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005713 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005714#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02005715 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005716#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005717 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005718 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005719 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005720
Bram Moolenaar69905d12017-08-13 18:14:47 +02005721#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02005722 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02005723#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005724#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02005725 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
5726 dict_add_number(dict, "loclist",
5727 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02005728#endif
5729
Bram Moolenaar30567352016-08-27 21:25:44 +02005730 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005731 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005732
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005733 return dict;
5734}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005735
5736/*
5737 * "getwininfo()" function
5738 */
5739 static void
5740f_getwininfo(typval_T *argvars, typval_T *rettv)
5741{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005742 tabpage_T *tp;
5743 win_T *wp = NULL, *wparg = NULL;
5744 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005745 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005746
5747 if (rettv_list_alloc(rettv) != OK)
5748 return;
5749
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005750 if (argvars[0].v_type != VAR_UNKNOWN)
5751 {
5752 wparg = win_id2wp(argvars);
5753 if (wparg == NULL)
5754 return;
5755 }
5756
5757 /* Collect information about either all the windows across all the tab
5758 * pages or one particular window.
5759 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005760 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005761 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005762 tabnr++;
5763 winnr = 0;
5764 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005765 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005766 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005767 if (wparg != NULL && wp != wparg)
5768 continue;
5769 d = get_win_info(wp, tabnr, winnr);
5770 if (d != NULL)
5771 list_append_dict(rettv->vval.v_list, d);
5772 if (wparg != NULL)
5773 /* found information about a specific window */
5774 return;
5775 }
5776 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005777}
5778
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005779/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005780 * "win_findbuf()" function
5781 */
5782 static void
5783f_win_findbuf(typval_T *argvars, typval_T *rettv)
5784{
5785 if (rettv_list_alloc(rettv) != FAIL)
5786 win_findbuf(argvars, rettv->vval.v_list);
5787}
5788
5789/*
5790 * "win_getid()" function
5791 */
5792 static void
5793f_win_getid(typval_T *argvars, typval_T *rettv)
5794{
5795 rettv->vval.v_number = win_getid(argvars);
5796}
5797
5798/*
5799 * "win_gotoid()" function
5800 */
5801 static void
5802f_win_gotoid(typval_T *argvars, typval_T *rettv)
5803{
5804 rettv->vval.v_number = win_gotoid(argvars);
5805}
5806
5807/*
5808 * "win_id2tabwin()" function
5809 */
5810 static void
5811f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5812{
5813 if (rettv_list_alloc(rettv) != FAIL)
5814 win_id2tabwin(argvars, rettv->vval.v_list);
5815}
5816
5817/*
5818 * "win_id2win()" function
5819 */
5820 static void
5821f_win_id2win(typval_T *argvars, typval_T *rettv)
5822{
5823 rettv->vval.v_number = win_id2win(argvars);
5824}
5825
5826/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005827 * "win_screenpos()" function
5828 */
5829 static void
5830f_win_screenpos(typval_T *argvars, typval_T *rettv)
5831{
5832 win_T *wp;
5833
5834 if (rettv_list_alloc(rettv) == FAIL)
5835 return;
5836
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005837 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005838 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5839 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5840}
5841
5842/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005843 * "getwinpos({timeout})" function
5844 */
5845 static void
5846f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
5847{
5848 int x = -1;
5849 int y = -1;
5850
5851 if (rettv_list_alloc(rettv) == FAIL)
5852 return;
5853#ifdef FEAT_GUI
5854 if (gui.in_use)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01005855 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005856# if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5857 else
5858# endif
5859#endif
5860#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5861 {
5862 varnumber_T timeout = 100;
5863
5864 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005865 timeout = tv_get_number(&argvars[0]);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005866 term_get_winpos(&x, &y, timeout);
5867 }
5868#endif
5869 list_append_number(rettv->vval.v_list, (varnumber_T)x);
5870 list_append_number(rettv->vval.v_list, (varnumber_T)y);
5871}
5872
5873
5874/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005875 * "getwinposx()" function
5876 */
5877 static void
5878f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5879{
5880 rettv->vval.v_number = -1;
5881#ifdef FEAT_GUI
5882 if (gui.in_use)
5883 {
5884 int x, y;
5885
5886 if (gui_mch_get_winpos(&x, &y) == OK)
5887 rettv->vval.v_number = x;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005888 return;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005889 }
5890#endif
5891#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5892 {
5893 int x, y;
5894
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005895 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005896 rettv->vval.v_number = x;
5897 }
5898#endif
5899}
5900
5901/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005902 * "getwinposy()" function
5903 */
5904 static void
5905f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5906{
5907 rettv->vval.v_number = -1;
5908#ifdef FEAT_GUI
5909 if (gui.in_use)
5910 {
5911 int x, y;
5912
5913 if (gui_mch_get_winpos(&x, &y) == OK)
5914 rettv->vval.v_number = y;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005915 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005916 }
5917#endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005918#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5919 {
5920 int x, y;
5921
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005922 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005923 rettv->vval.v_number = y;
5924 }
5925#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005926}
5927
5928/*
5929 * "getwinvar()" function
5930 */
5931 static void
5932f_getwinvar(typval_T *argvars, typval_T *rettv)
5933{
5934 getwinvar(argvars, rettv, 0);
5935}
5936
5937/*
5938 * "glob()" function
5939 */
5940 static void
5941f_glob(typval_T *argvars, typval_T *rettv)
5942{
5943 int options = WILD_SILENT|WILD_USE_NL;
5944 expand_T xpc;
5945 int error = FALSE;
5946
5947 /* When the optional second argument is non-zero, don't remove matches
5948 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5949 rettv->v_type = VAR_STRING;
5950 if (argvars[1].v_type != VAR_UNKNOWN)
5951 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005952 if (tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005953 options |= WILD_KEEP_ALL;
5954 if (argvars[2].v_type != VAR_UNKNOWN)
5955 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005956 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005957 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005958 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005959 }
5960 if (argvars[3].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005961 && tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005962 options |= WILD_ALLLINKS;
5963 }
5964 }
5965 if (!error)
5966 {
5967 ExpandInit(&xpc);
5968 xpc.xp_context = EXPAND_FILES;
5969 if (p_wic)
5970 options += WILD_ICASE;
5971 if (rettv->v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005972 rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005973 NULL, options, WILD_ALL);
5974 else if (rettv_list_alloc(rettv) != FAIL)
5975 {
5976 int i;
5977
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005978 ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005979 NULL, options, WILD_ALL_KEEP);
5980 for (i = 0; i < xpc.xp_numfiles; i++)
5981 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5982
5983 ExpandCleanup(&xpc);
5984 }
5985 }
5986 else
5987 rettv->vval.v_string = NULL;
5988}
5989
5990/*
5991 * "globpath()" function
5992 */
5993 static void
5994f_globpath(typval_T *argvars, typval_T *rettv)
5995{
5996 int flags = 0;
5997 char_u buf1[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005998 char_u *file = tv_get_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005999 int error = FALSE;
6000 garray_T ga;
6001 int i;
6002
6003 /* When the optional second argument is non-zero, don't remove matches
6004 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6005 rettv->v_type = VAR_STRING;
6006 if (argvars[2].v_type != VAR_UNKNOWN)
6007 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006008 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006009 flags |= WILD_KEEP_ALL;
6010 if (argvars[3].v_type != VAR_UNKNOWN)
6011 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006012 if (tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006013 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006014 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006015 }
6016 if (argvars[4].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006017 && tv_get_number_chk(&argvars[4], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006018 flags |= WILD_ALLLINKS;
6019 }
6020 }
6021 if (file != NULL && !error)
6022 {
6023 ga_init2(&ga, (int)sizeof(char_u *), 10);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006024 globpath(tv_get_string(&argvars[0]), file, &ga, flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006025 if (rettv->v_type == VAR_STRING)
6026 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
6027 else if (rettv_list_alloc(rettv) != FAIL)
6028 for (i = 0; i < ga.ga_len; ++i)
6029 list_append_string(rettv->vval.v_list,
6030 ((char_u **)(ga.ga_data))[i], -1);
6031 ga_clear_strings(&ga);
6032 }
6033 else
6034 rettv->vval.v_string = NULL;
6035}
6036
6037/*
6038 * "glob2regpat()" function
6039 */
6040 static void
6041f_glob2regpat(typval_T *argvars, typval_T *rettv)
6042{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006043 char_u *pat = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006044
6045 rettv->v_type = VAR_STRING;
6046 rettv->vval.v_string = (pat == NULL)
6047 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
6048}
6049
6050/* for VIM_VERSION_ defines */
6051#include "version.h"
6052
6053/*
6054 * "has()" function
6055 */
6056 static void
6057f_has(typval_T *argvars, typval_T *rettv)
6058{
6059 int i;
6060 char_u *name;
6061 int n = FALSE;
6062 static char *(has_list[]) =
6063 {
6064#ifdef AMIGA
6065 "amiga",
6066# ifdef FEAT_ARP
6067 "arp",
6068# endif
6069#endif
6070#ifdef __BEOS__
6071 "beos",
6072#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02006073#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01006074 "mac", /* Mac OS X (and, once, Mac OS Classic) */
6075 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02006076# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01006077 "macunix", /* Mac OS X, with the darwin feature */
6078 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02006079# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006080#endif
6081#ifdef __QNX__
6082 "qnx",
6083#endif
6084#ifdef UNIX
6085 "unix",
6086#endif
6087#ifdef VMS
6088 "vms",
6089#endif
6090#ifdef WIN32
6091 "win32",
6092#endif
6093#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
6094 "win32unix",
6095#endif
6096#if defined(WIN64) || defined(_WIN64)
6097 "win64",
6098#endif
6099#ifdef EBCDIC
6100 "ebcdic",
6101#endif
6102#ifndef CASE_INSENSITIVE_FILENAME
6103 "fname_case",
6104#endif
6105#ifdef HAVE_ACL
6106 "acl",
6107#endif
6108#ifdef FEAT_ARABIC
6109 "arabic",
6110#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006111 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006112#ifdef FEAT_AUTOCHDIR
6113 "autochdir",
6114#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006115#ifdef FEAT_AUTOSERVERNAME
6116 "autoservername",
6117#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006118#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006119 "balloon_eval",
6120# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
6121 "balloon_multiline",
6122# endif
6123#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006124#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006125 "balloon_eval_term",
6126#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006127#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6128 "builtin_terms",
6129# ifdef ALL_BUILTIN_TCAPS
6130 "all_builtin_terms",
6131# endif
6132#endif
6133#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
6134 || defined(FEAT_GUI_W32) \
6135 || defined(FEAT_GUI_MOTIF))
6136 "browsefilter",
6137#endif
6138#ifdef FEAT_BYTEOFF
6139 "byte_offset",
6140#endif
6141#ifdef FEAT_JOB_CHANNEL
6142 "channel",
6143#endif
6144#ifdef FEAT_CINDENT
6145 "cindent",
6146#endif
6147#ifdef FEAT_CLIENTSERVER
6148 "clientserver",
6149#endif
6150#ifdef FEAT_CLIPBOARD
6151 "clipboard",
6152#endif
6153#ifdef FEAT_CMDL_COMPL
6154 "cmdline_compl",
6155#endif
6156#ifdef FEAT_CMDHIST
6157 "cmdline_hist",
6158#endif
6159#ifdef FEAT_COMMENTS
6160 "comments",
6161#endif
6162#ifdef FEAT_CONCEAL
6163 "conceal",
6164#endif
6165#ifdef FEAT_CRYPT
6166 "cryptv",
6167 "crypt-blowfish",
6168 "crypt-blowfish2",
6169#endif
6170#ifdef FEAT_CSCOPE
6171 "cscope",
6172#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006173 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006174#ifdef CURSOR_SHAPE
6175 "cursorshape",
6176#endif
6177#ifdef DEBUG
6178 "debug",
6179#endif
6180#ifdef FEAT_CON_DIALOG
6181 "dialog_con",
6182#endif
6183#ifdef FEAT_GUI_DIALOG
6184 "dialog_gui",
6185#endif
6186#ifdef FEAT_DIFF
6187 "diff",
6188#endif
6189#ifdef FEAT_DIGRAPHS
6190 "digraphs",
6191#endif
6192#ifdef FEAT_DIRECTX
6193 "directx",
6194#endif
6195#ifdef FEAT_DND
6196 "dnd",
6197#endif
6198#ifdef FEAT_EMACS_TAGS
6199 "emacs_tags",
6200#endif
6201 "eval", /* always present, of course! */
6202 "ex_extra", /* graduated feature */
6203#ifdef FEAT_SEARCH_EXTRA
6204 "extra_search",
6205#endif
6206#ifdef FEAT_FKMAP
6207 "farsi",
6208#endif
6209#ifdef FEAT_SEARCHPATH
6210 "file_in_path",
6211#endif
6212#ifdef FEAT_FILTERPIPE
6213 "filterpipe",
6214#endif
6215#ifdef FEAT_FIND_ID
6216 "find_in_path",
6217#endif
6218#ifdef FEAT_FLOAT
6219 "float",
6220#endif
6221#ifdef FEAT_FOLDING
6222 "folding",
6223#endif
6224#ifdef FEAT_FOOTER
6225 "footer",
6226#endif
6227#if !defined(USE_SYSTEM) && defined(UNIX)
6228 "fork",
6229#endif
6230#ifdef FEAT_GETTEXT
6231 "gettext",
6232#endif
6233#ifdef FEAT_GUI
6234 "gui",
6235#endif
6236#ifdef FEAT_GUI_ATHENA
6237# ifdef FEAT_GUI_NEXTAW
6238 "gui_neXtaw",
6239# else
6240 "gui_athena",
6241# endif
6242#endif
6243#ifdef FEAT_GUI_GTK
6244 "gui_gtk",
6245# ifdef USE_GTK3
6246 "gui_gtk3",
6247# else
6248 "gui_gtk2",
6249# endif
6250#endif
6251#ifdef FEAT_GUI_GNOME
6252 "gui_gnome",
6253#endif
6254#ifdef FEAT_GUI_MAC
6255 "gui_mac",
6256#endif
6257#ifdef FEAT_GUI_MOTIF
6258 "gui_motif",
6259#endif
6260#ifdef FEAT_GUI_PHOTON
6261 "gui_photon",
6262#endif
6263#ifdef FEAT_GUI_W32
6264 "gui_win32",
6265#endif
6266#ifdef FEAT_HANGULIN
6267 "hangul_input",
6268#endif
6269#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6270 "iconv",
6271#endif
6272#ifdef FEAT_INS_EXPAND
6273 "insert_expand",
6274#endif
6275#ifdef FEAT_JOB_CHANNEL
6276 "job",
6277#endif
6278#ifdef FEAT_JUMPLIST
6279 "jumplist",
6280#endif
6281#ifdef FEAT_KEYMAP
6282 "keymap",
6283#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006284 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006285#ifdef FEAT_LANGMAP
6286 "langmap",
6287#endif
6288#ifdef FEAT_LIBCALL
6289 "libcall",
6290#endif
6291#ifdef FEAT_LINEBREAK
6292 "linebreak",
6293#endif
6294#ifdef FEAT_LISP
6295 "lispindent",
6296#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006297 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006298#ifdef FEAT_LOCALMAP
6299 "localmap",
6300#endif
6301#ifdef FEAT_LUA
6302# ifndef DYNAMIC_LUA
6303 "lua",
6304# endif
6305#endif
6306#ifdef FEAT_MENU
6307 "menu",
6308#endif
6309#ifdef FEAT_SESSION
6310 "mksession",
6311#endif
6312#ifdef FEAT_MODIFY_FNAME
6313 "modify_fname",
6314#endif
6315#ifdef FEAT_MOUSE
6316 "mouse",
6317#endif
6318#ifdef FEAT_MOUSESHAPE
6319 "mouseshape",
6320#endif
6321#if defined(UNIX) || defined(VMS)
6322# ifdef FEAT_MOUSE_DEC
6323 "mouse_dec",
6324# endif
6325# ifdef FEAT_MOUSE_GPM
6326 "mouse_gpm",
6327# endif
6328# ifdef FEAT_MOUSE_JSB
6329 "mouse_jsbterm",
6330# endif
6331# ifdef FEAT_MOUSE_NET
6332 "mouse_netterm",
6333# endif
6334# ifdef FEAT_MOUSE_PTERM
6335 "mouse_pterm",
6336# endif
6337# ifdef FEAT_MOUSE_SGR
6338 "mouse_sgr",
6339# endif
6340# ifdef FEAT_SYSMOUSE
6341 "mouse_sysmouse",
6342# endif
6343# ifdef FEAT_MOUSE_URXVT
6344 "mouse_urxvt",
6345# endif
6346# ifdef FEAT_MOUSE_XTERM
6347 "mouse_xterm",
6348# endif
6349#endif
6350#ifdef FEAT_MBYTE
6351 "multi_byte",
6352#endif
6353#ifdef FEAT_MBYTE_IME
6354 "multi_byte_ime",
6355#endif
6356#ifdef FEAT_MULTI_LANG
6357 "multi_lang",
6358#endif
6359#ifdef FEAT_MZSCHEME
6360#ifndef DYNAMIC_MZSCHEME
6361 "mzscheme",
6362#endif
6363#endif
6364#ifdef FEAT_NUM64
6365 "num64",
6366#endif
6367#ifdef FEAT_OLE
6368 "ole",
6369#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006370#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006371 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006372#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006373#ifdef FEAT_PATH_EXTRA
6374 "path_extra",
6375#endif
6376#ifdef FEAT_PERL
6377#ifndef DYNAMIC_PERL
6378 "perl",
6379#endif
6380#endif
6381#ifdef FEAT_PERSISTENT_UNDO
6382 "persistent_undo",
6383#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006384#if defined(FEAT_PYTHON)
6385 "python_compiled",
6386# if defined(DYNAMIC_PYTHON)
6387 "python_dynamic",
6388# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006389 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006390 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006391# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006392#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006393#if defined(FEAT_PYTHON3)
6394 "python3_compiled",
6395# if defined(DYNAMIC_PYTHON3)
6396 "python3_dynamic",
6397# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006398 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006399 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006400# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006401#endif
6402#ifdef FEAT_POSTSCRIPT
6403 "postscript",
6404#endif
6405#ifdef FEAT_PRINTER
6406 "printer",
6407#endif
6408#ifdef FEAT_PROFILE
6409 "profile",
6410#endif
6411#ifdef FEAT_RELTIME
6412 "reltime",
6413#endif
6414#ifdef FEAT_QUICKFIX
6415 "quickfix",
6416#endif
6417#ifdef FEAT_RIGHTLEFT
6418 "rightleft",
6419#endif
6420#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6421 "ruby",
6422#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006423 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006424#ifdef FEAT_CMDL_INFO
6425 "showcmd",
6426 "cmdline_info",
6427#endif
6428#ifdef FEAT_SIGNS
6429 "signs",
6430#endif
6431#ifdef FEAT_SMARTINDENT
6432 "smartindent",
6433#endif
6434#ifdef STARTUPTIME
6435 "startuptime",
6436#endif
6437#ifdef FEAT_STL_OPT
6438 "statusline",
6439#endif
6440#ifdef FEAT_SUN_WORKSHOP
6441 "sun_workshop",
6442#endif
6443#ifdef FEAT_NETBEANS_INTG
6444 "netbeans_intg",
6445#endif
6446#ifdef FEAT_SPELL
6447 "spell",
6448#endif
6449#ifdef FEAT_SYN_HL
6450 "syntax",
6451#endif
6452#if defined(USE_SYSTEM) || !defined(UNIX)
6453 "system",
6454#endif
6455#ifdef FEAT_TAG_BINS
6456 "tag_binary",
6457#endif
6458#ifdef FEAT_TAG_OLDSTATIC
6459 "tag_old_static",
6460#endif
6461#ifdef FEAT_TAG_ANYWHITE
6462 "tag_any_white",
6463#endif
6464#ifdef FEAT_TCL
6465# ifndef DYNAMIC_TCL
6466 "tcl",
6467# endif
6468#endif
6469#ifdef FEAT_TERMGUICOLORS
6470 "termguicolors",
6471#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006472#if defined(FEAT_TERMINAL) && !defined(WIN3264)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006473 "terminal",
6474#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006475#ifdef TERMINFO
6476 "terminfo",
6477#endif
6478#ifdef FEAT_TERMRESPONSE
6479 "termresponse",
6480#endif
6481#ifdef FEAT_TEXTOBJ
6482 "textobjects",
6483#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006484#ifdef FEAT_TEXT_PROP
6485 "textprop",
6486#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006487#ifdef HAVE_TGETENT
6488 "tgetent",
6489#endif
6490#ifdef FEAT_TIMERS
6491 "timers",
6492#endif
6493#ifdef FEAT_TITLE
6494 "title",
6495#endif
6496#ifdef FEAT_TOOLBAR
6497 "toolbar",
6498#endif
6499#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6500 "unnamedplus",
6501#endif
6502#ifdef FEAT_USR_CMDS
6503 "user-commands", /* was accidentally included in 5.4 */
6504 "user_commands",
6505#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006506#ifdef FEAT_VARTABS
6507 "vartabs",
6508#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006509#ifdef FEAT_VIMINFO
6510 "viminfo",
6511#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006512 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006513#ifdef FEAT_VIRTUALEDIT
6514 "virtualedit",
6515#endif
6516 "visual",
6517#ifdef FEAT_VISUALEXTRA
6518 "visualextra",
6519#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006520 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006521#ifdef FEAT_VTP
6522 "vtp",
6523#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006524#ifdef FEAT_WILDIGN
6525 "wildignore",
6526#endif
6527#ifdef FEAT_WILDMENU
6528 "wildmenu",
6529#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006530 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006531#ifdef FEAT_WAK
6532 "winaltkeys",
6533#endif
6534#ifdef FEAT_WRITEBACKUP
6535 "writebackup",
6536#endif
6537#ifdef FEAT_XIM
6538 "xim",
6539#endif
6540#ifdef FEAT_XFONTSET
6541 "xfontset",
6542#endif
6543#ifdef FEAT_XPM_W32
6544 "xpm",
6545 "xpm_w32", /* for backward compatibility */
6546#else
6547# if defined(HAVE_XPM)
6548 "xpm",
6549# endif
6550#endif
6551#ifdef USE_XSMP
6552 "xsmp",
6553#endif
6554#ifdef USE_XSMP_INTERACT
6555 "xsmp_interact",
6556#endif
6557#ifdef FEAT_XCLIPBOARD
6558 "xterm_clipboard",
6559#endif
6560#ifdef FEAT_XTERM_SAVE
6561 "xterm_save",
6562#endif
6563#if defined(UNIX) && defined(FEAT_X11)
6564 "X11",
6565#endif
6566 NULL
6567 };
6568
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006569 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006570 for (i = 0; has_list[i] != NULL; ++i)
6571 if (STRICMP(name, has_list[i]) == 0)
6572 {
6573 n = TRUE;
6574 break;
6575 }
6576
6577 if (n == FALSE)
6578 {
6579 if (STRNICMP(name, "patch", 5) == 0)
6580 {
6581 if (name[5] == '-'
6582 && STRLEN(name) >= 11
6583 && vim_isdigit(name[6])
6584 && vim_isdigit(name[8])
6585 && vim_isdigit(name[10]))
6586 {
6587 int major = atoi((char *)name + 6);
6588 int minor = atoi((char *)name + 8);
6589
6590 /* Expect "patch-9.9.01234". */
6591 n = (major < VIM_VERSION_MAJOR
6592 || (major == VIM_VERSION_MAJOR
6593 && (minor < VIM_VERSION_MINOR
6594 || (minor == VIM_VERSION_MINOR
6595 && has_patch(atoi((char *)name + 10))))));
6596 }
6597 else
6598 n = has_patch(atoi((char *)name + 5));
6599 }
6600 else if (STRICMP(name, "vim_starting") == 0)
6601 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006602 else if (STRICMP(name, "ttyin") == 0)
6603 n = mch_input_isatty();
6604 else if (STRICMP(name, "ttyout") == 0)
6605 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006606#ifdef FEAT_MBYTE
6607 else if (STRICMP(name, "multi_byte_encoding") == 0)
6608 n = has_mbyte;
6609#endif
6610#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
6611 else if (STRICMP(name, "balloon_multiline") == 0)
6612 n = multiline_balloon_available();
6613#endif
6614#ifdef DYNAMIC_TCL
6615 else if (STRICMP(name, "tcl") == 0)
6616 n = tcl_enabled(FALSE);
6617#endif
6618#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6619 else if (STRICMP(name, "iconv") == 0)
6620 n = iconv_enabled(FALSE);
6621#endif
6622#ifdef DYNAMIC_LUA
6623 else if (STRICMP(name, "lua") == 0)
6624 n = lua_enabled(FALSE);
6625#endif
6626#ifdef DYNAMIC_MZSCHEME
6627 else if (STRICMP(name, "mzscheme") == 0)
6628 n = mzscheme_enabled(FALSE);
6629#endif
6630#ifdef DYNAMIC_RUBY
6631 else if (STRICMP(name, "ruby") == 0)
6632 n = ruby_enabled(FALSE);
6633#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006634#ifdef DYNAMIC_PYTHON
6635 else if (STRICMP(name, "python") == 0)
6636 n = python_enabled(FALSE);
6637#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006638#ifdef DYNAMIC_PYTHON3
6639 else if (STRICMP(name, "python3") == 0)
6640 n = python3_enabled(FALSE);
6641#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006642#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6643 else if (STRICMP(name, "pythonx") == 0)
6644 {
6645# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6646 if (p_pyx == 0)
6647 n = python3_enabled(FALSE) || python_enabled(FALSE);
6648 else if (p_pyx == 3)
6649 n = python3_enabled(FALSE);
6650 else if (p_pyx == 2)
6651 n = python_enabled(FALSE);
6652# elif defined(DYNAMIC_PYTHON)
6653 n = python_enabled(FALSE);
6654# elif defined(DYNAMIC_PYTHON3)
6655 n = python3_enabled(FALSE);
6656# endif
6657 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006658#endif
6659#ifdef DYNAMIC_PERL
6660 else if (STRICMP(name, "perl") == 0)
6661 n = perl_enabled(FALSE);
6662#endif
6663#ifdef FEAT_GUI
6664 else if (STRICMP(name, "gui_running") == 0)
6665 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006666# ifdef FEAT_BROWSE
6667 else if (STRICMP(name, "browse") == 0)
6668 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6669# endif
6670#endif
6671#ifdef FEAT_SYN_HL
6672 else if (STRICMP(name, "syntax_items") == 0)
6673 n = syntax_present(curwin);
6674#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006675#ifdef FEAT_VTP
6676 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02006677 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006678#endif
6679#ifdef FEAT_NETBEANS_INTG
6680 else if (STRICMP(name, "netbeans_enabled") == 0)
6681 n = netbeans_active();
6682#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006683#if defined(FEAT_TERMINAL) && defined(WIN3264)
6684 else if (STRICMP(name, "terminal") == 0)
6685 n = terminal_enabled();
6686#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006687 }
6688
6689 rettv->vval.v_number = n;
6690}
6691
6692/*
6693 * "has_key()" function
6694 */
6695 static void
6696f_has_key(typval_T *argvars, typval_T *rettv)
6697{
6698 if (argvars[0].v_type != VAR_DICT)
6699 {
6700 EMSG(_(e_dictreq));
6701 return;
6702 }
6703 if (argvars[0].vval.v_dict == NULL)
6704 return;
6705
6706 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006707 tv_get_string(&argvars[1]), -1) != NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006708}
6709
6710/*
6711 * "haslocaldir()" function
6712 */
6713 static void
6714f_haslocaldir(typval_T *argvars, typval_T *rettv)
6715{
6716 win_T *wp = NULL;
6717
6718 wp = find_tabwin(&argvars[0], &argvars[1]);
6719 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6720}
6721
6722/*
6723 * "hasmapto()" function
6724 */
6725 static void
6726f_hasmapto(typval_T *argvars, typval_T *rettv)
6727{
6728 char_u *name;
6729 char_u *mode;
6730 char_u buf[NUMBUFLEN];
6731 int abbr = FALSE;
6732
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006733 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006734 if (argvars[1].v_type == VAR_UNKNOWN)
6735 mode = (char_u *)"nvo";
6736 else
6737 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006738 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006739 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006740 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006741 }
6742
6743 if (map_to_exists(name, mode, abbr))
6744 rettv->vval.v_number = TRUE;
6745 else
6746 rettv->vval.v_number = FALSE;
6747}
6748
6749/*
6750 * "histadd()" function
6751 */
6752 static void
6753f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6754{
6755#ifdef FEAT_CMDHIST
6756 int histype;
6757 char_u *str;
6758 char_u buf[NUMBUFLEN];
6759#endif
6760
6761 rettv->vval.v_number = FALSE;
6762 if (check_restricted() || check_secure())
6763 return;
6764#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006765 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006766 histype = str != NULL ? get_histtype(str) : -1;
6767 if (histype >= 0)
6768 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006769 str = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006770 if (*str != NUL)
6771 {
6772 init_history();
6773 add_to_history(histype, str, FALSE, NUL);
6774 rettv->vval.v_number = TRUE;
6775 return;
6776 }
6777 }
6778#endif
6779}
6780
6781/*
6782 * "histdel()" function
6783 */
6784 static void
6785f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6786{
6787#ifdef FEAT_CMDHIST
6788 int n;
6789 char_u buf[NUMBUFLEN];
6790 char_u *str;
6791
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006792 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006793 if (str == NULL)
6794 n = 0;
6795 else if (argvars[1].v_type == VAR_UNKNOWN)
6796 /* only one argument: clear entire history */
6797 n = clr_history(get_histtype(str));
6798 else if (argvars[1].v_type == VAR_NUMBER)
6799 /* index given: remove that entry */
6800 n = del_history_idx(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006801 (int)tv_get_number(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006802 else
6803 /* string given: remove all matching entries */
6804 n = del_history_entry(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006805 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006806 rettv->vval.v_number = n;
6807#endif
6808}
6809
6810/*
6811 * "histget()" function
6812 */
6813 static void
6814f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6815{
6816#ifdef FEAT_CMDHIST
6817 int type;
6818 int idx;
6819 char_u *str;
6820
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006821 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006822 if (str == NULL)
6823 rettv->vval.v_string = NULL;
6824 else
6825 {
6826 type = get_histtype(str);
6827 if (argvars[1].v_type == VAR_UNKNOWN)
6828 idx = get_history_idx(type);
6829 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006830 idx = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006831 /* -1 on type error */
6832 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6833 }
6834#else
6835 rettv->vval.v_string = NULL;
6836#endif
6837 rettv->v_type = VAR_STRING;
6838}
6839
6840/*
6841 * "histnr()" function
6842 */
6843 static void
6844f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6845{
6846 int i;
6847
6848#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006849 char_u *history = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006850
6851 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6852 if (i >= HIST_CMD && i < HIST_COUNT)
6853 i = get_history_idx(i);
6854 else
6855#endif
6856 i = -1;
6857 rettv->vval.v_number = i;
6858}
6859
6860/*
6861 * "highlightID(name)" function
6862 */
6863 static void
6864f_hlID(typval_T *argvars, typval_T *rettv)
6865{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006866 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006867}
6868
6869/*
6870 * "highlight_exists()" function
6871 */
6872 static void
6873f_hlexists(typval_T *argvars, typval_T *rettv)
6874{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006875 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006876}
6877
6878/*
6879 * "hostname()" function
6880 */
6881 static void
6882f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6883{
6884 char_u hostname[256];
6885
6886 mch_get_host_name(hostname, 256);
6887 rettv->v_type = VAR_STRING;
6888 rettv->vval.v_string = vim_strsave(hostname);
6889}
6890
6891/*
6892 * iconv() function
6893 */
6894 static void
6895f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6896{
6897#ifdef FEAT_MBYTE
6898 char_u buf1[NUMBUFLEN];
6899 char_u buf2[NUMBUFLEN];
6900 char_u *from, *to, *str;
6901 vimconv_T vimconv;
6902#endif
6903
6904 rettv->v_type = VAR_STRING;
6905 rettv->vval.v_string = NULL;
6906
6907#ifdef FEAT_MBYTE
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006908 str = tv_get_string(&argvars[0]);
6909 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
6910 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006911 vimconv.vc_type = CONV_NONE;
6912 convert_setup(&vimconv, from, to);
6913
6914 /* If the encodings are equal, no conversion needed. */
6915 if (vimconv.vc_type == CONV_NONE)
6916 rettv->vval.v_string = vim_strsave(str);
6917 else
6918 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6919
6920 convert_setup(&vimconv, NULL, NULL);
6921 vim_free(from);
6922 vim_free(to);
6923#endif
6924}
6925
6926/*
6927 * "indent()" function
6928 */
6929 static void
6930f_indent(typval_T *argvars, typval_T *rettv)
6931{
6932 linenr_T lnum;
6933
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006934 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006935 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6936 rettv->vval.v_number = get_indent_lnum(lnum);
6937 else
6938 rettv->vval.v_number = -1;
6939}
6940
6941/*
6942 * "index()" function
6943 */
6944 static void
6945f_index(typval_T *argvars, typval_T *rettv)
6946{
6947 list_T *l;
6948 listitem_T *item;
6949 long idx = 0;
6950 int ic = FALSE;
6951
6952 rettv->vval.v_number = -1;
6953 if (argvars[0].v_type != VAR_LIST)
6954 {
6955 EMSG(_(e_listreq));
6956 return;
6957 }
6958 l = argvars[0].vval.v_list;
6959 if (l != NULL)
6960 {
6961 item = l->lv_first;
6962 if (argvars[2].v_type != VAR_UNKNOWN)
6963 {
6964 int error = FALSE;
6965
6966 /* Start at specified item. Use the cached index that list_find()
6967 * sets, so that a negative number also works. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006968 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006969 idx = l->lv_idx;
6970 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006971 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006972 if (error)
6973 item = NULL;
6974 }
6975
6976 for ( ; item != NULL; item = item->li_next, ++idx)
6977 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6978 {
6979 rettv->vval.v_number = idx;
6980 break;
6981 }
6982 }
6983}
6984
6985static int inputsecret_flag = 0;
6986
6987/*
6988 * "input()" function
6989 * Also handles inputsecret() when inputsecret is set.
6990 */
6991 static void
6992f_input(typval_T *argvars, typval_T *rettv)
6993{
6994 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6995}
6996
6997/*
6998 * "inputdialog()" function
6999 */
7000 static void
7001f_inputdialog(typval_T *argvars, typval_T *rettv)
7002{
7003#if defined(FEAT_GUI_TEXTDIALOG)
7004 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
7005 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
7006 {
7007 char_u *message;
7008 char_u buf[NUMBUFLEN];
7009 char_u *defstr = (char_u *)"";
7010
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007011 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007012 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007013 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007014 vim_strncpy(IObuff, defstr, IOSIZE - 1);
7015 else
7016 IObuff[0] = NUL;
7017 if (message != NULL && defstr != NULL
7018 && do_dialog(VIM_QUESTION, NULL, message,
7019 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
7020 rettv->vval.v_string = vim_strsave(IObuff);
7021 else
7022 {
7023 if (message != NULL && defstr != NULL
7024 && argvars[1].v_type != VAR_UNKNOWN
7025 && argvars[2].v_type != VAR_UNKNOWN)
7026 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007027 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007028 else
7029 rettv->vval.v_string = NULL;
7030 }
7031 rettv->v_type = VAR_STRING;
7032 }
7033 else
7034#endif
7035 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
7036}
7037
7038/*
7039 * "inputlist()" function
7040 */
7041 static void
7042f_inputlist(typval_T *argvars, typval_T *rettv)
7043{
7044 listitem_T *li;
7045 int selected;
7046 int mouse_used;
7047
7048#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007049 /* While starting up, there is no place to enter text. When running tests
7050 * with --not-a-term we assume feedkeys() will be used. */
7051 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007052 return;
7053#endif
7054 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
7055 {
7056 EMSG2(_(e_listarg), "inputlist()");
7057 return;
7058 }
7059
7060 msg_start();
7061 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
7062 lines_left = Rows; /* avoid more prompt */
7063 msg_scroll = TRUE;
7064 msg_clr_eos();
7065
7066 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
7067 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007068 msg_puts(tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007069 msg_putchar('\n');
7070 }
7071
7072 /* Ask for choice. */
7073 selected = prompt_for_number(&mouse_used);
7074 if (mouse_used)
7075 selected -= lines_left;
7076
7077 rettv->vval.v_number = selected;
7078}
7079
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007080static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7081
7082/*
7083 * "inputrestore()" function
7084 */
7085 static void
7086f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7087{
7088 if (ga_userinput.ga_len > 0)
7089 {
7090 --ga_userinput.ga_len;
7091 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7092 + ga_userinput.ga_len);
7093 /* default return is zero == OK */
7094 }
7095 else if (p_verbose > 1)
7096 {
7097 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
7098 rettv->vval.v_number = 1; /* Failed */
7099 }
7100}
7101
7102/*
7103 * "inputsave()" function
7104 */
7105 static void
7106f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7107{
7108 /* Add an entry to the stack of typeahead storage. */
7109 if (ga_grow(&ga_userinput, 1) == OK)
7110 {
7111 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7112 + ga_userinput.ga_len);
7113 ++ga_userinput.ga_len;
7114 /* default return is zero == OK */
7115 }
7116 else
7117 rettv->vval.v_number = 1; /* Failed */
7118}
7119
7120/*
7121 * "inputsecret()" function
7122 */
7123 static void
7124f_inputsecret(typval_T *argvars, typval_T *rettv)
7125{
7126 ++cmdline_star;
7127 ++inputsecret_flag;
7128 f_input(argvars, rettv);
7129 --cmdline_star;
7130 --inputsecret_flag;
7131}
7132
7133/*
7134 * "insert()" function
7135 */
7136 static void
7137f_insert(typval_T *argvars, typval_T *rettv)
7138{
7139 long before = 0;
7140 listitem_T *item;
7141 list_T *l;
7142 int error = FALSE;
7143
7144 if (argvars[0].v_type != VAR_LIST)
7145 EMSG2(_(e_listarg), "insert()");
7146 else if ((l = argvars[0].vval.v_list) != NULL
7147 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
7148 {
7149 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007150 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007151 if (error)
7152 return; /* type error; errmsg already given */
7153
7154 if (before == l->lv_len)
7155 item = NULL;
7156 else
7157 {
7158 item = list_find(l, before);
7159 if (item == NULL)
7160 {
7161 EMSGN(_(e_listidx), before);
7162 l = NULL;
7163 }
7164 }
7165 if (l != NULL)
7166 {
7167 list_insert_tv(l, &argvars[1], item);
7168 copy_tv(&argvars[0], rettv);
7169 }
7170 }
7171}
7172
7173/*
7174 * "invert(expr)" function
7175 */
7176 static void
7177f_invert(typval_T *argvars, typval_T *rettv)
7178{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007179 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007180}
7181
7182/*
7183 * "isdirectory()" function
7184 */
7185 static void
7186f_isdirectory(typval_T *argvars, typval_T *rettv)
7187{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007188 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007189}
7190
7191/*
7192 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7193 * or it refers to a List or Dictionary that is locked.
7194 */
7195 static int
7196tv_islocked(typval_T *tv)
7197{
7198 return (tv->v_lock & VAR_LOCKED)
7199 || (tv->v_type == VAR_LIST
7200 && tv->vval.v_list != NULL
7201 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7202 || (tv->v_type == VAR_DICT
7203 && tv->vval.v_dict != NULL
7204 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7205}
7206
7207/*
7208 * "islocked()" function
7209 */
7210 static void
7211f_islocked(typval_T *argvars, typval_T *rettv)
7212{
7213 lval_T lv;
7214 char_u *end;
7215 dictitem_T *di;
7216
7217 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007218 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007219 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007220 if (end != NULL && lv.ll_name != NULL)
7221 {
7222 if (*end != NUL)
7223 EMSG(_(e_trailing));
7224 else
7225 {
7226 if (lv.ll_tv == NULL)
7227 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007228 di = find_var(lv.ll_name, NULL, TRUE);
7229 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007230 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007231 /* Consider a variable locked when:
7232 * 1. the variable itself is locked
7233 * 2. the value of the variable is locked.
7234 * 3. the List or Dict value is locked.
7235 */
7236 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7237 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007238 }
7239 }
7240 else if (lv.ll_range)
7241 EMSG(_("E786: Range not allowed"));
7242 else if (lv.ll_newkey != NULL)
7243 EMSG2(_(e_dictkey), lv.ll_newkey);
7244 else if (lv.ll_list != NULL)
7245 /* List item. */
7246 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7247 else
7248 /* Dictionary item. */
7249 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7250 }
7251 }
7252
7253 clear_lval(&lv);
7254}
7255
7256#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7257/*
7258 * "isnan()" function
7259 */
7260 static void
7261f_isnan(typval_T *argvars, typval_T *rettv)
7262{
7263 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7264 && isnan(argvars[0].vval.v_float);
7265}
7266#endif
7267
7268/*
7269 * "items(dict)" function
7270 */
7271 static void
7272f_items(typval_T *argvars, typval_T *rettv)
7273{
7274 dict_list(argvars, rettv, 2);
7275}
7276
7277#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
7278/*
7279 * Get the job from the argument.
7280 * Returns NULL if the job is invalid.
7281 */
7282 static job_T *
7283get_job_arg(typval_T *tv)
7284{
7285 job_T *job;
7286
7287 if (tv->v_type != VAR_JOB)
7288 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007289 EMSG2(_(e_invarg2), tv_get_string(tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007290 return NULL;
7291 }
7292 job = tv->vval.v_job;
7293
7294 if (job == NULL)
7295 EMSG(_("E916: not a valid job"));
7296 return job;
7297}
7298
7299/*
7300 * "job_getchannel()" function
7301 */
7302 static void
7303f_job_getchannel(typval_T *argvars, typval_T *rettv)
7304{
7305 job_T *job = get_job_arg(&argvars[0]);
7306
7307 if (job != NULL)
7308 {
7309 rettv->v_type = VAR_CHANNEL;
7310 rettv->vval.v_channel = job->jv_channel;
7311 if (job->jv_channel != NULL)
7312 ++job->jv_channel->ch_refcount;
7313 }
7314}
7315
7316/*
7317 * "job_info()" function
7318 */
7319 static void
7320f_job_info(typval_T *argvars, typval_T *rettv)
7321{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007322 if (argvars[0].v_type != VAR_UNKNOWN)
7323 {
7324 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007325
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007326 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7327 job_info(job, rettv->vval.v_dict);
7328 }
7329 else if (rettv_list_alloc(rettv) == OK)
7330 job_info_all(rettv->vval.v_list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007331}
7332
7333/*
7334 * "job_setoptions()" function
7335 */
7336 static void
7337f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7338{
7339 job_T *job = get_job_arg(&argvars[0]);
7340 jobopt_T opt;
7341
7342 if (job == NULL)
7343 return;
7344 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007345 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007346 job_set_options(job, &opt);
7347 free_job_options(&opt);
7348}
7349
7350/*
7351 * "job_start()" function
7352 */
7353 static void
7354f_job_start(typval_T *argvars, typval_T *rettv)
7355{
7356 rettv->v_type = VAR_JOB;
7357 if (check_restricted() || check_secure())
7358 return;
Bram Moolenaar493359e2018-06-12 20:25:52 +02007359 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007360}
7361
7362/*
7363 * "job_status()" function
7364 */
7365 static void
7366f_job_status(typval_T *argvars, typval_T *rettv)
7367{
7368 job_T *job = get_job_arg(&argvars[0]);
7369
7370 if (job != NULL)
7371 {
7372 rettv->v_type = VAR_STRING;
7373 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7374 }
7375}
7376
7377/*
7378 * "job_stop()" function
7379 */
7380 static void
7381f_job_stop(typval_T *argvars, typval_T *rettv)
7382{
7383 job_T *job = get_job_arg(&argvars[0]);
7384
7385 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007386 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007387}
7388#endif
7389
7390/*
7391 * "join()" function
7392 */
7393 static void
7394f_join(typval_T *argvars, typval_T *rettv)
7395{
7396 garray_T ga;
7397 char_u *sep;
7398
7399 if (argvars[0].v_type != VAR_LIST)
7400 {
7401 EMSG(_(e_listreq));
7402 return;
7403 }
7404 if (argvars[0].vval.v_list == NULL)
7405 return;
7406 if (argvars[1].v_type == VAR_UNKNOWN)
7407 sep = (char_u *)" ";
7408 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007409 sep = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007410
7411 rettv->v_type = VAR_STRING;
7412
7413 if (sep != NULL)
7414 {
7415 ga_init2(&ga, (int)sizeof(char), 80);
7416 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7417 ga_append(&ga, NUL);
7418 rettv->vval.v_string = (char_u *)ga.ga_data;
7419 }
7420 else
7421 rettv->vval.v_string = NULL;
7422}
7423
7424/*
7425 * "js_decode()" function
7426 */
7427 static void
7428f_js_decode(typval_T *argvars, typval_T *rettv)
7429{
7430 js_read_T reader;
7431
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007432 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007433 reader.js_fill = NULL;
7434 reader.js_used = 0;
7435 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
7436 EMSG(_(e_invarg));
7437}
7438
7439/*
7440 * "js_encode()" function
7441 */
7442 static void
7443f_js_encode(typval_T *argvars, typval_T *rettv)
7444{
7445 rettv->v_type = VAR_STRING;
7446 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7447}
7448
7449/*
7450 * "json_decode()" function
7451 */
7452 static void
7453f_json_decode(typval_T *argvars, typval_T *rettv)
7454{
7455 js_read_T reader;
7456
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007457 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007458 reader.js_fill = NULL;
7459 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007460 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007461}
7462
7463/*
7464 * "json_encode()" function
7465 */
7466 static void
7467f_json_encode(typval_T *argvars, typval_T *rettv)
7468{
7469 rettv->v_type = VAR_STRING;
7470 rettv->vval.v_string = json_encode(&argvars[0], 0);
7471}
7472
7473/*
7474 * "keys()" function
7475 */
7476 static void
7477f_keys(typval_T *argvars, typval_T *rettv)
7478{
7479 dict_list(argvars, rettv, 0);
7480}
7481
7482/*
7483 * "last_buffer_nr()" function.
7484 */
7485 static void
7486f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7487{
7488 int n = 0;
7489 buf_T *buf;
7490
Bram Moolenaar29323592016-07-24 22:04:11 +02007491 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007492 if (n < buf->b_fnum)
7493 n = buf->b_fnum;
7494
7495 rettv->vval.v_number = n;
7496}
7497
7498/*
7499 * "len()" function
7500 */
7501 static void
7502f_len(typval_T *argvars, typval_T *rettv)
7503{
7504 switch (argvars[0].v_type)
7505 {
7506 case VAR_STRING:
7507 case VAR_NUMBER:
7508 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007509 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007510 break;
7511 case VAR_LIST:
7512 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7513 break;
7514 case VAR_DICT:
7515 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7516 break;
7517 case VAR_UNKNOWN:
7518 case VAR_SPECIAL:
7519 case VAR_FLOAT:
7520 case VAR_FUNC:
7521 case VAR_PARTIAL:
7522 case VAR_JOB:
7523 case VAR_CHANNEL:
7524 EMSG(_("E701: Invalid type for len()"));
7525 break;
7526 }
7527}
7528
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007529 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007530libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007531{
7532#ifdef FEAT_LIBCALL
7533 char_u *string_in;
7534 char_u **string_result;
7535 int nr_result;
7536#endif
7537
7538 rettv->v_type = type;
7539 if (type != VAR_NUMBER)
7540 rettv->vval.v_string = NULL;
7541
7542 if (check_restricted() || check_secure())
7543 return;
7544
7545#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007546 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007547 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7548 {
7549 string_in = NULL;
7550 if (argvars[2].v_type == VAR_STRING)
7551 string_in = argvars[2].vval.v_string;
7552 if (type == VAR_NUMBER)
7553 string_result = NULL;
7554 else
7555 string_result = &rettv->vval.v_string;
7556 if (mch_libcall(argvars[0].vval.v_string,
7557 argvars[1].vval.v_string,
7558 string_in,
7559 argvars[2].vval.v_number,
7560 string_result,
7561 &nr_result) == OK
7562 && type == VAR_NUMBER)
7563 rettv->vval.v_number = nr_result;
7564 }
7565#endif
7566}
7567
7568/*
7569 * "libcall()" function
7570 */
7571 static void
7572f_libcall(typval_T *argvars, typval_T *rettv)
7573{
7574 libcall_common(argvars, rettv, VAR_STRING);
7575}
7576
7577/*
7578 * "libcallnr()" function
7579 */
7580 static void
7581f_libcallnr(typval_T *argvars, typval_T *rettv)
7582{
7583 libcall_common(argvars, rettv, VAR_NUMBER);
7584}
7585
7586/*
7587 * "line(string)" function
7588 */
7589 static void
7590f_line(typval_T *argvars, typval_T *rettv)
7591{
7592 linenr_T lnum = 0;
7593 pos_T *fp;
7594 int fnum;
7595
7596 fp = var2fpos(&argvars[0], TRUE, &fnum);
7597 if (fp != NULL)
7598 lnum = fp->lnum;
7599 rettv->vval.v_number = lnum;
7600}
7601
7602/*
7603 * "line2byte(lnum)" function
7604 */
7605 static void
7606f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7607{
7608#ifndef FEAT_BYTEOFF
7609 rettv->vval.v_number = -1;
7610#else
7611 linenr_T lnum;
7612
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007613 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007614 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7615 rettv->vval.v_number = -1;
7616 else
7617 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7618 if (rettv->vval.v_number >= 0)
7619 ++rettv->vval.v_number;
7620#endif
7621}
7622
7623/*
7624 * "lispindent(lnum)" function
7625 */
7626 static void
7627f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7628{
7629#ifdef FEAT_LISP
7630 pos_T pos;
7631 linenr_T lnum;
7632
7633 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007634 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007635 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7636 {
7637 curwin->w_cursor.lnum = lnum;
7638 rettv->vval.v_number = get_lisp_indent();
7639 curwin->w_cursor = pos;
7640 }
7641 else
7642#endif
7643 rettv->vval.v_number = -1;
7644}
7645
7646/*
7647 * "localtime()" function
7648 */
7649 static void
7650f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7651{
7652 rettv->vval.v_number = (varnumber_T)time(NULL);
7653}
7654
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007655 static void
7656get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7657{
7658 char_u *keys;
7659 char_u *which;
7660 char_u buf[NUMBUFLEN];
7661 char_u *keys_buf = NULL;
7662 char_u *rhs;
7663 int mode;
7664 int abbr = FALSE;
7665 int get_dict = FALSE;
7666 mapblock_T *mp;
7667 int buffer_local;
7668
7669 /* return empty string for failure */
7670 rettv->v_type = VAR_STRING;
7671 rettv->vval.v_string = NULL;
7672
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007673 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007674 if (*keys == NUL)
7675 return;
7676
7677 if (argvars[1].v_type != VAR_UNKNOWN)
7678 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007679 which = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007680 if (argvars[2].v_type != VAR_UNKNOWN)
7681 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007682 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007683 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007684 get_dict = (int)tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007685 }
7686 }
7687 else
7688 which = (char_u *)"";
7689 if (which == NULL)
7690 return;
7691
7692 mode = get_map_mode(&which, 0);
7693
7694 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7695 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7696 vim_free(keys_buf);
7697
7698 if (!get_dict)
7699 {
7700 /* Return a string. */
7701 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02007702 {
7703 if (*rhs == NUL)
7704 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
7705 else
7706 rettv->vval.v_string = str2special_save(rhs, FALSE);
7707 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007708
7709 }
7710 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7711 {
7712 /* Return a dictionary. */
7713 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7714 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7715 dict_T *dict = rettv->vval.v_dict;
7716
Bram Moolenaare0be1672018-07-08 16:50:37 +02007717 dict_add_string(dict, "lhs", lhs);
7718 dict_add_string(dict, "rhs", mp->m_orig_str);
7719 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
7720 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
7721 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007722 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
7723 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02007724 dict_add_number(dict, "buffer", (long)buffer_local);
7725 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
7726 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007727
7728 vim_free(lhs);
7729 vim_free(mapmode);
7730 }
7731}
7732
7733#ifdef FEAT_FLOAT
7734/*
7735 * "log()" function
7736 */
7737 static void
7738f_log(typval_T *argvars, typval_T *rettv)
7739{
7740 float_T f = 0.0;
7741
7742 rettv->v_type = VAR_FLOAT;
7743 if (get_float_arg(argvars, &f) == OK)
7744 rettv->vval.v_float = log(f);
7745 else
7746 rettv->vval.v_float = 0.0;
7747}
7748
7749/*
7750 * "log10()" function
7751 */
7752 static void
7753f_log10(typval_T *argvars, typval_T *rettv)
7754{
7755 float_T f = 0.0;
7756
7757 rettv->v_type = VAR_FLOAT;
7758 if (get_float_arg(argvars, &f) == OK)
7759 rettv->vval.v_float = log10(f);
7760 else
7761 rettv->vval.v_float = 0.0;
7762}
7763#endif
7764
7765#ifdef FEAT_LUA
7766/*
7767 * "luaeval()" function
7768 */
7769 static void
7770f_luaeval(typval_T *argvars, typval_T *rettv)
7771{
7772 char_u *str;
7773 char_u buf[NUMBUFLEN];
7774
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007775 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007776 do_luaeval(str, argvars + 1, rettv);
7777}
7778#endif
7779
7780/*
7781 * "map()" function
7782 */
7783 static void
7784f_map(typval_T *argvars, typval_T *rettv)
7785{
7786 filter_map(argvars, rettv, TRUE);
7787}
7788
7789/*
7790 * "maparg()" function
7791 */
7792 static void
7793f_maparg(typval_T *argvars, typval_T *rettv)
7794{
7795 get_maparg(argvars, rettv, TRUE);
7796}
7797
7798/*
7799 * "mapcheck()" function
7800 */
7801 static void
7802f_mapcheck(typval_T *argvars, typval_T *rettv)
7803{
7804 get_maparg(argvars, rettv, FALSE);
7805}
7806
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007807typedef enum
7808{
7809 MATCH_END, /* matchend() */
7810 MATCH_MATCH, /* match() */
7811 MATCH_STR, /* matchstr() */
7812 MATCH_LIST, /* matchlist() */
7813 MATCH_POS /* matchstrpos() */
7814} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007815
7816 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007817find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007818{
7819 char_u *str = NULL;
7820 long len = 0;
7821 char_u *expr = NULL;
7822 char_u *pat;
7823 regmatch_T regmatch;
7824 char_u patbuf[NUMBUFLEN];
7825 char_u strbuf[NUMBUFLEN];
7826 char_u *save_cpo;
7827 long start = 0;
7828 long nth = 1;
7829 colnr_T startcol = 0;
7830 int match = 0;
7831 list_T *l = NULL;
7832 listitem_T *li = NULL;
7833 long idx = 0;
7834 char_u *tofree = NULL;
7835
7836 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7837 save_cpo = p_cpo;
7838 p_cpo = (char_u *)"";
7839
7840 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007841 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007842 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007843 /* type MATCH_LIST: return empty list when there are no matches.
7844 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007845 if (rettv_list_alloc(rettv) == FAIL)
7846 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007847 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007848 && (list_append_string(rettv->vval.v_list,
7849 (char_u *)"", 0) == FAIL
7850 || list_append_number(rettv->vval.v_list,
7851 (varnumber_T)-1) == FAIL
7852 || list_append_number(rettv->vval.v_list,
7853 (varnumber_T)-1) == FAIL
7854 || list_append_number(rettv->vval.v_list,
7855 (varnumber_T)-1) == FAIL))
7856 {
7857 list_free(rettv->vval.v_list);
7858 rettv->vval.v_list = NULL;
7859 goto theend;
7860 }
7861 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007862 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007863 {
7864 rettv->v_type = VAR_STRING;
7865 rettv->vval.v_string = NULL;
7866 }
7867
7868 if (argvars[0].v_type == VAR_LIST)
7869 {
7870 if ((l = argvars[0].vval.v_list) == NULL)
7871 goto theend;
7872 li = l->lv_first;
7873 }
7874 else
7875 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007876 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007877 len = (long)STRLEN(str);
7878 }
7879
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007880 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007881 if (pat == NULL)
7882 goto theend;
7883
7884 if (argvars[2].v_type != VAR_UNKNOWN)
7885 {
7886 int error = FALSE;
7887
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007888 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007889 if (error)
7890 goto theend;
7891 if (l != NULL)
7892 {
7893 li = list_find(l, start);
7894 if (li == NULL)
7895 goto theend;
7896 idx = l->lv_idx; /* use the cached index */
7897 }
7898 else
7899 {
7900 if (start < 0)
7901 start = 0;
7902 if (start > len)
7903 goto theend;
7904 /* When "count" argument is there ignore matches before "start",
7905 * otherwise skip part of the string. Differs when pattern is "^"
7906 * or "\<". */
7907 if (argvars[3].v_type != VAR_UNKNOWN)
7908 startcol = start;
7909 else
7910 {
7911 str += start;
7912 len -= start;
7913 }
7914 }
7915
7916 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007917 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007918 if (error)
7919 goto theend;
7920 }
7921
7922 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7923 if (regmatch.regprog != NULL)
7924 {
7925 regmatch.rm_ic = p_ic;
7926
7927 for (;;)
7928 {
7929 if (l != NULL)
7930 {
7931 if (li == NULL)
7932 {
7933 match = FALSE;
7934 break;
7935 }
7936 vim_free(tofree);
7937 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
7938 if (str == NULL)
7939 break;
7940 }
7941
7942 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
7943
7944 if (match && --nth <= 0)
7945 break;
7946 if (l == NULL && !match)
7947 break;
7948
7949 /* Advance to just after the match. */
7950 if (l != NULL)
7951 {
7952 li = li->li_next;
7953 ++idx;
7954 }
7955 else
7956 {
7957#ifdef FEAT_MBYTE
7958 startcol = (colnr_T)(regmatch.startp[0]
7959 + (*mb_ptr2len)(regmatch.startp[0]) - str);
7960#else
7961 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
7962#endif
7963 if (startcol > (colnr_T)len
7964 || str + startcol <= regmatch.startp[0])
7965 {
7966 match = FALSE;
7967 break;
7968 }
7969 }
7970 }
7971
7972 if (match)
7973 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007974 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007975 {
7976 listitem_T *li1 = rettv->vval.v_list->lv_first;
7977 listitem_T *li2 = li1->li_next;
7978 listitem_T *li3 = li2->li_next;
7979 listitem_T *li4 = li3->li_next;
7980
7981 vim_free(li1->li_tv.vval.v_string);
7982 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
7983 (int)(regmatch.endp[0] - regmatch.startp[0]));
7984 li3->li_tv.vval.v_number =
7985 (varnumber_T)(regmatch.startp[0] - expr);
7986 li4->li_tv.vval.v_number =
7987 (varnumber_T)(regmatch.endp[0] - expr);
7988 if (l != NULL)
7989 li2->li_tv.vval.v_number = (varnumber_T)idx;
7990 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007991 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007992 {
7993 int i;
7994
7995 /* return list with matched string and submatches */
7996 for (i = 0; i < NSUBEXP; ++i)
7997 {
7998 if (regmatch.endp[i] == NULL)
7999 {
8000 if (list_append_string(rettv->vval.v_list,
8001 (char_u *)"", 0) == FAIL)
8002 break;
8003 }
8004 else if (list_append_string(rettv->vval.v_list,
8005 regmatch.startp[i],
8006 (int)(regmatch.endp[i] - regmatch.startp[i]))
8007 == FAIL)
8008 break;
8009 }
8010 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008011 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008012 {
8013 /* return matched string */
8014 if (l != NULL)
8015 copy_tv(&li->li_tv, rettv);
8016 else
8017 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
8018 (int)(regmatch.endp[0] - regmatch.startp[0]));
8019 }
8020 else if (l != NULL)
8021 rettv->vval.v_number = idx;
8022 else
8023 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008024 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008025 rettv->vval.v_number =
8026 (varnumber_T)(regmatch.startp[0] - str);
8027 else
8028 rettv->vval.v_number =
8029 (varnumber_T)(regmatch.endp[0] - str);
8030 rettv->vval.v_number += (varnumber_T)(str - expr);
8031 }
8032 }
8033 vim_regfree(regmatch.regprog);
8034 }
8035
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008036theend:
8037 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008038 /* matchstrpos() without a list: drop the second item. */
8039 listitem_remove(rettv->vval.v_list,
8040 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008041 vim_free(tofree);
8042 p_cpo = save_cpo;
8043}
8044
8045/*
8046 * "match()" function
8047 */
8048 static void
8049f_match(typval_T *argvars, typval_T *rettv)
8050{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008051 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008052}
8053
Bram Moolenaar95e51472018-07-28 16:55:56 +02008054#ifdef FEAT_SEARCH_EXTRA
8055 static int
8056matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
8057{
8058 dictitem_T *di;
8059
8060 if (tv->v_type != VAR_DICT)
8061 {
8062 EMSG(_(e_dictreq));
8063 return FAIL;
8064 }
8065
8066 if (dict_find(tv->vval.v_dict, (char_u *)"conceal", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01008067 *conceal_char = dict_get_string(tv->vval.v_dict,
Bram Moolenaar95e51472018-07-28 16:55:56 +02008068 (char_u *)"conceal", FALSE);
8069
8070 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
8071 {
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02008072 *win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008073 if (*win == NULL)
8074 {
8075 EMSG(_("E957: Invalid window number"));
8076 return FAIL;
8077 }
8078 }
8079
8080 return OK;
8081}
8082#endif
8083
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008084/*
8085 * "matchadd()" function
8086 */
8087 static void
8088f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8089{
8090#ifdef FEAT_SEARCH_EXTRA
8091 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008092 char_u *grp = tv_get_string_buf_chk(&argvars[0], buf); /* group */
8093 char_u *pat = tv_get_string_buf_chk(&argvars[1], buf); /* pattern */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008094 int prio = 10; /* default priority */
8095 int id = -1;
8096 int error = FALSE;
8097 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008098 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008099
8100 rettv->vval.v_number = -1;
8101
8102 if (grp == NULL || pat == NULL)
8103 return;
8104 if (argvars[2].v_type != VAR_UNKNOWN)
8105 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008106 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008107 if (argvars[3].v_type != VAR_UNKNOWN)
8108 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008109 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008110 if (argvars[4].v_type != VAR_UNKNOWN
8111 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8112 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008113 }
8114 }
8115 if (error == TRUE)
8116 return;
8117 if (id >= 1 && id <= 3)
8118 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02008119 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008120 return;
8121 }
8122
Bram Moolenaar95e51472018-07-28 16:55:56 +02008123 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008124 conceal_char);
8125#endif
8126}
8127
8128/*
8129 * "matchaddpos()" function
8130 */
8131 static void
8132f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8133{
8134#ifdef FEAT_SEARCH_EXTRA
8135 char_u buf[NUMBUFLEN];
8136 char_u *group;
8137 int prio = 10;
8138 int id = -1;
8139 int error = FALSE;
8140 list_T *l;
8141 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008142 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008143
8144 rettv->vval.v_number = -1;
8145
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008146 group = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008147 if (group == NULL)
8148 return;
8149
8150 if (argvars[1].v_type != VAR_LIST)
8151 {
8152 EMSG2(_(e_listarg), "matchaddpos()");
8153 return;
8154 }
8155 l = argvars[1].vval.v_list;
8156 if (l == NULL)
8157 return;
8158
8159 if (argvars[2].v_type != VAR_UNKNOWN)
8160 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008161 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008162 if (argvars[3].v_type != VAR_UNKNOWN)
8163 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008164 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008165
8166 if (argvars[4].v_type != VAR_UNKNOWN
8167 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8168 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008169 }
8170 }
8171 if (error == TRUE)
8172 return;
8173
8174 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8175 if (id == 1 || id == 2)
8176 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02008177 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008178 return;
8179 }
8180
Bram Moolenaar95e51472018-07-28 16:55:56 +02008181 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008182 conceal_char);
8183#endif
8184}
8185
8186/*
8187 * "matcharg()" function
8188 */
8189 static void
8190f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8191{
8192 if (rettv_list_alloc(rettv) == OK)
8193 {
8194#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008195 int id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008196 matchitem_T *m;
8197
8198 if (id >= 1 && id <= 3)
8199 {
8200 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8201 {
8202 list_append_string(rettv->vval.v_list,
8203 syn_id2name(m->hlg_id), -1);
8204 list_append_string(rettv->vval.v_list, m->pattern, -1);
8205 }
8206 else
8207 {
8208 list_append_string(rettv->vval.v_list, NULL, -1);
8209 list_append_string(rettv->vval.v_list, NULL, -1);
8210 }
8211 }
8212#endif
8213 }
8214}
8215
8216/*
8217 * "matchdelete()" function
8218 */
8219 static void
8220f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8221{
8222#ifdef FEAT_SEARCH_EXTRA
8223 rettv->vval.v_number = match_delete(curwin,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008224 (int)tv_get_number(&argvars[0]), TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008225#endif
8226}
8227
8228/*
8229 * "matchend()" function
8230 */
8231 static void
8232f_matchend(typval_T *argvars, typval_T *rettv)
8233{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008234 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008235}
8236
8237/*
8238 * "matchlist()" function
8239 */
8240 static void
8241f_matchlist(typval_T *argvars, typval_T *rettv)
8242{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008243 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008244}
8245
8246/*
8247 * "matchstr()" function
8248 */
8249 static void
8250f_matchstr(typval_T *argvars, typval_T *rettv)
8251{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008252 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008253}
8254
8255/*
8256 * "matchstrpos()" function
8257 */
8258 static void
8259f_matchstrpos(typval_T *argvars, typval_T *rettv)
8260{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008261 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008262}
8263
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008264 static void
8265max_min(typval_T *argvars, typval_T *rettv, int domax)
8266{
8267 varnumber_T n = 0;
8268 varnumber_T i;
8269 int error = FALSE;
8270
8271 if (argvars[0].v_type == VAR_LIST)
8272 {
8273 list_T *l;
8274 listitem_T *li;
8275
8276 l = argvars[0].vval.v_list;
8277 if (l != NULL)
8278 {
8279 li = l->lv_first;
8280 if (li != NULL)
8281 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008282 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008283 for (;;)
8284 {
8285 li = li->li_next;
8286 if (li == NULL)
8287 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008288 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008289 if (domax ? i > n : i < n)
8290 n = i;
8291 }
8292 }
8293 }
8294 }
8295 else if (argvars[0].v_type == VAR_DICT)
8296 {
8297 dict_T *d;
8298 int first = TRUE;
8299 hashitem_T *hi;
8300 int todo;
8301
8302 d = argvars[0].vval.v_dict;
8303 if (d != NULL)
8304 {
8305 todo = (int)d->dv_hashtab.ht_used;
8306 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8307 {
8308 if (!HASHITEM_EMPTY(hi))
8309 {
8310 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008311 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008312 if (first)
8313 {
8314 n = i;
8315 first = FALSE;
8316 }
8317 else if (domax ? i > n : i < n)
8318 n = i;
8319 }
8320 }
8321 }
8322 }
8323 else
Bram Moolenaar26b84332016-09-04 21:42:36 +02008324 EMSG2(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008325 rettv->vval.v_number = error ? 0 : n;
8326}
8327
8328/*
8329 * "max()" function
8330 */
8331 static void
8332f_max(typval_T *argvars, typval_T *rettv)
8333{
8334 max_min(argvars, rettv, TRUE);
8335}
8336
8337/*
8338 * "min()" function
8339 */
8340 static void
8341f_min(typval_T *argvars, typval_T *rettv)
8342{
8343 max_min(argvars, rettv, FALSE);
8344}
8345
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008346/*
8347 * Create the directory in which "dir" is located, and higher levels when
8348 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008349 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008350 */
8351 static int
8352mkdir_recurse(char_u *dir, int prot)
8353{
8354 char_u *p;
8355 char_u *updir;
8356 int r = FAIL;
8357
8358 /* Get end of directory name in "dir".
8359 * We're done when it's "/" or "c:/". */
8360 p = gettail_sep(dir);
8361 if (p <= get_past_head(dir))
8362 return OK;
8363
8364 /* If the directory exists we're done. Otherwise: create it.*/
8365 updir = vim_strnsave(dir, (int)(p - dir));
8366 if (updir == NULL)
8367 return FAIL;
8368 if (mch_isdir(updir))
8369 r = OK;
8370 else if (mkdir_recurse(updir, prot) == OK)
8371 r = vim_mkdir_emsg(updir, prot);
8372 vim_free(updir);
8373 return r;
8374}
8375
8376#ifdef vim_mkdir
8377/*
8378 * "mkdir()" function
8379 */
8380 static void
8381f_mkdir(typval_T *argvars, typval_T *rettv)
8382{
8383 char_u *dir;
8384 char_u buf[NUMBUFLEN];
8385 int prot = 0755;
8386
8387 rettv->vval.v_number = FAIL;
8388 if (check_restricted() || check_secure())
8389 return;
8390
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008391 dir = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008392 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008393 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008394
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008395 if (*gettail(dir) == NUL)
8396 /* remove trailing slashes */
8397 *gettail_sep(dir) = NUL;
8398
8399 if (argvars[1].v_type != VAR_UNKNOWN)
8400 {
8401 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008402 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008403 prot = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008404 if (prot == -1)
8405 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008406 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008407 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008408 {
8409 if (mch_isdir(dir))
8410 {
8411 /* With the "p" flag it's OK if the dir already exists. */
8412 rettv->vval.v_number = OK;
8413 return;
8414 }
8415 mkdir_recurse(dir, prot);
8416 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008417 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008418 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008419}
8420#endif
8421
8422/*
8423 * "mode()" function
8424 */
8425 static void
8426f_mode(typval_T *argvars, typval_T *rettv)
8427{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008428 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008429
Bram Moolenaar612cc382018-07-29 15:34:26 +02008430 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008431
8432 if (time_for_testing == 93784)
8433 {
8434 /* Testing the two-character code. */
8435 buf[0] = 'x';
8436 buf[1] = '!';
8437 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008438#ifdef FEAT_TERMINAL
8439 else if (term_use_loop())
8440 buf[0] = 't';
8441#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008442 else if (VIsual_active)
8443 {
8444 if (VIsual_select)
8445 buf[0] = VIsual_mode + 's' - 'v';
8446 else
8447 buf[0] = VIsual_mode;
8448 }
8449 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8450 || State == CONFIRM)
8451 {
8452 buf[0] = 'r';
8453 if (State == ASKMORE)
8454 buf[1] = 'm';
8455 else if (State == CONFIRM)
8456 buf[1] = '?';
8457 }
8458 else if (State == EXTERNCMD)
8459 buf[0] = '!';
8460 else if (State & INSERT)
8461 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008462 if (State & VREPLACE_FLAG)
8463 {
8464 buf[0] = 'R';
8465 buf[1] = 'v';
8466 }
8467 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008468 {
8469 if (State & REPLACE_FLAG)
8470 buf[0] = 'R';
8471 else
8472 buf[0] = 'i';
8473#ifdef FEAT_INS_EXPAND
8474 if (ins_compl_active())
8475 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008476 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008477 buf[1] = 'x';
8478#endif
8479 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008480 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008481 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008482 {
8483 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008484 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008485 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008486 else if (exmode_active == EXMODE_NORMAL)
8487 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008488 }
8489 else
8490 {
8491 buf[0] = 'n';
8492 if (finish_op)
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008493 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008494 buf[1] = 'o';
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008495 // to be able to detect force-linewise/blockwise/characterwise operations
8496 buf[2] = motion_force;
8497 }
Bram Moolenaar612cc382018-07-29 15:34:26 +02008498 else if (restart_edit == 'I' || restart_edit == 'R'
8499 || restart_edit == 'V')
8500 {
8501 buf[1] = 'i';
8502 buf[2] = restart_edit;
8503 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008504 }
8505
8506 /* Clear out the minor mode when the argument is not a non-zero number or
8507 * non-empty string. */
8508 if (!non_zero_arg(&argvars[0]))
8509 buf[1] = NUL;
8510
8511 rettv->vval.v_string = vim_strsave(buf);
8512 rettv->v_type = VAR_STRING;
8513}
8514
8515#if defined(FEAT_MZSCHEME) || defined(PROTO)
8516/*
8517 * "mzeval()" function
8518 */
8519 static void
8520f_mzeval(typval_T *argvars, typval_T *rettv)
8521{
8522 char_u *str;
8523 char_u buf[NUMBUFLEN];
8524
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008525 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008526 do_mzeval(str, rettv);
8527}
8528
8529 void
8530mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8531{
8532 typval_T argvars[3];
8533
8534 argvars[0].v_type = VAR_STRING;
8535 argvars[0].vval.v_string = name;
8536 copy_tv(args, &argvars[1]);
8537 argvars[2].v_type = VAR_UNKNOWN;
8538 f_call(argvars, rettv);
8539 clear_tv(&argvars[1]);
8540}
8541#endif
8542
8543/*
8544 * "nextnonblank()" function
8545 */
8546 static void
8547f_nextnonblank(typval_T *argvars, typval_T *rettv)
8548{
8549 linenr_T lnum;
8550
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008551 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008552 {
8553 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8554 {
8555 lnum = 0;
8556 break;
8557 }
8558 if (*skipwhite(ml_get(lnum)) != NUL)
8559 break;
8560 }
8561 rettv->vval.v_number = lnum;
8562}
8563
8564/*
8565 * "nr2char()" function
8566 */
8567 static void
8568f_nr2char(typval_T *argvars, typval_T *rettv)
8569{
8570 char_u buf[NUMBUFLEN];
8571
8572#ifdef FEAT_MBYTE
8573 if (has_mbyte)
8574 {
8575 int utf8 = 0;
8576
8577 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008578 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008579 if (utf8)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008580 buf[(*utf_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008581 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008582 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008583 }
8584 else
8585#endif
8586 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008587 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008588 buf[1] = NUL;
8589 }
8590 rettv->v_type = VAR_STRING;
8591 rettv->vval.v_string = vim_strsave(buf);
8592}
8593
8594/*
8595 * "or(expr, expr)" function
8596 */
8597 static void
8598f_or(typval_T *argvars, typval_T *rettv)
8599{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008600 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
8601 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008602}
8603
8604/*
8605 * "pathshorten()" function
8606 */
8607 static void
8608f_pathshorten(typval_T *argvars, typval_T *rettv)
8609{
8610 char_u *p;
8611
8612 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008613 p = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008614 if (p == NULL)
8615 rettv->vval.v_string = NULL;
8616 else
8617 {
8618 p = vim_strsave(p);
8619 rettv->vval.v_string = p;
8620 if (p != NULL)
8621 shorten_dir(p);
8622 }
8623}
8624
8625#ifdef FEAT_PERL
8626/*
8627 * "perleval()" function
8628 */
8629 static void
8630f_perleval(typval_T *argvars, typval_T *rettv)
8631{
8632 char_u *str;
8633 char_u buf[NUMBUFLEN];
8634
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008635 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008636 do_perleval(str, rettv);
8637}
8638#endif
8639
8640#ifdef FEAT_FLOAT
8641/*
8642 * "pow()" function
8643 */
8644 static void
8645f_pow(typval_T *argvars, typval_T *rettv)
8646{
8647 float_T fx = 0.0, fy = 0.0;
8648
8649 rettv->v_type = VAR_FLOAT;
8650 if (get_float_arg(argvars, &fx) == OK
8651 && get_float_arg(&argvars[1], &fy) == OK)
8652 rettv->vval.v_float = pow(fx, fy);
8653 else
8654 rettv->vval.v_float = 0.0;
8655}
8656#endif
8657
8658/*
8659 * "prevnonblank()" function
8660 */
8661 static void
8662f_prevnonblank(typval_T *argvars, typval_T *rettv)
8663{
8664 linenr_T lnum;
8665
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008666 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008667 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8668 lnum = 0;
8669 else
8670 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8671 --lnum;
8672 rettv->vval.v_number = lnum;
8673}
8674
8675/* This dummy va_list is here because:
8676 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8677 * - locally in the function results in a "used before set" warning
8678 * - using va_start() to initialize it gives "function with fixed args" error */
8679static va_list ap;
8680
8681/*
8682 * "printf()" function
8683 */
8684 static void
8685f_printf(typval_T *argvars, typval_T *rettv)
8686{
8687 char_u buf[NUMBUFLEN];
8688 int len;
8689 char_u *s;
8690 int saved_did_emsg = did_emsg;
8691 char *fmt;
8692
8693 rettv->v_type = VAR_STRING;
8694 rettv->vval.v_string = NULL;
8695
8696 /* Get the required length, allocate the buffer and do it for real. */
8697 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008698 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008699 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008700 if (!did_emsg)
8701 {
8702 s = alloc(len + 1);
8703 if (s != NULL)
8704 {
8705 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008706 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8707 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008708 }
8709 }
8710 did_emsg |= saved_did_emsg;
8711}
8712
Bram Moolenaarf2732452018-06-03 14:47:35 +02008713#ifdef FEAT_JOB_CHANNEL
8714/*
8715 * "prompt_setcallback({buffer}, {callback})" function
8716 */
8717 static void
8718f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
8719{
8720 buf_T *buf;
8721 char_u *callback;
8722 partial_T *partial;
8723
8724 if (check_secure())
8725 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01008726 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008727 if (buf == NULL)
8728 return;
8729
8730 callback = get_callback(&argvars[1], &partial);
8731 if (callback == NULL)
8732 return;
8733
8734 free_callback(buf->b_prompt_callback, buf->b_prompt_partial);
8735 if (partial == NULL)
8736 buf->b_prompt_callback = vim_strsave(callback);
8737 else
8738 /* pointer into the partial */
8739 buf->b_prompt_callback = callback;
8740 buf->b_prompt_partial = partial;
8741}
8742
8743/*
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02008744 * "prompt_setinterrupt({buffer}, {callback})" function
8745 */
8746 static void
8747f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
8748{
8749 buf_T *buf;
8750 char_u *callback;
8751 partial_T *partial;
8752
8753 if (check_secure())
8754 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01008755 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02008756 if (buf == NULL)
8757 return;
8758
8759 callback = get_callback(&argvars[1], &partial);
8760 if (callback == NULL)
8761 return;
8762
8763 free_callback(buf->b_prompt_interrupt, buf->b_prompt_int_partial);
8764 if (partial == NULL)
8765 buf->b_prompt_interrupt = vim_strsave(callback);
8766 else
8767 /* pointer into the partial */
8768 buf->b_prompt_interrupt = callback;
8769 buf->b_prompt_int_partial = partial;
8770}
8771
8772/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02008773 * "prompt_setprompt({buffer}, {text})" function
8774 */
8775 static void
8776f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
8777{
8778 buf_T *buf;
8779 char_u *text;
8780
8781 if (check_secure())
8782 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01008783 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008784 if (buf == NULL)
8785 return;
8786
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008787 text = tv_get_string(&argvars[1]);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008788 vim_free(buf->b_prompt_text);
8789 buf->b_prompt_text = vim_strsave(text);
8790}
8791#endif
8792
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008793/*
8794 * "pumvisible()" function
8795 */
8796 static void
8797f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8798{
8799#ifdef FEAT_INS_EXPAND
8800 if (pum_visible())
8801 rettv->vval.v_number = 1;
8802#endif
8803}
8804
8805#ifdef FEAT_PYTHON3
8806/*
8807 * "py3eval()" function
8808 */
8809 static void
8810f_py3eval(typval_T *argvars, typval_T *rettv)
8811{
8812 char_u *str;
8813 char_u buf[NUMBUFLEN];
8814
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008815 if (p_pyx == 0)
8816 p_pyx = 3;
8817
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008818 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008819 do_py3eval(str, rettv);
8820}
8821#endif
8822
8823#ifdef FEAT_PYTHON
8824/*
8825 * "pyeval()" function
8826 */
8827 static void
8828f_pyeval(typval_T *argvars, typval_T *rettv)
8829{
8830 char_u *str;
8831 char_u buf[NUMBUFLEN];
8832
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008833 if (p_pyx == 0)
8834 p_pyx = 2;
8835
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008836 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008837 do_pyeval(str, rettv);
8838}
8839#endif
8840
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008841#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8842/*
8843 * "pyxeval()" function
8844 */
8845 static void
8846f_pyxeval(typval_T *argvars, typval_T *rettv)
8847{
8848# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8849 init_pyxversion();
8850 if (p_pyx == 2)
8851 f_pyeval(argvars, rettv);
8852 else
8853 f_py3eval(argvars, rettv);
8854# elif defined(FEAT_PYTHON)
8855 f_pyeval(argvars, rettv);
8856# elif defined(FEAT_PYTHON3)
8857 f_py3eval(argvars, rettv);
8858# endif
8859}
8860#endif
8861
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008862/*
8863 * "range()" function
8864 */
8865 static void
8866f_range(typval_T *argvars, typval_T *rettv)
8867{
8868 varnumber_T start;
8869 varnumber_T end;
8870 varnumber_T stride = 1;
8871 varnumber_T i;
8872 int error = FALSE;
8873
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008874 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008875 if (argvars[1].v_type == VAR_UNKNOWN)
8876 {
8877 end = start - 1;
8878 start = 0;
8879 }
8880 else
8881 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008882 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008883 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008884 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008885 }
8886
8887 if (error)
8888 return; /* type error; errmsg already given */
8889 if (stride == 0)
8890 EMSG(_("E726: Stride is zero"));
8891 else if (stride > 0 ? end + 1 < start : end - 1 > start)
8892 EMSG(_("E727: Start past end"));
8893 else
8894 {
8895 if (rettv_list_alloc(rettv) == OK)
8896 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8897 if (list_append_number(rettv->vval.v_list,
8898 (varnumber_T)i) == FAIL)
8899 break;
8900 }
8901}
8902
8903/*
8904 * "readfile()" function
8905 */
8906 static void
8907f_readfile(typval_T *argvars, typval_T *rettv)
8908{
8909 int binary = FALSE;
8910 int failed = FALSE;
8911 char_u *fname;
8912 FILE *fd;
8913 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
8914 int io_size = sizeof(buf);
8915 int readlen; /* size of last fread() */
8916 char_u *prev = NULL; /* previously read bytes, if any */
8917 long prevlen = 0; /* length of data in prev */
8918 long prevsize = 0; /* size of prev buffer */
8919 long maxline = MAXLNUM;
8920 long cnt = 0;
8921 char_u *p; /* position in buf */
8922 char_u *start; /* start of current line */
8923
8924 if (argvars[1].v_type != VAR_UNKNOWN)
8925 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008926 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008927 binary = TRUE;
8928 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008929 maxline = (long)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008930 }
8931
8932 if (rettv_list_alloc(rettv) == FAIL)
8933 return;
8934
8935 /* Always open the file in binary mode, library functions have a mind of
8936 * their own about CR-LF conversion. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008937 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008938 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
8939 {
8940 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
8941 return;
8942 }
8943
8944 while (cnt < maxline || maxline < 0)
8945 {
8946 readlen = (int)fread(buf, 1, io_size, fd);
8947
8948 /* This for loop processes what was read, but is also entered at end
8949 * of file so that either:
8950 * - an incomplete line gets written
8951 * - a "binary" file gets an empty line at the end if it ends in a
8952 * newline. */
8953 for (p = buf, start = buf;
8954 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
8955 ++p)
8956 {
8957 if (*p == '\n' || readlen <= 0)
8958 {
8959 listitem_T *li;
8960 char_u *s = NULL;
8961 long_u len = p - start;
8962
8963 /* Finished a line. Remove CRs before NL. */
8964 if (readlen > 0 && !binary)
8965 {
8966 while (len > 0 && start[len - 1] == '\r')
8967 --len;
8968 /* removal may cross back to the "prev" string */
8969 if (len == 0)
8970 while (prevlen > 0 && prev[prevlen - 1] == '\r')
8971 --prevlen;
8972 }
8973 if (prevlen == 0)
8974 s = vim_strnsave(start, (int)len);
8975 else
8976 {
8977 /* Change "prev" buffer to be the right size. This way
8978 * the bytes are only copied once, and very long lines are
8979 * allocated only once. */
8980 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
8981 {
8982 mch_memmove(s + prevlen, start, len);
8983 s[prevlen + len] = NUL;
8984 prev = NULL; /* the list will own the string */
8985 prevlen = prevsize = 0;
8986 }
8987 }
8988 if (s == NULL)
8989 {
8990 do_outofmem_msg((long_u) prevlen + len + 1);
8991 failed = TRUE;
8992 break;
8993 }
8994
8995 if ((li = listitem_alloc()) == NULL)
8996 {
8997 vim_free(s);
8998 failed = TRUE;
8999 break;
9000 }
9001 li->li_tv.v_type = VAR_STRING;
9002 li->li_tv.v_lock = 0;
9003 li->li_tv.vval.v_string = s;
9004 list_append(rettv->vval.v_list, li);
9005
9006 start = p + 1; /* step over newline */
9007 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
9008 break;
9009 }
9010 else if (*p == NUL)
9011 *p = '\n';
9012#ifdef FEAT_MBYTE
9013 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
9014 * when finding the BF and check the previous two bytes. */
9015 else if (*p == 0xbf && enc_utf8 && !binary)
9016 {
9017 /* Find the two bytes before the 0xbf. If p is at buf, or buf
9018 * + 1, these may be in the "prev" string. */
9019 char_u back1 = p >= buf + 1 ? p[-1]
9020 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
9021 char_u back2 = p >= buf + 2 ? p[-2]
9022 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
9023 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
9024
9025 if (back2 == 0xef && back1 == 0xbb)
9026 {
9027 char_u *dest = p - 2;
9028
9029 /* Usually a BOM is at the beginning of a file, and so at
9030 * the beginning of a line; then we can just step over it.
9031 */
9032 if (start == dest)
9033 start = p + 1;
9034 else
9035 {
9036 /* have to shuffle buf to close gap */
9037 int adjust_prevlen = 0;
9038
9039 if (dest < buf)
9040 {
9041 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
9042 dest = buf;
9043 }
9044 if (readlen > p - buf + 1)
9045 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
9046 readlen -= 3 - adjust_prevlen;
9047 prevlen -= adjust_prevlen;
9048 p = dest - 1;
9049 }
9050 }
9051 }
9052#endif
9053 } /* for */
9054
9055 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
9056 break;
9057 if (start < p)
9058 {
9059 /* There's part of a line in buf, store it in "prev". */
9060 if (p - start + prevlen >= prevsize)
9061 {
9062 /* need bigger "prev" buffer */
9063 char_u *newprev;
9064
9065 /* A common use case is ordinary text files and "prev" gets a
9066 * fragment of a line, so the first allocation is made
9067 * small, to avoid repeatedly 'allocing' large and
9068 * 'reallocing' small. */
9069 if (prevsize == 0)
9070 prevsize = (long)(p - start);
9071 else
9072 {
9073 long grow50pc = (prevsize * 3) / 2;
9074 long growmin = (long)((p - start) * 2 + prevlen);
9075 prevsize = grow50pc > growmin ? grow50pc : growmin;
9076 }
9077 newprev = prev == NULL ? alloc(prevsize)
9078 : vim_realloc(prev, prevsize);
9079 if (newprev == NULL)
9080 {
9081 do_outofmem_msg((long_u)prevsize);
9082 failed = TRUE;
9083 break;
9084 }
9085 prev = newprev;
9086 }
9087 /* Add the line part to end of "prev". */
9088 mch_memmove(prev + prevlen, start, p - start);
9089 prevlen += (long)(p - start);
9090 }
9091 } /* while */
9092
9093 /*
9094 * For a negative line count use only the lines at the end of the file,
9095 * free the rest.
9096 */
9097 if (!failed && maxline < 0)
9098 while (cnt > -maxline)
9099 {
9100 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
9101 --cnt;
9102 }
9103
9104 if (failed)
9105 {
9106 list_free(rettv->vval.v_list);
9107 /* readfile doc says an empty list is returned on error */
9108 rettv->vval.v_list = list_alloc();
9109 }
9110
9111 vim_free(prev);
9112 fclose(fd);
9113}
9114
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009115 static void
9116return_register(int regname, typval_T *rettv)
9117{
9118 char_u buf[2] = {0, 0};
9119
9120 buf[0] = (char_u)regname;
9121 rettv->v_type = VAR_STRING;
9122 rettv->vval.v_string = vim_strsave(buf);
9123}
9124
9125/*
9126 * "reg_executing()" function
9127 */
9128 static void
9129f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9130{
9131 return_register(reg_executing, rettv);
9132}
9133
9134/*
9135 * "reg_recording()" function
9136 */
9137 static void
9138f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9139{
9140 return_register(reg_recording, rettv);
9141}
9142
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009143#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009144/*
9145 * Convert a List to proftime_T.
9146 * Return FAIL when there is something wrong.
9147 */
9148 static int
9149list2proftime(typval_T *arg, proftime_T *tm)
9150{
9151 long n1, n2;
9152 int error = FALSE;
9153
9154 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9155 || arg->vval.v_list->lv_len != 2)
9156 return FAIL;
9157 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9158 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
9159# ifdef WIN3264
9160 tm->HighPart = n1;
9161 tm->LowPart = n2;
9162# else
9163 tm->tv_sec = n1;
9164 tm->tv_usec = n2;
9165# endif
9166 return error ? FAIL : OK;
9167}
9168#endif /* FEAT_RELTIME */
9169
9170/*
9171 * "reltime()" function
9172 */
9173 static void
9174f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9175{
9176#ifdef FEAT_RELTIME
9177 proftime_T res;
9178 proftime_T start;
9179
9180 if (argvars[0].v_type == VAR_UNKNOWN)
9181 {
9182 /* No arguments: get current time. */
9183 profile_start(&res);
9184 }
9185 else if (argvars[1].v_type == VAR_UNKNOWN)
9186 {
9187 if (list2proftime(&argvars[0], &res) == FAIL)
9188 return;
9189 profile_end(&res);
9190 }
9191 else
9192 {
9193 /* Two arguments: compute the difference. */
9194 if (list2proftime(&argvars[0], &start) == FAIL
9195 || list2proftime(&argvars[1], &res) == FAIL)
9196 return;
9197 profile_sub(&res, &start);
9198 }
9199
9200 if (rettv_list_alloc(rettv) == OK)
9201 {
9202 long n1, n2;
9203
9204# ifdef WIN3264
9205 n1 = res.HighPart;
9206 n2 = res.LowPart;
9207# else
9208 n1 = res.tv_sec;
9209 n2 = res.tv_usec;
9210# endif
9211 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9212 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9213 }
9214#endif
9215}
9216
9217#ifdef FEAT_FLOAT
9218/*
9219 * "reltimefloat()" function
9220 */
9221 static void
9222f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9223{
9224# ifdef FEAT_RELTIME
9225 proftime_T tm;
9226# endif
9227
9228 rettv->v_type = VAR_FLOAT;
9229 rettv->vval.v_float = 0;
9230# ifdef FEAT_RELTIME
9231 if (list2proftime(&argvars[0], &tm) == OK)
9232 rettv->vval.v_float = profile_float(&tm);
9233# endif
9234}
9235#endif
9236
9237/*
9238 * "reltimestr()" function
9239 */
9240 static void
9241f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9242{
9243#ifdef FEAT_RELTIME
9244 proftime_T tm;
9245#endif
9246
9247 rettv->v_type = VAR_STRING;
9248 rettv->vval.v_string = NULL;
9249#ifdef FEAT_RELTIME
9250 if (list2proftime(&argvars[0], &tm) == OK)
9251 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9252#endif
9253}
9254
9255#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009256 static void
9257make_connection(void)
9258{
9259 if (X_DISPLAY == NULL
9260# ifdef FEAT_GUI
9261 && !gui.in_use
9262# endif
9263 )
9264 {
9265 x_force_connect = TRUE;
9266 setup_term_clip();
9267 x_force_connect = FALSE;
9268 }
9269}
9270
9271 static int
9272check_connection(void)
9273{
9274 make_connection();
9275 if (X_DISPLAY == NULL)
9276 {
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009277 EMSG(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009278 return FAIL;
9279 }
9280 return OK;
9281}
9282#endif
9283
9284#ifdef FEAT_CLIENTSERVER
9285 static void
9286remote_common(typval_T *argvars, typval_T *rettv, int expr)
9287{
9288 char_u *server_name;
9289 char_u *keys;
9290 char_u *r = NULL;
9291 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009292 int timeout = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009293# ifdef WIN32
9294 HWND w;
9295# else
9296 Window w;
9297# endif
9298
9299 if (check_restricted() || check_secure())
9300 return;
9301
9302# ifdef FEAT_X11
9303 if (check_connection() == FAIL)
9304 return;
9305# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009306 if (argvars[2].v_type != VAR_UNKNOWN
9307 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009308 timeout = tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009309
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009310 server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009311 if (server_name == NULL)
9312 return; /* type error; errmsg already given */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009313 keys = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009314# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009315 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009316# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009317 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9318 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009319# endif
9320 {
9321 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009322 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009323 EMSG(r); /* sending worked but evaluation failed */
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009324 vim_free(r);
9325 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009326 else
9327 EMSG2(_("E241: Unable to send to %s"), server_name);
9328 return;
9329 }
9330
9331 rettv->vval.v_string = r;
9332
9333 if (argvars[2].v_type != VAR_UNKNOWN)
9334 {
9335 dictitem_T v;
9336 char_u str[30];
9337 char_u *idvar;
9338
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009339 idvar = tv_get_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009340 if (idvar != NULL && *idvar != NUL)
9341 {
9342 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9343 v.di_tv.v_type = VAR_STRING;
9344 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009345 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009346 vim_free(v.di_tv.vval.v_string);
9347 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009348 }
9349}
9350#endif
9351
9352/*
9353 * "remote_expr()" function
9354 */
9355 static void
9356f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9357{
9358 rettv->v_type = VAR_STRING;
9359 rettv->vval.v_string = NULL;
9360#ifdef FEAT_CLIENTSERVER
9361 remote_common(argvars, rettv, TRUE);
9362#endif
9363}
9364
9365/*
9366 * "remote_foreground()" function
9367 */
9368 static void
9369f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9370{
9371#ifdef FEAT_CLIENTSERVER
9372# ifdef WIN32
9373 /* On Win32 it's done in this application. */
9374 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009375 char_u *server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009376
9377 if (server_name != NULL)
9378 serverForeground(server_name);
9379 }
9380# else
9381 /* Send a foreground() expression to the server. */
9382 argvars[1].v_type = VAR_STRING;
9383 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9384 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009385 rettv->v_type = VAR_STRING;
9386 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009387 remote_common(argvars, rettv, TRUE);
9388 vim_free(argvars[1].vval.v_string);
9389# endif
9390#endif
9391}
9392
9393 static void
9394f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
9395{
9396#ifdef FEAT_CLIENTSERVER
9397 dictitem_T v;
9398 char_u *s = NULL;
9399# ifdef WIN32
9400 long_u n = 0;
9401# endif
9402 char_u *serverid;
9403
9404 if (check_restricted() || check_secure())
9405 {
9406 rettv->vval.v_number = -1;
9407 return;
9408 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009409 serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009410 if (serverid == NULL)
9411 {
9412 rettv->vval.v_number = -1;
9413 return; /* type error; errmsg already given */
9414 }
9415# ifdef WIN32
9416 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
9417 if (n == 0)
9418 rettv->vval.v_number = -1;
9419 else
9420 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009421 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009422 rettv->vval.v_number = (s != NULL);
9423 }
9424# else
9425 if (check_connection() == FAIL)
9426 return;
9427
9428 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
9429 serverStrToWin(serverid), &s);
9430# endif
9431
9432 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
9433 {
9434 char_u *retvar;
9435
9436 v.di_tv.v_type = VAR_STRING;
9437 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009438 retvar = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009439 if (retvar != NULL)
9440 set_var(retvar, &v.di_tv, FALSE);
9441 vim_free(v.di_tv.vval.v_string);
9442 }
9443#else
9444 rettv->vval.v_number = -1;
9445#endif
9446}
9447
9448 static void
9449f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
9450{
9451 char_u *r = NULL;
9452
9453#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009454 char_u *serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009455
9456 if (serverid != NULL && !check_restricted() && !check_secure())
9457 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009458 int timeout = 0;
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009459# ifdef WIN32
9460 /* The server's HWND is encoded in the 'id' parameter */
9461 long_u n = 0;
9462# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009463
9464 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009465 timeout = tv_get_number(&argvars[1]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009466
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009467# ifdef WIN32
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009468 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
9469 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009470 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009471 if (r == NULL)
9472# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009473 if (check_connection() == FAIL
9474 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
9475 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009476# endif
9477 EMSG(_("E277: Unable to read a server reply"));
9478 }
9479#endif
9480 rettv->v_type = VAR_STRING;
9481 rettv->vval.v_string = r;
9482}
9483
9484/*
9485 * "remote_send()" function
9486 */
9487 static void
9488f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9489{
9490 rettv->v_type = VAR_STRING;
9491 rettv->vval.v_string = NULL;
9492#ifdef FEAT_CLIENTSERVER
9493 remote_common(argvars, rettv, FALSE);
9494#endif
9495}
9496
9497/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009498 * "remote_startserver()" function
9499 */
9500 static void
9501f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9502{
9503#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009504 char_u *server = tv_get_string_chk(&argvars[0]);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009505
9506 if (server == NULL)
9507 return; /* type error; errmsg already given */
9508 if (serverName != NULL)
9509 EMSG(_("E941: already started a server"));
9510 else
9511 {
9512# ifdef FEAT_X11
9513 if (check_connection() == OK)
9514 serverRegisterName(X_DISPLAY, server);
9515# else
9516 serverSetName(server);
9517# endif
9518 }
9519#else
9520 EMSG(_("E942: +clientserver feature not available"));
9521#endif
9522}
9523
9524/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009525 * "remove()" function
9526 */
9527 static void
9528f_remove(typval_T *argvars, typval_T *rettv)
9529{
9530 list_T *l;
9531 listitem_T *item, *item2;
9532 listitem_T *li;
9533 long idx;
9534 long end;
9535 char_u *key;
9536 dict_T *d;
9537 dictitem_T *di;
9538 char_u *arg_errmsg = (char_u *)N_("remove() argument");
9539
9540 if (argvars[0].v_type == VAR_DICT)
9541 {
9542 if (argvars[2].v_type != VAR_UNKNOWN)
9543 EMSG2(_(e_toomanyarg), "remove()");
9544 else if ((d = argvars[0].vval.v_dict) != NULL
9545 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
9546 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009547 key = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009548 if (key != NULL)
9549 {
9550 di = dict_find(d, key, -1);
9551 if (di == NULL)
9552 EMSG2(_(e_dictkey), key);
9553 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
9554 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
9555 {
9556 *rettv = di->di_tv;
9557 init_tv(&di->di_tv);
9558 dictitem_remove(d, di);
9559 }
9560 }
9561 }
9562 }
9563 else if (argvars[0].v_type != VAR_LIST)
9564 EMSG2(_(e_listdictarg), "remove()");
9565 else if ((l = argvars[0].vval.v_list) != NULL
9566 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
9567 {
9568 int error = FALSE;
9569
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009570 idx = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009571 if (error)
9572 ; /* type error: do nothing, errmsg already given */
9573 else if ((item = list_find(l, idx)) == NULL)
9574 EMSGN(_(e_listidx), idx);
9575 else
9576 {
9577 if (argvars[2].v_type == VAR_UNKNOWN)
9578 {
9579 /* Remove one item, return its value. */
9580 vimlist_remove(l, item, item);
9581 *rettv = item->li_tv;
9582 vim_free(item);
9583 }
9584 else
9585 {
9586 /* Remove range of items, return list with values. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009587 end = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009588 if (error)
9589 ; /* type error: do nothing */
9590 else if ((item2 = list_find(l, end)) == NULL)
9591 EMSGN(_(e_listidx), end);
9592 else
9593 {
9594 int cnt = 0;
9595
9596 for (li = item; li != NULL; li = li->li_next)
9597 {
9598 ++cnt;
9599 if (li == item2)
9600 break;
9601 }
9602 if (li == NULL) /* didn't find "item2" after "item" */
9603 EMSG(_(e_invrange));
9604 else
9605 {
9606 vimlist_remove(l, item, item2);
9607 if (rettv_list_alloc(rettv) == OK)
9608 {
9609 l = rettv->vval.v_list;
9610 l->lv_first = item;
9611 l->lv_last = item2;
9612 item->li_prev = NULL;
9613 item2->li_next = NULL;
9614 l->lv_len = cnt;
9615 }
9616 }
9617 }
9618 }
9619 }
9620 }
9621}
9622
9623/*
9624 * "rename({from}, {to})" function
9625 */
9626 static void
9627f_rename(typval_T *argvars, typval_T *rettv)
9628{
9629 char_u buf[NUMBUFLEN];
9630
9631 if (check_restricted() || check_secure())
9632 rettv->vval.v_number = -1;
9633 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009634 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
9635 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009636}
9637
9638/*
9639 * "repeat()" function
9640 */
9641 static void
9642f_repeat(typval_T *argvars, typval_T *rettv)
9643{
9644 char_u *p;
9645 int n;
9646 int slen;
9647 int len;
9648 char_u *r;
9649 int i;
9650
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009651 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009652 if (argvars[0].v_type == VAR_LIST)
9653 {
9654 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9655 while (n-- > 0)
9656 if (list_extend(rettv->vval.v_list,
9657 argvars[0].vval.v_list, NULL) == FAIL)
9658 break;
9659 }
9660 else
9661 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009662 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009663 rettv->v_type = VAR_STRING;
9664 rettv->vval.v_string = NULL;
9665
9666 slen = (int)STRLEN(p);
9667 len = slen * n;
9668 if (len <= 0)
9669 return;
9670
9671 r = alloc(len + 1);
9672 if (r != NULL)
9673 {
9674 for (i = 0; i < n; i++)
9675 mch_memmove(r + i * slen, p, (size_t)slen);
9676 r[len] = NUL;
9677 }
9678
9679 rettv->vval.v_string = r;
9680 }
9681}
9682
9683/*
9684 * "resolve()" function
9685 */
9686 static void
9687f_resolve(typval_T *argvars, typval_T *rettv)
9688{
9689 char_u *p;
9690#ifdef HAVE_READLINK
9691 char_u *buf = NULL;
9692#endif
9693
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009694 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009695#ifdef FEAT_SHORTCUT
9696 {
9697 char_u *v = NULL;
9698
9699 v = mch_resolve_shortcut(p);
9700 if (v != NULL)
9701 rettv->vval.v_string = v;
9702 else
9703 rettv->vval.v_string = vim_strsave(p);
9704 }
9705#else
9706# ifdef HAVE_READLINK
9707 {
9708 char_u *cpy;
9709 int len;
9710 char_u *remain = NULL;
9711 char_u *q;
9712 int is_relative_to_current = FALSE;
9713 int has_trailing_pathsep = FALSE;
9714 int limit = 100;
9715
9716 p = vim_strsave(p);
9717
9718 if (p[0] == '.' && (vim_ispathsep(p[1])
9719 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9720 is_relative_to_current = TRUE;
9721
9722 len = STRLEN(p);
9723 if (len > 0 && after_pathsep(p, p + len))
9724 {
9725 has_trailing_pathsep = TRUE;
9726 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9727 }
9728
9729 q = getnextcomp(p);
9730 if (*q != NUL)
9731 {
9732 /* Separate the first path component in "p", and keep the
9733 * remainder (beginning with the path separator). */
9734 remain = vim_strsave(q - 1);
9735 q[-1] = NUL;
9736 }
9737
9738 buf = alloc(MAXPATHL + 1);
9739 if (buf == NULL)
9740 goto fail;
9741
9742 for (;;)
9743 {
9744 for (;;)
9745 {
9746 len = readlink((char *)p, (char *)buf, MAXPATHL);
9747 if (len <= 0)
9748 break;
9749 buf[len] = NUL;
9750
9751 if (limit-- == 0)
9752 {
9753 vim_free(p);
9754 vim_free(remain);
9755 EMSG(_("E655: Too many symbolic links (cycle?)"));
9756 rettv->vval.v_string = NULL;
9757 goto fail;
9758 }
9759
9760 /* Ensure that the result will have a trailing path separator
9761 * if the argument has one. */
9762 if (remain == NULL && has_trailing_pathsep)
9763 add_pathsep(buf);
9764
9765 /* Separate the first path component in the link value and
9766 * concatenate the remainders. */
9767 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9768 if (*q != NUL)
9769 {
9770 if (remain == NULL)
9771 remain = vim_strsave(q - 1);
9772 else
9773 {
9774 cpy = concat_str(q - 1, remain);
9775 if (cpy != NULL)
9776 {
9777 vim_free(remain);
9778 remain = cpy;
9779 }
9780 }
9781 q[-1] = NUL;
9782 }
9783
9784 q = gettail(p);
9785 if (q > p && *q == NUL)
9786 {
9787 /* Ignore trailing path separator. */
9788 q[-1] = NUL;
9789 q = gettail(p);
9790 }
9791 if (q > p && !mch_isFullName(buf))
9792 {
9793 /* symlink is relative to directory of argument */
9794 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
9795 if (cpy != NULL)
9796 {
9797 STRCPY(cpy, p);
9798 STRCPY(gettail(cpy), buf);
9799 vim_free(p);
9800 p = cpy;
9801 }
9802 }
9803 else
9804 {
9805 vim_free(p);
9806 p = vim_strsave(buf);
9807 }
9808 }
9809
9810 if (remain == NULL)
9811 break;
9812
9813 /* Append the first path component of "remain" to "p". */
9814 q = getnextcomp(remain + 1);
9815 len = q - remain - (*q != NUL);
9816 cpy = vim_strnsave(p, STRLEN(p) + len);
9817 if (cpy != NULL)
9818 {
9819 STRNCAT(cpy, remain, len);
9820 vim_free(p);
9821 p = cpy;
9822 }
9823 /* Shorten "remain". */
9824 if (*q != NUL)
9825 STRMOVE(remain, q - 1);
9826 else
Bram Moolenaard23a8232018-02-10 18:45:26 +01009827 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009828 }
9829
9830 /* If the result is a relative path name, make it explicitly relative to
9831 * the current directory if and only if the argument had this form. */
9832 if (!vim_ispathsep(*p))
9833 {
9834 if (is_relative_to_current
9835 && *p != NUL
9836 && !(p[0] == '.'
9837 && (p[1] == NUL
9838 || vim_ispathsep(p[1])
9839 || (p[1] == '.'
9840 && (p[2] == NUL
9841 || vim_ispathsep(p[2]))))))
9842 {
9843 /* Prepend "./". */
9844 cpy = concat_str((char_u *)"./", p);
9845 if (cpy != NULL)
9846 {
9847 vim_free(p);
9848 p = cpy;
9849 }
9850 }
9851 else if (!is_relative_to_current)
9852 {
9853 /* Strip leading "./". */
9854 q = p;
9855 while (q[0] == '.' && vim_ispathsep(q[1]))
9856 q += 2;
9857 if (q > p)
9858 STRMOVE(p, p + 2);
9859 }
9860 }
9861
9862 /* Ensure that the result will have no trailing path separator
9863 * if the argument had none. But keep "/" or "//". */
9864 if (!has_trailing_pathsep)
9865 {
9866 q = p + STRLEN(p);
9867 if (after_pathsep(p, q))
9868 *gettail_sep(p) = NUL;
9869 }
9870
9871 rettv->vval.v_string = p;
9872 }
9873# else
9874 rettv->vval.v_string = vim_strsave(p);
9875# endif
9876#endif
9877
9878 simplify_filename(rettv->vval.v_string);
9879
9880#ifdef HAVE_READLINK
9881fail:
9882 vim_free(buf);
9883#endif
9884 rettv->v_type = VAR_STRING;
9885}
9886
9887/*
9888 * "reverse({list})" function
9889 */
9890 static void
9891f_reverse(typval_T *argvars, typval_T *rettv)
9892{
9893 list_T *l;
9894 listitem_T *li, *ni;
9895
9896 if (argvars[0].v_type != VAR_LIST)
9897 EMSG2(_(e_listarg), "reverse()");
9898 else if ((l = argvars[0].vval.v_list) != NULL
9899 && !tv_check_lock(l->lv_lock,
9900 (char_u *)N_("reverse() argument"), TRUE))
9901 {
9902 li = l->lv_last;
9903 l->lv_first = l->lv_last = NULL;
9904 l->lv_len = 0;
9905 while (li != NULL)
9906 {
9907 ni = li->li_prev;
9908 list_append(l, li);
9909 li = ni;
9910 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009911 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009912 l->lv_idx = l->lv_len - l->lv_idx - 1;
9913 }
9914}
9915
9916#define SP_NOMOVE 0x01 /* don't move cursor */
9917#define SP_REPEAT 0x02 /* repeat to find outer pair */
9918#define SP_RETCOUNT 0x04 /* return matchcount */
9919#define SP_SETPCMARK 0x08 /* set previous context mark */
9920#define SP_START 0x10 /* accept match at start position */
9921#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
9922#define SP_END 0x40 /* leave cursor at end of match */
9923#define SP_COLUMN 0x80 /* start at cursor column */
9924
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009925/*
9926 * Get flags for a search function.
9927 * Possibly sets "p_ws".
9928 * Returns BACKWARD, FORWARD or zero (for an error).
9929 */
9930 static int
9931get_search_arg(typval_T *varp, int *flagsp)
9932{
9933 int dir = FORWARD;
9934 char_u *flags;
9935 char_u nbuf[NUMBUFLEN];
9936 int mask;
9937
9938 if (varp->v_type != VAR_UNKNOWN)
9939 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009940 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009941 if (flags == NULL)
9942 return 0; /* type error; errmsg already given */
9943 while (*flags != NUL)
9944 {
9945 switch (*flags)
9946 {
9947 case 'b': dir = BACKWARD; break;
9948 case 'w': p_ws = TRUE; break;
9949 case 'W': p_ws = FALSE; break;
9950 default: mask = 0;
9951 if (flagsp != NULL)
9952 switch (*flags)
9953 {
9954 case 'c': mask = SP_START; break;
9955 case 'e': mask = SP_END; break;
9956 case 'm': mask = SP_RETCOUNT; break;
9957 case 'n': mask = SP_NOMOVE; break;
9958 case 'p': mask = SP_SUBPAT; break;
9959 case 'r': mask = SP_REPEAT; break;
9960 case 's': mask = SP_SETPCMARK; break;
9961 case 'z': mask = SP_COLUMN; break;
9962 }
9963 if (mask == 0)
9964 {
9965 EMSG2(_(e_invarg2), flags);
9966 dir = 0;
9967 }
9968 else
9969 *flagsp |= mask;
9970 }
9971 if (dir == 0)
9972 break;
9973 ++flags;
9974 }
9975 }
9976 return dir;
9977}
9978
9979/*
9980 * Shared by search() and searchpos() functions.
9981 */
9982 static int
9983search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
9984{
9985 int flags;
9986 char_u *pat;
9987 pos_T pos;
9988 pos_T save_cursor;
9989 int save_p_ws = p_ws;
9990 int dir;
9991 int retval = 0; /* default: FAIL */
9992 long lnum_stop = 0;
9993 proftime_T tm;
9994#ifdef FEAT_RELTIME
9995 long time_limit = 0;
9996#endif
9997 int options = SEARCH_KEEP;
9998 int subpatnum;
9999
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010000 pat = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010001 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
10002 if (dir == 0)
10003 goto theend;
10004 flags = *flagsp;
10005 if (flags & SP_START)
10006 options |= SEARCH_START;
10007 if (flags & SP_END)
10008 options |= SEARCH_END;
10009 if (flags & SP_COLUMN)
10010 options |= SEARCH_COL;
10011
10012 /* Optional arguments: line number to stop searching and timeout. */
10013 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
10014 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010015 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010016 if (lnum_stop < 0)
10017 goto theend;
10018#ifdef FEAT_RELTIME
10019 if (argvars[3].v_type != VAR_UNKNOWN)
10020 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010021 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010022 if (time_limit < 0)
10023 goto theend;
10024 }
10025#endif
10026 }
10027
10028#ifdef FEAT_RELTIME
10029 /* Set the time limit, if there is one. */
10030 profile_setlimit(time_limit, &tm);
10031#endif
10032
10033 /*
10034 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
10035 * Check to make sure only those flags are set.
10036 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
10037 * flags cannot be set. Check for that condition also.
10038 */
10039 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
10040 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10041 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010042 EMSG2(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010043 goto theend;
10044 }
10045
10046 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010047 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010048 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010049 if (subpatnum != FAIL)
10050 {
10051 if (flags & SP_SUBPAT)
10052 retval = subpatnum;
10053 else
10054 retval = pos.lnum;
10055 if (flags & SP_SETPCMARK)
10056 setpcmark();
10057 curwin->w_cursor = pos;
10058 if (match_pos != NULL)
10059 {
10060 /* Store the match cursor position */
10061 match_pos->lnum = pos.lnum;
10062 match_pos->col = pos.col + 1;
10063 }
10064 /* "/$" will put the cursor after the end of the line, may need to
10065 * correct that here */
10066 check_cursor();
10067 }
10068
10069 /* If 'n' flag is used: restore cursor position. */
10070 if (flags & SP_NOMOVE)
10071 curwin->w_cursor = save_cursor;
10072 else
10073 curwin->w_set_curswant = TRUE;
10074theend:
10075 p_ws = save_p_ws;
10076
10077 return retval;
10078}
10079
10080#ifdef FEAT_FLOAT
10081
10082/*
10083 * round() is not in C90, use ceil() or floor() instead.
10084 */
10085 float_T
10086vim_round(float_T f)
10087{
10088 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
10089}
10090
10091/*
10092 * "round({float})" function
10093 */
10094 static void
10095f_round(typval_T *argvars, typval_T *rettv)
10096{
10097 float_T f = 0.0;
10098
10099 rettv->v_type = VAR_FLOAT;
10100 if (get_float_arg(argvars, &f) == OK)
10101 rettv->vval.v_float = vim_round(f);
10102 else
10103 rettv->vval.v_float = 0.0;
10104}
10105#endif
10106
10107/*
10108 * "screenattr()" function
10109 */
10110 static void
10111f_screenattr(typval_T *argvars, typval_T *rettv)
10112{
10113 int row;
10114 int col;
10115 int c;
10116
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010117 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10118 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010119 if (row < 0 || row >= screen_Rows
10120 || col < 0 || col >= screen_Columns)
10121 c = -1;
10122 else
10123 c = ScreenAttrs[LineOffset[row] + col];
10124 rettv->vval.v_number = c;
10125}
10126
10127/*
10128 * "screenchar()" function
10129 */
10130 static void
10131f_screenchar(typval_T *argvars, typval_T *rettv)
10132{
10133 int row;
10134 int col;
10135 int off;
10136 int c;
10137
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010138 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10139 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010140 if (row < 0 || row >= screen_Rows
10141 || col < 0 || col >= screen_Columns)
10142 c = -1;
10143 else
10144 {
10145 off = LineOffset[row] + col;
10146#ifdef FEAT_MBYTE
10147 if (enc_utf8 && ScreenLinesUC[off] != 0)
10148 c = ScreenLinesUC[off];
10149 else
10150#endif
10151 c = ScreenLines[off];
10152 }
10153 rettv->vval.v_number = c;
10154}
10155
10156/*
10157 * "screencol()" function
10158 *
10159 * First column is 1 to be consistent with virtcol().
10160 */
10161 static void
10162f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10163{
10164 rettv->vval.v_number = screen_screencol() + 1;
10165}
10166
10167/*
10168 * "screenrow()" function
10169 */
10170 static void
10171f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10172{
10173 rettv->vval.v_number = screen_screenrow() + 1;
10174}
10175
10176/*
10177 * "search()" function
10178 */
10179 static void
10180f_search(typval_T *argvars, typval_T *rettv)
10181{
10182 int flags = 0;
10183
10184 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10185}
10186
10187/*
10188 * "searchdecl()" function
10189 */
10190 static void
10191f_searchdecl(typval_T *argvars, typval_T *rettv)
10192{
10193 int locally = 1;
10194 int thisblock = 0;
10195 int error = FALSE;
10196 char_u *name;
10197
10198 rettv->vval.v_number = 1; /* default: FAIL */
10199
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010200 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010201 if (argvars[1].v_type != VAR_UNKNOWN)
10202 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010203 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010204 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010205 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010206 }
10207 if (!error && name != NULL)
10208 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10209 locally, thisblock, SEARCH_KEEP) == FAIL;
10210}
10211
10212/*
10213 * Used by searchpair() and searchpairpos()
10214 */
10215 static int
10216searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10217{
10218 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010219 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010220 int save_p_ws = p_ws;
10221 int dir;
10222 int flags = 0;
10223 char_u nbuf1[NUMBUFLEN];
10224 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010225 int retval = 0; /* default: FAIL */
10226 long lnum_stop = 0;
10227 long time_limit = 0;
10228
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010229 /* Get the three pattern arguments: start, middle, end. Will result in an
10230 * error if not a valid argument. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010231 spat = tv_get_string_chk(&argvars[0]);
10232 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
10233 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010234 if (spat == NULL || mpat == NULL || epat == NULL)
10235 goto theend; /* type error */
10236
10237 /* Handle the optional fourth argument: flags */
10238 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
10239 if (dir == 0)
10240 goto theend;
10241
10242 /* Don't accept SP_END or SP_SUBPAT.
10243 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
10244 */
10245 if ((flags & (SP_END | SP_SUBPAT)) != 0
10246 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10247 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010248 EMSG2(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010249 goto theend;
10250 }
10251
10252 /* Using 'r' implies 'W', otherwise it doesn't work. */
10253 if (flags & SP_REPEAT)
10254 p_ws = FALSE;
10255
10256 /* Optional fifth argument: skip expression */
10257 if (argvars[3].v_type == VAR_UNKNOWN
10258 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010010259 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010260 else
10261 {
Bram Moolenaar48570482017-10-30 21:48:41 +010010262 skip = &argvars[4];
10263 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
10264 && skip->v_type != VAR_STRING)
10265 {
10266 /* Type error */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010267 EMSG2(_(e_invarg2), tv_get_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010010268 goto theend;
10269 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010270 if (argvars[5].v_type != VAR_UNKNOWN)
10271 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010272 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010273 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010274 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010275 EMSG2(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010276 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010277 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010278#ifdef FEAT_RELTIME
10279 if (argvars[6].v_type != VAR_UNKNOWN)
10280 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010281 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010282 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010283 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010284 EMSG2(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010285 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010286 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010287 }
10288#endif
10289 }
10290 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010291
10292 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
10293 match_pos, lnum_stop, time_limit);
10294
10295theend:
10296 p_ws = save_p_ws;
10297
10298 return retval;
10299}
10300
10301/*
10302 * "searchpair()" function
10303 */
10304 static void
10305f_searchpair(typval_T *argvars, typval_T *rettv)
10306{
10307 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
10308}
10309
10310/*
10311 * "searchpairpos()" function
10312 */
10313 static void
10314f_searchpairpos(typval_T *argvars, typval_T *rettv)
10315{
10316 pos_T match_pos;
10317 int lnum = 0;
10318 int col = 0;
10319
10320 if (rettv_list_alloc(rettv) == FAIL)
10321 return;
10322
10323 if (searchpair_cmn(argvars, &match_pos) > 0)
10324 {
10325 lnum = match_pos.lnum;
10326 col = match_pos.col;
10327 }
10328
10329 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10330 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10331}
10332
10333/*
10334 * Search for a start/middle/end thing.
10335 * Used by searchpair(), see its documentation for the details.
10336 * Returns 0 or -1 for no match,
10337 */
10338 long
10339do_searchpair(
10340 char_u *spat, /* start pattern */
10341 char_u *mpat, /* middle pattern */
10342 char_u *epat, /* end pattern */
10343 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010010344 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010345 int flags, /* SP_SETPCMARK and other SP_ values */
10346 pos_T *match_pos,
10347 linenr_T lnum_stop, /* stop at this line if not zero */
10348 long time_limit UNUSED) /* stop after this many msec */
10349{
10350 char_u *save_cpo;
10351 char_u *pat, *pat2 = NULL, *pat3 = NULL;
10352 long retval = 0;
10353 pos_T pos;
10354 pos_T firstpos;
10355 pos_T foundpos;
10356 pos_T save_cursor;
10357 pos_T save_pos;
10358 int n;
10359 int r;
10360 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010010361 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010362 int err;
10363 int options = SEARCH_KEEP;
10364 proftime_T tm;
10365
10366 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10367 save_cpo = p_cpo;
10368 p_cpo = empty_option;
10369
10370#ifdef FEAT_RELTIME
10371 /* Set the time limit, if there is one. */
10372 profile_setlimit(time_limit, &tm);
10373#endif
10374
10375 /* Make two search patterns: start/end (pat2, for in nested pairs) and
10376 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010377 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
10378 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010379 if (pat2 == NULL || pat3 == NULL)
10380 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010381 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010382 if (*mpat == NUL)
10383 STRCPY(pat3, pat2);
10384 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010385 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010386 spat, epat, mpat);
10387 if (flags & SP_START)
10388 options |= SEARCH_START;
10389
Bram Moolenaar48570482017-10-30 21:48:41 +010010390 if (skip != NULL)
10391 {
10392 /* Empty string means to not use the skip expression. */
10393 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
10394 use_skip = skip->vval.v_string != NULL
10395 && *skip->vval.v_string != NUL;
10396 }
10397
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010398 save_cursor = curwin->w_cursor;
10399 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010400 CLEAR_POS(&firstpos);
10401 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010402 pat = pat3;
10403 for (;;)
10404 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010405 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010406 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010407 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010408 /* didn't find it or found the first match again: FAIL */
10409 break;
10410
10411 if (firstpos.lnum == 0)
10412 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010413 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010414 {
10415 /* Found the same position again. Can happen with a pattern that
10416 * has "\zs" at the end and searching backwards. Advance one
10417 * character and try again. */
10418 if (dir == BACKWARD)
10419 decl(&pos);
10420 else
10421 incl(&pos);
10422 }
10423 foundpos = pos;
10424
10425 /* clear the start flag to avoid getting stuck here */
10426 options &= ~SEARCH_START;
10427
10428 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010010429 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010430 {
10431 save_pos = curwin->w_cursor;
10432 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010010433 err = FALSE;
10434 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010435 curwin->w_cursor = save_pos;
10436 if (err)
10437 {
10438 /* Evaluating {skip} caused an error, break here. */
10439 curwin->w_cursor = save_cursor;
10440 retval = -1;
10441 break;
10442 }
10443 if (r)
10444 continue;
10445 }
10446
10447 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
10448 {
10449 /* Found end when searching backwards or start when searching
10450 * forward: nested pair. */
10451 ++nest;
10452 pat = pat2; /* nested, don't search for middle */
10453 }
10454 else
10455 {
10456 /* Found end when searching forward or start when searching
10457 * backward: end of (nested) pair; or found middle in outer pair. */
10458 if (--nest == 1)
10459 pat = pat3; /* outer level, search for middle */
10460 }
10461
10462 if (nest == 0)
10463 {
10464 /* Found the match: return matchcount or line number. */
10465 if (flags & SP_RETCOUNT)
10466 ++retval;
10467 else
10468 retval = pos.lnum;
10469 if (flags & SP_SETPCMARK)
10470 setpcmark();
10471 curwin->w_cursor = pos;
10472 if (!(flags & SP_REPEAT))
10473 break;
10474 nest = 1; /* search for next unmatched */
10475 }
10476 }
10477
10478 if (match_pos != NULL)
10479 {
10480 /* Store the match cursor position */
10481 match_pos->lnum = curwin->w_cursor.lnum;
10482 match_pos->col = curwin->w_cursor.col + 1;
10483 }
10484
10485 /* If 'n' flag is used or search failed: restore cursor position. */
10486 if ((flags & SP_NOMOVE) || retval == 0)
10487 curwin->w_cursor = save_cursor;
10488
10489theend:
10490 vim_free(pat2);
10491 vim_free(pat3);
10492 if (p_cpo == empty_option)
10493 p_cpo = save_cpo;
10494 else
10495 /* Darn, evaluating the {skip} expression changed the value. */
10496 free_string_option(save_cpo);
10497
10498 return retval;
10499}
10500
10501/*
10502 * "searchpos()" function
10503 */
10504 static void
10505f_searchpos(typval_T *argvars, typval_T *rettv)
10506{
10507 pos_T match_pos;
10508 int lnum = 0;
10509 int col = 0;
10510 int n;
10511 int flags = 0;
10512
10513 if (rettv_list_alloc(rettv) == FAIL)
10514 return;
10515
10516 n = search_cmn(argvars, &match_pos, &flags);
10517 if (n > 0)
10518 {
10519 lnum = match_pos.lnum;
10520 col = match_pos.col;
10521 }
10522
10523 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10524 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10525 if (flags & SP_SUBPAT)
10526 list_append_number(rettv->vval.v_list, (varnumber_T)n);
10527}
10528
10529 static void
10530f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
10531{
10532#ifdef FEAT_CLIENTSERVER
10533 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010534 char_u *server = tv_get_string_chk(&argvars[0]);
10535 char_u *reply = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010536
10537 rettv->vval.v_number = -1;
10538 if (server == NULL || reply == NULL)
10539 return;
10540 if (check_restricted() || check_secure())
10541 return;
10542# ifdef FEAT_X11
10543 if (check_connection() == FAIL)
10544 return;
10545# endif
10546
10547 if (serverSendReply(server, reply) < 0)
10548 {
10549 EMSG(_("E258: Unable to send to client"));
10550 return;
10551 }
10552 rettv->vval.v_number = 0;
10553#else
10554 rettv->vval.v_number = -1;
10555#endif
10556}
10557
10558 static void
10559f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
10560{
10561 char_u *r = NULL;
10562
10563#ifdef FEAT_CLIENTSERVER
10564# ifdef WIN32
10565 r = serverGetVimNames();
10566# else
10567 make_connection();
10568 if (X_DISPLAY != NULL)
10569 r = serverGetVimNames(X_DISPLAY);
10570# endif
10571#endif
10572 rettv->v_type = VAR_STRING;
10573 rettv->vval.v_string = r;
10574}
10575
10576/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010577 * "setbufline()" function
10578 */
10579 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020010580f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010581{
10582 linenr_T lnum;
10583 buf_T *buf;
10584
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010010585 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010586 if (buf == NULL)
10587 rettv->vval.v_number = 1; /* FAIL */
10588 else
10589 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010590 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020010591 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010592 }
10593}
10594
10595/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010596 * "setbufvar()" function
10597 */
10598 static void
10599f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10600{
10601 buf_T *buf;
10602 char_u *varname, *bufvarname;
10603 typval_T *varp;
10604 char_u nbuf[NUMBUFLEN];
10605
10606 if (check_restricted() || check_secure())
10607 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010608 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
10609 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010010610 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010611 varp = &argvars[2];
10612
10613 if (buf != NULL && varname != NULL && varp != NULL)
10614 {
10615 if (*varname == '&')
10616 {
10617 long numval;
10618 char_u *strval;
10619 int error = FALSE;
10620 aco_save_T aco;
10621
10622 /* set curbuf to be our buf, temporarily */
10623 aucmd_prepbuf(&aco, buf);
10624
10625 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010626 numval = (long)tv_get_number_chk(varp, &error);
10627 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010628 if (!error && strval != NULL)
10629 set_option_value(varname, numval, strval, OPT_LOCAL);
10630
10631 /* reset notion of buffer */
10632 aucmd_restbuf(&aco);
10633 }
10634 else
10635 {
10636 buf_T *save_curbuf = curbuf;
10637
10638 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
10639 if (bufvarname != NULL)
10640 {
10641 curbuf = buf;
10642 STRCPY(bufvarname, "b:");
10643 STRCPY(bufvarname + 2, varname);
10644 set_var(bufvarname, varp, TRUE);
10645 vim_free(bufvarname);
10646 curbuf = save_curbuf;
10647 }
10648 }
10649 }
10650}
10651
10652 static void
10653f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10654{
10655 dict_T *d;
10656 dictitem_T *di;
10657 char_u *csearch;
10658
10659 if (argvars[0].v_type != VAR_DICT)
10660 {
10661 EMSG(_(e_dictreq));
10662 return;
10663 }
10664
10665 if ((d = argvars[0].vval.v_dict) != NULL)
10666 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010010667 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010668 if (csearch != NULL)
10669 {
10670#ifdef FEAT_MBYTE
10671 if (enc_utf8)
10672 {
10673 int pcc[MAX_MCO];
10674 int c = utfc_ptr2char(csearch, pcc);
10675
10676 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
10677 }
10678 else
10679#endif
10680 set_last_csearch(PTR2CHAR(csearch),
10681 csearch, MB_PTR2LEN(csearch));
10682 }
10683
10684 di = dict_find(d, (char_u *)"forward", -1);
10685 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010686 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010687 ? FORWARD : BACKWARD);
10688
10689 di = dict_find(d, (char_u *)"until", -1);
10690 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010691 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010692 }
10693}
10694
10695/*
10696 * "setcmdpos()" function
10697 */
10698 static void
10699f_setcmdpos(typval_T *argvars, typval_T *rettv)
10700{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010701 int pos = (int)tv_get_number(&argvars[0]) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010702
10703 if (pos >= 0)
10704 rettv->vval.v_number = set_cmdline_pos(pos);
10705}
10706
10707/*
10708 * "setfperm({fname}, {mode})" function
10709 */
10710 static void
10711f_setfperm(typval_T *argvars, typval_T *rettv)
10712{
10713 char_u *fname;
10714 char_u modebuf[NUMBUFLEN];
10715 char_u *mode_str;
10716 int i;
10717 int mask;
10718 int mode = 0;
10719
10720 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010721 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010722 if (fname == NULL)
10723 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010724 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010725 if (mode_str == NULL)
10726 return;
10727 if (STRLEN(mode_str) != 9)
10728 {
10729 EMSG2(_(e_invarg2), mode_str);
10730 return;
10731 }
10732
10733 mask = 1;
10734 for (i = 8; i >= 0; --i)
10735 {
10736 if (mode_str[i] != '-')
10737 mode |= mask;
10738 mask = mask << 1;
10739 }
10740 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
10741}
10742
10743/*
10744 * "setline()" function
10745 */
10746 static void
10747f_setline(typval_T *argvars, typval_T *rettv)
10748{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010749 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010750
Bram Moolenaarca851592018-06-06 21:04:07 +020010751 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010752}
10753
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010754/*
10755 * Used by "setqflist()" and "setloclist()" functions
10756 */
10757 static void
10758set_qf_ll_list(
10759 win_T *wp UNUSED,
10760 typval_T *list_arg UNUSED,
10761 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020010762 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010763 typval_T *rettv)
10764{
10765#ifdef FEAT_QUICKFIX
10766 static char *e_invact = N_("E927: Invalid action: '%s'");
10767 char_u *act;
10768 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010769 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010770#endif
10771
10772 rettv->vval.v_number = -1;
10773
10774#ifdef FEAT_QUICKFIX
10775 if (list_arg->v_type != VAR_LIST)
10776 EMSG(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010777 else if (recursive != 0)
10778 EMSG(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010779 else
10780 {
10781 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010782 dict_T *d = NULL;
10783 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010784
10785 if (action_arg->v_type == VAR_STRING)
10786 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010787 act = tv_get_string_chk(action_arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010788 if (act == NULL)
10789 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020010790 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
10791 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010792 action = *act;
10793 else
10794 EMSG2(_(e_invact), act);
10795 }
10796 else if (action_arg->v_type == VAR_UNKNOWN)
10797 action = ' ';
10798 else
10799 EMSG(_(e_stringreq));
10800
Bram Moolenaard823fa92016-08-12 16:29:27 +020010801 if (action_arg->v_type != VAR_UNKNOWN
10802 && what_arg->v_type != VAR_UNKNOWN)
10803 {
10804 if (what_arg->v_type == VAR_DICT)
10805 d = what_arg->vval.v_dict;
10806 else
10807 {
10808 EMSG(_(e_dictreq));
10809 valid_dict = FALSE;
10810 }
10811 }
10812
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010813 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010814 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010815 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
10816 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010817 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010818 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010819 }
10820#endif
10821}
10822
10823/*
10824 * "setloclist()" function
10825 */
10826 static void
10827f_setloclist(typval_T *argvars, typval_T *rettv)
10828{
10829 win_T *win;
10830
10831 rettv->vval.v_number = -1;
10832
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020010833 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010834 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020010835 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010836}
10837
10838/*
10839 * "setmatches()" function
10840 */
10841 static void
10842f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10843{
10844#ifdef FEAT_SEARCH_EXTRA
10845 list_T *l;
10846 listitem_T *li;
10847 dict_T *d;
10848 list_T *s = NULL;
10849
10850 rettv->vval.v_number = -1;
10851 if (argvars[0].v_type != VAR_LIST)
10852 {
10853 EMSG(_(e_listreq));
10854 return;
10855 }
10856 if ((l = argvars[0].vval.v_list) != NULL)
10857 {
10858
10859 /* To some extent make sure that we are dealing with a list from
10860 * "getmatches()". */
10861 li = l->lv_first;
10862 while (li != NULL)
10863 {
10864 if (li->li_tv.v_type != VAR_DICT
10865 || (d = li->li_tv.vval.v_dict) == NULL)
10866 {
10867 EMSG(_(e_invarg));
10868 return;
10869 }
10870 if (!(dict_find(d, (char_u *)"group", -1) != NULL
10871 && (dict_find(d, (char_u *)"pattern", -1) != NULL
10872 || dict_find(d, (char_u *)"pos1", -1) != NULL)
10873 && dict_find(d, (char_u *)"priority", -1) != NULL
10874 && dict_find(d, (char_u *)"id", -1) != NULL))
10875 {
10876 EMSG(_(e_invarg));
10877 return;
10878 }
10879 li = li->li_next;
10880 }
10881
10882 clear_matches(curwin);
10883 li = l->lv_first;
10884 while (li != NULL)
10885 {
10886 int i = 0;
10887 char_u buf[5];
10888 dictitem_T *di;
10889 char_u *group;
10890 int priority;
10891 int id;
10892 char_u *conceal;
10893
10894 d = li->li_tv.vval.v_dict;
10895 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
10896 {
10897 if (s == NULL)
10898 {
10899 s = list_alloc();
10900 if (s == NULL)
10901 return;
10902 }
10903
10904 /* match from matchaddpos() */
10905 for (i = 1; i < 9; i++)
10906 {
10907 sprintf((char *)buf, (char *)"pos%d", i);
10908 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
10909 {
10910 if (di->di_tv.v_type != VAR_LIST)
10911 return;
10912
10913 list_append_tv(s, &di->di_tv);
10914 s->lv_refcount++;
10915 }
10916 else
10917 break;
10918 }
10919 }
10920
Bram Moolenaar8f667172018-12-14 15:38:31 +010010921 group = dict_get_string(d, (char_u *)"group", TRUE);
10922 priority = (int)dict_get_number(d, (char_u *)"priority");
10923 id = (int)dict_get_number(d, (char_u *)"id");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010924 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar8f667172018-12-14 15:38:31 +010010925 ? dict_get_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010926 : NULL;
10927 if (i == 0)
10928 {
10929 match_add(curwin, group,
Bram Moolenaar8f667172018-12-14 15:38:31 +010010930 dict_get_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010931 priority, id, NULL, conceal);
10932 }
10933 else
10934 {
10935 match_add(curwin, group, NULL, priority, id, s, conceal);
10936 list_unref(s);
10937 s = NULL;
10938 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010939 vim_free(group);
10940 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010941
10942 li = li->li_next;
10943 }
10944 rettv->vval.v_number = 0;
10945 }
10946#endif
10947}
10948
10949/*
10950 * "setpos()" function
10951 */
10952 static void
10953f_setpos(typval_T *argvars, typval_T *rettv)
10954{
10955 pos_T pos;
10956 int fnum;
10957 char_u *name;
10958 colnr_T curswant = -1;
10959
10960 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010961 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010962 if (name != NULL)
10963 {
10964 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
10965 {
10966 if (--pos.col < 0)
10967 pos.col = 0;
10968 if (name[0] == '.' && name[1] == NUL)
10969 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010970 /* set cursor; "fnum" is ignored */
10971 curwin->w_cursor = pos;
10972 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010973 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010974 curwin->w_curswant = curswant - 1;
10975 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010976 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010977 check_cursor();
10978 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010979 }
10980 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
10981 {
10982 /* set mark */
10983 if (setmark_pos(name[1], &pos, fnum) == OK)
10984 rettv->vval.v_number = 0;
10985 }
10986 else
10987 EMSG(_(e_invarg));
10988 }
10989 }
10990}
10991
10992/*
10993 * "setqflist()" function
10994 */
10995 static void
10996f_setqflist(typval_T *argvars, typval_T *rettv)
10997{
Bram Moolenaard823fa92016-08-12 16:29:27 +020010998 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010999}
11000
11001/*
11002 * "setreg()" function
11003 */
11004 static void
11005f_setreg(typval_T *argvars, typval_T *rettv)
11006{
11007 int regname;
11008 char_u *strregname;
11009 char_u *stropt;
11010 char_u *strval;
11011 int append;
11012 char_u yank_type;
11013 long block_len;
11014
11015 block_len = -1;
11016 yank_type = MAUTO;
11017 append = FALSE;
11018
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011019 strregname = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011020 rettv->vval.v_number = 1; /* FAIL is default */
11021
11022 if (strregname == NULL)
11023 return; /* type error; errmsg already given */
11024 regname = *strregname;
11025 if (regname == 0 || regname == '@')
11026 regname = '"';
11027
11028 if (argvars[2].v_type != VAR_UNKNOWN)
11029 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011030 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011031 if (stropt == NULL)
11032 return; /* type error */
11033 for (; *stropt != NUL; ++stropt)
11034 switch (*stropt)
11035 {
11036 case 'a': case 'A': /* append */
11037 append = TRUE;
11038 break;
11039 case 'v': case 'c': /* character-wise selection */
11040 yank_type = MCHAR;
11041 break;
11042 case 'V': case 'l': /* line-wise selection */
11043 yank_type = MLINE;
11044 break;
11045 case 'b': case Ctrl_V: /* block-wise selection */
11046 yank_type = MBLOCK;
11047 if (VIM_ISDIGIT(stropt[1]))
11048 {
11049 ++stropt;
11050 block_len = getdigits(&stropt) - 1;
11051 --stropt;
11052 }
11053 break;
11054 }
11055 }
11056
11057 if (argvars[1].v_type == VAR_LIST)
11058 {
11059 char_u **lstval;
11060 char_u **allocval;
11061 char_u buf[NUMBUFLEN];
11062 char_u **curval;
11063 char_u **curallocval;
11064 list_T *ll = argvars[1].vval.v_list;
11065 listitem_T *li;
11066 int len;
11067
11068 /* If the list is NULL handle like an empty list. */
11069 len = ll == NULL ? 0 : ll->lv_len;
11070
11071 /* First half: use for pointers to result lines; second half: use for
11072 * pointers to allocated copies. */
11073 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
11074 if (lstval == NULL)
11075 return;
11076 curval = lstval;
11077 allocval = lstval + len + 2;
11078 curallocval = allocval;
11079
11080 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
11081 li = li->li_next)
11082 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011083 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011084 if (strval == NULL)
11085 goto free_lstval;
11086 if (strval == buf)
11087 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011088 /* Need to make a copy, next tv_get_string_buf_chk() will
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011089 * overwrite the string. */
11090 strval = vim_strsave(buf);
11091 if (strval == NULL)
11092 goto free_lstval;
11093 *curallocval++ = strval;
11094 }
11095 *curval++ = strval;
11096 }
11097 *curval++ = NULL;
11098
11099 write_reg_contents_lst(regname, lstval, -1,
11100 append, yank_type, block_len);
11101free_lstval:
11102 while (curallocval > allocval)
11103 vim_free(*--curallocval);
11104 vim_free(lstval);
11105 }
11106 else
11107 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011108 strval = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011109 if (strval == NULL)
11110 return;
11111 write_reg_contents_ex(regname, strval, -1,
11112 append, yank_type, block_len);
11113 }
11114 rettv->vval.v_number = 0;
11115}
11116
11117/*
11118 * "settabvar()" function
11119 */
11120 static void
11121f_settabvar(typval_T *argvars, typval_T *rettv)
11122{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011123 tabpage_T *save_curtab;
11124 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011125 char_u *varname, *tabvarname;
11126 typval_T *varp;
11127
11128 rettv->vval.v_number = 0;
11129
11130 if (check_restricted() || check_secure())
11131 return;
11132
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011133 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
11134 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011135 varp = &argvars[2];
11136
Bram Moolenaar4033c552017-09-16 20:54:51 +020011137 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011138 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011139 save_curtab = curtab;
11140 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011141
11142 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
11143 if (tabvarname != NULL)
11144 {
11145 STRCPY(tabvarname, "t:");
11146 STRCPY(tabvarname + 2, varname);
11147 set_var(tabvarname, varp, TRUE);
11148 vim_free(tabvarname);
11149 }
11150
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011151 /* Restore current tabpage */
11152 if (valid_tabpage(save_curtab))
11153 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011154 }
11155}
11156
11157/*
11158 * "settabwinvar()" function
11159 */
11160 static void
11161f_settabwinvar(typval_T *argvars, typval_T *rettv)
11162{
11163 setwinvar(argvars, rettv, 1);
11164}
11165
11166/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011167 * "settagstack()" function
11168 */
11169 static void
11170f_settagstack(typval_T *argvars, typval_T *rettv)
11171{
11172 static char *e_invact2 = N_("E962: Invalid action: '%s'");
11173 win_T *wp;
11174 dict_T *d;
11175 int action = 'r';
11176
11177 rettv->vval.v_number = -1;
11178
11179 // first argument: window number or id
11180 wp = find_win_by_nr_or_id(&argvars[0]);
11181 if (wp == NULL)
11182 return;
11183
11184 // second argument: dict with items to set in the tag stack
11185 if (argvars[1].v_type != VAR_DICT)
11186 {
11187 EMSG(_(e_dictreq));
11188 return;
11189 }
11190 d = argvars[1].vval.v_dict;
11191 if (d == NULL)
11192 return;
11193
11194 // third argument: action - 'a' for append and 'r' for replace.
11195 // default is to replace the stack.
11196 if (argvars[2].v_type == VAR_UNKNOWN)
11197 action = 'r';
11198 else if (argvars[2].v_type == VAR_STRING)
11199 {
11200 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011201 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011202 if (actstr == NULL)
11203 return;
11204 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
11205 action = *actstr;
11206 else
11207 {
11208 EMSG2(_(e_invact2), actstr);
11209 return;
11210 }
11211 }
11212 else
11213 {
11214 EMSG(_(e_stringreq));
11215 return;
11216 }
11217
11218 if (set_tagstack(wp, d, action) == OK)
11219 rettv->vval.v_number = 0;
11220}
11221
11222/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011223 * "setwinvar()" function
11224 */
11225 static void
11226f_setwinvar(typval_T *argvars, typval_T *rettv)
11227{
11228 setwinvar(argvars, rettv, 0);
11229}
11230
11231#ifdef FEAT_CRYPT
11232/*
11233 * "sha256({string})" function
11234 */
11235 static void
11236f_sha256(typval_T *argvars, typval_T *rettv)
11237{
11238 char_u *p;
11239
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011240 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011241 rettv->vval.v_string = vim_strsave(
11242 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
11243 rettv->v_type = VAR_STRING;
11244}
11245#endif /* FEAT_CRYPT */
11246
11247/*
11248 * "shellescape({string})" function
11249 */
11250 static void
11251f_shellescape(typval_T *argvars, typval_T *rettv)
11252{
Bram Moolenaar20615522017-06-05 18:46:26 +020011253 int do_special = non_zero_arg(&argvars[1]);
11254
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011255 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011256 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011257 rettv->v_type = VAR_STRING;
11258}
11259
11260/*
11261 * shiftwidth() function
11262 */
11263 static void
11264f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
11265{
Bram Moolenaarf9514162018-11-22 03:08:29 +010011266 rettv->vval.v_number = 0;
11267
11268 if (argvars[0].v_type != VAR_UNKNOWN)
11269 {
11270 long col;
11271
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011272 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +010011273 if (col < 0)
11274 return; // type error; errmsg already given
11275#ifdef FEAT_VARTABS
11276 rettv->vval.v_number = get_sw_value_col(curbuf, col);
11277 return;
11278#endif
11279 }
11280
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011281 rettv->vval.v_number = get_sw_value(curbuf);
11282}
11283
Bram Moolenaar162b7142018-12-21 15:17:36 +010011284#ifdef FEAT_SIGNS
11285/*
11286 * "sign_define()" function
11287 */
11288 static void
11289f_sign_define(typval_T *argvars, typval_T *rettv)
11290{
11291 char_u *name;
11292 dict_T *dict;
11293 char_u *icon = NULL;
11294 char_u *linehl = NULL;
11295 char_u *text = NULL;
11296 char_u *texthl = NULL;
11297
11298 rettv->vval.v_number = -1;
11299
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011300 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011301 if (name == NULL)
11302 return;
11303
11304 if (argvars[1].v_type != VAR_UNKNOWN)
11305 {
11306 if (argvars[1].v_type != VAR_DICT)
11307 {
11308 EMSG(_(e_dictreq));
11309 return;
11310 }
11311
11312 // sign attributes
11313 dict = argvars[1].vval.v_dict;
11314 if (dict_find(dict, (char_u *)"icon", -1) != NULL)
11315 icon = dict_get_string(dict, (char_u *)"icon", TRUE);
11316 if (dict_find(dict, (char_u *)"linehl", -1) != NULL)
11317 linehl = dict_get_string(dict, (char_u *)"linehl", TRUE);
11318 if (dict_find(dict, (char_u *)"text", -1) != NULL)
11319 text = dict_get_string(dict, (char_u *)"text", TRUE);
11320 if (dict_find(dict, (char_u *)"texthl", -1) != NULL)
11321 texthl = dict_get_string(dict, (char_u *)"texthl", TRUE);
11322 }
11323
11324 if (sign_define_by_name(name, icon, linehl, text, texthl) == OK)
11325 rettv->vval.v_number = 0;
11326
11327 vim_free(icon);
11328 vim_free(linehl);
11329 vim_free(text);
11330 vim_free(texthl);
11331}
11332
11333/*
11334 * "sign_getdefined()" function
11335 */
11336 static void
11337f_sign_getdefined(typval_T *argvars, typval_T *rettv)
11338{
11339 char_u *name = NULL;
11340
11341 if (rettv_list_alloc_id(rettv, aid_sign_getdefined) != OK)
11342 return;
11343
11344 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011345 name = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011346
11347 sign_getlist(name, rettv->vval.v_list);
11348}
11349
11350/*
11351 * "sign_getplaced()" function
11352 */
11353 static void
11354f_sign_getplaced(typval_T *argvars, typval_T *rettv)
11355{
11356 buf_T *buf = NULL;
11357 dict_T *dict;
11358 dictitem_T *di;
11359 linenr_T lnum = 0;
11360 int sign_id = 0;
11361 char_u *group = NULL;
11362 int notanum = FALSE;
11363
11364 if (rettv_list_alloc_id(rettv, aid_sign_getplaced) != OK)
11365 return;
11366
11367 if (argvars[0].v_type != VAR_UNKNOWN)
11368 {
11369 // get signs placed in this buffer
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011370 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011371 if (buf == NULL)
11372 {
11373 EMSG2(_("E158: Invalid buffer name: %s"),
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011374 tv_get_string(&argvars[0]));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011375 return;
11376 }
11377
11378 if (argvars[1].v_type != VAR_UNKNOWN)
11379 {
11380 if (argvars[1].v_type != VAR_DICT ||
11381 ((dict = argvars[1].vval.v_dict) == NULL))
11382 {
11383 EMSG(_(e_dictreq));
11384 return;
11385 }
11386 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
11387 {
11388 // get signs placed at this line
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011389 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011390 if (notanum)
11391 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011392 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011393 }
11394 if ((di = dict_find(dict, (char_u *)"id", -1)) != NULL)
11395 {
11396 // get sign placed with this identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011397 sign_id = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011398 if (notanum)
11399 return;
11400 }
11401 if ((di = dict_find(dict, (char_u *)"group", -1)) != NULL)
11402 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011403 group = tv_get_string_chk(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011404 if (group == NULL)
11405 return;
Bram Moolenaar6436cd82018-12-27 00:28:33 +010011406 if (*group == '\0') // empty string means global group
11407 group = NULL;
Bram Moolenaar162b7142018-12-21 15:17:36 +010011408 }
11409 }
11410 }
11411
11412 sign_get_placed(buf, lnum, sign_id, group, rettv->vval.v_list);
11413}
11414
11415/*
11416 * "sign_place()" function
11417 */
11418 static void
11419f_sign_place(typval_T *argvars, typval_T *rettv)
11420{
11421 int sign_id;
11422 char_u *group = NULL;
11423 char_u *sign_name;
11424 buf_T *buf;
11425 dict_T *dict;
11426 dictitem_T *di;
11427 linenr_T lnum = 0;
11428 int prio = SIGN_DEF_PRIO;
11429 int notanum = FALSE;
11430
11431 rettv->vval.v_number = -1;
11432
11433 // Sign identifer
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011434 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011435 if (notanum)
11436 return;
11437 if (sign_id < 0)
11438 {
11439 EMSG(_(e_invarg));
11440 return;
11441 }
11442
11443 // Sign group
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011444 group = tv_get_string_chk(&argvars[1]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011445 if (group == NULL)
11446 return;
11447 if (group[0] == '\0')
11448 group = NULL; // global sign group
11449 else
11450 {
11451 group = vim_strsave(group);
11452 if (group == NULL)
11453 return;
11454 }
11455
11456 // Sign name
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011457 sign_name = tv_get_string_chk(&argvars[2]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011458 if (sign_name == NULL)
11459 goto cleanup;
11460
11461 // Buffer to place the sign
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011462 buf = tv_get_buf(&argvars[3], FALSE);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011463 if (buf == NULL)
11464 {
Bram Moolenaarb589f952019-01-07 22:10:00 +010011465 EMSG2(_("E158: Invalid buffer name: %s"), tv_get_string(&argvars[3]));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011466 goto cleanup;
11467 }
11468
11469 if (argvars[4].v_type != VAR_UNKNOWN)
11470 {
11471 if (argvars[4].v_type != VAR_DICT ||
11472 ((dict = argvars[4].vval.v_dict) == NULL))
11473 {
11474 EMSG(_(e_dictreq));
11475 goto cleanup;
11476 }
11477
11478 // Line number where the sign is to be placed
11479 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
11480 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011481 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011482 if (notanum)
11483 goto cleanup;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011484 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011485 }
11486 if ((di = dict_find(dict, (char_u *)"priority", -1)) != NULL)
11487 {
11488 // Sign priority
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011489 prio = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011490 if (notanum)
11491 goto cleanup;
11492 }
11493 }
11494
11495 if (sign_place(&sign_id, group, sign_name, buf, lnum, prio) == OK)
11496 rettv->vval.v_number = sign_id;
11497
11498cleanup:
11499 vim_free(group);
11500}
11501
11502/*
11503 * "sign_undefine()" function
11504 */
11505 static void
11506f_sign_undefine(typval_T *argvars, typval_T *rettv)
11507{
11508 char_u *name;
11509
11510 rettv->vval.v_number = -1;
11511
11512 if (argvars[0].v_type == VAR_UNKNOWN)
11513 {
11514 // Free all the signs
11515 free_signs();
11516 rettv->vval.v_number = 0;
11517 }
11518 else
11519 {
11520 // Free only the specified sign
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011521 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011522 if (name == NULL)
11523 return;
11524
11525 if (sign_undefine_by_name(name) == OK)
11526 rettv->vval.v_number = 0;
11527 }
11528}
11529
11530/*
11531 * "sign_unplace()" function
11532 */
11533 static void
11534f_sign_unplace(typval_T *argvars, typval_T *rettv)
11535{
11536 dict_T *dict;
11537 dictitem_T *di;
11538 int sign_id = 0;
11539 buf_T *buf = NULL;
11540 char_u *group = NULL;
11541
11542 rettv->vval.v_number = -1;
11543
11544 if (argvars[0].v_type != VAR_STRING)
11545 {
11546 EMSG(_(e_invarg));
11547 return;
11548 }
11549
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011550 group = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011551 if (group[0] == '\0')
11552 group = NULL; // global sign group
11553 else
11554 {
11555 group = vim_strsave(group);
11556 if (group == NULL)
11557 return;
11558 }
11559
11560 if (argvars[1].v_type != VAR_UNKNOWN)
11561 {
11562 if (argvars[1].v_type != VAR_DICT)
11563 {
11564 EMSG(_(e_dictreq));
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010011565 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010011566 }
11567 dict = argvars[1].vval.v_dict;
11568
11569 if ((di = dict_find(dict, (char_u *)"buffer", -1)) != NULL)
11570 {
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011571 buf = tv_get_buf(&di->di_tv, FALSE);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011572 if (buf == NULL)
11573 {
11574 EMSG2(_("E158: Invalid buffer name: %s"),
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011575 tv_get_string(&di->di_tv));
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010011576 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010011577 }
11578 }
11579 if (dict_find(dict, (char_u *)"id", -1) != NULL)
11580 sign_id = dict_get_number(dict, (char_u *)"id");
11581 }
11582
11583 if (buf == NULL)
11584 {
11585 // Delete the sign in all the buffers
11586 FOR_ALL_BUFFERS(buf)
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010011587 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010011588 rettv->vval.v_number = 0;
11589 }
11590 else
11591 {
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010011592 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010011593 rettv->vval.v_number = 0;
11594 }
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010011595
11596cleanup:
Bram Moolenaar162b7142018-12-21 15:17:36 +010011597 vim_free(group);
11598}
11599#endif
11600
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011601/*
11602 * "simplify()" function
11603 */
11604 static void
11605f_simplify(typval_T *argvars, typval_T *rettv)
11606{
11607 char_u *p;
11608
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011609 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011610 rettv->vval.v_string = vim_strsave(p);
11611 simplify_filename(rettv->vval.v_string); /* simplify in place */
11612 rettv->v_type = VAR_STRING;
11613}
11614
11615#ifdef FEAT_FLOAT
11616/*
11617 * "sin()" function
11618 */
11619 static void
11620f_sin(typval_T *argvars, typval_T *rettv)
11621{
11622 float_T f = 0.0;
11623
11624 rettv->v_type = VAR_FLOAT;
11625 if (get_float_arg(argvars, &f) == OK)
11626 rettv->vval.v_float = sin(f);
11627 else
11628 rettv->vval.v_float = 0.0;
11629}
11630
11631/*
11632 * "sinh()" function
11633 */
11634 static void
11635f_sinh(typval_T *argvars, typval_T *rettv)
11636{
11637 float_T f = 0.0;
11638
11639 rettv->v_type = VAR_FLOAT;
11640 if (get_float_arg(argvars, &f) == OK)
11641 rettv->vval.v_float = sinh(f);
11642 else
11643 rettv->vval.v_float = 0.0;
11644}
11645#endif
11646
11647static int
11648#ifdef __BORLANDC__
11649 _RTLENTRYF
11650#endif
11651 item_compare(const void *s1, const void *s2);
11652static int
11653#ifdef __BORLANDC__
11654 _RTLENTRYF
11655#endif
11656 item_compare2(const void *s1, const void *s2);
11657
11658/* struct used in the array that's given to qsort() */
11659typedef struct
11660{
11661 listitem_T *item;
11662 int idx;
11663} sortItem_T;
11664
11665/* struct storing information about current sort */
11666typedef struct
11667{
11668 int item_compare_ic;
11669 int item_compare_numeric;
11670 int item_compare_numbers;
11671#ifdef FEAT_FLOAT
11672 int item_compare_float;
11673#endif
11674 char_u *item_compare_func;
11675 partial_T *item_compare_partial;
11676 dict_T *item_compare_selfdict;
11677 int item_compare_func_err;
11678 int item_compare_keep_zero;
11679} sortinfo_T;
11680static sortinfo_T *sortinfo = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011681#define ITEM_COMPARE_FAIL 999
11682
11683/*
11684 * Compare functions for f_sort() and f_uniq() below.
11685 */
11686 static int
11687#ifdef __BORLANDC__
11688_RTLENTRYF
11689#endif
11690item_compare(const void *s1, const void *s2)
11691{
11692 sortItem_T *si1, *si2;
11693 typval_T *tv1, *tv2;
11694 char_u *p1, *p2;
11695 char_u *tofree1 = NULL, *tofree2 = NULL;
11696 int res;
11697 char_u numbuf1[NUMBUFLEN];
11698 char_u numbuf2[NUMBUFLEN];
11699
11700 si1 = (sortItem_T *)s1;
11701 si2 = (sortItem_T *)s2;
11702 tv1 = &si1->item->li_tv;
11703 tv2 = &si2->item->li_tv;
11704
11705 if (sortinfo->item_compare_numbers)
11706 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011707 varnumber_T v1 = tv_get_number(tv1);
11708 varnumber_T v2 = tv_get_number(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011709
11710 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11711 }
11712
11713#ifdef FEAT_FLOAT
11714 if (sortinfo->item_compare_float)
11715 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011716 float_T v1 = tv_get_float(tv1);
11717 float_T v2 = tv_get_float(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011718
11719 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11720 }
11721#endif
11722
11723 /* tv2string() puts quotes around a string and allocates memory. Don't do
11724 * that for string variables. Use a single quote when comparing with a
11725 * non-string to do what the docs promise. */
11726 if (tv1->v_type == VAR_STRING)
11727 {
11728 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11729 p1 = (char_u *)"'";
11730 else
11731 p1 = tv1->vval.v_string;
11732 }
11733 else
11734 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
11735 if (tv2->v_type == VAR_STRING)
11736 {
11737 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11738 p2 = (char_u *)"'";
11739 else
11740 p2 = tv2->vval.v_string;
11741 }
11742 else
11743 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
11744 if (p1 == NULL)
11745 p1 = (char_u *)"";
11746 if (p2 == NULL)
11747 p2 = (char_u *)"";
11748 if (!sortinfo->item_compare_numeric)
11749 {
11750 if (sortinfo->item_compare_ic)
11751 res = STRICMP(p1, p2);
11752 else
11753 res = STRCMP(p1, p2);
11754 }
11755 else
11756 {
11757 double n1, n2;
11758 n1 = strtod((char *)p1, (char **)&p1);
11759 n2 = strtod((char *)p2, (char **)&p2);
11760 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
11761 }
11762
11763 /* When the result would be zero, compare the item indexes. Makes the
11764 * sort stable. */
11765 if (res == 0 && !sortinfo->item_compare_keep_zero)
11766 res = si1->idx > si2->idx ? 1 : -1;
11767
11768 vim_free(tofree1);
11769 vim_free(tofree2);
11770 return res;
11771}
11772
11773 static int
11774#ifdef __BORLANDC__
11775_RTLENTRYF
11776#endif
11777item_compare2(const void *s1, const void *s2)
11778{
11779 sortItem_T *si1, *si2;
11780 int res;
11781 typval_T rettv;
11782 typval_T argv[3];
11783 int dummy;
11784 char_u *func_name;
11785 partial_T *partial = sortinfo->item_compare_partial;
11786
11787 /* shortcut after failure in previous call; compare all items equal */
11788 if (sortinfo->item_compare_func_err)
11789 return 0;
11790
11791 si1 = (sortItem_T *)s1;
11792 si2 = (sortItem_T *)s2;
11793
11794 if (partial == NULL)
11795 func_name = sortinfo->item_compare_func;
11796 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020011797 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011798
11799 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
11800 * in the copy without changing the original list items. */
11801 copy_tv(&si1->item->li_tv, &argv[0]);
11802 copy_tv(&si2->item->li_tv, &argv[1]);
11803
11804 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
11805 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020011806 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011807 partial, sortinfo->item_compare_selfdict);
11808 clear_tv(&argv[0]);
11809 clear_tv(&argv[1]);
11810
11811 if (res == FAIL)
11812 res = ITEM_COMPARE_FAIL;
11813 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011814 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011815 if (sortinfo->item_compare_func_err)
11816 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
11817 clear_tv(&rettv);
11818
11819 /* When the result would be zero, compare the pointers themselves. Makes
11820 * the sort stable. */
11821 if (res == 0 && !sortinfo->item_compare_keep_zero)
11822 res = si1->idx > si2->idx ? 1 : -1;
11823
11824 return res;
11825}
11826
11827/*
11828 * "sort({list})" function
11829 */
11830 static void
11831do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
11832{
11833 list_T *l;
11834 listitem_T *li;
11835 sortItem_T *ptrs;
11836 sortinfo_T *old_sortinfo;
11837 sortinfo_T info;
11838 long len;
11839 long i;
11840
11841 /* Pointer to current info struct used in compare function. Save and
11842 * restore the current one for nested calls. */
11843 old_sortinfo = sortinfo;
11844 sortinfo = &info;
11845
11846 if (argvars[0].v_type != VAR_LIST)
11847 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
11848 else
11849 {
11850 l = argvars[0].vval.v_list;
11851 if (l == NULL || tv_check_lock(l->lv_lock,
11852 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
11853 TRUE))
11854 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011855 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011856
11857 len = list_len(l);
11858 if (len <= 1)
11859 goto theend; /* short list sorts pretty quickly */
11860
11861 info.item_compare_ic = FALSE;
11862 info.item_compare_numeric = FALSE;
11863 info.item_compare_numbers = FALSE;
11864#ifdef FEAT_FLOAT
11865 info.item_compare_float = FALSE;
11866#endif
11867 info.item_compare_func = NULL;
11868 info.item_compare_partial = NULL;
11869 info.item_compare_selfdict = NULL;
11870 if (argvars[1].v_type != VAR_UNKNOWN)
11871 {
11872 /* optional second argument: {func} */
11873 if (argvars[1].v_type == VAR_FUNC)
11874 info.item_compare_func = argvars[1].vval.v_string;
11875 else if (argvars[1].v_type == VAR_PARTIAL)
11876 info.item_compare_partial = argvars[1].vval.v_partial;
11877 else
11878 {
11879 int error = FALSE;
11880
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011881 i = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011882 if (error)
11883 goto theend; /* type error; errmsg already given */
11884 if (i == 1)
11885 info.item_compare_ic = TRUE;
11886 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011887 info.item_compare_func = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011888 else if (i != 0)
11889 {
11890 EMSG(_(e_invarg));
11891 goto theend;
11892 }
11893 if (info.item_compare_func != NULL)
11894 {
11895 if (*info.item_compare_func == NUL)
11896 {
11897 /* empty string means default sort */
11898 info.item_compare_func = NULL;
11899 }
11900 else if (STRCMP(info.item_compare_func, "n") == 0)
11901 {
11902 info.item_compare_func = NULL;
11903 info.item_compare_numeric = TRUE;
11904 }
11905 else if (STRCMP(info.item_compare_func, "N") == 0)
11906 {
11907 info.item_compare_func = NULL;
11908 info.item_compare_numbers = TRUE;
11909 }
11910#ifdef FEAT_FLOAT
11911 else if (STRCMP(info.item_compare_func, "f") == 0)
11912 {
11913 info.item_compare_func = NULL;
11914 info.item_compare_float = TRUE;
11915 }
11916#endif
11917 else if (STRCMP(info.item_compare_func, "i") == 0)
11918 {
11919 info.item_compare_func = NULL;
11920 info.item_compare_ic = TRUE;
11921 }
11922 }
11923 }
11924
11925 if (argvars[2].v_type != VAR_UNKNOWN)
11926 {
11927 /* optional third argument: {dict} */
11928 if (argvars[2].v_type != VAR_DICT)
11929 {
11930 EMSG(_(e_dictreq));
11931 goto theend;
11932 }
11933 info.item_compare_selfdict = argvars[2].vval.v_dict;
11934 }
11935 }
11936
11937 /* Make an array with each entry pointing to an item in the List. */
11938 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
11939 if (ptrs == NULL)
11940 goto theend;
11941
11942 i = 0;
11943 if (sort)
11944 {
11945 /* sort(): ptrs will be the list to sort */
11946 for (li = l->lv_first; li != NULL; li = li->li_next)
11947 {
11948 ptrs[i].item = li;
11949 ptrs[i].idx = i;
11950 ++i;
11951 }
11952
11953 info.item_compare_func_err = FALSE;
11954 info.item_compare_keep_zero = FALSE;
11955 /* test the compare function */
11956 if ((info.item_compare_func != NULL
11957 || info.item_compare_partial != NULL)
11958 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
11959 == ITEM_COMPARE_FAIL)
11960 EMSG(_("E702: Sort compare function failed"));
11961 else
11962 {
11963 /* Sort the array with item pointers. */
11964 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
11965 info.item_compare_func == NULL
11966 && info.item_compare_partial == NULL
11967 ? item_compare : item_compare2);
11968
11969 if (!info.item_compare_func_err)
11970 {
11971 /* Clear the List and append the items in sorted order. */
11972 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
11973 l->lv_len = 0;
11974 for (i = 0; i < len; ++i)
11975 list_append(l, ptrs[i].item);
11976 }
11977 }
11978 }
11979 else
11980 {
11981 int (*item_compare_func_ptr)(const void *, const void *);
11982
11983 /* f_uniq(): ptrs will be a stack of items to remove */
11984 info.item_compare_func_err = FALSE;
11985 info.item_compare_keep_zero = TRUE;
11986 item_compare_func_ptr = info.item_compare_func != NULL
11987 || info.item_compare_partial != NULL
11988 ? item_compare2 : item_compare;
11989
11990 for (li = l->lv_first; li != NULL && li->li_next != NULL;
11991 li = li->li_next)
11992 {
11993 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
11994 == 0)
11995 ptrs[i++].item = li;
11996 if (info.item_compare_func_err)
11997 {
11998 EMSG(_("E882: Uniq compare function failed"));
11999 break;
12000 }
12001 }
12002
12003 if (!info.item_compare_func_err)
12004 {
12005 while (--i >= 0)
12006 {
12007 li = ptrs[i].item->li_next;
12008 ptrs[i].item->li_next = li->li_next;
12009 if (li->li_next != NULL)
12010 li->li_next->li_prev = ptrs[i].item;
12011 else
12012 l->lv_last = ptrs[i].item;
12013 list_fix_watch(l, li);
12014 listitem_free(li);
12015 l->lv_len--;
12016 }
12017 }
12018 }
12019
12020 vim_free(ptrs);
12021 }
12022theend:
12023 sortinfo = old_sortinfo;
12024}
12025
12026/*
12027 * "sort({list})" function
12028 */
12029 static void
12030f_sort(typval_T *argvars, typval_T *rettv)
12031{
12032 do_sort_uniq(argvars, rettv, TRUE);
12033}
12034
12035/*
12036 * "uniq({list})" function
12037 */
12038 static void
12039f_uniq(typval_T *argvars, typval_T *rettv)
12040{
12041 do_sort_uniq(argvars, rettv, FALSE);
12042}
12043
12044/*
12045 * "soundfold({word})" function
12046 */
12047 static void
12048f_soundfold(typval_T *argvars, typval_T *rettv)
12049{
12050 char_u *s;
12051
12052 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012053 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012054#ifdef FEAT_SPELL
12055 rettv->vval.v_string = eval_soundfold(s);
12056#else
12057 rettv->vval.v_string = vim_strsave(s);
12058#endif
12059}
12060
12061/*
12062 * "spellbadword()" function
12063 */
12064 static void
12065f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
12066{
12067 char_u *word = (char_u *)"";
12068 hlf_T attr = HLF_COUNT;
12069 int len = 0;
12070
12071 if (rettv_list_alloc(rettv) == FAIL)
12072 return;
12073
12074#ifdef FEAT_SPELL
12075 if (argvars[0].v_type == VAR_UNKNOWN)
12076 {
12077 /* Find the start and length of the badly spelled word. */
12078 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
12079 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012080 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012081 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012082 curwin->w_set_curswant = TRUE;
12083 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012084 }
12085 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
12086 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012087 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012088 int capcol = -1;
12089
12090 if (str != NULL)
12091 {
12092 /* Check the argument for spelling. */
12093 while (*str != NUL)
12094 {
12095 len = spell_check(curwin, str, &attr, &capcol, FALSE);
12096 if (attr != HLF_COUNT)
12097 {
12098 word = str;
12099 break;
12100 }
12101 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020012102 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012103 }
12104 }
12105 }
12106#endif
12107
12108 list_append_string(rettv->vval.v_list, word, len);
12109 list_append_string(rettv->vval.v_list, (char_u *)(
12110 attr == HLF_SPB ? "bad" :
12111 attr == HLF_SPR ? "rare" :
12112 attr == HLF_SPL ? "local" :
12113 attr == HLF_SPC ? "caps" :
12114 ""), -1);
12115}
12116
12117/*
12118 * "spellsuggest()" function
12119 */
12120 static void
12121f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
12122{
12123#ifdef FEAT_SPELL
12124 char_u *str;
12125 int typeerr = FALSE;
12126 int maxcount;
12127 garray_T ga;
12128 int i;
12129 listitem_T *li;
12130 int need_capital = FALSE;
12131#endif
12132
12133 if (rettv_list_alloc(rettv) == FAIL)
12134 return;
12135
12136#ifdef FEAT_SPELL
12137 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
12138 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012139 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012140 if (argvars[1].v_type != VAR_UNKNOWN)
12141 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012142 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012143 if (maxcount <= 0)
12144 return;
12145 if (argvars[2].v_type != VAR_UNKNOWN)
12146 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012147 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012148 if (typeerr)
12149 return;
12150 }
12151 }
12152 else
12153 maxcount = 25;
12154
12155 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
12156
12157 for (i = 0; i < ga.ga_len; ++i)
12158 {
12159 str = ((char_u **)ga.ga_data)[i];
12160
12161 li = listitem_alloc();
12162 if (li == NULL)
12163 vim_free(str);
12164 else
12165 {
12166 li->li_tv.v_type = VAR_STRING;
12167 li->li_tv.v_lock = 0;
12168 li->li_tv.vval.v_string = str;
12169 list_append(rettv->vval.v_list, li);
12170 }
12171 }
12172 ga_clear(&ga);
12173 }
12174#endif
12175}
12176
12177 static void
12178f_split(typval_T *argvars, typval_T *rettv)
12179{
12180 char_u *str;
12181 char_u *end;
12182 char_u *pat = NULL;
12183 regmatch_T regmatch;
12184 char_u patbuf[NUMBUFLEN];
12185 char_u *save_cpo;
12186 int match;
12187 colnr_T col = 0;
12188 int keepempty = FALSE;
12189 int typeerr = FALSE;
12190
12191 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12192 save_cpo = p_cpo;
12193 p_cpo = (char_u *)"";
12194
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012195 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012196 if (argvars[1].v_type != VAR_UNKNOWN)
12197 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012198 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012199 if (pat == NULL)
12200 typeerr = TRUE;
12201 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012202 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012203 }
12204 if (pat == NULL || *pat == NUL)
12205 pat = (char_u *)"[\\x01- ]\\+";
12206
12207 if (rettv_list_alloc(rettv) == FAIL)
12208 return;
12209 if (typeerr)
12210 return;
12211
12212 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12213 if (regmatch.regprog != NULL)
12214 {
12215 regmatch.rm_ic = FALSE;
12216 while (*str != NUL || keepempty)
12217 {
12218 if (*str == NUL)
12219 match = FALSE; /* empty item at the end */
12220 else
12221 match = vim_regexec_nl(&regmatch, str, col);
12222 if (match)
12223 end = regmatch.startp[0];
12224 else
12225 end = str + STRLEN(str);
12226 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
12227 && *str != NUL && match && end < regmatch.endp[0]))
12228 {
12229 if (list_append_string(rettv->vval.v_list, str,
12230 (int)(end - str)) == FAIL)
12231 break;
12232 }
12233 if (!match)
12234 break;
12235 /* Advance to just after the match. */
12236 if (regmatch.endp[0] > str)
12237 col = 0;
12238 else
12239 {
12240 /* Don't get stuck at the same match. */
12241#ifdef FEAT_MBYTE
12242 col = (*mb_ptr2len)(regmatch.endp[0]);
12243#else
12244 col = 1;
12245#endif
12246 }
12247 str = regmatch.endp[0];
12248 }
12249
12250 vim_regfree(regmatch.regprog);
12251 }
12252
12253 p_cpo = save_cpo;
12254}
12255
12256#ifdef FEAT_FLOAT
12257/*
12258 * "sqrt()" function
12259 */
12260 static void
12261f_sqrt(typval_T *argvars, typval_T *rettv)
12262{
12263 float_T f = 0.0;
12264
12265 rettv->v_type = VAR_FLOAT;
12266 if (get_float_arg(argvars, &f) == OK)
12267 rettv->vval.v_float = sqrt(f);
12268 else
12269 rettv->vval.v_float = 0.0;
12270}
12271
12272/*
12273 * "str2float()" function
12274 */
12275 static void
12276f_str2float(typval_T *argvars, typval_T *rettv)
12277{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012278 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010012279 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012280
Bram Moolenaar08243d22017-01-10 16:12:29 +010012281 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012282 p = skipwhite(p + 1);
12283 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010012284 if (isneg)
12285 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012286 rettv->v_type = VAR_FLOAT;
12287}
12288#endif
12289
12290/*
12291 * "str2nr()" function
12292 */
12293 static void
12294f_str2nr(typval_T *argvars, typval_T *rettv)
12295{
12296 int base = 10;
12297 char_u *p;
12298 varnumber_T n;
12299 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010012300 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012301
12302 if (argvars[1].v_type != VAR_UNKNOWN)
12303 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012304 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012305 if (base != 2 && base != 8 && base != 10 && base != 16)
12306 {
12307 EMSG(_(e_invarg));
12308 return;
12309 }
12310 }
12311
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012312 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010012313 isneg = (*p == '-');
12314 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012315 p = skipwhite(p + 1);
12316 switch (base)
12317 {
12318 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
12319 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
12320 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
12321 default: what = 0;
12322 }
12323 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010012324 if (isneg)
12325 rettv->vval.v_number = -n;
12326 else
12327 rettv->vval.v_number = n;
12328
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012329}
12330
12331#ifdef HAVE_STRFTIME
12332/*
12333 * "strftime({format}[, {time}])" function
12334 */
12335 static void
12336f_strftime(typval_T *argvars, typval_T *rettv)
12337{
12338 char_u result_buf[256];
12339 struct tm *curtime;
12340 time_t seconds;
12341 char_u *p;
12342
12343 rettv->v_type = VAR_STRING;
12344
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012345 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012346 if (argvars[1].v_type == VAR_UNKNOWN)
12347 seconds = time(NULL);
12348 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012349 seconds = (time_t)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012350 curtime = localtime(&seconds);
12351 /* MSVC returns NULL for an invalid value of seconds. */
12352 if (curtime == NULL)
12353 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
12354 else
12355 {
12356# ifdef FEAT_MBYTE
12357 vimconv_T conv;
12358 char_u *enc;
12359
12360 conv.vc_type = CONV_NONE;
12361 enc = enc_locale();
12362 convert_setup(&conv, p_enc, enc);
12363 if (conv.vc_type != CONV_NONE)
12364 p = string_convert(&conv, p, NULL);
12365# endif
12366 if (p != NULL)
12367 (void)strftime((char *)result_buf, sizeof(result_buf),
12368 (char *)p, curtime);
12369 else
12370 result_buf[0] = NUL;
12371
12372# ifdef FEAT_MBYTE
12373 if (conv.vc_type != CONV_NONE)
12374 vim_free(p);
12375 convert_setup(&conv, enc, p_enc);
12376 if (conv.vc_type != CONV_NONE)
12377 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
12378 else
12379# endif
12380 rettv->vval.v_string = vim_strsave(result_buf);
12381
12382# ifdef FEAT_MBYTE
12383 /* Release conversion descriptors */
12384 convert_setup(&conv, NULL, NULL);
12385 vim_free(enc);
12386# endif
12387 }
12388}
12389#endif
12390
12391/*
12392 * "strgetchar()" function
12393 */
12394 static void
12395f_strgetchar(typval_T *argvars, typval_T *rettv)
12396{
12397 char_u *str;
12398 int len;
12399 int error = FALSE;
12400 int charidx;
12401
12402 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012403 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012404 if (str == NULL)
12405 return;
12406 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012407 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012408 if (error)
12409 return;
12410#ifdef FEAT_MBYTE
12411 {
12412 int byteidx = 0;
12413
12414 while (charidx >= 0 && byteidx < len)
12415 {
12416 if (charidx == 0)
12417 {
12418 rettv->vval.v_number = mb_ptr2char(str + byteidx);
12419 break;
12420 }
12421 --charidx;
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012422 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012423 }
12424 }
12425#else
12426 if (charidx < len)
12427 rettv->vval.v_number = str[charidx];
12428#endif
12429}
12430
12431/*
12432 * "stridx()" function
12433 */
12434 static void
12435f_stridx(typval_T *argvars, typval_T *rettv)
12436{
12437 char_u buf[NUMBUFLEN];
12438 char_u *needle;
12439 char_u *haystack;
12440 char_u *save_haystack;
12441 char_u *pos;
12442 int start_idx;
12443
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012444 needle = tv_get_string_chk(&argvars[1]);
12445 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012446 rettv->vval.v_number = -1;
12447 if (needle == NULL || haystack == NULL)
12448 return; /* type error; errmsg already given */
12449
12450 if (argvars[2].v_type != VAR_UNKNOWN)
12451 {
12452 int error = FALSE;
12453
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012454 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012455 if (error || start_idx >= (int)STRLEN(haystack))
12456 return;
12457 if (start_idx >= 0)
12458 haystack += start_idx;
12459 }
12460
12461 pos = (char_u *)strstr((char *)haystack, (char *)needle);
12462 if (pos != NULL)
12463 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
12464}
12465
12466/*
12467 * "string()" function
12468 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010012469 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012470f_string(typval_T *argvars, typval_T *rettv)
12471{
12472 char_u *tofree;
12473 char_u numbuf[NUMBUFLEN];
12474
12475 rettv->v_type = VAR_STRING;
12476 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
12477 get_copyID());
12478 /* Make a copy if we have a value but it's not in allocated memory. */
12479 if (rettv->vval.v_string != NULL && tofree == NULL)
12480 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
12481}
12482
12483/*
12484 * "strlen()" function
12485 */
12486 static void
12487f_strlen(typval_T *argvars, typval_T *rettv)
12488{
12489 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012490 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012491}
12492
12493/*
12494 * "strchars()" function
12495 */
12496 static void
12497f_strchars(typval_T *argvars, typval_T *rettv)
12498{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012499 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012500 int skipcc = 0;
12501#ifdef FEAT_MBYTE
12502 varnumber_T len = 0;
12503 int (*func_mb_ptr2char_adv)(char_u **pp);
12504#endif
12505
12506 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012507 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012508 if (skipcc < 0 || skipcc > 1)
12509 EMSG(_(e_invarg));
12510 else
12511 {
12512#ifdef FEAT_MBYTE
12513 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
12514 while (*s != NUL)
12515 {
12516 func_mb_ptr2char_adv(&s);
12517 ++len;
12518 }
12519 rettv->vval.v_number = len;
12520#else
12521 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
12522#endif
12523 }
12524}
12525
12526/*
12527 * "strdisplaywidth()" function
12528 */
12529 static void
12530f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
12531{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012532 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012533 int col = 0;
12534
12535 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012536 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012537
12538 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
12539}
12540
12541/*
12542 * "strwidth()" function
12543 */
12544 static void
12545f_strwidth(typval_T *argvars, typval_T *rettv)
12546{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012547 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012548
12549 rettv->vval.v_number = (varnumber_T)(
12550#ifdef FEAT_MBYTE
12551 mb_string2cells(s, -1)
12552#else
12553 STRLEN(s)
12554#endif
12555 );
12556}
12557
12558/*
12559 * "strcharpart()" function
12560 */
12561 static void
12562f_strcharpart(typval_T *argvars, typval_T *rettv)
12563{
12564#ifdef FEAT_MBYTE
12565 char_u *p;
12566 int nchar;
12567 int nbyte = 0;
12568 int charlen;
12569 int len = 0;
12570 int slen;
12571 int error = FALSE;
12572
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012573 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012574 slen = (int)STRLEN(p);
12575
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012576 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012577 if (!error)
12578 {
12579 if (nchar > 0)
12580 while (nchar > 0 && nbyte < slen)
12581 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012582 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012583 --nchar;
12584 }
12585 else
12586 nbyte = nchar;
12587 if (argvars[2].v_type != VAR_UNKNOWN)
12588 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012589 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012590 while (charlen > 0 && nbyte + len < slen)
12591 {
12592 int off = nbyte + len;
12593
12594 if (off < 0)
12595 len += 1;
12596 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012597 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012598 --charlen;
12599 }
12600 }
12601 else
12602 len = slen - nbyte; /* default: all bytes that are available. */
12603 }
12604
12605 /*
12606 * Only return the overlap between the specified part and the actual
12607 * string.
12608 */
12609 if (nbyte < 0)
12610 {
12611 len += nbyte;
12612 nbyte = 0;
12613 }
12614 else if (nbyte > slen)
12615 nbyte = slen;
12616 if (len < 0)
12617 len = 0;
12618 else if (nbyte + len > slen)
12619 len = slen - nbyte;
12620
12621 rettv->v_type = VAR_STRING;
12622 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
12623#else
12624 f_strpart(argvars, rettv);
12625#endif
12626}
12627
12628/*
12629 * "strpart()" function
12630 */
12631 static void
12632f_strpart(typval_T *argvars, typval_T *rettv)
12633{
12634 char_u *p;
12635 int n;
12636 int len;
12637 int slen;
12638 int error = FALSE;
12639
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012640 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012641 slen = (int)STRLEN(p);
12642
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012643 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012644 if (error)
12645 len = 0;
12646 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012647 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012648 else
12649 len = slen - n; /* default len: all bytes that are available. */
12650
12651 /*
12652 * Only return the overlap between the specified part and the actual
12653 * string.
12654 */
12655 if (n < 0)
12656 {
12657 len += n;
12658 n = 0;
12659 }
12660 else if (n > slen)
12661 n = slen;
12662 if (len < 0)
12663 len = 0;
12664 else if (n + len > slen)
12665 len = slen - n;
12666
12667 rettv->v_type = VAR_STRING;
12668 rettv->vval.v_string = vim_strnsave(p + n, len);
12669}
12670
12671/*
12672 * "strridx()" function
12673 */
12674 static void
12675f_strridx(typval_T *argvars, typval_T *rettv)
12676{
12677 char_u buf[NUMBUFLEN];
12678 char_u *needle;
12679 char_u *haystack;
12680 char_u *rest;
12681 char_u *lastmatch = NULL;
12682 int haystack_len, end_idx;
12683
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012684 needle = tv_get_string_chk(&argvars[1]);
12685 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012686
12687 rettv->vval.v_number = -1;
12688 if (needle == NULL || haystack == NULL)
12689 return; /* type error; errmsg already given */
12690
12691 haystack_len = (int)STRLEN(haystack);
12692 if (argvars[2].v_type != VAR_UNKNOWN)
12693 {
12694 /* Third argument: upper limit for index */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012695 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012696 if (end_idx < 0)
12697 return; /* can never find a match */
12698 }
12699 else
12700 end_idx = haystack_len;
12701
12702 if (*needle == NUL)
12703 {
12704 /* Empty string matches past the end. */
12705 lastmatch = haystack + end_idx;
12706 }
12707 else
12708 {
12709 for (rest = haystack; *rest != '\0'; ++rest)
12710 {
12711 rest = (char_u *)strstr((char *)rest, (char *)needle);
12712 if (rest == NULL || rest > haystack + end_idx)
12713 break;
12714 lastmatch = rest;
12715 }
12716 }
12717
12718 if (lastmatch == NULL)
12719 rettv->vval.v_number = -1;
12720 else
12721 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
12722}
12723
12724/*
12725 * "strtrans()" function
12726 */
12727 static void
12728f_strtrans(typval_T *argvars, typval_T *rettv)
12729{
12730 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012731 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012732}
12733
12734/*
12735 * "submatch()" function
12736 */
12737 static void
12738f_submatch(typval_T *argvars, typval_T *rettv)
12739{
12740 int error = FALSE;
12741 int no;
12742 int retList = 0;
12743
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012744 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012745 if (error)
12746 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012747 if (no < 0 || no >= NSUBEXP)
12748 {
Bram Moolenaar79518e22017-02-17 16:31:35 +010012749 EMSGN(_("E935: invalid submatch number: %d"), no);
12750 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012751 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012752 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012753 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012754 if (error)
12755 return;
12756
12757 if (retList == 0)
12758 {
12759 rettv->v_type = VAR_STRING;
12760 rettv->vval.v_string = reg_submatch(no);
12761 }
12762 else
12763 {
12764 rettv->v_type = VAR_LIST;
12765 rettv->vval.v_list = reg_submatch_list(no);
12766 }
12767}
12768
12769/*
12770 * "substitute()" function
12771 */
12772 static void
12773f_substitute(typval_T *argvars, typval_T *rettv)
12774{
12775 char_u patbuf[NUMBUFLEN];
12776 char_u subbuf[NUMBUFLEN];
12777 char_u flagsbuf[NUMBUFLEN];
12778
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012779 char_u *str = tv_get_string_chk(&argvars[0]);
12780 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012781 char_u *sub = NULL;
12782 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012783 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012784
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012785 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
12786 expr = &argvars[2];
12787 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012788 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012789
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012790 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012791 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
12792 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012793 rettv->vval.v_string = NULL;
12794 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012795 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012796}
12797
12798/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020012799 * "swapinfo(swap_filename)" function
12800 */
12801 static void
12802f_swapinfo(typval_T *argvars, typval_T *rettv)
12803{
12804 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012805 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +020012806}
12807
12808/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020012809 * "swapname(expr)" function
12810 */
12811 static void
12812f_swapname(typval_T *argvars, typval_T *rettv)
12813{
12814 buf_T *buf;
12815
12816 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010012817 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +020012818 if (buf == NULL || buf->b_ml.ml_mfp == NULL
12819 || buf->b_ml.ml_mfp->mf_fname == NULL)
12820 rettv->vval.v_string = NULL;
12821 else
12822 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
12823}
12824
12825/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012826 * "synID(lnum, col, trans)" function
12827 */
12828 static void
12829f_synID(typval_T *argvars UNUSED, typval_T *rettv)
12830{
12831 int id = 0;
12832#ifdef FEAT_SYN_HL
12833 linenr_T lnum;
12834 colnr_T col;
12835 int trans;
12836 int transerr = FALSE;
12837
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012838 lnum = tv_get_lnum(argvars); /* -1 on type error */
12839 col = (linenr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
12840 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012841
12842 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12843 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
12844 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
12845#endif
12846
12847 rettv->vval.v_number = id;
12848}
12849
12850/*
12851 * "synIDattr(id, what [, mode])" function
12852 */
12853 static void
12854f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
12855{
12856 char_u *p = NULL;
12857#ifdef FEAT_SYN_HL
12858 int id;
12859 char_u *what;
12860 char_u *mode;
12861 char_u modebuf[NUMBUFLEN];
12862 int modec;
12863
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012864 id = (int)tv_get_number(&argvars[0]);
12865 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012866 if (argvars[2].v_type != VAR_UNKNOWN)
12867 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012868 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012869 modec = TOLOWER_ASC(mode[0]);
12870 if (modec != 't' && modec != 'c' && modec != 'g')
12871 modec = 0; /* replace invalid with current */
12872 }
12873 else
12874 {
12875#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
12876 if (USE_24BIT)
12877 modec = 'g';
12878 else
12879#endif
12880 if (t_colors > 1)
12881 modec = 'c';
12882 else
12883 modec = 't';
12884 }
12885
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012886 switch (TOLOWER_ASC(what[0]))
12887 {
12888 case 'b':
12889 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
12890 p = highlight_color(id, what, modec);
12891 else /* bold */
12892 p = highlight_has_attr(id, HL_BOLD, modec);
12893 break;
12894
12895 case 'f': /* fg[#] or font */
12896 p = highlight_color(id, what, modec);
12897 break;
12898
12899 case 'i':
12900 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
12901 p = highlight_has_attr(id, HL_INVERSE, modec);
12902 else /* italic */
12903 p = highlight_has_attr(id, HL_ITALIC, modec);
12904 break;
12905
12906 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020012907 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012908 break;
12909
12910 case 'r': /* reverse */
12911 p = highlight_has_attr(id, HL_INVERSE, modec);
12912 break;
12913
12914 case 's':
12915 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
12916 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020012917 /* strikeout */
12918 else if (TOLOWER_ASC(what[1]) == 't' &&
12919 TOLOWER_ASC(what[2]) == 'r')
12920 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012921 else /* standout */
12922 p = highlight_has_attr(id, HL_STANDOUT, modec);
12923 break;
12924
12925 case 'u':
12926 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
12927 /* underline */
12928 p = highlight_has_attr(id, HL_UNDERLINE, modec);
12929 else
12930 /* undercurl */
12931 p = highlight_has_attr(id, HL_UNDERCURL, modec);
12932 break;
12933 }
12934
12935 if (p != NULL)
12936 p = vim_strsave(p);
12937#endif
12938 rettv->v_type = VAR_STRING;
12939 rettv->vval.v_string = p;
12940}
12941
12942/*
12943 * "synIDtrans(id)" function
12944 */
12945 static void
12946f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
12947{
12948 int id;
12949
12950#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012951 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012952
12953 if (id > 0)
12954 id = syn_get_final_id(id);
12955 else
12956#endif
12957 id = 0;
12958
12959 rettv->vval.v_number = id;
12960}
12961
12962/*
12963 * "synconcealed(lnum, col)" function
12964 */
12965 static void
12966f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
12967{
12968#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12969 linenr_T lnum;
12970 colnr_T col;
12971 int syntax_flags = 0;
12972 int cchar;
12973 int matchid = 0;
12974 char_u str[NUMBUFLEN];
12975#endif
12976
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012977 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012978
12979#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012980 lnum = tv_get_lnum(argvars); /* -1 on type error */
12981 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012982
12983 vim_memset(str, NUL, sizeof(str));
12984
12985 if (rettv_list_alloc(rettv) != FAIL)
12986 {
12987 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12988 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12989 && curwin->w_p_cole > 0)
12990 {
12991 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
12992 syntax_flags = get_syntax_info(&matchid);
12993
12994 /* get the conceal character */
12995 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
12996 {
12997 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020012998 if (cchar == NUL && curwin->w_p_cole == 1)
12999 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013000 if (cchar != NUL)
13001 {
13002# ifdef FEAT_MBYTE
13003 if (has_mbyte)
13004 (*mb_char2bytes)(cchar, str);
13005 else
13006# endif
13007 str[0] = cchar;
13008 }
13009 }
13010 }
13011
13012 list_append_number(rettv->vval.v_list,
13013 (syntax_flags & HL_CONCEAL) != 0);
13014 /* -1 to auto-determine strlen */
13015 list_append_string(rettv->vval.v_list, str, -1);
13016 list_append_number(rettv->vval.v_list, matchid);
13017 }
13018#endif
13019}
13020
13021/*
13022 * "synstack(lnum, col)" function
13023 */
13024 static void
13025f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
13026{
13027#ifdef FEAT_SYN_HL
13028 linenr_T lnum;
13029 colnr_T col;
13030 int i;
13031 int id;
13032#endif
13033
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013034 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013035
13036#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013037 lnum = tv_get_lnum(argvars); /* -1 on type error */
13038 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013039
13040 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13041 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13042 && rettv_list_alloc(rettv) != FAIL)
13043 {
13044 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
13045 for (i = 0; ; ++i)
13046 {
13047 id = syn_get_stack_item(i);
13048 if (id < 0)
13049 break;
13050 if (list_append_number(rettv->vval.v_list, id) == FAIL)
13051 break;
13052 }
13053 }
13054#endif
13055}
13056
13057 static void
13058get_cmd_output_as_rettv(
13059 typval_T *argvars,
13060 typval_T *rettv,
13061 int retlist)
13062{
13063 char_u *res = NULL;
13064 char_u *p;
13065 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013066 int err = FALSE;
13067 FILE *fd;
13068 list_T *list = NULL;
13069 int flags = SHELL_SILENT;
13070
13071 rettv->v_type = VAR_STRING;
13072 rettv->vval.v_string = NULL;
13073 if (check_restricted() || check_secure())
13074 goto errret;
13075
13076 if (argvars[1].v_type != VAR_UNKNOWN)
13077 {
13078 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010013079 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013080 * command.
13081 */
13082 if ((infile = vim_tempname('i', TRUE)) == NULL)
13083 {
13084 EMSG(_(e_notmp));
13085 goto errret;
13086 }
13087
13088 fd = mch_fopen((char *)infile, WRITEBIN);
13089 if (fd == NULL)
13090 {
13091 EMSG2(_(e_notopen), infile);
13092 goto errret;
13093 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010013094 if (argvars[1].v_type == VAR_NUMBER)
13095 {
13096 linenr_T lnum;
13097 buf_T *buf;
13098
13099 buf = buflist_findnr(argvars[1].vval.v_number);
13100 if (buf == NULL)
13101 {
13102 EMSGN(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010013103 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010013104 goto errret;
13105 }
13106
13107 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
13108 {
13109 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
13110 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
13111 {
13112 err = TRUE;
13113 break;
13114 }
13115 if (putc(NL, fd) == EOF)
13116 {
13117 err = TRUE;
13118 break;
13119 }
13120 }
13121 }
13122 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013123 {
13124 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
13125 err = TRUE;
13126 }
13127 else
13128 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010013129 size_t len;
13130 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013131
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013132 p = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013133 if (p == NULL)
13134 {
13135 fclose(fd);
13136 goto errret; /* type error; errmsg already given */
13137 }
13138 len = STRLEN(p);
13139 if (len > 0 && fwrite(p, len, 1, fd) != 1)
13140 err = TRUE;
13141 }
13142 if (fclose(fd) != 0)
13143 err = TRUE;
13144 if (err)
13145 {
13146 EMSG(_("E677: Error writing temp file"));
13147 goto errret;
13148 }
13149 }
13150
13151 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
13152 * echoes typeahead, that messes up the display. */
13153 if (!msg_silent)
13154 flags += SHELL_COOKED;
13155
13156 if (retlist)
13157 {
13158 int len;
13159 listitem_T *li;
13160 char_u *s = NULL;
13161 char_u *start;
13162 char_u *end;
13163 int i;
13164
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013165 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013166 if (res == NULL)
13167 goto errret;
13168
13169 list = list_alloc();
13170 if (list == NULL)
13171 goto errret;
13172
13173 for (i = 0; i < len; ++i)
13174 {
13175 start = res + i;
13176 while (i < len && res[i] != NL)
13177 ++i;
13178 end = res + i;
13179
13180 s = alloc((unsigned)(end - start + 1));
13181 if (s == NULL)
13182 goto errret;
13183
13184 for (p = s; start < end; ++p, ++start)
13185 *p = *start == NUL ? NL : *start;
13186 *p = NUL;
13187
13188 li = listitem_alloc();
13189 if (li == NULL)
13190 {
13191 vim_free(s);
13192 goto errret;
13193 }
13194 li->li_tv.v_type = VAR_STRING;
13195 li->li_tv.v_lock = 0;
13196 li->li_tv.vval.v_string = s;
13197 list_append(list, li);
13198 }
13199
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013200 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013201 list = NULL;
13202 }
13203 else
13204 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013205 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013206#ifdef USE_CR
13207 /* translate <CR> into <NL> */
13208 if (res != NULL)
13209 {
13210 char_u *s;
13211
13212 for (s = res; *s; ++s)
13213 {
13214 if (*s == CAR)
13215 *s = NL;
13216 }
13217 }
13218#else
13219# ifdef USE_CRNL
13220 /* translate <CR><NL> into <NL> */
13221 if (res != NULL)
13222 {
13223 char_u *s, *d;
13224
13225 d = res;
13226 for (s = res; *s; ++s)
13227 {
13228 if (s[0] == CAR && s[1] == NL)
13229 ++s;
13230 *d++ = *s;
13231 }
13232 *d = NUL;
13233 }
13234# endif
13235#endif
13236 rettv->vval.v_string = res;
13237 res = NULL;
13238 }
13239
13240errret:
13241 if (infile != NULL)
13242 {
13243 mch_remove(infile);
13244 vim_free(infile);
13245 }
13246 if (res != NULL)
13247 vim_free(res);
13248 if (list != NULL)
13249 list_free(list);
13250}
13251
13252/*
13253 * "system()" function
13254 */
13255 static void
13256f_system(typval_T *argvars, typval_T *rettv)
13257{
13258 get_cmd_output_as_rettv(argvars, rettv, FALSE);
13259}
13260
13261/*
13262 * "systemlist()" function
13263 */
13264 static void
13265f_systemlist(typval_T *argvars, typval_T *rettv)
13266{
13267 get_cmd_output_as_rettv(argvars, rettv, TRUE);
13268}
13269
13270/*
13271 * "tabpagebuflist()" function
13272 */
13273 static void
13274f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13275{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013276 tabpage_T *tp;
13277 win_T *wp = NULL;
13278
13279 if (argvars[0].v_type == VAR_UNKNOWN)
13280 wp = firstwin;
13281 else
13282 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013283 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013284 if (tp != NULL)
13285 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
13286 }
13287 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
13288 {
13289 for (; wp != NULL; wp = wp->w_next)
13290 if (list_append_number(rettv->vval.v_list,
13291 wp->w_buffer->b_fnum) == FAIL)
13292 break;
13293 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013294}
13295
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013296/*
13297 * "tabpagenr()" function
13298 */
13299 static void
13300f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
13301{
13302 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013303 char_u *arg;
13304
13305 if (argvars[0].v_type != VAR_UNKNOWN)
13306 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013307 arg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013308 nr = 0;
13309 if (arg != NULL)
13310 {
13311 if (STRCMP(arg, "$") == 0)
13312 nr = tabpage_index(NULL) - 1;
13313 else
13314 EMSG2(_(e_invexpr2), arg);
13315 }
13316 }
13317 else
13318 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013319 rettv->vval.v_number = nr;
13320}
13321
13322
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013323/*
13324 * Common code for tabpagewinnr() and winnr().
13325 */
13326 static int
13327get_winnr(tabpage_T *tp, typval_T *argvar)
13328{
13329 win_T *twin;
13330 int nr = 1;
13331 win_T *wp;
13332 char_u *arg;
13333
13334 twin = (tp == curtab) ? curwin : tp->tp_curwin;
13335 if (argvar->v_type != VAR_UNKNOWN)
13336 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013337 arg = tv_get_string_chk(argvar);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013338 if (arg == NULL)
13339 nr = 0; /* type error; errmsg already given */
13340 else if (STRCMP(arg, "$") == 0)
13341 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
13342 else if (STRCMP(arg, "#") == 0)
13343 {
13344 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
13345 if (twin == NULL)
13346 nr = 0;
13347 }
13348 else
13349 {
13350 EMSG2(_(e_invexpr2), arg);
13351 nr = 0;
13352 }
13353 }
13354
13355 if (nr > 0)
13356 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
13357 wp != twin; wp = wp->w_next)
13358 {
13359 if (wp == NULL)
13360 {
13361 /* didn't find it in this tabpage */
13362 nr = 0;
13363 break;
13364 }
13365 ++nr;
13366 }
13367 return nr;
13368}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013369
13370/*
13371 * "tabpagewinnr()" function
13372 */
13373 static void
13374f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
13375{
13376 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013377 tabpage_T *tp;
13378
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013379 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013380 if (tp == NULL)
13381 nr = 0;
13382 else
13383 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013384 rettv->vval.v_number = nr;
13385}
13386
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013387/*
13388 * "tagfiles()" function
13389 */
13390 static void
13391f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
13392{
13393 char_u *fname;
13394 tagname_T tn;
13395 int first;
13396
13397 if (rettv_list_alloc(rettv) == FAIL)
13398 return;
13399 fname = alloc(MAXPATHL);
13400 if (fname == NULL)
13401 return;
13402
13403 for (first = TRUE; ; first = FALSE)
13404 if (get_tagfname(&tn, first, fname) == FAIL
13405 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
13406 break;
13407 tagname_free(&tn);
13408 vim_free(fname);
13409}
13410
13411/*
13412 * "taglist()" function
13413 */
13414 static void
13415f_taglist(typval_T *argvars, typval_T *rettv)
13416{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013417 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013418 char_u *tag_pattern;
13419
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013420 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013421
13422 rettv->vval.v_number = FALSE;
13423 if (*tag_pattern == NUL)
13424 return;
13425
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013426 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013427 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013428 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013429 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013430}
13431
13432/*
13433 * "tempname()" function
13434 */
13435 static void
13436f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
13437{
13438 static int x = 'A';
13439
13440 rettv->v_type = VAR_STRING;
13441 rettv->vval.v_string = vim_tempname(x, FALSE);
13442
13443 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
13444 * names. Skip 'I' and 'O', they are used for shell redirection. */
13445 do
13446 {
13447 if (x == 'Z')
13448 x = '0';
13449 else if (x == '9')
13450 x = 'A';
13451 else
13452 {
13453#ifdef EBCDIC
13454 if (x == 'I')
13455 x = 'J';
13456 else if (x == 'R')
13457 x = 'S';
13458 else
13459#endif
13460 ++x;
13461 }
13462 } while (x == 'I' || x == 'O');
13463}
13464
13465#ifdef FEAT_FLOAT
13466/*
13467 * "tan()" function
13468 */
13469 static void
13470f_tan(typval_T *argvars, typval_T *rettv)
13471{
13472 float_T f = 0.0;
13473
13474 rettv->v_type = VAR_FLOAT;
13475 if (get_float_arg(argvars, &f) == OK)
13476 rettv->vval.v_float = tan(f);
13477 else
13478 rettv->vval.v_float = 0.0;
13479}
13480
13481/*
13482 * "tanh()" function
13483 */
13484 static void
13485f_tanh(typval_T *argvars, typval_T *rettv)
13486{
13487 float_T f = 0.0;
13488
13489 rettv->v_type = VAR_FLOAT;
13490 if (get_float_arg(argvars, &f) == OK)
13491 rettv->vval.v_float = tanh(f);
13492 else
13493 rettv->vval.v_float = 0.0;
13494}
13495#endif
13496
13497/*
13498 * "test_alloc_fail(id, countdown, repeat)" function
13499 */
13500 static void
13501f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
13502{
13503 if (argvars[0].v_type != VAR_NUMBER
13504 || argvars[0].vval.v_number <= 0
13505 || argvars[1].v_type != VAR_NUMBER
13506 || argvars[1].vval.v_number < 0
13507 || argvars[2].v_type != VAR_NUMBER)
13508 EMSG(_(e_invarg));
13509 else
13510 {
13511 alloc_fail_id = argvars[0].vval.v_number;
13512 if (alloc_fail_id >= aid_last)
13513 EMSG(_(e_invarg));
13514 alloc_fail_countdown = argvars[1].vval.v_number;
13515 alloc_fail_repeat = argvars[2].vval.v_number;
13516 did_outofmem_msg = FALSE;
13517 }
13518}
13519
13520/*
13521 * "test_autochdir()"
13522 */
13523 static void
13524f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13525{
13526#if defined(FEAT_AUTOCHDIR)
13527 test_autochdir = TRUE;
13528#endif
13529}
13530
13531/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020013532 * "test_feedinput()"
13533 */
13534 static void
13535f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
13536{
13537#ifdef USE_INPUT_BUF
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013538 char_u *val = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5e80de32017-09-03 15:48:12 +020013539
13540 if (val != NULL)
13541 {
13542 trash_input_buf();
13543 add_to_input_buf_csi(val, (int)STRLEN(val));
13544 }
13545#endif
13546}
13547
13548/*
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013549 * "test_option_not_set({name})" function
13550 */
13551 static void
13552f_test_option_not_set(typval_T *argvars, typval_T *rettv UNUSED)
13553{
13554 char_u *name = (char_u *)"";
13555
13556 if (argvars[0].v_type != VAR_STRING)
13557 EMSG(_(e_invarg));
13558 else
13559 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013560 name = tv_get_string(&argvars[0]);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013561 if (reset_option_was_set(name) == FAIL)
13562 EMSG2(_(e_invarg2), name);
13563 }
13564}
13565
13566/*
13567 * "test_override({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013568 */
13569 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013570f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013571{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013572 char_u *name = (char_u *)"";
13573 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013574 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013575
13576 if (argvars[0].v_type != VAR_STRING
13577 || (argvars[1].v_type) != VAR_NUMBER)
13578 EMSG(_(e_invarg));
13579 else
13580 {
Bram Moolenaare38197d2018-12-24 23:35:13 +010013581 name = tv_get_string(&argvars[0]);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013582 val = (int)tv_get_number(&argvars[1]);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013583
13584 if (STRCMP(name, (char_u *)"redraw") == 0)
13585 disable_redraw_for_testing = val;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020013586 else if (STRCMP(name, (char_u *)"redraw_flag") == 0)
13587 ignore_redraw_flag_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013588 else if (STRCMP(name, (char_u *)"char_avail") == 0)
13589 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013590 else if (STRCMP(name, (char_u *)"starting") == 0)
13591 {
13592 if (val)
13593 {
13594 if (save_starting < 0)
13595 save_starting = starting;
13596 starting = 0;
13597 }
13598 else
13599 {
13600 starting = save_starting;
13601 save_starting = -1;
13602 }
13603 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013604 else if (STRCMP(name, (char_u *)"nfa_fail") == 0)
13605 nfa_fail_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013606 else if (STRCMP(name, (char_u *)"ALL") == 0)
13607 {
13608 disable_char_avail_for_testing = FALSE;
13609 disable_redraw_for_testing = FALSE;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020013610 ignore_redraw_flag_for_testing = FALSE;
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013611 nfa_fail_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013612 if (save_starting >= 0)
13613 {
13614 starting = save_starting;
13615 save_starting = -1;
13616 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013617 }
13618 else
13619 EMSG2(_(e_invarg2), name);
13620 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013621}
13622
13623/*
13624 * "test_garbagecollect_now()" function
13625 */
13626 static void
13627f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13628{
13629 /* This is dangerous, any Lists and Dicts used internally may be freed
13630 * while still in use. */
13631 garbage_collect(TRUE);
13632}
13633
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013634/*
13635 * "test_ignore_error()" function
13636 */
13637 static void
13638f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
13639{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013640 ignore_error_for_testing(tv_get_string(&argvars[0]));
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013641}
13642
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013643#ifdef FEAT_JOB_CHANNEL
13644 static void
13645f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
13646{
13647 rettv->v_type = VAR_CHANNEL;
13648 rettv->vval.v_channel = NULL;
13649}
13650#endif
13651
13652 static void
13653f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
13654{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013655 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013656}
13657
13658#ifdef FEAT_JOB_CHANNEL
13659 static void
13660f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
13661{
13662 rettv->v_type = VAR_JOB;
13663 rettv->vval.v_job = NULL;
13664}
13665#endif
13666
13667 static void
13668f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
13669{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013670 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013671}
13672
13673 static void
13674f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
13675{
13676 rettv->v_type = VAR_PARTIAL;
13677 rettv->vval.v_partial = NULL;
13678}
13679
13680 static void
13681f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
13682{
13683 rettv->v_type = VAR_STRING;
13684 rettv->vval.v_string = NULL;
13685}
13686
Bram Moolenaarab186732018-09-14 21:27:06 +020013687#ifdef FEAT_GUI
13688 static void
13689f_test_scrollbar(typval_T *argvars, typval_T *rettv UNUSED)
13690{
13691 char_u *which;
13692 long value;
13693 int dragging;
13694 scrollbar_T *sb = NULL;
13695
13696 if (argvars[0].v_type != VAR_STRING
13697 || (argvars[1].v_type) != VAR_NUMBER
13698 || (argvars[2].v_type) != VAR_NUMBER)
13699 {
13700 EMSG(_(e_invarg));
13701 return;
13702 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013703 which = tv_get_string(&argvars[0]);
13704 value = tv_get_number(&argvars[1]);
13705 dragging = tv_get_number(&argvars[2]);
Bram Moolenaarab186732018-09-14 21:27:06 +020013706
13707 if (STRCMP(which, "left") == 0)
13708 sb = &curwin->w_scrollbars[SBAR_LEFT];
13709 else if (STRCMP(which, "right") == 0)
13710 sb = &curwin->w_scrollbars[SBAR_RIGHT];
13711 else if (STRCMP(which, "hor") == 0)
13712 sb = &gui.bottom_sbar;
13713 if (sb == NULL)
13714 {
13715 EMSG2(_(e_invarg2), which);
13716 return;
13717 }
13718 gui_drag_scrollbar(sb, value, dragging);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020013719# ifndef USE_ON_FLY_SCROLL
13720 // need to loop through normal_cmd() to handle the scroll events
13721 exec_normal(FALSE, TRUE, FALSE);
13722# endif
Bram Moolenaarab186732018-09-14 21:27:06 +020013723}
13724#endif
13725
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013726 static void
13727f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
13728{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013729 time_for_testing = (time_t)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013730}
13731
13732#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
13733/*
13734 * Get a callback from "arg". It can be a Funcref or a function name.
13735 * When "arg" is zero return an empty string.
13736 * Return NULL for an invalid argument.
13737 */
13738 char_u *
13739get_callback(typval_T *arg, partial_T **pp)
13740{
13741 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
13742 {
13743 *pp = arg->vval.v_partial;
13744 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020013745 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013746 }
13747 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020013748 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013749 {
13750 func_ref(arg->vval.v_string);
13751 return arg->vval.v_string;
13752 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013753 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
13754 return (char_u *)"";
13755 EMSG(_("E921: Invalid callback argument"));
13756 return NULL;
13757}
13758
13759/*
Bram Moolenaard5359b22018-04-05 22:44:39 +020013760 * Unref/free "callback" and "partial" returned by get_callback().
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013761 */
13762 void
13763free_callback(char_u *callback, partial_T *partial)
13764{
13765 if (partial != NULL)
13766 partial_unref(partial);
13767 else if (callback != NULL)
13768 {
13769 func_unref(callback);
13770 vim_free(callback);
13771 }
13772}
13773#endif
13774
13775#ifdef FEAT_TIMERS
13776/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020013777 * "timer_info([timer])" function
13778 */
13779 static void
13780f_timer_info(typval_T *argvars, typval_T *rettv)
13781{
13782 timer_T *timer = NULL;
13783
13784 if (rettv_list_alloc(rettv) != OK)
13785 return;
13786 if (argvars[0].v_type != VAR_UNKNOWN)
13787 {
13788 if (argvars[0].v_type != VAR_NUMBER)
13789 EMSG(_(e_number_exp));
13790 else
13791 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013792 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020013793 if (timer != NULL)
13794 add_timer_info(rettv, timer);
13795 }
13796 }
13797 else
13798 add_timer_info_all(rettv);
13799}
13800
13801/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013802 * "timer_pause(timer, paused)" function
13803 */
13804 static void
13805f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
13806{
13807 timer_T *timer = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013808 int paused = (int)tv_get_number(&argvars[1]);
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013809
13810 if (argvars[0].v_type != VAR_NUMBER)
13811 EMSG(_(e_number_exp));
13812 else
13813 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013814 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013815 if (timer != NULL)
13816 timer->tr_paused = paused;
13817 }
13818}
13819
13820/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013821 * "timer_start(time, callback [, options])" function
13822 */
13823 static void
13824f_timer_start(typval_T *argvars, typval_T *rettv)
13825{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013826 long msec = (long)tv_get_number(&argvars[0]);
Bram Moolenaar75537a92016-09-05 22:45:28 +020013827 timer_T *timer;
13828 int repeat = 0;
13829 char_u *callback;
13830 dict_T *dict;
13831 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013832
Bram Moolenaar75537a92016-09-05 22:45:28 +020013833 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013834 if (check_secure())
13835 return;
13836 if (argvars[2].v_type != VAR_UNKNOWN)
13837 {
13838 if (argvars[2].v_type != VAR_DICT
13839 || (dict = argvars[2].vval.v_dict) == NULL)
13840 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013841 EMSG2(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013842 return;
13843 }
13844 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010013845 repeat = dict_get_number(dict, (char_u *)"repeat");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013846 }
13847
Bram Moolenaar75537a92016-09-05 22:45:28 +020013848 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013849 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020013850 return;
13851
13852 timer = create_timer(msec, repeat);
13853 if (timer == NULL)
13854 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013855 else
13856 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020013857 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020013858 timer->tr_callback = vim_strsave(callback);
13859 else
13860 /* pointer into the partial */
13861 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020013862 timer->tr_partial = partial;
13863 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013864 }
13865}
13866
13867/*
13868 * "timer_stop(timer)" function
13869 */
13870 static void
13871f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
13872{
13873 timer_T *timer;
13874
13875 if (argvars[0].v_type != VAR_NUMBER)
13876 {
13877 EMSG(_(e_number_exp));
13878 return;
13879 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013880 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013881 if (timer != NULL)
13882 stop_timer(timer);
13883}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013884
13885/*
13886 * "timer_stopall()" function
13887 */
13888 static void
13889f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13890{
13891 stop_all_timers();
13892}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013893#endif
13894
13895/*
13896 * "tolower(string)" function
13897 */
13898 static void
13899f_tolower(typval_T *argvars, typval_T *rettv)
13900{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013901 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013902 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013903}
13904
13905/*
13906 * "toupper(string)" function
13907 */
13908 static void
13909f_toupper(typval_T *argvars, typval_T *rettv)
13910{
13911 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013912 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013913}
13914
13915/*
13916 * "tr(string, fromstr, tostr)" function
13917 */
13918 static void
13919f_tr(typval_T *argvars, typval_T *rettv)
13920{
13921 char_u *in_str;
13922 char_u *fromstr;
13923 char_u *tostr;
13924 char_u *p;
13925#ifdef FEAT_MBYTE
13926 int inlen;
13927 int fromlen;
13928 int tolen;
13929 int idx;
13930 char_u *cpstr;
13931 int cplen;
13932 int first = TRUE;
13933#endif
13934 char_u buf[NUMBUFLEN];
13935 char_u buf2[NUMBUFLEN];
13936 garray_T ga;
13937
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013938 in_str = tv_get_string(&argvars[0]);
13939 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
13940 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013941
13942 /* Default return value: empty string. */
13943 rettv->v_type = VAR_STRING;
13944 rettv->vval.v_string = NULL;
13945 if (fromstr == NULL || tostr == NULL)
13946 return; /* type error; errmsg already given */
13947 ga_init2(&ga, (int)sizeof(char), 80);
13948
13949#ifdef FEAT_MBYTE
13950 if (!has_mbyte)
13951#endif
13952 /* not multi-byte: fromstr and tostr must be the same length */
13953 if (STRLEN(fromstr) != STRLEN(tostr))
13954 {
13955#ifdef FEAT_MBYTE
13956error:
13957#endif
13958 EMSG2(_(e_invarg2), fromstr);
13959 ga_clear(&ga);
13960 return;
13961 }
13962
13963 /* fromstr and tostr have to contain the same number of chars */
13964 while (*in_str != NUL)
13965 {
13966#ifdef FEAT_MBYTE
13967 if (has_mbyte)
13968 {
13969 inlen = (*mb_ptr2len)(in_str);
13970 cpstr = in_str;
13971 cplen = inlen;
13972 idx = 0;
13973 for (p = fromstr; *p != NUL; p += fromlen)
13974 {
13975 fromlen = (*mb_ptr2len)(p);
13976 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
13977 {
13978 for (p = tostr; *p != NUL; p += tolen)
13979 {
13980 tolen = (*mb_ptr2len)(p);
13981 if (idx-- == 0)
13982 {
13983 cplen = tolen;
13984 cpstr = p;
13985 break;
13986 }
13987 }
13988 if (*p == NUL) /* tostr is shorter than fromstr */
13989 goto error;
13990 break;
13991 }
13992 ++idx;
13993 }
13994
13995 if (first && cpstr == in_str)
13996 {
13997 /* Check that fromstr and tostr have the same number of
13998 * (multi-byte) characters. Done only once when a character
13999 * of in_str doesn't appear in fromstr. */
14000 first = FALSE;
14001 for (p = tostr; *p != NUL; p += tolen)
14002 {
14003 tolen = (*mb_ptr2len)(p);
14004 --idx;
14005 }
14006 if (idx != 0)
14007 goto error;
14008 }
14009
14010 (void)ga_grow(&ga, cplen);
14011 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
14012 ga.ga_len += cplen;
14013
14014 in_str += inlen;
14015 }
14016 else
14017#endif
14018 {
14019 /* When not using multi-byte chars we can do it faster. */
14020 p = vim_strchr(fromstr, *in_str);
14021 if (p != NULL)
14022 ga_append(&ga, tostr[p - fromstr]);
14023 else
14024 ga_append(&ga, *in_str);
14025 ++in_str;
14026 }
14027 }
14028
14029 /* add a terminating NUL */
14030 (void)ga_grow(&ga, 1);
14031 ga_append(&ga, NUL);
14032
14033 rettv->vval.v_string = ga.ga_data;
14034}
14035
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014036/*
14037 * "trim({expr})" function
14038 */
14039 static void
14040f_trim(typval_T *argvars, typval_T *rettv)
14041{
14042 char_u buf1[NUMBUFLEN];
14043 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014044 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014045 char_u *mask = NULL;
14046 char_u *tail;
14047 char_u *prev;
14048 char_u *p;
14049 int c1;
14050
14051 rettv->v_type = VAR_STRING;
14052 if (head == NULL)
14053 {
14054 rettv->vval.v_string = NULL;
14055 return;
14056 }
14057
14058 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014059 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014060
14061 while (*head != NUL)
14062 {
14063 c1 = PTR2CHAR(head);
14064 if (mask == NULL)
14065 {
14066 if (c1 > ' ' && c1 != 0xa0)
14067 break;
14068 }
14069 else
14070 {
14071 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14072 if (c1 == PTR2CHAR(p))
14073 break;
14074 if (*p == NUL)
14075 break;
14076 }
14077 MB_PTR_ADV(head);
14078 }
14079
14080 for (tail = head + STRLEN(head); tail > head; tail = prev)
14081 {
14082 prev = tail;
14083 MB_PTR_BACK(head, prev);
14084 c1 = PTR2CHAR(prev);
14085 if (mask == NULL)
14086 {
14087 if (c1 > ' ' && c1 != 0xa0)
14088 break;
14089 }
14090 else
14091 {
14092 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14093 if (c1 == PTR2CHAR(p))
14094 break;
14095 if (*p == NUL)
14096 break;
14097 }
14098 }
14099 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
14100}
14101
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014102#ifdef FEAT_FLOAT
14103/*
14104 * "trunc({float})" function
14105 */
14106 static void
14107f_trunc(typval_T *argvars, typval_T *rettv)
14108{
14109 float_T f = 0.0;
14110
14111 rettv->v_type = VAR_FLOAT;
14112 if (get_float_arg(argvars, &f) == OK)
14113 /* trunc() is not in C90, use floor() or ceil() instead. */
14114 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
14115 else
14116 rettv->vval.v_float = 0.0;
14117}
14118#endif
14119
14120/*
14121 * "type(expr)" function
14122 */
14123 static void
14124f_type(typval_T *argvars, typval_T *rettv)
14125{
14126 int n = -1;
14127
14128 switch (argvars[0].v_type)
14129 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020014130 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
14131 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014132 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020014133 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
14134 case VAR_LIST: n = VAR_TYPE_LIST; break;
14135 case VAR_DICT: n = VAR_TYPE_DICT; break;
14136 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014137 case VAR_SPECIAL:
14138 if (argvars[0].vval.v_number == VVAL_FALSE
14139 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020014140 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014141 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020014142 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014143 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020014144 case VAR_JOB: n = VAR_TYPE_JOB; break;
14145 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014146 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010014147 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014148 n = -1;
14149 break;
14150 }
14151 rettv->vval.v_number = n;
14152}
14153
14154/*
14155 * "undofile(name)" function
14156 */
14157 static void
14158f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
14159{
14160 rettv->v_type = VAR_STRING;
14161#ifdef FEAT_PERSISTENT_UNDO
14162 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014163 char_u *fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014164
14165 if (*fname == NUL)
14166 {
14167 /* If there is no file name there will be no undo file. */
14168 rettv->vval.v_string = NULL;
14169 }
14170 else
14171 {
14172 char_u *ffname = FullName_save(fname, FALSE);
14173
14174 if (ffname != NULL)
14175 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
14176 vim_free(ffname);
14177 }
14178 }
14179#else
14180 rettv->vval.v_string = NULL;
14181#endif
14182}
14183
14184/*
14185 * "undotree()" function
14186 */
14187 static void
14188f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
14189{
14190 if (rettv_dict_alloc(rettv) == OK)
14191 {
14192 dict_T *dict = rettv->vval.v_dict;
14193 list_T *list;
14194
Bram Moolenaare0be1672018-07-08 16:50:37 +020014195 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
14196 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
14197 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
14198 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
14199 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
14200 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014201
14202 list = list_alloc();
14203 if (list != NULL)
14204 {
14205 u_eval_tree(curbuf->b_u_oldhead, list);
14206 dict_add_list(dict, "entries", list);
14207 }
14208 }
14209}
14210
14211/*
14212 * "values(dict)" function
14213 */
14214 static void
14215f_values(typval_T *argvars, typval_T *rettv)
14216{
14217 dict_list(argvars, rettv, 1);
14218}
14219
14220/*
14221 * "virtcol(string)" function
14222 */
14223 static void
14224f_virtcol(typval_T *argvars, typval_T *rettv)
14225{
14226 colnr_T vcol = 0;
14227 pos_T *fp;
14228 int fnum = curbuf->b_fnum;
14229
14230 fp = var2fpos(&argvars[0], FALSE, &fnum);
14231 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
14232 && fnum == curbuf->b_fnum)
14233 {
14234 getvvcol(curwin, fp, NULL, NULL, &vcol);
14235 ++vcol;
14236 }
14237
14238 rettv->vval.v_number = vcol;
14239}
14240
14241/*
14242 * "visualmode()" function
14243 */
14244 static void
14245f_visualmode(typval_T *argvars, typval_T *rettv)
14246{
14247 char_u str[2];
14248
14249 rettv->v_type = VAR_STRING;
14250 str[0] = curbuf->b_visual_mode_eval;
14251 str[1] = NUL;
14252 rettv->vval.v_string = vim_strsave(str);
14253
14254 /* A non-zero number or non-empty string argument: reset mode. */
14255 if (non_zero_arg(&argvars[0]))
14256 curbuf->b_visual_mode_eval = NUL;
14257}
14258
14259/*
14260 * "wildmenumode()" function
14261 */
14262 static void
14263f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14264{
14265#ifdef FEAT_WILDMENU
14266 if (wild_menu_showing)
14267 rettv->vval.v_number = 1;
14268#endif
14269}
14270
14271/*
14272 * "winbufnr(nr)" function
14273 */
14274 static void
14275f_winbufnr(typval_T *argvars, typval_T *rettv)
14276{
14277 win_T *wp;
14278
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014279 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014280 if (wp == NULL)
14281 rettv->vval.v_number = -1;
14282 else
14283 rettv->vval.v_number = wp->w_buffer->b_fnum;
14284}
14285
14286/*
14287 * "wincol()" function
14288 */
14289 static void
14290f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
14291{
14292 validate_cursor();
14293 rettv->vval.v_number = curwin->w_wcol + 1;
14294}
14295
14296/*
14297 * "winheight(nr)" function
14298 */
14299 static void
14300f_winheight(typval_T *argvars, typval_T *rettv)
14301{
14302 win_T *wp;
14303
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014304 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014305 if (wp == NULL)
14306 rettv->vval.v_number = -1;
14307 else
14308 rettv->vval.v_number = wp->w_height;
14309}
14310
14311/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020014312 * "winlayout()" function
14313 */
14314 static void
14315f_winlayout(typval_T *argvars, typval_T *rettv)
14316{
14317 tabpage_T *tp;
14318
14319 if (rettv_list_alloc(rettv) != OK)
14320 return;
14321
14322 if (argvars[0].v_type == VAR_UNKNOWN)
14323 tp = curtab;
14324 else
14325 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014326 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020014327 if (tp == NULL)
14328 return;
14329 }
14330
14331 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
14332}
14333
14334/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014335 * "winline()" function
14336 */
14337 static void
14338f_winline(typval_T *argvars UNUSED, typval_T *rettv)
14339{
14340 validate_cursor();
14341 rettv->vval.v_number = curwin->w_wrow + 1;
14342}
14343
14344/*
14345 * "winnr()" function
14346 */
14347 static void
14348f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
14349{
14350 int nr = 1;
14351
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014352 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014353 rettv->vval.v_number = nr;
14354}
14355
14356/*
14357 * "winrestcmd()" function
14358 */
14359 static void
14360f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
14361{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014362 win_T *wp;
14363 int winnr = 1;
14364 garray_T ga;
14365 char_u buf[50];
14366
14367 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020014368 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014369 {
14370 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
14371 ga_concat(&ga, buf);
14372 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
14373 ga_concat(&ga, buf);
14374 ++winnr;
14375 }
14376 ga_append(&ga, NUL);
14377
14378 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014379 rettv->v_type = VAR_STRING;
14380}
14381
14382/*
14383 * "winrestview()" function
14384 */
14385 static void
14386f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
14387{
14388 dict_T *dict;
14389
14390 if (argvars[0].v_type != VAR_DICT
14391 || (dict = argvars[0].vval.v_dict) == NULL)
14392 EMSG(_(e_invarg));
14393 else
14394 {
14395 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014396 curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014397 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014398 curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014399#ifdef FEAT_VIRTUALEDIT
14400 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014401 curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014402#endif
14403 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
14404 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010014405 curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014406 curwin->w_set_curswant = FALSE;
14407 }
14408
14409 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014410 set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014411#ifdef FEAT_DIFF
14412 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014413 curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014414#endif
14415 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014416 curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014417 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014418 curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014419
14420 check_cursor();
14421 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020014422 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014423 changed_window_setting();
14424
14425 if (curwin->w_topline <= 0)
14426 curwin->w_topline = 1;
14427 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
14428 curwin->w_topline = curbuf->b_ml.ml_line_count;
14429#ifdef FEAT_DIFF
14430 check_topfill(curwin, TRUE);
14431#endif
14432 }
14433}
14434
14435/*
14436 * "winsaveview()" function
14437 */
14438 static void
14439f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
14440{
14441 dict_T *dict;
14442
14443 if (rettv_dict_alloc(rettv) == FAIL)
14444 return;
14445 dict = rettv->vval.v_dict;
14446
Bram Moolenaare0be1672018-07-08 16:50:37 +020014447 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
14448 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014449#ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +020014450 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014451#endif
14452 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020014453 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014454
Bram Moolenaare0be1672018-07-08 16:50:37 +020014455 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014456#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020014457 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014458#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020014459 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
14460 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014461}
14462
14463/*
14464 * "winwidth(nr)" function
14465 */
14466 static void
14467f_winwidth(typval_T *argvars, typval_T *rettv)
14468{
14469 win_T *wp;
14470
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014471 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014472 if (wp == NULL)
14473 rettv->vval.v_number = -1;
14474 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014475 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014476}
14477
14478/*
14479 * "wordcount()" function
14480 */
14481 static void
14482f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
14483{
14484 if (rettv_dict_alloc(rettv) == FAIL)
14485 return;
14486 cursor_pos_info(rettv->vval.v_dict);
14487}
14488
14489/*
14490 * "writefile()" function
14491 */
14492 static void
14493f_writefile(typval_T *argvars, typval_T *rettv)
14494{
14495 int binary = FALSE;
14496 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014497#ifdef HAVE_FSYNC
14498 int do_fsync = p_fs;
14499#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014500 char_u *fname;
14501 FILE *fd;
14502 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014503 listitem_T *li;
14504 list_T *list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014505
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014506 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014507 if (check_restricted() || check_secure())
14508 return;
14509
14510 if (argvars[0].v_type != VAR_LIST)
14511 {
14512 EMSG2(_(e_listarg), "writefile()");
14513 return;
14514 }
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014515 list = argvars[0].vval.v_list;
14516 if (list == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014517 return;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014518 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014519 if (tv_get_string_chk(&li->li_tv) == NULL)
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014520 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014521
14522 if (argvars[2].v_type != VAR_UNKNOWN)
14523 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014524 char_u *arg2 = tv_get_string_chk(&argvars[2]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014525
14526 if (arg2 == NULL)
14527 return;
14528 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014529 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014530 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014531 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014532#ifdef HAVE_FSYNC
14533 if (vim_strchr(arg2, 's') != NULL)
14534 do_fsync = TRUE;
14535 else if (vim_strchr(arg2, 'S') != NULL)
14536 do_fsync = FALSE;
14537#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014538 }
14539
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014540 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014541 if (fname == NULL)
14542 return;
14543
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014544 /* Always open the file in binary mode, library functions have a mind of
14545 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014546 if (*fname == NUL || (fd = mch_fopen((char *)fname,
14547 append ? APPENDBIN : WRITEBIN)) == NULL)
14548 {
14549 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
14550 ret = -1;
14551 }
14552 else
14553 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014554 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014555 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014556#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010014557 else if (do_fsync)
14558 /* Ignore the error, the user wouldn't know what to do about it.
14559 * May happen for a device. */
Bram Moolenaar42335f52018-09-13 15:33:43 +020014560 vim_ignored = fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014561#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014562 fclose(fd);
14563 }
14564
14565 rettv->vval.v_number = ret;
14566}
14567
14568/*
14569 * "xor(expr, expr)" function
14570 */
14571 static void
14572f_xor(typval_T *argvars, typval_T *rettv)
14573{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014574 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
14575 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014576}
14577
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014578#endif /* FEAT_EVAL */