blob: d3b2da7c4659d8a01418b8f3282d514545c1773e [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);
370static void f_simplify(typval_T *argvars, typval_T *rettv);
371#ifdef FEAT_FLOAT
372static void f_sin(typval_T *argvars, typval_T *rettv);
373static void f_sinh(typval_T *argvars, typval_T *rettv);
374#endif
375static void f_sort(typval_T *argvars, typval_T *rettv);
376static void f_soundfold(typval_T *argvars, typval_T *rettv);
377static void f_spellbadword(typval_T *argvars, typval_T *rettv);
378static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
379static void f_split(typval_T *argvars, typval_T *rettv);
380#ifdef FEAT_FLOAT
381static void f_sqrt(typval_T *argvars, typval_T *rettv);
382static void f_str2float(typval_T *argvars, typval_T *rettv);
383#endif
384static void f_str2nr(typval_T *argvars, typval_T *rettv);
385static void f_strchars(typval_T *argvars, typval_T *rettv);
386#ifdef HAVE_STRFTIME
387static void f_strftime(typval_T *argvars, typval_T *rettv);
388#endif
389static void f_strgetchar(typval_T *argvars, typval_T *rettv);
390static void f_stridx(typval_T *argvars, typval_T *rettv);
391static void f_string(typval_T *argvars, typval_T *rettv);
392static void f_strlen(typval_T *argvars, typval_T *rettv);
393static void f_strcharpart(typval_T *argvars, typval_T *rettv);
394static void f_strpart(typval_T *argvars, typval_T *rettv);
395static void f_strridx(typval_T *argvars, typval_T *rettv);
396static void f_strtrans(typval_T *argvars, typval_T *rettv);
397static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
398static void f_strwidth(typval_T *argvars, typval_T *rettv);
399static void f_submatch(typval_T *argvars, typval_T *rettv);
400static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200401static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200402static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200403static void f_synID(typval_T *argvars, typval_T *rettv);
404static void f_synIDattr(typval_T *argvars, typval_T *rettv);
405static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
406static void f_synstack(typval_T *argvars, typval_T *rettv);
407static void f_synconcealed(typval_T *argvars, typval_T *rettv);
408static void f_system(typval_T *argvars, typval_T *rettv);
409static void f_systemlist(typval_T *argvars, typval_T *rettv);
410static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
411static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
412static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
413static void f_taglist(typval_T *argvars, typval_T *rettv);
414static void f_tagfiles(typval_T *argvars, typval_T *rettv);
415static void f_tempname(typval_T *argvars, typval_T *rettv);
416static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
417static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200418static void f_test_feedinput(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200419static void f_test_option_not_set(typval_T *argvars, typval_T *rettv);
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100420static void f_test_override(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200421static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100422static void f_test_ignore_error(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200423#ifdef FEAT_JOB_CHANNEL
424static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
425#endif
426static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
427#ifdef FEAT_JOB_CHANNEL
428static void f_test_null_job(typval_T *argvars, typval_T *rettv);
429#endif
430static void f_test_null_list(typval_T *argvars, typval_T *rettv);
431static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
432static void f_test_null_string(typval_T *argvars, typval_T *rettv);
Bram Moolenaarab186732018-09-14 21:27:06 +0200433#ifdef FEAT_GUI
434static void f_test_scrollbar(typval_T *argvars, typval_T *rettv);
435#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200436static void f_test_settime(typval_T *argvars, typval_T *rettv);
437#ifdef FEAT_FLOAT
438static void f_tan(typval_T *argvars, typval_T *rettv);
439static void f_tanh(typval_T *argvars, typval_T *rettv);
440#endif
441#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200442static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200443static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200444static void f_timer_start(typval_T *argvars, typval_T *rettv);
445static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200446static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200447#endif
448static void f_tolower(typval_T *argvars, typval_T *rettv);
449static void f_toupper(typval_T *argvars, typval_T *rettv);
450static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100451static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200452#ifdef FEAT_FLOAT
453static void f_trunc(typval_T *argvars, typval_T *rettv);
454#endif
455static void f_type(typval_T *argvars, typval_T *rettv);
456static void f_undofile(typval_T *argvars, typval_T *rettv);
457static void f_undotree(typval_T *argvars, typval_T *rettv);
458static void f_uniq(typval_T *argvars, typval_T *rettv);
459static void f_values(typval_T *argvars, typval_T *rettv);
460static void f_virtcol(typval_T *argvars, typval_T *rettv);
461static void f_visualmode(typval_T *argvars, typval_T *rettv);
462static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
463static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
464static void f_win_getid(typval_T *argvars, typval_T *rettv);
465static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
466static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
467static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100468static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200469static void f_winbufnr(typval_T *argvars, typval_T *rettv);
470static void f_wincol(typval_T *argvars, typval_T *rettv);
471static void f_winheight(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200472static void f_winlayout(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200473static void f_winline(typval_T *argvars, typval_T *rettv);
474static void f_winnr(typval_T *argvars, typval_T *rettv);
475static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
476static void f_winrestview(typval_T *argvars, typval_T *rettv);
477static void f_winsaveview(typval_T *argvars, typval_T *rettv);
478static void f_winwidth(typval_T *argvars, typval_T *rettv);
479static void f_writefile(typval_T *argvars, typval_T *rettv);
480static void f_wordcount(typval_T *argvars, typval_T *rettv);
481static void f_xor(typval_T *argvars, typval_T *rettv);
482
483/*
484 * Array with names and number of arguments of all internal functions
485 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
486 */
487static struct fst
488{
489 char *f_name; /* function name */
490 char f_min_argc; /* minimal number of arguments */
491 char f_max_argc; /* maximal number of arguments */
492 void (*f_func)(typval_T *args, typval_T *rvar);
493 /* implementation of function */
494} functions[] =
495{
496#ifdef FEAT_FLOAT
497 {"abs", 1, 1, f_abs},
498 {"acos", 1, 1, f_acos}, /* WJMc */
499#endif
500 {"add", 2, 2, f_add},
501 {"and", 2, 2, f_and},
502 {"append", 2, 2, f_append},
Bram Moolenaarca851592018-06-06 21:04:07 +0200503 {"appendbufline", 3, 3, f_appendbufline},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200504 {"argc", 0, 1, f_argc},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200505 {"argidx", 0, 0, f_argidx},
506 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200507 {"argv", 0, 2, f_argv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200508#ifdef FEAT_FLOAT
509 {"asin", 1, 1, f_asin}, /* WJMc */
510#endif
Bram Moolenaarb48e96f2018-02-13 12:26:14 +0100511 {"assert_beeps", 1, 2, f_assert_beeps},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200512 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaard96ff162018-02-18 22:13:29 +0100513 {"assert_equalfile", 2, 2, f_assert_equalfile},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200514 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaar1307d1c2018-10-07 20:16:49 +0200515 {"assert_fails", 1, 3, f_assert_fails},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200516 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100517 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200518 {"assert_match", 2, 3, f_assert_match},
519 {"assert_notequal", 2, 3, f_assert_notequal},
520 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100521 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200522 {"assert_true", 1, 2, f_assert_true},
523#ifdef FEAT_FLOAT
524 {"atan", 1, 1, f_atan},
525 {"atan2", 2, 2, f_atan2},
526#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100527#ifdef FEAT_BEVAL
528 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100529# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100530 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100531# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100532#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200533 {"browse", 4, 4, f_browse},
534 {"browsedir", 2, 2, f_browsedir},
535 {"bufexists", 1, 1, f_bufexists},
536 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
537 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
538 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
539 {"buflisted", 1, 1, f_buflisted},
540 {"bufloaded", 1, 1, f_bufloaded},
541 {"bufname", 1, 1, f_bufname},
542 {"bufnr", 1, 2, f_bufnr},
543 {"bufwinid", 1, 1, f_bufwinid},
544 {"bufwinnr", 1, 1, f_bufwinnr},
545 {"byte2line", 1, 1, f_byte2line},
546 {"byteidx", 2, 2, f_byteidx},
547 {"byteidxcomp", 2, 2, f_byteidxcomp},
548 {"call", 2, 3, f_call},
549#ifdef FEAT_FLOAT
550 {"ceil", 1, 1, f_ceil},
551#endif
552#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100553 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200554 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200555 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200556 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
557 {"ch_evalraw", 2, 3, f_ch_evalraw},
558 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
559 {"ch_getjob", 1, 1, f_ch_getjob},
560 {"ch_info", 1, 1, f_ch_info},
561 {"ch_log", 1, 2, f_ch_log},
562 {"ch_logfile", 1, 2, f_ch_logfile},
563 {"ch_open", 1, 2, f_ch_open},
564 {"ch_read", 1, 2, f_ch_read},
565 {"ch_readraw", 1, 2, f_ch_readraw},
566 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
567 {"ch_sendraw", 2, 3, f_ch_sendraw},
568 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200569 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200570#endif
571 {"changenr", 0, 0, f_changenr},
572 {"char2nr", 1, 2, f_char2nr},
573 {"cindent", 1, 1, f_cindent},
574 {"clearmatches", 0, 0, f_clearmatches},
575 {"col", 1, 1, f_col},
576#if defined(FEAT_INS_EXPAND)
577 {"complete", 2, 2, f_complete},
578 {"complete_add", 1, 1, f_complete_add},
579 {"complete_check", 0, 0, f_complete_check},
580#endif
581 {"confirm", 1, 4, f_confirm},
582 {"copy", 1, 1, f_copy},
583#ifdef FEAT_FLOAT
584 {"cos", 1, 1, f_cos},
585 {"cosh", 1, 1, f_cosh},
586#endif
587 {"count", 2, 4, f_count},
588 {"cscope_connection",0,3, f_cscope_connection},
589 {"cursor", 1, 3, f_cursor},
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200590#ifdef WIN3264
591 {"debugbreak", 1, 1, f_debugbreak},
592#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200593 {"deepcopy", 1, 2, f_deepcopy},
594 {"delete", 1, 2, f_delete},
Bram Moolenaard79a2622018-06-07 18:17:46 +0200595 {"deletebufline", 2, 3, f_deletebufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200596 {"did_filetype", 0, 0, f_did_filetype},
597 {"diff_filler", 1, 1, f_diff_filler},
598 {"diff_hlID", 2, 2, f_diff_hlID},
599 {"empty", 1, 1, f_empty},
600 {"escape", 2, 2, f_escape},
601 {"eval", 1, 1, f_eval},
602 {"eventhandler", 0, 0, f_eventhandler},
603 {"executable", 1, 1, f_executable},
604 {"execute", 1, 2, f_execute},
605 {"exepath", 1, 1, f_exepath},
606 {"exists", 1, 1, f_exists},
607#ifdef FEAT_FLOAT
608 {"exp", 1, 1, f_exp},
609#endif
610 {"expand", 1, 3, f_expand},
611 {"extend", 2, 3, f_extend},
612 {"feedkeys", 1, 2, f_feedkeys},
613 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
614 {"filereadable", 1, 1, f_filereadable},
615 {"filewritable", 1, 1, f_filewritable},
616 {"filter", 2, 2, f_filter},
617 {"finddir", 1, 3, f_finddir},
618 {"findfile", 1, 3, f_findfile},
619#ifdef FEAT_FLOAT
620 {"float2nr", 1, 1, f_float2nr},
621 {"floor", 1, 1, f_floor},
622 {"fmod", 2, 2, f_fmod},
623#endif
624 {"fnameescape", 1, 1, f_fnameescape},
625 {"fnamemodify", 2, 2, f_fnamemodify},
626 {"foldclosed", 1, 1, f_foldclosed},
627 {"foldclosedend", 1, 1, f_foldclosedend},
628 {"foldlevel", 1, 1, f_foldlevel},
629 {"foldtext", 0, 0, f_foldtext},
630 {"foldtextresult", 1, 1, f_foldtextresult},
631 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200632 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200633 {"function", 1, 3, f_function},
634 {"garbagecollect", 0, 1, f_garbagecollect},
635 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200636 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200637 {"getbufline", 2, 3, f_getbufline},
638 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100639 {"getchangelist", 1, 1, f_getchangelist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200640 {"getchar", 0, 1, f_getchar},
641 {"getcharmod", 0, 0, f_getcharmod},
642 {"getcharsearch", 0, 0, f_getcharsearch},
643 {"getcmdline", 0, 0, f_getcmdline},
644 {"getcmdpos", 0, 0, f_getcmdpos},
645 {"getcmdtype", 0, 0, f_getcmdtype},
646 {"getcmdwintype", 0, 0, f_getcmdwintype},
647#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200648 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200649#endif
650 {"getcurpos", 0, 0, f_getcurpos},
651 {"getcwd", 0, 2, f_getcwd},
652 {"getfontname", 0, 1, f_getfontname},
653 {"getfperm", 1, 1, f_getfperm},
654 {"getfsize", 1, 1, f_getfsize},
655 {"getftime", 1, 1, f_getftime},
656 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100657 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200658 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200659 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200660 {"getmatches", 0, 0, f_getmatches},
661 {"getpid", 0, 0, f_getpid},
662 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200663 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200664 {"getreg", 0, 3, f_getreg},
665 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200666 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200667 {"gettabvar", 2, 3, f_gettabvar},
668 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100669 {"gettagstack", 0, 1, f_gettagstack},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200670 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100671 {"getwinpos", 0, 1, f_getwinpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200672 {"getwinposx", 0, 0, f_getwinposx},
673 {"getwinposy", 0, 0, f_getwinposy},
674 {"getwinvar", 2, 3, f_getwinvar},
675 {"glob", 1, 4, f_glob},
676 {"glob2regpat", 1, 1, f_glob2regpat},
677 {"globpath", 2, 5, f_globpath},
678 {"has", 1, 1, f_has},
679 {"has_key", 2, 2, f_has_key},
680 {"haslocaldir", 0, 2, f_haslocaldir},
681 {"hasmapto", 1, 3, f_hasmapto},
682 {"highlightID", 1, 1, f_hlID}, /* obsolete */
683 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
684 {"histadd", 2, 2, f_histadd},
685 {"histdel", 1, 2, f_histdel},
686 {"histget", 1, 2, f_histget},
687 {"histnr", 1, 1, f_histnr},
688 {"hlID", 1, 1, f_hlID},
689 {"hlexists", 1, 1, f_hlexists},
690 {"hostname", 0, 0, f_hostname},
691 {"iconv", 3, 3, f_iconv},
692 {"indent", 1, 1, f_indent},
693 {"index", 2, 4, f_index},
694 {"input", 1, 3, f_input},
695 {"inputdialog", 1, 3, f_inputdialog},
696 {"inputlist", 1, 1, f_inputlist},
697 {"inputrestore", 0, 0, f_inputrestore},
698 {"inputsave", 0, 0, f_inputsave},
699 {"inputsecret", 1, 2, f_inputsecret},
700 {"insert", 2, 3, f_insert},
701 {"invert", 1, 1, f_invert},
702 {"isdirectory", 1, 1, f_isdirectory},
703 {"islocked", 1, 1, f_islocked},
704#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
705 {"isnan", 1, 1, f_isnan},
706#endif
707 {"items", 1, 1, f_items},
708#ifdef FEAT_JOB_CHANNEL
709 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200710 {"job_info", 0, 1, f_job_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200711 {"job_setoptions", 2, 2, f_job_setoptions},
712 {"job_start", 1, 2, f_job_start},
713 {"job_status", 1, 1, f_job_status},
714 {"job_stop", 1, 2, f_job_stop},
715#endif
716 {"join", 1, 2, f_join},
717 {"js_decode", 1, 1, f_js_decode},
718 {"js_encode", 1, 1, f_js_encode},
719 {"json_decode", 1, 1, f_json_decode},
720 {"json_encode", 1, 1, f_json_encode},
721 {"keys", 1, 1, f_keys},
722 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
723 {"len", 1, 1, f_len},
724 {"libcall", 3, 3, f_libcall},
725 {"libcallnr", 3, 3, f_libcallnr},
726 {"line", 1, 1, f_line},
727 {"line2byte", 1, 1, f_line2byte},
728 {"lispindent", 1, 1, f_lispindent},
729 {"localtime", 0, 0, f_localtime},
730#ifdef FEAT_FLOAT
731 {"log", 1, 1, f_log},
732 {"log10", 1, 1, f_log10},
733#endif
734#ifdef FEAT_LUA
735 {"luaeval", 1, 2, f_luaeval},
736#endif
737 {"map", 2, 2, f_map},
738 {"maparg", 1, 4, f_maparg},
739 {"mapcheck", 1, 3, f_mapcheck},
740 {"match", 2, 4, f_match},
741 {"matchadd", 2, 5, f_matchadd},
742 {"matchaddpos", 2, 5, f_matchaddpos},
743 {"matcharg", 1, 1, f_matcharg},
744 {"matchdelete", 1, 1, f_matchdelete},
745 {"matchend", 2, 4, f_matchend},
746 {"matchlist", 2, 4, f_matchlist},
747 {"matchstr", 2, 4, f_matchstr},
748 {"matchstrpos", 2, 4, f_matchstrpos},
749 {"max", 1, 1, f_max},
750 {"min", 1, 1, f_min},
751#ifdef vim_mkdir
752 {"mkdir", 1, 3, f_mkdir},
753#endif
754 {"mode", 0, 1, f_mode},
755#ifdef FEAT_MZSCHEME
756 {"mzeval", 1, 1, f_mzeval},
757#endif
758 {"nextnonblank", 1, 1, f_nextnonblank},
759 {"nr2char", 1, 2, f_nr2char},
760 {"or", 2, 2, f_or},
761 {"pathshorten", 1, 1, f_pathshorten},
762#ifdef FEAT_PERL
763 {"perleval", 1, 1, f_perleval},
764#endif
765#ifdef FEAT_FLOAT
766 {"pow", 2, 2, f_pow},
767#endif
768 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100769 {"printf", 1, 19, f_printf},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200770#ifdef FEAT_JOB_CHANNEL
771 {"prompt_setcallback", 2, 2, f_prompt_setcallback},
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200772 {"prompt_setinterrupt", 2, 2, f_prompt_setinterrupt},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200773 {"prompt_setprompt", 2, 2, f_prompt_setprompt},
774#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200775 {"pumvisible", 0, 0, f_pumvisible},
776#ifdef FEAT_PYTHON3
777 {"py3eval", 1, 1, f_py3eval},
778#endif
779#ifdef FEAT_PYTHON
780 {"pyeval", 1, 1, f_pyeval},
781#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100782#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
783 {"pyxeval", 1, 1, f_pyxeval},
784#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200785 {"range", 1, 3, f_range},
786 {"readfile", 1, 3, f_readfile},
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200787 {"reg_executing", 0, 0, f_reg_executing},
788 {"reg_recording", 0, 0, f_reg_recording},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200789 {"reltime", 0, 2, f_reltime},
790#ifdef FEAT_FLOAT
791 {"reltimefloat", 1, 1, f_reltimefloat},
792#endif
793 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100794 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200795 {"remote_foreground", 1, 1, f_remote_foreground},
796 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100797 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200798 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100799 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200800 {"remove", 2, 3, f_remove},
801 {"rename", 2, 2, f_rename},
802 {"repeat", 2, 2, f_repeat},
803 {"resolve", 1, 1, f_resolve},
804 {"reverse", 1, 1, f_reverse},
805#ifdef FEAT_FLOAT
806 {"round", 1, 1, f_round},
807#endif
808 {"screenattr", 2, 2, f_screenattr},
809 {"screenchar", 2, 2, f_screenchar},
810 {"screencol", 0, 0, f_screencol},
811 {"screenrow", 0, 0, f_screenrow},
812 {"search", 1, 4, f_search},
813 {"searchdecl", 1, 3, f_searchdecl},
814 {"searchpair", 3, 7, f_searchpair},
815 {"searchpairpos", 3, 7, f_searchpairpos},
816 {"searchpos", 1, 4, f_searchpos},
817 {"server2client", 2, 2, f_server2client},
818 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200819 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200820 {"setbufvar", 3, 3, f_setbufvar},
821 {"setcharsearch", 1, 1, f_setcharsearch},
822 {"setcmdpos", 1, 1, f_setcmdpos},
823 {"setfperm", 2, 2, f_setfperm},
824 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200825 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200826 {"setmatches", 1, 1, f_setmatches},
827 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200828 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200829 {"setreg", 2, 3, f_setreg},
830 {"settabvar", 3, 3, f_settabvar},
831 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100832 {"settagstack", 2, 3, f_settagstack},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200833 {"setwinvar", 3, 3, f_setwinvar},
834#ifdef FEAT_CRYPT
835 {"sha256", 1, 1, f_sha256},
836#endif
837 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaarf9514162018-11-22 03:08:29 +0100838 {"shiftwidth", 0, 1, f_shiftwidth},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200839 {"simplify", 1, 1, f_simplify},
840#ifdef FEAT_FLOAT
841 {"sin", 1, 1, f_sin},
842 {"sinh", 1, 1, f_sinh},
843#endif
844 {"sort", 1, 3, f_sort},
845 {"soundfold", 1, 1, f_soundfold},
846 {"spellbadword", 0, 1, f_spellbadword},
847 {"spellsuggest", 1, 3, f_spellsuggest},
848 {"split", 1, 3, f_split},
849#ifdef FEAT_FLOAT
850 {"sqrt", 1, 1, f_sqrt},
851 {"str2float", 1, 1, f_str2float},
852#endif
853 {"str2nr", 1, 2, f_str2nr},
854 {"strcharpart", 2, 3, f_strcharpart},
855 {"strchars", 1, 2, f_strchars},
856 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
857#ifdef HAVE_STRFTIME
858 {"strftime", 1, 2, f_strftime},
859#endif
860 {"strgetchar", 2, 2, f_strgetchar},
861 {"stridx", 2, 3, f_stridx},
862 {"string", 1, 1, f_string},
863 {"strlen", 1, 1, f_strlen},
864 {"strpart", 2, 3, f_strpart},
865 {"strridx", 2, 3, f_strridx},
866 {"strtrans", 1, 1, f_strtrans},
867 {"strwidth", 1, 1, f_strwidth},
868 {"submatch", 1, 2, f_submatch},
869 {"substitute", 4, 4, f_substitute},
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200870 {"swapinfo", 1, 1, f_swapinfo},
Bram Moolenaar110bd602018-09-16 18:46:59 +0200871 {"swapname", 1, 1, f_swapname},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200872 {"synID", 3, 3, f_synID},
873 {"synIDattr", 2, 3, f_synIDattr},
874 {"synIDtrans", 1, 1, f_synIDtrans},
875 {"synconcealed", 2, 2, f_synconcealed},
876 {"synstack", 2, 2, f_synstack},
877 {"system", 1, 2, f_system},
878 {"systemlist", 1, 2, f_systemlist},
879 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
880 {"tabpagenr", 0, 1, f_tabpagenr},
881 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
882 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100883 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200884#ifdef FEAT_FLOAT
885 {"tan", 1, 1, f_tan},
886 {"tanh", 1, 1, f_tanh},
887#endif
888 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200889#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100890 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
891 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100892 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200893 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200894# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
895 {"term_getansicolors", 1, 1, f_term_getansicolors},
896# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200897 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200898 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200899 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200900 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200901 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200902 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200903 {"term_getstatus", 1, 1, f_term_getstatus},
904 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200905 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200906 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200907 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200908 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200909# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
910 {"term_setansicolors", 2, 2, f_term_setansicolors},
911# endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100912 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100913 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaara42d3632018-04-14 17:05:38 +0200914 {"term_setsize", 3, 3, f_term_setsize},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200915 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200916 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200917#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200918 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
919 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200920 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200921 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100922 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200923#ifdef FEAT_JOB_CHANNEL
924 {"test_null_channel", 0, 0, f_test_null_channel},
925#endif
926 {"test_null_dict", 0, 0, f_test_null_dict},
927#ifdef FEAT_JOB_CHANNEL
928 {"test_null_job", 0, 0, f_test_null_job},
929#endif
930 {"test_null_list", 0, 0, f_test_null_list},
931 {"test_null_partial", 0, 0, f_test_null_partial},
932 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200933 {"test_option_not_set", 1, 1, f_test_option_not_set},
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100934 {"test_override", 2, 2, f_test_override},
Bram Moolenaarab186732018-09-14 21:27:06 +0200935#ifdef FEAT_GUI
936 {"test_scrollbar", 3, 3, f_test_scrollbar},
937#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200938 {"test_settime", 1, 1, f_test_settime},
939#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200940 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200941 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200942 {"timer_start", 2, 3, f_timer_start},
943 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200944 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200945#endif
946 {"tolower", 1, 1, f_tolower},
947 {"toupper", 1, 1, f_toupper},
948 {"tr", 3, 3, f_tr},
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100949 {"trim", 1, 2, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200950#ifdef FEAT_FLOAT
951 {"trunc", 1, 1, f_trunc},
952#endif
953 {"type", 1, 1, f_type},
954 {"undofile", 1, 1, f_undofile},
955 {"undotree", 0, 0, f_undotree},
956 {"uniq", 1, 3, f_uniq},
957 {"values", 1, 1, f_values},
958 {"virtcol", 1, 1, f_virtcol},
959 {"visualmode", 0, 1, f_visualmode},
960 {"wildmenumode", 0, 0, f_wildmenumode},
961 {"win_findbuf", 1, 1, f_win_findbuf},
962 {"win_getid", 0, 2, f_win_getid},
963 {"win_gotoid", 1, 1, f_win_gotoid},
964 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
965 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100966 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200967 {"winbufnr", 1, 1, f_winbufnr},
968 {"wincol", 0, 0, f_wincol},
969 {"winheight", 1, 1, f_winheight},
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200970 {"winlayout", 0, 1, f_winlayout},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200971 {"winline", 0, 0, f_winline},
972 {"winnr", 0, 1, f_winnr},
973 {"winrestcmd", 0, 0, f_winrestcmd},
974 {"winrestview", 1, 1, f_winrestview},
975 {"winsaveview", 0, 0, f_winsaveview},
976 {"winwidth", 1, 1, f_winwidth},
977 {"wordcount", 0, 0, f_wordcount},
978 {"writefile", 2, 3, f_writefile},
979 {"xor", 2, 2, f_xor},
980};
981
982#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
983
984/*
985 * Function given to ExpandGeneric() to obtain the list of internal
986 * or user defined function names.
987 */
988 char_u *
989get_function_name(expand_T *xp, int idx)
990{
991 static int intidx = -1;
992 char_u *name;
993
994 if (idx == 0)
995 intidx = -1;
996 if (intidx < 0)
997 {
998 name = get_user_func_name(xp, idx);
999 if (name != NULL)
1000 return name;
1001 }
1002 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
1003 {
1004 STRCPY(IObuff, functions[intidx].f_name);
1005 STRCAT(IObuff, "(");
1006 if (functions[intidx].f_max_argc == 0)
1007 STRCAT(IObuff, ")");
1008 return IObuff;
1009 }
1010
1011 return NULL;
1012}
1013
1014/*
1015 * Function given to ExpandGeneric() to obtain the list of internal or
1016 * user defined variable or function names.
1017 */
1018 char_u *
1019get_expr_name(expand_T *xp, int idx)
1020{
1021 static int intidx = -1;
1022 char_u *name;
1023
1024 if (idx == 0)
1025 intidx = -1;
1026 if (intidx < 0)
1027 {
1028 name = get_function_name(xp, idx);
1029 if (name != NULL)
1030 return name;
1031 }
1032 return get_user_var_name(xp, ++intidx);
1033}
1034
1035#endif /* FEAT_CMDL_COMPL */
1036
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001037/*
1038 * Find internal function in table above.
1039 * Return index, or -1 if not found
1040 */
1041 int
1042find_internal_func(
1043 char_u *name) /* name of the function */
1044{
1045 int first = 0;
1046 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
1047 int cmp;
1048 int x;
1049
1050 /*
1051 * Find the function name in the table. Binary search.
1052 */
1053 while (first <= last)
1054 {
1055 x = first + ((unsigned)(last - first) >> 1);
1056 cmp = STRCMP(name, functions[x].f_name);
1057 if (cmp < 0)
1058 last = x - 1;
1059 else if (cmp > 0)
1060 first = x + 1;
1061 else
1062 return x;
1063 }
1064 return -1;
1065}
1066
1067 int
1068call_internal_func(
1069 char_u *name,
1070 int argcount,
1071 typval_T *argvars,
1072 typval_T *rettv)
1073{
1074 int i;
1075
1076 i = find_internal_func(name);
1077 if (i < 0)
1078 return ERROR_UNKNOWN;
1079 if (argcount < functions[i].f_min_argc)
1080 return ERROR_TOOFEW;
1081 if (argcount > functions[i].f_max_argc)
1082 return ERROR_TOOMANY;
1083 argvars[argcount].v_type = VAR_UNKNOWN;
1084 functions[i].f_func(argvars, rettv);
1085 return ERROR_NONE;
1086}
1087
1088/*
1089 * Return TRUE for a non-zero Number and a non-empty String.
1090 */
1091 static int
1092non_zero_arg(typval_T *argvars)
1093{
1094 return ((argvars[0].v_type == VAR_NUMBER
1095 && argvars[0].vval.v_number != 0)
1096 || (argvars[0].v_type == VAR_SPECIAL
1097 && argvars[0].vval.v_number == VVAL_TRUE)
1098 || (argvars[0].v_type == VAR_STRING
1099 && argvars[0].vval.v_string != NULL
1100 && *argvars[0].vval.v_string != NUL));
1101}
1102
1103/*
1104 * Get the lnum from the first argument.
1105 * Also accepts ".", "$", etc., but that only works for the current buffer.
1106 * Returns -1 on error.
1107 */
1108 static linenr_T
1109get_tv_lnum(typval_T *argvars)
1110{
1111 typval_T rettv;
1112 linenr_T lnum;
1113
1114 lnum = (linenr_T)get_tv_number_chk(&argvars[0], NULL);
1115 if (lnum == 0) /* no valid number, try using line() */
1116 {
1117 rettv.v_type = VAR_NUMBER;
1118 f_line(argvars, &rettv);
1119 lnum = (linenr_T)rettv.vval.v_number;
1120 clear_tv(&rettv);
1121 }
1122 return lnum;
1123}
1124
1125#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001126/*
1127 * Get the float value of "argvars[0]" into "f".
1128 * Returns FAIL when the argument is not a Number or Float.
1129 */
1130 static int
1131get_float_arg(typval_T *argvars, float_T *f)
1132{
1133 if (argvars[0].v_type == VAR_FLOAT)
1134 {
1135 *f = argvars[0].vval.v_float;
1136 return OK;
1137 }
1138 if (argvars[0].v_type == VAR_NUMBER)
1139 {
1140 *f = (float_T)argvars[0].vval.v_number;
1141 return OK;
1142 }
1143 EMSG(_("E808: Number or Float required"));
1144 return FAIL;
1145}
1146
1147/*
1148 * "abs(expr)" function
1149 */
1150 static void
1151f_abs(typval_T *argvars, typval_T *rettv)
1152{
1153 if (argvars[0].v_type == VAR_FLOAT)
1154 {
1155 rettv->v_type = VAR_FLOAT;
1156 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1157 }
1158 else
1159 {
1160 varnumber_T n;
1161 int error = FALSE;
1162
1163 n = get_tv_number_chk(&argvars[0], &error);
1164 if (error)
1165 rettv->vval.v_number = -1;
1166 else if (n > 0)
1167 rettv->vval.v_number = n;
1168 else
1169 rettv->vval.v_number = -n;
1170 }
1171}
1172
1173/*
1174 * "acos()" function
1175 */
1176 static void
1177f_acos(typval_T *argvars, typval_T *rettv)
1178{
1179 float_T f = 0.0;
1180
1181 rettv->v_type = VAR_FLOAT;
1182 if (get_float_arg(argvars, &f) == OK)
1183 rettv->vval.v_float = acos(f);
1184 else
1185 rettv->vval.v_float = 0.0;
1186}
1187#endif
1188
1189/*
1190 * "add(list, item)" function
1191 */
1192 static void
1193f_add(typval_T *argvars, typval_T *rettv)
1194{
1195 list_T *l;
1196
1197 rettv->vval.v_number = 1; /* Default: Failed */
1198 if (argvars[0].v_type == VAR_LIST)
1199 {
1200 if ((l = argvars[0].vval.v_list) != NULL
1201 && !tv_check_lock(l->lv_lock,
1202 (char_u *)N_("add() argument"), TRUE)
1203 && list_append_tv(l, &argvars[1]) == OK)
1204 copy_tv(&argvars[0], rettv);
1205 }
1206 else
1207 EMSG(_(e_listreq));
1208}
1209
1210/*
1211 * "and(expr, expr)" function
1212 */
1213 static void
1214f_and(typval_T *argvars, typval_T *rettv)
1215{
1216 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
1217 & get_tv_number_chk(&argvars[1], NULL);
1218}
1219
1220/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001221 * Get the lnum from the first argument.
1222 * Also accepts "$", then "buf" is used.
1223 * Returns 0 on error.
1224 */
1225 static linenr_T
1226get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
1227{
1228 if (argvars[0].v_type == VAR_STRING
1229 && argvars[0].vval.v_string != NULL
1230 && argvars[0].vval.v_string[0] == '$'
1231 && buf != NULL)
1232 return buf->b_ml.ml_line_count;
1233 return (linenr_T)get_tv_number_chk(&argvars[0], NULL);
1234}
1235
1236/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02001237 * If there is a window for "curbuf", make it the current window.
1238 */
1239 static void
1240find_win_for_curbuf(void)
1241{
1242 wininfo_T *wip;
1243
1244 for (wip = curbuf->b_wininfo; wip != NULL; wip = wip->wi_next)
1245 {
1246 if (wip->wi_win != NULL)
1247 {
1248 curwin = wip->wi_win;
1249 break;
1250 }
1251 }
1252}
1253
1254/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001255 * Set line or list of lines in buffer "buf".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001256 */
1257 static void
Bram Moolenaarca851592018-06-06 21:04:07 +02001258set_buffer_lines(
1259 buf_T *buf,
1260 linenr_T lnum_arg,
1261 int append,
1262 typval_T *lines,
1263 typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001264{
Bram Moolenaarca851592018-06-06 21:04:07 +02001265 linenr_T lnum = lnum_arg + (append ? 1 : 0);
1266 char_u *line = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001267 list_T *l = NULL;
1268 listitem_T *li = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001269 long added = 0;
Bram Moolenaarca851592018-06-06 21:04:07 +02001270 linenr_T append_lnum;
1271 buf_T *curbuf_save = NULL;
1272 win_T *curwin_save = NULL;
1273 int is_curbuf = buf == curbuf;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001274
Bram Moolenaarca851592018-06-06 21:04:07 +02001275 /* When using the current buffer ml_mfp will be set if needed. Useful when
1276 * setline() is used on startup. For other buffers the buffer must be
1277 * loaded. */
1278 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001279 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001280 rettv->vval.v_number = 1; /* FAIL */
1281 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001282 }
1283
Bram Moolenaarca851592018-06-06 21:04:07 +02001284 if (!is_curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001285 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001286 curbuf_save = curbuf;
1287 curwin_save = curwin;
1288 curbuf = buf;
Bram Moolenaard79a2622018-06-07 18:17:46 +02001289 find_win_for_curbuf();
Bram Moolenaarca851592018-06-06 21:04:07 +02001290 }
1291
1292 if (append)
1293 // appendbufline() uses the line number below which we insert
1294 append_lnum = lnum - 1;
1295 else
1296 // setbufline() uses the line number above which we insert, we only
1297 // append if it's below the last line
1298 append_lnum = curbuf->b_ml.ml_line_count;
1299
1300 if (lines->v_type == VAR_LIST)
1301 {
1302 l = lines->vval.v_list;
1303 li = l->lv_first;
1304 }
1305 else
1306 line = get_tv_string_chk(lines);
1307
1308 /* default result is zero == OK */
1309 for (;;)
1310 {
1311 if (l != NULL)
1312 {
1313 /* list argument, get next string */
1314 if (li == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001315 break;
Bram Moolenaarca851592018-06-06 21:04:07 +02001316 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001317 li = li->li_next;
1318 }
1319
Bram Moolenaarca851592018-06-06 21:04:07 +02001320 rettv->vval.v_number = 1; /* FAIL */
1321 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
1322 break;
1323
1324 /* When coming here from Insert mode, sync undo, so that this can be
1325 * undone separately from what was previously inserted. */
1326 if (u_sync_once == 2)
1327 {
1328 u_sync_once = 1; /* notify that u_sync() was called */
1329 u_sync(TRUE);
1330 }
1331
1332 if (!append && lnum <= curbuf->b_ml.ml_line_count)
1333 {
1334 /* existing line, replace it */
1335 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
1336 {
1337 changed_bytes(lnum, 0);
1338 if (is_curbuf && lnum == curwin->w_cursor.lnum)
1339 check_cursor_col();
1340 rettv->vval.v_number = 0; /* OK */
1341 }
1342 }
1343 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
1344 {
1345 /* append the line */
1346 ++added;
1347 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
1348 rettv->vval.v_number = 0; /* OK */
1349 }
1350
1351 if (l == NULL) /* only one string argument */
1352 break;
1353 ++lnum;
1354 }
1355
1356 if (added > 0)
1357 {
1358 win_T *wp;
1359 tabpage_T *tp;
1360
1361 appended_lines_mark(append_lnum, added);
1362 FOR_ALL_TAB_WINDOWS(tp, wp)
1363 if (wp->w_buffer == buf && wp->w_cursor.lnum > append_lnum)
1364 wp->w_cursor.lnum += added;
1365 check_cursor_col();
1366
Bram Moolenaarf2732452018-06-03 14:47:35 +02001367#ifdef FEAT_JOB_CHANNEL
1368 if (bt_prompt(curbuf) && (State & INSERT))
1369 // show the line with the prompt
1370 update_topline();
1371#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001372 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001373
1374 if (!is_curbuf)
1375 {
1376 curbuf = curbuf_save;
1377 curwin = curwin_save;
1378 }
1379}
1380
1381/*
1382 * "append(lnum, string/list)" function
1383 */
1384 static void
1385f_append(typval_T *argvars, typval_T *rettv)
1386{
1387 linenr_T lnum = get_tv_lnum(&argvars[0]);
1388
1389 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1390}
1391
1392/*
1393 * "appendbufline(buf, lnum, string/list)" function
1394 */
1395 static void
1396f_appendbufline(typval_T *argvars, typval_T *rettv)
1397{
1398 linenr_T lnum;
1399 buf_T *buf;
1400
1401 buf = get_buf_tv(&argvars[0], FALSE);
1402 if (buf == NULL)
1403 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001404 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001405 {
1406 lnum = get_tv_lnum_buf(&argvars[1], buf);
1407 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1408 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001409}
1410
1411/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001412 * "argc([window id])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001413 */
1414 static void
Bram Moolenaare6e39892018-10-25 12:32:11 +02001415f_argc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001416{
Bram Moolenaare6e39892018-10-25 12:32:11 +02001417 win_T *wp;
1418
1419 if (argvars[0].v_type == VAR_UNKNOWN)
1420 // use the current window
1421 rettv->vval.v_number = ARGCOUNT;
1422 else if (argvars[0].v_type == VAR_NUMBER
1423 && get_tv_number(&argvars[0]) == -1)
1424 // use the global argument list
1425 rettv->vval.v_number = GARGCOUNT;
1426 else
1427 {
1428 // use the argument list of the specified window
1429 wp = find_win_by_nr_or_id(&argvars[0]);
1430 if (wp != NULL)
1431 rettv->vval.v_number = WARGCOUNT(wp);
1432 else
1433 rettv->vval.v_number = -1;
1434 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001435}
1436
1437/*
1438 * "argidx()" function
1439 */
1440 static void
1441f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1442{
1443 rettv->vval.v_number = curwin->w_arg_idx;
1444}
1445
1446/*
1447 * "arglistid()" function
1448 */
1449 static void
1450f_arglistid(typval_T *argvars, typval_T *rettv)
1451{
1452 win_T *wp;
1453
1454 rettv->vval.v_number = -1;
1455 wp = find_tabwin(&argvars[0], &argvars[1]);
1456 if (wp != NULL)
1457 rettv->vval.v_number = wp->w_alist->id;
1458}
1459
1460/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001461 * Get the argument list for a given window
1462 */
1463 static void
1464get_arglist_as_rettv(aentry_T *arglist, int argcount, typval_T *rettv)
1465{
1466 int idx;
1467
1468 if (rettv_list_alloc(rettv) == OK && arglist != NULL)
1469 for (idx = 0; idx < argcount; ++idx)
1470 list_append_string(rettv->vval.v_list,
1471 alist_name(&arglist[idx]), -1);
1472}
1473
1474/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001475 * "argv(nr)" function
1476 */
1477 static void
1478f_argv(typval_T *argvars, typval_T *rettv)
1479{
1480 int idx;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001481 aentry_T *arglist = NULL;
1482 int argcount = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001483
1484 if (argvars[0].v_type != VAR_UNKNOWN)
1485 {
Bram Moolenaare6e39892018-10-25 12:32:11 +02001486 if (argvars[1].v_type == VAR_UNKNOWN)
1487 {
1488 arglist = ARGLIST;
1489 argcount = ARGCOUNT;
1490 }
1491 else if (argvars[1].v_type == VAR_NUMBER
1492 && get_tv_number(&argvars[1]) == -1)
1493 {
1494 arglist = GARGLIST;
1495 argcount = GARGCOUNT;
1496 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001497 else
Bram Moolenaare6e39892018-10-25 12:32:11 +02001498 {
1499 win_T *wp = find_win_by_nr_or_id(&argvars[1]);
1500
1501 if (wp != NULL)
1502 {
1503 /* Use the argument list of the specified window */
1504 arglist = WARGLIST(wp);
1505 argcount = WARGCOUNT(wp);
1506 }
1507 }
1508
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001509 rettv->v_type = VAR_STRING;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001510 rettv->vval.v_string = NULL;
1511 idx = get_tv_number_chk(&argvars[0], NULL);
1512 if (arglist != NULL && idx >= 0 && idx < argcount)
1513 rettv->vval.v_string = vim_strsave(alist_name(&arglist[idx]));
1514 else if (idx == -1)
1515 get_arglist_as_rettv(arglist, argcount, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001516 }
Bram Moolenaare6e39892018-10-25 12:32:11 +02001517 else
1518 get_arglist_as_rettv(ARGLIST, ARGCOUNT, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001519}
1520
1521/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001522 * "assert_beeps(cmd [, error])" function
1523 */
1524 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001525f_assert_beeps(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001526{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001527 rettv->vval.v_number = assert_beeps(argvars);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001528}
1529
1530/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001531 * "assert_equal(expected, actual[, msg])" function
1532 */
1533 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001534f_assert_equal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001535{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001536 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_EQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001537}
1538
1539/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01001540 * "assert_equalfile(fname-one, fname-two)" function
1541 */
1542 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001543f_assert_equalfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaard96ff162018-02-18 22:13:29 +01001544{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001545 rettv->vval.v_number = assert_equalfile(argvars);
Bram Moolenaard96ff162018-02-18 22:13:29 +01001546}
1547
1548/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001549 * "assert_notequal(expected, actual[, msg])" function
1550 */
1551 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001552f_assert_notequal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001553{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001554 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001555}
1556
1557/*
1558 * "assert_exception(string[, msg])" function
1559 */
1560 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001561f_assert_exception(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001562{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001563 rettv->vval.v_number = assert_exception(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001564}
1565
1566/*
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02001567 * "assert_fails(cmd [, error[, msg]])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001568 */
1569 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001570f_assert_fails(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001571{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001572 rettv->vval.v_number = assert_fails(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001573}
1574
1575/*
1576 * "assert_false(actual[, msg])" function
1577 */
1578 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001579f_assert_false(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001580{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001581 rettv->vval.v_number = assert_bool(argvars, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001582}
1583
1584/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001585 * "assert_inrange(lower, upper[, msg])" function
1586 */
1587 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001588f_assert_inrange(typval_T *argvars, typval_T *rettv)
Bram Moolenaar61c04492016-07-23 15:35:35 +02001589{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001590 rettv->vval.v_number = assert_inrange(argvars);
Bram Moolenaar61c04492016-07-23 15:35:35 +02001591}
1592
1593/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001594 * "assert_match(pattern, actual[, msg])" function
1595 */
1596 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001597f_assert_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001598{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001599 rettv->vval.v_number = assert_match_common(argvars, ASSERT_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001600}
1601
1602/*
1603 * "assert_notmatch(pattern, actual[, msg])" function
1604 */
1605 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001606f_assert_notmatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001607{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001608 rettv->vval.v_number = assert_match_common(argvars, ASSERT_NOTMATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001609}
1610
1611/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001612 * "assert_report(msg)" function
1613 */
1614 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001615f_assert_report(typval_T *argvars, typval_T *rettv)
Bram Moolenaar42205552017-03-18 19:42:22 +01001616{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001617 rettv->vval.v_number = assert_report(argvars);
Bram Moolenaar42205552017-03-18 19:42:22 +01001618}
1619
1620/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001621 * "assert_true(actual[, msg])" function
1622 */
1623 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001624f_assert_true(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001625{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001626 rettv->vval.v_number = assert_bool(argvars, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001627}
1628
1629#ifdef FEAT_FLOAT
1630/*
1631 * "asin()" function
1632 */
1633 static void
1634f_asin(typval_T *argvars, typval_T *rettv)
1635{
1636 float_T f = 0.0;
1637
1638 rettv->v_type = VAR_FLOAT;
1639 if (get_float_arg(argvars, &f) == OK)
1640 rettv->vval.v_float = asin(f);
1641 else
1642 rettv->vval.v_float = 0.0;
1643}
1644
1645/*
1646 * "atan()" function
1647 */
1648 static void
1649f_atan(typval_T *argvars, typval_T *rettv)
1650{
1651 float_T f = 0.0;
1652
1653 rettv->v_type = VAR_FLOAT;
1654 if (get_float_arg(argvars, &f) == OK)
1655 rettv->vval.v_float = atan(f);
1656 else
1657 rettv->vval.v_float = 0.0;
1658}
1659
1660/*
1661 * "atan2()" function
1662 */
1663 static void
1664f_atan2(typval_T *argvars, typval_T *rettv)
1665{
1666 float_T fx = 0.0, fy = 0.0;
1667
1668 rettv->v_type = VAR_FLOAT;
1669 if (get_float_arg(argvars, &fx) == OK
1670 && get_float_arg(&argvars[1], &fy) == OK)
1671 rettv->vval.v_float = atan2(fx, fy);
1672 else
1673 rettv->vval.v_float = 0.0;
1674}
1675#endif
1676
1677/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001678 * "balloon_show()" function
1679 */
1680#ifdef FEAT_BEVAL
1681 static void
1682f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1683{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001684 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001685 {
1686 if (argvars[0].v_type == VAR_LIST
1687# ifdef FEAT_GUI
1688 && !gui.in_use
1689# endif
1690 )
1691 post_balloon(balloonEval, NULL, argvars[0].vval.v_list);
1692 else
1693 post_balloon(balloonEval, get_tv_string_chk(&argvars[0]), NULL);
1694 }
1695}
1696
Bram Moolenaar669a8282017-11-19 20:13:05 +01001697# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001698 static void
1699f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1700{
1701 if (rettv_list_alloc(rettv) == OK)
1702 {
1703 char_u *msg = get_tv_string_chk(&argvars[0]);
1704
1705 if (msg != NULL)
1706 {
1707 pumitem_T *array;
1708 int size = split_message(msg, &array);
1709 int i;
1710
1711 /* Skip the first and last item, they are always empty. */
1712 for (i = 1; i < size - 1; ++i)
1713 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001714 while (size > 0)
1715 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001716 vim_free(array);
1717 }
1718 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001719}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001720# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001721#endif
1722
1723/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001724 * "browse(save, title, initdir, default)" function
1725 */
1726 static void
1727f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1728{
1729#ifdef FEAT_BROWSE
1730 int save;
1731 char_u *title;
1732 char_u *initdir;
1733 char_u *defname;
1734 char_u buf[NUMBUFLEN];
1735 char_u buf2[NUMBUFLEN];
1736 int error = FALSE;
1737
1738 save = (int)get_tv_number_chk(&argvars[0], &error);
1739 title = get_tv_string_chk(&argvars[1]);
1740 initdir = get_tv_string_buf_chk(&argvars[2], buf);
1741 defname = get_tv_string_buf_chk(&argvars[3], buf2);
1742
1743 if (error || title == NULL || initdir == NULL || defname == NULL)
1744 rettv->vval.v_string = NULL;
1745 else
1746 rettv->vval.v_string =
1747 do_browse(save ? BROWSE_SAVE : 0,
1748 title, defname, NULL, initdir, NULL, curbuf);
1749#else
1750 rettv->vval.v_string = NULL;
1751#endif
1752 rettv->v_type = VAR_STRING;
1753}
1754
1755/*
1756 * "browsedir(title, initdir)" function
1757 */
1758 static void
1759f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1760{
1761#ifdef FEAT_BROWSE
1762 char_u *title;
1763 char_u *initdir;
1764 char_u buf[NUMBUFLEN];
1765
1766 title = get_tv_string_chk(&argvars[0]);
1767 initdir = get_tv_string_buf_chk(&argvars[1], buf);
1768
1769 if (title == NULL || initdir == NULL)
1770 rettv->vval.v_string = NULL;
1771 else
1772 rettv->vval.v_string = do_browse(BROWSE_DIR,
1773 title, NULL, NULL, initdir, NULL, curbuf);
1774#else
1775 rettv->vval.v_string = NULL;
1776#endif
1777 rettv->v_type = VAR_STRING;
1778}
1779
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001780/*
1781 * Find a buffer by number or exact name.
1782 */
1783 static buf_T *
1784find_buffer(typval_T *avar)
1785{
1786 buf_T *buf = NULL;
1787
1788 if (avar->v_type == VAR_NUMBER)
1789 buf = buflist_findnr((int)avar->vval.v_number);
1790 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1791 {
1792 buf = buflist_findname_exp(avar->vval.v_string);
1793 if (buf == NULL)
1794 {
1795 /* No full path name match, try a match with a URL or a "nofile"
1796 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001797 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001798 if (buf->b_fname != NULL
1799 && (path_with_url(buf->b_fname)
1800#ifdef FEAT_QUICKFIX
1801 || bt_nofile(buf)
1802#endif
1803 )
1804 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1805 break;
1806 }
1807 }
1808 return buf;
1809}
1810
1811/*
1812 * "bufexists(expr)" function
1813 */
1814 static void
1815f_bufexists(typval_T *argvars, typval_T *rettv)
1816{
1817 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1818}
1819
1820/*
1821 * "buflisted(expr)" function
1822 */
1823 static void
1824f_buflisted(typval_T *argvars, typval_T *rettv)
1825{
1826 buf_T *buf;
1827
1828 buf = find_buffer(&argvars[0]);
1829 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1830}
1831
1832/*
1833 * "bufloaded(expr)" function
1834 */
1835 static void
1836f_bufloaded(typval_T *argvars, typval_T *rettv)
1837{
1838 buf_T *buf;
1839
1840 buf = find_buffer(&argvars[0]);
1841 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1842}
1843
1844 buf_T *
1845buflist_find_by_name(char_u *name, int curtab_only)
1846{
1847 int save_magic;
1848 char_u *save_cpo;
1849 buf_T *buf;
1850
1851 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1852 save_magic = p_magic;
1853 p_magic = TRUE;
1854 save_cpo = p_cpo;
1855 p_cpo = (char_u *)"";
1856
1857 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1858 TRUE, FALSE, curtab_only));
1859
1860 p_magic = save_magic;
1861 p_cpo = save_cpo;
1862 return buf;
1863}
1864
1865/*
1866 * Get buffer by number or pattern.
1867 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001868 buf_T *
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001869get_buf_tv(typval_T *tv, int curtab_only)
1870{
1871 char_u *name = tv->vval.v_string;
1872 buf_T *buf;
1873
1874 if (tv->v_type == VAR_NUMBER)
1875 return buflist_findnr((int)tv->vval.v_number);
1876 if (tv->v_type != VAR_STRING)
1877 return NULL;
1878 if (name == NULL || *name == NUL)
1879 return curbuf;
1880 if (name[0] == '$' && name[1] == NUL)
1881 return lastbuf;
1882
1883 buf = buflist_find_by_name(name, curtab_only);
1884
1885 /* If not found, try expanding the name, like done for bufexists(). */
1886 if (buf == NULL)
1887 buf = find_buffer(tv);
1888
1889 return buf;
1890}
1891
1892/*
1893 * "bufname(expr)" function
1894 */
1895 static void
1896f_bufname(typval_T *argvars, typval_T *rettv)
1897{
1898 buf_T *buf;
1899
1900 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1901 ++emsg_off;
1902 buf = get_buf_tv(&argvars[0], FALSE);
1903 rettv->v_type = VAR_STRING;
1904 if (buf != NULL && buf->b_fname != NULL)
1905 rettv->vval.v_string = vim_strsave(buf->b_fname);
1906 else
1907 rettv->vval.v_string = NULL;
1908 --emsg_off;
1909}
1910
1911/*
1912 * "bufnr(expr)" function
1913 */
1914 static void
1915f_bufnr(typval_T *argvars, typval_T *rettv)
1916{
1917 buf_T *buf;
1918 int error = FALSE;
1919 char_u *name;
1920
1921 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1922 ++emsg_off;
1923 buf = get_buf_tv(&argvars[0], FALSE);
1924 --emsg_off;
1925
1926 /* If the buffer isn't found and the second argument is not zero create a
1927 * new buffer. */
1928 if (buf == NULL
1929 && argvars[1].v_type != VAR_UNKNOWN
1930 && get_tv_number_chk(&argvars[1], &error) != 0
1931 && !error
1932 && (name = get_tv_string_chk(&argvars[0])) != NULL
1933 && !error)
1934 buf = buflist_new(name, NULL, (linenr_T)1, 0);
1935
1936 if (buf != NULL)
1937 rettv->vval.v_number = buf->b_fnum;
1938 else
1939 rettv->vval.v_number = -1;
1940}
1941
1942 static void
1943buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
1944{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001945 win_T *wp;
1946 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001947 buf_T *buf;
1948
1949 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1950 ++emsg_off;
1951 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02001952 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001953 {
1954 ++winnr;
1955 if (wp->w_buffer == buf)
1956 break;
1957 }
1958 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001959 --emsg_off;
1960}
1961
1962/*
1963 * "bufwinid(nr)" function
1964 */
1965 static void
1966f_bufwinid(typval_T *argvars, typval_T *rettv)
1967{
1968 buf_win_common(argvars, rettv, FALSE);
1969}
1970
1971/*
1972 * "bufwinnr(nr)" function
1973 */
1974 static void
1975f_bufwinnr(typval_T *argvars, typval_T *rettv)
1976{
1977 buf_win_common(argvars, rettv, TRUE);
1978}
1979
1980/*
1981 * "byte2line(byte)" function
1982 */
1983 static void
1984f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
1985{
1986#ifndef FEAT_BYTEOFF
1987 rettv->vval.v_number = -1;
1988#else
1989 long boff = 0;
1990
1991 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
1992 if (boff < 0)
1993 rettv->vval.v_number = -1;
1994 else
1995 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
1996 (linenr_T)0, &boff);
1997#endif
1998}
1999
2000 static void
2001byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2002{
2003#ifdef FEAT_MBYTE
2004 char_u *t;
2005#endif
2006 char_u *str;
2007 varnumber_T idx;
2008
2009 str = get_tv_string_chk(&argvars[0]);
2010 idx = get_tv_number_chk(&argvars[1], NULL);
2011 rettv->vval.v_number = -1;
2012 if (str == NULL || idx < 0)
2013 return;
2014
2015#ifdef FEAT_MBYTE
2016 t = str;
2017 for ( ; idx > 0; idx--)
2018 {
2019 if (*t == NUL) /* EOL reached */
2020 return;
2021 if (enc_utf8 && comp)
2022 t += utf_ptr2len(t);
2023 else
2024 t += (*mb_ptr2len)(t);
2025 }
2026 rettv->vval.v_number = (varnumber_T)(t - str);
2027#else
2028 if ((size_t)idx <= STRLEN(str))
2029 rettv->vval.v_number = idx;
2030#endif
2031}
2032
2033/*
2034 * "byteidx()" function
2035 */
2036 static void
2037f_byteidx(typval_T *argvars, typval_T *rettv)
2038{
2039 byteidx(argvars, rettv, FALSE);
2040}
2041
2042/*
2043 * "byteidxcomp()" function
2044 */
2045 static void
2046f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2047{
2048 byteidx(argvars, rettv, TRUE);
2049}
2050
2051/*
2052 * "call(func, arglist [, dict])" function
2053 */
2054 static void
2055f_call(typval_T *argvars, typval_T *rettv)
2056{
2057 char_u *func;
2058 partial_T *partial = NULL;
2059 dict_T *selfdict = NULL;
2060
2061 if (argvars[1].v_type != VAR_LIST)
2062 {
2063 EMSG(_(e_listreq));
2064 return;
2065 }
2066 if (argvars[1].vval.v_list == NULL)
2067 return;
2068
2069 if (argvars[0].v_type == VAR_FUNC)
2070 func = argvars[0].vval.v_string;
2071 else if (argvars[0].v_type == VAR_PARTIAL)
2072 {
2073 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002074 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002075 }
2076 else
2077 func = get_tv_string(&argvars[0]);
2078 if (*func == NUL)
2079 return; /* type error or empty name */
2080
2081 if (argvars[2].v_type != VAR_UNKNOWN)
2082 {
2083 if (argvars[2].v_type != VAR_DICT)
2084 {
2085 EMSG(_(e_dictreq));
2086 return;
2087 }
2088 selfdict = argvars[2].vval.v_dict;
2089 }
2090
2091 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2092}
2093
2094#ifdef FEAT_FLOAT
2095/*
2096 * "ceil({float})" function
2097 */
2098 static void
2099f_ceil(typval_T *argvars, typval_T *rettv)
2100{
2101 float_T f = 0.0;
2102
2103 rettv->v_type = VAR_FLOAT;
2104 if (get_float_arg(argvars, &f) == OK)
2105 rettv->vval.v_float = ceil(f);
2106 else
2107 rettv->vval.v_float = 0.0;
2108}
2109#endif
2110
2111#ifdef FEAT_JOB_CHANNEL
2112/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002113 * "ch_canread()" function
2114 */
2115 static void
2116f_ch_canread(typval_T *argvars, typval_T *rettv)
2117{
Bram Moolenaar958dc692016-12-01 15:34:12 +01002118 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002119
2120 rettv->vval.v_number = 0;
2121 if (channel != NULL)
2122 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
2123 || channel_has_readahead(channel, PART_OUT)
2124 || channel_has_readahead(channel, PART_ERR);
2125}
2126
2127/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002128 * "ch_close()" function
2129 */
2130 static void
2131f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
2132{
2133 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2134
2135 if (channel != NULL)
2136 {
2137 channel_close(channel, FALSE);
2138 channel_clear(channel);
2139 }
2140}
2141
2142/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002143 * "ch_close()" function
2144 */
2145 static void
2146f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
2147{
2148 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2149
2150 if (channel != NULL)
2151 channel_close_in(channel);
2152}
2153
2154/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002155 * "ch_getbufnr()" function
2156 */
2157 static void
2158f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
2159{
2160 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2161
2162 rettv->vval.v_number = -1;
2163 if (channel != NULL)
2164 {
2165 char_u *what = get_tv_string(&argvars[1]);
2166 int part;
2167
2168 if (STRCMP(what, "err") == 0)
2169 part = PART_ERR;
2170 else if (STRCMP(what, "out") == 0)
2171 part = PART_OUT;
2172 else if (STRCMP(what, "in") == 0)
2173 part = PART_IN;
2174 else
2175 part = PART_SOCK;
2176 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
2177 rettv->vval.v_number =
2178 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
2179 }
2180}
2181
2182/*
2183 * "ch_getjob()" function
2184 */
2185 static void
2186f_ch_getjob(typval_T *argvars, typval_T *rettv)
2187{
2188 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2189
2190 if (channel != NULL)
2191 {
2192 rettv->v_type = VAR_JOB;
2193 rettv->vval.v_job = channel->ch_job;
2194 if (channel->ch_job != NULL)
2195 ++channel->ch_job->jv_refcount;
2196 }
2197}
2198
2199/*
2200 * "ch_info()" function
2201 */
2202 static void
2203f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
2204{
2205 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2206
2207 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
2208 channel_info(channel, rettv->vval.v_dict);
2209}
2210
2211/*
2212 * "ch_log()" function
2213 */
2214 static void
2215f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
2216{
2217 char_u *msg = get_tv_string(&argvars[0]);
2218 channel_T *channel = NULL;
2219
2220 if (argvars[1].v_type != VAR_UNKNOWN)
2221 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
2222
Bram Moolenaard5359b22018-04-05 22:44:39 +02002223 ch_log(channel, "%s", msg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002224}
2225
2226/*
2227 * "ch_logfile()" function
2228 */
2229 static void
2230f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
2231{
2232 char_u *fname;
2233 char_u *opt = (char_u *)"";
2234 char_u buf[NUMBUFLEN];
2235
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02002236 /* Don't open a file in restricted mode. */
2237 if (check_restricted() || check_secure())
2238 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002239 fname = get_tv_string(&argvars[0]);
2240 if (argvars[1].v_type == VAR_STRING)
2241 opt = get_tv_string_buf(&argvars[1], buf);
2242 ch_logfile(fname, opt);
2243}
2244
2245/*
2246 * "ch_open()" function
2247 */
2248 static void
2249f_ch_open(typval_T *argvars, typval_T *rettv)
2250{
2251 rettv->v_type = VAR_CHANNEL;
2252 if (check_restricted() || check_secure())
2253 return;
2254 rettv->vval.v_channel = channel_open_func(argvars);
2255}
2256
2257/*
2258 * "ch_read()" function
2259 */
2260 static void
2261f_ch_read(typval_T *argvars, typval_T *rettv)
2262{
2263 common_channel_read(argvars, rettv, FALSE);
2264}
2265
2266/*
2267 * "ch_readraw()" function
2268 */
2269 static void
2270f_ch_readraw(typval_T *argvars, typval_T *rettv)
2271{
2272 common_channel_read(argvars, rettv, TRUE);
2273}
2274
2275/*
2276 * "ch_evalexpr()" function
2277 */
2278 static void
2279f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2280{
2281 ch_expr_common(argvars, rettv, TRUE);
2282}
2283
2284/*
2285 * "ch_sendexpr()" function
2286 */
2287 static void
2288f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2289{
2290 ch_expr_common(argvars, rettv, FALSE);
2291}
2292
2293/*
2294 * "ch_evalraw()" function
2295 */
2296 static void
2297f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2298{
2299 ch_raw_common(argvars, rettv, TRUE);
2300}
2301
2302/*
2303 * "ch_sendraw()" function
2304 */
2305 static void
2306f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2307{
2308 ch_raw_common(argvars, rettv, FALSE);
2309}
2310
2311/*
2312 * "ch_setoptions()" function
2313 */
2314 static void
2315f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2316{
2317 channel_T *channel;
2318 jobopt_T opt;
2319
2320 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2321 if (channel == NULL)
2322 return;
2323 clear_job_options(&opt);
2324 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002325 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002326 channel_set_options(channel, &opt);
2327 free_job_options(&opt);
2328}
2329
2330/*
2331 * "ch_status()" function
2332 */
2333 static void
2334f_ch_status(typval_T *argvars, typval_T *rettv)
2335{
2336 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002337 jobopt_T opt;
2338 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002339
2340 /* return an empty string by default */
2341 rettv->v_type = VAR_STRING;
2342 rettv->vval.v_string = NULL;
2343
2344 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002345
2346 if (argvars[1].v_type != VAR_UNKNOWN)
2347 {
2348 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002349 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002350 && (opt.jo_set & JO_PART))
2351 part = opt.jo_part;
2352 }
2353
2354 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002355}
2356#endif
2357
2358/*
2359 * "changenr()" function
2360 */
2361 static void
2362f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2363{
2364 rettv->vval.v_number = curbuf->b_u_seq_cur;
2365}
2366
2367/*
2368 * "char2nr(string)" function
2369 */
2370 static void
2371f_char2nr(typval_T *argvars, typval_T *rettv)
2372{
2373#ifdef FEAT_MBYTE
2374 if (has_mbyte)
2375 {
2376 int utf8 = 0;
2377
2378 if (argvars[1].v_type != VAR_UNKNOWN)
2379 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
2380
2381 if (utf8)
2382 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
2383 else
2384 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
2385 }
2386 else
2387#endif
2388 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
2389}
2390
2391/*
2392 * "cindent(lnum)" function
2393 */
2394 static void
2395f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2396{
2397#ifdef FEAT_CINDENT
2398 pos_T pos;
2399 linenr_T lnum;
2400
2401 pos = curwin->w_cursor;
2402 lnum = get_tv_lnum(argvars);
2403 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2404 {
2405 curwin->w_cursor.lnum = lnum;
2406 rettv->vval.v_number = get_c_indent();
2407 curwin->w_cursor = pos;
2408 }
2409 else
2410#endif
2411 rettv->vval.v_number = -1;
2412}
2413
2414/*
2415 * "clearmatches()" function
2416 */
2417 static void
2418f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2419{
2420#ifdef FEAT_SEARCH_EXTRA
2421 clear_matches(curwin);
2422#endif
2423}
2424
2425/*
2426 * "col(string)" function
2427 */
2428 static void
2429f_col(typval_T *argvars, typval_T *rettv)
2430{
2431 colnr_T col = 0;
2432 pos_T *fp;
2433 int fnum = curbuf->b_fnum;
2434
2435 fp = var2fpos(&argvars[0], FALSE, &fnum);
2436 if (fp != NULL && fnum == curbuf->b_fnum)
2437 {
2438 if (fp->col == MAXCOL)
2439 {
2440 /* '> can be MAXCOL, get the length of the line then */
2441 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2442 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2443 else
2444 col = MAXCOL;
2445 }
2446 else
2447 {
2448 col = fp->col + 1;
2449#ifdef FEAT_VIRTUALEDIT
2450 /* col(".") when the cursor is on the NUL at the end of the line
2451 * because of "coladd" can be seen as an extra column. */
2452 if (virtual_active() && fp == &curwin->w_cursor)
2453 {
2454 char_u *p = ml_get_cursor();
2455
2456 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2457 curwin->w_virtcol - curwin->w_cursor.coladd))
2458 {
2459# ifdef FEAT_MBYTE
2460 int l;
2461
2462 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2463 col += l;
2464# else
2465 if (*p != NUL && p[1] == NUL)
2466 ++col;
2467# endif
2468 }
2469 }
2470#endif
2471 }
2472 }
2473 rettv->vval.v_number = col;
2474}
2475
2476#if defined(FEAT_INS_EXPAND)
2477/*
2478 * "complete()" function
2479 */
2480 static void
2481f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2482{
2483 int startcol;
2484
2485 if ((State & INSERT) == 0)
2486 {
2487 EMSG(_("E785: complete() can only be used in Insert mode"));
2488 return;
2489 }
2490
2491 /* Check for undo allowed here, because if something was already inserted
2492 * the line was already saved for undo and this check isn't done. */
2493 if (!undo_allowed())
2494 return;
2495
2496 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2497 {
2498 EMSG(_(e_invarg));
2499 return;
2500 }
2501
2502 startcol = (int)get_tv_number_chk(&argvars[0], NULL);
2503 if (startcol <= 0)
2504 return;
2505
2506 set_completion(startcol - 1, argvars[1].vval.v_list);
2507}
2508
2509/*
2510 * "complete_add()" function
2511 */
2512 static void
2513f_complete_add(typval_T *argvars, typval_T *rettv)
2514{
2515 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2516}
2517
2518/*
2519 * "complete_check()" function
2520 */
2521 static void
2522f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2523{
2524 int saved = RedrawingDisabled;
2525
2526 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002527 ins_compl_check_keys(0, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002528 rettv->vval.v_number = compl_interrupted;
2529 RedrawingDisabled = saved;
2530}
2531#endif
2532
2533/*
2534 * "confirm(message, buttons[, default [, type]])" function
2535 */
2536 static void
2537f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2538{
2539#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2540 char_u *message;
2541 char_u *buttons = NULL;
2542 char_u buf[NUMBUFLEN];
2543 char_u buf2[NUMBUFLEN];
2544 int def = 1;
2545 int type = VIM_GENERIC;
2546 char_u *typestr;
2547 int error = FALSE;
2548
2549 message = get_tv_string_chk(&argvars[0]);
2550 if (message == NULL)
2551 error = TRUE;
2552 if (argvars[1].v_type != VAR_UNKNOWN)
2553 {
2554 buttons = get_tv_string_buf_chk(&argvars[1], buf);
2555 if (buttons == NULL)
2556 error = TRUE;
2557 if (argvars[2].v_type != VAR_UNKNOWN)
2558 {
2559 def = (int)get_tv_number_chk(&argvars[2], &error);
2560 if (argvars[3].v_type != VAR_UNKNOWN)
2561 {
2562 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
2563 if (typestr == NULL)
2564 error = TRUE;
2565 else
2566 {
2567 switch (TOUPPER_ASC(*typestr))
2568 {
2569 case 'E': type = VIM_ERROR; break;
2570 case 'Q': type = VIM_QUESTION; break;
2571 case 'I': type = VIM_INFO; break;
2572 case 'W': type = VIM_WARNING; break;
2573 case 'G': type = VIM_GENERIC; break;
2574 }
2575 }
2576 }
2577 }
2578 }
2579
2580 if (buttons == NULL || *buttons == NUL)
2581 buttons = (char_u *)_("&Ok");
2582
2583 if (!error)
2584 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2585 def, NULL, FALSE);
2586#endif
2587}
2588
2589/*
2590 * "copy()" function
2591 */
2592 static void
2593f_copy(typval_T *argvars, typval_T *rettv)
2594{
2595 item_copy(&argvars[0], rettv, FALSE, 0);
2596}
2597
2598#ifdef FEAT_FLOAT
2599/*
2600 * "cos()" function
2601 */
2602 static void
2603f_cos(typval_T *argvars, typval_T *rettv)
2604{
2605 float_T f = 0.0;
2606
2607 rettv->v_type = VAR_FLOAT;
2608 if (get_float_arg(argvars, &f) == OK)
2609 rettv->vval.v_float = cos(f);
2610 else
2611 rettv->vval.v_float = 0.0;
2612}
2613
2614/*
2615 * "cosh()" function
2616 */
2617 static void
2618f_cosh(typval_T *argvars, typval_T *rettv)
2619{
2620 float_T f = 0.0;
2621
2622 rettv->v_type = VAR_FLOAT;
2623 if (get_float_arg(argvars, &f) == OK)
2624 rettv->vval.v_float = cosh(f);
2625 else
2626 rettv->vval.v_float = 0.0;
2627}
2628#endif
2629
2630/*
2631 * "count()" function
2632 */
2633 static void
2634f_count(typval_T *argvars, typval_T *rettv)
2635{
2636 long n = 0;
2637 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002638 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002639
Bram Moolenaar9966b212017-07-28 16:46:57 +02002640 if (argvars[2].v_type != VAR_UNKNOWN)
2641 ic = (int)get_tv_number_chk(&argvars[2], &error);
2642
2643 if (argvars[0].v_type == VAR_STRING)
2644 {
2645 char_u *expr = get_tv_string_chk(&argvars[1]);
2646 char_u *p = argvars[0].vval.v_string;
2647 char_u *next;
2648
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002649 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002650 {
2651 if (ic)
2652 {
2653 size_t len = STRLEN(expr);
2654
2655 while (*p != NUL)
2656 {
2657 if (MB_STRNICMP(p, expr, len) == 0)
2658 {
2659 ++n;
2660 p += len;
2661 }
2662 else
2663 MB_PTR_ADV(p);
2664 }
2665 }
2666 else
2667 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2668 != NULL)
2669 {
2670 ++n;
2671 p = next + STRLEN(expr);
2672 }
2673 }
2674
2675 }
2676 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002677 {
2678 listitem_T *li;
2679 list_T *l;
2680 long idx;
2681
2682 if ((l = argvars[0].vval.v_list) != NULL)
2683 {
2684 li = l->lv_first;
2685 if (argvars[2].v_type != VAR_UNKNOWN)
2686 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002687 if (argvars[3].v_type != VAR_UNKNOWN)
2688 {
2689 idx = (long)get_tv_number_chk(&argvars[3], &error);
2690 if (!error)
2691 {
2692 li = list_find(l, idx);
2693 if (li == NULL)
2694 EMSGN(_(e_listidx), idx);
2695 }
2696 }
2697 if (error)
2698 li = NULL;
2699 }
2700
2701 for ( ; li != NULL; li = li->li_next)
2702 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2703 ++n;
2704 }
2705 }
2706 else if (argvars[0].v_type == VAR_DICT)
2707 {
2708 int todo;
2709 dict_T *d;
2710 hashitem_T *hi;
2711
2712 if ((d = argvars[0].vval.v_dict) != NULL)
2713 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002714 if (argvars[2].v_type != VAR_UNKNOWN)
2715 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002716 if (argvars[3].v_type != VAR_UNKNOWN)
2717 EMSG(_(e_invarg));
2718 }
2719
2720 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2721 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2722 {
2723 if (!HASHITEM_EMPTY(hi))
2724 {
2725 --todo;
2726 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2727 ++n;
2728 }
2729 }
2730 }
2731 }
2732 else
2733 EMSG2(_(e_listdictarg), "count()");
2734 rettv->vval.v_number = n;
2735}
2736
2737/*
2738 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2739 *
2740 * Checks the existence of a cscope connection.
2741 */
2742 static void
2743f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2744{
2745#ifdef FEAT_CSCOPE
2746 int num = 0;
2747 char_u *dbpath = NULL;
2748 char_u *prepend = NULL;
2749 char_u buf[NUMBUFLEN];
2750
2751 if (argvars[0].v_type != VAR_UNKNOWN
2752 && argvars[1].v_type != VAR_UNKNOWN)
2753 {
2754 num = (int)get_tv_number(&argvars[0]);
2755 dbpath = get_tv_string(&argvars[1]);
2756 if (argvars[2].v_type != VAR_UNKNOWN)
2757 prepend = get_tv_string_buf(&argvars[2], buf);
2758 }
2759
2760 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2761#endif
2762}
2763
2764/*
2765 * "cursor(lnum, col)" function, or
2766 * "cursor(list)"
2767 *
2768 * Moves the cursor to the specified line and column.
2769 * Returns 0 when the position could be set, -1 otherwise.
2770 */
2771 static void
2772f_cursor(typval_T *argvars, typval_T *rettv)
2773{
2774 long line, col;
2775#ifdef FEAT_VIRTUALEDIT
2776 long coladd = 0;
2777#endif
2778 int set_curswant = TRUE;
2779
2780 rettv->vval.v_number = -1;
2781 if (argvars[1].v_type == VAR_UNKNOWN)
2782 {
2783 pos_T pos;
2784 colnr_T curswant = -1;
2785
2786 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2787 {
2788 EMSG(_(e_invarg));
2789 return;
2790 }
2791 line = pos.lnum;
2792 col = pos.col;
2793#ifdef FEAT_VIRTUALEDIT
2794 coladd = pos.coladd;
2795#endif
2796 if (curswant >= 0)
2797 {
2798 curwin->w_curswant = curswant - 1;
2799 set_curswant = FALSE;
2800 }
2801 }
2802 else
2803 {
2804 line = get_tv_lnum(argvars);
2805 col = (long)get_tv_number_chk(&argvars[1], NULL);
2806#ifdef FEAT_VIRTUALEDIT
2807 if (argvars[2].v_type != VAR_UNKNOWN)
2808 coladd = (long)get_tv_number_chk(&argvars[2], NULL);
2809#endif
2810 }
2811 if (line < 0 || col < 0
2812#ifdef FEAT_VIRTUALEDIT
2813 || coladd < 0
2814#endif
2815 )
2816 return; /* type error; errmsg already given */
2817 if (line > 0)
2818 curwin->w_cursor.lnum = line;
2819 if (col > 0)
2820 curwin->w_cursor.col = col - 1;
2821#ifdef FEAT_VIRTUALEDIT
2822 curwin->w_cursor.coladd = coladd;
2823#endif
2824
2825 /* Make sure the cursor is in a valid position. */
2826 check_cursor();
2827#ifdef FEAT_MBYTE
2828 /* Correct cursor for multi-byte character. */
2829 if (has_mbyte)
2830 mb_adjust_cursor();
2831#endif
2832
2833 curwin->w_set_curswant = set_curswant;
2834 rettv->vval.v_number = 0;
2835}
2836
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002837#ifdef WIN3264
2838/*
2839 * "debugbreak()" function
2840 */
2841 static void
2842f_debugbreak(typval_T *argvars, typval_T *rettv)
2843{
2844 int pid;
2845
2846 rettv->vval.v_number = FAIL;
2847 pid = (int)get_tv_number(&argvars[0]);
2848 if (pid == 0)
2849 EMSG(_(e_invarg));
2850 else
2851 {
2852 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2853
2854 if (hProcess != NULL)
2855 {
2856 DebugBreakProcess(hProcess);
2857 CloseHandle(hProcess);
2858 rettv->vval.v_number = OK;
2859 }
2860 }
2861}
2862#endif
2863
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002864/*
2865 * "deepcopy()" function
2866 */
2867 static void
2868f_deepcopy(typval_T *argvars, typval_T *rettv)
2869{
2870 int noref = 0;
2871 int copyID;
2872
2873 if (argvars[1].v_type != VAR_UNKNOWN)
2874 noref = (int)get_tv_number_chk(&argvars[1], NULL);
2875 if (noref < 0 || noref > 1)
2876 EMSG(_(e_invarg));
2877 else
2878 {
2879 copyID = get_copyID();
2880 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2881 }
2882}
2883
2884/*
2885 * "delete()" function
2886 */
2887 static void
2888f_delete(typval_T *argvars, typval_T *rettv)
2889{
2890 char_u nbuf[NUMBUFLEN];
2891 char_u *name;
2892 char_u *flags;
2893
2894 rettv->vval.v_number = -1;
2895 if (check_restricted() || check_secure())
2896 return;
2897
2898 name = get_tv_string(&argvars[0]);
2899 if (name == NULL || *name == NUL)
2900 {
2901 EMSG(_(e_invarg));
2902 return;
2903 }
2904
2905 if (argvars[1].v_type != VAR_UNKNOWN)
2906 flags = get_tv_string_buf(&argvars[1], nbuf);
2907 else
2908 flags = (char_u *)"";
2909
2910 if (*flags == NUL)
2911 /* delete a file */
2912 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2913 else if (STRCMP(flags, "d") == 0)
2914 /* delete an empty directory */
2915 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2916 else if (STRCMP(flags, "rf") == 0)
2917 /* delete a directory recursively */
2918 rettv->vval.v_number = delete_recursive(name);
2919 else
2920 EMSG2(_(e_invexpr2), flags);
2921}
2922
2923/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02002924 * "deletebufline()" function
2925 */
2926 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02002927f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02002928{
2929 buf_T *buf;
2930 linenr_T first, last;
2931 linenr_T lnum;
2932 long count;
2933 int is_curbuf;
2934 buf_T *curbuf_save = NULL;
2935 win_T *curwin_save = NULL;
2936 tabpage_T *tp;
2937 win_T *wp;
2938
2939 buf = get_buf_tv(&argvars[0], FALSE);
2940 if (buf == NULL)
2941 {
2942 rettv->vval.v_number = 1; /* FAIL */
2943 return;
2944 }
2945 is_curbuf = buf == curbuf;
2946
2947 first = get_tv_lnum_buf(&argvars[1], buf);
2948 if (argvars[2].v_type != VAR_UNKNOWN)
2949 last = get_tv_lnum_buf(&argvars[2], buf);
2950 else
2951 last = first;
2952
2953 if (buf->b_ml.ml_mfp == NULL || first < 1
2954 || first > buf->b_ml.ml_line_count || last < first)
2955 {
2956 rettv->vval.v_number = 1; /* FAIL */
2957 return;
2958 }
2959
2960 if (!is_curbuf)
2961 {
2962 curbuf_save = curbuf;
2963 curwin_save = curwin;
2964 curbuf = buf;
2965 find_win_for_curbuf();
2966 }
2967 if (last > curbuf->b_ml.ml_line_count)
2968 last = curbuf->b_ml.ml_line_count;
2969 count = last - first + 1;
2970
2971 // When coming here from Insert mode, sync undo, so that this can be
2972 // undone separately from what was previously inserted.
2973 if (u_sync_once == 2)
2974 {
2975 u_sync_once = 1; // notify that u_sync() was called
2976 u_sync(TRUE);
2977 }
2978
2979 if (u_save(first - 1, last + 1) == FAIL)
2980 {
2981 rettv->vval.v_number = 1; /* FAIL */
2982 return;
2983 }
2984
2985 for (lnum = first; lnum <= last; ++lnum)
2986 ml_delete(first, TRUE);
2987
2988 FOR_ALL_TAB_WINDOWS(tp, wp)
2989 if (wp->w_buffer == buf)
2990 {
2991 if (wp->w_cursor.lnum > last)
2992 wp->w_cursor.lnum -= count;
2993 else if (wp->w_cursor.lnum> first)
2994 wp->w_cursor.lnum = first;
2995 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
2996 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
2997 }
2998 check_cursor_col();
2999 deleted_lines_mark(first, count);
3000
3001 if (!is_curbuf)
3002 {
3003 curbuf = curbuf_save;
3004 curwin = curwin_save;
3005 }
3006}
3007
3008/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003009 * "did_filetype()" function
3010 */
3011 static void
3012f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3013{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003014 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003015}
3016
3017/*
3018 * "diff_filler()" function
3019 */
3020 static void
3021f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3022{
3023#ifdef FEAT_DIFF
3024 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
3025#endif
3026}
3027
3028/*
3029 * "diff_hlID()" function
3030 */
3031 static void
3032f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3033{
3034#ifdef FEAT_DIFF
3035 linenr_T lnum = get_tv_lnum(argvars);
3036 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01003037 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003038 static int fnum = 0;
3039 static int change_start = 0;
3040 static int change_end = 0;
3041 static hlf_T hlID = (hlf_T)0;
3042 int filler_lines;
3043 int col;
3044
3045 if (lnum < 0) /* ignore type error in {lnum} arg */
3046 lnum = 0;
3047 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003048 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003049 || fnum != curbuf->b_fnum)
3050 {
3051 /* New line, buffer, change: need to get the values. */
3052 filler_lines = diff_check(curwin, lnum);
3053 if (filler_lines < 0)
3054 {
3055 if (filler_lines == -1)
3056 {
3057 change_start = MAXCOL;
3058 change_end = -1;
3059 if (diff_find_change(curwin, lnum, &change_start, &change_end))
3060 hlID = HLF_ADD; /* added line */
3061 else
3062 hlID = HLF_CHD; /* changed line */
3063 }
3064 else
3065 hlID = HLF_ADD; /* added line */
3066 }
3067 else
3068 hlID = (hlf_T)0;
3069 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003070 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003071 fnum = curbuf->b_fnum;
3072 }
3073
3074 if (hlID == HLF_CHD || hlID == HLF_TXD)
3075 {
3076 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
3077 if (col >= change_start && col <= change_end)
3078 hlID = HLF_TXD; /* changed text */
3079 else
3080 hlID = HLF_CHD; /* changed line */
3081 }
3082 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
3083#endif
3084}
3085
3086/*
3087 * "empty({expr})" function
3088 */
3089 static void
3090f_empty(typval_T *argvars, typval_T *rettv)
3091{
3092 int n = FALSE;
3093
3094 switch (argvars[0].v_type)
3095 {
3096 case VAR_STRING:
3097 case VAR_FUNC:
3098 n = argvars[0].vval.v_string == NULL
3099 || *argvars[0].vval.v_string == NUL;
3100 break;
3101 case VAR_PARTIAL:
3102 n = FALSE;
3103 break;
3104 case VAR_NUMBER:
3105 n = argvars[0].vval.v_number == 0;
3106 break;
3107 case VAR_FLOAT:
3108#ifdef FEAT_FLOAT
3109 n = argvars[0].vval.v_float == 0.0;
3110 break;
3111#endif
3112 case VAR_LIST:
3113 n = argvars[0].vval.v_list == NULL
3114 || argvars[0].vval.v_list->lv_first == NULL;
3115 break;
3116 case VAR_DICT:
3117 n = argvars[0].vval.v_dict == NULL
3118 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
3119 break;
3120 case VAR_SPECIAL:
3121 n = argvars[0].vval.v_number != VVAL_TRUE;
3122 break;
3123
3124 case VAR_JOB:
3125#ifdef FEAT_JOB_CHANNEL
3126 n = argvars[0].vval.v_job == NULL
3127 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
3128 break;
3129#endif
3130 case VAR_CHANNEL:
3131#ifdef FEAT_JOB_CHANNEL
3132 n = argvars[0].vval.v_channel == NULL
3133 || !channel_is_open(argvars[0].vval.v_channel);
3134 break;
3135#endif
3136 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01003137 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003138 n = TRUE;
3139 break;
3140 }
3141
3142 rettv->vval.v_number = n;
3143}
3144
3145/*
3146 * "escape({string}, {chars})" function
3147 */
3148 static void
3149f_escape(typval_T *argvars, typval_T *rettv)
3150{
3151 char_u buf[NUMBUFLEN];
3152
3153 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
3154 get_tv_string_buf(&argvars[1], buf));
3155 rettv->v_type = VAR_STRING;
3156}
3157
3158/*
3159 * "eval()" function
3160 */
3161 static void
3162f_eval(typval_T *argvars, typval_T *rettv)
3163{
3164 char_u *s, *p;
3165
3166 s = get_tv_string_chk(&argvars[0]);
3167 if (s != NULL)
3168 s = skipwhite(s);
3169
3170 p = s;
3171 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3172 {
3173 if (p != NULL && !aborting())
3174 EMSG2(_(e_invexpr2), p);
3175 need_clr_eos = FALSE;
3176 rettv->v_type = VAR_NUMBER;
3177 rettv->vval.v_number = 0;
3178 }
3179 else if (*s != NUL)
3180 EMSG(_(e_trailing));
3181}
3182
3183/*
3184 * "eventhandler()" function
3185 */
3186 static void
3187f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3188{
3189 rettv->vval.v_number = vgetc_busy;
3190}
3191
3192/*
3193 * "executable()" function
3194 */
3195 static void
3196f_executable(typval_T *argvars, typval_T *rettv)
3197{
3198 char_u *name = get_tv_string(&argvars[0]);
3199
3200 /* Check in $PATH and also check directly if there is a directory name. */
3201 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
3202 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
3203}
3204
3205static garray_T redir_execute_ga;
3206
3207/*
3208 * Append "value[value_len]" to the execute() output.
3209 */
3210 void
3211execute_redir_str(char_u *value, int value_len)
3212{
3213 int len;
3214
3215 if (value_len == -1)
3216 len = (int)STRLEN(value); /* Append the entire string */
3217 else
3218 len = value_len; /* Append only "value_len" characters */
3219 if (ga_grow(&redir_execute_ga, len) == OK)
3220 {
3221 mch_memmove((char *)redir_execute_ga.ga_data
3222 + redir_execute_ga.ga_len, value, len);
3223 redir_execute_ga.ga_len += len;
3224 }
3225}
3226
3227/*
3228 * Get next line from a list.
3229 * Called by do_cmdline() to get the next line.
3230 * Returns allocated string, or NULL for end of function.
3231 */
3232
3233 static char_u *
3234get_list_line(
3235 int c UNUSED,
3236 void *cookie,
3237 int indent UNUSED)
3238{
3239 listitem_T **p = (listitem_T **)cookie;
3240 listitem_T *item = *p;
3241 char_u buf[NUMBUFLEN];
3242 char_u *s;
3243
3244 if (item == NULL)
3245 return NULL;
3246 s = get_tv_string_buf_chk(&item->li_tv, buf);
3247 *p = item->li_next;
3248 return s == NULL ? NULL : vim_strsave(s);
3249}
3250
3251/*
3252 * "execute()" function
3253 */
3254 static void
3255f_execute(typval_T *argvars, typval_T *rettv)
3256{
3257 char_u *cmd = NULL;
3258 list_T *list = NULL;
3259 int save_msg_silent = msg_silent;
3260 int save_emsg_silent = emsg_silent;
3261 int save_emsg_noredir = emsg_noredir;
3262 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003263 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003264 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003265 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003266 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003267
3268 rettv->vval.v_string = NULL;
3269 rettv->v_type = VAR_STRING;
3270
3271 if (argvars[0].v_type == VAR_LIST)
3272 {
3273 list = argvars[0].vval.v_list;
3274 if (list == NULL || list->lv_first == NULL)
3275 /* empty list, no commands, empty output */
3276 return;
3277 ++list->lv_refcount;
3278 }
3279 else
3280 {
3281 cmd = get_tv_string_chk(&argvars[0]);
3282 if (cmd == NULL)
3283 return;
3284 }
3285
3286 if (argvars[1].v_type != VAR_UNKNOWN)
3287 {
3288 char_u buf[NUMBUFLEN];
3289 char_u *s = get_tv_string_buf_chk(&argvars[1], buf);
3290
3291 if (s == NULL)
3292 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003293 if (*s == NUL)
3294 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003295 if (STRNCMP(s, "silent", 6) == 0)
3296 ++msg_silent;
3297 if (STRCMP(s, "silent!") == 0)
3298 {
3299 emsg_silent = TRUE;
3300 emsg_noredir = TRUE;
3301 }
3302 }
3303 else
3304 ++msg_silent;
3305
3306 if (redir_execute)
3307 save_ga = redir_execute_ga;
3308 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3309 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003310 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003311 if (!echo_output)
3312 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003313
3314 if (cmd != NULL)
3315 do_cmdline_cmd(cmd);
3316 else
3317 {
3318 listitem_T *item = list->lv_first;
3319
3320 do_cmdline(NULL, get_list_line, (void *)&item,
3321 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3322 --list->lv_refcount;
3323 }
3324
Bram Moolenaard297f352017-01-29 20:31:21 +01003325 /* Need to append a NUL to the result. */
3326 if (ga_grow(&redir_execute_ga, 1) == OK)
3327 {
3328 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3329 rettv->vval.v_string = redir_execute_ga.ga_data;
3330 }
3331 else
3332 {
3333 ga_clear(&redir_execute_ga);
3334 rettv->vval.v_string = NULL;
3335 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003336 msg_silent = save_msg_silent;
3337 emsg_silent = save_emsg_silent;
3338 emsg_noredir = save_emsg_noredir;
3339
3340 redir_execute = save_redir_execute;
3341 if (redir_execute)
3342 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003343 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003344
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003345 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003346 if (echo_output)
3347 // When not working silently: put it in column zero. A following
3348 // "echon" will overwrite the message, unavoidably.
3349 msg_col = 0;
3350 else
3351 // When working silently: Put it back where it was, since nothing
3352 // should have been written.
3353 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003354}
3355
3356/*
3357 * "exepath()" function
3358 */
3359 static void
3360f_exepath(typval_T *argvars, typval_T *rettv)
3361{
3362 char_u *p = NULL;
3363
3364 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
3365 rettv->v_type = VAR_STRING;
3366 rettv->vval.v_string = p;
3367}
3368
3369/*
3370 * "exists()" function
3371 */
3372 static void
3373f_exists(typval_T *argvars, typval_T *rettv)
3374{
3375 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003376 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003377
3378 p = get_tv_string(&argvars[0]);
3379 if (*p == '$') /* environment variable */
3380 {
3381 /* first try "normal" environment variables (fast) */
3382 if (mch_getenv(p + 1) != NULL)
3383 n = TRUE;
3384 else
3385 {
3386 /* try expanding things like $VIM and ${HOME} */
3387 p = expand_env_save(p);
3388 if (p != NULL && *p != '$')
3389 n = TRUE;
3390 vim_free(p);
3391 }
3392 }
3393 else if (*p == '&' || *p == '+') /* option */
3394 {
3395 n = (get_option_tv(&p, NULL, TRUE) == OK);
3396 if (*skipwhite(p) != NUL)
3397 n = FALSE; /* trailing garbage */
3398 }
3399 else if (*p == '*') /* internal or user defined function */
3400 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003401 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003402 }
3403 else if (*p == ':')
3404 {
3405 n = cmd_exists(p + 1);
3406 }
3407 else if (*p == '#')
3408 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003409 if (p[1] == '#')
3410 n = autocmd_supported(p + 2);
3411 else
3412 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003413 }
3414 else /* internal variable */
3415 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003416 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003417 }
3418
3419 rettv->vval.v_number = n;
3420}
3421
3422#ifdef FEAT_FLOAT
3423/*
3424 * "exp()" function
3425 */
3426 static void
3427f_exp(typval_T *argvars, typval_T *rettv)
3428{
3429 float_T f = 0.0;
3430
3431 rettv->v_type = VAR_FLOAT;
3432 if (get_float_arg(argvars, &f) == OK)
3433 rettv->vval.v_float = exp(f);
3434 else
3435 rettv->vval.v_float = 0.0;
3436}
3437#endif
3438
3439/*
3440 * "expand()" function
3441 */
3442 static void
3443f_expand(typval_T *argvars, typval_T *rettv)
3444{
3445 char_u *s;
3446 int len;
3447 char_u *errormsg;
3448 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3449 expand_T xpc;
3450 int error = FALSE;
3451 char_u *result;
3452
3453 rettv->v_type = VAR_STRING;
3454 if (argvars[1].v_type != VAR_UNKNOWN
3455 && argvars[2].v_type != VAR_UNKNOWN
3456 && get_tv_number_chk(&argvars[2], &error)
3457 && !error)
3458 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003459 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003460 }
3461
3462 s = get_tv_string(&argvars[0]);
3463 if (*s == '%' || *s == '#' || *s == '<')
3464 {
3465 ++emsg_off;
3466 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3467 --emsg_off;
3468 if (rettv->v_type == VAR_LIST)
3469 {
3470 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3471 list_append_string(rettv->vval.v_list, result, -1);
3472 else
3473 vim_free(result);
3474 }
3475 else
3476 rettv->vval.v_string = result;
3477 }
3478 else
3479 {
3480 /* When the optional second argument is non-zero, don't remove matches
3481 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3482 if (argvars[1].v_type != VAR_UNKNOWN
3483 && get_tv_number_chk(&argvars[1], &error))
3484 options |= WILD_KEEP_ALL;
3485 if (!error)
3486 {
3487 ExpandInit(&xpc);
3488 xpc.xp_context = EXPAND_FILES;
3489 if (p_wic)
3490 options += WILD_ICASE;
3491 if (rettv->v_type == VAR_STRING)
3492 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3493 options, WILD_ALL);
3494 else if (rettv_list_alloc(rettv) != FAIL)
3495 {
3496 int i;
3497
3498 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3499 for (i = 0; i < xpc.xp_numfiles; i++)
3500 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3501 ExpandCleanup(&xpc);
3502 }
3503 }
3504 else
3505 rettv->vval.v_string = NULL;
3506 }
3507}
3508
3509/*
3510 * "extend(list, list [, idx])" function
3511 * "extend(dict, dict [, action])" function
3512 */
3513 static void
3514f_extend(typval_T *argvars, typval_T *rettv)
3515{
3516 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3517
3518 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3519 {
3520 list_T *l1, *l2;
3521 listitem_T *item;
3522 long before;
3523 int error = FALSE;
3524
3525 l1 = argvars[0].vval.v_list;
3526 l2 = argvars[1].vval.v_list;
3527 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
3528 && l2 != NULL)
3529 {
3530 if (argvars[2].v_type != VAR_UNKNOWN)
3531 {
3532 before = (long)get_tv_number_chk(&argvars[2], &error);
3533 if (error)
3534 return; /* type error; errmsg already given */
3535
3536 if (before == l1->lv_len)
3537 item = NULL;
3538 else
3539 {
3540 item = list_find(l1, before);
3541 if (item == NULL)
3542 {
3543 EMSGN(_(e_listidx), before);
3544 return;
3545 }
3546 }
3547 }
3548 else
3549 item = NULL;
3550 list_extend(l1, l2, item);
3551
3552 copy_tv(&argvars[0], rettv);
3553 }
3554 }
3555 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3556 {
3557 dict_T *d1, *d2;
3558 char_u *action;
3559 int i;
3560
3561 d1 = argvars[0].vval.v_dict;
3562 d2 = argvars[1].vval.v_dict;
3563 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
3564 && d2 != NULL)
3565 {
3566 /* Check the third argument. */
3567 if (argvars[2].v_type != VAR_UNKNOWN)
3568 {
3569 static char *(av[]) = {"keep", "force", "error"};
3570
3571 action = get_tv_string_chk(&argvars[2]);
3572 if (action == NULL)
3573 return; /* type error; errmsg already given */
3574 for (i = 0; i < 3; ++i)
3575 if (STRCMP(action, av[i]) == 0)
3576 break;
3577 if (i == 3)
3578 {
3579 EMSG2(_(e_invarg2), action);
3580 return;
3581 }
3582 }
3583 else
3584 action = (char_u *)"force";
3585
3586 dict_extend(d1, d2, action);
3587
3588 copy_tv(&argvars[0], rettv);
3589 }
3590 }
3591 else
3592 EMSG2(_(e_listdictarg), "extend()");
3593}
3594
3595/*
3596 * "feedkeys()" function
3597 */
3598 static void
3599f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3600{
3601 int remap = TRUE;
3602 int insert = FALSE;
3603 char_u *keys, *flags;
3604 char_u nbuf[NUMBUFLEN];
3605 int typed = FALSE;
3606 int execute = FALSE;
3607 int dangerous = FALSE;
3608 char_u *keys_esc;
3609
3610 /* This is not allowed in the sandbox. If the commands would still be
3611 * executed in the sandbox it would be OK, but it probably happens later,
3612 * when "sandbox" is no longer set. */
3613 if (check_secure())
3614 return;
3615
3616 keys = get_tv_string(&argvars[0]);
3617
3618 if (argvars[1].v_type != VAR_UNKNOWN)
3619 {
3620 flags = get_tv_string_buf(&argvars[1], nbuf);
3621 for ( ; *flags != NUL; ++flags)
3622 {
3623 switch (*flags)
3624 {
3625 case 'n': remap = FALSE; break;
3626 case 'm': remap = TRUE; break;
3627 case 't': typed = TRUE; break;
3628 case 'i': insert = TRUE; break;
3629 case 'x': execute = TRUE; break;
3630 case '!': dangerous = TRUE; break;
3631 }
3632 }
3633 }
3634
3635 if (*keys != NUL || execute)
3636 {
3637 /* Need to escape K_SPECIAL and CSI before putting the string in the
3638 * typeahead buffer. */
3639 keys_esc = vim_strsave_escape_csi(keys);
3640 if (keys_esc != NULL)
3641 {
3642 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
3643 insert ? 0 : typebuf.tb_len, !typed, FALSE);
3644 vim_free(keys_esc);
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003645 if (vgetc_busy
3646#ifdef FEAT_TIMERS
3647 || timer_busy
3648#endif
3649 )
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003650 typebuf_was_filled = TRUE;
3651 if (execute)
3652 {
3653 int save_msg_scroll = msg_scroll;
3654
3655 /* Avoid a 1 second delay when the keys start Insert mode. */
3656 msg_scroll = FALSE;
3657
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003658 if (!dangerous)
3659 ++ex_normal_busy;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02003660 exec_normal(TRUE, FALSE, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003661 if (!dangerous)
3662 --ex_normal_busy;
3663
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003664 msg_scroll |= save_msg_scroll;
3665 }
3666 }
3667 }
3668}
3669
3670/*
3671 * "filereadable()" function
3672 */
3673 static void
3674f_filereadable(typval_T *argvars, typval_T *rettv)
3675{
3676 int fd;
3677 char_u *p;
3678 int n;
3679
3680#ifndef O_NONBLOCK
3681# define O_NONBLOCK 0
3682#endif
3683 p = get_tv_string(&argvars[0]);
3684 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3685 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3686 {
3687 n = TRUE;
3688 close(fd);
3689 }
3690 else
3691 n = FALSE;
3692
3693 rettv->vval.v_number = n;
3694}
3695
3696/*
3697 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3698 * rights to write into.
3699 */
3700 static void
3701f_filewritable(typval_T *argvars, typval_T *rettv)
3702{
3703 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
3704}
3705
3706 static void
3707findfilendir(
3708 typval_T *argvars UNUSED,
3709 typval_T *rettv,
3710 int find_what UNUSED)
3711{
3712#ifdef FEAT_SEARCHPATH
3713 char_u *fname;
3714 char_u *fresult = NULL;
3715 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3716 char_u *p;
3717 char_u pathbuf[NUMBUFLEN];
3718 int count = 1;
3719 int first = TRUE;
3720 int error = FALSE;
3721#endif
3722
3723 rettv->vval.v_string = NULL;
3724 rettv->v_type = VAR_STRING;
3725
3726#ifdef FEAT_SEARCHPATH
3727 fname = get_tv_string(&argvars[0]);
3728
3729 if (argvars[1].v_type != VAR_UNKNOWN)
3730 {
3731 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
3732 if (p == NULL)
3733 error = TRUE;
3734 else
3735 {
3736 if (*p != NUL)
3737 path = p;
3738
3739 if (argvars[2].v_type != VAR_UNKNOWN)
3740 count = (int)get_tv_number_chk(&argvars[2], &error);
3741 }
3742 }
3743
3744 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3745 error = TRUE;
3746
3747 if (*fname != NUL && !error)
3748 {
3749 do
3750 {
3751 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3752 vim_free(fresult);
3753 fresult = find_file_in_path_option(first ? fname : NULL,
3754 first ? (int)STRLEN(fname) : 0,
3755 0, first, path,
3756 find_what,
3757 curbuf->b_ffname,
3758 find_what == FINDFILE_DIR
3759 ? (char_u *)"" : curbuf->b_p_sua);
3760 first = FALSE;
3761
3762 if (fresult != NULL && rettv->v_type == VAR_LIST)
3763 list_append_string(rettv->vval.v_list, fresult, -1);
3764
3765 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3766 }
3767
3768 if (rettv->v_type == VAR_STRING)
3769 rettv->vval.v_string = fresult;
3770#endif
3771}
3772
3773/*
3774 * "filter()" function
3775 */
3776 static void
3777f_filter(typval_T *argvars, typval_T *rettv)
3778{
3779 filter_map(argvars, rettv, FALSE);
3780}
3781
3782/*
3783 * "finddir({fname}[, {path}[, {count}]])" function
3784 */
3785 static void
3786f_finddir(typval_T *argvars, typval_T *rettv)
3787{
3788 findfilendir(argvars, rettv, FINDFILE_DIR);
3789}
3790
3791/*
3792 * "findfile({fname}[, {path}[, {count}]])" function
3793 */
3794 static void
3795f_findfile(typval_T *argvars, typval_T *rettv)
3796{
3797 findfilendir(argvars, rettv, FINDFILE_FILE);
3798}
3799
3800#ifdef FEAT_FLOAT
3801/*
3802 * "float2nr({float})" function
3803 */
3804 static void
3805f_float2nr(typval_T *argvars, typval_T *rettv)
3806{
3807 float_T f = 0.0;
3808
3809 if (get_float_arg(argvars, &f) == OK)
3810 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003811 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003812 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003813 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003814 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003815 else
3816 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003817 }
3818}
3819
3820/*
3821 * "floor({float})" function
3822 */
3823 static void
3824f_floor(typval_T *argvars, typval_T *rettv)
3825{
3826 float_T f = 0.0;
3827
3828 rettv->v_type = VAR_FLOAT;
3829 if (get_float_arg(argvars, &f) == OK)
3830 rettv->vval.v_float = floor(f);
3831 else
3832 rettv->vval.v_float = 0.0;
3833}
3834
3835/*
3836 * "fmod()" function
3837 */
3838 static void
3839f_fmod(typval_T *argvars, typval_T *rettv)
3840{
3841 float_T fx = 0.0, fy = 0.0;
3842
3843 rettv->v_type = VAR_FLOAT;
3844 if (get_float_arg(argvars, &fx) == OK
3845 && get_float_arg(&argvars[1], &fy) == OK)
3846 rettv->vval.v_float = fmod(fx, fy);
3847 else
3848 rettv->vval.v_float = 0.0;
3849}
3850#endif
3851
3852/*
3853 * "fnameescape({string})" function
3854 */
3855 static void
3856f_fnameescape(typval_T *argvars, typval_T *rettv)
3857{
3858 rettv->vval.v_string = vim_strsave_fnameescape(
3859 get_tv_string(&argvars[0]), FALSE);
3860 rettv->v_type = VAR_STRING;
3861}
3862
3863/*
3864 * "fnamemodify({fname}, {mods})" function
3865 */
3866 static void
3867f_fnamemodify(typval_T *argvars, typval_T *rettv)
3868{
3869 char_u *fname;
3870 char_u *mods;
3871 int usedlen = 0;
3872 int len;
3873 char_u *fbuf = NULL;
3874 char_u buf[NUMBUFLEN];
3875
3876 fname = get_tv_string_chk(&argvars[0]);
3877 mods = get_tv_string_buf_chk(&argvars[1], buf);
3878 if (fname == NULL || mods == NULL)
3879 fname = NULL;
3880 else
3881 {
3882 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02003883 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003884 }
3885
3886 rettv->v_type = VAR_STRING;
3887 if (fname == NULL)
3888 rettv->vval.v_string = NULL;
3889 else
3890 rettv->vval.v_string = vim_strnsave(fname, len);
3891 vim_free(fbuf);
3892}
3893
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003894/*
3895 * "foldclosed()" function
3896 */
3897 static void
3898foldclosed_both(
3899 typval_T *argvars UNUSED,
3900 typval_T *rettv,
3901 int end UNUSED)
3902{
3903#ifdef FEAT_FOLDING
3904 linenr_T lnum;
3905 linenr_T first, last;
3906
3907 lnum = get_tv_lnum(argvars);
3908 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3909 {
3910 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3911 {
3912 if (end)
3913 rettv->vval.v_number = (varnumber_T)last;
3914 else
3915 rettv->vval.v_number = (varnumber_T)first;
3916 return;
3917 }
3918 }
3919#endif
3920 rettv->vval.v_number = -1;
3921}
3922
3923/*
3924 * "foldclosed()" function
3925 */
3926 static void
3927f_foldclosed(typval_T *argvars, typval_T *rettv)
3928{
3929 foldclosed_both(argvars, rettv, FALSE);
3930}
3931
3932/*
3933 * "foldclosedend()" function
3934 */
3935 static void
3936f_foldclosedend(typval_T *argvars, typval_T *rettv)
3937{
3938 foldclosed_both(argvars, rettv, TRUE);
3939}
3940
3941/*
3942 * "foldlevel()" function
3943 */
3944 static void
3945f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3946{
3947#ifdef FEAT_FOLDING
3948 linenr_T lnum;
3949
3950 lnum = get_tv_lnum(argvars);
3951 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3952 rettv->vval.v_number = foldLevel(lnum);
3953#endif
3954}
3955
3956/*
3957 * "foldtext()" function
3958 */
3959 static void
3960f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
3961{
3962#ifdef FEAT_FOLDING
3963 linenr_T foldstart;
3964 linenr_T foldend;
3965 char_u *dashes;
3966 linenr_T lnum;
3967 char_u *s;
3968 char_u *r;
3969 int len;
3970 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003971 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003972#endif
3973
3974 rettv->v_type = VAR_STRING;
3975 rettv->vval.v_string = NULL;
3976#ifdef FEAT_FOLDING
3977 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
3978 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
3979 dashes = get_vim_var_str(VV_FOLDDASHES);
3980 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
3981 && dashes != NULL)
3982 {
3983 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02003984 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003985 if (!linewhite(lnum))
3986 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003987
3988 /* Find interesting text in this line. */
3989 s = skipwhite(ml_get(lnum));
3990 /* skip C comment-start */
3991 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
3992 {
3993 s = skipwhite(s + 2);
3994 if (*skipwhite(s) == NUL
3995 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
3996 {
3997 s = skipwhite(ml_get(lnum + 1));
3998 if (*s == '*')
3999 s = skipwhite(s + 1);
4000 }
4001 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02004002 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01004003 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004004 r = alloc((unsigned)(STRLEN(txt)
4005 + STRLEN(dashes) /* for %s */
4006 + 20 /* for %3ld */
4007 + STRLEN(s))); /* concatenated */
4008 if (r != NULL)
4009 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02004010 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004011 len = (int)STRLEN(r);
4012 STRCAT(r, s);
4013 /* remove 'foldmarker' and 'commentstring' */
4014 foldtext_cleanup(r + len);
4015 rettv->vval.v_string = r;
4016 }
4017 }
4018#endif
4019}
4020
4021/*
4022 * "foldtextresult(lnum)" function
4023 */
4024 static void
4025f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
4026{
4027#ifdef FEAT_FOLDING
4028 linenr_T lnum;
4029 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004030 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004031 foldinfo_T foldinfo;
4032 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004033 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004034#endif
4035
4036 rettv->v_type = VAR_STRING;
4037 rettv->vval.v_string = NULL;
4038#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004039 if (entered)
4040 return; /* reject recursive use */
4041 entered = TRUE;
4042
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004043 lnum = get_tv_lnum(argvars);
4044 /* treat illegal types and illegal string values for {lnum} the same */
4045 if (lnum < 0)
4046 lnum = 0;
4047 fold_count = foldedCount(curwin, lnum, &foldinfo);
4048 if (fold_count > 0)
4049 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01004050 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
4051 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004052 if (text == buf)
4053 text = vim_strsave(text);
4054 rettv->vval.v_string = text;
4055 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004056
4057 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004058#endif
4059}
4060
4061/*
4062 * "foreground()" function
4063 */
4064 static void
4065f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4066{
4067#ifdef FEAT_GUI
4068 if (gui.in_use)
4069 gui_mch_set_foreground();
4070#else
4071# ifdef WIN32
4072 win32_set_foreground();
4073# endif
4074#endif
4075}
4076
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004077 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004078common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004079{
4080 char_u *s;
4081 char_u *name;
4082 int use_string = FALSE;
4083 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004084 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004085
4086 if (argvars[0].v_type == VAR_FUNC)
4087 {
4088 /* function(MyFunc, [arg], dict) */
4089 s = argvars[0].vval.v_string;
4090 }
4091 else if (argvars[0].v_type == VAR_PARTIAL
4092 && argvars[0].vval.v_partial != NULL)
4093 {
4094 /* function(dict.MyFunc, [arg]) */
4095 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004096 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004097 }
4098 else
4099 {
4100 /* function('MyFunc', [arg], dict) */
4101 s = get_tv_string(&argvars[0]);
4102 use_string = TRUE;
4103 }
4104
Bram Moolenaar843b8842016-08-21 14:36:15 +02004105 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004106 {
4107 name = s;
4108 trans_name = trans_function_name(&name, FALSE,
4109 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4110 if (*name != NUL)
4111 s = NULL;
4112 }
4113
Bram Moolenaar843b8842016-08-21 14:36:15 +02004114 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4115 || (is_funcref && trans_name == NULL))
Bram Moolenaar5582ef12016-09-14 22:16:13 +02004116 EMSG2(_(e_invarg2), use_string ? get_tv_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004117 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004118 else if (trans_name != NULL && (is_funcref
4119 ? find_func(trans_name) == NULL
4120 : !translated_function_exists(trans_name)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004121 EMSG2(_("E700: Unknown function: %s"), s);
4122 else
4123 {
4124 int dict_idx = 0;
4125 int arg_idx = 0;
4126 list_T *list = NULL;
4127
4128 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4129 {
4130 char sid_buf[25];
4131 int off = *s == 's' ? 2 : 5;
4132
4133 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4134 * also be called from another script. Using trans_function_name()
4135 * would also work, but some plugins depend on the name being
4136 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004137 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004138 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
4139 if (name != NULL)
4140 {
4141 STRCPY(name, sid_buf);
4142 STRCAT(name, s + off);
4143 }
4144 }
4145 else
4146 name = vim_strsave(s);
4147
4148 if (argvars[1].v_type != VAR_UNKNOWN)
4149 {
4150 if (argvars[2].v_type != VAR_UNKNOWN)
4151 {
4152 /* function(name, [args], dict) */
4153 arg_idx = 1;
4154 dict_idx = 2;
4155 }
4156 else if (argvars[1].v_type == VAR_DICT)
4157 /* function(name, dict) */
4158 dict_idx = 1;
4159 else
4160 /* function(name, [args]) */
4161 arg_idx = 1;
4162 if (dict_idx > 0)
4163 {
4164 if (argvars[dict_idx].v_type != VAR_DICT)
4165 {
4166 EMSG(_("E922: expected a dict"));
4167 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004168 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004169 }
4170 if (argvars[dict_idx].vval.v_dict == NULL)
4171 dict_idx = 0;
4172 }
4173 if (arg_idx > 0)
4174 {
4175 if (argvars[arg_idx].v_type != VAR_LIST)
4176 {
4177 EMSG(_("E923: Second argument of function() must be a list or a dict"));
4178 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004179 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004180 }
4181 list = argvars[arg_idx].vval.v_list;
4182 if (list == NULL || list->lv_len == 0)
4183 arg_idx = 0;
4184 }
4185 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004186 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004187 {
4188 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
4189
4190 /* result is a VAR_PARTIAL */
4191 if (pt == NULL)
4192 vim_free(name);
4193 else
4194 {
4195 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4196 {
4197 listitem_T *li;
4198 int i = 0;
4199 int arg_len = 0;
4200 int lv_len = 0;
4201
4202 if (arg_pt != NULL)
4203 arg_len = arg_pt->pt_argc;
4204 if (list != NULL)
4205 lv_len = list->lv_len;
4206 pt->pt_argc = arg_len + lv_len;
4207 pt->pt_argv = (typval_T *)alloc(
4208 sizeof(typval_T) * pt->pt_argc);
4209 if (pt->pt_argv == NULL)
4210 {
4211 vim_free(pt);
4212 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004213 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004214 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004215 for (i = 0; i < arg_len; i++)
4216 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4217 if (lv_len > 0)
4218 for (li = list->lv_first; li != NULL;
4219 li = li->li_next)
4220 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004221 }
4222
4223 /* For "function(dict.func, [], dict)" and "func" is a partial
4224 * use "dict". That is backwards compatible. */
4225 if (dict_idx > 0)
4226 {
4227 /* The dict is bound explicitly, pt_auto is FALSE. */
4228 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4229 ++pt->pt_dict->dv_refcount;
4230 }
4231 else if (arg_pt != NULL)
4232 {
4233 /* If the dict was bound automatically the result is also
4234 * bound automatically. */
4235 pt->pt_dict = arg_pt->pt_dict;
4236 pt->pt_auto = arg_pt->pt_auto;
4237 if (pt->pt_dict != NULL)
4238 ++pt->pt_dict->dv_refcount;
4239 }
4240
4241 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004242 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4243 {
4244 pt->pt_func = arg_pt->pt_func;
4245 func_ptr_ref(pt->pt_func);
4246 vim_free(name);
4247 }
4248 else if (is_funcref)
4249 {
4250 pt->pt_func = find_func(trans_name);
4251 func_ptr_ref(pt->pt_func);
4252 vim_free(name);
4253 }
4254 else
4255 {
4256 pt->pt_name = name;
4257 func_ref(name);
4258 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004259 }
4260 rettv->v_type = VAR_PARTIAL;
4261 rettv->vval.v_partial = pt;
4262 }
4263 else
4264 {
4265 /* result is a VAR_FUNC */
4266 rettv->v_type = VAR_FUNC;
4267 rettv->vval.v_string = name;
4268 func_ref(name);
4269 }
4270 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004271theend:
4272 vim_free(trans_name);
4273}
4274
4275/*
4276 * "funcref()" function
4277 */
4278 static void
4279f_funcref(typval_T *argvars, typval_T *rettv)
4280{
4281 common_function(argvars, rettv, TRUE);
4282}
4283
4284/*
4285 * "function()" function
4286 */
4287 static void
4288f_function(typval_T *argvars, typval_T *rettv)
4289{
4290 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004291}
4292
4293/*
4294 * "garbagecollect()" function
4295 */
4296 static void
4297f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4298{
4299 /* This is postponed until we are back at the toplevel, because we may be
4300 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4301 want_garbage_collect = TRUE;
4302
4303 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
4304 garbage_collect_at_exit = TRUE;
4305}
4306
4307/*
4308 * "get()" function
4309 */
4310 static void
4311f_get(typval_T *argvars, typval_T *rettv)
4312{
4313 listitem_T *li;
4314 list_T *l;
4315 dictitem_T *di;
4316 dict_T *d;
4317 typval_T *tv = NULL;
4318
4319 if (argvars[0].v_type == VAR_LIST)
4320 {
4321 if ((l = argvars[0].vval.v_list) != NULL)
4322 {
4323 int error = FALSE;
4324
4325 li = list_find(l, (long)get_tv_number_chk(&argvars[1], &error));
4326 if (!error && li != NULL)
4327 tv = &li->li_tv;
4328 }
4329 }
4330 else if (argvars[0].v_type == VAR_DICT)
4331 {
4332 if ((d = argvars[0].vval.v_dict) != NULL)
4333 {
4334 di = dict_find(d, get_tv_string(&argvars[1]), -1);
4335 if (di != NULL)
4336 tv = &di->di_tv;
4337 }
4338 }
4339 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4340 {
4341 partial_T *pt;
4342 partial_T fref_pt;
4343
4344 if (argvars[0].v_type == VAR_PARTIAL)
4345 pt = argvars[0].vval.v_partial;
4346 else
4347 {
4348 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4349 fref_pt.pt_name = argvars[0].vval.v_string;
4350 pt = &fref_pt;
4351 }
4352
4353 if (pt != NULL)
4354 {
4355 char_u *what = get_tv_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004356 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004357
4358 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4359 {
4360 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004361 n = partial_name(pt);
4362 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004363 rettv->vval.v_string = NULL;
4364 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004365 {
4366 rettv->vval.v_string = vim_strsave(n);
4367 if (rettv->v_type == VAR_FUNC)
4368 func_ref(rettv->vval.v_string);
4369 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004370 }
4371 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004372 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004373 else if (STRCMP(what, "args") == 0)
4374 {
4375 rettv->v_type = VAR_LIST;
4376 if (rettv_list_alloc(rettv) == OK)
4377 {
4378 int i;
4379
4380 for (i = 0; i < pt->pt_argc; ++i)
4381 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4382 }
4383 }
4384 else
4385 EMSG2(_(e_invarg2), what);
4386 return;
4387 }
4388 }
4389 else
4390 EMSG2(_(e_listdictarg), "get()");
4391
4392 if (tv == NULL)
4393 {
4394 if (argvars[2].v_type != VAR_UNKNOWN)
4395 copy_tv(&argvars[2], rettv);
4396 }
4397 else
4398 copy_tv(tv, rettv);
4399}
4400
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004401#ifdef FEAT_SIGNS
4402/*
4403 * Returns information about signs placed in a buffer as list of dicts.
4404 */
4405 static void
4406get_buffer_signs(buf_T *buf, list_T *l)
4407{
4408 signlist_T *sign;
4409
4410 for (sign = buf->b_signlist; sign; sign = sign->next)
4411 {
4412 dict_T *d = dict_alloc();
4413
4414 if (d != NULL)
4415 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02004416 dict_add_number(d, "id", sign->id);
4417 dict_add_number(d, "lnum", sign->lnum);
4418 dict_add_string(d, "name", sign_typenr2name(sign->typenr));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004419
4420 list_append_dict(l, d);
4421 }
4422 }
4423}
4424#endif
4425
4426/*
4427 * Returns buffer options, variables and other attributes in a dictionary.
4428 */
4429 static dict_T *
4430get_buffer_info(buf_T *buf)
4431{
4432 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004433 tabpage_T *tp;
4434 win_T *wp;
4435 list_T *windows;
4436
4437 dict = dict_alloc();
4438 if (dict == NULL)
4439 return NULL;
4440
Bram Moolenaare0be1672018-07-08 16:50:37 +02004441 dict_add_number(dict, "bufnr", buf->b_fnum);
4442 dict_add_string(dict, "name", buf->b_ffname);
4443 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4444 : buflist_findlnum(buf));
4445 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4446 dict_add_number(dict, "listed", buf->b_p_bl);
4447 dict_add_number(dict, "changed", bufIsChanged(buf));
4448 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4449 dict_add_number(dict, "hidden",
4450 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004451
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004452 /* Get a reference to buffer variables */
4453 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004454
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004455 /* List of windows displaying this buffer */
4456 windows = list_alloc();
4457 if (windows != NULL)
4458 {
4459 FOR_ALL_TAB_WINDOWS(tp, wp)
4460 if (wp->w_buffer == buf)
4461 list_append_number(windows, (varnumber_T)wp->w_id);
4462 dict_add_list(dict, "windows", windows);
4463 }
4464
4465#ifdef FEAT_SIGNS
4466 if (buf->b_signlist != NULL)
4467 {
4468 /* List of signs placed in this buffer */
4469 list_T *signs = list_alloc();
4470 if (signs != NULL)
4471 {
4472 get_buffer_signs(buf, signs);
4473 dict_add_list(dict, "signs", signs);
4474 }
4475 }
4476#endif
4477
4478 return dict;
4479}
4480
4481/*
4482 * "getbufinfo()" function
4483 */
4484 static void
4485f_getbufinfo(typval_T *argvars, typval_T *rettv)
4486{
4487 buf_T *buf = NULL;
4488 buf_T *argbuf = NULL;
4489 dict_T *d;
4490 int filtered = FALSE;
4491 int sel_buflisted = FALSE;
4492 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004493 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004494
4495 if (rettv_list_alloc(rettv) != OK)
4496 return;
4497
4498 /* List of all the buffers or selected buffers */
4499 if (argvars[0].v_type == VAR_DICT)
4500 {
4501 dict_T *sel_d = argvars[0].vval.v_dict;
4502
4503 if (sel_d != NULL)
4504 {
4505 dictitem_T *di;
4506
4507 filtered = TRUE;
4508
4509 di = dict_find(sel_d, (char_u *)"buflisted", -1);
4510 if (di != NULL && get_tv_number(&di->di_tv))
4511 sel_buflisted = TRUE;
4512
4513 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
4514 if (di != NULL && get_tv_number(&di->di_tv))
4515 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004516
4517 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
4518 if (di != NULL && get_tv_number(&di->di_tv))
4519 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004520 }
4521 }
4522 else if (argvars[0].v_type != VAR_UNKNOWN)
4523 {
4524 /* Information about one buffer. Argument specifies the buffer */
4525 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4526 ++emsg_off;
4527 argbuf = get_buf_tv(&argvars[0], FALSE);
4528 --emsg_off;
4529 if (argbuf == NULL)
4530 return;
4531 }
4532
4533 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004534 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004535 {
4536 if (argbuf != NULL && argbuf != buf)
4537 continue;
4538 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004539 || (sel_buflisted && !buf->b_p_bl)
4540 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004541 continue;
4542
4543 d = get_buffer_info(buf);
4544 if (d != NULL)
4545 list_append_dict(rettv->vval.v_list, d);
4546 if (argbuf != NULL)
4547 return;
4548 }
4549}
4550
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004551/*
4552 * Get line or list of lines from buffer "buf" into "rettv".
4553 * Return a range (from start to end) of lines in rettv from the specified
4554 * buffer.
4555 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4556 */
4557 static void
4558get_buffer_lines(
4559 buf_T *buf,
4560 linenr_T start,
4561 linenr_T end,
4562 int retlist,
4563 typval_T *rettv)
4564{
4565 char_u *p;
4566
4567 rettv->v_type = VAR_STRING;
4568 rettv->vval.v_string = NULL;
4569 if (retlist && rettv_list_alloc(rettv) == FAIL)
4570 return;
4571
4572 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4573 return;
4574
4575 if (!retlist)
4576 {
4577 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4578 p = ml_get_buf(buf, start, FALSE);
4579 else
4580 p = (char_u *)"";
4581 rettv->vval.v_string = vim_strsave(p);
4582 }
4583 else
4584 {
4585 if (end < start)
4586 return;
4587
4588 if (start < 1)
4589 start = 1;
4590 if (end > buf->b_ml.ml_line_count)
4591 end = buf->b_ml.ml_line_count;
4592 while (start <= end)
4593 if (list_append_string(rettv->vval.v_list,
4594 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4595 break;
4596 }
4597}
4598
4599/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004600 * "getbufline()" function
4601 */
4602 static void
4603f_getbufline(typval_T *argvars, typval_T *rettv)
4604{
4605 linenr_T lnum;
4606 linenr_T end;
4607 buf_T *buf;
4608
4609 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4610 ++emsg_off;
4611 buf = get_buf_tv(&argvars[0], FALSE);
4612 --emsg_off;
4613
4614 lnum = get_tv_lnum_buf(&argvars[1], buf);
4615 if (argvars[2].v_type == VAR_UNKNOWN)
4616 end = lnum;
4617 else
4618 end = get_tv_lnum_buf(&argvars[2], buf);
4619
4620 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4621}
4622
4623/*
4624 * "getbufvar()" function
4625 */
4626 static void
4627f_getbufvar(typval_T *argvars, typval_T *rettv)
4628{
4629 buf_T *buf;
4630 buf_T *save_curbuf;
4631 char_u *varname;
4632 dictitem_T *v;
4633 int done = FALSE;
4634
4635 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4636 varname = get_tv_string_chk(&argvars[1]);
4637 ++emsg_off;
4638 buf = get_buf_tv(&argvars[0], FALSE);
4639
4640 rettv->v_type = VAR_STRING;
4641 rettv->vval.v_string = NULL;
4642
4643 if (buf != NULL && varname != NULL)
4644 {
4645 /* set curbuf to be our buf, temporarily */
4646 save_curbuf = curbuf;
4647 curbuf = buf;
4648
Bram Moolenaar30567352016-08-27 21:25:44 +02004649 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004650 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004651 if (varname[1] == NUL)
4652 {
4653 /* get all buffer-local options in a dict */
4654 dict_T *opts = get_winbuf_options(TRUE);
4655
4656 if (opts != NULL)
4657 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004658 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004659 done = TRUE;
4660 }
4661 }
4662 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4663 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004664 done = TRUE;
4665 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004666 else
4667 {
4668 /* Look up the variable. */
4669 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4670 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4671 'b', varname, FALSE);
4672 if (v != NULL)
4673 {
4674 copy_tv(&v->di_tv, rettv);
4675 done = TRUE;
4676 }
4677 }
4678
4679 /* restore previous notion of curbuf */
4680 curbuf = save_curbuf;
4681 }
4682
4683 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4684 /* use the default value */
4685 copy_tv(&argvars[2], rettv);
4686
4687 --emsg_off;
4688}
4689
4690/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004691 * "getchangelist()" function
4692 */
4693 static void
4694f_getchangelist(typval_T *argvars, typval_T *rettv)
4695{
4696#ifdef FEAT_JUMPLIST
4697 buf_T *buf;
4698 int i;
4699 list_T *l;
4700 dict_T *d;
4701#endif
4702
4703 if (rettv_list_alloc(rettv) != OK)
4704 return;
4705
4706#ifdef FEAT_JUMPLIST
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004707 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4708 ++emsg_off;
4709 buf = get_buf_tv(&argvars[0], FALSE);
4710 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004711 if (buf == NULL)
4712 return;
4713
4714 l = list_alloc();
4715 if (l == NULL)
4716 return;
4717
4718 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4719 return;
4720 /*
4721 * The current window change list index tracks only the position in the
4722 * current buffer change list. For other buffers, use the change list
4723 * length as the current index.
4724 */
4725 list_append_number(rettv->vval.v_list,
4726 (varnumber_T)((buf == curwin->w_buffer)
4727 ? curwin->w_changelistidx : buf->b_changelistlen));
4728
4729 for (i = 0; i < buf->b_changelistlen; ++i)
4730 {
4731 if (buf->b_changelist[i].lnum == 0)
4732 continue;
4733 if ((d = dict_alloc()) == NULL)
4734 return;
4735 if (list_append_dict(l, d) == FAIL)
4736 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004737 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4738 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004739# ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +02004740 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004741# endif
4742 }
4743#endif
4744}
4745/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004746 * "getchar()" function
4747 */
4748 static void
4749f_getchar(typval_T *argvars, typval_T *rettv)
4750{
4751 varnumber_T n;
4752 int error = FALSE;
4753
Bram Moolenaar84d93902018-09-11 20:10:20 +02004754#ifdef MESSAGE_QUEUE
4755 // vpeekc() used to check for messages, but that caused problems, invoking
4756 // a callback where it was not expected. Some plugins use getchar(1) in a
4757 // loop to await a message, therefore make sure we check for messages here.
4758 parse_queued_messages();
4759#endif
4760
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004761 /* Position the cursor. Needed after a message that ends in a space. */
4762 windgoto(msg_row, msg_col);
4763
4764 ++no_mapping;
4765 ++allow_keys;
4766 for (;;)
4767 {
4768 if (argvars[0].v_type == VAR_UNKNOWN)
4769 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004770 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004771 else if (get_tv_number_chk(&argvars[0], &error) == 1)
4772 /* getchar(1): only check if char avail */
4773 n = vpeekc_any();
4774 else if (error || vpeekc_any() == NUL)
4775 /* illegal argument or getchar(0) and no char avail: return zero */
4776 n = 0;
4777 else
4778 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004779 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004780
4781 if (n == K_IGNORE)
4782 continue;
4783 break;
4784 }
4785 --no_mapping;
4786 --allow_keys;
4787
4788 set_vim_var_nr(VV_MOUSE_WIN, 0);
4789 set_vim_var_nr(VV_MOUSE_WINID, 0);
4790 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4791 set_vim_var_nr(VV_MOUSE_COL, 0);
4792
4793 rettv->vval.v_number = n;
4794 if (IS_SPECIAL(n) || mod_mask != 0)
4795 {
4796 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4797 int i = 0;
4798
4799 /* Turn a special key into three bytes, plus modifier. */
4800 if (mod_mask != 0)
4801 {
4802 temp[i++] = K_SPECIAL;
4803 temp[i++] = KS_MODIFIER;
4804 temp[i++] = mod_mask;
4805 }
4806 if (IS_SPECIAL(n))
4807 {
4808 temp[i++] = K_SPECIAL;
4809 temp[i++] = K_SECOND(n);
4810 temp[i++] = K_THIRD(n);
4811 }
4812#ifdef FEAT_MBYTE
4813 else if (has_mbyte)
4814 i += (*mb_char2bytes)(n, temp + i);
4815#endif
4816 else
4817 temp[i++] = n;
4818 temp[i++] = NUL;
4819 rettv->v_type = VAR_STRING;
4820 rettv->vval.v_string = vim_strsave(temp);
4821
4822#ifdef FEAT_MOUSE
4823 if (is_mouse_key(n))
4824 {
4825 int row = mouse_row;
4826 int col = mouse_col;
4827 win_T *win;
4828 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004829 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004830 int winnr = 1;
4831
4832 if (row >= 0 && col >= 0)
4833 {
4834 /* Find the window at the mouse coordinates and compute the
4835 * text position. */
4836 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004837 if (win == NULL)
4838 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004839 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004840 for (wp = firstwin; wp != win; wp = wp->w_next)
4841 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004842 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4843 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4844 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4845 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4846 }
4847 }
4848#endif
4849 }
4850}
4851
4852/*
4853 * "getcharmod()" function
4854 */
4855 static void
4856f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4857{
4858 rettv->vval.v_number = mod_mask;
4859}
4860
4861/*
4862 * "getcharsearch()" function
4863 */
4864 static void
4865f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4866{
4867 if (rettv_dict_alloc(rettv) != FAIL)
4868 {
4869 dict_T *dict = rettv->vval.v_dict;
4870
Bram Moolenaare0be1672018-07-08 16:50:37 +02004871 dict_add_string(dict, "char", last_csearch());
4872 dict_add_number(dict, "forward", last_csearch_forward());
4873 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004874 }
4875}
4876
4877/*
4878 * "getcmdline()" function
4879 */
4880 static void
4881f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4882{
4883 rettv->v_type = VAR_STRING;
4884 rettv->vval.v_string = get_cmdline_str();
4885}
4886
4887/*
4888 * "getcmdpos()" function
4889 */
4890 static void
4891f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4892{
4893 rettv->vval.v_number = get_cmdline_pos() + 1;
4894}
4895
4896/*
4897 * "getcmdtype()" function
4898 */
4899 static void
4900f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4901{
4902 rettv->v_type = VAR_STRING;
4903 rettv->vval.v_string = alloc(2);
4904 if (rettv->vval.v_string != NULL)
4905 {
4906 rettv->vval.v_string[0] = get_cmdline_type();
4907 rettv->vval.v_string[1] = NUL;
4908 }
4909}
4910
4911/*
4912 * "getcmdwintype()" function
4913 */
4914 static void
4915f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4916{
4917 rettv->v_type = VAR_STRING;
4918 rettv->vval.v_string = NULL;
4919#ifdef FEAT_CMDWIN
4920 rettv->vval.v_string = alloc(2);
4921 if (rettv->vval.v_string != NULL)
4922 {
4923 rettv->vval.v_string[0] = cmdwin_type;
4924 rettv->vval.v_string[1] = NUL;
4925 }
4926#endif
4927}
4928
4929#if defined(FEAT_CMDL_COMPL)
4930/*
4931 * "getcompletion()" function
4932 */
4933 static void
4934f_getcompletion(typval_T *argvars, typval_T *rettv)
4935{
4936 char_u *pat;
4937 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004938 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004939 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
4940 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004941
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004942 if (argvars[2].v_type != VAR_UNKNOWN)
4943 filtered = get_tv_number_chk(&argvars[2], NULL);
4944
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004945 if (p_wic)
4946 options |= WILD_ICASE;
4947
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004948 /* For filtered results, 'wildignore' is used */
4949 if (!filtered)
4950 options |= WILD_KEEP_ALL;
4951
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004952 ExpandInit(&xpc);
4953 xpc.xp_pattern = get_tv_string(&argvars[0]);
4954 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4955 xpc.xp_context = cmdcomplete_str_to_type(get_tv_string(&argvars[1]));
4956 if (xpc.xp_context == EXPAND_NOTHING)
4957 {
4958 if (argvars[1].v_type == VAR_STRING)
4959 EMSG2(_(e_invarg2), argvars[1].vval.v_string);
4960 else
4961 EMSG(_(e_invarg));
4962 return;
4963 }
4964
4965# if defined(FEAT_MENU)
4966 if (xpc.xp_context == EXPAND_MENUS)
4967 {
4968 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4969 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4970 }
4971# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02004972#ifdef FEAT_CSCOPE
4973 if (xpc.xp_context == EXPAND_CSCOPE)
4974 {
4975 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4976 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4977 }
4978#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02004979#ifdef FEAT_SIGNS
4980 if (xpc.xp_context == EXPAND_SIGN)
4981 {
4982 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4983 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4984 }
4985#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004986
4987 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4988 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
4989 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004990 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004991
4992 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4993
4994 for (i = 0; i < xpc.xp_numfiles; i++)
4995 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4996 }
4997 vim_free(pat);
4998 ExpandCleanup(&xpc);
4999}
5000#endif
5001
5002/*
5003 * "getcwd()" function
5004 */
5005 static void
5006f_getcwd(typval_T *argvars, typval_T *rettv)
5007{
5008 win_T *wp = NULL;
5009 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01005010 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005011
5012 rettv->v_type = VAR_STRING;
5013 rettv->vval.v_string = NULL;
5014
Bram Moolenaar54591292018-02-09 20:53:59 +01005015 if (argvars[0].v_type == VAR_NUMBER && argvars[0].vval.v_number == -1)
5016 global = TRUE;
5017 else
5018 wp = find_tabwin(&argvars[0], &argvars[1]);
5019
5020 if (wp != NULL && wp->w_localdir != NULL)
5021 rettv->vval.v_string = vim_strsave(wp->w_localdir);
5022 else if (wp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005023 {
Bram Moolenaar54591292018-02-09 20:53:59 +01005024 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005025 rettv->vval.v_string = vim_strsave(globaldir);
5026 else
5027 {
5028 cwd = alloc(MAXPATHL);
5029 if (cwd != NULL)
5030 {
5031 if (mch_dirname(cwd, MAXPATHL) != FAIL)
5032 rettv->vval.v_string = vim_strsave(cwd);
5033 vim_free(cwd);
5034 }
5035 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005036 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02005037#ifdef BACKSLASH_IN_FILENAME
5038 if (rettv->vval.v_string != NULL)
5039 slash_adjust(rettv->vval.v_string);
5040#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005041}
5042
5043/*
5044 * "getfontname()" function
5045 */
5046 static void
5047f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
5048{
5049 rettv->v_type = VAR_STRING;
5050 rettv->vval.v_string = NULL;
5051#ifdef FEAT_GUI
5052 if (gui.in_use)
5053 {
5054 GuiFont font;
5055 char_u *name = NULL;
5056
5057 if (argvars[0].v_type == VAR_UNKNOWN)
5058 {
5059 /* Get the "Normal" font. Either the name saved by
5060 * hl_set_font_name() or from the font ID. */
5061 font = gui.norm_font;
5062 name = hl_get_font_name();
5063 }
5064 else
5065 {
5066 name = get_tv_string(&argvars[0]);
5067 if (STRCMP(name, "*") == 0) /* don't use font dialog */
5068 return;
5069 font = gui_mch_get_font(name, FALSE);
5070 if (font == NOFONT)
5071 return; /* Invalid font name, return empty string. */
5072 }
5073 rettv->vval.v_string = gui_mch_get_fontname(font, name);
5074 if (argvars[0].v_type != VAR_UNKNOWN)
5075 gui_mch_free_font(font);
5076 }
5077#endif
5078}
5079
5080/*
5081 * "getfperm({fname})" function
5082 */
5083 static void
5084f_getfperm(typval_T *argvars, typval_T *rettv)
5085{
5086 char_u *fname;
5087 stat_T st;
5088 char_u *perm = NULL;
5089 char_u flags[] = "rwx";
5090 int i;
5091
5092 fname = get_tv_string(&argvars[0]);
5093
5094 rettv->v_type = VAR_STRING;
5095 if (mch_stat((char *)fname, &st) >= 0)
5096 {
5097 perm = vim_strsave((char_u *)"---------");
5098 if (perm != NULL)
5099 {
5100 for (i = 0; i < 9; i++)
5101 {
5102 if (st.st_mode & (1 << (8 - i)))
5103 perm[i] = flags[i % 3];
5104 }
5105 }
5106 }
5107 rettv->vval.v_string = perm;
5108}
5109
5110/*
5111 * "getfsize({fname})" function
5112 */
5113 static void
5114f_getfsize(typval_T *argvars, typval_T *rettv)
5115{
5116 char_u *fname;
5117 stat_T st;
5118
5119 fname = get_tv_string(&argvars[0]);
5120
5121 rettv->v_type = VAR_NUMBER;
5122
5123 if (mch_stat((char *)fname, &st) >= 0)
5124 {
5125 if (mch_isdir(fname))
5126 rettv->vval.v_number = 0;
5127 else
5128 {
5129 rettv->vval.v_number = (varnumber_T)st.st_size;
5130
5131 /* non-perfect check for overflow */
5132 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5133 rettv->vval.v_number = -2;
5134 }
5135 }
5136 else
5137 rettv->vval.v_number = -1;
5138}
5139
5140/*
5141 * "getftime({fname})" function
5142 */
5143 static void
5144f_getftime(typval_T *argvars, typval_T *rettv)
5145{
5146 char_u *fname;
5147 stat_T st;
5148
5149 fname = get_tv_string(&argvars[0]);
5150
5151 if (mch_stat((char *)fname, &st) >= 0)
5152 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5153 else
5154 rettv->vval.v_number = -1;
5155}
5156
5157/*
5158 * "getftype({fname})" function
5159 */
5160 static void
5161f_getftype(typval_T *argvars, typval_T *rettv)
5162{
5163 char_u *fname;
5164 stat_T st;
5165 char_u *type = NULL;
5166 char *t;
5167
5168 fname = get_tv_string(&argvars[0]);
5169
5170 rettv->v_type = VAR_STRING;
5171 if (mch_lstat((char *)fname, &st) >= 0)
5172 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005173 if (S_ISREG(st.st_mode))
5174 t = "file";
5175 else if (S_ISDIR(st.st_mode))
5176 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005177 else if (S_ISLNK(st.st_mode))
5178 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005179 else if (S_ISBLK(st.st_mode))
5180 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005181 else if (S_ISCHR(st.st_mode))
5182 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005183 else if (S_ISFIFO(st.st_mode))
5184 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005185 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005186 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005187 else
5188 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005189 type = vim_strsave((char_u *)t);
5190 }
5191 rettv->vval.v_string = type;
5192}
5193
5194/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005195 * "getjumplist()" function
5196 */
5197 static void
5198f_getjumplist(typval_T *argvars, typval_T *rettv)
5199{
5200#ifdef FEAT_JUMPLIST
5201 win_T *wp;
5202 int i;
5203 list_T *l;
5204 dict_T *d;
5205#endif
5206
5207 if (rettv_list_alloc(rettv) != OK)
5208 return;
5209
5210#ifdef FEAT_JUMPLIST
5211 wp = find_tabwin(&argvars[0], &argvars[1]);
5212 if (wp == NULL)
5213 return;
5214
5215 l = list_alloc();
5216 if (l == NULL)
5217 return;
5218
5219 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5220 return;
5221 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5222
Bram Moolenaar48679742018-02-13 13:33:29 +01005223 cleanup_jumplist(wp, TRUE);
5224
Bram Moolenaar4f505882018-02-10 21:06:32 +01005225 for (i = 0; i < wp->w_jumplistlen; ++i)
5226 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005227 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5228 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005229 if ((d = dict_alloc()) == NULL)
5230 return;
5231 if (list_append_dict(l, d) == FAIL)
5232 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005233 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5234 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005235# ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +02005236 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005237# endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005238 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005239 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005240 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005241 }
5242#endif
5243}
5244
5245/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005246 * "getline(lnum, [end])" function
5247 */
5248 static void
5249f_getline(typval_T *argvars, typval_T *rettv)
5250{
5251 linenr_T lnum;
5252 linenr_T end;
5253 int retlist;
5254
5255 lnum = get_tv_lnum(argvars);
5256 if (argvars[1].v_type == VAR_UNKNOWN)
5257 {
5258 end = 0;
5259 retlist = FALSE;
5260 }
5261 else
5262 {
5263 end = get_tv_lnum(&argvars[1]);
5264 retlist = TRUE;
5265 }
5266
5267 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5268}
5269
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005270#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005271 static void
5272get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5273{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005274 if (what_arg->v_type == VAR_UNKNOWN)
5275 {
5276 if (rettv_list_alloc(rettv) == OK)
5277 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005278 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005279 }
5280 else
5281 {
5282 if (rettv_dict_alloc(rettv) == OK)
5283 if (is_qf || (wp != NULL))
5284 {
5285 if (what_arg->v_type == VAR_DICT)
5286 {
5287 dict_T *d = what_arg->vval.v_dict;
5288
5289 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005290 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005291 }
5292 else
5293 EMSG(_(e_dictreq));
5294 }
5295 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005296}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005297#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005298
5299/*
5300 * "getloclist()" function
5301 */
5302 static void
5303f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5304{
5305#ifdef FEAT_QUICKFIX
5306 win_T *wp;
5307
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005308 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005309 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5310#endif
5311}
5312
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005313/*
5314 * "getmatches()" function
5315 */
5316 static void
5317f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5318{
5319#ifdef FEAT_SEARCH_EXTRA
5320 dict_T *dict;
5321 matchitem_T *cur = curwin->w_match_head;
5322 int i;
5323
5324 if (rettv_list_alloc(rettv) == OK)
5325 {
5326 while (cur != NULL)
5327 {
5328 dict = dict_alloc();
5329 if (dict == NULL)
5330 return;
5331 if (cur->match.regprog == NULL)
5332 {
5333 /* match added with matchaddpos() */
5334 for (i = 0; i < MAXPOSMATCH; ++i)
5335 {
5336 llpos_T *llpos;
5337 char buf[6];
5338 list_T *l;
5339
5340 llpos = &cur->pos.pos[i];
5341 if (llpos->lnum == 0)
5342 break;
5343 l = list_alloc();
5344 if (l == NULL)
5345 break;
5346 list_append_number(l, (varnumber_T)llpos->lnum);
5347 if (llpos->col > 0)
5348 {
5349 list_append_number(l, (varnumber_T)llpos->col);
5350 list_append_number(l, (varnumber_T)llpos->len);
5351 }
5352 sprintf(buf, "pos%d", i + 1);
5353 dict_add_list(dict, buf, l);
5354 }
5355 }
5356 else
5357 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02005358 dict_add_string(dict, "pattern", cur->pattern);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005359 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02005360 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
5361 dict_add_number(dict, "priority", (long)cur->priority);
5362 dict_add_number(dict, "id", (long)cur->id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005363# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
5364 if (cur->conceal_char)
5365 {
5366 char_u buf[MB_MAXBYTES + 1];
5367
5368 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005369 dict_add_string(dict, "conceal", (char_u *)&buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005370 }
5371# endif
5372 list_append_dict(rettv->vval.v_list, dict);
5373 cur = cur->next;
5374 }
5375 }
5376#endif
5377}
5378
5379/*
5380 * "getpid()" function
5381 */
5382 static void
5383f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5384{
5385 rettv->vval.v_number = mch_get_pid();
5386}
5387
5388 static void
5389getpos_both(
5390 typval_T *argvars,
5391 typval_T *rettv,
5392 int getcurpos)
5393{
5394 pos_T *fp;
5395 list_T *l;
5396 int fnum = -1;
5397
5398 if (rettv_list_alloc(rettv) == OK)
5399 {
5400 l = rettv->vval.v_list;
5401 if (getcurpos)
5402 fp = &curwin->w_cursor;
5403 else
5404 fp = var2fpos(&argvars[0], TRUE, &fnum);
5405 if (fnum != -1)
5406 list_append_number(l, (varnumber_T)fnum);
5407 else
5408 list_append_number(l, (varnumber_T)0);
5409 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5410 : (varnumber_T)0);
5411 list_append_number(l, (fp != NULL)
5412 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5413 : (varnumber_T)0);
5414 list_append_number(l,
5415#ifdef FEAT_VIRTUALEDIT
5416 (fp != NULL) ? (varnumber_T)fp->coladd :
5417#endif
5418 (varnumber_T)0);
5419 if (getcurpos)
5420 {
5421 update_curswant();
5422 list_append_number(l, curwin->w_curswant == MAXCOL ?
5423 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
5424 }
5425 }
5426 else
5427 rettv->vval.v_number = FALSE;
5428}
5429
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005430/*
5431 * "getcurpos()" function
5432 */
5433 static void
5434f_getcurpos(typval_T *argvars, typval_T *rettv)
5435{
5436 getpos_both(argvars, rettv, TRUE);
5437}
5438
5439/*
5440 * "getpos(string)" function
5441 */
5442 static void
5443f_getpos(typval_T *argvars, typval_T *rettv)
5444{
5445 getpos_both(argvars, rettv, FALSE);
5446}
5447
5448/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005449 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005450 */
5451 static void
5452f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5453{
5454#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005455 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005456#endif
5457}
5458
5459/*
5460 * "getreg()" function
5461 */
5462 static void
5463f_getreg(typval_T *argvars, typval_T *rettv)
5464{
5465 char_u *strregname;
5466 int regname;
5467 int arg2 = FALSE;
5468 int return_list = FALSE;
5469 int error = FALSE;
5470
5471 if (argvars[0].v_type != VAR_UNKNOWN)
5472 {
5473 strregname = get_tv_string_chk(&argvars[0]);
5474 error = strregname == NULL;
5475 if (argvars[1].v_type != VAR_UNKNOWN)
5476 {
5477 arg2 = (int)get_tv_number_chk(&argvars[1], &error);
5478 if (!error && argvars[2].v_type != VAR_UNKNOWN)
5479 return_list = (int)get_tv_number_chk(&argvars[2], &error);
5480 }
5481 }
5482 else
5483 strregname = get_vim_var_str(VV_REG);
5484
5485 if (error)
5486 return;
5487
5488 regname = (strregname == NULL ? '"' : *strregname);
5489 if (regname == 0)
5490 regname = '"';
5491
5492 if (return_list)
5493 {
5494 rettv->v_type = VAR_LIST;
5495 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5496 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5497 if (rettv->vval.v_list == NULL)
5498 (void)rettv_list_alloc(rettv);
5499 else
5500 ++rettv->vval.v_list->lv_refcount;
5501 }
5502 else
5503 {
5504 rettv->v_type = VAR_STRING;
5505 rettv->vval.v_string = get_reg_contents(regname,
5506 arg2 ? GREG_EXPR_SRC : 0);
5507 }
5508}
5509
5510/*
5511 * "getregtype()" function
5512 */
5513 static void
5514f_getregtype(typval_T *argvars, typval_T *rettv)
5515{
5516 char_u *strregname;
5517 int regname;
5518 char_u buf[NUMBUFLEN + 2];
5519 long reglen = 0;
5520
5521 if (argvars[0].v_type != VAR_UNKNOWN)
5522 {
5523 strregname = get_tv_string_chk(&argvars[0]);
5524 if (strregname == NULL) /* type error; errmsg already given */
5525 {
5526 rettv->v_type = VAR_STRING;
5527 rettv->vval.v_string = NULL;
5528 return;
5529 }
5530 }
5531 else
5532 /* Default to v:register */
5533 strregname = get_vim_var_str(VV_REG);
5534
5535 regname = (strregname == NULL ? '"' : *strregname);
5536 if (regname == 0)
5537 regname = '"';
5538
5539 buf[0] = NUL;
5540 buf[1] = NUL;
5541 switch (get_reg_type(regname, &reglen))
5542 {
5543 case MLINE: buf[0] = 'V'; break;
5544 case MCHAR: buf[0] = 'v'; break;
5545 case MBLOCK:
5546 buf[0] = Ctrl_V;
5547 sprintf((char *)buf + 1, "%ld", reglen + 1);
5548 break;
5549 }
5550 rettv->v_type = VAR_STRING;
5551 rettv->vval.v_string = vim_strsave(buf);
5552}
5553
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005554/*
5555 * Returns information (variables, options, etc.) about a tab page
5556 * as a dictionary.
5557 */
5558 static dict_T *
5559get_tabpage_info(tabpage_T *tp, int tp_idx)
5560{
5561 win_T *wp;
5562 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005563 list_T *l;
5564
5565 dict = dict_alloc();
5566 if (dict == NULL)
5567 return NULL;
5568
Bram Moolenaare0be1672018-07-08 16:50:37 +02005569 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005570
5571 l = list_alloc();
5572 if (l != NULL)
5573 {
5574 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5575 wp; wp = wp->w_next)
5576 list_append_number(l, (varnumber_T)wp->w_id);
5577 dict_add_list(dict, "windows", l);
5578 }
5579
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005580 /* Make a reference to tabpage variables */
5581 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005582
5583 return dict;
5584}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005585
5586/*
5587 * "gettabinfo()" function
5588 */
5589 static void
5590f_gettabinfo(typval_T *argvars, typval_T *rettv)
5591{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005592 tabpage_T *tp, *tparg = NULL;
5593 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005594 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005595
5596 if (rettv_list_alloc(rettv) != OK)
5597 return;
5598
5599 if (argvars[0].v_type != VAR_UNKNOWN)
5600 {
5601 /* Information about one tab page */
5602 tparg = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5603 if (tparg == NULL)
5604 return;
5605 }
5606
5607 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005608 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005609 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005610 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005611 if (tparg != NULL && tp != tparg)
5612 continue;
5613 d = get_tabpage_info(tp, tpnr);
5614 if (d != NULL)
5615 list_append_dict(rettv->vval.v_list, d);
5616 if (tparg != NULL)
5617 return;
5618 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005619}
5620
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005621/*
5622 * "gettabvar()" function
5623 */
5624 static void
5625f_gettabvar(typval_T *argvars, typval_T *rettv)
5626{
5627 win_T *oldcurwin;
5628 tabpage_T *tp, *oldtabpage;
5629 dictitem_T *v;
5630 char_u *varname;
5631 int done = FALSE;
5632
5633 rettv->v_type = VAR_STRING;
5634 rettv->vval.v_string = NULL;
5635
5636 varname = get_tv_string_chk(&argvars[1]);
5637 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5638 if (tp != NULL && varname != NULL)
5639 {
5640 /* Set tp to be our tabpage, temporarily. Also set the window to the
5641 * first window in the tabpage, otherwise the window is not valid. */
5642 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005643 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5644 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005645 {
5646 /* look up the variable */
5647 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5648 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5649 if (v != NULL)
5650 {
5651 copy_tv(&v->di_tv, rettv);
5652 done = TRUE;
5653 }
5654 }
5655
5656 /* restore previous notion of curwin */
5657 restore_win(oldcurwin, oldtabpage, TRUE);
5658 }
5659
5660 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5661 copy_tv(&argvars[2], rettv);
5662}
5663
5664/*
5665 * "gettabwinvar()" function
5666 */
5667 static void
5668f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5669{
5670 getwinvar(argvars, rettv, 1);
5671}
5672
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005673/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01005674 * "gettagstack()" function
5675 */
5676 static void
5677f_gettagstack(typval_T *argvars, typval_T *rettv)
5678{
5679 win_T *wp = curwin; // default is current window
5680
5681 if (rettv_dict_alloc(rettv) != OK)
5682 return;
5683
5684 if (argvars[0].v_type != VAR_UNKNOWN)
5685 {
5686 wp = find_win_by_nr_or_id(&argvars[0]);
5687 if (wp == NULL)
5688 return;
5689 }
5690
5691 get_tagstack(wp, rettv->vval.v_dict);
5692}
5693
5694/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005695 * Returns information about a window as a dictionary.
5696 */
5697 static dict_T *
5698get_win_info(win_T *wp, short tpnr, short winnr)
5699{
5700 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005701
5702 dict = dict_alloc();
5703 if (dict == NULL)
5704 return NULL;
5705
Bram Moolenaare0be1672018-07-08 16:50:37 +02005706 dict_add_number(dict, "tabnr", tpnr);
5707 dict_add_number(dict, "winnr", winnr);
5708 dict_add_number(dict, "winid", wp->w_id);
5709 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005710 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005711#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02005712 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005713#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005714 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005715 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005716 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005717
Bram Moolenaar69905d12017-08-13 18:14:47 +02005718#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02005719 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02005720#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005721#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02005722 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
5723 dict_add_number(dict, "loclist",
5724 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02005725#endif
5726
Bram Moolenaar30567352016-08-27 21:25:44 +02005727 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005728 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005729
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005730 return dict;
5731}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005732
5733/*
5734 * "getwininfo()" function
5735 */
5736 static void
5737f_getwininfo(typval_T *argvars, typval_T *rettv)
5738{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005739 tabpage_T *tp;
5740 win_T *wp = NULL, *wparg = NULL;
5741 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005742 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005743
5744 if (rettv_list_alloc(rettv) != OK)
5745 return;
5746
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005747 if (argvars[0].v_type != VAR_UNKNOWN)
5748 {
5749 wparg = win_id2wp(argvars);
5750 if (wparg == NULL)
5751 return;
5752 }
5753
5754 /* Collect information about either all the windows across all the tab
5755 * pages or one particular window.
5756 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005757 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005758 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005759 tabnr++;
5760 winnr = 0;
5761 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005762 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005763 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005764 if (wparg != NULL && wp != wparg)
5765 continue;
5766 d = get_win_info(wp, tabnr, winnr);
5767 if (d != NULL)
5768 list_append_dict(rettv->vval.v_list, d);
5769 if (wparg != NULL)
5770 /* found information about a specific window */
5771 return;
5772 }
5773 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005774}
5775
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005776/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005777 * "win_findbuf()" function
5778 */
5779 static void
5780f_win_findbuf(typval_T *argvars, typval_T *rettv)
5781{
5782 if (rettv_list_alloc(rettv) != FAIL)
5783 win_findbuf(argvars, rettv->vval.v_list);
5784}
5785
5786/*
5787 * "win_getid()" function
5788 */
5789 static void
5790f_win_getid(typval_T *argvars, typval_T *rettv)
5791{
5792 rettv->vval.v_number = win_getid(argvars);
5793}
5794
5795/*
5796 * "win_gotoid()" function
5797 */
5798 static void
5799f_win_gotoid(typval_T *argvars, typval_T *rettv)
5800{
5801 rettv->vval.v_number = win_gotoid(argvars);
5802}
5803
5804/*
5805 * "win_id2tabwin()" function
5806 */
5807 static void
5808f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5809{
5810 if (rettv_list_alloc(rettv) != FAIL)
5811 win_id2tabwin(argvars, rettv->vval.v_list);
5812}
5813
5814/*
5815 * "win_id2win()" function
5816 */
5817 static void
5818f_win_id2win(typval_T *argvars, typval_T *rettv)
5819{
5820 rettv->vval.v_number = win_id2win(argvars);
5821}
5822
5823/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005824 * "win_screenpos()" function
5825 */
5826 static void
5827f_win_screenpos(typval_T *argvars, typval_T *rettv)
5828{
5829 win_T *wp;
5830
5831 if (rettv_list_alloc(rettv) == FAIL)
5832 return;
5833
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005834 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005835 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5836 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5837}
5838
5839/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005840 * "getwinpos({timeout})" function
5841 */
5842 static void
5843f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
5844{
5845 int x = -1;
5846 int y = -1;
5847
5848 if (rettv_list_alloc(rettv) == FAIL)
5849 return;
5850#ifdef FEAT_GUI
5851 if (gui.in_use)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01005852 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005853# if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5854 else
5855# endif
5856#endif
5857#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5858 {
5859 varnumber_T timeout = 100;
5860
5861 if (argvars[0].v_type != VAR_UNKNOWN)
5862 timeout = get_tv_number(&argvars[0]);
5863 term_get_winpos(&x, &y, timeout);
5864 }
5865#endif
5866 list_append_number(rettv->vval.v_list, (varnumber_T)x);
5867 list_append_number(rettv->vval.v_list, (varnumber_T)y);
5868}
5869
5870
5871/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005872 * "getwinposx()" function
5873 */
5874 static void
5875f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5876{
5877 rettv->vval.v_number = -1;
5878#ifdef FEAT_GUI
5879 if (gui.in_use)
5880 {
5881 int x, y;
5882
5883 if (gui_mch_get_winpos(&x, &y) == OK)
5884 rettv->vval.v_number = x;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005885 return;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005886 }
5887#endif
5888#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5889 {
5890 int x, y;
5891
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005892 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005893 rettv->vval.v_number = x;
5894 }
5895#endif
5896}
5897
5898/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005899 * "getwinposy()" function
5900 */
5901 static void
5902f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5903{
5904 rettv->vval.v_number = -1;
5905#ifdef FEAT_GUI
5906 if (gui.in_use)
5907 {
5908 int x, y;
5909
5910 if (gui_mch_get_winpos(&x, &y) == OK)
5911 rettv->vval.v_number = y;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005912 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005913 }
5914#endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005915#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5916 {
5917 int x, y;
5918
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005919 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005920 rettv->vval.v_number = y;
5921 }
5922#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005923}
5924
5925/*
5926 * "getwinvar()" function
5927 */
5928 static void
5929f_getwinvar(typval_T *argvars, typval_T *rettv)
5930{
5931 getwinvar(argvars, rettv, 0);
5932}
5933
5934/*
5935 * "glob()" function
5936 */
5937 static void
5938f_glob(typval_T *argvars, typval_T *rettv)
5939{
5940 int options = WILD_SILENT|WILD_USE_NL;
5941 expand_T xpc;
5942 int error = FALSE;
5943
5944 /* When the optional second argument is non-zero, don't remove matches
5945 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5946 rettv->v_type = VAR_STRING;
5947 if (argvars[1].v_type != VAR_UNKNOWN)
5948 {
5949 if (get_tv_number_chk(&argvars[1], &error))
5950 options |= WILD_KEEP_ALL;
5951 if (argvars[2].v_type != VAR_UNKNOWN)
5952 {
5953 if (get_tv_number_chk(&argvars[2], &error))
5954 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005955 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005956 }
5957 if (argvars[3].v_type != VAR_UNKNOWN
5958 && get_tv_number_chk(&argvars[3], &error))
5959 options |= WILD_ALLLINKS;
5960 }
5961 }
5962 if (!error)
5963 {
5964 ExpandInit(&xpc);
5965 xpc.xp_context = EXPAND_FILES;
5966 if (p_wic)
5967 options += WILD_ICASE;
5968 if (rettv->v_type == VAR_STRING)
5969 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
5970 NULL, options, WILD_ALL);
5971 else if (rettv_list_alloc(rettv) != FAIL)
5972 {
5973 int i;
5974
5975 ExpandOne(&xpc, get_tv_string(&argvars[0]),
5976 NULL, options, WILD_ALL_KEEP);
5977 for (i = 0; i < xpc.xp_numfiles; i++)
5978 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5979
5980 ExpandCleanup(&xpc);
5981 }
5982 }
5983 else
5984 rettv->vval.v_string = NULL;
5985}
5986
5987/*
5988 * "globpath()" function
5989 */
5990 static void
5991f_globpath(typval_T *argvars, typval_T *rettv)
5992{
5993 int flags = 0;
5994 char_u buf1[NUMBUFLEN];
5995 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
5996 int error = FALSE;
5997 garray_T ga;
5998 int i;
5999
6000 /* When the optional second argument is non-zero, don't remove matches
6001 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6002 rettv->v_type = VAR_STRING;
6003 if (argvars[2].v_type != VAR_UNKNOWN)
6004 {
6005 if (get_tv_number_chk(&argvars[2], &error))
6006 flags |= WILD_KEEP_ALL;
6007 if (argvars[3].v_type != VAR_UNKNOWN)
6008 {
6009 if (get_tv_number_chk(&argvars[3], &error))
6010 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006011 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006012 }
6013 if (argvars[4].v_type != VAR_UNKNOWN
6014 && get_tv_number_chk(&argvars[4], &error))
6015 flags |= WILD_ALLLINKS;
6016 }
6017 }
6018 if (file != NULL && !error)
6019 {
6020 ga_init2(&ga, (int)sizeof(char_u *), 10);
6021 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
6022 if (rettv->v_type == VAR_STRING)
6023 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
6024 else if (rettv_list_alloc(rettv) != FAIL)
6025 for (i = 0; i < ga.ga_len; ++i)
6026 list_append_string(rettv->vval.v_list,
6027 ((char_u **)(ga.ga_data))[i], -1);
6028 ga_clear_strings(&ga);
6029 }
6030 else
6031 rettv->vval.v_string = NULL;
6032}
6033
6034/*
6035 * "glob2regpat()" function
6036 */
6037 static void
6038f_glob2regpat(typval_T *argvars, typval_T *rettv)
6039{
6040 char_u *pat = get_tv_string_chk(&argvars[0]);
6041
6042 rettv->v_type = VAR_STRING;
6043 rettv->vval.v_string = (pat == NULL)
6044 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
6045}
6046
6047/* for VIM_VERSION_ defines */
6048#include "version.h"
6049
6050/*
6051 * "has()" function
6052 */
6053 static void
6054f_has(typval_T *argvars, typval_T *rettv)
6055{
6056 int i;
6057 char_u *name;
6058 int n = FALSE;
6059 static char *(has_list[]) =
6060 {
6061#ifdef AMIGA
6062 "amiga",
6063# ifdef FEAT_ARP
6064 "arp",
6065# endif
6066#endif
6067#ifdef __BEOS__
6068 "beos",
6069#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02006070#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01006071 "mac", /* Mac OS X (and, once, Mac OS Classic) */
6072 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02006073# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01006074 "macunix", /* Mac OS X, with the darwin feature */
6075 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02006076# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006077#endif
6078#ifdef __QNX__
6079 "qnx",
6080#endif
6081#ifdef UNIX
6082 "unix",
6083#endif
6084#ifdef VMS
6085 "vms",
6086#endif
6087#ifdef WIN32
6088 "win32",
6089#endif
6090#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
6091 "win32unix",
6092#endif
6093#if defined(WIN64) || defined(_WIN64)
6094 "win64",
6095#endif
6096#ifdef EBCDIC
6097 "ebcdic",
6098#endif
6099#ifndef CASE_INSENSITIVE_FILENAME
6100 "fname_case",
6101#endif
6102#ifdef HAVE_ACL
6103 "acl",
6104#endif
6105#ifdef FEAT_ARABIC
6106 "arabic",
6107#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006108 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006109#ifdef FEAT_AUTOCHDIR
6110 "autochdir",
6111#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006112#ifdef FEAT_AUTOSERVERNAME
6113 "autoservername",
6114#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006115#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006116 "balloon_eval",
6117# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
6118 "balloon_multiline",
6119# endif
6120#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006121#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006122 "balloon_eval_term",
6123#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006124#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6125 "builtin_terms",
6126# ifdef ALL_BUILTIN_TCAPS
6127 "all_builtin_terms",
6128# endif
6129#endif
6130#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
6131 || defined(FEAT_GUI_W32) \
6132 || defined(FEAT_GUI_MOTIF))
6133 "browsefilter",
6134#endif
6135#ifdef FEAT_BYTEOFF
6136 "byte_offset",
6137#endif
6138#ifdef FEAT_JOB_CHANNEL
6139 "channel",
6140#endif
6141#ifdef FEAT_CINDENT
6142 "cindent",
6143#endif
6144#ifdef FEAT_CLIENTSERVER
6145 "clientserver",
6146#endif
6147#ifdef FEAT_CLIPBOARD
6148 "clipboard",
6149#endif
6150#ifdef FEAT_CMDL_COMPL
6151 "cmdline_compl",
6152#endif
6153#ifdef FEAT_CMDHIST
6154 "cmdline_hist",
6155#endif
6156#ifdef FEAT_COMMENTS
6157 "comments",
6158#endif
6159#ifdef FEAT_CONCEAL
6160 "conceal",
6161#endif
6162#ifdef FEAT_CRYPT
6163 "cryptv",
6164 "crypt-blowfish",
6165 "crypt-blowfish2",
6166#endif
6167#ifdef FEAT_CSCOPE
6168 "cscope",
6169#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006170 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006171#ifdef CURSOR_SHAPE
6172 "cursorshape",
6173#endif
6174#ifdef DEBUG
6175 "debug",
6176#endif
6177#ifdef FEAT_CON_DIALOG
6178 "dialog_con",
6179#endif
6180#ifdef FEAT_GUI_DIALOG
6181 "dialog_gui",
6182#endif
6183#ifdef FEAT_DIFF
6184 "diff",
6185#endif
6186#ifdef FEAT_DIGRAPHS
6187 "digraphs",
6188#endif
6189#ifdef FEAT_DIRECTX
6190 "directx",
6191#endif
6192#ifdef FEAT_DND
6193 "dnd",
6194#endif
6195#ifdef FEAT_EMACS_TAGS
6196 "emacs_tags",
6197#endif
6198 "eval", /* always present, of course! */
6199 "ex_extra", /* graduated feature */
6200#ifdef FEAT_SEARCH_EXTRA
6201 "extra_search",
6202#endif
6203#ifdef FEAT_FKMAP
6204 "farsi",
6205#endif
6206#ifdef FEAT_SEARCHPATH
6207 "file_in_path",
6208#endif
6209#ifdef FEAT_FILTERPIPE
6210 "filterpipe",
6211#endif
6212#ifdef FEAT_FIND_ID
6213 "find_in_path",
6214#endif
6215#ifdef FEAT_FLOAT
6216 "float",
6217#endif
6218#ifdef FEAT_FOLDING
6219 "folding",
6220#endif
6221#ifdef FEAT_FOOTER
6222 "footer",
6223#endif
6224#if !defined(USE_SYSTEM) && defined(UNIX)
6225 "fork",
6226#endif
6227#ifdef FEAT_GETTEXT
6228 "gettext",
6229#endif
6230#ifdef FEAT_GUI
6231 "gui",
6232#endif
6233#ifdef FEAT_GUI_ATHENA
6234# ifdef FEAT_GUI_NEXTAW
6235 "gui_neXtaw",
6236# else
6237 "gui_athena",
6238# endif
6239#endif
6240#ifdef FEAT_GUI_GTK
6241 "gui_gtk",
6242# ifdef USE_GTK3
6243 "gui_gtk3",
6244# else
6245 "gui_gtk2",
6246# endif
6247#endif
6248#ifdef FEAT_GUI_GNOME
6249 "gui_gnome",
6250#endif
6251#ifdef FEAT_GUI_MAC
6252 "gui_mac",
6253#endif
6254#ifdef FEAT_GUI_MOTIF
6255 "gui_motif",
6256#endif
6257#ifdef FEAT_GUI_PHOTON
6258 "gui_photon",
6259#endif
6260#ifdef FEAT_GUI_W32
6261 "gui_win32",
6262#endif
6263#ifdef FEAT_HANGULIN
6264 "hangul_input",
6265#endif
6266#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6267 "iconv",
6268#endif
6269#ifdef FEAT_INS_EXPAND
6270 "insert_expand",
6271#endif
6272#ifdef FEAT_JOB_CHANNEL
6273 "job",
6274#endif
6275#ifdef FEAT_JUMPLIST
6276 "jumplist",
6277#endif
6278#ifdef FEAT_KEYMAP
6279 "keymap",
6280#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006281 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006282#ifdef FEAT_LANGMAP
6283 "langmap",
6284#endif
6285#ifdef FEAT_LIBCALL
6286 "libcall",
6287#endif
6288#ifdef FEAT_LINEBREAK
6289 "linebreak",
6290#endif
6291#ifdef FEAT_LISP
6292 "lispindent",
6293#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006294 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006295#ifdef FEAT_LOCALMAP
6296 "localmap",
6297#endif
6298#ifdef FEAT_LUA
6299# ifndef DYNAMIC_LUA
6300 "lua",
6301# endif
6302#endif
6303#ifdef FEAT_MENU
6304 "menu",
6305#endif
6306#ifdef FEAT_SESSION
6307 "mksession",
6308#endif
6309#ifdef FEAT_MODIFY_FNAME
6310 "modify_fname",
6311#endif
6312#ifdef FEAT_MOUSE
6313 "mouse",
6314#endif
6315#ifdef FEAT_MOUSESHAPE
6316 "mouseshape",
6317#endif
6318#if defined(UNIX) || defined(VMS)
6319# ifdef FEAT_MOUSE_DEC
6320 "mouse_dec",
6321# endif
6322# ifdef FEAT_MOUSE_GPM
6323 "mouse_gpm",
6324# endif
6325# ifdef FEAT_MOUSE_JSB
6326 "mouse_jsbterm",
6327# endif
6328# ifdef FEAT_MOUSE_NET
6329 "mouse_netterm",
6330# endif
6331# ifdef FEAT_MOUSE_PTERM
6332 "mouse_pterm",
6333# endif
6334# ifdef FEAT_MOUSE_SGR
6335 "mouse_sgr",
6336# endif
6337# ifdef FEAT_SYSMOUSE
6338 "mouse_sysmouse",
6339# endif
6340# ifdef FEAT_MOUSE_URXVT
6341 "mouse_urxvt",
6342# endif
6343# ifdef FEAT_MOUSE_XTERM
6344 "mouse_xterm",
6345# endif
6346#endif
6347#ifdef FEAT_MBYTE
6348 "multi_byte",
6349#endif
6350#ifdef FEAT_MBYTE_IME
6351 "multi_byte_ime",
6352#endif
6353#ifdef FEAT_MULTI_LANG
6354 "multi_lang",
6355#endif
6356#ifdef FEAT_MZSCHEME
6357#ifndef DYNAMIC_MZSCHEME
6358 "mzscheme",
6359#endif
6360#endif
6361#ifdef FEAT_NUM64
6362 "num64",
6363#endif
6364#ifdef FEAT_OLE
6365 "ole",
6366#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006367#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006368 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006369#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006370#ifdef FEAT_PATH_EXTRA
6371 "path_extra",
6372#endif
6373#ifdef FEAT_PERL
6374#ifndef DYNAMIC_PERL
6375 "perl",
6376#endif
6377#endif
6378#ifdef FEAT_PERSISTENT_UNDO
6379 "persistent_undo",
6380#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006381#if defined(FEAT_PYTHON)
6382 "python_compiled",
6383# if defined(DYNAMIC_PYTHON)
6384 "python_dynamic",
6385# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006386 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006387 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006388# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006389#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006390#if defined(FEAT_PYTHON3)
6391 "python3_compiled",
6392# if defined(DYNAMIC_PYTHON3)
6393 "python3_dynamic",
6394# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006395 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006396 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006397# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006398#endif
6399#ifdef FEAT_POSTSCRIPT
6400 "postscript",
6401#endif
6402#ifdef FEAT_PRINTER
6403 "printer",
6404#endif
6405#ifdef FEAT_PROFILE
6406 "profile",
6407#endif
6408#ifdef FEAT_RELTIME
6409 "reltime",
6410#endif
6411#ifdef FEAT_QUICKFIX
6412 "quickfix",
6413#endif
6414#ifdef FEAT_RIGHTLEFT
6415 "rightleft",
6416#endif
6417#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6418 "ruby",
6419#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006420 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006421#ifdef FEAT_CMDL_INFO
6422 "showcmd",
6423 "cmdline_info",
6424#endif
6425#ifdef FEAT_SIGNS
6426 "signs",
6427#endif
6428#ifdef FEAT_SMARTINDENT
6429 "smartindent",
6430#endif
6431#ifdef STARTUPTIME
6432 "startuptime",
6433#endif
6434#ifdef FEAT_STL_OPT
6435 "statusline",
6436#endif
6437#ifdef FEAT_SUN_WORKSHOP
6438 "sun_workshop",
6439#endif
6440#ifdef FEAT_NETBEANS_INTG
6441 "netbeans_intg",
6442#endif
6443#ifdef FEAT_SPELL
6444 "spell",
6445#endif
6446#ifdef FEAT_SYN_HL
6447 "syntax",
6448#endif
6449#if defined(USE_SYSTEM) || !defined(UNIX)
6450 "system",
6451#endif
6452#ifdef FEAT_TAG_BINS
6453 "tag_binary",
6454#endif
6455#ifdef FEAT_TAG_OLDSTATIC
6456 "tag_old_static",
6457#endif
6458#ifdef FEAT_TAG_ANYWHITE
6459 "tag_any_white",
6460#endif
6461#ifdef FEAT_TCL
6462# ifndef DYNAMIC_TCL
6463 "tcl",
6464# endif
6465#endif
6466#ifdef FEAT_TERMGUICOLORS
6467 "termguicolors",
6468#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006469#if defined(FEAT_TERMINAL) && !defined(WIN3264)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006470 "terminal",
6471#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006472#ifdef TERMINFO
6473 "terminfo",
6474#endif
6475#ifdef FEAT_TERMRESPONSE
6476 "termresponse",
6477#endif
6478#ifdef FEAT_TEXTOBJ
6479 "textobjects",
6480#endif
6481#ifdef HAVE_TGETENT
6482 "tgetent",
6483#endif
6484#ifdef FEAT_TIMERS
6485 "timers",
6486#endif
6487#ifdef FEAT_TITLE
6488 "title",
6489#endif
6490#ifdef FEAT_TOOLBAR
6491 "toolbar",
6492#endif
6493#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6494 "unnamedplus",
6495#endif
6496#ifdef FEAT_USR_CMDS
6497 "user-commands", /* was accidentally included in 5.4 */
6498 "user_commands",
6499#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006500#ifdef FEAT_VARTABS
6501 "vartabs",
6502#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006503#ifdef FEAT_VIMINFO
6504 "viminfo",
6505#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006506 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006507#ifdef FEAT_VIRTUALEDIT
6508 "virtualedit",
6509#endif
6510 "visual",
6511#ifdef FEAT_VISUALEXTRA
6512 "visualextra",
6513#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006514 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006515#ifdef FEAT_VTP
6516 "vtp",
6517#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006518#ifdef FEAT_WILDIGN
6519 "wildignore",
6520#endif
6521#ifdef FEAT_WILDMENU
6522 "wildmenu",
6523#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006524 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006525#ifdef FEAT_WAK
6526 "winaltkeys",
6527#endif
6528#ifdef FEAT_WRITEBACKUP
6529 "writebackup",
6530#endif
6531#ifdef FEAT_XIM
6532 "xim",
6533#endif
6534#ifdef FEAT_XFONTSET
6535 "xfontset",
6536#endif
6537#ifdef FEAT_XPM_W32
6538 "xpm",
6539 "xpm_w32", /* for backward compatibility */
6540#else
6541# if defined(HAVE_XPM)
6542 "xpm",
6543# endif
6544#endif
6545#ifdef USE_XSMP
6546 "xsmp",
6547#endif
6548#ifdef USE_XSMP_INTERACT
6549 "xsmp_interact",
6550#endif
6551#ifdef FEAT_XCLIPBOARD
6552 "xterm_clipboard",
6553#endif
6554#ifdef FEAT_XTERM_SAVE
6555 "xterm_save",
6556#endif
6557#if defined(UNIX) && defined(FEAT_X11)
6558 "X11",
6559#endif
6560 NULL
6561 };
6562
6563 name = get_tv_string(&argvars[0]);
6564 for (i = 0; has_list[i] != NULL; ++i)
6565 if (STRICMP(name, has_list[i]) == 0)
6566 {
6567 n = TRUE;
6568 break;
6569 }
6570
6571 if (n == FALSE)
6572 {
6573 if (STRNICMP(name, "patch", 5) == 0)
6574 {
6575 if (name[5] == '-'
6576 && STRLEN(name) >= 11
6577 && vim_isdigit(name[6])
6578 && vim_isdigit(name[8])
6579 && vim_isdigit(name[10]))
6580 {
6581 int major = atoi((char *)name + 6);
6582 int minor = atoi((char *)name + 8);
6583
6584 /* Expect "patch-9.9.01234". */
6585 n = (major < VIM_VERSION_MAJOR
6586 || (major == VIM_VERSION_MAJOR
6587 && (minor < VIM_VERSION_MINOR
6588 || (minor == VIM_VERSION_MINOR
6589 && has_patch(atoi((char *)name + 10))))));
6590 }
6591 else
6592 n = has_patch(atoi((char *)name + 5));
6593 }
6594 else if (STRICMP(name, "vim_starting") == 0)
6595 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006596 else if (STRICMP(name, "ttyin") == 0)
6597 n = mch_input_isatty();
6598 else if (STRICMP(name, "ttyout") == 0)
6599 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006600#ifdef FEAT_MBYTE
6601 else if (STRICMP(name, "multi_byte_encoding") == 0)
6602 n = has_mbyte;
6603#endif
6604#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
6605 else if (STRICMP(name, "balloon_multiline") == 0)
6606 n = multiline_balloon_available();
6607#endif
6608#ifdef DYNAMIC_TCL
6609 else if (STRICMP(name, "tcl") == 0)
6610 n = tcl_enabled(FALSE);
6611#endif
6612#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6613 else if (STRICMP(name, "iconv") == 0)
6614 n = iconv_enabled(FALSE);
6615#endif
6616#ifdef DYNAMIC_LUA
6617 else if (STRICMP(name, "lua") == 0)
6618 n = lua_enabled(FALSE);
6619#endif
6620#ifdef DYNAMIC_MZSCHEME
6621 else if (STRICMP(name, "mzscheme") == 0)
6622 n = mzscheme_enabled(FALSE);
6623#endif
6624#ifdef DYNAMIC_RUBY
6625 else if (STRICMP(name, "ruby") == 0)
6626 n = ruby_enabled(FALSE);
6627#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006628#ifdef DYNAMIC_PYTHON
6629 else if (STRICMP(name, "python") == 0)
6630 n = python_enabled(FALSE);
6631#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006632#ifdef DYNAMIC_PYTHON3
6633 else if (STRICMP(name, "python3") == 0)
6634 n = python3_enabled(FALSE);
6635#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006636#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6637 else if (STRICMP(name, "pythonx") == 0)
6638 {
6639# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6640 if (p_pyx == 0)
6641 n = python3_enabled(FALSE) || python_enabled(FALSE);
6642 else if (p_pyx == 3)
6643 n = python3_enabled(FALSE);
6644 else if (p_pyx == 2)
6645 n = python_enabled(FALSE);
6646# elif defined(DYNAMIC_PYTHON)
6647 n = python_enabled(FALSE);
6648# elif defined(DYNAMIC_PYTHON3)
6649 n = python3_enabled(FALSE);
6650# endif
6651 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006652#endif
6653#ifdef DYNAMIC_PERL
6654 else if (STRICMP(name, "perl") == 0)
6655 n = perl_enabled(FALSE);
6656#endif
6657#ifdef FEAT_GUI
6658 else if (STRICMP(name, "gui_running") == 0)
6659 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006660# ifdef FEAT_BROWSE
6661 else if (STRICMP(name, "browse") == 0)
6662 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6663# endif
6664#endif
6665#ifdef FEAT_SYN_HL
6666 else if (STRICMP(name, "syntax_items") == 0)
6667 n = syntax_present(curwin);
6668#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006669#ifdef FEAT_VTP
6670 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02006671 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006672#endif
6673#ifdef FEAT_NETBEANS_INTG
6674 else if (STRICMP(name, "netbeans_enabled") == 0)
6675 n = netbeans_active();
6676#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006677#if defined(FEAT_TERMINAL) && defined(WIN3264)
6678 else if (STRICMP(name, "terminal") == 0)
6679 n = terminal_enabled();
6680#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006681 }
6682
6683 rettv->vval.v_number = n;
6684}
6685
6686/*
6687 * "has_key()" function
6688 */
6689 static void
6690f_has_key(typval_T *argvars, typval_T *rettv)
6691{
6692 if (argvars[0].v_type != VAR_DICT)
6693 {
6694 EMSG(_(e_dictreq));
6695 return;
6696 }
6697 if (argvars[0].vval.v_dict == NULL)
6698 return;
6699
6700 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
6701 get_tv_string(&argvars[1]), -1) != NULL;
6702}
6703
6704/*
6705 * "haslocaldir()" function
6706 */
6707 static void
6708f_haslocaldir(typval_T *argvars, typval_T *rettv)
6709{
6710 win_T *wp = NULL;
6711
6712 wp = find_tabwin(&argvars[0], &argvars[1]);
6713 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6714}
6715
6716/*
6717 * "hasmapto()" function
6718 */
6719 static void
6720f_hasmapto(typval_T *argvars, typval_T *rettv)
6721{
6722 char_u *name;
6723 char_u *mode;
6724 char_u buf[NUMBUFLEN];
6725 int abbr = FALSE;
6726
6727 name = get_tv_string(&argvars[0]);
6728 if (argvars[1].v_type == VAR_UNKNOWN)
6729 mode = (char_u *)"nvo";
6730 else
6731 {
6732 mode = get_tv_string_buf(&argvars[1], buf);
6733 if (argvars[2].v_type != VAR_UNKNOWN)
6734 abbr = (int)get_tv_number(&argvars[2]);
6735 }
6736
6737 if (map_to_exists(name, mode, abbr))
6738 rettv->vval.v_number = TRUE;
6739 else
6740 rettv->vval.v_number = FALSE;
6741}
6742
6743/*
6744 * "histadd()" function
6745 */
6746 static void
6747f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6748{
6749#ifdef FEAT_CMDHIST
6750 int histype;
6751 char_u *str;
6752 char_u buf[NUMBUFLEN];
6753#endif
6754
6755 rettv->vval.v_number = FALSE;
6756 if (check_restricted() || check_secure())
6757 return;
6758#ifdef FEAT_CMDHIST
6759 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6760 histype = str != NULL ? get_histtype(str) : -1;
6761 if (histype >= 0)
6762 {
6763 str = get_tv_string_buf(&argvars[1], buf);
6764 if (*str != NUL)
6765 {
6766 init_history();
6767 add_to_history(histype, str, FALSE, NUL);
6768 rettv->vval.v_number = TRUE;
6769 return;
6770 }
6771 }
6772#endif
6773}
6774
6775/*
6776 * "histdel()" function
6777 */
6778 static void
6779f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6780{
6781#ifdef FEAT_CMDHIST
6782 int n;
6783 char_u buf[NUMBUFLEN];
6784 char_u *str;
6785
6786 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6787 if (str == NULL)
6788 n = 0;
6789 else if (argvars[1].v_type == VAR_UNKNOWN)
6790 /* only one argument: clear entire history */
6791 n = clr_history(get_histtype(str));
6792 else if (argvars[1].v_type == VAR_NUMBER)
6793 /* index given: remove that entry */
6794 n = del_history_idx(get_histtype(str),
6795 (int)get_tv_number(&argvars[1]));
6796 else
6797 /* string given: remove all matching entries */
6798 n = del_history_entry(get_histtype(str),
6799 get_tv_string_buf(&argvars[1], buf));
6800 rettv->vval.v_number = n;
6801#endif
6802}
6803
6804/*
6805 * "histget()" function
6806 */
6807 static void
6808f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6809{
6810#ifdef FEAT_CMDHIST
6811 int type;
6812 int idx;
6813 char_u *str;
6814
6815 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6816 if (str == NULL)
6817 rettv->vval.v_string = NULL;
6818 else
6819 {
6820 type = get_histtype(str);
6821 if (argvars[1].v_type == VAR_UNKNOWN)
6822 idx = get_history_idx(type);
6823 else
6824 idx = (int)get_tv_number_chk(&argvars[1], NULL);
6825 /* -1 on type error */
6826 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6827 }
6828#else
6829 rettv->vval.v_string = NULL;
6830#endif
6831 rettv->v_type = VAR_STRING;
6832}
6833
6834/*
6835 * "histnr()" function
6836 */
6837 static void
6838f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6839{
6840 int i;
6841
6842#ifdef FEAT_CMDHIST
6843 char_u *history = get_tv_string_chk(&argvars[0]);
6844
6845 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6846 if (i >= HIST_CMD && i < HIST_COUNT)
6847 i = get_history_idx(i);
6848 else
6849#endif
6850 i = -1;
6851 rettv->vval.v_number = i;
6852}
6853
6854/*
6855 * "highlightID(name)" function
6856 */
6857 static void
6858f_hlID(typval_T *argvars, typval_T *rettv)
6859{
6860 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
6861}
6862
6863/*
6864 * "highlight_exists()" function
6865 */
6866 static void
6867f_hlexists(typval_T *argvars, typval_T *rettv)
6868{
6869 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
6870}
6871
6872/*
6873 * "hostname()" function
6874 */
6875 static void
6876f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6877{
6878 char_u hostname[256];
6879
6880 mch_get_host_name(hostname, 256);
6881 rettv->v_type = VAR_STRING;
6882 rettv->vval.v_string = vim_strsave(hostname);
6883}
6884
6885/*
6886 * iconv() function
6887 */
6888 static void
6889f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6890{
6891#ifdef FEAT_MBYTE
6892 char_u buf1[NUMBUFLEN];
6893 char_u buf2[NUMBUFLEN];
6894 char_u *from, *to, *str;
6895 vimconv_T vimconv;
6896#endif
6897
6898 rettv->v_type = VAR_STRING;
6899 rettv->vval.v_string = NULL;
6900
6901#ifdef FEAT_MBYTE
6902 str = get_tv_string(&argvars[0]);
6903 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
6904 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
6905 vimconv.vc_type = CONV_NONE;
6906 convert_setup(&vimconv, from, to);
6907
6908 /* If the encodings are equal, no conversion needed. */
6909 if (vimconv.vc_type == CONV_NONE)
6910 rettv->vval.v_string = vim_strsave(str);
6911 else
6912 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6913
6914 convert_setup(&vimconv, NULL, NULL);
6915 vim_free(from);
6916 vim_free(to);
6917#endif
6918}
6919
6920/*
6921 * "indent()" function
6922 */
6923 static void
6924f_indent(typval_T *argvars, typval_T *rettv)
6925{
6926 linenr_T lnum;
6927
6928 lnum = get_tv_lnum(argvars);
6929 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6930 rettv->vval.v_number = get_indent_lnum(lnum);
6931 else
6932 rettv->vval.v_number = -1;
6933}
6934
6935/*
6936 * "index()" function
6937 */
6938 static void
6939f_index(typval_T *argvars, typval_T *rettv)
6940{
6941 list_T *l;
6942 listitem_T *item;
6943 long idx = 0;
6944 int ic = FALSE;
6945
6946 rettv->vval.v_number = -1;
6947 if (argvars[0].v_type != VAR_LIST)
6948 {
6949 EMSG(_(e_listreq));
6950 return;
6951 }
6952 l = argvars[0].vval.v_list;
6953 if (l != NULL)
6954 {
6955 item = l->lv_first;
6956 if (argvars[2].v_type != VAR_UNKNOWN)
6957 {
6958 int error = FALSE;
6959
6960 /* Start at specified item. Use the cached index that list_find()
6961 * sets, so that a negative number also works. */
6962 item = list_find(l, (long)get_tv_number_chk(&argvars[2], &error));
6963 idx = l->lv_idx;
6964 if (argvars[3].v_type != VAR_UNKNOWN)
6965 ic = (int)get_tv_number_chk(&argvars[3], &error);
6966 if (error)
6967 item = NULL;
6968 }
6969
6970 for ( ; item != NULL; item = item->li_next, ++idx)
6971 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6972 {
6973 rettv->vval.v_number = idx;
6974 break;
6975 }
6976 }
6977}
6978
6979static int inputsecret_flag = 0;
6980
6981/*
6982 * "input()" function
6983 * Also handles inputsecret() when inputsecret is set.
6984 */
6985 static void
6986f_input(typval_T *argvars, typval_T *rettv)
6987{
6988 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6989}
6990
6991/*
6992 * "inputdialog()" function
6993 */
6994 static void
6995f_inputdialog(typval_T *argvars, typval_T *rettv)
6996{
6997#if defined(FEAT_GUI_TEXTDIALOG)
6998 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
6999 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
7000 {
7001 char_u *message;
7002 char_u buf[NUMBUFLEN];
7003 char_u *defstr = (char_u *)"";
7004
7005 message = get_tv_string_chk(&argvars[0]);
7006 if (argvars[1].v_type != VAR_UNKNOWN
7007 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
7008 vim_strncpy(IObuff, defstr, IOSIZE - 1);
7009 else
7010 IObuff[0] = NUL;
7011 if (message != NULL && defstr != NULL
7012 && do_dialog(VIM_QUESTION, NULL, message,
7013 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
7014 rettv->vval.v_string = vim_strsave(IObuff);
7015 else
7016 {
7017 if (message != NULL && defstr != NULL
7018 && argvars[1].v_type != VAR_UNKNOWN
7019 && argvars[2].v_type != VAR_UNKNOWN)
7020 rettv->vval.v_string = vim_strsave(
7021 get_tv_string_buf(&argvars[2], buf));
7022 else
7023 rettv->vval.v_string = NULL;
7024 }
7025 rettv->v_type = VAR_STRING;
7026 }
7027 else
7028#endif
7029 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
7030}
7031
7032/*
7033 * "inputlist()" function
7034 */
7035 static void
7036f_inputlist(typval_T *argvars, typval_T *rettv)
7037{
7038 listitem_T *li;
7039 int selected;
7040 int mouse_used;
7041
7042#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007043 /* While starting up, there is no place to enter text. When running tests
7044 * with --not-a-term we assume feedkeys() will be used. */
7045 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007046 return;
7047#endif
7048 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
7049 {
7050 EMSG2(_(e_listarg), "inputlist()");
7051 return;
7052 }
7053
7054 msg_start();
7055 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
7056 lines_left = Rows; /* avoid more prompt */
7057 msg_scroll = TRUE;
7058 msg_clr_eos();
7059
7060 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
7061 {
7062 msg_puts(get_tv_string(&li->li_tv));
7063 msg_putchar('\n');
7064 }
7065
7066 /* Ask for choice. */
7067 selected = prompt_for_number(&mouse_used);
7068 if (mouse_used)
7069 selected -= lines_left;
7070
7071 rettv->vval.v_number = selected;
7072}
7073
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007074static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7075
7076/*
7077 * "inputrestore()" function
7078 */
7079 static void
7080f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7081{
7082 if (ga_userinput.ga_len > 0)
7083 {
7084 --ga_userinput.ga_len;
7085 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7086 + ga_userinput.ga_len);
7087 /* default return is zero == OK */
7088 }
7089 else if (p_verbose > 1)
7090 {
7091 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
7092 rettv->vval.v_number = 1; /* Failed */
7093 }
7094}
7095
7096/*
7097 * "inputsave()" function
7098 */
7099 static void
7100f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7101{
7102 /* Add an entry to the stack of typeahead storage. */
7103 if (ga_grow(&ga_userinput, 1) == OK)
7104 {
7105 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7106 + ga_userinput.ga_len);
7107 ++ga_userinput.ga_len;
7108 /* default return is zero == OK */
7109 }
7110 else
7111 rettv->vval.v_number = 1; /* Failed */
7112}
7113
7114/*
7115 * "inputsecret()" function
7116 */
7117 static void
7118f_inputsecret(typval_T *argvars, typval_T *rettv)
7119{
7120 ++cmdline_star;
7121 ++inputsecret_flag;
7122 f_input(argvars, rettv);
7123 --cmdline_star;
7124 --inputsecret_flag;
7125}
7126
7127/*
7128 * "insert()" function
7129 */
7130 static void
7131f_insert(typval_T *argvars, typval_T *rettv)
7132{
7133 long before = 0;
7134 listitem_T *item;
7135 list_T *l;
7136 int error = FALSE;
7137
7138 if (argvars[0].v_type != VAR_LIST)
7139 EMSG2(_(e_listarg), "insert()");
7140 else if ((l = argvars[0].vval.v_list) != NULL
7141 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
7142 {
7143 if (argvars[2].v_type != VAR_UNKNOWN)
7144 before = (long)get_tv_number_chk(&argvars[2], &error);
7145 if (error)
7146 return; /* type error; errmsg already given */
7147
7148 if (before == l->lv_len)
7149 item = NULL;
7150 else
7151 {
7152 item = list_find(l, before);
7153 if (item == NULL)
7154 {
7155 EMSGN(_(e_listidx), before);
7156 l = NULL;
7157 }
7158 }
7159 if (l != NULL)
7160 {
7161 list_insert_tv(l, &argvars[1], item);
7162 copy_tv(&argvars[0], rettv);
7163 }
7164 }
7165}
7166
7167/*
7168 * "invert(expr)" function
7169 */
7170 static void
7171f_invert(typval_T *argvars, typval_T *rettv)
7172{
7173 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
7174}
7175
7176/*
7177 * "isdirectory()" function
7178 */
7179 static void
7180f_isdirectory(typval_T *argvars, typval_T *rettv)
7181{
7182 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
7183}
7184
7185/*
7186 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7187 * or it refers to a List or Dictionary that is locked.
7188 */
7189 static int
7190tv_islocked(typval_T *tv)
7191{
7192 return (tv->v_lock & VAR_LOCKED)
7193 || (tv->v_type == VAR_LIST
7194 && tv->vval.v_list != NULL
7195 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7196 || (tv->v_type == VAR_DICT
7197 && tv->vval.v_dict != NULL
7198 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7199}
7200
7201/*
7202 * "islocked()" function
7203 */
7204 static void
7205f_islocked(typval_T *argvars, typval_T *rettv)
7206{
7207 lval_T lv;
7208 char_u *end;
7209 dictitem_T *di;
7210
7211 rettv->vval.v_number = -1;
7212 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007213 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007214 if (end != NULL && lv.ll_name != NULL)
7215 {
7216 if (*end != NUL)
7217 EMSG(_(e_trailing));
7218 else
7219 {
7220 if (lv.ll_tv == NULL)
7221 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007222 di = find_var(lv.ll_name, NULL, TRUE);
7223 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007224 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007225 /* Consider a variable locked when:
7226 * 1. the variable itself is locked
7227 * 2. the value of the variable is locked.
7228 * 3. the List or Dict value is locked.
7229 */
7230 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7231 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007232 }
7233 }
7234 else if (lv.ll_range)
7235 EMSG(_("E786: Range not allowed"));
7236 else if (lv.ll_newkey != NULL)
7237 EMSG2(_(e_dictkey), lv.ll_newkey);
7238 else if (lv.ll_list != NULL)
7239 /* List item. */
7240 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7241 else
7242 /* Dictionary item. */
7243 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7244 }
7245 }
7246
7247 clear_lval(&lv);
7248}
7249
7250#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7251/*
7252 * "isnan()" function
7253 */
7254 static void
7255f_isnan(typval_T *argvars, typval_T *rettv)
7256{
7257 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7258 && isnan(argvars[0].vval.v_float);
7259}
7260#endif
7261
7262/*
7263 * "items(dict)" function
7264 */
7265 static void
7266f_items(typval_T *argvars, typval_T *rettv)
7267{
7268 dict_list(argvars, rettv, 2);
7269}
7270
7271#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
7272/*
7273 * Get the job from the argument.
7274 * Returns NULL if the job is invalid.
7275 */
7276 static job_T *
7277get_job_arg(typval_T *tv)
7278{
7279 job_T *job;
7280
7281 if (tv->v_type != VAR_JOB)
7282 {
7283 EMSG2(_(e_invarg2), get_tv_string(tv));
7284 return NULL;
7285 }
7286 job = tv->vval.v_job;
7287
7288 if (job == NULL)
7289 EMSG(_("E916: not a valid job"));
7290 return job;
7291}
7292
7293/*
7294 * "job_getchannel()" function
7295 */
7296 static void
7297f_job_getchannel(typval_T *argvars, typval_T *rettv)
7298{
7299 job_T *job = get_job_arg(&argvars[0]);
7300
7301 if (job != NULL)
7302 {
7303 rettv->v_type = VAR_CHANNEL;
7304 rettv->vval.v_channel = job->jv_channel;
7305 if (job->jv_channel != NULL)
7306 ++job->jv_channel->ch_refcount;
7307 }
7308}
7309
7310/*
7311 * "job_info()" function
7312 */
7313 static void
7314f_job_info(typval_T *argvars, typval_T *rettv)
7315{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007316 if (argvars[0].v_type != VAR_UNKNOWN)
7317 {
7318 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007319
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007320 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7321 job_info(job, rettv->vval.v_dict);
7322 }
7323 else if (rettv_list_alloc(rettv) == OK)
7324 job_info_all(rettv->vval.v_list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007325}
7326
7327/*
7328 * "job_setoptions()" function
7329 */
7330 static void
7331f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7332{
7333 job_T *job = get_job_arg(&argvars[0]);
7334 jobopt_T opt;
7335
7336 if (job == NULL)
7337 return;
7338 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007339 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007340 job_set_options(job, &opt);
7341 free_job_options(&opt);
7342}
7343
7344/*
7345 * "job_start()" function
7346 */
7347 static void
7348f_job_start(typval_T *argvars, typval_T *rettv)
7349{
7350 rettv->v_type = VAR_JOB;
7351 if (check_restricted() || check_secure())
7352 return;
Bram Moolenaar493359e2018-06-12 20:25:52 +02007353 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007354}
7355
7356/*
7357 * "job_status()" function
7358 */
7359 static void
7360f_job_status(typval_T *argvars, typval_T *rettv)
7361{
7362 job_T *job = get_job_arg(&argvars[0]);
7363
7364 if (job != NULL)
7365 {
7366 rettv->v_type = VAR_STRING;
7367 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7368 }
7369}
7370
7371/*
7372 * "job_stop()" function
7373 */
7374 static void
7375f_job_stop(typval_T *argvars, typval_T *rettv)
7376{
7377 job_T *job = get_job_arg(&argvars[0]);
7378
7379 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007380 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007381}
7382#endif
7383
7384/*
7385 * "join()" function
7386 */
7387 static void
7388f_join(typval_T *argvars, typval_T *rettv)
7389{
7390 garray_T ga;
7391 char_u *sep;
7392
7393 if (argvars[0].v_type != VAR_LIST)
7394 {
7395 EMSG(_(e_listreq));
7396 return;
7397 }
7398 if (argvars[0].vval.v_list == NULL)
7399 return;
7400 if (argvars[1].v_type == VAR_UNKNOWN)
7401 sep = (char_u *)" ";
7402 else
7403 sep = get_tv_string_chk(&argvars[1]);
7404
7405 rettv->v_type = VAR_STRING;
7406
7407 if (sep != NULL)
7408 {
7409 ga_init2(&ga, (int)sizeof(char), 80);
7410 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7411 ga_append(&ga, NUL);
7412 rettv->vval.v_string = (char_u *)ga.ga_data;
7413 }
7414 else
7415 rettv->vval.v_string = NULL;
7416}
7417
7418/*
7419 * "js_decode()" function
7420 */
7421 static void
7422f_js_decode(typval_T *argvars, typval_T *rettv)
7423{
7424 js_read_T reader;
7425
7426 reader.js_buf = get_tv_string(&argvars[0]);
7427 reader.js_fill = NULL;
7428 reader.js_used = 0;
7429 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
7430 EMSG(_(e_invarg));
7431}
7432
7433/*
7434 * "js_encode()" function
7435 */
7436 static void
7437f_js_encode(typval_T *argvars, typval_T *rettv)
7438{
7439 rettv->v_type = VAR_STRING;
7440 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7441}
7442
7443/*
7444 * "json_decode()" function
7445 */
7446 static void
7447f_json_decode(typval_T *argvars, typval_T *rettv)
7448{
7449 js_read_T reader;
7450
7451 reader.js_buf = get_tv_string(&argvars[0]);
7452 reader.js_fill = NULL;
7453 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007454 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007455}
7456
7457/*
7458 * "json_encode()" function
7459 */
7460 static void
7461f_json_encode(typval_T *argvars, typval_T *rettv)
7462{
7463 rettv->v_type = VAR_STRING;
7464 rettv->vval.v_string = json_encode(&argvars[0], 0);
7465}
7466
7467/*
7468 * "keys()" function
7469 */
7470 static void
7471f_keys(typval_T *argvars, typval_T *rettv)
7472{
7473 dict_list(argvars, rettv, 0);
7474}
7475
7476/*
7477 * "last_buffer_nr()" function.
7478 */
7479 static void
7480f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7481{
7482 int n = 0;
7483 buf_T *buf;
7484
Bram Moolenaar29323592016-07-24 22:04:11 +02007485 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007486 if (n < buf->b_fnum)
7487 n = buf->b_fnum;
7488
7489 rettv->vval.v_number = n;
7490}
7491
7492/*
7493 * "len()" function
7494 */
7495 static void
7496f_len(typval_T *argvars, typval_T *rettv)
7497{
7498 switch (argvars[0].v_type)
7499 {
7500 case VAR_STRING:
7501 case VAR_NUMBER:
7502 rettv->vval.v_number = (varnumber_T)STRLEN(
7503 get_tv_string(&argvars[0]));
7504 break;
7505 case VAR_LIST:
7506 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7507 break;
7508 case VAR_DICT:
7509 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7510 break;
7511 case VAR_UNKNOWN:
7512 case VAR_SPECIAL:
7513 case VAR_FLOAT:
7514 case VAR_FUNC:
7515 case VAR_PARTIAL:
7516 case VAR_JOB:
7517 case VAR_CHANNEL:
7518 EMSG(_("E701: Invalid type for len()"));
7519 break;
7520 }
7521}
7522
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007523 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007524libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007525{
7526#ifdef FEAT_LIBCALL
7527 char_u *string_in;
7528 char_u **string_result;
7529 int nr_result;
7530#endif
7531
7532 rettv->v_type = type;
7533 if (type != VAR_NUMBER)
7534 rettv->vval.v_string = NULL;
7535
7536 if (check_restricted() || check_secure())
7537 return;
7538
7539#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007540 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007541 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7542 {
7543 string_in = NULL;
7544 if (argvars[2].v_type == VAR_STRING)
7545 string_in = argvars[2].vval.v_string;
7546 if (type == VAR_NUMBER)
7547 string_result = NULL;
7548 else
7549 string_result = &rettv->vval.v_string;
7550 if (mch_libcall(argvars[0].vval.v_string,
7551 argvars[1].vval.v_string,
7552 string_in,
7553 argvars[2].vval.v_number,
7554 string_result,
7555 &nr_result) == OK
7556 && type == VAR_NUMBER)
7557 rettv->vval.v_number = nr_result;
7558 }
7559#endif
7560}
7561
7562/*
7563 * "libcall()" function
7564 */
7565 static void
7566f_libcall(typval_T *argvars, typval_T *rettv)
7567{
7568 libcall_common(argvars, rettv, VAR_STRING);
7569}
7570
7571/*
7572 * "libcallnr()" function
7573 */
7574 static void
7575f_libcallnr(typval_T *argvars, typval_T *rettv)
7576{
7577 libcall_common(argvars, rettv, VAR_NUMBER);
7578}
7579
7580/*
7581 * "line(string)" function
7582 */
7583 static void
7584f_line(typval_T *argvars, typval_T *rettv)
7585{
7586 linenr_T lnum = 0;
7587 pos_T *fp;
7588 int fnum;
7589
7590 fp = var2fpos(&argvars[0], TRUE, &fnum);
7591 if (fp != NULL)
7592 lnum = fp->lnum;
7593 rettv->vval.v_number = lnum;
7594}
7595
7596/*
7597 * "line2byte(lnum)" function
7598 */
7599 static void
7600f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7601{
7602#ifndef FEAT_BYTEOFF
7603 rettv->vval.v_number = -1;
7604#else
7605 linenr_T lnum;
7606
7607 lnum = get_tv_lnum(argvars);
7608 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7609 rettv->vval.v_number = -1;
7610 else
7611 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7612 if (rettv->vval.v_number >= 0)
7613 ++rettv->vval.v_number;
7614#endif
7615}
7616
7617/*
7618 * "lispindent(lnum)" function
7619 */
7620 static void
7621f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7622{
7623#ifdef FEAT_LISP
7624 pos_T pos;
7625 linenr_T lnum;
7626
7627 pos = curwin->w_cursor;
7628 lnum = get_tv_lnum(argvars);
7629 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7630 {
7631 curwin->w_cursor.lnum = lnum;
7632 rettv->vval.v_number = get_lisp_indent();
7633 curwin->w_cursor = pos;
7634 }
7635 else
7636#endif
7637 rettv->vval.v_number = -1;
7638}
7639
7640/*
7641 * "localtime()" function
7642 */
7643 static void
7644f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7645{
7646 rettv->vval.v_number = (varnumber_T)time(NULL);
7647}
7648
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007649 static void
7650get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7651{
7652 char_u *keys;
7653 char_u *which;
7654 char_u buf[NUMBUFLEN];
7655 char_u *keys_buf = NULL;
7656 char_u *rhs;
7657 int mode;
7658 int abbr = FALSE;
7659 int get_dict = FALSE;
7660 mapblock_T *mp;
7661 int buffer_local;
7662
7663 /* return empty string for failure */
7664 rettv->v_type = VAR_STRING;
7665 rettv->vval.v_string = NULL;
7666
7667 keys = get_tv_string(&argvars[0]);
7668 if (*keys == NUL)
7669 return;
7670
7671 if (argvars[1].v_type != VAR_UNKNOWN)
7672 {
7673 which = get_tv_string_buf_chk(&argvars[1], buf);
7674 if (argvars[2].v_type != VAR_UNKNOWN)
7675 {
7676 abbr = (int)get_tv_number(&argvars[2]);
7677 if (argvars[3].v_type != VAR_UNKNOWN)
7678 get_dict = (int)get_tv_number(&argvars[3]);
7679 }
7680 }
7681 else
7682 which = (char_u *)"";
7683 if (which == NULL)
7684 return;
7685
7686 mode = get_map_mode(&which, 0);
7687
7688 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7689 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7690 vim_free(keys_buf);
7691
7692 if (!get_dict)
7693 {
7694 /* Return a string. */
7695 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02007696 {
7697 if (*rhs == NUL)
7698 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
7699 else
7700 rettv->vval.v_string = str2special_save(rhs, FALSE);
7701 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007702
7703 }
7704 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7705 {
7706 /* Return a dictionary. */
7707 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7708 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7709 dict_T *dict = rettv->vval.v_dict;
7710
Bram Moolenaare0be1672018-07-08 16:50:37 +02007711 dict_add_string(dict, "lhs", lhs);
7712 dict_add_string(dict, "rhs", mp->m_orig_str);
7713 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
7714 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
7715 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007716 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
7717 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02007718 dict_add_number(dict, "buffer", (long)buffer_local);
7719 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
7720 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007721
7722 vim_free(lhs);
7723 vim_free(mapmode);
7724 }
7725}
7726
7727#ifdef FEAT_FLOAT
7728/*
7729 * "log()" function
7730 */
7731 static void
7732f_log(typval_T *argvars, typval_T *rettv)
7733{
7734 float_T f = 0.0;
7735
7736 rettv->v_type = VAR_FLOAT;
7737 if (get_float_arg(argvars, &f) == OK)
7738 rettv->vval.v_float = log(f);
7739 else
7740 rettv->vval.v_float = 0.0;
7741}
7742
7743/*
7744 * "log10()" function
7745 */
7746 static void
7747f_log10(typval_T *argvars, typval_T *rettv)
7748{
7749 float_T f = 0.0;
7750
7751 rettv->v_type = VAR_FLOAT;
7752 if (get_float_arg(argvars, &f) == OK)
7753 rettv->vval.v_float = log10(f);
7754 else
7755 rettv->vval.v_float = 0.0;
7756}
7757#endif
7758
7759#ifdef FEAT_LUA
7760/*
7761 * "luaeval()" function
7762 */
7763 static void
7764f_luaeval(typval_T *argvars, typval_T *rettv)
7765{
7766 char_u *str;
7767 char_u buf[NUMBUFLEN];
7768
7769 str = get_tv_string_buf(&argvars[0], buf);
7770 do_luaeval(str, argvars + 1, rettv);
7771}
7772#endif
7773
7774/*
7775 * "map()" function
7776 */
7777 static void
7778f_map(typval_T *argvars, typval_T *rettv)
7779{
7780 filter_map(argvars, rettv, TRUE);
7781}
7782
7783/*
7784 * "maparg()" function
7785 */
7786 static void
7787f_maparg(typval_T *argvars, typval_T *rettv)
7788{
7789 get_maparg(argvars, rettv, TRUE);
7790}
7791
7792/*
7793 * "mapcheck()" function
7794 */
7795 static void
7796f_mapcheck(typval_T *argvars, typval_T *rettv)
7797{
7798 get_maparg(argvars, rettv, FALSE);
7799}
7800
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007801typedef enum
7802{
7803 MATCH_END, /* matchend() */
7804 MATCH_MATCH, /* match() */
7805 MATCH_STR, /* matchstr() */
7806 MATCH_LIST, /* matchlist() */
7807 MATCH_POS /* matchstrpos() */
7808} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007809
7810 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007811find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007812{
7813 char_u *str = NULL;
7814 long len = 0;
7815 char_u *expr = NULL;
7816 char_u *pat;
7817 regmatch_T regmatch;
7818 char_u patbuf[NUMBUFLEN];
7819 char_u strbuf[NUMBUFLEN];
7820 char_u *save_cpo;
7821 long start = 0;
7822 long nth = 1;
7823 colnr_T startcol = 0;
7824 int match = 0;
7825 list_T *l = NULL;
7826 listitem_T *li = NULL;
7827 long idx = 0;
7828 char_u *tofree = NULL;
7829
7830 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7831 save_cpo = p_cpo;
7832 p_cpo = (char_u *)"";
7833
7834 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007835 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007836 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007837 /* type MATCH_LIST: return empty list when there are no matches.
7838 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007839 if (rettv_list_alloc(rettv) == FAIL)
7840 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007841 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007842 && (list_append_string(rettv->vval.v_list,
7843 (char_u *)"", 0) == FAIL
7844 || list_append_number(rettv->vval.v_list,
7845 (varnumber_T)-1) == FAIL
7846 || list_append_number(rettv->vval.v_list,
7847 (varnumber_T)-1) == FAIL
7848 || list_append_number(rettv->vval.v_list,
7849 (varnumber_T)-1) == FAIL))
7850 {
7851 list_free(rettv->vval.v_list);
7852 rettv->vval.v_list = NULL;
7853 goto theend;
7854 }
7855 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007856 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007857 {
7858 rettv->v_type = VAR_STRING;
7859 rettv->vval.v_string = NULL;
7860 }
7861
7862 if (argvars[0].v_type == VAR_LIST)
7863 {
7864 if ((l = argvars[0].vval.v_list) == NULL)
7865 goto theend;
7866 li = l->lv_first;
7867 }
7868 else
7869 {
7870 expr = str = get_tv_string(&argvars[0]);
7871 len = (long)STRLEN(str);
7872 }
7873
7874 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
7875 if (pat == NULL)
7876 goto theend;
7877
7878 if (argvars[2].v_type != VAR_UNKNOWN)
7879 {
7880 int error = FALSE;
7881
7882 start = (long)get_tv_number_chk(&argvars[2], &error);
7883 if (error)
7884 goto theend;
7885 if (l != NULL)
7886 {
7887 li = list_find(l, start);
7888 if (li == NULL)
7889 goto theend;
7890 idx = l->lv_idx; /* use the cached index */
7891 }
7892 else
7893 {
7894 if (start < 0)
7895 start = 0;
7896 if (start > len)
7897 goto theend;
7898 /* When "count" argument is there ignore matches before "start",
7899 * otherwise skip part of the string. Differs when pattern is "^"
7900 * or "\<". */
7901 if (argvars[3].v_type != VAR_UNKNOWN)
7902 startcol = start;
7903 else
7904 {
7905 str += start;
7906 len -= start;
7907 }
7908 }
7909
7910 if (argvars[3].v_type != VAR_UNKNOWN)
7911 nth = (long)get_tv_number_chk(&argvars[3], &error);
7912 if (error)
7913 goto theend;
7914 }
7915
7916 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7917 if (regmatch.regprog != NULL)
7918 {
7919 regmatch.rm_ic = p_ic;
7920
7921 for (;;)
7922 {
7923 if (l != NULL)
7924 {
7925 if (li == NULL)
7926 {
7927 match = FALSE;
7928 break;
7929 }
7930 vim_free(tofree);
7931 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
7932 if (str == NULL)
7933 break;
7934 }
7935
7936 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
7937
7938 if (match && --nth <= 0)
7939 break;
7940 if (l == NULL && !match)
7941 break;
7942
7943 /* Advance to just after the match. */
7944 if (l != NULL)
7945 {
7946 li = li->li_next;
7947 ++idx;
7948 }
7949 else
7950 {
7951#ifdef FEAT_MBYTE
7952 startcol = (colnr_T)(regmatch.startp[0]
7953 + (*mb_ptr2len)(regmatch.startp[0]) - str);
7954#else
7955 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
7956#endif
7957 if (startcol > (colnr_T)len
7958 || str + startcol <= regmatch.startp[0])
7959 {
7960 match = FALSE;
7961 break;
7962 }
7963 }
7964 }
7965
7966 if (match)
7967 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007968 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007969 {
7970 listitem_T *li1 = rettv->vval.v_list->lv_first;
7971 listitem_T *li2 = li1->li_next;
7972 listitem_T *li3 = li2->li_next;
7973 listitem_T *li4 = li3->li_next;
7974
7975 vim_free(li1->li_tv.vval.v_string);
7976 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
7977 (int)(regmatch.endp[0] - regmatch.startp[0]));
7978 li3->li_tv.vval.v_number =
7979 (varnumber_T)(regmatch.startp[0] - expr);
7980 li4->li_tv.vval.v_number =
7981 (varnumber_T)(regmatch.endp[0] - expr);
7982 if (l != NULL)
7983 li2->li_tv.vval.v_number = (varnumber_T)idx;
7984 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007985 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007986 {
7987 int i;
7988
7989 /* return list with matched string and submatches */
7990 for (i = 0; i < NSUBEXP; ++i)
7991 {
7992 if (regmatch.endp[i] == NULL)
7993 {
7994 if (list_append_string(rettv->vval.v_list,
7995 (char_u *)"", 0) == FAIL)
7996 break;
7997 }
7998 else if (list_append_string(rettv->vval.v_list,
7999 regmatch.startp[i],
8000 (int)(regmatch.endp[i] - regmatch.startp[i]))
8001 == FAIL)
8002 break;
8003 }
8004 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008005 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008006 {
8007 /* return matched string */
8008 if (l != NULL)
8009 copy_tv(&li->li_tv, rettv);
8010 else
8011 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
8012 (int)(regmatch.endp[0] - regmatch.startp[0]));
8013 }
8014 else if (l != NULL)
8015 rettv->vval.v_number = idx;
8016 else
8017 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008018 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008019 rettv->vval.v_number =
8020 (varnumber_T)(regmatch.startp[0] - str);
8021 else
8022 rettv->vval.v_number =
8023 (varnumber_T)(regmatch.endp[0] - str);
8024 rettv->vval.v_number += (varnumber_T)(str - expr);
8025 }
8026 }
8027 vim_regfree(regmatch.regprog);
8028 }
8029
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008030theend:
8031 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008032 /* matchstrpos() without a list: drop the second item. */
8033 listitem_remove(rettv->vval.v_list,
8034 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008035 vim_free(tofree);
8036 p_cpo = save_cpo;
8037}
8038
8039/*
8040 * "match()" function
8041 */
8042 static void
8043f_match(typval_T *argvars, typval_T *rettv)
8044{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008045 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008046}
8047
Bram Moolenaar95e51472018-07-28 16:55:56 +02008048#ifdef FEAT_SEARCH_EXTRA
8049 static int
8050matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
8051{
8052 dictitem_T *di;
8053
8054 if (tv->v_type != VAR_DICT)
8055 {
8056 EMSG(_(e_dictreq));
8057 return FAIL;
8058 }
8059
8060 if (dict_find(tv->vval.v_dict, (char_u *)"conceal", -1) != NULL)
8061 *conceal_char = get_dict_string(tv->vval.v_dict,
8062 (char_u *)"conceal", FALSE);
8063
8064 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
8065 {
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02008066 *win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008067 if (*win == NULL)
8068 {
8069 EMSG(_("E957: Invalid window number"));
8070 return FAIL;
8071 }
8072 }
8073
8074 return OK;
8075}
8076#endif
8077
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008078/*
8079 * "matchadd()" function
8080 */
8081 static void
8082f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8083{
8084#ifdef FEAT_SEARCH_EXTRA
8085 char_u buf[NUMBUFLEN];
8086 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
8087 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
8088 int prio = 10; /* default priority */
8089 int id = -1;
8090 int error = FALSE;
8091 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008092 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008093
8094 rettv->vval.v_number = -1;
8095
8096 if (grp == NULL || pat == NULL)
8097 return;
8098 if (argvars[2].v_type != VAR_UNKNOWN)
8099 {
8100 prio = (int)get_tv_number_chk(&argvars[2], &error);
8101 if (argvars[3].v_type != VAR_UNKNOWN)
8102 {
8103 id = (int)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008104 if (argvars[4].v_type != VAR_UNKNOWN
8105 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8106 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008107 }
8108 }
8109 if (error == TRUE)
8110 return;
8111 if (id >= 1 && id <= 3)
8112 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02008113 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008114 return;
8115 }
8116
Bram Moolenaar95e51472018-07-28 16:55:56 +02008117 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008118 conceal_char);
8119#endif
8120}
8121
8122/*
8123 * "matchaddpos()" function
8124 */
8125 static void
8126f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8127{
8128#ifdef FEAT_SEARCH_EXTRA
8129 char_u buf[NUMBUFLEN];
8130 char_u *group;
8131 int prio = 10;
8132 int id = -1;
8133 int error = FALSE;
8134 list_T *l;
8135 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008136 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008137
8138 rettv->vval.v_number = -1;
8139
8140 group = get_tv_string_buf_chk(&argvars[0], buf);
8141 if (group == NULL)
8142 return;
8143
8144 if (argvars[1].v_type != VAR_LIST)
8145 {
8146 EMSG2(_(e_listarg), "matchaddpos()");
8147 return;
8148 }
8149 l = argvars[1].vval.v_list;
8150 if (l == NULL)
8151 return;
8152
8153 if (argvars[2].v_type != VAR_UNKNOWN)
8154 {
8155 prio = (int)get_tv_number_chk(&argvars[2], &error);
8156 if (argvars[3].v_type != VAR_UNKNOWN)
8157 {
8158 id = (int)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008159
8160 if (argvars[4].v_type != VAR_UNKNOWN
8161 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8162 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008163 }
8164 }
8165 if (error == TRUE)
8166 return;
8167
8168 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8169 if (id == 1 || id == 2)
8170 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02008171 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008172 return;
8173 }
8174
Bram Moolenaar95e51472018-07-28 16:55:56 +02008175 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008176 conceal_char);
8177#endif
8178}
8179
8180/*
8181 * "matcharg()" function
8182 */
8183 static void
8184f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8185{
8186 if (rettv_list_alloc(rettv) == OK)
8187 {
8188#ifdef FEAT_SEARCH_EXTRA
8189 int id = (int)get_tv_number(&argvars[0]);
8190 matchitem_T *m;
8191
8192 if (id >= 1 && id <= 3)
8193 {
8194 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8195 {
8196 list_append_string(rettv->vval.v_list,
8197 syn_id2name(m->hlg_id), -1);
8198 list_append_string(rettv->vval.v_list, m->pattern, -1);
8199 }
8200 else
8201 {
8202 list_append_string(rettv->vval.v_list, NULL, -1);
8203 list_append_string(rettv->vval.v_list, NULL, -1);
8204 }
8205 }
8206#endif
8207 }
8208}
8209
8210/*
8211 * "matchdelete()" function
8212 */
8213 static void
8214f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8215{
8216#ifdef FEAT_SEARCH_EXTRA
8217 rettv->vval.v_number = match_delete(curwin,
8218 (int)get_tv_number(&argvars[0]), TRUE);
8219#endif
8220}
8221
8222/*
8223 * "matchend()" function
8224 */
8225 static void
8226f_matchend(typval_T *argvars, typval_T *rettv)
8227{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008228 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008229}
8230
8231/*
8232 * "matchlist()" function
8233 */
8234 static void
8235f_matchlist(typval_T *argvars, typval_T *rettv)
8236{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008237 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008238}
8239
8240/*
8241 * "matchstr()" function
8242 */
8243 static void
8244f_matchstr(typval_T *argvars, typval_T *rettv)
8245{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008246 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008247}
8248
8249/*
8250 * "matchstrpos()" function
8251 */
8252 static void
8253f_matchstrpos(typval_T *argvars, typval_T *rettv)
8254{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008255 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008256}
8257
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008258 static void
8259max_min(typval_T *argvars, typval_T *rettv, int domax)
8260{
8261 varnumber_T n = 0;
8262 varnumber_T i;
8263 int error = FALSE;
8264
8265 if (argvars[0].v_type == VAR_LIST)
8266 {
8267 list_T *l;
8268 listitem_T *li;
8269
8270 l = argvars[0].vval.v_list;
8271 if (l != NULL)
8272 {
8273 li = l->lv_first;
8274 if (li != NULL)
8275 {
8276 n = get_tv_number_chk(&li->li_tv, &error);
8277 for (;;)
8278 {
8279 li = li->li_next;
8280 if (li == NULL)
8281 break;
8282 i = get_tv_number_chk(&li->li_tv, &error);
8283 if (domax ? i > n : i < n)
8284 n = i;
8285 }
8286 }
8287 }
8288 }
8289 else if (argvars[0].v_type == VAR_DICT)
8290 {
8291 dict_T *d;
8292 int first = TRUE;
8293 hashitem_T *hi;
8294 int todo;
8295
8296 d = argvars[0].vval.v_dict;
8297 if (d != NULL)
8298 {
8299 todo = (int)d->dv_hashtab.ht_used;
8300 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8301 {
8302 if (!HASHITEM_EMPTY(hi))
8303 {
8304 --todo;
8305 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
8306 if (first)
8307 {
8308 n = i;
8309 first = FALSE;
8310 }
8311 else if (domax ? i > n : i < n)
8312 n = i;
8313 }
8314 }
8315 }
8316 }
8317 else
Bram Moolenaar26b84332016-09-04 21:42:36 +02008318 EMSG2(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008319 rettv->vval.v_number = error ? 0 : n;
8320}
8321
8322/*
8323 * "max()" function
8324 */
8325 static void
8326f_max(typval_T *argvars, typval_T *rettv)
8327{
8328 max_min(argvars, rettv, TRUE);
8329}
8330
8331/*
8332 * "min()" function
8333 */
8334 static void
8335f_min(typval_T *argvars, typval_T *rettv)
8336{
8337 max_min(argvars, rettv, FALSE);
8338}
8339
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008340/*
8341 * Create the directory in which "dir" is located, and higher levels when
8342 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008343 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008344 */
8345 static int
8346mkdir_recurse(char_u *dir, int prot)
8347{
8348 char_u *p;
8349 char_u *updir;
8350 int r = FAIL;
8351
8352 /* Get end of directory name in "dir".
8353 * We're done when it's "/" or "c:/". */
8354 p = gettail_sep(dir);
8355 if (p <= get_past_head(dir))
8356 return OK;
8357
8358 /* If the directory exists we're done. Otherwise: create it.*/
8359 updir = vim_strnsave(dir, (int)(p - dir));
8360 if (updir == NULL)
8361 return FAIL;
8362 if (mch_isdir(updir))
8363 r = OK;
8364 else if (mkdir_recurse(updir, prot) == OK)
8365 r = vim_mkdir_emsg(updir, prot);
8366 vim_free(updir);
8367 return r;
8368}
8369
8370#ifdef vim_mkdir
8371/*
8372 * "mkdir()" function
8373 */
8374 static void
8375f_mkdir(typval_T *argvars, typval_T *rettv)
8376{
8377 char_u *dir;
8378 char_u buf[NUMBUFLEN];
8379 int prot = 0755;
8380
8381 rettv->vval.v_number = FAIL;
8382 if (check_restricted() || check_secure())
8383 return;
8384
8385 dir = get_tv_string_buf(&argvars[0], buf);
8386 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008387 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008388
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008389 if (*gettail(dir) == NUL)
8390 /* remove trailing slashes */
8391 *gettail_sep(dir) = NUL;
8392
8393 if (argvars[1].v_type != VAR_UNKNOWN)
8394 {
8395 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008396 {
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008397 prot = (int)get_tv_number_chk(&argvars[2], NULL);
8398 if (prot == -1)
8399 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008400 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008401 if (STRCMP(get_tv_string(&argvars[1]), "p") == 0)
8402 {
8403 if (mch_isdir(dir))
8404 {
8405 /* With the "p" flag it's OK if the dir already exists. */
8406 rettv->vval.v_number = OK;
8407 return;
8408 }
8409 mkdir_recurse(dir, prot);
8410 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008411 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008412 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008413}
8414#endif
8415
8416/*
8417 * "mode()" function
8418 */
8419 static void
8420f_mode(typval_T *argvars, typval_T *rettv)
8421{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008422 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008423
Bram Moolenaar612cc382018-07-29 15:34:26 +02008424 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008425
8426 if (time_for_testing == 93784)
8427 {
8428 /* Testing the two-character code. */
8429 buf[0] = 'x';
8430 buf[1] = '!';
8431 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008432#ifdef FEAT_TERMINAL
8433 else if (term_use_loop())
8434 buf[0] = 't';
8435#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008436 else if (VIsual_active)
8437 {
8438 if (VIsual_select)
8439 buf[0] = VIsual_mode + 's' - 'v';
8440 else
8441 buf[0] = VIsual_mode;
8442 }
8443 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8444 || State == CONFIRM)
8445 {
8446 buf[0] = 'r';
8447 if (State == ASKMORE)
8448 buf[1] = 'm';
8449 else if (State == CONFIRM)
8450 buf[1] = '?';
8451 }
8452 else if (State == EXTERNCMD)
8453 buf[0] = '!';
8454 else if (State & INSERT)
8455 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008456 if (State & VREPLACE_FLAG)
8457 {
8458 buf[0] = 'R';
8459 buf[1] = 'v';
8460 }
8461 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008462 {
8463 if (State & REPLACE_FLAG)
8464 buf[0] = 'R';
8465 else
8466 buf[0] = 'i';
8467#ifdef FEAT_INS_EXPAND
8468 if (ins_compl_active())
8469 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008470 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008471 buf[1] = 'x';
8472#endif
8473 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008474 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008475 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008476 {
8477 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008478 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008479 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008480 else if (exmode_active == EXMODE_NORMAL)
8481 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008482 }
8483 else
8484 {
8485 buf[0] = 'n';
8486 if (finish_op)
8487 buf[1] = 'o';
Bram Moolenaar612cc382018-07-29 15:34:26 +02008488 else if (restart_edit == 'I' || restart_edit == 'R'
8489 || restart_edit == 'V')
8490 {
8491 buf[1] = 'i';
8492 buf[2] = restart_edit;
8493 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008494 }
8495
8496 /* Clear out the minor mode when the argument is not a non-zero number or
8497 * non-empty string. */
8498 if (!non_zero_arg(&argvars[0]))
8499 buf[1] = NUL;
8500
8501 rettv->vval.v_string = vim_strsave(buf);
8502 rettv->v_type = VAR_STRING;
8503}
8504
8505#if defined(FEAT_MZSCHEME) || defined(PROTO)
8506/*
8507 * "mzeval()" function
8508 */
8509 static void
8510f_mzeval(typval_T *argvars, typval_T *rettv)
8511{
8512 char_u *str;
8513 char_u buf[NUMBUFLEN];
8514
8515 str = get_tv_string_buf(&argvars[0], buf);
8516 do_mzeval(str, rettv);
8517}
8518
8519 void
8520mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8521{
8522 typval_T argvars[3];
8523
8524 argvars[0].v_type = VAR_STRING;
8525 argvars[0].vval.v_string = name;
8526 copy_tv(args, &argvars[1]);
8527 argvars[2].v_type = VAR_UNKNOWN;
8528 f_call(argvars, rettv);
8529 clear_tv(&argvars[1]);
8530}
8531#endif
8532
8533/*
8534 * "nextnonblank()" function
8535 */
8536 static void
8537f_nextnonblank(typval_T *argvars, typval_T *rettv)
8538{
8539 linenr_T lnum;
8540
8541 for (lnum = get_tv_lnum(argvars); ; ++lnum)
8542 {
8543 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8544 {
8545 lnum = 0;
8546 break;
8547 }
8548 if (*skipwhite(ml_get(lnum)) != NUL)
8549 break;
8550 }
8551 rettv->vval.v_number = lnum;
8552}
8553
8554/*
8555 * "nr2char()" function
8556 */
8557 static void
8558f_nr2char(typval_T *argvars, typval_T *rettv)
8559{
8560 char_u buf[NUMBUFLEN];
8561
8562#ifdef FEAT_MBYTE
8563 if (has_mbyte)
8564 {
8565 int utf8 = 0;
8566
8567 if (argvars[1].v_type != VAR_UNKNOWN)
8568 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
8569 if (utf8)
8570 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8571 else
8572 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8573 }
8574 else
8575#endif
8576 {
8577 buf[0] = (char_u)get_tv_number(&argvars[0]);
8578 buf[1] = NUL;
8579 }
8580 rettv->v_type = VAR_STRING;
8581 rettv->vval.v_string = vim_strsave(buf);
8582}
8583
8584/*
8585 * "or(expr, expr)" function
8586 */
8587 static void
8588f_or(typval_T *argvars, typval_T *rettv)
8589{
8590 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8591 | get_tv_number_chk(&argvars[1], NULL);
8592}
8593
8594/*
8595 * "pathshorten()" function
8596 */
8597 static void
8598f_pathshorten(typval_T *argvars, typval_T *rettv)
8599{
8600 char_u *p;
8601
8602 rettv->v_type = VAR_STRING;
8603 p = get_tv_string_chk(&argvars[0]);
8604 if (p == NULL)
8605 rettv->vval.v_string = NULL;
8606 else
8607 {
8608 p = vim_strsave(p);
8609 rettv->vval.v_string = p;
8610 if (p != NULL)
8611 shorten_dir(p);
8612 }
8613}
8614
8615#ifdef FEAT_PERL
8616/*
8617 * "perleval()" function
8618 */
8619 static void
8620f_perleval(typval_T *argvars, typval_T *rettv)
8621{
8622 char_u *str;
8623 char_u buf[NUMBUFLEN];
8624
8625 str = get_tv_string_buf(&argvars[0], buf);
8626 do_perleval(str, rettv);
8627}
8628#endif
8629
8630#ifdef FEAT_FLOAT
8631/*
8632 * "pow()" function
8633 */
8634 static void
8635f_pow(typval_T *argvars, typval_T *rettv)
8636{
8637 float_T fx = 0.0, fy = 0.0;
8638
8639 rettv->v_type = VAR_FLOAT;
8640 if (get_float_arg(argvars, &fx) == OK
8641 && get_float_arg(&argvars[1], &fy) == OK)
8642 rettv->vval.v_float = pow(fx, fy);
8643 else
8644 rettv->vval.v_float = 0.0;
8645}
8646#endif
8647
8648/*
8649 * "prevnonblank()" function
8650 */
8651 static void
8652f_prevnonblank(typval_T *argvars, typval_T *rettv)
8653{
8654 linenr_T lnum;
8655
8656 lnum = get_tv_lnum(argvars);
8657 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8658 lnum = 0;
8659 else
8660 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8661 --lnum;
8662 rettv->vval.v_number = lnum;
8663}
8664
8665/* This dummy va_list is here because:
8666 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8667 * - locally in the function results in a "used before set" warning
8668 * - using va_start() to initialize it gives "function with fixed args" error */
8669static va_list ap;
8670
8671/*
8672 * "printf()" function
8673 */
8674 static void
8675f_printf(typval_T *argvars, typval_T *rettv)
8676{
8677 char_u buf[NUMBUFLEN];
8678 int len;
8679 char_u *s;
8680 int saved_did_emsg = did_emsg;
8681 char *fmt;
8682
8683 rettv->v_type = VAR_STRING;
8684 rettv->vval.v_string = NULL;
8685
8686 /* Get the required length, allocate the buffer and do it for real. */
8687 did_emsg = FALSE;
8688 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008689 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008690 if (!did_emsg)
8691 {
8692 s = alloc(len + 1);
8693 if (s != NULL)
8694 {
8695 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008696 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8697 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008698 }
8699 }
8700 did_emsg |= saved_did_emsg;
8701}
8702
Bram Moolenaarf2732452018-06-03 14:47:35 +02008703#ifdef FEAT_JOB_CHANNEL
8704/*
8705 * "prompt_setcallback({buffer}, {callback})" function
8706 */
8707 static void
8708f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
8709{
8710 buf_T *buf;
8711 char_u *callback;
8712 partial_T *partial;
8713
8714 if (check_secure())
8715 return;
8716 buf = get_buf_tv(&argvars[0], FALSE);
8717 if (buf == NULL)
8718 return;
8719
8720 callback = get_callback(&argvars[1], &partial);
8721 if (callback == NULL)
8722 return;
8723
8724 free_callback(buf->b_prompt_callback, buf->b_prompt_partial);
8725 if (partial == NULL)
8726 buf->b_prompt_callback = vim_strsave(callback);
8727 else
8728 /* pointer into the partial */
8729 buf->b_prompt_callback = callback;
8730 buf->b_prompt_partial = partial;
8731}
8732
8733/*
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02008734 * "prompt_setinterrupt({buffer}, {callback})" function
8735 */
8736 static void
8737f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
8738{
8739 buf_T *buf;
8740 char_u *callback;
8741 partial_T *partial;
8742
8743 if (check_secure())
8744 return;
8745 buf = get_buf_tv(&argvars[0], FALSE);
8746 if (buf == NULL)
8747 return;
8748
8749 callback = get_callback(&argvars[1], &partial);
8750 if (callback == NULL)
8751 return;
8752
8753 free_callback(buf->b_prompt_interrupt, buf->b_prompt_int_partial);
8754 if (partial == NULL)
8755 buf->b_prompt_interrupt = vim_strsave(callback);
8756 else
8757 /* pointer into the partial */
8758 buf->b_prompt_interrupt = callback;
8759 buf->b_prompt_int_partial = partial;
8760}
8761
8762/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02008763 * "prompt_setprompt({buffer}, {text})" function
8764 */
8765 static void
8766f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
8767{
8768 buf_T *buf;
8769 char_u *text;
8770
8771 if (check_secure())
8772 return;
8773 buf = get_buf_tv(&argvars[0], FALSE);
8774 if (buf == NULL)
8775 return;
8776
8777 text = get_tv_string(&argvars[1]);
8778 vim_free(buf->b_prompt_text);
8779 buf->b_prompt_text = vim_strsave(text);
8780}
8781#endif
8782
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008783/*
8784 * "pumvisible()" function
8785 */
8786 static void
8787f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8788{
8789#ifdef FEAT_INS_EXPAND
8790 if (pum_visible())
8791 rettv->vval.v_number = 1;
8792#endif
8793}
8794
8795#ifdef FEAT_PYTHON3
8796/*
8797 * "py3eval()" function
8798 */
8799 static void
8800f_py3eval(typval_T *argvars, typval_T *rettv)
8801{
8802 char_u *str;
8803 char_u buf[NUMBUFLEN];
8804
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008805 if (p_pyx == 0)
8806 p_pyx = 3;
8807
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008808 str = get_tv_string_buf(&argvars[0], buf);
8809 do_py3eval(str, rettv);
8810}
8811#endif
8812
8813#ifdef FEAT_PYTHON
8814/*
8815 * "pyeval()" function
8816 */
8817 static void
8818f_pyeval(typval_T *argvars, typval_T *rettv)
8819{
8820 char_u *str;
8821 char_u buf[NUMBUFLEN];
8822
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008823 if (p_pyx == 0)
8824 p_pyx = 2;
8825
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008826 str = get_tv_string_buf(&argvars[0], buf);
8827 do_pyeval(str, rettv);
8828}
8829#endif
8830
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008831#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8832/*
8833 * "pyxeval()" function
8834 */
8835 static void
8836f_pyxeval(typval_T *argvars, typval_T *rettv)
8837{
8838# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8839 init_pyxversion();
8840 if (p_pyx == 2)
8841 f_pyeval(argvars, rettv);
8842 else
8843 f_py3eval(argvars, rettv);
8844# elif defined(FEAT_PYTHON)
8845 f_pyeval(argvars, rettv);
8846# elif defined(FEAT_PYTHON3)
8847 f_py3eval(argvars, rettv);
8848# endif
8849}
8850#endif
8851
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008852/*
8853 * "range()" function
8854 */
8855 static void
8856f_range(typval_T *argvars, typval_T *rettv)
8857{
8858 varnumber_T start;
8859 varnumber_T end;
8860 varnumber_T stride = 1;
8861 varnumber_T i;
8862 int error = FALSE;
8863
8864 start = get_tv_number_chk(&argvars[0], &error);
8865 if (argvars[1].v_type == VAR_UNKNOWN)
8866 {
8867 end = start - 1;
8868 start = 0;
8869 }
8870 else
8871 {
8872 end = get_tv_number_chk(&argvars[1], &error);
8873 if (argvars[2].v_type != VAR_UNKNOWN)
8874 stride = get_tv_number_chk(&argvars[2], &error);
8875 }
8876
8877 if (error)
8878 return; /* type error; errmsg already given */
8879 if (stride == 0)
8880 EMSG(_("E726: Stride is zero"));
8881 else if (stride > 0 ? end + 1 < start : end - 1 > start)
8882 EMSG(_("E727: Start past end"));
8883 else
8884 {
8885 if (rettv_list_alloc(rettv) == OK)
8886 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8887 if (list_append_number(rettv->vval.v_list,
8888 (varnumber_T)i) == FAIL)
8889 break;
8890 }
8891}
8892
8893/*
8894 * "readfile()" function
8895 */
8896 static void
8897f_readfile(typval_T *argvars, typval_T *rettv)
8898{
8899 int binary = FALSE;
8900 int failed = FALSE;
8901 char_u *fname;
8902 FILE *fd;
8903 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
8904 int io_size = sizeof(buf);
8905 int readlen; /* size of last fread() */
8906 char_u *prev = NULL; /* previously read bytes, if any */
8907 long prevlen = 0; /* length of data in prev */
8908 long prevsize = 0; /* size of prev buffer */
8909 long maxline = MAXLNUM;
8910 long cnt = 0;
8911 char_u *p; /* position in buf */
8912 char_u *start; /* start of current line */
8913
8914 if (argvars[1].v_type != VAR_UNKNOWN)
8915 {
8916 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
8917 binary = TRUE;
8918 if (argvars[2].v_type != VAR_UNKNOWN)
8919 maxline = (long)get_tv_number(&argvars[2]);
8920 }
8921
8922 if (rettv_list_alloc(rettv) == FAIL)
8923 return;
8924
8925 /* Always open the file in binary mode, library functions have a mind of
8926 * their own about CR-LF conversion. */
8927 fname = get_tv_string(&argvars[0]);
8928 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
8929 {
8930 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
8931 return;
8932 }
8933
8934 while (cnt < maxline || maxline < 0)
8935 {
8936 readlen = (int)fread(buf, 1, io_size, fd);
8937
8938 /* This for loop processes what was read, but is also entered at end
8939 * of file so that either:
8940 * - an incomplete line gets written
8941 * - a "binary" file gets an empty line at the end if it ends in a
8942 * newline. */
8943 for (p = buf, start = buf;
8944 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
8945 ++p)
8946 {
8947 if (*p == '\n' || readlen <= 0)
8948 {
8949 listitem_T *li;
8950 char_u *s = NULL;
8951 long_u len = p - start;
8952
8953 /* Finished a line. Remove CRs before NL. */
8954 if (readlen > 0 && !binary)
8955 {
8956 while (len > 0 && start[len - 1] == '\r')
8957 --len;
8958 /* removal may cross back to the "prev" string */
8959 if (len == 0)
8960 while (prevlen > 0 && prev[prevlen - 1] == '\r')
8961 --prevlen;
8962 }
8963 if (prevlen == 0)
8964 s = vim_strnsave(start, (int)len);
8965 else
8966 {
8967 /* Change "prev" buffer to be the right size. This way
8968 * the bytes are only copied once, and very long lines are
8969 * allocated only once. */
8970 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
8971 {
8972 mch_memmove(s + prevlen, start, len);
8973 s[prevlen + len] = NUL;
8974 prev = NULL; /* the list will own the string */
8975 prevlen = prevsize = 0;
8976 }
8977 }
8978 if (s == NULL)
8979 {
8980 do_outofmem_msg((long_u) prevlen + len + 1);
8981 failed = TRUE;
8982 break;
8983 }
8984
8985 if ((li = listitem_alloc()) == NULL)
8986 {
8987 vim_free(s);
8988 failed = TRUE;
8989 break;
8990 }
8991 li->li_tv.v_type = VAR_STRING;
8992 li->li_tv.v_lock = 0;
8993 li->li_tv.vval.v_string = s;
8994 list_append(rettv->vval.v_list, li);
8995
8996 start = p + 1; /* step over newline */
8997 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
8998 break;
8999 }
9000 else if (*p == NUL)
9001 *p = '\n';
9002#ifdef FEAT_MBYTE
9003 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
9004 * when finding the BF and check the previous two bytes. */
9005 else if (*p == 0xbf && enc_utf8 && !binary)
9006 {
9007 /* Find the two bytes before the 0xbf. If p is at buf, or buf
9008 * + 1, these may be in the "prev" string. */
9009 char_u back1 = p >= buf + 1 ? p[-1]
9010 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
9011 char_u back2 = p >= buf + 2 ? p[-2]
9012 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
9013 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
9014
9015 if (back2 == 0xef && back1 == 0xbb)
9016 {
9017 char_u *dest = p - 2;
9018
9019 /* Usually a BOM is at the beginning of a file, and so at
9020 * the beginning of a line; then we can just step over it.
9021 */
9022 if (start == dest)
9023 start = p + 1;
9024 else
9025 {
9026 /* have to shuffle buf to close gap */
9027 int adjust_prevlen = 0;
9028
9029 if (dest < buf)
9030 {
9031 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
9032 dest = buf;
9033 }
9034 if (readlen > p - buf + 1)
9035 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
9036 readlen -= 3 - adjust_prevlen;
9037 prevlen -= adjust_prevlen;
9038 p = dest - 1;
9039 }
9040 }
9041 }
9042#endif
9043 } /* for */
9044
9045 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
9046 break;
9047 if (start < p)
9048 {
9049 /* There's part of a line in buf, store it in "prev". */
9050 if (p - start + prevlen >= prevsize)
9051 {
9052 /* need bigger "prev" buffer */
9053 char_u *newprev;
9054
9055 /* A common use case is ordinary text files and "prev" gets a
9056 * fragment of a line, so the first allocation is made
9057 * small, to avoid repeatedly 'allocing' large and
9058 * 'reallocing' small. */
9059 if (prevsize == 0)
9060 prevsize = (long)(p - start);
9061 else
9062 {
9063 long grow50pc = (prevsize * 3) / 2;
9064 long growmin = (long)((p - start) * 2 + prevlen);
9065 prevsize = grow50pc > growmin ? grow50pc : growmin;
9066 }
9067 newprev = prev == NULL ? alloc(prevsize)
9068 : vim_realloc(prev, prevsize);
9069 if (newprev == NULL)
9070 {
9071 do_outofmem_msg((long_u)prevsize);
9072 failed = TRUE;
9073 break;
9074 }
9075 prev = newprev;
9076 }
9077 /* Add the line part to end of "prev". */
9078 mch_memmove(prev + prevlen, start, p - start);
9079 prevlen += (long)(p - start);
9080 }
9081 } /* while */
9082
9083 /*
9084 * For a negative line count use only the lines at the end of the file,
9085 * free the rest.
9086 */
9087 if (!failed && maxline < 0)
9088 while (cnt > -maxline)
9089 {
9090 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
9091 --cnt;
9092 }
9093
9094 if (failed)
9095 {
9096 list_free(rettv->vval.v_list);
9097 /* readfile doc says an empty list is returned on error */
9098 rettv->vval.v_list = list_alloc();
9099 }
9100
9101 vim_free(prev);
9102 fclose(fd);
9103}
9104
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009105 static void
9106return_register(int regname, typval_T *rettv)
9107{
9108 char_u buf[2] = {0, 0};
9109
9110 buf[0] = (char_u)regname;
9111 rettv->v_type = VAR_STRING;
9112 rettv->vval.v_string = vim_strsave(buf);
9113}
9114
9115/*
9116 * "reg_executing()" function
9117 */
9118 static void
9119f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9120{
9121 return_register(reg_executing, rettv);
9122}
9123
9124/*
9125 * "reg_recording()" function
9126 */
9127 static void
9128f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9129{
9130 return_register(reg_recording, rettv);
9131}
9132
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009133#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009134/*
9135 * Convert a List to proftime_T.
9136 * Return FAIL when there is something wrong.
9137 */
9138 static int
9139list2proftime(typval_T *arg, proftime_T *tm)
9140{
9141 long n1, n2;
9142 int error = FALSE;
9143
9144 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9145 || arg->vval.v_list->lv_len != 2)
9146 return FAIL;
9147 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9148 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
9149# ifdef WIN3264
9150 tm->HighPart = n1;
9151 tm->LowPart = n2;
9152# else
9153 tm->tv_sec = n1;
9154 tm->tv_usec = n2;
9155# endif
9156 return error ? FAIL : OK;
9157}
9158#endif /* FEAT_RELTIME */
9159
9160/*
9161 * "reltime()" function
9162 */
9163 static void
9164f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9165{
9166#ifdef FEAT_RELTIME
9167 proftime_T res;
9168 proftime_T start;
9169
9170 if (argvars[0].v_type == VAR_UNKNOWN)
9171 {
9172 /* No arguments: get current time. */
9173 profile_start(&res);
9174 }
9175 else if (argvars[1].v_type == VAR_UNKNOWN)
9176 {
9177 if (list2proftime(&argvars[0], &res) == FAIL)
9178 return;
9179 profile_end(&res);
9180 }
9181 else
9182 {
9183 /* Two arguments: compute the difference. */
9184 if (list2proftime(&argvars[0], &start) == FAIL
9185 || list2proftime(&argvars[1], &res) == FAIL)
9186 return;
9187 profile_sub(&res, &start);
9188 }
9189
9190 if (rettv_list_alloc(rettv) == OK)
9191 {
9192 long n1, n2;
9193
9194# ifdef WIN3264
9195 n1 = res.HighPart;
9196 n2 = res.LowPart;
9197# else
9198 n1 = res.tv_sec;
9199 n2 = res.tv_usec;
9200# endif
9201 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9202 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9203 }
9204#endif
9205}
9206
9207#ifdef FEAT_FLOAT
9208/*
9209 * "reltimefloat()" function
9210 */
9211 static void
9212f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9213{
9214# ifdef FEAT_RELTIME
9215 proftime_T tm;
9216# endif
9217
9218 rettv->v_type = VAR_FLOAT;
9219 rettv->vval.v_float = 0;
9220# ifdef FEAT_RELTIME
9221 if (list2proftime(&argvars[0], &tm) == OK)
9222 rettv->vval.v_float = profile_float(&tm);
9223# endif
9224}
9225#endif
9226
9227/*
9228 * "reltimestr()" function
9229 */
9230 static void
9231f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9232{
9233#ifdef FEAT_RELTIME
9234 proftime_T tm;
9235#endif
9236
9237 rettv->v_type = VAR_STRING;
9238 rettv->vval.v_string = NULL;
9239#ifdef FEAT_RELTIME
9240 if (list2proftime(&argvars[0], &tm) == OK)
9241 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9242#endif
9243}
9244
9245#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009246 static void
9247make_connection(void)
9248{
9249 if (X_DISPLAY == NULL
9250# ifdef FEAT_GUI
9251 && !gui.in_use
9252# endif
9253 )
9254 {
9255 x_force_connect = TRUE;
9256 setup_term_clip();
9257 x_force_connect = FALSE;
9258 }
9259}
9260
9261 static int
9262check_connection(void)
9263{
9264 make_connection();
9265 if (X_DISPLAY == NULL)
9266 {
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009267 EMSG(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009268 return FAIL;
9269 }
9270 return OK;
9271}
9272#endif
9273
9274#ifdef FEAT_CLIENTSERVER
9275 static void
9276remote_common(typval_T *argvars, typval_T *rettv, int expr)
9277{
9278 char_u *server_name;
9279 char_u *keys;
9280 char_u *r = NULL;
9281 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009282 int timeout = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009283# ifdef WIN32
9284 HWND w;
9285# else
9286 Window w;
9287# endif
9288
9289 if (check_restricted() || check_secure())
9290 return;
9291
9292# ifdef FEAT_X11
9293 if (check_connection() == FAIL)
9294 return;
9295# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009296 if (argvars[2].v_type != VAR_UNKNOWN
9297 && argvars[3].v_type != VAR_UNKNOWN)
9298 timeout = get_tv_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009299
9300 server_name = get_tv_string_chk(&argvars[0]);
9301 if (server_name == NULL)
9302 return; /* type error; errmsg already given */
9303 keys = get_tv_string_buf(&argvars[1], buf);
9304# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009305 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009306# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009307 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9308 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009309# endif
9310 {
9311 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009312 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009313 EMSG(r); /* sending worked but evaluation failed */
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009314 vim_free(r);
9315 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009316 else
9317 EMSG2(_("E241: Unable to send to %s"), server_name);
9318 return;
9319 }
9320
9321 rettv->vval.v_string = r;
9322
9323 if (argvars[2].v_type != VAR_UNKNOWN)
9324 {
9325 dictitem_T v;
9326 char_u str[30];
9327 char_u *idvar;
9328
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009329 idvar = get_tv_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009330 if (idvar != NULL && *idvar != NUL)
9331 {
9332 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9333 v.di_tv.v_type = VAR_STRING;
9334 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009335 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009336 vim_free(v.di_tv.vval.v_string);
9337 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009338 }
9339}
9340#endif
9341
9342/*
9343 * "remote_expr()" function
9344 */
9345 static void
9346f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9347{
9348 rettv->v_type = VAR_STRING;
9349 rettv->vval.v_string = NULL;
9350#ifdef FEAT_CLIENTSERVER
9351 remote_common(argvars, rettv, TRUE);
9352#endif
9353}
9354
9355/*
9356 * "remote_foreground()" function
9357 */
9358 static void
9359f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9360{
9361#ifdef FEAT_CLIENTSERVER
9362# ifdef WIN32
9363 /* On Win32 it's done in this application. */
9364 {
9365 char_u *server_name = get_tv_string_chk(&argvars[0]);
9366
9367 if (server_name != NULL)
9368 serverForeground(server_name);
9369 }
9370# else
9371 /* Send a foreground() expression to the server. */
9372 argvars[1].v_type = VAR_STRING;
9373 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9374 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009375 rettv->v_type = VAR_STRING;
9376 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009377 remote_common(argvars, rettv, TRUE);
9378 vim_free(argvars[1].vval.v_string);
9379# endif
9380#endif
9381}
9382
9383 static void
9384f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
9385{
9386#ifdef FEAT_CLIENTSERVER
9387 dictitem_T v;
9388 char_u *s = NULL;
9389# ifdef WIN32
9390 long_u n = 0;
9391# endif
9392 char_u *serverid;
9393
9394 if (check_restricted() || check_secure())
9395 {
9396 rettv->vval.v_number = -1;
9397 return;
9398 }
9399 serverid = get_tv_string_chk(&argvars[0]);
9400 if (serverid == NULL)
9401 {
9402 rettv->vval.v_number = -1;
9403 return; /* type error; errmsg already given */
9404 }
9405# ifdef WIN32
9406 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
9407 if (n == 0)
9408 rettv->vval.v_number = -1;
9409 else
9410 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009411 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009412 rettv->vval.v_number = (s != NULL);
9413 }
9414# else
9415 if (check_connection() == FAIL)
9416 return;
9417
9418 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
9419 serverStrToWin(serverid), &s);
9420# endif
9421
9422 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
9423 {
9424 char_u *retvar;
9425
9426 v.di_tv.v_type = VAR_STRING;
9427 v.di_tv.vval.v_string = vim_strsave(s);
9428 retvar = get_tv_string_chk(&argvars[1]);
9429 if (retvar != NULL)
9430 set_var(retvar, &v.di_tv, FALSE);
9431 vim_free(v.di_tv.vval.v_string);
9432 }
9433#else
9434 rettv->vval.v_number = -1;
9435#endif
9436}
9437
9438 static void
9439f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
9440{
9441 char_u *r = NULL;
9442
9443#ifdef FEAT_CLIENTSERVER
9444 char_u *serverid = get_tv_string_chk(&argvars[0]);
9445
9446 if (serverid != NULL && !check_restricted() && !check_secure())
9447 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009448 int timeout = 0;
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009449# ifdef WIN32
9450 /* The server's HWND is encoded in the 'id' parameter */
9451 long_u n = 0;
9452# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009453
9454 if (argvars[1].v_type != VAR_UNKNOWN)
9455 timeout = get_tv_number(&argvars[1]);
9456
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009457# ifdef WIN32
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009458 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
9459 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009460 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009461 if (r == NULL)
9462# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009463 if (check_connection() == FAIL
9464 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
9465 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009466# endif
9467 EMSG(_("E277: Unable to read a server reply"));
9468 }
9469#endif
9470 rettv->v_type = VAR_STRING;
9471 rettv->vval.v_string = r;
9472}
9473
9474/*
9475 * "remote_send()" function
9476 */
9477 static void
9478f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9479{
9480 rettv->v_type = VAR_STRING;
9481 rettv->vval.v_string = NULL;
9482#ifdef FEAT_CLIENTSERVER
9483 remote_common(argvars, rettv, FALSE);
9484#endif
9485}
9486
9487/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009488 * "remote_startserver()" function
9489 */
9490 static void
9491f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9492{
9493#ifdef FEAT_CLIENTSERVER
9494 char_u *server = get_tv_string_chk(&argvars[0]);
9495
9496 if (server == NULL)
9497 return; /* type error; errmsg already given */
9498 if (serverName != NULL)
9499 EMSG(_("E941: already started a server"));
9500 else
9501 {
9502# ifdef FEAT_X11
9503 if (check_connection() == OK)
9504 serverRegisterName(X_DISPLAY, server);
9505# else
9506 serverSetName(server);
9507# endif
9508 }
9509#else
9510 EMSG(_("E942: +clientserver feature not available"));
9511#endif
9512}
9513
9514/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009515 * "remove()" function
9516 */
9517 static void
9518f_remove(typval_T *argvars, typval_T *rettv)
9519{
9520 list_T *l;
9521 listitem_T *item, *item2;
9522 listitem_T *li;
9523 long idx;
9524 long end;
9525 char_u *key;
9526 dict_T *d;
9527 dictitem_T *di;
9528 char_u *arg_errmsg = (char_u *)N_("remove() argument");
9529
9530 if (argvars[0].v_type == VAR_DICT)
9531 {
9532 if (argvars[2].v_type != VAR_UNKNOWN)
9533 EMSG2(_(e_toomanyarg), "remove()");
9534 else if ((d = argvars[0].vval.v_dict) != NULL
9535 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
9536 {
9537 key = get_tv_string_chk(&argvars[1]);
9538 if (key != NULL)
9539 {
9540 di = dict_find(d, key, -1);
9541 if (di == NULL)
9542 EMSG2(_(e_dictkey), key);
9543 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
9544 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
9545 {
9546 *rettv = di->di_tv;
9547 init_tv(&di->di_tv);
9548 dictitem_remove(d, di);
9549 }
9550 }
9551 }
9552 }
9553 else if (argvars[0].v_type != VAR_LIST)
9554 EMSG2(_(e_listdictarg), "remove()");
9555 else if ((l = argvars[0].vval.v_list) != NULL
9556 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
9557 {
9558 int error = FALSE;
9559
9560 idx = (long)get_tv_number_chk(&argvars[1], &error);
9561 if (error)
9562 ; /* type error: do nothing, errmsg already given */
9563 else if ((item = list_find(l, idx)) == NULL)
9564 EMSGN(_(e_listidx), idx);
9565 else
9566 {
9567 if (argvars[2].v_type == VAR_UNKNOWN)
9568 {
9569 /* Remove one item, return its value. */
9570 vimlist_remove(l, item, item);
9571 *rettv = item->li_tv;
9572 vim_free(item);
9573 }
9574 else
9575 {
9576 /* Remove range of items, return list with values. */
9577 end = (long)get_tv_number_chk(&argvars[2], &error);
9578 if (error)
9579 ; /* type error: do nothing */
9580 else if ((item2 = list_find(l, end)) == NULL)
9581 EMSGN(_(e_listidx), end);
9582 else
9583 {
9584 int cnt = 0;
9585
9586 for (li = item; li != NULL; li = li->li_next)
9587 {
9588 ++cnt;
9589 if (li == item2)
9590 break;
9591 }
9592 if (li == NULL) /* didn't find "item2" after "item" */
9593 EMSG(_(e_invrange));
9594 else
9595 {
9596 vimlist_remove(l, item, item2);
9597 if (rettv_list_alloc(rettv) == OK)
9598 {
9599 l = rettv->vval.v_list;
9600 l->lv_first = item;
9601 l->lv_last = item2;
9602 item->li_prev = NULL;
9603 item2->li_next = NULL;
9604 l->lv_len = cnt;
9605 }
9606 }
9607 }
9608 }
9609 }
9610 }
9611}
9612
9613/*
9614 * "rename({from}, {to})" function
9615 */
9616 static void
9617f_rename(typval_T *argvars, typval_T *rettv)
9618{
9619 char_u buf[NUMBUFLEN];
9620
9621 if (check_restricted() || check_secure())
9622 rettv->vval.v_number = -1;
9623 else
9624 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
9625 get_tv_string_buf(&argvars[1], buf));
9626}
9627
9628/*
9629 * "repeat()" function
9630 */
9631 static void
9632f_repeat(typval_T *argvars, typval_T *rettv)
9633{
9634 char_u *p;
9635 int n;
9636 int slen;
9637 int len;
9638 char_u *r;
9639 int i;
9640
9641 n = (int)get_tv_number(&argvars[1]);
9642 if (argvars[0].v_type == VAR_LIST)
9643 {
9644 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9645 while (n-- > 0)
9646 if (list_extend(rettv->vval.v_list,
9647 argvars[0].vval.v_list, NULL) == FAIL)
9648 break;
9649 }
9650 else
9651 {
9652 p = get_tv_string(&argvars[0]);
9653 rettv->v_type = VAR_STRING;
9654 rettv->vval.v_string = NULL;
9655
9656 slen = (int)STRLEN(p);
9657 len = slen * n;
9658 if (len <= 0)
9659 return;
9660
9661 r = alloc(len + 1);
9662 if (r != NULL)
9663 {
9664 for (i = 0; i < n; i++)
9665 mch_memmove(r + i * slen, p, (size_t)slen);
9666 r[len] = NUL;
9667 }
9668
9669 rettv->vval.v_string = r;
9670 }
9671}
9672
9673/*
9674 * "resolve()" function
9675 */
9676 static void
9677f_resolve(typval_T *argvars, typval_T *rettv)
9678{
9679 char_u *p;
9680#ifdef HAVE_READLINK
9681 char_u *buf = NULL;
9682#endif
9683
9684 p = get_tv_string(&argvars[0]);
9685#ifdef FEAT_SHORTCUT
9686 {
9687 char_u *v = NULL;
9688
9689 v = mch_resolve_shortcut(p);
9690 if (v != NULL)
9691 rettv->vval.v_string = v;
9692 else
9693 rettv->vval.v_string = vim_strsave(p);
9694 }
9695#else
9696# ifdef HAVE_READLINK
9697 {
9698 char_u *cpy;
9699 int len;
9700 char_u *remain = NULL;
9701 char_u *q;
9702 int is_relative_to_current = FALSE;
9703 int has_trailing_pathsep = FALSE;
9704 int limit = 100;
9705
9706 p = vim_strsave(p);
9707
9708 if (p[0] == '.' && (vim_ispathsep(p[1])
9709 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9710 is_relative_to_current = TRUE;
9711
9712 len = STRLEN(p);
9713 if (len > 0 && after_pathsep(p, p + len))
9714 {
9715 has_trailing_pathsep = TRUE;
9716 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9717 }
9718
9719 q = getnextcomp(p);
9720 if (*q != NUL)
9721 {
9722 /* Separate the first path component in "p", and keep the
9723 * remainder (beginning with the path separator). */
9724 remain = vim_strsave(q - 1);
9725 q[-1] = NUL;
9726 }
9727
9728 buf = alloc(MAXPATHL + 1);
9729 if (buf == NULL)
9730 goto fail;
9731
9732 for (;;)
9733 {
9734 for (;;)
9735 {
9736 len = readlink((char *)p, (char *)buf, MAXPATHL);
9737 if (len <= 0)
9738 break;
9739 buf[len] = NUL;
9740
9741 if (limit-- == 0)
9742 {
9743 vim_free(p);
9744 vim_free(remain);
9745 EMSG(_("E655: Too many symbolic links (cycle?)"));
9746 rettv->vval.v_string = NULL;
9747 goto fail;
9748 }
9749
9750 /* Ensure that the result will have a trailing path separator
9751 * if the argument has one. */
9752 if (remain == NULL && has_trailing_pathsep)
9753 add_pathsep(buf);
9754
9755 /* Separate the first path component in the link value and
9756 * concatenate the remainders. */
9757 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9758 if (*q != NUL)
9759 {
9760 if (remain == NULL)
9761 remain = vim_strsave(q - 1);
9762 else
9763 {
9764 cpy = concat_str(q - 1, remain);
9765 if (cpy != NULL)
9766 {
9767 vim_free(remain);
9768 remain = cpy;
9769 }
9770 }
9771 q[-1] = NUL;
9772 }
9773
9774 q = gettail(p);
9775 if (q > p && *q == NUL)
9776 {
9777 /* Ignore trailing path separator. */
9778 q[-1] = NUL;
9779 q = gettail(p);
9780 }
9781 if (q > p && !mch_isFullName(buf))
9782 {
9783 /* symlink is relative to directory of argument */
9784 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
9785 if (cpy != NULL)
9786 {
9787 STRCPY(cpy, p);
9788 STRCPY(gettail(cpy), buf);
9789 vim_free(p);
9790 p = cpy;
9791 }
9792 }
9793 else
9794 {
9795 vim_free(p);
9796 p = vim_strsave(buf);
9797 }
9798 }
9799
9800 if (remain == NULL)
9801 break;
9802
9803 /* Append the first path component of "remain" to "p". */
9804 q = getnextcomp(remain + 1);
9805 len = q - remain - (*q != NUL);
9806 cpy = vim_strnsave(p, STRLEN(p) + len);
9807 if (cpy != NULL)
9808 {
9809 STRNCAT(cpy, remain, len);
9810 vim_free(p);
9811 p = cpy;
9812 }
9813 /* Shorten "remain". */
9814 if (*q != NUL)
9815 STRMOVE(remain, q - 1);
9816 else
Bram Moolenaard23a8232018-02-10 18:45:26 +01009817 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009818 }
9819
9820 /* If the result is a relative path name, make it explicitly relative to
9821 * the current directory if and only if the argument had this form. */
9822 if (!vim_ispathsep(*p))
9823 {
9824 if (is_relative_to_current
9825 && *p != NUL
9826 && !(p[0] == '.'
9827 && (p[1] == NUL
9828 || vim_ispathsep(p[1])
9829 || (p[1] == '.'
9830 && (p[2] == NUL
9831 || vim_ispathsep(p[2]))))))
9832 {
9833 /* Prepend "./". */
9834 cpy = concat_str((char_u *)"./", p);
9835 if (cpy != NULL)
9836 {
9837 vim_free(p);
9838 p = cpy;
9839 }
9840 }
9841 else if (!is_relative_to_current)
9842 {
9843 /* Strip leading "./". */
9844 q = p;
9845 while (q[0] == '.' && vim_ispathsep(q[1]))
9846 q += 2;
9847 if (q > p)
9848 STRMOVE(p, p + 2);
9849 }
9850 }
9851
9852 /* Ensure that the result will have no trailing path separator
9853 * if the argument had none. But keep "/" or "//". */
9854 if (!has_trailing_pathsep)
9855 {
9856 q = p + STRLEN(p);
9857 if (after_pathsep(p, q))
9858 *gettail_sep(p) = NUL;
9859 }
9860
9861 rettv->vval.v_string = p;
9862 }
9863# else
9864 rettv->vval.v_string = vim_strsave(p);
9865# endif
9866#endif
9867
9868 simplify_filename(rettv->vval.v_string);
9869
9870#ifdef HAVE_READLINK
9871fail:
9872 vim_free(buf);
9873#endif
9874 rettv->v_type = VAR_STRING;
9875}
9876
9877/*
9878 * "reverse({list})" function
9879 */
9880 static void
9881f_reverse(typval_T *argvars, typval_T *rettv)
9882{
9883 list_T *l;
9884 listitem_T *li, *ni;
9885
9886 if (argvars[0].v_type != VAR_LIST)
9887 EMSG2(_(e_listarg), "reverse()");
9888 else if ((l = argvars[0].vval.v_list) != NULL
9889 && !tv_check_lock(l->lv_lock,
9890 (char_u *)N_("reverse() argument"), TRUE))
9891 {
9892 li = l->lv_last;
9893 l->lv_first = l->lv_last = NULL;
9894 l->lv_len = 0;
9895 while (li != NULL)
9896 {
9897 ni = li->li_prev;
9898 list_append(l, li);
9899 li = ni;
9900 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009901 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009902 l->lv_idx = l->lv_len - l->lv_idx - 1;
9903 }
9904}
9905
9906#define SP_NOMOVE 0x01 /* don't move cursor */
9907#define SP_REPEAT 0x02 /* repeat to find outer pair */
9908#define SP_RETCOUNT 0x04 /* return matchcount */
9909#define SP_SETPCMARK 0x08 /* set previous context mark */
9910#define SP_START 0x10 /* accept match at start position */
9911#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
9912#define SP_END 0x40 /* leave cursor at end of match */
9913#define SP_COLUMN 0x80 /* start at cursor column */
9914
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009915/*
9916 * Get flags for a search function.
9917 * Possibly sets "p_ws".
9918 * Returns BACKWARD, FORWARD or zero (for an error).
9919 */
9920 static int
9921get_search_arg(typval_T *varp, int *flagsp)
9922{
9923 int dir = FORWARD;
9924 char_u *flags;
9925 char_u nbuf[NUMBUFLEN];
9926 int mask;
9927
9928 if (varp->v_type != VAR_UNKNOWN)
9929 {
9930 flags = get_tv_string_buf_chk(varp, nbuf);
9931 if (flags == NULL)
9932 return 0; /* type error; errmsg already given */
9933 while (*flags != NUL)
9934 {
9935 switch (*flags)
9936 {
9937 case 'b': dir = BACKWARD; break;
9938 case 'w': p_ws = TRUE; break;
9939 case 'W': p_ws = FALSE; break;
9940 default: mask = 0;
9941 if (flagsp != NULL)
9942 switch (*flags)
9943 {
9944 case 'c': mask = SP_START; break;
9945 case 'e': mask = SP_END; break;
9946 case 'm': mask = SP_RETCOUNT; break;
9947 case 'n': mask = SP_NOMOVE; break;
9948 case 'p': mask = SP_SUBPAT; break;
9949 case 'r': mask = SP_REPEAT; break;
9950 case 's': mask = SP_SETPCMARK; break;
9951 case 'z': mask = SP_COLUMN; break;
9952 }
9953 if (mask == 0)
9954 {
9955 EMSG2(_(e_invarg2), flags);
9956 dir = 0;
9957 }
9958 else
9959 *flagsp |= mask;
9960 }
9961 if (dir == 0)
9962 break;
9963 ++flags;
9964 }
9965 }
9966 return dir;
9967}
9968
9969/*
9970 * Shared by search() and searchpos() functions.
9971 */
9972 static int
9973search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
9974{
9975 int flags;
9976 char_u *pat;
9977 pos_T pos;
9978 pos_T save_cursor;
9979 int save_p_ws = p_ws;
9980 int dir;
9981 int retval = 0; /* default: FAIL */
9982 long lnum_stop = 0;
9983 proftime_T tm;
9984#ifdef FEAT_RELTIME
9985 long time_limit = 0;
9986#endif
9987 int options = SEARCH_KEEP;
9988 int subpatnum;
9989
9990 pat = get_tv_string(&argvars[0]);
9991 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
9992 if (dir == 0)
9993 goto theend;
9994 flags = *flagsp;
9995 if (flags & SP_START)
9996 options |= SEARCH_START;
9997 if (flags & SP_END)
9998 options |= SEARCH_END;
9999 if (flags & SP_COLUMN)
10000 options |= SEARCH_COL;
10001
10002 /* Optional arguments: line number to stop searching and timeout. */
10003 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
10004 {
10005 lnum_stop = (long)get_tv_number_chk(&argvars[2], NULL);
10006 if (lnum_stop < 0)
10007 goto theend;
10008#ifdef FEAT_RELTIME
10009 if (argvars[3].v_type != VAR_UNKNOWN)
10010 {
10011 time_limit = (long)get_tv_number_chk(&argvars[3], NULL);
10012 if (time_limit < 0)
10013 goto theend;
10014 }
10015#endif
10016 }
10017
10018#ifdef FEAT_RELTIME
10019 /* Set the time limit, if there is one. */
10020 profile_setlimit(time_limit, &tm);
10021#endif
10022
10023 /*
10024 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
10025 * Check to make sure only those flags are set.
10026 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
10027 * flags cannot be set. Check for that condition also.
10028 */
10029 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
10030 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10031 {
10032 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
10033 goto theend;
10034 }
10035
10036 pos = save_cursor = curwin->w_cursor;
10037 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010038 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010039 if (subpatnum != FAIL)
10040 {
10041 if (flags & SP_SUBPAT)
10042 retval = subpatnum;
10043 else
10044 retval = pos.lnum;
10045 if (flags & SP_SETPCMARK)
10046 setpcmark();
10047 curwin->w_cursor = pos;
10048 if (match_pos != NULL)
10049 {
10050 /* Store the match cursor position */
10051 match_pos->lnum = pos.lnum;
10052 match_pos->col = pos.col + 1;
10053 }
10054 /* "/$" will put the cursor after the end of the line, may need to
10055 * correct that here */
10056 check_cursor();
10057 }
10058
10059 /* If 'n' flag is used: restore cursor position. */
10060 if (flags & SP_NOMOVE)
10061 curwin->w_cursor = save_cursor;
10062 else
10063 curwin->w_set_curswant = TRUE;
10064theend:
10065 p_ws = save_p_ws;
10066
10067 return retval;
10068}
10069
10070#ifdef FEAT_FLOAT
10071
10072/*
10073 * round() is not in C90, use ceil() or floor() instead.
10074 */
10075 float_T
10076vim_round(float_T f)
10077{
10078 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
10079}
10080
10081/*
10082 * "round({float})" function
10083 */
10084 static void
10085f_round(typval_T *argvars, typval_T *rettv)
10086{
10087 float_T f = 0.0;
10088
10089 rettv->v_type = VAR_FLOAT;
10090 if (get_float_arg(argvars, &f) == OK)
10091 rettv->vval.v_float = vim_round(f);
10092 else
10093 rettv->vval.v_float = 0.0;
10094}
10095#endif
10096
10097/*
10098 * "screenattr()" function
10099 */
10100 static void
10101f_screenattr(typval_T *argvars, typval_T *rettv)
10102{
10103 int row;
10104 int col;
10105 int c;
10106
10107 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
10108 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
10109 if (row < 0 || row >= screen_Rows
10110 || col < 0 || col >= screen_Columns)
10111 c = -1;
10112 else
10113 c = ScreenAttrs[LineOffset[row] + col];
10114 rettv->vval.v_number = c;
10115}
10116
10117/*
10118 * "screenchar()" function
10119 */
10120 static void
10121f_screenchar(typval_T *argvars, typval_T *rettv)
10122{
10123 int row;
10124 int col;
10125 int off;
10126 int c;
10127
10128 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
10129 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
10130 if (row < 0 || row >= screen_Rows
10131 || col < 0 || col >= screen_Columns)
10132 c = -1;
10133 else
10134 {
10135 off = LineOffset[row] + col;
10136#ifdef FEAT_MBYTE
10137 if (enc_utf8 && ScreenLinesUC[off] != 0)
10138 c = ScreenLinesUC[off];
10139 else
10140#endif
10141 c = ScreenLines[off];
10142 }
10143 rettv->vval.v_number = c;
10144}
10145
10146/*
10147 * "screencol()" function
10148 *
10149 * First column is 1 to be consistent with virtcol().
10150 */
10151 static void
10152f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10153{
10154 rettv->vval.v_number = screen_screencol() + 1;
10155}
10156
10157/*
10158 * "screenrow()" function
10159 */
10160 static void
10161f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10162{
10163 rettv->vval.v_number = screen_screenrow() + 1;
10164}
10165
10166/*
10167 * "search()" function
10168 */
10169 static void
10170f_search(typval_T *argvars, typval_T *rettv)
10171{
10172 int flags = 0;
10173
10174 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10175}
10176
10177/*
10178 * "searchdecl()" function
10179 */
10180 static void
10181f_searchdecl(typval_T *argvars, typval_T *rettv)
10182{
10183 int locally = 1;
10184 int thisblock = 0;
10185 int error = FALSE;
10186 char_u *name;
10187
10188 rettv->vval.v_number = 1; /* default: FAIL */
10189
10190 name = get_tv_string_chk(&argvars[0]);
10191 if (argvars[1].v_type != VAR_UNKNOWN)
10192 {
10193 locally = (int)get_tv_number_chk(&argvars[1], &error) == 0;
10194 if (!error && argvars[2].v_type != VAR_UNKNOWN)
10195 thisblock = (int)get_tv_number_chk(&argvars[2], &error) != 0;
10196 }
10197 if (!error && name != NULL)
10198 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10199 locally, thisblock, SEARCH_KEEP) == FAIL;
10200}
10201
10202/*
10203 * Used by searchpair() and searchpairpos()
10204 */
10205 static int
10206searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10207{
10208 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010209 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010210 int save_p_ws = p_ws;
10211 int dir;
10212 int flags = 0;
10213 char_u nbuf1[NUMBUFLEN];
10214 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010215 int retval = 0; /* default: FAIL */
10216 long lnum_stop = 0;
10217 long time_limit = 0;
10218
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010219 /* Get the three pattern arguments: start, middle, end. Will result in an
10220 * error if not a valid argument. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010221 spat = get_tv_string_chk(&argvars[0]);
10222 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
10223 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
10224 if (spat == NULL || mpat == NULL || epat == NULL)
10225 goto theend; /* type error */
10226
10227 /* Handle the optional fourth argument: flags */
10228 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
10229 if (dir == 0)
10230 goto theend;
10231
10232 /* Don't accept SP_END or SP_SUBPAT.
10233 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
10234 */
10235 if ((flags & (SP_END | SP_SUBPAT)) != 0
10236 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10237 {
10238 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
10239 goto theend;
10240 }
10241
10242 /* Using 'r' implies 'W', otherwise it doesn't work. */
10243 if (flags & SP_REPEAT)
10244 p_ws = FALSE;
10245
10246 /* Optional fifth argument: skip expression */
10247 if (argvars[3].v_type == VAR_UNKNOWN
10248 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010010249 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010250 else
10251 {
Bram Moolenaar48570482017-10-30 21:48:41 +010010252 skip = &argvars[4];
10253 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
10254 && skip->v_type != VAR_STRING)
10255 {
10256 /* Type error */
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010257 EMSG2(_(e_invarg2), get_tv_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010010258 goto theend;
10259 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010260 if (argvars[5].v_type != VAR_UNKNOWN)
10261 {
10262 lnum_stop = (long)get_tv_number_chk(&argvars[5], NULL);
10263 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010264 {
10265 EMSG2(_(e_invarg2), get_tv_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010266 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010267 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010268#ifdef FEAT_RELTIME
10269 if (argvars[6].v_type != VAR_UNKNOWN)
10270 {
10271 time_limit = (long)get_tv_number_chk(&argvars[6], NULL);
10272 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010273 {
10274 EMSG2(_(e_invarg2), get_tv_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010275 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010276 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010277 }
10278#endif
10279 }
10280 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010281
10282 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
10283 match_pos, lnum_stop, time_limit);
10284
10285theend:
10286 p_ws = save_p_ws;
10287
10288 return retval;
10289}
10290
10291/*
10292 * "searchpair()" function
10293 */
10294 static void
10295f_searchpair(typval_T *argvars, typval_T *rettv)
10296{
10297 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
10298}
10299
10300/*
10301 * "searchpairpos()" function
10302 */
10303 static void
10304f_searchpairpos(typval_T *argvars, typval_T *rettv)
10305{
10306 pos_T match_pos;
10307 int lnum = 0;
10308 int col = 0;
10309
10310 if (rettv_list_alloc(rettv) == FAIL)
10311 return;
10312
10313 if (searchpair_cmn(argvars, &match_pos) > 0)
10314 {
10315 lnum = match_pos.lnum;
10316 col = match_pos.col;
10317 }
10318
10319 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10320 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10321}
10322
10323/*
10324 * Search for a start/middle/end thing.
10325 * Used by searchpair(), see its documentation for the details.
10326 * Returns 0 or -1 for no match,
10327 */
10328 long
10329do_searchpair(
10330 char_u *spat, /* start pattern */
10331 char_u *mpat, /* middle pattern */
10332 char_u *epat, /* end pattern */
10333 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010010334 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010335 int flags, /* SP_SETPCMARK and other SP_ values */
10336 pos_T *match_pos,
10337 linenr_T lnum_stop, /* stop at this line if not zero */
10338 long time_limit UNUSED) /* stop after this many msec */
10339{
10340 char_u *save_cpo;
10341 char_u *pat, *pat2 = NULL, *pat3 = NULL;
10342 long retval = 0;
10343 pos_T pos;
10344 pos_T firstpos;
10345 pos_T foundpos;
10346 pos_T save_cursor;
10347 pos_T save_pos;
10348 int n;
10349 int r;
10350 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010010351 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010352 int err;
10353 int options = SEARCH_KEEP;
10354 proftime_T tm;
10355
10356 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10357 save_cpo = p_cpo;
10358 p_cpo = empty_option;
10359
10360#ifdef FEAT_RELTIME
10361 /* Set the time limit, if there is one. */
10362 profile_setlimit(time_limit, &tm);
10363#endif
10364
10365 /* Make two search patterns: start/end (pat2, for in nested pairs) and
10366 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010367 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
10368 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010369 if (pat2 == NULL || pat3 == NULL)
10370 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010371 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010372 if (*mpat == NUL)
10373 STRCPY(pat3, pat2);
10374 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010375 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010376 spat, epat, mpat);
10377 if (flags & SP_START)
10378 options |= SEARCH_START;
10379
Bram Moolenaar48570482017-10-30 21:48:41 +010010380 if (skip != NULL)
10381 {
10382 /* Empty string means to not use the skip expression. */
10383 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
10384 use_skip = skip->vval.v_string != NULL
10385 && *skip->vval.v_string != NUL;
10386 }
10387
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010388 save_cursor = curwin->w_cursor;
10389 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010390 CLEAR_POS(&firstpos);
10391 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010392 pat = pat3;
10393 for (;;)
10394 {
10395 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010396 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010397 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010398 /* didn't find it or found the first match again: FAIL */
10399 break;
10400
10401 if (firstpos.lnum == 0)
10402 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010403 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010404 {
10405 /* Found the same position again. Can happen with a pattern that
10406 * has "\zs" at the end and searching backwards. Advance one
10407 * character and try again. */
10408 if (dir == BACKWARD)
10409 decl(&pos);
10410 else
10411 incl(&pos);
10412 }
10413 foundpos = pos;
10414
10415 /* clear the start flag to avoid getting stuck here */
10416 options &= ~SEARCH_START;
10417
10418 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010010419 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010420 {
10421 save_pos = curwin->w_cursor;
10422 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010010423 err = FALSE;
10424 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010425 curwin->w_cursor = save_pos;
10426 if (err)
10427 {
10428 /* Evaluating {skip} caused an error, break here. */
10429 curwin->w_cursor = save_cursor;
10430 retval = -1;
10431 break;
10432 }
10433 if (r)
10434 continue;
10435 }
10436
10437 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
10438 {
10439 /* Found end when searching backwards or start when searching
10440 * forward: nested pair. */
10441 ++nest;
10442 pat = pat2; /* nested, don't search for middle */
10443 }
10444 else
10445 {
10446 /* Found end when searching forward or start when searching
10447 * backward: end of (nested) pair; or found middle in outer pair. */
10448 if (--nest == 1)
10449 pat = pat3; /* outer level, search for middle */
10450 }
10451
10452 if (nest == 0)
10453 {
10454 /* Found the match: return matchcount or line number. */
10455 if (flags & SP_RETCOUNT)
10456 ++retval;
10457 else
10458 retval = pos.lnum;
10459 if (flags & SP_SETPCMARK)
10460 setpcmark();
10461 curwin->w_cursor = pos;
10462 if (!(flags & SP_REPEAT))
10463 break;
10464 nest = 1; /* search for next unmatched */
10465 }
10466 }
10467
10468 if (match_pos != NULL)
10469 {
10470 /* Store the match cursor position */
10471 match_pos->lnum = curwin->w_cursor.lnum;
10472 match_pos->col = curwin->w_cursor.col + 1;
10473 }
10474
10475 /* If 'n' flag is used or search failed: restore cursor position. */
10476 if ((flags & SP_NOMOVE) || retval == 0)
10477 curwin->w_cursor = save_cursor;
10478
10479theend:
10480 vim_free(pat2);
10481 vim_free(pat3);
10482 if (p_cpo == empty_option)
10483 p_cpo = save_cpo;
10484 else
10485 /* Darn, evaluating the {skip} expression changed the value. */
10486 free_string_option(save_cpo);
10487
10488 return retval;
10489}
10490
10491/*
10492 * "searchpos()" function
10493 */
10494 static void
10495f_searchpos(typval_T *argvars, typval_T *rettv)
10496{
10497 pos_T match_pos;
10498 int lnum = 0;
10499 int col = 0;
10500 int n;
10501 int flags = 0;
10502
10503 if (rettv_list_alloc(rettv) == FAIL)
10504 return;
10505
10506 n = search_cmn(argvars, &match_pos, &flags);
10507 if (n > 0)
10508 {
10509 lnum = match_pos.lnum;
10510 col = match_pos.col;
10511 }
10512
10513 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10514 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10515 if (flags & SP_SUBPAT)
10516 list_append_number(rettv->vval.v_list, (varnumber_T)n);
10517}
10518
10519 static void
10520f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
10521{
10522#ifdef FEAT_CLIENTSERVER
10523 char_u buf[NUMBUFLEN];
10524 char_u *server = get_tv_string_chk(&argvars[0]);
10525 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
10526
10527 rettv->vval.v_number = -1;
10528 if (server == NULL || reply == NULL)
10529 return;
10530 if (check_restricted() || check_secure())
10531 return;
10532# ifdef FEAT_X11
10533 if (check_connection() == FAIL)
10534 return;
10535# endif
10536
10537 if (serverSendReply(server, reply) < 0)
10538 {
10539 EMSG(_("E258: Unable to send to client"));
10540 return;
10541 }
10542 rettv->vval.v_number = 0;
10543#else
10544 rettv->vval.v_number = -1;
10545#endif
10546}
10547
10548 static void
10549f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
10550{
10551 char_u *r = NULL;
10552
10553#ifdef FEAT_CLIENTSERVER
10554# ifdef WIN32
10555 r = serverGetVimNames();
10556# else
10557 make_connection();
10558 if (X_DISPLAY != NULL)
10559 r = serverGetVimNames(X_DISPLAY);
10560# endif
10561#endif
10562 rettv->v_type = VAR_STRING;
10563 rettv->vval.v_string = r;
10564}
10565
10566/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010567 * "setbufline()" function
10568 */
10569 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020010570f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010571{
10572 linenr_T lnum;
10573 buf_T *buf;
10574
10575 buf = get_buf_tv(&argvars[0], FALSE);
10576 if (buf == NULL)
10577 rettv->vval.v_number = 1; /* FAIL */
10578 else
10579 {
10580 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020010581 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010582 }
10583}
10584
10585/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010586 * "setbufvar()" function
10587 */
10588 static void
10589f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10590{
10591 buf_T *buf;
10592 char_u *varname, *bufvarname;
10593 typval_T *varp;
10594 char_u nbuf[NUMBUFLEN];
10595
10596 if (check_restricted() || check_secure())
10597 return;
10598 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10599 varname = get_tv_string_chk(&argvars[1]);
10600 buf = get_buf_tv(&argvars[0], FALSE);
10601 varp = &argvars[2];
10602
10603 if (buf != NULL && varname != NULL && varp != NULL)
10604 {
10605 if (*varname == '&')
10606 {
10607 long numval;
10608 char_u *strval;
10609 int error = FALSE;
10610 aco_save_T aco;
10611
10612 /* set curbuf to be our buf, temporarily */
10613 aucmd_prepbuf(&aco, buf);
10614
10615 ++varname;
10616 numval = (long)get_tv_number_chk(varp, &error);
10617 strval = get_tv_string_buf_chk(varp, nbuf);
10618 if (!error && strval != NULL)
10619 set_option_value(varname, numval, strval, OPT_LOCAL);
10620
10621 /* reset notion of buffer */
10622 aucmd_restbuf(&aco);
10623 }
10624 else
10625 {
10626 buf_T *save_curbuf = curbuf;
10627
10628 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
10629 if (bufvarname != NULL)
10630 {
10631 curbuf = buf;
10632 STRCPY(bufvarname, "b:");
10633 STRCPY(bufvarname + 2, varname);
10634 set_var(bufvarname, varp, TRUE);
10635 vim_free(bufvarname);
10636 curbuf = save_curbuf;
10637 }
10638 }
10639 }
10640}
10641
10642 static void
10643f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10644{
10645 dict_T *d;
10646 dictitem_T *di;
10647 char_u *csearch;
10648
10649 if (argvars[0].v_type != VAR_DICT)
10650 {
10651 EMSG(_(e_dictreq));
10652 return;
10653 }
10654
10655 if ((d = argvars[0].vval.v_dict) != NULL)
10656 {
10657 csearch = get_dict_string(d, (char_u *)"char", FALSE);
10658 if (csearch != NULL)
10659 {
10660#ifdef FEAT_MBYTE
10661 if (enc_utf8)
10662 {
10663 int pcc[MAX_MCO];
10664 int c = utfc_ptr2char(csearch, pcc);
10665
10666 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
10667 }
10668 else
10669#endif
10670 set_last_csearch(PTR2CHAR(csearch),
10671 csearch, MB_PTR2LEN(csearch));
10672 }
10673
10674 di = dict_find(d, (char_u *)"forward", -1);
10675 if (di != NULL)
10676 set_csearch_direction((int)get_tv_number(&di->di_tv)
10677 ? FORWARD : BACKWARD);
10678
10679 di = dict_find(d, (char_u *)"until", -1);
10680 if (di != NULL)
10681 set_csearch_until(!!get_tv_number(&di->di_tv));
10682 }
10683}
10684
10685/*
10686 * "setcmdpos()" function
10687 */
10688 static void
10689f_setcmdpos(typval_T *argvars, typval_T *rettv)
10690{
10691 int pos = (int)get_tv_number(&argvars[0]) - 1;
10692
10693 if (pos >= 0)
10694 rettv->vval.v_number = set_cmdline_pos(pos);
10695}
10696
10697/*
10698 * "setfperm({fname}, {mode})" function
10699 */
10700 static void
10701f_setfperm(typval_T *argvars, typval_T *rettv)
10702{
10703 char_u *fname;
10704 char_u modebuf[NUMBUFLEN];
10705 char_u *mode_str;
10706 int i;
10707 int mask;
10708 int mode = 0;
10709
10710 rettv->vval.v_number = 0;
10711 fname = get_tv_string_chk(&argvars[0]);
10712 if (fname == NULL)
10713 return;
10714 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
10715 if (mode_str == NULL)
10716 return;
10717 if (STRLEN(mode_str) != 9)
10718 {
10719 EMSG2(_(e_invarg2), mode_str);
10720 return;
10721 }
10722
10723 mask = 1;
10724 for (i = 8; i >= 0; --i)
10725 {
10726 if (mode_str[i] != '-')
10727 mode |= mask;
10728 mask = mask << 1;
10729 }
10730 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
10731}
10732
10733/*
10734 * "setline()" function
10735 */
10736 static void
10737f_setline(typval_T *argvars, typval_T *rettv)
10738{
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010739 linenr_T lnum = get_tv_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010740
Bram Moolenaarca851592018-06-06 21:04:07 +020010741 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010742}
10743
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010744/*
10745 * Used by "setqflist()" and "setloclist()" functions
10746 */
10747 static void
10748set_qf_ll_list(
10749 win_T *wp UNUSED,
10750 typval_T *list_arg UNUSED,
10751 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020010752 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010753 typval_T *rettv)
10754{
10755#ifdef FEAT_QUICKFIX
10756 static char *e_invact = N_("E927: Invalid action: '%s'");
10757 char_u *act;
10758 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010759 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010760#endif
10761
10762 rettv->vval.v_number = -1;
10763
10764#ifdef FEAT_QUICKFIX
10765 if (list_arg->v_type != VAR_LIST)
10766 EMSG(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010767 else if (recursive != 0)
10768 EMSG(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010769 else
10770 {
10771 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010772 dict_T *d = NULL;
10773 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010774
10775 if (action_arg->v_type == VAR_STRING)
10776 {
10777 act = get_tv_string_chk(action_arg);
10778 if (act == NULL)
10779 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020010780 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
10781 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010782 action = *act;
10783 else
10784 EMSG2(_(e_invact), act);
10785 }
10786 else if (action_arg->v_type == VAR_UNKNOWN)
10787 action = ' ';
10788 else
10789 EMSG(_(e_stringreq));
10790
Bram Moolenaard823fa92016-08-12 16:29:27 +020010791 if (action_arg->v_type != VAR_UNKNOWN
10792 && what_arg->v_type != VAR_UNKNOWN)
10793 {
10794 if (what_arg->v_type == VAR_DICT)
10795 d = what_arg->vval.v_dict;
10796 else
10797 {
10798 EMSG(_(e_dictreq));
10799 valid_dict = FALSE;
10800 }
10801 }
10802
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010803 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010804 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010805 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
10806 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010807 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010808 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010809 }
10810#endif
10811}
10812
10813/*
10814 * "setloclist()" function
10815 */
10816 static void
10817f_setloclist(typval_T *argvars, typval_T *rettv)
10818{
10819 win_T *win;
10820
10821 rettv->vval.v_number = -1;
10822
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020010823 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010824 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020010825 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010826}
10827
10828/*
10829 * "setmatches()" function
10830 */
10831 static void
10832f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10833{
10834#ifdef FEAT_SEARCH_EXTRA
10835 list_T *l;
10836 listitem_T *li;
10837 dict_T *d;
10838 list_T *s = NULL;
10839
10840 rettv->vval.v_number = -1;
10841 if (argvars[0].v_type != VAR_LIST)
10842 {
10843 EMSG(_(e_listreq));
10844 return;
10845 }
10846 if ((l = argvars[0].vval.v_list) != NULL)
10847 {
10848
10849 /* To some extent make sure that we are dealing with a list from
10850 * "getmatches()". */
10851 li = l->lv_first;
10852 while (li != NULL)
10853 {
10854 if (li->li_tv.v_type != VAR_DICT
10855 || (d = li->li_tv.vval.v_dict) == NULL)
10856 {
10857 EMSG(_(e_invarg));
10858 return;
10859 }
10860 if (!(dict_find(d, (char_u *)"group", -1) != NULL
10861 && (dict_find(d, (char_u *)"pattern", -1) != NULL
10862 || dict_find(d, (char_u *)"pos1", -1) != NULL)
10863 && dict_find(d, (char_u *)"priority", -1) != NULL
10864 && dict_find(d, (char_u *)"id", -1) != NULL))
10865 {
10866 EMSG(_(e_invarg));
10867 return;
10868 }
10869 li = li->li_next;
10870 }
10871
10872 clear_matches(curwin);
10873 li = l->lv_first;
10874 while (li != NULL)
10875 {
10876 int i = 0;
10877 char_u buf[5];
10878 dictitem_T *di;
10879 char_u *group;
10880 int priority;
10881 int id;
10882 char_u *conceal;
10883
10884 d = li->li_tv.vval.v_dict;
10885 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
10886 {
10887 if (s == NULL)
10888 {
10889 s = list_alloc();
10890 if (s == NULL)
10891 return;
10892 }
10893
10894 /* match from matchaddpos() */
10895 for (i = 1; i < 9; i++)
10896 {
10897 sprintf((char *)buf, (char *)"pos%d", i);
10898 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
10899 {
10900 if (di->di_tv.v_type != VAR_LIST)
10901 return;
10902
10903 list_append_tv(s, &di->di_tv);
10904 s->lv_refcount++;
10905 }
10906 else
10907 break;
10908 }
10909 }
10910
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010911 group = get_dict_string(d, (char_u *)"group", TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010912 priority = (int)get_dict_number(d, (char_u *)"priority");
10913 id = (int)get_dict_number(d, (char_u *)"id");
10914 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010915 ? get_dict_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010916 : NULL;
10917 if (i == 0)
10918 {
10919 match_add(curwin, group,
10920 get_dict_string(d, (char_u *)"pattern", FALSE),
10921 priority, id, NULL, conceal);
10922 }
10923 else
10924 {
10925 match_add(curwin, group, NULL, priority, id, s, conceal);
10926 list_unref(s);
10927 s = NULL;
10928 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010929 vim_free(group);
10930 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010931
10932 li = li->li_next;
10933 }
10934 rettv->vval.v_number = 0;
10935 }
10936#endif
10937}
10938
10939/*
10940 * "setpos()" function
10941 */
10942 static void
10943f_setpos(typval_T *argvars, typval_T *rettv)
10944{
10945 pos_T pos;
10946 int fnum;
10947 char_u *name;
10948 colnr_T curswant = -1;
10949
10950 rettv->vval.v_number = -1;
10951 name = get_tv_string_chk(argvars);
10952 if (name != NULL)
10953 {
10954 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
10955 {
10956 if (--pos.col < 0)
10957 pos.col = 0;
10958 if (name[0] == '.' && name[1] == NUL)
10959 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010960 /* set cursor; "fnum" is ignored */
10961 curwin->w_cursor = pos;
10962 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010963 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010964 curwin->w_curswant = curswant - 1;
10965 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010966 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010967 check_cursor();
10968 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010969 }
10970 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
10971 {
10972 /* set mark */
10973 if (setmark_pos(name[1], &pos, fnum) == OK)
10974 rettv->vval.v_number = 0;
10975 }
10976 else
10977 EMSG(_(e_invarg));
10978 }
10979 }
10980}
10981
10982/*
10983 * "setqflist()" function
10984 */
10985 static void
10986f_setqflist(typval_T *argvars, typval_T *rettv)
10987{
Bram Moolenaard823fa92016-08-12 16:29:27 +020010988 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010989}
10990
10991/*
10992 * "setreg()" function
10993 */
10994 static void
10995f_setreg(typval_T *argvars, typval_T *rettv)
10996{
10997 int regname;
10998 char_u *strregname;
10999 char_u *stropt;
11000 char_u *strval;
11001 int append;
11002 char_u yank_type;
11003 long block_len;
11004
11005 block_len = -1;
11006 yank_type = MAUTO;
11007 append = FALSE;
11008
11009 strregname = get_tv_string_chk(argvars);
11010 rettv->vval.v_number = 1; /* FAIL is default */
11011
11012 if (strregname == NULL)
11013 return; /* type error; errmsg already given */
11014 regname = *strregname;
11015 if (regname == 0 || regname == '@')
11016 regname = '"';
11017
11018 if (argvars[2].v_type != VAR_UNKNOWN)
11019 {
11020 stropt = get_tv_string_chk(&argvars[2]);
11021 if (stropt == NULL)
11022 return; /* type error */
11023 for (; *stropt != NUL; ++stropt)
11024 switch (*stropt)
11025 {
11026 case 'a': case 'A': /* append */
11027 append = TRUE;
11028 break;
11029 case 'v': case 'c': /* character-wise selection */
11030 yank_type = MCHAR;
11031 break;
11032 case 'V': case 'l': /* line-wise selection */
11033 yank_type = MLINE;
11034 break;
11035 case 'b': case Ctrl_V: /* block-wise selection */
11036 yank_type = MBLOCK;
11037 if (VIM_ISDIGIT(stropt[1]))
11038 {
11039 ++stropt;
11040 block_len = getdigits(&stropt) - 1;
11041 --stropt;
11042 }
11043 break;
11044 }
11045 }
11046
11047 if (argvars[1].v_type == VAR_LIST)
11048 {
11049 char_u **lstval;
11050 char_u **allocval;
11051 char_u buf[NUMBUFLEN];
11052 char_u **curval;
11053 char_u **curallocval;
11054 list_T *ll = argvars[1].vval.v_list;
11055 listitem_T *li;
11056 int len;
11057
11058 /* If the list is NULL handle like an empty list. */
11059 len = ll == NULL ? 0 : ll->lv_len;
11060
11061 /* First half: use for pointers to result lines; second half: use for
11062 * pointers to allocated copies. */
11063 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
11064 if (lstval == NULL)
11065 return;
11066 curval = lstval;
11067 allocval = lstval + len + 2;
11068 curallocval = allocval;
11069
11070 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
11071 li = li->li_next)
11072 {
11073 strval = get_tv_string_buf_chk(&li->li_tv, buf);
11074 if (strval == NULL)
11075 goto free_lstval;
11076 if (strval == buf)
11077 {
11078 /* Need to make a copy, next get_tv_string_buf_chk() will
11079 * overwrite the string. */
11080 strval = vim_strsave(buf);
11081 if (strval == NULL)
11082 goto free_lstval;
11083 *curallocval++ = strval;
11084 }
11085 *curval++ = strval;
11086 }
11087 *curval++ = NULL;
11088
11089 write_reg_contents_lst(regname, lstval, -1,
11090 append, yank_type, block_len);
11091free_lstval:
11092 while (curallocval > allocval)
11093 vim_free(*--curallocval);
11094 vim_free(lstval);
11095 }
11096 else
11097 {
11098 strval = get_tv_string_chk(&argvars[1]);
11099 if (strval == NULL)
11100 return;
11101 write_reg_contents_ex(regname, strval, -1,
11102 append, yank_type, block_len);
11103 }
11104 rettv->vval.v_number = 0;
11105}
11106
11107/*
11108 * "settabvar()" function
11109 */
11110 static void
11111f_settabvar(typval_T *argvars, typval_T *rettv)
11112{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011113 tabpage_T *save_curtab;
11114 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011115 char_u *varname, *tabvarname;
11116 typval_T *varp;
11117
11118 rettv->vval.v_number = 0;
11119
11120 if (check_restricted() || check_secure())
11121 return;
11122
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011123 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011124 varname = get_tv_string_chk(&argvars[1]);
11125 varp = &argvars[2];
11126
Bram Moolenaar4033c552017-09-16 20:54:51 +020011127 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011128 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011129 save_curtab = curtab;
11130 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011131
11132 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
11133 if (tabvarname != NULL)
11134 {
11135 STRCPY(tabvarname, "t:");
11136 STRCPY(tabvarname + 2, varname);
11137 set_var(tabvarname, varp, TRUE);
11138 vim_free(tabvarname);
11139 }
11140
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011141 /* Restore current tabpage */
11142 if (valid_tabpage(save_curtab))
11143 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011144 }
11145}
11146
11147/*
11148 * "settabwinvar()" function
11149 */
11150 static void
11151f_settabwinvar(typval_T *argvars, typval_T *rettv)
11152{
11153 setwinvar(argvars, rettv, 1);
11154}
11155
11156/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011157 * "settagstack()" function
11158 */
11159 static void
11160f_settagstack(typval_T *argvars, typval_T *rettv)
11161{
11162 static char *e_invact2 = N_("E962: Invalid action: '%s'");
11163 win_T *wp;
11164 dict_T *d;
11165 int action = 'r';
11166
11167 rettv->vval.v_number = -1;
11168
11169 // first argument: window number or id
11170 wp = find_win_by_nr_or_id(&argvars[0]);
11171 if (wp == NULL)
11172 return;
11173
11174 // second argument: dict with items to set in the tag stack
11175 if (argvars[1].v_type != VAR_DICT)
11176 {
11177 EMSG(_(e_dictreq));
11178 return;
11179 }
11180 d = argvars[1].vval.v_dict;
11181 if (d == NULL)
11182 return;
11183
11184 // third argument: action - 'a' for append and 'r' for replace.
11185 // default is to replace the stack.
11186 if (argvars[2].v_type == VAR_UNKNOWN)
11187 action = 'r';
11188 else if (argvars[2].v_type == VAR_STRING)
11189 {
11190 char_u *actstr;
11191 actstr = get_tv_string_chk(&argvars[2]);
11192 if (actstr == NULL)
11193 return;
11194 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
11195 action = *actstr;
11196 else
11197 {
11198 EMSG2(_(e_invact2), actstr);
11199 return;
11200 }
11201 }
11202 else
11203 {
11204 EMSG(_(e_stringreq));
11205 return;
11206 }
11207
11208 if (set_tagstack(wp, d, action) == OK)
11209 rettv->vval.v_number = 0;
11210}
11211
11212/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011213 * "setwinvar()" function
11214 */
11215 static void
11216f_setwinvar(typval_T *argvars, typval_T *rettv)
11217{
11218 setwinvar(argvars, rettv, 0);
11219}
11220
11221#ifdef FEAT_CRYPT
11222/*
11223 * "sha256({string})" function
11224 */
11225 static void
11226f_sha256(typval_T *argvars, typval_T *rettv)
11227{
11228 char_u *p;
11229
11230 p = get_tv_string(&argvars[0]);
11231 rettv->vval.v_string = vim_strsave(
11232 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
11233 rettv->v_type = VAR_STRING;
11234}
11235#endif /* FEAT_CRYPT */
11236
11237/*
11238 * "shellescape({string})" function
11239 */
11240 static void
11241f_shellescape(typval_T *argvars, typval_T *rettv)
11242{
Bram Moolenaar20615522017-06-05 18:46:26 +020011243 int do_special = non_zero_arg(&argvars[1]);
11244
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011245 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar20615522017-06-05 18:46:26 +020011246 get_tv_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011247 rettv->v_type = VAR_STRING;
11248}
11249
11250/*
11251 * shiftwidth() function
11252 */
11253 static void
11254f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
11255{
Bram Moolenaarf9514162018-11-22 03:08:29 +010011256 rettv->vval.v_number = 0;
11257
11258 if (argvars[0].v_type != VAR_UNKNOWN)
11259 {
11260 long col;
11261
11262 col = (long)get_tv_number_chk(argvars, NULL);
11263 if (col < 0)
11264 return; // type error; errmsg already given
11265#ifdef FEAT_VARTABS
11266 rettv->vval.v_number = get_sw_value_col(curbuf, col);
11267 return;
11268#endif
11269 }
11270
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011271 rettv->vval.v_number = get_sw_value(curbuf);
11272}
11273
11274/*
11275 * "simplify()" function
11276 */
11277 static void
11278f_simplify(typval_T *argvars, typval_T *rettv)
11279{
11280 char_u *p;
11281
11282 p = get_tv_string(&argvars[0]);
11283 rettv->vval.v_string = vim_strsave(p);
11284 simplify_filename(rettv->vval.v_string); /* simplify in place */
11285 rettv->v_type = VAR_STRING;
11286}
11287
11288#ifdef FEAT_FLOAT
11289/*
11290 * "sin()" function
11291 */
11292 static void
11293f_sin(typval_T *argvars, typval_T *rettv)
11294{
11295 float_T f = 0.0;
11296
11297 rettv->v_type = VAR_FLOAT;
11298 if (get_float_arg(argvars, &f) == OK)
11299 rettv->vval.v_float = sin(f);
11300 else
11301 rettv->vval.v_float = 0.0;
11302}
11303
11304/*
11305 * "sinh()" function
11306 */
11307 static void
11308f_sinh(typval_T *argvars, typval_T *rettv)
11309{
11310 float_T f = 0.0;
11311
11312 rettv->v_type = VAR_FLOAT;
11313 if (get_float_arg(argvars, &f) == OK)
11314 rettv->vval.v_float = sinh(f);
11315 else
11316 rettv->vval.v_float = 0.0;
11317}
11318#endif
11319
11320static int
11321#ifdef __BORLANDC__
11322 _RTLENTRYF
11323#endif
11324 item_compare(const void *s1, const void *s2);
11325static int
11326#ifdef __BORLANDC__
11327 _RTLENTRYF
11328#endif
11329 item_compare2(const void *s1, const void *s2);
11330
11331/* struct used in the array that's given to qsort() */
11332typedef struct
11333{
11334 listitem_T *item;
11335 int idx;
11336} sortItem_T;
11337
11338/* struct storing information about current sort */
11339typedef struct
11340{
11341 int item_compare_ic;
11342 int item_compare_numeric;
11343 int item_compare_numbers;
11344#ifdef FEAT_FLOAT
11345 int item_compare_float;
11346#endif
11347 char_u *item_compare_func;
11348 partial_T *item_compare_partial;
11349 dict_T *item_compare_selfdict;
11350 int item_compare_func_err;
11351 int item_compare_keep_zero;
11352} sortinfo_T;
11353static sortinfo_T *sortinfo = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011354#define ITEM_COMPARE_FAIL 999
11355
11356/*
11357 * Compare functions for f_sort() and f_uniq() below.
11358 */
11359 static int
11360#ifdef __BORLANDC__
11361_RTLENTRYF
11362#endif
11363item_compare(const void *s1, const void *s2)
11364{
11365 sortItem_T *si1, *si2;
11366 typval_T *tv1, *tv2;
11367 char_u *p1, *p2;
11368 char_u *tofree1 = NULL, *tofree2 = NULL;
11369 int res;
11370 char_u numbuf1[NUMBUFLEN];
11371 char_u numbuf2[NUMBUFLEN];
11372
11373 si1 = (sortItem_T *)s1;
11374 si2 = (sortItem_T *)s2;
11375 tv1 = &si1->item->li_tv;
11376 tv2 = &si2->item->li_tv;
11377
11378 if (sortinfo->item_compare_numbers)
11379 {
11380 varnumber_T v1 = get_tv_number(tv1);
11381 varnumber_T v2 = get_tv_number(tv2);
11382
11383 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11384 }
11385
11386#ifdef FEAT_FLOAT
11387 if (sortinfo->item_compare_float)
11388 {
11389 float_T v1 = get_tv_float(tv1);
11390 float_T v2 = get_tv_float(tv2);
11391
11392 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11393 }
11394#endif
11395
11396 /* tv2string() puts quotes around a string and allocates memory. Don't do
11397 * that for string variables. Use a single quote when comparing with a
11398 * non-string to do what the docs promise. */
11399 if (tv1->v_type == VAR_STRING)
11400 {
11401 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11402 p1 = (char_u *)"'";
11403 else
11404 p1 = tv1->vval.v_string;
11405 }
11406 else
11407 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
11408 if (tv2->v_type == VAR_STRING)
11409 {
11410 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11411 p2 = (char_u *)"'";
11412 else
11413 p2 = tv2->vval.v_string;
11414 }
11415 else
11416 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
11417 if (p1 == NULL)
11418 p1 = (char_u *)"";
11419 if (p2 == NULL)
11420 p2 = (char_u *)"";
11421 if (!sortinfo->item_compare_numeric)
11422 {
11423 if (sortinfo->item_compare_ic)
11424 res = STRICMP(p1, p2);
11425 else
11426 res = STRCMP(p1, p2);
11427 }
11428 else
11429 {
11430 double n1, n2;
11431 n1 = strtod((char *)p1, (char **)&p1);
11432 n2 = strtod((char *)p2, (char **)&p2);
11433 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
11434 }
11435
11436 /* When the result would be zero, compare the item indexes. Makes the
11437 * sort stable. */
11438 if (res == 0 && !sortinfo->item_compare_keep_zero)
11439 res = si1->idx > si2->idx ? 1 : -1;
11440
11441 vim_free(tofree1);
11442 vim_free(tofree2);
11443 return res;
11444}
11445
11446 static int
11447#ifdef __BORLANDC__
11448_RTLENTRYF
11449#endif
11450item_compare2(const void *s1, const void *s2)
11451{
11452 sortItem_T *si1, *si2;
11453 int res;
11454 typval_T rettv;
11455 typval_T argv[3];
11456 int dummy;
11457 char_u *func_name;
11458 partial_T *partial = sortinfo->item_compare_partial;
11459
11460 /* shortcut after failure in previous call; compare all items equal */
11461 if (sortinfo->item_compare_func_err)
11462 return 0;
11463
11464 si1 = (sortItem_T *)s1;
11465 si2 = (sortItem_T *)s2;
11466
11467 if (partial == NULL)
11468 func_name = sortinfo->item_compare_func;
11469 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020011470 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011471
11472 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
11473 * in the copy without changing the original list items. */
11474 copy_tv(&si1->item->li_tv, &argv[0]);
11475 copy_tv(&si2->item->li_tv, &argv[1]);
11476
11477 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
11478 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020011479 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011480 partial, sortinfo->item_compare_selfdict);
11481 clear_tv(&argv[0]);
11482 clear_tv(&argv[1]);
11483
11484 if (res == FAIL)
11485 res = ITEM_COMPARE_FAIL;
11486 else
11487 res = (int)get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
11488 if (sortinfo->item_compare_func_err)
11489 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
11490 clear_tv(&rettv);
11491
11492 /* When the result would be zero, compare the pointers themselves. Makes
11493 * the sort stable. */
11494 if (res == 0 && !sortinfo->item_compare_keep_zero)
11495 res = si1->idx > si2->idx ? 1 : -1;
11496
11497 return res;
11498}
11499
11500/*
11501 * "sort({list})" function
11502 */
11503 static void
11504do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
11505{
11506 list_T *l;
11507 listitem_T *li;
11508 sortItem_T *ptrs;
11509 sortinfo_T *old_sortinfo;
11510 sortinfo_T info;
11511 long len;
11512 long i;
11513
11514 /* Pointer to current info struct used in compare function. Save and
11515 * restore the current one for nested calls. */
11516 old_sortinfo = sortinfo;
11517 sortinfo = &info;
11518
11519 if (argvars[0].v_type != VAR_LIST)
11520 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
11521 else
11522 {
11523 l = argvars[0].vval.v_list;
11524 if (l == NULL || tv_check_lock(l->lv_lock,
11525 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
11526 TRUE))
11527 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011528 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011529
11530 len = list_len(l);
11531 if (len <= 1)
11532 goto theend; /* short list sorts pretty quickly */
11533
11534 info.item_compare_ic = FALSE;
11535 info.item_compare_numeric = FALSE;
11536 info.item_compare_numbers = FALSE;
11537#ifdef FEAT_FLOAT
11538 info.item_compare_float = FALSE;
11539#endif
11540 info.item_compare_func = NULL;
11541 info.item_compare_partial = NULL;
11542 info.item_compare_selfdict = NULL;
11543 if (argvars[1].v_type != VAR_UNKNOWN)
11544 {
11545 /* optional second argument: {func} */
11546 if (argvars[1].v_type == VAR_FUNC)
11547 info.item_compare_func = argvars[1].vval.v_string;
11548 else if (argvars[1].v_type == VAR_PARTIAL)
11549 info.item_compare_partial = argvars[1].vval.v_partial;
11550 else
11551 {
11552 int error = FALSE;
11553
11554 i = (long)get_tv_number_chk(&argvars[1], &error);
11555 if (error)
11556 goto theend; /* type error; errmsg already given */
11557 if (i == 1)
11558 info.item_compare_ic = TRUE;
11559 else if (argvars[1].v_type != VAR_NUMBER)
11560 info.item_compare_func = get_tv_string(&argvars[1]);
11561 else if (i != 0)
11562 {
11563 EMSG(_(e_invarg));
11564 goto theend;
11565 }
11566 if (info.item_compare_func != NULL)
11567 {
11568 if (*info.item_compare_func == NUL)
11569 {
11570 /* empty string means default sort */
11571 info.item_compare_func = NULL;
11572 }
11573 else if (STRCMP(info.item_compare_func, "n") == 0)
11574 {
11575 info.item_compare_func = NULL;
11576 info.item_compare_numeric = TRUE;
11577 }
11578 else if (STRCMP(info.item_compare_func, "N") == 0)
11579 {
11580 info.item_compare_func = NULL;
11581 info.item_compare_numbers = TRUE;
11582 }
11583#ifdef FEAT_FLOAT
11584 else if (STRCMP(info.item_compare_func, "f") == 0)
11585 {
11586 info.item_compare_func = NULL;
11587 info.item_compare_float = TRUE;
11588 }
11589#endif
11590 else if (STRCMP(info.item_compare_func, "i") == 0)
11591 {
11592 info.item_compare_func = NULL;
11593 info.item_compare_ic = TRUE;
11594 }
11595 }
11596 }
11597
11598 if (argvars[2].v_type != VAR_UNKNOWN)
11599 {
11600 /* optional third argument: {dict} */
11601 if (argvars[2].v_type != VAR_DICT)
11602 {
11603 EMSG(_(e_dictreq));
11604 goto theend;
11605 }
11606 info.item_compare_selfdict = argvars[2].vval.v_dict;
11607 }
11608 }
11609
11610 /* Make an array with each entry pointing to an item in the List. */
11611 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
11612 if (ptrs == NULL)
11613 goto theend;
11614
11615 i = 0;
11616 if (sort)
11617 {
11618 /* sort(): ptrs will be the list to sort */
11619 for (li = l->lv_first; li != NULL; li = li->li_next)
11620 {
11621 ptrs[i].item = li;
11622 ptrs[i].idx = i;
11623 ++i;
11624 }
11625
11626 info.item_compare_func_err = FALSE;
11627 info.item_compare_keep_zero = FALSE;
11628 /* test the compare function */
11629 if ((info.item_compare_func != NULL
11630 || info.item_compare_partial != NULL)
11631 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
11632 == ITEM_COMPARE_FAIL)
11633 EMSG(_("E702: Sort compare function failed"));
11634 else
11635 {
11636 /* Sort the array with item pointers. */
11637 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
11638 info.item_compare_func == NULL
11639 && info.item_compare_partial == NULL
11640 ? item_compare : item_compare2);
11641
11642 if (!info.item_compare_func_err)
11643 {
11644 /* Clear the List and append the items in sorted order. */
11645 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
11646 l->lv_len = 0;
11647 for (i = 0; i < len; ++i)
11648 list_append(l, ptrs[i].item);
11649 }
11650 }
11651 }
11652 else
11653 {
11654 int (*item_compare_func_ptr)(const void *, const void *);
11655
11656 /* f_uniq(): ptrs will be a stack of items to remove */
11657 info.item_compare_func_err = FALSE;
11658 info.item_compare_keep_zero = TRUE;
11659 item_compare_func_ptr = info.item_compare_func != NULL
11660 || info.item_compare_partial != NULL
11661 ? item_compare2 : item_compare;
11662
11663 for (li = l->lv_first; li != NULL && li->li_next != NULL;
11664 li = li->li_next)
11665 {
11666 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
11667 == 0)
11668 ptrs[i++].item = li;
11669 if (info.item_compare_func_err)
11670 {
11671 EMSG(_("E882: Uniq compare function failed"));
11672 break;
11673 }
11674 }
11675
11676 if (!info.item_compare_func_err)
11677 {
11678 while (--i >= 0)
11679 {
11680 li = ptrs[i].item->li_next;
11681 ptrs[i].item->li_next = li->li_next;
11682 if (li->li_next != NULL)
11683 li->li_next->li_prev = ptrs[i].item;
11684 else
11685 l->lv_last = ptrs[i].item;
11686 list_fix_watch(l, li);
11687 listitem_free(li);
11688 l->lv_len--;
11689 }
11690 }
11691 }
11692
11693 vim_free(ptrs);
11694 }
11695theend:
11696 sortinfo = old_sortinfo;
11697}
11698
11699/*
11700 * "sort({list})" function
11701 */
11702 static void
11703f_sort(typval_T *argvars, typval_T *rettv)
11704{
11705 do_sort_uniq(argvars, rettv, TRUE);
11706}
11707
11708/*
11709 * "uniq({list})" function
11710 */
11711 static void
11712f_uniq(typval_T *argvars, typval_T *rettv)
11713{
11714 do_sort_uniq(argvars, rettv, FALSE);
11715}
11716
11717/*
11718 * "soundfold({word})" function
11719 */
11720 static void
11721f_soundfold(typval_T *argvars, typval_T *rettv)
11722{
11723 char_u *s;
11724
11725 rettv->v_type = VAR_STRING;
11726 s = get_tv_string(&argvars[0]);
11727#ifdef FEAT_SPELL
11728 rettv->vval.v_string = eval_soundfold(s);
11729#else
11730 rettv->vval.v_string = vim_strsave(s);
11731#endif
11732}
11733
11734/*
11735 * "spellbadword()" function
11736 */
11737 static void
11738f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
11739{
11740 char_u *word = (char_u *)"";
11741 hlf_T attr = HLF_COUNT;
11742 int len = 0;
11743
11744 if (rettv_list_alloc(rettv) == FAIL)
11745 return;
11746
11747#ifdef FEAT_SPELL
11748 if (argvars[0].v_type == VAR_UNKNOWN)
11749 {
11750 /* Find the start and length of the badly spelled word. */
11751 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
11752 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011753 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011754 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011755 curwin->w_set_curswant = TRUE;
11756 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011757 }
11758 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
11759 {
11760 char_u *str = get_tv_string_chk(&argvars[0]);
11761 int capcol = -1;
11762
11763 if (str != NULL)
11764 {
11765 /* Check the argument for spelling. */
11766 while (*str != NUL)
11767 {
11768 len = spell_check(curwin, str, &attr, &capcol, FALSE);
11769 if (attr != HLF_COUNT)
11770 {
11771 word = str;
11772 break;
11773 }
11774 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020011775 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011776 }
11777 }
11778 }
11779#endif
11780
11781 list_append_string(rettv->vval.v_list, word, len);
11782 list_append_string(rettv->vval.v_list, (char_u *)(
11783 attr == HLF_SPB ? "bad" :
11784 attr == HLF_SPR ? "rare" :
11785 attr == HLF_SPL ? "local" :
11786 attr == HLF_SPC ? "caps" :
11787 ""), -1);
11788}
11789
11790/*
11791 * "spellsuggest()" function
11792 */
11793 static void
11794f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
11795{
11796#ifdef FEAT_SPELL
11797 char_u *str;
11798 int typeerr = FALSE;
11799 int maxcount;
11800 garray_T ga;
11801 int i;
11802 listitem_T *li;
11803 int need_capital = FALSE;
11804#endif
11805
11806 if (rettv_list_alloc(rettv) == FAIL)
11807 return;
11808
11809#ifdef FEAT_SPELL
11810 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
11811 {
11812 str = get_tv_string(&argvars[0]);
11813 if (argvars[1].v_type != VAR_UNKNOWN)
11814 {
11815 maxcount = (int)get_tv_number_chk(&argvars[1], &typeerr);
11816 if (maxcount <= 0)
11817 return;
11818 if (argvars[2].v_type != VAR_UNKNOWN)
11819 {
11820 need_capital = (int)get_tv_number_chk(&argvars[2], &typeerr);
11821 if (typeerr)
11822 return;
11823 }
11824 }
11825 else
11826 maxcount = 25;
11827
11828 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
11829
11830 for (i = 0; i < ga.ga_len; ++i)
11831 {
11832 str = ((char_u **)ga.ga_data)[i];
11833
11834 li = listitem_alloc();
11835 if (li == NULL)
11836 vim_free(str);
11837 else
11838 {
11839 li->li_tv.v_type = VAR_STRING;
11840 li->li_tv.v_lock = 0;
11841 li->li_tv.vval.v_string = str;
11842 list_append(rettv->vval.v_list, li);
11843 }
11844 }
11845 ga_clear(&ga);
11846 }
11847#endif
11848}
11849
11850 static void
11851f_split(typval_T *argvars, typval_T *rettv)
11852{
11853 char_u *str;
11854 char_u *end;
11855 char_u *pat = NULL;
11856 regmatch_T regmatch;
11857 char_u patbuf[NUMBUFLEN];
11858 char_u *save_cpo;
11859 int match;
11860 colnr_T col = 0;
11861 int keepempty = FALSE;
11862 int typeerr = FALSE;
11863
11864 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11865 save_cpo = p_cpo;
11866 p_cpo = (char_u *)"";
11867
11868 str = get_tv_string(&argvars[0]);
11869 if (argvars[1].v_type != VAR_UNKNOWN)
11870 {
11871 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11872 if (pat == NULL)
11873 typeerr = TRUE;
11874 if (argvars[2].v_type != VAR_UNKNOWN)
11875 keepempty = (int)get_tv_number_chk(&argvars[2], &typeerr);
11876 }
11877 if (pat == NULL || *pat == NUL)
11878 pat = (char_u *)"[\\x01- ]\\+";
11879
11880 if (rettv_list_alloc(rettv) == FAIL)
11881 return;
11882 if (typeerr)
11883 return;
11884
11885 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11886 if (regmatch.regprog != NULL)
11887 {
11888 regmatch.rm_ic = FALSE;
11889 while (*str != NUL || keepempty)
11890 {
11891 if (*str == NUL)
11892 match = FALSE; /* empty item at the end */
11893 else
11894 match = vim_regexec_nl(&regmatch, str, col);
11895 if (match)
11896 end = regmatch.startp[0];
11897 else
11898 end = str + STRLEN(str);
11899 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
11900 && *str != NUL && match && end < regmatch.endp[0]))
11901 {
11902 if (list_append_string(rettv->vval.v_list, str,
11903 (int)(end - str)) == FAIL)
11904 break;
11905 }
11906 if (!match)
11907 break;
11908 /* Advance to just after the match. */
11909 if (regmatch.endp[0] > str)
11910 col = 0;
11911 else
11912 {
11913 /* Don't get stuck at the same match. */
11914#ifdef FEAT_MBYTE
11915 col = (*mb_ptr2len)(regmatch.endp[0]);
11916#else
11917 col = 1;
11918#endif
11919 }
11920 str = regmatch.endp[0];
11921 }
11922
11923 vim_regfree(regmatch.regprog);
11924 }
11925
11926 p_cpo = save_cpo;
11927}
11928
11929#ifdef FEAT_FLOAT
11930/*
11931 * "sqrt()" function
11932 */
11933 static void
11934f_sqrt(typval_T *argvars, typval_T *rettv)
11935{
11936 float_T f = 0.0;
11937
11938 rettv->v_type = VAR_FLOAT;
11939 if (get_float_arg(argvars, &f) == OK)
11940 rettv->vval.v_float = sqrt(f);
11941 else
11942 rettv->vval.v_float = 0.0;
11943}
11944
11945/*
11946 * "str2float()" function
11947 */
11948 static void
11949f_str2float(typval_T *argvars, typval_T *rettv)
11950{
11951 char_u *p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011952 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011953
Bram Moolenaar08243d22017-01-10 16:12:29 +010011954 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011955 p = skipwhite(p + 1);
11956 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011957 if (isneg)
11958 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011959 rettv->v_type = VAR_FLOAT;
11960}
11961#endif
11962
11963/*
11964 * "str2nr()" function
11965 */
11966 static void
11967f_str2nr(typval_T *argvars, typval_T *rettv)
11968{
11969 int base = 10;
11970 char_u *p;
11971 varnumber_T n;
11972 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010011973 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011974
11975 if (argvars[1].v_type != VAR_UNKNOWN)
11976 {
11977 base = (int)get_tv_number(&argvars[1]);
11978 if (base != 2 && base != 8 && base != 10 && base != 16)
11979 {
11980 EMSG(_(e_invarg));
11981 return;
11982 }
11983 }
11984
11985 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011986 isneg = (*p == '-');
11987 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011988 p = skipwhite(p + 1);
11989 switch (base)
11990 {
11991 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
11992 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
11993 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
11994 default: what = 0;
11995 }
11996 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011997 if (isneg)
11998 rettv->vval.v_number = -n;
11999 else
12000 rettv->vval.v_number = n;
12001
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012002}
12003
12004#ifdef HAVE_STRFTIME
12005/*
12006 * "strftime({format}[, {time}])" function
12007 */
12008 static void
12009f_strftime(typval_T *argvars, typval_T *rettv)
12010{
12011 char_u result_buf[256];
12012 struct tm *curtime;
12013 time_t seconds;
12014 char_u *p;
12015
12016 rettv->v_type = VAR_STRING;
12017
12018 p = get_tv_string(&argvars[0]);
12019 if (argvars[1].v_type == VAR_UNKNOWN)
12020 seconds = time(NULL);
12021 else
12022 seconds = (time_t)get_tv_number(&argvars[1]);
12023 curtime = localtime(&seconds);
12024 /* MSVC returns NULL for an invalid value of seconds. */
12025 if (curtime == NULL)
12026 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
12027 else
12028 {
12029# ifdef FEAT_MBYTE
12030 vimconv_T conv;
12031 char_u *enc;
12032
12033 conv.vc_type = CONV_NONE;
12034 enc = enc_locale();
12035 convert_setup(&conv, p_enc, enc);
12036 if (conv.vc_type != CONV_NONE)
12037 p = string_convert(&conv, p, NULL);
12038# endif
12039 if (p != NULL)
12040 (void)strftime((char *)result_buf, sizeof(result_buf),
12041 (char *)p, curtime);
12042 else
12043 result_buf[0] = NUL;
12044
12045# ifdef FEAT_MBYTE
12046 if (conv.vc_type != CONV_NONE)
12047 vim_free(p);
12048 convert_setup(&conv, enc, p_enc);
12049 if (conv.vc_type != CONV_NONE)
12050 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
12051 else
12052# endif
12053 rettv->vval.v_string = vim_strsave(result_buf);
12054
12055# ifdef FEAT_MBYTE
12056 /* Release conversion descriptors */
12057 convert_setup(&conv, NULL, NULL);
12058 vim_free(enc);
12059# endif
12060 }
12061}
12062#endif
12063
12064/*
12065 * "strgetchar()" function
12066 */
12067 static void
12068f_strgetchar(typval_T *argvars, typval_T *rettv)
12069{
12070 char_u *str;
12071 int len;
12072 int error = FALSE;
12073 int charidx;
12074
12075 rettv->vval.v_number = -1;
12076 str = get_tv_string_chk(&argvars[0]);
12077 if (str == NULL)
12078 return;
12079 len = (int)STRLEN(str);
12080 charidx = (int)get_tv_number_chk(&argvars[1], &error);
12081 if (error)
12082 return;
12083#ifdef FEAT_MBYTE
12084 {
12085 int byteidx = 0;
12086
12087 while (charidx >= 0 && byteidx < len)
12088 {
12089 if (charidx == 0)
12090 {
12091 rettv->vval.v_number = mb_ptr2char(str + byteidx);
12092 break;
12093 }
12094 --charidx;
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012095 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012096 }
12097 }
12098#else
12099 if (charidx < len)
12100 rettv->vval.v_number = str[charidx];
12101#endif
12102}
12103
12104/*
12105 * "stridx()" function
12106 */
12107 static void
12108f_stridx(typval_T *argvars, typval_T *rettv)
12109{
12110 char_u buf[NUMBUFLEN];
12111 char_u *needle;
12112 char_u *haystack;
12113 char_u *save_haystack;
12114 char_u *pos;
12115 int start_idx;
12116
12117 needle = get_tv_string_chk(&argvars[1]);
12118 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
12119 rettv->vval.v_number = -1;
12120 if (needle == NULL || haystack == NULL)
12121 return; /* type error; errmsg already given */
12122
12123 if (argvars[2].v_type != VAR_UNKNOWN)
12124 {
12125 int error = FALSE;
12126
12127 start_idx = (int)get_tv_number_chk(&argvars[2], &error);
12128 if (error || start_idx >= (int)STRLEN(haystack))
12129 return;
12130 if (start_idx >= 0)
12131 haystack += start_idx;
12132 }
12133
12134 pos = (char_u *)strstr((char *)haystack, (char *)needle);
12135 if (pos != NULL)
12136 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
12137}
12138
12139/*
12140 * "string()" function
12141 */
12142 static void
12143f_string(typval_T *argvars, typval_T *rettv)
12144{
12145 char_u *tofree;
12146 char_u numbuf[NUMBUFLEN];
12147
12148 rettv->v_type = VAR_STRING;
12149 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
12150 get_copyID());
12151 /* Make a copy if we have a value but it's not in allocated memory. */
12152 if (rettv->vval.v_string != NULL && tofree == NULL)
12153 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
12154}
12155
12156/*
12157 * "strlen()" function
12158 */
12159 static void
12160f_strlen(typval_T *argvars, typval_T *rettv)
12161{
12162 rettv->vval.v_number = (varnumber_T)(STRLEN(
12163 get_tv_string(&argvars[0])));
12164}
12165
12166/*
12167 * "strchars()" function
12168 */
12169 static void
12170f_strchars(typval_T *argvars, typval_T *rettv)
12171{
12172 char_u *s = get_tv_string(&argvars[0]);
12173 int skipcc = 0;
12174#ifdef FEAT_MBYTE
12175 varnumber_T len = 0;
12176 int (*func_mb_ptr2char_adv)(char_u **pp);
12177#endif
12178
12179 if (argvars[1].v_type != VAR_UNKNOWN)
12180 skipcc = (int)get_tv_number_chk(&argvars[1], NULL);
12181 if (skipcc < 0 || skipcc > 1)
12182 EMSG(_(e_invarg));
12183 else
12184 {
12185#ifdef FEAT_MBYTE
12186 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
12187 while (*s != NUL)
12188 {
12189 func_mb_ptr2char_adv(&s);
12190 ++len;
12191 }
12192 rettv->vval.v_number = len;
12193#else
12194 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
12195#endif
12196 }
12197}
12198
12199/*
12200 * "strdisplaywidth()" function
12201 */
12202 static void
12203f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
12204{
12205 char_u *s = get_tv_string(&argvars[0]);
12206 int col = 0;
12207
12208 if (argvars[1].v_type != VAR_UNKNOWN)
12209 col = (int)get_tv_number(&argvars[1]);
12210
12211 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
12212}
12213
12214/*
12215 * "strwidth()" function
12216 */
12217 static void
12218f_strwidth(typval_T *argvars, typval_T *rettv)
12219{
12220 char_u *s = get_tv_string(&argvars[0]);
12221
12222 rettv->vval.v_number = (varnumber_T)(
12223#ifdef FEAT_MBYTE
12224 mb_string2cells(s, -1)
12225#else
12226 STRLEN(s)
12227#endif
12228 );
12229}
12230
12231/*
12232 * "strcharpart()" function
12233 */
12234 static void
12235f_strcharpart(typval_T *argvars, typval_T *rettv)
12236{
12237#ifdef FEAT_MBYTE
12238 char_u *p;
12239 int nchar;
12240 int nbyte = 0;
12241 int charlen;
12242 int len = 0;
12243 int slen;
12244 int error = FALSE;
12245
12246 p = get_tv_string(&argvars[0]);
12247 slen = (int)STRLEN(p);
12248
12249 nchar = (int)get_tv_number_chk(&argvars[1], &error);
12250 if (!error)
12251 {
12252 if (nchar > 0)
12253 while (nchar > 0 && nbyte < slen)
12254 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012255 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012256 --nchar;
12257 }
12258 else
12259 nbyte = nchar;
12260 if (argvars[2].v_type != VAR_UNKNOWN)
12261 {
12262 charlen = (int)get_tv_number(&argvars[2]);
12263 while (charlen > 0 && nbyte + len < slen)
12264 {
12265 int off = nbyte + len;
12266
12267 if (off < 0)
12268 len += 1;
12269 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012270 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012271 --charlen;
12272 }
12273 }
12274 else
12275 len = slen - nbyte; /* default: all bytes that are available. */
12276 }
12277
12278 /*
12279 * Only return the overlap between the specified part and the actual
12280 * string.
12281 */
12282 if (nbyte < 0)
12283 {
12284 len += nbyte;
12285 nbyte = 0;
12286 }
12287 else if (nbyte > slen)
12288 nbyte = slen;
12289 if (len < 0)
12290 len = 0;
12291 else if (nbyte + len > slen)
12292 len = slen - nbyte;
12293
12294 rettv->v_type = VAR_STRING;
12295 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
12296#else
12297 f_strpart(argvars, rettv);
12298#endif
12299}
12300
12301/*
12302 * "strpart()" function
12303 */
12304 static void
12305f_strpart(typval_T *argvars, typval_T *rettv)
12306{
12307 char_u *p;
12308 int n;
12309 int len;
12310 int slen;
12311 int error = FALSE;
12312
12313 p = get_tv_string(&argvars[0]);
12314 slen = (int)STRLEN(p);
12315
12316 n = (int)get_tv_number_chk(&argvars[1], &error);
12317 if (error)
12318 len = 0;
12319 else if (argvars[2].v_type != VAR_UNKNOWN)
12320 len = (int)get_tv_number(&argvars[2]);
12321 else
12322 len = slen - n; /* default len: all bytes that are available. */
12323
12324 /*
12325 * Only return the overlap between the specified part and the actual
12326 * string.
12327 */
12328 if (n < 0)
12329 {
12330 len += n;
12331 n = 0;
12332 }
12333 else if (n > slen)
12334 n = slen;
12335 if (len < 0)
12336 len = 0;
12337 else if (n + len > slen)
12338 len = slen - n;
12339
12340 rettv->v_type = VAR_STRING;
12341 rettv->vval.v_string = vim_strnsave(p + n, len);
12342}
12343
12344/*
12345 * "strridx()" function
12346 */
12347 static void
12348f_strridx(typval_T *argvars, typval_T *rettv)
12349{
12350 char_u buf[NUMBUFLEN];
12351 char_u *needle;
12352 char_u *haystack;
12353 char_u *rest;
12354 char_u *lastmatch = NULL;
12355 int haystack_len, end_idx;
12356
12357 needle = get_tv_string_chk(&argvars[1]);
12358 haystack = get_tv_string_buf_chk(&argvars[0], buf);
12359
12360 rettv->vval.v_number = -1;
12361 if (needle == NULL || haystack == NULL)
12362 return; /* type error; errmsg already given */
12363
12364 haystack_len = (int)STRLEN(haystack);
12365 if (argvars[2].v_type != VAR_UNKNOWN)
12366 {
12367 /* Third argument: upper limit for index */
12368 end_idx = (int)get_tv_number_chk(&argvars[2], NULL);
12369 if (end_idx < 0)
12370 return; /* can never find a match */
12371 }
12372 else
12373 end_idx = haystack_len;
12374
12375 if (*needle == NUL)
12376 {
12377 /* Empty string matches past the end. */
12378 lastmatch = haystack + end_idx;
12379 }
12380 else
12381 {
12382 for (rest = haystack; *rest != '\0'; ++rest)
12383 {
12384 rest = (char_u *)strstr((char *)rest, (char *)needle);
12385 if (rest == NULL || rest > haystack + end_idx)
12386 break;
12387 lastmatch = rest;
12388 }
12389 }
12390
12391 if (lastmatch == NULL)
12392 rettv->vval.v_number = -1;
12393 else
12394 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
12395}
12396
12397/*
12398 * "strtrans()" function
12399 */
12400 static void
12401f_strtrans(typval_T *argvars, typval_T *rettv)
12402{
12403 rettv->v_type = VAR_STRING;
12404 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
12405}
12406
12407/*
12408 * "submatch()" function
12409 */
12410 static void
12411f_submatch(typval_T *argvars, typval_T *rettv)
12412{
12413 int error = FALSE;
12414 int no;
12415 int retList = 0;
12416
12417 no = (int)get_tv_number_chk(&argvars[0], &error);
12418 if (error)
12419 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012420 if (no < 0 || no >= NSUBEXP)
12421 {
Bram Moolenaar79518e22017-02-17 16:31:35 +010012422 EMSGN(_("E935: invalid submatch number: %d"), no);
12423 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012424 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012425 if (argvars[1].v_type != VAR_UNKNOWN)
12426 retList = (int)get_tv_number_chk(&argvars[1], &error);
12427 if (error)
12428 return;
12429
12430 if (retList == 0)
12431 {
12432 rettv->v_type = VAR_STRING;
12433 rettv->vval.v_string = reg_submatch(no);
12434 }
12435 else
12436 {
12437 rettv->v_type = VAR_LIST;
12438 rettv->vval.v_list = reg_submatch_list(no);
12439 }
12440}
12441
12442/*
12443 * "substitute()" function
12444 */
12445 static void
12446f_substitute(typval_T *argvars, typval_T *rettv)
12447{
12448 char_u patbuf[NUMBUFLEN];
12449 char_u subbuf[NUMBUFLEN];
12450 char_u flagsbuf[NUMBUFLEN];
12451
12452 char_u *str = get_tv_string_chk(&argvars[0]);
12453 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012454 char_u *sub = NULL;
12455 typval_T *expr = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012456 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
12457
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012458 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
12459 expr = &argvars[2];
12460 else
12461 sub = get_tv_string_buf_chk(&argvars[2], subbuf);
12462
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012463 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012464 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
12465 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012466 rettv->vval.v_string = NULL;
12467 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012468 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012469}
12470
12471/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020012472 * "swapinfo(swap_filename)" function
12473 */
12474 static void
12475f_swapinfo(typval_T *argvars, typval_T *rettv)
12476{
12477 if (rettv_dict_alloc(rettv) == OK)
12478 get_b0_dict(get_tv_string(argvars), rettv->vval.v_dict);
12479}
12480
12481/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020012482 * "swapname(expr)" function
12483 */
12484 static void
12485f_swapname(typval_T *argvars, typval_T *rettv)
12486{
12487 buf_T *buf;
12488
12489 rettv->v_type = VAR_STRING;
12490 buf = get_buf_tv(&argvars[0], FALSE);
12491 if (buf == NULL || buf->b_ml.ml_mfp == NULL
12492 || buf->b_ml.ml_mfp->mf_fname == NULL)
12493 rettv->vval.v_string = NULL;
12494 else
12495 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
12496}
12497
12498/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012499 * "synID(lnum, col, trans)" function
12500 */
12501 static void
12502f_synID(typval_T *argvars UNUSED, typval_T *rettv)
12503{
12504 int id = 0;
12505#ifdef FEAT_SYN_HL
12506 linenr_T lnum;
12507 colnr_T col;
12508 int trans;
12509 int transerr = FALSE;
12510
12511 lnum = get_tv_lnum(argvars); /* -1 on type error */
12512 col = (linenr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12513 trans = (int)get_tv_number_chk(&argvars[2], &transerr);
12514
12515 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12516 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
12517 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
12518#endif
12519
12520 rettv->vval.v_number = id;
12521}
12522
12523/*
12524 * "synIDattr(id, what [, mode])" function
12525 */
12526 static void
12527f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
12528{
12529 char_u *p = NULL;
12530#ifdef FEAT_SYN_HL
12531 int id;
12532 char_u *what;
12533 char_u *mode;
12534 char_u modebuf[NUMBUFLEN];
12535 int modec;
12536
12537 id = (int)get_tv_number(&argvars[0]);
12538 what = get_tv_string(&argvars[1]);
12539 if (argvars[2].v_type != VAR_UNKNOWN)
12540 {
12541 mode = get_tv_string_buf(&argvars[2], modebuf);
12542 modec = TOLOWER_ASC(mode[0]);
12543 if (modec != 't' && modec != 'c' && modec != 'g')
12544 modec = 0; /* replace invalid with current */
12545 }
12546 else
12547 {
12548#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
12549 if (USE_24BIT)
12550 modec = 'g';
12551 else
12552#endif
12553 if (t_colors > 1)
12554 modec = 'c';
12555 else
12556 modec = 't';
12557 }
12558
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012559 switch (TOLOWER_ASC(what[0]))
12560 {
12561 case 'b':
12562 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
12563 p = highlight_color(id, what, modec);
12564 else /* bold */
12565 p = highlight_has_attr(id, HL_BOLD, modec);
12566 break;
12567
12568 case 'f': /* fg[#] or font */
12569 p = highlight_color(id, what, modec);
12570 break;
12571
12572 case 'i':
12573 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
12574 p = highlight_has_attr(id, HL_INVERSE, modec);
12575 else /* italic */
12576 p = highlight_has_attr(id, HL_ITALIC, modec);
12577 break;
12578
12579 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020012580 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012581 break;
12582
12583 case 'r': /* reverse */
12584 p = highlight_has_attr(id, HL_INVERSE, modec);
12585 break;
12586
12587 case 's':
12588 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
12589 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020012590 /* strikeout */
12591 else if (TOLOWER_ASC(what[1]) == 't' &&
12592 TOLOWER_ASC(what[2]) == 'r')
12593 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012594 else /* standout */
12595 p = highlight_has_attr(id, HL_STANDOUT, modec);
12596 break;
12597
12598 case 'u':
12599 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
12600 /* underline */
12601 p = highlight_has_attr(id, HL_UNDERLINE, modec);
12602 else
12603 /* undercurl */
12604 p = highlight_has_attr(id, HL_UNDERCURL, modec);
12605 break;
12606 }
12607
12608 if (p != NULL)
12609 p = vim_strsave(p);
12610#endif
12611 rettv->v_type = VAR_STRING;
12612 rettv->vval.v_string = p;
12613}
12614
12615/*
12616 * "synIDtrans(id)" function
12617 */
12618 static void
12619f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
12620{
12621 int id;
12622
12623#ifdef FEAT_SYN_HL
12624 id = (int)get_tv_number(&argvars[0]);
12625
12626 if (id > 0)
12627 id = syn_get_final_id(id);
12628 else
12629#endif
12630 id = 0;
12631
12632 rettv->vval.v_number = id;
12633}
12634
12635/*
12636 * "synconcealed(lnum, col)" function
12637 */
12638 static void
12639f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
12640{
12641#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12642 linenr_T lnum;
12643 colnr_T col;
12644 int syntax_flags = 0;
12645 int cchar;
12646 int matchid = 0;
12647 char_u str[NUMBUFLEN];
12648#endif
12649
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012650 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012651
12652#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12653 lnum = get_tv_lnum(argvars); /* -1 on type error */
12654 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12655
12656 vim_memset(str, NUL, sizeof(str));
12657
12658 if (rettv_list_alloc(rettv) != FAIL)
12659 {
12660 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12661 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12662 && curwin->w_p_cole > 0)
12663 {
12664 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
12665 syntax_flags = get_syntax_info(&matchid);
12666
12667 /* get the conceal character */
12668 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
12669 {
12670 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020012671 if (cchar == NUL && curwin->w_p_cole == 1)
12672 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012673 if (cchar != NUL)
12674 {
12675# ifdef FEAT_MBYTE
12676 if (has_mbyte)
12677 (*mb_char2bytes)(cchar, str);
12678 else
12679# endif
12680 str[0] = cchar;
12681 }
12682 }
12683 }
12684
12685 list_append_number(rettv->vval.v_list,
12686 (syntax_flags & HL_CONCEAL) != 0);
12687 /* -1 to auto-determine strlen */
12688 list_append_string(rettv->vval.v_list, str, -1);
12689 list_append_number(rettv->vval.v_list, matchid);
12690 }
12691#endif
12692}
12693
12694/*
12695 * "synstack(lnum, col)" function
12696 */
12697 static void
12698f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
12699{
12700#ifdef FEAT_SYN_HL
12701 linenr_T lnum;
12702 colnr_T col;
12703 int i;
12704 int id;
12705#endif
12706
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012707 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012708
12709#ifdef FEAT_SYN_HL
12710 lnum = get_tv_lnum(argvars); /* -1 on type error */
12711 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12712
12713 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12714 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12715 && rettv_list_alloc(rettv) != FAIL)
12716 {
12717 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
12718 for (i = 0; ; ++i)
12719 {
12720 id = syn_get_stack_item(i);
12721 if (id < 0)
12722 break;
12723 if (list_append_number(rettv->vval.v_list, id) == FAIL)
12724 break;
12725 }
12726 }
12727#endif
12728}
12729
12730 static void
12731get_cmd_output_as_rettv(
12732 typval_T *argvars,
12733 typval_T *rettv,
12734 int retlist)
12735{
12736 char_u *res = NULL;
12737 char_u *p;
12738 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012739 int err = FALSE;
12740 FILE *fd;
12741 list_T *list = NULL;
12742 int flags = SHELL_SILENT;
12743
12744 rettv->v_type = VAR_STRING;
12745 rettv->vval.v_string = NULL;
12746 if (check_restricted() || check_secure())
12747 goto errret;
12748
12749 if (argvars[1].v_type != VAR_UNKNOWN)
12750 {
12751 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010012752 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012753 * command.
12754 */
12755 if ((infile = vim_tempname('i', TRUE)) == NULL)
12756 {
12757 EMSG(_(e_notmp));
12758 goto errret;
12759 }
12760
12761 fd = mch_fopen((char *)infile, WRITEBIN);
12762 if (fd == NULL)
12763 {
12764 EMSG2(_(e_notopen), infile);
12765 goto errret;
12766 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010012767 if (argvars[1].v_type == VAR_NUMBER)
12768 {
12769 linenr_T lnum;
12770 buf_T *buf;
12771
12772 buf = buflist_findnr(argvars[1].vval.v_number);
12773 if (buf == NULL)
12774 {
12775 EMSGN(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010012776 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010012777 goto errret;
12778 }
12779
12780 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
12781 {
12782 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
12783 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
12784 {
12785 err = TRUE;
12786 break;
12787 }
12788 if (putc(NL, fd) == EOF)
12789 {
12790 err = TRUE;
12791 break;
12792 }
12793 }
12794 }
12795 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012796 {
12797 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
12798 err = TRUE;
12799 }
12800 else
12801 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010012802 size_t len;
12803 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012804
12805 p = get_tv_string_buf_chk(&argvars[1], buf);
12806 if (p == NULL)
12807 {
12808 fclose(fd);
12809 goto errret; /* type error; errmsg already given */
12810 }
12811 len = STRLEN(p);
12812 if (len > 0 && fwrite(p, len, 1, fd) != 1)
12813 err = TRUE;
12814 }
12815 if (fclose(fd) != 0)
12816 err = TRUE;
12817 if (err)
12818 {
12819 EMSG(_("E677: Error writing temp file"));
12820 goto errret;
12821 }
12822 }
12823
12824 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
12825 * echoes typeahead, that messes up the display. */
12826 if (!msg_silent)
12827 flags += SHELL_COOKED;
12828
12829 if (retlist)
12830 {
12831 int len;
12832 listitem_T *li;
12833 char_u *s = NULL;
12834 char_u *start;
12835 char_u *end;
12836 int i;
12837
12838 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
12839 if (res == NULL)
12840 goto errret;
12841
12842 list = list_alloc();
12843 if (list == NULL)
12844 goto errret;
12845
12846 for (i = 0; i < len; ++i)
12847 {
12848 start = res + i;
12849 while (i < len && res[i] != NL)
12850 ++i;
12851 end = res + i;
12852
12853 s = alloc((unsigned)(end - start + 1));
12854 if (s == NULL)
12855 goto errret;
12856
12857 for (p = s; start < end; ++p, ++start)
12858 *p = *start == NUL ? NL : *start;
12859 *p = NUL;
12860
12861 li = listitem_alloc();
12862 if (li == NULL)
12863 {
12864 vim_free(s);
12865 goto errret;
12866 }
12867 li->li_tv.v_type = VAR_STRING;
12868 li->li_tv.v_lock = 0;
12869 li->li_tv.vval.v_string = s;
12870 list_append(list, li);
12871 }
12872
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012873 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012874 list = NULL;
12875 }
12876 else
12877 {
12878 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
12879#ifdef USE_CR
12880 /* translate <CR> into <NL> */
12881 if (res != NULL)
12882 {
12883 char_u *s;
12884
12885 for (s = res; *s; ++s)
12886 {
12887 if (*s == CAR)
12888 *s = NL;
12889 }
12890 }
12891#else
12892# ifdef USE_CRNL
12893 /* translate <CR><NL> into <NL> */
12894 if (res != NULL)
12895 {
12896 char_u *s, *d;
12897
12898 d = res;
12899 for (s = res; *s; ++s)
12900 {
12901 if (s[0] == CAR && s[1] == NL)
12902 ++s;
12903 *d++ = *s;
12904 }
12905 *d = NUL;
12906 }
12907# endif
12908#endif
12909 rettv->vval.v_string = res;
12910 res = NULL;
12911 }
12912
12913errret:
12914 if (infile != NULL)
12915 {
12916 mch_remove(infile);
12917 vim_free(infile);
12918 }
12919 if (res != NULL)
12920 vim_free(res);
12921 if (list != NULL)
12922 list_free(list);
12923}
12924
12925/*
12926 * "system()" function
12927 */
12928 static void
12929f_system(typval_T *argvars, typval_T *rettv)
12930{
12931 get_cmd_output_as_rettv(argvars, rettv, FALSE);
12932}
12933
12934/*
12935 * "systemlist()" function
12936 */
12937 static void
12938f_systemlist(typval_T *argvars, typval_T *rettv)
12939{
12940 get_cmd_output_as_rettv(argvars, rettv, TRUE);
12941}
12942
12943/*
12944 * "tabpagebuflist()" function
12945 */
12946 static void
12947f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12948{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012949 tabpage_T *tp;
12950 win_T *wp = NULL;
12951
12952 if (argvars[0].v_type == VAR_UNKNOWN)
12953 wp = firstwin;
12954 else
12955 {
12956 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12957 if (tp != NULL)
12958 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12959 }
12960 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
12961 {
12962 for (; wp != NULL; wp = wp->w_next)
12963 if (list_append_number(rettv->vval.v_list,
12964 wp->w_buffer->b_fnum) == FAIL)
12965 break;
12966 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012967}
12968
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012969/*
12970 * "tabpagenr()" function
12971 */
12972 static void
12973f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
12974{
12975 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012976 char_u *arg;
12977
12978 if (argvars[0].v_type != VAR_UNKNOWN)
12979 {
12980 arg = get_tv_string_chk(&argvars[0]);
12981 nr = 0;
12982 if (arg != NULL)
12983 {
12984 if (STRCMP(arg, "$") == 0)
12985 nr = tabpage_index(NULL) - 1;
12986 else
12987 EMSG2(_(e_invexpr2), arg);
12988 }
12989 }
12990 else
12991 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012992 rettv->vval.v_number = nr;
12993}
12994
12995
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012996/*
12997 * Common code for tabpagewinnr() and winnr().
12998 */
12999 static int
13000get_winnr(tabpage_T *tp, typval_T *argvar)
13001{
13002 win_T *twin;
13003 int nr = 1;
13004 win_T *wp;
13005 char_u *arg;
13006
13007 twin = (tp == curtab) ? curwin : tp->tp_curwin;
13008 if (argvar->v_type != VAR_UNKNOWN)
13009 {
13010 arg = get_tv_string_chk(argvar);
13011 if (arg == NULL)
13012 nr = 0; /* type error; errmsg already given */
13013 else if (STRCMP(arg, "$") == 0)
13014 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
13015 else if (STRCMP(arg, "#") == 0)
13016 {
13017 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
13018 if (twin == NULL)
13019 nr = 0;
13020 }
13021 else
13022 {
13023 EMSG2(_(e_invexpr2), arg);
13024 nr = 0;
13025 }
13026 }
13027
13028 if (nr > 0)
13029 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
13030 wp != twin; wp = wp->w_next)
13031 {
13032 if (wp == NULL)
13033 {
13034 /* didn't find it in this tabpage */
13035 nr = 0;
13036 break;
13037 }
13038 ++nr;
13039 }
13040 return nr;
13041}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013042
13043/*
13044 * "tabpagewinnr()" function
13045 */
13046 static void
13047f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
13048{
13049 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013050 tabpage_T *tp;
13051
13052 tp = find_tabpage((int)get_tv_number(&argvars[0]));
13053 if (tp == NULL)
13054 nr = 0;
13055 else
13056 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013057 rettv->vval.v_number = nr;
13058}
13059
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013060/*
13061 * "tagfiles()" function
13062 */
13063 static void
13064f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
13065{
13066 char_u *fname;
13067 tagname_T tn;
13068 int first;
13069
13070 if (rettv_list_alloc(rettv) == FAIL)
13071 return;
13072 fname = alloc(MAXPATHL);
13073 if (fname == NULL)
13074 return;
13075
13076 for (first = TRUE; ; first = FALSE)
13077 if (get_tagfname(&tn, first, fname) == FAIL
13078 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
13079 break;
13080 tagname_free(&tn);
13081 vim_free(fname);
13082}
13083
13084/*
13085 * "taglist()" function
13086 */
13087 static void
13088f_taglist(typval_T *argvars, typval_T *rettv)
13089{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013090 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013091 char_u *tag_pattern;
13092
13093 tag_pattern = get_tv_string(&argvars[0]);
13094
13095 rettv->vval.v_number = FALSE;
13096 if (*tag_pattern == NUL)
13097 return;
13098
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013099 if (argvars[1].v_type != VAR_UNKNOWN)
13100 fname = get_tv_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013101 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013102 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013103}
13104
13105/*
13106 * "tempname()" function
13107 */
13108 static void
13109f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
13110{
13111 static int x = 'A';
13112
13113 rettv->v_type = VAR_STRING;
13114 rettv->vval.v_string = vim_tempname(x, FALSE);
13115
13116 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
13117 * names. Skip 'I' and 'O', they are used for shell redirection. */
13118 do
13119 {
13120 if (x == 'Z')
13121 x = '0';
13122 else if (x == '9')
13123 x = 'A';
13124 else
13125 {
13126#ifdef EBCDIC
13127 if (x == 'I')
13128 x = 'J';
13129 else if (x == 'R')
13130 x = 'S';
13131 else
13132#endif
13133 ++x;
13134 }
13135 } while (x == 'I' || x == 'O');
13136}
13137
13138#ifdef FEAT_FLOAT
13139/*
13140 * "tan()" function
13141 */
13142 static void
13143f_tan(typval_T *argvars, typval_T *rettv)
13144{
13145 float_T f = 0.0;
13146
13147 rettv->v_type = VAR_FLOAT;
13148 if (get_float_arg(argvars, &f) == OK)
13149 rettv->vval.v_float = tan(f);
13150 else
13151 rettv->vval.v_float = 0.0;
13152}
13153
13154/*
13155 * "tanh()" function
13156 */
13157 static void
13158f_tanh(typval_T *argvars, typval_T *rettv)
13159{
13160 float_T f = 0.0;
13161
13162 rettv->v_type = VAR_FLOAT;
13163 if (get_float_arg(argvars, &f) == OK)
13164 rettv->vval.v_float = tanh(f);
13165 else
13166 rettv->vval.v_float = 0.0;
13167}
13168#endif
13169
13170/*
13171 * "test_alloc_fail(id, countdown, repeat)" function
13172 */
13173 static void
13174f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
13175{
13176 if (argvars[0].v_type != VAR_NUMBER
13177 || argvars[0].vval.v_number <= 0
13178 || argvars[1].v_type != VAR_NUMBER
13179 || argvars[1].vval.v_number < 0
13180 || argvars[2].v_type != VAR_NUMBER)
13181 EMSG(_(e_invarg));
13182 else
13183 {
13184 alloc_fail_id = argvars[0].vval.v_number;
13185 if (alloc_fail_id >= aid_last)
13186 EMSG(_(e_invarg));
13187 alloc_fail_countdown = argvars[1].vval.v_number;
13188 alloc_fail_repeat = argvars[2].vval.v_number;
13189 did_outofmem_msg = FALSE;
13190 }
13191}
13192
13193/*
13194 * "test_autochdir()"
13195 */
13196 static void
13197f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13198{
13199#if defined(FEAT_AUTOCHDIR)
13200 test_autochdir = TRUE;
13201#endif
13202}
13203
13204/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020013205 * "test_feedinput()"
13206 */
13207 static void
13208f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
13209{
13210#ifdef USE_INPUT_BUF
13211 char_u *val = get_tv_string_chk(&argvars[0]);
13212
13213 if (val != NULL)
13214 {
13215 trash_input_buf();
13216 add_to_input_buf_csi(val, (int)STRLEN(val));
13217 }
13218#endif
13219}
13220
13221/*
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013222 * "test_option_not_set({name})" function
13223 */
13224 static void
13225f_test_option_not_set(typval_T *argvars, typval_T *rettv UNUSED)
13226{
13227 char_u *name = (char_u *)"";
13228
13229 if (argvars[0].v_type != VAR_STRING)
13230 EMSG(_(e_invarg));
13231 else
13232 {
Bram Moolenaarbf1c1b82018-09-13 21:30:05 +020013233 name = get_tv_string(&argvars[0]);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013234 if (reset_option_was_set(name) == FAIL)
13235 EMSG2(_(e_invarg2), name);
13236 }
13237}
13238
13239/*
13240 * "test_override({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013241 */
13242 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013243f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013244{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013245 char_u *name = (char_u *)"";
13246 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013247 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013248
13249 if (argvars[0].v_type != VAR_STRING
13250 || (argvars[1].v_type) != VAR_NUMBER)
13251 EMSG(_(e_invarg));
13252 else
13253 {
13254 name = get_tv_string_chk(&argvars[0]);
13255 val = (int)get_tv_number(&argvars[1]);
13256
13257 if (STRCMP(name, (char_u *)"redraw") == 0)
13258 disable_redraw_for_testing = val;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020013259 else if (STRCMP(name, (char_u *)"redraw_flag") == 0)
13260 ignore_redraw_flag_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013261 else if (STRCMP(name, (char_u *)"char_avail") == 0)
13262 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013263 else if (STRCMP(name, (char_u *)"starting") == 0)
13264 {
13265 if (val)
13266 {
13267 if (save_starting < 0)
13268 save_starting = starting;
13269 starting = 0;
13270 }
13271 else
13272 {
13273 starting = save_starting;
13274 save_starting = -1;
13275 }
13276 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013277 else if (STRCMP(name, (char_u *)"nfa_fail") == 0)
13278 nfa_fail_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013279 else if (STRCMP(name, (char_u *)"ALL") == 0)
13280 {
13281 disable_char_avail_for_testing = FALSE;
13282 disable_redraw_for_testing = FALSE;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020013283 ignore_redraw_flag_for_testing = FALSE;
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013284 nfa_fail_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013285 if (save_starting >= 0)
13286 {
13287 starting = save_starting;
13288 save_starting = -1;
13289 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013290 }
13291 else
13292 EMSG2(_(e_invarg2), name);
13293 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013294}
13295
13296/*
13297 * "test_garbagecollect_now()" function
13298 */
13299 static void
13300f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13301{
13302 /* This is dangerous, any Lists and Dicts used internally may be freed
13303 * while still in use. */
13304 garbage_collect(TRUE);
13305}
13306
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013307/*
13308 * "test_ignore_error()" function
13309 */
13310 static void
13311f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
13312{
13313 ignore_error_for_testing(get_tv_string(&argvars[0]));
13314}
13315
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013316#ifdef FEAT_JOB_CHANNEL
13317 static void
13318f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
13319{
13320 rettv->v_type = VAR_CHANNEL;
13321 rettv->vval.v_channel = NULL;
13322}
13323#endif
13324
13325 static void
13326f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
13327{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013328 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013329}
13330
13331#ifdef FEAT_JOB_CHANNEL
13332 static void
13333f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
13334{
13335 rettv->v_type = VAR_JOB;
13336 rettv->vval.v_job = NULL;
13337}
13338#endif
13339
13340 static void
13341f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
13342{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013343 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013344}
13345
13346 static void
13347f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
13348{
13349 rettv->v_type = VAR_PARTIAL;
13350 rettv->vval.v_partial = NULL;
13351}
13352
13353 static void
13354f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
13355{
13356 rettv->v_type = VAR_STRING;
13357 rettv->vval.v_string = NULL;
13358}
13359
Bram Moolenaarab186732018-09-14 21:27:06 +020013360#ifdef FEAT_GUI
13361 static void
13362f_test_scrollbar(typval_T *argvars, typval_T *rettv UNUSED)
13363{
13364 char_u *which;
13365 long value;
13366 int dragging;
13367 scrollbar_T *sb = NULL;
13368
13369 if (argvars[0].v_type != VAR_STRING
13370 || (argvars[1].v_type) != VAR_NUMBER
13371 || (argvars[2].v_type) != VAR_NUMBER)
13372 {
13373 EMSG(_(e_invarg));
13374 return;
13375 }
13376 which = get_tv_string(&argvars[0]);
13377 value = get_tv_number(&argvars[1]);
13378 dragging = get_tv_number(&argvars[2]);
13379
13380 if (STRCMP(which, "left") == 0)
13381 sb = &curwin->w_scrollbars[SBAR_LEFT];
13382 else if (STRCMP(which, "right") == 0)
13383 sb = &curwin->w_scrollbars[SBAR_RIGHT];
13384 else if (STRCMP(which, "hor") == 0)
13385 sb = &gui.bottom_sbar;
13386 if (sb == NULL)
13387 {
13388 EMSG2(_(e_invarg2), which);
13389 return;
13390 }
13391 gui_drag_scrollbar(sb, value, dragging);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020013392# ifndef USE_ON_FLY_SCROLL
13393 // need to loop through normal_cmd() to handle the scroll events
13394 exec_normal(FALSE, TRUE, FALSE);
13395# endif
Bram Moolenaarab186732018-09-14 21:27:06 +020013396}
13397#endif
13398
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013399 static void
13400f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
13401{
13402 time_for_testing = (time_t)get_tv_number(&argvars[0]);
13403}
13404
13405#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
13406/*
13407 * Get a callback from "arg". It can be a Funcref or a function name.
13408 * When "arg" is zero return an empty string.
13409 * Return NULL for an invalid argument.
13410 */
13411 char_u *
13412get_callback(typval_T *arg, partial_T **pp)
13413{
13414 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
13415 {
13416 *pp = arg->vval.v_partial;
13417 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020013418 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013419 }
13420 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020013421 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013422 {
13423 func_ref(arg->vval.v_string);
13424 return arg->vval.v_string;
13425 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013426 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
13427 return (char_u *)"";
13428 EMSG(_("E921: Invalid callback argument"));
13429 return NULL;
13430}
13431
13432/*
Bram Moolenaard5359b22018-04-05 22:44:39 +020013433 * Unref/free "callback" and "partial" returned by get_callback().
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013434 */
13435 void
13436free_callback(char_u *callback, partial_T *partial)
13437{
13438 if (partial != NULL)
13439 partial_unref(partial);
13440 else if (callback != NULL)
13441 {
13442 func_unref(callback);
13443 vim_free(callback);
13444 }
13445}
13446#endif
13447
13448#ifdef FEAT_TIMERS
13449/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020013450 * "timer_info([timer])" function
13451 */
13452 static void
13453f_timer_info(typval_T *argvars, typval_T *rettv)
13454{
13455 timer_T *timer = NULL;
13456
13457 if (rettv_list_alloc(rettv) != OK)
13458 return;
13459 if (argvars[0].v_type != VAR_UNKNOWN)
13460 {
13461 if (argvars[0].v_type != VAR_NUMBER)
13462 EMSG(_(e_number_exp));
13463 else
13464 {
13465 timer = find_timer((int)get_tv_number(&argvars[0]));
13466 if (timer != NULL)
13467 add_timer_info(rettv, timer);
13468 }
13469 }
13470 else
13471 add_timer_info_all(rettv);
13472}
13473
13474/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013475 * "timer_pause(timer, paused)" function
13476 */
13477 static void
13478f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
13479{
13480 timer_T *timer = NULL;
13481 int paused = (int)get_tv_number(&argvars[1]);
13482
13483 if (argvars[0].v_type != VAR_NUMBER)
13484 EMSG(_(e_number_exp));
13485 else
13486 {
13487 timer = find_timer((int)get_tv_number(&argvars[0]));
13488 if (timer != NULL)
13489 timer->tr_paused = paused;
13490 }
13491}
13492
13493/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013494 * "timer_start(time, callback [, options])" function
13495 */
13496 static void
13497f_timer_start(typval_T *argvars, typval_T *rettv)
13498{
Bram Moolenaar75537a92016-09-05 22:45:28 +020013499 long msec = (long)get_tv_number(&argvars[0]);
13500 timer_T *timer;
13501 int repeat = 0;
13502 char_u *callback;
13503 dict_T *dict;
13504 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013505
Bram Moolenaar75537a92016-09-05 22:45:28 +020013506 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013507 if (check_secure())
13508 return;
13509 if (argvars[2].v_type != VAR_UNKNOWN)
13510 {
13511 if (argvars[2].v_type != VAR_DICT
13512 || (dict = argvars[2].vval.v_dict) == NULL)
13513 {
13514 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
13515 return;
13516 }
13517 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
13518 repeat = get_dict_number(dict, (char_u *)"repeat");
13519 }
13520
Bram Moolenaar75537a92016-09-05 22:45:28 +020013521 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013522 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020013523 return;
13524
13525 timer = create_timer(msec, repeat);
13526 if (timer == NULL)
13527 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013528 else
13529 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020013530 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020013531 timer->tr_callback = vim_strsave(callback);
13532 else
13533 /* pointer into the partial */
13534 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020013535 timer->tr_partial = partial;
13536 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013537 }
13538}
13539
13540/*
13541 * "timer_stop(timer)" function
13542 */
13543 static void
13544f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
13545{
13546 timer_T *timer;
13547
13548 if (argvars[0].v_type != VAR_NUMBER)
13549 {
13550 EMSG(_(e_number_exp));
13551 return;
13552 }
13553 timer = find_timer((int)get_tv_number(&argvars[0]));
13554 if (timer != NULL)
13555 stop_timer(timer);
13556}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013557
13558/*
13559 * "timer_stopall()" function
13560 */
13561 static void
13562f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13563{
13564 stop_all_timers();
13565}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013566#endif
13567
13568/*
13569 * "tolower(string)" function
13570 */
13571 static void
13572f_tolower(typval_T *argvars, typval_T *rettv)
13573{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013574 rettv->v_type = VAR_STRING;
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +010013575 rettv->vval.v_string = strlow_save(get_tv_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013576}
13577
13578/*
13579 * "toupper(string)" function
13580 */
13581 static void
13582f_toupper(typval_T *argvars, typval_T *rettv)
13583{
13584 rettv->v_type = VAR_STRING;
13585 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
13586}
13587
13588/*
13589 * "tr(string, fromstr, tostr)" function
13590 */
13591 static void
13592f_tr(typval_T *argvars, typval_T *rettv)
13593{
13594 char_u *in_str;
13595 char_u *fromstr;
13596 char_u *tostr;
13597 char_u *p;
13598#ifdef FEAT_MBYTE
13599 int inlen;
13600 int fromlen;
13601 int tolen;
13602 int idx;
13603 char_u *cpstr;
13604 int cplen;
13605 int first = TRUE;
13606#endif
13607 char_u buf[NUMBUFLEN];
13608 char_u buf2[NUMBUFLEN];
13609 garray_T ga;
13610
13611 in_str = get_tv_string(&argvars[0]);
13612 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
13613 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
13614
13615 /* Default return value: empty string. */
13616 rettv->v_type = VAR_STRING;
13617 rettv->vval.v_string = NULL;
13618 if (fromstr == NULL || tostr == NULL)
13619 return; /* type error; errmsg already given */
13620 ga_init2(&ga, (int)sizeof(char), 80);
13621
13622#ifdef FEAT_MBYTE
13623 if (!has_mbyte)
13624#endif
13625 /* not multi-byte: fromstr and tostr must be the same length */
13626 if (STRLEN(fromstr) != STRLEN(tostr))
13627 {
13628#ifdef FEAT_MBYTE
13629error:
13630#endif
13631 EMSG2(_(e_invarg2), fromstr);
13632 ga_clear(&ga);
13633 return;
13634 }
13635
13636 /* fromstr and tostr have to contain the same number of chars */
13637 while (*in_str != NUL)
13638 {
13639#ifdef FEAT_MBYTE
13640 if (has_mbyte)
13641 {
13642 inlen = (*mb_ptr2len)(in_str);
13643 cpstr = in_str;
13644 cplen = inlen;
13645 idx = 0;
13646 for (p = fromstr; *p != NUL; p += fromlen)
13647 {
13648 fromlen = (*mb_ptr2len)(p);
13649 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
13650 {
13651 for (p = tostr; *p != NUL; p += tolen)
13652 {
13653 tolen = (*mb_ptr2len)(p);
13654 if (idx-- == 0)
13655 {
13656 cplen = tolen;
13657 cpstr = p;
13658 break;
13659 }
13660 }
13661 if (*p == NUL) /* tostr is shorter than fromstr */
13662 goto error;
13663 break;
13664 }
13665 ++idx;
13666 }
13667
13668 if (first && cpstr == in_str)
13669 {
13670 /* Check that fromstr and tostr have the same number of
13671 * (multi-byte) characters. Done only once when a character
13672 * of in_str doesn't appear in fromstr. */
13673 first = FALSE;
13674 for (p = tostr; *p != NUL; p += tolen)
13675 {
13676 tolen = (*mb_ptr2len)(p);
13677 --idx;
13678 }
13679 if (idx != 0)
13680 goto error;
13681 }
13682
13683 (void)ga_grow(&ga, cplen);
13684 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
13685 ga.ga_len += cplen;
13686
13687 in_str += inlen;
13688 }
13689 else
13690#endif
13691 {
13692 /* When not using multi-byte chars we can do it faster. */
13693 p = vim_strchr(fromstr, *in_str);
13694 if (p != NULL)
13695 ga_append(&ga, tostr[p - fromstr]);
13696 else
13697 ga_append(&ga, *in_str);
13698 ++in_str;
13699 }
13700 }
13701
13702 /* add a terminating NUL */
13703 (void)ga_grow(&ga, 1);
13704 ga_append(&ga, NUL);
13705
13706 rettv->vval.v_string = ga.ga_data;
13707}
13708
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010013709/*
13710 * "trim({expr})" function
13711 */
13712 static void
13713f_trim(typval_T *argvars, typval_T *rettv)
13714{
13715 char_u buf1[NUMBUFLEN];
13716 char_u buf2[NUMBUFLEN];
13717 char_u *head = get_tv_string_buf_chk(&argvars[0], buf1);
13718 char_u *mask = NULL;
13719 char_u *tail;
13720 char_u *prev;
13721 char_u *p;
13722 int c1;
13723
13724 rettv->v_type = VAR_STRING;
13725 if (head == NULL)
13726 {
13727 rettv->vval.v_string = NULL;
13728 return;
13729 }
13730
13731 if (argvars[1].v_type == VAR_STRING)
13732 mask = get_tv_string_buf_chk(&argvars[1], buf2);
13733
13734 while (*head != NUL)
13735 {
13736 c1 = PTR2CHAR(head);
13737 if (mask == NULL)
13738 {
13739 if (c1 > ' ' && c1 != 0xa0)
13740 break;
13741 }
13742 else
13743 {
13744 for (p = mask; *p != NUL; MB_PTR_ADV(p))
13745 if (c1 == PTR2CHAR(p))
13746 break;
13747 if (*p == NUL)
13748 break;
13749 }
13750 MB_PTR_ADV(head);
13751 }
13752
13753 for (tail = head + STRLEN(head); tail > head; tail = prev)
13754 {
13755 prev = tail;
13756 MB_PTR_BACK(head, prev);
13757 c1 = PTR2CHAR(prev);
13758 if (mask == NULL)
13759 {
13760 if (c1 > ' ' && c1 != 0xa0)
13761 break;
13762 }
13763 else
13764 {
13765 for (p = mask; *p != NUL; MB_PTR_ADV(p))
13766 if (c1 == PTR2CHAR(p))
13767 break;
13768 if (*p == NUL)
13769 break;
13770 }
13771 }
13772 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
13773}
13774
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013775#ifdef FEAT_FLOAT
13776/*
13777 * "trunc({float})" function
13778 */
13779 static void
13780f_trunc(typval_T *argvars, typval_T *rettv)
13781{
13782 float_T f = 0.0;
13783
13784 rettv->v_type = VAR_FLOAT;
13785 if (get_float_arg(argvars, &f) == OK)
13786 /* trunc() is not in C90, use floor() or ceil() instead. */
13787 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
13788 else
13789 rettv->vval.v_float = 0.0;
13790}
13791#endif
13792
13793/*
13794 * "type(expr)" function
13795 */
13796 static void
13797f_type(typval_T *argvars, typval_T *rettv)
13798{
13799 int n = -1;
13800
13801 switch (argvars[0].v_type)
13802 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020013803 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
13804 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013805 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020013806 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
13807 case VAR_LIST: n = VAR_TYPE_LIST; break;
13808 case VAR_DICT: n = VAR_TYPE_DICT; break;
13809 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013810 case VAR_SPECIAL:
13811 if (argvars[0].vval.v_number == VVAL_FALSE
13812 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020013813 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013814 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020013815 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013816 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020013817 case VAR_JOB: n = VAR_TYPE_JOB; break;
13818 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013819 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010013820 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013821 n = -1;
13822 break;
13823 }
13824 rettv->vval.v_number = n;
13825}
13826
13827/*
13828 * "undofile(name)" function
13829 */
13830 static void
13831f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
13832{
13833 rettv->v_type = VAR_STRING;
13834#ifdef FEAT_PERSISTENT_UNDO
13835 {
13836 char_u *fname = get_tv_string(&argvars[0]);
13837
13838 if (*fname == NUL)
13839 {
13840 /* If there is no file name there will be no undo file. */
13841 rettv->vval.v_string = NULL;
13842 }
13843 else
13844 {
13845 char_u *ffname = FullName_save(fname, FALSE);
13846
13847 if (ffname != NULL)
13848 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
13849 vim_free(ffname);
13850 }
13851 }
13852#else
13853 rettv->vval.v_string = NULL;
13854#endif
13855}
13856
13857/*
13858 * "undotree()" function
13859 */
13860 static void
13861f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
13862{
13863 if (rettv_dict_alloc(rettv) == OK)
13864 {
13865 dict_T *dict = rettv->vval.v_dict;
13866 list_T *list;
13867
Bram Moolenaare0be1672018-07-08 16:50:37 +020013868 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
13869 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
13870 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
13871 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
13872 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
13873 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013874
13875 list = list_alloc();
13876 if (list != NULL)
13877 {
13878 u_eval_tree(curbuf->b_u_oldhead, list);
13879 dict_add_list(dict, "entries", list);
13880 }
13881 }
13882}
13883
13884/*
13885 * "values(dict)" function
13886 */
13887 static void
13888f_values(typval_T *argvars, typval_T *rettv)
13889{
13890 dict_list(argvars, rettv, 1);
13891}
13892
13893/*
13894 * "virtcol(string)" function
13895 */
13896 static void
13897f_virtcol(typval_T *argvars, typval_T *rettv)
13898{
13899 colnr_T vcol = 0;
13900 pos_T *fp;
13901 int fnum = curbuf->b_fnum;
13902
13903 fp = var2fpos(&argvars[0], FALSE, &fnum);
13904 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
13905 && fnum == curbuf->b_fnum)
13906 {
13907 getvvcol(curwin, fp, NULL, NULL, &vcol);
13908 ++vcol;
13909 }
13910
13911 rettv->vval.v_number = vcol;
13912}
13913
13914/*
13915 * "visualmode()" function
13916 */
13917 static void
13918f_visualmode(typval_T *argvars, typval_T *rettv)
13919{
13920 char_u str[2];
13921
13922 rettv->v_type = VAR_STRING;
13923 str[0] = curbuf->b_visual_mode_eval;
13924 str[1] = NUL;
13925 rettv->vval.v_string = vim_strsave(str);
13926
13927 /* A non-zero number or non-empty string argument: reset mode. */
13928 if (non_zero_arg(&argvars[0]))
13929 curbuf->b_visual_mode_eval = NUL;
13930}
13931
13932/*
13933 * "wildmenumode()" function
13934 */
13935 static void
13936f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13937{
13938#ifdef FEAT_WILDMENU
13939 if (wild_menu_showing)
13940 rettv->vval.v_number = 1;
13941#endif
13942}
13943
13944/*
13945 * "winbufnr(nr)" function
13946 */
13947 static void
13948f_winbufnr(typval_T *argvars, typval_T *rettv)
13949{
13950 win_T *wp;
13951
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020013952 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013953 if (wp == NULL)
13954 rettv->vval.v_number = -1;
13955 else
13956 rettv->vval.v_number = wp->w_buffer->b_fnum;
13957}
13958
13959/*
13960 * "wincol()" function
13961 */
13962 static void
13963f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
13964{
13965 validate_cursor();
13966 rettv->vval.v_number = curwin->w_wcol + 1;
13967}
13968
13969/*
13970 * "winheight(nr)" function
13971 */
13972 static void
13973f_winheight(typval_T *argvars, typval_T *rettv)
13974{
13975 win_T *wp;
13976
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020013977 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013978 if (wp == NULL)
13979 rettv->vval.v_number = -1;
13980 else
13981 rettv->vval.v_number = wp->w_height;
13982}
13983
13984/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020013985 * "winlayout()" function
13986 */
13987 static void
13988f_winlayout(typval_T *argvars, typval_T *rettv)
13989{
13990 tabpage_T *tp;
13991
13992 if (rettv_list_alloc(rettv) != OK)
13993 return;
13994
13995 if (argvars[0].v_type == VAR_UNKNOWN)
13996 tp = curtab;
13997 else
13998 {
13999 tp = find_tabpage((int)get_tv_number(&argvars[0]));
14000 if (tp == NULL)
14001 return;
14002 }
14003
14004 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
14005}
14006
14007/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014008 * "winline()" function
14009 */
14010 static void
14011f_winline(typval_T *argvars UNUSED, typval_T *rettv)
14012{
14013 validate_cursor();
14014 rettv->vval.v_number = curwin->w_wrow + 1;
14015}
14016
14017/*
14018 * "winnr()" function
14019 */
14020 static void
14021f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
14022{
14023 int nr = 1;
14024
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014025 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014026 rettv->vval.v_number = nr;
14027}
14028
14029/*
14030 * "winrestcmd()" function
14031 */
14032 static void
14033f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
14034{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014035 win_T *wp;
14036 int winnr = 1;
14037 garray_T ga;
14038 char_u buf[50];
14039
14040 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020014041 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014042 {
14043 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
14044 ga_concat(&ga, buf);
14045 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
14046 ga_concat(&ga, buf);
14047 ++winnr;
14048 }
14049 ga_append(&ga, NUL);
14050
14051 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014052 rettv->v_type = VAR_STRING;
14053}
14054
14055/*
14056 * "winrestview()" function
14057 */
14058 static void
14059f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
14060{
14061 dict_T *dict;
14062
14063 if (argvars[0].v_type != VAR_DICT
14064 || (dict = argvars[0].vval.v_dict) == NULL)
14065 EMSG(_(e_invarg));
14066 else
14067 {
14068 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
14069 curwin->w_cursor.lnum = (linenr_T)get_dict_number(dict, (char_u *)"lnum");
14070 if (dict_find(dict, (char_u *)"col", -1) != NULL)
14071 curwin->w_cursor.col = (colnr_T)get_dict_number(dict, (char_u *)"col");
14072#ifdef FEAT_VIRTUALEDIT
14073 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
14074 curwin->w_cursor.coladd = (colnr_T)get_dict_number(dict, (char_u *)"coladd");
14075#endif
14076 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
14077 {
14078 curwin->w_curswant = (colnr_T)get_dict_number(dict, (char_u *)"curswant");
14079 curwin->w_set_curswant = FALSE;
14080 }
14081
14082 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
14083 set_topline(curwin, (linenr_T)get_dict_number(dict, (char_u *)"topline"));
14084#ifdef FEAT_DIFF
14085 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
14086 curwin->w_topfill = (int)get_dict_number(dict, (char_u *)"topfill");
14087#endif
14088 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
14089 curwin->w_leftcol = (colnr_T)get_dict_number(dict, (char_u *)"leftcol");
14090 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
14091 curwin->w_skipcol = (colnr_T)get_dict_number(dict, (char_u *)"skipcol");
14092
14093 check_cursor();
14094 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020014095 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014096 changed_window_setting();
14097
14098 if (curwin->w_topline <= 0)
14099 curwin->w_topline = 1;
14100 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
14101 curwin->w_topline = curbuf->b_ml.ml_line_count;
14102#ifdef FEAT_DIFF
14103 check_topfill(curwin, TRUE);
14104#endif
14105 }
14106}
14107
14108/*
14109 * "winsaveview()" function
14110 */
14111 static void
14112f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
14113{
14114 dict_T *dict;
14115
14116 if (rettv_dict_alloc(rettv) == FAIL)
14117 return;
14118 dict = rettv->vval.v_dict;
14119
Bram Moolenaare0be1672018-07-08 16:50:37 +020014120 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
14121 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014122#ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +020014123 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014124#endif
14125 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020014126 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014127
Bram Moolenaare0be1672018-07-08 16:50:37 +020014128 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014129#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020014130 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014131#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020014132 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
14133 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014134}
14135
14136/*
14137 * "winwidth(nr)" function
14138 */
14139 static void
14140f_winwidth(typval_T *argvars, typval_T *rettv)
14141{
14142 win_T *wp;
14143
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014144 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014145 if (wp == NULL)
14146 rettv->vval.v_number = -1;
14147 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014148 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014149}
14150
14151/*
14152 * "wordcount()" function
14153 */
14154 static void
14155f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
14156{
14157 if (rettv_dict_alloc(rettv) == FAIL)
14158 return;
14159 cursor_pos_info(rettv->vval.v_dict);
14160}
14161
14162/*
14163 * "writefile()" function
14164 */
14165 static void
14166f_writefile(typval_T *argvars, typval_T *rettv)
14167{
14168 int binary = FALSE;
14169 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014170#ifdef HAVE_FSYNC
14171 int do_fsync = p_fs;
14172#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014173 char_u *fname;
14174 FILE *fd;
14175 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014176 listitem_T *li;
14177 list_T *list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014178
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014179 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014180 if (check_restricted() || check_secure())
14181 return;
14182
14183 if (argvars[0].v_type != VAR_LIST)
14184 {
14185 EMSG2(_(e_listarg), "writefile()");
14186 return;
14187 }
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014188 list = argvars[0].vval.v_list;
14189 if (list == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014190 return;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014191 for (li = list->lv_first; li != NULL; li = li->li_next)
14192 if (get_tv_string_chk(&li->li_tv) == NULL)
14193 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014194
14195 if (argvars[2].v_type != VAR_UNKNOWN)
14196 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014197 char_u *arg2 = get_tv_string_chk(&argvars[2]);
14198
14199 if (arg2 == NULL)
14200 return;
14201 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014202 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014203 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014204 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014205#ifdef HAVE_FSYNC
14206 if (vim_strchr(arg2, 's') != NULL)
14207 do_fsync = TRUE;
14208 else if (vim_strchr(arg2, 'S') != NULL)
14209 do_fsync = FALSE;
14210#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014211 }
14212
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014213 fname = get_tv_string_chk(&argvars[1]);
14214 if (fname == NULL)
14215 return;
14216
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014217 /* Always open the file in binary mode, library functions have a mind of
14218 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014219 if (*fname == NUL || (fd = mch_fopen((char *)fname,
14220 append ? APPENDBIN : WRITEBIN)) == NULL)
14221 {
14222 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
14223 ret = -1;
14224 }
14225 else
14226 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014227 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014228 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014229#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010014230 else if (do_fsync)
14231 /* Ignore the error, the user wouldn't know what to do about it.
14232 * May happen for a device. */
Bram Moolenaar42335f52018-09-13 15:33:43 +020014233 vim_ignored = fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014234#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014235 fclose(fd);
14236 }
14237
14238 rettv->vval.v_number = ret;
14239}
14240
14241/*
14242 * "xor(expr, expr)" function
14243 */
14244 static void
14245f_xor(typval_T *argvars, typval_T *rettv)
14246{
14247 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14248 ^ get_tv_number_chk(&argvars[1], NULL);
14249}
14250
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014251#endif /* FEAT_EVAL */