blob: 51e069981ba32ebf2e8983ce8ac65b3b648183fd [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 Moolenaar73dad1e2016-07-17 22:13:49 +02003266
3267 rettv->vval.v_string = NULL;
3268 rettv->v_type = VAR_STRING;
3269
3270 if (argvars[0].v_type == VAR_LIST)
3271 {
3272 list = argvars[0].vval.v_list;
3273 if (list == NULL || list->lv_first == NULL)
3274 /* empty list, no commands, empty output */
3275 return;
3276 ++list->lv_refcount;
3277 }
3278 else
3279 {
3280 cmd = get_tv_string_chk(&argvars[0]);
3281 if (cmd == NULL)
3282 return;
3283 }
3284
3285 if (argvars[1].v_type != VAR_UNKNOWN)
3286 {
3287 char_u buf[NUMBUFLEN];
3288 char_u *s = get_tv_string_buf_chk(&argvars[1], buf);
3289
3290 if (s == NULL)
3291 return;
3292 if (STRNCMP(s, "silent", 6) == 0)
3293 ++msg_silent;
3294 if (STRCMP(s, "silent!") == 0)
3295 {
3296 emsg_silent = TRUE;
3297 emsg_noredir = TRUE;
3298 }
3299 }
3300 else
3301 ++msg_silent;
3302
3303 if (redir_execute)
3304 save_ga = redir_execute_ga;
3305 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3306 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003307 redir_off = FALSE;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003308 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003309
3310 if (cmd != NULL)
3311 do_cmdline_cmd(cmd);
3312 else
3313 {
3314 listitem_T *item = list->lv_first;
3315
3316 do_cmdline(NULL, get_list_line, (void *)&item,
3317 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3318 --list->lv_refcount;
3319 }
3320
Bram Moolenaard297f352017-01-29 20:31:21 +01003321 /* Need to append a NUL to the result. */
3322 if (ga_grow(&redir_execute_ga, 1) == OK)
3323 {
3324 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3325 rettv->vval.v_string = redir_execute_ga.ga_data;
3326 }
3327 else
3328 {
3329 ga_clear(&redir_execute_ga);
3330 rettv->vval.v_string = NULL;
3331 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003332 msg_silent = save_msg_silent;
3333 emsg_silent = save_emsg_silent;
3334 emsg_noredir = save_emsg_noredir;
3335
3336 redir_execute = save_redir_execute;
3337 if (redir_execute)
3338 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003339 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003340
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003341 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
3342 // Put it back where it was, since nothing should have been written.
3343 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003344}
3345
3346/*
3347 * "exepath()" function
3348 */
3349 static void
3350f_exepath(typval_T *argvars, typval_T *rettv)
3351{
3352 char_u *p = NULL;
3353
3354 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
3355 rettv->v_type = VAR_STRING;
3356 rettv->vval.v_string = p;
3357}
3358
3359/*
3360 * "exists()" function
3361 */
3362 static void
3363f_exists(typval_T *argvars, typval_T *rettv)
3364{
3365 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003366 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003367
3368 p = get_tv_string(&argvars[0]);
3369 if (*p == '$') /* environment variable */
3370 {
3371 /* first try "normal" environment variables (fast) */
3372 if (mch_getenv(p + 1) != NULL)
3373 n = TRUE;
3374 else
3375 {
3376 /* try expanding things like $VIM and ${HOME} */
3377 p = expand_env_save(p);
3378 if (p != NULL && *p != '$')
3379 n = TRUE;
3380 vim_free(p);
3381 }
3382 }
3383 else if (*p == '&' || *p == '+') /* option */
3384 {
3385 n = (get_option_tv(&p, NULL, TRUE) == OK);
3386 if (*skipwhite(p) != NUL)
3387 n = FALSE; /* trailing garbage */
3388 }
3389 else if (*p == '*') /* internal or user defined function */
3390 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003391 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003392 }
3393 else if (*p == ':')
3394 {
3395 n = cmd_exists(p + 1);
3396 }
3397 else if (*p == '#')
3398 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003399 if (p[1] == '#')
3400 n = autocmd_supported(p + 2);
3401 else
3402 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003403 }
3404 else /* internal variable */
3405 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003406 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003407 }
3408
3409 rettv->vval.v_number = n;
3410}
3411
3412#ifdef FEAT_FLOAT
3413/*
3414 * "exp()" function
3415 */
3416 static void
3417f_exp(typval_T *argvars, typval_T *rettv)
3418{
3419 float_T f = 0.0;
3420
3421 rettv->v_type = VAR_FLOAT;
3422 if (get_float_arg(argvars, &f) == OK)
3423 rettv->vval.v_float = exp(f);
3424 else
3425 rettv->vval.v_float = 0.0;
3426}
3427#endif
3428
3429/*
3430 * "expand()" function
3431 */
3432 static void
3433f_expand(typval_T *argvars, typval_T *rettv)
3434{
3435 char_u *s;
3436 int len;
3437 char_u *errormsg;
3438 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3439 expand_T xpc;
3440 int error = FALSE;
3441 char_u *result;
3442
3443 rettv->v_type = VAR_STRING;
3444 if (argvars[1].v_type != VAR_UNKNOWN
3445 && argvars[2].v_type != VAR_UNKNOWN
3446 && get_tv_number_chk(&argvars[2], &error)
3447 && !error)
3448 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003449 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003450 }
3451
3452 s = get_tv_string(&argvars[0]);
3453 if (*s == '%' || *s == '#' || *s == '<')
3454 {
3455 ++emsg_off;
3456 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3457 --emsg_off;
3458 if (rettv->v_type == VAR_LIST)
3459 {
3460 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3461 list_append_string(rettv->vval.v_list, result, -1);
3462 else
3463 vim_free(result);
3464 }
3465 else
3466 rettv->vval.v_string = result;
3467 }
3468 else
3469 {
3470 /* When the optional second argument is non-zero, don't remove matches
3471 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3472 if (argvars[1].v_type != VAR_UNKNOWN
3473 && get_tv_number_chk(&argvars[1], &error))
3474 options |= WILD_KEEP_ALL;
3475 if (!error)
3476 {
3477 ExpandInit(&xpc);
3478 xpc.xp_context = EXPAND_FILES;
3479 if (p_wic)
3480 options += WILD_ICASE;
3481 if (rettv->v_type == VAR_STRING)
3482 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3483 options, WILD_ALL);
3484 else if (rettv_list_alloc(rettv) != FAIL)
3485 {
3486 int i;
3487
3488 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3489 for (i = 0; i < xpc.xp_numfiles; i++)
3490 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3491 ExpandCleanup(&xpc);
3492 }
3493 }
3494 else
3495 rettv->vval.v_string = NULL;
3496 }
3497}
3498
3499/*
3500 * "extend(list, list [, idx])" function
3501 * "extend(dict, dict [, action])" function
3502 */
3503 static void
3504f_extend(typval_T *argvars, typval_T *rettv)
3505{
3506 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3507
3508 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3509 {
3510 list_T *l1, *l2;
3511 listitem_T *item;
3512 long before;
3513 int error = FALSE;
3514
3515 l1 = argvars[0].vval.v_list;
3516 l2 = argvars[1].vval.v_list;
3517 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
3518 && l2 != NULL)
3519 {
3520 if (argvars[2].v_type != VAR_UNKNOWN)
3521 {
3522 before = (long)get_tv_number_chk(&argvars[2], &error);
3523 if (error)
3524 return; /* type error; errmsg already given */
3525
3526 if (before == l1->lv_len)
3527 item = NULL;
3528 else
3529 {
3530 item = list_find(l1, before);
3531 if (item == NULL)
3532 {
3533 EMSGN(_(e_listidx), before);
3534 return;
3535 }
3536 }
3537 }
3538 else
3539 item = NULL;
3540 list_extend(l1, l2, item);
3541
3542 copy_tv(&argvars[0], rettv);
3543 }
3544 }
3545 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3546 {
3547 dict_T *d1, *d2;
3548 char_u *action;
3549 int i;
3550
3551 d1 = argvars[0].vval.v_dict;
3552 d2 = argvars[1].vval.v_dict;
3553 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
3554 && d2 != NULL)
3555 {
3556 /* Check the third argument. */
3557 if (argvars[2].v_type != VAR_UNKNOWN)
3558 {
3559 static char *(av[]) = {"keep", "force", "error"};
3560
3561 action = get_tv_string_chk(&argvars[2]);
3562 if (action == NULL)
3563 return; /* type error; errmsg already given */
3564 for (i = 0; i < 3; ++i)
3565 if (STRCMP(action, av[i]) == 0)
3566 break;
3567 if (i == 3)
3568 {
3569 EMSG2(_(e_invarg2), action);
3570 return;
3571 }
3572 }
3573 else
3574 action = (char_u *)"force";
3575
3576 dict_extend(d1, d2, action);
3577
3578 copy_tv(&argvars[0], rettv);
3579 }
3580 }
3581 else
3582 EMSG2(_(e_listdictarg), "extend()");
3583}
3584
3585/*
3586 * "feedkeys()" function
3587 */
3588 static void
3589f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3590{
3591 int remap = TRUE;
3592 int insert = FALSE;
3593 char_u *keys, *flags;
3594 char_u nbuf[NUMBUFLEN];
3595 int typed = FALSE;
3596 int execute = FALSE;
3597 int dangerous = FALSE;
3598 char_u *keys_esc;
3599
3600 /* This is not allowed in the sandbox. If the commands would still be
3601 * executed in the sandbox it would be OK, but it probably happens later,
3602 * when "sandbox" is no longer set. */
3603 if (check_secure())
3604 return;
3605
3606 keys = get_tv_string(&argvars[0]);
3607
3608 if (argvars[1].v_type != VAR_UNKNOWN)
3609 {
3610 flags = get_tv_string_buf(&argvars[1], nbuf);
3611 for ( ; *flags != NUL; ++flags)
3612 {
3613 switch (*flags)
3614 {
3615 case 'n': remap = FALSE; break;
3616 case 'm': remap = TRUE; break;
3617 case 't': typed = TRUE; break;
3618 case 'i': insert = TRUE; break;
3619 case 'x': execute = TRUE; break;
3620 case '!': dangerous = TRUE; break;
3621 }
3622 }
3623 }
3624
3625 if (*keys != NUL || execute)
3626 {
3627 /* Need to escape K_SPECIAL and CSI before putting the string in the
3628 * typeahead buffer. */
3629 keys_esc = vim_strsave_escape_csi(keys);
3630 if (keys_esc != NULL)
3631 {
3632 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
3633 insert ? 0 : typebuf.tb_len, !typed, FALSE);
3634 vim_free(keys_esc);
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003635 if (vgetc_busy
3636#ifdef FEAT_TIMERS
3637 || timer_busy
3638#endif
3639 )
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003640 typebuf_was_filled = TRUE;
3641 if (execute)
3642 {
3643 int save_msg_scroll = msg_scroll;
3644
3645 /* Avoid a 1 second delay when the keys start Insert mode. */
3646 msg_scroll = FALSE;
3647
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003648 if (!dangerous)
3649 ++ex_normal_busy;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02003650 exec_normal(TRUE, FALSE, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003651 if (!dangerous)
3652 --ex_normal_busy;
3653
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003654 msg_scroll |= save_msg_scroll;
3655 }
3656 }
3657 }
3658}
3659
3660/*
3661 * "filereadable()" function
3662 */
3663 static void
3664f_filereadable(typval_T *argvars, typval_T *rettv)
3665{
3666 int fd;
3667 char_u *p;
3668 int n;
3669
3670#ifndef O_NONBLOCK
3671# define O_NONBLOCK 0
3672#endif
3673 p = get_tv_string(&argvars[0]);
3674 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3675 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3676 {
3677 n = TRUE;
3678 close(fd);
3679 }
3680 else
3681 n = FALSE;
3682
3683 rettv->vval.v_number = n;
3684}
3685
3686/*
3687 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3688 * rights to write into.
3689 */
3690 static void
3691f_filewritable(typval_T *argvars, typval_T *rettv)
3692{
3693 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
3694}
3695
3696 static void
3697findfilendir(
3698 typval_T *argvars UNUSED,
3699 typval_T *rettv,
3700 int find_what UNUSED)
3701{
3702#ifdef FEAT_SEARCHPATH
3703 char_u *fname;
3704 char_u *fresult = NULL;
3705 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3706 char_u *p;
3707 char_u pathbuf[NUMBUFLEN];
3708 int count = 1;
3709 int first = TRUE;
3710 int error = FALSE;
3711#endif
3712
3713 rettv->vval.v_string = NULL;
3714 rettv->v_type = VAR_STRING;
3715
3716#ifdef FEAT_SEARCHPATH
3717 fname = get_tv_string(&argvars[0]);
3718
3719 if (argvars[1].v_type != VAR_UNKNOWN)
3720 {
3721 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
3722 if (p == NULL)
3723 error = TRUE;
3724 else
3725 {
3726 if (*p != NUL)
3727 path = p;
3728
3729 if (argvars[2].v_type != VAR_UNKNOWN)
3730 count = (int)get_tv_number_chk(&argvars[2], &error);
3731 }
3732 }
3733
3734 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3735 error = TRUE;
3736
3737 if (*fname != NUL && !error)
3738 {
3739 do
3740 {
3741 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3742 vim_free(fresult);
3743 fresult = find_file_in_path_option(first ? fname : NULL,
3744 first ? (int)STRLEN(fname) : 0,
3745 0, first, path,
3746 find_what,
3747 curbuf->b_ffname,
3748 find_what == FINDFILE_DIR
3749 ? (char_u *)"" : curbuf->b_p_sua);
3750 first = FALSE;
3751
3752 if (fresult != NULL && rettv->v_type == VAR_LIST)
3753 list_append_string(rettv->vval.v_list, fresult, -1);
3754
3755 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3756 }
3757
3758 if (rettv->v_type == VAR_STRING)
3759 rettv->vval.v_string = fresult;
3760#endif
3761}
3762
3763/*
3764 * "filter()" function
3765 */
3766 static void
3767f_filter(typval_T *argvars, typval_T *rettv)
3768{
3769 filter_map(argvars, rettv, FALSE);
3770}
3771
3772/*
3773 * "finddir({fname}[, {path}[, {count}]])" function
3774 */
3775 static void
3776f_finddir(typval_T *argvars, typval_T *rettv)
3777{
3778 findfilendir(argvars, rettv, FINDFILE_DIR);
3779}
3780
3781/*
3782 * "findfile({fname}[, {path}[, {count}]])" function
3783 */
3784 static void
3785f_findfile(typval_T *argvars, typval_T *rettv)
3786{
3787 findfilendir(argvars, rettv, FINDFILE_FILE);
3788}
3789
3790#ifdef FEAT_FLOAT
3791/*
3792 * "float2nr({float})" function
3793 */
3794 static void
3795f_float2nr(typval_T *argvars, typval_T *rettv)
3796{
3797 float_T f = 0.0;
3798
3799 if (get_float_arg(argvars, &f) == OK)
3800 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003801 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003802 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003803 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003804 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003805 else
3806 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003807 }
3808}
3809
3810/*
3811 * "floor({float})" function
3812 */
3813 static void
3814f_floor(typval_T *argvars, typval_T *rettv)
3815{
3816 float_T f = 0.0;
3817
3818 rettv->v_type = VAR_FLOAT;
3819 if (get_float_arg(argvars, &f) == OK)
3820 rettv->vval.v_float = floor(f);
3821 else
3822 rettv->vval.v_float = 0.0;
3823}
3824
3825/*
3826 * "fmod()" function
3827 */
3828 static void
3829f_fmod(typval_T *argvars, typval_T *rettv)
3830{
3831 float_T fx = 0.0, fy = 0.0;
3832
3833 rettv->v_type = VAR_FLOAT;
3834 if (get_float_arg(argvars, &fx) == OK
3835 && get_float_arg(&argvars[1], &fy) == OK)
3836 rettv->vval.v_float = fmod(fx, fy);
3837 else
3838 rettv->vval.v_float = 0.0;
3839}
3840#endif
3841
3842/*
3843 * "fnameescape({string})" function
3844 */
3845 static void
3846f_fnameescape(typval_T *argvars, typval_T *rettv)
3847{
3848 rettv->vval.v_string = vim_strsave_fnameescape(
3849 get_tv_string(&argvars[0]), FALSE);
3850 rettv->v_type = VAR_STRING;
3851}
3852
3853/*
3854 * "fnamemodify({fname}, {mods})" function
3855 */
3856 static void
3857f_fnamemodify(typval_T *argvars, typval_T *rettv)
3858{
3859 char_u *fname;
3860 char_u *mods;
3861 int usedlen = 0;
3862 int len;
3863 char_u *fbuf = NULL;
3864 char_u buf[NUMBUFLEN];
3865
3866 fname = get_tv_string_chk(&argvars[0]);
3867 mods = get_tv_string_buf_chk(&argvars[1], buf);
3868 if (fname == NULL || mods == NULL)
3869 fname = NULL;
3870 else
3871 {
3872 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02003873 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003874 }
3875
3876 rettv->v_type = VAR_STRING;
3877 if (fname == NULL)
3878 rettv->vval.v_string = NULL;
3879 else
3880 rettv->vval.v_string = vim_strnsave(fname, len);
3881 vim_free(fbuf);
3882}
3883
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003884/*
3885 * "foldclosed()" function
3886 */
3887 static void
3888foldclosed_both(
3889 typval_T *argvars UNUSED,
3890 typval_T *rettv,
3891 int end UNUSED)
3892{
3893#ifdef FEAT_FOLDING
3894 linenr_T lnum;
3895 linenr_T first, last;
3896
3897 lnum = get_tv_lnum(argvars);
3898 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3899 {
3900 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3901 {
3902 if (end)
3903 rettv->vval.v_number = (varnumber_T)last;
3904 else
3905 rettv->vval.v_number = (varnumber_T)first;
3906 return;
3907 }
3908 }
3909#endif
3910 rettv->vval.v_number = -1;
3911}
3912
3913/*
3914 * "foldclosed()" function
3915 */
3916 static void
3917f_foldclosed(typval_T *argvars, typval_T *rettv)
3918{
3919 foldclosed_both(argvars, rettv, FALSE);
3920}
3921
3922/*
3923 * "foldclosedend()" function
3924 */
3925 static void
3926f_foldclosedend(typval_T *argvars, typval_T *rettv)
3927{
3928 foldclosed_both(argvars, rettv, TRUE);
3929}
3930
3931/*
3932 * "foldlevel()" function
3933 */
3934 static void
3935f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3936{
3937#ifdef FEAT_FOLDING
3938 linenr_T lnum;
3939
3940 lnum = get_tv_lnum(argvars);
3941 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3942 rettv->vval.v_number = foldLevel(lnum);
3943#endif
3944}
3945
3946/*
3947 * "foldtext()" function
3948 */
3949 static void
3950f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
3951{
3952#ifdef FEAT_FOLDING
3953 linenr_T foldstart;
3954 linenr_T foldend;
3955 char_u *dashes;
3956 linenr_T lnum;
3957 char_u *s;
3958 char_u *r;
3959 int len;
3960 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003961 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003962#endif
3963
3964 rettv->v_type = VAR_STRING;
3965 rettv->vval.v_string = NULL;
3966#ifdef FEAT_FOLDING
3967 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
3968 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
3969 dashes = get_vim_var_str(VV_FOLDDASHES);
3970 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
3971 && dashes != NULL)
3972 {
3973 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02003974 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003975 if (!linewhite(lnum))
3976 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003977
3978 /* Find interesting text in this line. */
3979 s = skipwhite(ml_get(lnum));
3980 /* skip C comment-start */
3981 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
3982 {
3983 s = skipwhite(s + 2);
3984 if (*skipwhite(s) == NUL
3985 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
3986 {
3987 s = skipwhite(ml_get(lnum + 1));
3988 if (*s == '*')
3989 s = skipwhite(s + 1);
3990 }
3991 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02003992 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003993 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003994 r = alloc((unsigned)(STRLEN(txt)
3995 + STRLEN(dashes) /* for %s */
3996 + 20 /* for %3ld */
3997 + STRLEN(s))); /* concatenated */
3998 if (r != NULL)
3999 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02004000 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004001 len = (int)STRLEN(r);
4002 STRCAT(r, s);
4003 /* remove 'foldmarker' and 'commentstring' */
4004 foldtext_cleanup(r + len);
4005 rettv->vval.v_string = r;
4006 }
4007 }
4008#endif
4009}
4010
4011/*
4012 * "foldtextresult(lnum)" function
4013 */
4014 static void
4015f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
4016{
4017#ifdef FEAT_FOLDING
4018 linenr_T lnum;
4019 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004020 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004021 foldinfo_T foldinfo;
4022 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004023 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004024#endif
4025
4026 rettv->v_type = VAR_STRING;
4027 rettv->vval.v_string = NULL;
4028#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004029 if (entered)
4030 return; /* reject recursive use */
4031 entered = TRUE;
4032
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004033 lnum = get_tv_lnum(argvars);
4034 /* treat illegal types and illegal string values for {lnum} the same */
4035 if (lnum < 0)
4036 lnum = 0;
4037 fold_count = foldedCount(curwin, lnum, &foldinfo);
4038 if (fold_count > 0)
4039 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01004040 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
4041 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004042 if (text == buf)
4043 text = vim_strsave(text);
4044 rettv->vval.v_string = text;
4045 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004046
4047 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004048#endif
4049}
4050
4051/*
4052 * "foreground()" function
4053 */
4054 static void
4055f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4056{
4057#ifdef FEAT_GUI
4058 if (gui.in_use)
4059 gui_mch_set_foreground();
4060#else
4061# ifdef WIN32
4062 win32_set_foreground();
4063# endif
4064#endif
4065}
4066
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004067 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004068common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004069{
4070 char_u *s;
4071 char_u *name;
4072 int use_string = FALSE;
4073 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004074 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004075
4076 if (argvars[0].v_type == VAR_FUNC)
4077 {
4078 /* function(MyFunc, [arg], dict) */
4079 s = argvars[0].vval.v_string;
4080 }
4081 else if (argvars[0].v_type == VAR_PARTIAL
4082 && argvars[0].vval.v_partial != NULL)
4083 {
4084 /* function(dict.MyFunc, [arg]) */
4085 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004086 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004087 }
4088 else
4089 {
4090 /* function('MyFunc', [arg], dict) */
4091 s = get_tv_string(&argvars[0]);
4092 use_string = TRUE;
4093 }
4094
Bram Moolenaar843b8842016-08-21 14:36:15 +02004095 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004096 {
4097 name = s;
4098 trans_name = trans_function_name(&name, FALSE,
4099 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4100 if (*name != NUL)
4101 s = NULL;
4102 }
4103
Bram Moolenaar843b8842016-08-21 14:36:15 +02004104 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4105 || (is_funcref && trans_name == NULL))
Bram Moolenaar5582ef12016-09-14 22:16:13 +02004106 EMSG2(_(e_invarg2), use_string ? get_tv_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004107 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004108 else if (trans_name != NULL && (is_funcref
4109 ? find_func(trans_name) == NULL
4110 : !translated_function_exists(trans_name)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004111 EMSG2(_("E700: Unknown function: %s"), s);
4112 else
4113 {
4114 int dict_idx = 0;
4115 int arg_idx = 0;
4116 list_T *list = NULL;
4117
4118 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4119 {
4120 char sid_buf[25];
4121 int off = *s == 's' ? 2 : 5;
4122
4123 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4124 * also be called from another script. Using trans_function_name()
4125 * would also work, but some plugins depend on the name being
4126 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004127 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004128 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
4129 if (name != NULL)
4130 {
4131 STRCPY(name, sid_buf);
4132 STRCAT(name, s + off);
4133 }
4134 }
4135 else
4136 name = vim_strsave(s);
4137
4138 if (argvars[1].v_type != VAR_UNKNOWN)
4139 {
4140 if (argvars[2].v_type != VAR_UNKNOWN)
4141 {
4142 /* function(name, [args], dict) */
4143 arg_idx = 1;
4144 dict_idx = 2;
4145 }
4146 else if (argvars[1].v_type == VAR_DICT)
4147 /* function(name, dict) */
4148 dict_idx = 1;
4149 else
4150 /* function(name, [args]) */
4151 arg_idx = 1;
4152 if (dict_idx > 0)
4153 {
4154 if (argvars[dict_idx].v_type != VAR_DICT)
4155 {
4156 EMSG(_("E922: expected a dict"));
4157 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004158 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004159 }
4160 if (argvars[dict_idx].vval.v_dict == NULL)
4161 dict_idx = 0;
4162 }
4163 if (arg_idx > 0)
4164 {
4165 if (argvars[arg_idx].v_type != VAR_LIST)
4166 {
4167 EMSG(_("E923: Second argument of function() must be a list or a dict"));
4168 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004169 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004170 }
4171 list = argvars[arg_idx].vval.v_list;
4172 if (list == NULL || list->lv_len == 0)
4173 arg_idx = 0;
4174 }
4175 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004176 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004177 {
4178 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
4179
4180 /* result is a VAR_PARTIAL */
4181 if (pt == NULL)
4182 vim_free(name);
4183 else
4184 {
4185 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4186 {
4187 listitem_T *li;
4188 int i = 0;
4189 int arg_len = 0;
4190 int lv_len = 0;
4191
4192 if (arg_pt != NULL)
4193 arg_len = arg_pt->pt_argc;
4194 if (list != NULL)
4195 lv_len = list->lv_len;
4196 pt->pt_argc = arg_len + lv_len;
4197 pt->pt_argv = (typval_T *)alloc(
4198 sizeof(typval_T) * pt->pt_argc);
4199 if (pt->pt_argv == NULL)
4200 {
4201 vim_free(pt);
4202 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004203 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004204 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004205 for (i = 0; i < arg_len; i++)
4206 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4207 if (lv_len > 0)
4208 for (li = list->lv_first; li != NULL;
4209 li = li->li_next)
4210 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004211 }
4212
4213 /* For "function(dict.func, [], dict)" and "func" is a partial
4214 * use "dict". That is backwards compatible. */
4215 if (dict_idx > 0)
4216 {
4217 /* The dict is bound explicitly, pt_auto is FALSE. */
4218 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4219 ++pt->pt_dict->dv_refcount;
4220 }
4221 else if (arg_pt != NULL)
4222 {
4223 /* If the dict was bound automatically the result is also
4224 * bound automatically. */
4225 pt->pt_dict = arg_pt->pt_dict;
4226 pt->pt_auto = arg_pt->pt_auto;
4227 if (pt->pt_dict != NULL)
4228 ++pt->pt_dict->dv_refcount;
4229 }
4230
4231 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004232 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4233 {
4234 pt->pt_func = arg_pt->pt_func;
4235 func_ptr_ref(pt->pt_func);
4236 vim_free(name);
4237 }
4238 else if (is_funcref)
4239 {
4240 pt->pt_func = find_func(trans_name);
4241 func_ptr_ref(pt->pt_func);
4242 vim_free(name);
4243 }
4244 else
4245 {
4246 pt->pt_name = name;
4247 func_ref(name);
4248 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004249 }
4250 rettv->v_type = VAR_PARTIAL;
4251 rettv->vval.v_partial = pt;
4252 }
4253 else
4254 {
4255 /* result is a VAR_FUNC */
4256 rettv->v_type = VAR_FUNC;
4257 rettv->vval.v_string = name;
4258 func_ref(name);
4259 }
4260 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004261theend:
4262 vim_free(trans_name);
4263}
4264
4265/*
4266 * "funcref()" function
4267 */
4268 static void
4269f_funcref(typval_T *argvars, typval_T *rettv)
4270{
4271 common_function(argvars, rettv, TRUE);
4272}
4273
4274/*
4275 * "function()" function
4276 */
4277 static void
4278f_function(typval_T *argvars, typval_T *rettv)
4279{
4280 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004281}
4282
4283/*
4284 * "garbagecollect()" function
4285 */
4286 static void
4287f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4288{
4289 /* This is postponed until we are back at the toplevel, because we may be
4290 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4291 want_garbage_collect = TRUE;
4292
4293 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
4294 garbage_collect_at_exit = TRUE;
4295}
4296
4297/*
4298 * "get()" function
4299 */
4300 static void
4301f_get(typval_T *argvars, typval_T *rettv)
4302{
4303 listitem_T *li;
4304 list_T *l;
4305 dictitem_T *di;
4306 dict_T *d;
4307 typval_T *tv = NULL;
4308
4309 if (argvars[0].v_type == VAR_LIST)
4310 {
4311 if ((l = argvars[0].vval.v_list) != NULL)
4312 {
4313 int error = FALSE;
4314
4315 li = list_find(l, (long)get_tv_number_chk(&argvars[1], &error));
4316 if (!error && li != NULL)
4317 tv = &li->li_tv;
4318 }
4319 }
4320 else if (argvars[0].v_type == VAR_DICT)
4321 {
4322 if ((d = argvars[0].vval.v_dict) != NULL)
4323 {
4324 di = dict_find(d, get_tv_string(&argvars[1]), -1);
4325 if (di != NULL)
4326 tv = &di->di_tv;
4327 }
4328 }
4329 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4330 {
4331 partial_T *pt;
4332 partial_T fref_pt;
4333
4334 if (argvars[0].v_type == VAR_PARTIAL)
4335 pt = argvars[0].vval.v_partial;
4336 else
4337 {
4338 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4339 fref_pt.pt_name = argvars[0].vval.v_string;
4340 pt = &fref_pt;
4341 }
4342
4343 if (pt != NULL)
4344 {
4345 char_u *what = get_tv_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004346 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004347
4348 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4349 {
4350 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004351 n = partial_name(pt);
4352 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004353 rettv->vval.v_string = NULL;
4354 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004355 {
4356 rettv->vval.v_string = vim_strsave(n);
4357 if (rettv->v_type == VAR_FUNC)
4358 func_ref(rettv->vval.v_string);
4359 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004360 }
4361 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004362 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004363 else if (STRCMP(what, "args") == 0)
4364 {
4365 rettv->v_type = VAR_LIST;
4366 if (rettv_list_alloc(rettv) == OK)
4367 {
4368 int i;
4369
4370 for (i = 0; i < pt->pt_argc; ++i)
4371 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4372 }
4373 }
4374 else
4375 EMSG2(_(e_invarg2), what);
4376 return;
4377 }
4378 }
4379 else
4380 EMSG2(_(e_listdictarg), "get()");
4381
4382 if (tv == NULL)
4383 {
4384 if (argvars[2].v_type != VAR_UNKNOWN)
4385 copy_tv(&argvars[2], rettv);
4386 }
4387 else
4388 copy_tv(tv, rettv);
4389}
4390
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004391#ifdef FEAT_SIGNS
4392/*
4393 * Returns information about signs placed in a buffer as list of dicts.
4394 */
4395 static void
4396get_buffer_signs(buf_T *buf, list_T *l)
4397{
4398 signlist_T *sign;
4399
4400 for (sign = buf->b_signlist; sign; sign = sign->next)
4401 {
4402 dict_T *d = dict_alloc();
4403
4404 if (d != NULL)
4405 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02004406 dict_add_number(d, "id", sign->id);
4407 dict_add_number(d, "lnum", sign->lnum);
4408 dict_add_string(d, "name", sign_typenr2name(sign->typenr));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004409
4410 list_append_dict(l, d);
4411 }
4412 }
4413}
4414#endif
4415
4416/*
4417 * Returns buffer options, variables and other attributes in a dictionary.
4418 */
4419 static dict_T *
4420get_buffer_info(buf_T *buf)
4421{
4422 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004423 tabpage_T *tp;
4424 win_T *wp;
4425 list_T *windows;
4426
4427 dict = dict_alloc();
4428 if (dict == NULL)
4429 return NULL;
4430
Bram Moolenaare0be1672018-07-08 16:50:37 +02004431 dict_add_number(dict, "bufnr", buf->b_fnum);
4432 dict_add_string(dict, "name", buf->b_ffname);
4433 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4434 : buflist_findlnum(buf));
4435 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4436 dict_add_number(dict, "listed", buf->b_p_bl);
4437 dict_add_number(dict, "changed", bufIsChanged(buf));
4438 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4439 dict_add_number(dict, "hidden",
4440 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004441
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004442 /* Get a reference to buffer variables */
4443 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004444
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004445 /* List of windows displaying this buffer */
4446 windows = list_alloc();
4447 if (windows != NULL)
4448 {
4449 FOR_ALL_TAB_WINDOWS(tp, wp)
4450 if (wp->w_buffer == buf)
4451 list_append_number(windows, (varnumber_T)wp->w_id);
4452 dict_add_list(dict, "windows", windows);
4453 }
4454
4455#ifdef FEAT_SIGNS
4456 if (buf->b_signlist != NULL)
4457 {
4458 /* List of signs placed in this buffer */
4459 list_T *signs = list_alloc();
4460 if (signs != NULL)
4461 {
4462 get_buffer_signs(buf, signs);
4463 dict_add_list(dict, "signs", signs);
4464 }
4465 }
4466#endif
4467
4468 return dict;
4469}
4470
4471/*
4472 * "getbufinfo()" function
4473 */
4474 static void
4475f_getbufinfo(typval_T *argvars, typval_T *rettv)
4476{
4477 buf_T *buf = NULL;
4478 buf_T *argbuf = NULL;
4479 dict_T *d;
4480 int filtered = FALSE;
4481 int sel_buflisted = FALSE;
4482 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004483 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004484
4485 if (rettv_list_alloc(rettv) != OK)
4486 return;
4487
4488 /* List of all the buffers or selected buffers */
4489 if (argvars[0].v_type == VAR_DICT)
4490 {
4491 dict_T *sel_d = argvars[0].vval.v_dict;
4492
4493 if (sel_d != NULL)
4494 {
4495 dictitem_T *di;
4496
4497 filtered = TRUE;
4498
4499 di = dict_find(sel_d, (char_u *)"buflisted", -1);
4500 if (di != NULL && get_tv_number(&di->di_tv))
4501 sel_buflisted = TRUE;
4502
4503 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
4504 if (di != NULL && get_tv_number(&di->di_tv))
4505 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004506
4507 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
4508 if (di != NULL && get_tv_number(&di->di_tv))
4509 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004510 }
4511 }
4512 else if (argvars[0].v_type != VAR_UNKNOWN)
4513 {
4514 /* Information about one buffer. Argument specifies the buffer */
4515 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4516 ++emsg_off;
4517 argbuf = get_buf_tv(&argvars[0], FALSE);
4518 --emsg_off;
4519 if (argbuf == NULL)
4520 return;
4521 }
4522
4523 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004524 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004525 {
4526 if (argbuf != NULL && argbuf != buf)
4527 continue;
4528 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004529 || (sel_buflisted && !buf->b_p_bl)
4530 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004531 continue;
4532
4533 d = get_buffer_info(buf);
4534 if (d != NULL)
4535 list_append_dict(rettv->vval.v_list, d);
4536 if (argbuf != NULL)
4537 return;
4538 }
4539}
4540
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004541/*
4542 * Get line or list of lines from buffer "buf" into "rettv".
4543 * Return a range (from start to end) of lines in rettv from the specified
4544 * buffer.
4545 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4546 */
4547 static void
4548get_buffer_lines(
4549 buf_T *buf,
4550 linenr_T start,
4551 linenr_T end,
4552 int retlist,
4553 typval_T *rettv)
4554{
4555 char_u *p;
4556
4557 rettv->v_type = VAR_STRING;
4558 rettv->vval.v_string = NULL;
4559 if (retlist && rettv_list_alloc(rettv) == FAIL)
4560 return;
4561
4562 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4563 return;
4564
4565 if (!retlist)
4566 {
4567 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4568 p = ml_get_buf(buf, start, FALSE);
4569 else
4570 p = (char_u *)"";
4571 rettv->vval.v_string = vim_strsave(p);
4572 }
4573 else
4574 {
4575 if (end < start)
4576 return;
4577
4578 if (start < 1)
4579 start = 1;
4580 if (end > buf->b_ml.ml_line_count)
4581 end = buf->b_ml.ml_line_count;
4582 while (start <= end)
4583 if (list_append_string(rettv->vval.v_list,
4584 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4585 break;
4586 }
4587}
4588
4589/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004590 * "getbufline()" function
4591 */
4592 static void
4593f_getbufline(typval_T *argvars, typval_T *rettv)
4594{
4595 linenr_T lnum;
4596 linenr_T end;
4597 buf_T *buf;
4598
4599 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4600 ++emsg_off;
4601 buf = get_buf_tv(&argvars[0], FALSE);
4602 --emsg_off;
4603
4604 lnum = get_tv_lnum_buf(&argvars[1], buf);
4605 if (argvars[2].v_type == VAR_UNKNOWN)
4606 end = lnum;
4607 else
4608 end = get_tv_lnum_buf(&argvars[2], buf);
4609
4610 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4611}
4612
4613/*
4614 * "getbufvar()" function
4615 */
4616 static void
4617f_getbufvar(typval_T *argvars, typval_T *rettv)
4618{
4619 buf_T *buf;
4620 buf_T *save_curbuf;
4621 char_u *varname;
4622 dictitem_T *v;
4623 int done = FALSE;
4624
4625 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4626 varname = get_tv_string_chk(&argvars[1]);
4627 ++emsg_off;
4628 buf = get_buf_tv(&argvars[0], FALSE);
4629
4630 rettv->v_type = VAR_STRING;
4631 rettv->vval.v_string = NULL;
4632
4633 if (buf != NULL && varname != NULL)
4634 {
4635 /* set curbuf to be our buf, temporarily */
4636 save_curbuf = curbuf;
4637 curbuf = buf;
4638
Bram Moolenaar30567352016-08-27 21:25:44 +02004639 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004640 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004641 if (varname[1] == NUL)
4642 {
4643 /* get all buffer-local options in a dict */
4644 dict_T *opts = get_winbuf_options(TRUE);
4645
4646 if (opts != NULL)
4647 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004648 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004649 done = TRUE;
4650 }
4651 }
4652 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4653 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004654 done = TRUE;
4655 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004656 else
4657 {
4658 /* Look up the variable. */
4659 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4660 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4661 'b', varname, FALSE);
4662 if (v != NULL)
4663 {
4664 copy_tv(&v->di_tv, rettv);
4665 done = TRUE;
4666 }
4667 }
4668
4669 /* restore previous notion of curbuf */
4670 curbuf = save_curbuf;
4671 }
4672
4673 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4674 /* use the default value */
4675 copy_tv(&argvars[2], rettv);
4676
4677 --emsg_off;
4678}
4679
4680/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004681 * "getchangelist()" function
4682 */
4683 static void
4684f_getchangelist(typval_T *argvars, typval_T *rettv)
4685{
4686#ifdef FEAT_JUMPLIST
4687 buf_T *buf;
4688 int i;
4689 list_T *l;
4690 dict_T *d;
4691#endif
4692
4693 if (rettv_list_alloc(rettv) != OK)
4694 return;
4695
4696#ifdef FEAT_JUMPLIST
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004697 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4698 ++emsg_off;
4699 buf = get_buf_tv(&argvars[0], FALSE);
4700 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004701 if (buf == NULL)
4702 return;
4703
4704 l = list_alloc();
4705 if (l == NULL)
4706 return;
4707
4708 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4709 return;
4710 /*
4711 * The current window change list index tracks only the position in the
4712 * current buffer change list. For other buffers, use the change list
4713 * length as the current index.
4714 */
4715 list_append_number(rettv->vval.v_list,
4716 (varnumber_T)((buf == curwin->w_buffer)
4717 ? curwin->w_changelistidx : buf->b_changelistlen));
4718
4719 for (i = 0; i < buf->b_changelistlen; ++i)
4720 {
4721 if (buf->b_changelist[i].lnum == 0)
4722 continue;
4723 if ((d = dict_alloc()) == NULL)
4724 return;
4725 if (list_append_dict(l, d) == FAIL)
4726 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004727 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4728 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004729# ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +02004730 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004731# endif
4732 }
4733#endif
4734}
4735/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004736 * "getchar()" function
4737 */
4738 static void
4739f_getchar(typval_T *argvars, typval_T *rettv)
4740{
4741 varnumber_T n;
4742 int error = FALSE;
4743
Bram Moolenaar84d93902018-09-11 20:10:20 +02004744#ifdef MESSAGE_QUEUE
4745 // vpeekc() used to check for messages, but that caused problems, invoking
4746 // a callback where it was not expected. Some plugins use getchar(1) in a
4747 // loop to await a message, therefore make sure we check for messages here.
4748 parse_queued_messages();
4749#endif
4750
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004751 /* Position the cursor. Needed after a message that ends in a space. */
4752 windgoto(msg_row, msg_col);
4753
4754 ++no_mapping;
4755 ++allow_keys;
4756 for (;;)
4757 {
4758 if (argvars[0].v_type == VAR_UNKNOWN)
4759 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004760 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004761 else if (get_tv_number_chk(&argvars[0], &error) == 1)
4762 /* getchar(1): only check if char avail */
4763 n = vpeekc_any();
4764 else if (error || vpeekc_any() == NUL)
4765 /* illegal argument or getchar(0) and no char avail: return zero */
4766 n = 0;
4767 else
4768 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004769 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004770
4771 if (n == K_IGNORE)
4772 continue;
4773 break;
4774 }
4775 --no_mapping;
4776 --allow_keys;
4777
4778 set_vim_var_nr(VV_MOUSE_WIN, 0);
4779 set_vim_var_nr(VV_MOUSE_WINID, 0);
4780 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4781 set_vim_var_nr(VV_MOUSE_COL, 0);
4782
4783 rettv->vval.v_number = n;
4784 if (IS_SPECIAL(n) || mod_mask != 0)
4785 {
4786 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4787 int i = 0;
4788
4789 /* Turn a special key into three bytes, plus modifier. */
4790 if (mod_mask != 0)
4791 {
4792 temp[i++] = K_SPECIAL;
4793 temp[i++] = KS_MODIFIER;
4794 temp[i++] = mod_mask;
4795 }
4796 if (IS_SPECIAL(n))
4797 {
4798 temp[i++] = K_SPECIAL;
4799 temp[i++] = K_SECOND(n);
4800 temp[i++] = K_THIRD(n);
4801 }
4802#ifdef FEAT_MBYTE
4803 else if (has_mbyte)
4804 i += (*mb_char2bytes)(n, temp + i);
4805#endif
4806 else
4807 temp[i++] = n;
4808 temp[i++] = NUL;
4809 rettv->v_type = VAR_STRING;
4810 rettv->vval.v_string = vim_strsave(temp);
4811
4812#ifdef FEAT_MOUSE
4813 if (is_mouse_key(n))
4814 {
4815 int row = mouse_row;
4816 int col = mouse_col;
4817 win_T *win;
4818 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004819 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004820 int winnr = 1;
4821
4822 if (row >= 0 && col >= 0)
4823 {
4824 /* Find the window at the mouse coordinates and compute the
4825 * text position. */
4826 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004827 if (win == NULL)
4828 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004829 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004830 for (wp = firstwin; wp != win; wp = wp->w_next)
4831 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004832 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4833 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4834 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4835 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4836 }
4837 }
4838#endif
4839 }
4840}
4841
4842/*
4843 * "getcharmod()" function
4844 */
4845 static void
4846f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4847{
4848 rettv->vval.v_number = mod_mask;
4849}
4850
4851/*
4852 * "getcharsearch()" function
4853 */
4854 static void
4855f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4856{
4857 if (rettv_dict_alloc(rettv) != FAIL)
4858 {
4859 dict_T *dict = rettv->vval.v_dict;
4860
Bram Moolenaare0be1672018-07-08 16:50:37 +02004861 dict_add_string(dict, "char", last_csearch());
4862 dict_add_number(dict, "forward", last_csearch_forward());
4863 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004864 }
4865}
4866
4867/*
4868 * "getcmdline()" function
4869 */
4870 static void
4871f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4872{
4873 rettv->v_type = VAR_STRING;
4874 rettv->vval.v_string = get_cmdline_str();
4875}
4876
4877/*
4878 * "getcmdpos()" function
4879 */
4880 static void
4881f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4882{
4883 rettv->vval.v_number = get_cmdline_pos() + 1;
4884}
4885
4886/*
4887 * "getcmdtype()" function
4888 */
4889 static void
4890f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4891{
4892 rettv->v_type = VAR_STRING;
4893 rettv->vval.v_string = alloc(2);
4894 if (rettv->vval.v_string != NULL)
4895 {
4896 rettv->vval.v_string[0] = get_cmdline_type();
4897 rettv->vval.v_string[1] = NUL;
4898 }
4899}
4900
4901/*
4902 * "getcmdwintype()" function
4903 */
4904 static void
4905f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4906{
4907 rettv->v_type = VAR_STRING;
4908 rettv->vval.v_string = NULL;
4909#ifdef FEAT_CMDWIN
4910 rettv->vval.v_string = alloc(2);
4911 if (rettv->vval.v_string != NULL)
4912 {
4913 rettv->vval.v_string[0] = cmdwin_type;
4914 rettv->vval.v_string[1] = NUL;
4915 }
4916#endif
4917}
4918
4919#if defined(FEAT_CMDL_COMPL)
4920/*
4921 * "getcompletion()" function
4922 */
4923 static void
4924f_getcompletion(typval_T *argvars, typval_T *rettv)
4925{
4926 char_u *pat;
4927 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004928 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004929 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
4930 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004931
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004932 if (argvars[2].v_type != VAR_UNKNOWN)
4933 filtered = get_tv_number_chk(&argvars[2], NULL);
4934
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004935 if (p_wic)
4936 options |= WILD_ICASE;
4937
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004938 /* For filtered results, 'wildignore' is used */
4939 if (!filtered)
4940 options |= WILD_KEEP_ALL;
4941
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004942 ExpandInit(&xpc);
4943 xpc.xp_pattern = get_tv_string(&argvars[0]);
4944 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4945 xpc.xp_context = cmdcomplete_str_to_type(get_tv_string(&argvars[1]));
4946 if (xpc.xp_context == EXPAND_NOTHING)
4947 {
4948 if (argvars[1].v_type == VAR_STRING)
4949 EMSG2(_(e_invarg2), argvars[1].vval.v_string);
4950 else
4951 EMSG(_(e_invarg));
4952 return;
4953 }
4954
4955# if defined(FEAT_MENU)
4956 if (xpc.xp_context == EXPAND_MENUS)
4957 {
4958 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4959 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4960 }
4961# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02004962#ifdef FEAT_CSCOPE
4963 if (xpc.xp_context == EXPAND_CSCOPE)
4964 {
4965 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4966 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4967 }
4968#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02004969#ifdef FEAT_SIGNS
4970 if (xpc.xp_context == EXPAND_SIGN)
4971 {
4972 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4973 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4974 }
4975#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004976
4977 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4978 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
4979 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004980 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004981
4982 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4983
4984 for (i = 0; i < xpc.xp_numfiles; i++)
4985 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4986 }
4987 vim_free(pat);
4988 ExpandCleanup(&xpc);
4989}
4990#endif
4991
4992/*
4993 * "getcwd()" function
4994 */
4995 static void
4996f_getcwd(typval_T *argvars, typval_T *rettv)
4997{
4998 win_T *wp = NULL;
4999 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01005000 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005001
5002 rettv->v_type = VAR_STRING;
5003 rettv->vval.v_string = NULL;
5004
Bram Moolenaar54591292018-02-09 20:53:59 +01005005 if (argvars[0].v_type == VAR_NUMBER && argvars[0].vval.v_number == -1)
5006 global = TRUE;
5007 else
5008 wp = find_tabwin(&argvars[0], &argvars[1]);
5009
5010 if (wp != NULL && wp->w_localdir != NULL)
5011 rettv->vval.v_string = vim_strsave(wp->w_localdir);
5012 else if (wp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005013 {
Bram Moolenaar54591292018-02-09 20:53:59 +01005014 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005015 rettv->vval.v_string = vim_strsave(globaldir);
5016 else
5017 {
5018 cwd = alloc(MAXPATHL);
5019 if (cwd != NULL)
5020 {
5021 if (mch_dirname(cwd, MAXPATHL) != FAIL)
5022 rettv->vval.v_string = vim_strsave(cwd);
5023 vim_free(cwd);
5024 }
5025 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005026 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02005027#ifdef BACKSLASH_IN_FILENAME
5028 if (rettv->vval.v_string != NULL)
5029 slash_adjust(rettv->vval.v_string);
5030#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005031}
5032
5033/*
5034 * "getfontname()" function
5035 */
5036 static void
5037f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
5038{
5039 rettv->v_type = VAR_STRING;
5040 rettv->vval.v_string = NULL;
5041#ifdef FEAT_GUI
5042 if (gui.in_use)
5043 {
5044 GuiFont font;
5045 char_u *name = NULL;
5046
5047 if (argvars[0].v_type == VAR_UNKNOWN)
5048 {
5049 /* Get the "Normal" font. Either the name saved by
5050 * hl_set_font_name() or from the font ID. */
5051 font = gui.norm_font;
5052 name = hl_get_font_name();
5053 }
5054 else
5055 {
5056 name = get_tv_string(&argvars[0]);
5057 if (STRCMP(name, "*") == 0) /* don't use font dialog */
5058 return;
5059 font = gui_mch_get_font(name, FALSE);
5060 if (font == NOFONT)
5061 return; /* Invalid font name, return empty string. */
5062 }
5063 rettv->vval.v_string = gui_mch_get_fontname(font, name);
5064 if (argvars[0].v_type != VAR_UNKNOWN)
5065 gui_mch_free_font(font);
5066 }
5067#endif
5068}
5069
5070/*
5071 * "getfperm({fname})" function
5072 */
5073 static void
5074f_getfperm(typval_T *argvars, typval_T *rettv)
5075{
5076 char_u *fname;
5077 stat_T st;
5078 char_u *perm = NULL;
5079 char_u flags[] = "rwx";
5080 int i;
5081
5082 fname = get_tv_string(&argvars[0]);
5083
5084 rettv->v_type = VAR_STRING;
5085 if (mch_stat((char *)fname, &st) >= 0)
5086 {
5087 perm = vim_strsave((char_u *)"---------");
5088 if (perm != NULL)
5089 {
5090 for (i = 0; i < 9; i++)
5091 {
5092 if (st.st_mode & (1 << (8 - i)))
5093 perm[i] = flags[i % 3];
5094 }
5095 }
5096 }
5097 rettv->vval.v_string = perm;
5098}
5099
5100/*
5101 * "getfsize({fname})" function
5102 */
5103 static void
5104f_getfsize(typval_T *argvars, typval_T *rettv)
5105{
5106 char_u *fname;
5107 stat_T st;
5108
5109 fname = get_tv_string(&argvars[0]);
5110
5111 rettv->v_type = VAR_NUMBER;
5112
5113 if (mch_stat((char *)fname, &st) >= 0)
5114 {
5115 if (mch_isdir(fname))
5116 rettv->vval.v_number = 0;
5117 else
5118 {
5119 rettv->vval.v_number = (varnumber_T)st.st_size;
5120
5121 /* non-perfect check for overflow */
5122 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5123 rettv->vval.v_number = -2;
5124 }
5125 }
5126 else
5127 rettv->vval.v_number = -1;
5128}
5129
5130/*
5131 * "getftime({fname})" function
5132 */
5133 static void
5134f_getftime(typval_T *argvars, typval_T *rettv)
5135{
5136 char_u *fname;
5137 stat_T st;
5138
5139 fname = get_tv_string(&argvars[0]);
5140
5141 if (mch_stat((char *)fname, &st) >= 0)
5142 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5143 else
5144 rettv->vval.v_number = -1;
5145}
5146
5147/*
5148 * "getftype({fname})" function
5149 */
5150 static void
5151f_getftype(typval_T *argvars, typval_T *rettv)
5152{
5153 char_u *fname;
5154 stat_T st;
5155 char_u *type = NULL;
5156 char *t;
5157
5158 fname = get_tv_string(&argvars[0]);
5159
5160 rettv->v_type = VAR_STRING;
5161 if (mch_lstat((char *)fname, &st) >= 0)
5162 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005163 if (S_ISREG(st.st_mode))
5164 t = "file";
5165 else if (S_ISDIR(st.st_mode))
5166 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005167 else if (S_ISLNK(st.st_mode))
5168 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005169 else if (S_ISBLK(st.st_mode))
5170 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005171 else if (S_ISCHR(st.st_mode))
5172 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005173 else if (S_ISFIFO(st.st_mode))
5174 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005175 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005176 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005177 else
5178 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005179 type = vim_strsave((char_u *)t);
5180 }
5181 rettv->vval.v_string = type;
5182}
5183
5184/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005185 * "getjumplist()" function
5186 */
5187 static void
5188f_getjumplist(typval_T *argvars, typval_T *rettv)
5189{
5190#ifdef FEAT_JUMPLIST
5191 win_T *wp;
5192 int i;
5193 list_T *l;
5194 dict_T *d;
5195#endif
5196
5197 if (rettv_list_alloc(rettv) != OK)
5198 return;
5199
5200#ifdef FEAT_JUMPLIST
5201 wp = find_tabwin(&argvars[0], &argvars[1]);
5202 if (wp == NULL)
5203 return;
5204
5205 l = list_alloc();
5206 if (l == NULL)
5207 return;
5208
5209 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5210 return;
5211 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5212
Bram Moolenaar48679742018-02-13 13:33:29 +01005213 cleanup_jumplist(wp, TRUE);
5214
Bram Moolenaar4f505882018-02-10 21:06:32 +01005215 for (i = 0; i < wp->w_jumplistlen; ++i)
5216 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005217 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5218 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005219 if ((d = dict_alloc()) == NULL)
5220 return;
5221 if (list_append_dict(l, d) == FAIL)
5222 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005223 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5224 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005225# ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +02005226 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005227# endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005228 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005229 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005230 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005231 }
5232#endif
5233}
5234
5235/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005236 * "getline(lnum, [end])" function
5237 */
5238 static void
5239f_getline(typval_T *argvars, typval_T *rettv)
5240{
5241 linenr_T lnum;
5242 linenr_T end;
5243 int retlist;
5244
5245 lnum = get_tv_lnum(argvars);
5246 if (argvars[1].v_type == VAR_UNKNOWN)
5247 {
5248 end = 0;
5249 retlist = FALSE;
5250 }
5251 else
5252 {
5253 end = get_tv_lnum(&argvars[1]);
5254 retlist = TRUE;
5255 }
5256
5257 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5258}
5259
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005260#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005261 static void
5262get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5263{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005264 if (what_arg->v_type == VAR_UNKNOWN)
5265 {
5266 if (rettv_list_alloc(rettv) == OK)
5267 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005268 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005269 }
5270 else
5271 {
5272 if (rettv_dict_alloc(rettv) == OK)
5273 if (is_qf || (wp != NULL))
5274 {
5275 if (what_arg->v_type == VAR_DICT)
5276 {
5277 dict_T *d = what_arg->vval.v_dict;
5278
5279 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005280 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005281 }
5282 else
5283 EMSG(_(e_dictreq));
5284 }
5285 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005286}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005287#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005288
5289/*
5290 * "getloclist()" function
5291 */
5292 static void
5293f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5294{
5295#ifdef FEAT_QUICKFIX
5296 win_T *wp;
5297
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005298 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005299 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5300#endif
5301}
5302
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005303/*
5304 * "getmatches()" function
5305 */
5306 static void
5307f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5308{
5309#ifdef FEAT_SEARCH_EXTRA
5310 dict_T *dict;
5311 matchitem_T *cur = curwin->w_match_head;
5312 int i;
5313
5314 if (rettv_list_alloc(rettv) == OK)
5315 {
5316 while (cur != NULL)
5317 {
5318 dict = dict_alloc();
5319 if (dict == NULL)
5320 return;
5321 if (cur->match.regprog == NULL)
5322 {
5323 /* match added with matchaddpos() */
5324 for (i = 0; i < MAXPOSMATCH; ++i)
5325 {
5326 llpos_T *llpos;
5327 char buf[6];
5328 list_T *l;
5329
5330 llpos = &cur->pos.pos[i];
5331 if (llpos->lnum == 0)
5332 break;
5333 l = list_alloc();
5334 if (l == NULL)
5335 break;
5336 list_append_number(l, (varnumber_T)llpos->lnum);
5337 if (llpos->col > 0)
5338 {
5339 list_append_number(l, (varnumber_T)llpos->col);
5340 list_append_number(l, (varnumber_T)llpos->len);
5341 }
5342 sprintf(buf, "pos%d", i + 1);
5343 dict_add_list(dict, buf, l);
5344 }
5345 }
5346 else
5347 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02005348 dict_add_string(dict, "pattern", cur->pattern);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005349 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02005350 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
5351 dict_add_number(dict, "priority", (long)cur->priority);
5352 dict_add_number(dict, "id", (long)cur->id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005353# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
5354 if (cur->conceal_char)
5355 {
5356 char_u buf[MB_MAXBYTES + 1];
5357
5358 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005359 dict_add_string(dict, "conceal", (char_u *)&buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005360 }
5361# endif
5362 list_append_dict(rettv->vval.v_list, dict);
5363 cur = cur->next;
5364 }
5365 }
5366#endif
5367}
5368
5369/*
5370 * "getpid()" function
5371 */
5372 static void
5373f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5374{
5375 rettv->vval.v_number = mch_get_pid();
5376}
5377
5378 static void
5379getpos_both(
5380 typval_T *argvars,
5381 typval_T *rettv,
5382 int getcurpos)
5383{
5384 pos_T *fp;
5385 list_T *l;
5386 int fnum = -1;
5387
5388 if (rettv_list_alloc(rettv) == OK)
5389 {
5390 l = rettv->vval.v_list;
5391 if (getcurpos)
5392 fp = &curwin->w_cursor;
5393 else
5394 fp = var2fpos(&argvars[0], TRUE, &fnum);
5395 if (fnum != -1)
5396 list_append_number(l, (varnumber_T)fnum);
5397 else
5398 list_append_number(l, (varnumber_T)0);
5399 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5400 : (varnumber_T)0);
5401 list_append_number(l, (fp != NULL)
5402 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5403 : (varnumber_T)0);
5404 list_append_number(l,
5405#ifdef FEAT_VIRTUALEDIT
5406 (fp != NULL) ? (varnumber_T)fp->coladd :
5407#endif
5408 (varnumber_T)0);
5409 if (getcurpos)
5410 {
5411 update_curswant();
5412 list_append_number(l, curwin->w_curswant == MAXCOL ?
5413 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
5414 }
5415 }
5416 else
5417 rettv->vval.v_number = FALSE;
5418}
5419
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005420/*
5421 * "getcurpos()" function
5422 */
5423 static void
5424f_getcurpos(typval_T *argvars, typval_T *rettv)
5425{
5426 getpos_both(argvars, rettv, TRUE);
5427}
5428
5429/*
5430 * "getpos(string)" function
5431 */
5432 static void
5433f_getpos(typval_T *argvars, typval_T *rettv)
5434{
5435 getpos_both(argvars, rettv, FALSE);
5436}
5437
5438/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005439 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005440 */
5441 static void
5442f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5443{
5444#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005445 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005446#endif
5447}
5448
5449/*
5450 * "getreg()" function
5451 */
5452 static void
5453f_getreg(typval_T *argvars, typval_T *rettv)
5454{
5455 char_u *strregname;
5456 int regname;
5457 int arg2 = FALSE;
5458 int return_list = FALSE;
5459 int error = FALSE;
5460
5461 if (argvars[0].v_type != VAR_UNKNOWN)
5462 {
5463 strregname = get_tv_string_chk(&argvars[0]);
5464 error = strregname == NULL;
5465 if (argvars[1].v_type != VAR_UNKNOWN)
5466 {
5467 arg2 = (int)get_tv_number_chk(&argvars[1], &error);
5468 if (!error && argvars[2].v_type != VAR_UNKNOWN)
5469 return_list = (int)get_tv_number_chk(&argvars[2], &error);
5470 }
5471 }
5472 else
5473 strregname = get_vim_var_str(VV_REG);
5474
5475 if (error)
5476 return;
5477
5478 regname = (strregname == NULL ? '"' : *strregname);
5479 if (regname == 0)
5480 regname = '"';
5481
5482 if (return_list)
5483 {
5484 rettv->v_type = VAR_LIST;
5485 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5486 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5487 if (rettv->vval.v_list == NULL)
5488 (void)rettv_list_alloc(rettv);
5489 else
5490 ++rettv->vval.v_list->lv_refcount;
5491 }
5492 else
5493 {
5494 rettv->v_type = VAR_STRING;
5495 rettv->vval.v_string = get_reg_contents(regname,
5496 arg2 ? GREG_EXPR_SRC : 0);
5497 }
5498}
5499
5500/*
5501 * "getregtype()" function
5502 */
5503 static void
5504f_getregtype(typval_T *argvars, typval_T *rettv)
5505{
5506 char_u *strregname;
5507 int regname;
5508 char_u buf[NUMBUFLEN + 2];
5509 long reglen = 0;
5510
5511 if (argvars[0].v_type != VAR_UNKNOWN)
5512 {
5513 strregname = get_tv_string_chk(&argvars[0]);
5514 if (strregname == NULL) /* type error; errmsg already given */
5515 {
5516 rettv->v_type = VAR_STRING;
5517 rettv->vval.v_string = NULL;
5518 return;
5519 }
5520 }
5521 else
5522 /* Default to v:register */
5523 strregname = get_vim_var_str(VV_REG);
5524
5525 regname = (strregname == NULL ? '"' : *strregname);
5526 if (regname == 0)
5527 regname = '"';
5528
5529 buf[0] = NUL;
5530 buf[1] = NUL;
5531 switch (get_reg_type(regname, &reglen))
5532 {
5533 case MLINE: buf[0] = 'V'; break;
5534 case MCHAR: buf[0] = 'v'; break;
5535 case MBLOCK:
5536 buf[0] = Ctrl_V;
5537 sprintf((char *)buf + 1, "%ld", reglen + 1);
5538 break;
5539 }
5540 rettv->v_type = VAR_STRING;
5541 rettv->vval.v_string = vim_strsave(buf);
5542}
5543
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005544/*
5545 * Returns information (variables, options, etc.) about a tab page
5546 * as a dictionary.
5547 */
5548 static dict_T *
5549get_tabpage_info(tabpage_T *tp, int tp_idx)
5550{
5551 win_T *wp;
5552 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005553 list_T *l;
5554
5555 dict = dict_alloc();
5556 if (dict == NULL)
5557 return NULL;
5558
Bram Moolenaare0be1672018-07-08 16:50:37 +02005559 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005560
5561 l = list_alloc();
5562 if (l != NULL)
5563 {
5564 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5565 wp; wp = wp->w_next)
5566 list_append_number(l, (varnumber_T)wp->w_id);
5567 dict_add_list(dict, "windows", l);
5568 }
5569
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005570 /* Make a reference to tabpage variables */
5571 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005572
5573 return dict;
5574}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005575
5576/*
5577 * "gettabinfo()" function
5578 */
5579 static void
5580f_gettabinfo(typval_T *argvars, typval_T *rettv)
5581{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005582 tabpage_T *tp, *tparg = NULL;
5583 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005584 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005585
5586 if (rettv_list_alloc(rettv) != OK)
5587 return;
5588
5589 if (argvars[0].v_type != VAR_UNKNOWN)
5590 {
5591 /* Information about one tab page */
5592 tparg = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5593 if (tparg == NULL)
5594 return;
5595 }
5596
5597 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005598 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005599 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005600 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005601 if (tparg != NULL && tp != tparg)
5602 continue;
5603 d = get_tabpage_info(tp, tpnr);
5604 if (d != NULL)
5605 list_append_dict(rettv->vval.v_list, d);
5606 if (tparg != NULL)
5607 return;
5608 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005609}
5610
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005611/*
5612 * "gettabvar()" function
5613 */
5614 static void
5615f_gettabvar(typval_T *argvars, typval_T *rettv)
5616{
5617 win_T *oldcurwin;
5618 tabpage_T *tp, *oldtabpage;
5619 dictitem_T *v;
5620 char_u *varname;
5621 int done = FALSE;
5622
5623 rettv->v_type = VAR_STRING;
5624 rettv->vval.v_string = NULL;
5625
5626 varname = get_tv_string_chk(&argvars[1]);
5627 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5628 if (tp != NULL && varname != NULL)
5629 {
5630 /* Set tp to be our tabpage, temporarily. Also set the window to the
5631 * first window in the tabpage, otherwise the window is not valid. */
5632 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005633 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5634 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005635 {
5636 /* look up the variable */
5637 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5638 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5639 if (v != NULL)
5640 {
5641 copy_tv(&v->di_tv, rettv);
5642 done = TRUE;
5643 }
5644 }
5645
5646 /* restore previous notion of curwin */
5647 restore_win(oldcurwin, oldtabpage, TRUE);
5648 }
5649
5650 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5651 copy_tv(&argvars[2], rettv);
5652}
5653
5654/*
5655 * "gettabwinvar()" function
5656 */
5657 static void
5658f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5659{
5660 getwinvar(argvars, rettv, 1);
5661}
5662
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005663/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01005664 * "gettagstack()" function
5665 */
5666 static void
5667f_gettagstack(typval_T *argvars, typval_T *rettv)
5668{
5669 win_T *wp = curwin; // default is current window
5670
5671 if (rettv_dict_alloc(rettv) != OK)
5672 return;
5673
5674 if (argvars[0].v_type != VAR_UNKNOWN)
5675 {
5676 wp = find_win_by_nr_or_id(&argvars[0]);
5677 if (wp == NULL)
5678 return;
5679 }
5680
5681 get_tagstack(wp, rettv->vval.v_dict);
5682}
5683
5684/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005685 * Returns information about a window as a dictionary.
5686 */
5687 static dict_T *
5688get_win_info(win_T *wp, short tpnr, short winnr)
5689{
5690 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005691
5692 dict = dict_alloc();
5693 if (dict == NULL)
5694 return NULL;
5695
Bram Moolenaare0be1672018-07-08 16:50:37 +02005696 dict_add_number(dict, "tabnr", tpnr);
5697 dict_add_number(dict, "winnr", winnr);
5698 dict_add_number(dict, "winid", wp->w_id);
5699 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005700 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005701#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02005702 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005703#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005704 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005705 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005706 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005707
Bram Moolenaar69905d12017-08-13 18:14:47 +02005708#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02005709 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02005710#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005711#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02005712 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
5713 dict_add_number(dict, "loclist",
5714 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02005715#endif
5716
Bram Moolenaar30567352016-08-27 21:25:44 +02005717 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005718 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005719
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005720 return dict;
5721}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005722
5723/*
5724 * "getwininfo()" function
5725 */
5726 static void
5727f_getwininfo(typval_T *argvars, typval_T *rettv)
5728{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005729 tabpage_T *tp;
5730 win_T *wp = NULL, *wparg = NULL;
5731 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005732 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005733
5734 if (rettv_list_alloc(rettv) != OK)
5735 return;
5736
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005737 if (argvars[0].v_type != VAR_UNKNOWN)
5738 {
5739 wparg = win_id2wp(argvars);
5740 if (wparg == NULL)
5741 return;
5742 }
5743
5744 /* Collect information about either all the windows across all the tab
5745 * pages or one particular window.
5746 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005747 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005748 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005749 tabnr++;
5750 winnr = 0;
5751 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005752 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005753 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005754 if (wparg != NULL && wp != wparg)
5755 continue;
5756 d = get_win_info(wp, tabnr, winnr);
5757 if (d != NULL)
5758 list_append_dict(rettv->vval.v_list, d);
5759 if (wparg != NULL)
5760 /* found information about a specific window */
5761 return;
5762 }
5763 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005764}
5765
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005766/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005767 * "win_findbuf()" function
5768 */
5769 static void
5770f_win_findbuf(typval_T *argvars, typval_T *rettv)
5771{
5772 if (rettv_list_alloc(rettv) != FAIL)
5773 win_findbuf(argvars, rettv->vval.v_list);
5774}
5775
5776/*
5777 * "win_getid()" function
5778 */
5779 static void
5780f_win_getid(typval_T *argvars, typval_T *rettv)
5781{
5782 rettv->vval.v_number = win_getid(argvars);
5783}
5784
5785/*
5786 * "win_gotoid()" function
5787 */
5788 static void
5789f_win_gotoid(typval_T *argvars, typval_T *rettv)
5790{
5791 rettv->vval.v_number = win_gotoid(argvars);
5792}
5793
5794/*
5795 * "win_id2tabwin()" function
5796 */
5797 static void
5798f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5799{
5800 if (rettv_list_alloc(rettv) != FAIL)
5801 win_id2tabwin(argvars, rettv->vval.v_list);
5802}
5803
5804/*
5805 * "win_id2win()" function
5806 */
5807 static void
5808f_win_id2win(typval_T *argvars, typval_T *rettv)
5809{
5810 rettv->vval.v_number = win_id2win(argvars);
5811}
5812
5813/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005814 * "win_screenpos()" function
5815 */
5816 static void
5817f_win_screenpos(typval_T *argvars, typval_T *rettv)
5818{
5819 win_T *wp;
5820
5821 if (rettv_list_alloc(rettv) == FAIL)
5822 return;
5823
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005824 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005825 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5826 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5827}
5828
5829/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005830 * "getwinpos({timeout})" function
5831 */
5832 static void
5833f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
5834{
5835 int x = -1;
5836 int y = -1;
5837
5838 if (rettv_list_alloc(rettv) == FAIL)
5839 return;
5840#ifdef FEAT_GUI
5841 if (gui.in_use)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01005842 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005843# if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5844 else
5845# endif
5846#endif
5847#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5848 {
5849 varnumber_T timeout = 100;
5850
5851 if (argvars[0].v_type != VAR_UNKNOWN)
5852 timeout = get_tv_number(&argvars[0]);
5853 term_get_winpos(&x, &y, timeout);
5854 }
5855#endif
5856 list_append_number(rettv->vval.v_list, (varnumber_T)x);
5857 list_append_number(rettv->vval.v_list, (varnumber_T)y);
5858}
5859
5860
5861/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005862 * "getwinposx()" function
5863 */
5864 static void
5865f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5866{
5867 rettv->vval.v_number = -1;
5868#ifdef FEAT_GUI
5869 if (gui.in_use)
5870 {
5871 int x, y;
5872
5873 if (gui_mch_get_winpos(&x, &y) == OK)
5874 rettv->vval.v_number = x;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005875 return;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005876 }
5877#endif
5878#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5879 {
5880 int x, y;
5881
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005882 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005883 rettv->vval.v_number = x;
5884 }
5885#endif
5886}
5887
5888/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005889 * "getwinposy()" function
5890 */
5891 static void
5892f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5893{
5894 rettv->vval.v_number = -1;
5895#ifdef FEAT_GUI
5896 if (gui.in_use)
5897 {
5898 int x, y;
5899
5900 if (gui_mch_get_winpos(&x, &y) == OK)
5901 rettv->vval.v_number = y;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005902 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005903 }
5904#endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005905#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5906 {
5907 int x, y;
5908
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005909 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005910 rettv->vval.v_number = y;
5911 }
5912#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005913}
5914
5915/*
5916 * "getwinvar()" function
5917 */
5918 static void
5919f_getwinvar(typval_T *argvars, typval_T *rettv)
5920{
5921 getwinvar(argvars, rettv, 0);
5922}
5923
5924/*
5925 * "glob()" function
5926 */
5927 static void
5928f_glob(typval_T *argvars, typval_T *rettv)
5929{
5930 int options = WILD_SILENT|WILD_USE_NL;
5931 expand_T xpc;
5932 int error = FALSE;
5933
5934 /* When the optional second argument is non-zero, don't remove matches
5935 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5936 rettv->v_type = VAR_STRING;
5937 if (argvars[1].v_type != VAR_UNKNOWN)
5938 {
5939 if (get_tv_number_chk(&argvars[1], &error))
5940 options |= WILD_KEEP_ALL;
5941 if (argvars[2].v_type != VAR_UNKNOWN)
5942 {
5943 if (get_tv_number_chk(&argvars[2], &error))
5944 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005945 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005946 }
5947 if (argvars[3].v_type != VAR_UNKNOWN
5948 && get_tv_number_chk(&argvars[3], &error))
5949 options |= WILD_ALLLINKS;
5950 }
5951 }
5952 if (!error)
5953 {
5954 ExpandInit(&xpc);
5955 xpc.xp_context = EXPAND_FILES;
5956 if (p_wic)
5957 options += WILD_ICASE;
5958 if (rettv->v_type == VAR_STRING)
5959 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
5960 NULL, options, WILD_ALL);
5961 else if (rettv_list_alloc(rettv) != FAIL)
5962 {
5963 int i;
5964
5965 ExpandOne(&xpc, get_tv_string(&argvars[0]),
5966 NULL, options, WILD_ALL_KEEP);
5967 for (i = 0; i < xpc.xp_numfiles; i++)
5968 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5969
5970 ExpandCleanup(&xpc);
5971 }
5972 }
5973 else
5974 rettv->vval.v_string = NULL;
5975}
5976
5977/*
5978 * "globpath()" function
5979 */
5980 static void
5981f_globpath(typval_T *argvars, typval_T *rettv)
5982{
5983 int flags = 0;
5984 char_u buf1[NUMBUFLEN];
5985 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
5986 int error = FALSE;
5987 garray_T ga;
5988 int i;
5989
5990 /* When the optional second argument is non-zero, don't remove matches
5991 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5992 rettv->v_type = VAR_STRING;
5993 if (argvars[2].v_type != VAR_UNKNOWN)
5994 {
5995 if (get_tv_number_chk(&argvars[2], &error))
5996 flags |= WILD_KEEP_ALL;
5997 if (argvars[3].v_type != VAR_UNKNOWN)
5998 {
5999 if (get_tv_number_chk(&argvars[3], &error))
6000 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006001 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006002 }
6003 if (argvars[4].v_type != VAR_UNKNOWN
6004 && get_tv_number_chk(&argvars[4], &error))
6005 flags |= WILD_ALLLINKS;
6006 }
6007 }
6008 if (file != NULL && !error)
6009 {
6010 ga_init2(&ga, (int)sizeof(char_u *), 10);
6011 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
6012 if (rettv->v_type == VAR_STRING)
6013 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
6014 else if (rettv_list_alloc(rettv) != FAIL)
6015 for (i = 0; i < ga.ga_len; ++i)
6016 list_append_string(rettv->vval.v_list,
6017 ((char_u **)(ga.ga_data))[i], -1);
6018 ga_clear_strings(&ga);
6019 }
6020 else
6021 rettv->vval.v_string = NULL;
6022}
6023
6024/*
6025 * "glob2regpat()" function
6026 */
6027 static void
6028f_glob2regpat(typval_T *argvars, typval_T *rettv)
6029{
6030 char_u *pat = get_tv_string_chk(&argvars[0]);
6031
6032 rettv->v_type = VAR_STRING;
6033 rettv->vval.v_string = (pat == NULL)
6034 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
6035}
6036
6037/* for VIM_VERSION_ defines */
6038#include "version.h"
6039
6040/*
6041 * "has()" function
6042 */
6043 static void
6044f_has(typval_T *argvars, typval_T *rettv)
6045{
6046 int i;
6047 char_u *name;
6048 int n = FALSE;
6049 static char *(has_list[]) =
6050 {
6051#ifdef AMIGA
6052 "amiga",
6053# ifdef FEAT_ARP
6054 "arp",
6055# endif
6056#endif
6057#ifdef __BEOS__
6058 "beos",
6059#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02006060#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01006061 "mac", /* Mac OS X (and, once, Mac OS Classic) */
6062 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02006063# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01006064 "macunix", /* Mac OS X, with the darwin feature */
6065 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02006066# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006067#endif
6068#ifdef __QNX__
6069 "qnx",
6070#endif
6071#ifdef UNIX
6072 "unix",
6073#endif
6074#ifdef VMS
6075 "vms",
6076#endif
6077#ifdef WIN32
6078 "win32",
6079#endif
6080#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
6081 "win32unix",
6082#endif
6083#if defined(WIN64) || defined(_WIN64)
6084 "win64",
6085#endif
6086#ifdef EBCDIC
6087 "ebcdic",
6088#endif
6089#ifndef CASE_INSENSITIVE_FILENAME
6090 "fname_case",
6091#endif
6092#ifdef HAVE_ACL
6093 "acl",
6094#endif
6095#ifdef FEAT_ARABIC
6096 "arabic",
6097#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006098 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006099#ifdef FEAT_AUTOCHDIR
6100 "autochdir",
6101#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006102#ifdef FEAT_AUTOSERVERNAME
6103 "autoservername",
6104#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006105#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006106 "balloon_eval",
6107# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
6108 "balloon_multiline",
6109# endif
6110#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006111#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006112 "balloon_eval_term",
6113#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006114#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6115 "builtin_terms",
6116# ifdef ALL_BUILTIN_TCAPS
6117 "all_builtin_terms",
6118# endif
6119#endif
6120#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
6121 || defined(FEAT_GUI_W32) \
6122 || defined(FEAT_GUI_MOTIF))
6123 "browsefilter",
6124#endif
6125#ifdef FEAT_BYTEOFF
6126 "byte_offset",
6127#endif
6128#ifdef FEAT_JOB_CHANNEL
6129 "channel",
6130#endif
6131#ifdef FEAT_CINDENT
6132 "cindent",
6133#endif
6134#ifdef FEAT_CLIENTSERVER
6135 "clientserver",
6136#endif
6137#ifdef FEAT_CLIPBOARD
6138 "clipboard",
6139#endif
6140#ifdef FEAT_CMDL_COMPL
6141 "cmdline_compl",
6142#endif
6143#ifdef FEAT_CMDHIST
6144 "cmdline_hist",
6145#endif
6146#ifdef FEAT_COMMENTS
6147 "comments",
6148#endif
6149#ifdef FEAT_CONCEAL
6150 "conceal",
6151#endif
6152#ifdef FEAT_CRYPT
6153 "cryptv",
6154 "crypt-blowfish",
6155 "crypt-blowfish2",
6156#endif
6157#ifdef FEAT_CSCOPE
6158 "cscope",
6159#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006160 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006161#ifdef CURSOR_SHAPE
6162 "cursorshape",
6163#endif
6164#ifdef DEBUG
6165 "debug",
6166#endif
6167#ifdef FEAT_CON_DIALOG
6168 "dialog_con",
6169#endif
6170#ifdef FEAT_GUI_DIALOG
6171 "dialog_gui",
6172#endif
6173#ifdef FEAT_DIFF
6174 "diff",
6175#endif
6176#ifdef FEAT_DIGRAPHS
6177 "digraphs",
6178#endif
6179#ifdef FEAT_DIRECTX
6180 "directx",
6181#endif
6182#ifdef FEAT_DND
6183 "dnd",
6184#endif
6185#ifdef FEAT_EMACS_TAGS
6186 "emacs_tags",
6187#endif
6188 "eval", /* always present, of course! */
6189 "ex_extra", /* graduated feature */
6190#ifdef FEAT_SEARCH_EXTRA
6191 "extra_search",
6192#endif
6193#ifdef FEAT_FKMAP
6194 "farsi",
6195#endif
6196#ifdef FEAT_SEARCHPATH
6197 "file_in_path",
6198#endif
6199#ifdef FEAT_FILTERPIPE
6200 "filterpipe",
6201#endif
6202#ifdef FEAT_FIND_ID
6203 "find_in_path",
6204#endif
6205#ifdef FEAT_FLOAT
6206 "float",
6207#endif
6208#ifdef FEAT_FOLDING
6209 "folding",
6210#endif
6211#ifdef FEAT_FOOTER
6212 "footer",
6213#endif
6214#if !defined(USE_SYSTEM) && defined(UNIX)
6215 "fork",
6216#endif
6217#ifdef FEAT_GETTEXT
6218 "gettext",
6219#endif
6220#ifdef FEAT_GUI
6221 "gui",
6222#endif
6223#ifdef FEAT_GUI_ATHENA
6224# ifdef FEAT_GUI_NEXTAW
6225 "gui_neXtaw",
6226# else
6227 "gui_athena",
6228# endif
6229#endif
6230#ifdef FEAT_GUI_GTK
6231 "gui_gtk",
6232# ifdef USE_GTK3
6233 "gui_gtk3",
6234# else
6235 "gui_gtk2",
6236# endif
6237#endif
6238#ifdef FEAT_GUI_GNOME
6239 "gui_gnome",
6240#endif
6241#ifdef FEAT_GUI_MAC
6242 "gui_mac",
6243#endif
6244#ifdef FEAT_GUI_MOTIF
6245 "gui_motif",
6246#endif
6247#ifdef FEAT_GUI_PHOTON
6248 "gui_photon",
6249#endif
6250#ifdef FEAT_GUI_W32
6251 "gui_win32",
6252#endif
6253#ifdef FEAT_HANGULIN
6254 "hangul_input",
6255#endif
6256#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6257 "iconv",
6258#endif
6259#ifdef FEAT_INS_EXPAND
6260 "insert_expand",
6261#endif
6262#ifdef FEAT_JOB_CHANNEL
6263 "job",
6264#endif
6265#ifdef FEAT_JUMPLIST
6266 "jumplist",
6267#endif
6268#ifdef FEAT_KEYMAP
6269 "keymap",
6270#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006271 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006272#ifdef FEAT_LANGMAP
6273 "langmap",
6274#endif
6275#ifdef FEAT_LIBCALL
6276 "libcall",
6277#endif
6278#ifdef FEAT_LINEBREAK
6279 "linebreak",
6280#endif
6281#ifdef FEAT_LISP
6282 "lispindent",
6283#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006284 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006285#ifdef FEAT_LOCALMAP
6286 "localmap",
6287#endif
6288#ifdef FEAT_LUA
6289# ifndef DYNAMIC_LUA
6290 "lua",
6291# endif
6292#endif
6293#ifdef FEAT_MENU
6294 "menu",
6295#endif
6296#ifdef FEAT_SESSION
6297 "mksession",
6298#endif
6299#ifdef FEAT_MODIFY_FNAME
6300 "modify_fname",
6301#endif
6302#ifdef FEAT_MOUSE
6303 "mouse",
6304#endif
6305#ifdef FEAT_MOUSESHAPE
6306 "mouseshape",
6307#endif
6308#if defined(UNIX) || defined(VMS)
6309# ifdef FEAT_MOUSE_DEC
6310 "mouse_dec",
6311# endif
6312# ifdef FEAT_MOUSE_GPM
6313 "mouse_gpm",
6314# endif
6315# ifdef FEAT_MOUSE_JSB
6316 "mouse_jsbterm",
6317# endif
6318# ifdef FEAT_MOUSE_NET
6319 "mouse_netterm",
6320# endif
6321# ifdef FEAT_MOUSE_PTERM
6322 "mouse_pterm",
6323# endif
6324# ifdef FEAT_MOUSE_SGR
6325 "mouse_sgr",
6326# endif
6327# ifdef FEAT_SYSMOUSE
6328 "mouse_sysmouse",
6329# endif
6330# ifdef FEAT_MOUSE_URXVT
6331 "mouse_urxvt",
6332# endif
6333# ifdef FEAT_MOUSE_XTERM
6334 "mouse_xterm",
6335# endif
6336#endif
6337#ifdef FEAT_MBYTE
6338 "multi_byte",
6339#endif
6340#ifdef FEAT_MBYTE_IME
6341 "multi_byte_ime",
6342#endif
6343#ifdef FEAT_MULTI_LANG
6344 "multi_lang",
6345#endif
6346#ifdef FEAT_MZSCHEME
6347#ifndef DYNAMIC_MZSCHEME
6348 "mzscheme",
6349#endif
6350#endif
6351#ifdef FEAT_NUM64
6352 "num64",
6353#endif
6354#ifdef FEAT_OLE
6355 "ole",
6356#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006357#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006358 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006359#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006360#ifdef FEAT_PATH_EXTRA
6361 "path_extra",
6362#endif
6363#ifdef FEAT_PERL
6364#ifndef DYNAMIC_PERL
6365 "perl",
6366#endif
6367#endif
6368#ifdef FEAT_PERSISTENT_UNDO
6369 "persistent_undo",
6370#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006371#if defined(FEAT_PYTHON)
6372 "python_compiled",
6373# if defined(DYNAMIC_PYTHON)
6374 "python_dynamic",
6375# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006376 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006377 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006378# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006379#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006380#if defined(FEAT_PYTHON3)
6381 "python3_compiled",
6382# if defined(DYNAMIC_PYTHON3)
6383 "python3_dynamic",
6384# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006385 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006386 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006387# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006388#endif
6389#ifdef FEAT_POSTSCRIPT
6390 "postscript",
6391#endif
6392#ifdef FEAT_PRINTER
6393 "printer",
6394#endif
6395#ifdef FEAT_PROFILE
6396 "profile",
6397#endif
6398#ifdef FEAT_RELTIME
6399 "reltime",
6400#endif
6401#ifdef FEAT_QUICKFIX
6402 "quickfix",
6403#endif
6404#ifdef FEAT_RIGHTLEFT
6405 "rightleft",
6406#endif
6407#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6408 "ruby",
6409#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006410 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006411#ifdef FEAT_CMDL_INFO
6412 "showcmd",
6413 "cmdline_info",
6414#endif
6415#ifdef FEAT_SIGNS
6416 "signs",
6417#endif
6418#ifdef FEAT_SMARTINDENT
6419 "smartindent",
6420#endif
6421#ifdef STARTUPTIME
6422 "startuptime",
6423#endif
6424#ifdef FEAT_STL_OPT
6425 "statusline",
6426#endif
6427#ifdef FEAT_SUN_WORKSHOP
6428 "sun_workshop",
6429#endif
6430#ifdef FEAT_NETBEANS_INTG
6431 "netbeans_intg",
6432#endif
6433#ifdef FEAT_SPELL
6434 "spell",
6435#endif
6436#ifdef FEAT_SYN_HL
6437 "syntax",
6438#endif
6439#if defined(USE_SYSTEM) || !defined(UNIX)
6440 "system",
6441#endif
6442#ifdef FEAT_TAG_BINS
6443 "tag_binary",
6444#endif
6445#ifdef FEAT_TAG_OLDSTATIC
6446 "tag_old_static",
6447#endif
6448#ifdef FEAT_TAG_ANYWHITE
6449 "tag_any_white",
6450#endif
6451#ifdef FEAT_TCL
6452# ifndef DYNAMIC_TCL
6453 "tcl",
6454# endif
6455#endif
6456#ifdef FEAT_TERMGUICOLORS
6457 "termguicolors",
6458#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006459#if defined(FEAT_TERMINAL) && !defined(WIN3264)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006460 "terminal",
6461#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006462#ifdef TERMINFO
6463 "terminfo",
6464#endif
6465#ifdef FEAT_TERMRESPONSE
6466 "termresponse",
6467#endif
6468#ifdef FEAT_TEXTOBJ
6469 "textobjects",
6470#endif
6471#ifdef HAVE_TGETENT
6472 "tgetent",
6473#endif
6474#ifdef FEAT_TIMERS
6475 "timers",
6476#endif
6477#ifdef FEAT_TITLE
6478 "title",
6479#endif
6480#ifdef FEAT_TOOLBAR
6481 "toolbar",
6482#endif
6483#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6484 "unnamedplus",
6485#endif
6486#ifdef FEAT_USR_CMDS
6487 "user-commands", /* was accidentally included in 5.4 */
6488 "user_commands",
6489#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006490#ifdef FEAT_VARTABS
6491 "vartabs",
6492#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006493#ifdef FEAT_VIMINFO
6494 "viminfo",
6495#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006496 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006497#ifdef FEAT_VIRTUALEDIT
6498 "virtualedit",
6499#endif
6500 "visual",
6501#ifdef FEAT_VISUALEXTRA
6502 "visualextra",
6503#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006504 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006505#ifdef FEAT_VTP
6506 "vtp",
6507#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006508#ifdef FEAT_WILDIGN
6509 "wildignore",
6510#endif
6511#ifdef FEAT_WILDMENU
6512 "wildmenu",
6513#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006514 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006515#ifdef FEAT_WAK
6516 "winaltkeys",
6517#endif
6518#ifdef FEAT_WRITEBACKUP
6519 "writebackup",
6520#endif
6521#ifdef FEAT_XIM
6522 "xim",
6523#endif
6524#ifdef FEAT_XFONTSET
6525 "xfontset",
6526#endif
6527#ifdef FEAT_XPM_W32
6528 "xpm",
6529 "xpm_w32", /* for backward compatibility */
6530#else
6531# if defined(HAVE_XPM)
6532 "xpm",
6533# endif
6534#endif
6535#ifdef USE_XSMP
6536 "xsmp",
6537#endif
6538#ifdef USE_XSMP_INTERACT
6539 "xsmp_interact",
6540#endif
6541#ifdef FEAT_XCLIPBOARD
6542 "xterm_clipboard",
6543#endif
6544#ifdef FEAT_XTERM_SAVE
6545 "xterm_save",
6546#endif
6547#if defined(UNIX) && defined(FEAT_X11)
6548 "X11",
6549#endif
6550 NULL
6551 };
6552
6553 name = get_tv_string(&argvars[0]);
6554 for (i = 0; has_list[i] != NULL; ++i)
6555 if (STRICMP(name, has_list[i]) == 0)
6556 {
6557 n = TRUE;
6558 break;
6559 }
6560
6561 if (n == FALSE)
6562 {
6563 if (STRNICMP(name, "patch", 5) == 0)
6564 {
6565 if (name[5] == '-'
6566 && STRLEN(name) >= 11
6567 && vim_isdigit(name[6])
6568 && vim_isdigit(name[8])
6569 && vim_isdigit(name[10]))
6570 {
6571 int major = atoi((char *)name + 6);
6572 int minor = atoi((char *)name + 8);
6573
6574 /* Expect "patch-9.9.01234". */
6575 n = (major < VIM_VERSION_MAJOR
6576 || (major == VIM_VERSION_MAJOR
6577 && (minor < VIM_VERSION_MINOR
6578 || (minor == VIM_VERSION_MINOR
6579 && has_patch(atoi((char *)name + 10))))));
6580 }
6581 else
6582 n = has_patch(atoi((char *)name + 5));
6583 }
6584 else if (STRICMP(name, "vim_starting") == 0)
6585 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006586 else if (STRICMP(name, "ttyin") == 0)
6587 n = mch_input_isatty();
6588 else if (STRICMP(name, "ttyout") == 0)
6589 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006590#ifdef FEAT_MBYTE
6591 else if (STRICMP(name, "multi_byte_encoding") == 0)
6592 n = has_mbyte;
6593#endif
6594#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
6595 else if (STRICMP(name, "balloon_multiline") == 0)
6596 n = multiline_balloon_available();
6597#endif
6598#ifdef DYNAMIC_TCL
6599 else if (STRICMP(name, "tcl") == 0)
6600 n = tcl_enabled(FALSE);
6601#endif
6602#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6603 else if (STRICMP(name, "iconv") == 0)
6604 n = iconv_enabled(FALSE);
6605#endif
6606#ifdef DYNAMIC_LUA
6607 else if (STRICMP(name, "lua") == 0)
6608 n = lua_enabled(FALSE);
6609#endif
6610#ifdef DYNAMIC_MZSCHEME
6611 else if (STRICMP(name, "mzscheme") == 0)
6612 n = mzscheme_enabled(FALSE);
6613#endif
6614#ifdef DYNAMIC_RUBY
6615 else if (STRICMP(name, "ruby") == 0)
6616 n = ruby_enabled(FALSE);
6617#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006618#ifdef DYNAMIC_PYTHON
6619 else if (STRICMP(name, "python") == 0)
6620 n = python_enabled(FALSE);
6621#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006622#ifdef DYNAMIC_PYTHON3
6623 else if (STRICMP(name, "python3") == 0)
6624 n = python3_enabled(FALSE);
6625#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006626#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6627 else if (STRICMP(name, "pythonx") == 0)
6628 {
6629# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6630 if (p_pyx == 0)
6631 n = python3_enabled(FALSE) || python_enabled(FALSE);
6632 else if (p_pyx == 3)
6633 n = python3_enabled(FALSE);
6634 else if (p_pyx == 2)
6635 n = python_enabled(FALSE);
6636# elif defined(DYNAMIC_PYTHON)
6637 n = python_enabled(FALSE);
6638# elif defined(DYNAMIC_PYTHON3)
6639 n = python3_enabled(FALSE);
6640# endif
6641 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006642#endif
6643#ifdef DYNAMIC_PERL
6644 else if (STRICMP(name, "perl") == 0)
6645 n = perl_enabled(FALSE);
6646#endif
6647#ifdef FEAT_GUI
6648 else if (STRICMP(name, "gui_running") == 0)
6649 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006650# ifdef FEAT_BROWSE
6651 else if (STRICMP(name, "browse") == 0)
6652 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6653# endif
6654#endif
6655#ifdef FEAT_SYN_HL
6656 else if (STRICMP(name, "syntax_items") == 0)
6657 n = syntax_present(curwin);
6658#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006659#ifdef FEAT_VTP
6660 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02006661 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006662#endif
6663#ifdef FEAT_NETBEANS_INTG
6664 else if (STRICMP(name, "netbeans_enabled") == 0)
6665 n = netbeans_active();
6666#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006667#if defined(FEAT_TERMINAL) && defined(WIN3264)
6668 else if (STRICMP(name, "terminal") == 0)
6669 n = terminal_enabled();
6670#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006671 }
6672
6673 rettv->vval.v_number = n;
6674}
6675
6676/*
6677 * "has_key()" function
6678 */
6679 static void
6680f_has_key(typval_T *argvars, typval_T *rettv)
6681{
6682 if (argvars[0].v_type != VAR_DICT)
6683 {
6684 EMSG(_(e_dictreq));
6685 return;
6686 }
6687 if (argvars[0].vval.v_dict == NULL)
6688 return;
6689
6690 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
6691 get_tv_string(&argvars[1]), -1) != NULL;
6692}
6693
6694/*
6695 * "haslocaldir()" function
6696 */
6697 static void
6698f_haslocaldir(typval_T *argvars, typval_T *rettv)
6699{
6700 win_T *wp = NULL;
6701
6702 wp = find_tabwin(&argvars[0], &argvars[1]);
6703 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6704}
6705
6706/*
6707 * "hasmapto()" function
6708 */
6709 static void
6710f_hasmapto(typval_T *argvars, typval_T *rettv)
6711{
6712 char_u *name;
6713 char_u *mode;
6714 char_u buf[NUMBUFLEN];
6715 int abbr = FALSE;
6716
6717 name = get_tv_string(&argvars[0]);
6718 if (argvars[1].v_type == VAR_UNKNOWN)
6719 mode = (char_u *)"nvo";
6720 else
6721 {
6722 mode = get_tv_string_buf(&argvars[1], buf);
6723 if (argvars[2].v_type != VAR_UNKNOWN)
6724 abbr = (int)get_tv_number(&argvars[2]);
6725 }
6726
6727 if (map_to_exists(name, mode, abbr))
6728 rettv->vval.v_number = TRUE;
6729 else
6730 rettv->vval.v_number = FALSE;
6731}
6732
6733/*
6734 * "histadd()" function
6735 */
6736 static void
6737f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6738{
6739#ifdef FEAT_CMDHIST
6740 int histype;
6741 char_u *str;
6742 char_u buf[NUMBUFLEN];
6743#endif
6744
6745 rettv->vval.v_number = FALSE;
6746 if (check_restricted() || check_secure())
6747 return;
6748#ifdef FEAT_CMDHIST
6749 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6750 histype = str != NULL ? get_histtype(str) : -1;
6751 if (histype >= 0)
6752 {
6753 str = get_tv_string_buf(&argvars[1], buf);
6754 if (*str != NUL)
6755 {
6756 init_history();
6757 add_to_history(histype, str, FALSE, NUL);
6758 rettv->vval.v_number = TRUE;
6759 return;
6760 }
6761 }
6762#endif
6763}
6764
6765/*
6766 * "histdel()" function
6767 */
6768 static void
6769f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6770{
6771#ifdef FEAT_CMDHIST
6772 int n;
6773 char_u buf[NUMBUFLEN];
6774 char_u *str;
6775
6776 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6777 if (str == NULL)
6778 n = 0;
6779 else if (argvars[1].v_type == VAR_UNKNOWN)
6780 /* only one argument: clear entire history */
6781 n = clr_history(get_histtype(str));
6782 else if (argvars[1].v_type == VAR_NUMBER)
6783 /* index given: remove that entry */
6784 n = del_history_idx(get_histtype(str),
6785 (int)get_tv_number(&argvars[1]));
6786 else
6787 /* string given: remove all matching entries */
6788 n = del_history_entry(get_histtype(str),
6789 get_tv_string_buf(&argvars[1], buf));
6790 rettv->vval.v_number = n;
6791#endif
6792}
6793
6794/*
6795 * "histget()" function
6796 */
6797 static void
6798f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6799{
6800#ifdef FEAT_CMDHIST
6801 int type;
6802 int idx;
6803 char_u *str;
6804
6805 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6806 if (str == NULL)
6807 rettv->vval.v_string = NULL;
6808 else
6809 {
6810 type = get_histtype(str);
6811 if (argvars[1].v_type == VAR_UNKNOWN)
6812 idx = get_history_idx(type);
6813 else
6814 idx = (int)get_tv_number_chk(&argvars[1], NULL);
6815 /* -1 on type error */
6816 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6817 }
6818#else
6819 rettv->vval.v_string = NULL;
6820#endif
6821 rettv->v_type = VAR_STRING;
6822}
6823
6824/*
6825 * "histnr()" function
6826 */
6827 static void
6828f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6829{
6830 int i;
6831
6832#ifdef FEAT_CMDHIST
6833 char_u *history = get_tv_string_chk(&argvars[0]);
6834
6835 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6836 if (i >= HIST_CMD && i < HIST_COUNT)
6837 i = get_history_idx(i);
6838 else
6839#endif
6840 i = -1;
6841 rettv->vval.v_number = i;
6842}
6843
6844/*
6845 * "highlightID(name)" function
6846 */
6847 static void
6848f_hlID(typval_T *argvars, typval_T *rettv)
6849{
6850 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
6851}
6852
6853/*
6854 * "highlight_exists()" function
6855 */
6856 static void
6857f_hlexists(typval_T *argvars, typval_T *rettv)
6858{
6859 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
6860}
6861
6862/*
6863 * "hostname()" function
6864 */
6865 static void
6866f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6867{
6868 char_u hostname[256];
6869
6870 mch_get_host_name(hostname, 256);
6871 rettv->v_type = VAR_STRING;
6872 rettv->vval.v_string = vim_strsave(hostname);
6873}
6874
6875/*
6876 * iconv() function
6877 */
6878 static void
6879f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6880{
6881#ifdef FEAT_MBYTE
6882 char_u buf1[NUMBUFLEN];
6883 char_u buf2[NUMBUFLEN];
6884 char_u *from, *to, *str;
6885 vimconv_T vimconv;
6886#endif
6887
6888 rettv->v_type = VAR_STRING;
6889 rettv->vval.v_string = NULL;
6890
6891#ifdef FEAT_MBYTE
6892 str = get_tv_string(&argvars[0]);
6893 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
6894 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
6895 vimconv.vc_type = CONV_NONE;
6896 convert_setup(&vimconv, from, to);
6897
6898 /* If the encodings are equal, no conversion needed. */
6899 if (vimconv.vc_type == CONV_NONE)
6900 rettv->vval.v_string = vim_strsave(str);
6901 else
6902 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6903
6904 convert_setup(&vimconv, NULL, NULL);
6905 vim_free(from);
6906 vim_free(to);
6907#endif
6908}
6909
6910/*
6911 * "indent()" function
6912 */
6913 static void
6914f_indent(typval_T *argvars, typval_T *rettv)
6915{
6916 linenr_T lnum;
6917
6918 lnum = get_tv_lnum(argvars);
6919 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6920 rettv->vval.v_number = get_indent_lnum(lnum);
6921 else
6922 rettv->vval.v_number = -1;
6923}
6924
6925/*
6926 * "index()" function
6927 */
6928 static void
6929f_index(typval_T *argvars, typval_T *rettv)
6930{
6931 list_T *l;
6932 listitem_T *item;
6933 long idx = 0;
6934 int ic = FALSE;
6935
6936 rettv->vval.v_number = -1;
6937 if (argvars[0].v_type != VAR_LIST)
6938 {
6939 EMSG(_(e_listreq));
6940 return;
6941 }
6942 l = argvars[0].vval.v_list;
6943 if (l != NULL)
6944 {
6945 item = l->lv_first;
6946 if (argvars[2].v_type != VAR_UNKNOWN)
6947 {
6948 int error = FALSE;
6949
6950 /* Start at specified item. Use the cached index that list_find()
6951 * sets, so that a negative number also works. */
6952 item = list_find(l, (long)get_tv_number_chk(&argvars[2], &error));
6953 idx = l->lv_idx;
6954 if (argvars[3].v_type != VAR_UNKNOWN)
6955 ic = (int)get_tv_number_chk(&argvars[3], &error);
6956 if (error)
6957 item = NULL;
6958 }
6959
6960 for ( ; item != NULL; item = item->li_next, ++idx)
6961 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6962 {
6963 rettv->vval.v_number = idx;
6964 break;
6965 }
6966 }
6967}
6968
6969static int inputsecret_flag = 0;
6970
6971/*
6972 * "input()" function
6973 * Also handles inputsecret() when inputsecret is set.
6974 */
6975 static void
6976f_input(typval_T *argvars, typval_T *rettv)
6977{
6978 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6979}
6980
6981/*
6982 * "inputdialog()" function
6983 */
6984 static void
6985f_inputdialog(typval_T *argvars, typval_T *rettv)
6986{
6987#if defined(FEAT_GUI_TEXTDIALOG)
6988 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
6989 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6990 {
6991 char_u *message;
6992 char_u buf[NUMBUFLEN];
6993 char_u *defstr = (char_u *)"";
6994
6995 message = get_tv_string_chk(&argvars[0]);
6996 if (argvars[1].v_type != VAR_UNKNOWN
6997 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
6998 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6999 else
7000 IObuff[0] = NUL;
7001 if (message != NULL && defstr != NULL
7002 && do_dialog(VIM_QUESTION, NULL, message,
7003 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
7004 rettv->vval.v_string = vim_strsave(IObuff);
7005 else
7006 {
7007 if (message != NULL && defstr != NULL
7008 && argvars[1].v_type != VAR_UNKNOWN
7009 && argvars[2].v_type != VAR_UNKNOWN)
7010 rettv->vval.v_string = vim_strsave(
7011 get_tv_string_buf(&argvars[2], buf));
7012 else
7013 rettv->vval.v_string = NULL;
7014 }
7015 rettv->v_type = VAR_STRING;
7016 }
7017 else
7018#endif
7019 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
7020}
7021
7022/*
7023 * "inputlist()" function
7024 */
7025 static void
7026f_inputlist(typval_T *argvars, typval_T *rettv)
7027{
7028 listitem_T *li;
7029 int selected;
7030 int mouse_used;
7031
7032#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007033 /* While starting up, there is no place to enter text. When running tests
7034 * with --not-a-term we assume feedkeys() will be used. */
7035 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007036 return;
7037#endif
7038 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
7039 {
7040 EMSG2(_(e_listarg), "inputlist()");
7041 return;
7042 }
7043
7044 msg_start();
7045 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
7046 lines_left = Rows; /* avoid more prompt */
7047 msg_scroll = TRUE;
7048 msg_clr_eos();
7049
7050 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
7051 {
7052 msg_puts(get_tv_string(&li->li_tv));
7053 msg_putchar('\n');
7054 }
7055
7056 /* Ask for choice. */
7057 selected = prompt_for_number(&mouse_used);
7058 if (mouse_used)
7059 selected -= lines_left;
7060
7061 rettv->vval.v_number = selected;
7062}
7063
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007064static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7065
7066/*
7067 * "inputrestore()" function
7068 */
7069 static void
7070f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7071{
7072 if (ga_userinput.ga_len > 0)
7073 {
7074 --ga_userinput.ga_len;
7075 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7076 + ga_userinput.ga_len);
7077 /* default return is zero == OK */
7078 }
7079 else if (p_verbose > 1)
7080 {
7081 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
7082 rettv->vval.v_number = 1; /* Failed */
7083 }
7084}
7085
7086/*
7087 * "inputsave()" function
7088 */
7089 static void
7090f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7091{
7092 /* Add an entry to the stack of typeahead storage. */
7093 if (ga_grow(&ga_userinput, 1) == OK)
7094 {
7095 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7096 + ga_userinput.ga_len);
7097 ++ga_userinput.ga_len;
7098 /* default return is zero == OK */
7099 }
7100 else
7101 rettv->vval.v_number = 1; /* Failed */
7102}
7103
7104/*
7105 * "inputsecret()" function
7106 */
7107 static void
7108f_inputsecret(typval_T *argvars, typval_T *rettv)
7109{
7110 ++cmdline_star;
7111 ++inputsecret_flag;
7112 f_input(argvars, rettv);
7113 --cmdline_star;
7114 --inputsecret_flag;
7115}
7116
7117/*
7118 * "insert()" function
7119 */
7120 static void
7121f_insert(typval_T *argvars, typval_T *rettv)
7122{
7123 long before = 0;
7124 listitem_T *item;
7125 list_T *l;
7126 int error = FALSE;
7127
7128 if (argvars[0].v_type != VAR_LIST)
7129 EMSG2(_(e_listarg), "insert()");
7130 else if ((l = argvars[0].vval.v_list) != NULL
7131 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
7132 {
7133 if (argvars[2].v_type != VAR_UNKNOWN)
7134 before = (long)get_tv_number_chk(&argvars[2], &error);
7135 if (error)
7136 return; /* type error; errmsg already given */
7137
7138 if (before == l->lv_len)
7139 item = NULL;
7140 else
7141 {
7142 item = list_find(l, before);
7143 if (item == NULL)
7144 {
7145 EMSGN(_(e_listidx), before);
7146 l = NULL;
7147 }
7148 }
7149 if (l != NULL)
7150 {
7151 list_insert_tv(l, &argvars[1], item);
7152 copy_tv(&argvars[0], rettv);
7153 }
7154 }
7155}
7156
7157/*
7158 * "invert(expr)" function
7159 */
7160 static void
7161f_invert(typval_T *argvars, typval_T *rettv)
7162{
7163 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
7164}
7165
7166/*
7167 * "isdirectory()" function
7168 */
7169 static void
7170f_isdirectory(typval_T *argvars, typval_T *rettv)
7171{
7172 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
7173}
7174
7175/*
7176 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7177 * or it refers to a List or Dictionary that is locked.
7178 */
7179 static int
7180tv_islocked(typval_T *tv)
7181{
7182 return (tv->v_lock & VAR_LOCKED)
7183 || (tv->v_type == VAR_LIST
7184 && tv->vval.v_list != NULL
7185 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7186 || (tv->v_type == VAR_DICT
7187 && tv->vval.v_dict != NULL
7188 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7189}
7190
7191/*
7192 * "islocked()" function
7193 */
7194 static void
7195f_islocked(typval_T *argvars, typval_T *rettv)
7196{
7197 lval_T lv;
7198 char_u *end;
7199 dictitem_T *di;
7200
7201 rettv->vval.v_number = -1;
7202 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007203 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007204 if (end != NULL && lv.ll_name != NULL)
7205 {
7206 if (*end != NUL)
7207 EMSG(_(e_trailing));
7208 else
7209 {
7210 if (lv.ll_tv == NULL)
7211 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007212 di = find_var(lv.ll_name, NULL, TRUE);
7213 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007214 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007215 /* Consider a variable locked when:
7216 * 1. the variable itself is locked
7217 * 2. the value of the variable is locked.
7218 * 3. the List or Dict value is locked.
7219 */
7220 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7221 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007222 }
7223 }
7224 else if (lv.ll_range)
7225 EMSG(_("E786: Range not allowed"));
7226 else if (lv.ll_newkey != NULL)
7227 EMSG2(_(e_dictkey), lv.ll_newkey);
7228 else if (lv.ll_list != NULL)
7229 /* List item. */
7230 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7231 else
7232 /* Dictionary item. */
7233 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7234 }
7235 }
7236
7237 clear_lval(&lv);
7238}
7239
7240#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7241/*
7242 * "isnan()" function
7243 */
7244 static void
7245f_isnan(typval_T *argvars, typval_T *rettv)
7246{
7247 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7248 && isnan(argvars[0].vval.v_float);
7249}
7250#endif
7251
7252/*
7253 * "items(dict)" function
7254 */
7255 static void
7256f_items(typval_T *argvars, typval_T *rettv)
7257{
7258 dict_list(argvars, rettv, 2);
7259}
7260
7261#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
7262/*
7263 * Get the job from the argument.
7264 * Returns NULL if the job is invalid.
7265 */
7266 static job_T *
7267get_job_arg(typval_T *tv)
7268{
7269 job_T *job;
7270
7271 if (tv->v_type != VAR_JOB)
7272 {
7273 EMSG2(_(e_invarg2), get_tv_string(tv));
7274 return NULL;
7275 }
7276 job = tv->vval.v_job;
7277
7278 if (job == NULL)
7279 EMSG(_("E916: not a valid job"));
7280 return job;
7281}
7282
7283/*
7284 * "job_getchannel()" function
7285 */
7286 static void
7287f_job_getchannel(typval_T *argvars, typval_T *rettv)
7288{
7289 job_T *job = get_job_arg(&argvars[0]);
7290
7291 if (job != NULL)
7292 {
7293 rettv->v_type = VAR_CHANNEL;
7294 rettv->vval.v_channel = job->jv_channel;
7295 if (job->jv_channel != NULL)
7296 ++job->jv_channel->ch_refcount;
7297 }
7298}
7299
7300/*
7301 * "job_info()" function
7302 */
7303 static void
7304f_job_info(typval_T *argvars, typval_T *rettv)
7305{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007306 if (argvars[0].v_type != VAR_UNKNOWN)
7307 {
7308 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007309
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007310 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7311 job_info(job, rettv->vval.v_dict);
7312 }
7313 else if (rettv_list_alloc(rettv) == OK)
7314 job_info_all(rettv->vval.v_list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007315}
7316
7317/*
7318 * "job_setoptions()" function
7319 */
7320 static void
7321f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7322{
7323 job_T *job = get_job_arg(&argvars[0]);
7324 jobopt_T opt;
7325
7326 if (job == NULL)
7327 return;
7328 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007329 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007330 job_set_options(job, &opt);
7331 free_job_options(&opt);
7332}
7333
7334/*
7335 * "job_start()" function
7336 */
7337 static void
7338f_job_start(typval_T *argvars, typval_T *rettv)
7339{
7340 rettv->v_type = VAR_JOB;
7341 if (check_restricted() || check_secure())
7342 return;
Bram Moolenaar493359e2018-06-12 20:25:52 +02007343 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007344}
7345
7346/*
7347 * "job_status()" function
7348 */
7349 static void
7350f_job_status(typval_T *argvars, typval_T *rettv)
7351{
7352 job_T *job = get_job_arg(&argvars[0]);
7353
7354 if (job != NULL)
7355 {
7356 rettv->v_type = VAR_STRING;
7357 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7358 }
7359}
7360
7361/*
7362 * "job_stop()" function
7363 */
7364 static void
7365f_job_stop(typval_T *argvars, typval_T *rettv)
7366{
7367 job_T *job = get_job_arg(&argvars[0]);
7368
7369 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007370 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007371}
7372#endif
7373
7374/*
7375 * "join()" function
7376 */
7377 static void
7378f_join(typval_T *argvars, typval_T *rettv)
7379{
7380 garray_T ga;
7381 char_u *sep;
7382
7383 if (argvars[0].v_type != VAR_LIST)
7384 {
7385 EMSG(_(e_listreq));
7386 return;
7387 }
7388 if (argvars[0].vval.v_list == NULL)
7389 return;
7390 if (argvars[1].v_type == VAR_UNKNOWN)
7391 sep = (char_u *)" ";
7392 else
7393 sep = get_tv_string_chk(&argvars[1]);
7394
7395 rettv->v_type = VAR_STRING;
7396
7397 if (sep != NULL)
7398 {
7399 ga_init2(&ga, (int)sizeof(char), 80);
7400 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7401 ga_append(&ga, NUL);
7402 rettv->vval.v_string = (char_u *)ga.ga_data;
7403 }
7404 else
7405 rettv->vval.v_string = NULL;
7406}
7407
7408/*
7409 * "js_decode()" function
7410 */
7411 static void
7412f_js_decode(typval_T *argvars, typval_T *rettv)
7413{
7414 js_read_T reader;
7415
7416 reader.js_buf = get_tv_string(&argvars[0]);
7417 reader.js_fill = NULL;
7418 reader.js_used = 0;
7419 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
7420 EMSG(_(e_invarg));
7421}
7422
7423/*
7424 * "js_encode()" function
7425 */
7426 static void
7427f_js_encode(typval_T *argvars, typval_T *rettv)
7428{
7429 rettv->v_type = VAR_STRING;
7430 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7431}
7432
7433/*
7434 * "json_decode()" function
7435 */
7436 static void
7437f_json_decode(typval_T *argvars, typval_T *rettv)
7438{
7439 js_read_T reader;
7440
7441 reader.js_buf = get_tv_string(&argvars[0]);
7442 reader.js_fill = NULL;
7443 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007444 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007445}
7446
7447/*
7448 * "json_encode()" function
7449 */
7450 static void
7451f_json_encode(typval_T *argvars, typval_T *rettv)
7452{
7453 rettv->v_type = VAR_STRING;
7454 rettv->vval.v_string = json_encode(&argvars[0], 0);
7455}
7456
7457/*
7458 * "keys()" function
7459 */
7460 static void
7461f_keys(typval_T *argvars, typval_T *rettv)
7462{
7463 dict_list(argvars, rettv, 0);
7464}
7465
7466/*
7467 * "last_buffer_nr()" function.
7468 */
7469 static void
7470f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7471{
7472 int n = 0;
7473 buf_T *buf;
7474
Bram Moolenaar29323592016-07-24 22:04:11 +02007475 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007476 if (n < buf->b_fnum)
7477 n = buf->b_fnum;
7478
7479 rettv->vval.v_number = n;
7480}
7481
7482/*
7483 * "len()" function
7484 */
7485 static void
7486f_len(typval_T *argvars, typval_T *rettv)
7487{
7488 switch (argvars[0].v_type)
7489 {
7490 case VAR_STRING:
7491 case VAR_NUMBER:
7492 rettv->vval.v_number = (varnumber_T)STRLEN(
7493 get_tv_string(&argvars[0]));
7494 break;
7495 case VAR_LIST:
7496 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7497 break;
7498 case VAR_DICT:
7499 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7500 break;
7501 case VAR_UNKNOWN:
7502 case VAR_SPECIAL:
7503 case VAR_FLOAT:
7504 case VAR_FUNC:
7505 case VAR_PARTIAL:
7506 case VAR_JOB:
7507 case VAR_CHANNEL:
7508 EMSG(_("E701: Invalid type for len()"));
7509 break;
7510 }
7511}
7512
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007513 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007514libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007515{
7516#ifdef FEAT_LIBCALL
7517 char_u *string_in;
7518 char_u **string_result;
7519 int nr_result;
7520#endif
7521
7522 rettv->v_type = type;
7523 if (type != VAR_NUMBER)
7524 rettv->vval.v_string = NULL;
7525
7526 if (check_restricted() || check_secure())
7527 return;
7528
7529#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007530 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007531 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7532 {
7533 string_in = NULL;
7534 if (argvars[2].v_type == VAR_STRING)
7535 string_in = argvars[2].vval.v_string;
7536 if (type == VAR_NUMBER)
7537 string_result = NULL;
7538 else
7539 string_result = &rettv->vval.v_string;
7540 if (mch_libcall(argvars[0].vval.v_string,
7541 argvars[1].vval.v_string,
7542 string_in,
7543 argvars[2].vval.v_number,
7544 string_result,
7545 &nr_result) == OK
7546 && type == VAR_NUMBER)
7547 rettv->vval.v_number = nr_result;
7548 }
7549#endif
7550}
7551
7552/*
7553 * "libcall()" function
7554 */
7555 static void
7556f_libcall(typval_T *argvars, typval_T *rettv)
7557{
7558 libcall_common(argvars, rettv, VAR_STRING);
7559}
7560
7561/*
7562 * "libcallnr()" function
7563 */
7564 static void
7565f_libcallnr(typval_T *argvars, typval_T *rettv)
7566{
7567 libcall_common(argvars, rettv, VAR_NUMBER);
7568}
7569
7570/*
7571 * "line(string)" function
7572 */
7573 static void
7574f_line(typval_T *argvars, typval_T *rettv)
7575{
7576 linenr_T lnum = 0;
7577 pos_T *fp;
7578 int fnum;
7579
7580 fp = var2fpos(&argvars[0], TRUE, &fnum);
7581 if (fp != NULL)
7582 lnum = fp->lnum;
7583 rettv->vval.v_number = lnum;
7584}
7585
7586/*
7587 * "line2byte(lnum)" function
7588 */
7589 static void
7590f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7591{
7592#ifndef FEAT_BYTEOFF
7593 rettv->vval.v_number = -1;
7594#else
7595 linenr_T lnum;
7596
7597 lnum = get_tv_lnum(argvars);
7598 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7599 rettv->vval.v_number = -1;
7600 else
7601 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7602 if (rettv->vval.v_number >= 0)
7603 ++rettv->vval.v_number;
7604#endif
7605}
7606
7607/*
7608 * "lispindent(lnum)" function
7609 */
7610 static void
7611f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7612{
7613#ifdef FEAT_LISP
7614 pos_T pos;
7615 linenr_T lnum;
7616
7617 pos = curwin->w_cursor;
7618 lnum = get_tv_lnum(argvars);
7619 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7620 {
7621 curwin->w_cursor.lnum = lnum;
7622 rettv->vval.v_number = get_lisp_indent();
7623 curwin->w_cursor = pos;
7624 }
7625 else
7626#endif
7627 rettv->vval.v_number = -1;
7628}
7629
7630/*
7631 * "localtime()" function
7632 */
7633 static void
7634f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7635{
7636 rettv->vval.v_number = (varnumber_T)time(NULL);
7637}
7638
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007639 static void
7640get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7641{
7642 char_u *keys;
7643 char_u *which;
7644 char_u buf[NUMBUFLEN];
7645 char_u *keys_buf = NULL;
7646 char_u *rhs;
7647 int mode;
7648 int abbr = FALSE;
7649 int get_dict = FALSE;
7650 mapblock_T *mp;
7651 int buffer_local;
7652
7653 /* return empty string for failure */
7654 rettv->v_type = VAR_STRING;
7655 rettv->vval.v_string = NULL;
7656
7657 keys = get_tv_string(&argvars[0]);
7658 if (*keys == NUL)
7659 return;
7660
7661 if (argvars[1].v_type != VAR_UNKNOWN)
7662 {
7663 which = get_tv_string_buf_chk(&argvars[1], buf);
7664 if (argvars[2].v_type != VAR_UNKNOWN)
7665 {
7666 abbr = (int)get_tv_number(&argvars[2]);
7667 if (argvars[3].v_type != VAR_UNKNOWN)
7668 get_dict = (int)get_tv_number(&argvars[3]);
7669 }
7670 }
7671 else
7672 which = (char_u *)"";
7673 if (which == NULL)
7674 return;
7675
7676 mode = get_map_mode(&which, 0);
7677
7678 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7679 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7680 vim_free(keys_buf);
7681
7682 if (!get_dict)
7683 {
7684 /* Return a string. */
7685 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02007686 {
7687 if (*rhs == NUL)
7688 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
7689 else
7690 rettv->vval.v_string = str2special_save(rhs, FALSE);
7691 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007692
7693 }
7694 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7695 {
7696 /* Return a dictionary. */
7697 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7698 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7699 dict_T *dict = rettv->vval.v_dict;
7700
Bram Moolenaare0be1672018-07-08 16:50:37 +02007701 dict_add_string(dict, "lhs", lhs);
7702 dict_add_string(dict, "rhs", mp->m_orig_str);
7703 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
7704 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
7705 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007706 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
7707 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02007708 dict_add_number(dict, "buffer", (long)buffer_local);
7709 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
7710 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007711
7712 vim_free(lhs);
7713 vim_free(mapmode);
7714 }
7715}
7716
7717#ifdef FEAT_FLOAT
7718/*
7719 * "log()" function
7720 */
7721 static void
7722f_log(typval_T *argvars, typval_T *rettv)
7723{
7724 float_T f = 0.0;
7725
7726 rettv->v_type = VAR_FLOAT;
7727 if (get_float_arg(argvars, &f) == OK)
7728 rettv->vval.v_float = log(f);
7729 else
7730 rettv->vval.v_float = 0.0;
7731}
7732
7733/*
7734 * "log10()" function
7735 */
7736 static void
7737f_log10(typval_T *argvars, typval_T *rettv)
7738{
7739 float_T f = 0.0;
7740
7741 rettv->v_type = VAR_FLOAT;
7742 if (get_float_arg(argvars, &f) == OK)
7743 rettv->vval.v_float = log10(f);
7744 else
7745 rettv->vval.v_float = 0.0;
7746}
7747#endif
7748
7749#ifdef FEAT_LUA
7750/*
7751 * "luaeval()" function
7752 */
7753 static void
7754f_luaeval(typval_T *argvars, typval_T *rettv)
7755{
7756 char_u *str;
7757 char_u buf[NUMBUFLEN];
7758
7759 str = get_tv_string_buf(&argvars[0], buf);
7760 do_luaeval(str, argvars + 1, rettv);
7761}
7762#endif
7763
7764/*
7765 * "map()" function
7766 */
7767 static void
7768f_map(typval_T *argvars, typval_T *rettv)
7769{
7770 filter_map(argvars, rettv, TRUE);
7771}
7772
7773/*
7774 * "maparg()" function
7775 */
7776 static void
7777f_maparg(typval_T *argvars, typval_T *rettv)
7778{
7779 get_maparg(argvars, rettv, TRUE);
7780}
7781
7782/*
7783 * "mapcheck()" function
7784 */
7785 static void
7786f_mapcheck(typval_T *argvars, typval_T *rettv)
7787{
7788 get_maparg(argvars, rettv, FALSE);
7789}
7790
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007791typedef enum
7792{
7793 MATCH_END, /* matchend() */
7794 MATCH_MATCH, /* match() */
7795 MATCH_STR, /* matchstr() */
7796 MATCH_LIST, /* matchlist() */
7797 MATCH_POS /* matchstrpos() */
7798} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007799
7800 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007801find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007802{
7803 char_u *str = NULL;
7804 long len = 0;
7805 char_u *expr = NULL;
7806 char_u *pat;
7807 regmatch_T regmatch;
7808 char_u patbuf[NUMBUFLEN];
7809 char_u strbuf[NUMBUFLEN];
7810 char_u *save_cpo;
7811 long start = 0;
7812 long nth = 1;
7813 colnr_T startcol = 0;
7814 int match = 0;
7815 list_T *l = NULL;
7816 listitem_T *li = NULL;
7817 long idx = 0;
7818 char_u *tofree = NULL;
7819
7820 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7821 save_cpo = p_cpo;
7822 p_cpo = (char_u *)"";
7823
7824 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007825 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007826 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007827 /* type MATCH_LIST: return empty list when there are no matches.
7828 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007829 if (rettv_list_alloc(rettv) == FAIL)
7830 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007831 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007832 && (list_append_string(rettv->vval.v_list,
7833 (char_u *)"", 0) == FAIL
7834 || list_append_number(rettv->vval.v_list,
7835 (varnumber_T)-1) == FAIL
7836 || list_append_number(rettv->vval.v_list,
7837 (varnumber_T)-1) == FAIL
7838 || list_append_number(rettv->vval.v_list,
7839 (varnumber_T)-1) == FAIL))
7840 {
7841 list_free(rettv->vval.v_list);
7842 rettv->vval.v_list = NULL;
7843 goto theend;
7844 }
7845 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007846 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007847 {
7848 rettv->v_type = VAR_STRING;
7849 rettv->vval.v_string = NULL;
7850 }
7851
7852 if (argvars[0].v_type == VAR_LIST)
7853 {
7854 if ((l = argvars[0].vval.v_list) == NULL)
7855 goto theend;
7856 li = l->lv_first;
7857 }
7858 else
7859 {
7860 expr = str = get_tv_string(&argvars[0]);
7861 len = (long)STRLEN(str);
7862 }
7863
7864 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
7865 if (pat == NULL)
7866 goto theend;
7867
7868 if (argvars[2].v_type != VAR_UNKNOWN)
7869 {
7870 int error = FALSE;
7871
7872 start = (long)get_tv_number_chk(&argvars[2], &error);
7873 if (error)
7874 goto theend;
7875 if (l != NULL)
7876 {
7877 li = list_find(l, start);
7878 if (li == NULL)
7879 goto theend;
7880 idx = l->lv_idx; /* use the cached index */
7881 }
7882 else
7883 {
7884 if (start < 0)
7885 start = 0;
7886 if (start > len)
7887 goto theend;
7888 /* When "count" argument is there ignore matches before "start",
7889 * otherwise skip part of the string. Differs when pattern is "^"
7890 * or "\<". */
7891 if (argvars[3].v_type != VAR_UNKNOWN)
7892 startcol = start;
7893 else
7894 {
7895 str += start;
7896 len -= start;
7897 }
7898 }
7899
7900 if (argvars[3].v_type != VAR_UNKNOWN)
7901 nth = (long)get_tv_number_chk(&argvars[3], &error);
7902 if (error)
7903 goto theend;
7904 }
7905
7906 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7907 if (regmatch.regprog != NULL)
7908 {
7909 regmatch.rm_ic = p_ic;
7910
7911 for (;;)
7912 {
7913 if (l != NULL)
7914 {
7915 if (li == NULL)
7916 {
7917 match = FALSE;
7918 break;
7919 }
7920 vim_free(tofree);
7921 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
7922 if (str == NULL)
7923 break;
7924 }
7925
7926 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
7927
7928 if (match && --nth <= 0)
7929 break;
7930 if (l == NULL && !match)
7931 break;
7932
7933 /* Advance to just after the match. */
7934 if (l != NULL)
7935 {
7936 li = li->li_next;
7937 ++idx;
7938 }
7939 else
7940 {
7941#ifdef FEAT_MBYTE
7942 startcol = (colnr_T)(regmatch.startp[0]
7943 + (*mb_ptr2len)(regmatch.startp[0]) - str);
7944#else
7945 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
7946#endif
7947 if (startcol > (colnr_T)len
7948 || str + startcol <= regmatch.startp[0])
7949 {
7950 match = FALSE;
7951 break;
7952 }
7953 }
7954 }
7955
7956 if (match)
7957 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007958 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007959 {
7960 listitem_T *li1 = rettv->vval.v_list->lv_first;
7961 listitem_T *li2 = li1->li_next;
7962 listitem_T *li3 = li2->li_next;
7963 listitem_T *li4 = li3->li_next;
7964
7965 vim_free(li1->li_tv.vval.v_string);
7966 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
7967 (int)(regmatch.endp[0] - regmatch.startp[0]));
7968 li3->li_tv.vval.v_number =
7969 (varnumber_T)(regmatch.startp[0] - expr);
7970 li4->li_tv.vval.v_number =
7971 (varnumber_T)(regmatch.endp[0] - expr);
7972 if (l != NULL)
7973 li2->li_tv.vval.v_number = (varnumber_T)idx;
7974 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007975 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007976 {
7977 int i;
7978
7979 /* return list with matched string and submatches */
7980 for (i = 0; i < NSUBEXP; ++i)
7981 {
7982 if (regmatch.endp[i] == NULL)
7983 {
7984 if (list_append_string(rettv->vval.v_list,
7985 (char_u *)"", 0) == FAIL)
7986 break;
7987 }
7988 else if (list_append_string(rettv->vval.v_list,
7989 regmatch.startp[i],
7990 (int)(regmatch.endp[i] - regmatch.startp[i]))
7991 == FAIL)
7992 break;
7993 }
7994 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007995 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007996 {
7997 /* return matched string */
7998 if (l != NULL)
7999 copy_tv(&li->li_tv, rettv);
8000 else
8001 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
8002 (int)(regmatch.endp[0] - regmatch.startp[0]));
8003 }
8004 else if (l != NULL)
8005 rettv->vval.v_number = idx;
8006 else
8007 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008008 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008009 rettv->vval.v_number =
8010 (varnumber_T)(regmatch.startp[0] - str);
8011 else
8012 rettv->vval.v_number =
8013 (varnumber_T)(regmatch.endp[0] - str);
8014 rettv->vval.v_number += (varnumber_T)(str - expr);
8015 }
8016 }
8017 vim_regfree(regmatch.regprog);
8018 }
8019
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008020theend:
8021 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008022 /* matchstrpos() without a list: drop the second item. */
8023 listitem_remove(rettv->vval.v_list,
8024 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008025 vim_free(tofree);
8026 p_cpo = save_cpo;
8027}
8028
8029/*
8030 * "match()" function
8031 */
8032 static void
8033f_match(typval_T *argvars, typval_T *rettv)
8034{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008035 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008036}
8037
Bram Moolenaar95e51472018-07-28 16:55:56 +02008038#ifdef FEAT_SEARCH_EXTRA
8039 static int
8040matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
8041{
8042 dictitem_T *di;
8043
8044 if (tv->v_type != VAR_DICT)
8045 {
8046 EMSG(_(e_dictreq));
8047 return FAIL;
8048 }
8049
8050 if (dict_find(tv->vval.v_dict, (char_u *)"conceal", -1) != NULL)
8051 *conceal_char = get_dict_string(tv->vval.v_dict,
8052 (char_u *)"conceal", FALSE);
8053
8054 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
8055 {
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02008056 *win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008057 if (*win == NULL)
8058 {
8059 EMSG(_("E957: Invalid window number"));
8060 return FAIL;
8061 }
8062 }
8063
8064 return OK;
8065}
8066#endif
8067
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008068/*
8069 * "matchadd()" function
8070 */
8071 static void
8072f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8073{
8074#ifdef FEAT_SEARCH_EXTRA
8075 char_u buf[NUMBUFLEN];
8076 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
8077 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
8078 int prio = 10; /* default priority */
8079 int id = -1;
8080 int error = FALSE;
8081 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008082 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008083
8084 rettv->vval.v_number = -1;
8085
8086 if (grp == NULL || pat == NULL)
8087 return;
8088 if (argvars[2].v_type != VAR_UNKNOWN)
8089 {
8090 prio = (int)get_tv_number_chk(&argvars[2], &error);
8091 if (argvars[3].v_type != VAR_UNKNOWN)
8092 {
8093 id = (int)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008094 if (argvars[4].v_type != VAR_UNKNOWN
8095 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8096 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008097 }
8098 }
8099 if (error == TRUE)
8100 return;
8101 if (id >= 1 && id <= 3)
8102 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02008103 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008104 return;
8105 }
8106
Bram Moolenaar95e51472018-07-28 16:55:56 +02008107 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008108 conceal_char);
8109#endif
8110}
8111
8112/*
8113 * "matchaddpos()" function
8114 */
8115 static void
8116f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8117{
8118#ifdef FEAT_SEARCH_EXTRA
8119 char_u buf[NUMBUFLEN];
8120 char_u *group;
8121 int prio = 10;
8122 int id = -1;
8123 int error = FALSE;
8124 list_T *l;
8125 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008126 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008127
8128 rettv->vval.v_number = -1;
8129
8130 group = get_tv_string_buf_chk(&argvars[0], buf);
8131 if (group == NULL)
8132 return;
8133
8134 if (argvars[1].v_type != VAR_LIST)
8135 {
8136 EMSG2(_(e_listarg), "matchaddpos()");
8137 return;
8138 }
8139 l = argvars[1].vval.v_list;
8140 if (l == NULL)
8141 return;
8142
8143 if (argvars[2].v_type != VAR_UNKNOWN)
8144 {
8145 prio = (int)get_tv_number_chk(&argvars[2], &error);
8146 if (argvars[3].v_type != VAR_UNKNOWN)
8147 {
8148 id = (int)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008149
8150 if (argvars[4].v_type != VAR_UNKNOWN
8151 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8152 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008153 }
8154 }
8155 if (error == TRUE)
8156 return;
8157
8158 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8159 if (id == 1 || id == 2)
8160 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02008161 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008162 return;
8163 }
8164
Bram Moolenaar95e51472018-07-28 16:55:56 +02008165 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008166 conceal_char);
8167#endif
8168}
8169
8170/*
8171 * "matcharg()" function
8172 */
8173 static void
8174f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8175{
8176 if (rettv_list_alloc(rettv) == OK)
8177 {
8178#ifdef FEAT_SEARCH_EXTRA
8179 int id = (int)get_tv_number(&argvars[0]);
8180 matchitem_T *m;
8181
8182 if (id >= 1 && id <= 3)
8183 {
8184 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8185 {
8186 list_append_string(rettv->vval.v_list,
8187 syn_id2name(m->hlg_id), -1);
8188 list_append_string(rettv->vval.v_list, m->pattern, -1);
8189 }
8190 else
8191 {
8192 list_append_string(rettv->vval.v_list, NULL, -1);
8193 list_append_string(rettv->vval.v_list, NULL, -1);
8194 }
8195 }
8196#endif
8197 }
8198}
8199
8200/*
8201 * "matchdelete()" function
8202 */
8203 static void
8204f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8205{
8206#ifdef FEAT_SEARCH_EXTRA
8207 rettv->vval.v_number = match_delete(curwin,
8208 (int)get_tv_number(&argvars[0]), TRUE);
8209#endif
8210}
8211
8212/*
8213 * "matchend()" function
8214 */
8215 static void
8216f_matchend(typval_T *argvars, typval_T *rettv)
8217{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008218 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008219}
8220
8221/*
8222 * "matchlist()" function
8223 */
8224 static void
8225f_matchlist(typval_T *argvars, typval_T *rettv)
8226{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008227 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008228}
8229
8230/*
8231 * "matchstr()" function
8232 */
8233 static void
8234f_matchstr(typval_T *argvars, typval_T *rettv)
8235{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008236 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008237}
8238
8239/*
8240 * "matchstrpos()" function
8241 */
8242 static void
8243f_matchstrpos(typval_T *argvars, typval_T *rettv)
8244{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008245 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008246}
8247
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008248 static void
8249max_min(typval_T *argvars, typval_T *rettv, int domax)
8250{
8251 varnumber_T n = 0;
8252 varnumber_T i;
8253 int error = FALSE;
8254
8255 if (argvars[0].v_type == VAR_LIST)
8256 {
8257 list_T *l;
8258 listitem_T *li;
8259
8260 l = argvars[0].vval.v_list;
8261 if (l != NULL)
8262 {
8263 li = l->lv_first;
8264 if (li != NULL)
8265 {
8266 n = get_tv_number_chk(&li->li_tv, &error);
8267 for (;;)
8268 {
8269 li = li->li_next;
8270 if (li == NULL)
8271 break;
8272 i = get_tv_number_chk(&li->li_tv, &error);
8273 if (domax ? i > n : i < n)
8274 n = i;
8275 }
8276 }
8277 }
8278 }
8279 else if (argvars[0].v_type == VAR_DICT)
8280 {
8281 dict_T *d;
8282 int first = TRUE;
8283 hashitem_T *hi;
8284 int todo;
8285
8286 d = argvars[0].vval.v_dict;
8287 if (d != NULL)
8288 {
8289 todo = (int)d->dv_hashtab.ht_used;
8290 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8291 {
8292 if (!HASHITEM_EMPTY(hi))
8293 {
8294 --todo;
8295 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
8296 if (first)
8297 {
8298 n = i;
8299 first = FALSE;
8300 }
8301 else if (domax ? i > n : i < n)
8302 n = i;
8303 }
8304 }
8305 }
8306 }
8307 else
Bram Moolenaar26b84332016-09-04 21:42:36 +02008308 EMSG2(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008309 rettv->vval.v_number = error ? 0 : n;
8310}
8311
8312/*
8313 * "max()" function
8314 */
8315 static void
8316f_max(typval_T *argvars, typval_T *rettv)
8317{
8318 max_min(argvars, rettv, TRUE);
8319}
8320
8321/*
8322 * "min()" function
8323 */
8324 static void
8325f_min(typval_T *argvars, typval_T *rettv)
8326{
8327 max_min(argvars, rettv, FALSE);
8328}
8329
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008330/*
8331 * Create the directory in which "dir" is located, and higher levels when
8332 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008333 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008334 */
8335 static int
8336mkdir_recurse(char_u *dir, int prot)
8337{
8338 char_u *p;
8339 char_u *updir;
8340 int r = FAIL;
8341
8342 /* Get end of directory name in "dir".
8343 * We're done when it's "/" or "c:/". */
8344 p = gettail_sep(dir);
8345 if (p <= get_past_head(dir))
8346 return OK;
8347
8348 /* If the directory exists we're done. Otherwise: create it.*/
8349 updir = vim_strnsave(dir, (int)(p - dir));
8350 if (updir == NULL)
8351 return FAIL;
8352 if (mch_isdir(updir))
8353 r = OK;
8354 else if (mkdir_recurse(updir, prot) == OK)
8355 r = vim_mkdir_emsg(updir, prot);
8356 vim_free(updir);
8357 return r;
8358}
8359
8360#ifdef vim_mkdir
8361/*
8362 * "mkdir()" function
8363 */
8364 static void
8365f_mkdir(typval_T *argvars, typval_T *rettv)
8366{
8367 char_u *dir;
8368 char_u buf[NUMBUFLEN];
8369 int prot = 0755;
8370
8371 rettv->vval.v_number = FAIL;
8372 if (check_restricted() || check_secure())
8373 return;
8374
8375 dir = get_tv_string_buf(&argvars[0], buf);
8376 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008377 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008378
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008379 if (*gettail(dir) == NUL)
8380 /* remove trailing slashes */
8381 *gettail_sep(dir) = NUL;
8382
8383 if (argvars[1].v_type != VAR_UNKNOWN)
8384 {
8385 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008386 {
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008387 prot = (int)get_tv_number_chk(&argvars[2], NULL);
8388 if (prot == -1)
8389 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008390 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008391 if (STRCMP(get_tv_string(&argvars[1]), "p") == 0)
8392 {
8393 if (mch_isdir(dir))
8394 {
8395 /* With the "p" flag it's OK if the dir already exists. */
8396 rettv->vval.v_number = OK;
8397 return;
8398 }
8399 mkdir_recurse(dir, prot);
8400 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008401 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008402 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008403}
8404#endif
8405
8406/*
8407 * "mode()" function
8408 */
8409 static void
8410f_mode(typval_T *argvars, typval_T *rettv)
8411{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008412 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008413
Bram Moolenaar612cc382018-07-29 15:34:26 +02008414 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008415
8416 if (time_for_testing == 93784)
8417 {
8418 /* Testing the two-character code. */
8419 buf[0] = 'x';
8420 buf[1] = '!';
8421 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008422#ifdef FEAT_TERMINAL
8423 else if (term_use_loop())
8424 buf[0] = 't';
8425#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008426 else if (VIsual_active)
8427 {
8428 if (VIsual_select)
8429 buf[0] = VIsual_mode + 's' - 'v';
8430 else
8431 buf[0] = VIsual_mode;
8432 }
8433 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8434 || State == CONFIRM)
8435 {
8436 buf[0] = 'r';
8437 if (State == ASKMORE)
8438 buf[1] = 'm';
8439 else if (State == CONFIRM)
8440 buf[1] = '?';
8441 }
8442 else if (State == EXTERNCMD)
8443 buf[0] = '!';
8444 else if (State & INSERT)
8445 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008446 if (State & VREPLACE_FLAG)
8447 {
8448 buf[0] = 'R';
8449 buf[1] = 'v';
8450 }
8451 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008452 {
8453 if (State & REPLACE_FLAG)
8454 buf[0] = 'R';
8455 else
8456 buf[0] = 'i';
8457#ifdef FEAT_INS_EXPAND
8458 if (ins_compl_active())
8459 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008460 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008461 buf[1] = 'x';
8462#endif
8463 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008464 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008465 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008466 {
8467 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008468 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008469 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008470 else if (exmode_active == EXMODE_NORMAL)
8471 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008472 }
8473 else
8474 {
8475 buf[0] = 'n';
8476 if (finish_op)
8477 buf[1] = 'o';
Bram Moolenaar612cc382018-07-29 15:34:26 +02008478 else if (restart_edit == 'I' || restart_edit == 'R'
8479 || restart_edit == 'V')
8480 {
8481 buf[1] = 'i';
8482 buf[2] = restart_edit;
8483 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008484 }
8485
8486 /* Clear out the minor mode when the argument is not a non-zero number or
8487 * non-empty string. */
8488 if (!non_zero_arg(&argvars[0]))
8489 buf[1] = NUL;
8490
8491 rettv->vval.v_string = vim_strsave(buf);
8492 rettv->v_type = VAR_STRING;
8493}
8494
8495#if defined(FEAT_MZSCHEME) || defined(PROTO)
8496/*
8497 * "mzeval()" function
8498 */
8499 static void
8500f_mzeval(typval_T *argvars, typval_T *rettv)
8501{
8502 char_u *str;
8503 char_u buf[NUMBUFLEN];
8504
8505 str = get_tv_string_buf(&argvars[0], buf);
8506 do_mzeval(str, rettv);
8507}
8508
8509 void
8510mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8511{
8512 typval_T argvars[3];
8513
8514 argvars[0].v_type = VAR_STRING;
8515 argvars[0].vval.v_string = name;
8516 copy_tv(args, &argvars[1]);
8517 argvars[2].v_type = VAR_UNKNOWN;
8518 f_call(argvars, rettv);
8519 clear_tv(&argvars[1]);
8520}
8521#endif
8522
8523/*
8524 * "nextnonblank()" function
8525 */
8526 static void
8527f_nextnonblank(typval_T *argvars, typval_T *rettv)
8528{
8529 linenr_T lnum;
8530
8531 for (lnum = get_tv_lnum(argvars); ; ++lnum)
8532 {
8533 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8534 {
8535 lnum = 0;
8536 break;
8537 }
8538 if (*skipwhite(ml_get(lnum)) != NUL)
8539 break;
8540 }
8541 rettv->vval.v_number = lnum;
8542}
8543
8544/*
8545 * "nr2char()" function
8546 */
8547 static void
8548f_nr2char(typval_T *argvars, typval_T *rettv)
8549{
8550 char_u buf[NUMBUFLEN];
8551
8552#ifdef FEAT_MBYTE
8553 if (has_mbyte)
8554 {
8555 int utf8 = 0;
8556
8557 if (argvars[1].v_type != VAR_UNKNOWN)
8558 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
8559 if (utf8)
8560 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8561 else
8562 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8563 }
8564 else
8565#endif
8566 {
8567 buf[0] = (char_u)get_tv_number(&argvars[0]);
8568 buf[1] = NUL;
8569 }
8570 rettv->v_type = VAR_STRING;
8571 rettv->vval.v_string = vim_strsave(buf);
8572}
8573
8574/*
8575 * "or(expr, expr)" function
8576 */
8577 static void
8578f_or(typval_T *argvars, typval_T *rettv)
8579{
8580 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8581 | get_tv_number_chk(&argvars[1], NULL);
8582}
8583
8584/*
8585 * "pathshorten()" function
8586 */
8587 static void
8588f_pathshorten(typval_T *argvars, typval_T *rettv)
8589{
8590 char_u *p;
8591
8592 rettv->v_type = VAR_STRING;
8593 p = get_tv_string_chk(&argvars[0]);
8594 if (p == NULL)
8595 rettv->vval.v_string = NULL;
8596 else
8597 {
8598 p = vim_strsave(p);
8599 rettv->vval.v_string = p;
8600 if (p != NULL)
8601 shorten_dir(p);
8602 }
8603}
8604
8605#ifdef FEAT_PERL
8606/*
8607 * "perleval()" function
8608 */
8609 static void
8610f_perleval(typval_T *argvars, typval_T *rettv)
8611{
8612 char_u *str;
8613 char_u buf[NUMBUFLEN];
8614
8615 str = get_tv_string_buf(&argvars[0], buf);
8616 do_perleval(str, rettv);
8617}
8618#endif
8619
8620#ifdef FEAT_FLOAT
8621/*
8622 * "pow()" function
8623 */
8624 static void
8625f_pow(typval_T *argvars, typval_T *rettv)
8626{
8627 float_T fx = 0.0, fy = 0.0;
8628
8629 rettv->v_type = VAR_FLOAT;
8630 if (get_float_arg(argvars, &fx) == OK
8631 && get_float_arg(&argvars[1], &fy) == OK)
8632 rettv->vval.v_float = pow(fx, fy);
8633 else
8634 rettv->vval.v_float = 0.0;
8635}
8636#endif
8637
8638/*
8639 * "prevnonblank()" function
8640 */
8641 static void
8642f_prevnonblank(typval_T *argvars, typval_T *rettv)
8643{
8644 linenr_T lnum;
8645
8646 lnum = get_tv_lnum(argvars);
8647 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8648 lnum = 0;
8649 else
8650 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8651 --lnum;
8652 rettv->vval.v_number = lnum;
8653}
8654
8655/* This dummy va_list is here because:
8656 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8657 * - locally in the function results in a "used before set" warning
8658 * - using va_start() to initialize it gives "function with fixed args" error */
8659static va_list ap;
8660
8661/*
8662 * "printf()" function
8663 */
8664 static void
8665f_printf(typval_T *argvars, typval_T *rettv)
8666{
8667 char_u buf[NUMBUFLEN];
8668 int len;
8669 char_u *s;
8670 int saved_did_emsg = did_emsg;
8671 char *fmt;
8672
8673 rettv->v_type = VAR_STRING;
8674 rettv->vval.v_string = NULL;
8675
8676 /* Get the required length, allocate the buffer and do it for real. */
8677 did_emsg = FALSE;
8678 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008679 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008680 if (!did_emsg)
8681 {
8682 s = alloc(len + 1);
8683 if (s != NULL)
8684 {
8685 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008686 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8687 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008688 }
8689 }
8690 did_emsg |= saved_did_emsg;
8691}
8692
Bram Moolenaarf2732452018-06-03 14:47:35 +02008693#ifdef FEAT_JOB_CHANNEL
8694/*
8695 * "prompt_setcallback({buffer}, {callback})" function
8696 */
8697 static void
8698f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
8699{
8700 buf_T *buf;
8701 char_u *callback;
8702 partial_T *partial;
8703
8704 if (check_secure())
8705 return;
8706 buf = get_buf_tv(&argvars[0], FALSE);
8707 if (buf == NULL)
8708 return;
8709
8710 callback = get_callback(&argvars[1], &partial);
8711 if (callback == NULL)
8712 return;
8713
8714 free_callback(buf->b_prompt_callback, buf->b_prompt_partial);
8715 if (partial == NULL)
8716 buf->b_prompt_callback = vim_strsave(callback);
8717 else
8718 /* pointer into the partial */
8719 buf->b_prompt_callback = callback;
8720 buf->b_prompt_partial = partial;
8721}
8722
8723/*
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02008724 * "prompt_setinterrupt({buffer}, {callback})" function
8725 */
8726 static void
8727f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
8728{
8729 buf_T *buf;
8730 char_u *callback;
8731 partial_T *partial;
8732
8733 if (check_secure())
8734 return;
8735 buf = get_buf_tv(&argvars[0], FALSE);
8736 if (buf == NULL)
8737 return;
8738
8739 callback = get_callback(&argvars[1], &partial);
8740 if (callback == NULL)
8741 return;
8742
8743 free_callback(buf->b_prompt_interrupt, buf->b_prompt_int_partial);
8744 if (partial == NULL)
8745 buf->b_prompt_interrupt = vim_strsave(callback);
8746 else
8747 /* pointer into the partial */
8748 buf->b_prompt_interrupt = callback;
8749 buf->b_prompt_int_partial = partial;
8750}
8751
8752/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02008753 * "prompt_setprompt({buffer}, {text})" function
8754 */
8755 static void
8756f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
8757{
8758 buf_T *buf;
8759 char_u *text;
8760
8761 if (check_secure())
8762 return;
8763 buf = get_buf_tv(&argvars[0], FALSE);
8764 if (buf == NULL)
8765 return;
8766
8767 text = get_tv_string(&argvars[1]);
8768 vim_free(buf->b_prompt_text);
8769 buf->b_prompt_text = vim_strsave(text);
8770}
8771#endif
8772
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008773/*
8774 * "pumvisible()" function
8775 */
8776 static void
8777f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8778{
8779#ifdef FEAT_INS_EXPAND
8780 if (pum_visible())
8781 rettv->vval.v_number = 1;
8782#endif
8783}
8784
8785#ifdef FEAT_PYTHON3
8786/*
8787 * "py3eval()" function
8788 */
8789 static void
8790f_py3eval(typval_T *argvars, typval_T *rettv)
8791{
8792 char_u *str;
8793 char_u buf[NUMBUFLEN];
8794
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008795 if (p_pyx == 0)
8796 p_pyx = 3;
8797
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008798 str = get_tv_string_buf(&argvars[0], buf);
8799 do_py3eval(str, rettv);
8800}
8801#endif
8802
8803#ifdef FEAT_PYTHON
8804/*
8805 * "pyeval()" function
8806 */
8807 static void
8808f_pyeval(typval_T *argvars, typval_T *rettv)
8809{
8810 char_u *str;
8811 char_u buf[NUMBUFLEN];
8812
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008813 if (p_pyx == 0)
8814 p_pyx = 2;
8815
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008816 str = get_tv_string_buf(&argvars[0], buf);
8817 do_pyeval(str, rettv);
8818}
8819#endif
8820
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008821#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8822/*
8823 * "pyxeval()" function
8824 */
8825 static void
8826f_pyxeval(typval_T *argvars, typval_T *rettv)
8827{
8828# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8829 init_pyxversion();
8830 if (p_pyx == 2)
8831 f_pyeval(argvars, rettv);
8832 else
8833 f_py3eval(argvars, rettv);
8834# elif defined(FEAT_PYTHON)
8835 f_pyeval(argvars, rettv);
8836# elif defined(FEAT_PYTHON3)
8837 f_py3eval(argvars, rettv);
8838# endif
8839}
8840#endif
8841
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008842/*
8843 * "range()" function
8844 */
8845 static void
8846f_range(typval_T *argvars, typval_T *rettv)
8847{
8848 varnumber_T start;
8849 varnumber_T end;
8850 varnumber_T stride = 1;
8851 varnumber_T i;
8852 int error = FALSE;
8853
8854 start = get_tv_number_chk(&argvars[0], &error);
8855 if (argvars[1].v_type == VAR_UNKNOWN)
8856 {
8857 end = start - 1;
8858 start = 0;
8859 }
8860 else
8861 {
8862 end = get_tv_number_chk(&argvars[1], &error);
8863 if (argvars[2].v_type != VAR_UNKNOWN)
8864 stride = get_tv_number_chk(&argvars[2], &error);
8865 }
8866
8867 if (error)
8868 return; /* type error; errmsg already given */
8869 if (stride == 0)
8870 EMSG(_("E726: Stride is zero"));
8871 else if (stride > 0 ? end + 1 < start : end - 1 > start)
8872 EMSG(_("E727: Start past end"));
8873 else
8874 {
8875 if (rettv_list_alloc(rettv) == OK)
8876 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8877 if (list_append_number(rettv->vval.v_list,
8878 (varnumber_T)i) == FAIL)
8879 break;
8880 }
8881}
8882
8883/*
8884 * "readfile()" function
8885 */
8886 static void
8887f_readfile(typval_T *argvars, typval_T *rettv)
8888{
8889 int binary = FALSE;
8890 int failed = FALSE;
8891 char_u *fname;
8892 FILE *fd;
8893 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
8894 int io_size = sizeof(buf);
8895 int readlen; /* size of last fread() */
8896 char_u *prev = NULL; /* previously read bytes, if any */
8897 long prevlen = 0; /* length of data in prev */
8898 long prevsize = 0; /* size of prev buffer */
8899 long maxline = MAXLNUM;
8900 long cnt = 0;
8901 char_u *p; /* position in buf */
8902 char_u *start; /* start of current line */
8903
8904 if (argvars[1].v_type != VAR_UNKNOWN)
8905 {
8906 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
8907 binary = TRUE;
8908 if (argvars[2].v_type != VAR_UNKNOWN)
8909 maxline = (long)get_tv_number(&argvars[2]);
8910 }
8911
8912 if (rettv_list_alloc(rettv) == FAIL)
8913 return;
8914
8915 /* Always open the file in binary mode, library functions have a mind of
8916 * their own about CR-LF conversion. */
8917 fname = get_tv_string(&argvars[0]);
8918 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
8919 {
8920 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
8921 return;
8922 }
8923
8924 while (cnt < maxline || maxline < 0)
8925 {
8926 readlen = (int)fread(buf, 1, io_size, fd);
8927
8928 /* This for loop processes what was read, but is also entered at end
8929 * of file so that either:
8930 * - an incomplete line gets written
8931 * - a "binary" file gets an empty line at the end if it ends in a
8932 * newline. */
8933 for (p = buf, start = buf;
8934 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
8935 ++p)
8936 {
8937 if (*p == '\n' || readlen <= 0)
8938 {
8939 listitem_T *li;
8940 char_u *s = NULL;
8941 long_u len = p - start;
8942
8943 /* Finished a line. Remove CRs before NL. */
8944 if (readlen > 0 && !binary)
8945 {
8946 while (len > 0 && start[len - 1] == '\r')
8947 --len;
8948 /* removal may cross back to the "prev" string */
8949 if (len == 0)
8950 while (prevlen > 0 && prev[prevlen - 1] == '\r')
8951 --prevlen;
8952 }
8953 if (prevlen == 0)
8954 s = vim_strnsave(start, (int)len);
8955 else
8956 {
8957 /* Change "prev" buffer to be the right size. This way
8958 * the bytes are only copied once, and very long lines are
8959 * allocated only once. */
8960 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
8961 {
8962 mch_memmove(s + prevlen, start, len);
8963 s[prevlen + len] = NUL;
8964 prev = NULL; /* the list will own the string */
8965 prevlen = prevsize = 0;
8966 }
8967 }
8968 if (s == NULL)
8969 {
8970 do_outofmem_msg((long_u) prevlen + len + 1);
8971 failed = TRUE;
8972 break;
8973 }
8974
8975 if ((li = listitem_alloc()) == NULL)
8976 {
8977 vim_free(s);
8978 failed = TRUE;
8979 break;
8980 }
8981 li->li_tv.v_type = VAR_STRING;
8982 li->li_tv.v_lock = 0;
8983 li->li_tv.vval.v_string = s;
8984 list_append(rettv->vval.v_list, li);
8985
8986 start = p + 1; /* step over newline */
8987 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
8988 break;
8989 }
8990 else if (*p == NUL)
8991 *p = '\n';
8992#ifdef FEAT_MBYTE
8993 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
8994 * when finding the BF and check the previous two bytes. */
8995 else if (*p == 0xbf && enc_utf8 && !binary)
8996 {
8997 /* Find the two bytes before the 0xbf. If p is at buf, or buf
8998 * + 1, these may be in the "prev" string. */
8999 char_u back1 = p >= buf + 1 ? p[-1]
9000 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
9001 char_u back2 = p >= buf + 2 ? p[-2]
9002 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
9003 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
9004
9005 if (back2 == 0xef && back1 == 0xbb)
9006 {
9007 char_u *dest = p - 2;
9008
9009 /* Usually a BOM is at the beginning of a file, and so at
9010 * the beginning of a line; then we can just step over it.
9011 */
9012 if (start == dest)
9013 start = p + 1;
9014 else
9015 {
9016 /* have to shuffle buf to close gap */
9017 int adjust_prevlen = 0;
9018
9019 if (dest < buf)
9020 {
9021 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
9022 dest = buf;
9023 }
9024 if (readlen > p - buf + 1)
9025 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
9026 readlen -= 3 - adjust_prevlen;
9027 prevlen -= adjust_prevlen;
9028 p = dest - 1;
9029 }
9030 }
9031 }
9032#endif
9033 } /* for */
9034
9035 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
9036 break;
9037 if (start < p)
9038 {
9039 /* There's part of a line in buf, store it in "prev". */
9040 if (p - start + prevlen >= prevsize)
9041 {
9042 /* need bigger "prev" buffer */
9043 char_u *newprev;
9044
9045 /* A common use case is ordinary text files and "prev" gets a
9046 * fragment of a line, so the first allocation is made
9047 * small, to avoid repeatedly 'allocing' large and
9048 * 'reallocing' small. */
9049 if (prevsize == 0)
9050 prevsize = (long)(p - start);
9051 else
9052 {
9053 long grow50pc = (prevsize * 3) / 2;
9054 long growmin = (long)((p - start) * 2 + prevlen);
9055 prevsize = grow50pc > growmin ? grow50pc : growmin;
9056 }
9057 newprev = prev == NULL ? alloc(prevsize)
9058 : vim_realloc(prev, prevsize);
9059 if (newprev == NULL)
9060 {
9061 do_outofmem_msg((long_u)prevsize);
9062 failed = TRUE;
9063 break;
9064 }
9065 prev = newprev;
9066 }
9067 /* Add the line part to end of "prev". */
9068 mch_memmove(prev + prevlen, start, p - start);
9069 prevlen += (long)(p - start);
9070 }
9071 } /* while */
9072
9073 /*
9074 * For a negative line count use only the lines at the end of the file,
9075 * free the rest.
9076 */
9077 if (!failed && maxline < 0)
9078 while (cnt > -maxline)
9079 {
9080 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
9081 --cnt;
9082 }
9083
9084 if (failed)
9085 {
9086 list_free(rettv->vval.v_list);
9087 /* readfile doc says an empty list is returned on error */
9088 rettv->vval.v_list = list_alloc();
9089 }
9090
9091 vim_free(prev);
9092 fclose(fd);
9093}
9094
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009095 static void
9096return_register(int regname, typval_T *rettv)
9097{
9098 char_u buf[2] = {0, 0};
9099
9100 buf[0] = (char_u)regname;
9101 rettv->v_type = VAR_STRING;
9102 rettv->vval.v_string = vim_strsave(buf);
9103}
9104
9105/*
9106 * "reg_executing()" function
9107 */
9108 static void
9109f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9110{
9111 return_register(reg_executing, rettv);
9112}
9113
9114/*
9115 * "reg_recording()" function
9116 */
9117 static void
9118f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9119{
9120 return_register(reg_recording, rettv);
9121}
9122
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009123#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009124/*
9125 * Convert a List to proftime_T.
9126 * Return FAIL when there is something wrong.
9127 */
9128 static int
9129list2proftime(typval_T *arg, proftime_T *tm)
9130{
9131 long n1, n2;
9132 int error = FALSE;
9133
9134 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9135 || arg->vval.v_list->lv_len != 2)
9136 return FAIL;
9137 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9138 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
9139# ifdef WIN3264
9140 tm->HighPart = n1;
9141 tm->LowPart = n2;
9142# else
9143 tm->tv_sec = n1;
9144 tm->tv_usec = n2;
9145# endif
9146 return error ? FAIL : OK;
9147}
9148#endif /* FEAT_RELTIME */
9149
9150/*
9151 * "reltime()" function
9152 */
9153 static void
9154f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9155{
9156#ifdef FEAT_RELTIME
9157 proftime_T res;
9158 proftime_T start;
9159
9160 if (argvars[0].v_type == VAR_UNKNOWN)
9161 {
9162 /* No arguments: get current time. */
9163 profile_start(&res);
9164 }
9165 else if (argvars[1].v_type == VAR_UNKNOWN)
9166 {
9167 if (list2proftime(&argvars[0], &res) == FAIL)
9168 return;
9169 profile_end(&res);
9170 }
9171 else
9172 {
9173 /* Two arguments: compute the difference. */
9174 if (list2proftime(&argvars[0], &start) == FAIL
9175 || list2proftime(&argvars[1], &res) == FAIL)
9176 return;
9177 profile_sub(&res, &start);
9178 }
9179
9180 if (rettv_list_alloc(rettv) == OK)
9181 {
9182 long n1, n2;
9183
9184# ifdef WIN3264
9185 n1 = res.HighPart;
9186 n2 = res.LowPart;
9187# else
9188 n1 = res.tv_sec;
9189 n2 = res.tv_usec;
9190# endif
9191 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9192 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9193 }
9194#endif
9195}
9196
9197#ifdef FEAT_FLOAT
9198/*
9199 * "reltimefloat()" function
9200 */
9201 static void
9202f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9203{
9204# ifdef FEAT_RELTIME
9205 proftime_T tm;
9206# endif
9207
9208 rettv->v_type = VAR_FLOAT;
9209 rettv->vval.v_float = 0;
9210# ifdef FEAT_RELTIME
9211 if (list2proftime(&argvars[0], &tm) == OK)
9212 rettv->vval.v_float = profile_float(&tm);
9213# endif
9214}
9215#endif
9216
9217/*
9218 * "reltimestr()" function
9219 */
9220 static void
9221f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9222{
9223#ifdef FEAT_RELTIME
9224 proftime_T tm;
9225#endif
9226
9227 rettv->v_type = VAR_STRING;
9228 rettv->vval.v_string = NULL;
9229#ifdef FEAT_RELTIME
9230 if (list2proftime(&argvars[0], &tm) == OK)
9231 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9232#endif
9233}
9234
9235#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009236 static void
9237make_connection(void)
9238{
9239 if (X_DISPLAY == NULL
9240# ifdef FEAT_GUI
9241 && !gui.in_use
9242# endif
9243 )
9244 {
9245 x_force_connect = TRUE;
9246 setup_term_clip();
9247 x_force_connect = FALSE;
9248 }
9249}
9250
9251 static int
9252check_connection(void)
9253{
9254 make_connection();
9255 if (X_DISPLAY == NULL)
9256 {
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009257 EMSG(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009258 return FAIL;
9259 }
9260 return OK;
9261}
9262#endif
9263
9264#ifdef FEAT_CLIENTSERVER
9265 static void
9266remote_common(typval_T *argvars, typval_T *rettv, int expr)
9267{
9268 char_u *server_name;
9269 char_u *keys;
9270 char_u *r = NULL;
9271 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009272 int timeout = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009273# ifdef WIN32
9274 HWND w;
9275# else
9276 Window w;
9277# endif
9278
9279 if (check_restricted() || check_secure())
9280 return;
9281
9282# ifdef FEAT_X11
9283 if (check_connection() == FAIL)
9284 return;
9285# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009286 if (argvars[2].v_type != VAR_UNKNOWN
9287 && argvars[3].v_type != VAR_UNKNOWN)
9288 timeout = get_tv_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009289
9290 server_name = get_tv_string_chk(&argvars[0]);
9291 if (server_name == NULL)
9292 return; /* type error; errmsg already given */
9293 keys = get_tv_string_buf(&argvars[1], buf);
9294# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009295 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009296# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009297 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9298 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009299# endif
9300 {
9301 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009302 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009303 EMSG(r); /* sending worked but evaluation failed */
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009304 vim_free(r);
9305 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009306 else
9307 EMSG2(_("E241: Unable to send to %s"), server_name);
9308 return;
9309 }
9310
9311 rettv->vval.v_string = r;
9312
9313 if (argvars[2].v_type != VAR_UNKNOWN)
9314 {
9315 dictitem_T v;
9316 char_u str[30];
9317 char_u *idvar;
9318
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009319 idvar = get_tv_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009320 if (idvar != NULL && *idvar != NUL)
9321 {
9322 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9323 v.di_tv.v_type = VAR_STRING;
9324 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009325 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009326 vim_free(v.di_tv.vval.v_string);
9327 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009328 }
9329}
9330#endif
9331
9332/*
9333 * "remote_expr()" function
9334 */
9335 static void
9336f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9337{
9338 rettv->v_type = VAR_STRING;
9339 rettv->vval.v_string = NULL;
9340#ifdef FEAT_CLIENTSERVER
9341 remote_common(argvars, rettv, TRUE);
9342#endif
9343}
9344
9345/*
9346 * "remote_foreground()" function
9347 */
9348 static void
9349f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9350{
9351#ifdef FEAT_CLIENTSERVER
9352# ifdef WIN32
9353 /* On Win32 it's done in this application. */
9354 {
9355 char_u *server_name = get_tv_string_chk(&argvars[0]);
9356
9357 if (server_name != NULL)
9358 serverForeground(server_name);
9359 }
9360# else
9361 /* Send a foreground() expression to the server. */
9362 argvars[1].v_type = VAR_STRING;
9363 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9364 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009365 rettv->v_type = VAR_STRING;
9366 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009367 remote_common(argvars, rettv, TRUE);
9368 vim_free(argvars[1].vval.v_string);
9369# endif
9370#endif
9371}
9372
9373 static void
9374f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
9375{
9376#ifdef FEAT_CLIENTSERVER
9377 dictitem_T v;
9378 char_u *s = NULL;
9379# ifdef WIN32
9380 long_u n = 0;
9381# endif
9382 char_u *serverid;
9383
9384 if (check_restricted() || check_secure())
9385 {
9386 rettv->vval.v_number = -1;
9387 return;
9388 }
9389 serverid = get_tv_string_chk(&argvars[0]);
9390 if (serverid == NULL)
9391 {
9392 rettv->vval.v_number = -1;
9393 return; /* type error; errmsg already given */
9394 }
9395# ifdef WIN32
9396 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
9397 if (n == 0)
9398 rettv->vval.v_number = -1;
9399 else
9400 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009401 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009402 rettv->vval.v_number = (s != NULL);
9403 }
9404# else
9405 if (check_connection() == FAIL)
9406 return;
9407
9408 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
9409 serverStrToWin(serverid), &s);
9410# endif
9411
9412 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
9413 {
9414 char_u *retvar;
9415
9416 v.di_tv.v_type = VAR_STRING;
9417 v.di_tv.vval.v_string = vim_strsave(s);
9418 retvar = get_tv_string_chk(&argvars[1]);
9419 if (retvar != NULL)
9420 set_var(retvar, &v.di_tv, FALSE);
9421 vim_free(v.di_tv.vval.v_string);
9422 }
9423#else
9424 rettv->vval.v_number = -1;
9425#endif
9426}
9427
9428 static void
9429f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
9430{
9431 char_u *r = NULL;
9432
9433#ifdef FEAT_CLIENTSERVER
9434 char_u *serverid = get_tv_string_chk(&argvars[0]);
9435
9436 if (serverid != NULL && !check_restricted() && !check_secure())
9437 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009438 int timeout = 0;
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009439# ifdef WIN32
9440 /* The server's HWND is encoded in the 'id' parameter */
9441 long_u n = 0;
9442# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009443
9444 if (argvars[1].v_type != VAR_UNKNOWN)
9445 timeout = get_tv_number(&argvars[1]);
9446
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009447# ifdef WIN32
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009448 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
9449 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009450 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009451 if (r == NULL)
9452# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009453 if (check_connection() == FAIL
9454 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
9455 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009456# endif
9457 EMSG(_("E277: Unable to read a server reply"));
9458 }
9459#endif
9460 rettv->v_type = VAR_STRING;
9461 rettv->vval.v_string = r;
9462}
9463
9464/*
9465 * "remote_send()" function
9466 */
9467 static void
9468f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9469{
9470 rettv->v_type = VAR_STRING;
9471 rettv->vval.v_string = NULL;
9472#ifdef FEAT_CLIENTSERVER
9473 remote_common(argvars, rettv, FALSE);
9474#endif
9475}
9476
9477/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009478 * "remote_startserver()" function
9479 */
9480 static void
9481f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9482{
9483#ifdef FEAT_CLIENTSERVER
9484 char_u *server = get_tv_string_chk(&argvars[0]);
9485
9486 if (server == NULL)
9487 return; /* type error; errmsg already given */
9488 if (serverName != NULL)
9489 EMSG(_("E941: already started a server"));
9490 else
9491 {
9492# ifdef FEAT_X11
9493 if (check_connection() == OK)
9494 serverRegisterName(X_DISPLAY, server);
9495# else
9496 serverSetName(server);
9497# endif
9498 }
9499#else
9500 EMSG(_("E942: +clientserver feature not available"));
9501#endif
9502}
9503
9504/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009505 * "remove()" function
9506 */
9507 static void
9508f_remove(typval_T *argvars, typval_T *rettv)
9509{
9510 list_T *l;
9511 listitem_T *item, *item2;
9512 listitem_T *li;
9513 long idx;
9514 long end;
9515 char_u *key;
9516 dict_T *d;
9517 dictitem_T *di;
9518 char_u *arg_errmsg = (char_u *)N_("remove() argument");
9519
9520 if (argvars[0].v_type == VAR_DICT)
9521 {
9522 if (argvars[2].v_type != VAR_UNKNOWN)
9523 EMSG2(_(e_toomanyarg), "remove()");
9524 else if ((d = argvars[0].vval.v_dict) != NULL
9525 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
9526 {
9527 key = get_tv_string_chk(&argvars[1]);
9528 if (key != NULL)
9529 {
9530 di = dict_find(d, key, -1);
9531 if (di == NULL)
9532 EMSG2(_(e_dictkey), key);
9533 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
9534 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
9535 {
9536 *rettv = di->di_tv;
9537 init_tv(&di->di_tv);
9538 dictitem_remove(d, di);
9539 }
9540 }
9541 }
9542 }
9543 else if (argvars[0].v_type != VAR_LIST)
9544 EMSG2(_(e_listdictarg), "remove()");
9545 else if ((l = argvars[0].vval.v_list) != NULL
9546 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
9547 {
9548 int error = FALSE;
9549
9550 idx = (long)get_tv_number_chk(&argvars[1], &error);
9551 if (error)
9552 ; /* type error: do nothing, errmsg already given */
9553 else if ((item = list_find(l, idx)) == NULL)
9554 EMSGN(_(e_listidx), idx);
9555 else
9556 {
9557 if (argvars[2].v_type == VAR_UNKNOWN)
9558 {
9559 /* Remove one item, return its value. */
9560 vimlist_remove(l, item, item);
9561 *rettv = item->li_tv;
9562 vim_free(item);
9563 }
9564 else
9565 {
9566 /* Remove range of items, return list with values. */
9567 end = (long)get_tv_number_chk(&argvars[2], &error);
9568 if (error)
9569 ; /* type error: do nothing */
9570 else if ((item2 = list_find(l, end)) == NULL)
9571 EMSGN(_(e_listidx), end);
9572 else
9573 {
9574 int cnt = 0;
9575
9576 for (li = item; li != NULL; li = li->li_next)
9577 {
9578 ++cnt;
9579 if (li == item2)
9580 break;
9581 }
9582 if (li == NULL) /* didn't find "item2" after "item" */
9583 EMSG(_(e_invrange));
9584 else
9585 {
9586 vimlist_remove(l, item, item2);
9587 if (rettv_list_alloc(rettv) == OK)
9588 {
9589 l = rettv->vval.v_list;
9590 l->lv_first = item;
9591 l->lv_last = item2;
9592 item->li_prev = NULL;
9593 item2->li_next = NULL;
9594 l->lv_len = cnt;
9595 }
9596 }
9597 }
9598 }
9599 }
9600 }
9601}
9602
9603/*
9604 * "rename({from}, {to})" function
9605 */
9606 static void
9607f_rename(typval_T *argvars, typval_T *rettv)
9608{
9609 char_u buf[NUMBUFLEN];
9610
9611 if (check_restricted() || check_secure())
9612 rettv->vval.v_number = -1;
9613 else
9614 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
9615 get_tv_string_buf(&argvars[1], buf));
9616}
9617
9618/*
9619 * "repeat()" function
9620 */
9621 static void
9622f_repeat(typval_T *argvars, typval_T *rettv)
9623{
9624 char_u *p;
9625 int n;
9626 int slen;
9627 int len;
9628 char_u *r;
9629 int i;
9630
9631 n = (int)get_tv_number(&argvars[1]);
9632 if (argvars[0].v_type == VAR_LIST)
9633 {
9634 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9635 while (n-- > 0)
9636 if (list_extend(rettv->vval.v_list,
9637 argvars[0].vval.v_list, NULL) == FAIL)
9638 break;
9639 }
9640 else
9641 {
9642 p = get_tv_string(&argvars[0]);
9643 rettv->v_type = VAR_STRING;
9644 rettv->vval.v_string = NULL;
9645
9646 slen = (int)STRLEN(p);
9647 len = slen * n;
9648 if (len <= 0)
9649 return;
9650
9651 r = alloc(len + 1);
9652 if (r != NULL)
9653 {
9654 for (i = 0; i < n; i++)
9655 mch_memmove(r + i * slen, p, (size_t)slen);
9656 r[len] = NUL;
9657 }
9658
9659 rettv->vval.v_string = r;
9660 }
9661}
9662
9663/*
9664 * "resolve()" function
9665 */
9666 static void
9667f_resolve(typval_T *argvars, typval_T *rettv)
9668{
9669 char_u *p;
9670#ifdef HAVE_READLINK
9671 char_u *buf = NULL;
9672#endif
9673
9674 p = get_tv_string(&argvars[0]);
9675#ifdef FEAT_SHORTCUT
9676 {
9677 char_u *v = NULL;
9678
9679 v = mch_resolve_shortcut(p);
9680 if (v != NULL)
9681 rettv->vval.v_string = v;
9682 else
9683 rettv->vval.v_string = vim_strsave(p);
9684 }
9685#else
9686# ifdef HAVE_READLINK
9687 {
9688 char_u *cpy;
9689 int len;
9690 char_u *remain = NULL;
9691 char_u *q;
9692 int is_relative_to_current = FALSE;
9693 int has_trailing_pathsep = FALSE;
9694 int limit = 100;
9695
9696 p = vim_strsave(p);
9697
9698 if (p[0] == '.' && (vim_ispathsep(p[1])
9699 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9700 is_relative_to_current = TRUE;
9701
9702 len = STRLEN(p);
9703 if (len > 0 && after_pathsep(p, p + len))
9704 {
9705 has_trailing_pathsep = TRUE;
9706 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9707 }
9708
9709 q = getnextcomp(p);
9710 if (*q != NUL)
9711 {
9712 /* Separate the first path component in "p", and keep the
9713 * remainder (beginning with the path separator). */
9714 remain = vim_strsave(q - 1);
9715 q[-1] = NUL;
9716 }
9717
9718 buf = alloc(MAXPATHL + 1);
9719 if (buf == NULL)
9720 goto fail;
9721
9722 for (;;)
9723 {
9724 for (;;)
9725 {
9726 len = readlink((char *)p, (char *)buf, MAXPATHL);
9727 if (len <= 0)
9728 break;
9729 buf[len] = NUL;
9730
9731 if (limit-- == 0)
9732 {
9733 vim_free(p);
9734 vim_free(remain);
9735 EMSG(_("E655: Too many symbolic links (cycle?)"));
9736 rettv->vval.v_string = NULL;
9737 goto fail;
9738 }
9739
9740 /* Ensure that the result will have a trailing path separator
9741 * if the argument has one. */
9742 if (remain == NULL && has_trailing_pathsep)
9743 add_pathsep(buf);
9744
9745 /* Separate the first path component in the link value and
9746 * concatenate the remainders. */
9747 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9748 if (*q != NUL)
9749 {
9750 if (remain == NULL)
9751 remain = vim_strsave(q - 1);
9752 else
9753 {
9754 cpy = concat_str(q - 1, remain);
9755 if (cpy != NULL)
9756 {
9757 vim_free(remain);
9758 remain = cpy;
9759 }
9760 }
9761 q[-1] = NUL;
9762 }
9763
9764 q = gettail(p);
9765 if (q > p && *q == NUL)
9766 {
9767 /* Ignore trailing path separator. */
9768 q[-1] = NUL;
9769 q = gettail(p);
9770 }
9771 if (q > p && !mch_isFullName(buf))
9772 {
9773 /* symlink is relative to directory of argument */
9774 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
9775 if (cpy != NULL)
9776 {
9777 STRCPY(cpy, p);
9778 STRCPY(gettail(cpy), buf);
9779 vim_free(p);
9780 p = cpy;
9781 }
9782 }
9783 else
9784 {
9785 vim_free(p);
9786 p = vim_strsave(buf);
9787 }
9788 }
9789
9790 if (remain == NULL)
9791 break;
9792
9793 /* Append the first path component of "remain" to "p". */
9794 q = getnextcomp(remain + 1);
9795 len = q - remain - (*q != NUL);
9796 cpy = vim_strnsave(p, STRLEN(p) + len);
9797 if (cpy != NULL)
9798 {
9799 STRNCAT(cpy, remain, len);
9800 vim_free(p);
9801 p = cpy;
9802 }
9803 /* Shorten "remain". */
9804 if (*q != NUL)
9805 STRMOVE(remain, q - 1);
9806 else
Bram Moolenaard23a8232018-02-10 18:45:26 +01009807 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009808 }
9809
9810 /* If the result is a relative path name, make it explicitly relative to
9811 * the current directory if and only if the argument had this form. */
9812 if (!vim_ispathsep(*p))
9813 {
9814 if (is_relative_to_current
9815 && *p != NUL
9816 && !(p[0] == '.'
9817 && (p[1] == NUL
9818 || vim_ispathsep(p[1])
9819 || (p[1] == '.'
9820 && (p[2] == NUL
9821 || vim_ispathsep(p[2]))))))
9822 {
9823 /* Prepend "./". */
9824 cpy = concat_str((char_u *)"./", p);
9825 if (cpy != NULL)
9826 {
9827 vim_free(p);
9828 p = cpy;
9829 }
9830 }
9831 else if (!is_relative_to_current)
9832 {
9833 /* Strip leading "./". */
9834 q = p;
9835 while (q[0] == '.' && vim_ispathsep(q[1]))
9836 q += 2;
9837 if (q > p)
9838 STRMOVE(p, p + 2);
9839 }
9840 }
9841
9842 /* Ensure that the result will have no trailing path separator
9843 * if the argument had none. But keep "/" or "//". */
9844 if (!has_trailing_pathsep)
9845 {
9846 q = p + STRLEN(p);
9847 if (after_pathsep(p, q))
9848 *gettail_sep(p) = NUL;
9849 }
9850
9851 rettv->vval.v_string = p;
9852 }
9853# else
9854 rettv->vval.v_string = vim_strsave(p);
9855# endif
9856#endif
9857
9858 simplify_filename(rettv->vval.v_string);
9859
9860#ifdef HAVE_READLINK
9861fail:
9862 vim_free(buf);
9863#endif
9864 rettv->v_type = VAR_STRING;
9865}
9866
9867/*
9868 * "reverse({list})" function
9869 */
9870 static void
9871f_reverse(typval_T *argvars, typval_T *rettv)
9872{
9873 list_T *l;
9874 listitem_T *li, *ni;
9875
9876 if (argvars[0].v_type != VAR_LIST)
9877 EMSG2(_(e_listarg), "reverse()");
9878 else if ((l = argvars[0].vval.v_list) != NULL
9879 && !tv_check_lock(l->lv_lock,
9880 (char_u *)N_("reverse() argument"), TRUE))
9881 {
9882 li = l->lv_last;
9883 l->lv_first = l->lv_last = NULL;
9884 l->lv_len = 0;
9885 while (li != NULL)
9886 {
9887 ni = li->li_prev;
9888 list_append(l, li);
9889 li = ni;
9890 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009891 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009892 l->lv_idx = l->lv_len - l->lv_idx - 1;
9893 }
9894}
9895
9896#define SP_NOMOVE 0x01 /* don't move cursor */
9897#define SP_REPEAT 0x02 /* repeat to find outer pair */
9898#define SP_RETCOUNT 0x04 /* return matchcount */
9899#define SP_SETPCMARK 0x08 /* set previous context mark */
9900#define SP_START 0x10 /* accept match at start position */
9901#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
9902#define SP_END 0x40 /* leave cursor at end of match */
9903#define SP_COLUMN 0x80 /* start at cursor column */
9904
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009905/*
9906 * Get flags for a search function.
9907 * Possibly sets "p_ws".
9908 * Returns BACKWARD, FORWARD or zero (for an error).
9909 */
9910 static int
9911get_search_arg(typval_T *varp, int *flagsp)
9912{
9913 int dir = FORWARD;
9914 char_u *flags;
9915 char_u nbuf[NUMBUFLEN];
9916 int mask;
9917
9918 if (varp->v_type != VAR_UNKNOWN)
9919 {
9920 flags = get_tv_string_buf_chk(varp, nbuf);
9921 if (flags == NULL)
9922 return 0; /* type error; errmsg already given */
9923 while (*flags != NUL)
9924 {
9925 switch (*flags)
9926 {
9927 case 'b': dir = BACKWARD; break;
9928 case 'w': p_ws = TRUE; break;
9929 case 'W': p_ws = FALSE; break;
9930 default: mask = 0;
9931 if (flagsp != NULL)
9932 switch (*flags)
9933 {
9934 case 'c': mask = SP_START; break;
9935 case 'e': mask = SP_END; break;
9936 case 'm': mask = SP_RETCOUNT; break;
9937 case 'n': mask = SP_NOMOVE; break;
9938 case 'p': mask = SP_SUBPAT; break;
9939 case 'r': mask = SP_REPEAT; break;
9940 case 's': mask = SP_SETPCMARK; break;
9941 case 'z': mask = SP_COLUMN; break;
9942 }
9943 if (mask == 0)
9944 {
9945 EMSG2(_(e_invarg2), flags);
9946 dir = 0;
9947 }
9948 else
9949 *flagsp |= mask;
9950 }
9951 if (dir == 0)
9952 break;
9953 ++flags;
9954 }
9955 }
9956 return dir;
9957}
9958
9959/*
9960 * Shared by search() and searchpos() functions.
9961 */
9962 static int
9963search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
9964{
9965 int flags;
9966 char_u *pat;
9967 pos_T pos;
9968 pos_T save_cursor;
9969 int save_p_ws = p_ws;
9970 int dir;
9971 int retval = 0; /* default: FAIL */
9972 long lnum_stop = 0;
9973 proftime_T tm;
9974#ifdef FEAT_RELTIME
9975 long time_limit = 0;
9976#endif
9977 int options = SEARCH_KEEP;
9978 int subpatnum;
9979
9980 pat = get_tv_string(&argvars[0]);
9981 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
9982 if (dir == 0)
9983 goto theend;
9984 flags = *flagsp;
9985 if (flags & SP_START)
9986 options |= SEARCH_START;
9987 if (flags & SP_END)
9988 options |= SEARCH_END;
9989 if (flags & SP_COLUMN)
9990 options |= SEARCH_COL;
9991
9992 /* Optional arguments: line number to stop searching and timeout. */
9993 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9994 {
9995 lnum_stop = (long)get_tv_number_chk(&argvars[2], NULL);
9996 if (lnum_stop < 0)
9997 goto theend;
9998#ifdef FEAT_RELTIME
9999 if (argvars[3].v_type != VAR_UNKNOWN)
10000 {
10001 time_limit = (long)get_tv_number_chk(&argvars[3], NULL);
10002 if (time_limit < 0)
10003 goto theend;
10004 }
10005#endif
10006 }
10007
10008#ifdef FEAT_RELTIME
10009 /* Set the time limit, if there is one. */
10010 profile_setlimit(time_limit, &tm);
10011#endif
10012
10013 /*
10014 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
10015 * Check to make sure only those flags are set.
10016 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
10017 * flags cannot be set. Check for that condition also.
10018 */
10019 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
10020 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10021 {
10022 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
10023 goto theend;
10024 }
10025
10026 pos = save_cursor = curwin->w_cursor;
10027 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010028 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010029 if (subpatnum != FAIL)
10030 {
10031 if (flags & SP_SUBPAT)
10032 retval = subpatnum;
10033 else
10034 retval = pos.lnum;
10035 if (flags & SP_SETPCMARK)
10036 setpcmark();
10037 curwin->w_cursor = pos;
10038 if (match_pos != NULL)
10039 {
10040 /* Store the match cursor position */
10041 match_pos->lnum = pos.lnum;
10042 match_pos->col = pos.col + 1;
10043 }
10044 /* "/$" will put the cursor after the end of the line, may need to
10045 * correct that here */
10046 check_cursor();
10047 }
10048
10049 /* If 'n' flag is used: restore cursor position. */
10050 if (flags & SP_NOMOVE)
10051 curwin->w_cursor = save_cursor;
10052 else
10053 curwin->w_set_curswant = TRUE;
10054theend:
10055 p_ws = save_p_ws;
10056
10057 return retval;
10058}
10059
10060#ifdef FEAT_FLOAT
10061
10062/*
10063 * round() is not in C90, use ceil() or floor() instead.
10064 */
10065 float_T
10066vim_round(float_T f)
10067{
10068 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
10069}
10070
10071/*
10072 * "round({float})" function
10073 */
10074 static void
10075f_round(typval_T *argvars, typval_T *rettv)
10076{
10077 float_T f = 0.0;
10078
10079 rettv->v_type = VAR_FLOAT;
10080 if (get_float_arg(argvars, &f) == OK)
10081 rettv->vval.v_float = vim_round(f);
10082 else
10083 rettv->vval.v_float = 0.0;
10084}
10085#endif
10086
10087/*
10088 * "screenattr()" function
10089 */
10090 static void
10091f_screenattr(typval_T *argvars, typval_T *rettv)
10092{
10093 int row;
10094 int col;
10095 int c;
10096
10097 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
10098 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
10099 if (row < 0 || row >= screen_Rows
10100 || col < 0 || col >= screen_Columns)
10101 c = -1;
10102 else
10103 c = ScreenAttrs[LineOffset[row] + col];
10104 rettv->vval.v_number = c;
10105}
10106
10107/*
10108 * "screenchar()" function
10109 */
10110 static void
10111f_screenchar(typval_T *argvars, typval_T *rettv)
10112{
10113 int row;
10114 int col;
10115 int off;
10116 int c;
10117
10118 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
10119 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
10120 if (row < 0 || row >= screen_Rows
10121 || col < 0 || col >= screen_Columns)
10122 c = -1;
10123 else
10124 {
10125 off = LineOffset[row] + col;
10126#ifdef FEAT_MBYTE
10127 if (enc_utf8 && ScreenLinesUC[off] != 0)
10128 c = ScreenLinesUC[off];
10129 else
10130#endif
10131 c = ScreenLines[off];
10132 }
10133 rettv->vval.v_number = c;
10134}
10135
10136/*
10137 * "screencol()" function
10138 *
10139 * First column is 1 to be consistent with virtcol().
10140 */
10141 static void
10142f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10143{
10144 rettv->vval.v_number = screen_screencol() + 1;
10145}
10146
10147/*
10148 * "screenrow()" function
10149 */
10150 static void
10151f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10152{
10153 rettv->vval.v_number = screen_screenrow() + 1;
10154}
10155
10156/*
10157 * "search()" function
10158 */
10159 static void
10160f_search(typval_T *argvars, typval_T *rettv)
10161{
10162 int flags = 0;
10163
10164 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10165}
10166
10167/*
10168 * "searchdecl()" function
10169 */
10170 static void
10171f_searchdecl(typval_T *argvars, typval_T *rettv)
10172{
10173 int locally = 1;
10174 int thisblock = 0;
10175 int error = FALSE;
10176 char_u *name;
10177
10178 rettv->vval.v_number = 1; /* default: FAIL */
10179
10180 name = get_tv_string_chk(&argvars[0]);
10181 if (argvars[1].v_type != VAR_UNKNOWN)
10182 {
10183 locally = (int)get_tv_number_chk(&argvars[1], &error) == 0;
10184 if (!error && argvars[2].v_type != VAR_UNKNOWN)
10185 thisblock = (int)get_tv_number_chk(&argvars[2], &error) != 0;
10186 }
10187 if (!error && name != NULL)
10188 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10189 locally, thisblock, SEARCH_KEEP) == FAIL;
10190}
10191
10192/*
10193 * Used by searchpair() and searchpairpos()
10194 */
10195 static int
10196searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10197{
10198 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010199 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010200 int save_p_ws = p_ws;
10201 int dir;
10202 int flags = 0;
10203 char_u nbuf1[NUMBUFLEN];
10204 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010205 int retval = 0; /* default: FAIL */
10206 long lnum_stop = 0;
10207 long time_limit = 0;
10208
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010209 /* Get the three pattern arguments: start, middle, end. Will result in an
10210 * error if not a valid argument. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010211 spat = get_tv_string_chk(&argvars[0]);
10212 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
10213 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
10214 if (spat == NULL || mpat == NULL || epat == NULL)
10215 goto theend; /* type error */
10216
10217 /* Handle the optional fourth argument: flags */
10218 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
10219 if (dir == 0)
10220 goto theend;
10221
10222 /* Don't accept SP_END or SP_SUBPAT.
10223 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
10224 */
10225 if ((flags & (SP_END | SP_SUBPAT)) != 0
10226 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10227 {
10228 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
10229 goto theend;
10230 }
10231
10232 /* Using 'r' implies 'W', otherwise it doesn't work. */
10233 if (flags & SP_REPEAT)
10234 p_ws = FALSE;
10235
10236 /* Optional fifth argument: skip expression */
10237 if (argvars[3].v_type == VAR_UNKNOWN
10238 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010010239 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010240 else
10241 {
Bram Moolenaar48570482017-10-30 21:48:41 +010010242 skip = &argvars[4];
10243 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
10244 && skip->v_type != VAR_STRING)
10245 {
10246 /* Type error */
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010247 EMSG2(_(e_invarg2), get_tv_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010010248 goto theend;
10249 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010250 if (argvars[5].v_type != VAR_UNKNOWN)
10251 {
10252 lnum_stop = (long)get_tv_number_chk(&argvars[5], NULL);
10253 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010254 {
10255 EMSG2(_(e_invarg2), get_tv_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010256 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010257 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010258#ifdef FEAT_RELTIME
10259 if (argvars[6].v_type != VAR_UNKNOWN)
10260 {
10261 time_limit = (long)get_tv_number_chk(&argvars[6], NULL);
10262 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010263 {
10264 EMSG2(_(e_invarg2), get_tv_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010265 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010266 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010267 }
10268#endif
10269 }
10270 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010271
10272 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
10273 match_pos, lnum_stop, time_limit);
10274
10275theend:
10276 p_ws = save_p_ws;
10277
10278 return retval;
10279}
10280
10281/*
10282 * "searchpair()" function
10283 */
10284 static void
10285f_searchpair(typval_T *argvars, typval_T *rettv)
10286{
10287 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
10288}
10289
10290/*
10291 * "searchpairpos()" function
10292 */
10293 static void
10294f_searchpairpos(typval_T *argvars, typval_T *rettv)
10295{
10296 pos_T match_pos;
10297 int lnum = 0;
10298 int col = 0;
10299
10300 if (rettv_list_alloc(rettv) == FAIL)
10301 return;
10302
10303 if (searchpair_cmn(argvars, &match_pos) > 0)
10304 {
10305 lnum = match_pos.lnum;
10306 col = match_pos.col;
10307 }
10308
10309 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10310 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10311}
10312
10313/*
10314 * Search for a start/middle/end thing.
10315 * Used by searchpair(), see its documentation for the details.
10316 * Returns 0 or -1 for no match,
10317 */
10318 long
10319do_searchpair(
10320 char_u *spat, /* start pattern */
10321 char_u *mpat, /* middle pattern */
10322 char_u *epat, /* end pattern */
10323 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010010324 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010325 int flags, /* SP_SETPCMARK and other SP_ values */
10326 pos_T *match_pos,
10327 linenr_T lnum_stop, /* stop at this line if not zero */
10328 long time_limit UNUSED) /* stop after this many msec */
10329{
10330 char_u *save_cpo;
10331 char_u *pat, *pat2 = NULL, *pat3 = NULL;
10332 long retval = 0;
10333 pos_T pos;
10334 pos_T firstpos;
10335 pos_T foundpos;
10336 pos_T save_cursor;
10337 pos_T save_pos;
10338 int n;
10339 int r;
10340 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010010341 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010342 int err;
10343 int options = SEARCH_KEEP;
10344 proftime_T tm;
10345
10346 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10347 save_cpo = p_cpo;
10348 p_cpo = empty_option;
10349
10350#ifdef FEAT_RELTIME
10351 /* Set the time limit, if there is one. */
10352 profile_setlimit(time_limit, &tm);
10353#endif
10354
10355 /* Make two search patterns: start/end (pat2, for in nested pairs) and
10356 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010357 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
10358 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010359 if (pat2 == NULL || pat3 == NULL)
10360 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010361 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010362 if (*mpat == NUL)
10363 STRCPY(pat3, pat2);
10364 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010365 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010366 spat, epat, mpat);
10367 if (flags & SP_START)
10368 options |= SEARCH_START;
10369
Bram Moolenaar48570482017-10-30 21:48:41 +010010370 if (skip != NULL)
10371 {
10372 /* Empty string means to not use the skip expression. */
10373 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
10374 use_skip = skip->vval.v_string != NULL
10375 && *skip->vval.v_string != NUL;
10376 }
10377
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010378 save_cursor = curwin->w_cursor;
10379 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010380 CLEAR_POS(&firstpos);
10381 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010382 pat = pat3;
10383 for (;;)
10384 {
10385 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010386 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010387 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010388 /* didn't find it or found the first match again: FAIL */
10389 break;
10390
10391 if (firstpos.lnum == 0)
10392 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010393 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010394 {
10395 /* Found the same position again. Can happen with a pattern that
10396 * has "\zs" at the end and searching backwards. Advance one
10397 * character and try again. */
10398 if (dir == BACKWARD)
10399 decl(&pos);
10400 else
10401 incl(&pos);
10402 }
10403 foundpos = pos;
10404
10405 /* clear the start flag to avoid getting stuck here */
10406 options &= ~SEARCH_START;
10407
10408 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010010409 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010410 {
10411 save_pos = curwin->w_cursor;
10412 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010010413 err = FALSE;
10414 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010415 curwin->w_cursor = save_pos;
10416 if (err)
10417 {
10418 /* Evaluating {skip} caused an error, break here. */
10419 curwin->w_cursor = save_cursor;
10420 retval = -1;
10421 break;
10422 }
10423 if (r)
10424 continue;
10425 }
10426
10427 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
10428 {
10429 /* Found end when searching backwards or start when searching
10430 * forward: nested pair. */
10431 ++nest;
10432 pat = pat2; /* nested, don't search for middle */
10433 }
10434 else
10435 {
10436 /* Found end when searching forward or start when searching
10437 * backward: end of (nested) pair; or found middle in outer pair. */
10438 if (--nest == 1)
10439 pat = pat3; /* outer level, search for middle */
10440 }
10441
10442 if (nest == 0)
10443 {
10444 /* Found the match: return matchcount or line number. */
10445 if (flags & SP_RETCOUNT)
10446 ++retval;
10447 else
10448 retval = pos.lnum;
10449 if (flags & SP_SETPCMARK)
10450 setpcmark();
10451 curwin->w_cursor = pos;
10452 if (!(flags & SP_REPEAT))
10453 break;
10454 nest = 1; /* search for next unmatched */
10455 }
10456 }
10457
10458 if (match_pos != NULL)
10459 {
10460 /* Store the match cursor position */
10461 match_pos->lnum = curwin->w_cursor.lnum;
10462 match_pos->col = curwin->w_cursor.col + 1;
10463 }
10464
10465 /* If 'n' flag is used or search failed: restore cursor position. */
10466 if ((flags & SP_NOMOVE) || retval == 0)
10467 curwin->w_cursor = save_cursor;
10468
10469theend:
10470 vim_free(pat2);
10471 vim_free(pat3);
10472 if (p_cpo == empty_option)
10473 p_cpo = save_cpo;
10474 else
10475 /* Darn, evaluating the {skip} expression changed the value. */
10476 free_string_option(save_cpo);
10477
10478 return retval;
10479}
10480
10481/*
10482 * "searchpos()" function
10483 */
10484 static void
10485f_searchpos(typval_T *argvars, typval_T *rettv)
10486{
10487 pos_T match_pos;
10488 int lnum = 0;
10489 int col = 0;
10490 int n;
10491 int flags = 0;
10492
10493 if (rettv_list_alloc(rettv) == FAIL)
10494 return;
10495
10496 n = search_cmn(argvars, &match_pos, &flags);
10497 if (n > 0)
10498 {
10499 lnum = match_pos.lnum;
10500 col = match_pos.col;
10501 }
10502
10503 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10504 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10505 if (flags & SP_SUBPAT)
10506 list_append_number(rettv->vval.v_list, (varnumber_T)n);
10507}
10508
10509 static void
10510f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
10511{
10512#ifdef FEAT_CLIENTSERVER
10513 char_u buf[NUMBUFLEN];
10514 char_u *server = get_tv_string_chk(&argvars[0]);
10515 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
10516
10517 rettv->vval.v_number = -1;
10518 if (server == NULL || reply == NULL)
10519 return;
10520 if (check_restricted() || check_secure())
10521 return;
10522# ifdef FEAT_X11
10523 if (check_connection() == FAIL)
10524 return;
10525# endif
10526
10527 if (serverSendReply(server, reply) < 0)
10528 {
10529 EMSG(_("E258: Unable to send to client"));
10530 return;
10531 }
10532 rettv->vval.v_number = 0;
10533#else
10534 rettv->vval.v_number = -1;
10535#endif
10536}
10537
10538 static void
10539f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
10540{
10541 char_u *r = NULL;
10542
10543#ifdef FEAT_CLIENTSERVER
10544# ifdef WIN32
10545 r = serverGetVimNames();
10546# else
10547 make_connection();
10548 if (X_DISPLAY != NULL)
10549 r = serverGetVimNames(X_DISPLAY);
10550# endif
10551#endif
10552 rettv->v_type = VAR_STRING;
10553 rettv->vval.v_string = r;
10554}
10555
10556/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010557 * "setbufline()" function
10558 */
10559 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020010560f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010561{
10562 linenr_T lnum;
10563 buf_T *buf;
10564
10565 buf = get_buf_tv(&argvars[0], FALSE);
10566 if (buf == NULL)
10567 rettv->vval.v_number = 1; /* FAIL */
10568 else
10569 {
10570 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020010571 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010572 }
10573}
10574
10575/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010576 * "setbufvar()" function
10577 */
10578 static void
10579f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10580{
10581 buf_T *buf;
10582 char_u *varname, *bufvarname;
10583 typval_T *varp;
10584 char_u nbuf[NUMBUFLEN];
10585
10586 if (check_restricted() || check_secure())
10587 return;
10588 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10589 varname = get_tv_string_chk(&argvars[1]);
10590 buf = get_buf_tv(&argvars[0], FALSE);
10591 varp = &argvars[2];
10592
10593 if (buf != NULL && varname != NULL && varp != NULL)
10594 {
10595 if (*varname == '&')
10596 {
10597 long numval;
10598 char_u *strval;
10599 int error = FALSE;
10600 aco_save_T aco;
10601
10602 /* set curbuf to be our buf, temporarily */
10603 aucmd_prepbuf(&aco, buf);
10604
10605 ++varname;
10606 numval = (long)get_tv_number_chk(varp, &error);
10607 strval = get_tv_string_buf_chk(varp, nbuf);
10608 if (!error && strval != NULL)
10609 set_option_value(varname, numval, strval, OPT_LOCAL);
10610
10611 /* reset notion of buffer */
10612 aucmd_restbuf(&aco);
10613 }
10614 else
10615 {
10616 buf_T *save_curbuf = curbuf;
10617
10618 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
10619 if (bufvarname != NULL)
10620 {
10621 curbuf = buf;
10622 STRCPY(bufvarname, "b:");
10623 STRCPY(bufvarname + 2, varname);
10624 set_var(bufvarname, varp, TRUE);
10625 vim_free(bufvarname);
10626 curbuf = save_curbuf;
10627 }
10628 }
10629 }
10630}
10631
10632 static void
10633f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10634{
10635 dict_T *d;
10636 dictitem_T *di;
10637 char_u *csearch;
10638
10639 if (argvars[0].v_type != VAR_DICT)
10640 {
10641 EMSG(_(e_dictreq));
10642 return;
10643 }
10644
10645 if ((d = argvars[0].vval.v_dict) != NULL)
10646 {
10647 csearch = get_dict_string(d, (char_u *)"char", FALSE);
10648 if (csearch != NULL)
10649 {
10650#ifdef FEAT_MBYTE
10651 if (enc_utf8)
10652 {
10653 int pcc[MAX_MCO];
10654 int c = utfc_ptr2char(csearch, pcc);
10655
10656 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
10657 }
10658 else
10659#endif
10660 set_last_csearch(PTR2CHAR(csearch),
10661 csearch, MB_PTR2LEN(csearch));
10662 }
10663
10664 di = dict_find(d, (char_u *)"forward", -1);
10665 if (di != NULL)
10666 set_csearch_direction((int)get_tv_number(&di->di_tv)
10667 ? FORWARD : BACKWARD);
10668
10669 di = dict_find(d, (char_u *)"until", -1);
10670 if (di != NULL)
10671 set_csearch_until(!!get_tv_number(&di->di_tv));
10672 }
10673}
10674
10675/*
10676 * "setcmdpos()" function
10677 */
10678 static void
10679f_setcmdpos(typval_T *argvars, typval_T *rettv)
10680{
10681 int pos = (int)get_tv_number(&argvars[0]) - 1;
10682
10683 if (pos >= 0)
10684 rettv->vval.v_number = set_cmdline_pos(pos);
10685}
10686
10687/*
10688 * "setfperm({fname}, {mode})" function
10689 */
10690 static void
10691f_setfperm(typval_T *argvars, typval_T *rettv)
10692{
10693 char_u *fname;
10694 char_u modebuf[NUMBUFLEN];
10695 char_u *mode_str;
10696 int i;
10697 int mask;
10698 int mode = 0;
10699
10700 rettv->vval.v_number = 0;
10701 fname = get_tv_string_chk(&argvars[0]);
10702 if (fname == NULL)
10703 return;
10704 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
10705 if (mode_str == NULL)
10706 return;
10707 if (STRLEN(mode_str) != 9)
10708 {
10709 EMSG2(_(e_invarg2), mode_str);
10710 return;
10711 }
10712
10713 mask = 1;
10714 for (i = 8; i >= 0; --i)
10715 {
10716 if (mode_str[i] != '-')
10717 mode |= mask;
10718 mask = mask << 1;
10719 }
10720 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
10721}
10722
10723/*
10724 * "setline()" function
10725 */
10726 static void
10727f_setline(typval_T *argvars, typval_T *rettv)
10728{
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010729 linenr_T lnum = get_tv_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010730
Bram Moolenaarca851592018-06-06 21:04:07 +020010731 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010732}
10733
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010734/*
10735 * Used by "setqflist()" and "setloclist()" functions
10736 */
10737 static void
10738set_qf_ll_list(
10739 win_T *wp UNUSED,
10740 typval_T *list_arg UNUSED,
10741 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020010742 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010743 typval_T *rettv)
10744{
10745#ifdef FEAT_QUICKFIX
10746 static char *e_invact = N_("E927: Invalid action: '%s'");
10747 char_u *act;
10748 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010749 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010750#endif
10751
10752 rettv->vval.v_number = -1;
10753
10754#ifdef FEAT_QUICKFIX
10755 if (list_arg->v_type != VAR_LIST)
10756 EMSG(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010757 else if (recursive != 0)
10758 EMSG(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010759 else
10760 {
10761 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010762 dict_T *d = NULL;
10763 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010764
10765 if (action_arg->v_type == VAR_STRING)
10766 {
10767 act = get_tv_string_chk(action_arg);
10768 if (act == NULL)
10769 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020010770 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
10771 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010772 action = *act;
10773 else
10774 EMSG2(_(e_invact), act);
10775 }
10776 else if (action_arg->v_type == VAR_UNKNOWN)
10777 action = ' ';
10778 else
10779 EMSG(_(e_stringreq));
10780
Bram Moolenaard823fa92016-08-12 16:29:27 +020010781 if (action_arg->v_type != VAR_UNKNOWN
10782 && what_arg->v_type != VAR_UNKNOWN)
10783 {
10784 if (what_arg->v_type == VAR_DICT)
10785 d = what_arg->vval.v_dict;
10786 else
10787 {
10788 EMSG(_(e_dictreq));
10789 valid_dict = FALSE;
10790 }
10791 }
10792
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010793 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010794 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010795 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
10796 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010797 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010798 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010799 }
10800#endif
10801}
10802
10803/*
10804 * "setloclist()" function
10805 */
10806 static void
10807f_setloclist(typval_T *argvars, typval_T *rettv)
10808{
10809 win_T *win;
10810
10811 rettv->vval.v_number = -1;
10812
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020010813 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010814 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020010815 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010816}
10817
10818/*
10819 * "setmatches()" function
10820 */
10821 static void
10822f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10823{
10824#ifdef FEAT_SEARCH_EXTRA
10825 list_T *l;
10826 listitem_T *li;
10827 dict_T *d;
10828 list_T *s = NULL;
10829
10830 rettv->vval.v_number = -1;
10831 if (argvars[0].v_type != VAR_LIST)
10832 {
10833 EMSG(_(e_listreq));
10834 return;
10835 }
10836 if ((l = argvars[0].vval.v_list) != NULL)
10837 {
10838
10839 /* To some extent make sure that we are dealing with a list from
10840 * "getmatches()". */
10841 li = l->lv_first;
10842 while (li != NULL)
10843 {
10844 if (li->li_tv.v_type != VAR_DICT
10845 || (d = li->li_tv.vval.v_dict) == NULL)
10846 {
10847 EMSG(_(e_invarg));
10848 return;
10849 }
10850 if (!(dict_find(d, (char_u *)"group", -1) != NULL
10851 && (dict_find(d, (char_u *)"pattern", -1) != NULL
10852 || dict_find(d, (char_u *)"pos1", -1) != NULL)
10853 && dict_find(d, (char_u *)"priority", -1) != NULL
10854 && dict_find(d, (char_u *)"id", -1) != NULL))
10855 {
10856 EMSG(_(e_invarg));
10857 return;
10858 }
10859 li = li->li_next;
10860 }
10861
10862 clear_matches(curwin);
10863 li = l->lv_first;
10864 while (li != NULL)
10865 {
10866 int i = 0;
10867 char_u buf[5];
10868 dictitem_T *di;
10869 char_u *group;
10870 int priority;
10871 int id;
10872 char_u *conceal;
10873
10874 d = li->li_tv.vval.v_dict;
10875 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
10876 {
10877 if (s == NULL)
10878 {
10879 s = list_alloc();
10880 if (s == NULL)
10881 return;
10882 }
10883
10884 /* match from matchaddpos() */
10885 for (i = 1; i < 9; i++)
10886 {
10887 sprintf((char *)buf, (char *)"pos%d", i);
10888 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
10889 {
10890 if (di->di_tv.v_type != VAR_LIST)
10891 return;
10892
10893 list_append_tv(s, &di->di_tv);
10894 s->lv_refcount++;
10895 }
10896 else
10897 break;
10898 }
10899 }
10900
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010901 group = get_dict_string(d, (char_u *)"group", TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010902 priority = (int)get_dict_number(d, (char_u *)"priority");
10903 id = (int)get_dict_number(d, (char_u *)"id");
10904 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010905 ? get_dict_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010906 : NULL;
10907 if (i == 0)
10908 {
10909 match_add(curwin, group,
10910 get_dict_string(d, (char_u *)"pattern", FALSE),
10911 priority, id, NULL, conceal);
10912 }
10913 else
10914 {
10915 match_add(curwin, group, NULL, priority, id, s, conceal);
10916 list_unref(s);
10917 s = NULL;
10918 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010919 vim_free(group);
10920 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010921
10922 li = li->li_next;
10923 }
10924 rettv->vval.v_number = 0;
10925 }
10926#endif
10927}
10928
10929/*
10930 * "setpos()" function
10931 */
10932 static void
10933f_setpos(typval_T *argvars, typval_T *rettv)
10934{
10935 pos_T pos;
10936 int fnum;
10937 char_u *name;
10938 colnr_T curswant = -1;
10939
10940 rettv->vval.v_number = -1;
10941 name = get_tv_string_chk(argvars);
10942 if (name != NULL)
10943 {
10944 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
10945 {
10946 if (--pos.col < 0)
10947 pos.col = 0;
10948 if (name[0] == '.' && name[1] == NUL)
10949 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010950 /* set cursor; "fnum" is ignored */
10951 curwin->w_cursor = pos;
10952 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010953 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010954 curwin->w_curswant = curswant - 1;
10955 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010956 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010957 check_cursor();
10958 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010959 }
10960 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
10961 {
10962 /* set mark */
10963 if (setmark_pos(name[1], &pos, fnum) == OK)
10964 rettv->vval.v_number = 0;
10965 }
10966 else
10967 EMSG(_(e_invarg));
10968 }
10969 }
10970}
10971
10972/*
10973 * "setqflist()" function
10974 */
10975 static void
10976f_setqflist(typval_T *argvars, typval_T *rettv)
10977{
Bram Moolenaard823fa92016-08-12 16:29:27 +020010978 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010979}
10980
10981/*
10982 * "setreg()" function
10983 */
10984 static void
10985f_setreg(typval_T *argvars, typval_T *rettv)
10986{
10987 int regname;
10988 char_u *strregname;
10989 char_u *stropt;
10990 char_u *strval;
10991 int append;
10992 char_u yank_type;
10993 long block_len;
10994
10995 block_len = -1;
10996 yank_type = MAUTO;
10997 append = FALSE;
10998
10999 strregname = get_tv_string_chk(argvars);
11000 rettv->vval.v_number = 1; /* FAIL is default */
11001
11002 if (strregname == NULL)
11003 return; /* type error; errmsg already given */
11004 regname = *strregname;
11005 if (regname == 0 || regname == '@')
11006 regname = '"';
11007
11008 if (argvars[2].v_type != VAR_UNKNOWN)
11009 {
11010 stropt = get_tv_string_chk(&argvars[2]);
11011 if (stropt == NULL)
11012 return; /* type error */
11013 for (; *stropt != NUL; ++stropt)
11014 switch (*stropt)
11015 {
11016 case 'a': case 'A': /* append */
11017 append = TRUE;
11018 break;
11019 case 'v': case 'c': /* character-wise selection */
11020 yank_type = MCHAR;
11021 break;
11022 case 'V': case 'l': /* line-wise selection */
11023 yank_type = MLINE;
11024 break;
11025 case 'b': case Ctrl_V: /* block-wise selection */
11026 yank_type = MBLOCK;
11027 if (VIM_ISDIGIT(stropt[1]))
11028 {
11029 ++stropt;
11030 block_len = getdigits(&stropt) - 1;
11031 --stropt;
11032 }
11033 break;
11034 }
11035 }
11036
11037 if (argvars[1].v_type == VAR_LIST)
11038 {
11039 char_u **lstval;
11040 char_u **allocval;
11041 char_u buf[NUMBUFLEN];
11042 char_u **curval;
11043 char_u **curallocval;
11044 list_T *ll = argvars[1].vval.v_list;
11045 listitem_T *li;
11046 int len;
11047
11048 /* If the list is NULL handle like an empty list. */
11049 len = ll == NULL ? 0 : ll->lv_len;
11050
11051 /* First half: use for pointers to result lines; second half: use for
11052 * pointers to allocated copies. */
11053 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
11054 if (lstval == NULL)
11055 return;
11056 curval = lstval;
11057 allocval = lstval + len + 2;
11058 curallocval = allocval;
11059
11060 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
11061 li = li->li_next)
11062 {
11063 strval = get_tv_string_buf_chk(&li->li_tv, buf);
11064 if (strval == NULL)
11065 goto free_lstval;
11066 if (strval == buf)
11067 {
11068 /* Need to make a copy, next get_tv_string_buf_chk() will
11069 * overwrite the string. */
11070 strval = vim_strsave(buf);
11071 if (strval == NULL)
11072 goto free_lstval;
11073 *curallocval++ = strval;
11074 }
11075 *curval++ = strval;
11076 }
11077 *curval++ = NULL;
11078
11079 write_reg_contents_lst(regname, lstval, -1,
11080 append, yank_type, block_len);
11081free_lstval:
11082 while (curallocval > allocval)
11083 vim_free(*--curallocval);
11084 vim_free(lstval);
11085 }
11086 else
11087 {
11088 strval = get_tv_string_chk(&argvars[1]);
11089 if (strval == NULL)
11090 return;
11091 write_reg_contents_ex(regname, strval, -1,
11092 append, yank_type, block_len);
11093 }
11094 rettv->vval.v_number = 0;
11095}
11096
11097/*
11098 * "settabvar()" function
11099 */
11100 static void
11101f_settabvar(typval_T *argvars, typval_T *rettv)
11102{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011103 tabpage_T *save_curtab;
11104 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011105 char_u *varname, *tabvarname;
11106 typval_T *varp;
11107
11108 rettv->vval.v_number = 0;
11109
11110 if (check_restricted() || check_secure())
11111 return;
11112
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011113 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011114 varname = get_tv_string_chk(&argvars[1]);
11115 varp = &argvars[2];
11116
Bram Moolenaar4033c552017-09-16 20:54:51 +020011117 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011118 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011119 save_curtab = curtab;
11120 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011121
11122 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
11123 if (tabvarname != NULL)
11124 {
11125 STRCPY(tabvarname, "t:");
11126 STRCPY(tabvarname + 2, varname);
11127 set_var(tabvarname, varp, TRUE);
11128 vim_free(tabvarname);
11129 }
11130
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011131 /* Restore current tabpage */
11132 if (valid_tabpage(save_curtab))
11133 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011134 }
11135}
11136
11137/*
11138 * "settabwinvar()" function
11139 */
11140 static void
11141f_settabwinvar(typval_T *argvars, typval_T *rettv)
11142{
11143 setwinvar(argvars, rettv, 1);
11144}
11145
11146/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011147 * "settagstack()" function
11148 */
11149 static void
11150f_settagstack(typval_T *argvars, typval_T *rettv)
11151{
11152 static char *e_invact2 = N_("E962: Invalid action: '%s'");
11153 win_T *wp;
11154 dict_T *d;
11155 int action = 'r';
11156
11157 rettv->vval.v_number = -1;
11158
11159 // first argument: window number or id
11160 wp = find_win_by_nr_or_id(&argvars[0]);
11161 if (wp == NULL)
11162 return;
11163
11164 // second argument: dict with items to set in the tag stack
11165 if (argvars[1].v_type != VAR_DICT)
11166 {
11167 EMSG(_(e_dictreq));
11168 return;
11169 }
11170 d = argvars[1].vval.v_dict;
11171 if (d == NULL)
11172 return;
11173
11174 // third argument: action - 'a' for append and 'r' for replace.
11175 // default is to replace the stack.
11176 if (argvars[2].v_type == VAR_UNKNOWN)
11177 action = 'r';
11178 else if (argvars[2].v_type == VAR_STRING)
11179 {
11180 char_u *actstr;
11181 actstr = get_tv_string_chk(&argvars[2]);
11182 if (actstr == NULL)
11183 return;
11184 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
11185 action = *actstr;
11186 else
11187 {
11188 EMSG2(_(e_invact2), actstr);
11189 return;
11190 }
11191 }
11192 else
11193 {
11194 EMSG(_(e_stringreq));
11195 return;
11196 }
11197
11198 if (set_tagstack(wp, d, action) == OK)
11199 rettv->vval.v_number = 0;
11200}
11201
11202/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011203 * "setwinvar()" function
11204 */
11205 static void
11206f_setwinvar(typval_T *argvars, typval_T *rettv)
11207{
11208 setwinvar(argvars, rettv, 0);
11209}
11210
11211#ifdef FEAT_CRYPT
11212/*
11213 * "sha256({string})" function
11214 */
11215 static void
11216f_sha256(typval_T *argvars, typval_T *rettv)
11217{
11218 char_u *p;
11219
11220 p = get_tv_string(&argvars[0]);
11221 rettv->vval.v_string = vim_strsave(
11222 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
11223 rettv->v_type = VAR_STRING;
11224}
11225#endif /* FEAT_CRYPT */
11226
11227/*
11228 * "shellescape({string})" function
11229 */
11230 static void
11231f_shellescape(typval_T *argvars, typval_T *rettv)
11232{
Bram Moolenaar20615522017-06-05 18:46:26 +020011233 int do_special = non_zero_arg(&argvars[1]);
11234
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011235 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar20615522017-06-05 18:46:26 +020011236 get_tv_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011237 rettv->v_type = VAR_STRING;
11238}
11239
11240/*
11241 * shiftwidth() function
11242 */
11243 static void
11244f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
11245{
Bram Moolenaarf9514162018-11-22 03:08:29 +010011246 rettv->vval.v_number = 0;
11247
11248 if (argvars[0].v_type != VAR_UNKNOWN)
11249 {
11250 long col;
11251
11252 col = (long)get_tv_number_chk(argvars, NULL);
11253 if (col < 0)
11254 return; // type error; errmsg already given
11255#ifdef FEAT_VARTABS
11256 rettv->vval.v_number = get_sw_value_col(curbuf, col);
11257 return;
11258#endif
11259 }
11260
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011261 rettv->vval.v_number = get_sw_value(curbuf);
11262}
11263
11264/*
11265 * "simplify()" function
11266 */
11267 static void
11268f_simplify(typval_T *argvars, typval_T *rettv)
11269{
11270 char_u *p;
11271
11272 p = get_tv_string(&argvars[0]);
11273 rettv->vval.v_string = vim_strsave(p);
11274 simplify_filename(rettv->vval.v_string); /* simplify in place */
11275 rettv->v_type = VAR_STRING;
11276}
11277
11278#ifdef FEAT_FLOAT
11279/*
11280 * "sin()" function
11281 */
11282 static void
11283f_sin(typval_T *argvars, typval_T *rettv)
11284{
11285 float_T f = 0.0;
11286
11287 rettv->v_type = VAR_FLOAT;
11288 if (get_float_arg(argvars, &f) == OK)
11289 rettv->vval.v_float = sin(f);
11290 else
11291 rettv->vval.v_float = 0.0;
11292}
11293
11294/*
11295 * "sinh()" function
11296 */
11297 static void
11298f_sinh(typval_T *argvars, typval_T *rettv)
11299{
11300 float_T f = 0.0;
11301
11302 rettv->v_type = VAR_FLOAT;
11303 if (get_float_arg(argvars, &f) == OK)
11304 rettv->vval.v_float = sinh(f);
11305 else
11306 rettv->vval.v_float = 0.0;
11307}
11308#endif
11309
11310static int
11311#ifdef __BORLANDC__
11312 _RTLENTRYF
11313#endif
11314 item_compare(const void *s1, const void *s2);
11315static int
11316#ifdef __BORLANDC__
11317 _RTLENTRYF
11318#endif
11319 item_compare2(const void *s1, const void *s2);
11320
11321/* struct used in the array that's given to qsort() */
11322typedef struct
11323{
11324 listitem_T *item;
11325 int idx;
11326} sortItem_T;
11327
11328/* struct storing information about current sort */
11329typedef struct
11330{
11331 int item_compare_ic;
11332 int item_compare_numeric;
11333 int item_compare_numbers;
11334#ifdef FEAT_FLOAT
11335 int item_compare_float;
11336#endif
11337 char_u *item_compare_func;
11338 partial_T *item_compare_partial;
11339 dict_T *item_compare_selfdict;
11340 int item_compare_func_err;
11341 int item_compare_keep_zero;
11342} sortinfo_T;
11343static sortinfo_T *sortinfo = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011344#define ITEM_COMPARE_FAIL 999
11345
11346/*
11347 * Compare functions for f_sort() and f_uniq() below.
11348 */
11349 static int
11350#ifdef __BORLANDC__
11351_RTLENTRYF
11352#endif
11353item_compare(const void *s1, const void *s2)
11354{
11355 sortItem_T *si1, *si2;
11356 typval_T *tv1, *tv2;
11357 char_u *p1, *p2;
11358 char_u *tofree1 = NULL, *tofree2 = NULL;
11359 int res;
11360 char_u numbuf1[NUMBUFLEN];
11361 char_u numbuf2[NUMBUFLEN];
11362
11363 si1 = (sortItem_T *)s1;
11364 si2 = (sortItem_T *)s2;
11365 tv1 = &si1->item->li_tv;
11366 tv2 = &si2->item->li_tv;
11367
11368 if (sortinfo->item_compare_numbers)
11369 {
11370 varnumber_T v1 = get_tv_number(tv1);
11371 varnumber_T v2 = get_tv_number(tv2);
11372
11373 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11374 }
11375
11376#ifdef FEAT_FLOAT
11377 if (sortinfo->item_compare_float)
11378 {
11379 float_T v1 = get_tv_float(tv1);
11380 float_T v2 = get_tv_float(tv2);
11381
11382 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11383 }
11384#endif
11385
11386 /* tv2string() puts quotes around a string and allocates memory. Don't do
11387 * that for string variables. Use a single quote when comparing with a
11388 * non-string to do what the docs promise. */
11389 if (tv1->v_type == VAR_STRING)
11390 {
11391 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11392 p1 = (char_u *)"'";
11393 else
11394 p1 = tv1->vval.v_string;
11395 }
11396 else
11397 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
11398 if (tv2->v_type == VAR_STRING)
11399 {
11400 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11401 p2 = (char_u *)"'";
11402 else
11403 p2 = tv2->vval.v_string;
11404 }
11405 else
11406 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
11407 if (p1 == NULL)
11408 p1 = (char_u *)"";
11409 if (p2 == NULL)
11410 p2 = (char_u *)"";
11411 if (!sortinfo->item_compare_numeric)
11412 {
11413 if (sortinfo->item_compare_ic)
11414 res = STRICMP(p1, p2);
11415 else
11416 res = STRCMP(p1, p2);
11417 }
11418 else
11419 {
11420 double n1, n2;
11421 n1 = strtod((char *)p1, (char **)&p1);
11422 n2 = strtod((char *)p2, (char **)&p2);
11423 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
11424 }
11425
11426 /* When the result would be zero, compare the item indexes. Makes the
11427 * sort stable. */
11428 if (res == 0 && !sortinfo->item_compare_keep_zero)
11429 res = si1->idx > si2->idx ? 1 : -1;
11430
11431 vim_free(tofree1);
11432 vim_free(tofree2);
11433 return res;
11434}
11435
11436 static int
11437#ifdef __BORLANDC__
11438_RTLENTRYF
11439#endif
11440item_compare2(const void *s1, const void *s2)
11441{
11442 sortItem_T *si1, *si2;
11443 int res;
11444 typval_T rettv;
11445 typval_T argv[3];
11446 int dummy;
11447 char_u *func_name;
11448 partial_T *partial = sortinfo->item_compare_partial;
11449
11450 /* shortcut after failure in previous call; compare all items equal */
11451 if (sortinfo->item_compare_func_err)
11452 return 0;
11453
11454 si1 = (sortItem_T *)s1;
11455 si2 = (sortItem_T *)s2;
11456
11457 if (partial == NULL)
11458 func_name = sortinfo->item_compare_func;
11459 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020011460 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011461
11462 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
11463 * in the copy without changing the original list items. */
11464 copy_tv(&si1->item->li_tv, &argv[0]);
11465 copy_tv(&si2->item->li_tv, &argv[1]);
11466
11467 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
11468 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020011469 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011470 partial, sortinfo->item_compare_selfdict);
11471 clear_tv(&argv[0]);
11472 clear_tv(&argv[1]);
11473
11474 if (res == FAIL)
11475 res = ITEM_COMPARE_FAIL;
11476 else
11477 res = (int)get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
11478 if (sortinfo->item_compare_func_err)
11479 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
11480 clear_tv(&rettv);
11481
11482 /* When the result would be zero, compare the pointers themselves. Makes
11483 * the sort stable. */
11484 if (res == 0 && !sortinfo->item_compare_keep_zero)
11485 res = si1->idx > si2->idx ? 1 : -1;
11486
11487 return res;
11488}
11489
11490/*
11491 * "sort({list})" function
11492 */
11493 static void
11494do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
11495{
11496 list_T *l;
11497 listitem_T *li;
11498 sortItem_T *ptrs;
11499 sortinfo_T *old_sortinfo;
11500 sortinfo_T info;
11501 long len;
11502 long i;
11503
11504 /* Pointer to current info struct used in compare function. Save and
11505 * restore the current one for nested calls. */
11506 old_sortinfo = sortinfo;
11507 sortinfo = &info;
11508
11509 if (argvars[0].v_type != VAR_LIST)
11510 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
11511 else
11512 {
11513 l = argvars[0].vval.v_list;
11514 if (l == NULL || tv_check_lock(l->lv_lock,
11515 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
11516 TRUE))
11517 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011518 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011519
11520 len = list_len(l);
11521 if (len <= 1)
11522 goto theend; /* short list sorts pretty quickly */
11523
11524 info.item_compare_ic = FALSE;
11525 info.item_compare_numeric = FALSE;
11526 info.item_compare_numbers = FALSE;
11527#ifdef FEAT_FLOAT
11528 info.item_compare_float = FALSE;
11529#endif
11530 info.item_compare_func = NULL;
11531 info.item_compare_partial = NULL;
11532 info.item_compare_selfdict = NULL;
11533 if (argvars[1].v_type != VAR_UNKNOWN)
11534 {
11535 /* optional second argument: {func} */
11536 if (argvars[1].v_type == VAR_FUNC)
11537 info.item_compare_func = argvars[1].vval.v_string;
11538 else if (argvars[1].v_type == VAR_PARTIAL)
11539 info.item_compare_partial = argvars[1].vval.v_partial;
11540 else
11541 {
11542 int error = FALSE;
11543
11544 i = (long)get_tv_number_chk(&argvars[1], &error);
11545 if (error)
11546 goto theend; /* type error; errmsg already given */
11547 if (i == 1)
11548 info.item_compare_ic = TRUE;
11549 else if (argvars[1].v_type != VAR_NUMBER)
11550 info.item_compare_func = get_tv_string(&argvars[1]);
11551 else if (i != 0)
11552 {
11553 EMSG(_(e_invarg));
11554 goto theend;
11555 }
11556 if (info.item_compare_func != NULL)
11557 {
11558 if (*info.item_compare_func == NUL)
11559 {
11560 /* empty string means default sort */
11561 info.item_compare_func = NULL;
11562 }
11563 else if (STRCMP(info.item_compare_func, "n") == 0)
11564 {
11565 info.item_compare_func = NULL;
11566 info.item_compare_numeric = TRUE;
11567 }
11568 else if (STRCMP(info.item_compare_func, "N") == 0)
11569 {
11570 info.item_compare_func = NULL;
11571 info.item_compare_numbers = TRUE;
11572 }
11573#ifdef FEAT_FLOAT
11574 else if (STRCMP(info.item_compare_func, "f") == 0)
11575 {
11576 info.item_compare_func = NULL;
11577 info.item_compare_float = TRUE;
11578 }
11579#endif
11580 else if (STRCMP(info.item_compare_func, "i") == 0)
11581 {
11582 info.item_compare_func = NULL;
11583 info.item_compare_ic = TRUE;
11584 }
11585 }
11586 }
11587
11588 if (argvars[2].v_type != VAR_UNKNOWN)
11589 {
11590 /* optional third argument: {dict} */
11591 if (argvars[2].v_type != VAR_DICT)
11592 {
11593 EMSG(_(e_dictreq));
11594 goto theend;
11595 }
11596 info.item_compare_selfdict = argvars[2].vval.v_dict;
11597 }
11598 }
11599
11600 /* Make an array with each entry pointing to an item in the List. */
11601 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
11602 if (ptrs == NULL)
11603 goto theend;
11604
11605 i = 0;
11606 if (sort)
11607 {
11608 /* sort(): ptrs will be the list to sort */
11609 for (li = l->lv_first; li != NULL; li = li->li_next)
11610 {
11611 ptrs[i].item = li;
11612 ptrs[i].idx = i;
11613 ++i;
11614 }
11615
11616 info.item_compare_func_err = FALSE;
11617 info.item_compare_keep_zero = FALSE;
11618 /* test the compare function */
11619 if ((info.item_compare_func != NULL
11620 || info.item_compare_partial != NULL)
11621 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
11622 == ITEM_COMPARE_FAIL)
11623 EMSG(_("E702: Sort compare function failed"));
11624 else
11625 {
11626 /* Sort the array with item pointers. */
11627 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
11628 info.item_compare_func == NULL
11629 && info.item_compare_partial == NULL
11630 ? item_compare : item_compare2);
11631
11632 if (!info.item_compare_func_err)
11633 {
11634 /* Clear the List and append the items in sorted order. */
11635 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
11636 l->lv_len = 0;
11637 for (i = 0; i < len; ++i)
11638 list_append(l, ptrs[i].item);
11639 }
11640 }
11641 }
11642 else
11643 {
11644 int (*item_compare_func_ptr)(const void *, const void *);
11645
11646 /* f_uniq(): ptrs will be a stack of items to remove */
11647 info.item_compare_func_err = FALSE;
11648 info.item_compare_keep_zero = TRUE;
11649 item_compare_func_ptr = info.item_compare_func != NULL
11650 || info.item_compare_partial != NULL
11651 ? item_compare2 : item_compare;
11652
11653 for (li = l->lv_first; li != NULL && li->li_next != NULL;
11654 li = li->li_next)
11655 {
11656 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
11657 == 0)
11658 ptrs[i++].item = li;
11659 if (info.item_compare_func_err)
11660 {
11661 EMSG(_("E882: Uniq compare function failed"));
11662 break;
11663 }
11664 }
11665
11666 if (!info.item_compare_func_err)
11667 {
11668 while (--i >= 0)
11669 {
11670 li = ptrs[i].item->li_next;
11671 ptrs[i].item->li_next = li->li_next;
11672 if (li->li_next != NULL)
11673 li->li_next->li_prev = ptrs[i].item;
11674 else
11675 l->lv_last = ptrs[i].item;
11676 list_fix_watch(l, li);
11677 listitem_free(li);
11678 l->lv_len--;
11679 }
11680 }
11681 }
11682
11683 vim_free(ptrs);
11684 }
11685theend:
11686 sortinfo = old_sortinfo;
11687}
11688
11689/*
11690 * "sort({list})" function
11691 */
11692 static void
11693f_sort(typval_T *argvars, typval_T *rettv)
11694{
11695 do_sort_uniq(argvars, rettv, TRUE);
11696}
11697
11698/*
11699 * "uniq({list})" function
11700 */
11701 static void
11702f_uniq(typval_T *argvars, typval_T *rettv)
11703{
11704 do_sort_uniq(argvars, rettv, FALSE);
11705}
11706
11707/*
11708 * "soundfold({word})" function
11709 */
11710 static void
11711f_soundfold(typval_T *argvars, typval_T *rettv)
11712{
11713 char_u *s;
11714
11715 rettv->v_type = VAR_STRING;
11716 s = get_tv_string(&argvars[0]);
11717#ifdef FEAT_SPELL
11718 rettv->vval.v_string = eval_soundfold(s);
11719#else
11720 rettv->vval.v_string = vim_strsave(s);
11721#endif
11722}
11723
11724/*
11725 * "spellbadword()" function
11726 */
11727 static void
11728f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
11729{
11730 char_u *word = (char_u *)"";
11731 hlf_T attr = HLF_COUNT;
11732 int len = 0;
11733
11734 if (rettv_list_alloc(rettv) == FAIL)
11735 return;
11736
11737#ifdef FEAT_SPELL
11738 if (argvars[0].v_type == VAR_UNKNOWN)
11739 {
11740 /* Find the start and length of the badly spelled word. */
11741 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
11742 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011743 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011744 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011745 curwin->w_set_curswant = TRUE;
11746 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011747 }
11748 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
11749 {
11750 char_u *str = get_tv_string_chk(&argvars[0]);
11751 int capcol = -1;
11752
11753 if (str != NULL)
11754 {
11755 /* Check the argument for spelling. */
11756 while (*str != NUL)
11757 {
11758 len = spell_check(curwin, str, &attr, &capcol, FALSE);
11759 if (attr != HLF_COUNT)
11760 {
11761 word = str;
11762 break;
11763 }
11764 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020011765 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011766 }
11767 }
11768 }
11769#endif
11770
11771 list_append_string(rettv->vval.v_list, word, len);
11772 list_append_string(rettv->vval.v_list, (char_u *)(
11773 attr == HLF_SPB ? "bad" :
11774 attr == HLF_SPR ? "rare" :
11775 attr == HLF_SPL ? "local" :
11776 attr == HLF_SPC ? "caps" :
11777 ""), -1);
11778}
11779
11780/*
11781 * "spellsuggest()" function
11782 */
11783 static void
11784f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
11785{
11786#ifdef FEAT_SPELL
11787 char_u *str;
11788 int typeerr = FALSE;
11789 int maxcount;
11790 garray_T ga;
11791 int i;
11792 listitem_T *li;
11793 int need_capital = FALSE;
11794#endif
11795
11796 if (rettv_list_alloc(rettv) == FAIL)
11797 return;
11798
11799#ifdef FEAT_SPELL
11800 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
11801 {
11802 str = get_tv_string(&argvars[0]);
11803 if (argvars[1].v_type != VAR_UNKNOWN)
11804 {
11805 maxcount = (int)get_tv_number_chk(&argvars[1], &typeerr);
11806 if (maxcount <= 0)
11807 return;
11808 if (argvars[2].v_type != VAR_UNKNOWN)
11809 {
11810 need_capital = (int)get_tv_number_chk(&argvars[2], &typeerr);
11811 if (typeerr)
11812 return;
11813 }
11814 }
11815 else
11816 maxcount = 25;
11817
11818 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
11819
11820 for (i = 0; i < ga.ga_len; ++i)
11821 {
11822 str = ((char_u **)ga.ga_data)[i];
11823
11824 li = listitem_alloc();
11825 if (li == NULL)
11826 vim_free(str);
11827 else
11828 {
11829 li->li_tv.v_type = VAR_STRING;
11830 li->li_tv.v_lock = 0;
11831 li->li_tv.vval.v_string = str;
11832 list_append(rettv->vval.v_list, li);
11833 }
11834 }
11835 ga_clear(&ga);
11836 }
11837#endif
11838}
11839
11840 static void
11841f_split(typval_T *argvars, typval_T *rettv)
11842{
11843 char_u *str;
11844 char_u *end;
11845 char_u *pat = NULL;
11846 regmatch_T regmatch;
11847 char_u patbuf[NUMBUFLEN];
11848 char_u *save_cpo;
11849 int match;
11850 colnr_T col = 0;
11851 int keepempty = FALSE;
11852 int typeerr = FALSE;
11853
11854 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11855 save_cpo = p_cpo;
11856 p_cpo = (char_u *)"";
11857
11858 str = get_tv_string(&argvars[0]);
11859 if (argvars[1].v_type != VAR_UNKNOWN)
11860 {
11861 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11862 if (pat == NULL)
11863 typeerr = TRUE;
11864 if (argvars[2].v_type != VAR_UNKNOWN)
11865 keepempty = (int)get_tv_number_chk(&argvars[2], &typeerr);
11866 }
11867 if (pat == NULL || *pat == NUL)
11868 pat = (char_u *)"[\\x01- ]\\+";
11869
11870 if (rettv_list_alloc(rettv) == FAIL)
11871 return;
11872 if (typeerr)
11873 return;
11874
11875 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11876 if (regmatch.regprog != NULL)
11877 {
11878 regmatch.rm_ic = FALSE;
11879 while (*str != NUL || keepempty)
11880 {
11881 if (*str == NUL)
11882 match = FALSE; /* empty item at the end */
11883 else
11884 match = vim_regexec_nl(&regmatch, str, col);
11885 if (match)
11886 end = regmatch.startp[0];
11887 else
11888 end = str + STRLEN(str);
11889 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
11890 && *str != NUL && match && end < regmatch.endp[0]))
11891 {
11892 if (list_append_string(rettv->vval.v_list, str,
11893 (int)(end - str)) == FAIL)
11894 break;
11895 }
11896 if (!match)
11897 break;
11898 /* Advance to just after the match. */
11899 if (regmatch.endp[0] > str)
11900 col = 0;
11901 else
11902 {
11903 /* Don't get stuck at the same match. */
11904#ifdef FEAT_MBYTE
11905 col = (*mb_ptr2len)(regmatch.endp[0]);
11906#else
11907 col = 1;
11908#endif
11909 }
11910 str = regmatch.endp[0];
11911 }
11912
11913 vim_regfree(regmatch.regprog);
11914 }
11915
11916 p_cpo = save_cpo;
11917}
11918
11919#ifdef FEAT_FLOAT
11920/*
11921 * "sqrt()" function
11922 */
11923 static void
11924f_sqrt(typval_T *argvars, typval_T *rettv)
11925{
11926 float_T f = 0.0;
11927
11928 rettv->v_type = VAR_FLOAT;
11929 if (get_float_arg(argvars, &f) == OK)
11930 rettv->vval.v_float = sqrt(f);
11931 else
11932 rettv->vval.v_float = 0.0;
11933}
11934
11935/*
11936 * "str2float()" function
11937 */
11938 static void
11939f_str2float(typval_T *argvars, typval_T *rettv)
11940{
11941 char_u *p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011942 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011943
Bram Moolenaar08243d22017-01-10 16:12:29 +010011944 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011945 p = skipwhite(p + 1);
11946 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011947 if (isneg)
11948 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011949 rettv->v_type = VAR_FLOAT;
11950}
11951#endif
11952
11953/*
11954 * "str2nr()" function
11955 */
11956 static void
11957f_str2nr(typval_T *argvars, typval_T *rettv)
11958{
11959 int base = 10;
11960 char_u *p;
11961 varnumber_T n;
11962 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010011963 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011964
11965 if (argvars[1].v_type != VAR_UNKNOWN)
11966 {
11967 base = (int)get_tv_number(&argvars[1]);
11968 if (base != 2 && base != 8 && base != 10 && base != 16)
11969 {
11970 EMSG(_(e_invarg));
11971 return;
11972 }
11973 }
11974
11975 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011976 isneg = (*p == '-');
11977 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011978 p = skipwhite(p + 1);
11979 switch (base)
11980 {
11981 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
11982 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
11983 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
11984 default: what = 0;
11985 }
11986 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011987 if (isneg)
11988 rettv->vval.v_number = -n;
11989 else
11990 rettv->vval.v_number = n;
11991
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011992}
11993
11994#ifdef HAVE_STRFTIME
11995/*
11996 * "strftime({format}[, {time}])" function
11997 */
11998 static void
11999f_strftime(typval_T *argvars, typval_T *rettv)
12000{
12001 char_u result_buf[256];
12002 struct tm *curtime;
12003 time_t seconds;
12004 char_u *p;
12005
12006 rettv->v_type = VAR_STRING;
12007
12008 p = get_tv_string(&argvars[0]);
12009 if (argvars[1].v_type == VAR_UNKNOWN)
12010 seconds = time(NULL);
12011 else
12012 seconds = (time_t)get_tv_number(&argvars[1]);
12013 curtime = localtime(&seconds);
12014 /* MSVC returns NULL for an invalid value of seconds. */
12015 if (curtime == NULL)
12016 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
12017 else
12018 {
12019# ifdef FEAT_MBYTE
12020 vimconv_T conv;
12021 char_u *enc;
12022
12023 conv.vc_type = CONV_NONE;
12024 enc = enc_locale();
12025 convert_setup(&conv, p_enc, enc);
12026 if (conv.vc_type != CONV_NONE)
12027 p = string_convert(&conv, p, NULL);
12028# endif
12029 if (p != NULL)
12030 (void)strftime((char *)result_buf, sizeof(result_buf),
12031 (char *)p, curtime);
12032 else
12033 result_buf[0] = NUL;
12034
12035# ifdef FEAT_MBYTE
12036 if (conv.vc_type != CONV_NONE)
12037 vim_free(p);
12038 convert_setup(&conv, enc, p_enc);
12039 if (conv.vc_type != CONV_NONE)
12040 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
12041 else
12042# endif
12043 rettv->vval.v_string = vim_strsave(result_buf);
12044
12045# ifdef FEAT_MBYTE
12046 /* Release conversion descriptors */
12047 convert_setup(&conv, NULL, NULL);
12048 vim_free(enc);
12049# endif
12050 }
12051}
12052#endif
12053
12054/*
12055 * "strgetchar()" function
12056 */
12057 static void
12058f_strgetchar(typval_T *argvars, typval_T *rettv)
12059{
12060 char_u *str;
12061 int len;
12062 int error = FALSE;
12063 int charidx;
12064
12065 rettv->vval.v_number = -1;
12066 str = get_tv_string_chk(&argvars[0]);
12067 if (str == NULL)
12068 return;
12069 len = (int)STRLEN(str);
12070 charidx = (int)get_tv_number_chk(&argvars[1], &error);
12071 if (error)
12072 return;
12073#ifdef FEAT_MBYTE
12074 {
12075 int byteidx = 0;
12076
12077 while (charidx >= 0 && byteidx < len)
12078 {
12079 if (charidx == 0)
12080 {
12081 rettv->vval.v_number = mb_ptr2char(str + byteidx);
12082 break;
12083 }
12084 --charidx;
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012085 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012086 }
12087 }
12088#else
12089 if (charidx < len)
12090 rettv->vval.v_number = str[charidx];
12091#endif
12092}
12093
12094/*
12095 * "stridx()" function
12096 */
12097 static void
12098f_stridx(typval_T *argvars, typval_T *rettv)
12099{
12100 char_u buf[NUMBUFLEN];
12101 char_u *needle;
12102 char_u *haystack;
12103 char_u *save_haystack;
12104 char_u *pos;
12105 int start_idx;
12106
12107 needle = get_tv_string_chk(&argvars[1]);
12108 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
12109 rettv->vval.v_number = -1;
12110 if (needle == NULL || haystack == NULL)
12111 return; /* type error; errmsg already given */
12112
12113 if (argvars[2].v_type != VAR_UNKNOWN)
12114 {
12115 int error = FALSE;
12116
12117 start_idx = (int)get_tv_number_chk(&argvars[2], &error);
12118 if (error || start_idx >= (int)STRLEN(haystack))
12119 return;
12120 if (start_idx >= 0)
12121 haystack += start_idx;
12122 }
12123
12124 pos = (char_u *)strstr((char *)haystack, (char *)needle);
12125 if (pos != NULL)
12126 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
12127}
12128
12129/*
12130 * "string()" function
12131 */
12132 static void
12133f_string(typval_T *argvars, typval_T *rettv)
12134{
12135 char_u *tofree;
12136 char_u numbuf[NUMBUFLEN];
12137
12138 rettv->v_type = VAR_STRING;
12139 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
12140 get_copyID());
12141 /* Make a copy if we have a value but it's not in allocated memory. */
12142 if (rettv->vval.v_string != NULL && tofree == NULL)
12143 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
12144}
12145
12146/*
12147 * "strlen()" function
12148 */
12149 static void
12150f_strlen(typval_T *argvars, typval_T *rettv)
12151{
12152 rettv->vval.v_number = (varnumber_T)(STRLEN(
12153 get_tv_string(&argvars[0])));
12154}
12155
12156/*
12157 * "strchars()" function
12158 */
12159 static void
12160f_strchars(typval_T *argvars, typval_T *rettv)
12161{
12162 char_u *s = get_tv_string(&argvars[0]);
12163 int skipcc = 0;
12164#ifdef FEAT_MBYTE
12165 varnumber_T len = 0;
12166 int (*func_mb_ptr2char_adv)(char_u **pp);
12167#endif
12168
12169 if (argvars[1].v_type != VAR_UNKNOWN)
12170 skipcc = (int)get_tv_number_chk(&argvars[1], NULL);
12171 if (skipcc < 0 || skipcc > 1)
12172 EMSG(_(e_invarg));
12173 else
12174 {
12175#ifdef FEAT_MBYTE
12176 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
12177 while (*s != NUL)
12178 {
12179 func_mb_ptr2char_adv(&s);
12180 ++len;
12181 }
12182 rettv->vval.v_number = len;
12183#else
12184 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
12185#endif
12186 }
12187}
12188
12189/*
12190 * "strdisplaywidth()" function
12191 */
12192 static void
12193f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
12194{
12195 char_u *s = get_tv_string(&argvars[0]);
12196 int col = 0;
12197
12198 if (argvars[1].v_type != VAR_UNKNOWN)
12199 col = (int)get_tv_number(&argvars[1]);
12200
12201 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
12202}
12203
12204/*
12205 * "strwidth()" function
12206 */
12207 static void
12208f_strwidth(typval_T *argvars, typval_T *rettv)
12209{
12210 char_u *s = get_tv_string(&argvars[0]);
12211
12212 rettv->vval.v_number = (varnumber_T)(
12213#ifdef FEAT_MBYTE
12214 mb_string2cells(s, -1)
12215#else
12216 STRLEN(s)
12217#endif
12218 );
12219}
12220
12221/*
12222 * "strcharpart()" function
12223 */
12224 static void
12225f_strcharpart(typval_T *argvars, typval_T *rettv)
12226{
12227#ifdef FEAT_MBYTE
12228 char_u *p;
12229 int nchar;
12230 int nbyte = 0;
12231 int charlen;
12232 int len = 0;
12233 int slen;
12234 int error = FALSE;
12235
12236 p = get_tv_string(&argvars[0]);
12237 slen = (int)STRLEN(p);
12238
12239 nchar = (int)get_tv_number_chk(&argvars[1], &error);
12240 if (!error)
12241 {
12242 if (nchar > 0)
12243 while (nchar > 0 && nbyte < slen)
12244 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012245 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012246 --nchar;
12247 }
12248 else
12249 nbyte = nchar;
12250 if (argvars[2].v_type != VAR_UNKNOWN)
12251 {
12252 charlen = (int)get_tv_number(&argvars[2]);
12253 while (charlen > 0 && nbyte + len < slen)
12254 {
12255 int off = nbyte + len;
12256
12257 if (off < 0)
12258 len += 1;
12259 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012260 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012261 --charlen;
12262 }
12263 }
12264 else
12265 len = slen - nbyte; /* default: all bytes that are available. */
12266 }
12267
12268 /*
12269 * Only return the overlap between the specified part and the actual
12270 * string.
12271 */
12272 if (nbyte < 0)
12273 {
12274 len += nbyte;
12275 nbyte = 0;
12276 }
12277 else if (nbyte > slen)
12278 nbyte = slen;
12279 if (len < 0)
12280 len = 0;
12281 else if (nbyte + len > slen)
12282 len = slen - nbyte;
12283
12284 rettv->v_type = VAR_STRING;
12285 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
12286#else
12287 f_strpart(argvars, rettv);
12288#endif
12289}
12290
12291/*
12292 * "strpart()" function
12293 */
12294 static void
12295f_strpart(typval_T *argvars, typval_T *rettv)
12296{
12297 char_u *p;
12298 int n;
12299 int len;
12300 int slen;
12301 int error = FALSE;
12302
12303 p = get_tv_string(&argvars[0]);
12304 slen = (int)STRLEN(p);
12305
12306 n = (int)get_tv_number_chk(&argvars[1], &error);
12307 if (error)
12308 len = 0;
12309 else if (argvars[2].v_type != VAR_UNKNOWN)
12310 len = (int)get_tv_number(&argvars[2]);
12311 else
12312 len = slen - n; /* default len: all bytes that are available. */
12313
12314 /*
12315 * Only return the overlap between the specified part and the actual
12316 * string.
12317 */
12318 if (n < 0)
12319 {
12320 len += n;
12321 n = 0;
12322 }
12323 else if (n > slen)
12324 n = slen;
12325 if (len < 0)
12326 len = 0;
12327 else if (n + len > slen)
12328 len = slen - n;
12329
12330 rettv->v_type = VAR_STRING;
12331 rettv->vval.v_string = vim_strnsave(p + n, len);
12332}
12333
12334/*
12335 * "strridx()" function
12336 */
12337 static void
12338f_strridx(typval_T *argvars, typval_T *rettv)
12339{
12340 char_u buf[NUMBUFLEN];
12341 char_u *needle;
12342 char_u *haystack;
12343 char_u *rest;
12344 char_u *lastmatch = NULL;
12345 int haystack_len, end_idx;
12346
12347 needle = get_tv_string_chk(&argvars[1]);
12348 haystack = get_tv_string_buf_chk(&argvars[0], buf);
12349
12350 rettv->vval.v_number = -1;
12351 if (needle == NULL || haystack == NULL)
12352 return; /* type error; errmsg already given */
12353
12354 haystack_len = (int)STRLEN(haystack);
12355 if (argvars[2].v_type != VAR_UNKNOWN)
12356 {
12357 /* Third argument: upper limit for index */
12358 end_idx = (int)get_tv_number_chk(&argvars[2], NULL);
12359 if (end_idx < 0)
12360 return; /* can never find a match */
12361 }
12362 else
12363 end_idx = haystack_len;
12364
12365 if (*needle == NUL)
12366 {
12367 /* Empty string matches past the end. */
12368 lastmatch = haystack + end_idx;
12369 }
12370 else
12371 {
12372 for (rest = haystack; *rest != '\0'; ++rest)
12373 {
12374 rest = (char_u *)strstr((char *)rest, (char *)needle);
12375 if (rest == NULL || rest > haystack + end_idx)
12376 break;
12377 lastmatch = rest;
12378 }
12379 }
12380
12381 if (lastmatch == NULL)
12382 rettv->vval.v_number = -1;
12383 else
12384 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
12385}
12386
12387/*
12388 * "strtrans()" function
12389 */
12390 static void
12391f_strtrans(typval_T *argvars, typval_T *rettv)
12392{
12393 rettv->v_type = VAR_STRING;
12394 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
12395}
12396
12397/*
12398 * "submatch()" function
12399 */
12400 static void
12401f_submatch(typval_T *argvars, typval_T *rettv)
12402{
12403 int error = FALSE;
12404 int no;
12405 int retList = 0;
12406
12407 no = (int)get_tv_number_chk(&argvars[0], &error);
12408 if (error)
12409 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012410 if (no < 0 || no >= NSUBEXP)
12411 {
Bram Moolenaar79518e22017-02-17 16:31:35 +010012412 EMSGN(_("E935: invalid submatch number: %d"), no);
12413 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012414 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012415 if (argvars[1].v_type != VAR_UNKNOWN)
12416 retList = (int)get_tv_number_chk(&argvars[1], &error);
12417 if (error)
12418 return;
12419
12420 if (retList == 0)
12421 {
12422 rettv->v_type = VAR_STRING;
12423 rettv->vval.v_string = reg_submatch(no);
12424 }
12425 else
12426 {
12427 rettv->v_type = VAR_LIST;
12428 rettv->vval.v_list = reg_submatch_list(no);
12429 }
12430}
12431
12432/*
12433 * "substitute()" function
12434 */
12435 static void
12436f_substitute(typval_T *argvars, typval_T *rettv)
12437{
12438 char_u patbuf[NUMBUFLEN];
12439 char_u subbuf[NUMBUFLEN];
12440 char_u flagsbuf[NUMBUFLEN];
12441
12442 char_u *str = get_tv_string_chk(&argvars[0]);
12443 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012444 char_u *sub = NULL;
12445 typval_T *expr = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012446 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
12447
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012448 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
12449 expr = &argvars[2];
12450 else
12451 sub = get_tv_string_buf_chk(&argvars[2], subbuf);
12452
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012453 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012454 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
12455 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012456 rettv->vval.v_string = NULL;
12457 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012458 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012459}
12460
12461/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020012462 * "swapinfo(swap_filename)" function
12463 */
12464 static void
12465f_swapinfo(typval_T *argvars, typval_T *rettv)
12466{
12467 if (rettv_dict_alloc(rettv) == OK)
12468 get_b0_dict(get_tv_string(argvars), rettv->vval.v_dict);
12469}
12470
12471/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020012472 * "swapname(expr)" function
12473 */
12474 static void
12475f_swapname(typval_T *argvars, typval_T *rettv)
12476{
12477 buf_T *buf;
12478
12479 rettv->v_type = VAR_STRING;
12480 buf = get_buf_tv(&argvars[0], FALSE);
12481 if (buf == NULL || buf->b_ml.ml_mfp == NULL
12482 || buf->b_ml.ml_mfp->mf_fname == NULL)
12483 rettv->vval.v_string = NULL;
12484 else
12485 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
12486}
12487
12488/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012489 * "synID(lnum, col, trans)" function
12490 */
12491 static void
12492f_synID(typval_T *argvars UNUSED, typval_T *rettv)
12493{
12494 int id = 0;
12495#ifdef FEAT_SYN_HL
12496 linenr_T lnum;
12497 colnr_T col;
12498 int trans;
12499 int transerr = FALSE;
12500
12501 lnum = get_tv_lnum(argvars); /* -1 on type error */
12502 col = (linenr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12503 trans = (int)get_tv_number_chk(&argvars[2], &transerr);
12504
12505 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12506 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
12507 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
12508#endif
12509
12510 rettv->vval.v_number = id;
12511}
12512
12513/*
12514 * "synIDattr(id, what [, mode])" function
12515 */
12516 static void
12517f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
12518{
12519 char_u *p = NULL;
12520#ifdef FEAT_SYN_HL
12521 int id;
12522 char_u *what;
12523 char_u *mode;
12524 char_u modebuf[NUMBUFLEN];
12525 int modec;
12526
12527 id = (int)get_tv_number(&argvars[0]);
12528 what = get_tv_string(&argvars[1]);
12529 if (argvars[2].v_type != VAR_UNKNOWN)
12530 {
12531 mode = get_tv_string_buf(&argvars[2], modebuf);
12532 modec = TOLOWER_ASC(mode[0]);
12533 if (modec != 't' && modec != 'c' && modec != 'g')
12534 modec = 0; /* replace invalid with current */
12535 }
12536 else
12537 {
12538#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
12539 if (USE_24BIT)
12540 modec = 'g';
12541 else
12542#endif
12543 if (t_colors > 1)
12544 modec = 'c';
12545 else
12546 modec = 't';
12547 }
12548
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012549 switch (TOLOWER_ASC(what[0]))
12550 {
12551 case 'b':
12552 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
12553 p = highlight_color(id, what, modec);
12554 else /* bold */
12555 p = highlight_has_attr(id, HL_BOLD, modec);
12556 break;
12557
12558 case 'f': /* fg[#] or font */
12559 p = highlight_color(id, what, modec);
12560 break;
12561
12562 case 'i':
12563 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
12564 p = highlight_has_attr(id, HL_INVERSE, modec);
12565 else /* italic */
12566 p = highlight_has_attr(id, HL_ITALIC, modec);
12567 break;
12568
12569 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020012570 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012571 break;
12572
12573 case 'r': /* reverse */
12574 p = highlight_has_attr(id, HL_INVERSE, modec);
12575 break;
12576
12577 case 's':
12578 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
12579 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020012580 /* strikeout */
12581 else if (TOLOWER_ASC(what[1]) == 't' &&
12582 TOLOWER_ASC(what[2]) == 'r')
12583 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012584 else /* standout */
12585 p = highlight_has_attr(id, HL_STANDOUT, modec);
12586 break;
12587
12588 case 'u':
12589 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
12590 /* underline */
12591 p = highlight_has_attr(id, HL_UNDERLINE, modec);
12592 else
12593 /* undercurl */
12594 p = highlight_has_attr(id, HL_UNDERCURL, modec);
12595 break;
12596 }
12597
12598 if (p != NULL)
12599 p = vim_strsave(p);
12600#endif
12601 rettv->v_type = VAR_STRING;
12602 rettv->vval.v_string = p;
12603}
12604
12605/*
12606 * "synIDtrans(id)" function
12607 */
12608 static void
12609f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
12610{
12611 int id;
12612
12613#ifdef FEAT_SYN_HL
12614 id = (int)get_tv_number(&argvars[0]);
12615
12616 if (id > 0)
12617 id = syn_get_final_id(id);
12618 else
12619#endif
12620 id = 0;
12621
12622 rettv->vval.v_number = id;
12623}
12624
12625/*
12626 * "synconcealed(lnum, col)" function
12627 */
12628 static void
12629f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
12630{
12631#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12632 linenr_T lnum;
12633 colnr_T col;
12634 int syntax_flags = 0;
12635 int cchar;
12636 int matchid = 0;
12637 char_u str[NUMBUFLEN];
12638#endif
12639
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012640 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012641
12642#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12643 lnum = get_tv_lnum(argvars); /* -1 on type error */
12644 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12645
12646 vim_memset(str, NUL, sizeof(str));
12647
12648 if (rettv_list_alloc(rettv) != FAIL)
12649 {
12650 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12651 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12652 && curwin->w_p_cole > 0)
12653 {
12654 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
12655 syntax_flags = get_syntax_info(&matchid);
12656
12657 /* get the conceal character */
12658 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
12659 {
12660 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020012661 if (cchar == NUL && curwin->w_p_cole == 1)
12662 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012663 if (cchar != NUL)
12664 {
12665# ifdef FEAT_MBYTE
12666 if (has_mbyte)
12667 (*mb_char2bytes)(cchar, str);
12668 else
12669# endif
12670 str[0] = cchar;
12671 }
12672 }
12673 }
12674
12675 list_append_number(rettv->vval.v_list,
12676 (syntax_flags & HL_CONCEAL) != 0);
12677 /* -1 to auto-determine strlen */
12678 list_append_string(rettv->vval.v_list, str, -1);
12679 list_append_number(rettv->vval.v_list, matchid);
12680 }
12681#endif
12682}
12683
12684/*
12685 * "synstack(lnum, col)" function
12686 */
12687 static void
12688f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
12689{
12690#ifdef FEAT_SYN_HL
12691 linenr_T lnum;
12692 colnr_T col;
12693 int i;
12694 int id;
12695#endif
12696
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012697 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012698
12699#ifdef FEAT_SYN_HL
12700 lnum = get_tv_lnum(argvars); /* -1 on type error */
12701 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12702
12703 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12704 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12705 && rettv_list_alloc(rettv) != FAIL)
12706 {
12707 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
12708 for (i = 0; ; ++i)
12709 {
12710 id = syn_get_stack_item(i);
12711 if (id < 0)
12712 break;
12713 if (list_append_number(rettv->vval.v_list, id) == FAIL)
12714 break;
12715 }
12716 }
12717#endif
12718}
12719
12720 static void
12721get_cmd_output_as_rettv(
12722 typval_T *argvars,
12723 typval_T *rettv,
12724 int retlist)
12725{
12726 char_u *res = NULL;
12727 char_u *p;
12728 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012729 int err = FALSE;
12730 FILE *fd;
12731 list_T *list = NULL;
12732 int flags = SHELL_SILENT;
12733
12734 rettv->v_type = VAR_STRING;
12735 rettv->vval.v_string = NULL;
12736 if (check_restricted() || check_secure())
12737 goto errret;
12738
12739 if (argvars[1].v_type != VAR_UNKNOWN)
12740 {
12741 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010012742 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012743 * command.
12744 */
12745 if ((infile = vim_tempname('i', TRUE)) == NULL)
12746 {
12747 EMSG(_(e_notmp));
12748 goto errret;
12749 }
12750
12751 fd = mch_fopen((char *)infile, WRITEBIN);
12752 if (fd == NULL)
12753 {
12754 EMSG2(_(e_notopen), infile);
12755 goto errret;
12756 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010012757 if (argvars[1].v_type == VAR_NUMBER)
12758 {
12759 linenr_T lnum;
12760 buf_T *buf;
12761
12762 buf = buflist_findnr(argvars[1].vval.v_number);
12763 if (buf == NULL)
12764 {
12765 EMSGN(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010012766 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010012767 goto errret;
12768 }
12769
12770 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
12771 {
12772 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
12773 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
12774 {
12775 err = TRUE;
12776 break;
12777 }
12778 if (putc(NL, fd) == EOF)
12779 {
12780 err = TRUE;
12781 break;
12782 }
12783 }
12784 }
12785 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012786 {
12787 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
12788 err = TRUE;
12789 }
12790 else
12791 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010012792 size_t len;
12793 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012794
12795 p = get_tv_string_buf_chk(&argvars[1], buf);
12796 if (p == NULL)
12797 {
12798 fclose(fd);
12799 goto errret; /* type error; errmsg already given */
12800 }
12801 len = STRLEN(p);
12802 if (len > 0 && fwrite(p, len, 1, fd) != 1)
12803 err = TRUE;
12804 }
12805 if (fclose(fd) != 0)
12806 err = TRUE;
12807 if (err)
12808 {
12809 EMSG(_("E677: Error writing temp file"));
12810 goto errret;
12811 }
12812 }
12813
12814 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
12815 * echoes typeahead, that messes up the display. */
12816 if (!msg_silent)
12817 flags += SHELL_COOKED;
12818
12819 if (retlist)
12820 {
12821 int len;
12822 listitem_T *li;
12823 char_u *s = NULL;
12824 char_u *start;
12825 char_u *end;
12826 int i;
12827
12828 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
12829 if (res == NULL)
12830 goto errret;
12831
12832 list = list_alloc();
12833 if (list == NULL)
12834 goto errret;
12835
12836 for (i = 0; i < len; ++i)
12837 {
12838 start = res + i;
12839 while (i < len && res[i] != NL)
12840 ++i;
12841 end = res + i;
12842
12843 s = alloc((unsigned)(end - start + 1));
12844 if (s == NULL)
12845 goto errret;
12846
12847 for (p = s; start < end; ++p, ++start)
12848 *p = *start == NUL ? NL : *start;
12849 *p = NUL;
12850
12851 li = listitem_alloc();
12852 if (li == NULL)
12853 {
12854 vim_free(s);
12855 goto errret;
12856 }
12857 li->li_tv.v_type = VAR_STRING;
12858 li->li_tv.v_lock = 0;
12859 li->li_tv.vval.v_string = s;
12860 list_append(list, li);
12861 }
12862
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012863 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012864 list = NULL;
12865 }
12866 else
12867 {
12868 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
12869#ifdef USE_CR
12870 /* translate <CR> into <NL> */
12871 if (res != NULL)
12872 {
12873 char_u *s;
12874
12875 for (s = res; *s; ++s)
12876 {
12877 if (*s == CAR)
12878 *s = NL;
12879 }
12880 }
12881#else
12882# ifdef USE_CRNL
12883 /* translate <CR><NL> into <NL> */
12884 if (res != NULL)
12885 {
12886 char_u *s, *d;
12887
12888 d = res;
12889 for (s = res; *s; ++s)
12890 {
12891 if (s[0] == CAR && s[1] == NL)
12892 ++s;
12893 *d++ = *s;
12894 }
12895 *d = NUL;
12896 }
12897# endif
12898#endif
12899 rettv->vval.v_string = res;
12900 res = NULL;
12901 }
12902
12903errret:
12904 if (infile != NULL)
12905 {
12906 mch_remove(infile);
12907 vim_free(infile);
12908 }
12909 if (res != NULL)
12910 vim_free(res);
12911 if (list != NULL)
12912 list_free(list);
12913}
12914
12915/*
12916 * "system()" function
12917 */
12918 static void
12919f_system(typval_T *argvars, typval_T *rettv)
12920{
12921 get_cmd_output_as_rettv(argvars, rettv, FALSE);
12922}
12923
12924/*
12925 * "systemlist()" function
12926 */
12927 static void
12928f_systemlist(typval_T *argvars, typval_T *rettv)
12929{
12930 get_cmd_output_as_rettv(argvars, rettv, TRUE);
12931}
12932
12933/*
12934 * "tabpagebuflist()" function
12935 */
12936 static void
12937f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12938{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012939 tabpage_T *tp;
12940 win_T *wp = NULL;
12941
12942 if (argvars[0].v_type == VAR_UNKNOWN)
12943 wp = firstwin;
12944 else
12945 {
12946 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12947 if (tp != NULL)
12948 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12949 }
12950 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
12951 {
12952 for (; wp != NULL; wp = wp->w_next)
12953 if (list_append_number(rettv->vval.v_list,
12954 wp->w_buffer->b_fnum) == FAIL)
12955 break;
12956 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012957}
12958
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012959/*
12960 * "tabpagenr()" function
12961 */
12962 static void
12963f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
12964{
12965 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012966 char_u *arg;
12967
12968 if (argvars[0].v_type != VAR_UNKNOWN)
12969 {
12970 arg = get_tv_string_chk(&argvars[0]);
12971 nr = 0;
12972 if (arg != NULL)
12973 {
12974 if (STRCMP(arg, "$") == 0)
12975 nr = tabpage_index(NULL) - 1;
12976 else
12977 EMSG2(_(e_invexpr2), arg);
12978 }
12979 }
12980 else
12981 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012982 rettv->vval.v_number = nr;
12983}
12984
12985
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012986/*
12987 * Common code for tabpagewinnr() and winnr().
12988 */
12989 static int
12990get_winnr(tabpage_T *tp, typval_T *argvar)
12991{
12992 win_T *twin;
12993 int nr = 1;
12994 win_T *wp;
12995 char_u *arg;
12996
12997 twin = (tp == curtab) ? curwin : tp->tp_curwin;
12998 if (argvar->v_type != VAR_UNKNOWN)
12999 {
13000 arg = get_tv_string_chk(argvar);
13001 if (arg == NULL)
13002 nr = 0; /* type error; errmsg already given */
13003 else if (STRCMP(arg, "$") == 0)
13004 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
13005 else if (STRCMP(arg, "#") == 0)
13006 {
13007 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
13008 if (twin == NULL)
13009 nr = 0;
13010 }
13011 else
13012 {
13013 EMSG2(_(e_invexpr2), arg);
13014 nr = 0;
13015 }
13016 }
13017
13018 if (nr > 0)
13019 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
13020 wp != twin; wp = wp->w_next)
13021 {
13022 if (wp == NULL)
13023 {
13024 /* didn't find it in this tabpage */
13025 nr = 0;
13026 break;
13027 }
13028 ++nr;
13029 }
13030 return nr;
13031}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013032
13033/*
13034 * "tabpagewinnr()" function
13035 */
13036 static void
13037f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
13038{
13039 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013040 tabpage_T *tp;
13041
13042 tp = find_tabpage((int)get_tv_number(&argvars[0]));
13043 if (tp == NULL)
13044 nr = 0;
13045 else
13046 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013047 rettv->vval.v_number = nr;
13048}
13049
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013050/*
13051 * "tagfiles()" function
13052 */
13053 static void
13054f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
13055{
13056 char_u *fname;
13057 tagname_T tn;
13058 int first;
13059
13060 if (rettv_list_alloc(rettv) == FAIL)
13061 return;
13062 fname = alloc(MAXPATHL);
13063 if (fname == NULL)
13064 return;
13065
13066 for (first = TRUE; ; first = FALSE)
13067 if (get_tagfname(&tn, first, fname) == FAIL
13068 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
13069 break;
13070 tagname_free(&tn);
13071 vim_free(fname);
13072}
13073
13074/*
13075 * "taglist()" function
13076 */
13077 static void
13078f_taglist(typval_T *argvars, typval_T *rettv)
13079{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013080 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013081 char_u *tag_pattern;
13082
13083 tag_pattern = get_tv_string(&argvars[0]);
13084
13085 rettv->vval.v_number = FALSE;
13086 if (*tag_pattern == NUL)
13087 return;
13088
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013089 if (argvars[1].v_type != VAR_UNKNOWN)
13090 fname = get_tv_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013091 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013092 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013093}
13094
13095/*
13096 * "tempname()" function
13097 */
13098 static void
13099f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
13100{
13101 static int x = 'A';
13102
13103 rettv->v_type = VAR_STRING;
13104 rettv->vval.v_string = vim_tempname(x, FALSE);
13105
13106 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
13107 * names. Skip 'I' and 'O', they are used for shell redirection. */
13108 do
13109 {
13110 if (x == 'Z')
13111 x = '0';
13112 else if (x == '9')
13113 x = 'A';
13114 else
13115 {
13116#ifdef EBCDIC
13117 if (x == 'I')
13118 x = 'J';
13119 else if (x == 'R')
13120 x = 'S';
13121 else
13122#endif
13123 ++x;
13124 }
13125 } while (x == 'I' || x == 'O');
13126}
13127
13128#ifdef FEAT_FLOAT
13129/*
13130 * "tan()" function
13131 */
13132 static void
13133f_tan(typval_T *argvars, typval_T *rettv)
13134{
13135 float_T f = 0.0;
13136
13137 rettv->v_type = VAR_FLOAT;
13138 if (get_float_arg(argvars, &f) == OK)
13139 rettv->vval.v_float = tan(f);
13140 else
13141 rettv->vval.v_float = 0.0;
13142}
13143
13144/*
13145 * "tanh()" function
13146 */
13147 static void
13148f_tanh(typval_T *argvars, typval_T *rettv)
13149{
13150 float_T f = 0.0;
13151
13152 rettv->v_type = VAR_FLOAT;
13153 if (get_float_arg(argvars, &f) == OK)
13154 rettv->vval.v_float = tanh(f);
13155 else
13156 rettv->vval.v_float = 0.0;
13157}
13158#endif
13159
13160/*
13161 * "test_alloc_fail(id, countdown, repeat)" function
13162 */
13163 static void
13164f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
13165{
13166 if (argvars[0].v_type != VAR_NUMBER
13167 || argvars[0].vval.v_number <= 0
13168 || argvars[1].v_type != VAR_NUMBER
13169 || argvars[1].vval.v_number < 0
13170 || argvars[2].v_type != VAR_NUMBER)
13171 EMSG(_(e_invarg));
13172 else
13173 {
13174 alloc_fail_id = argvars[0].vval.v_number;
13175 if (alloc_fail_id >= aid_last)
13176 EMSG(_(e_invarg));
13177 alloc_fail_countdown = argvars[1].vval.v_number;
13178 alloc_fail_repeat = argvars[2].vval.v_number;
13179 did_outofmem_msg = FALSE;
13180 }
13181}
13182
13183/*
13184 * "test_autochdir()"
13185 */
13186 static void
13187f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13188{
13189#if defined(FEAT_AUTOCHDIR)
13190 test_autochdir = TRUE;
13191#endif
13192}
13193
13194/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020013195 * "test_feedinput()"
13196 */
13197 static void
13198f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
13199{
13200#ifdef USE_INPUT_BUF
13201 char_u *val = get_tv_string_chk(&argvars[0]);
13202
13203 if (val != NULL)
13204 {
13205 trash_input_buf();
13206 add_to_input_buf_csi(val, (int)STRLEN(val));
13207 }
13208#endif
13209}
13210
13211/*
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013212 * "test_option_not_set({name})" function
13213 */
13214 static void
13215f_test_option_not_set(typval_T *argvars, typval_T *rettv UNUSED)
13216{
13217 char_u *name = (char_u *)"";
13218
13219 if (argvars[0].v_type != VAR_STRING)
13220 EMSG(_(e_invarg));
13221 else
13222 {
Bram Moolenaarbf1c1b82018-09-13 21:30:05 +020013223 name = get_tv_string(&argvars[0]);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013224 if (reset_option_was_set(name) == FAIL)
13225 EMSG2(_(e_invarg2), name);
13226 }
13227}
13228
13229/*
13230 * "test_override({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013231 */
13232 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013233f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013234{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013235 char_u *name = (char_u *)"";
13236 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013237 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013238
13239 if (argvars[0].v_type != VAR_STRING
13240 || (argvars[1].v_type) != VAR_NUMBER)
13241 EMSG(_(e_invarg));
13242 else
13243 {
13244 name = get_tv_string_chk(&argvars[0]);
13245 val = (int)get_tv_number(&argvars[1]);
13246
13247 if (STRCMP(name, (char_u *)"redraw") == 0)
13248 disable_redraw_for_testing = val;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020013249 else if (STRCMP(name, (char_u *)"redraw_flag") == 0)
13250 ignore_redraw_flag_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013251 else if (STRCMP(name, (char_u *)"char_avail") == 0)
13252 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013253 else if (STRCMP(name, (char_u *)"starting") == 0)
13254 {
13255 if (val)
13256 {
13257 if (save_starting < 0)
13258 save_starting = starting;
13259 starting = 0;
13260 }
13261 else
13262 {
13263 starting = save_starting;
13264 save_starting = -1;
13265 }
13266 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013267 else if (STRCMP(name, (char_u *)"nfa_fail") == 0)
13268 nfa_fail_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013269 else if (STRCMP(name, (char_u *)"ALL") == 0)
13270 {
13271 disable_char_avail_for_testing = FALSE;
13272 disable_redraw_for_testing = FALSE;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020013273 ignore_redraw_flag_for_testing = FALSE;
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013274 nfa_fail_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013275 if (save_starting >= 0)
13276 {
13277 starting = save_starting;
13278 save_starting = -1;
13279 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013280 }
13281 else
13282 EMSG2(_(e_invarg2), name);
13283 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013284}
13285
13286/*
13287 * "test_garbagecollect_now()" function
13288 */
13289 static void
13290f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13291{
13292 /* This is dangerous, any Lists and Dicts used internally may be freed
13293 * while still in use. */
13294 garbage_collect(TRUE);
13295}
13296
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013297/*
13298 * "test_ignore_error()" function
13299 */
13300 static void
13301f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
13302{
13303 ignore_error_for_testing(get_tv_string(&argvars[0]));
13304}
13305
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013306#ifdef FEAT_JOB_CHANNEL
13307 static void
13308f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
13309{
13310 rettv->v_type = VAR_CHANNEL;
13311 rettv->vval.v_channel = NULL;
13312}
13313#endif
13314
13315 static void
13316f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
13317{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013318 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013319}
13320
13321#ifdef FEAT_JOB_CHANNEL
13322 static void
13323f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
13324{
13325 rettv->v_type = VAR_JOB;
13326 rettv->vval.v_job = NULL;
13327}
13328#endif
13329
13330 static void
13331f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
13332{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013333 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013334}
13335
13336 static void
13337f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
13338{
13339 rettv->v_type = VAR_PARTIAL;
13340 rettv->vval.v_partial = NULL;
13341}
13342
13343 static void
13344f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
13345{
13346 rettv->v_type = VAR_STRING;
13347 rettv->vval.v_string = NULL;
13348}
13349
Bram Moolenaarab186732018-09-14 21:27:06 +020013350#ifdef FEAT_GUI
13351 static void
13352f_test_scrollbar(typval_T *argvars, typval_T *rettv UNUSED)
13353{
13354 char_u *which;
13355 long value;
13356 int dragging;
13357 scrollbar_T *sb = NULL;
13358
13359 if (argvars[0].v_type != VAR_STRING
13360 || (argvars[1].v_type) != VAR_NUMBER
13361 || (argvars[2].v_type) != VAR_NUMBER)
13362 {
13363 EMSG(_(e_invarg));
13364 return;
13365 }
13366 which = get_tv_string(&argvars[0]);
13367 value = get_tv_number(&argvars[1]);
13368 dragging = get_tv_number(&argvars[2]);
13369
13370 if (STRCMP(which, "left") == 0)
13371 sb = &curwin->w_scrollbars[SBAR_LEFT];
13372 else if (STRCMP(which, "right") == 0)
13373 sb = &curwin->w_scrollbars[SBAR_RIGHT];
13374 else if (STRCMP(which, "hor") == 0)
13375 sb = &gui.bottom_sbar;
13376 if (sb == NULL)
13377 {
13378 EMSG2(_(e_invarg2), which);
13379 return;
13380 }
13381 gui_drag_scrollbar(sb, value, dragging);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020013382# ifndef USE_ON_FLY_SCROLL
13383 // need to loop through normal_cmd() to handle the scroll events
13384 exec_normal(FALSE, TRUE, FALSE);
13385# endif
Bram Moolenaarab186732018-09-14 21:27:06 +020013386}
13387#endif
13388
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013389 static void
13390f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
13391{
13392 time_for_testing = (time_t)get_tv_number(&argvars[0]);
13393}
13394
13395#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
13396/*
13397 * Get a callback from "arg". It can be a Funcref or a function name.
13398 * When "arg" is zero return an empty string.
13399 * Return NULL for an invalid argument.
13400 */
13401 char_u *
13402get_callback(typval_T *arg, partial_T **pp)
13403{
13404 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
13405 {
13406 *pp = arg->vval.v_partial;
13407 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020013408 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013409 }
13410 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020013411 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013412 {
13413 func_ref(arg->vval.v_string);
13414 return arg->vval.v_string;
13415 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013416 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
13417 return (char_u *)"";
13418 EMSG(_("E921: Invalid callback argument"));
13419 return NULL;
13420}
13421
13422/*
Bram Moolenaard5359b22018-04-05 22:44:39 +020013423 * Unref/free "callback" and "partial" returned by get_callback().
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013424 */
13425 void
13426free_callback(char_u *callback, partial_T *partial)
13427{
13428 if (partial != NULL)
13429 partial_unref(partial);
13430 else if (callback != NULL)
13431 {
13432 func_unref(callback);
13433 vim_free(callback);
13434 }
13435}
13436#endif
13437
13438#ifdef FEAT_TIMERS
13439/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020013440 * "timer_info([timer])" function
13441 */
13442 static void
13443f_timer_info(typval_T *argvars, typval_T *rettv)
13444{
13445 timer_T *timer = NULL;
13446
13447 if (rettv_list_alloc(rettv) != OK)
13448 return;
13449 if (argvars[0].v_type != VAR_UNKNOWN)
13450 {
13451 if (argvars[0].v_type != VAR_NUMBER)
13452 EMSG(_(e_number_exp));
13453 else
13454 {
13455 timer = find_timer((int)get_tv_number(&argvars[0]));
13456 if (timer != NULL)
13457 add_timer_info(rettv, timer);
13458 }
13459 }
13460 else
13461 add_timer_info_all(rettv);
13462}
13463
13464/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013465 * "timer_pause(timer, paused)" function
13466 */
13467 static void
13468f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
13469{
13470 timer_T *timer = NULL;
13471 int paused = (int)get_tv_number(&argvars[1]);
13472
13473 if (argvars[0].v_type != VAR_NUMBER)
13474 EMSG(_(e_number_exp));
13475 else
13476 {
13477 timer = find_timer((int)get_tv_number(&argvars[0]));
13478 if (timer != NULL)
13479 timer->tr_paused = paused;
13480 }
13481}
13482
13483/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013484 * "timer_start(time, callback [, options])" function
13485 */
13486 static void
13487f_timer_start(typval_T *argvars, typval_T *rettv)
13488{
Bram Moolenaar75537a92016-09-05 22:45:28 +020013489 long msec = (long)get_tv_number(&argvars[0]);
13490 timer_T *timer;
13491 int repeat = 0;
13492 char_u *callback;
13493 dict_T *dict;
13494 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013495
Bram Moolenaar75537a92016-09-05 22:45:28 +020013496 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013497 if (check_secure())
13498 return;
13499 if (argvars[2].v_type != VAR_UNKNOWN)
13500 {
13501 if (argvars[2].v_type != VAR_DICT
13502 || (dict = argvars[2].vval.v_dict) == NULL)
13503 {
13504 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
13505 return;
13506 }
13507 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
13508 repeat = get_dict_number(dict, (char_u *)"repeat");
13509 }
13510
Bram Moolenaar75537a92016-09-05 22:45:28 +020013511 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013512 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020013513 return;
13514
13515 timer = create_timer(msec, repeat);
13516 if (timer == NULL)
13517 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013518 else
13519 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020013520 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020013521 timer->tr_callback = vim_strsave(callback);
13522 else
13523 /* pointer into the partial */
13524 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020013525 timer->tr_partial = partial;
13526 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013527 }
13528}
13529
13530/*
13531 * "timer_stop(timer)" function
13532 */
13533 static void
13534f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
13535{
13536 timer_T *timer;
13537
13538 if (argvars[0].v_type != VAR_NUMBER)
13539 {
13540 EMSG(_(e_number_exp));
13541 return;
13542 }
13543 timer = find_timer((int)get_tv_number(&argvars[0]));
13544 if (timer != NULL)
13545 stop_timer(timer);
13546}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013547
13548/*
13549 * "timer_stopall()" function
13550 */
13551 static void
13552f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13553{
13554 stop_all_timers();
13555}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013556#endif
13557
13558/*
13559 * "tolower(string)" function
13560 */
13561 static void
13562f_tolower(typval_T *argvars, typval_T *rettv)
13563{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013564 rettv->v_type = VAR_STRING;
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +010013565 rettv->vval.v_string = strlow_save(get_tv_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013566}
13567
13568/*
13569 * "toupper(string)" function
13570 */
13571 static void
13572f_toupper(typval_T *argvars, typval_T *rettv)
13573{
13574 rettv->v_type = VAR_STRING;
13575 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
13576}
13577
13578/*
13579 * "tr(string, fromstr, tostr)" function
13580 */
13581 static void
13582f_tr(typval_T *argvars, typval_T *rettv)
13583{
13584 char_u *in_str;
13585 char_u *fromstr;
13586 char_u *tostr;
13587 char_u *p;
13588#ifdef FEAT_MBYTE
13589 int inlen;
13590 int fromlen;
13591 int tolen;
13592 int idx;
13593 char_u *cpstr;
13594 int cplen;
13595 int first = TRUE;
13596#endif
13597 char_u buf[NUMBUFLEN];
13598 char_u buf2[NUMBUFLEN];
13599 garray_T ga;
13600
13601 in_str = get_tv_string(&argvars[0]);
13602 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
13603 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
13604
13605 /* Default return value: empty string. */
13606 rettv->v_type = VAR_STRING;
13607 rettv->vval.v_string = NULL;
13608 if (fromstr == NULL || tostr == NULL)
13609 return; /* type error; errmsg already given */
13610 ga_init2(&ga, (int)sizeof(char), 80);
13611
13612#ifdef FEAT_MBYTE
13613 if (!has_mbyte)
13614#endif
13615 /* not multi-byte: fromstr and tostr must be the same length */
13616 if (STRLEN(fromstr) != STRLEN(tostr))
13617 {
13618#ifdef FEAT_MBYTE
13619error:
13620#endif
13621 EMSG2(_(e_invarg2), fromstr);
13622 ga_clear(&ga);
13623 return;
13624 }
13625
13626 /* fromstr and tostr have to contain the same number of chars */
13627 while (*in_str != NUL)
13628 {
13629#ifdef FEAT_MBYTE
13630 if (has_mbyte)
13631 {
13632 inlen = (*mb_ptr2len)(in_str);
13633 cpstr = in_str;
13634 cplen = inlen;
13635 idx = 0;
13636 for (p = fromstr; *p != NUL; p += fromlen)
13637 {
13638 fromlen = (*mb_ptr2len)(p);
13639 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
13640 {
13641 for (p = tostr; *p != NUL; p += tolen)
13642 {
13643 tolen = (*mb_ptr2len)(p);
13644 if (idx-- == 0)
13645 {
13646 cplen = tolen;
13647 cpstr = p;
13648 break;
13649 }
13650 }
13651 if (*p == NUL) /* tostr is shorter than fromstr */
13652 goto error;
13653 break;
13654 }
13655 ++idx;
13656 }
13657
13658 if (first && cpstr == in_str)
13659 {
13660 /* Check that fromstr and tostr have the same number of
13661 * (multi-byte) characters. Done only once when a character
13662 * of in_str doesn't appear in fromstr. */
13663 first = FALSE;
13664 for (p = tostr; *p != NUL; p += tolen)
13665 {
13666 tolen = (*mb_ptr2len)(p);
13667 --idx;
13668 }
13669 if (idx != 0)
13670 goto error;
13671 }
13672
13673 (void)ga_grow(&ga, cplen);
13674 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
13675 ga.ga_len += cplen;
13676
13677 in_str += inlen;
13678 }
13679 else
13680#endif
13681 {
13682 /* When not using multi-byte chars we can do it faster. */
13683 p = vim_strchr(fromstr, *in_str);
13684 if (p != NULL)
13685 ga_append(&ga, tostr[p - fromstr]);
13686 else
13687 ga_append(&ga, *in_str);
13688 ++in_str;
13689 }
13690 }
13691
13692 /* add a terminating NUL */
13693 (void)ga_grow(&ga, 1);
13694 ga_append(&ga, NUL);
13695
13696 rettv->vval.v_string = ga.ga_data;
13697}
13698
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010013699/*
13700 * "trim({expr})" function
13701 */
13702 static void
13703f_trim(typval_T *argvars, typval_T *rettv)
13704{
13705 char_u buf1[NUMBUFLEN];
13706 char_u buf2[NUMBUFLEN];
13707 char_u *head = get_tv_string_buf_chk(&argvars[0], buf1);
13708 char_u *mask = NULL;
13709 char_u *tail;
13710 char_u *prev;
13711 char_u *p;
13712 int c1;
13713
13714 rettv->v_type = VAR_STRING;
13715 if (head == NULL)
13716 {
13717 rettv->vval.v_string = NULL;
13718 return;
13719 }
13720
13721 if (argvars[1].v_type == VAR_STRING)
13722 mask = get_tv_string_buf_chk(&argvars[1], buf2);
13723
13724 while (*head != NUL)
13725 {
13726 c1 = PTR2CHAR(head);
13727 if (mask == NULL)
13728 {
13729 if (c1 > ' ' && c1 != 0xa0)
13730 break;
13731 }
13732 else
13733 {
13734 for (p = mask; *p != NUL; MB_PTR_ADV(p))
13735 if (c1 == PTR2CHAR(p))
13736 break;
13737 if (*p == NUL)
13738 break;
13739 }
13740 MB_PTR_ADV(head);
13741 }
13742
13743 for (tail = head + STRLEN(head); tail > head; tail = prev)
13744 {
13745 prev = tail;
13746 MB_PTR_BACK(head, prev);
13747 c1 = PTR2CHAR(prev);
13748 if (mask == NULL)
13749 {
13750 if (c1 > ' ' && c1 != 0xa0)
13751 break;
13752 }
13753 else
13754 {
13755 for (p = mask; *p != NUL; MB_PTR_ADV(p))
13756 if (c1 == PTR2CHAR(p))
13757 break;
13758 if (*p == NUL)
13759 break;
13760 }
13761 }
13762 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
13763}
13764
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013765#ifdef FEAT_FLOAT
13766/*
13767 * "trunc({float})" function
13768 */
13769 static void
13770f_trunc(typval_T *argvars, typval_T *rettv)
13771{
13772 float_T f = 0.0;
13773
13774 rettv->v_type = VAR_FLOAT;
13775 if (get_float_arg(argvars, &f) == OK)
13776 /* trunc() is not in C90, use floor() or ceil() instead. */
13777 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
13778 else
13779 rettv->vval.v_float = 0.0;
13780}
13781#endif
13782
13783/*
13784 * "type(expr)" function
13785 */
13786 static void
13787f_type(typval_T *argvars, typval_T *rettv)
13788{
13789 int n = -1;
13790
13791 switch (argvars[0].v_type)
13792 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020013793 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
13794 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013795 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020013796 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
13797 case VAR_LIST: n = VAR_TYPE_LIST; break;
13798 case VAR_DICT: n = VAR_TYPE_DICT; break;
13799 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013800 case VAR_SPECIAL:
13801 if (argvars[0].vval.v_number == VVAL_FALSE
13802 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020013803 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013804 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020013805 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013806 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020013807 case VAR_JOB: n = VAR_TYPE_JOB; break;
13808 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013809 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010013810 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013811 n = -1;
13812 break;
13813 }
13814 rettv->vval.v_number = n;
13815}
13816
13817/*
13818 * "undofile(name)" function
13819 */
13820 static void
13821f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
13822{
13823 rettv->v_type = VAR_STRING;
13824#ifdef FEAT_PERSISTENT_UNDO
13825 {
13826 char_u *fname = get_tv_string(&argvars[0]);
13827
13828 if (*fname == NUL)
13829 {
13830 /* If there is no file name there will be no undo file. */
13831 rettv->vval.v_string = NULL;
13832 }
13833 else
13834 {
13835 char_u *ffname = FullName_save(fname, FALSE);
13836
13837 if (ffname != NULL)
13838 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
13839 vim_free(ffname);
13840 }
13841 }
13842#else
13843 rettv->vval.v_string = NULL;
13844#endif
13845}
13846
13847/*
13848 * "undotree()" function
13849 */
13850 static void
13851f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
13852{
13853 if (rettv_dict_alloc(rettv) == OK)
13854 {
13855 dict_T *dict = rettv->vval.v_dict;
13856 list_T *list;
13857
Bram Moolenaare0be1672018-07-08 16:50:37 +020013858 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
13859 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
13860 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
13861 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
13862 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
13863 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013864
13865 list = list_alloc();
13866 if (list != NULL)
13867 {
13868 u_eval_tree(curbuf->b_u_oldhead, list);
13869 dict_add_list(dict, "entries", list);
13870 }
13871 }
13872}
13873
13874/*
13875 * "values(dict)" function
13876 */
13877 static void
13878f_values(typval_T *argvars, typval_T *rettv)
13879{
13880 dict_list(argvars, rettv, 1);
13881}
13882
13883/*
13884 * "virtcol(string)" function
13885 */
13886 static void
13887f_virtcol(typval_T *argvars, typval_T *rettv)
13888{
13889 colnr_T vcol = 0;
13890 pos_T *fp;
13891 int fnum = curbuf->b_fnum;
13892
13893 fp = var2fpos(&argvars[0], FALSE, &fnum);
13894 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
13895 && fnum == curbuf->b_fnum)
13896 {
13897 getvvcol(curwin, fp, NULL, NULL, &vcol);
13898 ++vcol;
13899 }
13900
13901 rettv->vval.v_number = vcol;
13902}
13903
13904/*
13905 * "visualmode()" function
13906 */
13907 static void
13908f_visualmode(typval_T *argvars, typval_T *rettv)
13909{
13910 char_u str[2];
13911
13912 rettv->v_type = VAR_STRING;
13913 str[0] = curbuf->b_visual_mode_eval;
13914 str[1] = NUL;
13915 rettv->vval.v_string = vim_strsave(str);
13916
13917 /* A non-zero number or non-empty string argument: reset mode. */
13918 if (non_zero_arg(&argvars[0]))
13919 curbuf->b_visual_mode_eval = NUL;
13920}
13921
13922/*
13923 * "wildmenumode()" function
13924 */
13925 static void
13926f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13927{
13928#ifdef FEAT_WILDMENU
13929 if (wild_menu_showing)
13930 rettv->vval.v_number = 1;
13931#endif
13932}
13933
13934/*
13935 * "winbufnr(nr)" function
13936 */
13937 static void
13938f_winbufnr(typval_T *argvars, typval_T *rettv)
13939{
13940 win_T *wp;
13941
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020013942 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013943 if (wp == NULL)
13944 rettv->vval.v_number = -1;
13945 else
13946 rettv->vval.v_number = wp->w_buffer->b_fnum;
13947}
13948
13949/*
13950 * "wincol()" function
13951 */
13952 static void
13953f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
13954{
13955 validate_cursor();
13956 rettv->vval.v_number = curwin->w_wcol + 1;
13957}
13958
13959/*
13960 * "winheight(nr)" function
13961 */
13962 static void
13963f_winheight(typval_T *argvars, typval_T *rettv)
13964{
13965 win_T *wp;
13966
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020013967 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013968 if (wp == NULL)
13969 rettv->vval.v_number = -1;
13970 else
13971 rettv->vval.v_number = wp->w_height;
13972}
13973
13974/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020013975 * "winlayout()" function
13976 */
13977 static void
13978f_winlayout(typval_T *argvars, typval_T *rettv)
13979{
13980 tabpage_T *tp;
13981
13982 if (rettv_list_alloc(rettv) != OK)
13983 return;
13984
13985 if (argvars[0].v_type == VAR_UNKNOWN)
13986 tp = curtab;
13987 else
13988 {
13989 tp = find_tabpage((int)get_tv_number(&argvars[0]));
13990 if (tp == NULL)
13991 return;
13992 }
13993
13994 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
13995}
13996
13997/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013998 * "winline()" function
13999 */
14000 static void
14001f_winline(typval_T *argvars UNUSED, typval_T *rettv)
14002{
14003 validate_cursor();
14004 rettv->vval.v_number = curwin->w_wrow + 1;
14005}
14006
14007/*
14008 * "winnr()" function
14009 */
14010 static void
14011f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
14012{
14013 int nr = 1;
14014
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014015 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014016 rettv->vval.v_number = nr;
14017}
14018
14019/*
14020 * "winrestcmd()" function
14021 */
14022 static void
14023f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
14024{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014025 win_T *wp;
14026 int winnr = 1;
14027 garray_T ga;
14028 char_u buf[50];
14029
14030 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020014031 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014032 {
14033 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
14034 ga_concat(&ga, buf);
14035 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
14036 ga_concat(&ga, buf);
14037 ++winnr;
14038 }
14039 ga_append(&ga, NUL);
14040
14041 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014042 rettv->v_type = VAR_STRING;
14043}
14044
14045/*
14046 * "winrestview()" function
14047 */
14048 static void
14049f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
14050{
14051 dict_T *dict;
14052
14053 if (argvars[0].v_type != VAR_DICT
14054 || (dict = argvars[0].vval.v_dict) == NULL)
14055 EMSG(_(e_invarg));
14056 else
14057 {
14058 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
14059 curwin->w_cursor.lnum = (linenr_T)get_dict_number(dict, (char_u *)"lnum");
14060 if (dict_find(dict, (char_u *)"col", -1) != NULL)
14061 curwin->w_cursor.col = (colnr_T)get_dict_number(dict, (char_u *)"col");
14062#ifdef FEAT_VIRTUALEDIT
14063 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
14064 curwin->w_cursor.coladd = (colnr_T)get_dict_number(dict, (char_u *)"coladd");
14065#endif
14066 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
14067 {
14068 curwin->w_curswant = (colnr_T)get_dict_number(dict, (char_u *)"curswant");
14069 curwin->w_set_curswant = FALSE;
14070 }
14071
14072 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
14073 set_topline(curwin, (linenr_T)get_dict_number(dict, (char_u *)"topline"));
14074#ifdef FEAT_DIFF
14075 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
14076 curwin->w_topfill = (int)get_dict_number(dict, (char_u *)"topfill");
14077#endif
14078 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
14079 curwin->w_leftcol = (colnr_T)get_dict_number(dict, (char_u *)"leftcol");
14080 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
14081 curwin->w_skipcol = (colnr_T)get_dict_number(dict, (char_u *)"skipcol");
14082
14083 check_cursor();
14084 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020014085 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014086 changed_window_setting();
14087
14088 if (curwin->w_topline <= 0)
14089 curwin->w_topline = 1;
14090 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
14091 curwin->w_topline = curbuf->b_ml.ml_line_count;
14092#ifdef FEAT_DIFF
14093 check_topfill(curwin, TRUE);
14094#endif
14095 }
14096}
14097
14098/*
14099 * "winsaveview()" function
14100 */
14101 static void
14102f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
14103{
14104 dict_T *dict;
14105
14106 if (rettv_dict_alloc(rettv) == FAIL)
14107 return;
14108 dict = rettv->vval.v_dict;
14109
Bram Moolenaare0be1672018-07-08 16:50:37 +020014110 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
14111 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014112#ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +020014113 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014114#endif
14115 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020014116 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014117
Bram Moolenaare0be1672018-07-08 16:50:37 +020014118 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014119#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020014120 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014121#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020014122 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
14123 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014124}
14125
14126/*
14127 * "winwidth(nr)" function
14128 */
14129 static void
14130f_winwidth(typval_T *argvars, typval_T *rettv)
14131{
14132 win_T *wp;
14133
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014134 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014135 if (wp == NULL)
14136 rettv->vval.v_number = -1;
14137 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014138 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014139}
14140
14141/*
14142 * "wordcount()" function
14143 */
14144 static void
14145f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
14146{
14147 if (rettv_dict_alloc(rettv) == FAIL)
14148 return;
14149 cursor_pos_info(rettv->vval.v_dict);
14150}
14151
14152/*
14153 * "writefile()" function
14154 */
14155 static void
14156f_writefile(typval_T *argvars, typval_T *rettv)
14157{
14158 int binary = FALSE;
14159 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014160#ifdef HAVE_FSYNC
14161 int do_fsync = p_fs;
14162#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014163 char_u *fname;
14164 FILE *fd;
14165 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014166 listitem_T *li;
14167 list_T *list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014168
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014169 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014170 if (check_restricted() || check_secure())
14171 return;
14172
14173 if (argvars[0].v_type != VAR_LIST)
14174 {
14175 EMSG2(_(e_listarg), "writefile()");
14176 return;
14177 }
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014178 list = argvars[0].vval.v_list;
14179 if (list == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014180 return;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014181 for (li = list->lv_first; li != NULL; li = li->li_next)
14182 if (get_tv_string_chk(&li->li_tv) == NULL)
14183 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014184
14185 if (argvars[2].v_type != VAR_UNKNOWN)
14186 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014187 char_u *arg2 = get_tv_string_chk(&argvars[2]);
14188
14189 if (arg2 == NULL)
14190 return;
14191 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014192 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014193 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014194 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014195#ifdef HAVE_FSYNC
14196 if (vim_strchr(arg2, 's') != NULL)
14197 do_fsync = TRUE;
14198 else if (vim_strchr(arg2, 'S') != NULL)
14199 do_fsync = FALSE;
14200#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014201 }
14202
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014203 fname = get_tv_string_chk(&argvars[1]);
14204 if (fname == NULL)
14205 return;
14206
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014207 /* Always open the file in binary mode, library functions have a mind of
14208 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014209 if (*fname == NUL || (fd = mch_fopen((char *)fname,
14210 append ? APPENDBIN : WRITEBIN)) == NULL)
14211 {
14212 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
14213 ret = -1;
14214 }
14215 else
14216 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014217 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014218 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014219#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010014220 else if (do_fsync)
14221 /* Ignore the error, the user wouldn't know what to do about it.
14222 * May happen for a device. */
Bram Moolenaar42335f52018-09-13 15:33:43 +020014223 vim_ignored = fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014224#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014225 fclose(fd);
14226 }
14227
14228 rettv->vval.v_number = ret;
14229}
14230
14231/*
14232 * "xor(expr, expr)" function
14233 */
14234 static void
14235f_xor(typval_T *argvars, typval_T *rettv)
14236{
14237 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14238 ^ get_tv_number_chk(&argvars[1], NULL);
14239}
14240
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014241#endif /* FEAT_EVAL */